blob: 3c592e7e377a584b0d5a8dc484e1ef83401299f8 [file] [log] [blame]
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_mips.h"
18
Alexey Frunze4147fcc2017-06-17 19:57:27 -070019#include "arch/mips/asm_support_mips.h"
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020020#include "arch/mips/entrypoints_direct_mips.h"
21#include "arch/mips/instruction_set_features_mips.h"
22#include "art_method.h"
Vladimir Marko94ec2db2017-09-06 17:21:03 +010023#include "class_table.h"
Chris Larsen701566a2015-10-27 15:29:13 -070024#include "code_generator_utils.h"
Vladimir Marko3a21e382016-09-02 12:38:38 +010025#include "compiled_method.h"
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020026#include "entrypoints/quick/quick_entrypoints.h"
27#include "entrypoints/quick/quick_entrypoints_enum.h"
28#include "gc/accounting/card_table.h"
Andreas Gampe09659c22017-09-18 18:23:32 -070029#include "heap_poisoning.h"
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020030#include "intrinsics.h"
Chris Larsen701566a2015-10-27 15:29:13 -070031#include "intrinsics_mips.h"
Vladimir Markod8dbc8d2017-09-20 13:37:47 +010032#include "linker/linker_patch.h"
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020033#include "mirror/array-inl.h"
34#include "mirror/class-inl.h"
35#include "offsets.h"
36#include "thread.h"
37#include "utils/assembler.h"
38#include "utils/mips/assembler_mips.h"
39#include "utils/stack_checks.h"
40
41namespace art {
42namespace mips {
43
44static constexpr int kCurrentMethodStackOffset = 0;
45static constexpr Register kMethodRegisterArgument = A0;
46
Alexey Frunze4147fcc2017-06-17 19:57:27 -070047// Flags controlling the use of thunks for Baker read barriers.
48constexpr bool kBakerReadBarrierThunksEnableForFields = true;
49constexpr bool kBakerReadBarrierThunksEnableForArrays = true;
50constexpr bool kBakerReadBarrierThunksEnableForGcRoots = true;
51
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010052Location MipsReturnLocation(DataType::Type return_type) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020053 switch (return_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +010054 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010055 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +010056 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010057 case DataType::Type::kInt8:
58 case DataType::Type::kUint16:
59 case DataType::Type::kInt16:
60 case DataType::Type::kInt32:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020061 return Location::RegisterLocation(V0);
62
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010063 case DataType::Type::kInt64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020064 return Location::RegisterPairLocation(V0, V1);
65
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010066 case DataType::Type::kFloat32:
67 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020068 return Location::FpuRegisterLocation(F0);
69
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010070 case DataType::Type::kVoid:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020071 return Location();
72 }
73 UNREACHABLE();
74}
75
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010076Location InvokeDexCallingConventionVisitorMIPS::GetReturnLocation(DataType::Type type) const {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020077 return MipsReturnLocation(type);
78}
79
80Location InvokeDexCallingConventionVisitorMIPS::GetMethodLocation() const {
81 return Location::RegisterLocation(kMethodRegisterArgument);
82}
83
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010084Location InvokeDexCallingConventionVisitorMIPS::GetNextLocation(DataType::Type type) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020085 Location next_location;
86
87 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +010088 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010089 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +010090 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010091 case DataType::Type::kInt8:
92 case DataType::Type::kUint16:
93 case DataType::Type::kInt16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +010094 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020095 uint32_t gp_index = gp_index_++;
96 if (gp_index < calling_convention.GetNumberOfRegisters()) {
97 next_location = Location::RegisterLocation(calling_convention.GetRegisterAt(gp_index));
98 } else {
99 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
100 next_location = Location::StackSlot(stack_offset);
101 }
102 break;
103 }
104
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100105 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200106 uint32_t gp_index = gp_index_;
107 gp_index_ += 2;
108 if (gp_index + 1 < calling_convention.GetNumberOfRegisters()) {
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800109 Register reg = calling_convention.GetRegisterAt(gp_index);
110 if (reg == A1 || reg == A3) {
111 gp_index_++; // Skip A1(A3), and use A2_A3(T0_T1) instead.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200112 gp_index++;
113 }
114 Register low_even = calling_convention.GetRegisterAt(gp_index);
115 Register high_odd = calling_convention.GetRegisterAt(gp_index + 1);
116 DCHECK_EQ(low_even + 1, high_odd);
117 next_location = Location::RegisterPairLocation(low_even, high_odd);
118 } else {
119 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
120 next_location = Location::DoubleStackSlot(stack_offset);
121 }
122 break;
123 }
124
125 // Note: both float and double types are stored in even FPU registers. On 32 bit FPU, double
126 // will take up the even/odd pair, while floats are stored in even regs only.
127 // On 64 bit FPU, both double and float are stored in even registers only.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100128 case DataType::Type::kFloat32:
129 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200130 uint32_t float_index = float_index_++;
131 if (float_index < calling_convention.GetNumberOfFpuRegisters()) {
132 next_location = Location::FpuRegisterLocation(
133 calling_convention.GetFpuRegisterAt(float_index));
134 } else {
135 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100136 next_location = DataType::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
137 : Location::StackSlot(stack_offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200138 }
139 break;
140 }
141
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100142 case DataType::Type::kVoid:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200143 LOG(FATAL) << "Unexpected parameter type " << type;
144 break;
145 }
146
147 // Space on the stack is reserved for all arguments.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100148 stack_index_ += DataType::Is64BitType(type) ? 2 : 1;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200149
150 return next_location;
151}
152
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100153Location InvokeRuntimeCallingConvention::GetReturnLocation(DataType::Type type) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200154 return MipsReturnLocation(type);
155}
156
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100157// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
158#define __ down_cast<CodeGeneratorMIPS*>(codegen)->GetAssembler()-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -0700159#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMipsPointerSize, x).Int32Value()
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200160
161class BoundsCheckSlowPathMIPS : public SlowPathCodeMIPS {
162 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000163 explicit BoundsCheckSlowPathMIPS(HBoundsCheck* instruction) : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200164
165 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
166 LocationSummary* locations = instruction_->GetLocations();
167 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
168 __ Bind(GetEntryLabel());
169 if (instruction_->CanThrowIntoCatchBlock()) {
170 // Live registers will be restored in the catch block if caught.
171 SaveLiveRegisters(codegen, instruction_->GetLocations());
172 }
173 // We're moving two locations to locations that could overlap, so we need a parallel
174 // move resolver.
175 InvokeRuntimeCallingConvention calling_convention;
176 codegen->EmitParallelMoves(locations->InAt(0),
177 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100178 DataType::Type::kInt32,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200179 locations->InAt(1),
180 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100181 DataType::Type::kInt32);
Serban Constantinescufca16662016-07-14 09:21:59 +0100182 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
183 ? kQuickThrowStringBounds
184 : kQuickThrowArrayBounds;
185 mips_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100186 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200187 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
188 }
189
190 bool IsFatal() const OVERRIDE { return true; }
191
192 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathMIPS"; }
193
194 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200195 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathMIPS);
196};
197
198class DivZeroCheckSlowPathMIPS : public SlowPathCodeMIPS {
199 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000200 explicit DivZeroCheckSlowPathMIPS(HDivZeroCheck* instruction) : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200201
202 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
203 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
204 __ Bind(GetEntryLabel());
Serban Constantinescufca16662016-07-14 09:21:59 +0100205 mips_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200206 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
207 }
208
209 bool IsFatal() const OVERRIDE { return true; }
210
211 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathMIPS"; }
212
213 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200214 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathMIPS);
215};
216
217class LoadClassSlowPathMIPS : public SlowPathCodeMIPS {
218 public:
219 LoadClassSlowPathMIPS(HLoadClass* cls,
220 HInstruction* at,
221 uint32_t dex_pc,
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700222 bool do_clinit,
223 const CodeGeneratorMIPS::PcRelativePatchInfo* bss_info_high = nullptr)
224 : SlowPathCodeMIPS(at),
225 cls_(cls),
226 dex_pc_(dex_pc),
227 do_clinit_(do_clinit),
228 bss_info_high_(bss_info_high) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200229 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
230 }
231
232 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000233 LocationSummary* locations = instruction_->GetLocations();
Alexey Frunzec61c0762017-04-10 13:54:23 -0700234 Location out = locations->Out();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200235 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700236 const bool baker_or_no_read_barriers = (!kUseReadBarrier || kUseBakerReadBarrier);
Alexey Frunzec61c0762017-04-10 13:54:23 -0700237 InvokeRuntimeCallingConvention calling_convention;
238 DCHECK_EQ(instruction_->IsLoadClass(), cls_ == instruction_);
239 const bool is_load_class_bss_entry =
240 (cls_ == instruction_) && (cls_->GetLoadKind() == HLoadClass::LoadKind::kBssEntry);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200241 __ Bind(GetEntryLabel());
242 SaveLiveRegisters(codegen, locations);
243
Alexey Frunzec61c0762017-04-10 13:54:23 -0700244 // For HLoadClass/kBssEntry/kSaveEverything, make sure we preserve the address of the entry.
245 Register entry_address = kNoRegister;
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700246 if (is_load_class_bss_entry && baker_or_no_read_barriers) {
Alexey Frunzec61c0762017-04-10 13:54:23 -0700247 Register temp = locations->GetTemp(0).AsRegister<Register>();
248 bool temp_is_a0 = (temp == calling_convention.GetRegisterAt(0));
249 // In the unlucky case that `temp` is A0, we preserve the address in `out` across the
250 // kSaveEverything call.
251 entry_address = temp_is_a0 ? out.AsRegister<Register>() : temp;
252 DCHECK_NE(entry_address, calling_convention.GetRegisterAt(0));
253 if (temp_is_a0) {
254 __ Move(entry_address, temp);
255 }
256 }
257
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000258 dex::TypeIndex type_index = cls_->GetTypeIndex();
259 __ LoadConst32(calling_convention.GetRegisterAt(0), type_index.index_);
Serban Constantinescufca16662016-07-14 09:21:59 +0100260 QuickEntrypointEnum entrypoint = do_clinit_ ? kQuickInitializeStaticStorage
261 : kQuickInitializeType;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000262 mips_codegen->InvokeRuntime(entrypoint, instruction_, dex_pc_, this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200263 if (do_clinit_) {
264 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
265 } else {
266 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
267 }
268
Alexey Frunzec61c0762017-04-10 13:54:23 -0700269 // For HLoadClass/kBssEntry, store the resolved class to the BSS entry.
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700270 if (is_load_class_bss_entry && baker_or_no_read_barriers) {
Alexey Frunzec61c0762017-04-10 13:54:23 -0700271 // The class entry address was preserved in `entry_address` thanks to kSaveEverything.
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700272 DCHECK(bss_info_high_);
273 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
274 mips_codegen->NewTypeBssEntryPatch(cls_->GetDexFile(), type_index, bss_info_high_);
Alexey Frunzea663d9d2017-07-31 18:43:18 -0700275 __ Sw(calling_convention.GetRegisterAt(0),
276 entry_address,
277 /* placeholder */ 0x5678,
278 &info_low->label);
Alexey Frunzec61c0762017-04-10 13:54:23 -0700279 }
280
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200281 // Move the class to the desired location.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200282 if (out.IsValid()) {
283 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100284 DataType::Type type = instruction_->GetType();
Alexey Frunzec61c0762017-04-10 13:54:23 -0700285 mips_codegen->MoveLocation(out,
286 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
287 type);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200288 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200289 RestoreLiveRegisters(codegen, locations);
Alexey Frunzec61c0762017-04-10 13:54:23 -0700290
291 // For HLoadClass/kBssEntry, store the resolved class to the BSS entry.
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700292 if (is_load_class_bss_entry && !baker_or_no_read_barriers) {
293 // For non-Baker read barriers we need to re-calculate the address of
Alexey Frunzec61c0762017-04-10 13:54:23 -0700294 // the class entry.
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700295 const bool isR6 = mips_codegen->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +0200296 const bool has_irreducible_loops = codegen->GetGraph()->HasIrreducibleLoops();
297 Register base =
298 (isR6 || has_irreducible_loops) ? ZERO : locations->InAt(0).AsRegister<Register>();
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700299 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
Vladimir Marko1998cd02017-01-13 13:02:58 +0000300 mips_codegen->NewTypeBssEntryPatch(cls_->GetDexFile(), type_index);
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700301 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
302 mips_codegen->NewTypeBssEntryPatch(cls_->GetDexFile(), type_index, info_high);
Alexey Frunzea663d9d2017-07-31 18:43:18 -0700303 mips_codegen->EmitPcRelativeAddressPlaceholderHigh(info_high, TMP, base);
304 __ Sw(out.AsRegister<Register>(), TMP, /* placeholder */ 0x5678, &info_low->label);
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000305 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200306 __ B(GetExitLabel());
307 }
308
309 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathMIPS"; }
310
311 private:
312 // The class this slow path will load.
313 HLoadClass* const cls_;
314
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200315 // The dex PC of `at_`.
316 const uint32_t dex_pc_;
317
318 // Whether to initialize the class.
319 const bool do_clinit_;
320
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700321 // Pointer to the high half PC-relative patch info for HLoadClass/kBssEntry.
322 const CodeGeneratorMIPS::PcRelativePatchInfo* bss_info_high_;
323
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200324 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathMIPS);
325};
326
327class LoadStringSlowPathMIPS : public SlowPathCodeMIPS {
328 public:
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700329 explicit LoadStringSlowPathMIPS(HLoadString* instruction,
330 const CodeGeneratorMIPS::PcRelativePatchInfo* bss_info_high)
331 : SlowPathCodeMIPS(instruction), bss_info_high_(bss_info_high) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200332
333 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexey Frunzec61c0762017-04-10 13:54:23 -0700334 DCHECK(instruction_->IsLoadString());
335 DCHECK_EQ(instruction_->AsLoadString()->GetLoadKind(), HLoadString::LoadKind::kBssEntry);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200336 LocationSummary* locations = instruction_->GetLocations();
337 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Alexey Frunzec61c0762017-04-10 13:54:23 -0700338 HLoadString* load = instruction_->AsLoadString();
339 const dex::StringIndex string_index = load->GetStringIndex();
340 Register out = locations->Out().AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200341 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700342 const bool baker_or_no_read_barriers = (!kUseReadBarrier || kUseBakerReadBarrier);
Alexey Frunzec61c0762017-04-10 13:54:23 -0700343 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200344 __ Bind(GetEntryLabel());
345 SaveLiveRegisters(codegen, locations);
346
Alexey Frunzec61c0762017-04-10 13:54:23 -0700347 // For HLoadString/kBssEntry/kSaveEverything, make sure we preserve the address of the entry.
348 Register entry_address = kNoRegister;
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700349 if (baker_or_no_read_barriers) {
Alexey Frunzec61c0762017-04-10 13:54:23 -0700350 Register temp = locations->GetTemp(0).AsRegister<Register>();
351 bool temp_is_a0 = (temp == calling_convention.GetRegisterAt(0));
352 // In the unlucky case that `temp` is A0, we preserve the address in `out` across the
353 // kSaveEverything call.
354 entry_address = temp_is_a0 ? out : temp;
355 DCHECK_NE(entry_address, calling_convention.GetRegisterAt(0));
356 if (temp_is_a0) {
357 __ Move(entry_address, temp);
358 }
359 }
360
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000361 __ LoadConst32(calling_convention.GetRegisterAt(0), string_index.index_);
Serban Constantinescufca16662016-07-14 09:21:59 +0100362 mips_codegen->InvokeRuntime(kQuickResolveString, instruction_, instruction_->GetDexPc(), this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200363 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexey Frunzec61c0762017-04-10 13:54:23 -0700364
365 // Store the resolved string to the BSS entry.
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700366 if (baker_or_no_read_barriers) {
Alexey Frunzec61c0762017-04-10 13:54:23 -0700367 // The string entry address was preserved in `entry_address` thanks to kSaveEverything.
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700368 DCHECK(bss_info_high_);
369 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +0100370 mips_codegen->NewStringBssEntryPatch(load->GetDexFile(), string_index, bss_info_high_);
Alexey Frunzea663d9d2017-07-31 18:43:18 -0700371 __ Sw(calling_convention.GetRegisterAt(0),
372 entry_address,
373 /* placeholder */ 0x5678,
374 &info_low->label);
Alexey Frunzec61c0762017-04-10 13:54:23 -0700375 }
376
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100377 DataType::Type type = instruction_->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200378 mips_codegen->MoveLocation(locations->Out(),
Alexey Frunzec61c0762017-04-10 13:54:23 -0700379 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200380 type);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200381 RestoreLiveRegisters(codegen, locations);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000382
Alexey Frunzec61c0762017-04-10 13:54:23 -0700383 // Store the resolved string to the BSS entry.
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700384 if (!baker_or_no_read_barriers) {
385 // For non-Baker read barriers we need to re-calculate the address of
Alexey Frunzec61c0762017-04-10 13:54:23 -0700386 // the string entry.
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700387 const bool isR6 = mips_codegen->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +0200388 const bool has_irreducible_loops = codegen->GetGraph()->HasIrreducibleLoops();
389 Register base =
390 (isR6 || has_irreducible_loops) ? ZERO : locations->InAt(0).AsRegister<Register>();
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700391 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +0100392 mips_codegen->NewStringBssEntryPatch(load->GetDexFile(), string_index);
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700393 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +0100394 mips_codegen->NewStringBssEntryPatch(load->GetDexFile(), string_index, info_high);
Alexey Frunzea663d9d2017-07-31 18:43:18 -0700395 mips_codegen->EmitPcRelativeAddressPlaceholderHigh(info_high, TMP, base);
396 __ Sw(out, TMP, /* placeholder */ 0x5678, &info_low->label);
Alexey Frunzec61c0762017-04-10 13:54:23 -0700397 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200398 __ B(GetExitLabel());
399 }
400
401 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathMIPS"; }
402
403 private:
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700404 // Pointer to the high half PC-relative patch info.
405 const CodeGeneratorMIPS::PcRelativePatchInfo* bss_info_high_;
406
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200407 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathMIPS);
408};
409
410class NullCheckSlowPathMIPS : public SlowPathCodeMIPS {
411 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000412 explicit NullCheckSlowPathMIPS(HNullCheck* instr) : SlowPathCodeMIPS(instr) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200413
414 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
415 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
416 __ Bind(GetEntryLabel());
417 if (instruction_->CanThrowIntoCatchBlock()) {
418 // Live registers will be restored in the catch block if caught.
419 SaveLiveRegisters(codegen, instruction_->GetLocations());
420 }
Serban Constantinescufca16662016-07-14 09:21:59 +0100421 mips_codegen->InvokeRuntime(kQuickThrowNullPointer,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200422 instruction_,
423 instruction_->GetDexPc(),
Serban Constantinescufca16662016-07-14 09:21:59 +0100424 this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200425 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
426 }
427
428 bool IsFatal() const OVERRIDE { return true; }
429
430 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathMIPS"; }
431
432 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200433 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathMIPS);
434};
435
436class SuspendCheckSlowPathMIPS : public SlowPathCodeMIPS {
437 public:
438 SuspendCheckSlowPathMIPS(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000439 : SlowPathCodeMIPS(instruction), successor_(successor) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200440
441 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Lena Djokicca8c2952017-05-29 11:31:46 +0200442 LocationSummary* locations = instruction_->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200443 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
444 __ Bind(GetEntryLabel());
Lena Djokicca8c2952017-05-29 11:31:46 +0200445 SaveLiveRegisters(codegen, locations); // Only saves live vector registers for SIMD.
Serban Constantinescufca16662016-07-14 09:21:59 +0100446 mips_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200447 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Lena Djokicca8c2952017-05-29 11:31:46 +0200448 RestoreLiveRegisters(codegen, locations); // Only restores live vector registers for SIMD.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200449 if (successor_ == nullptr) {
450 __ B(GetReturnLabel());
451 } else {
452 __ B(mips_codegen->GetLabelOf(successor_));
453 }
454 }
455
456 MipsLabel* GetReturnLabel() {
457 DCHECK(successor_ == nullptr);
458 return &return_label_;
459 }
460
461 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathMIPS"; }
462
463 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200464 // If not null, the block to branch to after the suspend check.
465 HBasicBlock* const successor_;
466
467 // If `successor_` is null, the label to branch to after the suspend check.
468 MipsLabel return_label_;
469
470 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathMIPS);
471};
472
473class TypeCheckSlowPathMIPS : public SlowPathCodeMIPS {
474 public:
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800475 explicit TypeCheckSlowPathMIPS(HInstruction* instruction, bool is_fatal)
476 : SlowPathCodeMIPS(instruction), is_fatal_(is_fatal) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200477
478 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
479 LocationSummary* locations = instruction_->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200480 uint32_t dex_pc = instruction_->GetDexPc();
481 DCHECK(instruction_->IsCheckCast()
482 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
483 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
484
485 __ Bind(GetEntryLabel());
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800486 if (!is_fatal_) {
487 SaveLiveRegisters(codegen, locations);
488 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200489
490 // We're moving two locations to locations that could overlap, so we need a parallel
491 // move resolver.
492 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800493 codegen->EmitParallelMoves(locations->InAt(0),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200494 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100495 DataType::Type::kReference,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800496 locations->InAt(1),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200497 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100498 DataType::Type::kReference);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200499 if (instruction_->IsInstanceOf()) {
Serban Constantinescufca16662016-07-14 09:21:59 +0100500 mips_codegen->InvokeRuntime(kQuickInstanceofNonTrivial, instruction_, dex_pc, this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800501 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100502 DataType::Type ret_type = instruction_->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200503 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
504 mips_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200505 } else {
506 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800507 mips_codegen->InvokeRuntime(kQuickCheckInstanceOf, instruction_, dex_pc, this);
508 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200509 }
510
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800511 if (!is_fatal_) {
512 RestoreLiveRegisters(codegen, locations);
513 __ B(GetExitLabel());
514 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200515 }
516
517 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathMIPS"; }
518
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800519 bool IsFatal() const OVERRIDE { return is_fatal_; }
520
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200521 private:
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800522 const bool is_fatal_;
523
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200524 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathMIPS);
525};
526
527class DeoptimizationSlowPathMIPS : public SlowPathCodeMIPS {
528 public:
Aart Bik42249c32016-01-07 15:33:50 -0800529 explicit DeoptimizationSlowPathMIPS(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000530 : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200531
532 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800533 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200534 __ Bind(GetEntryLabel());
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100535 LocationSummary* locations = instruction_->GetLocations();
536 SaveLiveRegisters(codegen, locations);
537 InvokeRuntimeCallingConvention calling_convention;
538 __ LoadConst32(calling_convention.GetRegisterAt(0),
539 static_cast<uint32_t>(instruction_->AsDeoptimize()->GetDeoptimizationKind()));
Serban Constantinescufca16662016-07-14 09:21:59 +0100540 mips_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100541 CheckEntrypointTypes<kQuickDeoptimize, void, DeoptimizationKind>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200542 }
543
544 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathMIPS"; }
545
546 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200547 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathMIPS);
548};
549
Alexey Frunze15958152017-02-09 19:08:30 -0800550class ArraySetSlowPathMIPS : public SlowPathCodeMIPS {
551 public:
552 explicit ArraySetSlowPathMIPS(HInstruction* instruction) : SlowPathCodeMIPS(instruction) {}
553
554 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
555 LocationSummary* locations = instruction_->GetLocations();
556 __ Bind(GetEntryLabel());
557 SaveLiveRegisters(codegen, locations);
558
559 InvokeRuntimeCallingConvention calling_convention;
560 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
561 parallel_move.AddMove(
562 locations->InAt(0),
563 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100564 DataType::Type::kReference,
Alexey Frunze15958152017-02-09 19:08:30 -0800565 nullptr);
566 parallel_move.AddMove(
567 locations->InAt(1),
568 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100569 DataType::Type::kInt32,
Alexey Frunze15958152017-02-09 19:08:30 -0800570 nullptr);
571 parallel_move.AddMove(
572 locations->InAt(2),
573 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100574 DataType::Type::kReference,
Alexey Frunze15958152017-02-09 19:08:30 -0800575 nullptr);
576 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
577
578 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
579 mips_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
580 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
581 RestoreLiveRegisters(codegen, locations);
582 __ B(GetExitLabel());
583 }
584
585 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathMIPS"; }
586
587 private:
588 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathMIPS);
589};
590
591// Slow path marking an object reference `ref` during a read
592// barrier. The field `obj.field` in the object `obj` holding this
593// reference does not get updated by this slow path after marking (see
594// ReadBarrierMarkAndUpdateFieldSlowPathMIPS below for that).
595//
596// This means that after the execution of this slow path, `ref` will
597// always be up-to-date, but `obj.field` may not; i.e., after the
598// flip, `ref` will be a to-space reference, but `obj.field` will
599// probably still be a from-space reference (unless it gets updated by
600// another thread, or if another thread installed another object
601// reference (different from `ref`) in `obj.field`).
602//
603// If `entrypoint` is a valid location it is assumed to already be
604// holding the entrypoint. The case where the entrypoint is passed in
605// is for the GcRoot read barrier.
606class ReadBarrierMarkSlowPathMIPS : public SlowPathCodeMIPS {
607 public:
608 ReadBarrierMarkSlowPathMIPS(HInstruction* instruction,
609 Location ref,
610 Location entrypoint = Location::NoLocation())
611 : SlowPathCodeMIPS(instruction), ref_(ref), entrypoint_(entrypoint) {
612 DCHECK(kEmitCompilerReadBarrier);
613 }
614
615 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathMIPS"; }
616
617 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
618 LocationSummary* locations = instruction_->GetLocations();
619 Register ref_reg = ref_.AsRegister<Register>();
620 DCHECK(locations->CanCall());
621 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
622 DCHECK(instruction_->IsInstanceFieldGet() ||
623 instruction_->IsStaticFieldGet() ||
624 instruction_->IsArrayGet() ||
625 instruction_->IsArraySet() ||
626 instruction_->IsLoadClass() ||
627 instruction_->IsLoadString() ||
628 instruction_->IsInstanceOf() ||
629 instruction_->IsCheckCast() ||
630 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
631 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
632 << "Unexpected instruction in read barrier marking slow path: "
633 << instruction_->DebugName();
634
635 __ Bind(GetEntryLabel());
636 // No need to save live registers; it's taken care of by the
637 // entrypoint. Also, there is no need to update the stack mask,
638 // as this runtime call will not trigger a garbage collection.
639 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
640 DCHECK((V0 <= ref_reg && ref_reg <= T7) ||
641 (S2 <= ref_reg && ref_reg <= S7) ||
642 (ref_reg == FP)) << ref_reg;
643 // "Compact" slow path, saving two moves.
644 //
645 // Instead of using the standard runtime calling convention (input
646 // and output in A0 and V0 respectively):
647 //
648 // A0 <- ref
649 // V0 <- ReadBarrierMark(A0)
650 // ref <- V0
651 //
652 // we just use rX (the register containing `ref`) as input and output
653 // of a dedicated entrypoint:
654 //
655 // rX <- ReadBarrierMarkRegX(rX)
656 //
657 if (entrypoint_.IsValid()) {
658 mips_codegen->ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction_, this);
659 DCHECK_EQ(entrypoint_.AsRegister<Register>(), T9);
660 __ Jalr(entrypoint_.AsRegister<Register>());
661 __ NopIfNoReordering();
662 } else {
663 int32_t entry_point_offset =
Roland Levillain97c46462017-05-11 14:04:03 +0100664 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(ref_reg - 1);
Alexey Frunze15958152017-02-09 19:08:30 -0800665 // This runtime call does not require a stack map.
666 mips_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset,
667 instruction_,
668 this,
669 /* direct */ false);
670 }
671 __ B(GetExitLabel());
672 }
673
674 private:
675 // The location (register) of the marked object reference.
676 const Location ref_;
677
678 // The location of the entrypoint if already loaded.
679 const Location entrypoint_;
680
681 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathMIPS);
682};
683
684// Slow path marking an object reference `ref` during a read barrier,
685// and if needed, atomically updating the field `obj.field` in the
686// object `obj` holding this reference after marking (contrary to
687// ReadBarrierMarkSlowPathMIPS above, which never tries to update
688// `obj.field`).
689//
690// This means that after the execution of this slow path, both `ref`
691// and `obj.field` will be up-to-date; i.e., after the flip, both will
692// hold the same to-space reference (unless another thread installed
693// another object reference (different from `ref`) in `obj.field`).
694class ReadBarrierMarkAndUpdateFieldSlowPathMIPS : public SlowPathCodeMIPS {
695 public:
696 ReadBarrierMarkAndUpdateFieldSlowPathMIPS(HInstruction* instruction,
697 Location ref,
698 Register obj,
699 Location field_offset,
700 Register temp1)
701 : SlowPathCodeMIPS(instruction),
702 ref_(ref),
703 obj_(obj),
704 field_offset_(field_offset),
705 temp1_(temp1) {
706 DCHECK(kEmitCompilerReadBarrier);
707 }
708
709 const char* GetDescription() const OVERRIDE {
710 return "ReadBarrierMarkAndUpdateFieldSlowPathMIPS";
711 }
712
713 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
714 LocationSummary* locations = instruction_->GetLocations();
715 Register ref_reg = ref_.AsRegister<Register>();
716 DCHECK(locations->CanCall());
717 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
718 // This slow path is only used by the UnsafeCASObject intrinsic.
719 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
720 << "Unexpected instruction in read barrier marking and field updating slow path: "
721 << instruction_->DebugName();
722 DCHECK(instruction_->GetLocations()->Intrinsified());
723 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
724 DCHECK(field_offset_.IsRegisterPair()) << field_offset_;
725
726 __ Bind(GetEntryLabel());
727
728 // Save the old reference.
729 // Note that we cannot use AT or TMP to save the old reference, as those
730 // are used by the code that follows, but we need the old reference after
731 // the call to the ReadBarrierMarkRegX entry point.
732 DCHECK_NE(temp1_, AT);
733 DCHECK_NE(temp1_, TMP);
734 __ Move(temp1_, ref_reg);
735
736 // No need to save live registers; it's taken care of by the
737 // entrypoint. Also, there is no need to update the stack mask,
738 // as this runtime call will not trigger a garbage collection.
739 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
740 DCHECK((V0 <= ref_reg && ref_reg <= T7) ||
741 (S2 <= ref_reg && ref_reg <= S7) ||
742 (ref_reg == FP)) << ref_reg;
743 // "Compact" slow path, saving two moves.
744 //
745 // Instead of using the standard runtime calling convention (input
746 // and output in A0 and V0 respectively):
747 //
748 // A0 <- ref
749 // V0 <- ReadBarrierMark(A0)
750 // ref <- V0
751 //
752 // we just use rX (the register containing `ref`) as input and output
753 // of a dedicated entrypoint:
754 //
755 // rX <- ReadBarrierMarkRegX(rX)
756 //
757 int32_t entry_point_offset =
Roland Levillain97c46462017-05-11 14:04:03 +0100758 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(ref_reg - 1);
Alexey Frunze15958152017-02-09 19:08:30 -0800759 // This runtime call does not require a stack map.
760 mips_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset,
761 instruction_,
762 this,
763 /* direct */ false);
764
765 // If the new reference is different from the old reference,
766 // update the field in the holder (`*(obj_ + field_offset_)`).
767 //
768 // Note that this field could also hold a different object, if
769 // another thread had concurrently changed it. In that case, the
770 // the compare-and-set (CAS) loop below would abort, leaving the
771 // field as-is.
772 MipsLabel done;
773 __ Beq(temp1_, ref_reg, &done);
774
775 // Update the the holder's field atomically. This may fail if
776 // mutator updates before us, but it's OK. This is achieved
777 // using a strong compare-and-set (CAS) operation with relaxed
778 // memory synchronization ordering, where the expected value is
779 // the old reference and the desired value is the new reference.
780
781 // Convenience aliases.
782 Register base = obj_;
783 // The UnsafeCASObject intrinsic uses a register pair as field
784 // offset ("long offset"), of which only the low part contains
785 // data.
786 Register offset = field_offset_.AsRegisterPairLow<Register>();
787 Register expected = temp1_;
788 Register value = ref_reg;
789 Register tmp_ptr = TMP; // Pointer to actual memory.
790 Register tmp = AT; // Value in memory.
791
792 __ Addu(tmp_ptr, base, offset);
793
794 if (kPoisonHeapReferences) {
795 __ PoisonHeapReference(expected);
796 // Do not poison `value` if it is the same register as
797 // `expected`, which has just been poisoned.
798 if (value != expected) {
799 __ PoisonHeapReference(value);
800 }
801 }
802
803 // do {
804 // tmp = [r_ptr] - expected;
805 // } while (tmp == 0 && failure([r_ptr] <- r_new_value));
806
807 bool is_r6 = mips_codegen->GetInstructionSetFeatures().IsR6();
808 MipsLabel loop_head, exit_loop;
809 __ Bind(&loop_head);
810 if (is_r6) {
811 __ LlR6(tmp, tmp_ptr);
812 } else {
813 __ LlR2(tmp, tmp_ptr);
814 }
815 __ Bne(tmp, expected, &exit_loop);
816 __ Move(tmp, value);
817 if (is_r6) {
818 __ ScR6(tmp, tmp_ptr);
819 } else {
820 __ ScR2(tmp, tmp_ptr);
821 }
822 __ Beqz(tmp, &loop_head);
823 __ Bind(&exit_loop);
824
825 if (kPoisonHeapReferences) {
826 __ UnpoisonHeapReference(expected);
827 // Do not unpoison `value` if it is the same register as
828 // `expected`, which has just been unpoisoned.
829 if (value != expected) {
830 __ UnpoisonHeapReference(value);
831 }
832 }
833
834 __ Bind(&done);
835 __ B(GetExitLabel());
836 }
837
838 private:
839 // The location (register) of the marked object reference.
840 const Location ref_;
841 // The register containing the object holding the marked object reference field.
842 const Register obj_;
843 // The location of the offset of the marked reference field within `obj_`.
844 Location field_offset_;
845
846 const Register temp1_;
847
848 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkAndUpdateFieldSlowPathMIPS);
849};
850
851// Slow path generating a read barrier for a heap reference.
852class ReadBarrierForHeapReferenceSlowPathMIPS : public SlowPathCodeMIPS {
853 public:
854 ReadBarrierForHeapReferenceSlowPathMIPS(HInstruction* instruction,
855 Location out,
856 Location ref,
857 Location obj,
858 uint32_t offset,
859 Location index)
860 : SlowPathCodeMIPS(instruction),
861 out_(out),
862 ref_(ref),
863 obj_(obj),
864 offset_(offset),
865 index_(index) {
866 DCHECK(kEmitCompilerReadBarrier);
867 // If `obj` is equal to `out` or `ref`, it means the initial object
868 // has been overwritten by (or after) the heap object reference load
869 // to be instrumented, e.g.:
870 //
871 // __ LoadFromOffset(kLoadWord, out, out, offset);
872 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
873 //
874 // In that case, we have lost the information about the original
875 // object, and the emitted read barrier cannot work properly.
876 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
877 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
878 }
879
880 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
881 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
882 LocationSummary* locations = instruction_->GetLocations();
883 Register reg_out = out_.AsRegister<Register>();
884 DCHECK(locations->CanCall());
885 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
886 DCHECK(instruction_->IsInstanceFieldGet() ||
887 instruction_->IsStaticFieldGet() ||
888 instruction_->IsArrayGet() ||
889 instruction_->IsInstanceOf() ||
890 instruction_->IsCheckCast() ||
891 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
892 << "Unexpected instruction in read barrier for heap reference slow path: "
893 << instruction_->DebugName();
894
895 __ Bind(GetEntryLabel());
896 SaveLiveRegisters(codegen, locations);
897
898 // We may have to change the index's value, but as `index_` is a
899 // constant member (like other "inputs" of this slow path),
900 // introduce a copy of it, `index`.
901 Location index = index_;
902 if (index_.IsValid()) {
903 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
904 if (instruction_->IsArrayGet()) {
905 // Compute the actual memory offset and store it in `index`.
906 Register index_reg = index_.AsRegister<Register>();
907 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
908 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
909 // We are about to change the value of `index_reg` (see the
910 // calls to art::mips::MipsAssembler::Sll and
911 // art::mips::MipsAssembler::Addiu32 below), but it has
912 // not been saved by the previous call to
913 // art::SlowPathCode::SaveLiveRegisters, as it is a
914 // callee-save register --
915 // art::SlowPathCode::SaveLiveRegisters does not consider
916 // callee-save registers, as it has been designed with the
917 // assumption that callee-save registers are supposed to be
918 // handled by the called function. So, as a callee-save
919 // register, `index_reg` _would_ eventually be saved onto
920 // the stack, but it would be too late: we would have
921 // changed its value earlier. Therefore, we manually save
922 // it here into another freely available register,
923 // `free_reg`, chosen of course among the caller-save
924 // registers (as a callee-save `free_reg` register would
925 // exhibit the same problem).
926 //
927 // Note we could have requested a temporary register from
928 // the register allocator instead; but we prefer not to, as
929 // this is a slow path, and we know we can find a
930 // caller-save register that is available.
931 Register free_reg = FindAvailableCallerSaveRegister(codegen);
932 __ Move(free_reg, index_reg);
933 index_reg = free_reg;
934 index = Location::RegisterLocation(index_reg);
935 } else {
936 // The initial register stored in `index_` has already been
937 // saved in the call to art::SlowPathCode::SaveLiveRegisters
938 // (as it is not a callee-save register), so we can freely
939 // use it.
940 }
941 // Shifting the index value contained in `index_reg` by the scale
942 // factor (2) cannot overflow in practice, as the runtime is
943 // unable to allocate object arrays with a size larger than
944 // 2^26 - 1 (that is, 2^28 - 4 bytes).
945 __ Sll(index_reg, index_reg, TIMES_4);
946 static_assert(
947 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
948 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
949 __ Addiu32(index_reg, index_reg, offset_);
950 } else {
951 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
952 // intrinsics, `index_` is not shifted by a scale factor of 2
953 // (as in the case of ArrayGet), as it is actually an offset
954 // to an object field within an object.
955 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
956 DCHECK(instruction_->GetLocations()->Intrinsified());
957 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
958 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
959 << instruction_->AsInvoke()->GetIntrinsic();
960 DCHECK_EQ(offset_, 0U);
961 DCHECK(index_.IsRegisterPair());
962 // UnsafeGet's offset location is a register pair, the low
963 // part contains the correct offset.
964 index = index_.ToLow();
965 }
966 }
967
968 // We're moving two or three locations to locations that could
969 // overlap, so we need a parallel move resolver.
970 InvokeRuntimeCallingConvention calling_convention;
971 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
972 parallel_move.AddMove(ref_,
973 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100974 DataType::Type::kReference,
Alexey Frunze15958152017-02-09 19:08:30 -0800975 nullptr);
976 parallel_move.AddMove(obj_,
977 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100978 DataType::Type::kReference,
Alexey Frunze15958152017-02-09 19:08:30 -0800979 nullptr);
980 if (index.IsValid()) {
981 parallel_move.AddMove(index,
982 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100983 DataType::Type::kInt32,
Alexey Frunze15958152017-02-09 19:08:30 -0800984 nullptr);
985 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
986 } else {
987 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
988 __ LoadConst32(calling_convention.GetRegisterAt(2), offset_);
989 }
990 mips_codegen->InvokeRuntime(kQuickReadBarrierSlow,
991 instruction_,
992 instruction_->GetDexPc(),
993 this);
994 CheckEntrypointTypes<
995 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
Lena Djokic8098da92017-06-28 12:07:50 +0200996 mips_codegen->MoveLocation(out_,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100997 calling_convention.GetReturnLocation(DataType::Type::kReference),
998 DataType::Type::kReference);
Alexey Frunze15958152017-02-09 19:08:30 -0800999
1000 RestoreLiveRegisters(codegen, locations);
1001 __ B(GetExitLabel());
1002 }
1003
1004 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathMIPS"; }
1005
1006 private:
1007 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
1008 size_t ref = static_cast<int>(ref_.AsRegister<Register>());
1009 size_t obj = static_cast<int>(obj_.AsRegister<Register>());
1010 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
1011 if (i != ref &&
1012 i != obj &&
1013 !codegen->IsCoreCalleeSaveRegister(i) &&
1014 !codegen->IsBlockedCoreRegister(i)) {
1015 return static_cast<Register>(i);
1016 }
1017 }
1018 // We shall never fail to find a free caller-save register, as
1019 // there are more than two core caller-save registers on MIPS
1020 // (meaning it is possible to find one which is different from
1021 // `ref` and `obj`).
1022 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
1023 LOG(FATAL) << "Could not find a free caller-save register";
1024 UNREACHABLE();
1025 }
1026
1027 const Location out_;
1028 const Location ref_;
1029 const Location obj_;
1030 const uint32_t offset_;
1031 // An additional location containing an index to an array.
1032 // Only used for HArrayGet and the UnsafeGetObject &
1033 // UnsafeGetObjectVolatile intrinsics.
1034 const Location index_;
1035
1036 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathMIPS);
1037};
1038
1039// Slow path generating a read barrier for a GC root.
1040class ReadBarrierForRootSlowPathMIPS : public SlowPathCodeMIPS {
1041 public:
1042 ReadBarrierForRootSlowPathMIPS(HInstruction* instruction, Location out, Location root)
1043 : SlowPathCodeMIPS(instruction), out_(out), root_(root) {
1044 DCHECK(kEmitCompilerReadBarrier);
1045 }
1046
1047 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
1048 LocationSummary* locations = instruction_->GetLocations();
1049 Register reg_out = out_.AsRegister<Register>();
1050 DCHECK(locations->CanCall());
1051 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
1052 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
1053 << "Unexpected instruction in read barrier for GC root slow path: "
1054 << instruction_->DebugName();
1055
1056 __ Bind(GetEntryLabel());
1057 SaveLiveRegisters(codegen, locations);
1058
1059 InvokeRuntimeCallingConvention calling_convention;
1060 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
Lena Djokic8098da92017-06-28 12:07:50 +02001061 mips_codegen->MoveLocation(Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
1062 root_,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001063 DataType::Type::kReference);
Alexey Frunze15958152017-02-09 19:08:30 -08001064 mips_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
1065 instruction_,
1066 instruction_->GetDexPc(),
1067 this);
1068 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
Lena Djokic8098da92017-06-28 12:07:50 +02001069 mips_codegen->MoveLocation(out_,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001070 calling_convention.GetReturnLocation(DataType::Type::kReference),
1071 DataType::Type::kReference);
Alexey Frunze15958152017-02-09 19:08:30 -08001072
1073 RestoreLiveRegisters(codegen, locations);
1074 __ B(GetExitLabel());
1075 }
1076
1077 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathMIPS"; }
1078
1079 private:
1080 const Location out_;
1081 const Location root_;
1082
1083 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathMIPS);
1084};
1085
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001086CodeGeneratorMIPS::CodeGeneratorMIPS(HGraph* graph,
1087 const MipsInstructionSetFeatures& isa_features,
1088 const CompilerOptions& compiler_options,
1089 OptimizingCompilerStats* stats)
1090 : CodeGenerator(graph,
1091 kNumberOfCoreRegisters,
1092 kNumberOfFRegisters,
1093 kNumberOfRegisterPairs,
1094 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
1095 arraysize(kCoreCalleeSaves)),
1096 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
1097 arraysize(kFpuCalleeSaves)),
1098 compiler_options,
1099 stats),
1100 block_labels_(nullptr),
1101 location_builder_(graph, this),
1102 instruction_visitor_(graph, this),
1103 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +01001104 assembler_(graph->GetArena(), &isa_features),
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001105 isa_features_(isa_features),
Alexey Frunze06a46c42016-07-19 15:00:40 -07001106 uint32_literals_(std::less<uint32_t>(),
1107 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko65979462017-05-19 17:25:12 +01001108 pc_relative_method_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001109 method_bss_entry_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunze06a46c42016-07-19 15:00:40 -07001110 pc_relative_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko1998cd02017-01-13 13:02:58 +00001111 type_bss_entry_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko65979462017-05-19 17:25:12 +01001112 pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01001113 string_bss_entry_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunze627c1a02017-01-30 19:28:14 -08001114 jit_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1115 jit_class_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunze06a46c42016-07-19 15:00:40 -07001116 clobbered_ra_(false) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001117 // Save RA (containing the return address) to mimic Quick.
1118 AddAllocatedRegister(Location::RegisterLocation(RA));
1119}
1120
1121#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +01001122// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
1123#define __ down_cast<MipsAssembler*>(GetAssembler())-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -07001124#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMipsPointerSize, x).Int32Value()
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001125
1126void CodeGeneratorMIPS::Finalize(CodeAllocator* allocator) {
1127 // Ensure that we fix up branches.
1128 __ FinalizeCode();
1129
1130 // Adjust native pc offsets in stack maps.
1131 for (size_t i = 0, num = stack_map_stream_.GetNumberOfStackMaps(); i != num; ++i) {
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001132 uint32_t old_position =
1133 stack_map_stream_.GetStackMap(i).native_pc_code_offset.Uint32Value(kMips);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001134 uint32_t new_position = __ GetAdjustedPosition(old_position);
1135 DCHECK_GE(new_position, old_position);
1136 stack_map_stream_.SetStackMapNativePcOffset(i, new_position);
1137 }
1138
1139 // Adjust pc offsets for the disassembly information.
1140 if (disasm_info_ != nullptr) {
1141 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
1142 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
1143 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
1144 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
1145 it.second.start = __ GetAdjustedPosition(it.second.start);
1146 it.second.end = __ GetAdjustedPosition(it.second.end);
1147 }
1148 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
1149 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
1150 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
1151 }
1152 }
1153
1154 CodeGenerator::Finalize(allocator);
1155}
1156
1157MipsAssembler* ParallelMoveResolverMIPS::GetAssembler() const {
1158 return codegen_->GetAssembler();
1159}
1160
1161void ParallelMoveResolverMIPS::EmitMove(size_t index) {
1162 DCHECK_LT(index, moves_.size());
1163 MoveOperands* move = moves_[index];
1164 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), move->GetType());
1165}
1166
1167void ParallelMoveResolverMIPS::EmitSwap(size_t index) {
1168 DCHECK_LT(index, moves_.size());
1169 MoveOperands* move = moves_[index];
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001170 DataType::Type type = move->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001171 Location loc1 = move->GetDestination();
1172 Location loc2 = move->GetSource();
1173
1174 DCHECK(!loc1.IsConstant());
1175 DCHECK(!loc2.IsConstant());
1176
1177 if (loc1.Equals(loc2)) {
1178 return;
1179 }
1180
1181 if (loc1.IsRegister() && loc2.IsRegister()) {
1182 // Swap 2 GPRs.
1183 Register r1 = loc1.AsRegister<Register>();
1184 Register r2 = loc2.AsRegister<Register>();
1185 __ Move(TMP, r2);
1186 __ Move(r2, r1);
1187 __ Move(r1, TMP);
1188 } else if (loc1.IsFpuRegister() && loc2.IsFpuRegister()) {
1189 FRegister f1 = loc1.AsFpuRegister<FRegister>();
1190 FRegister f2 = loc2.AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001191 if (type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001192 __ MovS(FTMP, f2);
1193 __ MovS(f2, f1);
1194 __ MovS(f1, FTMP);
1195 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001196 DCHECK_EQ(type, DataType::Type::kFloat64);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001197 __ MovD(FTMP, f2);
1198 __ MovD(f2, f1);
1199 __ MovD(f1, FTMP);
1200 }
1201 } else if ((loc1.IsRegister() && loc2.IsFpuRegister()) ||
1202 (loc1.IsFpuRegister() && loc2.IsRegister())) {
1203 // Swap FPR and GPR.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001204 DCHECK_EQ(type, DataType::Type::kFloat32); // Can only swap a float.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001205 FRegister f1 = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
1206 : loc2.AsFpuRegister<FRegister>();
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +02001207 Register r2 = loc1.IsRegister() ? loc1.AsRegister<Register>() : loc2.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001208 __ Move(TMP, r2);
1209 __ Mfc1(r2, f1);
1210 __ Mtc1(TMP, f1);
1211 } else if (loc1.IsRegisterPair() && loc2.IsRegisterPair()) {
1212 // Swap 2 GPR register pairs.
1213 Register r1 = loc1.AsRegisterPairLow<Register>();
1214 Register r2 = loc2.AsRegisterPairLow<Register>();
1215 __ Move(TMP, r2);
1216 __ Move(r2, r1);
1217 __ Move(r1, TMP);
1218 r1 = loc1.AsRegisterPairHigh<Register>();
1219 r2 = loc2.AsRegisterPairHigh<Register>();
1220 __ Move(TMP, r2);
1221 __ Move(r2, r1);
1222 __ Move(r1, TMP);
1223 } else if ((loc1.IsRegisterPair() && loc2.IsFpuRegister()) ||
1224 (loc1.IsFpuRegister() && loc2.IsRegisterPair())) {
1225 // Swap FPR and GPR register pair.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001226 DCHECK_EQ(type, DataType::Type::kFloat64);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001227 FRegister f1 = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
1228 : loc2.AsFpuRegister<FRegister>();
1229 Register r2_l = loc1.IsRegisterPair() ? loc1.AsRegisterPairLow<Register>()
1230 : loc2.AsRegisterPairLow<Register>();
1231 Register r2_h = loc1.IsRegisterPair() ? loc1.AsRegisterPairHigh<Register>()
1232 : loc2.AsRegisterPairHigh<Register>();
1233 // Use 2 temporary registers because we can't first swap the low 32 bits of an FPR and
1234 // then swap the high 32 bits of the same FPR. mtc1 makes the high 32 bits of an FPR
1235 // unpredictable and the following mfch1 will fail.
1236 __ Mfc1(TMP, f1);
Alexey Frunzebb9863a2016-01-11 15:51:16 -08001237 __ MoveFromFpuHigh(AT, f1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001238 __ Mtc1(r2_l, f1);
Alexey Frunzebb9863a2016-01-11 15:51:16 -08001239 __ MoveToFpuHigh(r2_h, f1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001240 __ Move(r2_l, TMP);
1241 __ Move(r2_h, AT);
1242 } else if (loc1.IsStackSlot() && loc2.IsStackSlot()) {
1243 Exchange(loc1.GetStackIndex(), loc2.GetStackIndex(), /* double_slot */ false);
1244 } else if (loc1.IsDoubleStackSlot() && loc2.IsDoubleStackSlot()) {
1245 Exchange(loc1.GetStackIndex(), loc2.GetStackIndex(), /* double_slot */ true);
David Brazdilcc0f3112016-01-28 17:14:52 +00001246 } else if ((loc1.IsRegister() && loc2.IsStackSlot()) ||
1247 (loc1.IsStackSlot() && loc2.IsRegister())) {
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +02001248 Register reg = loc1.IsRegister() ? loc1.AsRegister<Register>() : loc2.AsRegister<Register>();
1249 intptr_t offset = loc1.IsStackSlot() ? loc1.GetStackIndex() : loc2.GetStackIndex();
David Brazdilcc0f3112016-01-28 17:14:52 +00001250 __ Move(TMP, reg);
1251 __ LoadFromOffset(kLoadWord, reg, SP, offset);
1252 __ StoreToOffset(kStoreWord, TMP, SP, offset);
1253 } else if ((loc1.IsRegisterPair() && loc2.IsDoubleStackSlot()) ||
1254 (loc1.IsDoubleStackSlot() && loc2.IsRegisterPair())) {
1255 Register reg_l = loc1.IsRegisterPair() ? loc1.AsRegisterPairLow<Register>()
1256 : loc2.AsRegisterPairLow<Register>();
1257 Register reg_h = loc1.IsRegisterPair() ? loc1.AsRegisterPairHigh<Register>()
1258 : loc2.AsRegisterPairHigh<Register>();
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +02001259 intptr_t offset_l = loc1.IsDoubleStackSlot() ? loc1.GetStackIndex() : loc2.GetStackIndex();
David Brazdilcc0f3112016-01-28 17:14:52 +00001260 intptr_t offset_h = loc1.IsDoubleStackSlot() ? loc1.GetHighStackIndex(kMipsWordSize)
1261 : loc2.GetHighStackIndex(kMipsWordSize);
1262 __ Move(TMP, reg_l);
David Brazdilcc0f3112016-01-28 17:14:52 +00001263 __ LoadFromOffset(kLoadWord, reg_l, SP, offset_l);
David Brazdilcc0f3112016-01-28 17:14:52 +00001264 __ StoreToOffset(kStoreWord, TMP, SP, offset_l);
David Brazdil04d3e872016-01-29 09:50:09 +00001265 __ Move(TMP, reg_h);
1266 __ LoadFromOffset(kLoadWord, reg_h, SP, offset_h);
1267 __ StoreToOffset(kStoreWord, TMP, SP, offset_h);
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +02001268 } else if (loc1.IsFpuRegister() || loc2.IsFpuRegister()) {
1269 FRegister reg = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
1270 : loc2.AsFpuRegister<FRegister>();
1271 intptr_t offset = loc1.IsFpuRegister() ? loc2.GetStackIndex() : loc1.GetStackIndex();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001272 if (type == DataType::Type::kFloat32) {
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +02001273 __ MovS(FTMP, reg);
1274 __ LoadSFromOffset(reg, SP, offset);
1275 __ StoreSToOffset(FTMP, SP, offset);
1276 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001277 DCHECK_EQ(type, DataType::Type::kFloat64);
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +02001278 __ MovD(FTMP, reg);
1279 __ LoadDFromOffset(reg, SP, offset);
1280 __ StoreDToOffset(FTMP, SP, offset);
1281 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001282 } else {
1283 LOG(FATAL) << "Swap between " << loc1 << " and " << loc2 << " is unsupported";
1284 }
1285}
1286
1287void ParallelMoveResolverMIPS::RestoreScratch(int reg) {
1288 __ Pop(static_cast<Register>(reg));
1289}
1290
1291void ParallelMoveResolverMIPS::SpillScratch(int reg) {
1292 __ Push(static_cast<Register>(reg));
1293}
1294
1295void ParallelMoveResolverMIPS::Exchange(int index1, int index2, bool double_slot) {
1296 // Allocate a scratch register other than TMP, if available.
1297 // Else, spill V0 (arbitrary choice) and use it as a scratch register (it will be
1298 // automatically unspilled when the scratch scope object is destroyed).
1299 ScratchRegisterScope ensure_scratch(this, TMP, V0, codegen_->GetNumberOfCoreRegisters());
1300 // If V0 spills onto the stack, SP-relative offsets need to be adjusted.
1301 int stack_offset = ensure_scratch.IsSpilled() ? kMipsWordSize : 0;
1302 for (int i = 0; i <= (double_slot ? 1 : 0); i++, stack_offset += kMipsWordSize) {
1303 __ LoadFromOffset(kLoadWord,
1304 Register(ensure_scratch.GetRegister()),
1305 SP,
1306 index1 + stack_offset);
1307 __ LoadFromOffset(kLoadWord,
1308 TMP,
1309 SP,
1310 index2 + stack_offset);
1311 __ StoreToOffset(kStoreWord,
1312 Register(ensure_scratch.GetRegister()),
1313 SP,
1314 index2 + stack_offset);
1315 __ StoreToOffset(kStoreWord, TMP, SP, index1 + stack_offset);
1316 }
1317}
1318
Alexey Frunze73296a72016-06-03 22:51:46 -07001319void CodeGeneratorMIPS::ComputeSpillMask() {
1320 core_spill_mask_ = allocated_registers_.GetCoreRegisters() & core_callee_save_mask_;
1321 fpu_spill_mask_ = allocated_registers_.GetFloatingPointRegisters() & fpu_callee_save_mask_;
1322 DCHECK_NE(core_spill_mask_, 0u) << "At least the return address register must be saved";
1323 // If there're FPU callee-saved registers and there's an odd number of GPR callee-saved
1324 // registers, include the ZERO register to force alignment of FPU callee-saved registers
1325 // within the stack frame.
1326 if ((fpu_spill_mask_ != 0) && (POPCOUNT(core_spill_mask_) % 2 != 0)) {
1327 core_spill_mask_ |= (1 << ZERO);
1328 }
Alexey Frunze58320ce2016-08-30 21:40:46 -07001329}
1330
1331bool CodeGeneratorMIPS::HasAllocatedCalleeSaveRegisters() const {
Alexey Frunze06a46c42016-07-19 15:00:40 -07001332 // If RA is clobbered by PC-relative operations on R2 and it's the only spilled register
Alexey Frunze58320ce2016-08-30 21:40:46 -07001333 // (this can happen in leaf methods), force CodeGenerator::InitializeCodeGeneration()
1334 // into the path that creates a stack frame so that RA can be explicitly saved and restored.
1335 // RA can't otherwise be saved/restored when it's the only spilled register.
Alexey Frunze58320ce2016-08-30 21:40:46 -07001336 return CodeGenerator::HasAllocatedCalleeSaveRegisters() || clobbered_ra_;
Alexey Frunze73296a72016-06-03 22:51:46 -07001337}
1338
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001339static dwarf::Reg DWARFReg(Register reg) {
1340 return dwarf::Reg::MipsCore(static_cast<int>(reg));
1341}
1342
1343// TODO: mapping of floating-point registers to DWARF.
1344
1345void CodeGeneratorMIPS::GenerateFrameEntry() {
1346 __ Bind(&frame_entry_label_);
1347
1348 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kMips) || !IsLeafMethod();
1349
1350 if (do_overflow_check) {
1351 __ LoadFromOffset(kLoadWord,
1352 ZERO,
1353 SP,
1354 -static_cast<int32_t>(GetStackOverflowReservedBytes(kMips)));
1355 RecordPcInfo(nullptr, 0);
1356 }
1357
1358 if (HasEmptyFrame()) {
Alexey Frunze58320ce2016-08-30 21:40:46 -07001359 CHECK_EQ(fpu_spill_mask_, 0u);
1360 CHECK_EQ(core_spill_mask_, 1u << RA);
1361 CHECK(!clobbered_ra_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001362 return;
1363 }
1364
1365 // Make sure the frame size isn't unreasonably large.
1366 if (GetFrameSize() > GetStackOverflowReservedBytes(kMips)) {
1367 LOG(FATAL) << "Stack frame larger than " << GetStackOverflowReservedBytes(kMips) << " bytes";
1368 }
1369
1370 // Spill callee-saved registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001371
Alexey Frunze73296a72016-06-03 22:51:46 -07001372 uint32_t ofs = GetFrameSize();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001373 __ IncreaseFrameSize(ofs);
1374
Alexey Frunze73296a72016-06-03 22:51:46 -07001375 for (uint32_t mask = core_spill_mask_; mask != 0; ) {
1376 Register reg = static_cast<Register>(MostSignificantBit(mask));
1377 mask ^= 1u << reg;
1378 ofs -= kMipsWordSize;
1379 // The ZERO register is only included for alignment.
1380 if (reg != ZERO) {
1381 __ StoreToOffset(kStoreWord, reg, SP, ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001382 __ cfi().RelOffset(DWARFReg(reg), ofs);
1383 }
1384 }
1385
Alexey Frunze73296a72016-06-03 22:51:46 -07001386 for (uint32_t mask = fpu_spill_mask_; mask != 0; ) {
1387 FRegister reg = static_cast<FRegister>(MostSignificantBit(mask));
1388 mask ^= 1u << reg;
1389 ofs -= kMipsDoublewordSize;
1390 __ StoreDToOffset(reg, SP, ofs);
1391 // TODO: __ cfi().RelOffset(DWARFReg(reg), ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001392 }
1393
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001394 // Save the current method if we need it. Note that we do not
1395 // do this in HCurrentMethod, as the instruction might have been removed
1396 // in the SSA graph.
1397 if (RequiresCurrentMethod()) {
1398 __ StoreToOffset(kStoreWord, kMethodRegisterArgument, SP, kCurrentMethodStackOffset);
1399 }
Goran Jakovljevicc6418422016-12-05 16:31:55 +01001400
1401 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1402 // Initialize should deoptimize flag to 0.
1403 __ StoreToOffset(kStoreWord, ZERO, SP, GetStackOffsetOfShouldDeoptimizeFlag());
1404 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001405}
1406
1407void CodeGeneratorMIPS::GenerateFrameExit() {
1408 __ cfi().RememberState();
1409
1410 if (!HasEmptyFrame()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001411 // Restore callee-saved registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001412
Alexey Frunze73296a72016-06-03 22:51:46 -07001413 // For better instruction scheduling restore RA before other registers.
1414 uint32_t ofs = GetFrameSize();
1415 for (uint32_t mask = core_spill_mask_; mask != 0; ) {
1416 Register reg = static_cast<Register>(MostSignificantBit(mask));
1417 mask ^= 1u << reg;
1418 ofs -= kMipsWordSize;
1419 // The ZERO register is only included for alignment.
1420 if (reg != ZERO) {
1421 __ LoadFromOffset(kLoadWord, reg, SP, ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001422 __ cfi().Restore(DWARFReg(reg));
1423 }
1424 }
1425
Alexey Frunze73296a72016-06-03 22:51:46 -07001426 for (uint32_t mask = fpu_spill_mask_; mask != 0; ) {
1427 FRegister reg = static_cast<FRegister>(MostSignificantBit(mask));
1428 mask ^= 1u << reg;
1429 ofs -= kMipsDoublewordSize;
1430 __ LoadDFromOffset(reg, SP, ofs);
1431 // TODO: __ cfi().Restore(DWARFReg(reg));
1432 }
1433
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001434 size_t frame_size = GetFrameSize();
1435 // Adjust the stack pointer in the delay slot if doing so doesn't break CFI.
1436 bool exchange = IsInt<16>(static_cast<int32_t>(frame_size));
1437 bool reordering = __ SetReorder(false);
1438 if (exchange) {
1439 __ Jr(RA);
1440 __ DecreaseFrameSize(frame_size); // Single instruction in delay slot.
1441 } else {
1442 __ DecreaseFrameSize(frame_size);
1443 __ Jr(RA);
1444 __ Nop(); // In delay slot.
1445 }
1446 __ SetReorder(reordering);
1447 } else {
1448 __ Jr(RA);
1449 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001450 }
1451
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001452 __ cfi().RestoreState();
1453 __ cfi().DefCFAOffset(GetFrameSize());
1454}
1455
1456void CodeGeneratorMIPS::Bind(HBasicBlock* block) {
1457 __ Bind(GetLabelOf(block));
1458}
1459
Lena Djokicca8c2952017-05-29 11:31:46 +02001460VectorRegister VectorRegisterFrom(Location location) {
1461 DCHECK(location.IsFpuRegister());
1462 return static_cast<VectorRegister>(location.AsFpuRegister<FRegister>());
1463}
1464
Lena Djokic8098da92017-06-28 12:07:50 +02001465void CodeGeneratorMIPS::MoveLocation(Location destination,
1466 Location source,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001467 DataType::Type dst_type) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001468 if (source.Equals(destination)) {
1469 return;
1470 }
1471
Lena Djokic8098da92017-06-28 12:07:50 +02001472 if (source.IsConstant()) {
1473 MoveConstant(destination, source.GetConstant());
1474 } else {
1475 if (destination.IsRegister()) {
1476 if (source.IsRegister()) {
1477 __ Move(destination.AsRegister<Register>(), source.AsRegister<Register>());
1478 } else if (source.IsFpuRegister()) {
1479 __ Mfc1(destination.AsRegister<Register>(), source.AsFpuRegister<FRegister>());
1480 } else {
1481 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001482 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(), SP, source.GetStackIndex());
Lena Djokic8098da92017-06-28 12:07:50 +02001483 }
1484 } else if (destination.IsRegisterPair()) {
1485 if (source.IsRegisterPair()) {
1486 __ Move(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
1487 __ Move(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
1488 } else if (source.IsFpuRegister()) {
1489 Register dst_high = destination.AsRegisterPairHigh<Register>();
1490 Register dst_low = destination.AsRegisterPairLow<Register>();
1491 FRegister src = source.AsFpuRegister<FRegister>();
1492 __ Mfc1(dst_low, src);
1493 __ MoveFromFpuHigh(dst_high, src);
1494 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001495 DCHECK(source.IsDoubleStackSlot())
1496 << "Cannot move from " << source << " to " << destination;
Lena Djokic8098da92017-06-28 12:07:50 +02001497 int32_t off = source.GetStackIndex();
1498 Register r = destination.AsRegisterPairLow<Register>();
1499 __ LoadFromOffset(kLoadDoubleword, r, SP, off);
1500 }
1501 } else if (destination.IsFpuRegister()) {
1502 if (source.IsRegister()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001503 DCHECK(!DataType::Is64BitType(dst_type));
Lena Djokic8098da92017-06-28 12:07:50 +02001504 __ Mtc1(source.AsRegister<Register>(), destination.AsFpuRegister<FRegister>());
1505 } else if (source.IsRegisterPair()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001506 DCHECK(DataType::Is64BitType(dst_type));
Lena Djokic8098da92017-06-28 12:07:50 +02001507 FRegister dst = destination.AsFpuRegister<FRegister>();
1508 Register src_high = source.AsRegisterPairHigh<Register>();
1509 Register src_low = source.AsRegisterPairLow<Register>();
1510 __ Mtc1(src_low, dst);
1511 __ MoveToFpuHigh(src_high, dst);
1512 } else if (source.IsFpuRegister()) {
Lena Djokicca8c2952017-05-29 11:31:46 +02001513 if (GetGraph()->HasSIMD()) {
1514 __ MoveV(VectorRegisterFrom(destination),
1515 VectorRegisterFrom(source));
Lena Djokic8098da92017-06-28 12:07:50 +02001516 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001517 if (DataType::Is64BitType(dst_type)) {
Lena Djokicca8c2952017-05-29 11:31:46 +02001518 __ MovD(destination.AsFpuRegister<FRegister>(), source.AsFpuRegister<FRegister>());
1519 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001520 DCHECK_EQ(dst_type, DataType::Type::kFloat32);
Lena Djokicca8c2952017-05-29 11:31:46 +02001521 __ MovS(destination.AsFpuRegister<FRegister>(), source.AsFpuRegister<FRegister>());
1522 }
Lena Djokic8098da92017-06-28 12:07:50 +02001523 }
Lena Djokicca8c2952017-05-29 11:31:46 +02001524 } else if (source.IsSIMDStackSlot()) {
1525 __ LoadQFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
Lena Djokic8098da92017-06-28 12:07:50 +02001526 } else if (source.IsDoubleStackSlot()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001527 DCHECK(DataType::Is64BitType(dst_type));
Lena Djokic8098da92017-06-28 12:07:50 +02001528 __ LoadDFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
1529 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001530 DCHECK(!DataType::Is64BitType(dst_type));
Lena Djokic8098da92017-06-28 12:07:50 +02001531 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
1532 __ LoadSFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
1533 }
Lena Djokicca8c2952017-05-29 11:31:46 +02001534 } else if (destination.IsSIMDStackSlot()) {
1535 if (source.IsFpuRegister()) {
1536 __ StoreQToOffset(source.AsFpuRegister<FRegister>(), SP, destination.GetStackIndex());
1537 } else {
1538 DCHECK(source.IsSIMDStackSlot());
1539 __ LoadQFromOffset(FTMP, SP, source.GetStackIndex());
1540 __ StoreQToOffset(FTMP, SP, destination.GetStackIndex());
1541 }
Lena Djokic8098da92017-06-28 12:07:50 +02001542 } else if (destination.IsDoubleStackSlot()) {
1543 int32_t dst_offset = destination.GetStackIndex();
1544 if (source.IsRegisterPair()) {
1545 __ StoreToOffset(kStoreDoubleword, source.AsRegisterPairLow<Register>(), SP, dst_offset);
1546 } else if (source.IsFpuRegister()) {
1547 __ StoreDToOffset(source.AsFpuRegister<FRegister>(), SP, dst_offset);
1548 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001549 DCHECK(source.IsDoubleStackSlot())
1550 << "Cannot move from " << source << " to " << destination;
Lena Djokic8098da92017-06-28 12:07:50 +02001551 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
1552 __ StoreToOffset(kStoreWord, TMP, SP, dst_offset);
1553 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex() + 4);
1554 __ StoreToOffset(kStoreWord, TMP, SP, dst_offset + 4);
1555 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001556 } else {
Lena Djokic8098da92017-06-28 12:07:50 +02001557 DCHECK(destination.IsStackSlot()) << destination;
1558 int32_t dst_offset = destination.GetStackIndex();
1559 if (source.IsRegister()) {
1560 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(), SP, dst_offset);
1561 } else if (source.IsFpuRegister()) {
1562 __ StoreSToOffset(source.AsFpuRegister<FRegister>(), SP, dst_offset);
1563 } else {
1564 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
1565 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
1566 __ StoreToOffset(kStoreWord, TMP, SP, dst_offset);
1567 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001568 }
1569 }
1570}
1571
1572void CodeGeneratorMIPS::MoveConstant(Location destination, HConstant* c) {
1573 if (c->IsIntConstant() || c->IsNullConstant()) {
1574 // Move 32 bit constant.
1575 int32_t value = GetInt32ValueOf(c);
1576 if (destination.IsRegister()) {
1577 Register dst = destination.AsRegister<Register>();
1578 __ LoadConst32(dst, value);
1579 } else {
1580 DCHECK(destination.IsStackSlot())
1581 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -07001582 __ StoreConstToOffset(kStoreWord, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001583 }
1584 } else if (c->IsLongConstant()) {
1585 // Move 64 bit constant.
1586 int64_t value = GetInt64ValueOf(c);
1587 if (destination.IsRegisterPair()) {
1588 Register r_h = destination.AsRegisterPairHigh<Register>();
1589 Register r_l = destination.AsRegisterPairLow<Register>();
1590 __ LoadConst64(r_h, r_l, value);
1591 } else {
1592 DCHECK(destination.IsDoubleStackSlot())
1593 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -07001594 __ StoreConstToOffset(kStoreDoubleword, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001595 }
1596 } else if (c->IsFloatConstant()) {
1597 // Move 32 bit float constant.
1598 int32_t value = GetInt32ValueOf(c);
1599 if (destination.IsFpuRegister()) {
1600 __ LoadSConst32(destination.AsFpuRegister<FRegister>(), value, TMP);
1601 } else {
1602 DCHECK(destination.IsStackSlot())
1603 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -07001604 __ StoreConstToOffset(kStoreWord, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001605 }
1606 } else {
1607 // Move 64 bit double constant.
1608 DCHECK(c->IsDoubleConstant()) << c->DebugName();
1609 int64_t value = GetInt64ValueOf(c);
1610 if (destination.IsFpuRegister()) {
1611 FRegister fd = destination.AsFpuRegister<FRegister>();
1612 __ LoadDConst64(fd, value, TMP);
1613 } else {
1614 DCHECK(destination.IsDoubleStackSlot())
1615 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -07001616 __ StoreConstToOffset(kStoreDoubleword, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001617 }
1618 }
1619}
1620
1621void CodeGeneratorMIPS::MoveConstant(Location destination, int32_t value) {
1622 DCHECK(destination.IsRegister());
1623 Register dst = destination.AsRegister<Register>();
1624 __ LoadConst32(dst, value);
1625}
1626
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001627void CodeGeneratorMIPS::AddLocationAsTemp(Location location, LocationSummary* locations) {
1628 if (location.IsRegister()) {
1629 locations->AddTemp(location);
Alexey Frunzec9e94f32015-10-26 16:11:39 -07001630 } else if (location.IsRegisterPair()) {
1631 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1632 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001633 } else {
1634 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1635 }
1636}
1637
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001638template <linker::LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
Vladimir Markoaad75c62016-10-03 08:46:48 +00001639inline void CodeGeneratorMIPS::EmitPcRelativeLinkerPatches(
1640 const ArenaDeque<PcRelativePatchInfo>& infos,
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001641 ArenaVector<linker::LinkerPatch>* linker_patches) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00001642 for (const PcRelativePatchInfo& info : infos) {
1643 const DexFile& dex_file = info.target_dex_file;
1644 size_t offset_or_index = info.offset_or_index;
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001645 DCHECK(info.label.IsBound());
1646 uint32_t literal_offset = __ GetLabelLocation(&info.label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001647 // On R2 we use HMipsComputeBaseMethodAddress and patch relative to
1648 // the assembler's base label used for PC-relative addressing.
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001649 const PcRelativePatchInfo& info_high = info.patch_info_high ? *info.patch_info_high : info;
1650 uint32_t pc_rel_offset = info_high.pc_rel_label.IsBound()
1651 ? __ GetLabelLocation(&info_high.pc_rel_label)
Vladimir Markoaad75c62016-10-03 08:46:48 +00001652 : __ GetPcRelBaseLabelLocation();
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001653 linker_patches->push_back(Factory(literal_offset, &dex_file, pc_rel_offset, offset_or_index));
Vladimir Markoaad75c62016-10-03 08:46:48 +00001654 }
1655}
1656
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001657void CodeGeneratorMIPS::EmitLinkerPatches(ArenaVector<linker::LinkerPatch>* linker_patches) {
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001658 DCHECK(linker_patches->empty());
1659 size_t size =
Vladimir Marko65979462017-05-19 17:25:12 +01001660 pc_relative_method_patches_.size() +
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001661 method_bss_entry_patches_.size() +
Alexey Frunze06a46c42016-07-19 15:00:40 -07001662 pc_relative_type_patches_.size() +
Vladimir Marko65979462017-05-19 17:25:12 +01001663 type_bss_entry_patches_.size() +
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01001664 pc_relative_string_patches_.size() +
1665 string_bss_entry_patches_.size();
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001666 linker_patches->reserve(size);
Vladimir Marko65979462017-05-19 17:25:12 +01001667 if (GetCompilerOptions().IsBootImage()) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001668 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeMethodPatch>(
1669 pc_relative_method_patches_, linker_patches);
1670 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeTypePatch>(
1671 pc_relative_type_patches_, linker_patches);
1672 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeStringPatch>(
1673 pc_relative_string_patches_, linker_patches);
Vladimir Marko65979462017-05-19 17:25:12 +01001674 } else {
1675 DCHECK(pc_relative_method_patches_.empty());
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001676 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeClassTablePatch>(
1677 pc_relative_type_patches_, linker_patches);
1678 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringInternTablePatch>(
1679 pc_relative_string_patches_, linker_patches);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001680 }
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001681 EmitPcRelativeLinkerPatches<linker::LinkerPatch::MethodBssEntryPatch>(
1682 method_bss_entry_patches_, linker_patches);
1683 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeBssEntryPatch>(
1684 type_bss_entry_patches_, linker_patches);
1685 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringBssEntryPatch>(
1686 string_bss_entry_patches_, linker_patches);
Vladimir Marko1998cd02017-01-13 13:02:58 +00001687 DCHECK_EQ(size, linker_patches->size());
Alexey Frunze06a46c42016-07-19 15:00:40 -07001688}
1689
Vladimir Marko65979462017-05-19 17:25:12 +01001690CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeMethodPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001691 MethodReference target_method,
1692 const PcRelativePatchInfo* info_high) {
Vladimir Marko65979462017-05-19 17:25:12 +01001693 return NewPcRelativePatch(*target_method.dex_file,
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07001694 target_method.index,
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001695 info_high,
Vladimir Marko65979462017-05-19 17:25:12 +01001696 &pc_relative_method_patches_);
Alexey Frunze06a46c42016-07-19 15:00:40 -07001697}
1698
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001699CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewMethodBssEntryPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001700 MethodReference target_method,
1701 const PcRelativePatchInfo* info_high) {
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001702 return NewPcRelativePatch(*target_method.dex_file,
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07001703 target_method.index,
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001704 info_high,
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001705 &method_bss_entry_patches_);
1706}
1707
Alexey Frunze06a46c42016-07-19 15:00:40 -07001708CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeTypePatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001709 const DexFile& dex_file,
1710 dex::TypeIndex type_index,
1711 const PcRelativePatchInfo* info_high) {
1712 return NewPcRelativePatch(dex_file, type_index.index_, info_high, &pc_relative_type_patches_);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001713}
1714
Vladimir Marko1998cd02017-01-13 13:02:58 +00001715CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewTypeBssEntryPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001716 const DexFile& dex_file,
1717 dex::TypeIndex type_index,
1718 const PcRelativePatchInfo* info_high) {
1719 return NewPcRelativePatch(dex_file, type_index.index_, info_high, &type_bss_entry_patches_);
Vladimir Marko1998cd02017-01-13 13:02:58 +00001720}
1721
Vladimir Marko65979462017-05-19 17:25:12 +01001722CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeStringPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001723 const DexFile& dex_file,
1724 dex::StringIndex string_index,
1725 const PcRelativePatchInfo* info_high) {
1726 return NewPcRelativePatch(dex_file, string_index.index_, info_high, &pc_relative_string_patches_);
Vladimir Marko65979462017-05-19 17:25:12 +01001727}
1728
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01001729CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewStringBssEntryPatch(
1730 const DexFile& dex_file,
1731 dex::StringIndex string_index,
1732 const PcRelativePatchInfo* info_high) {
1733 return NewPcRelativePatch(dex_file, string_index.index_, info_high, &string_bss_entry_patches_);
1734}
1735
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001736CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativePatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001737 const DexFile& dex_file,
1738 uint32_t offset_or_index,
1739 const PcRelativePatchInfo* info_high,
1740 ArenaDeque<PcRelativePatchInfo>* patches) {
1741 patches->emplace_back(dex_file, offset_or_index, info_high);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001742 return &patches->back();
1743}
1744
Alexey Frunze06a46c42016-07-19 15:00:40 -07001745Literal* CodeGeneratorMIPS::DeduplicateUint32Literal(uint32_t value, Uint32ToLiteralMap* map) {
1746 return map->GetOrCreate(
1747 value,
1748 [this, value]() { return __ NewLiteral<uint32_t>(value); });
1749}
1750
Alexey Frunze06a46c42016-07-19 15:00:40 -07001751Literal* CodeGeneratorMIPS::DeduplicateBootImageAddressLiteral(uint32_t address) {
Richard Uhlerc52f3032017-03-02 13:45:45 +00001752 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), &uint32_literals_);
Alexey Frunze06a46c42016-07-19 15:00:40 -07001753}
1754
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001755void CodeGeneratorMIPS::EmitPcRelativeAddressPlaceholderHigh(PcRelativePatchInfo* info_high,
Alexey Frunze6b892cd2017-01-03 17:11:38 -08001756 Register out,
Alexey Frunzea663d9d2017-07-31 18:43:18 -07001757 Register base) {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001758 DCHECK(!info_high->patch_info_high);
Alexey Frunze6079dca2017-05-28 19:10:28 -07001759 DCHECK_NE(out, base);
Alexey Frunzea663d9d2017-07-31 18:43:18 -07001760 bool reordering = __ SetReorder(false);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001761 if (GetInstructionSetFeatures().IsR6()) {
1762 DCHECK_EQ(base, ZERO);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001763 __ Bind(&info_high->label);
1764 __ Bind(&info_high->pc_rel_label);
Alexey Frunze6b892cd2017-01-03 17:11:38 -08001765 // Add the high half of a 32-bit offset to PC.
Vladimir Markoaad75c62016-10-03 08:46:48 +00001766 __ Auipc(out, /* placeholder */ 0x1234);
Alexey Frunzea663d9d2017-07-31 18:43:18 -07001767 __ SetReorder(reordering);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001768 } else {
1769 // If base is ZERO, emit NAL to obtain the actual base.
1770 if (base == ZERO) {
1771 // Generate a dummy PC-relative call to obtain PC.
1772 __ Nal();
1773 }
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001774 __ Bind(&info_high->label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001775 __ Lui(out, /* placeholder */ 0x1234);
1776 // If we emitted the NAL, bind the pc_rel_label, otherwise base is a register holding
1777 // the HMipsComputeBaseMethodAddress which has its own label stored in MipsAssembler.
1778 if (base == ZERO) {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001779 __ Bind(&info_high->pc_rel_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001780 }
Alexey Frunzea663d9d2017-07-31 18:43:18 -07001781 __ SetReorder(reordering);
Alexey Frunze6b892cd2017-01-03 17:11:38 -08001782 // Add the high half of a 32-bit offset to PC.
Vladimir Markoaad75c62016-10-03 08:46:48 +00001783 __ Addu(out, out, (base == ZERO) ? RA : base);
1784 }
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001785 // A following instruction will add the sign-extended low half of the 32-bit
Alexey Frunze6b892cd2017-01-03 17:11:38 -08001786 // offset to `out` (e.g. lw, jialc, addiu).
Vladimir Markoaad75c62016-10-03 08:46:48 +00001787}
1788
Alexey Frunze627c1a02017-01-30 19:28:14 -08001789CodeGeneratorMIPS::JitPatchInfo* CodeGeneratorMIPS::NewJitRootStringPatch(
1790 const DexFile& dex_file,
1791 dex::StringIndex dex_index,
1792 Handle<mirror::String> handle) {
1793 jit_string_roots_.Overwrite(StringReference(&dex_file, dex_index),
1794 reinterpret_cast64<uint64_t>(handle.GetReference()));
1795 jit_string_patches_.emplace_back(dex_file, dex_index.index_);
1796 return &jit_string_patches_.back();
1797}
1798
1799CodeGeneratorMIPS::JitPatchInfo* CodeGeneratorMIPS::NewJitRootClassPatch(
1800 const DexFile& dex_file,
1801 dex::TypeIndex dex_index,
1802 Handle<mirror::Class> handle) {
1803 jit_class_roots_.Overwrite(TypeReference(&dex_file, dex_index),
1804 reinterpret_cast64<uint64_t>(handle.GetReference()));
1805 jit_class_patches_.emplace_back(dex_file, dex_index.index_);
1806 return &jit_class_patches_.back();
1807}
1808
1809void CodeGeneratorMIPS::PatchJitRootUse(uint8_t* code,
1810 const uint8_t* roots_data,
1811 const CodeGeneratorMIPS::JitPatchInfo& info,
1812 uint64_t index_in_table) const {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001813 uint32_t high_literal_offset = GetAssembler().GetLabelLocation(&info.high_label);
1814 uint32_t low_literal_offset = GetAssembler().GetLabelLocation(&info.low_label);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001815 uintptr_t address =
1816 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
1817 uint32_t addr32 = dchecked_integral_cast<uint32_t>(address);
1818 // lui reg, addr32_high
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001819 DCHECK_EQ(code[high_literal_offset + 0], 0x34);
1820 DCHECK_EQ(code[high_literal_offset + 1], 0x12);
1821 DCHECK_EQ((code[high_literal_offset + 2] & 0xE0), 0x00);
1822 DCHECK_EQ(code[high_literal_offset + 3], 0x3C);
Alexey Frunzec61c0762017-04-10 13:54:23 -07001823 // instr reg, reg, addr32_low
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001824 DCHECK_EQ(code[low_literal_offset + 0], 0x78);
1825 DCHECK_EQ(code[low_literal_offset + 1], 0x56);
Alexey Frunzec61c0762017-04-10 13:54:23 -07001826 addr32 += (addr32 & 0x8000) << 1; // Account for sign extension in "instr reg, reg, addr32_low".
Alexey Frunze627c1a02017-01-30 19:28:14 -08001827 // lui reg, addr32_high
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001828 code[high_literal_offset + 0] = static_cast<uint8_t>(addr32 >> 16);
1829 code[high_literal_offset + 1] = static_cast<uint8_t>(addr32 >> 24);
Alexey Frunzec61c0762017-04-10 13:54:23 -07001830 // instr reg, reg, addr32_low
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001831 code[low_literal_offset + 0] = static_cast<uint8_t>(addr32 >> 0);
1832 code[low_literal_offset + 1] = static_cast<uint8_t>(addr32 >> 8);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001833}
1834
1835void CodeGeneratorMIPS::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
1836 for (const JitPatchInfo& info : jit_string_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001837 const auto it = jit_string_roots_.find(StringReference(&info.target_dex_file,
1838 dex::StringIndex(info.index)));
Alexey Frunze627c1a02017-01-30 19:28:14 -08001839 DCHECK(it != jit_string_roots_.end());
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001840 uint64_t index_in_table = it->second;
1841 PatchJitRootUse(code, roots_data, info, index_in_table);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001842 }
1843 for (const JitPatchInfo& info : jit_class_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001844 const auto it = jit_class_roots_.find(TypeReference(&info.target_dex_file,
1845 dex::TypeIndex(info.index)));
Alexey Frunze627c1a02017-01-30 19:28:14 -08001846 DCHECK(it != jit_class_roots_.end());
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001847 uint64_t index_in_table = it->second;
1848 PatchJitRootUse(code, roots_data, info, index_in_table);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001849 }
1850}
1851
Goran Jakovljevice114da22016-12-26 14:21:43 +01001852void CodeGeneratorMIPS::MarkGCCard(Register object,
1853 Register value,
1854 bool value_can_be_null) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001855 MipsLabel done;
1856 Register card = AT;
1857 Register temp = TMP;
Goran Jakovljevice114da22016-12-26 14:21:43 +01001858 if (value_can_be_null) {
1859 __ Beqz(value, &done);
1860 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001861 __ LoadFromOffset(kLoadWord,
1862 card,
1863 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001864 Thread::CardTableOffset<kMipsPointerSize>().Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001865 __ Srl(temp, object, gc::accounting::CardTable::kCardShift);
1866 __ Addu(temp, card, temp);
1867 __ Sb(card, temp, 0);
Goran Jakovljevice114da22016-12-26 14:21:43 +01001868 if (value_can_be_null) {
1869 __ Bind(&done);
1870 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001871}
1872
David Brazdil58282f42016-01-14 12:45:10 +00001873void CodeGeneratorMIPS::SetupBlockedRegisters() const {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001874 // ZERO, K0, K1, GP, SP, RA are always reserved and can't be allocated.
1875 blocked_core_registers_[ZERO] = true;
1876 blocked_core_registers_[K0] = true;
1877 blocked_core_registers_[K1] = true;
1878 blocked_core_registers_[GP] = true;
1879 blocked_core_registers_[SP] = true;
1880 blocked_core_registers_[RA] = true;
1881
1882 // AT and TMP(T8) are used as temporary/scratch registers
1883 // (similar to how AT is used by MIPS assemblers).
1884 blocked_core_registers_[AT] = true;
1885 blocked_core_registers_[TMP] = true;
1886 blocked_fpu_registers_[FTMP] = true;
1887
1888 // Reserve suspend and thread registers.
1889 blocked_core_registers_[S0] = true;
1890 blocked_core_registers_[TR] = true;
1891
1892 // Reserve T9 for function calls
1893 blocked_core_registers_[T9] = true;
1894
1895 // Reserve odd-numbered FPU registers.
1896 for (size_t i = 1; i < kNumberOfFRegisters; i += 2) {
1897 blocked_fpu_registers_[i] = true;
1898 }
1899
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02001900 if (GetGraph()->IsDebuggable()) {
1901 // Stubs do not save callee-save floating point registers. If the graph
1902 // is debuggable, we need to deal with these registers differently. For
1903 // now, just block them.
1904 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1905 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
1906 }
1907 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001908}
1909
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001910size_t CodeGeneratorMIPS::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1911 __ StoreToOffset(kStoreWord, Register(reg_id), SP, stack_index);
1912 return kMipsWordSize;
1913}
1914
1915size_t CodeGeneratorMIPS::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1916 __ LoadFromOffset(kLoadWord, Register(reg_id), SP, stack_index);
1917 return kMipsWordSize;
1918}
1919
1920size_t CodeGeneratorMIPS::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Lena Djokicca8c2952017-05-29 11:31:46 +02001921 if (GetGraph()->HasSIMD()) {
1922 __ StoreQToOffset(FRegister(reg_id), SP, stack_index);
1923 } else {
1924 __ StoreDToOffset(FRegister(reg_id), SP, stack_index);
1925 }
1926 return GetFloatingPointSpillSlotSize();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001927}
1928
1929size_t CodeGeneratorMIPS::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Lena Djokicca8c2952017-05-29 11:31:46 +02001930 if (GetGraph()->HasSIMD()) {
1931 __ LoadQFromOffset(FRegister(reg_id), SP, stack_index);
1932 } else {
1933 __ LoadDFromOffset(FRegister(reg_id), SP, stack_index);
1934 }
1935 return GetFloatingPointSpillSlotSize();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001936}
1937
1938void CodeGeneratorMIPS::DumpCoreRegister(std::ostream& stream, int reg) const {
Vladimir Marko623a7a22016-02-02 18:14:52 +00001939 stream << Register(reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001940}
1941
1942void CodeGeneratorMIPS::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
Vladimir Marko623a7a22016-02-02 18:14:52 +00001943 stream << FRegister(reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001944}
1945
Serban Constantinescufca16662016-07-14 09:21:59 +01001946constexpr size_t kMipsDirectEntrypointRuntimeOffset = 16;
1947
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001948void CodeGeneratorMIPS::InvokeRuntime(QuickEntrypointEnum entrypoint,
1949 HInstruction* instruction,
1950 uint32_t dex_pc,
1951 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001952 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Alexey Frunze15958152017-02-09 19:08:30 -08001953 GenerateInvokeRuntime(GetThreadOffset<kMipsPointerSize>(entrypoint).Int32Value(),
1954 IsDirectEntrypoint(entrypoint));
1955 if (EntrypointRequiresStackMap(entrypoint)) {
1956 RecordPcInfo(instruction, dex_pc, slow_path);
1957 }
1958}
1959
1960void CodeGeneratorMIPS::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1961 HInstruction* instruction,
1962 SlowPathCode* slow_path,
1963 bool direct) {
1964 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
1965 GenerateInvokeRuntime(entry_point_offset, direct);
1966}
1967
1968void CodeGeneratorMIPS::GenerateInvokeRuntime(int32_t entry_point_offset, bool direct) {
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001969 bool reordering = __ SetReorder(false);
Alexey Frunze15958152017-02-09 19:08:30 -08001970 __ LoadFromOffset(kLoadWord, T9, TR, entry_point_offset);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001971 __ Jalr(T9);
Alexey Frunze15958152017-02-09 19:08:30 -08001972 if (direct) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001973 // Reserve argument space on stack (for $a0-$a3) for
1974 // entrypoints that directly reference native implementations.
1975 // Called function may use this space to store $a0-$a3 regs.
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001976 __ IncreaseFrameSize(kMipsDirectEntrypointRuntimeOffset); // Single instruction in delay slot.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001977 __ DecreaseFrameSize(kMipsDirectEntrypointRuntimeOffset);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001978 } else {
1979 __ Nop(); // In delay slot.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001980 }
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001981 __ SetReorder(reordering);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001982}
1983
1984void InstructionCodeGeneratorMIPS::GenerateClassInitializationCheck(SlowPathCodeMIPS* slow_path,
1985 Register class_reg) {
1986 __ LoadFromOffset(kLoadWord, TMP, class_reg, mirror::Class::StatusOffset().Int32Value());
1987 __ LoadConst32(AT, mirror::Class::kStatusInitialized);
1988 __ Blt(TMP, AT, slow_path->GetEntryLabel());
1989 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
1990 __ Sync(0);
1991 __ Bind(slow_path->GetExitLabel());
1992}
1993
1994void InstructionCodeGeneratorMIPS::GenerateMemoryBarrier(MemBarrierKind kind ATTRIBUTE_UNUSED) {
1995 __ Sync(0); // Only stype 0 is supported.
1996}
1997
1998void InstructionCodeGeneratorMIPS::GenerateSuspendCheck(HSuspendCheck* instruction,
1999 HBasicBlock* successor) {
2000 SuspendCheckSlowPathMIPS* slow_path =
2001 new (GetGraph()->GetArena()) SuspendCheckSlowPathMIPS(instruction, successor);
2002 codegen_->AddSlowPath(slow_path);
2003
2004 __ LoadFromOffset(kLoadUnsignedHalfword,
2005 TMP,
2006 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07002007 Thread::ThreadFlagsOffset<kMipsPointerSize>().Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002008 if (successor == nullptr) {
2009 __ Bnez(TMP, slow_path->GetEntryLabel());
2010 __ Bind(slow_path->GetReturnLabel());
2011 } else {
2012 __ Beqz(TMP, codegen_->GetLabelOf(successor));
2013 __ B(slow_path->GetEntryLabel());
2014 // slow_path will return to GetLabelOf(successor).
2015 }
2016}
2017
2018InstructionCodeGeneratorMIPS::InstructionCodeGeneratorMIPS(HGraph* graph,
2019 CodeGeneratorMIPS* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08002020 : InstructionCodeGenerator(graph, codegen),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002021 assembler_(codegen->GetAssembler()),
2022 codegen_(codegen) {}
2023
2024void LocationsBuilderMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
2025 DCHECK_EQ(instruction->InputCount(), 2U);
2026 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002027 DataType::Type type = instruction->GetResultType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002028 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002029 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002030 locations->SetInAt(0, Location::RequiresRegister());
2031 HInstruction* right = instruction->InputAt(1);
2032 bool can_use_imm = false;
2033 if (right->IsConstant()) {
2034 int32_t imm = CodeGenerator::GetInt32ValueOf(right->AsConstant());
2035 if (instruction->IsAnd() || instruction->IsOr() || instruction->IsXor()) {
2036 can_use_imm = IsUint<16>(imm);
2037 } else if (instruction->IsAdd()) {
2038 can_use_imm = IsInt<16>(imm);
2039 } else {
2040 DCHECK(instruction->IsSub());
2041 can_use_imm = IsInt<16>(-imm);
2042 }
2043 }
2044 if (can_use_imm)
2045 locations->SetInAt(1, Location::ConstantLocation(right->AsConstant()));
2046 else
2047 locations->SetInAt(1, Location::RequiresRegister());
2048 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2049 break;
2050 }
2051
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002052 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002053 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002054 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
2055 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002056 break;
2057 }
2058
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002059 case DataType::Type::kFloat32:
2060 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002061 DCHECK(instruction->IsAdd() || instruction->IsSub());
2062 locations->SetInAt(0, Location::RequiresFpuRegister());
2063 locations->SetInAt(1, Location::RequiresFpuRegister());
2064 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2065 break;
2066
2067 default:
2068 LOG(FATAL) << "Unexpected " << instruction->DebugName() << " type " << type;
2069 }
2070}
2071
2072void InstructionCodeGeneratorMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002073 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002074 LocationSummary* locations = instruction->GetLocations();
2075
2076 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002077 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002078 Register dst = locations->Out().AsRegister<Register>();
2079 Register lhs = locations->InAt(0).AsRegister<Register>();
2080 Location rhs_location = locations->InAt(1);
2081
2082 Register rhs_reg = ZERO;
2083 int32_t rhs_imm = 0;
2084 bool use_imm = rhs_location.IsConstant();
2085 if (use_imm) {
2086 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2087 } else {
2088 rhs_reg = rhs_location.AsRegister<Register>();
2089 }
2090
2091 if (instruction->IsAnd()) {
2092 if (use_imm)
2093 __ Andi(dst, lhs, rhs_imm);
2094 else
2095 __ And(dst, lhs, rhs_reg);
2096 } else if (instruction->IsOr()) {
2097 if (use_imm)
2098 __ Ori(dst, lhs, rhs_imm);
2099 else
2100 __ Or(dst, lhs, rhs_reg);
2101 } else if (instruction->IsXor()) {
2102 if (use_imm)
2103 __ Xori(dst, lhs, rhs_imm);
2104 else
2105 __ Xor(dst, lhs, rhs_reg);
2106 } else if (instruction->IsAdd()) {
2107 if (use_imm)
2108 __ Addiu(dst, lhs, rhs_imm);
2109 else
2110 __ Addu(dst, lhs, rhs_reg);
2111 } else {
2112 DCHECK(instruction->IsSub());
2113 if (use_imm)
2114 __ Addiu(dst, lhs, -rhs_imm);
2115 else
2116 __ Subu(dst, lhs, rhs_reg);
2117 }
2118 break;
2119 }
2120
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002121 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002122 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
2123 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
2124 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
2125 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002126 Location rhs_location = locations->InAt(1);
2127 bool use_imm = rhs_location.IsConstant();
2128 if (!use_imm) {
2129 Register rhs_high = rhs_location.AsRegisterPairHigh<Register>();
2130 Register rhs_low = rhs_location.AsRegisterPairLow<Register>();
2131 if (instruction->IsAnd()) {
2132 __ And(dst_low, lhs_low, rhs_low);
2133 __ And(dst_high, lhs_high, rhs_high);
2134 } else if (instruction->IsOr()) {
2135 __ Or(dst_low, lhs_low, rhs_low);
2136 __ Or(dst_high, lhs_high, rhs_high);
2137 } else if (instruction->IsXor()) {
2138 __ Xor(dst_low, lhs_low, rhs_low);
2139 __ Xor(dst_high, lhs_high, rhs_high);
2140 } else if (instruction->IsAdd()) {
2141 if (lhs_low == rhs_low) {
2142 // Special case for lhs = rhs and the sum potentially overwriting both lhs and rhs.
2143 __ Slt(TMP, lhs_low, ZERO);
2144 __ Addu(dst_low, lhs_low, rhs_low);
2145 } else {
2146 __ Addu(dst_low, lhs_low, rhs_low);
2147 // If the sum overwrites rhs, lhs remains unchanged, otherwise rhs remains unchanged.
2148 __ Sltu(TMP, dst_low, (dst_low == rhs_low) ? lhs_low : rhs_low);
2149 }
2150 __ Addu(dst_high, lhs_high, rhs_high);
2151 __ Addu(dst_high, dst_high, TMP);
2152 } else {
2153 DCHECK(instruction->IsSub());
2154 __ Sltu(TMP, lhs_low, rhs_low);
2155 __ Subu(dst_low, lhs_low, rhs_low);
2156 __ Subu(dst_high, lhs_high, rhs_high);
2157 __ Subu(dst_high, dst_high, TMP);
2158 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002159 } else {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002160 int64_t value = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()->AsConstant());
2161 if (instruction->IsOr()) {
2162 uint32_t low = Low32Bits(value);
2163 uint32_t high = High32Bits(value);
2164 if (IsUint<16>(low)) {
2165 if (dst_low != lhs_low || low != 0) {
2166 __ Ori(dst_low, lhs_low, low);
2167 }
2168 } else {
2169 __ LoadConst32(TMP, low);
2170 __ Or(dst_low, lhs_low, TMP);
2171 }
2172 if (IsUint<16>(high)) {
2173 if (dst_high != lhs_high || high != 0) {
2174 __ Ori(dst_high, lhs_high, high);
2175 }
2176 } else {
2177 if (high != low) {
2178 __ LoadConst32(TMP, high);
2179 }
2180 __ Or(dst_high, lhs_high, TMP);
2181 }
2182 } else if (instruction->IsXor()) {
2183 uint32_t low = Low32Bits(value);
2184 uint32_t high = High32Bits(value);
2185 if (IsUint<16>(low)) {
2186 if (dst_low != lhs_low || low != 0) {
2187 __ Xori(dst_low, lhs_low, low);
2188 }
2189 } else {
2190 __ LoadConst32(TMP, low);
2191 __ Xor(dst_low, lhs_low, TMP);
2192 }
2193 if (IsUint<16>(high)) {
2194 if (dst_high != lhs_high || high != 0) {
2195 __ Xori(dst_high, lhs_high, high);
2196 }
2197 } else {
2198 if (high != low) {
2199 __ LoadConst32(TMP, high);
2200 }
2201 __ Xor(dst_high, lhs_high, TMP);
2202 }
2203 } else if (instruction->IsAnd()) {
2204 uint32_t low = Low32Bits(value);
2205 uint32_t high = High32Bits(value);
2206 if (IsUint<16>(low)) {
2207 __ Andi(dst_low, lhs_low, low);
2208 } else if (low != 0xFFFFFFFF) {
2209 __ LoadConst32(TMP, low);
2210 __ And(dst_low, lhs_low, TMP);
2211 } else if (dst_low != lhs_low) {
2212 __ Move(dst_low, lhs_low);
2213 }
2214 if (IsUint<16>(high)) {
2215 __ Andi(dst_high, lhs_high, high);
2216 } else if (high != 0xFFFFFFFF) {
2217 if (high != low) {
2218 __ LoadConst32(TMP, high);
2219 }
2220 __ And(dst_high, lhs_high, TMP);
2221 } else if (dst_high != lhs_high) {
2222 __ Move(dst_high, lhs_high);
2223 }
2224 } else {
2225 if (instruction->IsSub()) {
2226 value = -value;
2227 } else {
2228 DCHECK(instruction->IsAdd());
2229 }
2230 int32_t low = Low32Bits(value);
2231 int32_t high = High32Bits(value);
2232 if (IsInt<16>(low)) {
2233 if (dst_low != lhs_low || low != 0) {
2234 __ Addiu(dst_low, lhs_low, low);
2235 }
2236 if (low != 0) {
2237 __ Sltiu(AT, dst_low, low);
2238 }
2239 } else {
2240 __ LoadConst32(TMP, low);
2241 __ Addu(dst_low, lhs_low, TMP);
2242 __ Sltu(AT, dst_low, TMP);
2243 }
2244 if (IsInt<16>(high)) {
2245 if (dst_high != lhs_high || high != 0) {
2246 __ Addiu(dst_high, lhs_high, high);
2247 }
2248 } else {
2249 if (high != low) {
2250 __ LoadConst32(TMP, high);
2251 }
2252 __ Addu(dst_high, lhs_high, TMP);
2253 }
2254 if (low != 0) {
2255 __ Addu(dst_high, dst_high, AT);
2256 }
2257 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002258 }
2259 break;
2260 }
2261
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002262 case DataType::Type::kFloat32:
2263 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002264 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
2265 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2266 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2267 if (instruction->IsAdd()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002268 if (type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002269 __ AddS(dst, lhs, rhs);
2270 } else {
2271 __ AddD(dst, lhs, rhs);
2272 }
2273 } else {
2274 DCHECK(instruction->IsSub());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002275 if (type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002276 __ SubS(dst, lhs, rhs);
2277 } else {
2278 __ SubD(dst, lhs, rhs);
2279 }
2280 }
2281 break;
2282 }
2283
2284 default:
2285 LOG(FATAL) << "Unexpected binary operation type " << type;
2286 }
2287}
2288
2289void LocationsBuilderMIPS::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002290 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002291
2292 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002293 DataType::Type type = instr->GetResultType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002294 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002295 case DataType::Type::kInt32:
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002296 locations->SetInAt(0, Location::RequiresRegister());
2297 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
2298 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2299 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002300 case DataType::Type::kInt64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002301 locations->SetInAt(0, Location::RequiresRegister());
2302 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
2303 locations->SetOut(Location::RequiresRegister());
2304 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002305 default:
2306 LOG(FATAL) << "Unexpected shift type " << type;
2307 }
2308}
2309
2310static constexpr size_t kMipsBitsPerWord = kMipsWordSize * kBitsPerByte;
2311
2312void InstructionCodeGeneratorMIPS::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002313 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002314 LocationSummary* locations = instr->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002315 DataType::Type type = instr->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002316
2317 Location rhs_location = locations->InAt(1);
2318 bool use_imm = rhs_location.IsConstant();
2319 Register rhs_reg = use_imm ? ZERO : rhs_location.AsRegister<Register>();
2320 int64_t rhs_imm = use_imm ? CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()) : 0;
Roland Levillain5b5b9312016-03-22 14:57:31 +00002321 const uint32_t shift_mask =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002322 (type == DataType::Type::kInt32) ? kMaxIntShiftDistance : kMaxLongShiftDistance;
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002323 const uint32_t shift_value = rhs_imm & shift_mask;
Alexey Frunze92d90602015-12-18 18:16:36 -08002324 // Are the INS (Insert Bit Field) and ROTR instructions supported?
2325 bool has_ins_rotr = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002326
2327 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002328 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002329 Register dst = locations->Out().AsRegister<Register>();
2330 Register lhs = locations->InAt(0).AsRegister<Register>();
2331 if (use_imm) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002332 if (shift_value == 0) {
2333 if (dst != lhs) {
2334 __ Move(dst, lhs);
2335 }
2336 } else if (instr->IsShl()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002337 __ Sll(dst, lhs, shift_value);
2338 } else if (instr->IsShr()) {
2339 __ Sra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002340 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002341 __ Srl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002342 } else {
2343 if (has_ins_rotr) {
2344 __ Rotr(dst, lhs, shift_value);
2345 } else {
2346 __ Sll(TMP, lhs, (kMipsBitsPerWord - shift_value) & shift_mask);
2347 __ Srl(dst, lhs, shift_value);
2348 __ Or(dst, dst, TMP);
2349 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002350 }
2351 } else {
2352 if (instr->IsShl()) {
2353 __ Sllv(dst, lhs, rhs_reg);
2354 } else if (instr->IsShr()) {
2355 __ Srav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08002356 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002357 __ Srlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08002358 } else {
2359 if (has_ins_rotr) {
2360 __ Rotrv(dst, lhs, rhs_reg);
2361 } else {
2362 __ Subu(TMP, ZERO, rhs_reg);
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002363 // 32-bit shift instructions use the 5 least significant bits of the shift count, so
2364 // shifting by `-rhs_reg` is equivalent to shifting by `(32 - rhs_reg) & 31`. The case
2365 // when `rhs_reg & 31 == 0` is OK even though we don't shift `lhs` left all the way out
2366 // by 32, because the result in this case is computed as `(lhs >> 0) | (lhs << 0)`,
2367 // IOW, the OR'd values are equal.
Alexey Frunze92d90602015-12-18 18:16:36 -08002368 __ Sllv(TMP, lhs, TMP);
2369 __ Srlv(dst, lhs, rhs_reg);
2370 __ Or(dst, dst, TMP);
2371 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002372 }
2373 }
2374 break;
2375 }
2376
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002377 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002378 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
2379 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
2380 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
2381 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
2382 if (use_imm) {
2383 if (shift_value == 0) {
Lena Djokic8098da92017-06-28 12:07:50 +02002384 codegen_->MoveLocation(locations->Out(), locations->InAt(0), type);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002385 } else if (shift_value < kMipsBitsPerWord) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002386 if (has_ins_rotr) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002387 if (instr->IsShl()) {
2388 __ Srl(dst_high, lhs_low, kMipsBitsPerWord - shift_value);
2389 __ Ins(dst_high, lhs_high, shift_value, kMipsBitsPerWord - shift_value);
2390 __ Sll(dst_low, lhs_low, shift_value);
2391 } else if (instr->IsShr()) {
2392 __ Srl(dst_low, lhs_low, shift_value);
2393 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
2394 __ Sra(dst_high, lhs_high, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002395 } else if (instr->IsUShr()) {
2396 __ Srl(dst_low, lhs_low, shift_value);
2397 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
2398 __ Srl(dst_high, lhs_high, shift_value);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002399 } else {
2400 __ Srl(dst_low, lhs_low, shift_value);
2401 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
2402 __ Srl(dst_high, lhs_high, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002403 __ Ins(dst_high, lhs_low, kMipsBitsPerWord - shift_value, shift_value);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002404 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002405 } else {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002406 if (instr->IsShl()) {
2407 __ Sll(dst_low, lhs_low, shift_value);
2408 __ Srl(TMP, lhs_low, kMipsBitsPerWord - shift_value);
2409 __ Sll(dst_high, lhs_high, shift_value);
2410 __ Or(dst_high, dst_high, TMP);
2411 } else if (instr->IsShr()) {
2412 __ Sra(dst_high, lhs_high, shift_value);
2413 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
2414 __ Srl(dst_low, lhs_low, shift_value);
2415 __ Or(dst_low, dst_low, TMP);
Alexey Frunze92d90602015-12-18 18:16:36 -08002416 } else if (instr->IsUShr()) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002417 __ Srl(dst_high, lhs_high, shift_value);
2418 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
2419 __ Srl(dst_low, lhs_low, shift_value);
2420 __ Or(dst_low, dst_low, TMP);
Alexey Frunze92d90602015-12-18 18:16:36 -08002421 } else {
2422 __ Srl(TMP, lhs_low, shift_value);
2423 __ Sll(dst_low, lhs_high, kMipsBitsPerWord - shift_value);
2424 __ Or(dst_low, dst_low, TMP);
2425 __ Srl(TMP, lhs_high, shift_value);
2426 __ Sll(dst_high, lhs_low, kMipsBitsPerWord - shift_value);
2427 __ Or(dst_high, dst_high, TMP);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002428 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002429 }
2430 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002431 const uint32_t shift_value_high = shift_value - kMipsBitsPerWord;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002432 if (instr->IsShl()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002433 __ Sll(dst_high, lhs_low, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002434 __ Move(dst_low, ZERO);
2435 } else if (instr->IsShr()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002436 __ Sra(dst_low, lhs_high, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002437 __ Sra(dst_high, dst_low, kMipsBitsPerWord - 1);
Alexey Frunze92d90602015-12-18 18:16:36 -08002438 } else if (instr->IsUShr()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002439 __ Srl(dst_low, lhs_high, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002440 __ Move(dst_high, ZERO);
Alexey Frunze92d90602015-12-18 18:16:36 -08002441 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002442 if (shift_value == kMipsBitsPerWord) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002443 // 64-bit rotation by 32 is just a swap.
2444 __ Move(dst_low, lhs_high);
2445 __ Move(dst_high, lhs_low);
2446 } else {
2447 if (has_ins_rotr) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002448 __ Srl(dst_low, lhs_high, shift_value_high);
2449 __ Ins(dst_low, lhs_low, kMipsBitsPerWord - shift_value_high, shift_value_high);
2450 __ Srl(dst_high, lhs_low, shift_value_high);
2451 __ Ins(dst_high, lhs_high, kMipsBitsPerWord - shift_value_high, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08002452 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002453 __ Sll(TMP, lhs_low, kMipsBitsPerWord - shift_value_high);
2454 __ Srl(dst_low, lhs_high, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08002455 __ Or(dst_low, dst_low, TMP);
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002456 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value_high);
2457 __ Srl(dst_high, lhs_low, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08002458 __ Or(dst_high, dst_high, TMP);
2459 }
2460 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002461 }
2462 }
2463 } else {
2464 MipsLabel done;
2465 if (instr->IsShl()) {
2466 __ Sllv(dst_low, lhs_low, rhs_reg);
2467 __ Nor(AT, ZERO, rhs_reg);
2468 __ Srl(TMP, lhs_low, 1);
2469 __ Srlv(TMP, TMP, AT);
2470 __ Sllv(dst_high, lhs_high, rhs_reg);
2471 __ Or(dst_high, dst_high, TMP);
2472 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
2473 __ Beqz(TMP, &done);
2474 __ Move(dst_high, dst_low);
2475 __ Move(dst_low, ZERO);
2476 } else if (instr->IsShr()) {
2477 __ Srav(dst_high, lhs_high, rhs_reg);
2478 __ Nor(AT, ZERO, rhs_reg);
2479 __ Sll(TMP, lhs_high, 1);
2480 __ Sllv(TMP, TMP, AT);
2481 __ Srlv(dst_low, lhs_low, rhs_reg);
2482 __ Or(dst_low, dst_low, TMP);
2483 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
2484 __ Beqz(TMP, &done);
2485 __ Move(dst_low, dst_high);
2486 __ Sra(dst_high, dst_high, 31);
Alexey Frunze92d90602015-12-18 18:16:36 -08002487 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002488 __ Srlv(dst_high, lhs_high, rhs_reg);
2489 __ Nor(AT, ZERO, rhs_reg);
2490 __ Sll(TMP, lhs_high, 1);
2491 __ Sllv(TMP, TMP, AT);
2492 __ Srlv(dst_low, lhs_low, rhs_reg);
2493 __ Or(dst_low, dst_low, TMP);
2494 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
2495 __ Beqz(TMP, &done);
2496 __ Move(dst_low, dst_high);
2497 __ Move(dst_high, ZERO);
Alexey Frunze92d90602015-12-18 18:16:36 -08002498 } else {
2499 __ Nor(AT, ZERO, rhs_reg);
2500 __ Srlv(TMP, lhs_low, rhs_reg);
2501 __ Sll(dst_low, lhs_high, 1);
2502 __ Sllv(dst_low, dst_low, AT);
2503 __ Or(dst_low, dst_low, TMP);
2504 __ Srlv(TMP, lhs_high, rhs_reg);
2505 __ Sll(dst_high, lhs_low, 1);
2506 __ Sllv(dst_high, dst_high, AT);
2507 __ Or(dst_high, dst_high, TMP);
2508 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
2509 __ Beqz(TMP, &done);
2510 __ Move(TMP, dst_high);
2511 __ Move(dst_high, dst_low);
2512 __ Move(dst_low, TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002513 }
2514 __ Bind(&done);
2515 }
2516 break;
2517 }
2518
2519 default:
2520 LOG(FATAL) << "Unexpected shift operation type " << type;
2521 }
2522}
2523
2524void LocationsBuilderMIPS::VisitAdd(HAdd* instruction) {
2525 HandleBinaryOp(instruction);
2526}
2527
2528void InstructionCodeGeneratorMIPS::VisitAdd(HAdd* instruction) {
2529 HandleBinaryOp(instruction);
2530}
2531
2532void LocationsBuilderMIPS::VisitAnd(HAnd* instruction) {
2533 HandleBinaryOp(instruction);
2534}
2535
2536void InstructionCodeGeneratorMIPS::VisitAnd(HAnd* instruction) {
2537 HandleBinaryOp(instruction);
2538}
2539
2540void LocationsBuilderMIPS::VisitArrayGet(HArrayGet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002541 DataType::Type type = instruction->GetType();
Alexey Frunze15958152017-02-09 19:08:30 -08002542 bool object_array_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002543 kEmitCompilerReadBarrier && (type == DataType::Type::kReference);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002544 LocationSummary* locations =
Alexey Frunze15958152017-02-09 19:08:30 -08002545 new (GetGraph()->GetArena()) LocationSummary(instruction,
2546 object_array_get_with_read_barrier
2547 ? LocationSummary::kCallOnSlowPath
2548 : LocationSummary::kNoCall);
Alexey Frunzec61c0762017-04-10 13:54:23 -07002549 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
2550 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
2551 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002552 locations->SetInAt(0, Location::RequiresRegister());
2553 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002554 if (DataType::IsFloatingPointType(type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002555 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2556 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08002557 // The output overlaps in the case of an object array get with
2558 // read barriers enabled: we do not want the move to overwrite the
2559 // array's location, as we need it to emit the read barrier.
2560 locations->SetOut(Location::RequiresRegister(),
2561 object_array_get_with_read_barrier
2562 ? Location::kOutputOverlap
2563 : Location::kNoOutputOverlap);
2564 }
2565 // We need a temporary register for the read barrier marking slow
2566 // path in CodeGeneratorMIPS::GenerateArrayLoadWithBakerReadBarrier.
2567 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002568 bool temp_needed = instruction->GetIndex()->IsConstant()
2569 ? !kBakerReadBarrierThunksEnableForFields
2570 : !kBakerReadBarrierThunksEnableForArrays;
2571 if (temp_needed) {
2572 locations->AddTemp(Location::RequiresRegister());
2573 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002574 }
2575}
2576
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002577static auto GetImplicitNullChecker(HInstruction* instruction, CodeGeneratorMIPS* codegen) {
2578 auto null_checker = [codegen, instruction]() {
2579 codegen->MaybeRecordImplicitNullCheck(instruction);
Alexey Frunze2923db72016-08-20 01:55:47 -07002580 };
2581 return null_checker;
2582}
2583
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002584void InstructionCodeGeneratorMIPS::VisitArrayGet(HArrayGet* instruction) {
2585 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08002586 Location obj_loc = locations->InAt(0);
2587 Register obj = obj_loc.AsRegister<Register>();
2588 Location out_loc = locations->Out();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002589 Location index = locations->InAt(1);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002590 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002591 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002592
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002593 DataType::Type type = instruction->GetType();
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002594 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
2595 instruction->IsStringCharAt();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002596 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002597 case DataType::Type::kBool:
2598 case DataType::Type::kUint8: {
Alexey Frunze15958152017-02-09 19:08:30 -08002599 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002600 if (index.IsConstant()) {
2601 size_t offset =
2602 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002603 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002604 } else {
2605 __ Addu(TMP, obj, index.AsRegister<Register>());
Alexey Frunze2923db72016-08-20 01:55:47 -07002606 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002607 }
2608 break;
2609 }
2610
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002611 case DataType::Type::kInt8: {
Alexey Frunze15958152017-02-09 19:08:30 -08002612 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002613 if (index.IsConstant()) {
2614 size_t offset =
2615 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002616 __ LoadFromOffset(kLoadSignedByte, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002617 } else {
2618 __ Addu(TMP, obj, index.AsRegister<Register>());
Alexey Frunze2923db72016-08-20 01:55:47 -07002619 __ LoadFromOffset(kLoadSignedByte, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002620 }
2621 break;
2622 }
2623
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002624 case DataType::Type::kUint16: {
Alexey Frunze15958152017-02-09 19:08:30 -08002625 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002626 if (maybe_compressed_char_at) {
2627 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
2628 __ LoadFromOffset(kLoadWord, TMP, obj, count_offset, null_checker);
2629 __ Sll(TMP, TMP, 31); // Extract compression flag into the most significant bit of TMP.
2630 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2631 "Expecting 0=compressed, 1=uncompressed");
2632 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002633 if (index.IsConstant()) {
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002634 int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
2635 if (maybe_compressed_char_at) {
2636 MipsLabel uncompressed_load, done;
2637 __ Bnez(TMP, &uncompressed_load);
2638 __ LoadFromOffset(kLoadUnsignedByte,
2639 out,
2640 obj,
2641 data_offset + (const_index << TIMES_1));
2642 __ B(&done);
2643 __ Bind(&uncompressed_load);
2644 __ LoadFromOffset(kLoadUnsignedHalfword,
2645 out,
2646 obj,
2647 data_offset + (const_index << TIMES_2));
2648 __ Bind(&done);
2649 } else {
2650 __ LoadFromOffset(kLoadUnsignedHalfword,
2651 out,
2652 obj,
2653 data_offset + (const_index << TIMES_2),
2654 null_checker);
2655 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002656 } else {
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002657 Register index_reg = index.AsRegister<Register>();
2658 if (maybe_compressed_char_at) {
2659 MipsLabel uncompressed_load, done;
2660 __ Bnez(TMP, &uncompressed_load);
2661 __ Addu(TMP, obj, index_reg);
2662 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset);
2663 __ B(&done);
2664 __ Bind(&uncompressed_load);
Chris Larsencd0295d2017-03-31 15:26:54 -07002665 __ ShiftAndAdd(TMP, index_reg, obj, TIMES_2, TMP);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002666 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset);
2667 __ Bind(&done);
Lena Djokica2901602017-09-21 13:50:52 +02002668 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2669 __ Addu(TMP, index_reg, obj);
2670 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002671 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002672 __ ShiftAndAdd(TMP, index_reg, obj, TIMES_2, TMP);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002673 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset, null_checker);
2674 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002675 }
2676 break;
2677 }
2678
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002679 case DataType::Type::kInt16: {
2680 Register out = out_loc.AsRegister<Register>();
2681 if (index.IsConstant()) {
2682 size_t offset =
2683 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
2684 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset, null_checker);
Lena Djokica2901602017-09-21 13:50:52 +02002685 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2686 __ Addu(TMP, index.AsRegister<Register>(), obj);
2687 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset, null_checker);
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002688 } else {
2689 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_2, TMP);
2690 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset, null_checker);
2691 }
2692 break;
2693 }
2694
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002695 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002696 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
Alexey Frunze15958152017-02-09 19:08:30 -08002697 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002698 if (index.IsConstant()) {
2699 size_t offset =
2700 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002701 __ LoadFromOffset(kLoadWord, out, obj, offset, null_checker);
Lena Djokica2901602017-09-21 13:50:52 +02002702 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2703 __ Addu(TMP, index.AsRegister<Register>(), obj);
2704 __ LoadFromOffset(kLoadWord, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002705 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002706 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_4, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002707 __ LoadFromOffset(kLoadWord, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002708 }
2709 break;
2710 }
2711
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002712 case DataType::Type::kReference: {
Alexey Frunze15958152017-02-09 19:08:30 -08002713 static_assert(
2714 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2715 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2716 // /* HeapReference<Object> */ out =
2717 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
2718 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002719 bool temp_needed = index.IsConstant()
2720 ? !kBakerReadBarrierThunksEnableForFields
2721 : !kBakerReadBarrierThunksEnableForArrays;
2722 Location temp = temp_needed ? locations->GetTemp(0) : Location::NoLocation();
Alexey Frunze15958152017-02-09 19:08:30 -08002723 // Note that a potential implicit null check is handled in this
2724 // CodeGeneratorMIPS::GenerateArrayLoadWithBakerReadBarrier call.
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002725 DCHECK(!instruction->CanDoImplicitNullCheckOn(instruction->InputAt(0)));
2726 if (index.IsConstant()) {
2727 // Array load with a constant index can be treated as a field load.
2728 size_t offset =
2729 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2730 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
2731 out_loc,
2732 obj,
2733 offset,
2734 temp,
2735 /* needs_null_check */ false);
2736 } else {
2737 codegen_->GenerateArrayLoadWithBakerReadBarrier(instruction,
2738 out_loc,
2739 obj,
2740 data_offset,
2741 index,
2742 temp,
2743 /* needs_null_check */ false);
2744 }
Alexey Frunze15958152017-02-09 19:08:30 -08002745 } else {
2746 Register out = out_loc.AsRegister<Register>();
2747 if (index.IsConstant()) {
2748 size_t offset =
2749 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2750 __ LoadFromOffset(kLoadWord, out, obj, offset, null_checker);
2751 // If read barriers are enabled, emit read barriers other than
2752 // Baker's using a slow path (and also unpoison the loaded
2753 // reference, if heap poisoning is enabled).
2754 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
2755 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002756 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_4, TMP);
Alexey Frunze15958152017-02-09 19:08:30 -08002757 __ LoadFromOffset(kLoadWord, out, TMP, data_offset, null_checker);
2758 // If read barriers are enabled, emit read barriers other than
2759 // Baker's using a slow path (and also unpoison the loaded
2760 // reference, if heap poisoning is enabled).
2761 codegen_->MaybeGenerateReadBarrierSlow(instruction,
2762 out_loc,
2763 out_loc,
2764 obj_loc,
2765 data_offset,
2766 index);
2767 }
2768 }
2769 break;
2770 }
2771
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002772 case DataType::Type::kInt64: {
Alexey Frunze15958152017-02-09 19:08:30 -08002773 Register out = out_loc.AsRegisterPairLow<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002774 if (index.IsConstant()) {
2775 size_t offset =
2776 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002777 __ LoadFromOffset(kLoadDoubleword, out, obj, offset, null_checker);
Lena Djokica2901602017-09-21 13:50:52 +02002778 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2779 __ Addu(TMP, index.AsRegister<Register>(), obj);
2780 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002781 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002782 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_8, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002783 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002784 }
2785 break;
2786 }
2787
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002788 case DataType::Type::kFloat32: {
Alexey Frunze15958152017-02-09 19:08:30 -08002789 FRegister out = out_loc.AsFpuRegister<FRegister>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002790 if (index.IsConstant()) {
2791 size_t offset =
2792 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002793 __ LoadSFromOffset(out, obj, offset, null_checker);
Lena Djokica2901602017-09-21 13:50:52 +02002794 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2795 __ Addu(TMP, index.AsRegister<Register>(), obj);
2796 __ LoadSFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002797 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002798 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_4, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002799 __ LoadSFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002800 }
2801 break;
2802 }
2803
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002804 case DataType::Type::kFloat64: {
Alexey Frunze15958152017-02-09 19:08:30 -08002805 FRegister out = out_loc.AsFpuRegister<FRegister>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002806 if (index.IsConstant()) {
2807 size_t offset =
2808 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002809 __ LoadDFromOffset(out, obj, offset, null_checker);
Lena Djokica2901602017-09-21 13:50:52 +02002810 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2811 __ Addu(TMP, index.AsRegister<Register>(), obj);
2812 __ LoadDFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002813 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002814 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_8, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002815 __ LoadDFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002816 }
2817 break;
2818 }
2819
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002820 case DataType::Type::kVoid:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002821 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2822 UNREACHABLE();
2823 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002824}
2825
2826void LocationsBuilderMIPS::VisitArrayLength(HArrayLength* instruction) {
2827 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2828 locations->SetInAt(0, Location::RequiresRegister());
2829 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2830}
2831
2832void InstructionCodeGeneratorMIPS::VisitArrayLength(HArrayLength* instruction) {
2833 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01002834 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002835 Register obj = locations->InAt(0).AsRegister<Register>();
2836 Register out = locations->Out().AsRegister<Register>();
2837 __ LoadFromOffset(kLoadWord, out, obj, offset);
2838 codegen_->MaybeRecordImplicitNullCheck(instruction);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002839 // Mask out compression flag from String's array length.
2840 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
2841 __ Srl(out, out, 1u);
2842 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002843}
2844
Alexey Frunzef58b2482016-09-02 22:14:06 -07002845Location LocationsBuilderMIPS::RegisterOrZeroConstant(HInstruction* instruction) {
2846 return (instruction->IsConstant() && instruction->AsConstant()->IsZeroBitPattern())
2847 ? Location::ConstantLocation(instruction->AsConstant())
2848 : Location::RequiresRegister();
2849}
2850
2851Location LocationsBuilderMIPS::FpuRegisterOrConstantForStore(HInstruction* instruction) {
2852 // We can store 0.0 directly (from the ZERO register) without loading it into an FPU register.
2853 // We can store a non-zero float or double constant without first loading it into the FPU,
2854 // but we should only prefer this if the constant has a single use.
2855 if (instruction->IsConstant() &&
2856 (instruction->AsConstant()->IsZeroBitPattern() ||
2857 instruction->GetUses().HasExactlyOneElement())) {
2858 return Location::ConstantLocation(instruction->AsConstant());
2859 // Otherwise fall through and require an FPU register for the constant.
2860 }
2861 return Location::RequiresFpuRegister();
2862}
2863
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002864void LocationsBuilderMIPS::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002865 DataType::Type value_type = instruction->GetComponentType();
Alexey Frunze15958152017-02-09 19:08:30 -08002866
2867 bool needs_write_barrier =
2868 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
2869 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
2870
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002871 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2872 instruction,
Alexey Frunze15958152017-02-09 19:08:30 -08002873 may_need_runtime_call_for_type_check ?
2874 LocationSummary::kCallOnSlowPath :
2875 LocationSummary::kNoCall);
2876
2877 locations->SetInAt(0, Location::RequiresRegister());
2878 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002879 if (DataType::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
Alexey Frunze15958152017-02-09 19:08:30 -08002880 locations->SetInAt(2, FpuRegisterOrConstantForStore(instruction->InputAt(2)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002881 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08002882 locations->SetInAt(2, RegisterOrZeroConstant(instruction->InputAt(2)));
2883 }
2884 if (needs_write_barrier) {
2885 // Temporary register for the write barrier.
2886 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002887 }
2888}
2889
2890void InstructionCodeGeneratorMIPS::VisitArraySet(HArraySet* instruction) {
2891 LocationSummary* locations = instruction->GetLocations();
2892 Register obj = locations->InAt(0).AsRegister<Register>();
2893 Location index = locations->InAt(1);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002894 Location value_location = locations->InAt(2);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002895 DataType::Type value_type = instruction->GetComponentType();
Alexey Frunze15958152017-02-09 19:08:30 -08002896 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002897 bool needs_write_barrier =
2898 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002899 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002900 Register base_reg = index.IsConstant() ? obj : TMP;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002901
2902 switch (value_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002903 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002904 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002905 case DataType::Type::kInt8: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002906 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002907 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002908 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002909 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002910 __ Addu(base_reg, obj, index.AsRegister<Register>());
2911 }
2912 if (value_location.IsConstant()) {
2913 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2914 __ StoreConstToOffset(kStoreByte, value, base_reg, data_offset, TMP, null_checker);
2915 } else {
2916 Register value = value_location.AsRegister<Register>();
2917 __ StoreToOffset(kStoreByte, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002918 }
2919 break;
2920 }
2921
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002922 case DataType::Type::kUint16:
2923 case DataType::Type::kInt16: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002924 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002925 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002926 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2;
Lena Djokica2901602017-09-21 13:50:52 +02002927 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2928 __ Addu(base_reg, index.AsRegister<Register>(), obj);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002929 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002930 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_2, base_reg);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002931 }
2932 if (value_location.IsConstant()) {
2933 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2934 __ StoreConstToOffset(kStoreHalfword, value, base_reg, data_offset, TMP, null_checker);
2935 } else {
2936 Register value = value_location.AsRegister<Register>();
2937 __ StoreToOffset(kStoreHalfword, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002938 }
2939 break;
2940 }
2941
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002942 case DataType::Type::kInt32: {
Alexey Frunze15958152017-02-09 19:08:30 -08002943 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2944 if (index.IsConstant()) {
2945 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Lena Djokica2901602017-09-21 13:50:52 +02002946 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2947 __ Addu(base_reg, index.AsRegister<Register>(), obj);
Alexey Frunze15958152017-02-09 19:08:30 -08002948 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002949 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Alexey Frunze15958152017-02-09 19:08:30 -08002950 }
2951 if (value_location.IsConstant()) {
2952 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2953 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2954 } else {
2955 Register value = value_location.AsRegister<Register>();
2956 __ StoreToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
2957 }
2958 break;
2959 }
2960
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002961 case DataType::Type::kReference: {
Alexey Frunze15958152017-02-09 19:08:30 -08002962 if (value_location.IsConstant()) {
2963 // Just setting null.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002964 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002965 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002966 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002967 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002968 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002969 }
Alexey Frunze15958152017-02-09 19:08:30 -08002970 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2971 DCHECK_EQ(value, 0);
2972 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2973 DCHECK(!needs_write_barrier);
2974 DCHECK(!may_need_runtime_call_for_type_check);
2975 break;
2976 }
2977
2978 DCHECK(needs_write_barrier);
2979 Register value = value_location.AsRegister<Register>();
2980 Register temp1 = locations->GetTemp(0).AsRegister<Register>();
2981 Register temp2 = TMP; // Doesn't need to survive slow path.
2982 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2983 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2984 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2985 MipsLabel done;
2986 SlowPathCodeMIPS* slow_path = nullptr;
2987
2988 if (may_need_runtime_call_for_type_check) {
2989 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathMIPS(instruction);
2990 codegen_->AddSlowPath(slow_path);
2991 if (instruction->GetValueCanBeNull()) {
2992 MipsLabel non_zero;
2993 __ Bnez(value, &non_zero);
2994 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2995 if (index.IsConstant()) {
2996 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Lena Djokica2901602017-09-21 13:50:52 +02002997 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2998 __ Addu(base_reg, index.AsRegister<Register>(), obj);
Alexey Frunzec061de12017-02-14 13:27:23 -08002999 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003000 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Alexey Frunzec061de12017-02-14 13:27:23 -08003001 }
Alexey Frunze15958152017-02-09 19:08:30 -08003002 __ StoreToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
3003 __ B(&done);
3004 __ Bind(&non_zero);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003005 }
Alexey Frunze15958152017-02-09 19:08:30 -08003006
3007 // Note that when read barriers are enabled, the type checks
3008 // are performed without read barriers. This is fine, even in
3009 // the case where a class object is in the from-space after
3010 // the flip, as a comparison involving such a type would not
3011 // produce a false positive; it may of course produce a false
3012 // negative, in which case we would take the ArraySet slow
3013 // path.
3014
3015 // /* HeapReference<Class> */ temp1 = obj->klass_
3016 __ LoadFromOffset(kLoadWord, temp1, obj, class_offset, null_checker);
3017 __ MaybeUnpoisonHeapReference(temp1);
3018
3019 // /* HeapReference<Class> */ temp1 = temp1->component_type_
3020 __ LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
3021 // /* HeapReference<Class> */ temp2 = value->klass_
3022 __ LoadFromOffset(kLoadWord, temp2, value, class_offset);
3023 // If heap poisoning is enabled, no need to unpoison `temp1`
3024 // nor `temp2`, as we are comparing two poisoned references.
3025
3026 if (instruction->StaticTypeOfArrayIsObjectArray()) {
3027 MipsLabel do_put;
3028 __ Beq(temp1, temp2, &do_put);
3029 // If heap poisoning is enabled, the `temp1` reference has
3030 // not been unpoisoned yet; unpoison it now.
3031 __ MaybeUnpoisonHeapReference(temp1);
3032
3033 // /* HeapReference<Class> */ temp1 = temp1->super_class_
3034 __ LoadFromOffset(kLoadWord, temp1, temp1, super_offset);
3035 // If heap poisoning is enabled, no need to unpoison
3036 // `temp1`, as we are comparing against null below.
3037 __ Bnez(temp1, slow_path->GetEntryLabel());
3038 __ Bind(&do_put);
3039 } else {
3040 __ Bne(temp1, temp2, slow_path->GetEntryLabel());
3041 }
3042 }
3043
3044 Register source = value;
3045 if (kPoisonHeapReferences) {
3046 // Note that in the case where `value` is a null reference,
3047 // we do not enter this block, as a null reference does not
3048 // need poisoning.
3049 __ Move(temp1, value);
3050 __ PoisonHeapReference(temp1);
3051 source = temp1;
3052 }
3053
3054 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
3055 if (index.IsConstant()) {
3056 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003057 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003058 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Alexey Frunze15958152017-02-09 19:08:30 -08003059 }
3060 __ StoreToOffset(kStoreWord, source, base_reg, data_offset);
3061
3062 if (!may_need_runtime_call_for_type_check) {
3063 codegen_->MaybeRecordImplicitNullCheck(instruction);
3064 }
3065
3066 codegen_->MarkGCCard(obj, value, instruction->GetValueCanBeNull());
3067
3068 if (done.IsLinked()) {
3069 __ Bind(&done);
3070 }
3071
3072 if (slow_path != nullptr) {
3073 __ Bind(slow_path->GetExitLabel());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003074 }
3075 break;
3076 }
3077
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003078 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003079 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003080 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07003081 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Lena Djokica2901602017-09-21 13:50:52 +02003082 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
3083 __ Addu(base_reg, index.AsRegister<Register>(), obj);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003084 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003085 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_8, base_reg);
Alexey Frunzef58b2482016-09-02 22:14:06 -07003086 }
3087 if (value_location.IsConstant()) {
3088 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
3089 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
3090 } else {
3091 Register value = value_location.AsRegisterPairLow<Register>();
3092 __ StoreToOffset(kStoreDoubleword, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003093 }
3094 break;
3095 }
3096
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003097 case DataType::Type::kFloat32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003098 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003099 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07003100 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Lena Djokica2901602017-09-21 13:50:52 +02003101 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
3102 __ Addu(base_reg, index.AsRegister<Register>(), obj);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003103 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003104 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Alexey Frunzef58b2482016-09-02 22:14:06 -07003105 }
3106 if (value_location.IsConstant()) {
3107 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
3108 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
3109 } else {
3110 FRegister value = value_location.AsFpuRegister<FRegister>();
3111 __ StoreSToOffset(value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003112 }
3113 break;
3114 }
3115
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003116 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003117 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003118 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07003119 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Lena Djokica2901602017-09-21 13:50:52 +02003120 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
3121 __ Addu(base_reg, index.AsRegister<Register>(), obj);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003122 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003123 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_8, base_reg);
Alexey Frunzef58b2482016-09-02 22:14:06 -07003124 }
3125 if (value_location.IsConstant()) {
3126 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
3127 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
3128 } else {
3129 FRegister value = value_location.AsFpuRegister<FRegister>();
3130 __ StoreDToOffset(value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003131 }
3132 break;
3133 }
3134
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003135 case DataType::Type::kVoid:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003136 LOG(FATAL) << "Unreachable type " << instruction->GetType();
3137 UNREACHABLE();
3138 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003139}
3140
Lena Djokica2901602017-09-21 13:50:52 +02003141void LocationsBuilderMIPS::VisitIntermediateArrayAddressIndex(
3142 HIntermediateArrayAddressIndex* instruction) {
3143 LocationSummary* locations =
3144 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3145
3146 HIntConstant* shift = instruction->GetShift()->AsIntConstant();
3147
3148 locations->SetInAt(0, Location::RequiresRegister());
3149 locations->SetInAt(1, Location::ConstantLocation(shift));
3150 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3151}
3152
3153void InstructionCodeGeneratorMIPS::VisitIntermediateArrayAddressIndex(
3154 HIntermediateArrayAddressIndex* instruction) {
3155 LocationSummary* locations = instruction->GetLocations();
3156 Register index_reg = locations->InAt(0).AsRegister<Register>();
3157 uint32_t shift = instruction->GetShift()->AsIntConstant()->GetValue();
3158 __ Sll(locations->Out().AsRegister<Register>(), index_reg, shift);
3159}
3160
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003161void LocationsBuilderMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003162 RegisterSet caller_saves = RegisterSet::Empty();
3163 InvokeRuntimeCallingConvention calling_convention;
3164 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3165 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3166 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003167 locations->SetInAt(0, Location::RequiresRegister());
3168 locations->SetInAt(1, Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003169}
3170
3171void InstructionCodeGeneratorMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
3172 LocationSummary* locations = instruction->GetLocations();
3173 BoundsCheckSlowPathMIPS* slow_path =
3174 new (GetGraph()->GetArena()) BoundsCheckSlowPathMIPS(instruction);
3175 codegen_->AddSlowPath(slow_path);
3176
3177 Register index = locations->InAt(0).AsRegister<Register>();
3178 Register length = locations->InAt(1).AsRegister<Register>();
3179
3180 // length is limited by the maximum positive signed 32-bit integer.
3181 // Unsigned comparison of length and index checks for index < 0
3182 // and for length <= index simultaneously.
3183 __ Bgeu(index, length, slow_path->GetEntryLabel());
3184}
3185
Alexey Frunze15958152017-02-09 19:08:30 -08003186// Temp is used for read barrier.
3187static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
3188 if (kEmitCompilerReadBarrier &&
Alexey Frunze4147fcc2017-06-17 19:57:27 -07003189 !(kUseBakerReadBarrier && kBakerReadBarrierThunksEnableForFields) &&
Alexey Frunze15958152017-02-09 19:08:30 -08003190 (kUseBakerReadBarrier ||
3191 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3192 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3193 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
3194 return 1;
3195 }
3196 return 0;
3197}
3198
3199// Extra temp is used for read barrier.
3200static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
3201 return 1 + NumberOfInstanceOfTemps(type_check_kind);
3202}
3203
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003204void LocationsBuilderMIPS::VisitCheckCast(HCheckCast* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003205 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
3206 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
3207
3208 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
3209 switch (type_check_kind) {
3210 case TypeCheckKind::kExactCheck:
3211 case TypeCheckKind::kAbstractClassCheck:
3212 case TypeCheckKind::kClassHierarchyCheck:
3213 case TypeCheckKind::kArrayObjectCheck:
Alexey Frunze15958152017-02-09 19:08:30 -08003214 call_kind = (throws_into_catch || kEmitCompilerReadBarrier)
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003215 ? LocationSummary::kCallOnSlowPath
3216 : LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
3217 break;
3218 case TypeCheckKind::kArrayCheck:
3219 case TypeCheckKind::kUnresolvedCheck:
3220 case TypeCheckKind::kInterfaceCheck:
3221 call_kind = LocationSummary::kCallOnSlowPath;
3222 break;
3223 }
3224
3225 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003226 locations->SetInAt(0, Location::RequiresRegister());
3227 locations->SetInAt(1, Location::RequiresRegister());
Alexey Frunze15958152017-02-09 19:08:30 -08003228 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003229}
3230
3231void InstructionCodeGeneratorMIPS::VisitCheckCast(HCheckCast* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003232 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003233 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08003234 Location obj_loc = locations->InAt(0);
3235 Register obj = obj_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003236 Register cls = locations->InAt(1).AsRegister<Register>();
Alexey Frunze15958152017-02-09 19:08:30 -08003237 Location temp_loc = locations->GetTemp(0);
3238 Register temp = temp_loc.AsRegister<Register>();
3239 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
3240 DCHECK_LE(num_temps, 2u);
3241 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003242 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3243 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3244 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3245 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
3246 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
3247 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
3248 const uint32_t object_array_data_offset =
3249 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
3250 MipsLabel done;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003251
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003252 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
3253 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
3254 // read barriers is done for performance and code size reasons.
3255 bool is_type_check_slow_path_fatal = false;
3256 if (!kEmitCompilerReadBarrier) {
3257 is_type_check_slow_path_fatal =
3258 (type_check_kind == TypeCheckKind::kExactCheck ||
3259 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3260 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3261 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
3262 !instruction->CanThrowIntoCatchBlock();
3263 }
3264 SlowPathCodeMIPS* slow_path =
3265 new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS(instruction,
3266 is_type_check_slow_path_fatal);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003267 codegen_->AddSlowPath(slow_path);
3268
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003269 // Avoid this check if we know `obj` is not null.
3270 if (instruction->MustDoNullCheck()) {
3271 __ Beqz(obj, &done);
3272 }
3273
3274 switch (type_check_kind) {
3275 case TypeCheckKind::kExactCheck:
3276 case TypeCheckKind::kArrayCheck: {
3277 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003278 GenerateReferenceLoadTwoRegisters(instruction,
3279 temp_loc,
3280 obj_loc,
3281 class_offset,
3282 maybe_temp2_loc,
3283 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003284 // Jump to slow path for throwing the exception or doing a
3285 // more involved array check.
3286 __ Bne(temp, cls, slow_path->GetEntryLabel());
3287 break;
3288 }
3289
3290 case TypeCheckKind::kAbstractClassCheck: {
3291 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003292 GenerateReferenceLoadTwoRegisters(instruction,
3293 temp_loc,
3294 obj_loc,
3295 class_offset,
3296 maybe_temp2_loc,
3297 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003298 // If the class is abstract, we eagerly fetch the super class of the
3299 // object to avoid doing a comparison we know will fail.
3300 MipsLabel loop;
3301 __ Bind(&loop);
3302 // /* HeapReference<Class> */ temp = temp->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08003303 GenerateReferenceLoadOneRegister(instruction,
3304 temp_loc,
3305 super_offset,
3306 maybe_temp2_loc,
3307 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003308 // If the class reference currently in `temp` is null, jump to the slow path to throw the
3309 // exception.
3310 __ Beqz(temp, slow_path->GetEntryLabel());
3311 // Otherwise, compare the classes.
3312 __ Bne(temp, cls, &loop);
3313 break;
3314 }
3315
3316 case TypeCheckKind::kClassHierarchyCheck: {
3317 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003318 GenerateReferenceLoadTwoRegisters(instruction,
3319 temp_loc,
3320 obj_loc,
3321 class_offset,
3322 maybe_temp2_loc,
3323 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003324 // Walk over the class hierarchy to find a match.
3325 MipsLabel loop;
3326 __ Bind(&loop);
3327 __ Beq(temp, cls, &done);
3328 // /* HeapReference<Class> */ temp = temp->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08003329 GenerateReferenceLoadOneRegister(instruction,
3330 temp_loc,
3331 super_offset,
3332 maybe_temp2_loc,
3333 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003334 // If the class reference currently in `temp` is null, jump to the slow path to throw the
3335 // exception. Otherwise, jump to the beginning of the loop.
3336 __ Bnez(temp, &loop);
3337 __ B(slow_path->GetEntryLabel());
3338 break;
3339 }
3340
3341 case TypeCheckKind::kArrayObjectCheck: {
3342 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003343 GenerateReferenceLoadTwoRegisters(instruction,
3344 temp_loc,
3345 obj_loc,
3346 class_offset,
3347 maybe_temp2_loc,
3348 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003349 // Do an exact check.
3350 __ Beq(temp, cls, &done);
3351 // Otherwise, we need to check that the object's class is a non-primitive array.
3352 // /* HeapReference<Class> */ temp = temp->component_type_
Alexey Frunze15958152017-02-09 19:08:30 -08003353 GenerateReferenceLoadOneRegister(instruction,
3354 temp_loc,
3355 component_offset,
3356 maybe_temp2_loc,
3357 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003358 // If the component type is null, jump to the slow path to throw the exception.
3359 __ Beqz(temp, slow_path->GetEntryLabel());
3360 // Otherwise, the object is indeed an array, further check that this component
3361 // type is not a primitive type.
3362 __ LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
3363 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
3364 __ Bnez(temp, slow_path->GetEntryLabel());
3365 break;
3366 }
3367
3368 case TypeCheckKind::kUnresolvedCheck:
3369 // We always go into the type check slow path for the unresolved check case.
3370 // We cannot directly call the CheckCast runtime entry point
3371 // without resorting to a type checking slow path here (i.e. by
3372 // calling InvokeRuntime directly), as it would require to
3373 // assign fixed registers for the inputs of this HInstanceOf
3374 // instruction (following the runtime calling convention), which
3375 // might be cluttered by the potential first read barrier
3376 // emission at the beginning of this method.
3377 __ B(slow_path->GetEntryLabel());
3378 break;
3379
3380 case TypeCheckKind::kInterfaceCheck: {
3381 // Avoid read barriers to improve performance of the fast path. We can not get false
3382 // positives by doing this.
3383 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003384 GenerateReferenceLoadTwoRegisters(instruction,
3385 temp_loc,
3386 obj_loc,
3387 class_offset,
3388 maybe_temp2_loc,
3389 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003390 // /* HeapReference<Class> */ temp = temp->iftable_
Alexey Frunze15958152017-02-09 19:08:30 -08003391 GenerateReferenceLoadTwoRegisters(instruction,
3392 temp_loc,
3393 temp_loc,
3394 iftable_offset,
3395 maybe_temp2_loc,
3396 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003397 // Iftable is never null.
3398 __ Lw(TMP, temp, array_length_offset);
3399 // Loop through the iftable and check if any class matches.
3400 MipsLabel loop;
3401 __ Bind(&loop);
3402 __ Addiu(temp, temp, 2 * kHeapReferenceSize); // Possibly in delay slot on R2.
3403 __ Beqz(TMP, slow_path->GetEntryLabel());
3404 __ Lw(AT, temp, object_array_data_offset - 2 * kHeapReferenceSize);
3405 __ MaybeUnpoisonHeapReference(AT);
3406 // Go to next interface.
3407 __ Addiu(TMP, TMP, -2);
3408 // Compare the classes and continue the loop if they do not match.
3409 __ Bne(AT, cls, &loop);
3410 break;
3411 }
3412 }
3413
3414 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003415 __ Bind(slow_path->GetExitLabel());
3416}
3417
3418void LocationsBuilderMIPS::VisitClinitCheck(HClinitCheck* check) {
3419 LocationSummary* locations =
3420 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
3421 locations->SetInAt(0, Location::RequiresRegister());
3422 if (check->HasUses()) {
3423 locations->SetOut(Location::SameAsFirstInput());
3424 }
3425}
3426
3427void InstructionCodeGeneratorMIPS::VisitClinitCheck(HClinitCheck* check) {
3428 // We assume the class is not null.
3429 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS(
3430 check->GetLoadClass(),
3431 check,
3432 check->GetDexPc(),
3433 true);
3434 codegen_->AddSlowPath(slow_path);
3435 GenerateClassInitializationCheck(slow_path,
3436 check->GetLocations()->InAt(0).AsRegister<Register>());
3437}
3438
3439void LocationsBuilderMIPS::VisitCompare(HCompare* compare) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003440 DataType::Type in_type = compare->InputAt(0)->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003441
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003442 LocationSummary* locations =
3443 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003444
3445 switch (in_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003446 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003447 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003448 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003449 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003450 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003451 case DataType::Type::kInt32:
Alexey Frunzee7697712016-09-15 21:37:49 -07003452 locations->SetInAt(0, Location::RequiresRegister());
3453 locations->SetInAt(1, Location::RequiresRegister());
3454 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3455 break;
3456
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003457 case DataType::Type::kInt64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003458 locations->SetInAt(0, Location::RequiresRegister());
3459 locations->SetInAt(1, Location::RequiresRegister());
3460 // Output overlaps because it is written before doing the low comparison.
3461 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3462 break;
3463
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003464 case DataType::Type::kFloat32:
3465 case DataType::Type::kFloat64:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003466 locations->SetInAt(0, Location::RequiresFpuRegister());
3467 locations->SetInAt(1, Location::RequiresFpuRegister());
3468 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003469 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003470
3471 default:
3472 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
3473 }
3474}
3475
3476void InstructionCodeGeneratorMIPS::VisitCompare(HCompare* instruction) {
3477 LocationSummary* locations = instruction->GetLocations();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003478 Register res = locations->Out().AsRegister<Register>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003479 DataType::Type in_type = instruction->InputAt(0)->GetType();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003480 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003481
3482 // 0 if: left == right
3483 // 1 if: left > right
3484 // -1 if: left < right
3485 switch (in_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003486 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003487 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003488 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003489 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003490 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003491 case DataType::Type::kInt32: {
Aart Bika19616e2016-02-01 18:57:58 -08003492 Register lhs = locations->InAt(0).AsRegister<Register>();
3493 Register rhs = locations->InAt(1).AsRegister<Register>();
3494 __ Slt(TMP, lhs, rhs);
3495 __ Slt(res, rhs, lhs);
3496 __ Subu(res, res, TMP);
3497 break;
3498 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003499 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003500 MipsLabel done;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003501 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
3502 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
3503 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
3504 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
3505 // TODO: more efficient (direct) comparison with a constant.
3506 __ Slt(TMP, lhs_high, rhs_high);
3507 __ Slt(AT, rhs_high, lhs_high); // Inverted: is actually gt.
3508 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
3509 __ Bnez(res, &done); // If we compared ==, check if lower bits are also equal.
3510 __ Sltu(TMP, lhs_low, rhs_low);
3511 __ Sltu(AT, rhs_low, lhs_low); // Inverted: is actually gt.
3512 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
3513 __ Bind(&done);
3514 break;
3515 }
3516
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003517 case DataType::Type::kFloat32: {
Roland Levillain32ca3752016-02-17 16:49:37 +00003518 bool gt_bias = instruction->IsGtBias();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003519 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3520 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
3521 MipsLabel done;
3522 if (isR6) {
3523 __ CmpEqS(FTMP, lhs, rhs);
3524 __ LoadConst32(res, 0);
3525 __ Bc1nez(FTMP, &done);
3526 if (gt_bias) {
3527 __ CmpLtS(FTMP, lhs, rhs);
3528 __ LoadConst32(res, -1);
3529 __ Bc1nez(FTMP, &done);
3530 __ LoadConst32(res, 1);
3531 } else {
3532 __ CmpLtS(FTMP, rhs, lhs);
3533 __ LoadConst32(res, 1);
3534 __ Bc1nez(FTMP, &done);
3535 __ LoadConst32(res, -1);
3536 }
3537 } else {
3538 if (gt_bias) {
3539 __ ColtS(0, lhs, rhs);
3540 __ LoadConst32(res, -1);
3541 __ Bc1t(0, &done);
3542 __ CeqS(0, lhs, rhs);
3543 __ LoadConst32(res, 1);
3544 __ Movt(res, ZERO, 0);
3545 } else {
3546 __ ColtS(0, rhs, lhs);
3547 __ LoadConst32(res, 1);
3548 __ Bc1t(0, &done);
3549 __ CeqS(0, lhs, rhs);
3550 __ LoadConst32(res, -1);
3551 __ Movt(res, ZERO, 0);
3552 }
3553 }
3554 __ Bind(&done);
3555 break;
3556 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003557 case DataType::Type::kFloat64: {
Roland Levillain32ca3752016-02-17 16:49:37 +00003558 bool gt_bias = instruction->IsGtBias();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003559 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3560 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
3561 MipsLabel done;
3562 if (isR6) {
3563 __ CmpEqD(FTMP, lhs, rhs);
3564 __ LoadConst32(res, 0);
3565 __ Bc1nez(FTMP, &done);
3566 if (gt_bias) {
3567 __ CmpLtD(FTMP, lhs, rhs);
3568 __ LoadConst32(res, -1);
3569 __ Bc1nez(FTMP, &done);
3570 __ LoadConst32(res, 1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003571 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003572 __ CmpLtD(FTMP, rhs, lhs);
3573 __ LoadConst32(res, 1);
3574 __ Bc1nez(FTMP, &done);
3575 __ LoadConst32(res, -1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003576 }
3577 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003578 if (gt_bias) {
3579 __ ColtD(0, lhs, rhs);
3580 __ LoadConst32(res, -1);
3581 __ Bc1t(0, &done);
3582 __ CeqD(0, lhs, rhs);
3583 __ LoadConst32(res, 1);
3584 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003585 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003586 __ ColtD(0, rhs, lhs);
3587 __ LoadConst32(res, 1);
3588 __ Bc1t(0, &done);
3589 __ CeqD(0, lhs, rhs);
3590 __ LoadConst32(res, -1);
3591 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003592 }
3593 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003594 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003595 break;
3596 }
3597
3598 default:
3599 LOG(FATAL) << "Unimplemented compare type " << in_type;
3600 }
3601}
3602
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003603void LocationsBuilderMIPS::HandleCondition(HCondition* instruction) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003604 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003605 switch (instruction->InputAt(0)->GetType()) {
3606 default:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003607 case DataType::Type::kInt64:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003608 locations->SetInAt(0, Location::RequiresRegister());
3609 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
3610 break;
3611
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003612 case DataType::Type::kFloat32:
3613 case DataType::Type::kFloat64:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003614 locations->SetInAt(0, Location::RequiresFpuRegister());
3615 locations->SetInAt(1, Location::RequiresFpuRegister());
3616 break;
3617 }
David Brazdilb3e773e2016-01-26 11:28:37 +00003618 if (!instruction->IsEmittedAtUseSite()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003619 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3620 }
3621}
3622
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003623void InstructionCodeGeneratorMIPS::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00003624 if (instruction->IsEmittedAtUseSite()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003625 return;
3626 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003627
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003628 DataType::Type type = instruction->InputAt(0)->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003629 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003630
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003631 switch (type) {
3632 default:
3633 // Integer case.
3634 GenerateIntCompare(instruction->GetCondition(), locations);
3635 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003636
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003637 case DataType::Type::kInt64:
Tijana Jakovljevic6d482aa2017-02-03 13:24:08 +01003638 GenerateLongCompare(instruction->GetCondition(), locations);
3639 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003640
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003641 case DataType::Type::kFloat32:
3642 case DataType::Type::kFloat64:
Alexey Frunze2ddb7172016-09-06 17:04:55 -07003643 GenerateFpCompare(instruction->GetCondition(), instruction->IsGtBias(), type, locations);
3644 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003645 }
3646}
3647
Alexey Frunze7e99e052015-11-24 19:28:01 -08003648void InstructionCodeGeneratorMIPS::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3649 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003650 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt32);
Alexey Frunze7e99e052015-11-24 19:28:01 -08003651
3652 LocationSummary* locations = instruction->GetLocations();
3653 Location second = locations->InAt(1);
3654 DCHECK(second.IsConstant());
3655
3656 Register out = locations->Out().AsRegister<Register>();
3657 Register dividend = locations->InAt(0).AsRegister<Register>();
3658 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3659 DCHECK(imm == 1 || imm == -1);
3660
3661 if (instruction->IsRem()) {
3662 __ Move(out, ZERO);
3663 } else {
3664 if (imm == -1) {
3665 __ Subu(out, ZERO, dividend);
3666 } else if (out != dividend) {
3667 __ Move(out, dividend);
3668 }
3669 }
3670}
3671
3672void InstructionCodeGeneratorMIPS::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
3673 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003674 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt32);
Alexey Frunze7e99e052015-11-24 19:28:01 -08003675
3676 LocationSummary* locations = instruction->GetLocations();
3677 Location second = locations->InAt(1);
3678 DCHECK(second.IsConstant());
3679
3680 Register out = locations->Out().AsRegister<Register>();
3681 Register dividend = locations->InAt(0).AsRegister<Register>();
3682 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003683 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Alexey Frunze7e99e052015-11-24 19:28:01 -08003684 int ctz_imm = CTZ(abs_imm);
3685
3686 if (instruction->IsDiv()) {
3687 if (ctz_imm == 1) {
3688 // Fast path for division by +/-2, which is very common.
3689 __ Srl(TMP, dividend, 31);
3690 } else {
3691 __ Sra(TMP, dividend, 31);
3692 __ Srl(TMP, TMP, 32 - ctz_imm);
3693 }
3694 __ Addu(out, dividend, TMP);
3695 __ Sra(out, out, ctz_imm);
3696 if (imm < 0) {
3697 __ Subu(out, ZERO, out);
3698 }
3699 } else {
3700 if (ctz_imm == 1) {
3701 // Fast path for modulo +/-2, which is very common.
3702 __ Sra(TMP, dividend, 31);
3703 __ Subu(out, dividend, TMP);
3704 __ Andi(out, out, 1);
3705 __ Addu(out, out, TMP);
3706 } else {
3707 __ Sra(TMP, dividend, 31);
3708 __ Srl(TMP, TMP, 32 - ctz_imm);
3709 __ Addu(out, dividend, TMP);
3710 if (IsUint<16>(abs_imm - 1)) {
3711 __ Andi(out, out, abs_imm - 1);
3712 } else {
3713 __ Sll(out, out, 32 - ctz_imm);
3714 __ Srl(out, out, 32 - ctz_imm);
3715 }
3716 __ Subu(out, out, TMP);
3717 }
3718 }
3719}
3720
3721void InstructionCodeGeneratorMIPS::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3722 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003723 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt32);
Alexey Frunze7e99e052015-11-24 19:28:01 -08003724
3725 LocationSummary* locations = instruction->GetLocations();
3726 Location second = locations->InAt(1);
3727 DCHECK(second.IsConstant());
3728
3729 Register out = locations->Out().AsRegister<Register>();
3730 Register dividend = locations->InAt(0).AsRegister<Register>();
3731 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3732
3733 int64_t magic;
3734 int shift;
3735 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3736
3737 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3738
3739 __ LoadConst32(TMP, magic);
3740 if (isR6) {
3741 __ MuhR6(TMP, dividend, TMP);
3742 } else {
3743 __ MultR2(dividend, TMP);
3744 __ Mfhi(TMP);
3745 }
3746 if (imm > 0 && magic < 0) {
3747 __ Addu(TMP, TMP, dividend);
3748 } else if (imm < 0 && magic > 0) {
3749 __ Subu(TMP, TMP, dividend);
3750 }
3751
3752 if (shift != 0) {
3753 __ Sra(TMP, TMP, shift);
3754 }
3755
3756 if (instruction->IsDiv()) {
3757 __ Sra(out, TMP, 31);
3758 __ Subu(out, TMP, out);
3759 } else {
3760 __ Sra(AT, TMP, 31);
3761 __ Subu(AT, TMP, AT);
3762 __ LoadConst32(TMP, imm);
3763 if (isR6) {
3764 __ MulR6(TMP, AT, TMP);
3765 } else {
3766 __ MulR2(TMP, AT, TMP);
3767 }
3768 __ Subu(out, dividend, TMP);
3769 }
3770}
3771
3772void InstructionCodeGeneratorMIPS::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3773 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003774 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt32);
Alexey Frunze7e99e052015-11-24 19:28:01 -08003775
3776 LocationSummary* locations = instruction->GetLocations();
3777 Register out = locations->Out().AsRegister<Register>();
3778 Location second = locations->InAt(1);
3779
3780 if (second.IsConstant()) {
3781 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3782 if (imm == 0) {
3783 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3784 } else if (imm == 1 || imm == -1) {
3785 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003786 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08003787 DivRemByPowerOfTwo(instruction);
3788 } else {
3789 DCHECK(imm <= -2 || imm >= 2);
3790 GenerateDivRemWithAnyConstant(instruction);
3791 }
3792 } else {
3793 Register dividend = locations->InAt(0).AsRegister<Register>();
3794 Register divisor = second.AsRegister<Register>();
3795 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3796 if (instruction->IsDiv()) {
3797 if (isR6) {
3798 __ DivR6(out, dividend, divisor);
3799 } else {
3800 __ DivR2(out, dividend, divisor);
3801 }
3802 } else {
3803 if (isR6) {
3804 __ ModR6(out, dividend, divisor);
3805 } else {
3806 __ ModR2(out, dividend, divisor);
3807 }
3808 }
3809 }
3810}
3811
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003812void LocationsBuilderMIPS::VisitDiv(HDiv* div) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003813 DataType::Type type = div->GetResultType();
3814 LocationSummary::CallKind call_kind = (type == DataType::Type::kInt64)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003815 ? LocationSummary::kCallOnMainOnly
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003816 : LocationSummary::kNoCall;
3817
3818 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
3819
3820 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003821 case DataType::Type::kInt32:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003822 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08003823 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003824 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3825 break;
3826
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003827 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003828 InvokeRuntimeCallingConvention calling_convention;
3829 locations->SetInAt(0, Location::RegisterPairLocation(
3830 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3831 locations->SetInAt(1, Location::RegisterPairLocation(
3832 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3833 locations->SetOut(calling_convention.GetReturnLocation(type));
3834 break;
3835 }
3836
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003837 case DataType::Type::kFloat32:
3838 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003839 locations->SetInAt(0, Location::RequiresFpuRegister());
3840 locations->SetInAt(1, Location::RequiresFpuRegister());
3841 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3842 break;
3843
3844 default:
3845 LOG(FATAL) << "Unexpected div type " << type;
3846 }
3847}
3848
3849void InstructionCodeGeneratorMIPS::VisitDiv(HDiv* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003850 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003851 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003852
3853 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003854 case DataType::Type::kInt32:
Alexey Frunze7e99e052015-11-24 19:28:01 -08003855 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003856 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003857 case DataType::Type::kInt64: {
Serban Constantinescufca16662016-07-14 09:21:59 +01003858 codegen_->InvokeRuntime(kQuickLdiv, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003859 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
3860 break;
3861 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003862 case DataType::Type::kFloat32:
3863 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003864 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
3865 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3866 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003867 if (type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003868 __ DivS(dst, lhs, rhs);
3869 } else {
3870 __ DivD(dst, lhs, rhs);
3871 }
3872 break;
3873 }
3874 default:
3875 LOG(FATAL) << "Unexpected div type " << type;
3876 }
3877}
3878
3879void LocationsBuilderMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003880 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003881 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003882}
3883
3884void InstructionCodeGeneratorMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
3885 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathMIPS(instruction);
3886 codegen_->AddSlowPath(slow_path);
3887 Location value = instruction->GetLocations()->InAt(0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003888 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003889
3890 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003891 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003892 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003893 case DataType::Type::kInt8:
3894 case DataType::Type::kUint16:
3895 case DataType::Type::kInt16:
3896 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003897 if (value.IsConstant()) {
3898 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3899 __ B(slow_path->GetEntryLabel());
3900 } else {
3901 // A division by a non-null constant is valid. We don't need to perform
3902 // any check, so simply fall through.
3903 }
3904 } else {
3905 DCHECK(value.IsRegister()) << value;
3906 __ Beqz(value.AsRegister<Register>(), slow_path->GetEntryLabel());
3907 }
3908 break;
3909 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003910 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003911 if (value.IsConstant()) {
3912 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3913 __ B(slow_path->GetEntryLabel());
3914 } else {
3915 // A division by a non-null constant is valid. We don't need to perform
3916 // any check, so simply fall through.
3917 }
3918 } else {
3919 DCHECK(value.IsRegisterPair()) << value;
3920 __ Or(TMP, value.AsRegisterPairHigh<Register>(), value.AsRegisterPairLow<Register>());
3921 __ Beqz(TMP, slow_path->GetEntryLabel());
3922 }
3923 break;
3924 }
3925 default:
3926 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
3927 }
3928}
3929
3930void LocationsBuilderMIPS::VisitDoubleConstant(HDoubleConstant* constant) {
3931 LocationSummary* locations =
3932 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3933 locations->SetOut(Location::ConstantLocation(constant));
3934}
3935
3936void InstructionCodeGeneratorMIPS::VisitDoubleConstant(HDoubleConstant* cst ATTRIBUTE_UNUSED) {
3937 // Will be generated at use site.
3938}
3939
3940void LocationsBuilderMIPS::VisitExit(HExit* exit) {
3941 exit->SetLocations(nullptr);
3942}
3943
3944void InstructionCodeGeneratorMIPS::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
3945}
3946
3947void LocationsBuilderMIPS::VisitFloatConstant(HFloatConstant* constant) {
3948 LocationSummary* locations =
3949 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3950 locations->SetOut(Location::ConstantLocation(constant));
3951}
3952
3953void InstructionCodeGeneratorMIPS::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
3954 // Will be generated at use site.
3955}
3956
3957void LocationsBuilderMIPS::VisitGoto(HGoto* got) {
3958 got->SetLocations(nullptr);
3959}
3960
3961void InstructionCodeGeneratorMIPS::HandleGoto(HInstruction* got, HBasicBlock* successor) {
3962 DCHECK(!successor->IsExitBlock());
3963 HBasicBlock* block = got->GetBlock();
3964 HInstruction* previous = got->GetPrevious();
3965 HLoopInformation* info = block->GetLoopInformation();
3966
3967 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
3968 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
3969 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
3970 return;
3971 }
3972 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
3973 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
3974 }
3975 if (!codegen_->GoesToNextBlock(block, successor)) {
3976 __ B(codegen_->GetLabelOf(successor));
3977 }
3978}
3979
3980void InstructionCodeGeneratorMIPS::VisitGoto(HGoto* got) {
3981 HandleGoto(got, got->GetSuccessor());
3982}
3983
3984void LocationsBuilderMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
3985 try_boundary->SetLocations(nullptr);
3986}
3987
3988void InstructionCodeGeneratorMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
3989 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
3990 if (!successor->IsExitBlock()) {
3991 HandleGoto(try_boundary, successor);
3992 }
3993}
3994
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003995void InstructionCodeGeneratorMIPS::GenerateIntCompare(IfCondition cond,
3996 LocationSummary* locations) {
3997 Register dst = locations->Out().AsRegister<Register>();
3998 Register lhs = locations->InAt(0).AsRegister<Register>();
3999 Location rhs_location = locations->InAt(1);
4000 Register rhs_reg = ZERO;
4001 int64_t rhs_imm = 0;
4002 bool use_imm = rhs_location.IsConstant();
4003 if (use_imm) {
4004 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
4005 } else {
4006 rhs_reg = rhs_location.AsRegister<Register>();
4007 }
4008
4009 switch (cond) {
4010 case kCondEQ:
4011 case kCondNE:
Alexey Frunzee7697712016-09-15 21:37:49 -07004012 if (use_imm && IsInt<16>(-rhs_imm)) {
4013 if (rhs_imm == 0) {
4014 if (cond == kCondEQ) {
4015 __ Sltiu(dst, lhs, 1);
4016 } else {
4017 __ Sltu(dst, ZERO, lhs);
4018 }
4019 } else {
4020 __ Addiu(dst, lhs, -rhs_imm);
4021 if (cond == kCondEQ) {
4022 __ Sltiu(dst, dst, 1);
4023 } else {
4024 __ Sltu(dst, ZERO, dst);
4025 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004026 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004027 } else {
Alexey Frunzee7697712016-09-15 21:37:49 -07004028 if (use_imm && IsUint<16>(rhs_imm)) {
4029 __ Xori(dst, lhs, rhs_imm);
4030 } else {
4031 if (use_imm) {
4032 rhs_reg = TMP;
4033 __ LoadConst32(rhs_reg, rhs_imm);
4034 }
4035 __ Xor(dst, lhs, rhs_reg);
4036 }
4037 if (cond == kCondEQ) {
4038 __ Sltiu(dst, dst, 1);
4039 } else {
4040 __ Sltu(dst, ZERO, dst);
4041 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004042 }
4043 break;
4044
4045 case kCondLT:
4046 case kCondGE:
4047 if (use_imm && IsInt<16>(rhs_imm)) {
4048 __ Slti(dst, lhs, rhs_imm);
4049 } else {
4050 if (use_imm) {
4051 rhs_reg = TMP;
4052 __ LoadConst32(rhs_reg, rhs_imm);
4053 }
4054 __ Slt(dst, lhs, rhs_reg);
4055 }
4056 if (cond == kCondGE) {
4057 // Simulate lhs >= rhs via !(lhs < rhs) since there's
4058 // only the slt instruction but no sge.
4059 __ Xori(dst, dst, 1);
4060 }
4061 break;
4062
4063 case kCondLE:
4064 case kCondGT:
4065 if (use_imm && IsInt<16>(rhs_imm + 1)) {
4066 // Simulate lhs <= rhs via lhs < rhs + 1.
4067 __ Slti(dst, lhs, rhs_imm + 1);
4068 if (cond == kCondGT) {
4069 // Simulate lhs > rhs via !(lhs <= rhs) since there's
4070 // only the slti instruction but no sgti.
4071 __ Xori(dst, dst, 1);
4072 }
4073 } else {
4074 if (use_imm) {
4075 rhs_reg = TMP;
4076 __ LoadConst32(rhs_reg, rhs_imm);
4077 }
4078 __ Slt(dst, rhs_reg, lhs);
4079 if (cond == kCondLE) {
4080 // Simulate lhs <= rhs via !(rhs < lhs) since there's
4081 // only the slt instruction but no sle.
4082 __ Xori(dst, dst, 1);
4083 }
4084 }
4085 break;
4086
4087 case kCondB:
4088 case kCondAE:
4089 if (use_imm && IsInt<16>(rhs_imm)) {
4090 // Sltiu sign-extends its 16-bit immediate operand before
4091 // the comparison and thus lets us compare directly with
4092 // unsigned values in the ranges [0, 0x7fff] and
4093 // [0xffff8000, 0xffffffff].
4094 __ Sltiu(dst, lhs, rhs_imm);
4095 } else {
4096 if (use_imm) {
4097 rhs_reg = TMP;
4098 __ LoadConst32(rhs_reg, rhs_imm);
4099 }
4100 __ Sltu(dst, lhs, rhs_reg);
4101 }
4102 if (cond == kCondAE) {
4103 // Simulate lhs >= rhs via !(lhs < rhs) since there's
4104 // only the sltu instruction but no sgeu.
4105 __ Xori(dst, dst, 1);
4106 }
4107 break;
4108
4109 case kCondBE:
4110 case kCondA:
4111 if (use_imm && (rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4112 // Simulate lhs <= rhs via lhs < rhs + 1.
4113 // Note that this only works if rhs + 1 does not overflow
4114 // to 0, hence the check above.
4115 // Sltiu sign-extends its 16-bit immediate operand before
4116 // the comparison and thus lets us compare directly with
4117 // unsigned values in the ranges [0, 0x7fff] and
4118 // [0xffff8000, 0xffffffff].
4119 __ Sltiu(dst, lhs, rhs_imm + 1);
4120 if (cond == kCondA) {
4121 // Simulate lhs > rhs via !(lhs <= rhs) since there's
4122 // only the sltiu instruction but no sgtiu.
4123 __ Xori(dst, dst, 1);
4124 }
4125 } else {
4126 if (use_imm) {
4127 rhs_reg = TMP;
4128 __ LoadConst32(rhs_reg, rhs_imm);
4129 }
4130 __ Sltu(dst, rhs_reg, lhs);
4131 if (cond == kCondBE) {
4132 // Simulate lhs <= rhs via !(rhs < lhs) since there's
4133 // only the sltu instruction but no sleu.
4134 __ Xori(dst, dst, 1);
4135 }
4136 }
4137 break;
4138 }
4139}
4140
Alexey Frunze674b9ee2016-09-20 14:54:15 -07004141bool InstructionCodeGeneratorMIPS::MaterializeIntCompare(IfCondition cond,
4142 LocationSummary* input_locations,
4143 Register dst) {
4144 Register lhs = input_locations->InAt(0).AsRegister<Register>();
4145 Location rhs_location = input_locations->InAt(1);
4146 Register rhs_reg = ZERO;
4147 int64_t rhs_imm = 0;
4148 bool use_imm = rhs_location.IsConstant();
4149 if (use_imm) {
4150 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
4151 } else {
4152 rhs_reg = rhs_location.AsRegister<Register>();
4153 }
4154
4155 switch (cond) {
4156 case kCondEQ:
4157 case kCondNE:
4158 if (use_imm && IsInt<16>(-rhs_imm)) {
4159 __ Addiu(dst, lhs, -rhs_imm);
4160 } else if (use_imm && IsUint<16>(rhs_imm)) {
4161 __ Xori(dst, lhs, rhs_imm);
4162 } else {
4163 if (use_imm) {
4164 rhs_reg = TMP;
4165 __ LoadConst32(rhs_reg, rhs_imm);
4166 }
4167 __ Xor(dst, lhs, rhs_reg);
4168 }
4169 return (cond == kCondEQ);
4170
4171 case kCondLT:
4172 case kCondGE:
4173 if (use_imm && IsInt<16>(rhs_imm)) {
4174 __ Slti(dst, lhs, rhs_imm);
4175 } else {
4176 if (use_imm) {
4177 rhs_reg = TMP;
4178 __ LoadConst32(rhs_reg, rhs_imm);
4179 }
4180 __ Slt(dst, lhs, rhs_reg);
4181 }
4182 return (cond == kCondGE);
4183
4184 case kCondLE:
4185 case kCondGT:
4186 if (use_imm && IsInt<16>(rhs_imm + 1)) {
4187 // Simulate lhs <= rhs via lhs < rhs + 1.
4188 __ Slti(dst, lhs, rhs_imm + 1);
4189 return (cond == kCondGT);
4190 } else {
4191 if (use_imm) {
4192 rhs_reg = TMP;
4193 __ LoadConst32(rhs_reg, rhs_imm);
4194 }
4195 __ Slt(dst, rhs_reg, lhs);
4196 return (cond == kCondLE);
4197 }
4198
4199 case kCondB:
4200 case kCondAE:
4201 if (use_imm && IsInt<16>(rhs_imm)) {
4202 // Sltiu sign-extends its 16-bit immediate operand before
4203 // the comparison and thus lets us compare directly with
4204 // unsigned values in the ranges [0, 0x7fff] and
4205 // [0xffff8000, 0xffffffff].
4206 __ Sltiu(dst, lhs, rhs_imm);
4207 } else {
4208 if (use_imm) {
4209 rhs_reg = TMP;
4210 __ LoadConst32(rhs_reg, rhs_imm);
4211 }
4212 __ Sltu(dst, lhs, rhs_reg);
4213 }
4214 return (cond == kCondAE);
4215
4216 case kCondBE:
4217 case kCondA:
4218 if (use_imm && (rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4219 // Simulate lhs <= rhs via lhs < rhs + 1.
4220 // Note that this only works if rhs + 1 does not overflow
4221 // to 0, hence the check above.
4222 // Sltiu sign-extends its 16-bit immediate operand before
4223 // the comparison and thus lets us compare directly with
4224 // unsigned values in the ranges [0, 0x7fff] and
4225 // [0xffff8000, 0xffffffff].
4226 __ Sltiu(dst, lhs, rhs_imm + 1);
4227 return (cond == kCondA);
4228 } else {
4229 if (use_imm) {
4230 rhs_reg = TMP;
4231 __ LoadConst32(rhs_reg, rhs_imm);
4232 }
4233 __ Sltu(dst, rhs_reg, lhs);
4234 return (cond == kCondBE);
4235 }
4236 }
4237}
4238
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004239void InstructionCodeGeneratorMIPS::GenerateIntCompareAndBranch(IfCondition cond,
4240 LocationSummary* locations,
4241 MipsLabel* label) {
4242 Register lhs = locations->InAt(0).AsRegister<Register>();
4243 Location rhs_location = locations->InAt(1);
4244 Register rhs_reg = ZERO;
Alexey Frunzee7697712016-09-15 21:37:49 -07004245 int64_t rhs_imm = 0;
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004246 bool use_imm = rhs_location.IsConstant();
4247 if (use_imm) {
4248 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
4249 } else {
4250 rhs_reg = rhs_location.AsRegister<Register>();
4251 }
4252
4253 if (use_imm && rhs_imm == 0) {
4254 switch (cond) {
4255 case kCondEQ:
4256 case kCondBE: // <= 0 if zero
4257 __ Beqz(lhs, label);
4258 break;
4259 case kCondNE:
4260 case kCondA: // > 0 if non-zero
4261 __ Bnez(lhs, label);
4262 break;
4263 case kCondLT:
4264 __ Bltz(lhs, label);
4265 break;
4266 case kCondGE:
4267 __ Bgez(lhs, label);
4268 break;
4269 case kCondLE:
4270 __ Blez(lhs, label);
4271 break;
4272 case kCondGT:
4273 __ Bgtz(lhs, label);
4274 break;
4275 case kCondB: // always false
4276 break;
4277 case kCondAE: // always true
4278 __ B(label);
4279 break;
4280 }
4281 } else {
Alexey Frunzee7697712016-09-15 21:37:49 -07004282 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
4283 if (isR6 || !use_imm) {
4284 if (use_imm) {
4285 rhs_reg = TMP;
4286 __ LoadConst32(rhs_reg, rhs_imm);
4287 }
4288 switch (cond) {
4289 case kCondEQ:
4290 __ Beq(lhs, rhs_reg, label);
4291 break;
4292 case kCondNE:
4293 __ Bne(lhs, rhs_reg, label);
4294 break;
4295 case kCondLT:
4296 __ Blt(lhs, rhs_reg, label);
4297 break;
4298 case kCondGE:
4299 __ Bge(lhs, rhs_reg, label);
4300 break;
4301 case kCondLE:
4302 __ Bge(rhs_reg, lhs, label);
4303 break;
4304 case kCondGT:
4305 __ Blt(rhs_reg, lhs, label);
4306 break;
4307 case kCondB:
4308 __ Bltu(lhs, rhs_reg, label);
4309 break;
4310 case kCondAE:
4311 __ Bgeu(lhs, rhs_reg, label);
4312 break;
4313 case kCondBE:
4314 __ Bgeu(rhs_reg, lhs, label);
4315 break;
4316 case kCondA:
4317 __ Bltu(rhs_reg, lhs, label);
4318 break;
4319 }
4320 } else {
4321 // Special cases for more efficient comparison with constants on R2.
4322 switch (cond) {
4323 case kCondEQ:
4324 __ LoadConst32(TMP, rhs_imm);
4325 __ Beq(lhs, TMP, label);
4326 break;
4327 case kCondNE:
4328 __ LoadConst32(TMP, rhs_imm);
4329 __ Bne(lhs, TMP, label);
4330 break;
4331 case kCondLT:
4332 if (IsInt<16>(rhs_imm)) {
4333 __ Slti(TMP, lhs, rhs_imm);
4334 __ Bnez(TMP, label);
4335 } else {
4336 __ LoadConst32(TMP, rhs_imm);
4337 __ Blt(lhs, TMP, label);
4338 }
4339 break;
4340 case kCondGE:
4341 if (IsInt<16>(rhs_imm)) {
4342 __ Slti(TMP, lhs, rhs_imm);
4343 __ Beqz(TMP, label);
4344 } else {
4345 __ LoadConst32(TMP, rhs_imm);
4346 __ Bge(lhs, TMP, label);
4347 }
4348 break;
4349 case kCondLE:
4350 if (IsInt<16>(rhs_imm + 1)) {
4351 // Simulate lhs <= rhs via lhs < rhs + 1.
4352 __ Slti(TMP, lhs, rhs_imm + 1);
4353 __ Bnez(TMP, label);
4354 } else {
4355 __ LoadConst32(TMP, rhs_imm);
4356 __ Bge(TMP, lhs, label);
4357 }
4358 break;
4359 case kCondGT:
4360 if (IsInt<16>(rhs_imm + 1)) {
4361 // Simulate lhs > rhs via !(lhs < rhs + 1).
4362 __ Slti(TMP, lhs, rhs_imm + 1);
4363 __ Beqz(TMP, label);
4364 } else {
4365 __ LoadConst32(TMP, rhs_imm);
4366 __ Blt(TMP, lhs, label);
4367 }
4368 break;
4369 case kCondB:
4370 if (IsInt<16>(rhs_imm)) {
4371 __ Sltiu(TMP, lhs, rhs_imm);
4372 __ Bnez(TMP, label);
4373 } else {
4374 __ LoadConst32(TMP, rhs_imm);
4375 __ Bltu(lhs, TMP, label);
4376 }
4377 break;
4378 case kCondAE:
4379 if (IsInt<16>(rhs_imm)) {
4380 __ Sltiu(TMP, lhs, rhs_imm);
4381 __ Beqz(TMP, label);
4382 } else {
4383 __ LoadConst32(TMP, rhs_imm);
4384 __ Bgeu(lhs, TMP, label);
4385 }
4386 break;
4387 case kCondBE:
4388 if ((rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4389 // Simulate lhs <= rhs via lhs < rhs + 1.
4390 // Note that this only works if rhs + 1 does not overflow
4391 // to 0, hence the check above.
4392 __ Sltiu(TMP, lhs, rhs_imm + 1);
4393 __ Bnez(TMP, label);
4394 } else {
4395 __ LoadConst32(TMP, rhs_imm);
4396 __ Bgeu(TMP, lhs, label);
4397 }
4398 break;
4399 case kCondA:
4400 if ((rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4401 // Simulate lhs > rhs via !(lhs < rhs + 1).
4402 // Note that this only works if rhs + 1 does not overflow
4403 // to 0, hence the check above.
4404 __ Sltiu(TMP, lhs, rhs_imm + 1);
4405 __ Beqz(TMP, label);
4406 } else {
4407 __ LoadConst32(TMP, rhs_imm);
4408 __ Bltu(TMP, lhs, label);
4409 }
4410 break;
4411 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004412 }
4413 }
4414}
4415
Tijana Jakovljevic6d482aa2017-02-03 13:24:08 +01004416void InstructionCodeGeneratorMIPS::GenerateLongCompare(IfCondition cond,
4417 LocationSummary* locations) {
4418 Register dst = locations->Out().AsRegister<Register>();
4419 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
4420 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
4421 Location rhs_location = locations->InAt(1);
4422 Register rhs_high = ZERO;
4423 Register rhs_low = ZERO;
4424 int64_t imm = 0;
4425 uint32_t imm_high = 0;
4426 uint32_t imm_low = 0;
4427 bool use_imm = rhs_location.IsConstant();
4428 if (use_imm) {
4429 imm = rhs_location.GetConstant()->AsLongConstant()->GetValue();
4430 imm_high = High32Bits(imm);
4431 imm_low = Low32Bits(imm);
4432 } else {
4433 rhs_high = rhs_location.AsRegisterPairHigh<Register>();
4434 rhs_low = rhs_location.AsRegisterPairLow<Register>();
4435 }
4436 if (use_imm && imm == 0) {
4437 switch (cond) {
4438 case kCondEQ:
4439 case kCondBE: // <= 0 if zero
4440 __ Or(dst, lhs_high, lhs_low);
4441 __ Sltiu(dst, dst, 1);
4442 break;
4443 case kCondNE:
4444 case kCondA: // > 0 if non-zero
4445 __ Or(dst, lhs_high, lhs_low);
4446 __ Sltu(dst, ZERO, dst);
4447 break;
4448 case kCondLT:
4449 __ Slt(dst, lhs_high, ZERO);
4450 break;
4451 case kCondGE:
4452 __ Slt(dst, lhs_high, ZERO);
4453 __ Xori(dst, dst, 1);
4454 break;
4455 case kCondLE:
4456 __ Or(TMP, lhs_high, lhs_low);
4457 __ Sra(AT, lhs_high, 31);
4458 __ Sltu(dst, AT, TMP);
4459 __ Xori(dst, dst, 1);
4460 break;
4461 case kCondGT:
4462 __ Or(TMP, lhs_high, lhs_low);
4463 __ Sra(AT, lhs_high, 31);
4464 __ Sltu(dst, AT, TMP);
4465 break;
4466 case kCondB: // always false
4467 __ Andi(dst, dst, 0);
4468 break;
4469 case kCondAE: // always true
4470 __ Ori(dst, ZERO, 1);
4471 break;
4472 }
4473 } else if (use_imm) {
4474 // TODO: more efficient comparison with constants without loading them into TMP/AT.
4475 switch (cond) {
4476 case kCondEQ:
4477 __ LoadConst32(TMP, imm_high);
4478 __ Xor(TMP, TMP, lhs_high);
4479 __ LoadConst32(AT, imm_low);
4480 __ Xor(AT, AT, lhs_low);
4481 __ Or(dst, TMP, AT);
4482 __ Sltiu(dst, dst, 1);
4483 break;
4484 case kCondNE:
4485 __ LoadConst32(TMP, imm_high);
4486 __ Xor(TMP, TMP, lhs_high);
4487 __ LoadConst32(AT, imm_low);
4488 __ Xor(AT, AT, lhs_low);
4489 __ Or(dst, TMP, AT);
4490 __ Sltu(dst, ZERO, dst);
4491 break;
4492 case kCondLT:
4493 case kCondGE:
4494 if (dst == lhs_low) {
4495 __ LoadConst32(TMP, imm_low);
4496 __ Sltu(dst, lhs_low, TMP);
4497 }
4498 __ LoadConst32(TMP, imm_high);
4499 __ Slt(AT, lhs_high, TMP);
4500 __ Slt(TMP, TMP, lhs_high);
4501 if (dst != lhs_low) {
4502 __ LoadConst32(dst, imm_low);
4503 __ Sltu(dst, lhs_low, dst);
4504 }
4505 __ Slt(dst, TMP, dst);
4506 __ Or(dst, dst, AT);
4507 if (cond == kCondGE) {
4508 __ Xori(dst, dst, 1);
4509 }
4510 break;
4511 case kCondGT:
4512 case kCondLE:
4513 if (dst == lhs_low) {
4514 __ LoadConst32(TMP, imm_low);
4515 __ Sltu(dst, TMP, lhs_low);
4516 }
4517 __ LoadConst32(TMP, imm_high);
4518 __ Slt(AT, TMP, lhs_high);
4519 __ Slt(TMP, lhs_high, TMP);
4520 if (dst != lhs_low) {
4521 __ LoadConst32(dst, imm_low);
4522 __ Sltu(dst, dst, lhs_low);
4523 }
4524 __ Slt(dst, TMP, dst);
4525 __ Or(dst, dst, AT);
4526 if (cond == kCondLE) {
4527 __ Xori(dst, dst, 1);
4528 }
4529 break;
4530 case kCondB:
4531 case kCondAE:
4532 if (dst == lhs_low) {
4533 __ LoadConst32(TMP, imm_low);
4534 __ Sltu(dst, lhs_low, TMP);
4535 }
4536 __ LoadConst32(TMP, imm_high);
4537 __ Sltu(AT, lhs_high, TMP);
4538 __ Sltu(TMP, TMP, lhs_high);
4539 if (dst != lhs_low) {
4540 __ LoadConst32(dst, imm_low);
4541 __ Sltu(dst, lhs_low, dst);
4542 }
4543 __ Slt(dst, TMP, dst);
4544 __ Or(dst, dst, AT);
4545 if (cond == kCondAE) {
4546 __ Xori(dst, dst, 1);
4547 }
4548 break;
4549 case kCondA:
4550 case kCondBE:
4551 if (dst == lhs_low) {
4552 __ LoadConst32(TMP, imm_low);
4553 __ Sltu(dst, TMP, lhs_low);
4554 }
4555 __ LoadConst32(TMP, imm_high);
4556 __ Sltu(AT, TMP, lhs_high);
4557 __ Sltu(TMP, lhs_high, TMP);
4558 if (dst != lhs_low) {
4559 __ LoadConst32(dst, imm_low);
4560 __ Sltu(dst, dst, lhs_low);
4561 }
4562 __ Slt(dst, TMP, dst);
4563 __ Or(dst, dst, AT);
4564 if (cond == kCondBE) {
4565 __ Xori(dst, dst, 1);
4566 }
4567 break;
4568 }
4569 } else {
4570 switch (cond) {
4571 case kCondEQ:
4572 __ Xor(TMP, lhs_high, rhs_high);
4573 __ Xor(AT, lhs_low, rhs_low);
4574 __ Or(dst, TMP, AT);
4575 __ Sltiu(dst, dst, 1);
4576 break;
4577 case kCondNE:
4578 __ Xor(TMP, lhs_high, rhs_high);
4579 __ Xor(AT, lhs_low, rhs_low);
4580 __ Or(dst, TMP, AT);
4581 __ Sltu(dst, ZERO, dst);
4582 break;
4583 case kCondLT:
4584 case kCondGE:
4585 __ Slt(TMP, rhs_high, lhs_high);
4586 __ Sltu(AT, lhs_low, rhs_low);
4587 __ Slt(TMP, TMP, AT);
4588 __ Slt(AT, lhs_high, rhs_high);
4589 __ Or(dst, AT, TMP);
4590 if (cond == kCondGE) {
4591 __ Xori(dst, dst, 1);
4592 }
4593 break;
4594 case kCondGT:
4595 case kCondLE:
4596 __ Slt(TMP, lhs_high, rhs_high);
4597 __ Sltu(AT, rhs_low, lhs_low);
4598 __ Slt(TMP, TMP, AT);
4599 __ Slt(AT, rhs_high, lhs_high);
4600 __ Or(dst, AT, TMP);
4601 if (cond == kCondLE) {
4602 __ Xori(dst, dst, 1);
4603 }
4604 break;
4605 case kCondB:
4606 case kCondAE:
4607 __ Sltu(TMP, rhs_high, lhs_high);
4608 __ Sltu(AT, lhs_low, rhs_low);
4609 __ Slt(TMP, TMP, AT);
4610 __ Sltu(AT, lhs_high, rhs_high);
4611 __ Or(dst, AT, TMP);
4612 if (cond == kCondAE) {
4613 __ Xori(dst, dst, 1);
4614 }
4615 break;
4616 case kCondA:
4617 case kCondBE:
4618 __ Sltu(TMP, lhs_high, rhs_high);
4619 __ Sltu(AT, rhs_low, lhs_low);
4620 __ Slt(TMP, TMP, AT);
4621 __ Sltu(AT, rhs_high, lhs_high);
4622 __ Or(dst, AT, TMP);
4623 if (cond == kCondBE) {
4624 __ Xori(dst, dst, 1);
4625 }
4626 break;
4627 }
4628 }
4629}
4630
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004631void InstructionCodeGeneratorMIPS::GenerateLongCompareAndBranch(IfCondition cond,
4632 LocationSummary* locations,
4633 MipsLabel* label) {
4634 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
4635 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
4636 Location rhs_location = locations->InAt(1);
4637 Register rhs_high = ZERO;
4638 Register rhs_low = ZERO;
4639 int64_t imm = 0;
4640 uint32_t imm_high = 0;
4641 uint32_t imm_low = 0;
4642 bool use_imm = rhs_location.IsConstant();
4643 if (use_imm) {
4644 imm = rhs_location.GetConstant()->AsLongConstant()->GetValue();
4645 imm_high = High32Bits(imm);
4646 imm_low = Low32Bits(imm);
4647 } else {
4648 rhs_high = rhs_location.AsRegisterPairHigh<Register>();
4649 rhs_low = rhs_location.AsRegisterPairLow<Register>();
4650 }
4651
4652 if (use_imm && imm == 0) {
4653 switch (cond) {
4654 case kCondEQ:
4655 case kCondBE: // <= 0 if zero
4656 __ Or(TMP, lhs_high, lhs_low);
4657 __ Beqz(TMP, label);
4658 break;
4659 case kCondNE:
4660 case kCondA: // > 0 if non-zero
4661 __ Or(TMP, lhs_high, lhs_low);
4662 __ Bnez(TMP, label);
4663 break;
4664 case kCondLT:
4665 __ Bltz(lhs_high, label);
4666 break;
4667 case kCondGE:
4668 __ Bgez(lhs_high, label);
4669 break;
4670 case kCondLE:
4671 __ Or(TMP, lhs_high, lhs_low);
4672 __ Sra(AT, lhs_high, 31);
4673 __ Bgeu(AT, TMP, label);
4674 break;
4675 case kCondGT:
4676 __ Or(TMP, lhs_high, lhs_low);
4677 __ Sra(AT, lhs_high, 31);
4678 __ Bltu(AT, TMP, label);
4679 break;
4680 case kCondB: // always false
4681 break;
4682 case kCondAE: // always true
4683 __ B(label);
4684 break;
4685 }
4686 } else if (use_imm) {
4687 // TODO: more efficient comparison with constants without loading them into TMP/AT.
4688 switch (cond) {
4689 case kCondEQ:
4690 __ LoadConst32(TMP, imm_high);
4691 __ Xor(TMP, TMP, lhs_high);
4692 __ LoadConst32(AT, imm_low);
4693 __ Xor(AT, AT, lhs_low);
4694 __ Or(TMP, TMP, AT);
4695 __ Beqz(TMP, label);
4696 break;
4697 case kCondNE:
4698 __ LoadConst32(TMP, imm_high);
4699 __ Xor(TMP, TMP, lhs_high);
4700 __ LoadConst32(AT, imm_low);
4701 __ Xor(AT, AT, lhs_low);
4702 __ Or(TMP, TMP, AT);
4703 __ Bnez(TMP, label);
4704 break;
4705 case kCondLT:
4706 __ LoadConst32(TMP, imm_high);
4707 __ Blt(lhs_high, TMP, label);
4708 __ Slt(TMP, TMP, lhs_high);
4709 __ LoadConst32(AT, imm_low);
4710 __ Sltu(AT, lhs_low, AT);
4711 __ Blt(TMP, AT, label);
4712 break;
4713 case kCondGE:
4714 __ LoadConst32(TMP, imm_high);
4715 __ Blt(TMP, lhs_high, label);
4716 __ Slt(TMP, lhs_high, TMP);
4717 __ LoadConst32(AT, imm_low);
4718 __ Sltu(AT, lhs_low, AT);
4719 __ Or(TMP, TMP, AT);
4720 __ Beqz(TMP, label);
4721 break;
4722 case kCondLE:
4723 __ LoadConst32(TMP, imm_high);
4724 __ Blt(lhs_high, TMP, label);
4725 __ Slt(TMP, TMP, lhs_high);
4726 __ LoadConst32(AT, imm_low);
4727 __ Sltu(AT, AT, lhs_low);
4728 __ Or(TMP, TMP, AT);
4729 __ Beqz(TMP, label);
4730 break;
4731 case kCondGT:
4732 __ LoadConst32(TMP, imm_high);
4733 __ Blt(TMP, lhs_high, label);
4734 __ Slt(TMP, lhs_high, TMP);
4735 __ LoadConst32(AT, imm_low);
4736 __ Sltu(AT, AT, lhs_low);
4737 __ Blt(TMP, AT, label);
4738 break;
4739 case kCondB:
4740 __ LoadConst32(TMP, imm_high);
4741 __ Bltu(lhs_high, TMP, label);
4742 __ Sltu(TMP, TMP, lhs_high);
4743 __ LoadConst32(AT, imm_low);
4744 __ Sltu(AT, lhs_low, AT);
4745 __ Blt(TMP, AT, label);
4746 break;
4747 case kCondAE:
4748 __ LoadConst32(TMP, imm_high);
4749 __ Bltu(TMP, lhs_high, label);
4750 __ Sltu(TMP, lhs_high, TMP);
4751 __ LoadConst32(AT, imm_low);
4752 __ Sltu(AT, lhs_low, AT);
4753 __ Or(TMP, TMP, AT);
4754 __ Beqz(TMP, label);
4755 break;
4756 case kCondBE:
4757 __ LoadConst32(TMP, imm_high);
4758 __ Bltu(lhs_high, TMP, label);
4759 __ Sltu(TMP, TMP, lhs_high);
4760 __ LoadConst32(AT, imm_low);
4761 __ Sltu(AT, AT, lhs_low);
4762 __ Or(TMP, TMP, AT);
4763 __ Beqz(TMP, label);
4764 break;
4765 case kCondA:
4766 __ LoadConst32(TMP, imm_high);
4767 __ Bltu(TMP, lhs_high, label);
4768 __ Sltu(TMP, lhs_high, TMP);
4769 __ LoadConst32(AT, imm_low);
4770 __ Sltu(AT, AT, lhs_low);
4771 __ Blt(TMP, AT, label);
4772 break;
4773 }
4774 } else {
4775 switch (cond) {
4776 case kCondEQ:
4777 __ Xor(TMP, lhs_high, rhs_high);
4778 __ Xor(AT, lhs_low, rhs_low);
4779 __ Or(TMP, TMP, AT);
4780 __ Beqz(TMP, label);
4781 break;
4782 case kCondNE:
4783 __ Xor(TMP, lhs_high, rhs_high);
4784 __ Xor(AT, lhs_low, rhs_low);
4785 __ Or(TMP, TMP, AT);
4786 __ Bnez(TMP, label);
4787 break;
4788 case kCondLT:
4789 __ Blt(lhs_high, rhs_high, label);
4790 __ Slt(TMP, rhs_high, lhs_high);
4791 __ Sltu(AT, lhs_low, rhs_low);
4792 __ Blt(TMP, AT, label);
4793 break;
4794 case kCondGE:
4795 __ Blt(rhs_high, lhs_high, label);
4796 __ Slt(TMP, lhs_high, rhs_high);
4797 __ Sltu(AT, lhs_low, rhs_low);
4798 __ Or(TMP, TMP, AT);
4799 __ Beqz(TMP, label);
4800 break;
4801 case kCondLE:
4802 __ Blt(lhs_high, rhs_high, label);
4803 __ Slt(TMP, rhs_high, lhs_high);
4804 __ Sltu(AT, rhs_low, lhs_low);
4805 __ Or(TMP, TMP, AT);
4806 __ Beqz(TMP, label);
4807 break;
4808 case kCondGT:
4809 __ Blt(rhs_high, lhs_high, label);
4810 __ Slt(TMP, lhs_high, rhs_high);
4811 __ Sltu(AT, rhs_low, lhs_low);
4812 __ Blt(TMP, AT, label);
4813 break;
4814 case kCondB:
4815 __ Bltu(lhs_high, rhs_high, label);
4816 __ Sltu(TMP, rhs_high, lhs_high);
4817 __ Sltu(AT, lhs_low, rhs_low);
4818 __ Blt(TMP, AT, label);
4819 break;
4820 case kCondAE:
4821 __ Bltu(rhs_high, lhs_high, label);
4822 __ Sltu(TMP, lhs_high, rhs_high);
4823 __ Sltu(AT, lhs_low, rhs_low);
4824 __ Or(TMP, TMP, AT);
4825 __ Beqz(TMP, label);
4826 break;
4827 case kCondBE:
4828 __ Bltu(lhs_high, rhs_high, label);
4829 __ Sltu(TMP, rhs_high, lhs_high);
4830 __ Sltu(AT, rhs_low, lhs_low);
4831 __ Or(TMP, TMP, AT);
4832 __ Beqz(TMP, label);
4833 break;
4834 case kCondA:
4835 __ Bltu(rhs_high, lhs_high, label);
4836 __ Sltu(TMP, lhs_high, rhs_high);
4837 __ Sltu(AT, rhs_low, lhs_low);
4838 __ Blt(TMP, AT, label);
4839 break;
4840 }
4841 }
4842}
4843
Alexey Frunze2ddb7172016-09-06 17:04:55 -07004844void InstructionCodeGeneratorMIPS::GenerateFpCompare(IfCondition cond,
4845 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004846 DataType::Type type,
Alexey Frunze2ddb7172016-09-06 17:04:55 -07004847 LocationSummary* locations) {
4848 Register dst = locations->Out().AsRegister<Register>();
4849 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
4850 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
4851 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004852 if (type == DataType::Type::kFloat32) {
Alexey Frunze2ddb7172016-09-06 17:04:55 -07004853 if (isR6) {
4854 switch (cond) {
4855 case kCondEQ:
4856 __ CmpEqS(FTMP, lhs, rhs);
4857 __ Mfc1(dst, FTMP);
4858 __ Andi(dst, dst, 1);
4859 break;
4860 case kCondNE:
4861 __ CmpEqS(FTMP, lhs, rhs);
4862 __ Mfc1(dst, FTMP);
4863 __ Addiu(dst, dst, 1);
4864 break;
4865 case kCondLT:
4866 if (gt_bias) {
4867 __ CmpLtS(FTMP, lhs, rhs);
4868 } else {
4869 __ CmpUltS(FTMP, lhs, rhs);
4870 }
4871 __ Mfc1(dst, FTMP);
4872 __ Andi(dst, dst, 1);
4873 break;
4874 case kCondLE:
4875 if (gt_bias) {
4876 __ CmpLeS(FTMP, lhs, rhs);
4877 } else {
4878 __ CmpUleS(FTMP, lhs, rhs);
4879 }
4880 __ Mfc1(dst, FTMP);
4881 __ Andi(dst, dst, 1);
4882 break;
4883 case kCondGT:
4884 if (gt_bias) {
4885 __ CmpUltS(FTMP, rhs, lhs);
4886 } else {
4887 __ CmpLtS(FTMP, rhs, lhs);
4888 }
4889 __ Mfc1(dst, FTMP);
4890 __ Andi(dst, dst, 1);
4891 break;
4892 case kCondGE:
4893 if (gt_bias) {
4894 __ CmpUleS(FTMP, rhs, lhs);
4895 } else {
4896 __ CmpLeS(FTMP, rhs, lhs);
4897 }
4898 __ Mfc1(dst, FTMP);
4899 __ Andi(dst, dst, 1);
4900 break;
4901 default:
4902 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
4903 UNREACHABLE();
4904 }
4905 } else {
4906 switch (cond) {
4907 case kCondEQ:
4908 __ CeqS(0, lhs, rhs);
4909 __ LoadConst32(dst, 1);
4910 __ Movf(dst, ZERO, 0);
4911 break;
4912 case kCondNE:
4913 __ CeqS(0, lhs, rhs);
4914 __ LoadConst32(dst, 1);
4915 __ Movt(dst, ZERO, 0);
4916 break;
4917 case kCondLT:
4918 if (gt_bias) {
4919 __ ColtS(0, lhs, rhs);
4920 } else {
4921 __ CultS(0, lhs, rhs);
4922 }
4923 __ LoadConst32(dst, 1);
4924 __ Movf(dst, ZERO, 0);
4925 break;
4926 case kCondLE:
4927 if (gt_bias) {
4928 __ ColeS(0, lhs, rhs);
4929 } else {
4930 __ CuleS(0, lhs, rhs);
4931 }
4932 __ LoadConst32(dst, 1);
4933 __ Movf(dst, ZERO, 0);
4934 break;
4935 case kCondGT:
4936 if (gt_bias) {
4937 __ CultS(0, rhs, lhs);
4938 } else {
4939 __ ColtS(0, rhs, lhs);
4940 }
4941 __ LoadConst32(dst, 1);
4942 __ Movf(dst, ZERO, 0);
4943 break;
4944 case kCondGE:
4945 if (gt_bias) {
4946 __ CuleS(0, rhs, lhs);
4947 } else {
4948 __ ColeS(0, rhs, lhs);
4949 }
4950 __ LoadConst32(dst, 1);
4951 __ Movf(dst, ZERO, 0);
4952 break;
4953 default:
4954 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
4955 UNREACHABLE();
4956 }
4957 }
4958 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004959 DCHECK_EQ(type, DataType::Type::kFloat64);
Alexey Frunze2ddb7172016-09-06 17:04:55 -07004960 if (isR6) {
4961 switch (cond) {
4962 case kCondEQ:
4963 __ CmpEqD(FTMP, lhs, rhs);
4964 __ Mfc1(dst, FTMP);
4965 __ Andi(dst, dst, 1);
4966 break;
4967 case kCondNE:
4968 __ CmpEqD(FTMP, lhs, rhs);
4969 __ Mfc1(dst, FTMP);
4970 __ Addiu(dst, dst, 1);
4971 break;
4972 case kCondLT:
4973 if (gt_bias) {
4974 __ CmpLtD(FTMP, lhs, rhs);
4975 } else {
4976 __ CmpUltD(FTMP, lhs, rhs);
4977 }
4978 __ Mfc1(dst, FTMP);
4979 __ Andi(dst, dst, 1);
4980 break;
4981 case kCondLE:
4982 if (gt_bias) {
4983 __ CmpLeD(FTMP, lhs, rhs);
4984 } else {
4985 __ CmpUleD(FTMP, lhs, rhs);
4986 }
4987 __ Mfc1(dst, FTMP);
4988 __ Andi(dst, dst, 1);
4989 break;
4990 case kCondGT:
4991 if (gt_bias) {
4992 __ CmpUltD(FTMP, rhs, lhs);
4993 } else {
4994 __ CmpLtD(FTMP, rhs, lhs);
4995 }
4996 __ Mfc1(dst, FTMP);
4997 __ Andi(dst, dst, 1);
4998 break;
4999 case kCondGE:
5000 if (gt_bias) {
5001 __ CmpUleD(FTMP, rhs, lhs);
5002 } else {
5003 __ CmpLeD(FTMP, rhs, lhs);
5004 }
5005 __ Mfc1(dst, FTMP);
5006 __ Andi(dst, dst, 1);
5007 break;
5008 default:
5009 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
5010 UNREACHABLE();
5011 }
5012 } else {
5013 switch (cond) {
5014 case kCondEQ:
5015 __ CeqD(0, lhs, rhs);
5016 __ LoadConst32(dst, 1);
5017 __ Movf(dst, ZERO, 0);
5018 break;
5019 case kCondNE:
5020 __ CeqD(0, lhs, rhs);
5021 __ LoadConst32(dst, 1);
5022 __ Movt(dst, ZERO, 0);
5023 break;
5024 case kCondLT:
5025 if (gt_bias) {
5026 __ ColtD(0, lhs, rhs);
5027 } else {
5028 __ CultD(0, lhs, rhs);
5029 }
5030 __ LoadConst32(dst, 1);
5031 __ Movf(dst, ZERO, 0);
5032 break;
5033 case kCondLE:
5034 if (gt_bias) {
5035 __ ColeD(0, lhs, rhs);
5036 } else {
5037 __ CuleD(0, lhs, rhs);
5038 }
5039 __ LoadConst32(dst, 1);
5040 __ Movf(dst, ZERO, 0);
5041 break;
5042 case kCondGT:
5043 if (gt_bias) {
5044 __ CultD(0, rhs, lhs);
5045 } else {
5046 __ ColtD(0, rhs, lhs);
5047 }
5048 __ LoadConst32(dst, 1);
5049 __ Movf(dst, ZERO, 0);
5050 break;
5051 case kCondGE:
5052 if (gt_bias) {
5053 __ CuleD(0, rhs, lhs);
5054 } else {
5055 __ ColeD(0, rhs, lhs);
5056 }
5057 __ LoadConst32(dst, 1);
5058 __ Movf(dst, ZERO, 0);
5059 break;
5060 default:
5061 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
5062 UNREACHABLE();
5063 }
5064 }
5065 }
5066}
5067
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005068bool InstructionCodeGeneratorMIPS::MaterializeFpCompareR2(IfCondition cond,
5069 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005070 DataType::Type type,
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005071 LocationSummary* input_locations,
5072 int cc) {
5073 FRegister lhs = input_locations->InAt(0).AsFpuRegister<FRegister>();
5074 FRegister rhs = input_locations->InAt(1).AsFpuRegister<FRegister>();
5075 CHECK(!codegen_->GetInstructionSetFeatures().IsR6());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005076 if (type == DataType::Type::kFloat32) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005077 switch (cond) {
5078 case kCondEQ:
5079 __ CeqS(cc, lhs, rhs);
5080 return false;
5081 case kCondNE:
5082 __ CeqS(cc, lhs, rhs);
5083 return true;
5084 case kCondLT:
5085 if (gt_bias) {
5086 __ ColtS(cc, lhs, rhs);
5087 } else {
5088 __ CultS(cc, lhs, rhs);
5089 }
5090 return false;
5091 case kCondLE:
5092 if (gt_bias) {
5093 __ ColeS(cc, lhs, rhs);
5094 } else {
5095 __ CuleS(cc, lhs, rhs);
5096 }
5097 return false;
5098 case kCondGT:
5099 if (gt_bias) {
5100 __ CultS(cc, rhs, lhs);
5101 } else {
5102 __ ColtS(cc, rhs, lhs);
5103 }
5104 return false;
5105 case kCondGE:
5106 if (gt_bias) {
5107 __ CuleS(cc, rhs, lhs);
5108 } else {
5109 __ ColeS(cc, rhs, lhs);
5110 }
5111 return false;
5112 default:
5113 LOG(FATAL) << "Unexpected non-floating-point condition";
5114 UNREACHABLE();
5115 }
5116 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005117 DCHECK_EQ(type, DataType::Type::kFloat64);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005118 switch (cond) {
5119 case kCondEQ:
5120 __ CeqD(cc, lhs, rhs);
5121 return false;
5122 case kCondNE:
5123 __ CeqD(cc, lhs, rhs);
5124 return true;
5125 case kCondLT:
5126 if (gt_bias) {
5127 __ ColtD(cc, lhs, rhs);
5128 } else {
5129 __ CultD(cc, lhs, rhs);
5130 }
5131 return false;
5132 case kCondLE:
5133 if (gt_bias) {
5134 __ ColeD(cc, lhs, rhs);
5135 } else {
5136 __ CuleD(cc, lhs, rhs);
5137 }
5138 return false;
5139 case kCondGT:
5140 if (gt_bias) {
5141 __ CultD(cc, rhs, lhs);
5142 } else {
5143 __ ColtD(cc, rhs, lhs);
5144 }
5145 return false;
5146 case kCondGE:
5147 if (gt_bias) {
5148 __ CuleD(cc, rhs, lhs);
5149 } else {
5150 __ ColeD(cc, rhs, lhs);
5151 }
5152 return false;
5153 default:
5154 LOG(FATAL) << "Unexpected non-floating-point condition";
5155 UNREACHABLE();
5156 }
5157 }
5158}
5159
5160bool InstructionCodeGeneratorMIPS::MaterializeFpCompareR6(IfCondition cond,
5161 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005162 DataType::Type type,
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005163 LocationSummary* input_locations,
5164 FRegister dst) {
5165 FRegister lhs = input_locations->InAt(0).AsFpuRegister<FRegister>();
5166 FRegister rhs = input_locations->InAt(1).AsFpuRegister<FRegister>();
5167 CHECK(codegen_->GetInstructionSetFeatures().IsR6());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005168 if (type == DataType::Type::kFloat32) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005169 switch (cond) {
5170 case kCondEQ:
5171 __ CmpEqS(dst, lhs, rhs);
5172 return false;
5173 case kCondNE:
5174 __ CmpEqS(dst, lhs, rhs);
5175 return true;
5176 case kCondLT:
5177 if (gt_bias) {
5178 __ CmpLtS(dst, lhs, rhs);
5179 } else {
5180 __ CmpUltS(dst, lhs, rhs);
5181 }
5182 return false;
5183 case kCondLE:
5184 if (gt_bias) {
5185 __ CmpLeS(dst, lhs, rhs);
5186 } else {
5187 __ CmpUleS(dst, lhs, rhs);
5188 }
5189 return false;
5190 case kCondGT:
5191 if (gt_bias) {
5192 __ CmpUltS(dst, rhs, lhs);
5193 } else {
5194 __ CmpLtS(dst, rhs, lhs);
5195 }
5196 return false;
5197 case kCondGE:
5198 if (gt_bias) {
5199 __ CmpUleS(dst, rhs, lhs);
5200 } else {
5201 __ CmpLeS(dst, rhs, lhs);
5202 }
5203 return false;
5204 default:
5205 LOG(FATAL) << "Unexpected non-floating-point condition";
5206 UNREACHABLE();
5207 }
5208 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005209 DCHECK_EQ(type, DataType::Type::kFloat64);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005210 switch (cond) {
5211 case kCondEQ:
5212 __ CmpEqD(dst, lhs, rhs);
5213 return false;
5214 case kCondNE:
5215 __ CmpEqD(dst, lhs, rhs);
5216 return true;
5217 case kCondLT:
5218 if (gt_bias) {
5219 __ CmpLtD(dst, lhs, rhs);
5220 } else {
5221 __ CmpUltD(dst, lhs, rhs);
5222 }
5223 return false;
5224 case kCondLE:
5225 if (gt_bias) {
5226 __ CmpLeD(dst, lhs, rhs);
5227 } else {
5228 __ CmpUleD(dst, lhs, rhs);
5229 }
5230 return false;
5231 case kCondGT:
5232 if (gt_bias) {
5233 __ CmpUltD(dst, rhs, lhs);
5234 } else {
5235 __ CmpLtD(dst, rhs, lhs);
5236 }
5237 return false;
5238 case kCondGE:
5239 if (gt_bias) {
5240 __ CmpUleD(dst, rhs, lhs);
5241 } else {
5242 __ CmpLeD(dst, rhs, lhs);
5243 }
5244 return false;
5245 default:
5246 LOG(FATAL) << "Unexpected non-floating-point condition";
5247 UNREACHABLE();
5248 }
5249 }
5250}
5251
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005252void InstructionCodeGeneratorMIPS::GenerateFpCompareAndBranch(IfCondition cond,
5253 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005254 DataType::Type type,
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005255 LocationSummary* locations,
5256 MipsLabel* label) {
5257 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
5258 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
5259 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005260 if (type == DataType::Type::kFloat32) {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005261 if (isR6) {
5262 switch (cond) {
5263 case kCondEQ:
5264 __ CmpEqS(FTMP, lhs, rhs);
5265 __ Bc1nez(FTMP, label);
5266 break;
5267 case kCondNE:
5268 __ CmpEqS(FTMP, lhs, rhs);
5269 __ Bc1eqz(FTMP, label);
5270 break;
5271 case kCondLT:
5272 if (gt_bias) {
5273 __ CmpLtS(FTMP, lhs, rhs);
5274 } else {
5275 __ CmpUltS(FTMP, lhs, rhs);
5276 }
5277 __ Bc1nez(FTMP, label);
5278 break;
5279 case kCondLE:
5280 if (gt_bias) {
5281 __ CmpLeS(FTMP, lhs, rhs);
5282 } else {
5283 __ CmpUleS(FTMP, lhs, rhs);
5284 }
5285 __ Bc1nez(FTMP, label);
5286 break;
5287 case kCondGT:
5288 if (gt_bias) {
5289 __ CmpUltS(FTMP, rhs, lhs);
5290 } else {
5291 __ CmpLtS(FTMP, rhs, lhs);
5292 }
5293 __ Bc1nez(FTMP, label);
5294 break;
5295 case kCondGE:
5296 if (gt_bias) {
5297 __ CmpUleS(FTMP, rhs, lhs);
5298 } else {
5299 __ CmpLeS(FTMP, rhs, lhs);
5300 }
5301 __ Bc1nez(FTMP, label);
5302 break;
5303 default:
5304 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005305 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005306 }
5307 } else {
5308 switch (cond) {
5309 case kCondEQ:
5310 __ CeqS(0, lhs, rhs);
5311 __ Bc1t(0, label);
5312 break;
5313 case kCondNE:
5314 __ CeqS(0, lhs, rhs);
5315 __ Bc1f(0, label);
5316 break;
5317 case kCondLT:
5318 if (gt_bias) {
5319 __ ColtS(0, lhs, rhs);
5320 } else {
5321 __ CultS(0, lhs, rhs);
5322 }
5323 __ Bc1t(0, label);
5324 break;
5325 case kCondLE:
5326 if (gt_bias) {
5327 __ ColeS(0, lhs, rhs);
5328 } else {
5329 __ CuleS(0, lhs, rhs);
5330 }
5331 __ Bc1t(0, label);
5332 break;
5333 case kCondGT:
5334 if (gt_bias) {
5335 __ CultS(0, rhs, lhs);
5336 } else {
5337 __ ColtS(0, rhs, lhs);
5338 }
5339 __ Bc1t(0, label);
5340 break;
5341 case kCondGE:
5342 if (gt_bias) {
5343 __ CuleS(0, rhs, lhs);
5344 } else {
5345 __ ColeS(0, rhs, lhs);
5346 }
5347 __ Bc1t(0, label);
5348 break;
5349 default:
5350 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005351 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005352 }
5353 }
5354 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005355 DCHECK_EQ(type, DataType::Type::kFloat64);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005356 if (isR6) {
5357 switch (cond) {
5358 case kCondEQ:
5359 __ CmpEqD(FTMP, lhs, rhs);
5360 __ Bc1nez(FTMP, label);
5361 break;
5362 case kCondNE:
5363 __ CmpEqD(FTMP, lhs, rhs);
5364 __ Bc1eqz(FTMP, label);
5365 break;
5366 case kCondLT:
5367 if (gt_bias) {
5368 __ CmpLtD(FTMP, lhs, rhs);
5369 } else {
5370 __ CmpUltD(FTMP, lhs, rhs);
5371 }
5372 __ Bc1nez(FTMP, label);
5373 break;
5374 case kCondLE:
5375 if (gt_bias) {
5376 __ CmpLeD(FTMP, lhs, rhs);
5377 } else {
5378 __ CmpUleD(FTMP, lhs, rhs);
5379 }
5380 __ Bc1nez(FTMP, label);
5381 break;
5382 case kCondGT:
5383 if (gt_bias) {
5384 __ CmpUltD(FTMP, rhs, lhs);
5385 } else {
5386 __ CmpLtD(FTMP, rhs, lhs);
5387 }
5388 __ Bc1nez(FTMP, label);
5389 break;
5390 case kCondGE:
5391 if (gt_bias) {
5392 __ CmpUleD(FTMP, rhs, lhs);
5393 } else {
5394 __ CmpLeD(FTMP, rhs, lhs);
5395 }
5396 __ Bc1nez(FTMP, label);
5397 break;
5398 default:
5399 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005400 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005401 }
5402 } else {
5403 switch (cond) {
5404 case kCondEQ:
5405 __ CeqD(0, lhs, rhs);
5406 __ Bc1t(0, label);
5407 break;
5408 case kCondNE:
5409 __ CeqD(0, lhs, rhs);
5410 __ Bc1f(0, label);
5411 break;
5412 case kCondLT:
5413 if (gt_bias) {
5414 __ ColtD(0, lhs, rhs);
5415 } else {
5416 __ CultD(0, lhs, rhs);
5417 }
5418 __ Bc1t(0, label);
5419 break;
5420 case kCondLE:
5421 if (gt_bias) {
5422 __ ColeD(0, lhs, rhs);
5423 } else {
5424 __ CuleD(0, lhs, rhs);
5425 }
5426 __ Bc1t(0, label);
5427 break;
5428 case kCondGT:
5429 if (gt_bias) {
5430 __ CultD(0, rhs, lhs);
5431 } else {
5432 __ ColtD(0, rhs, lhs);
5433 }
5434 __ Bc1t(0, label);
5435 break;
5436 case kCondGE:
5437 if (gt_bias) {
5438 __ CuleD(0, rhs, lhs);
5439 } else {
5440 __ ColeD(0, rhs, lhs);
5441 }
5442 __ Bc1t(0, label);
5443 break;
5444 default:
5445 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005446 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005447 }
5448 }
5449 }
5450}
5451
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005452void InstructionCodeGeneratorMIPS::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00005453 size_t condition_input_index,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005454 MipsLabel* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00005455 MipsLabel* false_target) {
5456 HInstruction* cond = instruction->InputAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005457
David Brazdil0debae72015-11-12 18:37:00 +00005458 if (true_target == nullptr && false_target == nullptr) {
5459 // Nothing to do. The code always falls through.
5460 return;
5461 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00005462 // Constant condition, statically compared against "true" (integer value 1).
5463 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00005464 if (true_target != nullptr) {
5465 __ B(true_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005466 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005467 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00005468 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00005469 if (false_target != nullptr) {
5470 __ B(false_target);
5471 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005472 }
David Brazdil0debae72015-11-12 18:37:00 +00005473 return;
5474 }
5475
5476 // The following code generates these patterns:
5477 // (1) true_target == nullptr && false_target != nullptr
5478 // - opposite condition true => branch to false_target
5479 // (2) true_target != nullptr && false_target == nullptr
5480 // - condition true => branch to true_target
5481 // (3) true_target != nullptr && false_target != nullptr
5482 // - condition true => branch to true_target
5483 // - branch to false_target
5484 if (IsBooleanValueOrMaterializedCondition(cond)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005485 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00005486 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005487 DCHECK(cond_val.IsRegister());
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005488 if (true_target == nullptr) {
David Brazdil0debae72015-11-12 18:37:00 +00005489 __ Beqz(cond_val.AsRegister<Register>(), false_target);
5490 } else {
5491 __ Bnez(cond_val.AsRegister<Register>(), true_target);
5492 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005493 } else {
5494 // The condition instruction has not been materialized, use its inputs as
5495 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00005496 HCondition* condition = cond->AsCondition();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005497 DataType::Type type = condition->InputAt(0)->GetType();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005498 LocationSummary* locations = cond->GetLocations();
5499 IfCondition if_cond = condition->GetCondition();
5500 MipsLabel* branch_target = true_target;
David Brazdil0debae72015-11-12 18:37:00 +00005501
David Brazdil0debae72015-11-12 18:37:00 +00005502 if (true_target == nullptr) {
5503 if_cond = condition->GetOppositeCondition();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005504 branch_target = false_target;
David Brazdil0debae72015-11-12 18:37:00 +00005505 }
5506
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005507 switch (type) {
5508 default:
5509 GenerateIntCompareAndBranch(if_cond, locations, branch_target);
5510 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005511 case DataType::Type::kInt64:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005512 GenerateLongCompareAndBranch(if_cond, locations, branch_target);
5513 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005514 case DataType::Type::kFloat32:
5515 case DataType::Type::kFloat64:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005516 GenerateFpCompareAndBranch(if_cond, condition->IsGtBias(), type, locations, branch_target);
5517 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005518 }
5519 }
David Brazdil0debae72015-11-12 18:37:00 +00005520
5521 // If neither branch falls through (case 3), the conditional branch to `true_target`
5522 // was already emitted (case 2) and we need to emit a jump to `false_target`.
5523 if (true_target != nullptr && false_target != nullptr) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005524 __ B(false_target);
5525 }
5526}
5527
5528void LocationsBuilderMIPS::VisitIf(HIf* if_instr) {
5529 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00005530 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005531 locations->SetInAt(0, Location::RequiresRegister());
5532 }
5533}
5534
5535void InstructionCodeGeneratorMIPS::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00005536 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
5537 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
5538 MipsLabel* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
5539 nullptr : codegen_->GetLabelOf(true_successor);
5540 MipsLabel* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
5541 nullptr : codegen_->GetLabelOf(false_successor);
5542 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005543}
5544
5545void LocationsBuilderMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
5546 LocationSummary* locations = new (GetGraph()->GetArena())
5547 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01005548 InvokeRuntimeCallingConvention calling_convention;
5549 RegisterSet caller_saves = RegisterSet::Empty();
5550 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5551 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00005552 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005553 locations->SetInAt(0, Location::RequiresRegister());
5554 }
5555}
5556
5557void InstructionCodeGeneratorMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08005558 SlowPathCodeMIPS* slow_path =
5559 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathMIPS>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00005560 GenerateTestAndBranch(deoptimize,
5561 /* condition_input_index */ 0,
5562 slow_path->GetEntryLabel(),
5563 /* false_target */ nullptr);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005564}
5565
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005566// This function returns true if a conditional move can be generated for HSelect.
5567// Otherwise it returns false and HSelect must be implemented in terms of conditonal
5568// branches and regular moves.
5569//
5570// If `locations_to_set` isn't nullptr, its inputs and outputs are set for HSelect.
5571//
5572// While determining feasibility of a conditional move and setting inputs/outputs
5573// are two distinct tasks, this function does both because they share quite a bit
5574// of common logic.
5575static bool CanMoveConditionally(HSelect* select, bool is_r6, LocationSummary* locations_to_set) {
5576 bool materialized = IsBooleanValueOrMaterializedCondition(select->GetCondition());
5577 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
5578 HCondition* condition = cond->AsCondition();
5579
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005580 DataType::Type cond_type =
5581 materialized ? DataType::Type::kInt32 : condition->InputAt(0)->GetType();
5582 DataType::Type dst_type = select->GetType();
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005583
5584 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
5585 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
5586 bool is_true_value_zero_constant =
5587 (cst_true_value != nullptr && cst_true_value->IsZeroBitPattern());
5588 bool is_false_value_zero_constant =
5589 (cst_false_value != nullptr && cst_false_value->IsZeroBitPattern());
5590
5591 bool can_move_conditionally = false;
5592 bool use_const_for_false_in = false;
5593 bool use_const_for_true_in = false;
5594
5595 if (!cond->IsConstant()) {
5596 switch (cond_type) {
5597 default:
5598 switch (dst_type) {
5599 default:
5600 // Moving int on int condition.
5601 if (is_r6) {
5602 if (is_true_value_zero_constant) {
5603 // seleqz out_reg, false_reg, cond_reg
5604 can_move_conditionally = true;
5605 use_const_for_true_in = true;
5606 } else if (is_false_value_zero_constant) {
5607 // selnez out_reg, true_reg, cond_reg
5608 can_move_conditionally = true;
5609 use_const_for_false_in = true;
5610 } else if (materialized) {
5611 // Not materializing unmaterialized int conditions
5612 // to keep the instruction count low.
5613 // selnez AT, true_reg, cond_reg
5614 // seleqz TMP, false_reg, cond_reg
5615 // or out_reg, AT, TMP
5616 can_move_conditionally = true;
5617 }
5618 } else {
5619 // movn out_reg, true_reg/ZERO, cond_reg
5620 can_move_conditionally = true;
5621 use_const_for_true_in = is_true_value_zero_constant;
5622 }
5623 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005624 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005625 // Moving long on int condition.
5626 if (is_r6) {
5627 if (is_true_value_zero_constant) {
5628 // seleqz out_reg_lo, false_reg_lo, cond_reg
5629 // seleqz out_reg_hi, false_reg_hi, cond_reg
5630 can_move_conditionally = true;
5631 use_const_for_true_in = true;
5632 } else if (is_false_value_zero_constant) {
5633 // selnez out_reg_lo, true_reg_lo, cond_reg
5634 // selnez out_reg_hi, true_reg_hi, cond_reg
5635 can_move_conditionally = true;
5636 use_const_for_false_in = true;
5637 }
5638 // Other long conditional moves would generate 6+ instructions,
5639 // which is too many.
5640 } else {
5641 // movn out_reg_lo, true_reg_lo/ZERO, cond_reg
5642 // movn out_reg_hi, true_reg_hi/ZERO, cond_reg
5643 can_move_conditionally = true;
5644 use_const_for_true_in = is_true_value_zero_constant;
5645 }
5646 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005647 case DataType::Type::kFloat32:
5648 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005649 // Moving float/double on int condition.
5650 if (is_r6) {
5651 if (materialized) {
5652 // Not materializing unmaterialized int conditions
5653 // to keep the instruction count low.
5654 can_move_conditionally = true;
5655 if (is_true_value_zero_constant) {
5656 // sltu TMP, ZERO, cond_reg
5657 // mtc1 TMP, temp_cond_reg
5658 // seleqz.fmt out_reg, false_reg, temp_cond_reg
5659 use_const_for_true_in = true;
5660 } else if (is_false_value_zero_constant) {
5661 // sltu TMP, ZERO, cond_reg
5662 // mtc1 TMP, temp_cond_reg
5663 // selnez.fmt out_reg, true_reg, temp_cond_reg
5664 use_const_for_false_in = true;
5665 } else {
5666 // sltu TMP, ZERO, cond_reg
5667 // mtc1 TMP, temp_cond_reg
5668 // sel.fmt temp_cond_reg, false_reg, true_reg
5669 // mov.fmt out_reg, temp_cond_reg
5670 }
5671 }
5672 } else {
5673 // movn.fmt out_reg, true_reg, cond_reg
5674 can_move_conditionally = true;
5675 }
5676 break;
5677 }
5678 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005679 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005680 // We don't materialize long comparison now
5681 // and use conditional branches instead.
5682 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005683 case DataType::Type::kFloat32:
5684 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005685 switch (dst_type) {
5686 default:
5687 // Moving int on float/double condition.
5688 if (is_r6) {
5689 if (is_true_value_zero_constant) {
5690 // mfc1 TMP, temp_cond_reg
5691 // seleqz out_reg, false_reg, TMP
5692 can_move_conditionally = true;
5693 use_const_for_true_in = true;
5694 } else if (is_false_value_zero_constant) {
5695 // mfc1 TMP, temp_cond_reg
5696 // selnez out_reg, true_reg, TMP
5697 can_move_conditionally = true;
5698 use_const_for_false_in = true;
5699 } else {
5700 // mfc1 TMP, temp_cond_reg
5701 // selnez AT, true_reg, TMP
5702 // seleqz TMP, false_reg, TMP
5703 // or out_reg, AT, TMP
5704 can_move_conditionally = true;
5705 }
5706 } else {
5707 // movt out_reg, true_reg/ZERO, cc
5708 can_move_conditionally = true;
5709 use_const_for_true_in = is_true_value_zero_constant;
5710 }
5711 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005712 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005713 // Moving long on float/double condition.
5714 if (is_r6) {
5715 if (is_true_value_zero_constant) {
5716 // mfc1 TMP, temp_cond_reg
5717 // seleqz out_reg_lo, false_reg_lo, TMP
5718 // seleqz out_reg_hi, false_reg_hi, TMP
5719 can_move_conditionally = true;
5720 use_const_for_true_in = true;
5721 } else if (is_false_value_zero_constant) {
5722 // mfc1 TMP, temp_cond_reg
5723 // selnez out_reg_lo, true_reg_lo, TMP
5724 // selnez out_reg_hi, true_reg_hi, TMP
5725 can_move_conditionally = true;
5726 use_const_for_false_in = true;
5727 }
5728 // Other long conditional moves would generate 6+ instructions,
5729 // which is too many.
5730 } else {
5731 // movt out_reg_lo, true_reg_lo/ZERO, cc
5732 // movt out_reg_hi, true_reg_hi/ZERO, cc
5733 can_move_conditionally = true;
5734 use_const_for_true_in = is_true_value_zero_constant;
5735 }
5736 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005737 case DataType::Type::kFloat32:
5738 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005739 // Moving float/double on float/double condition.
5740 if (is_r6) {
5741 can_move_conditionally = true;
5742 if (is_true_value_zero_constant) {
5743 // seleqz.fmt out_reg, false_reg, temp_cond_reg
5744 use_const_for_true_in = true;
5745 } else if (is_false_value_zero_constant) {
5746 // selnez.fmt out_reg, true_reg, temp_cond_reg
5747 use_const_for_false_in = true;
5748 } else {
5749 // sel.fmt temp_cond_reg, false_reg, true_reg
5750 // mov.fmt out_reg, temp_cond_reg
5751 }
5752 } else {
5753 // movt.fmt out_reg, true_reg, cc
5754 can_move_conditionally = true;
5755 }
5756 break;
5757 }
5758 break;
5759 }
5760 }
5761
5762 if (can_move_conditionally) {
5763 DCHECK(!use_const_for_false_in || !use_const_for_true_in);
5764 } else {
5765 DCHECK(!use_const_for_false_in);
5766 DCHECK(!use_const_for_true_in);
5767 }
5768
5769 if (locations_to_set != nullptr) {
5770 if (use_const_for_false_in) {
5771 locations_to_set->SetInAt(0, Location::ConstantLocation(cst_false_value));
5772 } else {
5773 locations_to_set->SetInAt(0,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005774 DataType::IsFloatingPointType(dst_type)
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005775 ? Location::RequiresFpuRegister()
5776 : Location::RequiresRegister());
5777 }
5778 if (use_const_for_true_in) {
5779 locations_to_set->SetInAt(1, Location::ConstantLocation(cst_true_value));
5780 } else {
5781 locations_to_set->SetInAt(1,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005782 DataType::IsFloatingPointType(dst_type)
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005783 ? Location::RequiresFpuRegister()
5784 : Location::RequiresRegister());
5785 }
5786 if (materialized) {
5787 locations_to_set->SetInAt(2, Location::RequiresRegister());
5788 }
5789 // On R6 we don't require the output to be the same as the
5790 // first input for conditional moves unlike on R2.
5791 bool is_out_same_as_first_in = !can_move_conditionally || !is_r6;
5792 if (is_out_same_as_first_in) {
5793 locations_to_set->SetOut(Location::SameAsFirstInput());
5794 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005795 locations_to_set->SetOut(DataType::IsFloatingPointType(dst_type)
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005796 ? Location::RequiresFpuRegister()
5797 : Location::RequiresRegister());
5798 }
5799 }
5800
5801 return can_move_conditionally;
5802}
5803
5804void InstructionCodeGeneratorMIPS::GenConditionalMoveR2(HSelect* select) {
5805 LocationSummary* locations = select->GetLocations();
5806 Location dst = locations->Out();
5807 Location src = locations->InAt(1);
5808 Register src_reg = ZERO;
5809 Register src_reg_high = ZERO;
5810 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
5811 Register cond_reg = TMP;
5812 int cond_cc = 0;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005813 DataType::Type cond_type = DataType::Type::kInt32;
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005814 bool cond_inverted = false;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005815 DataType::Type dst_type = select->GetType();
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005816
5817 if (IsBooleanValueOrMaterializedCondition(cond)) {
5818 cond_reg = locations->InAt(/* condition_input_index */ 2).AsRegister<Register>();
5819 } else {
5820 HCondition* condition = cond->AsCondition();
5821 LocationSummary* cond_locations = cond->GetLocations();
5822 IfCondition if_cond = condition->GetCondition();
5823 cond_type = condition->InputAt(0)->GetType();
5824 switch (cond_type) {
5825 default:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005826 DCHECK_NE(cond_type, DataType::Type::kInt64);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005827 cond_inverted = MaterializeIntCompare(if_cond, cond_locations, cond_reg);
5828 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005829 case DataType::Type::kFloat32:
5830 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005831 cond_inverted = MaterializeFpCompareR2(if_cond,
5832 condition->IsGtBias(),
5833 cond_type,
5834 cond_locations,
5835 cond_cc);
5836 break;
5837 }
5838 }
5839
5840 DCHECK(dst.Equals(locations->InAt(0)));
5841 if (src.IsRegister()) {
5842 src_reg = src.AsRegister<Register>();
5843 } else if (src.IsRegisterPair()) {
5844 src_reg = src.AsRegisterPairLow<Register>();
5845 src_reg_high = src.AsRegisterPairHigh<Register>();
5846 } else if (src.IsConstant()) {
5847 DCHECK(src.GetConstant()->IsZeroBitPattern());
5848 }
5849
5850 switch (cond_type) {
5851 default:
5852 switch (dst_type) {
5853 default:
5854 if (cond_inverted) {
5855 __ Movz(dst.AsRegister<Register>(), src_reg, cond_reg);
5856 } else {
5857 __ Movn(dst.AsRegister<Register>(), src_reg, cond_reg);
5858 }
5859 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005860 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005861 if (cond_inverted) {
5862 __ Movz(dst.AsRegisterPairLow<Register>(), src_reg, cond_reg);
5863 __ Movz(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_reg);
5864 } else {
5865 __ Movn(dst.AsRegisterPairLow<Register>(), src_reg, cond_reg);
5866 __ Movn(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_reg);
5867 }
5868 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005869 case DataType::Type::kFloat32:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005870 if (cond_inverted) {
5871 __ MovzS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
5872 } else {
5873 __ MovnS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
5874 }
5875 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005876 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005877 if (cond_inverted) {
5878 __ MovzD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
5879 } else {
5880 __ MovnD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
5881 }
5882 break;
5883 }
5884 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005885 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005886 LOG(FATAL) << "Unreachable";
5887 UNREACHABLE();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005888 case DataType::Type::kFloat32:
5889 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005890 switch (dst_type) {
5891 default:
5892 if (cond_inverted) {
5893 __ Movf(dst.AsRegister<Register>(), src_reg, cond_cc);
5894 } else {
5895 __ Movt(dst.AsRegister<Register>(), src_reg, cond_cc);
5896 }
5897 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005898 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005899 if (cond_inverted) {
5900 __ Movf(dst.AsRegisterPairLow<Register>(), src_reg, cond_cc);
5901 __ Movf(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_cc);
5902 } else {
5903 __ Movt(dst.AsRegisterPairLow<Register>(), src_reg, cond_cc);
5904 __ Movt(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_cc);
5905 }
5906 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005907 case DataType::Type::kFloat32:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005908 if (cond_inverted) {
5909 __ MovfS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
5910 } else {
5911 __ MovtS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
5912 }
5913 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005914 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005915 if (cond_inverted) {
5916 __ MovfD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
5917 } else {
5918 __ MovtD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
5919 }
5920 break;
5921 }
5922 break;
5923 }
5924}
5925
5926void InstructionCodeGeneratorMIPS::GenConditionalMoveR6(HSelect* select) {
5927 LocationSummary* locations = select->GetLocations();
5928 Location dst = locations->Out();
5929 Location false_src = locations->InAt(0);
5930 Location true_src = locations->InAt(1);
5931 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
5932 Register cond_reg = TMP;
5933 FRegister fcond_reg = FTMP;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005934 DataType::Type cond_type = DataType::Type::kInt32;
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005935 bool cond_inverted = false;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005936 DataType::Type dst_type = select->GetType();
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005937
5938 if (IsBooleanValueOrMaterializedCondition(cond)) {
5939 cond_reg = locations->InAt(/* condition_input_index */ 2).AsRegister<Register>();
5940 } else {
5941 HCondition* condition = cond->AsCondition();
5942 LocationSummary* cond_locations = cond->GetLocations();
5943 IfCondition if_cond = condition->GetCondition();
5944 cond_type = condition->InputAt(0)->GetType();
5945 switch (cond_type) {
5946 default:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005947 DCHECK_NE(cond_type, DataType::Type::kInt64);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005948 cond_inverted = MaterializeIntCompare(if_cond, cond_locations, cond_reg);
5949 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005950 case DataType::Type::kFloat32:
5951 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005952 cond_inverted = MaterializeFpCompareR6(if_cond,
5953 condition->IsGtBias(),
5954 cond_type,
5955 cond_locations,
5956 fcond_reg);
5957 break;
5958 }
5959 }
5960
5961 if (true_src.IsConstant()) {
5962 DCHECK(true_src.GetConstant()->IsZeroBitPattern());
5963 }
5964 if (false_src.IsConstant()) {
5965 DCHECK(false_src.GetConstant()->IsZeroBitPattern());
5966 }
5967
5968 switch (dst_type) {
5969 default:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005970 if (DataType::IsFloatingPointType(cond_type)) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005971 __ Mfc1(cond_reg, fcond_reg);
5972 }
5973 if (true_src.IsConstant()) {
5974 if (cond_inverted) {
5975 __ Selnez(dst.AsRegister<Register>(), false_src.AsRegister<Register>(), cond_reg);
5976 } else {
5977 __ Seleqz(dst.AsRegister<Register>(), false_src.AsRegister<Register>(), cond_reg);
5978 }
5979 } else if (false_src.IsConstant()) {
5980 if (cond_inverted) {
5981 __ Seleqz(dst.AsRegister<Register>(), true_src.AsRegister<Register>(), cond_reg);
5982 } else {
5983 __ Selnez(dst.AsRegister<Register>(), true_src.AsRegister<Register>(), cond_reg);
5984 }
5985 } else {
5986 DCHECK_NE(cond_reg, AT);
5987 if (cond_inverted) {
5988 __ Seleqz(AT, true_src.AsRegister<Register>(), cond_reg);
5989 __ Selnez(TMP, false_src.AsRegister<Register>(), cond_reg);
5990 } else {
5991 __ Selnez(AT, true_src.AsRegister<Register>(), cond_reg);
5992 __ Seleqz(TMP, false_src.AsRegister<Register>(), cond_reg);
5993 }
5994 __ Or(dst.AsRegister<Register>(), AT, TMP);
5995 }
5996 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005997 case DataType::Type::kInt64: {
5998 if (DataType::IsFloatingPointType(cond_type)) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005999 __ Mfc1(cond_reg, fcond_reg);
6000 }
6001 Register dst_lo = dst.AsRegisterPairLow<Register>();
6002 Register dst_hi = dst.AsRegisterPairHigh<Register>();
6003 if (true_src.IsConstant()) {
6004 Register src_lo = false_src.AsRegisterPairLow<Register>();
6005 Register src_hi = false_src.AsRegisterPairHigh<Register>();
6006 if (cond_inverted) {
6007 __ Selnez(dst_lo, src_lo, cond_reg);
6008 __ Selnez(dst_hi, src_hi, cond_reg);
6009 } else {
6010 __ Seleqz(dst_lo, src_lo, cond_reg);
6011 __ Seleqz(dst_hi, src_hi, cond_reg);
6012 }
6013 } else {
6014 DCHECK(false_src.IsConstant());
6015 Register src_lo = true_src.AsRegisterPairLow<Register>();
6016 Register src_hi = true_src.AsRegisterPairHigh<Register>();
6017 if (cond_inverted) {
6018 __ Seleqz(dst_lo, src_lo, cond_reg);
6019 __ Seleqz(dst_hi, src_hi, cond_reg);
6020 } else {
6021 __ Selnez(dst_lo, src_lo, cond_reg);
6022 __ Selnez(dst_hi, src_hi, cond_reg);
6023 }
6024 }
6025 break;
6026 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006027 case DataType::Type::kFloat32: {
6028 if (!DataType::IsFloatingPointType(cond_type)) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006029 // sel*.fmt tests bit 0 of the condition register, account for that.
6030 __ Sltu(TMP, ZERO, cond_reg);
6031 __ Mtc1(TMP, fcond_reg);
6032 }
6033 FRegister dst_reg = dst.AsFpuRegister<FRegister>();
6034 if (true_src.IsConstant()) {
6035 FRegister src_reg = false_src.AsFpuRegister<FRegister>();
6036 if (cond_inverted) {
6037 __ SelnezS(dst_reg, src_reg, fcond_reg);
6038 } else {
6039 __ SeleqzS(dst_reg, src_reg, fcond_reg);
6040 }
6041 } else if (false_src.IsConstant()) {
6042 FRegister src_reg = true_src.AsFpuRegister<FRegister>();
6043 if (cond_inverted) {
6044 __ SeleqzS(dst_reg, src_reg, fcond_reg);
6045 } else {
6046 __ SelnezS(dst_reg, src_reg, fcond_reg);
6047 }
6048 } else {
6049 if (cond_inverted) {
6050 __ SelS(fcond_reg,
6051 true_src.AsFpuRegister<FRegister>(),
6052 false_src.AsFpuRegister<FRegister>());
6053 } else {
6054 __ SelS(fcond_reg,
6055 false_src.AsFpuRegister<FRegister>(),
6056 true_src.AsFpuRegister<FRegister>());
6057 }
6058 __ MovS(dst_reg, fcond_reg);
6059 }
6060 break;
6061 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006062 case DataType::Type::kFloat64: {
6063 if (!DataType::IsFloatingPointType(cond_type)) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006064 // sel*.fmt tests bit 0 of the condition register, account for that.
6065 __ Sltu(TMP, ZERO, cond_reg);
6066 __ Mtc1(TMP, fcond_reg);
6067 }
6068 FRegister dst_reg = dst.AsFpuRegister<FRegister>();
6069 if (true_src.IsConstant()) {
6070 FRegister src_reg = false_src.AsFpuRegister<FRegister>();
6071 if (cond_inverted) {
6072 __ SelnezD(dst_reg, src_reg, fcond_reg);
6073 } else {
6074 __ SeleqzD(dst_reg, src_reg, fcond_reg);
6075 }
6076 } else if (false_src.IsConstant()) {
6077 FRegister src_reg = true_src.AsFpuRegister<FRegister>();
6078 if (cond_inverted) {
6079 __ SeleqzD(dst_reg, src_reg, fcond_reg);
6080 } else {
6081 __ SelnezD(dst_reg, src_reg, fcond_reg);
6082 }
6083 } else {
6084 if (cond_inverted) {
6085 __ SelD(fcond_reg,
6086 true_src.AsFpuRegister<FRegister>(),
6087 false_src.AsFpuRegister<FRegister>());
6088 } else {
6089 __ SelD(fcond_reg,
6090 false_src.AsFpuRegister<FRegister>(),
6091 true_src.AsFpuRegister<FRegister>());
6092 }
6093 __ MovD(dst_reg, fcond_reg);
6094 }
6095 break;
6096 }
6097 }
6098}
6099
Goran Jakovljevicc6418422016-12-05 16:31:55 +01006100void LocationsBuilderMIPS::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
6101 LocationSummary* locations = new (GetGraph()->GetArena())
6102 LocationSummary(flag, LocationSummary::kNoCall);
6103 locations->SetOut(Location::RequiresRegister());
Mingyao Yang063fc772016-08-02 11:02:54 -07006104}
6105
Goran Jakovljevicc6418422016-12-05 16:31:55 +01006106void InstructionCodeGeneratorMIPS::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
6107 __ LoadFromOffset(kLoadWord,
6108 flag->GetLocations()->Out().AsRegister<Register>(),
6109 SP,
6110 codegen_->GetStackOffsetOfShouldDeoptimizeFlag());
Mingyao Yang063fc772016-08-02 11:02:54 -07006111}
6112
David Brazdil74eb1b22015-12-14 11:44:01 +00006113void LocationsBuilderMIPS::VisitSelect(HSelect* select) {
6114 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006115 CanMoveConditionally(select, codegen_->GetInstructionSetFeatures().IsR6(), locations);
David Brazdil74eb1b22015-12-14 11:44:01 +00006116}
6117
6118void InstructionCodeGeneratorMIPS::VisitSelect(HSelect* select) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006119 bool is_r6 = codegen_->GetInstructionSetFeatures().IsR6();
6120 if (CanMoveConditionally(select, is_r6, /* locations_to_set */ nullptr)) {
6121 if (is_r6) {
6122 GenConditionalMoveR6(select);
6123 } else {
6124 GenConditionalMoveR2(select);
6125 }
6126 } else {
6127 LocationSummary* locations = select->GetLocations();
6128 MipsLabel false_target;
6129 GenerateTestAndBranch(select,
6130 /* condition_input_index */ 2,
6131 /* true_target */ nullptr,
6132 &false_target);
6133 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
6134 __ Bind(&false_target);
6135 }
David Brazdil74eb1b22015-12-14 11:44:01 +00006136}
6137
David Srbecky0cf44932015-12-09 14:09:59 +00006138void LocationsBuilderMIPS::VisitNativeDebugInfo(HNativeDebugInfo* info) {
6139 new (GetGraph()->GetArena()) LocationSummary(info);
6140}
6141
David Srbeckyd28f4a02016-03-14 17:14:24 +00006142void InstructionCodeGeneratorMIPS::VisitNativeDebugInfo(HNativeDebugInfo*) {
6143 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00006144}
6145
6146void CodeGeneratorMIPS::GenerateNop() {
6147 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00006148}
6149
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006150void LocationsBuilderMIPS::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006151 DataType::Type field_type = field_info.GetFieldType();
6152 bool is_wide = (field_type == DataType::Type::kInt64) || (field_type == DataType::Type::kFloat64);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006153 bool generate_volatile = field_info.IsVolatile() && is_wide;
Alexey Frunze15958152017-02-09 19:08:30 -08006154 bool object_field_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006155 kEmitCompilerReadBarrier && (field_type == DataType::Type::kReference);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006156 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Alexey Frunze15958152017-02-09 19:08:30 -08006157 instruction,
6158 generate_volatile
6159 ? LocationSummary::kCallOnMainOnly
6160 : (object_field_get_with_read_barrier
6161 ? LocationSummary::kCallOnSlowPath
6162 : LocationSummary::kNoCall));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006163
Alexey Frunzec61c0762017-04-10 13:54:23 -07006164 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
6165 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
6166 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006167 locations->SetInAt(0, Location::RequiresRegister());
6168 if (generate_volatile) {
6169 InvokeRuntimeCallingConvention calling_convention;
6170 // need A0 to hold base + offset
6171 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006172 if (field_type == DataType::Type::kInt64) {
6173 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kInt64));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006174 } else {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006175 // Use Location::Any() to prevent situations when running out of available fp registers.
6176 locations->SetOut(Location::Any());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006177 // Need some temp core regs since FP results are returned in core registers
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006178 Location reg = calling_convention.GetReturnLocation(DataType::Type::kInt64);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006179 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairLow<Register>()));
6180 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairHigh<Register>()));
6181 }
6182 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006183 if (DataType::IsFloatingPointType(instruction->GetType())) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006184 locations->SetOut(Location::RequiresFpuRegister());
6185 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006186 // The output overlaps in the case of an object field get with
6187 // read barriers enabled: we do not want the move to overwrite the
6188 // object's location, as we need it to emit the read barrier.
6189 locations->SetOut(Location::RequiresRegister(),
6190 object_field_get_with_read_barrier
6191 ? Location::kOutputOverlap
6192 : Location::kNoOutputOverlap);
6193 }
6194 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
6195 // We need a temporary register for the read barrier marking slow
6196 // path in CodeGeneratorMIPS::GenerateFieldLoadWithBakerReadBarrier.
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006197 if (!kBakerReadBarrierThunksEnableForFields) {
6198 locations->AddTemp(Location::RequiresRegister());
6199 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006200 }
6201 }
6202}
6203
6204void InstructionCodeGeneratorMIPS::HandleFieldGet(HInstruction* instruction,
6205 const FieldInfo& field_info,
6206 uint32_t dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006207 DataType::Type type = field_info.GetFieldType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006208 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08006209 Location obj_loc = locations->InAt(0);
6210 Register obj = obj_loc.AsRegister<Register>();
6211 Location dst_loc = locations->Out();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006212 LoadOperandType load_type = kLoadUnsignedByte;
6213 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006214 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Tijana Jakovljevic57433862017-01-17 16:59:03 +01006215 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006216
6217 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006218 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006219 case DataType::Type::kUint8:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006220 load_type = kLoadUnsignedByte;
6221 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006222 case DataType::Type::kInt8:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006223 load_type = kLoadSignedByte;
6224 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006225 case DataType::Type::kUint16:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006226 load_type = kLoadUnsignedHalfword;
6227 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006228 case DataType::Type::kInt16:
6229 load_type = kLoadSignedHalfword;
6230 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006231 case DataType::Type::kInt32:
6232 case DataType::Type::kFloat32:
6233 case DataType::Type::kReference:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006234 load_type = kLoadWord;
6235 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006236 case DataType::Type::kInt64:
6237 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006238 load_type = kLoadDoubleword;
6239 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006240 case DataType::Type::kVoid:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006241 LOG(FATAL) << "Unreachable type " << type;
6242 UNREACHABLE();
6243 }
6244
6245 if (is_volatile && load_type == kLoadDoubleword) {
6246 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006247 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006248 // Do implicit Null check
6249 __ Lw(ZERO, locations->GetTemp(0).AsRegister<Register>(), 0);
6250 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Serban Constantinescufca16662016-07-14 09:21:59 +01006251 codegen_->InvokeRuntime(kQuickA64Load, instruction, dex_pc);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006252 CheckEntrypointTypes<kQuickA64Load, int64_t, volatile const int64_t*>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006253 if (type == DataType::Type::kFloat64) {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006254 // FP results are returned in core registers. Need to move them.
Alexey Frunze15958152017-02-09 19:08:30 -08006255 if (dst_loc.IsFpuRegister()) {
6256 __ Mtc1(locations->GetTemp(1).AsRegister<Register>(), dst_loc.AsFpuRegister<FRegister>());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006257 __ MoveToFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
Alexey Frunze15958152017-02-09 19:08:30 -08006258 dst_loc.AsFpuRegister<FRegister>());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006259 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006260 DCHECK(dst_loc.IsDoubleStackSlot());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006261 __ StoreToOffset(kStoreWord,
6262 locations->GetTemp(1).AsRegister<Register>(),
6263 SP,
Alexey Frunze15958152017-02-09 19:08:30 -08006264 dst_loc.GetStackIndex());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006265 __ StoreToOffset(kStoreWord,
6266 locations->GetTemp(2).AsRegister<Register>(),
6267 SP,
Alexey Frunze15958152017-02-09 19:08:30 -08006268 dst_loc.GetStackIndex() + 4);
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006269 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006270 }
6271 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006272 if (type == DataType::Type::kReference) {
Alexey Frunze15958152017-02-09 19:08:30 -08006273 // /* HeapReference<Object> */ dst = *(obj + offset)
6274 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006275 Location temp_loc =
6276 kBakerReadBarrierThunksEnableForFields ? Location::NoLocation() : locations->GetTemp(0);
Alexey Frunze15958152017-02-09 19:08:30 -08006277 // Note that a potential implicit null check is handled in this
6278 // CodeGeneratorMIPS::GenerateFieldLoadWithBakerReadBarrier call.
6279 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6280 dst_loc,
6281 obj,
6282 offset,
6283 temp_loc,
6284 /* needs_null_check */ true);
6285 if (is_volatile) {
6286 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6287 }
6288 } else {
6289 __ LoadFromOffset(kLoadWord, dst_loc.AsRegister<Register>(), obj, offset, null_checker);
6290 if (is_volatile) {
6291 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6292 }
6293 // If read barriers are enabled, emit read barriers other than
6294 // Baker's using a slow path (and also unpoison the loaded
6295 // reference, if heap poisoning is enabled).
6296 codegen_->MaybeGenerateReadBarrierSlow(instruction, dst_loc, dst_loc, obj_loc, offset);
6297 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006298 } else if (!DataType::IsFloatingPointType(type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006299 Register dst;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006300 if (type == DataType::Type::kInt64) {
Alexey Frunze15958152017-02-09 19:08:30 -08006301 DCHECK(dst_loc.IsRegisterPair());
6302 dst = dst_loc.AsRegisterPairLow<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006303 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006304 DCHECK(dst_loc.IsRegister());
6305 dst = dst_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006306 }
Alexey Frunze2923db72016-08-20 01:55:47 -07006307 __ LoadFromOffset(load_type, dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006308 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006309 DCHECK(dst_loc.IsFpuRegister());
6310 FRegister dst = dst_loc.AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006311 if (type == DataType::Type::kFloat32) {
Alexey Frunze2923db72016-08-20 01:55:47 -07006312 __ LoadSFromOffset(dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006313 } else {
Alexey Frunze2923db72016-08-20 01:55:47 -07006314 __ LoadDFromOffset(dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006315 }
6316 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006317 }
6318
Alexey Frunze15958152017-02-09 19:08:30 -08006319 // Memory barriers, in the case of references, are handled in the
6320 // previous switch statement.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006321 if (is_volatile && (type != DataType::Type::kReference)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006322 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6323 }
6324}
6325
6326void LocationsBuilderMIPS::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006327 DataType::Type field_type = field_info.GetFieldType();
6328 bool is_wide = (field_type == DataType::Type::kInt64) || (field_type == DataType::Type::kFloat64);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006329 bool generate_volatile = field_info.IsVolatile() && is_wide;
6330 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006331 instruction, generate_volatile ? LocationSummary::kCallOnMainOnly : LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006332
6333 locations->SetInAt(0, Location::RequiresRegister());
6334 if (generate_volatile) {
6335 InvokeRuntimeCallingConvention calling_convention;
6336 // need A0 to hold base + offset
6337 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006338 if (field_type == DataType::Type::kInt64) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006339 locations->SetInAt(1, Location::RegisterPairLocation(
6340 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
6341 } else {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006342 // Use Location::Any() to prevent situations when running out of available fp registers.
6343 locations->SetInAt(1, Location::Any());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006344 // Pass FP parameters in core registers.
6345 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
6346 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(3)));
6347 }
6348 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006349 if (DataType::IsFloatingPointType(field_type)) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006350 locations->SetInAt(1, FpuRegisterOrConstantForStore(instruction->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006351 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006352 locations->SetInAt(1, RegisterOrZeroConstant(instruction->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006353 }
6354 }
6355}
6356
6357void InstructionCodeGeneratorMIPS::HandleFieldSet(HInstruction* instruction,
6358 const FieldInfo& field_info,
Goran Jakovljevice114da22016-12-26 14:21:43 +01006359 uint32_t dex_pc,
6360 bool value_can_be_null) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006361 DataType::Type type = field_info.GetFieldType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006362 LocationSummary* locations = instruction->GetLocations();
6363 Register obj = locations->InAt(0).AsRegister<Register>();
Alexey Frunzef58b2482016-09-02 22:14:06 -07006364 Location value_location = locations->InAt(1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006365 StoreOperandType store_type = kStoreByte;
6366 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006367 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Alexey Frunzec061de12017-02-14 13:27:23 -08006368 bool needs_write_barrier = CodeGenerator::StoreNeedsWriteBarrier(type, instruction->InputAt(1));
Tijana Jakovljevic57433862017-01-17 16:59:03 +01006369 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006370
6371 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006372 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006373 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006374 case DataType::Type::kInt8:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006375 store_type = kStoreByte;
6376 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006377 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006378 case DataType::Type::kInt16:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006379 store_type = kStoreHalfword;
6380 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006381 case DataType::Type::kInt32:
6382 case DataType::Type::kFloat32:
6383 case DataType::Type::kReference:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006384 store_type = kStoreWord;
6385 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006386 case DataType::Type::kInt64:
6387 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006388 store_type = kStoreDoubleword;
6389 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006390 case DataType::Type::kVoid:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006391 LOG(FATAL) << "Unreachable type " << type;
6392 UNREACHABLE();
6393 }
6394
6395 if (is_volatile) {
6396 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
6397 }
6398
6399 if (is_volatile && store_type == kStoreDoubleword) {
6400 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006401 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006402 // Do implicit Null check.
6403 __ Lw(ZERO, locations->GetTemp(0).AsRegister<Register>(), 0);
6404 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006405 if (type == DataType::Type::kFloat64) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006406 // Pass FP parameters in core registers.
Alexey Frunzef58b2482016-09-02 22:14:06 -07006407 if (value_location.IsFpuRegister()) {
6408 __ Mfc1(locations->GetTemp(1).AsRegister<Register>(),
6409 value_location.AsFpuRegister<FRegister>());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006410 __ MoveFromFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
Alexey Frunzef58b2482016-09-02 22:14:06 -07006411 value_location.AsFpuRegister<FRegister>());
6412 } else if (value_location.IsDoubleStackSlot()) {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006413 __ LoadFromOffset(kLoadWord,
6414 locations->GetTemp(1).AsRegister<Register>(),
6415 SP,
Alexey Frunzef58b2482016-09-02 22:14:06 -07006416 value_location.GetStackIndex());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006417 __ LoadFromOffset(kLoadWord,
6418 locations->GetTemp(2).AsRegister<Register>(),
6419 SP,
Alexey Frunzef58b2482016-09-02 22:14:06 -07006420 value_location.GetStackIndex() + 4);
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006421 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006422 DCHECK(value_location.IsConstant());
6423 DCHECK(value_location.GetConstant()->IsDoubleConstant());
6424 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006425 __ LoadConst64(locations->GetTemp(2).AsRegister<Register>(),
6426 locations->GetTemp(1).AsRegister<Register>(),
6427 value);
6428 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006429 }
Serban Constantinescufca16662016-07-14 09:21:59 +01006430 codegen_->InvokeRuntime(kQuickA64Store, instruction, dex_pc);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006431 CheckEntrypointTypes<kQuickA64Store, void, volatile int64_t *, int64_t>();
6432 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006433 if (value_location.IsConstant()) {
6434 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
6435 __ StoreConstToOffset(store_type, value, obj, offset, TMP, null_checker);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006436 } else if (!DataType::IsFloatingPointType(type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006437 Register src;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006438 if (type == DataType::Type::kInt64) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006439 src = value_location.AsRegisterPairLow<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006440 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006441 src = value_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006442 }
Alexey Frunzec061de12017-02-14 13:27:23 -08006443 if (kPoisonHeapReferences && needs_write_barrier) {
6444 // Note that in the case where `value` is a null reference,
6445 // we do not enter this block, as a null reference does not
6446 // need poisoning.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006447 DCHECK_EQ(type, DataType::Type::kReference);
Alexey Frunzec061de12017-02-14 13:27:23 -08006448 __ PoisonHeapReference(TMP, src);
6449 __ StoreToOffset(store_type, TMP, obj, offset, null_checker);
6450 } else {
6451 __ StoreToOffset(store_type, src, obj, offset, null_checker);
6452 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006453 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006454 FRegister src = value_location.AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006455 if (type == DataType::Type::kFloat32) {
Alexey Frunze2923db72016-08-20 01:55:47 -07006456 __ StoreSToOffset(src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006457 } else {
Alexey Frunze2923db72016-08-20 01:55:47 -07006458 __ StoreDToOffset(src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006459 }
6460 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006461 }
6462
Alexey Frunzec061de12017-02-14 13:27:23 -08006463 if (needs_write_barrier) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006464 Register src = value_location.AsRegister<Register>();
Goran Jakovljevice114da22016-12-26 14:21:43 +01006465 codegen_->MarkGCCard(obj, src, value_can_be_null);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006466 }
6467
6468 if (is_volatile) {
6469 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
6470 }
6471}
6472
6473void LocationsBuilderMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
6474 HandleFieldGet(instruction, instruction->GetFieldInfo());
6475}
6476
6477void InstructionCodeGeneratorMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
6478 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
6479}
6480
6481void LocationsBuilderMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
6482 HandleFieldSet(instruction, instruction->GetFieldInfo());
6483}
6484
6485void InstructionCodeGeneratorMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Goran Jakovljevice114da22016-12-26 14:21:43 +01006486 HandleFieldSet(instruction,
6487 instruction->GetFieldInfo(),
6488 instruction->GetDexPc(),
6489 instruction->GetValueCanBeNull());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006490}
6491
Alexey Frunze15958152017-02-09 19:08:30 -08006492void InstructionCodeGeneratorMIPS::GenerateReferenceLoadOneRegister(
6493 HInstruction* instruction,
6494 Location out,
6495 uint32_t offset,
6496 Location maybe_temp,
6497 ReadBarrierOption read_barrier_option) {
6498 Register out_reg = out.AsRegister<Register>();
6499 if (read_barrier_option == kWithReadBarrier) {
6500 CHECK(kEmitCompilerReadBarrier);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006501 if (!kUseBakerReadBarrier || !kBakerReadBarrierThunksEnableForFields) {
6502 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
6503 }
Alexey Frunze15958152017-02-09 19:08:30 -08006504 if (kUseBakerReadBarrier) {
6505 // Load with fast path based Baker's read barrier.
6506 // /* HeapReference<Object> */ out = *(out + offset)
6507 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6508 out,
6509 out_reg,
6510 offset,
6511 maybe_temp,
6512 /* needs_null_check */ false);
6513 } else {
6514 // Load with slow path based read barrier.
6515 // Save the value of `out` into `maybe_temp` before overwriting it
6516 // in the following move operation, as we will need it for the
6517 // read barrier below.
6518 __ Move(maybe_temp.AsRegister<Register>(), out_reg);
6519 // /* HeapReference<Object> */ out = *(out + offset)
6520 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
6521 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
6522 }
6523 } else {
6524 // Plain load with no read barrier.
6525 // /* HeapReference<Object> */ out = *(out + offset)
6526 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
6527 __ MaybeUnpoisonHeapReference(out_reg);
6528 }
6529}
6530
6531void InstructionCodeGeneratorMIPS::GenerateReferenceLoadTwoRegisters(
6532 HInstruction* instruction,
6533 Location out,
6534 Location obj,
6535 uint32_t offset,
6536 Location maybe_temp,
6537 ReadBarrierOption read_barrier_option) {
6538 Register out_reg = out.AsRegister<Register>();
6539 Register obj_reg = obj.AsRegister<Register>();
6540 if (read_barrier_option == kWithReadBarrier) {
6541 CHECK(kEmitCompilerReadBarrier);
6542 if (kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006543 if (!kBakerReadBarrierThunksEnableForFields) {
6544 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
6545 }
Alexey Frunze15958152017-02-09 19:08:30 -08006546 // Load with fast path based Baker's read barrier.
6547 // /* HeapReference<Object> */ out = *(obj + offset)
6548 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6549 out,
6550 obj_reg,
6551 offset,
6552 maybe_temp,
6553 /* needs_null_check */ false);
6554 } else {
6555 // Load with slow path based read barrier.
6556 // /* HeapReference<Object> */ out = *(obj + offset)
6557 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6558 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6559 }
6560 } else {
6561 // Plain load with no read barrier.
6562 // /* HeapReference<Object> */ out = *(obj + offset)
6563 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6564 __ MaybeUnpoisonHeapReference(out_reg);
6565 }
6566}
6567
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006568static inline int GetBakerMarkThunkNumber(Register reg) {
6569 static_assert(BAKER_MARK_INTROSPECTION_REGISTER_COUNT == 21, "Expecting equal");
6570 if (reg >= V0 && reg <= T7) { // 14 consequtive regs.
6571 return reg - V0;
6572 } else if (reg >= S2 && reg <= S7) { // 6 consequtive regs.
6573 return 14 + (reg - S2);
6574 } else if (reg == FP) { // One more.
6575 return 20;
6576 }
6577 LOG(FATAL) << "Unexpected register " << reg;
6578 UNREACHABLE();
6579}
6580
6581static inline int GetBakerMarkFieldArrayThunkDisplacement(Register reg, bool short_offset) {
6582 int num = GetBakerMarkThunkNumber(reg) +
6583 (short_offset ? BAKER_MARK_INTROSPECTION_REGISTER_COUNT : 0);
6584 return num * BAKER_MARK_INTROSPECTION_FIELD_ARRAY_ENTRY_SIZE;
6585}
6586
6587static inline int GetBakerMarkGcRootThunkDisplacement(Register reg) {
6588 return GetBakerMarkThunkNumber(reg) * BAKER_MARK_INTROSPECTION_GC_ROOT_ENTRY_SIZE +
6589 BAKER_MARK_INTROSPECTION_GC_ROOT_ENTRIES_OFFSET;
6590}
6591
Alexey Frunze15958152017-02-09 19:08:30 -08006592void InstructionCodeGeneratorMIPS::GenerateGcRootFieldLoad(HInstruction* instruction,
6593 Location root,
6594 Register obj,
6595 uint32_t offset,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006596 ReadBarrierOption read_barrier_option,
6597 MipsLabel* label_low) {
6598 bool reordering;
6599 if (label_low != nullptr) {
6600 DCHECK_EQ(offset, 0x5678u);
6601 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07006602 Register root_reg = root.AsRegister<Register>();
Alexey Frunze15958152017-02-09 19:08:30 -08006603 if (read_barrier_option == kWithReadBarrier) {
6604 DCHECK(kEmitCompilerReadBarrier);
6605 if (kUseBakerReadBarrier) {
6606 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6607 // Baker's read barrier are used:
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006608 if (kBakerReadBarrierThunksEnableForGcRoots) {
6609 // Note that we do not actually check the value of `GetIsGcMarking()`
6610 // to decide whether to mark the loaded GC root or not. Instead, we
6611 // load into `temp` (T9) the read barrier mark introspection entrypoint.
6612 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
6613 // vice versa.
6614 //
6615 // We use thunks for the slow path. That thunk checks the reference
6616 // and jumps to the entrypoint if needed.
6617 //
6618 // temp = Thread::Current()->pReadBarrierMarkReg00
6619 // // AKA &art_quick_read_barrier_mark_introspection.
6620 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
6621 // if (temp != nullptr) {
6622 // temp = &gc_root_thunk<root_reg>
6623 // root = temp(root)
6624 // }
Alexey Frunze15958152017-02-09 19:08:30 -08006625
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006626 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
6627 const int32_t entry_point_offset =
6628 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(0);
6629 const int thunk_disp = GetBakerMarkGcRootThunkDisplacement(root_reg);
6630 int16_t offset_low = Low16Bits(offset);
6631 int16_t offset_high = High16Bits(offset - offset_low); // Accounts for sign
6632 // extension in lw.
6633 bool short_offset = IsInt<16>(static_cast<int32_t>(offset));
6634 Register base = short_offset ? obj : TMP;
6635 // Loading the entrypoint does not require a load acquire since it is only changed when
6636 // threads are suspended or running a checkpoint.
6637 __ LoadFromOffset(kLoadWord, T9, TR, entry_point_offset);
6638 reordering = __ SetReorder(false);
6639 if (!short_offset) {
6640 DCHECK(!label_low);
6641 __ AddUpper(base, obj, offset_high);
6642 }
Alexey Frunze0cab6562017-07-25 15:19:36 -07006643 MipsLabel skip_call;
6644 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006645 if (label_low != nullptr) {
6646 DCHECK(short_offset);
6647 __ Bind(label_low);
6648 }
6649 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6650 __ LoadFromOffset(kLoadWord, root_reg, base, offset_low); // Single instruction
6651 // in delay slot.
6652 if (isR6) {
6653 __ Jialc(T9, thunk_disp);
6654 } else {
6655 __ Addiu(T9, T9, thunk_disp);
6656 __ Jalr(T9);
6657 __ Nop();
6658 }
Alexey Frunze0cab6562017-07-25 15:19:36 -07006659 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006660 __ SetReorder(reordering);
6661 } else {
6662 // Note that we do not actually check the value of `GetIsGcMarking()`
6663 // to decide whether to mark the loaded GC root or not. Instead, we
6664 // load into `temp` (T9) the read barrier mark entry point corresponding
6665 // to register `root`. If `temp` is null, it means that `GetIsGcMarking()`
6666 // is false, and vice versa.
6667 //
6668 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
6669 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
6670 // if (temp != null) {
6671 // root = temp(root)
6672 // }
Alexey Frunze15958152017-02-09 19:08:30 -08006673
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006674 if (label_low != nullptr) {
6675 reordering = __ SetReorder(false);
6676 __ Bind(label_low);
6677 }
6678 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6679 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
6680 if (label_low != nullptr) {
6681 __ SetReorder(reordering);
6682 }
6683 static_assert(
6684 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6685 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6686 "have different sizes.");
6687 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6688 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6689 "have different sizes.");
Alexey Frunze15958152017-02-09 19:08:30 -08006690
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006691 // Slow path marking the GC root `root`.
6692 Location temp = Location::RegisterLocation(T9);
6693 SlowPathCodeMIPS* slow_path =
6694 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathMIPS(
6695 instruction,
6696 root,
6697 /*entrypoint*/ temp);
6698 codegen_->AddSlowPath(slow_path);
6699
6700 const int32_t entry_point_offset =
6701 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(root.reg() - 1);
6702 // Loading the entrypoint does not require a load acquire since it is only changed when
6703 // threads are suspended or running a checkpoint.
6704 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), TR, entry_point_offset);
6705 __ Bnez(temp.AsRegister<Register>(), slow_path->GetEntryLabel());
6706 __ Bind(slow_path->GetExitLabel());
6707 }
Alexey Frunze15958152017-02-09 19:08:30 -08006708 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006709 if (label_low != nullptr) {
6710 reordering = __ SetReorder(false);
6711 __ Bind(label_low);
6712 }
Alexey Frunze15958152017-02-09 19:08:30 -08006713 // GC root loaded through a slow path for read barriers other
6714 // than Baker's.
6715 // /* GcRoot<mirror::Object>* */ root = obj + offset
6716 __ Addiu32(root_reg, obj, offset);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006717 if (label_low != nullptr) {
6718 __ SetReorder(reordering);
6719 }
Alexey Frunze15958152017-02-09 19:08:30 -08006720 // /* mirror::Object* */ root = root->Read()
6721 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6722 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07006723 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006724 if (label_low != nullptr) {
6725 reordering = __ SetReorder(false);
6726 __ Bind(label_low);
6727 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07006728 // Plain GC root load with no read barrier.
6729 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6730 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
6731 // Note that GC roots are not affected by heap poisoning, thus we
6732 // do not have to unpoison `root_reg` here.
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006733 if (label_low != nullptr) {
6734 __ SetReorder(reordering);
6735 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07006736 }
6737}
6738
Alexey Frunze15958152017-02-09 19:08:30 -08006739void CodeGeneratorMIPS::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6740 Location ref,
6741 Register obj,
6742 uint32_t offset,
6743 Location temp,
6744 bool needs_null_check) {
6745 DCHECK(kEmitCompilerReadBarrier);
6746 DCHECK(kUseBakerReadBarrier);
6747
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006748 if (kBakerReadBarrierThunksEnableForFields) {
6749 // Note that we do not actually check the value of `GetIsGcMarking()`
6750 // to decide whether to mark the loaded reference or not. Instead, we
6751 // load into `temp` (T9) the read barrier mark introspection entrypoint.
6752 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
6753 // vice versa.
6754 //
6755 // We use thunks for the slow path. That thunk checks the reference
6756 // and jumps to the entrypoint if needed. If the holder is not gray,
6757 // it issues a load-load memory barrier and returns to the original
6758 // reference load.
6759 //
6760 // temp = Thread::Current()->pReadBarrierMarkReg00
6761 // // AKA &art_quick_read_barrier_mark_introspection.
6762 // if (temp != nullptr) {
6763 // temp = &field_array_thunk<holder_reg>
6764 // temp()
6765 // }
6766 // not_gray_return_address:
6767 // // If the offset is too large to fit into the lw instruction, we
6768 // // use an adjusted base register (TMP) here. This register
6769 // // receives bits 16 ... 31 of the offset before the thunk invocation
6770 // // and the thunk benefits from it.
6771 // HeapReference<mirror::Object> reference = *(obj+offset); // Original reference load.
6772 // gray_return_address:
6773
6774 DCHECK(temp.IsInvalid());
6775 bool isR6 = GetInstructionSetFeatures().IsR6();
6776 int16_t offset_low = Low16Bits(offset);
6777 int16_t offset_high = High16Bits(offset - offset_low); // Accounts for sign extension in lw.
6778 bool short_offset = IsInt<16>(static_cast<int32_t>(offset));
6779 bool reordering = __ SetReorder(false);
6780 const int32_t entry_point_offset =
6781 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(0);
6782 // There may have or may have not been a null check if the field offset is smaller than
6783 // the page size.
6784 // There must've been a null check in case it's actually a load from an array.
6785 // We will, however, perform an explicit null check in the thunk as it's easier to
6786 // do it than not.
6787 if (instruction->IsArrayGet()) {
6788 DCHECK(!needs_null_check);
6789 }
6790 const int thunk_disp = GetBakerMarkFieldArrayThunkDisplacement(obj, short_offset);
6791 // Loading the entrypoint does not require a load acquire since it is only changed when
6792 // threads are suspended or running a checkpoint.
6793 __ LoadFromOffset(kLoadWord, T9, TR, entry_point_offset);
6794 Register ref_reg = ref.AsRegister<Register>();
6795 Register base = short_offset ? obj : TMP;
Alexey Frunze0cab6562017-07-25 15:19:36 -07006796 MipsLabel skip_call;
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006797 if (short_offset) {
6798 if (isR6) {
Alexey Frunze0cab6562017-07-25 15:19:36 -07006799 __ Beqzc(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006800 __ Nop(); // In forbidden slot.
6801 __ Jialc(T9, thunk_disp);
6802 } else {
Alexey Frunze0cab6562017-07-25 15:19:36 -07006803 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006804 __ Addiu(T9, T9, thunk_disp); // In delay slot.
6805 __ Jalr(T9);
6806 __ Nop(); // In delay slot.
6807 }
Alexey Frunze0cab6562017-07-25 15:19:36 -07006808 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006809 } else {
6810 if (isR6) {
Alexey Frunze0cab6562017-07-25 15:19:36 -07006811 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006812 __ Aui(base, obj, offset_high); // In delay slot.
6813 __ Jialc(T9, thunk_disp);
Alexey Frunze0cab6562017-07-25 15:19:36 -07006814 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006815 } else {
6816 __ Lui(base, offset_high);
Alexey Frunze0cab6562017-07-25 15:19:36 -07006817 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006818 __ Addiu(T9, T9, thunk_disp); // In delay slot.
6819 __ Jalr(T9);
Alexey Frunze0cab6562017-07-25 15:19:36 -07006820 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006821 __ Addu(base, base, obj); // In delay slot.
6822 }
6823 }
6824 // /* HeapReference<Object> */ ref = *(obj + offset)
6825 __ LoadFromOffset(kLoadWord, ref_reg, base, offset_low); // Single instruction.
6826 if (needs_null_check) {
6827 MaybeRecordImplicitNullCheck(instruction);
6828 }
6829 __ MaybeUnpoisonHeapReference(ref_reg);
6830 __ SetReorder(reordering);
6831 return;
6832 }
6833
Alexey Frunze15958152017-02-09 19:08:30 -08006834 // /* HeapReference<Object> */ ref = *(obj + offset)
6835 Location no_index = Location::NoLocation();
6836 ScaleFactor no_scale_factor = TIMES_1;
6837 GenerateReferenceLoadWithBakerReadBarrier(instruction,
6838 ref,
6839 obj,
6840 offset,
6841 no_index,
6842 no_scale_factor,
6843 temp,
6844 needs_null_check);
6845}
6846
6847void CodeGeneratorMIPS::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6848 Location ref,
6849 Register obj,
6850 uint32_t data_offset,
6851 Location index,
6852 Location temp,
6853 bool needs_null_check) {
6854 DCHECK(kEmitCompilerReadBarrier);
6855 DCHECK(kUseBakerReadBarrier);
6856
6857 static_assert(
6858 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6859 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006860 ScaleFactor scale_factor = TIMES_4;
6861
6862 if (kBakerReadBarrierThunksEnableForArrays) {
6863 // Note that we do not actually check the value of `GetIsGcMarking()`
6864 // to decide whether to mark the loaded reference or not. Instead, we
6865 // load into `temp` (T9) the read barrier mark introspection entrypoint.
6866 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
6867 // vice versa.
6868 //
6869 // We use thunks for the slow path. That thunk checks the reference
6870 // and jumps to the entrypoint if needed. If the holder is not gray,
6871 // it issues a load-load memory barrier and returns to the original
6872 // reference load.
6873 //
6874 // temp = Thread::Current()->pReadBarrierMarkReg00
6875 // // AKA &art_quick_read_barrier_mark_introspection.
6876 // if (temp != nullptr) {
6877 // temp = &field_array_thunk<holder_reg>
6878 // temp()
6879 // }
6880 // not_gray_return_address:
6881 // // The element address is pre-calculated in the TMP register before the
6882 // // thunk invocation and the thunk benefits from it.
6883 // HeapReference<mirror::Object> reference = data[index]; // Original reference load.
6884 // gray_return_address:
6885
6886 DCHECK(temp.IsInvalid());
6887 DCHECK(index.IsValid());
6888 bool reordering = __ SetReorder(false);
6889 const int32_t entry_point_offset =
6890 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(0);
6891 // We will not do the explicit null check in the thunk as some form of a null check
6892 // must've been done earlier.
6893 DCHECK(!needs_null_check);
6894 const int thunk_disp = GetBakerMarkFieldArrayThunkDisplacement(obj, /* short_offset */ false);
6895 // Loading the entrypoint does not require a load acquire since it is only changed when
6896 // threads are suspended or running a checkpoint.
6897 __ LoadFromOffset(kLoadWord, T9, TR, entry_point_offset);
6898 Register ref_reg = ref.AsRegister<Register>();
6899 Register index_reg = index.IsRegisterPair()
6900 ? index.AsRegisterPairLow<Register>()
6901 : index.AsRegister<Register>();
Alexey Frunze0cab6562017-07-25 15:19:36 -07006902 MipsLabel skip_call;
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006903 if (GetInstructionSetFeatures().IsR6()) {
Alexey Frunze0cab6562017-07-25 15:19:36 -07006904 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006905 __ Lsa(TMP, index_reg, obj, scale_factor); // In delay slot.
6906 __ Jialc(T9, thunk_disp);
Alexey Frunze0cab6562017-07-25 15:19:36 -07006907 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006908 } else {
6909 __ Sll(TMP, index_reg, scale_factor);
Alexey Frunze0cab6562017-07-25 15:19:36 -07006910 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006911 __ Addiu(T9, T9, thunk_disp); // In delay slot.
6912 __ Jalr(T9);
Alexey Frunze0cab6562017-07-25 15:19:36 -07006913 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006914 __ Addu(TMP, TMP, obj); // In delay slot.
6915 }
6916 // /* HeapReference<Object> */ ref = *(obj + data_offset + (index << scale_factor))
6917 DCHECK(IsInt<16>(static_cast<int32_t>(data_offset))) << data_offset;
6918 __ LoadFromOffset(kLoadWord, ref_reg, TMP, data_offset); // Single instruction.
6919 __ MaybeUnpoisonHeapReference(ref_reg);
6920 __ SetReorder(reordering);
6921 return;
6922 }
6923
Alexey Frunze15958152017-02-09 19:08:30 -08006924 // /* HeapReference<Object> */ ref =
6925 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Alexey Frunze15958152017-02-09 19:08:30 -08006926 GenerateReferenceLoadWithBakerReadBarrier(instruction,
6927 ref,
6928 obj,
6929 data_offset,
6930 index,
6931 scale_factor,
6932 temp,
6933 needs_null_check);
6934}
6935
6936void CodeGeneratorMIPS::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6937 Location ref,
6938 Register obj,
6939 uint32_t offset,
6940 Location index,
6941 ScaleFactor scale_factor,
6942 Location temp,
6943 bool needs_null_check,
6944 bool always_update_field) {
6945 DCHECK(kEmitCompilerReadBarrier);
6946 DCHECK(kUseBakerReadBarrier);
6947
6948 // In slow path based read barriers, the read barrier call is
6949 // inserted after the original load. However, in fast path based
6950 // Baker's read barriers, we need to perform the load of
6951 // mirror::Object::monitor_ *before* the original reference load.
6952 // This load-load ordering is required by the read barrier.
6953 // The fast path/slow path (for Baker's algorithm) should look like:
6954 //
6955 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6956 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6957 // HeapReference<Object> ref = *src; // Original reference load.
6958 // bool is_gray = (rb_state == ReadBarrier::GrayState());
6959 // if (is_gray) {
6960 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
6961 // }
6962 //
6963 // Note: the original implementation in ReadBarrier::Barrier is
6964 // slightly more complex as it performs additional checks that we do
6965 // not do here for performance reasons.
6966
6967 Register ref_reg = ref.AsRegister<Register>();
6968 Register temp_reg = temp.AsRegister<Register>();
6969 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
6970
6971 // /* int32_t */ monitor = obj->monitor_
6972 __ LoadFromOffset(kLoadWord, temp_reg, obj, monitor_offset);
6973 if (needs_null_check) {
6974 MaybeRecordImplicitNullCheck(instruction);
6975 }
6976 // /* LockWord */ lock_word = LockWord(monitor)
6977 static_assert(sizeof(LockWord) == sizeof(int32_t),
6978 "art::LockWord and int32_t have different sizes.");
6979
6980 __ Sync(0); // Barrier to prevent load-load reordering.
6981
6982 // The actual reference load.
6983 if (index.IsValid()) {
6984 // Load types involving an "index": ArrayGet,
6985 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
6986 // intrinsics.
6987 // /* HeapReference<Object> */ ref = *(obj + offset + (index << scale_factor))
6988 if (index.IsConstant()) {
6989 size_t computed_offset =
6990 (index.GetConstant()->AsIntConstant()->GetValue() << scale_factor) + offset;
6991 __ LoadFromOffset(kLoadWord, ref_reg, obj, computed_offset);
6992 } else {
6993 // Handle the special case of the
6994 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
6995 // intrinsics, which use a register pair as index ("long
6996 // offset"), of which only the low part contains data.
6997 Register index_reg = index.IsRegisterPair()
6998 ? index.AsRegisterPairLow<Register>()
6999 : index.AsRegister<Register>();
Chris Larsencd0295d2017-03-31 15:26:54 -07007000 __ ShiftAndAdd(TMP, index_reg, obj, scale_factor, TMP);
Alexey Frunze15958152017-02-09 19:08:30 -08007001 __ LoadFromOffset(kLoadWord, ref_reg, TMP, offset);
7002 }
7003 } else {
7004 // /* HeapReference<Object> */ ref = *(obj + offset)
7005 __ LoadFromOffset(kLoadWord, ref_reg, obj, offset);
7006 }
7007
7008 // Object* ref = ref_addr->AsMirrorPtr()
7009 __ MaybeUnpoisonHeapReference(ref_reg);
7010
7011 // Slow path marking the object `ref` when it is gray.
7012 SlowPathCodeMIPS* slow_path;
7013 if (always_update_field) {
7014 // ReadBarrierMarkAndUpdateFieldSlowPathMIPS only supports address
7015 // of the form `obj + field_offset`, where `obj` is a register and
7016 // `field_offset` is a register pair (of which only the lower half
7017 // is used). Thus `offset` and `scale_factor` above are expected
7018 // to be null in this code path.
7019 DCHECK_EQ(offset, 0u);
7020 DCHECK_EQ(scale_factor, ScaleFactor::TIMES_1);
7021 slow_path = new (GetGraph()->GetArena())
7022 ReadBarrierMarkAndUpdateFieldSlowPathMIPS(instruction,
7023 ref,
7024 obj,
7025 /* field_offset */ index,
7026 temp_reg);
7027 } else {
7028 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathMIPS(instruction, ref);
7029 }
7030 AddSlowPath(slow_path);
7031
7032 // if (rb_state == ReadBarrier::GrayState())
7033 // ref = ReadBarrier::Mark(ref);
7034 // Given the numeric representation, it's enough to check the low bit of the
7035 // rb_state. We do that by shifting the bit into the sign bit (31) and
7036 // performing a branch on less than zero.
7037 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
7038 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
7039 static_assert(LockWord::kReadBarrierStateSize == 1, "Expecting 1-bit read barrier state size");
7040 __ Sll(temp_reg, temp_reg, 31 - LockWord::kReadBarrierStateShift);
7041 __ Bltz(temp_reg, slow_path->GetEntryLabel());
7042 __ Bind(slow_path->GetExitLabel());
7043}
7044
7045void CodeGeneratorMIPS::GenerateReadBarrierSlow(HInstruction* instruction,
7046 Location out,
7047 Location ref,
7048 Location obj,
7049 uint32_t offset,
7050 Location index) {
7051 DCHECK(kEmitCompilerReadBarrier);
7052
7053 // Insert a slow path based read barrier *after* the reference load.
7054 //
7055 // If heap poisoning is enabled, the unpoisoning of the loaded
7056 // reference will be carried out by the runtime within the slow
7057 // path.
7058 //
7059 // Note that `ref` currently does not get unpoisoned (when heap
7060 // poisoning is enabled), which is alright as the `ref` argument is
7061 // not used by the artReadBarrierSlow entry point.
7062 //
7063 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
7064 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena())
7065 ReadBarrierForHeapReferenceSlowPathMIPS(instruction, out, ref, obj, offset, index);
7066 AddSlowPath(slow_path);
7067
7068 __ B(slow_path->GetEntryLabel());
7069 __ Bind(slow_path->GetExitLabel());
7070}
7071
7072void CodeGeneratorMIPS::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
7073 Location out,
7074 Location ref,
7075 Location obj,
7076 uint32_t offset,
7077 Location index) {
7078 if (kEmitCompilerReadBarrier) {
7079 // Baker's read barriers shall be handled by the fast path
7080 // (CodeGeneratorMIPS::GenerateReferenceLoadWithBakerReadBarrier).
7081 DCHECK(!kUseBakerReadBarrier);
7082 // If heap poisoning is enabled, unpoisoning will be taken care of
7083 // by the runtime within the slow path.
7084 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
7085 } else if (kPoisonHeapReferences) {
7086 __ UnpoisonHeapReference(out.AsRegister<Register>());
7087 }
7088}
7089
7090void CodeGeneratorMIPS::GenerateReadBarrierForRootSlow(HInstruction* instruction,
7091 Location out,
7092 Location root) {
7093 DCHECK(kEmitCompilerReadBarrier);
7094
7095 // Insert a slow path based read barrier *after* the GC root load.
7096 //
7097 // Note that GC roots are not affected by heap poisoning, so we do
7098 // not need to do anything special for this here.
7099 SlowPathCodeMIPS* slow_path =
7100 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathMIPS(instruction, out, root);
7101 AddSlowPath(slow_path);
7102
7103 __ B(slow_path->GetEntryLabel());
7104 __ Bind(slow_path->GetExitLabel());
7105}
7106
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007107void LocationsBuilderMIPS::VisitInstanceOf(HInstanceOf* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007108 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
7109 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexey Frunzec61c0762017-04-10 13:54:23 -07007110 bool baker_read_barrier_slow_path = false;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007111 switch (type_check_kind) {
7112 case TypeCheckKind::kExactCheck:
7113 case TypeCheckKind::kAbstractClassCheck:
7114 case TypeCheckKind::kClassHierarchyCheck:
7115 case TypeCheckKind::kArrayObjectCheck:
Alexey Frunze15958152017-02-09 19:08:30 -08007116 call_kind =
7117 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Alexey Frunzec61c0762017-04-10 13:54:23 -07007118 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007119 break;
7120 case TypeCheckKind::kArrayCheck:
7121 case TypeCheckKind::kUnresolvedCheck:
7122 case TypeCheckKind::kInterfaceCheck:
7123 call_kind = LocationSummary::kCallOnSlowPath;
7124 break;
7125 }
7126
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007127 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexey Frunzec61c0762017-04-10 13:54:23 -07007128 if (baker_read_barrier_slow_path) {
7129 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
7130 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007131 locations->SetInAt(0, Location::RequiresRegister());
7132 locations->SetInAt(1, Location::RequiresRegister());
7133 // The output does overlap inputs.
7134 // Note that TypeCheckSlowPathMIPS uses this register too.
7135 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Alexey Frunze15958152017-02-09 19:08:30 -08007136 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007137}
7138
7139void InstructionCodeGeneratorMIPS::VisitInstanceOf(HInstanceOf* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007140 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007141 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08007142 Location obj_loc = locations->InAt(0);
7143 Register obj = obj_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007144 Register cls = locations->InAt(1).AsRegister<Register>();
Alexey Frunze15958152017-02-09 19:08:30 -08007145 Location out_loc = locations->Out();
7146 Register out = out_loc.AsRegister<Register>();
7147 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
7148 DCHECK_LE(num_temps, 1u);
7149 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007150 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
7151 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
7152 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
7153 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007154 MipsLabel done;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007155 SlowPathCodeMIPS* slow_path = nullptr;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007156
7157 // Return 0 if `obj` is null.
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007158 // Avoid this check if we know `obj` is not null.
7159 if (instruction->MustDoNullCheck()) {
7160 __ Move(out, ZERO);
7161 __ Beqz(obj, &done);
7162 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007163
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007164 switch (type_check_kind) {
7165 case TypeCheckKind::kExactCheck: {
7166 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007167 GenerateReferenceLoadTwoRegisters(instruction,
7168 out_loc,
7169 obj_loc,
7170 class_offset,
7171 maybe_temp_loc,
7172 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007173 // Classes must be equal for the instanceof to succeed.
7174 __ Xor(out, out, cls);
7175 __ Sltiu(out, out, 1);
7176 break;
7177 }
7178
7179 case TypeCheckKind::kAbstractClassCheck: {
7180 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007181 GenerateReferenceLoadTwoRegisters(instruction,
7182 out_loc,
7183 obj_loc,
7184 class_offset,
7185 maybe_temp_loc,
7186 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007187 // If the class is abstract, we eagerly fetch the super class of the
7188 // object to avoid doing a comparison we know will fail.
7189 MipsLabel loop;
7190 __ Bind(&loop);
7191 // /* HeapReference<Class> */ out = out->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08007192 GenerateReferenceLoadOneRegister(instruction,
7193 out_loc,
7194 super_offset,
7195 maybe_temp_loc,
7196 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007197 // If `out` is null, we use it for the result, and jump to `done`.
7198 __ Beqz(out, &done);
7199 __ Bne(out, cls, &loop);
7200 __ LoadConst32(out, 1);
7201 break;
7202 }
7203
7204 case TypeCheckKind::kClassHierarchyCheck: {
7205 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007206 GenerateReferenceLoadTwoRegisters(instruction,
7207 out_loc,
7208 obj_loc,
7209 class_offset,
7210 maybe_temp_loc,
7211 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007212 // Walk over the class hierarchy to find a match.
7213 MipsLabel loop, success;
7214 __ Bind(&loop);
7215 __ Beq(out, cls, &success);
7216 // /* HeapReference<Class> */ out = out->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08007217 GenerateReferenceLoadOneRegister(instruction,
7218 out_loc,
7219 super_offset,
7220 maybe_temp_loc,
7221 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007222 __ Bnez(out, &loop);
7223 // If `out` is null, we use it for the result, and jump to `done`.
7224 __ B(&done);
7225 __ Bind(&success);
7226 __ LoadConst32(out, 1);
7227 break;
7228 }
7229
7230 case TypeCheckKind::kArrayObjectCheck: {
7231 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007232 GenerateReferenceLoadTwoRegisters(instruction,
7233 out_loc,
7234 obj_loc,
7235 class_offset,
7236 maybe_temp_loc,
7237 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007238 // Do an exact check.
7239 MipsLabel success;
7240 __ Beq(out, cls, &success);
7241 // Otherwise, we need to check that the object's class is a non-primitive array.
7242 // /* HeapReference<Class> */ out = out->component_type_
Alexey Frunze15958152017-02-09 19:08:30 -08007243 GenerateReferenceLoadOneRegister(instruction,
7244 out_loc,
7245 component_offset,
7246 maybe_temp_loc,
7247 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007248 // If `out` is null, we use it for the result, and jump to `done`.
7249 __ Beqz(out, &done);
7250 __ LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
7251 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
7252 __ Sltiu(out, out, 1);
7253 __ B(&done);
7254 __ Bind(&success);
7255 __ LoadConst32(out, 1);
7256 break;
7257 }
7258
7259 case TypeCheckKind::kArrayCheck: {
7260 // No read barrier since the slow path will retry upon failure.
7261 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007262 GenerateReferenceLoadTwoRegisters(instruction,
7263 out_loc,
7264 obj_loc,
7265 class_offset,
7266 maybe_temp_loc,
7267 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007268 DCHECK(locations->OnlyCallsOnSlowPath());
7269 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS(instruction,
7270 /* is_fatal */ false);
7271 codegen_->AddSlowPath(slow_path);
7272 __ Bne(out, cls, slow_path->GetEntryLabel());
7273 __ LoadConst32(out, 1);
7274 break;
7275 }
7276
7277 case TypeCheckKind::kUnresolvedCheck:
7278 case TypeCheckKind::kInterfaceCheck: {
7279 // Note that we indeed only call on slow path, but we always go
7280 // into the slow path for the unresolved and interface check
7281 // cases.
7282 //
7283 // We cannot directly call the InstanceofNonTrivial runtime
7284 // entry point without resorting to a type checking slow path
7285 // here (i.e. by calling InvokeRuntime directly), as it would
7286 // require to assign fixed registers for the inputs of this
7287 // HInstanceOf instruction (following the runtime calling
7288 // convention), which might be cluttered by the potential first
7289 // read barrier emission at the beginning of this method.
7290 //
7291 // TODO: Introduce a new runtime entry point taking the object
7292 // to test (instead of its class) as argument, and let it deal
7293 // with the read barrier issues. This will let us refactor this
7294 // case of the `switch` code as it was previously (with a direct
7295 // call to the runtime not using a type checking slow path).
7296 // This should also be beneficial for the other cases above.
7297 DCHECK(locations->OnlyCallsOnSlowPath());
7298 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS(instruction,
7299 /* is_fatal */ false);
7300 codegen_->AddSlowPath(slow_path);
7301 __ B(slow_path->GetEntryLabel());
7302 break;
7303 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007304 }
7305
7306 __ Bind(&done);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007307
7308 if (slow_path != nullptr) {
7309 __ Bind(slow_path->GetExitLabel());
7310 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007311}
7312
7313void LocationsBuilderMIPS::VisitIntConstant(HIntConstant* constant) {
7314 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
7315 locations->SetOut(Location::ConstantLocation(constant));
7316}
7317
7318void InstructionCodeGeneratorMIPS::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
7319 // Will be generated at use site.
7320}
7321
7322void LocationsBuilderMIPS::VisitNullConstant(HNullConstant* constant) {
7323 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
7324 locations->SetOut(Location::ConstantLocation(constant));
7325}
7326
7327void InstructionCodeGeneratorMIPS::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
7328 // Will be generated at use site.
7329}
7330
7331void LocationsBuilderMIPS::HandleInvoke(HInvoke* invoke) {
7332 InvokeDexCallingConventionVisitorMIPS calling_convention_visitor;
7333 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
7334}
7335
7336void LocationsBuilderMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
7337 HandleInvoke(invoke);
Alexey Frunze1b8464d2016-11-12 17:22:05 -08007338 // The register T7 is required to be used for the hidden argument in
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007339 // art_quick_imt_conflict_trampoline, so add the hidden argument.
Alexey Frunze1b8464d2016-11-12 17:22:05 -08007340 invoke->GetLocations()->AddTemp(Location::RegisterLocation(T7));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007341}
7342
7343void InstructionCodeGeneratorMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
7344 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
7345 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007346 Location receiver = invoke->GetLocations()->InAt(0);
7347 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07007348 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007349
7350 // Set the hidden argument.
7351 __ LoadConst32(invoke->GetLocations()->GetTemp(1).AsRegister<Register>(),
7352 invoke->GetDexMethodIndex());
7353
7354 // temp = object->GetClass();
7355 if (receiver.IsStackSlot()) {
7356 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
7357 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
7358 } else {
7359 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
7360 }
7361 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08007362 // Instead of simply (possibly) unpoisoning `temp` here, we should
7363 // emit a read barrier for the previous class reference load.
7364 // However this is not required in practice, as this is an
7365 // intermediate/temporary reference and because the current
7366 // concurrent copying collector keeps the from-space memory
7367 // intact/accessible until the end of the marking phase (the
7368 // concurrent copying collector may not in the future).
7369 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00007370 __ LoadFromOffset(kLoadWord, temp, temp,
7371 mirror::Class::ImtPtrOffset(kMipsPointerSize).Uint32Value());
7372 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00007373 invoke->GetImtIndex(), kMipsPointerSize));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007374 // temp = temp->GetImtEntryAt(method_offset);
7375 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
7376 // T9 = temp->GetEntryPoint();
7377 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
7378 // T9();
7379 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07007380 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007381 DCHECK(!codegen_->IsLeafMethod());
7382 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
7383}
7384
7385void LocationsBuilderMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen701566a2015-10-27 15:29:13 -07007386 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
7387 if (intrinsic.TryDispatch(invoke)) {
7388 return;
7389 }
7390
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007391 HandleInvoke(invoke);
7392}
7393
7394void LocationsBuilderMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00007395 // Explicit clinit checks triggered by static invokes must have been pruned by
7396 // art::PrepareForRegisterAllocation.
7397 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007398
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007399 bool is_r6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007400 bool has_irreducible_loops = codegen_->GetGraph()->HasIrreducibleLoops();
7401 bool has_extra_input = invoke->HasPcRelativeMethodLoadKind() && !is_r6 && !has_irreducible_loops;
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007402
Chris Larsen701566a2015-10-27 15:29:13 -07007403 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
7404 if (intrinsic.TryDispatch(invoke)) {
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007405 if (invoke->GetLocations()->CanCall() && has_extra_input) {
7406 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
7407 }
Chris Larsen701566a2015-10-27 15:29:13 -07007408 return;
7409 }
7410
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007411 HandleInvoke(invoke);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007412
7413 // Add the extra input register if either the dex cache array base register
7414 // or the PC-relative base register for accessing literals is needed.
7415 if (has_extra_input) {
7416 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
7417 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007418}
7419
Orion Hodsonac141392017-01-13 11:53:47 +00007420void LocationsBuilderMIPS::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
7421 HandleInvoke(invoke);
7422}
7423
7424void InstructionCodeGeneratorMIPS::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
7425 codegen_->GenerateInvokePolymorphicCall(invoke);
7426}
7427
Chris Larsen701566a2015-10-27 15:29:13 -07007428static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorMIPS* codegen) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007429 if (invoke->GetLocations()->Intrinsified()) {
Chris Larsen701566a2015-10-27 15:29:13 -07007430 IntrinsicCodeGeneratorMIPS intrinsic(codegen);
7431 intrinsic.Dispatch(invoke);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007432 return true;
7433 }
7434 return false;
7435}
7436
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007437HLoadString::LoadKind CodeGeneratorMIPS::GetSupportedLoadStringKind(
Alexey Frunze06a46c42016-07-19 15:00:40 -07007438 HLoadString::LoadKind desired_string_load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007439 switch (desired_string_load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007440 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007441 case HLoadString::LoadKind::kBootImageInternTable:
Vladimir Markoaad75c62016-10-03 08:46:48 +00007442 case HLoadString::LoadKind::kBssEntry:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007443 DCHECK(!Runtime::Current()->UseJitCompilation());
Alexey Frunze06a46c42016-07-19 15:00:40 -07007444 break;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007445 case HLoadString::LoadKind::kJitTableAddress:
7446 DCHECK(Runtime::Current()->UseJitCompilation());
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007447 break;
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007448 case HLoadString::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007449 case HLoadString::LoadKind::kRuntimeCall:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007450 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007451 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007452 return desired_string_load_kind;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007453}
7454
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007455HLoadClass::LoadKind CodeGeneratorMIPS::GetSupportedLoadClassKind(
7456 HLoadClass::LoadKind desired_class_load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007457 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00007458 case HLoadClass::LoadKind::kInvalid:
7459 LOG(FATAL) << "UNREACHABLE";
7460 UNREACHABLE();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007461 case HLoadClass::LoadKind::kReferrersClass:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007462 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007463 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko94ec2db2017-09-06 17:21:03 +01007464 case HLoadClass::LoadKind::kBootImageClassTable:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007465 case HLoadClass::LoadKind::kBssEntry:
7466 DCHECK(!Runtime::Current()->UseJitCompilation());
7467 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007468 case HLoadClass::LoadKind::kJitTableAddress:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007469 DCHECK(Runtime::Current()->UseJitCompilation());
Alexey Frunze06a46c42016-07-19 15:00:40 -07007470 break;
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007471 case HLoadClass::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007472 case HLoadClass::LoadKind::kRuntimeCall:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007473 break;
7474 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007475 return desired_class_load_kind;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007476}
7477
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007478Register CodeGeneratorMIPS::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
7479 Register temp) {
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007480 CHECK(!GetInstructionSetFeatures().IsR6());
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007481 CHECK(!GetGraph()->HasIrreducibleLoops());
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007482 CHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
7483 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
7484 if (!invoke->GetLocations()->Intrinsified()) {
7485 return location.AsRegister<Register>();
7486 }
7487 // For intrinsics we allow any location, so it may be on the stack.
7488 if (!location.IsRegister()) {
7489 __ LoadFromOffset(kLoadWord, temp, SP, location.GetStackIndex());
7490 return temp;
7491 }
7492 // For register locations, check if the register was saved. If so, get it from the stack.
7493 // Note: There is a chance that the register was saved but not overwritten, so we could
7494 // save one load. However, since this is just an intrinsic slow path we prefer this
7495 // simple and more robust approach rather that trying to determine if that's the case.
7496 SlowPathCode* slow_path = GetCurrentSlowPath();
7497 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
7498 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
7499 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
7500 __ LoadFromOffset(kLoadWord, temp, SP, stack_offset);
7501 return temp;
7502 }
7503 return location.AsRegister<Register>();
7504}
7505
Vladimir Markodc151b22015-10-15 18:02:30 +01007506HInvokeStaticOrDirect::DispatchInfo CodeGeneratorMIPS::GetSupportedInvokeStaticOrDirectDispatch(
7507 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01007508 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007509 return desired_dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01007510}
7511
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007512void CodeGeneratorMIPS::GenerateStaticOrDirectCall(
7513 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007514 // All registers are assumed to be correctly set up per the calling convention.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007515 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007516 HInvokeStaticOrDirect::MethodLoadKind method_load_kind = invoke->GetMethodLoadKind();
7517 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location = invoke->GetCodePtrLocation();
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007518 bool is_r6 = GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007519 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
7520 Register base_reg = (invoke->HasPcRelativeMethodLoadKind() && !is_r6 && !has_irreducible_loops)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007521 ? GetInvokeStaticOrDirectExtraParameter(invoke, temp.AsRegister<Register>())
7522 : ZERO;
7523
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007524 switch (method_load_kind) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007525 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007526 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007527 uint32_t offset =
7528 GetThreadOffset<kMipsPointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007529 __ LoadFromOffset(kLoadWord,
7530 temp.AsRegister<Register>(),
7531 TR,
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007532 offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007533 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007534 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007535 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00007536 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007537 break;
Vladimir Marko65979462017-05-19 17:25:12 +01007538 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative: {
7539 DCHECK(GetCompilerOptions().IsBootImage());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007540 PcRelativePatchInfo* info_high = NewPcRelativeMethodPatch(invoke->GetTargetMethod());
7541 PcRelativePatchInfo* info_low =
7542 NewPcRelativeMethodPatch(invoke->GetTargetMethod(), info_high);
Vladimir Marko65979462017-05-19 17:25:12 +01007543 Register temp_reg = temp.AsRegister<Register>();
Alexey Frunzea663d9d2017-07-31 18:43:18 -07007544 EmitPcRelativeAddressPlaceholderHigh(info_high, TMP, base_reg);
7545 __ Addiu(temp_reg, TMP, /* placeholder */ 0x5678, &info_low->label);
Vladimir Marko65979462017-05-19 17:25:12 +01007546 break;
7547 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007548 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
7549 __ LoadConst32(temp.AsRegister<Register>(), invoke->GetMethodAddress());
7550 break;
Vladimir Marko0eb882b2017-05-15 13:39:18 +01007551 case HInvokeStaticOrDirect::MethodLoadKind::kBssEntry: {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007552 PcRelativePatchInfo* info_high = NewMethodBssEntryPatch(
Vladimir Marko0eb882b2017-05-15 13:39:18 +01007553 MethodReference(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex()));
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007554 PcRelativePatchInfo* info_low = NewMethodBssEntryPatch(
7555 MethodReference(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex()), info_high);
Vladimir Marko0eb882b2017-05-15 13:39:18 +01007556 Register temp_reg = temp.AsRegister<Register>();
Alexey Frunzea663d9d2017-07-31 18:43:18 -07007557 EmitPcRelativeAddressPlaceholderHigh(info_high, TMP, base_reg);
7558 __ Lw(temp_reg, TMP, /* placeholder */ 0x5678, &info_low->label);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007559 break;
Vladimir Marko0eb882b2017-05-15 13:39:18 +01007560 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007561 case HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall: {
7562 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
7563 return; // No code pointer retrieval; the runtime performs the call directly.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007564 }
7565 }
7566
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007567 switch (code_ptr_location) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007568 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007569 __ Bal(&frame_entry_label_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007570 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007571 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
7572 // T9 = callee_method->entry_point_from_quick_compiled_code_;
Goran Jakovljevic1a878372015-10-26 14:28:52 +01007573 __ LoadFromOffset(kLoadWord,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007574 T9,
7575 callee_method.AsRegister<Register>(),
7576 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07007577 kMipsPointerSize).Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007578 // T9()
7579 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07007580 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007581 break;
7582 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007583 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
7584
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007585 DCHECK(!IsLeafMethod());
7586}
7587
7588void InstructionCodeGeneratorMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00007589 // Explicit clinit checks triggered by static invokes must have been pruned by
7590 // art::PrepareForRegisterAllocation.
7591 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007592
7593 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
7594 return;
7595 }
7596
7597 LocationSummary* locations = invoke->GetLocations();
7598 codegen_->GenerateStaticOrDirectCall(invoke,
7599 locations->HasTemps()
7600 ? locations->GetTemp(0)
7601 : Location::NoLocation());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007602}
7603
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007604void CodeGeneratorMIPS::GenerateVirtualCall(
7605 HInvokeVirtual* invoke, Location temp_location, SlowPathCode* slow_path) {
Goran Jakovljevice919b072016-10-04 10:17:34 +02007606 // Use the calling convention instead of the location of the receiver, as
7607 // intrinsics may have put the receiver in a different register. In the intrinsics
7608 // slow path, the arguments have been moved to the right place, so here we are
7609 // guaranteed that the receiver is the first register of the calling convention.
7610 InvokeDexCallingConvention calling_convention;
7611 Register receiver = calling_convention.GetRegisterAt(0);
7612
Chris Larsen3acee732015-11-18 13:31:08 -08007613 Register temp = temp_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007614 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
7615 invoke->GetVTableIndex(), kMipsPointerSize).SizeValue();
7616 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07007617 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007618
7619 // temp = object->GetClass();
Goran Jakovljevice919b072016-10-04 10:17:34 +02007620 __ LoadFromOffset(kLoadWord, temp, receiver, class_offset);
Chris Larsen3acee732015-11-18 13:31:08 -08007621 MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08007622 // Instead of simply (possibly) unpoisoning `temp` here, we should
7623 // emit a read barrier for the previous class reference load.
7624 // However this is not required in practice, as this is an
7625 // intermediate/temporary reference and because the current
7626 // concurrent copying collector keeps the from-space memory
7627 // intact/accessible until the end of the marking phase (the
7628 // concurrent copying collector may not in the future).
7629 __ MaybeUnpoisonHeapReference(temp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007630 // temp = temp->GetMethodAt(method_offset);
7631 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
7632 // T9 = temp->GetEntryPoint();
7633 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
7634 // T9();
7635 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07007636 __ NopIfNoReordering();
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007637 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Chris Larsen3acee732015-11-18 13:31:08 -08007638}
7639
7640void InstructionCodeGeneratorMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
7641 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
7642 return;
7643 }
7644
7645 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007646 DCHECK(!codegen_->IsLeafMethod());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007647}
7648
7649void LocationsBuilderMIPS::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00007650 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007651 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007652 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07007653 Location loc = Location::RegisterLocation(calling_convention.GetRegisterAt(0));
7654 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(cls, loc, loc);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007655 return;
7656 }
Vladimir Marko41559982017-01-06 14:04:23 +00007657 DCHECK(!cls->NeedsAccessCheck());
Alexey Frunzec61c0762017-04-10 13:54:23 -07007658 const bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007659 const bool has_irreducible_loops = codegen_->GetGraph()->HasIrreducibleLoops();
Alexey Frunze15958152017-02-09 19:08:30 -08007660 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
7661 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Alexey Frunze06a46c42016-07-19 15:00:40 -07007662 ? LocationSummary::kCallOnSlowPath
7663 : LocationSummary::kNoCall;
7664 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Alexey Frunzec61c0762017-04-10 13:54:23 -07007665 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
7666 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
7667 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007668 switch (load_kind) {
7669 // We need an extra register for PC-relative literals on R2.
Alexey Frunze06a46c42016-07-19 15:00:40 -07007670 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007671 case HLoadClass::LoadKind::kBootImageAddress:
Vladimir Marko94ec2db2017-09-06 17:21:03 +01007672 case HLoadClass::LoadKind::kBootImageClassTable:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007673 case HLoadClass::LoadKind::kBssEntry:
Alexey Frunzec61c0762017-04-10 13:54:23 -07007674 if (isR6) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007675 break;
7676 }
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007677 if (has_irreducible_loops) {
7678 codegen_->ClobberRA();
7679 break;
7680 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007681 FALLTHROUGH_INTENDED;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007682 case HLoadClass::LoadKind::kReferrersClass:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007683 locations->SetInAt(0, Location::RequiresRegister());
7684 break;
7685 default:
7686 break;
7687 }
7688 locations->SetOut(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07007689 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
7690 if (!kUseReadBarrier || kUseBakerReadBarrier) {
7691 // Rely on the type resolution or initialization and marking to save everything we need.
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007692 // Request a temp to hold the BSS entry location for the slow path.
7693 locations->AddTemp(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07007694 RegisterSet caller_saves = RegisterSet::Empty();
7695 InvokeRuntimeCallingConvention calling_convention;
7696 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
7697 locations->SetCustomSlowPathCallerSaves(caller_saves);
7698 } else {
7699 // For non-Baker read barriers we have a temp-clobbering call.
7700 }
7701 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007702}
7703
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007704// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
7705// move.
7706void InstructionCodeGeneratorMIPS::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00007707 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007708 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00007709 codegen_->GenerateLoadClassRuntimeCall(cls);
Pavle Batutae87a7182015-10-28 13:10:42 +01007710 return;
7711 }
Vladimir Marko41559982017-01-06 14:04:23 +00007712 DCHECK(!cls->NeedsAccessCheck());
Pavle Batutae87a7182015-10-28 13:10:42 +01007713
Vladimir Marko41559982017-01-06 14:04:23 +00007714 LocationSummary* locations = cls->GetLocations();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007715 Location out_loc = locations->Out();
7716 Register out = out_loc.AsRegister<Register>();
7717 Register base_or_current_method_reg;
7718 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007719 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007720 switch (load_kind) {
7721 // We need an extra register for PC-relative literals on R2.
Alexey Frunze06a46c42016-07-19 15:00:40 -07007722 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007723 case HLoadClass::LoadKind::kBootImageAddress:
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007724 case HLoadClass::LoadKind::kBootImageClassTable:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007725 case HLoadClass::LoadKind::kBssEntry:
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007726 base_or_current_method_reg =
7727 (isR6 || has_irreducible_loops) ? ZERO : locations->InAt(0).AsRegister<Register>();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007728 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007729 case HLoadClass::LoadKind::kReferrersClass:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007730 case HLoadClass::LoadKind::kRuntimeCall:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007731 base_or_current_method_reg = locations->InAt(0).AsRegister<Register>();
7732 break;
7733 default:
7734 base_or_current_method_reg = ZERO;
7735 break;
7736 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00007737
Alexey Frunze15958152017-02-09 19:08:30 -08007738 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
7739 ? kWithoutReadBarrier
7740 : kCompilerReadBarrierOption;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007741 bool generate_null_check = false;
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007742 CodeGeneratorMIPS::PcRelativePatchInfo* bss_info_high = nullptr;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007743 switch (load_kind) {
7744 case HLoadClass::LoadKind::kReferrersClass: {
7745 DCHECK(!cls->CanCallRuntime());
7746 DCHECK(!cls->MustGenerateClinitCheck());
7747 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
7748 GenerateGcRootFieldLoad(cls,
7749 out_loc,
7750 base_or_current_method_reg,
Alexey Frunze15958152017-02-09 19:08:30 -08007751 ArtMethod::DeclaringClassOffset().Int32Value(),
7752 read_barrier_option);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007753 break;
7754 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007755 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007756 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze15958152017-02-09 19:08:30 -08007757 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007758 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
Alexey Frunze06a46c42016-07-19 15:00:40 -07007759 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007760 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
7761 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex(), info_high);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007762 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
7763 out,
Alexey Frunzea663d9d2017-07-31 18:43:18 -07007764 base_or_current_method_reg);
7765 __ Addiu(out, out, /* placeholder */ 0x5678, &info_low->label);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007766 break;
7767 }
7768 case HLoadClass::LoadKind::kBootImageAddress: {
Alexey Frunze15958152017-02-09 19:08:30 -08007769 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007770 uint32_t address = dchecked_integral_cast<uint32_t>(
7771 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
7772 DCHECK_NE(address, 0u);
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007773 if (isR6 || !has_irreducible_loops) {
7774 __ LoadLiteral(out,
7775 base_or_current_method_reg,
7776 codegen_->DeduplicateBootImageAddressLiteral(address));
7777 } else {
7778 __ LoadConst32(out, address);
7779 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007780 break;
7781 }
Vladimir Marko94ec2db2017-09-06 17:21:03 +01007782 case HLoadClass::LoadKind::kBootImageClassTable: {
7783 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
7784 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
7785 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
7786 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
7787 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex(), info_high);
7788 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
7789 out,
7790 base_or_current_method_reg);
7791 __ Lw(out, out, /* placeholder */ 0x5678, &info_low->label);
7792 // Extract the reference from the slot data, i.e. clear the hash bits.
7793 int32_t masked_hash = ClassTable::TableSlot::MaskHash(
7794 ComputeModifiedUtf8Hash(cls->GetDexFile().StringByTypeIdx(cls->GetTypeIndex())));
7795 if (masked_hash != 0) {
7796 __ Addiu(out, out, -masked_hash);
7797 }
7798 break;
7799 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007800 case HLoadClass::LoadKind::kBssEntry: {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007801 bss_info_high = codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex());
7802 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
7803 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex(), bss_info_high);
Alexey Frunzec61c0762017-04-10 13:54:23 -07007804 constexpr bool non_baker_read_barrier = kUseReadBarrier && !kUseBakerReadBarrier;
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007805 Register temp = non_baker_read_barrier ? out : locations->GetTemp(0).AsRegister<Register>();
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007806 codegen_->EmitPcRelativeAddressPlaceholderHigh(bss_info_high,
7807 temp,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007808 base_or_current_method_reg);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007809 GenerateGcRootFieldLoad(cls,
7810 out_loc,
7811 temp,
7812 /* placeholder */ 0x5678,
7813 read_barrier_option,
7814 &info_low->label);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007815 generate_null_check = true;
7816 break;
7817 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007818 case HLoadClass::LoadKind::kJitTableAddress: {
Alexey Frunze627c1a02017-01-30 19:28:14 -08007819 CodeGeneratorMIPS::JitPatchInfo* info = codegen_->NewJitRootClassPatch(cls->GetDexFile(),
7820 cls->GetTypeIndex(),
7821 cls->GetClass());
7822 bool reordering = __ SetReorder(false);
7823 __ Bind(&info->high_label);
7824 __ Lui(out, /* placeholder */ 0x1234);
Alexey Frunze627c1a02017-01-30 19:28:14 -08007825 __ SetReorder(reordering);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007826 GenerateGcRootFieldLoad(cls,
7827 out_loc,
7828 out,
7829 /* placeholder */ 0x5678,
7830 read_barrier_option,
7831 &info->low_label);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007832 break;
7833 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007834 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00007835 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00007836 LOG(FATAL) << "UNREACHABLE";
7837 UNREACHABLE();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007838 }
7839
7840 if (generate_null_check || cls->MustGenerateClinitCheck()) {
7841 DCHECK(cls->CanCallRuntime());
7842 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007843 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck(), bss_info_high);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007844 codegen_->AddSlowPath(slow_path);
7845 if (generate_null_check) {
7846 __ Beqz(out, slow_path->GetEntryLabel());
7847 }
7848 if (cls->MustGenerateClinitCheck()) {
7849 GenerateClassInitializationCheck(slow_path, out);
7850 } else {
7851 __ Bind(slow_path->GetExitLabel());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007852 }
7853 }
7854}
7855
7856static int32_t GetExceptionTlsOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -07007857 return Thread::ExceptionOffset<kMipsPointerSize>().Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007858}
7859
7860void LocationsBuilderMIPS::VisitLoadException(HLoadException* load) {
7861 LocationSummary* locations =
7862 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
7863 locations->SetOut(Location::RequiresRegister());
7864}
7865
7866void InstructionCodeGeneratorMIPS::VisitLoadException(HLoadException* load) {
7867 Register out = load->GetLocations()->Out().AsRegister<Register>();
7868 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
7869}
7870
7871void LocationsBuilderMIPS::VisitClearException(HClearException* clear) {
7872 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
7873}
7874
7875void InstructionCodeGeneratorMIPS::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
7876 __ StoreToOffset(kStoreWord, ZERO, TR, GetExceptionTlsOffset());
7877}
7878
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007879void LocationsBuilderMIPS::VisitLoadString(HLoadString* load) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08007880 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Nicolas Geoffray917d0162015-11-24 18:25:35 +00007881 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007882 HLoadString::LoadKind load_kind = load->GetLoadKind();
Alexey Frunzec61c0762017-04-10 13:54:23 -07007883 const bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007884 const bool has_irreducible_loops = codegen_->GetGraph()->HasIrreducibleLoops();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007885 switch (load_kind) {
7886 // We need an extra register for PC-relative literals on R2.
Alexey Frunze06a46c42016-07-19 15:00:40 -07007887 case HLoadString::LoadKind::kBootImageAddress:
7888 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007889 case HLoadString::LoadKind::kBootImageInternTable:
Vladimir Markoaad75c62016-10-03 08:46:48 +00007890 case HLoadString::LoadKind::kBssEntry:
Alexey Frunzec61c0762017-04-10 13:54:23 -07007891 if (isR6) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007892 break;
7893 }
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007894 if (has_irreducible_loops) {
7895 codegen_->ClobberRA();
7896 break;
7897 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007898 FALLTHROUGH_INTENDED;
7899 // We need an extra register for PC-relative dex cache accesses.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007900 case HLoadString::LoadKind::kRuntimeCall:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007901 locations->SetInAt(0, Location::RequiresRegister());
7902 break;
7903 default:
7904 break;
7905 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007906 if (load_kind == HLoadString::LoadKind::kRuntimeCall) {
Alexey Frunzebb51df82016-11-01 16:07:32 -07007907 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07007908 locations->SetOut(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Alexey Frunzebb51df82016-11-01 16:07:32 -07007909 } else {
7910 locations->SetOut(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07007911 if (load_kind == HLoadString::LoadKind::kBssEntry) {
7912 if (!kUseReadBarrier || kUseBakerReadBarrier) {
7913 // Rely on the pResolveString and marking to save everything we need.
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007914 // Request a temp to hold the BSS entry location for the slow path.
7915 locations->AddTemp(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07007916 RegisterSet caller_saves = RegisterSet::Empty();
7917 InvokeRuntimeCallingConvention calling_convention;
7918 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
7919 locations->SetCustomSlowPathCallerSaves(caller_saves);
7920 } else {
7921 // For non-Baker read barriers we have a temp-clobbering call.
7922 }
7923 }
Alexey Frunzebb51df82016-11-01 16:07:32 -07007924 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007925}
7926
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007927// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
7928// move.
7929void InstructionCodeGeneratorMIPS::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007930 HLoadString::LoadKind load_kind = load->GetLoadKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007931 LocationSummary* locations = load->GetLocations();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007932 Location out_loc = locations->Out();
7933 Register out = out_loc.AsRegister<Register>();
7934 Register base_or_current_method_reg;
7935 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007936 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007937 switch (load_kind) {
7938 // We need an extra register for PC-relative literals on R2.
Alexey Frunze06a46c42016-07-19 15:00:40 -07007939 case HLoadString::LoadKind::kBootImageAddress:
7940 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007941 case HLoadString::LoadKind::kBootImageInternTable:
Vladimir Markoaad75c62016-10-03 08:46:48 +00007942 case HLoadString::LoadKind::kBssEntry:
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007943 base_or_current_method_reg =
7944 (isR6 || has_irreducible_loops) ? ZERO : locations->InAt(0).AsRegister<Register>();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007945 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007946 default:
7947 base_or_current_method_reg = ZERO;
7948 break;
7949 }
7950
7951 switch (load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007952 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Markoaad75c62016-10-03 08:46:48 +00007953 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007954 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007955 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007956 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
7957 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex(), info_high);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007958 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
7959 out,
Alexey Frunzea663d9d2017-07-31 18:43:18 -07007960 base_or_current_method_reg);
7961 __ Addiu(out, out, /* placeholder */ 0x5678, &info_low->label);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007962 return;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007963 }
7964 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007965 uint32_t address = dchecked_integral_cast<uint32_t>(
7966 reinterpret_cast<uintptr_t>(load->GetString().Get()));
7967 DCHECK_NE(address, 0u);
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007968 if (isR6 || !has_irreducible_loops) {
7969 __ LoadLiteral(out,
7970 base_or_current_method_reg,
7971 codegen_->DeduplicateBootImageAddressLiteral(address));
7972 } else {
7973 __ LoadConst32(out, address);
7974 }
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007975 return;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007976 }
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007977 case HLoadString::LoadKind::kBootImageInternTable: {
Vladimir Markoaad75c62016-10-03 08:46:48 +00007978 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007979 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007980 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007981 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
7982 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex(), info_high);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007983 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
7984 out,
7985 base_or_current_method_reg);
7986 __ Lw(out, out, /* placeholder */ 0x5678, &info_low->label);
7987 return;
7988 }
7989 case HLoadString::LoadKind::kBssEntry: {
7990 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
7991 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
7992 codegen_->NewStringBssEntryPatch(load->GetDexFile(), load->GetStringIndex());
7993 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
7994 codegen_->NewStringBssEntryPatch(load->GetDexFile(), load->GetStringIndex(), info_high);
Alexey Frunzec61c0762017-04-10 13:54:23 -07007995 constexpr bool non_baker_read_barrier = kUseReadBarrier && !kUseBakerReadBarrier;
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007996 Register temp = non_baker_read_barrier ? out : locations->GetTemp(0).AsRegister<Register>();
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007997 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
7998 temp,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007999 base_or_current_method_reg);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008000 GenerateGcRootFieldLoad(load,
8001 out_loc,
8002 temp,
8003 /* placeholder */ 0x5678,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07008004 kCompilerReadBarrierOption,
8005 &info_low->label);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008006 SlowPathCodeMIPS* slow_path =
8007 new (GetGraph()->GetArena()) LoadStringSlowPathMIPS(load, info_high);
Vladimir Markoaad75c62016-10-03 08:46:48 +00008008 codegen_->AddSlowPath(slow_path);
8009 __ Beqz(out, slow_path->GetEntryLabel());
8010 __ Bind(slow_path->GetExitLabel());
8011 return;
8012 }
Alexey Frunze627c1a02017-01-30 19:28:14 -08008013 case HLoadString::LoadKind::kJitTableAddress: {
8014 CodeGeneratorMIPS::JitPatchInfo* info =
8015 codegen_->NewJitRootStringPatch(load->GetDexFile(),
8016 load->GetStringIndex(),
8017 load->GetString());
8018 bool reordering = __ SetReorder(false);
8019 __ Bind(&info->high_label);
8020 __ Lui(out, /* placeholder */ 0x1234);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07008021 __ SetReorder(reordering);
Alexey Frunze15958152017-02-09 19:08:30 -08008022 GenerateGcRootFieldLoad(load,
8023 out_loc,
8024 out,
8025 /* placeholder */ 0x5678,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07008026 kCompilerReadBarrierOption,
8027 &info->low_label);
Alexey Frunze627c1a02017-01-30 19:28:14 -08008028 return;
8029 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07008030 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07008031 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07008032 }
Nicolas Geoffray917d0162015-11-24 18:25:35 +00008033
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07008034 // TODO: Re-add the compiler code to do string dex cache lookup again.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01008035 DCHECK(load_kind == HLoadString::LoadKind::kRuntimeCall);
Vladimir Markoaad75c62016-10-03 08:46:48 +00008036 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07008037 DCHECK_EQ(calling_convention.GetRegisterAt(0), out);
Andreas Gampe8a0128a2016-11-28 07:38:35 -08008038 __ LoadConst32(calling_convention.GetRegisterAt(0), load->GetStringIndex().index_);
Vladimir Markoaad75c62016-10-03 08:46:48 +00008039 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
8040 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008041}
8042
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008043void LocationsBuilderMIPS::VisitLongConstant(HLongConstant* constant) {
8044 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
8045 locations->SetOut(Location::ConstantLocation(constant));
8046}
8047
8048void InstructionCodeGeneratorMIPS::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
8049 // Will be generated at use site.
8050}
8051
8052void LocationsBuilderMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
8053 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01008054 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008055 InvokeRuntimeCallingConvention calling_convention;
8056 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
8057}
8058
8059void InstructionCodeGeneratorMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
8060 if (instruction->IsEnter()) {
Serban Constantinescufca16662016-07-14 09:21:59 +01008061 codegen_->InvokeRuntime(kQuickLockObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008062 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
8063 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01008064 codegen_->InvokeRuntime(kQuickUnlockObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008065 }
8066 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
8067}
8068
8069void LocationsBuilderMIPS::VisitMul(HMul* mul) {
8070 LocationSummary* locations =
8071 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
8072 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008073 case DataType::Type::kInt32:
8074 case DataType::Type::kInt64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008075 locations->SetInAt(0, Location::RequiresRegister());
8076 locations->SetInAt(1, Location::RequiresRegister());
8077 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8078 break;
8079
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008080 case DataType::Type::kFloat32:
8081 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008082 locations->SetInAt(0, Location::RequiresFpuRegister());
8083 locations->SetInAt(1, Location::RequiresFpuRegister());
8084 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
8085 break;
8086
8087 default:
8088 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
8089 }
8090}
8091
8092void InstructionCodeGeneratorMIPS::VisitMul(HMul* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008093 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008094 LocationSummary* locations = instruction->GetLocations();
8095 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
8096
8097 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008098 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008099 Register dst = locations->Out().AsRegister<Register>();
8100 Register lhs = locations->InAt(0).AsRegister<Register>();
8101 Register rhs = locations->InAt(1).AsRegister<Register>();
8102
8103 if (isR6) {
8104 __ MulR6(dst, lhs, rhs);
8105 } else {
8106 __ MulR2(dst, lhs, rhs);
8107 }
8108 break;
8109 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008110 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008111 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8112 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
8113 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
8114 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
8115 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
8116 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
8117
8118 // Extra checks to protect caused by the existance of A1_A2.
8119 // The algorithm is wrong if dst_high is either lhs_lo or rhs_lo:
8120 // (e.g. lhs=a0_a1, rhs=a2_a3 and dst=a1_a2).
8121 DCHECK_NE(dst_high, lhs_low);
8122 DCHECK_NE(dst_high, rhs_low);
8123
8124 // A_B * C_D
8125 // dst_hi: [ low(A*D) + low(B*C) + hi(B*D) ]
8126 // dst_lo: [ low(B*D) ]
8127 // Note: R2 and R6 MUL produce the low 32 bit of the multiplication result.
8128
8129 if (isR6) {
8130 __ MulR6(TMP, lhs_high, rhs_low);
8131 __ MulR6(dst_high, lhs_low, rhs_high);
8132 __ Addu(dst_high, dst_high, TMP);
8133 __ MuhuR6(TMP, lhs_low, rhs_low);
8134 __ Addu(dst_high, dst_high, TMP);
8135 __ MulR6(dst_low, lhs_low, rhs_low);
8136 } else {
8137 __ MulR2(TMP, lhs_high, rhs_low);
8138 __ MulR2(dst_high, lhs_low, rhs_high);
8139 __ Addu(dst_high, dst_high, TMP);
8140 __ MultuR2(lhs_low, rhs_low);
8141 __ Mfhi(TMP);
8142 __ Addu(dst_high, dst_high, TMP);
8143 __ Mflo(dst_low);
8144 }
8145 break;
8146 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008147 case DataType::Type::kFloat32:
8148 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008149 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
8150 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
8151 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008152 if (type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008153 __ MulS(dst, lhs, rhs);
8154 } else {
8155 __ MulD(dst, lhs, rhs);
8156 }
8157 break;
8158 }
8159 default:
8160 LOG(FATAL) << "Unexpected mul type " << type;
8161 }
8162}
8163
8164void LocationsBuilderMIPS::VisitNeg(HNeg* neg) {
8165 LocationSummary* locations =
8166 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
8167 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008168 case DataType::Type::kInt32:
8169 case DataType::Type::kInt64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008170 locations->SetInAt(0, Location::RequiresRegister());
8171 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8172 break;
8173
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008174 case DataType::Type::kFloat32:
8175 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008176 locations->SetInAt(0, Location::RequiresFpuRegister());
8177 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
8178 break;
8179
8180 default:
8181 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
8182 }
8183}
8184
8185void InstructionCodeGeneratorMIPS::VisitNeg(HNeg* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008186 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008187 LocationSummary* locations = instruction->GetLocations();
8188
8189 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008190 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008191 Register dst = locations->Out().AsRegister<Register>();
8192 Register src = locations->InAt(0).AsRegister<Register>();
8193 __ Subu(dst, ZERO, src);
8194 break;
8195 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008196 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008197 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8198 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
8199 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
8200 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
8201 __ Subu(dst_low, ZERO, src_low);
8202 __ Sltu(TMP, ZERO, dst_low);
8203 __ Subu(dst_high, ZERO, src_high);
8204 __ Subu(dst_high, dst_high, TMP);
8205 break;
8206 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008207 case DataType::Type::kFloat32:
8208 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008209 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
8210 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008211 if (type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008212 __ NegS(dst, src);
8213 } else {
8214 __ NegD(dst, src);
8215 }
8216 break;
8217 }
8218 default:
8219 LOG(FATAL) << "Unexpected neg type " << type;
8220 }
8221}
8222
8223void LocationsBuilderMIPS::VisitNewArray(HNewArray* instruction) {
8224 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01008225 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008226 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008227 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kReference));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00008228 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
8229 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008230}
8231
8232void InstructionCodeGeneratorMIPS::VisitNewArray(HNewArray* instruction) {
Alexey Frunzec061de12017-02-14 13:27:23 -08008233 // Note: if heap poisoning is enabled, the entry point takes care
8234 // of poisoning the reference.
Goran Jakovljevic854df412017-06-27 14:41:39 +02008235 QuickEntrypointEnum entrypoint =
8236 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
8237 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00008238 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Goran Jakovljevic854df412017-06-27 14:41:39 +02008239 DCHECK(!codegen_->IsLeafMethod());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008240}
8241
8242void LocationsBuilderMIPS::VisitNewInstance(HNewInstance* instruction) {
8243 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01008244 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008245 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00008246 if (instruction->IsStringAlloc()) {
8247 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
8248 } else {
8249 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00008250 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008251 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kReference));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008252}
8253
8254void InstructionCodeGeneratorMIPS::VisitNewInstance(HNewInstance* instruction) {
Alexey Frunzec061de12017-02-14 13:27:23 -08008255 // Note: if heap poisoning is enabled, the entry point takes care
8256 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00008257 if (instruction->IsStringAlloc()) {
8258 // String is allocated through StringFactory. Call NewEmptyString entry point.
8259 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07008260 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00008261 __ LoadFromOffset(kLoadWord, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
8262 __ LoadFromOffset(kLoadWord, T9, temp, code_offset.Int32Value());
8263 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07008264 __ NopIfNoReordering();
David Brazdil6de19382016-01-08 17:37:10 +00008265 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
8266 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01008267 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00008268 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00008269 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008270}
8271
8272void LocationsBuilderMIPS::VisitNot(HNot* instruction) {
8273 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
8274 locations->SetInAt(0, Location::RequiresRegister());
8275 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8276}
8277
8278void InstructionCodeGeneratorMIPS::VisitNot(HNot* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008279 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008280 LocationSummary* locations = instruction->GetLocations();
8281
8282 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008283 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008284 Register dst = locations->Out().AsRegister<Register>();
8285 Register src = locations->InAt(0).AsRegister<Register>();
8286 __ Nor(dst, src, ZERO);
8287 break;
8288 }
8289
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008290 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008291 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8292 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
8293 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
8294 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
8295 __ Nor(dst_high, src_high, ZERO);
8296 __ Nor(dst_low, src_low, ZERO);
8297 break;
8298 }
8299
8300 default:
8301 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
8302 }
8303}
8304
8305void LocationsBuilderMIPS::VisitBooleanNot(HBooleanNot* instruction) {
8306 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
8307 locations->SetInAt(0, Location::RequiresRegister());
8308 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8309}
8310
8311void InstructionCodeGeneratorMIPS::VisitBooleanNot(HBooleanNot* instruction) {
8312 LocationSummary* locations = instruction->GetLocations();
8313 __ Xori(locations->Out().AsRegister<Register>(),
8314 locations->InAt(0).AsRegister<Register>(),
8315 1);
8316}
8317
8318void LocationsBuilderMIPS::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01008319 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
8320 locations->SetInAt(0, Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008321}
8322
Calin Juravle2ae48182016-03-16 14:05:09 +00008323void CodeGeneratorMIPS::GenerateImplicitNullCheck(HNullCheck* instruction) {
8324 if (CanMoveNullCheckToUser(instruction)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008325 return;
8326 }
8327 Location obj = instruction->GetLocations()->InAt(0);
8328
8329 __ Lw(ZERO, obj.AsRegister<Register>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00008330 RecordPcInfo(instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008331}
8332
Calin Juravle2ae48182016-03-16 14:05:09 +00008333void CodeGeneratorMIPS::GenerateExplicitNullCheck(HNullCheck* instruction) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008334 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathMIPS(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00008335 AddSlowPath(slow_path);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008336
8337 Location obj = instruction->GetLocations()->InAt(0);
8338
8339 __ Beqz(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
8340}
8341
8342void InstructionCodeGeneratorMIPS::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00008343 codegen_->GenerateNullCheck(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008344}
8345
8346void LocationsBuilderMIPS::VisitOr(HOr* instruction) {
8347 HandleBinaryOp(instruction);
8348}
8349
8350void InstructionCodeGeneratorMIPS::VisitOr(HOr* instruction) {
8351 HandleBinaryOp(instruction);
8352}
8353
8354void LocationsBuilderMIPS::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
8355 LOG(FATAL) << "Unreachable";
8356}
8357
8358void InstructionCodeGeneratorMIPS::VisitParallelMove(HParallelMove* instruction) {
8359 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
8360}
8361
8362void LocationsBuilderMIPS::VisitParameterValue(HParameterValue* instruction) {
8363 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
8364 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
8365 if (location.IsStackSlot()) {
8366 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
8367 } else if (location.IsDoubleStackSlot()) {
8368 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
8369 }
8370 locations->SetOut(location);
8371}
8372
8373void InstructionCodeGeneratorMIPS::VisitParameterValue(HParameterValue* instruction
8374 ATTRIBUTE_UNUSED) {
8375 // Nothing to do, the parameter is already at its location.
8376}
8377
8378void LocationsBuilderMIPS::VisitCurrentMethod(HCurrentMethod* instruction) {
8379 LocationSummary* locations =
8380 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
8381 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
8382}
8383
8384void InstructionCodeGeneratorMIPS::VisitCurrentMethod(HCurrentMethod* instruction
8385 ATTRIBUTE_UNUSED) {
8386 // Nothing to do, the method is already at its location.
8387}
8388
8389void LocationsBuilderMIPS::VisitPhi(HPhi* instruction) {
8390 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01008391 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008392 locations->SetInAt(i, Location::Any());
8393 }
8394 locations->SetOut(Location::Any());
8395}
8396
8397void InstructionCodeGeneratorMIPS::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
8398 LOG(FATAL) << "Unreachable";
8399}
8400
8401void LocationsBuilderMIPS::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008402 DataType::Type type = rem->GetResultType();
8403 LocationSummary::CallKind call_kind = (type == DataType::Type::kInt32)
8404 ? LocationSummary::kNoCall
8405 : LocationSummary::kCallOnMainOnly;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008406 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
8407
8408 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008409 case DataType::Type::kInt32:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008410 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08008411 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008412 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8413 break;
8414
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008415 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008416 InvokeRuntimeCallingConvention calling_convention;
8417 locations->SetInAt(0, Location::RegisterPairLocation(
8418 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
8419 locations->SetInAt(1, Location::RegisterPairLocation(
8420 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
8421 locations->SetOut(calling_convention.GetReturnLocation(type));
8422 break;
8423 }
8424
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008425 case DataType::Type::kFloat32:
8426 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008427 InvokeRuntimeCallingConvention calling_convention;
8428 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
8429 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
8430 locations->SetOut(calling_convention.GetReturnLocation(type));
8431 break;
8432 }
8433
8434 default:
8435 LOG(FATAL) << "Unexpected rem type " << type;
8436 }
8437}
8438
8439void InstructionCodeGeneratorMIPS::VisitRem(HRem* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008440 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008441
8442 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008443 case DataType::Type::kInt32:
Alexey Frunze7e99e052015-11-24 19:28:01 -08008444 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008445 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008446 case DataType::Type::kInt64: {
Serban Constantinescufca16662016-07-14 09:21:59 +01008447 codegen_->InvokeRuntime(kQuickLmod, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008448 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
8449 break;
8450 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008451 case DataType::Type::kFloat32: {
Serban Constantinescufca16662016-07-14 09:21:59 +01008452 codegen_->InvokeRuntime(kQuickFmodf, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00008453 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008454 break;
8455 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008456 case DataType::Type::kFloat64: {
Serban Constantinescufca16662016-07-14 09:21:59 +01008457 codegen_->InvokeRuntime(kQuickFmod, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00008458 CheckEntrypointTypes<kQuickFmod, double, double, double>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008459 break;
8460 }
8461 default:
8462 LOG(FATAL) << "Unexpected rem type " << type;
8463 }
8464}
8465
Igor Murashkind01745e2017-04-05 16:40:31 -07008466void LocationsBuilderMIPS::VisitConstructorFence(HConstructorFence* constructor_fence) {
8467 constructor_fence->SetLocations(nullptr);
8468}
8469
8470void InstructionCodeGeneratorMIPS::VisitConstructorFence(
8471 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
8472 GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
8473}
8474
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008475void LocationsBuilderMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
8476 memory_barrier->SetLocations(nullptr);
8477}
8478
8479void InstructionCodeGeneratorMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
8480 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
8481}
8482
8483void LocationsBuilderMIPS::VisitReturn(HReturn* ret) {
8484 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(ret);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008485 DataType::Type return_type = ret->InputAt(0)->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008486 locations->SetInAt(0, MipsReturnLocation(return_type));
8487}
8488
8489void InstructionCodeGeneratorMIPS::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
8490 codegen_->GenerateFrameExit();
8491}
8492
8493void LocationsBuilderMIPS::VisitReturnVoid(HReturnVoid* ret) {
8494 ret->SetLocations(nullptr);
8495}
8496
8497void InstructionCodeGeneratorMIPS::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
8498 codegen_->GenerateFrameExit();
8499}
8500
Alexey Frunze92d90602015-12-18 18:16:36 -08008501void LocationsBuilderMIPS::VisitRor(HRor* ror) {
8502 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00008503}
8504
Alexey Frunze92d90602015-12-18 18:16:36 -08008505void InstructionCodeGeneratorMIPS::VisitRor(HRor* ror) {
8506 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00008507}
8508
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008509void LocationsBuilderMIPS::VisitShl(HShl* shl) {
8510 HandleShift(shl);
8511}
8512
8513void InstructionCodeGeneratorMIPS::VisitShl(HShl* shl) {
8514 HandleShift(shl);
8515}
8516
8517void LocationsBuilderMIPS::VisitShr(HShr* shr) {
8518 HandleShift(shr);
8519}
8520
8521void InstructionCodeGeneratorMIPS::VisitShr(HShr* shr) {
8522 HandleShift(shr);
8523}
8524
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008525void LocationsBuilderMIPS::VisitSub(HSub* instruction) {
8526 HandleBinaryOp(instruction);
8527}
8528
8529void InstructionCodeGeneratorMIPS::VisitSub(HSub* instruction) {
8530 HandleBinaryOp(instruction);
8531}
8532
8533void LocationsBuilderMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
8534 HandleFieldGet(instruction, instruction->GetFieldInfo());
8535}
8536
8537void InstructionCodeGeneratorMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
8538 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
8539}
8540
8541void LocationsBuilderMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
8542 HandleFieldSet(instruction, instruction->GetFieldInfo());
8543}
8544
8545void InstructionCodeGeneratorMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Goran Jakovljevice114da22016-12-26 14:21:43 +01008546 HandleFieldSet(instruction,
8547 instruction->GetFieldInfo(),
8548 instruction->GetDexPc(),
8549 instruction->GetValueCanBeNull());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008550}
8551
8552void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldGet(
8553 HUnresolvedInstanceFieldGet* instruction) {
8554 FieldAccessCallingConventionMIPS calling_convention;
8555 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
8556 instruction->GetFieldType(),
8557 calling_convention);
8558}
8559
8560void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldGet(
8561 HUnresolvedInstanceFieldGet* instruction) {
8562 FieldAccessCallingConventionMIPS calling_convention;
8563 codegen_->GenerateUnresolvedFieldAccess(instruction,
8564 instruction->GetFieldType(),
8565 instruction->GetFieldIndex(),
8566 instruction->GetDexPc(),
8567 calling_convention);
8568}
8569
8570void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldSet(
8571 HUnresolvedInstanceFieldSet* instruction) {
8572 FieldAccessCallingConventionMIPS calling_convention;
8573 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
8574 instruction->GetFieldType(),
8575 calling_convention);
8576}
8577
8578void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldSet(
8579 HUnresolvedInstanceFieldSet* instruction) {
8580 FieldAccessCallingConventionMIPS calling_convention;
8581 codegen_->GenerateUnresolvedFieldAccess(instruction,
8582 instruction->GetFieldType(),
8583 instruction->GetFieldIndex(),
8584 instruction->GetDexPc(),
8585 calling_convention);
8586}
8587
8588void LocationsBuilderMIPS::VisitUnresolvedStaticFieldGet(
8589 HUnresolvedStaticFieldGet* instruction) {
8590 FieldAccessCallingConventionMIPS calling_convention;
8591 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
8592 instruction->GetFieldType(),
8593 calling_convention);
8594}
8595
8596void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldGet(
8597 HUnresolvedStaticFieldGet* instruction) {
8598 FieldAccessCallingConventionMIPS calling_convention;
8599 codegen_->GenerateUnresolvedFieldAccess(instruction,
8600 instruction->GetFieldType(),
8601 instruction->GetFieldIndex(),
8602 instruction->GetDexPc(),
8603 calling_convention);
8604}
8605
8606void LocationsBuilderMIPS::VisitUnresolvedStaticFieldSet(
8607 HUnresolvedStaticFieldSet* instruction) {
8608 FieldAccessCallingConventionMIPS calling_convention;
8609 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
8610 instruction->GetFieldType(),
8611 calling_convention);
8612}
8613
8614void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldSet(
8615 HUnresolvedStaticFieldSet* instruction) {
8616 FieldAccessCallingConventionMIPS calling_convention;
8617 codegen_->GenerateUnresolvedFieldAccess(instruction,
8618 instruction->GetFieldType(),
8619 instruction->GetFieldIndex(),
8620 instruction->GetDexPc(),
8621 calling_convention);
8622}
8623
8624void LocationsBuilderMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01008625 LocationSummary* locations =
8626 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Lena Djokicca8c2952017-05-29 11:31:46 +02008627 // In suspend check slow path, usually there are no caller-save registers at all.
8628 // If SIMD instructions are present, however, we force spilling all live SIMD
8629 // registers in full width (since the runtime only saves/restores lower part).
8630 locations->SetCustomSlowPathCallerSaves(
8631 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008632}
8633
8634void InstructionCodeGeneratorMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
8635 HBasicBlock* block = instruction->GetBlock();
8636 if (block->GetLoopInformation() != nullptr) {
8637 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
8638 // The back edge will generate the suspend check.
8639 return;
8640 }
8641 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
8642 // The goto will generate the suspend check.
8643 return;
8644 }
8645 GenerateSuspendCheck(instruction, nullptr);
8646}
8647
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008648void LocationsBuilderMIPS::VisitThrow(HThrow* instruction) {
8649 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01008650 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008651 InvokeRuntimeCallingConvention calling_convention;
8652 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
8653}
8654
8655void InstructionCodeGeneratorMIPS::VisitThrow(HThrow* instruction) {
Serban Constantinescufca16662016-07-14 09:21:59 +01008656 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008657 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
8658}
8659
8660void LocationsBuilderMIPS::VisitTypeConversion(HTypeConversion* conversion) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008661 DataType::Type input_type = conversion->GetInputType();
8662 DataType::Type result_type = conversion->GetResultType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01008663 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
8664 << input_type << " -> " << result_type;
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008665 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008666
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008667 if ((input_type == DataType::Type::kReference) || (input_type == DataType::Type::kVoid) ||
8668 (result_type == DataType::Type::kReference) || (result_type == DataType::Type::kVoid)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008669 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
8670 }
8671
8672 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008673 if (!isR6 &&
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008674 ((DataType::IsFloatingPointType(result_type) && input_type == DataType::Type::kInt64) ||
8675 (result_type == DataType::Type::kInt64 && DataType::IsFloatingPointType(input_type)))) {
Serban Constantinescu54ff4822016-07-07 18:03:19 +01008676 call_kind = LocationSummary::kCallOnMainOnly;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008677 }
8678
8679 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
8680
8681 if (call_kind == LocationSummary::kNoCall) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008682 if (DataType::IsFloatingPointType(input_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008683 locations->SetInAt(0, Location::RequiresFpuRegister());
8684 } else {
8685 locations->SetInAt(0, Location::RequiresRegister());
8686 }
8687
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008688 if (DataType::IsFloatingPointType(result_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008689 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
8690 } else {
8691 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8692 }
8693 } else {
8694 InvokeRuntimeCallingConvention calling_convention;
8695
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008696 if (DataType::IsFloatingPointType(input_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008697 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
8698 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008699 DCHECK_EQ(input_type, DataType::Type::kInt64);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008700 locations->SetInAt(0, Location::RegisterPairLocation(
8701 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
8702 }
8703
8704 locations->SetOut(calling_convention.GetReturnLocation(result_type));
8705 }
8706}
8707
8708void InstructionCodeGeneratorMIPS::VisitTypeConversion(HTypeConversion* conversion) {
8709 LocationSummary* locations = conversion->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008710 DataType::Type result_type = conversion->GetResultType();
8711 DataType::Type input_type = conversion->GetInputType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008712 bool has_sign_extension = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008713 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008714
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01008715 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
8716 << input_type << " -> " << result_type;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008717
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008718 if (result_type == DataType::Type::kInt64 && DataType::IsIntegralType(input_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008719 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8720 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
8721 Register src = locations->InAt(0).AsRegister<Register>();
8722
Alexey Frunzea871ef12016-06-27 15:20:11 -07008723 if (dst_low != src) {
8724 __ Move(dst_low, src);
8725 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008726 __ Sra(dst_high, src, 31);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008727 } else if (DataType::IsIntegralType(result_type) && DataType::IsIntegralType(input_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008728 Register dst = locations->Out().AsRegister<Register>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008729 Register src = (input_type == DataType::Type::kInt64)
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008730 ? locations->InAt(0).AsRegisterPairLow<Register>()
8731 : locations->InAt(0).AsRegister<Register>();
8732
8733 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01008734 case DataType::Type::kUint8:
8735 __ Andi(dst, src, 0xFF);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008736 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008737 case DataType::Type::kInt8:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008738 if (has_sign_extension) {
8739 __ Seb(dst, src);
8740 } else {
8741 __ Sll(dst, src, 24);
8742 __ Sra(dst, dst, 24);
8743 }
8744 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01008745 case DataType::Type::kUint16:
8746 __ Andi(dst, src, 0xFFFF);
8747 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008748 case DataType::Type::kInt16:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008749 if (has_sign_extension) {
8750 __ Seh(dst, src);
8751 } else {
8752 __ Sll(dst, src, 16);
8753 __ Sra(dst, dst, 16);
8754 }
8755 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008756 case DataType::Type::kInt32:
Alexey Frunzea871ef12016-06-27 15:20:11 -07008757 if (dst != src) {
8758 __ Move(dst, src);
8759 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008760 break;
8761
8762 default:
8763 LOG(FATAL) << "Unexpected type conversion from " << input_type
8764 << " to " << result_type;
8765 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008766 } else if (DataType::IsFloatingPointType(result_type) && DataType::IsIntegralType(input_type)) {
8767 if (input_type == DataType::Type::kInt64) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008768 if (isR6) {
8769 // cvt.s.l/cvt.d.l requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
8770 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
8771 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
8772 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
8773 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
8774 __ Mtc1(src_low, FTMP);
8775 __ Mthc1(src_high, FTMP);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008776 if (result_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008777 __ Cvtsl(dst, FTMP);
8778 } else {
8779 __ Cvtdl(dst, FTMP);
8780 }
8781 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008782 QuickEntrypointEnum entrypoint =
8783 (result_type == DataType::Type::kFloat32) ? kQuickL2f : kQuickL2d;
Serban Constantinescufca16662016-07-14 09:21:59 +01008784 codegen_->InvokeRuntime(entrypoint, conversion, conversion->GetDexPc());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008785 if (result_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008786 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
8787 } else {
8788 CheckEntrypointTypes<kQuickL2d, double, int64_t>();
8789 }
8790 }
8791 } else {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008792 Register src = locations->InAt(0).AsRegister<Register>();
8793 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
8794 __ Mtc1(src, FTMP);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008795 if (result_type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008796 __ Cvtsw(dst, FTMP);
8797 } else {
8798 __ Cvtdw(dst, FTMP);
8799 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008800 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008801 } else if (DataType::IsIntegralType(result_type) && DataType::IsFloatingPointType(input_type)) {
8802 CHECK(result_type == DataType::Type::kInt32 || result_type == DataType::Type::kInt64);
Lena Djokicf4e23a82017-05-09 15:43:45 +02008803
8804 // When NAN2008=1 (R6), the truncate instruction caps the output at the minimum/maximum
8805 // value of the output type if the input is outside of the range after the truncation or
8806 // produces 0 when the input is a NaN. IOW, the three special cases produce three distinct
8807 // results. This matches the desired float/double-to-int/long conversion exactly.
8808 //
8809 // When NAN2008=0 (R2 and before), the truncate instruction produces the maximum positive
8810 // value when the input is either a NaN or is outside of the range of the output type
8811 // after the truncation. IOW, the three special cases (NaN, too small, too big) produce
8812 // the same result.
8813 //
8814 // The code takes care of the different behaviors by first comparing the input to the
8815 // minimum output value (-2**-63 for truncating to long, -2**-31 for truncating to int).
8816 // If the input is greater than or equal to the minimum, it procedes to the truncate
8817 // instruction, which will handle such an input the same way irrespective of NAN2008.
8818 // Otherwise the input is compared to itself to determine whether it is a NaN or not
8819 // in order to return either zero or the minimum value.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008820 if (result_type == DataType::Type::kInt64) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008821 if (isR6) {
8822 // trunc.l.s/trunc.l.d requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
8823 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
8824 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
8825 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8826 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008827
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008828 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008829 __ TruncLS(FTMP, src);
8830 } else {
8831 __ TruncLD(FTMP, src);
8832 }
8833 __ Mfc1(dst_low, FTMP);
8834 __ Mfhc1(dst_high, FTMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008835 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008836 QuickEntrypointEnum entrypoint =
8837 (input_type == DataType::Type::kFloat32) ? kQuickF2l : kQuickD2l;
Serban Constantinescufca16662016-07-14 09:21:59 +01008838 codegen_->InvokeRuntime(entrypoint, conversion, conversion->GetDexPc());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008839 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008840 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
8841 } else {
8842 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
8843 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008844 }
8845 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008846 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
8847 Register dst = locations->Out().AsRegister<Register>();
8848 MipsLabel truncate;
8849 MipsLabel done;
8850
Lena Djokicf4e23a82017-05-09 15:43:45 +02008851 if (!isR6) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008852 if (input_type == DataType::Type::kFloat32) {
Lena Djokicf4e23a82017-05-09 15:43:45 +02008853 uint32_t min_val = bit_cast<uint32_t, float>(std::numeric_limits<int32_t>::min());
8854 __ LoadConst32(TMP, min_val);
8855 __ Mtc1(TMP, FTMP);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008856 } else {
Lena Djokicf4e23a82017-05-09 15:43:45 +02008857 uint64_t min_val = bit_cast<uint64_t, double>(std::numeric_limits<int32_t>::min());
8858 __ LoadConst32(TMP, High32Bits(min_val));
8859 __ Mtc1(ZERO, FTMP);
8860 __ MoveToFpuHigh(TMP, FTMP);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008861 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008862
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008863 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008864 __ ColeS(0, FTMP, src);
8865 } else {
8866 __ ColeD(0, FTMP, src);
8867 }
8868 __ Bc1t(0, &truncate);
8869
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008870 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008871 __ CeqS(0, src, src);
8872 } else {
8873 __ CeqD(0, src, src);
8874 }
8875 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
8876 __ Movf(dst, ZERO, 0);
Lena Djokicf4e23a82017-05-09 15:43:45 +02008877
8878 __ B(&done);
8879
8880 __ Bind(&truncate);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008881 }
8882
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008883 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008884 __ TruncWS(FTMP, src);
8885 } else {
8886 __ TruncWD(FTMP, src);
8887 }
8888 __ Mfc1(dst, FTMP);
8889
Lena Djokicf4e23a82017-05-09 15:43:45 +02008890 if (!isR6) {
8891 __ Bind(&done);
8892 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008893 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008894 } else if (DataType::IsFloatingPointType(result_type) &&
8895 DataType::IsFloatingPointType(input_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008896 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
8897 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008898 if (result_type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008899 __ Cvtsd(dst, src);
8900 } else {
8901 __ Cvtds(dst, src);
8902 }
8903 } else {
8904 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
8905 << " to " << result_type;
8906 }
8907}
8908
8909void LocationsBuilderMIPS::VisitUShr(HUShr* ushr) {
8910 HandleShift(ushr);
8911}
8912
8913void InstructionCodeGeneratorMIPS::VisitUShr(HUShr* ushr) {
8914 HandleShift(ushr);
8915}
8916
8917void LocationsBuilderMIPS::VisitXor(HXor* instruction) {
8918 HandleBinaryOp(instruction);
8919}
8920
8921void InstructionCodeGeneratorMIPS::VisitXor(HXor* instruction) {
8922 HandleBinaryOp(instruction);
8923}
8924
8925void LocationsBuilderMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
8926 // Nothing to do, this should be removed during prepare for register allocator.
8927 LOG(FATAL) << "Unreachable";
8928}
8929
8930void InstructionCodeGeneratorMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
8931 // Nothing to do, this should be removed during prepare for register allocator.
8932 LOG(FATAL) << "Unreachable";
8933}
8934
8935void LocationsBuilderMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008936 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008937}
8938
8939void InstructionCodeGeneratorMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008940 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008941}
8942
8943void LocationsBuilderMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008944 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008945}
8946
8947void InstructionCodeGeneratorMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008948 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008949}
8950
8951void LocationsBuilderMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008952 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008953}
8954
8955void InstructionCodeGeneratorMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008956 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008957}
8958
8959void LocationsBuilderMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008960 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008961}
8962
8963void InstructionCodeGeneratorMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008964 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008965}
8966
8967void LocationsBuilderMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008968 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008969}
8970
8971void InstructionCodeGeneratorMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008972 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008973}
8974
8975void LocationsBuilderMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008976 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008977}
8978
8979void InstructionCodeGeneratorMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008980 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008981}
8982
8983void LocationsBuilderMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008984 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008985}
8986
8987void InstructionCodeGeneratorMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008988 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008989}
8990
8991void LocationsBuilderMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008992 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008993}
8994
8995void InstructionCodeGeneratorMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008996 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008997}
8998
8999void LocationsBuilderMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009000 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009001}
9002
9003void InstructionCodeGeneratorMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009004 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009005}
9006
9007void LocationsBuilderMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009008 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009009}
9010
9011void InstructionCodeGeneratorMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009012 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009013}
9014
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009015void LocationsBuilderMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
9016 LocationSummary* locations =
9017 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
9018 locations->SetInAt(0, Location::RequiresRegister());
9019}
9020
Alexey Frunze96b66822016-09-10 02:32:44 -07009021void InstructionCodeGeneratorMIPS::GenPackedSwitchWithCompares(Register value_reg,
9022 int32_t lower_bound,
9023 uint32_t num_entries,
9024 HBasicBlock* switch_block,
9025 HBasicBlock* default_block) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009026 // Create a set of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00009027 Register temp_reg = TMP;
9028 __ Addiu32(temp_reg, value_reg, -lower_bound);
9029 // Jump to default if index is negative
9030 // Note: We don't check the case that index is positive while value < lower_bound, because in
9031 // this case, index >= num_entries must be true. So that we can save one branch instruction.
9032 __ Bltz(temp_reg, codegen_->GetLabelOf(default_block));
9033
Alexey Frunze96b66822016-09-10 02:32:44 -07009034 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00009035 // Jump to successors[0] if value == lower_bound.
9036 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[0]));
9037 int32_t last_index = 0;
9038 for (; num_entries - last_index > 2; last_index += 2) {
9039 __ Addiu(temp_reg, temp_reg, -2);
9040 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
9041 __ Bltz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
9042 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
9043 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 2]));
9044 }
9045 if (num_entries - last_index == 2) {
9046 // The last missing case_value.
9047 __ Addiu(temp_reg, temp_reg, -1);
9048 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009049 }
9050
Vladimir Markof3e0ee22015-12-17 15:23:13 +00009051 // And the default for any other value.
Alexey Frunze96b66822016-09-10 02:32:44 -07009052 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009053 __ B(codegen_->GetLabelOf(default_block));
9054 }
9055}
9056
Alexey Frunze96b66822016-09-10 02:32:44 -07009057void InstructionCodeGeneratorMIPS::GenTableBasedPackedSwitch(Register value_reg,
9058 Register constant_area,
9059 int32_t lower_bound,
9060 uint32_t num_entries,
9061 HBasicBlock* switch_block,
9062 HBasicBlock* default_block) {
9063 // Create a jump table.
9064 std::vector<MipsLabel*> labels(num_entries);
9065 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
9066 for (uint32_t i = 0; i < num_entries; i++) {
9067 labels[i] = codegen_->GetLabelOf(successors[i]);
9068 }
9069 JumpTable* table = __ CreateJumpTable(std::move(labels));
9070
9071 // Is the value in range?
9072 __ Addiu32(TMP, value_reg, -lower_bound);
9073 if (IsInt<16>(static_cast<int32_t>(num_entries))) {
9074 __ Sltiu(AT, TMP, num_entries);
9075 __ Beqz(AT, codegen_->GetLabelOf(default_block));
9076 } else {
9077 __ LoadConst32(AT, num_entries);
9078 __ Bgeu(TMP, AT, codegen_->GetLabelOf(default_block));
9079 }
9080
9081 // We are in the range of the table.
9082 // Load the target address from the jump table, indexing by the value.
9083 __ LoadLabelAddress(AT, constant_area, table->GetLabel());
Chris Larsencd0295d2017-03-31 15:26:54 -07009084 __ ShiftAndAdd(TMP, TMP, AT, 2, TMP);
Alexey Frunze96b66822016-09-10 02:32:44 -07009085 __ Lw(TMP, TMP, 0);
9086 // Compute the absolute target address by adding the table start address
9087 // (the table contains offsets to targets relative to its start).
9088 __ Addu(TMP, TMP, AT);
9089 // And jump.
9090 __ Jr(TMP);
9091 __ NopIfNoReordering();
9092}
9093
9094void InstructionCodeGeneratorMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
9095 int32_t lower_bound = switch_instr->GetStartValue();
9096 uint32_t num_entries = switch_instr->GetNumEntries();
9097 LocationSummary* locations = switch_instr->GetLocations();
9098 Register value_reg = locations->InAt(0).AsRegister<Register>();
9099 HBasicBlock* switch_block = switch_instr->GetBlock();
9100 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
9101
9102 if (codegen_->GetInstructionSetFeatures().IsR6() &&
9103 num_entries > kPackedSwitchJumpTableThreshold) {
9104 // R6 uses PC-relative addressing to access the jump table.
9105 // R2, OTOH, requires an HMipsComputeBaseMethodAddress input to access
9106 // the jump table and it is implemented by changing HPackedSwitch to
9107 // HMipsPackedSwitch, which bears HMipsComputeBaseMethodAddress.
9108 // See VisitMipsPackedSwitch() for the table-based implementation on R2.
9109 GenTableBasedPackedSwitch(value_reg,
9110 ZERO,
9111 lower_bound,
9112 num_entries,
9113 switch_block,
9114 default_block);
9115 } else {
9116 GenPackedSwitchWithCompares(value_reg,
9117 lower_bound,
9118 num_entries,
9119 switch_block,
9120 default_block);
9121 }
9122}
9123
9124void LocationsBuilderMIPS::VisitMipsPackedSwitch(HMipsPackedSwitch* switch_instr) {
9125 LocationSummary* locations =
9126 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
9127 locations->SetInAt(0, Location::RequiresRegister());
9128 // Constant area pointer (HMipsComputeBaseMethodAddress).
9129 locations->SetInAt(1, Location::RequiresRegister());
9130}
9131
9132void InstructionCodeGeneratorMIPS::VisitMipsPackedSwitch(HMipsPackedSwitch* switch_instr) {
9133 int32_t lower_bound = switch_instr->GetStartValue();
9134 uint32_t num_entries = switch_instr->GetNumEntries();
9135 LocationSummary* locations = switch_instr->GetLocations();
9136 Register value_reg = locations->InAt(0).AsRegister<Register>();
9137 Register constant_area = locations->InAt(1).AsRegister<Register>();
9138 HBasicBlock* switch_block = switch_instr->GetBlock();
9139 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
9140
9141 // This is an R2-only path. HPackedSwitch has been changed to
9142 // HMipsPackedSwitch, which bears HMipsComputeBaseMethodAddress
9143 // required to address the jump table relative to PC.
9144 GenTableBasedPackedSwitch(value_reg,
9145 constant_area,
9146 lower_bound,
9147 num_entries,
9148 switch_block,
9149 default_block);
9150}
9151
Alexey Frunzee3fb2452016-05-10 16:08:05 -07009152void LocationsBuilderMIPS::VisitMipsComputeBaseMethodAddress(
9153 HMipsComputeBaseMethodAddress* insn) {
9154 LocationSummary* locations =
9155 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
9156 locations->SetOut(Location::RequiresRegister());
9157}
9158
9159void InstructionCodeGeneratorMIPS::VisitMipsComputeBaseMethodAddress(
9160 HMipsComputeBaseMethodAddress* insn) {
9161 LocationSummary* locations = insn->GetLocations();
9162 Register reg = locations->Out().AsRegister<Register>();
9163
9164 CHECK(!codegen_->GetInstructionSetFeatures().IsR6());
9165
9166 // Generate a dummy PC-relative call to obtain PC.
9167 __ Nal();
9168 // Grab the return address off RA.
9169 __ Move(reg, RA);
9170
9171 // Remember this offset (the obtained PC value) for later use with constant area.
9172 __ BindPcRelBaseLabel();
9173}
9174
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009175void LocationsBuilderMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
9176 // The trampoline uses the same calling convention as dex calling conventions,
9177 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
9178 // the method_idx.
9179 HandleInvoke(invoke);
9180}
9181
9182void InstructionCodeGeneratorMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
9183 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
9184}
9185
Roland Levillain2aba7cd2016-02-03 12:27:20 +00009186void LocationsBuilderMIPS::VisitClassTableGet(HClassTableGet* instruction) {
9187 LocationSummary* locations =
9188 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
9189 locations->SetInAt(0, Location::RequiresRegister());
9190 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00009191}
9192
Roland Levillain2aba7cd2016-02-03 12:27:20 +00009193void InstructionCodeGeneratorMIPS::VisitClassTableGet(HClassTableGet* instruction) {
9194 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00009195 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01009196 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Roland Levillain2aba7cd2016-02-03 12:27:20 +00009197 instruction->GetIndex(), kMipsPointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01009198 __ LoadFromOffset(kLoadWord,
9199 locations->Out().AsRegister<Register>(),
9200 locations->InAt(0).AsRegister<Register>(),
9201 method_offset);
Roland Levillain2aba7cd2016-02-03 12:27:20 +00009202 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01009203 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00009204 instruction->GetIndex(), kMipsPointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00009205 __ LoadFromOffset(kLoadWord,
9206 locations->Out().AsRegister<Register>(),
9207 locations->InAt(0).AsRegister<Register>(),
9208 mirror::Class::ImtPtrOffset(kMipsPointerSize).Uint32Value());
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01009209 __ LoadFromOffset(kLoadWord,
9210 locations->Out().AsRegister<Register>(),
9211 locations->Out().AsRegister<Register>(),
9212 method_offset);
Roland Levillain2aba7cd2016-02-03 12:27:20 +00009213 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00009214}
9215
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009216#undef __
9217#undef QUICK_ENTRY_POINT
9218
9219} // namespace mips
9220} // namespace art