blob: 7350b146f95759d541d14f2ac0b5aff1683616b3 [file] [log] [blame]
Scott Wakelingfe885462016-09-22 10:24:38 +01001/*
2 * Copyright (C) 2016 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_arm_vixl.h"
18
Vladimir Markoeee1c0e2017-04-21 17:58:41 +010019#include "arch/arm/asm_support_arm.h"
Scott Wakelingfe885462016-09-22 10:24:38 +010020#include "arch/arm/instruction_set_features_arm.h"
21#include "art_method.h"
Andreas Gampe5678db52017-06-08 14:11:18 -070022#include "base/bit_utils.h"
23#include "base/bit_utils_iterator.h"
Vladimir Marko94ec2db2017-09-06 17:21:03 +010024#include "class_table.h"
Scott Wakelingfe885462016-09-22 10:24:38 +010025#include "code_generator_utils.h"
26#include "common_arm.h"
27#include "compiled_method.h"
28#include "entrypoints/quick/quick_entrypoints.h"
29#include "gc/accounting/card_table.h"
Andreas Gampe09659c22017-09-18 18:23:32 -070030#include "heap_poisoning.h"
Anton Kirilov5ec62182016-10-13 20:16:02 +010031#include "intrinsics_arm_vixl.h"
Vladimir Markod8dbc8d2017-09-20 13:37:47 +010032#include "linker/linker_patch.h"
Scott Wakelingfe885462016-09-22 10:24:38 +010033#include "mirror/array-inl.h"
34#include "mirror/class-inl.h"
35#include "thread.h"
36#include "utils/arm/assembler_arm_vixl.h"
37#include "utils/arm/managed_register_arm.h"
38#include "utils/assembler.h"
39#include "utils/stack_checks.h"
40
41namespace art {
42namespace arm {
43
44namespace vixl32 = vixl::aarch32;
45using namespace vixl32; // NOLINT(build/namespaces)
46
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +010047using helpers::DRegisterFrom;
Scott Wakelingfe885462016-09-22 10:24:38 +010048using helpers::DWARFReg;
Scott Wakelinga7812ae2016-10-17 10:03:36 +010049using helpers::HighDRegisterFrom;
50using helpers::HighRegisterFrom;
Donghui Bai426b49c2016-11-08 14:55:38 +080051using helpers::InputDRegisterAt;
Scott Wakelingfe885462016-09-22 10:24:38 +010052using helpers::InputOperandAt;
Scott Wakelingc34dba72016-10-03 10:14:44 +010053using helpers::InputRegister;
Scott Wakelinga7812ae2016-10-17 10:03:36 +010054using helpers::InputRegisterAt;
Scott Wakelingfe885462016-09-22 10:24:38 +010055using helpers::InputSRegisterAt;
Anton Kirilov644032c2016-12-06 17:51:43 +000056using helpers::InputVRegister;
Scott Wakelinga7812ae2016-10-17 10:03:36 +010057using helpers::InputVRegisterAt;
Scott Wakelingb77051e2016-11-21 19:46:00 +000058using helpers::Int32ConstantFrom;
Anton Kirilov644032c2016-12-06 17:51:43 +000059using helpers::Int64ConstantFrom;
Scott Wakelinga7812ae2016-10-17 10:03:36 +010060using helpers::LocationFrom;
61using helpers::LowRegisterFrom;
62using helpers::LowSRegisterFrom;
Donghui Bai426b49c2016-11-08 14:55:38 +080063using helpers::OperandFrom;
Scott Wakelinga7812ae2016-10-17 10:03:36 +010064using helpers::OutputRegister;
65using helpers::OutputSRegister;
66using helpers::OutputVRegister;
67using helpers::RegisterFrom;
68using helpers::SRegisterFrom;
Anton Kirilov644032c2016-12-06 17:51:43 +000069using helpers::Uint64ConstantFrom;
Scott Wakelingfe885462016-09-22 10:24:38 +010070
Artem Serov0fb37192016-12-06 18:13:40 +000071using vixl::ExactAssemblyScope;
72using vixl::CodeBufferCheckScope;
73
Scott Wakelingfe885462016-09-22 10:24:38 +010074using RegisterList = vixl32::RegisterList;
75
76static bool ExpectedPairLayout(Location location) {
77 // We expected this for both core and fpu register pairs.
78 return ((location.low() & 1) == 0) && (location.low() + 1 == location.high());
79}
Artem Serovd4cc5b22016-11-04 11:19:09 +000080// Use a local definition to prevent copying mistakes.
81static constexpr size_t kArmWordSize = static_cast<size_t>(kArmPointerSize);
82static constexpr size_t kArmBitsPerWord = kArmWordSize * kBitsPerByte;
Artem Serov551b28f2016-10-18 19:11:30 +010083static constexpr uint32_t kPackedSwitchCompareJumpThreshold = 7;
Scott Wakelingfe885462016-09-22 10:24:38 +010084
Vladimir Markoeee1c0e2017-04-21 17:58:41 +010085// Reference load (except object array loads) is using LDR Rt, [Rn, #offset] which can handle
86// offset < 4KiB. For offsets >= 4KiB, the load shall be emitted as two or more instructions.
87// For the Baker read barrier implementation using link-generated thunks we need to split
88// the offset explicitly.
89constexpr uint32_t kReferenceLoadMinFarOffset = 4 * KB;
90
91// Flags controlling the use of link-time generated thunks for Baker read barriers.
92constexpr bool kBakerReadBarrierLinkTimeThunksEnableForFields = true;
93constexpr bool kBakerReadBarrierLinkTimeThunksEnableForArrays = true;
94constexpr bool kBakerReadBarrierLinkTimeThunksEnableForGcRoots = true;
95
Roland Levillain5daa4952017-07-03 17:23:56 +010096// Using a base helps identify when we hit Marking Register check breakpoints.
97constexpr int kMarkingRegisterCheckBreakCodeBaseCode = 0x10;
98
Scott Wakelingfe885462016-09-22 10:24:38 +010099#ifdef __
100#error "ARM Codegen VIXL macro-assembler macro already defined."
101#endif
102
Scott Wakelingfe885462016-09-22 10:24:38 +0100103// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
104#define __ down_cast<CodeGeneratorARMVIXL*>(codegen)->GetVIXLAssembler()-> // NOLINT
105#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kArmPointerSize, x).Int32Value()
106
107// Marker that code is yet to be, and must, be implemented.
108#define TODO_VIXL32(level) LOG(level) << __PRETTY_FUNCTION__ << " unimplemented "
109
Vladimir Markoeee1c0e2017-04-21 17:58:41 +0100110static inline void ExcludeIPAndBakerCcEntrypointRegister(UseScratchRegisterScope* temps,
111 HInstruction* instruction) {
112 DCHECK(temps->IsAvailable(ip));
113 temps->Exclude(ip);
114 DCHECK(!temps->IsAvailable(kBakerCcEntrypointRegister));
Vladimir Markoeee1c0e2017-04-21 17:58:41 +0100115 DCHECK_NE(instruction->GetLocations()->GetTempCount(), 0u);
116 DCHECK(RegisterFrom(instruction->GetLocations()->GetTemp(
117 instruction->GetLocations()->GetTempCount() - 1u)).Is(kBakerCcEntrypointRegister));
118}
119
120static inline void EmitPlaceholderBne(CodeGeneratorARMVIXL* codegen, vixl32::Label* patch_label) {
121 ExactAssemblyScope eas(codegen->GetVIXLAssembler(), kMaxInstructionSizeInBytes);
122 __ bind(patch_label);
123 vixl32::Label placeholder_label;
124 __ b(ne, EncodingSize(Wide), &placeholder_label); // Placeholder, patched at link-time.
125 __ bind(&placeholder_label);
126}
127
Vladimir Marko88abba22017-05-03 17:09:25 +0100128static inline bool CanEmitNarrowLdr(vixl32::Register rt, vixl32::Register rn, uint32_t offset) {
129 return rt.IsLow() && rn.IsLow() && offset < 32u;
130}
131
Vladimir Markoeee1c0e2017-04-21 17:58:41 +0100132class EmitAdrCode {
133 public:
134 EmitAdrCode(ArmVIXLMacroAssembler* assembler, vixl32::Register rd, vixl32::Label* label)
135 : assembler_(assembler), rd_(rd), label_(label) {
136 ExactAssemblyScope aas(assembler, kMaxInstructionSizeInBytes);
137 adr_location_ = assembler->GetCursorOffset();
138 assembler->adr(EncodingSize(Wide), rd, label);
139 }
140
141 ~EmitAdrCode() {
142 DCHECK(label_->IsBound());
143 // The ADR emitted by the assembler does not set the Thumb mode bit we need.
144 // TODO: Maybe extend VIXL to allow ADR for return address?
145 uint8_t* raw_adr = assembler_->GetBuffer()->GetOffsetAddress<uint8_t*>(adr_location_);
146 // Expecting ADR encoding T3 with `(offset & 1) == 0`.
147 DCHECK_EQ(raw_adr[1] & 0xfbu, 0xf2u); // Check bits 24-31, except 26.
148 DCHECK_EQ(raw_adr[0] & 0xffu, 0x0fu); // Check bits 16-23.
149 DCHECK_EQ(raw_adr[3] & 0x8fu, rd_.GetCode()); // Check bits 8-11 and 15.
150 DCHECK_EQ(raw_adr[2] & 0x01u, 0x00u); // Check bit 0, i.e. the `offset & 1`.
151 // Add the Thumb mode bit.
152 raw_adr[2] |= 0x01u;
153 }
154
155 private:
156 ArmVIXLMacroAssembler* const assembler_;
157 vixl32::Register rd_;
158 vixl32::Label* const label_;
159 int32_t adr_location_;
160};
161
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100162// SaveLiveRegisters and RestoreLiveRegisters from SlowPathCodeARM operate on sets of S registers,
163// for each live D registers they treat two corresponding S registers as live ones.
164//
165// Two following functions (SaveContiguousSRegisterList, RestoreContiguousSRegisterList) build
166// from a list of contiguous S registers a list of contiguous D registers (processing first/last
167// S registers corner cases) and save/restore this new list treating them as D registers.
168// - decreasing code size
169// - avoiding hazards on Cortex-A57, when a pair of S registers for an actual live D register is
170// restored and then used in regular non SlowPath code as D register.
171//
172// For the following example (v means the S register is live):
173// D names: | D0 | D1 | D2 | D4 | ...
174// S names: | S0 | S1 | S2 | S3 | S4 | S5 | S6 | S7 | ...
175// Live? | | v | v | v | v | v | v | | ...
176//
177// S1 and S6 will be saved/restored independently; D registers list (D1, D2) will be processed
178// as D registers.
179//
180// TODO(VIXL): All this code should be unnecessary once the VIXL AArch32 backend provides helpers
181// for lists of floating-point registers.
182static size_t SaveContiguousSRegisterList(size_t first,
183 size_t last,
184 CodeGenerator* codegen,
185 size_t stack_offset) {
186 static_assert(kSRegSizeInBytes == kArmWordSize, "Broken assumption on reg/word sizes.");
187 static_assert(kDRegSizeInBytes == 2 * kArmWordSize, "Broken assumption on reg/word sizes.");
188 DCHECK_LE(first, last);
189 if ((first == last) && (first == 0)) {
190 __ Vstr(vixl32::SRegister(first), MemOperand(sp, stack_offset));
191 return stack_offset + kSRegSizeInBytes;
192 }
193 if (first % 2 == 1) {
194 __ Vstr(vixl32::SRegister(first++), MemOperand(sp, stack_offset));
195 stack_offset += kSRegSizeInBytes;
196 }
197
198 bool save_last = false;
199 if (last % 2 == 0) {
200 save_last = true;
201 --last;
202 }
203
204 if (first < last) {
205 vixl32::DRegister d_reg = vixl32::DRegister(first / 2);
206 DCHECK_EQ((last - first + 1) % 2, 0u);
207 size_t number_of_d_regs = (last - first + 1) / 2;
208
209 if (number_of_d_regs == 1) {
210 __ Vstr(d_reg, MemOperand(sp, stack_offset));
211 } else if (number_of_d_regs > 1) {
212 UseScratchRegisterScope temps(down_cast<CodeGeneratorARMVIXL*>(codegen)->GetVIXLAssembler());
213 vixl32::Register base = sp;
214 if (stack_offset != 0) {
215 base = temps.Acquire();
Scott Wakelingb77051e2016-11-21 19:46:00 +0000216 __ Add(base, sp, Operand::From(stack_offset));
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100217 }
218 __ Vstm(F64, base, NO_WRITE_BACK, DRegisterList(d_reg, number_of_d_regs));
219 }
220 stack_offset += number_of_d_regs * kDRegSizeInBytes;
221 }
222
223 if (save_last) {
224 __ Vstr(vixl32::SRegister(last + 1), MemOperand(sp, stack_offset));
225 stack_offset += kSRegSizeInBytes;
226 }
227
228 return stack_offset;
229}
230
231static size_t RestoreContiguousSRegisterList(size_t first,
232 size_t last,
233 CodeGenerator* codegen,
234 size_t stack_offset) {
235 static_assert(kSRegSizeInBytes == kArmWordSize, "Broken assumption on reg/word sizes.");
236 static_assert(kDRegSizeInBytes == 2 * kArmWordSize, "Broken assumption on reg/word sizes.");
237 DCHECK_LE(first, last);
238 if ((first == last) && (first == 0)) {
239 __ Vldr(vixl32::SRegister(first), MemOperand(sp, stack_offset));
240 return stack_offset + kSRegSizeInBytes;
241 }
242 if (first % 2 == 1) {
243 __ Vldr(vixl32::SRegister(first++), MemOperand(sp, stack_offset));
244 stack_offset += kSRegSizeInBytes;
245 }
246
247 bool restore_last = false;
248 if (last % 2 == 0) {
249 restore_last = true;
250 --last;
251 }
252
253 if (first < last) {
254 vixl32::DRegister d_reg = vixl32::DRegister(first / 2);
255 DCHECK_EQ((last - first + 1) % 2, 0u);
256 size_t number_of_d_regs = (last - first + 1) / 2;
257 if (number_of_d_regs == 1) {
258 __ Vldr(d_reg, MemOperand(sp, stack_offset));
259 } else if (number_of_d_regs > 1) {
260 UseScratchRegisterScope temps(down_cast<CodeGeneratorARMVIXL*>(codegen)->GetVIXLAssembler());
261 vixl32::Register base = sp;
262 if (stack_offset != 0) {
263 base = temps.Acquire();
Scott Wakelingb77051e2016-11-21 19:46:00 +0000264 __ Add(base, sp, Operand::From(stack_offset));
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100265 }
266 __ Vldm(F64, base, NO_WRITE_BACK, DRegisterList(d_reg, number_of_d_regs));
267 }
268 stack_offset += number_of_d_regs * kDRegSizeInBytes;
269 }
270
271 if (restore_last) {
272 __ Vldr(vixl32::SRegister(last + 1), MemOperand(sp, stack_offset));
273 stack_offset += kSRegSizeInBytes;
274 }
275
276 return stack_offset;
277}
278
Vladimir Markod5d2f2c2017-09-26 12:37:26 +0100279static LoadOperandType GetLoadOperandType(DataType::Type type) {
280 switch (type) {
281 case DataType::Type::kReference:
282 return kLoadWord;
283 case DataType::Type::kBool:
284 case DataType::Type::kUint8:
285 return kLoadUnsignedByte;
286 case DataType::Type::kInt8:
287 return kLoadSignedByte;
288 case DataType::Type::kUint16:
289 return kLoadUnsignedHalfword;
290 case DataType::Type::kInt16:
291 return kLoadSignedHalfword;
292 case DataType::Type::kInt32:
293 return kLoadWord;
294 case DataType::Type::kInt64:
295 return kLoadWordPair;
296 case DataType::Type::kFloat32:
297 return kLoadSWord;
298 case DataType::Type::kFloat64:
299 return kLoadDWord;
300 default:
301 LOG(FATAL) << "Unreachable type " << type;
302 UNREACHABLE();
303 }
304}
305
306static StoreOperandType GetStoreOperandType(DataType::Type type) {
307 switch (type) {
308 case DataType::Type::kReference:
309 return kStoreWord;
310 case DataType::Type::kBool:
311 case DataType::Type::kUint8:
312 case DataType::Type::kInt8:
313 return kStoreByte;
314 case DataType::Type::kUint16:
315 case DataType::Type::kInt16:
316 return kStoreHalfword;
317 case DataType::Type::kInt32:
318 return kStoreWord;
319 case DataType::Type::kInt64:
320 return kStoreWordPair;
321 case DataType::Type::kFloat32:
322 return kStoreSWord;
323 case DataType::Type::kFloat64:
324 return kStoreDWord;
325 default:
326 LOG(FATAL) << "Unreachable type " << type;
327 UNREACHABLE();
328 }
329}
330
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100331void SlowPathCodeARMVIXL::SaveLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
332 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
333 size_t orig_offset = stack_offset;
334
335 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ true);
336 for (uint32_t i : LowToHighBits(core_spills)) {
337 // If the register holds an object, update the stack mask.
338 if (locations->RegisterContainsObject(i)) {
339 locations->SetStackBit(stack_offset / kVRegSize);
340 }
341 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
342 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
343 saved_core_stack_offsets_[i] = stack_offset;
344 stack_offset += kArmWordSize;
345 }
346
347 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
348 arm_codegen->GetAssembler()->StoreRegisterList(core_spills, orig_offset);
349
350 uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ false);
351 orig_offset = stack_offset;
352 for (uint32_t i : LowToHighBits(fp_spills)) {
353 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
354 saved_fpu_stack_offsets_[i] = stack_offset;
355 stack_offset += kArmWordSize;
356 }
357
358 stack_offset = orig_offset;
359 while (fp_spills != 0u) {
360 uint32_t begin = CTZ(fp_spills);
361 uint32_t tmp = fp_spills + (1u << begin);
362 fp_spills &= tmp; // Clear the contiguous range of 1s.
363 uint32_t end = (tmp == 0u) ? 32u : CTZ(tmp); // CTZ(0) is undefined.
364 stack_offset = SaveContiguousSRegisterList(begin, end - 1, codegen, stack_offset);
365 }
366 DCHECK_LE(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
367}
368
369void SlowPathCodeARMVIXL::RestoreLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
370 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
371 size_t orig_offset = stack_offset;
372
373 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ true);
374 for (uint32_t i : LowToHighBits(core_spills)) {
375 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
376 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
377 stack_offset += kArmWordSize;
378 }
379
380 // TODO(VIXL): Check the coherency of stack_offset after this with a test.
381 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
382 arm_codegen->GetAssembler()->LoadRegisterList(core_spills, orig_offset);
383
384 uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ false);
385 while (fp_spills != 0u) {
386 uint32_t begin = CTZ(fp_spills);
387 uint32_t tmp = fp_spills + (1u << begin);
388 fp_spills &= tmp; // Clear the contiguous range of 1s.
389 uint32_t end = (tmp == 0u) ? 32u : CTZ(tmp); // CTZ(0) is undefined.
390 stack_offset = RestoreContiguousSRegisterList(begin, end - 1, codegen, stack_offset);
391 }
392 DCHECK_LE(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
393}
394
395class NullCheckSlowPathARMVIXL : public SlowPathCodeARMVIXL {
396 public:
397 explicit NullCheckSlowPathARMVIXL(HNullCheck* instruction) : SlowPathCodeARMVIXL(instruction) {}
398
399 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
400 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
401 __ Bind(GetEntryLabel());
402 if (instruction_->CanThrowIntoCatchBlock()) {
403 // Live registers will be restored in the catch block if caught.
404 SaveLiveRegisters(codegen, instruction_->GetLocations());
405 }
406 arm_codegen->InvokeRuntime(kQuickThrowNullPointer,
407 instruction_,
408 instruction_->GetDexPc(),
409 this);
410 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
411 }
412
413 bool IsFatal() const OVERRIDE { return true; }
414
415 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathARMVIXL"; }
416
417 private:
418 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARMVIXL);
419};
420
Scott Wakelingfe885462016-09-22 10:24:38 +0100421class DivZeroCheckSlowPathARMVIXL : public SlowPathCodeARMVIXL {
422 public:
423 explicit DivZeroCheckSlowPathARMVIXL(HDivZeroCheck* instruction)
424 : SlowPathCodeARMVIXL(instruction) {}
425
426 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100427 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
Scott Wakelingfe885462016-09-22 10:24:38 +0100428 __ Bind(GetEntryLabel());
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100429 arm_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Scott Wakelingfe885462016-09-22 10:24:38 +0100430 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
431 }
432
433 bool IsFatal() const OVERRIDE { return true; }
434
435 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathARMVIXL"; }
436
437 private:
438 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARMVIXL);
439};
440
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100441class SuspendCheckSlowPathARMVIXL : public SlowPathCodeARMVIXL {
442 public:
443 SuspendCheckSlowPathARMVIXL(HSuspendCheck* instruction, HBasicBlock* successor)
444 : SlowPathCodeARMVIXL(instruction), successor_(successor) {}
445
446 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
447 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
448 __ Bind(GetEntryLabel());
449 arm_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
450 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
451 if (successor_ == nullptr) {
452 __ B(GetReturnLabel());
453 } else {
454 __ B(arm_codegen->GetLabelOf(successor_));
455 }
456 }
457
458 vixl32::Label* GetReturnLabel() {
459 DCHECK(successor_ == nullptr);
460 return &return_label_;
461 }
462
463 HBasicBlock* GetSuccessor() const {
464 return successor_;
465 }
466
467 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathARMVIXL"; }
468
469 private:
470 // If not null, the block to branch to after the suspend check.
471 HBasicBlock* const successor_;
472
473 // If `successor_` is null, the label to branch to after the suspend check.
474 vixl32::Label return_label_;
475
476 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARMVIXL);
477};
478
Scott Wakelingc34dba72016-10-03 10:14:44 +0100479class BoundsCheckSlowPathARMVIXL : public SlowPathCodeARMVIXL {
480 public:
481 explicit BoundsCheckSlowPathARMVIXL(HBoundsCheck* instruction)
482 : SlowPathCodeARMVIXL(instruction) {}
483
484 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
485 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
486 LocationSummary* locations = instruction_->GetLocations();
487
488 __ Bind(GetEntryLabel());
489 if (instruction_->CanThrowIntoCatchBlock()) {
490 // Live registers will be restored in the catch block if caught.
491 SaveLiveRegisters(codegen, instruction_->GetLocations());
492 }
493 // We're moving two locations to locations that could overlap, so we need a parallel
494 // move resolver.
495 InvokeRuntimeCallingConventionARMVIXL calling_convention;
496 codegen->EmitParallelMoves(
497 locations->InAt(0),
498 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100499 DataType::Type::kInt32,
Scott Wakelingc34dba72016-10-03 10:14:44 +0100500 locations->InAt(1),
501 LocationFrom(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100502 DataType::Type::kInt32);
Scott Wakelingc34dba72016-10-03 10:14:44 +0100503 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
504 ? kQuickThrowStringBounds
505 : kQuickThrowArrayBounds;
506 arm_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
507 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
508 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
509 }
510
511 bool IsFatal() const OVERRIDE { return true; }
512
513 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathARMVIXL"; }
514
515 private:
516 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARMVIXL);
517};
518
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100519class LoadClassSlowPathARMVIXL : public SlowPathCodeARMVIXL {
520 public:
521 LoadClassSlowPathARMVIXL(HLoadClass* cls, HInstruction* at, uint32_t dex_pc, bool do_clinit)
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000522 : SlowPathCodeARMVIXL(at), cls_(cls), dex_pc_(dex_pc), do_clinit_(do_clinit) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100523 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
524 }
525
526 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000527 LocationSummary* locations = instruction_->GetLocations();
Vladimir Markoea4c1262017-02-06 19:59:33 +0000528 Location out = locations->Out();
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100529
530 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
531 __ Bind(GetEntryLabel());
532 SaveLiveRegisters(codegen, locations);
533
534 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000535 dex::TypeIndex type_index = cls_->GetTypeIndex();
536 __ Mov(calling_convention.GetRegisterAt(0), type_index.index_);
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100537 QuickEntrypointEnum entrypoint = do_clinit_ ? kQuickInitializeStaticStorage
538 : kQuickInitializeType;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000539 arm_codegen->InvokeRuntime(entrypoint, instruction_, dex_pc_, this);
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100540 if (do_clinit_) {
541 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
542 } else {
543 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
544 }
545
546 // Move the class to the desired location.
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100547 if (out.IsValid()) {
548 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
549 arm_codegen->Move32(locations->Out(), LocationFrom(r0));
550 }
551 RestoreLiveRegisters(codegen, locations);
552 __ B(GetExitLabel());
553 }
554
555 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathARMVIXL"; }
556
557 private:
558 // The class this slow path will load.
559 HLoadClass* const cls_;
560
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100561 // The dex PC of `at_`.
562 const uint32_t dex_pc_;
563
564 // Whether to initialize the class.
565 const bool do_clinit_;
566
567 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARMVIXL);
568};
569
Artem Serovd4cc5b22016-11-04 11:19:09 +0000570class LoadStringSlowPathARMVIXL : public SlowPathCodeARMVIXL {
571 public:
572 explicit LoadStringSlowPathARMVIXL(HLoadString* instruction)
573 : SlowPathCodeARMVIXL(instruction) {}
574
575 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Vladimir Markoea4c1262017-02-06 19:59:33 +0000576 DCHECK(instruction_->IsLoadString());
577 DCHECK_EQ(instruction_->AsLoadString()->GetLoadKind(), HLoadString::LoadKind::kBssEntry);
Artem Serovd4cc5b22016-11-04 11:19:09 +0000578 LocationSummary* locations = instruction_->GetLocations();
579 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Vladimir Markof3c52b42017-11-17 17:32:12 +0000580 const dex::StringIndex string_index = instruction_->AsLoadString()->GetStringIndex();
Artem Serovd4cc5b22016-11-04 11:19:09 +0000581
582 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
583 __ Bind(GetEntryLabel());
584 SaveLiveRegisters(codegen, locations);
585
586 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000587 __ Mov(calling_convention.GetRegisterAt(0), string_index.index_);
Artem Serovd4cc5b22016-11-04 11:19:09 +0000588 arm_codegen->InvokeRuntime(kQuickResolveString, instruction_, instruction_->GetDexPc(), this);
589 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
590
Artem Serovd4cc5b22016-11-04 11:19:09 +0000591 arm_codegen->Move32(locations->Out(), LocationFrom(r0));
592 RestoreLiveRegisters(codegen, locations);
593
594 __ B(GetExitLabel());
595 }
596
597 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathARMVIXL"; }
598
599 private:
600 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARMVIXL);
601};
602
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100603class TypeCheckSlowPathARMVIXL : public SlowPathCodeARMVIXL {
604 public:
605 TypeCheckSlowPathARMVIXL(HInstruction* instruction, bool is_fatal)
606 : SlowPathCodeARMVIXL(instruction), is_fatal_(is_fatal) {}
607
608 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
609 LocationSummary* locations = instruction_->GetLocations();
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100610 DCHECK(instruction_->IsCheckCast()
611 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
612
613 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
614 __ Bind(GetEntryLabel());
615
Vladimir Marko87584542017-12-12 17:47:52 +0000616 if (!is_fatal_ || instruction_->CanThrowIntoCatchBlock()) {
Artem Serovcfbe9132016-10-14 15:58:56 +0100617 SaveLiveRegisters(codegen, locations);
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100618 }
619
620 // We're moving two locations to locations that could overlap, so we need a parallel
621 // move resolver.
622 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100623
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800624 codegen->EmitParallelMoves(locations->InAt(0),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800625 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100626 DataType::Type::kReference,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800627 locations->InAt(1),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800628 LocationFrom(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100629 DataType::Type::kReference);
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100630 if (instruction_->IsInstanceOf()) {
Artem Serovcfbe9132016-10-14 15:58:56 +0100631 arm_codegen->InvokeRuntime(kQuickInstanceofNonTrivial,
632 instruction_,
633 instruction_->GetDexPc(),
634 this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800635 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Artem Serovcfbe9132016-10-14 15:58:56 +0100636 arm_codegen->Move32(locations->Out(), LocationFrom(r0));
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100637 } else {
638 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800639 arm_codegen->InvokeRuntime(kQuickCheckInstanceOf,
640 instruction_,
641 instruction_->GetDexPc(),
642 this);
643 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100644 }
645
646 if (!is_fatal_) {
Artem Serovcfbe9132016-10-14 15:58:56 +0100647 RestoreLiveRegisters(codegen, locations);
648 __ B(GetExitLabel());
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100649 }
650 }
651
652 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathARMVIXL"; }
653
654 bool IsFatal() const OVERRIDE { return is_fatal_; }
655
656 private:
657 const bool is_fatal_;
658
659 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARMVIXL);
660};
661
Scott Wakelingc34dba72016-10-03 10:14:44 +0100662class DeoptimizationSlowPathARMVIXL : public SlowPathCodeARMVIXL {
663 public:
664 explicit DeoptimizationSlowPathARMVIXL(HDeoptimize* instruction)
665 : SlowPathCodeARMVIXL(instruction) {}
666
667 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
668 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
669 __ Bind(GetEntryLabel());
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100670 LocationSummary* locations = instruction_->GetLocations();
671 SaveLiveRegisters(codegen, locations);
672 InvokeRuntimeCallingConventionARMVIXL calling_convention;
673 __ Mov(calling_convention.GetRegisterAt(0),
674 static_cast<uint32_t>(instruction_->AsDeoptimize()->GetDeoptimizationKind()));
675
Scott Wakelingc34dba72016-10-03 10:14:44 +0100676 arm_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100677 CheckEntrypointTypes<kQuickDeoptimize, void, DeoptimizationKind>();
Scott Wakelingc34dba72016-10-03 10:14:44 +0100678 }
679
680 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathARMVIXL"; }
681
682 private:
683 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARMVIXL);
684};
685
686class ArraySetSlowPathARMVIXL : public SlowPathCodeARMVIXL {
687 public:
688 explicit ArraySetSlowPathARMVIXL(HInstruction* instruction) : SlowPathCodeARMVIXL(instruction) {}
689
690 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
691 LocationSummary* locations = instruction_->GetLocations();
692 __ Bind(GetEntryLabel());
693 SaveLiveRegisters(codegen, locations);
694
695 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100696 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Scott Wakelingc34dba72016-10-03 10:14:44 +0100697 parallel_move.AddMove(
698 locations->InAt(0),
699 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100700 DataType::Type::kReference,
Scott Wakelingc34dba72016-10-03 10:14:44 +0100701 nullptr);
702 parallel_move.AddMove(
703 locations->InAt(1),
704 LocationFrom(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100705 DataType::Type::kInt32,
Scott Wakelingc34dba72016-10-03 10:14:44 +0100706 nullptr);
707 parallel_move.AddMove(
708 locations->InAt(2),
709 LocationFrom(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100710 DataType::Type::kReference,
Scott Wakelingc34dba72016-10-03 10:14:44 +0100711 nullptr);
712 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
713
714 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
715 arm_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
716 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
717 RestoreLiveRegisters(codegen, locations);
718 __ B(GetExitLabel());
719 }
720
721 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathARMVIXL"; }
722
723 private:
724 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathARMVIXL);
725};
726
Roland Levillain54f869e2017-03-06 13:54:11 +0000727// Abstract base class for read barrier slow paths marking a reference
728// `ref`.
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000729//
Roland Levillain54f869e2017-03-06 13:54:11 +0000730// Argument `entrypoint` must be a register location holding the read
Roland Levillain6d729a72017-06-30 18:34:01 +0100731// barrier marking runtime entry point to be invoked or an empty
732// location; in the latter case, the read barrier marking runtime
733// entry point will be loaded by the slow path code itself.
Roland Levillain54f869e2017-03-06 13:54:11 +0000734class ReadBarrierMarkSlowPathBaseARMVIXL : public SlowPathCodeARMVIXL {
735 protected:
736 ReadBarrierMarkSlowPathBaseARMVIXL(HInstruction* instruction, Location ref, Location entrypoint)
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000737 : SlowPathCodeARMVIXL(instruction), ref_(ref), entrypoint_(entrypoint) {
738 DCHECK(kEmitCompilerReadBarrier);
739 }
740
Roland Levillain54f869e2017-03-06 13:54:11 +0000741 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathBaseARMVIXL"; }
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000742
Roland Levillain54f869e2017-03-06 13:54:11 +0000743 // Generate assembly code calling the read barrier marking runtime
744 // entry point (ReadBarrierMarkRegX).
745 void GenerateReadBarrierMarkRuntimeCall(CodeGenerator* codegen) {
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000746 vixl32::Register ref_reg = RegisterFrom(ref_);
Roland Levillain47b3ab22017-02-27 14:31:35 +0000747
Roland Levillain47b3ab22017-02-27 14:31:35 +0000748 // No need to save live registers; it's taken care of by the
749 // entrypoint. Also, there is no need to update the stack mask,
750 // as this runtime call will not trigger a garbage collection.
751 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
752 DCHECK(!ref_reg.Is(sp));
753 DCHECK(!ref_reg.Is(lr));
754 DCHECK(!ref_reg.Is(pc));
755 // IP is used internally by the ReadBarrierMarkRegX entry point
756 // as a temporary, it cannot be the entry point's input/output.
757 DCHECK(!ref_reg.Is(ip));
758 DCHECK(ref_reg.IsRegister()) << ref_reg;
759 // "Compact" slow path, saving two moves.
760 //
761 // Instead of using the standard runtime calling convention (input
762 // and output in R0):
763 //
764 // R0 <- ref
765 // R0 <- ReadBarrierMark(R0)
766 // ref <- R0
767 //
768 // we just use rX (the register containing `ref`) as input and output
769 // of a dedicated entrypoint:
770 //
771 // rX <- ReadBarrierMarkRegX(rX)
772 //
773 if (entrypoint_.IsValid()) {
774 arm_codegen->ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction_, this);
775 __ Blx(RegisterFrom(entrypoint_));
776 } else {
Roland Levillain54f869e2017-03-06 13:54:11 +0000777 // Entrypoint is not already loaded, load from the thread.
Roland Levillain47b3ab22017-02-27 14:31:35 +0000778 int32_t entry_point_offset =
Roland Levillain97c46462017-05-11 14:04:03 +0100779 Thread::ReadBarrierMarkEntryPointsOffset<kArmPointerSize>(ref_reg.GetCode());
Roland Levillain47b3ab22017-02-27 14:31:35 +0000780 // This runtime call does not require a stack map.
781 arm_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
782 }
Roland Levillain47b3ab22017-02-27 14:31:35 +0000783 }
784
Roland Levillain47b3ab22017-02-27 14:31:35 +0000785 // The location (register) of the marked object reference.
786 const Location ref_;
787
788 // The location of the entrypoint if already loaded.
789 const Location entrypoint_;
790
Roland Levillain54f869e2017-03-06 13:54:11 +0000791 private:
792 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathBaseARMVIXL);
Roland Levillain47b3ab22017-02-27 14:31:35 +0000793};
794
Scott Wakelingc34dba72016-10-03 10:14:44 +0100795// Slow path marking an object reference `ref` during a read
796// barrier. The field `obj.field` in the object `obj` holding this
Roland Levillain54f869e2017-03-06 13:54:11 +0000797// reference does not get updated by this slow path after marking.
Roland Levillain47b3ab22017-02-27 14:31:35 +0000798//
Scott Wakelingc34dba72016-10-03 10:14:44 +0100799// This means that after the execution of this slow path, `ref` will
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000800// always be up-to-date, but `obj.field` may not; i.e., after the
801// flip, `ref` will be a to-space reference, but `obj.field` will
802// probably still be a from-space reference (unless it gets updated by
803// another thread, or if another thread installed another object
804// reference (different from `ref`) in `obj.field`).
Roland Levillainba650a42017-03-06 13:52:32 +0000805//
Roland Levillain6d729a72017-06-30 18:34:01 +0100806// Argument `entrypoint` must be a register location holding the read
807// barrier marking runtime entry point to be invoked or an empty
808// location; in the latter case, the read barrier marking runtime
809// entry point will be loaded by the slow path code itself.
Roland Levillain54f869e2017-03-06 13:54:11 +0000810class ReadBarrierMarkSlowPathARMVIXL : public ReadBarrierMarkSlowPathBaseARMVIXL {
Roland Levillain47b3ab22017-02-27 14:31:35 +0000811 public:
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000812 ReadBarrierMarkSlowPathARMVIXL(HInstruction* instruction,
813 Location ref,
814 Location entrypoint = Location::NoLocation())
Roland Levillain54f869e2017-03-06 13:54:11 +0000815 : ReadBarrierMarkSlowPathBaseARMVIXL(instruction, ref, entrypoint) {
Roland Levillain47b3ab22017-02-27 14:31:35 +0000816 DCHECK(kEmitCompilerReadBarrier);
817 }
818
Roland Levillain47b3ab22017-02-27 14:31:35 +0000819 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathARMVIXL"; }
820
821 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
822 LocationSummary* locations = instruction_->GetLocations();
Roland Levillain54f869e2017-03-06 13:54:11 +0000823 DCHECK(locations->CanCall());
824 DCHECK(ref_.IsRegister()) << ref_;
825 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_.reg())) << ref_.reg();
826 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
827 << "Unexpected instruction in read barrier marking slow path: "
828 << instruction_->DebugName();
829
830 __ Bind(GetEntryLabel());
831 GenerateReadBarrierMarkRuntimeCall(codegen);
832 __ B(GetExitLabel());
833 }
834
835 private:
836 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathARMVIXL);
837};
838
839// Slow path loading `obj`'s lock word, loading a reference from
840// object `*(obj + offset + (index << scale_factor))` into `ref`, and
841// marking `ref` if `obj` is gray according to the lock word (Baker
842// read barrier). The field `obj.field` in the object `obj` holding
843// this reference does not get updated by this slow path after marking
844// (see LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARMVIXL
845// below for that).
846//
847// This means that after the execution of this slow path, `ref` will
848// always be up-to-date, but `obj.field` may not; i.e., after the
849// flip, `ref` will be a to-space reference, but `obj.field` will
850// probably still be a from-space reference (unless it gets updated by
851// another thread, or if another thread installed another object
852// reference (different from `ref`) in `obj.field`).
853//
854// Argument `entrypoint` must be a register location holding the read
Roland Levillain6d729a72017-06-30 18:34:01 +0100855// barrier marking runtime entry point to be invoked or an empty
856// location; in the latter case, the read barrier marking runtime
857// entry point will be loaded by the slow path code itself.
Roland Levillain54f869e2017-03-06 13:54:11 +0000858class LoadReferenceWithBakerReadBarrierSlowPathARMVIXL : public ReadBarrierMarkSlowPathBaseARMVIXL {
859 public:
860 LoadReferenceWithBakerReadBarrierSlowPathARMVIXL(HInstruction* instruction,
861 Location ref,
862 vixl32::Register obj,
863 uint32_t offset,
864 Location index,
865 ScaleFactor scale_factor,
866 bool needs_null_check,
867 vixl32::Register temp,
Roland Levillain6d729a72017-06-30 18:34:01 +0100868 Location entrypoint = Location::NoLocation())
Roland Levillain54f869e2017-03-06 13:54:11 +0000869 : ReadBarrierMarkSlowPathBaseARMVIXL(instruction, ref, entrypoint),
870 obj_(obj),
871 offset_(offset),
872 index_(index),
873 scale_factor_(scale_factor),
874 needs_null_check_(needs_null_check),
875 temp_(temp) {
876 DCHECK(kEmitCompilerReadBarrier);
877 DCHECK(kUseBakerReadBarrier);
878 }
879
Roland Levillain47b3ab22017-02-27 14:31:35 +0000880 const char* GetDescription() const OVERRIDE {
Roland Levillain54f869e2017-03-06 13:54:11 +0000881 return "LoadReferenceWithBakerReadBarrierSlowPathARMVIXL";
Roland Levillain47b3ab22017-02-27 14:31:35 +0000882 }
883
884 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
885 LocationSummary* locations = instruction_->GetLocations();
886 vixl32::Register ref_reg = RegisterFrom(ref_);
887 DCHECK(locations->CanCall());
888 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg.GetCode())) << ref_reg;
Roland Levillain47b3ab22017-02-27 14:31:35 +0000889 DCHECK(instruction_->IsInstanceFieldGet() ||
890 instruction_->IsStaticFieldGet() ||
891 instruction_->IsArrayGet() ||
892 instruction_->IsArraySet() ||
Roland Levillain47b3ab22017-02-27 14:31:35 +0000893 instruction_->IsInstanceOf() ||
894 instruction_->IsCheckCast() ||
895 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
896 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
897 << "Unexpected instruction in read barrier marking slow path: "
898 << instruction_->DebugName();
899 // The read barrier instrumentation of object ArrayGet
900 // instructions does not support the HIntermediateAddress
901 // instruction.
902 DCHECK(!(instruction_->IsArrayGet() &&
903 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
904
Roland Levillain54f869e2017-03-06 13:54:11 +0000905 // Temporary register `temp_`, used to store the lock word, must
906 // not be IP, as we may use it to emit the reference load (in the
907 // call to GenerateRawReferenceLoad below), and we need the lock
908 // word to still be in `temp_` after the reference load.
909 DCHECK(!temp_.Is(ip));
910
Roland Levillain47b3ab22017-02-27 14:31:35 +0000911 __ Bind(GetEntryLabel());
Roland Levillain54f869e2017-03-06 13:54:11 +0000912
913 // When using MaybeGenerateReadBarrierSlow, the read barrier call is
914 // inserted after the original load. However, in fast path based
915 // Baker's read barriers, we need to perform the load of
916 // mirror::Object::monitor_ *before* the original reference load.
917 // This load-load ordering is required by the read barrier.
Roland Levillainff487002017-03-07 16:50:01 +0000918 // The slow path (for Baker's algorithm) should look like:
Roland Levillain54f869e2017-03-06 13:54:11 +0000919 //
920 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
921 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
922 // HeapReference<mirror::Object> ref = *src; // Original reference load.
923 // bool is_gray = (rb_state == ReadBarrier::GrayState());
924 // if (is_gray) {
925 // ref = entrypoint(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
926 // }
927 //
928 // Note: the original implementation in ReadBarrier::Barrier is
929 // slightly more complex as it performs additional checks that we do
930 // not do here for performance reasons.
931
Roland Levillain47b3ab22017-02-27 14:31:35 +0000932 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
Roland Levillain54f869e2017-03-06 13:54:11 +0000933
934 // /* int32_t */ monitor = obj->monitor_
935 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
936 arm_codegen->GetAssembler()->LoadFromOffset(kLoadWord, temp_, obj_, monitor_offset);
937 if (needs_null_check_) {
938 codegen->MaybeRecordImplicitNullCheck(instruction_);
Roland Levillain47b3ab22017-02-27 14:31:35 +0000939 }
Roland Levillain54f869e2017-03-06 13:54:11 +0000940 // /* LockWord */ lock_word = LockWord(monitor)
941 static_assert(sizeof(LockWord) == sizeof(int32_t),
942 "art::LockWord and int32_t have different sizes.");
943
944 // Introduce a dependency on the lock_word including the rb_state,
945 // which shall prevent load-load reordering without using
946 // a memory barrier (which would be more expensive).
947 // `obj` is unchanged by this operation, but its value now depends
948 // on `temp`.
949 __ Add(obj_, obj_, Operand(temp_, ShiftType::LSR, 32));
950
951 // The actual reference load.
952 // A possible implicit null check has already been handled above.
953 arm_codegen->GenerateRawReferenceLoad(
954 instruction_, ref_, obj_, offset_, index_, scale_factor_, /* needs_null_check */ false);
955
956 // Mark the object `ref` when `obj` is gray.
957 //
958 // if (rb_state == ReadBarrier::GrayState())
959 // ref = ReadBarrier::Mark(ref);
960 //
961 // Given the numeric representation, it's enough to check the low bit of the
962 // rb_state. We do that by shifting the bit out of the lock word with LSRS
963 // which can be a 16-bit instruction unlike the TST immediate.
964 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
965 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
966 __ Lsrs(temp_, temp_, LockWord::kReadBarrierStateShift + 1);
967 __ B(cc, GetExitLabel()); // Carry flag is the last bit shifted out by LSRS.
968 GenerateReadBarrierMarkRuntimeCall(codegen);
969
Roland Levillain47b3ab22017-02-27 14:31:35 +0000970 __ B(GetExitLabel());
971 }
972
973 private:
Roland Levillain54f869e2017-03-06 13:54:11 +0000974 // The register containing the object holding the marked object reference field.
975 vixl32::Register obj_;
976 // The offset, index and scale factor to access the reference in `obj_`.
977 uint32_t offset_;
978 Location index_;
979 ScaleFactor scale_factor_;
980 // Is a null check required?
981 bool needs_null_check_;
982 // A temporary register used to hold the lock word of `obj_`.
983 vixl32::Register temp_;
Roland Levillain47b3ab22017-02-27 14:31:35 +0000984
Roland Levillain54f869e2017-03-06 13:54:11 +0000985 DISALLOW_COPY_AND_ASSIGN(LoadReferenceWithBakerReadBarrierSlowPathARMVIXL);
Roland Levillain47b3ab22017-02-27 14:31:35 +0000986};
987
Roland Levillain54f869e2017-03-06 13:54:11 +0000988// Slow path loading `obj`'s lock word, loading a reference from
989// object `*(obj + offset + (index << scale_factor))` into `ref`, and
990// marking `ref` if `obj` is gray according to the lock word (Baker
991// read barrier). If needed, this slow path also atomically updates
992// the field `obj.field` in the object `obj` holding this reference
993// after marking (contrary to
994// LoadReferenceWithBakerReadBarrierSlowPathARMVIXL above, which never
995// tries to update `obj.field`).
Roland Levillain47b3ab22017-02-27 14:31:35 +0000996//
997// This means that after the execution of this slow path, both `ref`
998// and `obj.field` will be up-to-date; i.e., after the flip, both will
999// hold the same to-space reference (unless another thread installed
1000// another object reference (different from `ref`) in `obj.field`).
Roland Levillainba650a42017-03-06 13:52:32 +00001001//
Roland Levillain54f869e2017-03-06 13:54:11 +00001002// Argument `entrypoint` must be a register location holding the read
Roland Levillain6d729a72017-06-30 18:34:01 +01001003// barrier marking runtime entry point to be invoked or an empty
1004// location; in the latter case, the read barrier marking runtime
1005// entry point will be loaded by the slow path code itself.
Roland Levillain54f869e2017-03-06 13:54:11 +00001006class LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARMVIXL
1007 : public ReadBarrierMarkSlowPathBaseARMVIXL {
Roland Levillain47b3ab22017-02-27 14:31:35 +00001008 public:
Roland Levillain6d729a72017-06-30 18:34:01 +01001009 LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARMVIXL(
1010 HInstruction* instruction,
1011 Location ref,
1012 vixl32::Register obj,
1013 uint32_t offset,
1014 Location index,
1015 ScaleFactor scale_factor,
1016 bool needs_null_check,
1017 vixl32::Register temp1,
1018 vixl32::Register temp2,
1019 Location entrypoint = Location::NoLocation())
Roland Levillain54f869e2017-03-06 13:54:11 +00001020 : ReadBarrierMarkSlowPathBaseARMVIXL(instruction, ref, entrypoint),
Roland Levillain47b3ab22017-02-27 14:31:35 +00001021 obj_(obj),
Roland Levillain54f869e2017-03-06 13:54:11 +00001022 offset_(offset),
1023 index_(index),
1024 scale_factor_(scale_factor),
1025 needs_null_check_(needs_null_check),
Roland Levillain47b3ab22017-02-27 14:31:35 +00001026 temp1_(temp1),
Roland Levillain54f869e2017-03-06 13:54:11 +00001027 temp2_(temp2) {
Roland Levillain47b3ab22017-02-27 14:31:35 +00001028 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain54f869e2017-03-06 13:54:11 +00001029 DCHECK(kUseBakerReadBarrier);
Roland Levillain47b3ab22017-02-27 14:31:35 +00001030 }
1031
1032 const char* GetDescription() const OVERRIDE {
Roland Levillain54f869e2017-03-06 13:54:11 +00001033 return "LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARMVIXL";
Roland Levillain47b3ab22017-02-27 14:31:35 +00001034 }
1035
1036 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
1037 LocationSummary* locations = instruction_->GetLocations();
1038 vixl32::Register ref_reg = RegisterFrom(ref_);
1039 DCHECK(locations->CanCall());
1040 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg.GetCode())) << ref_reg;
Roland Levillain54f869e2017-03-06 13:54:11 +00001041 DCHECK_NE(ref_.reg(), LocationFrom(temp1_).reg());
1042
1043 // This slow path is only used by the UnsafeCASObject intrinsic at the moment.
Roland Levillain47b3ab22017-02-27 14:31:35 +00001044 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
1045 << "Unexpected instruction in read barrier marking and field updating slow path: "
1046 << instruction_->DebugName();
1047 DCHECK(instruction_->GetLocations()->Intrinsified());
1048 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
Roland Levillain54f869e2017-03-06 13:54:11 +00001049 DCHECK_EQ(offset_, 0u);
1050 DCHECK_EQ(scale_factor_, ScaleFactor::TIMES_1);
1051 Location field_offset = index_;
1052 DCHECK(field_offset.IsRegisterPair()) << field_offset;
1053
1054 // Temporary register `temp1_`, used to store the lock word, must
1055 // not be IP, as we may use it to emit the reference load (in the
1056 // call to GenerateRawReferenceLoad below), and we need the lock
1057 // word to still be in `temp1_` after the reference load.
1058 DCHECK(!temp1_.Is(ip));
Roland Levillain47b3ab22017-02-27 14:31:35 +00001059
1060 __ Bind(GetEntryLabel());
1061
Roland Levillainff487002017-03-07 16:50:01 +00001062 // The implementation is similar to LoadReferenceWithBakerReadBarrierSlowPathARMVIXL's:
1063 //
1064 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
1065 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
1066 // HeapReference<mirror::Object> ref = *src; // Original reference load.
1067 // bool is_gray = (rb_state == ReadBarrier::GrayState());
1068 // if (is_gray) {
1069 // old_ref = ref;
1070 // ref = entrypoint(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
1071 // compareAndSwapObject(obj, field_offset, old_ref, ref);
1072 // }
1073
Roland Levillain54f869e2017-03-06 13:54:11 +00001074 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
1075
1076 // /* int32_t */ monitor = obj->monitor_
1077 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
1078 arm_codegen->GetAssembler()->LoadFromOffset(kLoadWord, temp1_, obj_, monitor_offset);
1079 if (needs_null_check_) {
1080 codegen->MaybeRecordImplicitNullCheck(instruction_);
1081 }
1082 // /* LockWord */ lock_word = LockWord(monitor)
1083 static_assert(sizeof(LockWord) == sizeof(int32_t),
1084 "art::LockWord and int32_t have different sizes.");
1085
1086 // Introduce a dependency on the lock_word including the rb_state,
1087 // which shall prevent load-load reordering without using
1088 // a memory barrier (which would be more expensive).
1089 // `obj` is unchanged by this operation, but its value now depends
1090 // on `temp`.
1091 __ Add(obj_, obj_, Operand(temp1_, ShiftType::LSR, 32));
1092
1093 // The actual reference load.
1094 // A possible implicit null check has already been handled above.
1095 arm_codegen->GenerateRawReferenceLoad(
1096 instruction_, ref_, obj_, offset_, index_, scale_factor_, /* needs_null_check */ false);
1097
1098 // Mark the object `ref` when `obj` is gray.
1099 //
1100 // if (rb_state == ReadBarrier::GrayState())
1101 // ref = ReadBarrier::Mark(ref);
1102 //
1103 // Given the numeric representation, it's enough to check the low bit of the
1104 // rb_state. We do that by shifting the bit out of the lock word with LSRS
1105 // which can be a 16-bit instruction unlike the TST immediate.
1106 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
1107 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
1108 __ Lsrs(temp1_, temp1_, LockWord::kReadBarrierStateShift + 1);
1109 __ B(cc, GetExitLabel()); // Carry flag is the last bit shifted out by LSRS.
1110
1111 // Save the old value of the reference before marking it.
Roland Levillain47b3ab22017-02-27 14:31:35 +00001112 // Note that we cannot use IP to save the old reference, as IP is
1113 // used internally by the ReadBarrierMarkRegX entry point, and we
1114 // need the old reference after the call to that entry point.
1115 DCHECK(!temp1_.Is(ip));
1116 __ Mov(temp1_, ref_reg);
Roland Levillain27b1f9c2017-01-17 16:56:34 +00001117
Roland Levillain54f869e2017-03-06 13:54:11 +00001118 GenerateReadBarrierMarkRuntimeCall(codegen);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001119
1120 // If the new reference is different from the old reference,
Roland Levillain54f869e2017-03-06 13:54:11 +00001121 // update the field in the holder (`*(obj_ + field_offset)`).
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001122 //
1123 // Note that this field could also hold a different object, if
1124 // another thread had concurrently changed it. In that case, the
Anton Kirilov349e61f2017-12-15 17:11:33 +00001125 // LDREX/CMP/BNE sequence of instructions in the compare-and-set
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001126 // (CAS) operation below would abort the CAS, leaving the field
1127 // as-is.
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001128 __ Cmp(temp1_, ref_reg);
Roland Levillain54f869e2017-03-06 13:54:11 +00001129 __ B(eq, GetExitLabel());
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001130
1131 // Update the the holder's field atomically. This may fail if
1132 // mutator updates before us, but it's OK. This is achieved
1133 // using a strong compare-and-set (CAS) operation with relaxed
1134 // memory synchronization ordering, where the expected value is
1135 // the old reference and the desired value is the new reference.
1136
1137 UseScratchRegisterScope temps(arm_codegen->GetVIXLAssembler());
1138 // Convenience aliases.
1139 vixl32::Register base = obj_;
1140 // The UnsafeCASObject intrinsic uses a register pair as field
1141 // offset ("long offset"), of which only the low part contains
1142 // data.
Roland Levillain54f869e2017-03-06 13:54:11 +00001143 vixl32::Register offset = LowRegisterFrom(field_offset);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001144 vixl32::Register expected = temp1_;
1145 vixl32::Register value = ref_reg;
1146 vixl32::Register tmp_ptr = temps.Acquire(); // Pointer to actual memory.
1147 vixl32::Register tmp = temp2_; // Value in memory.
1148
1149 __ Add(tmp_ptr, base, offset);
1150
1151 if (kPoisonHeapReferences) {
1152 arm_codegen->GetAssembler()->PoisonHeapReference(expected);
1153 if (value.Is(expected)) {
1154 // Do not poison `value`, as it is the same register as
1155 // `expected`, which has just been poisoned.
1156 } else {
1157 arm_codegen->GetAssembler()->PoisonHeapReference(value);
1158 }
1159 }
1160
1161 // do {
1162 // tmp = [r_ptr] - expected;
1163 // } while (tmp == 0 && failure([r_ptr] <- r_new_value));
1164
Anton Kirilov349e61f2017-12-15 17:11:33 +00001165 vixl32::Label loop_head, comparison_failed, exit_loop;
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001166 __ Bind(&loop_head);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001167 __ Ldrex(tmp, MemOperand(tmp_ptr));
Anton Kirilov349e61f2017-12-15 17:11:33 +00001168 __ Cmp(tmp, expected);
1169 __ B(ne, &comparison_failed, /* far_target */ false);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001170 __ Strex(tmp, value, MemOperand(tmp_ptr));
Anton Kirilov349e61f2017-12-15 17:11:33 +00001171 __ CompareAndBranchIfZero(tmp, &exit_loop, /* far_target */ false);
1172 __ B(&loop_head);
1173 __ Bind(&comparison_failed);
1174 __ Clrex();
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001175 __ Bind(&exit_loop);
1176
1177 if (kPoisonHeapReferences) {
1178 arm_codegen->GetAssembler()->UnpoisonHeapReference(expected);
1179 if (value.Is(expected)) {
1180 // Do not unpoison `value`, as it is the same register as
1181 // `expected`, which has just been unpoisoned.
1182 } else {
1183 arm_codegen->GetAssembler()->UnpoisonHeapReference(value);
1184 }
1185 }
1186
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001187 __ B(GetExitLabel());
1188 }
1189
1190 private:
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001191 // The register containing the object holding the marked object reference field.
1192 const vixl32::Register obj_;
Roland Levillain54f869e2017-03-06 13:54:11 +00001193 // The offset, index and scale factor to access the reference in `obj_`.
1194 uint32_t offset_;
1195 Location index_;
1196 ScaleFactor scale_factor_;
1197 // Is a null check required?
1198 bool needs_null_check_;
1199 // A temporary register used to hold the lock word of `obj_`; and
1200 // also to hold the original reference value, when the reference is
1201 // marked.
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001202 const vixl32::Register temp1_;
Roland Levillain54f869e2017-03-06 13:54:11 +00001203 // A temporary register used in the implementation of the CAS, to
1204 // update the object's reference field.
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001205 const vixl32::Register temp2_;
1206
Roland Levillain54f869e2017-03-06 13:54:11 +00001207 DISALLOW_COPY_AND_ASSIGN(LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARMVIXL);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001208};
1209
1210// Slow path generating a read barrier for a heap reference.
1211class ReadBarrierForHeapReferenceSlowPathARMVIXL : public SlowPathCodeARMVIXL {
1212 public:
1213 ReadBarrierForHeapReferenceSlowPathARMVIXL(HInstruction* instruction,
1214 Location out,
1215 Location ref,
1216 Location obj,
1217 uint32_t offset,
1218 Location index)
1219 : SlowPathCodeARMVIXL(instruction),
1220 out_(out),
1221 ref_(ref),
1222 obj_(obj),
1223 offset_(offset),
1224 index_(index) {
1225 DCHECK(kEmitCompilerReadBarrier);
1226 // If `obj` is equal to `out` or `ref`, it means the initial object
1227 // has been overwritten by (or after) the heap object reference load
1228 // to be instrumented, e.g.:
1229 //
1230 // __ LoadFromOffset(kLoadWord, out, out, offset);
1231 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
1232 //
1233 // In that case, we have lost the information about the original
1234 // object, and the emitted read barrier cannot work properly.
1235 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
1236 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
1237 }
1238
1239 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
1240 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
1241 LocationSummary* locations = instruction_->GetLocations();
1242 vixl32::Register reg_out = RegisterFrom(out_);
1243 DCHECK(locations->CanCall());
1244 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out.GetCode()));
1245 DCHECK(instruction_->IsInstanceFieldGet() ||
1246 instruction_->IsStaticFieldGet() ||
1247 instruction_->IsArrayGet() ||
1248 instruction_->IsInstanceOf() ||
1249 instruction_->IsCheckCast() ||
Andreas Gamped9911ee2017-03-27 13:27:24 -07001250 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001251 << "Unexpected instruction in read barrier for heap reference slow path: "
1252 << instruction_->DebugName();
1253 // The read barrier instrumentation of object ArrayGet
1254 // instructions does not support the HIntermediateAddress
1255 // instruction.
1256 DCHECK(!(instruction_->IsArrayGet() &&
1257 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
1258
1259 __ Bind(GetEntryLabel());
1260 SaveLiveRegisters(codegen, locations);
1261
1262 // We may have to change the index's value, but as `index_` is a
1263 // constant member (like other "inputs" of this slow path),
1264 // introduce a copy of it, `index`.
1265 Location index = index_;
1266 if (index_.IsValid()) {
1267 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
1268 if (instruction_->IsArrayGet()) {
1269 // Compute the actual memory offset and store it in `index`.
1270 vixl32::Register index_reg = RegisterFrom(index_);
1271 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg.GetCode()));
1272 if (codegen->IsCoreCalleeSaveRegister(index_reg.GetCode())) {
1273 // We are about to change the value of `index_reg` (see the
Roland Levillain9983e302017-07-14 14:34:22 +01001274 // calls to art::arm::ArmVIXLMacroAssembler::Lsl and
1275 // art::arm::ArmVIXLMacroAssembler::Add below), but it has
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001276 // not been saved by the previous call to
1277 // art::SlowPathCode::SaveLiveRegisters, as it is a
1278 // callee-save register --
1279 // art::SlowPathCode::SaveLiveRegisters does not consider
1280 // callee-save registers, as it has been designed with the
1281 // assumption that callee-save registers are supposed to be
1282 // handled by the called function. So, as a callee-save
1283 // register, `index_reg` _would_ eventually be saved onto
1284 // the stack, but it would be too late: we would have
1285 // changed its value earlier. Therefore, we manually save
1286 // it here into another freely available register,
1287 // `free_reg`, chosen of course among the caller-save
1288 // registers (as a callee-save `free_reg` register would
1289 // exhibit the same problem).
1290 //
1291 // Note we could have requested a temporary register from
1292 // the register allocator instead; but we prefer not to, as
1293 // this is a slow path, and we know we can find a
1294 // caller-save register that is available.
1295 vixl32::Register free_reg = FindAvailableCallerSaveRegister(codegen);
1296 __ Mov(free_reg, index_reg);
1297 index_reg = free_reg;
1298 index = LocationFrom(index_reg);
1299 } else {
1300 // The initial register stored in `index_` has already been
1301 // saved in the call to art::SlowPathCode::SaveLiveRegisters
1302 // (as it is not a callee-save register), so we can freely
1303 // use it.
1304 }
1305 // Shifting the index value contained in `index_reg` by the scale
1306 // factor (2) cannot overflow in practice, as the runtime is
1307 // unable to allocate object arrays with a size larger than
1308 // 2^26 - 1 (that is, 2^28 - 4 bytes).
1309 __ Lsl(index_reg, index_reg, TIMES_4);
1310 static_assert(
1311 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
1312 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
1313 __ Add(index_reg, index_reg, offset_);
1314 } else {
1315 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
1316 // intrinsics, `index_` is not shifted by a scale factor of 2
1317 // (as in the case of ArrayGet), as it is actually an offset
1318 // to an object field within an object.
1319 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
1320 DCHECK(instruction_->GetLocations()->Intrinsified());
1321 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
1322 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
1323 << instruction_->AsInvoke()->GetIntrinsic();
1324 DCHECK_EQ(offset_, 0U);
1325 DCHECK(index_.IsRegisterPair());
1326 // UnsafeGet's offset location is a register pair, the low
1327 // part contains the correct offset.
1328 index = index_.ToLow();
1329 }
1330 }
1331
1332 // We're moving two or three locations to locations that could
1333 // overlap, so we need a parallel move resolver.
1334 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +01001335 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001336 parallel_move.AddMove(ref_,
1337 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001338 DataType::Type::kReference,
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001339 nullptr);
1340 parallel_move.AddMove(obj_,
1341 LocationFrom(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001342 DataType::Type::kReference,
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001343 nullptr);
1344 if (index.IsValid()) {
1345 parallel_move.AddMove(index,
1346 LocationFrom(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001347 DataType::Type::kInt32,
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001348 nullptr);
1349 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
1350 } else {
1351 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
1352 __ Mov(calling_convention.GetRegisterAt(2), offset_);
1353 }
1354 arm_codegen->InvokeRuntime(kQuickReadBarrierSlow, instruction_, instruction_->GetDexPc(), this);
1355 CheckEntrypointTypes<
1356 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
1357 arm_codegen->Move32(out_, LocationFrom(r0));
1358
1359 RestoreLiveRegisters(codegen, locations);
1360 __ B(GetExitLabel());
1361 }
1362
1363 const char* GetDescription() const OVERRIDE {
1364 return "ReadBarrierForHeapReferenceSlowPathARMVIXL";
1365 }
1366
1367 private:
1368 vixl32::Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
1369 uint32_t ref = RegisterFrom(ref_).GetCode();
1370 uint32_t obj = RegisterFrom(obj_).GetCode();
1371 for (uint32_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
1372 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
1373 return vixl32::Register(i);
1374 }
1375 }
1376 // We shall never fail to find a free caller-save register, as
1377 // there are more than two core caller-save registers on ARM
1378 // (meaning it is possible to find one which is different from
1379 // `ref` and `obj`).
1380 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
1381 LOG(FATAL) << "Could not find a free caller-save register";
1382 UNREACHABLE();
1383 }
1384
1385 const Location out_;
1386 const Location ref_;
1387 const Location obj_;
1388 const uint32_t offset_;
1389 // An additional location containing an index to an array.
1390 // Only used for HArrayGet and the UnsafeGetObject &
1391 // UnsafeGetObjectVolatile intrinsics.
1392 const Location index_;
1393
1394 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathARMVIXL);
1395};
1396
1397// Slow path generating a read barrier for a GC root.
1398class ReadBarrierForRootSlowPathARMVIXL : public SlowPathCodeARMVIXL {
1399 public:
1400 ReadBarrierForRootSlowPathARMVIXL(HInstruction* instruction, Location out, Location root)
1401 : SlowPathCodeARMVIXL(instruction), out_(out), root_(root) {
1402 DCHECK(kEmitCompilerReadBarrier);
1403 }
1404
1405 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
1406 LocationSummary* locations = instruction_->GetLocations();
1407 vixl32::Register reg_out = RegisterFrom(out_);
1408 DCHECK(locations->CanCall());
1409 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out.GetCode()));
1410 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
1411 << "Unexpected instruction in read barrier for GC root slow path: "
1412 << instruction_->DebugName();
1413
1414 __ Bind(GetEntryLabel());
1415 SaveLiveRegisters(codegen, locations);
1416
1417 InvokeRuntimeCallingConventionARMVIXL calling_convention;
1418 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
1419 arm_codegen->Move32(LocationFrom(calling_convention.GetRegisterAt(0)), root_);
1420 arm_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
1421 instruction_,
1422 instruction_->GetDexPc(),
1423 this);
1424 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
1425 arm_codegen->Move32(out_, LocationFrom(r0));
1426
1427 RestoreLiveRegisters(codegen, locations);
1428 __ B(GetExitLabel());
1429 }
1430
1431 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathARMVIXL"; }
1432
1433 private:
1434 const Location out_;
1435 const Location root_;
1436
1437 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathARMVIXL);
1438};
Scott Wakelingc34dba72016-10-03 10:14:44 +01001439
Scott Wakelingfe885462016-09-22 10:24:38 +01001440inline vixl32::Condition ARMCondition(IfCondition cond) {
1441 switch (cond) {
1442 case kCondEQ: return eq;
1443 case kCondNE: return ne;
1444 case kCondLT: return lt;
1445 case kCondLE: return le;
1446 case kCondGT: return gt;
1447 case kCondGE: return ge;
1448 case kCondB: return lo;
1449 case kCondBE: return ls;
1450 case kCondA: return hi;
1451 case kCondAE: return hs;
1452 }
1453 LOG(FATAL) << "Unreachable";
1454 UNREACHABLE();
1455}
1456
1457// Maps signed condition to unsigned condition.
1458inline vixl32::Condition ARMUnsignedCondition(IfCondition cond) {
1459 switch (cond) {
1460 case kCondEQ: return eq;
1461 case kCondNE: return ne;
1462 // Signed to unsigned.
1463 case kCondLT: return lo;
1464 case kCondLE: return ls;
1465 case kCondGT: return hi;
1466 case kCondGE: return hs;
1467 // Unsigned remain unchanged.
1468 case kCondB: return lo;
1469 case kCondBE: return ls;
1470 case kCondA: return hi;
1471 case kCondAE: return hs;
1472 }
1473 LOG(FATAL) << "Unreachable";
1474 UNREACHABLE();
1475}
1476
1477inline vixl32::Condition ARMFPCondition(IfCondition cond, bool gt_bias) {
1478 // The ARM condition codes can express all the necessary branches, see the
1479 // "Meaning (floating-point)" column in the table A8-1 of the ARMv7 reference manual.
1480 // There is no dex instruction or HIR that would need the missing conditions
1481 // "equal or unordered" or "not equal".
1482 switch (cond) {
1483 case kCondEQ: return eq;
1484 case kCondNE: return ne /* unordered */;
1485 case kCondLT: return gt_bias ? cc : lt /* unordered */;
1486 case kCondLE: return gt_bias ? ls : le /* unordered */;
1487 case kCondGT: return gt_bias ? hi /* unordered */ : gt;
1488 case kCondGE: return gt_bias ? cs /* unordered */ : ge;
1489 default:
1490 LOG(FATAL) << "UNREACHABLE";
1491 UNREACHABLE();
1492 }
1493}
1494
Anton Kirilov74234da2017-01-13 14:42:47 +00001495inline ShiftType ShiftFromOpKind(HDataProcWithShifterOp::OpKind op_kind) {
1496 switch (op_kind) {
1497 case HDataProcWithShifterOp::kASR: return ShiftType::ASR;
1498 case HDataProcWithShifterOp::kLSL: return ShiftType::LSL;
1499 case HDataProcWithShifterOp::kLSR: return ShiftType::LSR;
1500 default:
1501 LOG(FATAL) << "Unexpected op kind " << op_kind;
1502 UNREACHABLE();
1503 }
1504}
1505
Scott Wakelingfe885462016-09-22 10:24:38 +01001506void CodeGeneratorARMVIXL::DumpCoreRegister(std::ostream& stream, int reg) const {
1507 stream << vixl32::Register(reg);
1508}
1509
1510void CodeGeneratorARMVIXL::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
1511 stream << vixl32::SRegister(reg);
1512}
1513
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001514static uint32_t ComputeSRegisterListMask(const SRegisterList& regs) {
Scott Wakelingfe885462016-09-22 10:24:38 +01001515 uint32_t mask = 0;
1516 for (uint32_t i = regs.GetFirstSRegister().GetCode();
1517 i <= regs.GetLastSRegister().GetCode();
1518 ++i) {
1519 mask |= (1 << i);
1520 }
1521 return mask;
1522}
1523
Artem Serovd4cc5b22016-11-04 11:19:09 +00001524// Saves the register in the stack. Returns the size taken on stack.
1525size_t CodeGeneratorARMVIXL::SaveCoreRegister(size_t stack_index ATTRIBUTE_UNUSED,
1526 uint32_t reg_id ATTRIBUTE_UNUSED) {
1527 TODO_VIXL32(FATAL);
1528 return 0;
1529}
1530
1531// Restores the register from the stack. Returns the size taken on stack.
1532size_t CodeGeneratorARMVIXL::RestoreCoreRegister(size_t stack_index ATTRIBUTE_UNUSED,
1533 uint32_t reg_id ATTRIBUTE_UNUSED) {
1534 TODO_VIXL32(FATAL);
1535 return 0;
1536}
1537
1538size_t CodeGeneratorARMVIXL::SaveFloatingPointRegister(size_t stack_index ATTRIBUTE_UNUSED,
1539 uint32_t reg_id ATTRIBUTE_UNUSED) {
1540 TODO_VIXL32(FATAL);
1541 return 0;
1542}
1543
1544size_t CodeGeneratorARMVIXL::RestoreFloatingPointRegister(size_t stack_index ATTRIBUTE_UNUSED,
1545 uint32_t reg_id ATTRIBUTE_UNUSED) {
1546 TODO_VIXL32(FATAL);
1547 return 0;
Anton Kirilove28d9ae2016-10-25 18:17:23 +01001548}
1549
Anton Kirilov74234da2017-01-13 14:42:47 +00001550static void GenerateDataProcInstruction(HInstruction::InstructionKind kind,
1551 vixl32::Register out,
1552 vixl32::Register first,
1553 const Operand& second,
1554 CodeGeneratorARMVIXL* codegen) {
1555 if (second.IsImmediate() && second.GetImmediate() == 0) {
1556 const Operand in = kind == HInstruction::kAnd
1557 ? Operand(0)
1558 : Operand(first);
1559
1560 __ Mov(out, in);
1561 } else {
1562 switch (kind) {
1563 case HInstruction::kAdd:
1564 __ Add(out, first, second);
1565 break;
1566 case HInstruction::kAnd:
1567 __ And(out, first, second);
1568 break;
1569 case HInstruction::kOr:
1570 __ Orr(out, first, second);
1571 break;
1572 case HInstruction::kSub:
1573 __ Sub(out, first, second);
1574 break;
1575 case HInstruction::kXor:
1576 __ Eor(out, first, second);
1577 break;
1578 default:
1579 LOG(FATAL) << "Unexpected instruction kind: " << kind;
1580 UNREACHABLE();
1581 }
1582 }
1583}
1584
1585static void GenerateDataProc(HInstruction::InstructionKind kind,
1586 const Location& out,
1587 const Location& first,
1588 const Operand& second_lo,
1589 const Operand& second_hi,
1590 CodeGeneratorARMVIXL* codegen) {
1591 const vixl32::Register first_hi = HighRegisterFrom(first);
1592 const vixl32::Register first_lo = LowRegisterFrom(first);
1593 const vixl32::Register out_hi = HighRegisterFrom(out);
1594 const vixl32::Register out_lo = LowRegisterFrom(out);
1595
1596 if (kind == HInstruction::kAdd) {
1597 __ Adds(out_lo, first_lo, second_lo);
1598 __ Adc(out_hi, first_hi, second_hi);
1599 } else if (kind == HInstruction::kSub) {
1600 __ Subs(out_lo, first_lo, second_lo);
1601 __ Sbc(out_hi, first_hi, second_hi);
1602 } else {
1603 GenerateDataProcInstruction(kind, out_lo, first_lo, second_lo, codegen);
1604 GenerateDataProcInstruction(kind, out_hi, first_hi, second_hi, codegen);
1605 }
1606}
1607
1608static Operand GetShifterOperand(vixl32::Register rm, ShiftType shift, uint32_t shift_imm) {
1609 return shift_imm == 0 ? Operand(rm) : Operand(rm, shift, shift_imm);
1610}
1611
1612static void GenerateLongDataProc(HDataProcWithShifterOp* instruction,
1613 CodeGeneratorARMVIXL* codegen) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001614 DCHECK_EQ(instruction->GetType(), DataType::Type::kInt64);
Anton Kirilov74234da2017-01-13 14:42:47 +00001615 DCHECK(HDataProcWithShifterOp::IsShiftOp(instruction->GetOpKind()));
1616
1617 const LocationSummary* const locations = instruction->GetLocations();
1618 const uint32_t shift_value = instruction->GetShiftAmount();
1619 const HInstruction::InstructionKind kind = instruction->GetInstrKind();
1620 const Location first = locations->InAt(0);
1621 const Location second = locations->InAt(1);
1622 const Location out = locations->Out();
1623 const vixl32::Register first_hi = HighRegisterFrom(first);
1624 const vixl32::Register first_lo = LowRegisterFrom(first);
1625 const vixl32::Register out_hi = HighRegisterFrom(out);
1626 const vixl32::Register out_lo = LowRegisterFrom(out);
1627 const vixl32::Register second_hi = HighRegisterFrom(second);
1628 const vixl32::Register second_lo = LowRegisterFrom(second);
1629 const ShiftType shift = ShiftFromOpKind(instruction->GetOpKind());
1630
1631 if (shift_value >= 32) {
1632 if (shift == ShiftType::LSL) {
1633 GenerateDataProcInstruction(kind,
1634 out_hi,
1635 first_hi,
1636 Operand(second_lo, ShiftType::LSL, shift_value - 32),
1637 codegen);
1638 GenerateDataProcInstruction(kind, out_lo, first_lo, 0, codegen);
1639 } else if (shift == ShiftType::ASR) {
1640 GenerateDataProc(kind,
1641 out,
1642 first,
1643 GetShifterOperand(second_hi, ShiftType::ASR, shift_value - 32),
1644 Operand(second_hi, ShiftType::ASR, 31),
1645 codegen);
1646 } else {
1647 DCHECK_EQ(shift, ShiftType::LSR);
1648 GenerateDataProc(kind,
1649 out,
1650 first,
1651 GetShifterOperand(second_hi, ShiftType::LSR, shift_value - 32),
1652 0,
1653 codegen);
1654 }
1655 } else {
1656 DCHECK_GT(shift_value, 1U);
1657 DCHECK_LT(shift_value, 32U);
1658
1659 UseScratchRegisterScope temps(codegen->GetVIXLAssembler());
1660
1661 if (shift == ShiftType::LSL) {
1662 // We are not doing this for HInstruction::kAdd because the output will require
1663 // Location::kOutputOverlap; not applicable to other cases.
1664 if (kind == HInstruction::kOr || kind == HInstruction::kXor) {
1665 GenerateDataProcInstruction(kind,
1666 out_hi,
1667 first_hi,
1668 Operand(second_hi, ShiftType::LSL, shift_value),
1669 codegen);
1670 GenerateDataProcInstruction(kind,
1671 out_hi,
1672 out_hi,
1673 Operand(second_lo, ShiftType::LSR, 32 - shift_value),
1674 codegen);
1675 GenerateDataProcInstruction(kind,
1676 out_lo,
1677 first_lo,
1678 Operand(second_lo, ShiftType::LSL, shift_value),
1679 codegen);
1680 } else {
1681 const vixl32::Register temp = temps.Acquire();
1682
1683 __ Lsl(temp, second_hi, shift_value);
1684 __ Orr(temp, temp, Operand(second_lo, ShiftType::LSR, 32 - shift_value));
1685 GenerateDataProc(kind,
1686 out,
1687 first,
1688 Operand(second_lo, ShiftType::LSL, shift_value),
1689 temp,
1690 codegen);
1691 }
1692 } else {
1693 DCHECK(shift == ShiftType::ASR || shift == ShiftType::LSR);
1694
1695 // We are not doing this for HInstruction::kAdd because the output will require
1696 // Location::kOutputOverlap; not applicable to other cases.
1697 if (kind == HInstruction::kOr || kind == HInstruction::kXor) {
1698 GenerateDataProcInstruction(kind,
1699 out_lo,
1700 first_lo,
1701 Operand(second_lo, ShiftType::LSR, shift_value),
1702 codegen);
1703 GenerateDataProcInstruction(kind,
1704 out_lo,
1705 out_lo,
1706 Operand(second_hi, ShiftType::LSL, 32 - shift_value),
1707 codegen);
1708 GenerateDataProcInstruction(kind,
1709 out_hi,
1710 first_hi,
1711 Operand(second_hi, shift, shift_value),
1712 codegen);
1713 } else {
1714 const vixl32::Register temp = temps.Acquire();
1715
1716 __ Lsr(temp, second_lo, shift_value);
1717 __ Orr(temp, temp, Operand(second_hi, ShiftType::LSL, 32 - shift_value));
1718 GenerateDataProc(kind,
1719 out,
1720 first,
1721 temp,
1722 Operand(second_hi, shift, shift_value),
1723 codegen);
1724 }
1725 }
1726 }
1727}
1728
Donghui Bai426b49c2016-11-08 14:55:38 +08001729static void GenerateVcmp(HInstruction* instruction, CodeGeneratorARMVIXL* codegen) {
1730 const Location rhs_loc = instruction->GetLocations()->InAt(1);
1731 if (rhs_loc.IsConstant()) {
1732 // 0.0 is the only immediate that can be encoded directly in
1733 // a VCMP instruction.
1734 //
1735 // Both the JLS (section 15.20.1) and the JVMS (section 6.5)
1736 // specify that in a floating-point comparison, positive zero
1737 // and negative zero are considered equal, so we can use the
1738 // literal 0.0 for both cases here.
1739 //
1740 // Note however that some methods (Float.equal, Float.compare,
1741 // Float.compareTo, Double.equal, Double.compare,
1742 // Double.compareTo, Math.max, Math.min, StrictMath.max,
1743 // StrictMath.min) consider 0.0 to be (strictly) greater than
1744 // -0.0. So if we ever translate calls to these methods into a
1745 // HCompare instruction, we must handle the -0.0 case with
1746 // care here.
1747 DCHECK(rhs_loc.GetConstant()->IsArithmeticZero());
1748
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001749 const DataType::Type type = instruction->InputAt(0)->GetType();
Donghui Bai426b49c2016-11-08 14:55:38 +08001750
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001751 if (type == DataType::Type::kFloat32) {
Donghui Bai426b49c2016-11-08 14:55:38 +08001752 __ Vcmp(F32, InputSRegisterAt(instruction, 0), 0.0);
1753 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001754 DCHECK_EQ(type, DataType::Type::kFloat64);
Donghui Bai426b49c2016-11-08 14:55:38 +08001755 __ Vcmp(F64, InputDRegisterAt(instruction, 0), 0.0);
1756 }
1757 } else {
1758 __ Vcmp(InputVRegisterAt(instruction, 0), InputVRegisterAt(instruction, 1));
1759 }
1760}
1761
Anton Kirilov5601d4e2017-05-11 19:33:50 +01001762static int64_t AdjustConstantForCondition(int64_t value,
1763 IfCondition* condition,
1764 IfCondition* opposite) {
1765 if (value == 1) {
1766 if (*condition == kCondB) {
1767 value = 0;
1768 *condition = kCondEQ;
1769 *opposite = kCondNE;
1770 } else if (*condition == kCondAE) {
1771 value = 0;
1772 *condition = kCondNE;
1773 *opposite = kCondEQ;
1774 }
1775 } else if (value == -1) {
1776 if (*condition == kCondGT) {
1777 value = 0;
1778 *condition = kCondGE;
1779 *opposite = kCondLT;
1780 } else if (*condition == kCondLE) {
1781 value = 0;
1782 *condition = kCondLT;
1783 *opposite = kCondGE;
1784 }
1785 }
1786
1787 return value;
1788}
1789
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001790static std::pair<vixl32::Condition, vixl32::Condition> GenerateLongTestConstant(
1791 HCondition* condition,
1792 bool invert,
1793 CodeGeneratorARMVIXL* codegen) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001794 DCHECK_EQ(condition->GetLeft()->GetType(), DataType::Type::kInt64);
Donghui Bai426b49c2016-11-08 14:55:38 +08001795
1796 const LocationSummary* const locations = condition->GetLocations();
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001797 IfCondition cond = condition->GetCondition();
1798 IfCondition opposite = condition->GetOppositeCondition();
1799
1800 if (invert) {
1801 std::swap(cond, opposite);
1802 }
1803
1804 std::pair<vixl32::Condition, vixl32::Condition> ret(eq, ne);
Donghui Bai426b49c2016-11-08 14:55:38 +08001805 const Location left = locations->InAt(0);
1806 const Location right = locations->InAt(1);
1807
1808 DCHECK(right.IsConstant());
1809
1810 const vixl32::Register left_high = HighRegisterFrom(left);
1811 const vixl32::Register left_low = LowRegisterFrom(left);
Anton Kirilov5601d4e2017-05-11 19:33:50 +01001812 int64_t value = AdjustConstantForCondition(Int64ConstantFrom(right), &cond, &opposite);
1813 UseScratchRegisterScope temps(codegen->GetVIXLAssembler());
1814
1815 // Comparisons against 0 are common enough to deserve special attention.
1816 if (value == 0) {
1817 switch (cond) {
1818 case kCondNE:
1819 // x > 0 iff x != 0 when the comparison is unsigned.
1820 case kCondA:
1821 ret = std::make_pair(ne, eq);
1822 FALLTHROUGH_INTENDED;
1823 case kCondEQ:
1824 // x <= 0 iff x == 0 when the comparison is unsigned.
1825 case kCondBE:
1826 __ Orrs(temps.Acquire(), left_low, left_high);
1827 return ret;
1828 case kCondLT:
1829 case kCondGE:
1830 __ Cmp(left_high, 0);
1831 return std::make_pair(ARMCondition(cond), ARMCondition(opposite));
1832 // Trivially true or false.
1833 case kCondB:
1834 ret = std::make_pair(ne, eq);
1835 FALLTHROUGH_INTENDED;
1836 case kCondAE:
1837 __ Cmp(left_low, left_low);
1838 return ret;
1839 default:
1840 break;
1841 }
1842 }
Donghui Bai426b49c2016-11-08 14:55:38 +08001843
1844 switch (cond) {
1845 case kCondEQ:
1846 case kCondNE:
1847 case kCondB:
1848 case kCondBE:
1849 case kCondA:
1850 case kCondAE: {
Anton Kirilov23b752b2017-07-20 14:40:44 +01001851 const uint32_t value_low = Low32Bits(value);
1852 Operand operand_low(value_low);
1853
Donghui Bai426b49c2016-11-08 14:55:38 +08001854 __ Cmp(left_high, High32Bits(value));
1855
Anton Kirilov23b752b2017-07-20 14:40:44 +01001856 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
1857 // we must ensure that the operands corresponding to the least significant
1858 // halves of the inputs fit into a 16-bit CMP encoding.
1859 if (!left_low.IsLow() || !IsUint<8>(value_low)) {
1860 operand_low = Operand(temps.Acquire());
1861 __ Mov(LeaveFlags, operand_low.GetBaseRegister(), value_low);
1862 }
1863
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001864 // We use the scope because of the IT block that follows.
Donghui Bai426b49c2016-11-08 14:55:38 +08001865 ExactAssemblyScope guard(codegen->GetVIXLAssembler(),
1866 2 * vixl32::k16BitT32InstructionSizeInBytes,
1867 CodeBufferCheckScope::kExactSize);
1868
1869 __ it(eq);
Anton Kirilov23b752b2017-07-20 14:40:44 +01001870 __ cmp(eq, left_low, operand_low);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001871 ret = std::make_pair(ARMUnsignedCondition(cond), ARMUnsignedCondition(opposite));
Donghui Bai426b49c2016-11-08 14:55:38 +08001872 break;
1873 }
1874 case kCondLE:
1875 case kCondGT:
1876 // Trivially true or false.
1877 if (value == std::numeric_limits<int64_t>::max()) {
1878 __ Cmp(left_low, left_low);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001879 ret = cond == kCondLE ? std::make_pair(eq, ne) : std::make_pair(ne, eq);
Donghui Bai426b49c2016-11-08 14:55:38 +08001880 break;
1881 }
1882
1883 if (cond == kCondLE) {
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001884 DCHECK_EQ(opposite, kCondGT);
Donghui Bai426b49c2016-11-08 14:55:38 +08001885 cond = kCondLT;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001886 opposite = kCondGE;
Donghui Bai426b49c2016-11-08 14:55:38 +08001887 } else {
1888 DCHECK_EQ(cond, kCondGT);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001889 DCHECK_EQ(opposite, kCondLE);
Donghui Bai426b49c2016-11-08 14:55:38 +08001890 cond = kCondGE;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001891 opposite = kCondLT;
Donghui Bai426b49c2016-11-08 14:55:38 +08001892 }
1893
1894 value++;
1895 FALLTHROUGH_INTENDED;
1896 case kCondGE:
1897 case kCondLT: {
Donghui Bai426b49c2016-11-08 14:55:38 +08001898 __ Cmp(left_low, Low32Bits(value));
1899 __ Sbcs(temps.Acquire(), left_high, High32Bits(value));
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001900 ret = std::make_pair(ARMCondition(cond), ARMCondition(opposite));
Donghui Bai426b49c2016-11-08 14:55:38 +08001901 break;
1902 }
1903 default:
1904 LOG(FATAL) << "Unreachable";
1905 UNREACHABLE();
1906 }
1907
1908 return ret;
1909}
1910
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001911static std::pair<vixl32::Condition, vixl32::Condition> GenerateLongTest(
1912 HCondition* condition,
1913 bool invert,
1914 CodeGeneratorARMVIXL* codegen) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001915 DCHECK_EQ(condition->GetLeft()->GetType(), DataType::Type::kInt64);
Donghui Bai426b49c2016-11-08 14:55:38 +08001916
1917 const LocationSummary* const locations = condition->GetLocations();
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001918 IfCondition cond = condition->GetCondition();
1919 IfCondition opposite = condition->GetOppositeCondition();
1920
1921 if (invert) {
1922 std::swap(cond, opposite);
1923 }
1924
1925 std::pair<vixl32::Condition, vixl32::Condition> ret(eq, ne);
Donghui Bai426b49c2016-11-08 14:55:38 +08001926 Location left = locations->InAt(0);
1927 Location right = locations->InAt(1);
1928
1929 DCHECK(right.IsRegisterPair());
1930
1931 switch (cond) {
1932 case kCondEQ:
1933 case kCondNE:
1934 case kCondB:
1935 case kCondBE:
1936 case kCondA:
1937 case kCondAE: {
1938 __ Cmp(HighRegisterFrom(left), HighRegisterFrom(right));
1939
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001940 // We use the scope because of the IT block that follows.
Donghui Bai426b49c2016-11-08 14:55:38 +08001941 ExactAssemblyScope guard(codegen->GetVIXLAssembler(),
1942 2 * vixl32::k16BitT32InstructionSizeInBytes,
1943 CodeBufferCheckScope::kExactSize);
1944
1945 __ it(eq);
1946 __ cmp(eq, LowRegisterFrom(left), LowRegisterFrom(right));
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001947 ret = std::make_pair(ARMUnsignedCondition(cond), ARMUnsignedCondition(opposite));
Donghui Bai426b49c2016-11-08 14:55:38 +08001948 break;
1949 }
1950 case kCondLE:
1951 case kCondGT:
1952 if (cond == kCondLE) {
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001953 DCHECK_EQ(opposite, kCondGT);
Donghui Bai426b49c2016-11-08 14:55:38 +08001954 cond = kCondGE;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001955 opposite = kCondLT;
Donghui Bai426b49c2016-11-08 14:55:38 +08001956 } else {
1957 DCHECK_EQ(cond, kCondGT);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001958 DCHECK_EQ(opposite, kCondLE);
Donghui Bai426b49c2016-11-08 14:55:38 +08001959 cond = kCondLT;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001960 opposite = kCondGE;
Donghui Bai426b49c2016-11-08 14:55:38 +08001961 }
1962
1963 std::swap(left, right);
1964 FALLTHROUGH_INTENDED;
1965 case kCondGE:
1966 case kCondLT: {
1967 UseScratchRegisterScope temps(codegen->GetVIXLAssembler());
1968
1969 __ Cmp(LowRegisterFrom(left), LowRegisterFrom(right));
1970 __ Sbcs(temps.Acquire(), HighRegisterFrom(left), HighRegisterFrom(right));
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001971 ret = std::make_pair(ARMCondition(cond), ARMCondition(opposite));
Donghui Bai426b49c2016-11-08 14:55:38 +08001972 break;
1973 }
1974 default:
1975 LOG(FATAL) << "Unreachable";
1976 UNREACHABLE();
1977 }
1978
1979 return ret;
1980}
1981
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001982static std::pair<vixl32::Condition, vixl32::Condition> GenerateTest(HCondition* condition,
1983 bool invert,
1984 CodeGeneratorARMVIXL* codegen) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001985 const DataType::Type type = condition->GetLeft()->GetType();
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001986 IfCondition cond = condition->GetCondition();
1987 IfCondition opposite = condition->GetOppositeCondition();
1988 std::pair<vixl32::Condition, vixl32::Condition> ret(eq, ne);
Donghui Bai426b49c2016-11-08 14:55:38 +08001989
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001990 if (invert) {
1991 std::swap(cond, opposite);
1992 }
Donghui Bai426b49c2016-11-08 14:55:38 +08001993
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001994 if (type == DataType::Type::kInt64) {
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001995 ret = condition->GetLocations()->InAt(1).IsConstant()
1996 ? GenerateLongTestConstant(condition, invert, codegen)
1997 : GenerateLongTest(condition, invert, codegen);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001998 } else if (DataType::IsFloatingPointType(type)) {
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001999 GenerateVcmp(condition, codegen);
2000 __ Vmrs(RegisterOrAPSR_nzcv(kPcCode), FPSCR);
2001 ret = std::make_pair(ARMFPCondition(cond, condition->IsGtBias()),
2002 ARMFPCondition(opposite, condition->IsGtBias()));
Donghui Bai426b49c2016-11-08 14:55:38 +08002003 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002004 DCHECK(DataType::IsIntegralType(type) || type == DataType::Type::kReference) << type;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002005 __ Cmp(InputRegisterAt(condition, 0), InputOperandAt(condition, 1));
2006 ret = std::make_pair(ARMCondition(cond), ARMCondition(opposite));
Donghui Bai426b49c2016-11-08 14:55:38 +08002007 }
2008
2009 return ret;
2010}
2011
Anton Kirilov5601d4e2017-05-11 19:33:50 +01002012static void GenerateConditionGeneric(HCondition* cond, CodeGeneratorARMVIXL* codegen) {
Anton Kirilov5601d4e2017-05-11 19:33:50 +01002013 const vixl32::Register out = OutputRegister(cond);
2014 const auto condition = GenerateTest(cond, false, codegen);
2015
2016 __ Mov(LeaveFlags, out, 0);
2017
2018 if (out.IsLow()) {
2019 // We use the scope because of the IT block that follows.
2020 ExactAssemblyScope guard(codegen->GetVIXLAssembler(),
2021 2 * vixl32::k16BitT32InstructionSizeInBytes,
2022 CodeBufferCheckScope::kExactSize);
2023
2024 __ it(condition.first);
2025 __ mov(condition.first, out, 1);
2026 } else {
2027 vixl32::Label done_label;
2028 vixl32::Label* const final_label = codegen->GetFinalLabel(cond, &done_label);
2029
2030 __ B(condition.second, final_label, /* far_target */ false);
2031 __ Mov(out, 1);
2032
2033 if (done_label.IsReferenced()) {
2034 __ Bind(&done_label);
2035 }
2036 }
2037}
2038
2039static void GenerateEqualLong(HCondition* cond, CodeGeneratorARMVIXL* codegen) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002040 DCHECK_EQ(cond->GetLeft()->GetType(), DataType::Type::kInt64);
Anton Kirilov5601d4e2017-05-11 19:33:50 +01002041
2042 const LocationSummary* const locations = cond->GetLocations();
2043 IfCondition condition = cond->GetCondition();
2044 const vixl32::Register out = OutputRegister(cond);
2045 const Location left = locations->InAt(0);
2046 const Location right = locations->InAt(1);
2047 vixl32::Register left_high = HighRegisterFrom(left);
2048 vixl32::Register left_low = LowRegisterFrom(left);
2049 vixl32::Register temp;
2050 UseScratchRegisterScope temps(codegen->GetVIXLAssembler());
2051
2052 if (right.IsConstant()) {
2053 IfCondition opposite = cond->GetOppositeCondition();
2054 const int64_t value = AdjustConstantForCondition(Int64ConstantFrom(right),
2055 &condition,
2056 &opposite);
2057 Operand right_high = High32Bits(value);
2058 Operand right_low = Low32Bits(value);
2059
2060 // The output uses Location::kNoOutputOverlap.
2061 if (out.Is(left_high)) {
2062 std::swap(left_low, left_high);
2063 std::swap(right_low, right_high);
2064 }
2065
2066 __ Sub(out, left_low, right_low);
2067 temp = temps.Acquire();
2068 __ Sub(temp, left_high, right_high);
2069 } else {
2070 DCHECK(right.IsRegisterPair());
2071 temp = temps.Acquire();
2072 __ Sub(temp, left_high, HighRegisterFrom(right));
2073 __ Sub(out, left_low, LowRegisterFrom(right));
2074 }
2075
2076 // Need to check after calling AdjustConstantForCondition().
2077 DCHECK(condition == kCondEQ || condition == kCondNE) << condition;
2078
2079 if (condition == kCondNE && out.IsLow()) {
2080 __ Orrs(out, out, temp);
2081
2082 // We use the scope because of the IT block that follows.
2083 ExactAssemblyScope guard(codegen->GetVIXLAssembler(),
2084 2 * vixl32::k16BitT32InstructionSizeInBytes,
2085 CodeBufferCheckScope::kExactSize);
2086
2087 __ it(ne);
2088 __ mov(ne, out, 1);
2089 } else {
2090 __ Orr(out, out, temp);
2091 codegen->GenerateConditionWithZero(condition, out, out, temp);
2092 }
2093}
2094
Anton Kirilov5601d4e2017-05-11 19:33:50 +01002095static void GenerateConditionLong(HCondition* cond, CodeGeneratorARMVIXL* codegen) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002096 DCHECK_EQ(cond->GetLeft()->GetType(), DataType::Type::kInt64);
Anton Kirilov5601d4e2017-05-11 19:33:50 +01002097
2098 const LocationSummary* const locations = cond->GetLocations();
2099 IfCondition condition = cond->GetCondition();
2100 const vixl32::Register out = OutputRegister(cond);
2101 const Location left = locations->InAt(0);
2102 const Location right = locations->InAt(1);
2103
2104 if (right.IsConstant()) {
2105 IfCondition opposite = cond->GetOppositeCondition();
2106
2107 // Comparisons against 0 are common enough to deserve special attention.
2108 if (AdjustConstantForCondition(Int64ConstantFrom(right), &condition, &opposite) == 0) {
2109 switch (condition) {
2110 case kCondNE:
2111 case kCondA:
2112 if (out.IsLow()) {
2113 // We only care if both input registers are 0 or not.
2114 __ Orrs(out, LowRegisterFrom(left), HighRegisterFrom(left));
2115
2116 // We use the scope because of the IT block that follows.
2117 ExactAssemblyScope guard(codegen->GetVIXLAssembler(),
2118 2 * vixl32::k16BitT32InstructionSizeInBytes,
2119 CodeBufferCheckScope::kExactSize);
2120
2121 __ it(ne);
2122 __ mov(ne, out, 1);
2123 return;
2124 }
2125
2126 FALLTHROUGH_INTENDED;
2127 case kCondEQ:
2128 case kCondBE:
2129 // We only care if both input registers are 0 or not.
2130 __ Orr(out, LowRegisterFrom(left), HighRegisterFrom(left));
2131 codegen->GenerateConditionWithZero(condition, out, out);
2132 return;
2133 case kCondLT:
2134 case kCondGE:
2135 // We only care about the sign bit.
2136 FALLTHROUGH_INTENDED;
2137 case kCondAE:
2138 case kCondB:
2139 codegen->GenerateConditionWithZero(condition, out, HighRegisterFrom(left));
2140 return;
2141 case kCondLE:
2142 case kCondGT:
2143 default:
2144 break;
2145 }
2146 }
2147 }
2148
Anton Kirilov23b752b2017-07-20 14:40:44 +01002149 // If `out` is a low register, then the GenerateConditionGeneric()
2150 // function generates a shorter code sequence that is still branchless.
2151 if ((condition == kCondEQ || condition == kCondNE) && !out.IsLow()) {
Anton Kirilov5601d4e2017-05-11 19:33:50 +01002152 GenerateEqualLong(cond, codegen);
2153 return;
2154 }
2155
Anton Kirilov23b752b2017-07-20 14:40:44 +01002156 GenerateConditionGeneric(cond, codegen);
Anton Kirilov5601d4e2017-05-11 19:33:50 +01002157}
2158
Roland Levillain6d729a72017-06-30 18:34:01 +01002159static void GenerateConditionIntegralOrNonPrimitive(HCondition* cond,
2160 CodeGeneratorARMVIXL* codegen) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002161 const DataType::Type type = cond->GetLeft()->GetType();
Anton Kirilov5601d4e2017-05-11 19:33:50 +01002162
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002163 DCHECK(DataType::IsIntegralType(type) || type == DataType::Type::kReference) << type;
Anton Kirilov5601d4e2017-05-11 19:33:50 +01002164
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002165 if (type == DataType::Type::kInt64) {
Anton Kirilov5601d4e2017-05-11 19:33:50 +01002166 GenerateConditionLong(cond, codegen);
2167 return;
2168 }
2169
2170 IfCondition condition = cond->GetCondition();
2171 vixl32::Register in = InputRegisterAt(cond, 0);
2172 const vixl32::Register out = OutputRegister(cond);
2173 const Location right = cond->GetLocations()->InAt(1);
2174 int64_t value;
2175
2176 if (right.IsConstant()) {
2177 IfCondition opposite = cond->GetOppositeCondition();
2178
2179 value = AdjustConstantForCondition(Int64ConstantFrom(right), &condition, &opposite);
2180
2181 // Comparisons against 0 are common enough to deserve special attention.
2182 if (value == 0) {
2183 switch (condition) {
2184 case kCondNE:
2185 case kCondA:
2186 if (out.IsLow() && out.Is(in)) {
2187 __ Cmp(out, 0);
2188
2189 // We use the scope because of the IT block that follows.
2190 ExactAssemblyScope guard(codegen->GetVIXLAssembler(),
2191 2 * vixl32::k16BitT32InstructionSizeInBytes,
2192 CodeBufferCheckScope::kExactSize);
2193
2194 __ it(ne);
2195 __ mov(ne, out, 1);
2196 return;
2197 }
2198
2199 FALLTHROUGH_INTENDED;
2200 case kCondEQ:
2201 case kCondBE:
2202 case kCondLT:
2203 case kCondGE:
2204 case kCondAE:
2205 case kCondB:
2206 codegen->GenerateConditionWithZero(condition, out, in);
2207 return;
2208 case kCondLE:
2209 case kCondGT:
2210 default:
2211 break;
2212 }
2213 }
2214 }
2215
2216 if (condition == kCondEQ || condition == kCondNE) {
2217 Operand operand(0);
2218
2219 if (right.IsConstant()) {
2220 operand = Operand::From(value);
2221 } else if (out.Is(RegisterFrom(right))) {
2222 // Avoid 32-bit instructions if possible.
2223 operand = InputOperandAt(cond, 0);
2224 in = RegisterFrom(right);
2225 } else {
2226 operand = InputOperandAt(cond, 1);
2227 }
2228
2229 if (condition == kCondNE && out.IsLow()) {
2230 __ Subs(out, in, operand);
2231
2232 // We use the scope because of the IT block that follows.
2233 ExactAssemblyScope guard(codegen->GetVIXLAssembler(),
2234 2 * vixl32::k16BitT32InstructionSizeInBytes,
2235 CodeBufferCheckScope::kExactSize);
2236
2237 __ it(ne);
2238 __ mov(ne, out, 1);
2239 } else {
2240 __ Sub(out, in, operand);
2241 codegen->GenerateConditionWithZero(condition, out, out);
2242 }
2243
2244 return;
2245 }
2246
2247 GenerateConditionGeneric(cond, codegen);
2248}
2249
Donghui Bai426b49c2016-11-08 14:55:38 +08002250static bool CanEncodeConstantAs8BitImmediate(HConstant* constant) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002251 const DataType::Type type = constant->GetType();
Donghui Bai426b49c2016-11-08 14:55:38 +08002252 bool ret = false;
2253
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002254 DCHECK(DataType::IsIntegralType(type) || type == DataType::Type::kReference) << type;
Donghui Bai426b49c2016-11-08 14:55:38 +08002255
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002256 if (type == DataType::Type::kInt64) {
Donghui Bai426b49c2016-11-08 14:55:38 +08002257 const uint64_t value = Uint64ConstantFrom(constant);
2258
2259 ret = IsUint<8>(Low32Bits(value)) && IsUint<8>(High32Bits(value));
2260 } else {
2261 ret = IsUint<8>(Int32ConstantFrom(constant));
2262 }
2263
2264 return ret;
2265}
2266
2267static Location Arm8BitEncodableConstantOrRegister(HInstruction* constant) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002268 DCHECK(!DataType::IsFloatingPointType(constant->GetType()));
Donghui Bai426b49c2016-11-08 14:55:38 +08002269
2270 if (constant->IsConstant() && CanEncodeConstantAs8BitImmediate(constant->AsConstant())) {
2271 return Location::ConstantLocation(constant->AsConstant());
2272 }
2273
2274 return Location::RequiresRegister();
2275}
2276
2277static bool CanGenerateConditionalMove(const Location& out, const Location& src) {
2278 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
2279 // we check that we are not dealing with floating-point output (there is no
2280 // 16-bit VMOV encoding).
2281 if (!out.IsRegister() && !out.IsRegisterPair()) {
2282 return false;
2283 }
2284
2285 // For constants, we also check that the output is in one or two low registers,
2286 // and that the constants fit in an 8-bit unsigned integer, so that a 16-bit
2287 // MOV encoding can be used.
2288 if (src.IsConstant()) {
2289 if (!CanEncodeConstantAs8BitImmediate(src.GetConstant())) {
2290 return false;
2291 }
2292
2293 if (out.IsRegister()) {
2294 if (!RegisterFrom(out).IsLow()) {
2295 return false;
2296 }
2297 } else {
2298 DCHECK(out.IsRegisterPair());
2299
2300 if (!HighRegisterFrom(out).IsLow()) {
2301 return false;
2302 }
2303 }
2304 }
2305
2306 return true;
2307}
2308
Scott Wakelingfe885462016-09-22 10:24:38 +01002309#undef __
2310
Donghui Bai426b49c2016-11-08 14:55:38 +08002311vixl32::Label* CodeGeneratorARMVIXL::GetFinalLabel(HInstruction* instruction,
2312 vixl32::Label* final_label) {
2313 DCHECK(!instruction->IsControlFlow() && !instruction->IsSuspendCheck());
Anton Kirilov6f644202017-02-27 18:29:45 +00002314 DCHECK(!instruction->IsInvoke() || !instruction->GetLocations()->CanCall());
Donghui Bai426b49c2016-11-08 14:55:38 +08002315
2316 const HBasicBlock* const block = instruction->GetBlock();
2317 const HLoopInformation* const info = block->GetLoopInformation();
2318 HInstruction* const next = instruction->GetNext();
2319
2320 // Avoid a branch to a branch.
2321 if (next->IsGoto() && (info == nullptr ||
2322 !info->IsBackEdge(*block) ||
2323 !info->HasSuspendCheck())) {
2324 final_label = GetLabelOf(next->AsGoto()->GetSuccessor());
2325 }
2326
2327 return final_label;
2328}
2329
Scott Wakelingfe885462016-09-22 10:24:38 +01002330CodeGeneratorARMVIXL::CodeGeneratorARMVIXL(HGraph* graph,
2331 const ArmInstructionSetFeatures& isa_features,
2332 const CompilerOptions& compiler_options,
2333 OptimizingCompilerStats* stats)
2334 : CodeGenerator(graph,
2335 kNumberOfCoreRegisters,
2336 kNumberOfSRegisters,
2337 kNumberOfRegisterPairs,
2338 kCoreCalleeSaves.GetList(),
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002339 ComputeSRegisterListMask(kFpuCalleeSaves),
Scott Wakelingfe885462016-09-22 10:24:38 +01002340 compiler_options,
2341 stats),
Vladimir Markoca6fff82017-10-03 14:49:14 +01002342 block_labels_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
2343 jump_tables_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Scott Wakelingfe885462016-09-22 10:24:38 +01002344 location_builder_(graph, this),
2345 instruction_visitor_(graph, this),
Vladimir Markoca6fff82017-10-03 14:49:14 +01002346 move_resolver_(graph->GetAllocator(), this),
2347 assembler_(graph->GetAllocator()),
Artem Serovd4cc5b22016-11-04 11:19:09 +00002348 isa_features_(isa_features),
Artem Serovc5fcb442016-12-02 19:19:58 +00002349 uint32_literals_(std::less<uint32_t>(),
Vladimir Markoca6fff82017-10-03 14:49:14 +01002350 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko59eb30f2018-02-20 11:52:34 +00002351 boot_image_method_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +01002352 method_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko59eb30f2018-02-20 11:52:34 +00002353 boot_image_type_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +01002354 type_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko59eb30f2018-02-20 11:52:34 +00002355 boot_image_string_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +01002356 string_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
2357 baker_read_barrier_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Artem Serovc5fcb442016-12-02 19:19:58 +00002358 jit_string_patches_(StringReferenceValueComparator(),
Vladimir Markoca6fff82017-10-03 14:49:14 +01002359 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Artem Serovc5fcb442016-12-02 19:19:58 +00002360 jit_class_patches_(TypeReferenceValueComparator(),
Vladimir Markoca6fff82017-10-03 14:49:14 +01002361 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)) {
Scott Wakelingfe885462016-09-22 10:24:38 +01002362 // Always save the LR register to mimic Quick.
2363 AddAllocatedRegister(Location::RegisterLocation(LR));
Nicolas Geoffray13a797b2017-03-15 16:41:31 +00002364 // Give D30 and D31 as scratch register to VIXL. The register allocator only works on
2365 // S0-S31, which alias to D0-D15.
2366 GetVIXLAssembler()->GetScratchVRegisterList()->Combine(d31);
2367 GetVIXLAssembler()->GetScratchVRegisterList()->Combine(d30);
Scott Wakelingfe885462016-09-22 10:24:38 +01002368}
2369
Artem Serov551b28f2016-10-18 19:11:30 +01002370void JumpTableARMVIXL::EmitTable(CodeGeneratorARMVIXL* codegen) {
2371 uint32_t num_entries = switch_instr_->GetNumEntries();
2372 DCHECK_GE(num_entries, kPackedSwitchCompareJumpThreshold);
2373
2374 // We are about to use the assembler to place literals directly. Make sure we have enough
Scott Wakelingb77051e2016-11-21 19:46:00 +00002375 // underlying code buffer and we have generated a jump table of the right size, using
2376 // codegen->GetVIXLAssembler()->GetBuffer().Align();
Artem Serov0fb37192016-12-06 18:13:40 +00002377 ExactAssemblyScope aas(codegen->GetVIXLAssembler(),
2378 num_entries * sizeof(int32_t),
2379 CodeBufferCheckScope::kMaximumSize);
Artem Serov551b28f2016-10-18 19:11:30 +01002380 // TODO(VIXL): Check that using lower case bind is fine here.
2381 codegen->GetVIXLAssembler()->bind(&table_start_);
Artem Serov09a940d2016-11-11 16:15:11 +00002382 for (uint32_t i = 0; i < num_entries; i++) {
2383 codegen->GetVIXLAssembler()->place(bb_addresses_[i].get());
2384 }
2385}
2386
2387void JumpTableARMVIXL::FixTable(CodeGeneratorARMVIXL* codegen) {
2388 uint32_t num_entries = switch_instr_->GetNumEntries();
2389 DCHECK_GE(num_entries, kPackedSwitchCompareJumpThreshold);
2390
Artem Serov551b28f2016-10-18 19:11:30 +01002391 const ArenaVector<HBasicBlock*>& successors = switch_instr_->GetBlock()->GetSuccessors();
2392 for (uint32_t i = 0; i < num_entries; i++) {
2393 vixl32::Label* target_label = codegen->GetLabelOf(successors[i]);
2394 DCHECK(target_label->IsBound());
2395 int32_t jump_offset = target_label->GetLocation() - table_start_.GetLocation();
2396 // When doing BX to address we need to have lower bit set to 1 in T32.
2397 if (codegen->GetVIXLAssembler()->IsUsingT32()) {
2398 jump_offset++;
2399 }
2400 DCHECK_GT(jump_offset, std::numeric_limits<int32_t>::min());
2401 DCHECK_LE(jump_offset, std::numeric_limits<int32_t>::max());
Artem Serov09a940d2016-11-11 16:15:11 +00002402
Scott Wakelingb77051e2016-11-21 19:46:00 +00002403 bb_addresses_[i].get()->UpdateValue(jump_offset, codegen->GetVIXLAssembler()->GetBuffer());
Artem Serov551b28f2016-10-18 19:11:30 +01002404 }
2405}
2406
Artem Serov09a940d2016-11-11 16:15:11 +00002407void CodeGeneratorARMVIXL::FixJumpTables() {
Artem Serov551b28f2016-10-18 19:11:30 +01002408 for (auto&& jump_table : jump_tables_) {
Artem Serov09a940d2016-11-11 16:15:11 +00002409 jump_table->FixTable(this);
Artem Serov551b28f2016-10-18 19:11:30 +01002410 }
2411}
2412
Andreas Gampeca620d72016-11-08 08:09:33 -08002413#define __ reinterpret_cast<ArmVIXLAssembler*>(GetAssembler())->GetVIXLAssembler()-> // NOLINT
Scott Wakelingfe885462016-09-22 10:24:38 +01002414
2415void CodeGeneratorARMVIXL::Finalize(CodeAllocator* allocator) {
Artem Serov09a940d2016-11-11 16:15:11 +00002416 FixJumpTables();
Scott Wakelingfe885462016-09-22 10:24:38 +01002417 GetAssembler()->FinalizeCode();
2418 CodeGenerator::Finalize(allocator);
Vladimir Markoca1e0382018-04-11 09:58:41 +00002419
2420 // Verify Baker read barrier linker patches.
2421 if (kIsDebugBuild) {
2422 ArrayRef<const uint8_t> code = allocator->GetMemory();
2423 for (const BakerReadBarrierPatchInfo& info : baker_read_barrier_patches_) {
2424 DCHECK(info.label.IsBound());
2425 uint32_t literal_offset = info.label.GetLocation();
2426 DCHECK_ALIGNED(literal_offset, 2u);
2427
2428 auto GetInsn16 = [&code](uint32_t offset) {
2429 DCHECK_ALIGNED(offset, 2u);
2430 return (static_cast<uint32_t>(code[offset + 0]) << 0) +
2431 (static_cast<uint32_t>(code[offset + 1]) << 8);
2432 };
2433 auto GetInsn32 = [=](uint32_t offset) {
2434 return (GetInsn16(offset) << 16) + (GetInsn16(offset + 2u) << 0);
2435 };
2436
2437 uint32_t encoded_data = info.custom_data;
2438 BakerReadBarrierKind kind = BakerReadBarrierKindField::Decode(encoded_data);
2439 // Check that the next instruction matches the expected LDR.
2440 switch (kind) {
2441 case BakerReadBarrierKind::kField: {
2442 BakerReadBarrierWidth width = BakerReadBarrierWidthField::Decode(encoded_data);
2443 if (width == BakerReadBarrierWidth::kWide) {
2444 DCHECK_GE(code.size() - literal_offset, 8u);
2445 uint32_t next_insn = GetInsn32(literal_offset + 4u);
2446 // LDR (immediate), encoding T3, with correct base_reg.
2447 CheckValidReg((next_insn >> 12) & 0xfu); // Check destination register.
2448 const uint32_t base_reg = BakerReadBarrierFirstRegField::Decode(encoded_data);
2449 CHECK_EQ(next_insn & 0xffff0000u, 0xf8d00000u | (base_reg << 16));
2450 } else {
2451 DCHECK_GE(code.size() - literal_offset, 6u);
2452 uint32_t next_insn = GetInsn16(literal_offset + 4u);
2453 // LDR (immediate), encoding T1, with correct base_reg.
2454 CheckValidReg(next_insn & 0x7u); // Check destination register.
2455 const uint32_t base_reg = BakerReadBarrierFirstRegField::Decode(encoded_data);
2456 CHECK_EQ(next_insn & 0xf838u, 0x6800u | (base_reg << 3));
2457 }
2458 break;
2459 }
2460 case BakerReadBarrierKind::kArray: {
2461 DCHECK_GE(code.size() - literal_offset, 8u);
2462 uint32_t next_insn = GetInsn32(literal_offset + 4u);
2463 // LDR (register) with correct base_reg, S=1 and option=011 (LDR Wt, [Xn, Xm, LSL #2]).
2464 CheckValidReg((next_insn >> 12) & 0xfu); // Check destination register.
2465 const uint32_t base_reg = BakerReadBarrierFirstRegField::Decode(encoded_data);
2466 CHECK_EQ(next_insn & 0xffff0ff0u, 0xf8500020u | (base_reg << 16));
2467 CheckValidReg(next_insn & 0xf); // Check index register
2468 break;
2469 }
2470 case BakerReadBarrierKind::kGcRoot: {
2471 BakerReadBarrierWidth width = BakerReadBarrierWidthField::Decode(encoded_data);
2472 if (width == BakerReadBarrierWidth::kWide) {
2473 DCHECK_GE(literal_offset, 4u);
2474 uint32_t prev_insn = GetInsn32(literal_offset - 4u);
2475 // LDR (immediate), encoding T3, with correct root_reg.
2476 const uint32_t root_reg = BakerReadBarrierFirstRegField::Decode(encoded_data);
2477 CHECK_EQ(prev_insn & 0xfff0f000u, 0xf8d00000u | (root_reg << 12));
2478 } else {
2479 DCHECK_GE(literal_offset, 2u);
2480 uint32_t prev_insn = GetInsn16(literal_offset - 2u);
2481 // LDR (immediate), encoding T1, with correct root_reg.
2482 const uint32_t root_reg = BakerReadBarrierFirstRegField::Decode(encoded_data);
2483 CHECK_EQ(prev_insn & 0xf807u, 0x6800u | root_reg);
2484 }
2485 break;
2486 }
2487 default:
2488 LOG(FATAL) << "Unexpected kind: " << static_cast<uint32_t>(kind);
2489 UNREACHABLE();
2490 }
2491 }
2492 }
Scott Wakelingfe885462016-09-22 10:24:38 +01002493}
2494
2495void CodeGeneratorARMVIXL::SetupBlockedRegisters() const {
Scott Wakelingfe885462016-09-22 10:24:38 +01002496 // Stack register, LR and PC are always reserved.
2497 blocked_core_registers_[SP] = true;
2498 blocked_core_registers_[LR] = true;
2499 blocked_core_registers_[PC] = true;
2500
Roland Levillain6d729a72017-06-30 18:34:01 +01002501 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2502 // Reserve marking register.
2503 blocked_core_registers_[MR] = true;
2504 }
2505
Scott Wakelingfe885462016-09-22 10:24:38 +01002506 // Reserve thread register.
2507 blocked_core_registers_[TR] = true;
2508
2509 // Reserve temp register.
2510 blocked_core_registers_[IP] = true;
2511
2512 if (GetGraph()->IsDebuggable()) {
2513 // Stubs do not save callee-save floating point registers. If the graph
2514 // is debuggable, we need to deal with these registers differently. For
2515 // now, just block them.
2516 for (uint32_t i = kFpuCalleeSaves.GetFirstSRegister().GetCode();
2517 i <= kFpuCalleeSaves.GetLastSRegister().GetCode();
2518 ++i) {
2519 blocked_fpu_registers_[i] = true;
2520 }
2521 }
Scott Wakelingfe885462016-09-22 10:24:38 +01002522}
2523
Scott Wakelingfe885462016-09-22 10:24:38 +01002524InstructionCodeGeneratorARMVIXL::InstructionCodeGeneratorARMVIXL(HGraph* graph,
2525 CodeGeneratorARMVIXL* codegen)
2526 : InstructionCodeGenerator(graph, codegen),
2527 assembler_(codegen->GetAssembler()),
2528 codegen_(codegen) {}
2529
2530void CodeGeneratorARMVIXL::ComputeSpillMask() {
2531 core_spill_mask_ = allocated_registers_.GetCoreRegisters() & core_callee_save_mask_;
2532 DCHECK_NE(core_spill_mask_, 0u) << "At least the return address register must be saved";
2533 // There is no easy instruction to restore just the PC on thumb2. We spill and
2534 // restore another arbitrary register.
2535 core_spill_mask_ |= (1 << kCoreAlwaysSpillRegister.GetCode());
2536 fpu_spill_mask_ = allocated_registers_.GetFloatingPointRegisters() & fpu_callee_save_mask_;
2537 // We use vpush and vpop for saving and restoring floating point registers, which take
2538 // a SRegister and the number of registers to save/restore after that SRegister. We
2539 // therefore update the `fpu_spill_mask_` to also contain those registers not allocated,
2540 // but in the range.
2541 if (fpu_spill_mask_ != 0) {
2542 uint32_t least_significant_bit = LeastSignificantBit(fpu_spill_mask_);
2543 uint32_t most_significant_bit = MostSignificantBit(fpu_spill_mask_);
2544 for (uint32_t i = least_significant_bit + 1 ; i < most_significant_bit; ++i) {
2545 fpu_spill_mask_ |= (1 << i);
2546 }
2547 }
2548}
2549
2550void CodeGeneratorARMVIXL::GenerateFrameEntry() {
2551 bool skip_overflow_check =
2552 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kArm);
2553 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
2554 __ Bind(&frame_entry_label_);
2555
Nicolas Geoffray8d728322018-01-18 22:44:32 +00002556 if (GetCompilerOptions().CountHotnessInCompiledCode()) {
2557 UseScratchRegisterScope temps(GetVIXLAssembler());
2558 vixl32::Register temp = temps.Acquire();
2559 __ Ldrh(temp, MemOperand(kMethodRegister, ArtMethod::HotnessCountOffset().Int32Value()));
2560 __ Add(temp, temp, 1);
2561 __ Strh(temp, MemOperand(kMethodRegister, ArtMethod::HotnessCountOffset().Int32Value()));
2562 }
2563
Scott Wakelingfe885462016-09-22 10:24:38 +01002564 if (HasEmptyFrame()) {
2565 return;
2566 }
2567
Scott Wakelingfe885462016-09-22 10:24:38 +01002568 if (!skip_overflow_check) {
xueliang.zhong10049552018-01-31 17:10:36 +00002569 // Using r4 instead of IP saves 2 bytes.
Nicolas Geoffray1a4f3ca2018-01-25 14:07:15 +00002570 UseScratchRegisterScope temps(GetVIXLAssembler());
xueliang.zhong10049552018-01-31 17:10:36 +00002571 vixl32::Register temp;
2572 // TODO: Remove this check when R4 is made a callee-save register
2573 // in ART compiled code (b/72801708). Currently we need to make
2574 // sure r4 is not blocked, e.g. in special purpose
2575 // TestCodeGeneratorARMVIXL; also asserting that r4 is available
2576 // here.
2577 if (!blocked_core_registers_[R4]) {
2578 for (vixl32::Register reg : kParameterCoreRegistersVIXL) {
2579 DCHECK(!reg.Is(r4));
2580 }
2581 DCHECK(!kCoreCalleeSaves.Includes(r4));
2582 temp = r4;
2583 } else {
2584 temp = temps.Acquire();
2585 }
Vladimir Marko33bff252017-11-01 14:35:42 +00002586 __ Sub(temp, sp, Operand::From(GetStackOverflowReservedBytes(InstructionSet::kArm)));
Scott Wakelingfe885462016-09-22 10:24:38 +01002587 // The load must immediately precede RecordPcInfo.
Artem Serov0fb37192016-12-06 18:13:40 +00002588 ExactAssemblyScope aas(GetVIXLAssembler(),
2589 vixl32::kMaxInstructionSizeInBytes,
2590 CodeBufferCheckScope::kMaximumSize);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002591 __ ldr(temp, MemOperand(temp));
2592 RecordPcInfo(nullptr, 0);
Scott Wakelingfe885462016-09-22 10:24:38 +01002593 }
2594
2595 __ Push(RegisterList(core_spill_mask_));
2596 GetAssembler()->cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(core_spill_mask_));
2597 GetAssembler()->cfi().RelOffsetForMany(DWARFReg(kMethodRegister),
2598 0,
2599 core_spill_mask_,
2600 kArmWordSize);
2601 if (fpu_spill_mask_ != 0) {
2602 uint32_t first = LeastSignificantBit(fpu_spill_mask_);
2603
2604 // Check that list is contiguous.
2605 DCHECK_EQ(fpu_spill_mask_ >> CTZ(fpu_spill_mask_), ~0u >> (32 - POPCOUNT(fpu_spill_mask_)));
2606
2607 __ Vpush(SRegisterList(vixl32::SRegister(first), POPCOUNT(fpu_spill_mask_)));
2608 GetAssembler()->cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(fpu_spill_mask_));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002609 GetAssembler()->cfi().RelOffsetForMany(DWARFReg(s0), 0, fpu_spill_mask_, kArmWordSize);
Scott Wakelingfe885462016-09-22 10:24:38 +01002610 }
Scott Wakelingbffdc702016-12-07 17:46:03 +00002611
Scott Wakelingfe885462016-09-22 10:24:38 +01002612 int adjust = GetFrameSize() - FrameEntrySpillSize();
2613 __ Sub(sp, sp, adjust);
2614 GetAssembler()->cfi().AdjustCFAOffset(adjust);
Scott Wakelingbffdc702016-12-07 17:46:03 +00002615
2616 // Save the current method if we need it. Note that we do not
2617 // do this in HCurrentMethod, as the instruction might have been removed
2618 // in the SSA graph.
2619 if (RequiresCurrentMethod()) {
2620 GetAssembler()->StoreToOffset(kStoreWord, kMethodRegister, sp, 0);
2621 }
Nicolas Geoffrayf7893532017-06-15 12:34:36 +01002622
2623 if (GetGraph()->HasShouldDeoptimizeFlag()) {
2624 UseScratchRegisterScope temps(GetVIXLAssembler());
2625 vixl32::Register temp = temps.Acquire();
2626 // Initialize should_deoptimize flag to 0.
2627 __ Mov(temp, 0);
2628 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, GetStackOffsetOfShouldDeoptimizeFlag());
2629 }
Roland Levillain5daa4952017-07-03 17:23:56 +01002630
2631 MaybeGenerateMarkingRegisterCheck(/* code */ 1);
Scott Wakelingfe885462016-09-22 10:24:38 +01002632}
2633
2634void CodeGeneratorARMVIXL::GenerateFrameExit() {
2635 if (HasEmptyFrame()) {
2636 __ Bx(lr);
2637 return;
2638 }
2639 GetAssembler()->cfi().RememberState();
2640 int adjust = GetFrameSize() - FrameEntrySpillSize();
2641 __ Add(sp, sp, adjust);
2642 GetAssembler()->cfi().AdjustCFAOffset(-adjust);
2643 if (fpu_spill_mask_ != 0) {
2644 uint32_t first = LeastSignificantBit(fpu_spill_mask_);
2645
2646 // Check that list is contiguous.
2647 DCHECK_EQ(fpu_spill_mask_ >> CTZ(fpu_spill_mask_), ~0u >> (32 - POPCOUNT(fpu_spill_mask_)));
2648
2649 __ Vpop(SRegisterList(vixl32::SRegister(first), POPCOUNT(fpu_spill_mask_)));
2650 GetAssembler()->cfi().AdjustCFAOffset(
2651 -static_cast<int>(kArmWordSize) * POPCOUNT(fpu_spill_mask_));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002652 GetAssembler()->cfi().RestoreMany(DWARFReg(vixl32::SRegister(0)), fpu_spill_mask_);
Scott Wakelingfe885462016-09-22 10:24:38 +01002653 }
2654 // Pop LR into PC to return.
2655 DCHECK_NE(core_spill_mask_ & (1 << kLrCode), 0U);
2656 uint32_t pop_mask = (core_spill_mask_ & (~(1 << kLrCode))) | 1 << kPcCode;
2657 __ Pop(RegisterList(pop_mask));
2658 GetAssembler()->cfi().RestoreState();
2659 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
2660}
2661
2662void CodeGeneratorARMVIXL::Bind(HBasicBlock* block) {
2663 __ Bind(GetLabelOf(block));
2664}
2665
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002666Location InvokeDexCallingConventionVisitorARMVIXL::GetNextLocation(DataType::Type type) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002667 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002668 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002669 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002670 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002671 case DataType::Type::kInt8:
2672 case DataType::Type::kUint16:
2673 case DataType::Type::kInt16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002674 case DataType::Type::kInt32: {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002675 uint32_t index = gp_index_++;
2676 uint32_t stack_index = stack_index_++;
2677 if (index < calling_convention.GetNumberOfRegisters()) {
2678 return LocationFrom(calling_convention.GetRegisterAt(index));
2679 } else {
2680 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
2681 }
2682 }
2683
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002684 case DataType::Type::kInt64: {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002685 uint32_t index = gp_index_;
2686 uint32_t stack_index = stack_index_;
2687 gp_index_ += 2;
2688 stack_index_ += 2;
2689 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
2690 if (calling_convention.GetRegisterAt(index).Is(r1)) {
2691 // Skip R1, and use R2_R3 instead.
2692 gp_index_++;
2693 index++;
2694 }
2695 }
2696 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
2697 DCHECK_EQ(calling_convention.GetRegisterAt(index).GetCode() + 1,
2698 calling_convention.GetRegisterAt(index + 1).GetCode());
2699
2700 return LocationFrom(calling_convention.GetRegisterAt(index),
2701 calling_convention.GetRegisterAt(index + 1));
2702 } else {
2703 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
2704 }
2705 }
2706
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002707 case DataType::Type::kFloat32: {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002708 uint32_t stack_index = stack_index_++;
2709 if (float_index_ % 2 == 0) {
2710 float_index_ = std::max(double_index_, float_index_);
2711 }
2712 if (float_index_ < calling_convention.GetNumberOfFpuRegisters()) {
2713 return LocationFrom(calling_convention.GetFpuRegisterAt(float_index_++));
2714 } else {
2715 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
2716 }
2717 }
2718
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002719 case DataType::Type::kFloat64: {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002720 double_index_ = std::max(double_index_, RoundUp(float_index_, 2));
2721 uint32_t stack_index = stack_index_;
2722 stack_index_ += 2;
2723 if (double_index_ + 1 < calling_convention.GetNumberOfFpuRegisters()) {
2724 uint32_t index = double_index_;
2725 double_index_ += 2;
2726 Location result = LocationFrom(
2727 calling_convention.GetFpuRegisterAt(index),
2728 calling_convention.GetFpuRegisterAt(index + 1));
2729 DCHECK(ExpectedPairLayout(result));
2730 return result;
2731 } else {
2732 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
2733 }
2734 }
2735
Aart Bik66c158e2018-01-31 12:55:04 -08002736 case DataType::Type::kUint32:
2737 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002738 case DataType::Type::kVoid:
Artem Serovd4cc5b22016-11-04 11:19:09 +00002739 LOG(FATAL) << "Unexpected parameter type " << type;
2740 break;
2741 }
2742 return Location::NoLocation();
2743}
2744
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002745Location InvokeDexCallingConventionVisitorARMVIXL::GetReturnLocation(DataType::Type type) const {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002746 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002747 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002748 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002749 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002750 case DataType::Type::kInt8:
2751 case DataType::Type::kUint16:
2752 case DataType::Type::kInt16:
Aart Bik66c158e2018-01-31 12:55:04 -08002753 case DataType::Type::kUint32:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002754 case DataType::Type::kInt32: {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002755 return LocationFrom(r0);
2756 }
2757
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002758 case DataType::Type::kFloat32: {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002759 return LocationFrom(s0);
2760 }
2761
Aart Bik66c158e2018-01-31 12:55:04 -08002762 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002763 case DataType::Type::kInt64: {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002764 return LocationFrom(r0, r1);
2765 }
2766
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002767 case DataType::Type::kFloat64: {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002768 return LocationFrom(s0, s1);
2769 }
2770
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002771 case DataType::Type::kVoid:
Artem Serovd4cc5b22016-11-04 11:19:09 +00002772 return Location::NoLocation();
2773 }
2774
2775 UNREACHABLE();
2776}
2777
2778Location InvokeDexCallingConventionVisitorARMVIXL::GetMethodLocation() const {
2779 return LocationFrom(kMethodRegister);
2780}
2781
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002782void CodeGeneratorARMVIXL::Move32(Location destination, Location source) {
2783 if (source.Equals(destination)) {
2784 return;
2785 }
2786 if (destination.IsRegister()) {
2787 if (source.IsRegister()) {
2788 __ Mov(RegisterFrom(destination), RegisterFrom(source));
2789 } else if (source.IsFpuRegister()) {
2790 __ Vmov(RegisterFrom(destination), SRegisterFrom(source));
2791 } else {
2792 GetAssembler()->LoadFromOffset(kLoadWord,
2793 RegisterFrom(destination),
2794 sp,
2795 source.GetStackIndex());
2796 }
2797 } else if (destination.IsFpuRegister()) {
2798 if (source.IsRegister()) {
2799 __ Vmov(SRegisterFrom(destination), RegisterFrom(source));
2800 } else if (source.IsFpuRegister()) {
2801 __ Vmov(SRegisterFrom(destination), SRegisterFrom(source));
2802 } else {
2803 GetAssembler()->LoadSFromOffset(SRegisterFrom(destination), sp, source.GetStackIndex());
2804 }
2805 } else {
2806 DCHECK(destination.IsStackSlot()) << destination;
2807 if (source.IsRegister()) {
2808 GetAssembler()->StoreToOffset(kStoreWord,
2809 RegisterFrom(source),
2810 sp,
2811 destination.GetStackIndex());
2812 } else if (source.IsFpuRegister()) {
2813 GetAssembler()->StoreSToOffset(SRegisterFrom(source), sp, destination.GetStackIndex());
2814 } else {
2815 DCHECK(source.IsStackSlot()) << source;
2816 UseScratchRegisterScope temps(GetVIXLAssembler());
2817 vixl32::Register temp = temps.Acquire();
2818 GetAssembler()->LoadFromOffset(kLoadWord, temp, sp, source.GetStackIndex());
2819 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
2820 }
2821 }
2822}
2823
Artem Serovcfbe9132016-10-14 15:58:56 +01002824void CodeGeneratorARMVIXL::MoveConstant(Location location, int32_t value) {
2825 DCHECK(location.IsRegister());
2826 __ Mov(RegisterFrom(location), value);
Scott Wakelingfe885462016-09-22 10:24:38 +01002827}
2828
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002829void CodeGeneratorARMVIXL::MoveLocation(Location dst, Location src, DataType::Type dst_type) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002830 // TODO(VIXL): Maybe refactor to have the 'move' implementation here and use it in
2831 // `ParallelMoveResolverARMVIXL::EmitMove`, as is done in the `arm64` backend.
Vladimir Markoca6fff82017-10-03 14:49:14 +01002832 HParallelMove move(GetGraph()->GetAllocator());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002833 move.AddMove(src, dst, dst_type, nullptr);
2834 GetMoveResolver()->EmitNativeCode(&move);
Scott Wakelingfe885462016-09-22 10:24:38 +01002835}
2836
Artem Serovcfbe9132016-10-14 15:58:56 +01002837void CodeGeneratorARMVIXL::AddLocationAsTemp(Location location, LocationSummary* locations) {
2838 if (location.IsRegister()) {
2839 locations->AddTemp(location);
2840 } else if (location.IsRegisterPair()) {
2841 locations->AddTemp(LocationFrom(LowRegisterFrom(location)));
2842 locations->AddTemp(LocationFrom(HighRegisterFrom(location)));
2843 } else {
2844 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
2845 }
Scott Wakelingfe885462016-09-22 10:24:38 +01002846}
2847
2848void CodeGeneratorARMVIXL::InvokeRuntime(QuickEntrypointEnum entrypoint,
2849 HInstruction* instruction,
2850 uint32_t dex_pc,
2851 SlowPathCode* slow_path) {
2852 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Alexandre Rames374ddf32016-11-04 10:40:49 +00002853 __ Ldr(lr, MemOperand(tr, GetThreadOffset<kArmPointerSize>(entrypoint).Int32Value()));
2854 // Ensure the pc position is recorded immediately after the `blx` instruction.
2855 // blx in T32 has only 16bit encoding that's why a stricter check for the scope is used.
Artem Serov0fb37192016-12-06 18:13:40 +00002856 ExactAssemblyScope aas(GetVIXLAssembler(),
2857 vixl32::k16BitT32InstructionSizeInBytes,
2858 CodeBufferCheckScope::kExactSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00002859 __ blx(lr);
Scott Wakelingfe885462016-09-22 10:24:38 +01002860 if (EntrypointRequiresStackMap(entrypoint)) {
2861 RecordPcInfo(instruction, dex_pc, slow_path);
2862 }
2863}
2864
2865void CodeGeneratorARMVIXL::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
2866 HInstruction* instruction,
2867 SlowPathCode* slow_path) {
2868 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Alexandre Rames374ddf32016-11-04 10:40:49 +00002869 __ Ldr(lr, MemOperand(tr, entry_point_offset));
Scott Wakelingfe885462016-09-22 10:24:38 +01002870 __ Blx(lr);
2871}
2872
Scott Wakelingfe885462016-09-22 10:24:38 +01002873void InstructionCodeGeneratorARMVIXL::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Aart Bika8b8e9b2018-01-09 11:01:02 -08002874 if (successor->IsExitBlock()) {
2875 DCHECK(got->GetPrevious()->AlwaysThrows());
2876 return; // no code needed
2877 }
2878
Scott Wakelingfe885462016-09-22 10:24:38 +01002879 HBasicBlock* block = got->GetBlock();
2880 HInstruction* previous = got->GetPrevious();
2881 HLoopInformation* info = block->GetLoopInformation();
2882
2883 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray8d728322018-01-18 22:44:32 +00002884 if (codegen_->GetCompilerOptions().CountHotnessInCompiledCode()) {
2885 UseScratchRegisterScope temps(GetVIXLAssembler());
2886 vixl32::Register temp = temps.Acquire();
2887 __ Push(vixl32::Register(kMethodRegister));
2888 GetAssembler()->LoadFromOffset(kLoadWord, kMethodRegister, sp, kArmWordSize);
2889 __ Ldrh(temp, MemOperand(kMethodRegister, ArtMethod::HotnessCountOffset().Int32Value()));
2890 __ Add(temp, temp, 1);
2891 __ Strh(temp, MemOperand(kMethodRegister, ArtMethod::HotnessCountOffset().Int32Value()));
2892 __ Pop(vixl32::Register(kMethodRegister));
2893 }
Scott Wakelingfe885462016-09-22 10:24:38 +01002894 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2895 return;
2896 }
2897 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2898 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
Roland Levillain5daa4952017-07-03 17:23:56 +01002899 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 2);
Scott Wakelingfe885462016-09-22 10:24:38 +01002900 }
2901 if (!codegen_->GoesToNextBlock(block, successor)) {
2902 __ B(codegen_->GetLabelOf(successor));
2903 }
2904}
2905
2906void LocationsBuilderARMVIXL::VisitGoto(HGoto* got) {
2907 got->SetLocations(nullptr);
2908}
2909
2910void InstructionCodeGeneratorARMVIXL::VisitGoto(HGoto* got) {
2911 HandleGoto(got, got->GetSuccessor());
2912}
2913
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002914void LocationsBuilderARMVIXL::VisitTryBoundary(HTryBoundary* try_boundary) {
2915 try_boundary->SetLocations(nullptr);
2916}
2917
2918void InstructionCodeGeneratorARMVIXL::VisitTryBoundary(HTryBoundary* try_boundary) {
2919 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2920 if (!successor->IsExitBlock()) {
2921 HandleGoto(try_boundary, successor);
2922 }
2923}
2924
Scott Wakelingfe885462016-09-22 10:24:38 +01002925void LocationsBuilderARMVIXL::VisitExit(HExit* exit) {
2926 exit->SetLocations(nullptr);
2927}
2928
2929void InstructionCodeGeneratorARMVIXL::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
2930}
2931
Scott Wakelingfe885462016-09-22 10:24:38 +01002932void InstructionCodeGeneratorARMVIXL::GenerateCompareTestAndBranch(HCondition* condition,
Anton Kirilov23b752b2017-07-20 14:40:44 +01002933 vixl32::Label* true_target,
2934 vixl32::Label* false_target,
Anton Kirilovfd522532017-05-10 12:46:57 +01002935 bool is_far_target) {
Anton Kirilov23b752b2017-07-20 14:40:44 +01002936 if (true_target == false_target) {
2937 DCHECK(true_target != nullptr);
2938 __ B(true_target);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002939 return;
2940 }
2941
Anton Kirilov23b752b2017-07-20 14:40:44 +01002942 vixl32::Label* non_fallthrough_target;
2943 bool invert;
2944 bool emit_both_branches;
Scott Wakelingfe885462016-09-22 10:24:38 +01002945
Anton Kirilov23b752b2017-07-20 14:40:44 +01002946 if (true_target == nullptr) {
2947 // The true target is fallthrough.
2948 DCHECK(false_target != nullptr);
2949 non_fallthrough_target = false_target;
2950 invert = true;
2951 emit_both_branches = false;
2952 } else {
2953 non_fallthrough_target = true_target;
2954 invert = false;
2955 // Either the false target is fallthrough, or there is no fallthrough
2956 // and both branches must be emitted.
2957 emit_both_branches = (false_target != nullptr);
Scott Wakelingfe885462016-09-22 10:24:38 +01002958 }
2959
Anton Kirilov23b752b2017-07-20 14:40:44 +01002960 const auto cond = GenerateTest(condition, invert, codegen_);
2961
2962 __ B(cond.first, non_fallthrough_target, is_far_target);
2963
2964 if (emit_both_branches) {
2965 // No target falls through, we need to branch.
2966 __ B(false_target);
Scott Wakelingfe885462016-09-22 10:24:38 +01002967 }
2968}
2969
2970void InstructionCodeGeneratorARMVIXL::GenerateTestAndBranch(HInstruction* instruction,
2971 size_t condition_input_index,
2972 vixl32::Label* true_target,
xueliang.zhongf51bc622016-11-04 09:23:32 +00002973 vixl32::Label* false_target,
2974 bool far_target) {
Scott Wakelingfe885462016-09-22 10:24:38 +01002975 HInstruction* cond = instruction->InputAt(condition_input_index);
2976
2977 if (true_target == nullptr && false_target == nullptr) {
2978 // Nothing to do. The code always falls through.
2979 return;
2980 } else if (cond->IsIntConstant()) {
2981 // Constant condition, statically compared against "true" (integer value 1).
2982 if (cond->AsIntConstant()->IsTrue()) {
2983 if (true_target != nullptr) {
2984 __ B(true_target);
2985 }
2986 } else {
Anton Kirilov644032c2016-12-06 17:51:43 +00002987 DCHECK(cond->AsIntConstant()->IsFalse()) << Int32ConstantFrom(cond);
Scott Wakelingfe885462016-09-22 10:24:38 +01002988 if (false_target != nullptr) {
2989 __ B(false_target);
2990 }
2991 }
2992 return;
2993 }
2994
2995 // The following code generates these patterns:
2996 // (1) true_target == nullptr && false_target != nullptr
2997 // - opposite condition true => branch to false_target
2998 // (2) true_target != nullptr && false_target == nullptr
2999 // - condition true => branch to true_target
3000 // (3) true_target != nullptr && false_target != nullptr
3001 // - condition true => branch to true_target
3002 // - branch to false_target
3003 if (IsBooleanValueOrMaterializedCondition(cond)) {
3004 // Condition has been materialized, compare the output to 0.
3005 if (kIsDebugBuild) {
3006 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
3007 DCHECK(cond_val.IsRegister());
3008 }
3009 if (true_target == nullptr) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00003010 __ CompareAndBranchIfZero(InputRegisterAt(instruction, condition_input_index),
3011 false_target,
3012 far_target);
Scott Wakelingfe885462016-09-22 10:24:38 +01003013 } else {
xueliang.zhongf51bc622016-11-04 09:23:32 +00003014 __ CompareAndBranchIfNonZero(InputRegisterAt(instruction, condition_input_index),
3015 true_target,
3016 far_target);
Scott Wakelingfe885462016-09-22 10:24:38 +01003017 }
3018 } else {
3019 // Condition has not been materialized. Use its inputs as the comparison and
3020 // its condition as the branch condition.
3021 HCondition* condition = cond->AsCondition();
3022
3023 // If this is a long or FP comparison that has been folded into
3024 // the HCondition, generate the comparison directly.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003025 DataType::Type type = condition->InputAt(0)->GetType();
3026 if (type == DataType::Type::kInt64 || DataType::IsFloatingPointType(type)) {
Anton Kirilovfd522532017-05-10 12:46:57 +01003027 GenerateCompareTestAndBranch(condition, true_target, false_target, far_target);
Scott Wakelingfe885462016-09-22 10:24:38 +01003028 return;
3029 }
3030
Donghui Bai426b49c2016-11-08 14:55:38 +08003031 vixl32::Label* non_fallthrough_target;
3032 vixl32::Condition arm_cond = vixl32::Condition::None();
3033 const vixl32::Register left = InputRegisterAt(cond, 0);
3034 const Operand right = InputOperandAt(cond, 1);
3035
Scott Wakelingfe885462016-09-22 10:24:38 +01003036 if (true_target == nullptr) {
Donghui Bai426b49c2016-11-08 14:55:38 +08003037 arm_cond = ARMCondition(condition->GetOppositeCondition());
3038 non_fallthrough_target = false_target;
Scott Wakelingfe885462016-09-22 10:24:38 +01003039 } else {
Donghui Bai426b49c2016-11-08 14:55:38 +08003040 arm_cond = ARMCondition(condition->GetCondition());
3041 non_fallthrough_target = true_target;
3042 }
3043
3044 if (right.IsImmediate() && right.GetImmediate() == 0 && (arm_cond.Is(ne) || arm_cond.Is(eq))) {
3045 if (arm_cond.Is(eq)) {
Anton Kirilovfd522532017-05-10 12:46:57 +01003046 __ CompareAndBranchIfZero(left, non_fallthrough_target, far_target);
Donghui Bai426b49c2016-11-08 14:55:38 +08003047 } else {
3048 DCHECK(arm_cond.Is(ne));
Anton Kirilovfd522532017-05-10 12:46:57 +01003049 __ CompareAndBranchIfNonZero(left, non_fallthrough_target, far_target);
Donghui Bai426b49c2016-11-08 14:55:38 +08003050 }
3051 } else {
3052 __ Cmp(left, right);
Anton Kirilovfd522532017-05-10 12:46:57 +01003053 __ B(arm_cond, non_fallthrough_target, far_target);
Scott Wakelingfe885462016-09-22 10:24:38 +01003054 }
3055 }
3056
3057 // If neither branch falls through (case 3), the conditional branch to `true_target`
3058 // was already emitted (case 2) and we need to emit a jump to `false_target`.
3059 if (true_target != nullptr && false_target != nullptr) {
3060 __ B(false_target);
3061 }
3062}
3063
3064void LocationsBuilderARMVIXL::VisitIf(HIf* if_instr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003065 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(if_instr);
Scott Wakelingfe885462016-09-22 10:24:38 +01003066 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
3067 locations->SetInAt(0, Location::RequiresRegister());
3068 }
3069}
3070
3071void InstructionCodeGeneratorARMVIXL::VisitIf(HIf* if_instr) {
3072 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
3073 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003074 vixl32::Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
3075 nullptr : codegen_->GetLabelOf(true_successor);
3076 vixl32::Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
3077 nullptr : codegen_->GetLabelOf(false_successor);
Scott Wakelingfe885462016-09-22 10:24:38 +01003078 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
3079}
3080
Scott Wakelingc34dba72016-10-03 10:14:44 +01003081void LocationsBuilderARMVIXL::VisitDeoptimize(HDeoptimize* deoptimize) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003082 LocationSummary* locations = new (GetGraph()->GetAllocator())
Scott Wakelingc34dba72016-10-03 10:14:44 +01003083 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003084 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3085 RegisterSet caller_saves = RegisterSet::Empty();
3086 caller_saves.Add(LocationFrom(calling_convention.GetRegisterAt(0)));
3087 locations->SetCustomSlowPathCallerSaves(caller_saves);
Scott Wakelingc34dba72016-10-03 10:14:44 +01003088 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
3089 locations->SetInAt(0, Location::RequiresRegister());
3090 }
3091}
3092
3093void InstructionCodeGeneratorARMVIXL::VisitDeoptimize(HDeoptimize* deoptimize) {
3094 SlowPathCodeARMVIXL* slow_path =
3095 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARMVIXL>(deoptimize);
3096 GenerateTestAndBranch(deoptimize,
3097 /* condition_input_index */ 0,
3098 slow_path->GetEntryLabel(),
3099 /* false_target */ nullptr);
3100}
3101
Artem Serovd4cc5b22016-11-04 11:19:09 +00003102void LocationsBuilderARMVIXL::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003103 LocationSummary* locations = new (GetGraph()->GetAllocator())
Artem Serovd4cc5b22016-11-04 11:19:09 +00003104 LocationSummary(flag, LocationSummary::kNoCall);
3105 locations->SetOut(Location::RequiresRegister());
3106}
3107
3108void InstructionCodeGeneratorARMVIXL::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
3109 GetAssembler()->LoadFromOffset(kLoadWord,
3110 OutputRegister(flag),
3111 sp,
3112 codegen_->GetStackOffsetOfShouldDeoptimizeFlag());
3113}
3114
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003115void LocationsBuilderARMVIXL::VisitSelect(HSelect* select) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003116 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(select);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003117 const bool is_floating_point = DataType::IsFloatingPointType(select->GetType());
Donghui Bai426b49c2016-11-08 14:55:38 +08003118
3119 if (is_floating_point) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003120 locations->SetInAt(0, Location::RequiresFpuRegister());
Donghui Bai426b49c2016-11-08 14:55:38 +08003121 locations->SetInAt(1, Location::FpuRegisterOrConstant(select->GetTrueValue()));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003122 } else {
3123 locations->SetInAt(0, Location::RequiresRegister());
Donghui Bai426b49c2016-11-08 14:55:38 +08003124 locations->SetInAt(1, Arm8BitEncodableConstantOrRegister(select->GetTrueValue()));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003125 }
Donghui Bai426b49c2016-11-08 14:55:38 +08003126
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003127 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
Donghui Bai426b49c2016-11-08 14:55:38 +08003128 locations->SetInAt(2, Location::RegisterOrConstant(select->GetCondition()));
3129 // The code generator handles overlap with the values, but not with the condition.
3130 locations->SetOut(Location::SameAsFirstInput());
3131 } else if (is_floating_point) {
3132 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3133 } else {
3134 if (!locations->InAt(1).IsConstant()) {
3135 locations->SetInAt(0, Arm8BitEncodableConstantOrRegister(select->GetFalseValue()));
3136 }
3137
3138 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003139 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003140}
3141
3142void InstructionCodeGeneratorARMVIXL::VisitSelect(HSelect* select) {
Donghui Bai426b49c2016-11-08 14:55:38 +08003143 HInstruction* const condition = select->GetCondition();
3144 const LocationSummary* const locations = select->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003145 const DataType::Type type = select->GetType();
Donghui Bai426b49c2016-11-08 14:55:38 +08003146 const Location first = locations->InAt(0);
3147 const Location out = locations->Out();
3148 const Location second = locations->InAt(1);
3149 Location src;
3150
3151 if (condition->IsIntConstant()) {
3152 if (condition->AsIntConstant()->IsFalse()) {
3153 src = first;
3154 } else {
3155 src = second;
3156 }
3157
3158 codegen_->MoveLocation(out, src, type);
3159 return;
3160 }
3161
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003162 if (!DataType::IsFloatingPointType(type)) {
Donghui Bai426b49c2016-11-08 14:55:38 +08003163 bool invert = false;
3164
3165 if (out.Equals(second)) {
3166 src = first;
3167 invert = true;
3168 } else if (out.Equals(first)) {
3169 src = second;
3170 } else if (second.IsConstant()) {
3171 DCHECK(CanEncodeConstantAs8BitImmediate(second.GetConstant()));
3172 src = second;
3173 } else if (first.IsConstant()) {
3174 DCHECK(CanEncodeConstantAs8BitImmediate(first.GetConstant()));
3175 src = first;
3176 invert = true;
3177 } else {
3178 src = second;
3179 }
3180
3181 if (CanGenerateConditionalMove(out, src)) {
3182 if (!out.Equals(first) && !out.Equals(second)) {
3183 codegen_->MoveLocation(out, src.Equals(first) ? second : first, type);
3184 }
3185
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003186 std::pair<vixl32::Condition, vixl32::Condition> cond(eq, ne);
3187
3188 if (IsBooleanValueOrMaterializedCondition(condition)) {
3189 __ Cmp(InputRegisterAt(select, 2), 0);
3190 cond = invert ? std::make_pair(eq, ne) : std::make_pair(ne, eq);
3191 } else {
3192 cond = GenerateTest(condition->AsCondition(), invert, codegen_);
3193 }
3194
Donghui Bai426b49c2016-11-08 14:55:38 +08003195 const size_t instr_count = out.IsRegisterPair() ? 4 : 2;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003196 // We use the scope because of the IT block that follows.
Donghui Bai426b49c2016-11-08 14:55:38 +08003197 ExactAssemblyScope guard(GetVIXLAssembler(),
3198 instr_count * vixl32::k16BitT32InstructionSizeInBytes,
3199 CodeBufferCheckScope::kExactSize);
3200
3201 if (out.IsRegister()) {
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003202 __ it(cond.first);
3203 __ mov(cond.first, RegisterFrom(out), OperandFrom(src, type));
Donghui Bai426b49c2016-11-08 14:55:38 +08003204 } else {
3205 DCHECK(out.IsRegisterPair());
3206
3207 Operand operand_high(0);
3208 Operand operand_low(0);
3209
3210 if (src.IsConstant()) {
3211 const int64_t value = Int64ConstantFrom(src);
3212
3213 operand_high = High32Bits(value);
3214 operand_low = Low32Bits(value);
3215 } else {
3216 DCHECK(src.IsRegisterPair());
3217 operand_high = HighRegisterFrom(src);
3218 operand_low = LowRegisterFrom(src);
3219 }
3220
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003221 __ it(cond.first);
3222 __ mov(cond.first, LowRegisterFrom(out), operand_low);
3223 __ it(cond.first);
3224 __ mov(cond.first, HighRegisterFrom(out), operand_high);
Donghui Bai426b49c2016-11-08 14:55:38 +08003225 }
3226
3227 return;
3228 }
3229 }
3230
3231 vixl32::Label* false_target = nullptr;
3232 vixl32::Label* true_target = nullptr;
3233 vixl32::Label select_end;
3234 vixl32::Label* const target = codegen_->GetFinalLabel(select, &select_end);
3235
3236 if (out.Equals(second)) {
3237 true_target = target;
3238 src = first;
3239 } else {
3240 false_target = target;
3241 src = second;
3242
3243 if (!out.Equals(first)) {
3244 codegen_->MoveLocation(out, first, type);
3245 }
3246 }
3247
3248 GenerateTestAndBranch(select, 2, true_target, false_target, /* far_target */ false);
3249 codegen_->MoveLocation(out, src, type);
3250
3251 if (select_end.IsReferenced()) {
3252 __ Bind(&select_end);
3253 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003254}
3255
Artem Serov551b28f2016-10-18 19:11:30 +01003256void LocationsBuilderARMVIXL::VisitNativeDebugInfo(HNativeDebugInfo* info) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003257 new (GetGraph()->GetAllocator()) LocationSummary(info);
Artem Serov551b28f2016-10-18 19:11:30 +01003258}
3259
3260void InstructionCodeGeneratorARMVIXL::VisitNativeDebugInfo(HNativeDebugInfo*) {
3261 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
3262}
3263
Scott Wakelingfe885462016-09-22 10:24:38 +01003264void CodeGeneratorARMVIXL::GenerateNop() {
3265 __ Nop();
3266}
3267
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003268// `temp` is an extra temporary register that is used for some conditions;
3269// callers may not specify it, in which case the method will use a scratch
3270// register instead.
3271void CodeGeneratorARMVIXL::GenerateConditionWithZero(IfCondition condition,
3272 vixl32::Register out,
3273 vixl32::Register in,
3274 vixl32::Register temp) {
3275 switch (condition) {
3276 case kCondEQ:
3277 // x <= 0 iff x == 0 when the comparison is unsigned.
3278 case kCondBE:
3279 if (!temp.IsValid() || (out.IsLow() && !out.Is(in))) {
3280 temp = out;
3281 }
3282
3283 // Avoid 32-bit instructions if possible; note that `in` and `temp` must be
3284 // different as well.
3285 if (in.IsLow() && temp.IsLow() && !in.Is(temp)) {
3286 // temp = - in; only 0 sets the carry flag.
3287 __ Rsbs(temp, in, 0);
3288
3289 if (out.Is(in)) {
3290 std::swap(in, temp);
3291 }
3292
3293 // out = - in + in + carry = carry
3294 __ Adc(out, temp, in);
3295 } else {
3296 // If `in` is 0, then it has 32 leading zeros, and less than that otherwise.
3297 __ Clz(out, in);
3298 // Any number less than 32 logically shifted right by 5 bits results in 0;
3299 // the same operation on 32 yields 1.
3300 __ Lsr(out, out, 5);
3301 }
3302
3303 break;
3304 case kCondNE:
3305 // x > 0 iff x != 0 when the comparison is unsigned.
3306 case kCondA: {
3307 UseScratchRegisterScope temps(GetVIXLAssembler());
3308
3309 if (out.Is(in)) {
3310 if (!temp.IsValid() || in.Is(temp)) {
3311 temp = temps.Acquire();
3312 }
3313 } else if (!temp.IsValid() || !temp.IsLow()) {
3314 temp = out;
3315 }
3316
3317 // temp = in - 1; only 0 does not set the carry flag.
3318 __ Subs(temp, in, 1);
3319 // out = in + ~temp + carry = in + (-(in - 1) - 1) + carry = in - in + 1 - 1 + carry = carry
3320 __ Sbc(out, in, temp);
3321 break;
3322 }
3323 case kCondGE:
3324 __ Mvn(out, in);
3325 in = out;
3326 FALLTHROUGH_INTENDED;
3327 case kCondLT:
3328 // We only care about the sign bit.
3329 __ Lsr(out, in, 31);
3330 break;
3331 case kCondAE:
3332 // Trivially true.
3333 __ Mov(out, 1);
3334 break;
3335 case kCondB:
3336 // Trivially false.
3337 __ Mov(out, 0);
3338 break;
3339 default:
3340 LOG(FATAL) << "Unexpected condition " << condition;
3341 UNREACHABLE();
3342 }
3343}
3344
Scott Wakelingfe885462016-09-22 10:24:38 +01003345void LocationsBuilderARMVIXL::HandleCondition(HCondition* cond) {
3346 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003347 new (GetGraph()->GetAllocator()) LocationSummary(cond, LocationSummary::kNoCall);
Scott Wakelingfe885462016-09-22 10:24:38 +01003348 // Handle the long/FP comparisons made in instruction simplification.
3349 switch (cond->InputAt(0)->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003350 case DataType::Type::kInt64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003351 locations->SetInAt(0, Location::RequiresRegister());
3352 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
3353 if (!cond->IsEmittedAtUseSite()) {
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003354 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Scott Wakelingfe885462016-09-22 10:24:38 +01003355 }
3356 break;
3357
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003358 case DataType::Type::kFloat32:
3359 case DataType::Type::kFloat64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003360 locations->SetInAt(0, Location::RequiresFpuRegister());
Artem Serov657022c2016-11-23 14:19:38 +00003361 locations->SetInAt(1, ArithmeticZeroOrFpuRegister(cond->InputAt(1)));
Scott Wakelingfe885462016-09-22 10:24:38 +01003362 if (!cond->IsEmittedAtUseSite()) {
3363 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3364 }
3365 break;
3366
3367 default:
3368 locations->SetInAt(0, Location::RequiresRegister());
3369 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
3370 if (!cond->IsEmittedAtUseSite()) {
3371 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3372 }
3373 }
3374}
3375
3376void InstructionCodeGeneratorARMVIXL::HandleCondition(HCondition* cond) {
3377 if (cond->IsEmittedAtUseSite()) {
3378 return;
3379 }
3380
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003381 const DataType::Type type = cond->GetLeft()->GetType();
Scott Wakelingfe885462016-09-22 10:24:38 +01003382
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003383 if (DataType::IsFloatingPointType(type)) {
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003384 GenerateConditionGeneric(cond, codegen_);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003385 return;
Scott Wakelingfe885462016-09-22 10:24:38 +01003386 }
3387
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003388 DCHECK(DataType::IsIntegralType(type) || type == DataType::Type::kReference) << type;
Scott Wakelingfe885462016-09-22 10:24:38 +01003389
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003390 const IfCondition condition = cond->GetCondition();
Scott Wakelingfe885462016-09-22 10:24:38 +01003391
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003392 // A condition with only one boolean input, or two boolean inputs without being equality or
3393 // inequality results from transformations done by the instruction simplifier, and is handled
3394 // as a regular condition with integral inputs.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003395 if (type == DataType::Type::kBool &&
3396 cond->GetRight()->GetType() == DataType::Type::kBool &&
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003397 (condition == kCondEQ || condition == kCondNE)) {
3398 vixl32::Register left = InputRegisterAt(cond, 0);
3399 const vixl32::Register out = OutputRegister(cond);
3400 const Location right_loc = cond->GetLocations()->InAt(1);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003401
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003402 // The constant case is handled by the instruction simplifier.
3403 DCHECK(!right_loc.IsConstant());
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003404
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003405 vixl32::Register right = RegisterFrom(right_loc);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003406
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003407 // Avoid 32-bit instructions if possible.
3408 if (out.Is(right)) {
3409 std::swap(left, right);
3410 }
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003411
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003412 __ Eor(out, left, right);
3413
3414 if (condition == kCondEQ) {
3415 __ Eor(out, out, 1);
3416 }
3417
3418 return;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003419 }
Anton Kirilov6f644202017-02-27 18:29:45 +00003420
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003421 GenerateConditionIntegralOrNonPrimitive(cond, codegen_);
Scott Wakelingfe885462016-09-22 10:24:38 +01003422}
3423
3424void LocationsBuilderARMVIXL::VisitEqual(HEqual* comp) {
3425 HandleCondition(comp);
3426}
3427
3428void InstructionCodeGeneratorARMVIXL::VisitEqual(HEqual* comp) {
3429 HandleCondition(comp);
3430}
3431
3432void LocationsBuilderARMVIXL::VisitNotEqual(HNotEqual* comp) {
3433 HandleCondition(comp);
3434}
3435
3436void InstructionCodeGeneratorARMVIXL::VisitNotEqual(HNotEqual* comp) {
3437 HandleCondition(comp);
3438}
3439
3440void LocationsBuilderARMVIXL::VisitLessThan(HLessThan* comp) {
3441 HandleCondition(comp);
3442}
3443
3444void InstructionCodeGeneratorARMVIXL::VisitLessThan(HLessThan* comp) {
3445 HandleCondition(comp);
3446}
3447
3448void LocationsBuilderARMVIXL::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
3449 HandleCondition(comp);
3450}
3451
3452void InstructionCodeGeneratorARMVIXL::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
3453 HandleCondition(comp);
3454}
3455
3456void LocationsBuilderARMVIXL::VisitGreaterThan(HGreaterThan* comp) {
3457 HandleCondition(comp);
3458}
3459
3460void InstructionCodeGeneratorARMVIXL::VisitGreaterThan(HGreaterThan* comp) {
3461 HandleCondition(comp);
3462}
3463
3464void LocationsBuilderARMVIXL::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
3465 HandleCondition(comp);
3466}
3467
3468void InstructionCodeGeneratorARMVIXL::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
3469 HandleCondition(comp);
3470}
3471
3472void LocationsBuilderARMVIXL::VisitBelow(HBelow* comp) {
3473 HandleCondition(comp);
3474}
3475
3476void InstructionCodeGeneratorARMVIXL::VisitBelow(HBelow* comp) {
3477 HandleCondition(comp);
3478}
3479
3480void LocationsBuilderARMVIXL::VisitBelowOrEqual(HBelowOrEqual* comp) {
3481 HandleCondition(comp);
3482}
3483
3484void InstructionCodeGeneratorARMVIXL::VisitBelowOrEqual(HBelowOrEqual* comp) {
3485 HandleCondition(comp);
3486}
3487
3488void LocationsBuilderARMVIXL::VisitAbove(HAbove* comp) {
3489 HandleCondition(comp);
3490}
3491
3492void InstructionCodeGeneratorARMVIXL::VisitAbove(HAbove* comp) {
3493 HandleCondition(comp);
3494}
3495
3496void LocationsBuilderARMVIXL::VisitAboveOrEqual(HAboveOrEqual* comp) {
3497 HandleCondition(comp);
3498}
3499
3500void InstructionCodeGeneratorARMVIXL::VisitAboveOrEqual(HAboveOrEqual* comp) {
3501 HandleCondition(comp);
3502}
3503
3504void LocationsBuilderARMVIXL::VisitIntConstant(HIntConstant* constant) {
3505 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003506 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Scott Wakelingfe885462016-09-22 10:24:38 +01003507 locations->SetOut(Location::ConstantLocation(constant));
3508}
3509
3510void InstructionCodeGeneratorARMVIXL::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
3511 // Will be generated at use site.
3512}
3513
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003514void LocationsBuilderARMVIXL::VisitNullConstant(HNullConstant* constant) {
3515 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003516 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003517 locations->SetOut(Location::ConstantLocation(constant));
3518}
3519
3520void InstructionCodeGeneratorARMVIXL::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
3521 // Will be generated at use site.
3522}
3523
Scott Wakelingfe885462016-09-22 10:24:38 +01003524void LocationsBuilderARMVIXL::VisitLongConstant(HLongConstant* constant) {
3525 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003526 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Scott Wakelingfe885462016-09-22 10:24:38 +01003527 locations->SetOut(Location::ConstantLocation(constant));
3528}
3529
3530void InstructionCodeGeneratorARMVIXL::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
3531 // Will be generated at use site.
3532}
3533
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01003534void LocationsBuilderARMVIXL::VisitFloatConstant(HFloatConstant* constant) {
3535 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003536 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01003537 locations->SetOut(Location::ConstantLocation(constant));
3538}
3539
Scott Wakelingc34dba72016-10-03 10:14:44 +01003540void InstructionCodeGeneratorARMVIXL::VisitFloatConstant(
3541 HFloatConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01003542 // Will be generated at use site.
3543}
3544
3545void LocationsBuilderARMVIXL::VisitDoubleConstant(HDoubleConstant* constant) {
3546 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003547 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01003548 locations->SetOut(Location::ConstantLocation(constant));
3549}
3550
Scott Wakelingc34dba72016-10-03 10:14:44 +01003551void InstructionCodeGeneratorARMVIXL::VisitDoubleConstant(
3552 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01003553 // Will be generated at use site.
3554}
3555
Igor Murashkind01745e2017-04-05 16:40:31 -07003556void LocationsBuilderARMVIXL::VisitConstructorFence(HConstructorFence* constructor_fence) {
3557 constructor_fence->SetLocations(nullptr);
3558}
3559
3560void InstructionCodeGeneratorARMVIXL::VisitConstructorFence(
3561 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
3562 codegen_->GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
3563}
3564
Scott Wakelingfe885462016-09-22 10:24:38 +01003565void LocationsBuilderARMVIXL::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3566 memory_barrier->SetLocations(nullptr);
3567}
3568
3569void InstructionCodeGeneratorARMVIXL::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3570 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
3571}
3572
3573void LocationsBuilderARMVIXL::VisitReturnVoid(HReturnVoid* ret) {
3574 ret->SetLocations(nullptr);
3575}
3576
3577void InstructionCodeGeneratorARMVIXL::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
3578 codegen_->GenerateFrameExit();
3579}
3580
3581void LocationsBuilderARMVIXL::VisitReturn(HReturn* ret) {
3582 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003583 new (GetGraph()->GetAllocator()) LocationSummary(ret, LocationSummary::kNoCall);
Scott Wakelingfe885462016-09-22 10:24:38 +01003584 locations->SetInAt(0, parameter_visitor_.GetReturnLocation(ret->InputAt(0)->GetType()));
3585}
3586
3587void InstructionCodeGeneratorARMVIXL::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
3588 codegen_->GenerateFrameExit();
3589}
3590
Artem Serovcfbe9132016-10-14 15:58:56 +01003591void LocationsBuilderARMVIXL::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3592 // The trampoline uses the same calling convention as dex calling conventions,
3593 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
3594 // the method_idx.
3595 HandleInvoke(invoke);
3596}
3597
3598void InstructionCodeGeneratorARMVIXL::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3599 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
Roland Levillain5daa4952017-07-03 17:23:56 +01003600 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 3);
Artem Serovcfbe9132016-10-14 15:58:56 +01003601}
3602
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003603void LocationsBuilderARMVIXL::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
3604 // Explicit clinit checks triggered by static invokes must have been pruned by
3605 // art::PrepareForRegisterAllocation.
3606 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
3607
Anton Kirilov5ec62182016-10-13 20:16:02 +01003608 IntrinsicLocationsBuilderARMVIXL intrinsic(codegen_);
3609 if (intrinsic.TryDispatch(invoke)) {
Anton Kirilov5ec62182016-10-13 20:16:02 +01003610 return;
3611 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003612
3613 HandleInvoke(invoke);
3614}
3615
Anton Kirilov5ec62182016-10-13 20:16:02 +01003616static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARMVIXL* codegen) {
3617 if (invoke->GetLocations()->Intrinsified()) {
3618 IntrinsicCodeGeneratorARMVIXL intrinsic(codegen);
3619 intrinsic.Dispatch(invoke);
3620 return true;
3621 }
3622 return false;
3623}
3624
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003625void InstructionCodeGeneratorARMVIXL::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
3626 // Explicit clinit checks triggered by static invokes must have been pruned by
3627 // art::PrepareForRegisterAllocation.
3628 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
3629
Anton Kirilov5ec62182016-10-13 20:16:02 +01003630 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
Roland Levillain5daa4952017-07-03 17:23:56 +01003631 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 4);
Anton Kirilov5ec62182016-10-13 20:16:02 +01003632 return;
3633 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003634
3635 LocationSummary* locations = invoke->GetLocations();
Artem Serovd4cc5b22016-11-04 11:19:09 +00003636 codegen_->GenerateStaticOrDirectCall(
3637 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Roland Levillain5daa4952017-07-03 17:23:56 +01003638
3639 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 5);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003640}
3641
3642void LocationsBuilderARMVIXL::HandleInvoke(HInvoke* invoke) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00003643 InvokeDexCallingConventionVisitorARMVIXL calling_convention_visitor;
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003644 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
3645}
3646
3647void LocationsBuilderARMVIXL::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Anton Kirilov5ec62182016-10-13 20:16:02 +01003648 IntrinsicLocationsBuilderARMVIXL intrinsic(codegen_);
3649 if (intrinsic.TryDispatch(invoke)) {
3650 return;
3651 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003652
3653 HandleInvoke(invoke);
3654}
3655
3656void InstructionCodeGeneratorARMVIXL::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Anton Kirilov5ec62182016-10-13 20:16:02 +01003657 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
Roland Levillain5daa4952017-07-03 17:23:56 +01003658 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 6);
Anton Kirilov5ec62182016-10-13 20:16:02 +01003659 return;
3660 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003661
3662 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexandre Rames374ddf32016-11-04 10:40:49 +00003663 DCHECK(!codegen_->IsLeafMethod());
Roland Levillain5daa4952017-07-03 17:23:56 +01003664
3665 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 7);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003666}
3667
Artem Serovcfbe9132016-10-14 15:58:56 +01003668void LocationsBuilderARMVIXL::VisitInvokeInterface(HInvokeInterface* invoke) {
3669 HandleInvoke(invoke);
3670 // Add the hidden argument.
3671 invoke->GetLocations()->AddTemp(LocationFrom(r12));
3672}
3673
3674void InstructionCodeGeneratorARMVIXL::VisitInvokeInterface(HInvokeInterface* invoke) {
3675 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
3676 LocationSummary* locations = invoke->GetLocations();
3677 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
3678 vixl32::Register hidden_reg = RegisterFrom(locations->GetTemp(1));
3679 Location receiver = locations->InAt(0);
3680 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3681
3682 DCHECK(!receiver.IsStackSlot());
3683
Alexandre Rames374ddf32016-11-04 10:40:49 +00003684 // Ensure the pc position is recorded immediately after the `ldr` instruction.
3685 {
Artem Serov0fb37192016-12-06 18:13:40 +00003686 ExactAssemblyScope aas(GetVIXLAssembler(),
3687 vixl32::kMaxInstructionSizeInBytes,
3688 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00003689 // /* HeapReference<Class> */ temp = receiver->klass_
3690 __ ldr(temp, MemOperand(RegisterFrom(receiver), class_offset));
3691 codegen_->MaybeRecordImplicitNullCheck(invoke);
3692 }
Artem Serovcfbe9132016-10-14 15:58:56 +01003693 // Instead of simply (possibly) unpoisoning `temp` here, we should
3694 // emit a read barrier for the previous class reference load.
3695 // However this is not required in practice, as this is an
3696 // intermediate/temporary reference and because the current
3697 // concurrent copying collector keeps the from-space memory
3698 // intact/accessible until the end of the marking phase (the
3699 // concurrent copying collector may not in the future).
3700 GetAssembler()->MaybeUnpoisonHeapReference(temp);
3701 GetAssembler()->LoadFromOffset(kLoadWord,
3702 temp,
3703 temp,
3704 mirror::Class::ImtPtrOffset(kArmPointerSize).Uint32Value());
3705 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
3706 invoke->GetImtIndex(), kArmPointerSize));
3707 // temp = temp->GetImtEntryAt(method_offset);
3708 GetAssembler()->LoadFromOffset(kLoadWord, temp, temp, method_offset);
3709 uint32_t entry_point =
3710 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize).Int32Value();
3711 // LR = temp->GetEntryPoint();
3712 GetAssembler()->LoadFromOffset(kLoadWord, lr, temp, entry_point);
3713
3714 // Set the hidden (in r12) argument. It is done here, right before a BLX to prevent other
3715 // instruction from clobbering it as they might use r12 as a scratch register.
3716 DCHECK(hidden_reg.Is(r12));
Scott Wakelingb77051e2016-11-21 19:46:00 +00003717
3718 {
3719 // The VIXL macro assembler may clobber any of the scratch registers that are available to it,
3720 // so it checks if the application is using them (by passing them to the macro assembler
3721 // methods). The following application of UseScratchRegisterScope corrects VIXL's notion of
3722 // what is available, and is the opposite of the standard usage: Instead of requesting a
3723 // temporary location, it imposes an external constraint (i.e. a specific register is reserved
3724 // for the hidden argument). Note that this works even if VIXL needs a scratch register itself
3725 // (to materialize the constant), since the destination register becomes available for such use
3726 // internally for the duration of the macro instruction.
3727 UseScratchRegisterScope temps(GetVIXLAssembler());
3728 temps.Exclude(hidden_reg);
3729 __ Mov(hidden_reg, invoke->GetDexMethodIndex());
3730 }
Artem Serovcfbe9132016-10-14 15:58:56 +01003731 {
Alexandre Rames374ddf32016-11-04 10:40:49 +00003732 // Ensure the pc position is recorded immediately after the `blx` instruction.
3733 // blx in T32 has only 16bit encoding that's why a stricter check for the scope is used.
Artem Serov0fb37192016-12-06 18:13:40 +00003734 ExactAssemblyScope aas(GetVIXLAssembler(),
Alexandre Rames374ddf32016-11-04 10:40:49 +00003735 vixl32::k16BitT32InstructionSizeInBytes,
3736 CodeBufferCheckScope::kExactSize);
Artem Serovcfbe9132016-10-14 15:58:56 +01003737 // LR();
3738 __ blx(lr);
Artem Serovcfbe9132016-10-14 15:58:56 +01003739 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Alexandre Rames374ddf32016-11-04 10:40:49 +00003740 DCHECK(!codegen_->IsLeafMethod());
Artem Serovcfbe9132016-10-14 15:58:56 +01003741 }
Roland Levillain5daa4952017-07-03 17:23:56 +01003742
3743 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 8);
Artem Serovcfbe9132016-10-14 15:58:56 +01003744}
3745
Orion Hodsonac141392017-01-13 11:53:47 +00003746void LocationsBuilderARMVIXL::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
3747 HandleInvoke(invoke);
3748}
3749
3750void InstructionCodeGeneratorARMVIXL::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
3751 codegen_->GenerateInvokePolymorphicCall(invoke);
Roland Levillain5daa4952017-07-03 17:23:56 +01003752 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 9);
Orion Hodsonac141392017-01-13 11:53:47 +00003753}
3754
Artem Serov02109dd2016-09-23 17:17:54 +01003755void LocationsBuilderARMVIXL::VisitNeg(HNeg* neg) {
3756 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003757 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Artem Serov02109dd2016-09-23 17:17:54 +01003758 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003759 case DataType::Type::kInt32: {
Artem Serov02109dd2016-09-23 17:17:54 +01003760 locations->SetInAt(0, Location::RequiresRegister());
3761 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3762 break;
3763 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003764 case DataType::Type::kInt64: {
Artem Serov02109dd2016-09-23 17:17:54 +01003765 locations->SetInAt(0, Location::RequiresRegister());
3766 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3767 break;
3768 }
3769
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003770 case DataType::Type::kFloat32:
3771 case DataType::Type::kFloat64:
Artem Serov02109dd2016-09-23 17:17:54 +01003772 locations->SetInAt(0, Location::RequiresFpuRegister());
3773 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3774 break;
3775
3776 default:
3777 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
3778 }
3779}
3780
3781void InstructionCodeGeneratorARMVIXL::VisitNeg(HNeg* neg) {
3782 LocationSummary* locations = neg->GetLocations();
3783 Location out = locations->Out();
3784 Location in = locations->InAt(0);
3785 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003786 case DataType::Type::kInt32:
Artem Serov02109dd2016-09-23 17:17:54 +01003787 __ Rsb(OutputRegister(neg), InputRegisterAt(neg, 0), 0);
3788 break;
3789
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003790 case DataType::Type::kInt64:
Artem Serov02109dd2016-09-23 17:17:54 +01003791 // out.lo = 0 - in.lo (and update the carry/borrow (C) flag)
3792 __ Rsbs(LowRegisterFrom(out), LowRegisterFrom(in), 0);
3793 // We cannot emit an RSC (Reverse Subtract with Carry)
3794 // instruction here, as it does not exist in the Thumb-2
3795 // instruction set. We use the following approach
3796 // using SBC and SUB instead.
3797 //
3798 // out.hi = -C
3799 __ Sbc(HighRegisterFrom(out), HighRegisterFrom(out), HighRegisterFrom(out));
3800 // out.hi = out.hi - in.hi
3801 __ Sub(HighRegisterFrom(out), HighRegisterFrom(out), HighRegisterFrom(in));
3802 break;
3803
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003804 case DataType::Type::kFloat32:
3805 case DataType::Type::kFloat64:
Anton Kirilov644032c2016-12-06 17:51:43 +00003806 __ Vneg(OutputVRegister(neg), InputVRegister(neg));
Artem Serov02109dd2016-09-23 17:17:54 +01003807 break;
3808
3809 default:
3810 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
3811 }
3812}
3813
Scott Wakelingfe885462016-09-22 10:24:38 +01003814void LocationsBuilderARMVIXL::VisitTypeConversion(HTypeConversion* conversion) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003815 DataType::Type result_type = conversion->GetResultType();
3816 DataType::Type input_type = conversion->GetInputType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003817 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
3818 << input_type << " -> " << result_type;
Scott Wakelingfe885462016-09-22 10:24:38 +01003819
3820 // The float-to-long, double-to-long and long-to-float type conversions
3821 // rely on a call to the runtime.
3822 LocationSummary::CallKind call_kind =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003823 (((input_type == DataType::Type::kFloat32 || input_type == DataType::Type::kFloat64)
3824 && result_type == DataType::Type::kInt64)
3825 || (input_type == DataType::Type::kInt64 && result_type == DataType::Type::kFloat32))
Scott Wakelingfe885462016-09-22 10:24:38 +01003826 ? LocationSummary::kCallOnMainOnly
3827 : LocationSummary::kNoCall;
3828 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003829 new (GetGraph()->GetAllocator()) LocationSummary(conversion, call_kind);
Scott Wakelingfe885462016-09-22 10:24:38 +01003830
Scott Wakelingfe885462016-09-22 10:24:38 +01003831 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003832 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003833 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003834 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003835 case DataType::Type::kInt16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003836 DCHECK(DataType::IsIntegralType(input_type)) << input_type;
3837 locations->SetInAt(0, Location::RequiresRegister());
3838 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Scott Wakelingfe885462016-09-22 10:24:38 +01003839 break;
3840
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003841 case DataType::Type::kInt32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003842 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003843 case DataType::Type::kInt64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003844 locations->SetInAt(0, Location::Any());
3845 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3846 break;
3847
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003848 case DataType::Type::kFloat32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003849 locations->SetInAt(0, Location::RequiresFpuRegister());
3850 locations->SetOut(Location::RequiresRegister());
3851 locations->AddTemp(Location::RequiresFpuRegister());
3852 break;
3853
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003854 case DataType::Type::kFloat64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003855 locations->SetInAt(0, Location::RequiresFpuRegister());
3856 locations->SetOut(Location::RequiresRegister());
3857 locations->AddTemp(Location::RequiresFpuRegister());
3858 break;
3859
3860 default:
3861 LOG(FATAL) << "Unexpected type conversion from " << input_type
3862 << " to " << result_type;
3863 }
3864 break;
3865
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003866 case DataType::Type::kInt64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003867 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003868 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003869 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003870 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003871 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003872 case DataType::Type::kInt16:
3873 case DataType::Type::kInt32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003874 locations->SetInAt(0, Location::RequiresRegister());
3875 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3876 break;
3877
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003878 case DataType::Type::kFloat32: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003879 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3880 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
3881 locations->SetOut(LocationFrom(r0, r1));
Scott Wakelingfe885462016-09-22 10:24:38 +01003882 break;
3883 }
3884
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003885 case DataType::Type::kFloat64: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003886 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3887 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0),
3888 calling_convention.GetFpuRegisterAt(1)));
3889 locations->SetOut(LocationFrom(r0, r1));
Scott Wakelingfe885462016-09-22 10:24:38 +01003890 break;
3891 }
3892
3893 default:
3894 LOG(FATAL) << "Unexpected type conversion from " << input_type
3895 << " to " << result_type;
3896 }
3897 break;
3898
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003899 case DataType::Type::kFloat32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003900 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003901 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003902 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003903 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003904 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003905 case DataType::Type::kInt16:
3906 case DataType::Type::kInt32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003907 locations->SetInAt(0, Location::RequiresRegister());
3908 locations->SetOut(Location::RequiresFpuRegister());
3909 break;
3910
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003911 case DataType::Type::kInt64: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003912 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3913 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0),
3914 calling_convention.GetRegisterAt(1)));
3915 locations->SetOut(LocationFrom(calling_convention.GetFpuRegisterAt(0)));
Scott Wakelingfe885462016-09-22 10:24:38 +01003916 break;
3917 }
3918
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003919 case DataType::Type::kFloat64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003920 locations->SetInAt(0, Location::RequiresFpuRegister());
3921 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3922 break;
3923
3924 default:
3925 LOG(FATAL) << "Unexpected type conversion from " << input_type
3926 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08003927 }
Scott Wakelingfe885462016-09-22 10:24:38 +01003928 break;
3929
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003930 case DataType::Type::kFloat64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003931 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003932 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003933 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003934 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003935 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003936 case DataType::Type::kInt16:
3937 case DataType::Type::kInt32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003938 locations->SetInAt(0, Location::RequiresRegister());
3939 locations->SetOut(Location::RequiresFpuRegister());
3940 break;
3941
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003942 case DataType::Type::kInt64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003943 locations->SetInAt(0, Location::RequiresRegister());
3944 locations->SetOut(Location::RequiresFpuRegister());
3945 locations->AddTemp(Location::RequiresFpuRegister());
3946 locations->AddTemp(Location::RequiresFpuRegister());
3947 break;
3948
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003949 case DataType::Type::kFloat32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003950 locations->SetInAt(0, Location::RequiresFpuRegister());
3951 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3952 break;
3953
3954 default:
3955 LOG(FATAL) << "Unexpected type conversion from " << input_type
3956 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08003957 }
Scott Wakelingfe885462016-09-22 10:24:38 +01003958 break;
3959
3960 default:
3961 LOG(FATAL) << "Unexpected type conversion from " << input_type
3962 << " to " << result_type;
3963 }
3964}
3965
3966void InstructionCodeGeneratorARMVIXL::VisitTypeConversion(HTypeConversion* conversion) {
3967 LocationSummary* locations = conversion->GetLocations();
3968 Location out = locations->Out();
3969 Location in = locations->InAt(0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003970 DataType::Type result_type = conversion->GetResultType();
3971 DataType::Type input_type = conversion->GetInputType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003972 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
3973 << input_type << " -> " << result_type;
Scott Wakelingfe885462016-09-22 10:24:38 +01003974 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003975 case DataType::Type::kUint8:
Scott Wakelingfe885462016-09-22 10:24:38 +01003976 switch (input_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003977 case DataType::Type::kInt8:
3978 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003979 case DataType::Type::kInt16:
3980 case DataType::Type::kInt32:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003981 __ Ubfx(OutputRegister(conversion), InputRegisterAt(conversion, 0), 0, 8);
3982 break;
3983 case DataType::Type::kInt64:
3984 __ Ubfx(OutputRegister(conversion), LowRegisterFrom(in), 0, 8);
3985 break;
3986
3987 default:
3988 LOG(FATAL) << "Unexpected type conversion from " << input_type
3989 << " to " << result_type;
3990 }
3991 break;
3992
3993 case DataType::Type::kInt8:
3994 switch (input_type) {
3995 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003996 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003997 case DataType::Type::kInt16:
3998 case DataType::Type::kInt32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003999 __ Sbfx(OutputRegister(conversion), InputRegisterAt(conversion, 0), 0, 8);
4000 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004001 case DataType::Type::kInt64:
4002 __ Sbfx(OutputRegister(conversion), LowRegisterFrom(in), 0, 8);
4003 break;
4004
4005 default:
4006 LOG(FATAL) << "Unexpected type conversion from " << input_type
4007 << " to " << result_type;
4008 }
4009 break;
4010
4011 case DataType::Type::kUint16:
4012 switch (input_type) {
4013 case DataType::Type::kInt8:
4014 case DataType::Type::kInt16:
4015 case DataType::Type::kInt32:
4016 __ Ubfx(OutputRegister(conversion), InputRegisterAt(conversion, 0), 0, 16);
4017 break;
4018 case DataType::Type::kInt64:
4019 __ Ubfx(OutputRegister(conversion), LowRegisterFrom(in), 0, 16);
4020 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01004021
4022 default:
4023 LOG(FATAL) << "Unexpected type conversion from " << input_type
4024 << " to " << result_type;
4025 }
4026 break;
4027
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004028 case DataType::Type::kInt16:
Scott Wakelingfe885462016-09-22 10:24:38 +01004029 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004030 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004031 case DataType::Type::kInt32:
Scott Wakelingfe885462016-09-22 10:24:38 +01004032 __ Sbfx(OutputRegister(conversion), InputRegisterAt(conversion, 0), 0, 16);
4033 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004034 case DataType::Type::kInt64:
4035 __ Sbfx(OutputRegister(conversion), LowRegisterFrom(in), 0, 16);
4036 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01004037
4038 default:
4039 LOG(FATAL) << "Unexpected type conversion from " << input_type
4040 << " to " << result_type;
4041 }
4042 break;
4043
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004044 case DataType::Type::kInt32:
Scott Wakelingfe885462016-09-22 10:24:38 +01004045 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004046 case DataType::Type::kInt64:
Scott Wakelingfe885462016-09-22 10:24:38 +01004047 DCHECK(out.IsRegister());
4048 if (in.IsRegisterPair()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004049 __ Mov(OutputRegister(conversion), LowRegisterFrom(in));
Scott Wakelingfe885462016-09-22 10:24:38 +01004050 } else if (in.IsDoubleStackSlot()) {
4051 GetAssembler()->LoadFromOffset(kLoadWord,
4052 OutputRegister(conversion),
4053 sp,
4054 in.GetStackIndex());
4055 } else {
4056 DCHECK(in.IsConstant());
4057 DCHECK(in.GetConstant()->IsLongConstant());
Vladimir Markoba1a48e2017-04-13 11:50:14 +01004058 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
4059 __ Mov(OutputRegister(conversion), static_cast<int32_t>(value));
Scott Wakelingfe885462016-09-22 10:24:38 +01004060 }
4061 break;
4062
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004063 case DataType::Type::kFloat32: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004064 vixl32::SRegister temp = LowSRegisterFrom(locations->GetTemp(0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01004065 __ Vcvt(S32, F32, temp, InputSRegisterAt(conversion, 0));
Scott Wakelingfe885462016-09-22 10:24:38 +01004066 __ Vmov(OutputRegister(conversion), temp);
4067 break;
4068 }
4069
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004070 case DataType::Type::kFloat64: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004071 vixl32::SRegister temp_s = LowSRegisterFrom(locations->GetTemp(0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01004072 __ Vcvt(S32, F64, temp_s, DRegisterFrom(in));
Scott Wakelingfe885462016-09-22 10:24:38 +01004073 __ Vmov(OutputRegister(conversion), temp_s);
4074 break;
4075 }
4076
4077 default:
4078 LOG(FATAL) << "Unexpected type conversion from " << input_type
4079 << " to " << result_type;
4080 }
4081 break;
4082
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004083 case DataType::Type::kInt64:
Scott Wakelingfe885462016-09-22 10:24:38 +01004084 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004085 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004086 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004087 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004088 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004089 case DataType::Type::kInt16:
4090 case DataType::Type::kInt32:
Scott Wakelingfe885462016-09-22 10:24:38 +01004091 DCHECK(out.IsRegisterPair());
4092 DCHECK(in.IsRegister());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004093 __ Mov(LowRegisterFrom(out), InputRegisterAt(conversion, 0));
Scott Wakelingfe885462016-09-22 10:24:38 +01004094 // Sign extension.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004095 __ Asr(HighRegisterFrom(out), LowRegisterFrom(out), 31);
Scott Wakelingfe885462016-09-22 10:24:38 +01004096 break;
4097
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004098 case DataType::Type::kFloat32:
Scott Wakelingfe885462016-09-22 10:24:38 +01004099 codegen_->InvokeRuntime(kQuickF2l, conversion, conversion->GetDexPc());
4100 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
4101 break;
4102
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004103 case DataType::Type::kFloat64:
Scott Wakelingfe885462016-09-22 10:24:38 +01004104 codegen_->InvokeRuntime(kQuickD2l, conversion, conversion->GetDexPc());
4105 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
4106 break;
4107
4108 default:
4109 LOG(FATAL) << "Unexpected type conversion from " << input_type
4110 << " to " << result_type;
4111 }
4112 break;
4113
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004114 case DataType::Type::kFloat32:
Scott Wakelingfe885462016-09-22 10:24:38 +01004115 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004116 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004117 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004118 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004119 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004120 case DataType::Type::kInt16:
4121 case DataType::Type::kInt32:
Scott Wakelingfe885462016-09-22 10:24:38 +01004122 __ Vmov(OutputSRegister(conversion), InputRegisterAt(conversion, 0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01004123 __ Vcvt(F32, S32, OutputSRegister(conversion), OutputSRegister(conversion));
Scott Wakelingfe885462016-09-22 10:24:38 +01004124 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01004125
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004126 case DataType::Type::kInt64:
Scott Wakelingfe885462016-09-22 10:24:38 +01004127 codegen_->InvokeRuntime(kQuickL2f, conversion, conversion->GetDexPc());
4128 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
4129 break;
4130
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004131 case DataType::Type::kFloat64:
Scott Wakelingc34dba72016-10-03 10:14:44 +01004132 __ Vcvt(F32, F64, OutputSRegister(conversion), DRegisterFrom(in));
Scott Wakelingfe885462016-09-22 10:24:38 +01004133 break;
4134
4135 default:
4136 LOG(FATAL) << "Unexpected type conversion from " << input_type
4137 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08004138 }
Scott Wakelingfe885462016-09-22 10:24:38 +01004139 break;
4140
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004141 case DataType::Type::kFloat64:
Scott Wakelingfe885462016-09-22 10:24:38 +01004142 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004143 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004144 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004145 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004146 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004147 case DataType::Type::kInt16:
4148 case DataType::Type::kInt32:
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004149 __ Vmov(LowSRegisterFrom(out), InputRegisterAt(conversion, 0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01004150 __ Vcvt(F64, S32, DRegisterFrom(out), LowSRegisterFrom(out));
Scott Wakelingfe885462016-09-22 10:24:38 +01004151 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01004152
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004153 case DataType::Type::kInt64: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004154 vixl32::Register low = LowRegisterFrom(in);
4155 vixl32::Register high = HighRegisterFrom(in);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004156 vixl32::SRegister out_s = LowSRegisterFrom(out);
Scott Wakelingc34dba72016-10-03 10:14:44 +01004157 vixl32::DRegister out_d = DRegisterFrom(out);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004158 vixl32::SRegister temp_s = LowSRegisterFrom(locations->GetTemp(0));
Scott Wakelingc34dba72016-10-03 10:14:44 +01004159 vixl32::DRegister temp_d = DRegisterFrom(locations->GetTemp(0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01004160 vixl32::DRegister constant_d = DRegisterFrom(locations->GetTemp(1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004161
4162 // temp_d = int-to-double(high)
4163 __ Vmov(temp_s, high);
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01004164 __ Vcvt(F64, S32, temp_d, temp_s);
Scott Wakelingfe885462016-09-22 10:24:38 +01004165 // constant_d = k2Pow32EncodingForDouble
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004166 __ Vmov(constant_d, bit_cast<double, int64_t>(k2Pow32EncodingForDouble));
Scott Wakelingfe885462016-09-22 10:24:38 +01004167 // out_d = unsigned-to-double(low)
4168 __ Vmov(out_s, low);
4169 __ Vcvt(F64, U32, out_d, out_s);
4170 // out_d += temp_d * constant_d
4171 __ Vmla(F64, out_d, temp_d, constant_d);
4172 break;
4173 }
4174
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004175 case DataType::Type::kFloat32:
Scott Wakelingc34dba72016-10-03 10:14:44 +01004176 __ Vcvt(F64, F32, DRegisterFrom(out), InputSRegisterAt(conversion, 0));
Scott Wakelingfe885462016-09-22 10:24:38 +01004177 break;
4178
4179 default:
4180 LOG(FATAL) << "Unexpected type conversion from " << input_type
4181 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08004182 }
Scott Wakelingfe885462016-09-22 10:24:38 +01004183 break;
4184
4185 default:
4186 LOG(FATAL) << "Unexpected type conversion from " << input_type
4187 << " to " << result_type;
4188 }
4189}
4190
4191void LocationsBuilderARMVIXL::VisitAdd(HAdd* add) {
4192 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004193 new (GetGraph()->GetAllocator()) LocationSummary(add, LocationSummary::kNoCall);
Scott Wakelingfe885462016-09-22 10:24:38 +01004194 switch (add->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004195 case DataType::Type::kInt32: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004196 locations->SetInAt(0, Location::RequiresRegister());
4197 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
4198 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4199 break;
4200 }
4201
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004202 case DataType::Type::kInt64: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004203 locations->SetInAt(0, Location::RequiresRegister());
Anton Kirilovdda43962016-11-21 19:55:20 +00004204 locations->SetInAt(1, ArmEncodableConstantOrRegister(add->InputAt(1), ADD));
Scott Wakelingfe885462016-09-22 10:24:38 +01004205 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4206 break;
4207 }
4208
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004209 case DataType::Type::kFloat32:
4210 case DataType::Type::kFloat64: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004211 locations->SetInAt(0, Location::RequiresFpuRegister());
4212 locations->SetInAt(1, Location::RequiresFpuRegister());
4213 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4214 break;
4215 }
4216
4217 default:
4218 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
4219 }
4220}
4221
4222void InstructionCodeGeneratorARMVIXL::VisitAdd(HAdd* add) {
4223 LocationSummary* locations = add->GetLocations();
4224 Location out = locations->Out();
4225 Location first = locations->InAt(0);
4226 Location second = locations->InAt(1);
4227
4228 switch (add->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004229 case DataType::Type::kInt32: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004230 __ Add(OutputRegister(add), InputRegisterAt(add, 0), InputOperandAt(add, 1));
4231 }
4232 break;
4233
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004234 case DataType::Type::kInt64: {
Anton Kirilovdda43962016-11-21 19:55:20 +00004235 if (second.IsConstant()) {
4236 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
4237 GenerateAddLongConst(out, first, value);
4238 } else {
4239 DCHECK(second.IsRegisterPair());
4240 __ Adds(LowRegisterFrom(out), LowRegisterFrom(first), LowRegisterFrom(second));
4241 __ Adc(HighRegisterFrom(out), HighRegisterFrom(first), HighRegisterFrom(second));
4242 }
Scott Wakelingfe885462016-09-22 10:24:38 +01004243 break;
4244 }
4245
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004246 case DataType::Type::kFloat32:
4247 case DataType::Type::kFloat64:
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004248 __ Vadd(OutputVRegister(add), InputVRegisterAt(add, 0), InputVRegisterAt(add, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004249 break;
4250
4251 default:
4252 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
4253 }
4254}
4255
4256void LocationsBuilderARMVIXL::VisitSub(HSub* sub) {
4257 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004258 new (GetGraph()->GetAllocator()) LocationSummary(sub, LocationSummary::kNoCall);
Scott Wakelingfe885462016-09-22 10:24:38 +01004259 switch (sub->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004260 case DataType::Type::kInt32: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004261 locations->SetInAt(0, Location::RequiresRegister());
4262 locations->SetInAt(1, Location::RegisterOrConstant(sub->InputAt(1)));
4263 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4264 break;
4265 }
4266
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004267 case DataType::Type::kInt64: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004268 locations->SetInAt(0, Location::RequiresRegister());
Anton Kirilovdda43962016-11-21 19:55:20 +00004269 locations->SetInAt(1, ArmEncodableConstantOrRegister(sub->InputAt(1), SUB));
Scott Wakelingfe885462016-09-22 10:24:38 +01004270 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4271 break;
4272 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004273 case DataType::Type::kFloat32:
4274 case DataType::Type::kFloat64: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004275 locations->SetInAt(0, Location::RequiresFpuRegister());
4276 locations->SetInAt(1, Location::RequiresFpuRegister());
4277 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4278 break;
4279 }
4280 default:
4281 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
4282 }
4283}
4284
4285void InstructionCodeGeneratorARMVIXL::VisitSub(HSub* sub) {
4286 LocationSummary* locations = sub->GetLocations();
4287 Location out = locations->Out();
4288 Location first = locations->InAt(0);
4289 Location second = locations->InAt(1);
4290 switch (sub->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004291 case DataType::Type::kInt32: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004292 __ Sub(OutputRegister(sub), InputRegisterAt(sub, 0), InputOperandAt(sub, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004293 break;
4294 }
4295
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004296 case DataType::Type::kInt64: {
Anton Kirilovdda43962016-11-21 19:55:20 +00004297 if (second.IsConstant()) {
4298 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
4299 GenerateAddLongConst(out, first, -value);
4300 } else {
4301 DCHECK(second.IsRegisterPair());
4302 __ Subs(LowRegisterFrom(out), LowRegisterFrom(first), LowRegisterFrom(second));
4303 __ Sbc(HighRegisterFrom(out), HighRegisterFrom(first), HighRegisterFrom(second));
4304 }
Scott Wakelingfe885462016-09-22 10:24:38 +01004305 break;
4306 }
4307
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004308 case DataType::Type::kFloat32:
4309 case DataType::Type::kFloat64:
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004310 __ Vsub(OutputVRegister(sub), InputVRegisterAt(sub, 0), InputVRegisterAt(sub, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004311 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01004312
4313 default:
4314 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
4315 }
4316}
4317
4318void LocationsBuilderARMVIXL::VisitMul(HMul* mul) {
4319 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004320 new (GetGraph()->GetAllocator()) LocationSummary(mul, LocationSummary::kNoCall);
Scott Wakelingfe885462016-09-22 10:24:38 +01004321 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004322 case DataType::Type::kInt32:
4323 case DataType::Type::kInt64: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004324 locations->SetInAt(0, Location::RequiresRegister());
4325 locations->SetInAt(1, Location::RequiresRegister());
4326 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4327 break;
4328 }
4329
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004330 case DataType::Type::kFloat32:
4331 case DataType::Type::kFloat64: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004332 locations->SetInAt(0, Location::RequiresFpuRegister());
4333 locations->SetInAt(1, Location::RequiresFpuRegister());
4334 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4335 break;
4336 }
4337
4338 default:
4339 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4340 }
4341}
4342
4343void InstructionCodeGeneratorARMVIXL::VisitMul(HMul* mul) {
4344 LocationSummary* locations = mul->GetLocations();
4345 Location out = locations->Out();
4346 Location first = locations->InAt(0);
4347 Location second = locations->InAt(1);
4348 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004349 case DataType::Type::kInt32: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004350 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
4351 break;
4352 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004353 case DataType::Type::kInt64: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004354 vixl32::Register out_hi = HighRegisterFrom(out);
4355 vixl32::Register out_lo = LowRegisterFrom(out);
4356 vixl32::Register in1_hi = HighRegisterFrom(first);
4357 vixl32::Register in1_lo = LowRegisterFrom(first);
4358 vixl32::Register in2_hi = HighRegisterFrom(second);
4359 vixl32::Register in2_lo = LowRegisterFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01004360
4361 // Extra checks to protect caused by the existence of R1_R2.
4362 // The algorithm is wrong if out.hi is either in1.lo or in2.lo:
4363 // (e.g. in1=r0_r1, in2=r2_r3 and out=r1_r2);
Anton Kirilov644032c2016-12-06 17:51:43 +00004364 DCHECK(!out_hi.Is(in1_lo));
4365 DCHECK(!out_hi.Is(in2_lo));
Scott Wakelingfe885462016-09-22 10:24:38 +01004366
4367 // input: in1 - 64 bits, in2 - 64 bits
4368 // output: out
4369 // formula: out.hi : out.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
4370 // parts: out.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
4371 // parts: out.lo = (in1.lo * in2.lo)[31:0]
4372
4373 UseScratchRegisterScope temps(GetVIXLAssembler());
4374 vixl32::Register temp = temps.Acquire();
4375 // temp <- in1.lo * in2.hi
4376 __ Mul(temp, in1_lo, in2_hi);
4377 // out.hi <- in1.lo * in2.hi + in1.hi * in2.lo
4378 __ Mla(out_hi, in1_hi, in2_lo, temp);
4379 // out.lo <- (in1.lo * in2.lo)[31:0];
4380 __ Umull(out_lo, temp, in1_lo, in2_lo);
4381 // out.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004382 __ Add(out_hi, out_hi, temp);
Scott Wakelingfe885462016-09-22 10:24:38 +01004383 break;
4384 }
4385
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004386 case DataType::Type::kFloat32:
4387 case DataType::Type::kFloat64:
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004388 __ Vmul(OutputVRegister(mul), InputVRegisterAt(mul, 0), InputVRegisterAt(mul, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004389 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01004390
4391 default:
4392 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4393 }
4394}
4395
Scott Wakelingfe885462016-09-22 10:24:38 +01004396void InstructionCodeGeneratorARMVIXL::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
4397 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004398 DCHECK(instruction->GetResultType() == DataType::Type::kInt32);
Scott Wakelingfe885462016-09-22 10:24:38 +01004399
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004400 Location second = instruction->GetLocations()->InAt(1);
Scott Wakelingfe885462016-09-22 10:24:38 +01004401 DCHECK(second.IsConstant());
4402
4403 vixl32::Register out = OutputRegister(instruction);
4404 vixl32::Register dividend = InputRegisterAt(instruction, 0);
Anton Kirilov644032c2016-12-06 17:51:43 +00004405 int32_t imm = Int32ConstantFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01004406 DCHECK(imm == 1 || imm == -1);
4407
4408 if (instruction->IsRem()) {
4409 __ Mov(out, 0);
4410 } else {
4411 if (imm == 1) {
4412 __ Mov(out, dividend);
4413 } else {
4414 __ Rsb(out, dividend, 0);
4415 }
4416 }
4417}
4418
4419void InstructionCodeGeneratorARMVIXL::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
4420 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004421 DCHECK(instruction->GetResultType() == DataType::Type::kInt32);
Scott Wakelingfe885462016-09-22 10:24:38 +01004422
4423 LocationSummary* locations = instruction->GetLocations();
4424 Location second = locations->InAt(1);
4425 DCHECK(second.IsConstant());
4426
4427 vixl32::Register out = OutputRegister(instruction);
4428 vixl32::Register dividend = InputRegisterAt(instruction, 0);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004429 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
Anton Kirilov644032c2016-12-06 17:51:43 +00004430 int32_t imm = Int32ConstantFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01004431 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
4432 int ctz_imm = CTZ(abs_imm);
4433
4434 if (ctz_imm == 1) {
4435 __ Lsr(temp, dividend, 32 - ctz_imm);
4436 } else {
4437 __ Asr(temp, dividend, 31);
4438 __ Lsr(temp, temp, 32 - ctz_imm);
4439 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004440 __ Add(out, temp, dividend);
Scott Wakelingfe885462016-09-22 10:24:38 +01004441
4442 if (instruction->IsDiv()) {
4443 __ Asr(out, out, ctz_imm);
4444 if (imm < 0) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004445 __ Rsb(out, out, 0);
Scott Wakelingfe885462016-09-22 10:24:38 +01004446 }
4447 } else {
4448 __ Ubfx(out, out, 0, ctz_imm);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004449 __ Sub(out, out, temp);
Scott Wakelingfe885462016-09-22 10:24:38 +01004450 }
4451}
4452
4453void InstructionCodeGeneratorARMVIXL::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
4454 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004455 DCHECK(instruction->GetResultType() == DataType::Type::kInt32);
Scott Wakelingfe885462016-09-22 10:24:38 +01004456
4457 LocationSummary* locations = instruction->GetLocations();
4458 Location second = locations->InAt(1);
4459 DCHECK(second.IsConstant());
4460
4461 vixl32::Register out = OutputRegister(instruction);
4462 vixl32::Register dividend = InputRegisterAt(instruction, 0);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004463 vixl32::Register temp1 = RegisterFrom(locations->GetTemp(0));
4464 vixl32::Register temp2 = RegisterFrom(locations->GetTemp(1));
Scott Wakelingb77051e2016-11-21 19:46:00 +00004465 int32_t imm = Int32ConstantFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01004466
4467 int64_t magic;
4468 int shift;
4469 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
4470
Anton Kirilovdda43962016-11-21 19:55:20 +00004471 // TODO(VIXL): Change the static cast to Operand::From() after VIXL is fixed.
4472 __ Mov(temp1, static_cast<int32_t>(magic));
Scott Wakelingfe885462016-09-22 10:24:38 +01004473 __ Smull(temp2, temp1, dividend, temp1);
4474
4475 if (imm > 0 && magic < 0) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004476 __ Add(temp1, temp1, dividend);
Scott Wakelingfe885462016-09-22 10:24:38 +01004477 } else if (imm < 0 && magic > 0) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004478 __ Sub(temp1, temp1, dividend);
Scott Wakelingfe885462016-09-22 10:24:38 +01004479 }
4480
4481 if (shift != 0) {
4482 __ Asr(temp1, temp1, shift);
4483 }
4484
4485 if (instruction->IsDiv()) {
4486 __ Sub(out, temp1, Operand(temp1, vixl32::Shift(ASR), 31));
4487 } else {
4488 __ Sub(temp1, temp1, Operand(temp1, vixl32::Shift(ASR), 31));
4489 // TODO: Strength reduction for mls.
4490 __ Mov(temp2, imm);
4491 __ Mls(out, temp1, temp2, dividend);
4492 }
4493}
4494
4495void InstructionCodeGeneratorARMVIXL::GenerateDivRemConstantIntegral(
4496 HBinaryOperation* instruction) {
4497 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004498 DCHECK(instruction->GetResultType() == DataType::Type::kInt32);
Scott Wakelingfe885462016-09-22 10:24:38 +01004499
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004500 Location second = instruction->GetLocations()->InAt(1);
Scott Wakelingfe885462016-09-22 10:24:38 +01004501 DCHECK(second.IsConstant());
4502
Anton Kirilov644032c2016-12-06 17:51:43 +00004503 int32_t imm = Int32ConstantFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01004504 if (imm == 0) {
4505 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
4506 } else if (imm == 1 || imm == -1) {
4507 DivRemOneOrMinusOne(instruction);
4508 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
4509 DivRemByPowerOfTwo(instruction);
4510 } else {
4511 DCHECK(imm <= -2 || imm >= 2);
4512 GenerateDivRemWithAnyConstant(instruction);
4513 }
4514}
4515
4516void LocationsBuilderARMVIXL::VisitDiv(HDiv* div) {
4517 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004518 if (div->GetResultType() == DataType::Type::kInt64) {
Scott Wakelingfe885462016-09-22 10:24:38 +01004519 // pLdiv runtime call.
4520 call_kind = LocationSummary::kCallOnMainOnly;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004521 } else if (div->GetResultType() == DataType::Type::kInt32 && div->InputAt(1)->IsConstant()) {
Scott Wakelingfe885462016-09-22 10:24:38 +01004522 // sdiv will be replaced by other instruction sequence.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004523 } else if (div->GetResultType() == DataType::Type::kInt32 &&
Scott Wakelingfe885462016-09-22 10:24:38 +01004524 !codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4525 // pIdivmod runtime call.
4526 call_kind = LocationSummary::kCallOnMainOnly;
4527 }
4528
Vladimir Markoca6fff82017-10-03 14:49:14 +01004529 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(div, call_kind);
Scott Wakelingfe885462016-09-22 10:24:38 +01004530
4531 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004532 case DataType::Type::kInt32: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004533 if (div->InputAt(1)->IsConstant()) {
4534 locations->SetInAt(0, Location::RequiresRegister());
4535 locations->SetInAt(1, Location::ConstantLocation(div->InputAt(1)->AsConstant()));
4536 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Anton Kirilov644032c2016-12-06 17:51:43 +00004537 int32_t value = Int32ConstantFrom(div->InputAt(1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004538 if (value == 1 || value == 0 || value == -1) {
4539 // No temp register required.
4540 } else {
4541 locations->AddTemp(Location::RequiresRegister());
4542 if (!IsPowerOfTwo(AbsOrMin(value))) {
4543 locations->AddTemp(Location::RequiresRegister());
4544 }
4545 }
4546 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4547 locations->SetInAt(0, Location::RequiresRegister());
4548 locations->SetInAt(1, Location::RequiresRegister());
4549 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4550 } else {
Artem Serov551b28f2016-10-18 19:11:30 +01004551 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4552 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4553 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
Roland Levillain5e8d5f02016-10-18 18:03:43 +01004554 // Note: divmod will compute both the quotient and the remainder as the pair R0 and R1, but
Artem Serov551b28f2016-10-18 19:11:30 +01004555 // we only need the former.
4556 locations->SetOut(LocationFrom(r0));
Scott Wakelingfe885462016-09-22 10:24:38 +01004557 }
4558 break;
4559 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004560 case DataType::Type::kInt64: {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01004561 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4562 locations->SetInAt(0, LocationFrom(
4563 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
4564 locations->SetInAt(1, LocationFrom(
4565 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
4566 locations->SetOut(LocationFrom(r0, r1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004567 break;
4568 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004569 case DataType::Type::kFloat32:
4570 case DataType::Type::kFloat64: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004571 locations->SetInAt(0, Location::RequiresFpuRegister());
4572 locations->SetInAt(1, Location::RequiresFpuRegister());
4573 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4574 break;
4575 }
4576
4577 default:
4578 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
4579 }
4580}
4581
4582void InstructionCodeGeneratorARMVIXL::VisitDiv(HDiv* div) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01004583 Location lhs = div->GetLocations()->InAt(0);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004584 Location rhs = div->GetLocations()->InAt(1);
Scott Wakelingfe885462016-09-22 10:24:38 +01004585
4586 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004587 case DataType::Type::kInt32: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004588 if (rhs.IsConstant()) {
Scott Wakelingfe885462016-09-22 10:24:38 +01004589 GenerateDivRemConstantIntegral(div);
4590 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4591 __ Sdiv(OutputRegister(div), InputRegisterAt(div, 0), InputRegisterAt(div, 1));
4592 } else {
Artem Serov551b28f2016-10-18 19:11:30 +01004593 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4594 DCHECK(calling_convention.GetRegisterAt(0).Is(RegisterFrom(lhs)));
4595 DCHECK(calling_convention.GetRegisterAt(1).Is(RegisterFrom(rhs)));
4596 DCHECK(r0.Is(OutputRegister(div)));
4597
4598 codegen_->InvokeRuntime(kQuickIdivmod, div, div->GetDexPc());
4599 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
Scott Wakelingfe885462016-09-22 10:24:38 +01004600 }
4601 break;
4602 }
4603
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004604 case DataType::Type::kInt64: {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01004605 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4606 DCHECK(calling_convention.GetRegisterAt(0).Is(LowRegisterFrom(lhs)));
4607 DCHECK(calling_convention.GetRegisterAt(1).Is(HighRegisterFrom(lhs)));
4608 DCHECK(calling_convention.GetRegisterAt(2).Is(LowRegisterFrom(rhs)));
4609 DCHECK(calling_convention.GetRegisterAt(3).Is(HighRegisterFrom(rhs)));
4610 DCHECK(LowRegisterFrom(div->GetLocations()->Out()).Is(r0));
4611 DCHECK(HighRegisterFrom(div->GetLocations()->Out()).Is(r1));
4612
4613 codegen_->InvokeRuntime(kQuickLdiv, div, div->GetDexPc());
4614 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Scott Wakelingfe885462016-09-22 10:24:38 +01004615 break;
4616 }
4617
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004618 case DataType::Type::kFloat32:
4619 case DataType::Type::kFloat64:
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004620 __ Vdiv(OutputVRegister(div), InputVRegisterAt(div, 0), InputVRegisterAt(div, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004621 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01004622
4623 default:
4624 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
4625 }
4626}
4627
Artem Serov551b28f2016-10-18 19:11:30 +01004628void LocationsBuilderARMVIXL::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004629 DataType::Type type = rem->GetResultType();
Artem Serov551b28f2016-10-18 19:11:30 +01004630
4631 // Most remainders are implemented in the runtime.
4632 LocationSummary::CallKind call_kind = LocationSummary::kCallOnMainOnly;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004633 if (rem->GetResultType() == DataType::Type::kInt32 && rem->InputAt(1)->IsConstant()) {
Artem Serov551b28f2016-10-18 19:11:30 +01004634 // sdiv will be replaced by other instruction sequence.
4635 call_kind = LocationSummary::kNoCall;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004636 } else if ((rem->GetResultType() == DataType::Type::kInt32)
Artem Serov551b28f2016-10-18 19:11:30 +01004637 && codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4638 // Have hardware divide instruction for int, do it with three instructions.
4639 call_kind = LocationSummary::kNoCall;
4640 }
4641
Vladimir Markoca6fff82017-10-03 14:49:14 +01004642 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(rem, call_kind);
Artem Serov551b28f2016-10-18 19:11:30 +01004643
4644 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004645 case DataType::Type::kInt32: {
Artem Serov551b28f2016-10-18 19:11:30 +01004646 if (rem->InputAt(1)->IsConstant()) {
4647 locations->SetInAt(0, Location::RequiresRegister());
4648 locations->SetInAt(1, Location::ConstantLocation(rem->InputAt(1)->AsConstant()));
4649 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Anton Kirilov644032c2016-12-06 17:51:43 +00004650 int32_t value = Int32ConstantFrom(rem->InputAt(1));
Artem Serov551b28f2016-10-18 19:11:30 +01004651 if (value == 1 || value == 0 || value == -1) {
4652 // No temp register required.
4653 } else {
4654 locations->AddTemp(Location::RequiresRegister());
4655 if (!IsPowerOfTwo(AbsOrMin(value))) {
4656 locations->AddTemp(Location::RequiresRegister());
4657 }
4658 }
4659 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4660 locations->SetInAt(0, Location::RequiresRegister());
4661 locations->SetInAt(1, Location::RequiresRegister());
4662 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4663 locations->AddTemp(Location::RequiresRegister());
4664 } else {
4665 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4666 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4667 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
Roland Levillain5e8d5f02016-10-18 18:03:43 +01004668 // Note: divmod will compute both the quotient and the remainder as the pair R0 and R1, but
Artem Serov551b28f2016-10-18 19:11:30 +01004669 // we only need the latter.
4670 locations->SetOut(LocationFrom(r1));
4671 }
4672 break;
4673 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004674 case DataType::Type::kInt64: {
Artem Serov551b28f2016-10-18 19:11:30 +01004675 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4676 locations->SetInAt(0, LocationFrom(
4677 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
4678 locations->SetInAt(1, LocationFrom(
4679 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
4680 // The runtime helper puts the output in R2,R3.
4681 locations->SetOut(LocationFrom(r2, r3));
4682 break;
4683 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004684 case DataType::Type::kFloat32: {
Artem Serov551b28f2016-10-18 19:11:30 +01004685 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4686 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
4687 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
4688 locations->SetOut(LocationFrom(s0));
4689 break;
4690 }
4691
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004692 case DataType::Type::kFloat64: {
Artem Serov551b28f2016-10-18 19:11:30 +01004693 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4694 locations->SetInAt(0, LocationFrom(
4695 calling_convention.GetFpuRegisterAt(0), calling_convention.GetFpuRegisterAt(1)));
4696 locations->SetInAt(1, LocationFrom(
4697 calling_convention.GetFpuRegisterAt(2), calling_convention.GetFpuRegisterAt(3)));
4698 locations->SetOut(LocationFrom(s0, s1));
4699 break;
4700 }
4701
4702 default:
4703 LOG(FATAL) << "Unexpected rem type " << type;
4704 }
4705}
4706
4707void InstructionCodeGeneratorARMVIXL::VisitRem(HRem* rem) {
4708 LocationSummary* locations = rem->GetLocations();
4709 Location second = locations->InAt(1);
4710
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004711 DataType::Type type = rem->GetResultType();
Artem Serov551b28f2016-10-18 19:11:30 +01004712 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004713 case DataType::Type::kInt32: {
Artem Serov551b28f2016-10-18 19:11:30 +01004714 vixl32::Register reg1 = InputRegisterAt(rem, 0);
4715 vixl32::Register out_reg = OutputRegister(rem);
4716 if (second.IsConstant()) {
4717 GenerateDivRemConstantIntegral(rem);
4718 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4719 vixl32::Register reg2 = RegisterFrom(second);
4720 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
4721
4722 // temp = reg1 / reg2 (integer division)
4723 // dest = reg1 - temp * reg2
4724 __ Sdiv(temp, reg1, reg2);
4725 __ Mls(out_reg, temp, reg2, reg1);
4726 } else {
4727 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4728 DCHECK(reg1.Is(calling_convention.GetRegisterAt(0)));
4729 DCHECK(RegisterFrom(second).Is(calling_convention.GetRegisterAt(1)));
4730 DCHECK(out_reg.Is(r1));
4731
4732 codegen_->InvokeRuntime(kQuickIdivmod, rem, rem->GetDexPc());
4733 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
4734 }
4735 break;
4736 }
4737
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004738 case DataType::Type::kInt64: {
Artem Serov551b28f2016-10-18 19:11:30 +01004739 codegen_->InvokeRuntime(kQuickLmod, rem, rem->GetDexPc());
4740 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
4741 break;
4742 }
4743
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004744 case DataType::Type::kFloat32: {
Artem Serov551b28f2016-10-18 19:11:30 +01004745 codegen_->InvokeRuntime(kQuickFmodf, rem, rem->GetDexPc());
4746 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
4747 break;
4748 }
4749
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004750 case DataType::Type::kFloat64: {
Artem Serov551b28f2016-10-18 19:11:30 +01004751 codegen_->InvokeRuntime(kQuickFmod, rem, rem->GetDexPc());
4752 CheckEntrypointTypes<kQuickFmod, double, double, double>();
4753 break;
4754 }
4755
4756 default:
4757 LOG(FATAL) << "Unexpected rem type " << type;
4758 }
4759}
4760
Aart Bik1f8d51b2018-02-15 10:42:37 -08004761static void CreateMinMaxLocations(ArenaAllocator* allocator, HBinaryOperation* minmax) {
4762 LocationSummary* locations = new (allocator) LocationSummary(minmax);
4763 switch (minmax->GetResultType()) {
4764 case DataType::Type::kInt32:
4765 locations->SetInAt(0, Location::RequiresRegister());
4766 locations->SetInAt(1, Location::RequiresRegister());
4767 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4768 break;
4769 case DataType::Type::kInt64:
4770 locations->SetInAt(0, Location::RequiresRegister());
4771 locations->SetInAt(1, Location::RequiresRegister());
4772 locations->SetOut(Location::SameAsFirstInput());
4773 break;
4774 case DataType::Type::kFloat32:
4775 locations->SetInAt(0, Location::RequiresFpuRegister());
4776 locations->SetInAt(1, Location::RequiresFpuRegister());
4777 locations->SetOut(Location::SameAsFirstInput());
4778 locations->AddTemp(Location::RequiresRegister());
4779 break;
4780 case DataType::Type::kFloat64:
4781 locations->SetInAt(0, Location::RequiresFpuRegister());
4782 locations->SetInAt(1, Location::RequiresFpuRegister());
4783 locations->SetOut(Location::SameAsFirstInput());
4784 break;
4785 default:
4786 LOG(FATAL) << "Unexpected type for HMinMax " << minmax->GetResultType();
4787 }
4788}
4789
Aart Bik351df3e2018-03-07 11:54:57 -08004790void InstructionCodeGeneratorARMVIXL::GenerateMinMaxInt(LocationSummary* locations, bool is_min) {
Aart Bik1f8d51b2018-02-15 10:42:37 -08004791 Location op1_loc = locations->InAt(0);
4792 Location op2_loc = locations->InAt(1);
4793 Location out_loc = locations->Out();
4794
4795 vixl32::Register op1 = RegisterFrom(op1_loc);
4796 vixl32::Register op2 = RegisterFrom(op2_loc);
4797 vixl32::Register out = RegisterFrom(out_loc);
4798
4799 __ Cmp(op1, op2);
4800
4801 {
4802 ExactAssemblyScope aas(GetVIXLAssembler(),
4803 3 * kMaxInstructionSizeInBytes,
4804 CodeBufferCheckScope::kMaximumSize);
4805
4806 __ ite(is_min ? lt : gt);
4807 __ mov(is_min ? lt : gt, out, op1);
4808 __ mov(is_min ? ge : le, out, op2);
4809 }
4810}
4811
4812void InstructionCodeGeneratorARMVIXL::GenerateMinMaxLong(LocationSummary* locations, bool is_min) {
4813 Location op1_loc = locations->InAt(0);
4814 Location op2_loc = locations->InAt(1);
4815 Location out_loc = locations->Out();
4816
4817 // Optimization: don't generate any code if inputs are the same.
4818 if (op1_loc.Equals(op2_loc)) {
4819 DCHECK(out_loc.Equals(op1_loc)); // out_loc is set as SameAsFirstInput() in location builder.
4820 return;
4821 }
4822
4823 vixl32::Register op1_lo = LowRegisterFrom(op1_loc);
4824 vixl32::Register op1_hi = HighRegisterFrom(op1_loc);
4825 vixl32::Register op2_lo = LowRegisterFrom(op2_loc);
4826 vixl32::Register op2_hi = HighRegisterFrom(op2_loc);
4827 vixl32::Register out_lo = LowRegisterFrom(out_loc);
4828 vixl32::Register out_hi = HighRegisterFrom(out_loc);
4829 UseScratchRegisterScope temps(GetVIXLAssembler());
4830 const vixl32::Register temp = temps.Acquire();
4831
4832 DCHECK(op1_lo.Is(out_lo));
4833 DCHECK(op1_hi.Is(out_hi));
4834
4835 // Compare op1 >= op2, or op1 < op2.
4836 __ Cmp(out_lo, op2_lo);
4837 __ Sbcs(temp, out_hi, op2_hi);
4838
4839 // Now GE/LT condition code is correct for the long comparison.
4840 {
4841 vixl32::ConditionType cond = is_min ? ge : lt;
4842 ExactAssemblyScope it_scope(GetVIXLAssembler(),
4843 3 * kMaxInstructionSizeInBytes,
4844 CodeBufferCheckScope::kMaximumSize);
4845 __ itt(cond);
4846 __ mov(cond, out_lo, op2_lo);
4847 __ mov(cond, out_hi, op2_hi);
4848 }
4849}
4850
Aart Bik351df3e2018-03-07 11:54:57 -08004851void InstructionCodeGeneratorARMVIXL::GenerateMinMaxFloat(HInstruction* minmax, bool is_min) {
4852 LocationSummary* locations = minmax->GetLocations();
Aart Bik1f8d51b2018-02-15 10:42:37 -08004853 Location op1_loc = locations->InAt(0);
4854 Location op2_loc = locations->InAt(1);
4855 Location out_loc = locations->Out();
4856
4857 // Optimization: don't generate any code if inputs are the same.
4858 if (op1_loc.Equals(op2_loc)) {
4859 DCHECK(out_loc.Equals(op1_loc)); // out_loc is set as SameAsFirstInput() in location builder.
4860 return;
4861 }
4862
4863 vixl32::SRegister op1 = SRegisterFrom(op1_loc);
4864 vixl32::SRegister op2 = SRegisterFrom(op2_loc);
4865 vixl32::SRegister out = SRegisterFrom(out_loc);
4866
4867 UseScratchRegisterScope temps(GetVIXLAssembler());
4868 const vixl32::Register temp1 = temps.Acquire();
4869 vixl32::Register temp2 = RegisterFrom(locations->GetTemp(0));
4870 vixl32::Label nan, done;
Aart Bik351df3e2018-03-07 11:54:57 -08004871 vixl32::Label* final_label = codegen_->GetFinalLabel(minmax, &done);
Aart Bik1f8d51b2018-02-15 10:42:37 -08004872
4873 DCHECK(op1.Is(out));
4874
4875 __ Vcmp(op1, op2);
4876 __ Vmrs(RegisterOrAPSR_nzcv(kPcCode), FPSCR);
4877 __ B(vs, &nan, /* far_target */ false); // if un-ordered, go to NaN handling.
4878
4879 // op1 <> op2
4880 vixl32::ConditionType cond = is_min ? gt : lt;
4881 {
4882 ExactAssemblyScope it_scope(GetVIXLAssembler(),
4883 2 * kMaxInstructionSizeInBytes,
4884 CodeBufferCheckScope::kMaximumSize);
4885 __ it(cond);
4886 __ vmov(cond, F32, out, op2);
4887 }
4888 // for <>(not equal), we've done min/max calculation.
4889 __ B(ne, final_label, /* far_target */ false);
4890
4891 // handle op1 == op2, max(+0.0,-0.0), min(+0.0,-0.0).
4892 __ Vmov(temp1, op1);
4893 __ Vmov(temp2, op2);
4894 if (is_min) {
4895 __ Orr(temp1, temp1, temp2);
4896 } else {
4897 __ And(temp1, temp1, temp2);
4898 }
4899 __ Vmov(out, temp1);
4900 __ B(final_label);
4901
4902 // handle NaN input.
4903 __ Bind(&nan);
4904 __ Movt(temp1, High16Bits(kNanFloat)); // 0x7FC0xxxx is a NaN.
4905 __ Vmov(out, temp1);
4906
4907 if (done.IsReferenced()) {
4908 __ Bind(&done);
4909 }
4910}
4911
Aart Bik351df3e2018-03-07 11:54:57 -08004912void InstructionCodeGeneratorARMVIXL::GenerateMinMaxDouble(HInstruction* minmax, bool is_min) {
4913 LocationSummary* locations = minmax->GetLocations();
Aart Bik1f8d51b2018-02-15 10:42:37 -08004914 Location op1_loc = locations->InAt(0);
4915 Location op2_loc = locations->InAt(1);
4916 Location out_loc = locations->Out();
4917
4918 // Optimization: don't generate any code if inputs are the same.
4919 if (op1_loc.Equals(op2_loc)) {
4920 DCHECK(out_loc.Equals(op1_loc)); // out_loc is set as SameAsFirstInput() in.
4921 return;
4922 }
4923
4924 vixl32::DRegister op1 = DRegisterFrom(op1_loc);
4925 vixl32::DRegister op2 = DRegisterFrom(op2_loc);
4926 vixl32::DRegister out = DRegisterFrom(out_loc);
4927 vixl32::Label handle_nan_eq, done;
Aart Bik351df3e2018-03-07 11:54:57 -08004928 vixl32::Label* final_label = codegen_->GetFinalLabel(minmax, &done);
Aart Bik1f8d51b2018-02-15 10:42:37 -08004929
4930 DCHECK(op1.Is(out));
4931
4932 __ Vcmp(op1, op2);
4933 __ Vmrs(RegisterOrAPSR_nzcv(kPcCode), FPSCR);
4934 __ B(vs, &handle_nan_eq, /* far_target */ false); // if un-ordered, go to NaN handling.
4935
4936 // op1 <> op2
4937 vixl32::ConditionType cond = is_min ? gt : lt;
4938 {
4939 ExactAssemblyScope it_scope(GetVIXLAssembler(),
4940 2 * kMaxInstructionSizeInBytes,
4941 CodeBufferCheckScope::kMaximumSize);
4942 __ it(cond);
4943 __ vmov(cond, F64, out, op2);
4944 }
4945 // for <>(not equal), we've done min/max calculation.
4946 __ B(ne, final_label, /* far_target */ false);
4947
4948 // handle op1 == op2, max(+0.0,-0.0).
4949 if (!is_min) {
4950 __ Vand(F64, out, op1, op2);
4951 __ B(final_label);
4952 }
4953
4954 // handle op1 == op2, min(+0.0,-0.0), NaN input.
4955 __ Bind(&handle_nan_eq);
4956 __ Vorr(F64, out, op1, op2); // assemble op1/-0.0/NaN.
4957
4958 if (done.IsReferenced()) {
4959 __ Bind(&done);
4960 }
4961}
4962
Aart Bik351df3e2018-03-07 11:54:57 -08004963void InstructionCodeGeneratorARMVIXL::GenerateMinMax(HBinaryOperation* minmax, bool is_min) {
4964 DataType::Type type = minmax->GetResultType();
4965 switch (type) {
4966 case DataType::Type::kInt32:
4967 GenerateMinMaxInt(minmax->GetLocations(), is_min);
4968 break;
4969 case DataType::Type::kInt64:
4970 GenerateMinMaxLong(minmax->GetLocations(), is_min);
4971 break;
4972 case DataType::Type::kFloat32:
4973 GenerateMinMaxFloat(minmax, is_min);
4974 break;
4975 case DataType::Type::kFloat64:
4976 GenerateMinMaxDouble(minmax, is_min);
4977 break;
4978 default:
4979 LOG(FATAL) << "Unexpected type for HMinMax " << type;
4980 }
4981}
4982
Aart Bik1f8d51b2018-02-15 10:42:37 -08004983void LocationsBuilderARMVIXL::VisitMin(HMin* min) {
4984 CreateMinMaxLocations(GetGraph()->GetAllocator(), min);
4985}
4986
4987void InstructionCodeGeneratorARMVIXL::VisitMin(HMin* min) {
Aart Bik351df3e2018-03-07 11:54:57 -08004988 GenerateMinMax(min, /*is_min*/ true);
Aart Bik1f8d51b2018-02-15 10:42:37 -08004989}
4990
4991void LocationsBuilderARMVIXL::VisitMax(HMax* max) {
4992 CreateMinMaxLocations(GetGraph()->GetAllocator(), max);
4993}
4994
4995void InstructionCodeGeneratorARMVIXL::VisitMax(HMax* max) {
Aart Bik351df3e2018-03-07 11:54:57 -08004996 GenerateMinMax(max, /*is_min*/ false);
Aart Bik1f8d51b2018-02-15 10:42:37 -08004997}
4998
Aart Bik3dad3412018-02-28 12:01:46 -08004999void LocationsBuilderARMVIXL::VisitAbs(HAbs* abs) {
5000 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(abs);
5001 switch (abs->GetResultType()) {
5002 case DataType::Type::kInt32:
5003 case DataType::Type::kInt64:
5004 locations->SetInAt(0, Location::RequiresRegister());
5005 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5006 locations->AddTemp(Location::RequiresRegister());
5007 break;
5008 case DataType::Type::kFloat32:
5009 case DataType::Type::kFloat64:
5010 locations->SetInAt(0, Location::RequiresFpuRegister());
5011 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5012 break;
5013 default:
5014 LOG(FATAL) << "Unexpected type for abs operation " << abs->GetResultType();
5015 }
5016}
5017
5018void InstructionCodeGeneratorARMVIXL::VisitAbs(HAbs* abs) {
5019 LocationSummary* locations = abs->GetLocations();
5020 switch (abs->GetResultType()) {
5021 case DataType::Type::kInt32: {
5022 vixl32::Register in_reg = RegisterFrom(locations->InAt(0));
5023 vixl32::Register out_reg = RegisterFrom(locations->Out());
5024 vixl32::Register mask = RegisterFrom(locations->GetTemp(0));
5025 __ Asr(mask, in_reg, 31);
5026 __ Add(out_reg, in_reg, mask);
5027 __ Eor(out_reg, out_reg, mask);
5028 break;
5029 }
5030 case DataType::Type::kInt64: {
5031 Location in = locations->InAt(0);
5032 vixl32::Register in_reg_lo = LowRegisterFrom(in);
5033 vixl32::Register in_reg_hi = HighRegisterFrom(in);
5034 Location output = locations->Out();
5035 vixl32::Register out_reg_lo = LowRegisterFrom(output);
5036 vixl32::Register out_reg_hi = HighRegisterFrom(output);
5037 DCHECK(!out_reg_lo.Is(in_reg_hi)) << "Diagonal overlap unexpected.";
5038 vixl32::Register mask = RegisterFrom(locations->GetTemp(0));
5039 __ Asr(mask, in_reg_hi, 31);
5040 __ Adds(out_reg_lo, in_reg_lo, mask);
5041 __ Adc(out_reg_hi, in_reg_hi, mask);
5042 __ Eor(out_reg_lo, out_reg_lo, mask);
5043 __ Eor(out_reg_hi, out_reg_hi, mask);
5044 break;
5045 }
5046 case DataType::Type::kFloat32:
5047 case DataType::Type::kFloat64:
5048 __ Vabs(OutputVRegister(abs), InputVRegisterAt(abs, 0));
5049 break;
5050 default:
5051 LOG(FATAL) << "Unexpected type for abs operation " << abs->GetResultType();
5052 }
5053}
Artem Serov551b28f2016-10-18 19:11:30 +01005054
Scott Wakelingfe885462016-09-22 10:24:38 +01005055void LocationsBuilderARMVIXL::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Artem Serov657022c2016-11-23 14:19:38 +00005056 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Scott Wakelingfe885462016-09-22 10:24:38 +01005057 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Scott Wakelingfe885462016-09-22 10:24:38 +01005058}
5059
5060void InstructionCodeGeneratorARMVIXL::VisitDivZeroCheck(HDivZeroCheck* instruction) {
5061 DivZeroCheckSlowPathARMVIXL* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01005062 new (codegen_->GetScopedAllocator()) DivZeroCheckSlowPathARMVIXL(instruction);
Scott Wakelingfe885462016-09-22 10:24:38 +01005063 codegen_->AddSlowPath(slow_path);
5064
5065 LocationSummary* locations = instruction->GetLocations();
5066 Location value = locations->InAt(0);
5067
5068 switch (instruction->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005069 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005070 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005071 case DataType::Type::kInt8:
5072 case DataType::Type::kUint16:
5073 case DataType::Type::kInt16:
5074 case DataType::Type::kInt32: {
Scott Wakelingfe885462016-09-22 10:24:38 +01005075 if (value.IsRegister()) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00005076 __ CompareAndBranchIfZero(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
Scott Wakelingfe885462016-09-22 10:24:38 +01005077 } else {
5078 DCHECK(value.IsConstant()) << value;
Anton Kirilov644032c2016-12-06 17:51:43 +00005079 if (Int32ConstantFrom(value) == 0) {
Scott Wakelingfe885462016-09-22 10:24:38 +01005080 __ B(slow_path->GetEntryLabel());
5081 }
5082 }
5083 break;
5084 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005085 case DataType::Type::kInt64: {
Scott Wakelingfe885462016-09-22 10:24:38 +01005086 if (value.IsRegisterPair()) {
5087 UseScratchRegisterScope temps(GetVIXLAssembler());
5088 vixl32::Register temp = temps.Acquire();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005089 __ Orrs(temp, LowRegisterFrom(value), HighRegisterFrom(value));
Scott Wakelingfe885462016-09-22 10:24:38 +01005090 __ B(eq, slow_path->GetEntryLabel());
5091 } else {
5092 DCHECK(value.IsConstant()) << value;
Anton Kirilov644032c2016-12-06 17:51:43 +00005093 if (Int64ConstantFrom(value) == 0) {
Scott Wakelingfe885462016-09-22 10:24:38 +01005094 __ B(slow_path->GetEntryLabel());
5095 }
5096 }
5097 break;
5098 }
5099 default:
5100 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
5101 }
5102}
5103
Artem Serov02109dd2016-09-23 17:17:54 +01005104void InstructionCodeGeneratorARMVIXL::HandleIntegerRotate(HRor* ror) {
5105 LocationSummary* locations = ror->GetLocations();
5106 vixl32::Register in = InputRegisterAt(ror, 0);
5107 Location rhs = locations->InAt(1);
5108 vixl32::Register out = OutputRegister(ror);
5109
5110 if (rhs.IsConstant()) {
5111 // Arm32 and Thumb2 assemblers require a rotation on the interval [1,31],
5112 // so map all rotations to a +ve. equivalent in that range.
5113 // (e.g. left *or* right by -2 bits == 30 bits in the same direction.)
5114 uint32_t rot = CodeGenerator::GetInt32ValueOf(rhs.GetConstant()) & 0x1F;
5115 if (rot) {
5116 // Rotate, mapping left rotations to right equivalents if necessary.
5117 // (e.g. left by 2 bits == right by 30.)
5118 __ Ror(out, in, rot);
5119 } else if (!out.Is(in)) {
5120 __ Mov(out, in);
5121 }
5122 } else {
5123 __ Ror(out, in, RegisterFrom(rhs));
5124 }
5125}
5126
5127// Gain some speed by mapping all Long rotates onto equivalent pairs of Integer
5128// rotates by swapping input regs (effectively rotating by the first 32-bits of
5129// a larger rotation) or flipping direction (thus treating larger right/left
5130// rotations as sub-word sized rotations in the other direction) as appropriate.
5131void InstructionCodeGeneratorARMVIXL::HandleLongRotate(HRor* ror) {
5132 LocationSummary* locations = ror->GetLocations();
5133 vixl32::Register in_reg_lo = LowRegisterFrom(locations->InAt(0));
5134 vixl32::Register in_reg_hi = HighRegisterFrom(locations->InAt(0));
5135 Location rhs = locations->InAt(1);
5136 vixl32::Register out_reg_lo = LowRegisterFrom(locations->Out());
5137 vixl32::Register out_reg_hi = HighRegisterFrom(locations->Out());
5138
5139 if (rhs.IsConstant()) {
5140 uint64_t rot = CodeGenerator::GetInt64ValueOf(rhs.GetConstant());
5141 // Map all rotations to +ve. equivalents on the interval [0,63].
5142 rot &= kMaxLongShiftDistance;
5143 // For rotates over a word in size, 'pre-rotate' by 32-bits to keep rotate
5144 // logic below to a simple pair of binary orr.
5145 // (e.g. 34 bits == in_reg swap + 2 bits right.)
5146 if (rot >= kArmBitsPerWord) {
5147 rot -= kArmBitsPerWord;
5148 std::swap(in_reg_hi, in_reg_lo);
5149 }
5150 // Rotate, or mov to out for zero or word size rotations.
5151 if (rot != 0u) {
Scott Wakelingb77051e2016-11-21 19:46:00 +00005152 __ Lsr(out_reg_hi, in_reg_hi, Operand::From(rot));
Artem Serov02109dd2016-09-23 17:17:54 +01005153 __ Orr(out_reg_hi, out_reg_hi, Operand(in_reg_lo, ShiftType::LSL, kArmBitsPerWord - rot));
Scott Wakelingb77051e2016-11-21 19:46:00 +00005154 __ Lsr(out_reg_lo, in_reg_lo, Operand::From(rot));
Artem Serov02109dd2016-09-23 17:17:54 +01005155 __ Orr(out_reg_lo, out_reg_lo, Operand(in_reg_hi, ShiftType::LSL, kArmBitsPerWord - rot));
5156 } else {
5157 __ Mov(out_reg_lo, in_reg_lo);
5158 __ Mov(out_reg_hi, in_reg_hi);
5159 }
5160 } else {
5161 vixl32::Register shift_right = RegisterFrom(locations->GetTemp(0));
5162 vixl32::Register shift_left = RegisterFrom(locations->GetTemp(1));
5163 vixl32::Label end;
5164 vixl32::Label shift_by_32_plus_shift_right;
Anton Kirilov6f644202017-02-27 18:29:45 +00005165 vixl32::Label* final_label = codegen_->GetFinalLabel(ror, &end);
Artem Serov02109dd2016-09-23 17:17:54 +01005166
5167 __ And(shift_right, RegisterFrom(rhs), 0x1F);
5168 __ Lsrs(shift_left, RegisterFrom(rhs), 6);
Scott Wakelingbffdc702016-12-07 17:46:03 +00005169 __ Rsb(LeaveFlags, shift_left, shift_right, Operand::From(kArmBitsPerWord));
Artem Serov517d9f62016-12-12 15:51:15 +00005170 __ B(cc, &shift_by_32_plus_shift_right, /* far_target */ false);
Artem Serov02109dd2016-09-23 17:17:54 +01005171
5172 // out_reg_hi = (reg_hi << shift_left) | (reg_lo >> shift_right).
5173 // out_reg_lo = (reg_lo << shift_left) | (reg_hi >> shift_right).
5174 __ Lsl(out_reg_hi, in_reg_hi, shift_left);
5175 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
5176 __ Add(out_reg_hi, out_reg_hi, out_reg_lo);
5177 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
5178 __ Lsr(shift_left, in_reg_hi, shift_right);
5179 __ Add(out_reg_lo, out_reg_lo, shift_left);
Anton Kirilov6f644202017-02-27 18:29:45 +00005180 __ B(final_label);
Artem Serov02109dd2016-09-23 17:17:54 +01005181
5182 __ Bind(&shift_by_32_plus_shift_right); // Shift by 32+shift_right.
5183 // out_reg_hi = (reg_hi >> shift_right) | (reg_lo << shift_left).
5184 // out_reg_lo = (reg_lo >> shift_right) | (reg_hi << shift_left).
5185 __ Lsr(out_reg_hi, in_reg_hi, shift_right);
5186 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
5187 __ Add(out_reg_hi, out_reg_hi, out_reg_lo);
5188 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
5189 __ Lsl(shift_right, in_reg_hi, shift_left);
5190 __ Add(out_reg_lo, out_reg_lo, shift_right);
5191
Anton Kirilov6f644202017-02-27 18:29:45 +00005192 if (end.IsReferenced()) {
5193 __ Bind(&end);
5194 }
Artem Serov02109dd2016-09-23 17:17:54 +01005195 }
5196}
5197
5198void LocationsBuilderARMVIXL::VisitRor(HRor* ror) {
5199 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005200 new (GetGraph()->GetAllocator()) LocationSummary(ror, LocationSummary::kNoCall);
Artem Serov02109dd2016-09-23 17:17:54 +01005201 switch (ror->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005202 case DataType::Type::kInt32: {
Artem Serov02109dd2016-09-23 17:17:54 +01005203 locations->SetInAt(0, Location::RequiresRegister());
5204 locations->SetInAt(1, Location::RegisterOrConstant(ror->InputAt(1)));
5205 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5206 break;
5207 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005208 case DataType::Type::kInt64: {
Artem Serov02109dd2016-09-23 17:17:54 +01005209 locations->SetInAt(0, Location::RequiresRegister());
5210 if (ror->InputAt(1)->IsConstant()) {
5211 locations->SetInAt(1, Location::ConstantLocation(ror->InputAt(1)->AsConstant()));
5212 } else {
5213 locations->SetInAt(1, Location::RequiresRegister());
5214 locations->AddTemp(Location::RequiresRegister());
5215 locations->AddTemp(Location::RequiresRegister());
5216 }
5217 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
5218 break;
5219 }
5220 default:
5221 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
5222 }
5223}
5224
5225void InstructionCodeGeneratorARMVIXL::VisitRor(HRor* ror) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005226 DataType::Type type = ror->GetResultType();
Artem Serov02109dd2016-09-23 17:17:54 +01005227 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005228 case DataType::Type::kInt32: {
Artem Serov02109dd2016-09-23 17:17:54 +01005229 HandleIntegerRotate(ror);
5230 break;
5231 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005232 case DataType::Type::kInt64: {
Artem Serov02109dd2016-09-23 17:17:54 +01005233 HandleLongRotate(ror);
5234 break;
5235 }
5236 default:
5237 LOG(FATAL) << "Unexpected operation type " << type;
5238 UNREACHABLE();
5239 }
5240}
5241
Artem Serov02d37832016-10-25 15:25:33 +01005242void LocationsBuilderARMVIXL::HandleShift(HBinaryOperation* op) {
5243 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
5244
5245 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005246 new (GetGraph()->GetAllocator()) LocationSummary(op, LocationSummary::kNoCall);
Artem Serov02d37832016-10-25 15:25:33 +01005247
5248 switch (op->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005249 case DataType::Type::kInt32: {
Artem Serov02d37832016-10-25 15:25:33 +01005250 locations->SetInAt(0, Location::RequiresRegister());
5251 if (op->InputAt(1)->IsConstant()) {
5252 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
5253 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5254 } else {
5255 locations->SetInAt(1, Location::RequiresRegister());
5256 // Make the output overlap, as it will be used to hold the masked
5257 // second input.
5258 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
5259 }
5260 break;
5261 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005262 case DataType::Type::kInt64: {
Artem Serov02d37832016-10-25 15:25:33 +01005263 locations->SetInAt(0, Location::RequiresRegister());
5264 if (op->InputAt(1)->IsConstant()) {
5265 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
5266 // For simplicity, use kOutputOverlap even though we only require that low registers
5267 // don't clash with high registers which the register allocator currently guarantees.
5268 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
5269 } else {
5270 locations->SetInAt(1, Location::RequiresRegister());
5271 locations->AddTemp(Location::RequiresRegister());
5272 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
5273 }
5274 break;
5275 }
5276 default:
5277 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
5278 }
5279}
5280
5281void InstructionCodeGeneratorARMVIXL::HandleShift(HBinaryOperation* op) {
5282 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
5283
5284 LocationSummary* locations = op->GetLocations();
5285 Location out = locations->Out();
5286 Location first = locations->InAt(0);
5287 Location second = locations->InAt(1);
5288
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005289 DataType::Type type = op->GetResultType();
Artem Serov02d37832016-10-25 15:25:33 +01005290 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005291 case DataType::Type::kInt32: {
Artem Serov02d37832016-10-25 15:25:33 +01005292 vixl32::Register out_reg = OutputRegister(op);
5293 vixl32::Register first_reg = InputRegisterAt(op, 0);
5294 if (second.IsRegister()) {
5295 vixl32::Register second_reg = RegisterFrom(second);
5296 // ARM doesn't mask the shift count so we need to do it ourselves.
5297 __ And(out_reg, second_reg, kMaxIntShiftDistance);
5298 if (op->IsShl()) {
5299 __ Lsl(out_reg, first_reg, out_reg);
5300 } else if (op->IsShr()) {
5301 __ Asr(out_reg, first_reg, out_reg);
5302 } else {
5303 __ Lsr(out_reg, first_reg, out_reg);
5304 }
5305 } else {
Anton Kirilov644032c2016-12-06 17:51:43 +00005306 int32_t cst = Int32ConstantFrom(second);
Artem Serov02d37832016-10-25 15:25:33 +01005307 uint32_t shift_value = cst & kMaxIntShiftDistance;
5308 if (shift_value == 0) { // ARM does not support shifting with 0 immediate.
5309 __ Mov(out_reg, first_reg);
5310 } else if (op->IsShl()) {
5311 __ Lsl(out_reg, first_reg, shift_value);
5312 } else if (op->IsShr()) {
5313 __ Asr(out_reg, first_reg, shift_value);
5314 } else {
5315 __ Lsr(out_reg, first_reg, shift_value);
5316 }
5317 }
5318 break;
5319 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005320 case DataType::Type::kInt64: {
Artem Serov02d37832016-10-25 15:25:33 +01005321 vixl32::Register o_h = HighRegisterFrom(out);
5322 vixl32::Register o_l = LowRegisterFrom(out);
5323
5324 vixl32::Register high = HighRegisterFrom(first);
5325 vixl32::Register low = LowRegisterFrom(first);
5326
5327 if (second.IsRegister()) {
5328 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
5329
5330 vixl32::Register second_reg = RegisterFrom(second);
5331
5332 if (op->IsShl()) {
5333 __ And(o_l, second_reg, kMaxLongShiftDistance);
5334 // Shift the high part
5335 __ Lsl(o_h, high, o_l);
5336 // Shift the low part and `or` what overflew on the high part
Scott Wakelingb77051e2016-11-21 19:46:00 +00005337 __ Rsb(temp, o_l, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01005338 __ Lsr(temp, low, temp);
5339 __ Orr(o_h, o_h, temp);
5340 // If the shift is > 32 bits, override the high part
Scott Wakelingb77051e2016-11-21 19:46:00 +00005341 __ Subs(temp, o_l, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01005342 {
Artem Serov0fb37192016-12-06 18:13:40 +00005343 ExactAssemblyScope guard(GetVIXLAssembler(),
5344 2 * vixl32::kMaxInstructionSizeInBytes,
5345 CodeBufferCheckScope::kMaximumSize);
Artem Serov02d37832016-10-25 15:25:33 +01005346 __ it(pl);
5347 __ lsl(pl, o_h, low, temp);
5348 }
5349 // Shift the low part
5350 __ Lsl(o_l, low, o_l);
5351 } else if (op->IsShr()) {
5352 __ And(o_h, second_reg, kMaxLongShiftDistance);
5353 // Shift the low part
5354 __ Lsr(o_l, low, o_h);
5355 // Shift the high part and `or` what underflew on the low part
Scott Wakelingb77051e2016-11-21 19:46:00 +00005356 __ Rsb(temp, o_h, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01005357 __ Lsl(temp, high, temp);
5358 __ Orr(o_l, o_l, temp);
5359 // If the shift is > 32 bits, override the low part
Scott Wakelingb77051e2016-11-21 19:46:00 +00005360 __ Subs(temp, o_h, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01005361 {
Artem Serov0fb37192016-12-06 18:13:40 +00005362 ExactAssemblyScope guard(GetVIXLAssembler(),
5363 2 * vixl32::kMaxInstructionSizeInBytes,
5364 CodeBufferCheckScope::kMaximumSize);
Artem Serov02d37832016-10-25 15:25:33 +01005365 __ it(pl);
5366 __ asr(pl, o_l, high, temp);
5367 }
5368 // Shift the high part
5369 __ Asr(o_h, high, o_h);
5370 } else {
5371 __ And(o_h, second_reg, kMaxLongShiftDistance);
5372 // same as Shr except we use `Lsr`s and not `Asr`s
5373 __ Lsr(o_l, low, o_h);
Scott Wakelingb77051e2016-11-21 19:46:00 +00005374 __ Rsb(temp, o_h, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01005375 __ Lsl(temp, high, temp);
5376 __ Orr(o_l, o_l, temp);
Scott Wakelingb77051e2016-11-21 19:46:00 +00005377 __ Subs(temp, o_h, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01005378 {
Artem Serov0fb37192016-12-06 18:13:40 +00005379 ExactAssemblyScope guard(GetVIXLAssembler(),
5380 2 * vixl32::kMaxInstructionSizeInBytes,
5381 CodeBufferCheckScope::kMaximumSize);
Artem Serov02d37832016-10-25 15:25:33 +01005382 __ it(pl);
5383 __ lsr(pl, o_l, high, temp);
5384 }
5385 __ Lsr(o_h, high, o_h);
5386 }
5387 } else {
5388 // Register allocator doesn't create partial overlap.
5389 DCHECK(!o_l.Is(high));
5390 DCHECK(!o_h.Is(low));
Anton Kirilov644032c2016-12-06 17:51:43 +00005391 int32_t cst = Int32ConstantFrom(second);
Artem Serov02d37832016-10-25 15:25:33 +01005392 uint32_t shift_value = cst & kMaxLongShiftDistance;
5393 if (shift_value > 32) {
5394 if (op->IsShl()) {
5395 __ Lsl(o_h, low, shift_value - 32);
5396 __ Mov(o_l, 0);
5397 } else if (op->IsShr()) {
5398 __ Asr(o_l, high, shift_value - 32);
5399 __ Asr(o_h, high, 31);
5400 } else {
5401 __ Lsr(o_l, high, shift_value - 32);
5402 __ Mov(o_h, 0);
5403 }
5404 } else if (shift_value == 32) {
5405 if (op->IsShl()) {
5406 __ Mov(o_h, low);
5407 __ Mov(o_l, 0);
5408 } else if (op->IsShr()) {
5409 __ Mov(o_l, high);
5410 __ Asr(o_h, high, 31);
5411 } else {
5412 __ Mov(o_l, high);
5413 __ Mov(o_h, 0);
5414 }
5415 } else if (shift_value == 1) {
5416 if (op->IsShl()) {
5417 __ Lsls(o_l, low, 1);
5418 __ Adc(o_h, high, high);
5419 } else if (op->IsShr()) {
5420 __ Asrs(o_h, high, 1);
5421 __ Rrx(o_l, low);
5422 } else {
5423 __ Lsrs(o_h, high, 1);
5424 __ Rrx(o_l, low);
5425 }
5426 } else {
5427 DCHECK(2 <= shift_value && shift_value < 32) << shift_value;
5428 if (op->IsShl()) {
5429 __ Lsl(o_h, high, shift_value);
5430 __ Orr(o_h, o_h, Operand(low, ShiftType::LSR, 32 - shift_value));
5431 __ Lsl(o_l, low, shift_value);
5432 } else if (op->IsShr()) {
5433 __ Lsr(o_l, low, shift_value);
5434 __ Orr(o_l, o_l, Operand(high, ShiftType::LSL, 32 - shift_value));
5435 __ Asr(o_h, high, shift_value);
5436 } else {
5437 __ Lsr(o_l, low, shift_value);
5438 __ Orr(o_l, o_l, Operand(high, ShiftType::LSL, 32 - shift_value));
5439 __ Lsr(o_h, high, shift_value);
5440 }
5441 }
5442 }
5443 break;
5444 }
5445 default:
5446 LOG(FATAL) << "Unexpected operation type " << type;
5447 UNREACHABLE();
5448 }
5449}
5450
5451void LocationsBuilderARMVIXL::VisitShl(HShl* shl) {
5452 HandleShift(shl);
5453}
5454
5455void InstructionCodeGeneratorARMVIXL::VisitShl(HShl* shl) {
5456 HandleShift(shl);
5457}
5458
5459void LocationsBuilderARMVIXL::VisitShr(HShr* shr) {
5460 HandleShift(shr);
5461}
5462
5463void InstructionCodeGeneratorARMVIXL::VisitShr(HShr* shr) {
5464 HandleShift(shr);
5465}
5466
5467void LocationsBuilderARMVIXL::VisitUShr(HUShr* ushr) {
5468 HandleShift(ushr);
5469}
5470
5471void InstructionCodeGeneratorARMVIXL::VisitUShr(HUShr* ushr) {
5472 HandleShift(ushr);
5473}
5474
5475void LocationsBuilderARMVIXL::VisitNewInstance(HNewInstance* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005476 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5477 instruction, LocationSummary::kCallOnMainOnly);
Artem Serov02d37832016-10-25 15:25:33 +01005478 if (instruction->IsStringAlloc()) {
5479 locations->AddTemp(LocationFrom(kMethodRegister));
5480 } else {
5481 InvokeRuntimeCallingConventionARMVIXL calling_convention;
5482 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
Artem Serov02d37832016-10-25 15:25:33 +01005483 }
5484 locations->SetOut(LocationFrom(r0));
5485}
5486
5487void InstructionCodeGeneratorARMVIXL::VisitNewInstance(HNewInstance* instruction) {
5488 // Note: if heap poisoning is enabled, the entry point takes cares
5489 // of poisoning the reference.
5490 if (instruction->IsStringAlloc()) {
5491 // String is allocated through StringFactory. Call NewEmptyString entry point.
5492 vixl32::Register temp = RegisterFrom(instruction->GetLocations()->GetTemp(0));
5493 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize);
5494 GetAssembler()->LoadFromOffset(kLoadWord, temp, tr, QUICK_ENTRY_POINT(pNewEmptyString));
5495 GetAssembler()->LoadFromOffset(kLoadWord, lr, temp, code_offset.Int32Value());
Alexandre Rames374ddf32016-11-04 10:40:49 +00005496 // blx in T32 has only 16bit encoding that's why a stricter check for the scope is used.
Artem Serov0fb37192016-12-06 18:13:40 +00005497 ExactAssemblyScope aas(GetVIXLAssembler(),
5498 vixl32::k16BitT32InstructionSizeInBytes,
5499 CodeBufferCheckScope::kExactSize);
Artem Serov02d37832016-10-25 15:25:33 +01005500 __ blx(lr);
5501 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
5502 } else {
5503 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00005504 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
Artem Serov02d37832016-10-25 15:25:33 +01005505 }
Roland Levillain5daa4952017-07-03 17:23:56 +01005506 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 10);
Artem Serov02d37832016-10-25 15:25:33 +01005507}
5508
5509void LocationsBuilderARMVIXL::VisitNewArray(HNewArray* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005510 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5511 instruction, LocationSummary::kCallOnMainOnly);
Artem Serov02d37832016-10-25 15:25:33 +01005512 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Artem Serov02d37832016-10-25 15:25:33 +01005513 locations->SetOut(LocationFrom(r0));
Nicolas Geoffray8c7c4f12017-01-26 10:13:11 +00005514 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5515 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
Artem Serov02d37832016-10-25 15:25:33 +01005516}
5517
5518void InstructionCodeGeneratorARMVIXL::VisitNewArray(HNewArray* instruction) {
Artem Serov02d37832016-10-25 15:25:33 +01005519 // Note: if heap poisoning is enabled, the entry point takes cares
5520 // of poisoning the reference.
Artem Serov7b3672e2017-02-03 17:30:34 +00005521 QuickEntrypointEnum entrypoint =
5522 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
5523 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005524 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Artem Serov7b3672e2017-02-03 17:30:34 +00005525 DCHECK(!codegen_->IsLeafMethod());
Roland Levillain5daa4952017-07-03 17:23:56 +01005526 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 11);
Artem Serov02d37832016-10-25 15:25:33 +01005527}
5528
5529void LocationsBuilderARMVIXL::VisitParameterValue(HParameterValue* instruction) {
5530 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005531 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Artem Serov02d37832016-10-25 15:25:33 +01005532 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
5533 if (location.IsStackSlot()) {
5534 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5535 } else if (location.IsDoubleStackSlot()) {
5536 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5537 }
5538 locations->SetOut(location);
5539}
5540
5541void InstructionCodeGeneratorARMVIXL::VisitParameterValue(
5542 HParameterValue* instruction ATTRIBUTE_UNUSED) {
5543 // Nothing to do, the parameter is already at its location.
5544}
5545
5546void LocationsBuilderARMVIXL::VisitCurrentMethod(HCurrentMethod* instruction) {
5547 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005548 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Artem Serov02d37832016-10-25 15:25:33 +01005549 locations->SetOut(LocationFrom(kMethodRegister));
5550}
5551
5552void InstructionCodeGeneratorARMVIXL::VisitCurrentMethod(
5553 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
5554 // Nothing to do, the method is already at its location.
5555}
5556
5557void LocationsBuilderARMVIXL::VisitNot(HNot* not_) {
5558 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005559 new (GetGraph()->GetAllocator()) LocationSummary(not_, LocationSummary::kNoCall);
Artem Serov02d37832016-10-25 15:25:33 +01005560 locations->SetInAt(0, Location::RequiresRegister());
5561 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5562}
5563
5564void InstructionCodeGeneratorARMVIXL::VisitNot(HNot* not_) {
5565 LocationSummary* locations = not_->GetLocations();
5566 Location out = locations->Out();
5567 Location in = locations->InAt(0);
5568 switch (not_->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005569 case DataType::Type::kInt32:
Artem Serov02d37832016-10-25 15:25:33 +01005570 __ Mvn(OutputRegister(not_), InputRegisterAt(not_, 0));
5571 break;
5572
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005573 case DataType::Type::kInt64:
Artem Serov02d37832016-10-25 15:25:33 +01005574 __ Mvn(LowRegisterFrom(out), LowRegisterFrom(in));
5575 __ Mvn(HighRegisterFrom(out), HighRegisterFrom(in));
5576 break;
5577
5578 default:
5579 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
5580 }
5581}
5582
Scott Wakelingc34dba72016-10-03 10:14:44 +01005583void LocationsBuilderARMVIXL::VisitBooleanNot(HBooleanNot* bool_not) {
5584 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005585 new (GetGraph()->GetAllocator()) LocationSummary(bool_not, LocationSummary::kNoCall);
Scott Wakelingc34dba72016-10-03 10:14:44 +01005586 locations->SetInAt(0, Location::RequiresRegister());
5587 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5588}
5589
5590void InstructionCodeGeneratorARMVIXL::VisitBooleanNot(HBooleanNot* bool_not) {
5591 __ Eor(OutputRegister(bool_not), InputRegister(bool_not), 1);
5592}
5593
Artem Serov02d37832016-10-25 15:25:33 +01005594void LocationsBuilderARMVIXL::VisitCompare(HCompare* compare) {
5595 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005596 new (GetGraph()->GetAllocator()) LocationSummary(compare, LocationSummary::kNoCall);
Artem Serov02d37832016-10-25 15:25:33 +01005597 switch (compare->InputAt(0)->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005598 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005599 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005600 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005601 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005602 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005603 case DataType::Type::kInt32:
5604 case DataType::Type::kInt64: {
Artem Serov02d37832016-10-25 15:25:33 +01005605 locations->SetInAt(0, Location::RequiresRegister());
5606 locations->SetInAt(1, Location::RequiresRegister());
5607 // Output overlaps because it is written before doing the low comparison.
5608 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
5609 break;
5610 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005611 case DataType::Type::kFloat32:
5612 case DataType::Type::kFloat64: {
Artem Serov02d37832016-10-25 15:25:33 +01005613 locations->SetInAt(0, Location::RequiresFpuRegister());
5614 locations->SetInAt(1, ArithmeticZeroOrFpuRegister(compare->InputAt(1)));
5615 locations->SetOut(Location::RequiresRegister());
5616 break;
5617 }
5618 default:
5619 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
5620 }
5621}
5622
5623void InstructionCodeGeneratorARMVIXL::VisitCompare(HCompare* compare) {
5624 LocationSummary* locations = compare->GetLocations();
5625 vixl32::Register out = OutputRegister(compare);
5626 Location left = locations->InAt(0);
5627 Location right = locations->InAt(1);
5628
5629 vixl32::Label less, greater, done;
Anton Kirilov6f644202017-02-27 18:29:45 +00005630 vixl32::Label* final_label = codegen_->GetFinalLabel(compare, &done);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005631 DataType::Type type = compare->InputAt(0)->GetType();
Vladimir Marko33bff252017-11-01 14:35:42 +00005632 vixl32::Condition less_cond = vixl32::Condition::None();
Artem Serov02d37832016-10-25 15:25:33 +01005633 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005634 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005635 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005636 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005637 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005638 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005639 case DataType::Type::kInt32: {
Artem Serov02d37832016-10-25 15:25:33 +01005640 // Emit move to `out` before the `Cmp`, as `Mov` might affect the status flags.
5641 __ Mov(out, 0);
5642 __ Cmp(RegisterFrom(left), RegisterFrom(right)); // Signed compare.
5643 less_cond = lt;
5644 break;
5645 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005646 case DataType::Type::kInt64: {
Artem Serov02d37832016-10-25 15:25:33 +01005647 __ Cmp(HighRegisterFrom(left), HighRegisterFrom(right)); // Signed compare.
Artem Serov517d9f62016-12-12 15:51:15 +00005648 __ B(lt, &less, /* far_target */ false);
5649 __ B(gt, &greater, /* far_target */ false);
Artem Serov02d37832016-10-25 15:25:33 +01005650 // Emit move to `out` before the last `Cmp`, as `Mov` might affect the status flags.
5651 __ Mov(out, 0);
5652 __ Cmp(LowRegisterFrom(left), LowRegisterFrom(right)); // Unsigned compare.
5653 less_cond = lo;
5654 break;
5655 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005656 case DataType::Type::kFloat32:
5657 case DataType::Type::kFloat64: {
Artem Serov02d37832016-10-25 15:25:33 +01005658 __ Mov(out, 0);
Donghui Bai426b49c2016-11-08 14:55:38 +08005659 GenerateVcmp(compare, codegen_);
Artem Serov02d37832016-10-25 15:25:33 +01005660 // To branch on the FP compare result we transfer FPSCR to APSR (encoded as PC in VMRS).
5661 __ Vmrs(RegisterOrAPSR_nzcv(kPcCode), FPSCR);
5662 less_cond = ARMFPCondition(kCondLT, compare->IsGtBias());
5663 break;
5664 }
5665 default:
5666 LOG(FATAL) << "Unexpected compare type " << type;
5667 UNREACHABLE();
5668 }
5669
Anton Kirilov6f644202017-02-27 18:29:45 +00005670 __ B(eq, final_label, /* far_target */ false);
Artem Serov517d9f62016-12-12 15:51:15 +00005671 __ B(less_cond, &less, /* far_target */ false);
Artem Serov02d37832016-10-25 15:25:33 +01005672
5673 __ Bind(&greater);
5674 __ Mov(out, 1);
Anton Kirilov6f644202017-02-27 18:29:45 +00005675 __ B(final_label);
Artem Serov02d37832016-10-25 15:25:33 +01005676
5677 __ Bind(&less);
5678 __ Mov(out, -1);
5679
Anton Kirilov6f644202017-02-27 18:29:45 +00005680 if (done.IsReferenced()) {
5681 __ Bind(&done);
5682 }
Artem Serov02d37832016-10-25 15:25:33 +01005683}
5684
5685void LocationsBuilderARMVIXL::VisitPhi(HPhi* instruction) {
5686 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005687 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Artem Serov02d37832016-10-25 15:25:33 +01005688 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
5689 locations->SetInAt(i, Location::Any());
5690 }
5691 locations->SetOut(Location::Any());
5692}
5693
5694void InstructionCodeGeneratorARMVIXL::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
5695 LOG(FATAL) << "Unreachable";
5696}
5697
5698void CodeGeneratorARMVIXL::GenerateMemoryBarrier(MemBarrierKind kind) {
5699 // TODO (ported from quick): revisit ARM barrier kinds.
5700 DmbOptions flavor = DmbOptions::ISH; // Quiet C++ warnings.
5701 switch (kind) {
5702 case MemBarrierKind::kAnyStore:
5703 case MemBarrierKind::kLoadAny:
5704 case MemBarrierKind::kAnyAny: {
5705 flavor = DmbOptions::ISH;
5706 break;
5707 }
5708 case MemBarrierKind::kStoreStore: {
5709 flavor = DmbOptions::ISHST;
5710 break;
5711 }
5712 default:
5713 LOG(FATAL) << "Unexpected memory barrier " << kind;
5714 }
5715 __ Dmb(flavor);
5716}
5717
5718void InstructionCodeGeneratorARMVIXL::GenerateWideAtomicLoad(vixl32::Register addr,
5719 uint32_t offset,
5720 vixl32::Register out_lo,
5721 vixl32::Register out_hi) {
5722 UseScratchRegisterScope temps(GetVIXLAssembler());
5723 if (offset != 0) {
5724 vixl32::Register temp = temps.Acquire();
5725 __ Add(temp, addr, offset);
5726 addr = temp;
5727 }
Scott Wakelingb77051e2016-11-21 19:46:00 +00005728 __ Ldrexd(out_lo, out_hi, MemOperand(addr));
Artem Serov02d37832016-10-25 15:25:33 +01005729}
5730
5731void InstructionCodeGeneratorARMVIXL::GenerateWideAtomicStore(vixl32::Register addr,
5732 uint32_t offset,
5733 vixl32::Register value_lo,
5734 vixl32::Register value_hi,
5735 vixl32::Register temp1,
5736 vixl32::Register temp2,
5737 HInstruction* instruction) {
5738 UseScratchRegisterScope temps(GetVIXLAssembler());
5739 vixl32::Label fail;
5740 if (offset != 0) {
5741 vixl32::Register temp = temps.Acquire();
5742 __ Add(temp, addr, offset);
5743 addr = temp;
5744 }
5745 __ Bind(&fail);
Alexandre Rames374ddf32016-11-04 10:40:49 +00005746 {
5747 // Ensure the pc position is recorded immediately after the `ldrexd` instruction.
Artem Serov0fb37192016-12-06 18:13:40 +00005748 ExactAssemblyScope aas(GetVIXLAssembler(),
5749 vixl32::kMaxInstructionSizeInBytes,
5750 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00005751 // We need a load followed by store. (The address used in a STREX instruction must
5752 // be the same as the address in the most recently executed LDREX instruction.)
5753 __ ldrexd(temp1, temp2, MemOperand(addr));
5754 codegen_->MaybeRecordImplicitNullCheck(instruction);
5755 }
Scott Wakelingb77051e2016-11-21 19:46:00 +00005756 __ Strexd(temp1, value_lo, value_hi, MemOperand(addr));
xueliang.zhongf51bc622016-11-04 09:23:32 +00005757 __ CompareAndBranchIfNonZero(temp1, &fail);
Artem Serov02d37832016-10-25 15:25:33 +01005758}
Artem Serov02109dd2016-09-23 17:17:54 +01005759
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005760void LocationsBuilderARMVIXL::HandleFieldSet(
5761 HInstruction* instruction, const FieldInfo& field_info) {
5762 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
5763
5764 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005765 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005766 locations->SetInAt(0, Location::RequiresRegister());
5767
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005768 DataType::Type field_type = field_info.GetFieldType();
5769 if (DataType::IsFloatingPointType(field_type)) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005770 locations->SetInAt(1, Location::RequiresFpuRegister());
5771 } else {
5772 locations->SetInAt(1, Location::RequiresRegister());
5773 }
5774
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005775 bool is_wide = field_type == DataType::Type::kInt64 || field_type == DataType::Type::kFloat64;
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005776 bool generate_volatile = field_info.IsVolatile()
5777 && is_wide
5778 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
5779 bool needs_write_barrier =
5780 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
5781 // Temporary registers for the write barrier.
5782 // TODO: consider renaming StoreNeedsWriteBarrier to StoreNeedsGCMark.
5783 if (needs_write_barrier) {
5784 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
5785 locations->AddTemp(Location::RequiresRegister());
5786 } else if (generate_volatile) {
5787 // ARM encoding have some additional constraints for ldrexd/strexd:
5788 // - registers need to be consecutive
5789 // - the first register should be even but not R14.
5790 // We don't test for ARM yet, and the assertion makes sure that we
5791 // revisit this if we ever enable ARM encoding.
5792 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
5793
5794 locations->AddTemp(Location::RequiresRegister());
5795 locations->AddTemp(Location::RequiresRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005796 if (field_type == DataType::Type::kFloat64) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005797 // For doubles we need two more registers to copy the value.
5798 locations->AddTemp(LocationFrom(r2));
5799 locations->AddTemp(LocationFrom(r3));
5800 }
5801 }
5802}
5803
5804void InstructionCodeGeneratorARMVIXL::HandleFieldSet(HInstruction* instruction,
5805 const FieldInfo& field_info,
5806 bool value_can_be_null) {
5807 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
5808
5809 LocationSummary* locations = instruction->GetLocations();
5810 vixl32::Register base = InputRegisterAt(instruction, 0);
5811 Location value = locations->InAt(1);
5812
5813 bool is_volatile = field_info.IsVolatile();
5814 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005815 DataType::Type field_type = field_info.GetFieldType();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005816 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
5817 bool needs_write_barrier =
5818 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
5819
5820 if (is_volatile) {
5821 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
5822 }
5823
5824 switch (field_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005825 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005826 case DataType::Type::kUint8:
5827 case DataType::Type::kInt8:
5828 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005829 case DataType::Type::kInt16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005830 case DataType::Type::kInt32: {
5831 StoreOperandType operand_type = GetStoreOperandType(field_type);
5832 GetAssembler()->StoreToOffset(operand_type, RegisterFrom(value), base, offset);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005833 break;
5834 }
5835
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005836 case DataType::Type::kReference: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005837 if (kPoisonHeapReferences && needs_write_barrier) {
5838 // Note that in the case where `value` is a null reference,
5839 // we do not enter this block, as a null reference does not
5840 // need poisoning.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005841 DCHECK_EQ(field_type, DataType::Type::kReference);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005842 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
5843 __ Mov(temp, RegisterFrom(value));
5844 GetAssembler()->PoisonHeapReference(temp);
5845 GetAssembler()->StoreToOffset(kStoreWord, temp, base, offset);
5846 } else {
5847 GetAssembler()->StoreToOffset(kStoreWord, RegisterFrom(value), base, offset);
5848 }
5849 break;
5850 }
5851
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005852 case DataType::Type::kInt64: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005853 if (is_volatile && !atomic_ldrd_strd) {
5854 GenerateWideAtomicStore(base,
5855 offset,
5856 LowRegisterFrom(value),
5857 HighRegisterFrom(value),
5858 RegisterFrom(locations->GetTemp(0)),
5859 RegisterFrom(locations->GetTemp(1)),
5860 instruction);
5861 } else {
5862 GetAssembler()->StoreToOffset(kStoreWordPair, LowRegisterFrom(value), base, offset);
5863 codegen_->MaybeRecordImplicitNullCheck(instruction);
5864 }
5865 break;
5866 }
5867
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005868 case DataType::Type::kFloat32: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005869 GetAssembler()->StoreSToOffset(SRegisterFrom(value), base, offset);
5870 break;
5871 }
5872
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005873 case DataType::Type::kFloat64: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01005874 vixl32::DRegister value_reg = DRegisterFrom(value);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005875 if (is_volatile && !atomic_ldrd_strd) {
5876 vixl32::Register value_reg_lo = RegisterFrom(locations->GetTemp(0));
5877 vixl32::Register value_reg_hi = RegisterFrom(locations->GetTemp(1));
5878
5879 __ Vmov(value_reg_lo, value_reg_hi, value_reg);
5880
5881 GenerateWideAtomicStore(base,
5882 offset,
5883 value_reg_lo,
5884 value_reg_hi,
5885 RegisterFrom(locations->GetTemp(2)),
5886 RegisterFrom(locations->GetTemp(3)),
5887 instruction);
5888 } else {
5889 GetAssembler()->StoreDToOffset(value_reg, base, offset);
5890 codegen_->MaybeRecordImplicitNullCheck(instruction);
5891 }
5892 break;
5893 }
5894
Aart Bik66c158e2018-01-31 12:55:04 -08005895 case DataType::Type::kUint32:
5896 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005897 case DataType::Type::kVoid:
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005898 LOG(FATAL) << "Unreachable type " << field_type;
5899 UNREACHABLE();
5900 }
5901
5902 // Longs and doubles are handled in the switch.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005903 if (field_type != DataType::Type::kInt64 && field_type != DataType::Type::kFloat64) {
Alexandre Rames374ddf32016-11-04 10:40:49 +00005904 // TODO(VIXL): Here and for other calls to `MaybeRecordImplicitNullCheck` in this method, we
5905 // should use a scope and the assembler to emit the store instruction to guarantee that we
5906 // record the pc at the correct position. But the `Assembler` does not automatically handle
5907 // unencodable offsets. Practically, everything is fine because the helper and VIXL, at the time
5908 // of writing, do generate the store instruction last.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005909 codegen_->MaybeRecordImplicitNullCheck(instruction);
5910 }
5911
5912 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
5913 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
5914 vixl32::Register card = RegisterFrom(locations->GetTemp(1));
5915 codegen_->MarkGCCard(temp, card, base, RegisterFrom(value), value_can_be_null);
5916 }
5917
5918 if (is_volatile) {
5919 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
5920 }
5921}
5922
Artem Serov02d37832016-10-25 15:25:33 +01005923void LocationsBuilderARMVIXL::HandleFieldGet(HInstruction* instruction,
5924 const FieldInfo& field_info) {
5925 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
5926
5927 bool object_field_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005928 kEmitCompilerReadBarrier && (field_info.GetFieldType() == DataType::Type::kReference);
Artem Serov02d37832016-10-25 15:25:33 +01005929 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005930 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
5931 object_field_get_with_read_barrier
5932 ? LocationSummary::kCallOnSlowPath
5933 : LocationSummary::kNoCall);
Artem Serov02d37832016-10-25 15:25:33 +01005934 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
5935 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
5936 }
5937 locations->SetInAt(0, Location::RequiresRegister());
5938
5939 bool volatile_for_double = field_info.IsVolatile()
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005940 && (field_info.GetFieldType() == DataType::Type::kFloat64)
Artem Serov02d37832016-10-25 15:25:33 +01005941 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
5942 // The output overlaps in case of volatile long: we don't want the
5943 // code generated by GenerateWideAtomicLoad to overwrite the
5944 // object's location. Likewise, in the case of an object field get
5945 // with read barriers enabled, we do not want the load to overwrite
5946 // the object's location, as we need it to emit the read barrier.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005947 bool overlap =
5948 (field_info.IsVolatile() && (field_info.GetFieldType() == DataType::Type::kInt64)) ||
Artem Serov02d37832016-10-25 15:25:33 +01005949 object_field_get_with_read_barrier;
5950
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005951 if (DataType::IsFloatingPointType(instruction->GetType())) {
Artem Serov02d37832016-10-25 15:25:33 +01005952 locations->SetOut(Location::RequiresFpuRegister());
5953 } else {
5954 locations->SetOut(Location::RequiresRegister(),
5955 (overlap ? Location::kOutputOverlap : Location::kNoOutputOverlap));
5956 }
5957 if (volatile_for_double) {
5958 // ARM encoding have some additional constraints for ldrexd/strexd:
5959 // - registers need to be consecutive
5960 // - the first register should be even but not R14.
5961 // We don't test for ARM yet, and the assertion makes sure that we
5962 // revisit this if we ever enable ARM encoding.
5963 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
5964 locations->AddTemp(Location::RequiresRegister());
5965 locations->AddTemp(Location::RequiresRegister());
5966 } else if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
5967 // We need a temporary register for the read barrier marking slow
Artem Serovc5fcb442016-12-02 19:19:58 +00005968 // path in CodeGeneratorARMVIXL::GenerateFieldLoadWithBakerReadBarrier.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01005969 if (kBakerReadBarrierLinkTimeThunksEnableForFields &&
5970 !Runtime::Current()->UseJitCompilation()) {
5971 // If link-time thunks for the Baker read barrier are enabled, for AOT
5972 // loads we need a temporary only if the offset is too big.
5973 if (field_info.GetFieldOffset().Uint32Value() >= kReferenceLoadMinFarOffset) {
5974 locations->AddTemp(Location::RequiresRegister());
5975 }
5976 // And we always need the reserved entrypoint register.
5977 locations->AddTemp(Location::RegisterLocation(kBakerCcEntrypointRegister.GetCode()));
5978 } else {
5979 locations->AddTemp(Location::RequiresRegister());
5980 }
Artem Serov02d37832016-10-25 15:25:33 +01005981 }
5982}
5983
5984Location LocationsBuilderARMVIXL::ArithmeticZeroOrFpuRegister(HInstruction* input) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005985 DCHECK(DataType::IsFloatingPointType(input->GetType())) << input->GetType();
Artem Serov02d37832016-10-25 15:25:33 +01005986 if ((input->IsFloatConstant() && (input->AsFloatConstant()->IsArithmeticZero())) ||
5987 (input->IsDoubleConstant() && (input->AsDoubleConstant()->IsArithmeticZero()))) {
5988 return Location::ConstantLocation(input->AsConstant());
5989 } else {
5990 return Location::RequiresFpuRegister();
5991 }
5992}
5993
Artem Serov02109dd2016-09-23 17:17:54 +01005994Location LocationsBuilderARMVIXL::ArmEncodableConstantOrRegister(HInstruction* constant,
5995 Opcode opcode) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005996 DCHECK(!DataType::IsFloatingPointType(constant->GetType()));
Artem Serov02109dd2016-09-23 17:17:54 +01005997 if (constant->IsConstant() &&
5998 CanEncodeConstantAsImmediate(constant->AsConstant(), opcode)) {
5999 return Location::ConstantLocation(constant->AsConstant());
6000 }
6001 return Location::RequiresRegister();
6002}
6003
Vladimir Markof0a6a1d2018-01-08 14:23:56 +00006004static bool CanEncode32BitConstantAsImmediate(
6005 CodeGeneratorARMVIXL* codegen,
6006 uint32_t value,
6007 Opcode opcode,
6008 vixl32::FlagsUpdate flags_update = vixl32::FlagsUpdate::DontCare) {
6009 ArmVIXLAssembler* assembler = codegen->GetAssembler();
6010 if (assembler->ShifterOperandCanHold(opcode, value, flags_update)) {
Artem Serov02109dd2016-09-23 17:17:54 +01006011 return true;
6012 }
6013 Opcode neg_opcode = kNoOperand;
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00006014 uint32_t neg_value = 0;
Artem Serov02109dd2016-09-23 17:17:54 +01006015 switch (opcode) {
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00006016 case AND: neg_opcode = BIC; neg_value = ~value; break;
6017 case ORR: neg_opcode = ORN; neg_value = ~value; break;
6018 case ADD: neg_opcode = SUB; neg_value = -value; break;
6019 case ADC: neg_opcode = SBC; neg_value = ~value; break;
6020 case SUB: neg_opcode = ADD; neg_value = -value; break;
6021 case SBC: neg_opcode = ADC; neg_value = ~value; break;
6022 case MOV: neg_opcode = MVN; neg_value = ~value; break;
Artem Serov02109dd2016-09-23 17:17:54 +01006023 default:
6024 return false;
6025 }
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00006026
Vladimir Markof0a6a1d2018-01-08 14:23:56 +00006027 if (assembler->ShifterOperandCanHold(neg_opcode, neg_value, flags_update)) {
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00006028 return true;
6029 }
6030
6031 return opcode == AND && IsPowerOfTwo(value + 1);
Artem Serov02109dd2016-09-23 17:17:54 +01006032}
6033
Vladimir Markof0a6a1d2018-01-08 14:23:56 +00006034bool LocationsBuilderARMVIXL::CanEncodeConstantAsImmediate(HConstant* input_cst, Opcode opcode) {
6035 uint64_t value = static_cast<uint64_t>(Int64FromConstant(input_cst));
6036 if (DataType::Is64BitType(input_cst->GetType())) {
6037 Opcode high_opcode = opcode;
6038 vixl32::FlagsUpdate low_flags_update = vixl32::FlagsUpdate::DontCare;
6039 switch (opcode) {
6040 case SUB:
6041 // Flip the operation to an ADD.
6042 value = -value;
6043 opcode = ADD;
6044 FALLTHROUGH_INTENDED;
6045 case ADD:
6046 if (Low32Bits(value) == 0u) {
6047 return CanEncode32BitConstantAsImmediate(codegen_, High32Bits(value), opcode);
6048 }
6049 high_opcode = ADC;
6050 low_flags_update = vixl32::FlagsUpdate::SetFlags;
6051 break;
6052 default:
6053 break;
6054 }
6055 return CanEncode32BitConstantAsImmediate(codegen_, High32Bits(value), high_opcode) &&
6056 CanEncode32BitConstantAsImmediate(codegen_, Low32Bits(value), opcode, low_flags_update);
6057 } else {
6058 return CanEncode32BitConstantAsImmediate(codegen_, Low32Bits(value), opcode);
6059 }
6060}
6061
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006062void InstructionCodeGeneratorARMVIXL::HandleFieldGet(HInstruction* instruction,
6063 const FieldInfo& field_info) {
6064 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
6065
6066 LocationSummary* locations = instruction->GetLocations();
6067 vixl32::Register base = InputRegisterAt(instruction, 0);
6068 Location out = locations->Out();
6069 bool is_volatile = field_info.IsVolatile();
6070 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Vladimir Marko61b92282017-10-11 13:23:17 +01006071 DCHECK_EQ(DataType::Size(field_info.GetFieldType()), DataType::Size(instruction->GetType()));
6072 DataType::Type load_type = instruction->GetType();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006073 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
6074
Vladimir Marko61b92282017-10-11 13:23:17 +01006075 switch (load_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006076 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006077 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006078 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006079 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006080 case DataType::Type::kInt16:
6081 case DataType::Type::kInt32: {
Vladimir Marko61b92282017-10-11 13:23:17 +01006082 LoadOperandType operand_type = GetLoadOperandType(load_type);
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006083 GetAssembler()->LoadFromOffset(operand_type, RegisterFrom(out), base, offset);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006084 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006085 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006086
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006087 case DataType::Type::kReference: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006088 // /* HeapReference<Object> */ out = *(base + offset)
6089 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006090 Location temp_loc = locations->GetTemp(0);
6091 // Note that a potential implicit null check is handled in this
6092 // CodeGeneratorARMVIXL::GenerateFieldLoadWithBakerReadBarrier call.
6093 codegen_->GenerateFieldLoadWithBakerReadBarrier(
6094 instruction, out, base, offset, temp_loc, /* needs_null_check */ true);
6095 if (is_volatile) {
6096 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6097 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006098 } else {
6099 GetAssembler()->LoadFromOffset(kLoadWord, RegisterFrom(out), base, offset);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006100 codegen_->MaybeRecordImplicitNullCheck(instruction);
6101 if (is_volatile) {
6102 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6103 }
6104 // If read barriers are enabled, emit read barriers other than
6105 // Baker's using a slow path (and also unpoison the loaded
6106 // reference, if heap poisoning is enabled).
6107 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, locations->InAt(0), offset);
6108 }
6109 break;
6110 }
6111
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006112 case DataType::Type::kInt64:
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006113 if (is_volatile && !atomic_ldrd_strd) {
6114 GenerateWideAtomicLoad(base, offset, LowRegisterFrom(out), HighRegisterFrom(out));
6115 } else {
6116 GetAssembler()->LoadFromOffset(kLoadWordPair, LowRegisterFrom(out), base, offset);
6117 }
6118 break;
6119
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006120 case DataType::Type::kFloat32:
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006121 GetAssembler()->LoadSFromOffset(SRegisterFrom(out), base, offset);
6122 break;
6123
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006124 case DataType::Type::kFloat64: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006125 vixl32::DRegister out_dreg = DRegisterFrom(out);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006126 if (is_volatile && !atomic_ldrd_strd) {
6127 vixl32::Register lo = RegisterFrom(locations->GetTemp(0));
6128 vixl32::Register hi = RegisterFrom(locations->GetTemp(1));
6129 GenerateWideAtomicLoad(base, offset, lo, hi);
6130 // TODO(VIXL): Do we need to be immediately after the ldrexd instruction? If so we need a
6131 // scope.
6132 codegen_->MaybeRecordImplicitNullCheck(instruction);
6133 __ Vmov(out_dreg, lo, hi);
6134 } else {
6135 GetAssembler()->LoadDFromOffset(out_dreg, base, offset);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006136 codegen_->MaybeRecordImplicitNullCheck(instruction);
6137 }
6138 break;
6139 }
6140
Aart Bik66c158e2018-01-31 12:55:04 -08006141 case DataType::Type::kUint32:
6142 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006143 case DataType::Type::kVoid:
Vladimir Marko61b92282017-10-11 13:23:17 +01006144 LOG(FATAL) << "Unreachable type " << load_type;
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006145 UNREACHABLE();
6146 }
6147
Vladimir Marko61b92282017-10-11 13:23:17 +01006148 if (load_type == DataType::Type::kReference || load_type == DataType::Type::kFloat64) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006149 // Potential implicit null checks, in the case of reference or
6150 // double fields, are handled in the previous switch statement.
6151 } else {
6152 // Address cases other than reference and double that may require an implicit null check.
Alexandre Rames374ddf32016-11-04 10:40:49 +00006153 // TODO(VIXL): Here and for other calls to `MaybeRecordImplicitNullCheck` in this method, we
6154 // should use a scope and the assembler to emit the load instruction to guarantee that we
6155 // record the pc at the correct position. But the `Assembler` does not automatically handle
6156 // unencodable offsets. Practically, everything is fine because the helper and VIXL, at the time
6157 // of writing, do generate the store instruction last.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006158 codegen_->MaybeRecordImplicitNullCheck(instruction);
6159 }
6160
6161 if (is_volatile) {
Vladimir Marko61b92282017-10-11 13:23:17 +01006162 if (load_type == DataType::Type::kReference) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006163 // Memory barriers, in the case of references, are also handled
6164 // in the previous switch statement.
6165 } else {
6166 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6167 }
6168 }
6169}
6170
6171void LocationsBuilderARMVIXL::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
6172 HandleFieldSet(instruction, instruction->GetFieldInfo());
6173}
6174
6175void InstructionCodeGeneratorARMVIXL::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
6176 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
6177}
6178
6179void LocationsBuilderARMVIXL::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
6180 HandleFieldGet(instruction, instruction->GetFieldInfo());
6181}
6182
6183void InstructionCodeGeneratorARMVIXL::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
6184 HandleFieldGet(instruction, instruction->GetFieldInfo());
6185}
6186
6187void LocationsBuilderARMVIXL::VisitStaticFieldGet(HStaticFieldGet* instruction) {
6188 HandleFieldGet(instruction, instruction->GetFieldInfo());
6189}
6190
6191void InstructionCodeGeneratorARMVIXL::VisitStaticFieldGet(HStaticFieldGet* instruction) {
6192 HandleFieldGet(instruction, instruction->GetFieldInfo());
6193}
6194
Scott Wakelingc34dba72016-10-03 10:14:44 +01006195void LocationsBuilderARMVIXL::VisitStaticFieldSet(HStaticFieldSet* instruction) {
6196 HandleFieldSet(instruction, instruction->GetFieldInfo());
6197}
6198
6199void InstructionCodeGeneratorARMVIXL::VisitStaticFieldSet(HStaticFieldSet* instruction) {
6200 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
6201}
6202
Artem Serovcfbe9132016-10-14 15:58:56 +01006203void LocationsBuilderARMVIXL::VisitUnresolvedInstanceFieldGet(
6204 HUnresolvedInstanceFieldGet* instruction) {
6205 FieldAccessCallingConventionARMVIXL calling_convention;
6206 codegen_->CreateUnresolvedFieldLocationSummary(
6207 instruction, instruction->GetFieldType(), calling_convention);
6208}
6209
6210void InstructionCodeGeneratorARMVIXL::VisitUnresolvedInstanceFieldGet(
6211 HUnresolvedInstanceFieldGet* instruction) {
6212 FieldAccessCallingConventionARMVIXL calling_convention;
6213 codegen_->GenerateUnresolvedFieldAccess(instruction,
6214 instruction->GetFieldType(),
6215 instruction->GetFieldIndex(),
6216 instruction->GetDexPc(),
6217 calling_convention);
6218}
6219
6220void LocationsBuilderARMVIXL::VisitUnresolvedInstanceFieldSet(
6221 HUnresolvedInstanceFieldSet* instruction) {
6222 FieldAccessCallingConventionARMVIXL calling_convention;
6223 codegen_->CreateUnresolvedFieldLocationSummary(
6224 instruction, instruction->GetFieldType(), calling_convention);
6225}
6226
6227void InstructionCodeGeneratorARMVIXL::VisitUnresolvedInstanceFieldSet(
6228 HUnresolvedInstanceFieldSet* instruction) {
6229 FieldAccessCallingConventionARMVIXL calling_convention;
6230 codegen_->GenerateUnresolvedFieldAccess(instruction,
6231 instruction->GetFieldType(),
6232 instruction->GetFieldIndex(),
6233 instruction->GetDexPc(),
6234 calling_convention);
6235}
6236
6237void LocationsBuilderARMVIXL::VisitUnresolvedStaticFieldGet(
6238 HUnresolvedStaticFieldGet* instruction) {
6239 FieldAccessCallingConventionARMVIXL calling_convention;
6240 codegen_->CreateUnresolvedFieldLocationSummary(
6241 instruction, instruction->GetFieldType(), calling_convention);
6242}
6243
6244void InstructionCodeGeneratorARMVIXL::VisitUnresolvedStaticFieldGet(
6245 HUnresolvedStaticFieldGet* instruction) {
6246 FieldAccessCallingConventionARMVIXL calling_convention;
6247 codegen_->GenerateUnresolvedFieldAccess(instruction,
6248 instruction->GetFieldType(),
6249 instruction->GetFieldIndex(),
6250 instruction->GetDexPc(),
6251 calling_convention);
6252}
6253
6254void LocationsBuilderARMVIXL::VisitUnresolvedStaticFieldSet(
6255 HUnresolvedStaticFieldSet* instruction) {
6256 FieldAccessCallingConventionARMVIXL calling_convention;
6257 codegen_->CreateUnresolvedFieldLocationSummary(
6258 instruction, instruction->GetFieldType(), calling_convention);
6259}
6260
6261void InstructionCodeGeneratorARMVIXL::VisitUnresolvedStaticFieldSet(
6262 HUnresolvedStaticFieldSet* instruction) {
6263 FieldAccessCallingConventionARMVIXL calling_convention;
6264 codegen_->GenerateUnresolvedFieldAccess(instruction,
6265 instruction->GetFieldType(),
6266 instruction->GetFieldIndex(),
6267 instruction->GetDexPc(),
6268 calling_convention);
6269}
6270
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006271void LocationsBuilderARMVIXL::VisitNullCheck(HNullCheck* instruction) {
Artem Serov657022c2016-11-23 14:19:38 +00006272 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006273 locations->SetInAt(0, Location::RequiresRegister());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006274}
6275
6276void CodeGeneratorARMVIXL::GenerateImplicitNullCheck(HNullCheck* instruction) {
6277 if (CanMoveNullCheckToUser(instruction)) {
6278 return;
6279 }
6280
6281 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames374ddf32016-11-04 10:40:49 +00006282 // Ensure the pc position is recorded immediately after the `ldr` instruction.
Artem Serov0fb37192016-12-06 18:13:40 +00006283 ExactAssemblyScope aas(GetVIXLAssembler(),
6284 vixl32::kMaxInstructionSizeInBytes,
6285 CodeBufferCheckScope::kMaximumSize);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006286 __ ldr(temps.Acquire(), MemOperand(InputRegisterAt(instruction, 0)));
6287 RecordPcInfo(instruction, instruction->GetDexPc());
6288}
6289
6290void CodeGeneratorARMVIXL::GenerateExplicitNullCheck(HNullCheck* instruction) {
6291 NullCheckSlowPathARMVIXL* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006292 new (GetScopedAllocator()) NullCheckSlowPathARMVIXL(instruction);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006293 AddSlowPath(slow_path);
xueliang.zhongf51bc622016-11-04 09:23:32 +00006294 __ CompareAndBranchIfZero(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006295}
6296
6297void InstructionCodeGeneratorARMVIXL::VisitNullCheck(HNullCheck* instruction) {
6298 codegen_->GenerateNullCheck(instruction);
6299}
6300
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006301void CodeGeneratorARMVIXL::LoadFromShiftedRegOffset(DataType::Type type,
Scott Wakelingc34dba72016-10-03 10:14:44 +01006302 Location out_loc,
6303 vixl32::Register base,
6304 vixl32::Register reg_index,
6305 vixl32::Condition cond) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006306 uint32_t shift_count = DataType::SizeShift(type);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006307 MemOperand mem_address(base, reg_index, vixl32::LSL, shift_count);
6308
6309 switch (type) {
Vladimir Marko61b92282017-10-11 13:23:17 +01006310 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006311 case DataType::Type::kUint8:
Vladimir Marko61b92282017-10-11 13:23:17 +01006312 __ Ldrb(cond, RegisterFrom(out_loc), mem_address);
6313 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006314 case DataType::Type::kInt8:
Scott Wakelingc34dba72016-10-03 10:14:44 +01006315 __ Ldrsb(cond, RegisterFrom(out_loc), mem_address);
6316 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006317 case DataType::Type::kUint16:
Scott Wakelingc34dba72016-10-03 10:14:44 +01006318 __ Ldrh(cond, RegisterFrom(out_loc), mem_address);
6319 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006320 case DataType::Type::kInt16:
6321 __ Ldrsh(cond, RegisterFrom(out_loc), mem_address);
6322 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006323 case DataType::Type::kReference:
6324 case DataType::Type::kInt32:
Scott Wakelingc34dba72016-10-03 10:14:44 +01006325 __ Ldr(cond, RegisterFrom(out_loc), mem_address);
6326 break;
6327 // T32 doesn't support LoadFromShiftedRegOffset mem address mode for these types.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006328 case DataType::Type::kInt64:
6329 case DataType::Type::kFloat32:
6330 case DataType::Type::kFloat64:
Scott Wakelingc34dba72016-10-03 10:14:44 +01006331 default:
6332 LOG(FATAL) << "Unreachable type " << type;
6333 UNREACHABLE();
6334 }
6335}
6336
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006337void CodeGeneratorARMVIXL::StoreToShiftedRegOffset(DataType::Type type,
Scott Wakelingc34dba72016-10-03 10:14:44 +01006338 Location loc,
6339 vixl32::Register base,
6340 vixl32::Register reg_index,
6341 vixl32::Condition cond) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006342 uint32_t shift_count = DataType::SizeShift(type);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006343 MemOperand mem_address(base, reg_index, vixl32::LSL, shift_count);
6344
6345 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006346 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006347 case DataType::Type::kUint8:
6348 case DataType::Type::kInt8:
Scott Wakelingc34dba72016-10-03 10:14:44 +01006349 __ Strb(cond, RegisterFrom(loc), mem_address);
6350 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006351 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006352 case DataType::Type::kInt16:
Scott Wakelingc34dba72016-10-03 10:14:44 +01006353 __ Strh(cond, RegisterFrom(loc), mem_address);
6354 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006355 case DataType::Type::kReference:
6356 case DataType::Type::kInt32:
Scott Wakelingc34dba72016-10-03 10:14:44 +01006357 __ Str(cond, RegisterFrom(loc), mem_address);
6358 break;
6359 // T32 doesn't support StoreToShiftedRegOffset mem address mode for these types.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006360 case DataType::Type::kInt64:
6361 case DataType::Type::kFloat32:
6362 case DataType::Type::kFloat64:
Scott Wakelingc34dba72016-10-03 10:14:44 +01006363 default:
6364 LOG(FATAL) << "Unreachable type " << type;
6365 UNREACHABLE();
6366 }
6367}
6368
6369void LocationsBuilderARMVIXL::VisitArrayGet(HArrayGet* instruction) {
6370 bool object_array_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006371 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006372 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006373 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
6374 object_array_get_with_read_barrier
6375 ? LocationSummary::kCallOnSlowPath
6376 : LocationSummary::kNoCall);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006377 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006378 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Scott Wakelingc34dba72016-10-03 10:14:44 +01006379 }
6380 locations->SetInAt(0, Location::RequiresRegister());
6381 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006382 if (DataType::IsFloatingPointType(instruction->GetType())) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006383 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
6384 } else {
6385 // The output overlaps in the case of an object array get with
6386 // read barriers enabled: we do not want the move to overwrite the
6387 // array's location, as we need it to emit the read barrier.
6388 locations->SetOut(
6389 Location::RequiresRegister(),
6390 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
6391 }
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01006392 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
6393 // We need a temporary register for the read barrier marking slow
6394 // path in CodeGeneratorARMVIXL::GenerateArrayLoadWithBakerReadBarrier.
6395 if (kBakerReadBarrierLinkTimeThunksEnableForFields &&
6396 !Runtime::Current()->UseJitCompilation() &&
6397 instruction->GetIndex()->IsConstant()) {
6398 // Array loads with constant index are treated as field loads.
6399 // If link-time thunks for the Baker read barrier are enabled, for AOT
6400 // constant index loads we need a temporary only if the offset is too big.
6401 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
6402 uint32_t index = instruction->GetIndex()->AsIntConstant()->GetValue();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006403 offset += index << DataType::SizeShift(DataType::Type::kReference);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01006404 if (offset >= kReferenceLoadMinFarOffset) {
6405 locations->AddTemp(Location::RequiresRegister());
6406 }
6407 // And we always need the reserved entrypoint register.
6408 locations->AddTemp(Location::RegisterLocation(kBakerCcEntrypointRegister.GetCode()));
6409 } else if (kBakerReadBarrierLinkTimeThunksEnableForArrays &&
6410 !Runtime::Current()->UseJitCompilation() &&
6411 !instruction->GetIndex()->IsConstant()) {
6412 // We need a non-scratch temporary for the array data pointer.
6413 locations->AddTemp(Location::RequiresRegister());
6414 // And we always need the reserved entrypoint register.
6415 locations->AddTemp(Location::RegisterLocation(kBakerCcEntrypointRegister.GetCode()));
6416 } else {
6417 locations->AddTemp(Location::RequiresRegister());
6418 }
6419 } else if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
6420 // Also need a temporary for String compression feature.
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006421 locations->AddTemp(Location::RequiresRegister());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006422 }
6423}
6424
6425void InstructionCodeGeneratorARMVIXL::VisitArrayGet(HArrayGet* instruction) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006426 LocationSummary* locations = instruction->GetLocations();
6427 Location obj_loc = locations->InAt(0);
6428 vixl32::Register obj = InputRegisterAt(instruction, 0);
6429 Location index = locations->InAt(1);
6430 Location out_loc = locations->Out();
6431 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006432 DataType::Type type = instruction->GetType();
Scott Wakelingc34dba72016-10-03 10:14:44 +01006433 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
6434 instruction->IsStringCharAt();
6435 HInstruction* array_instr = instruction->GetArray();
6436 bool has_intermediate_address = array_instr->IsIntermediateAddress();
Scott Wakelingc34dba72016-10-03 10:14:44 +01006437
6438 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006439 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006440 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006441 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006442 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006443 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006444 case DataType::Type::kInt32: {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006445 vixl32::Register length;
6446 if (maybe_compressed_char_at) {
6447 length = RegisterFrom(locations->GetTemp(0));
6448 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
6449 GetAssembler()->LoadFromOffset(kLoadWord, length, obj, count_offset);
6450 codegen_->MaybeRecordImplicitNullCheck(instruction);
6451 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006452 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006453 int32_t const_index = Int32ConstantFrom(index);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006454 if (maybe_compressed_char_at) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006455 vixl32::Label uncompressed_load, done;
Anton Kirilov6f644202017-02-27 18:29:45 +00006456 vixl32::Label* final_label = codegen_->GetFinalLabel(instruction, &done);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006457 __ Lsrs(length, length, 1u); // LSRS has a 16-bit encoding, TST (immediate) does not.
6458 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
6459 "Expecting 0=compressed, 1=uncompressed");
Artem Serov517d9f62016-12-12 15:51:15 +00006460 __ B(cs, &uncompressed_load, /* far_target */ false);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006461 GetAssembler()->LoadFromOffset(kLoadUnsignedByte,
6462 RegisterFrom(out_loc),
6463 obj,
6464 data_offset + const_index);
Anton Kirilov6f644202017-02-27 18:29:45 +00006465 __ B(final_label);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006466 __ Bind(&uncompressed_load);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006467 GetAssembler()->LoadFromOffset(GetLoadOperandType(DataType::Type::kUint16),
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006468 RegisterFrom(out_loc),
6469 obj,
6470 data_offset + (const_index << 1));
Anton Kirilov6f644202017-02-27 18:29:45 +00006471 if (done.IsReferenced()) {
6472 __ Bind(&done);
6473 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006474 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006475 uint32_t full_offset = data_offset + (const_index << DataType::SizeShift(type));
Scott Wakelingc34dba72016-10-03 10:14:44 +01006476
6477 LoadOperandType load_type = GetLoadOperandType(type);
6478 GetAssembler()->LoadFromOffset(load_type, RegisterFrom(out_loc), obj, full_offset);
6479 }
6480 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006481 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006482 vixl32::Register temp = temps.Acquire();
6483
6484 if (has_intermediate_address) {
Artem Serov2bbc9532016-10-21 11:51:50 +01006485 // We do not need to compute the intermediate address from the array: the
6486 // input instruction has done it already. See the comment in
6487 // `TryExtractArrayAccessAddress()`.
6488 if (kIsDebugBuild) {
6489 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
Anton Kirilov644032c2016-12-06 17:51:43 +00006490 DCHECK_EQ(Uint64ConstantFrom(tmp->GetOffset()), data_offset);
Artem Serov2bbc9532016-10-21 11:51:50 +01006491 }
6492 temp = obj;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006493 } else {
6494 __ Add(temp, obj, data_offset);
6495 }
6496 if (maybe_compressed_char_at) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006497 vixl32::Label uncompressed_load, done;
Anton Kirilov6f644202017-02-27 18:29:45 +00006498 vixl32::Label* final_label = codegen_->GetFinalLabel(instruction, &done);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006499 __ Lsrs(length, length, 1u); // LSRS has a 16-bit encoding, TST (immediate) does not.
6500 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
6501 "Expecting 0=compressed, 1=uncompressed");
Artem Serov517d9f62016-12-12 15:51:15 +00006502 __ B(cs, &uncompressed_load, /* far_target */ false);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006503 __ Ldrb(RegisterFrom(out_loc), MemOperand(temp, RegisterFrom(index), vixl32::LSL, 0));
Anton Kirilov6f644202017-02-27 18:29:45 +00006504 __ B(final_label);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006505 __ Bind(&uncompressed_load);
6506 __ Ldrh(RegisterFrom(out_loc), MemOperand(temp, RegisterFrom(index), vixl32::LSL, 1));
Anton Kirilov6f644202017-02-27 18:29:45 +00006507 if (done.IsReferenced()) {
6508 __ Bind(&done);
6509 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006510 } else {
6511 codegen_->LoadFromShiftedRegOffset(type, out_loc, temp, RegisterFrom(index));
6512 }
6513 }
6514 break;
6515 }
6516
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006517 case DataType::Type::kReference: {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006518 // The read barrier instrumentation of object ArrayGet
6519 // instructions does not support the HIntermediateAddress
6520 // instruction.
6521 DCHECK(!(has_intermediate_address && kEmitCompilerReadBarrier));
6522
Scott Wakelingc34dba72016-10-03 10:14:44 +01006523 static_assert(
6524 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6525 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
6526 // /* HeapReference<Object> */ out =
6527 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
6528 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006529 Location temp = locations->GetTemp(0);
6530 // Note that a potential implicit null check is handled in this
6531 // CodeGeneratorARMVIXL::GenerateArrayLoadWithBakerReadBarrier call.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01006532 DCHECK(!instruction->CanDoImplicitNullCheckOn(instruction->InputAt(0)));
6533 if (index.IsConstant()) {
6534 // Array load with a constant index can be treated as a field load.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006535 data_offset += Int32ConstantFrom(index) << DataType::SizeShift(type);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01006536 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6537 out_loc,
6538 obj,
6539 data_offset,
6540 locations->GetTemp(0),
6541 /* needs_null_check */ false);
6542 } else {
6543 codegen_->GenerateArrayLoadWithBakerReadBarrier(
6544 instruction, out_loc, obj, data_offset, index, temp, /* needs_null_check */ false);
6545 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006546 } else {
6547 vixl32::Register out = OutputRegister(instruction);
6548 if (index.IsConstant()) {
6549 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00006550 (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006551 GetAssembler()->LoadFromOffset(kLoadWord, out, obj, offset);
Alexandre Rames374ddf32016-11-04 10:40:49 +00006552 // TODO(VIXL): Here and for other calls to `MaybeRecordImplicitNullCheck` in this method,
6553 // we should use a scope and the assembler to emit the load instruction to guarantee that
6554 // we record the pc at the correct position. But the `Assembler` does not automatically
6555 // handle unencodable offsets. Practically, everything is fine because the helper and
6556 // VIXL, at the time of writing, do generate the store instruction last.
Scott Wakelingc34dba72016-10-03 10:14:44 +01006557 codegen_->MaybeRecordImplicitNullCheck(instruction);
6558 // If read barriers are enabled, emit read barriers other than
6559 // Baker's using a slow path (and also unpoison the loaded
6560 // reference, if heap poisoning is enabled).
6561 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
6562 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006563 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006564 vixl32::Register temp = temps.Acquire();
6565
6566 if (has_intermediate_address) {
Artem Serov2bbc9532016-10-21 11:51:50 +01006567 // We do not need to compute the intermediate address from the array: the
6568 // input instruction has done it already. See the comment in
6569 // `TryExtractArrayAccessAddress()`.
6570 if (kIsDebugBuild) {
6571 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
Anton Kirilov644032c2016-12-06 17:51:43 +00006572 DCHECK_EQ(Uint64ConstantFrom(tmp->GetOffset()), data_offset);
Artem Serov2bbc9532016-10-21 11:51:50 +01006573 }
6574 temp = obj;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006575 } else {
6576 __ Add(temp, obj, data_offset);
6577 }
6578 codegen_->LoadFromShiftedRegOffset(type, out_loc, temp, RegisterFrom(index));
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006579 temps.Close();
Alexandre Rames374ddf32016-11-04 10:40:49 +00006580 // TODO(VIXL): Use a scope to ensure that we record the pc position immediately after the
6581 // load instruction. Practically, everything is fine because the helper and VIXL, at the
6582 // time of writing, do generate the store instruction last.
Scott Wakelingc34dba72016-10-03 10:14:44 +01006583 codegen_->MaybeRecordImplicitNullCheck(instruction);
6584 // If read barriers are enabled, emit read barriers other than
6585 // Baker's using a slow path (and also unpoison the loaded
6586 // reference, if heap poisoning is enabled).
6587 codegen_->MaybeGenerateReadBarrierSlow(
6588 instruction, out_loc, out_loc, obj_loc, data_offset, index);
6589 }
6590 }
6591 break;
6592 }
6593
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006594 case DataType::Type::kInt64: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006595 if (index.IsConstant()) {
6596 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00006597 (Int32ConstantFrom(index) << TIMES_8) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006598 GetAssembler()->LoadFromOffset(kLoadWordPair, LowRegisterFrom(out_loc), obj, offset);
6599 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006600 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006601 vixl32::Register temp = temps.Acquire();
6602 __ Add(temp, obj, Operand(RegisterFrom(index), vixl32::LSL, TIMES_8));
6603 GetAssembler()->LoadFromOffset(kLoadWordPair, LowRegisterFrom(out_loc), temp, data_offset);
6604 }
6605 break;
6606 }
6607
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006608 case DataType::Type::kFloat32: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006609 vixl32::SRegister out = SRegisterFrom(out_loc);
6610 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006611 size_t offset = (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006612 GetAssembler()->LoadSFromOffset(out, obj, offset);
6613 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006614 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006615 vixl32::Register temp = temps.Acquire();
6616 __ Add(temp, obj, Operand(RegisterFrom(index), vixl32::LSL, TIMES_4));
6617 GetAssembler()->LoadSFromOffset(out, temp, data_offset);
6618 }
6619 break;
6620 }
6621
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006622 case DataType::Type::kFloat64: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006623 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006624 size_t offset = (Int32ConstantFrom(index) << TIMES_8) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006625 GetAssembler()->LoadDFromOffset(DRegisterFrom(out_loc), obj, offset);
6626 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006627 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006628 vixl32::Register temp = temps.Acquire();
6629 __ Add(temp, obj, Operand(RegisterFrom(index), vixl32::LSL, TIMES_8));
6630 GetAssembler()->LoadDFromOffset(DRegisterFrom(out_loc), temp, data_offset);
6631 }
6632 break;
6633 }
6634
Aart Bik66c158e2018-01-31 12:55:04 -08006635 case DataType::Type::kUint32:
6636 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006637 case DataType::Type::kVoid:
Scott Wakelingc34dba72016-10-03 10:14:44 +01006638 LOG(FATAL) << "Unreachable type " << type;
6639 UNREACHABLE();
6640 }
6641
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006642 if (type == DataType::Type::kReference) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006643 // Potential implicit null checks, in the case of reference
6644 // arrays, are handled in the previous switch statement.
6645 } else if (!maybe_compressed_char_at) {
Alexandre Rames374ddf32016-11-04 10:40:49 +00006646 // TODO(VIXL): Use a scope to ensure we record the pc info immediately after
6647 // the preceding load instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01006648 codegen_->MaybeRecordImplicitNullCheck(instruction);
6649 }
6650}
6651
6652void LocationsBuilderARMVIXL::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006653 DataType::Type value_type = instruction->GetComponentType();
Scott Wakelingc34dba72016-10-03 10:14:44 +01006654
6655 bool needs_write_barrier =
6656 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
6657 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
6658
Vladimir Markoca6fff82017-10-03 14:49:14 +01006659 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Scott Wakelingc34dba72016-10-03 10:14:44 +01006660 instruction,
6661 may_need_runtime_call_for_type_check ?
6662 LocationSummary::kCallOnSlowPath :
6663 LocationSummary::kNoCall);
6664
6665 locations->SetInAt(0, Location::RequiresRegister());
6666 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006667 if (DataType::IsFloatingPointType(value_type)) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006668 locations->SetInAt(2, Location::RequiresFpuRegister());
6669 } else {
6670 locations->SetInAt(2, Location::RequiresRegister());
6671 }
6672 if (needs_write_barrier) {
6673 // Temporary registers for the write barrier.
6674 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
6675 locations->AddTemp(Location::RequiresRegister());
6676 }
6677}
6678
6679void InstructionCodeGeneratorARMVIXL::VisitArraySet(HArraySet* instruction) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006680 LocationSummary* locations = instruction->GetLocations();
6681 vixl32::Register array = InputRegisterAt(instruction, 0);
6682 Location index = locations->InAt(1);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006683 DataType::Type value_type = instruction->GetComponentType();
Scott Wakelingc34dba72016-10-03 10:14:44 +01006684 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
6685 bool needs_write_barrier =
6686 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
6687 uint32_t data_offset =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006688 mirror::Array::DataOffset(DataType::Size(value_type)).Uint32Value();
Scott Wakelingc34dba72016-10-03 10:14:44 +01006689 Location value_loc = locations->InAt(2);
6690 HInstruction* array_instr = instruction->GetArray();
6691 bool has_intermediate_address = array_instr->IsIntermediateAddress();
Scott Wakelingc34dba72016-10-03 10:14:44 +01006692
6693 switch (value_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006694 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006695 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006696 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006697 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006698 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006699 case DataType::Type::kInt32: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006700 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006701 int32_t const_index = Int32ConstantFrom(index);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006702 uint32_t full_offset =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006703 data_offset + (const_index << DataType::SizeShift(value_type));
Scott Wakelingc34dba72016-10-03 10:14:44 +01006704 StoreOperandType store_type = GetStoreOperandType(value_type);
6705 GetAssembler()->StoreToOffset(store_type, RegisterFrom(value_loc), array, full_offset);
6706 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006707 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006708 vixl32::Register temp = temps.Acquire();
6709
6710 if (has_intermediate_address) {
Artem Serov2bbc9532016-10-21 11:51:50 +01006711 // We do not need to compute the intermediate address from the array: the
6712 // input instruction has done it already. See the comment in
6713 // `TryExtractArrayAccessAddress()`.
6714 if (kIsDebugBuild) {
6715 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
Anton Kirilov644032c2016-12-06 17:51:43 +00006716 DCHECK_EQ(Uint64ConstantFrom(tmp->GetOffset()), data_offset);
Artem Serov2bbc9532016-10-21 11:51:50 +01006717 }
6718 temp = array;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006719 } else {
6720 __ Add(temp, array, data_offset);
6721 }
6722 codegen_->StoreToShiftedRegOffset(value_type, value_loc, temp, RegisterFrom(index));
6723 }
6724 break;
6725 }
6726
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006727 case DataType::Type::kReference: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006728 vixl32::Register value = RegisterFrom(value_loc);
6729 // TryExtractArrayAccessAddress optimization is never applied for non-primitive ArraySet.
6730 // See the comment in instruction_simplifier_shared.cc.
6731 DCHECK(!has_intermediate_address);
6732
6733 if (instruction->InputAt(2)->IsNullConstant()) {
6734 // Just setting null.
6735 if (index.IsConstant()) {
6736 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00006737 (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006738 GetAssembler()->StoreToOffset(kStoreWord, value, array, offset);
6739 } else {
6740 DCHECK(index.IsRegister()) << index;
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006741 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006742 vixl32::Register temp = temps.Acquire();
6743 __ Add(temp, array, data_offset);
6744 codegen_->StoreToShiftedRegOffset(value_type, value_loc, temp, RegisterFrom(index));
6745 }
Alexandre Rames374ddf32016-11-04 10:40:49 +00006746 // TODO(VIXL): Use a scope to ensure we record the pc info immediately after the preceding
6747 // store instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01006748 codegen_->MaybeRecordImplicitNullCheck(instruction);
6749 DCHECK(!needs_write_barrier);
6750 DCHECK(!may_need_runtime_call_for_type_check);
6751 break;
6752 }
6753
6754 DCHECK(needs_write_barrier);
6755 Location temp1_loc = locations->GetTemp(0);
6756 vixl32::Register temp1 = RegisterFrom(temp1_loc);
6757 Location temp2_loc = locations->GetTemp(1);
6758 vixl32::Register temp2 = RegisterFrom(temp2_loc);
6759 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6760 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6761 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6762 vixl32::Label done;
Anton Kirilov6f644202017-02-27 18:29:45 +00006763 vixl32::Label* final_label = codegen_->GetFinalLabel(instruction, &done);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006764 SlowPathCodeARMVIXL* slow_path = nullptr;
6765
6766 if (may_need_runtime_call_for_type_check) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01006767 slow_path = new (codegen_->GetScopedAllocator()) ArraySetSlowPathARMVIXL(instruction);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006768 codegen_->AddSlowPath(slow_path);
6769 if (instruction->GetValueCanBeNull()) {
6770 vixl32::Label non_zero;
xueliang.zhongf51bc622016-11-04 09:23:32 +00006771 __ CompareAndBranchIfNonZero(value, &non_zero);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006772 if (index.IsConstant()) {
6773 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00006774 (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006775 GetAssembler()->StoreToOffset(kStoreWord, value, array, offset);
6776 } else {
6777 DCHECK(index.IsRegister()) << index;
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006778 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006779 vixl32::Register temp = temps.Acquire();
6780 __ Add(temp, array, data_offset);
6781 codegen_->StoreToShiftedRegOffset(value_type, value_loc, temp, RegisterFrom(index));
6782 }
Alexandre Rames374ddf32016-11-04 10:40:49 +00006783 // TODO(VIXL): Use a scope to ensure we record the pc info immediately after the preceding
6784 // store instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01006785 codegen_->MaybeRecordImplicitNullCheck(instruction);
Anton Kirilov6f644202017-02-27 18:29:45 +00006786 __ B(final_label);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006787 __ Bind(&non_zero);
6788 }
6789
6790 // Note that when read barriers are enabled, the type checks
6791 // are performed without read barriers. This is fine, even in
6792 // the case where a class object is in the from-space after
6793 // the flip, as a comparison involving such a type would not
6794 // produce a false positive; it may of course produce a false
6795 // negative, in which case we would take the ArraySet slow
6796 // path.
6797
Alexandre Rames374ddf32016-11-04 10:40:49 +00006798 {
6799 // Ensure we record the pc position immediately after the `ldr` instruction.
Artem Serov0fb37192016-12-06 18:13:40 +00006800 ExactAssemblyScope aas(GetVIXLAssembler(),
6801 vixl32::kMaxInstructionSizeInBytes,
6802 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00006803 // /* HeapReference<Class> */ temp1 = array->klass_
6804 __ ldr(temp1, MemOperand(array, class_offset));
6805 codegen_->MaybeRecordImplicitNullCheck(instruction);
6806 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006807 GetAssembler()->MaybeUnpoisonHeapReference(temp1);
6808
6809 // /* HeapReference<Class> */ temp1 = temp1->component_type_
6810 GetAssembler()->LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
6811 // /* HeapReference<Class> */ temp2 = value->klass_
6812 GetAssembler()->LoadFromOffset(kLoadWord, temp2, value, class_offset);
6813 // If heap poisoning is enabled, no need to unpoison `temp1`
6814 // nor `temp2`, as we are comparing two poisoned references.
6815 __ Cmp(temp1, temp2);
6816
6817 if (instruction->StaticTypeOfArrayIsObjectArray()) {
6818 vixl32::Label do_put;
Artem Serov517d9f62016-12-12 15:51:15 +00006819 __ B(eq, &do_put, /* far_target */ false);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006820 // If heap poisoning is enabled, the `temp1` reference has
6821 // not been unpoisoned yet; unpoison it now.
6822 GetAssembler()->MaybeUnpoisonHeapReference(temp1);
6823
6824 // /* HeapReference<Class> */ temp1 = temp1->super_class_
6825 GetAssembler()->LoadFromOffset(kLoadWord, temp1, temp1, super_offset);
6826 // If heap poisoning is enabled, no need to unpoison
6827 // `temp1`, as we are comparing against null below.
xueliang.zhongf51bc622016-11-04 09:23:32 +00006828 __ CompareAndBranchIfNonZero(temp1, slow_path->GetEntryLabel());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006829 __ Bind(&do_put);
6830 } else {
6831 __ B(ne, slow_path->GetEntryLabel());
6832 }
6833 }
6834
6835 vixl32::Register source = value;
6836 if (kPoisonHeapReferences) {
6837 // Note that in the case where `value` is a null reference,
6838 // we do not enter this block, as a null reference does not
6839 // need poisoning.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006840 DCHECK_EQ(value_type, DataType::Type::kReference);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006841 __ Mov(temp1, value);
6842 GetAssembler()->PoisonHeapReference(temp1);
6843 source = temp1;
6844 }
6845
6846 if (index.IsConstant()) {
6847 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00006848 (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006849 GetAssembler()->StoreToOffset(kStoreWord, source, array, offset);
6850 } else {
6851 DCHECK(index.IsRegister()) << index;
6852
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006853 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006854 vixl32::Register temp = temps.Acquire();
6855 __ Add(temp, array, data_offset);
6856 codegen_->StoreToShiftedRegOffset(value_type,
6857 LocationFrom(source),
6858 temp,
6859 RegisterFrom(index));
6860 }
6861
6862 if (!may_need_runtime_call_for_type_check) {
Alexandre Rames374ddf32016-11-04 10:40:49 +00006863 // TODO(VIXL): Ensure we record the pc position immediately after the preceding store
6864 // instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01006865 codegen_->MaybeRecordImplicitNullCheck(instruction);
6866 }
6867
6868 codegen_->MarkGCCard(temp1, temp2, array, value, instruction->GetValueCanBeNull());
6869
6870 if (done.IsReferenced()) {
6871 __ Bind(&done);
6872 }
6873
6874 if (slow_path != nullptr) {
6875 __ Bind(slow_path->GetExitLabel());
6876 }
6877
6878 break;
6879 }
6880
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006881 case DataType::Type::kInt64: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006882 Location value = locations->InAt(2);
6883 if (index.IsConstant()) {
6884 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00006885 (Int32ConstantFrom(index) << TIMES_8) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006886 GetAssembler()->StoreToOffset(kStoreWordPair, LowRegisterFrom(value), array, offset);
6887 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006888 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006889 vixl32::Register temp = temps.Acquire();
6890 __ Add(temp, array, Operand(RegisterFrom(index), vixl32::LSL, TIMES_8));
6891 GetAssembler()->StoreToOffset(kStoreWordPair, LowRegisterFrom(value), temp, data_offset);
6892 }
6893 break;
6894 }
6895
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006896 case DataType::Type::kFloat32: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006897 Location value = locations->InAt(2);
6898 DCHECK(value.IsFpuRegister());
6899 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006900 size_t offset = (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006901 GetAssembler()->StoreSToOffset(SRegisterFrom(value), array, offset);
6902 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006903 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006904 vixl32::Register temp = temps.Acquire();
6905 __ Add(temp, array, Operand(RegisterFrom(index), vixl32::LSL, TIMES_4));
6906 GetAssembler()->StoreSToOffset(SRegisterFrom(value), temp, data_offset);
6907 }
6908 break;
6909 }
6910
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006911 case DataType::Type::kFloat64: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006912 Location value = locations->InAt(2);
6913 DCHECK(value.IsFpuRegisterPair());
6914 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006915 size_t offset = (Int32ConstantFrom(index) << TIMES_8) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006916 GetAssembler()->StoreDToOffset(DRegisterFrom(value), array, offset);
6917 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006918 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006919 vixl32::Register temp = temps.Acquire();
6920 __ Add(temp, array, Operand(RegisterFrom(index), vixl32::LSL, TIMES_8));
6921 GetAssembler()->StoreDToOffset(DRegisterFrom(value), temp, data_offset);
6922 }
6923 break;
6924 }
6925
Aart Bik66c158e2018-01-31 12:55:04 -08006926 case DataType::Type::kUint32:
6927 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006928 case DataType::Type::kVoid:
Scott Wakelingc34dba72016-10-03 10:14:44 +01006929 LOG(FATAL) << "Unreachable type " << value_type;
6930 UNREACHABLE();
6931 }
6932
6933 // Objects are handled in the switch.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006934 if (value_type != DataType::Type::kReference) {
Alexandre Rames374ddf32016-11-04 10:40:49 +00006935 // TODO(VIXL): Ensure we record the pc position immediately after the preceding store
6936 // instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01006937 codegen_->MaybeRecordImplicitNullCheck(instruction);
6938 }
6939}
6940
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006941void LocationsBuilderARMVIXL::VisitArrayLength(HArrayLength* instruction) {
6942 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006943 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006944 locations->SetInAt(0, Location::RequiresRegister());
6945 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6946}
6947
6948void InstructionCodeGeneratorARMVIXL::VisitArrayLength(HArrayLength* instruction) {
6949 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
6950 vixl32::Register obj = InputRegisterAt(instruction, 0);
6951 vixl32::Register out = OutputRegister(instruction);
Alexandre Rames374ddf32016-11-04 10:40:49 +00006952 {
Artem Serov0fb37192016-12-06 18:13:40 +00006953 ExactAssemblyScope aas(GetVIXLAssembler(),
6954 vixl32::kMaxInstructionSizeInBytes,
6955 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00006956 __ ldr(out, MemOperand(obj, offset));
6957 codegen_->MaybeRecordImplicitNullCheck(instruction);
6958 }
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006959 // Mask out compression flag from String's array length.
6960 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006961 __ Lsr(out, out, 1u);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006962 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006963}
6964
Artem Serov2bbc9532016-10-21 11:51:50 +01006965void LocationsBuilderARMVIXL::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Artem Serov2bbc9532016-10-21 11:51:50 +01006966 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006967 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Artem Serov2bbc9532016-10-21 11:51:50 +01006968
6969 locations->SetInAt(0, Location::RequiresRegister());
6970 locations->SetInAt(1, Location::RegisterOrConstant(instruction->GetOffset()));
6971 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6972}
6973
6974void InstructionCodeGeneratorARMVIXL::VisitIntermediateAddress(HIntermediateAddress* instruction) {
6975 vixl32::Register out = OutputRegister(instruction);
6976 vixl32::Register first = InputRegisterAt(instruction, 0);
6977 Location second = instruction->GetLocations()->InAt(1);
6978
Artem Serov2bbc9532016-10-21 11:51:50 +01006979 if (second.IsRegister()) {
6980 __ Add(out, first, RegisterFrom(second));
6981 } else {
Anton Kirilov644032c2016-12-06 17:51:43 +00006982 __ Add(out, first, Int32ConstantFrom(second));
Artem Serov2bbc9532016-10-21 11:51:50 +01006983 }
6984}
6985
Artem Serove1811ed2017-04-27 16:50:47 +01006986void LocationsBuilderARMVIXL::VisitIntermediateAddressIndex(
6987 HIntermediateAddressIndex* instruction) {
6988 LOG(FATAL) << "Unreachable " << instruction->GetId();
6989}
6990
6991void InstructionCodeGeneratorARMVIXL::VisitIntermediateAddressIndex(
6992 HIntermediateAddressIndex* instruction) {
6993 LOG(FATAL) << "Unreachable " << instruction->GetId();
6994}
6995
Scott Wakelingc34dba72016-10-03 10:14:44 +01006996void LocationsBuilderARMVIXL::VisitBoundsCheck(HBoundsCheck* instruction) {
6997 RegisterSet caller_saves = RegisterSet::Empty();
6998 InvokeRuntimeCallingConventionARMVIXL calling_convention;
6999 caller_saves.Add(LocationFrom(calling_convention.GetRegisterAt(0)));
7000 caller_saves.Add(LocationFrom(calling_convention.GetRegisterAt(1)));
7001 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Artem Serov2dd053d2017-03-08 14:54:06 +00007002
7003 HInstruction* index = instruction->InputAt(0);
7004 HInstruction* length = instruction->InputAt(1);
7005 // If both index and length are constants we can statically check the bounds. But if at least one
7006 // of them is not encodable ArmEncodableConstantOrRegister will create
7007 // Location::RequiresRegister() which is not desired to happen. Instead we create constant
7008 // locations.
7009 bool both_const = index->IsConstant() && length->IsConstant();
7010 locations->SetInAt(0, both_const
7011 ? Location::ConstantLocation(index->AsConstant())
7012 : ArmEncodableConstantOrRegister(index, CMP));
7013 locations->SetInAt(1, both_const
7014 ? Location::ConstantLocation(length->AsConstant())
7015 : ArmEncodableConstantOrRegister(length, CMP));
Scott Wakelingc34dba72016-10-03 10:14:44 +01007016}
7017
7018void InstructionCodeGeneratorARMVIXL::VisitBoundsCheck(HBoundsCheck* instruction) {
Artem Serov2dd053d2017-03-08 14:54:06 +00007019 LocationSummary* locations = instruction->GetLocations();
7020 Location index_loc = locations->InAt(0);
7021 Location length_loc = locations->InAt(1);
Scott Wakelingc34dba72016-10-03 10:14:44 +01007022
Artem Serov2dd053d2017-03-08 14:54:06 +00007023 if (length_loc.IsConstant()) {
7024 int32_t length = Int32ConstantFrom(length_loc);
7025 if (index_loc.IsConstant()) {
7026 // BCE will remove the bounds check if we are guaranteed to pass.
7027 int32_t index = Int32ConstantFrom(index_loc);
7028 if (index < 0 || index >= length) {
7029 SlowPathCodeARMVIXL* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01007030 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathARMVIXL(instruction);
Artem Serov2dd053d2017-03-08 14:54:06 +00007031 codegen_->AddSlowPath(slow_path);
7032 __ B(slow_path->GetEntryLabel());
7033 } else {
7034 // Some optimization after BCE may have generated this, and we should not
7035 // generate a bounds check if it is a valid range.
7036 }
7037 return;
7038 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01007039
Artem Serov2dd053d2017-03-08 14:54:06 +00007040 SlowPathCodeARMVIXL* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01007041 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathARMVIXL(instruction);
Artem Serov2dd053d2017-03-08 14:54:06 +00007042 __ Cmp(RegisterFrom(index_loc), length);
7043 codegen_->AddSlowPath(slow_path);
7044 __ B(hs, slow_path->GetEntryLabel());
7045 } else {
7046 SlowPathCodeARMVIXL* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01007047 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathARMVIXL(instruction);
Artem Serov2dd053d2017-03-08 14:54:06 +00007048 __ Cmp(RegisterFrom(length_loc), InputOperandAt(instruction, 0));
7049 codegen_->AddSlowPath(slow_path);
7050 __ B(ls, slow_path->GetEntryLabel());
7051 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01007052}
7053
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007054void CodeGeneratorARMVIXL::MarkGCCard(vixl32::Register temp,
7055 vixl32::Register card,
7056 vixl32::Register object,
7057 vixl32::Register value,
7058 bool can_be_null) {
7059 vixl32::Label is_null;
7060 if (can_be_null) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00007061 __ CompareAndBranchIfZero(value, &is_null);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007062 }
7063 GetAssembler()->LoadFromOffset(
7064 kLoadWord, card, tr, Thread::CardTableOffset<kArmPointerSize>().Int32Value());
Scott Wakelingb77051e2016-11-21 19:46:00 +00007065 __ Lsr(temp, object, Operand::From(gc::accounting::CardTable::kCardShift));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007066 __ Strb(card, MemOperand(card, temp));
7067 if (can_be_null) {
7068 __ Bind(&is_null);
7069 }
7070}
7071
Scott Wakelingfe885462016-09-22 10:24:38 +01007072void LocationsBuilderARMVIXL::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
7073 LOG(FATAL) << "Unreachable";
7074}
7075
7076void InstructionCodeGeneratorARMVIXL::VisitParallelMove(HParallelMove* instruction) {
Vladimir Markobea75ff2017-10-11 20:39:54 +01007077 if (instruction->GetNext()->IsSuspendCheck() &&
7078 instruction->GetBlock()->GetLoopInformation() != nullptr) {
7079 HSuspendCheck* suspend_check = instruction->GetNext()->AsSuspendCheck();
7080 // The back edge will generate the suspend check.
7081 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(suspend_check, instruction);
7082 }
7083
Scott Wakelingfe885462016-09-22 10:24:38 +01007084 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
7085}
7086
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007087void LocationsBuilderARMVIXL::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007088 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
7089 instruction, LocationSummary::kCallOnSlowPath);
Artem Serov657022c2016-11-23 14:19:38 +00007090 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007091}
7092
7093void InstructionCodeGeneratorARMVIXL::VisitSuspendCheck(HSuspendCheck* instruction) {
7094 HBasicBlock* block = instruction->GetBlock();
7095 if (block->GetLoopInformation() != nullptr) {
7096 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
7097 // The back edge will generate the suspend check.
7098 return;
7099 }
7100 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
7101 // The goto will generate the suspend check.
7102 return;
7103 }
7104 GenerateSuspendCheck(instruction, nullptr);
Roland Levillain5daa4952017-07-03 17:23:56 +01007105 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 12);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007106}
7107
7108void InstructionCodeGeneratorARMVIXL::GenerateSuspendCheck(HSuspendCheck* instruction,
7109 HBasicBlock* successor) {
7110 SuspendCheckSlowPathARMVIXL* slow_path =
7111 down_cast<SuspendCheckSlowPathARMVIXL*>(instruction->GetSlowPath());
7112 if (slow_path == nullptr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007113 slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01007114 new (codegen_->GetScopedAllocator()) SuspendCheckSlowPathARMVIXL(instruction, successor);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007115 instruction->SetSlowPath(slow_path);
7116 codegen_->AddSlowPath(slow_path);
7117 if (successor != nullptr) {
7118 DCHECK(successor->IsLoopHeader());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007119 }
7120 } else {
7121 DCHECK_EQ(slow_path->GetSuccessor(), successor);
7122 }
7123
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007124 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007125 vixl32::Register temp = temps.Acquire();
7126 GetAssembler()->LoadFromOffset(
7127 kLoadUnsignedHalfword, temp, tr, Thread::ThreadFlagsOffset<kArmPointerSize>().Int32Value());
7128 if (successor == nullptr) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00007129 __ CompareAndBranchIfNonZero(temp, slow_path->GetEntryLabel());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007130 __ Bind(slow_path->GetReturnLabel());
7131 } else {
xueliang.zhongf51bc622016-11-04 09:23:32 +00007132 __ CompareAndBranchIfZero(temp, codegen_->GetLabelOf(successor));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007133 __ B(slow_path->GetEntryLabel());
7134 }
7135}
7136
Scott Wakelingfe885462016-09-22 10:24:38 +01007137ArmVIXLAssembler* ParallelMoveResolverARMVIXL::GetAssembler() const {
7138 return codegen_->GetAssembler();
7139}
7140
7141void ParallelMoveResolverARMVIXL::EmitMove(size_t index) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007142 UseScratchRegisterScope temps(GetAssembler()->GetVIXLAssembler());
Scott Wakelingfe885462016-09-22 10:24:38 +01007143 MoveOperands* move = moves_[index];
7144 Location source = move->GetSource();
7145 Location destination = move->GetDestination();
7146
7147 if (source.IsRegister()) {
7148 if (destination.IsRegister()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007149 __ Mov(RegisterFrom(destination), RegisterFrom(source));
Scott Wakelingfe885462016-09-22 10:24:38 +01007150 } else if (destination.IsFpuRegister()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007151 __ Vmov(SRegisterFrom(destination), RegisterFrom(source));
Scott Wakelingfe885462016-09-22 10:24:38 +01007152 } else {
7153 DCHECK(destination.IsStackSlot());
7154 GetAssembler()->StoreToOffset(kStoreWord,
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007155 RegisterFrom(source),
Scott Wakelingfe885462016-09-22 10:24:38 +01007156 sp,
7157 destination.GetStackIndex());
7158 }
7159 } else if (source.IsStackSlot()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007160 if (destination.IsRegister()) {
7161 GetAssembler()->LoadFromOffset(kLoadWord,
7162 RegisterFrom(destination),
7163 sp,
7164 source.GetStackIndex());
7165 } else if (destination.IsFpuRegister()) {
7166 GetAssembler()->LoadSFromOffset(SRegisterFrom(destination), sp, source.GetStackIndex());
7167 } else {
7168 DCHECK(destination.IsStackSlot());
7169 vixl32::Register temp = temps.Acquire();
7170 GetAssembler()->LoadFromOffset(kLoadWord, temp, sp, source.GetStackIndex());
7171 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
7172 }
Scott Wakelingfe885462016-09-22 10:24:38 +01007173 } else if (source.IsFpuRegister()) {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01007174 if (destination.IsRegister()) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01007175 __ Vmov(RegisterFrom(destination), SRegisterFrom(source));
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01007176 } else if (destination.IsFpuRegister()) {
7177 __ Vmov(SRegisterFrom(destination), SRegisterFrom(source));
7178 } else {
7179 DCHECK(destination.IsStackSlot());
7180 GetAssembler()->StoreSToOffset(SRegisterFrom(source), sp, destination.GetStackIndex());
7181 }
Scott Wakelingfe885462016-09-22 10:24:38 +01007182 } else if (source.IsDoubleStackSlot()) {
Alexandre Rames9c19bd62016-10-24 11:50:32 +01007183 if (destination.IsDoubleStackSlot()) {
7184 vixl32::DRegister temp = temps.AcquireD();
7185 GetAssembler()->LoadDFromOffset(temp, sp, source.GetStackIndex());
7186 GetAssembler()->StoreDToOffset(temp, sp, destination.GetStackIndex());
7187 } else if (destination.IsRegisterPair()) {
7188 DCHECK(ExpectedPairLayout(destination));
7189 GetAssembler()->LoadFromOffset(
7190 kLoadWordPair, LowRegisterFrom(destination), sp, source.GetStackIndex());
7191 } else {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01007192 DCHECK(destination.IsFpuRegisterPair()) << destination;
7193 GetAssembler()->LoadDFromOffset(DRegisterFrom(destination), sp, source.GetStackIndex());
Alexandre Rames9c19bd62016-10-24 11:50:32 +01007194 }
Scott Wakelingfe885462016-09-22 10:24:38 +01007195 } else if (source.IsRegisterPair()) {
7196 if (destination.IsRegisterPair()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007197 __ Mov(LowRegisterFrom(destination), LowRegisterFrom(source));
7198 __ Mov(HighRegisterFrom(destination), HighRegisterFrom(source));
Scott Wakelingfe885462016-09-22 10:24:38 +01007199 } else if (destination.IsFpuRegisterPair()) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01007200 __ Vmov(DRegisterFrom(destination), LowRegisterFrom(source), HighRegisterFrom(source));
Scott Wakelingfe885462016-09-22 10:24:38 +01007201 } else {
7202 DCHECK(destination.IsDoubleStackSlot()) << destination;
7203 DCHECK(ExpectedPairLayout(source));
7204 GetAssembler()->StoreToOffset(kStoreWordPair,
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007205 LowRegisterFrom(source),
Scott Wakelingfe885462016-09-22 10:24:38 +01007206 sp,
7207 destination.GetStackIndex());
7208 }
7209 } else if (source.IsFpuRegisterPair()) {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01007210 if (destination.IsRegisterPair()) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01007211 __ Vmov(LowRegisterFrom(destination), HighRegisterFrom(destination), DRegisterFrom(source));
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01007212 } else if (destination.IsFpuRegisterPair()) {
7213 __ Vmov(DRegisterFrom(destination), DRegisterFrom(source));
7214 } else {
7215 DCHECK(destination.IsDoubleStackSlot()) << destination;
7216 GetAssembler()->StoreDToOffset(DRegisterFrom(source), sp, destination.GetStackIndex());
7217 }
Scott Wakelingfe885462016-09-22 10:24:38 +01007218 } else {
7219 DCHECK(source.IsConstant()) << source;
7220 HConstant* constant = source.GetConstant();
7221 if (constant->IsIntConstant() || constant->IsNullConstant()) {
7222 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
7223 if (destination.IsRegister()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007224 __ Mov(RegisterFrom(destination), value);
Scott Wakelingfe885462016-09-22 10:24:38 +01007225 } else {
7226 DCHECK(destination.IsStackSlot());
Scott Wakelingfe885462016-09-22 10:24:38 +01007227 vixl32::Register temp = temps.Acquire();
7228 __ Mov(temp, value);
7229 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
7230 }
7231 } else if (constant->IsLongConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00007232 int64_t value = Int64ConstantFrom(source);
Scott Wakelingfe885462016-09-22 10:24:38 +01007233 if (destination.IsRegisterPair()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007234 __ Mov(LowRegisterFrom(destination), Low32Bits(value));
7235 __ Mov(HighRegisterFrom(destination), High32Bits(value));
Scott Wakelingfe885462016-09-22 10:24:38 +01007236 } else {
7237 DCHECK(destination.IsDoubleStackSlot()) << destination;
Scott Wakelingfe885462016-09-22 10:24:38 +01007238 vixl32::Register temp = temps.Acquire();
7239 __ Mov(temp, Low32Bits(value));
7240 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
7241 __ Mov(temp, High32Bits(value));
7242 GetAssembler()->StoreToOffset(kStoreWord,
7243 temp,
7244 sp,
7245 destination.GetHighStackIndex(kArmWordSize));
7246 }
7247 } else if (constant->IsDoubleConstant()) {
7248 double value = constant->AsDoubleConstant()->GetValue();
7249 if (destination.IsFpuRegisterPair()) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01007250 __ Vmov(DRegisterFrom(destination), value);
Scott Wakelingfe885462016-09-22 10:24:38 +01007251 } else {
7252 DCHECK(destination.IsDoubleStackSlot()) << destination;
7253 uint64_t int_value = bit_cast<uint64_t, double>(value);
Scott Wakelingfe885462016-09-22 10:24:38 +01007254 vixl32::Register temp = temps.Acquire();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007255 __ Mov(temp, Low32Bits(int_value));
Scott Wakelingfe885462016-09-22 10:24:38 +01007256 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007257 __ Mov(temp, High32Bits(int_value));
Scott Wakelingfe885462016-09-22 10:24:38 +01007258 GetAssembler()->StoreToOffset(kStoreWord,
7259 temp,
7260 sp,
7261 destination.GetHighStackIndex(kArmWordSize));
7262 }
7263 } else {
7264 DCHECK(constant->IsFloatConstant()) << constant->DebugName();
7265 float value = constant->AsFloatConstant()->GetValue();
7266 if (destination.IsFpuRegister()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007267 __ Vmov(SRegisterFrom(destination), value);
Scott Wakelingfe885462016-09-22 10:24:38 +01007268 } else {
7269 DCHECK(destination.IsStackSlot());
Scott Wakelingfe885462016-09-22 10:24:38 +01007270 vixl32::Register temp = temps.Acquire();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007271 __ Mov(temp, bit_cast<int32_t, float>(value));
Scott Wakelingfe885462016-09-22 10:24:38 +01007272 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
7273 }
7274 }
7275 }
7276}
7277
Alexandre Rames9c19bd62016-10-24 11:50:32 +01007278void ParallelMoveResolverARMVIXL::Exchange(vixl32::Register reg, int mem) {
7279 UseScratchRegisterScope temps(GetAssembler()->GetVIXLAssembler());
7280 vixl32::Register temp = temps.Acquire();
7281 __ Mov(temp, reg);
7282 GetAssembler()->LoadFromOffset(kLoadWord, reg, sp, mem);
7283 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, mem);
Scott Wakelingfe885462016-09-22 10:24:38 +01007284}
7285
Alexandre Rames9c19bd62016-10-24 11:50:32 +01007286void ParallelMoveResolverARMVIXL::Exchange(int mem1, int mem2) {
7287 // TODO(VIXL32): Double check the performance of this implementation.
7288 UseScratchRegisterScope temps(GetAssembler()->GetVIXLAssembler());
Nicolas Geoffray13a797b2017-03-15 16:41:31 +00007289 vixl32::Register temp1 = temps.Acquire();
7290 ScratchRegisterScope ensure_scratch(
7291 this, temp1.GetCode(), r0.GetCode(), codegen_->GetNumberOfCoreRegisters());
7292 vixl32::Register temp2(ensure_scratch.GetRegister());
Alexandre Rames9c19bd62016-10-24 11:50:32 +01007293
Nicolas Geoffray13a797b2017-03-15 16:41:31 +00007294 int stack_offset = ensure_scratch.IsSpilled() ? kArmWordSize : 0;
7295 GetAssembler()->LoadFromOffset(kLoadWord, temp1, sp, mem1 + stack_offset);
7296 GetAssembler()->LoadFromOffset(kLoadWord, temp2, sp, mem2 + stack_offset);
7297 GetAssembler()->StoreToOffset(kStoreWord, temp1, sp, mem2 + stack_offset);
7298 GetAssembler()->StoreToOffset(kStoreWord, temp2, sp, mem1 + stack_offset);
Scott Wakelingfe885462016-09-22 10:24:38 +01007299}
7300
Alexandre Rames9c19bd62016-10-24 11:50:32 +01007301void ParallelMoveResolverARMVIXL::EmitSwap(size_t index) {
7302 MoveOperands* move = moves_[index];
7303 Location source = move->GetSource();
7304 Location destination = move->GetDestination();
7305 UseScratchRegisterScope temps(GetAssembler()->GetVIXLAssembler());
7306
7307 if (source.IsRegister() && destination.IsRegister()) {
7308 vixl32::Register temp = temps.Acquire();
7309 DCHECK(!RegisterFrom(source).Is(temp));
7310 DCHECK(!RegisterFrom(destination).Is(temp));
7311 __ Mov(temp, RegisterFrom(destination));
7312 __ Mov(RegisterFrom(destination), RegisterFrom(source));
7313 __ Mov(RegisterFrom(source), temp);
7314 } else if (source.IsRegister() && destination.IsStackSlot()) {
7315 Exchange(RegisterFrom(source), destination.GetStackIndex());
7316 } else if (source.IsStackSlot() && destination.IsRegister()) {
7317 Exchange(RegisterFrom(destination), source.GetStackIndex());
7318 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Anton Kirilovdda43962016-11-21 19:55:20 +00007319 Exchange(source.GetStackIndex(), destination.GetStackIndex());
Alexandre Rames9c19bd62016-10-24 11:50:32 +01007320 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Nicolas Geoffray13a797b2017-03-15 16:41:31 +00007321 vixl32::Register temp = temps.Acquire();
Anton Kirilovdda43962016-11-21 19:55:20 +00007322 __ Vmov(temp, SRegisterFrom(source));
7323 __ Vmov(SRegisterFrom(source), SRegisterFrom(destination));
7324 __ Vmov(SRegisterFrom(destination), temp);
Alexandre Rames9c19bd62016-10-24 11:50:32 +01007325 } else if (source.IsRegisterPair() && destination.IsRegisterPair()) {
7326 vixl32::DRegister temp = temps.AcquireD();
7327 __ Vmov(temp, LowRegisterFrom(source), HighRegisterFrom(source));
7328 __ Mov(LowRegisterFrom(source), LowRegisterFrom(destination));
7329 __ Mov(HighRegisterFrom(source), HighRegisterFrom(destination));
7330 __ Vmov(LowRegisterFrom(destination), HighRegisterFrom(destination), temp);
7331 } else if (source.IsRegisterPair() || destination.IsRegisterPair()) {
7332 vixl32::Register low_reg = LowRegisterFrom(source.IsRegisterPair() ? source : destination);
7333 int mem = source.IsRegisterPair() ? destination.GetStackIndex() : source.GetStackIndex();
7334 DCHECK(ExpectedPairLayout(source.IsRegisterPair() ? source : destination));
7335 vixl32::DRegister temp = temps.AcquireD();
7336 __ Vmov(temp, low_reg, vixl32::Register(low_reg.GetCode() + 1));
7337 GetAssembler()->LoadFromOffset(kLoadWordPair, low_reg, sp, mem);
7338 GetAssembler()->StoreDToOffset(temp, sp, mem);
7339 } else if (source.IsFpuRegisterPair() && destination.IsFpuRegisterPair()) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007340 vixl32::DRegister first = DRegisterFrom(source);
7341 vixl32::DRegister second = DRegisterFrom(destination);
7342 vixl32::DRegister temp = temps.AcquireD();
7343 __ Vmov(temp, first);
7344 __ Vmov(first, second);
7345 __ Vmov(second, temp);
Alexandre Rames9c19bd62016-10-24 11:50:32 +01007346 } else if (source.IsFpuRegisterPair() || destination.IsFpuRegisterPair()) {
Anton Kirilovdda43962016-11-21 19:55:20 +00007347 vixl32::DRegister reg = source.IsFpuRegisterPair()
7348 ? DRegisterFrom(source)
7349 : DRegisterFrom(destination);
7350 int mem = source.IsFpuRegisterPair()
7351 ? destination.GetStackIndex()
7352 : source.GetStackIndex();
7353 vixl32::DRegister temp = temps.AcquireD();
7354 __ Vmov(temp, reg);
7355 GetAssembler()->LoadDFromOffset(reg, sp, mem);
7356 GetAssembler()->StoreDToOffset(temp, sp, mem);
Alexandre Rames9c19bd62016-10-24 11:50:32 +01007357 } else if (source.IsFpuRegister() || destination.IsFpuRegister()) {
Anton Kirilovdda43962016-11-21 19:55:20 +00007358 vixl32::SRegister reg = source.IsFpuRegister()
7359 ? SRegisterFrom(source)
7360 : SRegisterFrom(destination);
7361 int mem = source.IsFpuRegister()
7362 ? destination.GetStackIndex()
7363 : source.GetStackIndex();
7364 vixl32::Register temp = temps.Acquire();
7365 __ Vmov(temp, reg);
7366 GetAssembler()->LoadSFromOffset(reg, sp, mem);
7367 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, mem);
Alexandre Rames9c19bd62016-10-24 11:50:32 +01007368 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
7369 vixl32::DRegister temp1 = temps.AcquireD();
7370 vixl32::DRegister temp2 = temps.AcquireD();
7371 __ Vldr(temp1, MemOperand(sp, source.GetStackIndex()));
7372 __ Vldr(temp2, MemOperand(sp, destination.GetStackIndex()));
7373 __ Vstr(temp1, MemOperand(sp, destination.GetStackIndex()));
7374 __ Vstr(temp2, MemOperand(sp, source.GetStackIndex()));
7375 } else {
7376 LOG(FATAL) << "Unimplemented" << source << " <-> " << destination;
7377 }
Scott Wakelingfe885462016-09-22 10:24:38 +01007378}
7379
Nicolas Geoffray13a797b2017-03-15 16:41:31 +00007380void ParallelMoveResolverARMVIXL::SpillScratch(int reg) {
7381 __ Push(vixl32::Register(reg));
Scott Wakelingfe885462016-09-22 10:24:38 +01007382}
7383
Nicolas Geoffray13a797b2017-03-15 16:41:31 +00007384void ParallelMoveResolverARMVIXL::RestoreScratch(int reg) {
7385 __ Pop(vixl32::Register(reg));
Scott Wakelingfe885462016-09-22 10:24:38 +01007386}
7387
Artem Serov02d37832016-10-25 15:25:33 +01007388HLoadClass::LoadKind CodeGeneratorARMVIXL::GetSupportedLoadClassKind(
Artem Serovd4cc5b22016-11-04 11:19:09 +00007389 HLoadClass::LoadKind desired_class_load_kind) {
7390 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00007391 case HLoadClass::LoadKind::kInvalid:
7392 LOG(FATAL) << "UNREACHABLE";
7393 UNREACHABLE();
Artem Serovd4cc5b22016-11-04 11:19:09 +00007394 case HLoadClass::LoadKind::kReferrersClass:
7395 break;
Artem Serovd4cc5b22016-11-04 11:19:09 +00007396 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00007397 case HLoadClass::LoadKind::kBootImageRelRo:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007398 case HLoadClass::LoadKind::kBssEntry:
7399 DCHECK(!Runtime::Current()->UseJitCompilation());
7400 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007401 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007402 DCHECK(Runtime::Current()->UseJitCompilation());
Artem Serovc5fcb442016-12-02 19:19:58 +00007403 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01007404 case HLoadClass::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007405 case HLoadClass::LoadKind::kRuntimeCall:
Artem Serovd4cc5b22016-11-04 11:19:09 +00007406 break;
7407 }
7408 return desired_class_load_kind;
Artem Serov02d37832016-10-25 15:25:33 +01007409}
7410
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007411void LocationsBuilderARMVIXL::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00007412 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007413 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007414 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00007415 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007416 cls,
7417 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00007418 LocationFrom(r0));
Vladimir Markoea4c1262017-02-06 19:59:33 +00007419 DCHECK(calling_convention.GetRegisterAt(0).Is(r0));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007420 return;
7421 }
Vladimir Marko41559982017-01-06 14:04:23 +00007422 DCHECK(!cls->NeedsAccessCheck());
Scott Wakelingfe885462016-09-22 10:24:38 +01007423
Artem Serovd4cc5b22016-11-04 11:19:09 +00007424 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
7425 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007426 ? LocationSummary::kCallOnSlowPath
7427 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01007428 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(cls, call_kind);
Artem Serovd4cc5b22016-11-04 11:19:09 +00007429 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007430 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Artem Serovd4cc5b22016-11-04 11:19:09 +00007431 }
7432
Vladimir Marko41559982017-01-06 14:04:23 +00007433 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007434 locations->SetInAt(0, Location::RequiresRegister());
7435 }
7436 locations->SetOut(Location::RequiresRegister());
Vladimir Markoea4c1262017-02-06 19:59:33 +00007437 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
7438 if (!kUseReadBarrier || kUseBakerReadBarrier) {
7439 // Rely on the type resolution or initialization and marking to save everything we need.
Vladimir Markoea4c1262017-02-06 19:59:33 +00007440 RegisterSet caller_saves = RegisterSet::Empty();
7441 InvokeRuntimeCallingConventionARMVIXL calling_convention;
7442 caller_saves.Add(LocationFrom(calling_convention.GetRegisterAt(0)));
7443 // TODO: Add GetReturnLocation() to the calling convention so that we can DCHECK()
7444 // that the the kPrimNot result register is the same as the first argument register.
7445 locations->SetCustomSlowPathCallerSaves(caller_saves);
7446 } else {
7447 // For non-Baker read barrier we have a temp-clobbering call.
7448 }
7449 }
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01007450 if (kUseBakerReadBarrier && kBakerReadBarrierLinkTimeThunksEnableForGcRoots) {
7451 if (load_kind == HLoadClass::LoadKind::kBssEntry ||
7452 (load_kind == HLoadClass::LoadKind::kReferrersClass &&
7453 !Runtime::Current()->UseJitCompilation())) {
7454 locations->AddTemp(Location::RegisterLocation(kBakerCcEntrypointRegister.GetCode()));
7455 }
7456 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007457}
7458
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007459// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
7460// move.
7461void InstructionCodeGeneratorARMVIXL::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00007462 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007463 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00007464 codegen_->GenerateLoadClassRuntimeCall(cls);
Roland Levillain5daa4952017-07-03 17:23:56 +01007465 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 13);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007466 return;
7467 }
Vladimir Marko41559982017-01-06 14:04:23 +00007468 DCHECK(!cls->NeedsAccessCheck());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007469
Vladimir Marko41559982017-01-06 14:04:23 +00007470 LocationSummary* locations = cls->GetLocations();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007471 Location out_loc = locations->Out();
7472 vixl32::Register out = OutputRegister(cls);
7473
Artem Serovd4cc5b22016-11-04 11:19:09 +00007474 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
7475 ? kWithoutReadBarrier
7476 : kCompilerReadBarrierOption;
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007477 bool generate_null_check = false;
Vladimir Marko41559982017-01-06 14:04:23 +00007478 switch (load_kind) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007479 case HLoadClass::LoadKind::kReferrersClass: {
7480 DCHECK(!cls->CanCallRuntime());
7481 DCHECK(!cls->MustGenerateClinitCheck());
7482 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
7483 vixl32::Register current_method = InputRegisterAt(cls, 0);
Vladimir Markoca1e0382018-04-11 09:58:41 +00007484 codegen_->GenerateGcRootFieldLoad(cls,
7485 out_loc,
7486 current_method,
7487 ArtMethod::DeclaringClassOffset().Int32Value(),
7488 read_barrier_option);
Artem Serovd4cc5b22016-11-04 11:19:09 +00007489 break;
7490 }
Artem Serovd4cc5b22016-11-04 11:19:09 +00007491 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007492 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Artem Serovd4cc5b22016-11-04 11:19:09 +00007493 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
7494 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00007495 codegen_->NewBootImageTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
Artem Serovd4cc5b22016-11-04 11:19:09 +00007496 codegen_->EmitMovwMovtPlaceholder(labels, out);
7497 break;
7498 }
7499 case HLoadClass::LoadKind::kBootImageAddress: {
Artem Serovc5fcb442016-12-02 19:19:58 +00007500 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007501 uint32_t address = dchecked_integral_cast<uint32_t>(
7502 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
7503 DCHECK_NE(address, 0u);
Artem Serovc5fcb442016-12-02 19:19:58 +00007504 __ Ldr(out, codegen_->DeduplicateBootImageAddressLiteral(address));
Artem Serovd4cc5b22016-11-04 11:19:09 +00007505 break;
7506 }
Vladimir Markoe47f60c2018-02-21 13:43:28 +00007507 case HLoadClass::LoadKind::kBootImageRelRo: {
Vladimir Marko94ec2db2017-09-06 17:21:03 +01007508 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
7509 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
Vladimir Markoe47f60c2018-02-21 13:43:28 +00007510 codegen_->NewBootImageRelRoPatch(codegen_->GetBootImageOffset(cls));
Vladimir Marko94ec2db2017-09-06 17:21:03 +01007511 codegen_->EmitMovwMovtPlaceholder(labels, out);
7512 __ Ldr(out, MemOperand(out, /* offset */ 0));
Vladimir Marko94ec2db2017-09-06 17:21:03 +01007513 break;
7514 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007515 case HLoadClass::LoadKind::kBssEntry: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007516 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
Vladimir Marko1998cd02017-01-13 13:02:58 +00007517 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex());
Vladimir Markof3c52b42017-11-17 17:32:12 +00007518 codegen_->EmitMovwMovtPlaceholder(labels, out);
Vladimir Markoca1e0382018-04-11 09:58:41 +00007519 codegen_->GenerateGcRootFieldLoad(cls, out_loc, out, /* offset */ 0, read_barrier_option);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007520 generate_null_check = true;
7521 break;
7522 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007523 case HLoadClass::LoadKind::kJitTableAddress: {
Artem Serovc5fcb442016-12-02 19:19:58 +00007524 __ Ldr(out, codegen_->DeduplicateJitClassLiteral(cls->GetDexFile(),
7525 cls->GetTypeIndex(),
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007526 cls->GetClass()));
Artem Serovc5fcb442016-12-02 19:19:58 +00007527 // /* GcRoot<mirror::Class> */ out = *out
Vladimir Markoca1e0382018-04-11 09:58:41 +00007528 codegen_->GenerateGcRootFieldLoad(cls, out_loc, out, /* offset */ 0, read_barrier_option);
Artem Serovd4cc5b22016-11-04 11:19:09 +00007529 break;
7530 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007531 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00007532 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00007533 LOG(FATAL) << "UNREACHABLE";
7534 UNREACHABLE();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007535 }
7536
7537 if (generate_null_check || cls->MustGenerateClinitCheck()) {
7538 DCHECK(cls->CanCallRuntime());
Vladimir Marko174b2e22017-10-12 13:34:49 +01007539 LoadClassSlowPathARMVIXL* slow_path =
7540 new (codegen_->GetScopedAllocator()) LoadClassSlowPathARMVIXL(
7541 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007542 codegen_->AddSlowPath(slow_path);
7543 if (generate_null_check) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00007544 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007545 }
7546 if (cls->MustGenerateClinitCheck()) {
7547 GenerateClassInitializationCheck(slow_path, out);
7548 } else {
7549 __ Bind(slow_path->GetExitLabel());
7550 }
Roland Levillain5daa4952017-07-03 17:23:56 +01007551 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 14);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007552 }
7553}
7554
Artem Serov02d37832016-10-25 15:25:33 +01007555void LocationsBuilderARMVIXL::VisitClinitCheck(HClinitCheck* check) {
7556 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007557 new (GetGraph()->GetAllocator()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
Artem Serov02d37832016-10-25 15:25:33 +01007558 locations->SetInAt(0, Location::RequiresRegister());
7559 if (check->HasUses()) {
7560 locations->SetOut(Location::SameAsFirstInput());
7561 }
7562}
7563
7564void InstructionCodeGeneratorARMVIXL::VisitClinitCheck(HClinitCheck* check) {
7565 // We assume the class is not null.
7566 LoadClassSlowPathARMVIXL* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01007567 new (codegen_->GetScopedAllocator()) LoadClassSlowPathARMVIXL(check->GetLoadClass(),
7568 check,
7569 check->GetDexPc(),
7570 /* do_clinit */ true);
Artem Serov02d37832016-10-25 15:25:33 +01007571 codegen_->AddSlowPath(slow_path);
7572 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
7573}
7574
7575void InstructionCodeGeneratorARMVIXL::GenerateClassInitializationCheck(
7576 LoadClassSlowPathARMVIXL* slow_path, vixl32::Register class_reg) {
7577 UseScratchRegisterScope temps(GetVIXLAssembler());
7578 vixl32::Register temp = temps.Acquire();
Vladimir Markodc682aa2018-01-04 18:42:57 +00007579 constexpr size_t status_lsb_position = SubtypeCheckBits::BitStructSizeOf();
7580 const size_t status_byte_offset =
7581 mirror::Class::StatusOffset().SizeValue() + (status_lsb_position / kBitsPerByte);
7582 constexpr uint32_t shifted_initialized_value =
7583 enum_cast<uint32_t>(ClassStatus::kInitialized) << (status_lsb_position % kBitsPerByte);
7584
7585 GetAssembler()->LoadFromOffset(kLoadUnsignedByte, temp, class_reg, status_byte_offset);
7586 __ Cmp(temp, shifted_initialized_value);
Vladimir Marko2c64a832018-01-04 11:31:56 +00007587 __ B(lo, slow_path->GetEntryLabel());
Artem Serov02d37832016-10-25 15:25:33 +01007588 // Even if the initialized flag is set, we may be in a situation where caches are not synced
7589 // properly. Therefore, we do a memory fence.
7590 __ Dmb(ISH);
7591 __ Bind(slow_path->GetExitLabel());
7592}
7593
Vladimir Marko175e7862018-03-27 09:03:13 +00007594void InstructionCodeGeneratorARMVIXL::GenerateBitstringTypeCheckCompare(
7595 HTypeCheckInstruction* check,
7596 vixl32::Register temp,
7597 vixl32::FlagsUpdate flags_update) {
7598 uint32_t path_to_root = check->GetBitstringPathToRoot();
7599 uint32_t mask = check->GetBitstringMask();
7600 DCHECK(IsPowerOfTwo(mask + 1));
7601 size_t mask_bits = WhichPowerOf2(mask + 1);
7602
7603 // Note that HInstanceOf shall check for zero value in `temp` but HCheckCast needs
7604 // the Z flag for BNE. This is indicated by the `flags_update` parameter.
7605 if (mask_bits == 16u) {
7606 // Load only the bitstring part of the status word.
7607 __ Ldrh(temp, MemOperand(temp, mirror::Class::StatusOffset().Int32Value()));
7608 // Check if the bitstring bits are equal to `path_to_root`.
7609 if (flags_update == SetFlags) {
7610 __ Cmp(temp, path_to_root);
7611 } else {
7612 __ Sub(temp, temp, path_to_root);
7613 }
7614 } else {
7615 // /* uint32_t */ temp = temp->status_
7616 __ Ldr(temp, MemOperand(temp, mirror::Class::StatusOffset().Int32Value()));
7617 if (GetAssembler()->ShifterOperandCanHold(SUB, path_to_root)) {
7618 // Compare the bitstring bits using SUB.
7619 __ Sub(temp, temp, path_to_root);
7620 // Shift out bits that do not contribute to the comparison.
7621 __ Lsl(flags_update, temp, temp, dchecked_integral_cast<uint32_t>(32u - mask_bits));
7622 } else if (IsUint<16>(path_to_root)) {
7623 if (temp.IsLow()) {
7624 // Note: Optimized for size but contains one more dependent instruction than necessary.
7625 // MOVW+SUB(register) would be 8 bytes unless we find a low-reg temporary but the
7626 // macro assembler would use the high reg IP for the constant by default.
7627 // Compare the bitstring bits using SUB.
7628 __ Sub(temp, temp, path_to_root & 0x00ffu); // 16-bit SUB (immediate) T2
7629 __ Sub(temp, temp, path_to_root & 0xff00u); // 32-bit SUB (immediate) T3
7630 // Shift out bits that do not contribute to the comparison.
7631 __ Lsl(flags_update, temp, temp, dchecked_integral_cast<uint32_t>(32u - mask_bits));
7632 } else {
7633 // Extract the bitstring bits.
7634 __ Ubfx(temp, temp, 0, mask_bits);
7635 // Check if the bitstring bits are equal to `path_to_root`.
7636 if (flags_update == SetFlags) {
7637 __ Cmp(temp, path_to_root);
7638 } else {
7639 __ Sub(temp, temp, path_to_root);
7640 }
7641 }
7642 } else {
7643 // Shift out bits that do not contribute to the comparison.
7644 __ Lsl(temp, temp, dchecked_integral_cast<uint32_t>(32u - mask_bits));
7645 // Check if the shifted bitstring bits are equal to `path_to_root << (32u - mask_bits)`.
7646 if (flags_update == SetFlags) {
7647 __ Cmp(temp, path_to_root << (32u - mask_bits));
7648 } else {
7649 __ Sub(temp, temp, path_to_root << (32u - mask_bits));
7650 }
7651 }
7652 }
7653}
7654
Artem Serov02d37832016-10-25 15:25:33 +01007655HLoadString::LoadKind CodeGeneratorARMVIXL::GetSupportedLoadStringKind(
Artem Serovd4cc5b22016-11-04 11:19:09 +00007656 HLoadString::LoadKind desired_string_load_kind) {
7657 switch (desired_string_load_kind) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00007658 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00007659 case HLoadString::LoadKind::kBootImageRelRo:
Artem Serovd4cc5b22016-11-04 11:19:09 +00007660 case HLoadString::LoadKind::kBssEntry:
7661 DCHECK(!Runtime::Current()->UseJitCompilation());
7662 break;
7663 case HLoadString::LoadKind::kJitTableAddress:
7664 DCHECK(Runtime::Current()->UseJitCompilation());
Artem Serovc5fcb442016-12-02 19:19:58 +00007665 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01007666 case HLoadString::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007667 case HLoadString::LoadKind::kRuntimeCall:
Artem Serovd4cc5b22016-11-04 11:19:09 +00007668 break;
7669 }
7670 return desired_string_load_kind;
Artem Serov02d37832016-10-25 15:25:33 +01007671}
7672
7673void LocationsBuilderARMVIXL::VisitLoadString(HLoadString* load) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00007674 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Vladimir Markoca6fff82017-10-03 14:49:14 +01007675 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(load, call_kind);
Artem Serov02d37832016-10-25 15:25:33 +01007676 HLoadString::LoadKind load_kind = load->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007677 if (load_kind == HLoadString::LoadKind::kRuntimeCall) {
Artem Serov02d37832016-10-25 15:25:33 +01007678 locations->SetOut(LocationFrom(r0));
7679 } else {
7680 locations->SetOut(Location::RequiresRegister());
Artem Serovd4cc5b22016-11-04 11:19:09 +00007681 if (load_kind == HLoadString::LoadKind::kBssEntry) {
7682 if (!kUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00007683 // Rely on the pResolveString and marking to save everything we need, including temps.
Artem Serovd4cc5b22016-11-04 11:19:09 +00007684 RegisterSet caller_saves = RegisterSet::Empty();
7685 InvokeRuntimeCallingConventionARMVIXL calling_convention;
7686 caller_saves.Add(LocationFrom(calling_convention.GetRegisterAt(0)));
7687 // TODO: Add GetReturnLocation() to the calling convention so that we can DCHECK()
7688 // that the the kPrimNot result register is the same as the first argument register.
7689 locations->SetCustomSlowPathCallerSaves(caller_saves);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01007690 if (kUseBakerReadBarrier && kBakerReadBarrierLinkTimeThunksEnableForGcRoots) {
7691 locations->AddTemp(Location::RegisterLocation(kBakerCcEntrypointRegister.GetCode()));
7692 }
Artem Serovd4cc5b22016-11-04 11:19:09 +00007693 } else {
7694 // For non-Baker read barrier we have a temp-clobbering call.
7695 }
7696 }
Artem Serov02d37832016-10-25 15:25:33 +01007697 }
7698}
7699
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007700// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
7701// move.
7702void InstructionCodeGeneratorARMVIXL::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Artem Serovd4cc5b22016-11-04 11:19:09 +00007703 LocationSummary* locations = load->GetLocations();
7704 Location out_loc = locations->Out();
7705 vixl32::Register out = OutputRegister(load);
7706 HLoadString::LoadKind load_kind = load->GetLoadKind();
7707
7708 switch (load_kind) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00007709 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
7710 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
7711 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00007712 codegen_->NewBootImageStringPatch(load->GetDexFile(), load->GetStringIndex());
Artem Serovd4cc5b22016-11-04 11:19:09 +00007713 codegen_->EmitMovwMovtPlaceholder(labels, out);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007714 return;
Artem Serovd4cc5b22016-11-04 11:19:09 +00007715 }
7716 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007717 uint32_t address = dchecked_integral_cast<uint32_t>(
7718 reinterpret_cast<uintptr_t>(load->GetString().Get()));
7719 DCHECK_NE(address, 0u);
Artem Serovc5fcb442016-12-02 19:19:58 +00007720 __ Ldr(out, codegen_->DeduplicateBootImageAddressLiteral(address));
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007721 return;
7722 }
Vladimir Markoe47f60c2018-02-21 13:43:28 +00007723 case HLoadString::LoadKind::kBootImageRelRo: {
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007724 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
7725 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
Vladimir Markoe47f60c2018-02-21 13:43:28 +00007726 codegen_->NewBootImageRelRoPatch(codegen_->GetBootImageOffset(load));
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007727 codegen_->EmitMovwMovtPlaceholder(labels, out);
7728 __ Ldr(out, MemOperand(out, /* offset */ 0));
7729 return;
Artem Serovd4cc5b22016-11-04 11:19:09 +00007730 }
7731 case HLoadString::LoadKind::kBssEntry: {
7732 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Artem Serovd4cc5b22016-11-04 11:19:09 +00007733 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007734 codegen_->NewStringBssEntryPatch(load->GetDexFile(), load->GetStringIndex());
Vladimir Markof3c52b42017-11-17 17:32:12 +00007735 codegen_->EmitMovwMovtPlaceholder(labels, out);
Vladimir Markoca1e0382018-04-11 09:58:41 +00007736 codegen_->GenerateGcRootFieldLoad(
7737 load, out_loc, out, /* offset */ 0, kCompilerReadBarrierOption);
Artem Serovd4cc5b22016-11-04 11:19:09 +00007738 LoadStringSlowPathARMVIXL* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01007739 new (codegen_->GetScopedAllocator()) LoadStringSlowPathARMVIXL(load);
Artem Serovd4cc5b22016-11-04 11:19:09 +00007740 codegen_->AddSlowPath(slow_path);
7741 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
7742 __ Bind(slow_path->GetExitLabel());
Roland Levillain5daa4952017-07-03 17:23:56 +01007743 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 15);
Artem Serovd4cc5b22016-11-04 11:19:09 +00007744 return;
7745 }
7746 case HLoadString::LoadKind::kJitTableAddress: {
Artem Serovc5fcb442016-12-02 19:19:58 +00007747 __ Ldr(out, codegen_->DeduplicateJitStringLiteral(load->GetDexFile(),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007748 load->GetStringIndex(),
7749 load->GetString()));
Artem Serovc5fcb442016-12-02 19:19:58 +00007750 // /* GcRoot<mirror::String> */ out = *out
Vladimir Markoca1e0382018-04-11 09:58:41 +00007751 codegen_->GenerateGcRootFieldLoad(
7752 load, out_loc, out, /* offset */ 0, kCompilerReadBarrierOption);
Artem Serovc5fcb442016-12-02 19:19:58 +00007753 return;
Artem Serovd4cc5b22016-11-04 11:19:09 +00007754 }
7755 default:
7756 break;
7757 }
Artem Serov02d37832016-10-25 15:25:33 +01007758
7759 // TODO: Re-add the compiler code to do string dex cache lookup again.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007760 DCHECK_EQ(load->GetLoadKind(), HLoadString::LoadKind::kRuntimeCall);
Artem Serov02d37832016-10-25 15:25:33 +01007761 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Andreas Gampe8a0128a2016-11-28 07:38:35 -08007762 __ Mov(calling_convention.GetRegisterAt(0), load->GetStringIndex().index_);
Artem Serov02d37832016-10-25 15:25:33 +01007763 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
7764 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Roland Levillain5daa4952017-07-03 17:23:56 +01007765 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 16);
Artem Serov02d37832016-10-25 15:25:33 +01007766}
7767
7768static int32_t GetExceptionTlsOffset() {
7769 return Thread::ExceptionOffset<kArmPointerSize>().Int32Value();
7770}
7771
7772void LocationsBuilderARMVIXL::VisitLoadException(HLoadException* load) {
7773 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007774 new (GetGraph()->GetAllocator()) LocationSummary(load, LocationSummary::kNoCall);
Artem Serov02d37832016-10-25 15:25:33 +01007775 locations->SetOut(Location::RequiresRegister());
7776}
7777
7778void InstructionCodeGeneratorARMVIXL::VisitLoadException(HLoadException* load) {
7779 vixl32::Register out = OutputRegister(load);
7780 GetAssembler()->LoadFromOffset(kLoadWord, out, tr, GetExceptionTlsOffset());
7781}
7782
7783
7784void LocationsBuilderARMVIXL::VisitClearException(HClearException* clear) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007785 new (GetGraph()->GetAllocator()) LocationSummary(clear, LocationSummary::kNoCall);
Artem Serov02d37832016-10-25 15:25:33 +01007786}
7787
7788void InstructionCodeGeneratorARMVIXL::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
7789 UseScratchRegisterScope temps(GetVIXLAssembler());
7790 vixl32::Register temp = temps.Acquire();
7791 __ Mov(temp, 0);
7792 GetAssembler()->StoreToOffset(kStoreWord, temp, tr, GetExceptionTlsOffset());
7793}
7794
7795void LocationsBuilderARMVIXL::VisitThrow(HThrow* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007796 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
7797 instruction, LocationSummary::kCallOnMainOnly);
Artem Serov02d37832016-10-25 15:25:33 +01007798 InvokeRuntimeCallingConventionARMVIXL calling_convention;
7799 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
7800}
7801
7802void InstructionCodeGeneratorARMVIXL::VisitThrow(HThrow* instruction) {
7803 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
7804 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
7805}
7806
Artem Serov657022c2016-11-23 14:19:38 +00007807// Temp is used for read barrier.
7808static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
7809 if (kEmitCompilerReadBarrier &&
7810 (kUseBakerReadBarrier ||
7811 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
7812 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
7813 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
7814 return 1;
7815 }
7816 return 0;
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007817}
7818
Artem Serov657022c2016-11-23 14:19:38 +00007819// Interface case has 3 temps, one for holding the number of interfaces, one for the current
7820// interface pointer, one for loading the current interface.
7821// The other checks have one temp for loading the object's class.
7822static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
7823 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
7824 return 3;
7825 }
7826 return 1 + NumberOfInstanceOfTemps(type_check_kind);
7827}
Artem Serovcfbe9132016-10-14 15:58:56 +01007828
7829void LocationsBuilderARMVIXL::VisitInstanceOf(HInstanceOf* instruction) {
7830 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
7831 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
7832 bool baker_read_barrier_slow_path = false;
7833 switch (type_check_kind) {
7834 case TypeCheckKind::kExactCheck:
7835 case TypeCheckKind::kAbstractClassCheck:
7836 case TypeCheckKind::kClassHierarchyCheck:
Vladimir Marko87584542017-12-12 17:47:52 +00007837 case TypeCheckKind::kArrayObjectCheck: {
7838 bool needs_read_barrier = CodeGenerator::InstanceOfNeedsReadBarrier(instruction);
7839 call_kind = needs_read_barrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
7840 baker_read_barrier_slow_path = kUseBakerReadBarrier && needs_read_barrier;
Artem Serovcfbe9132016-10-14 15:58:56 +01007841 break;
Vladimir Marko87584542017-12-12 17:47:52 +00007842 }
Artem Serovcfbe9132016-10-14 15:58:56 +01007843 case TypeCheckKind::kArrayCheck:
7844 case TypeCheckKind::kUnresolvedCheck:
7845 case TypeCheckKind::kInterfaceCheck:
7846 call_kind = LocationSummary::kCallOnSlowPath;
7847 break;
Vladimir Marko175e7862018-03-27 09:03:13 +00007848 case TypeCheckKind::kBitstringCheck:
7849 break;
Artem Serovcfbe9132016-10-14 15:58:56 +01007850 }
7851
Vladimir Markoca6fff82017-10-03 14:49:14 +01007852 LocationSummary* locations =
7853 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Artem Serovcfbe9132016-10-14 15:58:56 +01007854 if (baker_read_barrier_slow_path) {
7855 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
7856 }
7857 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko175e7862018-03-27 09:03:13 +00007858 if (type_check_kind == TypeCheckKind::kBitstringCheck) {
7859 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
7860 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
7861 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
7862 } else {
7863 locations->SetInAt(1, Location::RequiresRegister());
7864 }
Artem Serovcfbe9132016-10-14 15:58:56 +01007865 // The "out" register is used as a temporary, so it overlaps with the inputs.
7866 // Note that TypeCheckSlowPathARM uses this register too.
7867 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Artem Serov657022c2016-11-23 14:19:38 +00007868 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01007869 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
7870 codegen_->MaybeAddBakerCcEntrypointTempForFields(locations);
7871 }
Artem Serovcfbe9132016-10-14 15:58:56 +01007872}
7873
7874void InstructionCodeGeneratorARMVIXL::VisitInstanceOf(HInstanceOf* instruction) {
7875 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
7876 LocationSummary* locations = instruction->GetLocations();
7877 Location obj_loc = locations->InAt(0);
7878 vixl32::Register obj = InputRegisterAt(instruction, 0);
Vladimir Marko175e7862018-03-27 09:03:13 +00007879 vixl32::Register cls = (type_check_kind == TypeCheckKind::kBitstringCheck)
7880 ? vixl32::Register()
7881 : InputRegisterAt(instruction, 1);
Artem Serovcfbe9132016-10-14 15:58:56 +01007882 Location out_loc = locations->Out();
7883 vixl32::Register out = OutputRegister(instruction);
Artem Serov657022c2016-11-23 14:19:38 +00007884 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
7885 DCHECK_LE(num_temps, 1u);
7886 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Artem Serovcfbe9132016-10-14 15:58:56 +01007887 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
7888 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
7889 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
7890 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007891 vixl32::Label done;
7892 vixl32::Label* const final_label = codegen_->GetFinalLabel(instruction, &done);
Artem Serovcfbe9132016-10-14 15:58:56 +01007893 SlowPathCodeARMVIXL* slow_path = nullptr;
7894
7895 // Return 0 if `obj` is null.
7896 // avoid null check if we know obj is not null.
7897 if (instruction->MustDoNullCheck()) {
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007898 DCHECK(!out.Is(obj));
7899 __ Mov(out, 0);
7900 __ CompareAndBranchIfZero(obj, final_label, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007901 }
7902
Artem Serovcfbe9132016-10-14 15:58:56 +01007903 switch (type_check_kind) {
7904 case TypeCheckKind::kExactCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00007905 ReadBarrierOption read_barrier_option =
7906 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier6beced42016-11-15 15:51:31 -08007907 // /* HeapReference<Class> */ out = obj->klass_
7908 GenerateReferenceLoadTwoRegisters(instruction,
7909 out_loc,
7910 obj_loc,
7911 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00007912 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007913 read_barrier_option);
Artem Serovcfbe9132016-10-14 15:58:56 +01007914 // Classes must be equal for the instanceof to succeed.
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007915 __ Cmp(out, cls);
7916 // We speculatively set the result to false without changing the condition
7917 // flags, which allows us to avoid some branching later.
7918 __ Mov(LeaveFlags, out, 0);
7919
7920 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
7921 // we check that the output is in a low register, so that a 16-bit MOV
7922 // encoding can be used.
7923 if (out.IsLow()) {
7924 // We use the scope because of the IT block that follows.
7925 ExactAssemblyScope guard(GetVIXLAssembler(),
7926 2 * vixl32::k16BitT32InstructionSizeInBytes,
7927 CodeBufferCheckScope::kExactSize);
7928
7929 __ it(eq);
7930 __ mov(eq, out, 1);
7931 } else {
7932 __ B(ne, final_label, /* far_target */ false);
7933 __ Mov(out, 1);
7934 }
7935
Artem Serovcfbe9132016-10-14 15:58:56 +01007936 break;
7937 }
7938
7939 case TypeCheckKind::kAbstractClassCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00007940 ReadBarrierOption read_barrier_option =
7941 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier6beced42016-11-15 15:51:31 -08007942 // /* HeapReference<Class> */ out = obj->klass_
7943 GenerateReferenceLoadTwoRegisters(instruction,
7944 out_loc,
7945 obj_loc,
7946 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00007947 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007948 read_barrier_option);
Artem Serovcfbe9132016-10-14 15:58:56 +01007949 // If the class is abstract, we eagerly fetch the super class of the
7950 // object to avoid doing a comparison we know will fail.
7951 vixl32::Label loop;
7952 __ Bind(&loop);
7953 // /* HeapReference<Class> */ out = out->super_class_
Artem Serov657022c2016-11-23 14:19:38 +00007954 GenerateReferenceLoadOneRegister(instruction,
7955 out_loc,
7956 super_offset,
7957 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007958 read_barrier_option);
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007959 // If `out` is null, we use it for the result, and jump to the final label.
Anton Kirilov6f644202017-02-27 18:29:45 +00007960 __ CompareAndBranchIfZero(out, final_label, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007961 __ Cmp(out, cls);
Artem Serov517d9f62016-12-12 15:51:15 +00007962 __ B(ne, &loop, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007963 __ Mov(out, 1);
Artem Serovcfbe9132016-10-14 15:58:56 +01007964 break;
7965 }
7966
7967 case TypeCheckKind::kClassHierarchyCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00007968 ReadBarrierOption read_barrier_option =
7969 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier6beced42016-11-15 15:51:31 -08007970 // /* HeapReference<Class> */ out = obj->klass_
7971 GenerateReferenceLoadTwoRegisters(instruction,
7972 out_loc,
7973 obj_loc,
7974 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00007975 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007976 read_barrier_option);
Artem Serovcfbe9132016-10-14 15:58:56 +01007977 // Walk over the class hierarchy to find a match.
7978 vixl32::Label loop, success;
7979 __ Bind(&loop);
7980 __ Cmp(out, cls);
Artem Serov517d9f62016-12-12 15:51:15 +00007981 __ B(eq, &success, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007982 // /* HeapReference<Class> */ out = out->super_class_
Artem Serov657022c2016-11-23 14:19:38 +00007983 GenerateReferenceLoadOneRegister(instruction,
7984 out_loc,
7985 super_offset,
7986 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007987 read_barrier_option);
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007988 // This is essentially a null check, but it sets the condition flags to the
7989 // proper value for the code that follows the loop, i.e. not `eq`.
7990 __ Cmp(out, 1);
7991 __ B(hs, &loop, /* far_target */ false);
7992
7993 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
7994 // we check that the output is in a low register, so that a 16-bit MOV
7995 // encoding can be used.
7996 if (out.IsLow()) {
7997 // If `out` is null, we use it for the result, and the condition flags
7998 // have already been set to `ne`, so the IT block that comes afterwards
7999 // (and which handles the successful case) turns into a NOP (instead of
8000 // overwriting `out`).
8001 __ Bind(&success);
8002
8003 // We use the scope because of the IT block that follows.
8004 ExactAssemblyScope guard(GetVIXLAssembler(),
8005 2 * vixl32::k16BitT32InstructionSizeInBytes,
8006 CodeBufferCheckScope::kExactSize);
8007
8008 // There is only one branch to the `success` label (which is bound to this
8009 // IT block), and it has the same condition, `eq`, so in that case the MOV
8010 // is executed.
8011 __ it(eq);
8012 __ mov(eq, out, 1);
8013 } else {
8014 // If `out` is null, we use it for the result, and jump to the final label.
Anton Kirilov6f644202017-02-27 18:29:45 +00008015 __ B(final_label);
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00008016 __ Bind(&success);
8017 __ Mov(out, 1);
Artem Serovcfbe9132016-10-14 15:58:56 +01008018 }
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00008019
Artem Serovcfbe9132016-10-14 15:58:56 +01008020 break;
8021 }
8022
8023 case TypeCheckKind::kArrayObjectCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00008024 ReadBarrierOption read_barrier_option =
8025 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier6beced42016-11-15 15:51:31 -08008026 // /* HeapReference<Class> */ out = obj->klass_
8027 GenerateReferenceLoadTwoRegisters(instruction,
8028 out_loc,
8029 obj_loc,
8030 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00008031 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00008032 read_barrier_option);
Artem Serovcfbe9132016-10-14 15:58:56 +01008033 // Do an exact check.
8034 vixl32::Label exact_check;
8035 __ Cmp(out, cls);
Artem Serov517d9f62016-12-12 15:51:15 +00008036 __ B(eq, &exact_check, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01008037 // Otherwise, we need to check that the object's class is a non-primitive array.
8038 // /* HeapReference<Class> */ out = out->component_type_
Artem Serov657022c2016-11-23 14:19:38 +00008039 GenerateReferenceLoadOneRegister(instruction,
8040 out_loc,
8041 component_offset,
8042 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00008043 read_barrier_option);
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00008044 // If `out` is null, we use it for the result, and jump to the final label.
Anton Kirilov6f644202017-02-27 18:29:45 +00008045 __ CompareAndBranchIfZero(out, final_label, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01008046 GetAssembler()->LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
8047 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00008048 __ Cmp(out, 0);
8049 // We speculatively set the result to false without changing the condition
8050 // flags, which allows us to avoid some branching later.
8051 __ Mov(LeaveFlags, out, 0);
8052
8053 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
8054 // we check that the output is in a low register, so that a 16-bit MOV
8055 // encoding can be used.
8056 if (out.IsLow()) {
8057 __ Bind(&exact_check);
8058
8059 // We use the scope because of the IT block that follows.
8060 ExactAssemblyScope guard(GetVIXLAssembler(),
8061 2 * vixl32::k16BitT32InstructionSizeInBytes,
8062 CodeBufferCheckScope::kExactSize);
8063
8064 __ it(eq);
8065 __ mov(eq, out, 1);
8066 } else {
8067 __ B(ne, final_label, /* far_target */ false);
8068 __ Bind(&exact_check);
8069 __ Mov(out, 1);
8070 }
8071
Artem Serovcfbe9132016-10-14 15:58:56 +01008072 break;
8073 }
8074
8075 case TypeCheckKind::kArrayCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00008076 // No read barrier since the slow path will retry upon failure.
Mathieu Chartier6beced42016-11-15 15:51:31 -08008077 // /* HeapReference<Class> */ out = obj->klass_
8078 GenerateReferenceLoadTwoRegisters(instruction,
8079 out_loc,
8080 obj_loc,
8081 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00008082 maybe_temp_loc,
8083 kWithoutReadBarrier);
Artem Serovcfbe9132016-10-14 15:58:56 +01008084 __ Cmp(out, cls);
8085 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01008086 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathARMVIXL(
8087 instruction, /* is_fatal */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01008088 codegen_->AddSlowPath(slow_path);
8089 __ B(ne, slow_path->GetEntryLabel());
8090 __ Mov(out, 1);
Artem Serovcfbe9132016-10-14 15:58:56 +01008091 break;
8092 }
8093
8094 case TypeCheckKind::kUnresolvedCheck:
8095 case TypeCheckKind::kInterfaceCheck: {
8096 // Note that we indeed only call on slow path, but we always go
8097 // into the slow path for the unresolved and interface check
8098 // cases.
8099 //
8100 // We cannot directly call the InstanceofNonTrivial runtime
8101 // entry point without resorting to a type checking slow path
8102 // here (i.e. by calling InvokeRuntime directly), as it would
8103 // require to assign fixed registers for the inputs of this
8104 // HInstanceOf instruction (following the runtime calling
8105 // convention), which might be cluttered by the potential first
8106 // read barrier emission at the beginning of this method.
8107 //
8108 // TODO: Introduce a new runtime entry point taking the object
8109 // to test (instead of its class) as argument, and let it deal
8110 // with the read barrier issues. This will let us refactor this
8111 // case of the `switch` code as it was previously (with a direct
8112 // call to the runtime not using a type checking slow path).
8113 // This should also be beneficial for the other cases above.
8114 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01008115 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathARMVIXL(
8116 instruction, /* is_fatal */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01008117 codegen_->AddSlowPath(slow_path);
8118 __ B(slow_path->GetEntryLabel());
Artem Serovcfbe9132016-10-14 15:58:56 +01008119 break;
8120 }
Vladimir Marko175e7862018-03-27 09:03:13 +00008121
8122 case TypeCheckKind::kBitstringCheck: {
8123 // /* HeapReference<Class> */ temp = obj->klass_
8124 GenerateReferenceLoadTwoRegisters(instruction,
8125 out_loc,
8126 obj_loc,
8127 class_offset,
8128 maybe_temp_loc,
8129 kWithoutReadBarrier);
8130
8131 GenerateBitstringTypeCheckCompare(instruction, out, DontCare);
8132 // If `out` is a low reg and we would have another low reg temp, we could
8133 // optimize this as RSBS+ADC, see GenerateConditionWithZero().
8134 //
8135 // Also, in some cases when `out` is a low reg and we're loading a constant to IP
8136 // it would make sense to use CMP+MOV+IT+MOV instead of SUB+CLZ+LSR as the code size
8137 // would be the same and we would have fewer direct data dependencies.
8138 codegen_->GenerateConditionWithZero(kCondEQ, out, out); // CLZ+LSR
8139 break;
8140 }
Artem Serovcfbe9132016-10-14 15:58:56 +01008141 }
8142
Artem Serovcfbe9132016-10-14 15:58:56 +01008143 if (done.IsReferenced()) {
8144 __ Bind(&done);
8145 }
8146
8147 if (slow_path != nullptr) {
8148 __ Bind(slow_path->GetExitLabel());
8149 }
8150}
8151
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008152void LocationsBuilderARMVIXL::VisitCheckCast(HCheckCast* instruction) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008153 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko87584542017-12-12 17:47:52 +00008154 LocationSummary::CallKind call_kind = CodeGenerator::GetCheckCastCallKind(instruction);
Vladimir Markoca6fff82017-10-03 14:49:14 +01008155 LocationSummary* locations =
8156 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008157 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko175e7862018-03-27 09:03:13 +00008158 if (type_check_kind == TypeCheckKind::kBitstringCheck) {
8159 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
8160 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
8161 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
8162 } else {
8163 locations->SetInAt(1, Location::RequiresRegister());
8164 }
Artem Serov657022c2016-11-23 14:19:38 +00008165 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008166}
8167
8168void InstructionCodeGeneratorARMVIXL::VisitCheckCast(HCheckCast* instruction) {
8169 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
8170 LocationSummary* locations = instruction->GetLocations();
8171 Location obj_loc = locations->InAt(0);
8172 vixl32::Register obj = InputRegisterAt(instruction, 0);
Vladimir Marko175e7862018-03-27 09:03:13 +00008173 vixl32::Register cls = (type_check_kind == TypeCheckKind::kBitstringCheck)
8174 ? vixl32::Register()
8175 : InputRegisterAt(instruction, 1);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008176 Location temp_loc = locations->GetTemp(0);
8177 vixl32::Register temp = RegisterFrom(temp_loc);
Artem Serov657022c2016-11-23 14:19:38 +00008178 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
8179 DCHECK_LE(num_temps, 3u);
8180 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
8181 Location maybe_temp3_loc = (num_temps >= 3) ? locations->GetTemp(2) : Location::NoLocation();
8182 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
8183 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
8184 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
8185 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
8186 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
8187 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
8188 const uint32_t object_array_data_offset =
8189 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008190
Vladimir Marko87584542017-12-12 17:47:52 +00008191 bool is_type_check_slow_path_fatal = CodeGenerator::IsTypeCheckSlowPathFatal(instruction);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008192 SlowPathCodeARMVIXL* type_check_slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01008193 new (codegen_->GetScopedAllocator()) TypeCheckSlowPathARMVIXL(
8194 instruction, is_type_check_slow_path_fatal);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008195 codegen_->AddSlowPath(type_check_slow_path);
8196
8197 vixl32::Label done;
Anton Kirilov6f644202017-02-27 18:29:45 +00008198 vixl32::Label* final_label = codegen_->GetFinalLabel(instruction, &done);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008199 // Avoid null check if we know obj is not null.
8200 if (instruction->MustDoNullCheck()) {
Anton Kirilov6f644202017-02-27 18:29:45 +00008201 __ CompareAndBranchIfZero(obj, final_label, /* far_target */ false);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008202 }
8203
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008204 switch (type_check_kind) {
8205 case TypeCheckKind::kExactCheck:
8206 case TypeCheckKind::kArrayCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00008207 // /* HeapReference<Class> */ temp = obj->klass_
8208 GenerateReferenceLoadTwoRegisters(instruction,
8209 temp_loc,
8210 obj_loc,
8211 class_offset,
8212 maybe_temp2_loc,
8213 kWithoutReadBarrier);
8214
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008215 __ Cmp(temp, cls);
8216 // Jump to slow path for throwing the exception or doing a
8217 // more involved array check.
8218 __ B(ne, type_check_slow_path->GetEntryLabel());
8219 break;
8220 }
8221
8222 case TypeCheckKind::kAbstractClassCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00008223 // /* HeapReference<Class> */ temp = obj->klass_
8224 GenerateReferenceLoadTwoRegisters(instruction,
8225 temp_loc,
8226 obj_loc,
8227 class_offset,
8228 maybe_temp2_loc,
8229 kWithoutReadBarrier);
8230
Artem Serovcfbe9132016-10-14 15:58:56 +01008231 // If the class is abstract, we eagerly fetch the super class of the
8232 // object to avoid doing a comparison we know will fail.
8233 vixl32::Label loop;
8234 __ Bind(&loop);
8235 // /* HeapReference<Class> */ temp = temp->super_class_
Artem Serov657022c2016-11-23 14:19:38 +00008236 GenerateReferenceLoadOneRegister(instruction,
8237 temp_loc,
8238 super_offset,
8239 maybe_temp2_loc,
8240 kWithoutReadBarrier);
Artem Serovcfbe9132016-10-14 15:58:56 +01008241
8242 // If the class reference currently in `temp` is null, jump to the slow path to throw the
8243 // exception.
xueliang.zhongf51bc622016-11-04 09:23:32 +00008244 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
Artem Serovcfbe9132016-10-14 15:58:56 +01008245
8246 // Otherwise, compare the classes.
8247 __ Cmp(temp, cls);
Artem Serov517d9f62016-12-12 15:51:15 +00008248 __ B(ne, &loop, /* far_target */ false);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008249 break;
8250 }
8251
8252 case TypeCheckKind::kClassHierarchyCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00008253 // /* HeapReference<Class> */ temp = obj->klass_
8254 GenerateReferenceLoadTwoRegisters(instruction,
8255 temp_loc,
8256 obj_loc,
8257 class_offset,
8258 maybe_temp2_loc,
8259 kWithoutReadBarrier);
8260
Artem Serovcfbe9132016-10-14 15:58:56 +01008261 // Walk over the class hierarchy to find a match.
8262 vixl32::Label loop;
8263 __ Bind(&loop);
8264 __ Cmp(temp, cls);
Anton Kirilov6f644202017-02-27 18:29:45 +00008265 __ B(eq, final_label, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01008266
8267 // /* HeapReference<Class> */ temp = temp->super_class_
Artem Serov657022c2016-11-23 14:19:38 +00008268 GenerateReferenceLoadOneRegister(instruction,
8269 temp_loc,
8270 super_offset,
8271 maybe_temp2_loc,
8272 kWithoutReadBarrier);
Artem Serovcfbe9132016-10-14 15:58:56 +01008273
8274 // If the class reference currently in `temp` is null, jump to the slow path to throw the
8275 // exception.
xueliang.zhongf51bc622016-11-04 09:23:32 +00008276 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
Artem Serovcfbe9132016-10-14 15:58:56 +01008277 // Otherwise, jump to the beginning of the loop.
8278 __ B(&loop);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008279 break;
8280 }
8281
Artem Serovcfbe9132016-10-14 15:58:56 +01008282 case TypeCheckKind::kArrayObjectCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00008283 // /* HeapReference<Class> */ temp = obj->klass_
8284 GenerateReferenceLoadTwoRegisters(instruction,
8285 temp_loc,
8286 obj_loc,
8287 class_offset,
8288 maybe_temp2_loc,
8289 kWithoutReadBarrier);
8290
Artem Serovcfbe9132016-10-14 15:58:56 +01008291 // Do an exact check.
8292 __ Cmp(temp, cls);
Anton Kirilov6f644202017-02-27 18:29:45 +00008293 __ B(eq, final_label, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01008294
8295 // Otherwise, we need to check that the object's class is a non-primitive array.
8296 // /* HeapReference<Class> */ temp = temp->component_type_
Artem Serov657022c2016-11-23 14:19:38 +00008297 GenerateReferenceLoadOneRegister(instruction,
8298 temp_loc,
8299 component_offset,
8300 maybe_temp2_loc,
8301 kWithoutReadBarrier);
Artem Serovcfbe9132016-10-14 15:58:56 +01008302 // If the component type is null, jump to the slow path to throw the exception.
xueliang.zhongf51bc622016-11-04 09:23:32 +00008303 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
Artem Serovcfbe9132016-10-14 15:58:56 +01008304 // Otherwise,the object is indeed an array, jump to label `check_non_primitive_component_type`
8305 // to further check that this component type is not a primitive type.
8306 GetAssembler()->LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008307 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
xueliang.zhongf51bc622016-11-04 09:23:32 +00008308 __ CompareAndBranchIfNonZero(temp, type_check_slow_path->GetEntryLabel());
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008309 break;
8310 }
8311
8312 case TypeCheckKind::kUnresolvedCheck:
Artem Serov657022c2016-11-23 14:19:38 +00008313 // We always go into the type check slow path for the unresolved check case.
Artem Serovcfbe9132016-10-14 15:58:56 +01008314 // We cannot directly call the CheckCast runtime entry point
8315 // without resorting to a type checking slow path here (i.e. by
8316 // calling InvokeRuntime directly), as it would require to
8317 // assign fixed registers for the inputs of this HInstanceOf
8318 // instruction (following the runtime calling convention), which
8319 // might be cluttered by the potential first read barrier
8320 // emission at the beginning of this method.
Artem Serov657022c2016-11-23 14:19:38 +00008321
Artem Serovcfbe9132016-10-14 15:58:56 +01008322 __ B(type_check_slow_path->GetEntryLabel());
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008323 break;
Artem Serov657022c2016-11-23 14:19:38 +00008324
8325 case TypeCheckKind::kInterfaceCheck: {
8326 // Avoid read barriers to improve performance of the fast path. We can not get false
8327 // positives by doing this.
8328 // /* HeapReference<Class> */ temp = obj->klass_
8329 GenerateReferenceLoadTwoRegisters(instruction,
8330 temp_loc,
8331 obj_loc,
8332 class_offset,
8333 maybe_temp2_loc,
8334 kWithoutReadBarrier);
8335
8336 // /* HeapReference<Class> */ temp = temp->iftable_
8337 GenerateReferenceLoadTwoRegisters(instruction,
8338 temp_loc,
8339 temp_loc,
8340 iftable_offset,
8341 maybe_temp2_loc,
8342 kWithoutReadBarrier);
8343 // Iftable is never null.
8344 __ Ldr(RegisterFrom(maybe_temp2_loc), MemOperand(temp, array_length_offset));
8345 // Loop through the iftable and check if any class matches.
8346 vixl32::Label start_loop;
8347 __ Bind(&start_loop);
8348 __ CompareAndBranchIfZero(RegisterFrom(maybe_temp2_loc),
8349 type_check_slow_path->GetEntryLabel());
8350 __ Ldr(RegisterFrom(maybe_temp3_loc), MemOperand(temp, object_array_data_offset));
8351 GetAssembler()->MaybeUnpoisonHeapReference(RegisterFrom(maybe_temp3_loc));
8352 // Go to next interface.
8353 __ Add(temp, temp, Operand::From(2 * kHeapReferenceSize));
8354 __ Sub(RegisterFrom(maybe_temp2_loc), RegisterFrom(maybe_temp2_loc), 2);
8355 // Compare the classes and continue the loop if they do not match.
8356 __ Cmp(cls, RegisterFrom(maybe_temp3_loc));
Artem Serov517d9f62016-12-12 15:51:15 +00008357 __ B(ne, &start_loop, /* far_target */ false);
Artem Serov657022c2016-11-23 14:19:38 +00008358 break;
8359 }
Vladimir Marko175e7862018-03-27 09:03:13 +00008360
8361 case TypeCheckKind::kBitstringCheck: {
8362 // /* HeapReference<Class> */ temp = obj->klass_
8363 GenerateReferenceLoadTwoRegisters(instruction,
8364 temp_loc,
8365 obj_loc,
8366 class_offset,
8367 maybe_temp2_loc,
8368 kWithoutReadBarrier);
8369
8370 GenerateBitstringTypeCheckCompare(instruction, temp, SetFlags);
8371 __ B(ne, type_check_slow_path->GetEntryLabel());
8372 break;
8373 }
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008374 }
Anton Kirilov6f644202017-02-27 18:29:45 +00008375 if (done.IsReferenced()) {
8376 __ Bind(&done);
8377 }
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008378
8379 __ Bind(type_check_slow_path->GetExitLabel());
8380}
8381
Artem Serov551b28f2016-10-18 19:11:30 +01008382void LocationsBuilderARMVIXL::VisitMonitorOperation(HMonitorOperation* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008383 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
8384 instruction, LocationSummary::kCallOnMainOnly);
Artem Serov551b28f2016-10-18 19:11:30 +01008385 InvokeRuntimeCallingConventionARMVIXL calling_convention;
8386 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
8387}
8388
8389void InstructionCodeGeneratorARMVIXL::VisitMonitorOperation(HMonitorOperation* instruction) {
8390 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
8391 instruction,
8392 instruction->GetDexPc());
8393 if (instruction->IsEnter()) {
8394 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
8395 } else {
8396 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
8397 }
Roland Levillain5daa4952017-07-03 17:23:56 +01008398 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 17);
Artem Serov551b28f2016-10-18 19:11:30 +01008399}
8400
Artem Serov02109dd2016-09-23 17:17:54 +01008401void LocationsBuilderARMVIXL::VisitAnd(HAnd* instruction) {
8402 HandleBitwiseOperation(instruction, AND);
8403}
8404
8405void LocationsBuilderARMVIXL::VisitOr(HOr* instruction) {
8406 HandleBitwiseOperation(instruction, ORR);
8407}
8408
8409void LocationsBuilderARMVIXL::VisitXor(HXor* instruction) {
8410 HandleBitwiseOperation(instruction, EOR);
8411}
8412
8413void LocationsBuilderARMVIXL::HandleBitwiseOperation(HBinaryOperation* instruction, Opcode opcode) {
8414 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008415 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008416 DCHECK(instruction->GetResultType() == DataType::Type::kInt32
8417 || instruction->GetResultType() == DataType::Type::kInt64);
Artem Serov02109dd2016-09-23 17:17:54 +01008418 // Note: GVN reorders commutative operations to have the constant on the right hand side.
8419 locations->SetInAt(0, Location::RequiresRegister());
8420 locations->SetInAt(1, ArmEncodableConstantOrRegister(instruction->InputAt(1), opcode));
8421 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8422}
8423
8424void InstructionCodeGeneratorARMVIXL::VisitAnd(HAnd* instruction) {
8425 HandleBitwiseOperation(instruction);
8426}
8427
8428void InstructionCodeGeneratorARMVIXL::VisitOr(HOr* instruction) {
8429 HandleBitwiseOperation(instruction);
8430}
8431
8432void InstructionCodeGeneratorARMVIXL::VisitXor(HXor* instruction) {
8433 HandleBitwiseOperation(instruction);
8434}
8435
Artem Serov2bbc9532016-10-21 11:51:50 +01008436void LocationsBuilderARMVIXL::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
8437 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008438 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008439 DCHECK(instruction->GetResultType() == DataType::Type::kInt32
8440 || instruction->GetResultType() == DataType::Type::kInt64);
Artem Serov2bbc9532016-10-21 11:51:50 +01008441
8442 locations->SetInAt(0, Location::RequiresRegister());
8443 locations->SetInAt(1, Location::RequiresRegister());
8444 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8445}
8446
8447void InstructionCodeGeneratorARMVIXL::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
8448 LocationSummary* locations = instruction->GetLocations();
8449 Location first = locations->InAt(0);
8450 Location second = locations->InAt(1);
8451 Location out = locations->Out();
8452
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008453 if (instruction->GetResultType() == DataType::Type::kInt32) {
Artem Serov2bbc9532016-10-21 11:51:50 +01008454 vixl32::Register first_reg = RegisterFrom(first);
8455 vixl32::Register second_reg = RegisterFrom(second);
8456 vixl32::Register out_reg = RegisterFrom(out);
8457
8458 switch (instruction->GetOpKind()) {
8459 case HInstruction::kAnd:
8460 __ Bic(out_reg, first_reg, second_reg);
8461 break;
8462 case HInstruction::kOr:
8463 __ Orn(out_reg, first_reg, second_reg);
8464 break;
8465 // There is no EON on arm.
8466 case HInstruction::kXor:
8467 default:
8468 LOG(FATAL) << "Unexpected instruction " << instruction->DebugName();
8469 UNREACHABLE();
8470 }
8471 return;
8472
8473 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008474 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
Artem Serov2bbc9532016-10-21 11:51:50 +01008475 vixl32::Register first_low = LowRegisterFrom(first);
8476 vixl32::Register first_high = HighRegisterFrom(first);
8477 vixl32::Register second_low = LowRegisterFrom(second);
8478 vixl32::Register second_high = HighRegisterFrom(second);
8479 vixl32::Register out_low = LowRegisterFrom(out);
8480 vixl32::Register out_high = HighRegisterFrom(out);
8481
8482 switch (instruction->GetOpKind()) {
8483 case HInstruction::kAnd:
8484 __ Bic(out_low, first_low, second_low);
8485 __ Bic(out_high, first_high, second_high);
8486 break;
8487 case HInstruction::kOr:
8488 __ Orn(out_low, first_low, second_low);
8489 __ Orn(out_high, first_high, second_high);
8490 break;
8491 // There is no EON on arm.
8492 case HInstruction::kXor:
8493 default:
8494 LOG(FATAL) << "Unexpected instruction " << instruction->DebugName();
8495 UNREACHABLE();
8496 }
8497 }
8498}
8499
Anton Kirilov74234da2017-01-13 14:42:47 +00008500void LocationsBuilderARMVIXL::VisitDataProcWithShifterOp(
8501 HDataProcWithShifterOp* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008502 DCHECK(instruction->GetType() == DataType::Type::kInt32 ||
8503 instruction->GetType() == DataType::Type::kInt64);
Anton Kirilov74234da2017-01-13 14:42:47 +00008504 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008505 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008506 const bool overlap = instruction->GetType() == DataType::Type::kInt64 &&
Anton Kirilov74234da2017-01-13 14:42:47 +00008507 HDataProcWithShifterOp::IsExtensionOp(instruction->GetOpKind());
8508
8509 locations->SetInAt(0, Location::RequiresRegister());
8510 locations->SetInAt(1, Location::RequiresRegister());
8511 locations->SetOut(Location::RequiresRegister(),
8512 overlap ? Location::kOutputOverlap : Location::kNoOutputOverlap);
8513}
8514
8515void InstructionCodeGeneratorARMVIXL::VisitDataProcWithShifterOp(
8516 HDataProcWithShifterOp* instruction) {
8517 const LocationSummary* const locations = instruction->GetLocations();
8518 const HInstruction::InstructionKind kind = instruction->GetInstrKind();
8519 const HDataProcWithShifterOp::OpKind op_kind = instruction->GetOpKind();
8520
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008521 if (instruction->GetType() == DataType::Type::kInt32) {
Anton Kirilov420ee302017-02-21 18:10:26 +00008522 const vixl32::Register first = InputRegisterAt(instruction, 0);
8523 const vixl32::Register output = OutputRegister(instruction);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008524 const vixl32::Register second = instruction->InputAt(1)->GetType() == DataType::Type::kInt64
Anton Kirilov74234da2017-01-13 14:42:47 +00008525 ? LowRegisterFrom(locations->InAt(1))
8526 : InputRegisterAt(instruction, 1);
8527
Anton Kirilov420ee302017-02-21 18:10:26 +00008528 if (HDataProcWithShifterOp::IsExtensionOp(op_kind)) {
8529 DCHECK_EQ(kind, HInstruction::kAdd);
8530
8531 switch (op_kind) {
8532 case HDataProcWithShifterOp::kUXTB:
8533 __ Uxtab(output, first, second);
8534 break;
8535 case HDataProcWithShifterOp::kUXTH:
8536 __ Uxtah(output, first, second);
8537 break;
8538 case HDataProcWithShifterOp::kSXTB:
8539 __ Sxtab(output, first, second);
8540 break;
8541 case HDataProcWithShifterOp::kSXTH:
8542 __ Sxtah(output, first, second);
8543 break;
8544 default:
8545 LOG(FATAL) << "Unexpected operation kind: " << op_kind;
8546 UNREACHABLE();
8547 }
8548 } else {
8549 GenerateDataProcInstruction(kind,
8550 output,
8551 first,
8552 Operand(second,
8553 ShiftFromOpKind(op_kind),
8554 instruction->GetShiftAmount()),
8555 codegen_);
8556 }
Anton Kirilov74234da2017-01-13 14:42:47 +00008557 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008558 DCHECK_EQ(instruction->GetType(), DataType::Type::kInt64);
Anton Kirilov74234da2017-01-13 14:42:47 +00008559
8560 if (HDataProcWithShifterOp::IsExtensionOp(op_kind)) {
8561 const vixl32::Register second = InputRegisterAt(instruction, 1);
8562
8563 DCHECK(!LowRegisterFrom(locations->Out()).Is(second));
8564 GenerateDataProc(kind,
8565 locations->Out(),
8566 locations->InAt(0),
8567 second,
8568 Operand(second, ShiftType::ASR, 31),
8569 codegen_);
8570 } else {
8571 GenerateLongDataProc(instruction, codegen_);
8572 }
8573 }
8574}
8575
Artem Serov02109dd2016-09-23 17:17:54 +01008576// TODO(VIXL): Remove optimizations in the helper when they are implemented in vixl.
8577void InstructionCodeGeneratorARMVIXL::GenerateAndConst(vixl32::Register out,
8578 vixl32::Register first,
8579 uint32_t value) {
8580 // Optimize special cases for individual halfs of `and-long` (`and` is simplified earlier).
8581 if (value == 0xffffffffu) {
8582 if (!out.Is(first)) {
8583 __ Mov(out, first);
8584 }
8585 return;
8586 }
8587 if (value == 0u) {
8588 __ Mov(out, 0);
8589 return;
8590 }
8591 if (GetAssembler()->ShifterOperandCanHold(AND, value)) {
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00008592 __ And(out, first, value);
8593 } else if (GetAssembler()->ShifterOperandCanHold(BIC, ~value)) {
8594 __ Bic(out, first, ~value);
Artem Serov02109dd2016-09-23 17:17:54 +01008595 } else {
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00008596 DCHECK(IsPowerOfTwo(value + 1));
8597 __ Ubfx(out, first, 0, WhichPowerOf2(value + 1));
Artem Serov02109dd2016-09-23 17:17:54 +01008598 }
8599}
8600
8601// TODO(VIXL): Remove optimizations in the helper when they are implemented in vixl.
8602void InstructionCodeGeneratorARMVIXL::GenerateOrrConst(vixl32::Register out,
8603 vixl32::Register first,
8604 uint32_t value) {
8605 // Optimize special cases for individual halfs of `or-long` (`or` is simplified earlier).
8606 if (value == 0u) {
8607 if (!out.Is(first)) {
8608 __ Mov(out, first);
8609 }
8610 return;
8611 }
8612 if (value == 0xffffffffu) {
8613 __ Mvn(out, 0);
8614 return;
8615 }
8616 if (GetAssembler()->ShifterOperandCanHold(ORR, value)) {
8617 __ Orr(out, first, value);
8618 } else {
8619 DCHECK(GetAssembler()->ShifterOperandCanHold(ORN, ~value));
8620 __ Orn(out, first, ~value);
8621 }
8622}
8623
8624// TODO(VIXL): Remove optimizations in the helper when they are implemented in vixl.
8625void InstructionCodeGeneratorARMVIXL::GenerateEorConst(vixl32::Register out,
8626 vixl32::Register first,
8627 uint32_t value) {
8628 // Optimize special case for individual halfs of `xor-long` (`xor` is simplified earlier).
8629 if (value == 0u) {
8630 if (!out.Is(first)) {
8631 __ Mov(out, first);
8632 }
8633 return;
8634 }
8635 __ Eor(out, first, value);
8636}
8637
Anton Kirilovdda43962016-11-21 19:55:20 +00008638void InstructionCodeGeneratorARMVIXL::GenerateAddLongConst(Location out,
8639 Location first,
8640 uint64_t value) {
8641 vixl32::Register out_low = LowRegisterFrom(out);
8642 vixl32::Register out_high = HighRegisterFrom(out);
8643 vixl32::Register first_low = LowRegisterFrom(first);
8644 vixl32::Register first_high = HighRegisterFrom(first);
8645 uint32_t value_low = Low32Bits(value);
8646 uint32_t value_high = High32Bits(value);
8647 if (value_low == 0u) {
8648 if (!out_low.Is(first_low)) {
8649 __ Mov(out_low, first_low);
8650 }
8651 __ Add(out_high, first_high, value_high);
8652 return;
8653 }
8654 __ Adds(out_low, first_low, value_low);
Vladimir Markof0a6a1d2018-01-08 14:23:56 +00008655 if (GetAssembler()->ShifterOperandCanHold(ADC, value_high)) {
Anton Kirilovdda43962016-11-21 19:55:20 +00008656 __ Adc(out_high, first_high, value_high);
Anton Kirilovdda43962016-11-21 19:55:20 +00008657 } else {
Vladimir Markof0a6a1d2018-01-08 14:23:56 +00008658 DCHECK(GetAssembler()->ShifterOperandCanHold(SBC, ~value_high));
8659 __ Sbc(out_high, first_high, ~value_high);
Anton Kirilovdda43962016-11-21 19:55:20 +00008660 }
8661}
8662
Artem Serov02109dd2016-09-23 17:17:54 +01008663void InstructionCodeGeneratorARMVIXL::HandleBitwiseOperation(HBinaryOperation* instruction) {
8664 LocationSummary* locations = instruction->GetLocations();
8665 Location first = locations->InAt(0);
8666 Location second = locations->InAt(1);
8667 Location out = locations->Out();
8668
8669 if (second.IsConstant()) {
8670 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
8671 uint32_t value_low = Low32Bits(value);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008672 if (instruction->GetResultType() == DataType::Type::kInt32) {
Artem Serov02109dd2016-09-23 17:17:54 +01008673 vixl32::Register first_reg = InputRegisterAt(instruction, 0);
8674 vixl32::Register out_reg = OutputRegister(instruction);
8675 if (instruction->IsAnd()) {
8676 GenerateAndConst(out_reg, first_reg, value_low);
8677 } else if (instruction->IsOr()) {
8678 GenerateOrrConst(out_reg, first_reg, value_low);
8679 } else {
8680 DCHECK(instruction->IsXor());
8681 GenerateEorConst(out_reg, first_reg, value_low);
8682 }
8683 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008684 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
Artem Serov02109dd2016-09-23 17:17:54 +01008685 uint32_t value_high = High32Bits(value);
8686 vixl32::Register first_low = LowRegisterFrom(first);
8687 vixl32::Register first_high = HighRegisterFrom(first);
8688 vixl32::Register out_low = LowRegisterFrom(out);
8689 vixl32::Register out_high = HighRegisterFrom(out);
8690 if (instruction->IsAnd()) {
8691 GenerateAndConst(out_low, first_low, value_low);
8692 GenerateAndConst(out_high, first_high, value_high);
8693 } else if (instruction->IsOr()) {
8694 GenerateOrrConst(out_low, first_low, value_low);
8695 GenerateOrrConst(out_high, first_high, value_high);
8696 } else {
8697 DCHECK(instruction->IsXor());
8698 GenerateEorConst(out_low, first_low, value_low);
8699 GenerateEorConst(out_high, first_high, value_high);
8700 }
8701 }
8702 return;
8703 }
8704
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008705 if (instruction->GetResultType() == DataType::Type::kInt32) {
Artem Serov02109dd2016-09-23 17:17:54 +01008706 vixl32::Register first_reg = InputRegisterAt(instruction, 0);
8707 vixl32::Register second_reg = InputRegisterAt(instruction, 1);
8708 vixl32::Register out_reg = OutputRegister(instruction);
8709 if (instruction->IsAnd()) {
8710 __ And(out_reg, first_reg, second_reg);
8711 } else if (instruction->IsOr()) {
8712 __ Orr(out_reg, first_reg, second_reg);
8713 } else {
8714 DCHECK(instruction->IsXor());
8715 __ Eor(out_reg, first_reg, second_reg);
8716 }
8717 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008718 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
Artem Serov02109dd2016-09-23 17:17:54 +01008719 vixl32::Register first_low = LowRegisterFrom(first);
8720 vixl32::Register first_high = HighRegisterFrom(first);
8721 vixl32::Register second_low = LowRegisterFrom(second);
8722 vixl32::Register second_high = HighRegisterFrom(second);
8723 vixl32::Register out_low = LowRegisterFrom(out);
8724 vixl32::Register out_high = HighRegisterFrom(out);
8725 if (instruction->IsAnd()) {
8726 __ And(out_low, first_low, second_low);
8727 __ And(out_high, first_high, second_high);
8728 } else if (instruction->IsOr()) {
8729 __ Orr(out_low, first_low, second_low);
8730 __ Orr(out_high, first_high, second_high);
8731 } else {
8732 DCHECK(instruction->IsXor());
8733 __ Eor(out_low, first_low, second_low);
8734 __ Eor(out_high, first_high, second_high);
8735 }
8736 }
8737}
8738
Artem Serovcfbe9132016-10-14 15:58:56 +01008739void InstructionCodeGeneratorARMVIXL::GenerateReferenceLoadOneRegister(
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008740 HInstruction* instruction,
Artem Serovcfbe9132016-10-14 15:58:56 +01008741 Location out,
8742 uint32_t offset,
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008743 Location maybe_temp,
8744 ReadBarrierOption read_barrier_option) {
Artem Serovcfbe9132016-10-14 15:58:56 +01008745 vixl32::Register out_reg = RegisterFrom(out);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008746 if (read_barrier_option == kWithReadBarrier) {
8747 CHECK(kEmitCompilerReadBarrier);
8748 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
8749 if (kUseBakerReadBarrier) {
8750 // Load with fast path based Baker's read barrier.
8751 // /* HeapReference<Object> */ out = *(out + offset)
8752 codegen_->GenerateFieldLoadWithBakerReadBarrier(
8753 instruction, out, out_reg, offset, maybe_temp, /* needs_null_check */ false);
8754 } else {
8755 // Load with slow path based read barrier.
8756 // Save the value of `out` into `maybe_temp` before overwriting it
8757 // in the following move operation, as we will need it for the
8758 // read barrier below.
8759 __ Mov(RegisterFrom(maybe_temp), out_reg);
8760 // /* HeapReference<Object> */ out = *(out + offset)
8761 GetAssembler()->LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
8762 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
8763 }
Artem Serovcfbe9132016-10-14 15:58:56 +01008764 } else {
8765 // Plain load with no read barrier.
8766 // /* HeapReference<Object> */ out = *(out + offset)
8767 GetAssembler()->LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
8768 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
8769 }
8770}
8771
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008772void InstructionCodeGeneratorARMVIXL::GenerateReferenceLoadTwoRegisters(
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008773 HInstruction* instruction,
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008774 Location out,
8775 Location obj,
8776 uint32_t offset,
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008777 Location maybe_temp,
8778 ReadBarrierOption read_barrier_option) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008779 vixl32::Register out_reg = RegisterFrom(out);
8780 vixl32::Register obj_reg = RegisterFrom(obj);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008781 if (read_barrier_option == kWithReadBarrier) {
8782 CHECK(kEmitCompilerReadBarrier);
8783 if (kUseBakerReadBarrier) {
8784 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
8785 // Load with fast path based Baker's read barrier.
8786 // /* HeapReference<Object> */ out = *(obj + offset)
8787 codegen_->GenerateFieldLoadWithBakerReadBarrier(
8788 instruction, out, obj_reg, offset, maybe_temp, /* needs_null_check */ false);
8789 } else {
8790 // Load with slow path based read barrier.
8791 // /* HeapReference<Object> */ out = *(obj + offset)
8792 GetAssembler()->LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
8793 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
8794 }
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008795 } else {
8796 // Plain load with no read barrier.
8797 // /* HeapReference<Object> */ out = *(obj + offset)
8798 GetAssembler()->LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
8799 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
8800 }
8801}
8802
Vladimir Markoca1e0382018-04-11 09:58:41 +00008803void CodeGeneratorARMVIXL::GenerateGcRootFieldLoad(
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008804 HInstruction* instruction,
Scott Wakelinga7812ae2016-10-17 10:03:36 +01008805 Location root,
8806 vixl32::Register obj,
8807 uint32_t offset,
Artem Serovd4cc5b22016-11-04 11:19:09 +00008808 ReadBarrierOption read_barrier_option) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01008809 vixl32::Register root_reg = RegisterFrom(root);
Artem Serovd4cc5b22016-11-04 11:19:09 +00008810 if (read_barrier_option == kWithReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008811 DCHECK(kEmitCompilerReadBarrier);
8812 if (kUseBakerReadBarrier) {
8813 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
Roland Levillainba650a42017-03-06 13:52:32 +00008814 // Baker's read barrier are used.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008815 if (kBakerReadBarrierLinkTimeThunksEnableForGcRoots &&
8816 !Runtime::Current()->UseJitCompilation()) {
Roland Levillain6d729a72017-06-30 18:34:01 +01008817 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in
8818 // the Marking Register) to decide whether we need to enter
8819 // the slow path to mark the GC root.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008820 //
8821 // We use link-time generated thunks for the slow path. That thunk
8822 // checks the reference and jumps to the entrypoint if needed.
8823 //
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008824 // lr = &return_address;
8825 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
Roland Levillain6d729a72017-06-30 18:34:01 +01008826 // if (mr) { // Thread::Current()->GetIsGcMarking()
8827 // goto gc_root_thunk<root_reg>(lr)
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008828 // }
8829 // return_address:
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008830
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008831 UseScratchRegisterScope temps(GetVIXLAssembler());
8832 ExcludeIPAndBakerCcEntrypointRegister(&temps, instruction);
Vladimir Marko88abba22017-05-03 17:09:25 +01008833 bool narrow = CanEmitNarrowLdr(root_reg, obj, offset);
Vladimir Markoca1e0382018-04-11 09:58:41 +00008834 uint32_t custom_data = EncodeBakerReadBarrierGcRootData(root_reg.GetCode(), narrow);
8835 vixl32::Label* bne_label = NewBakerReadBarrierPatch(custom_data);
Roland Levillainba650a42017-03-06 13:52:32 +00008836
Roland Levillain6d729a72017-06-30 18:34:01 +01008837 vixl::EmissionCheckScope guard(GetVIXLAssembler(), 4 * vixl32::kMaxInstructionSizeInBytes);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008838 vixl32::Label return_address;
8839 EmitAdrCode adr(GetVIXLAssembler(), lr, &return_address);
Roland Levillain6d729a72017-06-30 18:34:01 +01008840 __ cmp(mr, Operand(0));
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008841 // Currently the offset is always within range. If that changes,
8842 // we shall have to split the load the same way as for fields.
8843 DCHECK_LT(offset, kReferenceLoadMinFarOffset);
Vladimir Marko88abba22017-05-03 17:09:25 +01008844 ptrdiff_t old_offset = GetVIXLAssembler()->GetBuffer()->GetCursorOffset();
8845 __ ldr(EncodingSize(narrow ? Narrow : Wide), root_reg, MemOperand(obj, offset));
Vladimir Markoca1e0382018-04-11 09:58:41 +00008846 EmitPlaceholderBne(this, bne_label);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008847 __ Bind(&return_address);
Vladimir Marko88abba22017-05-03 17:09:25 +01008848 DCHECK_EQ(old_offset - GetVIXLAssembler()->GetBuffer()->GetCursorOffset(),
8849 narrow ? BAKER_MARK_INTROSPECTION_GC_ROOT_LDR_NARROW_OFFSET
8850 : BAKER_MARK_INTROSPECTION_GC_ROOT_LDR_WIDE_OFFSET);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008851 } else {
Roland Levillain6d729a72017-06-30 18:34:01 +01008852 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in
8853 // the Marking Register) to decide whether we need to enter
8854 // the slow path to mark the GC root.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008855 //
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008856 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
Roland Levillain6d729a72017-06-30 18:34:01 +01008857 // if (mr) { // Thread::Current()->GetIsGcMarking()
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008858 // // Slow path.
Roland Levillain6d729a72017-06-30 18:34:01 +01008859 // entrypoint = Thread::Current()->pReadBarrierMarkReg ## root.reg()
8860 // root = entrypoint(root); // root = ReadBarrier::Mark(root); // Entry point call.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008861 // }
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008862
Roland Levillain6d729a72017-06-30 18:34:01 +01008863 // Slow path marking the GC root `root`. The entrypoint will
8864 // be loaded by the slow path code.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008865 SlowPathCodeARMVIXL* slow_path =
Vladimir Markoca1e0382018-04-11 09:58:41 +00008866 new (GetScopedAllocator()) ReadBarrierMarkSlowPathARMVIXL(instruction, root);
8867 AddSlowPath(slow_path);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008868
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008869 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
8870 GetAssembler()->LoadFromOffset(kLoadWord, root_reg, obj, offset);
8871 static_assert(
8872 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
8873 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
8874 "have different sizes.");
8875 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
8876 "art::mirror::CompressedReference<mirror::Object> and int32_t "
8877 "have different sizes.");
8878
Roland Levillain6d729a72017-06-30 18:34:01 +01008879 __ CompareAndBranchIfNonZero(mr, slow_path->GetEntryLabel());
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008880 __ Bind(slow_path->GetExitLabel());
8881 }
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008882 } else {
8883 // GC root loaded through a slow path for read barriers other
8884 // than Baker's.
8885 // /* GcRoot<mirror::Object>* */ root = obj + offset
8886 __ Add(root_reg, obj, offset);
8887 // /* mirror::Object* */ root = root->Read()
Vladimir Markoca1e0382018-04-11 09:58:41 +00008888 GenerateReadBarrierForRootSlow(instruction, root, root);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008889 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01008890 } else {
8891 // Plain GC root load with no read barrier.
8892 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
8893 GetAssembler()->LoadFromOffset(kLoadWord, root_reg, obj, offset);
8894 // Note that GC roots are not affected by heap poisoning, thus we
8895 // do not have to unpoison `root_reg` here.
8896 }
Vladimir Markoca1e0382018-04-11 09:58:41 +00008897 MaybeGenerateMarkingRegisterCheck(/* code */ 18);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01008898}
8899
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008900void CodeGeneratorARMVIXL::MaybeAddBakerCcEntrypointTempForFields(LocationSummary* locations) {
8901 DCHECK(kEmitCompilerReadBarrier);
8902 DCHECK(kUseBakerReadBarrier);
8903 if (kBakerReadBarrierLinkTimeThunksEnableForFields) {
8904 if (!Runtime::Current()->UseJitCompilation()) {
8905 locations->AddTemp(Location::RegisterLocation(kBakerCcEntrypointRegister.GetCode()));
8906 }
8907 }
8908}
8909
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008910void CodeGeneratorARMVIXL::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
8911 Location ref,
8912 vixl32::Register obj,
8913 uint32_t offset,
8914 Location temp,
8915 bool needs_null_check) {
8916 DCHECK(kEmitCompilerReadBarrier);
8917 DCHECK(kUseBakerReadBarrier);
8918
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008919 if (kBakerReadBarrierLinkTimeThunksEnableForFields &&
8920 !Runtime::Current()->UseJitCompilation()) {
Roland Levillain6d729a72017-06-30 18:34:01 +01008921 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in the
8922 // Marking Register) to decide whether we need to enter the slow
8923 // path to mark the reference. Then, in the slow path, check the
8924 // gray bit in the lock word of the reference's holder (`obj`) to
8925 // decide whether to mark `ref` or not.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008926 //
8927 // We use link-time generated thunks for the slow path. That thunk checks
8928 // the holder and jumps to the entrypoint if needed. If the holder is not
8929 // gray, it creates a fake dependency and returns to the LDR instruction.
8930 //
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008931 // lr = &gray_return_address;
Roland Levillain6d729a72017-06-30 18:34:01 +01008932 // if (mr) { // Thread::Current()->GetIsGcMarking()
8933 // goto field_thunk<holder_reg, base_reg>(lr)
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008934 // }
8935 // not_gray_return_address:
8936 // // Original reference load. If the offset is too large to fit
8937 // // into LDR, we use an adjusted base register here.
Vladimir Marko88abba22017-05-03 17:09:25 +01008938 // HeapReference<mirror::Object> reference = *(obj+offset);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008939 // gray_return_address:
8940
8941 DCHECK_ALIGNED(offset, sizeof(mirror::HeapReference<mirror::Object>));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008942 vixl32::Register ref_reg = RegisterFrom(ref, DataType::Type::kReference);
Vladimir Marko88abba22017-05-03 17:09:25 +01008943 bool narrow = CanEmitNarrowLdr(ref_reg, obj, offset);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008944 vixl32::Register base = obj;
8945 if (offset >= kReferenceLoadMinFarOffset) {
8946 base = RegisterFrom(temp);
8947 DCHECK(!base.Is(kBakerCcEntrypointRegister));
8948 static_assert(IsPowerOfTwo(kReferenceLoadMinFarOffset), "Expecting a power of 2.");
8949 __ Add(base, obj, Operand(offset & ~(kReferenceLoadMinFarOffset - 1u)));
8950 offset &= (kReferenceLoadMinFarOffset - 1u);
Vladimir Marko88abba22017-05-03 17:09:25 +01008951 // Use narrow LDR only for small offsets. Generating narrow encoding LDR for the large
8952 // offsets with `(offset & (kReferenceLoadMinFarOffset - 1u)) < 32u` would most likely
8953 // increase the overall code size when taking the generated thunks into account.
8954 DCHECK(!narrow);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008955 }
8956 UseScratchRegisterScope temps(GetVIXLAssembler());
8957 ExcludeIPAndBakerCcEntrypointRegister(&temps, instruction);
Vladimir Markoca1e0382018-04-11 09:58:41 +00008958 uint32_t custom_data = EncodeBakerReadBarrierFieldData(base.GetCode(), obj.GetCode(), narrow);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008959 vixl32::Label* bne_label = NewBakerReadBarrierPatch(custom_data);
8960
Roland Levillain5daa4952017-07-03 17:23:56 +01008961 {
8962 vixl::EmissionCheckScope guard(
8963 GetVIXLAssembler(),
8964 (kPoisonHeapReferences ? 5u : 4u) * vixl32::kMaxInstructionSizeInBytes);
8965 vixl32::Label return_address;
8966 EmitAdrCode adr(GetVIXLAssembler(), lr, &return_address);
8967 __ cmp(mr, Operand(0));
8968 EmitPlaceholderBne(this, bne_label);
8969 ptrdiff_t old_offset = GetVIXLAssembler()->GetBuffer()->GetCursorOffset();
8970 __ ldr(EncodingSize(narrow ? Narrow : Wide), ref_reg, MemOperand(base, offset));
8971 if (needs_null_check) {
8972 MaybeRecordImplicitNullCheck(instruction);
Vladimir Marko88abba22017-05-03 17:09:25 +01008973 }
Roland Levillain5daa4952017-07-03 17:23:56 +01008974 // Note: We need a specific width for the unpoisoning NEG.
8975 if (kPoisonHeapReferences) {
8976 if (narrow) {
8977 // The only 16-bit encoding is T1 which sets flags outside IT block (i.e. RSBS, not RSB).
8978 __ rsbs(EncodingSize(Narrow), ref_reg, ref_reg, Operand(0));
8979 } else {
8980 __ rsb(EncodingSize(Wide), ref_reg, ref_reg, Operand(0));
8981 }
8982 }
8983 __ Bind(&return_address);
8984 DCHECK_EQ(old_offset - GetVIXLAssembler()->GetBuffer()->GetCursorOffset(),
8985 narrow ? BAKER_MARK_INTROSPECTION_FIELD_LDR_NARROW_OFFSET
8986 : BAKER_MARK_INTROSPECTION_FIELD_LDR_WIDE_OFFSET);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008987 }
Roland Levillain5daa4952017-07-03 17:23:56 +01008988 MaybeGenerateMarkingRegisterCheck(/* code */ 19, /* temp_loc */ LocationFrom(ip));
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008989 return;
8990 }
8991
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008992 // /* HeapReference<Object> */ ref = *(obj + offset)
8993 Location no_index = Location::NoLocation();
8994 ScaleFactor no_scale_factor = TIMES_1;
8995 GenerateReferenceLoadWithBakerReadBarrier(
8996 instruction, ref, obj, offset, no_index, no_scale_factor, temp, needs_null_check);
Roland Levillain6070e882016-11-03 17:51:58 +00008997}
8998
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008999void CodeGeneratorARMVIXL::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
9000 Location ref,
9001 vixl32::Register obj,
9002 uint32_t data_offset,
9003 Location index,
9004 Location temp,
9005 bool needs_null_check) {
9006 DCHECK(kEmitCompilerReadBarrier);
9007 DCHECK(kUseBakerReadBarrier);
9008
9009 static_assert(
9010 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
9011 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01009012 ScaleFactor scale_factor = TIMES_4;
9013
9014 if (kBakerReadBarrierLinkTimeThunksEnableForArrays &&
9015 !Runtime::Current()->UseJitCompilation()) {
Roland Levillain6d729a72017-06-30 18:34:01 +01009016 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in the
9017 // Marking Register) to decide whether we need to enter the slow
9018 // path to mark the reference. Then, in the slow path, check the
9019 // gray bit in the lock word of the reference's holder (`obj`) to
9020 // decide whether to mark `ref` or not.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01009021 //
9022 // We use link-time generated thunks for the slow path. That thunk checks
9023 // the holder and jumps to the entrypoint if needed. If the holder is not
9024 // gray, it creates a fake dependency and returns to the LDR instruction.
9025 //
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01009026 // lr = &gray_return_address;
Roland Levillain6d729a72017-06-30 18:34:01 +01009027 // if (mr) { // Thread::Current()->GetIsGcMarking()
9028 // goto array_thunk<base_reg>(lr)
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01009029 // }
9030 // not_gray_return_address:
9031 // // Original reference load. If the offset is too large to fit
9032 // // into LDR, we use an adjusted base register here.
Vladimir Marko88abba22017-05-03 17:09:25 +01009033 // HeapReference<mirror::Object> reference = data[index];
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01009034 // gray_return_address:
9035
9036 DCHECK(index.IsValid());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009037 vixl32::Register index_reg = RegisterFrom(index, DataType::Type::kInt32);
9038 vixl32::Register ref_reg = RegisterFrom(ref, DataType::Type::kReference);
9039 vixl32::Register data_reg = RegisterFrom(temp, DataType::Type::kInt32); // Raw pointer.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01009040 DCHECK(!data_reg.Is(kBakerCcEntrypointRegister));
9041
9042 UseScratchRegisterScope temps(GetVIXLAssembler());
9043 ExcludeIPAndBakerCcEntrypointRegister(&temps, instruction);
Vladimir Markoca1e0382018-04-11 09:58:41 +00009044 uint32_t custom_data = EncodeBakerReadBarrierArrayData(data_reg.GetCode());
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01009045 vixl32::Label* bne_label = NewBakerReadBarrierPatch(custom_data);
9046
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01009047 __ Add(data_reg, obj, Operand(data_offset));
Roland Levillain5daa4952017-07-03 17:23:56 +01009048 {
9049 vixl::EmissionCheckScope guard(
9050 GetVIXLAssembler(),
9051 (kPoisonHeapReferences ? 5u : 4u) * vixl32::kMaxInstructionSizeInBytes);
9052 vixl32::Label return_address;
9053 EmitAdrCode adr(GetVIXLAssembler(), lr, &return_address);
9054 __ cmp(mr, Operand(0));
9055 EmitPlaceholderBne(this, bne_label);
9056 ptrdiff_t old_offset = GetVIXLAssembler()->GetBuffer()->GetCursorOffset();
9057 __ ldr(ref_reg, MemOperand(data_reg, index_reg, vixl32::LSL, scale_factor));
9058 DCHECK(!needs_null_check); // The thunk cannot handle the null check.
9059 // Note: We need a Wide NEG for the unpoisoning.
9060 if (kPoisonHeapReferences) {
9061 __ rsb(EncodingSize(Wide), ref_reg, ref_reg, Operand(0));
9062 }
9063 __ Bind(&return_address);
9064 DCHECK_EQ(old_offset - GetVIXLAssembler()->GetBuffer()->GetCursorOffset(),
9065 BAKER_MARK_INTROSPECTION_ARRAY_LDR_OFFSET);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01009066 }
Roland Levillain5daa4952017-07-03 17:23:56 +01009067 MaybeGenerateMarkingRegisterCheck(/* code */ 20, /* temp_loc */ LocationFrom(ip));
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01009068 return;
9069 }
9070
Anton Kirilovedb2ac32016-11-30 15:14:10 +00009071 // /* HeapReference<Object> */ ref =
9072 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Anton Kirilovedb2ac32016-11-30 15:14:10 +00009073 GenerateReferenceLoadWithBakerReadBarrier(
9074 instruction, ref, obj, data_offset, index, scale_factor, temp, needs_null_check);
Roland Levillain6070e882016-11-03 17:51:58 +00009075}
9076
Anton Kirilovedb2ac32016-11-30 15:14:10 +00009077void CodeGeneratorARMVIXL::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
9078 Location ref,
9079 vixl32::Register obj,
9080 uint32_t offset,
9081 Location index,
9082 ScaleFactor scale_factor,
9083 Location temp,
Roland Levillainff487002017-03-07 16:50:01 +00009084 bool needs_null_check) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00009085 DCHECK(kEmitCompilerReadBarrier);
9086 DCHECK(kUseBakerReadBarrier);
9087
Roland Levillain6d729a72017-06-30 18:34:01 +01009088 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in the
9089 // Marking Register) to decide whether we need to enter the slow
9090 // path to mark the reference. Then, in the slow path, check the
9091 // gray bit in the lock word of the reference's holder (`obj`) to
9092 // decide whether to mark `ref` or not.
Anton Kirilovedb2ac32016-11-30 15:14:10 +00009093 //
Roland Levillain6d729a72017-06-30 18:34:01 +01009094 // if (mr) { // Thread::Current()->GetIsGcMarking()
Roland Levillainff487002017-03-07 16:50:01 +00009095 // // Slow path.
9096 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
9097 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
9098 // HeapReference<mirror::Object> ref = *src; // Original reference load.
9099 // bool is_gray = (rb_state == ReadBarrier::GrayState());
9100 // if (is_gray) {
Roland Levillain6d729a72017-06-30 18:34:01 +01009101 // entrypoint = Thread::Current()->pReadBarrierMarkReg ## root.reg()
9102 // ref = entrypoint(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
Roland Levillainff487002017-03-07 16:50:01 +00009103 // }
9104 // } else {
9105 // HeapReference<mirror::Object> ref = *src; // Original reference load.
9106 // }
9107
9108 vixl32::Register temp_reg = RegisterFrom(temp);
9109
9110 // Slow path marking the object `ref` when the GC is marking. The
Roland Levillain6d729a72017-06-30 18:34:01 +01009111 // entrypoint will be loaded by the slow path code.
Roland Levillainff487002017-03-07 16:50:01 +00009112 SlowPathCodeARMVIXL* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01009113 new (GetScopedAllocator()) LoadReferenceWithBakerReadBarrierSlowPathARMVIXL(
Roland Levillain6d729a72017-06-30 18:34:01 +01009114 instruction, ref, obj, offset, index, scale_factor, needs_null_check, temp_reg);
Roland Levillainff487002017-03-07 16:50:01 +00009115 AddSlowPath(slow_path);
9116
Roland Levillain6d729a72017-06-30 18:34:01 +01009117 __ CompareAndBranchIfNonZero(mr, slow_path->GetEntryLabel());
Roland Levillainff487002017-03-07 16:50:01 +00009118 // Fast path: the GC is not marking: just load the reference.
9119 GenerateRawReferenceLoad(instruction, ref, obj, offset, index, scale_factor, needs_null_check);
9120 __ Bind(slow_path->GetExitLabel());
Roland Levillain5daa4952017-07-03 17:23:56 +01009121 MaybeGenerateMarkingRegisterCheck(/* code */ 21);
Roland Levillainff487002017-03-07 16:50:01 +00009122}
9123
9124void CodeGeneratorARMVIXL::UpdateReferenceFieldWithBakerReadBarrier(HInstruction* instruction,
9125 Location ref,
9126 vixl32::Register obj,
9127 Location field_offset,
9128 Location temp,
9129 bool needs_null_check,
9130 vixl32::Register temp2) {
9131 DCHECK(kEmitCompilerReadBarrier);
9132 DCHECK(kUseBakerReadBarrier);
9133
Roland Levillain6d729a72017-06-30 18:34:01 +01009134 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in the
9135 // Marking Register) to decide whether we need to enter the slow
9136 // path to update the reference field within `obj`. Then, in the
9137 // slow path, check the gray bit in the lock word of the reference's
9138 // holder (`obj`) to decide whether to mark `ref` and update the
9139 // field or not.
Roland Levillainff487002017-03-07 16:50:01 +00009140 //
Roland Levillain6d729a72017-06-30 18:34:01 +01009141 // if (mr) { // Thread::Current()->GetIsGcMarking()
Roland Levillainba650a42017-03-06 13:52:32 +00009142 // // Slow path.
Roland Levillain54f869e2017-03-06 13:54:11 +00009143 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
9144 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
Roland Levillainff487002017-03-07 16:50:01 +00009145 // HeapReference<mirror::Object> ref = *(obj + field_offset); // Reference load.
Roland Levillain54f869e2017-03-06 13:54:11 +00009146 // bool is_gray = (rb_state == ReadBarrier::GrayState());
9147 // if (is_gray) {
Roland Levillainff487002017-03-07 16:50:01 +00009148 // old_ref = ref;
Roland Levillain6d729a72017-06-30 18:34:01 +01009149 // entrypoint = Thread::Current()->pReadBarrierMarkReg ## root.reg()
9150 // ref = entrypoint(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
Roland Levillainff487002017-03-07 16:50:01 +00009151 // compareAndSwapObject(obj, field_offset, old_ref, ref);
Roland Levillain54f869e2017-03-06 13:54:11 +00009152 // }
Anton Kirilovedb2ac32016-11-30 15:14:10 +00009153 // }
Anton Kirilovedb2ac32016-11-30 15:14:10 +00009154
Anton Kirilovedb2ac32016-11-30 15:14:10 +00009155 vixl32::Register temp_reg = RegisterFrom(temp);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00009156
Roland Levillainff487002017-03-07 16:50:01 +00009157 // Slow path updating the object reference at address `obj + field_offset`
Roland Levillain6d729a72017-06-30 18:34:01 +01009158 // when the GC is marking. The entrypoint will be loaded by the slow path code.
Vladimir Marko174b2e22017-10-12 13:34:49 +01009159 SlowPathCodeARMVIXL* slow_path =
9160 new (GetScopedAllocator()) LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARMVIXL(
Roland Levillainff487002017-03-07 16:50:01 +00009161 instruction,
9162 ref,
9163 obj,
9164 /* offset */ 0u,
9165 /* index */ field_offset,
9166 /* scale_factor */ ScaleFactor::TIMES_1,
9167 needs_null_check,
9168 temp_reg,
Roland Levillain6d729a72017-06-30 18:34:01 +01009169 temp2);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00009170 AddSlowPath(slow_path);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00009171
Roland Levillain6d729a72017-06-30 18:34:01 +01009172 __ CompareAndBranchIfNonZero(mr, slow_path->GetEntryLabel());
Roland Levillainff487002017-03-07 16:50:01 +00009173 // Fast path: the GC is not marking: nothing to do (the field is
9174 // up-to-date, and we don't need to load the reference).
Anton Kirilovedb2ac32016-11-30 15:14:10 +00009175 __ Bind(slow_path->GetExitLabel());
Roland Levillain5daa4952017-07-03 17:23:56 +01009176 MaybeGenerateMarkingRegisterCheck(/* code */ 22);
Roland Levillain844e6532016-11-03 16:09:47 +00009177}
Scott Wakelingfe885462016-09-22 10:24:38 +01009178
Roland Levillainba650a42017-03-06 13:52:32 +00009179void CodeGeneratorARMVIXL::GenerateRawReferenceLoad(HInstruction* instruction,
9180 Location ref,
Vladimir Markoca1e0382018-04-11 09:58:41 +00009181 vixl32::Register obj,
Roland Levillainba650a42017-03-06 13:52:32 +00009182 uint32_t offset,
9183 Location index,
9184 ScaleFactor scale_factor,
9185 bool needs_null_check) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009186 DataType::Type type = DataType::Type::kReference;
Roland Levillainba650a42017-03-06 13:52:32 +00009187 vixl32::Register ref_reg = RegisterFrom(ref, type);
9188
9189 // If needed, vixl::EmissionCheckScope guards are used to ensure
9190 // that no pools are emitted between the load (macro) instruction
9191 // and MaybeRecordImplicitNullCheck.
9192
Scott Wakelingfe885462016-09-22 10:24:38 +01009193 if (index.IsValid()) {
9194 // Load types involving an "index": ArrayGet,
9195 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
9196 // intrinsics.
Roland Levillainba650a42017-03-06 13:52:32 +00009197 // /* HeapReference<mirror::Object> */ ref = *(obj + offset + (index << scale_factor))
Scott Wakelingfe885462016-09-22 10:24:38 +01009198 if (index.IsConstant()) {
9199 size_t computed_offset =
9200 (Int32ConstantFrom(index) << scale_factor) + offset;
Roland Levillainba650a42017-03-06 13:52:32 +00009201 vixl::EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Scott Wakelingfe885462016-09-22 10:24:38 +01009202 GetAssembler()->LoadFromOffset(kLoadWord, ref_reg, obj, computed_offset);
Roland Levillainba650a42017-03-06 13:52:32 +00009203 if (needs_null_check) {
9204 MaybeRecordImplicitNullCheck(instruction);
9205 }
Scott Wakelingfe885462016-09-22 10:24:38 +01009206 } else {
9207 // Handle the special case of the
9208 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
9209 // intrinsics, which use a register pair as index ("long
9210 // offset"), of which only the low part contains data.
9211 vixl32::Register index_reg = index.IsRegisterPair()
9212 ? LowRegisterFrom(index)
9213 : RegisterFrom(index);
9214 UseScratchRegisterScope temps(GetVIXLAssembler());
Roland Levillainba650a42017-03-06 13:52:32 +00009215 vixl32::Register temp = temps.Acquire();
9216 __ Add(temp, obj, Operand(index_reg, ShiftType::LSL, scale_factor));
9217 {
9218 vixl::EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
9219 GetAssembler()->LoadFromOffset(kLoadWord, ref_reg, temp, offset);
9220 if (needs_null_check) {
9221 MaybeRecordImplicitNullCheck(instruction);
9222 }
9223 }
Scott Wakelingfe885462016-09-22 10:24:38 +01009224 }
9225 } else {
Roland Levillainba650a42017-03-06 13:52:32 +00009226 // /* HeapReference<mirror::Object> */ ref = *(obj + offset)
9227 vixl::EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Scott Wakelingfe885462016-09-22 10:24:38 +01009228 GetAssembler()->LoadFromOffset(kLoadWord, ref_reg, obj, offset);
Roland Levillainba650a42017-03-06 13:52:32 +00009229 if (needs_null_check) {
9230 MaybeRecordImplicitNullCheck(instruction);
9231 }
Scott Wakelingfe885462016-09-22 10:24:38 +01009232 }
9233
Roland Levillain844e6532016-11-03 16:09:47 +00009234 // Object* ref = ref_addr->AsMirrorPtr()
9235 GetAssembler()->MaybeUnpoisonHeapReference(ref_reg);
Roland Levillain844e6532016-11-03 16:09:47 +00009236}
9237
Roland Levillain5daa4952017-07-03 17:23:56 +01009238void CodeGeneratorARMVIXL::MaybeGenerateMarkingRegisterCheck(int code, Location temp_loc) {
9239 // The following condition is a compile-time one, so it does not have a run-time cost.
9240 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier && kIsDebugBuild) {
9241 // The following condition is a run-time one; it is executed after the
9242 // previous compile-time test, to avoid penalizing non-debug builds.
9243 if (GetCompilerOptions().EmitRunTimeChecksInDebugMode()) {
9244 UseScratchRegisterScope temps(GetVIXLAssembler());
9245 vixl32::Register temp = temp_loc.IsValid() ? RegisterFrom(temp_loc) : temps.Acquire();
9246 GetAssembler()->GenerateMarkingRegisterCheck(temp,
9247 kMarkingRegisterCheckBreakCodeBaseCode + code);
9248 }
9249 }
9250}
9251
Anton Kirilovedb2ac32016-11-30 15:14:10 +00009252void CodeGeneratorARMVIXL::GenerateReadBarrierSlow(HInstruction* instruction,
9253 Location out,
9254 Location ref,
9255 Location obj,
9256 uint32_t offset,
9257 Location index) {
9258 DCHECK(kEmitCompilerReadBarrier);
9259
9260 // Insert a slow path based read barrier *after* the reference load.
9261 //
9262 // If heap poisoning is enabled, the unpoisoning of the loaded
9263 // reference will be carried out by the runtime within the slow
9264 // path.
9265 //
9266 // Note that `ref` currently does not get unpoisoned (when heap
9267 // poisoning is enabled), which is alright as the `ref` argument is
9268 // not used by the artReadBarrierSlow entry point.
9269 //
9270 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Vladimir Marko174b2e22017-10-12 13:34:49 +01009271 SlowPathCodeARMVIXL* slow_path = new (GetScopedAllocator())
Anton Kirilovedb2ac32016-11-30 15:14:10 +00009272 ReadBarrierForHeapReferenceSlowPathARMVIXL(instruction, out, ref, obj, offset, index);
9273 AddSlowPath(slow_path);
9274
9275 __ B(slow_path->GetEntryLabel());
9276 __ Bind(slow_path->GetExitLabel());
9277}
9278
9279void CodeGeneratorARMVIXL::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
Artem Serov02d37832016-10-25 15:25:33 +01009280 Location out,
Anton Kirilovedb2ac32016-11-30 15:14:10 +00009281 Location ref,
9282 Location obj,
9283 uint32_t offset,
9284 Location index) {
Artem Serov02d37832016-10-25 15:25:33 +01009285 if (kEmitCompilerReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00009286 // Baker's read barriers shall be handled by the fast path
Roland Levillain9983e302017-07-14 14:34:22 +01009287 // (CodeGeneratorARMVIXL::GenerateReferenceLoadWithBakerReadBarrier).
Artem Serov02d37832016-10-25 15:25:33 +01009288 DCHECK(!kUseBakerReadBarrier);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00009289 // If heap poisoning is enabled, unpoisoning will be taken care of
9290 // by the runtime within the slow path.
9291 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Artem Serov02d37832016-10-25 15:25:33 +01009292 } else if (kPoisonHeapReferences) {
9293 GetAssembler()->UnpoisonHeapReference(RegisterFrom(out));
9294 }
9295}
9296
Anton Kirilovedb2ac32016-11-30 15:14:10 +00009297void CodeGeneratorARMVIXL::GenerateReadBarrierForRootSlow(HInstruction* instruction,
9298 Location out,
9299 Location root) {
9300 DCHECK(kEmitCompilerReadBarrier);
9301
9302 // Insert a slow path based read barrier *after* the GC root load.
9303 //
9304 // Note that GC roots are not affected by heap poisoning, so we do
9305 // not need to do anything special for this here.
9306 SlowPathCodeARMVIXL* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01009307 new (GetScopedAllocator()) ReadBarrierForRootSlowPathARMVIXL(instruction, out, root);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00009308 AddSlowPath(slow_path);
9309
9310 __ B(slow_path->GetEntryLabel());
9311 __ Bind(slow_path->GetExitLabel());
9312}
9313
Artem Serov02d37832016-10-25 15:25:33 +01009314// Check if the desired_dispatch_info is supported. If it is, return it,
9315// otherwise return a fall-back info that should be used instead.
9316HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARMVIXL::GetSupportedInvokeStaticOrDirectDispatch(
Artem Serovd4cc5b22016-11-04 11:19:09 +00009317 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +00009318 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Nicolas Geoffraye807ff72017-01-23 09:03:12 +00009319 return desired_dispatch_info;
Artem Serov02d37832016-10-25 15:25:33 +01009320}
9321
Scott Wakelingfe885462016-09-22 10:24:38 +01009322vixl32::Register CodeGeneratorARMVIXL::GetInvokeStaticOrDirectExtraParameter(
9323 HInvokeStaticOrDirect* invoke, vixl32::Register temp) {
9324 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
9325 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
9326 if (!invoke->GetLocations()->Intrinsified()) {
9327 return RegisterFrom(location);
9328 }
9329 // For intrinsics we allow any location, so it may be on the stack.
9330 if (!location.IsRegister()) {
9331 GetAssembler()->LoadFromOffset(kLoadWord, temp, sp, location.GetStackIndex());
9332 return temp;
9333 }
9334 // For register locations, check if the register was saved. If so, get it from the stack.
9335 // Note: There is a chance that the register was saved but not overwritten, so we could
9336 // save one load. However, since this is just an intrinsic slow path we prefer this
9337 // simple and more robust approach rather that trying to determine if that's the case.
9338 SlowPathCode* slow_path = GetCurrentSlowPath();
Scott Wakelingd5cd4972017-02-03 11:38:35 +00009339 if (slow_path != nullptr && slow_path->IsCoreRegisterSaved(RegisterFrom(location).GetCode())) {
Scott Wakelingfe885462016-09-22 10:24:38 +01009340 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(RegisterFrom(location).GetCode());
9341 GetAssembler()->LoadFromOffset(kLoadWord, temp, sp, stack_offset);
9342 return temp;
9343 }
9344 return RegisterFrom(location);
9345}
9346
Vladimir Markod254f5c2017-06-02 15:18:36 +00009347void CodeGeneratorARMVIXL::GenerateStaticOrDirectCall(
Vladimir Markoe7197bf2017-06-02 17:00:23 +01009348 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00009349 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
Scott Wakelingfe885462016-09-22 10:24:38 +01009350 switch (invoke->GetMethodLoadKind()) {
9351 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
9352 uint32_t offset =
9353 GetThreadOffset<kArmPointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
9354 // temp = thread->string_init_entrypoint
Artem Serovd4cc5b22016-11-04 11:19:09 +00009355 GetAssembler()->LoadFromOffset(kLoadWord, RegisterFrom(temp), tr, offset);
9356 break;
9357 }
9358 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
9359 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
9360 break;
Vladimir Marko65979462017-05-19 17:25:12 +01009361 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative: {
9362 DCHECK(GetCompilerOptions().IsBootImage());
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009363 PcRelativePatchInfo* labels = NewBootImageMethodPatch(invoke->GetTargetMethod());
Vladimir Marko65979462017-05-19 17:25:12 +01009364 vixl32::Register temp_reg = RegisterFrom(temp);
9365 EmitMovwMovtPlaceholder(labels, temp_reg);
9366 break;
9367 }
Artem Serovd4cc5b22016-11-04 11:19:09 +00009368 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
9369 __ Mov(RegisterFrom(temp), Operand::From(invoke->GetMethodAddress()));
9370 break;
Vladimir Markob066d432018-01-03 13:14:37 +00009371 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageRelRo: {
Vladimir Markoe47f60c2018-02-21 13:43:28 +00009372 uint32_t boot_image_offset = GetBootImageOffset(invoke);
Vladimir Markob066d432018-01-03 13:14:37 +00009373 PcRelativePatchInfo* labels = NewBootImageRelRoPatch(boot_image_offset);
9374 vixl32::Register temp_reg = RegisterFrom(temp);
9375 EmitMovwMovtPlaceholder(labels, temp_reg);
9376 GetAssembler()->LoadFromOffset(kLoadWord, temp_reg, temp_reg, /* offset*/ 0);
9377 break;
9378 }
Vladimir Marko0eb882b2017-05-15 13:39:18 +01009379 case HInvokeStaticOrDirect::MethodLoadKind::kBssEntry: {
9380 PcRelativePatchInfo* labels = NewMethodBssEntryPatch(
9381 MethodReference(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex()));
9382 vixl32::Register temp_reg = RegisterFrom(temp);
9383 EmitMovwMovtPlaceholder(labels, temp_reg);
9384 GetAssembler()->LoadFromOffset(kLoadWord, temp_reg, temp_reg, /* offset*/ 0);
Scott Wakelingfe885462016-09-22 10:24:38 +01009385 break;
9386 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01009387 case HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall: {
9388 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
9389 return; // No code pointer retrieval; the runtime performs the call directly.
Scott Wakelingfe885462016-09-22 10:24:38 +01009390 }
Scott Wakelingfe885462016-09-22 10:24:38 +01009391 }
9392
Artem Serovd4cc5b22016-11-04 11:19:09 +00009393 switch (invoke->GetCodePtrLocation()) {
9394 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Vladimir Markoe7197bf2017-06-02 17:00:23 +01009395 {
9396 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
9397 ExactAssemblyScope aas(GetVIXLAssembler(),
9398 vixl32::k32BitT32InstructionSizeInBytes,
9399 CodeBufferCheckScope::kMaximumSize);
9400 __ bl(GetFrameEntryLabel());
9401 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
9402 }
Artem Serovd4cc5b22016-11-04 11:19:09 +00009403 break;
Artem Serovd4cc5b22016-11-04 11:19:09 +00009404 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
9405 // LR = callee_method->entry_point_from_quick_compiled_code_
9406 GetAssembler()->LoadFromOffset(
9407 kLoadWord,
9408 lr,
9409 RegisterFrom(callee_method),
9410 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize).Int32Value());
Alexandre Rames374ddf32016-11-04 10:40:49 +00009411 {
Vladimir Markoe7197bf2017-06-02 17:00:23 +01009412 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
Alexandre Rames374ddf32016-11-04 10:40:49 +00009413 // blx in T32 has only 16bit encoding that's why a stricter check for the scope is used.
Artem Serov0fb37192016-12-06 18:13:40 +00009414 ExactAssemblyScope aas(GetVIXLAssembler(),
9415 vixl32::k16BitT32InstructionSizeInBytes,
9416 CodeBufferCheckScope::kExactSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00009417 // LR()
9418 __ blx(lr);
Vladimir Markoe7197bf2017-06-02 17:00:23 +01009419 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Alexandre Rames374ddf32016-11-04 10:40:49 +00009420 }
Artem Serovd4cc5b22016-11-04 11:19:09 +00009421 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01009422 }
9423
Scott Wakelingfe885462016-09-22 10:24:38 +01009424 DCHECK(!IsLeafMethod());
9425}
9426
Vladimir Markoe7197bf2017-06-02 17:00:23 +01009427void CodeGeneratorARMVIXL::GenerateVirtualCall(
9428 HInvokeVirtual* invoke, Location temp_location, SlowPathCode* slow_path) {
Scott Wakelingfe885462016-09-22 10:24:38 +01009429 vixl32::Register temp = RegisterFrom(temp_location);
9430 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
9431 invoke->GetVTableIndex(), kArmPointerSize).Uint32Value();
9432
9433 // Use the calling convention instead of the location of the receiver, as
9434 // intrinsics may have put the receiver in a different register. In the intrinsics
9435 // slow path, the arguments have been moved to the right place, so here we are
9436 // guaranteed that the receiver is the first register of the calling convention.
9437 InvokeDexCallingConventionARMVIXL calling_convention;
9438 vixl32::Register receiver = calling_convention.GetRegisterAt(0);
9439 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Alexandre Rames374ddf32016-11-04 10:40:49 +00009440 {
9441 // Make sure the pc is recorded immediately after the `ldr` instruction.
Artem Serov0fb37192016-12-06 18:13:40 +00009442 ExactAssemblyScope aas(GetVIXLAssembler(),
9443 vixl32::kMaxInstructionSizeInBytes,
9444 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00009445 // /* HeapReference<Class> */ temp = receiver->klass_
9446 __ ldr(temp, MemOperand(receiver, class_offset));
9447 MaybeRecordImplicitNullCheck(invoke);
9448 }
Scott Wakelingfe885462016-09-22 10:24:38 +01009449 // Instead of simply (possibly) unpoisoning `temp` here, we should
9450 // emit a read barrier for the previous class reference load.
9451 // However this is not required in practice, as this is an
9452 // intermediate/temporary reference and because the current
9453 // concurrent copying collector keeps the from-space memory
9454 // intact/accessible until the end of the marking phase (the
9455 // concurrent copying collector may not in the future).
9456 GetAssembler()->MaybeUnpoisonHeapReference(temp);
9457
9458 // temp = temp->GetMethodAt(method_offset);
9459 uint32_t entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(
9460 kArmPointerSize).Int32Value();
9461 GetAssembler()->LoadFromOffset(kLoadWord, temp, temp, method_offset);
9462 // LR = temp->GetEntryPoint();
9463 GetAssembler()->LoadFromOffset(kLoadWord, lr, temp, entry_point);
Vladimir Markoe7197bf2017-06-02 17:00:23 +01009464 {
9465 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
9466 // blx in T32 has only 16bit encoding that's why a stricter check for the scope is used.
9467 ExactAssemblyScope aas(GetVIXLAssembler(),
9468 vixl32::k16BitT32InstructionSizeInBytes,
9469 CodeBufferCheckScope::kExactSize);
9470 // LR();
9471 __ blx(lr);
9472 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
9473 }
Scott Wakelingfe885462016-09-22 10:24:38 +01009474}
9475
Vladimir Markob066d432018-01-03 13:14:37 +00009476CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewBootImageRelRoPatch(
9477 uint32_t boot_image_offset) {
9478 return NewPcRelativePatch(/* dex_file */ nullptr,
9479 boot_image_offset,
9480 &boot_image_method_patches_);
9481}
9482
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009483CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewBootImageMethodPatch(
Vladimir Marko65979462017-05-19 17:25:12 +01009484 MethodReference target_method) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009485 return NewPcRelativePatch(
9486 target_method.dex_file, target_method.index, &boot_image_method_patches_);
Artem Serovd4cc5b22016-11-04 11:19:09 +00009487}
9488
Vladimir Marko0eb882b2017-05-15 13:39:18 +01009489CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewMethodBssEntryPatch(
9490 MethodReference target_method) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009491 return NewPcRelativePatch(
9492 target_method.dex_file, target_method.index, &method_bss_entry_patches_);
Vladimir Marko0eb882b2017-05-15 13:39:18 +01009493}
9494
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009495CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewBootImageTypePatch(
Artem Serovd4cc5b22016-11-04 11:19:09 +00009496 const DexFile& dex_file, dex::TypeIndex type_index) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009497 return NewPcRelativePatch(&dex_file, type_index.index_, &boot_image_type_patches_);
Artem Serovd4cc5b22016-11-04 11:19:09 +00009498}
9499
Vladimir Marko1998cd02017-01-13 13:02:58 +00009500CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewTypeBssEntryPatch(
9501 const DexFile& dex_file, dex::TypeIndex type_index) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009502 return NewPcRelativePatch(&dex_file, type_index.index_, &type_bss_entry_patches_);
Vladimir Marko1998cd02017-01-13 13:02:58 +00009503}
9504
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009505CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewBootImageStringPatch(
Vladimir Marko65979462017-05-19 17:25:12 +01009506 const DexFile& dex_file, dex::StringIndex string_index) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009507 return NewPcRelativePatch(&dex_file, string_index.index_, &boot_image_string_patches_);
Vladimir Marko65979462017-05-19 17:25:12 +01009508}
9509
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01009510CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewStringBssEntryPatch(
9511 const DexFile& dex_file, dex::StringIndex string_index) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009512 return NewPcRelativePatch(&dex_file, string_index.index_, &string_bss_entry_patches_);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01009513}
9514
Artem Serovd4cc5b22016-11-04 11:19:09 +00009515CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewPcRelativePatch(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009516 const DexFile* dex_file, uint32_t offset_or_index, ArenaDeque<PcRelativePatchInfo>* patches) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00009517 patches->emplace_back(dex_file, offset_or_index);
9518 return &patches->back();
9519}
9520
Vladimir Markoca1e0382018-04-11 09:58:41 +00009521vixl32::Label* CodeGeneratorARMVIXL::NewBakerReadBarrierPatch(uint32_t custom_data) {
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01009522 baker_read_barrier_patches_.emplace_back(custom_data);
9523 return &baker_read_barrier_patches_.back().label;
9524}
9525
Artem Serovc5fcb442016-12-02 19:19:58 +00009526VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateBootImageAddressLiteral(uint32_t address) {
Richard Uhlerc52f3032017-03-02 13:45:45 +00009527 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), &uint32_literals_);
Artem Serovc5fcb442016-12-02 19:19:58 +00009528}
9529
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00009530VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateJitStringLiteral(
9531 const DexFile& dex_file,
9532 dex::StringIndex string_index,
9533 Handle<mirror::String> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01009534 ReserveJitStringRoot(StringReference(&dex_file, string_index), handle);
Artem Serovc5fcb442016-12-02 19:19:58 +00009535 return jit_string_patches_.GetOrCreate(
9536 StringReference(&dex_file, string_index),
9537 [this]() {
9538 return GetAssembler()->CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u);
9539 });
9540}
9541
9542VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateJitClassLiteral(const DexFile& dex_file,
9543 dex::TypeIndex type_index,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00009544 Handle<mirror::Class> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01009545 ReserveJitClassRoot(TypeReference(&dex_file, type_index), handle);
Artem Serovc5fcb442016-12-02 19:19:58 +00009546 return jit_class_patches_.GetOrCreate(
9547 TypeReference(&dex_file, type_index),
9548 [this]() {
9549 return GetAssembler()->CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u);
9550 });
9551}
9552
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01009553template <linker::LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
Artem Serovd4cc5b22016-11-04 11:19:09 +00009554inline void CodeGeneratorARMVIXL::EmitPcRelativeLinkerPatches(
9555 const ArenaDeque<PcRelativePatchInfo>& infos,
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01009556 ArenaVector<linker::LinkerPatch>* linker_patches) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00009557 for (const PcRelativePatchInfo& info : infos) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009558 const DexFile* dex_file = info.target_dex_file;
Artem Serovd4cc5b22016-11-04 11:19:09 +00009559 size_t offset_or_index = info.offset_or_index;
9560 DCHECK(info.add_pc_label.IsBound());
9561 uint32_t add_pc_offset = dchecked_integral_cast<uint32_t>(info.add_pc_label.GetLocation());
9562 // Add MOVW patch.
9563 DCHECK(info.movw_label.IsBound());
9564 uint32_t movw_offset = dchecked_integral_cast<uint32_t>(info.movw_label.GetLocation());
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009565 linker_patches->push_back(Factory(movw_offset, dex_file, add_pc_offset, offset_or_index));
Artem Serovd4cc5b22016-11-04 11:19:09 +00009566 // Add MOVT patch.
9567 DCHECK(info.movt_label.IsBound());
9568 uint32_t movt_offset = dchecked_integral_cast<uint32_t>(info.movt_label.GetLocation());
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009569 linker_patches->push_back(Factory(movt_offset, dex_file, add_pc_offset, offset_or_index));
Artem Serovd4cc5b22016-11-04 11:19:09 +00009570 }
9571}
9572
Vladimir Markob066d432018-01-03 13:14:37 +00009573linker::LinkerPatch DataBimgRelRoPatchAdapter(size_t literal_offset,
9574 const DexFile* target_dex_file,
9575 uint32_t pc_insn_offset,
9576 uint32_t boot_image_offset) {
9577 DCHECK(target_dex_file == nullptr); // Unused for DataBimgRelRoPatch(), should be null.
9578 return linker::LinkerPatch::DataBimgRelRoPatch(literal_offset, pc_insn_offset, boot_image_offset);
9579}
9580
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01009581void CodeGeneratorARMVIXL::EmitLinkerPatches(ArenaVector<linker::LinkerPatch>* linker_patches) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00009582 DCHECK(linker_patches->empty());
9583 size_t size =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009584 /* MOVW+MOVT for each entry */ 2u * boot_image_method_patches_.size() +
Vladimir Marko0eb882b2017-05-15 13:39:18 +01009585 /* MOVW+MOVT for each entry */ 2u * method_bss_entry_patches_.size() +
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009586 /* MOVW+MOVT for each entry */ 2u * boot_image_type_patches_.size() +
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01009587 /* MOVW+MOVT for each entry */ 2u * type_bss_entry_patches_.size() +
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009588 /* MOVW+MOVT for each entry */ 2u * boot_image_string_patches_.size() +
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01009589 /* MOVW+MOVT for each entry */ 2u * string_bss_entry_patches_.size() +
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01009590 baker_read_barrier_patches_.size();
Artem Serovd4cc5b22016-11-04 11:19:09 +00009591 linker_patches->reserve(size);
Vladimir Marko65979462017-05-19 17:25:12 +01009592 if (GetCompilerOptions().IsBootImage()) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01009593 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeMethodPatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009594 boot_image_method_patches_, linker_patches);
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01009595 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeTypePatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009596 boot_image_type_patches_, linker_patches);
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01009597 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeStringPatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009598 boot_image_string_patches_, linker_patches);
Vladimir Marko65979462017-05-19 17:25:12 +01009599 } else {
Vladimir Markob066d432018-01-03 13:14:37 +00009600 EmitPcRelativeLinkerPatches<DataBimgRelRoPatchAdapter>(
9601 boot_image_method_patches_, linker_patches);
Vladimir Markoe47f60c2018-02-21 13:43:28 +00009602 DCHECK(boot_image_type_patches_.empty());
9603 DCHECK(boot_image_string_patches_.empty());
Artem Serovd4cc5b22016-11-04 11:19:09 +00009604 }
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01009605 EmitPcRelativeLinkerPatches<linker::LinkerPatch::MethodBssEntryPatch>(
9606 method_bss_entry_patches_, linker_patches);
9607 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeBssEntryPatch>(
9608 type_bss_entry_patches_, linker_patches);
9609 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringBssEntryPatch>(
9610 string_bss_entry_patches_, linker_patches);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01009611 for (const BakerReadBarrierPatchInfo& info : baker_read_barrier_patches_) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01009612 linker_patches->push_back(linker::LinkerPatch::BakerReadBarrierBranchPatch(
9613 info.label.GetLocation(), info.custom_data));
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01009614 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00009615 DCHECK_EQ(size, linker_patches->size());
Artem Serovc5fcb442016-12-02 19:19:58 +00009616}
9617
Vladimir Markoca1e0382018-04-11 09:58:41 +00009618bool CodeGeneratorARMVIXL::NeedsThunkCode(const linker::LinkerPatch& patch) const {
9619 return patch.GetType() == linker::LinkerPatch::Type::kBakerReadBarrierBranch ||
9620 patch.GetType() == linker::LinkerPatch::Type::kCallRelative;
9621}
9622
9623void CodeGeneratorARMVIXL::EmitThunkCode(const linker::LinkerPatch& patch,
9624 /*out*/ ArenaVector<uint8_t>* code,
9625 /*out*/ std::string* debug_name) {
9626 arm::ArmVIXLAssembler assembler(GetGraph()->GetAllocator());
9627 switch (patch.GetType()) {
9628 case linker::LinkerPatch::Type::kCallRelative:
9629 // The thunk just uses the entry point in the ArtMethod. This works even for calls
9630 // to the generic JNI and interpreter trampolines.
9631 assembler.LoadFromOffset(
9632 arm::kLoadWord,
9633 vixl32::pc,
9634 vixl32::r0,
9635 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize).Int32Value());
9636 assembler.GetVIXLAssembler()->Bkpt(0);
9637 if (GetCompilerOptions().GenerateAnyDebugInfo()) {
9638 *debug_name = "MethodCallThunk";
9639 }
9640 break;
9641 case linker::LinkerPatch::Type::kBakerReadBarrierBranch:
9642 DCHECK_EQ(patch.GetBakerCustomValue2(), 0u);
9643 CompileBakerReadBarrierThunk(assembler, patch.GetBakerCustomValue1(), debug_name);
9644 break;
9645 default:
9646 LOG(FATAL) << "Unexpected patch type " << patch.GetType();
9647 UNREACHABLE();
9648 }
9649
9650 // Ensure we emit the literal pool if any.
9651 assembler.FinalizeCode();
9652 code->resize(assembler.CodeSize());
9653 MemoryRegion code_region(code->data(), code->size());
9654 assembler.FinalizeInstructions(code_region);
9655}
9656
Artem Serovc5fcb442016-12-02 19:19:58 +00009657VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateUint32Literal(
9658 uint32_t value,
9659 Uint32ToLiteralMap* map) {
9660 return map->GetOrCreate(
9661 value,
9662 [this, value]() {
9663 return GetAssembler()->CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ value);
9664 });
9665}
9666
Artem Serov2bbc9532016-10-21 11:51:50 +01009667void LocationsBuilderARMVIXL::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
9668 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01009669 new (GetGraph()->GetAllocator()) LocationSummary(instr, LocationSummary::kNoCall);
Artem Serov2bbc9532016-10-21 11:51:50 +01009670 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
9671 Location::RequiresRegister());
9672 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
9673 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
9674 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
9675}
9676
9677void InstructionCodeGeneratorARMVIXL::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
9678 vixl32::Register res = OutputRegister(instr);
9679 vixl32::Register accumulator =
9680 InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
9681 vixl32::Register mul_left =
9682 InputRegisterAt(instr, HMultiplyAccumulate::kInputMulLeftIndex);
9683 vixl32::Register mul_right =
9684 InputRegisterAt(instr, HMultiplyAccumulate::kInputMulRightIndex);
9685
9686 if (instr->GetOpKind() == HInstruction::kAdd) {
9687 __ Mla(res, mul_left, mul_right, accumulator);
9688 } else {
9689 __ Mls(res, mul_left, mul_right, accumulator);
9690 }
9691}
9692
Artem Serov551b28f2016-10-18 19:11:30 +01009693void LocationsBuilderARMVIXL::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
9694 // Nothing to do, this should be removed during prepare for register allocator.
9695 LOG(FATAL) << "Unreachable";
9696}
9697
9698void InstructionCodeGeneratorARMVIXL::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
9699 // Nothing to do, this should be removed during prepare for register allocator.
9700 LOG(FATAL) << "Unreachable";
9701}
9702
9703// Simple implementation of packed switch - generate cascaded compare/jumps.
9704void LocationsBuilderARMVIXL::VisitPackedSwitch(HPackedSwitch* switch_instr) {
9705 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01009706 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Artem Serov551b28f2016-10-18 19:11:30 +01009707 locations->SetInAt(0, Location::RequiresRegister());
9708 if (switch_instr->GetNumEntries() > kPackedSwitchCompareJumpThreshold &&
9709 codegen_->GetAssembler()->GetVIXLAssembler()->IsUsingT32()) {
9710 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the table base.
9711 if (switch_instr->GetStartValue() != 0) {
9712 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the bias.
9713 }
9714 }
9715}
9716
9717// TODO(VIXL): Investigate and reach the parity with old arm codegen.
9718void InstructionCodeGeneratorARMVIXL::VisitPackedSwitch(HPackedSwitch* switch_instr) {
9719 int32_t lower_bound = switch_instr->GetStartValue();
9720 uint32_t num_entries = switch_instr->GetNumEntries();
9721 LocationSummary* locations = switch_instr->GetLocations();
9722 vixl32::Register value_reg = InputRegisterAt(switch_instr, 0);
9723 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
9724
9725 if (num_entries <= kPackedSwitchCompareJumpThreshold ||
9726 !codegen_->GetAssembler()->GetVIXLAssembler()->IsUsingT32()) {
9727 // Create a series of compare/jumps.
Anton Kirilovedb2ac32016-11-30 15:14:10 +00009728 UseScratchRegisterScope temps(GetVIXLAssembler());
Artem Serov551b28f2016-10-18 19:11:30 +01009729 vixl32::Register temp_reg = temps.Acquire();
9730 // Note: It is fine for the below AddConstantSetFlags() using IP register to temporarily store
9731 // the immediate, because IP is used as the destination register. For the other
9732 // AddConstantSetFlags() and GenerateCompareWithImmediate(), the immediate values are constant,
9733 // and they can be encoded in the instruction without making use of IP register.
9734 __ Adds(temp_reg, value_reg, -lower_bound);
9735
9736 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
9737 // Jump to successors[0] if value == lower_bound.
9738 __ B(eq, codegen_->GetLabelOf(successors[0]));
9739 int32_t last_index = 0;
9740 for (; num_entries - last_index > 2; last_index += 2) {
9741 __ Adds(temp_reg, temp_reg, -2);
9742 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
9743 __ B(lo, codegen_->GetLabelOf(successors[last_index + 1]));
9744 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
9745 __ B(eq, codegen_->GetLabelOf(successors[last_index + 2]));
9746 }
9747 if (num_entries - last_index == 2) {
9748 // The last missing case_value.
9749 __ Cmp(temp_reg, 1);
9750 __ B(eq, codegen_->GetLabelOf(successors[last_index + 1]));
9751 }
9752
9753 // And the default for any other value.
9754 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
9755 __ B(codegen_->GetLabelOf(default_block));
9756 }
9757 } else {
9758 // Create a table lookup.
9759 vixl32::Register table_base = RegisterFrom(locations->GetTemp(0));
9760
9761 JumpTableARMVIXL* jump_table = codegen_->CreateJumpTable(switch_instr);
9762
9763 // Remove the bias.
9764 vixl32::Register key_reg;
9765 if (lower_bound != 0) {
9766 key_reg = RegisterFrom(locations->GetTemp(1));
9767 __ Sub(key_reg, value_reg, lower_bound);
9768 } else {
9769 key_reg = value_reg;
9770 }
9771
9772 // Check whether the value is in the table, jump to default block if not.
9773 __ Cmp(key_reg, num_entries - 1);
9774 __ B(hi, codegen_->GetLabelOf(default_block));
9775
Anton Kirilovedb2ac32016-11-30 15:14:10 +00009776 UseScratchRegisterScope temps(GetVIXLAssembler());
Artem Serov551b28f2016-10-18 19:11:30 +01009777 vixl32::Register jump_offset = temps.Acquire();
9778
9779 // Load jump offset from the table.
Scott Wakeling86e9d262017-01-18 15:59:24 +00009780 {
9781 const size_t jump_size = switch_instr->GetNumEntries() * sizeof(int32_t);
9782 ExactAssemblyScope aas(GetVIXLAssembler(),
9783 (vixl32::kMaxInstructionSizeInBytes * 4) + jump_size,
9784 CodeBufferCheckScope::kMaximumSize);
9785 __ adr(table_base, jump_table->GetTableStartLabel());
9786 __ ldr(jump_offset, MemOperand(table_base, key_reg, vixl32::LSL, 2));
Artem Serov551b28f2016-10-18 19:11:30 +01009787
Scott Wakeling86e9d262017-01-18 15:59:24 +00009788 // Jump to target block by branching to table_base(pc related) + offset.
9789 vixl32::Register target_address = table_base;
9790 __ add(target_address, table_base, jump_offset);
9791 __ bx(target_address);
Artem Serov09a940d2016-11-11 16:15:11 +00009792
Scott Wakeling86e9d262017-01-18 15:59:24 +00009793 jump_table->EmitTable(codegen_);
9794 }
Artem Serov551b28f2016-10-18 19:11:30 +01009795 }
9796}
9797
Artem Serov02d37832016-10-25 15:25:33 +01009798// Copy the result of a call into the given target.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009799void CodeGeneratorARMVIXL::MoveFromReturnRegister(Location trg, DataType::Type type) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01009800 if (!trg.IsValid()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009801 DCHECK_EQ(type, DataType::Type::kVoid);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01009802 return;
9803 }
9804
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009805 DCHECK_NE(type, DataType::Type::kVoid);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01009806
Artem Serovd4cc5b22016-11-04 11:19:09 +00009807 Location return_loc = InvokeDexCallingConventionVisitorARMVIXL().GetReturnLocation(type);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01009808 if (return_loc.Equals(trg)) {
9809 return;
9810 }
9811
9812 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
9813 // with the last branch.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009814 if (type == DataType::Type::kInt64) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01009815 TODO_VIXL32(FATAL);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009816 } else if (type == DataType::Type::kFloat64) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01009817 TODO_VIXL32(FATAL);
9818 } else {
9819 // Let the parallel move resolver take care of all of this.
Vladimir Markoca6fff82017-10-03 14:49:14 +01009820 HParallelMove parallel_move(GetGraph()->GetAllocator());
Anton Kirilove28d9ae2016-10-25 18:17:23 +01009821 parallel_move.AddMove(return_loc, trg, type, nullptr);
9822 GetMoveResolver()->EmitNativeCode(&parallel_move);
9823 }
Scott Wakelingfe885462016-09-22 10:24:38 +01009824}
9825
xueliang.zhong8d2c4592016-11-23 17:05:25 +00009826void LocationsBuilderARMVIXL::VisitClassTableGet(HClassTableGet* instruction) {
9827 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01009828 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
xueliang.zhong8d2c4592016-11-23 17:05:25 +00009829 locations->SetInAt(0, Location::RequiresRegister());
9830 locations->SetOut(Location::RequiresRegister());
Artem Serov551b28f2016-10-18 19:11:30 +01009831}
9832
xueliang.zhong8d2c4592016-11-23 17:05:25 +00009833void InstructionCodeGeneratorARMVIXL::VisitClassTableGet(HClassTableGet* instruction) {
9834 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
9835 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
9836 instruction->GetIndex(), kArmPointerSize).SizeValue();
9837 GetAssembler()->LoadFromOffset(kLoadWord,
9838 OutputRegister(instruction),
9839 InputRegisterAt(instruction, 0),
9840 method_offset);
9841 } else {
9842 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
9843 instruction->GetIndex(), kArmPointerSize));
9844 GetAssembler()->LoadFromOffset(kLoadWord,
9845 OutputRegister(instruction),
9846 InputRegisterAt(instruction, 0),
9847 mirror::Class::ImtPtrOffset(kArmPointerSize).Uint32Value());
9848 GetAssembler()->LoadFromOffset(kLoadWord,
9849 OutputRegister(instruction),
9850 OutputRegister(instruction),
9851 method_offset);
9852 }
Artem Serov551b28f2016-10-18 19:11:30 +01009853}
9854
Artem Serovc5fcb442016-12-02 19:19:58 +00009855static void PatchJitRootUse(uint8_t* code,
9856 const uint8_t* roots_data,
9857 VIXLUInt32Literal* literal,
9858 uint64_t index_in_table) {
9859 DCHECK(literal->IsBound());
9860 uint32_t literal_offset = literal->GetLocation();
9861 uintptr_t address =
9862 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
9863 uint8_t* data = code + literal_offset;
9864 reinterpret_cast<uint32_t*>(data)[0] = dchecked_integral_cast<uint32_t>(address);
9865}
9866
9867void CodeGeneratorARMVIXL::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
9868 for (const auto& entry : jit_string_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01009869 const StringReference& string_reference = entry.first;
9870 VIXLUInt32Literal* table_entry_literal = entry.second;
Vladimir Marko174b2e22017-10-12 13:34:49 +01009871 uint64_t index_in_table = GetJitStringRootIndex(string_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01009872 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Artem Serovc5fcb442016-12-02 19:19:58 +00009873 }
9874 for (const auto& entry : jit_class_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01009875 const TypeReference& type_reference = entry.first;
9876 VIXLUInt32Literal* table_entry_literal = entry.second;
Vladimir Marko174b2e22017-10-12 13:34:49 +01009877 uint64_t index_in_table = GetJitClassRootIndex(type_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01009878 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Artem Serovc5fcb442016-12-02 19:19:58 +00009879 }
9880}
9881
Artem Serovd4cc5b22016-11-04 11:19:09 +00009882void CodeGeneratorARMVIXL::EmitMovwMovtPlaceholder(
9883 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels,
9884 vixl32::Register out) {
Artem Serov0fb37192016-12-06 18:13:40 +00009885 ExactAssemblyScope aas(GetVIXLAssembler(),
9886 3 * vixl32::kMaxInstructionSizeInBytes,
9887 CodeBufferCheckScope::kMaximumSize);
Artem Serovd4cc5b22016-11-04 11:19:09 +00009888 // TODO(VIXL): Think about using mov instead of movw.
9889 __ bind(&labels->movw_label);
9890 __ movw(out, /* placeholder */ 0u);
9891 __ bind(&labels->movt_label);
9892 __ movt(out, /* placeholder */ 0u);
9893 __ bind(&labels->add_pc_label);
9894 __ add(out, out, pc);
9895}
9896
Scott Wakelingfe885462016-09-22 10:24:38 +01009897#undef __
9898#undef QUICK_ENTRY_POINT
9899#undef TODO_VIXL32
9900
Vladimir Markoca1e0382018-04-11 09:58:41 +00009901#define __ assembler.GetVIXLAssembler()->
9902
9903static void EmitGrayCheckAndFastPath(ArmVIXLAssembler& assembler,
9904 vixl32::Register base_reg,
9905 vixl32::MemOperand& lock_word,
9906 vixl32::Label* slow_path,
Vladimir Marko7a695052018-04-12 10:26:50 +01009907 int32_t raw_ldr_offset,
9908 vixl32::Label* throw_npe = nullptr) {
Vladimir Markoca1e0382018-04-11 09:58:41 +00009909 // Load the lock word containing the rb_state.
9910 __ Ldr(ip, lock_word);
9911 // Given the numeric representation, it's enough to check the low bit of the rb_state.
9912 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
9913 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
9914 __ Tst(ip, Operand(LockWord::kReadBarrierStateMaskShifted));
9915 __ B(ne, slow_path, /* is_far_target */ false);
Vladimir Marko7a695052018-04-12 10:26:50 +01009916 // To throw NPE, we return to the fast path; the artificial dependence below does not matter.
9917 if (throw_npe != nullptr) {
9918 __ Bind(throw_npe);
9919 }
Vladimir Markoca1e0382018-04-11 09:58:41 +00009920 __ Add(lr, lr, raw_ldr_offset);
9921 // Introduce a dependency on the lock_word including rb_state,
9922 // to prevent load-load reordering, and without using
9923 // a memory barrier (which would be more expensive).
9924 __ Add(base_reg, base_reg, Operand(ip, LSR, 32));
9925 __ Bx(lr); // And return back to the function.
9926 // Note: The fake dependency is unnecessary for the slow path.
9927}
9928
9929// Load the read barrier introspection entrypoint in register `entrypoint`
9930static void LoadReadBarrierMarkIntrospectionEntrypoint(ArmVIXLAssembler& assembler,
9931 vixl32::Register entrypoint) {
9932 // The register where the read barrier introspection entrypoint is loaded
Vladimir Marko7a695052018-04-12 10:26:50 +01009933 // is fixed: `kBakerCcEntrypointRegister` (R4).
Vladimir Markoca1e0382018-04-11 09:58:41 +00009934 DCHECK(entrypoint.Is(kBakerCcEntrypointRegister));
9935 // entrypoint = Thread::Current()->pReadBarrierMarkReg12, i.e. pReadBarrierMarkIntrospection.
9936 DCHECK_EQ(ip.GetCode(), 12u);
9937 const int32_t entry_point_offset =
9938 Thread::ReadBarrierMarkEntryPointsOffset<kArmPointerSize>(ip.GetCode());
9939 __ Ldr(entrypoint, MemOperand(tr, entry_point_offset));
9940}
9941
9942void CodeGeneratorARMVIXL::CompileBakerReadBarrierThunk(ArmVIXLAssembler& assembler,
9943 uint32_t encoded_data,
9944 /*out*/ std::string* debug_name) {
9945 BakerReadBarrierKind kind = BakerReadBarrierKindField::Decode(encoded_data);
9946 switch (kind) {
9947 case BakerReadBarrierKind::kField: {
Vladimir Markoca1e0382018-04-11 09:58:41 +00009948 vixl32::Register base_reg(BakerReadBarrierFirstRegField::Decode(encoded_data));
9949 CheckValidReg(base_reg.GetCode());
9950 vixl32::Register holder_reg(BakerReadBarrierSecondRegField::Decode(encoded_data));
9951 CheckValidReg(holder_reg.GetCode());
9952 BakerReadBarrierWidth width = BakerReadBarrierWidthField::Decode(encoded_data);
9953 UseScratchRegisterScope temps(assembler.GetVIXLAssembler());
9954 temps.Exclude(ip);
Vladimir Marko7a695052018-04-12 10:26:50 +01009955 // If base_reg differs from holder_reg, the offset was too large and we must have emitted
9956 // an explicit null check before the load. Otherwise, for implicit null checks, we need to
9957 // null-check the holder as we do not necessarily do that check before going to the thunk.
9958 vixl32::Label throw_npe_label;
9959 vixl32::Label* throw_npe = nullptr;
9960 if (GetCompilerOptions().GetImplicitNullChecks() && holder_reg.Is(base_reg)) {
9961 throw_npe = &throw_npe_label;
9962 __ CompareAndBranchIfZero(holder_reg, throw_npe, /* is_far_target */ false);
Vladimir Markoca1e0382018-04-11 09:58:41 +00009963 }
Vladimir Marko7a695052018-04-12 10:26:50 +01009964 // Check if the holder is gray and, if not, add fake dependency to the base register
9965 // and return to the LDR instruction to load the reference. Otherwise, use introspection
9966 // to load the reference and call the entrypoint that performs further checks on the
9967 // reference and marks it if needed.
Vladimir Markoca1e0382018-04-11 09:58:41 +00009968 vixl32::Label slow_path;
9969 MemOperand lock_word(holder_reg, mirror::Object::MonitorOffset().Int32Value());
9970 const int32_t raw_ldr_offset = (width == BakerReadBarrierWidth::kWide)
9971 ? BAKER_MARK_INTROSPECTION_FIELD_LDR_WIDE_OFFSET
9972 : BAKER_MARK_INTROSPECTION_FIELD_LDR_NARROW_OFFSET;
Vladimir Marko7a695052018-04-12 10:26:50 +01009973 EmitGrayCheckAndFastPath(
9974 assembler, base_reg, lock_word, &slow_path, raw_ldr_offset, throw_npe);
Vladimir Markoca1e0382018-04-11 09:58:41 +00009975 __ Bind(&slow_path);
9976 const int32_t ldr_offset = /* Thumb state adjustment (LR contains Thumb state). */ -1 +
9977 raw_ldr_offset;
9978 vixl32::Register ep_reg(kBakerCcEntrypointRegister);
9979 LoadReadBarrierMarkIntrospectionEntrypoint(assembler, ep_reg);
9980 if (width == BakerReadBarrierWidth::kWide) {
9981 MemOperand ldr_half_address(lr, ldr_offset + 2);
9982 __ Ldrh(ip, ldr_half_address); // Load the LDR immediate half-word with "Rt | imm12".
9983 __ Ubfx(ip, ip, 0, 12); // Extract the offset imm12.
9984 __ Ldr(ip, MemOperand(base_reg, ip)); // Load the reference.
9985 } else {
9986 MemOperand ldr_address(lr, ldr_offset);
9987 __ Ldrh(ip, ldr_address); // Load the LDR immediate, encoding T1.
9988 __ Add(ep_reg, // Adjust the entrypoint address to the entrypoint
9989 ep_reg, // for narrow LDR.
9990 Operand(BAKER_MARK_INTROSPECTION_FIELD_LDR_NARROW_ENTRYPOINT_OFFSET));
9991 __ Ubfx(ip, ip, 6, 5); // Extract the imm5, i.e. offset / 4.
9992 __ Ldr(ip, MemOperand(base_reg, ip, LSL, 2)); // Load the reference.
9993 }
9994 // Do not unpoison. With heap poisoning enabled, the entrypoint expects a poisoned reference.
9995 __ Bx(ep_reg); // Jump to the entrypoint.
Vladimir Markoca1e0382018-04-11 09:58:41 +00009996 break;
9997 }
9998 case BakerReadBarrierKind::kArray: {
9999 vixl32::Register base_reg(BakerReadBarrierFirstRegField::Decode(encoded_data));
10000 CheckValidReg(base_reg.GetCode());
10001 DCHECK_EQ(kBakerReadBarrierInvalidEncodedReg,
10002 BakerReadBarrierSecondRegField::Decode(encoded_data));
10003 DCHECK(BakerReadBarrierWidthField::Decode(encoded_data) == BakerReadBarrierWidth::kWide);
10004 UseScratchRegisterScope temps(assembler.GetVIXLAssembler());
10005 temps.Exclude(ip);
10006 vixl32::Label slow_path;
10007 int32_t data_offset =
10008 mirror::Array::DataOffset(Primitive::ComponentSize(Primitive::kPrimNot)).Int32Value();
10009 MemOperand lock_word(base_reg, mirror::Object::MonitorOffset().Int32Value() - data_offset);
10010 DCHECK_LT(lock_word.GetOffsetImmediate(), 0);
10011 const int32_t raw_ldr_offset = BAKER_MARK_INTROSPECTION_ARRAY_LDR_OFFSET;
10012 EmitGrayCheckAndFastPath(assembler, base_reg, lock_word, &slow_path, raw_ldr_offset);
10013 __ Bind(&slow_path);
10014 const int32_t ldr_offset = /* Thumb state adjustment (LR contains Thumb state). */ -1 +
10015 raw_ldr_offset;
10016 MemOperand ldr_address(lr, ldr_offset + 2);
10017 __ Ldrb(ip, ldr_address); // Load the LDR (register) byte with "00 | imm2 | Rm",
10018 // i.e. Rm+32 because the scale in imm2 is 2.
10019 vixl32::Register ep_reg(kBakerCcEntrypointRegister);
10020 LoadReadBarrierMarkIntrospectionEntrypoint(assembler, ep_reg);
10021 __ Bfi(ep_reg, ip, 3, 6); // Insert ip to the entrypoint address to create
10022 // a switch case target based on the index register.
10023 __ Mov(ip, base_reg); // Move the base register to ip0.
10024 __ Bx(ep_reg); // Jump to the entrypoint's array switch case.
10025 break;
10026 }
10027 case BakerReadBarrierKind::kGcRoot: {
10028 // Check if the reference needs to be marked and if so (i.e. not null, not marked yet
10029 // and it does not have a forwarding address), call the correct introspection entrypoint;
10030 // otherwise return the reference (or the extracted forwarding address).
10031 // There is no gray bit check for GC roots.
10032 vixl32::Register root_reg(BakerReadBarrierFirstRegField::Decode(encoded_data));
10033 CheckValidReg(root_reg.GetCode());
10034 DCHECK_EQ(kBakerReadBarrierInvalidEncodedReg,
10035 BakerReadBarrierSecondRegField::Decode(encoded_data));
10036 BakerReadBarrierWidth width = BakerReadBarrierWidthField::Decode(encoded_data);
10037 UseScratchRegisterScope temps(assembler.GetVIXLAssembler());
10038 temps.Exclude(ip);
10039 vixl32::Label return_label, not_marked, forwarding_address;
10040 __ CompareAndBranchIfZero(root_reg, &return_label, /* is_far_target */ false);
10041 MemOperand lock_word(root_reg, mirror::Object::MonitorOffset().Int32Value());
10042 __ Ldr(ip, lock_word);
10043 __ Tst(ip, LockWord::kMarkBitStateMaskShifted);
10044 __ B(eq, &not_marked);
10045 __ Bind(&return_label);
10046 __ Bx(lr);
10047 __ Bind(&not_marked);
10048 static_assert(LockWord::kStateShift == 30 && LockWord::kStateForwardingAddress == 3,
10049 "To use 'CMP ip, #modified-immediate; BHS', we need the lock word state in "
10050 " the highest bits and the 'forwarding address' state to have all bits set");
10051 __ Cmp(ip, Operand(0xc0000000));
10052 __ B(hs, &forwarding_address);
10053 vixl32::Register ep_reg(kBakerCcEntrypointRegister);
10054 LoadReadBarrierMarkIntrospectionEntrypoint(assembler, ep_reg);
10055 // Adjust the art_quick_read_barrier_mark_introspection address in kBakerCcEntrypointRegister
10056 // to art_quick_read_barrier_mark_introspection_gc_roots.
10057 int32_t entrypoint_offset = (width == BakerReadBarrierWidth::kWide)
10058 ? BAKER_MARK_INTROSPECTION_GC_ROOT_LDR_WIDE_ENTRYPOINT_OFFSET
10059 : BAKER_MARK_INTROSPECTION_GC_ROOT_LDR_NARROW_ENTRYPOINT_OFFSET;
10060 __ Add(ep_reg, ep_reg, Operand(entrypoint_offset));
10061 __ Mov(ip, root_reg);
10062 __ Bx(ep_reg);
10063 __ Bind(&forwarding_address);
10064 __ Lsl(root_reg, ip, LockWord::kForwardingAddressShift);
10065 __ Bx(lr);
10066 break;
10067 }
10068 default:
10069 LOG(FATAL) << "Unexpected kind: " << static_cast<uint32_t>(kind);
10070 UNREACHABLE();
10071 }
10072
10073 if (GetCompilerOptions().GenerateAnyDebugInfo()) {
10074 std::ostringstream oss;
10075 oss << "BakerReadBarrierThunk";
10076 switch (kind) {
10077 case BakerReadBarrierKind::kField:
10078 oss << "Field";
10079 if (BakerReadBarrierWidthField::Decode(encoded_data) == BakerReadBarrierWidth::kWide) {
10080 oss << "Wide";
10081 }
10082 oss << "_r" << BakerReadBarrierFirstRegField::Decode(encoded_data)
10083 << "_r" << BakerReadBarrierSecondRegField::Decode(encoded_data);
10084 break;
10085 case BakerReadBarrierKind::kArray:
10086 oss << "Array_r" << BakerReadBarrierFirstRegField::Decode(encoded_data);
10087 DCHECK_EQ(kBakerReadBarrierInvalidEncodedReg,
10088 BakerReadBarrierSecondRegField::Decode(encoded_data));
10089 DCHECK(BakerReadBarrierWidthField::Decode(encoded_data) == BakerReadBarrierWidth::kWide);
10090 break;
10091 case BakerReadBarrierKind::kGcRoot:
10092 oss << "GcRoot";
10093 if (BakerReadBarrierWidthField::Decode(encoded_data) == BakerReadBarrierWidth::kWide) {
10094 oss << "Wide";
10095 }
10096 oss << "_r" << BakerReadBarrierFirstRegField::Decode(encoded_data);
10097 DCHECK_EQ(kBakerReadBarrierInvalidEncodedReg,
10098 BakerReadBarrierSecondRegField::Decode(encoded_data));
10099 break;
10100 }
10101 *debug_name = oss.str();
10102 }
10103}
10104
10105#undef __
10106
Scott Wakelingfe885462016-09-22 10:24:38 +010010107} // namespace arm
10108} // namespace art