blob: ebf8d218b124662a2c0bd0c449e984867148261f [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"
22#include "code_generator_utils.h"
23#include "common_arm.h"
24#include "compiled_method.h"
25#include "entrypoints/quick/quick_entrypoints.h"
26#include "gc/accounting/card_table.h"
Anton Kirilov5ec62182016-10-13 20:16:02 +010027#include "intrinsics_arm_vixl.h"
Vladimir Markoeee1c0e2017-04-21 17:58:41 +010028#include "linker/arm/relative_patcher_thumb2.h"
Scott Wakelingfe885462016-09-22 10:24:38 +010029#include "mirror/array-inl.h"
30#include "mirror/class-inl.h"
31#include "thread.h"
32#include "utils/arm/assembler_arm_vixl.h"
33#include "utils/arm/managed_register_arm.h"
34#include "utils/assembler.h"
35#include "utils/stack_checks.h"
36
37namespace art {
38namespace arm {
39
40namespace vixl32 = vixl::aarch32;
41using namespace vixl32; // NOLINT(build/namespaces)
42
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +010043using helpers::DRegisterFrom;
Scott Wakelingfe885462016-09-22 10:24:38 +010044using helpers::DWARFReg;
Scott Wakelinga7812ae2016-10-17 10:03:36 +010045using helpers::HighDRegisterFrom;
46using helpers::HighRegisterFrom;
Donghui Bai426b49c2016-11-08 14:55:38 +080047using helpers::InputDRegisterAt;
Scott Wakelingfe885462016-09-22 10:24:38 +010048using helpers::InputOperandAt;
Scott Wakelingc34dba72016-10-03 10:14:44 +010049using helpers::InputRegister;
Scott Wakelinga7812ae2016-10-17 10:03:36 +010050using helpers::InputRegisterAt;
Scott Wakelingfe885462016-09-22 10:24:38 +010051using helpers::InputSRegisterAt;
Anton Kirilov644032c2016-12-06 17:51:43 +000052using helpers::InputVRegister;
Scott Wakelinga7812ae2016-10-17 10:03:36 +010053using helpers::InputVRegisterAt;
Scott Wakelingb77051e2016-11-21 19:46:00 +000054using helpers::Int32ConstantFrom;
Anton Kirilov644032c2016-12-06 17:51:43 +000055using helpers::Int64ConstantFrom;
Scott Wakelinga7812ae2016-10-17 10:03:36 +010056using helpers::LocationFrom;
57using helpers::LowRegisterFrom;
58using helpers::LowSRegisterFrom;
Donghui Bai426b49c2016-11-08 14:55:38 +080059using helpers::OperandFrom;
Scott Wakelinga7812ae2016-10-17 10:03:36 +010060using helpers::OutputRegister;
61using helpers::OutputSRegister;
62using helpers::OutputVRegister;
63using helpers::RegisterFrom;
64using helpers::SRegisterFrom;
Anton Kirilov644032c2016-12-06 17:51:43 +000065using helpers::Uint64ConstantFrom;
Scott Wakelingfe885462016-09-22 10:24:38 +010066
Artem Serov0fb37192016-12-06 18:13:40 +000067using vixl::ExactAssemblyScope;
68using vixl::CodeBufferCheckScope;
69
Scott Wakelingfe885462016-09-22 10:24:38 +010070using RegisterList = vixl32::RegisterList;
71
72static bool ExpectedPairLayout(Location location) {
73 // We expected this for both core and fpu register pairs.
74 return ((location.low() & 1) == 0) && (location.low() + 1 == location.high());
75}
Artem Serovd4cc5b22016-11-04 11:19:09 +000076// Use a local definition to prevent copying mistakes.
77static constexpr size_t kArmWordSize = static_cast<size_t>(kArmPointerSize);
78static constexpr size_t kArmBitsPerWord = kArmWordSize * kBitsPerByte;
Anton Kirilove28d9ae2016-10-25 18:17:23 +010079static constexpr int kCurrentMethodStackOffset = 0;
Artem Serov551b28f2016-10-18 19:11:30 +010080static constexpr uint32_t kPackedSwitchCompareJumpThreshold = 7;
Scott Wakelingfe885462016-09-22 10:24:38 +010081
Vladimir Markoeee1c0e2017-04-21 17:58:41 +010082// Reference load (except object array loads) is using LDR Rt, [Rn, #offset] which can handle
83// offset < 4KiB. For offsets >= 4KiB, the load shall be emitted as two or more instructions.
84// For the Baker read barrier implementation using link-generated thunks we need to split
85// the offset explicitly.
86constexpr uint32_t kReferenceLoadMinFarOffset = 4 * KB;
87
88// Flags controlling the use of link-time generated thunks for Baker read barriers.
89constexpr bool kBakerReadBarrierLinkTimeThunksEnableForFields = true;
90constexpr bool kBakerReadBarrierLinkTimeThunksEnableForArrays = true;
91constexpr bool kBakerReadBarrierLinkTimeThunksEnableForGcRoots = true;
92
93// The reserved entrypoint register for link-time generated thunks.
94const vixl32::Register kBakerCcEntrypointRegister = r4;
95
Scott Wakelingfe885462016-09-22 10:24:38 +010096#ifdef __
97#error "ARM Codegen VIXL macro-assembler macro already defined."
98#endif
99
Scott Wakelingfe885462016-09-22 10:24:38 +0100100// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
101#define __ down_cast<CodeGeneratorARMVIXL*>(codegen)->GetVIXLAssembler()-> // NOLINT
102#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kArmPointerSize, x).Int32Value()
103
104// Marker that code is yet to be, and must, be implemented.
105#define TODO_VIXL32(level) LOG(level) << __PRETTY_FUNCTION__ << " unimplemented "
106
Vladimir Markoeee1c0e2017-04-21 17:58:41 +0100107static inline void ExcludeIPAndBakerCcEntrypointRegister(UseScratchRegisterScope* temps,
108 HInstruction* instruction) {
109 DCHECK(temps->IsAvailable(ip));
110 temps->Exclude(ip);
111 DCHECK(!temps->IsAvailable(kBakerCcEntrypointRegister));
112 DCHECK_EQ(kBakerCcEntrypointRegister.GetCode(),
113 linker::Thumb2RelativePatcher::kBakerCcEntrypointRegister);
114 DCHECK_NE(instruction->GetLocations()->GetTempCount(), 0u);
115 DCHECK(RegisterFrom(instruction->GetLocations()->GetTemp(
116 instruction->GetLocations()->GetTempCount() - 1u)).Is(kBakerCcEntrypointRegister));
117}
118
119static inline void EmitPlaceholderBne(CodeGeneratorARMVIXL* codegen, vixl32::Label* patch_label) {
120 ExactAssemblyScope eas(codegen->GetVIXLAssembler(), kMaxInstructionSizeInBytes);
121 __ bind(patch_label);
122 vixl32::Label placeholder_label;
123 __ b(ne, EncodingSize(Wide), &placeholder_label); // Placeholder, patched at link-time.
124 __ bind(&placeholder_label);
125}
126
Vladimir Marko88abba22017-05-03 17:09:25 +0100127static inline bool CanEmitNarrowLdr(vixl32::Register rt, vixl32::Register rn, uint32_t offset) {
128 return rt.IsLow() && rn.IsLow() && offset < 32u;
129}
130
Vladimir Markoeee1c0e2017-04-21 17:58:41 +0100131class EmitAdrCode {
132 public:
133 EmitAdrCode(ArmVIXLMacroAssembler* assembler, vixl32::Register rd, vixl32::Label* label)
134 : assembler_(assembler), rd_(rd), label_(label) {
135 ExactAssemblyScope aas(assembler, kMaxInstructionSizeInBytes);
136 adr_location_ = assembler->GetCursorOffset();
137 assembler->adr(EncodingSize(Wide), rd, label);
138 }
139
140 ~EmitAdrCode() {
141 DCHECK(label_->IsBound());
142 // The ADR emitted by the assembler does not set the Thumb mode bit we need.
143 // TODO: Maybe extend VIXL to allow ADR for return address?
144 uint8_t* raw_adr = assembler_->GetBuffer()->GetOffsetAddress<uint8_t*>(adr_location_);
145 // Expecting ADR encoding T3 with `(offset & 1) == 0`.
146 DCHECK_EQ(raw_adr[1] & 0xfbu, 0xf2u); // Check bits 24-31, except 26.
147 DCHECK_EQ(raw_adr[0] & 0xffu, 0x0fu); // Check bits 16-23.
148 DCHECK_EQ(raw_adr[3] & 0x8fu, rd_.GetCode()); // Check bits 8-11 and 15.
149 DCHECK_EQ(raw_adr[2] & 0x01u, 0x00u); // Check bit 0, i.e. the `offset & 1`.
150 // Add the Thumb mode bit.
151 raw_adr[2] |= 0x01u;
152 }
153
154 private:
155 ArmVIXLMacroAssembler* const assembler_;
156 vixl32::Register rd_;
157 vixl32::Label* const label_;
158 int32_t adr_location_;
159};
160
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100161// SaveLiveRegisters and RestoreLiveRegisters from SlowPathCodeARM operate on sets of S registers,
162// for each live D registers they treat two corresponding S registers as live ones.
163//
164// Two following functions (SaveContiguousSRegisterList, RestoreContiguousSRegisterList) build
165// from a list of contiguous S registers a list of contiguous D registers (processing first/last
166// S registers corner cases) and save/restore this new list treating them as D registers.
167// - decreasing code size
168// - avoiding hazards on Cortex-A57, when a pair of S registers for an actual live D register is
169// restored and then used in regular non SlowPath code as D register.
170//
171// For the following example (v means the S register is live):
172// D names: | D0 | D1 | D2 | D4 | ...
173// S names: | S0 | S1 | S2 | S3 | S4 | S5 | S6 | S7 | ...
174// Live? | | v | v | v | v | v | v | | ...
175//
176// S1 and S6 will be saved/restored independently; D registers list (D1, D2) will be processed
177// as D registers.
178//
179// TODO(VIXL): All this code should be unnecessary once the VIXL AArch32 backend provides helpers
180// for lists of floating-point registers.
181static size_t SaveContiguousSRegisterList(size_t first,
182 size_t last,
183 CodeGenerator* codegen,
184 size_t stack_offset) {
185 static_assert(kSRegSizeInBytes == kArmWordSize, "Broken assumption on reg/word sizes.");
186 static_assert(kDRegSizeInBytes == 2 * kArmWordSize, "Broken assumption on reg/word sizes.");
187 DCHECK_LE(first, last);
188 if ((first == last) && (first == 0)) {
189 __ Vstr(vixl32::SRegister(first), MemOperand(sp, stack_offset));
190 return stack_offset + kSRegSizeInBytes;
191 }
192 if (first % 2 == 1) {
193 __ Vstr(vixl32::SRegister(first++), MemOperand(sp, stack_offset));
194 stack_offset += kSRegSizeInBytes;
195 }
196
197 bool save_last = false;
198 if (last % 2 == 0) {
199 save_last = true;
200 --last;
201 }
202
203 if (first < last) {
204 vixl32::DRegister d_reg = vixl32::DRegister(first / 2);
205 DCHECK_EQ((last - first + 1) % 2, 0u);
206 size_t number_of_d_regs = (last - first + 1) / 2;
207
208 if (number_of_d_regs == 1) {
209 __ Vstr(d_reg, MemOperand(sp, stack_offset));
210 } else if (number_of_d_regs > 1) {
211 UseScratchRegisterScope temps(down_cast<CodeGeneratorARMVIXL*>(codegen)->GetVIXLAssembler());
212 vixl32::Register base = sp;
213 if (stack_offset != 0) {
214 base = temps.Acquire();
Scott Wakelingb77051e2016-11-21 19:46:00 +0000215 __ Add(base, sp, Operand::From(stack_offset));
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100216 }
217 __ Vstm(F64, base, NO_WRITE_BACK, DRegisterList(d_reg, number_of_d_regs));
218 }
219 stack_offset += number_of_d_regs * kDRegSizeInBytes;
220 }
221
222 if (save_last) {
223 __ Vstr(vixl32::SRegister(last + 1), MemOperand(sp, stack_offset));
224 stack_offset += kSRegSizeInBytes;
225 }
226
227 return stack_offset;
228}
229
230static size_t RestoreContiguousSRegisterList(size_t first,
231 size_t last,
232 CodeGenerator* codegen,
233 size_t stack_offset) {
234 static_assert(kSRegSizeInBytes == kArmWordSize, "Broken assumption on reg/word sizes.");
235 static_assert(kDRegSizeInBytes == 2 * kArmWordSize, "Broken assumption on reg/word sizes.");
236 DCHECK_LE(first, last);
237 if ((first == last) && (first == 0)) {
238 __ Vldr(vixl32::SRegister(first), MemOperand(sp, stack_offset));
239 return stack_offset + kSRegSizeInBytes;
240 }
241 if (first % 2 == 1) {
242 __ Vldr(vixl32::SRegister(first++), MemOperand(sp, stack_offset));
243 stack_offset += kSRegSizeInBytes;
244 }
245
246 bool restore_last = false;
247 if (last % 2 == 0) {
248 restore_last = true;
249 --last;
250 }
251
252 if (first < last) {
253 vixl32::DRegister d_reg = vixl32::DRegister(first / 2);
254 DCHECK_EQ((last - first + 1) % 2, 0u);
255 size_t number_of_d_regs = (last - first + 1) / 2;
256 if (number_of_d_regs == 1) {
257 __ Vldr(d_reg, MemOperand(sp, stack_offset));
258 } else if (number_of_d_regs > 1) {
259 UseScratchRegisterScope temps(down_cast<CodeGeneratorARMVIXL*>(codegen)->GetVIXLAssembler());
260 vixl32::Register base = sp;
261 if (stack_offset != 0) {
262 base = temps.Acquire();
Scott Wakelingb77051e2016-11-21 19:46:00 +0000263 __ Add(base, sp, Operand::From(stack_offset));
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100264 }
265 __ Vldm(F64, base, NO_WRITE_BACK, DRegisterList(d_reg, number_of_d_regs));
266 }
267 stack_offset += number_of_d_regs * kDRegSizeInBytes;
268 }
269
270 if (restore_last) {
271 __ Vldr(vixl32::SRegister(last + 1), MemOperand(sp, stack_offset));
272 stack_offset += kSRegSizeInBytes;
273 }
274
275 return stack_offset;
276}
277
278void SlowPathCodeARMVIXL::SaveLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
279 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
280 size_t orig_offset = stack_offset;
281
282 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ true);
283 for (uint32_t i : LowToHighBits(core_spills)) {
284 // If the register holds an object, update the stack mask.
285 if (locations->RegisterContainsObject(i)) {
286 locations->SetStackBit(stack_offset / kVRegSize);
287 }
288 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
289 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
290 saved_core_stack_offsets_[i] = stack_offset;
291 stack_offset += kArmWordSize;
292 }
293
294 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
295 arm_codegen->GetAssembler()->StoreRegisterList(core_spills, orig_offset);
296
297 uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ false);
298 orig_offset = stack_offset;
299 for (uint32_t i : LowToHighBits(fp_spills)) {
300 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
301 saved_fpu_stack_offsets_[i] = stack_offset;
302 stack_offset += kArmWordSize;
303 }
304
305 stack_offset = orig_offset;
306 while (fp_spills != 0u) {
307 uint32_t begin = CTZ(fp_spills);
308 uint32_t tmp = fp_spills + (1u << begin);
309 fp_spills &= tmp; // Clear the contiguous range of 1s.
310 uint32_t end = (tmp == 0u) ? 32u : CTZ(tmp); // CTZ(0) is undefined.
311 stack_offset = SaveContiguousSRegisterList(begin, end - 1, codegen, stack_offset);
312 }
313 DCHECK_LE(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
314}
315
316void SlowPathCodeARMVIXL::RestoreLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
317 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
318 size_t orig_offset = stack_offset;
319
320 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ true);
321 for (uint32_t i : LowToHighBits(core_spills)) {
322 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
323 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
324 stack_offset += kArmWordSize;
325 }
326
327 // TODO(VIXL): Check the coherency of stack_offset after this with a test.
328 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
329 arm_codegen->GetAssembler()->LoadRegisterList(core_spills, orig_offset);
330
331 uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ false);
332 while (fp_spills != 0u) {
333 uint32_t begin = CTZ(fp_spills);
334 uint32_t tmp = fp_spills + (1u << begin);
335 fp_spills &= tmp; // Clear the contiguous range of 1s.
336 uint32_t end = (tmp == 0u) ? 32u : CTZ(tmp); // CTZ(0) is undefined.
337 stack_offset = RestoreContiguousSRegisterList(begin, end - 1, codegen, stack_offset);
338 }
339 DCHECK_LE(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
340}
341
342class NullCheckSlowPathARMVIXL : public SlowPathCodeARMVIXL {
343 public:
344 explicit NullCheckSlowPathARMVIXL(HNullCheck* instruction) : SlowPathCodeARMVIXL(instruction) {}
345
346 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
347 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
348 __ Bind(GetEntryLabel());
349 if (instruction_->CanThrowIntoCatchBlock()) {
350 // Live registers will be restored in the catch block if caught.
351 SaveLiveRegisters(codegen, instruction_->GetLocations());
352 }
353 arm_codegen->InvokeRuntime(kQuickThrowNullPointer,
354 instruction_,
355 instruction_->GetDexPc(),
356 this);
357 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
358 }
359
360 bool IsFatal() const OVERRIDE { return true; }
361
362 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathARMVIXL"; }
363
364 private:
365 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARMVIXL);
366};
367
Scott Wakelingfe885462016-09-22 10:24:38 +0100368class DivZeroCheckSlowPathARMVIXL : public SlowPathCodeARMVIXL {
369 public:
370 explicit DivZeroCheckSlowPathARMVIXL(HDivZeroCheck* instruction)
371 : SlowPathCodeARMVIXL(instruction) {}
372
373 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100374 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
Scott Wakelingfe885462016-09-22 10:24:38 +0100375 __ Bind(GetEntryLabel());
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100376 arm_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Scott Wakelingfe885462016-09-22 10:24:38 +0100377 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
378 }
379
380 bool IsFatal() const OVERRIDE { return true; }
381
382 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathARMVIXL"; }
383
384 private:
385 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARMVIXL);
386};
387
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100388class SuspendCheckSlowPathARMVIXL : public SlowPathCodeARMVIXL {
389 public:
390 SuspendCheckSlowPathARMVIXL(HSuspendCheck* instruction, HBasicBlock* successor)
391 : SlowPathCodeARMVIXL(instruction), successor_(successor) {}
392
393 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
394 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
395 __ Bind(GetEntryLabel());
396 arm_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
397 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
398 if (successor_ == nullptr) {
399 __ B(GetReturnLabel());
400 } else {
401 __ B(arm_codegen->GetLabelOf(successor_));
402 }
403 }
404
405 vixl32::Label* GetReturnLabel() {
406 DCHECK(successor_ == nullptr);
407 return &return_label_;
408 }
409
410 HBasicBlock* GetSuccessor() const {
411 return successor_;
412 }
413
414 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathARMVIXL"; }
415
416 private:
417 // If not null, the block to branch to after the suspend check.
418 HBasicBlock* const successor_;
419
420 // If `successor_` is null, the label to branch to after the suspend check.
421 vixl32::Label return_label_;
422
423 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARMVIXL);
424};
425
Scott Wakelingc34dba72016-10-03 10:14:44 +0100426class BoundsCheckSlowPathARMVIXL : public SlowPathCodeARMVIXL {
427 public:
428 explicit BoundsCheckSlowPathARMVIXL(HBoundsCheck* instruction)
429 : SlowPathCodeARMVIXL(instruction) {}
430
431 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
432 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
433 LocationSummary* locations = instruction_->GetLocations();
434
435 __ Bind(GetEntryLabel());
436 if (instruction_->CanThrowIntoCatchBlock()) {
437 // Live registers will be restored in the catch block if caught.
438 SaveLiveRegisters(codegen, instruction_->GetLocations());
439 }
440 // We're moving two locations to locations that could overlap, so we need a parallel
441 // move resolver.
442 InvokeRuntimeCallingConventionARMVIXL calling_convention;
443 codegen->EmitParallelMoves(
444 locations->InAt(0),
445 LocationFrom(calling_convention.GetRegisterAt(0)),
446 Primitive::kPrimInt,
447 locations->InAt(1),
448 LocationFrom(calling_convention.GetRegisterAt(1)),
449 Primitive::kPrimInt);
450 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
451 ? kQuickThrowStringBounds
452 : kQuickThrowArrayBounds;
453 arm_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
454 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
455 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
456 }
457
458 bool IsFatal() const OVERRIDE { return true; }
459
460 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathARMVIXL"; }
461
462 private:
463 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARMVIXL);
464};
465
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100466class LoadClassSlowPathARMVIXL : public SlowPathCodeARMVIXL {
467 public:
468 LoadClassSlowPathARMVIXL(HLoadClass* cls, HInstruction* at, uint32_t dex_pc, bool do_clinit)
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000469 : SlowPathCodeARMVIXL(at), cls_(cls), dex_pc_(dex_pc), do_clinit_(do_clinit) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100470 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
471 }
472
473 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000474 LocationSummary* locations = instruction_->GetLocations();
Vladimir Markoea4c1262017-02-06 19:59:33 +0000475 Location out = locations->Out();
476 constexpr bool call_saves_everything_except_r0 = (!kUseReadBarrier || kUseBakerReadBarrier);
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100477
478 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
479 __ Bind(GetEntryLabel());
480 SaveLiveRegisters(codegen, locations);
481
482 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Vladimir Markoea4c1262017-02-06 19:59:33 +0000483 // For HLoadClass/kBssEntry/kSaveEverything, make sure we preserve the address of the entry.
484 DCHECK_EQ(instruction_->IsLoadClass(), cls_ == instruction_);
485 bool is_load_class_bss_entry =
486 (cls_ == instruction_) && (cls_->GetLoadKind() == HLoadClass::LoadKind::kBssEntry);
487 vixl32::Register entry_address;
488 if (is_load_class_bss_entry && call_saves_everything_except_r0) {
489 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
490 // In the unlucky case that the `temp` is R0, we preserve the address in `out` across
491 // the kSaveEverything call.
492 bool temp_is_r0 = temp.Is(calling_convention.GetRegisterAt(0));
493 entry_address = temp_is_r0 ? RegisterFrom(out) : temp;
494 DCHECK(!entry_address.Is(calling_convention.GetRegisterAt(0)));
495 if (temp_is_r0) {
496 __ Mov(entry_address, temp);
497 }
498 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000499 dex::TypeIndex type_index = cls_->GetTypeIndex();
500 __ Mov(calling_convention.GetRegisterAt(0), type_index.index_);
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100501 QuickEntrypointEnum entrypoint = do_clinit_ ? kQuickInitializeStaticStorage
502 : kQuickInitializeType;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000503 arm_codegen->InvokeRuntime(entrypoint, instruction_, dex_pc_, this);
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100504 if (do_clinit_) {
505 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
506 } else {
507 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
508 }
509
Vladimir Markoea4c1262017-02-06 19:59:33 +0000510 // For HLoadClass/kBssEntry, store the resolved Class to the BSS entry.
511 if (is_load_class_bss_entry) {
512 if (call_saves_everything_except_r0) {
513 // The class entry address was preserved in `entry_address` thanks to kSaveEverything.
514 __ Str(r0, MemOperand(entry_address));
515 } else {
516 // For non-Baker read barrier, we need to re-calculate the address of the string entry.
517 UseScratchRegisterScope temps(
518 down_cast<CodeGeneratorARMVIXL*>(codegen)->GetVIXLAssembler());
519 vixl32::Register temp = temps.Acquire();
520 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
521 arm_codegen->NewTypeBssEntryPatch(cls_->GetDexFile(), type_index);
522 arm_codegen->EmitMovwMovtPlaceholder(labels, temp);
523 __ Str(r0, MemOperand(temp));
524 }
525 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100526 // Move the class to the desired location.
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100527 if (out.IsValid()) {
528 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
529 arm_codegen->Move32(locations->Out(), LocationFrom(r0));
530 }
531 RestoreLiveRegisters(codegen, locations);
532 __ B(GetExitLabel());
533 }
534
535 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathARMVIXL"; }
536
537 private:
538 // The class this slow path will load.
539 HLoadClass* const cls_;
540
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100541 // The dex PC of `at_`.
542 const uint32_t dex_pc_;
543
544 // Whether to initialize the class.
545 const bool do_clinit_;
546
547 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARMVIXL);
548};
549
Artem Serovd4cc5b22016-11-04 11:19:09 +0000550class LoadStringSlowPathARMVIXL : public SlowPathCodeARMVIXL {
551 public:
552 explicit LoadStringSlowPathARMVIXL(HLoadString* instruction)
553 : SlowPathCodeARMVIXL(instruction) {}
554
555 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Vladimir Markoea4c1262017-02-06 19:59:33 +0000556 DCHECK(instruction_->IsLoadString());
557 DCHECK_EQ(instruction_->AsLoadString()->GetLoadKind(), HLoadString::LoadKind::kBssEntry);
Artem Serovd4cc5b22016-11-04 11:19:09 +0000558 LocationSummary* locations = instruction_->GetLocations();
559 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
560 HLoadString* load = instruction_->AsLoadString();
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000561 const dex::StringIndex string_index = load->GetStringIndex();
Artem Serovd4cc5b22016-11-04 11:19:09 +0000562 vixl32::Register out = OutputRegister(load);
Artem Serovd4cc5b22016-11-04 11:19:09 +0000563 constexpr bool call_saves_everything_except_r0 = (!kUseReadBarrier || kUseBakerReadBarrier);
564
565 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
566 __ Bind(GetEntryLabel());
567 SaveLiveRegisters(codegen, locations);
568
569 InvokeRuntimeCallingConventionARMVIXL calling_convention;
570 // In the unlucky case that the `temp` is R0, we preserve the address in `out` across
Vladimir Markoea4c1262017-02-06 19:59:33 +0000571 // the kSaveEverything call.
572 vixl32::Register entry_address;
573 if (call_saves_everything_except_r0) {
574 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
575 bool temp_is_r0 = (temp.Is(calling_convention.GetRegisterAt(0)));
576 entry_address = temp_is_r0 ? out : temp;
577 DCHECK(!entry_address.Is(calling_convention.GetRegisterAt(0)));
578 if (temp_is_r0) {
579 __ Mov(entry_address, temp);
580 }
Artem Serovd4cc5b22016-11-04 11:19:09 +0000581 }
582
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000583 __ Mov(calling_convention.GetRegisterAt(0), string_index.index_);
Artem Serovd4cc5b22016-11-04 11:19:09 +0000584 arm_codegen->InvokeRuntime(kQuickResolveString, instruction_, instruction_->GetDexPc(), this);
585 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
586
587 // Store the resolved String to the .bss entry.
588 if (call_saves_everything_except_r0) {
589 // The string entry address was preserved in `entry_address` thanks to kSaveEverything.
590 __ Str(r0, MemOperand(entry_address));
591 } else {
592 // For non-Baker read barrier, we need to re-calculate the address of the string entry.
Vladimir Markoea4c1262017-02-06 19:59:33 +0000593 UseScratchRegisterScope temps(
594 down_cast<CodeGeneratorARMVIXL*>(codegen)->GetVIXLAssembler());
595 vixl32::Register temp = temps.Acquire();
Artem Serovd4cc5b22016-11-04 11:19:09 +0000596 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
597 arm_codegen->NewPcRelativeStringPatch(load->GetDexFile(), string_index);
Vladimir Markoea4c1262017-02-06 19:59:33 +0000598 arm_codegen->EmitMovwMovtPlaceholder(labels, temp);
599 __ Str(r0, MemOperand(temp));
Artem Serovd4cc5b22016-11-04 11:19:09 +0000600 }
601
602 arm_codegen->Move32(locations->Out(), LocationFrom(r0));
603 RestoreLiveRegisters(codegen, locations);
604
605 __ B(GetExitLabel());
606 }
607
608 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathARMVIXL"; }
609
610 private:
611 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARMVIXL);
612};
613
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100614class TypeCheckSlowPathARMVIXL : public SlowPathCodeARMVIXL {
615 public:
616 TypeCheckSlowPathARMVIXL(HInstruction* instruction, bool is_fatal)
617 : SlowPathCodeARMVIXL(instruction), is_fatal_(is_fatal) {}
618
619 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
620 LocationSummary* locations = instruction_->GetLocations();
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100621 DCHECK(instruction_->IsCheckCast()
622 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
623
624 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
625 __ Bind(GetEntryLabel());
626
627 if (!is_fatal_) {
Artem Serovcfbe9132016-10-14 15:58:56 +0100628 SaveLiveRegisters(codegen, locations);
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100629 }
630
631 // We're moving two locations to locations that could overlap, so we need a parallel
632 // move resolver.
633 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100634
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800635 codegen->EmitParallelMoves(locations->InAt(0),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800636 LocationFrom(calling_convention.GetRegisterAt(0)),
637 Primitive::kPrimNot,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800638 locations->InAt(1),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800639 LocationFrom(calling_convention.GetRegisterAt(1)),
640 Primitive::kPrimNot);
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100641 if (instruction_->IsInstanceOf()) {
Artem Serovcfbe9132016-10-14 15:58:56 +0100642 arm_codegen->InvokeRuntime(kQuickInstanceofNonTrivial,
643 instruction_,
644 instruction_->GetDexPc(),
645 this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800646 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Artem Serovcfbe9132016-10-14 15:58:56 +0100647 arm_codegen->Move32(locations->Out(), LocationFrom(r0));
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100648 } else {
649 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800650 arm_codegen->InvokeRuntime(kQuickCheckInstanceOf,
651 instruction_,
652 instruction_->GetDexPc(),
653 this);
654 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100655 }
656
657 if (!is_fatal_) {
Artem Serovcfbe9132016-10-14 15:58:56 +0100658 RestoreLiveRegisters(codegen, locations);
659 __ B(GetExitLabel());
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100660 }
661 }
662
663 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathARMVIXL"; }
664
665 bool IsFatal() const OVERRIDE { return is_fatal_; }
666
667 private:
668 const bool is_fatal_;
669
670 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARMVIXL);
671};
672
Scott Wakelingc34dba72016-10-03 10:14:44 +0100673class DeoptimizationSlowPathARMVIXL : public SlowPathCodeARMVIXL {
674 public:
675 explicit DeoptimizationSlowPathARMVIXL(HDeoptimize* instruction)
676 : SlowPathCodeARMVIXL(instruction) {}
677
678 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
679 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
680 __ Bind(GetEntryLabel());
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100681 LocationSummary* locations = instruction_->GetLocations();
682 SaveLiveRegisters(codegen, locations);
683 InvokeRuntimeCallingConventionARMVIXL calling_convention;
684 __ Mov(calling_convention.GetRegisterAt(0),
685 static_cast<uint32_t>(instruction_->AsDeoptimize()->GetDeoptimizationKind()));
686
Scott Wakelingc34dba72016-10-03 10:14:44 +0100687 arm_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100688 CheckEntrypointTypes<kQuickDeoptimize, void, DeoptimizationKind>();
Scott Wakelingc34dba72016-10-03 10:14:44 +0100689 }
690
691 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathARMVIXL"; }
692
693 private:
694 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARMVIXL);
695};
696
697class ArraySetSlowPathARMVIXL : public SlowPathCodeARMVIXL {
698 public:
699 explicit ArraySetSlowPathARMVIXL(HInstruction* instruction) : SlowPathCodeARMVIXL(instruction) {}
700
701 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
702 LocationSummary* locations = instruction_->GetLocations();
703 __ Bind(GetEntryLabel());
704 SaveLiveRegisters(codegen, locations);
705
706 InvokeRuntimeCallingConventionARMVIXL calling_convention;
707 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
708 parallel_move.AddMove(
709 locations->InAt(0),
710 LocationFrom(calling_convention.GetRegisterAt(0)),
711 Primitive::kPrimNot,
712 nullptr);
713 parallel_move.AddMove(
714 locations->InAt(1),
715 LocationFrom(calling_convention.GetRegisterAt(1)),
716 Primitive::kPrimInt,
717 nullptr);
718 parallel_move.AddMove(
719 locations->InAt(2),
720 LocationFrom(calling_convention.GetRegisterAt(2)),
721 Primitive::kPrimNot,
722 nullptr);
723 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
724
725 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
726 arm_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
727 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
728 RestoreLiveRegisters(codegen, locations);
729 __ B(GetExitLabel());
730 }
731
732 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathARMVIXL"; }
733
734 private:
735 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathARMVIXL);
736};
737
Roland Levillain54f869e2017-03-06 13:54:11 +0000738// Abstract base class for read barrier slow paths marking a reference
739// `ref`.
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000740//
Roland Levillain54f869e2017-03-06 13:54:11 +0000741// Argument `entrypoint` must be a register location holding the read
742// barrier marking runtime entry point to be invoked.
743class ReadBarrierMarkSlowPathBaseARMVIXL : public SlowPathCodeARMVIXL {
744 protected:
745 ReadBarrierMarkSlowPathBaseARMVIXL(HInstruction* instruction, Location ref, Location entrypoint)
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000746 : SlowPathCodeARMVIXL(instruction), ref_(ref), entrypoint_(entrypoint) {
747 DCHECK(kEmitCompilerReadBarrier);
748 }
749
Roland Levillain54f869e2017-03-06 13:54:11 +0000750 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathBaseARMVIXL"; }
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000751
Roland Levillain54f869e2017-03-06 13:54:11 +0000752 // Generate assembly code calling the read barrier marking runtime
753 // entry point (ReadBarrierMarkRegX).
754 void GenerateReadBarrierMarkRuntimeCall(CodeGenerator* codegen) {
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000755 vixl32::Register ref_reg = RegisterFrom(ref_);
Roland Levillain47b3ab22017-02-27 14:31:35 +0000756
Roland Levillain47b3ab22017-02-27 14:31:35 +0000757 // No need to save live registers; it's taken care of by the
758 // entrypoint. Also, there is no need to update the stack mask,
759 // as this runtime call will not trigger a garbage collection.
760 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
761 DCHECK(!ref_reg.Is(sp));
762 DCHECK(!ref_reg.Is(lr));
763 DCHECK(!ref_reg.Is(pc));
764 // IP is used internally by the ReadBarrierMarkRegX entry point
765 // as a temporary, it cannot be the entry point's input/output.
766 DCHECK(!ref_reg.Is(ip));
767 DCHECK(ref_reg.IsRegister()) << ref_reg;
768 // "Compact" slow path, saving two moves.
769 //
770 // Instead of using the standard runtime calling convention (input
771 // and output in R0):
772 //
773 // R0 <- ref
774 // R0 <- ReadBarrierMark(R0)
775 // ref <- R0
776 //
777 // we just use rX (the register containing `ref`) as input and output
778 // of a dedicated entrypoint:
779 //
780 // rX <- ReadBarrierMarkRegX(rX)
781 //
782 if (entrypoint_.IsValid()) {
783 arm_codegen->ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction_, this);
784 __ Blx(RegisterFrom(entrypoint_));
785 } else {
Roland Levillain54f869e2017-03-06 13:54:11 +0000786 // Entrypoint is not already loaded, load from the thread.
Roland Levillain47b3ab22017-02-27 14:31:35 +0000787 int32_t entry_point_offset =
788 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmPointerSize>(ref_reg.GetCode());
789 // This runtime call does not require a stack map.
790 arm_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
791 }
Roland Levillain47b3ab22017-02-27 14:31:35 +0000792 }
793
Roland Levillain47b3ab22017-02-27 14:31:35 +0000794 // The location (register) of the marked object reference.
795 const Location ref_;
796
797 // The location of the entrypoint if already loaded.
798 const Location entrypoint_;
799
Roland Levillain54f869e2017-03-06 13:54:11 +0000800 private:
801 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathBaseARMVIXL);
Roland Levillain47b3ab22017-02-27 14:31:35 +0000802};
803
Scott Wakelingc34dba72016-10-03 10:14:44 +0100804// Slow path marking an object reference `ref` during a read
805// barrier. The field `obj.field` in the object `obj` holding this
Roland Levillain54f869e2017-03-06 13:54:11 +0000806// reference does not get updated by this slow path after marking.
Roland Levillain47b3ab22017-02-27 14:31:35 +0000807//
Scott Wakelingc34dba72016-10-03 10:14:44 +0100808// This means that after the execution of this slow path, `ref` will
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000809// always be up-to-date, but `obj.field` may not; i.e., after the
810// flip, `ref` will be a to-space reference, but `obj.field` will
811// probably still be a from-space reference (unless it gets updated by
812// another thread, or if another thread installed another object
813// reference (different from `ref`) in `obj.field`).
Roland Levillainba650a42017-03-06 13:52:32 +0000814//
815// If `entrypoint` is a valid location it is assumed to already be
816// holding the entrypoint. The case where the entrypoint is passed in
Roland Levillain54f869e2017-03-06 13:54:11 +0000817// is when the decision to mark is based on whether the GC is marking.
818class ReadBarrierMarkSlowPathARMVIXL : public ReadBarrierMarkSlowPathBaseARMVIXL {
Roland Levillain47b3ab22017-02-27 14:31:35 +0000819 public:
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000820 ReadBarrierMarkSlowPathARMVIXL(HInstruction* instruction,
821 Location ref,
822 Location entrypoint = Location::NoLocation())
Roland Levillain54f869e2017-03-06 13:54:11 +0000823 : ReadBarrierMarkSlowPathBaseARMVIXL(instruction, ref, entrypoint) {
Roland Levillain47b3ab22017-02-27 14:31:35 +0000824 DCHECK(kEmitCompilerReadBarrier);
825 }
826
Roland Levillain47b3ab22017-02-27 14:31:35 +0000827 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathARMVIXL"; }
828
829 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
830 LocationSummary* locations = instruction_->GetLocations();
Roland Levillain54f869e2017-03-06 13:54:11 +0000831 DCHECK(locations->CanCall());
832 DCHECK(ref_.IsRegister()) << ref_;
833 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_.reg())) << ref_.reg();
834 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
835 << "Unexpected instruction in read barrier marking slow path: "
836 << instruction_->DebugName();
837
838 __ Bind(GetEntryLabel());
839 GenerateReadBarrierMarkRuntimeCall(codegen);
840 __ B(GetExitLabel());
841 }
842
843 private:
844 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathARMVIXL);
845};
846
847// Slow path loading `obj`'s lock word, loading a reference from
848// object `*(obj + offset + (index << scale_factor))` into `ref`, and
849// marking `ref` if `obj` is gray according to the lock word (Baker
850// read barrier). The field `obj.field` in the object `obj` holding
851// this reference does not get updated by this slow path after marking
852// (see LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARMVIXL
853// below for that).
854//
855// This means that after the execution of this slow path, `ref` will
856// always be up-to-date, but `obj.field` may not; i.e., after the
857// flip, `ref` will be a to-space reference, but `obj.field` will
858// probably still be a from-space reference (unless it gets updated by
859// another thread, or if another thread installed another object
860// reference (different from `ref`) in `obj.field`).
861//
862// Argument `entrypoint` must be a register location holding the read
863// barrier marking runtime entry point to be invoked.
864class LoadReferenceWithBakerReadBarrierSlowPathARMVIXL : public ReadBarrierMarkSlowPathBaseARMVIXL {
865 public:
866 LoadReferenceWithBakerReadBarrierSlowPathARMVIXL(HInstruction* instruction,
867 Location ref,
868 vixl32::Register obj,
869 uint32_t offset,
870 Location index,
871 ScaleFactor scale_factor,
872 bool needs_null_check,
873 vixl32::Register temp,
874 Location entrypoint)
875 : ReadBarrierMarkSlowPathBaseARMVIXL(instruction, ref, entrypoint),
876 obj_(obj),
877 offset_(offset),
878 index_(index),
879 scale_factor_(scale_factor),
880 needs_null_check_(needs_null_check),
881 temp_(temp) {
882 DCHECK(kEmitCompilerReadBarrier);
883 DCHECK(kUseBakerReadBarrier);
884 }
885
Roland Levillain47b3ab22017-02-27 14:31:35 +0000886 const char* GetDescription() const OVERRIDE {
Roland Levillain54f869e2017-03-06 13:54:11 +0000887 return "LoadReferenceWithBakerReadBarrierSlowPathARMVIXL";
Roland Levillain47b3ab22017-02-27 14:31:35 +0000888 }
889
890 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
891 LocationSummary* locations = instruction_->GetLocations();
892 vixl32::Register ref_reg = RegisterFrom(ref_);
893 DCHECK(locations->CanCall());
894 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg.GetCode())) << ref_reg;
Roland Levillain47b3ab22017-02-27 14:31:35 +0000895 DCHECK(instruction_->IsInstanceFieldGet() ||
896 instruction_->IsStaticFieldGet() ||
897 instruction_->IsArrayGet() ||
898 instruction_->IsArraySet() ||
Roland Levillain47b3ab22017-02-27 14:31:35 +0000899 instruction_->IsInstanceOf() ||
900 instruction_->IsCheckCast() ||
901 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
902 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
903 << "Unexpected instruction in read barrier marking slow path: "
904 << instruction_->DebugName();
905 // The read barrier instrumentation of object ArrayGet
906 // instructions does not support the HIntermediateAddress
907 // instruction.
908 DCHECK(!(instruction_->IsArrayGet() &&
909 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
910
Roland Levillain54f869e2017-03-06 13:54:11 +0000911 // Temporary register `temp_`, used to store the lock word, must
912 // not be IP, as we may use it to emit the reference load (in the
913 // call to GenerateRawReferenceLoad below), and we need the lock
914 // word to still be in `temp_` after the reference load.
915 DCHECK(!temp_.Is(ip));
916
Roland Levillain47b3ab22017-02-27 14:31:35 +0000917 __ Bind(GetEntryLabel());
Roland Levillain54f869e2017-03-06 13:54:11 +0000918
919 // When using MaybeGenerateReadBarrierSlow, the read barrier call is
920 // inserted after the original load. However, in fast path based
921 // Baker's read barriers, we need to perform the load of
922 // mirror::Object::monitor_ *before* the original reference load.
923 // This load-load ordering is required by the read barrier.
Roland Levillainff487002017-03-07 16:50:01 +0000924 // The slow path (for Baker's algorithm) should look like:
Roland Levillain54f869e2017-03-06 13:54:11 +0000925 //
926 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
927 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
928 // HeapReference<mirror::Object> ref = *src; // Original reference load.
929 // bool is_gray = (rb_state == ReadBarrier::GrayState());
930 // if (is_gray) {
931 // ref = entrypoint(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
932 // }
933 //
934 // Note: the original implementation in ReadBarrier::Barrier is
935 // slightly more complex as it performs additional checks that we do
936 // not do here for performance reasons.
937
Roland Levillain47b3ab22017-02-27 14:31:35 +0000938 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
Roland Levillain54f869e2017-03-06 13:54:11 +0000939
940 // /* int32_t */ monitor = obj->monitor_
941 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
942 arm_codegen->GetAssembler()->LoadFromOffset(kLoadWord, temp_, obj_, monitor_offset);
943 if (needs_null_check_) {
944 codegen->MaybeRecordImplicitNullCheck(instruction_);
Roland Levillain47b3ab22017-02-27 14:31:35 +0000945 }
Roland Levillain54f869e2017-03-06 13:54:11 +0000946 // /* LockWord */ lock_word = LockWord(monitor)
947 static_assert(sizeof(LockWord) == sizeof(int32_t),
948 "art::LockWord and int32_t have different sizes.");
949
950 // Introduce a dependency on the lock_word including the rb_state,
951 // which shall prevent load-load reordering without using
952 // a memory barrier (which would be more expensive).
953 // `obj` is unchanged by this operation, but its value now depends
954 // on `temp`.
955 __ Add(obj_, obj_, Operand(temp_, ShiftType::LSR, 32));
956
957 // The actual reference load.
958 // A possible implicit null check has already been handled above.
959 arm_codegen->GenerateRawReferenceLoad(
960 instruction_, ref_, obj_, offset_, index_, scale_factor_, /* needs_null_check */ false);
961
962 // Mark the object `ref` when `obj` is gray.
963 //
964 // if (rb_state == ReadBarrier::GrayState())
965 // ref = ReadBarrier::Mark(ref);
966 //
967 // Given the numeric representation, it's enough to check the low bit of the
968 // rb_state. We do that by shifting the bit out of the lock word with LSRS
969 // which can be a 16-bit instruction unlike the TST immediate.
970 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
971 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
972 __ Lsrs(temp_, temp_, LockWord::kReadBarrierStateShift + 1);
973 __ B(cc, GetExitLabel()); // Carry flag is the last bit shifted out by LSRS.
974 GenerateReadBarrierMarkRuntimeCall(codegen);
975
Roland Levillain47b3ab22017-02-27 14:31:35 +0000976 __ B(GetExitLabel());
977 }
978
979 private:
Roland Levillain54f869e2017-03-06 13:54:11 +0000980 // The register containing the object holding the marked object reference field.
981 vixl32::Register obj_;
982 // The offset, index and scale factor to access the reference in `obj_`.
983 uint32_t offset_;
984 Location index_;
985 ScaleFactor scale_factor_;
986 // Is a null check required?
987 bool needs_null_check_;
988 // A temporary register used to hold the lock word of `obj_`.
989 vixl32::Register temp_;
Roland Levillain47b3ab22017-02-27 14:31:35 +0000990
Roland Levillain54f869e2017-03-06 13:54:11 +0000991 DISALLOW_COPY_AND_ASSIGN(LoadReferenceWithBakerReadBarrierSlowPathARMVIXL);
Roland Levillain47b3ab22017-02-27 14:31:35 +0000992};
993
Roland Levillain54f869e2017-03-06 13:54:11 +0000994// Slow path loading `obj`'s lock word, loading a reference from
995// object `*(obj + offset + (index << scale_factor))` into `ref`, and
996// marking `ref` if `obj` is gray according to the lock word (Baker
997// read barrier). If needed, this slow path also atomically updates
998// the field `obj.field` in the object `obj` holding this reference
999// after marking (contrary to
1000// LoadReferenceWithBakerReadBarrierSlowPathARMVIXL above, which never
1001// tries to update `obj.field`).
Roland Levillain47b3ab22017-02-27 14:31:35 +00001002//
1003// This means that after the execution of this slow path, both `ref`
1004// and `obj.field` will be up-to-date; i.e., after the flip, both will
1005// hold the same to-space reference (unless another thread installed
1006// another object reference (different from `ref`) in `obj.field`).
Roland Levillainba650a42017-03-06 13:52:32 +00001007//
Roland Levillain54f869e2017-03-06 13:54:11 +00001008//
1009// Argument `entrypoint` must be a register location holding the read
1010// barrier marking runtime entry point to be invoked.
1011class LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARMVIXL
1012 : public ReadBarrierMarkSlowPathBaseARMVIXL {
Roland Levillain47b3ab22017-02-27 14:31:35 +00001013 public:
Roland Levillain54f869e2017-03-06 13:54:11 +00001014 LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARMVIXL(HInstruction* instruction,
1015 Location ref,
1016 vixl32::Register obj,
1017 uint32_t offset,
1018 Location index,
1019 ScaleFactor scale_factor,
1020 bool needs_null_check,
1021 vixl32::Register temp1,
1022 vixl32::Register temp2,
1023 Location entrypoint)
1024 : ReadBarrierMarkSlowPathBaseARMVIXL(instruction, ref, entrypoint),
Roland Levillain47b3ab22017-02-27 14:31:35 +00001025 obj_(obj),
Roland Levillain54f869e2017-03-06 13:54:11 +00001026 offset_(offset),
1027 index_(index),
1028 scale_factor_(scale_factor),
1029 needs_null_check_(needs_null_check),
Roland Levillain47b3ab22017-02-27 14:31:35 +00001030 temp1_(temp1),
Roland Levillain54f869e2017-03-06 13:54:11 +00001031 temp2_(temp2) {
Roland Levillain47b3ab22017-02-27 14:31:35 +00001032 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain54f869e2017-03-06 13:54:11 +00001033 DCHECK(kUseBakerReadBarrier);
Roland Levillain47b3ab22017-02-27 14:31:35 +00001034 }
1035
1036 const char* GetDescription() const OVERRIDE {
Roland Levillain54f869e2017-03-06 13:54:11 +00001037 return "LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARMVIXL";
Roland Levillain47b3ab22017-02-27 14:31:35 +00001038 }
1039
1040 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
1041 LocationSummary* locations = instruction_->GetLocations();
1042 vixl32::Register ref_reg = RegisterFrom(ref_);
1043 DCHECK(locations->CanCall());
1044 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg.GetCode())) << ref_reg;
Roland Levillain54f869e2017-03-06 13:54:11 +00001045 DCHECK_NE(ref_.reg(), LocationFrom(temp1_).reg());
1046
1047 // This slow path is only used by the UnsafeCASObject intrinsic at the moment.
Roland Levillain47b3ab22017-02-27 14:31:35 +00001048 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
1049 << "Unexpected instruction in read barrier marking and field updating slow path: "
1050 << instruction_->DebugName();
1051 DCHECK(instruction_->GetLocations()->Intrinsified());
1052 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
Roland Levillain54f869e2017-03-06 13:54:11 +00001053 DCHECK_EQ(offset_, 0u);
1054 DCHECK_EQ(scale_factor_, ScaleFactor::TIMES_1);
1055 Location field_offset = index_;
1056 DCHECK(field_offset.IsRegisterPair()) << field_offset;
1057
1058 // Temporary register `temp1_`, used to store the lock word, must
1059 // not be IP, as we may use it to emit the reference load (in the
1060 // call to GenerateRawReferenceLoad below), and we need the lock
1061 // word to still be in `temp1_` after the reference load.
1062 DCHECK(!temp1_.Is(ip));
Roland Levillain47b3ab22017-02-27 14:31:35 +00001063
1064 __ Bind(GetEntryLabel());
1065
Roland Levillainff487002017-03-07 16:50:01 +00001066 // The implementation is similar to LoadReferenceWithBakerReadBarrierSlowPathARMVIXL's:
1067 //
1068 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
1069 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
1070 // HeapReference<mirror::Object> ref = *src; // Original reference load.
1071 // bool is_gray = (rb_state == ReadBarrier::GrayState());
1072 // if (is_gray) {
1073 // old_ref = ref;
1074 // ref = entrypoint(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
1075 // compareAndSwapObject(obj, field_offset, old_ref, ref);
1076 // }
1077
Roland Levillain54f869e2017-03-06 13:54:11 +00001078 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
1079
1080 // /* int32_t */ monitor = obj->monitor_
1081 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
1082 arm_codegen->GetAssembler()->LoadFromOffset(kLoadWord, temp1_, obj_, monitor_offset);
1083 if (needs_null_check_) {
1084 codegen->MaybeRecordImplicitNullCheck(instruction_);
1085 }
1086 // /* LockWord */ lock_word = LockWord(monitor)
1087 static_assert(sizeof(LockWord) == sizeof(int32_t),
1088 "art::LockWord and int32_t have different sizes.");
1089
1090 // Introduce a dependency on the lock_word including the rb_state,
1091 // which shall prevent load-load reordering without using
1092 // a memory barrier (which would be more expensive).
1093 // `obj` is unchanged by this operation, but its value now depends
1094 // on `temp`.
1095 __ Add(obj_, obj_, Operand(temp1_, ShiftType::LSR, 32));
1096
1097 // The actual reference load.
1098 // A possible implicit null check has already been handled above.
1099 arm_codegen->GenerateRawReferenceLoad(
1100 instruction_, ref_, obj_, offset_, index_, scale_factor_, /* needs_null_check */ false);
1101
1102 // Mark the object `ref` when `obj` is gray.
1103 //
1104 // if (rb_state == ReadBarrier::GrayState())
1105 // ref = ReadBarrier::Mark(ref);
1106 //
1107 // Given the numeric representation, it's enough to check the low bit of the
1108 // rb_state. We do that by shifting the bit out of the lock word with LSRS
1109 // which can be a 16-bit instruction unlike the TST immediate.
1110 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
1111 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
1112 __ Lsrs(temp1_, temp1_, LockWord::kReadBarrierStateShift + 1);
1113 __ B(cc, GetExitLabel()); // Carry flag is the last bit shifted out by LSRS.
1114
1115 // Save the old value of the reference before marking it.
Roland Levillain47b3ab22017-02-27 14:31:35 +00001116 // Note that we cannot use IP to save the old reference, as IP is
1117 // used internally by the ReadBarrierMarkRegX entry point, and we
1118 // need the old reference after the call to that entry point.
1119 DCHECK(!temp1_.Is(ip));
1120 __ Mov(temp1_, ref_reg);
Roland Levillain27b1f9c2017-01-17 16:56:34 +00001121
Roland Levillain54f869e2017-03-06 13:54:11 +00001122 GenerateReadBarrierMarkRuntimeCall(codegen);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001123
1124 // If the new reference is different from the old reference,
Roland Levillain54f869e2017-03-06 13:54:11 +00001125 // update the field in the holder (`*(obj_ + field_offset)`).
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001126 //
1127 // Note that this field could also hold a different object, if
1128 // another thread had concurrently changed it. In that case, the
1129 // LDREX/SUBS/ITNE sequence of instructions in the compare-and-set
1130 // (CAS) operation below would abort the CAS, leaving the field
1131 // as-is.
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001132 __ Cmp(temp1_, ref_reg);
Roland Levillain54f869e2017-03-06 13:54:11 +00001133 __ B(eq, GetExitLabel());
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001134
1135 // Update the the holder's field atomically. This may fail if
1136 // mutator updates before us, but it's OK. This is achieved
1137 // using a strong compare-and-set (CAS) operation with relaxed
1138 // memory synchronization ordering, where the expected value is
1139 // the old reference and the desired value is the new reference.
1140
1141 UseScratchRegisterScope temps(arm_codegen->GetVIXLAssembler());
1142 // Convenience aliases.
1143 vixl32::Register base = obj_;
1144 // The UnsafeCASObject intrinsic uses a register pair as field
1145 // offset ("long offset"), of which only the low part contains
1146 // data.
Roland Levillain54f869e2017-03-06 13:54:11 +00001147 vixl32::Register offset = LowRegisterFrom(field_offset);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001148 vixl32::Register expected = temp1_;
1149 vixl32::Register value = ref_reg;
1150 vixl32::Register tmp_ptr = temps.Acquire(); // Pointer to actual memory.
1151 vixl32::Register tmp = temp2_; // Value in memory.
1152
1153 __ Add(tmp_ptr, base, offset);
1154
1155 if (kPoisonHeapReferences) {
1156 arm_codegen->GetAssembler()->PoisonHeapReference(expected);
1157 if (value.Is(expected)) {
1158 // Do not poison `value`, as it is the same register as
1159 // `expected`, which has just been poisoned.
1160 } else {
1161 arm_codegen->GetAssembler()->PoisonHeapReference(value);
1162 }
1163 }
1164
1165 // do {
1166 // tmp = [r_ptr] - expected;
1167 // } while (tmp == 0 && failure([r_ptr] <- r_new_value));
1168
1169 vixl32::Label loop_head, exit_loop;
1170 __ Bind(&loop_head);
1171
1172 __ Ldrex(tmp, MemOperand(tmp_ptr));
1173
1174 __ Subs(tmp, tmp, expected);
1175
1176 {
Artem Serov0fb37192016-12-06 18:13:40 +00001177 ExactAssemblyScope aas(arm_codegen->GetVIXLAssembler(),
1178 2 * kMaxInstructionSizeInBytes,
1179 CodeBufferCheckScope::kMaximumSize);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001180
1181 __ it(ne);
1182 __ clrex(ne);
1183 }
1184
Artem Serov517d9f62016-12-12 15:51:15 +00001185 __ B(ne, &exit_loop, /* far_target */ false);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001186
1187 __ Strex(tmp, value, MemOperand(tmp_ptr));
1188 __ Cmp(tmp, 1);
Artem Serov517d9f62016-12-12 15:51:15 +00001189 __ B(eq, &loop_head, /* far_target */ false);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001190
1191 __ Bind(&exit_loop);
1192
1193 if (kPoisonHeapReferences) {
1194 arm_codegen->GetAssembler()->UnpoisonHeapReference(expected);
1195 if (value.Is(expected)) {
1196 // Do not unpoison `value`, as it is the same register as
1197 // `expected`, which has just been unpoisoned.
1198 } else {
1199 arm_codegen->GetAssembler()->UnpoisonHeapReference(value);
1200 }
1201 }
1202
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001203 __ B(GetExitLabel());
1204 }
1205
1206 private:
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001207 // The register containing the object holding the marked object reference field.
1208 const vixl32::Register obj_;
Roland Levillain54f869e2017-03-06 13:54:11 +00001209 // The offset, index and scale factor to access the reference in `obj_`.
1210 uint32_t offset_;
1211 Location index_;
1212 ScaleFactor scale_factor_;
1213 // Is a null check required?
1214 bool needs_null_check_;
1215 // A temporary register used to hold the lock word of `obj_`; and
1216 // also to hold the original reference value, when the reference is
1217 // marked.
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001218 const vixl32::Register temp1_;
Roland Levillain54f869e2017-03-06 13:54:11 +00001219 // A temporary register used in the implementation of the CAS, to
1220 // update the object's reference field.
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001221 const vixl32::Register temp2_;
1222
Roland Levillain54f869e2017-03-06 13:54:11 +00001223 DISALLOW_COPY_AND_ASSIGN(LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARMVIXL);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001224};
1225
1226// Slow path generating a read barrier for a heap reference.
1227class ReadBarrierForHeapReferenceSlowPathARMVIXL : public SlowPathCodeARMVIXL {
1228 public:
1229 ReadBarrierForHeapReferenceSlowPathARMVIXL(HInstruction* instruction,
1230 Location out,
1231 Location ref,
1232 Location obj,
1233 uint32_t offset,
1234 Location index)
1235 : SlowPathCodeARMVIXL(instruction),
1236 out_(out),
1237 ref_(ref),
1238 obj_(obj),
1239 offset_(offset),
1240 index_(index) {
1241 DCHECK(kEmitCompilerReadBarrier);
1242 // If `obj` is equal to `out` or `ref`, it means the initial object
1243 // has been overwritten by (or after) the heap object reference load
1244 // to be instrumented, e.g.:
1245 //
1246 // __ LoadFromOffset(kLoadWord, out, out, offset);
1247 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
1248 //
1249 // In that case, we have lost the information about the original
1250 // object, and the emitted read barrier cannot work properly.
1251 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
1252 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
1253 }
1254
1255 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
1256 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
1257 LocationSummary* locations = instruction_->GetLocations();
1258 vixl32::Register reg_out = RegisterFrom(out_);
1259 DCHECK(locations->CanCall());
1260 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out.GetCode()));
1261 DCHECK(instruction_->IsInstanceFieldGet() ||
1262 instruction_->IsStaticFieldGet() ||
1263 instruction_->IsArrayGet() ||
1264 instruction_->IsInstanceOf() ||
1265 instruction_->IsCheckCast() ||
Andreas Gamped9911ee2017-03-27 13:27:24 -07001266 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001267 << "Unexpected instruction in read barrier for heap reference slow path: "
1268 << instruction_->DebugName();
1269 // The read barrier instrumentation of object ArrayGet
1270 // instructions does not support the HIntermediateAddress
1271 // instruction.
1272 DCHECK(!(instruction_->IsArrayGet() &&
1273 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
1274
1275 __ Bind(GetEntryLabel());
1276 SaveLiveRegisters(codegen, locations);
1277
1278 // We may have to change the index's value, but as `index_` is a
1279 // constant member (like other "inputs" of this slow path),
1280 // introduce a copy of it, `index`.
1281 Location index = index_;
1282 if (index_.IsValid()) {
1283 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
1284 if (instruction_->IsArrayGet()) {
1285 // Compute the actual memory offset and store it in `index`.
1286 vixl32::Register index_reg = RegisterFrom(index_);
1287 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg.GetCode()));
1288 if (codegen->IsCoreCalleeSaveRegister(index_reg.GetCode())) {
1289 // We are about to change the value of `index_reg` (see the
1290 // calls to art::arm::Thumb2Assembler::Lsl and
1291 // art::arm::Thumb2Assembler::AddConstant below), but it has
1292 // not been saved by the previous call to
1293 // art::SlowPathCode::SaveLiveRegisters, as it is a
1294 // callee-save register --
1295 // art::SlowPathCode::SaveLiveRegisters does not consider
1296 // callee-save registers, as it has been designed with the
1297 // assumption that callee-save registers are supposed to be
1298 // handled by the called function. So, as a callee-save
1299 // register, `index_reg` _would_ eventually be saved onto
1300 // the stack, but it would be too late: we would have
1301 // changed its value earlier. Therefore, we manually save
1302 // it here into another freely available register,
1303 // `free_reg`, chosen of course among the caller-save
1304 // registers (as a callee-save `free_reg` register would
1305 // exhibit the same problem).
1306 //
1307 // Note we could have requested a temporary register from
1308 // the register allocator instead; but we prefer not to, as
1309 // this is a slow path, and we know we can find a
1310 // caller-save register that is available.
1311 vixl32::Register free_reg = FindAvailableCallerSaveRegister(codegen);
1312 __ Mov(free_reg, index_reg);
1313 index_reg = free_reg;
1314 index = LocationFrom(index_reg);
1315 } else {
1316 // The initial register stored in `index_` has already been
1317 // saved in the call to art::SlowPathCode::SaveLiveRegisters
1318 // (as it is not a callee-save register), so we can freely
1319 // use it.
1320 }
1321 // Shifting the index value contained in `index_reg` by the scale
1322 // factor (2) cannot overflow in practice, as the runtime is
1323 // unable to allocate object arrays with a size larger than
1324 // 2^26 - 1 (that is, 2^28 - 4 bytes).
1325 __ Lsl(index_reg, index_reg, TIMES_4);
1326 static_assert(
1327 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
1328 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
1329 __ Add(index_reg, index_reg, offset_);
1330 } else {
1331 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
1332 // intrinsics, `index_` is not shifted by a scale factor of 2
1333 // (as in the case of ArrayGet), as it is actually an offset
1334 // to an object field within an object.
1335 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
1336 DCHECK(instruction_->GetLocations()->Intrinsified());
1337 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
1338 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
1339 << instruction_->AsInvoke()->GetIntrinsic();
1340 DCHECK_EQ(offset_, 0U);
1341 DCHECK(index_.IsRegisterPair());
1342 // UnsafeGet's offset location is a register pair, the low
1343 // part contains the correct offset.
1344 index = index_.ToLow();
1345 }
1346 }
1347
1348 // We're moving two or three locations to locations that could
1349 // overlap, so we need a parallel move resolver.
1350 InvokeRuntimeCallingConventionARMVIXL calling_convention;
1351 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
1352 parallel_move.AddMove(ref_,
1353 LocationFrom(calling_convention.GetRegisterAt(0)),
1354 Primitive::kPrimNot,
1355 nullptr);
1356 parallel_move.AddMove(obj_,
1357 LocationFrom(calling_convention.GetRegisterAt(1)),
1358 Primitive::kPrimNot,
1359 nullptr);
1360 if (index.IsValid()) {
1361 parallel_move.AddMove(index,
1362 LocationFrom(calling_convention.GetRegisterAt(2)),
1363 Primitive::kPrimInt,
1364 nullptr);
1365 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
1366 } else {
1367 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
1368 __ Mov(calling_convention.GetRegisterAt(2), offset_);
1369 }
1370 arm_codegen->InvokeRuntime(kQuickReadBarrierSlow, instruction_, instruction_->GetDexPc(), this);
1371 CheckEntrypointTypes<
1372 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
1373 arm_codegen->Move32(out_, LocationFrom(r0));
1374
1375 RestoreLiveRegisters(codegen, locations);
1376 __ B(GetExitLabel());
1377 }
1378
1379 const char* GetDescription() const OVERRIDE {
1380 return "ReadBarrierForHeapReferenceSlowPathARMVIXL";
1381 }
1382
1383 private:
1384 vixl32::Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
1385 uint32_t ref = RegisterFrom(ref_).GetCode();
1386 uint32_t obj = RegisterFrom(obj_).GetCode();
1387 for (uint32_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
1388 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
1389 return vixl32::Register(i);
1390 }
1391 }
1392 // We shall never fail to find a free caller-save register, as
1393 // there are more than two core caller-save registers on ARM
1394 // (meaning it is possible to find one which is different from
1395 // `ref` and `obj`).
1396 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
1397 LOG(FATAL) << "Could not find a free caller-save register";
1398 UNREACHABLE();
1399 }
1400
1401 const Location out_;
1402 const Location ref_;
1403 const Location obj_;
1404 const uint32_t offset_;
1405 // An additional location containing an index to an array.
1406 // Only used for HArrayGet and the UnsafeGetObject &
1407 // UnsafeGetObjectVolatile intrinsics.
1408 const Location index_;
1409
1410 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathARMVIXL);
1411};
1412
1413// Slow path generating a read barrier for a GC root.
1414class ReadBarrierForRootSlowPathARMVIXL : public SlowPathCodeARMVIXL {
1415 public:
1416 ReadBarrierForRootSlowPathARMVIXL(HInstruction* instruction, Location out, Location root)
1417 : SlowPathCodeARMVIXL(instruction), out_(out), root_(root) {
1418 DCHECK(kEmitCompilerReadBarrier);
1419 }
1420
1421 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
1422 LocationSummary* locations = instruction_->GetLocations();
1423 vixl32::Register reg_out = RegisterFrom(out_);
1424 DCHECK(locations->CanCall());
1425 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out.GetCode()));
1426 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
1427 << "Unexpected instruction in read barrier for GC root slow path: "
1428 << instruction_->DebugName();
1429
1430 __ Bind(GetEntryLabel());
1431 SaveLiveRegisters(codegen, locations);
1432
1433 InvokeRuntimeCallingConventionARMVIXL calling_convention;
1434 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
1435 arm_codegen->Move32(LocationFrom(calling_convention.GetRegisterAt(0)), root_);
1436 arm_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
1437 instruction_,
1438 instruction_->GetDexPc(),
1439 this);
1440 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
1441 arm_codegen->Move32(out_, LocationFrom(r0));
1442
1443 RestoreLiveRegisters(codegen, locations);
1444 __ B(GetExitLabel());
1445 }
1446
1447 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathARMVIXL"; }
1448
1449 private:
1450 const Location out_;
1451 const Location root_;
1452
1453 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathARMVIXL);
1454};
Scott Wakelingc34dba72016-10-03 10:14:44 +01001455
Scott Wakelingfe885462016-09-22 10:24:38 +01001456inline vixl32::Condition ARMCondition(IfCondition cond) {
1457 switch (cond) {
1458 case kCondEQ: return eq;
1459 case kCondNE: return ne;
1460 case kCondLT: return lt;
1461 case kCondLE: return le;
1462 case kCondGT: return gt;
1463 case kCondGE: return ge;
1464 case kCondB: return lo;
1465 case kCondBE: return ls;
1466 case kCondA: return hi;
1467 case kCondAE: return hs;
1468 }
1469 LOG(FATAL) << "Unreachable";
1470 UNREACHABLE();
1471}
1472
1473// Maps signed condition to unsigned condition.
1474inline vixl32::Condition ARMUnsignedCondition(IfCondition cond) {
1475 switch (cond) {
1476 case kCondEQ: return eq;
1477 case kCondNE: return ne;
1478 // Signed to unsigned.
1479 case kCondLT: return lo;
1480 case kCondLE: return ls;
1481 case kCondGT: return hi;
1482 case kCondGE: return hs;
1483 // Unsigned remain unchanged.
1484 case kCondB: return lo;
1485 case kCondBE: return ls;
1486 case kCondA: return hi;
1487 case kCondAE: return hs;
1488 }
1489 LOG(FATAL) << "Unreachable";
1490 UNREACHABLE();
1491}
1492
1493inline vixl32::Condition ARMFPCondition(IfCondition cond, bool gt_bias) {
1494 // The ARM condition codes can express all the necessary branches, see the
1495 // "Meaning (floating-point)" column in the table A8-1 of the ARMv7 reference manual.
1496 // There is no dex instruction or HIR that would need the missing conditions
1497 // "equal or unordered" or "not equal".
1498 switch (cond) {
1499 case kCondEQ: return eq;
1500 case kCondNE: return ne /* unordered */;
1501 case kCondLT: return gt_bias ? cc : lt /* unordered */;
1502 case kCondLE: return gt_bias ? ls : le /* unordered */;
1503 case kCondGT: return gt_bias ? hi /* unordered */ : gt;
1504 case kCondGE: return gt_bias ? cs /* unordered */ : ge;
1505 default:
1506 LOG(FATAL) << "UNREACHABLE";
1507 UNREACHABLE();
1508 }
1509}
1510
Anton Kirilov74234da2017-01-13 14:42:47 +00001511inline ShiftType ShiftFromOpKind(HDataProcWithShifterOp::OpKind op_kind) {
1512 switch (op_kind) {
1513 case HDataProcWithShifterOp::kASR: return ShiftType::ASR;
1514 case HDataProcWithShifterOp::kLSL: return ShiftType::LSL;
1515 case HDataProcWithShifterOp::kLSR: return ShiftType::LSR;
1516 default:
1517 LOG(FATAL) << "Unexpected op kind " << op_kind;
1518 UNREACHABLE();
1519 }
1520}
1521
Scott Wakelingfe885462016-09-22 10:24:38 +01001522void CodeGeneratorARMVIXL::DumpCoreRegister(std::ostream& stream, int reg) const {
1523 stream << vixl32::Register(reg);
1524}
1525
1526void CodeGeneratorARMVIXL::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
1527 stream << vixl32::SRegister(reg);
1528}
1529
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001530static uint32_t ComputeSRegisterListMask(const SRegisterList& regs) {
Scott Wakelingfe885462016-09-22 10:24:38 +01001531 uint32_t mask = 0;
1532 for (uint32_t i = regs.GetFirstSRegister().GetCode();
1533 i <= regs.GetLastSRegister().GetCode();
1534 ++i) {
1535 mask |= (1 << i);
1536 }
1537 return mask;
1538}
1539
Artem Serovd4cc5b22016-11-04 11:19:09 +00001540// Saves the register in the stack. Returns the size taken on stack.
1541size_t CodeGeneratorARMVIXL::SaveCoreRegister(size_t stack_index ATTRIBUTE_UNUSED,
1542 uint32_t reg_id ATTRIBUTE_UNUSED) {
1543 TODO_VIXL32(FATAL);
1544 return 0;
1545}
1546
1547// Restores the register from the stack. Returns the size taken on stack.
1548size_t CodeGeneratorARMVIXL::RestoreCoreRegister(size_t stack_index ATTRIBUTE_UNUSED,
1549 uint32_t reg_id ATTRIBUTE_UNUSED) {
1550 TODO_VIXL32(FATAL);
1551 return 0;
1552}
1553
1554size_t CodeGeneratorARMVIXL::SaveFloatingPointRegister(size_t stack_index ATTRIBUTE_UNUSED,
1555 uint32_t reg_id ATTRIBUTE_UNUSED) {
1556 TODO_VIXL32(FATAL);
1557 return 0;
1558}
1559
1560size_t CodeGeneratorARMVIXL::RestoreFloatingPointRegister(size_t stack_index ATTRIBUTE_UNUSED,
1561 uint32_t reg_id ATTRIBUTE_UNUSED) {
1562 TODO_VIXL32(FATAL);
1563 return 0;
Anton Kirilove28d9ae2016-10-25 18:17:23 +01001564}
1565
Anton Kirilov74234da2017-01-13 14:42:47 +00001566static void GenerateDataProcInstruction(HInstruction::InstructionKind kind,
1567 vixl32::Register out,
1568 vixl32::Register first,
1569 const Operand& second,
1570 CodeGeneratorARMVIXL* codegen) {
1571 if (second.IsImmediate() && second.GetImmediate() == 0) {
1572 const Operand in = kind == HInstruction::kAnd
1573 ? Operand(0)
1574 : Operand(first);
1575
1576 __ Mov(out, in);
1577 } else {
1578 switch (kind) {
1579 case HInstruction::kAdd:
1580 __ Add(out, first, second);
1581 break;
1582 case HInstruction::kAnd:
1583 __ And(out, first, second);
1584 break;
1585 case HInstruction::kOr:
1586 __ Orr(out, first, second);
1587 break;
1588 case HInstruction::kSub:
1589 __ Sub(out, first, second);
1590 break;
1591 case HInstruction::kXor:
1592 __ Eor(out, first, second);
1593 break;
1594 default:
1595 LOG(FATAL) << "Unexpected instruction kind: " << kind;
1596 UNREACHABLE();
1597 }
1598 }
1599}
1600
1601static void GenerateDataProc(HInstruction::InstructionKind kind,
1602 const Location& out,
1603 const Location& first,
1604 const Operand& second_lo,
1605 const Operand& second_hi,
1606 CodeGeneratorARMVIXL* codegen) {
1607 const vixl32::Register first_hi = HighRegisterFrom(first);
1608 const vixl32::Register first_lo = LowRegisterFrom(first);
1609 const vixl32::Register out_hi = HighRegisterFrom(out);
1610 const vixl32::Register out_lo = LowRegisterFrom(out);
1611
1612 if (kind == HInstruction::kAdd) {
1613 __ Adds(out_lo, first_lo, second_lo);
1614 __ Adc(out_hi, first_hi, second_hi);
1615 } else if (kind == HInstruction::kSub) {
1616 __ Subs(out_lo, first_lo, second_lo);
1617 __ Sbc(out_hi, first_hi, second_hi);
1618 } else {
1619 GenerateDataProcInstruction(kind, out_lo, first_lo, second_lo, codegen);
1620 GenerateDataProcInstruction(kind, out_hi, first_hi, second_hi, codegen);
1621 }
1622}
1623
1624static Operand GetShifterOperand(vixl32::Register rm, ShiftType shift, uint32_t shift_imm) {
1625 return shift_imm == 0 ? Operand(rm) : Operand(rm, shift, shift_imm);
1626}
1627
1628static void GenerateLongDataProc(HDataProcWithShifterOp* instruction,
1629 CodeGeneratorARMVIXL* codegen) {
1630 DCHECK_EQ(instruction->GetType(), Primitive::kPrimLong);
1631 DCHECK(HDataProcWithShifterOp::IsShiftOp(instruction->GetOpKind()));
1632
1633 const LocationSummary* const locations = instruction->GetLocations();
1634 const uint32_t shift_value = instruction->GetShiftAmount();
1635 const HInstruction::InstructionKind kind = instruction->GetInstrKind();
1636 const Location first = locations->InAt(0);
1637 const Location second = locations->InAt(1);
1638 const Location out = locations->Out();
1639 const vixl32::Register first_hi = HighRegisterFrom(first);
1640 const vixl32::Register first_lo = LowRegisterFrom(first);
1641 const vixl32::Register out_hi = HighRegisterFrom(out);
1642 const vixl32::Register out_lo = LowRegisterFrom(out);
1643 const vixl32::Register second_hi = HighRegisterFrom(second);
1644 const vixl32::Register second_lo = LowRegisterFrom(second);
1645 const ShiftType shift = ShiftFromOpKind(instruction->GetOpKind());
1646
1647 if (shift_value >= 32) {
1648 if (shift == ShiftType::LSL) {
1649 GenerateDataProcInstruction(kind,
1650 out_hi,
1651 first_hi,
1652 Operand(second_lo, ShiftType::LSL, shift_value - 32),
1653 codegen);
1654 GenerateDataProcInstruction(kind, out_lo, first_lo, 0, codegen);
1655 } else if (shift == ShiftType::ASR) {
1656 GenerateDataProc(kind,
1657 out,
1658 first,
1659 GetShifterOperand(second_hi, ShiftType::ASR, shift_value - 32),
1660 Operand(second_hi, ShiftType::ASR, 31),
1661 codegen);
1662 } else {
1663 DCHECK_EQ(shift, ShiftType::LSR);
1664 GenerateDataProc(kind,
1665 out,
1666 first,
1667 GetShifterOperand(second_hi, ShiftType::LSR, shift_value - 32),
1668 0,
1669 codegen);
1670 }
1671 } else {
1672 DCHECK_GT(shift_value, 1U);
1673 DCHECK_LT(shift_value, 32U);
1674
1675 UseScratchRegisterScope temps(codegen->GetVIXLAssembler());
1676
1677 if (shift == ShiftType::LSL) {
1678 // We are not doing this for HInstruction::kAdd because the output will require
1679 // Location::kOutputOverlap; not applicable to other cases.
1680 if (kind == HInstruction::kOr || kind == HInstruction::kXor) {
1681 GenerateDataProcInstruction(kind,
1682 out_hi,
1683 first_hi,
1684 Operand(second_hi, ShiftType::LSL, shift_value),
1685 codegen);
1686 GenerateDataProcInstruction(kind,
1687 out_hi,
1688 out_hi,
1689 Operand(second_lo, ShiftType::LSR, 32 - shift_value),
1690 codegen);
1691 GenerateDataProcInstruction(kind,
1692 out_lo,
1693 first_lo,
1694 Operand(second_lo, ShiftType::LSL, shift_value),
1695 codegen);
1696 } else {
1697 const vixl32::Register temp = temps.Acquire();
1698
1699 __ Lsl(temp, second_hi, shift_value);
1700 __ Orr(temp, temp, Operand(second_lo, ShiftType::LSR, 32 - shift_value));
1701 GenerateDataProc(kind,
1702 out,
1703 first,
1704 Operand(second_lo, ShiftType::LSL, shift_value),
1705 temp,
1706 codegen);
1707 }
1708 } else {
1709 DCHECK(shift == ShiftType::ASR || shift == ShiftType::LSR);
1710
1711 // We are not doing this for HInstruction::kAdd because the output will require
1712 // Location::kOutputOverlap; not applicable to other cases.
1713 if (kind == HInstruction::kOr || kind == HInstruction::kXor) {
1714 GenerateDataProcInstruction(kind,
1715 out_lo,
1716 first_lo,
1717 Operand(second_lo, ShiftType::LSR, shift_value),
1718 codegen);
1719 GenerateDataProcInstruction(kind,
1720 out_lo,
1721 out_lo,
1722 Operand(second_hi, ShiftType::LSL, 32 - shift_value),
1723 codegen);
1724 GenerateDataProcInstruction(kind,
1725 out_hi,
1726 first_hi,
1727 Operand(second_hi, shift, shift_value),
1728 codegen);
1729 } else {
1730 const vixl32::Register temp = temps.Acquire();
1731
1732 __ Lsr(temp, second_lo, shift_value);
1733 __ Orr(temp, temp, Operand(second_hi, ShiftType::LSL, 32 - shift_value));
1734 GenerateDataProc(kind,
1735 out,
1736 first,
1737 temp,
1738 Operand(second_hi, shift, shift_value),
1739 codegen);
1740 }
1741 }
1742 }
1743}
1744
Donghui Bai426b49c2016-11-08 14:55:38 +08001745static void GenerateVcmp(HInstruction* instruction, CodeGeneratorARMVIXL* codegen) {
1746 const Location rhs_loc = instruction->GetLocations()->InAt(1);
1747 if (rhs_loc.IsConstant()) {
1748 // 0.0 is the only immediate that can be encoded directly in
1749 // a VCMP instruction.
1750 //
1751 // Both the JLS (section 15.20.1) and the JVMS (section 6.5)
1752 // specify that in a floating-point comparison, positive zero
1753 // and negative zero are considered equal, so we can use the
1754 // literal 0.0 for both cases here.
1755 //
1756 // Note however that some methods (Float.equal, Float.compare,
1757 // Float.compareTo, Double.equal, Double.compare,
1758 // Double.compareTo, Math.max, Math.min, StrictMath.max,
1759 // StrictMath.min) consider 0.0 to be (strictly) greater than
1760 // -0.0. So if we ever translate calls to these methods into a
1761 // HCompare instruction, we must handle the -0.0 case with
1762 // care here.
1763 DCHECK(rhs_loc.GetConstant()->IsArithmeticZero());
1764
1765 const Primitive::Type type = instruction->InputAt(0)->GetType();
1766
1767 if (type == Primitive::kPrimFloat) {
1768 __ Vcmp(F32, InputSRegisterAt(instruction, 0), 0.0);
1769 } else {
1770 DCHECK_EQ(type, Primitive::kPrimDouble);
1771 __ Vcmp(F64, InputDRegisterAt(instruction, 0), 0.0);
1772 }
1773 } else {
1774 __ Vcmp(InputVRegisterAt(instruction, 0), InputVRegisterAt(instruction, 1));
1775 }
1776}
1777
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001778static std::pair<vixl32::Condition, vixl32::Condition> GenerateLongTestConstant(
1779 HCondition* condition,
1780 bool invert,
1781 CodeGeneratorARMVIXL* codegen) {
Donghui Bai426b49c2016-11-08 14:55:38 +08001782 DCHECK_EQ(condition->GetLeft()->GetType(), Primitive::kPrimLong);
1783
1784 const LocationSummary* const locations = condition->GetLocations();
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001785 IfCondition cond = condition->GetCondition();
1786 IfCondition opposite = condition->GetOppositeCondition();
1787
1788 if (invert) {
1789 std::swap(cond, opposite);
1790 }
1791
1792 std::pair<vixl32::Condition, vixl32::Condition> ret(eq, ne);
Donghui Bai426b49c2016-11-08 14:55:38 +08001793 const Location left = locations->InAt(0);
1794 const Location right = locations->InAt(1);
1795
1796 DCHECK(right.IsConstant());
1797
1798 const vixl32::Register left_high = HighRegisterFrom(left);
1799 const vixl32::Register left_low = LowRegisterFrom(left);
Nicolas Geoffray30826612017-05-10 11:59:26 +00001800 int64_t value = Int64ConstantFrom(right);
Donghui Bai426b49c2016-11-08 14:55:38 +08001801
1802 switch (cond) {
1803 case kCondEQ:
1804 case kCondNE:
1805 case kCondB:
1806 case kCondBE:
1807 case kCondA:
1808 case kCondAE: {
1809 __ Cmp(left_high, High32Bits(value));
1810
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001811 // We use the scope because of the IT block that follows.
Donghui Bai426b49c2016-11-08 14:55:38 +08001812 ExactAssemblyScope guard(codegen->GetVIXLAssembler(),
1813 2 * vixl32::k16BitT32InstructionSizeInBytes,
1814 CodeBufferCheckScope::kExactSize);
1815
1816 __ it(eq);
1817 __ cmp(eq, left_low, Low32Bits(value));
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001818 ret = std::make_pair(ARMUnsignedCondition(cond), ARMUnsignedCondition(opposite));
Donghui Bai426b49c2016-11-08 14:55:38 +08001819 break;
1820 }
1821 case kCondLE:
1822 case kCondGT:
1823 // Trivially true or false.
1824 if (value == std::numeric_limits<int64_t>::max()) {
1825 __ Cmp(left_low, left_low);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001826 ret = cond == kCondLE ? std::make_pair(eq, ne) : std::make_pair(ne, eq);
Donghui Bai426b49c2016-11-08 14:55:38 +08001827 break;
1828 }
1829
1830 if (cond == kCondLE) {
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001831 DCHECK_EQ(opposite, kCondGT);
Donghui Bai426b49c2016-11-08 14:55:38 +08001832 cond = kCondLT;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001833 opposite = kCondGE;
Donghui Bai426b49c2016-11-08 14:55:38 +08001834 } else {
1835 DCHECK_EQ(cond, kCondGT);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001836 DCHECK_EQ(opposite, kCondLE);
Donghui Bai426b49c2016-11-08 14:55:38 +08001837 cond = kCondGE;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001838 opposite = kCondLT;
Donghui Bai426b49c2016-11-08 14:55:38 +08001839 }
1840
1841 value++;
1842 FALLTHROUGH_INTENDED;
1843 case kCondGE:
1844 case kCondLT: {
Nicolas Geoffray30826612017-05-10 11:59:26 +00001845 UseScratchRegisterScope temps(codegen->GetVIXLAssembler());
1846
Donghui Bai426b49c2016-11-08 14:55:38 +08001847 __ Cmp(left_low, Low32Bits(value));
1848 __ Sbcs(temps.Acquire(), left_high, High32Bits(value));
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001849 ret = std::make_pair(ARMCondition(cond), ARMCondition(opposite));
Donghui Bai426b49c2016-11-08 14:55:38 +08001850 break;
1851 }
1852 default:
1853 LOG(FATAL) << "Unreachable";
1854 UNREACHABLE();
1855 }
1856
1857 return ret;
1858}
1859
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001860static std::pair<vixl32::Condition, vixl32::Condition> GenerateLongTest(
1861 HCondition* condition,
1862 bool invert,
1863 CodeGeneratorARMVIXL* codegen) {
Donghui Bai426b49c2016-11-08 14:55:38 +08001864 DCHECK_EQ(condition->GetLeft()->GetType(), Primitive::kPrimLong);
1865
1866 const LocationSummary* const locations = condition->GetLocations();
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001867 IfCondition cond = condition->GetCondition();
1868 IfCondition opposite = condition->GetOppositeCondition();
1869
1870 if (invert) {
1871 std::swap(cond, opposite);
1872 }
1873
1874 std::pair<vixl32::Condition, vixl32::Condition> ret(eq, ne);
Donghui Bai426b49c2016-11-08 14:55:38 +08001875 Location left = locations->InAt(0);
1876 Location right = locations->InAt(1);
1877
1878 DCHECK(right.IsRegisterPair());
1879
1880 switch (cond) {
1881 case kCondEQ:
1882 case kCondNE:
1883 case kCondB:
1884 case kCondBE:
1885 case kCondA:
1886 case kCondAE: {
1887 __ Cmp(HighRegisterFrom(left), HighRegisterFrom(right));
1888
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001889 // We use the scope because of the IT block that follows.
Donghui Bai426b49c2016-11-08 14:55:38 +08001890 ExactAssemblyScope guard(codegen->GetVIXLAssembler(),
1891 2 * vixl32::k16BitT32InstructionSizeInBytes,
1892 CodeBufferCheckScope::kExactSize);
1893
1894 __ it(eq);
1895 __ cmp(eq, LowRegisterFrom(left), LowRegisterFrom(right));
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001896 ret = std::make_pair(ARMUnsignedCondition(cond), ARMUnsignedCondition(opposite));
Donghui Bai426b49c2016-11-08 14:55:38 +08001897 break;
1898 }
1899 case kCondLE:
1900 case kCondGT:
1901 if (cond == kCondLE) {
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001902 DCHECK_EQ(opposite, kCondGT);
Donghui Bai426b49c2016-11-08 14:55:38 +08001903 cond = kCondGE;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001904 opposite = kCondLT;
Donghui Bai426b49c2016-11-08 14:55:38 +08001905 } else {
1906 DCHECK_EQ(cond, kCondGT);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001907 DCHECK_EQ(opposite, kCondLE);
Donghui Bai426b49c2016-11-08 14:55:38 +08001908 cond = kCondLT;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001909 opposite = kCondGE;
Donghui Bai426b49c2016-11-08 14:55:38 +08001910 }
1911
1912 std::swap(left, right);
1913 FALLTHROUGH_INTENDED;
1914 case kCondGE:
1915 case kCondLT: {
1916 UseScratchRegisterScope temps(codegen->GetVIXLAssembler());
1917
1918 __ Cmp(LowRegisterFrom(left), LowRegisterFrom(right));
1919 __ Sbcs(temps.Acquire(), HighRegisterFrom(left), HighRegisterFrom(right));
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001920 ret = std::make_pair(ARMCondition(cond), ARMCondition(opposite));
Donghui Bai426b49c2016-11-08 14:55:38 +08001921 break;
1922 }
1923 default:
1924 LOG(FATAL) << "Unreachable";
1925 UNREACHABLE();
1926 }
1927
1928 return ret;
1929}
1930
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001931static std::pair<vixl32::Condition, vixl32::Condition> GenerateTest(HCondition* condition,
1932 bool invert,
1933 CodeGeneratorARMVIXL* codegen) {
1934 const Primitive::Type type = condition->GetLeft()->GetType();
1935 IfCondition cond = condition->GetCondition();
1936 IfCondition opposite = condition->GetOppositeCondition();
1937 std::pair<vixl32::Condition, vixl32::Condition> ret(eq, ne);
Donghui Bai426b49c2016-11-08 14:55:38 +08001938
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001939 if (invert) {
1940 std::swap(cond, opposite);
1941 }
Donghui Bai426b49c2016-11-08 14:55:38 +08001942
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001943 if (type == Primitive::kPrimLong) {
1944 ret = condition->GetLocations()->InAt(1).IsConstant()
1945 ? GenerateLongTestConstant(condition, invert, codegen)
1946 : GenerateLongTest(condition, invert, codegen);
1947 } else if (Primitive::IsFloatingPointType(type)) {
1948 GenerateVcmp(condition, codegen);
1949 __ Vmrs(RegisterOrAPSR_nzcv(kPcCode), FPSCR);
1950 ret = std::make_pair(ARMFPCondition(cond, condition->IsGtBias()),
1951 ARMFPCondition(opposite, condition->IsGtBias()));
Donghui Bai426b49c2016-11-08 14:55:38 +08001952 } else {
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001953 DCHECK(Primitive::IsIntegralType(type) || type == Primitive::kPrimNot) << type;
1954 __ Cmp(InputRegisterAt(condition, 0), InputOperandAt(condition, 1));
1955 ret = std::make_pair(ARMCondition(cond), ARMCondition(opposite));
Donghui Bai426b49c2016-11-08 14:55:38 +08001956 }
1957
1958 return ret;
1959}
1960
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001961static bool CanGenerateTest(HCondition* condition, ArmVIXLAssembler* assembler) {
1962 if (condition->GetLeft()->GetType() == Primitive::kPrimLong) {
1963 const LocationSummary* const locations = condition->GetLocations();
Nicolas Geoffray30826612017-05-10 11:59:26 +00001964 const IfCondition c = condition->GetCondition();
Donghui Bai426b49c2016-11-08 14:55:38 +08001965
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001966 if (locations->InAt(1).IsConstant()) {
Nicolas Geoffray30826612017-05-10 11:59:26 +00001967 const int64_t value = Int64ConstantFrom(locations->InAt(1));
Donghui Bai426b49c2016-11-08 14:55:38 +08001968
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001969 if (c < kCondLT || c > kCondGE) {
1970 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
1971 // we check that the least significant half of the first input to be compared
1972 // is in a low register (the other half is read outside an IT block), and
1973 // the constant fits in an 8-bit unsigned integer, so that a 16-bit CMP
Nicolas Geoffray30826612017-05-10 11:59:26 +00001974 // encoding can be used.
1975 if (!LowRegisterFrom(locations->InAt(0)).IsLow() || !IsUint<8>(Low32Bits(value))) {
Donghui Bai426b49c2016-11-08 14:55:38 +08001976 return false;
1977 }
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001978 // TODO(VIXL): The rest of the checks are there to keep the backend in sync with
1979 // the previous one, but are not strictly necessary.
1980 } else if (c == kCondLE || c == kCondGT) {
1981 if (value < std::numeric_limits<int64_t>::max() &&
1982 !assembler->ShifterOperandCanHold(SBC, High32Bits(value + 1), kCcSet)) {
1983 return false;
1984 }
1985 } else if (!assembler->ShifterOperandCanHold(SBC, High32Bits(value), kCcSet)) {
1986 return false;
Donghui Bai426b49c2016-11-08 14:55:38 +08001987 }
1988 }
1989 }
1990
1991 return true;
1992}
1993
1994static bool CanEncodeConstantAs8BitImmediate(HConstant* constant) {
1995 const Primitive::Type type = constant->GetType();
1996 bool ret = false;
1997
1998 DCHECK(Primitive::IsIntegralType(type) || type == Primitive::kPrimNot) << type;
1999
2000 if (type == Primitive::kPrimLong) {
2001 const uint64_t value = Uint64ConstantFrom(constant);
2002
2003 ret = IsUint<8>(Low32Bits(value)) && IsUint<8>(High32Bits(value));
2004 } else {
2005 ret = IsUint<8>(Int32ConstantFrom(constant));
2006 }
2007
2008 return ret;
2009}
2010
2011static Location Arm8BitEncodableConstantOrRegister(HInstruction* constant) {
2012 DCHECK(!Primitive::IsFloatingPointType(constant->GetType()));
2013
2014 if (constant->IsConstant() && CanEncodeConstantAs8BitImmediate(constant->AsConstant())) {
2015 return Location::ConstantLocation(constant->AsConstant());
2016 }
2017
2018 return Location::RequiresRegister();
2019}
2020
2021static bool CanGenerateConditionalMove(const Location& out, const Location& src) {
2022 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
2023 // we check that we are not dealing with floating-point output (there is no
2024 // 16-bit VMOV encoding).
2025 if (!out.IsRegister() && !out.IsRegisterPair()) {
2026 return false;
2027 }
2028
2029 // For constants, we also check that the output is in one or two low registers,
2030 // and that the constants fit in an 8-bit unsigned integer, so that a 16-bit
2031 // MOV encoding can be used.
2032 if (src.IsConstant()) {
2033 if (!CanEncodeConstantAs8BitImmediate(src.GetConstant())) {
2034 return false;
2035 }
2036
2037 if (out.IsRegister()) {
2038 if (!RegisterFrom(out).IsLow()) {
2039 return false;
2040 }
2041 } else {
2042 DCHECK(out.IsRegisterPair());
2043
2044 if (!HighRegisterFrom(out).IsLow()) {
2045 return false;
2046 }
2047 }
2048 }
2049
2050 return true;
2051}
2052
Scott Wakelingfe885462016-09-22 10:24:38 +01002053#undef __
2054
Donghui Bai426b49c2016-11-08 14:55:38 +08002055vixl32::Label* CodeGeneratorARMVIXL::GetFinalLabel(HInstruction* instruction,
2056 vixl32::Label* final_label) {
2057 DCHECK(!instruction->IsControlFlow() && !instruction->IsSuspendCheck());
Anton Kirilov6f644202017-02-27 18:29:45 +00002058 DCHECK(!instruction->IsInvoke() || !instruction->GetLocations()->CanCall());
Donghui Bai426b49c2016-11-08 14:55:38 +08002059
2060 const HBasicBlock* const block = instruction->GetBlock();
2061 const HLoopInformation* const info = block->GetLoopInformation();
2062 HInstruction* const next = instruction->GetNext();
2063
2064 // Avoid a branch to a branch.
2065 if (next->IsGoto() && (info == nullptr ||
2066 !info->IsBackEdge(*block) ||
2067 !info->HasSuspendCheck())) {
2068 final_label = GetLabelOf(next->AsGoto()->GetSuccessor());
2069 }
2070
2071 return final_label;
2072}
2073
Scott Wakelingfe885462016-09-22 10:24:38 +01002074CodeGeneratorARMVIXL::CodeGeneratorARMVIXL(HGraph* graph,
2075 const ArmInstructionSetFeatures& isa_features,
2076 const CompilerOptions& compiler_options,
2077 OptimizingCompilerStats* stats)
2078 : CodeGenerator(graph,
2079 kNumberOfCoreRegisters,
2080 kNumberOfSRegisters,
2081 kNumberOfRegisterPairs,
2082 kCoreCalleeSaves.GetList(),
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002083 ComputeSRegisterListMask(kFpuCalleeSaves),
Scott Wakelingfe885462016-09-22 10:24:38 +01002084 compiler_options,
2085 stats),
2086 block_labels_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Artem Serov551b28f2016-10-18 19:11:30 +01002087 jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Scott Wakelingfe885462016-09-22 10:24:38 +01002088 location_builder_(graph, this),
2089 instruction_visitor_(graph, this),
2090 move_resolver_(graph->GetArena(), this),
2091 assembler_(graph->GetArena()),
Artem Serovd4cc5b22016-11-04 11:19:09 +00002092 isa_features_(isa_features),
Artem Serovc5fcb442016-12-02 19:19:58 +00002093 uint32_literals_(std::less<uint32_t>(),
2094 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Artem Serovd4cc5b22016-11-04 11:19:09 +00002095 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Artem Serovc5fcb442016-12-02 19:19:58 +00002096 boot_image_string_patches_(StringReferenceValueComparator(),
2097 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Artem Serovd4cc5b22016-11-04 11:19:09 +00002098 pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Artem Serovc5fcb442016-12-02 19:19:58 +00002099 boot_image_type_patches_(TypeReferenceValueComparator(),
2100 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
2101 pc_relative_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko1998cd02017-01-13 13:02:58 +00002102 type_bss_entry_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01002103 baker_read_barrier_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Artem Serovc5fcb442016-12-02 19:19:58 +00002104 jit_string_patches_(StringReferenceValueComparator(),
2105 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
2106 jit_class_patches_(TypeReferenceValueComparator(),
2107 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Scott Wakelingfe885462016-09-22 10:24:38 +01002108 // Always save the LR register to mimic Quick.
2109 AddAllocatedRegister(Location::RegisterLocation(LR));
Nicolas Geoffray13a797b2017-03-15 16:41:31 +00002110 // Give D30 and D31 as scratch register to VIXL. The register allocator only works on
2111 // S0-S31, which alias to D0-D15.
2112 GetVIXLAssembler()->GetScratchVRegisterList()->Combine(d31);
2113 GetVIXLAssembler()->GetScratchVRegisterList()->Combine(d30);
Scott Wakelingfe885462016-09-22 10:24:38 +01002114}
2115
Artem Serov551b28f2016-10-18 19:11:30 +01002116void JumpTableARMVIXL::EmitTable(CodeGeneratorARMVIXL* codegen) {
2117 uint32_t num_entries = switch_instr_->GetNumEntries();
2118 DCHECK_GE(num_entries, kPackedSwitchCompareJumpThreshold);
2119
2120 // We are about to use the assembler to place literals directly. Make sure we have enough
Scott Wakelingb77051e2016-11-21 19:46:00 +00002121 // underlying code buffer and we have generated a jump table of the right size, using
2122 // codegen->GetVIXLAssembler()->GetBuffer().Align();
Artem Serov0fb37192016-12-06 18:13:40 +00002123 ExactAssemblyScope aas(codegen->GetVIXLAssembler(),
2124 num_entries * sizeof(int32_t),
2125 CodeBufferCheckScope::kMaximumSize);
Artem Serov551b28f2016-10-18 19:11:30 +01002126 // TODO(VIXL): Check that using lower case bind is fine here.
2127 codegen->GetVIXLAssembler()->bind(&table_start_);
Artem Serov09a940d2016-11-11 16:15:11 +00002128 for (uint32_t i = 0; i < num_entries; i++) {
2129 codegen->GetVIXLAssembler()->place(bb_addresses_[i].get());
2130 }
2131}
2132
2133void JumpTableARMVIXL::FixTable(CodeGeneratorARMVIXL* codegen) {
2134 uint32_t num_entries = switch_instr_->GetNumEntries();
2135 DCHECK_GE(num_entries, kPackedSwitchCompareJumpThreshold);
2136
Artem Serov551b28f2016-10-18 19:11:30 +01002137 const ArenaVector<HBasicBlock*>& successors = switch_instr_->GetBlock()->GetSuccessors();
2138 for (uint32_t i = 0; i < num_entries; i++) {
2139 vixl32::Label* target_label = codegen->GetLabelOf(successors[i]);
2140 DCHECK(target_label->IsBound());
2141 int32_t jump_offset = target_label->GetLocation() - table_start_.GetLocation();
2142 // When doing BX to address we need to have lower bit set to 1 in T32.
2143 if (codegen->GetVIXLAssembler()->IsUsingT32()) {
2144 jump_offset++;
2145 }
2146 DCHECK_GT(jump_offset, std::numeric_limits<int32_t>::min());
2147 DCHECK_LE(jump_offset, std::numeric_limits<int32_t>::max());
Artem Serov09a940d2016-11-11 16:15:11 +00002148
Scott Wakelingb77051e2016-11-21 19:46:00 +00002149 bb_addresses_[i].get()->UpdateValue(jump_offset, codegen->GetVIXLAssembler()->GetBuffer());
Artem Serov551b28f2016-10-18 19:11:30 +01002150 }
2151}
2152
Artem Serov09a940d2016-11-11 16:15:11 +00002153void CodeGeneratorARMVIXL::FixJumpTables() {
Artem Serov551b28f2016-10-18 19:11:30 +01002154 for (auto&& jump_table : jump_tables_) {
Artem Serov09a940d2016-11-11 16:15:11 +00002155 jump_table->FixTable(this);
Artem Serov551b28f2016-10-18 19:11:30 +01002156 }
2157}
2158
Andreas Gampeca620d72016-11-08 08:09:33 -08002159#define __ reinterpret_cast<ArmVIXLAssembler*>(GetAssembler())->GetVIXLAssembler()-> // NOLINT
Scott Wakelingfe885462016-09-22 10:24:38 +01002160
2161void CodeGeneratorARMVIXL::Finalize(CodeAllocator* allocator) {
Artem Serov09a940d2016-11-11 16:15:11 +00002162 FixJumpTables();
Scott Wakelingfe885462016-09-22 10:24:38 +01002163 GetAssembler()->FinalizeCode();
2164 CodeGenerator::Finalize(allocator);
2165}
2166
2167void CodeGeneratorARMVIXL::SetupBlockedRegisters() const {
Scott Wakelingfe885462016-09-22 10:24:38 +01002168 // Stack register, LR and PC are always reserved.
2169 blocked_core_registers_[SP] = true;
2170 blocked_core_registers_[LR] = true;
2171 blocked_core_registers_[PC] = true;
2172
2173 // Reserve thread register.
2174 blocked_core_registers_[TR] = true;
2175
2176 // Reserve temp register.
2177 blocked_core_registers_[IP] = true;
2178
2179 if (GetGraph()->IsDebuggable()) {
2180 // Stubs do not save callee-save floating point registers. If the graph
2181 // is debuggable, we need to deal with these registers differently. For
2182 // now, just block them.
2183 for (uint32_t i = kFpuCalleeSaves.GetFirstSRegister().GetCode();
2184 i <= kFpuCalleeSaves.GetLastSRegister().GetCode();
2185 ++i) {
2186 blocked_fpu_registers_[i] = true;
2187 }
2188 }
Scott Wakelingfe885462016-09-22 10:24:38 +01002189}
2190
Scott Wakelingfe885462016-09-22 10:24:38 +01002191InstructionCodeGeneratorARMVIXL::InstructionCodeGeneratorARMVIXL(HGraph* graph,
2192 CodeGeneratorARMVIXL* codegen)
2193 : InstructionCodeGenerator(graph, codegen),
2194 assembler_(codegen->GetAssembler()),
2195 codegen_(codegen) {}
2196
2197void CodeGeneratorARMVIXL::ComputeSpillMask() {
2198 core_spill_mask_ = allocated_registers_.GetCoreRegisters() & core_callee_save_mask_;
2199 DCHECK_NE(core_spill_mask_, 0u) << "At least the return address register must be saved";
2200 // There is no easy instruction to restore just the PC on thumb2. We spill and
2201 // restore another arbitrary register.
2202 core_spill_mask_ |= (1 << kCoreAlwaysSpillRegister.GetCode());
2203 fpu_spill_mask_ = allocated_registers_.GetFloatingPointRegisters() & fpu_callee_save_mask_;
2204 // We use vpush and vpop for saving and restoring floating point registers, which take
2205 // a SRegister and the number of registers to save/restore after that SRegister. We
2206 // therefore update the `fpu_spill_mask_` to also contain those registers not allocated,
2207 // but in the range.
2208 if (fpu_spill_mask_ != 0) {
2209 uint32_t least_significant_bit = LeastSignificantBit(fpu_spill_mask_);
2210 uint32_t most_significant_bit = MostSignificantBit(fpu_spill_mask_);
2211 for (uint32_t i = least_significant_bit + 1 ; i < most_significant_bit; ++i) {
2212 fpu_spill_mask_ |= (1 << i);
2213 }
2214 }
2215}
2216
2217void CodeGeneratorARMVIXL::GenerateFrameEntry() {
2218 bool skip_overflow_check =
2219 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kArm);
2220 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
2221 __ Bind(&frame_entry_label_);
2222
2223 if (HasEmptyFrame()) {
2224 return;
2225 }
2226
Scott Wakelingfe885462016-09-22 10:24:38 +01002227 if (!skip_overflow_check) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002228 UseScratchRegisterScope temps(GetVIXLAssembler());
2229 vixl32::Register temp = temps.Acquire();
Anton Kirilov644032c2016-12-06 17:51:43 +00002230 __ Sub(temp, sp, Operand::From(GetStackOverflowReservedBytes(kArm)));
Scott Wakelingfe885462016-09-22 10:24:38 +01002231 // The load must immediately precede RecordPcInfo.
Artem Serov0fb37192016-12-06 18:13:40 +00002232 ExactAssemblyScope aas(GetVIXLAssembler(),
2233 vixl32::kMaxInstructionSizeInBytes,
2234 CodeBufferCheckScope::kMaximumSize);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002235 __ ldr(temp, MemOperand(temp));
2236 RecordPcInfo(nullptr, 0);
Scott Wakelingfe885462016-09-22 10:24:38 +01002237 }
2238
2239 __ Push(RegisterList(core_spill_mask_));
2240 GetAssembler()->cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(core_spill_mask_));
2241 GetAssembler()->cfi().RelOffsetForMany(DWARFReg(kMethodRegister),
2242 0,
2243 core_spill_mask_,
2244 kArmWordSize);
2245 if (fpu_spill_mask_ != 0) {
2246 uint32_t first = LeastSignificantBit(fpu_spill_mask_);
2247
2248 // Check that list is contiguous.
2249 DCHECK_EQ(fpu_spill_mask_ >> CTZ(fpu_spill_mask_), ~0u >> (32 - POPCOUNT(fpu_spill_mask_)));
2250
2251 __ Vpush(SRegisterList(vixl32::SRegister(first), POPCOUNT(fpu_spill_mask_)));
2252 GetAssembler()->cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(fpu_spill_mask_));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002253 GetAssembler()->cfi().RelOffsetForMany(DWARFReg(s0), 0, fpu_spill_mask_, kArmWordSize);
Scott Wakelingfe885462016-09-22 10:24:38 +01002254 }
Scott Wakelingbffdc702016-12-07 17:46:03 +00002255
2256 if (GetGraph()->HasShouldDeoptimizeFlag()) {
2257 UseScratchRegisterScope temps(GetVIXLAssembler());
2258 vixl32::Register temp = temps.Acquire();
2259 // Initialize should_deoptimize flag to 0.
2260 __ Mov(temp, 0);
2261 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, -kShouldDeoptimizeFlagSize);
2262 }
2263
Scott Wakelingfe885462016-09-22 10:24:38 +01002264 int adjust = GetFrameSize() - FrameEntrySpillSize();
2265 __ Sub(sp, sp, adjust);
2266 GetAssembler()->cfi().AdjustCFAOffset(adjust);
Scott Wakelingbffdc702016-12-07 17:46:03 +00002267
2268 // Save the current method if we need it. Note that we do not
2269 // do this in HCurrentMethod, as the instruction might have been removed
2270 // in the SSA graph.
2271 if (RequiresCurrentMethod()) {
2272 GetAssembler()->StoreToOffset(kStoreWord, kMethodRegister, sp, 0);
2273 }
Scott Wakelingfe885462016-09-22 10:24:38 +01002274}
2275
2276void CodeGeneratorARMVIXL::GenerateFrameExit() {
2277 if (HasEmptyFrame()) {
2278 __ Bx(lr);
2279 return;
2280 }
2281 GetAssembler()->cfi().RememberState();
2282 int adjust = GetFrameSize() - FrameEntrySpillSize();
2283 __ Add(sp, sp, adjust);
2284 GetAssembler()->cfi().AdjustCFAOffset(-adjust);
2285 if (fpu_spill_mask_ != 0) {
2286 uint32_t first = LeastSignificantBit(fpu_spill_mask_);
2287
2288 // Check that list is contiguous.
2289 DCHECK_EQ(fpu_spill_mask_ >> CTZ(fpu_spill_mask_), ~0u >> (32 - POPCOUNT(fpu_spill_mask_)));
2290
2291 __ Vpop(SRegisterList(vixl32::SRegister(first), POPCOUNT(fpu_spill_mask_)));
2292 GetAssembler()->cfi().AdjustCFAOffset(
2293 -static_cast<int>(kArmWordSize) * POPCOUNT(fpu_spill_mask_));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002294 GetAssembler()->cfi().RestoreMany(DWARFReg(vixl32::SRegister(0)), fpu_spill_mask_);
Scott Wakelingfe885462016-09-22 10:24:38 +01002295 }
2296 // Pop LR into PC to return.
2297 DCHECK_NE(core_spill_mask_ & (1 << kLrCode), 0U);
2298 uint32_t pop_mask = (core_spill_mask_ & (~(1 << kLrCode))) | 1 << kPcCode;
2299 __ Pop(RegisterList(pop_mask));
2300 GetAssembler()->cfi().RestoreState();
2301 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
2302}
2303
2304void CodeGeneratorARMVIXL::Bind(HBasicBlock* block) {
2305 __ Bind(GetLabelOf(block));
2306}
2307
Artem Serovd4cc5b22016-11-04 11:19:09 +00002308Location InvokeDexCallingConventionVisitorARMVIXL::GetNextLocation(Primitive::Type type) {
2309 switch (type) {
2310 case Primitive::kPrimBoolean:
2311 case Primitive::kPrimByte:
2312 case Primitive::kPrimChar:
2313 case Primitive::kPrimShort:
2314 case Primitive::kPrimInt:
2315 case Primitive::kPrimNot: {
2316 uint32_t index = gp_index_++;
2317 uint32_t stack_index = stack_index_++;
2318 if (index < calling_convention.GetNumberOfRegisters()) {
2319 return LocationFrom(calling_convention.GetRegisterAt(index));
2320 } else {
2321 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
2322 }
2323 }
2324
2325 case Primitive::kPrimLong: {
2326 uint32_t index = gp_index_;
2327 uint32_t stack_index = stack_index_;
2328 gp_index_ += 2;
2329 stack_index_ += 2;
2330 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
2331 if (calling_convention.GetRegisterAt(index).Is(r1)) {
2332 // Skip R1, and use R2_R3 instead.
2333 gp_index_++;
2334 index++;
2335 }
2336 }
2337 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
2338 DCHECK_EQ(calling_convention.GetRegisterAt(index).GetCode() + 1,
2339 calling_convention.GetRegisterAt(index + 1).GetCode());
2340
2341 return LocationFrom(calling_convention.GetRegisterAt(index),
2342 calling_convention.GetRegisterAt(index + 1));
2343 } else {
2344 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
2345 }
2346 }
2347
2348 case Primitive::kPrimFloat: {
2349 uint32_t stack_index = stack_index_++;
2350 if (float_index_ % 2 == 0) {
2351 float_index_ = std::max(double_index_, float_index_);
2352 }
2353 if (float_index_ < calling_convention.GetNumberOfFpuRegisters()) {
2354 return LocationFrom(calling_convention.GetFpuRegisterAt(float_index_++));
2355 } else {
2356 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
2357 }
2358 }
2359
2360 case Primitive::kPrimDouble: {
2361 double_index_ = std::max(double_index_, RoundUp(float_index_, 2));
2362 uint32_t stack_index = stack_index_;
2363 stack_index_ += 2;
2364 if (double_index_ + 1 < calling_convention.GetNumberOfFpuRegisters()) {
2365 uint32_t index = double_index_;
2366 double_index_ += 2;
2367 Location result = LocationFrom(
2368 calling_convention.GetFpuRegisterAt(index),
2369 calling_convention.GetFpuRegisterAt(index + 1));
2370 DCHECK(ExpectedPairLayout(result));
2371 return result;
2372 } else {
2373 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
2374 }
2375 }
2376
2377 case Primitive::kPrimVoid:
2378 LOG(FATAL) << "Unexpected parameter type " << type;
2379 break;
2380 }
2381 return Location::NoLocation();
2382}
2383
2384Location InvokeDexCallingConventionVisitorARMVIXL::GetReturnLocation(Primitive::Type type) const {
2385 switch (type) {
2386 case Primitive::kPrimBoolean:
2387 case Primitive::kPrimByte:
2388 case Primitive::kPrimChar:
2389 case Primitive::kPrimShort:
2390 case Primitive::kPrimInt:
2391 case Primitive::kPrimNot: {
2392 return LocationFrom(r0);
2393 }
2394
2395 case Primitive::kPrimFloat: {
2396 return LocationFrom(s0);
2397 }
2398
2399 case Primitive::kPrimLong: {
2400 return LocationFrom(r0, r1);
2401 }
2402
2403 case Primitive::kPrimDouble: {
2404 return LocationFrom(s0, s1);
2405 }
2406
2407 case Primitive::kPrimVoid:
2408 return Location::NoLocation();
2409 }
2410
2411 UNREACHABLE();
2412}
2413
2414Location InvokeDexCallingConventionVisitorARMVIXL::GetMethodLocation() const {
2415 return LocationFrom(kMethodRegister);
2416}
2417
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002418void CodeGeneratorARMVIXL::Move32(Location destination, Location source) {
2419 if (source.Equals(destination)) {
2420 return;
2421 }
2422 if (destination.IsRegister()) {
2423 if (source.IsRegister()) {
2424 __ Mov(RegisterFrom(destination), RegisterFrom(source));
2425 } else if (source.IsFpuRegister()) {
2426 __ Vmov(RegisterFrom(destination), SRegisterFrom(source));
2427 } else {
2428 GetAssembler()->LoadFromOffset(kLoadWord,
2429 RegisterFrom(destination),
2430 sp,
2431 source.GetStackIndex());
2432 }
2433 } else if (destination.IsFpuRegister()) {
2434 if (source.IsRegister()) {
2435 __ Vmov(SRegisterFrom(destination), RegisterFrom(source));
2436 } else if (source.IsFpuRegister()) {
2437 __ Vmov(SRegisterFrom(destination), SRegisterFrom(source));
2438 } else {
2439 GetAssembler()->LoadSFromOffset(SRegisterFrom(destination), sp, source.GetStackIndex());
2440 }
2441 } else {
2442 DCHECK(destination.IsStackSlot()) << destination;
2443 if (source.IsRegister()) {
2444 GetAssembler()->StoreToOffset(kStoreWord,
2445 RegisterFrom(source),
2446 sp,
2447 destination.GetStackIndex());
2448 } else if (source.IsFpuRegister()) {
2449 GetAssembler()->StoreSToOffset(SRegisterFrom(source), sp, destination.GetStackIndex());
2450 } else {
2451 DCHECK(source.IsStackSlot()) << source;
2452 UseScratchRegisterScope temps(GetVIXLAssembler());
2453 vixl32::Register temp = temps.Acquire();
2454 GetAssembler()->LoadFromOffset(kLoadWord, temp, sp, source.GetStackIndex());
2455 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
2456 }
2457 }
2458}
2459
Artem Serovcfbe9132016-10-14 15:58:56 +01002460void CodeGeneratorARMVIXL::MoveConstant(Location location, int32_t value) {
2461 DCHECK(location.IsRegister());
2462 __ Mov(RegisterFrom(location), value);
Scott Wakelingfe885462016-09-22 10:24:38 +01002463}
2464
2465void CodeGeneratorARMVIXL::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002466 // TODO(VIXL): Maybe refactor to have the 'move' implementation here and use it in
2467 // `ParallelMoveResolverARMVIXL::EmitMove`, as is done in the `arm64` backend.
2468 HParallelMove move(GetGraph()->GetArena());
2469 move.AddMove(src, dst, dst_type, nullptr);
2470 GetMoveResolver()->EmitNativeCode(&move);
Scott Wakelingfe885462016-09-22 10:24:38 +01002471}
2472
Artem Serovcfbe9132016-10-14 15:58:56 +01002473void CodeGeneratorARMVIXL::AddLocationAsTemp(Location location, LocationSummary* locations) {
2474 if (location.IsRegister()) {
2475 locations->AddTemp(location);
2476 } else if (location.IsRegisterPair()) {
2477 locations->AddTemp(LocationFrom(LowRegisterFrom(location)));
2478 locations->AddTemp(LocationFrom(HighRegisterFrom(location)));
2479 } else {
2480 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
2481 }
Scott Wakelingfe885462016-09-22 10:24:38 +01002482}
2483
2484void CodeGeneratorARMVIXL::InvokeRuntime(QuickEntrypointEnum entrypoint,
2485 HInstruction* instruction,
2486 uint32_t dex_pc,
2487 SlowPathCode* slow_path) {
2488 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Alexandre Rames374ddf32016-11-04 10:40:49 +00002489 __ Ldr(lr, MemOperand(tr, GetThreadOffset<kArmPointerSize>(entrypoint).Int32Value()));
2490 // Ensure the pc position is recorded immediately after the `blx` instruction.
2491 // 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 +00002492 ExactAssemblyScope aas(GetVIXLAssembler(),
2493 vixl32::k16BitT32InstructionSizeInBytes,
2494 CodeBufferCheckScope::kExactSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00002495 __ blx(lr);
Scott Wakelingfe885462016-09-22 10:24:38 +01002496 if (EntrypointRequiresStackMap(entrypoint)) {
2497 RecordPcInfo(instruction, dex_pc, slow_path);
2498 }
2499}
2500
2501void CodeGeneratorARMVIXL::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
2502 HInstruction* instruction,
2503 SlowPathCode* slow_path) {
2504 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Alexandre Rames374ddf32016-11-04 10:40:49 +00002505 __ Ldr(lr, MemOperand(tr, entry_point_offset));
Scott Wakelingfe885462016-09-22 10:24:38 +01002506 __ Blx(lr);
2507}
2508
Scott Wakelingfe885462016-09-22 10:24:38 +01002509void InstructionCodeGeneratorARMVIXL::HandleGoto(HInstruction* got, HBasicBlock* successor) {
2510 DCHECK(!successor->IsExitBlock());
2511 HBasicBlock* block = got->GetBlock();
2512 HInstruction* previous = got->GetPrevious();
2513 HLoopInformation* info = block->GetLoopInformation();
2514
2515 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
2516 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
2517 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2518 return;
2519 }
2520 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2521 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
2522 }
2523 if (!codegen_->GoesToNextBlock(block, successor)) {
2524 __ B(codegen_->GetLabelOf(successor));
2525 }
2526}
2527
2528void LocationsBuilderARMVIXL::VisitGoto(HGoto* got) {
2529 got->SetLocations(nullptr);
2530}
2531
2532void InstructionCodeGeneratorARMVIXL::VisitGoto(HGoto* got) {
2533 HandleGoto(got, got->GetSuccessor());
2534}
2535
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002536void LocationsBuilderARMVIXL::VisitTryBoundary(HTryBoundary* try_boundary) {
2537 try_boundary->SetLocations(nullptr);
2538}
2539
2540void InstructionCodeGeneratorARMVIXL::VisitTryBoundary(HTryBoundary* try_boundary) {
2541 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2542 if (!successor->IsExitBlock()) {
2543 HandleGoto(try_boundary, successor);
2544 }
2545}
2546
Scott Wakelingfe885462016-09-22 10:24:38 +01002547void LocationsBuilderARMVIXL::VisitExit(HExit* exit) {
2548 exit->SetLocations(nullptr);
2549}
2550
2551void InstructionCodeGeneratorARMVIXL::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
2552}
2553
Nicolas Geoffray30826612017-05-10 11:59:26 +00002554void InstructionCodeGeneratorARMVIXL::GenerateLongComparesAndJumps(HCondition* cond,
2555 vixl32::Label* true_label,
2556 vixl32::Label* false_label) {
2557 LocationSummary* locations = cond->GetLocations();
2558 Location left = locations->InAt(0);
2559 Location right = locations->InAt(1);
2560 IfCondition if_cond = cond->GetCondition();
2561
2562 vixl32::Register left_high = HighRegisterFrom(left);
2563 vixl32::Register left_low = LowRegisterFrom(left);
2564 IfCondition true_high_cond = if_cond;
2565 IfCondition false_high_cond = cond->GetOppositeCondition();
2566 vixl32::Condition final_condition = ARMUnsignedCondition(if_cond); // unsigned on lower part
2567
2568 // Set the conditions for the test, remembering that == needs to be
2569 // decided using the low words.
2570 switch (if_cond) {
2571 case kCondEQ:
2572 case kCondNE:
2573 // Nothing to do.
2574 break;
2575 case kCondLT:
2576 false_high_cond = kCondGT;
2577 break;
2578 case kCondLE:
2579 true_high_cond = kCondLT;
2580 break;
2581 case kCondGT:
2582 false_high_cond = kCondLT;
2583 break;
2584 case kCondGE:
2585 true_high_cond = kCondGT;
2586 break;
2587 case kCondB:
2588 false_high_cond = kCondA;
2589 break;
2590 case kCondBE:
2591 true_high_cond = kCondB;
2592 break;
2593 case kCondA:
2594 false_high_cond = kCondB;
2595 break;
2596 case kCondAE:
2597 true_high_cond = kCondA;
2598 break;
2599 }
2600 if (right.IsConstant()) {
2601 int64_t value = Int64ConstantFrom(right);
2602 int32_t val_low = Low32Bits(value);
2603 int32_t val_high = High32Bits(value);
2604
2605 __ Cmp(left_high, val_high);
2606 if (if_cond == kCondNE) {
2607 __ B(ARMCondition(true_high_cond), true_label);
2608 } else if (if_cond == kCondEQ) {
2609 __ B(ARMCondition(false_high_cond), false_label);
2610 } else {
2611 __ B(ARMCondition(true_high_cond), true_label);
2612 __ B(ARMCondition(false_high_cond), false_label);
2613 }
2614 // Must be equal high, so compare the lows.
2615 __ Cmp(left_low, val_low);
2616 } else {
2617 vixl32::Register right_high = HighRegisterFrom(right);
2618 vixl32::Register right_low = LowRegisterFrom(right);
2619
2620 __ Cmp(left_high, right_high);
2621 if (if_cond == kCondNE) {
2622 __ B(ARMCondition(true_high_cond), true_label);
2623 } else if (if_cond == kCondEQ) {
2624 __ B(ARMCondition(false_high_cond), false_label);
2625 } else {
2626 __ B(ARMCondition(true_high_cond), true_label);
2627 __ B(ARMCondition(false_high_cond), false_label);
2628 }
2629 // Must be equal high, so compare the lows.
2630 __ Cmp(left_low, right_low);
2631 }
2632 // The last comparison might be unsigned.
2633 // TODO: optimize cases where this is always true/false
2634 __ B(final_condition, true_label);
2635}
2636
Scott Wakelingfe885462016-09-22 10:24:38 +01002637void InstructionCodeGeneratorARMVIXL::GenerateCompareTestAndBranch(HCondition* condition,
2638 vixl32::Label* true_target_in,
2639 vixl32::Label* false_target_in) {
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002640 if (CanGenerateTest(condition, codegen_->GetAssembler())) {
2641 vixl32::Label* non_fallthrough_target;
2642 bool invert;
2643
2644 if (true_target_in == nullptr) {
2645 DCHECK(false_target_in != nullptr);
2646 non_fallthrough_target = false_target_in;
2647 invert = true;
2648 } else {
2649 non_fallthrough_target = true_target_in;
2650 invert = false;
2651 }
2652
2653 const auto cond = GenerateTest(condition, invert, codegen_);
2654
2655 __ B(cond.first, non_fallthrough_target);
2656
2657 if (false_target_in != nullptr && false_target_in != non_fallthrough_target) {
2658 __ B(false_target_in);
2659 }
2660
2661 return;
2662 }
2663
Scott Wakelingfe885462016-09-22 10:24:38 +01002664 // Generated branching requires both targets to be explicit. If either of the
2665 // targets is nullptr (fallthrough) use and bind `fallthrough` instead.
2666 vixl32::Label fallthrough;
2667 vixl32::Label* true_target = (true_target_in == nullptr) ? &fallthrough : true_target_in;
2668 vixl32::Label* false_target = (false_target_in == nullptr) ? &fallthrough : false_target_in;
2669
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002670 DCHECK_EQ(condition->InputAt(0)->GetType(), Primitive::kPrimLong);
Nicolas Geoffray30826612017-05-10 11:59:26 +00002671 GenerateLongComparesAndJumps(condition, true_target, false_target);
Scott Wakelingfe885462016-09-22 10:24:38 +01002672
2673 if (false_target != &fallthrough) {
2674 __ B(false_target);
2675 }
2676
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002677 if (fallthrough.IsReferenced()) {
Scott Wakelingfe885462016-09-22 10:24:38 +01002678 __ Bind(&fallthrough);
2679 }
2680}
2681
2682void InstructionCodeGeneratorARMVIXL::GenerateTestAndBranch(HInstruction* instruction,
2683 size_t condition_input_index,
2684 vixl32::Label* true_target,
xueliang.zhongf51bc622016-11-04 09:23:32 +00002685 vixl32::Label* false_target,
2686 bool far_target) {
Scott Wakelingfe885462016-09-22 10:24:38 +01002687 HInstruction* cond = instruction->InputAt(condition_input_index);
2688
2689 if (true_target == nullptr && false_target == nullptr) {
2690 // Nothing to do. The code always falls through.
2691 return;
2692 } else if (cond->IsIntConstant()) {
2693 // Constant condition, statically compared against "true" (integer value 1).
2694 if (cond->AsIntConstant()->IsTrue()) {
2695 if (true_target != nullptr) {
2696 __ B(true_target);
2697 }
2698 } else {
Anton Kirilov644032c2016-12-06 17:51:43 +00002699 DCHECK(cond->AsIntConstant()->IsFalse()) << Int32ConstantFrom(cond);
Scott Wakelingfe885462016-09-22 10:24:38 +01002700 if (false_target != nullptr) {
2701 __ B(false_target);
2702 }
2703 }
2704 return;
2705 }
2706
2707 // The following code generates these patterns:
2708 // (1) true_target == nullptr && false_target != nullptr
2709 // - opposite condition true => branch to false_target
2710 // (2) true_target != nullptr && false_target == nullptr
2711 // - condition true => branch to true_target
2712 // (3) true_target != nullptr && false_target != nullptr
2713 // - condition true => branch to true_target
2714 // - branch to false_target
2715 if (IsBooleanValueOrMaterializedCondition(cond)) {
2716 // Condition has been materialized, compare the output to 0.
2717 if (kIsDebugBuild) {
2718 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
2719 DCHECK(cond_val.IsRegister());
2720 }
2721 if (true_target == nullptr) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00002722 __ CompareAndBranchIfZero(InputRegisterAt(instruction, condition_input_index),
2723 false_target,
2724 far_target);
Scott Wakelingfe885462016-09-22 10:24:38 +01002725 } else {
xueliang.zhongf51bc622016-11-04 09:23:32 +00002726 __ CompareAndBranchIfNonZero(InputRegisterAt(instruction, condition_input_index),
2727 true_target,
2728 far_target);
Scott Wakelingfe885462016-09-22 10:24:38 +01002729 }
2730 } else {
2731 // Condition has not been materialized. Use its inputs as the comparison and
2732 // its condition as the branch condition.
2733 HCondition* condition = cond->AsCondition();
2734
2735 // If this is a long or FP comparison that has been folded into
2736 // the HCondition, generate the comparison directly.
2737 Primitive::Type type = condition->InputAt(0)->GetType();
2738 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
2739 GenerateCompareTestAndBranch(condition, true_target, false_target);
2740 return;
2741 }
2742
Donghui Bai426b49c2016-11-08 14:55:38 +08002743 vixl32::Label* non_fallthrough_target;
2744 vixl32::Condition arm_cond = vixl32::Condition::None();
2745 const vixl32::Register left = InputRegisterAt(cond, 0);
2746 const Operand right = InputOperandAt(cond, 1);
2747
Scott Wakelingfe885462016-09-22 10:24:38 +01002748 if (true_target == nullptr) {
Donghui Bai426b49c2016-11-08 14:55:38 +08002749 arm_cond = ARMCondition(condition->GetOppositeCondition());
2750 non_fallthrough_target = false_target;
Scott Wakelingfe885462016-09-22 10:24:38 +01002751 } else {
Donghui Bai426b49c2016-11-08 14:55:38 +08002752 arm_cond = ARMCondition(condition->GetCondition());
2753 non_fallthrough_target = true_target;
2754 }
2755
2756 if (right.IsImmediate() && right.GetImmediate() == 0 && (arm_cond.Is(ne) || arm_cond.Is(eq))) {
2757 if (arm_cond.Is(eq)) {
2758 __ CompareAndBranchIfZero(left, non_fallthrough_target);
2759 } else {
2760 DCHECK(arm_cond.Is(ne));
2761 __ CompareAndBranchIfNonZero(left, non_fallthrough_target);
2762 }
2763 } else {
2764 __ Cmp(left, right);
2765 __ B(arm_cond, non_fallthrough_target);
Scott Wakelingfe885462016-09-22 10:24:38 +01002766 }
2767 }
2768
2769 // If neither branch falls through (case 3), the conditional branch to `true_target`
2770 // was already emitted (case 2) and we need to emit a jump to `false_target`.
2771 if (true_target != nullptr && false_target != nullptr) {
2772 __ B(false_target);
2773 }
2774}
2775
2776void LocationsBuilderARMVIXL::VisitIf(HIf* if_instr) {
2777 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
2778 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
2779 locations->SetInAt(0, Location::RequiresRegister());
2780 }
2781}
2782
2783void InstructionCodeGeneratorARMVIXL::VisitIf(HIf* if_instr) {
2784 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
2785 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002786 vixl32::Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
2787 nullptr : codegen_->GetLabelOf(true_successor);
2788 vixl32::Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
2789 nullptr : codegen_->GetLabelOf(false_successor);
Scott Wakelingfe885462016-09-22 10:24:38 +01002790 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
2791}
2792
Scott Wakelingc34dba72016-10-03 10:14:44 +01002793void LocationsBuilderARMVIXL::VisitDeoptimize(HDeoptimize* deoptimize) {
2794 LocationSummary* locations = new (GetGraph()->GetArena())
2795 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01002796 InvokeRuntimeCallingConventionARMVIXL calling_convention;
2797 RegisterSet caller_saves = RegisterSet::Empty();
2798 caller_saves.Add(LocationFrom(calling_convention.GetRegisterAt(0)));
2799 locations->SetCustomSlowPathCallerSaves(caller_saves);
Scott Wakelingc34dba72016-10-03 10:14:44 +01002800 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
2801 locations->SetInAt(0, Location::RequiresRegister());
2802 }
2803}
2804
2805void InstructionCodeGeneratorARMVIXL::VisitDeoptimize(HDeoptimize* deoptimize) {
2806 SlowPathCodeARMVIXL* slow_path =
2807 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARMVIXL>(deoptimize);
2808 GenerateTestAndBranch(deoptimize,
2809 /* condition_input_index */ 0,
2810 slow_path->GetEntryLabel(),
2811 /* false_target */ nullptr);
2812}
2813
Artem Serovd4cc5b22016-11-04 11:19:09 +00002814void LocationsBuilderARMVIXL::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
2815 LocationSummary* locations = new (GetGraph()->GetArena())
2816 LocationSummary(flag, LocationSummary::kNoCall);
2817 locations->SetOut(Location::RequiresRegister());
2818}
2819
2820void InstructionCodeGeneratorARMVIXL::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
2821 GetAssembler()->LoadFromOffset(kLoadWord,
2822 OutputRegister(flag),
2823 sp,
2824 codegen_->GetStackOffsetOfShouldDeoptimizeFlag());
2825}
2826
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002827void LocationsBuilderARMVIXL::VisitSelect(HSelect* select) {
2828 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Donghui Bai426b49c2016-11-08 14:55:38 +08002829 const bool is_floating_point = Primitive::IsFloatingPointType(select->GetType());
2830
2831 if (is_floating_point) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002832 locations->SetInAt(0, Location::RequiresFpuRegister());
Donghui Bai426b49c2016-11-08 14:55:38 +08002833 locations->SetInAt(1, Location::FpuRegisterOrConstant(select->GetTrueValue()));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002834 } else {
2835 locations->SetInAt(0, Location::RequiresRegister());
Donghui Bai426b49c2016-11-08 14:55:38 +08002836 locations->SetInAt(1, Arm8BitEncodableConstantOrRegister(select->GetTrueValue()));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002837 }
Donghui Bai426b49c2016-11-08 14:55:38 +08002838
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002839 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
Donghui Bai426b49c2016-11-08 14:55:38 +08002840 locations->SetInAt(2, Location::RegisterOrConstant(select->GetCondition()));
2841 // The code generator handles overlap with the values, but not with the condition.
2842 locations->SetOut(Location::SameAsFirstInput());
2843 } else if (is_floating_point) {
2844 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2845 } else {
2846 if (!locations->InAt(1).IsConstant()) {
2847 locations->SetInAt(0, Arm8BitEncodableConstantOrRegister(select->GetFalseValue()));
2848 }
2849
2850 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002851 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002852}
2853
2854void InstructionCodeGeneratorARMVIXL::VisitSelect(HSelect* select) {
Donghui Bai426b49c2016-11-08 14:55:38 +08002855 HInstruction* const condition = select->GetCondition();
2856 const LocationSummary* const locations = select->GetLocations();
2857 const Primitive::Type type = select->GetType();
2858 const Location first = locations->InAt(0);
2859 const Location out = locations->Out();
2860 const Location second = locations->InAt(1);
2861 Location src;
2862
2863 if (condition->IsIntConstant()) {
2864 if (condition->AsIntConstant()->IsFalse()) {
2865 src = first;
2866 } else {
2867 src = second;
2868 }
2869
2870 codegen_->MoveLocation(out, src, type);
2871 return;
2872 }
2873
2874 if (!Primitive::IsFloatingPointType(type) &&
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002875 (IsBooleanValueOrMaterializedCondition(condition) ||
2876 CanGenerateTest(condition->AsCondition(), codegen_->GetAssembler()))) {
Donghui Bai426b49c2016-11-08 14:55:38 +08002877 bool invert = false;
2878
2879 if (out.Equals(second)) {
2880 src = first;
2881 invert = true;
2882 } else if (out.Equals(first)) {
2883 src = second;
2884 } else if (second.IsConstant()) {
2885 DCHECK(CanEncodeConstantAs8BitImmediate(second.GetConstant()));
2886 src = second;
2887 } else if (first.IsConstant()) {
2888 DCHECK(CanEncodeConstantAs8BitImmediate(first.GetConstant()));
2889 src = first;
2890 invert = true;
2891 } else {
2892 src = second;
2893 }
2894
2895 if (CanGenerateConditionalMove(out, src)) {
2896 if (!out.Equals(first) && !out.Equals(second)) {
2897 codegen_->MoveLocation(out, src.Equals(first) ? second : first, type);
2898 }
2899
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002900 std::pair<vixl32::Condition, vixl32::Condition> cond(eq, ne);
2901
2902 if (IsBooleanValueOrMaterializedCondition(condition)) {
2903 __ Cmp(InputRegisterAt(select, 2), 0);
2904 cond = invert ? std::make_pair(eq, ne) : std::make_pair(ne, eq);
2905 } else {
2906 cond = GenerateTest(condition->AsCondition(), invert, codegen_);
2907 }
2908
Donghui Bai426b49c2016-11-08 14:55:38 +08002909 const size_t instr_count = out.IsRegisterPair() ? 4 : 2;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002910 // We use the scope because of the IT block that follows.
Donghui Bai426b49c2016-11-08 14:55:38 +08002911 ExactAssemblyScope guard(GetVIXLAssembler(),
2912 instr_count * vixl32::k16BitT32InstructionSizeInBytes,
2913 CodeBufferCheckScope::kExactSize);
2914
2915 if (out.IsRegister()) {
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002916 __ it(cond.first);
2917 __ mov(cond.first, RegisterFrom(out), OperandFrom(src, type));
Donghui Bai426b49c2016-11-08 14:55:38 +08002918 } else {
2919 DCHECK(out.IsRegisterPair());
2920
2921 Operand operand_high(0);
2922 Operand operand_low(0);
2923
2924 if (src.IsConstant()) {
2925 const int64_t value = Int64ConstantFrom(src);
2926
2927 operand_high = High32Bits(value);
2928 operand_low = Low32Bits(value);
2929 } else {
2930 DCHECK(src.IsRegisterPair());
2931 operand_high = HighRegisterFrom(src);
2932 operand_low = LowRegisterFrom(src);
2933 }
2934
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002935 __ it(cond.first);
2936 __ mov(cond.first, LowRegisterFrom(out), operand_low);
2937 __ it(cond.first);
2938 __ mov(cond.first, HighRegisterFrom(out), operand_high);
Donghui Bai426b49c2016-11-08 14:55:38 +08002939 }
2940
2941 return;
2942 }
2943 }
2944
2945 vixl32::Label* false_target = nullptr;
2946 vixl32::Label* true_target = nullptr;
2947 vixl32::Label select_end;
2948 vixl32::Label* const target = codegen_->GetFinalLabel(select, &select_end);
2949
2950 if (out.Equals(second)) {
2951 true_target = target;
2952 src = first;
2953 } else {
2954 false_target = target;
2955 src = second;
2956
2957 if (!out.Equals(first)) {
2958 codegen_->MoveLocation(out, first, type);
2959 }
2960 }
2961
2962 GenerateTestAndBranch(select, 2, true_target, false_target, /* far_target */ false);
2963 codegen_->MoveLocation(out, src, type);
2964
2965 if (select_end.IsReferenced()) {
2966 __ Bind(&select_end);
2967 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002968}
2969
Artem Serov551b28f2016-10-18 19:11:30 +01002970void LocationsBuilderARMVIXL::VisitNativeDebugInfo(HNativeDebugInfo* info) {
2971 new (GetGraph()->GetArena()) LocationSummary(info);
2972}
2973
2974void InstructionCodeGeneratorARMVIXL::VisitNativeDebugInfo(HNativeDebugInfo*) {
2975 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
2976}
2977
Scott Wakelingfe885462016-09-22 10:24:38 +01002978void CodeGeneratorARMVIXL::GenerateNop() {
2979 __ Nop();
2980}
2981
2982void LocationsBuilderARMVIXL::HandleCondition(HCondition* cond) {
2983 LocationSummary* locations =
2984 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
2985 // Handle the long/FP comparisons made in instruction simplification.
2986 switch (cond->InputAt(0)->GetType()) {
2987 case Primitive::kPrimLong:
2988 locations->SetInAt(0, Location::RequiresRegister());
2989 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
2990 if (!cond->IsEmittedAtUseSite()) {
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002991 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Scott Wakelingfe885462016-09-22 10:24:38 +01002992 }
2993 break;
2994
Scott Wakelingfe885462016-09-22 10:24:38 +01002995 case Primitive::kPrimFloat:
2996 case Primitive::kPrimDouble:
2997 locations->SetInAt(0, Location::RequiresFpuRegister());
Artem Serov657022c2016-11-23 14:19:38 +00002998 locations->SetInAt(1, ArithmeticZeroOrFpuRegister(cond->InputAt(1)));
Scott Wakelingfe885462016-09-22 10:24:38 +01002999 if (!cond->IsEmittedAtUseSite()) {
3000 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3001 }
3002 break;
3003
3004 default:
3005 locations->SetInAt(0, Location::RequiresRegister());
3006 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
3007 if (!cond->IsEmittedAtUseSite()) {
3008 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3009 }
3010 }
3011}
3012
3013void InstructionCodeGeneratorARMVIXL::HandleCondition(HCondition* cond) {
3014 if (cond->IsEmittedAtUseSite()) {
3015 return;
3016 }
3017
Nicolas Geoffray30826612017-05-10 11:59:26 +00003018 const vixl32::Register out = OutputRegister(cond);
Scott Wakelingfe885462016-09-22 10:24:38 +01003019
Nicolas Geoffray30826612017-05-10 11:59:26 +00003020 if (out.IsLow() && CanGenerateTest(cond, codegen_->GetAssembler())) {
3021 const auto condition = GenerateTest(cond, false, codegen_);
3022 // We use the scope because of the IT block that follows.
3023 ExactAssemblyScope guard(GetVIXLAssembler(),
3024 4 * vixl32::k16BitT32InstructionSizeInBytes,
3025 CodeBufferCheckScope::kExactSize);
3026
3027 __ it(condition.first);
3028 __ mov(condition.first, out, 1);
3029 __ it(condition.second);
3030 __ mov(condition.second, out, 0);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003031 return;
Scott Wakelingfe885462016-09-22 10:24:38 +01003032 }
3033
Nicolas Geoffray30826612017-05-10 11:59:26 +00003034 // Convert the jumps into the result.
3035 vixl32::Label done_label;
3036 vixl32::Label* const final_label = codegen_->GetFinalLabel(cond, &done_label);
Scott Wakelingfe885462016-09-22 10:24:38 +01003037
Nicolas Geoffray30826612017-05-10 11:59:26 +00003038 if (cond->InputAt(0)->GetType() == Primitive::kPrimLong) {
3039 vixl32::Label true_label, false_label;
Scott Wakelingfe885462016-09-22 10:24:38 +01003040
Nicolas Geoffray30826612017-05-10 11:59:26 +00003041 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003042
Nicolas Geoffray30826612017-05-10 11:59:26 +00003043 // False case: result = 0.
3044 __ Bind(&false_label);
3045 __ Mov(out, 0);
3046 __ B(final_label);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003047
Nicolas Geoffray30826612017-05-10 11:59:26 +00003048 // True case: result = 1.
3049 __ Bind(&true_label);
3050 __ Mov(out, 1);
3051 } else {
3052 DCHECK(CanGenerateTest(cond, codegen_->GetAssembler()));
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003053
Nicolas Geoffray30826612017-05-10 11:59:26 +00003054 const auto condition = GenerateTest(cond, false, codegen_);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003055
Nicolas Geoffray30826612017-05-10 11:59:26 +00003056 __ Mov(LeaveFlags, out, 0);
3057 __ B(condition.second, final_label, /* far_target */ false);
3058 __ Mov(out, 1);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003059 }
Anton Kirilov6f644202017-02-27 18:29:45 +00003060
Nicolas Geoffray30826612017-05-10 11:59:26 +00003061 if (done_label.IsReferenced()) {
3062 __ Bind(&done_label);
3063 }
Scott Wakelingfe885462016-09-22 10:24:38 +01003064}
3065
3066void LocationsBuilderARMVIXL::VisitEqual(HEqual* comp) {
3067 HandleCondition(comp);
3068}
3069
3070void InstructionCodeGeneratorARMVIXL::VisitEqual(HEqual* comp) {
3071 HandleCondition(comp);
3072}
3073
3074void LocationsBuilderARMVIXL::VisitNotEqual(HNotEqual* comp) {
3075 HandleCondition(comp);
3076}
3077
3078void InstructionCodeGeneratorARMVIXL::VisitNotEqual(HNotEqual* comp) {
3079 HandleCondition(comp);
3080}
3081
3082void LocationsBuilderARMVIXL::VisitLessThan(HLessThan* comp) {
3083 HandleCondition(comp);
3084}
3085
3086void InstructionCodeGeneratorARMVIXL::VisitLessThan(HLessThan* comp) {
3087 HandleCondition(comp);
3088}
3089
3090void LocationsBuilderARMVIXL::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
3091 HandleCondition(comp);
3092}
3093
3094void InstructionCodeGeneratorARMVIXL::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
3095 HandleCondition(comp);
3096}
3097
3098void LocationsBuilderARMVIXL::VisitGreaterThan(HGreaterThan* comp) {
3099 HandleCondition(comp);
3100}
3101
3102void InstructionCodeGeneratorARMVIXL::VisitGreaterThan(HGreaterThan* comp) {
3103 HandleCondition(comp);
3104}
3105
3106void LocationsBuilderARMVIXL::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
3107 HandleCondition(comp);
3108}
3109
3110void InstructionCodeGeneratorARMVIXL::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
3111 HandleCondition(comp);
3112}
3113
3114void LocationsBuilderARMVIXL::VisitBelow(HBelow* comp) {
3115 HandleCondition(comp);
3116}
3117
3118void InstructionCodeGeneratorARMVIXL::VisitBelow(HBelow* comp) {
3119 HandleCondition(comp);
3120}
3121
3122void LocationsBuilderARMVIXL::VisitBelowOrEqual(HBelowOrEqual* comp) {
3123 HandleCondition(comp);
3124}
3125
3126void InstructionCodeGeneratorARMVIXL::VisitBelowOrEqual(HBelowOrEqual* comp) {
3127 HandleCondition(comp);
3128}
3129
3130void LocationsBuilderARMVIXL::VisitAbove(HAbove* comp) {
3131 HandleCondition(comp);
3132}
3133
3134void InstructionCodeGeneratorARMVIXL::VisitAbove(HAbove* comp) {
3135 HandleCondition(comp);
3136}
3137
3138void LocationsBuilderARMVIXL::VisitAboveOrEqual(HAboveOrEqual* comp) {
3139 HandleCondition(comp);
3140}
3141
3142void InstructionCodeGeneratorARMVIXL::VisitAboveOrEqual(HAboveOrEqual* comp) {
3143 HandleCondition(comp);
3144}
3145
3146void LocationsBuilderARMVIXL::VisitIntConstant(HIntConstant* constant) {
3147 LocationSummary* locations =
3148 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3149 locations->SetOut(Location::ConstantLocation(constant));
3150}
3151
3152void InstructionCodeGeneratorARMVIXL::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
3153 // Will be generated at use site.
3154}
3155
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003156void LocationsBuilderARMVIXL::VisitNullConstant(HNullConstant* constant) {
3157 LocationSummary* locations =
3158 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3159 locations->SetOut(Location::ConstantLocation(constant));
3160}
3161
3162void InstructionCodeGeneratorARMVIXL::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
3163 // Will be generated at use site.
3164}
3165
Scott Wakelingfe885462016-09-22 10:24:38 +01003166void LocationsBuilderARMVIXL::VisitLongConstant(HLongConstant* constant) {
3167 LocationSummary* locations =
3168 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3169 locations->SetOut(Location::ConstantLocation(constant));
3170}
3171
3172void InstructionCodeGeneratorARMVIXL::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
3173 // Will be generated at use site.
3174}
3175
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01003176void LocationsBuilderARMVIXL::VisitFloatConstant(HFloatConstant* constant) {
3177 LocationSummary* locations =
3178 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3179 locations->SetOut(Location::ConstantLocation(constant));
3180}
3181
Scott Wakelingc34dba72016-10-03 10:14:44 +01003182void InstructionCodeGeneratorARMVIXL::VisitFloatConstant(
3183 HFloatConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01003184 // Will be generated at use site.
3185}
3186
3187void LocationsBuilderARMVIXL::VisitDoubleConstant(HDoubleConstant* constant) {
3188 LocationSummary* locations =
3189 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3190 locations->SetOut(Location::ConstantLocation(constant));
3191}
3192
Scott Wakelingc34dba72016-10-03 10:14:44 +01003193void InstructionCodeGeneratorARMVIXL::VisitDoubleConstant(
3194 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01003195 // Will be generated at use site.
3196}
3197
Igor Murashkind01745e2017-04-05 16:40:31 -07003198void LocationsBuilderARMVIXL::VisitConstructorFence(HConstructorFence* constructor_fence) {
3199 constructor_fence->SetLocations(nullptr);
3200}
3201
3202void InstructionCodeGeneratorARMVIXL::VisitConstructorFence(
3203 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
3204 codegen_->GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
3205}
3206
Scott Wakelingfe885462016-09-22 10:24:38 +01003207void LocationsBuilderARMVIXL::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3208 memory_barrier->SetLocations(nullptr);
3209}
3210
3211void InstructionCodeGeneratorARMVIXL::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3212 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
3213}
3214
3215void LocationsBuilderARMVIXL::VisitReturnVoid(HReturnVoid* ret) {
3216 ret->SetLocations(nullptr);
3217}
3218
3219void InstructionCodeGeneratorARMVIXL::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
3220 codegen_->GenerateFrameExit();
3221}
3222
3223void LocationsBuilderARMVIXL::VisitReturn(HReturn* ret) {
3224 LocationSummary* locations =
3225 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
3226 locations->SetInAt(0, parameter_visitor_.GetReturnLocation(ret->InputAt(0)->GetType()));
3227}
3228
3229void InstructionCodeGeneratorARMVIXL::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
3230 codegen_->GenerateFrameExit();
3231}
3232
Artem Serovcfbe9132016-10-14 15:58:56 +01003233void LocationsBuilderARMVIXL::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3234 // The trampoline uses the same calling convention as dex calling conventions,
3235 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
3236 // the method_idx.
3237 HandleInvoke(invoke);
3238}
3239
3240void InstructionCodeGeneratorARMVIXL::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3241 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
3242}
3243
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003244void LocationsBuilderARMVIXL::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
3245 // Explicit clinit checks triggered by static invokes must have been pruned by
3246 // art::PrepareForRegisterAllocation.
3247 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
3248
Anton Kirilov5ec62182016-10-13 20:16:02 +01003249 IntrinsicLocationsBuilderARMVIXL intrinsic(codegen_);
3250 if (intrinsic.TryDispatch(invoke)) {
3251 if (invoke->GetLocations()->CanCall() && invoke->HasPcRelativeDexCache()) {
3252 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
3253 }
3254 return;
3255 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003256
3257 HandleInvoke(invoke);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01003258
Artem Serovd4cc5b22016-11-04 11:19:09 +00003259 // For PC-relative dex cache the invoke has an extra input, the PC-relative address base.
3260 if (invoke->HasPcRelativeDexCache()) {
3261 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
3262 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003263}
3264
Anton Kirilov5ec62182016-10-13 20:16:02 +01003265static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARMVIXL* codegen) {
3266 if (invoke->GetLocations()->Intrinsified()) {
3267 IntrinsicCodeGeneratorARMVIXL intrinsic(codegen);
3268 intrinsic.Dispatch(invoke);
3269 return true;
3270 }
3271 return false;
3272}
3273
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003274void InstructionCodeGeneratorARMVIXL::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
3275 // Explicit clinit checks triggered by static invokes must have been pruned by
3276 // art::PrepareForRegisterAllocation.
3277 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
3278
Anton Kirilov5ec62182016-10-13 20:16:02 +01003279 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3280 return;
3281 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003282
3283 LocationSummary* locations = invoke->GetLocations();
Artem Serovd4cc5b22016-11-04 11:19:09 +00003284 codegen_->GenerateStaticOrDirectCall(
3285 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003286 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3287}
3288
3289void LocationsBuilderARMVIXL::HandleInvoke(HInvoke* invoke) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00003290 InvokeDexCallingConventionVisitorARMVIXL calling_convention_visitor;
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003291 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
3292}
3293
3294void LocationsBuilderARMVIXL::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Anton Kirilov5ec62182016-10-13 20:16:02 +01003295 IntrinsicLocationsBuilderARMVIXL intrinsic(codegen_);
3296 if (intrinsic.TryDispatch(invoke)) {
3297 return;
3298 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003299
3300 HandleInvoke(invoke);
3301}
3302
3303void InstructionCodeGeneratorARMVIXL::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Anton Kirilov5ec62182016-10-13 20:16:02 +01003304 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3305 return;
3306 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003307
3308 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003309 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Alexandre Rames374ddf32016-11-04 10:40:49 +00003310 DCHECK(!codegen_->IsLeafMethod());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003311}
3312
Artem Serovcfbe9132016-10-14 15:58:56 +01003313void LocationsBuilderARMVIXL::VisitInvokeInterface(HInvokeInterface* invoke) {
3314 HandleInvoke(invoke);
3315 // Add the hidden argument.
3316 invoke->GetLocations()->AddTemp(LocationFrom(r12));
3317}
3318
3319void InstructionCodeGeneratorARMVIXL::VisitInvokeInterface(HInvokeInterface* invoke) {
3320 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
3321 LocationSummary* locations = invoke->GetLocations();
3322 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
3323 vixl32::Register hidden_reg = RegisterFrom(locations->GetTemp(1));
3324 Location receiver = locations->InAt(0);
3325 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3326
3327 DCHECK(!receiver.IsStackSlot());
3328
Alexandre Rames374ddf32016-11-04 10:40:49 +00003329 // Ensure the pc position is recorded immediately after the `ldr` instruction.
3330 {
Artem Serov0fb37192016-12-06 18:13:40 +00003331 ExactAssemblyScope aas(GetVIXLAssembler(),
3332 vixl32::kMaxInstructionSizeInBytes,
3333 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00003334 // /* HeapReference<Class> */ temp = receiver->klass_
3335 __ ldr(temp, MemOperand(RegisterFrom(receiver), class_offset));
3336 codegen_->MaybeRecordImplicitNullCheck(invoke);
3337 }
Artem Serovcfbe9132016-10-14 15:58:56 +01003338 // Instead of simply (possibly) unpoisoning `temp` here, we should
3339 // emit a read barrier for the previous class reference load.
3340 // However this is not required in practice, as this is an
3341 // intermediate/temporary reference and because the current
3342 // concurrent copying collector keeps the from-space memory
3343 // intact/accessible until the end of the marking phase (the
3344 // concurrent copying collector may not in the future).
3345 GetAssembler()->MaybeUnpoisonHeapReference(temp);
3346 GetAssembler()->LoadFromOffset(kLoadWord,
3347 temp,
3348 temp,
3349 mirror::Class::ImtPtrOffset(kArmPointerSize).Uint32Value());
3350 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
3351 invoke->GetImtIndex(), kArmPointerSize));
3352 // temp = temp->GetImtEntryAt(method_offset);
3353 GetAssembler()->LoadFromOffset(kLoadWord, temp, temp, method_offset);
3354 uint32_t entry_point =
3355 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize).Int32Value();
3356 // LR = temp->GetEntryPoint();
3357 GetAssembler()->LoadFromOffset(kLoadWord, lr, temp, entry_point);
3358
3359 // Set the hidden (in r12) argument. It is done here, right before a BLX to prevent other
3360 // instruction from clobbering it as they might use r12 as a scratch register.
3361 DCHECK(hidden_reg.Is(r12));
Scott Wakelingb77051e2016-11-21 19:46:00 +00003362
3363 {
3364 // The VIXL macro assembler may clobber any of the scratch registers that are available to it,
3365 // so it checks if the application is using them (by passing them to the macro assembler
3366 // methods). The following application of UseScratchRegisterScope corrects VIXL's notion of
3367 // what is available, and is the opposite of the standard usage: Instead of requesting a
3368 // temporary location, it imposes an external constraint (i.e. a specific register is reserved
3369 // for the hidden argument). Note that this works even if VIXL needs a scratch register itself
3370 // (to materialize the constant), since the destination register becomes available for such use
3371 // internally for the duration of the macro instruction.
3372 UseScratchRegisterScope temps(GetVIXLAssembler());
3373 temps.Exclude(hidden_reg);
3374 __ Mov(hidden_reg, invoke->GetDexMethodIndex());
3375 }
Artem Serovcfbe9132016-10-14 15:58:56 +01003376 {
Alexandre Rames374ddf32016-11-04 10:40:49 +00003377 // Ensure the pc position is recorded immediately after the `blx` instruction.
3378 // 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 +00003379 ExactAssemblyScope aas(GetVIXLAssembler(),
Alexandre Rames374ddf32016-11-04 10:40:49 +00003380 vixl32::k16BitT32InstructionSizeInBytes,
3381 CodeBufferCheckScope::kExactSize);
Artem Serovcfbe9132016-10-14 15:58:56 +01003382 // LR();
3383 __ blx(lr);
Artem Serovcfbe9132016-10-14 15:58:56 +01003384 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Alexandre Rames374ddf32016-11-04 10:40:49 +00003385 DCHECK(!codegen_->IsLeafMethod());
Artem Serovcfbe9132016-10-14 15:58:56 +01003386 }
3387}
3388
Orion Hodsonac141392017-01-13 11:53:47 +00003389void LocationsBuilderARMVIXL::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
3390 HandleInvoke(invoke);
3391}
3392
3393void InstructionCodeGeneratorARMVIXL::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
3394 codegen_->GenerateInvokePolymorphicCall(invoke);
3395}
3396
Artem Serov02109dd2016-09-23 17:17:54 +01003397void LocationsBuilderARMVIXL::VisitNeg(HNeg* neg) {
3398 LocationSummary* locations =
3399 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
3400 switch (neg->GetResultType()) {
3401 case Primitive::kPrimInt: {
3402 locations->SetInAt(0, Location::RequiresRegister());
3403 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3404 break;
3405 }
3406 case Primitive::kPrimLong: {
3407 locations->SetInAt(0, Location::RequiresRegister());
3408 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3409 break;
3410 }
3411
3412 case Primitive::kPrimFloat:
3413 case Primitive::kPrimDouble:
3414 locations->SetInAt(0, Location::RequiresFpuRegister());
3415 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3416 break;
3417
3418 default:
3419 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
3420 }
3421}
3422
3423void InstructionCodeGeneratorARMVIXL::VisitNeg(HNeg* neg) {
3424 LocationSummary* locations = neg->GetLocations();
3425 Location out = locations->Out();
3426 Location in = locations->InAt(0);
3427 switch (neg->GetResultType()) {
3428 case Primitive::kPrimInt:
3429 __ Rsb(OutputRegister(neg), InputRegisterAt(neg, 0), 0);
3430 break;
3431
3432 case Primitive::kPrimLong:
3433 // out.lo = 0 - in.lo (and update the carry/borrow (C) flag)
3434 __ Rsbs(LowRegisterFrom(out), LowRegisterFrom(in), 0);
3435 // We cannot emit an RSC (Reverse Subtract with Carry)
3436 // instruction here, as it does not exist in the Thumb-2
3437 // instruction set. We use the following approach
3438 // using SBC and SUB instead.
3439 //
3440 // out.hi = -C
3441 __ Sbc(HighRegisterFrom(out), HighRegisterFrom(out), HighRegisterFrom(out));
3442 // out.hi = out.hi - in.hi
3443 __ Sub(HighRegisterFrom(out), HighRegisterFrom(out), HighRegisterFrom(in));
3444 break;
3445
3446 case Primitive::kPrimFloat:
3447 case Primitive::kPrimDouble:
Anton Kirilov644032c2016-12-06 17:51:43 +00003448 __ Vneg(OutputVRegister(neg), InputVRegister(neg));
Artem Serov02109dd2016-09-23 17:17:54 +01003449 break;
3450
3451 default:
3452 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
3453 }
3454}
3455
Scott Wakelingfe885462016-09-22 10:24:38 +01003456void LocationsBuilderARMVIXL::VisitTypeConversion(HTypeConversion* conversion) {
3457 Primitive::Type result_type = conversion->GetResultType();
3458 Primitive::Type input_type = conversion->GetInputType();
3459 DCHECK_NE(result_type, input_type);
3460
3461 // The float-to-long, double-to-long and long-to-float type conversions
3462 // rely on a call to the runtime.
3463 LocationSummary::CallKind call_kind =
3464 (((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
3465 && result_type == Primitive::kPrimLong)
3466 || (input_type == Primitive::kPrimLong && result_type == Primitive::kPrimFloat))
3467 ? LocationSummary::kCallOnMainOnly
3468 : LocationSummary::kNoCall;
3469 LocationSummary* locations =
3470 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
3471
3472 // The Java language does not allow treating boolean as an integral type but
3473 // our bit representation makes it safe.
3474
3475 switch (result_type) {
3476 case Primitive::kPrimByte:
3477 switch (input_type) {
3478 case Primitive::kPrimLong:
3479 // Type conversion from long to byte is a result of code transformations.
3480 case Primitive::kPrimBoolean:
3481 // Boolean input is a result of code transformations.
3482 case Primitive::kPrimShort:
3483 case Primitive::kPrimInt:
3484 case Primitive::kPrimChar:
3485 // Processing a Dex `int-to-byte' instruction.
3486 locations->SetInAt(0, Location::RequiresRegister());
3487 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3488 break;
3489
3490 default:
3491 LOG(FATAL) << "Unexpected type conversion from " << input_type
3492 << " to " << result_type;
3493 }
3494 break;
3495
3496 case Primitive::kPrimShort:
3497 switch (input_type) {
3498 case Primitive::kPrimLong:
3499 // Type conversion from long to short is a result of code transformations.
3500 case Primitive::kPrimBoolean:
3501 // Boolean input is a result of code transformations.
3502 case Primitive::kPrimByte:
3503 case Primitive::kPrimInt:
3504 case Primitive::kPrimChar:
3505 // Processing a Dex `int-to-short' instruction.
3506 locations->SetInAt(0, Location::RequiresRegister());
3507 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3508 break;
3509
3510 default:
3511 LOG(FATAL) << "Unexpected type conversion from " << input_type
3512 << " to " << result_type;
3513 }
3514 break;
3515
3516 case Primitive::kPrimInt:
3517 switch (input_type) {
3518 case Primitive::kPrimLong:
3519 // Processing a Dex `long-to-int' instruction.
3520 locations->SetInAt(0, Location::Any());
3521 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3522 break;
3523
3524 case Primitive::kPrimFloat:
3525 // Processing a Dex `float-to-int' instruction.
3526 locations->SetInAt(0, Location::RequiresFpuRegister());
3527 locations->SetOut(Location::RequiresRegister());
3528 locations->AddTemp(Location::RequiresFpuRegister());
3529 break;
3530
3531 case Primitive::kPrimDouble:
3532 // Processing a Dex `double-to-int' instruction.
3533 locations->SetInAt(0, Location::RequiresFpuRegister());
3534 locations->SetOut(Location::RequiresRegister());
3535 locations->AddTemp(Location::RequiresFpuRegister());
3536 break;
3537
3538 default:
3539 LOG(FATAL) << "Unexpected type conversion from " << input_type
3540 << " to " << result_type;
3541 }
3542 break;
3543
3544 case Primitive::kPrimLong:
3545 switch (input_type) {
3546 case Primitive::kPrimBoolean:
3547 // Boolean input is a result of code transformations.
3548 case Primitive::kPrimByte:
3549 case Primitive::kPrimShort:
3550 case Primitive::kPrimInt:
3551 case Primitive::kPrimChar:
3552 // Processing a Dex `int-to-long' instruction.
3553 locations->SetInAt(0, Location::RequiresRegister());
3554 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3555 break;
3556
3557 case Primitive::kPrimFloat: {
3558 // Processing a Dex `float-to-long' instruction.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003559 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3560 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
3561 locations->SetOut(LocationFrom(r0, r1));
Scott Wakelingfe885462016-09-22 10:24:38 +01003562 break;
3563 }
3564
3565 case Primitive::kPrimDouble: {
3566 // Processing a Dex `double-to-long' instruction.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003567 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3568 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0),
3569 calling_convention.GetFpuRegisterAt(1)));
3570 locations->SetOut(LocationFrom(r0, r1));
Scott Wakelingfe885462016-09-22 10:24:38 +01003571 break;
3572 }
3573
3574 default:
3575 LOG(FATAL) << "Unexpected type conversion from " << input_type
3576 << " to " << result_type;
3577 }
3578 break;
3579
3580 case Primitive::kPrimChar:
3581 switch (input_type) {
3582 case Primitive::kPrimLong:
3583 // Type conversion from long to char is a result of code transformations.
3584 case Primitive::kPrimBoolean:
3585 // Boolean input is a result of code transformations.
3586 case Primitive::kPrimByte:
3587 case Primitive::kPrimShort:
3588 case Primitive::kPrimInt:
3589 // Processing a Dex `int-to-char' instruction.
3590 locations->SetInAt(0, Location::RequiresRegister());
3591 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3592 break;
3593
3594 default:
3595 LOG(FATAL) << "Unexpected type conversion from " << input_type
3596 << " to " << result_type;
3597 }
3598 break;
3599
3600 case Primitive::kPrimFloat:
3601 switch (input_type) {
3602 case Primitive::kPrimBoolean:
3603 // Boolean input is a result of code transformations.
3604 case Primitive::kPrimByte:
3605 case Primitive::kPrimShort:
3606 case Primitive::kPrimInt:
3607 case Primitive::kPrimChar:
3608 // Processing a Dex `int-to-float' instruction.
3609 locations->SetInAt(0, Location::RequiresRegister());
3610 locations->SetOut(Location::RequiresFpuRegister());
3611 break;
3612
3613 case Primitive::kPrimLong: {
3614 // Processing a Dex `long-to-float' instruction.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003615 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3616 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0),
3617 calling_convention.GetRegisterAt(1)));
3618 locations->SetOut(LocationFrom(calling_convention.GetFpuRegisterAt(0)));
Scott Wakelingfe885462016-09-22 10:24:38 +01003619 break;
3620 }
3621
3622 case Primitive::kPrimDouble:
3623 // Processing a Dex `double-to-float' instruction.
3624 locations->SetInAt(0, Location::RequiresFpuRegister());
3625 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3626 break;
3627
3628 default:
3629 LOG(FATAL) << "Unexpected type conversion from " << input_type
3630 << " to " << result_type;
3631 };
3632 break;
3633
3634 case Primitive::kPrimDouble:
3635 switch (input_type) {
3636 case Primitive::kPrimBoolean:
3637 // Boolean input is a result of code transformations.
3638 case Primitive::kPrimByte:
3639 case Primitive::kPrimShort:
3640 case Primitive::kPrimInt:
3641 case Primitive::kPrimChar:
3642 // Processing a Dex `int-to-double' instruction.
3643 locations->SetInAt(0, Location::RequiresRegister());
3644 locations->SetOut(Location::RequiresFpuRegister());
3645 break;
3646
3647 case Primitive::kPrimLong:
3648 // Processing a Dex `long-to-double' instruction.
3649 locations->SetInAt(0, Location::RequiresRegister());
3650 locations->SetOut(Location::RequiresFpuRegister());
3651 locations->AddTemp(Location::RequiresFpuRegister());
3652 locations->AddTemp(Location::RequiresFpuRegister());
3653 break;
3654
3655 case Primitive::kPrimFloat:
3656 // Processing a Dex `float-to-double' instruction.
3657 locations->SetInAt(0, Location::RequiresFpuRegister());
3658 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3659 break;
3660
3661 default:
3662 LOG(FATAL) << "Unexpected type conversion from " << input_type
3663 << " to " << result_type;
3664 };
3665 break;
3666
3667 default:
3668 LOG(FATAL) << "Unexpected type conversion from " << input_type
3669 << " to " << result_type;
3670 }
3671}
3672
3673void InstructionCodeGeneratorARMVIXL::VisitTypeConversion(HTypeConversion* conversion) {
3674 LocationSummary* locations = conversion->GetLocations();
3675 Location out = locations->Out();
3676 Location in = locations->InAt(0);
3677 Primitive::Type result_type = conversion->GetResultType();
3678 Primitive::Type input_type = conversion->GetInputType();
3679 DCHECK_NE(result_type, input_type);
3680 switch (result_type) {
3681 case Primitive::kPrimByte:
3682 switch (input_type) {
3683 case Primitive::kPrimLong:
3684 // Type conversion from long to byte is a result of code transformations.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003685 __ Sbfx(OutputRegister(conversion), LowRegisterFrom(in), 0, 8);
Scott Wakelingfe885462016-09-22 10:24:38 +01003686 break;
3687 case Primitive::kPrimBoolean:
3688 // Boolean input is a result of code transformations.
3689 case Primitive::kPrimShort:
3690 case Primitive::kPrimInt:
3691 case Primitive::kPrimChar:
3692 // Processing a Dex `int-to-byte' instruction.
3693 __ Sbfx(OutputRegister(conversion), InputRegisterAt(conversion, 0), 0, 8);
3694 break;
3695
3696 default:
3697 LOG(FATAL) << "Unexpected type conversion from " << input_type
3698 << " to " << result_type;
3699 }
3700 break;
3701
3702 case Primitive::kPrimShort:
3703 switch (input_type) {
3704 case Primitive::kPrimLong:
3705 // Type conversion from long to short is a result of code transformations.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003706 __ Sbfx(OutputRegister(conversion), LowRegisterFrom(in), 0, 16);
Scott Wakelingfe885462016-09-22 10:24:38 +01003707 break;
3708 case Primitive::kPrimBoolean:
3709 // Boolean input is a result of code transformations.
3710 case Primitive::kPrimByte:
3711 case Primitive::kPrimInt:
3712 case Primitive::kPrimChar:
3713 // Processing a Dex `int-to-short' instruction.
3714 __ Sbfx(OutputRegister(conversion), InputRegisterAt(conversion, 0), 0, 16);
3715 break;
3716
3717 default:
3718 LOG(FATAL) << "Unexpected type conversion from " << input_type
3719 << " to " << result_type;
3720 }
3721 break;
3722
3723 case Primitive::kPrimInt:
3724 switch (input_type) {
3725 case Primitive::kPrimLong:
3726 // Processing a Dex `long-to-int' instruction.
3727 DCHECK(out.IsRegister());
3728 if (in.IsRegisterPair()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003729 __ Mov(OutputRegister(conversion), LowRegisterFrom(in));
Scott Wakelingfe885462016-09-22 10:24:38 +01003730 } else if (in.IsDoubleStackSlot()) {
3731 GetAssembler()->LoadFromOffset(kLoadWord,
3732 OutputRegister(conversion),
3733 sp,
3734 in.GetStackIndex());
3735 } else {
3736 DCHECK(in.IsConstant());
3737 DCHECK(in.GetConstant()->IsLongConstant());
Vladimir Markoba1a48e2017-04-13 11:50:14 +01003738 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
3739 __ Mov(OutputRegister(conversion), static_cast<int32_t>(value));
Scott Wakelingfe885462016-09-22 10:24:38 +01003740 }
3741 break;
3742
3743 case Primitive::kPrimFloat: {
3744 // Processing a Dex `float-to-int' instruction.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003745 vixl32::SRegister temp = LowSRegisterFrom(locations->GetTemp(0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01003746 __ Vcvt(S32, F32, temp, InputSRegisterAt(conversion, 0));
Scott Wakelingfe885462016-09-22 10:24:38 +01003747 __ Vmov(OutputRegister(conversion), temp);
3748 break;
3749 }
3750
3751 case Primitive::kPrimDouble: {
3752 // Processing a Dex `double-to-int' instruction.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003753 vixl32::SRegister temp_s = LowSRegisterFrom(locations->GetTemp(0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01003754 __ Vcvt(S32, F64, temp_s, DRegisterFrom(in));
Scott Wakelingfe885462016-09-22 10:24:38 +01003755 __ Vmov(OutputRegister(conversion), temp_s);
3756 break;
3757 }
3758
3759 default:
3760 LOG(FATAL) << "Unexpected type conversion from " << input_type
3761 << " to " << result_type;
3762 }
3763 break;
3764
3765 case Primitive::kPrimLong:
3766 switch (input_type) {
3767 case Primitive::kPrimBoolean:
3768 // Boolean input is a result of code transformations.
3769 case Primitive::kPrimByte:
3770 case Primitive::kPrimShort:
3771 case Primitive::kPrimInt:
3772 case Primitive::kPrimChar:
3773 // Processing a Dex `int-to-long' instruction.
3774 DCHECK(out.IsRegisterPair());
3775 DCHECK(in.IsRegister());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003776 __ Mov(LowRegisterFrom(out), InputRegisterAt(conversion, 0));
Scott Wakelingfe885462016-09-22 10:24:38 +01003777 // Sign extension.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003778 __ Asr(HighRegisterFrom(out), LowRegisterFrom(out), 31);
Scott Wakelingfe885462016-09-22 10:24:38 +01003779 break;
3780
3781 case Primitive::kPrimFloat:
3782 // Processing a Dex `float-to-long' instruction.
3783 codegen_->InvokeRuntime(kQuickF2l, conversion, conversion->GetDexPc());
3784 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
3785 break;
3786
3787 case Primitive::kPrimDouble:
3788 // Processing a Dex `double-to-long' instruction.
3789 codegen_->InvokeRuntime(kQuickD2l, conversion, conversion->GetDexPc());
3790 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
3791 break;
3792
3793 default:
3794 LOG(FATAL) << "Unexpected type conversion from " << input_type
3795 << " to " << result_type;
3796 }
3797 break;
3798
3799 case Primitive::kPrimChar:
3800 switch (input_type) {
3801 case Primitive::kPrimLong:
3802 // Type conversion from long to char is a result of code transformations.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003803 __ Ubfx(OutputRegister(conversion), LowRegisterFrom(in), 0, 16);
Scott Wakelingfe885462016-09-22 10:24:38 +01003804 break;
3805 case Primitive::kPrimBoolean:
3806 // Boolean input is a result of code transformations.
3807 case Primitive::kPrimByte:
3808 case Primitive::kPrimShort:
3809 case Primitive::kPrimInt:
3810 // Processing a Dex `int-to-char' instruction.
3811 __ Ubfx(OutputRegister(conversion), InputRegisterAt(conversion, 0), 0, 16);
3812 break;
3813
3814 default:
3815 LOG(FATAL) << "Unexpected type conversion from " << input_type
3816 << " to " << result_type;
3817 }
3818 break;
3819
3820 case Primitive::kPrimFloat:
3821 switch (input_type) {
3822 case Primitive::kPrimBoolean:
3823 // Boolean input is a result of code transformations.
3824 case Primitive::kPrimByte:
3825 case Primitive::kPrimShort:
3826 case Primitive::kPrimInt:
3827 case Primitive::kPrimChar: {
3828 // Processing a Dex `int-to-float' instruction.
3829 __ Vmov(OutputSRegister(conversion), InputRegisterAt(conversion, 0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01003830 __ Vcvt(F32, S32, OutputSRegister(conversion), OutputSRegister(conversion));
Scott Wakelingfe885462016-09-22 10:24:38 +01003831 break;
3832 }
3833
3834 case Primitive::kPrimLong:
3835 // Processing a Dex `long-to-float' instruction.
3836 codegen_->InvokeRuntime(kQuickL2f, conversion, conversion->GetDexPc());
3837 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
3838 break;
3839
3840 case Primitive::kPrimDouble:
3841 // Processing a Dex `double-to-float' instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01003842 __ Vcvt(F32, F64, OutputSRegister(conversion), DRegisterFrom(in));
Scott Wakelingfe885462016-09-22 10:24:38 +01003843 break;
3844
3845 default:
3846 LOG(FATAL) << "Unexpected type conversion from " << input_type
3847 << " to " << result_type;
3848 };
3849 break;
3850
3851 case Primitive::kPrimDouble:
3852 switch (input_type) {
3853 case Primitive::kPrimBoolean:
3854 // Boolean input is a result of code transformations.
3855 case Primitive::kPrimByte:
3856 case Primitive::kPrimShort:
3857 case Primitive::kPrimInt:
3858 case Primitive::kPrimChar: {
3859 // Processing a Dex `int-to-double' instruction.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003860 __ Vmov(LowSRegisterFrom(out), InputRegisterAt(conversion, 0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01003861 __ Vcvt(F64, S32, DRegisterFrom(out), LowSRegisterFrom(out));
Scott Wakelingfe885462016-09-22 10:24:38 +01003862 break;
3863 }
3864
3865 case Primitive::kPrimLong: {
3866 // Processing a Dex `long-to-double' instruction.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003867 vixl32::Register low = LowRegisterFrom(in);
3868 vixl32::Register high = HighRegisterFrom(in);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003869 vixl32::SRegister out_s = LowSRegisterFrom(out);
Scott Wakelingc34dba72016-10-03 10:14:44 +01003870 vixl32::DRegister out_d = DRegisterFrom(out);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003871 vixl32::SRegister temp_s = LowSRegisterFrom(locations->GetTemp(0));
Scott Wakelingc34dba72016-10-03 10:14:44 +01003872 vixl32::DRegister temp_d = DRegisterFrom(locations->GetTemp(0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01003873 vixl32::DRegister constant_d = DRegisterFrom(locations->GetTemp(1));
Scott Wakelingfe885462016-09-22 10:24:38 +01003874
3875 // temp_d = int-to-double(high)
3876 __ Vmov(temp_s, high);
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01003877 __ Vcvt(F64, S32, temp_d, temp_s);
Scott Wakelingfe885462016-09-22 10:24:38 +01003878 // constant_d = k2Pow32EncodingForDouble
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003879 __ Vmov(constant_d, bit_cast<double, int64_t>(k2Pow32EncodingForDouble));
Scott Wakelingfe885462016-09-22 10:24:38 +01003880 // out_d = unsigned-to-double(low)
3881 __ Vmov(out_s, low);
3882 __ Vcvt(F64, U32, out_d, out_s);
3883 // out_d += temp_d * constant_d
3884 __ Vmla(F64, out_d, temp_d, constant_d);
3885 break;
3886 }
3887
3888 case Primitive::kPrimFloat:
3889 // Processing a Dex `float-to-double' instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01003890 __ Vcvt(F64, F32, DRegisterFrom(out), InputSRegisterAt(conversion, 0));
Scott Wakelingfe885462016-09-22 10:24:38 +01003891 break;
3892
3893 default:
3894 LOG(FATAL) << "Unexpected type conversion from " << input_type
3895 << " to " << result_type;
3896 };
3897 break;
3898
3899 default:
3900 LOG(FATAL) << "Unexpected type conversion from " << input_type
3901 << " to " << result_type;
3902 }
3903}
3904
3905void LocationsBuilderARMVIXL::VisitAdd(HAdd* add) {
3906 LocationSummary* locations =
3907 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
3908 switch (add->GetResultType()) {
3909 case Primitive::kPrimInt: {
3910 locations->SetInAt(0, Location::RequiresRegister());
3911 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
3912 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3913 break;
3914 }
3915
Scott Wakelingfe885462016-09-22 10:24:38 +01003916 case Primitive::kPrimLong: {
3917 locations->SetInAt(0, Location::RequiresRegister());
Anton Kirilovdda43962016-11-21 19:55:20 +00003918 locations->SetInAt(1, ArmEncodableConstantOrRegister(add->InputAt(1), ADD));
Scott Wakelingfe885462016-09-22 10:24:38 +01003919 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3920 break;
3921 }
3922
3923 case Primitive::kPrimFloat:
3924 case Primitive::kPrimDouble: {
3925 locations->SetInAt(0, Location::RequiresFpuRegister());
3926 locations->SetInAt(1, Location::RequiresFpuRegister());
3927 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3928 break;
3929 }
3930
3931 default:
3932 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
3933 }
3934}
3935
3936void InstructionCodeGeneratorARMVIXL::VisitAdd(HAdd* add) {
3937 LocationSummary* locations = add->GetLocations();
3938 Location out = locations->Out();
3939 Location first = locations->InAt(0);
3940 Location second = locations->InAt(1);
3941
3942 switch (add->GetResultType()) {
3943 case Primitive::kPrimInt: {
3944 __ Add(OutputRegister(add), InputRegisterAt(add, 0), InputOperandAt(add, 1));
3945 }
3946 break;
3947
Scott Wakelingfe885462016-09-22 10:24:38 +01003948 case Primitive::kPrimLong: {
Anton Kirilovdda43962016-11-21 19:55:20 +00003949 if (second.IsConstant()) {
3950 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
3951 GenerateAddLongConst(out, first, value);
3952 } else {
3953 DCHECK(second.IsRegisterPair());
3954 __ Adds(LowRegisterFrom(out), LowRegisterFrom(first), LowRegisterFrom(second));
3955 __ Adc(HighRegisterFrom(out), HighRegisterFrom(first), HighRegisterFrom(second));
3956 }
Scott Wakelingfe885462016-09-22 10:24:38 +01003957 break;
3958 }
3959
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003960 case Primitive::kPrimFloat:
Scott Wakelingfe885462016-09-22 10:24:38 +01003961 case Primitive::kPrimDouble:
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003962 __ Vadd(OutputVRegister(add), InputVRegisterAt(add, 0), InputVRegisterAt(add, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01003963 break;
3964
3965 default:
3966 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
3967 }
3968}
3969
3970void LocationsBuilderARMVIXL::VisitSub(HSub* sub) {
3971 LocationSummary* locations =
3972 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
3973 switch (sub->GetResultType()) {
3974 case Primitive::kPrimInt: {
3975 locations->SetInAt(0, Location::RequiresRegister());
3976 locations->SetInAt(1, Location::RegisterOrConstant(sub->InputAt(1)));
3977 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3978 break;
3979 }
3980
Scott Wakelingfe885462016-09-22 10:24:38 +01003981 case Primitive::kPrimLong: {
3982 locations->SetInAt(0, Location::RequiresRegister());
Anton Kirilovdda43962016-11-21 19:55:20 +00003983 locations->SetInAt(1, ArmEncodableConstantOrRegister(sub->InputAt(1), SUB));
Scott Wakelingfe885462016-09-22 10:24:38 +01003984 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3985 break;
3986 }
3987 case Primitive::kPrimFloat:
3988 case Primitive::kPrimDouble: {
3989 locations->SetInAt(0, Location::RequiresFpuRegister());
3990 locations->SetInAt(1, Location::RequiresFpuRegister());
3991 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3992 break;
3993 }
3994 default:
3995 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
3996 }
3997}
3998
3999void InstructionCodeGeneratorARMVIXL::VisitSub(HSub* sub) {
4000 LocationSummary* locations = sub->GetLocations();
4001 Location out = locations->Out();
4002 Location first = locations->InAt(0);
4003 Location second = locations->InAt(1);
4004 switch (sub->GetResultType()) {
4005 case Primitive::kPrimInt: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004006 __ Sub(OutputRegister(sub), InputRegisterAt(sub, 0), InputOperandAt(sub, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004007 break;
4008 }
4009
Scott Wakelingfe885462016-09-22 10:24:38 +01004010 case Primitive::kPrimLong: {
Anton Kirilovdda43962016-11-21 19:55:20 +00004011 if (second.IsConstant()) {
4012 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
4013 GenerateAddLongConst(out, first, -value);
4014 } else {
4015 DCHECK(second.IsRegisterPair());
4016 __ Subs(LowRegisterFrom(out), LowRegisterFrom(first), LowRegisterFrom(second));
4017 __ Sbc(HighRegisterFrom(out), HighRegisterFrom(first), HighRegisterFrom(second));
4018 }
Scott Wakelingfe885462016-09-22 10:24:38 +01004019 break;
4020 }
4021
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004022 case Primitive::kPrimFloat:
4023 case Primitive::kPrimDouble:
4024 __ Vsub(OutputVRegister(sub), InputVRegisterAt(sub, 0), InputVRegisterAt(sub, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004025 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01004026
4027 default:
4028 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
4029 }
4030}
4031
4032void LocationsBuilderARMVIXL::VisitMul(HMul* mul) {
4033 LocationSummary* locations =
4034 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
4035 switch (mul->GetResultType()) {
4036 case Primitive::kPrimInt:
4037 case Primitive::kPrimLong: {
4038 locations->SetInAt(0, Location::RequiresRegister());
4039 locations->SetInAt(1, Location::RequiresRegister());
4040 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4041 break;
4042 }
4043
4044 case Primitive::kPrimFloat:
4045 case Primitive::kPrimDouble: {
4046 locations->SetInAt(0, Location::RequiresFpuRegister());
4047 locations->SetInAt(1, Location::RequiresFpuRegister());
4048 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4049 break;
4050 }
4051
4052 default:
4053 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4054 }
4055}
4056
4057void InstructionCodeGeneratorARMVIXL::VisitMul(HMul* mul) {
4058 LocationSummary* locations = mul->GetLocations();
4059 Location out = locations->Out();
4060 Location first = locations->InAt(0);
4061 Location second = locations->InAt(1);
4062 switch (mul->GetResultType()) {
4063 case Primitive::kPrimInt: {
4064 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
4065 break;
4066 }
4067 case Primitive::kPrimLong: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004068 vixl32::Register out_hi = HighRegisterFrom(out);
4069 vixl32::Register out_lo = LowRegisterFrom(out);
4070 vixl32::Register in1_hi = HighRegisterFrom(first);
4071 vixl32::Register in1_lo = LowRegisterFrom(first);
4072 vixl32::Register in2_hi = HighRegisterFrom(second);
4073 vixl32::Register in2_lo = LowRegisterFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01004074
4075 // Extra checks to protect caused by the existence of R1_R2.
4076 // The algorithm is wrong if out.hi is either in1.lo or in2.lo:
4077 // (e.g. in1=r0_r1, in2=r2_r3 and out=r1_r2);
Anton Kirilov644032c2016-12-06 17:51:43 +00004078 DCHECK(!out_hi.Is(in1_lo));
4079 DCHECK(!out_hi.Is(in2_lo));
Scott Wakelingfe885462016-09-22 10:24:38 +01004080
4081 // input: in1 - 64 bits, in2 - 64 bits
4082 // output: out
4083 // formula: out.hi : out.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
4084 // parts: out.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
4085 // parts: out.lo = (in1.lo * in2.lo)[31:0]
4086
4087 UseScratchRegisterScope temps(GetVIXLAssembler());
4088 vixl32::Register temp = temps.Acquire();
4089 // temp <- in1.lo * in2.hi
4090 __ Mul(temp, in1_lo, in2_hi);
4091 // out.hi <- in1.lo * in2.hi + in1.hi * in2.lo
4092 __ Mla(out_hi, in1_hi, in2_lo, temp);
4093 // out.lo <- (in1.lo * in2.lo)[31:0];
4094 __ Umull(out_lo, temp, in1_lo, in2_lo);
4095 // out.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004096 __ Add(out_hi, out_hi, temp);
Scott Wakelingfe885462016-09-22 10:24:38 +01004097 break;
4098 }
4099
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004100 case Primitive::kPrimFloat:
4101 case Primitive::kPrimDouble:
4102 __ Vmul(OutputVRegister(mul), InputVRegisterAt(mul, 0), InputVRegisterAt(mul, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004103 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01004104
4105 default:
4106 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4107 }
4108}
4109
Scott Wakelingfe885462016-09-22 10:24:38 +01004110void InstructionCodeGeneratorARMVIXL::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
4111 DCHECK(instruction->IsDiv() || instruction->IsRem());
4112 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
4113
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004114 Location second = instruction->GetLocations()->InAt(1);
Scott Wakelingfe885462016-09-22 10:24:38 +01004115 DCHECK(second.IsConstant());
4116
4117 vixl32::Register out = OutputRegister(instruction);
4118 vixl32::Register dividend = InputRegisterAt(instruction, 0);
Anton Kirilov644032c2016-12-06 17:51:43 +00004119 int32_t imm = Int32ConstantFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01004120 DCHECK(imm == 1 || imm == -1);
4121
4122 if (instruction->IsRem()) {
4123 __ Mov(out, 0);
4124 } else {
4125 if (imm == 1) {
4126 __ Mov(out, dividend);
4127 } else {
4128 __ Rsb(out, dividend, 0);
4129 }
4130 }
4131}
4132
4133void InstructionCodeGeneratorARMVIXL::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
4134 DCHECK(instruction->IsDiv() || instruction->IsRem());
4135 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
4136
4137 LocationSummary* locations = instruction->GetLocations();
4138 Location second = locations->InAt(1);
4139 DCHECK(second.IsConstant());
4140
4141 vixl32::Register out = OutputRegister(instruction);
4142 vixl32::Register dividend = InputRegisterAt(instruction, 0);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004143 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
Anton Kirilov644032c2016-12-06 17:51:43 +00004144 int32_t imm = Int32ConstantFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01004145 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
4146 int ctz_imm = CTZ(abs_imm);
4147
4148 if (ctz_imm == 1) {
4149 __ Lsr(temp, dividend, 32 - ctz_imm);
4150 } else {
4151 __ Asr(temp, dividend, 31);
4152 __ Lsr(temp, temp, 32 - ctz_imm);
4153 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004154 __ Add(out, temp, dividend);
Scott Wakelingfe885462016-09-22 10:24:38 +01004155
4156 if (instruction->IsDiv()) {
4157 __ Asr(out, out, ctz_imm);
4158 if (imm < 0) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004159 __ Rsb(out, out, 0);
Scott Wakelingfe885462016-09-22 10:24:38 +01004160 }
4161 } else {
4162 __ Ubfx(out, out, 0, ctz_imm);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004163 __ Sub(out, out, temp);
Scott Wakelingfe885462016-09-22 10:24:38 +01004164 }
4165}
4166
4167void InstructionCodeGeneratorARMVIXL::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
4168 DCHECK(instruction->IsDiv() || instruction->IsRem());
4169 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
4170
4171 LocationSummary* locations = instruction->GetLocations();
4172 Location second = locations->InAt(1);
4173 DCHECK(second.IsConstant());
4174
4175 vixl32::Register out = OutputRegister(instruction);
4176 vixl32::Register dividend = InputRegisterAt(instruction, 0);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004177 vixl32::Register temp1 = RegisterFrom(locations->GetTemp(0));
4178 vixl32::Register temp2 = RegisterFrom(locations->GetTemp(1));
Scott Wakelingb77051e2016-11-21 19:46:00 +00004179 int32_t imm = Int32ConstantFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01004180
4181 int64_t magic;
4182 int shift;
4183 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
4184
Anton Kirilovdda43962016-11-21 19:55:20 +00004185 // TODO(VIXL): Change the static cast to Operand::From() after VIXL is fixed.
4186 __ Mov(temp1, static_cast<int32_t>(magic));
Scott Wakelingfe885462016-09-22 10:24:38 +01004187 __ Smull(temp2, temp1, dividend, temp1);
4188
4189 if (imm > 0 && magic < 0) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004190 __ Add(temp1, temp1, dividend);
Scott Wakelingfe885462016-09-22 10:24:38 +01004191 } else if (imm < 0 && magic > 0) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004192 __ Sub(temp1, temp1, dividend);
Scott Wakelingfe885462016-09-22 10:24:38 +01004193 }
4194
4195 if (shift != 0) {
4196 __ Asr(temp1, temp1, shift);
4197 }
4198
4199 if (instruction->IsDiv()) {
4200 __ Sub(out, temp1, Operand(temp1, vixl32::Shift(ASR), 31));
4201 } else {
4202 __ Sub(temp1, temp1, Operand(temp1, vixl32::Shift(ASR), 31));
4203 // TODO: Strength reduction for mls.
4204 __ Mov(temp2, imm);
4205 __ Mls(out, temp1, temp2, dividend);
4206 }
4207}
4208
4209void InstructionCodeGeneratorARMVIXL::GenerateDivRemConstantIntegral(
4210 HBinaryOperation* instruction) {
4211 DCHECK(instruction->IsDiv() || instruction->IsRem());
4212 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
4213
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004214 Location second = instruction->GetLocations()->InAt(1);
Scott Wakelingfe885462016-09-22 10:24:38 +01004215 DCHECK(second.IsConstant());
4216
Anton Kirilov644032c2016-12-06 17:51:43 +00004217 int32_t imm = Int32ConstantFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01004218 if (imm == 0) {
4219 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
4220 } else if (imm == 1 || imm == -1) {
4221 DivRemOneOrMinusOne(instruction);
4222 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
4223 DivRemByPowerOfTwo(instruction);
4224 } else {
4225 DCHECK(imm <= -2 || imm >= 2);
4226 GenerateDivRemWithAnyConstant(instruction);
4227 }
4228}
4229
4230void LocationsBuilderARMVIXL::VisitDiv(HDiv* div) {
4231 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
4232 if (div->GetResultType() == Primitive::kPrimLong) {
4233 // pLdiv runtime call.
4234 call_kind = LocationSummary::kCallOnMainOnly;
4235 } else if (div->GetResultType() == Primitive::kPrimInt && div->InputAt(1)->IsConstant()) {
4236 // sdiv will be replaced by other instruction sequence.
4237 } else if (div->GetResultType() == Primitive::kPrimInt &&
4238 !codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4239 // pIdivmod runtime call.
4240 call_kind = LocationSummary::kCallOnMainOnly;
4241 }
4242
4243 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
4244
4245 switch (div->GetResultType()) {
4246 case Primitive::kPrimInt: {
4247 if (div->InputAt(1)->IsConstant()) {
4248 locations->SetInAt(0, Location::RequiresRegister());
4249 locations->SetInAt(1, Location::ConstantLocation(div->InputAt(1)->AsConstant()));
4250 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Anton Kirilov644032c2016-12-06 17:51:43 +00004251 int32_t value = Int32ConstantFrom(div->InputAt(1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004252 if (value == 1 || value == 0 || value == -1) {
4253 // No temp register required.
4254 } else {
4255 locations->AddTemp(Location::RequiresRegister());
4256 if (!IsPowerOfTwo(AbsOrMin(value))) {
4257 locations->AddTemp(Location::RequiresRegister());
4258 }
4259 }
4260 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4261 locations->SetInAt(0, Location::RequiresRegister());
4262 locations->SetInAt(1, Location::RequiresRegister());
4263 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4264 } else {
Artem Serov551b28f2016-10-18 19:11:30 +01004265 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4266 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4267 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
Roland Levillain5e8d5f02016-10-18 18:03:43 +01004268 // Note: divmod will compute both the quotient and the remainder as the pair R0 and R1, but
Artem Serov551b28f2016-10-18 19:11:30 +01004269 // we only need the former.
4270 locations->SetOut(LocationFrom(r0));
Scott Wakelingfe885462016-09-22 10:24:38 +01004271 }
4272 break;
4273 }
4274 case Primitive::kPrimLong: {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01004275 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4276 locations->SetInAt(0, LocationFrom(
4277 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
4278 locations->SetInAt(1, LocationFrom(
4279 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
4280 locations->SetOut(LocationFrom(r0, r1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004281 break;
4282 }
4283 case Primitive::kPrimFloat:
4284 case Primitive::kPrimDouble: {
4285 locations->SetInAt(0, Location::RequiresFpuRegister());
4286 locations->SetInAt(1, Location::RequiresFpuRegister());
4287 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4288 break;
4289 }
4290
4291 default:
4292 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
4293 }
4294}
4295
4296void InstructionCodeGeneratorARMVIXL::VisitDiv(HDiv* div) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01004297 Location lhs = div->GetLocations()->InAt(0);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004298 Location rhs = div->GetLocations()->InAt(1);
Scott Wakelingfe885462016-09-22 10:24:38 +01004299
4300 switch (div->GetResultType()) {
4301 case Primitive::kPrimInt: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004302 if (rhs.IsConstant()) {
Scott Wakelingfe885462016-09-22 10:24:38 +01004303 GenerateDivRemConstantIntegral(div);
4304 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4305 __ Sdiv(OutputRegister(div), InputRegisterAt(div, 0), InputRegisterAt(div, 1));
4306 } else {
Artem Serov551b28f2016-10-18 19:11:30 +01004307 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4308 DCHECK(calling_convention.GetRegisterAt(0).Is(RegisterFrom(lhs)));
4309 DCHECK(calling_convention.GetRegisterAt(1).Is(RegisterFrom(rhs)));
4310 DCHECK(r0.Is(OutputRegister(div)));
4311
4312 codegen_->InvokeRuntime(kQuickIdivmod, div, div->GetDexPc());
4313 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
Scott Wakelingfe885462016-09-22 10:24:38 +01004314 }
4315 break;
4316 }
4317
4318 case Primitive::kPrimLong: {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01004319 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4320 DCHECK(calling_convention.GetRegisterAt(0).Is(LowRegisterFrom(lhs)));
4321 DCHECK(calling_convention.GetRegisterAt(1).Is(HighRegisterFrom(lhs)));
4322 DCHECK(calling_convention.GetRegisterAt(2).Is(LowRegisterFrom(rhs)));
4323 DCHECK(calling_convention.GetRegisterAt(3).Is(HighRegisterFrom(rhs)));
4324 DCHECK(LowRegisterFrom(div->GetLocations()->Out()).Is(r0));
4325 DCHECK(HighRegisterFrom(div->GetLocations()->Out()).Is(r1));
4326
4327 codegen_->InvokeRuntime(kQuickLdiv, div, div->GetDexPc());
4328 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Scott Wakelingfe885462016-09-22 10:24:38 +01004329 break;
4330 }
4331
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004332 case Primitive::kPrimFloat:
4333 case Primitive::kPrimDouble:
4334 __ Vdiv(OutputVRegister(div), InputVRegisterAt(div, 0), InputVRegisterAt(div, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004335 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01004336
4337 default:
4338 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
4339 }
4340}
4341
Artem Serov551b28f2016-10-18 19:11:30 +01004342void LocationsBuilderARMVIXL::VisitRem(HRem* rem) {
4343 Primitive::Type type = rem->GetResultType();
4344
4345 // Most remainders are implemented in the runtime.
4346 LocationSummary::CallKind call_kind = LocationSummary::kCallOnMainOnly;
4347 if (rem->GetResultType() == Primitive::kPrimInt && rem->InputAt(1)->IsConstant()) {
4348 // sdiv will be replaced by other instruction sequence.
4349 call_kind = LocationSummary::kNoCall;
4350 } else if ((rem->GetResultType() == Primitive::kPrimInt)
4351 && codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4352 // Have hardware divide instruction for int, do it with three instructions.
4353 call_kind = LocationSummary::kNoCall;
4354 }
4355
4356 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
4357
4358 switch (type) {
4359 case Primitive::kPrimInt: {
4360 if (rem->InputAt(1)->IsConstant()) {
4361 locations->SetInAt(0, Location::RequiresRegister());
4362 locations->SetInAt(1, Location::ConstantLocation(rem->InputAt(1)->AsConstant()));
4363 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Anton Kirilov644032c2016-12-06 17:51:43 +00004364 int32_t value = Int32ConstantFrom(rem->InputAt(1));
Artem Serov551b28f2016-10-18 19:11:30 +01004365 if (value == 1 || value == 0 || value == -1) {
4366 // No temp register required.
4367 } else {
4368 locations->AddTemp(Location::RequiresRegister());
4369 if (!IsPowerOfTwo(AbsOrMin(value))) {
4370 locations->AddTemp(Location::RequiresRegister());
4371 }
4372 }
4373 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4374 locations->SetInAt(0, Location::RequiresRegister());
4375 locations->SetInAt(1, Location::RequiresRegister());
4376 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4377 locations->AddTemp(Location::RequiresRegister());
4378 } else {
4379 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4380 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4381 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
Roland Levillain5e8d5f02016-10-18 18:03:43 +01004382 // Note: divmod will compute both the quotient and the remainder as the pair R0 and R1, but
Artem Serov551b28f2016-10-18 19:11:30 +01004383 // we only need the latter.
4384 locations->SetOut(LocationFrom(r1));
4385 }
4386 break;
4387 }
4388 case Primitive::kPrimLong: {
4389 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4390 locations->SetInAt(0, LocationFrom(
4391 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
4392 locations->SetInAt(1, LocationFrom(
4393 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
4394 // The runtime helper puts the output in R2,R3.
4395 locations->SetOut(LocationFrom(r2, r3));
4396 break;
4397 }
4398 case Primitive::kPrimFloat: {
4399 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4400 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
4401 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
4402 locations->SetOut(LocationFrom(s0));
4403 break;
4404 }
4405
4406 case Primitive::kPrimDouble: {
4407 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4408 locations->SetInAt(0, LocationFrom(
4409 calling_convention.GetFpuRegisterAt(0), calling_convention.GetFpuRegisterAt(1)));
4410 locations->SetInAt(1, LocationFrom(
4411 calling_convention.GetFpuRegisterAt(2), calling_convention.GetFpuRegisterAt(3)));
4412 locations->SetOut(LocationFrom(s0, s1));
4413 break;
4414 }
4415
4416 default:
4417 LOG(FATAL) << "Unexpected rem type " << type;
4418 }
4419}
4420
4421void InstructionCodeGeneratorARMVIXL::VisitRem(HRem* rem) {
4422 LocationSummary* locations = rem->GetLocations();
4423 Location second = locations->InAt(1);
4424
4425 Primitive::Type type = rem->GetResultType();
4426 switch (type) {
4427 case Primitive::kPrimInt: {
4428 vixl32::Register reg1 = InputRegisterAt(rem, 0);
4429 vixl32::Register out_reg = OutputRegister(rem);
4430 if (second.IsConstant()) {
4431 GenerateDivRemConstantIntegral(rem);
4432 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4433 vixl32::Register reg2 = RegisterFrom(second);
4434 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
4435
4436 // temp = reg1 / reg2 (integer division)
4437 // dest = reg1 - temp * reg2
4438 __ Sdiv(temp, reg1, reg2);
4439 __ Mls(out_reg, temp, reg2, reg1);
4440 } else {
4441 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4442 DCHECK(reg1.Is(calling_convention.GetRegisterAt(0)));
4443 DCHECK(RegisterFrom(second).Is(calling_convention.GetRegisterAt(1)));
4444 DCHECK(out_reg.Is(r1));
4445
4446 codegen_->InvokeRuntime(kQuickIdivmod, rem, rem->GetDexPc());
4447 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
4448 }
4449 break;
4450 }
4451
4452 case Primitive::kPrimLong: {
4453 codegen_->InvokeRuntime(kQuickLmod, rem, rem->GetDexPc());
4454 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
4455 break;
4456 }
4457
4458 case Primitive::kPrimFloat: {
4459 codegen_->InvokeRuntime(kQuickFmodf, rem, rem->GetDexPc());
4460 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
4461 break;
4462 }
4463
4464 case Primitive::kPrimDouble: {
4465 codegen_->InvokeRuntime(kQuickFmod, rem, rem->GetDexPc());
4466 CheckEntrypointTypes<kQuickFmod, double, double, double>();
4467 break;
4468 }
4469
4470 default:
4471 LOG(FATAL) << "Unexpected rem type " << type;
4472 }
4473}
4474
4475
Scott Wakelingfe885462016-09-22 10:24:38 +01004476void LocationsBuilderARMVIXL::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Artem Serov657022c2016-11-23 14:19:38 +00004477 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Scott Wakelingfe885462016-09-22 10:24:38 +01004478 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Scott Wakelingfe885462016-09-22 10:24:38 +01004479}
4480
4481void InstructionCodeGeneratorARMVIXL::VisitDivZeroCheck(HDivZeroCheck* instruction) {
4482 DivZeroCheckSlowPathARMVIXL* slow_path =
4483 new (GetGraph()->GetArena()) DivZeroCheckSlowPathARMVIXL(instruction);
4484 codegen_->AddSlowPath(slow_path);
4485
4486 LocationSummary* locations = instruction->GetLocations();
4487 Location value = locations->InAt(0);
4488
4489 switch (instruction->GetType()) {
4490 case Primitive::kPrimBoolean:
4491 case Primitive::kPrimByte:
4492 case Primitive::kPrimChar:
4493 case Primitive::kPrimShort:
4494 case Primitive::kPrimInt: {
4495 if (value.IsRegister()) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00004496 __ CompareAndBranchIfZero(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
Scott Wakelingfe885462016-09-22 10:24:38 +01004497 } else {
4498 DCHECK(value.IsConstant()) << value;
Anton Kirilov644032c2016-12-06 17:51:43 +00004499 if (Int32ConstantFrom(value) == 0) {
Scott Wakelingfe885462016-09-22 10:24:38 +01004500 __ B(slow_path->GetEntryLabel());
4501 }
4502 }
4503 break;
4504 }
4505 case Primitive::kPrimLong: {
4506 if (value.IsRegisterPair()) {
4507 UseScratchRegisterScope temps(GetVIXLAssembler());
4508 vixl32::Register temp = temps.Acquire();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004509 __ Orrs(temp, LowRegisterFrom(value), HighRegisterFrom(value));
Scott Wakelingfe885462016-09-22 10:24:38 +01004510 __ B(eq, slow_path->GetEntryLabel());
4511 } else {
4512 DCHECK(value.IsConstant()) << value;
Anton Kirilov644032c2016-12-06 17:51:43 +00004513 if (Int64ConstantFrom(value) == 0) {
Scott Wakelingfe885462016-09-22 10:24:38 +01004514 __ B(slow_path->GetEntryLabel());
4515 }
4516 }
4517 break;
4518 }
4519 default:
4520 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
4521 }
4522}
4523
Artem Serov02109dd2016-09-23 17:17:54 +01004524void InstructionCodeGeneratorARMVIXL::HandleIntegerRotate(HRor* ror) {
4525 LocationSummary* locations = ror->GetLocations();
4526 vixl32::Register in = InputRegisterAt(ror, 0);
4527 Location rhs = locations->InAt(1);
4528 vixl32::Register out = OutputRegister(ror);
4529
4530 if (rhs.IsConstant()) {
4531 // Arm32 and Thumb2 assemblers require a rotation on the interval [1,31],
4532 // so map all rotations to a +ve. equivalent in that range.
4533 // (e.g. left *or* right by -2 bits == 30 bits in the same direction.)
4534 uint32_t rot = CodeGenerator::GetInt32ValueOf(rhs.GetConstant()) & 0x1F;
4535 if (rot) {
4536 // Rotate, mapping left rotations to right equivalents if necessary.
4537 // (e.g. left by 2 bits == right by 30.)
4538 __ Ror(out, in, rot);
4539 } else if (!out.Is(in)) {
4540 __ Mov(out, in);
4541 }
4542 } else {
4543 __ Ror(out, in, RegisterFrom(rhs));
4544 }
4545}
4546
4547// Gain some speed by mapping all Long rotates onto equivalent pairs of Integer
4548// rotates by swapping input regs (effectively rotating by the first 32-bits of
4549// a larger rotation) or flipping direction (thus treating larger right/left
4550// rotations as sub-word sized rotations in the other direction) as appropriate.
4551void InstructionCodeGeneratorARMVIXL::HandleLongRotate(HRor* ror) {
4552 LocationSummary* locations = ror->GetLocations();
4553 vixl32::Register in_reg_lo = LowRegisterFrom(locations->InAt(0));
4554 vixl32::Register in_reg_hi = HighRegisterFrom(locations->InAt(0));
4555 Location rhs = locations->InAt(1);
4556 vixl32::Register out_reg_lo = LowRegisterFrom(locations->Out());
4557 vixl32::Register out_reg_hi = HighRegisterFrom(locations->Out());
4558
4559 if (rhs.IsConstant()) {
4560 uint64_t rot = CodeGenerator::GetInt64ValueOf(rhs.GetConstant());
4561 // Map all rotations to +ve. equivalents on the interval [0,63].
4562 rot &= kMaxLongShiftDistance;
4563 // For rotates over a word in size, 'pre-rotate' by 32-bits to keep rotate
4564 // logic below to a simple pair of binary orr.
4565 // (e.g. 34 bits == in_reg swap + 2 bits right.)
4566 if (rot >= kArmBitsPerWord) {
4567 rot -= kArmBitsPerWord;
4568 std::swap(in_reg_hi, in_reg_lo);
4569 }
4570 // Rotate, or mov to out for zero or word size rotations.
4571 if (rot != 0u) {
Scott Wakelingb77051e2016-11-21 19:46:00 +00004572 __ Lsr(out_reg_hi, in_reg_hi, Operand::From(rot));
Artem Serov02109dd2016-09-23 17:17:54 +01004573 __ Orr(out_reg_hi, out_reg_hi, Operand(in_reg_lo, ShiftType::LSL, kArmBitsPerWord - rot));
Scott Wakelingb77051e2016-11-21 19:46:00 +00004574 __ Lsr(out_reg_lo, in_reg_lo, Operand::From(rot));
Artem Serov02109dd2016-09-23 17:17:54 +01004575 __ Orr(out_reg_lo, out_reg_lo, Operand(in_reg_hi, ShiftType::LSL, kArmBitsPerWord - rot));
4576 } else {
4577 __ Mov(out_reg_lo, in_reg_lo);
4578 __ Mov(out_reg_hi, in_reg_hi);
4579 }
4580 } else {
4581 vixl32::Register shift_right = RegisterFrom(locations->GetTemp(0));
4582 vixl32::Register shift_left = RegisterFrom(locations->GetTemp(1));
4583 vixl32::Label end;
4584 vixl32::Label shift_by_32_plus_shift_right;
Anton Kirilov6f644202017-02-27 18:29:45 +00004585 vixl32::Label* final_label = codegen_->GetFinalLabel(ror, &end);
Artem Serov02109dd2016-09-23 17:17:54 +01004586
4587 __ And(shift_right, RegisterFrom(rhs), 0x1F);
4588 __ Lsrs(shift_left, RegisterFrom(rhs), 6);
Scott Wakelingbffdc702016-12-07 17:46:03 +00004589 __ Rsb(LeaveFlags, shift_left, shift_right, Operand::From(kArmBitsPerWord));
Artem Serov517d9f62016-12-12 15:51:15 +00004590 __ B(cc, &shift_by_32_plus_shift_right, /* far_target */ false);
Artem Serov02109dd2016-09-23 17:17:54 +01004591
4592 // out_reg_hi = (reg_hi << shift_left) | (reg_lo >> shift_right).
4593 // out_reg_lo = (reg_lo << shift_left) | (reg_hi >> shift_right).
4594 __ Lsl(out_reg_hi, in_reg_hi, shift_left);
4595 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
4596 __ Add(out_reg_hi, out_reg_hi, out_reg_lo);
4597 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
4598 __ Lsr(shift_left, in_reg_hi, shift_right);
4599 __ Add(out_reg_lo, out_reg_lo, shift_left);
Anton Kirilov6f644202017-02-27 18:29:45 +00004600 __ B(final_label);
Artem Serov02109dd2016-09-23 17:17:54 +01004601
4602 __ Bind(&shift_by_32_plus_shift_right); // Shift by 32+shift_right.
4603 // out_reg_hi = (reg_hi >> shift_right) | (reg_lo << shift_left).
4604 // out_reg_lo = (reg_lo >> shift_right) | (reg_hi << shift_left).
4605 __ Lsr(out_reg_hi, in_reg_hi, shift_right);
4606 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
4607 __ Add(out_reg_hi, out_reg_hi, out_reg_lo);
4608 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
4609 __ Lsl(shift_right, in_reg_hi, shift_left);
4610 __ Add(out_reg_lo, out_reg_lo, shift_right);
4611
Anton Kirilov6f644202017-02-27 18:29:45 +00004612 if (end.IsReferenced()) {
4613 __ Bind(&end);
4614 }
Artem Serov02109dd2016-09-23 17:17:54 +01004615 }
4616}
4617
4618void LocationsBuilderARMVIXL::VisitRor(HRor* ror) {
4619 LocationSummary* locations =
4620 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
4621 switch (ror->GetResultType()) {
4622 case Primitive::kPrimInt: {
4623 locations->SetInAt(0, Location::RequiresRegister());
4624 locations->SetInAt(1, Location::RegisterOrConstant(ror->InputAt(1)));
4625 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4626 break;
4627 }
4628 case Primitive::kPrimLong: {
4629 locations->SetInAt(0, Location::RequiresRegister());
4630 if (ror->InputAt(1)->IsConstant()) {
4631 locations->SetInAt(1, Location::ConstantLocation(ror->InputAt(1)->AsConstant()));
4632 } else {
4633 locations->SetInAt(1, Location::RequiresRegister());
4634 locations->AddTemp(Location::RequiresRegister());
4635 locations->AddTemp(Location::RequiresRegister());
4636 }
4637 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
4638 break;
4639 }
4640 default:
4641 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
4642 }
4643}
4644
4645void InstructionCodeGeneratorARMVIXL::VisitRor(HRor* ror) {
4646 Primitive::Type type = ror->GetResultType();
4647 switch (type) {
4648 case Primitive::kPrimInt: {
4649 HandleIntegerRotate(ror);
4650 break;
4651 }
4652 case Primitive::kPrimLong: {
4653 HandleLongRotate(ror);
4654 break;
4655 }
4656 default:
4657 LOG(FATAL) << "Unexpected operation type " << type;
4658 UNREACHABLE();
4659 }
4660}
4661
Artem Serov02d37832016-10-25 15:25:33 +01004662void LocationsBuilderARMVIXL::HandleShift(HBinaryOperation* op) {
4663 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
4664
4665 LocationSummary* locations =
4666 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
4667
4668 switch (op->GetResultType()) {
4669 case Primitive::kPrimInt: {
4670 locations->SetInAt(0, Location::RequiresRegister());
4671 if (op->InputAt(1)->IsConstant()) {
4672 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
4673 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4674 } else {
4675 locations->SetInAt(1, Location::RequiresRegister());
4676 // Make the output overlap, as it will be used to hold the masked
4677 // second input.
4678 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
4679 }
4680 break;
4681 }
4682 case Primitive::kPrimLong: {
4683 locations->SetInAt(0, Location::RequiresRegister());
4684 if (op->InputAt(1)->IsConstant()) {
4685 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
4686 // For simplicity, use kOutputOverlap even though we only require that low registers
4687 // don't clash with high registers which the register allocator currently guarantees.
4688 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
4689 } else {
4690 locations->SetInAt(1, Location::RequiresRegister());
4691 locations->AddTemp(Location::RequiresRegister());
4692 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
4693 }
4694 break;
4695 }
4696 default:
4697 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
4698 }
4699}
4700
4701void InstructionCodeGeneratorARMVIXL::HandleShift(HBinaryOperation* op) {
4702 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
4703
4704 LocationSummary* locations = op->GetLocations();
4705 Location out = locations->Out();
4706 Location first = locations->InAt(0);
4707 Location second = locations->InAt(1);
4708
4709 Primitive::Type type = op->GetResultType();
4710 switch (type) {
4711 case Primitive::kPrimInt: {
4712 vixl32::Register out_reg = OutputRegister(op);
4713 vixl32::Register first_reg = InputRegisterAt(op, 0);
4714 if (second.IsRegister()) {
4715 vixl32::Register second_reg = RegisterFrom(second);
4716 // ARM doesn't mask the shift count so we need to do it ourselves.
4717 __ And(out_reg, second_reg, kMaxIntShiftDistance);
4718 if (op->IsShl()) {
4719 __ Lsl(out_reg, first_reg, out_reg);
4720 } else if (op->IsShr()) {
4721 __ Asr(out_reg, first_reg, out_reg);
4722 } else {
4723 __ Lsr(out_reg, first_reg, out_reg);
4724 }
4725 } else {
Anton Kirilov644032c2016-12-06 17:51:43 +00004726 int32_t cst = Int32ConstantFrom(second);
Artem Serov02d37832016-10-25 15:25:33 +01004727 uint32_t shift_value = cst & kMaxIntShiftDistance;
4728 if (shift_value == 0) { // ARM does not support shifting with 0 immediate.
4729 __ Mov(out_reg, first_reg);
4730 } else if (op->IsShl()) {
4731 __ Lsl(out_reg, first_reg, shift_value);
4732 } else if (op->IsShr()) {
4733 __ Asr(out_reg, first_reg, shift_value);
4734 } else {
4735 __ Lsr(out_reg, first_reg, shift_value);
4736 }
4737 }
4738 break;
4739 }
4740 case Primitive::kPrimLong: {
4741 vixl32::Register o_h = HighRegisterFrom(out);
4742 vixl32::Register o_l = LowRegisterFrom(out);
4743
4744 vixl32::Register high = HighRegisterFrom(first);
4745 vixl32::Register low = LowRegisterFrom(first);
4746
4747 if (second.IsRegister()) {
4748 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
4749
4750 vixl32::Register second_reg = RegisterFrom(second);
4751
4752 if (op->IsShl()) {
4753 __ And(o_l, second_reg, kMaxLongShiftDistance);
4754 // Shift the high part
4755 __ Lsl(o_h, high, o_l);
4756 // Shift the low part and `or` what overflew on the high part
Scott Wakelingb77051e2016-11-21 19:46:00 +00004757 __ Rsb(temp, o_l, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01004758 __ Lsr(temp, low, temp);
4759 __ Orr(o_h, o_h, temp);
4760 // If the shift is > 32 bits, override the high part
Scott Wakelingb77051e2016-11-21 19:46:00 +00004761 __ Subs(temp, o_l, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01004762 {
Artem Serov0fb37192016-12-06 18:13:40 +00004763 ExactAssemblyScope guard(GetVIXLAssembler(),
4764 2 * vixl32::kMaxInstructionSizeInBytes,
4765 CodeBufferCheckScope::kMaximumSize);
Artem Serov02d37832016-10-25 15:25:33 +01004766 __ it(pl);
4767 __ lsl(pl, o_h, low, temp);
4768 }
4769 // Shift the low part
4770 __ Lsl(o_l, low, o_l);
4771 } else if (op->IsShr()) {
4772 __ And(o_h, second_reg, kMaxLongShiftDistance);
4773 // Shift the low part
4774 __ Lsr(o_l, low, o_h);
4775 // Shift the high part and `or` what underflew on the low part
Scott Wakelingb77051e2016-11-21 19:46:00 +00004776 __ Rsb(temp, o_h, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01004777 __ Lsl(temp, high, temp);
4778 __ Orr(o_l, o_l, temp);
4779 // If the shift is > 32 bits, override the low part
Scott Wakelingb77051e2016-11-21 19:46:00 +00004780 __ Subs(temp, o_h, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01004781 {
Artem Serov0fb37192016-12-06 18:13:40 +00004782 ExactAssemblyScope guard(GetVIXLAssembler(),
4783 2 * vixl32::kMaxInstructionSizeInBytes,
4784 CodeBufferCheckScope::kMaximumSize);
Artem Serov02d37832016-10-25 15:25:33 +01004785 __ it(pl);
4786 __ asr(pl, o_l, high, temp);
4787 }
4788 // Shift the high part
4789 __ Asr(o_h, high, o_h);
4790 } else {
4791 __ And(o_h, second_reg, kMaxLongShiftDistance);
4792 // same as Shr except we use `Lsr`s and not `Asr`s
4793 __ Lsr(o_l, low, o_h);
Scott Wakelingb77051e2016-11-21 19:46:00 +00004794 __ Rsb(temp, o_h, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01004795 __ Lsl(temp, high, temp);
4796 __ Orr(o_l, o_l, temp);
Scott Wakelingb77051e2016-11-21 19:46:00 +00004797 __ Subs(temp, o_h, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01004798 {
Artem Serov0fb37192016-12-06 18:13:40 +00004799 ExactAssemblyScope guard(GetVIXLAssembler(),
4800 2 * vixl32::kMaxInstructionSizeInBytes,
4801 CodeBufferCheckScope::kMaximumSize);
Artem Serov02d37832016-10-25 15:25:33 +01004802 __ it(pl);
4803 __ lsr(pl, o_l, high, temp);
4804 }
4805 __ Lsr(o_h, high, o_h);
4806 }
4807 } else {
4808 // Register allocator doesn't create partial overlap.
4809 DCHECK(!o_l.Is(high));
4810 DCHECK(!o_h.Is(low));
Anton Kirilov644032c2016-12-06 17:51:43 +00004811 int32_t cst = Int32ConstantFrom(second);
Artem Serov02d37832016-10-25 15:25:33 +01004812 uint32_t shift_value = cst & kMaxLongShiftDistance;
4813 if (shift_value > 32) {
4814 if (op->IsShl()) {
4815 __ Lsl(o_h, low, shift_value - 32);
4816 __ Mov(o_l, 0);
4817 } else if (op->IsShr()) {
4818 __ Asr(o_l, high, shift_value - 32);
4819 __ Asr(o_h, high, 31);
4820 } else {
4821 __ Lsr(o_l, high, shift_value - 32);
4822 __ Mov(o_h, 0);
4823 }
4824 } else if (shift_value == 32) {
4825 if (op->IsShl()) {
4826 __ Mov(o_h, low);
4827 __ Mov(o_l, 0);
4828 } else if (op->IsShr()) {
4829 __ Mov(o_l, high);
4830 __ Asr(o_h, high, 31);
4831 } else {
4832 __ Mov(o_l, high);
4833 __ Mov(o_h, 0);
4834 }
4835 } else if (shift_value == 1) {
4836 if (op->IsShl()) {
4837 __ Lsls(o_l, low, 1);
4838 __ Adc(o_h, high, high);
4839 } else if (op->IsShr()) {
4840 __ Asrs(o_h, high, 1);
4841 __ Rrx(o_l, low);
4842 } else {
4843 __ Lsrs(o_h, high, 1);
4844 __ Rrx(o_l, low);
4845 }
4846 } else {
4847 DCHECK(2 <= shift_value && shift_value < 32) << shift_value;
4848 if (op->IsShl()) {
4849 __ Lsl(o_h, high, shift_value);
4850 __ Orr(o_h, o_h, Operand(low, ShiftType::LSR, 32 - shift_value));
4851 __ Lsl(o_l, low, shift_value);
4852 } else if (op->IsShr()) {
4853 __ Lsr(o_l, low, shift_value);
4854 __ Orr(o_l, o_l, Operand(high, ShiftType::LSL, 32 - shift_value));
4855 __ Asr(o_h, high, shift_value);
4856 } else {
4857 __ Lsr(o_l, low, shift_value);
4858 __ Orr(o_l, o_l, Operand(high, ShiftType::LSL, 32 - shift_value));
4859 __ Lsr(o_h, high, shift_value);
4860 }
4861 }
4862 }
4863 break;
4864 }
4865 default:
4866 LOG(FATAL) << "Unexpected operation type " << type;
4867 UNREACHABLE();
4868 }
4869}
4870
4871void LocationsBuilderARMVIXL::VisitShl(HShl* shl) {
4872 HandleShift(shl);
4873}
4874
4875void InstructionCodeGeneratorARMVIXL::VisitShl(HShl* shl) {
4876 HandleShift(shl);
4877}
4878
4879void LocationsBuilderARMVIXL::VisitShr(HShr* shr) {
4880 HandleShift(shr);
4881}
4882
4883void InstructionCodeGeneratorARMVIXL::VisitShr(HShr* shr) {
4884 HandleShift(shr);
4885}
4886
4887void LocationsBuilderARMVIXL::VisitUShr(HUShr* ushr) {
4888 HandleShift(ushr);
4889}
4890
4891void InstructionCodeGeneratorARMVIXL::VisitUShr(HUShr* ushr) {
4892 HandleShift(ushr);
4893}
4894
4895void LocationsBuilderARMVIXL::VisitNewInstance(HNewInstance* instruction) {
4896 LocationSummary* locations =
4897 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
4898 if (instruction->IsStringAlloc()) {
4899 locations->AddTemp(LocationFrom(kMethodRegister));
4900 } else {
4901 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4902 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
Artem Serov02d37832016-10-25 15:25:33 +01004903 }
4904 locations->SetOut(LocationFrom(r0));
4905}
4906
4907void InstructionCodeGeneratorARMVIXL::VisitNewInstance(HNewInstance* instruction) {
4908 // Note: if heap poisoning is enabled, the entry point takes cares
4909 // of poisoning the reference.
4910 if (instruction->IsStringAlloc()) {
4911 // String is allocated through StringFactory. Call NewEmptyString entry point.
4912 vixl32::Register temp = RegisterFrom(instruction->GetLocations()->GetTemp(0));
4913 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize);
4914 GetAssembler()->LoadFromOffset(kLoadWord, temp, tr, QUICK_ENTRY_POINT(pNewEmptyString));
4915 GetAssembler()->LoadFromOffset(kLoadWord, lr, temp, code_offset.Int32Value());
Alexandre Rames374ddf32016-11-04 10:40:49 +00004916 // 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 +00004917 ExactAssemblyScope aas(GetVIXLAssembler(),
4918 vixl32::k16BitT32InstructionSizeInBytes,
4919 CodeBufferCheckScope::kExactSize);
Artem Serov02d37832016-10-25 15:25:33 +01004920 __ blx(lr);
4921 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4922 } else {
4923 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00004924 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
Artem Serov02d37832016-10-25 15:25:33 +01004925 }
4926}
4927
4928void LocationsBuilderARMVIXL::VisitNewArray(HNewArray* instruction) {
4929 LocationSummary* locations =
4930 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
4931 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Artem Serov02d37832016-10-25 15:25:33 +01004932 locations->SetOut(LocationFrom(r0));
Nicolas Geoffray8c7c4f12017-01-26 10:13:11 +00004933 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4934 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
Artem Serov02d37832016-10-25 15:25:33 +01004935}
4936
4937void InstructionCodeGeneratorARMVIXL::VisitNewArray(HNewArray* instruction) {
Artem Serov02d37832016-10-25 15:25:33 +01004938 // Note: if heap poisoning is enabled, the entry point takes cares
4939 // of poisoning the reference.
Artem Serov7b3672e2017-02-03 17:30:34 +00004940 QuickEntrypointEnum entrypoint =
4941 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
4942 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004943 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Artem Serov7b3672e2017-02-03 17:30:34 +00004944 DCHECK(!codegen_->IsLeafMethod());
Artem Serov02d37832016-10-25 15:25:33 +01004945}
4946
4947void LocationsBuilderARMVIXL::VisitParameterValue(HParameterValue* instruction) {
4948 LocationSummary* locations =
4949 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4950 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4951 if (location.IsStackSlot()) {
4952 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4953 } else if (location.IsDoubleStackSlot()) {
4954 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4955 }
4956 locations->SetOut(location);
4957}
4958
4959void InstructionCodeGeneratorARMVIXL::VisitParameterValue(
4960 HParameterValue* instruction ATTRIBUTE_UNUSED) {
4961 // Nothing to do, the parameter is already at its location.
4962}
4963
4964void LocationsBuilderARMVIXL::VisitCurrentMethod(HCurrentMethod* instruction) {
4965 LocationSummary* locations =
4966 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4967 locations->SetOut(LocationFrom(kMethodRegister));
4968}
4969
4970void InstructionCodeGeneratorARMVIXL::VisitCurrentMethod(
4971 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
4972 // Nothing to do, the method is already at its location.
4973}
4974
4975void LocationsBuilderARMVIXL::VisitNot(HNot* not_) {
4976 LocationSummary* locations =
4977 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
4978 locations->SetInAt(0, Location::RequiresRegister());
4979 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4980}
4981
4982void InstructionCodeGeneratorARMVIXL::VisitNot(HNot* not_) {
4983 LocationSummary* locations = not_->GetLocations();
4984 Location out = locations->Out();
4985 Location in = locations->InAt(0);
4986 switch (not_->GetResultType()) {
4987 case Primitive::kPrimInt:
4988 __ Mvn(OutputRegister(not_), InputRegisterAt(not_, 0));
4989 break;
4990
4991 case Primitive::kPrimLong:
4992 __ Mvn(LowRegisterFrom(out), LowRegisterFrom(in));
4993 __ Mvn(HighRegisterFrom(out), HighRegisterFrom(in));
4994 break;
4995
4996 default:
4997 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4998 }
4999}
5000
Scott Wakelingc34dba72016-10-03 10:14:44 +01005001void LocationsBuilderARMVIXL::VisitBooleanNot(HBooleanNot* bool_not) {
5002 LocationSummary* locations =
5003 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
5004 locations->SetInAt(0, Location::RequiresRegister());
5005 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5006}
5007
5008void InstructionCodeGeneratorARMVIXL::VisitBooleanNot(HBooleanNot* bool_not) {
5009 __ Eor(OutputRegister(bool_not), InputRegister(bool_not), 1);
5010}
5011
Artem Serov02d37832016-10-25 15:25:33 +01005012void LocationsBuilderARMVIXL::VisitCompare(HCompare* compare) {
5013 LocationSummary* locations =
5014 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
5015 switch (compare->InputAt(0)->GetType()) {
5016 case Primitive::kPrimBoolean:
5017 case Primitive::kPrimByte:
5018 case Primitive::kPrimShort:
5019 case Primitive::kPrimChar:
5020 case Primitive::kPrimInt:
5021 case Primitive::kPrimLong: {
5022 locations->SetInAt(0, Location::RequiresRegister());
5023 locations->SetInAt(1, Location::RequiresRegister());
5024 // Output overlaps because it is written before doing the low comparison.
5025 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
5026 break;
5027 }
5028 case Primitive::kPrimFloat:
5029 case Primitive::kPrimDouble: {
5030 locations->SetInAt(0, Location::RequiresFpuRegister());
5031 locations->SetInAt(1, ArithmeticZeroOrFpuRegister(compare->InputAt(1)));
5032 locations->SetOut(Location::RequiresRegister());
5033 break;
5034 }
5035 default:
5036 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
5037 }
5038}
5039
5040void InstructionCodeGeneratorARMVIXL::VisitCompare(HCompare* compare) {
5041 LocationSummary* locations = compare->GetLocations();
5042 vixl32::Register out = OutputRegister(compare);
5043 Location left = locations->InAt(0);
5044 Location right = locations->InAt(1);
5045
5046 vixl32::Label less, greater, done;
Anton Kirilov6f644202017-02-27 18:29:45 +00005047 vixl32::Label* final_label = codegen_->GetFinalLabel(compare, &done);
Artem Serov02d37832016-10-25 15:25:33 +01005048 Primitive::Type type = compare->InputAt(0)->GetType();
5049 vixl32::Condition less_cond = vixl32::Condition(kNone);
5050 switch (type) {
5051 case Primitive::kPrimBoolean:
5052 case Primitive::kPrimByte:
5053 case Primitive::kPrimShort:
5054 case Primitive::kPrimChar:
5055 case Primitive::kPrimInt: {
5056 // Emit move to `out` before the `Cmp`, as `Mov` might affect the status flags.
5057 __ Mov(out, 0);
5058 __ Cmp(RegisterFrom(left), RegisterFrom(right)); // Signed compare.
5059 less_cond = lt;
5060 break;
5061 }
5062 case Primitive::kPrimLong: {
5063 __ Cmp(HighRegisterFrom(left), HighRegisterFrom(right)); // Signed compare.
Artem Serov517d9f62016-12-12 15:51:15 +00005064 __ B(lt, &less, /* far_target */ false);
5065 __ B(gt, &greater, /* far_target */ false);
Artem Serov02d37832016-10-25 15:25:33 +01005066 // Emit move to `out` before the last `Cmp`, as `Mov` might affect the status flags.
5067 __ Mov(out, 0);
5068 __ Cmp(LowRegisterFrom(left), LowRegisterFrom(right)); // Unsigned compare.
5069 less_cond = lo;
5070 break;
5071 }
5072 case Primitive::kPrimFloat:
5073 case Primitive::kPrimDouble: {
5074 __ Mov(out, 0);
Donghui Bai426b49c2016-11-08 14:55:38 +08005075 GenerateVcmp(compare, codegen_);
Artem Serov02d37832016-10-25 15:25:33 +01005076 // To branch on the FP compare result we transfer FPSCR to APSR (encoded as PC in VMRS).
5077 __ Vmrs(RegisterOrAPSR_nzcv(kPcCode), FPSCR);
5078 less_cond = ARMFPCondition(kCondLT, compare->IsGtBias());
5079 break;
5080 }
5081 default:
5082 LOG(FATAL) << "Unexpected compare type " << type;
5083 UNREACHABLE();
5084 }
5085
Anton Kirilov6f644202017-02-27 18:29:45 +00005086 __ B(eq, final_label, /* far_target */ false);
Artem Serov517d9f62016-12-12 15:51:15 +00005087 __ B(less_cond, &less, /* far_target */ false);
Artem Serov02d37832016-10-25 15:25:33 +01005088
5089 __ Bind(&greater);
5090 __ Mov(out, 1);
Anton Kirilov6f644202017-02-27 18:29:45 +00005091 __ B(final_label);
Artem Serov02d37832016-10-25 15:25:33 +01005092
5093 __ Bind(&less);
5094 __ Mov(out, -1);
5095
Anton Kirilov6f644202017-02-27 18:29:45 +00005096 if (done.IsReferenced()) {
5097 __ Bind(&done);
5098 }
Artem Serov02d37832016-10-25 15:25:33 +01005099}
5100
5101void LocationsBuilderARMVIXL::VisitPhi(HPhi* instruction) {
5102 LocationSummary* locations =
5103 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5104 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
5105 locations->SetInAt(i, Location::Any());
5106 }
5107 locations->SetOut(Location::Any());
5108}
5109
5110void InstructionCodeGeneratorARMVIXL::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
5111 LOG(FATAL) << "Unreachable";
5112}
5113
5114void CodeGeneratorARMVIXL::GenerateMemoryBarrier(MemBarrierKind kind) {
5115 // TODO (ported from quick): revisit ARM barrier kinds.
5116 DmbOptions flavor = DmbOptions::ISH; // Quiet C++ warnings.
5117 switch (kind) {
5118 case MemBarrierKind::kAnyStore:
5119 case MemBarrierKind::kLoadAny:
5120 case MemBarrierKind::kAnyAny: {
5121 flavor = DmbOptions::ISH;
5122 break;
5123 }
5124 case MemBarrierKind::kStoreStore: {
5125 flavor = DmbOptions::ISHST;
5126 break;
5127 }
5128 default:
5129 LOG(FATAL) << "Unexpected memory barrier " << kind;
5130 }
5131 __ Dmb(flavor);
5132}
5133
5134void InstructionCodeGeneratorARMVIXL::GenerateWideAtomicLoad(vixl32::Register addr,
5135 uint32_t offset,
5136 vixl32::Register out_lo,
5137 vixl32::Register out_hi) {
5138 UseScratchRegisterScope temps(GetVIXLAssembler());
5139 if (offset != 0) {
5140 vixl32::Register temp = temps.Acquire();
5141 __ Add(temp, addr, offset);
5142 addr = temp;
5143 }
Scott Wakelingb77051e2016-11-21 19:46:00 +00005144 __ Ldrexd(out_lo, out_hi, MemOperand(addr));
Artem Serov02d37832016-10-25 15:25:33 +01005145}
5146
5147void InstructionCodeGeneratorARMVIXL::GenerateWideAtomicStore(vixl32::Register addr,
5148 uint32_t offset,
5149 vixl32::Register value_lo,
5150 vixl32::Register value_hi,
5151 vixl32::Register temp1,
5152 vixl32::Register temp2,
5153 HInstruction* instruction) {
5154 UseScratchRegisterScope temps(GetVIXLAssembler());
5155 vixl32::Label fail;
5156 if (offset != 0) {
5157 vixl32::Register temp = temps.Acquire();
5158 __ Add(temp, addr, offset);
5159 addr = temp;
5160 }
5161 __ Bind(&fail);
Alexandre Rames374ddf32016-11-04 10:40:49 +00005162 {
5163 // Ensure the pc position is recorded immediately after the `ldrexd` instruction.
Artem Serov0fb37192016-12-06 18:13:40 +00005164 ExactAssemblyScope aas(GetVIXLAssembler(),
5165 vixl32::kMaxInstructionSizeInBytes,
5166 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00005167 // We need a load followed by store. (The address used in a STREX instruction must
5168 // be the same as the address in the most recently executed LDREX instruction.)
5169 __ ldrexd(temp1, temp2, MemOperand(addr));
5170 codegen_->MaybeRecordImplicitNullCheck(instruction);
5171 }
Scott Wakelingb77051e2016-11-21 19:46:00 +00005172 __ Strexd(temp1, value_lo, value_hi, MemOperand(addr));
xueliang.zhongf51bc622016-11-04 09:23:32 +00005173 __ CompareAndBranchIfNonZero(temp1, &fail);
Artem Serov02d37832016-10-25 15:25:33 +01005174}
Artem Serov02109dd2016-09-23 17:17:54 +01005175
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005176void LocationsBuilderARMVIXL::HandleFieldSet(
5177 HInstruction* instruction, const FieldInfo& field_info) {
5178 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
5179
5180 LocationSummary* locations =
5181 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5182 locations->SetInAt(0, Location::RequiresRegister());
5183
5184 Primitive::Type field_type = field_info.GetFieldType();
5185 if (Primitive::IsFloatingPointType(field_type)) {
5186 locations->SetInAt(1, Location::RequiresFpuRegister());
5187 } else {
5188 locations->SetInAt(1, Location::RequiresRegister());
5189 }
5190
5191 bool is_wide = field_type == Primitive::kPrimLong || field_type == Primitive::kPrimDouble;
5192 bool generate_volatile = field_info.IsVolatile()
5193 && is_wide
5194 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
5195 bool needs_write_barrier =
5196 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
5197 // Temporary registers for the write barrier.
5198 // TODO: consider renaming StoreNeedsWriteBarrier to StoreNeedsGCMark.
5199 if (needs_write_barrier) {
5200 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
5201 locations->AddTemp(Location::RequiresRegister());
5202 } else if (generate_volatile) {
5203 // ARM encoding have some additional constraints for ldrexd/strexd:
5204 // - registers need to be consecutive
5205 // - the first register should be even but not R14.
5206 // We don't test for ARM yet, and the assertion makes sure that we
5207 // revisit this if we ever enable ARM encoding.
5208 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
5209
5210 locations->AddTemp(Location::RequiresRegister());
5211 locations->AddTemp(Location::RequiresRegister());
5212 if (field_type == Primitive::kPrimDouble) {
5213 // For doubles we need two more registers to copy the value.
5214 locations->AddTemp(LocationFrom(r2));
5215 locations->AddTemp(LocationFrom(r3));
5216 }
5217 }
5218}
5219
5220void InstructionCodeGeneratorARMVIXL::HandleFieldSet(HInstruction* instruction,
5221 const FieldInfo& field_info,
5222 bool value_can_be_null) {
5223 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
5224
5225 LocationSummary* locations = instruction->GetLocations();
5226 vixl32::Register base = InputRegisterAt(instruction, 0);
5227 Location value = locations->InAt(1);
5228
5229 bool is_volatile = field_info.IsVolatile();
5230 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
5231 Primitive::Type field_type = field_info.GetFieldType();
5232 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
5233 bool needs_write_barrier =
5234 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
5235
5236 if (is_volatile) {
5237 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
5238 }
5239
5240 switch (field_type) {
5241 case Primitive::kPrimBoolean:
5242 case Primitive::kPrimByte: {
5243 GetAssembler()->StoreToOffset(kStoreByte, RegisterFrom(value), base, offset);
5244 break;
5245 }
5246
5247 case Primitive::kPrimShort:
5248 case Primitive::kPrimChar: {
5249 GetAssembler()->StoreToOffset(kStoreHalfword, RegisterFrom(value), base, offset);
5250 break;
5251 }
5252
5253 case Primitive::kPrimInt:
5254 case Primitive::kPrimNot: {
5255 if (kPoisonHeapReferences && needs_write_barrier) {
5256 // Note that in the case where `value` is a null reference,
5257 // we do not enter this block, as a null reference does not
5258 // need poisoning.
5259 DCHECK_EQ(field_type, Primitive::kPrimNot);
5260 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
5261 __ Mov(temp, RegisterFrom(value));
5262 GetAssembler()->PoisonHeapReference(temp);
5263 GetAssembler()->StoreToOffset(kStoreWord, temp, base, offset);
5264 } else {
5265 GetAssembler()->StoreToOffset(kStoreWord, RegisterFrom(value), base, offset);
5266 }
5267 break;
5268 }
5269
5270 case Primitive::kPrimLong: {
5271 if (is_volatile && !atomic_ldrd_strd) {
5272 GenerateWideAtomicStore(base,
5273 offset,
5274 LowRegisterFrom(value),
5275 HighRegisterFrom(value),
5276 RegisterFrom(locations->GetTemp(0)),
5277 RegisterFrom(locations->GetTemp(1)),
5278 instruction);
5279 } else {
5280 GetAssembler()->StoreToOffset(kStoreWordPair, LowRegisterFrom(value), base, offset);
5281 codegen_->MaybeRecordImplicitNullCheck(instruction);
5282 }
5283 break;
5284 }
5285
5286 case Primitive::kPrimFloat: {
5287 GetAssembler()->StoreSToOffset(SRegisterFrom(value), base, offset);
5288 break;
5289 }
5290
5291 case Primitive::kPrimDouble: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01005292 vixl32::DRegister value_reg = DRegisterFrom(value);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005293 if (is_volatile && !atomic_ldrd_strd) {
5294 vixl32::Register value_reg_lo = RegisterFrom(locations->GetTemp(0));
5295 vixl32::Register value_reg_hi = RegisterFrom(locations->GetTemp(1));
5296
5297 __ Vmov(value_reg_lo, value_reg_hi, value_reg);
5298
5299 GenerateWideAtomicStore(base,
5300 offset,
5301 value_reg_lo,
5302 value_reg_hi,
5303 RegisterFrom(locations->GetTemp(2)),
5304 RegisterFrom(locations->GetTemp(3)),
5305 instruction);
5306 } else {
5307 GetAssembler()->StoreDToOffset(value_reg, base, offset);
5308 codegen_->MaybeRecordImplicitNullCheck(instruction);
5309 }
5310 break;
5311 }
5312
5313 case Primitive::kPrimVoid:
5314 LOG(FATAL) << "Unreachable type " << field_type;
5315 UNREACHABLE();
5316 }
5317
5318 // Longs and doubles are handled in the switch.
5319 if (field_type != Primitive::kPrimLong && field_type != Primitive::kPrimDouble) {
Alexandre Rames374ddf32016-11-04 10:40:49 +00005320 // TODO(VIXL): Here and for other calls to `MaybeRecordImplicitNullCheck` in this method, we
5321 // should use a scope and the assembler to emit the store instruction to guarantee that we
5322 // record the pc at the correct position. But the `Assembler` does not automatically handle
5323 // unencodable offsets. Practically, everything is fine because the helper and VIXL, at the time
5324 // of writing, do generate the store instruction last.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005325 codegen_->MaybeRecordImplicitNullCheck(instruction);
5326 }
5327
5328 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
5329 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
5330 vixl32::Register card = RegisterFrom(locations->GetTemp(1));
5331 codegen_->MarkGCCard(temp, card, base, RegisterFrom(value), value_can_be_null);
5332 }
5333
5334 if (is_volatile) {
5335 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
5336 }
5337}
5338
Artem Serov02d37832016-10-25 15:25:33 +01005339void LocationsBuilderARMVIXL::HandleFieldGet(HInstruction* instruction,
5340 const FieldInfo& field_info) {
5341 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
5342
5343 bool object_field_get_with_read_barrier =
5344 kEmitCompilerReadBarrier && (field_info.GetFieldType() == Primitive::kPrimNot);
5345 LocationSummary* locations =
5346 new (GetGraph()->GetArena()) LocationSummary(instruction,
5347 object_field_get_with_read_barrier ?
5348 LocationSummary::kCallOnSlowPath :
5349 LocationSummary::kNoCall);
5350 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
5351 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
5352 }
5353 locations->SetInAt(0, Location::RequiresRegister());
5354
5355 bool volatile_for_double = field_info.IsVolatile()
5356 && (field_info.GetFieldType() == Primitive::kPrimDouble)
5357 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
5358 // The output overlaps in case of volatile long: we don't want the
5359 // code generated by GenerateWideAtomicLoad to overwrite the
5360 // object's location. Likewise, in the case of an object field get
5361 // with read barriers enabled, we do not want the load to overwrite
5362 // the object's location, as we need it to emit the read barrier.
5363 bool overlap = (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) ||
5364 object_field_get_with_read_barrier;
5365
5366 if (Primitive::IsFloatingPointType(instruction->GetType())) {
5367 locations->SetOut(Location::RequiresFpuRegister());
5368 } else {
5369 locations->SetOut(Location::RequiresRegister(),
5370 (overlap ? Location::kOutputOverlap : Location::kNoOutputOverlap));
5371 }
5372 if (volatile_for_double) {
5373 // ARM encoding have some additional constraints for ldrexd/strexd:
5374 // - registers need to be consecutive
5375 // - the first register should be even but not R14.
5376 // We don't test for ARM yet, and the assertion makes sure that we
5377 // revisit this if we ever enable ARM encoding.
5378 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
5379 locations->AddTemp(Location::RequiresRegister());
5380 locations->AddTemp(Location::RequiresRegister());
5381 } else if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
5382 // We need a temporary register for the read barrier marking slow
Artem Serovc5fcb442016-12-02 19:19:58 +00005383 // path in CodeGeneratorARMVIXL::GenerateFieldLoadWithBakerReadBarrier.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01005384 if (kBakerReadBarrierLinkTimeThunksEnableForFields &&
5385 !Runtime::Current()->UseJitCompilation()) {
5386 // If link-time thunks for the Baker read barrier are enabled, for AOT
5387 // loads we need a temporary only if the offset is too big.
5388 if (field_info.GetFieldOffset().Uint32Value() >= kReferenceLoadMinFarOffset) {
5389 locations->AddTemp(Location::RequiresRegister());
5390 }
5391 // And we always need the reserved entrypoint register.
5392 locations->AddTemp(Location::RegisterLocation(kBakerCcEntrypointRegister.GetCode()));
5393 } else {
5394 locations->AddTemp(Location::RequiresRegister());
5395 }
Artem Serov02d37832016-10-25 15:25:33 +01005396 }
5397}
5398
5399Location LocationsBuilderARMVIXL::ArithmeticZeroOrFpuRegister(HInstruction* input) {
5400 DCHECK(Primitive::IsFloatingPointType(input->GetType())) << input->GetType();
5401 if ((input->IsFloatConstant() && (input->AsFloatConstant()->IsArithmeticZero())) ||
5402 (input->IsDoubleConstant() && (input->AsDoubleConstant()->IsArithmeticZero()))) {
5403 return Location::ConstantLocation(input->AsConstant());
5404 } else {
5405 return Location::RequiresFpuRegister();
5406 }
5407}
5408
Artem Serov02109dd2016-09-23 17:17:54 +01005409Location LocationsBuilderARMVIXL::ArmEncodableConstantOrRegister(HInstruction* constant,
5410 Opcode opcode) {
5411 DCHECK(!Primitive::IsFloatingPointType(constant->GetType()));
5412 if (constant->IsConstant() &&
5413 CanEncodeConstantAsImmediate(constant->AsConstant(), opcode)) {
5414 return Location::ConstantLocation(constant->AsConstant());
5415 }
5416 return Location::RequiresRegister();
5417}
5418
5419bool LocationsBuilderARMVIXL::CanEncodeConstantAsImmediate(HConstant* input_cst,
5420 Opcode opcode) {
5421 uint64_t value = static_cast<uint64_t>(Int64FromConstant(input_cst));
5422 if (Primitive::Is64BitType(input_cst->GetType())) {
5423 Opcode high_opcode = opcode;
5424 SetCc low_set_cc = kCcDontCare;
5425 switch (opcode) {
5426 case SUB:
5427 // Flip the operation to an ADD.
5428 value = -value;
5429 opcode = ADD;
5430 FALLTHROUGH_INTENDED;
5431 case ADD:
5432 if (Low32Bits(value) == 0u) {
5433 return CanEncodeConstantAsImmediate(High32Bits(value), opcode, kCcDontCare);
5434 }
5435 high_opcode = ADC;
5436 low_set_cc = kCcSet;
5437 break;
5438 default:
5439 break;
5440 }
5441 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode, low_set_cc) &&
5442 CanEncodeConstantAsImmediate(High32Bits(value), high_opcode, kCcDontCare);
5443 } else {
5444 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode);
5445 }
5446}
5447
5448// TODO(VIXL): Replace art::arm::SetCc` with `vixl32::FlagsUpdate after flags set optimization
5449// enabled.
5450bool LocationsBuilderARMVIXL::CanEncodeConstantAsImmediate(uint32_t value,
5451 Opcode opcode,
5452 SetCc set_cc) {
5453 ArmVIXLAssembler* assembler = codegen_->GetAssembler();
5454 if (assembler->ShifterOperandCanHold(opcode, value, set_cc)) {
5455 return true;
5456 }
5457 Opcode neg_opcode = kNoOperand;
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00005458 uint32_t neg_value = 0;
Artem Serov02109dd2016-09-23 17:17:54 +01005459 switch (opcode) {
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00005460 case AND: neg_opcode = BIC; neg_value = ~value; break;
5461 case ORR: neg_opcode = ORN; neg_value = ~value; break;
5462 case ADD: neg_opcode = SUB; neg_value = -value; break;
5463 case ADC: neg_opcode = SBC; neg_value = ~value; break;
5464 case SUB: neg_opcode = ADD; neg_value = -value; break;
5465 case SBC: neg_opcode = ADC; neg_value = ~value; break;
5466 case MOV: neg_opcode = MVN; neg_value = ~value; break;
Artem Serov02109dd2016-09-23 17:17:54 +01005467 default:
5468 return false;
5469 }
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00005470
5471 if (assembler->ShifterOperandCanHold(neg_opcode, neg_value, set_cc)) {
5472 return true;
5473 }
5474
5475 return opcode == AND && IsPowerOfTwo(value + 1);
Artem Serov02109dd2016-09-23 17:17:54 +01005476}
5477
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005478void InstructionCodeGeneratorARMVIXL::HandleFieldGet(HInstruction* instruction,
5479 const FieldInfo& field_info) {
5480 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
5481
5482 LocationSummary* locations = instruction->GetLocations();
5483 vixl32::Register base = InputRegisterAt(instruction, 0);
5484 Location out = locations->Out();
5485 bool is_volatile = field_info.IsVolatile();
5486 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
5487 Primitive::Type field_type = field_info.GetFieldType();
5488 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
5489
5490 switch (field_type) {
5491 case Primitive::kPrimBoolean:
5492 GetAssembler()->LoadFromOffset(kLoadUnsignedByte, RegisterFrom(out), base, offset);
5493 break;
5494
5495 case Primitive::kPrimByte:
5496 GetAssembler()->LoadFromOffset(kLoadSignedByte, RegisterFrom(out), base, offset);
5497 break;
5498
5499 case Primitive::kPrimShort:
5500 GetAssembler()->LoadFromOffset(kLoadSignedHalfword, RegisterFrom(out), base, offset);
5501 break;
5502
5503 case Primitive::kPrimChar:
5504 GetAssembler()->LoadFromOffset(kLoadUnsignedHalfword, RegisterFrom(out), base, offset);
5505 break;
5506
5507 case Primitive::kPrimInt:
5508 GetAssembler()->LoadFromOffset(kLoadWord, RegisterFrom(out), base, offset);
5509 break;
5510
5511 case Primitive::kPrimNot: {
5512 // /* HeapReference<Object> */ out = *(base + offset)
5513 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005514 Location temp_loc = locations->GetTemp(0);
5515 // Note that a potential implicit null check is handled in this
5516 // CodeGeneratorARMVIXL::GenerateFieldLoadWithBakerReadBarrier call.
5517 codegen_->GenerateFieldLoadWithBakerReadBarrier(
5518 instruction, out, base, offset, temp_loc, /* needs_null_check */ true);
5519 if (is_volatile) {
5520 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5521 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005522 } else {
5523 GetAssembler()->LoadFromOffset(kLoadWord, RegisterFrom(out), base, offset);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005524 codegen_->MaybeRecordImplicitNullCheck(instruction);
5525 if (is_volatile) {
5526 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5527 }
5528 // If read barriers are enabled, emit read barriers other than
5529 // Baker's using a slow path (and also unpoison the loaded
5530 // reference, if heap poisoning is enabled).
5531 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, locations->InAt(0), offset);
5532 }
5533 break;
5534 }
5535
5536 case Primitive::kPrimLong:
5537 if (is_volatile && !atomic_ldrd_strd) {
5538 GenerateWideAtomicLoad(base, offset, LowRegisterFrom(out), HighRegisterFrom(out));
5539 } else {
5540 GetAssembler()->LoadFromOffset(kLoadWordPair, LowRegisterFrom(out), base, offset);
5541 }
5542 break;
5543
5544 case Primitive::kPrimFloat:
5545 GetAssembler()->LoadSFromOffset(SRegisterFrom(out), base, offset);
5546 break;
5547
5548 case Primitive::kPrimDouble: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01005549 vixl32::DRegister out_dreg = DRegisterFrom(out);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005550 if (is_volatile && !atomic_ldrd_strd) {
5551 vixl32::Register lo = RegisterFrom(locations->GetTemp(0));
5552 vixl32::Register hi = RegisterFrom(locations->GetTemp(1));
5553 GenerateWideAtomicLoad(base, offset, lo, hi);
5554 // TODO(VIXL): Do we need to be immediately after the ldrexd instruction? If so we need a
5555 // scope.
5556 codegen_->MaybeRecordImplicitNullCheck(instruction);
5557 __ Vmov(out_dreg, lo, hi);
5558 } else {
5559 GetAssembler()->LoadDFromOffset(out_dreg, base, offset);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005560 codegen_->MaybeRecordImplicitNullCheck(instruction);
5561 }
5562 break;
5563 }
5564
5565 case Primitive::kPrimVoid:
5566 LOG(FATAL) << "Unreachable type " << field_type;
5567 UNREACHABLE();
5568 }
5569
5570 if (field_type == Primitive::kPrimNot || field_type == Primitive::kPrimDouble) {
5571 // Potential implicit null checks, in the case of reference or
5572 // double fields, are handled in the previous switch statement.
5573 } else {
5574 // Address cases other than reference and double that may require an implicit null check.
Alexandre Rames374ddf32016-11-04 10:40:49 +00005575 // TODO(VIXL): Here and for other calls to `MaybeRecordImplicitNullCheck` in this method, we
5576 // should use a scope and the assembler to emit the load instruction to guarantee that we
5577 // record the pc at the correct position. But the `Assembler` does not automatically handle
5578 // unencodable offsets. Practically, everything is fine because the helper and VIXL, at the time
5579 // of writing, do generate the store instruction last.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005580 codegen_->MaybeRecordImplicitNullCheck(instruction);
5581 }
5582
5583 if (is_volatile) {
5584 if (field_type == Primitive::kPrimNot) {
5585 // Memory barriers, in the case of references, are also handled
5586 // in the previous switch statement.
5587 } else {
5588 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5589 }
5590 }
5591}
5592
5593void LocationsBuilderARMVIXL::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
5594 HandleFieldSet(instruction, instruction->GetFieldInfo());
5595}
5596
5597void InstructionCodeGeneratorARMVIXL::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
5598 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
5599}
5600
5601void LocationsBuilderARMVIXL::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5602 HandleFieldGet(instruction, instruction->GetFieldInfo());
5603}
5604
5605void InstructionCodeGeneratorARMVIXL::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5606 HandleFieldGet(instruction, instruction->GetFieldInfo());
5607}
5608
5609void LocationsBuilderARMVIXL::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5610 HandleFieldGet(instruction, instruction->GetFieldInfo());
5611}
5612
5613void InstructionCodeGeneratorARMVIXL::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5614 HandleFieldGet(instruction, instruction->GetFieldInfo());
5615}
5616
Scott Wakelingc34dba72016-10-03 10:14:44 +01005617void LocationsBuilderARMVIXL::VisitStaticFieldSet(HStaticFieldSet* instruction) {
5618 HandleFieldSet(instruction, instruction->GetFieldInfo());
5619}
5620
5621void InstructionCodeGeneratorARMVIXL::VisitStaticFieldSet(HStaticFieldSet* instruction) {
5622 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
5623}
5624
Artem Serovcfbe9132016-10-14 15:58:56 +01005625void LocationsBuilderARMVIXL::VisitUnresolvedInstanceFieldGet(
5626 HUnresolvedInstanceFieldGet* instruction) {
5627 FieldAccessCallingConventionARMVIXL calling_convention;
5628 codegen_->CreateUnresolvedFieldLocationSummary(
5629 instruction, instruction->GetFieldType(), calling_convention);
5630}
5631
5632void InstructionCodeGeneratorARMVIXL::VisitUnresolvedInstanceFieldGet(
5633 HUnresolvedInstanceFieldGet* instruction) {
5634 FieldAccessCallingConventionARMVIXL calling_convention;
5635 codegen_->GenerateUnresolvedFieldAccess(instruction,
5636 instruction->GetFieldType(),
5637 instruction->GetFieldIndex(),
5638 instruction->GetDexPc(),
5639 calling_convention);
5640}
5641
5642void LocationsBuilderARMVIXL::VisitUnresolvedInstanceFieldSet(
5643 HUnresolvedInstanceFieldSet* instruction) {
5644 FieldAccessCallingConventionARMVIXL calling_convention;
5645 codegen_->CreateUnresolvedFieldLocationSummary(
5646 instruction, instruction->GetFieldType(), calling_convention);
5647}
5648
5649void InstructionCodeGeneratorARMVIXL::VisitUnresolvedInstanceFieldSet(
5650 HUnresolvedInstanceFieldSet* instruction) {
5651 FieldAccessCallingConventionARMVIXL calling_convention;
5652 codegen_->GenerateUnresolvedFieldAccess(instruction,
5653 instruction->GetFieldType(),
5654 instruction->GetFieldIndex(),
5655 instruction->GetDexPc(),
5656 calling_convention);
5657}
5658
5659void LocationsBuilderARMVIXL::VisitUnresolvedStaticFieldGet(
5660 HUnresolvedStaticFieldGet* instruction) {
5661 FieldAccessCallingConventionARMVIXL calling_convention;
5662 codegen_->CreateUnresolvedFieldLocationSummary(
5663 instruction, instruction->GetFieldType(), calling_convention);
5664}
5665
5666void InstructionCodeGeneratorARMVIXL::VisitUnresolvedStaticFieldGet(
5667 HUnresolvedStaticFieldGet* instruction) {
5668 FieldAccessCallingConventionARMVIXL calling_convention;
5669 codegen_->GenerateUnresolvedFieldAccess(instruction,
5670 instruction->GetFieldType(),
5671 instruction->GetFieldIndex(),
5672 instruction->GetDexPc(),
5673 calling_convention);
5674}
5675
5676void LocationsBuilderARMVIXL::VisitUnresolvedStaticFieldSet(
5677 HUnresolvedStaticFieldSet* instruction) {
5678 FieldAccessCallingConventionARMVIXL calling_convention;
5679 codegen_->CreateUnresolvedFieldLocationSummary(
5680 instruction, instruction->GetFieldType(), calling_convention);
5681}
5682
5683void InstructionCodeGeneratorARMVIXL::VisitUnresolvedStaticFieldSet(
5684 HUnresolvedStaticFieldSet* instruction) {
5685 FieldAccessCallingConventionARMVIXL calling_convention;
5686 codegen_->GenerateUnresolvedFieldAccess(instruction,
5687 instruction->GetFieldType(),
5688 instruction->GetFieldIndex(),
5689 instruction->GetDexPc(),
5690 calling_convention);
5691}
5692
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005693void LocationsBuilderARMVIXL::VisitNullCheck(HNullCheck* instruction) {
Artem Serov657022c2016-11-23 14:19:38 +00005694 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005695 locations->SetInAt(0, Location::RequiresRegister());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005696}
5697
5698void CodeGeneratorARMVIXL::GenerateImplicitNullCheck(HNullCheck* instruction) {
5699 if (CanMoveNullCheckToUser(instruction)) {
5700 return;
5701 }
5702
5703 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames374ddf32016-11-04 10:40:49 +00005704 // Ensure the pc position is recorded immediately after the `ldr` instruction.
Artem Serov0fb37192016-12-06 18:13:40 +00005705 ExactAssemblyScope aas(GetVIXLAssembler(),
5706 vixl32::kMaxInstructionSizeInBytes,
5707 CodeBufferCheckScope::kMaximumSize);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005708 __ ldr(temps.Acquire(), MemOperand(InputRegisterAt(instruction, 0)));
5709 RecordPcInfo(instruction, instruction->GetDexPc());
5710}
5711
5712void CodeGeneratorARMVIXL::GenerateExplicitNullCheck(HNullCheck* instruction) {
5713 NullCheckSlowPathARMVIXL* slow_path =
5714 new (GetGraph()->GetArena()) NullCheckSlowPathARMVIXL(instruction);
5715 AddSlowPath(slow_path);
xueliang.zhongf51bc622016-11-04 09:23:32 +00005716 __ CompareAndBranchIfZero(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005717}
5718
5719void InstructionCodeGeneratorARMVIXL::VisitNullCheck(HNullCheck* instruction) {
5720 codegen_->GenerateNullCheck(instruction);
5721}
5722
Scott Wakelingc34dba72016-10-03 10:14:44 +01005723static LoadOperandType GetLoadOperandType(Primitive::Type type) {
5724 switch (type) {
5725 case Primitive::kPrimNot:
5726 return kLoadWord;
5727 case Primitive::kPrimBoolean:
5728 return kLoadUnsignedByte;
5729 case Primitive::kPrimByte:
5730 return kLoadSignedByte;
5731 case Primitive::kPrimChar:
5732 return kLoadUnsignedHalfword;
5733 case Primitive::kPrimShort:
5734 return kLoadSignedHalfword;
5735 case Primitive::kPrimInt:
5736 return kLoadWord;
5737 case Primitive::kPrimLong:
5738 return kLoadWordPair;
5739 case Primitive::kPrimFloat:
5740 return kLoadSWord;
5741 case Primitive::kPrimDouble:
5742 return kLoadDWord;
5743 default:
5744 LOG(FATAL) << "Unreachable type " << type;
5745 UNREACHABLE();
5746 }
5747}
5748
5749static StoreOperandType GetStoreOperandType(Primitive::Type type) {
5750 switch (type) {
5751 case Primitive::kPrimNot:
5752 return kStoreWord;
5753 case Primitive::kPrimBoolean:
5754 case Primitive::kPrimByte:
5755 return kStoreByte;
5756 case Primitive::kPrimChar:
5757 case Primitive::kPrimShort:
5758 return kStoreHalfword;
5759 case Primitive::kPrimInt:
5760 return kStoreWord;
5761 case Primitive::kPrimLong:
5762 return kStoreWordPair;
5763 case Primitive::kPrimFloat:
5764 return kStoreSWord;
5765 case Primitive::kPrimDouble:
5766 return kStoreDWord;
5767 default:
5768 LOG(FATAL) << "Unreachable type " << type;
5769 UNREACHABLE();
5770 }
5771}
5772
5773void CodeGeneratorARMVIXL::LoadFromShiftedRegOffset(Primitive::Type type,
5774 Location out_loc,
5775 vixl32::Register base,
5776 vixl32::Register reg_index,
5777 vixl32::Condition cond) {
5778 uint32_t shift_count = Primitive::ComponentSizeShift(type);
5779 MemOperand mem_address(base, reg_index, vixl32::LSL, shift_count);
5780
5781 switch (type) {
5782 case Primitive::kPrimByte:
5783 __ Ldrsb(cond, RegisterFrom(out_loc), mem_address);
5784 break;
5785 case Primitive::kPrimBoolean:
5786 __ Ldrb(cond, RegisterFrom(out_loc), mem_address);
5787 break;
5788 case Primitive::kPrimShort:
5789 __ Ldrsh(cond, RegisterFrom(out_loc), mem_address);
5790 break;
5791 case Primitive::kPrimChar:
5792 __ Ldrh(cond, RegisterFrom(out_loc), mem_address);
5793 break;
5794 case Primitive::kPrimNot:
5795 case Primitive::kPrimInt:
5796 __ Ldr(cond, RegisterFrom(out_loc), mem_address);
5797 break;
5798 // T32 doesn't support LoadFromShiftedRegOffset mem address mode for these types.
5799 case Primitive::kPrimLong:
5800 case Primitive::kPrimFloat:
5801 case Primitive::kPrimDouble:
5802 default:
5803 LOG(FATAL) << "Unreachable type " << type;
5804 UNREACHABLE();
5805 }
5806}
5807
5808void CodeGeneratorARMVIXL::StoreToShiftedRegOffset(Primitive::Type type,
5809 Location loc,
5810 vixl32::Register base,
5811 vixl32::Register reg_index,
5812 vixl32::Condition cond) {
5813 uint32_t shift_count = Primitive::ComponentSizeShift(type);
5814 MemOperand mem_address(base, reg_index, vixl32::LSL, shift_count);
5815
5816 switch (type) {
5817 case Primitive::kPrimByte:
5818 case Primitive::kPrimBoolean:
5819 __ Strb(cond, RegisterFrom(loc), mem_address);
5820 break;
5821 case Primitive::kPrimShort:
5822 case Primitive::kPrimChar:
5823 __ Strh(cond, RegisterFrom(loc), mem_address);
5824 break;
5825 case Primitive::kPrimNot:
5826 case Primitive::kPrimInt:
5827 __ Str(cond, RegisterFrom(loc), mem_address);
5828 break;
5829 // T32 doesn't support StoreToShiftedRegOffset mem address mode for these types.
5830 case Primitive::kPrimLong:
5831 case Primitive::kPrimFloat:
5832 case Primitive::kPrimDouble:
5833 default:
5834 LOG(FATAL) << "Unreachable type " << type;
5835 UNREACHABLE();
5836 }
5837}
5838
5839void LocationsBuilderARMVIXL::VisitArrayGet(HArrayGet* instruction) {
5840 bool object_array_get_with_read_barrier =
5841 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
5842 LocationSummary* locations =
5843 new (GetGraph()->GetArena()) LocationSummary(instruction,
5844 object_array_get_with_read_barrier ?
5845 LocationSummary::kCallOnSlowPath :
5846 LocationSummary::kNoCall);
5847 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005848 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Scott Wakelingc34dba72016-10-03 10:14:44 +01005849 }
5850 locations->SetInAt(0, Location::RequiresRegister());
5851 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5852 if (Primitive::IsFloatingPointType(instruction->GetType())) {
5853 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5854 } else {
5855 // The output overlaps in the case of an object array get with
5856 // read barriers enabled: we do not want the move to overwrite the
5857 // array's location, as we need it to emit the read barrier.
5858 locations->SetOut(
5859 Location::RequiresRegister(),
5860 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
5861 }
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01005862 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
5863 // We need a temporary register for the read barrier marking slow
5864 // path in CodeGeneratorARMVIXL::GenerateArrayLoadWithBakerReadBarrier.
5865 if (kBakerReadBarrierLinkTimeThunksEnableForFields &&
5866 !Runtime::Current()->UseJitCompilation() &&
5867 instruction->GetIndex()->IsConstant()) {
5868 // Array loads with constant index are treated as field loads.
5869 // If link-time thunks for the Baker read barrier are enabled, for AOT
5870 // constant index loads we need a temporary only if the offset is too big.
5871 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
5872 uint32_t index = instruction->GetIndex()->AsIntConstant()->GetValue();
5873 offset += index << Primitive::ComponentSizeShift(Primitive::kPrimNot);
5874 if (offset >= kReferenceLoadMinFarOffset) {
5875 locations->AddTemp(Location::RequiresRegister());
5876 }
5877 // And we always need the reserved entrypoint register.
5878 locations->AddTemp(Location::RegisterLocation(kBakerCcEntrypointRegister.GetCode()));
5879 } else if (kBakerReadBarrierLinkTimeThunksEnableForArrays &&
5880 !Runtime::Current()->UseJitCompilation() &&
5881 !instruction->GetIndex()->IsConstant()) {
5882 // We need a non-scratch temporary for the array data pointer.
5883 locations->AddTemp(Location::RequiresRegister());
5884 // And we always need the reserved entrypoint register.
5885 locations->AddTemp(Location::RegisterLocation(kBakerCcEntrypointRegister.GetCode()));
5886 } else {
5887 locations->AddTemp(Location::RequiresRegister());
5888 }
5889 } else if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
5890 // Also need a temporary for String compression feature.
Anton Kirilove28d9ae2016-10-25 18:17:23 +01005891 locations->AddTemp(Location::RequiresRegister());
Scott Wakelingc34dba72016-10-03 10:14:44 +01005892 }
5893}
5894
5895void InstructionCodeGeneratorARMVIXL::VisitArrayGet(HArrayGet* instruction) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01005896 LocationSummary* locations = instruction->GetLocations();
5897 Location obj_loc = locations->InAt(0);
5898 vixl32::Register obj = InputRegisterAt(instruction, 0);
5899 Location index = locations->InAt(1);
5900 Location out_loc = locations->Out();
5901 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
5902 Primitive::Type type = instruction->GetType();
5903 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
5904 instruction->IsStringCharAt();
5905 HInstruction* array_instr = instruction->GetArray();
5906 bool has_intermediate_address = array_instr->IsIntermediateAddress();
Scott Wakelingc34dba72016-10-03 10:14:44 +01005907
5908 switch (type) {
5909 case Primitive::kPrimBoolean:
5910 case Primitive::kPrimByte:
5911 case Primitive::kPrimShort:
5912 case Primitive::kPrimChar:
5913 case Primitive::kPrimInt: {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005914 vixl32::Register length;
5915 if (maybe_compressed_char_at) {
5916 length = RegisterFrom(locations->GetTemp(0));
5917 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
5918 GetAssembler()->LoadFromOffset(kLoadWord, length, obj, count_offset);
5919 codegen_->MaybeRecordImplicitNullCheck(instruction);
5920 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01005921 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00005922 int32_t const_index = Int32ConstantFrom(index);
Scott Wakelingc34dba72016-10-03 10:14:44 +01005923 if (maybe_compressed_char_at) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01005924 vixl32::Label uncompressed_load, done;
Anton Kirilov6f644202017-02-27 18:29:45 +00005925 vixl32::Label* final_label = codegen_->GetFinalLabel(instruction, &done);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005926 __ Lsrs(length, length, 1u); // LSRS has a 16-bit encoding, TST (immediate) does not.
5927 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
5928 "Expecting 0=compressed, 1=uncompressed");
Artem Serov517d9f62016-12-12 15:51:15 +00005929 __ B(cs, &uncompressed_load, /* far_target */ false);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01005930 GetAssembler()->LoadFromOffset(kLoadUnsignedByte,
5931 RegisterFrom(out_loc),
5932 obj,
5933 data_offset + const_index);
Anton Kirilov6f644202017-02-27 18:29:45 +00005934 __ B(final_label);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01005935 __ Bind(&uncompressed_load);
5936 GetAssembler()->LoadFromOffset(GetLoadOperandType(Primitive::kPrimChar),
5937 RegisterFrom(out_loc),
5938 obj,
5939 data_offset + (const_index << 1));
Anton Kirilov6f644202017-02-27 18:29:45 +00005940 if (done.IsReferenced()) {
5941 __ Bind(&done);
5942 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01005943 } else {
5944 uint32_t full_offset = data_offset + (const_index << Primitive::ComponentSizeShift(type));
5945
5946 LoadOperandType load_type = GetLoadOperandType(type);
5947 GetAssembler()->LoadFromOffset(load_type, RegisterFrom(out_loc), obj, full_offset);
5948 }
5949 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005950 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01005951 vixl32::Register temp = temps.Acquire();
5952
5953 if (has_intermediate_address) {
Artem Serov2bbc9532016-10-21 11:51:50 +01005954 // We do not need to compute the intermediate address from the array: the
5955 // input instruction has done it already. See the comment in
5956 // `TryExtractArrayAccessAddress()`.
5957 if (kIsDebugBuild) {
5958 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
Anton Kirilov644032c2016-12-06 17:51:43 +00005959 DCHECK_EQ(Uint64ConstantFrom(tmp->GetOffset()), data_offset);
Artem Serov2bbc9532016-10-21 11:51:50 +01005960 }
5961 temp = obj;
Scott Wakelingc34dba72016-10-03 10:14:44 +01005962 } else {
5963 __ Add(temp, obj, data_offset);
5964 }
5965 if (maybe_compressed_char_at) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01005966 vixl32::Label uncompressed_load, done;
Anton Kirilov6f644202017-02-27 18:29:45 +00005967 vixl32::Label* final_label = codegen_->GetFinalLabel(instruction, &done);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005968 __ Lsrs(length, length, 1u); // LSRS has a 16-bit encoding, TST (immediate) does not.
5969 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
5970 "Expecting 0=compressed, 1=uncompressed");
Artem Serov517d9f62016-12-12 15:51:15 +00005971 __ B(cs, &uncompressed_load, /* far_target */ false);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01005972 __ Ldrb(RegisterFrom(out_loc), MemOperand(temp, RegisterFrom(index), vixl32::LSL, 0));
Anton Kirilov6f644202017-02-27 18:29:45 +00005973 __ B(final_label);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01005974 __ Bind(&uncompressed_load);
5975 __ Ldrh(RegisterFrom(out_loc), MemOperand(temp, RegisterFrom(index), vixl32::LSL, 1));
Anton Kirilov6f644202017-02-27 18:29:45 +00005976 if (done.IsReferenced()) {
5977 __ Bind(&done);
5978 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01005979 } else {
5980 codegen_->LoadFromShiftedRegOffset(type, out_loc, temp, RegisterFrom(index));
5981 }
5982 }
5983 break;
5984 }
5985
5986 case Primitive::kPrimNot: {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005987 // The read barrier instrumentation of object ArrayGet
5988 // instructions does not support the HIntermediateAddress
5989 // instruction.
5990 DCHECK(!(has_intermediate_address && kEmitCompilerReadBarrier));
5991
Scott Wakelingc34dba72016-10-03 10:14:44 +01005992 static_assert(
5993 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5994 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
5995 // /* HeapReference<Object> */ out =
5996 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
5997 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005998 Location temp = locations->GetTemp(0);
5999 // Note that a potential implicit null check is handled in this
6000 // CodeGeneratorARMVIXL::GenerateArrayLoadWithBakerReadBarrier call.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01006001 DCHECK(!instruction->CanDoImplicitNullCheckOn(instruction->InputAt(0)));
6002 if (index.IsConstant()) {
6003 // Array load with a constant index can be treated as a field load.
6004 data_offset += Int32ConstantFrom(index) << Primitive::ComponentSizeShift(type);
6005 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6006 out_loc,
6007 obj,
6008 data_offset,
6009 locations->GetTemp(0),
6010 /* needs_null_check */ false);
6011 } else {
6012 codegen_->GenerateArrayLoadWithBakerReadBarrier(
6013 instruction, out_loc, obj, data_offset, index, temp, /* needs_null_check */ false);
6014 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006015 } else {
6016 vixl32::Register out = OutputRegister(instruction);
6017 if (index.IsConstant()) {
6018 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00006019 (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006020 GetAssembler()->LoadFromOffset(kLoadWord, out, obj, offset);
Alexandre Rames374ddf32016-11-04 10:40:49 +00006021 // TODO(VIXL): Here and for other calls to `MaybeRecordImplicitNullCheck` in this method,
6022 // we should use a scope and the assembler to emit the load instruction to guarantee that
6023 // we record the pc at the correct position. But the `Assembler` does not automatically
6024 // handle unencodable offsets. Practically, everything is fine because the helper and
6025 // VIXL, at the time of writing, do generate the store instruction last.
Scott Wakelingc34dba72016-10-03 10:14:44 +01006026 codegen_->MaybeRecordImplicitNullCheck(instruction);
6027 // If read barriers are enabled, emit read barriers other than
6028 // Baker's using a slow path (and also unpoison the loaded
6029 // reference, if heap poisoning is enabled).
6030 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
6031 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006032 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006033 vixl32::Register temp = temps.Acquire();
6034
6035 if (has_intermediate_address) {
Artem Serov2bbc9532016-10-21 11:51:50 +01006036 // We do not need to compute the intermediate address from the array: the
6037 // input instruction has done it already. See the comment in
6038 // `TryExtractArrayAccessAddress()`.
6039 if (kIsDebugBuild) {
6040 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
Anton Kirilov644032c2016-12-06 17:51:43 +00006041 DCHECK_EQ(Uint64ConstantFrom(tmp->GetOffset()), data_offset);
Artem Serov2bbc9532016-10-21 11:51:50 +01006042 }
6043 temp = obj;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006044 } else {
6045 __ Add(temp, obj, data_offset);
6046 }
6047 codegen_->LoadFromShiftedRegOffset(type, out_loc, temp, RegisterFrom(index));
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006048 temps.Close();
Alexandre Rames374ddf32016-11-04 10:40:49 +00006049 // TODO(VIXL): Use a scope to ensure that we record the pc position immediately after the
6050 // load instruction. Practically, everything is fine because the helper and VIXL, at the
6051 // time of writing, do generate the store instruction last.
Scott Wakelingc34dba72016-10-03 10:14:44 +01006052 codegen_->MaybeRecordImplicitNullCheck(instruction);
6053 // If read barriers are enabled, emit read barriers other than
6054 // Baker's using a slow path (and also unpoison the loaded
6055 // reference, if heap poisoning is enabled).
6056 codegen_->MaybeGenerateReadBarrierSlow(
6057 instruction, out_loc, out_loc, obj_loc, data_offset, index);
6058 }
6059 }
6060 break;
6061 }
6062
6063 case Primitive::kPrimLong: {
6064 if (index.IsConstant()) {
6065 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00006066 (Int32ConstantFrom(index) << TIMES_8) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006067 GetAssembler()->LoadFromOffset(kLoadWordPair, LowRegisterFrom(out_loc), obj, offset);
6068 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006069 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006070 vixl32::Register temp = temps.Acquire();
6071 __ Add(temp, obj, Operand(RegisterFrom(index), vixl32::LSL, TIMES_8));
6072 GetAssembler()->LoadFromOffset(kLoadWordPair, LowRegisterFrom(out_loc), temp, data_offset);
6073 }
6074 break;
6075 }
6076
6077 case Primitive::kPrimFloat: {
6078 vixl32::SRegister out = SRegisterFrom(out_loc);
6079 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006080 size_t offset = (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006081 GetAssembler()->LoadSFromOffset(out, obj, offset);
6082 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006083 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006084 vixl32::Register temp = temps.Acquire();
6085 __ Add(temp, obj, Operand(RegisterFrom(index), vixl32::LSL, TIMES_4));
6086 GetAssembler()->LoadSFromOffset(out, temp, data_offset);
6087 }
6088 break;
6089 }
6090
6091 case Primitive::kPrimDouble: {
6092 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006093 size_t offset = (Int32ConstantFrom(index) << TIMES_8) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006094 GetAssembler()->LoadDFromOffset(DRegisterFrom(out_loc), obj, offset);
6095 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006096 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006097 vixl32::Register temp = temps.Acquire();
6098 __ Add(temp, obj, Operand(RegisterFrom(index), vixl32::LSL, TIMES_8));
6099 GetAssembler()->LoadDFromOffset(DRegisterFrom(out_loc), temp, data_offset);
6100 }
6101 break;
6102 }
6103
6104 case Primitive::kPrimVoid:
6105 LOG(FATAL) << "Unreachable type " << type;
6106 UNREACHABLE();
6107 }
6108
6109 if (type == Primitive::kPrimNot) {
6110 // Potential implicit null checks, in the case of reference
6111 // arrays, are handled in the previous switch statement.
6112 } else if (!maybe_compressed_char_at) {
Alexandre Rames374ddf32016-11-04 10:40:49 +00006113 // TODO(VIXL): Use a scope to ensure we record the pc info immediately after
6114 // the preceding load instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01006115 codegen_->MaybeRecordImplicitNullCheck(instruction);
6116 }
6117}
6118
6119void LocationsBuilderARMVIXL::VisitArraySet(HArraySet* instruction) {
6120 Primitive::Type value_type = instruction->GetComponentType();
6121
6122 bool needs_write_barrier =
6123 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
6124 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
6125
6126 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
6127 instruction,
6128 may_need_runtime_call_for_type_check ?
6129 LocationSummary::kCallOnSlowPath :
6130 LocationSummary::kNoCall);
6131
6132 locations->SetInAt(0, Location::RequiresRegister());
6133 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
6134 if (Primitive::IsFloatingPointType(value_type)) {
6135 locations->SetInAt(2, Location::RequiresFpuRegister());
6136 } else {
6137 locations->SetInAt(2, Location::RequiresRegister());
6138 }
6139 if (needs_write_barrier) {
6140 // Temporary registers for the write barrier.
6141 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
6142 locations->AddTemp(Location::RequiresRegister());
6143 }
6144}
6145
6146void InstructionCodeGeneratorARMVIXL::VisitArraySet(HArraySet* instruction) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006147 LocationSummary* locations = instruction->GetLocations();
6148 vixl32::Register array = InputRegisterAt(instruction, 0);
6149 Location index = locations->InAt(1);
6150 Primitive::Type value_type = instruction->GetComponentType();
6151 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
6152 bool needs_write_barrier =
6153 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
6154 uint32_t data_offset =
6155 mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
6156 Location value_loc = locations->InAt(2);
6157 HInstruction* array_instr = instruction->GetArray();
6158 bool has_intermediate_address = array_instr->IsIntermediateAddress();
Scott Wakelingc34dba72016-10-03 10:14:44 +01006159
6160 switch (value_type) {
6161 case Primitive::kPrimBoolean:
6162 case Primitive::kPrimByte:
6163 case Primitive::kPrimShort:
6164 case Primitive::kPrimChar:
6165 case Primitive::kPrimInt: {
6166 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006167 int32_t const_index = Int32ConstantFrom(index);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006168 uint32_t full_offset =
6169 data_offset + (const_index << Primitive::ComponentSizeShift(value_type));
6170 StoreOperandType store_type = GetStoreOperandType(value_type);
6171 GetAssembler()->StoreToOffset(store_type, RegisterFrom(value_loc), array, full_offset);
6172 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006173 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006174 vixl32::Register temp = temps.Acquire();
6175
6176 if (has_intermediate_address) {
Artem Serov2bbc9532016-10-21 11:51:50 +01006177 // We do not need to compute the intermediate address from the array: the
6178 // input instruction has done it already. See the comment in
6179 // `TryExtractArrayAccessAddress()`.
6180 if (kIsDebugBuild) {
6181 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
Anton Kirilov644032c2016-12-06 17:51:43 +00006182 DCHECK_EQ(Uint64ConstantFrom(tmp->GetOffset()), data_offset);
Artem Serov2bbc9532016-10-21 11:51:50 +01006183 }
6184 temp = array;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006185 } else {
6186 __ Add(temp, array, data_offset);
6187 }
6188 codegen_->StoreToShiftedRegOffset(value_type, value_loc, temp, RegisterFrom(index));
6189 }
6190 break;
6191 }
6192
6193 case Primitive::kPrimNot: {
6194 vixl32::Register value = RegisterFrom(value_loc);
6195 // TryExtractArrayAccessAddress optimization is never applied for non-primitive ArraySet.
6196 // See the comment in instruction_simplifier_shared.cc.
6197 DCHECK(!has_intermediate_address);
6198
6199 if (instruction->InputAt(2)->IsNullConstant()) {
6200 // Just setting null.
6201 if (index.IsConstant()) {
6202 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00006203 (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006204 GetAssembler()->StoreToOffset(kStoreWord, value, array, offset);
6205 } else {
6206 DCHECK(index.IsRegister()) << index;
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006207 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006208 vixl32::Register temp = temps.Acquire();
6209 __ Add(temp, array, data_offset);
6210 codegen_->StoreToShiftedRegOffset(value_type, value_loc, temp, RegisterFrom(index));
6211 }
Alexandre Rames374ddf32016-11-04 10:40:49 +00006212 // TODO(VIXL): Use a scope to ensure we record the pc info immediately after the preceding
6213 // store instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01006214 codegen_->MaybeRecordImplicitNullCheck(instruction);
6215 DCHECK(!needs_write_barrier);
6216 DCHECK(!may_need_runtime_call_for_type_check);
6217 break;
6218 }
6219
6220 DCHECK(needs_write_barrier);
6221 Location temp1_loc = locations->GetTemp(0);
6222 vixl32::Register temp1 = RegisterFrom(temp1_loc);
6223 Location temp2_loc = locations->GetTemp(1);
6224 vixl32::Register temp2 = RegisterFrom(temp2_loc);
6225 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6226 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6227 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6228 vixl32::Label done;
Anton Kirilov6f644202017-02-27 18:29:45 +00006229 vixl32::Label* final_label = codegen_->GetFinalLabel(instruction, &done);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006230 SlowPathCodeARMVIXL* slow_path = nullptr;
6231
6232 if (may_need_runtime_call_for_type_check) {
6233 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARMVIXL(instruction);
6234 codegen_->AddSlowPath(slow_path);
6235 if (instruction->GetValueCanBeNull()) {
6236 vixl32::Label non_zero;
xueliang.zhongf51bc622016-11-04 09:23:32 +00006237 __ CompareAndBranchIfNonZero(value, &non_zero);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006238 if (index.IsConstant()) {
6239 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00006240 (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006241 GetAssembler()->StoreToOffset(kStoreWord, value, array, offset);
6242 } else {
6243 DCHECK(index.IsRegister()) << index;
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006244 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006245 vixl32::Register temp = temps.Acquire();
6246 __ Add(temp, array, data_offset);
6247 codegen_->StoreToShiftedRegOffset(value_type, value_loc, temp, RegisterFrom(index));
6248 }
Alexandre Rames374ddf32016-11-04 10:40:49 +00006249 // TODO(VIXL): Use a scope to ensure we record the pc info immediately after the preceding
6250 // store instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01006251 codegen_->MaybeRecordImplicitNullCheck(instruction);
Anton Kirilov6f644202017-02-27 18:29:45 +00006252 __ B(final_label);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006253 __ Bind(&non_zero);
6254 }
6255
6256 // Note that when read barriers are enabled, the type checks
6257 // are performed without read barriers. This is fine, even in
6258 // the case where a class object is in the from-space after
6259 // the flip, as a comparison involving such a type would not
6260 // produce a false positive; it may of course produce a false
6261 // negative, in which case we would take the ArraySet slow
6262 // path.
6263
Alexandre Rames374ddf32016-11-04 10:40:49 +00006264 {
6265 // Ensure we record the pc position immediately after the `ldr` instruction.
Artem Serov0fb37192016-12-06 18:13:40 +00006266 ExactAssemblyScope aas(GetVIXLAssembler(),
6267 vixl32::kMaxInstructionSizeInBytes,
6268 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00006269 // /* HeapReference<Class> */ temp1 = array->klass_
6270 __ ldr(temp1, MemOperand(array, class_offset));
6271 codegen_->MaybeRecordImplicitNullCheck(instruction);
6272 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006273 GetAssembler()->MaybeUnpoisonHeapReference(temp1);
6274
6275 // /* HeapReference<Class> */ temp1 = temp1->component_type_
6276 GetAssembler()->LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
6277 // /* HeapReference<Class> */ temp2 = value->klass_
6278 GetAssembler()->LoadFromOffset(kLoadWord, temp2, value, class_offset);
6279 // If heap poisoning is enabled, no need to unpoison `temp1`
6280 // nor `temp2`, as we are comparing two poisoned references.
6281 __ Cmp(temp1, temp2);
6282
6283 if (instruction->StaticTypeOfArrayIsObjectArray()) {
6284 vixl32::Label do_put;
Artem Serov517d9f62016-12-12 15:51:15 +00006285 __ B(eq, &do_put, /* far_target */ false);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006286 // If heap poisoning is enabled, the `temp1` reference has
6287 // not been unpoisoned yet; unpoison it now.
6288 GetAssembler()->MaybeUnpoisonHeapReference(temp1);
6289
6290 // /* HeapReference<Class> */ temp1 = temp1->super_class_
6291 GetAssembler()->LoadFromOffset(kLoadWord, temp1, temp1, super_offset);
6292 // If heap poisoning is enabled, no need to unpoison
6293 // `temp1`, as we are comparing against null below.
xueliang.zhongf51bc622016-11-04 09:23:32 +00006294 __ CompareAndBranchIfNonZero(temp1, slow_path->GetEntryLabel());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006295 __ Bind(&do_put);
6296 } else {
6297 __ B(ne, slow_path->GetEntryLabel());
6298 }
6299 }
6300
6301 vixl32::Register source = value;
6302 if (kPoisonHeapReferences) {
6303 // Note that in the case where `value` is a null reference,
6304 // we do not enter this block, as a null reference does not
6305 // need poisoning.
6306 DCHECK_EQ(value_type, Primitive::kPrimNot);
6307 __ Mov(temp1, value);
6308 GetAssembler()->PoisonHeapReference(temp1);
6309 source = temp1;
6310 }
6311
6312 if (index.IsConstant()) {
6313 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00006314 (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006315 GetAssembler()->StoreToOffset(kStoreWord, source, array, offset);
6316 } else {
6317 DCHECK(index.IsRegister()) << index;
6318
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006319 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006320 vixl32::Register temp = temps.Acquire();
6321 __ Add(temp, array, data_offset);
6322 codegen_->StoreToShiftedRegOffset(value_type,
6323 LocationFrom(source),
6324 temp,
6325 RegisterFrom(index));
6326 }
6327
6328 if (!may_need_runtime_call_for_type_check) {
Alexandre Rames374ddf32016-11-04 10:40:49 +00006329 // TODO(VIXL): Ensure we record the pc position immediately after the preceding store
6330 // instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01006331 codegen_->MaybeRecordImplicitNullCheck(instruction);
6332 }
6333
6334 codegen_->MarkGCCard(temp1, temp2, array, value, instruction->GetValueCanBeNull());
6335
6336 if (done.IsReferenced()) {
6337 __ Bind(&done);
6338 }
6339
6340 if (slow_path != nullptr) {
6341 __ Bind(slow_path->GetExitLabel());
6342 }
6343
6344 break;
6345 }
6346
6347 case Primitive::kPrimLong: {
6348 Location value = locations->InAt(2);
6349 if (index.IsConstant()) {
6350 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00006351 (Int32ConstantFrom(index) << TIMES_8) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006352 GetAssembler()->StoreToOffset(kStoreWordPair, LowRegisterFrom(value), array, offset);
6353 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006354 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006355 vixl32::Register temp = temps.Acquire();
6356 __ Add(temp, array, Operand(RegisterFrom(index), vixl32::LSL, TIMES_8));
6357 GetAssembler()->StoreToOffset(kStoreWordPair, LowRegisterFrom(value), temp, data_offset);
6358 }
6359 break;
6360 }
6361
6362 case Primitive::kPrimFloat: {
6363 Location value = locations->InAt(2);
6364 DCHECK(value.IsFpuRegister());
6365 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006366 size_t offset = (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006367 GetAssembler()->StoreSToOffset(SRegisterFrom(value), array, offset);
6368 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006369 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006370 vixl32::Register temp = temps.Acquire();
6371 __ Add(temp, array, Operand(RegisterFrom(index), vixl32::LSL, TIMES_4));
6372 GetAssembler()->StoreSToOffset(SRegisterFrom(value), temp, data_offset);
6373 }
6374 break;
6375 }
6376
6377 case Primitive::kPrimDouble: {
6378 Location value = locations->InAt(2);
6379 DCHECK(value.IsFpuRegisterPair());
6380 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006381 size_t offset = (Int32ConstantFrom(index) << TIMES_8) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006382 GetAssembler()->StoreDToOffset(DRegisterFrom(value), array, offset);
6383 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006384 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006385 vixl32::Register temp = temps.Acquire();
6386 __ Add(temp, array, Operand(RegisterFrom(index), vixl32::LSL, TIMES_8));
6387 GetAssembler()->StoreDToOffset(DRegisterFrom(value), temp, data_offset);
6388 }
6389 break;
6390 }
6391
6392 case Primitive::kPrimVoid:
6393 LOG(FATAL) << "Unreachable type " << value_type;
6394 UNREACHABLE();
6395 }
6396
6397 // Objects are handled in the switch.
6398 if (value_type != Primitive::kPrimNot) {
Alexandre Rames374ddf32016-11-04 10:40:49 +00006399 // TODO(VIXL): Ensure we record the pc position immediately after the preceding store
6400 // instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01006401 codegen_->MaybeRecordImplicitNullCheck(instruction);
6402 }
6403}
6404
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006405void LocationsBuilderARMVIXL::VisitArrayLength(HArrayLength* instruction) {
6406 LocationSummary* locations =
6407 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6408 locations->SetInAt(0, Location::RequiresRegister());
6409 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6410}
6411
6412void InstructionCodeGeneratorARMVIXL::VisitArrayLength(HArrayLength* instruction) {
6413 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
6414 vixl32::Register obj = InputRegisterAt(instruction, 0);
6415 vixl32::Register out = OutputRegister(instruction);
Alexandre Rames374ddf32016-11-04 10:40:49 +00006416 {
Artem Serov0fb37192016-12-06 18:13:40 +00006417 ExactAssemblyScope aas(GetVIXLAssembler(),
6418 vixl32::kMaxInstructionSizeInBytes,
6419 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00006420 __ ldr(out, MemOperand(obj, offset));
6421 codegen_->MaybeRecordImplicitNullCheck(instruction);
6422 }
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006423 // Mask out compression flag from String's array length.
6424 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006425 __ Lsr(out, out, 1u);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006426 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006427}
6428
Artem Serov2bbc9532016-10-21 11:51:50 +01006429void LocationsBuilderARMVIXL::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Artem Serov2bbc9532016-10-21 11:51:50 +01006430 LocationSummary* locations =
6431 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6432
6433 locations->SetInAt(0, Location::RequiresRegister());
6434 locations->SetInAt(1, Location::RegisterOrConstant(instruction->GetOffset()));
6435 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6436}
6437
6438void InstructionCodeGeneratorARMVIXL::VisitIntermediateAddress(HIntermediateAddress* instruction) {
6439 vixl32::Register out = OutputRegister(instruction);
6440 vixl32::Register first = InputRegisterAt(instruction, 0);
6441 Location second = instruction->GetLocations()->InAt(1);
6442
Artem Serov2bbc9532016-10-21 11:51:50 +01006443 if (second.IsRegister()) {
6444 __ Add(out, first, RegisterFrom(second));
6445 } else {
Anton Kirilov644032c2016-12-06 17:51:43 +00006446 __ Add(out, first, Int32ConstantFrom(second));
Artem Serov2bbc9532016-10-21 11:51:50 +01006447 }
6448}
6449
Scott Wakelingc34dba72016-10-03 10:14:44 +01006450void LocationsBuilderARMVIXL::VisitBoundsCheck(HBoundsCheck* instruction) {
6451 RegisterSet caller_saves = RegisterSet::Empty();
6452 InvokeRuntimeCallingConventionARMVIXL calling_convention;
6453 caller_saves.Add(LocationFrom(calling_convention.GetRegisterAt(0)));
6454 caller_saves.Add(LocationFrom(calling_convention.GetRegisterAt(1)));
6455 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Artem Serov2dd053d2017-03-08 14:54:06 +00006456
6457 HInstruction* index = instruction->InputAt(0);
6458 HInstruction* length = instruction->InputAt(1);
6459 // If both index and length are constants we can statically check the bounds. But if at least one
6460 // of them is not encodable ArmEncodableConstantOrRegister will create
6461 // Location::RequiresRegister() which is not desired to happen. Instead we create constant
6462 // locations.
6463 bool both_const = index->IsConstant() && length->IsConstant();
6464 locations->SetInAt(0, both_const
6465 ? Location::ConstantLocation(index->AsConstant())
6466 : ArmEncodableConstantOrRegister(index, CMP));
6467 locations->SetInAt(1, both_const
6468 ? Location::ConstantLocation(length->AsConstant())
6469 : ArmEncodableConstantOrRegister(length, CMP));
Scott Wakelingc34dba72016-10-03 10:14:44 +01006470}
6471
6472void InstructionCodeGeneratorARMVIXL::VisitBoundsCheck(HBoundsCheck* instruction) {
Artem Serov2dd053d2017-03-08 14:54:06 +00006473 LocationSummary* locations = instruction->GetLocations();
6474 Location index_loc = locations->InAt(0);
6475 Location length_loc = locations->InAt(1);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006476
Artem Serov2dd053d2017-03-08 14:54:06 +00006477 if (length_loc.IsConstant()) {
6478 int32_t length = Int32ConstantFrom(length_loc);
6479 if (index_loc.IsConstant()) {
6480 // BCE will remove the bounds check if we are guaranteed to pass.
6481 int32_t index = Int32ConstantFrom(index_loc);
6482 if (index < 0 || index >= length) {
6483 SlowPathCodeARMVIXL* slow_path =
6484 new (GetGraph()->GetArena()) BoundsCheckSlowPathARMVIXL(instruction);
6485 codegen_->AddSlowPath(slow_path);
6486 __ B(slow_path->GetEntryLabel());
6487 } else {
6488 // Some optimization after BCE may have generated this, and we should not
6489 // generate a bounds check if it is a valid range.
6490 }
6491 return;
6492 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006493
Artem Serov2dd053d2017-03-08 14:54:06 +00006494 SlowPathCodeARMVIXL* slow_path =
6495 new (GetGraph()->GetArena()) BoundsCheckSlowPathARMVIXL(instruction);
6496 __ Cmp(RegisterFrom(index_loc), length);
6497 codegen_->AddSlowPath(slow_path);
6498 __ B(hs, slow_path->GetEntryLabel());
6499 } else {
6500 SlowPathCodeARMVIXL* slow_path =
6501 new (GetGraph()->GetArena()) BoundsCheckSlowPathARMVIXL(instruction);
6502 __ Cmp(RegisterFrom(length_loc), InputOperandAt(instruction, 0));
6503 codegen_->AddSlowPath(slow_path);
6504 __ B(ls, slow_path->GetEntryLabel());
6505 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006506}
6507
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006508void CodeGeneratorARMVIXL::MarkGCCard(vixl32::Register temp,
6509 vixl32::Register card,
6510 vixl32::Register object,
6511 vixl32::Register value,
6512 bool can_be_null) {
6513 vixl32::Label is_null;
6514 if (can_be_null) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00006515 __ CompareAndBranchIfZero(value, &is_null);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006516 }
6517 GetAssembler()->LoadFromOffset(
6518 kLoadWord, card, tr, Thread::CardTableOffset<kArmPointerSize>().Int32Value());
Scott Wakelingb77051e2016-11-21 19:46:00 +00006519 __ Lsr(temp, object, Operand::From(gc::accounting::CardTable::kCardShift));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006520 __ Strb(card, MemOperand(card, temp));
6521 if (can_be_null) {
6522 __ Bind(&is_null);
6523 }
6524}
6525
Scott Wakelingfe885462016-09-22 10:24:38 +01006526void LocationsBuilderARMVIXL::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
6527 LOG(FATAL) << "Unreachable";
6528}
6529
6530void InstructionCodeGeneratorARMVIXL::VisitParallelMove(HParallelMove* instruction) {
6531 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
6532}
6533
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006534void LocationsBuilderARMVIXL::VisitSuspendCheck(HSuspendCheck* instruction) {
Artem Serov657022c2016-11-23 14:19:38 +00006535 LocationSummary* locations =
6536 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
6537 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006538}
6539
6540void InstructionCodeGeneratorARMVIXL::VisitSuspendCheck(HSuspendCheck* instruction) {
6541 HBasicBlock* block = instruction->GetBlock();
6542 if (block->GetLoopInformation() != nullptr) {
6543 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
6544 // The back edge will generate the suspend check.
6545 return;
6546 }
6547 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
6548 // The goto will generate the suspend check.
6549 return;
6550 }
6551 GenerateSuspendCheck(instruction, nullptr);
6552}
6553
6554void InstructionCodeGeneratorARMVIXL::GenerateSuspendCheck(HSuspendCheck* instruction,
6555 HBasicBlock* successor) {
6556 SuspendCheckSlowPathARMVIXL* slow_path =
6557 down_cast<SuspendCheckSlowPathARMVIXL*>(instruction->GetSlowPath());
6558 if (slow_path == nullptr) {
6559 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARMVIXL(instruction, successor);
6560 instruction->SetSlowPath(slow_path);
6561 codegen_->AddSlowPath(slow_path);
6562 if (successor != nullptr) {
6563 DCHECK(successor->IsLoopHeader());
6564 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
6565 }
6566 } else {
6567 DCHECK_EQ(slow_path->GetSuccessor(), successor);
6568 }
6569
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006570 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006571 vixl32::Register temp = temps.Acquire();
6572 GetAssembler()->LoadFromOffset(
6573 kLoadUnsignedHalfword, temp, tr, Thread::ThreadFlagsOffset<kArmPointerSize>().Int32Value());
6574 if (successor == nullptr) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00006575 __ CompareAndBranchIfNonZero(temp, slow_path->GetEntryLabel());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006576 __ Bind(slow_path->GetReturnLabel());
6577 } else {
xueliang.zhongf51bc622016-11-04 09:23:32 +00006578 __ CompareAndBranchIfZero(temp, codegen_->GetLabelOf(successor));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006579 __ B(slow_path->GetEntryLabel());
6580 }
6581}
6582
Scott Wakelingfe885462016-09-22 10:24:38 +01006583ArmVIXLAssembler* ParallelMoveResolverARMVIXL::GetAssembler() const {
6584 return codegen_->GetAssembler();
6585}
6586
6587void ParallelMoveResolverARMVIXL::EmitMove(size_t index) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006588 UseScratchRegisterScope temps(GetAssembler()->GetVIXLAssembler());
Scott Wakelingfe885462016-09-22 10:24:38 +01006589 MoveOperands* move = moves_[index];
6590 Location source = move->GetSource();
6591 Location destination = move->GetDestination();
6592
6593 if (source.IsRegister()) {
6594 if (destination.IsRegister()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006595 __ Mov(RegisterFrom(destination), RegisterFrom(source));
Scott Wakelingfe885462016-09-22 10:24:38 +01006596 } else if (destination.IsFpuRegister()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006597 __ Vmov(SRegisterFrom(destination), RegisterFrom(source));
Scott Wakelingfe885462016-09-22 10:24:38 +01006598 } else {
6599 DCHECK(destination.IsStackSlot());
6600 GetAssembler()->StoreToOffset(kStoreWord,
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006601 RegisterFrom(source),
Scott Wakelingfe885462016-09-22 10:24:38 +01006602 sp,
6603 destination.GetStackIndex());
6604 }
6605 } else if (source.IsStackSlot()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006606 if (destination.IsRegister()) {
6607 GetAssembler()->LoadFromOffset(kLoadWord,
6608 RegisterFrom(destination),
6609 sp,
6610 source.GetStackIndex());
6611 } else if (destination.IsFpuRegister()) {
6612 GetAssembler()->LoadSFromOffset(SRegisterFrom(destination), sp, source.GetStackIndex());
6613 } else {
6614 DCHECK(destination.IsStackSlot());
6615 vixl32::Register temp = temps.Acquire();
6616 GetAssembler()->LoadFromOffset(kLoadWord, temp, sp, source.GetStackIndex());
6617 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
6618 }
Scott Wakelingfe885462016-09-22 10:24:38 +01006619 } else if (source.IsFpuRegister()) {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01006620 if (destination.IsRegister()) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006621 __ Vmov(RegisterFrom(destination), SRegisterFrom(source));
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01006622 } else if (destination.IsFpuRegister()) {
6623 __ Vmov(SRegisterFrom(destination), SRegisterFrom(source));
6624 } else {
6625 DCHECK(destination.IsStackSlot());
6626 GetAssembler()->StoreSToOffset(SRegisterFrom(source), sp, destination.GetStackIndex());
6627 }
Scott Wakelingfe885462016-09-22 10:24:38 +01006628 } else if (source.IsDoubleStackSlot()) {
Alexandre Rames9c19bd62016-10-24 11:50:32 +01006629 if (destination.IsDoubleStackSlot()) {
6630 vixl32::DRegister temp = temps.AcquireD();
6631 GetAssembler()->LoadDFromOffset(temp, sp, source.GetStackIndex());
6632 GetAssembler()->StoreDToOffset(temp, sp, destination.GetStackIndex());
6633 } else if (destination.IsRegisterPair()) {
6634 DCHECK(ExpectedPairLayout(destination));
6635 GetAssembler()->LoadFromOffset(
6636 kLoadWordPair, LowRegisterFrom(destination), sp, source.GetStackIndex());
6637 } else {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01006638 DCHECK(destination.IsFpuRegisterPair()) << destination;
6639 GetAssembler()->LoadDFromOffset(DRegisterFrom(destination), sp, source.GetStackIndex());
Alexandre Rames9c19bd62016-10-24 11:50:32 +01006640 }
Scott Wakelingfe885462016-09-22 10:24:38 +01006641 } else if (source.IsRegisterPair()) {
6642 if (destination.IsRegisterPair()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006643 __ Mov(LowRegisterFrom(destination), LowRegisterFrom(source));
6644 __ Mov(HighRegisterFrom(destination), HighRegisterFrom(source));
Scott Wakelingfe885462016-09-22 10:24:38 +01006645 } else if (destination.IsFpuRegisterPair()) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006646 __ Vmov(DRegisterFrom(destination), LowRegisterFrom(source), HighRegisterFrom(source));
Scott Wakelingfe885462016-09-22 10:24:38 +01006647 } else {
6648 DCHECK(destination.IsDoubleStackSlot()) << destination;
6649 DCHECK(ExpectedPairLayout(source));
6650 GetAssembler()->StoreToOffset(kStoreWordPair,
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006651 LowRegisterFrom(source),
Scott Wakelingfe885462016-09-22 10:24:38 +01006652 sp,
6653 destination.GetStackIndex());
6654 }
6655 } else if (source.IsFpuRegisterPair()) {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01006656 if (destination.IsRegisterPair()) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006657 __ Vmov(LowRegisterFrom(destination), HighRegisterFrom(destination), DRegisterFrom(source));
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01006658 } else if (destination.IsFpuRegisterPair()) {
6659 __ Vmov(DRegisterFrom(destination), DRegisterFrom(source));
6660 } else {
6661 DCHECK(destination.IsDoubleStackSlot()) << destination;
6662 GetAssembler()->StoreDToOffset(DRegisterFrom(source), sp, destination.GetStackIndex());
6663 }
Scott Wakelingfe885462016-09-22 10:24:38 +01006664 } else {
6665 DCHECK(source.IsConstant()) << source;
6666 HConstant* constant = source.GetConstant();
6667 if (constant->IsIntConstant() || constant->IsNullConstant()) {
6668 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
6669 if (destination.IsRegister()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006670 __ Mov(RegisterFrom(destination), value);
Scott Wakelingfe885462016-09-22 10:24:38 +01006671 } else {
6672 DCHECK(destination.IsStackSlot());
Scott Wakelingfe885462016-09-22 10:24:38 +01006673 vixl32::Register temp = temps.Acquire();
6674 __ Mov(temp, value);
6675 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
6676 }
6677 } else if (constant->IsLongConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006678 int64_t value = Int64ConstantFrom(source);
Scott Wakelingfe885462016-09-22 10:24:38 +01006679 if (destination.IsRegisterPair()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006680 __ Mov(LowRegisterFrom(destination), Low32Bits(value));
6681 __ Mov(HighRegisterFrom(destination), High32Bits(value));
Scott Wakelingfe885462016-09-22 10:24:38 +01006682 } else {
6683 DCHECK(destination.IsDoubleStackSlot()) << destination;
Scott Wakelingfe885462016-09-22 10:24:38 +01006684 vixl32::Register temp = temps.Acquire();
6685 __ Mov(temp, Low32Bits(value));
6686 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
6687 __ Mov(temp, High32Bits(value));
6688 GetAssembler()->StoreToOffset(kStoreWord,
6689 temp,
6690 sp,
6691 destination.GetHighStackIndex(kArmWordSize));
6692 }
6693 } else if (constant->IsDoubleConstant()) {
6694 double value = constant->AsDoubleConstant()->GetValue();
6695 if (destination.IsFpuRegisterPair()) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006696 __ Vmov(DRegisterFrom(destination), value);
Scott Wakelingfe885462016-09-22 10:24:38 +01006697 } else {
6698 DCHECK(destination.IsDoubleStackSlot()) << destination;
6699 uint64_t int_value = bit_cast<uint64_t, double>(value);
Scott Wakelingfe885462016-09-22 10:24:38 +01006700 vixl32::Register temp = temps.Acquire();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006701 __ Mov(temp, Low32Bits(int_value));
Scott Wakelingfe885462016-09-22 10:24:38 +01006702 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006703 __ Mov(temp, High32Bits(int_value));
Scott Wakelingfe885462016-09-22 10:24:38 +01006704 GetAssembler()->StoreToOffset(kStoreWord,
6705 temp,
6706 sp,
6707 destination.GetHighStackIndex(kArmWordSize));
6708 }
6709 } else {
6710 DCHECK(constant->IsFloatConstant()) << constant->DebugName();
6711 float value = constant->AsFloatConstant()->GetValue();
6712 if (destination.IsFpuRegister()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006713 __ Vmov(SRegisterFrom(destination), value);
Scott Wakelingfe885462016-09-22 10:24:38 +01006714 } else {
6715 DCHECK(destination.IsStackSlot());
Scott Wakelingfe885462016-09-22 10:24:38 +01006716 vixl32::Register temp = temps.Acquire();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006717 __ Mov(temp, bit_cast<int32_t, float>(value));
Scott Wakelingfe885462016-09-22 10:24:38 +01006718 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
6719 }
6720 }
6721 }
6722}
6723
Alexandre Rames9c19bd62016-10-24 11:50:32 +01006724void ParallelMoveResolverARMVIXL::Exchange(vixl32::Register reg, int mem) {
6725 UseScratchRegisterScope temps(GetAssembler()->GetVIXLAssembler());
6726 vixl32::Register temp = temps.Acquire();
6727 __ Mov(temp, reg);
6728 GetAssembler()->LoadFromOffset(kLoadWord, reg, sp, mem);
6729 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, mem);
Scott Wakelingfe885462016-09-22 10:24:38 +01006730}
6731
Alexandre Rames9c19bd62016-10-24 11:50:32 +01006732void ParallelMoveResolverARMVIXL::Exchange(int mem1, int mem2) {
6733 // TODO(VIXL32): Double check the performance of this implementation.
6734 UseScratchRegisterScope temps(GetAssembler()->GetVIXLAssembler());
Nicolas Geoffray13a797b2017-03-15 16:41:31 +00006735 vixl32::Register temp1 = temps.Acquire();
6736 ScratchRegisterScope ensure_scratch(
6737 this, temp1.GetCode(), r0.GetCode(), codegen_->GetNumberOfCoreRegisters());
6738 vixl32::Register temp2(ensure_scratch.GetRegister());
Alexandre Rames9c19bd62016-10-24 11:50:32 +01006739
Nicolas Geoffray13a797b2017-03-15 16:41:31 +00006740 int stack_offset = ensure_scratch.IsSpilled() ? kArmWordSize : 0;
6741 GetAssembler()->LoadFromOffset(kLoadWord, temp1, sp, mem1 + stack_offset);
6742 GetAssembler()->LoadFromOffset(kLoadWord, temp2, sp, mem2 + stack_offset);
6743 GetAssembler()->StoreToOffset(kStoreWord, temp1, sp, mem2 + stack_offset);
6744 GetAssembler()->StoreToOffset(kStoreWord, temp2, sp, mem1 + stack_offset);
Scott Wakelingfe885462016-09-22 10:24:38 +01006745}
6746
Alexandre Rames9c19bd62016-10-24 11:50:32 +01006747void ParallelMoveResolverARMVIXL::EmitSwap(size_t index) {
6748 MoveOperands* move = moves_[index];
6749 Location source = move->GetSource();
6750 Location destination = move->GetDestination();
6751 UseScratchRegisterScope temps(GetAssembler()->GetVIXLAssembler());
6752
6753 if (source.IsRegister() && destination.IsRegister()) {
6754 vixl32::Register temp = temps.Acquire();
6755 DCHECK(!RegisterFrom(source).Is(temp));
6756 DCHECK(!RegisterFrom(destination).Is(temp));
6757 __ Mov(temp, RegisterFrom(destination));
6758 __ Mov(RegisterFrom(destination), RegisterFrom(source));
6759 __ Mov(RegisterFrom(source), temp);
6760 } else if (source.IsRegister() && destination.IsStackSlot()) {
6761 Exchange(RegisterFrom(source), destination.GetStackIndex());
6762 } else if (source.IsStackSlot() && destination.IsRegister()) {
6763 Exchange(RegisterFrom(destination), source.GetStackIndex());
6764 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Anton Kirilovdda43962016-11-21 19:55:20 +00006765 Exchange(source.GetStackIndex(), destination.GetStackIndex());
Alexandre Rames9c19bd62016-10-24 11:50:32 +01006766 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Nicolas Geoffray13a797b2017-03-15 16:41:31 +00006767 vixl32::Register temp = temps.Acquire();
Anton Kirilovdda43962016-11-21 19:55:20 +00006768 __ Vmov(temp, SRegisterFrom(source));
6769 __ Vmov(SRegisterFrom(source), SRegisterFrom(destination));
6770 __ Vmov(SRegisterFrom(destination), temp);
Alexandre Rames9c19bd62016-10-24 11:50:32 +01006771 } else if (source.IsRegisterPair() && destination.IsRegisterPair()) {
6772 vixl32::DRegister temp = temps.AcquireD();
6773 __ Vmov(temp, LowRegisterFrom(source), HighRegisterFrom(source));
6774 __ Mov(LowRegisterFrom(source), LowRegisterFrom(destination));
6775 __ Mov(HighRegisterFrom(source), HighRegisterFrom(destination));
6776 __ Vmov(LowRegisterFrom(destination), HighRegisterFrom(destination), temp);
6777 } else if (source.IsRegisterPair() || destination.IsRegisterPair()) {
6778 vixl32::Register low_reg = LowRegisterFrom(source.IsRegisterPair() ? source : destination);
6779 int mem = source.IsRegisterPair() ? destination.GetStackIndex() : source.GetStackIndex();
6780 DCHECK(ExpectedPairLayout(source.IsRegisterPair() ? source : destination));
6781 vixl32::DRegister temp = temps.AcquireD();
6782 __ Vmov(temp, low_reg, vixl32::Register(low_reg.GetCode() + 1));
6783 GetAssembler()->LoadFromOffset(kLoadWordPair, low_reg, sp, mem);
6784 GetAssembler()->StoreDToOffset(temp, sp, mem);
6785 } else if (source.IsFpuRegisterPair() && destination.IsFpuRegisterPair()) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006786 vixl32::DRegister first = DRegisterFrom(source);
6787 vixl32::DRegister second = DRegisterFrom(destination);
6788 vixl32::DRegister temp = temps.AcquireD();
6789 __ Vmov(temp, first);
6790 __ Vmov(first, second);
6791 __ Vmov(second, temp);
Alexandre Rames9c19bd62016-10-24 11:50:32 +01006792 } else if (source.IsFpuRegisterPair() || destination.IsFpuRegisterPair()) {
Anton Kirilovdda43962016-11-21 19:55:20 +00006793 vixl32::DRegister reg = source.IsFpuRegisterPair()
6794 ? DRegisterFrom(source)
6795 : DRegisterFrom(destination);
6796 int mem = source.IsFpuRegisterPair()
6797 ? destination.GetStackIndex()
6798 : source.GetStackIndex();
6799 vixl32::DRegister temp = temps.AcquireD();
6800 __ Vmov(temp, reg);
6801 GetAssembler()->LoadDFromOffset(reg, sp, mem);
6802 GetAssembler()->StoreDToOffset(temp, sp, mem);
Alexandre Rames9c19bd62016-10-24 11:50:32 +01006803 } else if (source.IsFpuRegister() || destination.IsFpuRegister()) {
Anton Kirilovdda43962016-11-21 19:55:20 +00006804 vixl32::SRegister reg = source.IsFpuRegister()
6805 ? SRegisterFrom(source)
6806 : SRegisterFrom(destination);
6807 int mem = source.IsFpuRegister()
6808 ? destination.GetStackIndex()
6809 : source.GetStackIndex();
6810 vixl32::Register temp = temps.Acquire();
6811 __ Vmov(temp, reg);
6812 GetAssembler()->LoadSFromOffset(reg, sp, mem);
6813 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, mem);
Alexandre Rames9c19bd62016-10-24 11:50:32 +01006814 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
6815 vixl32::DRegister temp1 = temps.AcquireD();
6816 vixl32::DRegister temp2 = temps.AcquireD();
6817 __ Vldr(temp1, MemOperand(sp, source.GetStackIndex()));
6818 __ Vldr(temp2, MemOperand(sp, destination.GetStackIndex()));
6819 __ Vstr(temp1, MemOperand(sp, destination.GetStackIndex()));
6820 __ Vstr(temp2, MemOperand(sp, source.GetStackIndex()));
6821 } else {
6822 LOG(FATAL) << "Unimplemented" << source << " <-> " << destination;
6823 }
Scott Wakelingfe885462016-09-22 10:24:38 +01006824}
6825
Nicolas Geoffray13a797b2017-03-15 16:41:31 +00006826void ParallelMoveResolverARMVIXL::SpillScratch(int reg) {
6827 __ Push(vixl32::Register(reg));
Scott Wakelingfe885462016-09-22 10:24:38 +01006828}
6829
Nicolas Geoffray13a797b2017-03-15 16:41:31 +00006830void ParallelMoveResolverARMVIXL::RestoreScratch(int reg) {
6831 __ Pop(vixl32::Register(reg));
Scott Wakelingfe885462016-09-22 10:24:38 +01006832}
6833
Artem Serov02d37832016-10-25 15:25:33 +01006834HLoadClass::LoadKind CodeGeneratorARMVIXL::GetSupportedLoadClassKind(
Artem Serovd4cc5b22016-11-04 11:19:09 +00006835 HLoadClass::LoadKind desired_class_load_kind) {
6836 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006837 case HLoadClass::LoadKind::kInvalid:
6838 LOG(FATAL) << "UNREACHABLE";
6839 UNREACHABLE();
Artem Serovd4cc5b22016-11-04 11:19:09 +00006840 case HLoadClass::LoadKind::kReferrersClass:
6841 break;
6842 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
Artem Serovc5fcb442016-12-02 19:19:58 +00006843 DCHECK(!GetCompilerOptions().GetCompilePic());
6844 break;
Artem Serovd4cc5b22016-11-04 11:19:09 +00006845 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
6846 DCHECK(GetCompilerOptions().GetCompilePic());
6847 break;
6848 case HLoadClass::LoadKind::kBootImageAddress:
Artem Serovc5fcb442016-12-02 19:19:58 +00006849 break;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006850 case HLoadClass::LoadKind::kBssEntry:
6851 DCHECK(!Runtime::Current()->UseJitCompilation());
6852 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006853 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006854 DCHECK(Runtime::Current()->UseJitCompilation());
Artem Serovc5fcb442016-12-02 19:19:58 +00006855 break;
Artem Serovd4cc5b22016-11-04 11:19:09 +00006856 case HLoadClass::LoadKind::kDexCacheViaMethod:
6857 break;
6858 }
6859 return desired_class_load_kind;
Artem Serov02d37832016-10-25 15:25:33 +01006860}
6861
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006862void LocationsBuilderARMVIXL::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00006863 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
6864 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006865 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00006866 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006867 cls,
6868 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00006869 LocationFrom(r0));
Vladimir Markoea4c1262017-02-06 19:59:33 +00006870 DCHECK(calling_convention.GetRegisterAt(0).Is(r0));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006871 return;
6872 }
Vladimir Marko41559982017-01-06 14:04:23 +00006873 DCHECK(!cls->NeedsAccessCheck());
Scott Wakelingfe885462016-09-22 10:24:38 +01006874
Artem Serovd4cc5b22016-11-04 11:19:09 +00006875 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
6876 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006877 ? LocationSummary::kCallOnSlowPath
6878 : LocationSummary::kNoCall;
6879 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Artem Serovd4cc5b22016-11-04 11:19:09 +00006880 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006881 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Artem Serovd4cc5b22016-11-04 11:19:09 +00006882 }
6883
Vladimir Marko41559982017-01-06 14:04:23 +00006884 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006885 locations->SetInAt(0, Location::RequiresRegister());
6886 }
6887 locations->SetOut(Location::RequiresRegister());
Vladimir Markoea4c1262017-02-06 19:59:33 +00006888 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
6889 if (!kUseReadBarrier || kUseBakerReadBarrier) {
6890 // Rely on the type resolution or initialization and marking to save everything we need.
6891 // Note that IP may be clobbered by saving/restoring the live register (only one thanks
6892 // to the custom calling convention) or by marking, so we request a different temp.
6893 locations->AddTemp(Location::RequiresRegister());
6894 RegisterSet caller_saves = RegisterSet::Empty();
6895 InvokeRuntimeCallingConventionARMVIXL calling_convention;
6896 caller_saves.Add(LocationFrom(calling_convention.GetRegisterAt(0)));
6897 // TODO: Add GetReturnLocation() to the calling convention so that we can DCHECK()
6898 // that the the kPrimNot result register is the same as the first argument register.
6899 locations->SetCustomSlowPathCallerSaves(caller_saves);
6900 } else {
6901 // For non-Baker read barrier we have a temp-clobbering call.
6902 }
6903 }
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01006904 if (kUseBakerReadBarrier && kBakerReadBarrierLinkTimeThunksEnableForGcRoots) {
6905 if (load_kind == HLoadClass::LoadKind::kBssEntry ||
6906 (load_kind == HLoadClass::LoadKind::kReferrersClass &&
6907 !Runtime::Current()->UseJitCompilation())) {
6908 locations->AddTemp(Location::RegisterLocation(kBakerCcEntrypointRegister.GetCode()));
6909 }
6910 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006911}
6912
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006913// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
6914// move.
6915void InstructionCodeGeneratorARMVIXL::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00006916 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
6917 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
6918 codegen_->GenerateLoadClassRuntimeCall(cls);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006919 return;
6920 }
Vladimir Marko41559982017-01-06 14:04:23 +00006921 DCHECK(!cls->NeedsAccessCheck());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006922
Vladimir Marko41559982017-01-06 14:04:23 +00006923 LocationSummary* locations = cls->GetLocations();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006924 Location out_loc = locations->Out();
6925 vixl32::Register out = OutputRegister(cls);
6926
Artem Serovd4cc5b22016-11-04 11:19:09 +00006927 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
6928 ? kWithoutReadBarrier
6929 : kCompilerReadBarrierOption;
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006930 bool generate_null_check = false;
Vladimir Marko41559982017-01-06 14:04:23 +00006931 switch (load_kind) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006932 case HLoadClass::LoadKind::kReferrersClass: {
6933 DCHECK(!cls->CanCallRuntime());
6934 DCHECK(!cls->MustGenerateClinitCheck());
6935 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
6936 vixl32::Register current_method = InputRegisterAt(cls, 0);
6937 GenerateGcRootFieldLoad(cls,
6938 out_loc,
6939 current_method,
Roland Levillain00468f32016-10-27 18:02:48 +01006940 ArtMethod::DeclaringClassOffset().Int32Value(),
Artem Serovd4cc5b22016-11-04 11:19:09 +00006941 read_barrier_option);
6942 break;
6943 }
6944 case HLoadClass::LoadKind::kBootImageLinkTimeAddress: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006945 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Artem Serovc5fcb442016-12-02 19:19:58 +00006946 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
6947 __ Ldr(out, codegen_->DeduplicateBootImageTypeLiteral(cls->GetDexFile(),
6948 cls->GetTypeIndex()));
Artem Serovd4cc5b22016-11-04 11:19:09 +00006949 break;
6950 }
6951 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006952 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Artem Serovd4cc5b22016-11-04 11:19:09 +00006953 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
6954 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
6955 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
6956 codegen_->EmitMovwMovtPlaceholder(labels, out);
6957 break;
6958 }
6959 case HLoadClass::LoadKind::kBootImageAddress: {
Artem Serovc5fcb442016-12-02 19:19:58 +00006960 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006961 uint32_t address = dchecked_integral_cast<uint32_t>(
6962 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
6963 DCHECK_NE(address, 0u);
Artem Serovc5fcb442016-12-02 19:19:58 +00006964 __ Ldr(out, codegen_->DeduplicateBootImageAddressLiteral(address));
Artem Serovd4cc5b22016-11-04 11:19:09 +00006965 break;
6966 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006967 case HLoadClass::LoadKind::kBssEntry: {
Vladimir Markoea4c1262017-02-06 19:59:33 +00006968 vixl32::Register temp = (!kUseReadBarrier || kUseBakerReadBarrier)
6969 ? RegisterFrom(locations->GetTemp(0))
6970 : out;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006971 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
Vladimir Marko1998cd02017-01-13 13:02:58 +00006972 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex());
Vladimir Markoea4c1262017-02-06 19:59:33 +00006973 codegen_->EmitMovwMovtPlaceholder(labels, temp);
6974 GenerateGcRootFieldLoad(cls, out_loc, temp, /* offset */ 0, read_barrier_option);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006975 generate_null_check = true;
6976 break;
6977 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006978 case HLoadClass::LoadKind::kJitTableAddress: {
Artem Serovc5fcb442016-12-02 19:19:58 +00006979 __ Ldr(out, codegen_->DeduplicateJitClassLiteral(cls->GetDexFile(),
6980 cls->GetTypeIndex(),
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006981 cls->GetClass()));
Artem Serovc5fcb442016-12-02 19:19:58 +00006982 // /* GcRoot<mirror::Class> */ out = *out
Vladimir Markoea4c1262017-02-06 19:59:33 +00006983 GenerateGcRootFieldLoad(cls, out_loc, out, /* offset */ 0, read_barrier_option);
Artem Serovd4cc5b22016-11-04 11:19:09 +00006984 break;
6985 }
Vladimir Marko41559982017-01-06 14:04:23 +00006986 case HLoadClass::LoadKind::kDexCacheViaMethod:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006987 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00006988 LOG(FATAL) << "UNREACHABLE";
6989 UNREACHABLE();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006990 }
6991
6992 if (generate_null_check || cls->MustGenerateClinitCheck()) {
6993 DCHECK(cls->CanCallRuntime());
6994 LoadClassSlowPathARMVIXL* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARMVIXL(
6995 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
6996 codegen_->AddSlowPath(slow_path);
6997 if (generate_null_check) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00006998 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006999 }
7000 if (cls->MustGenerateClinitCheck()) {
7001 GenerateClassInitializationCheck(slow_path, out);
7002 } else {
7003 __ Bind(slow_path->GetExitLabel());
7004 }
7005 }
7006}
7007
Artem Serov02d37832016-10-25 15:25:33 +01007008void LocationsBuilderARMVIXL::VisitClinitCheck(HClinitCheck* check) {
7009 LocationSummary* locations =
7010 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
7011 locations->SetInAt(0, Location::RequiresRegister());
7012 if (check->HasUses()) {
7013 locations->SetOut(Location::SameAsFirstInput());
7014 }
7015}
7016
7017void InstructionCodeGeneratorARMVIXL::VisitClinitCheck(HClinitCheck* check) {
7018 // We assume the class is not null.
7019 LoadClassSlowPathARMVIXL* slow_path =
7020 new (GetGraph()->GetArena()) LoadClassSlowPathARMVIXL(check->GetLoadClass(),
7021 check,
7022 check->GetDexPc(),
7023 /* do_clinit */ true);
7024 codegen_->AddSlowPath(slow_path);
7025 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
7026}
7027
7028void InstructionCodeGeneratorARMVIXL::GenerateClassInitializationCheck(
7029 LoadClassSlowPathARMVIXL* slow_path, vixl32::Register class_reg) {
7030 UseScratchRegisterScope temps(GetVIXLAssembler());
7031 vixl32::Register temp = temps.Acquire();
7032 GetAssembler()->LoadFromOffset(kLoadWord,
7033 temp,
7034 class_reg,
7035 mirror::Class::StatusOffset().Int32Value());
7036 __ Cmp(temp, mirror::Class::kStatusInitialized);
7037 __ B(lt, slow_path->GetEntryLabel());
7038 // Even if the initialized flag is set, we may be in a situation where caches are not synced
7039 // properly. Therefore, we do a memory fence.
7040 __ Dmb(ISH);
7041 __ Bind(slow_path->GetExitLabel());
7042}
7043
Artem Serov02d37832016-10-25 15:25:33 +01007044HLoadString::LoadKind CodeGeneratorARMVIXL::GetSupportedLoadStringKind(
Artem Serovd4cc5b22016-11-04 11:19:09 +00007045 HLoadString::LoadKind desired_string_load_kind) {
7046 switch (desired_string_load_kind) {
7047 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
Artem Serovc5fcb442016-12-02 19:19:58 +00007048 DCHECK(!GetCompilerOptions().GetCompilePic());
7049 break;
Artem Serovd4cc5b22016-11-04 11:19:09 +00007050 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
7051 DCHECK(GetCompilerOptions().GetCompilePic());
7052 break;
7053 case HLoadString::LoadKind::kBootImageAddress:
Artem Serovc5fcb442016-12-02 19:19:58 +00007054 break;
Artem Serovd4cc5b22016-11-04 11:19:09 +00007055 case HLoadString::LoadKind::kBssEntry:
7056 DCHECK(!Runtime::Current()->UseJitCompilation());
7057 break;
7058 case HLoadString::LoadKind::kJitTableAddress:
7059 DCHECK(Runtime::Current()->UseJitCompilation());
Artem Serovc5fcb442016-12-02 19:19:58 +00007060 break;
Artem Serovd4cc5b22016-11-04 11:19:09 +00007061 case HLoadString::LoadKind::kDexCacheViaMethod:
7062 break;
7063 }
7064 return desired_string_load_kind;
Artem Serov02d37832016-10-25 15:25:33 +01007065}
7066
7067void LocationsBuilderARMVIXL::VisitLoadString(HLoadString* load) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00007068 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Artem Serov02d37832016-10-25 15:25:33 +01007069 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Artem Serov02d37832016-10-25 15:25:33 +01007070 HLoadString::LoadKind load_kind = load->GetLoadKind();
7071 if (load_kind == HLoadString::LoadKind::kDexCacheViaMethod) {
Artem Serov02d37832016-10-25 15:25:33 +01007072 locations->SetOut(LocationFrom(r0));
7073 } else {
7074 locations->SetOut(Location::RequiresRegister());
Artem Serovd4cc5b22016-11-04 11:19:09 +00007075 if (load_kind == HLoadString::LoadKind::kBssEntry) {
7076 if (!kUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00007077 // Rely on the pResolveString and marking to save everything we need, including temps.
7078 // Note that IP may be clobbered by saving/restoring the live register (only one thanks
7079 // to the custom calling convention) or by marking, so we request a different temp.
Artem Serovd4cc5b22016-11-04 11:19:09 +00007080 locations->AddTemp(Location::RequiresRegister());
7081 RegisterSet caller_saves = RegisterSet::Empty();
7082 InvokeRuntimeCallingConventionARMVIXL calling_convention;
7083 caller_saves.Add(LocationFrom(calling_convention.GetRegisterAt(0)));
7084 // TODO: Add GetReturnLocation() to the calling convention so that we can DCHECK()
7085 // that the the kPrimNot result register is the same as the first argument register.
7086 locations->SetCustomSlowPathCallerSaves(caller_saves);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01007087 if (kUseBakerReadBarrier && kBakerReadBarrierLinkTimeThunksEnableForGcRoots) {
7088 locations->AddTemp(Location::RegisterLocation(kBakerCcEntrypointRegister.GetCode()));
7089 }
Artem Serovd4cc5b22016-11-04 11:19:09 +00007090 } else {
7091 // For non-Baker read barrier we have a temp-clobbering call.
7092 }
7093 }
Artem Serov02d37832016-10-25 15:25:33 +01007094 }
7095}
7096
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007097// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
7098// move.
7099void InstructionCodeGeneratorARMVIXL::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Artem Serovd4cc5b22016-11-04 11:19:09 +00007100 LocationSummary* locations = load->GetLocations();
7101 Location out_loc = locations->Out();
7102 vixl32::Register out = OutputRegister(load);
7103 HLoadString::LoadKind load_kind = load->GetLoadKind();
7104
7105 switch (load_kind) {
7106 case HLoadString::LoadKind::kBootImageLinkTimeAddress: {
Artem Serovc5fcb442016-12-02 19:19:58 +00007107 __ Ldr(out, codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
7108 load->GetStringIndex()));
7109 return; // No dex cache slow path.
Artem Serovd4cc5b22016-11-04 11:19:09 +00007110 }
7111 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
7112 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
7113 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007114 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Artem Serovd4cc5b22016-11-04 11:19:09 +00007115 codegen_->EmitMovwMovtPlaceholder(labels, out);
7116 return; // No dex cache slow path.
7117 }
7118 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007119 uint32_t address = dchecked_integral_cast<uint32_t>(
7120 reinterpret_cast<uintptr_t>(load->GetString().Get()));
7121 DCHECK_NE(address, 0u);
Artem Serovc5fcb442016-12-02 19:19:58 +00007122 __ Ldr(out, codegen_->DeduplicateBootImageAddressLiteral(address));
7123 return; // No dex cache slow path.
Artem Serovd4cc5b22016-11-04 11:19:09 +00007124 }
7125 case HLoadString::LoadKind::kBssEntry: {
7126 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markoea4c1262017-02-06 19:59:33 +00007127 vixl32::Register temp = (!kUseReadBarrier || kUseBakerReadBarrier)
7128 ? RegisterFrom(locations->GetTemp(0))
7129 : out;
Artem Serovd4cc5b22016-11-04 11:19:09 +00007130 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007131 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Artem Serovd4cc5b22016-11-04 11:19:09 +00007132 codegen_->EmitMovwMovtPlaceholder(labels, temp);
7133 GenerateGcRootFieldLoad(load, out_loc, temp, /* offset */ 0, kCompilerReadBarrierOption);
7134 LoadStringSlowPathARMVIXL* slow_path =
7135 new (GetGraph()->GetArena()) LoadStringSlowPathARMVIXL(load);
7136 codegen_->AddSlowPath(slow_path);
7137 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
7138 __ Bind(slow_path->GetExitLabel());
7139 return;
7140 }
7141 case HLoadString::LoadKind::kJitTableAddress: {
Artem Serovc5fcb442016-12-02 19:19:58 +00007142 __ Ldr(out, codegen_->DeduplicateJitStringLiteral(load->GetDexFile(),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007143 load->GetStringIndex(),
7144 load->GetString()));
Artem Serovc5fcb442016-12-02 19:19:58 +00007145 // /* GcRoot<mirror::String> */ out = *out
7146 GenerateGcRootFieldLoad(load, out_loc, out, /* offset */ 0, kCompilerReadBarrierOption);
7147 return;
Artem Serovd4cc5b22016-11-04 11:19:09 +00007148 }
7149 default:
7150 break;
7151 }
Artem Serov02d37832016-10-25 15:25:33 +01007152
7153 // TODO: Re-add the compiler code to do string dex cache lookup again.
7154 DCHECK_EQ(load->GetLoadKind(), HLoadString::LoadKind::kDexCacheViaMethod);
7155 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Andreas Gampe8a0128a2016-11-28 07:38:35 -08007156 __ Mov(calling_convention.GetRegisterAt(0), load->GetStringIndex().index_);
Artem Serov02d37832016-10-25 15:25:33 +01007157 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
7158 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
7159}
7160
7161static int32_t GetExceptionTlsOffset() {
7162 return Thread::ExceptionOffset<kArmPointerSize>().Int32Value();
7163}
7164
7165void LocationsBuilderARMVIXL::VisitLoadException(HLoadException* load) {
7166 LocationSummary* locations =
7167 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
7168 locations->SetOut(Location::RequiresRegister());
7169}
7170
7171void InstructionCodeGeneratorARMVIXL::VisitLoadException(HLoadException* load) {
7172 vixl32::Register out = OutputRegister(load);
7173 GetAssembler()->LoadFromOffset(kLoadWord, out, tr, GetExceptionTlsOffset());
7174}
7175
7176
7177void LocationsBuilderARMVIXL::VisitClearException(HClearException* clear) {
7178 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
7179}
7180
7181void InstructionCodeGeneratorARMVIXL::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
7182 UseScratchRegisterScope temps(GetVIXLAssembler());
7183 vixl32::Register temp = temps.Acquire();
7184 __ Mov(temp, 0);
7185 GetAssembler()->StoreToOffset(kStoreWord, temp, tr, GetExceptionTlsOffset());
7186}
7187
7188void LocationsBuilderARMVIXL::VisitThrow(HThrow* instruction) {
7189 LocationSummary* locations =
7190 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
7191 InvokeRuntimeCallingConventionARMVIXL calling_convention;
7192 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
7193}
7194
7195void InstructionCodeGeneratorARMVIXL::VisitThrow(HThrow* instruction) {
7196 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
7197 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
7198}
7199
Artem Serov657022c2016-11-23 14:19:38 +00007200// Temp is used for read barrier.
7201static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
7202 if (kEmitCompilerReadBarrier &&
7203 (kUseBakerReadBarrier ||
7204 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
7205 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
7206 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
7207 return 1;
7208 }
7209 return 0;
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007210}
7211
Artem Serov657022c2016-11-23 14:19:38 +00007212// Interface case has 3 temps, one for holding the number of interfaces, one for the current
7213// interface pointer, one for loading the current interface.
7214// The other checks have one temp for loading the object's class.
7215static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
7216 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
7217 return 3;
7218 }
7219 return 1 + NumberOfInstanceOfTemps(type_check_kind);
7220}
Artem Serovcfbe9132016-10-14 15:58:56 +01007221
7222void LocationsBuilderARMVIXL::VisitInstanceOf(HInstanceOf* instruction) {
7223 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
7224 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
7225 bool baker_read_barrier_slow_path = false;
7226 switch (type_check_kind) {
7227 case TypeCheckKind::kExactCheck:
7228 case TypeCheckKind::kAbstractClassCheck:
7229 case TypeCheckKind::kClassHierarchyCheck:
7230 case TypeCheckKind::kArrayObjectCheck:
7231 call_kind =
7232 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
7233 baker_read_barrier_slow_path = kUseBakerReadBarrier;
7234 break;
7235 case TypeCheckKind::kArrayCheck:
7236 case TypeCheckKind::kUnresolvedCheck:
7237 case TypeCheckKind::kInterfaceCheck:
7238 call_kind = LocationSummary::kCallOnSlowPath;
7239 break;
7240 }
7241
7242 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
7243 if (baker_read_barrier_slow_path) {
7244 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
7245 }
7246 locations->SetInAt(0, Location::RequiresRegister());
7247 locations->SetInAt(1, Location::RequiresRegister());
7248 // The "out" register is used as a temporary, so it overlaps with the inputs.
7249 // Note that TypeCheckSlowPathARM uses this register too.
7250 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Artem Serov657022c2016-11-23 14:19:38 +00007251 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01007252 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
7253 codegen_->MaybeAddBakerCcEntrypointTempForFields(locations);
7254 }
Artem Serovcfbe9132016-10-14 15:58:56 +01007255}
7256
7257void InstructionCodeGeneratorARMVIXL::VisitInstanceOf(HInstanceOf* instruction) {
7258 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
7259 LocationSummary* locations = instruction->GetLocations();
7260 Location obj_loc = locations->InAt(0);
7261 vixl32::Register obj = InputRegisterAt(instruction, 0);
7262 vixl32::Register cls = InputRegisterAt(instruction, 1);
7263 Location out_loc = locations->Out();
7264 vixl32::Register out = OutputRegister(instruction);
Artem Serov657022c2016-11-23 14:19:38 +00007265 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
7266 DCHECK_LE(num_temps, 1u);
7267 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Artem Serovcfbe9132016-10-14 15:58:56 +01007268 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
7269 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
7270 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
7271 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007272 vixl32::Label done;
7273 vixl32::Label* const final_label = codegen_->GetFinalLabel(instruction, &done);
Artem Serovcfbe9132016-10-14 15:58:56 +01007274 SlowPathCodeARMVIXL* slow_path = nullptr;
7275
7276 // Return 0 if `obj` is null.
7277 // avoid null check if we know obj is not null.
7278 if (instruction->MustDoNullCheck()) {
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007279 DCHECK(!out.Is(obj));
7280 __ Mov(out, 0);
7281 __ CompareAndBranchIfZero(obj, final_label, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007282 }
7283
Artem Serovcfbe9132016-10-14 15:58:56 +01007284 switch (type_check_kind) {
7285 case TypeCheckKind::kExactCheck: {
Mathieu Chartier6beced42016-11-15 15:51:31 -08007286 // /* HeapReference<Class> */ out = obj->klass_
7287 GenerateReferenceLoadTwoRegisters(instruction,
7288 out_loc,
7289 obj_loc,
7290 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00007291 maybe_temp_loc,
7292 kCompilerReadBarrierOption);
Artem Serovcfbe9132016-10-14 15:58:56 +01007293 // Classes must be equal for the instanceof to succeed.
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007294 __ Cmp(out, cls);
7295 // We speculatively set the result to false without changing the condition
7296 // flags, which allows us to avoid some branching later.
7297 __ Mov(LeaveFlags, out, 0);
7298
7299 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
7300 // we check that the output is in a low register, so that a 16-bit MOV
7301 // encoding can be used.
7302 if (out.IsLow()) {
7303 // We use the scope because of the IT block that follows.
7304 ExactAssemblyScope guard(GetVIXLAssembler(),
7305 2 * vixl32::k16BitT32InstructionSizeInBytes,
7306 CodeBufferCheckScope::kExactSize);
7307
7308 __ it(eq);
7309 __ mov(eq, out, 1);
7310 } else {
7311 __ B(ne, final_label, /* far_target */ false);
7312 __ Mov(out, 1);
7313 }
7314
Artem Serovcfbe9132016-10-14 15:58:56 +01007315 break;
7316 }
7317
7318 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier6beced42016-11-15 15:51:31 -08007319 // /* HeapReference<Class> */ out = obj->klass_
7320 GenerateReferenceLoadTwoRegisters(instruction,
7321 out_loc,
7322 obj_loc,
7323 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00007324 maybe_temp_loc,
7325 kCompilerReadBarrierOption);
Artem Serovcfbe9132016-10-14 15:58:56 +01007326 // If the class is abstract, we eagerly fetch the super class of the
7327 // object to avoid doing a comparison we know will fail.
7328 vixl32::Label loop;
7329 __ Bind(&loop);
7330 // /* HeapReference<Class> */ out = out->super_class_
Artem Serov657022c2016-11-23 14:19:38 +00007331 GenerateReferenceLoadOneRegister(instruction,
7332 out_loc,
7333 super_offset,
7334 maybe_temp_loc,
7335 kCompilerReadBarrierOption);
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007336 // If `out` is null, we use it for the result, and jump to the final label.
Anton Kirilov6f644202017-02-27 18:29:45 +00007337 __ CompareAndBranchIfZero(out, final_label, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007338 __ Cmp(out, cls);
Artem Serov517d9f62016-12-12 15:51:15 +00007339 __ B(ne, &loop, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007340 __ Mov(out, 1);
Artem Serovcfbe9132016-10-14 15:58:56 +01007341 break;
7342 }
7343
7344 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier6beced42016-11-15 15:51:31 -08007345 // /* HeapReference<Class> */ out = obj->klass_
7346 GenerateReferenceLoadTwoRegisters(instruction,
7347 out_loc,
7348 obj_loc,
7349 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00007350 maybe_temp_loc,
7351 kCompilerReadBarrierOption);
Artem Serovcfbe9132016-10-14 15:58:56 +01007352 // Walk over the class hierarchy to find a match.
7353 vixl32::Label loop, success;
7354 __ Bind(&loop);
7355 __ Cmp(out, cls);
Artem Serov517d9f62016-12-12 15:51:15 +00007356 __ B(eq, &success, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007357 // /* HeapReference<Class> */ out = out->super_class_
Artem Serov657022c2016-11-23 14:19:38 +00007358 GenerateReferenceLoadOneRegister(instruction,
7359 out_loc,
7360 super_offset,
7361 maybe_temp_loc,
7362 kCompilerReadBarrierOption);
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007363 // This is essentially a null check, but it sets the condition flags to the
7364 // proper value for the code that follows the loop, i.e. not `eq`.
7365 __ Cmp(out, 1);
7366 __ B(hs, &loop, /* far_target */ false);
7367
7368 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
7369 // we check that the output is in a low register, so that a 16-bit MOV
7370 // encoding can be used.
7371 if (out.IsLow()) {
7372 // If `out` is null, we use it for the result, and the condition flags
7373 // have already been set to `ne`, so the IT block that comes afterwards
7374 // (and which handles the successful case) turns into a NOP (instead of
7375 // overwriting `out`).
7376 __ Bind(&success);
7377
7378 // We use the scope because of the IT block that follows.
7379 ExactAssemblyScope guard(GetVIXLAssembler(),
7380 2 * vixl32::k16BitT32InstructionSizeInBytes,
7381 CodeBufferCheckScope::kExactSize);
7382
7383 // There is only one branch to the `success` label (which is bound to this
7384 // IT block), and it has the same condition, `eq`, so in that case the MOV
7385 // is executed.
7386 __ it(eq);
7387 __ mov(eq, out, 1);
7388 } else {
7389 // If `out` is null, we use it for the result, and jump to the final label.
Anton Kirilov6f644202017-02-27 18:29:45 +00007390 __ B(final_label);
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007391 __ Bind(&success);
7392 __ Mov(out, 1);
Artem Serovcfbe9132016-10-14 15:58:56 +01007393 }
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007394
Artem Serovcfbe9132016-10-14 15:58:56 +01007395 break;
7396 }
7397
7398 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier6beced42016-11-15 15:51:31 -08007399 // /* HeapReference<Class> */ out = obj->klass_
7400 GenerateReferenceLoadTwoRegisters(instruction,
7401 out_loc,
7402 obj_loc,
7403 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00007404 maybe_temp_loc,
7405 kCompilerReadBarrierOption);
Artem Serovcfbe9132016-10-14 15:58:56 +01007406 // Do an exact check.
7407 vixl32::Label exact_check;
7408 __ Cmp(out, cls);
Artem Serov517d9f62016-12-12 15:51:15 +00007409 __ B(eq, &exact_check, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007410 // Otherwise, we need to check that the object's class is a non-primitive array.
7411 // /* HeapReference<Class> */ out = out->component_type_
Artem Serov657022c2016-11-23 14:19:38 +00007412 GenerateReferenceLoadOneRegister(instruction,
7413 out_loc,
7414 component_offset,
7415 maybe_temp_loc,
7416 kCompilerReadBarrierOption);
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007417 // If `out` is null, we use it for the result, and jump to the final label.
Anton Kirilov6f644202017-02-27 18:29:45 +00007418 __ CompareAndBranchIfZero(out, final_label, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007419 GetAssembler()->LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
7420 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007421 __ Cmp(out, 0);
7422 // We speculatively set the result to false without changing the condition
7423 // flags, which allows us to avoid some branching later.
7424 __ Mov(LeaveFlags, out, 0);
7425
7426 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
7427 // we check that the output is in a low register, so that a 16-bit MOV
7428 // encoding can be used.
7429 if (out.IsLow()) {
7430 __ Bind(&exact_check);
7431
7432 // We use the scope because of the IT block that follows.
7433 ExactAssemblyScope guard(GetVIXLAssembler(),
7434 2 * vixl32::k16BitT32InstructionSizeInBytes,
7435 CodeBufferCheckScope::kExactSize);
7436
7437 __ it(eq);
7438 __ mov(eq, out, 1);
7439 } else {
7440 __ B(ne, final_label, /* far_target */ false);
7441 __ Bind(&exact_check);
7442 __ Mov(out, 1);
7443 }
7444
Artem Serovcfbe9132016-10-14 15:58:56 +01007445 break;
7446 }
7447
7448 case TypeCheckKind::kArrayCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00007449 // No read barrier since the slow path will retry upon failure.
Mathieu Chartier6beced42016-11-15 15:51:31 -08007450 // /* HeapReference<Class> */ out = obj->klass_
7451 GenerateReferenceLoadTwoRegisters(instruction,
7452 out_loc,
7453 obj_loc,
7454 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00007455 maybe_temp_loc,
7456 kWithoutReadBarrier);
Artem Serovcfbe9132016-10-14 15:58:56 +01007457 __ Cmp(out, cls);
7458 DCHECK(locations->OnlyCallsOnSlowPath());
7459 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARMVIXL(instruction,
7460 /* is_fatal */ false);
7461 codegen_->AddSlowPath(slow_path);
7462 __ B(ne, slow_path->GetEntryLabel());
7463 __ Mov(out, 1);
Artem Serovcfbe9132016-10-14 15:58:56 +01007464 break;
7465 }
7466
7467 case TypeCheckKind::kUnresolvedCheck:
7468 case TypeCheckKind::kInterfaceCheck: {
7469 // Note that we indeed only call on slow path, but we always go
7470 // into the slow path for the unresolved and interface check
7471 // cases.
7472 //
7473 // We cannot directly call the InstanceofNonTrivial runtime
7474 // entry point without resorting to a type checking slow path
7475 // here (i.e. by calling InvokeRuntime directly), as it would
7476 // require to assign fixed registers for the inputs of this
7477 // HInstanceOf instruction (following the runtime calling
7478 // convention), which might be cluttered by the potential first
7479 // read barrier emission at the beginning of this method.
7480 //
7481 // TODO: Introduce a new runtime entry point taking the object
7482 // to test (instead of its class) as argument, and let it deal
7483 // with the read barrier issues. This will let us refactor this
7484 // case of the `switch` code as it was previously (with a direct
7485 // call to the runtime not using a type checking slow path).
7486 // This should also be beneficial for the other cases above.
7487 DCHECK(locations->OnlyCallsOnSlowPath());
7488 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARMVIXL(instruction,
7489 /* is_fatal */ false);
7490 codegen_->AddSlowPath(slow_path);
7491 __ B(slow_path->GetEntryLabel());
Artem Serovcfbe9132016-10-14 15:58:56 +01007492 break;
7493 }
7494 }
7495
Artem Serovcfbe9132016-10-14 15:58:56 +01007496 if (done.IsReferenced()) {
7497 __ Bind(&done);
7498 }
7499
7500 if (slow_path != nullptr) {
7501 __ Bind(slow_path->GetExitLabel());
7502 }
7503}
7504
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007505void LocationsBuilderARMVIXL::VisitCheckCast(HCheckCast* instruction) {
7506 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
7507 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
7508
7509 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
7510 switch (type_check_kind) {
7511 case TypeCheckKind::kExactCheck:
7512 case TypeCheckKind::kAbstractClassCheck:
7513 case TypeCheckKind::kClassHierarchyCheck:
7514 case TypeCheckKind::kArrayObjectCheck:
7515 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
7516 LocationSummary::kCallOnSlowPath :
7517 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
7518 break;
7519 case TypeCheckKind::kArrayCheck:
7520 case TypeCheckKind::kUnresolvedCheck:
7521 case TypeCheckKind::kInterfaceCheck:
7522 call_kind = LocationSummary::kCallOnSlowPath;
7523 break;
7524 }
7525
7526 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
7527 locations->SetInAt(0, Location::RequiresRegister());
7528 locations->SetInAt(1, Location::RequiresRegister());
Artem Serov657022c2016-11-23 14:19:38 +00007529 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007530}
7531
7532void InstructionCodeGeneratorARMVIXL::VisitCheckCast(HCheckCast* instruction) {
7533 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
7534 LocationSummary* locations = instruction->GetLocations();
7535 Location obj_loc = locations->InAt(0);
7536 vixl32::Register obj = InputRegisterAt(instruction, 0);
7537 vixl32::Register cls = InputRegisterAt(instruction, 1);
7538 Location temp_loc = locations->GetTemp(0);
7539 vixl32::Register temp = RegisterFrom(temp_loc);
Artem Serov657022c2016-11-23 14:19:38 +00007540 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
7541 DCHECK_LE(num_temps, 3u);
7542 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
7543 Location maybe_temp3_loc = (num_temps >= 3) ? locations->GetTemp(2) : Location::NoLocation();
7544 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
7545 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
7546 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
7547 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
7548 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
7549 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
7550 const uint32_t object_array_data_offset =
7551 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007552
Artem Serov657022c2016-11-23 14:19:38 +00007553 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
7554 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
7555 // read barriers is done for performance and code size reasons.
7556 bool is_type_check_slow_path_fatal = false;
7557 if (!kEmitCompilerReadBarrier) {
7558 is_type_check_slow_path_fatal =
7559 (type_check_kind == TypeCheckKind::kExactCheck ||
7560 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
7561 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
7562 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
7563 !instruction->CanThrowIntoCatchBlock();
7564 }
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007565 SlowPathCodeARMVIXL* type_check_slow_path =
7566 new (GetGraph()->GetArena()) TypeCheckSlowPathARMVIXL(instruction,
7567 is_type_check_slow_path_fatal);
7568 codegen_->AddSlowPath(type_check_slow_path);
7569
7570 vixl32::Label done;
Anton Kirilov6f644202017-02-27 18:29:45 +00007571 vixl32::Label* final_label = codegen_->GetFinalLabel(instruction, &done);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007572 // Avoid null check if we know obj is not null.
7573 if (instruction->MustDoNullCheck()) {
Anton Kirilov6f644202017-02-27 18:29:45 +00007574 __ CompareAndBranchIfZero(obj, final_label, /* far_target */ false);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007575 }
7576
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007577 switch (type_check_kind) {
7578 case TypeCheckKind::kExactCheck:
7579 case TypeCheckKind::kArrayCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00007580 // /* HeapReference<Class> */ temp = obj->klass_
7581 GenerateReferenceLoadTwoRegisters(instruction,
7582 temp_loc,
7583 obj_loc,
7584 class_offset,
7585 maybe_temp2_loc,
7586 kWithoutReadBarrier);
7587
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007588 __ Cmp(temp, cls);
7589 // Jump to slow path for throwing the exception or doing a
7590 // more involved array check.
7591 __ B(ne, type_check_slow_path->GetEntryLabel());
7592 break;
7593 }
7594
7595 case TypeCheckKind::kAbstractClassCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00007596 // /* HeapReference<Class> */ temp = obj->klass_
7597 GenerateReferenceLoadTwoRegisters(instruction,
7598 temp_loc,
7599 obj_loc,
7600 class_offset,
7601 maybe_temp2_loc,
7602 kWithoutReadBarrier);
7603
Artem Serovcfbe9132016-10-14 15:58:56 +01007604 // If the class is abstract, we eagerly fetch the super class of the
7605 // object to avoid doing a comparison we know will fail.
7606 vixl32::Label loop;
7607 __ Bind(&loop);
7608 // /* HeapReference<Class> */ temp = temp->super_class_
Artem Serov657022c2016-11-23 14:19:38 +00007609 GenerateReferenceLoadOneRegister(instruction,
7610 temp_loc,
7611 super_offset,
7612 maybe_temp2_loc,
7613 kWithoutReadBarrier);
Artem Serovcfbe9132016-10-14 15:58:56 +01007614
7615 // If the class reference currently in `temp` is null, jump to the slow path to throw the
7616 // exception.
xueliang.zhongf51bc622016-11-04 09:23:32 +00007617 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
Artem Serovcfbe9132016-10-14 15:58:56 +01007618
7619 // Otherwise, compare the classes.
7620 __ Cmp(temp, cls);
Artem Serov517d9f62016-12-12 15:51:15 +00007621 __ B(ne, &loop, /* far_target */ false);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007622 break;
7623 }
7624
7625 case TypeCheckKind::kClassHierarchyCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00007626 // /* HeapReference<Class> */ temp = obj->klass_
7627 GenerateReferenceLoadTwoRegisters(instruction,
7628 temp_loc,
7629 obj_loc,
7630 class_offset,
7631 maybe_temp2_loc,
7632 kWithoutReadBarrier);
7633
Artem Serovcfbe9132016-10-14 15:58:56 +01007634 // Walk over the class hierarchy to find a match.
7635 vixl32::Label loop;
7636 __ Bind(&loop);
7637 __ Cmp(temp, cls);
Anton Kirilov6f644202017-02-27 18:29:45 +00007638 __ B(eq, final_label, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007639
7640 // /* HeapReference<Class> */ temp = temp->super_class_
Artem Serov657022c2016-11-23 14:19:38 +00007641 GenerateReferenceLoadOneRegister(instruction,
7642 temp_loc,
7643 super_offset,
7644 maybe_temp2_loc,
7645 kWithoutReadBarrier);
Artem Serovcfbe9132016-10-14 15:58:56 +01007646
7647 // If the class reference currently in `temp` is null, jump to the slow path to throw the
7648 // exception.
xueliang.zhongf51bc622016-11-04 09:23:32 +00007649 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
Artem Serovcfbe9132016-10-14 15:58:56 +01007650 // Otherwise, jump to the beginning of the loop.
7651 __ B(&loop);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007652 break;
7653 }
7654
Artem Serovcfbe9132016-10-14 15:58:56 +01007655 case TypeCheckKind::kArrayObjectCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00007656 // /* HeapReference<Class> */ temp = obj->klass_
7657 GenerateReferenceLoadTwoRegisters(instruction,
7658 temp_loc,
7659 obj_loc,
7660 class_offset,
7661 maybe_temp2_loc,
7662 kWithoutReadBarrier);
7663
Artem Serovcfbe9132016-10-14 15:58:56 +01007664 // Do an exact check.
7665 __ Cmp(temp, cls);
Anton Kirilov6f644202017-02-27 18:29:45 +00007666 __ B(eq, final_label, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007667
7668 // Otherwise, we need to check that the object's class is a non-primitive array.
7669 // /* HeapReference<Class> */ temp = temp->component_type_
Artem Serov657022c2016-11-23 14:19:38 +00007670 GenerateReferenceLoadOneRegister(instruction,
7671 temp_loc,
7672 component_offset,
7673 maybe_temp2_loc,
7674 kWithoutReadBarrier);
Artem Serovcfbe9132016-10-14 15:58:56 +01007675 // If the component type is null, jump to the slow path to throw the exception.
xueliang.zhongf51bc622016-11-04 09:23:32 +00007676 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
Artem Serovcfbe9132016-10-14 15:58:56 +01007677 // Otherwise,the object is indeed an array, jump to label `check_non_primitive_component_type`
7678 // to further check that this component type is not a primitive type.
7679 GetAssembler()->LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
7680 static_assert(Primitive::kPrimNot == 0, "Expected 0 for art::Primitive::kPrimNot");
xueliang.zhongf51bc622016-11-04 09:23:32 +00007681 __ CompareAndBranchIfNonZero(temp, type_check_slow_path->GetEntryLabel());
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007682 break;
7683 }
7684
7685 case TypeCheckKind::kUnresolvedCheck:
Artem Serov657022c2016-11-23 14:19:38 +00007686 // We always go into the type check slow path for the unresolved check case.
Artem Serovcfbe9132016-10-14 15:58:56 +01007687 // We cannot directly call the CheckCast runtime entry point
7688 // without resorting to a type checking slow path here (i.e. by
7689 // calling InvokeRuntime directly), as it would require to
7690 // assign fixed registers for the inputs of this HInstanceOf
7691 // instruction (following the runtime calling convention), which
7692 // might be cluttered by the potential first read barrier
7693 // emission at the beginning of this method.
Artem Serov657022c2016-11-23 14:19:38 +00007694
Artem Serovcfbe9132016-10-14 15:58:56 +01007695 __ B(type_check_slow_path->GetEntryLabel());
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007696 break;
Artem Serov657022c2016-11-23 14:19:38 +00007697
7698 case TypeCheckKind::kInterfaceCheck: {
7699 // Avoid read barriers to improve performance of the fast path. We can not get false
7700 // positives by doing this.
7701 // /* HeapReference<Class> */ temp = obj->klass_
7702 GenerateReferenceLoadTwoRegisters(instruction,
7703 temp_loc,
7704 obj_loc,
7705 class_offset,
7706 maybe_temp2_loc,
7707 kWithoutReadBarrier);
7708
7709 // /* HeapReference<Class> */ temp = temp->iftable_
7710 GenerateReferenceLoadTwoRegisters(instruction,
7711 temp_loc,
7712 temp_loc,
7713 iftable_offset,
7714 maybe_temp2_loc,
7715 kWithoutReadBarrier);
7716 // Iftable is never null.
7717 __ Ldr(RegisterFrom(maybe_temp2_loc), MemOperand(temp, array_length_offset));
7718 // Loop through the iftable and check if any class matches.
7719 vixl32::Label start_loop;
7720 __ Bind(&start_loop);
7721 __ CompareAndBranchIfZero(RegisterFrom(maybe_temp2_loc),
7722 type_check_slow_path->GetEntryLabel());
7723 __ Ldr(RegisterFrom(maybe_temp3_loc), MemOperand(temp, object_array_data_offset));
7724 GetAssembler()->MaybeUnpoisonHeapReference(RegisterFrom(maybe_temp3_loc));
7725 // Go to next interface.
7726 __ Add(temp, temp, Operand::From(2 * kHeapReferenceSize));
7727 __ Sub(RegisterFrom(maybe_temp2_loc), RegisterFrom(maybe_temp2_loc), 2);
7728 // Compare the classes and continue the loop if they do not match.
7729 __ Cmp(cls, RegisterFrom(maybe_temp3_loc));
Artem Serov517d9f62016-12-12 15:51:15 +00007730 __ B(ne, &start_loop, /* far_target */ false);
Artem Serov657022c2016-11-23 14:19:38 +00007731 break;
7732 }
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007733 }
Anton Kirilov6f644202017-02-27 18:29:45 +00007734 if (done.IsReferenced()) {
7735 __ Bind(&done);
7736 }
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007737
7738 __ Bind(type_check_slow_path->GetExitLabel());
7739}
7740
Artem Serov551b28f2016-10-18 19:11:30 +01007741void LocationsBuilderARMVIXL::VisitMonitorOperation(HMonitorOperation* instruction) {
7742 LocationSummary* locations =
7743 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
7744 InvokeRuntimeCallingConventionARMVIXL calling_convention;
7745 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
7746}
7747
7748void InstructionCodeGeneratorARMVIXL::VisitMonitorOperation(HMonitorOperation* instruction) {
7749 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
7750 instruction,
7751 instruction->GetDexPc());
7752 if (instruction->IsEnter()) {
7753 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
7754 } else {
7755 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
7756 }
7757}
7758
Artem Serov02109dd2016-09-23 17:17:54 +01007759void LocationsBuilderARMVIXL::VisitAnd(HAnd* instruction) {
7760 HandleBitwiseOperation(instruction, AND);
7761}
7762
7763void LocationsBuilderARMVIXL::VisitOr(HOr* instruction) {
7764 HandleBitwiseOperation(instruction, ORR);
7765}
7766
7767void LocationsBuilderARMVIXL::VisitXor(HXor* instruction) {
7768 HandleBitwiseOperation(instruction, EOR);
7769}
7770
7771void LocationsBuilderARMVIXL::HandleBitwiseOperation(HBinaryOperation* instruction, Opcode opcode) {
7772 LocationSummary* locations =
7773 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
7774 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
7775 || instruction->GetResultType() == Primitive::kPrimLong);
7776 // Note: GVN reorders commutative operations to have the constant on the right hand side.
7777 locations->SetInAt(0, Location::RequiresRegister());
7778 locations->SetInAt(1, ArmEncodableConstantOrRegister(instruction->InputAt(1), opcode));
7779 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
7780}
7781
7782void InstructionCodeGeneratorARMVIXL::VisitAnd(HAnd* instruction) {
7783 HandleBitwiseOperation(instruction);
7784}
7785
7786void InstructionCodeGeneratorARMVIXL::VisitOr(HOr* instruction) {
7787 HandleBitwiseOperation(instruction);
7788}
7789
7790void InstructionCodeGeneratorARMVIXL::VisitXor(HXor* instruction) {
7791 HandleBitwiseOperation(instruction);
7792}
7793
Artem Serov2bbc9532016-10-21 11:51:50 +01007794void LocationsBuilderARMVIXL::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
7795 LocationSummary* locations =
7796 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
7797 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
7798 || instruction->GetResultType() == Primitive::kPrimLong);
7799
7800 locations->SetInAt(0, Location::RequiresRegister());
7801 locations->SetInAt(1, Location::RequiresRegister());
7802 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
7803}
7804
7805void InstructionCodeGeneratorARMVIXL::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
7806 LocationSummary* locations = instruction->GetLocations();
7807 Location first = locations->InAt(0);
7808 Location second = locations->InAt(1);
7809 Location out = locations->Out();
7810
7811 if (instruction->GetResultType() == Primitive::kPrimInt) {
7812 vixl32::Register first_reg = RegisterFrom(first);
7813 vixl32::Register second_reg = RegisterFrom(second);
7814 vixl32::Register out_reg = RegisterFrom(out);
7815
7816 switch (instruction->GetOpKind()) {
7817 case HInstruction::kAnd:
7818 __ Bic(out_reg, first_reg, second_reg);
7819 break;
7820 case HInstruction::kOr:
7821 __ Orn(out_reg, first_reg, second_reg);
7822 break;
7823 // There is no EON on arm.
7824 case HInstruction::kXor:
7825 default:
7826 LOG(FATAL) << "Unexpected instruction " << instruction->DebugName();
7827 UNREACHABLE();
7828 }
7829 return;
7830
7831 } else {
7832 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
7833 vixl32::Register first_low = LowRegisterFrom(first);
7834 vixl32::Register first_high = HighRegisterFrom(first);
7835 vixl32::Register second_low = LowRegisterFrom(second);
7836 vixl32::Register second_high = HighRegisterFrom(second);
7837 vixl32::Register out_low = LowRegisterFrom(out);
7838 vixl32::Register out_high = HighRegisterFrom(out);
7839
7840 switch (instruction->GetOpKind()) {
7841 case HInstruction::kAnd:
7842 __ Bic(out_low, first_low, second_low);
7843 __ Bic(out_high, first_high, second_high);
7844 break;
7845 case HInstruction::kOr:
7846 __ Orn(out_low, first_low, second_low);
7847 __ Orn(out_high, first_high, second_high);
7848 break;
7849 // There is no EON on arm.
7850 case HInstruction::kXor:
7851 default:
7852 LOG(FATAL) << "Unexpected instruction " << instruction->DebugName();
7853 UNREACHABLE();
7854 }
7855 }
7856}
7857
Anton Kirilov74234da2017-01-13 14:42:47 +00007858void LocationsBuilderARMVIXL::VisitDataProcWithShifterOp(
7859 HDataProcWithShifterOp* instruction) {
7860 DCHECK(instruction->GetType() == Primitive::kPrimInt ||
7861 instruction->GetType() == Primitive::kPrimLong);
7862 LocationSummary* locations =
7863 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
7864 const bool overlap = instruction->GetType() == Primitive::kPrimLong &&
7865 HDataProcWithShifterOp::IsExtensionOp(instruction->GetOpKind());
7866
7867 locations->SetInAt(0, Location::RequiresRegister());
7868 locations->SetInAt(1, Location::RequiresRegister());
7869 locations->SetOut(Location::RequiresRegister(),
7870 overlap ? Location::kOutputOverlap : Location::kNoOutputOverlap);
7871}
7872
7873void InstructionCodeGeneratorARMVIXL::VisitDataProcWithShifterOp(
7874 HDataProcWithShifterOp* instruction) {
7875 const LocationSummary* const locations = instruction->GetLocations();
7876 const HInstruction::InstructionKind kind = instruction->GetInstrKind();
7877 const HDataProcWithShifterOp::OpKind op_kind = instruction->GetOpKind();
7878
7879 if (instruction->GetType() == Primitive::kPrimInt) {
7880 DCHECK(!HDataProcWithShifterOp::IsExtensionOp(op_kind));
7881
7882 const vixl32::Register second = instruction->InputAt(1)->GetType() == Primitive::kPrimLong
7883 ? LowRegisterFrom(locations->InAt(1))
7884 : InputRegisterAt(instruction, 1);
7885
7886 GenerateDataProcInstruction(kind,
7887 OutputRegister(instruction),
7888 InputRegisterAt(instruction, 0),
7889 Operand(second,
7890 ShiftFromOpKind(op_kind),
7891 instruction->GetShiftAmount()),
7892 codegen_);
7893 } else {
7894 DCHECK_EQ(instruction->GetType(), Primitive::kPrimLong);
7895
7896 if (HDataProcWithShifterOp::IsExtensionOp(op_kind)) {
7897 const vixl32::Register second = InputRegisterAt(instruction, 1);
7898
7899 DCHECK(!LowRegisterFrom(locations->Out()).Is(second));
7900 GenerateDataProc(kind,
7901 locations->Out(),
7902 locations->InAt(0),
7903 second,
7904 Operand(second, ShiftType::ASR, 31),
7905 codegen_);
7906 } else {
7907 GenerateLongDataProc(instruction, codegen_);
7908 }
7909 }
7910}
7911
Artem Serov02109dd2016-09-23 17:17:54 +01007912// TODO(VIXL): Remove optimizations in the helper when they are implemented in vixl.
7913void InstructionCodeGeneratorARMVIXL::GenerateAndConst(vixl32::Register out,
7914 vixl32::Register first,
7915 uint32_t value) {
7916 // Optimize special cases for individual halfs of `and-long` (`and` is simplified earlier).
7917 if (value == 0xffffffffu) {
7918 if (!out.Is(first)) {
7919 __ Mov(out, first);
7920 }
7921 return;
7922 }
7923 if (value == 0u) {
7924 __ Mov(out, 0);
7925 return;
7926 }
7927 if (GetAssembler()->ShifterOperandCanHold(AND, value)) {
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00007928 __ And(out, first, value);
7929 } else if (GetAssembler()->ShifterOperandCanHold(BIC, ~value)) {
7930 __ Bic(out, first, ~value);
Artem Serov02109dd2016-09-23 17:17:54 +01007931 } else {
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00007932 DCHECK(IsPowerOfTwo(value + 1));
7933 __ Ubfx(out, first, 0, WhichPowerOf2(value + 1));
Artem Serov02109dd2016-09-23 17:17:54 +01007934 }
7935}
7936
7937// TODO(VIXL): Remove optimizations in the helper when they are implemented in vixl.
7938void InstructionCodeGeneratorARMVIXL::GenerateOrrConst(vixl32::Register out,
7939 vixl32::Register first,
7940 uint32_t value) {
7941 // Optimize special cases for individual halfs of `or-long` (`or` is simplified earlier).
7942 if (value == 0u) {
7943 if (!out.Is(first)) {
7944 __ Mov(out, first);
7945 }
7946 return;
7947 }
7948 if (value == 0xffffffffu) {
7949 __ Mvn(out, 0);
7950 return;
7951 }
7952 if (GetAssembler()->ShifterOperandCanHold(ORR, value)) {
7953 __ Orr(out, first, value);
7954 } else {
7955 DCHECK(GetAssembler()->ShifterOperandCanHold(ORN, ~value));
7956 __ Orn(out, first, ~value);
7957 }
7958}
7959
7960// TODO(VIXL): Remove optimizations in the helper when they are implemented in vixl.
7961void InstructionCodeGeneratorARMVIXL::GenerateEorConst(vixl32::Register out,
7962 vixl32::Register first,
7963 uint32_t value) {
7964 // Optimize special case for individual halfs of `xor-long` (`xor` is simplified earlier).
7965 if (value == 0u) {
7966 if (!out.Is(first)) {
7967 __ Mov(out, first);
7968 }
7969 return;
7970 }
7971 __ Eor(out, first, value);
7972}
7973
Anton Kirilovdda43962016-11-21 19:55:20 +00007974void InstructionCodeGeneratorARMVIXL::GenerateAddLongConst(Location out,
7975 Location first,
7976 uint64_t value) {
7977 vixl32::Register out_low = LowRegisterFrom(out);
7978 vixl32::Register out_high = HighRegisterFrom(out);
7979 vixl32::Register first_low = LowRegisterFrom(first);
7980 vixl32::Register first_high = HighRegisterFrom(first);
7981 uint32_t value_low = Low32Bits(value);
7982 uint32_t value_high = High32Bits(value);
7983 if (value_low == 0u) {
7984 if (!out_low.Is(first_low)) {
7985 __ Mov(out_low, first_low);
7986 }
7987 __ Add(out_high, first_high, value_high);
7988 return;
7989 }
7990 __ Adds(out_low, first_low, value_low);
Scott Wakelingbffdc702016-12-07 17:46:03 +00007991 if (GetAssembler()->ShifterOperandCanHold(ADC, value_high, kCcDontCare)) {
Anton Kirilovdda43962016-11-21 19:55:20 +00007992 __ Adc(out_high, first_high, value_high);
Scott Wakelingbffdc702016-12-07 17:46:03 +00007993 } else if (GetAssembler()->ShifterOperandCanHold(SBC, ~value_high, kCcDontCare)) {
Anton Kirilovdda43962016-11-21 19:55:20 +00007994 __ Sbc(out_high, first_high, ~value_high);
7995 } else {
7996 LOG(FATAL) << "Unexpected constant " << value_high;
7997 UNREACHABLE();
7998 }
7999}
8000
Artem Serov02109dd2016-09-23 17:17:54 +01008001void InstructionCodeGeneratorARMVIXL::HandleBitwiseOperation(HBinaryOperation* instruction) {
8002 LocationSummary* locations = instruction->GetLocations();
8003 Location first = locations->InAt(0);
8004 Location second = locations->InAt(1);
8005 Location out = locations->Out();
8006
8007 if (second.IsConstant()) {
8008 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
8009 uint32_t value_low = Low32Bits(value);
8010 if (instruction->GetResultType() == Primitive::kPrimInt) {
8011 vixl32::Register first_reg = InputRegisterAt(instruction, 0);
8012 vixl32::Register out_reg = OutputRegister(instruction);
8013 if (instruction->IsAnd()) {
8014 GenerateAndConst(out_reg, first_reg, value_low);
8015 } else if (instruction->IsOr()) {
8016 GenerateOrrConst(out_reg, first_reg, value_low);
8017 } else {
8018 DCHECK(instruction->IsXor());
8019 GenerateEorConst(out_reg, first_reg, value_low);
8020 }
8021 } else {
8022 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
8023 uint32_t value_high = High32Bits(value);
8024 vixl32::Register first_low = LowRegisterFrom(first);
8025 vixl32::Register first_high = HighRegisterFrom(first);
8026 vixl32::Register out_low = LowRegisterFrom(out);
8027 vixl32::Register out_high = HighRegisterFrom(out);
8028 if (instruction->IsAnd()) {
8029 GenerateAndConst(out_low, first_low, value_low);
8030 GenerateAndConst(out_high, first_high, value_high);
8031 } else if (instruction->IsOr()) {
8032 GenerateOrrConst(out_low, first_low, value_low);
8033 GenerateOrrConst(out_high, first_high, value_high);
8034 } else {
8035 DCHECK(instruction->IsXor());
8036 GenerateEorConst(out_low, first_low, value_low);
8037 GenerateEorConst(out_high, first_high, value_high);
8038 }
8039 }
8040 return;
8041 }
8042
8043 if (instruction->GetResultType() == Primitive::kPrimInt) {
8044 vixl32::Register first_reg = InputRegisterAt(instruction, 0);
8045 vixl32::Register second_reg = InputRegisterAt(instruction, 1);
8046 vixl32::Register out_reg = OutputRegister(instruction);
8047 if (instruction->IsAnd()) {
8048 __ And(out_reg, first_reg, second_reg);
8049 } else if (instruction->IsOr()) {
8050 __ Orr(out_reg, first_reg, second_reg);
8051 } else {
8052 DCHECK(instruction->IsXor());
8053 __ Eor(out_reg, first_reg, second_reg);
8054 }
8055 } else {
8056 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
8057 vixl32::Register first_low = LowRegisterFrom(first);
8058 vixl32::Register first_high = HighRegisterFrom(first);
8059 vixl32::Register second_low = LowRegisterFrom(second);
8060 vixl32::Register second_high = HighRegisterFrom(second);
8061 vixl32::Register out_low = LowRegisterFrom(out);
8062 vixl32::Register out_high = HighRegisterFrom(out);
8063 if (instruction->IsAnd()) {
8064 __ And(out_low, first_low, second_low);
8065 __ And(out_high, first_high, second_high);
8066 } else if (instruction->IsOr()) {
8067 __ Orr(out_low, first_low, second_low);
8068 __ Orr(out_high, first_high, second_high);
8069 } else {
8070 DCHECK(instruction->IsXor());
8071 __ Eor(out_low, first_low, second_low);
8072 __ Eor(out_high, first_high, second_high);
8073 }
8074 }
8075}
8076
Artem Serovcfbe9132016-10-14 15:58:56 +01008077void InstructionCodeGeneratorARMVIXL::GenerateReferenceLoadOneRegister(
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008078 HInstruction* instruction,
Artem Serovcfbe9132016-10-14 15:58:56 +01008079 Location out,
8080 uint32_t offset,
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008081 Location maybe_temp,
8082 ReadBarrierOption read_barrier_option) {
Artem Serovcfbe9132016-10-14 15:58:56 +01008083 vixl32::Register out_reg = RegisterFrom(out);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008084 if (read_barrier_option == kWithReadBarrier) {
8085 CHECK(kEmitCompilerReadBarrier);
8086 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
8087 if (kUseBakerReadBarrier) {
8088 // Load with fast path based Baker's read barrier.
8089 // /* HeapReference<Object> */ out = *(out + offset)
8090 codegen_->GenerateFieldLoadWithBakerReadBarrier(
8091 instruction, out, out_reg, offset, maybe_temp, /* needs_null_check */ false);
8092 } else {
8093 // Load with slow path based read barrier.
8094 // Save the value of `out` into `maybe_temp` before overwriting it
8095 // in the following move operation, as we will need it for the
8096 // read barrier below.
8097 __ Mov(RegisterFrom(maybe_temp), out_reg);
8098 // /* HeapReference<Object> */ out = *(out + offset)
8099 GetAssembler()->LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
8100 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
8101 }
Artem Serovcfbe9132016-10-14 15:58:56 +01008102 } else {
8103 // Plain load with no read barrier.
8104 // /* HeapReference<Object> */ out = *(out + offset)
8105 GetAssembler()->LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
8106 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
8107 }
8108}
8109
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008110void InstructionCodeGeneratorARMVIXL::GenerateReferenceLoadTwoRegisters(
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008111 HInstruction* instruction,
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008112 Location out,
8113 Location obj,
8114 uint32_t offset,
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008115 Location maybe_temp,
8116 ReadBarrierOption read_barrier_option) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008117 vixl32::Register out_reg = RegisterFrom(out);
8118 vixl32::Register obj_reg = RegisterFrom(obj);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008119 if (read_barrier_option == kWithReadBarrier) {
8120 CHECK(kEmitCompilerReadBarrier);
8121 if (kUseBakerReadBarrier) {
8122 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
8123 // Load with fast path based Baker's read barrier.
8124 // /* HeapReference<Object> */ out = *(obj + offset)
8125 codegen_->GenerateFieldLoadWithBakerReadBarrier(
8126 instruction, out, obj_reg, offset, maybe_temp, /* needs_null_check */ false);
8127 } else {
8128 // Load with slow path based read barrier.
8129 // /* HeapReference<Object> */ out = *(obj + offset)
8130 GetAssembler()->LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
8131 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
8132 }
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008133 } else {
8134 // Plain load with no read barrier.
8135 // /* HeapReference<Object> */ out = *(obj + offset)
8136 GetAssembler()->LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
8137 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
8138 }
8139}
8140
Scott Wakelinga7812ae2016-10-17 10:03:36 +01008141void InstructionCodeGeneratorARMVIXL::GenerateGcRootFieldLoad(
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008142 HInstruction* instruction,
Scott Wakelinga7812ae2016-10-17 10:03:36 +01008143 Location root,
8144 vixl32::Register obj,
8145 uint32_t offset,
Artem Serovd4cc5b22016-11-04 11:19:09 +00008146 ReadBarrierOption read_barrier_option) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01008147 vixl32::Register root_reg = RegisterFrom(root);
Artem Serovd4cc5b22016-11-04 11:19:09 +00008148 if (read_barrier_option == kWithReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008149 DCHECK(kEmitCompilerReadBarrier);
8150 if (kUseBakerReadBarrier) {
8151 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
Roland Levillainba650a42017-03-06 13:52:32 +00008152 // Baker's read barrier are used.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008153 if (kBakerReadBarrierLinkTimeThunksEnableForGcRoots &&
8154 !Runtime::Current()->UseJitCompilation()) {
8155 // Note that we do not actually check the value of `GetIsGcMarking()`
8156 // to decide whether to mark the loaded GC root or not. Instead, we
8157 // load into `temp` (actually kBakerCcEntrypointRegister) the read
8158 // barrier mark introspection entrypoint. If `temp` is null, it means
8159 // that `GetIsGcMarking()` is false, and vice versa.
8160 //
8161 // We use link-time generated thunks for the slow path. That thunk
8162 // checks the reference and jumps to the entrypoint if needed.
8163 //
8164 // temp = Thread::Current()->pReadBarrierMarkIntrospection
8165 // lr = &return_address;
8166 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
8167 // if (temp != nullptr) {
8168 // goto gc_root_thunk<root_reg>(lr)
8169 // }
8170 // return_address:
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008171
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008172 UseScratchRegisterScope temps(GetVIXLAssembler());
8173 ExcludeIPAndBakerCcEntrypointRegister(&temps, instruction);
Vladimir Marko88abba22017-05-03 17:09:25 +01008174 bool narrow = CanEmitNarrowLdr(root_reg, obj, offset);
8175 uint32_t custom_data = linker::Thumb2RelativePatcher::EncodeBakerReadBarrierGcRootData(
8176 root_reg.GetCode(), narrow);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008177 vixl32::Label* bne_label = codegen_->NewBakerReadBarrierPatch(custom_data);
Roland Levillainba650a42017-03-06 13:52:32 +00008178
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008179 // entrypoint_reg =
8180 // Thread::Current()->pReadBarrierMarkReg12, i.e. pReadBarrierMarkIntrospection.
8181 DCHECK_EQ(ip.GetCode(), 12u);
8182 const int32_t entry_point_offset =
8183 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmPointerSize>(ip.GetCode());
8184 __ Ldr(kBakerCcEntrypointRegister, MemOperand(tr, entry_point_offset));
Roland Levillainba650a42017-03-06 13:52:32 +00008185
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008186 vixl::EmissionCheckScope guard(GetVIXLAssembler(),
8187 4 * vixl32::kMaxInstructionSizeInBytes);
8188 vixl32::Label return_address;
8189 EmitAdrCode adr(GetVIXLAssembler(), lr, &return_address);
8190 __ cmp(kBakerCcEntrypointRegister, Operand(0));
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008191 // Currently the offset is always within range. If that changes,
8192 // we shall have to split the load the same way as for fields.
8193 DCHECK_LT(offset, kReferenceLoadMinFarOffset);
Vladimir Marko88abba22017-05-03 17:09:25 +01008194 ptrdiff_t old_offset = GetVIXLAssembler()->GetBuffer()->GetCursorOffset();
8195 __ ldr(EncodingSize(narrow ? Narrow : Wide), root_reg, MemOperand(obj, offset));
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008196 EmitPlaceholderBne(codegen_, bne_label);
8197 __ Bind(&return_address);
Vladimir Marko88abba22017-05-03 17:09:25 +01008198 DCHECK_EQ(old_offset - GetVIXLAssembler()->GetBuffer()->GetCursorOffset(),
8199 narrow ? BAKER_MARK_INTROSPECTION_GC_ROOT_LDR_NARROW_OFFSET
8200 : BAKER_MARK_INTROSPECTION_GC_ROOT_LDR_WIDE_OFFSET);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008201 } else {
8202 // Note that we do not actually check the value of
8203 // `GetIsGcMarking()` to decide whether to mark the loaded GC
8204 // root or not. Instead, we load into `temp` the read barrier
8205 // mark entry point corresponding to register `root`. If `temp`
8206 // is null, it means that `GetIsGcMarking()` is false, and vice
8207 // versa.
8208 //
8209 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
8210 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
8211 // if (temp != nullptr) { // <=> Thread::Current()->GetIsGcMarking()
8212 // // Slow path.
8213 // root = temp(root); // root = ReadBarrier::Mark(root); // Runtime entry point call.
8214 // }
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008215
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008216 // Slow path marking the GC root `root`. The entrypoint will already be loaded in `temp`.
8217 Location temp = LocationFrom(lr);
8218 SlowPathCodeARMVIXL* slow_path =
8219 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARMVIXL(
8220 instruction, root, /* entrypoint */ temp);
8221 codegen_->AddSlowPath(slow_path);
8222
8223 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
8224 const int32_t entry_point_offset =
8225 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmPointerSize>(root.reg());
8226 // Loading the entrypoint does not require a load acquire since it is only changed when
8227 // threads are suspended or running a checkpoint.
8228 GetAssembler()->LoadFromOffset(kLoadWord, RegisterFrom(temp), tr, entry_point_offset);
8229
8230 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
8231 GetAssembler()->LoadFromOffset(kLoadWord, root_reg, obj, offset);
8232 static_assert(
8233 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
8234 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
8235 "have different sizes.");
8236 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
8237 "art::mirror::CompressedReference<mirror::Object> and int32_t "
8238 "have different sizes.");
8239
8240 // The entrypoint is null when the GC is not marking, this prevents one load compared to
8241 // checking GetIsGcMarking.
8242 __ CompareAndBranchIfNonZero(RegisterFrom(temp), slow_path->GetEntryLabel());
8243 __ Bind(slow_path->GetExitLabel());
8244 }
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008245 } else {
8246 // GC root loaded through a slow path for read barriers other
8247 // than Baker's.
8248 // /* GcRoot<mirror::Object>* */ root = obj + offset
8249 __ Add(root_reg, obj, offset);
8250 // /* mirror::Object* */ root = root->Read()
8251 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
8252 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01008253 } else {
8254 // Plain GC root load with no read barrier.
8255 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
8256 GetAssembler()->LoadFromOffset(kLoadWord, root_reg, obj, offset);
8257 // Note that GC roots are not affected by heap poisoning, thus we
8258 // do not have to unpoison `root_reg` here.
8259 }
8260}
8261
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008262void CodeGeneratorARMVIXL::MaybeAddBakerCcEntrypointTempForFields(LocationSummary* locations) {
8263 DCHECK(kEmitCompilerReadBarrier);
8264 DCHECK(kUseBakerReadBarrier);
8265 if (kBakerReadBarrierLinkTimeThunksEnableForFields) {
8266 if (!Runtime::Current()->UseJitCompilation()) {
8267 locations->AddTemp(Location::RegisterLocation(kBakerCcEntrypointRegister.GetCode()));
8268 }
8269 }
8270}
8271
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008272void CodeGeneratorARMVIXL::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
8273 Location ref,
8274 vixl32::Register obj,
8275 uint32_t offset,
8276 Location temp,
8277 bool needs_null_check) {
8278 DCHECK(kEmitCompilerReadBarrier);
8279 DCHECK(kUseBakerReadBarrier);
8280
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008281 if (kBakerReadBarrierLinkTimeThunksEnableForFields &&
8282 !Runtime::Current()->UseJitCompilation()) {
8283 // Note that we do not actually check the value of `GetIsGcMarking()`
8284 // to decide whether to mark the loaded reference or not. Instead, we
8285 // load into `temp` (actually kBakerCcEntrypointRegister) the read
8286 // barrier mark introspection entrypoint. If `temp` is null, it means
8287 // that `GetIsGcMarking()` is false, and vice versa.
8288 //
8289 // We use link-time generated thunks for the slow path. That thunk checks
8290 // the holder and jumps to the entrypoint if needed. If the holder is not
8291 // gray, it creates a fake dependency and returns to the LDR instruction.
8292 //
8293 // temp = Thread::Current()->pReadBarrierMarkIntrospection
8294 // lr = &gray_return_address;
8295 // if (temp != nullptr) {
8296 // goto field_thunk<holder_reg, base_reg>(lr)
8297 // }
8298 // not_gray_return_address:
8299 // // Original reference load. If the offset is too large to fit
8300 // // into LDR, we use an adjusted base register here.
Vladimir Marko88abba22017-05-03 17:09:25 +01008301 // HeapReference<mirror::Object> reference = *(obj+offset);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008302 // gray_return_address:
8303
8304 DCHECK_ALIGNED(offset, sizeof(mirror::HeapReference<mirror::Object>));
Vladimir Marko88abba22017-05-03 17:09:25 +01008305 vixl32::Register ref_reg = RegisterFrom(ref, Primitive::kPrimNot);
8306 bool narrow = CanEmitNarrowLdr(ref_reg, obj, offset);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008307 vixl32::Register base = obj;
8308 if (offset >= kReferenceLoadMinFarOffset) {
8309 base = RegisterFrom(temp);
8310 DCHECK(!base.Is(kBakerCcEntrypointRegister));
8311 static_assert(IsPowerOfTwo(kReferenceLoadMinFarOffset), "Expecting a power of 2.");
8312 __ Add(base, obj, Operand(offset & ~(kReferenceLoadMinFarOffset - 1u)));
8313 offset &= (kReferenceLoadMinFarOffset - 1u);
Vladimir Marko88abba22017-05-03 17:09:25 +01008314 // Use narrow LDR only for small offsets. Generating narrow encoding LDR for the large
8315 // offsets with `(offset & (kReferenceLoadMinFarOffset - 1u)) < 32u` would most likely
8316 // increase the overall code size when taking the generated thunks into account.
8317 DCHECK(!narrow);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008318 }
8319 UseScratchRegisterScope temps(GetVIXLAssembler());
8320 ExcludeIPAndBakerCcEntrypointRegister(&temps, instruction);
8321 uint32_t custom_data = linker::Thumb2RelativePatcher::EncodeBakerReadBarrierFieldData(
Vladimir Marko88abba22017-05-03 17:09:25 +01008322 base.GetCode(), obj.GetCode(), narrow);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008323 vixl32::Label* bne_label = NewBakerReadBarrierPatch(custom_data);
8324
8325 // entrypoint_reg =
8326 // Thread::Current()->pReadBarrierMarkReg12, i.e. pReadBarrierMarkIntrospection.
8327 DCHECK_EQ(ip.GetCode(), 12u);
8328 const int32_t entry_point_offset =
8329 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmPointerSize>(ip.GetCode());
8330 __ Ldr(kBakerCcEntrypointRegister, MemOperand(tr, entry_point_offset));
8331
8332 vixl::EmissionCheckScope guard(
8333 GetVIXLAssembler(),
8334 (kPoisonHeapReferences ? 5u : 4u) * vixl32::kMaxInstructionSizeInBytes);
8335 vixl32::Label return_address;
8336 EmitAdrCode adr(GetVIXLAssembler(), lr, &return_address);
8337 __ cmp(kBakerCcEntrypointRegister, Operand(0));
8338 EmitPlaceholderBne(this, bne_label);
Vladimir Marko88abba22017-05-03 17:09:25 +01008339 ptrdiff_t old_offset = GetVIXLAssembler()->GetBuffer()->GetCursorOffset();
8340 __ ldr(EncodingSize(narrow ? Narrow : Wide), ref_reg, MemOperand(base, offset));
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008341 if (needs_null_check) {
8342 MaybeRecordImplicitNullCheck(instruction);
8343 }
Vladimir Marko88abba22017-05-03 17:09:25 +01008344 // Note: We need a specific width for the unpoisoning NEG.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008345 if (kPoisonHeapReferences) {
Vladimir Marko88abba22017-05-03 17:09:25 +01008346 if (narrow) {
8347 // The only 16-bit encoding is T1 which sets flags outside IT block (i.e. RSBS, not RSB).
8348 __ rsbs(EncodingSize(Narrow), ref_reg, ref_reg, Operand(0));
8349 } else {
8350 __ rsb(EncodingSize(Wide), ref_reg, ref_reg, Operand(0));
8351 }
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008352 }
8353 __ Bind(&return_address);
Vladimir Marko88abba22017-05-03 17:09:25 +01008354 DCHECK_EQ(old_offset - GetVIXLAssembler()->GetBuffer()->GetCursorOffset(),
8355 narrow ? BAKER_MARK_INTROSPECTION_FIELD_LDR_NARROW_OFFSET
8356 : BAKER_MARK_INTROSPECTION_FIELD_LDR_WIDE_OFFSET);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008357 return;
8358 }
8359
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008360 // /* HeapReference<Object> */ ref = *(obj + offset)
8361 Location no_index = Location::NoLocation();
8362 ScaleFactor no_scale_factor = TIMES_1;
8363 GenerateReferenceLoadWithBakerReadBarrier(
8364 instruction, ref, obj, offset, no_index, no_scale_factor, temp, needs_null_check);
Roland Levillain6070e882016-11-03 17:51:58 +00008365}
8366
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008367void CodeGeneratorARMVIXL::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
8368 Location ref,
8369 vixl32::Register obj,
8370 uint32_t data_offset,
8371 Location index,
8372 Location temp,
8373 bool needs_null_check) {
8374 DCHECK(kEmitCompilerReadBarrier);
8375 DCHECK(kUseBakerReadBarrier);
8376
8377 static_assert(
8378 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
8379 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008380 ScaleFactor scale_factor = TIMES_4;
8381
8382 if (kBakerReadBarrierLinkTimeThunksEnableForArrays &&
8383 !Runtime::Current()->UseJitCompilation()) {
8384 // Note that we do not actually check the value of `GetIsGcMarking()`
8385 // to decide whether to mark the loaded reference or not. Instead, we
8386 // load into `temp` (actually kBakerCcEntrypointRegister) the read
8387 // barrier mark introspection entrypoint. If `temp` is null, it means
8388 // that `GetIsGcMarking()` is false, and vice versa.
8389 //
8390 // We use link-time generated thunks for the slow path. That thunk checks
8391 // the holder and jumps to the entrypoint if needed. If the holder is not
8392 // gray, it creates a fake dependency and returns to the LDR instruction.
8393 //
8394 // temp = Thread::Current()->pReadBarrierMarkIntrospection
8395 // lr = &gray_return_address;
8396 // if (temp != nullptr) {
8397 // goto field_thunk<holder_reg, base_reg>(lr)
8398 // }
8399 // not_gray_return_address:
8400 // // Original reference load. If the offset is too large to fit
8401 // // into LDR, we use an adjusted base register here.
Vladimir Marko88abba22017-05-03 17:09:25 +01008402 // HeapReference<mirror::Object> reference = data[index];
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008403 // gray_return_address:
8404
8405 DCHECK(index.IsValid());
8406 vixl32::Register index_reg = RegisterFrom(index, Primitive::kPrimInt);
8407 vixl32::Register ref_reg = RegisterFrom(ref, Primitive::kPrimNot);
8408 vixl32::Register data_reg = RegisterFrom(temp, Primitive::kPrimInt); // Raw pointer.
8409 DCHECK(!data_reg.Is(kBakerCcEntrypointRegister));
8410
8411 UseScratchRegisterScope temps(GetVIXLAssembler());
8412 ExcludeIPAndBakerCcEntrypointRegister(&temps, instruction);
8413 uint32_t custom_data =
8414 linker::Thumb2RelativePatcher::EncodeBakerReadBarrierArrayData(data_reg.GetCode());
8415 vixl32::Label* bne_label = NewBakerReadBarrierPatch(custom_data);
8416
8417 // entrypoint_reg =
8418 // Thread::Current()->pReadBarrierMarkReg16, i.e. pReadBarrierMarkIntrospection.
8419 DCHECK_EQ(ip.GetCode(), 12u);
8420 const int32_t entry_point_offset =
8421 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmPointerSize>(ip.GetCode());
8422 __ Ldr(kBakerCcEntrypointRegister, MemOperand(tr, entry_point_offset));
8423 __ Add(data_reg, obj, Operand(data_offset));
8424
8425 vixl::EmissionCheckScope guard(
8426 GetVIXLAssembler(),
8427 (kPoisonHeapReferences ? 5u : 4u) * vixl32::kMaxInstructionSizeInBytes);
8428 vixl32::Label return_address;
8429 EmitAdrCode adr(GetVIXLAssembler(), lr, &return_address);
8430 __ cmp(kBakerCcEntrypointRegister, Operand(0));
8431 EmitPlaceholderBne(this, bne_label);
Vladimir Marko88abba22017-05-03 17:09:25 +01008432 ptrdiff_t old_offset = GetVIXLAssembler()->GetBuffer()->GetCursorOffset();
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008433 __ ldr(ref_reg, MemOperand(data_reg, index_reg, vixl32::LSL, scale_factor));
8434 DCHECK(!needs_null_check); // The thunk cannot handle the null check.
8435 // Note: We need a Wide NEG for the unpoisoning.
8436 if (kPoisonHeapReferences) {
8437 __ rsb(EncodingSize(Wide), ref_reg, ref_reg, Operand(0));
8438 }
8439 __ Bind(&return_address);
Vladimir Marko88abba22017-05-03 17:09:25 +01008440 DCHECK_EQ(old_offset - GetVIXLAssembler()->GetBuffer()->GetCursorOffset(),
8441 BAKER_MARK_INTROSPECTION_ARRAY_LDR_OFFSET);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008442 return;
8443 }
8444
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008445 // /* HeapReference<Object> */ ref =
8446 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008447 GenerateReferenceLoadWithBakerReadBarrier(
8448 instruction, ref, obj, data_offset, index, scale_factor, temp, needs_null_check);
Roland Levillain6070e882016-11-03 17:51:58 +00008449}
8450
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008451void CodeGeneratorARMVIXL::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
8452 Location ref,
8453 vixl32::Register obj,
8454 uint32_t offset,
8455 Location index,
8456 ScaleFactor scale_factor,
8457 Location temp,
Roland Levillainff487002017-03-07 16:50:01 +00008458 bool needs_null_check) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008459 DCHECK(kEmitCompilerReadBarrier);
8460 DCHECK(kUseBakerReadBarrier);
8461
Roland Levillain54f869e2017-03-06 13:54:11 +00008462 // Query `art::Thread::Current()->GetIsGcMarking()` to decide
8463 // whether we need to enter the slow path to mark the reference.
8464 // Then, in the slow path, check the gray bit in the lock word of
8465 // the reference's holder (`obj`) to decide whether to mark `ref` or
8466 // not.
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008467 //
Roland Levillainba650a42017-03-06 13:52:32 +00008468 // Note that we do not actually check the value of `GetIsGcMarking()`;
Roland Levillainff487002017-03-07 16:50:01 +00008469 // instead, we load into `temp2` the read barrier mark entry point
8470 // corresponding to register `ref`. If `temp2` is null, it means
8471 // that `GetIsGcMarking()` is false, and vice versa.
8472 //
8473 // temp2 = Thread::Current()->pReadBarrierMarkReg ## root.reg()
8474 // if (temp2 != nullptr) { // <=> Thread::Current()->GetIsGcMarking()
8475 // // Slow path.
8476 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
8477 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
8478 // HeapReference<mirror::Object> ref = *src; // Original reference load.
8479 // bool is_gray = (rb_state == ReadBarrier::GrayState());
8480 // if (is_gray) {
8481 // ref = temp2(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
8482 // }
8483 // } else {
8484 // HeapReference<mirror::Object> ref = *src; // Original reference load.
8485 // }
8486
8487 vixl32::Register temp_reg = RegisterFrom(temp);
8488
8489 // Slow path marking the object `ref` when the GC is marking. The
8490 // entrypoint will already be loaded in `temp2`.
8491 Location temp2 = LocationFrom(lr);
8492 SlowPathCodeARMVIXL* slow_path =
8493 new (GetGraph()->GetArena()) LoadReferenceWithBakerReadBarrierSlowPathARMVIXL(
8494 instruction,
8495 ref,
8496 obj,
8497 offset,
8498 index,
8499 scale_factor,
8500 needs_null_check,
8501 temp_reg,
8502 /* entrypoint */ temp2);
8503 AddSlowPath(slow_path);
8504
8505 // temp2 = Thread::Current()->pReadBarrierMarkReg ## ref.reg()
8506 const int32_t entry_point_offset =
8507 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmPointerSize>(ref.reg());
8508 // Loading the entrypoint does not require a load acquire since it is only changed when
8509 // threads are suspended or running a checkpoint.
8510 GetAssembler()->LoadFromOffset(kLoadWord, RegisterFrom(temp2), tr, entry_point_offset);
8511 // The entrypoint is null when the GC is not marking, this prevents one load compared to
8512 // checking GetIsGcMarking.
8513 __ CompareAndBranchIfNonZero(RegisterFrom(temp2), slow_path->GetEntryLabel());
8514 // Fast path: the GC is not marking: just load the reference.
8515 GenerateRawReferenceLoad(instruction, ref, obj, offset, index, scale_factor, needs_null_check);
8516 __ Bind(slow_path->GetExitLabel());
8517}
8518
8519void CodeGeneratorARMVIXL::UpdateReferenceFieldWithBakerReadBarrier(HInstruction* instruction,
8520 Location ref,
8521 vixl32::Register obj,
8522 Location field_offset,
8523 Location temp,
8524 bool needs_null_check,
8525 vixl32::Register temp2) {
8526 DCHECK(kEmitCompilerReadBarrier);
8527 DCHECK(kUseBakerReadBarrier);
8528
8529 // Query `art::Thread::Current()->GetIsGcMarking()` to decide
8530 // whether we need to enter the slow path to update the reference
8531 // field within `obj`. Then, in the slow path, check the gray bit
8532 // in the lock word of the reference's holder (`obj`) to decide
8533 // whether to mark `ref` and update the field or not.
8534 //
8535 // Note that we do not actually check the value of `GetIsGcMarking()`;
Roland Levillainba650a42017-03-06 13:52:32 +00008536 // instead, we load into `temp3` the read barrier mark entry point
8537 // corresponding to register `ref`. If `temp3` is null, it means
8538 // that `GetIsGcMarking()` is false, and vice versa.
8539 //
8540 // temp3 = Thread::Current()->pReadBarrierMarkReg ## root.reg()
Roland Levillainba650a42017-03-06 13:52:32 +00008541 // if (temp3 != nullptr) { // <=> Thread::Current()->GetIsGcMarking()
8542 // // Slow path.
Roland Levillain54f869e2017-03-06 13:54:11 +00008543 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
8544 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
Roland Levillainff487002017-03-07 16:50:01 +00008545 // HeapReference<mirror::Object> ref = *(obj + field_offset); // Reference load.
Roland Levillain54f869e2017-03-06 13:54:11 +00008546 // bool is_gray = (rb_state == ReadBarrier::GrayState());
8547 // if (is_gray) {
Roland Levillainff487002017-03-07 16:50:01 +00008548 // old_ref = ref;
Roland Levillain54f869e2017-03-06 13:54:11 +00008549 // ref = temp3(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
Roland Levillainff487002017-03-07 16:50:01 +00008550 // compareAndSwapObject(obj, field_offset, old_ref, ref);
Roland Levillain54f869e2017-03-06 13:54:11 +00008551 // }
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008552 // }
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008553
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008554 vixl32::Register temp_reg = RegisterFrom(temp);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008555
Roland Levillainff487002017-03-07 16:50:01 +00008556 // Slow path updating the object reference at address `obj + field_offset`
8557 // when the GC is marking. The entrypoint will already be loaded in `temp3`.
Roland Levillainba650a42017-03-06 13:52:32 +00008558 Location temp3 = LocationFrom(lr);
Roland Levillainff487002017-03-07 16:50:01 +00008559 SlowPathCodeARMVIXL* slow_path =
8560 new (GetGraph()->GetArena()) LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARMVIXL(
8561 instruction,
8562 ref,
8563 obj,
8564 /* offset */ 0u,
8565 /* index */ field_offset,
8566 /* scale_factor */ ScaleFactor::TIMES_1,
8567 needs_null_check,
8568 temp_reg,
8569 temp2,
8570 /* entrypoint */ temp3);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008571 AddSlowPath(slow_path);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008572
Roland Levillainba650a42017-03-06 13:52:32 +00008573 // temp3 = Thread::Current()->pReadBarrierMarkReg ## ref.reg()
8574 const int32_t entry_point_offset =
8575 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmPointerSize>(ref.reg());
8576 // Loading the entrypoint does not require a load acquire since it is only changed when
8577 // threads are suspended or running a checkpoint.
8578 GetAssembler()->LoadFromOffset(kLoadWord, RegisterFrom(temp3), tr, entry_point_offset);
Roland Levillainba650a42017-03-06 13:52:32 +00008579 // The entrypoint is null when the GC is not marking, this prevents one load compared to
8580 // checking GetIsGcMarking.
8581 __ CompareAndBranchIfNonZero(RegisterFrom(temp3), slow_path->GetEntryLabel());
Roland Levillainff487002017-03-07 16:50:01 +00008582 // Fast path: the GC is not marking: nothing to do (the field is
8583 // up-to-date, and we don't need to load the reference).
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008584 __ Bind(slow_path->GetExitLabel());
Roland Levillain844e6532016-11-03 16:09:47 +00008585}
Scott Wakelingfe885462016-09-22 10:24:38 +01008586
Roland Levillainba650a42017-03-06 13:52:32 +00008587void CodeGeneratorARMVIXL::GenerateRawReferenceLoad(HInstruction* instruction,
8588 Location ref,
8589 vixl::aarch32::Register obj,
8590 uint32_t offset,
8591 Location index,
8592 ScaleFactor scale_factor,
8593 bool needs_null_check) {
8594 Primitive::Type type = Primitive::kPrimNot;
8595 vixl32::Register ref_reg = RegisterFrom(ref, type);
8596
8597 // If needed, vixl::EmissionCheckScope guards are used to ensure
8598 // that no pools are emitted between the load (macro) instruction
8599 // and MaybeRecordImplicitNullCheck.
8600
Scott Wakelingfe885462016-09-22 10:24:38 +01008601 if (index.IsValid()) {
8602 // Load types involving an "index": ArrayGet,
8603 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
8604 // intrinsics.
Roland Levillainba650a42017-03-06 13:52:32 +00008605 // /* HeapReference<mirror::Object> */ ref = *(obj + offset + (index << scale_factor))
Scott Wakelingfe885462016-09-22 10:24:38 +01008606 if (index.IsConstant()) {
8607 size_t computed_offset =
8608 (Int32ConstantFrom(index) << scale_factor) + offset;
Roland Levillainba650a42017-03-06 13:52:32 +00008609 vixl::EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Scott Wakelingfe885462016-09-22 10:24:38 +01008610 GetAssembler()->LoadFromOffset(kLoadWord, ref_reg, obj, computed_offset);
Roland Levillainba650a42017-03-06 13:52:32 +00008611 if (needs_null_check) {
8612 MaybeRecordImplicitNullCheck(instruction);
8613 }
Scott Wakelingfe885462016-09-22 10:24:38 +01008614 } else {
8615 // Handle the special case of the
8616 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
8617 // intrinsics, which use a register pair as index ("long
8618 // offset"), of which only the low part contains data.
8619 vixl32::Register index_reg = index.IsRegisterPair()
8620 ? LowRegisterFrom(index)
8621 : RegisterFrom(index);
8622 UseScratchRegisterScope temps(GetVIXLAssembler());
Roland Levillainba650a42017-03-06 13:52:32 +00008623 vixl32::Register temp = temps.Acquire();
8624 __ Add(temp, obj, Operand(index_reg, ShiftType::LSL, scale_factor));
8625 {
8626 vixl::EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
8627 GetAssembler()->LoadFromOffset(kLoadWord, ref_reg, temp, offset);
8628 if (needs_null_check) {
8629 MaybeRecordImplicitNullCheck(instruction);
8630 }
8631 }
Scott Wakelingfe885462016-09-22 10:24:38 +01008632 }
8633 } else {
Roland Levillainba650a42017-03-06 13:52:32 +00008634 // /* HeapReference<mirror::Object> */ ref = *(obj + offset)
8635 vixl::EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Scott Wakelingfe885462016-09-22 10:24:38 +01008636 GetAssembler()->LoadFromOffset(kLoadWord, ref_reg, obj, offset);
Roland Levillainba650a42017-03-06 13:52:32 +00008637 if (needs_null_check) {
8638 MaybeRecordImplicitNullCheck(instruction);
8639 }
Scott Wakelingfe885462016-09-22 10:24:38 +01008640 }
8641
Roland Levillain844e6532016-11-03 16:09:47 +00008642 // Object* ref = ref_addr->AsMirrorPtr()
8643 GetAssembler()->MaybeUnpoisonHeapReference(ref_reg);
Roland Levillain844e6532016-11-03 16:09:47 +00008644}
8645
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008646void CodeGeneratorARMVIXL::GenerateReadBarrierSlow(HInstruction* instruction,
8647 Location out,
8648 Location ref,
8649 Location obj,
8650 uint32_t offset,
8651 Location index) {
8652 DCHECK(kEmitCompilerReadBarrier);
8653
8654 // Insert a slow path based read barrier *after* the reference load.
8655 //
8656 // If heap poisoning is enabled, the unpoisoning of the loaded
8657 // reference will be carried out by the runtime within the slow
8658 // path.
8659 //
8660 // Note that `ref` currently does not get unpoisoned (when heap
8661 // poisoning is enabled), which is alright as the `ref` argument is
8662 // not used by the artReadBarrierSlow entry point.
8663 //
8664 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
8665 SlowPathCodeARMVIXL* slow_path = new (GetGraph()->GetArena())
8666 ReadBarrierForHeapReferenceSlowPathARMVIXL(instruction, out, ref, obj, offset, index);
8667 AddSlowPath(slow_path);
8668
8669 __ B(slow_path->GetEntryLabel());
8670 __ Bind(slow_path->GetExitLabel());
8671}
8672
8673void CodeGeneratorARMVIXL::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
Artem Serov02d37832016-10-25 15:25:33 +01008674 Location out,
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008675 Location ref,
8676 Location obj,
8677 uint32_t offset,
8678 Location index) {
Artem Serov02d37832016-10-25 15:25:33 +01008679 if (kEmitCompilerReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008680 // Baker's read barriers shall be handled by the fast path
8681 // (CodeGeneratorARM::GenerateReferenceLoadWithBakerReadBarrier).
Artem Serov02d37832016-10-25 15:25:33 +01008682 DCHECK(!kUseBakerReadBarrier);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008683 // If heap poisoning is enabled, unpoisoning will be taken care of
8684 // by the runtime within the slow path.
8685 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Artem Serov02d37832016-10-25 15:25:33 +01008686 } else if (kPoisonHeapReferences) {
8687 GetAssembler()->UnpoisonHeapReference(RegisterFrom(out));
8688 }
8689}
8690
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008691void CodeGeneratorARMVIXL::GenerateReadBarrierForRootSlow(HInstruction* instruction,
8692 Location out,
8693 Location root) {
8694 DCHECK(kEmitCompilerReadBarrier);
8695
8696 // Insert a slow path based read barrier *after* the GC root load.
8697 //
8698 // Note that GC roots are not affected by heap poisoning, so we do
8699 // not need to do anything special for this here.
8700 SlowPathCodeARMVIXL* slow_path =
8701 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathARMVIXL(instruction, out, root);
8702 AddSlowPath(slow_path);
8703
8704 __ B(slow_path->GetEntryLabel());
8705 __ Bind(slow_path->GetExitLabel());
8706}
8707
Artem Serov02d37832016-10-25 15:25:33 +01008708// Check if the desired_dispatch_info is supported. If it is, return it,
8709// otherwise return a fall-back info that should be used instead.
8710HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARMVIXL::GetSupportedInvokeStaticOrDirectDispatch(
Artem Serovd4cc5b22016-11-04 11:19:09 +00008711 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +00008712 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Nicolas Geoffraye807ff72017-01-23 09:03:12 +00008713 return desired_dispatch_info;
Artem Serov02d37832016-10-25 15:25:33 +01008714}
8715
Scott Wakelingfe885462016-09-22 10:24:38 +01008716vixl32::Register CodeGeneratorARMVIXL::GetInvokeStaticOrDirectExtraParameter(
8717 HInvokeStaticOrDirect* invoke, vixl32::Register temp) {
8718 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
8719 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
8720 if (!invoke->GetLocations()->Intrinsified()) {
8721 return RegisterFrom(location);
8722 }
8723 // For intrinsics we allow any location, so it may be on the stack.
8724 if (!location.IsRegister()) {
8725 GetAssembler()->LoadFromOffset(kLoadWord, temp, sp, location.GetStackIndex());
8726 return temp;
8727 }
8728 // For register locations, check if the register was saved. If so, get it from the stack.
8729 // Note: There is a chance that the register was saved but not overwritten, so we could
8730 // save one load. However, since this is just an intrinsic slow path we prefer this
8731 // simple and more robust approach rather that trying to determine if that's the case.
8732 SlowPathCode* slow_path = GetCurrentSlowPath();
Scott Wakelingd5cd4972017-02-03 11:38:35 +00008733 if (slow_path != nullptr && slow_path->IsCoreRegisterSaved(RegisterFrom(location).GetCode())) {
Scott Wakelingfe885462016-09-22 10:24:38 +01008734 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(RegisterFrom(location).GetCode());
8735 GetAssembler()->LoadFromOffset(kLoadWord, temp, sp, stack_offset);
8736 return temp;
8737 }
8738 return RegisterFrom(location);
8739}
8740
TatWai Chongd8c052a2016-11-02 16:12:48 +08008741Location CodeGeneratorARMVIXL::GenerateCalleeMethodStaticOrDirectCall(
Scott Wakelingfe885462016-09-22 10:24:38 +01008742 HInvokeStaticOrDirect* invoke, Location temp) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00008743 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
Scott Wakelingfe885462016-09-22 10:24:38 +01008744 switch (invoke->GetMethodLoadKind()) {
8745 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
8746 uint32_t offset =
8747 GetThreadOffset<kArmPointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
8748 // temp = thread->string_init_entrypoint
Artem Serovd4cc5b22016-11-04 11:19:09 +00008749 GetAssembler()->LoadFromOffset(kLoadWord, RegisterFrom(temp), tr, offset);
8750 break;
8751 }
8752 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
8753 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
8754 break;
8755 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
8756 __ Mov(RegisterFrom(temp), Operand::From(invoke->GetMethodAddress()));
8757 break;
Artem Serovd4cc5b22016-11-04 11:19:09 +00008758 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
8759 HArmDexCacheArraysBase* base =
8760 invoke->InputAt(invoke->GetSpecialInputIndex())->AsArmDexCacheArraysBase();
8761 vixl32::Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke, RegisterFrom(temp));
8762 int32_t offset = invoke->GetDexCacheArrayOffset() - base->GetElementOffset();
8763 GetAssembler()->LoadFromOffset(kLoadWord, RegisterFrom(temp), base_reg, offset);
Scott Wakelingfe885462016-09-22 10:24:38 +01008764 break;
8765 }
8766 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
8767 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
8768 vixl32::Register method_reg;
Artem Serovd4cc5b22016-11-04 11:19:09 +00008769 vixl32::Register reg = RegisterFrom(temp);
Scott Wakelingfe885462016-09-22 10:24:38 +01008770 if (current_method.IsRegister()) {
8771 method_reg = RegisterFrom(current_method);
8772 } else {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008773 DCHECK(invoke->GetLocations()->Intrinsified());
8774 DCHECK(!current_method.IsValid());
Artem Serovd4cc5b22016-11-04 11:19:09 +00008775 method_reg = reg;
8776 GetAssembler()->LoadFromOffset(kLoadWord, reg, sp, kCurrentMethodStackOffset);
Scott Wakelingfe885462016-09-22 10:24:38 +01008777 }
8778 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
8779 GetAssembler()->LoadFromOffset(
8780 kLoadWord,
Artem Serovd4cc5b22016-11-04 11:19:09 +00008781 reg,
Scott Wakelingfe885462016-09-22 10:24:38 +01008782 method_reg,
8783 ArtMethod::DexCacheResolvedMethodsOffset(kArmPointerSize).Int32Value());
8784 // temp = temp[index_in_cache];
8785 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
8786 uint32_t index_in_cache = invoke->GetDexMethodIndex();
8787 GetAssembler()->LoadFromOffset(
Artem Serovd4cc5b22016-11-04 11:19:09 +00008788 kLoadWord, reg, reg, CodeGenerator::GetCachePointerOffset(index_in_cache));
Scott Wakelingfe885462016-09-22 10:24:38 +01008789 break;
8790 }
Scott Wakelingfe885462016-09-22 10:24:38 +01008791 }
TatWai Chongd8c052a2016-11-02 16:12:48 +08008792 return callee_method;
8793}
8794
8795void CodeGeneratorARMVIXL::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
8796 Location temp) {
8797 Location callee_method = GenerateCalleeMethodStaticOrDirectCall(invoke, temp);
Scott Wakelingfe885462016-09-22 10:24:38 +01008798
Artem Serovd4cc5b22016-11-04 11:19:09 +00008799 switch (invoke->GetCodePtrLocation()) {
8800 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
8801 __ Bl(GetFrameEntryLabel());
8802 break;
Artem Serovd4cc5b22016-11-04 11:19:09 +00008803 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
8804 // LR = callee_method->entry_point_from_quick_compiled_code_
8805 GetAssembler()->LoadFromOffset(
8806 kLoadWord,
8807 lr,
8808 RegisterFrom(callee_method),
8809 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize).Int32Value());
Alexandre Rames374ddf32016-11-04 10:40:49 +00008810 {
8811 // 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 +00008812 ExactAssemblyScope aas(GetVIXLAssembler(),
8813 vixl32::k16BitT32InstructionSizeInBytes,
8814 CodeBufferCheckScope::kExactSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00008815 // LR()
8816 __ blx(lr);
8817 }
Artem Serovd4cc5b22016-11-04 11:19:09 +00008818 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01008819 }
8820
Scott Wakelingfe885462016-09-22 10:24:38 +01008821 DCHECK(!IsLeafMethod());
8822}
8823
8824void CodeGeneratorARMVIXL::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
8825 vixl32::Register temp = RegisterFrom(temp_location);
8826 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
8827 invoke->GetVTableIndex(), kArmPointerSize).Uint32Value();
8828
8829 // Use the calling convention instead of the location of the receiver, as
8830 // intrinsics may have put the receiver in a different register. In the intrinsics
8831 // slow path, the arguments have been moved to the right place, so here we are
8832 // guaranteed that the receiver is the first register of the calling convention.
8833 InvokeDexCallingConventionARMVIXL calling_convention;
8834 vixl32::Register receiver = calling_convention.GetRegisterAt(0);
8835 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Alexandre Rames374ddf32016-11-04 10:40:49 +00008836 {
8837 // Make sure the pc is recorded immediately after the `ldr` instruction.
Artem Serov0fb37192016-12-06 18:13:40 +00008838 ExactAssemblyScope aas(GetVIXLAssembler(),
8839 vixl32::kMaxInstructionSizeInBytes,
8840 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00008841 // /* HeapReference<Class> */ temp = receiver->klass_
8842 __ ldr(temp, MemOperand(receiver, class_offset));
8843 MaybeRecordImplicitNullCheck(invoke);
8844 }
Scott Wakelingfe885462016-09-22 10:24:38 +01008845 // Instead of simply (possibly) unpoisoning `temp` here, we should
8846 // emit a read barrier for the previous class reference load.
8847 // However this is not required in practice, as this is an
8848 // intermediate/temporary reference and because the current
8849 // concurrent copying collector keeps the from-space memory
8850 // intact/accessible until the end of the marking phase (the
8851 // concurrent copying collector may not in the future).
8852 GetAssembler()->MaybeUnpoisonHeapReference(temp);
8853
8854 // temp = temp->GetMethodAt(method_offset);
8855 uint32_t entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(
8856 kArmPointerSize).Int32Value();
8857 GetAssembler()->LoadFromOffset(kLoadWord, temp, temp, method_offset);
8858 // LR = temp->GetEntryPoint();
8859 GetAssembler()->LoadFromOffset(kLoadWord, lr, temp, entry_point);
8860 // LR();
Alexandre Rames374ddf32016-11-04 10:40:49 +00008861 // This `blx` *must* be the *last* instruction generated by this stub, so that calls to
8862 // `RecordPcInfo()` immediately following record the correct pc. Use a scope to help guarantee
8863 // that.
8864 // 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 +00008865 ExactAssemblyScope aas(GetVIXLAssembler(),
8866 vixl32::k16BitT32InstructionSizeInBytes,
8867 CodeBufferCheckScope::kExactSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00008868 __ blx(lr);
Scott Wakelingfe885462016-09-22 10:24:38 +01008869}
8870
Artem Serovd4cc5b22016-11-04 11:19:09 +00008871CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewPcRelativeStringPatch(
Vladimir Marko6bec91c2017-01-09 15:03:12 +00008872 const DexFile& dex_file, dex::StringIndex string_index) {
8873 return NewPcRelativePatch(dex_file, string_index.index_, &pc_relative_string_patches_);
Artem Serovd4cc5b22016-11-04 11:19:09 +00008874}
8875
8876CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewPcRelativeTypePatch(
8877 const DexFile& dex_file, dex::TypeIndex type_index) {
8878 return NewPcRelativePatch(dex_file, type_index.index_, &pc_relative_type_patches_);
8879}
8880
Vladimir Marko1998cd02017-01-13 13:02:58 +00008881CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewTypeBssEntryPatch(
8882 const DexFile& dex_file, dex::TypeIndex type_index) {
8883 return NewPcRelativePatch(dex_file, type_index.index_, &type_bss_entry_patches_);
8884}
8885
Artem Serovd4cc5b22016-11-04 11:19:09 +00008886CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewPcRelativeDexCacheArrayPatch(
8887 const DexFile& dex_file, uint32_t element_offset) {
8888 return NewPcRelativePatch(dex_file, element_offset, &pc_relative_dex_cache_patches_);
8889}
8890
8891CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewPcRelativePatch(
8892 const DexFile& dex_file, uint32_t offset_or_index, ArenaDeque<PcRelativePatchInfo>* patches) {
8893 patches->emplace_back(dex_file, offset_or_index);
8894 return &patches->back();
8895}
8896
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008897vixl::aarch32::Label* CodeGeneratorARMVIXL::NewBakerReadBarrierPatch(uint32_t custom_data) {
8898 baker_read_barrier_patches_.emplace_back(custom_data);
8899 return &baker_read_barrier_patches_.back().label;
8900}
8901
Artem Serovc5fcb442016-12-02 19:19:58 +00008902VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateBootImageStringLiteral(
8903 const DexFile& dex_file,
8904 dex::StringIndex string_index) {
8905 return boot_image_string_patches_.GetOrCreate(
8906 StringReference(&dex_file, string_index),
8907 [this]() {
8908 return GetAssembler()->CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u);
8909 });
8910}
8911
8912VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateBootImageTypeLiteral(
8913 const DexFile& dex_file,
8914 dex::TypeIndex type_index) {
8915 return boot_image_type_patches_.GetOrCreate(
8916 TypeReference(&dex_file, type_index),
8917 [this]() {
8918 return GetAssembler()->CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u);
8919 });
8920}
8921
8922VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateBootImageAddressLiteral(uint32_t address) {
Richard Uhlerc52f3032017-03-02 13:45:45 +00008923 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), &uint32_literals_);
Artem Serovc5fcb442016-12-02 19:19:58 +00008924}
8925
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00008926VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateJitStringLiteral(
8927 const DexFile& dex_file,
8928 dex::StringIndex string_index,
8929 Handle<mirror::String> handle) {
8930 jit_string_roots_.Overwrite(StringReference(&dex_file, string_index),
8931 reinterpret_cast64<uint64_t>(handle.GetReference()));
Artem Serovc5fcb442016-12-02 19:19:58 +00008932 return jit_string_patches_.GetOrCreate(
8933 StringReference(&dex_file, string_index),
8934 [this]() {
8935 return GetAssembler()->CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u);
8936 });
8937}
8938
8939VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateJitClassLiteral(const DexFile& dex_file,
8940 dex::TypeIndex type_index,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00008941 Handle<mirror::Class> handle) {
8942 jit_class_roots_.Overwrite(TypeReference(&dex_file, type_index),
8943 reinterpret_cast64<uint64_t>(handle.GetReference()));
Artem Serovc5fcb442016-12-02 19:19:58 +00008944 return jit_class_patches_.GetOrCreate(
8945 TypeReference(&dex_file, type_index),
8946 [this]() {
8947 return GetAssembler()->CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u);
8948 });
8949}
8950
Artem Serovd4cc5b22016-11-04 11:19:09 +00008951template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
8952inline void CodeGeneratorARMVIXL::EmitPcRelativeLinkerPatches(
8953 const ArenaDeque<PcRelativePatchInfo>& infos,
8954 ArenaVector<LinkerPatch>* linker_patches) {
8955 for (const PcRelativePatchInfo& info : infos) {
8956 const DexFile& dex_file = info.target_dex_file;
8957 size_t offset_or_index = info.offset_or_index;
8958 DCHECK(info.add_pc_label.IsBound());
8959 uint32_t add_pc_offset = dchecked_integral_cast<uint32_t>(info.add_pc_label.GetLocation());
8960 // Add MOVW patch.
8961 DCHECK(info.movw_label.IsBound());
8962 uint32_t movw_offset = dchecked_integral_cast<uint32_t>(info.movw_label.GetLocation());
8963 linker_patches->push_back(Factory(movw_offset, &dex_file, add_pc_offset, offset_or_index));
8964 // Add MOVT patch.
8965 DCHECK(info.movt_label.IsBound());
8966 uint32_t movt_offset = dchecked_integral_cast<uint32_t>(info.movt_label.GetLocation());
8967 linker_patches->push_back(Factory(movt_offset, &dex_file, add_pc_offset, offset_or_index));
8968 }
8969}
8970
8971void CodeGeneratorARMVIXL::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
8972 DCHECK(linker_patches->empty());
8973 size_t size =
Artem Serovd4cc5b22016-11-04 11:19:09 +00008974 /* MOVW+MOVT for each entry */ 2u * pc_relative_dex_cache_patches_.size() +
Artem Serovc5fcb442016-12-02 19:19:58 +00008975 boot_image_string_patches_.size() +
Artem Serovd4cc5b22016-11-04 11:19:09 +00008976 /* MOVW+MOVT for each entry */ 2u * pc_relative_string_patches_.size() +
Artem Serovc5fcb442016-12-02 19:19:58 +00008977 boot_image_type_patches_.size() +
8978 /* MOVW+MOVT for each entry */ 2u * pc_relative_type_patches_.size() +
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008979 /* MOVW+MOVT for each entry */ 2u * type_bss_entry_patches_.size() +
8980 baker_read_barrier_patches_.size();
Artem Serovd4cc5b22016-11-04 11:19:09 +00008981 linker_patches->reserve(size);
Artem Serovd4cc5b22016-11-04 11:19:09 +00008982 EmitPcRelativeLinkerPatches<LinkerPatch::DexCacheArrayPatch>(pc_relative_dex_cache_patches_,
8983 linker_patches);
Artem Serovc5fcb442016-12-02 19:19:58 +00008984 for (const auto& entry : boot_image_string_patches_) {
8985 const StringReference& target_string = entry.first;
8986 VIXLUInt32Literal* literal = entry.second;
8987 DCHECK(literal->IsBound());
8988 uint32_t literal_offset = literal->GetLocation();
8989 linker_patches->push_back(LinkerPatch::StringPatch(literal_offset,
8990 target_string.dex_file,
8991 target_string.string_index.index_));
8992 }
Artem Serovd4cc5b22016-11-04 11:19:09 +00008993 if (!GetCompilerOptions().IsBootImage()) {
Vladimir Marko1998cd02017-01-13 13:02:58 +00008994 DCHECK(pc_relative_type_patches_.empty());
Artem Serovd4cc5b22016-11-04 11:19:09 +00008995 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(pc_relative_string_patches_,
8996 linker_patches);
8997 } else {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00008998 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(pc_relative_type_patches_,
8999 linker_patches);
Artem Serovd4cc5b22016-11-04 11:19:09 +00009000 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(pc_relative_string_patches_,
9001 linker_patches);
9002 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00009003 EmitPcRelativeLinkerPatches<LinkerPatch::TypeBssEntryPatch>(type_bss_entry_patches_,
9004 linker_patches);
Artem Serovc5fcb442016-12-02 19:19:58 +00009005 for (const auto& entry : boot_image_type_patches_) {
9006 const TypeReference& target_type = entry.first;
9007 VIXLUInt32Literal* literal = entry.second;
9008 DCHECK(literal->IsBound());
9009 uint32_t literal_offset = literal->GetLocation();
9010 linker_patches->push_back(LinkerPatch::TypePatch(literal_offset,
9011 target_type.dex_file,
9012 target_type.type_index.index_));
9013 }
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01009014 for (const BakerReadBarrierPatchInfo& info : baker_read_barrier_patches_) {
9015 linker_patches->push_back(LinkerPatch::BakerReadBarrierBranchPatch(info.label.GetLocation(),
9016 info.custom_data));
9017 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00009018 DCHECK_EQ(size, linker_patches->size());
Artem Serovc5fcb442016-12-02 19:19:58 +00009019}
9020
9021VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateUint32Literal(
9022 uint32_t value,
9023 Uint32ToLiteralMap* map) {
9024 return map->GetOrCreate(
9025 value,
9026 [this, value]() {
9027 return GetAssembler()->CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ value);
9028 });
9029}
9030
9031VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateMethodLiteral(
9032 MethodReference target_method,
9033 MethodToLiteralMap* map) {
9034 return map->GetOrCreate(
9035 target_method,
9036 [this]() {
9037 return GetAssembler()->CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u);
9038 });
9039}
9040
Artem Serov2bbc9532016-10-21 11:51:50 +01009041void LocationsBuilderARMVIXL::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
9042 LocationSummary* locations =
9043 new (GetGraph()->GetArena()) LocationSummary(instr, LocationSummary::kNoCall);
9044 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
9045 Location::RequiresRegister());
9046 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
9047 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
9048 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
9049}
9050
9051void InstructionCodeGeneratorARMVIXL::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
9052 vixl32::Register res = OutputRegister(instr);
9053 vixl32::Register accumulator =
9054 InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
9055 vixl32::Register mul_left =
9056 InputRegisterAt(instr, HMultiplyAccumulate::kInputMulLeftIndex);
9057 vixl32::Register mul_right =
9058 InputRegisterAt(instr, HMultiplyAccumulate::kInputMulRightIndex);
9059
9060 if (instr->GetOpKind() == HInstruction::kAdd) {
9061 __ Mla(res, mul_left, mul_right, accumulator);
9062 } else {
9063 __ Mls(res, mul_left, mul_right, accumulator);
9064 }
9065}
9066
Artem Serov551b28f2016-10-18 19:11:30 +01009067void LocationsBuilderARMVIXL::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
9068 // Nothing to do, this should be removed during prepare for register allocator.
9069 LOG(FATAL) << "Unreachable";
9070}
9071
9072void InstructionCodeGeneratorARMVIXL::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
9073 // Nothing to do, this should be removed during prepare for register allocator.
9074 LOG(FATAL) << "Unreachable";
9075}
9076
9077// Simple implementation of packed switch - generate cascaded compare/jumps.
9078void LocationsBuilderARMVIXL::VisitPackedSwitch(HPackedSwitch* switch_instr) {
9079 LocationSummary* locations =
9080 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
9081 locations->SetInAt(0, Location::RequiresRegister());
9082 if (switch_instr->GetNumEntries() > kPackedSwitchCompareJumpThreshold &&
9083 codegen_->GetAssembler()->GetVIXLAssembler()->IsUsingT32()) {
9084 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the table base.
9085 if (switch_instr->GetStartValue() != 0) {
9086 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the bias.
9087 }
9088 }
9089}
9090
9091// TODO(VIXL): Investigate and reach the parity with old arm codegen.
9092void InstructionCodeGeneratorARMVIXL::VisitPackedSwitch(HPackedSwitch* switch_instr) {
9093 int32_t lower_bound = switch_instr->GetStartValue();
9094 uint32_t num_entries = switch_instr->GetNumEntries();
9095 LocationSummary* locations = switch_instr->GetLocations();
9096 vixl32::Register value_reg = InputRegisterAt(switch_instr, 0);
9097 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
9098
9099 if (num_entries <= kPackedSwitchCompareJumpThreshold ||
9100 !codegen_->GetAssembler()->GetVIXLAssembler()->IsUsingT32()) {
9101 // Create a series of compare/jumps.
Anton Kirilovedb2ac32016-11-30 15:14:10 +00009102 UseScratchRegisterScope temps(GetVIXLAssembler());
Artem Serov551b28f2016-10-18 19:11:30 +01009103 vixl32::Register temp_reg = temps.Acquire();
9104 // Note: It is fine for the below AddConstantSetFlags() using IP register to temporarily store
9105 // the immediate, because IP is used as the destination register. For the other
9106 // AddConstantSetFlags() and GenerateCompareWithImmediate(), the immediate values are constant,
9107 // and they can be encoded in the instruction without making use of IP register.
9108 __ Adds(temp_reg, value_reg, -lower_bound);
9109
9110 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
9111 // Jump to successors[0] if value == lower_bound.
9112 __ B(eq, codegen_->GetLabelOf(successors[0]));
9113 int32_t last_index = 0;
9114 for (; num_entries - last_index > 2; last_index += 2) {
9115 __ Adds(temp_reg, temp_reg, -2);
9116 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
9117 __ B(lo, codegen_->GetLabelOf(successors[last_index + 1]));
9118 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
9119 __ B(eq, codegen_->GetLabelOf(successors[last_index + 2]));
9120 }
9121 if (num_entries - last_index == 2) {
9122 // The last missing case_value.
9123 __ Cmp(temp_reg, 1);
9124 __ B(eq, codegen_->GetLabelOf(successors[last_index + 1]));
9125 }
9126
9127 // And the default for any other value.
9128 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
9129 __ B(codegen_->GetLabelOf(default_block));
9130 }
9131 } else {
9132 // Create a table lookup.
9133 vixl32::Register table_base = RegisterFrom(locations->GetTemp(0));
9134
9135 JumpTableARMVIXL* jump_table = codegen_->CreateJumpTable(switch_instr);
9136
9137 // Remove the bias.
9138 vixl32::Register key_reg;
9139 if (lower_bound != 0) {
9140 key_reg = RegisterFrom(locations->GetTemp(1));
9141 __ Sub(key_reg, value_reg, lower_bound);
9142 } else {
9143 key_reg = value_reg;
9144 }
9145
9146 // Check whether the value is in the table, jump to default block if not.
9147 __ Cmp(key_reg, num_entries - 1);
9148 __ B(hi, codegen_->GetLabelOf(default_block));
9149
Anton Kirilovedb2ac32016-11-30 15:14:10 +00009150 UseScratchRegisterScope temps(GetVIXLAssembler());
Artem Serov551b28f2016-10-18 19:11:30 +01009151 vixl32::Register jump_offset = temps.Acquire();
9152
9153 // Load jump offset from the table.
Scott Wakeling86e9d262017-01-18 15:59:24 +00009154 {
9155 const size_t jump_size = switch_instr->GetNumEntries() * sizeof(int32_t);
9156 ExactAssemblyScope aas(GetVIXLAssembler(),
9157 (vixl32::kMaxInstructionSizeInBytes * 4) + jump_size,
9158 CodeBufferCheckScope::kMaximumSize);
9159 __ adr(table_base, jump_table->GetTableStartLabel());
9160 __ ldr(jump_offset, MemOperand(table_base, key_reg, vixl32::LSL, 2));
Artem Serov551b28f2016-10-18 19:11:30 +01009161
Scott Wakeling86e9d262017-01-18 15:59:24 +00009162 // Jump to target block by branching to table_base(pc related) + offset.
9163 vixl32::Register target_address = table_base;
9164 __ add(target_address, table_base, jump_offset);
9165 __ bx(target_address);
Artem Serov09a940d2016-11-11 16:15:11 +00009166
Scott Wakeling86e9d262017-01-18 15:59:24 +00009167 jump_table->EmitTable(codegen_);
9168 }
Artem Serov551b28f2016-10-18 19:11:30 +01009169 }
9170}
Artem Serovd4cc5b22016-11-04 11:19:09 +00009171void LocationsBuilderARMVIXL::VisitArmDexCacheArraysBase(HArmDexCacheArraysBase* base) {
9172 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(base);
9173 locations->SetOut(Location::RequiresRegister());
9174}
9175
9176void InstructionCodeGeneratorARMVIXL::VisitArmDexCacheArraysBase(HArmDexCacheArraysBase* base) {
9177 vixl32::Register base_reg = OutputRegister(base);
9178 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
9179 codegen_->NewPcRelativeDexCacheArrayPatch(base->GetDexFile(), base->GetElementOffset());
9180 codegen_->EmitMovwMovtPlaceholder(labels, base_reg);
9181}
Artem Serov551b28f2016-10-18 19:11:30 +01009182
Artem Serov02d37832016-10-25 15:25:33 +01009183// Copy the result of a call into the given target.
Anton Kirilove28d9ae2016-10-25 18:17:23 +01009184void CodeGeneratorARMVIXL::MoveFromReturnRegister(Location trg, Primitive::Type type) {
9185 if (!trg.IsValid()) {
9186 DCHECK_EQ(type, Primitive::kPrimVoid);
9187 return;
9188 }
9189
9190 DCHECK_NE(type, Primitive::kPrimVoid);
9191
Artem Serovd4cc5b22016-11-04 11:19:09 +00009192 Location return_loc = InvokeDexCallingConventionVisitorARMVIXL().GetReturnLocation(type);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01009193 if (return_loc.Equals(trg)) {
9194 return;
9195 }
9196
9197 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
9198 // with the last branch.
9199 if (type == Primitive::kPrimLong) {
9200 TODO_VIXL32(FATAL);
9201 } else if (type == Primitive::kPrimDouble) {
9202 TODO_VIXL32(FATAL);
9203 } else {
9204 // Let the parallel move resolver take care of all of this.
9205 HParallelMove parallel_move(GetGraph()->GetArena());
9206 parallel_move.AddMove(return_loc, trg, type, nullptr);
9207 GetMoveResolver()->EmitNativeCode(&parallel_move);
9208 }
Scott Wakelingfe885462016-09-22 10:24:38 +01009209}
9210
xueliang.zhong8d2c4592016-11-23 17:05:25 +00009211void LocationsBuilderARMVIXL::VisitClassTableGet(HClassTableGet* instruction) {
9212 LocationSummary* locations =
9213 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
9214 locations->SetInAt(0, Location::RequiresRegister());
9215 locations->SetOut(Location::RequiresRegister());
Artem Serov551b28f2016-10-18 19:11:30 +01009216}
9217
xueliang.zhong8d2c4592016-11-23 17:05:25 +00009218void InstructionCodeGeneratorARMVIXL::VisitClassTableGet(HClassTableGet* instruction) {
9219 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
9220 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
9221 instruction->GetIndex(), kArmPointerSize).SizeValue();
9222 GetAssembler()->LoadFromOffset(kLoadWord,
9223 OutputRegister(instruction),
9224 InputRegisterAt(instruction, 0),
9225 method_offset);
9226 } else {
9227 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
9228 instruction->GetIndex(), kArmPointerSize));
9229 GetAssembler()->LoadFromOffset(kLoadWord,
9230 OutputRegister(instruction),
9231 InputRegisterAt(instruction, 0),
9232 mirror::Class::ImtPtrOffset(kArmPointerSize).Uint32Value());
9233 GetAssembler()->LoadFromOffset(kLoadWord,
9234 OutputRegister(instruction),
9235 OutputRegister(instruction),
9236 method_offset);
9237 }
Artem Serov551b28f2016-10-18 19:11:30 +01009238}
9239
Artem Serovc5fcb442016-12-02 19:19:58 +00009240static void PatchJitRootUse(uint8_t* code,
9241 const uint8_t* roots_data,
9242 VIXLUInt32Literal* literal,
9243 uint64_t index_in_table) {
9244 DCHECK(literal->IsBound());
9245 uint32_t literal_offset = literal->GetLocation();
9246 uintptr_t address =
9247 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
9248 uint8_t* data = code + literal_offset;
9249 reinterpret_cast<uint32_t*>(data)[0] = dchecked_integral_cast<uint32_t>(address);
9250}
9251
9252void CodeGeneratorARMVIXL::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
9253 for (const auto& entry : jit_string_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01009254 const StringReference& string_reference = entry.first;
9255 VIXLUInt32Literal* table_entry_literal = entry.second;
9256 const auto it = jit_string_roots_.find(string_reference);
Artem Serovc5fcb442016-12-02 19:19:58 +00009257 DCHECK(it != jit_string_roots_.end());
Vladimir Marko7d157fc2017-05-10 16:29:23 +01009258 uint64_t index_in_table = it->second;
9259 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Artem Serovc5fcb442016-12-02 19:19:58 +00009260 }
9261 for (const auto& entry : jit_class_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01009262 const TypeReference& type_reference = entry.first;
9263 VIXLUInt32Literal* table_entry_literal = entry.second;
9264 const auto it = jit_class_roots_.find(type_reference);
Artem Serovc5fcb442016-12-02 19:19:58 +00009265 DCHECK(it != jit_class_roots_.end());
Vladimir Marko7d157fc2017-05-10 16:29:23 +01009266 uint64_t index_in_table = it->second;
9267 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Artem Serovc5fcb442016-12-02 19:19:58 +00009268 }
9269}
9270
Artem Serovd4cc5b22016-11-04 11:19:09 +00009271void CodeGeneratorARMVIXL::EmitMovwMovtPlaceholder(
9272 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels,
9273 vixl32::Register out) {
Artem Serov0fb37192016-12-06 18:13:40 +00009274 ExactAssemblyScope aas(GetVIXLAssembler(),
9275 3 * vixl32::kMaxInstructionSizeInBytes,
9276 CodeBufferCheckScope::kMaximumSize);
Artem Serovd4cc5b22016-11-04 11:19:09 +00009277 // TODO(VIXL): Think about using mov instead of movw.
9278 __ bind(&labels->movw_label);
9279 __ movw(out, /* placeholder */ 0u);
9280 __ bind(&labels->movt_label);
9281 __ movt(out, /* placeholder */ 0u);
9282 __ bind(&labels->add_pc_label);
9283 __ add(out, out, pc);
9284}
9285
Scott Wakelingfe885462016-09-22 10:24:38 +01009286#undef __
9287#undef QUICK_ENTRY_POINT
9288#undef TODO_VIXL32
9289
9290} // namespace arm
9291} // namespace art