blob: 70c8a5b52353aa478a6be423117d9e49f5ac86ce [file] [log] [blame]
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_mips.h"
18
Alexey Frunze4147fcc2017-06-17 19:57:27 -070019#include "arch/mips/asm_support_mips.h"
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020020#include "arch/mips/entrypoints_direct_mips.h"
21#include "arch/mips/instruction_set_features_mips.h"
22#include "art_method.h"
Vladimir Marko94ec2db2017-09-06 17:21:03 +010023#include "class_table.h"
Chris Larsen701566a2015-10-27 15:29:13 -070024#include "code_generator_utils.h"
Vladimir Marko3a21e382016-09-02 12:38:38 +010025#include "compiled_method.h"
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020026#include "entrypoints/quick/quick_entrypoints.h"
27#include "entrypoints/quick/quick_entrypoints_enum.h"
28#include "gc/accounting/card_table.h"
Andreas Gampe09659c22017-09-18 18:23:32 -070029#include "heap_poisoning.h"
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020030#include "intrinsics.h"
Chris Larsen701566a2015-10-27 15:29:13 -070031#include "intrinsics_mips.h"
Vladimir Markod8dbc8d2017-09-20 13:37:47 +010032#include "linker/linker_patch.h"
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020033#include "mirror/array-inl.h"
34#include "mirror/class-inl.h"
35#include "offsets.h"
36#include "thread.h"
37#include "utils/assembler.h"
38#include "utils/mips/assembler_mips.h"
39#include "utils/stack_checks.h"
40
41namespace art {
42namespace mips {
43
44static constexpr int kCurrentMethodStackOffset = 0;
45static constexpr Register kMethodRegisterArgument = A0;
46
Alexey Frunze4147fcc2017-06-17 19:57:27 -070047// Flags controlling the use of thunks for Baker read barriers.
48constexpr bool kBakerReadBarrierThunksEnableForFields = true;
49constexpr bool kBakerReadBarrierThunksEnableForArrays = true;
50constexpr bool kBakerReadBarrierThunksEnableForGcRoots = true;
51
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010052Location MipsReturnLocation(DataType::Type return_type) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020053 switch (return_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +010054 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010055 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +010056 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010057 case DataType::Type::kInt8:
58 case DataType::Type::kUint16:
59 case DataType::Type::kInt16:
60 case DataType::Type::kInt32:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020061 return Location::RegisterLocation(V0);
62
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010063 case DataType::Type::kInt64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020064 return Location::RegisterPairLocation(V0, V1);
65
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010066 case DataType::Type::kFloat32:
67 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020068 return Location::FpuRegisterLocation(F0);
69
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010070 case DataType::Type::kVoid:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020071 return Location();
72 }
73 UNREACHABLE();
74}
75
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010076Location InvokeDexCallingConventionVisitorMIPS::GetReturnLocation(DataType::Type type) const {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020077 return MipsReturnLocation(type);
78}
79
80Location InvokeDexCallingConventionVisitorMIPS::GetMethodLocation() const {
81 return Location::RegisterLocation(kMethodRegisterArgument);
82}
83
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010084Location InvokeDexCallingConventionVisitorMIPS::GetNextLocation(DataType::Type type) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020085 Location next_location;
86
87 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +010088 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010089 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +010090 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010091 case DataType::Type::kInt8:
92 case DataType::Type::kUint16:
93 case DataType::Type::kInt16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +010094 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020095 uint32_t gp_index = gp_index_++;
96 if (gp_index < calling_convention.GetNumberOfRegisters()) {
97 next_location = Location::RegisterLocation(calling_convention.GetRegisterAt(gp_index));
98 } else {
99 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
100 next_location = Location::StackSlot(stack_offset);
101 }
102 break;
103 }
104
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100105 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200106 uint32_t gp_index = gp_index_;
107 gp_index_ += 2;
108 if (gp_index + 1 < calling_convention.GetNumberOfRegisters()) {
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800109 Register reg = calling_convention.GetRegisterAt(gp_index);
110 if (reg == A1 || reg == A3) {
111 gp_index_++; // Skip A1(A3), and use A2_A3(T0_T1) instead.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200112 gp_index++;
113 }
114 Register low_even = calling_convention.GetRegisterAt(gp_index);
115 Register high_odd = calling_convention.GetRegisterAt(gp_index + 1);
116 DCHECK_EQ(low_even + 1, high_odd);
117 next_location = Location::RegisterPairLocation(low_even, high_odd);
118 } else {
119 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
120 next_location = Location::DoubleStackSlot(stack_offset);
121 }
122 break;
123 }
124
125 // Note: both float and double types are stored in even FPU registers. On 32 bit FPU, double
126 // will take up the even/odd pair, while floats are stored in even regs only.
127 // On 64 bit FPU, both double and float are stored in even registers only.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100128 case DataType::Type::kFloat32:
129 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200130 uint32_t float_index = float_index_++;
131 if (float_index < calling_convention.GetNumberOfFpuRegisters()) {
132 next_location = Location::FpuRegisterLocation(
133 calling_convention.GetFpuRegisterAt(float_index));
134 } else {
135 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100136 next_location = DataType::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
137 : Location::StackSlot(stack_offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200138 }
139 break;
140 }
141
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100142 case DataType::Type::kVoid:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200143 LOG(FATAL) << "Unexpected parameter type " << type;
144 break;
145 }
146
147 // Space on the stack is reserved for all arguments.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100148 stack_index_ += DataType::Is64BitType(type) ? 2 : 1;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200149
150 return next_location;
151}
152
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100153Location InvokeRuntimeCallingConvention::GetReturnLocation(DataType::Type type) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200154 return MipsReturnLocation(type);
155}
156
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100157// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
158#define __ down_cast<CodeGeneratorMIPS*>(codegen)->GetAssembler()-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -0700159#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMipsPointerSize, x).Int32Value()
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200160
161class BoundsCheckSlowPathMIPS : public SlowPathCodeMIPS {
162 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000163 explicit BoundsCheckSlowPathMIPS(HBoundsCheck* instruction) : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200164
165 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
166 LocationSummary* locations = instruction_->GetLocations();
167 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
168 __ Bind(GetEntryLabel());
169 if (instruction_->CanThrowIntoCatchBlock()) {
170 // Live registers will be restored in the catch block if caught.
171 SaveLiveRegisters(codegen, instruction_->GetLocations());
172 }
173 // We're moving two locations to locations that could overlap, so we need a parallel
174 // move resolver.
175 InvokeRuntimeCallingConvention calling_convention;
176 codegen->EmitParallelMoves(locations->InAt(0),
177 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100178 DataType::Type::kInt32,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200179 locations->InAt(1),
180 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100181 DataType::Type::kInt32);
Serban Constantinescufca16662016-07-14 09:21:59 +0100182 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
183 ? kQuickThrowStringBounds
184 : kQuickThrowArrayBounds;
185 mips_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100186 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200187 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
188 }
189
190 bool IsFatal() const OVERRIDE { return true; }
191
192 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathMIPS"; }
193
194 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200195 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathMIPS);
196};
197
198class DivZeroCheckSlowPathMIPS : public SlowPathCodeMIPS {
199 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000200 explicit DivZeroCheckSlowPathMIPS(HDivZeroCheck* instruction) : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200201
202 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
203 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
204 __ Bind(GetEntryLabel());
Serban Constantinescufca16662016-07-14 09:21:59 +0100205 mips_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200206 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
207 }
208
209 bool IsFatal() const OVERRIDE { return true; }
210
211 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathMIPS"; }
212
213 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200214 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathMIPS);
215};
216
217class LoadClassSlowPathMIPS : public SlowPathCodeMIPS {
218 public:
219 LoadClassSlowPathMIPS(HLoadClass* cls,
220 HInstruction* at,
221 uint32_t dex_pc,
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700222 bool do_clinit,
223 const CodeGeneratorMIPS::PcRelativePatchInfo* bss_info_high = nullptr)
224 : SlowPathCodeMIPS(at),
225 cls_(cls),
226 dex_pc_(dex_pc),
227 do_clinit_(do_clinit),
228 bss_info_high_(bss_info_high) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200229 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
230 }
231
232 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000233 LocationSummary* locations = instruction_->GetLocations();
Alexey Frunzec61c0762017-04-10 13:54:23 -0700234 Location out = locations->Out();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200235 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700236 const bool baker_or_no_read_barriers = (!kUseReadBarrier || kUseBakerReadBarrier);
Alexey Frunzec61c0762017-04-10 13:54:23 -0700237 InvokeRuntimeCallingConvention calling_convention;
238 DCHECK_EQ(instruction_->IsLoadClass(), cls_ == instruction_);
239 const bool is_load_class_bss_entry =
240 (cls_ == instruction_) && (cls_->GetLoadKind() == HLoadClass::LoadKind::kBssEntry);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200241 __ Bind(GetEntryLabel());
242 SaveLiveRegisters(codegen, locations);
243
Alexey Frunzec61c0762017-04-10 13:54:23 -0700244 // For HLoadClass/kBssEntry/kSaveEverything, make sure we preserve the address of the entry.
245 Register entry_address = kNoRegister;
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700246 if (is_load_class_bss_entry && baker_or_no_read_barriers) {
Alexey Frunzec61c0762017-04-10 13:54:23 -0700247 Register temp = locations->GetTemp(0).AsRegister<Register>();
248 bool temp_is_a0 = (temp == calling_convention.GetRegisterAt(0));
249 // In the unlucky case that `temp` is A0, we preserve the address in `out` across the
250 // kSaveEverything call.
251 entry_address = temp_is_a0 ? out.AsRegister<Register>() : temp;
252 DCHECK_NE(entry_address, calling_convention.GetRegisterAt(0));
253 if (temp_is_a0) {
254 __ Move(entry_address, temp);
255 }
256 }
257
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000258 dex::TypeIndex type_index = cls_->GetTypeIndex();
259 __ LoadConst32(calling_convention.GetRegisterAt(0), type_index.index_);
Serban Constantinescufca16662016-07-14 09:21:59 +0100260 QuickEntrypointEnum entrypoint = do_clinit_ ? kQuickInitializeStaticStorage
261 : kQuickInitializeType;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000262 mips_codegen->InvokeRuntime(entrypoint, instruction_, dex_pc_, this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200263 if (do_clinit_) {
264 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
265 } else {
266 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
267 }
268
Alexey Frunzec61c0762017-04-10 13:54:23 -0700269 // For HLoadClass/kBssEntry, store the resolved class to the BSS entry.
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700270 if (is_load_class_bss_entry && baker_or_no_read_barriers) {
Alexey Frunzec61c0762017-04-10 13:54:23 -0700271 // The class entry address was preserved in `entry_address` thanks to kSaveEverything.
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700272 DCHECK(bss_info_high_);
273 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
274 mips_codegen->NewTypeBssEntryPatch(cls_->GetDexFile(), type_index, bss_info_high_);
Alexey Frunzea663d9d2017-07-31 18:43:18 -0700275 __ Sw(calling_convention.GetRegisterAt(0),
276 entry_address,
277 /* placeholder */ 0x5678,
278 &info_low->label);
Alexey Frunzec61c0762017-04-10 13:54:23 -0700279 }
280
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200281 // Move the class to the desired location.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200282 if (out.IsValid()) {
283 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100284 DataType::Type type = instruction_->GetType();
Alexey Frunzec61c0762017-04-10 13:54:23 -0700285 mips_codegen->MoveLocation(out,
286 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
287 type);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200288 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200289 RestoreLiveRegisters(codegen, locations);
Alexey Frunzec61c0762017-04-10 13:54:23 -0700290
291 // For HLoadClass/kBssEntry, store the resolved class to the BSS entry.
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700292 if (is_load_class_bss_entry && !baker_or_no_read_barriers) {
293 // For non-Baker read barriers we need to re-calculate the address of
Alexey Frunzec61c0762017-04-10 13:54:23 -0700294 // the class entry.
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700295 const bool isR6 = mips_codegen->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +0200296 const bool has_irreducible_loops = codegen->GetGraph()->HasIrreducibleLoops();
297 Register base =
298 (isR6 || has_irreducible_loops) ? ZERO : locations->InAt(0).AsRegister<Register>();
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700299 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
Vladimir Marko1998cd02017-01-13 13:02:58 +0000300 mips_codegen->NewTypeBssEntryPatch(cls_->GetDexFile(), type_index);
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700301 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
302 mips_codegen->NewTypeBssEntryPatch(cls_->GetDexFile(), type_index, info_high);
Alexey Frunzea663d9d2017-07-31 18:43:18 -0700303 mips_codegen->EmitPcRelativeAddressPlaceholderHigh(info_high, TMP, base);
304 __ Sw(out.AsRegister<Register>(), TMP, /* placeholder */ 0x5678, &info_low->label);
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000305 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200306 __ B(GetExitLabel());
307 }
308
309 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathMIPS"; }
310
311 private:
312 // The class this slow path will load.
313 HLoadClass* const cls_;
314
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200315 // The dex PC of `at_`.
316 const uint32_t dex_pc_;
317
318 // Whether to initialize the class.
319 const bool do_clinit_;
320
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700321 // Pointer to the high half PC-relative patch info for HLoadClass/kBssEntry.
322 const CodeGeneratorMIPS::PcRelativePatchInfo* bss_info_high_;
323
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200324 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathMIPS);
325};
326
327class LoadStringSlowPathMIPS : public SlowPathCodeMIPS {
328 public:
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700329 explicit LoadStringSlowPathMIPS(HLoadString* instruction,
330 const CodeGeneratorMIPS::PcRelativePatchInfo* bss_info_high)
331 : SlowPathCodeMIPS(instruction), bss_info_high_(bss_info_high) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200332
333 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexey Frunzec61c0762017-04-10 13:54:23 -0700334 DCHECK(instruction_->IsLoadString());
335 DCHECK_EQ(instruction_->AsLoadString()->GetLoadKind(), HLoadString::LoadKind::kBssEntry);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200336 LocationSummary* locations = instruction_->GetLocations();
337 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Alexey Frunzec61c0762017-04-10 13:54:23 -0700338 HLoadString* load = instruction_->AsLoadString();
339 const dex::StringIndex string_index = load->GetStringIndex();
340 Register out = locations->Out().AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200341 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700342 const bool baker_or_no_read_barriers = (!kUseReadBarrier || kUseBakerReadBarrier);
Alexey Frunzec61c0762017-04-10 13:54:23 -0700343 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200344 __ Bind(GetEntryLabel());
345 SaveLiveRegisters(codegen, locations);
346
Alexey Frunzec61c0762017-04-10 13:54:23 -0700347 // For HLoadString/kBssEntry/kSaveEverything, make sure we preserve the address of the entry.
348 Register entry_address = kNoRegister;
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700349 if (baker_or_no_read_barriers) {
Alexey Frunzec61c0762017-04-10 13:54:23 -0700350 Register temp = locations->GetTemp(0).AsRegister<Register>();
351 bool temp_is_a0 = (temp == calling_convention.GetRegisterAt(0));
352 // In the unlucky case that `temp` is A0, we preserve the address in `out` across the
353 // kSaveEverything call.
354 entry_address = temp_is_a0 ? out : temp;
355 DCHECK_NE(entry_address, calling_convention.GetRegisterAt(0));
356 if (temp_is_a0) {
357 __ Move(entry_address, temp);
358 }
359 }
360
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000361 __ LoadConst32(calling_convention.GetRegisterAt(0), string_index.index_);
Serban Constantinescufca16662016-07-14 09:21:59 +0100362 mips_codegen->InvokeRuntime(kQuickResolveString, instruction_, instruction_->GetDexPc(), this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200363 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexey Frunzec61c0762017-04-10 13:54:23 -0700364
365 // Store the resolved string to the BSS entry.
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700366 if (baker_or_no_read_barriers) {
Alexey Frunzec61c0762017-04-10 13:54:23 -0700367 // The string entry address was preserved in `entry_address` thanks to kSaveEverything.
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700368 DCHECK(bss_info_high_);
369 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +0100370 mips_codegen->NewStringBssEntryPatch(load->GetDexFile(), string_index, bss_info_high_);
Alexey Frunzea663d9d2017-07-31 18:43:18 -0700371 __ Sw(calling_convention.GetRegisterAt(0),
372 entry_address,
373 /* placeholder */ 0x5678,
374 &info_low->label);
Alexey Frunzec61c0762017-04-10 13:54:23 -0700375 }
376
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100377 DataType::Type type = instruction_->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200378 mips_codegen->MoveLocation(locations->Out(),
Alexey Frunzec61c0762017-04-10 13:54:23 -0700379 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200380 type);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200381 RestoreLiveRegisters(codegen, locations);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000382
Alexey Frunzec61c0762017-04-10 13:54:23 -0700383 // Store the resolved string to the BSS entry.
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700384 if (!baker_or_no_read_barriers) {
385 // For non-Baker read barriers we need to re-calculate the address of
Alexey Frunzec61c0762017-04-10 13:54:23 -0700386 // the string entry.
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700387 const bool isR6 = mips_codegen->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +0200388 const bool has_irreducible_loops = codegen->GetGraph()->HasIrreducibleLoops();
389 Register base =
390 (isR6 || has_irreducible_loops) ? ZERO : locations->InAt(0).AsRegister<Register>();
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700391 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +0100392 mips_codegen->NewStringBssEntryPatch(load->GetDexFile(), string_index);
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700393 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +0100394 mips_codegen->NewStringBssEntryPatch(load->GetDexFile(), string_index, info_high);
Alexey Frunzea663d9d2017-07-31 18:43:18 -0700395 mips_codegen->EmitPcRelativeAddressPlaceholderHigh(info_high, TMP, base);
396 __ Sw(out, TMP, /* placeholder */ 0x5678, &info_low->label);
Alexey Frunzec61c0762017-04-10 13:54:23 -0700397 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200398 __ B(GetExitLabel());
399 }
400
401 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathMIPS"; }
402
403 private:
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700404 // Pointer to the high half PC-relative patch info.
405 const CodeGeneratorMIPS::PcRelativePatchInfo* bss_info_high_;
406
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200407 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathMIPS);
408};
409
410class NullCheckSlowPathMIPS : public SlowPathCodeMIPS {
411 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000412 explicit NullCheckSlowPathMIPS(HNullCheck* instr) : SlowPathCodeMIPS(instr) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200413
414 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
415 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
416 __ Bind(GetEntryLabel());
417 if (instruction_->CanThrowIntoCatchBlock()) {
418 // Live registers will be restored in the catch block if caught.
419 SaveLiveRegisters(codegen, instruction_->GetLocations());
420 }
Serban Constantinescufca16662016-07-14 09:21:59 +0100421 mips_codegen->InvokeRuntime(kQuickThrowNullPointer,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200422 instruction_,
423 instruction_->GetDexPc(),
Serban Constantinescufca16662016-07-14 09:21:59 +0100424 this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200425 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
426 }
427
428 bool IsFatal() const OVERRIDE { return true; }
429
430 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathMIPS"; }
431
432 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200433 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathMIPS);
434};
435
436class SuspendCheckSlowPathMIPS : public SlowPathCodeMIPS {
437 public:
438 SuspendCheckSlowPathMIPS(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000439 : SlowPathCodeMIPS(instruction), successor_(successor) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200440
441 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Lena Djokicca8c2952017-05-29 11:31:46 +0200442 LocationSummary* locations = instruction_->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200443 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
444 __ Bind(GetEntryLabel());
Lena Djokicca8c2952017-05-29 11:31:46 +0200445 SaveLiveRegisters(codegen, locations); // Only saves live vector registers for SIMD.
Serban Constantinescufca16662016-07-14 09:21:59 +0100446 mips_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200447 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Lena Djokicca8c2952017-05-29 11:31:46 +0200448 RestoreLiveRegisters(codegen, locations); // Only restores live vector registers for SIMD.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200449 if (successor_ == nullptr) {
450 __ B(GetReturnLabel());
451 } else {
452 __ B(mips_codegen->GetLabelOf(successor_));
453 }
454 }
455
456 MipsLabel* GetReturnLabel() {
457 DCHECK(successor_ == nullptr);
458 return &return_label_;
459 }
460
461 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathMIPS"; }
462
463 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200464 // If not null, the block to branch to after the suspend check.
465 HBasicBlock* const successor_;
466
467 // If `successor_` is null, the label to branch to after the suspend check.
468 MipsLabel return_label_;
469
470 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathMIPS);
471};
472
473class TypeCheckSlowPathMIPS : public SlowPathCodeMIPS {
474 public:
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800475 explicit TypeCheckSlowPathMIPS(HInstruction* instruction, bool is_fatal)
476 : SlowPathCodeMIPS(instruction), is_fatal_(is_fatal) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200477
478 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
479 LocationSummary* locations = instruction_->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200480 uint32_t dex_pc = instruction_->GetDexPc();
481 DCHECK(instruction_->IsCheckCast()
482 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
483 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
484
485 __ Bind(GetEntryLabel());
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800486 if (!is_fatal_) {
487 SaveLiveRegisters(codegen, locations);
488 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200489
490 // We're moving two locations to locations that could overlap, so we need a parallel
491 // move resolver.
492 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800493 codegen->EmitParallelMoves(locations->InAt(0),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200494 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100495 DataType::Type::kReference,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800496 locations->InAt(1),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200497 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100498 DataType::Type::kReference);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200499 if (instruction_->IsInstanceOf()) {
Serban Constantinescufca16662016-07-14 09:21:59 +0100500 mips_codegen->InvokeRuntime(kQuickInstanceofNonTrivial, instruction_, dex_pc, this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800501 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100502 DataType::Type ret_type = instruction_->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200503 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
504 mips_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200505 } else {
506 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800507 mips_codegen->InvokeRuntime(kQuickCheckInstanceOf, instruction_, dex_pc, this);
508 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200509 }
510
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800511 if (!is_fatal_) {
512 RestoreLiveRegisters(codegen, locations);
513 __ B(GetExitLabel());
514 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200515 }
516
517 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathMIPS"; }
518
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800519 bool IsFatal() const OVERRIDE { return is_fatal_; }
520
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200521 private:
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800522 const bool is_fatal_;
523
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200524 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathMIPS);
525};
526
527class DeoptimizationSlowPathMIPS : public SlowPathCodeMIPS {
528 public:
Aart Bik42249c32016-01-07 15:33:50 -0800529 explicit DeoptimizationSlowPathMIPS(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000530 : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200531
532 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800533 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200534 __ Bind(GetEntryLabel());
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100535 LocationSummary* locations = instruction_->GetLocations();
536 SaveLiveRegisters(codegen, locations);
537 InvokeRuntimeCallingConvention calling_convention;
538 __ LoadConst32(calling_convention.GetRegisterAt(0),
539 static_cast<uint32_t>(instruction_->AsDeoptimize()->GetDeoptimizationKind()));
Serban Constantinescufca16662016-07-14 09:21:59 +0100540 mips_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100541 CheckEntrypointTypes<kQuickDeoptimize, void, DeoptimizationKind>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200542 }
543
544 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathMIPS"; }
545
546 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200547 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathMIPS);
548};
549
Alexey Frunze15958152017-02-09 19:08:30 -0800550class ArraySetSlowPathMIPS : public SlowPathCodeMIPS {
551 public:
552 explicit ArraySetSlowPathMIPS(HInstruction* instruction) : SlowPathCodeMIPS(instruction) {}
553
554 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
555 LocationSummary* locations = instruction_->GetLocations();
556 __ Bind(GetEntryLabel());
557 SaveLiveRegisters(codegen, locations);
558
559 InvokeRuntimeCallingConvention calling_convention;
560 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
561 parallel_move.AddMove(
562 locations->InAt(0),
563 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100564 DataType::Type::kReference,
Alexey Frunze15958152017-02-09 19:08:30 -0800565 nullptr);
566 parallel_move.AddMove(
567 locations->InAt(1),
568 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100569 DataType::Type::kInt32,
Alexey Frunze15958152017-02-09 19:08:30 -0800570 nullptr);
571 parallel_move.AddMove(
572 locations->InAt(2),
573 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100574 DataType::Type::kReference,
Alexey Frunze15958152017-02-09 19:08:30 -0800575 nullptr);
576 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
577
578 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
579 mips_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
580 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
581 RestoreLiveRegisters(codegen, locations);
582 __ B(GetExitLabel());
583 }
584
585 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathMIPS"; }
586
587 private:
588 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathMIPS);
589};
590
591// Slow path marking an object reference `ref` during a read
592// barrier. The field `obj.field` in the object `obj` holding this
593// reference does not get updated by this slow path after marking (see
594// ReadBarrierMarkAndUpdateFieldSlowPathMIPS below for that).
595//
596// This means that after the execution of this slow path, `ref` will
597// always be up-to-date, but `obj.field` may not; i.e., after the
598// flip, `ref` will be a to-space reference, but `obj.field` will
599// probably still be a from-space reference (unless it gets updated by
600// another thread, or if another thread installed another object
601// reference (different from `ref`) in `obj.field`).
602//
603// If `entrypoint` is a valid location it is assumed to already be
604// holding the entrypoint. The case where the entrypoint is passed in
605// is for the GcRoot read barrier.
606class ReadBarrierMarkSlowPathMIPS : public SlowPathCodeMIPS {
607 public:
608 ReadBarrierMarkSlowPathMIPS(HInstruction* instruction,
609 Location ref,
610 Location entrypoint = Location::NoLocation())
611 : SlowPathCodeMIPS(instruction), ref_(ref), entrypoint_(entrypoint) {
612 DCHECK(kEmitCompilerReadBarrier);
613 }
614
615 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathMIPS"; }
616
617 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
618 LocationSummary* locations = instruction_->GetLocations();
619 Register ref_reg = ref_.AsRegister<Register>();
620 DCHECK(locations->CanCall());
621 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
622 DCHECK(instruction_->IsInstanceFieldGet() ||
623 instruction_->IsStaticFieldGet() ||
624 instruction_->IsArrayGet() ||
625 instruction_->IsArraySet() ||
626 instruction_->IsLoadClass() ||
627 instruction_->IsLoadString() ||
628 instruction_->IsInstanceOf() ||
629 instruction_->IsCheckCast() ||
630 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
631 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
632 << "Unexpected instruction in read barrier marking slow path: "
633 << instruction_->DebugName();
634
635 __ Bind(GetEntryLabel());
636 // No need to save live registers; it's taken care of by the
637 // entrypoint. Also, there is no need to update the stack mask,
638 // as this runtime call will not trigger a garbage collection.
639 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
640 DCHECK((V0 <= ref_reg && ref_reg <= T7) ||
641 (S2 <= ref_reg && ref_reg <= S7) ||
642 (ref_reg == FP)) << ref_reg;
643 // "Compact" slow path, saving two moves.
644 //
645 // Instead of using the standard runtime calling convention (input
646 // and output in A0 and V0 respectively):
647 //
648 // A0 <- ref
649 // V0 <- ReadBarrierMark(A0)
650 // ref <- V0
651 //
652 // we just use rX (the register containing `ref`) as input and output
653 // of a dedicated entrypoint:
654 //
655 // rX <- ReadBarrierMarkRegX(rX)
656 //
657 if (entrypoint_.IsValid()) {
658 mips_codegen->ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction_, this);
659 DCHECK_EQ(entrypoint_.AsRegister<Register>(), T9);
660 __ Jalr(entrypoint_.AsRegister<Register>());
661 __ NopIfNoReordering();
662 } else {
663 int32_t entry_point_offset =
Roland Levillain97c46462017-05-11 14:04:03 +0100664 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(ref_reg - 1);
Alexey Frunze15958152017-02-09 19:08:30 -0800665 // This runtime call does not require a stack map.
666 mips_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset,
667 instruction_,
668 this,
669 /* direct */ false);
670 }
671 __ B(GetExitLabel());
672 }
673
674 private:
675 // The location (register) of the marked object reference.
676 const Location ref_;
677
678 // The location of the entrypoint if already loaded.
679 const Location entrypoint_;
680
681 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathMIPS);
682};
683
684// Slow path marking an object reference `ref` during a read barrier,
685// and if needed, atomically updating the field `obj.field` in the
686// object `obj` holding this reference after marking (contrary to
687// ReadBarrierMarkSlowPathMIPS above, which never tries to update
688// `obj.field`).
689//
690// This means that after the execution of this slow path, both `ref`
691// and `obj.field` will be up-to-date; i.e., after the flip, both will
692// hold the same to-space reference (unless another thread installed
693// another object reference (different from `ref`) in `obj.field`).
694class ReadBarrierMarkAndUpdateFieldSlowPathMIPS : public SlowPathCodeMIPS {
695 public:
696 ReadBarrierMarkAndUpdateFieldSlowPathMIPS(HInstruction* instruction,
697 Location ref,
698 Register obj,
699 Location field_offset,
700 Register temp1)
701 : SlowPathCodeMIPS(instruction),
702 ref_(ref),
703 obj_(obj),
704 field_offset_(field_offset),
705 temp1_(temp1) {
706 DCHECK(kEmitCompilerReadBarrier);
707 }
708
709 const char* GetDescription() const OVERRIDE {
710 return "ReadBarrierMarkAndUpdateFieldSlowPathMIPS";
711 }
712
713 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
714 LocationSummary* locations = instruction_->GetLocations();
715 Register ref_reg = ref_.AsRegister<Register>();
716 DCHECK(locations->CanCall());
717 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
718 // This slow path is only used by the UnsafeCASObject intrinsic.
719 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
720 << "Unexpected instruction in read barrier marking and field updating slow path: "
721 << instruction_->DebugName();
722 DCHECK(instruction_->GetLocations()->Intrinsified());
723 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
724 DCHECK(field_offset_.IsRegisterPair()) << field_offset_;
725
726 __ Bind(GetEntryLabel());
727
728 // Save the old reference.
729 // Note that we cannot use AT or TMP to save the old reference, as those
730 // are used by the code that follows, but we need the old reference after
731 // the call to the ReadBarrierMarkRegX entry point.
732 DCHECK_NE(temp1_, AT);
733 DCHECK_NE(temp1_, TMP);
734 __ Move(temp1_, ref_reg);
735
736 // No need to save live registers; it's taken care of by the
737 // entrypoint. Also, there is no need to update the stack mask,
738 // as this runtime call will not trigger a garbage collection.
739 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
740 DCHECK((V0 <= ref_reg && ref_reg <= T7) ||
741 (S2 <= ref_reg && ref_reg <= S7) ||
742 (ref_reg == FP)) << ref_reg;
743 // "Compact" slow path, saving two moves.
744 //
745 // Instead of using the standard runtime calling convention (input
746 // and output in A0 and V0 respectively):
747 //
748 // A0 <- ref
749 // V0 <- ReadBarrierMark(A0)
750 // ref <- V0
751 //
752 // we just use rX (the register containing `ref`) as input and output
753 // of a dedicated entrypoint:
754 //
755 // rX <- ReadBarrierMarkRegX(rX)
756 //
757 int32_t entry_point_offset =
Roland Levillain97c46462017-05-11 14:04:03 +0100758 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(ref_reg - 1);
Alexey Frunze15958152017-02-09 19:08:30 -0800759 // This runtime call does not require a stack map.
760 mips_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset,
761 instruction_,
762 this,
763 /* direct */ false);
764
765 // If the new reference is different from the old reference,
766 // update the field in the holder (`*(obj_ + field_offset_)`).
767 //
768 // Note that this field could also hold a different object, if
769 // another thread had concurrently changed it. In that case, the
770 // the compare-and-set (CAS) loop below would abort, leaving the
771 // field as-is.
772 MipsLabel done;
773 __ Beq(temp1_, ref_reg, &done);
774
775 // Update the the holder's field atomically. This may fail if
776 // mutator updates before us, but it's OK. This is achieved
777 // using a strong compare-and-set (CAS) operation with relaxed
778 // memory synchronization ordering, where the expected value is
779 // the old reference and the desired value is the new reference.
780
781 // Convenience aliases.
782 Register base = obj_;
783 // The UnsafeCASObject intrinsic uses a register pair as field
784 // offset ("long offset"), of which only the low part contains
785 // data.
786 Register offset = field_offset_.AsRegisterPairLow<Register>();
787 Register expected = temp1_;
788 Register value = ref_reg;
789 Register tmp_ptr = TMP; // Pointer to actual memory.
790 Register tmp = AT; // Value in memory.
791
792 __ Addu(tmp_ptr, base, offset);
793
794 if (kPoisonHeapReferences) {
795 __ PoisonHeapReference(expected);
796 // Do not poison `value` if it is the same register as
797 // `expected`, which has just been poisoned.
798 if (value != expected) {
799 __ PoisonHeapReference(value);
800 }
801 }
802
803 // do {
804 // tmp = [r_ptr] - expected;
805 // } while (tmp == 0 && failure([r_ptr] <- r_new_value));
806
807 bool is_r6 = mips_codegen->GetInstructionSetFeatures().IsR6();
808 MipsLabel loop_head, exit_loop;
809 __ Bind(&loop_head);
810 if (is_r6) {
811 __ LlR6(tmp, tmp_ptr);
812 } else {
813 __ LlR2(tmp, tmp_ptr);
814 }
815 __ Bne(tmp, expected, &exit_loop);
816 __ Move(tmp, value);
817 if (is_r6) {
818 __ ScR6(tmp, tmp_ptr);
819 } else {
820 __ ScR2(tmp, tmp_ptr);
821 }
822 __ Beqz(tmp, &loop_head);
823 __ Bind(&exit_loop);
824
825 if (kPoisonHeapReferences) {
826 __ UnpoisonHeapReference(expected);
827 // Do not unpoison `value` if it is the same register as
828 // `expected`, which has just been unpoisoned.
829 if (value != expected) {
830 __ UnpoisonHeapReference(value);
831 }
832 }
833
834 __ Bind(&done);
835 __ B(GetExitLabel());
836 }
837
838 private:
839 // The location (register) of the marked object reference.
840 const Location ref_;
841 // The register containing the object holding the marked object reference field.
842 const Register obj_;
843 // The location of the offset of the marked reference field within `obj_`.
844 Location field_offset_;
845
846 const Register temp1_;
847
848 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkAndUpdateFieldSlowPathMIPS);
849};
850
851// Slow path generating a read barrier for a heap reference.
852class ReadBarrierForHeapReferenceSlowPathMIPS : public SlowPathCodeMIPS {
853 public:
854 ReadBarrierForHeapReferenceSlowPathMIPS(HInstruction* instruction,
855 Location out,
856 Location ref,
857 Location obj,
858 uint32_t offset,
859 Location index)
860 : SlowPathCodeMIPS(instruction),
861 out_(out),
862 ref_(ref),
863 obj_(obj),
864 offset_(offset),
865 index_(index) {
866 DCHECK(kEmitCompilerReadBarrier);
867 // If `obj` is equal to `out` or `ref`, it means the initial object
868 // has been overwritten by (or after) the heap object reference load
869 // to be instrumented, e.g.:
870 //
871 // __ LoadFromOffset(kLoadWord, out, out, offset);
872 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
873 //
874 // In that case, we have lost the information about the original
875 // object, and the emitted read barrier cannot work properly.
876 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
877 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
878 }
879
880 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
881 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
882 LocationSummary* locations = instruction_->GetLocations();
883 Register reg_out = out_.AsRegister<Register>();
884 DCHECK(locations->CanCall());
885 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
886 DCHECK(instruction_->IsInstanceFieldGet() ||
887 instruction_->IsStaticFieldGet() ||
888 instruction_->IsArrayGet() ||
889 instruction_->IsInstanceOf() ||
890 instruction_->IsCheckCast() ||
891 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
892 << "Unexpected instruction in read barrier for heap reference slow path: "
893 << instruction_->DebugName();
894
895 __ Bind(GetEntryLabel());
896 SaveLiveRegisters(codegen, locations);
897
898 // We may have to change the index's value, but as `index_` is a
899 // constant member (like other "inputs" of this slow path),
900 // introduce a copy of it, `index`.
901 Location index = index_;
902 if (index_.IsValid()) {
903 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
904 if (instruction_->IsArrayGet()) {
905 // Compute the actual memory offset and store it in `index`.
906 Register index_reg = index_.AsRegister<Register>();
907 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
908 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
909 // We are about to change the value of `index_reg` (see the
910 // calls to art::mips::MipsAssembler::Sll and
911 // art::mips::MipsAssembler::Addiu32 below), but it has
912 // not been saved by the previous call to
913 // art::SlowPathCode::SaveLiveRegisters, as it is a
914 // callee-save register --
915 // art::SlowPathCode::SaveLiveRegisters does not consider
916 // callee-save registers, as it has been designed with the
917 // assumption that callee-save registers are supposed to be
918 // handled by the called function. So, as a callee-save
919 // register, `index_reg` _would_ eventually be saved onto
920 // the stack, but it would be too late: we would have
921 // changed its value earlier. Therefore, we manually save
922 // it here into another freely available register,
923 // `free_reg`, chosen of course among the caller-save
924 // registers (as a callee-save `free_reg` register would
925 // exhibit the same problem).
926 //
927 // Note we could have requested a temporary register from
928 // the register allocator instead; but we prefer not to, as
929 // this is a slow path, and we know we can find a
930 // caller-save register that is available.
931 Register free_reg = FindAvailableCallerSaveRegister(codegen);
932 __ Move(free_reg, index_reg);
933 index_reg = free_reg;
934 index = Location::RegisterLocation(index_reg);
935 } else {
936 // The initial register stored in `index_` has already been
937 // saved in the call to art::SlowPathCode::SaveLiveRegisters
938 // (as it is not a callee-save register), so we can freely
939 // use it.
940 }
941 // Shifting the index value contained in `index_reg` by the scale
942 // factor (2) cannot overflow in practice, as the runtime is
943 // unable to allocate object arrays with a size larger than
944 // 2^26 - 1 (that is, 2^28 - 4 bytes).
945 __ Sll(index_reg, index_reg, TIMES_4);
946 static_assert(
947 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
948 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
949 __ Addiu32(index_reg, index_reg, offset_);
950 } else {
951 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
952 // intrinsics, `index_` is not shifted by a scale factor of 2
953 // (as in the case of ArrayGet), as it is actually an offset
954 // to an object field within an object.
955 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
956 DCHECK(instruction_->GetLocations()->Intrinsified());
957 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
958 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
959 << instruction_->AsInvoke()->GetIntrinsic();
960 DCHECK_EQ(offset_, 0U);
961 DCHECK(index_.IsRegisterPair());
962 // UnsafeGet's offset location is a register pair, the low
963 // part contains the correct offset.
964 index = index_.ToLow();
965 }
966 }
967
968 // We're moving two or three locations to locations that could
969 // overlap, so we need a parallel move resolver.
970 InvokeRuntimeCallingConvention calling_convention;
971 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
972 parallel_move.AddMove(ref_,
973 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100974 DataType::Type::kReference,
Alexey Frunze15958152017-02-09 19:08:30 -0800975 nullptr);
976 parallel_move.AddMove(obj_,
977 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100978 DataType::Type::kReference,
Alexey Frunze15958152017-02-09 19:08:30 -0800979 nullptr);
980 if (index.IsValid()) {
981 parallel_move.AddMove(index,
982 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100983 DataType::Type::kInt32,
Alexey Frunze15958152017-02-09 19:08:30 -0800984 nullptr);
985 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
986 } else {
987 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
988 __ LoadConst32(calling_convention.GetRegisterAt(2), offset_);
989 }
990 mips_codegen->InvokeRuntime(kQuickReadBarrierSlow,
991 instruction_,
992 instruction_->GetDexPc(),
993 this);
994 CheckEntrypointTypes<
995 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
Lena Djokic8098da92017-06-28 12:07:50 +0200996 mips_codegen->MoveLocation(out_,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100997 calling_convention.GetReturnLocation(DataType::Type::kReference),
998 DataType::Type::kReference);
Alexey Frunze15958152017-02-09 19:08:30 -0800999
1000 RestoreLiveRegisters(codegen, locations);
1001 __ B(GetExitLabel());
1002 }
1003
1004 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathMIPS"; }
1005
1006 private:
1007 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
1008 size_t ref = static_cast<int>(ref_.AsRegister<Register>());
1009 size_t obj = static_cast<int>(obj_.AsRegister<Register>());
1010 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
1011 if (i != ref &&
1012 i != obj &&
1013 !codegen->IsCoreCalleeSaveRegister(i) &&
1014 !codegen->IsBlockedCoreRegister(i)) {
1015 return static_cast<Register>(i);
1016 }
1017 }
1018 // We shall never fail to find a free caller-save register, as
1019 // there are more than two core caller-save registers on MIPS
1020 // (meaning it is possible to find one which is different from
1021 // `ref` and `obj`).
1022 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
1023 LOG(FATAL) << "Could not find a free caller-save register";
1024 UNREACHABLE();
1025 }
1026
1027 const Location out_;
1028 const Location ref_;
1029 const Location obj_;
1030 const uint32_t offset_;
1031 // An additional location containing an index to an array.
1032 // Only used for HArrayGet and the UnsafeGetObject &
1033 // UnsafeGetObjectVolatile intrinsics.
1034 const Location index_;
1035
1036 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathMIPS);
1037};
1038
1039// Slow path generating a read barrier for a GC root.
1040class ReadBarrierForRootSlowPathMIPS : public SlowPathCodeMIPS {
1041 public:
1042 ReadBarrierForRootSlowPathMIPS(HInstruction* instruction, Location out, Location root)
1043 : SlowPathCodeMIPS(instruction), out_(out), root_(root) {
1044 DCHECK(kEmitCompilerReadBarrier);
1045 }
1046
1047 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
1048 LocationSummary* locations = instruction_->GetLocations();
1049 Register reg_out = out_.AsRegister<Register>();
1050 DCHECK(locations->CanCall());
1051 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
1052 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
1053 << "Unexpected instruction in read barrier for GC root slow path: "
1054 << instruction_->DebugName();
1055
1056 __ Bind(GetEntryLabel());
1057 SaveLiveRegisters(codegen, locations);
1058
1059 InvokeRuntimeCallingConvention calling_convention;
1060 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
Lena Djokic8098da92017-06-28 12:07:50 +02001061 mips_codegen->MoveLocation(Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
1062 root_,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001063 DataType::Type::kReference);
Alexey Frunze15958152017-02-09 19:08:30 -08001064 mips_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
1065 instruction_,
1066 instruction_->GetDexPc(),
1067 this);
1068 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
Lena Djokic8098da92017-06-28 12:07:50 +02001069 mips_codegen->MoveLocation(out_,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001070 calling_convention.GetReturnLocation(DataType::Type::kReference),
1071 DataType::Type::kReference);
Alexey Frunze15958152017-02-09 19:08:30 -08001072
1073 RestoreLiveRegisters(codegen, locations);
1074 __ B(GetExitLabel());
1075 }
1076
1077 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathMIPS"; }
1078
1079 private:
1080 const Location out_;
1081 const Location root_;
1082
1083 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathMIPS);
1084};
1085
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001086CodeGeneratorMIPS::CodeGeneratorMIPS(HGraph* graph,
1087 const MipsInstructionSetFeatures& isa_features,
1088 const CompilerOptions& compiler_options,
1089 OptimizingCompilerStats* stats)
1090 : CodeGenerator(graph,
1091 kNumberOfCoreRegisters,
1092 kNumberOfFRegisters,
1093 kNumberOfRegisterPairs,
1094 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
1095 arraysize(kCoreCalleeSaves)),
1096 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
1097 arraysize(kFpuCalleeSaves)),
1098 compiler_options,
1099 stats),
1100 block_labels_(nullptr),
1101 location_builder_(graph, this),
1102 instruction_visitor_(graph, this),
1103 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +01001104 assembler_(graph->GetArena(), &isa_features),
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001105 isa_features_(isa_features),
Alexey Frunze06a46c42016-07-19 15:00:40 -07001106 uint32_literals_(std::less<uint32_t>(),
1107 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko65979462017-05-19 17:25:12 +01001108 pc_relative_method_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001109 method_bss_entry_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunze06a46c42016-07-19 15:00:40 -07001110 pc_relative_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko1998cd02017-01-13 13:02:58 +00001111 type_bss_entry_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko65979462017-05-19 17:25:12 +01001112 pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01001113 string_bss_entry_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunze627c1a02017-01-30 19:28:14 -08001114 jit_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1115 jit_class_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunze06a46c42016-07-19 15:00:40 -07001116 clobbered_ra_(false) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001117 // Save RA (containing the return address) to mimic Quick.
1118 AddAllocatedRegister(Location::RegisterLocation(RA));
1119}
1120
1121#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +01001122// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
1123#define __ down_cast<MipsAssembler*>(GetAssembler())-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -07001124#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMipsPointerSize, x).Int32Value()
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001125
1126void CodeGeneratorMIPS::Finalize(CodeAllocator* allocator) {
1127 // Ensure that we fix up branches.
1128 __ FinalizeCode();
1129
1130 // Adjust native pc offsets in stack maps.
1131 for (size_t i = 0, num = stack_map_stream_.GetNumberOfStackMaps(); i != num; ++i) {
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001132 uint32_t old_position =
1133 stack_map_stream_.GetStackMap(i).native_pc_code_offset.Uint32Value(kMips);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001134 uint32_t new_position = __ GetAdjustedPosition(old_position);
1135 DCHECK_GE(new_position, old_position);
1136 stack_map_stream_.SetStackMapNativePcOffset(i, new_position);
1137 }
1138
1139 // Adjust pc offsets for the disassembly information.
1140 if (disasm_info_ != nullptr) {
1141 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
1142 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
1143 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
1144 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
1145 it.second.start = __ GetAdjustedPosition(it.second.start);
1146 it.second.end = __ GetAdjustedPosition(it.second.end);
1147 }
1148 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
1149 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
1150 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
1151 }
1152 }
1153
1154 CodeGenerator::Finalize(allocator);
1155}
1156
1157MipsAssembler* ParallelMoveResolverMIPS::GetAssembler() const {
1158 return codegen_->GetAssembler();
1159}
1160
1161void ParallelMoveResolverMIPS::EmitMove(size_t index) {
1162 DCHECK_LT(index, moves_.size());
1163 MoveOperands* move = moves_[index];
1164 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), move->GetType());
1165}
1166
1167void ParallelMoveResolverMIPS::EmitSwap(size_t index) {
1168 DCHECK_LT(index, moves_.size());
1169 MoveOperands* move = moves_[index];
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001170 DataType::Type type = move->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001171 Location loc1 = move->GetDestination();
1172 Location loc2 = move->GetSource();
1173
1174 DCHECK(!loc1.IsConstant());
1175 DCHECK(!loc2.IsConstant());
1176
1177 if (loc1.Equals(loc2)) {
1178 return;
1179 }
1180
1181 if (loc1.IsRegister() && loc2.IsRegister()) {
1182 // Swap 2 GPRs.
1183 Register r1 = loc1.AsRegister<Register>();
1184 Register r2 = loc2.AsRegister<Register>();
1185 __ Move(TMP, r2);
1186 __ Move(r2, r1);
1187 __ Move(r1, TMP);
1188 } else if (loc1.IsFpuRegister() && loc2.IsFpuRegister()) {
1189 FRegister f1 = loc1.AsFpuRegister<FRegister>();
1190 FRegister f2 = loc2.AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001191 if (type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001192 __ MovS(FTMP, f2);
1193 __ MovS(f2, f1);
1194 __ MovS(f1, FTMP);
1195 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001196 DCHECK_EQ(type, DataType::Type::kFloat64);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001197 __ MovD(FTMP, f2);
1198 __ MovD(f2, f1);
1199 __ MovD(f1, FTMP);
1200 }
1201 } else if ((loc1.IsRegister() && loc2.IsFpuRegister()) ||
1202 (loc1.IsFpuRegister() && loc2.IsRegister())) {
1203 // Swap FPR and GPR.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001204 DCHECK_EQ(type, DataType::Type::kFloat32); // Can only swap a float.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001205 FRegister f1 = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
1206 : loc2.AsFpuRegister<FRegister>();
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +02001207 Register r2 = loc1.IsRegister() ? loc1.AsRegister<Register>() : loc2.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001208 __ Move(TMP, r2);
1209 __ Mfc1(r2, f1);
1210 __ Mtc1(TMP, f1);
1211 } else if (loc1.IsRegisterPair() && loc2.IsRegisterPair()) {
1212 // Swap 2 GPR register pairs.
1213 Register r1 = loc1.AsRegisterPairLow<Register>();
1214 Register r2 = loc2.AsRegisterPairLow<Register>();
1215 __ Move(TMP, r2);
1216 __ Move(r2, r1);
1217 __ Move(r1, TMP);
1218 r1 = loc1.AsRegisterPairHigh<Register>();
1219 r2 = loc2.AsRegisterPairHigh<Register>();
1220 __ Move(TMP, r2);
1221 __ Move(r2, r1);
1222 __ Move(r1, TMP);
1223 } else if ((loc1.IsRegisterPair() && loc2.IsFpuRegister()) ||
1224 (loc1.IsFpuRegister() && loc2.IsRegisterPair())) {
1225 // Swap FPR and GPR register pair.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001226 DCHECK_EQ(type, DataType::Type::kFloat64);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001227 FRegister f1 = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
1228 : loc2.AsFpuRegister<FRegister>();
1229 Register r2_l = loc1.IsRegisterPair() ? loc1.AsRegisterPairLow<Register>()
1230 : loc2.AsRegisterPairLow<Register>();
1231 Register r2_h = loc1.IsRegisterPair() ? loc1.AsRegisterPairHigh<Register>()
1232 : loc2.AsRegisterPairHigh<Register>();
1233 // Use 2 temporary registers because we can't first swap the low 32 bits of an FPR and
1234 // then swap the high 32 bits of the same FPR. mtc1 makes the high 32 bits of an FPR
1235 // unpredictable and the following mfch1 will fail.
1236 __ Mfc1(TMP, f1);
Alexey Frunzebb9863a2016-01-11 15:51:16 -08001237 __ MoveFromFpuHigh(AT, f1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001238 __ Mtc1(r2_l, f1);
Alexey Frunzebb9863a2016-01-11 15:51:16 -08001239 __ MoveToFpuHigh(r2_h, f1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001240 __ Move(r2_l, TMP);
1241 __ Move(r2_h, AT);
1242 } else if (loc1.IsStackSlot() && loc2.IsStackSlot()) {
1243 Exchange(loc1.GetStackIndex(), loc2.GetStackIndex(), /* double_slot */ false);
1244 } else if (loc1.IsDoubleStackSlot() && loc2.IsDoubleStackSlot()) {
1245 Exchange(loc1.GetStackIndex(), loc2.GetStackIndex(), /* double_slot */ true);
David Brazdilcc0f3112016-01-28 17:14:52 +00001246 } else if ((loc1.IsRegister() && loc2.IsStackSlot()) ||
1247 (loc1.IsStackSlot() && loc2.IsRegister())) {
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +02001248 Register reg = loc1.IsRegister() ? loc1.AsRegister<Register>() : loc2.AsRegister<Register>();
1249 intptr_t offset = loc1.IsStackSlot() ? loc1.GetStackIndex() : loc2.GetStackIndex();
David Brazdilcc0f3112016-01-28 17:14:52 +00001250 __ Move(TMP, reg);
1251 __ LoadFromOffset(kLoadWord, reg, SP, offset);
1252 __ StoreToOffset(kStoreWord, TMP, SP, offset);
1253 } else if ((loc1.IsRegisterPair() && loc2.IsDoubleStackSlot()) ||
1254 (loc1.IsDoubleStackSlot() && loc2.IsRegisterPair())) {
1255 Register reg_l = loc1.IsRegisterPair() ? loc1.AsRegisterPairLow<Register>()
1256 : loc2.AsRegisterPairLow<Register>();
1257 Register reg_h = loc1.IsRegisterPair() ? loc1.AsRegisterPairHigh<Register>()
1258 : loc2.AsRegisterPairHigh<Register>();
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +02001259 intptr_t offset_l = loc1.IsDoubleStackSlot() ? loc1.GetStackIndex() : loc2.GetStackIndex();
David Brazdilcc0f3112016-01-28 17:14:52 +00001260 intptr_t offset_h = loc1.IsDoubleStackSlot() ? loc1.GetHighStackIndex(kMipsWordSize)
1261 : loc2.GetHighStackIndex(kMipsWordSize);
1262 __ Move(TMP, reg_l);
David Brazdilcc0f3112016-01-28 17:14:52 +00001263 __ LoadFromOffset(kLoadWord, reg_l, SP, offset_l);
David Brazdilcc0f3112016-01-28 17:14:52 +00001264 __ StoreToOffset(kStoreWord, TMP, SP, offset_l);
David Brazdil04d3e872016-01-29 09:50:09 +00001265 __ Move(TMP, reg_h);
1266 __ LoadFromOffset(kLoadWord, reg_h, SP, offset_h);
1267 __ StoreToOffset(kStoreWord, TMP, SP, offset_h);
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +02001268 } else if (loc1.IsFpuRegister() || loc2.IsFpuRegister()) {
1269 FRegister reg = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
1270 : loc2.AsFpuRegister<FRegister>();
1271 intptr_t offset = loc1.IsFpuRegister() ? loc2.GetStackIndex() : loc1.GetStackIndex();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001272 if (type == DataType::Type::kFloat32) {
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +02001273 __ MovS(FTMP, reg);
1274 __ LoadSFromOffset(reg, SP, offset);
1275 __ StoreSToOffset(FTMP, SP, offset);
1276 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001277 DCHECK_EQ(type, DataType::Type::kFloat64);
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +02001278 __ MovD(FTMP, reg);
1279 __ LoadDFromOffset(reg, SP, offset);
1280 __ StoreDToOffset(FTMP, SP, offset);
1281 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001282 } else {
1283 LOG(FATAL) << "Swap between " << loc1 << " and " << loc2 << " is unsupported";
1284 }
1285}
1286
1287void ParallelMoveResolverMIPS::RestoreScratch(int reg) {
1288 __ Pop(static_cast<Register>(reg));
1289}
1290
1291void ParallelMoveResolverMIPS::SpillScratch(int reg) {
1292 __ Push(static_cast<Register>(reg));
1293}
1294
1295void ParallelMoveResolverMIPS::Exchange(int index1, int index2, bool double_slot) {
1296 // Allocate a scratch register other than TMP, if available.
1297 // Else, spill V0 (arbitrary choice) and use it as a scratch register (it will be
1298 // automatically unspilled when the scratch scope object is destroyed).
1299 ScratchRegisterScope ensure_scratch(this, TMP, V0, codegen_->GetNumberOfCoreRegisters());
1300 // If V0 spills onto the stack, SP-relative offsets need to be adjusted.
1301 int stack_offset = ensure_scratch.IsSpilled() ? kMipsWordSize : 0;
1302 for (int i = 0; i <= (double_slot ? 1 : 0); i++, stack_offset += kMipsWordSize) {
1303 __ LoadFromOffset(kLoadWord,
1304 Register(ensure_scratch.GetRegister()),
1305 SP,
1306 index1 + stack_offset);
1307 __ LoadFromOffset(kLoadWord,
1308 TMP,
1309 SP,
1310 index2 + stack_offset);
1311 __ StoreToOffset(kStoreWord,
1312 Register(ensure_scratch.GetRegister()),
1313 SP,
1314 index2 + stack_offset);
1315 __ StoreToOffset(kStoreWord, TMP, SP, index1 + stack_offset);
1316 }
1317}
1318
Alexey Frunze73296a72016-06-03 22:51:46 -07001319void CodeGeneratorMIPS::ComputeSpillMask() {
1320 core_spill_mask_ = allocated_registers_.GetCoreRegisters() & core_callee_save_mask_;
1321 fpu_spill_mask_ = allocated_registers_.GetFloatingPointRegisters() & fpu_callee_save_mask_;
1322 DCHECK_NE(core_spill_mask_, 0u) << "At least the return address register must be saved";
1323 // If there're FPU callee-saved registers and there's an odd number of GPR callee-saved
1324 // registers, include the ZERO register to force alignment of FPU callee-saved registers
1325 // within the stack frame.
1326 if ((fpu_spill_mask_ != 0) && (POPCOUNT(core_spill_mask_) % 2 != 0)) {
1327 core_spill_mask_ |= (1 << ZERO);
1328 }
Alexey Frunze58320ce2016-08-30 21:40:46 -07001329}
1330
1331bool CodeGeneratorMIPS::HasAllocatedCalleeSaveRegisters() const {
Alexey Frunze06a46c42016-07-19 15:00:40 -07001332 // If RA is clobbered by PC-relative operations on R2 and it's the only spilled register
Alexey Frunze58320ce2016-08-30 21:40:46 -07001333 // (this can happen in leaf methods), force CodeGenerator::InitializeCodeGeneration()
1334 // into the path that creates a stack frame so that RA can be explicitly saved and restored.
1335 // RA can't otherwise be saved/restored when it's the only spilled register.
Alexey Frunze58320ce2016-08-30 21:40:46 -07001336 return CodeGenerator::HasAllocatedCalleeSaveRegisters() || clobbered_ra_;
Alexey Frunze73296a72016-06-03 22:51:46 -07001337}
1338
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001339static dwarf::Reg DWARFReg(Register reg) {
1340 return dwarf::Reg::MipsCore(static_cast<int>(reg));
1341}
1342
1343// TODO: mapping of floating-point registers to DWARF.
1344
1345void CodeGeneratorMIPS::GenerateFrameEntry() {
1346 __ Bind(&frame_entry_label_);
1347
1348 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kMips) || !IsLeafMethod();
1349
1350 if (do_overflow_check) {
1351 __ LoadFromOffset(kLoadWord,
1352 ZERO,
1353 SP,
1354 -static_cast<int32_t>(GetStackOverflowReservedBytes(kMips)));
1355 RecordPcInfo(nullptr, 0);
1356 }
1357
1358 if (HasEmptyFrame()) {
Alexey Frunze58320ce2016-08-30 21:40:46 -07001359 CHECK_EQ(fpu_spill_mask_, 0u);
1360 CHECK_EQ(core_spill_mask_, 1u << RA);
1361 CHECK(!clobbered_ra_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001362 return;
1363 }
1364
1365 // Make sure the frame size isn't unreasonably large.
1366 if (GetFrameSize() > GetStackOverflowReservedBytes(kMips)) {
1367 LOG(FATAL) << "Stack frame larger than " << GetStackOverflowReservedBytes(kMips) << " bytes";
1368 }
1369
1370 // Spill callee-saved registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001371
Alexey Frunze73296a72016-06-03 22:51:46 -07001372 uint32_t ofs = GetFrameSize();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001373 __ IncreaseFrameSize(ofs);
1374
Alexey Frunze73296a72016-06-03 22:51:46 -07001375 for (uint32_t mask = core_spill_mask_; mask != 0; ) {
1376 Register reg = static_cast<Register>(MostSignificantBit(mask));
1377 mask ^= 1u << reg;
1378 ofs -= kMipsWordSize;
1379 // The ZERO register is only included for alignment.
1380 if (reg != ZERO) {
1381 __ StoreToOffset(kStoreWord, reg, SP, ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001382 __ cfi().RelOffset(DWARFReg(reg), ofs);
1383 }
1384 }
1385
Alexey Frunze73296a72016-06-03 22:51:46 -07001386 for (uint32_t mask = fpu_spill_mask_; mask != 0; ) {
1387 FRegister reg = static_cast<FRegister>(MostSignificantBit(mask));
1388 mask ^= 1u << reg;
1389 ofs -= kMipsDoublewordSize;
1390 __ StoreDToOffset(reg, SP, ofs);
1391 // TODO: __ cfi().RelOffset(DWARFReg(reg), ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001392 }
1393
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001394 // Save the current method if we need it. Note that we do not
1395 // do this in HCurrentMethod, as the instruction might have been removed
1396 // in the SSA graph.
1397 if (RequiresCurrentMethod()) {
1398 __ StoreToOffset(kStoreWord, kMethodRegisterArgument, SP, kCurrentMethodStackOffset);
1399 }
Goran Jakovljevicc6418422016-12-05 16:31:55 +01001400
1401 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1402 // Initialize should deoptimize flag to 0.
1403 __ StoreToOffset(kStoreWord, ZERO, SP, GetStackOffsetOfShouldDeoptimizeFlag());
1404 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001405}
1406
1407void CodeGeneratorMIPS::GenerateFrameExit() {
1408 __ cfi().RememberState();
1409
1410 if (!HasEmptyFrame()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001411 // Restore callee-saved registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001412
Alexey Frunze73296a72016-06-03 22:51:46 -07001413 // For better instruction scheduling restore RA before other registers.
1414 uint32_t ofs = GetFrameSize();
1415 for (uint32_t mask = core_spill_mask_; mask != 0; ) {
1416 Register reg = static_cast<Register>(MostSignificantBit(mask));
1417 mask ^= 1u << reg;
1418 ofs -= kMipsWordSize;
1419 // The ZERO register is only included for alignment.
1420 if (reg != ZERO) {
1421 __ LoadFromOffset(kLoadWord, reg, SP, ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001422 __ cfi().Restore(DWARFReg(reg));
1423 }
1424 }
1425
Alexey Frunze73296a72016-06-03 22:51:46 -07001426 for (uint32_t mask = fpu_spill_mask_; mask != 0; ) {
1427 FRegister reg = static_cast<FRegister>(MostSignificantBit(mask));
1428 mask ^= 1u << reg;
1429 ofs -= kMipsDoublewordSize;
1430 __ LoadDFromOffset(reg, SP, ofs);
1431 // TODO: __ cfi().Restore(DWARFReg(reg));
1432 }
1433
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001434 size_t frame_size = GetFrameSize();
1435 // Adjust the stack pointer in the delay slot if doing so doesn't break CFI.
1436 bool exchange = IsInt<16>(static_cast<int32_t>(frame_size));
1437 bool reordering = __ SetReorder(false);
1438 if (exchange) {
1439 __ Jr(RA);
1440 __ DecreaseFrameSize(frame_size); // Single instruction in delay slot.
1441 } else {
1442 __ DecreaseFrameSize(frame_size);
1443 __ Jr(RA);
1444 __ Nop(); // In delay slot.
1445 }
1446 __ SetReorder(reordering);
1447 } else {
1448 __ Jr(RA);
1449 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001450 }
1451
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001452 __ cfi().RestoreState();
1453 __ cfi().DefCFAOffset(GetFrameSize());
1454}
1455
1456void CodeGeneratorMIPS::Bind(HBasicBlock* block) {
1457 __ Bind(GetLabelOf(block));
1458}
1459
Lena Djokicca8c2952017-05-29 11:31:46 +02001460VectorRegister VectorRegisterFrom(Location location) {
1461 DCHECK(location.IsFpuRegister());
1462 return static_cast<VectorRegister>(location.AsFpuRegister<FRegister>());
1463}
1464
Lena Djokic8098da92017-06-28 12:07:50 +02001465void CodeGeneratorMIPS::MoveLocation(Location destination,
1466 Location source,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001467 DataType::Type dst_type) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001468 if (source.Equals(destination)) {
1469 return;
1470 }
1471
Lena Djokic8098da92017-06-28 12:07:50 +02001472 if (source.IsConstant()) {
1473 MoveConstant(destination, source.GetConstant());
1474 } else {
1475 if (destination.IsRegister()) {
1476 if (source.IsRegister()) {
1477 __ Move(destination.AsRegister<Register>(), source.AsRegister<Register>());
1478 } else if (source.IsFpuRegister()) {
1479 __ Mfc1(destination.AsRegister<Register>(), source.AsFpuRegister<FRegister>());
1480 } else {
1481 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001482 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(), SP, source.GetStackIndex());
Lena Djokic8098da92017-06-28 12:07:50 +02001483 }
1484 } else if (destination.IsRegisterPair()) {
1485 if (source.IsRegisterPair()) {
1486 __ Move(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
1487 __ Move(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
1488 } else if (source.IsFpuRegister()) {
1489 Register dst_high = destination.AsRegisterPairHigh<Register>();
1490 Register dst_low = destination.AsRegisterPairLow<Register>();
1491 FRegister src = source.AsFpuRegister<FRegister>();
1492 __ Mfc1(dst_low, src);
1493 __ MoveFromFpuHigh(dst_high, src);
1494 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001495 DCHECK(source.IsDoubleStackSlot())
1496 << "Cannot move from " << source << " to " << destination;
Lena Djokic8098da92017-06-28 12:07:50 +02001497 int32_t off = source.GetStackIndex();
1498 Register r = destination.AsRegisterPairLow<Register>();
1499 __ LoadFromOffset(kLoadDoubleword, r, SP, off);
1500 }
1501 } else if (destination.IsFpuRegister()) {
1502 if (source.IsRegister()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001503 DCHECK(!DataType::Is64BitType(dst_type));
Lena Djokic8098da92017-06-28 12:07:50 +02001504 __ Mtc1(source.AsRegister<Register>(), destination.AsFpuRegister<FRegister>());
1505 } else if (source.IsRegisterPair()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001506 DCHECK(DataType::Is64BitType(dst_type));
Lena Djokic8098da92017-06-28 12:07:50 +02001507 FRegister dst = destination.AsFpuRegister<FRegister>();
1508 Register src_high = source.AsRegisterPairHigh<Register>();
1509 Register src_low = source.AsRegisterPairLow<Register>();
1510 __ Mtc1(src_low, dst);
1511 __ MoveToFpuHigh(src_high, dst);
1512 } else if (source.IsFpuRegister()) {
Lena Djokicca8c2952017-05-29 11:31:46 +02001513 if (GetGraph()->HasSIMD()) {
1514 __ MoveV(VectorRegisterFrom(destination),
1515 VectorRegisterFrom(source));
Lena Djokic8098da92017-06-28 12:07:50 +02001516 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001517 if (DataType::Is64BitType(dst_type)) {
Lena Djokicca8c2952017-05-29 11:31:46 +02001518 __ MovD(destination.AsFpuRegister<FRegister>(), source.AsFpuRegister<FRegister>());
1519 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001520 DCHECK_EQ(dst_type, DataType::Type::kFloat32);
Lena Djokicca8c2952017-05-29 11:31:46 +02001521 __ MovS(destination.AsFpuRegister<FRegister>(), source.AsFpuRegister<FRegister>());
1522 }
Lena Djokic8098da92017-06-28 12:07:50 +02001523 }
Lena Djokicca8c2952017-05-29 11:31:46 +02001524 } else if (source.IsSIMDStackSlot()) {
1525 __ LoadQFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
Lena Djokic8098da92017-06-28 12:07:50 +02001526 } else if (source.IsDoubleStackSlot()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001527 DCHECK(DataType::Is64BitType(dst_type));
Lena Djokic8098da92017-06-28 12:07:50 +02001528 __ LoadDFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
1529 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001530 DCHECK(!DataType::Is64BitType(dst_type));
Lena Djokic8098da92017-06-28 12:07:50 +02001531 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
1532 __ LoadSFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
1533 }
Lena Djokicca8c2952017-05-29 11:31:46 +02001534 } else if (destination.IsSIMDStackSlot()) {
1535 if (source.IsFpuRegister()) {
1536 __ StoreQToOffset(source.AsFpuRegister<FRegister>(), SP, destination.GetStackIndex());
1537 } else {
1538 DCHECK(source.IsSIMDStackSlot());
1539 __ LoadQFromOffset(FTMP, SP, source.GetStackIndex());
1540 __ StoreQToOffset(FTMP, SP, destination.GetStackIndex());
1541 }
Lena Djokic8098da92017-06-28 12:07:50 +02001542 } else if (destination.IsDoubleStackSlot()) {
1543 int32_t dst_offset = destination.GetStackIndex();
1544 if (source.IsRegisterPair()) {
1545 __ StoreToOffset(kStoreDoubleword, source.AsRegisterPairLow<Register>(), SP, dst_offset);
1546 } else if (source.IsFpuRegister()) {
1547 __ StoreDToOffset(source.AsFpuRegister<FRegister>(), SP, dst_offset);
1548 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001549 DCHECK(source.IsDoubleStackSlot())
1550 << "Cannot move from " << source << " to " << destination;
Lena Djokic8098da92017-06-28 12:07:50 +02001551 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
1552 __ StoreToOffset(kStoreWord, TMP, SP, dst_offset);
1553 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex() + 4);
1554 __ StoreToOffset(kStoreWord, TMP, SP, dst_offset + 4);
1555 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001556 } else {
Lena Djokic8098da92017-06-28 12:07:50 +02001557 DCHECK(destination.IsStackSlot()) << destination;
1558 int32_t dst_offset = destination.GetStackIndex();
1559 if (source.IsRegister()) {
1560 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(), SP, dst_offset);
1561 } else if (source.IsFpuRegister()) {
1562 __ StoreSToOffset(source.AsFpuRegister<FRegister>(), SP, dst_offset);
1563 } else {
1564 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
1565 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
1566 __ StoreToOffset(kStoreWord, TMP, SP, dst_offset);
1567 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001568 }
1569 }
1570}
1571
1572void CodeGeneratorMIPS::MoveConstant(Location destination, HConstant* c) {
1573 if (c->IsIntConstant() || c->IsNullConstant()) {
1574 // Move 32 bit constant.
1575 int32_t value = GetInt32ValueOf(c);
1576 if (destination.IsRegister()) {
1577 Register dst = destination.AsRegister<Register>();
1578 __ LoadConst32(dst, value);
1579 } else {
1580 DCHECK(destination.IsStackSlot())
1581 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -07001582 __ StoreConstToOffset(kStoreWord, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001583 }
1584 } else if (c->IsLongConstant()) {
1585 // Move 64 bit constant.
1586 int64_t value = GetInt64ValueOf(c);
1587 if (destination.IsRegisterPair()) {
1588 Register r_h = destination.AsRegisterPairHigh<Register>();
1589 Register r_l = destination.AsRegisterPairLow<Register>();
1590 __ LoadConst64(r_h, r_l, value);
1591 } else {
1592 DCHECK(destination.IsDoubleStackSlot())
1593 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -07001594 __ StoreConstToOffset(kStoreDoubleword, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001595 }
1596 } else if (c->IsFloatConstant()) {
1597 // Move 32 bit float constant.
1598 int32_t value = GetInt32ValueOf(c);
1599 if (destination.IsFpuRegister()) {
1600 __ LoadSConst32(destination.AsFpuRegister<FRegister>(), value, TMP);
1601 } else {
1602 DCHECK(destination.IsStackSlot())
1603 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -07001604 __ StoreConstToOffset(kStoreWord, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001605 }
1606 } else {
1607 // Move 64 bit double constant.
1608 DCHECK(c->IsDoubleConstant()) << c->DebugName();
1609 int64_t value = GetInt64ValueOf(c);
1610 if (destination.IsFpuRegister()) {
1611 FRegister fd = destination.AsFpuRegister<FRegister>();
1612 __ LoadDConst64(fd, value, TMP);
1613 } else {
1614 DCHECK(destination.IsDoubleStackSlot())
1615 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -07001616 __ StoreConstToOffset(kStoreDoubleword, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001617 }
1618 }
1619}
1620
1621void CodeGeneratorMIPS::MoveConstant(Location destination, int32_t value) {
1622 DCHECK(destination.IsRegister());
1623 Register dst = destination.AsRegister<Register>();
1624 __ LoadConst32(dst, value);
1625}
1626
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001627void CodeGeneratorMIPS::AddLocationAsTemp(Location location, LocationSummary* locations) {
1628 if (location.IsRegister()) {
1629 locations->AddTemp(location);
Alexey Frunzec9e94f32015-10-26 16:11:39 -07001630 } else if (location.IsRegisterPair()) {
1631 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1632 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001633 } else {
1634 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1635 }
1636}
1637
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001638template <linker::LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
Vladimir Markoaad75c62016-10-03 08:46:48 +00001639inline void CodeGeneratorMIPS::EmitPcRelativeLinkerPatches(
1640 const ArenaDeque<PcRelativePatchInfo>& infos,
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001641 ArenaVector<linker::LinkerPatch>* linker_patches) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00001642 for (const PcRelativePatchInfo& info : infos) {
1643 const DexFile& dex_file = info.target_dex_file;
1644 size_t offset_or_index = info.offset_or_index;
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001645 DCHECK(info.label.IsBound());
1646 uint32_t literal_offset = __ GetLabelLocation(&info.label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001647 // On R2 we use HMipsComputeBaseMethodAddress and patch relative to
1648 // the assembler's base label used for PC-relative addressing.
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001649 const PcRelativePatchInfo& info_high = info.patch_info_high ? *info.patch_info_high : info;
1650 uint32_t pc_rel_offset = info_high.pc_rel_label.IsBound()
1651 ? __ GetLabelLocation(&info_high.pc_rel_label)
Vladimir Markoaad75c62016-10-03 08:46:48 +00001652 : __ GetPcRelBaseLabelLocation();
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001653 linker_patches->push_back(Factory(literal_offset, &dex_file, pc_rel_offset, offset_or_index));
Vladimir Markoaad75c62016-10-03 08:46:48 +00001654 }
1655}
1656
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001657void CodeGeneratorMIPS::EmitLinkerPatches(ArenaVector<linker::LinkerPatch>* linker_patches) {
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001658 DCHECK(linker_patches->empty());
1659 size_t size =
Vladimir Marko65979462017-05-19 17:25:12 +01001660 pc_relative_method_patches_.size() +
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001661 method_bss_entry_patches_.size() +
Alexey Frunze06a46c42016-07-19 15:00:40 -07001662 pc_relative_type_patches_.size() +
Vladimir Marko65979462017-05-19 17:25:12 +01001663 type_bss_entry_patches_.size() +
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01001664 pc_relative_string_patches_.size() +
1665 string_bss_entry_patches_.size();
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001666 linker_patches->reserve(size);
Vladimir Marko65979462017-05-19 17:25:12 +01001667 if (GetCompilerOptions().IsBootImage()) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001668 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeMethodPatch>(
1669 pc_relative_method_patches_, linker_patches);
1670 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeTypePatch>(
1671 pc_relative_type_patches_, linker_patches);
1672 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeStringPatch>(
1673 pc_relative_string_patches_, linker_patches);
Vladimir Marko65979462017-05-19 17:25:12 +01001674 } else {
1675 DCHECK(pc_relative_method_patches_.empty());
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001676 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeClassTablePatch>(
1677 pc_relative_type_patches_, linker_patches);
1678 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringInternTablePatch>(
1679 pc_relative_string_patches_, linker_patches);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001680 }
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001681 EmitPcRelativeLinkerPatches<linker::LinkerPatch::MethodBssEntryPatch>(
1682 method_bss_entry_patches_, linker_patches);
1683 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeBssEntryPatch>(
1684 type_bss_entry_patches_, linker_patches);
1685 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringBssEntryPatch>(
1686 string_bss_entry_patches_, linker_patches);
Vladimir Marko1998cd02017-01-13 13:02:58 +00001687 DCHECK_EQ(size, linker_patches->size());
Alexey Frunze06a46c42016-07-19 15:00:40 -07001688}
1689
Vladimir Marko65979462017-05-19 17:25:12 +01001690CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeMethodPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001691 MethodReference target_method,
1692 const PcRelativePatchInfo* info_high) {
Vladimir Marko65979462017-05-19 17:25:12 +01001693 return NewPcRelativePatch(*target_method.dex_file,
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07001694 target_method.index,
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001695 info_high,
Vladimir Marko65979462017-05-19 17:25:12 +01001696 &pc_relative_method_patches_);
Alexey Frunze06a46c42016-07-19 15:00:40 -07001697}
1698
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001699CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewMethodBssEntryPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001700 MethodReference target_method,
1701 const PcRelativePatchInfo* info_high) {
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001702 return NewPcRelativePatch(*target_method.dex_file,
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07001703 target_method.index,
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001704 info_high,
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001705 &method_bss_entry_patches_);
1706}
1707
Alexey Frunze06a46c42016-07-19 15:00:40 -07001708CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeTypePatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001709 const DexFile& dex_file,
1710 dex::TypeIndex type_index,
1711 const PcRelativePatchInfo* info_high) {
1712 return NewPcRelativePatch(dex_file, type_index.index_, info_high, &pc_relative_type_patches_);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001713}
1714
Vladimir Marko1998cd02017-01-13 13:02:58 +00001715CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewTypeBssEntryPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001716 const DexFile& dex_file,
1717 dex::TypeIndex type_index,
1718 const PcRelativePatchInfo* info_high) {
1719 return NewPcRelativePatch(dex_file, type_index.index_, info_high, &type_bss_entry_patches_);
Vladimir Marko1998cd02017-01-13 13:02:58 +00001720}
1721
Vladimir Marko65979462017-05-19 17:25:12 +01001722CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeStringPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001723 const DexFile& dex_file,
1724 dex::StringIndex string_index,
1725 const PcRelativePatchInfo* info_high) {
1726 return NewPcRelativePatch(dex_file, string_index.index_, info_high, &pc_relative_string_patches_);
Vladimir Marko65979462017-05-19 17:25:12 +01001727}
1728
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01001729CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewStringBssEntryPatch(
1730 const DexFile& dex_file,
1731 dex::StringIndex string_index,
1732 const PcRelativePatchInfo* info_high) {
1733 return NewPcRelativePatch(dex_file, string_index.index_, info_high, &string_bss_entry_patches_);
1734}
1735
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001736CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativePatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001737 const DexFile& dex_file,
1738 uint32_t offset_or_index,
1739 const PcRelativePatchInfo* info_high,
1740 ArenaDeque<PcRelativePatchInfo>* patches) {
1741 patches->emplace_back(dex_file, offset_or_index, info_high);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001742 return &patches->back();
1743}
1744
Alexey Frunze06a46c42016-07-19 15:00:40 -07001745Literal* CodeGeneratorMIPS::DeduplicateUint32Literal(uint32_t value, Uint32ToLiteralMap* map) {
1746 return map->GetOrCreate(
1747 value,
1748 [this, value]() { return __ NewLiteral<uint32_t>(value); });
1749}
1750
Alexey Frunze06a46c42016-07-19 15:00:40 -07001751Literal* CodeGeneratorMIPS::DeduplicateBootImageAddressLiteral(uint32_t address) {
Richard Uhlerc52f3032017-03-02 13:45:45 +00001752 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), &uint32_literals_);
Alexey Frunze06a46c42016-07-19 15:00:40 -07001753}
1754
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001755void CodeGeneratorMIPS::EmitPcRelativeAddressPlaceholderHigh(PcRelativePatchInfo* info_high,
Alexey Frunze6b892cd2017-01-03 17:11:38 -08001756 Register out,
Alexey Frunzea663d9d2017-07-31 18:43:18 -07001757 Register base) {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001758 DCHECK(!info_high->patch_info_high);
Alexey Frunze6079dca2017-05-28 19:10:28 -07001759 DCHECK_NE(out, base);
Alexey Frunzea663d9d2017-07-31 18:43:18 -07001760 bool reordering = __ SetReorder(false);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001761 if (GetInstructionSetFeatures().IsR6()) {
1762 DCHECK_EQ(base, ZERO);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001763 __ Bind(&info_high->label);
1764 __ Bind(&info_high->pc_rel_label);
Alexey Frunze6b892cd2017-01-03 17:11:38 -08001765 // Add the high half of a 32-bit offset to PC.
Vladimir Markoaad75c62016-10-03 08:46:48 +00001766 __ Auipc(out, /* placeholder */ 0x1234);
Alexey Frunzea663d9d2017-07-31 18:43:18 -07001767 __ SetReorder(reordering);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001768 } else {
1769 // If base is ZERO, emit NAL to obtain the actual base.
1770 if (base == ZERO) {
1771 // Generate a dummy PC-relative call to obtain PC.
1772 __ Nal();
1773 }
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001774 __ Bind(&info_high->label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001775 __ Lui(out, /* placeholder */ 0x1234);
1776 // If we emitted the NAL, bind the pc_rel_label, otherwise base is a register holding
1777 // the HMipsComputeBaseMethodAddress which has its own label stored in MipsAssembler.
1778 if (base == ZERO) {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001779 __ Bind(&info_high->pc_rel_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001780 }
Alexey Frunzea663d9d2017-07-31 18:43:18 -07001781 __ SetReorder(reordering);
Alexey Frunze6b892cd2017-01-03 17:11:38 -08001782 // Add the high half of a 32-bit offset to PC.
Vladimir Markoaad75c62016-10-03 08:46:48 +00001783 __ Addu(out, out, (base == ZERO) ? RA : base);
1784 }
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001785 // A following instruction will add the sign-extended low half of the 32-bit
Alexey Frunze6b892cd2017-01-03 17:11:38 -08001786 // offset to `out` (e.g. lw, jialc, addiu).
Vladimir Markoaad75c62016-10-03 08:46:48 +00001787}
1788
Alexey Frunze627c1a02017-01-30 19:28:14 -08001789CodeGeneratorMIPS::JitPatchInfo* CodeGeneratorMIPS::NewJitRootStringPatch(
1790 const DexFile& dex_file,
1791 dex::StringIndex dex_index,
1792 Handle<mirror::String> handle) {
1793 jit_string_roots_.Overwrite(StringReference(&dex_file, dex_index),
1794 reinterpret_cast64<uint64_t>(handle.GetReference()));
1795 jit_string_patches_.emplace_back(dex_file, dex_index.index_);
1796 return &jit_string_patches_.back();
1797}
1798
1799CodeGeneratorMIPS::JitPatchInfo* CodeGeneratorMIPS::NewJitRootClassPatch(
1800 const DexFile& dex_file,
1801 dex::TypeIndex dex_index,
1802 Handle<mirror::Class> handle) {
1803 jit_class_roots_.Overwrite(TypeReference(&dex_file, dex_index),
1804 reinterpret_cast64<uint64_t>(handle.GetReference()));
1805 jit_class_patches_.emplace_back(dex_file, dex_index.index_);
1806 return &jit_class_patches_.back();
1807}
1808
1809void CodeGeneratorMIPS::PatchJitRootUse(uint8_t* code,
1810 const uint8_t* roots_data,
1811 const CodeGeneratorMIPS::JitPatchInfo& info,
1812 uint64_t index_in_table) const {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001813 uint32_t high_literal_offset = GetAssembler().GetLabelLocation(&info.high_label);
1814 uint32_t low_literal_offset = GetAssembler().GetLabelLocation(&info.low_label);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001815 uintptr_t address =
1816 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
1817 uint32_t addr32 = dchecked_integral_cast<uint32_t>(address);
1818 // lui reg, addr32_high
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001819 DCHECK_EQ(code[high_literal_offset + 0], 0x34);
1820 DCHECK_EQ(code[high_literal_offset + 1], 0x12);
1821 DCHECK_EQ((code[high_literal_offset + 2] & 0xE0), 0x00);
1822 DCHECK_EQ(code[high_literal_offset + 3], 0x3C);
Alexey Frunzec61c0762017-04-10 13:54:23 -07001823 // instr reg, reg, addr32_low
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001824 DCHECK_EQ(code[low_literal_offset + 0], 0x78);
1825 DCHECK_EQ(code[low_literal_offset + 1], 0x56);
Alexey Frunzec61c0762017-04-10 13:54:23 -07001826 addr32 += (addr32 & 0x8000) << 1; // Account for sign extension in "instr reg, reg, addr32_low".
Alexey Frunze627c1a02017-01-30 19:28:14 -08001827 // lui reg, addr32_high
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001828 code[high_literal_offset + 0] = static_cast<uint8_t>(addr32 >> 16);
1829 code[high_literal_offset + 1] = static_cast<uint8_t>(addr32 >> 24);
Alexey Frunzec61c0762017-04-10 13:54:23 -07001830 // instr reg, reg, addr32_low
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001831 code[low_literal_offset + 0] = static_cast<uint8_t>(addr32 >> 0);
1832 code[low_literal_offset + 1] = static_cast<uint8_t>(addr32 >> 8);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001833}
1834
1835void CodeGeneratorMIPS::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
1836 for (const JitPatchInfo& info : jit_string_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001837 const auto it = jit_string_roots_.find(StringReference(&info.target_dex_file,
1838 dex::StringIndex(info.index)));
Alexey Frunze627c1a02017-01-30 19:28:14 -08001839 DCHECK(it != jit_string_roots_.end());
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001840 uint64_t index_in_table = it->second;
1841 PatchJitRootUse(code, roots_data, info, index_in_table);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001842 }
1843 for (const JitPatchInfo& info : jit_class_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001844 const auto it = jit_class_roots_.find(TypeReference(&info.target_dex_file,
1845 dex::TypeIndex(info.index)));
Alexey Frunze627c1a02017-01-30 19:28:14 -08001846 DCHECK(it != jit_class_roots_.end());
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001847 uint64_t index_in_table = it->second;
1848 PatchJitRootUse(code, roots_data, info, index_in_table);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001849 }
1850}
1851
Goran Jakovljevice114da22016-12-26 14:21:43 +01001852void CodeGeneratorMIPS::MarkGCCard(Register object,
1853 Register value,
1854 bool value_can_be_null) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001855 MipsLabel done;
1856 Register card = AT;
1857 Register temp = TMP;
Goran Jakovljevice114da22016-12-26 14:21:43 +01001858 if (value_can_be_null) {
1859 __ Beqz(value, &done);
1860 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001861 __ LoadFromOffset(kLoadWord,
1862 card,
1863 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001864 Thread::CardTableOffset<kMipsPointerSize>().Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001865 __ Srl(temp, object, gc::accounting::CardTable::kCardShift);
1866 __ Addu(temp, card, temp);
1867 __ Sb(card, temp, 0);
Goran Jakovljevice114da22016-12-26 14:21:43 +01001868 if (value_can_be_null) {
1869 __ Bind(&done);
1870 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001871}
1872
David Brazdil58282f42016-01-14 12:45:10 +00001873void CodeGeneratorMIPS::SetupBlockedRegisters() const {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001874 // ZERO, K0, K1, GP, SP, RA are always reserved and can't be allocated.
1875 blocked_core_registers_[ZERO] = true;
1876 blocked_core_registers_[K0] = true;
1877 blocked_core_registers_[K1] = true;
1878 blocked_core_registers_[GP] = true;
1879 blocked_core_registers_[SP] = true;
1880 blocked_core_registers_[RA] = true;
1881
1882 // AT and TMP(T8) are used as temporary/scratch registers
1883 // (similar to how AT is used by MIPS assemblers).
1884 blocked_core_registers_[AT] = true;
1885 blocked_core_registers_[TMP] = true;
1886 blocked_fpu_registers_[FTMP] = true;
1887
1888 // Reserve suspend and thread registers.
1889 blocked_core_registers_[S0] = true;
1890 blocked_core_registers_[TR] = true;
1891
1892 // Reserve T9 for function calls
1893 blocked_core_registers_[T9] = true;
1894
1895 // Reserve odd-numbered FPU registers.
1896 for (size_t i = 1; i < kNumberOfFRegisters; i += 2) {
1897 blocked_fpu_registers_[i] = true;
1898 }
1899
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02001900 if (GetGraph()->IsDebuggable()) {
1901 // Stubs do not save callee-save floating point registers. If the graph
1902 // is debuggable, we need to deal with these registers differently. For
1903 // now, just block them.
1904 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1905 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
1906 }
1907 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001908}
1909
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001910size_t CodeGeneratorMIPS::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1911 __ StoreToOffset(kStoreWord, Register(reg_id), SP, stack_index);
1912 return kMipsWordSize;
1913}
1914
1915size_t CodeGeneratorMIPS::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1916 __ LoadFromOffset(kLoadWord, Register(reg_id), SP, stack_index);
1917 return kMipsWordSize;
1918}
1919
1920size_t CodeGeneratorMIPS::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Lena Djokicca8c2952017-05-29 11:31:46 +02001921 if (GetGraph()->HasSIMD()) {
1922 __ StoreQToOffset(FRegister(reg_id), SP, stack_index);
1923 } else {
1924 __ StoreDToOffset(FRegister(reg_id), SP, stack_index);
1925 }
1926 return GetFloatingPointSpillSlotSize();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001927}
1928
1929size_t CodeGeneratorMIPS::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Lena Djokicca8c2952017-05-29 11:31:46 +02001930 if (GetGraph()->HasSIMD()) {
1931 __ LoadQFromOffset(FRegister(reg_id), SP, stack_index);
1932 } else {
1933 __ LoadDFromOffset(FRegister(reg_id), SP, stack_index);
1934 }
1935 return GetFloatingPointSpillSlotSize();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001936}
1937
1938void CodeGeneratorMIPS::DumpCoreRegister(std::ostream& stream, int reg) const {
Vladimir Marko623a7a22016-02-02 18:14:52 +00001939 stream << Register(reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001940}
1941
1942void CodeGeneratorMIPS::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
Vladimir Marko623a7a22016-02-02 18:14:52 +00001943 stream << FRegister(reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001944}
1945
Serban Constantinescufca16662016-07-14 09:21:59 +01001946constexpr size_t kMipsDirectEntrypointRuntimeOffset = 16;
1947
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001948void CodeGeneratorMIPS::InvokeRuntime(QuickEntrypointEnum entrypoint,
1949 HInstruction* instruction,
1950 uint32_t dex_pc,
1951 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001952 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Alexey Frunze15958152017-02-09 19:08:30 -08001953 GenerateInvokeRuntime(GetThreadOffset<kMipsPointerSize>(entrypoint).Int32Value(),
1954 IsDirectEntrypoint(entrypoint));
1955 if (EntrypointRequiresStackMap(entrypoint)) {
1956 RecordPcInfo(instruction, dex_pc, slow_path);
1957 }
1958}
1959
1960void CodeGeneratorMIPS::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1961 HInstruction* instruction,
1962 SlowPathCode* slow_path,
1963 bool direct) {
1964 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
1965 GenerateInvokeRuntime(entry_point_offset, direct);
1966}
1967
1968void CodeGeneratorMIPS::GenerateInvokeRuntime(int32_t entry_point_offset, bool direct) {
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001969 bool reordering = __ SetReorder(false);
Alexey Frunze15958152017-02-09 19:08:30 -08001970 __ LoadFromOffset(kLoadWord, T9, TR, entry_point_offset);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001971 __ Jalr(T9);
Alexey Frunze15958152017-02-09 19:08:30 -08001972 if (direct) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001973 // Reserve argument space on stack (for $a0-$a3) for
1974 // entrypoints that directly reference native implementations.
1975 // Called function may use this space to store $a0-$a3 regs.
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001976 __ IncreaseFrameSize(kMipsDirectEntrypointRuntimeOffset); // Single instruction in delay slot.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001977 __ DecreaseFrameSize(kMipsDirectEntrypointRuntimeOffset);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001978 } else {
1979 __ Nop(); // In delay slot.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001980 }
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001981 __ SetReorder(reordering);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001982}
1983
1984void InstructionCodeGeneratorMIPS::GenerateClassInitializationCheck(SlowPathCodeMIPS* slow_path,
1985 Register class_reg) {
1986 __ LoadFromOffset(kLoadWord, TMP, class_reg, mirror::Class::StatusOffset().Int32Value());
1987 __ LoadConst32(AT, mirror::Class::kStatusInitialized);
1988 __ Blt(TMP, AT, slow_path->GetEntryLabel());
1989 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
1990 __ Sync(0);
1991 __ Bind(slow_path->GetExitLabel());
1992}
1993
1994void InstructionCodeGeneratorMIPS::GenerateMemoryBarrier(MemBarrierKind kind ATTRIBUTE_UNUSED) {
1995 __ Sync(0); // Only stype 0 is supported.
1996}
1997
1998void InstructionCodeGeneratorMIPS::GenerateSuspendCheck(HSuspendCheck* instruction,
1999 HBasicBlock* successor) {
2000 SuspendCheckSlowPathMIPS* slow_path =
2001 new (GetGraph()->GetArena()) SuspendCheckSlowPathMIPS(instruction, successor);
2002 codegen_->AddSlowPath(slow_path);
2003
2004 __ LoadFromOffset(kLoadUnsignedHalfword,
2005 TMP,
2006 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07002007 Thread::ThreadFlagsOffset<kMipsPointerSize>().Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002008 if (successor == nullptr) {
2009 __ Bnez(TMP, slow_path->GetEntryLabel());
2010 __ Bind(slow_path->GetReturnLabel());
2011 } else {
2012 __ Beqz(TMP, codegen_->GetLabelOf(successor));
2013 __ B(slow_path->GetEntryLabel());
2014 // slow_path will return to GetLabelOf(successor).
2015 }
2016}
2017
2018InstructionCodeGeneratorMIPS::InstructionCodeGeneratorMIPS(HGraph* graph,
2019 CodeGeneratorMIPS* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08002020 : InstructionCodeGenerator(graph, codegen),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002021 assembler_(codegen->GetAssembler()),
2022 codegen_(codegen) {}
2023
2024void LocationsBuilderMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
2025 DCHECK_EQ(instruction->InputCount(), 2U);
2026 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002027 DataType::Type type = instruction->GetResultType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002028 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002029 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002030 locations->SetInAt(0, Location::RequiresRegister());
2031 HInstruction* right = instruction->InputAt(1);
2032 bool can_use_imm = false;
2033 if (right->IsConstant()) {
2034 int32_t imm = CodeGenerator::GetInt32ValueOf(right->AsConstant());
2035 if (instruction->IsAnd() || instruction->IsOr() || instruction->IsXor()) {
2036 can_use_imm = IsUint<16>(imm);
2037 } else if (instruction->IsAdd()) {
2038 can_use_imm = IsInt<16>(imm);
2039 } else {
2040 DCHECK(instruction->IsSub());
2041 can_use_imm = IsInt<16>(-imm);
2042 }
2043 }
2044 if (can_use_imm)
2045 locations->SetInAt(1, Location::ConstantLocation(right->AsConstant()));
2046 else
2047 locations->SetInAt(1, Location::RequiresRegister());
2048 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2049 break;
2050 }
2051
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002052 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002053 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002054 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
2055 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002056 break;
2057 }
2058
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002059 case DataType::Type::kFloat32:
2060 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002061 DCHECK(instruction->IsAdd() || instruction->IsSub());
2062 locations->SetInAt(0, Location::RequiresFpuRegister());
2063 locations->SetInAt(1, Location::RequiresFpuRegister());
2064 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2065 break;
2066
2067 default:
2068 LOG(FATAL) << "Unexpected " << instruction->DebugName() << " type " << type;
2069 }
2070}
2071
2072void InstructionCodeGeneratorMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002073 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002074 LocationSummary* locations = instruction->GetLocations();
2075
2076 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002077 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002078 Register dst = locations->Out().AsRegister<Register>();
2079 Register lhs = locations->InAt(0).AsRegister<Register>();
2080 Location rhs_location = locations->InAt(1);
2081
2082 Register rhs_reg = ZERO;
2083 int32_t rhs_imm = 0;
2084 bool use_imm = rhs_location.IsConstant();
2085 if (use_imm) {
2086 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2087 } else {
2088 rhs_reg = rhs_location.AsRegister<Register>();
2089 }
2090
2091 if (instruction->IsAnd()) {
2092 if (use_imm)
2093 __ Andi(dst, lhs, rhs_imm);
2094 else
2095 __ And(dst, lhs, rhs_reg);
2096 } else if (instruction->IsOr()) {
2097 if (use_imm)
2098 __ Ori(dst, lhs, rhs_imm);
2099 else
2100 __ Or(dst, lhs, rhs_reg);
2101 } else if (instruction->IsXor()) {
2102 if (use_imm)
2103 __ Xori(dst, lhs, rhs_imm);
2104 else
2105 __ Xor(dst, lhs, rhs_reg);
2106 } else if (instruction->IsAdd()) {
2107 if (use_imm)
2108 __ Addiu(dst, lhs, rhs_imm);
2109 else
2110 __ Addu(dst, lhs, rhs_reg);
2111 } else {
2112 DCHECK(instruction->IsSub());
2113 if (use_imm)
2114 __ Addiu(dst, lhs, -rhs_imm);
2115 else
2116 __ Subu(dst, lhs, rhs_reg);
2117 }
2118 break;
2119 }
2120
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002121 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002122 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
2123 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
2124 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
2125 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002126 Location rhs_location = locations->InAt(1);
2127 bool use_imm = rhs_location.IsConstant();
2128 if (!use_imm) {
2129 Register rhs_high = rhs_location.AsRegisterPairHigh<Register>();
2130 Register rhs_low = rhs_location.AsRegisterPairLow<Register>();
2131 if (instruction->IsAnd()) {
2132 __ And(dst_low, lhs_low, rhs_low);
2133 __ And(dst_high, lhs_high, rhs_high);
2134 } else if (instruction->IsOr()) {
2135 __ Or(dst_low, lhs_low, rhs_low);
2136 __ Or(dst_high, lhs_high, rhs_high);
2137 } else if (instruction->IsXor()) {
2138 __ Xor(dst_low, lhs_low, rhs_low);
2139 __ Xor(dst_high, lhs_high, rhs_high);
2140 } else if (instruction->IsAdd()) {
2141 if (lhs_low == rhs_low) {
2142 // Special case for lhs = rhs and the sum potentially overwriting both lhs and rhs.
2143 __ Slt(TMP, lhs_low, ZERO);
2144 __ Addu(dst_low, lhs_low, rhs_low);
2145 } else {
2146 __ Addu(dst_low, lhs_low, rhs_low);
2147 // If the sum overwrites rhs, lhs remains unchanged, otherwise rhs remains unchanged.
2148 __ Sltu(TMP, dst_low, (dst_low == rhs_low) ? lhs_low : rhs_low);
2149 }
2150 __ Addu(dst_high, lhs_high, rhs_high);
2151 __ Addu(dst_high, dst_high, TMP);
2152 } else {
2153 DCHECK(instruction->IsSub());
2154 __ Sltu(TMP, lhs_low, rhs_low);
2155 __ Subu(dst_low, lhs_low, rhs_low);
2156 __ Subu(dst_high, lhs_high, rhs_high);
2157 __ Subu(dst_high, dst_high, TMP);
2158 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002159 } else {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002160 int64_t value = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()->AsConstant());
2161 if (instruction->IsOr()) {
2162 uint32_t low = Low32Bits(value);
2163 uint32_t high = High32Bits(value);
2164 if (IsUint<16>(low)) {
2165 if (dst_low != lhs_low || low != 0) {
2166 __ Ori(dst_low, lhs_low, low);
2167 }
2168 } else {
2169 __ LoadConst32(TMP, low);
2170 __ Or(dst_low, lhs_low, TMP);
2171 }
2172 if (IsUint<16>(high)) {
2173 if (dst_high != lhs_high || high != 0) {
2174 __ Ori(dst_high, lhs_high, high);
2175 }
2176 } else {
2177 if (high != low) {
2178 __ LoadConst32(TMP, high);
2179 }
2180 __ Or(dst_high, lhs_high, TMP);
2181 }
2182 } else if (instruction->IsXor()) {
2183 uint32_t low = Low32Bits(value);
2184 uint32_t high = High32Bits(value);
2185 if (IsUint<16>(low)) {
2186 if (dst_low != lhs_low || low != 0) {
2187 __ Xori(dst_low, lhs_low, low);
2188 }
2189 } else {
2190 __ LoadConst32(TMP, low);
2191 __ Xor(dst_low, lhs_low, TMP);
2192 }
2193 if (IsUint<16>(high)) {
2194 if (dst_high != lhs_high || high != 0) {
2195 __ Xori(dst_high, lhs_high, high);
2196 }
2197 } else {
2198 if (high != low) {
2199 __ LoadConst32(TMP, high);
2200 }
2201 __ Xor(dst_high, lhs_high, TMP);
2202 }
2203 } else if (instruction->IsAnd()) {
2204 uint32_t low = Low32Bits(value);
2205 uint32_t high = High32Bits(value);
2206 if (IsUint<16>(low)) {
2207 __ Andi(dst_low, lhs_low, low);
2208 } else if (low != 0xFFFFFFFF) {
2209 __ LoadConst32(TMP, low);
2210 __ And(dst_low, lhs_low, TMP);
2211 } else if (dst_low != lhs_low) {
2212 __ Move(dst_low, lhs_low);
2213 }
2214 if (IsUint<16>(high)) {
2215 __ Andi(dst_high, lhs_high, high);
2216 } else if (high != 0xFFFFFFFF) {
2217 if (high != low) {
2218 __ LoadConst32(TMP, high);
2219 }
2220 __ And(dst_high, lhs_high, TMP);
2221 } else if (dst_high != lhs_high) {
2222 __ Move(dst_high, lhs_high);
2223 }
2224 } else {
2225 if (instruction->IsSub()) {
2226 value = -value;
2227 } else {
2228 DCHECK(instruction->IsAdd());
2229 }
2230 int32_t low = Low32Bits(value);
2231 int32_t high = High32Bits(value);
2232 if (IsInt<16>(low)) {
2233 if (dst_low != lhs_low || low != 0) {
2234 __ Addiu(dst_low, lhs_low, low);
2235 }
2236 if (low != 0) {
2237 __ Sltiu(AT, dst_low, low);
2238 }
2239 } else {
2240 __ LoadConst32(TMP, low);
2241 __ Addu(dst_low, lhs_low, TMP);
2242 __ Sltu(AT, dst_low, TMP);
2243 }
2244 if (IsInt<16>(high)) {
2245 if (dst_high != lhs_high || high != 0) {
2246 __ Addiu(dst_high, lhs_high, high);
2247 }
2248 } else {
2249 if (high != low) {
2250 __ LoadConst32(TMP, high);
2251 }
2252 __ Addu(dst_high, lhs_high, TMP);
2253 }
2254 if (low != 0) {
2255 __ Addu(dst_high, dst_high, AT);
2256 }
2257 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002258 }
2259 break;
2260 }
2261
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002262 case DataType::Type::kFloat32:
2263 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002264 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
2265 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2266 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2267 if (instruction->IsAdd()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002268 if (type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002269 __ AddS(dst, lhs, rhs);
2270 } else {
2271 __ AddD(dst, lhs, rhs);
2272 }
2273 } else {
2274 DCHECK(instruction->IsSub());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002275 if (type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002276 __ SubS(dst, lhs, rhs);
2277 } else {
2278 __ SubD(dst, lhs, rhs);
2279 }
2280 }
2281 break;
2282 }
2283
2284 default:
2285 LOG(FATAL) << "Unexpected binary operation type " << type;
2286 }
2287}
2288
2289void LocationsBuilderMIPS::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002290 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002291
2292 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002293 DataType::Type type = instr->GetResultType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002294 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002295 case DataType::Type::kInt32:
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002296 locations->SetInAt(0, Location::RequiresRegister());
2297 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
2298 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2299 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002300 case DataType::Type::kInt64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002301 locations->SetInAt(0, Location::RequiresRegister());
2302 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
2303 locations->SetOut(Location::RequiresRegister());
2304 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002305 default:
2306 LOG(FATAL) << "Unexpected shift type " << type;
2307 }
2308}
2309
2310static constexpr size_t kMipsBitsPerWord = kMipsWordSize * kBitsPerByte;
2311
2312void InstructionCodeGeneratorMIPS::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002313 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002314 LocationSummary* locations = instr->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002315 DataType::Type type = instr->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002316
2317 Location rhs_location = locations->InAt(1);
2318 bool use_imm = rhs_location.IsConstant();
2319 Register rhs_reg = use_imm ? ZERO : rhs_location.AsRegister<Register>();
2320 int64_t rhs_imm = use_imm ? CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()) : 0;
Roland Levillain5b5b9312016-03-22 14:57:31 +00002321 const uint32_t shift_mask =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002322 (type == DataType::Type::kInt32) ? kMaxIntShiftDistance : kMaxLongShiftDistance;
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002323 const uint32_t shift_value = rhs_imm & shift_mask;
Alexey Frunze92d90602015-12-18 18:16:36 -08002324 // Are the INS (Insert Bit Field) and ROTR instructions supported?
2325 bool has_ins_rotr = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002326
2327 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002328 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002329 Register dst = locations->Out().AsRegister<Register>();
2330 Register lhs = locations->InAt(0).AsRegister<Register>();
2331 if (use_imm) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002332 if (shift_value == 0) {
2333 if (dst != lhs) {
2334 __ Move(dst, lhs);
2335 }
2336 } else if (instr->IsShl()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002337 __ Sll(dst, lhs, shift_value);
2338 } else if (instr->IsShr()) {
2339 __ Sra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002340 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002341 __ Srl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002342 } else {
2343 if (has_ins_rotr) {
2344 __ Rotr(dst, lhs, shift_value);
2345 } else {
2346 __ Sll(TMP, lhs, (kMipsBitsPerWord - shift_value) & shift_mask);
2347 __ Srl(dst, lhs, shift_value);
2348 __ Or(dst, dst, TMP);
2349 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002350 }
2351 } else {
2352 if (instr->IsShl()) {
2353 __ Sllv(dst, lhs, rhs_reg);
2354 } else if (instr->IsShr()) {
2355 __ Srav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08002356 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002357 __ Srlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08002358 } else {
2359 if (has_ins_rotr) {
2360 __ Rotrv(dst, lhs, rhs_reg);
2361 } else {
2362 __ Subu(TMP, ZERO, rhs_reg);
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002363 // 32-bit shift instructions use the 5 least significant bits of the shift count, so
2364 // shifting by `-rhs_reg` is equivalent to shifting by `(32 - rhs_reg) & 31`. The case
2365 // when `rhs_reg & 31 == 0` is OK even though we don't shift `lhs` left all the way out
2366 // by 32, because the result in this case is computed as `(lhs >> 0) | (lhs << 0)`,
2367 // IOW, the OR'd values are equal.
Alexey Frunze92d90602015-12-18 18:16:36 -08002368 __ Sllv(TMP, lhs, TMP);
2369 __ Srlv(dst, lhs, rhs_reg);
2370 __ Or(dst, dst, TMP);
2371 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002372 }
2373 }
2374 break;
2375 }
2376
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002377 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002378 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
2379 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
2380 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
2381 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
2382 if (use_imm) {
2383 if (shift_value == 0) {
Lena Djokic8098da92017-06-28 12:07:50 +02002384 codegen_->MoveLocation(locations->Out(), locations->InAt(0), type);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002385 } else if (shift_value < kMipsBitsPerWord) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002386 if (has_ins_rotr) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002387 if (instr->IsShl()) {
2388 __ Srl(dst_high, lhs_low, kMipsBitsPerWord - shift_value);
2389 __ Ins(dst_high, lhs_high, shift_value, kMipsBitsPerWord - shift_value);
2390 __ Sll(dst_low, lhs_low, shift_value);
2391 } else if (instr->IsShr()) {
2392 __ Srl(dst_low, lhs_low, shift_value);
2393 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
2394 __ Sra(dst_high, lhs_high, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002395 } else if (instr->IsUShr()) {
2396 __ Srl(dst_low, lhs_low, shift_value);
2397 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
2398 __ Srl(dst_high, lhs_high, shift_value);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002399 } else {
2400 __ Srl(dst_low, lhs_low, shift_value);
2401 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
2402 __ Srl(dst_high, lhs_high, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002403 __ Ins(dst_high, lhs_low, kMipsBitsPerWord - shift_value, shift_value);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002404 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002405 } else {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002406 if (instr->IsShl()) {
2407 __ Sll(dst_low, lhs_low, shift_value);
2408 __ Srl(TMP, lhs_low, kMipsBitsPerWord - shift_value);
2409 __ Sll(dst_high, lhs_high, shift_value);
2410 __ Or(dst_high, dst_high, TMP);
2411 } else if (instr->IsShr()) {
2412 __ Sra(dst_high, lhs_high, shift_value);
2413 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
2414 __ Srl(dst_low, lhs_low, shift_value);
2415 __ Or(dst_low, dst_low, TMP);
Alexey Frunze92d90602015-12-18 18:16:36 -08002416 } else if (instr->IsUShr()) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002417 __ Srl(dst_high, lhs_high, shift_value);
2418 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
2419 __ Srl(dst_low, lhs_low, shift_value);
2420 __ Or(dst_low, dst_low, TMP);
Alexey Frunze92d90602015-12-18 18:16:36 -08002421 } else {
2422 __ Srl(TMP, lhs_low, shift_value);
2423 __ Sll(dst_low, lhs_high, kMipsBitsPerWord - shift_value);
2424 __ Or(dst_low, dst_low, TMP);
2425 __ Srl(TMP, lhs_high, shift_value);
2426 __ Sll(dst_high, lhs_low, kMipsBitsPerWord - shift_value);
2427 __ Or(dst_high, dst_high, TMP);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002428 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002429 }
2430 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002431 const uint32_t shift_value_high = shift_value - kMipsBitsPerWord;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002432 if (instr->IsShl()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002433 __ Sll(dst_high, lhs_low, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002434 __ Move(dst_low, ZERO);
2435 } else if (instr->IsShr()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002436 __ Sra(dst_low, lhs_high, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002437 __ Sra(dst_high, dst_low, kMipsBitsPerWord - 1);
Alexey Frunze92d90602015-12-18 18:16:36 -08002438 } else if (instr->IsUShr()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002439 __ Srl(dst_low, lhs_high, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002440 __ Move(dst_high, ZERO);
Alexey Frunze92d90602015-12-18 18:16:36 -08002441 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002442 if (shift_value == kMipsBitsPerWord) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002443 // 64-bit rotation by 32 is just a swap.
2444 __ Move(dst_low, lhs_high);
2445 __ Move(dst_high, lhs_low);
2446 } else {
2447 if (has_ins_rotr) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002448 __ Srl(dst_low, lhs_high, shift_value_high);
2449 __ Ins(dst_low, lhs_low, kMipsBitsPerWord - shift_value_high, shift_value_high);
2450 __ Srl(dst_high, lhs_low, shift_value_high);
2451 __ Ins(dst_high, lhs_high, kMipsBitsPerWord - shift_value_high, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08002452 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002453 __ Sll(TMP, lhs_low, kMipsBitsPerWord - shift_value_high);
2454 __ Srl(dst_low, lhs_high, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08002455 __ Or(dst_low, dst_low, TMP);
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002456 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value_high);
2457 __ Srl(dst_high, lhs_low, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08002458 __ Or(dst_high, dst_high, TMP);
2459 }
2460 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002461 }
2462 }
2463 } else {
2464 MipsLabel done;
2465 if (instr->IsShl()) {
2466 __ Sllv(dst_low, lhs_low, rhs_reg);
2467 __ Nor(AT, ZERO, rhs_reg);
2468 __ Srl(TMP, lhs_low, 1);
2469 __ Srlv(TMP, TMP, AT);
2470 __ Sllv(dst_high, lhs_high, rhs_reg);
2471 __ Or(dst_high, dst_high, TMP);
2472 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
2473 __ Beqz(TMP, &done);
2474 __ Move(dst_high, dst_low);
2475 __ Move(dst_low, ZERO);
2476 } else if (instr->IsShr()) {
2477 __ Srav(dst_high, lhs_high, rhs_reg);
2478 __ Nor(AT, ZERO, rhs_reg);
2479 __ Sll(TMP, lhs_high, 1);
2480 __ Sllv(TMP, TMP, AT);
2481 __ Srlv(dst_low, lhs_low, rhs_reg);
2482 __ Or(dst_low, dst_low, TMP);
2483 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
2484 __ Beqz(TMP, &done);
2485 __ Move(dst_low, dst_high);
2486 __ Sra(dst_high, dst_high, 31);
Alexey Frunze92d90602015-12-18 18:16:36 -08002487 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002488 __ Srlv(dst_high, lhs_high, rhs_reg);
2489 __ Nor(AT, ZERO, rhs_reg);
2490 __ Sll(TMP, lhs_high, 1);
2491 __ Sllv(TMP, TMP, AT);
2492 __ Srlv(dst_low, lhs_low, rhs_reg);
2493 __ Or(dst_low, dst_low, TMP);
2494 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
2495 __ Beqz(TMP, &done);
2496 __ Move(dst_low, dst_high);
2497 __ Move(dst_high, ZERO);
Alexey Frunze92d90602015-12-18 18:16:36 -08002498 } else {
2499 __ Nor(AT, ZERO, rhs_reg);
2500 __ Srlv(TMP, lhs_low, rhs_reg);
2501 __ Sll(dst_low, lhs_high, 1);
2502 __ Sllv(dst_low, dst_low, AT);
2503 __ Or(dst_low, dst_low, TMP);
2504 __ Srlv(TMP, lhs_high, rhs_reg);
2505 __ Sll(dst_high, lhs_low, 1);
2506 __ Sllv(dst_high, dst_high, AT);
2507 __ Or(dst_high, dst_high, TMP);
2508 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
2509 __ Beqz(TMP, &done);
2510 __ Move(TMP, dst_high);
2511 __ Move(dst_high, dst_low);
2512 __ Move(dst_low, TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002513 }
2514 __ Bind(&done);
2515 }
2516 break;
2517 }
2518
2519 default:
2520 LOG(FATAL) << "Unexpected shift operation type " << type;
2521 }
2522}
2523
2524void LocationsBuilderMIPS::VisitAdd(HAdd* instruction) {
2525 HandleBinaryOp(instruction);
2526}
2527
2528void InstructionCodeGeneratorMIPS::VisitAdd(HAdd* instruction) {
2529 HandleBinaryOp(instruction);
2530}
2531
2532void LocationsBuilderMIPS::VisitAnd(HAnd* instruction) {
2533 HandleBinaryOp(instruction);
2534}
2535
2536void InstructionCodeGeneratorMIPS::VisitAnd(HAnd* instruction) {
2537 HandleBinaryOp(instruction);
2538}
2539
2540void LocationsBuilderMIPS::VisitArrayGet(HArrayGet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002541 DataType::Type type = instruction->GetType();
Alexey Frunze15958152017-02-09 19:08:30 -08002542 bool object_array_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002543 kEmitCompilerReadBarrier && (type == DataType::Type::kReference);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002544 LocationSummary* locations =
Alexey Frunze15958152017-02-09 19:08:30 -08002545 new (GetGraph()->GetArena()) LocationSummary(instruction,
2546 object_array_get_with_read_barrier
2547 ? LocationSummary::kCallOnSlowPath
2548 : LocationSummary::kNoCall);
Alexey Frunzec61c0762017-04-10 13:54:23 -07002549 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
2550 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
2551 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002552 locations->SetInAt(0, Location::RequiresRegister());
2553 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002554 if (DataType::IsFloatingPointType(type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002555 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2556 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08002557 // The output overlaps in the case of an object array get with
2558 // read barriers enabled: we do not want the move to overwrite the
2559 // array's location, as we need it to emit the read barrier.
2560 locations->SetOut(Location::RequiresRegister(),
2561 object_array_get_with_read_barrier
2562 ? Location::kOutputOverlap
2563 : Location::kNoOutputOverlap);
2564 }
2565 // We need a temporary register for the read barrier marking slow
2566 // path in CodeGeneratorMIPS::GenerateArrayLoadWithBakerReadBarrier.
2567 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002568 bool temp_needed = instruction->GetIndex()->IsConstant()
2569 ? !kBakerReadBarrierThunksEnableForFields
2570 : !kBakerReadBarrierThunksEnableForArrays;
2571 if (temp_needed) {
2572 locations->AddTemp(Location::RequiresRegister());
2573 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002574 }
2575}
2576
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002577static auto GetImplicitNullChecker(HInstruction* instruction, CodeGeneratorMIPS* codegen) {
2578 auto null_checker = [codegen, instruction]() {
2579 codegen->MaybeRecordImplicitNullCheck(instruction);
Alexey Frunze2923db72016-08-20 01:55:47 -07002580 };
2581 return null_checker;
2582}
2583
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002584void InstructionCodeGeneratorMIPS::VisitArrayGet(HArrayGet* instruction) {
2585 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08002586 Location obj_loc = locations->InAt(0);
2587 Register obj = obj_loc.AsRegister<Register>();
2588 Location out_loc = locations->Out();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002589 Location index = locations->InAt(1);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002590 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002591 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002592
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002593 DataType::Type type = instruction->GetType();
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002594 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
2595 instruction->IsStringCharAt();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002596 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002597 case DataType::Type::kBool:
2598 case DataType::Type::kUint8: {
Alexey Frunze15958152017-02-09 19:08:30 -08002599 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002600 if (index.IsConstant()) {
2601 size_t offset =
2602 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002603 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002604 } else {
2605 __ Addu(TMP, obj, index.AsRegister<Register>());
Alexey Frunze2923db72016-08-20 01:55:47 -07002606 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002607 }
2608 break;
2609 }
2610
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002611 case DataType::Type::kInt8: {
Alexey Frunze15958152017-02-09 19:08:30 -08002612 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002613 if (index.IsConstant()) {
2614 size_t offset =
2615 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002616 __ LoadFromOffset(kLoadSignedByte, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002617 } else {
2618 __ Addu(TMP, obj, index.AsRegister<Register>());
Alexey Frunze2923db72016-08-20 01:55:47 -07002619 __ LoadFromOffset(kLoadSignedByte, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002620 }
2621 break;
2622 }
2623
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002624 case DataType::Type::kUint16: {
Alexey Frunze15958152017-02-09 19:08:30 -08002625 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002626 if (maybe_compressed_char_at) {
2627 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
2628 __ LoadFromOffset(kLoadWord, TMP, obj, count_offset, null_checker);
2629 __ Sll(TMP, TMP, 31); // Extract compression flag into the most significant bit of TMP.
2630 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2631 "Expecting 0=compressed, 1=uncompressed");
2632 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002633 if (index.IsConstant()) {
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002634 int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
2635 if (maybe_compressed_char_at) {
2636 MipsLabel uncompressed_load, done;
2637 __ Bnez(TMP, &uncompressed_load);
2638 __ LoadFromOffset(kLoadUnsignedByte,
2639 out,
2640 obj,
2641 data_offset + (const_index << TIMES_1));
2642 __ B(&done);
2643 __ Bind(&uncompressed_load);
2644 __ LoadFromOffset(kLoadUnsignedHalfword,
2645 out,
2646 obj,
2647 data_offset + (const_index << TIMES_2));
2648 __ Bind(&done);
2649 } else {
2650 __ LoadFromOffset(kLoadUnsignedHalfword,
2651 out,
2652 obj,
2653 data_offset + (const_index << TIMES_2),
2654 null_checker);
2655 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002656 } else {
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002657 Register index_reg = index.AsRegister<Register>();
2658 if (maybe_compressed_char_at) {
2659 MipsLabel uncompressed_load, done;
2660 __ Bnez(TMP, &uncompressed_load);
2661 __ Addu(TMP, obj, index_reg);
2662 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset);
2663 __ B(&done);
2664 __ Bind(&uncompressed_load);
Chris Larsencd0295d2017-03-31 15:26:54 -07002665 __ ShiftAndAdd(TMP, index_reg, obj, TIMES_2, TMP);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002666 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset);
2667 __ Bind(&done);
2668 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002669 __ ShiftAndAdd(TMP, index_reg, obj, TIMES_2, TMP);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002670 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset, null_checker);
2671 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002672 }
2673 break;
2674 }
2675
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002676 case DataType::Type::kInt16: {
2677 Register out = out_loc.AsRegister<Register>();
2678 if (index.IsConstant()) {
2679 size_t offset =
2680 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
2681 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset, null_checker);
2682 } else {
2683 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_2, TMP);
2684 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset, null_checker);
2685 }
2686 break;
2687 }
2688
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002689 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002690 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
Alexey Frunze15958152017-02-09 19:08:30 -08002691 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002692 if (index.IsConstant()) {
2693 size_t offset =
2694 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002695 __ LoadFromOffset(kLoadWord, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002696 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002697 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_4, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002698 __ LoadFromOffset(kLoadWord, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002699 }
2700 break;
2701 }
2702
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002703 case DataType::Type::kReference: {
Alexey Frunze15958152017-02-09 19:08:30 -08002704 static_assert(
2705 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2706 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2707 // /* HeapReference<Object> */ out =
2708 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
2709 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002710 bool temp_needed = index.IsConstant()
2711 ? !kBakerReadBarrierThunksEnableForFields
2712 : !kBakerReadBarrierThunksEnableForArrays;
2713 Location temp = temp_needed ? locations->GetTemp(0) : Location::NoLocation();
Alexey Frunze15958152017-02-09 19:08:30 -08002714 // Note that a potential implicit null check is handled in this
2715 // CodeGeneratorMIPS::GenerateArrayLoadWithBakerReadBarrier call.
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002716 DCHECK(!instruction->CanDoImplicitNullCheckOn(instruction->InputAt(0)));
2717 if (index.IsConstant()) {
2718 // Array load with a constant index can be treated as a field load.
2719 size_t offset =
2720 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2721 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
2722 out_loc,
2723 obj,
2724 offset,
2725 temp,
2726 /* needs_null_check */ false);
2727 } else {
2728 codegen_->GenerateArrayLoadWithBakerReadBarrier(instruction,
2729 out_loc,
2730 obj,
2731 data_offset,
2732 index,
2733 temp,
2734 /* needs_null_check */ false);
2735 }
Alexey Frunze15958152017-02-09 19:08:30 -08002736 } else {
2737 Register out = out_loc.AsRegister<Register>();
2738 if (index.IsConstant()) {
2739 size_t offset =
2740 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2741 __ LoadFromOffset(kLoadWord, out, obj, offset, null_checker);
2742 // If read barriers are enabled, emit read barriers other than
2743 // Baker's using a slow path (and also unpoison the loaded
2744 // reference, if heap poisoning is enabled).
2745 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
2746 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002747 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_4, TMP);
Alexey Frunze15958152017-02-09 19:08:30 -08002748 __ LoadFromOffset(kLoadWord, out, TMP, data_offset, null_checker);
2749 // If read barriers are enabled, emit read barriers other than
2750 // Baker's using a slow path (and also unpoison the loaded
2751 // reference, if heap poisoning is enabled).
2752 codegen_->MaybeGenerateReadBarrierSlow(instruction,
2753 out_loc,
2754 out_loc,
2755 obj_loc,
2756 data_offset,
2757 index);
2758 }
2759 }
2760 break;
2761 }
2762
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002763 case DataType::Type::kInt64: {
Alexey Frunze15958152017-02-09 19:08:30 -08002764 Register out = out_loc.AsRegisterPairLow<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002765 if (index.IsConstant()) {
2766 size_t offset =
2767 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002768 __ LoadFromOffset(kLoadDoubleword, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002769 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002770 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_8, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002771 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002772 }
2773 break;
2774 }
2775
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002776 case DataType::Type::kFloat32: {
Alexey Frunze15958152017-02-09 19:08:30 -08002777 FRegister out = out_loc.AsFpuRegister<FRegister>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002778 if (index.IsConstant()) {
2779 size_t offset =
2780 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002781 __ LoadSFromOffset(out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002782 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002783 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_4, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002784 __ LoadSFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002785 }
2786 break;
2787 }
2788
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002789 case DataType::Type::kFloat64: {
Alexey Frunze15958152017-02-09 19:08:30 -08002790 FRegister out = out_loc.AsFpuRegister<FRegister>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002791 if (index.IsConstant()) {
2792 size_t offset =
2793 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002794 __ LoadDFromOffset(out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002795 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002796 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_8, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002797 __ LoadDFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002798 }
2799 break;
2800 }
2801
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002802 case DataType::Type::kVoid:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002803 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2804 UNREACHABLE();
2805 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002806}
2807
2808void LocationsBuilderMIPS::VisitArrayLength(HArrayLength* instruction) {
2809 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2810 locations->SetInAt(0, Location::RequiresRegister());
2811 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2812}
2813
2814void InstructionCodeGeneratorMIPS::VisitArrayLength(HArrayLength* instruction) {
2815 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01002816 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002817 Register obj = locations->InAt(0).AsRegister<Register>();
2818 Register out = locations->Out().AsRegister<Register>();
2819 __ LoadFromOffset(kLoadWord, out, obj, offset);
2820 codegen_->MaybeRecordImplicitNullCheck(instruction);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002821 // Mask out compression flag from String's array length.
2822 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
2823 __ Srl(out, out, 1u);
2824 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002825}
2826
Alexey Frunzef58b2482016-09-02 22:14:06 -07002827Location LocationsBuilderMIPS::RegisterOrZeroConstant(HInstruction* instruction) {
2828 return (instruction->IsConstant() && instruction->AsConstant()->IsZeroBitPattern())
2829 ? Location::ConstantLocation(instruction->AsConstant())
2830 : Location::RequiresRegister();
2831}
2832
2833Location LocationsBuilderMIPS::FpuRegisterOrConstantForStore(HInstruction* instruction) {
2834 // We can store 0.0 directly (from the ZERO register) without loading it into an FPU register.
2835 // We can store a non-zero float or double constant without first loading it into the FPU,
2836 // but we should only prefer this if the constant has a single use.
2837 if (instruction->IsConstant() &&
2838 (instruction->AsConstant()->IsZeroBitPattern() ||
2839 instruction->GetUses().HasExactlyOneElement())) {
2840 return Location::ConstantLocation(instruction->AsConstant());
2841 // Otherwise fall through and require an FPU register for the constant.
2842 }
2843 return Location::RequiresFpuRegister();
2844}
2845
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002846void LocationsBuilderMIPS::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002847 DataType::Type value_type = instruction->GetComponentType();
Alexey Frunze15958152017-02-09 19:08:30 -08002848
2849 bool needs_write_barrier =
2850 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
2851 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
2852
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002853 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2854 instruction,
Alexey Frunze15958152017-02-09 19:08:30 -08002855 may_need_runtime_call_for_type_check ?
2856 LocationSummary::kCallOnSlowPath :
2857 LocationSummary::kNoCall);
2858
2859 locations->SetInAt(0, Location::RequiresRegister());
2860 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002861 if (DataType::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
Alexey Frunze15958152017-02-09 19:08:30 -08002862 locations->SetInAt(2, FpuRegisterOrConstantForStore(instruction->InputAt(2)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002863 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08002864 locations->SetInAt(2, RegisterOrZeroConstant(instruction->InputAt(2)));
2865 }
2866 if (needs_write_barrier) {
2867 // Temporary register for the write barrier.
2868 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002869 }
2870}
2871
2872void InstructionCodeGeneratorMIPS::VisitArraySet(HArraySet* instruction) {
2873 LocationSummary* locations = instruction->GetLocations();
2874 Register obj = locations->InAt(0).AsRegister<Register>();
2875 Location index = locations->InAt(1);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002876 Location value_location = locations->InAt(2);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002877 DataType::Type value_type = instruction->GetComponentType();
Alexey Frunze15958152017-02-09 19:08:30 -08002878 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002879 bool needs_write_barrier =
2880 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002881 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002882 Register base_reg = index.IsConstant() ? obj : TMP;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002883
2884 switch (value_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002885 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002886 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002887 case DataType::Type::kInt8: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002888 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002889 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002890 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002891 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002892 __ Addu(base_reg, obj, index.AsRegister<Register>());
2893 }
2894 if (value_location.IsConstant()) {
2895 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2896 __ StoreConstToOffset(kStoreByte, value, base_reg, data_offset, TMP, null_checker);
2897 } else {
2898 Register value = value_location.AsRegister<Register>();
2899 __ StoreToOffset(kStoreByte, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002900 }
2901 break;
2902 }
2903
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002904 case DataType::Type::kUint16:
2905 case DataType::Type::kInt16: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002906 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_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_2;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002909 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002910 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_2, base_reg);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002911 }
2912 if (value_location.IsConstant()) {
2913 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2914 __ StoreConstToOffset(kStoreHalfword, value, base_reg, data_offset, TMP, null_checker);
2915 } else {
2916 Register value = value_location.AsRegister<Register>();
2917 __ StoreToOffset(kStoreHalfword, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002918 }
2919 break;
2920 }
2921
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002922 case DataType::Type::kInt32: {
Alexey Frunze15958152017-02-09 19:08:30 -08002923 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2924 if (index.IsConstant()) {
2925 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
2926 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002927 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Alexey Frunze15958152017-02-09 19:08:30 -08002928 }
2929 if (value_location.IsConstant()) {
2930 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2931 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2932 } else {
2933 Register value = value_location.AsRegister<Register>();
2934 __ StoreToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
2935 }
2936 break;
2937 }
2938
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002939 case DataType::Type::kReference: {
Alexey Frunze15958152017-02-09 19:08:30 -08002940 if (value_location.IsConstant()) {
2941 // Just setting null.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002942 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002943 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002944 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002945 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002946 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002947 }
Alexey Frunze15958152017-02-09 19:08:30 -08002948 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2949 DCHECK_EQ(value, 0);
2950 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2951 DCHECK(!needs_write_barrier);
2952 DCHECK(!may_need_runtime_call_for_type_check);
2953 break;
2954 }
2955
2956 DCHECK(needs_write_barrier);
2957 Register value = value_location.AsRegister<Register>();
2958 Register temp1 = locations->GetTemp(0).AsRegister<Register>();
2959 Register temp2 = TMP; // Doesn't need to survive slow path.
2960 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2961 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2962 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2963 MipsLabel done;
2964 SlowPathCodeMIPS* slow_path = nullptr;
2965
2966 if (may_need_runtime_call_for_type_check) {
2967 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathMIPS(instruction);
2968 codegen_->AddSlowPath(slow_path);
2969 if (instruction->GetValueCanBeNull()) {
2970 MipsLabel non_zero;
2971 __ Bnez(value, &non_zero);
2972 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2973 if (index.IsConstant()) {
2974 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Alexey Frunzec061de12017-02-14 13:27:23 -08002975 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002976 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Alexey Frunzec061de12017-02-14 13:27:23 -08002977 }
Alexey Frunze15958152017-02-09 19:08:30 -08002978 __ StoreToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
2979 __ B(&done);
2980 __ Bind(&non_zero);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002981 }
Alexey Frunze15958152017-02-09 19:08:30 -08002982
2983 // Note that when read barriers are enabled, the type checks
2984 // are performed without read barriers. This is fine, even in
2985 // the case where a class object is in the from-space after
2986 // the flip, as a comparison involving such a type would not
2987 // produce a false positive; it may of course produce a false
2988 // negative, in which case we would take the ArraySet slow
2989 // path.
2990
2991 // /* HeapReference<Class> */ temp1 = obj->klass_
2992 __ LoadFromOffset(kLoadWord, temp1, obj, class_offset, null_checker);
2993 __ MaybeUnpoisonHeapReference(temp1);
2994
2995 // /* HeapReference<Class> */ temp1 = temp1->component_type_
2996 __ LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
2997 // /* HeapReference<Class> */ temp2 = value->klass_
2998 __ LoadFromOffset(kLoadWord, temp2, value, class_offset);
2999 // If heap poisoning is enabled, no need to unpoison `temp1`
3000 // nor `temp2`, as we are comparing two poisoned references.
3001
3002 if (instruction->StaticTypeOfArrayIsObjectArray()) {
3003 MipsLabel do_put;
3004 __ Beq(temp1, temp2, &do_put);
3005 // If heap poisoning is enabled, the `temp1` reference has
3006 // not been unpoisoned yet; unpoison it now.
3007 __ MaybeUnpoisonHeapReference(temp1);
3008
3009 // /* HeapReference<Class> */ temp1 = temp1->super_class_
3010 __ LoadFromOffset(kLoadWord, temp1, temp1, super_offset);
3011 // If heap poisoning is enabled, no need to unpoison
3012 // `temp1`, as we are comparing against null below.
3013 __ Bnez(temp1, slow_path->GetEntryLabel());
3014 __ Bind(&do_put);
3015 } else {
3016 __ Bne(temp1, temp2, slow_path->GetEntryLabel());
3017 }
3018 }
3019
3020 Register source = value;
3021 if (kPoisonHeapReferences) {
3022 // Note that in the case where `value` is a null reference,
3023 // we do not enter this block, as a null reference does not
3024 // need poisoning.
3025 __ Move(temp1, value);
3026 __ PoisonHeapReference(temp1);
3027 source = temp1;
3028 }
3029
3030 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
3031 if (index.IsConstant()) {
3032 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003033 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003034 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Alexey Frunze15958152017-02-09 19:08:30 -08003035 }
3036 __ StoreToOffset(kStoreWord, source, base_reg, data_offset);
3037
3038 if (!may_need_runtime_call_for_type_check) {
3039 codegen_->MaybeRecordImplicitNullCheck(instruction);
3040 }
3041
3042 codegen_->MarkGCCard(obj, value, instruction->GetValueCanBeNull());
3043
3044 if (done.IsLinked()) {
3045 __ Bind(&done);
3046 }
3047
3048 if (slow_path != nullptr) {
3049 __ Bind(slow_path->GetExitLabel());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003050 }
3051 break;
3052 }
3053
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003054 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003055 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003056 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07003057 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003058 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003059 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_8, base_reg);
Alexey Frunzef58b2482016-09-02 22:14:06 -07003060 }
3061 if (value_location.IsConstant()) {
3062 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
3063 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
3064 } else {
3065 Register value = value_location.AsRegisterPairLow<Register>();
3066 __ StoreToOffset(kStoreDoubleword, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003067 }
3068 break;
3069 }
3070
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003071 case DataType::Type::kFloat32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003072 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003073 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07003074 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003075 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003076 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Alexey Frunzef58b2482016-09-02 22:14:06 -07003077 }
3078 if (value_location.IsConstant()) {
3079 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
3080 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
3081 } else {
3082 FRegister value = value_location.AsFpuRegister<FRegister>();
3083 __ StoreSToOffset(value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003084 }
3085 break;
3086 }
3087
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003088 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003089 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003090 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07003091 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003092 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003093 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_8, base_reg);
Alexey Frunzef58b2482016-09-02 22:14:06 -07003094 }
3095 if (value_location.IsConstant()) {
3096 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
3097 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
3098 } else {
3099 FRegister value = value_location.AsFpuRegister<FRegister>();
3100 __ StoreDToOffset(value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003101 }
3102 break;
3103 }
3104
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003105 case DataType::Type::kVoid:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003106 LOG(FATAL) << "Unreachable type " << instruction->GetType();
3107 UNREACHABLE();
3108 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003109}
3110
3111void LocationsBuilderMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003112 RegisterSet caller_saves = RegisterSet::Empty();
3113 InvokeRuntimeCallingConvention calling_convention;
3114 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3115 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3116 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003117 locations->SetInAt(0, Location::RequiresRegister());
3118 locations->SetInAt(1, Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003119}
3120
3121void InstructionCodeGeneratorMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
3122 LocationSummary* locations = instruction->GetLocations();
3123 BoundsCheckSlowPathMIPS* slow_path =
3124 new (GetGraph()->GetArena()) BoundsCheckSlowPathMIPS(instruction);
3125 codegen_->AddSlowPath(slow_path);
3126
3127 Register index = locations->InAt(0).AsRegister<Register>();
3128 Register length = locations->InAt(1).AsRegister<Register>();
3129
3130 // length is limited by the maximum positive signed 32-bit integer.
3131 // Unsigned comparison of length and index checks for index < 0
3132 // and for length <= index simultaneously.
3133 __ Bgeu(index, length, slow_path->GetEntryLabel());
3134}
3135
Alexey Frunze15958152017-02-09 19:08:30 -08003136// Temp is used for read barrier.
3137static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
3138 if (kEmitCompilerReadBarrier &&
Alexey Frunze4147fcc2017-06-17 19:57:27 -07003139 !(kUseBakerReadBarrier && kBakerReadBarrierThunksEnableForFields) &&
Alexey Frunze15958152017-02-09 19:08:30 -08003140 (kUseBakerReadBarrier ||
3141 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3142 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3143 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
3144 return 1;
3145 }
3146 return 0;
3147}
3148
3149// Extra temp is used for read barrier.
3150static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
3151 return 1 + NumberOfInstanceOfTemps(type_check_kind);
3152}
3153
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003154void LocationsBuilderMIPS::VisitCheckCast(HCheckCast* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003155 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
3156 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
3157
3158 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
3159 switch (type_check_kind) {
3160 case TypeCheckKind::kExactCheck:
3161 case TypeCheckKind::kAbstractClassCheck:
3162 case TypeCheckKind::kClassHierarchyCheck:
3163 case TypeCheckKind::kArrayObjectCheck:
Alexey Frunze15958152017-02-09 19:08:30 -08003164 call_kind = (throws_into_catch || kEmitCompilerReadBarrier)
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003165 ? LocationSummary::kCallOnSlowPath
3166 : LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
3167 break;
3168 case TypeCheckKind::kArrayCheck:
3169 case TypeCheckKind::kUnresolvedCheck:
3170 case TypeCheckKind::kInterfaceCheck:
3171 call_kind = LocationSummary::kCallOnSlowPath;
3172 break;
3173 }
3174
3175 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003176 locations->SetInAt(0, Location::RequiresRegister());
3177 locations->SetInAt(1, Location::RequiresRegister());
Alexey Frunze15958152017-02-09 19:08:30 -08003178 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003179}
3180
3181void InstructionCodeGeneratorMIPS::VisitCheckCast(HCheckCast* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003182 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003183 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08003184 Location obj_loc = locations->InAt(0);
3185 Register obj = obj_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003186 Register cls = locations->InAt(1).AsRegister<Register>();
Alexey Frunze15958152017-02-09 19:08:30 -08003187 Location temp_loc = locations->GetTemp(0);
3188 Register temp = temp_loc.AsRegister<Register>();
3189 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
3190 DCHECK_LE(num_temps, 2u);
3191 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003192 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3193 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3194 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3195 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
3196 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
3197 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
3198 const uint32_t object_array_data_offset =
3199 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
3200 MipsLabel done;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003201
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003202 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
3203 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
3204 // read barriers is done for performance and code size reasons.
3205 bool is_type_check_slow_path_fatal = false;
3206 if (!kEmitCompilerReadBarrier) {
3207 is_type_check_slow_path_fatal =
3208 (type_check_kind == TypeCheckKind::kExactCheck ||
3209 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3210 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3211 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
3212 !instruction->CanThrowIntoCatchBlock();
3213 }
3214 SlowPathCodeMIPS* slow_path =
3215 new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS(instruction,
3216 is_type_check_slow_path_fatal);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003217 codegen_->AddSlowPath(slow_path);
3218
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003219 // Avoid this check if we know `obj` is not null.
3220 if (instruction->MustDoNullCheck()) {
3221 __ Beqz(obj, &done);
3222 }
3223
3224 switch (type_check_kind) {
3225 case TypeCheckKind::kExactCheck:
3226 case TypeCheckKind::kArrayCheck: {
3227 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003228 GenerateReferenceLoadTwoRegisters(instruction,
3229 temp_loc,
3230 obj_loc,
3231 class_offset,
3232 maybe_temp2_loc,
3233 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003234 // Jump to slow path for throwing the exception or doing a
3235 // more involved array check.
3236 __ Bne(temp, cls, slow_path->GetEntryLabel());
3237 break;
3238 }
3239
3240 case TypeCheckKind::kAbstractClassCheck: {
3241 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003242 GenerateReferenceLoadTwoRegisters(instruction,
3243 temp_loc,
3244 obj_loc,
3245 class_offset,
3246 maybe_temp2_loc,
3247 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003248 // If the class is abstract, we eagerly fetch the super class of the
3249 // object to avoid doing a comparison we know will fail.
3250 MipsLabel loop;
3251 __ Bind(&loop);
3252 // /* HeapReference<Class> */ temp = temp->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08003253 GenerateReferenceLoadOneRegister(instruction,
3254 temp_loc,
3255 super_offset,
3256 maybe_temp2_loc,
3257 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003258 // If the class reference currently in `temp` is null, jump to the slow path to throw the
3259 // exception.
3260 __ Beqz(temp, slow_path->GetEntryLabel());
3261 // Otherwise, compare the classes.
3262 __ Bne(temp, cls, &loop);
3263 break;
3264 }
3265
3266 case TypeCheckKind::kClassHierarchyCheck: {
3267 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003268 GenerateReferenceLoadTwoRegisters(instruction,
3269 temp_loc,
3270 obj_loc,
3271 class_offset,
3272 maybe_temp2_loc,
3273 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003274 // Walk over the class hierarchy to find a match.
3275 MipsLabel loop;
3276 __ Bind(&loop);
3277 __ Beq(temp, cls, &done);
3278 // /* HeapReference<Class> */ temp = temp->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08003279 GenerateReferenceLoadOneRegister(instruction,
3280 temp_loc,
3281 super_offset,
3282 maybe_temp2_loc,
3283 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003284 // If the class reference currently in `temp` is null, jump to the slow path to throw the
3285 // exception. Otherwise, jump to the beginning of the loop.
3286 __ Bnez(temp, &loop);
3287 __ B(slow_path->GetEntryLabel());
3288 break;
3289 }
3290
3291 case TypeCheckKind::kArrayObjectCheck: {
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 // Do an exact check.
3300 __ Beq(temp, cls, &done);
3301 // Otherwise, we need to check that the object's class is a non-primitive array.
3302 // /* HeapReference<Class> */ temp = temp->component_type_
Alexey Frunze15958152017-02-09 19:08:30 -08003303 GenerateReferenceLoadOneRegister(instruction,
3304 temp_loc,
3305 component_offset,
3306 maybe_temp2_loc,
3307 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003308 // If the component type is null, jump to the slow path to throw the exception.
3309 __ Beqz(temp, slow_path->GetEntryLabel());
3310 // Otherwise, the object is indeed an array, further check that this component
3311 // type is not a primitive type.
3312 __ LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
3313 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
3314 __ Bnez(temp, slow_path->GetEntryLabel());
3315 break;
3316 }
3317
3318 case TypeCheckKind::kUnresolvedCheck:
3319 // We always go into the type check slow path for the unresolved check case.
3320 // We cannot directly call the CheckCast runtime entry point
3321 // without resorting to a type checking slow path here (i.e. by
3322 // calling InvokeRuntime directly), as it would require to
3323 // assign fixed registers for the inputs of this HInstanceOf
3324 // instruction (following the runtime calling convention), which
3325 // might be cluttered by the potential first read barrier
3326 // emission at the beginning of this method.
3327 __ B(slow_path->GetEntryLabel());
3328 break;
3329
3330 case TypeCheckKind::kInterfaceCheck: {
3331 // Avoid read barriers to improve performance of the fast path. We can not get false
3332 // positives by doing this.
3333 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003334 GenerateReferenceLoadTwoRegisters(instruction,
3335 temp_loc,
3336 obj_loc,
3337 class_offset,
3338 maybe_temp2_loc,
3339 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003340 // /* HeapReference<Class> */ temp = temp->iftable_
Alexey Frunze15958152017-02-09 19:08:30 -08003341 GenerateReferenceLoadTwoRegisters(instruction,
3342 temp_loc,
3343 temp_loc,
3344 iftable_offset,
3345 maybe_temp2_loc,
3346 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003347 // Iftable is never null.
3348 __ Lw(TMP, temp, array_length_offset);
3349 // Loop through the iftable and check if any class matches.
3350 MipsLabel loop;
3351 __ Bind(&loop);
3352 __ Addiu(temp, temp, 2 * kHeapReferenceSize); // Possibly in delay slot on R2.
3353 __ Beqz(TMP, slow_path->GetEntryLabel());
3354 __ Lw(AT, temp, object_array_data_offset - 2 * kHeapReferenceSize);
3355 __ MaybeUnpoisonHeapReference(AT);
3356 // Go to next interface.
3357 __ Addiu(TMP, TMP, -2);
3358 // Compare the classes and continue the loop if they do not match.
3359 __ Bne(AT, cls, &loop);
3360 break;
3361 }
3362 }
3363
3364 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003365 __ Bind(slow_path->GetExitLabel());
3366}
3367
3368void LocationsBuilderMIPS::VisitClinitCheck(HClinitCheck* check) {
3369 LocationSummary* locations =
3370 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
3371 locations->SetInAt(0, Location::RequiresRegister());
3372 if (check->HasUses()) {
3373 locations->SetOut(Location::SameAsFirstInput());
3374 }
3375}
3376
3377void InstructionCodeGeneratorMIPS::VisitClinitCheck(HClinitCheck* check) {
3378 // We assume the class is not null.
3379 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS(
3380 check->GetLoadClass(),
3381 check,
3382 check->GetDexPc(),
3383 true);
3384 codegen_->AddSlowPath(slow_path);
3385 GenerateClassInitializationCheck(slow_path,
3386 check->GetLocations()->InAt(0).AsRegister<Register>());
3387}
3388
3389void LocationsBuilderMIPS::VisitCompare(HCompare* compare) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003390 DataType::Type in_type = compare->InputAt(0)->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003391
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003392 LocationSummary* locations =
3393 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003394
3395 switch (in_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003396 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003397 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003398 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003399 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003400 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003401 case DataType::Type::kInt32:
Alexey Frunzee7697712016-09-15 21:37:49 -07003402 locations->SetInAt(0, Location::RequiresRegister());
3403 locations->SetInAt(1, Location::RequiresRegister());
3404 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3405 break;
3406
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003407 case DataType::Type::kInt64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003408 locations->SetInAt(0, Location::RequiresRegister());
3409 locations->SetInAt(1, Location::RequiresRegister());
3410 // Output overlaps because it is written before doing the low comparison.
3411 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3412 break;
3413
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003414 case DataType::Type::kFloat32:
3415 case DataType::Type::kFloat64:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003416 locations->SetInAt(0, Location::RequiresFpuRegister());
3417 locations->SetInAt(1, Location::RequiresFpuRegister());
3418 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003419 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003420
3421 default:
3422 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
3423 }
3424}
3425
3426void InstructionCodeGeneratorMIPS::VisitCompare(HCompare* instruction) {
3427 LocationSummary* locations = instruction->GetLocations();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003428 Register res = locations->Out().AsRegister<Register>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003429 DataType::Type in_type = instruction->InputAt(0)->GetType();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003430 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003431
3432 // 0 if: left == right
3433 // 1 if: left > right
3434 // -1 if: left < right
3435 switch (in_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003436 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003437 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003438 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003439 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003440 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003441 case DataType::Type::kInt32: {
Aart Bika19616e2016-02-01 18:57:58 -08003442 Register lhs = locations->InAt(0).AsRegister<Register>();
3443 Register rhs = locations->InAt(1).AsRegister<Register>();
3444 __ Slt(TMP, lhs, rhs);
3445 __ Slt(res, rhs, lhs);
3446 __ Subu(res, res, TMP);
3447 break;
3448 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003449 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003450 MipsLabel done;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003451 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
3452 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
3453 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
3454 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
3455 // TODO: more efficient (direct) comparison with a constant.
3456 __ Slt(TMP, lhs_high, rhs_high);
3457 __ Slt(AT, rhs_high, lhs_high); // Inverted: is actually gt.
3458 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
3459 __ Bnez(res, &done); // If we compared ==, check if lower bits are also equal.
3460 __ Sltu(TMP, lhs_low, rhs_low);
3461 __ Sltu(AT, rhs_low, lhs_low); // Inverted: is actually gt.
3462 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
3463 __ Bind(&done);
3464 break;
3465 }
3466
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003467 case DataType::Type::kFloat32: {
Roland Levillain32ca3752016-02-17 16:49:37 +00003468 bool gt_bias = instruction->IsGtBias();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003469 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3470 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
3471 MipsLabel done;
3472 if (isR6) {
3473 __ CmpEqS(FTMP, lhs, rhs);
3474 __ LoadConst32(res, 0);
3475 __ Bc1nez(FTMP, &done);
3476 if (gt_bias) {
3477 __ CmpLtS(FTMP, lhs, rhs);
3478 __ LoadConst32(res, -1);
3479 __ Bc1nez(FTMP, &done);
3480 __ LoadConst32(res, 1);
3481 } else {
3482 __ CmpLtS(FTMP, rhs, lhs);
3483 __ LoadConst32(res, 1);
3484 __ Bc1nez(FTMP, &done);
3485 __ LoadConst32(res, -1);
3486 }
3487 } else {
3488 if (gt_bias) {
3489 __ ColtS(0, lhs, rhs);
3490 __ LoadConst32(res, -1);
3491 __ Bc1t(0, &done);
3492 __ CeqS(0, lhs, rhs);
3493 __ LoadConst32(res, 1);
3494 __ Movt(res, ZERO, 0);
3495 } else {
3496 __ ColtS(0, rhs, lhs);
3497 __ LoadConst32(res, 1);
3498 __ Bc1t(0, &done);
3499 __ CeqS(0, lhs, rhs);
3500 __ LoadConst32(res, -1);
3501 __ Movt(res, ZERO, 0);
3502 }
3503 }
3504 __ Bind(&done);
3505 break;
3506 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003507 case DataType::Type::kFloat64: {
Roland Levillain32ca3752016-02-17 16:49:37 +00003508 bool gt_bias = instruction->IsGtBias();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003509 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3510 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
3511 MipsLabel done;
3512 if (isR6) {
3513 __ CmpEqD(FTMP, lhs, rhs);
3514 __ LoadConst32(res, 0);
3515 __ Bc1nez(FTMP, &done);
3516 if (gt_bias) {
3517 __ CmpLtD(FTMP, lhs, rhs);
3518 __ LoadConst32(res, -1);
3519 __ Bc1nez(FTMP, &done);
3520 __ LoadConst32(res, 1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003521 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003522 __ CmpLtD(FTMP, rhs, lhs);
3523 __ LoadConst32(res, 1);
3524 __ Bc1nez(FTMP, &done);
3525 __ LoadConst32(res, -1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003526 }
3527 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003528 if (gt_bias) {
3529 __ ColtD(0, lhs, rhs);
3530 __ LoadConst32(res, -1);
3531 __ Bc1t(0, &done);
3532 __ CeqD(0, lhs, rhs);
3533 __ LoadConst32(res, 1);
3534 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003535 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003536 __ ColtD(0, rhs, lhs);
3537 __ LoadConst32(res, 1);
3538 __ Bc1t(0, &done);
3539 __ CeqD(0, lhs, rhs);
3540 __ LoadConst32(res, -1);
3541 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003542 }
3543 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003544 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003545 break;
3546 }
3547
3548 default:
3549 LOG(FATAL) << "Unimplemented compare type " << in_type;
3550 }
3551}
3552
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003553void LocationsBuilderMIPS::HandleCondition(HCondition* instruction) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003554 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003555 switch (instruction->InputAt(0)->GetType()) {
3556 default:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003557 case DataType::Type::kInt64:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003558 locations->SetInAt(0, Location::RequiresRegister());
3559 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
3560 break;
3561
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003562 case DataType::Type::kFloat32:
3563 case DataType::Type::kFloat64:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003564 locations->SetInAt(0, Location::RequiresFpuRegister());
3565 locations->SetInAt(1, Location::RequiresFpuRegister());
3566 break;
3567 }
David Brazdilb3e773e2016-01-26 11:28:37 +00003568 if (!instruction->IsEmittedAtUseSite()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003569 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3570 }
3571}
3572
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003573void InstructionCodeGeneratorMIPS::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00003574 if (instruction->IsEmittedAtUseSite()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003575 return;
3576 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003577
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003578 DataType::Type type = instruction->InputAt(0)->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003579 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003580
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003581 switch (type) {
3582 default:
3583 // Integer case.
3584 GenerateIntCompare(instruction->GetCondition(), locations);
3585 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003586
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003587 case DataType::Type::kInt64:
Tijana Jakovljevic6d482aa2017-02-03 13:24:08 +01003588 GenerateLongCompare(instruction->GetCondition(), locations);
3589 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003590
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003591 case DataType::Type::kFloat32:
3592 case DataType::Type::kFloat64:
Alexey Frunze2ddb7172016-09-06 17:04:55 -07003593 GenerateFpCompare(instruction->GetCondition(), instruction->IsGtBias(), type, locations);
3594 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003595 }
3596}
3597
Alexey Frunze7e99e052015-11-24 19:28:01 -08003598void InstructionCodeGeneratorMIPS::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3599 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003600 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt32);
Alexey Frunze7e99e052015-11-24 19:28:01 -08003601
3602 LocationSummary* locations = instruction->GetLocations();
3603 Location second = locations->InAt(1);
3604 DCHECK(second.IsConstant());
3605
3606 Register out = locations->Out().AsRegister<Register>();
3607 Register dividend = locations->InAt(0).AsRegister<Register>();
3608 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3609 DCHECK(imm == 1 || imm == -1);
3610
3611 if (instruction->IsRem()) {
3612 __ Move(out, ZERO);
3613 } else {
3614 if (imm == -1) {
3615 __ Subu(out, ZERO, dividend);
3616 } else if (out != dividend) {
3617 __ Move(out, dividend);
3618 }
3619 }
3620}
3621
3622void InstructionCodeGeneratorMIPS::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
3623 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003624 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt32);
Alexey Frunze7e99e052015-11-24 19:28:01 -08003625
3626 LocationSummary* locations = instruction->GetLocations();
3627 Location second = locations->InAt(1);
3628 DCHECK(second.IsConstant());
3629
3630 Register out = locations->Out().AsRegister<Register>();
3631 Register dividend = locations->InAt(0).AsRegister<Register>();
3632 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003633 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Alexey Frunze7e99e052015-11-24 19:28:01 -08003634 int ctz_imm = CTZ(abs_imm);
3635
3636 if (instruction->IsDiv()) {
3637 if (ctz_imm == 1) {
3638 // Fast path for division by +/-2, which is very common.
3639 __ Srl(TMP, dividend, 31);
3640 } else {
3641 __ Sra(TMP, dividend, 31);
3642 __ Srl(TMP, TMP, 32 - ctz_imm);
3643 }
3644 __ Addu(out, dividend, TMP);
3645 __ Sra(out, out, ctz_imm);
3646 if (imm < 0) {
3647 __ Subu(out, ZERO, out);
3648 }
3649 } else {
3650 if (ctz_imm == 1) {
3651 // Fast path for modulo +/-2, which is very common.
3652 __ Sra(TMP, dividend, 31);
3653 __ Subu(out, dividend, TMP);
3654 __ Andi(out, out, 1);
3655 __ Addu(out, out, TMP);
3656 } else {
3657 __ Sra(TMP, dividend, 31);
3658 __ Srl(TMP, TMP, 32 - ctz_imm);
3659 __ Addu(out, dividend, TMP);
3660 if (IsUint<16>(abs_imm - 1)) {
3661 __ Andi(out, out, abs_imm - 1);
3662 } else {
3663 __ Sll(out, out, 32 - ctz_imm);
3664 __ Srl(out, out, 32 - ctz_imm);
3665 }
3666 __ Subu(out, out, TMP);
3667 }
3668 }
3669}
3670
3671void InstructionCodeGeneratorMIPS::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3672 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003673 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt32);
Alexey Frunze7e99e052015-11-24 19:28:01 -08003674
3675 LocationSummary* locations = instruction->GetLocations();
3676 Location second = locations->InAt(1);
3677 DCHECK(second.IsConstant());
3678
3679 Register out = locations->Out().AsRegister<Register>();
3680 Register dividend = locations->InAt(0).AsRegister<Register>();
3681 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3682
3683 int64_t magic;
3684 int shift;
3685 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3686
3687 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3688
3689 __ LoadConst32(TMP, magic);
3690 if (isR6) {
3691 __ MuhR6(TMP, dividend, TMP);
3692 } else {
3693 __ MultR2(dividend, TMP);
3694 __ Mfhi(TMP);
3695 }
3696 if (imm > 0 && magic < 0) {
3697 __ Addu(TMP, TMP, dividend);
3698 } else if (imm < 0 && magic > 0) {
3699 __ Subu(TMP, TMP, dividend);
3700 }
3701
3702 if (shift != 0) {
3703 __ Sra(TMP, TMP, shift);
3704 }
3705
3706 if (instruction->IsDiv()) {
3707 __ Sra(out, TMP, 31);
3708 __ Subu(out, TMP, out);
3709 } else {
3710 __ Sra(AT, TMP, 31);
3711 __ Subu(AT, TMP, AT);
3712 __ LoadConst32(TMP, imm);
3713 if (isR6) {
3714 __ MulR6(TMP, AT, TMP);
3715 } else {
3716 __ MulR2(TMP, AT, TMP);
3717 }
3718 __ Subu(out, dividend, TMP);
3719 }
3720}
3721
3722void InstructionCodeGeneratorMIPS::GenerateDivRemIntegral(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 Register out = locations->Out().AsRegister<Register>();
3728 Location second = locations->InAt(1);
3729
3730 if (second.IsConstant()) {
3731 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3732 if (imm == 0) {
3733 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3734 } else if (imm == 1 || imm == -1) {
3735 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003736 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08003737 DivRemByPowerOfTwo(instruction);
3738 } else {
3739 DCHECK(imm <= -2 || imm >= 2);
3740 GenerateDivRemWithAnyConstant(instruction);
3741 }
3742 } else {
3743 Register dividend = locations->InAt(0).AsRegister<Register>();
3744 Register divisor = second.AsRegister<Register>();
3745 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3746 if (instruction->IsDiv()) {
3747 if (isR6) {
3748 __ DivR6(out, dividend, divisor);
3749 } else {
3750 __ DivR2(out, dividend, divisor);
3751 }
3752 } else {
3753 if (isR6) {
3754 __ ModR6(out, dividend, divisor);
3755 } else {
3756 __ ModR2(out, dividend, divisor);
3757 }
3758 }
3759 }
3760}
3761
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003762void LocationsBuilderMIPS::VisitDiv(HDiv* div) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003763 DataType::Type type = div->GetResultType();
3764 LocationSummary::CallKind call_kind = (type == DataType::Type::kInt64)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003765 ? LocationSummary::kCallOnMainOnly
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003766 : LocationSummary::kNoCall;
3767
3768 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
3769
3770 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003771 case DataType::Type::kInt32:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003772 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08003773 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003774 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3775 break;
3776
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003777 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003778 InvokeRuntimeCallingConvention calling_convention;
3779 locations->SetInAt(0, Location::RegisterPairLocation(
3780 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3781 locations->SetInAt(1, Location::RegisterPairLocation(
3782 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3783 locations->SetOut(calling_convention.GetReturnLocation(type));
3784 break;
3785 }
3786
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003787 case DataType::Type::kFloat32:
3788 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003789 locations->SetInAt(0, Location::RequiresFpuRegister());
3790 locations->SetInAt(1, Location::RequiresFpuRegister());
3791 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3792 break;
3793
3794 default:
3795 LOG(FATAL) << "Unexpected div type " << type;
3796 }
3797}
3798
3799void InstructionCodeGeneratorMIPS::VisitDiv(HDiv* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003800 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003801 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003802
3803 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003804 case DataType::Type::kInt32:
Alexey Frunze7e99e052015-11-24 19:28:01 -08003805 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003806 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003807 case DataType::Type::kInt64: {
Serban Constantinescufca16662016-07-14 09:21:59 +01003808 codegen_->InvokeRuntime(kQuickLdiv, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003809 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
3810 break;
3811 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003812 case DataType::Type::kFloat32:
3813 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003814 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
3815 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3816 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003817 if (type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003818 __ DivS(dst, lhs, rhs);
3819 } else {
3820 __ DivD(dst, lhs, rhs);
3821 }
3822 break;
3823 }
3824 default:
3825 LOG(FATAL) << "Unexpected div type " << type;
3826 }
3827}
3828
3829void LocationsBuilderMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003830 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003831 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003832}
3833
3834void InstructionCodeGeneratorMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
3835 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathMIPS(instruction);
3836 codegen_->AddSlowPath(slow_path);
3837 Location value = instruction->GetLocations()->InAt(0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003838 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003839
3840 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003841 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003842 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003843 case DataType::Type::kInt8:
3844 case DataType::Type::kUint16:
3845 case DataType::Type::kInt16:
3846 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003847 if (value.IsConstant()) {
3848 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3849 __ B(slow_path->GetEntryLabel());
3850 } else {
3851 // A division by a non-null constant is valid. We don't need to perform
3852 // any check, so simply fall through.
3853 }
3854 } else {
3855 DCHECK(value.IsRegister()) << value;
3856 __ Beqz(value.AsRegister<Register>(), slow_path->GetEntryLabel());
3857 }
3858 break;
3859 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003860 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003861 if (value.IsConstant()) {
3862 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3863 __ B(slow_path->GetEntryLabel());
3864 } else {
3865 // A division by a non-null constant is valid. We don't need to perform
3866 // any check, so simply fall through.
3867 }
3868 } else {
3869 DCHECK(value.IsRegisterPair()) << value;
3870 __ Or(TMP, value.AsRegisterPairHigh<Register>(), value.AsRegisterPairLow<Register>());
3871 __ Beqz(TMP, slow_path->GetEntryLabel());
3872 }
3873 break;
3874 }
3875 default:
3876 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
3877 }
3878}
3879
3880void LocationsBuilderMIPS::VisitDoubleConstant(HDoubleConstant* constant) {
3881 LocationSummary* locations =
3882 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3883 locations->SetOut(Location::ConstantLocation(constant));
3884}
3885
3886void InstructionCodeGeneratorMIPS::VisitDoubleConstant(HDoubleConstant* cst ATTRIBUTE_UNUSED) {
3887 // Will be generated at use site.
3888}
3889
3890void LocationsBuilderMIPS::VisitExit(HExit* exit) {
3891 exit->SetLocations(nullptr);
3892}
3893
3894void InstructionCodeGeneratorMIPS::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
3895}
3896
3897void LocationsBuilderMIPS::VisitFloatConstant(HFloatConstant* constant) {
3898 LocationSummary* locations =
3899 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3900 locations->SetOut(Location::ConstantLocation(constant));
3901}
3902
3903void InstructionCodeGeneratorMIPS::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
3904 // Will be generated at use site.
3905}
3906
3907void LocationsBuilderMIPS::VisitGoto(HGoto* got) {
3908 got->SetLocations(nullptr);
3909}
3910
3911void InstructionCodeGeneratorMIPS::HandleGoto(HInstruction* got, HBasicBlock* successor) {
3912 DCHECK(!successor->IsExitBlock());
3913 HBasicBlock* block = got->GetBlock();
3914 HInstruction* previous = got->GetPrevious();
3915 HLoopInformation* info = block->GetLoopInformation();
3916
3917 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
3918 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
3919 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
3920 return;
3921 }
3922 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
3923 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
3924 }
3925 if (!codegen_->GoesToNextBlock(block, successor)) {
3926 __ B(codegen_->GetLabelOf(successor));
3927 }
3928}
3929
3930void InstructionCodeGeneratorMIPS::VisitGoto(HGoto* got) {
3931 HandleGoto(got, got->GetSuccessor());
3932}
3933
3934void LocationsBuilderMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
3935 try_boundary->SetLocations(nullptr);
3936}
3937
3938void InstructionCodeGeneratorMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
3939 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
3940 if (!successor->IsExitBlock()) {
3941 HandleGoto(try_boundary, successor);
3942 }
3943}
3944
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003945void InstructionCodeGeneratorMIPS::GenerateIntCompare(IfCondition cond,
3946 LocationSummary* locations) {
3947 Register dst = locations->Out().AsRegister<Register>();
3948 Register lhs = locations->InAt(0).AsRegister<Register>();
3949 Location rhs_location = locations->InAt(1);
3950 Register rhs_reg = ZERO;
3951 int64_t rhs_imm = 0;
3952 bool use_imm = rhs_location.IsConstant();
3953 if (use_imm) {
3954 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
3955 } else {
3956 rhs_reg = rhs_location.AsRegister<Register>();
3957 }
3958
3959 switch (cond) {
3960 case kCondEQ:
3961 case kCondNE:
Alexey Frunzee7697712016-09-15 21:37:49 -07003962 if (use_imm && IsInt<16>(-rhs_imm)) {
3963 if (rhs_imm == 0) {
3964 if (cond == kCondEQ) {
3965 __ Sltiu(dst, lhs, 1);
3966 } else {
3967 __ Sltu(dst, ZERO, lhs);
3968 }
3969 } else {
3970 __ Addiu(dst, lhs, -rhs_imm);
3971 if (cond == kCondEQ) {
3972 __ Sltiu(dst, dst, 1);
3973 } else {
3974 __ Sltu(dst, ZERO, dst);
3975 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003976 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003977 } else {
Alexey Frunzee7697712016-09-15 21:37:49 -07003978 if (use_imm && IsUint<16>(rhs_imm)) {
3979 __ Xori(dst, lhs, rhs_imm);
3980 } else {
3981 if (use_imm) {
3982 rhs_reg = TMP;
3983 __ LoadConst32(rhs_reg, rhs_imm);
3984 }
3985 __ Xor(dst, lhs, rhs_reg);
3986 }
3987 if (cond == kCondEQ) {
3988 __ Sltiu(dst, dst, 1);
3989 } else {
3990 __ Sltu(dst, ZERO, dst);
3991 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003992 }
3993 break;
3994
3995 case kCondLT:
3996 case kCondGE:
3997 if (use_imm && IsInt<16>(rhs_imm)) {
3998 __ Slti(dst, lhs, rhs_imm);
3999 } else {
4000 if (use_imm) {
4001 rhs_reg = TMP;
4002 __ LoadConst32(rhs_reg, rhs_imm);
4003 }
4004 __ Slt(dst, lhs, rhs_reg);
4005 }
4006 if (cond == kCondGE) {
4007 // Simulate lhs >= rhs via !(lhs < rhs) since there's
4008 // only the slt instruction but no sge.
4009 __ Xori(dst, dst, 1);
4010 }
4011 break;
4012
4013 case kCondLE:
4014 case kCondGT:
4015 if (use_imm && IsInt<16>(rhs_imm + 1)) {
4016 // Simulate lhs <= rhs via lhs < rhs + 1.
4017 __ Slti(dst, lhs, rhs_imm + 1);
4018 if (cond == kCondGT) {
4019 // Simulate lhs > rhs via !(lhs <= rhs) since there's
4020 // only the slti instruction but no sgti.
4021 __ Xori(dst, dst, 1);
4022 }
4023 } else {
4024 if (use_imm) {
4025 rhs_reg = TMP;
4026 __ LoadConst32(rhs_reg, rhs_imm);
4027 }
4028 __ Slt(dst, rhs_reg, lhs);
4029 if (cond == kCondLE) {
4030 // Simulate lhs <= rhs via !(rhs < lhs) since there's
4031 // only the slt instruction but no sle.
4032 __ Xori(dst, dst, 1);
4033 }
4034 }
4035 break;
4036
4037 case kCondB:
4038 case kCondAE:
4039 if (use_imm && IsInt<16>(rhs_imm)) {
4040 // Sltiu sign-extends its 16-bit immediate operand before
4041 // the comparison and thus lets us compare directly with
4042 // unsigned values in the ranges [0, 0x7fff] and
4043 // [0xffff8000, 0xffffffff].
4044 __ Sltiu(dst, lhs, rhs_imm);
4045 } else {
4046 if (use_imm) {
4047 rhs_reg = TMP;
4048 __ LoadConst32(rhs_reg, rhs_imm);
4049 }
4050 __ Sltu(dst, lhs, rhs_reg);
4051 }
4052 if (cond == kCondAE) {
4053 // Simulate lhs >= rhs via !(lhs < rhs) since there's
4054 // only the sltu instruction but no sgeu.
4055 __ Xori(dst, dst, 1);
4056 }
4057 break;
4058
4059 case kCondBE:
4060 case kCondA:
4061 if (use_imm && (rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4062 // Simulate lhs <= rhs via lhs < rhs + 1.
4063 // Note that this only works if rhs + 1 does not overflow
4064 // to 0, hence the check above.
4065 // Sltiu sign-extends its 16-bit immediate operand before
4066 // the comparison and thus lets us compare directly with
4067 // unsigned values in the ranges [0, 0x7fff] and
4068 // [0xffff8000, 0xffffffff].
4069 __ Sltiu(dst, lhs, rhs_imm + 1);
4070 if (cond == kCondA) {
4071 // Simulate lhs > rhs via !(lhs <= rhs) since there's
4072 // only the sltiu instruction but no sgtiu.
4073 __ Xori(dst, dst, 1);
4074 }
4075 } else {
4076 if (use_imm) {
4077 rhs_reg = TMP;
4078 __ LoadConst32(rhs_reg, rhs_imm);
4079 }
4080 __ Sltu(dst, rhs_reg, lhs);
4081 if (cond == kCondBE) {
4082 // Simulate lhs <= rhs via !(rhs < lhs) since there's
4083 // only the sltu instruction but no sleu.
4084 __ Xori(dst, dst, 1);
4085 }
4086 }
4087 break;
4088 }
4089}
4090
Alexey Frunze674b9ee2016-09-20 14:54:15 -07004091bool InstructionCodeGeneratorMIPS::MaterializeIntCompare(IfCondition cond,
4092 LocationSummary* input_locations,
4093 Register dst) {
4094 Register lhs = input_locations->InAt(0).AsRegister<Register>();
4095 Location rhs_location = input_locations->InAt(1);
4096 Register rhs_reg = ZERO;
4097 int64_t rhs_imm = 0;
4098 bool use_imm = rhs_location.IsConstant();
4099 if (use_imm) {
4100 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
4101 } else {
4102 rhs_reg = rhs_location.AsRegister<Register>();
4103 }
4104
4105 switch (cond) {
4106 case kCondEQ:
4107 case kCondNE:
4108 if (use_imm && IsInt<16>(-rhs_imm)) {
4109 __ Addiu(dst, lhs, -rhs_imm);
4110 } else if (use_imm && IsUint<16>(rhs_imm)) {
4111 __ Xori(dst, lhs, rhs_imm);
4112 } else {
4113 if (use_imm) {
4114 rhs_reg = TMP;
4115 __ LoadConst32(rhs_reg, rhs_imm);
4116 }
4117 __ Xor(dst, lhs, rhs_reg);
4118 }
4119 return (cond == kCondEQ);
4120
4121 case kCondLT:
4122 case kCondGE:
4123 if (use_imm && IsInt<16>(rhs_imm)) {
4124 __ Slti(dst, lhs, rhs_imm);
4125 } else {
4126 if (use_imm) {
4127 rhs_reg = TMP;
4128 __ LoadConst32(rhs_reg, rhs_imm);
4129 }
4130 __ Slt(dst, lhs, rhs_reg);
4131 }
4132 return (cond == kCondGE);
4133
4134 case kCondLE:
4135 case kCondGT:
4136 if (use_imm && IsInt<16>(rhs_imm + 1)) {
4137 // Simulate lhs <= rhs via lhs < rhs + 1.
4138 __ Slti(dst, lhs, rhs_imm + 1);
4139 return (cond == kCondGT);
4140 } else {
4141 if (use_imm) {
4142 rhs_reg = TMP;
4143 __ LoadConst32(rhs_reg, rhs_imm);
4144 }
4145 __ Slt(dst, rhs_reg, lhs);
4146 return (cond == kCondLE);
4147 }
4148
4149 case kCondB:
4150 case kCondAE:
4151 if (use_imm && IsInt<16>(rhs_imm)) {
4152 // Sltiu sign-extends its 16-bit immediate operand before
4153 // the comparison and thus lets us compare directly with
4154 // unsigned values in the ranges [0, 0x7fff] and
4155 // [0xffff8000, 0xffffffff].
4156 __ Sltiu(dst, lhs, rhs_imm);
4157 } else {
4158 if (use_imm) {
4159 rhs_reg = TMP;
4160 __ LoadConst32(rhs_reg, rhs_imm);
4161 }
4162 __ Sltu(dst, lhs, rhs_reg);
4163 }
4164 return (cond == kCondAE);
4165
4166 case kCondBE:
4167 case kCondA:
4168 if (use_imm && (rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4169 // Simulate lhs <= rhs via lhs < rhs + 1.
4170 // Note that this only works if rhs + 1 does not overflow
4171 // to 0, hence the check above.
4172 // Sltiu sign-extends its 16-bit immediate operand before
4173 // the comparison and thus lets us compare directly with
4174 // unsigned values in the ranges [0, 0x7fff] and
4175 // [0xffff8000, 0xffffffff].
4176 __ Sltiu(dst, lhs, rhs_imm + 1);
4177 return (cond == kCondA);
4178 } else {
4179 if (use_imm) {
4180 rhs_reg = TMP;
4181 __ LoadConst32(rhs_reg, rhs_imm);
4182 }
4183 __ Sltu(dst, rhs_reg, lhs);
4184 return (cond == kCondBE);
4185 }
4186 }
4187}
4188
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004189void InstructionCodeGeneratorMIPS::GenerateIntCompareAndBranch(IfCondition cond,
4190 LocationSummary* locations,
4191 MipsLabel* label) {
4192 Register lhs = locations->InAt(0).AsRegister<Register>();
4193 Location rhs_location = locations->InAt(1);
4194 Register rhs_reg = ZERO;
Alexey Frunzee7697712016-09-15 21:37:49 -07004195 int64_t rhs_imm = 0;
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004196 bool use_imm = rhs_location.IsConstant();
4197 if (use_imm) {
4198 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
4199 } else {
4200 rhs_reg = rhs_location.AsRegister<Register>();
4201 }
4202
4203 if (use_imm && rhs_imm == 0) {
4204 switch (cond) {
4205 case kCondEQ:
4206 case kCondBE: // <= 0 if zero
4207 __ Beqz(lhs, label);
4208 break;
4209 case kCondNE:
4210 case kCondA: // > 0 if non-zero
4211 __ Bnez(lhs, label);
4212 break;
4213 case kCondLT:
4214 __ Bltz(lhs, label);
4215 break;
4216 case kCondGE:
4217 __ Bgez(lhs, label);
4218 break;
4219 case kCondLE:
4220 __ Blez(lhs, label);
4221 break;
4222 case kCondGT:
4223 __ Bgtz(lhs, label);
4224 break;
4225 case kCondB: // always false
4226 break;
4227 case kCondAE: // always true
4228 __ B(label);
4229 break;
4230 }
4231 } else {
Alexey Frunzee7697712016-09-15 21:37:49 -07004232 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
4233 if (isR6 || !use_imm) {
4234 if (use_imm) {
4235 rhs_reg = TMP;
4236 __ LoadConst32(rhs_reg, rhs_imm);
4237 }
4238 switch (cond) {
4239 case kCondEQ:
4240 __ Beq(lhs, rhs_reg, label);
4241 break;
4242 case kCondNE:
4243 __ Bne(lhs, rhs_reg, label);
4244 break;
4245 case kCondLT:
4246 __ Blt(lhs, rhs_reg, label);
4247 break;
4248 case kCondGE:
4249 __ Bge(lhs, rhs_reg, label);
4250 break;
4251 case kCondLE:
4252 __ Bge(rhs_reg, lhs, label);
4253 break;
4254 case kCondGT:
4255 __ Blt(rhs_reg, lhs, label);
4256 break;
4257 case kCondB:
4258 __ Bltu(lhs, rhs_reg, label);
4259 break;
4260 case kCondAE:
4261 __ Bgeu(lhs, rhs_reg, label);
4262 break;
4263 case kCondBE:
4264 __ Bgeu(rhs_reg, lhs, label);
4265 break;
4266 case kCondA:
4267 __ Bltu(rhs_reg, lhs, label);
4268 break;
4269 }
4270 } else {
4271 // Special cases for more efficient comparison with constants on R2.
4272 switch (cond) {
4273 case kCondEQ:
4274 __ LoadConst32(TMP, rhs_imm);
4275 __ Beq(lhs, TMP, label);
4276 break;
4277 case kCondNE:
4278 __ LoadConst32(TMP, rhs_imm);
4279 __ Bne(lhs, TMP, label);
4280 break;
4281 case kCondLT:
4282 if (IsInt<16>(rhs_imm)) {
4283 __ Slti(TMP, lhs, rhs_imm);
4284 __ Bnez(TMP, label);
4285 } else {
4286 __ LoadConst32(TMP, rhs_imm);
4287 __ Blt(lhs, TMP, label);
4288 }
4289 break;
4290 case kCondGE:
4291 if (IsInt<16>(rhs_imm)) {
4292 __ Slti(TMP, lhs, rhs_imm);
4293 __ Beqz(TMP, label);
4294 } else {
4295 __ LoadConst32(TMP, rhs_imm);
4296 __ Bge(lhs, TMP, label);
4297 }
4298 break;
4299 case kCondLE:
4300 if (IsInt<16>(rhs_imm + 1)) {
4301 // Simulate lhs <= rhs via lhs < rhs + 1.
4302 __ Slti(TMP, lhs, rhs_imm + 1);
4303 __ Bnez(TMP, label);
4304 } else {
4305 __ LoadConst32(TMP, rhs_imm);
4306 __ Bge(TMP, lhs, label);
4307 }
4308 break;
4309 case kCondGT:
4310 if (IsInt<16>(rhs_imm + 1)) {
4311 // Simulate lhs > rhs via !(lhs < rhs + 1).
4312 __ Slti(TMP, lhs, rhs_imm + 1);
4313 __ Beqz(TMP, label);
4314 } else {
4315 __ LoadConst32(TMP, rhs_imm);
4316 __ Blt(TMP, lhs, label);
4317 }
4318 break;
4319 case kCondB:
4320 if (IsInt<16>(rhs_imm)) {
4321 __ Sltiu(TMP, lhs, rhs_imm);
4322 __ Bnez(TMP, label);
4323 } else {
4324 __ LoadConst32(TMP, rhs_imm);
4325 __ Bltu(lhs, TMP, label);
4326 }
4327 break;
4328 case kCondAE:
4329 if (IsInt<16>(rhs_imm)) {
4330 __ Sltiu(TMP, lhs, rhs_imm);
4331 __ Beqz(TMP, label);
4332 } else {
4333 __ LoadConst32(TMP, rhs_imm);
4334 __ Bgeu(lhs, TMP, label);
4335 }
4336 break;
4337 case kCondBE:
4338 if ((rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4339 // Simulate lhs <= rhs via lhs < rhs + 1.
4340 // Note that this only works if rhs + 1 does not overflow
4341 // to 0, hence the check above.
4342 __ Sltiu(TMP, lhs, rhs_imm + 1);
4343 __ Bnez(TMP, label);
4344 } else {
4345 __ LoadConst32(TMP, rhs_imm);
4346 __ Bgeu(TMP, lhs, label);
4347 }
4348 break;
4349 case kCondA:
4350 if ((rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4351 // Simulate lhs > rhs via !(lhs < rhs + 1).
4352 // Note that this only works if rhs + 1 does not overflow
4353 // to 0, hence the check above.
4354 __ Sltiu(TMP, lhs, rhs_imm + 1);
4355 __ Beqz(TMP, label);
4356 } else {
4357 __ LoadConst32(TMP, rhs_imm);
4358 __ Bltu(TMP, lhs, label);
4359 }
4360 break;
4361 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004362 }
4363 }
4364}
4365
Tijana Jakovljevic6d482aa2017-02-03 13:24:08 +01004366void InstructionCodeGeneratorMIPS::GenerateLongCompare(IfCondition cond,
4367 LocationSummary* locations) {
4368 Register dst = locations->Out().AsRegister<Register>();
4369 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
4370 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
4371 Location rhs_location = locations->InAt(1);
4372 Register rhs_high = ZERO;
4373 Register rhs_low = ZERO;
4374 int64_t imm = 0;
4375 uint32_t imm_high = 0;
4376 uint32_t imm_low = 0;
4377 bool use_imm = rhs_location.IsConstant();
4378 if (use_imm) {
4379 imm = rhs_location.GetConstant()->AsLongConstant()->GetValue();
4380 imm_high = High32Bits(imm);
4381 imm_low = Low32Bits(imm);
4382 } else {
4383 rhs_high = rhs_location.AsRegisterPairHigh<Register>();
4384 rhs_low = rhs_location.AsRegisterPairLow<Register>();
4385 }
4386 if (use_imm && imm == 0) {
4387 switch (cond) {
4388 case kCondEQ:
4389 case kCondBE: // <= 0 if zero
4390 __ Or(dst, lhs_high, lhs_low);
4391 __ Sltiu(dst, dst, 1);
4392 break;
4393 case kCondNE:
4394 case kCondA: // > 0 if non-zero
4395 __ Or(dst, lhs_high, lhs_low);
4396 __ Sltu(dst, ZERO, dst);
4397 break;
4398 case kCondLT:
4399 __ Slt(dst, lhs_high, ZERO);
4400 break;
4401 case kCondGE:
4402 __ Slt(dst, lhs_high, ZERO);
4403 __ Xori(dst, dst, 1);
4404 break;
4405 case kCondLE:
4406 __ Or(TMP, lhs_high, lhs_low);
4407 __ Sra(AT, lhs_high, 31);
4408 __ Sltu(dst, AT, TMP);
4409 __ Xori(dst, dst, 1);
4410 break;
4411 case kCondGT:
4412 __ Or(TMP, lhs_high, lhs_low);
4413 __ Sra(AT, lhs_high, 31);
4414 __ Sltu(dst, AT, TMP);
4415 break;
4416 case kCondB: // always false
4417 __ Andi(dst, dst, 0);
4418 break;
4419 case kCondAE: // always true
4420 __ Ori(dst, ZERO, 1);
4421 break;
4422 }
4423 } else if (use_imm) {
4424 // TODO: more efficient comparison with constants without loading them into TMP/AT.
4425 switch (cond) {
4426 case kCondEQ:
4427 __ LoadConst32(TMP, imm_high);
4428 __ Xor(TMP, TMP, lhs_high);
4429 __ LoadConst32(AT, imm_low);
4430 __ Xor(AT, AT, lhs_low);
4431 __ Or(dst, TMP, AT);
4432 __ Sltiu(dst, dst, 1);
4433 break;
4434 case kCondNE:
4435 __ LoadConst32(TMP, imm_high);
4436 __ Xor(TMP, TMP, lhs_high);
4437 __ LoadConst32(AT, imm_low);
4438 __ Xor(AT, AT, lhs_low);
4439 __ Or(dst, TMP, AT);
4440 __ Sltu(dst, ZERO, dst);
4441 break;
4442 case kCondLT:
4443 case kCondGE:
4444 if (dst == lhs_low) {
4445 __ LoadConst32(TMP, imm_low);
4446 __ Sltu(dst, lhs_low, TMP);
4447 }
4448 __ LoadConst32(TMP, imm_high);
4449 __ Slt(AT, lhs_high, TMP);
4450 __ Slt(TMP, TMP, lhs_high);
4451 if (dst != lhs_low) {
4452 __ LoadConst32(dst, imm_low);
4453 __ Sltu(dst, lhs_low, dst);
4454 }
4455 __ Slt(dst, TMP, dst);
4456 __ Or(dst, dst, AT);
4457 if (cond == kCondGE) {
4458 __ Xori(dst, dst, 1);
4459 }
4460 break;
4461 case kCondGT:
4462 case kCondLE:
4463 if (dst == lhs_low) {
4464 __ LoadConst32(TMP, imm_low);
4465 __ Sltu(dst, TMP, lhs_low);
4466 }
4467 __ LoadConst32(TMP, imm_high);
4468 __ Slt(AT, TMP, lhs_high);
4469 __ Slt(TMP, lhs_high, TMP);
4470 if (dst != lhs_low) {
4471 __ LoadConst32(dst, imm_low);
4472 __ Sltu(dst, dst, lhs_low);
4473 }
4474 __ Slt(dst, TMP, dst);
4475 __ Or(dst, dst, AT);
4476 if (cond == kCondLE) {
4477 __ Xori(dst, dst, 1);
4478 }
4479 break;
4480 case kCondB:
4481 case kCondAE:
4482 if (dst == lhs_low) {
4483 __ LoadConst32(TMP, imm_low);
4484 __ Sltu(dst, lhs_low, TMP);
4485 }
4486 __ LoadConst32(TMP, imm_high);
4487 __ Sltu(AT, lhs_high, TMP);
4488 __ Sltu(TMP, TMP, lhs_high);
4489 if (dst != lhs_low) {
4490 __ LoadConst32(dst, imm_low);
4491 __ Sltu(dst, lhs_low, dst);
4492 }
4493 __ Slt(dst, TMP, dst);
4494 __ Or(dst, dst, AT);
4495 if (cond == kCondAE) {
4496 __ Xori(dst, dst, 1);
4497 }
4498 break;
4499 case kCondA:
4500 case kCondBE:
4501 if (dst == lhs_low) {
4502 __ LoadConst32(TMP, imm_low);
4503 __ Sltu(dst, TMP, lhs_low);
4504 }
4505 __ LoadConst32(TMP, imm_high);
4506 __ Sltu(AT, TMP, lhs_high);
4507 __ Sltu(TMP, lhs_high, TMP);
4508 if (dst != lhs_low) {
4509 __ LoadConst32(dst, imm_low);
4510 __ Sltu(dst, dst, lhs_low);
4511 }
4512 __ Slt(dst, TMP, dst);
4513 __ Or(dst, dst, AT);
4514 if (cond == kCondBE) {
4515 __ Xori(dst, dst, 1);
4516 }
4517 break;
4518 }
4519 } else {
4520 switch (cond) {
4521 case kCondEQ:
4522 __ Xor(TMP, lhs_high, rhs_high);
4523 __ Xor(AT, lhs_low, rhs_low);
4524 __ Or(dst, TMP, AT);
4525 __ Sltiu(dst, dst, 1);
4526 break;
4527 case kCondNE:
4528 __ Xor(TMP, lhs_high, rhs_high);
4529 __ Xor(AT, lhs_low, rhs_low);
4530 __ Or(dst, TMP, AT);
4531 __ Sltu(dst, ZERO, dst);
4532 break;
4533 case kCondLT:
4534 case kCondGE:
4535 __ Slt(TMP, rhs_high, lhs_high);
4536 __ Sltu(AT, lhs_low, rhs_low);
4537 __ Slt(TMP, TMP, AT);
4538 __ Slt(AT, lhs_high, rhs_high);
4539 __ Or(dst, AT, TMP);
4540 if (cond == kCondGE) {
4541 __ Xori(dst, dst, 1);
4542 }
4543 break;
4544 case kCondGT:
4545 case kCondLE:
4546 __ Slt(TMP, lhs_high, rhs_high);
4547 __ Sltu(AT, rhs_low, lhs_low);
4548 __ Slt(TMP, TMP, AT);
4549 __ Slt(AT, rhs_high, lhs_high);
4550 __ Or(dst, AT, TMP);
4551 if (cond == kCondLE) {
4552 __ Xori(dst, dst, 1);
4553 }
4554 break;
4555 case kCondB:
4556 case kCondAE:
4557 __ Sltu(TMP, rhs_high, lhs_high);
4558 __ Sltu(AT, lhs_low, rhs_low);
4559 __ Slt(TMP, TMP, AT);
4560 __ Sltu(AT, lhs_high, rhs_high);
4561 __ Or(dst, AT, TMP);
4562 if (cond == kCondAE) {
4563 __ Xori(dst, dst, 1);
4564 }
4565 break;
4566 case kCondA:
4567 case kCondBE:
4568 __ Sltu(TMP, lhs_high, rhs_high);
4569 __ Sltu(AT, rhs_low, lhs_low);
4570 __ Slt(TMP, TMP, AT);
4571 __ Sltu(AT, rhs_high, lhs_high);
4572 __ Or(dst, AT, TMP);
4573 if (cond == kCondBE) {
4574 __ Xori(dst, dst, 1);
4575 }
4576 break;
4577 }
4578 }
4579}
4580
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004581void InstructionCodeGeneratorMIPS::GenerateLongCompareAndBranch(IfCondition cond,
4582 LocationSummary* locations,
4583 MipsLabel* label) {
4584 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
4585 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
4586 Location rhs_location = locations->InAt(1);
4587 Register rhs_high = ZERO;
4588 Register rhs_low = ZERO;
4589 int64_t imm = 0;
4590 uint32_t imm_high = 0;
4591 uint32_t imm_low = 0;
4592 bool use_imm = rhs_location.IsConstant();
4593 if (use_imm) {
4594 imm = rhs_location.GetConstant()->AsLongConstant()->GetValue();
4595 imm_high = High32Bits(imm);
4596 imm_low = Low32Bits(imm);
4597 } else {
4598 rhs_high = rhs_location.AsRegisterPairHigh<Register>();
4599 rhs_low = rhs_location.AsRegisterPairLow<Register>();
4600 }
4601
4602 if (use_imm && imm == 0) {
4603 switch (cond) {
4604 case kCondEQ:
4605 case kCondBE: // <= 0 if zero
4606 __ Or(TMP, lhs_high, lhs_low);
4607 __ Beqz(TMP, label);
4608 break;
4609 case kCondNE:
4610 case kCondA: // > 0 if non-zero
4611 __ Or(TMP, lhs_high, lhs_low);
4612 __ Bnez(TMP, label);
4613 break;
4614 case kCondLT:
4615 __ Bltz(lhs_high, label);
4616 break;
4617 case kCondGE:
4618 __ Bgez(lhs_high, label);
4619 break;
4620 case kCondLE:
4621 __ Or(TMP, lhs_high, lhs_low);
4622 __ Sra(AT, lhs_high, 31);
4623 __ Bgeu(AT, TMP, label);
4624 break;
4625 case kCondGT:
4626 __ Or(TMP, lhs_high, lhs_low);
4627 __ Sra(AT, lhs_high, 31);
4628 __ Bltu(AT, TMP, label);
4629 break;
4630 case kCondB: // always false
4631 break;
4632 case kCondAE: // always true
4633 __ B(label);
4634 break;
4635 }
4636 } else if (use_imm) {
4637 // TODO: more efficient comparison with constants without loading them into TMP/AT.
4638 switch (cond) {
4639 case kCondEQ:
4640 __ LoadConst32(TMP, imm_high);
4641 __ Xor(TMP, TMP, lhs_high);
4642 __ LoadConst32(AT, imm_low);
4643 __ Xor(AT, AT, lhs_low);
4644 __ Or(TMP, TMP, AT);
4645 __ Beqz(TMP, label);
4646 break;
4647 case kCondNE:
4648 __ LoadConst32(TMP, imm_high);
4649 __ Xor(TMP, TMP, lhs_high);
4650 __ LoadConst32(AT, imm_low);
4651 __ Xor(AT, AT, lhs_low);
4652 __ Or(TMP, TMP, AT);
4653 __ Bnez(TMP, label);
4654 break;
4655 case kCondLT:
4656 __ LoadConst32(TMP, imm_high);
4657 __ Blt(lhs_high, TMP, label);
4658 __ Slt(TMP, TMP, lhs_high);
4659 __ LoadConst32(AT, imm_low);
4660 __ Sltu(AT, lhs_low, AT);
4661 __ Blt(TMP, AT, label);
4662 break;
4663 case kCondGE:
4664 __ LoadConst32(TMP, imm_high);
4665 __ Blt(TMP, lhs_high, label);
4666 __ Slt(TMP, lhs_high, TMP);
4667 __ LoadConst32(AT, imm_low);
4668 __ Sltu(AT, lhs_low, AT);
4669 __ Or(TMP, TMP, AT);
4670 __ Beqz(TMP, label);
4671 break;
4672 case kCondLE:
4673 __ LoadConst32(TMP, imm_high);
4674 __ Blt(lhs_high, TMP, label);
4675 __ Slt(TMP, TMP, lhs_high);
4676 __ LoadConst32(AT, imm_low);
4677 __ Sltu(AT, AT, lhs_low);
4678 __ Or(TMP, TMP, AT);
4679 __ Beqz(TMP, label);
4680 break;
4681 case kCondGT:
4682 __ LoadConst32(TMP, imm_high);
4683 __ Blt(TMP, lhs_high, label);
4684 __ Slt(TMP, lhs_high, TMP);
4685 __ LoadConst32(AT, imm_low);
4686 __ Sltu(AT, AT, lhs_low);
4687 __ Blt(TMP, AT, label);
4688 break;
4689 case kCondB:
4690 __ LoadConst32(TMP, imm_high);
4691 __ Bltu(lhs_high, TMP, label);
4692 __ Sltu(TMP, TMP, lhs_high);
4693 __ LoadConst32(AT, imm_low);
4694 __ Sltu(AT, lhs_low, AT);
4695 __ Blt(TMP, AT, label);
4696 break;
4697 case kCondAE:
4698 __ LoadConst32(TMP, imm_high);
4699 __ Bltu(TMP, lhs_high, label);
4700 __ Sltu(TMP, lhs_high, TMP);
4701 __ LoadConst32(AT, imm_low);
4702 __ Sltu(AT, lhs_low, AT);
4703 __ Or(TMP, TMP, AT);
4704 __ Beqz(TMP, label);
4705 break;
4706 case kCondBE:
4707 __ LoadConst32(TMP, imm_high);
4708 __ Bltu(lhs_high, TMP, label);
4709 __ Sltu(TMP, TMP, lhs_high);
4710 __ LoadConst32(AT, imm_low);
4711 __ Sltu(AT, AT, lhs_low);
4712 __ Or(TMP, TMP, AT);
4713 __ Beqz(TMP, label);
4714 break;
4715 case kCondA:
4716 __ LoadConst32(TMP, imm_high);
4717 __ Bltu(TMP, lhs_high, label);
4718 __ Sltu(TMP, lhs_high, TMP);
4719 __ LoadConst32(AT, imm_low);
4720 __ Sltu(AT, AT, lhs_low);
4721 __ Blt(TMP, AT, label);
4722 break;
4723 }
4724 } else {
4725 switch (cond) {
4726 case kCondEQ:
4727 __ Xor(TMP, lhs_high, rhs_high);
4728 __ Xor(AT, lhs_low, rhs_low);
4729 __ Or(TMP, TMP, AT);
4730 __ Beqz(TMP, label);
4731 break;
4732 case kCondNE:
4733 __ Xor(TMP, lhs_high, rhs_high);
4734 __ Xor(AT, lhs_low, rhs_low);
4735 __ Or(TMP, TMP, AT);
4736 __ Bnez(TMP, label);
4737 break;
4738 case kCondLT:
4739 __ Blt(lhs_high, rhs_high, label);
4740 __ Slt(TMP, rhs_high, lhs_high);
4741 __ Sltu(AT, lhs_low, rhs_low);
4742 __ Blt(TMP, AT, label);
4743 break;
4744 case kCondGE:
4745 __ Blt(rhs_high, lhs_high, label);
4746 __ Slt(TMP, lhs_high, rhs_high);
4747 __ Sltu(AT, lhs_low, rhs_low);
4748 __ Or(TMP, TMP, AT);
4749 __ Beqz(TMP, label);
4750 break;
4751 case kCondLE:
4752 __ Blt(lhs_high, rhs_high, label);
4753 __ Slt(TMP, rhs_high, lhs_high);
4754 __ Sltu(AT, rhs_low, lhs_low);
4755 __ Or(TMP, TMP, AT);
4756 __ Beqz(TMP, label);
4757 break;
4758 case kCondGT:
4759 __ Blt(rhs_high, lhs_high, label);
4760 __ Slt(TMP, lhs_high, rhs_high);
4761 __ Sltu(AT, rhs_low, lhs_low);
4762 __ Blt(TMP, AT, label);
4763 break;
4764 case kCondB:
4765 __ Bltu(lhs_high, rhs_high, label);
4766 __ Sltu(TMP, rhs_high, lhs_high);
4767 __ Sltu(AT, lhs_low, rhs_low);
4768 __ Blt(TMP, AT, label);
4769 break;
4770 case kCondAE:
4771 __ Bltu(rhs_high, lhs_high, label);
4772 __ Sltu(TMP, lhs_high, rhs_high);
4773 __ Sltu(AT, lhs_low, rhs_low);
4774 __ Or(TMP, TMP, AT);
4775 __ Beqz(TMP, label);
4776 break;
4777 case kCondBE:
4778 __ Bltu(lhs_high, rhs_high, label);
4779 __ Sltu(TMP, rhs_high, lhs_high);
4780 __ Sltu(AT, rhs_low, lhs_low);
4781 __ Or(TMP, TMP, AT);
4782 __ Beqz(TMP, label);
4783 break;
4784 case kCondA:
4785 __ Bltu(rhs_high, lhs_high, label);
4786 __ Sltu(TMP, lhs_high, rhs_high);
4787 __ Sltu(AT, rhs_low, lhs_low);
4788 __ Blt(TMP, AT, label);
4789 break;
4790 }
4791 }
4792}
4793
Alexey Frunze2ddb7172016-09-06 17:04:55 -07004794void InstructionCodeGeneratorMIPS::GenerateFpCompare(IfCondition cond,
4795 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004796 DataType::Type type,
Alexey Frunze2ddb7172016-09-06 17:04:55 -07004797 LocationSummary* locations) {
4798 Register dst = locations->Out().AsRegister<Register>();
4799 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
4800 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
4801 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004802 if (type == DataType::Type::kFloat32) {
Alexey Frunze2ddb7172016-09-06 17:04:55 -07004803 if (isR6) {
4804 switch (cond) {
4805 case kCondEQ:
4806 __ CmpEqS(FTMP, lhs, rhs);
4807 __ Mfc1(dst, FTMP);
4808 __ Andi(dst, dst, 1);
4809 break;
4810 case kCondNE:
4811 __ CmpEqS(FTMP, lhs, rhs);
4812 __ Mfc1(dst, FTMP);
4813 __ Addiu(dst, dst, 1);
4814 break;
4815 case kCondLT:
4816 if (gt_bias) {
4817 __ CmpLtS(FTMP, lhs, rhs);
4818 } else {
4819 __ CmpUltS(FTMP, lhs, rhs);
4820 }
4821 __ Mfc1(dst, FTMP);
4822 __ Andi(dst, dst, 1);
4823 break;
4824 case kCondLE:
4825 if (gt_bias) {
4826 __ CmpLeS(FTMP, lhs, rhs);
4827 } else {
4828 __ CmpUleS(FTMP, lhs, rhs);
4829 }
4830 __ Mfc1(dst, FTMP);
4831 __ Andi(dst, dst, 1);
4832 break;
4833 case kCondGT:
4834 if (gt_bias) {
4835 __ CmpUltS(FTMP, rhs, lhs);
4836 } else {
4837 __ CmpLtS(FTMP, rhs, lhs);
4838 }
4839 __ Mfc1(dst, FTMP);
4840 __ Andi(dst, dst, 1);
4841 break;
4842 case kCondGE:
4843 if (gt_bias) {
4844 __ CmpUleS(FTMP, rhs, lhs);
4845 } else {
4846 __ CmpLeS(FTMP, rhs, lhs);
4847 }
4848 __ Mfc1(dst, FTMP);
4849 __ Andi(dst, dst, 1);
4850 break;
4851 default:
4852 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
4853 UNREACHABLE();
4854 }
4855 } else {
4856 switch (cond) {
4857 case kCondEQ:
4858 __ CeqS(0, lhs, rhs);
4859 __ LoadConst32(dst, 1);
4860 __ Movf(dst, ZERO, 0);
4861 break;
4862 case kCondNE:
4863 __ CeqS(0, lhs, rhs);
4864 __ LoadConst32(dst, 1);
4865 __ Movt(dst, ZERO, 0);
4866 break;
4867 case kCondLT:
4868 if (gt_bias) {
4869 __ ColtS(0, lhs, rhs);
4870 } else {
4871 __ CultS(0, lhs, rhs);
4872 }
4873 __ LoadConst32(dst, 1);
4874 __ Movf(dst, ZERO, 0);
4875 break;
4876 case kCondLE:
4877 if (gt_bias) {
4878 __ ColeS(0, lhs, rhs);
4879 } else {
4880 __ CuleS(0, lhs, rhs);
4881 }
4882 __ LoadConst32(dst, 1);
4883 __ Movf(dst, ZERO, 0);
4884 break;
4885 case kCondGT:
4886 if (gt_bias) {
4887 __ CultS(0, rhs, lhs);
4888 } else {
4889 __ ColtS(0, rhs, lhs);
4890 }
4891 __ LoadConst32(dst, 1);
4892 __ Movf(dst, ZERO, 0);
4893 break;
4894 case kCondGE:
4895 if (gt_bias) {
4896 __ CuleS(0, rhs, lhs);
4897 } else {
4898 __ ColeS(0, rhs, lhs);
4899 }
4900 __ LoadConst32(dst, 1);
4901 __ Movf(dst, ZERO, 0);
4902 break;
4903 default:
4904 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
4905 UNREACHABLE();
4906 }
4907 }
4908 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004909 DCHECK_EQ(type, DataType::Type::kFloat64);
Alexey Frunze2ddb7172016-09-06 17:04:55 -07004910 if (isR6) {
4911 switch (cond) {
4912 case kCondEQ:
4913 __ CmpEqD(FTMP, lhs, rhs);
4914 __ Mfc1(dst, FTMP);
4915 __ Andi(dst, dst, 1);
4916 break;
4917 case kCondNE:
4918 __ CmpEqD(FTMP, lhs, rhs);
4919 __ Mfc1(dst, FTMP);
4920 __ Addiu(dst, dst, 1);
4921 break;
4922 case kCondLT:
4923 if (gt_bias) {
4924 __ CmpLtD(FTMP, lhs, rhs);
4925 } else {
4926 __ CmpUltD(FTMP, lhs, rhs);
4927 }
4928 __ Mfc1(dst, FTMP);
4929 __ Andi(dst, dst, 1);
4930 break;
4931 case kCondLE:
4932 if (gt_bias) {
4933 __ CmpLeD(FTMP, lhs, rhs);
4934 } else {
4935 __ CmpUleD(FTMP, lhs, rhs);
4936 }
4937 __ Mfc1(dst, FTMP);
4938 __ Andi(dst, dst, 1);
4939 break;
4940 case kCondGT:
4941 if (gt_bias) {
4942 __ CmpUltD(FTMP, rhs, lhs);
4943 } else {
4944 __ CmpLtD(FTMP, rhs, lhs);
4945 }
4946 __ Mfc1(dst, FTMP);
4947 __ Andi(dst, dst, 1);
4948 break;
4949 case kCondGE:
4950 if (gt_bias) {
4951 __ CmpUleD(FTMP, rhs, lhs);
4952 } else {
4953 __ CmpLeD(FTMP, rhs, lhs);
4954 }
4955 __ Mfc1(dst, FTMP);
4956 __ Andi(dst, dst, 1);
4957 break;
4958 default:
4959 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
4960 UNREACHABLE();
4961 }
4962 } else {
4963 switch (cond) {
4964 case kCondEQ:
4965 __ CeqD(0, lhs, rhs);
4966 __ LoadConst32(dst, 1);
4967 __ Movf(dst, ZERO, 0);
4968 break;
4969 case kCondNE:
4970 __ CeqD(0, lhs, rhs);
4971 __ LoadConst32(dst, 1);
4972 __ Movt(dst, ZERO, 0);
4973 break;
4974 case kCondLT:
4975 if (gt_bias) {
4976 __ ColtD(0, lhs, rhs);
4977 } else {
4978 __ CultD(0, lhs, rhs);
4979 }
4980 __ LoadConst32(dst, 1);
4981 __ Movf(dst, ZERO, 0);
4982 break;
4983 case kCondLE:
4984 if (gt_bias) {
4985 __ ColeD(0, lhs, rhs);
4986 } else {
4987 __ CuleD(0, lhs, rhs);
4988 }
4989 __ LoadConst32(dst, 1);
4990 __ Movf(dst, ZERO, 0);
4991 break;
4992 case kCondGT:
4993 if (gt_bias) {
4994 __ CultD(0, rhs, lhs);
4995 } else {
4996 __ ColtD(0, rhs, lhs);
4997 }
4998 __ LoadConst32(dst, 1);
4999 __ Movf(dst, ZERO, 0);
5000 break;
5001 case kCondGE:
5002 if (gt_bias) {
5003 __ CuleD(0, rhs, lhs);
5004 } else {
5005 __ ColeD(0, rhs, lhs);
5006 }
5007 __ LoadConst32(dst, 1);
5008 __ Movf(dst, ZERO, 0);
5009 break;
5010 default:
5011 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
5012 UNREACHABLE();
5013 }
5014 }
5015 }
5016}
5017
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005018bool InstructionCodeGeneratorMIPS::MaterializeFpCompareR2(IfCondition cond,
5019 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005020 DataType::Type type,
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005021 LocationSummary* input_locations,
5022 int cc) {
5023 FRegister lhs = input_locations->InAt(0).AsFpuRegister<FRegister>();
5024 FRegister rhs = input_locations->InAt(1).AsFpuRegister<FRegister>();
5025 CHECK(!codegen_->GetInstructionSetFeatures().IsR6());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005026 if (type == DataType::Type::kFloat32) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005027 switch (cond) {
5028 case kCondEQ:
5029 __ CeqS(cc, lhs, rhs);
5030 return false;
5031 case kCondNE:
5032 __ CeqS(cc, lhs, rhs);
5033 return true;
5034 case kCondLT:
5035 if (gt_bias) {
5036 __ ColtS(cc, lhs, rhs);
5037 } else {
5038 __ CultS(cc, lhs, rhs);
5039 }
5040 return false;
5041 case kCondLE:
5042 if (gt_bias) {
5043 __ ColeS(cc, lhs, rhs);
5044 } else {
5045 __ CuleS(cc, lhs, rhs);
5046 }
5047 return false;
5048 case kCondGT:
5049 if (gt_bias) {
5050 __ CultS(cc, rhs, lhs);
5051 } else {
5052 __ ColtS(cc, rhs, lhs);
5053 }
5054 return false;
5055 case kCondGE:
5056 if (gt_bias) {
5057 __ CuleS(cc, rhs, lhs);
5058 } else {
5059 __ ColeS(cc, rhs, lhs);
5060 }
5061 return false;
5062 default:
5063 LOG(FATAL) << "Unexpected non-floating-point condition";
5064 UNREACHABLE();
5065 }
5066 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005067 DCHECK_EQ(type, DataType::Type::kFloat64);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005068 switch (cond) {
5069 case kCondEQ:
5070 __ CeqD(cc, lhs, rhs);
5071 return false;
5072 case kCondNE:
5073 __ CeqD(cc, lhs, rhs);
5074 return true;
5075 case kCondLT:
5076 if (gt_bias) {
5077 __ ColtD(cc, lhs, rhs);
5078 } else {
5079 __ CultD(cc, lhs, rhs);
5080 }
5081 return false;
5082 case kCondLE:
5083 if (gt_bias) {
5084 __ ColeD(cc, lhs, rhs);
5085 } else {
5086 __ CuleD(cc, lhs, rhs);
5087 }
5088 return false;
5089 case kCondGT:
5090 if (gt_bias) {
5091 __ CultD(cc, rhs, lhs);
5092 } else {
5093 __ ColtD(cc, rhs, lhs);
5094 }
5095 return false;
5096 case kCondGE:
5097 if (gt_bias) {
5098 __ CuleD(cc, rhs, lhs);
5099 } else {
5100 __ ColeD(cc, rhs, lhs);
5101 }
5102 return false;
5103 default:
5104 LOG(FATAL) << "Unexpected non-floating-point condition";
5105 UNREACHABLE();
5106 }
5107 }
5108}
5109
5110bool InstructionCodeGeneratorMIPS::MaterializeFpCompareR6(IfCondition cond,
5111 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005112 DataType::Type type,
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005113 LocationSummary* input_locations,
5114 FRegister dst) {
5115 FRegister lhs = input_locations->InAt(0).AsFpuRegister<FRegister>();
5116 FRegister rhs = input_locations->InAt(1).AsFpuRegister<FRegister>();
5117 CHECK(codegen_->GetInstructionSetFeatures().IsR6());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005118 if (type == DataType::Type::kFloat32) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005119 switch (cond) {
5120 case kCondEQ:
5121 __ CmpEqS(dst, lhs, rhs);
5122 return false;
5123 case kCondNE:
5124 __ CmpEqS(dst, lhs, rhs);
5125 return true;
5126 case kCondLT:
5127 if (gt_bias) {
5128 __ CmpLtS(dst, lhs, rhs);
5129 } else {
5130 __ CmpUltS(dst, lhs, rhs);
5131 }
5132 return false;
5133 case kCondLE:
5134 if (gt_bias) {
5135 __ CmpLeS(dst, lhs, rhs);
5136 } else {
5137 __ CmpUleS(dst, lhs, rhs);
5138 }
5139 return false;
5140 case kCondGT:
5141 if (gt_bias) {
5142 __ CmpUltS(dst, rhs, lhs);
5143 } else {
5144 __ CmpLtS(dst, rhs, lhs);
5145 }
5146 return false;
5147 case kCondGE:
5148 if (gt_bias) {
5149 __ CmpUleS(dst, rhs, lhs);
5150 } else {
5151 __ CmpLeS(dst, rhs, lhs);
5152 }
5153 return false;
5154 default:
5155 LOG(FATAL) << "Unexpected non-floating-point condition";
5156 UNREACHABLE();
5157 }
5158 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005159 DCHECK_EQ(type, DataType::Type::kFloat64);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005160 switch (cond) {
5161 case kCondEQ:
5162 __ CmpEqD(dst, lhs, rhs);
5163 return false;
5164 case kCondNE:
5165 __ CmpEqD(dst, lhs, rhs);
5166 return true;
5167 case kCondLT:
5168 if (gt_bias) {
5169 __ CmpLtD(dst, lhs, rhs);
5170 } else {
5171 __ CmpUltD(dst, lhs, rhs);
5172 }
5173 return false;
5174 case kCondLE:
5175 if (gt_bias) {
5176 __ CmpLeD(dst, lhs, rhs);
5177 } else {
5178 __ CmpUleD(dst, lhs, rhs);
5179 }
5180 return false;
5181 case kCondGT:
5182 if (gt_bias) {
5183 __ CmpUltD(dst, rhs, lhs);
5184 } else {
5185 __ CmpLtD(dst, rhs, lhs);
5186 }
5187 return false;
5188 case kCondGE:
5189 if (gt_bias) {
5190 __ CmpUleD(dst, rhs, lhs);
5191 } else {
5192 __ CmpLeD(dst, rhs, lhs);
5193 }
5194 return false;
5195 default:
5196 LOG(FATAL) << "Unexpected non-floating-point condition";
5197 UNREACHABLE();
5198 }
5199 }
5200}
5201
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005202void InstructionCodeGeneratorMIPS::GenerateFpCompareAndBranch(IfCondition cond,
5203 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005204 DataType::Type type,
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005205 LocationSummary* locations,
5206 MipsLabel* label) {
5207 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
5208 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
5209 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005210 if (type == DataType::Type::kFloat32) {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005211 if (isR6) {
5212 switch (cond) {
5213 case kCondEQ:
5214 __ CmpEqS(FTMP, lhs, rhs);
5215 __ Bc1nez(FTMP, label);
5216 break;
5217 case kCondNE:
5218 __ CmpEqS(FTMP, lhs, rhs);
5219 __ Bc1eqz(FTMP, label);
5220 break;
5221 case kCondLT:
5222 if (gt_bias) {
5223 __ CmpLtS(FTMP, lhs, rhs);
5224 } else {
5225 __ CmpUltS(FTMP, lhs, rhs);
5226 }
5227 __ Bc1nez(FTMP, label);
5228 break;
5229 case kCondLE:
5230 if (gt_bias) {
5231 __ CmpLeS(FTMP, lhs, rhs);
5232 } else {
5233 __ CmpUleS(FTMP, lhs, rhs);
5234 }
5235 __ Bc1nez(FTMP, label);
5236 break;
5237 case kCondGT:
5238 if (gt_bias) {
5239 __ CmpUltS(FTMP, rhs, lhs);
5240 } else {
5241 __ CmpLtS(FTMP, rhs, lhs);
5242 }
5243 __ Bc1nez(FTMP, label);
5244 break;
5245 case kCondGE:
5246 if (gt_bias) {
5247 __ CmpUleS(FTMP, rhs, lhs);
5248 } else {
5249 __ CmpLeS(FTMP, rhs, lhs);
5250 }
5251 __ Bc1nez(FTMP, label);
5252 break;
5253 default:
5254 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005255 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005256 }
5257 } else {
5258 switch (cond) {
5259 case kCondEQ:
5260 __ CeqS(0, lhs, rhs);
5261 __ Bc1t(0, label);
5262 break;
5263 case kCondNE:
5264 __ CeqS(0, lhs, rhs);
5265 __ Bc1f(0, label);
5266 break;
5267 case kCondLT:
5268 if (gt_bias) {
5269 __ ColtS(0, lhs, rhs);
5270 } else {
5271 __ CultS(0, lhs, rhs);
5272 }
5273 __ Bc1t(0, label);
5274 break;
5275 case kCondLE:
5276 if (gt_bias) {
5277 __ ColeS(0, lhs, rhs);
5278 } else {
5279 __ CuleS(0, lhs, rhs);
5280 }
5281 __ Bc1t(0, label);
5282 break;
5283 case kCondGT:
5284 if (gt_bias) {
5285 __ CultS(0, rhs, lhs);
5286 } else {
5287 __ ColtS(0, rhs, lhs);
5288 }
5289 __ Bc1t(0, label);
5290 break;
5291 case kCondGE:
5292 if (gt_bias) {
5293 __ CuleS(0, rhs, lhs);
5294 } else {
5295 __ ColeS(0, rhs, lhs);
5296 }
5297 __ Bc1t(0, label);
5298 break;
5299 default:
5300 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005301 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005302 }
5303 }
5304 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005305 DCHECK_EQ(type, DataType::Type::kFloat64);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005306 if (isR6) {
5307 switch (cond) {
5308 case kCondEQ:
5309 __ CmpEqD(FTMP, lhs, rhs);
5310 __ Bc1nez(FTMP, label);
5311 break;
5312 case kCondNE:
5313 __ CmpEqD(FTMP, lhs, rhs);
5314 __ Bc1eqz(FTMP, label);
5315 break;
5316 case kCondLT:
5317 if (gt_bias) {
5318 __ CmpLtD(FTMP, lhs, rhs);
5319 } else {
5320 __ CmpUltD(FTMP, lhs, rhs);
5321 }
5322 __ Bc1nez(FTMP, label);
5323 break;
5324 case kCondLE:
5325 if (gt_bias) {
5326 __ CmpLeD(FTMP, lhs, rhs);
5327 } else {
5328 __ CmpUleD(FTMP, lhs, rhs);
5329 }
5330 __ Bc1nez(FTMP, label);
5331 break;
5332 case kCondGT:
5333 if (gt_bias) {
5334 __ CmpUltD(FTMP, rhs, lhs);
5335 } else {
5336 __ CmpLtD(FTMP, rhs, lhs);
5337 }
5338 __ Bc1nez(FTMP, label);
5339 break;
5340 case kCondGE:
5341 if (gt_bias) {
5342 __ CmpUleD(FTMP, rhs, lhs);
5343 } else {
5344 __ CmpLeD(FTMP, rhs, lhs);
5345 }
5346 __ Bc1nez(FTMP, label);
5347 break;
5348 default:
5349 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005350 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005351 }
5352 } else {
5353 switch (cond) {
5354 case kCondEQ:
5355 __ CeqD(0, lhs, rhs);
5356 __ Bc1t(0, label);
5357 break;
5358 case kCondNE:
5359 __ CeqD(0, lhs, rhs);
5360 __ Bc1f(0, label);
5361 break;
5362 case kCondLT:
5363 if (gt_bias) {
5364 __ ColtD(0, lhs, rhs);
5365 } else {
5366 __ CultD(0, lhs, rhs);
5367 }
5368 __ Bc1t(0, label);
5369 break;
5370 case kCondLE:
5371 if (gt_bias) {
5372 __ ColeD(0, lhs, rhs);
5373 } else {
5374 __ CuleD(0, lhs, rhs);
5375 }
5376 __ Bc1t(0, label);
5377 break;
5378 case kCondGT:
5379 if (gt_bias) {
5380 __ CultD(0, rhs, lhs);
5381 } else {
5382 __ ColtD(0, rhs, lhs);
5383 }
5384 __ Bc1t(0, label);
5385 break;
5386 case kCondGE:
5387 if (gt_bias) {
5388 __ CuleD(0, rhs, lhs);
5389 } else {
5390 __ ColeD(0, rhs, lhs);
5391 }
5392 __ Bc1t(0, label);
5393 break;
5394 default:
5395 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005396 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005397 }
5398 }
5399 }
5400}
5401
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005402void InstructionCodeGeneratorMIPS::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00005403 size_t condition_input_index,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005404 MipsLabel* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00005405 MipsLabel* false_target) {
5406 HInstruction* cond = instruction->InputAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005407
David Brazdil0debae72015-11-12 18:37:00 +00005408 if (true_target == nullptr && false_target == nullptr) {
5409 // Nothing to do. The code always falls through.
5410 return;
5411 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00005412 // Constant condition, statically compared against "true" (integer value 1).
5413 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00005414 if (true_target != nullptr) {
5415 __ B(true_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005416 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005417 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00005418 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00005419 if (false_target != nullptr) {
5420 __ B(false_target);
5421 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005422 }
David Brazdil0debae72015-11-12 18:37:00 +00005423 return;
5424 }
5425
5426 // The following code generates these patterns:
5427 // (1) true_target == nullptr && false_target != nullptr
5428 // - opposite condition true => branch to false_target
5429 // (2) true_target != nullptr && false_target == nullptr
5430 // - condition true => branch to true_target
5431 // (3) true_target != nullptr && false_target != nullptr
5432 // - condition true => branch to true_target
5433 // - branch to false_target
5434 if (IsBooleanValueOrMaterializedCondition(cond)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005435 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00005436 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005437 DCHECK(cond_val.IsRegister());
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005438 if (true_target == nullptr) {
David Brazdil0debae72015-11-12 18:37:00 +00005439 __ Beqz(cond_val.AsRegister<Register>(), false_target);
5440 } else {
5441 __ Bnez(cond_val.AsRegister<Register>(), true_target);
5442 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005443 } else {
5444 // The condition instruction has not been materialized, use its inputs as
5445 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00005446 HCondition* condition = cond->AsCondition();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005447 DataType::Type type = condition->InputAt(0)->GetType();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005448 LocationSummary* locations = cond->GetLocations();
5449 IfCondition if_cond = condition->GetCondition();
5450 MipsLabel* branch_target = true_target;
David Brazdil0debae72015-11-12 18:37:00 +00005451
David Brazdil0debae72015-11-12 18:37:00 +00005452 if (true_target == nullptr) {
5453 if_cond = condition->GetOppositeCondition();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005454 branch_target = false_target;
David Brazdil0debae72015-11-12 18:37:00 +00005455 }
5456
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005457 switch (type) {
5458 default:
5459 GenerateIntCompareAndBranch(if_cond, locations, branch_target);
5460 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005461 case DataType::Type::kInt64:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005462 GenerateLongCompareAndBranch(if_cond, locations, branch_target);
5463 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005464 case DataType::Type::kFloat32:
5465 case DataType::Type::kFloat64:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005466 GenerateFpCompareAndBranch(if_cond, condition->IsGtBias(), type, locations, branch_target);
5467 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005468 }
5469 }
David Brazdil0debae72015-11-12 18:37:00 +00005470
5471 // If neither branch falls through (case 3), the conditional branch to `true_target`
5472 // was already emitted (case 2) and we need to emit a jump to `false_target`.
5473 if (true_target != nullptr && false_target != nullptr) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005474 __ B(false_target);
5475 }
5476}
5477
5478void LocationsBuilderMIPS::VisitIf(HIf* if_instr) {
5479 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00005480 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005481 locations->SetInAt(0, Location::RequiresRegister());
5482 }
5483}
5484
5485void InstructionCodeGeneratorMIPS::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00005486 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
5487 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
5488 MipsLabel* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
5489 nullptr : codegen_->GetLabelOf(true_successor);
5490 MipsLabel* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
5491 nullptr : codegen_->GetLabelOf(false_successor);
5492 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005493}
5494
5495void LocationsBuilderMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
5496 LocationSummary* locations = new (GetGraph()->GetArena())
5497 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01005498 InvokeRuntimeCallingConvention calling_convention;
5499 RegisterSet caller_saves = RegisterSet::Empty();
5500 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5501 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00005502 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005503 locations->SetInAt(0, Location::RequiresRegister());
5504 }
5505}
5506
5507void InstructionCodeGeneratorMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08005508 SlowPathCodeMIPS* slow_path =
5509 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathMIPS>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00005510 GenerateTestAndBranch(deoptimize,
5511 /* condition_input_index */ 0,
5512 slow_path->GetEntryLabel(),
5513 /* false_target */ nullptr);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005514}
5515
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005516// This function returns true if a conditional move can be generated for HSelect.
5517// Otherwise it returns false and HSelect must be implemented in terms of conditonal
5518// branches and regular moves.
5519//
5520// If `locations_to_set` isn't nullptr, its inputs and outputs are set for HSelect.
5521//
5522// While determining feasibility of a conditional move and setting inputs/outputs
5523// are two distinct tasks, this function does both because they share quite a bit
5524// of common logic.
5525static bool CanMoveConditionally(HSelect* select, bool is_r6, LocationSummary* locations_to_set) {
5526 bool materialized = IsBooleanValueOrMaterializedCondition(select->GetCondition());
5527 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
5528 HCondition* condition = cond->AsCondition();
5529
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005530 DataType::Type cond_type =
5531 materialized ? DataType::Type::kInt32 : condition->InputAt(0)->GetType();
5532 DataType::Type dst_type = select->GetType();
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005533
5534 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
5535 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
5536 bool is_true_value_zero_constant =
5537 (cst_true_value != nullptr && cst_true_value->IsZeroBitPattern());
5538 bool is_false_value_zero_constant =
5539 (cst_false_value != nullptr && cst_false_value->IsZeroBitPattern());
5540
5541 bool can_move_conditionally = false;
5542 bool use_const_for_false_in = false;
5543 bool use_const_for_true_in = false;
5544
5545 if (!cond->IsConstant()) {
5546 switch (cond_type) {
5547 default:
5548 switch (dst_type) {
5549 default:
5550 // Moving int on int condition.
5551 if (is_r6) {
5552 if (is_true_value_zero_constant) {
5553 // seleqz out_reg, false_reg, cond_reg
5554 can_move_conditionally = true;
5555 use_const_for_true_in = true;
5556 } else if (is_false_value_zero_constant) {
5557 // selnez out_reg, true_reg, cond_reg
5558 can_move_conditionally = true;
5559 use_const_for_false_in = true;
5560 } else if (materialized) {
5561 // Not materializing unmaterialized int conditions
5562 // to keep the instruction count low.
5563 // selnez AT, true_reg, cond_reg
5564 // seleqz TMP, false_reg, cond_reg
5565 // or out_reg, AT, TMP
5566 can_move_conditionally = true;
5567 }
5568 } else {
5569 // movn out_reg, true_reg/ZERO, cond_reg
5570 can_move_conditionally = true;
5571 use_const_for_true_in = is_true_value_zero_constant;
5572 }
5573 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005574 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005575 // Moving long on int condition.
5576 if (is_r6) {
5577 if (is_true_value_zero_constant) {
5578 // seleqz out_reg_lo, false_reg_lo, cond_reg
5579 // seleqz out_reg_hi, false_reg_hi, cond_reg
5580 can_move_conditionally = true;
5581 use_const_for_true_in = true;
5582 } else if (is_false_value_zero_constant) {
5583 // selnez out_reg_lo, true_reg_lo, cond_reg
5584 // selnez out_reg_hi, true_reg_hi, cond_reg
5585 can_move_conditionally = true;
5586 use_const_for_false_in = true;
5587 }
5588 // Other long conditional moves would generate 6+ instructions,
5589 // which is too many.
5590 } else {
5591 // movn out_reg_lo, true_reg_lo/ZERO, cond_reg
5592 // movn out_reg_hi, true_reg_hi/ZERO, cond_reg
5593 can_move_conditionally = true;
5594 use_const_for_true_in = is_true_value_zero_constant;
5595 }
5596 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005597 case DataType::Type::kFloat32:
5598 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005599 // Moving float/double on int condition.
5600 if (is_r6) {
5601 if (materialized) {
5602 // Not materializing unmaterialized int conditions
5603 // to keep the instruction count low.
5604 can_move_conditionally = true;
5605 if (is_true_value_zero_constant) {
5606 // sltu TMP, ZERO, cond_reg
5607 // mtc1 TMP, temp_cond_reg
5608 // seleqz.fmt out_reg, false_reg, temp_cond_reg
5609 use_const_for_true_in = true;
5610 } else if (is_false_value_zero_constant) {
5611 // sltu TMP, ZERO, cond_reg
5612 // mtc1 TMP, temp_cond_reg
5613 // selnez.fmt out_reg, true_reg, temp_cond_reg
5614 use_const_for_false_in = true;
5615 } else {
5616 // sltu TMP, ZERO, cond_reg
5617 // mtc1 TMP, temp_cond_reg
5618 // sel.fmt temp_cond_reg, false_reg, true_reg
5619 // mov.fmt out_reg, temp_cond_reg
5620 }
5621 }
5622 } else {
5623 // movn.fmt out_reg, true_reg, cond_reg
5624 can_move_conditionally = true;
5625 }
5626 break;
5627 }
5628 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005629 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005630 // We don't materialize long comparison now
5631 // and use conditional branches instead.
5632 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005633 case DataType::Type::kFloat32:
5634 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005635 switch (dst_type) {
5636 default:
5637 // Moving int on float/double condition.
5638 if (is_r6) {
5639 if (is_true_value_zero_constant) {
5640 // mfc1 TMP, temp_cond_reg
5641 // seleqz out_reg, false_reg, TMP
5642 can_move_conditionally = true;
5643 use_const_for_true_in = true;
5644 } else if (is_false_value_zero_constant) {
5645 // mfc1 TMP, temp_cond_reg
5646 // selnez out_reg, true_reg, TMP
5647 can_move_conditionally = true;
5648 use_const_for_false_in = true;
5649 } else {
5650 // mfc1 TMP, temp_cond_reg
5651 // selnez AT, true_reg, TMP
5652 // seleqz TMP, false_reg, TMP
5653 // or out_reg, AT, TMP
5654 can_move_conditionally = true;
5655 }
5656 } else {
5657 // movt out_reg, true_reg/ZERO, cc
5658 can_move_conditionally = true;
5659 use_const_for_true_in = is_true_value_zero_constant;
5660 }
5661 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005662 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005663 // Moving long on float/double condition.
5664 if (is_r6) {
5665 if (is_true_value_zero_constant) {
5666 // mfc1 TMP, temp_cond_reg
5667 // seleqz out_reg_lo, false_reg_lo, TMP
5668 // seleqz out_reg_hi, false_reg_hi, TMP
5669 can_move_conditionally = true;
5670 use_const_for_true_in = true;
5671 } else if (is_false_value_zero_constant) {
5672 // mfc1 TMP, temp_cond_reg
5673 // selnez out_reg_lo, true_reg_lo, TMP
5674 // selnez out_reg_hi, true_reg_hi, TMP
5675 can_move_conditionally = true;
5676 use_const_for_false_in = true;
5677 }
5678 // Other long conditional moves would generate 6+ instructions,
5679 // which is too many.
5680 } else {
5681 // movt out_reg_lo, true_reg_lo/ZERO, cc
5682 // movt out_reg_hi, true_reg_hi/ZERO, cc
5683 can_move_conditionally = true;
5684 use_const_for_true_in = is_true_value_zero_constant;
5685 }
5686 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005687 case DataType::Type::kFloat32:
5688 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005689 // Moving float/double on float/double condition.
5690 if (is_r6) {
5691 can_move_conditionally = true;
5692 if (is_true_value_zero_constant) {
5693 // seleqz.fmt out_reg, false_reg, temp_cond_reg
5694 use_const_for_true_in = true;
5695 } else if (is_false_value_zero_constant) {
5696 // selnez.fmt out_reg, true_reg, temp_cond_reg
5697 use_const_for_false_in = true;
5698 } else {
5699 // sel.fmt temp_cond_reg, false_reg, true_reg
5700 // mov.fmt out_reg, temp_cond_reg
5701 }
5702 } else {
5703 // movt.fmt out_reg, true_reg, cc
5704 can_move_conditionally = true;
5705 }
5706 break;
5707 }
5708 break;
5709 }
5710 }
5711
5712 if (can_move_conditionally) {
5713 DCHECK(!use_const_for_false_in || !use_const_for_true_in);
5714 } else {
5715 DCHECK(!use_const_for_false_in);
5716 DCHECK(!use_const_for_true_in);
5717 }
5718
5719 if (locations_to_set != nullptr) {
5720 if (use_const_for_false_in) {
5721 locations_to_set->SetInAt(0, Location::ConstantLocation(cst_false_value));
5722 } else {
5723 locations_to_set->SetInAt(0,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005724 DataType::IsFloatingPointType(dst_type)
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005725 ? Location::RequiresFpuRegister()
5726 : Location::RequiresRegister());
5727 }
5728 if (use_const_for_true_in) {
5729 locations_to_set->SetInAt(1, Location::ConstantLocation(cst_true_value));
5730 } else {
5731 locations_to_set->SetInAt(1,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005732 DataType::IsFloatingPointType(dst_type)
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005733 ? Location::RequiresFpuRegister()
5734 : Location::RequiresRegister());
5735 }
5736 if (materialized) {
5737 locations_to_set->SetInAt(2, Location::RequiresRegister());
5738 }
5739 // On R6 we don't require the output to be the same as the
5740 // first input for conditional moves unlike on R2.
5741 bool is_out_same_as_first_in = !can_move_conditionally || !is_r6;
5742 if (is_out_same_as_first_in) {
5743 locations_to_set->SetOut(Location::SameAsFirstInput());
5744 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005745 locations_to_set->SetOut(DataType::IsFloatingPointType(dst_type)
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005746 ? Location::RequiresFpuRegister()
5747 : Location::RequiresRegister());
5748 }
5749 }
5750
5751 return can_move_conditionally;
5752}
5753
5754void InstructionCodeGeneratorMIPS::GenConditionalMoveR2(HSelect* select) {
5755 LocationSummary* locations = select->GetLocations();
5756 Location dst = locations->Out();
5757 Location src = locations->InAt(1);
5758 Register src_reg = ZERO;
5759 Register src_reg_high = ZERO;
5760 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
5761 Register cond_reg = TMP;
5762 int cond_cc = 0;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005763 DataType::Type cond_type = DataType::Type::kInt32;
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005764 bool cond_inverted = false;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005765 DataType::Type dst_type = select->GetType();
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005766
5767 if (IsBooleanValueOrMaterializedCondition(cond)) {
5768 cond_reg = locations->InAt(/* condition_input_index */ 2).AsRegister<Register>();
5769 } else {
5770 HCondition* condition = cond->AsCondition();
5771 LocationSummary* cond_locations = cond->GetLocations();
5772 IfCondition if_cond = condition->GetCondition();
5773 cond_type = condition->InputAt(0)->GetType();
5774 switch (cond_type) {
5775 default:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005776 DCHECK_NE(cond_type, DataType::Type::kInt64);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005777 cond_inverted = MaterializeIntCompare(if_cond, cond_locations, cond_reg);
5778 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005779 case DataType::Type::kFloat32:
5780 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005781 cond_inverted = MaterializeFpCompareR2(if_cond,
5782 condition->IsGtBias(),
5783 cond_type,
5784 cond_locations,
5785 cond_cc);
5786 break;
5787 }
5788 }
5789
5790 DCHECK(dst.Equals(locations->InAt(0)));
5791 if (src.IsRegister()) {
5792 src_reg = src.AsRegister<Register>();
5793 } else if (src.IsRegisterPair()) {
5794 src_reg = src.AsRegisterPairLow<Register>();
5795 src_reg_high = src.AsRegisterPairHigh<Register>();
5796 } else if (src.IsConstant()) {
5797 DCHECK(src.GetConstant()->IsZeroBitPattern());
5798 }
5799
5800 switch (cond_type) {
5801 default:
5802 switch (dst_type) {
5803 default:
5804 if (cond_inverted) {
5805 __ Movz(dst.AsRegister<Register>(), src_reg, cond_reg);
5806 } else {
5807 __ Movn(dst.AsRegister<Register>(), src_reg, cond_reg);
5808 }
5809 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005810 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005811 if (cond_inverted) {
5812 __ Movz(dst.AsRegisterPairLow<Register>(), src_reg, cond_reg);
5813 __ Movz(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_reg);
5814 } else {
5815 __ Movn(dst.AsRegisterPairLow<Register>(), src_reg, cond_reg);
5816 __ Movn(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_reg);
5817 }
5818 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005819 case DataType::Type::kFloat32:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005820 if (cond_inverted) {
5821 __ MovzS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
5822 } else {
5823 __ MovnS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
5824 }
5825 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005826 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005827 if (cond_inverted) {
5828 __ MovzD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
5829 } else {
5830 __ MovnD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
5831 }
5832 break;
5833 }
5834 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005835 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005836 LOG(FATAL) << "Unreachable";
5837 UNREACHABLE();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005838 case DataType::Type::kFloat32:
5839 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005840 switch (dst_type) {
5841 default:
5842 if (cond_inverted) {
5843 __ Movf(dst.AsRegister<Register>(), src_reg, cond_cc);
5844 } else {
5845 __ Movt(dst.AsRegister<Register>(), src_reg, cond_cc);
5846 }
5847 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005848 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005849 if (cond_inverted) {
5850 __ Movf(dst.AsRegisterPairLow<Register>(), src_reg, cond_cc);
5851 __ Movf(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_cc);
5852 } else {
5853 __ Movt(dst.AsRegisterPairLow<Register>(), src_reg, cond_cc);
5854 __ Movt(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_cc);
5855 }
5856 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005857 case DataType::Type::kFloat32:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005858 if (cond_inverted) {
5859 __ MovfS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
5860 } else {
5861 __ MovtS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
5862 }
5863 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005864 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005865 if (cond_inverted) {
5866 __ MovfD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
5867 } else {
5868 __ MovtD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
5869 }
5870 break;
5871 }
5872 break;
5873 }
5874}
5875
5876void InstructionCodeGeneratorMIPS::GenConditionalMoveR6(HSelect* select) {
5877 LocationSummary* locations = select->GetLocations();
5878 Location dst = locations->Out();
5879 Location false_src = locations->InAt(0);
5880 Location true_src = locations->InAt(1);
5881 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
5882 Register cond_reg = TMP;
5883 FRegister fcond_reg = FTMP;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005884 DataType::Type cond_type = DataType::Type::kInt32;
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005885 bool cond_inverted = false;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005886 DataType::Type dst_type = select->GetType();
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005887
5888 if (IsBooleanValueOrMaterializedCondition(cond)) {
5889 cond_reg = locations->InAt(/* condition_input_index */ 2).AsRegister<Register>();
5890 } else {
5891 HCondition* condition = cond->AsCondition();
5892 LocationSummary* cond_locations = cond->GetLocations();
5893 IfCondition if_cond = condition->GetCondition();
5894 cond_type = condition->InputAt(0)->GetType();
5895 switch (cond_type) {
5896 default:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005897 DCHECK_NE(cond_type, DataType::Type::kInt64);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005898 cond_inverted = MaterializeIntCompare(if_cond, cond_locations, cond_reg);
5899 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005900 case DataType::Type::kFloat32:
5901 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005902 cond_inverted = MaterializeFpCompareR6(if_cond,
5903 condition->IsGtBias(),
5904 cond_type,
5905 cond_locations,
5906 fcond_reg);
5907 break;
5908 }
5909 }
5910
5911 if (true_src.IsConstant()) {
5912 DCHECK(true_src.GetConstant()->IsZeroBitPattern());
5913 }
5914 if (false_src.IsConstant()) {
5915 DCHECK(false_src.GetConstant()->IsZeroBitPattern());
5916 }
5917
5918 switch (dst_type) {
5919 default:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005920 if (DataType::IsFloatingPointType(cond_type)) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005921 __ Mfc1(cond_reg, fcond_reg);
5922 }
5923 if (true_src.IsConstant()) {
5924 if (cond_inverted) {
5925 __ Selnez(dst.AsRegister<Register>(), false_src.AsRegister<Register>(), cond_reg);
5926 } else {
5927 __ Seleqz(dst.AsRegister<Register>(), false_src.AsRegister<Register>(), cond_reg);
5928 }
5929 } else if (false_src.IsConstant()) {
5930 if (cond_inverted) {
5931 __ Seleqz(dst.AsRegister<Register>(), true_src.AsRegister<Register>(), cond_reg);
5932 } else {
5933 __ Selnez(dst.AsRegister<Register>(), true_src.AsRegister<Register>(), cond_reg);
5934 }
5935 } else {
5936 DCHECK_NE(cond_reg, AT);
5937 if (cond_inverted) {
5938 __ Seleqz(AT, true_src.AsRegister<Register>(), cond_reg);
5939 __ Selnez(TMP, false_src.AsRegister<Register>(), cond_reg);
5940 } else {
5941 __ Selnez(AT, true_src.AsRegister<Register>(), cond_reg);
5942 __ Seleqz(TMP, false_src.AsRegister<Register>(), cond_reg);
5943 }
5944 __ Or(dst.AsRegister<Register>(), AT, TMP);
5945 }
5946 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005947 case DataType::Type::kInt64: {
5948 if (DataType::IsFloatingPointType(cond_type)) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005949 __ Mfc1(cond_reg, fcond_reg);
5950 }
5951 Register dst_lo = dst.AsRegisterPairLow<Register>();
5952 Register dst_hi = dst.AsRegisterPairHigh<Register>();
5953 if (true_src.IsConstant()) {
5954 Register src_lo = false_src.AsRegisterPairLow<Register>();
5955 Register src_hi = false_src.AsRegisterPairHigh<Register>();
5956 if (cond_inverted) {
5957 __ Selnez(dst_lo, src_lo, cond_reg);
5958 __ Selnez(dst_hi, src_hi, cond_reg);
5959 } else {
5960 __ Seleqz(dst_lo, src_lo, cond_reg);
5961 __ Seleqz(dst_hi, src_hi, cond_reg);
5962 }
5963 } else {
5964 DCHECK(false_src.IsConstant());
5965 Register src_lo = true_src.AsRegisterPairLow<Register>();
5966 Register src_hi = true_src.AsRegisterPairHigh<Register>();
5967 if (cond_inverted) {
5968 __ Seleqz(dst_lo, src_lo, cond_reg);
5969 __ Seleqz(dst_hi, src_hi, cond_reg);
5970 } else {
5971 __ Selnez(dst_lo, src_lo, cond_reg);
5972 __ Selnez(dst_hi, src_hi, cond_reg);
5973 }
5974 }
5975 break;
5976 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005977 case DataType::Type::kFloat32: {
5978 if (!DataType::IsFloatingPointType(cond_type)) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005979 // sel*.fmt tests bit 0 of the condition register, account for that.
5980 __ Sltu(TMP, ZERO, cond_reg);
5981 __ Mtc1(TMP, fcond_reg);
5982 }
5983 FRegister dst_reg = dst.AsFpuRegister<FRegister>();
5984 if (true_src.IsConstant()) {
5985 FRegister src_reg = false_src.AsFpuRegister<FRegister>();
5986 if (cond_inverted) {
5987 __ SelnezS(dst_reg, src_reg, fcond_reg);
5988 } else {
5989 __ SeleqzS(dst_reg, src_reg, fcond_reg);
5990 }
5991 } else if (false_src.IsConstant()) {
5992 FRegister src_reg = true_src.AsFpuRegister<FRegister>();
5993 if (cond_inverted) {
5994 __ SeleqzS(dst_reg, src_reg, fcond_reg);
5995 } else {
5996 __ SelnezS(dst_reg, src_reg, fcond_reg);
5997 }
5998 } else {
5999 if (cond_inverted) {
6000 __ SelS(fcond_reg,
6001 true_src.AsFpuRegister<FRegister>(),
6002 false_src.AsFpuRegister<FRegister>());
6003 } else {
6004 __ SelS(fcond_reg,
6005 false_src.AsFpuRegister<FRegister>(),
6006 true_src.AsFpuRegister<FRegister>());
6007 }
6008 __ MovS(dst_reg, fcond_reg);
6009 }
6010 break;
6011 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006012 case DataType::Type::kFloat64: {
6013 if (!DataType::IsFloatingPointType(cond_type)) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006014 // sel*.fmt tests bit 0 of the condition register, account for that.
6015 __ Sltu(TMP, ZERO, cond_reg);
6016 __ Mtc1(TMP, fcond_reg);
6017 }
6018 FRegister dst_reg = dst.AsFpuRegister<FRegister>();
6019 if (true_src.IsConstant()) {
6020 FRegister src_reg = false_src.AsFpuRegister<FRegister>();
6021 if (cond_inverted) {
6022 __ SelnezD(dst_reg, src_reg, fcond_reg);
6023 } else {
6024 __ SeleqzD(dst_reg, src_reg, fcond_reg);
6025 }
6026 } else if (false_src.IsConstant()) {
6027 FRegister src_reg = true_src.AsFpuRegister<FRegister>();
6028 if (cond_inverted) {
6029 __ SeleqzD(dst_reg, src_reg, fcond_reg);
6030 } else {
6031 __ SelnezD(dst_reg, src_reg, fcond_reg);
6032 }
6033 } else {
6034 if (cond_inverted) {
6035 __ SelD(fcond_reg,
6036 true_src.AsFpuRegister<FRegister>(),
6037 false_src.AsFpuRegister<FRegister>());
6038 } else {
6039 __ SelD(fcond_reg,
6040 false_src.AsFpuRegister<FRegister>(),
6041 true_src.AsFpuRegister<FRegister>());
6042 }
6043 __ MovD(dst_reg, fcond_reg);
6044 }
6045 break;
6046 }
6047 }
6048}
6049
Goran Jakovljevicc6418422016-12-05 16:31:55 +01006050void LocationsBuilderMIPS::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
6051 LocationSummary* locations = new (GetGraph()->GetArena())
6052 LocationSummary(flag, LocationSummary::kNoCall);
6053 locations->SetOut(Location::RequiresRegister());
Mingyao Yang063fc772016-08-02 11:02:54 -07006054}
6055
Goran Jakovljevicc6418422016-12-05 16:31:55 +01006056void InstructionCodeGeneratorMIPS::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
6057 __ LoadFromOffset(kLoadWord,
6058 flag->GetLocations()->Out().AsRegister<Register>(),
6059 SP,
6060 codegen_->GetStackOffsetOfShouldDeoptimizeFlag());
Mingyao Yang063fc772016-08-02 11:02:54 -07006061}
6062
David Brazdil74eb1b22015-12-14 11:44:01 +00006063void LocationsBuilderMIPS::VisitSelect(HSelect* select) {
6064 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006065 CanMoveConditionally(select, codegen_->GetInstructionSetFeatures().IsR6(), locations);
David Brazdil74eb1b22015-12-14 11:44:01 +00006066}
6067
6068void InstructionCodeGeneratorMIPS::VisitSelect(HSelect* select) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006069 bool is_r6 = codegen_->GetInstructionSetFeatures().IsR6();
6070 if (CanMoveConditionally(select, is_r6, /* locations_to_set */ nullptr)) {
6071 if (is_r6) {
6072 GenConditionalMoveR6(select);
6073 } else {
6074 GenConditionalMoveR2(select);
6075 }
6076 } else {
6077 LocationSummary* locations = select->GetLocations();
6078 MipsLabel false_target;
6079 GenerateTestAndBranch(select,
6080 /* condition_input_index */ 2,
6081 /* true_target */ nullptr,
6082 &false_target);
6083 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
6084 __ Bind(&false_target);
6085 }
David Brazdil74eb1b22015-12-14 11:44:01 +00006086}
6087
David Srbecky0cf44932015-12-09 14:09:59 +00006088void LocationsBuilderMIPS::VisitNativeDebugInfo(HNativeDebugInfo* info) {
6089 new (GetGraph()->GetArena()) LocationSummary(info);
6090}
6091
David Srbeckyd28f4a02016-03-14 17:14:24 +00006092void InstructionCodeGeneratorMIPS::VisitNativeDebugInfo(HNativeDebugInfo*) {
6093 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00006094}
6095
6096void CodeGeneratorMIPS::GenerateNop() {
6097 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00006098}
6099
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006100void LocationsBuilderMIPS::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006101 DataType::Type field_type = field_info.GetFieldType();
6102 bool is_wide = (field_type == DataType::Type::kInt64) || (field_type == DataType::Type::kFloat64);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006103 bool generate_volatile = field_info.IsVolatile() && is_wide;
Alexey Frunze15958152017-02-09 19:08:30 -08006104 bool object_field_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006105 kEmitCompilerReadBarrier && (field_type == DataType::Type::kReference);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006106 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Alexey Frunze15958152017-02-09 19:08:30 -08006107 instruction,
6108 generate_volatile
6109 ? LocationSummary::kCallOnMainOnly
6110 : (object_field_get_with_read_barrier
6111 ? LocationSummary::kCallOnSlowPath
6112 : LocationSummary::kNoCall));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006113
Alexey Frunzec61c0762017-04-10 13:54:23 -07006114 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
6115 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
6116 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006117 locations->SetInAt(0, Location::RequiresRegister());
6118 if (generate_volatile) {
6119 InvokeRuntimeCallingConvention calling_convention;
6120 // need A0 to hold base + offset
6121 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006122 if (field_type == DataType::Type::kInt64) {
6123 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kInt64));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006124 } else {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006125 // Use Location::Any() to prevent situations when running out of available fp registers.
6126 locations->SetOut(Location::Any());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006127 // Need some temp core regs since FP results are returned in core registers
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006128 Location reg = calling_convention.GetReturnLocation(DataType::Type::kInt64);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006129 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairLow<Register>()));
6130 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairHigh<Register>()));
6131 }
6132 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006133 if (DataType::IsFloatingPointType(instruction->GetType())) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006134 locations->SetOut(Location::RequiresFpuRegister());
6135 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006136 // The output overlaps in the case of an object field get with
6137 // read barriers enabled: we do not want the move to overwrite the
6138 // object's location, as we need it to emit the read barrier.
6139 locations->SetOut(Location::RequiresRegister(),
6140 object_field_get_with_read_barrier
6141 ? Location::kOutputOverlap
6142 : Location::kNoOutputOverlap);
6143 }
6144 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
6145 // We need a temporary register for the read barrier marking slow
6146 // path in CodeGeneratorMIPS::GenerateFieldLoadWithBakerReadBarrier.
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006147 if (!kBakerReadBarrierThunksEnableForFields) {
6148 locations->AddTemp(Location::RequiresRegister());
6149 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006150 }
6151 }
6152}
6153
6154void InstructionCodeGeneratorMIPS::HandleFieldGet(HInstruction* instruction,
6155 const FieldInfo& field_info,
6156 uint32_t dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006157 DataType::Type type = field_info.GetFieldType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006158 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08006159 Location obj_loc = locations->InAt(0);
6160 Register obj = obj_loc.AsRegister<Register>();
6161 Location dst_loc = locations->Out();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006162 LoadOperandType load_type = kLoadUnsignedByte;
6163 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006164 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Tijana Jakovljevic57433862017-01-17 16:59:03 +01006165 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006166
6167 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006168 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006169 case DataType::Type::kUint8:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006170 load_type = kLoadUnsignedByte;
6171 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006172 case DataType::Type::kInt8:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006173 load_type = kLoadSignedByte;
6174 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006175 case DataType::Type::kUint16:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006176 load_type = kLoadUnsignedHalfword;
6177 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006178 case DataType::Type::kInt16:
6179 load_type = kLoadSignedHalfword;
6180 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006181 case DataType::Type::kInt32:
6182 case DataType::Type::kFloat32:
6183 case DataType::Type::kReference:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006184 load_type = kLoadWord;
6185 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006186 case DataType::Type::kInt64:
6187 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006188 load_type = kLoadDoubleword;
6189 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006190 case DataType::Type::kVoid:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006191 LOG(FATAL) << "Unreachable type " << type;
6192 UNREACHABLE();
6193 }
6194
6195 if (is_volatile && load_type == kLoadDoubleword) {
6196 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006197 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006198 // Do implicit Null check
6199 __ Lw(ZERO, locations->GetTemp(0).AsRegister<Register>(), 0);
6200 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Serban Constantinescufca16662016-07-14 09:21:59 +01006201 codegen_->InvokeRuntime(kQuickA64Load, instruction, dex_pc);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006202 CheckEntrypointTypes<kQuickA64Load, int64_t, volatile const int64_t*>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006203 if (type == DataType::Type::kFloat64) {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006204 // FP results are returned in core registers. Need to move them.
Alexey Frunze15958152017-02-09 19:08:30 -08006205 if (dst_loc.IsFpuRegister()) {
6206 __ Mtc1(locations->GetTemp(1).AsRegister<Register>(), dst_loc.AsFpuRegister<FRegister>());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006207 __ MoveToFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
Alexey Frunze15958152017-02-09 19:08:30 -08006208 dst_loc.AsFpuRegister<FRegister>());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006209 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006210 DCHECK(dst_loc.IsDoubleStackSlot());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006211 __ StoreToOffset(kStoreWord,
6212 locations->GetTemp(1).AsRegister<Register>(),
6213 SP,
Alexey Frunze15958152017-02-09 19:08:30 -08006214 dst_loc.GetStackIndex());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006215 __ StoreToOffset(kStoreWord,
6216 locations->GetTemp(2).AsRegister<Register>(),
6217 SP,
Alexey Frunze15958152017-02-09 19:08:30 -08006218 dst_loc.GetStackIndex() + 4);
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006219 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006220 }
6221 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006222 if (type == DataType::Type::kReference) {
Alexey Frunze15958152017-02-09 19:08:30 -08006223 // /* HeapReference<Object> */ dst = *(obj + offset)
6224 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006225 Location temp_loc =
6226 kBakerReadBarrierThunksEnableForFields ? Location::NoLocation() : locations->GetTemp(0);
Alexey Frunze15958152017-02-09 19:08:30 -08006227 // Note that a potential implicit null check is handled in this
6228 // CodeGeneratorMIPS::GenerateFieldLoadWithBakerReadBarrier call.
6229 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6230 dst_loc,
6231 obj,
6232 offset,
6233 temp_loc,
6234 /* needs_null_check */ true);
6235 if (is_volatile) {
6236 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6237 }
6238 } else {
6239 __ LoadFromOffset(kLoadWord, dst_loc.AsRegister<Register>(), obj, offset, null_checker);
6240 if (is_volatile) {
6241 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6242 }
6243 // If read barriers are enabled, emit read barriers other than
6244 // Baker's using a slow path (and also unpoison the loaded
6245 // reference, if heap poisoning is enabled).
6246 codegen_->MaybeGenerateReadBarrierSlow(instruction, dst_loc, dst_loc, obj_loc, offset);
6247 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006248 } else if (!DataType::IsFloatingPointType(type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006249 Register dst;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006250 if (type == DataType::Type::kInt64) {
Alexey Frunze15958152017-02-09 19:08:30 -08006251 DCHECK(dst_loc.IsRegisterPair());
6252 dst = dst_loc.AsRegisterPairLow<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006253 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006254 DCHECK(dst_loc.IsRegister());
6255 dst = dst_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006256 }
Alexey Frunze2923db72016-08-20 01:55:47 -07006257 __ LoadFromOffset(load_type, dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006258 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006259 DCHECK(dst_loc.IsFpuRegister());
6260 FRegister dst = dst_loc.AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006261 if (type == DataType::Type::kFloat32) {
Alexey Frunze2923db72016-08-20 01:55:47 -07006262 __ LoadSFromOffset(dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006263 } else {
Alexey Frunze2923db72016-08-20 01:55:47 -07006264 __ LoadDFromOffset(dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006265 }
6266 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006267 }
6268
Alexey Frunze15958152017-02-09 19:08:30 -08006269 // Memory barriers, in the case of references, are handled in the
6270 // previous switch statement.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006271 if (is_volatile && (type != DataType::Type::kReference)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006272 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6273 }
6274}
6275
6276void LocationsBuilderMIPS::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006277 DataType::Type field_type = field_info.GetFieldType();
6278 bool is_wide = (field_type == DataType::Type::kInt64) || (field_type == DataType::Type::kFloat64);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006279 bool generate_volatile = field_info.IsVolatile() && is_wide;
6280 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006281 instruction, generate_volatile ? LocationSummary::kCallOnMainOnly : LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006282
6283 locations->SetInAt(0, Location::RequiresRegister());
6284 if (generate_volatile) {
6285 InvokeRuntimeCallingConvention calling_convention;
6286 // need A0 to hold base + offset
6287 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006288 if (field_type == DataType::Type::kInt64) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006289 locations->SetInAt(1, Location::RegisterPairLocation(
6290 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
6291 } else {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006292 // Use Location::Any() to prevent situations when running out of available fp registers.
6293 locations->SetInAt(1, Location::Any());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006294 // Pass FP parameters in core registers.
6295 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
6296 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(3)));
6297 }
6298 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006299 if (DataType::IsFloatingPointType(field_type)) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006300 locations->SetInAt(1, FpuRegisterOrConstantForStore(instruction->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006301 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006302 locations->SetInAt(1, RegisterOrZeroConstant(instruction->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006303 }
6304 }
6305}
6306
6307void InstructionCodeGeneratorMIPS::HandleFieldSet(HInstruction* instruction,
6308 const FieldInfo& field_info,
Goran Jakovljevice114da22016-12-26 14:21:43 +01006309 uint32_t dex_pc,
6310 bool value_can_be_null) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006311 DataType::Type type = field_info.GetFieldType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006312 LocationSummary* locations = instruction->GetLocations();
6313 Register obj = locations->InAt(0).AsRegister<Register>();
Alexey Frunzef58b2482016-09-02 22:14:06 -07006314 Location value_location = locations->InAt(1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006315 StoreOperandType store_type = kStoreByte;
6316 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006317 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Alexey Frunzec061de12017-02-14 13:27:23 -08006318 bool needs_write_barrier = CodeGenerator::StoreNeedsWriteBarrier(type, instruction->InputAt(1));
Tijana Jakovljevic57433862017-01-17 16:59:03 +01006319 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006320
6321 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006322 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006323 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006324 case DataType::Type::kInt8:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006325 store_type = kStoreByte;
6326 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006327 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006328 case DataType::Type::kInt16:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006329 store_type = kStoreHalfword;
6330 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006331 case DataType::Type::kInt32:
6332 case DataType::Type::kFloat32:
6333 case DataType::Type::kReference:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006334 store_type = kStoreWord;
6335 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006336 case DataType::Type::kInt64:
6337 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006338 store_type = kStoreDoubleword;
6339 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006340 case DataType::Type::kVoid:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006341 LOG(FATAL) << "Unreachable type " << type;
6342 UNREACHABLE();
6343 }
6344
6345 if (is_volatile) {
6346 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
6347 }
6348
6349 if (is_volatile && store_type == kStoreDoubleword) {
6350 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006351 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006352 // Do implicit Null check.
6353 __ Lw(ZERO, locations->GetTemp(0).AsRegister<Register>(), 0);
6354 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006355 if (type == DataType::Type::kFloat64) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006356 // Pass FP parameters in core registers.
Alexey Frunzef58b2482016-09-02 22:14:06 -07006357 if (value_location.IsFpuRegister()) {
6358 __ Mfc1(locations->GetTemp(1).AsRegister<Register>(),
6359 value_location.AsFpuRegister<FRegister>());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006360 __ MoveFromFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
Alexey Frunzef58b2482016-09-02 22:14:06 -07006361 value_location.AsFpuRegister<FRegister>());
6362 } else if (value_location.IsDoubleStackSlot()) {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006363 __ LoadFromOffset(kLoadWord,
6364 locations->GetTemp(1).AsRegister<Register>(),
6365 SP,
Alexey Frunzef58b2482016-09-02 22:14:06 -07006366 value_location.GetStackIndex());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006367 __ LoadFromOffset(kLoadWord,
6368 locations->GetTemp(2).AsRegister<Register>(),
6369 SP,
Alexey Frunzef58b2482016-09-02 22:14:06 -07006370 value_location.GetStackIndex() + 4);
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006371 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006372 DCHECK(value_location.IsConstant());
6373 DCHECK(value_location.GetConstant()->IsDoubleConstant());
6374 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006375 __ LoadConst64(locations->GetTemp(2).AsRegister<Register>(),
6376 locations->GetTemp(1).AsRegister<Register>(),
6377 value);
6378 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006379 }
Serban Constantinescufca16662016-07-14 09:21:59 +01006380 codegen_->InvokeRuntime(kQuickA64Store, instruction, dex_pc);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006381 CheckEntrypointTypes<kQuickA64Store, void, volatile int64_t *, int64_t>();
6382 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006383 if (value_location.IsConstant()) {
6384 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
6385 __ StoreConstToOffset(store_type, value, obj, offset, TMP, null_checker);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006386 } else if (!DataType::IsFloatingPointType(type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006387 Register src;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006388 if (type == DataType::Type::kInt64) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006389 src = value_location.AsRegisterPairLow<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006390 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006391 src = value_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006392 }
Alexey Frunzec061de12017-02-14 13:27:23 -08006393 if (kPoisonHeapReferences && needs_write_barrier) {
6394 // Note that in the case where `value` is a null reference,
6395 // we do not enter this block, as a null reference does not
6396 // need poisoning.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006397 DCHECK_EQ(type, DataType::Type::kReference);
Alexey Frunzec061de12017-02-14 13:27:23 -08006398 __ PoisonHeapReference(TMP, src);
6399 __ StoreToOffset(store_type, TMP, obj, offset, null_checker);
6400 } else {
6401 __ StoreToOffset(store_type, src, obj, offset, null_checker);
6402 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006403 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006404 FRegister src = value_location.AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006405 if (type == DataType::Type::kFloat32) {
Alexey Frunze2923db72016-08-20 01:55:47 -07006406 __ StoreSToOffset(src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006407 } else {
Alexey Frunze2923db72016-08-20 01:55:47 -07006408 __ StoreDToOffset(src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006409 }
6410 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006411 }
6412
Alexey Frunzec061de12017-02-14 13:27:23 -08006413 if (needs_write_barrier) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006414 Register src = value_location.AsRegister<Register>();
Goran Jakovljevice114da22016-12-26 14:21:43 +01006415 codegen_->MarkGCCard(obj, src, value_can_be_null);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006416 }
6417
6418 if (is_volatile) {
6419 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
6420 }
6421}
6422
6423void LocationsBuilderMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
6424 HandleFieldGet(instruction, instruction->GetFieldInfo());
6425}
6426
6427void InstructionCodeGeneratorMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
6428 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
6429}
6430
6431void LocationsBuilderMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
6432 HandleFieldSet(instruction, instruction->GetFieldInfo());
6433}
6434
6435void InstructionCodeGeneratorMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Goran Jakovljevice114da22016-12-26 14:21:43 +01006436 HandleFieldSet(instruction,
6437 instruction->GetFieldInfo(),
6438 instruction->GetDexPc(),
6439 instruction->GetValueCanBeNull());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006440}
6441
Alexey Frunze15958152017-02-09 19:08:30 -08006442void InstructionCodeGeneratorMIPS::GenerateReferenceLoadOneRegister(
6443 HInstruction* instruction,
6444 Location out,
6445 uint32_t offset,
6446 Location maybe_temp,
6447 ReadBarrierOption read_barrier_option) {
6448 Register out_reg = out.AsRegister<Register>();
6449 if (read_barrier_option == kWithReadBarrier) {
6450 CHECK(kEmitCompilerReadBarrier);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006451 if (!kUseBakerReadBarrier || !kBakerReadBarrierThunksEnableForFields) {
6452 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
6453 }
Alexey Frunze15958152017-02-09 19:08:30 -08006454 if (kUseBakerReadBarrier) {
6455 // Load with fast path based Baker's read barrier.
6456 // /* HeapReference<Object> */ out = *(out + offset)
6457 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6458 out,
6459 out_reg,
6460 offset,
6461 maybe_temp,
6462 /* needs_null_check */ false);
6463 } else {
6464 // Load with slow path based read barrier.
6465 // Save the value of `out` into `maybe_temp` before overwriting it
6466 // in the following move operation, as we will need it for the
6467 // read barrier below.
6468 __ Move(maybe_temp.AsRegister<Register>(), out_reg);
6469 // /* HeapReference<Object> */ out = *(out + offset)
6470 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
6471 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
6472 }
6473 } else {
6474 // Plain load with no read barrier.
6475 // /* HeapReference<Object> */ out = *(out + offset)
6476 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
6477 __ MaybeUnpoisonHeapReference(out_reg);
6478 }
6479}
6480
6481void InstructionCodeGeneratorMIPS::GenerateReferenceLoadTwoRegisters(
6482 HInstruction* instruction,
6483 Location out,
6484 Location obj,
6485 uint32_t offset,
6486 Location maybe_temp,
6487 ReadBarrierOption read_barrier_option) {
6488 Register out_reg = out.AsRegister<Register>();
6489 Register obj_reg = obj.AsRegister<Register>();
6490 if (read_barrier_option == kWithReadBarrier) {
6491 CHECK(kEmitCompilerReadBarrier);
6492 if (kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006493 if (!kBakerReadBarrierThunksEnableForFields) {
6494 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
6495 }
Alexey Frunze15958152017-02-09 19:08:30 -08006496 // Load with fast path based Baker's read barrier.
6497 // /* HeapReference<Object> */ out = *(obj + offset)
6498 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6499 out,
6500 obj_reg,
6501 offset,
6502 maybe_temp,
6503 /* needs_null_check */ false);
6504 } else {
6505 // Load with slow path based read barrier.
6506 // /* HeapReference<Object> */ out = *(obj + offset)
6507 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6508 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6509 }
6510 } else {
6511 // Plain load with no read barrier.
6512 // /* HeapReference<Object> */ out = *(obj + offset)
6513 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6514 __ MaybeUnpoisonHeapReference(out_reg);
6515 }
6516}
6517
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006518static inline int GetBakerMarkThunkNumber(Register reg) {
6519 static_assert(BAKER_MARK_INTROSPECTION_REGISTER_COUNT == 21, "Expecting equal");
6520 if (reg >= V0 && reg <= T7) { // 14 consequtive regs.
6521 return reg - V0;
6522 } else if (reg >= S2 && reg <= S7) { // 6 consequtive regs.
6523 return 14 + (reg - S2);
6524 } else if (reg == FP) { // One more.
6525 return 20;
6526 }
6527 LOG(FATAL) << "Unexpected register " << reg;
6528 UNREACHABLE();
6529}
6530
6531static inline int GetBakerMarkFieldArrayThunkDisplacement(Register reg, bool short_offset) {
6532 int num = GetBakerMarkThunkNumber(reg) +
6533 (short_offset ? BAKER_MARK_INTROSPECTION_REGISTER_COUNT : 0);
6534 return num * BAKER_MARK_INTROSPECTION_FIELD_ARRAY_ENTRY_SIZE;
6535}
6536
6537static inline int GetBakerMarkGcRootThunkDisplacement(Register reg) {
6538 return GetBakerMarkThunkNumber(reg) * BAKER_MARK_INTROSPECTION_GC_ROOT_ENTRY_SIZE +
6539 BAKER_MARK_INTROSPECTION_GC_ROOT_ENTRIES_OFFSET;
6540}
6541
Alexey Frunze15958152017-02-09 19:08:30 -08006542void InstructionCodeGeneratorMIPS::GenerateGcRootFieldLoad(HInstruction* instruction,
6543 Location root,
6544 Register obj,
6545 uint32_t offset,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006546 ReadBarrierOption read_barrier_option,
6547 MipsLabel* label_low) {
6548 bool reordering;
6549 if (label_low != nullptr) {
6550 DCHECK_EQ(offset, 0x5678u);
6551 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07006552 Register root_reg = root.AsRegister<Register>();
Alexey Frunze15958152017-02-09 19:08:30 -08006553 if (read_barrier_option == kWithReadBarrier) {
6554 DCHECK(kEmitCompilerReadBarrier);
6555 if (kUseBakerReadBarrier) {
6556 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6557 // Baker's read barrier are used:
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006558 if (kBakerReadBarrierThunksEnableForGcRoots) {
6559 // Note that we do not actually check the value of `GetIsGcMarking()`
6560 // to decide whether to mark the loaded GC root or not. Instead, we
6561 // load into `temp` (T9) the read barrier mark introspection entrypoint.
6562 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
6563 // vice versa.
6564 //
6565 // We use thunks for the slow path. That thunk checks the reference
6566 // and jumps to the entrypoint if needed.
6567 //
6568 // temp = Thread::Current()->pReadBarrierMarkReg00
6569 // // AKA &art_quick_read_barrier_mark_introspection.
6570 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
6571 // if (temp != nullptr) {
6572 // temp = &gc_root_thunk<root_reg>
6573 // root = temp(root)
6574 // }
Alexey Frunze15958152017-02-09 19:08:30 -08006575
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006576 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
6577 const int32_t entry_point_offset =
6578 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(0);
6579 const int thunk_disp = GetBakerMarkGcRootThunkDisplacement(root_reg);
6580 int16_t offset_low = Low16Bits(offset);
6581 int16_t offset_high = High16Bits(offset - offset_low); // Accounts for sign
6582 // extension in lw.
6583 bool short_offset = IsInt<16>(static_cast<int32_t>(offset));
6584 Register base = short_offset ? obj : TMP;
6585 // Loading the entrypoint does not require a load acquire since it is only changed when
6586 // threads are suspended or running a checkpoint.
6587 __ LoadFromOffset(kLoadWord, T9, TR, entry_point_offset);
6588 reordering = __ SetReorder(false);
6589 if (!short_offset) {
6590 DCHECK(!label_low);
6591 __ AddUpper(base, obj, offset_high);
6592 }
Alexey Frunze0cab6562017-07-25 15:19:36 -07006593 MipsLabel skip_call;
6594 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006595 if (label_low != nullptr) {
6596 DCHECK(short_offset);
6597 __ Bind(label_low);
6598 }
6599 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6600 __ LoadFromOffset(kLoadWord, root_reg, base, offset_low); // Single instruction
6601 // in delay slot.
6602 if (isR6) {
6603 __ Jialc(T9, thunk_disp);
6604 } else {
6605 __ Addiu(T9, T9, thunk_disp);
6606 __ Jalr(T9);
6607 __ Nop();
6608 }
Alexey Frunze0cab6562017-07-25 15:19:36 -07006609 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006610 __ SetReorder(reordering);
6611 } else {
6612 // Note that we do not actually check the value of `GetIsGcMarking()`
6613 // to decide whether to mark the loaded GC root or not. Instead, we
6614 // load into `temp` (T9) the read barrier mark entry point corresponding
6615 // to register `root`. If `temp` is null, it means that `GetIsGcMarking()`
6616 // is false, and vice versa.
6617 //
6618 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
6619 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
6620 // if (temp != null) {
6621 // root = temp(root)
6622 // }
Alexey Frunze15958152017-02-09 19:08:30 -08006623
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006624 if (label_low != nullptr) {
6625 reordering = __ SetReorder(false);
6626 __ Bind(label_low);
6627 }
6628 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6629 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
6630 if (label_low != nullptr) {
6631 __ SetReorder(reordering);
6632 }
6633 static_assert(
6634 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6635 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6636 "have different sizes.");
6637 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6638 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6639 "have different sizes.");
Alexey Frunze15958152017-02-09 19:08:30 -08006640
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006641 // Slow path marking the GC root `root`.
6642 Location temp = Location::RegisterLocation(T9);
6643 SlowPathCodeMIPS* slow_path =
6644 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathMIPS(
6645 instruction,
6646 root,
6647 /*entrypoint*/ temp);
6648 codegen_->AddSlowPath(slow_path);
6649
6650 const int32_t entry_point_offset =
6651 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(root.reg() - 1);
6652 // Loading the entrypoint does not require a load acquire since it is only changed when
6653 // threads are suspended or running a checkpoint.
6654 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), TR, entry_point_offset);
6655 __ Bnez(temp.AsRegister<Register>(), slow_path->GetEntryLabel());
6656 __ Bind(slow_path->GetExitLabel());
6657 }
Alexey Frunze15958152017-02-09 19:08:30 -08006658 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006659 if (label_low != nullptr) {
6660 reordering = __ SetReorder(false);
6661 __ Bind(label_low);
6662 }
Alexey Frunze15958152017-02-09 19:08:30 -08006663 // GC root loaded through a slow path for read barriers other
6664 // than Baker's.
6665 // /* GcRoot<mirror::Object>* */ root = obj + offset
6666 __ Addiu32(root_reg, obj, offset);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006667 if (label_low != nullptr) {
6668 __ SetReorder(reordering);
6669 }
Alexey Frunze15958152017-02-09 19:08:30 -08006670 // /* mirror::Object* */ root = root->Read()
6671 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6672 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07006673 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006674 if (label_low != nullptr) {
6675 reordering = __ SetReorder(false);
6676 __ Bind(label_low);
6677 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07006678 // Plain GC root load with no read barrier.
6679 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6680 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
6681 // Note that GC roots are not affected by heap poisoning, thus we
6682 // do not have to unpoison `root_reg` here.
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006683 if (label_low != nullptr) {
6684 __ SetReorder(reordering);
6685 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07006686 }
6687}
6688
Alexey Frunze15958152017-02-09 19:08:30 -08006689void CodeGeneratorMIPS::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6690 Location ref,
6691 Register obj,
6692 uint32_t offset,
6693 Location temp,
6694 bool needs_null_check) {
6695 DCHECK(kEmitCompilerReadBarrier);
6696 DCHECK(kUseBakerReadBarrier);
6697
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006698 if (kBakerReadBarrierThunksEnableForFields) {
6699 // Note that we do not actually check the value of `GetIsGcMarking()`
6700 // to decide whether to mark the loaded reference or not. Instead, we
6701 // load into `temp` (T9) the read barrier mark introspection entrypoint.
6702 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
6703 // vice versa.
6704 //
6705 // We use thunks for the slow path. That thunk checks the reference
6706 // and jumps to the entrypoint if needed. If the holder is not gray,
6707 // it issues a load-load memory barrier and returns to the original
6708 // reference load.
6709 //
6710 // temp = Thread::Current()->pReadBarrierMarkReg00
6711 // // AKA &art_quick_read_barrier_mark_introspection.
6712 // if (temp != nullptr) {
6713 // temp = &field_array_thunk<holder_reg>
6714 // temp()
6715 // }
6716 // not_gray_return_address:
6717 // // If the offset is too large to fit into the lw instruction, we
6718 // // use an adjusted base register (TMP) here. This register
6719 // // receives bits 16 ... 31 of the offset before the thunk invocation
6720 // // and the thunk benefits from it.
6721 // HeapReference<mirror::Object> reference = *(obj+offset); // Original reference load.
6722 // gray_return_address:
6723
6724 DCHECK(temp.IsInvalid());
6725 bool isR6 = GetInstructionSetFeatures().IsR6();
6726 int16_t offset_low = Low16Bits(offset);
6727 int16_t offset_high = High16Bits(offset - offset_low); // Accounts for sign extension in lw.
6728 bool short_offset = IsInt<16>(static_cast<int32_t>(offset));
6729 bool reordering = __ SetReorder(false);
6730 const int32_t entry_point_offset =
6731 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(0);
6732 // There may have or may have not been a null check if the field offset is smaller than
6733 // the page size.
6734 // There must've been a null check in case it's actually a load from an array.
6735 // We will, however, perform an explicit null check in the thunk as it's easier to
6736 // do it than not.
6737 if (instruction->IsArrayGet()) {
6738 DCHECK(!needs_null_check);
6739 }
6740 const int thunk_disp = GetBakerMarkFieldArrayThunkDisplacement(obj, short_offset);
6741 // Loading the entrypoint does not require a load acquire since it is only changed when
6742 // threads are suspended or running a checkpoint.
6743 __ LoadFromOffset(kLoadWord, T9, TR, entry_point_offset);
6744 Register ref_reg = ref.AsRegister<Register>();
6745 Register base = short_offset ? obj : TMP;
Alexey Frunze0cab6562017-07-25 15:19:36 -07006746 MipsLabel skip_call;
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006747 if (short_offset) {
6748 if (isR6) {
Alexey Frunze0cab6562017-07-25 15:19:36 -07006749 __ Beqzc(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006750 __ Nop(); // In forbidden slot.
6751 __ Jialc(T9, thunk_disp);
6752 } else {
Alexey Frunze0cab6562017-07-25 15:19:36 -07006753 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006754 __ Addiu(T9, T9, thunk_disp); // In delay slot.
6755 __ Jalr(T9);
6756 __ Nop(); // In delay slot.
6757 }
Alexey Frunze0cab6562017-07-25 15:19:36 -07006758 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006759 } else {
6760 if (isR6) {
Alexey Frunze0cab6562017-07-25 15:19:36 -07006761 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006762 __ Aui(base, obj, offset_high); // In delay slot.
6763 __ Jialc(T9, thunk_disp);
Alexey Frunze0cab6562017-07-25 15:19:36 -07006764 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006765 } else {
6766 __ Lui(base, offset_high);
Alexey Frunze0cab6562017-07-25 15:19:36 -07006767 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006768 __ Addiu(T9, T9, thunk_disp); // In delay slot.
6769 __ Jalr(T9);
Alexey Frunze0cab6562017-07-25 15:19:36 -07006770 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006771 __ Addu(base, base, obj); // In delay slot.
6772 }
6773 }
6774 // /* HeapReference<Object> */ ref = *(obj + offset)
6775 __ LoadFromOffset(kLoadWord, ref_reg, base, offset_low); // Single instruction.
6776 if (needs_null_check) {
6777 MaybeRecordImplicitNullCheck(instruction);
6778 }
6779 __ MaybeUnpoisonHeapReference(ref_reg);
6780 __ SetReorder(reordering);
6781 return;
6782 }
6783
Alexey Frunze15958152017-02-09 19:08:30 -08006784 // /* HeapReference<Object> */ ref = *(obj + offset)
6785 Location no_index = Location::NoLocation();
6786 ScaleFactor no_scale_factor = TIMES_1;
6787 GenerateReferenceLoadWithBakerReadBarrier(instruction,
6788 ref,
6789 obj,
6790 offset,
6791 no_index,
6792 no_scale_factor,
6793 temp,
6794 needs_null_check);
6795}
6796
6797void CodeGeneratorMIPS::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6798 Location ref,
6799 Register obj,
6800 uint32_t data_offset,
6801 Location index,
6802 Location temp,
6803 bool needs_null_check) {
6804 DCHECK(kEmitCompilerReadBarrier);
6805 DCHECK(kUseBakerReadBarrier);
6806
6807 static_assert(
6808 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6809 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006810 ScaleFactor scale_factor = TIMES_4;
6811
6812 if (kBakerReadBarrierThunksEnableForArrays) {
6813 // Note that we do not actually check the value of `GetIsGcMarking()`
6814 // to decide whether to mark the loaded reference or not. Instead, we
6815 // load into `temp` (T9) the read barrier mark introspection entrypoint.
6816 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
6817 // vice versa.
6818 //
6819 // We use thunks for the slow path. That thunk checks the reference
6820 // and jumps to the entrypoint if needed. If the holder is not gray,
6821 // it issues a load-load memory barrier and returns to the original
6822 // reference load.
6823 //
6824 // temp = Thread::Current()->pReadBarrierMarkReg00
6825 // // AKA &art_quick_read_barrier_mark_introspection.
6826 // if (temp != nullptr) {
6827 // temp = &field_array_thunk<holder_reg>
6828 // temp()
6829 // }
6830 // not_gray_return_address:
6831 // // The element address is pre-calculated in the TMP register before the
6832 // // thunk invocation and the thunk benefits from it.
6833 // HeapReference<mirror::Object> reference = data[index]; // Original reference load.
6834 // gray_return_address:
6835
6836 DCHECK(temp.IsInvalid());
6837 DCHECK(index.IsValid());
6838 bool reordering = __ SetReorder(false);
6839 const int32_t entry_point_offset =
6840 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(0);
6841 // We will not do the explicit null check in the thunk as some form of a null check
6842 // must've been done earlier.
6843 DCHECK(!needs_null_check);
6844 const int thunk_disp = GetBakerMarkFieldArrayThunkDisplacement(obj, /* short_offset */ false);
6845 // Loading the entrypoint does not require a load acquire since it is only changed when
6846 // threads are suspended or running a checkpoint.
6847 __ LoadFromOffset(kLoadWord, T9, TR, entry_point_offset);
6848 Register ref_reg = ref.AsRegister<Register>();
6849 Register index_reg = index.IsRegisterPair()
6850 ? index.AsRegisterPairLow<Register>()
6851 : index.AsRegister<Register>();
Alexey Frunze0cab6562017-07-25 15:19:36 -07006852 MipsLabel skip_call;
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006853 if (GetInstructionSetFeatures().IsR6()) {
Alexey Frunze0cab6562017-07-25 15:19:36 -07006854 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006855 __ Lsa(TMP, index_reg, obj, scale_factor); // In delay slot.
6856 __ Jialc(T9, thunk_disp);
Alexey Frunze0cab6562017-07-25 15:19:36 -07006857 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006858 } else {
6859 __ Sll(TMP, index_reg, scale_factor);
Alexey Frunze0cab6562017-07-25 15:19:36 -07006860 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006861 __ Addiu(T9, T9, thunk_disp); // In delay slot.
6862 __ Jalr(T9);
Alexey Frunze0cab6562017-07-25 15:19:36 -07006863 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006864 __ Addu(TMP, TMP, obj); // In delay slot.
6865 }
6866 // /* HeapReference<Object> */ ref = *(obj + data_offset + (index << scale_factor))
6867 DCHECK(IsInt<16>(static_cast<int32_t>(data_offset))) << data_offset;
6868 __ LoadFromOffset(kLoadWord, ref_reg, TMP, data_offset); // Single instruction.
6869 __ MaybeUnpoisonHeapReference(ref_reg);
6870 __ SetReorder(reordering);
6871 return;
6872 }
6873
Alexey Frunze15958152017-02-09 19:08:30 -08006874 // /* HeapReference<Object> */ ref =
6875 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Alexey Frunze15958152017-02-09 19:08:30 -08006876 GenerateReferenceLoadWithBakerReadBarrier(instruction,
6877 ref,
6878 obj,
6879 data_offset,
6880 index,
6881 scale_factor,
6882 temp,
6883 needs_null_check);
6884}
6885
6886void CodeGeneratorMIPS::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6887 Location ref,
6888 Register obj,
6889 uint32_t offset,
6890 Location index,
6891 ScaleFactor scale_factor,
6892 Location temp,
6893 bool needs_null_check,
6894 bool always_update_field) {
6895 DCHECK(kEmitCompilerReadBarrier);
6896 DCHECK(kUseBakerReadBarrier);
6897
6898 // In slow path based read barriers, the read barrier call is
6899 // inserted after the original load. However, in fast path based
6900 // Baker's read barriers, we need to perform the load of
6901 // mirror::Object::monitor_ *before* the original reference load.
6902 // This load-load ordering is required by the read barrier.
6903 // The fast path/slow path (for Baker's algorithm) should look like:
6904 //
6905 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6906 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6907 // HeapReference<Object> ref = *src; // Original reference load.
6908 // bool is_gray = (rb_state == ReadBarrier::GrayState());
6909 // if (is_gray) {
6910 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
6911 // }
6912 //
6913 // Note: the original implementation in ReadBarrier::Barrier is
6914 // slightly more complex as it performs additional checks that we do
6915 // not do here for performance reasons.
6916
6917 Register ref_reg = ref.AsRegister<Register>();
6918 Register temp_reg = temp.AsRegister<Register>();
6919 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
6920
6921 // /* int32_t */ monitor = obj->monitor_
6922 __ LoadFromOffset(kLoadWord, temp_reg, obj, monitor_offset);
6923 if (needs_null_check) {
6924 MaybeRecordImplicitNullCheck(instruction);
6925 }
6926 // /* LockWord */ lock_word = LockWord(monitor)
6927 static_assert(sizeof(LockWord) == sizeof(int32_t),
6928 "art::LockWord and int32_t have different sizes.");
6929
6930 __ Sync(0); // Barrier to prevent load-load reordering.
6931
6932 // The actual reference load.
6933 if (index.IsValid()) {
6934 // Load types involving an "index": ArrayGet,
6935 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
6936 // intrinsics.
6937 // /* HeapReference<Object> */ ref = *(obj + offset + (index << scale_factor))
6938 if (index.IsConstant()) {
6939 size_t computed_offset =
6940 (index.GetConstant()->AsIntConstant()->GetValue() << scale_factor) + offset;
6941 __ LoadFromOffset(kLoadWord, ref_reg, obj, computed_offset);
6942 } else {
6943 // Handle the special case of the
6944 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
6945 // intrinsics, which use a register pair as index ("long
6946 // offset"), of which only the low part contains data.
6947 Register index_reg = index.IsRegisterPair()
6948 ? index.AsRegisterPairLow<Register>()
6949 : index.AsRegister<Register>();
Chris Larsencd0295d2017-03-31 15:26:54 -07006950 __ ShiftAndAdd(TMP, index_reg, obj, scale_factor, TMP);
Alexey Frunze15958152017-02-09 19:08:30 -08006951 __ LoadFromOffset(kLoadWord, ref_reg, TMP, offset);
6952 }
6953 } else {
6954 // /* HeapReference<Object> */ ref = *(obj + offset)
6955 __ LoadFromOffset(kLoadWord, ref_reg, obj, offset);
6956 }
6957
6958 // Object* ref = ref_addr->AsMirrorPtr()
6959 __ MaybeUnpoisonHeapReference(ref_reg);
6960
6961 // Slow path marking the object `ref` when it is gray.
6962 SlowPathCodeMIPS* slow_path;
6963 if (always_update_field) {
6964 // ReadBarrierMarkAndUpdateFieldSlowPathMIPS only supports address
6965 // of the form `obj + field_offset`, where `obj` is a register and
6966 // `field_offset` is a register pair (of which only the lower half
6967 // is used). Thus `offset` and `scale_factor` above are expected
6968 // to be null in this code path.
6969 DCHECK_EQ(offset, 0u);
6970 DCHECK_EQ(scale_factor, ScaleFactor::TIMES_1);
6971 slow_path = new (GetGraph()->GetArena())
6972 ReadBarrierMarkAndUpdateFieldSlowPathMIPS(instruction,
6973 ref,
6974 obj,
6975 /* field_offset */ index,
6976 temp_reg);
6977 } else {
6978 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathMIPS(instruction, ref);
6979 }
6980 AddSlowPath(slow_path);
6981
6982 // if (rb_state == ReadBarrier::GrayState())
6983 // ref = ReadBarrier::Mark(ref);
6984 // Given the numeric representation, it's enough to check the low bit of the
6985 // rb_state. We do that by shifting the bit into the sign bit (31) and
6986 // performing a branch on less than zero.
6987 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
6988 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
6989 static_assert(LockWord::kReadBarrierStateSize == 1, "Expecting 1-bit read barrier state size");
6990 __ Sll(temp_reg, temp_reg, 31 - LockWord::kReadBarrierStateShift);
6991 __ Bltz(temp_reg, slow_path->GetEntryLabel());
6992 __ Bind(slow_path->GetExitLabel());
6993}
6994
6995void CodeGeneratorMIPS::GenerateReadBarrierSlow(HInstruction* instruction,
6996 Location out,
6997 Location ref,
6998 Location obj,
6999 uint32_t offset,
7000 Location index) {
7001 DCHECK(kEmitCompilerReadBarrier);
7002
7003 // Insert a slow path based read barrier *after* the reference load.
7004 //
7005 // If heap poisoning is enabled, the unpoisoning of the loaded
7006 // reference will be carried out by the runtime within the slow
7007 // path.
7008 //
7009 // Note that `ref` currently does not get unpoisoned (when heap
7010 // poisoning is enabled), which is alright as the `ref` argument is
7011 // not used by the artReadBarrierSlow entry point.
7012 //
7013 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
7014 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena())
7015 ReadBarrierForHeapReferenceSlowPathMIPS(instruction, out, ref, obj, offset, index);
7016 AddSlowPath(slow_path);
7017
7018 __ B(slow_path->GetEntryLabel());
7019 __ Bind(slow_path->GetExitLabel());
7020}
7021
7022void CodeGeneratorMIPS::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
7023 Location out,
7024 Location ref,
7025 Location obj,
7026 uint32_t offset,
7027 Location index) {
7028 if (kEmitCompilerReadBarrier) {
7029 // Baker's read barriers shall be handled by the fast path
7030 // (CodeGeneratorMIPS::GenerateReferenceLoadWithBakerReadBarrier).
7031 DCHECK(!kUseBakerReadBarrier);
7032 // If heap poisoning is enabled, unpoisoning will be taken care of
7033 // by the runtime within the slow path.
7034 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
7035 } else if (kPoisonHeapReferences) {
7036 __ UnpoisonHeapReference(out.AsRegister<Register>());
7037 }
7038}
7039
7040void CodeGeneratorMIPS::GenerateReadBarrierForRootSlow(HInstruction* instruction,
7041 Location out,
7042 Location root) {
7043 DCHECK(kEmitCompilerReadBarrier);
7044
7045 // Insert a slow path based read barrier *after* the GC root load.
7046 //
7047 // Note that GC roots are not affected by heap poisoning, so we do
7048 // not need to do anything special for this here.
7049 SlowPathCodeMIPS* slow_path =
7050 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathMIPS(instruction, out, root);
7051 AddSlowPath(slow_path);
7052
7053 __ B(slow_path->GetEntryLabel());
7054 __ Bind(slow_path->GetExitLabel());
7055}
7056
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007057void LocationsBuilderMIPS::VisitInstanceOf(HInstanceOf* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007058 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
7059 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexey Frunzec61c0762017-04-10 13:54:23 -07007060 bool baker_read_barrier_slow_path = false;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007061 switch (type_check_kind) {
7062 case TypeCheckKind::kExactCheck:
7063 case TypeCheckKind::kAbstractClassCheck:
7064 case TypeCheckKind::kClassHierarchyCheck:
7065 case TypeCheckKind::kArrayObjectCheck:
Alexey Frunze15958152017-02-09 19:08:30 -08007066 call_kind =
7067 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Alexey Frunzec61c0762017-04-10 13:54:23 -07007068 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007069 break;
7070 case TypeCheckKind::kArrayCheck:
7071 case TypeCheckKind::kUnresolvedCheck:
7072 case TypeCheckKind::kInterfaceCheck:
7073 call_kind = LocationSummary::kCallOnSlowPath;
7074 break;
7075 }
7076
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007077 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexey Frunzec61c0762017-04-10 13:54:23 -07007078 if (baker_read_barrier_slow_path) {
7079 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
7080 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007081 locations->SetInAt(0, Location::RequiresRegister());
7082 locations->SetInAt(1, Location::RequiresRegister());
7083 // The output does overlap inputs.
7084 // Note that TypeCheckSlowPathMIPS uses this register too.
7085 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Alexey Frunze15958152017-02-09 19:08:30 -08007086 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007087}
7088
7089void InstructionCodeGeneratorMIPS::VisitInstanceOf(HInstanceOf* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007090 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007091 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08007092 Location obj_loc = locations->InAt(0);
7093 Register obj = obj_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007094 Register cls = locations->InAt(1).AsRegister<Register>();
Alexey Frunze15958152017-02-09 19:08:30 -08007095 Location out_loc = locations->Out();
7096 Register out = out_loc.AsRegister<Register>();
7097 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
7098 DCHECK_LE(num_temps, 1u);
7099 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007100 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
7101 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
7102 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
7103 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007104 MipsLabel done;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007105 SlowPathCodeMIPS* slow_path = nullptr;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007106
7107 // Return 0 if `obj` is null.
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007108 // Avoid this check if we know `obj` is not null.
7109 if (instruction->MustDoNullCheck()) {
7110 __ Move(out, ZERO);
7111 __ Beqz(obj, &done);
7112 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007113
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007114 switch (type_check_kind) {
7115 case TypeCheckKind::kExactCheck: {
7116 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007117 GenerateReferenceLoadTwoRegisters(instruction,
7118 out_loc,
7119 obj_loc,
7120 class_offset,
7121 maybe_temp_loc,
7122 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007123 // Classes must be equal for the instanceof to succeed.
7124 __ Xor(out, out, cls);
7125 __ Sltiu(out, out, 1);
7126 break;
7127 }
7128
7129 case TypeCheckKind::kAbstractClassCheck: {
7130 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007131 GenerateReferenceLoadTwoRegisters(instruction,
7132 out_loc,
7133 obj_loc,
7134 class_offset,
7135 maybe_temp_loc,
7136 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007137 // If the class is abstract, we eagerly fetch the super class of the
7138 // object to avoid doing a comparison we know will fail.
7139 MipsLabel loop;
7140 __ Bind(&loop);
7141 // /* HeapReference<Class> */ out = out->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08007142 GenerateReferenceLoadOneRegister(instruction,
7143 out_loc,
7144 super_offset,
7145 maybe_temp_loc,
7146 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007147 // If `out` is null, we use it for the result, and jump to `done`.
7148 __ Beqz(out, &done);
7149 __ Bne(out, cls, &loop);
7150 __ LoadConst32(out, 1);
7151 break;
7152 }
7153
7154 case TypeCheckKind::kClassHierarchyCheck: {
7155 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007156 GenerateReferenceLoadTwoRegisters(instruction,
7157 out_loc,
7158 obj_loc,
7159 class_offset,
7160 maybe_temp_loc,
7161 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007162 // Walk over the class hierarchy to find a match.
7163 MipsLabel loop, success;
7164 __ Bind(&loop);
7165 __ Beq(out, cls, &success);
7166 // /* HeapReference<Class> */ out = out->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08007167 GenerateReferenceLoadOneRegister(instruction,
7168 out_loc,
7169 super_offset,
7170 maybe_temp_loc,
7171 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007172 __ Bnez(out, &loop);
7173 // If `out` is null, we use it for the result, and jump to `done`.
7174 __ B(&done);
7175 __ Bind(&success);
7176 __ LoadConst32(out, 1);
7177 break;
7178 }
7179
7180 case TypeCheckKind::kArrayObjectCheck: {
7181 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007182 GenerateReferenceLoadTwoRegisters(instruction,
7183 out_loc,
7184 obj_loc,
7185 class_offset,
7186 maybe_temp_loc,
7187 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007188 // Do an exact check.
7189 MipsLabel success;
7190 __ Beq(out, cls, &success);
7191 // Otherwise, we need to check that the object's class is a non-primitive array.
7192 // /* HeapReference<Class> */ out = out->component_type_
Alexey Frunze15958152017-02-09 19:08:30 -08007193 GenerateReferenceLoadOneRegister(instruction,
7194 out_loc,
7195 component_offset,
7196 maybe_temp_loc,
7197 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007198 // If `out` is null, we use it for the result, and jump to `done`.
7199 __ Beqz(out, &done);
7200 __ LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
7201 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
7202 __ Sltiu(out, out, 1);
7203 __ B(&done);
7204 __ Bind(&success);
7205 __ LoadConst32(out, 1);
7206 break;
7207 }
7208
7209 case TypeCheckKind::kArrayCheck: {
7210 // No read barrier since the slow path will retry upon failure.
7211 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007212 GenerateReferenceLoadTwoRegisters(instruction,
7213 out_loc,
7214 obj_loc,
7215 class_offset,
7216 maybe_temp_loc,
7217 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007218 DCHECK(locations->OnlyCallsOnSlowPath());
7219 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS(instruction,
7220 /* is_fatal */ false);
7221 codegen_->AddSlowPath(slow_path);
7222 __ Bne(out, cls, slow_path->GetEntryLabel());
7223 __ LoadConst32(out, 1);
7224 break;
7225 }
7226
7227 case TypeCheckKind::kUnresolvedCheck:
7228 case TypeCheckKind::kInterfaceCheck: {
7229 // Note that we indeed only call on slow path, but we always go
7230 // into the slow path for the unresolved and interface check
7231 // cases.
7232 //
7233 // We cannot directly call the InstanceofNonTrivial runtime
7234 // entry point without resorting to a type checking slow path
7235 // here (i.e. by calling InvokeRuntime directly), as it would
7236 // require to assign fixed registers for the inputs of this
7237 // HInstanceOf instruction (following the runtime calling
7238 // convention), which might be cluttered by the potential first
7239 // read barrier emission at the beginning of this method.
7240 //
7241 // TODO: Introduce a new runtime entry point taking the object
7242 // to test (instead of its class) as argument, and let it deal
7243 // with the read barrier issues. This will let us refactor this
7244 // case of the `switch` code as it was previously (with a direct
7245 // call to the runtime not using a type checking slow path).
7246 // This should also be beneficial for the other cases above.
7247 DCHECK(locations->OnlyCallsOnSlowPath());
7248 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS(instruction,
7249 /* is_fatal */ false);
7250 codegen_->AddSlowPath(slow_path);
7251 __ B(slow_path->GetEntryLabel());
7252 break;
7253 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007254 }
7255
7256 __ Bind(&done);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007257
7258 if (slow_path != nullptr) {
7259 __ Bind(slow_path->GetExitLabel());
7260 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007261}
7262
7263void LocationsBuilderMIPS::VisitIntConstant(HIntConstant* constant) {
7264 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
7265 locations->SetOut(Location::ConstantLocation(constant));
7266}
7267
7268void InstructionCodeGeneratorMIPS::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
7269 // Will be generated at use site.
7270}
7271
7272void LocationsBuilderMIPS::VisitNullConstant(HNullConstant* constant) {
7273 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
7274 locations->SetOut(Location::ConstantLocation(constant));
7275}
7276
7277void InstructionCodeGeneratorMIPS::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
7278 // Will be generated at use site.
7279}
7280
7281void LocationsBuilderMIPS::HandleInvoke(HInvoke* invoke) {
7282 InvokeDexCallingConventionVisitorMIPS calling_convention_visitor;
7283 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
7284}
7285
7286void LocationsBuilderMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
7287 HandleInvoke(invoke);
Alexey Frunze1b8464d2016-11-12 17:22:05 -08007288 // The register T7 is required to be used for the hidden argument in
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007289 // art_quick_imt_conflict_trampoline, so add the hidden argument.
Alexey Frunze1b8464d2016-11-12 17:22:05 -08007290 invoke->GetLocations()->AddTemp(Location::RegisterLocation(T7));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007291}
7292
7293void InstructionCodeGeneratorMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
7294 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
7295 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007296 Location receiver = invoke->GetLocations()->InAt(0);
7297 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07007298 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007299
7300 // Set the hidden argument.
7301 __ LoadConst32(invoke->GetLocations()->GetTemp(1).AsRegister<Register>(),
7302 invoke->GetDexMethodIndex());
7303
7304 // temp = object->GetClass();
7305 if (receiver.IsStackSlot()) {
7306 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
7307 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
7308 } else {
7309 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
7310 }
7311 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08007312 // Instead of simply (possibly) unpoisoning `temp` here, we should
7313 // emit a read barrier for the previous class reference load.
7314 // However this is not required in practice, as this is an
7315 // intermediate/temporary reference and because the current
7316 // concurrent copying collector keeps the from-space memory
7317 // intact/accessible until the end of the marking phase (the
7318 // concurrent copying collector may not in the future).
7319 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00007320 __ LoadFromOffset(kLoadWord, temp, temp,
7321 mirror::Class::ImtPtrOffset(kMipsPointerSize).Uint32Value());
7322 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00007323 invoke->GetImtIndex(), kMipsPointerSize));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007324 // temp = temp->GetImtEntryAt(method_offset);
7325 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
7326 // T9 = temp->GetEntryPoint();
7327 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
7328 // T9();
7329 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07007330 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007331 DCHECK(!codegen_->IsLeafMethod());
7332 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
7333}
7334
7335void LocationsBuilderMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen701566a2015-10-27 15:29:13 -07007336 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
7337 if (intrinsic.TryDispatch(invoke)) {
7338 return;
7339 }
7340
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007341 HandleInvoke(invoke);
7342}
7343
7344void LocationsBuilderMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00007345 // Explicit clinit checks triggered by static invokes must have been pruned by
7346 // art::PrepareForRegisterAllocation.
7347 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007348
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007349 bool is_r6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007350 bool has_irreducible_loops = codegen_->GetGraph()->HasIrreducibleLoops();
7351 bool has_extra_input = invoke->HasPcRelativeMethodLoadKind() && !is_r6 && !has_irreducible_loops;
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007352
Chris Larsen701566a2015-10-27 15:29:13 -07007353 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
7354 if (intrinsic.TryDispatch(invoke)) {
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007355 if (invoke->GetLocations()->CanCall() && has_extra_input) {
7356 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
7357 }
Chris Larsen701566a2015-10-27 15:29:13 -07007358 return;
7359 }
7360
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007361 HandleInvoke(invoke);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007362
7363 // Add the extra input register if either the dex cache array base register
7364 // or the PC-relative base register for accessing literals is needed.
7365 if (has_extra_input) {
7366 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
7367 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007368}
7369
Orion Hodsonac141392017-01-13 11:53:47 +00007370void LocationsBuilderMIPS::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
7371 HandleInvoke(invoke);
7372}
7373
7374void InstructionCodeGeneratorMIPS::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
7375 codegen_->GenerateInvokePolymorphicCall(invoke);
7376}
7377
Chris Larsen701566a2015-10-27 15:29:13 -07007378static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorMIPS* codegen) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007379 if (invoke->GetLocations()->Intrinsified()) {
Chris Larsen701566a2015-10-27 15:29:13 -07007380 IntrinsicCodeGeneratorMIPS intrinsic(codegen);
7381 intrinsic.Dispatch(invoke);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007382 return true;
7383 }
7384 return false;
7385}
7386
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007387HLoadString::LoadKind CodeGeneratorMIPS::GetSupportedLoadStringKind(
Alexey Frunze06a46c42016-07-19 15:00:40 -07007388 HLoadString::LoadKind desired_string_load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007389 switch (desired_string_load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007390 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007391 case HLoadString::LoadKind::kBootImageInternTable:
Vladimir Markoaad75c62016-10-03 08:46:48 +00007392 case HLoadString::LoadKind::kBssEntry:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007393 DCHECK(!Runtime::Current()->UseJitCompilation());
Alexey Frunze06a46c42016-07-19 15:00:40 -07007394 break;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007395 case HLoadString::LoadKind::kJitTableAddress:
7396 DCHECK(Runtime::Current()->UseJitCompilation());
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007397 break;
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007398 case HLoadString::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007399 case HLoadString::LoadKind::kRuntimeCall:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007400 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007401 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007402 return desired_string_load_kind;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007403}
7404
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007405HLoadClass::LoadKind CodeGeneratorMIPS::GetSupportedLoadClassKind(
7406 HLoadClass::LoadKind desired_class_load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007407 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00007408 case HLoadClass::LoadKind::kInvalid:
7409 LOG(FATAL) << "UNREACHABLE";
7410 UNREACHABLE();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007411 case HLoadClass::LoadKind::kReferrersClass:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007412 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007413 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko94ec2db2017-09-06 17:21:03 +01007414 case HLoadClass::LoadKind::kBootImageClassTable:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007415 case HLoadClass::LoadKind::kBssEntry:
7416 DCHECK(!Runtime::Current()->UseJitCompilation());
7417 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007418 case HLoadClass::LoadKind::kJitTableAddress:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007419 DCHECK(Runtime::Current()->UseJitCompilation());
Alexey Frunze06a46c42016-07-19 15:00:40 -07007420 break;
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007421 case HLoadClass::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007422 case HLoadClass::LoadKind::kRuntimeCall:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007423 break;
7424 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007425 return desired_class_load_kind;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007426}
7427
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007428Register CodeGeneratorMIPS::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
7429 Register temp) {
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007430 CHECK(!GetInstructionSetFeatures().IsR6());
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007431 CHECK(!GetGraph()->HasIrreducibleLoops());
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007432 CHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
7433 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
7434 if (!invoke->GetLocations()->Intrinsified()) {
7435 return location.AsRegister<Register>();
7436 }
7437 // For intrinsics we allow any location, so it may be on the stack.
7438 if (!location.IsRegister()) {
7439 __ LoadFromOffset(kLoadWord, temp, SP, location.GetStackIndex());
7440 return temp;
7441 }
7442 // For register locations, check if the register was saved. If so, get it from the stack.
7443 // Note: There is a chance that the register was saved but not overwritten, so we could
7444 // save one load. However, since this is just an intrinsic slow path we prefer this
7445 // simple and more robust approach rather that trying to determine if that's the case.
7446 SlowPathCode* slow_path = GetCurrentSlowPath();
7447 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
7448 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
7449 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
7450 __ LoadFromOffset(kLoadWord, temp, SP, stack_offset);
7451 return temp;
7452 }
7453 return location.AsRegister<Register>();
7454}
7455
Vladimir Markodc151b22015-10-15 18:02:30 +01007456HInvokeStaticOrDirect::DispatchInfo CodeGeneratorMIPS::GetSupportedInvokeStaticOrDirectDispatch(
7457 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01007458 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007459 return desired_dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01007460}
7461
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007462void CodeGeneratorMIPS::GenerateStaticOrDirectCall(
7463 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007464 // All registers are assumed to be correctly set up per the calling convention.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007465 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007466 HInvokeStaticOrDirect::MethodLoadKind method_load_kind = invoke->GetMethodLoadKind();
7467 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location = invoke->GetCodePtrLocation();
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007468 bool is_r6 = GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007469 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
7470 Register base_reg = (invoke->HasPcRelativeMethodLoadKind() && !is_r6 && !has_irreducible_loops)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007471 ? GetInvokeStaticOrDirectExtraParameter(invoke, temp.AsRegister<Register>())
7472 : ZERO;
7473
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007474 switch (method_load_kind) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007475 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007476 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007477 uint32_t offset =
7478 GetThreadOffset<kMipsPointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007479 __ LoadFromOffset(kLoadWord,
7480 temp.AsRegister<Register>(),
7481 TR,
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007482 offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007483 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007484 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007485 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00007486 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007487 break;
Vladimir Marko65979462017-05-19 17:25:12 +01007488 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative: {
7489 DCHECK(GetCompilerOptions().IsBootImage());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007490 PcRelativePatchInfo* info_high = NewPcRelativeMethodPatch(invoke->GetTargetMethod());
7491 PcRelativePatchInfo* info_low =
7492 NewPcRelativeMethodPatch(invoke->GetTargetMethod(), info_high);
Vladimir Marko65979462017-05-19 17:25:12 +01007493 Register temp_reg = temp.AsRegister<Register>();
Alexey Frunzea663d9d2017-07-31 18:43:18 -07007494 EmitPcRelativeAddressPlaceholderHigh(info_high, TMP, base_reg);
7495 __ Addiu(temp_reg, TMP, /* placeholder */ 0x5678, &info_low->label);
Vladimir Marko65979462017-05-19 17:25:12 +01007496 break;
7497 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007498 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
7499 __ LoadConst32(temp.AsRegister<Register>(), invoke->GetMethodAddress());
7500 break;
Vladimir Marko0eb882b2017-05-15 13:39:18 +01007501 case HInvokeStaticOrDirect::MethodLoadKind::kBssEntry: {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007502 PcRelativePatchInfo* info_high = NewMethodBssEntryPatch(
Vladimir Marko0eb882b2017-05-15 13:39:18 +01007503 MethodReference(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex()));
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007504 PcRelativePatchInfo* info_low = NewMethodBssEntryPatch(
7505 MethodReference(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex()), info_high);
Vladimir Marko0eb882b2017-05-15 13:39:18 +01007506 Register temp_reg = temp.AsRegister<Register>();
Alexey Frunzea663d9d2017-07-31 18:43:18 -07007507 EmitPcRelativeAddressPlaceholderHigh(info_high, TMP, base_reg);
7508 __ Lw(temp_reg, TMP, /* placeholder */ 0x5678, &info_low->label);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007509 break;
Vladimir Marko0eb882b2017-05-15 13:39:18 +01007510 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007511 case HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall: {
7512 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
7513 return; // No code pointer retrieval; the runtime performs the call directly.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007514 }
7515 }
7516
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007517 switch (code_ptr_location) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007518 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007519 __ Bal(&frame_entry_label_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007520 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007521 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
7522 // T9 = callee_method->entry_point_from_quick_compiled_code_;
Goran Jakovljevic1a878372015-10-26 14:28:52 +01007523 __ LoadFromOffset(kLoadWord,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007524 T9,
7525 callee_method.AsRegister<Register>(),
7526 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07007527 kMipsPointerSize).Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007528 // T9()
7529 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07007530 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007531 break;
7532 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007533 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
7534
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007535 DCHECK(!IsLeafMethod());
7536}
7537
7538void InstructionCodeGeneratorMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00007539 // Explicit clinit checks triggered by static invokes must have been pruned by
7540 // art::PrepareForRegisterAllocation.
7541 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007542
7543 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
7544 return;
7545 }
7546
7547 LocationSummary* locations = invoke->GetLocations();
7548 codegen_->GenerateStaticOrDirectCall(invoke,
7549 locations->HasTemps()
7550 ? locations->GetTemp(0)
7551 : Location::NoLocation());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007552}
7553
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007554void CodeGeneratorMIPS::GenerateVirtualCall(
7555 HInvokeVirtual* invoke, Location temp_location, SlowPathCode* slow_path) {
Goran Jakovljevice919b072016-10-04 10:17:34 +02007556 // Use the calling convention instead of the location of the receiver, as
7557 // intrinsics may have put the receiver in a different register. In the intrinsics
7558 // slow path, the arguments have been moved to the right place, so here we are
7559 // guaranteed that the receiver is the first register of the calling convention.
7560 InvokeDexCallingConvention calling_convention;
7561 Register receiver = calling_convention.GetRegisterAt(0);
7562
Chris Larsen3acee732015-11-18 13:31:08 -08007563 Register temp = temp_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007564 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
7565 invoke->GetVTableIndex(), kMipsPointerSize).SizeValue();
7566 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07007567 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007568
7569 // temp = object->GetClass();
Goran Jakovljevice919b072016-10-04 10:17:34 +02007570 __ LoadFromOffset(kLoadWord, temp, receiver, class_offset);
Chris Larsen3acee732015-11-18 13:31:08 -08007571 MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08007572 // Instead of simply (possibly) unpoisoning `temp` here, we should
7573 // emit a read barrier for the previous class reference load.
7574 // However this is not required in practice, as this is an
7575 // intermediate/temporary reference and because the current
7576 // concurrent copying collector keeps the from-space memory
7577 // intact/accessible until the end of the marking phase (the
7578 // concurrent copying collector may not in the future).
7579 __ MaybeUnpoisonHeapReference(temp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007580 // temp = temp->GetMethodAt(method_offset);
7581 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
7582 // T9 = temp->GetEntryPoint();
7583 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
7584 // T9();
7585 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07007586 __ NopIfNoReordering();
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007587 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Chris Larsen3acee732015-11-18 13:31:08 -08007588}
7589
7590void InstructionCodeGeneratorMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
7591 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
7592 return;
7593 }
7594
7595 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007596 DCHECK(!codegen_->IsLeafMethod());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007597}
7598
7599void LocationsBuilderMIPS::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00007600 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007601 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007602 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07007603 Location loc = Location::RegisterLocation(calling_convention.GetRegisterAt(0));
7604 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(cls, loc, loc);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007605 return;
7606 }
Vladimir Marko41559982017-01-06 14:04:23 +00007607 DCHECK(!cls->NeedsAccessCheck());
Alexey Frunzec61c0762017-04-10 13:54:23 -07007608 const bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007609 const bool has_irreducible_loops = codegen_->GetGraph()->HasIrreducibleLoops();
Alexey Frunze15958152017-02-09 19:08:30 -08007610 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
7611 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Alexey Frunze06a46c42016-07-19 15:00:40 -07007612 ? LocationSummary::kCallOnSlowPath
7613 : LocationSummary::kNoCall;
7614 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Alexey Frunzec61c0762017-04-10 13:54:23 -07007615 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
7616 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
7617 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007618 switch (load_kind) {
7619 // We need an extra register for PC-relative literals on R2.
Alexey Frunze06a46c42016-07-19 15:00:40 -07007620 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007621 case HLoadClass::LoadKind::kBootImageAddress:
Vladimir Marko94ec2db2017-09-06 17:21:03 +01007622 case HLoadClass::LoadKind::kBootImageClassTable:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007623 case HLoadClass::LoadKind::kBssEntry:
Alexey Frunzec61c0762017-04-10 13:54:23 -07007624 if (isR6) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007625 break;
7626 }
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007627 if (has_irreducible_loops) {
7628 codegen_->ClobberRA();
7629 break;
7630 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007631 FALLTHROUGH_INTENDED;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007632 case HLoadClass::LoadKind::kReferrersClass:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007633 locations->SetInAt(0, Location::RequiresRegister());
7634 break;
7635 default:
7636 break;
7637 }
7638 locations->SetOut(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07007639 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
7640 if (!kUseReadBarrier || kUseBakerReadBarrier) {
7641 // Rely on the type resolution or initialization and marking to save everything we need.
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007642 // Request a temp to hold the BSS entry location for the slow path.
7643 locations->AddTemp(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07007644 RegisterSet caller_saves = RegisterSet::Empty();
7645 InvokeRuntimeCallingConvention calling_convention;
7646 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
7647 locations->SetCustomSlowPathCallerSaves(caller_saves);
7648 } else {
7649 // For non-Baker read barriers we have a temp-clobbering call.
7650 }
7651 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007652}
7653
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007654// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
7655// move.
7656void InstructionCodeGeneratorMIPS::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00007657 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007658 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00007659 codegen_->GenerateLoadClassRuntimeCall(cls);
Pavle Batutae87a7182015-10-28 13:10:42 +01007660 return;
7661 }
Vladimir Marko41559982017-01-06 14:04:23 +00007662 DCHECK(!cls->NeedsAccessCheck());
Pavle Batutae87a7182015-10-28 13:10:42 +01007663
Vladimir Marko41559982017-01-06 14:04:23 +00007664 LocationSummary* locations = cls->GetLocations();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007665 Location out_loc = locations->Out();
7666 Register out = out_loc.AsRegister<Register>();
7667 Register base_or_current_method_reg;
7668 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007669 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
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:
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007674 case HLoadClass::LoadKind::kBootImageClassTable:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007675 case HLoadClass::LoadKind::kBssEntry:
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007676 base_or_current_method_reg =
7677 (isR6 || has_irreducible_loops) ? ZERO : locations->InAt(0).AsRegister<Register>();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007678 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007679 case HLoadClass::LoadKind::kReferrersClass:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007680 case HLoadClass::LoadKind::kRuntimeCall:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007681 base_or_current_method_reg = locations->InAt(0).AsRegister<Register>();
7682 break;
7683 default:
7684 base_or_current_method_reg = ZERO;
7685 break;
7686 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00007687
Alexey Frunze15958152017-02-09 19:08:30 -08007688 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
7689 ? kWithoutReadBarrier
7690 : kCompilerReadBarrierOption;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007691 bool generate_null_check = false;
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007692 CodeGeneratorMIPS::PcRelativePatchInfo* bss_info_high = nullptr;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007693 switch (load_kind) {
7694 case HLoadClass::LoadKind::kReferrersClass: {
7695 DCHECK(!cls->CanCallRuntime());
7696 DCHECK(!cls->MustGenerateClinitCheck());
7697 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
7698 GenerateGcRootFieldLoad(cls,
7699 out_loc,
7700 base_or_current_method_reg,
Alexey Frunze15958152017-02-09 19:08:30 -08007701 ArtMethod::DeclaringClassOffset().Int32Value(),
7702 read_barrier_option);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007703 break;
7704 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007705 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007706 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze15958152017-02-09 19:08:30 -08007707 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007708 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
Alexey Frunze06a46c42016-07-19 15:00:40 -07007709 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007710 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
7711 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex(), info_high);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007712 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
7713 out,
Alexey Frunzea663d9d2017-07-31 18:43:18 -07007714 base_or_current_method_reg);
7715 __ Addiu(out, out, /* placeholder */ 0x5678, &info_low->label);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007716 break;
7717 }
7718 case HLoadClass::LoadKind::kBootImageAddress: {
Alexey Frunze15958152017-02-09 19:08:30 -08007719 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007720 uint32_t address = dchecked_integral_cast<uint32_t>(
7721 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
7722 DCHECK_NE(address, 0u);
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007723 if (isR6 || !has_irreducible_loops) {
7724 __ LoadLiteral(out,
7725 base_or_current_method_reg,
7726 codegen_->DeduplicateBootImageAddressLiteral(address));
7727 } else {
7728 __ LoadConst32(out, address);
7729 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007730 break;
7731 }
Vladimir Marko94ec2db2017-09-06 17:21:03 +01007732 case HLoadClass::LoadKind::kBootImageClassTable: {
7733 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
7734 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
7735 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
7736 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
7737 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex(), info_high);
7738 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
7739 out,
7740 base_or_current_method_reg);
7741 __ Lw(out, out, /* placeholder */ 0x5678, &info_low->label);
7742 // Extract the reference from the slot data, i.e. clear the hash bits.
7743 int32_t masked_hash = ClassTable::TableSlot::MaskHash(
7744 ComputeModifiedUtf8Hash(cls->GetDexFile().StringByTypeIdx(cls->GetTypeIndex())));
7745 if (masked_hash != 0) {
7746 __ Addiu(out, out, -masked_hash);
7747 }
7748 break;
7749 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007750 case HLoadClass::LoadKind::kBssEntry: {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007751 bss_info_high = codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex());
7752 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
7753 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex(), bss_info_high);
Alexey Frunzec61c0762017-04-10 13:54:23 -07007754 constexpr bool non_baker_read_barrier = kUseReadBarrier && !kUseBakerReadBarrier;
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007755 Register temp = non_baker_read_barrier ? out : locations->GetTemp(0).AsRegister<Register>();
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007756 codegen_->EmitPcRelativeAddressPlaceholderHigh(bss_info_high,
7757 temp,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007758 base_or_current_method_reg);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007759 GenerateGcRootFieldLoad(cls,
7760 out_loc,
7761 temp,
7762 /* placeholder */ 0x5678,
7763 read_barrier_option,
7764 &info_low->label);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007765 generate_null_check = true;
7766 break;
7767 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007768 case HLoadClass::LoadKind::kJitTableAddress: {
Alexey Frunze627c1a02017-01-30 19:28:14 -08007769 CodeGeneratorMIPS::JitPatchInfo* info = codegen_->NewJitRootClassPatch(cls->GetDexFile(),
7770 cls->GetTypeIndex(),
7771 cls->GetClass());
7772 bool reordering = __ SetReorder(false);
7773 __ Bind(&info->high_label);
7774 __ Lui(out, /* placeholder */ 0x1234);
Alexey Frunze627c1a02017-01-30 19:28:14 -08007775 __ SetReorder(reordering);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007776 GenerateGcRootFieldLoad(cls,
7777 out_loc,
7778 out,
7779 /* placeholder */ 0x5678,
7780 read_barrier_option,
7781 &info->low_label);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007782 break;
7783 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007784 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00007785 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00007786 LOG(FATAL) << "UNREACHABLE";
7787 UNREACHABLE();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007788 }
7789
7790 if (generate_null_check || cls->MustGenerateClinitCheck()) {
7791 DCHECK(cls->CanCallRuntime());
7792 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007793 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck(), bss_info_high);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007794 codegen_->AddSlowPath(slow_path);
7795 if (generate_null_check) {
7796 __ Beqz(out, slow_path->GetEntryLabel());
7797 }
7798 if (cls->MustGenerateClinitCheck()) {
7799 GenerateClassInitializationCheck(slow_path, out);
7800 } else {
7801 __ Bind(slow_path->GetExitLabel());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007802 }
7803 }
7804}
7805
7806static int32_t GetExceptionTlsOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -07007807 return Thread::ExceptionOffset<kMipsPointerSize>().Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007808}
7809
7810void LocationsBuilderMIPS::VisitLoadException(HLoadException* load) {
7811 LocationSummary* locations =
7812 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
7813 locations->SetOut(Location::RequiresRegister());
7814}
7815
7816void InstructionCodeGeneratorMIPS::VisitLoadException(HLoadException* load) {
7817 Register out = load->GetLocations()->Out().AsRegister<Register>();
7818 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
7819}
7820
7821void LocationsBuilderMIPS::VisitClearException(HClearException* clear) {
7822 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
7823}
7824
7825void InstructionCodeGeneratorMIPS::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
7826 __ StoreToOffset(kStoreWord, ZERO, TR, GetExceptionTlsOffset());
7827}
7828
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007829void LocationsBuilderMIPS::VisitLoadString(HLoadString* load) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08007830 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Nicolas Geoffray917d0162015-11-24 18:25:35 +00007831 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007832 HLoadString::LoadKind load_kind = load->GetLoadKind();
Alexey Frunzec61c0762017-04-10 13:54:23 -07007833 const bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007834 const bool has_irreducible_loops = codegen_->GetGraph()->HasIrreducibleLoops();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007835 switch (load_kind) {
7836 // We need an extra register for PC-relative literals on R2.
Alexey Frunze06a46c42016-07-19 15:00:40 -07007837 case HLoadString::LoadKind::kBootImageAddress:
7838 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007839 case HLoadString::LoadKind::kBootImageInternTable:
Vladimir Markoaad75c62016-10-03 08:46:48 +00007840 case HLoadString::LoadKind::kBssEntry:
Alexey Frunzec61c0762017-04-10 13:54:23 -07007841 if (isR6) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007842 break;
7843 }
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007844 if (has_irreducible_loops) {
7845 codegen_->ClobberRA();
7846 break;
7847 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007848 FALLTHROUGH_INTENDED;
7849 // We need an extra register for PC-relative dex cache accesses.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007850 case HLoadString::LoadKind::kRuntimeCall:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007851 locations->SetInAt(0, Location::RequiresRegister());
7852 break;
7853 default:
7854 break;
7855 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007856 if (load_kind == HLoadString::LoadKind::kRuntimeCall) {
Alexey Frunzebb51df82016-11-01 16:07:32 -07007857 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07007858 locations->SetOut(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Alexey Frunzebb51df82016-11-01 16:07:32 -07007859 } else {
7860 locations->SetOut(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07007861 if (load_kind == HLoadString::LoadKind::kBssEntry) {
7862 if (!kUseReadBarrier || kUseBakerReadBarrier) {
7863 // Rely on the pResolveString and marking to save everything we need.
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007864 // Request a temp to hold the BSS entry location for the slow path.
7865 locations->AddTemp(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07007866 RegisterSet caller_saves = RegisterSet::Empty();
7867 InvokeRuntimeCallingConvention calling_convention;
7868 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
7869 locations->SetCustomSlowPathCallerSaves(caller_saves);
7870 } else {
7871 // For non-Baker read barriers we have a temp-clobbering call.
7872 }
7873 }
Alexey Frunzebb51df82016-11-01 16:07:32 -07007874 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007875}
7876
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007877// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
7878// move.
7879void InstructionCodeGeneratorMIPS::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007880 HLoadString::LoadKind load_kind = load->GetLoadKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007881 LocationSummary* locations = load->GetLocations();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007882 Location out_loc = locations->Out();
7883 Register out = out_loc.AsRegister<Register>();
7884 Register base_or_current_method_reg;
7885 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007886 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007887 switch (load_kind) {
7888 // We need an extra register for PC-relative literals on R2.
Alexey Frunze06a46c42016-07-19 15:00:40 -07007889 case HLoadString::LoadKind::kBootImageAddress:
7890 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007891 case HLoadString::LoadKind::kBootImageInternTable:
Vladimir Markoaad75c62016-10-03 08:46:48 +00007892 case HLoadString::LoadKind::kBssEntry:
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007893 base_or_current_method_reg =
7894 (isR6 || has_irreducible_loops) ? ZERO : locations->InAt(0).AsRegister<Register>();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007895 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007896 default:
7897 base_or_current_method_reg = ZERO;
7898 break;
7899 }
7900
7901 switch (load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007902 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Markoaad75c62016-10-03 08:46:48 +00007903 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007904 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007905 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007906 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
7907 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex(), info_high);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007908 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
7909 out,
Alexey Frunzea663d9d2017-07-31 18:43:18 -07007910 base_or_current_method_reg);
7911 __ Addiu(out, out, /* placeholder */ 0x5678, &info_low->label);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007912 return;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007913 }
7914 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007915 uint32_t address = dchecked_integral_cast<uint32_t>(
7916 reinterpret_cast<uintptr_t>(load->GetString().Get()));
7917 DCHECK_NE(address, 0u);
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007918 if (isR6 || !has_irreducible_loops) {
7919 __ LoadLiteral(out,
7920 base_or_current_method_reg,
7921 codegen_->DeduplicateBootImageAddressLiteral(address));
7922 } else {
7923 __ LoadConst32(out, address);
7924 }
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007925 return;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007926 }
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007927 case HLoadString::LoadKind::kBootImageInternTable: {
Vladimir Markoaad75c62016-10-03 08:46:48 +00007928 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007929 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007930 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007931 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
7932 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex(), info_high);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007933 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
7934 out,
7935 base_or_current_method_reg);
7936 __ Lw(out, out, /* placeholder */ 0x5678, &info_low->label);
7937 return;
7938 }
7939 case HLoadString::LoadKind::kBssEntry: {
7940 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
7941 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
7942 codegen_->NewStringBssEntryPatch(load->GetDexFile(), load->GetStringIndex());
7943 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
7944 codegen_->NewStringBssEntryPatch(load->GetDexFile(), load->GetStringIndex(), info_high);
Alexey Frunzec61c0762017-04-10 13:54:23 -07007945 constexpr bool non_baker_read_barrier = kUseReadBarrier && !kUseBakerReadBarrier;
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007946 Register temp = non_baker_read_barrier ? out : locations->GetTemp(0).AsRegister<Register>();
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007947 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
7948 temp,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007949 base_or_current_method_reg);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007950 GenerateGcRootFieldLoad(load,
7951 out_loc,
7952 temp,
7953 /* placeholder */ 0x5678,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007954 kCompilerReadBarrierOption,
7955 &info_low->label);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007956 SlowPathCodeMIPS* slow_path =
7957 new (GetGraph()->GetArena()) LoadStringSlowPathMIPS(load, info_high);
Vladimir Markoaad75c62016-10-03 08:46:48 +00007958 codegen_->AddSlowPath(slow_path);
7959 __ Beqz(out, slow_path->GetEntryLabel());
7960 __ Bind(slow_path->GetExitLabel());
7961 return;
7962 }
Alexey Frunze627c1a02017-01-30 19:28:14 -08007963 case HLoadString::LoadKind::kJitTableAddress: {
7964 CodeGeneratorMIPS::JitPatchInfo* info =
7965 codegen_->NewJitRootStringPatch(load->GetDexFile(),
7966 load->GetStringIndex(),
7967 load->GetString());
7968 bool reordering = __ SetReorder(false);
7969 __ Bind(&info->high_label);
7970 __ Lui(out, /* placeholder */ 0x1234);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007971 __ SetReorder(reordering);
Alexey Frunze15958152017-02-09 19:08:30 -08007972 GenerateGcRootFieldLoad(load,
7973 out_loc,
7974 out,
7975 /* placeholder */ 0x5678,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007976 kCompilerReadBarrierOption,
7977 &info->low_label);
Alexey Frunze627c1a02017-01-30 19:28:14 -08007978 return;
7979 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007980 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07007981 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007982 }
Nicolas Geoffray917d0162015-11-24 18:25:35 +00007983
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07007984 // TODO: Re-add the compiler code to do string dex cache lookup again.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007985 DCHECK(load_kind == HLoadString::LoadKind::kRuntimeCall);
Vladimir Markoaad75c62016-10-03 08:46:48 +00007986 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07007987 DCHECK_EQ(calling_convention.GetRegisterAt(0), out);
Andreas Gampe8a0128a2016-11-28 07:38:35 -08007988 __ LoadConst32(calling_convention.GetRegisterAt(0), load->GetStringIndex().index_);
Vladimir Markoaad75c62016-10-03 08:46:48 +00007989 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
7990 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007991}
7992
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007993void LocationsBuilderMIPS::VisitLongConstant(HLongConstant* constant) {
7994 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
7995 locations->SetOut(Location::ConstantLocation(constant));
7996}
7997
7998void InstructionCodeGeneratorMIPS::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
7999 // Will be generated at use site.
8000}
8001
8002void LocationsBuilderMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
8003 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01008004 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008005 InvokeRuntimeCallingConvention calling_convention;
8006 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
8007}
8008
8009void InstructionCodeGeneratorMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
8010 if (instruction->IsEnter()) {
Serban Constantinescufca16662016-07-14 09:21:59 +01008011 codegen_->InvokeRuntime(kQuickLockObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008012 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
8013 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01008014 codegen_->InvokeRuntime(kQuickUnlockObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008015 }
8016 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
8017}
8018
8019void LocationsBuilderMIPS::VisitMul(HMul* mul) {
8020 LocationSummary* locations =
8021 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
8022 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008023 case DataType::Type::kInt32:
8024 case DataType::Type::kInt64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008025 locations->SetInAt(0, Location::RequiresRegister());
8026 locations->SetInAt(1, Location::RequiresRegister());
8027 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8028 break;
8029
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008030 case DataType::Type::kFloat32:
8031 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008032 locations->SetInAt(0, Location::RequiresFpuRegister());
8033 locations->SetInAt(1, Location::RequiresFpuRegister());
8034 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
8035 break;
8036
8037 default:
8038 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
8039 }
8040}
8041
8042void InstructionCodeGeneratorMIPS::VisitMul(HMul* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008043 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008044 LocationSummary* locations = instruction->GetLocations();
8045 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
8046
8047 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008048 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008049 Register dst = locations->Out().AsRegister<Register>();
8050 Register lhs = locations->InAt(0).AsRegister<Register>();
8051 Register rhs = locations->InAt(1).AsRegister<Register>();
8052
8053 if (isR6) {
8054 __ MulR6(dst, lhs, rhs);
8055 } else {
8056 __ MulR2(dst, lhs, rhs);
8057 }
8058 break;
8059 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008060 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008061 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8062 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
8063 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
8064 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
8065 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
8066 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
8067
8068 // Extra checks to protect caused by the existance of A1_A2.
8069 // The algorithm is wrong if dst_high is either lhs_lo or rhs_lo:
8070 // (e.g. lhs=a0_a1, rhs=a2_a3 and dst=a1_a2).
8071 DCHECK_NE(dst_high, lhs_low);
8072 DCHECK_NE(dst_high, rhs_low);
8073
8074 // A_B * C_D
8075 // dst_hi: [ low(A*D) + low(B*C) + hi(B*D) ]
8076 // dst_lo: [ low(B*D) ]
8077 // Note: R2 and R6 MUL produce the low 32 bit of the multiplication result.
8078
8079 if (isR6) {
8080 __ MulR6(TMP, lhs_high, rhs_low);
8081 __ MulR6(dst_high, lhs_low, rhs_high);
8082 __ Addu(dst_high, dst_high, TMP);
8083 __ MuhuR6(TMP, lhs_low, rhs_low);
8084 __ Addu(dst_high, dst_high, TMP);
8085 __ MulR6(dst_low, lhs_low, rhs_low);
8086 } else {
8087 __ MulR2(TMP, lhs_high, rhs_low);
8088 __ MulR2(dst_high, lhs_low, rhs_high);
8089 __ Addu(dst_high, dst_high, TMP);
8090 __ MultuR2(lhs_low, rhs_low);
8091 __ Mfhi(TMP);
8092 __ Addu(dst_high, dst_high, TMP);
8093 __ Mflo(dst_low);
8094 }
8095 break;
8096 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008097 case DataType::Type::kFloat32:
8098 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008099 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
8100 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
8101 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008102 if (type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008103 __ MulS(dst, lhs, rhs);
8104 } else {
8105 __ MulD(dst, lhs, rhs);
8106 }
8107 break;
8108 }
8109 default:
8110 LOG(FATAL) << "Unexpected mul type " << type;
8111 }
8112}
8113
8114void LocationsBuilderMIPS::VisitNeg(HNeg* neg) {
8115 LocationSummary* locations =
8116 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
8117 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008118 case DataType::Type::kInt32:
8119 case DataType::Type::kInt64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008120 locations->SetInAt(0, Location::RequiresRegister());
8121 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8122 break;
8123
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008124 case DataType::Type::kFloat32:
8125 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008126 locations->SetInAt(0, Location::RequiresFpuRegister());
8127 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
8128 break;
8129
8130 default:
8131 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
8132 }
8133}
8134
8135void InstructionCodeGeneratorMIPS::VisitNeg(HNeg* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008136 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008137 LocationSummary* locations = instruction->GetLocations();
8138
8139 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008140 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008141 Register dst = locations->Out().AsRegister<Register>();
8142 Register src = locations->InAt(0).AsRegister<Register>();
8143 __ Subu(dst, ZERO, src);
8144 break;
8145 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008146 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008147 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8148 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
8149 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
8150 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
8151 __ Subu(dst_low, ZERO, src_low);
8152 __ Sltu(TMP, ZERO, dst_low);
8153 __ Subu(dst_high, ZERO, src_high);
8154 __ Subu(dst_high, dst_high, TMP);
8155 break;
8156 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008157 case DataType::Type::kFloat32:
8158 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008159 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
8160 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008161 if (type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008162 __ NegS(dst, src);
8163 } else {
8164 __ NegD(dst, src);
8165 }
8166 break;
8167 }
8168 default:
8169 LOG(FATAL) << "Unexpected neg type " << type;
8170 }
8171}
8172
8173void LocationsBuilderMIPS::VisitNewArray(HNewArray* instruction) {
8174 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01008175 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008176 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008177 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kReference));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00008178 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
8179 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008180}
8181
8182void InstructionCodeGeneratorMIPS::VisitNewArray(HNewArray* instruction) {
Alexey Frunzec061de12017-02-14 13:27:23 -08008183 // Note: if heap poisoning is enabled, the entry point takes care
8184 // of poisoning the reference.
Goran Jakovljevic854df412017-06-27 14:41:39 +02008185 QuickEntrypointEnum entrypoint =
8186 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
8187 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00008188 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Goran Jakovljevic854df412017-06-27 14:41:39 +02008189 DCHECK(!codegen_->IsLeafMethod());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008190}
8191
8192void LocationsBuilderMIPS::VisitNewInstance(HNewInstance* instruction) {
8193 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01008194 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008195 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00008196 if (instruction->IsStringAlloc()) {
8197 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
8198 } else {
8199 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00008200 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008201 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kReference));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008202}
8203
8204void InstructionCodeGeneratorMIPS::VisitNewInstance(HNewInstance* instruction) {
Alexey Frunzec061de12017-02-14 13:27:23 -08008205 // Note: if heap poisoning is enabled, the entry point takes care
8206 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00008207 if (instruction->IsStringAlloc()) {
8208 // String is allocated through StringFactory. Call NewEmptyString entry point.
8209 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07008210 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00008211 __ LoadFromOffset(kLoadWord, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
8212 __ LoadFromOffset(kLoadWord, T9, temp, code_offset.Int32Value());
8213 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07008214 __ NopIfNoReordering();
David Brazdil6de19382016-01-08 17:37:10 +00008215 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
8216 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01008217 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00008218 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00008219 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008220}
8221
8222void LocationsBuilderMIPS::VisitNot(HNot* instruction) {
8223 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
8224 locations->SetInAt(0, Location::RequiresRegister());
8225 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8226}
8227
8228void InstructionCodeGeneratorMIPS::VisitNot(HNot* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008229 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008230 LocationSummary* locations = instruction->GetLocations();
8231
8232 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008233 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008234 Register dst = locations->Out().AsRegister<Register>();
8235 Register src = locations->InAt(0).AsRegister<Register>();
8236 __ Nor(dst, src, ZERO);
8237 break;
8238 }
8239
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008240 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008241 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8242 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
8243 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
8244 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
8245 __ Nor(dst_high, src_high, ZERO);
8246 __ Nor(dst_low, src_low, ZERO);
8247 break;
8248 }
8249
8250 default:
8251 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
8252 }
8253}
8254
8255void LocationsBuilderMIPS::VisitBooleanNot(HBooleanNot* instruction) {
8256 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
8257 locations->SetInAt(0, Location::RequiresRegister());
8258 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8259}
8260
8261void InstructionCodeGeneratorMIPS::VisitBooleanNot(HBooleanNot* instruction) {
8262 LocationSummary* locations = instruction->GetLocations();
8263 __ Xori(locations->Out().AsRegister<Register>(),
8264 locations->InAt(0).AsRegister<Register>(),
8265 1);
8266}
8267
8268void LocationsBuilderMIPS::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01008269 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
8270 locations->SetInAt(0, Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008271}
8272
Calin Juravle2ae48182016-03-16 14:05:09 +00008273void CodeGeneratorMIPS::GenerateImplicitNullCheck(HNullCheck* instruction) {
8274 if (CanMoveNullCheckToUser(instruction)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008275 return;
8276 }
8277 Location obj = instruction->GetLocations()->InAt(0);
8278
8279 __ Lw(ZERO, obj.AsRegister<Register>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00008280 RecordPcInfo(instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008281}
8282
Calin Juravle2ae48182016-03-16 14:05:09 +00008283void CodeGeneratorMIPS::GenerateExplicitNullCheck(HNullCheck* instruction) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008284 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathMIPS(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00008285 AddSlowPath(slow_path);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008286
8287 Location obj = instruction->GetLocations()->InAt(0);
8288
8289 __ Beqz(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
8290}
8291
8292void InstructionCodeGeneratorMIPS::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00008293 codegen_->GenerateNullCheck(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008294}
8295
8296void LocationsBuilderMIPS::VisitOr(HOr* instruction) {
8297 HandleBinaryOp(instruction);
8298}
8299
8300void InstructionCodeGeneratorMIPS::VisitOr(HOr* instruction) {
8301 HandleBinaryOp(instruction);
8302}
8303
8304void LocationsBuilderMIPS::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
8305 LOG(FATAL) << "Unreachable";
8306}
8307
8308void InstructionCodeGeneratorMIPS::VisitParallelMove(HParallelMove* instruction) {
8309 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
8310}
8311
8312void LocationsBuilderMIPS::VisitParameterValue(HParameterValue* instruction) {
8313 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
8314 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
8315 if (location.IsStackSlot()) {
8316 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
8317 } else if (location.IsDoubleStackSlot()) {
8318 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
8319 }
8320 locations->SetOut(location);
8321}
8322
8323void InstructionCodeGeneratorMIPS::VisitParameterValue(HParameterValue* instruction
8324 ATTRIBUTE_UNUSED) {
8325 // Nothing to do, the parameter is already at its location.
8326}
8327
8328void LocationsBuilderMIPS::VisitCurrentMethod(HCurrentMethod* instruction) {
8329 LocationSummary* locations =
8330 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
8331 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
8332}
8333
8334void InstructionCodeGeneratorMIPS::VisitCurrentMethod(HCurrentMethod* instruction
8335 ATTRIBUTE_UNUSED) {
8336 // Nothing to do, the method is already at its location.
8337}
8338
8339void LocationsBuilderMIPS::VisitPhi(HPhi* instruction) {
8340 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01008341 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008342 locations->SetInAt(i, Location::Any());
8343 }
8344 locations->SetOut(Location::Any());
8345}
8346
8347void InstructionCodeGeneratorMIPS::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
8348 LOG(FATAL) << "Unreachable";
8349}
8350
8351void LocationsBuilderMIPS::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008352 DataType::Type type = rem->GetResultType();
8353 LocationSummary::CallKind call_kind = (type == DataType::Type::kInt32)
8354 ? LocationSummary::kNoCall
8355 : LocationSummary::kCallOnMainOnly;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008356 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
8357
8358 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008359 case DataType::Type::kInt32:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008360 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08008361 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008362 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8363 break;
8364
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008365 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008366 InvokeRuntimeCallingConvention calling_convention;
8367 locations->SetInAt(0, Location::RegisterPairLocation(
8368 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
8369 locations->SetInAt(1, Location::RegisterPairLocation(
8370 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
8371 locations->SetOut(calling_convention.GetReturnLocation(type));
8372 break;
8373 }
8374
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008375 case DataType::Type::kFloat32:
8376 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008377 InvokeRuntimeCallingConvention calling_convention;
8378 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
8379 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
8380 locations->SetOut(calling_convention.GetReturnLocation(type));
8381 break;
8382 }
8383
8384 default:
8385 LOG(FATAL) << "Unexpected rem type " << type;
8386 }
8387}
8388
8389void InstructionCodeGeneratorMIPS::VisitRem(HRem* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008390 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008391
8392 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008393 case DataType::Type::kInt32:
Alexey Frunze7e99e052015-11-24 19:28:01 -08008394 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008395 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008396 case DataType::Type::kInt64: {
Serban Constantinescufca16662016-07-14 09:21:59 +01008397 codegen_->InvokeRuntime(kQuickLmod, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008398 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
8399 break;
8400 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008401 case DataType::Type::kFloat32: {
Serban Constantinescufca16662016-07-14 09:21:59 +01008402 codegen_->InvokeRuntime(kQuickFmodf, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00008403 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008404 break;
8405 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008406 case DataType::Type::kFloat64: {
Serban Constantinescufca16662016-07-14 09:21:59 +01008407 codegen_->InvokeRuntime(kQuickFmod, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00008408 CheckEntrypointTypes<kQuickFmod, double, double, double>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008409 break;
8410 }
8411 default:
8412 LOG(FATAL) << "Unexpected rem type " << type;
8413 }
8414}
8415
Igor Murashkind01745e2017-04-05 16:40:31 -07008416void LocationsBuilderMIPS::VisitConstructorFence(HConstructorFence* constructor_fence) {
8417 constructor_fence->SetLocations(nullptr);
8418}
8419
8420void InstructionCodeGeneratorMIPS::VisitConstructorFence(
8421 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
8422 GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
8423}
8424
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008425void LocationsBuilderMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
8426 memory_barrier->SetLocations(nullptr);
8427}
8428
8429void InstructionCodeGeneratorMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
8430 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
8431}
8432
8433void LocationsBuilderMIPS::VisitReturn(HReturn* ret) {
8434 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(ret);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008435 DataType::Type return_type = ret->InputAt(0)->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008436 locations->SetInAt(0, MipsReturnLocation(return_type));
8437}
8438
8439void InstructionCodeGeneratorMIPS::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
8440 codegen_->GenerateFrameExit();
8441}
8442
8443void LocationsBuilderMIPS::VisitReturnVoid(HReturnVoid* ret) {
8444 ret->SetLocations(nullptr);
8445}
8446
8447void InstructionCodeGeneratorMIPS::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
8448 codegen_->GenerateFrameExit();
8449}
8450
Alexey Frunze92d90602015-12-18 18:16:36 -08008451void LocationsBuilderMIPS::VisitRor(HRor* ror) {
8452 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00008453}
8454
Alexey Frunze92d90602015-12-18 18:16:36 -08008455void InstructionCodeGeneratorMIPS::VisitRor(HRor* ror) {
8456 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00008457}
8458
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008459void LocationsBuilderMIPS::VisitShl(HShl* shl) {
8460 HandleShift(shl);
8461}
8462
8463void InstructionCodeGeneratorMIPS::VisitShl(HShl* shl) {
8464 HandleShift(shl);
8465}
8466
8467void LocationsBuilderMIPS::VisitShr(HShr* shr) {
8468 HandleShift(shr);
8469}
8470
8471void InstructionCodeGeneratorMIPS::VisitShr(HShr* shr) {
8472 HandleShift(shr);
8473}
8474
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008475void LocationsBuilderMIPS::VisitSub(HSub* instruction) {
8476 HandleBinaryOp(instruction);
8477}
8478
8479void InstructionCodeGeneratorMIPS::VisitSub(HSub* instruction) {
8480 HandleBinaryOp(instruction);
8481}
8482
8483void LocationsBuilderMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
8484 HandleFieldGet(instruction, instruction->GetFieldInfo());
8485}
8486
8487void InstructionCodeGeneratorMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
8488 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
8489}
8490
8491void LocationsBuilderMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
8492 HandleFieldSet(instruction, instruction->GetFieldInfo());
8493}
8494
8495void InstructionCodeGeneratorMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Goran Jakovljevice114da22016-12-26 14:21:43 +01008496 HandleFieldSet(instruction,
8497 instruction->GetFieldInfo(),
8498 instruction->GetDexPc(),
8499 instruction->GetValueCanBeNull());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008500}
8501
8502void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldGet(
8503 HUnresolvedInstanceFieldGet* instruction) {
8504 FieldAccessCallingConventionMIPS calling_convention;
8505 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
8506 instruction->GetFieldType(),
8507 calling_convention);
8508}
8509
8510void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldGet(
8511 HUnresolvedInstanceFieldGet* instruction) {
8512 FieldAccessCallingConventionMIPS calling_convention;
8513 codegen_->GenerateUnresolvedFieldAccess(instruction,
8514 instruction->GetFieldType(),
8515 instruction->GetFieldIndex(),
8516 instruction->GetDexPc(),
8517 calling_convention);
8518}
8519
8520void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldSet(
8521 HUnresolvedInstanceFieldSet* instruction) {
8522 FieldAccessCallingConventionMIPS calling_convention;
8523 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
8524 instruction->GetFieldType(),
8525 calling_convention);
8526}
8527
8528void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldSet(
8529 HUnresolvedInstanceFieldSet* instruction) {
8530 FieldAccessCallingConventionMIPS calling_convention;
8531 codegen_->GenerateUnresolvedFieldAccess(instruction,
8532 instruction->GetFieldType(),
8533 instruction->GetFieldIndex(),
8534 instruction->GetDexPc(),
8535 calling_convention);
8536}
8537
8538void LocationsBuilderMIPS::VisitUnresolvedStaticFieldGet(
8539 HUnresolvedStaticFieldGet* instruction) {
8540 FieldAccessCallingConventionMIPS calling_convention;
8541 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
8542 instruction->GetFieldType(),
8543 calling_convention);
8544}
8545
8546void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldGet(
8547 HUnresolvedStaticFieldGet* instruction) {
8548 FieldAccessCallingConventionMIPS calling_convention;
8549 codegen_->GenerateUnresolvedFieldAccess(instruction,
8550 instruction->GetFieldType(),
8551 instruction->GetFieldIndex(),
8552 instruction->GetDexPc(),
8553 calling_convention);
8554}
8555
8556void LocationsBuilderMIPS::VisitUnresolvedStaticFieldSet(
8557 HUnresolvedStaticFieldSet* instruction) {
8558 FieldAccessCallingConventionMIPS calling_convention;
8559 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
8560 instruction->GetFieldType(),
8561 calling_convention);
8562}
8563
8564void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldSet(
8565 HUnresolvedStaticFieldSet* instruction) {
8566 FieldAccessCallingConventionMIPS calling_convention;
8567 codegen_->GenerateUnresolvedFieldAccess(instruction,
8568 instruction->GetFieldType(),
8569 instruction->GetFieldIndex(),
8570 instruction->GetDexPc(),
8571 calling_convention);
8572}
8573
8574void LocationsBuilderMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01008575 LocationSummary* locations =
8576 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Lena Djokicca8c2952017-05-29 11:31:46 +02008577 // In suspend check slow path, usually there are no caller-save registers at all.
8578 // If SIMD instructions are present, however, we force spilling all live SIMD
8579 // registers in full width (since the runtime only saves/restores lower part).
8580 locations->SetCustomSlowPathCallerSaves(
8581 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008582}
8583
8584void InstructionCodeGeneratorMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
8585 HBasicBlock* block = instruction->GetBlock();
8586 if (block->GetLoopInformation() != nullptr) {
8587 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
8588 // The back edge will generate the suspend check.
8589 return;
8590 }
8591 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
8592 // The goto will generate the suspend check.
8593 return;
8594 }
8595 GenerateSuspendCheck(instruction, nullptr);
8596}
8597
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008598void LocationsBuilderMIPS::VisitThrow(HThrow* instruction) {
8599 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01008600 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008601 InvokeRuntimeCallingConvention calling_convention;
8602 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
8603}
8604
8605void InstructionCodeGeneratorMIPS::VisitThrow(HThrow* instruction) {
Serban Constantinescufca16662016-07-14 09:21:59 +01008606 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008607 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
8608}
8609
8610void LocationsBuilderMIPS::VisitTypeConversion(HTypeConversion* conversion) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008611 DataType::Type input_type = conversion->GetInputType();
8612 DataType::Type result_type = conversion->GetResultType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01008613 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
8614 << input_type << " -> " << result_type;
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008615 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008616
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008617 if ((input_type == DataType::Type::kReference) || (input_type == DataType::Type::kVoid) ||
8618 (result_type == DataType::Type::kReference) || (result_type == DataType::Type::kVoid)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008619 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
8620 }
8621
8622 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008623 if (!isR6 &&
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008624 ((DataType::IsFloatingPointType(result_type) && input_type == DataType::Type::kInt64) ||
8625 (result_type == DataType::Type::kInt64 && DataType::IsFloatingPointType(input_type)))) {
Serban Constantinescu54ff4822016-07-07 18:03:19 +01008626 call_kind = LocationSummary::kCallOnMainOnly;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008627 }
8628
8629 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
8630
8631 if (call_kind == LocationSummary::kNoCall) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008632 if (DataType::IsFloatingPointType(input_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008633 locations->SetInAt(0, Location::RequiresFpuRegister());
8634 } else {
8635 locations->SetInAt(0, Location::RequiresRegister());
8636 }
8637
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008638 if (DataType::IsFloatingPointType(result_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008639 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
8640 } else {
8641 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8642 }
8643 } else {
8644 InvokeRuntimeCallingConvention calling_convention;
8645
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008646 if (DataType::IsFloatingPointType(input_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008647 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
8648 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008649 DCHECK_EQ(input_type, DataType::Type::kInt64);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008650 locations->SetInAt(0, Location::RegisterPairLocation(
8651 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
8652 }
8653
8654 locations->SetOut(calling_convention.GetReturnLocation(result_type));
8655 }
8656}
8657
8658void InstructionCodeGeneratorMIPS::VisitTypeConversion(HTypeConversion* conversion) {
8659 LocationSummary* locations = conversion->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008660 DataType::Type result_type = conversion->GetResultType();
8661 DataType::Type input_type = conversion->GetInputType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008662 bool has_sign_extension = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008663 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008664
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01008665 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
8666 << input_type << " -> " << result_type;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008667
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008668 if (result_type == DataType::Type::kInt64 && DataType::IsIntegralType(input_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008669 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8670 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
8671 Register src = locations->InAt(0).AsRegister<Register>();
8672
Alexey Frunzea871ef12016-06-27 15:20:11 -07008673 if (dst_low != src) {
8674 __ Move(dst_low, src);
8675 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008676 __ Sra(dst_high, src, 31);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008677 } else if (DataType::IsIntegralType(result_type) && DataType::IsIntegralType(input_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008678 Register dst = locations->Out().AsRegister<Register>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008679 Register src = (input_type == DataType::Type::kInt64)
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008680 ? locations->InAt(0).AsRegisterPairLow<Register>()
8681 : locations->InAt(0).AsRegister<Register>();
8682
8683 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01008684 case DataType::Type::kUint8:
8685 __ Andi(dst, src, 0xFF);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008686 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008687 case DataType::Type::kInt8:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008688 if (has_sign_extension) {
8689 __ Seb(dst, src);
8690 } else {
8691 __ Sll(dst, src, 24);
8692 __ Sra(dst, dst, 24);
8693 }
8694 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01008695 case DataType::Type::kUint16:
8696 __ Andi(dst, src, 0xFFFF);
8697 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008698 case DataType::Type::kInt16:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008699 if (has_sign_extension) {
8700 __ Seh(dst, src);
8701 } else {
8702 __ Sll(dst, src, 16);
8703 __ Sra(dst, dst, 16);
8704 }
8705 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008706 case DataType::Type::kInt32:
Alexey Frunzea871ef12016-06-27 15:20:11 -07008707 if (dst != src) {
8708 __ Move(dst, src);
8709 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008710 break;
8711
8712 default:
8713 LOG(FATAL) << "Unexpected type conversion from " << input_type
8714 << " to " << result_type;
8715 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008716 } else if (DataType::IsFloatingPointType(result_type) && DataType::IsIntegralType(input_type)) {
8717 if (input_type == DataType::Type::kInt64) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008718 if (isR6) {
8719 // cvt.s.l/cvt.d.l requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
8720 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
8721 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
8722 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
8723 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
8724 __ Mtc1(src_low, FTMP);
8725 __ Mthc1(src_high, FTMP);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008726 if (result_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008727 __ Cvtsl(dst, FTMP);
8728 } else {
8729 __ Cvtdl(dst, FTMP);
8730 }
8731 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008732 QuickEntrypointEnum entrypoint =
8733 (result_type == DataType::Type::kFloat32) ? kQuickL2f : kQuickL2d;
Serban Constantinescufca16662016-07-14 09:21:59 +01008734 codegen_->InvokeRuntime(entrypoint, conversion, conversion->GetDexPc());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008735 if (result_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008736 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
8737 } else {
8738 CheckEntrypointTypes<kQuickL2d, double, int64_t>();
8739 }
8740 }
8741 } else {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008742 Register src = locations->InAt(0).AsRegister<Register>();
8743 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
8744 __ Mtc1(src, FTMP);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008745 if (result_type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008746 __ Cvtsw(dst, FTMP);
8747 } else {
8748 __ Cvtdw(dst, FTMP);
8749 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008750 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008751 } else if (DataType::IsIntegralType(result_type) && DataType::IsFloatingPointType(input_type)) {
8752 CHECK(result_type == DataType::Type::kInt32 || result_type == DataType::Type::kInt64);
Lena Djokicf4e23a82017-05-09 15:43:45 +02008753
8754 // When NAN2008=1 (R6), the truncate instruction caps the output at the minimum/maximum
8755 // value of the output type if the input is outside of the range after the truncation or
8756 // produces 0 when the input is a NaN. IOW, the three special cases produce three distinct
8757 // results. This matches the desired float/double-to-int/long conversion exactly.
8758 //
8759 // When NAN2008=0 (R2 and before), the truncate instruction produces the maximum positive
8760 // value when the input is either a NaN or is outside of the range of the output type
8761 // after the truncation. IOW, the three special cases (NaN, too small, too big) produce
8762 // the same result.
8763 //
8764 // The code takes care of the different behaviors by first comparing the input to the
8765 // minimum output value (-2**-63 for truncating to long, -2**-31 for truncating to int).
8766 // If the input is greater than or equal to the minimum, it procedes to the truncate
8767 // instruction, which will handle such an input the same way irrespective of NAN2008.
8768 // Otherwise the input is compared to itself to determine whether it is a NaN or not
8769 // in order to return either zero or the minimum value.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008770 if (result_type == DataType::Type::kInt64) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008771 if (isR6) {
8772 // trunc.l.s/trunc.l.d requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
8773 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
8774 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
8775 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8776 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008777
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008778 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008779 __ TruncLS(FTMP, src);
8780 } else {
8781 __ TruncLD(FTMP, src);
8782 }
8783 __ Mfc1(dst_low, FTMP);
8784 __ Mfhc1(dst_high, FTMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008785 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008786 QuickEntrypointEnum entrypoint =
8787 (input_type == DataType::Type::kFloat32) ? kQuickF2l : kQuickD2l;
Serban Constantinescufca16662016-07-14 09:21:59 +01008788 codegen_->InvokeRuntime(entrypoint, conversion, conversion->GetDexPc());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008789 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008790 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
8791 } else {
8792 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
8793 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008794 }
8795 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008796 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
8797 Register dst = locations->Out().AsRegister<Register>();
8798 MipsLabel truncate;
8799 MipsLabel done;
8800
Lena Djokicf4e23a82017-05-09 15:43:45 +02008801 if (!isR6) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008802 if (input_type == DataType::Type::kFloat32) {
Lena Djokicf4e23a82017-05-09 15:43:45 +02008803 uint32_t min_val = bit_cast<uint32_t, float>(std::numeric_limits<int32_t>::min());
8804 __ LoadConst32(TMP, min_val);
8805 __ Mtc1(TMP, FTMP);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008806 } else {
Lena Djokicf4e23a82017-05-09 15:43:45 +02008807 uint64_t min_val = bit_cast<uint64_t, double>(std::numeric_limits<int32_t>::min());
8808 __ LoadConst32(TMP, High32Bits(min_val));
8809 __ Mtc1(ZERO, FTMP);
8810 __ MoveToFpuHigh(TMP, FTMP);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008811 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008812
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008813 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008814 __ ColeS(0, FTMP, src);
8815 } else {
8816 __ ColeD(0, FTMP, src);
8817 }
8818 __ Bc1t(0, &truncate);
8819
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008820 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008821 __ CeqS(0, src, src);
8822 } else {
8823 __ CeqD(0, src, src);
8824 }
8825 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
8826 __ Movf(dst, ZERO, 0);
Lena Djokicf4e23a82017-05-09 15:43:45 +02008827
8828 __ B(&done);
8829
8830 __ Bind(&truncate);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008831 }
8832
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008833 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008834 __ TruncWS(FTMP, src);
8835 } else {
8836 __ TruncWD(FTMP, src);
8837 }
8838 __ Mfc1(dst, FTMP);
8839
Lena Djokicf4e23a82017-05-09 15:43:45 +02008840 if (!isR6) {
8841 __ Bind(&done);
8842 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008843 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008844 } else if (DataType::IsFloatingPointType(result_type) &&
8845 DataType::IsFloatingPointType(input_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008846 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
8847 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008848 if (result_type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008849 __ Cvtsd(dst, src);
8850 } else {
8851 __ Cvtds(dst, src);
8852 }
8853 } else {
8854 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
8855 << " to " << result_type;
8856 }
8857}
8858
8859void LocationsBuilderMIPS::VisitUShr(HUShr* ushr) {
8860 HandleShift(ushr);
8861}
8862
8863void InstructionCodeGeneratorMIPS::VisitUShr(HUShr* ushr) {
8864 HandleShift(ushr);
8865}
8866
8867void LocationsBuilderMIPS::VisitXor(HXor* instruction) {
8868 HandleBinaryOp(instruction);
8869}
8870
8871void InstructionCodeGeneratorMIPS::VisitXor(HXor* instruction) {
8872 HandleBinaryOp(instruction);
8873}
8874
8875void LocationsBuilderMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
8876 // Nothing to do, this should be removed during prepare for register allocator.
8877 LOG(FATAL) << "Unreachable";
8878}
8879
8880void InstructionCodeGeneratorMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
8881 // Nothing to do, this should be removed during prepare for register allocator.
8882 LOG(FATAL) << "Unreachable";
8883}
8884
8885void LocationsBuilderMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008886 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008887}
8888
8889void InstructionCodeGeneratorMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008890 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008891}
8892
8893void LocationsBuilderMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008894 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008895}
8896
8897void InstructionCodeGeneratorMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008898 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008899}
8900
8901void LocationsBuilderMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008902 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008903}
8904
8905void InstructionCodeGeneratorMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008906 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008907}
8908
8909void LocationsBuilderMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008910 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008911}
8912
8913void InstructionCodeGeneratorMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008914 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008915}
8916
8917void LocationsBuilderMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008918 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008919}
8920
8921void InstructionCodeGeneratorMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008922 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008923}
8924
8925void LocationsBuilderMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008926 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008927}
8928
8929void InstructionCodeGeneratorMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008930 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008931}
8932
8933void LocationsBuilderMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008934 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008935}
8936
8937void InstructionCodeGeneratorMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008938 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008939}
8940
8941void LocationsBuilderMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008942 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008943}
8944
8945void InstructionCodeGeneratorMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008946 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008947}
8948
8949void LocationsBuilderMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008950 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008951}
8952
8953void InstructionCodeGeneratorMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008954 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008955}
8956
8957void LocationsBuilderMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008958 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008959}
8960
8961void InstructionCodeGeneratorMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008962 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008963}
8964
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008965void LocationsBuilderMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
8966 LocationSummary* locations =
8967 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
8968 locations->SetInAt(0, Location::RequiresRegister());
8969}
8970
Alexey Frunze96b66822016-09-10 02:32:44 -07008971void InstructionCodeGeneratorMIPS::GenPackedSwitchWithCompares(Register value_reg,
8972 int32_t lower_bound,
8973 uint32_t num_entries,
8974 HBasicBlock* switch_block,
8975 HBasicBlock* default_block) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008976 // Create a set of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008977 Register temp_reg = TMP;
8978 __ Addiu32(temp_reg, value_reg, -lower_bound);
8979 // Jump to default if index is negative
8980 // Note: We don't check the case that index is positive while value < lower_bound, because in
8981 // this case, index >= num_entries must be true. So that we can save one branch instruction.
8982 __ Bltz(temp_reg, codegen_->GetLabelOf(default_block));
8983
Alexey Frunze96b66822016-09-10 02:32:44 -07008984 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008985 // Jump to successors[0] if value == lower_bound.
8986 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[0]));
8987 int32_t last_index = 0;
8988 for (; num_entries - last_index > 2; last_index += 2) {
8989 __ Addiu(temp_reg, temp_reg, -2);
8990 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
8991 __ Bltz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
8992 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
8993 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 2]));
8994 }
8995 if (num_entries - last_index == 2) {
8996 // The last missing case_value.
8997 __ Addiu(temp_reg, temp_reg, -1);
8998 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008999 }
9000
Vladimir Markof3e0ee22015-12-17 15:23:13 +00009001 // And the default for any other value.
Alexey Frunze96b66822016-09-10 02:32:44 -07009002 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009003 __ B(codegen_->GetLabelOf(default_block));
9004 }
9005}
9006
Alexey Frunze96b66822016-09-10 02:32:44 -07009007void InstructionCodeGeneratorMIPS::GenTableBasedPackedSwitch(Register value_reg,
9008 Register constant_area,
9009 int32_t lower_bound,
9010 uint32_t num_entries,
9011 HBasicBlock* switch_block,
9012 HBasicBlock* default_block) {
9013 // Create a jump table.
9014 std::vector<MipsLabel*> labels(num_entries);
9015 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
9016 for (uint32_t i = 0; i < num_entries; i++) {
9017 labels[i] = codegen_->GetLabelOf(successors[i]);
9018 }
9019 JumpTable* table = __ CreateJumpTable(std::move(labels));
9020
9021 // Is the value in range?
9022 __ Addiu32(TMP, value_reg, -lower_bound);
9023 if (IsInt<16>(static_cast<int32_t>(num_entries))) {
9024 __ Sltiu(AT, TMP, num_entries);
9025 __ Beqz(AT, codegen_->GetLabelOf(default_block));
9026 } else {
9027 __ LoadConst32(AT, num_entries);
9028 __ Bgeu(TMP, AT, codegen_->GetLabelOf(default_block));
9029 }
9030
9031 // We are in the range of the table.
9032 // Load the target address from the jump table, indexing by the value.
9033 __ LoadLabelAddress(AT, constant_area, table->GetLabel());
Chris Larsencd0295d2017-03-31 15:26:54 -07009034 __ ShiftAndAdd(TMP, TMP, AT, 2, TMP);
Alexey Frunze96b66822016-09-10 02:32:44 -07009035 __ Lw(TMP, TMP, 0);
9036 // Compute the absolute target address by adding the table start address
9037 // (the table contains offsets to targets relative to its start).
9038 __ Addu(TMP, TMP, AT);
9039 // And jump.
9040 __ Jr(TMP);
9041 __ NopIfNoReordering();
9042}
9043
9044void InstructionCodeGeneratorMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
9045 int32_t lower_bound = switch_instr->GetStartValue();
9046 uint32_t num_entries = switch_instr->GetNumEntries();
9047 LocationSummary* locations = switch_instr->GetLocations();
9048 Register value_reg = locations->InAt(0).AsRegister<Register>();
9049 HBasicBlock* switch_block = switch_instr->GetBlock();
9050 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
9051
9052 if (codegen_->GetInstructionSetFeatures().IsR6() &&
9053 num_entries > kPackedSwitchJumpTableThreshold) {
9054 // R6 uses PC-relative addressing to access the jump table.
9055 // R2, OTOH, requires an HMipsComputeBaseMethodAddress input to access
9056 // the jump table and it is implemented by changing HPackedSwitch to
9057 // HMipsPackedSwitch, which bears HMipsComputeBaseMethodAddress.
9058 // See VisitMipsPackedSwitch() for the table-based implementation on R2.
9059 GenTableBasedPackedSwitch(value_reg,
9060 ZERO,
9061 lower_bound,
9062 num_entries,
9063 switch_block,
9064 default_block);
9065 } else {
9066 GenPackedSwitchWithCompares(value_reg,
9067 lower_bound,
9068 num_entries,
9069 switch_block,
9070 default_block);
9071 }
9072}
9073
9074void LocationsBuilderMIPS::VisitMipsPackedSwitch(HMipsPackedSwitch* switch_instr) {
9075 LocationSummary* locations =
9076 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
9077 locations->SetInAt(0, Location::RequiresRegister());
9078 // Constant area pointer (HMipsComputeBaseMethodAddress).
9079 locations->SetInAt(1, Location::RequiresRegister());
9080}
9081
9082void InstructionCodeGeneratorMIPS::VisitMipsPackedSwitch(HMipsPackedSwitch* switch_instr) {
9083 int32_t lower_bound = switch_instr->GetStartValue();
9084 uint32_t num_entries = switch_instr->GetNumEntries();
9085 LocationSummary* locations = switch_instr->GetLocations();
9086 Register value_reg = locations->InAt(0).AsRegister<Register>();
9087 Register constant_area = locations->InAt(1).AsRegister<Register>();
9088 HBasicBlock* switch_block = switch_instr->GetBlock();
9089 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
9090
9091 // This is an R2-only path. HPackedSwitch has been changed to
9092 // HMipsPackedSwitch, which bears HMipsComputeBaseMethodAddress
9093 // required to address the jump table relative to PC.
9094 GenTableBasedPackedSwitch(value_reg,
9095 constant_area,
9096 lower_bound,
9097 num_entries,
9098 switch_block,
9099 default_block);
9100}
9101
Alexey Frunzee3fb2452016-05-10 16:08:05 -07009102void LocationsBuilderMIPS::VisitMipsComputeBaseMethodAddress(
9103 HMipsComputeBaseMethodAddress* insn) {
9104 LocationSummary* locations =
9105 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
9106 locations->SetOut(Location::RequiresRegister());
9107}
9108
9109void InstructionCodeGeneratorMIPS::VisitMipsComputeBaseMethodAddress(
9110 HMipsComputeBaseMethodAddress* insn) {
9111 LocationSummary* locations = insn->GetLocations();
9112 Register reg = locations->Out().AsRegister<Register>();
9113
9114 CHECK(!codegen_->GetInstructionSetFeatures().IsR6());
9115
9116 // Generate a dummy PC-relative call to obtain PC.
9117 __ Nal();
9118 // Grab the return address off RA.
9119 __ Move(reg, RA);
9120
9121 // Remember this offset (the obtained PC value) for later use with constant area.
9122 __ BindPcRelBaseLabel();
9123}
9124
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009125void LocationsBuilderMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
9126 // The trampoline uses the same calling convention as dex calling conventions,
9127 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
9128 // the method_idx.
9129 HandleInvoke(invoke);
9130}
9131
9132void InstructionCodeGeneratorMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
9133 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
9134}
9135
Roland Levillain2aba7cd2016-02-03 12:27:20 +00009136void LocationsBuilderMIPS::VisitClassTableGet(HClassTableGet* instruction) {
9137 LocationSummary* locations =
9138 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
9139 locations->SetInAt(0, Location::RequiresRegister());
9140 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00009141}
9142
Roland Levillain2aba7cd2016-02-03 12:27:20 +00009143void InstructionCodeGeneratorMIPS::VisitClassTableGet(HClassTableGet* instruction) {
9144 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00009145 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01009146 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Roland Levillain2aba7cd2016-02-03 12:27:20 +00009147 instruction->GetIndex(), kMipsPointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01009148 __ LoadFromOffset(kLoadWord,
9149 locations->Out().AsRegister<Register>(),
9150 locations->InAt(0).AsRegister<Register>(),
9151 method_offset);
Roland Levillain2aba7cd2016-02-03 12:27:20 +00009152 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01009153 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00009154 instruction->GetIndex(), kMipsPointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00009155 __ LoadFromOffset(kLoadWord,
9156 locations->Out().AsRegister<Register>(),
9157 locations->InAt(0).AsRegister<Register>(),
9158 mirror::Class::ImtPtrOffset(kMipsPointerSize).Uint32Value());
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01009159 __ LoadFromOffset(kLoadWord,
9160 locations->Out().AsRegister<Register>(),
9161 locations->Out().AsRegister<Register>(),
9162 method_offset);
Roland Levillain2aba7cd2016-02-03 12:27:20 +00009163 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00009164}
9165
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009166#undef __
9167#undef QUICK_ENTRY_POINT
9168
9169} // namespace mips
9170} // namespace art