blob: 56df6b5289bc0676101040193fa1e544a0713bbd [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;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100560 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Alexey Frunze15958152017-02-09 19:08:30 -0800561 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;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100971 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Alexey Frunze15958152017-02-09 19:08:30 -0800972 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),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001103 move_resolver_(graph->GetAllocator(), this),
1104 assembler_(graph->GetAllocator(), &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>(),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001107 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1108 pc_relative_method_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1109 method_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1110 pc_relative_type_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1111 type_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1112 pc_relative_string_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1113 string_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1114 jit_string_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1115 jit_class_patches_(graph->GetAllocator()->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 =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002001 new (GetGraph()->GetAllocator()) SuspendCheckSlowPathMIPS(instruction, successor);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002002 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);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002026 LocationSummary* locations = new (GetGraph()->GetAllocator()) 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
Vladimir Markoca6fff82017-10-03 14:49:14 +01002292 LocationSummary* locations = new (GetGraph()->GetAllocator()) 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 =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002545 new (GetGraph()->GetAllocator()) 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) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002827 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002828 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
Vladimir Markoca6fff82017-10-03 14:49:14 +01002871 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002872 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) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002989 slow_path = new (GetGraph()->GetAllocator()) ArraySetSlowPathMIPS(instruction);
Alexey Frunze15958152017-02-09 19:08:30 -08002990 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 =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003144 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Lena Djokica2901602017-09-21 13:50:52 +02003145
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 =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003174 new (GetGraph()->GetAllocator()) BoundsCheckSlowPathMIPS(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003175 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
Vladimir Markoca6fff82017-10-03 14:49:14 +01003225 LocationSummary* locations =
3226 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003227 locations->SetInAt(0, Location::RequiresRegister());
3228 locations->SetInAt(1, Location::RequiresRegister());
Alexey Frunze15958152017-02-09 19:08:30 -08003229 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003230}
3231
3232void InstructionCodeGeneratorMIPS::VisitCheckCast(HCheckCast* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003233 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003234 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08003235 Location obj_loc = locations->InAt(0);
3236 Register obj = obj_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003237 Register cls = locations->InAt(1).AsRegister<Register>();
Alexey Frunze15958152017-02-09 19:08:30 -08003238 Location temp_loc = locations->GetTemp(0);
3239 Register temp = temp_loc.AsRegister<Register>();
3240 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
3241 DCHECK_LE(num_temps, 2u);
3242 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003243 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3244 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3245 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3246 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
3247 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
3248 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
3249 const uint32_t object_array_data_offset =
3250 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
3251 MipsLabel done;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003252
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003253 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
3254 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
3255 // read barriers is done for performance and code size reasons.
3256 bool is_type_check_slow_path_fatal = false;
3257 if (!kEmitCompilerReadBarrier) {
3258 is_type_check_slow_path_fatal =
3259 (type_check_kind == TypeCheckKind::kExactCheck ||
3260 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3261 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3262 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
3263 !instruction->CanThrowIntoCatchBlock();
3264 }
3265 SlowPathCodeMIPS* slow_path =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003266 new (GetGraph()->GetAllocator()) TypeCheckSlowPathMIPS(instruction,
3267 is_type_check_slow_path_fatal);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003268 codegen_->AddSlowPath(slow_path);
3269
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003270 // Avoid this check if we know `obj` is not null.
3271 if (instruction->MustDoNullCheck()) {
3272 __ Beqz(obj, &done);
3273 }
3274
3275 switch (type_check_kind) {
3276 case TypeCheckKind::kExactCheck:
3277 case TypeCheckKind::kArrayCheck: {
3278 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003279 GenerateReferenceLoadTwoRegisters(instruction,
3280 temp_loc,
3281 obj_loc,
3282 class_offset,
3283 maybe_temp2_loc,
3284 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003285 // Jump to slow path for throwing the exception or doing a
3286 // more involved array check.
3287 __ Bne(temp, cls, slow_path->GetEntryLabel());
3288 break;
3289 }
3290
3291 case TypeCheckKind::kAbstractClassCheck: {
3292 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003293 GenerateReferenceLoadTwoRegisters(instruction,
3294 temp_loc,
3295 obj_loc,
3296 class_offset,
3297 maybe_temp2_loc,
3298 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003299 // If the class is abstract, we eagerly fetch the super class of the
3300 // object to avoid doing a comparison we know will fail.
3301 MipsLabel loop;
3302 __ Bind(&loop);
3303 // /* HeapReference<Class> */ temp = temp->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08003304 GenerateReferenceLoadOneRegister(instruction,
3305 temp_loc,
3306 super_offset,
3307 maybe_temp2_loc,
3308 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003309 // If the class reference currently in `temp` is null, jump to the slow path to throw the
3310 // exception.
3311 __ Beqz(temp, slow_path->GetEntryLabel());
3312 // Otherwise, compare the classes.
3313 __ Bne(temp, cls, &loop);
3314 break;
3315 }
3316
3317 case TypeCheckKind::kClassHierarchyCheck: {
3318 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003319 GenerateReferenceLoadTwoRegisters(instruction,
3320 temp_loc,
3321 obj_loc,
3322 class_offset,
3323 maybe_temp2_loc,
3324 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003325 // Walk over the class hierarchy to find a match.
3326 MipsLabel loop;
3327 __ Bind(&loop);
3328 __ Beq(temp, cls, &done);
3329 // /* HeapReference<Class> */ temp = temp->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08003330 GenerateReferenceLoadOneRegister(instruction,
3331 temp_loc,
3332 super_offset,
3333 maybe_temp2_loc,
3334 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003335 // If the class reference currently in `temp` is null, jump to the slow path to throw the
3336 // exception. Otherwise, jump to the beginning of the loop.
3337 __ Bnez(temp, &loop);
3338 __ B(slow_path->GetEntryLabel());
3339 break;
3340 }
3341
3342 case TypeCheckKind::kArrayObjectCheck: {
3343 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003344 GenerateReferenceLoadTwoRegisters(instruction,
3345 temp_loc,
3346 obj_loc,
3347 class_offset,
3348 maybe_temp2_loc,
3349 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003350 // Do an exact check.
3351 __ Beq(temp, cls, &done);
3352 // Otherwise, we need to check that the object's class is a non-primitive array.
3353 // /* HeapReference<Class> */ temp = temp->component_type_
Alexey Frunze15958152017-02-09 19:08:30 -08003354 GenerateReferenceLoadOneRegister(instruction,
3355 temp_loc,
3356 component_offset,
3357 maybe_temp2_loc,
3358 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003359 // If the component type is null, jump to the slow path to throw the exception.
3360 __ Beqz(temp, slow_path->GetEntryLabel());
3361 // Otherwise, the object is indeed an array, further check that this component
3362 // type is not a primitive type.
3363 __ LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
3364 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
3365 __ Bnez(temp, slow_path->GetEntryLabel());
3366 break;
3367 }
3368
3369 case TypeCheckKind::kUnresolvedCheck:
3370 // We always go into the type check slow path for the unresolved check case.
3371 // We cannot directly call the CheckCast runtime entry point
3372 // without resorting to a type checking slow path here (i.e. by
3373 // calling InvokeRuntime directly), as it would require to
3374 // assign fixed registers for the inputs of this HInstanceOf
3375 // instruction (following the runtime calling convention), which
3376 // might be cluttered by the potential first read barrier
3377 // emission at the beginning of this method.
3378 __ B(slow_path->GetEntryLabel());
3379 break;
3380
3381 case TypeCheckKind::kInterfaceCheck: {
3382 // Avoid read barriers to improve performance of the fast path. We can not get false
3383 // positives by doing this.
3384 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003385 GenerateReferenceLoadTwoRegisters(instruction,
3386 temp_loc,
3387 obj_loc,
3388 class_offset,
3389 maybe_temp2_loc,
3390 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003391 // /* HeapReference<Class> */ temp = temp->iftable_
Alexey Frunze15958152017-02-09 19:08:30 -08003392 GenerateReferenceLoadTwoRegisters(instruction,
3393 temp_loc,
3394 temp_loc,
3395 iftable_offset,
3396 maybe_temp2_loc,
3397 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003398 // Iftable is never null.
3399 __ Lw(TMP, temp, array_length_offset);
3400 // Loop through the iftable and check if any class matches.
3401 MipsLabel loop;
3402 __ Bind(&loop);
3403 __ Addiu(temp, temp, 2 * kHeapReferenceSize); // Possibly in delay slot on R2.
3404 __ Beqz(TMP, slow_path->GetEntryLabel());
3405 __ Lw(AT, temp, object_array_data_offset - 2 * kHeapReferenceSize);
3406 __ MaybeUnpoisonHeapReference(AT);
3407 // Go to next interface.
3408 __ Addiu(TMP, TMP, -2);
3409 // Compare the classes and continue the loop if they do not match.
3410 __ Bne(AT, cls, &loop);
3411 break;
3412 }
3413 }
3414
3415 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003416 __ Bind(slow_path->GetExitLabel());
3417}
3418
3419void LocationsBuilderMIPS::VisitClinitCheck(HClinitCheck* check) {
3420 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003421 new (GetGraph()->GetAllocator()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003422 locations->SetInAt(0, Location::RequiresRegister());
3423 if (check->HasUses()) {
3424 locations->SetOut(Location::SameAsFirstInput());
3425 }
3426}
3427
3428void InstructionCodeGeneratorMIPS::VisitClinitCheck(HClinitCheck* check) {
3429 // We assume the class is not null.
Vladimir Markoca6fff82017-10-03 14:49:14 +01003430 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetAllocator()) LoadClassSlowPathMIPS(
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003431 check->GetLoadClass(),
3432 check,
3433 check->GetDexPc(),
3434 true);
3435 codegen_->AddSlowPath(slow_path);
3436 GenerateClassInitializationCheck(slow_path,
3437 check->GetLocations()->InAt(0).AsRegister<Register>());
3438}
3439
3440void LocationsBuilderMIPS::VisitCompare(HCompare* compare) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003441 DataType::Type in_type = compare->InputAt(0)->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003442
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003443 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003444 new (GetGraph()->GetAllocator()) LocationSummary(compare, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003445
3446 switch (in_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003447 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003448 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003449 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003450 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003451 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003452 case DataType::Type::kInt32:
Alexey Frunzee7697712016-09-15 21:37:49 -07003453 locations->SetInAt(0, Location::RequiresRegister());
3454 locations->SetInAt(1, Location::RequiresRegister());
3455 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3456 break;
3457
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003458 case DataType::Type::kInt64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003459 locations->SetInAt(0, Location::RequiresRegister());
3460 locations->SetInAt(1, Location::RequiresRegister());
3461 // Output overlaps because it is written before doing the low comparison.
3462 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3463 break;
3464
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003465 case DataType::Type::kFloat32:
3466 case DataType::Type::kFloat64:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003467 locations->SetInAt(0, Location::RequiresFpuRegister());
3468 locations->SetInAt(1, Location::RequiresFpuRegister());
3469 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003470 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003471
3472 default:
3473 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
3474 }
3475}
3476
3477void InstructionCodeGeneratorMIPS::VisitCompare(HCompare* instruction) {
3478 LocationSummary* locations = instruction->GetLocations();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003479 Register res = locations->Out().AsRegister<Register>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003480 DataType::Type in_type = instruction->InputAt(0)->GetType();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003481 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003482
3483 // 0 if: left == right
3484 // 1 if: left > right
3485 // -1 if: left < right
3486 switch (in_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003487 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003488 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003489 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003490 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003491 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003492 case DataType::Type::kInt32: {
Aart Bika19616e2016-02-01 18:57:58 -08003493 Register lhs = locations->InAt(0).AsRegister<Register>();
3494 Register rhs = locations->InAt(1).AsRegister<Register>();
3495 __ Slt(TMP, lhs, rhs);
3496 __ Slt(res, rhs, lhs);
3497 __ Subu(res, res, TMP);
3498 break;
3499 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003500 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003501 MipsLabel done;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003502 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
3503 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
3504 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
3505 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
3506 // TODO: more efficient (direct) comparison with a constant.
3507 __ Slt(TMP, lhs_high, rhs_high);
3508 __ Slt(AT, rhs_high, lhs_high); // Inverted: is actually gt.
3509 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
3510 __ Bnez(res, &done); // If we compared ==, check if lower bits are also equal.
3511 __ Sltu(TMP, lhs_low, rhs_low);
3512 __ Sltu(AT, rhs_low, lhs_low); // Inverted: is actually gt.
3513 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
3514 __ Bind(&done);
3515 break;
3516 }
3517
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003518 case DataType::Type::kFloat32: {
Roland Levillain32ca3752016-02-17 16:49:37 +00003519 bool gt_bias = instruction->IsGtBias();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003520 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3521 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
3522 MipsLabel done;
3523 if (isR6) {
3524 __ CmpEqS(FTMP, lhs, rhs);
3525 __ LoadConst32(res, 0);
3526 __ Bc1nez(FTMP, &done);
3527 if (gt_bias) {
3528 __ CmpLtS(FTMP, lhs, rhs);
3529 __ LoadConst32(res, -1);
3530 __ Bc1nez(FTMP, &done);
3531 __ LoadConst32(res, 1);
3532 } else {
3533 __ CmpLtS(FTMP, rhs, lhs);
3534 __ LoadConst32(res, 1);
3535 __ Bc1nez(FTMP, &done);
3536 __ LoadConst32(res, -1);
3537 }
3538 } else {
3539 if (gt_bias) {
3540 __ ColtS(0, lhs, rhs);
3541 __ LoadConst32(res, -1);
3542 __ Bc1t(0, &done);
3543 __ CeqS(0, lhs, rhs);
3544 __ LoadConst32(res, 1);
3545 __ Movt(res, ZERO, 0);
3546 } else {
3547 __ ColtS(0, rhs, lhs);
3548 __ LoadConst32(res, 1);
3549 __ Bc1t(0, &done);
3550 __ CeqS(0, lhs, rhs);
3551 __ LoadConst32(res, -1);
3552 __ Movt(res, ZERO, 0);
3553 }
3554 }
3555 __ Bind(&done);
3556 break;
3557 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003558 case DataType::Type::kFloat64: {
Roland Levillain32ca3752016-02-17 16:49:37 +00003559 bool gt_bias = instruction->IsGtBias();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003560 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3561 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
3562 MipsLabel done;
3563 if (isR6) {
3564 __ CmpEqD(FTMP, lhs, rhs);
3565 __ LoadConst32(res, 0);
3566 __ Bc1nez(FTMP, &done);
3567 if (gt_bias) {
3568 __ CmpLtD(FTMP, lhs, rhs);
3569 __ LoadConst32(res, -1);
3570 __ Bc1nez(FTMP, &done);
3571 __ LoadConst32(res, 1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003572 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003573 __ CmpLtD(FTMP, rhs, lhs);
3574 __ LoadConst32(res, 1);
3575 __ Bc1nez(FTMP, &done);
3576 __ LoadConst32(res, -1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003577 }
3578 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003579 if (gt_bias) {
3580 __ ColtD(0, lhs, rhs);
3581 __ LoadConst32(res, -1);
3582 __ Bc1t(0, &done);
3583 __ CeqD(0, lhs, rhs);
3584 __ LoadConst32(res, 1);
3585 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003586 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003587 __ ColtD(0, rhs, lhs);
3588 __ LoadConst32(res, 1);
3589 __ Bc1t(0, &done);
3590 __ CeqD(0, lhs, rhs);
3591 __ LoadConst32(res, -1);
3592 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003593 }
3594 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003595 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003596 break;
3597 }
3598
3599 default:
3600 LOG(FATAL) << "Unimplemented compare type " << in_type;
3601 }
3602}
3603
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003604void LocationsBuilderMIPS::HandleCondition(HCondition* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003605 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003606 switch (instruction->InputAt(0)->GetType()) {
3607 default:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003608 case DataType::Type::kInt64:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003609 locations->SetInAt(0, Location::RequiresRegister());
3610 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
3611 break;
3612
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003613 case DataType::Type::kFloat32:
3614 case DataType::Type::kFloat64:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003615 locations->SetInAt(0, Location::RequiresFpuRegister());
3616 locations->SetInAt(1, Location::RequiresFpuRegister());
3617 break;
3618 }
David Brazdilb3e773e2016-01-26 11:28:37 +00003619 if (!instruction->IsEmittedAtUseSite()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003620 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3621 }
3622}
3623
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003624void InstructionCodeGeneratorMIPS::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00003625 if (instruction->IsEmittedAtUseSite()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003626 return;
3627 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003628
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003629 DataType::Type type = instruction->InputAt(0)->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003630 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003631
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003632 switch (type) {
3633 default:
3634 // Integer case.
3635 GenerateIntCompare(instruction->GetCondition(), locations);
3636 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003637
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003638 case DataType::Type::kInt64:
Tijana Jakovljevic6d482aa2017-02-03 13:24:08 +01003639 GenerateLongCompare(instruction->GetCondition(), locations);
3640 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003641
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003642 case DataType::Type::kFloat32:
3643 case DataType::Type::kFloat64:
Alexey Frunze2ddb7172016-09-06 17:04:55 -07003644 GenerateFpCompare(instruction->GetCondition(), instruction->IsGtBias(), type, locations);
3645 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003646 }
3647}
3648
Alexey Frunze7e99e052015-11-24 19:28:01 -08003649void InstructionCodeGeneratorMIPS::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3650 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003651 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt32);
Alexey Frunze7e99e052015-11-24 19:28:01 -08003652
3653 LocationSummary* locations = instruction->GetLocations();
3654 Location second = locations->InAt(1);
3655 DCHECK(second.IsConstant());
3656
3657 Register out = locations->Out().AsRegister<Register>();
3658 Register dividend = locations->InAt(0).AsRegister<Register>();
3659 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3660 DCHECK(imm == 1 || imm == -1);
3661
3662 if (instruction->IsRem()) {
3663 __ Move(out, ZERO);
3664 } else {
3665 if (imm == -1) {
3666 __ Subu(out, ZERO, dividend);
3667 } else if (out != dividend) {
3668 __ Move(out, dividend);
3669 }
3670 }
3671}
3672
3673void InstructionCodeGeneratorMIPS::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
3674 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003675 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt32);
Alexey Frunze7e99e052015-11-24 19:28:01 -08003676
3677 LocationSummary* locations = instruction->GetLocations();
3678 Location second = locations->InAt(1);
3679 DCHECK(second.IsConstant());
3680
3681 Register out = locations->Out().AsRegister<Register>();
3682 Register dividend = locations->InAt(0).AsRegister<Register>();
3683 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003684 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Alexey Frunze7e99e052015-11-24 19:28:01 -08003685 int ctz_imm = CTZ(abs_imm);
3686
3687 if (instruction->IsDiv()) {
3688 if (ctz_imm == 1) {
3689 // Fast path for division by +/-2, which is very common.
3690 __ Srl(TMP, dividend, 31);
3691 } else {
3692 __ Sra(TMP, dividend, 31);
3693 __ Srl(TMP, TMP, 32 - ctz_imm);
3694 }
3695 __ Addu(out, dividend, TMP);
3696 __ Sra(out, out, ctz_imm);
3697 if (imm < 0) {
3698 __ Subu(out, ZERO, out);
3699 }
3700 } else {
3701 if (ctz_imm == 1) {
3702 // Fast path for modulo +/-2, which is very common.
3703 __ Sra(TMP, dividend, 31);
3704 __ Subu(out, dividend, TMP);
3705 __ Andi(out, out, 1);
3706 __ Addu(out, out, TMP);
3707 } else {
3708 __ Sra(TMP, dividend, 31);
3709 __ Srl(TMP, TMP, 32 - ctz_imm);
3710 __ Addu(out, dividend, TMP);
3711 if (IsUint<16>(abs_imm - 1)) {
3712 __ Andi(out, out, abs_imm - 1);
3713 } else {
3714 __ Sll(out, out, 32 - ctz_imm);
3715 __ Srl(out, out, 32 - ctz_imm);
3716 }
3717 __ Subu(out, out, TMP);
3718 }
3719 }
3720}
3721
3722void InstructionCodeGeneratorMIPS::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3723 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003724 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt32);
Alexey Frunze7e99e052015-11-24 19:28:01 -08003725
3726 LocationSummary* locations = instruction->GetLocations();
3727 Location second = locations->InAt(1);
3728 DCHECK(second.IsConstant());
3729
3730 Register out = locations->Out().AsRegister<Register>();
3731 Register dividend = locations->InAt(0).AsRegister<Register>();
3732 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3733
3734 int64_t magic;
3735 int shift;
3736 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3737
3738 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3739
3740 __ LoadConst32(TMP, magic);
3741 if (isR6) {
3742 __ MuhR6(TMP, dividend, TMP);
3743 } else {
3744 __ MultR2(dividend, TMP);
3745 __ Mfhi(TMP);
3746 }
3747 if (imm > 0 && magic < 0) {
3748 __ Addu(TMP, TMP, dividend);
3749 } else if (imm < 0 && magic > 0) {
3750 __ Subu(TMP, TMP, dividend);
3751 }
3752
3753 if (shift != 0) {
3754 __ Sra(TMP, TMP, shift);
3755 }
3756
3757 if (instruction->IsDiv()) {
3758 __ Sra(out, TMP, 31);
3759 __ Subu(out, TMP, out);
3760 } else {
3761 __ Sra(AT, TMP, 31);
3762 __ Subu(AT, TMP, AT);
3763 __ LoadConst32(TMP, imm);
3764 if (isR6) {
3765 __ MulR6(TMP, AT, TMP);
3766 } else {
3767 __ MulR2(TMP, AT, TMP);
3768 }
3769 __ Subu(out, dividend, TMP);
3770 }
3771}
3772
3773void InstructionCodeGeneratorMIPS::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3774 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003775 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt32);
Alexey Frunze7e99e052015-11-24 19:28:01 -08003776
3777 LocationSummary* locations = instruction->GetLocations();
3778 Register out = locations->Out().AsRegister<Register>();
3779 Location second = locations->InAt(1);
3780
3781 if (second.IsConstant()) {
3782 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3783 if (imm == 0) {
3784 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3785 } else if (imm == 1 || imm == -1) {
3786 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003787 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08003788 DivRemByPowerOfTwo(instruction);
3789 } else {
3790 DCHECK(imm <= -2 || imm >= 2);
3791 GenerateDivRemWithAnyConstant(instruction);
3792 }
3793 } else {
3794 Register dividend = locations->InAt(0).AsRegister<Register>();
3795 Register divisor = second.AsRegister<Register>();
3796 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3797 if (instruction->IsDiv()) {
3798 if (isR6) {
3799 __ DivR6(out, dividend, divisor);
3800 } else {
3801 __ DivR2(out, dividend, divisor);
3802 }
3803 } else {
3804 if (isR6) {
3805 __ ModR6(out, dividend, divisor);
3806 } else {
3807 __ ModR2(out, dividend, divisor);
3808 }
3809 }
3810 }
3811}
3812
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003813void LocationsBuilderMIPS::VisitDiv(HDiv* div) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003814 DataType::Type type = div->GetResultType();
3815 LocationSummary::CallKind call_kind = (type == DataType::Type::kInt64)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003816 ? LocationSummary::kCallOnMainOnly
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003817 : LocationSummary::kNoCall;
3818
Vladimir Markoca6fff82017-10-03 14:49:14 +01003819 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(div, call_kind);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003820
3821 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003822 case DataType::Type::kInt32:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003823 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08003824 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003825 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3826 break;
3827
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003828 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003829 InvokeRuntimeCallingConvention calling_convention;
3830 locations->SetInAt(0, Location::RegisterPairLocation(
3831 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3832 locations->SetInAt(1, Location::RegisterPairLocation(
3833 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3834 locations->SetOut(calling_convention.GetReturnLocation(type));
3835 break;
3836 }
3837
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003838 case DataType::Type::kFloat32:
3839 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003840 locations->SetInAt(0, Location::RequiresFpuRegister());
3841 locations->SetInAt(1, Location::RequiresFpuRegister());
3842 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3843 break;
3844
3845 default:
3846 LOG(FATAL) << "Unexpected div type " << type;
3847 }
3848}
3849
3850void InstructionCodeGeneratorMIPS::VisitDiv(HDiv* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003851 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003852 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003853
3854 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003855 case DataType::Type::kInt32:
Alexey Frunze7e99e052015-11-24 19:28:01 -08003856 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003857 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003858 case DataType::Type::kInt64: {
Serban Constantinescufca16662016-07-14 09:21:59 +01003859 codegen_->InvokeRuntime(kQuickLdiv, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003860 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
3861 break;
3862 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003863 case DataType::Type::kFloat32:
3864 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003865 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
3866 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3867 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003868 if (type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003869 __ DivS(dst, lhs, rhs);
3870 } else {
3871 __ DivD(dst, lhs, rhs);
3872 }
3873 break;
3874 }
3875 default:
3876 LOG(FATAL) << "Unexpected div type " << type;
3877 }
3878}
3879
3880void LocationsBuilderMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003881 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003882 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003883}
3884
3885void InstructionCodeGeneratorMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003886 SlowPathCodeMIPS* slow_path =
3887 new (GetGraph()->GetAllocator()) DivZeroCheckSlowPathMIPS(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003888 codegen_->AddSlowPath(slow_path);
3889 Location value = instruction->GetLocations()->InAt(0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003890 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003891
3892 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003893 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003894 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003895 case DataType::Type::kInt8:
3896 case DataType::Type::kUint16:
3897 case DataType::Type::kInt16:
3898 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003899 if (value.IsConstant()) {
3900 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3901 __ B(slow_path->GetEntryLabel());
3902 } else {
3903 // A division by a non-null constant is valid. We don't need to perform
3904 // any check, so simply fall through.
3905 }
3906 } else {
3907 DCHECK(value.IsRegister()) << value;
3908 __ Beqz(value.AsRegister<Register>(), slow_path->GetEntryLabel());
3909 }
3910 break;
3911 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003912 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003913 if (value.IsConstant()) {
3914 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3915 __ B(slow_path->GetEntryLabel());
3916 } else {
3917 // A division by a non-null constant is valid. We don't need to perform
3918 // any check, so simply fall through.
3919 }
3920 } else {
3921 DCHECK(value.IsRegisterPair()) << value;
3922 __ Or(TMP, value.AsRegisterPairHigh<Register>(), value.AsRegisterPairLow<Register>());
3923 __ Beqz(TMP, slow_path->GetEntryLabel());
3924 }
3925 break;
3926 }
3927 default:
3928 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
3929 }
3930}
3931
3932void LocationsBuilderMIPS::VisitDoubleConstant(HDoubleConstant* constant) {
3933 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003934 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003935 locations->SetOut(Location::ConstantLocation(constant));
3936}
3937
3938void InstructionCodeGeneratorMIPS::VisitDoubleConstant(HDoubleConstant* cst ATTRIBUTE_UNUSED) {
3939 // Will be generated at use site.
3940}
3941
3942void LocationsBuilderMIPS::VisitExit(HExit* exit) {
3943 exit->SetLocations(nullptr);
3944}
3945
3946void InstructionCodeGeneratorMIPS::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
3947}
3948
3949void LocationsBuilderMIPS::VisitFloatConstant(HFloatConstant* constant) {
3950 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003951 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003952 locations->SetOut(Location::ConstantLocation(constant));
3953}
3954
3955void InstructionCodeGeneratorMIPS::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
3956 // Will be generated at use site.
3957}
3958
3959void LocationsBuilderMIPS::VisitGoto(HGoto* got) {
3960 got->SetLocations(nullptr);
3961}
3962
3963void InstructionCodeGeneratorMIPS::HandleGoto(HInstruction* got, HBasicBlock* successor) {
3964 DCHECK(!successor->IsExitBlock());
3965 HBasicBlock* block = got->GetBlock();
3966 HInstruction* previous = got->GetPrevious();
3967 HLoopInformation* info = block->GetLoopInformation();
3968
3969 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003970 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
3971 return;
3972 }
3973 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
3974 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
3975 }
3976 if (!codegen_->GoesToNextBlock(block, successor)) {
3977 __ B(codegen_->GetLabelOf(successor));
3978 }
3979}
3980
3981void InstructionCodeGeneratorMIPS::VisitGoto(HGoto* got) {
3982 HandleGoto(got, got->GetSuccessor());
3983}
3984
3985void LocationsBuilderMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
3986 try_boundary->SetLocations(nullptr);
3987}
3988
3989void InstructionCodeGeneratorMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
3990 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
3991 if (!successor->IsExitBlock()) {
3992 HandleGoto(try_boundary, successor);
3993 }
3994}
3995
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003996void InstructionCodeGeneratorMIPS::GenerateIntCompare(IfCondition cond,
3997 LocationSummary* locations) {
3998 Register dst = locations->Out().AsRegister<Register>();
3999 Register lhs = locations->InAt(0).AsRegister<Register>();
4000 Location rhs_location = locations->InAt(1);
4001 Register rhs_reg = ZERO;
4002 int64_t rhs_imm = 0;
4003 bool use_imm = rhs_location.IsConstant();
4004 if (use_imm) {
4005 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
4006 } else {
4007 rhs_reg = rhs_location.AsRegister<Register>();
4008 }
4009
4010 switch (cond) {
4011 case kCondEQ:
4012 case kCondNE:
Alexey Frunzee7697712016-09-15 21:37:49 -07004013 if (use_imm && IsInt<16>(-rhs_imm)) {
4014 if (rhs_imm == 0) {
4015 if (cond == kCondEQ) {
4016 __ Sltiu(dst, lhs, 1);
4017 } else {
4018 __ Sltu(dst, ZERO, lhs);
4019 }
4020 } else {
4021 __ Addiu(dst, lhs, -rhs_imm);
4022 if (cond == kCondEQ) {
4023 __ Sltiu(dst, dst, 1);
4024 } else {
4025 __ Sltu(dst, ZERO, dst);
4026 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004027 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004028 } else {
Alexey Frunzee7697712016-09-15 21:37:49 -07004029 if (use_imm && IsUint<16>(rhs_imm)) {
4030 __ Xori(dst, lhs, rhs_imm);
4031 } else {
4032 if (use_imm) {
4033 rhs_reg = TMP;
4034 __ LoadConst32(rhs_reg, rhs_imm);
4035 }
4036 __ Xor(dst, lhs, rhs_reg);
4037 }
4038 if (cond == kCondEQ) {
4039 __ Sltiu(dst, dst, 1);
4040 } else {
4041 __ Sltu(dst, ZERO, dst);
4042 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004043 }
4044 break;
4045
4046 case kCondLT:
4047 case kCondGE:
4048 if (use_imm && IsInt<16>(rhs_imm)) {
4049 __ Slti(dst, lhs, rhs_imm);
4050 } else {
4051 if (use_imm) {
4052 rhs_reg = TMP;
4053 __ LoadConst32(rhs_reg, rhs_imm);
4054 }
4055 __ Slt(dst, lhs, rhs_reg);
4056 }
4057 if (cond == kCondGE) {
4058 // Simulate lhs >= rhs via !(lhs < rhs) since there's
4059 // only the slt instruction but no sge.
4060 __ Xori(dst, dst, 1);
4061 }
4062 break;
4063
4064 case kCondLE:
4065 case kCondGT:
4066 if (use_imm && IsInt<16>(rhs_imm + 1)) {
4067 // Simulate lhs <= rhs via lhs < rhs + 1.
4068 __ Slti(dst, lhs, rhs_imm + 1);
4069 if (cond == kCondGT) {
4070 // Simulate lhs > rhs via !(lhs <= rhs) since there's
4071 // only the slti instruction but no sgti.
4072 __ Xori(dst, dst, 1);
4073 }
4074 } else {
4075 if (use_imm) {
4076 rhs_reg = TMP;
4077 __ LoadConst32(rhs_reg, rhs_imm);
4078 }
4079 __ Slt(dst, rhs_reg, lhs);
4080 if (cond == kCondLE) {
4081 // Simulate lhs <= rhs via !(rhs < lhs) since there's
4082 // only the slt instruction but no sle.
4083 __ Xori(dst, dst, 1);
4084 }
4085 }
4086 break;
4087
4088 case kCondB:
4089 case kCondAE:
4090 if (use_imm && IsInt<16>(rhs_imm)) {
4091 // Sltiu sign-extends its 16-bit immediate operand before
4092 // the comparison and thus lets us compare directly with
4093 // unsigned values in the ranges [0, 0x7fff] and
4094 // [0xffff8000, 0xffffffff].
4095 __ Sltiu(dst, lhs, rhs_imm);
4096 } else {
4097 if (use_imm) {
4098 rhs_reg = TMP;
4099 __ LoadConst32(rhs_reg, rhs_imm);
4100 }
4101 __ Sltu(dst, lhs, rhs_reg);
4102 }
4103 if (cond == kCondAE) {
4104 // Simulate lhs >= rhs via !(lhs < rhs) since there's
4105 // only the sltu instruction but no sgeu.
4106 __ Xori(dst, dst, 1);
4107 }
4108 break;
4109
4110 case kCondBE:
4111 case kCondA:
4112 if (use_imm && (rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4113 // Simulate lhs <= rhs via lhs < rhs + 1.
4114 // Note that this only works if rhs + 1 does not overflow
4115 // to 0, hence the check above.
4116 // Sltiu sign-extends its 16-bit immediate operand before
4117 // the comparison and thus lets us compare directly with
4118 // unsigned values in the ranges [0, 0x7fff] and
4119 // [0xffff8000, 0xffffffff].
4120 __ Sltiu(dst, lhs, rhs_imm + 1);
4121 if (cond == kCondA) {
4122 // Simulate lhs > rhs via !(lhs <= rhs) since there's
4123 // only the sltiu instruction but no sgtiu.
4124 __ Xori(dst, dst, 1);
4125 }
4126 } else {
4127 if (use_imm) {
4128 rhs_reg = TMP;
4129 __ LoadConst32(rhs_reg, rhs_imm);
4130 }
4131 __ Sltu(dst, rhs_reg, lhs);
4132 if (cond == kCondBE) {
4133 // Simulate lhs <= rhs via !(rhs < lhs) since there's
4134 // only the sltu instruction but no sleu.
4135 __ Xori(dst, dst, 1);
4136 }
4137 }
4138 break;
4139 }
4140}
4141
Alexey Frunze674b9ee2016-09-20 14:54:15 -07004142bool InstructionCodeGeneratorMIPS::MaterializeIntCompare(IfCondition cond,
4143 LocationSummary* input_locations,
4144 Register dst) {
4145 Register lhs = input_locations->InAt(0).AsRegister<Register>();
4146 Location rhs_location = input_locations->InAt(1);
4147 Register rhs_reg = ZERO;
4148 int64_t rhs_imm = 0;
4149 bool use_imm = rhs_location.IsConstant();
4150 if (use_imm) {
4151 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
4152 } else {
4153 rhs_reg = rhs_location.AsRegister<Register>();
4154 }
4155
4156 switch (cond) {
4157 case kCondEQ:
4158 case kCondNE:
4159 if (use_imm && IsInt<16>(-rhs_imm)) {
4160 __ Addiu(dst, lhs, -rhs_imm);
4161 } else if (use_imm && IsUint<16>(rhs_imm)) {
4162 __ Xori(dst, lhs, rhs_imm);
4163 } else {
4164 if (use_imm) {
4165 rhs_reg = TMP;
4166 __ LoadConst32(rhs_reg, rhs_imm);
4167 }
4168 __ Xor(dst, lhs, rhs_reg);
4169 }
4170 return (cond == kCondEQ);
4171
4172 case kCondLT:
4173 case kCondGE:
4174 if (use_imm && IsInt<16>(rhs_imm)) {
4175 __ Slti(dst, lhs, rhs_imm);
4176 } else {
4177 if (use_imm) {
4178 rhs_reg = TMP;
4179 __ LoadConst32(rhs_reg, rhs_imm);
4180 }
4181 __ Slt(dst, lhs, rhs_reg);
4182 }
4183 return (cond == kCondGE);
4184
4185 case kCondLE:
4186 case kCondGT:
4187 if (use_imm && IsInt<16>(rhs_imm + 1)) {
4188 // Simulate lhs <= rhs via lhs < rhs + 1.
4189 __ Slti(dst, lhs, rhs_imm + 1);
4190 return (cond == kCondGT);
4191 } else {
4192 if (use_imm) {
4193 rhs_reg = TMP;
4194 __ LoadConst32(rhs_reg, rhs_imm);
4195 }
4196 __ Slt(dst, rhs_reg, lhs);
4197 return (cond == kCondLE);
4198 }
4199
4200 case kCondB:
4201 case kCondAE:
4202 if (use_imm && IsInt<16>(rhs_imm)) {
4203 // Sltiu sign-extends its 16-bit immediate operand before
4204 // the comparison and thus lets us compare directly with
4205 // unsigned values in the ranges [0, 0x7fff] and
4206 // [0xffff8000, 0xffffffff].
4207 __ Sltiu(dst, lhs, rhs_imm);
4208 } else {
4209 if (use_imm) {
4210 rhs_reg = TMP;
4211 __ LoadConst32(rhs_reg, rhs_imm);
4212 }
4213 __ Sltu(dst, lhs, rhs_reg);
4214 }
4215 return (cond == kCondAE);
4216
4217 case kCondBE:
4218 case kCondA:
4219 if (use_imm && (rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4220 // Simulate lhs <= rhs via lhs < rhs + 1.
4221 // Note that this only works if rhs + 1 does not overflow
4222 // to 0, hence the check above.
4223 // Sltiu sign-extends its 16-bit immediate operand before
4224 // the comparison and thus lets us compare directly with
4225 // unsigned values in the ranges [0, 0x7fff] and
4226 // [0xffff8000, 0xffffffff].
4227 __ Sltiu(dst, lhs, rhs_imm + 1);
4228 return (cond == kCondA);
4229 } else {
4230 if (use_imm) {
4231 rhs_reg = TMP;
4232 __ LoadConst32(rhs_reg, rhs_imm);
4233 }
4234 __ Sltu(dst, rhs_reg, lhs);
4235 return (cond == kCondBE);
4236 }
4237 }
4238}
4239
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004240void InstructionCodeGeneratorMIPS::GenerateIntCompareAndBranch(IfCondition cond,
4241 LocationSummary* locations,
4242 MipsLabel* label) {
4243 Register lhs = locations->InAt(0).AsRegister<Register>();
4244 Location rhs_location = locations->InAt(1);
4245 Register rhs_reg = ZERO;
Alexey Frunzee7697712016-09-15 21:37:49 -07004246 int64_t rhs_imm = 0;
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004247 bool use_imm = rhs_location.IsConstant();
4248 if (use_imm) {
4249 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
4250 } else {
4251 rhs_reg = rhs_location.AsRegister<Register>();
4252 }
4253
4254 if (use_imm && rhs_imm == 0) {
4255 switch (cond) {
4256 case kCondEQ:
4257 case kCondBE: // <= 0 if zero
4258 __ Beqz(lhs, label);
4259 break;
4260 case kCondNE:
4261 case kCondA: // > 0 if non-zero
4262 __ Bnez(lhs, label);
4263 break;
4264 case kCondLT:
4265 __ Bltz(lhs, label);
4266 break;
4267 case kCondGE:
4268 __ Bgez(lhs, label);
4269 break;
4270 case kCondLE:
4271 __ Blez(lhs, label);
4272 break;
4273 case kCondGT:
4274 __ Bgtz(lhs, label);
4275 break;
4276 case kCondB: // always false
4277 break;
4278 case kCondAE: // always true
4279 __ B(label);
4280 break;
4281 }
4282 } else {
Alexey Frunzee7697712016-09-15 21:37:49 -07004283 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
4284 if (isR6 || !use_imm) {
4285 if (use_imm) {
4286 rhs_reg = TMP;
4287 __ LoadConst32(rhs_reg, rhs_imm);
4288 }
4289 switch (cond) {
4290 case kCondEQ:
4291 __ Beq(lhs, rhs_reg, label);
4292 break;
4293 case kCondNE:
4294 __ Bne(lhs, rhs_reg, label);
4295 break;
4296 case kCondLT:
4297 __ Blt(lhs, rhs_reg, label);
4298 break;
4299 case kCondGE:
4300 __ Bge(lhs, rhs_reg, label);
4301 break;
4302 case kCondLE:
4303 __ Bge(rhs_reg, lhs, label);
4304 break;
4305 case kCondGT:
4306 __ Blt(rhs_reg, lhs, label);
4307 break;
4308 case kCondB:
4309 __ Bltu(lhs, rhs_reg, label);
4310 break;
4311 case kCondAE:
4312 __ Bgeu(lhs, rhs_reg, label);
4313 break;
4314 case kCondBE:
4315 __ Bgeu(rhs_reg, lhs, label);
4316 break;
4317 case kCondA:
4318 __ Bltu(rhs_reg, lhs, label);
4319 break;
4320 }
4321 } else {
4322 // Special cases for more efficient comparison with constants on R2.
4323 switch (cond) {
4324 case kCondEQ:
4325 __ LoadConst32(TMP, rhs_imm);
4326 __ Beq(lhs, TMP, label);
4327 break;
4328 case kCondNE:
4329 __ LoadConst32(TMP, rhs_imm);
4330 __ Bne(lhs, TMP, label);
4331 break;
4332 case kCondLT:
4333 if (IsInt<16>(rhs_imm)) {
4334 __ Slti(TMP, lhs, rhs_imm);
4335 __ Bnez(TMP, label);
4336 } else {
4337 __ LoadConst32(TMP, rhs_imm);
4338 __ Blt(lhs, TMP, label);
4339 }
4340 break;
4341 case kCondGE:
4342 if (IsInt<16>(rhs_imm)) {
4343 __ Slti(TMP, lhs, rhs_imm);
4344 __ Beqz(TMP, label);
4345 } else {
4346 __ LoadConst32(TMP, rhs_imm);
4347 __ Bge(lhs, TMP, label);
4348 }
4349 break;
4350 case kCondLE:
4351 if (IsInt<16>(rhs_imm + 1)) {
4352 // Simulate lhs <= rhs via lhs < rhs + 1.
4353 __ Slti(TMP, lhs, rhs_imm + 1);
4354 __ Bnez(TMP, label);
4355 } else {
4356 __ LoadConst32(TMP, rhs_imm);
4357 __ Bge(TMP, lhs, label);
4358 }
4359 break;
4360 case kCondGT:
4361 if (IsInt<16>(rhs_imm + 1)) {
4362 // Simulate lhs > rhs via !(lhs < rhs + 1).
4363 __ Slti(TMP, lhs, rhs_imm + 1);
4364 __ Beqz(TMP, label);
4365 } else {
4366 __ LoadConst32(TMP, rhs_imm);
4367 __ Blt(TMP, lhs, label);
4368 }
4369 break;
4370 case kCondB:
4371 if (IsInt<16>(rhs_imm)) {
4372 __ Sltiu(TMP, lhs, rhs_imm);
4373 __ Bnez(TMP, label);
4374 } else {
4375 __ LoadConst32(TMP, rhs_imm);
4376 __ Bltu(lhs, TMP, label);
4377 }
4378 break;
4379 case kCondAE:
4380 if (IsInt<16>(rhs_imm)) {
4381 __ Sltiu(TMP, lhs, rhs_imm);
4382 __ Beqz(TMP, label);
4383 } else {
4384 __ LoadConst32(TMP, rhs_imm);
4385 __ Bgeu(lhs, TMP, label);
4386 }
4387 break;
4388 case kCondBE:
4389 if ((rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4390 // Simulate lhs <= rhs via lhs < rhs + 1.
4391 // Note that this only works if rhs + 1 does not overflow
4392 // to 0, hence the check above.
4393 __ Sltiu(TMP, lhs, rhs_imm + 1);
4394 __ Bnez(TMP, label);
4395 } else {
4396 __ LoadConst32(TMP, rhs_imm);
4397 __ Bgeu(TMP, lhs, label);
4398 }
4399 break;
4400 case kCondA:
4401 if ((rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4402 // Simulate lhs > rhs via !(lhs < rhs + 1).
4403 // Note that this only works if rhs + 1 does not overflow
4404 // to 0, hence the check above.
4405 __ Sltiu(TMP, lhs, rhs_imm + 1);
4406 __ Beqz(TMP, label);
4407 } else {
4408 __ LoadConst32(TMP, rhs_imm);
4409 __ Bltu(TMP, lhs, label);
4410 }
4411 break;
4412 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004413 }
4414 }
4415}
4416
Tijana Jakovljevic6d482aa2017-02-03 13:24:08 +01004417void InstructionCodeGeneratorMIPS::GenerateLongCompare(IfCondition cond,
4418 LocationSummary* locations) {
4419 Register dst = locations->Out().AsRegister<Register>();
4420 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
4421 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
4422 Location rhs_location = locations->InAt(1);
4423 Register rhs_high = ZERO;
4424 Register rhs_low = ZERO;
4425 int64_t imm = 0;
4426 uint32_t imm_high = 0;
4427 uint32_t imm_low = 0;
4428 bool use_imm = rhs_location.IsConstant();
4429 if (use_imm) {
4430 imm = rhs_location.GetConstant()->AsLongConstant()->GetValue();
4431 imm_high = High32Bits(imm);
4432 imm_low = Low32Bits(imm);
4433 } else {
4434 rhs_high = rhs_location.AsRegisterPairHigh<Register>();
4435 rhs_low = rhs_location.AsRegisterPairLow<Register>();
4436 }
4437 if (use_imm && imm == 0) {
4438 switch (cond) {
4439 case kCondEQ:
4440 case kCondBE: // <= 0 if zero
4441 __ Or(dst, lhs_high, lhs_low);
4442 __ Sltiu(dst, dst, 1);
4443 break;
4444 case kCondNE:
4445 case kCondA: // > 0 if non-zero
4446 __ Or(dst, lhs_high, lhs_low);
4447 __ Sltu(dst, ZERO, dst);
4448 break;
4449 case kCondLT:
4450 __ Slt(dst, lhs_high, ZERO);
4451 break;
4452 case kCondGE:
4453 __ Slt(dst, lhs_high, ZERO);
4454 __ Xori(dst, dst, 1);
4455 break;
4456 case kCondLE:
4457 __ Or(TMP, lhs_high, lhs_low);
4458 __ Sra(AT, lhs_high, 31);
4459 __ Sltu(dst, AT, TMP);
4460 __ Xori(dst, dst, 1);
4461 break;
4462 case kCondGT:
4463 __ Or(TMP, lhs_high, lhs_low);
4464 __ Sra(AT, lhs_high, 31);
4465 __ Sltu(dst, AT, TMP);
4466 break;
4467 case kCondB: // always false
4468 __ Andi(dst, dst, 0);
4469 break;
4470 case kCondAE: // always true
4471 __ Ori(dst, ZERO, 1);
4472 break;
4473 }
4474 } else if (use_imm) {
4475 // TODO: more efficient comparison with constants without loading them into TMP/AT.
4476 switch (cond) {
4477 case kCondEQ:
4478 __ LoadConst32(TMP, imm_high);
4479 __ Xor(TMP, TMP, lhs_high);
4480 __ LoadConst32(AT, imm_low);
4481 __ Xor(AT, AT, lhs_low);
4482 __ Or(dst, TMP, AT);
4483 __ Sltiu(dst, dst, 1);
4484 break;
4485 case kCondNE:
4486 __ LoadConst32(TMP, imm_high);
4487 __ Xor(TMP, TMP, lhs_high);
4488 __ LoadConst32(AT, imm_low);
4489 __ Xor(AT, AT, lhs_low);
4490 __ Or(dst, TMP, AT);
4491 __ Sltu(dst, ZERO, dst);
4492 break;
4493 case kCondLT:
4494 case kCondGE:
4495 if (dst == lhs_low) {
4496 __ LoadConst32(TMP, imm_low);
4497 __ Sltu(dst, lhs_low, TMP);
4498 }
4499 __ LoadConst32(TMP, imm_high);
4500 __ Slt(AT, lhs_high, TMP);
4501 __ Slt(TMP, TMP, lhs_high);
4502 if (dst != lhs_low) {
4503 __ LoadConst32(dst, imm_low);
4504 __ Sltu(dst, lhs_low, dst);
4505 }
4506 __ Slt(dst, TMP, dst);
4507 __ Or(dst, dst, AT);
4508 if (cond == kCondGE) {
4509 __ Xori(dst, dst, 1);
4510 }
4511 break;
4512 case kCondGT:
4513 case kCondLE:
4514 if (dst == lhs_low) {
4515 __ LoadConst32(TMP, imm_low);
4516 __ Sltu(dst, TMP, lhs_low);
4517 }
4518 __ LoadConst32(TMP, imm_high);
4519 __ Slt(AT, TMP, lhs_high);
4520 __ Slt(TMP, lhs_high, TMP);
4521 if (dst != lhs_low) {
4522 __ LoadConst32(dst, imm_low);
4523 __ Sltu(dst, dst, lhs_low);
4524 }
4525 __ Slt(dst, TMP, dst);
4526 __ Or(dst, dst, AT);
4527 if (cond == kCondLE) {
4528 __ Xori(dst, dst, 1);
4529 }
4530 break;
4531 case kCondB:
4532 case kCondAE:
4533 if (dst == lhs_low) {
4534 __ LoadConst32(TMP, imm_low);
4535 __ Sltu(dst, lhs_low, TMP);
4536 }
4537 __ LoadConst32(TMP, imm_high);
4538 __ Sltu(AT, lhs_high, TMP);
4539 __ Sltu(TMP, TMP, lhs_high);
4540 if (dst != lhs_low) {
4541 __ LoadConst32(dst, imm_low);
4542 __ Sltu(dst, lhs_low, dst);
4543 }
4544 __ Slt(dst, TMP, dst);
4545 __ Or(dst, dst, AT);
4546 if (cond == kCondAE) {
4547 __ Xori(dst, dst, 1);
4548 }
4549 break;
4550 case kCondA:
4551 case kCondBE:
4552 if (dst == lhs_low) {
4553 __ LoadConst32(TMP, imm_low);
4554 __ Sltu(dst, TMP, lhs_low);
4555 }
4556 __ LoadConst32(TMP, imm_high);
4557 __ Sltu(AT, TMP, lhs_high);
4558 __ Sltu(TMP, lhs_high, TMP);
4559 if (dst != lhs_low) {
4560 __ LoadConst32(dst, imm_low);
4561 __ Sltu(dst, dst, lhs_low);
4562 }
4563 __ Slt(dst, TMP, dst);
4564 __ Or(dst, dst, AT);
4565 if (cond == kCondBE) {
4566 __ Xori(dst, dst, 1);
4567 }
4568 break;
4569 }
4570 } else {
4571 switch (cond) {
4572 case kCondEQ:
4573 __ Xor(TMP, lhs_high, rhs_high);
4574 __ Xor(AT, lhs_low, rhs_low);
4575 __ Or(dst, TMP, AT);
4576 __ Sltiu(dst, dst, 1);
4577 break;
4578 case kCondNE:
4579 __ Xor(TMP, lhs_high, rhs_high);
4580 __ Xor(AT, lhs_low, rhs_low);
4581 __ Or(dst, TMP, AT);
4582 __ Sltu(dst, ZERO, dst);
4583 break;
4584 case kCondLT:
4585 case kCondGE:
4586 __ Slt(TMP, rhs_high, lhs_high);
4587 __ Sltu(AT, lhs_low, rhs_low);
4588 __ Slt(TMP, TMP, AT);
4589 __ Slt(AT, lhs_high, rhs_high);
4590 __ Or(dst, AT, TMP);
4591 if (cond == kCondGE) {
4592 __ Xori(dst, dst, 1);
4593 }
4594 break;
4595 case kCondGT:
4596 case kCondLE:
4597 __ Slt(TMP, lhs_high, rhs_high);
4598 __ Sltu(AT, rhs_low, lhs_low);
4599 __ Slt(TMP, TMP, AT);
4600 __ Slt(AT, rhs_high, lhs_high);
4601 __ Or(dst, AT, TMP);
4602 if (cond == kCondLE) {
4603 __ Xori(dst, dst, 1);
4604 }
4605 break;
4606 case kCondB:
4607 case kCondAE:
4608 __ Sltu(TMP, rhs_high, lhs_high);
4609 __ Sltu(AT, lhs_low, rhs_low);
4610 __ Slt(TMP, TMP, AT);
4611 __ Sltu(AT, lhs_high, rhs_high);
4612 __ Or(dst, AT, TMP);
4613 if (cond == kCondAE) {
4614 __ Xori(dst, dst, 1);
4615 }
4616 break;
4617 case kCondA:
4618 case kCondBE:
4619 __ Sltu(TMP, lhs_high, rhs_high);
4620 __ Sltu(AT, rhs_low, lhs_low);
4621 __ Slt(TMP, TMP, AT);
4622 __ Sltu(AT, rhs_high, lhs_high);
4623 __ Or(dst, AT, TMP);
4624 if (cond == kCondBE) {
4625 __ Xori(dst, dst, 1);
4626 }
4627 break;
4628 }
4629 }
4630}
4631
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004632void InstructionCodeGeneratorMIPS::GenerateLongCompareAndBranch(IfCondition cond,
4633 LocationSummary* locations,
4634 MipsLabel* label) {
4635 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
4636 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
4637 Location rhs_location = locations->InAt(1);
4638 Register rhs_high = ZERO;
4639 Register rhs_low = ZERO;
4640 int64_t imm = 0;
4641 uint32_t imm_high = 0;
4642 uint32_t imm_low = 0;
4643 bool use_imm = rhs_location.IsConstant();
4644 if (use_imm) {
4645 imm = rhs_location.GetConstant()->AsLongConstant()->GetValue();
4646 imm_high = High32Bits(imm);
4647 imm_low = Low32Bits(imm);
4648 } else {
4649 rhs_high = rhs_location.AsRegisterPairHigh<Register>();
4650 rhs_low = rhs_location.AsRegisterPairLow<Register>();
4651 }
4652
4653 if (use_imm && imm == 0) {
4654 switch (cond) {
4655 case kCondEQ:
4656 case kCondBE: // <= 0 if zero
4657 __ Or(TMP, lhs_high, lhs_low);
4658 __ Beqz(TMP, label);
4659 break;
4660 case kCondNE:
4661 case kCondA: // > 0 if non-zero
4662 __ Or(TMP, lhs_high, lhs_low);
4663 __ Bnez(TMP, label);
4664 break;
4665 case kCondLT:
4666 __ Bltz(lhs_high, label);
4667 break;
4668 case kCondGE:
4669 __ Bgez(lhs_high, label);
4670 break;
4671 case kCondLE:
4672 __ Or(TMP, lhs_high, lhs_low);
4673 __ Sra(AT, lhs_high, 31);
4674 __ Bgeu(AT, TMP, label);
4675 break;
4676 case kCondGT:
4677 __ Or(TMP, lhs_high, lhs_low);
4678 __ Sra(AT, lhs_high, 31);
4679 __ Bltu(AT, TMP, label);
4680 break;
4681 case kCondB: // always false
4682 break;
4683 case kCondAE: // always true
4684 __ B(label);
4685 break;
4686 }
4687 } else if (use_imm) {
4688 // TODO: more efficient comparison with constants without loading them into TMP/AT.
4689 switch (cond) {
4690 case kCondEQ:
4691 __ LoadConst32(TMP, imm_high);
4692 __ Xor(TMP, TMP, lhs_high);
4693 __ LoadConst32(AT, imm_low);
4694 __ Xor(AT, AT, lhs_low);
4695 __ Or(TMP, TMP, AT);
4696 __ Beqz(TMP, label);
4697 break;
4698 case kCondNE:
4699 __ LoadConst32(TMP, imm_high);
4700 __ Xor(TMP, TMP, lhs_high);
4701 __ LoadConst32(AT, imm_low);
4702 __ Xor(AT, AT, lhs_low);
4703 __ Or(TMP, TMP, AT);
4704 __ Bnez(TMP, label);
4705 break;
4706 case kCondLT:
4707 __ LoadConst32(TMP, imm_high);
4708 __ Blt(lhs_high, TMP, label);
4709 __ Slt(TMP, TMP, lhs_high);
4710 __ LoadConst32(AT, imm_low);
4711 __ Sltu(AT, lhs_low, AT);
4712 __ Blt(TMP, AT, label);
4713 break;
4714 case kCondGE:
4715 __ LoadConst32(TMP, imm_high);
4716 __ Blt(TMP, lhs_high, label);
4717 __ Slt(TMP, lhs_high, TMP);
4718 __ LoadConst32(AT, imm_low);
4719 __ Sltu(AT, lhs_low, AT);
4720 __ Or(TMP, TMP, AT);
4721 __ Beqz(TMP, label);
4722 break;
4723 case kCondLE:
4724 __ LoadConst32(TMP, imm_high);
4725 __ Blt(lhs_high, TMP, label);
4726 __ Slt(TMP, TMP, lhs_high);
4727 __ LoadConst32(AT, imm_low);
4728 __ Sltu(AT, AT, lhs_low);
4729 __ Or(TMP, TMP, AT);
4730 __ Beqz(TMP, label);
4731 break;
4732 case kCondGT:
4733 __ LoadConst32(TMP, imm_high);
4734 __ Blt(TMP, lhs_high, label);
4735 __ Slt(TMP, lhs_high, TMP);
4736 __ LoadConst32(AT, imm_low);
4737 __ Sltu(AT, AT, lhs_low);
4738 __ Blt(TMP, AT, label);
4739 break;
4740 case kCondB:
4741 __ LoadConst32(TMP, imm_high);
4742 __ Bltu(lhs_high, TMP, label);
4743 __ Sltu(TMP, TMP, lhs_high);
4744 __ LoadConst32(AT, imm_low);
4745 __ Sltu(AT, lhs_low, AT);
4746 __ Blt(TMP, AT, label);
4747 break;
4748 case kCondAE:
4749 __ LoadConst32(TMP, imm_high);
4750 __ Bltu(TMP, lhs_high, label);
4751 __ Sltu(TMP, lhs_high, TMP);
4752 __ LoadConst32(AT, imm_low);
4753 __ Sltu(AT, lhs_low, AT);
4754 __ Or(TMP, TMP, AT);
4755 __ Beqz(TMP, label);
4756 break;
4757 case kCondBE:
4758 __ LoadConst32(TMP, imm_high);
4759 __ Bltu(lhs_high, TMP, label);
4760 __ Sltu(TMP, TMP, lhs_high);
4761 __ LoadConst32(AT, imm_low);
4762 __ Sltu(AT, AT, lhs_low);
4763 __ Or(TMP, TMP, AT);
4764 __ Beqz(TMP, label);
4765 break;
4766 case kCondA:
4767 __ LoadConst32(TMP, imm_high);
4768 __ Bltu(TMP, lhs_high, label);
4769 __ Sltu(TMP, lhs_high, TMP);
4770 __ LoadConst32(AT, imm_low);
4771 __ Sltu(AT, AT, lhs_low);
4772 __ Blt(TMP, AT, label);
4773 break;
4774 }
4775 } else {
4776 switch (cond) {
4777 case kCondEQ:
4778 __ Xor(TMP, lhs_high, rhs_high);
4779 __ Xor(AT, lhs_low, rhs_low);
4780 __ Or(TMP, TMP, AT);
4781 __ Beqz(TMP, label);
4782 break;
4783 case kCondNE:
4784 __ Xor(TMP, lhs_high, rhs_high);
4785 __ Xor(AT, lhs_low, rhs_low);
4786 __ Or(TMP, TMP, AT);
4787 __ Bnez(TMP, label);
4788 break;
4789 case kCondLT:
4790 __ Blt(lhs_high, rhs_high, label);
4791 __ Slt(TMP, rhs_high, lhs_high);
4792 __ Sltu(AT, lhs_low, rhs_low);
4793 __ Blt(TMP, AT, label);
4794 break;
4795 case kCondGE:
4796 __ Blt(rhs_high, lhs_high, label);
4797 __ Slt(TMP, lhs_high, rhs_high);
4798 __ Sltu(AT, lhs_low, rhs_low);
4799 __ Or(TMP, TMP, AT);
4800 __ Beqz(TMP, label);
4801 break;
4802 case kCondLE:
4803 __ Blt(lhs_high, rhs_high, label);
4804 __ Slt(TMP, rhs_high, lhs_high);
4805 __ Sltu(AT, rhs_low, lhs_low);
4806 __ Or(TMP, TMP, AT);
4807 __ Beqz(TMP, label);
4808 break;
4809 case kCondGT:
4810 __ Blt(rhs_high, lhs_high, label);
4811 __ Slt(TMP, lhs_high, rhs_high);
4812 __ Sltu(AT, rhs_low, lhs_low);
4813 __ Blt(TMP, AT, label);
4814 break;
4815 case kCondB:
4816 __ Bltu(lhs_high, rhs_high, label);
4817 __ Sltu(TMP, rhs_high, lhs_high);
4818 __ Sltu(AT, lhs_low, rhs_low);
4819 __ Blt(TMP, AT, label);
4820 break;
4821 case kCondAE:
4822 __ Bltu(rhs_high, lhs_high, label);
4823 __ Sltu(TMP, lhs_high, rhs_high);
4824 __ Sltu(AT, lhs_low, rhs_low);
4825 __ Or(TMP, TMP, AT);
4826 __ Beqz(TMP, label);
4827 break;
4828 case kCondBE:
4829 __ Bltu(lhs_high, rhs_high, label);
4830 __ Sltu(TMP, rhs_high, lhs_high);
4831 __ Sltu(AT, rhs_low, lhs_low);
4832 __ Or(TMP, TMP, AT);
4833 __ Beqz(TMP, label);
4834 break;
4835 case kCondA:
4836 __ Bltu(rhs_high, lhs_high, label);
4837 __ Sltu(TMP, lhs_high, rhs_high);
4838 __ Sltu(AT, rhs_low, lhs_low);
4839 __ Blt(TMP, AT, label);
4840 break;
4841 }
4842 }
4843}
4844
Alexey Frunze2ddb7172016-09-06 17:04:55 -07004845void InstructionCodeGeneratorMIPS::GenerateFpCompare(IfCondition cond,
4846 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004847 DataType::Type type,
Alexey Frunze2ddb7172016-09-06 17:04:55 -07004848 LocationSummary* locations) {
4849 Register dst = locations->Out().AsRegister<Register>();
4850 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
4851 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
4852 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004853 if (type == DataType::Type::kFloat32) {
Alexey Frunze2ddb7172016-09-06 17:04:55 -07004854 if (isR6) {
4855 switch (cond) {
4856 case kCondEQ:
4857 __ CmpEqS(FTMP, lhs, rhs);
4858 __ Mfc1(dst, FTMP);
4859 __ Andi(dst, dst, 1);
4860 break;
4861 case kCondNE:
4862 __ CmpEqS(FTMP, lhs, rhs);
4863 __ Mfc1(dst, FTMP);
4864 __ Addiu(dst, dst, 1);
4865 break;
4866 case kCondLT:
4867 if (gt_bias) {
4868 __ CmpLtS(FTMP, lhs, rhs);
4869 } else {
4870 __ CmpUltS(FTMP, lhs, rhs);
4871 }
4872 __ Mfc1(dst, FTMP);
4873 __ Andi(dst, dst, 1);
4874 break;
4875 case kCondLE:
4876 if (gt_bias) {
4877 __ CmpLeS(FTMP, lhs, rhs);
4878 } else {
4879 __ CmpUleS(FTMP, lhs, rhs);
4880 }
4881 __ Mfc1(dst, FTMP);
4882 __ Andi(dst, dst, 1);
4883 break;
4884 case kCondGT:
4885 if (gt_bias) {
4886 __ CmpUltS(FTMP, rhs, lhs);
4887 } else {
4888 __ CmpLtS(FTMP, rhs, lhs);
4889 }
4890 __ Mfc1(dst, FTMP);
4891 __ Andi(dst, dst, 1);
4892 break;
4893 case kCondGE:
4894 if (gt_bias) {
4895 __ CmpUleS(FTMP, rhs, lhs);
4896 } else {
4897 __ CmpLeS(FTMP, rhs, lhs);
4898 }
4899 __ Mfc1(dst, FTMP);
4900 __ Andi(dst, dst, 1);
4901 break;
4902 default:
4903 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
4904 UNREACHABLE();
4905 }
4906 } else {
4907 switch (cond) {
4908 case kCondEQ:
4909 __ CeqS(0, lhs, rhs);
4910 __ LoadConst32(dst, 1);
4911 __ Movf(dst, ZERO, 0);
4912 break;
4913 case kCondNE:
4914 __ CeqS(0, lhs, rhs);
4915 __ LoadConst32(dst, 1);
4916 __ Movt(dst, ZERO, 0);
4917 break;
4918 case kCondLT:
4919 if (gt_bias) {
4920 __ ColtS(0, lhs, rhs);
4921 } else {
4922 __ CultS(0, lhs, rhs);
4923 }
4924 __ LoadConst32(dst, 1);
4925 __ Movf(dst, ZERO, 0);
4926 break;
4927 case kCondLE:
4928 if (gt_bias) {
4929 __ ColeS(0, lhs, rhs);
4930 } else {
4931 __ CuleS(0, lhs, rhs);
4932 }
4933 __ LoadConst32(dst, 1);
4934 __ Movf(dst, ZERO, 0);
4935 break;
4936 case kCondGT:
4937 if (gt_bias) {
4938 __ CultS(0, rhs, lhs);
4939 } else {
4940 __ ColtS(0, rhs, lhs);
4941 }
4942 __ LoadConst32(dst, 1);
4943 __ Movf(dst, ZERO, 0);
4944 break;
4945 case kCondGE:
4946 if (gt_bias) {
4947 __ CuleS(0, rhs, lhs);
4948 } else {
4949 __ ColeS(0, rhs, lhs);
4950 }
4951 __ LoadConst32(dst, 1);
4952 __ Movf(dst, ZERO, 0);
4953 break;
4954 default:
4955 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
4956 UNREACHABLE();
4957 }
4958 }
4959 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004960 DCHECK_EQ(type, DataType::Type::kFloat64);
Alexey Frunze2ddb7172016-09-06 17:04:55 -07004961 if (isR6) {
4962 switch (cond) {
4963 case kCondEQ:
4964 __ CmpEqD(FTMP, lhs, rhs);
4965 __ Mfc1(dst, FTMP);
4966 __ Andi(dst, dst, 1);
4967 break;
4968 case kCondNE:
4969 __ CmpEqD(FTMP, lhs, rhs);
4970 __ Mfc1(dst, FTMP);
4971 __ Addiu(dst, dst, 1);
4972 break;
4973 case kCondLT:
4974 if (gt_bias) {
4975 __ CmpLtD(FTMP, lhs, rhs);
4976 } else {
4977 __ CmpUltD(FTMP, lhs, rhs);
4978 }
4979 __ Mfc1(dst, FTMP);
4980 __ Andi(dst, dst, 1);
4981 break;
4982 case kCondLE:
4983 if (gt_bias) {
4984 __ CmpLeD(FTMP, lhs, rhs);
4985 } else {
4986 __ CmpUleD(FTMP, lhs, rhs);
4987 }
4988 __ Mfc1(dst, FTMP);
4989 __ Andi(dst, dst, 1);
4990 break;
4991 case kCondGT:
4992 if (gt_bias) {
4993 __ CmpUltD(FTMP, rhs, lhs);
4994 } else {
4995 __ CmpLtD(FTMP, rhs, lhs);
4996 }
4997 __ Mfc1(dst, FTMP);
4998 __ Andi(dst, dst, 1);
4999 break;
5000 case kCondGE:
5001 if (gt_bias) {
5002 __ CmpUleD(FTMP, rhs, lhs);
5003 } else {
5004 __ CmpLeD(FTMP, rhs, lhs);
5005 }
5006 __ Mfc1(dst, FTMP);
5007 __ Andi(dst, dst, 1);
5008 break;
5009 default:
5010 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
5011 UNREACHABLE();
5012 }
5013 } else {
5014 switch (cond) {
5015 case kCondEQ:
5016 __ CeqD(0, lhs, rhs);
5017 __ LoadConst32(dst, 1);
5018 __ Movf(dst, ZERO, 0);
5019 break;
5020 case kCondNE:
5021 __ CeqD(0, lhs, rhs);
5022 __ LoadConst32(dst, 1);
5023 __ Movt(dst, ZERO, 0);
5024 break;
5025 case kCondLT:
5026 if (gt_bias) {
5027 __ ColtD(0, lhs, rhs);
5028 } else {
5029 __ CultD(0, lhs, rhs);
5030 }
5031 __ LoadConst32(dst, 1);
5032 __ Movf(dst, ZERO, 0);
5033 break;
5034 case kCondLE:
5035 if (gt_bias) {
5036 __ ColeD(0, lhs, rhs);
5037 } else {
5038 __ CuleD(0, lhs, rhs);
5039 }
5040 __ LoadConst32(dst, 1);
5041 __ Movf(dst, ZERO, 0);
5042 break;
5043 case kCondGT:
5044 if (gt_bias) {
5045 __ CultD(0, rhs, lhs);
5046 } else {
5047 __ ColtD(0, rhs, lhs);
5048 }
5049 __ LoadConst32(dst, 1);
5050 __ Movf(dst, ZERO, 0);
5051 break;
5052 case kCondGE:
5053 if (gt_bias) {
5054 __ CuleD(0, rhs, lhs);
5055 } else {
5056 __ ColeD(0, rhs, lhs);
5057 }
5058 __ LoadConst32(dst, 1);
5059 __ Movf(dst, ZERO, 0);
5060 break;
5061 default:
5062 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
5063 UNREACHABLE();
5064 }
5065 }
5066 }
5067}
5068
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005069bool InstructionCodeGeneratorMIPS::MaterializeFpCompareR2(IfCondition cond,
5070 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005071 DataType::Type type,
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005072 LocationSummary* input_locations,
5073 int cc) {
5074 FRegister lhs = input_locations->InAt(0).AsFpuRegister<FRegister>();
5075 FRegister rhs = input_locations->InAt(1).AsFpuRegister<FRegister>();
5076 CHECK(!codegen_->GetInstructionSetFeatures().IsR6());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005077 if (type == DataType::Type::kFloat32) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005078 switch (cond) {
5079 case kCondEQ:
5080 __ CeqS(cc, lhs, rhs);
5081 return false;
5082 case kCondNE:
5083 __ CeqS(cc, lhs, rhs);
5084 return true;
5085 case kCondLT:
5086 if (gt_bias) {
5087 __ ColtS(cc, lhs, rhs);
5088 } else {
5089 __ CultS(cc, lhs, rhs);
5090 }
5091 return false;
5092 case kCondLE:
5093 if (gt_bias) {
5094 __ ColeS(cc, lhs, rhs);
5095 } else {
5096 __ CuleS(cc, lhs, rhs);
5097 }
5098 return false;
5099 case kCondGT:
5100 if (gt_bias) {
5101 __ CultS(cc, rhs, lhs);
5102 } else {
5103 __ ColtS(cc, rhs, lhs);
5104 }
5105 return false;
5106 case kCondGE:
5107 if (gt_bias) {
5108 __ CuleS(cc, rhs, lhs);
5109 } else {
5110 __ ColeS(cc, rhs, lhs);
5111 }
5112 return false;
5113 default:
5114 LOG(FATAL) << "Unexpected non-floating-point condition";
5115 UNREACHABLE();
5116 }
5117 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005118 DCHECK_EQ(type, DataType::Type::kFloat64);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005119 switch (cond) {
5120 case kCondEQ:
5121 __ CeqD(cc, lhs, rhs);
5122 return false;
5123 case kCondNE:
5124 __ CeqD(cc, lhs, rhs);
5125 return true;
5126 case kCondLT:
5127 if (gt_bias) {
5128 __ ColtD(cc, lhs, rhs);
5129 } else {
5130 __ CultD(cc, lhs, rhs);
5131 }
5132 return false;
5133 case kCondLE:
5134 if (gt_bias) {
5135 __ ColeD(cc, lhs, rhs);
5136 } else {
5137 __ CuleD(cc, lhs, rhs);
5138 }
5139 return false;
5140 case kCondGT:
5141 if (gt_bias) {
5142 __ CultD(cc, rhs, lhs);
5143 } else {
5144 __ ColtD(cc, rhs, lhs);
5145 }
5146 return false;
5147 case kCondGE:
5148 if (gt_bias) {
5149 __ CuleD(cc, rhs, lhs);
5150 } else {
5151 __ ColeD(cc, rhs, lhs);
5152 }
5153 return false;
5154 default:
5155 LOG(FATAL) << "Unexpected non-floating-point condition";
5156 UNREACHABLE();
5157 }
5158 }
5159}
5160
5161bool InstructionCodeGeneratorMIPS::MaterializeFpCompareR6(IfCondition cond,
5162 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005163 DataType::Type type,
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005164 LocationSummary* input_locations,
5165 FRegister dst) {
5166 FRegister lhs = input_locations->InAt(0).AsFpuRegister<FRegister>();
5167 FRegister rhs = input_locations->InAt(1).AsFpuRegister<FRegister>();
5168 CHECK(codegen_->GetInstructionSetFeatures().IsR6());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005169 if (type == DataType::Type::kFloat32) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005170 switch (cond) {
5171 case kCondEQ:
5172 __ CmpEqS(dst, lhs, rhs);
5173 return false;
5174 case kCondNE:
5175 __ CmpEqS(dst, lhs, rhs);
5176 return true;
5177 case kCondLT:
5178 if (gt_bias) {
5179 __ CmpLtS(dst, lhs, rhs);
5180 } else {
5181 __ CmpUltS(dst, lhs, rhs);
5182 }
5183 return false;
5184 case kCondLE:
5185 if (gt_bias) {
5186 __ CmpLeS(dst, lhs, rhs);
5187 } else {
5188 __ CmpUleS(dst, lhs, rhs);
5189 }
5190 return false;
5191 case kCondGT:
5192 if (gt_bias) {
5193 __ CmpUltS(dst, rhs, lhs);
5194 } else {
5195 __ CmpLtS(dst, rhs, lhs);
5196 }
5197 return false;
5198 case kCondGE:
5199 if (gt_bias) {
5200 __ CmpUleS(dst, rhs, lhs);
5201 } else {
5202 __ CmpLeS(dst, rhs, lhs);
5203 }
5204 return false;
5205 default:
5206 LOG(FATAL) << "Unexpected non-floating-point condition";
5207 UNREACHABLE();
5208 }
5209 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005210 DCHECK_EQ(type, DataType::Type::kFloat64);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005211 switch (cond) {
5212 case kCondEQ:
5213 __ CmpEqD(dst, lhs, rhs);
5214 return false;
5215 case kCondNE:
5216 __ CmpEqD(dst, lhs, rhs);
5217 return true;
5218 case kCondLT:
5219 if (gt_bias) {
5220 __ CmpLtD(dst, lhs, rhs);
5221 } else {
5222 __ CmpUltD(dst, lhs, rhs);
5223 }
5224 return false;
5225 case kCondLE:
5226 if (gt_bias) {
5227 __ CmpLeD(dst, lhs, rhs);
5228 } else {
5229 __ CmpUleD(dst, lhs, rhs);
5230 }
5231 return false;
5232 case kCondGT:
5233 if (gt_bias) {
5234 __ CmpUltD(dst, rhs, lhs);
5235 } else {
5236 __ CmpLtD(dst, rhs, lhs);
5237 }
5238 return false;
5239 case kCondGE:
5240 if (gt_bias) {
5241 __ CmpUleD(dst, rhs, lhs);
5242 } else {
5243 __ CmpLeD(dst, rhs, lhs);
5244 }
5245 return false;
5246 default:
5247 LOG(FATAL) << "Unexpected non-floating-point condition";
5248 UNREACHABLE();
5249 }
5250 }
5251}
5252
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005253void InstructionCodeGeneratorMIPS::GenerateFpCompareAndBranch(IfCondition cond,
5254 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005255 DataType::Type type,
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005256 LocationSummary* locations,
5257 MipsLabel* label) {
5258 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
5259 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
5260 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005261 if (type == DataType::Type::kFloat32) {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005262 if (isR6) {
5263 switch (cond) {
5264 case kCondEQ:
5265 __ CmpEqS(FTMP, lhs, rhs);
5266 __ Bc1nez(FTMP, label);
5267 break;
5268 case kCondNE:
5269 __ CmpEqS(FTMP, lhs, rhs);
5270 __ Bc1eqz(FTMP, label);
5271 break;
5272 case kCondLT:
5273 if (gt_bias) {
5274 __ CmpLtS(FTMP, lhs, rhs);
5275 } else {
5276 __ CmpUltS(FTMP, lhs, rhs);
5277 }
5278 __ Bc1nez(FTMP, label);
5279 break;
5280 case kCondLE:
5281 if (gt_bias) {
5282 __ CmpLeS(FTMP, lhs, rhs);
5283 } else {
5284 __ CmpUleS(FTMP, lhs, rhs);
5285 }
5286 __ Bc1nez(FTMP, label);
5287 break;
5288 case kCondGT:
5289 if (gt_bias) {
5290 __ CmpUltS(FTMP, rhs, lhs);
5291 } else {
5292 __ CmpLtS(FTMP, rhs, lhs);
5293 }
5294 __ Bc1nez(FTMP, label);
5295 break;
5296 case kCondGE:
5297 if (gt_bias) {
5298 __ CmpUleS(FTMP, rhs, lhs);
5299 } else {
5300 __ CmpLeS(FTMP, rhs, lhs);
5301 }
5302 __ Bc1nez(FTMP, label);
5303 break;
5304 default:
5305 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005306 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005307 }
5308 } else {
5309 switch (cond) {
5310 case kCondEQ:
5311 __ CeqS(0, lhs, rhs);
5312 __ Bc1t(0, label);
5313 break;
5314 case kCondNE:
5315 __ CeqS(0, lhs, rhs);
5316 __ Bc1f(0, label);
5317 break;
5318 case kCondLT:
5319 if (gt_bias) {
5320 __ ColtS(0, lhs, rhs);
5321 } else {
5322 __ CultS(0, lhs, rhs);
5323 }
5324 __ Bc1t(0, label);
5325 break;
5326 case kCondLE:
5327 if (gt_bias) {
5328 __ ColeS(0, lhs, rhs);
5329 } else {
5330 __ CuleS(0, lhs, rhs);
5331 }
5332 __ Bc1t(0, label);
5333 break;
5334 case kCondGT:
5335 if (gt_bias) {
5336 __ CultS(0, rhs, lhs);
5337 } else {
5338 __ ColtS(0, rhs, lhs);
5339 }
5340 __ Bc1t(0, label);
5341 break;
5342 case kCondGE:
5343 if (gt_bias) {
5344 __ CuleS(0, rhs, lhs);
5345 } else {
5346 __ ColeS(0, rhs, lhs);
5347 }
5348 __ Bc1t(0, label);
5349 break;
5350 default:
5351 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005352 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005353 }
5354 }
5355 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005356 DCHECK_EQ(type, DataType::Type::kFloat64);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005357 if (isR6) {
5358 switch (cond) {
5359 case kCondEQ:
5360 __ CmpEqD(FTMP, lhs, rhs);
5361 __ Bc1nez(FTMP, label);
5362 break;
5363 case kCondNE:
5364 __ CmpEqD(FTMP, lhs, rhs);
5365 __ Bc1eqz(FTMP, label);
5366 break;
5367 case kCondLT:
5368 if (gt_bias) {
5369 __ CmpLtD(FTMP, lhs, rhs);
5370 } else {
5371 __ CmpUltD(FTMP, lhs, rhs);
5372 }
5373 __ Bc1nez(FTMP, label);
5374 break;
5375 case kCondLE:
5376 if (gt_bias) {
5377 __ CmpLeD(FTMP, lhs, rhs);
5378 } else {
5379 __ CmpUleD(FTMP, lhs, rhs);
5380 }
5381 __ Bc1nez(FTMP, label);
5382 break;
5383 case kCondGT:
5384 if (gt_bias) {
5385 __ CmpUltD(FTMP, rhs, lhs);
5386 } else {
5387 __ CmpLtD(FTMP, rhs, lhs);
5388 }
5389 __ Bc1nez(FTMP, label);
5390 break;
5391 case kCondGE:
5392 if (gt_bias) {
5393 __ CmpUleD(FTMP, rhs, lhs);
5394 } else {
5395 __ CmpLeD(FTMP, rhs, lhs);
5396 }
5397 __ Bc1nez(FTMP, label);
5398 break;
5399 default:
5400 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005401 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005402 }
5403 } else {
5404 switch (cond) {
5405 case kCondEQ:
5406 __ CeqD(0, lhs, rhs);
5407 __ Bc1t(0, label);
5408 break;
5409 case kCondNE:
5410 __ CeqD(0, lhs, rhs);
5411 __ Bc1f(0, label);
5412 break;
5413 case kCondLT:
5414 if (gt_bias) {
5415 __ ColtD(0, lhs, rhs);
5416 } else {
5417 __ CultD(0, lhs, rhs);
5418 }
5419 __ Bc1t(0, label);
5420 break;
5421 case kCondLE:
5422 if (gt_bias) {
5423 __ ColeD(0, lhs, rhs);
5424 } else {
5425 __ CuleD(0, lhs, rhs);
5426 }
5427 __ Bc1t(0, label);
5428 break;
5429 case kCondGT:
5430 if (gt_bias) {
5431 __ CultD(0, rhs, lhs);
5432 } else {
5433 __ ColtD(0, rhs, lhs);
5434 }
5435 __ Bc1t(0, label);
5436 break;
5437 case kCondGE:
5438 if (gt_bias) {
5439 __ CuleD(0, rhs, lhs);
5440 } else {
5441 __ ColeD(0, rhs, lhs);
5442 }
5443 __ Bc1t(0, label);
5444 break;
5445 default:
5446 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005447 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005448 }
5449 }
5450 }
5451}
5452
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005453void InstructionCodeGeneratorMIPS::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00005454 size_t condition_input_index,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005455 MipsLabel* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00005456 MipsLabel* false_target) {
5457 HInstruction* cond = instruction->InputAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005458
David Brazdil0debae72015-11-12 18:37:00 +00005459 if (true_target == nullptr && false_target == nullptr) {
5460 // Nothing to do. The code always falls through.
5461 return;
5462 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00005463 // Constant condition, statically compared against "true" (integer value 1).
5464 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00005465 if (true_target != nullptr) {
5466 __ B(true_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005467 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005468 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00005469 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00005470 if (false_target != nullptr) {
5471 __ B(false_target);
5472 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005473 }
David Brazdil0debae72015-11-12 18:37:00 +00005474 return;
5475 }
5476
5477 // The following code generates these patterns:
5478 // (1) true_target == nullptr && false_target != nullptr
5479 // - opposite condition true => branch to false_target
5480 // (2) true_target != nullptr && false_target == nullptr
5481 // - condition true => branch to true_target
5482 // (3) true_target != nullptr && false_target != nullptr
5483 // - condition true => branch to true_target
5484 // - branch to false_target
5485 if (IsBooleanValueOrMaterializedCondition(cond)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005486 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00005487 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005488 DCHECK(cond_val.IsRegister());
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005489 if (true_target == nullptr) {
David Brazdil0debae72015-11-12 18:37:00 +00005490 __ Beqz(cond_val.AsRegister<Register>(), false_target);
5491 } else {
5492 __ Bnez(cond_val.AsRegister<Register>(), true_target);
5493 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005494 } else {
5495 // The condition instruction has not been materialized, use its inputs as
5496 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00005497 HCondition* condition = cond->AsCondition();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005498 DataType::Type type = condition->InputAt(0)->GetType();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005499 LocationSummary* locations = cond->GetLocations();
5500 IfCondition if_cond = condition->GetCondition();
5501 MipsLabel* branch_target = true_target;
David Brazdil0debae72015-11-12 18:37:00 +00005502
David Brazdil0debae72015-11-12 18:37:00 +00005503 if (true_target == nullptr) {
5504 if_cond = condition->GetOppositeCondition();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005505 branch_target = false_target;
David Brazdil0debae72015-11-12 18:37:00 +00005506 }
5507
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005508 switch (type) {
5509 default:
5510 GenerateIntCompareAndBranch(if_cond, locations, branch_target);
5511 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005512 case DataType::Type::kInt64:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005513 GenerateLongCompareAndBranch(if_cond, locations, branch_target);
5514 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005515 case DataType::Type::kFloat32:
5516 case DataType::Type::kFloat64:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005517 GenerateFpCompareAndBranch(if_cond, condition->IsGtBias(), type, locations, branch_target);
5518 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005519 }
5520 }
David Brazdil0debae72015-11-12 18:37:00 +00005521
5522 // If neither branch falls through (case 3), the conditional branch to `true_target`
5523 // was already emitted (case 2) and we need to emit a jump to `false_target`.
5524 if (true_target != nullptr && false_target != nullptr) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005525 __ B(false_target);
5526 }
5527}
5528
5529void LocationsBuilderMIPS::VisitIf(HIf* if_instr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005530 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00005531 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005532 locations->SetInAt(0, Location::RequiresRegister());
5533 }
5534}
5535
5536void InstructionCodeGeneratorMIPS::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00005537 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
5538 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
5539 MipsLabel* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
5540 nullptr : codegen_->GetLabelOf(true_successor);
5541 MipsLabel* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
5542 nullptr : codegen_->GetLabelOf(false_successor);
5543 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005544}
5545
5546void LocationsBuilderMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005547 LocationSummary* locations = new (GetGraph()->GetAllocator())
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005548 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01005549 InvokeRuntimeCallingConvention calling_convention;
5550 RegisterSet caller_saves = RegisterSet::Empty();
5551 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5552 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00005553 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005554 locations->SetInAt(0, Location::RequiresRegister());
5555 }
5556}
5557
5558void InstructionCodeGeneratorMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08005559 SlowPathCodeMIPS* slow_path =
5560 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathMIPS>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00005561 GenerateTestAndBranch(deoptimize,
5562 /* condition_input_index */ 0,
5563 slow_path->GetEntryLabel(),
5564 /* false_target */ nullptr);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005565}
5566
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005567// This function returns true if a conditional move can be generated for HSelect.
5568// Otherwise it returns false and HSelect must be implemented in terms of conditonal
5569// branches and regular moves.
5570//
5571// If `locations_to_set` isn't nullptr, its inputs and outputs are set for HSelect.
5572//
5573// While determining feasibility of a conditional move and setting inputs/outputs
5574// are two distinct tasks, this function does both because they share quite a bit
5575// of common logic.
5576static bool CanMoveConditionally(HSelect* select, bool is_r6, LocationSummary* locations_to_set) {
5577 bool materialized = IsBooleanValueOrMaterializedCondition(select->GetCondition());
5578 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
5579 HCondition* condition = cond->AsCondition();
5580
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005581 DataType::Type cond_type =
5582 materialized ? DataType::Type::kInt32 : condition->InputAt(0)->GetType();
5583 DataType::Type dst_type = select->GetType();
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005584
5585 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
5586 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
5587 bool is_true_value_zero_constant =
5588 (cst_true_value != nullptr && cst_true_value->IsZeroBitPattern());
5589 bool is_false_value_zero_constant =
5590 (cst_false_value != nullptr && cst_false_value->IsZeroBitPattern());
5591
5592 bool can_move_conditionally = false;
5593 bool use_const_for_false_in = false;
5594 bool use_const_for_true_in = false;
5595
5596 if (!cond->IsConstant()) {
5597 switch (cond_type) {
5598 default:
5599 switch (dst_type) {
5600 default:
5601 // Moving int on int condition.
5602 if (is_r6) {
5603 if (is_true_value_zero_constant) {
5604 // seleqz out_reg, false_reg, cond_reg
5605 can_move_conditionally = true;
5606 use_const_for_true_in = true;
5607 } else if (is_false_value_zero_constant) {
5608 // selnez out_reg, true_reg, cond_reg
5609 can_move_conditionally = true;
5610 use_const_for_false_in = true;
5611 } else if (materialized) {
5612 // Not materializing unmaterialized int conditions
5613 // to keep the instruction count low.
5614 // selnez AT, true_reg, cond_reg
5615 // seleqz TMP, false_reg, cond_reg
5616 // or out_reg, AT, TMP
5617 can_move_conditionally = true;
5618 }
5619 } else {
5620 // movn out_reg, true_reg/ZERO, cond_reg
5621 can_move_conditionally = true;
5622 use_const_for_true_in = is_true_value_zero_constant;
5623 }
5624 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005625 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005626 // Moving long on int condition.
5627 if (is_r6) {
5628 if (is_true_value_zero_constant) {
5629 // seleqz out_reg_lo, false_reg_lo, cond_reg
5630 // seleqz out_reg_hi, false_reg_hi, cond_reg
5631 can_move_conditionally = true;
5632 use_const_for_true_in = true;
5633 } else if (is_false_value_zero_constant) {
5634 // selnez out_reg_lo, true_reg_lo, cond_reg
5635 // selnez out_reg_hi, true_reg_hi, cond_reg
5636 can_move_conditionally = true;
5637 use_const_for_false_in = true;
5638 }
5639 // Other long conditional moves would generate 6+ instructions,
5640 // which is too many.
5641 } else {
5642 // movn out_reg_lo, true_reg_lo/ZERO, cond_reg
5643 // movn out_reg_hi, true_reg_hi/ZERO, cond_reg
5644 can_move_conditionally = true;
5645 use_const_for_true_in = is_true_value_zero_constant;
5646 }
5647 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005648 case DataType::Type::kFloat32:
5649 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005650 // Moving float/double on int condition.
5651 if (is_r6) {
5652 if (materialized) {
5653 // Not materializing unmaterialized int conditions
5654 // to keep the instruction count low.
5655 can_move_conditionally = true;
5656 if (is_true_value_zero_constant) {
5657 // sltu TMP, ZERO, cond_reg
5658 // mtc1 TMP, temp_cond_reg
5659 // seleqz.fmt out_reg, false_reg, temp_cond_reg
5660 use_const_for_true_in = true;
5661 } else if (is_false_value_zero_constant) {
5662 // sltu TMP, ZERO, cond_reg
5663 // mtc1 TMP, temp_cond_reg
5664 // selnez.fmt out_reg, true_reg, temp_cond_reg
5665 use_const_for_false_in = true;
5666 } else {
5667 // sltu TMP, ZERO, cond_reg
5668 // mtc1 TMP, temp_cond_reg
5669 // sel.fmt temp_cond_reg, false_reg, true_reg
5670 // mov.fmt out_reg, temp_cond_reg
5671 }
5672 }
5673 } else {
5674 // movn.fmt out_reg, true_reg, cond_reg
5675 can_move_conditionally = true;
5676 }
5677 break;
5678 }
5679 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005680 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005681 // We don't materialize long comparison now
5682 // and use conditional branches instead.
5683 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005684 case DataType::Type::kFloat32:
5685 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005686 switch (dst_type) {
5687 default:
5688 // Moving int on float/double condition.
5689 if (is_r6) {
5690 if (is_true_value_zero_constant) {
5691 // mfc1 TMP, temp_cond_reg
5692 // seleqz out_reg, false_reg, TMP
5693 can_move_conditionally = true;
5694 use_const_for_true_in = true;
5695 } else if (is_false_value_zero_constant) {
5696 // mfc1 TMP, temp_cond_reg
5697 // selnez out_reg, true_reg, TMP
5698 can_move_conditionally = true;
5699 use_const_for_false_in = true;
5700 } else {
5701 // mfc1 TMP, temp_cond_reg
5702 // selnez AT, true_reg, TMP
5703 // seleqz TMP, false_reg, TMP
5704 // or out_reg, AT, TMP
5705 can_move_conditionally = true;
5706 }
5707 } else {
5708 // movt out_reg, true_reg/ZERO, cc
5709 can_move_conditionally = true;
5710 use_const_for_true_in = is_true_value_zero_constant;
5711 }
5712 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005713 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005714 // Moving long on float/double condition.
5715 if (is_r6) {
5716 if (is_true_value_zero_constant) {
5717 // mfc1 TMP, temp_cond_reg
5718 // seleqz out_reg_lo, false_reg_lo, TMP
5719 // seleqz out_reg_hi, false_reg_hi, TMP
5720 can_move_conditionally = true;
5721 use_const_for_true_in = true;
5722 } else if (is_false_value_zero_constant) {
5723 // mfc1 TMP, temp_cond_reg
5724 // selnez out_reg_lo, true_reg_lo, TMP
5725 // selnez out_reg_hi, true_reg_hi, TMP
5726 can_move_conditionally = true;
5727 use_const_for_false_in = true;
5728 }
5729 // Other long conditional moves would generate 6+ instructions,
5730 // which is too many.
5731 } else {
5732 // movt out_reg_lo, true_reg_lo/ZERO, cc
5733 // movt out_reg_hi, true_reg_hi/ZERO, cc
5734 can_move_conditionally = true;
5735 use_const_for_true_in = is_true_value_zero_constant;
5736 }
5737 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005738 case DataType::Type::kFloat32:
5739 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005740 // Moving float/double on float/double condition.
5741 if (is_r6) {
5742 can_move_conditionally = true;
5743 if (is_true_value_zero_constant) {
5744 // seleqz.fmt out_reg, false_reg, temp_cond_reg
5745 use_const_for_true_in = true;
5746 } else if (is_false_value_zero_constant) {
5747 // selnez.fmt out_reg, true_reg, temp_cond_reg
5748 use_const_for_false_in = true;
5749 } else {
5750 // sel.fmt temp_cond_reg, false_reg, true_reg
5751 // mov.fmt out_reg, temp_cond_reg
5752 }
5753 } else {
5754 // movt.fmt out_reg, true_reg, cc
5755 can_move_conditionally = true;
5756 }
5757 break;
5758 }
5759 break;
5760 }
5761 }
5762
5763 if (can_move_conditionally) {
5764 DCHECK(!use_const_for_false_in || !use_const_for_true_in);
5765 } else {
5766 DCHECK(!use_const_for_false_in);
5767 DCHECK(!use_const_for_true_in);
5768 }
5769
5770 if (locations_to_set != nullptr) {
5771 if (use_const_for_false_in) {
5772 locations_to_set->SetInAt(0, Location::ConstantLocation(cst_false_value));
5773 } else {
5774 locations_to_set->SetInAt(0,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005775 DataType::IsFloatingPointType(dst_type)
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005776 ? Location::RequiresFpuRegister()
5777 : Location::RequiresRegister());
5778 }
5779 if (use_const_for_true_in) {
5780 locations_to_set->SetInAt(1, Location::ConstantLocation(cst_true_value));
5781 } else {
5782 locations_to_set->SetInAt(1,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005783 DataType::IsFloatingPointType(dst_type)
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005784 ? Location::RequiresFpuRegister()
5785 : Location::RequiresRegister());
5786 }
5787 if (materialized) {
5788 locations_to_set->SetInAt(2, Location::RequiresRegister());
5789 }
5790 // On R6 we don't require the output to be the same as the
5791 // first input for conditional moves unlike on R2.
5792 bool is_out_same_as_first_in = !can_move_conditionally || !is_r6;
5793 if (is_out_same_as_first_in) {
5794 locations_to_set->SetOut(Location::SameAsFirstInput());
5795 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005796 locations_to_set->SetOut(DataType::IsFloatingPointType(dst_type)
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005797 ? Location::RequiresFpuRegister()
5798 : Location::RequiresRegister());
5799 }
5800 }
5801
5802 return can_move_conditionally;
5803}
5804
5805void InstructionCodeGeneratorMIPS::GenConditionalMoveR2(HSelect* select) {
5806 LocationSummary* locations = select->GetLocations();
5807 Location dst = locations->Out();
5808 Location src = locations->InAt(1);
5809 Register src_reg = ZERO;
5810 Register src_reg_high = ZERO;
5811 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
5812 Register cond_reg = TMP;
5813 int cond_cc = 0;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005814 DataType::Type cond_type = DataType::Type::kInt32;
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005815 bool cond_inverted = false;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005816 DataType::Type dst_type = select->GetType();
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005817
5818 if (IsBooleanValueOrMaterializedCondition(cond)) {
5819 cond_reg = locations->InAt(/* condition_input_index */ 2).AsRegister<Register>();
5820 } else {
5821 HCondition* condition = cond->AsCondition();
5822 LocationSummary* cond_locations = cond->GetLocations();
5823 IfCondition if_cond = condition->GetCondition();
5824 cond_type = condition->InputAt(0)->GetType();
5825 switch (cond_type) {
5826 default:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005827 DCHECK_NE(cond_type, DataType::Type::kInt64);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005828 cond_inverted = MaterializeIntCompare(if_cond, cond_locations, cond_reg);
5829 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005830 case DataType::Type::kFloat32:
5831 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005832 cond_inverted = MaterializeFpCompareR2(if_cond,
5833 condition->IsGtBias(),
5834 cond_type,
5835 cond_locations,
5836 cond_cc);
5837 break;
5838 }
5839 }
5840
5841 DCHECK(dst.Equals(locations->InAt(0)));
5842 if (src.IsRegister()) {
5843 src_reg = src.AsRegister<Register>();
5844 } else if (src.IsRegisterPair()) {
5845 src_reg = src.AsRegisterPairLow<Register>();
5846 src_reg_high = src.AsRegisterPairHigh<Register>();
5847 } else if (src.IsConstant()) {
5848 DCHECK(src.GetConstant()->IsZeroBitPattern());
5849 }
5850
5851 switch (cond_type) {
5852 default:
5853 switch (dst_type) {
5854 default:
5855 if (cond_inverted) {
5856 __ Movz(dst.AsRegister<Register>(), src_reg, cond_reg);
5857 } else {
5858 __ Movn(dst.AsRegister<Register>(), src_reg, cond_reg);
5859 }
5860 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005861 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005862 if (cond_inverted) {
5863 __ Movz(dst.AsRegisterPairLow<Register>(), src_reg, cond_reg);
5864 __ Movz(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_reg);
5865 } else {
5866 __ Movn(dst.AsRegisterPairLow<Register>(), src_reg, cond_reg);
5867 __ Movn(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_reg);
5868 }
5869 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005870 case DataType::Type::kFloat32:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005871 if (cond_inverted) {
5872 __ MovzS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
5873 } else {
5874 __ MovnS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
5875 }
5876 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005877 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005878 if (cond_inverted) {
5879 __ MovzD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
5880 } else {
5881 __ MovnD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
5882 }
5883 break;
5884 }
5885 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005886 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005887 LOG(FATAL) << "Unreachable";
5888 UNREACHABLE();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005889 case DataType::Type::kFloat32:
5890 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005891 switch (dst_type) {
5892 default:
5893 if (cond_inverted) {
5894 __ Movf(dst.AsRegister<Register>(), src_reg, cond_cc);
5895 } else {
5896 __ Movt(dst.AsRegister<Register>(), src_reg, cond_cc);
5897 }
5898 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005899 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005900 if (cond_inverted) {
5901 __ Movf(dst.AsRegisterPairLow<Register>(), src_reg, cond_cc);
5902 __ Movf(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_cc);
5903 } else {
5904 __ Movt(dst.AsRegisterPairLow<Register>(), src_reg, cond_cc);
5905 __ Movt(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_cc);
5906 }
5907 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005908 case DataType::Type::kFloat32:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005909 if (cond_inverted) {
5910 __ MovfS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
5911 } else {
5912 __ MovtS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
5913 }
5914 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005915 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005916 if (cond_inverted) {
5917 __ MovfD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
5918 } else {
5919 __ MovtD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
5920 }
5921 break;
5922 }
5923 break;
5924 }
5925}
5926
5927void InstructionCodeGeneratorMIPS::GenConditionalMoveR6(HSelect* select) {
5928 LocationSummary* locations = select->GetLocations();
5929 Location dst = locations->Out();
5930 Location false_src = locations->InAt(0);
5931 Location true_src = locations->InAt(1);
5932 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
5933 Register cond_reg = TMP;
5934 FRegister fcond_reg = FTMP;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005935 DataType::Type cond_type = DataType::Type::kInt32;
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005936 bool cond_inverted = false;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005937 DataType::Type dst_type = select->GetType();
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005938
5939 if (IsBooleanValueOrMaterializedCondition(cond)) {
5940 cond_reg = locations->InAt(/* condition_input_index */ 2).AsRegister<Register>();
5941 } else {
5942 HCondition* condition = cond->AsCondition();
5943 LocationSummary* cond_locations = cond->GetLocations();
5944 IfCondition if_cond = condition->GetCondition();
5945 cond_type = condition->InputAt(0)->GetType();
5946 switch (cond_type) {
5947 default:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005948 DCHECK_NE(cond_type, DataType::Type::kInt64);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005949 cond_inverted = MaterializeIntCompare(if_cond, cond_locations, cond_reg);
5950 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005951 case DataType::Type::kFloat32:
5952 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005953 cond_inverted = MaterializeFpCompareR6(if_cond,
5954 condition->IsGtBias(),
5955 cond_type,
5956 cond_locations,
5957 fcond_reg);
5958 break;
5959 }
5960 }
5961
5962 if (true_src.IsConstant()) {
5963 DCHECK(true_src.GetConstant()->IsZeroBitPattern());
5964 }
5965 if (false_src.IsConstant()) {
5966 DCHECK(false_src.GetConstant()->IsZeroBitPattern());
5967 }
5968
5969 switch (dst_type) {
5970 default:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005971 if (DataType::IsFloatingPointType(cond_type)) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005972 __ Mfc1(cond_reg, fcond_reg);
5973 }
5974 if (true_src.IsConstant()) {
5975 if (cond_inverted) {
5976 __ Selnez(dst.AsRegister<Register>(), false_src.AsRegister<Register>(), cond_reg);
5977 } else {
5978 __ Seleqz(dst.AsRegister<Register>(), false_src.AsRegister<Register>(), cond_reg);
5979 }
5980 } else if (false_src.IsConstant()) {
5981 if (cond_inverted) {
5982 __ Seleqz(dst.AsRegister<Register>(), true_src.AsRegister<Register>(), cond_reg);
5983 } else {
5984 __ Selnez(dst.AsRegister<Register>(), true_src.AsRegister<Register>(), cond_reg);
5985 }
5986 } else {
5987 DCHECK_NE(cond_reg, AT);
5988 if (cond_inverted) {
5989 __ Seleqz(AT, true_src.AsRegister<Register>(), cond_reg);
5990 __ Selnez(TMP, false_src.AsRegister<Register>(), cond_reg);
5991 } else {
5992 __ Selnez(AT, true_src.AsRegister<Register>(), cond_reg);
5993 __ Seleqz(TMP, false_src.AsRegister<Register>(), cond_reg);
5994 }
5995 __ Or(dst.AsRegister<Register>(), AT, TMP);
5996 }
5997 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005998 case DataType::Type::kInt64: {
5999 if (DataType::IsFloatingPointType(cond_type)) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006000 __ Mfc1(cond_reg, fcond_reg);
6001 }
6002 Register dst_lo = dst.AsRegisterPairLow<Register>();
6003 Register dst_hi = dst.AsRegisterPairHigh<Register>();
6004 if (true_src.IsConstant()) {
6005 Register src_lo = false_src.AsRegisterPairLow<Register>();
6006 Register src_hi = false_src.AsRegisterPairHigh<Register>();
6007 if (cond_inverted) {
6008 __ Selnez(dst_lo, src_lo, cond_reg);
6009 __ Selnez(dst_hi, src_hi, cond_reg);
6010 } else {
6011 __ Seleqz(dst_lo, src_lo, cond_reg);
6012 __ Seleqz(dst_hi, src_hi, cond_reg);
6013 }
6014 } else {
6015 DCHECK(false_src.IsConstant());
6016 Register src_lo = true_src.AsRegisterPairLow<Register>();
6017 Register src_hi = true_src.AsRegisterPairHigh<Register>();
6018 if (cond_inverted) {
6019 __ Seleqz(dst_lo, src_lo, cond_reg);
6020 __ Seleqz(dst_hi, src_hi, cond_reg);
6021 } else {
6022 __ Selnez(dst_lo, src_lo, cond_reg);
6023 __ Selnez(dst_hi, src_hi, cond_reg);
6024 }
6025 }
6026 break;
6027 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006028 case DataType::Type::kFloat32: {
6029 if (!DataType::IsFloatingPointType(cond_type)) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006030 // sel*.fmt tests bit 0 of the condition register, account for that.
6031 __ Sltu(TMP, ZERO, cond_reg);
6032 __ Mtc1(TMP, fcond_reg);
6033 }
6034 FRegister dst_reg = dst.AsFpuRegister<FRegister>();
6035 if (true_src.IsConstant()) {
6036 FRegister src_reg = false_src.AsFpuRegister<FRegister>();
6037 if (cond_inverted) {
6038 __ SelnezS(dst_reg, src_reg, fcond_reg);
6039 } else {
6040 __ SeleqzS(dst_reg, src_reg, fcond_reg);
6041 }
6042 } else if (false_src.IsConstant()) {
6043 FRegister src_reg = true_src.AsFpuRegister<FRegister>();
6044 if (cond_inverted) {
6045 __ SeleqzS(dst_reg, src_reg, fcond_reg);
6046 } else {
6047 __ SelnezS(dst_reg, src_reg, fcond_reg);
6048 }
6049 } else {
6050 if (cond_inverted) {
6051 __ SelS(fcond_reg,
6052 true_src.AsFpuRegister<FRegister>(),
6053 false_src.AsFpuRegister<FRegister>());
6054 } else {
6055 __ SelS(fcond_reg,
6056 false_src.AsFpuRegister<FRegister>(),
6057 true_src.AsFpuRegister<FRegister>());
6058 }
6059 __ MovS(dst_reg, fcond_reg);
6060 }
6061 break;
6062 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006063 case DataType::Type::kFloat64: {
6064 if (!DataType::IsFloatingPointType(cond_type)) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006065 // sel*.fmt tests bit 0 of the condition register, account for that.
6066 __ Sltu(TMP, ZERO, cond_reg);
6067 __ Mtc1(TMP, fcond_reg);
6068 }
6069 FRegister dst_reg = dst.AsFpuRegister<FRegister>();
6070 if (true_src.IsConstant()) {
6071 FRegister src_reg = false_src.AsFpuRegister<FRegister>();
6072 if (cond_inverted) {
6073 __ SelnezD(dst_reg, src_reg, fcond_reg);
6074 } else {
6075 __ SeleqzD(dst_reg, src_reg, fcond_reg);
6076 }
6077 } else if (false_src.IsConstant()) {
6078 FRegister src_reg = true_src.AsFpuRegister<FRegister>();
6079 if (cond_inverted) {
6080 __ SeleqzD(dst_reg, src_reg, fcond_reg);
6081 } else {
6082 __ SelnezD(dst_reg, src_reg, fcond_reg);
6083 }
6084 } else {
6085 if (cond_inverted) {
6086 __ SelD(fcond_reg,
6087 true_src.AsFpuRegister<FRegister>(),
6088 false_src.AsFpuRegister<FRegister>());
6089 } else {
6090 __ SelD(fcond_reg,
6091 false_src.AsFpuRegister<FRegister>(),
6092 true_src.AsFpuRegister<FRegister>());
6093 }
6094 __ MovD(dst_reg, fcond_reg);
6095 }
6096 break;
6097 }
6098 }
6099}
6100
Goran Jakovljevicc6418422016-12-05 16:31:55 +01006101void LocationsBuilderMIPS::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006102 LocationSummary* locations = new (GetGraph()->GetAllocator())
Goran Jakovljevicc6418422016-12-05 16:31:55 +01006103 LocationSummary(flag, LocationSummary::kNoCall);
6104 locations->SetOut(Location::RequiresRegister());
Mingyao Yang063fc772016-08-02 11:02:54 -07006105}
6106
Goran Jakovljevicc6418422016-12-05 16:31:55 +01006107void InstructionCodeGeneratorMIPS::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
6108 __ LoadFromOffset(kLoadWord,
6109 flag->GetLocations()->Out().AsRegister<Register>(),
6110 SP,
6111 codegen_->GetStackOffsetOfShouldDeoptimizeFlag());
Mingyao Yang063fc772016-08-02 11:02:54 -07006112}
6113
David Brazdil74eb1b22015-12-14 11:44:01 +00006114void LocationsBuilderMIPS::VisitSelect(HSelect* select) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006115 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(select);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006116 CanMoveConditionally(select, codegen_->GetInstructionSetFeatures().IsR6(), locations);
David Brazdil74eb1b22015-12-14 11:44:01 +00006117}
6118
6119void InstructionCodeGeneratorMIPS::VisitSelect(HSelect* select) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006120 bool is_r6 = codegen_->GetInstructionSetFeatures().IsR6();
6121 if (CanMoveConditionally(select, is_r6, /* locations_to_set */ nullptr)) {
6122 if (is_r6) {
6123 GenConditionalMoveR6(select);
6124 } else {
6125 GenConditionalMoveR2(select);
6126 }
6127 } else {
6128 LocationSummary* locations = select->GetLocations();
6129 MipsLabel false_target;
6130 GenerateTestAndBranch(select,
6131 /* condition_input_index */ 2,
6132 /* true_target */ nullptr,
6133 &false_target);
6134 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
6135 __ Bind(&false_target);
6136 }
David Brazdil74eb1b22015-12-14 11:44:01 +00006137}
6138
David Srbecky0cf44932015-12-09 14:09:59 +00006139void LocationsBuilderMIPS::VisitNativeDebugInfo(HNativeDebugInfo* info) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006140 new (GetGraph()->GetAllocator()) LocationSummary(info);
David Srbecky0cf44932015-12-09 14:09:59 +00006141}
6142
David Srbeckyd28f4a02016-03-14 17:14:24 +00006143void InstructionCodeGeneratorMIPS::VisitNativeDebugInfo(HNativeDebugInfo*) {
6144 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00006145}
6146
6147void CodeGeneratorMIPS::GenerateNop() {
6148 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00006149}
6150
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006151void LocationsBuilderMIPS::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006152 DataType::Type field_type = field_info.GetFieldType();
6153 bool is_wide = (field_type == DataType::Type::kInt64) || (field_type == DataType::Type::kFloat64);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006154 bool generate_volatile = field_info.IsVolatile() && is_wide;
Alexey Frunze15958152017-02-09 19:08:30 -08006155 bool object_field_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006156 kEmitCompilerReadBarrier && (field_type == DataType::Type::kReference);
Vladimir Markoca6fff82017-10-03 14:49:14 +01006157 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Alexey Frunze15958152017-02-09 19:08:30 -08006158 instruction,
6159 generate_volatile
6160 ? LocationSummary::kCallOnMainOnly
6161 : (object_field_get_with_read_barrier
6162 ? LocationSummary::kCallOnSlowPath
6163 : LocationSummary::kNoCall));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006164
Alexey Frunzec61c0762017-04-10 13:54:23 -07006165 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
6166 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
6167 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006168 locations->SetInAt(0, Location::RequiresRegister());
6169 if (generate_volatile) {
6170 InvokeRuntimeCallingConvention calling_convention;
6171 // need A0 to hold base + offset
6172 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006173 if (field_type == DataType::Type::kInt64) {
6174 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kInt64));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006175 } else {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006176 // Use Location::Any() to prevent situations when running out of available fp registers.
6177 locations->SetOut(Location::Any());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006178 // Need some temp core regs since FP results are returned in core registers
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006179 Location reg = calling_convention.GetReturnLocation(DataType::Type::kInt64);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006180 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairLow<Register>()));
6181 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairHigh<Register>()));
6182 }
6183 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006184 if (DataType::IsFloatingPointType(instruction->GetType())) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006185 locations->SetOut(Location::RequiresFpuRegister());
6186 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006187 // The output overlaps in the case of an object field get with
6188 // read barriers enabled: we do not want the move to overwrite the
6189 // object's location, as we need it to emit the read barrier.
6190 locations->SetOut(Location::RequiresRegister(),
6191 object_field_get_with_read_barrier
6192 ? Location::kOutputOverlap
6193 : Location::kNoOutputOverlap);
6194 }
6195 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
6196 // We need a temporary register for the read barrier marking slow
6197 // path in CodeGeneratorMIPS::GenerateFieldLoadWithBakerReadBarrier.
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006198 if (!kBakerReadBarrierThunksEnableForFields) {
6199 locations->AddTemp(Location::RequiresRegister());
6200 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006201 }
6202 }
6203}
6204
6205void InstructionCodeGeneratorMIPS::HandleFieldGet(HInstruction* instruction,
6206 const FieldInfo& field_info,
6207 uint32_t dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006208 DataType::Type type = field_info.GetFieldType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006209 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08006210 Location obj_loc = locations->InAt(0);
6211 Register obj = obj_loc.AsRegister<Register>();
6212 Location dst_loc = locations->Out();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006213 LoadOperandType load_type = kLoadUnsignedByte;
6214 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006215 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Tijana Jakovljevic57433862017-01-17 16:59:03 +01006216 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006217
6218 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006219 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006220 case DataType::Type::kUint8:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006221 load_type = kLoadUnsignedByte;
6222 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006223 case DataType::Type::kInt8:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006224 load_type = kLoadSignedByte;
6225 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006226 case DataType::Type::kUint16:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006227 load_type = kLoadUnsignedHalfword;
6228 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006229 case DataType::Type::kInt16:
6230 load_type = kLoadSignedHalfword;
6231 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006232 case DataType::Type::kInt32:
6233 case DataType::Type::kFloat32:
6234 case DataType::Type::kReference:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006235 load_type = kLoadWord;
6236 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006237 case DataType::Type::kInt64:
6238 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006239 load_type = kLoadDoubleword;
6240 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006241 case DataType::Type::kVoid:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006242 LOG(FATAL) << "Unreachable type " << type;
6243 UNREACHABLE();
6244 }
6245
6246 if (is_volatile && load_type == kLoadDoubleword) {
6247 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006248 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006249 // Do implicit Null check
6250 __ Lw(ZERO, locations->GetTemp(0).AsRegister<Register>(), 0);
6251 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Serban Constantinescufca16662016-07-14 09:21:59 +01006252 codegen_->InvokeRuntime(kQuickA64Load, instruction, dex_pc);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006253 CheckEntrypointTypes<kQuickA64Load, int64_t, volatile const int64_t*>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006254 if (type == DataType::Type::kFloat64) {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006255 // FP results are returned in core registers. Need to move them.
Alexey Frunze15958152017-02-09 19:08:30 -08006256 if (dst_loc.IsFpuRegister()) {
6257 __ Mtc1(locations->GetTemp(1).AsRegister<Register>(), dst_loc.AsFpuRegister<FRegister>());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006258 __ MoveToFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
Alexey Frunze15958152017-02-09 19:08:30 -08006259 dst_loc.AsFpuRegister<FRegister>());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006260 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006261 DCHECK(dst_loc.IsDoubleStackSlot());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006262 __ StoreToOffset(kStoreWord,
6263 locations->GetTemp(1).AsRegister<Register>(),
6264 SP,
Alexey Frunze15958152017-02-09 19:08:30 -08006265 dst_loc.GetStackIndex());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006266 __ StoreToOffset(kStoreWord,
6267 locations->GetTemp(2).AsRegister<Register>(),
6268 SP,
Alexey Frunze15958152017-02-09 19:08:30 -08006269 dst_loc.GetStackIndex() + 4);
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006270 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006271 }
6272 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006273 if (type == DataType::Type::kReference) {
Alexey Frunze15958152017-02-09 19:08:30 -08006274 // /* HeapReference<Object> */ dst = *(obj + offset)
6275 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006276 Location temp_loc =
6277 kBakerReadBarrierThunksEnableForFields ? Location::NoLocation() : locations->GetTemp(0);
Alexey Frunze15958152017-02-09 19:08:30 -08006278 // Note that a potential implicit null check is handled in this
6279 // CodeGeneratorMIPS::GenerateFieldLoadWithBakerReadBarrier call.
6280 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6281 dst_loc,
6282 obj,
6283 offset,
6284 temp_loc,
6285 /* needs_null_check */ true);
6286 if (is_volatile) {
6287 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6288 }
6289 } else {
6290 __ LoadFromOffset(kLoadWord, dst_loc.AsRegister<Register>(), obj, offset, null_checker);
6291 if (is_volatile) {
6292 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6293 }
6294 // If read barriers are enabled, emit read barriers other than
6295 // Baker's using a slow path (and also unpoison the loaded
6296 // reference, if heap poisoning is enabled).
6297 codegen_->MaybeGenerateReadBarrierSlow(instruction, dst_loc, dst_loc, obj_loc, offset);
6298 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006299 } else if (!DataType::IsFloatingPointType(type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006300 Register dst;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006301 if (type == DataType::Type::kInt64) {
Alexey Frunze15958152017-02-09 19:08:30 -08006302 DCHECK(dst_loc.IsRegisterPair());
6303 dst = dst_loc.AsRegisterPairLow<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006304 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006305 DCHECK(dst_loc.IsRegister());
6306 dst = dst_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006307 }
Alexey Frunze2923db72016-08-20 01:55:47 -07006308 __ LoadFromOffset(load_type, dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006309 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006310 DCHECK(dst_loc.IsFpuRegister());
6311 FRegister dst = dst_loc.AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006312 if (type == DataType::Type::kFloat32) {
Alexey Frunze2923db72016-08-20 01:55:47 -07006313 __ LoadSFromOffset(dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006314 } else {
Alexey Frunze2923db72016-08-20 01:55:47 -07006315 __ LoadDFromOffset(dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006316 }
6317 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006318 }
6319
Alexey Frunze15958152017-02-09 19:08:30 -08006320 // Memory barriers, in the case of references, are handled in the
6321 // previous switch statement.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006322 if (is_volatile && (type != DataType::Type::kReference)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006323 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6324 }
6325}
6326
6327void LocationsBuilderMIPS::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006328 DataType::Type field_type = field_info.GetFieldType();
6329 bool is_wide = (field_type == DataType::Type::kInt64) || (field_type == DataType::Type::kFloat64);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006330 bool generate_volatile = field_info.IsVolatile() && is_wide;
Vladimir Markoca6fff82017-10-03 14:49:14 +01006331 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006332 instruction, generate_volatile ? LocationSummary::kCallOnMainOnly : LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006333
6334 locations->SetInAt(0, Location::RequiresRegister());
6335 if (generate_volatile) {
6336 InvokeRuntimeCallingConvention calling_convention;
6337 // need A0 to hold base + offset
6338 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006339 if (field_type == DataType::Type::kInt64) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006340 locations->SetInAt(1, Location::RegisterPairLocation(
6341 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
6342 } else {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006343 // Use Location::Any() to prevent situations when running out of available fp registers.
6344 locations->SetInAt(1, Location::Any());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006345 // Pass FP parameters in core registers.
6346 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
6347 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(3)));
6348 }
6349 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006350 if (DataType::IsFloatingPointType(field_type)) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006351 locations->SetInAt(1, FpuRegisterOrConstantForStore(instruction->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006352 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006353 locations->SetInAt(1, RegisterOrZeroConstant(instruction->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006354 }
6355 }
6356}
6357
6358void InstructionCodeGeneratorMIPS::HandleFieldSet(HInstruction* instruction,
6359 const FieldInfo& field_info,
Goran Jakovljevice114da22016-12-26 14:21:43 +01006360 uint32_t dex_pc,
6361 bool value_can_be_null) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006362 DataType::Type type = field_info.GetFieldType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006363 LocationSummary* locations = instruction->GetLocations();
6364 Register obj = locations->InAt(0).AsRegister<Register>();
Alexey Frunzef58b2482016-09-02 22:14:06 -07006365 Location value_location = locations->InAt(1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006366 StoreOperandType store_type = kStoreByte;
6367 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006368 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Alexey Frunzec061de12017-02-14 13:27:23 -08006369 bool needs_write_barrier = CodeGenerator::StoreNeedsWriteBarrier(type, instruction->InputAt(1));
Tijana Jakovljevic57433862017-01-17 16:59:03 +01006370 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006371
6372 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006373 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006374 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006375 case DataType::Type::kInt8:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006376 store_type = kStoreByte;
6377 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006378 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006379 case DataType::Type::kInt16:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006380 store_type = kStoreHalfword;
6381 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006382 case DataType::Type::kInt32:
6383 case DataType::Type::kFloat32:
6384 case DataType::Type::kReference:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006385 store_type = kStoreWord;
6386 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006387 case DataType::Type::kInt64:
6388 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006389 store_type = kStoreDoubleword;
6390 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006391 case DataType::Type::kVoid:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006392 LOG(FATAL) << "Unreachable type " << type;
6393 UNREACHABLE();
6394 }
6395
6396 if (is_volatile) {
6397 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
6398 }
6399
6400 if (is_volatile && store_type == kStoreDoubleword) {
6401 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006402 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006403 // Do implicit Null check.
6404 __ Lw(ZERO, locations->GetTemp(0).AsRegister<Register>(), 0);
6405 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006406 if (type == DataType::Type::kFloat64) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006407 // Pass FP parameters in core registers.
Alexey Frunzef58b2482016-09-02 22:14:06 -07006408 if (value_location.IsFpuRegister()) {
6409 __ Mfc1(locations->GetTemp(1).AsRegister<Register>(),
6410 value_location.AsFpuRegister<FRegister>());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006411 __ MoveFromFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
Alexey Frunzef58b2482016-09-02 22:14:06 -07006412 value_location.AsFpuRegister<FRegister>());
6413 } else if (value_location.IsDoubleStackSlot()) {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006414 __ LoadFromOffset(kLoadWord,
6415 locations->GetTemp(1).AsRegister<Register>(),
6416 SP,
Alexey Frunzef58b2482016-09-02 22:14:06 -07006417 value_location.GetStackIndex());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006418 __ LoadFromOffset(kLoadWord,
6419 locations->GetTemp(2).AsRegister<Register>(),
6420 SP,
Alexey Frunzef58b2482016-09-02 22:14:06 -07006421 value_location.GetStackIndex() + 4);
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006422 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006423 DCHECK(value_location.IsConstant());
6424 DCHECK(value_location.GetConstant()->IsDoubleConstant());
6425 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006426 __ LoadConst64(locations->GetTemp(2).AsRegister<Register>(),
6427 locations->GetTemp(1).AsRegister<Register>(),
6428 value);
6429 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006430 }
Serban Constantinescufca16662016-07-14 09:21:59 +01006431 codegen_->InvokeRuntime(kQuickA64Store, instruction, dex_pc);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006432 CheckEntrypointTypes<kQuickA64Store, void, volatile int64_t *, int64_t>();
6433 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006434 if (value_location.IsConstant()) {
6435 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
6436 __ StoreConstToOffset(store_type, value, obj, offset, TMP, null_checker);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006437 } else if (!DataType::IsFloatingPointType(type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006438 Register src;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006439 if (type == DataType::Type::kInt64) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006440 src = value_location.AsRegisterPairLow<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006441 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006442 src = value_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006443 }
Alexey Frunzec061de12017-02-14 13:27:23 -08006444 if (kPoisonHeapReferences && needs_write_barrier) {
6445 // Note that in the case where `value` is a null reference,
6446 // we do not enter this block, as a null reference does not
6447 // need poisoning.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006448 DCHECK_EQ(type, DataType::Type::kReference);
Alexey Frunzec061de12017-02-14 13:27:23 -08006449 __ PoisonHeapReference(TMP, src);
6450 __ StoreToOffset(store_type, TMP, obj, offset, null_checker);
6451 } else {
6452 __ StoreToOffset(store_type, src, obj, offset, null_checker);
6453 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006454 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006455 FRegister src = value_location.AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006456 if (type == DataType::Type::kFloat32) {
Alexey Frunze2923db72016-08-20 01:55:47 -07006457 __ StoreSToOffset(src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006458 } else {
Alexey Frunze2923db72016-08-20 01:55:47 -07006459 __ StoreDToOffset(src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006460 }
6461 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006462 }
6463
Alexey Frunzec061de12017-02-14 13:27:23 -08006464 if (needs_write_barrier) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006465 Register src = value_location.AsRegister<Register>();
Goran Jakovljevice114da22016-12-26 14:21:43 +01006466 codegen_->MarkGCCard(obj, src, value_can_be_null);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006467 }
6468
6469 if (is_volatile) {
6470 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
6471 }
6472}
6473
6474void LocationsBuilderMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
6475 HandleFieldGet(instruction, instruction->GetFieldInfo());
6476}
6477
6478void InstructionCodeGeneratorMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
6479 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
6480}
6481
6482void LocationsBuilderMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
6483 HandleFieldSet(instruction, instruction->GetFieldInfo());
6484}
6485
6486void InstructionCodeGeneratorMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Goran Jakovljevice114da22016-12-26 14:21:43 +01006487 HandleFieldSet(instruction,
6488 instruction->GetFieldInfo(),
6489 instruction->GetDexPc(),
6490 instruction->GetValueCanBeNull());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006491}
6492
Alexey Frunze15958152017-02-09 19:08:30 -08006493void InstructionCodeGeneratorMIPS::GenerateReferenceLoadOneRegister(
6494 HInstruction* instruction,
6495 Location out,
6496 uint32_t offset,
6497 Location maybe_temp,
6498 ReadBarrierOption read_barrier_option) {
6499 Register out_reg = out.AsRegister<Register>();
6500 if (read_barrier_option == kWithReadBarrier) {
6501 CHECK(kEmitCompilerReadBarrier);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006502 if (!kUseBakerReadBarrier || !kBakerReadBarrierThunksEnableForFields) {
6503 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
6504 }
Alexey Frunze15958152017-02-09 19:08:30 -08006505 if (kUseBakerReadBarrier) {
6506 // Load with fast path based Baker's read barrier.
6507 // /* HeapReference<Object> */ out = *(out + offset)
6508 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6509 out,
6510 out_reg,
6511 offset,
6512 maybe_temp,
6513 /* needs_null_check */ false);
6514 } else {
6515 // Load with slow path based read barrier.
6516 // Save the value of `out` into `maybe_temp` before overwriting it
6517 // in the following move operation, as we will need it for the
6518 // read barrier below.
6519 __ Move(maybe_temp.AsRegister<Register>(), out_reg);
6520 // /* HeapReference<Object> */ out = *(out + offset)
6521 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
6522 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
6523 }
6524 } else {
6525 // Plain load with no read barrier.
6526 // /* HeapReference<Object> */ out = *(out + offset)
6527 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
6528 __ MaybeUnpoisonHeapReference(out_reg);
6529 }
6530}
6531
6532void InstructionCodeGeneratorMIPS::GenerateReferenceLoadTwoRegisters(
6533 HInstruction* instruction,
6534 Location out,
6535 Location obj,
6536 uint32_t offset,
6537 Location maybe_temp,
6538 ReadBarrierOption read_barrier_option) {
6539 Register out_reg = out.AsRegister<Register>();
6540 Register obj_reg = obj.AsRegister<Register>();
6541 if (read_barrier_option == kWithReadBarrier) {
6542 CHECK(kEmitCompilerReadBarrier);
6543 if (kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006544 if (!kBakerReadBarrierThunksEnableForFields) {
6545 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
6546 }
Alexey Frunze15958152017-02-09 19:08:30 -08006547 // Load with fast path based Baker's read barrier.
6548 // /* HeapReference<Object> */ out = *(obj + offset)
6549 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6550 out,
6551 obj_reg,
6552 offset,
6553 maybe_temp,
6554 /* needs_null_check */ false);
6555 } else {
6556 // Load with slow path based read barrier.
6557 // /* HeapReference<Object> */ out = *(obj + offset)
6558 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6559 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6560 }
6561 } else {
6562 // Plain load with no read barrier.
6563 // /* HeapReference<Object> */ out = *(obj + offset)
6564 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6565 __ MaybeUnpoisonHeapReference(out_reg);
6566 }
6567}
6568
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006569static inline int GetBakerMarkThunkNumber(Register reg) {
6570 static_assert(BAKER_MARK_INTROSPECTION_REGISTER_COUNT == 21, "Expecting equal");
6571 if (reg >= V0 && reg <= T7) { // 14 consequtive regs.
6572 return reg - V0;
6573 } else if (reg >= S2 && reg <= S7) { // 6 consequtive regs.
6574 return 14 + (reg - S2);
6575 } else if (reg == FP) { // One more.
6576 return 20;
6577 }
6578 LOG(FATAL) << "Unexpected register " << reg;
6579 UNREACHABLE();
6580}
6581
6582static inline int GetBakerMarkFieldArrayThunkDisplacement(Register reg, bool short_offset) {
6583 int num = GetBakerMarkThunkNumber(reg) +
6584 (short_offset ? BAKER_MARK_INTROSPECTION_REGISTER_COUNT : 0);
6585 return num * BAKER_MARK_INTROSPECTION_FIELD_ARRAY_ENTRY_SIZE;
6586}
6587
6588static inline int GetBakerMarkGcRootThunkDisplacement(Register reg) {
6589 return GetBakerMarkThunkNumber(reg) * BAKER_MARK_INTROSPECTION_GC_ROOT_ENTRY_SIZE +
6590 BAKER_MARK_INTROSPECTION_GC_ROOT_ENTRIES_OFFSET;
6591}
6592
Alexey Frunze15958152017-02-09 19:08:30 -08006593void InstructionCodeGeneratorMIPS::GenerateGcRootFieldLoad(HInstruction* instruction,
6594 Location root,
6595 Register obj,
6596 uint32_t offset,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006597 ReadBarrierOption read_barrier_option,
6598 MipsLabel* label_low) {
6599 bool reordering;
6600 if (label_low != nullptr) {
6601 DCHECK_EQ(offset, 0x5678u);
6602 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07006603 Register root_reg = root.AsRegister<Register>();
Alexey Frunze15958152017-02-09 19:08:30 -08006604 if (read_barrier_option == kWithReadBarrier) {
6605 DCHECK(kEmitCompilerReadBarrier);
6606 if (kUseBakerReadBarrier) {
6607 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6608 // Baker's read barrier are used:
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006609 if (kBakerReadBarrierThunksEnableForGcRoots) {
6610 // Note that we do not actually check the value of `GetIsGcMarking()`
6611 // to decide whether to mark the loaded GC root or not. Instead, we
6612 // load into `temp` (T9) the read barrier mark introspection entrypoint.
6613 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
6614 // vice versa.
6615 //
6616 // We use thunks for the slow path. That thunk checks the reference
6617 // and jumps to the entrypoint if needed.
6618 //
6619 // temp = Thread::Current()->pReadBarrierMarkReg00
6620 // // AKA &art_quick_read_barrier_mark_introspection.
6621 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
6622 // if (temp != nullptr) {
6623 // temp = &gc_root_thunk<root_reg>
6624 // root = temp(root)
6625 // }
Alexey Frunze15958152017-02-09 19:08:30 -08006626
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006627 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
6628 const int32_t entry_point_offset =
6629 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(0);
6630 const int thunk_disp = GetBakerMarkGcRootThunkDisplacement(root_reg);
6631 int16_t offset_low = Low16Bits(offset);
6632 int16_t offset_high = High16Bits(offset - offset_low); // Accounts for sign
6633 // extension in lw.
6634 bool short_offset = IsInt<16>(static_cast<int32_t>(offset));
6635 Register base = short_offset ? obj : TMP;
6636 // Loading the entrypoint does not require a load acquire since it is only changed when
6637 // threads are suspended or running a checkpoint.
6638 __ LoadFromOffset(kLoadWord, T9, TR, entry_point_offset);
6639 reordering = __ SetReorder(false);
6640 if (!short_offset) {
6641 DCHECK(!label_low);
6642 __ AddUpper(base, obj, offset_high);
6643 }
Alexey Frunze0cab6562017-07-25 15:19:36 -07006644 MipsLabel skip_call;
6645 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006646 if (label_low != nullptr) {
6647 DCHECK(short_offset);
6648 __ Bind(label_low);
6649 }
6650 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6651 __ LoadFromOffset(kLoadWord, root_reg, base, offset_low); // Single instruction
6652 // in delay slot.
6653 if (isR6) {
6654 __ Jialc(T9, thunk_disp);
6655 } else {
6656 __ Addiu(T9, T9, thunk_disp);
6657 __ Jalr(T9);
6658 __ Nop();
6659 }
Alexey Frunze0cab6562017-07-25 15:19:36 -07006660 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006661 __ SetReorder(reordering);
6662 } else {
6663 // Note that we do not actually check the value of `GetIsGcMarking()`
6664 // to decide whether to mark the loaded GC root or not. Instead, we
6665 // load into `temp` (T9) the read barrier mark entry point corresponding
6666 // to register `root`. If `temp` is null, it means that `GetIsGcMarking()`
6667 // is false, and vice versa.
6668 //
6669 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
6670 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
6671 // if (temp != null) {
6672 // root = temp(root)
6673 // }
Alexey Frunze15958152017-02-09 19:08:30 -08006674
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006675 if (label_low != nullptr) {
6676 reordering = __ SetReorder(false);
6677 __ Bind(label_low);
6678 }
6679 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6680 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
6681 if (label_low != nullptr) {
6682 __ SetReorder(reordering);
6683 }
6684 static_assert(
6685 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6686 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6687 "have different sizes.");
6688 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6689 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6690 "have different sizes.");
Alexey Frunze15958152017-02-09 19:08:30 -08006691
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006692 // Slow path marking the GC root `root`.
6693 Location temp = Location::RegisterLocation(T9);
6694 SlowPathCodeMIPS* slow_path =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006695 new (GetGraph()->GetAllocator()) ReadBarrierMarkSlowPathMIPS(
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006696 instruction,
6697 root,
6698 /*entrypoint*/ temp);
6699 codegen_->AddSlowPath(slow_path);
6700
6701 const int32_t entry_point_offset =
6702 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(root.reg() - 1);
6703 // Loading the entrypoint does not require a load acquire since it is only changed when
6704 // threads are suspended or running a checkpoint.
6705 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), TR, entry_point_offset);
6706 __ Bnez(temp.AsRegister<Register>(), slow_path->GetEntryLabel());
6707 __ Bind(slow_path->GetExitLabel());
6708 }
Alexey Frunze15958152017-02-09 19:08:30 -08006709 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006710 if (label_low != nullptr) {
6711 reordering = __ SetReorder(false);
6712 __ Bind(label_low);
6713 }
Alexey Frunze15958152017-02-09 19:08:30 -08006714 // GC root loaded through a slow path for read barriers other
6715 // than Baker's.
6716 // /* GcRoot<mirror::Object>* */ root = obj + offset
6717 __ Addiu32(root_reg, obj, offset);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006718 if (label_low != nullptr) {
6719 __ SetReorder(reordering);
6720 }
Alexey Frunze15958152017-02-09 19:08:30 -08006721 // /* mirror::Object* */ root = root->Read()
6722 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6723 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07006724 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006725 if (label_low != nullptr) {
6726 reordering = __ SetReorder(false);
6727 __ Bind(label_low);
6728 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07006729 // Plain GC root load with no read barrier.
6730 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6731 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
6732 // Note that GC roots are not affected by heap poisoning, thus we
6733 // do not have to unpoison `root_reg` here.
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006734 if (label_low != nullptr) {
6735 __ SetReorder(reordering);
6736 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07006737 }
6738}
6739
Alexey Frunze15958152017-02-09 19:08:30 -08006740void CodeGeneratorMIPS::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6741 Location ref,
6742 Register obj,
6743 uint32_t offset,
6744 Location temp,
6745 bool needs_null_check) {
6746 DCHECK(kEmitCompilerReadBarrier);
6747 DCHECK(kUseBakerReadBarrier);
6748
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006749 if (kBakerReadBarrierThunksEnableForFields) {
6750 // Note that we do not actually check the value of `GetIsGcMarking()`
6751 // to decide whether to mark the loaded reference or not. Instead, we
6752 // load into `temp` (T9) the read barrier mark introspection entrypoint.
6753 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
6754 // vice versa.
6755 //
6756 // We use thunks for the slow path. That thunk checks the reference
6757 // and jumps to the entrypoint if needed. If the holder is not gray,
6758 // it issues a load-load memory barrier and returns to the original
6759 // reference load.
6760 //
6761 // temp = Thread::Current()->pReadBarrierMarkReg00
6762 // // AKA &art_quick_read_barrier_mark_introspection.
6763 // if (temp != nullptr) {
6764 // temp = &field_array_thunk<holder_reg>
6765 // temp()
6766 // }
6767 // not_gray_return_address:
6768 // // If the offset is too large to fit into the lw instruction, we
6769 // // use an adjusted base register (TMP) here. This register
6770 // // receives bits 16 ... 31 of the offset before the thunk invocation
6771 // // and the thunk benefits from it.
6772 // HeapReference<mirror::Object> reference = *(obj+offset); // Original reference load.
6773 // gray_return_address:
6774
6775 DCHECK(temp.IsInvalid());
6776 bool isR6 = GetInstructionSetFeatures().IsR6();
6777 int16_t offset_low = Low16Bits(offset);
6778 int16_t offset_high = High16Bits(offset - offset_low); // Accounts for sign extension in lw.
6779 bool short_offset = IsInt<16>(static_cast<int32_t>(offset));
6780 bool reordering = __ SetReorder(false);
6781 const int32_t entry_point_offset =
6782 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(0);
6783 // There may have or may have not been a null check if the field offset is smaller than
6784 // the page size.
6785 // There must've been a null check in case it's actually a load from an array.
6786 // We will, however, perform an explicit null check in the thunk as it's easier to
6787 // do it than not.
6788 if (instruction->IsArrayGet()) {
6789 DCHECK(!needs_null_check);
6790 }
6791 const int thunk_disp = GetBakerMarkFieldArrayThunkDisplacement(obj, short_offset);
6792 // Loading the entrypoint does not require a load acquire since it is only changed when
6793 // threads are suspended or running a checkpoint.
6794 __ LoadFromOffset(kLoadWord, T9, TR, entry_point_offset);
6795 Register ref_reg = ref.AsRegister<Register>();
6796 Register base = short_offset ? obj : TMP;
Alexey Frunze0cab6562017-07-25 15:19:36 -07006797 MipsLabel skip_call;
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006798 if (short_offset) {
6799 if (isR6) {
Alexey Frunze0cab6562017-07-25 15:19:36 -07006800 __ Beqzc(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006801 __ Nop(); // In forbidden slot.
6802 __ Jialc(T9, thunk_disp);
6803 } else {
Alexey Frunze0cab6562017-07-25 15:19:36 -07006804 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006805 __ Addiu(T9, T9, thunk_disp); // In delay slot.
6806 __ Jalr(T9);
6807 __ Nop(); // In delay slot.
6808 }
Alexey Frunze0cab6562017-07-25 15:19:36 -07006809 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006810 } else {
6811 if (isR6) {
Alexey Frunze0cab6562017-07-25 15:19:36 -07006812 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006813 __ Aui(base, obj, offset_high); // In delay slot.
6814 __ Jialc(T9, thunk_disp);
Alexey Frunze0cab6562017-07-25 15:19:36 -07006815 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006816 } else {
6817 __ Lui(base, offset_high);
Alexey Frunze0cab6562017-07-25 15:19:36 -07006818 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006819 __ Addiu(T9, T9, thunk_disp); // In delay slot.
6820 __ Jalr(T9);
Alexey Frunze0cab6562017-07-25 15:19:36 -07006821 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006822 __ Addu(base, base, obj); // In delay slot.
6823 }
6824 }
6825 // /* HeapReference<Object> */ ref = *(obj + offset)
6826 __ LoadFromOffset(kLoadWord, ref_reg, base, offset_low); // Single instruction.
6827 if (needs_null_check) {
6828 MaybeRecordImplicitNullCheck(instruction);
6829 }
6830 __ MaybeUnpoisonHeapReference(ref_reg);
6831 __ SetReorder(reordering);
6832 return;
6833 }
6834
Alexey Frunze15958152017-02-09 19:08:30 -08006835 // /* HeapReference<Object> */ ref = *(obj + offset)
6836 Location no_index = Location::NoLocation();
6837 ScaleFactor no_scale_factor = TIMES_1;
6838 GenerateReferenceLoadWithBakerReadBarrier(instruction,
6839 ref,
6840 obj,
6841 offset,
6842 no_index,
6843 no_scale_factor,
6844 temp,
6845 needs_null_check);
6846}
6847
6848void CodeGeneratorMIPS::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6849 Location ref,
6850 Register obj,
6851 uint32_t data_offset,
6852 Location index,
6853 Location temp,
6854 bool needs_null_check) {
6855 DCHECK(kEmitCompilerReadBarrier);
6856 DCHECK(kUseBakerReadBarrier);
6857
6858 static_assert(
6859 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6860 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006861 ScaleFactor scale_factor = TIMES_4;
6862
6863 if (kBakerReadBarrierThunksEnableForArrays) {
6864 // Note that we do not actually check the value of `GetIsGcMarking()`
6865 // to decide whether to mark the loaded reference or not. Instead, we
6866 // load into `temp` (T9) the read barrier mark introspection entrypoint.
6867 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
6868 // vice versa.
6869 //
6870 // We use thunks for the slow path. That thunk checks the reference
6871 // and jumps to the entrypoint if needed. If the holder is not gray,
6872 // it issues a load-load memory barrier and returns to the original
6873 // reference load.
6874 //
6875 // temp = Thread::Current()->pReadBarrierMarkReg00
6876 // // AKA &art_quick_read_barrier_mark_introspection.
6877 // if (temp != nullptr) {
6878 // temp = &field_array_thunk<holder_reg>
6879 // temp()
6880 // }
6881 // not_gray_return_address:
6882 // // The element address is pre-calculated in the TMP register before the
6883 // // thunk invocation and the thunk benefits from it.
6884 // HeapReference<mirror::Object> reference = data[index]; // Original reference load.
6885 // gray_return_address:
6886
6887 DCHECK(temp.IsInvalid());
6888 DCHECK(index.IsValid());
6889 bool reordering = __ SetReorder(false);
6890 const int32_t entry_point_offset =
6891 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(0);
6892 // We will not do the explicit null check in the thunk as some form of a null check
6893 // must've been done earlier.
6894 DCHECK(!needs_null_check);
6895 const int thunk_disp = GetBakerMarkFieldArrayThunkDisplacement(obj, /* short_offset */ false);
6896 // Loading the entrypoint does not require a load acquire since it is only changed when
6897 // threads are suspended or running a checkpoint.
6898 __ LoadFromOffset(kLoadWord, T9, TR, entry_point_offset);
6899 Register ref_reg = ref.AsRegister<Register>();
6900 Register index_reg = index.IsRegisterPair()
6901 ? index.AsRegisterPairLow<Register>()
6902 : index.AsRegister<Register>();
Alexey Frunze0cab6562017-07-25 15:19:36 -07006903 MipsLabel skip_call;
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006904 if (GetInstructionSetFeatures().IsR6()) {
Alexey Frunze0cab6562017-07-25 15:19:36 -07006905 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006906 __ Lsa(TMP, index_reg, obj, scale_factor); // In delay slot.
6907 __ Jialc(T9, thunk_disp);
Alexey Frunze0cab6562017-07-25 15:19:36 -07006908 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006909 } else {
6910 __ Sll(TMP, index_reg, scale_factor);
Alexey Frunze0cab6562017-07-25 15:19:36 -07006911 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006912 __ Addiu(T9, T9, thunk_disp); // In delay slot.
6913 __ Jalr(T9);
Alexey Frunze0cab6562017-07-25 15:19:36 -07006914 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006915 __ Addu(TMP, TMP, obj); // In delay slot.
6916 }
6917 // /* HeapReference<Object> */ ref = *(obj + data_offset + (index << scale_factor))
6918 DCHECK(IsInt<16>(static_cast<int32_t>(data_offset))) << data_offset;
6919 __ LoadFromOffset(kLoadWord, ref_reg, TMP, data_offset); // Single instruction.
6920 __ MaybeUnpoisonHeapReference(ref_reg);
6921 __ SetReorder(reordering);
6922 return;
6923 }
6924
Alexey Frunze15958152017-02-09 19:08:30 -08006925 // /* HeapReference<Object> */ ref =
6926 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Alexey Frunze15958152017-02-09 19:08:30 -08006927 GenerateReferenceLoadWithBakerReadBarrier(instruction,
6928 ref,
6929 obj,
6930 data_offset,
6931 index,
6932 scale_factor,
6933 temp,
6934 needs_null_check);
6935}
6936
6937void CodeGeneratorMIPS::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6938 Location ref,
6939 Register obj,
6940 uint32_t offset,
6941 Location index,
6942 ScaleFactor scale_factor,
6943 Location temp,
6944 bool needs_null_check,
6945 bool always_update_field) {
6946 DCHECK(kEmitCompilerReadBarrier);
6947 DCHECK(kUseBakerReadBarrier);
6948
6949 // In slow path based read barriers, the read barrier call is
6950 // inserted after the original load. However, in fast path based
6951 // Baker's read barriers, we need to perform the load of
6952 // mirror::Object::monitor_ *before* the original reference load.
6953 // This load-load ordering is required by the read barrier.
6954 // The fast path/slow path (for Baker's algorithm) should look like:
6955 //
6956 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6957 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6958 // HeapReference<Object> ref = *src; // Original reference load.
6959 // bool is_gray = (rb_state == ReadBarrier::GrayState());
6960 // if (is_gray) {
6961 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
6962 // }
6963 //
6964 // Note: the original implementation in ReadBarrier::Barrier is
6965 // slightly more complex as it performs additional checks that we do
6966 // not do here for performance reasons.
6967
6968 Register ref_reg = ref.AsRegister<Register>();
6969 Register temp_reg = temp.AsRegister<Register>();
6970 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
6971
6972 // /* int32_t */ monitor = obj->monitor_
6973 __ LoadFromOffset(kLoadWord, temp_reg, obj, monitor_offset);
6974 if (needs_null_check) {
6975 MaybeRecordImplicitNullCheck(instruction);
6976 }
6977 // /* LockWord */ lock_word = LockWord(monitor)
6978 static_assert(sizeof(LockWord) == sizeof(int32_t),
6979 "art::LockWord and int32_t have different sizes.");
6980
6981 __ Sync(0); // Barrier to prevent load-load reordering.
6982
6983 // The actual reference load.
6984 if (index.IsValid()) {
6985 // Load types involving an "index": ArrayGet,
6986 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
6987 // intrinsics.
6988 // /* HeapReference<Object> */ ref = *(obj + offset + (index << scale_factor))
6989 if (index.IsConstant()) {
6990 size_t computed_offset =
6991 (index.GetConstant()->AsIntConstant()->GetValue() << scale_factor) + offset;
6992 __ LoadFromOffset(kLoadWord, ref_reg, obj, computed_offset);
6993 } else {
6994 // Handle the special case of the
6995 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
6996 // intrinsics, which use a register pair as index ("long
6997 // offset"), of which only the low part contains data.
6998 Register index_reg = index.IsRegisterPair()
6999 ? index.AsRegisterPairLow<Register>()
7000 : index.AsRegister<Register>();
Chris Larsencd0295d2017-03-31 15:26:54 -07007001 __ ShiftAndAdd(TMP, index_reg, obj, scale_factor, TMP);
Alexey Frunze15958152017-02-09 19:08:30 -08007002 __ LoadFromOffset(kLoadWord, ref_reg, TMP, offset);
7003 }
7004 } else {
7005 // /* HeapReference<Object> */ ref = *(obj + offset)
7006 __ LoadFromOffset(kLoadWord, ref_reg, obj, offset);
7007 }
7008
7009 // Object* ref = ref_addr->AsMirrorPtr()
7010 __ MaybeUnpoisonHeapReference(ref_reg);
7011
7012 // Slow path marking the object `ref` when it is gray.
7013 SlowPathCodeMIPS* slow_path;
7014 if (always_update_field) {
7015 // ReadBarrierMarkAndUpdateFieldSlowPathMIPS only supports address
7016 // of the form `obj + field_offset`, where `obj` is a register and
7017 // `field_offset` is a register pair (of which only the lower half
7018 // is used). Thus `offset` and `scale_factor` above are expected
7019 // to be null in this code path.
7020 DCHECK_EQ(offset, 0u);
7021 DCHECK_EQ(scale_factor, ScaleFactor::TIMES_1);
Vladimir Markoca6fff82017-10-03 14:49:14 +01007022 slow_path = new (GetGraph()->GetAllocator())
Alexey Frunze15958152017-02-09 19:08:30 -08007023 ReadBarrierMarkAndUpdateFieldSlowPathMIPS(instruction,
7024 ref,
7025 obj,
7026 /* field_offset */ index,
7027 temp_reg);
7028 } else {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007029 slow_path = new (GetGraph()->GetAllocator()) ReadBarrierMarkSlowPathMIPS(instruction, ref);
Alexey Frunze15958152017-02-09 19:08:30 -08007030 }
7031 AddSlowPath(slow_path);
7032
7033 // if (rb_state == ReadBarrier::GrayState())
7034 // ref = ReadBarrier::Mark(ref);
7035 // Given the numeric representation, it's enough to check the low bit of the
7036 // rb_state. We do that by shifting the bit into the sign bit (31) and
7037 // performing a branch on less than zero.
7038 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
7039 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
7040 static_assert(LockWord::kReadBarrierStateSize == 1, "Expecting 1-bit read barrier state size");
7041 __ Sll(temp_reg, temp_reg, 31 - LockWord::kReadBarrierStateShift);
7042 __ Bltz(temp_reg, slow_path->GetEntryLabel());
7043 __ Bind(slow_path->GetExitLabel());
7044}
7045
7046void CodeGeneratorMIPS::GenerateReadBarrierSlow(HInstruction* instruction,
7047 Location out,
7048 Location ref,
7049 Location obj,
7050 uint32_t offset,
7051 Location index) {
7052 DCHECK(kEmitCompilerReadBarrier);
7053
7054 // Insert a slow path based read barrier *after* the reference load.
7055 //
7056 // If heap poisoning is enabled, the unpoisoning of the loaded
7057 // reference will be carried out by the runtime within the slow
7058 // path.
7059 //
7060 // Note that `ref` currently does not get unpoisoned (when heap
7061 // poisoning is enabled), which is alright as the `ref` argument is
7062 // not used by the artReadBarrierSlow entry point.
7063 //
7064 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Vladimir Markoca6fff82017-10-03 14:49:14 +01007065 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetAllocator())
Alexey Frunze15958152017-02-09 19:08:30 -08007066 ReadBarrierForHeapReferenceSlowPathMIPS(instruction, out, ref, obj, offset, index);
7067 AddSlowPath(slow_path);
7068
7069 __ B(slow_path->GetEntryLabel());
7070 __ Bind(slow_path->GetExitLabel());
7071}
7072
7073void CodeGeneratorMIPS::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
7074 Location out,
7075 Location ref,
7076 Location obj,
7077 uint32_t offset,
7078 Location index) {
7079 if (kEmitCompilerReadBarrier) {
7080 // Baker's read barriers shall be handled by the fast path
7081 // (CodeGeneratorMIPS::GenerateReferenceLoadWithBakerReadBarrier).
7082 DCHECK(!kUseBakerReadBarrier);
7083 // If heap poisoning is enabled, unpoisoning will be taken care of
7084 // by the runtime within the slow path.
7085 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
7086 } else if (kPoisonHeapReferences) {
7087 __ UnpoisonHeapReference(out.AsRegister<Register>());
7088 }
7089}
7090
7091void CodeGeneratorMIPS::GenerateReadBarrierForRootSlow(HInstruction* instruction,
7092 Location out,
7093 Location root) {
7094 DCHECK(kEmitCompilerReadBarrier);
7095
7096 // Insert a slow path based read barrier *after* the GC root load.
7097 //
7098 // Note that GC roots are not affected by heap poisoning, so we do
7099 // not need to do anything special for this here.
7100 SlowPathCodeMIPS* slow_path =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007101 new (GetGraph()->GetAllocator()) ReadBarrierForRootSlowPathMIPS(instruction, out, root);
Alexey Frunze15958152017-02-09 19:08:30 -08007102 AddSlowPath(slow_path);
7103
7104 __ B(slow_path->GetEntryLabel());
7105 __ Bind(slow_path->GetExitLabel());
7106}
7107
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007108void LocationsBuilderMIPS::VisitInstanceOf(HInstanceOf* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007109 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
7110 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexey Frunzec61c0762017-04-10 13:54:23 -07007111 bool baker_read_barrier_slow_path = false;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007112 switch (type_check_kind) {
7113 case TypeCheckKind::kExactCheck:
7114 case TypeCheckKind::kAbstractClassCheck:
7115 case TypeCheckKind::kClassHierarchyCheck:
7116 case TypeCheckKind::kArrayObjectCheck:
Alexey Frunze15958152017-02-09 19:08:30 -08007117 call_kind =
7118 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Alexey Frunzec61c0762017-04-10 13:54:23 -07007119 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007120 break;
7121 case TypeCheckKind::kArrayCheck:
7122 case TypeCheckKind::kUnresolvedCheck:
7123 case TypeCheckKind::kInterfaceCheck:
7124 call_kind = LocationSummary::kCallOnSlowPath;
7125 break;
7126 }
7127
Vladimir Markoca6fff82017-10-03 14:49:14 +01007128 LocationSummary* locations =
7129 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Alexey Frunzec61c0762017-04-10 13:54:23 -07007130 if (baker_read_barrier_slow_path) {
7131 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
7132 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007133 locations->SetInAt(0, Location::RequiresRegister());
7134 locations->SetInAt(1, Location::RequiresRegister());
7135 // The output does overlap inputs.
7136 // Note that TypeCheckSlowPathMIPS uses this register too.
7137 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Alexey Frunze15958152017-02-09 19:08:30 -08007138 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007139}
7140
7141void InstructionCodeGeneratorMIPS::VisitInstanceOf(HInstanceOf* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007142 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007143 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08007144 Location obj_loc = locations->InAt(0);
7145 Register obj = obj_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007146 Register cls = locations->InAt(1).AsRegister<Register>();
Alexey Frunze15958152017-02-09 19:08:30 -08007147 Location out_loc = locations->Out();
7148 Register out = out_loc.AsRegister<Register>();
7149 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
7150 DCHECK_LE(num_temps, 1u);
7151 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007152 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
7153 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
7154 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
7155 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007156 MipsLabel done;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007157 SlowPathCodeMIPS* slow_path = nullptr;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007158
7159 // Return 0 if `obj` is null.
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007160 // Avoid this check if we know `obj` is not null.
7161 if (instruction->MustDoNullCheck()) {
7162 __ Move(out, ZERO);
7163 __ Beqz(obj, &done);
7164 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007165
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007166 switch (type_check_kind) {
7167 case TypeCheckKind::kExactCheck: {
7168 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007169 GenerateReferenceLoadTwoRegisters(instruction,
7170 out_loc,
7171 obj_loc,
7172 class_offset,
7173 maybe_temp_loc,
7174 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007175 // Classes must be equal for the instanceof to succeed.
7176 __ Xor(out, out, cls);
7177 __ Sltiu(out, out, 1);
7178 break;
7179 }
7180
7181 case TypeCheckKind::kAbstractClassCheck: {
7182 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007183 GenerateReferenceLoadTwoRegisters(instruction,
7184 out_loc,
7185 obj_loc,
7186 class_offset,
7187 maybe_temp_loc,
7188 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007189 // If the class is abstract, we eagerly fetch the super class of the
7190 // object to avoid doing a comparison we know will fail.
7191 MipsLabel loop;
7192 __ Bind(&loop);
7193 // /* HeapReference<Class> */ out = out->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08007194 GenerateReferenceLoadOneRegister(instruction,
7195 out_loc,
7196 super_offset,
7197 maybe_temp_loc,
7198 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007199 // If `out` is null, we use it for the result, and jump to `done`.
7200 __ Beqz(out, &done);
7201 __ Bne(out, cls, &loop);
7202 __ LoadConst32(out, 1);
7203 break;
7204 }
7205
7206 case TypeCheckKind::kClassHierarchyCheck: {
7207 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007208 GenerateReferenceLoadTwoRegisters(instruction,
7209 out_loc,
7210 obj_loc,
7211 class_offset,
7212 maybe_temp_loc,
7213 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007214 // Walk over the class hierarchy to find a match.
7215 MipsLabel loop, success;
7216 __ Bind(&loop);
7217 __ Beq(out, cls, &success);
7218 // /* HeapReference<Class> */ out = out->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08007219 GenerateReferenceLoadOneRegister(instruction,
7220 out_loc,
7221 super_offset,
7222 maybe_temp_loc,
7223 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007224 __ Bnez(out, &loop);
7225 // If `out` is null, we use it for the result, and jump to `done`.
7226 __ B(&done);
7227 __ Bind(&success);
7228 __ LoadConst32(out, 1);
7229 break;
7230 }
7231
7232 case TypeCheckKind::kArrayObjectCheck: {
7233 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007234 GenerateReferenceLoadTwoRegisters(instruction,
7235 out_loc,
7236 obj_loc,
7237 class_offset,
7238 maybe_temp_loc,
7239 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007240 // Do an exact check.
7241 MipsLabel success;
7242 __ Beq(out, cls, &success);
7243 // Otherwise, we need to check that the object's class is a non-primitive array.
7244 // /* HeapReference<Class> */ out = out->component_type_
Alexey Frunze15958152017-02-09 19:08:30 -08007245 GenerateReferenceLoadOneRegister(instruction,
7246 out_loc,
7247 component_offset,
7248 maybe_temp_loc,
7249 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007250 // If `out` is null, we use it for the result, and jump to `done`.
7251 __ Beqz(out, &done);
7252 __ LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
7253 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
7254 __ Sltiu(out, out, 1);
7255 __ B(&done);
7256 __ Bind(&success);
7257 __ LoadConst32(out, 1);
7258 break;
7259 }
7260
7261 case TypeCheckKind::kArrayCheck: {
7262 // No read barrier since the slow path will retry upon failure.
7263 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007264 GenerateReferenceLoadTwoRegisters(instruction,
7265 out_loc,
7266 obj_loc,
7267 class_offset,
7268 maybe_temp_loc,
7269 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007270 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Markoca6fff82017-10-03 14:49:14 +01007271 slow_path = new (GetGraph()->GetAllocator()) TypeCheckSlowPathMIPS(instruction,
7272 /* is_fatal */ false);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007273 codegen_->AddSlowPath(slow_path);
7274 __ Bne(out, cls, slow_path->GetEntryLabel());
7275 __ LoadConst32(out, 1);
7276 break;
7277 }
7278
7279 case TypeCheckKind::kUnresolvedCheck:
7280 case TypeCheckKind::kInterfaceCheck: {
7281 // Note that we indeed only call on slow path, but we always go
7282 // into the slow path for the unresolved and interface check
7283 // cases.
7284 //
7285 // We cannot directly call the InstanceofNonTrivial runtime
7286 // entry point without resorting to a type checking slow path
7287 // here (i.e. by calling InvokeRuntime directly), as it would
7288 // require to assign fixed registers for the inputs of this
7289 // HInstanceOf instruction (following the runtime calling
7290 // convention), which might be cluttered by the potential first
7291 // read barrier emission at the beginning of this method.
7292 //
7293 // TODO: Introduce a new runtime entry point taking the object
7294 // to test (instead of its class) as argument, and let it deal
7295 // with the read barrier issues. This will let us refactor this
7296 // case of the `switch` code as it was previously (with a direct
7297 // call to the runtime not using a type checking slow path).
7298 // This should also be beneficial for the other cases above.
7299 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Markoca6fff82017-10-03 14:49:14 +01007300 slow_path = new (GetGraph()->GetAllocator()) TypeCheckSlowPathMIPS(instruction,
7301 /* is_fatal */ false);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007302 codegen_->AddSlowPath(slow_path);
7303 __ B(slow_path->GetEntryLabel());
7304 break;
7305 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007306 }
7307
7308 __ Bind(&done);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007309
7310 if (slow_path != nullptr) {
7311 __ Bind(slow_path->GetExitLabel());
7312 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007313}
7314
7315void LocationsBuilderMIPS::VisitIntConstant(HIntConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007316 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007317 locations->SetOut(Location::ConstantLocation(constant));
7318}
7319
7320void InstructionCodeGeneratorMIPS::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
7321 // Will be generated at use site.
7322}
7323
7324void LocationsBuilderMIPS::VisitNullConstant(HNullConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007325 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007326 locations->SetOut(Location::ConstantLocation(constant));
7327}
7328
7329void InstructionCodeGeneratorMIPS::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
7330 // Will be generated at use site.
7331}
7332
7333void LocationsBuilderMIPS::HandleInvoke(HInvoke* invoke) {
7334 InvokeDexCallingConventionVisitorMIPS calling_convention_visitor;
7335 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
7336}
7337
7338void LocationsBuilderMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
7339 HandleInvoke(invoke);
Alexey Frunze1b8464d2016-11-12 17:22:05 -08007340 // The register T7 is required to be used for the hidden argument in
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007341 // art_quick_imt_conflict_trampoline, so add the hidden argument.
Alexey Frunze1b8464d2016-11-12 17:22:05 -08007342 invoke->GetLocations()->AddTemp(Location::RegisterLocation(T7));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007343}
7344
7345void InstructionCodeGeneratorMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
7346 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
7347 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007348 Location receiver = invoke->GetLocations()->InAt(0);
7349 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07007350 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007351
7352 // Set the hidden argument.
7353 __ LoadConst32(invoke->GetLocations()->GetTemp(1).AsRegister<Register>(),
7354 invoke->GetDexMethodIndex());
7355
7356 // temp = object->GetClass();
7357 if (receiver.IsStackSlot()) {
7358 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
7359 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
7360 } else {
7361 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
7362 }
7363 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08007364 // Instead of simply (possibly) unpoisoning `temp` here, we should
7365 // emit a read barrier for the previous class reference load.
7366 // However this is not required in practice, as this is an
7367 // intermediate/temporary reference and because the current
7368 // concurrent copying collector keeps the from-space memory
7369 // intact/accessible until the end of the marking phase (the
7370 // concurrent copying collector may not in the future).
7371 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00007372 __ LoadFromOffset(kLoadWord, temp, temp,
7373 mirror::Class::ImtPtrOffset(kMipsPointerSize).Uint32Value());
7374 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00007375 invoke->GetImtIndex(), kMipsPointerSize));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007376 // temp = temp->GetImtEntryAt(method_offset);
7377 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
7378 // T9 = temp->GetEntryPoint();
7379 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
7380 // T9();
7381 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07007382 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007383 DCHECK(!codegen_->IsLeafMethod());
7384 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
7385}
7386
7387void LocationsBuilderMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen701566a2015-10-27 15:29:13 -07007388 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
7389 if (intrinsic.TryDispatch(invoke)) {
7390 return;
7391 }
7392
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007393 HandleInvoke(invoke);
7394}
7395
7396void LocationsBuilderMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00007397 // Explicit clinit checks triggered by static invokes must have been pruned by
7398 // art::PrepareForRegisterAllocation.
7399 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007400
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007401 bool is_r6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007402 bool has_irreducible_loops = codegen_->GetGraph()->HasIrreducibleLoops();
7403 bool has_extra_input = invoke->HasPcRelativeMethodLoadKind() && !is_r6 && !has_irreducible_loops;
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007404
Chris Larsen701566a2015-10-27 15:29:13 -07007405 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
7406 if (intrinsic.TryDispatch(invoke)) {
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007407 if (invoke->GetLocations()->CanCall() && has_extra_input) {
7408 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
7409 }
Chris Larsen701566a2015-10-27 15:29:13 -07007410 return;
7411 }
7412
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007413 HandleInvoke(invoke);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007414
7415 // Add the extra input register if either the dex cache array base register
7416 // or the PC-relative base register for accessing literals is needed.
7417 if (has_extra_input) {
7418 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
7419 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007420}
7421
Orion Hodsonac141392017-01-13 11:53:47 +00007422void LocationsBuilderMIPS::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
7423 HandleInvoke(invoke);
7424}
7425
7426void InstructionCodeGeneratorMIPS::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
7427 codegen_->GenerateInvokePolymorphicCall(invoke);
7428}
7429
Chris Larsen701566a2015-10-27 15:29:13 -07007430static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorMIPS* codegen) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007431 if (invoke->GetLocations()->Intrinsified()) {
Chris Larsen701566a2015-10-27 15:29:13 -07007432 IntrinsicCodeGeneratorMIPS intrinsic(codegen);
7433 intrinsic.Dispatch(invoke);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007434 return true;
7435 }
7436 return false;
7437}
7438
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007439HLoadString::LoadKind CodeGeneratorMIPS::GetSupportedLoadStringKind(
Alexey Frunze06a46c42016-07-19 15:00:40 -07007440 HLoadString::LoadKind desired_string_load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007441 switch (desired_string_load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007442 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007443 case HLoadString::LoadKind::kBootImageInternTable:
Vladimir Markoaad75c62016-10-03 08:46:48 +00007444 case HLoadString::LoadKind::kBssEntry:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007445 DCHECK(!Runtime::Current()->UseJitCompilation());
Alexey Frunze06a46c42016-07-19 15:00:40 -07007446 break;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007447 case HLoadString::LoadKind::kJitTableAddress:
7448 DCHECK(Runtime::Current()->UseJitCompilation());
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007449 break;
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007450 case HLoadString::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007451 case HLoadString::LoadKind::kRuntimeCall:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007452 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007453 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007454 return desired_string_load_kind;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007455}
7456
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007457HLoadClass::LoadKind CodeGeneratorMIPS::GetSupportedLoadClassKind(
7458 HLoadClass::LoadKind desired_class_load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007459 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00007460 case HLoadClass::LoadKind::kInvalid:
7461 LOG(FATAL) << "UNREACHABLE";
7462 UNREACHABLE();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007463 case HLoadClass::LoadKind::kReferrersClass:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007464 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007465 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko94ec2db2017-09-06 17:21:03 +01007466 case HLoadClass::LoadKind::kBootImageClassTable:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007467 case HLoadClass::LoadKind::kBssEntry:
7468 DCHECK(!Runtime::Current()->UseJitCompilation());
7469 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007470 case HLoadClass::LoadKind::kJitTableAddress:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007471 DCHECK(Runtime::Current()->UseJitCompilation());
Alexey Frunze06a46c42016-07-19 15:00:40 -07007472 break;
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007473 case HLoadClass::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007474 case HLoadClass::LoadKind::kRuntimeCall:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007475 break;
7476 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007477 return desired_class_load_kind;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007478}
7479
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007480Register CodeGeneratorMIPS::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
7481 Register temp) {
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007482 CHECK(!GetInstructionSetFeatures().IsR6());
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007483 CHECK(!GetGraph()->HasIrreducibleLoops());
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007484 CHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
7485 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
7486 if (!invoke->GetLocations()->Intrinsified()) {
7487 return location.AsRegister<Register>();
7488 }
7489 // For intrinsics we allow any location, so it may be on the stack.
7490 if (!location.IsRegister()) {
7491 __ LoadFromOffset(kLoadWord, temp, SP, location.GetStackIndex());
7492 return temp;
7493 }
7494 // For register locations, check if the register was saved. If so, get it from the stack.
7495 // Note: There is a chance that the register was saved but not overwritten, so we could
7496 // save one load. However, since this is just an intrinsic slow path we prefer this
7497 // simple and more robust approach rather that trying to determine if that's the case.
7498 SlowPathCode* slow_path = GetCurrentSlowPath();
7499 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
7500 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
7501 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
7502 __ LoadFromOffset(kLoadWord, temp, SP, stack_offset);
7503 return temp;
7504 }
7505 return location.AsRegister<Register>();
7506}
7507
Vladimir Markodc151b22015-10-15 18:02:30 +01007508HInvokeStaticOrDirect::DispatchInfo CodeGeneratorMIPS::GetSupportedInvokeStaticOrDirectDispatch(
7509 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01007510 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007511 return desired_dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01007512}
7513
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007514void CodeGeneratorMIPS::GenerateStaticOrDirectCall(
7515 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007516 // All registers are assumed to be correctly set up per the calling convention.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007517 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007518 HInvokeStaticOrDirect::MethodLoadKind method_load_kind = invoke->GetMethodLoadKind();
7519 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location = invoke->GetCodePtrLocation();
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007520 bool is_r6 = GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007521 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
7522 Register base_reg = (invoke->HasPcRelativeMethodLoadKind() && !is_r6 && !has_irreducible_loops)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007523 ? GetInvokeStaticOrDirectExtraParameter(invoke, temp.AsRegister<Register>())
7524 : ZERO;
7525
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007526 switch (method_load_kind) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007527 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007528 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007529 uint32_t offset =
7530 GetThreadOffset<kMipsPointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007531 __ LoadFromOffset(kLoadWord,
7532 temp.AsRegister<Register>(),
7533 TR,
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007534 offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007535 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007536 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007537 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00007538 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007539 break;
Vladimir Marko65979462017-05-19 17:25:12 +01007540 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative: {
7541 DCHECK(GetCompilerOptions().IsBootImage());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007542 PcRelativePatchInfo* info_high = NewPcRelativeMethodPatch(invoke->GetTargetMethod());
7543 PcRelativePatchInfo* info_low =
7544 NewPcRelativeMethodPatch(invoke->GetTargetMethod(), info_high);
Vladimir Marko65979462017-05-19 17:25:12 +01007545 Register temp_reg = temp.AsRegister<Register>();
Alexey Frunzea663d9d2017-07-31 18:43:18 -07007546 EmitPcRelativeAddressPlaceholderHigh(info_high, TMP, base_reg);
7547 __ Addiu(temp_reg, TMP, /* placeholder */ 0x5678, &info_low->label);
Vladimir Marko65979462017-05-19 17:25:12 +01007548 break;
7549 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007550 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
7551 __ LoadConst32(temp.AsRegister<Register>(), invoke->GetMethodAddress());
7552 break;
Vladimir Marko0eb882b2017-05-15 13:39:18 +01007553 case HInvokeStaticOrDirect::MethodLoadKind::kBssEntry: {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007554 PcRelativePatchInfo* info_high = NewMethodBssEntryPatch(
Vladimir Marko0eb882b2017-05-15 13:39:18 +01007555 MethodReference(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex()));
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007556 PcRelativePatchInfo* info_low = NewMethodBssEntryPatch(
7557 MethodReference(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex()), info_high);
Vladimir Marko0eb882b2017-05-15 13:39:18 +01007558 Register temp_reg = temp.AsRegister<Register>();
Alexey Frunzea663d9d2017-07-31 18:43:18 -07007559 EmitPcRelativeAddressPlaceholderHigh(info_high, TMP, base_reg);
7560 __ Lw(temp_reg, TMP, /* placeholder */ 0x5678, &info_low->label);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007561 break;
Vladimir Marko0eb882b2017-05-15 13:39:18 +01007562 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007563 case HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall: {
7564 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
7565 return; // No code pointer retrieval; the runtime performs the call directly.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007566 }
7567 }
7568
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007569 switch (code_ptr_location) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007570 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007571 __ Bal(&frame_entry_label_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007572 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007573 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
7574 // T9 = callee_method->entry_point_from_quick_compiled_code_;
Goran Jakovljevic1a878372015-10-26 14:28:52 +01007575 __ LoadFromOffset(kLoadWord,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007576 T9,
7577 callee_method.AsRegister<Register>(),
7578 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07007579 kMipsPointerSize).Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007580 // T9()
7581 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07007582 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007583 break;
7584 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007585 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
7586
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007587 DCHECK(!IsLeafMethod());
7588}
7589
7590void InstructionCodeGeneratorMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00007591 // Explicit clinit checks triggered by static invokes must have been pruned by
7592 // art::PrepareForRegisterAllocation.
7593 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007594
7595 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
7596 return;
7597 }
7598
7599 LocationSummary* locations = invoke->GetLocations();
7600 codegen_->GenerateStaticOrDirectCall(invoke,
7601 locations->HasTemps()
7602 ? locations->GetTemp(0)
7603 : Location::NoLocation());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007604}
7605
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007606void CodeGeneratorMIPS::GenerateVirtualCall(
7607 HInvokeVirtual* invoke, Location temp_location, SlowPathCode* slow_path) {
Goran Jakovljevice919b072016-10-04 10:17:34 +02007608 // Use the calling convention instead of the location of the receiver, as
7609 // intrinsics may have put the receiver in a different register. In the intrinsics
7610 // slow path, the arguments have been moved to the right place, so here we are
7611 // guaranteed that the receiver is the first register of the calling convention.
7612 InvokeDexCallingConvention calling_convention;
7613 Register receiver = calling_convention.GetRegisterAt(0);
7614
Chris Larsen3acee732015-11-18 13:31:08 -08007615 Register temp = temp_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007616 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
7617 invoke->GetVTableIndex(), kMipsPointerSize).SizeValue();
7618 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07007619 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007620
7621 // temp = object->GetClass();
Goran Jakovljevice919b072016-10-04 10:17:34 +02007622 __ LoadFromOffset(kLoadWord, temp, receiver, class_offset);
Chris Larsen3acee732015-11-18 13:31:08 -08007623 MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08007624 // Instead of simply (possibly) unpoisoning `temp` here, we should
7625 // emit a read barrier for the previous class reference load.
7626 // However this is not required in practice, as this is an
7627 // intermediate/temporary reference and because the current
7628 // concurrent copying collector keeps the from-space memory
7629 // intact/accessible until the end of the marking phase (the
7630 // concurrent copying collector may not in the future).
7631 __ MaybeUnpoisonHeapReference(temp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007632 // temp = temp->GetMethodAt(method_offset);
7633 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
7634 // T9 = temp->GetEntryPoint();
7635 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
7636 // T9();
7637 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07007638 __ NopIfNoReordering();
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007639 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Chris Larsen3acee732015-11-18 13:31:08 -08007640}
7641
7642void InstructionCodeGeneratorMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
7643 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
7644 return;
7645 }
7646
7647 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007648 DCHECK(!codegen_->IsLeafMethod());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007649}
7650
7651void LocationsBuilderMIPS::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00007652 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007653 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007654 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07007655 Location loc = Location::RegisterLocation(calling_convention.GetRegisterAt(0));
7656 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(cls, loc, loc);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007657 return;
7658 }
Vladimir Marko41559982017-01-06 14:04:23 +00007659 DCHECK(!cls->NeedsAccessCheck());
Alexey Frunzec61c0762017-04-10 13:54:23 -07007660 const bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007661 const bool has_irreducible_loops = codegen_->GetGraph()->HasIrreducibleLoops();
Alexey Frunze15958152017-02-09 19:08:30 -08007662 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
7663 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Alexey Frunze06a46c42016-07-19 15:00:40 -07007664 ? LocationSummary::kCallOnSlowPath
7665 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01007666 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(cls, call_kind);
Alexey Frunzec61c0762017-04-10 13:54:23 -07007667 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
7668 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
7669 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007670 switch (load_kind) {
7671 // We need an extra register for PC-relative literals on R2.
Alexey Frunze06a46c42016-07-19 15:00:40 -07007672 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007673 case HLoadClass::LoadKind::kBootImageAddress:
Vladimir Marko94ec2db2017-09-06 17:21:03 +01007674 case HLoadClass::LoadKind::kBootImageClassTable:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007675 case HLoadClass::LoadKind::kBssEntry:
Alexey Frunzec61c0762017-04-10 13:54:23 -07007676 if (isR6) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007677 break;
7678 }
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007679 if (has_irreducible_loops) {
Alexey Frunze3b8c82f2017-10-10 23:01:34 -07007680 if (load_kind != HLoadClass::LoadKind::kBootImageAddress) {
7681 codegen_->ClobberRA();
7682 }
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007683 break;
7684 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007685 FALLTHROUGH_INTENDED;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007686 case HLoadClass::LoadKind::kReferrersClass:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007687 locations->SetInAt(0, Location::RequiresRegister());
7688 break;
7689 default:
7690 break;
7691 }
7692 locations->SetOut(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07007693 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
7694 if (!kUseReadBarrier || kUseBakerReadBarrier) {
7695 // Rely on the type resolution or initialization and marking to save everything we need.
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007696 // Request a temp to hold the BSS entry location for the slow path.
7697 locations->AddTemp(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07007698 RegisterSet caller_saves = RegisterSet::Empty();
7699 InvokeRuntimeCallingConvention calling_convention;
7700 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
7701 locations->SetCustomSlowPathCallerSaves(caller_saves);
7702 } else {
7703 // For non-Baker read barriers we have a temp-clobbering call.
7704 }
7705 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007706}
7707
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007708// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
7709// move.
7710void InstructionCodeGeneratorMIPS::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00007711 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007712 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00007713 codegen_->GenerateLoadClassRuntimeCall(cls);
Pavle Batutae87a7182015-10-28 13:10:42 +01007714 return;
7715 }
Vladimir Marko41559982017-01-06 14:04:23 +00007716 DCHECK(!cls->NeedsAccessCheck());
Pavle Batutae87a7182015-10-28 13:10:42 +01007717
Vladimir Marko41559982017-01-06 14:04:23 +00007718 LocationSummary* locations = cls->GetLocations();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007719 Location out_loc = locations->Out();
7720 Register out = out_loc.AsRegister<Register>();
7721 Register base_or_current_method_reg;
7722 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007723 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007724 switch (load_kind) {
7725 // We need an extra register for PC-relative literals on R2.
Alexey Frunze06a46c42016-07-19 15:00:40 -07007726 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007727 case HLoadClass::LoadKind::kBootImageAddress:
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007728 case HLoadClass::LoadKind::kBootImageClassTable:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007729 case HLoadClass::LoadKind::kBssEntry:
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007730 base_or_current_method_reg =
7731 (isR6 || has_irreducible_loops) ? ZERO : locations->InAt(0).AsRegister<Register>();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007732 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007733 case HLoadClass::LoadKind::kReferrersClass:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007734 case HLoadClass::LoadKind::kRuntimeCall:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007735 base_or_current_method_reg = locations->InAt(0).AsRegister<Register>();
7736 break;
7737 default:
7738 base_or_current_method_reg = ZERO;
7739 break;
7740 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00007741
Alexey Frunze15958152017-02-09 19:08:30 -08007742 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
7743 ? kWithoutReadBarrier
7744 : kCompilerReadBarrierOption;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007745 bool generate_null_check = false;
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007746 CodeGeneratorMIPS::PcRelativePatchInfo* bss_info_high = nullptr;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007747 switch (load_kind) {
7748 case HLoadClass::LoadKind::kReferrersClass: {
7749 DCHECK(!cls->CanCallRuntime());
7750 DCHECK(!cls->MustGenerateClinitCheck());
7751 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
7752 GenerateGcRootFieldLoad(cls,
7753 out_loc,
7754 base_or_current_method_reg,
Alexey Frunze15958152017-02-09 19:08:30 -08007755 ArtMethod::DeclaringClassOffset().Int32Value(),
7756 read_barrier_option);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007757 break;
7758 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007759 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007760 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze15958152017-02-09 19:08:30 -08007761 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007762 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
Alexey Frunze06a46c42016-07-19 15:00:40 -07007763 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007764 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
7765 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex(), info_high);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007766 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
7767 out,
Alexey Frunzea663d9d2017-07-31 18:43:18 -07007768 base_or_current_method_reg);
7769 __ Addiu(out, out, /* placeholder */ 0x5678, &info_low->label);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007770 break;
7771 }
7772 case HLoadClass::LoadKind::kBootImageAddress: {
Alexey Frunze15958152017-02-09 19:08:30 -08007773 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007774 uint32_t address = dchecked_integral_cast<uint32_t>(
7775 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
7776 DCHECK_NE(address, 0u);
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007777 if (isR6 || !has_irreducible_loops) {
7778 __ LoadLiteral(out,
7779 base_or_current_method_reg,
7780 codegen_->DeduplicateBootImageAddressLiteral(address));
7781 } else {
7782 __ LoadConst32(out, address);
7783 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007784 break;
7785 }
Vladimir Marko94ec2db2017-09-06 17:21:03 +01007786 case HLoadClass::LoadKind::kBootImageClassTable: {
7787 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
7788 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
7789 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
7790 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
7791 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex(), info_high);
7792 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
7793 out,
7794 base_or_current_method_reg);
7795 __ Lw(out, out, /* placeholder */ 0x5678, &info_low->label);
7796 // Extract the reference from the slot data, i.e. clear the hash bits.
7797 int32_t masked_hash = ClassTable::TableSlot::MaskHash(
7798 ComputeModifiedUtf8Hash(cls->GetDexFile().StringByTypeIdx(cls->GetTypeIndex())));
7799 if (masked_hash != 0) {
7800 __ Addiu(out, out, -masked_hash);
7801 }
7802 break;
7803 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007804 case HLoadClass::LoadKind::kBssEntry: {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007805 bss_info_high = codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex());
7806 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
7807 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex(), bss_info_high);
Alexey Frunzec61c0762017-04-10 13:54:23 -07007808 constexpr bool non_baker_read_barrier = kUseReadBarrier && !kUseBakerReadBarrier;
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007809 Register temp = non_baker_read_barrier ? out : locations->GetTemp(0).AsRegister<Register>();
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007810 codegen_->EmitPcRelativeAddressPlaceholderHigh(bss_info_high,
7811 temp,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007812 base_or_current_method_reg);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007813 GenerateGcRootFieldLoad(cls,
7814 out_loc,
7815 temp,
7816 /* placeholder */ 0x5678,
7817 read_barrier_option,
7818 &info_low->label);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007819 generate_null_check = true;
7820 break;
7821 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007822 case HLoadClass::LoadKind::kJitTableAddress: {
Alexey Frunze627c1a02017-01-30 19:28:14 -08007823 CodeGeneratorMIPS::JitPatchInfo* info = codegen_->NewJitRootClassPatch(cls->GetDexFile(),
7824 cls->GetTypeIndex(),
7825 cls->GetClass());
7826 bool reordering = __ SetReorder(false);
7827 __ Bind(&info->high_label);
7828 __ Lui(out, /* placeholder */ 0x1234);
Alexey Frunze627c1a02017-01-30 19:28:14 -08007829 __ SetReorder(reordering);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007830 GenerateGcRootFieldLoad(cls,
7831 out_loc,
7832 out,
7833 /* placeholder */ 0x5678,
7834 read_barrier_option,
7835 &info->low_label);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007836 break;
7837 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007838 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00007839 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00007840 LOG(FATAL) << "UNREACHABLE";
7841 UNREACHABLE();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007842 }
7843
7844 if (generate_null_check || cls->MustGenerateClinitCheck()) {
7845 DCHECK(cls->CanCallRuntime());
Vladimir Markoca6fff82017-10-03 14:49:14 +01007846 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetAllocator()) LoadClassSlowPathMIPS(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007847 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck(), bss_info_high);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007848 codegen_->AddSlowPath(slow_path);
7849 if (generate_null_check) {
7850 __ Beqz(out, slow_path->GetEntryLabel());
7851 }
7852 if (cls->MustGenerateClinitCheck()) {
7853 GenerateClassInitializationCheck(slow_path, out);
7854 } else {
7855 __ Bind(slow_path->GetExitLabel());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007856 }
7857 }
7858}
7859
7860static int32_t GetExceptionTlsOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -07007861 return Thread::ExceptionOffset<kMipsPointerSize>().Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007862}
7863
7864void LocationsBuilderMIPS::VisitLoadException(HLoadException* load) {
7865 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007866 new (GetGraph()->GetAllocator()) LocationSummary(load, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007867 locations->SetOut(Location::RequiresRegister());
7868}
7869
7870void InstructionCodeGeneratorMIPS::VisitLoadException(HLoadException* load) {
7871 Register out = load->GetLocations()->Out().AsRegister<Register>();
7872 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
7873}
7874
7875void LocationsBuilderMIPS::VisitClearException(HClearException* clear) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007876 new (GetGraph()->GetAllocator()) LocationSummary(clear, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007877}
7878
7879void InstructionCodeGeneratorMIPS::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
7880 __ StoreToOffset(kStoreWord, ZERO, TR, GetExceptionTlsOffset());
7881}
7882
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007883void LocationsBuilderMIPS::VisitLoadString(HLoadString* load) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08007884 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Vladimir Markoca6fff82017-10-03 14:49:14 +01007885 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(load, call_kind);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007886 HLoadString::LoadKind load_kind = load->GetLoadKind();
Alexey Frunzec61c0762017-04-10 13:54:23 -07007887 const bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007888 const bool has_irreducible_loops = codegen_->GetGraph()->HasIrreducibleLoops();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007889 switch (load_kind) {
7890 // We need an extra register for PC-relative literals on R2.
Alexey Frunze06a46c42016-07-19 15:00:40 -07007891 case HLoadString::LoadKind::kBootImageAddress:
7892 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007893 case HLoadString::LoadKind::kBootImageInternTable:
Vladimir Markoaad75c62016-10-03 08:46:48 +00007894 case HLoadString::LoadKind::kBssEntry:
Alexey Frunzec61c0762017-04-10 13:54:23 -07007895 if (isR6) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007896 break;
7897 }
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007898 if (has_irreducible_loops) {
Alexey Frunze3b8c82f2017-10-10 23:01:34 -07007899 if (load_kind != HLoadString::LoadKind::kBootImageAddress) {
7900 codegen_->ClobberRA();
7901 }
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007902 break;
7903 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007904 FALLTHROUGH_INTENDED;
7905 // We need an extra register for PC-relative dex cache accesses.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007906 case HLoadString::LoadKind::kRuntimeCall:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007907 locations->SetInAt(0, Location::RequiresRegister());
7908 break;
7909 default:
7910 break;
7911 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007912 if (load_kind == HLoadString::LoadKind::kRuntimeCall) {
Alexey Frunzebb51df82016-11-01 16:07:32 -07007913 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07007914 locations->SetOut(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Alexey Frunzebb51df82016-11-01 16:07:32 -07007915 } else {
7916 locations->SetOut(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07007917 if (load_kind == HLoadString::LoadKind::kBssEntry) {
7918 if (!kUseReadBarrier || kUseBakerReadBarrier) {
7919 // Rely on the pResolveString and marking to save everything we need.
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007920 // Request a temp to hold the BSS entry location for the slow path.
7921 locations->AddTemp(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07007922 RegisterSet caller_saves = RegisterSet::Empty();
7923 InvokeRuntimeCallingConvention calling_convention;
7924 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
7925 locations->SetCustomSlowPathCallerSaves(caller_saves);
7926 } else {
7927 // For non-Baker read barriers we have a temp-clobbering call.
7928 }
7929 }
Alexey Frunzebb51df82016-11-01 16:07:32 -07007930 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007931}
7932
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007933// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
7934// move.
7935void InstructionCodeGeneratorMIPS::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007936 HLoadString::LoadKind load_kind = load->GetLoadKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007937 LocationSummary* locations = load->GetLocations();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007938 Location out_loc = locations->Out();
7939 Register out = out_loc.AsRegister<Register>();
7940 Register base_or_current_method_reg;
7941 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007942 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007943 switch (load_kind) {
7944 // We need an extra register for PC-relative literals on R2.
Alexey Frunze06a46c42016-07-19 15:00:40 -07007945 case HLoadString::LoadKind::kBootImageAddress:
7946 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007947 case HLoadString::LoadKind::kBootImageInternTable:
Vladimir Markoaad75c62016-10-03 08:46:48 +00007948 case HLoadString::LoadKind::kBssEntry:
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007949 base_or_current_method_reg =
7950 (isR6 || has_irreducible_loops) ? ZERO : locations->InAt(0).AsRegister<Register>();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007951 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007952 default:
7953 base_or_current_method_reg = ZERO;
7954 break;
7955 }
7956
7957 switch (load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007958 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Markoaad75c62016-10-03 08:46:48 +00007959 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007960 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007961 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007962 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
7963 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex(), info_high);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007964 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
7965 out,
Alexey Frunzea663d9d2017-07-31 18:43:18 -07007966 base_or_current_method_reg);
7967 __ Addiu(out, out, /* placeholder */ 0x5678, &info_low->label);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007968 return;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007969 }
7970 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007971 uint32_t address = dchecked_integral_cast<uint32_t>(
7972 reinterpret_cast<uintptr_t>(load->GetString().Get()));
7973 DCHECK_NE(address, 0u);
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007974 if (isR6 || !has_irreducible_loops) {
7975 __ LoadLiteral(out,
7976 base_or_current_method_reg,
7977 codegen_->DeduplicateBootImageAddressLiteral(address));
7978 } else {
7979 __ LoadConst32(out, address);
7980 }
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007981 return;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007982 }
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007983 case HLoadString::LoadKind::kBootImageInternTable: {
Vladimir Markoaad75c62016-10-03 08:46:48 +00007984 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007985 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007986 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007987 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
7988 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex(), info_high);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007989 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
7990 out,
7991 base_or_current_method_reg);
7992 __ Lw(out, out, /* placeholder */ 0x5678, &info_low->label);
7993 return;
7994 }
7995 case HLoadString::LoadKind::kBssEntry: {
7996 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
7997 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
7998 codegen_->NewStringBssEntryPatch(load->GetDexFile(), load->GetStringIndex());
7999 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
8000 codegen_->NewStringBssEntryPatch(load->GetDexFile(), load->GetStringIndex(), info_high);
Alexey Frunzec61c0762017-04-10 13:54:23 -07008001 constexpr bool non_baker_read_barrier = kUseReadBarrier && !kUseBakerReadBarrier;
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008002 Register temp = non_baker_read_barrier ? out : locations->GetTemp(0).AsRegister<Register>();
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008003 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
8004 temp,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07008005 base_or_current_method_reg);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008006 GenerateGcRootFieldLoad(load,
8007 out_loc,
8008 temp,
8009 /* placeholder */ 0x5678,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07008010 kCompilerReadBarrierOption,
8011 &info_low->label);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008012 SlowPathCodeMIPS* slow_path =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008013 new (GetGraph()->GetAllocator()) LoadStringSlowPathMIPS(load, info_high);
Vladimir Markoaad75c62016-10-03 08:46:48 +00008014 codegen_->AddSlowPath(slow_path);
8015 __ Beqz(out, slow_path->GetEntryLabel());
8016 __ Bind(slow_path->GetExitLabel());
8017 return;
8018 }
Alexey Frunze627c1a02017-01-30 19:28:14 -08008019 case HLoadString::LoadKind::kJitTableAddress: {
8020 CodeGeneratorMIPS::JitPatchInfo* info =
8021 codegen_->NewJitRootStringPatch(load->GetDexFile(),
8022 load->GetStringIndex(),
8023 load->GetString());
8024 bool reordering = __ SetReorder(false);
8025 __ Bind(&info->high_label);
8026 __ Lui(out, /* placeholder */ 0x1234);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07008027 __ SetReorder(reordering);
Alexey Frunze15958152017-02-09 19:08:30 -08008028 GenerateGcRootFieldLoad(load,
8029 out_loc,
8030 out,
8031 /* placeholder */ 0x5678,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07008032 kCompilerReadBarrierOption,
8033 &info->low_label);
Alexey Frunze627c1a02017-01-30 19:28:14 -08008034 return;
8035 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07008036 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07008037 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07008038 }
Nicolas Geoffray917d0162015-11-24 18:25:35 +00008039
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07008040 // TODO: Re-add the compiler code to do string dex cache lookup again.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01008041 DCHECK(load_kind == HLoadString::LoadKind::kRuntimeCall);
Vladimir Markoaad75c62016-10-03 08:46:48 +00008042 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07008043 DCHECK_EQ(calling_convention.GetRegisterAt(0), out);
Andreas Gampe8a0128a2016-11-28 07:38:35 -08008044 __ LoadConst32(calling_convention.GetRegisterAt(0), load->GetStringIndex().index_);
Vladimir Markoaad75c62016-10-03 08:46:48 +00008045 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
8046 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008047}
8048
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008049void LocationsBuilderMIPS::VisitLongConstant(HLongConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008050 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008051 locations->SetOut(Location::ConstantLocation(constant));
8052}
8053
8054void InstructionCodeGeneratorMIPS::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
8055 // Will be generated at use site.
8056}
8057
8058void LocationsBuilderMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008059 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
8060 instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008061 InvokeRuntimeCallingConvention calling_convention;
8062 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
8063}
8064
8065void InstructionCodeGeneratorMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
8066 if (instruction->IsEnter()) {
Serban Constantinescufca16662016-07-14 09:21:59 +01008067 codegen_->InvokeRuntime(kQuickLockObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008068 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
8069 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01008070 codegen_->InvokeRuntime(kQuickUnlockObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008071 }
8072 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
8073}
8074
8075void LocationsBuilderMIPS::VisitMul(HMul* mul) {
8076 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008077 new (GetGraph()->GetAllocator()) LocationSummary(mul, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008078 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008079 case DataType::Type::kInt32:
8080 case DataType::Type::kInt64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008081 locations->SetInAt(0, Location::RequiresRegister());
8082 locations->SetInAt(1, Location::RequiresRegister());
8083 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8084 break;
8085
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008086 case DataType::Type::kFloat32:
8087 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008088 locations->SetInAt(0, Location::RequiresFpuRegister());
8089 locations->SetInAt(1, Location::RequiresFpuRegister());
8090 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
8091 break;
8092
8093 default:
8094 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
8095 }
8096}
8097
8098void InstructionCodeGeneratorMIPS::VisitMul(HMul* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008099 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008100 LocationSummary* locations = instruction->GetLocations();
8101 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
8102
8103 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008104 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008105 Register dst = locations->Out().AsRegister<Register>();
8106 Register lhs = locations->InAt(0).AsRegister<Register>();
8107 Register rhs = locations->InAt(1).AsRegister<Register>();
8108
8109 if (isR6) {
8110 __ MulR6(dst, lhs, rhs);
8111 } else {
8112 __ MulR2(dst, lhs, rhs);
8113 }
8114 break;
8115 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008116 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008117 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8118 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
8119 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
8120 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
8121 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
8122 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
8123
8124 // Extra checks to protect caused by the existance of A1_A2.
8125 // The algorithm is wrong if dst_high is either lhs_lo or rhs_lo:
8126 // (e.g. lhs=a0_a1, rhs=a2_a3 and dst=a1_a2).
8127 DCHECK_NE(dst_high, lhs_low);
8128 DCHECK_NE(dst_high, rhs_low);
8129
8130 // A_B * C_D
8131 // dst_hi: [ low(A*D) + low(B*C) + hi(B*D) ]
8132 // dst_lo: [ low(B*D) ]
8133 // Note: R2 and R6 MUL produce the low 32 bit of the multiplication result.
8134
8135 if (isR6) {
8136 __ MulR6(TMP, lhs_high, rhs_low);
8137 __ MulR6(dst_high, lhs_low, rhs_high);
8138 __ Addu(dst_high, dst_high, TMP);
8139 __ MuhuR6(TMP, lhs_low, rhs_low);
8140 __ Addu(dst_high, dst_high, TMP);
8141 __ MulR6(dst_low, lhs_low, rhs_low);
8142 } else {
8143 __ MulR2(TMP, lhs_high, rhs_low);
8144 __ MulR2(dst_high, lhs_low, rhs_high);
8145 __ Addu(dst_high, dst_high, TMP);
8146 __ MultuR2(lhs_low, rhs_low);
8147 __ Mfhi(TMP);
8148 __ Addu(dst_high, dst_high, TMP);
8149 __ Mflo(dst_low);
8150 }
8151 break;
8152 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008153 case DataType::Type::kFloat32:
8154 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008155 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
8156 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
8157 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008158 if (type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008159 __ MulS(dst, lhs, rhs);
8160 } else {
8161 __ MulD(dst, lhs, rhs);
8162 }
8163 break;
8164 }
8165 default:
8166 LOG(FATAL) << "Unexpected mul type " << type;
8167 }
8168}
8169
8170void LocationsBuilderMIPS::VisitNeg(HNeg* neg) {
8171 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008172 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008173 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008174 case DataType::Type::kInt32:
8175 case DataType::Type::kInt64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008176 locations->SetInAt(0, Location::RequiresRegister());
8177 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8178 break;
8179
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008180 case DataType::Type::kFloat32:
8181 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008182 locations->SetInAt(0, Location::RequiresFpuRegister());
8183 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
8184 break;
8185
8186 default:
8187 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
8188 }
8189}
8190
8191void InstructionCodeGeneratorMIPS::VisitNeg(HNeg* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008192 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008193 LocationSummary* locations = instruction->GetLocations();
8194
8195 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008196 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008197 Register dst = locations->Out().AsRegister<Register>();
8198 Register src = locations->InAt(0).AsRegister<Register>();
8199 __ Subu(dst, ZERO, src);
8200 break;
8201 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008202 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008203 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8204 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
8205 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
8206 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
8207 __ Subu(dst_low, ZERO, src_low);
8208 __ Sltu(TMP, ZERO, dst_low);
8209 __ Subu(dst_high, ZERO, src_high);
8210 __ Subu(dst_high, dst_high, TMP);
8211 break;
8212 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008213 case DataType::Type::kFloat32:
8214 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008215 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
8216 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008217 if (type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008218 __ NegS(dst, src);
8219 } else {
8220 __ NegD(dst, src);
8221 }
8222 break;
8223 }
8224 default:
8225 LOG(FATAL) << "Unexpected neg type " << type;
8226 }
8227}
8228
8229void LocationsBuilderMIPS::VisitNewArray(HNewArray* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008230 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
8231 instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008232 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008233 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kReference));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00008234 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
8235 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008236}
8237
8238void InstructionCodeGeneratorMIPS::VisitNewArray(HNewArray* instruction) {
Alexey Frunzec061de12017-02-14 13:27:23 -08008239 // Note: if heap poisoning is enabled, the entry point takes care
8240 // of poisoning the reference.
Goran Jakovljevic854df412017-06-27 14:41:39 +02008241 QuickEntrypointEnum entrypoint =
8242 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
8243 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00008244 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Goran Jakovljevic854df412017-06-27 14:41:39 +02008245 DCHECK(!codegen_->IsLeafMethod());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008246}
8247
8248void LocationsBuilderMIPS::VisitNewInstance(HNewInstance* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008249 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
8250 instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008251 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00008252 if (instruction->IsStringAlloc()) {
8253 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
8254 } else {
8255 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00008256 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008257 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kReference));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008258}
8259
8260void InstructionCodeGeneratorMIPS::VisitNewInstance(HNewInstance* instruction) {
Alexey Frunzec061de12017-02-14 13:27:23 -08008261 // Note: if heap poisoning is enabled, the entry point takes care
8262 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00008263 if (instruction->IsStringAlloc()) {
8264 // String is allocated through StringFactory. Call NewEmptyString entry point.
8265 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07008266 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00008267 __ LoadFromOffset(kLoadWord, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
8268 __ LoadFromOffset(kLoadWord, T9, temp, code_offset.Int32Value());
8269 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07008270 __ NopIfNoReordering();
David Brazdil6de19382016-01-08 17:37:10 +00008271 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
8272 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01008273 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00008274 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00008275 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008276}
8277
8278void LocationsBuilderMIPS::VisitNot(HNot* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008279 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008280 locations->SetInAt(0, Location::RequiresRegister());
8281 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8282}
8283
8284void InstructionCodeGeneratorMIPS::VisitNot(HNot* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008285 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008286 LocationSummary* locations = instruction->GetLocations();
8287
8288 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008289 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008290 Register dst = locations->Out().AsRegister<Register>();
8291 Register src = locations->InAt(0).AsRegister<Register>();
8292 __ Nor(dst, src, ZERO);
8293 break;
8294 }
8295
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008296 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008297 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8298 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
8299 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
8300 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
8301 __ Nor(dst_high, src_high, ZERO);
8302 __ Nor(dst_low, src_low, ZERO);
8303 break;
8304 }
8305
8306 default:
8307 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
8308 }
8309}
8310
8311void LocationsBuilderMIPS::VisitBooleanNot(HBooleanNot* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008312 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008313 locations->SetInAt(0, Location::RequiresRegister());
8314 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8315}
8316
8317void InstructionCodeGeneratorMIPS::VisitBooleanNot(HBooleanNot* instruction) {
8318 LocationSummary* locations = instruction->GetLocations();
8319 __ Xori(locations->Out().AsRegister<Register>(),
8320 locations->InAt(0).AsRegister<Register>(),
8321 1);
8322}
8323
8324void LocationsBuilderMIPS::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01008325 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
8326 locations->SetInAt(0, Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008327}
8328
Calin Juravle2ae48182016-03-16 14:05:09 +00008329void CodeGeneratorMIPS::GenerateImplicitNullCheck(HNullCheck* instruction) {
8330 if (CanMoveNullCheckToUser(instruction)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008331 return;
8332 }
8333 Location obj = instruction->GetLocations()->InAt(0);
8334
8335 __ Lw(ZERO, obj.AsRegister<Register>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00008336 RecordPcInfo(instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008337}
8338
Calin Juravle2ae48182016-03-16 14:05:09 +00008339void CodeGeneratorMIPS::GenerateExplicitNullCheck(HNullCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008340 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetAllocator()) NullCheckSlowPathMIPS(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00008341 AddSlowPath(slow_path);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008342
8343 Location obj = instruction->GetLocations()->InAt(0);
8344
8345 __ Beqz(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
8346}
8347
8348void InstructionCodeGeneratorMIPS::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00008349 codegen_->GenerateNullCheck(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008350}
8351
8352void LocationsBuilderMIPS::VisitOr(HOr* instruction) {
8353 HandleBinaryOp(instruction);
8354}
8355
8356void InstructionCodeGeneratorMIPS::VisitOr(HOr* instruction) {
8357 HandleBinaryOp(instruction);
8358}
8359
8360void LocationsBuilderMIPS::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
8361 LOG(FATAL) << "Unreachable";
8362}
8363
8364void InstructionCodeGeneratorMIPS::VisitParallelMove(HParallelMove* instruction) {
Vladimir Markobea75ff2017-10-11 20:39:54 +01008365 if (instruction->GetNext()->IsSuspendCheck() &&
8366 instruction->GetBlock()->GetLoopInformation() != nullptr) {
8367 HSuspendCheck* suspend_check = instruction->GetNext()->AsSuspendCheck();
8368 // The back edge will generate the suspend check.
8369 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(suspend_check, instruction);
8370 }
8371
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008372 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
8373}
8374
8375void LocationsBuilderMIPS::VisitParameterValue(HParameterValue* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008376 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008377 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
8378 if (location.IsStackSlot()) {
8379 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
8380 } else if (location.IsDoubleStackSlot()) {
8381 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
8382 }
8383 locations->SetOut(location);
8384}
8385
8386void InstructionCodeGeneratorMIPS::VisitParameterValue(HParameterValue* instruction
8387 ATTRIBUTE_UNUSED) {
8388 // Nothing to do, the parameter is already at its location.
8389}
8390
8391void LocationsBuilderMIPS::VisitCurrentMethod(HCurrentMethod* instruction) {
8392 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008393 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008394 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
8395}
8396
8397void InstructionCodeGeneratorMIPS::VisitCurrentMethod(HCurrentMethod* instruction
8398 ATTRIBUTE_UNUSED) {
8399 // Nothing to do, the method is already at its location.
8400}
8401
8402void LocationsBuilderMIPS::VisitPhi(HPhi* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008403 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01008404 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008405 locations->SetInAt(i, Location::Any());
8406 }
8407 locations->SetOut(Location::Any());
8408}
8409
8410void InstructionCodeGeneratorMIPS::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
8411 LOG(FATAL) << "Unreachable";
8412}
8413
8414void LocationsBuilderMIPS::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008415 DataType::Type type = rem->GetResultType();
8416 LocationSummary::CallKind call_kind = (type == DataType::Type::kInt32)
8417 ? LocationSummary::kNoCall
8418 : LocationSummary::kCallOnMainOnly;
Vladimir Markoca6fff82017-10-03 14:49:14 +01008419 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(rem, call_kind);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008420
8421 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008422 case DataType::Type::kInt32:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008423 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08008424 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008425 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8426 break;
8427
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008428 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008429 InvokeRuntimeCallingConvention calling_convention;
8430 locations->SetInAt(0, Location::RegisterPairLocation(
8431 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
8432 locations->SetInAt(1, Location::RegisterPairLocation(
8433 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
8434 locations->SetOut(calling_convention.GetReturnLocation(type));
8435 break;
8436 }
8437
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008438 case DataType::Type::kFloat32:
8439 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008440 InvokeRuntimeCallingConvention calling_convention;
8441 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
8442 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
8443 locations->SetOut(calling_convention.GetReturnLocation(type));
8444 break;
8445 }
8446
8447 default:
8448 LOG(FATAL) << "Unexpected rem type " << type;
8449 }
8450}
8451
8452void InstructionCodeGeneratorMIPS::VisitRem(HRem* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008453 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008454
8455 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008456 case DataType::Type::kInt32:
Alexey Frunze7e99e052015-11-24 19:28:01 -08008457 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008458 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008459 case DataType::Type::kInt64: {
Serban Constantinescufca16662016-07-14 09:21:59 +01008460 codegen_->InvokeRuntime(kQuickLmod, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008461 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
8462 break;
8463 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008464 case DataType::Type::kFloat32: {
Serban Constantinescufca16662016-07-14 09:21:59 +01008465 codegen_->InvokeRuntime(kQuickFmodf, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00008466 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008467 break;
8468 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008469 case DataType::Type::kFloat64: {
Serban Constantinescufca16662016-07-14 09:21:59 +01008470 codegen_->InvokeRuntime(kQuickFmod, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00008471 CheckEntrypointTypes<kQuickFmod, double, double, double>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008472 break;
8473 }
8474 default:
8475 LOG(FATAL) << "Unexpected rem type " << type;
8476 }
8477}
8478
Igor Murashkind01745e2017-04-05 16:40:31 -07008479void LocationsBuilderMIPS::VisitConstructorFence(HConstructorFence* constructor_fence) {
8480 constructor_fence->SetLocations(nullptr);
8481}
8482
8483void InstructionCodeGeneratorMIPS::VisitConstructorFence(
8484 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
8485 GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
8486}
8487
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008488void LocationsBuilderMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
8489 memory_barrier->SetLocations(nullptr);
8490}
8491
8492void InstructionCodeGeneratorMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
8493 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
8494}
8495
8496void LocationsBuilderMIPS::VisitReturn(HReturn* ret) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008497 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(ret);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008498 DataType::Type return_type = ret->InputAt(0)->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008499 locations->SetInAt(0, MipsReturnLocation(return_type));
8500}
8501
8502void InstructionCodeGeneratorMIPS::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
8503 codegen_->GenerateFrameExit();
8504}
8505
8506void LocationsBuilderMIPS::VisitReturnVoid(HReturnVoid* ret) {
8507 ret->SetLocations(nullptr);
8508}
8509
8510void InstructionCodeGeneratorMIPS::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
8511 codegen_->GenerateFrameExit();
8512}
8513
Alexey Frunze92d90602015-12-18 18:16:36 -08008514void LocationsBuilderMIPS::VisitRor(HRor* ror) {
8515 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00008516}
8517
Alexey Frunze92d90602015-12-18 18:16:36 -08008518void InstructionCodeGeneratorMIPS::VisitRor(HRor* ror) {
8519 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00008520}
8521
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008522void LocationsBuilderMIPS::VisitShl(HShl* shl) {
8523 HandleShift(shl);
8524}
8525
8526void InstructionCodeGeneratorMIPS::VisitShl(HShl* shl) {
8527 HandleShift(shl);
8528}
8529
8530void LocationsBuilderMIPS::VisitShr(HShr* shr) {
8531 HandleShift(shr);
8532}
8533
8534void InstructionCodeGeneratorMIPS::VisitShr(HShr* shr) {
8535 HandleShift(shr);
8536}
8537
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008538void LocationsBuilderMIPS::VisitSub(HSub* instruction) {
8539 HandleBinaryOp(instruction);
8540}
8541
8542void InstructionCodeGeneratorMIPS::VisitSub(HSub* instruction) {
8543 HandleBinaryOp(instruction);
8544}
8545
8546void LocationsBuilderMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
8547 HandleFieldGet(instruction, instruction->GetFieldInfo());
8548}
8549
8550void InstructionCodeGeneratorMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
8551 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
8552}
8553
8554void LocationsBuilderMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
8555 HandleFieldSet(instruction, instruction->GetFieldInfo());
8556}
8557
8558void InstructionCodeGeneratorMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Goran Jakovljevice114da22016-12-26 14:21:43 +01008559 HandleFieldSet(instruction,
8560 instruction->GetFieldInfo(),
8561 instruction->GetDexPc(),
8562 instruction->GetValueCanBeNull());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008563}
8564
8565void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldGet(
8566 HUnresolvedInstanceFieldGet* instruction) {
8567 FieldAccessCallingConventionMIPS calling_convention;
8568 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
8569 instruction->GetFieldType(),
8570 calling_convention);
8571}
8572
8573void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldGet(
8574 HUnresolvedInstanceFieldGet* instruction) {
8575 FieldAccessCallingConventionMIPS calling_convention;
8576 codegen_->GenerateUnresolvedFieldAccess(instruction,
8577 instruction->GetFieldType(),
8578 instruction->GetFieldIndex(),
8579 instruction->GetDexPc(),
8580 calling_convention);
8581}
8582
8583void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldSet(
8584 HUnresolvedInstanceFieldSet* instruction) {
8585 FieldAccessCallingConventionMIPS calling_convention;
8586 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
8587 instruction->GetFieldType(),
8588 calling_convention);
8589}
8590
8591void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldSet(
8592 HUnresolvedInstanceFieldSet* instruction) {
8593 FieldAccessCallingConventionMIPS calling_convention;
8594 codegen_->GenerateUnresolvedFieldAccess(instruction,
8595 instruction->GetFieldType(),
8596 instruction->GetFieldIndex(),
8597 instruction->GetDexPc(),
8598 calling_convention);
8599}
8600
8601void LocationsBuilderMIPS::VisitUnresolvedStaticFieldGet(
8602 HUnresolvedStaticFieldGet* instruction) {
8603 FieldAccessCallingConventionMIPS calling_convention;
8604 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
8605 instruction->GetFieldType(),
8606 calling_convention);
8607}
8608
8609void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldGet(
8610 HUnresolvedStaticFieldGet* instruction) {
8611 FieldAccessCallingConventionMIPS calling_convention;
8612 codegen_->GenerateUnresolvedFieldAccess(instruction,
8613 instruction->GetFieldType(),
8614 instruction->GetFieldIndex(),
8615 instruction->GetDexPc(),
8616 calling_convention);
8617}
8618
8619void LocationsBuilderMIPS::VisitUnresolvedStaticFieldSet(
8620 HUnresolvedStaticFieldSet* instruction) {
8621 FieldAccessCallingConventionMIPS calling_convention;
8622 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
8623 instruction->GetFieldType(),
8624 calling_convention);
8625}
8626
8627void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldSet(
8628 HUnresolvedStaticFieldSet* instruction) {
8629 FieldAccessCallingConventionMIPS calling_convention;
8630 codegen_->GenerateUnresolvedFieldAccess(instruction,
8631 instruction->GetFieldType(),
8632 instruction->GetFieldIndex(),
8633 instruction->GetDexPc(),
8634 calling_convention);
8635}
8636
8637void LocationsBuilderMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008638 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
8639 instruction, LocationSummary::kCallOnSlowPath);
Lena Djokicca8c2952017-05-29 11:31:46 +02008640 // In suspend check slow path, usually there are no caller-save registers at all.
8641 // If SIMD instructions are present, however, we force spilling all live SIMD
8642 // registers in full width (since the runtime only saves/restores lower part).
8643 locations->SetCustomSlowPathCallerSaves(
8644 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008645}
8646
8647void InstructionCodeGeneratorMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
8648 HBasicBlock* block = instruction->GetBlock();
8649 if (block->GetLoopInformation() != nullptr) {
8650 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
8651 // The back edge will generate the suspend check.
8652 return;
8653 }
8654 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
8655 // The goto will generate the suspend check.
8656 return;
8657 }
8658 GenerateSuspendCheck(instruction, nullptr);
8659}
8660
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008661void LocationsBuilderMIPS::VisitThrow(HThrow* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008662 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
8663 instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008664 InvokeRuntimeCallingConvention calling_convention;
8665 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
8666}
8667
8668void InstructionCodeGeneratorMIPS::VisitThrow(HThrow* instruction) {
Serban Constantinescufca16662016-07-14 09:21:59 +01008669 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008670 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
8671}
8672
8673void LocationsBuilderMIPS::VisitTypeConversion(HTypeConversion* conversion) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008674 DataType::Type input_type = conversion->GetInputType();
8675 DataType::Type result_type = conversion->GetResultType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01008676 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
8677 << input_type << " -> " << result_type;
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008678 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008679
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008680 if ((input_type == DataType::Type::kReference) || (input_type == DataType::Type::kVoid) ||
8681 (result_type == DataType::Type::kReference) || (result_type == DataType::Type::kVoid)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008682 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
8683 }
8684
8685 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008686 if (!isR6 &&
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008687 ((DataType::IsFloatingPointType(result_type) && input_type == DataType::Type::kInt64) ||
8688 (result_type == DataType::Type::kInt64 && DataType::IsFloatingPointType(input_type)))) {
Serban Constantinescu54ff4822016-07-07 18:03:19 +01008689 call_kind = LocationSummary::kCallOnMainOnly;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008690 }
8691
Vladimir Markoca6fff82017-10-03 14:49:14 +01008692 LocationSummary* locations =
8693 new (GetGraph()->GetAllocator()) LocationSummary(conversion, call_kind);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008694
8695 if (call_kind == LocationSummary::kNoCall) {
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::RequiresFpuRegister());
8698 } else {
8699 locations->SetInAt(0, Location::RequiresRegister());
8700 }
8701
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008702 if (DataType::IsFloatingPointType(result_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008703 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
8704 } else {
8705 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8706 }
8707 } else {
8708 InvokeRuntimeCallingConvention calling_convention;
8709
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008710 if (DataType::IsFloatingPointType(input_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008711 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
8712 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008713 DCHECK_EQ(input_type, DataType::Type::kInt64);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008714 locations->SetInAt(0, Location::RegisterPairLocation(
8715 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
8716 }
8717
8718 locations->SetOut(calling_convention.GetReturnLocation(result_type));
8719 }
8720}
8721
8722void InstructionCodeGeneratorMIPS::VisitTypeConversion(HTypeConversion* conversion) {
8723 LocationSummary* locations = conversion->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008724 DataType::Type result_type = conversion->GetResultType();
8725 DataType::Type input_type = conversion->GetInputType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008726 bool has_sign_extension = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008727 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008728
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01008729 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
8730 << input_type << " -> " << result_type;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008731
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008732 if (result_type == DataType::Type::kInt64 && DataType::IsIntegralType(input_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008733 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8734 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
8735 Register src = locations->InAt(0).AsRegister<Register>();
8736
Alexey Frunzea871ef12016-06-27 15:20:11 -07008737 if (dst_low != src) {
8738 __ Move(dst_low, src);
8739 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008740 __ Sra(dst_high, src, 31);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008741 } else if (DataType::IsIntegralType(result_type) && DataType::IsIntegralType(input_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008742 Register dst = locations->Out().AsRegister<Register>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008743 Register src = (input_type == DataType::Type::kInt64)
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008744 ? locations->InAt(0).AsRegisterPairLow<Register>()
8745 : locations->InAt(0).AsRegister<Register>();
8746
8747 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01008748 case DataType::Type::kUint8:
8749 __ Andi(dst, src, 0xFF);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008750 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008751 case DataType::Type::kInt8:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008752 if (has_sign_extension) {
8753 __ Seb(dst, src);
8754 } else {
8755 __ Sll(dst, src, 24);
8756 __ Sra(dst, dst, 24);
8757 }
8758 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01008759 case DataType::Type::kUint16:
8760 __ Andi(dst, src, 0xFFFF);
8761 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008762 case DataType::Type::kInt16:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008763 if (has_sign_extension) {
8764 __ Seh(dst, src);
8765 } else {
8766 __ Sll(dst, src, 16);
8767 __ Sra(dst, dst, 16);
8768 }
8769 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008770 case DataType::Type::kInt32:
Alexey Frunzea871ef12016-06-27 15:20:11 -07008771 if (dst != src) {
8772 __ Move(dst, src);
8773 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008774 break;
8775
8776 default:
8777 LOG(FATAL) << "Unexpected type conversion from " << input_type
8778 << " to " << result_type;
8779 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008780 } else if (DataType::IsFloatingPointType(result_type) && DataType::IsIntegralType(input_type)) {
8781 if (input_type == DataType::Type::kInt64) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008782 if (isR6) {
8783 // cvt.s.l/cvt.d.l requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
8784 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
8785 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
8786 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
8787 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
8788 __ Mtc1(src_low, FTMP);
8789 __ Mthc1(src_high, FTMP);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008790 if (result_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008791 __ Cvtsl(dst, FTMP);
8792 } else {
8793 __ Cvtdl(dst, FTMP);
8794 }
8795 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008796 QuickEntrypointEnum entrypoint =
8797 (result_type == DataType::Type::kFloat32) ? kQuickL2f : kQuickL2d;
Serban Constantinescufca16662016-07-14 09:21:59 +01008798 codegen_->InvokeRuntime(entrypoint, conversion, conversion->GetDexPc());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008799 if (result_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008800 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
8801 } else {
8802 CheckEntrypointTypes<kQuickL2d, double, int64_t>();
8803 }
8804 }
8805 } else {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008806 Register src = locations->InAt(0).AsRegister<Register>();
8807 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
8808 __ Mtc1(src, FTMP);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008809 if (result_type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008810 __ Cvtsw(dst, FTMP);
8811 } else {
8812 __ Cvtdw(dst, FTMP);
8813 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008814 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008815 } else if (DataType::IsIntegralType(result_type) && DataType::IsFloatingPointType(input_type)) {
8816 CHECK(result_type == DataType::Type::kInt32 || result_type == DataType::Type::kInt64);
Lena Djokicf4e23a82017-05-09 15:43:45 +02008817
8818 // When NAN2008=1 (R6), the truncate instruction caps the output at the minimum/maximum
8819 // value of the output type if the input is outside of the range after the truncation or
8820 // produces 0 when the input is a NaN. IOW, the three special cases produce three distinct
8821 // results. This matches the desired float/double-to-int/long conversion exactly.
8822 //
8823 // When NAN2008=0 (R2 and before), the truncate instruction produces the maximum positive
8824 // value when the input is either a NaN or is outside of the range of the output type
8825 // after the truncation. IOW, the three special cases (NaN, too small, too big) produce
8826 // the same result.
8827 //
8828 // The code takes care of the different behaviors by first comparing the input to the
8829 // minimum output value (-2**-63 for truncating to long, -2**-31 for truncating to int).
8830 // If the input is greater than or equal to the minimum, it procedes to the truncate
8831 // instruction, which will handle such an input the same way irrespective of NAN2008.
8832 // Otherwise the input is compared to itself to determine whether it is a NaN or not
8833 // in order to return either zero or the minimum value.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008834 if (result_type == DataType::Type::kInt64) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008835 if (isR6) {
8836 // trunc.l.s/trunc.l.d requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
8837 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
8838 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
8839 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8840 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008841
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008842 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008843 __ TruncLS(FTMP, src);
8844 } else {
8845 __ TruncLD(FTMP, src);
8846 }
8847 __ Mfc1(dst_low, FTMP);
8848 __ Mfhc1(dst_high, FTMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008849 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008850 QuickEntrypointEnum entrypoint =
8851 (input_type == DataType::Type::kFloat32) ? kQuickF2l : kQuickD2l;
Serban Constantinescufca16662016-07-14 09:21:59 +01008852 codegen_->InvokeRuntime(entrypoint, conversion, conversion->GetDexPc());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008853 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008854 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
8855 } else {
8856 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
8857 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008858 }
8859 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008860 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
8861 Register dst = locations->Out().AsRegister<Register>();
8862 MipsLabel truncate;
8863 MipsLabel done;
8864
Lena Djokicf4e23a82017-05-09 15:43:45 +02008865 if (!isR6) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008866 if (input_type == DataType::Type::kFloat32) {
Lena Djokicf4e23a82017-05-09 15:43:45 +02008867 uint32_t min_val = bit_cast<uint32_t, float>(std::numeric_limits<int32_t>::min());
8868 __ LoadConst32(TMP, min_val);
8869 __ Mtc1(TMP, FTMP);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008870 } else {
Lena Djokicf4e23a82017-05-09 15:43:45 +02008871 uint64_t min_val = bit_cast<uint64_t, double>(std::numeric_limits<int32_t>::min());
8872 __ LoadConst32(TMP, High32Bits(min_val));
8873 __ Mtc1(ZERO, FTMP);
8874 __ MoveToFpuHigh(TMP, FTMP);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008875 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008876
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008877 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008878 __ ColeS(0, FTMP, src);
8879 } else {
8880 __ ColeD(0, FTMP, src);
8881 }
8882 __ Bc1t(0, &truncate);
8883
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008884 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008885 __ CeqS(0, src, src);
8886 } else {
8887 __ CeqD(0, src, src);
8888 }
8889 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
8890 __ Movf(dst, ZERO, 0);
Lena Djokicf4e23a82017-05-09 15:43:45 +02008891
8892 __ B(&done);
8893
8894 __ Bind(&truncate);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008895 }
8896
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008897 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008898 __ TruncWS(FTMP, src);
8899 } else {
8900 __ TruncWD(FTMP, src);
8901 }
8902 __ Mfc1(dst, FTMP);
8903
Lena Djokicf4e23a82017-05-09 15:43:45 +02008904 if (!isR6) {
8905 __ Bind(&done);
8906 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008907 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008908 } else if (DataType::IsFloatingPointType(result_type) &&
8909 DataType::IsFloatingPointType(input_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008910 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
8911 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008912 if (result_type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008913 __ Cvtsd(dst, src);
8914 } else {
8915 __ Cvtds(dst, src);
8916 }
8917 } else {
8918 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
8919 << " to " << result_type;
8920 }
8921}
8922
8923void LocationsBuilderMIPS::VisitUShr(HUShr* ushr) {
8924 HandleShift(ushr);
8925}
8926
8927void InstructionCodeGeneratorMIPS::VisitUShr(HUShr* ushr) {
8928 HandleShift(ushr);
8929}
8930
8931void LocationsBuilderMIPS::VisitXor(HXor* instruction) {
8932 HandleBinaryOp(instruction);
8933}
8934
8935void InstructionCodeGeneratorMIPS::VisitXor(HXor* instruction) {
8936 HandleBinaryOp(instruction);
8937}
8938
8939void LocationsBuilderMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
8940 // Nothing to do, this should be removed during prepare for register allocator.
8941 LOG(FATAL) << "Unreachable";
8942}
8943
8944void InstructionCodeGeneratorMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
8945 // Nothing to do, this should be removed during prepare for register allocator.
8946 LOG(FATAL) << "Unreachable";
8947}
8948
8949void LocationsBuilderMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008950 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008951}
8952
8953void InstructionCodeGeneratorMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008954 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008955}
8956
8957void LocationsBuilderMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008958 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008959}
8960
8961void InstructionCodeGeneratorMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008962 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008963}
8964
8965void LocationsBuilderMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008966 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008967}
8968
8969void InstructionCodeGeneratorMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008970 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008971}
8972
8973void LocationsBuilderMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008974 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008975}
8976
8977void InstructionCodeGeneratorMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008978 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008979}
8980
8981void LocationsBuilderMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008982 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008983}
8984
8985void InstructionCodeGeneratorMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008986 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008987}
8988
8989void LocationsBuilderMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008990 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008991}
8992
8993void InstructionCodeGeneratorMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008994 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008995}
8996
8997void LocationsBuilderMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008998 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008999}
9000
9001void InstructionCodeGeneratorMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009002 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009003}
9004
9005void LocationsBuilderMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009006 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009007}
9008
9009void InstructionCodeGeneratorMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009010 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009011}
9012
9013void LocationsBuilderMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009014 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009015}
9016
9017void InstructionCodeGeneratorMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009018 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009019}
9020
9021void LocationsBuilderMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009022 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009023}
9024
9025void InstructionCodeGeneratorMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009026 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009027}
9028
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009029void LocationsBuilderMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
9030 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01009031 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009032 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze3b8c82f2017-10-10 23:01:34 -07009033 if (!codegen_->GetInstructionSetFeatures().IsR6()) {
9034 uint32_t num_entries = switch_instr->GetNumEntries();
9035 if (num_entries > InstructionCodeGeneratorMIPS::kPackedSwitchJumpTableThreshold) {
9036 // When there's no HMipsComputeBaseMethodAddress input, R2 uses the NAL
9037 // instruction to simulate PC-relative addressing when accessing the jump table.
9038 // NAL clobbers RA. Make sure RA is preserved.
9039 codegen_->ClobberRA();
9040 }
9041 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009042}
9043
Alexey Frunze96b66822016-09-10 02:32:44 -07009044void InstructionCodeGeneratorMIPS::GenPackedSwitchWithCompares(Register value_reg,
9045 int32_t lower_bound,
9046 uint32_t num_entries,
9047 HBasicBlock* switch_block,
9048 HBasicBlock* default_block) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009049 // Create a set of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00009050 Register temp_reg = TMP;
9051 __ Addiu32(temp_reg, value_reg, -lower_bound);
9052 // Jump to default if index is negative
9053 // Note: We don't check the case that index is positive while value < lower_bound, because in
9054 // this case, index >= num_entries must be true. So that we can save one branch instruction.
9055 __ Bltz(temp_reg, codegen_->GetLabelOf(default_block));
9056
Alexey Frunze96b66822016-09-10 02:32:44 -07009057 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00009058 // Jump to successors[0] if value == lower_bound.
9059 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[0]));
9060 int32_t last_index = 0;
9061 for (; num_entries - last_index > 2; last_index += 2) {
9062 __ Addiu(temp_reg, temp_reg, -2);
9063 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
9064 __ Bltz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
9065 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
9066 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 2]));
9067 }
9068 if (num_entries - last_index == 2) {
9069 // The last missing case_value.
9070 __ Addiu(temp_reg, temp_reg, -1);
9071 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009072 }
9073
Vladimir Markof3e0ee22015-12-17 15:23:13 +00009074 // And the default for any other value.
Alexey Frunze96b66822016-09-10 02:32:44 -07009075 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009076 __ B(codegen_->GetLabelOf(default_block));
9077 }
9078}
9079
Alexey Frunze96b66822016-09-10 02:32:44 -07009080void InstructionCodeGeneratorMIPS::GenTableBasedPackedSwitch(Register value_reg,
9081 Register constant_area,
9082 int32_t lower_bound,
9083 uint32_t num_entries,
9084 HBasicBlock* switch_block,
9085 HBasicBlock* default_block) {
9086 // Create a jump table.
9087 std::vector<MipsLabel*> labels(num_entries);
9088 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
9089 for (uint32_t i = 0; i < num_entries; i++) {
9090 labels[i] = codegen_->GetLabelOf(successors[i]);
9091 }
9092 JumpTable* table = __ CreateJumpTable(std::move(labels));
9093
9094 // Is the value in range?
9095 __ Addiu32(TMP, value_reg, -lower_bound);
9096 if (IsInt<16>(static_cast<int32_t>(num_entries))) {
9097 __ Sltiu(AT, TMP, num_entries);
9098 __ Beqz(AT, codegen_->GetLabelOf(default_block));
9099 } else {
9100 __ LoadConst32(AT, num_entries);
9101 __ Bgeu(TMP, AT, codegen_->GetLabelOf(default_block));
9102 }
9103
9104 // We are in the range of the table.
9105 // Load the target address from the jump table, indexing by the value.
9106 __ LoadLabelAddress(AT, constant_area, table->GetLabel());
Chris Larsencd0295d2017-03-31 15:26:54 -07009107 __ ShiftAndAdd(TMP, TMP, AT, 2, TMP);
Alexey Frunze96b66822016-09-10 02:32:44 -07009108 __ Lw(TMP, TMP, 0);
9109 // Compute the absolute target address by adding the table start address
9110 // (the table contains offsets to targets relative to its start).
9111 __ Addu(TMP, TMP, AT);
9112 // And jump.
9113 __ Jr(TMP);
9114 __ NopIfNoReordering();
9115}
9116
9117void InstructionCodeGeneratorMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
9118 int32_t lower_bound = switch_instr->GetStartValue();
9119 uint32_t num_entries = switch_instr->GetNumEntries();
9120 LocationSummary* locations = switch_instr->GetLocations();
9121 Register value_reg = locations->InAt(0).AsRegister<Register>();
9122 HBasicBlock* switch_block = switch_instr->GetBlock();
9123 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
9124
Alexey Frunze3b8c82f2017-10-10 23:01:34 -07009125 if (num_entries > kPackedSwitchJumpTableThreshold) {
Alexey Frunze96b66822016-09-10 02:32:44 -07009126 // R6 uses PC-relative addressing to access the jump table.
Alexey Frunze3b8c82f2017-10-10 23:01:34 -07009127 //
9128 // R2, OTOH, uses an HMipsComputeBaseMethodAddress input (when available)
9129 // to access the jump table and it is implemented by changing HPackedSwitch to
9130 // HMipsPackedSwitch, which bears HMipsComputeBaseMethodAddress (see
9131 // VisitMipsPackedSwitch()).
9132 //
9133 // When there's no HMipsComputeBaseMethodAddress input (e.g. in presence of
9134 // irreducible loops), R2 uses the NAL instruction to simulate PC-relative
9135 // addressing.
Alexey Frunze96b66822016-09-10 02:32:44 -07009136 GenTableBasedPackedSwitch(value_reg,
9137 ZERO,
9138 lower_bound,
9139 num_entries,
9140 switch_block,
9141 default_block);
9142 } else {
9143 GenPackedSwitchWithCompares(value_reg,
9144 lower_bound,
9145 num_entries,
9146 switch_block,
9147 default_block);
9148 }
9149}
9150
9151void LocationsBuilderMIPS::VisitMipsPackedSwitch(HMipsPackedSwitch* switch_instr) {
9152 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01009153 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Alexey Frunze96b66822016-09-10 02:32:44 -07009154 locations->SetInAt(0, Location::RequiresRegister());
9155 // Constant area pointer (HMipsComputeBaseMethodAddress).
9156 locations->SetInAt(1, Location::RequiresRegister());
9157}
9158
9159void InstructionCodeGeneratorMIPS::VisitMipsPackedSwitch(HMipsPackedSwitch* switch_instr) {
9160 int32_t lower_bound = switch_instr->GetStartValue();
9161 uint32_t num_entries = switch_instr->GetNumEntries();
9162 LocationSummary* locations = switch_instr->GetLocations();
9163 Register value_reg = locations->InAt(0).AsRegister<Register>();
9164 Register constant_area = locations->InAt(1).AsRegister<Register>();
9165 HBasicBlock* switch_block = switch_instr->GetBlock();
9166 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
9167
9168 // This is an R2-only path. HPackedSwitch has been changed to
9169 // HMipsPackedSwitch, which bears HMipsComputeBaseMethodAddress
9170 // required to address the jump table relative to PC.
9171 GenTableBasedPackedSwitch(value_reg,
9172 constant_area,
9173 lower_bound,
9174 num_entries,
9175 switch_block,
9176 default_block);
9177}
9178
Alexey Frunzee3fb2452016-05-10 16:08:05 -07009179void LocationsBuilderMIPS::VisitMipsComputeBaseMethodAddress(
9180 HMipsComputeBaseMethodAddress* insn) {
9181 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01009182 new (GetGraph()->GetAllocator()) LocationSummary(insn, LocationSummary::kNoCall);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07009183 locations->SetOut(Location::RequiresRegister());
9184}
9185
9186void InstructionCodeGeneratorMIPS::VisitMipsComputeBaseMethodAddress(
9187 HMipsComputeBaseMethodAddress* insn) {
9188 LocationSummary* locations = insn->GetLocations();
9189 Register reg = locations->Out().AsRegister<Register>();
9190
9191 CHECK(!codegen_->GetInstructionSetFeatures().IsR6());
9192
9193 // Generate a dummy PC-relative call to obtain PC.
9194 __ Nal();
9195 // Grab the return address off RA.
9196 __ Move(reg, RA);
9197
9198 // Remember this offset (the obtained PC value) for later use with constant area.
9199 __ BindPcRelBaseLabel();
9200}
9201
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009202void LocationsBuilderMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
9203 // The trampoline uses the same calling convention as dex calling conventions,
9204 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
9205 // the method_idx.
9206 HandleInvoke(invoke);
9207}
9208
9209void InstructionCodeGeneratorMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
9210 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
9211}
9212
Roland Levillain2aba7cd2016-02-03 12:27:20 +00009213void LocationsBuilderMIPS::VisitClassTableGet(HClassTableGet* instruction) {
9214 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01009215 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Roland Levillain2aba7cd2016-02-03 12:27:20 +00009216 locations->SetInAt(0, Location::RequiresRegister());
9217 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00009218}
9219
Roland Levillain2aba7cd2016-02-03 12:27:20 +00009220void InstructionCodeGeneratorMIPS::VisitClassTableGet(HClassTableGet* instruction) {
9221 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00009222 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01009223 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Roland Levillain2aba7cd2016-02-03 12:27:20 +00009224 instruction->GetIndex(), kMipsPointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01009225 __ LoadFromOffset(kLoadWord,
9226 locations->Out().AsRegister<Register>(),
9227 locations->InAt(0).AsRegister<Register>(),
9228 method_offset);
Roland Levillain2aba7cd2016-02-03 12:27:20 +00009229 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01009230 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00009231 instruction->GetIndex(), kMipsPointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00009232 __ LoadFromOffset(kLoadWord,
9233 locations->Out().AsRegister<Register>(),
9234 locations->InAt(0).AsRegister<Register>(),
9235 mirror::Class::ImtPtrOffset(kMipsPointerSize).Uint32Value());
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01009236 __ LoadFromOffset(kLoadWord,
9237 locations->Out().AsRegister<Register>(),
9238 locations->Out().AsRegister<Register>(),
9239 method_offset);
Roland Levillain2aba7cd2016-02-03 12:27:20 +00009240 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00009241}
9242
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009243#undef __
9244#undef QUICK_ENTRY_POINT
9245
9246} // namespace mips
9247} // namespace art