blob: 015e6ddea367fa78b690062b0daabe2e59101279 [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)),
2096 pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Artem Serovc5fcb442016-12-02 19:19:58 +00002097 pc_relative_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko1998cd02017-01-13 13:02:58 +00002098 type_bss_entry_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01002099 baker_read_barrier_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Artem Serovc5fcb442016-12-02 19:19:58 +00002100 jit_string_patches_(StringReferenceValueComparator(),
2101 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
2102 jit_class_patches_(TypeReferenceValueComparator(),
2103 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Scott Wakelingfe885462016-09-22 10:24:38 +01002104 // Always save the LR register to mimic Quick.
2105 AddAllocatedRegister(Location::RegisterLocation(LR));
Nicolas Geoffray13a797b2017-03-15 16:41:31 +00002106 // Give D30 and D31 as scratch register to VIXL. The register allocator only works on
2107 // S0-S31, which alias to D0-D15.
2108 GetVIXLAssembler()->GetScratchVRegisterList()->Combine(d31);
2109 GetVIXLAssembler()->GetScratchVRegisterList()->Combine(d30);
Scott Wakelingfe885462016-09-22 10:24:38 +01002110}
2111
Artem Serov551b28f2016-10-18 19:11:30 +01002112void JumpTableARMVIXL::EmitTable(CodeGeneratorARMVIXL* codegen) {
2113 uint32_t num_entries = switch_instr_->GetNumEntries();
2114 DCHECK_GE(num_entries, kPackedSwitchCompareJumpThreshold);
2115
2116 // We are about to use the assembler to place literals directly. Make sure we have enough
Scott Wakelingb77051e2016-11-21 19:46:00 +00002117 // underlying code buffer and we have generated a jump table of the right size, using
2118 // codegen->GetVIXLAssembler()->GetBuffer().Align();
Artem Serov0fb37192016-12-06 18:13:40 +00002119 ExactAssemblyScope aas(codegen->GetVIXLAssembler(),
2120 num_entries * sizeof(int32_t),
2121 CodeBufferCheckScope::kMaximumSize);
Artem Serov551b28f2016-10-18 19:11:30 +01002122 // TODO(VIXL): Check that using lower case bind is fine here.
2123 codegen->GetVIXLAssembler()->bind(&table_start_);
Artem Serov09a940d2016-11-11 16:15:11 +00002124 for (uint32_t i = 0; i < num_entries; i++) {
2125 codegen->GetVIXLAssembler()->place(bb_addresses_[i].get());
2126 }
2127}
2128
2129void JumpTableARMVIXL::FixTable(CodeGeneratorARMVIXL* codegen) {
2130 uint32_t num_entries = switch_instr_->GetNumEntries();
2131 DCHECK_GE(num_entries, kPackedSwitchCompareJumpThreshold);
2132
Artem Serov551b28f2016-10-18 19:11:30 +01002133 const ArenaVector<HBasicBlock*>& successors = switch_instr_->GetBlock()->GetSuccessors();
2134 for (uint32_t i = 0; i < num_entries; i++) {
2135 vixl32::Label* target_label = codegen->GetLabelOf(successors[i]);
2136 DCHECK(target_label->IsBound());
2137 int32_t jump_offset = target_label->GetLocation() - table_start_.GetLocation();
2138 // When doing BX to address we need to have lower bit set to 1 in T32.
2139 if (codegen->GetVIXLAssembler()->IsUsingT32()) {
2140 jump_offset++;
2141 }
2142 DCHECK_GT(jump_offset, std::numeric_limits<int32_t>::min());
2143 DCHECK_LE(jump_offset, std::numeric_limits<int32_t>::max());
Artem Serov09a940d2016-11-11 16:15:11 +00002144
Scott Wakelingb77051e2016-11-21 19:46:00 +00002145 bb_addresses_[i].get()->UpdateValue(jump_offset, codegen->GetVIXLAssembler()->GetBuffer());
Artem Serov551b28f2016-10-18 19:11:30 +01002146 }
2147}
2148
Artem Serov09a940d2016-11-11 16:15:11 +00002149void CodeGeneratorARMVIXL::FixJumpTables() {
Artem Serov551b28f2016-10-18 19:11:30 +01002150 for (auto&& jump_table : jump_tables_) {
Artem Serov09a940d2016-11-11 16:15:11 +00002151 jump_table->FixTable(this);
Artem Serov551b28f2016-10-18 19:11:30 +01002152 }
2153}
2154
Andreas Gampeca620d72016-11-08 08:09:33 -08002155#define __ reinterpret_cast<ArmVIXLAssembler*>(GetAssembler())->GetVIXLAssembler()-> // NOLINT
Scott Wakelingfe885462016-09-22 10:24:38 +01002156
2157void CodeGeneratorARMVIXL::Finalize(CodeAllocator* allocator) {
Artem Serov09a940d2016-11-11 16:15:11 +00002158 FixJumpTables();
Scott Wakelingfe885462016-09-22 10:24:38 +01002159 GetAssembler()->FinalizeCode();
2160 CodeGenerator::Finalize(allocator);
2161}
2162
2163void CodeGeneratorARMVIXL::SetupBlockedRegisters() const {
Scott Wakelingfe885462016-09-22 10:24:38 +01002164 // Stack register, LR and PC are always reserved.
2165 blocked_core_registers_[SP] = true;
2166 blocked_core_registers_[LR] = true;
2167 blocked_core_registers_[PC] = true;
2168
2169 // Reserve thread register.
2170 blocked_core_registers_[TR] = true;
2171
2172 // Reserve temp register.
2173 blocked_core_registers_[IP] = true;
2174
2175 if (GetGraph()->IsDebuggable()) {
2176 // Stubs do not save callee-save floating point registers. If the graph
2177 // is debuggable, we need to deal with these registers differently. For
2178 // now, just block them.
2179 for (uint32_t i = kFpuCalleeSaves.GetFirstSRegister().GetCode();
2180 i <= kFpuCalleeSaves.GetLastSRegister().GetCode();
2181 ++i) {
2182 blocked_fpu_registers_[i] = true;
2183 }
2184 }
Scott Wakelingfe885462016-09-22 10:24:38 +01002185}
2186
Scott Wakelingfe885462016-09-22 10:24:38 +01002187InstructionCodeGeneratorARMVIXL::InstructionCodeGeneratorARMVIXL(HGraph* graph,
2188 CodeGeneratorARMVIXL* codegen)
2189 : InstructionCodeGenerator(graph, codegen),
2190 assembler_(codegen->GetAssembler()),
2191 codegen_(codegen) {}
2192
2193void CodeGeneratorARMVIXL::ComputeSpillMask() {
2194 core_spill_mask_ = allocated_registers_.GetCoreRegisters() & core_callee_save_mask_;
2195 DCHECK_NE(core_spill_mask_, 0u) << "At least the return address register must be saved";
2196 // There is no easy instruction to restore just the PC on thumb2. We spill and
2197 // restore another arbitrary register.
2198 core_spill_mask_ |= (1 << kCoreAlwaysSpillRegister.GetCode());
2199 fpu_spill_mask_ = allocated_registers_.GetFloatingPointRegisters() & fpu_callee_save_mask_;
2200 // We use vpush and vpop for saving and restoring floating point registers, which take
2201 // a SRegister and the number of registers to save/restore after that SRegister. We
2202 // therefore update the `fpu_spill_mask_` to also contain those registers not allocated,
2203 // but in the range.
2204 if (fpu_spill_mask_ != 0) {
2205 uint32_t least_significant_bit = LeastSignificantBit(fpu_spill_mask_);
2206 uint32_t most_significant_bit = MostSignificantBit(fpu_spill_mask_);
2207 for (uint32_t i = least_significant_bit + 1 ; i < most_significant_bit; ++i) {
2208 fpu_spill_mask_ |= (1 << i);
2209 }
2210 }
2211}
2212
2213void CodeGeneratorARMVIXL::GenerateFrameEntry() {
2214 bool skip_overflow_check =
2215 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kArm);
2216 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
2217 __ Bind(&frame_entry_label_);
2218
2219 if (HasEmptyFrame()) {
2220 return;
2221 }
2222
Scott Wakelingfe885462016-09-22 10:24:38 +01002223 if (!skip_overflow_check) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002224 UseScratchRegisterScope temps(GetVIXLAssembler());
2225 vixl32::Register temp = temps.Acquire();
Anton Kirilov644032c2016-12-06 17:51:43 +00002226 __ Sub(temp, sp, Operand::From(GetStackOverflowReservedBytes(kArm)));
Scott Wakelingfe885462016-09-22 10:24:38 +01002227 // The load must immediately precede RecordPcInfo.
Artem Serov0fb37192016-12-06 18:13:40 +00002228 ExactAssemblyScope aas(GetVIXLAssembler(),
2229 vixl32::kMaxInstructionSizeInBytes,
2230 CodeBufferCheckScope::kMaximumSize);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002231 __ ldr(temp, MemOperand(temp));
2232 RecordPcInfo(nullptr, 0);
Scott Wakelingfe885462016-09-22 10:24:38 +01002233 }
2234
2235 __ Push(RegisterList(core_spill_mask_));
2236 GetAssembler()->cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(core_spill_mask_));
2237 GetAssembler()->cfi().RelOffsetForMany(DWARFReg(kMethodRegister),
2238 0,
2239 core_spill_mask_,
2240 kArmWordSize);
2241 if (fpu_spill_mask_ != 0) {
2242 uint32_t first = LeastSignificantBit(fpu_spill_mask_);
2243
2244 // Check that list is contiguous.
2245 DCHECK_EQ(fpu_spill_mask_ >> CTZ(fpu_spill_mask_), ~0u >> (32 - POPCOUNT(fpu_spill_mask_)));
2246
2247 __ Vpush(SRegisterList(vixl32::SRegister(first), POPCOUNT(fpu_spill_mask_)));
2248 GetAssembler()->cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(fpu_spill_mask_));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002249 GetAssembler()->cfi().RelOffsetForMany(DWARFReg(s0), 0, fpu_spill_mask_, kArmWordSize);
Scott Wakelingfe885462016-09-22 10:24:38 +01002250 }
Scott Wakelingbffdc702016-12-07 17:46:03 +00002251
2252 if (GetGraph()->HasShouldDeoptimizeFlag()) {
2253 UseScratchRegisterScope temps(GetVIXLAssembler());
2254 vixl32::Register temp = temps.Acquire();
2255 // Initialize should_deoptimize flag to 0.
2256 __ Mov(temp, 0);
2257 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, -kShouldDeoptimizeFlagSize);
2258 }
2259
Scott Wakelingfe885462016-09-22 10:24:38 +01002260 int adjust = GetFrameSize() - FrameEntrySpillSize();
2261 __ Sub(sp, sp, adjust);
2262 GetAssembler()->cfi().AdjustCFAOffset(adjust);
Scott Wakelingbffdc702016-12-07 17:46:03 +00002263
2264 // Save the current method if we need it. Note that we do not
2265 // do this in HCurrentMethod, as the instruction might have been removed
2266 // in the SSA graph.
2267 if (RequiresCurrentMethod()) {
2268 GetAssembler()->StoreToOffset(kStoreWord, kMethodRegister, sp, 0);
2269 }
Scott Wakelingfe885462016-09-22 10:24:38 +01002270}
2271
2272void CodeGeneratorARMVIXL::GenerateFrameExit() {
2273 if (HasEmptyFrame()) {
2274 __ Bx(lr);
2275 return;
2276 }
2277 GetAssembler()->cfi().RememberState();
2278 int adjust = GetFrameSize() - FrameEntrySpillSize();
2279 __ Add(sp, sp, adjust);
2280 GetAssembler()->cfi().AdjustCFAOffset(-adjust);
2281 if (fpu_spill_mask_ != 0) {
2282 uint32_t first = LeastSignificantBit(fpu_spill_mask_);
2283
2284 // Check that list is contiguous.
2285 DCHECK_EQ(fpu_spill_mask_ >> CTZ(fpu_spill_mask_), ~0u >> (32 - POPCOUNT(fpu_spill_mask_)));
2286
2287 __ Vpop(SRegisterList(vixl32::SRegister(first), POPCOUNT(fpu_spill_mask_)));
2288 GetAssembler()->cfi().AdjustCFAOffset(
2289 -static_cast<int>(kArmWordSize) * POPCOUNT(fpu_spill_mask_));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002290 GetAssembler()->cfi().RestoreMany(DWARFReg(vixl32::SRegister(0)), fpu_spill_mask_);
Scott Wakelingfe885462016-09-22 10:24:38 +01002291 }
2292 // Pop LR into PC to return.
2293 DCHECK_NE(core_spill_mask_ & (1 << kLrCode), 0U);
2294 uint32_t pop_mask = (core_spill_mask_ & (~(1 << kLrCode))) | 1 << kPcCode;
2295 __ Pop(RegisterList(pop_mask));
2296 GetAssembler()->cfi().RestoreState();
2297 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
2298}
2299
2300void CodeGeneratorARMVIXL::Bind(HBasicBlock* block) {
2301 __ Bind(GetLabelOf(block));
2302}
2303
Artem Serovd4cc5b22016-11-04 11:19:09 +00002304Location InvokeDexCallingConventionVisitorARMVIXL::GetNextLocation(Primitive::Type type) {
2305 switch (type) {
2306 case Primitive::kPrimBoolean:
2307 case Primitive::kPrimByte:
2308 case Primitive::kPrimChar:
2309 case Primitive::kPrimShort:
2310 case Primitive::kPrimInt:
2311 case Primitive::kPrimNot: {
2312 uint32_t index = gp_index_++;
2313 uint32_t stack_index = stack_index_++;
2314 if (index < calling_convention.GetNumberOfRegisters()) {
2315 return LocationFrom(calling_convention.GetRegisterAt(index));
2316 } else {
2317 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
2318 }
2319 }
2320
2321 case Primitive::kPrimLong: {
2322 uint32_t index = gp_index_;
2323 uint32_t stack_index = stack_index_;
2324 gp_index_ += 2;
2325 stack_index_ += 2;
2326 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
2327 if (calling_convention.GetRegisterAt(index).Is(r1)) {
2328 // Skip R1, and use R2_R3 instead.
2329 gp_index_++;
2330 index++;
2331 }
2332 }
2333 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
2334 DCHECK_EQ(calling_convention.GetRegisterAt(index).GetCode() + 1,
2335 calling_convention.GetRegisterAt(index + 1).GetCode());
2336
2337 return LocationFrom(calling_convention.GetRegisterAt(index),
2338 calling_convention.GetRegisterAt(index + 1));
2339 } else {
2340 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
2341 }
2342 }
2343
2344 case Primitive::kPrimFloat: {
2345 uint32_t stack_index = stack_index_++;
2346 if (float_index_ % 2 == 0) {
2347 float_index_ = std::max(double_index_, float_index_);
2348 }
2349 if (float_index_ < calling_convention.GetNumberOfFpuRegisters()) {
2350 return LocationFrom(calling_convention.GetFpuRegisterAt(float_index_++));
2351 } else {
2352 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
2353 }
2354 }
2355
2356 case Primitive::kPrimDouble: {
2357 double_index_ = std::max(double_index_, RoundUp(float_index_, 2));
2358 uint32_t stack_index = stack_index_;
2359 stack_index_ += 2;
2360 if (double_index_ + 1 < calling_convention.GetNumberOfFpuRegisters()) {
2361 uint32_t index = double_index_;
2362 double_index_ += 2;
2363 Location result = LocationFrom(
2364 calling_convention.GetFpuRegisterAt(index),
2365 calling_convention.GetFpuRegisterAt(index + 1));
2366 DCHECK(ExpectedPairLayout(result));
2367 return result;
2368 } else {
2369 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
2370 }
2371 }
2372
2373 case Primitive::kPrimVoid:
2374 LOG(FATAL) << "Unexpected parameter type " << type;
2375 break;
2376 }
2377 return Location::NoLocation();
2378}
2379
2380Location InvokeDexCallingConventionVisitorARMVIXL::GetReturnLocation(Primitive::Type type) const {
2381 switch (type) {
2382 case Primitive::kPrimBoolean:
2383 case Primitive::kPrimByte:
2384 case Primitive::kPrimChar:
2385 case Primitive::kPrimShort:
2386 case Primitive::kPrimInt:
2387 case Primitive::kPrimNot: {
2388 return LocationFrom(r0);
2389 }
2390
2391 case Primitive::kPrimFloat: {
2392 return LocationFrom(s0);
2393 }
2394
2395 case Primitive::kPrimLong: {
2396 return LocationFrom(r0, r1);
2397 }
2398
2399 case Primitive::kPrimDouble: {
2400 return LocationFrom(s0, s1);
2401 }
2402
2403 case Primitive::kPrimVoid:
2404 return Location::NoLocation();
2405 }
2406
2407 UNREACHABLE();
2408}
2409
2410Location InvokeDexCallingConventionVisitorARMVIXL::GetMethodLocation() const {
2411 return LocationFrom(kMethodRegister);
2412}
2413
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002414void CodeGeneratorARMVIXL::Move32(Location destination, Location source) {
2415 if (source.Equals(destination)) {
2416 return;
2417 }
2418 if (destination.IsRegister()) {
2419 if (source.IsRegister()) {
2420 __ Mov(RegisterFrom(destination), RegisterFrom(source));
2421 } else if (source.IsFpuRegister()) {
2422 __ Vmov(RegisterFrom(destination), SRegisterFrom(source));
2423 } else {
2424 GetAssembler()->LoadFromOffset(kLoadWord,
2425 RegisterFrom(destination),
2426 sp,
2427 source.GetStackIndex());
2428 }
2429 } else if (destination.IsFpuRegister()) {
2430 if (source.IsRegister()) {
2431 __ Vmov(SRegisterFrom(destination), RegisterFrom(source));
2432 } else if (source.IsFpuRegister()) {
2433 __ Vmov(SRegisterFrom(destination), SRegisterFrom(source));
2434 } else {
2435 GetAssembler()->LoadSFromOffset(SRegisterFrom(destination), sp, source.GetStackIndex());
2436 }
2437 } else {
2438 DCHECK(destination.IsStackSlot()) << destination;
2439 if (source.IsRegister()) {
2440 GetAssembler()->StoreToOffset(kStoreWord,
2441 RegisterFrom(source),
2442 sp,
2443 destination.GetStackIndex());
2444 } else if (source.IsFpuRegister()) {
2445 GetAssembler()->StoreSToOffset(SRegisterFrom(source), sp, destination.GetStackIndex());
2446 } else {
2447 DCHECK(source.IsStackSlot()) << source;
2448 UseScratchRegisterScope temps(GetVIXLAssembler());
2449 vixl32::Register temp = temps.Acquire();
2450 GetAssembler()->LoadFromOffset(kLoadWord, temp, sp, source.GetStackIndex());
2451 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
2452 }
2453 }
2454}
2455
Artem Serovcfbe9132016-10-14 15:58:56 +01002456void CodeGeneratorARMVIXL::MoveConstant(Location location, int32_t value) {
2457 DCHECK(location.IsRegister());
2458 __ Mov(RegisterFrom(location), value);
Scott Wakelingfe885462016-09-22 10:24:38 +01002459}
2460
2461void CodeGeneratorARMVIXL::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002462 // TODO(VIXL): Maybe refactor to have the 'move' implementation here and use it in
2463 // `ParallelMoveResolverARMVIXL::EmitMove`, as is done in the `arm64` backend.
2464 HParallelMove move(GetGraph()->GetArena());
2465 move.AddMove(src, dst, dst_type, nullptr);
2466 GetMoveResolver()->EmitNativeCode(&move);
Scott Wakelingfe885462016-09-22 10:24:38 +01002467}
2468
Artem Serovcfbe9132016-10-14 15:58:56 +01002469void CodeGeneratorARMVIXL::AddLocationAsTemp(Location location, LocationSummary* locations) {
2470 if (location.IsRegister()) {
2471 locations->AddTemp(location);
2472 } else if (location.IsRegisterPair()) {
2473 locations->AddTemp(LocationFrom(LowRegisterFrom(location)));
2474 locations->AddTemp(LocationFrom(HighRegisterFrom(location)));
2475 } else {
2476 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
2477 }
Scott Wakelingfe885462016-09-22 10:24:38 +01002478}
2479
2480void CodeGeneratorARMVIXL::InvokeRuntime(QuickEntrypointEnum entrypoint,
2481 HInstruction* instruction,
2482 uint32_t dex_pc,
2483 SlowPathCode* slow_path) {
2484 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Alexandre Rames374ddf32016-11-04 10:40:49 +00002485 __ Ldr(lr, MemOperand(tr, GetThreadOffset<kArmPointerSize>(entrypoint).Int32Value()));
2486 // Ensure the pc position is recorded immediately after the `blx` instruction.
2487 // 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 +00002488 ExactAssemblyScope aas(GetVIXLAssembler(),
2489 vixl32::k16BitT32InstructionSizeInBytes,
2490 CodeBufferCheckScope::kExactSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00002491 __ blx(lr);
Scott Wakelingfe885462016-09-22 10:24:38 +01002492 if (EntrypointRequiresStackMap(entrypoint)) {
2493 RecordPcInfo(instruction, dex_pc, slow_path);
2494 }
2495}
2496
2497void CodeGeneratorARMVIXL::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
2498 HInstruction* instruction,
2499 SlowPathCode* slow_path) {
2500 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Alexandre Rames374ddf32016-11-04 10:40:49 +00002501 __ Ldr(lr, MemOperand(tr, entry_point_offset));
Scott Wakelingfe885462016-09-22 10:24:38 +01002502 __ Blx(lr);
2503}
2504
Scott Wakelingfe885462016-09-22 10:24:38 +01002505void InstructionCodeGeneratorARMVIXL::HandleGoto(HInstruction* got, HBasicBlock* successor) {
2506 DCHECK(!successor->IsExitBlock());
2507 HBasicBlock* block = got->GetBlock();
2508 HInstruction* previous = got->GetPrevious();
2509 HLoopInformation* info = block->GetLoopInformation();
2510
2511 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
2512 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
2513 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2514 return;
2515 }
2516 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2517 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
2518 }
2519 if (!codegen_->GoesToNextBlock(block, successor)) {
2520 __ B(codegen_->GetLabelOf(successor));
2521 }
2522}
2523
2524void LocationsBuilderARMVIXL::VisitGoto(HGoto* got) {
2525 got->SetLocations(nullptr);
2526}
2527
2528void InstructionCodeGeneratorARMVIXL::VisitGoto(HGoto* got) {
2529 HandleGoto(got, got->GetSuccessor());
2530}
2531
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002532void LocationsBuilderARMVIXL::VisitTryBoundary(HTryBoundary* try_boundary) {
2533 try_boundary->SetLocations(nullptr);
2534}
2535
2536void InstructionCodeGeneratorARMVIXL::VisitTryBoundary(HTryBoundary* try_boundary) {
2537 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2538 if (!successor->IsExitBlock()) {
2539 HandleGoto(try_boundary, successor);
2540 }
2541}
2542
Scott Wakelingfe885462016-09-22 10:24:38 +01002543void LocationsBuilderARMVIXL::VisitExit(HExit* exit) {
2544 exit->SetLocations(nullptr);
2545}
2546
2547void InstructionCodeGeneratorARMVIXL::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
2548}
2549
Nicolas Geoffray30826612017-05-10 11:59:26 +00002550void InstructionCodeGeneratorARMVIXL::GenerateLongComparesAndJumps(HCondition* cond,
2551 vixl32::Label* true_label,
2552 vixl32::Label* false_label) {
2553 LocationSummary* locations = cond->GetLocations();
2554 Location left = locations->InAt(0);
2555 Location right = locations->InAt(1);
2556 IfCondition if_cond = cond->GetCondition();
2557
2558 vixl32::Register left_high = HighRegisterFrom(left);
2559 vixl32::Register left_low = LowRegisterFrom(left);
2560 IfCondition true_high_cond = if_cond;
2561 IfCondition false_high_cond = cond->GetOppositeCondition();
2562 vixl32::Condition final_condition = ARMUnsignedCondition(if_cond); // unsigned on lower part
2563
2564 // Set the conditions for the test, remembering that == needs to be
2565 // decided using the low words.
2566 switch (if_cond) {
2567 case kCondEQ:
2568 case kCondNE:
2569 // Nothing to do.
2570 break;
2571 case kCondLT:
2572 false_high_cond = kCondGT;
2573 break;
2574 case kCondLE:
2575 true_high_cond = kCondLT;
2576 break;
2577 case kCondGT:
2578 false_high_cond = kCondLT;
2579 break;
2580 case kCondGE:
2581 true_high_cond = kCondGT;
2582 break;
2583 case kCondB:
2584 false_high_cond = kCondA;
2585 break;
2586 case kCondBE:
2587 true_high_cond = kCondB;
2588 break;
2589 case kCondA:
2590 false_high_cond = kCondB;
2591 break;
2592 case kCondAE:
2593 true_high_cond = kCondA;
2594 break;
2595 }
2596 if (right.IsConstant()) {
2597 int64_t value = Int64ConstantFrom(right);
2598 int32_t val_low = Low32Bits(value);
2599 int32_t val_high = High32Bits(value);
2600
2601 __ Cmp(left_high, val_high);
2602 if (if_cond == kCondNE) {
2603 __ B(ARMCondition(true_high_cond), true_label);
2604 } else if (if_cond == kCondEQ) {
2605 __ B(ARMCondition(false_high_cond), false_label);
2606 } else {
2607 __ B(ARMCondition(true_high_cond), true_label);
2608 __ B(ARMCondition(false_high_cond), false_label);
2609 }
2610 // Must be equal high, so compare the lows.
2611 __ Cmp(left_low, val_low);
2612 } else {
2613 vixl32::Register right_high = HighRegisterFrom(right);
2614 vixl32::Register right_low = LowRegisterFrom(right);
2615
2616 __ Cmp(left_high, right_high);
2617 if (if_cond == kCondNE) {
2618 __ B(ARMCondition(true_high_cond), true_label);
2619 } else if (if_cond == kCondEQ) {
2620 __ B(ARMCondition(false_high_cond), false_label);
2621 } else {
2622 __ B(ARMCondition(true_high_cond), true_label);
2623 __ B(ARMCondition(false_high_cond), false_label);
2624 }
2625 // Must be equal high, so compare the lows.
2626 __ Cmp(left_low, right_low);
2627 }
2628 // The last comparison might be unsigned.
2629 // TODO: optimize cases where this is always true/false
2630 __ B(final_condition, true_label);
2631}
2632
Scott Wakelingfe885462016-09-22 10:24:38 +01002633void InstructionCodeGeneratorARMVIXL::GenerateCompareTestAndBranch(HCondition* condition,
2634 vixl32::Label* true_target_in,
2635 vixl32::Label* false_target_in) {
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002636 if (CanGenerateTest(condition, codegen_->GetAssembler())) {
2637 vixl32::Label* non_fallthrough_target;
2638 bool invert;
2639
2640 if (true_target_in == nullptr) {
2641 DCHECK(false_target_in != nullptr);
2642 non_fallthrough_target = false_target_in;
2643 invert = true;
2644 } else {
2645 non_fallthrough_target = true_target_in;
2646 invert = false;
2647 }
2648
2649 const auto cond = GenerateTest(condition, invert, codegen_);
2650
2651 __ B(cond.first, non_fallthrough_target);
2652
2653 if (false_target_in != nullptr && false_target_in != non_fallthrough_target) {
2654 __ B(false_target_in);
2655 }
2656
2657 return;
2658 }
2659
Scott Wakelingfe885462016-09-22 10:24:38 +01002660 // Generated branching requires both targets to be explicit. If either of the
2661 // targets is nullptr (fallthrough) use and bind `fallthrough` instead.
2662 vixl32::Label fallthrough;
2663 vixl32::Label* true_target = (true_target_in == nullptr) ? &fallthrough : true_target_in;
2664 vixl32::Label* false_target = (false_target_in == nullptr) ? &fallthrough : false_target_in;
2665
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002666 DCHECK_EQ(condition->InputAt(0)->GetType(), Primitive::kPrimLong);
Nicolas Geoffray30826612017-05-10 11:59:26 +00002667 GenerateLongComparesAndJumps(condition, true_target, false_target);
Scott Wakelingfe885462016-09-22 10:24:38 +01002668
2669 if (false_target != &fallthrough) {
2670 __ B(false_target);
2671 }
2672
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002673 if (fallthrough.IsReferenced()) {
Scott Wakelingfe885462016-09-22 10:24:38 +01002674 __ Bind(&fallthrough);
2675 }
2676}
2677
2678void InstructionCodeGeneratorARMVIXL::GenerateTestAndBranch(HInstruction* instruction,
2679 size_t condition_input_index,
2680 vixl32::Label* true_target,
xueliang.zhongf51bc622016-11-04 09:23:32 +00002681 vixl32::Label* false_target,
2682 bool far_target) {
Scott Wakelingfe885462016-09-22 10:24:38 +01002683 HInstruction* cond = instruction->InputAt(condition_input_index);
2684
2685 if (true_target == nullptr && false_target == nullptr) {
2686 // Nothing to do. The code always falls through.
2687 return;
2688 } else if (cond->IsIntConstant()) {
2689 // Constant condition, statically compared against "true" (integer value 1).
2690 if (cond->AsIntConstant()->IsTrue()) {
2691 if (true_target != nullptr) {
2692 __ B(true_target);
2693 }
2694 } else {
Anton Kirilov644032c2016-12-06 17:51:43 +00002695 DCHECK(cond->AsIntConstant()->IsFalse()) << Int32ConstantFrom(cond);
Scott Wakelingfe885462016-09-22 10:24:38 +01002696 if (false_target != nullptr) {
2697 __ B(false_target);
2698 }
2699 }
2700 return;
2701 }
2702
2703 // The following code generates these patterns:
2704 // (1) true_target == nullptr && false_target != nullptr
2705 // - opposite condition true => branch to false_target
2706 // (2) true_target != nullptr && false_target == nullptr
2707 // - condition true => branch to true_target
2708 // (3) true_target != nullptr && false_target != nullptr
2709 // - condition true => branch to true_target
2710 // - branch to false_target
2711 if (IsBooleanValueOrMaterializedCondition(cond)) {
2712 // Condition has been materialized, compare the output to 0.
2713 if (kIsDebugBuild) {
2714 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
2715 DCHECK(cond_val.IsRegister());
2716 }
2717 if (true_target == nullptr) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00002718 __ CompareAndBranchIfZero(InputRegisterAt(instruction, condition_input_index),
2719 false_target,
2720 far_target);
Scott Wakelingfe885462016-09-22 10:24:38 +01002721 } else {
xueliang.zhongf51bc622016-11-04 09:23:32 +00002722 __ CompareAndBranchIfNonZero(InputRegisterAt(instruction, condition_input_index),
2723 true_target,
2724 far_target);
Scott Wakelingfe885462016-09-22 10:24:38 +01002725 }
2726 } else {
2727 // Condition has not been materialized. Use its inputs as the comparison and
2728 // its condition as the branch condition.
2729 HCondition* condition = cond->AsCondition();
2730
2731 // If this is a long or FP comparison that has been folded into
2732 // the HCondition, generate the comparison directly.
2733 Primitive::Type type = condition->InputAt(0)->GetType();
2734 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
2735 GenerateCompareTestAndBranch(condition, true_target, false_target);
2736 return;
2737 }
2738
Donghui Bai426b49c2016-11-08 14:55:38 +08002739 vixl32::Label* non_fallthrough_target;
2740 vixl32::Condition arm_cond = vixl32::Condition::None();
2741 const vixl32::Register left = InputRegisterAt(cond, 0);
2742 const Operand right = InputOperandAt(cond, 1);
2743
Scott Wakelingfe885462016-09-22 10:24:38 +01002744 if (true_target == nullptr) {
Donghui Bai426b49c2016-11-08 14:55:38 +08002745 arm_cond = ARMCondition(condition->GetOppositeCondition());
2746 non_fallthrough_target = false_target;
Scott Wakelingfe885462016-09-22 10:24:38 +01002747 } else {
Donghui Bai426b49c2016-11-08 14:55:38 +08002748 arm_cond = ARMCondition(condition->GetCondition());
2749 non_fallthrough_target = true_target;
2750 }
2751
2752 if (right.IsImmediate() && right.GetImmediate() == 0 && (arm_cond.Is(ne) || arm_cond.Is(eq))) {
2753 if (arm_cond.Is(eq)) {
2754 __ CompareAndBranchIfZero(left, non_fallthrough_target);
2755 } else {
2756 DCHECK(arm_cond.Is(ne));
2757 __ CompareAndBranchIfNonZero(left, non_fallthrough_target);
2758 }
2759 } else {
2760 __ Cmp(left, right);
2761 __ B(arm_cond, non_fallthrough_target);
Scott Wakelingfe885462016-09-22 10:24:38 +01002762 }
2763 }
2764
2765 // If neither branch falls through (case 3), the conditional branch to `true_target`
2766 // was already emitted (case 2) and we need to emit a jump to `false_target`.
2767 if (true_target != nullptr && false_target != nullptr) {
2768 __ B(false_target);
2769 }
2770}
2771
2772void LocationsBuilderARMVIXL::VisitIf(HIf* if_instr) {
2773 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
2774 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
2775 locations->SetInAt(0, Location::RequiresRegister());
2776 }
2777}
2778
2779void InstructionCodeGeneratorARMVIXL::VisitIf(HIf* if_instr) {
2780 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
2781 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002782 vixl32::Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
2783 nullptr : codegen_->GetLabelOf(true_successor);
2784 vixl32::Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
2785 nullptr : codegen_->GetLabelOf(false_successor);
Scott Wakelingfe885462016-09-22 10:24:38 +01002786 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
2787}
2788
Scott Wakelingc34dba72016-10-03 10:14:44 +01002789void LocationsBuilderARMVIXL::VisitDeoptimize(HDeoptimize* deoptimize) {
2790 LocationSummary* locations = new (GetGraph()->GetArena())
2791 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01002792 InvokeRuntimeCallingConventionARMVIXL calling_convention;
2793 RegisterSet caller_saves = RegisterSet::Empty();
2794 caller_saves.Add(LocationFrom(calling_convention.GetRegisterAt(0)));
2795 locations->SetCustomSlowPathCallerSaves(caller_saves);
Scott Wakelingc34dba72016-10-03 10:14:44 +01002796 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
2797 locations->SetInAt(0, Location::RequiresRegister());
2798 }
2799}
2800
2801void InstructionCodeGeneratorARMVIXL::VisitDeoptimize(HDeoptimize* deoptimize) {
2802 SlowPathCodeARMVIXL* slow_path =
2803 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARMVIXL>(deoptimize);
2804 GenerateTestAndBranch(deoptimize,
2805 /* condition_input_index */ 0,
2806 slow_path->GetEntryLabel(),
2807 /* false_target */ nullptr);
2808}
2809
Artem Serovd4cc5b22016-11-04 11:19:09 +00002810void LocationsBuilderARMVIXL::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
2811 LocationSummary* locations = new (GetGraph()->GetArena())
2812 LocationSummary(flag, LocationSummary::kNoCall);
2813 locations->SetOut(Location::RequiresRegister());
2814}
2815
2816void InstructionCodeGeneratorARMVIXL::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
2817 GetAssembler()->LoadFromOffset(kLoadWord,
2818 OutputRegister(flag),
2819 sp,
2820 codegen_->GetStackOffsetOfShouldDeoptimizeFlag());
2821}
2822
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002823void LocationsBuilderARMVIXL::VisitSelect(HSelect* select) {
2824 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Donghui Bai426b49c2016-11-08 14:55:38 +08002825 const bool is_floating_point = Primitive::IsFloatingPointType(select->GetType());
2826
2827 if (is_floating_point) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002828 locations->SetInAt(0, Location::RequiresFpuRegister());
Donghui Bai426b49c2016-11-08 14:55:38 +08002829 locations->SetInAt(1, Location::FpuRegisterOrConstant(select->GetTrueValue()));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002830 } else {
2831 locations->SetInAt(0, Location::RequiresRegister());
Donghui Bai426b49c2016-11-08 14:55:38 +08002832 locations->SetInAt(1, Arm8BitEncodableConstantOrRegister(select->GetTrueValue()));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002833 }
Donghui Bai426b49c2016-11-08 14:55:38 +08002834
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002835 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
Donghui Bai426b49c2016-11-08 14:55:38 +08002836 locations->SetInAt(2, Location::RegisterOrConstant(select->GetCondition()));
2837 // The code generator handles overlap with the values, but not with the condition.
2838 locations->SetOut(Location::SameAsFirstInput());
2839 } else if (is_floating_point) {
2840 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2841 } else {
2842 if (!locations->InAt(1).IsConstant()) {
2843 locations->SetInAt(0, Arm8BitEncodableConstantOrRegister(select->GetFalseValue()));
2844 }
2845
2846 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002847 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002848}
2849
2850void InstructionCodeGeneratorARMVIXL::VisitSelect(HSelect* select) {
Donghui Bai426b49c2016-11-08 14:55:38 +08002851 HInstruction* const condition = select->GetCondition();
2852 const LocationSummary* const locations = select->GetLocations();
2853 const Primitive::Type type = select->GetType();
2854 const Location first = locations->InAt(0);
2855 const Location out = locations->Out();
2856 const Location second = locations->InAt(1);
2857 Location src;
2858
2859 if (condition->IsIntConstant()) {
2860 if (condition->AsIntConstant()->IsFalse()) {
2861 src = first;
2862 } else {
2863 src = second;
2864 }
2865
2866 codegen_->MoveLocation(out, src, type);
2867 return;
2868 }
2869
2870 if (!Primitive::IsFloatingPointType(type) &&
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002871 (IsBooleanValueOrMaterializedCondition(condition) ||
2872 CanGenerateTest(condition->AsCondition(), codegen_->GetAssembler()))) {
Donghui Bai426b49c2016-11-08 14:55:38 +08002873 bool invert = false;
2874
2875 if (out.Equals(second)) {
2876 src = first;
2877 invert = true;
2878 } else if (out.Equals(first)) {
2879 src = second;
2880 } else if (second.IsConstant()) {
2881 DCHECK(CanEncodeConstantAs8BitImmediate(second.GetConstant()));
2882 src = second;
2883 } else if (first.IsConstant()) {
2884 DCHECK(CanEncodeConstantAs8BitImmediate(first.GetConstant()));
2885 src = first;
2886 invert = true;
2887 } else {
2888 src = second;
2889 }
2890
2891 if (CanGenerateConditionalMove(out, src)) {
2892 if (!out.Equals(first) && !out.Equals(second)) {
2893 codegen_->MoveLocation(out, src.Equals(first) ? second : first, type);
2894 }
2895
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002896 std::pair<vixl32::Condition, vixl32::Condition> cond(eq, ne);
2897
2898 if (IsBooleanValueOrMaterializedCondition(condition)) {
2899 __ Cmp(InputRegisterAt(select, 2), 0);
2900 cond = invert ? std::make_pair(eq, ne) : std::make_pair(ne, eq);
2901 } else {
2902 cond = GenerateTest(condition->AsCondition(), invert, codegen_);
2903 }
2904
Donghui Bai426b49c2016-11-08 14:55:38 +08002905 const size_t instr_count = out.IsRegisterPair() ? 4 : 2;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002906 // We use the scope because of the IT block that follows.
Donghui Bai426b49c2016-11-08 14:55:38 +08002907 ExactAssemblyScope guard(GetVIXLAssembler(),
2908 instr_count * vixl32::k16BitT32InstructionSizeInBytes,
2909 CodeBufferCheckScope::kExactSize);
2910
2911 if (out.IsRegister()) {
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002912 __ it(cond.first);
2913 __ mov(cond.first, RegisterFrom(out), OperandFrom(src, type));
Donghui Bai426b49c2016-11-08 14:55:38 +08002914 } else {
2915 DCHECK(out.IsRegisterPair());
2916
2917 Operand operand_high(0);
2918 Operand operand_low(0);
2919
2920 if (src.IsConstant()) {
2921 const int64_t value = Int64ConstantFrom(src);
2922
2923 operand_high = High32Bits(value);
2924 operand_low = Low32Bits(value);
2925 } else {
2926 DCHECK(src.IsRegisterPair());
2927 operand_high = HighRegisterFrom(src);
2928 operand_low = LowRegisterFrom(src);
2929 }
2930
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002931 __ it(cond.first);
2932 __ mov(cond.first, LowRegisterFrom(out), operand_low);
2933 __ it(cond.first);
2934 __ mov(cond.first, HighRegisterFrom(out), operand_high);
Donghui Bai426b49c2016-11-08 14:55:38 +08002935 }
2936
2937 return;
2938 }
2939 }
2940
2941 vixl32::Label* false_target = nullptr;
2942 vixl32::Label* true_target = nullptr;
2943 vixl32::Label select_end;
2944 vixl32::Label* const target = codegen_->GetFinalLabel(select, &select_end);
2945
2946 if (out.Equals(second)) {
2947 true_target = target;
2948 src = first;
2949 } else {
2950 false_target = target;
2951 src = second;
2952
2953 if (!out.Equals(first)) {
2954 codegen_->MoveLocation(out, first, type);
2955 }
2956 }
2957
2958 GenerateTestAndBranch(select, 2, true_target, false_target, /* far_target */ false);
2959 codegen_->MoveLocation(out, src, type);
2960
2961 if (select_end.IsReferenced()) {
2962 __ Bind(&select_end);
2963 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002964}
2965
Artem Serov551b28f2016-10-18 19:11:30 +01002966void LocationsBuilderARMVIXL::VisitNativeDebugInfo(HNativeDebugInfo* info) {
2967 new (GetGraph()->GetArena()) LocationSummary(info);
2968}
2969
2970void InstructionCodeGeneratorARMVIXL::VisitNativeDebugInfo(HNativeDebugInfo*) {
2971 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
2972}
2973
Scott Wakelingfe885462016-09-22 10:24:38 +01002974void CodeGeneratorARMVIXL::GenerateNop() {
2975 __ Nop();
2976}
2977
2978void LocationsBuilderARMVIXL::HandleCondition(HCondition* cond) {
2979 LocationSummary* locations =
2980 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
2981 // Handle the long/FP comparisons made in instruction simplification.
2982 switch (cond->InputAt(0)->GetType()) {
2983 case Primitive::kPrimLong:
2984 locations->SetInAt(0, Location::RequiresRegister());
2985 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
2986 if (!cond->IsEmittedAtUseSite()) {
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002987 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Scott Wakelingfe885462016-09-22 10:24:38 +01002988 }
2989 break;
2990
Scott Wakelingfe885462016-09-22 10:24:38 +01002991 case Primitive::kPrimFloat:
2992 case Primitive::kPrimDouble:
2993 locations->SetInAt(0, Location::RequiresFpuRegister());
Artem Serov657022c2016-11-23 14:19:38 +00002994 locations->SetInAt(1, ArithmeticZeroOrFpuRegister(cond->InputAt(1)));
Scott Wakelingfe885462016-09-22 10:24:38 +01002995 if (!cond->IsEmittedAtUseSite()) {
2996 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2997 }
2998 break;
2999
3000 default:
3001 locations->SetInAt(0, Location::RequiresRegister());
3002 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
3003 if (!cond->IsEmittedAtUseSite()) {
3004 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3005 }
3006 }
3007}
3008
3009void InstructionCodeGeneratorARMVIXL::HandleCondition(HCondition* cond) {
3010 if (cond->IsEmittedAtUseSite()) {
3011 return;
3012 }
3013
Nicolas Geoffray30826612017-05-10 11:59:26 +00003014 const vixl32::Register out = OutputRegister(cond);
Scott Wakelingfe885462016-09-22 10:24:38 +01003015
Nicolas Geoffray30826612017-05-10 11:59:26 +00003016 if (out.IsLow() && CanGenerateTest(cond, codegen_->GetAssembler())) {
3017 const auto condition = GenerateTest(cond, false, codegen_);
3018 // We use the scope because of the IT block that follows.
3019 ExactAssemblyScope guard(GetVIXLAssembler(),
3020 4 * vixl32::k16BitT32InstructionSizeInBytes,
3021 CodeBufferCheckScope::kExactSize);
3022
3023 __ it(condition.first);
3024 __ mov(condition.first, out, 1);
3025 __ it(condition.second);
3026 __ mov(condition.second, out, 0);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003027 return;
Scott Wakelingfe885462016-09-22 10:24:38 +01003028 }
3029
Nicolas Geoffray30826612017-05-10 11:59:26 +00003030 // Convert the jumps into the result.
3031 vixl32::Label done_label;
3032 vixl32::Label* const final_label = codegen_->GetFinalLabel(cond, &done_label);
Scott Wakelingfe885462016-09-22 10:24:38 +01003033
Nicolas Geoffray30826612017-05-10 11:59:26 +00003034 if (cond->InputAt(0)->GetType() == Primitive::kPrimLong) {
3035 vixl32::Label true_label, false_label;
Scott Wakelingfe885462016-09-22 10:24:38 +01003036
Nicolas Geoffray30826612017-05-10 11:59:26 +00003037 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003038
Nicolas Geoffray30826612017-05-10 11:59:26 +00003039 // False case: result = 0.
3040 __ Bind(&false_label);
3041 __ Mov(out, 0);
3042 __ B(final_label);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003043
Nicolas Geoffray30826612017-05-10 11:59:26 +00003044 // True case: result = 1.
3045 __ Bind(&true_label);
3046 __ Mov(out, 1);
3047 } else {
3048 DCHECK(CanGenerateTest(cond, codegen_->GetAssembler()));
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003049
Nicolas Geoffray30826612017-05-10 11:59:26 +00003050 const auto condition = GenerateTest(cond, false, codegen_);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003051
Nicolas Geoffray30826612017-05-10 11:59:26 +00003052 __ Mov(LeaveFlags, out, 0);
3053 __ B(condition.second, final_label, /* far_target */ false);
3054 __ Mov(out, 1);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003055 }
Anton Kirilov6f644202017-02-27 18:29:45 +00003056
Nicolas Geoffray30826612017-05-10 11:59:26 +00003057 if (done_label.IsReferenced()) {
3058 __ Bind(&done_label);
3059 }
Scott Wakelingfe885462016-09-22 10:24:38 +01003060}
3061
3062void LocationsBuilderARMVIXL::VisitEqual(HEqual* comp) {
3063 HandleCondition(comp);
3064}
3065
3066void InstructionCodeGeneratorARMVIXL::VisitEqual(HEqual* comp) {
3067 HandleCondition(comp);
3068}
3069
3070void LocationsBuilderARMVIXL::VisitNotEqual(HNotEqual* comp) {
3071 HandleCondition(comp);
3072}
3073
3074void InstructionCodeGeneratorARMVIXL::VisitNotEqual(HNotEqual* comp) {
3075 HandleCondition(comp);
3076}
3077
3078void LocationsBuilderARMVIXL::VisitLessThan(HLessThan* comp) {
3079 HandleCondition(comp);
3080}
3081
3082void InstructionCodeGeneratorARMVIXL::VisitLessThan(HLessThan* comp) {
3083 HandleCondition(comp);
3084}
3085
3086void LocationsBuilderARMVIXL::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
3087 HandleCondition(comp);
3088}
3089
3090void InstructionCodeGeneratorARMVIXL::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
3091 HandleCondition(comp);
3092}
3093
3094void LocationsBuilderARMVIXL::VisitGreaterThan(HGreaterThan* comp) {
3095 HandleCondition(comp);
3096}
3097
3098void InstructionCodeGeneratorARMVIXL::VisitGreaterThan(HGreaterThan* comp) {
3099 HandleCondition(comp);
3100}
3101
3102void LocationsBuilderARMVIXL::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
3103 HandleCondition(comp);
3104}
3105
3106void InstructionCodeGeneratorARMVIXL::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
3107 HandleCondition(comp);
3108}
3109
3110void LocationsBuilderARMVIXL::VisitBelow(HBelow* comp) {
3111 HandleCondition(comp);
3112}
3113
3114void InstructionCodeGeneratorARMVIXL::VisitBelow(HBelow* comp) {
3115 HandleCondition(comp);
3116}
3117
3118void LocationsBuilderARMVIXL::VisitBelowOrEqual(HBelowOrEqual* comp) {
3119 HandleCondition(comp);
3120}
3121
3122void InstructionCodeGeneratorARMVIXL::VisitBelowOrEqual(HBelowOrEqual* comp) {
3123 HandleCondition(comp);
3124}
3125
3126void LocationsBuilderARMVIXL::VisitAbove(HAbove* comp) {
3127 HandleCondition(comp);
3128}
3129
3130void InstructionCodeGeneratorARMVIXL::VisitAbove(HAbove* comp) {
3131 HandleCondition(comp);
3132}
3133
3134void LocationsBuilderARMVIXL::VisitAboveOrEqual(HAboveOrEqual* comp) {
3135 HandleCondition(comp);
3136}
3137
3138void InstructionCodeGeneratorARMVIXL::VisitAboveOrEqual(HAboveOrEqual* comp) {
3139 HandleCondition(comp);
3140}
3141
3142void LocationsBuilderARMVIXL::VisitIntConstant(HIntConstant* constant) {
3143 LocationSummary* locations =
3144 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3145 locations->SetOut(Location::ConstantLocation(constant));
3146}
3147
3148void InstructionCodeGeneratorARMVIXL::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
3149 // Will be generated at use site.
3150}
3151
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003152void LocationsBuilderARMVIXL::VisitNullConstant(HNullConstant* constant) {
3153 LocationSummary* locations =
3154 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3155 locations->SetOut(Location::ConstantLocation(constant));
3156}
3157
3158void InstructionCodeGeneratorARMVIXL::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
3159 // Will be generated at use site.
3160}
3161
Scott Wakelingfe885462016-09-22 10:24:38 +01003162void LocationsBuilderARMVIXL::VisitLongConstant(HLongConstant* constant) {
3163 LocationSummary* locations =
3164 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3165 locations->SetOut(Location::ConstantLocation(constant));
3166}
3167
3168void InstructionCodeGeneratorARMVIXL::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
3169 // Will be generated at use site.
3170}
3171
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01003172void LocationsBuilderARMVIXL::VisitFloatConstant(HFloatConstant* constant) {
3173 LocationSummary* locations =
3174 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3175 locations->SetOut(Location::ConstantLocation(constant));
3176}
3177
Scott Wakelingc34dba72016-10-03 10:14:44 +01003178void InstructionCodeGeneratorARMVIXL::VisitFloatConstant(
3179 HFloatConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01003180 // Will be generated at use site.
3181}
3182
3183void LocationsBuilderARMVIXL::VisitDoubleConstant(HDoubleConstant* constant) {
3184 LocationSummary* locations =
3185 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3186 locations->SetOut(Location::ConstantLocation(constant));
3187}
3188
Scott Wakelingc34dba72016-10-03 10:14:44 +01003189void InstructionCodeGeneratorARMVIXL::VisitDoubleConstant(
3190 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01003191 // Will be generated at use site.
3192}
3193
Igor Murashkind01745e2017-04-05 16:40:31 -07003194void LocationsBuilderARMVIXL::VisitConstructorFence(HConstructorFence* constructor_fence) {
3195 constructor_fence->SetLocations(nullptr);
3196}
3197
3198void InstructionCodeGeneratorARMVIXL::VisitConstructorFence(
3199 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
3200 codegen_->GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
3201}
3202
Scott Wakelingfe885462016-09-22 10:24:38 +01003203void LocationsBuilderARMVIXL::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3204 memory_barrier->SetLocations(nullptr);
3205}
3206
3207void InstructionCodeGeneratorARMVIXL::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3208 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
3209}
3210
3211void LocationsBuilderARMVIXL::VisitReturnVoid(HReturnVoid* ret) {
3212 ret->SetLocations(nullptr);
3213}
3214
3215void InstructionCodeGeneratorARMVIXL::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
3216 codegen_->GenerateFrameExit();
3217}
3218
3219void LocationsBuilderARMVIXL::VisitReturn(HReturn* ret) {
3220 LocationSummary* locations =
3221 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
3222 locations->SetInAt(0, parameter_visitor_.GetReturnLocation(ret->InputAt(0)->GetType()));
3223}
3224
3225void InstructionCodeGeneratorARMVIXL::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
3226 codegen_->GenerateFrameExit();
3227}
3228
Artem Serovcfbe9132016-10-14 15:58:56 +01003229void LocationsBuilderARMVIXL::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3230 // The trampoline uses the same calling convention as dex calling conventions,
3231 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
3232 // the method_idx.
3233 HandleInvoke(invoke);
3234}
3235
3236void InstructionCodeGeneratorARMVIXL::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3237 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
3238}
3239
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003240void LocationsBuilderARMVIXL::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
3241 // Explicit clinit checks triggered by static invokes must have been pruned by
3242 // art::PrepareForRegisterAllocation.
3243 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
3244
Anton Kirilov5ec62182016-10-13 20:16:02 +01003245 IntrinsicLocationsBuilderARMVIXL intrinsic(codegen_);
3246 if (intrinsic.TryDispatch(invoke)) {
3247 if (invoke->GetLocations()->CanCall() && invoke->HasPcRelativeDexCache()) {
3248 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
3249 }
3250 return;
3251 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003252
3253 HandleInvoke(invoke);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01003254
Artem Serovd4cc5b22016-11-04 11:19:09 +00003255 // For PC-relative dex cache the invoke has an extra input, the PC-relative address base.
3256 if (invoke->HasPcRelativeDexCache()) {
3257 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
3258 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003259}
3260
Anton Kirilov5ec62182016-10-13 20:16:02 +01003261static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARMVIXL* codegen) {
3262 if (invoke->GetLocations()->Intrinsified()) {
3263 IntrinsicCodeGeneratorARMVIXL intrinsic(codegen);
3264 intrinsic.Dispatch(invoke);
3265 return true;
3266 }
3267 return false;
3268}
3269
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003270void InstructionCodeGeneratorARMVIXL::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
3271 // Explicit clinit checks triggered by static invokes must have been pruned by
3272 // art::PrepareForRegisterAllocation.
3273 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
3274
Anton Kirilov5ec62182016-10-13 20:16:02 +01003275 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3276 return;
3277 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003278
3279 LocationSummary* locations = invoke->GetLocations();
Artem Serovd4cc5b22016-11-04 11:19:09 +00003280 codegen_->GenerateStaticOrDirectCall(
3281 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003282 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3283}
3284
3285void LocationsBuilderARMVIXL::HandleInvoke(HInvoke* invoke) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00003286 InvokeDexCallingConventionVisitorARMVIXL calling_convention_visitor;
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003287 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
3288}
3289
3290void LocationsBuilderARMVIXL::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Anton Kirilov5ec62182016-10-13 20:16:02 +01003291 IntrinsicLocationsBuilderARMVIXL intrinsic(codegen_);
3292 if (intrinsic.TryDispatch(invoke)) {
3293 return;
3294 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003295
3296 HandleInvoke(invoke);
3297}
3298
3299void InstructionCodeGeneratorARMVIXL::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Anton Kirilov5ec62182016-10-13 20:16:02 +01003300 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3301 return;
3302 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003303
3304 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003305 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Alexandre Rames374ddf32016-11-04 10:40:49 +00003306 DCHECK(!codegen_->IsLeafMethod());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003307}
3308
Artem Serovcfbe9132016-10-14 15:58:56 +01003309void LocationsBuilderARMVIXL::VisitInvokeInterface(HInvokeInterface* invoke) {
3310 HandleInvoke(invoke);
3311 // Add the hidden argument.
3312 invoke->GetLocations()->AddTemp(LocationFrom(r12));
3313}
3314
3315void InstructionCodeGeneratorARMVIXL::VisitInvokeInterface(HInvokeInterface* invoke) {
3316 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
3317 LocationSummary* locations = invoke->GetLocations();
3318 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
3319 vixl32::Register hidden_reg = RegisterFrom(locations->GetTemp(1));
3320 Location receiver = locations->InAt(0);
3321 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3322
3323 DCHECK(!receiver.IsStackSlot());
3324
Alexandre Rames374ddf32016-11-04 10:40:49 +00003325 // Ensure the pc position is recorded immediately after the `ldr` instruction.
3326 {
Artem Serov0fb37192016-12-06 18:13:40 +00003327 ExactAssemblyScope aas(GetVIXLAssembler(),
3328 vixl32::kMaxInstructionSizeInBytes,
3329 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00003330 // /* HeapReference<Class> */ temp = receiver->klass_
3331 __ ldr(temp, MemOperand(RegisterFrom(receiver), class_offset));
3332 codegen_->MaybeRecordImplicitNullCheck(invoke);
3333 }
Artem Serovcfbe9132016-10-14 15:58:56 +01003334 // Instead of simply (possibly) unpoisoning `temp` here, we should
3335 // emit a read barrier for the previous class reference load.
3336 // However this is not required in practice, as this is an
3337 // intermediate/temporary reference and because the current
3338 // concurrent copying collector keeps the from-space memory
3339 // intact/accessible until the end of the marking phase (the
3340 // concurrent copying collector may not in the future).
3341 GetAssembler()->MaybeUnpoisonHeapReference(temp);
3342 GetAssembler()->LoadFromOffset(kLoadWord,
3343 temp,
3344 temp,
3345 mirror::Class::ImtPtrOffset(kArmPointerSize).Uint32Value());
3346 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
3347 invoke->GetImtIndex(), kArmPointerSize));
3348 // temp = temp->GetImtEntryAt(method_offset);
3349 GetAssembler()->LoadFromOffset(kLoadWord, temp, temp, method_offset);
3350 uint32_t entry_point =
3351 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize).Int32Value();
3352 // LR = temp->GetEntryPoint();
3353 GetAssembler()->LoadFromOffset(kLoadWord, lr, temp, entry_point);
3354
3355 // Set the hidden (in r12) argument. It is done here, right before a BLX to prevent other
3356 // instruction from clobbering it as they might use r12 as a scratch register.
3357 DCHECK(hidden_reg.Is(r12));
Scott Wakelingb77051e2016-11-21 19:46:00 +00003358
3359 {
3360 // The VIXL macro assembler may clobber any of the scratch registers that are available to it,
3361 // so it checks if the application is using them (by passing them to the macro assembler
3362 // methods). The following application of UseScratchRegisterScope corrects VIXL's notion of
3363 // what is available, and is the opposite of the standard usage: Instead of requesting a
3364 // temporary location, it imposes an external constraint (i.e. a specific register is reserved
3365 // for the hidden argument). Note that this works even if VIXL needs a scratch register itself
3366 // (to materialize the constant), since the destination register becomes available for such use
3367 // internally for the duration of the macro instruction.
3368 UseScratchRegisterScope temps(GetVIXLAssembler());
3369 temps.Exclude(hidden_reg);
3370 __ Mov(hidden_reg, invoke->GetDexMethodIndex());
3371 }
Artem Serovcfbe9132016-10-14 15:58:56 +01003372 {
Alexandre Rames374ddf32016-11-04 10:40:49 +00003373 // Ensure the pc position is recorded immediately after the `blx` instruction.
3374 // 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 +00003375 ExactAssemblyScope aas(GetVIXLAssembler(),
Alexandre Rames374ddf32016-11-04 10:40:49 +00003376 vixl32::k16BitT32InstructionSizeInBytes,
3377 CodeBufferCheckScope::kExactSize);
Artem Serovcfbe9132016-10-14 15:58:56 +01003378 // LR();
3379 __ blx(lr);
Artem Serovcfbe9132016-10-14 15:58:56 +01003380 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Alexandre Rames374ddf32016-11-04 10:40:49 +00003381 DCHECK(!codegen_->IsLeafMethod());
Artem Serovcfbe9132016-10-14 15:58:56 +01003382 }
3383}
3384
Orion Hodsonac141392017-01-13 11:53:47 +00003385void LocationsBuilderARMVIXL::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
3386 HandleInvoke(invoke);
3387}
3388
3389void InstructionCodeGeneratorARMVIXL::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
3390 codegen_->GenerateInvokePolymorphicCall(invoke);
3391}
3392
Artem Serov02109dd2016-09-23 17:17:54 +01003393void LocationsBuilderARMVIXL::VisitNeg(HNeg* neg) {
3394 LocationSummary* locations =
3395 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
3396 switch (neg->GetResultType()) {
3397 case Primitive::kPrimInt: {
3398 locations->SetInAt(0, Location::RequiresRegister());
3399 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3400 break;
3401 }
3402 case Primitive::kPrimLong: {
3403 locations->SetInAt(0, Location::RequiresRegister());
3404 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3405 break;
3406 }
3407
3408 case Primitive::kPrimFloat:
3409 case Primitive::kPrimDouble:
3410 locations->SetInAt(0, Location::RequiresFpuRegister());
3411 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3412 break;
3413
3414 default:
3415 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
3416 }
3417}
3418
3419void InstructionCodeGeneratorARMVIXL::VisitNeg(HNeg* neg) {
3420 LocationSummary* locations = neg->GetLocations();
3421 Location out = locations->Out();
3422 Location in = locations->InAt(0);
3423 switch (neg->GetResultType()) {
3424 case Primitive::kPrimInt:
3425 __ Rsb(OutputRegister(neg), InputRegisterAt(neg, 0), 0);
3426 break;
3427
3428 case Primitive::kPrimLong:
3429 // out.lo = 0 - in.lo (and update the carry/borrow (C) flag)
3430 __ Rsbs(LowRegisterFrom(out), LowRegisterFrom(in), 0);
3431 // We cannot emit an RSC (Reverse Subtract with Carry)
3432 // instruction here, as it does not exist in the Thumb-2
3433 // instruction set. We use the following approach
3434 // using SBC and SUB instead.
3435 //
3436 // out.hi = -C
3437 __ Sbc(HighRegisterFrom(out), HighRegisterFrom(out), HighRegisterFrom(out));
3438 // out.hi = out.hi - in.hi
3439 __ Sub(HighRegisterFrom(out), HighRegisterFrom(out), HighRegisterFrom(in));
3440 break;
3441
3442 case Primitive::kPrimFloat:
3443 case Primitive::kPrimDouble:
Anton Kirilov644032c2016-12-06 17:51:43 +00003444 __ Vneg(OutputVRegister(neg), InputVRegister(neg));
Artem Serov02109dd2016-09-23 17:17:54 +01003445 break;
3446
3447 default:
3448 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
3449 }
3450}
3451
Scott Wakelingfe885462016-09-22 10:24:38 +01003452void LocationsBuilderARMVIXL::VisitTypeConversion(HTypeConversion* conversion) {
3453 Primitive::Type result_type = conversion->GetResultType();
3454 Primitive::Type input_type = conversion->GetInputType();
3455 DCHECK_NE(result_type, input_type);
3456
3457 // The float-to-long, double-to-long and long-to-float type conversions
3458 // rely on a call to the runtime.
3459 LocationSummary::CallKind call_kind =
3460 (((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
3461 && result_type == Primitive::kPrimLong)
3462 || (input_type == Primitive::kPrimLong && result_type == Primitive::kPrimFloat))
3463 ? LocationSummary::kCallOnMainOnly
3464 : LocationSummary::kNoCall;
3465 LocationSummary* locations =
3466 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
3467
3468 // The Java language does not allow treating boolean as an integral type but
3469 // our bit representation makes it safe.
3470
3471 switch (result_type) {
3472 case Primitive::kPrimByte:
3473 switch (input_type) {
3474 case Primitive::kPrimLong:
3475 // Type conversion from long to byte is a result of code transformations.
3476 case Primitive::kPrimBoolean:
3477 // Boolean input is a result of code transformations.
3478 case Primitive::kPrimShort:
3479 case Primitive::kPrimInt:
3480 case Primitive::kPrimChar:
3481 // Processing a Dex `int-to-byte' instruction.
3482 locations->SetInAt(0, Location::RequiresRegister());
3483 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3484 break;
3485
3486 default:
3487 LOG(FATAL) << "Unexpected type conversion from " << input_type
3488 << " to " << result_type;
3489 }
3490 break;
3491
3492 case Primitive::kPrimShort:
3493 switch (input_type) {
3494 case Primitive::kPrimLong:
3495 // Type conversion from long to short is a result of code transformations.
3496 case Primitive::kPrimBoolean:
3497 // Boolean input is a result of code transformations.
3498 case Primitive::kPrimByte:
3499 case Primitive::kPrimInt:
3500 case Primitive::kPrimChar:
3501 // Processing a Dex `int-to-short' instruction.
3502 locations->SetInAt(0, Location::RequiresRegister());
3503 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3504 break;
3505
3506 default:
3507 LOG(FATAL) << "Unexpected type conversion from " << input_type
3508 << " to " << result_type;
3509 }
3510 break;
3511
3512 case Primitive::kPrimInt:
3513 switch (input_type) {
3514 case Primitive::kPrimLong:
3515 // Processing a Dex `long-to-int' instruction.
3516 locations->SetInAt(0, Location::Any());
3517 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3518 break;
3519
3520 case Primitive::kPrimFloat:
3521 // Processing a Dex `float-to-int' instruction.
3522 locations->SetInAt(0, Location::RequiresFpuRegister());
3523 locations->SetOut(Location::RequiresRegister());
3524 locations->AddTemp(Location::RequiresFpuRegister());
3525 break;
3526
3527 case Primitive::kPrimDouble:
3528 // Processing a Dex `double-to-int' instruction.
3529 locations->SetInAt(0, Location::RequiresFpuRegister());
3530 locations->SetOut(Location::RequiresRegister());
3531 locations->AddTemp(Location::RequiresFpuRegister());
3532 break;
3533
3534 default:
3535 LOG(FATAL) << "Unexpected type conversion from " << input_type
3536 << " to " << result_type;
3537 }
3538 break;
3539
3540 case Primitive::kPrimLong:
3541 switch (input_type) {
3542 case Primitive::kPrimBoolean:
3543 // Boolean input is a result of code transformations.
3544 case Primitive::kPrimByte:
3545 case Primitive::kPrimShort:
3546 case Primitive::kPrimInt:
3547 case Primitive::kPrimChar:
3548 // Processing a Dex `int-to-long' instruction.
3549 locations->SetInAt(0, Location::RequiresRegister());
3550 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3551 break;
3552
3553 case Primitive::kPrimFloat: {
3554 // Processing a Dex `float-to-long' instruction.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003555 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3556 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
3557 locations->SetOut(LocationFrom(r0, r1));
Scott Wakelingfe885462016-09-22 10:24:38 +01003558 break;
3559 }
3560
3561 case Primitive::kPrimDouble: {
3562 // Processing a Dex `double-to-long' instruction.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003563 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3564 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0),
3565 calling_convention.GetFpuRegisterAt(1)));
3566 locations->SetOut(LocationFrom(r0, r1));
Scott Wakelingfe885462016-09-22 10:24:38 +01003567 break;
3568 }
3569
3570 default:
3571 LOG(FATAL) << "Unexpected type conversion from " << input_type
3572 << " to " << result_type;
3573 }
3574 break;
3575
3576 case Primitive::kPrimChar:
3577 switch (input_type) {
3578 case Primitive::kPrimLong:
3579 // Type conversion from long to char is a result of code transformations.
3580 case Primitive::kPrimBoolean:
3581 // Boolean input is a result of code transformations.
3582 case Primitive::kPrimByte:
3583 case Primitive::kPrimShort:
3584 case Primitive::kPrimInt:
3585 // Processing a Dex `int-to-char' instruction.
3586 locations->SetInAt(0, Location::RequiresRegister());
3587 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3588 break;
3589
3590 default:
3591 LOG(FATAL) << "Unexpected type conversion from " << input_type
3592 << " to " << result_type;
3593 }
3594 break;
3595
3596 case Primitive::kPrimFloat:
3597 switch (input_type) {
3598 case Primitive::kPrimBoolean:
3599 // Boolean input is a result of code transformations.
3600 case Primitive::kPrimByte:
3601 case Primitive::kPrimShort:
3602 case Primitive::kPrimInt:
3603 case Primitive::kPrimChar:
3604 // Processing a Dex `int-to-float' instruction.
3605 locations->SetInAt(0, Location::RequiresRegister());
3606 locations->SetOut(Location::RequiresFpuRegister());
3607 break;
3608
3609 case Primitive::kPrimLong: {
3610 // Processing a Dex `long-to-float' instruction.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003611 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3612 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0),
3613 calling_convention.GetRegisterAt(1)));
3614 locations->SetOut(LocationFrom(calling_convention.GetFpuRegisterAt(0)));
Scott Wakelingfe885462016-09-22 10:24:38 +01003615 break;
3616 }
3617
3618 case Primitive::kPrimDouble:
3619 // Processing a Dex `double-to-float' instruction.
3620 locations->SetInAt(0, Location::RequiresFpuRegister());
3621 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3622 break;
3623
3624 default:
3625 LOG(FATAL) << "Unexpected type conversion from " << input_type
3626 << " to " << result_type;
3627 };
3628 break;
3629
3630 case Primitive::kPrimDouble:
3631 switch (input_type) {
3632 case Primitive::kPrimBoolean:
3633 // Boolean input is a result of code transformations.
3634 case Primitive::kPrimByte:
3635 case Primitive::kPrimShort:
3636 case Primitive::kPrimInt:
3637 case Primitive::kPrimChar:
3638 // Processing a Dex `int-to-double' instruction.
3639 locations->SetInAt(0, Location::RequiresRegister());
3640 locations->SetOut(Location::RequiresFpuRegister());
3641 break;
3642
3643 case Primitive::kPrimLong:
3644 // Processing a Dex `long-to-double' instruction.
3645 locations->SetInAt(0, Location::RequiresRegister());
3646 locations->SetOut(Location::RequiresFpuRegister());
3647 locations->AddTemp(Location::RequiresFpuRegister());
3648 locations->AddTemp(Location::RequiresFpuRegister());
3649 break;
3650
3651 case Primitive::kPrimFloat:
3652 // Processing a Dex `float-to-double' instruction.
3653 locations->SetInAt(0, Location::RequiresFpuRegister());
3654 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3655 break;
3656
3657 default:
3658 LOG(FATAL) << "Unexpected type conversion from " << input_type
3659 << " to " << result_type;
3660 };
3661 break;
3662
3663 default:
3664 LOG(FATAL) << "Unexpected type conversion from " << input_type
3665 << " to " << result_type;
3666 }
3667}
3668
3669void InstructionCodeGeneratorARMVIXL::VisitTypeConversion(HTypeConversion* conversion) {
3670 LocationSummary* locations = conversion->GetLocations();
3671 Location out = locations->Out();
3672 Location in = locations->InAt(0);
3673 Primitive::Type result_type = conversion->GetResultType();
3674 Primitive::Type input_type = conversion->GetInputType();
3675 DCHECK_NE(result_type, input_type);
3676 switch (result_type) {
3677 case Primitive::kPrimByte:
3678 switch (input_type) {
3679 case Primitive::kPrimLong:
3680 // Type conversion from long to byte is a result of code transformations.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003681 __ Sbfx(OutputRegister(conversion), LowRegisterFrom(in), 0, 8);
Scott Wakelingfe885462016-09-22 10:24:38 +01003682 break;
3683 case Primitive::kPrimBoolean:
3684 // Boolean input is a result of code transformations.
3685 case Primitive::kPrimShort:
3686 case Primitive::kPrimInt:
3687 case Primitive::kPrimChar:
3688 // Processing a Dex `int-to-byte' instruction.
3689 __ Sbfx(OutputRegister(conversion), InputRegisterAt(conversion, 0), 0, 8);
3690 break;
3691
3692 default:
3693 LOG(FATAL) << "Unexpected type conversion from " << input_type
3694 << " to " << result_type;
3695 }
3696 break;
3697
3698 case Primitive::kPrimShort:
3699 switch (input_type) {
3700 case Primitive::kPrimLong:
3701 // Type conversion from long to short is a result of code transformations.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003702 __ Sbfx(OutputRegister(conversion), LowRegisterFrom(in), 0, 16);
Scott Wakelingfe885462016-09-22 10:24:38 +01003703 break;
3704 case Primitive::kPrimBoolean:
3705 // Boolean input is a result of code transformations.
3706 case Primitive::kPrimByte:
3707 case Primitive::kPrimInt:
3708 case Primitive::kPrimChar:
3709 // Processing a Dex `int-to-short' instruction.
3710 __ Sbfx(OutputRegister(conversion), InputRegisterAt(conversion, 0), 0, 16);
3711 break;
3712
3713 default:
3714 LOG(FATAL) << "Unexpected type conversion from " << input_type
3715 << " to " << result_type;
3716 }
3717 break;
3718
3719 case Primitive::kPrimInt:
3720 switch (input_type) {
3721 case Primitive::kPrimLong:
3722 // Processing a Dex `long-to-int' instruction.
3723 DCHECK(out.IsRegister());
3724 if (in.IsRegisterPair()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003725 __ Mov(OutputRegister(conversion), LowRegisterFrom(in));
Scott Wakelingfe885462016-09-22 10:24:38 +01003726 } else if (in.IsDoubleStackSlot()) {
3727 GetAssembler()->LoadFromOffset(kLoadWord,
3728 OutputRegister(conversion),
3729 sp,
3730 in.GetStackIndex());
3731 } else {
3732 DCHECK(in.IsConstant());
3733 DCHECK(in.GetConstant()->IsLongConstant());
Vladimir Markoba1a48e2017-04-13 11:50:14 +01003734 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
3735 __ Mov(OutputRegister(conversion), static_cast<int32_t>(value));
Scott Wakelingfe885462016-09-22 10:24:38 +01003736 }
3737 break;
3738
3739 case Primitive::kPrimFloat: {
3740 // Processing a Dex `float-to-int' instruction.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003741 vixl32::SRegister temp = LowSRegisterFrom(locations->GetTemp(0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01003742 __ Vcvt(S32, F32, temp, InputSRegisterAt(conversion, 0));
Scott Wakelingfe885462016-09-22 10:24:38 +01003743 __ Vmov(OutputRegister(conversion), temp);
3744 break;
3745 }
3746
3747 case Primitive::kPrimDouble: {
3748 // Processing a Dex `double-to-int' instruction.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003749 vixl32::SRegister temp_s = LowSRegisterFrom(locations->GetTemp(0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01003750 __ Vcvt(S32, F64, temp_s, DRegisterFrom(in));
Scott Wakelingfe885462016-09-22 10:24:38 +01003751 __ Vmov(OutputRegister(conversion), temp_s);
3752 break;
3753 }
3754
3755 default:
3756 LOG(FATAL) << "Unexpected type conversion from " << input_type
3757 << " to " << result_type;
3758 }
3759 break;
3760
3761 case Primitive::kPrimLong:
3762 switch (input_type) {
3763 case Primitive::kPrimBoolean:
3764 // Boolean input is a result of code transformations.
3765 case Primitive::kPrimByte:
3766 case Primitive::kPrimShort:
3767 case Primitive::kPrimInt:
3768 case Primitive::kPrimChar:
3769 // Processing a Dex `int-to-long' instruction.
3770 DCHECK(out.IsRegisterPair());
3771 DCHECK(in.IsRegister());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003772 __ Mov(LowRegisterFrom(out), InputRegisterAt(conversion, 0));
Scott Wakelingfe885462016-09-22 10:24:38 +01003773 // Sign extension.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003774 __ Asr(HighRegisterFrom(out), LowRegisterFrom(out), 31);
Scott Wakelingfe885462016-09-22 10:24:38 +01003775 break;
3776
3777 case Primitive::kPrimFloat:
3778 // Processing a Dex `float-to-long' instruction.
3779 codegen_->InvokeRuntime(kQuickF2l, conversion, conversion->GetDexPc());
3780 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
3781 break;
3782
3783 case Primitive::kPrimDouble:
3784 // Processing a Dex `double-to-long' instruction.
3785 codegen_->InvokeRuntime(kQuickD2l, conversion, conversion->GetDexPc());
3786 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
3787 break;
3788
3789 default:
3790 LOG(FATAL) << "Unexpected type conversion from " << input_type
3791 << " to " << result_type;
3792 }
3793 break;
3794
3795 case Primitive::kPrimChar:
3796 switch (input_type) {
3797 case Primitive::kPrimLong:
3798 // Type conversion from long to char is a result of code transformations.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003799 __ Ubfx(OutputRegister(conversion), LowRegisterFrom(in), 0, 16);
Scott Wakelingfe885462016-09-22 10:24:38 +01003800 break;
3801 case Primitive::kPrimBoolean:
3802 // Boolean input is a result of code transformations.
3803 case Primitive::kPrimByte:
3804 case Primitive::kPrimShort:
3805 case Primitive::kPrimInt:
3806 // Processing a Dex `int-to-char' instruction.
3807 __ Ubfx(OutputRegister(conversion), InputRegisterAt(conversion, 0), 0, 16);
3808 break;
3809
3810 default:
3811 LOG(FATAL) << "Unexpected type conversion from " << input_type
3812 << " to " << result_type;
3813 }
3814 break;
3815
3816 case Primitive::kPrimFloat:
3817 switch (input_type) {
3818 case Primitive::kPrimBoolean:
3819 // Boolean input is a result of code transformations.
3820 case Primitive::kPrimByte:
3821 case Primitive::kPrimShort:
3822 case Primitive::kPrimInt:
3823 case Primitive::kPrimChar: {
3824 // Processing a Dex `int-to-float' instruction.
3825 __ Vmov(OutputSRegister(conversion), InputRegisterAt(conversion, 0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01003826 __ Vcvt(F32, S32, OutputSRegister(conversion), OutputSRegister(conversion));
Scott Wakelingfe885462016-09-22 10:24:38 +01003827 break;
3828 }
3829
3830 case Primitive::kPrimLong:
3831 // Processing a Dex `long-to-float' instruction.
3832 codegen_->InvokeRuntime(kQuickL2f, conversion, conversion->GetDexPc());
3833 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
3834 break;
3835
3836 case Primitive::kPrimDouble:
3837 // Processing a Dex `double-to-float' instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01003838 __ Vcvt(F32, F64, OutputSRegister(conversion), DRegisterFrom(in));
Scott Wakelingfe885462016-09-22 10:24:38 +01003839 break;
3840
3841 default:
3842 LOG(FATAL) << "Unexpected type conversion from " << input_type
3843 << " to " << result_type;
3844 };
3845 break;
3846
3847 case Primitive::kPrimDouble:
3848 switch (input_type) {
3849 case Primitive::kPrimBoolean:
3850 // Boolean input is a result of code transformations.
3851 case Primitive::kPrimByte:
3852 case Primitive::kPrimShort:
3853 case Primitive::kPrimInt:
3854 case Primitive::kPrimChar: {
3855 // Processing a Dex `int-to-double' instruction.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003856 __ Vmov(LowSRegisterFrom(out), InputRegisterAt(conversion, 0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01003857 __ Vcvt(F64, S32, DRegisterFrom(out), LowSRegisterFrom(out));
Scott Wakelingfe885462016-09-22 10:24:38 +01003858 break;
3859 }
3860
3861 case Primitive::kPrimLong: {
3862 // Processing a Dex `long-to-double' instruction.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003863 vixl32::Register low = LowRegisterFrom(in);
3864 vixl32::Register high = HighRegisterFrom(in);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003865 vixl32::SRegister out_s = LowSRegisterFrom(out);
Scott Wakelingc34dba72016-10-03 10:14:44 +01003866 vixl32::DRegister out_d = DRegisterFrom(out);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003867 vixl32::SRegister temp_s = LowSRegisterFrom(locations->GetTemp(0));
Scott Wakelingc34dba72016-10-03 10:14:44 +01003868 vixl32::DRegister temp_d = DRegisterFrom(locations->GetTemp(0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01003869 vixl32::DRegister constant_d = DRegisterFrom(locations->GetTemp(1));
Scott Wakelingfe885462016-09-22 10:24:38 +01003870
3871 // temp_d = int-to-double(high)
3872 __ Vmov(temp_s, high);
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01003873 __ Vcvt(F64, S32, temp_d, temp_s);
Scott Wakelingfe885462016-09-22 10:24:38 +01003874 // constant_d = k2Pow32EncodingForDouble
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003875 __ Vmov(constant_d, bit_cast<double, int64_t>(k2Pow32EncodingForDouble));
Scott Wakelingfe885462016-09-22 10:24:38 +01003876 // out_d = unsigned-to-double(low)
3877 __ Vmov(out_s, low);
3878 __ Vcvt(F64, U32, out_d, out_s);
3879 // out_d += temp_d * constant_d
3880 __ Vmla(F64, out_d, temp_d, constant_d);
3881 break;
3882 }
3883
3884 case Primitive::kPrimFloat:
3885 // Processing a Dex `float-to-double' instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01003886 __ Vcvt(F64, F32, DRegisterFrom(out), InputSRegisterAt(conversion, 0));
Scott Wakelingfe885462016-09-22 10:24:38 +01003887 break;
3888
3889 default:
3890 LOG(FATAL) << "Unexpected type conversion from " << input_type
3891 << " to " << result_type;
3892 };
3893 break;
3894
3895 default:
3896 LOG(FATAL) << "Unexpected type conversion from " << input_type
3897 << " to " << result_type;
3898 }
3899}
3900
3901void LocationsBuilderARMVIXL::VisitAdd(HAdd* add) {
3902 LocationSummary* locations =
3903 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
3904 switch (add->GetResultType()) {
3905 case Primitive::kPrimInt: {
3906 locations->SetInAt(0, Location::RequiresRegister());
3907 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
3908 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3909 break;
3910 }
3911
Scott Wakelingfe885462016-09-22 10:24:38 +01003912 case Primitive::kPrimLong: {
3913 locations->SetInAt(0, Location::RequiresRegister());
Anton Kirilovdda43962016-11-21 19:55:20 +00003914 locations->SetInAt(1, ArmEncodableConstantOrRegister(add->InputAt(1), ADD));
Scott Wakelingfe885462016-09-22 10:24:38 +01003915 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3916 break;
3917 }
3918
3919 case Primitive::kPrimFloat:
3920 case Primitive::kPrimDouble: {
3921 locations->SetInAt(0, Location::RequiresFpuRegister());
3922 locations->SetInAt(1, Location::RequiresFpuRegister());
3923 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3924 break;
3925 }
3926
3927 default:
3928 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
3929 }
3930}
3931
3932void InstructionCodeGeneratorARMVIXL::VisitAdd(HAdd* add) {
3933 LocationSummary* locations = add->GetLocations();
3934 Location out = locations->Out();
3935 Location first = locations->InAt(0);
3936 Location second = locations->InAt(1);
3937
3938 switch (add->GetResultType()) {
3939 case Primitive::kPrimInt: {
3940 __ Add(OutputRegister(add), InputRegisterAt(add, 0), InputOperandAt(add, 1));
3941 }
3942 break;
3943
Scott Wakelingfe885462016-09-22 10:24:38 +01003944 case Primitive::kPrimLong: {
Anton Kirilovdda43962016-11-21 19:55:20 +00003945 if (second.IsConstant()) {
3946 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
3947 GenerateAddLongConst(out, first, value);
3948 } else {
3949 DCHECK(second.IsRegisterPair());
3950 __ Adds(LowRegisterFrom(out), LowRegisterFrom(first), LowRegisterFrom(second));
3951 __ Adc(HighRegisterFrom(out), HighRegisterFrom(first), HighRegisterFrom(second));
3952 }
Scott Wakelingfe885462016-09-22 10:24:38 +01003953 break;
3954 }
3955
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003956 case Primitive::kPrimFloat:
Scott Wakelingfe885462016-09-22 10:24:38 +01003957 case Primitive::kPrimDouble:
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003958 __ Vadd(OutputVRegister(add), InputVRegisterAt(add, 0), InputVRegisterAt(add, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01003959 break;
3960
3961 default:
3962 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
3963 }
3964}
3965
3966void LocationsBuilderARMVIXL::VisitSub(HSub* sub) {
3967 LocationSummary* locations =
3968 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
3969 switch (sub->GetResultType()) {
3970 case Primitive::kPrimInt: {
3971 locations->SetInAt(0, Location::RequiresRegister());
3972 locations->SetInAt(1, Location::RegisterOrConstant(sub->InputAt(1)));
3973 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3974 break;
3975 }
3976
Scott Wakelingfe885462016-09-22 10:24:38 +01003977 case Primitive::kPrimLong: {
3978 locations->SetInAt(0, Location::RequiresRegister());
Anton Kirilovdda43962016-11-21 19:55:20 +00003979 locations->SetInAt(1, ArmEncodableConstantOrRegister(sub->InputAt(1), SUB));
Scott Wakelingfe885462016-09-22 10:24:38 +01003980 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3981 break;
3982 }
3983 case Primitive::kPrimFloat:
3984 case Primitive::kPrimDouble: {
3985 locations->SetInAt(0, Location::RequiresFpuRegister());
3986 locations->SetInAt(1, Location::RequiresFpuRegister());
3987 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3988 break;
3989 }
3990 default:
3991 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
3992 }
3993}
3994
3995void InstructionCodeGeneratorARMVIXL::VisitSub(HSub* sub) {
3996 LocationSummary* locations = sub->GetLocations();
3997 Location out = locations->Out();
3998 Location first = locations->InAt(0);
3999 Location second = locations->InAt(1);
4000 switch (sub->GetResultType()) {
4001 case Primitive::kPrimInt: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004002 __ Sub(OutputRegister(sub), InputRegisterAt(sub, 0), InputOperandAt(sub, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004003 break;
4004 }
4005
Scott Wakelingfe885462016-09-22 10:24:38 +01004006 case Primitive::kPrimLong: {
Anton Kirilovdda43962016-11-21 19:55:20 +00004007 if (second.IsConstant()) {
4008 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
4009 GenerateAddLongConst(out, first, -value);
4010 } else {
4011 DCHECK(second.IsRegisterPair());
4012 __ Subs(LowRegisterFrom(out), LowRegisterFrom(first), LowRegisterFrom(second));
4013 __ Sbc(HighRegisterFrom(out), HighRegisterFrom(first), HighRegisterFrom(second));
4014 }
Scott Wakelingfe885462016-09-22 10:24:38 +01004015 break;
4016 }
4017
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004018 case Primitive::kPrimFloat:
4019 case Primitive::kPrimDouble:
4020 __ Vsub(OutputVRegister(sub), InputVRegisterAt(sub, 0), InputVRegisterAt(sub, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004021 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01004022
4023 default:
4024 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
4025 }
4026}
4027
4028void LocationsBuilderARMVIXL::VisitMul(HMul* mul) {
4029 LocationSummary* locations =
4030 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
4031 switch (mul->GetResultType()) {
4032 case Primitive::kPrimInt:
4033 case Primitive::kPrimLong: {
4034 locations->SetInAt(0, Location::RequiresRegister());
4035 locations->SetInAt(1, Location::RequiresRegister());
4036 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4037 break;
4038 }
4039
4040 case Primitive::kPrimFloat:
4041 case Primitive::kPrimDouble: {
4042 locations->SetInAt(0, Location::RequiresFpuRegister());
4043 locations->SetInAt(1, Location::RequiresFpuRegister());
4044 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4045 break;
4046 }
4047
4048 default:
4049 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4050 }
4051}
4052
4053void InstructionCodeGeneratorARMVIXL::VisitMul(HMul* mul) {
4054 LocationSummary* locations = mul->GetLocations();
4055 Location out = locations->Out();
4056 Location first = locations->InAt(0);
4057 Location second = locations->InAt(1);
4058 switch (mul->GetResultType()) {
4059 case Primitive::kPrimInt: {
4060 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
4061 break;
4062 }
4063 case Primitive::kPrimLong: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004064 vixl32::Register out_hi = HighRegisterFrom(out);
4065 vixl32::Register out_lo = LowRegisterFrom(out);
4066 vixl32::Register in1_hi = HighRegisterFrom(first);
4067 vixl32::Register in1_lo = LowRegisterFrom(first);
4068 vixl32::Register in2_hi = HighRegisterFrom(second);
4069 vixl32::Register in2_lo = LowRegisterFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01004070
4071 // Extra checks to protect caused by the existence of R1_R2.
4072 // The algorithm is wrong if out.hi is either in1.lo or in2.lo:
4073 // (e.g. in1=r0_r1, in2=r2_r3 and out=r1_r2);
Anton Kirilov644032c2016-12-06 17:51:43 +00004074 DCHECK(!out_hi.Is(in1_lo));
4075 DCHECK(!out_hi.Is(in2_lo));
Scott Wakelingfe885462016-09-22 10:24:38 +01004076
4077 // input: in1 - 64 bits, in2 - 64 bits
4078 // output: out
4079 // formula: out.hi : out.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
4080 // parts: out.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
4081 // parts: out.lo = (in1.lo * in2.lo)[31:0]
4082
4083 UseScratchRegisterScope temps(GetVIXLAssembler());
4084 vixl32::Register temp = temps.Acquire();
4085 // temp <- in1.lo * in2.hi
4086 __ Mul(temp, in1_lo, in2_hi);
4087 // out.hi <- in1.lo * in2.hi + in1.hi * in2.lo
4088 __ Mla(out_hi, in1_hi, in2_lo, temp);
4089 // out.lo <- (in1.lo * in2.lo)[31:0];
4090 __ Umull(out_lo, temp, in1_lo, in2_lo);
4091 // out.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004092 __ Add(out_hi, out_hi, temp);
Scott Wakelingfe885462016-09-22 10:24:38 +01004093 break;
4094 }
4095
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004096 case Primitive::kPrimFloat:
4097 case Primitive::kPrimDouble:
4098 __ Vmul(OutputVRegister(mul), InputVRegisterAt(mul, 0), InputVRegisterAt(mul, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004099 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01004100
4101 default:
4102 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4103 }
4104}
4105
Scott Wakelingfe885462016-09-22 10:24:38 +01004106void InstructionCodeGeneratorARMVIXL::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
4107 DCHECK(instruction->IsDiv() || instruction->IsRem());
4108 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
4109
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004110 Location second = instruction->GetLocations()->InAt(1);
Scott Wakelingfe885462016-09-22 10:24:38 +01004111 DCHECK(second.IsConstant());
4112
4113 vixl32::Register out = OutputRegister(instruction);
4114 vixl32::Register dividend = InputRegisterAt(instruction, 0);
Anton Kirilov644032c2016-12-06 17:51:43 +00004115 int32_t imm = Int32ConstantFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01004116 DCHECK(imm == 1 || imm == -1);
4117
4118 if (instruction->IsRem()) {
4119 __ Mov(out, 0);
4120 } else {
4121 if (imm == 1) {
4122 __ Mov(out, dividend);
4123 } else {
4124 __ Rsb(out, dividend, 0);
4125 }
4126 }
4127}
4128
4129void InstructionCodeGeneratorARMVIXL::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
4130 DCHECK(instruction->IsDiv() || instruction->IsRem());
4131 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
4132
4133 LocationSummary* locations = instruction->GetLocations();
4134 Location second = locations->InAt(1);
4135 DCHECK(second.IsConstant());
4136
4137 vixl32::Register out = OutputRegister(instruction);
4138 vixl32::Register dividend = InputRegisterAt(instruction, 0);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004139 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
Anton Kirilov644032c2016-12-06 17:51:43 +00004140 int32_t imm = Int32ConstantFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01004141 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
4142 int ctz_imm = CTZ(abs_imm);
4143
4144 if (ctz_imm == 1) {
4145 __ Lsr(temp, dividend, 32 - ctz_imm);
4146 } else {
4147 __ Asr(temp, dividend, 31);
4148 __ Lsr(temp, temp, 32 - ctz_imm);
4149 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004150 __ Add(out, temp, dividend);
Scott Wakelingfe885462016-09-22 10:24:38 +01004151
4152 if (instruction->IsDiv()) {
4153 __ Asr(out, out, ctz_imm);
4154 if (imm < 0) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004155 __ Rsb(out, out, 0);
Scott Wakelingfe885462016-09-22 10:24:38 +01004156 }
4157 } else {
4158 __ Ubfx(out, out, 0, ctz_imm);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004159 __ Sub(out, out, temp);
Scott Wakelingfe885462016-09-22 10:24:38 +01004160 }
4161}
4162
4163void InstructionCodeGeneratorARMVIXL::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
4164 DCHECK(instruction->IsDiv() || instruction->IsRem());
4165 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
4166
4167 LocationSummary* locations = instruction->GetLocations();
4168 Location second = locations->InAt(1);
4169 DCHECK(second.IsConstant());
4170
4171 vixl32::Register out = OutputRegister(instruction);
4172 vixl32::Register dividend = InputRegisterAt(instruction, 0);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004173 vixl32::Register temp1 = RegisterFrom(locations->GetTemp(0));
4174 vixl32::Register temp2 = RegisterFrom(locations->GetTemp(1));
Scott Wakelingb77051e2016-11-21 19:46:00 +00004175 int32_t imm = Int32ConstantFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01004176
4177 int64_t magic;
4178 int shift;
4179 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
4180
Anton Kirilovdda43962016-11-21 19:55:20 +00004181 // TODO(VIXL): Change the static cast to Operand::From() after VIXL is fixed.
4182 __ Mov(temp1, static_cast<int32_t>(magic));
Scott Wakelingfe885462016-09-22 10:24:38 +01004183 __ Smull(temp2, temp1, dividend, temp1);
4184
4185 if (imm > 0 && magic < 0) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004186 __ Add(temp1, temp1, dividend);
Scott Wakelingfe885462016-09-22 10:24:38 +01004187 } else if (imm < 0 && magic > 0) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004188 __ Sub(temp1, temp1, dividend);
Scott Wakelingfe885462016-09-22 10:24:38 +01004189 }
4190
4191 if (shift != 0) {
4192 __ Asr(temp1, temp1, shift);
4193 }
4194
4195 if (instruction->IsDiv()) {
4196 __ Sub(out, temp1, Operand(temp1, vixl32::Shift(ASR), 31));
4197 } else {
4198 __ Sub(temp1, temp1, Operand(temp1, vixl32::Shift(ASR), 31));
4199 // TODO: Strength reduction for mls.
4200 __ Mov(temp2, imm);
4201 __ Mls(out, temp1, temp2, dividend);
4202 }
4203}
4204
4205void InstructionCodeGeneratorARMVIXL::GenerateDivRemConstantIntegral(
4206 HBinaryOperation* instruction) {
4207 DCHECK(instruction->IsDiv() || instruction->IsRem());
4208 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
4209
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004210 Location second = instruction->GetLocations()->InAt(1);
Scott Wakelingfe885462016-09-22 10:24:38 +01004211 DCHECK(second.IsConstant());
4212
Anton Kirilov644032c2016-12-06 17:51:43 +00004213 int32_t imm = Int32ConstantFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01004214 if (imm == 0) {
4215 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
4216 } else if (imm == 1 || imm == -1) {
4217 DivRemOneOrMinusOne(instruction);
4218 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
4219 DivRemByPowerOfTwo(instruction);
4220 } else {
4221 DCHECK(imm <= -2 || imm >= 2);
4222 GenerateDivRemWithAnyConstant(instruction);
4223 }
4224}
4225
4226void LocationsBuilderARMVIXL::VisitDiv(HDiv* div) {
4227 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
4228 if (div->GetResultType() == Primitive::kPrimLong) {
4229 // pLdiv runtime call.
4230 call_kind = LocationSummary::kCallOnMainOnly;
4231 } else if (div->GetResultType() == Primitive::kPrimInt && div->InputAt(1)->IsConstant()) {
4232 // sdiv will be replaced by other instruction sequence.
4233 } else if (div->GetResultType() == Primitive::kPrimInt &&
4234 !codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4235 // pIdivmod runtime call.
4236 call_kind = LocationSummary::kCallOnMainOnly;
4237 }
4238
4239 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
4240
4241 switch (div->GetResultType()) {
4242 case Primitive::kPrimInt: {
4243 if (div->InputAt(1)->IsConstant()) {
4244 locations->SetInAt(0, Location::RequiresRegister());
4245 locations->SetInAt(1, Location::ConstantLocation(div->InputAt(1)->AsConstant()));
4246 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Anton Kirilov644032c2016-12-06 17:51:43 +00004247 int32_t value = Int32ConstantFrom(div->InputAt(1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004248 if (value == 1 || value == 0 || value == -1) {
4249 // No temp register required.
4250 } else {
4251 locations->AddTemp(Location::RequiresRegister());
4252 if (!IsPowerOfTwo(AbsOrMin(value))) {
4253 locations->AddTemp(Location::RequiresRegister());
4254 }
4255 }
4256 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4257 locations->SetInAt(0, Location::RequiresRegister());
4258 locations->SetInAt(1, Location::RequiresRegister());
4259 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4260 } else {
Artem Serov551b28f2016-10-18 19:11:30 +01004261 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4262 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4263 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
Roland Levillain5e8d5f02016-10-18 18:03:43 +01004264 // Note: divmod will compute both the quotient and the remainder as the pair R0 and R1, but
Artem Serov551b28f2016-10-18 19:11:30 +01004265 // we only need the former.
4266 locations->SetOut(LocationFrom(r0));
Scott Wakelingfe885462016-09-22 10:24:38 +01004267 }
4268 break;
4269 }
4270 case Primitive::kPrimLong: {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01004271 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4272 locations->SetInAt(0, LocationFrom(
4273 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
4274 locations->SetInAt(1, LocationFrom(
4275 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
4276 locations->SetOut(LocationFrom(r0, r1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004277 break;
4278 }
4279 case Primitive::kPrimFloat:
4280 case Primitive::kPrimDouble: {
4281 locations->SetInAt(0, Location::RequiresFpuRegister());
4282 locations->SetInAt(1, Location::RequiresFpuRegister());
4283 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4284 break;
4285 }
4286
4287 default:
4288 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
4289 }
4290}
4291
4292void InstructionCodeGeneratorARMVIXL::VisitDiv(HDiv* div) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01004293 Location lhs = div->GetLocations()->InAt(0);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004294 Location rhs = div->GetLocations()->InAt(1);
Scott Wakelingfe885462016-09-22 10:24:38 +01004295
4296 switch (div->GetResultType()) {
4297 case Primitive::kPrimInt: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004298 if (rhs.IsConstant()) {
Scott Wakelingfe885462016-09-22 10:24:38 +01004299 GenerateDivRemConstantIntegral(div);
4300 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4301 __ Sdiv(OutputRegister(div), InputRegisterAt(div, 0), InputRegisterAt(div, 1));
4302 } else {
Artem Serov551b28f2016-10-18 19:11:30 +01004303 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4304 DCHECK(calling_convention.GetRegisterAt(0).Is(RegisterFrom(lhs)));
4305 DCHECK(calling_convention.GetRegisterAt(1).Is(RegisterFrom(rhs)));
4306 DCHECK(r0.Is(OutputRegister(div)));
4307
4308 codegen_->InvokeRuntime(kQuickIdivmod, div, div->GetDexPc());
4309 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
Scott Wakelingfe885462016-09-22 10:24:38 +01004310 }
4311 break;
4312 }
4313
4314 case Primitive::kPrimLong: {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01004315 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4316 DCHECK(calling_convention.GetRegisterAt(0).Is(LowRegisterFrom(lhs)));
4317 DCHECK(calling_convention.GetRegisterAt(1).Is(HighRegisterFrom(lhs)));
4318 DCHECK(calling_convention.GetRegisterAt(2).Is(LowRegisterFrom(rhs)));
4319 DCHECK(calling_convention.GetRegisterAt(3).Is(HighRegisterFrom(rhs)));
4320 DCHECK(LowRegisterFrom(div->GetLocations()->Out()).Is(r0));
4321 DCHECK(HighRegisterFrom(div->GetLocations()->Out()).Is(r1));
4322
4323 codegen_->InvokeRuntime(kQuickLdiv, div, div->GetDexPc());
4324 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Scott Wakelingfe885462016-09-22 10:24:38 +01004325 break;
4326 }
4327
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004328 case Primitive::kPrimFloat:
4329 case Primitive::kPrimDouble:
4330 __ Vdiv(OutputVRegister(div), InputVRegisterAt(div, 0), InputVRegisterAt(div, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004331 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01004332
4333 default:
4334 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
4335 }
4336}
4337
Artem Serov551b28f2016-10-18 19:11:30 +01004338void LocationsBuilderARMVIXL::VisitRem(HRem* rem) {
4339 Primitive::Type type = rem->GetResultType();
4340
4341 // Most remainders are implemented in the runtime.
4342 LocationSummary::CallKind call_kind = LocationSummary::kCallOnMainOnly;
4343 if (rem->GetResultType() == Primitive::kPrimInt && rem->InputAt(1)->IsConstant()) {
4344 // sdiv will be replaced by other instruction sequence.
4345 call_kind = LocationSummary::kNoCall;
4346 } else if ((rem->GetResultType() == Primitive::kPrimInt)
4347 && codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4348 // Have hardware divide instruction for int, do it with three instructions.
4349 call_kind = LocationSummary::kNoCall;
4350 }
4351
4352 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
4353
4354 switch (type) {
4355 case Primitive::kPrimInt: {
4356 if (rem->InputAt(1)->IsConstant()) {
4357 locations->SetInAt(0, Location::RequiresRegister());
4358 locations->SetInAt(1, Location::ConstantLocation(rem->InputAt(1)->AsConstant()));
4359 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Anton Kirilov644032c2016-12-06 17:51:43 +00004360 int32_t value = Int32ConstantFrom(rem->InputAt(1));
Artem Serov551b28f2016-10-18 19:11:30 +01004361 if (value == 1 || value == 0 || value == -1) {
4362 // No temp register required.
4363 } else {
4364 locations->AddTemp(Location::RequiresRegister());
4365 if (!IsPowerOfTwo(AbsOrMin(value))) {
4366 locations->AddTemp(Location::RequiresRegister());
4367 }
4368 }
4369 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4370 locations->SetInAt(0, Location::RequiresRegister());
4371 locations->SetInAt(1, Location::RequiresRegister());
4372 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4373 locations->AddTemp(Location::RequiresRegister());
4374 } else {
4375 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4376 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4377 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
Roland Levillain5e8d5f02016-10-18 18:03:43 +01004378 // Note: divmod will compute both the quotient and the remainder as the pair R0 and R1, but
Artem Serov551b28f2016-10-18 19:11:30 +01004379 // we only need the latter.
4380 locations->SetOut(LocationFrom(r1));
4381 }
4382 break;
4383 }
4384 case Primitive::kPrimLong: {
4385 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4386 locations->SetInAt(0, LocationFrom(
4387 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
4388 locations->SetInAt(1, LocationFrom(
4389 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
4390 // The runtime helper puts the output in R2,R3.
4391 locations->SetOut(LocationFrom(r2, r3));
4392 break;
4393 }
4394 case Primitive::kPrimFloat: {
4395 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4396 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
4397 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
4398 locations->SetOut(LocationFrom(s0));
4399 break;
4400 }
4401
4402 case Primitive::kPrimDouble: {
4403 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4404 locations->SetInAt(0, LocationFrom(
4405 calling_convention.GetFpuRegisterAt(0), calling_convention.GetFpuRegisterAt(1)));
4406 locations->SetInAt(1, LocationFrom(
4407 calling_convention.GetFpuRegisterAt(2), calling_convention.GetFpuRegisterAt(3)));
4408 locations->SetOut(LocationFrom(s0, s1));
4409 break;
4410 }
4411
4412 default:
4413 LOG(FATAL) << "Unexpected rem type " << type;
4414 }
4415}
4416
4417void InstructionCodeGeneratorARMVIXL::VisitRem(HRem* rem) {
4418 LocationSummary* locations = rem->GetLocations();
4419 Location second = locations->InAt(1);
4420
4421 Primitive::Type type = rem->GetResultType();
4422 switch (type) {
4423 case Primitive::kPrimInt: {
4424 vixl32::Register reg1 = InputRegisterAt(rem, 0);
4425 vixl32::Register out_reg = OutputRegister(rem);
4426 if (second.IsConstant()) {
4427 GenerateDivRemConstantIntegral(rem);
4428 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4429 vixl32::Register reg2 = RegisterFrom(second);
4430 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
4431
4432 // temp = reg1 / reg2 (integer division)
4433 // dest = reg1 - temp * reg2
4434 __ Sdiv(temp, reg1, reg2);
4435 __ Mls(out_reg, temp, reg2, reg1);
4436 } else {
4437 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4438 DCHECK(reg1.Is(calling_convention.GetRegisterAt(0)));
4439 DCHECK(RegisterFrom(second).Is(calling_convention.GetRegisterAt(1)));
4440 DCHECK(out_reg.Is(r1));
4441
4442 codegen_->InvokeRuntime(kQuickIdivmod, rem, rem->GetDexPc());
4443 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
4444 }
4445 break;
4446 }
4447
4448 case Primitive::kPrimLong: {
4449 codegen_->InvokeRuntime(kQuickLmod, rem, rem->GetDexPc());
4450 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
4451 break;
4452 }
4453
4454 case Primitive::kPrimFloat: {
4455 codegen_->InvokeRuntime(kQuickFmodf, rem, rem->GetDexPc());
4456 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
4457 break;
4458 }
4459
4460 case Primitive::kPrimDouble: {
4461 codegen_->InvokeRuntime(kQuickFmod, rem, rem->GetDexPc());
4462 CheckEntrypointTypes<kQuickFmod, double, double, double>();
4463 break;
4464 }
4465
4466 default:
4467 LOG(FATAL) << "Unexpected rem type " << type;
4468 }
4469}
4470
4471
Scott Wakelingfe885462016-09-22 10:24:38 +01004472void LocationsBuilderARMVIXL::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Artem Serov657022c2016-11-23 14:19:38 +00004473 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Scott Wakelingfe885462016-09-22 10:24:38 +01004474 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Scott Wakelingfe885462016-09-22 10:24:38 +01004475}
4476
4477void InstructionCodeGeneratorARMVIXL::VisitDivZeroCheck(HDivZeroCheck* instruction) {
4478 DivZeroCheckSlowPathARMVIXL* slow_path =
4479 new (GetGraph()->GetArena()) DivZeroCheckSlowPathARMVIXL(instruction);
4480 codegen_->AddSlowPath(slow_path);
4481
4482 LocationSummary* locations = instruction->GetLocations();
4483 Location value = locations->InAt(0);
4484
4485 switch (instruction->GetType()) {
4486 case Primitive::kPrimBoolean:
4487 case Primitive::kPrimByte:
4488 case Primitive::kPrimChar:
4489 case Primitive::kPrimShort:
4490 case Primitive::kPrimInt: {
4491 if (value.IsRegister()) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00004492 __ CompareAndBranchIfZero(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
Scott Wakelingfe885462016-09-22 10:24:38 +01004493 } else {
4494 DCHECK(value.IsConstant()) << value;
Anton Kirilov644032c2016-12-06 17:51:43 +00004495 if (Int32ConstantFrom(value) == 0) {
Scott Wakelingfe885462016-09-22 10:24:38 +01004496 __ B(slow_path->GetEntryLabel());
4497 }
4498 }
4499 break;
4500 }
4501 case Primitive::kPrimLong: {
4502 if (value.IsRegisterPair()) {
4503 UseScratchRegisterScope temps(GetVIXLAssembler());
4504 vixl32::Register temp = temps.Acquire();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004505 __ Orrs(temp, LowRegisterFrom(value), HighRegisterFrom(value));
Scott Wakelingfe885462016-09-22 10:24:38 +01004506 __ B(eq, slow_path->GetEntryLabel());
4507 } else {
4508 DCHECK(value.IsConstant()) << value;
Anton Kirilov644032c2016-12-06 17:51:43 +00004509 if (Int64ConstantFrom(value) == 0) {
Scott Wakelingfe885462016-09-22 10:24:38 +01004510 __ B(slow_path->GetEntryLabel());
4511 }
4512 }
4513 break;
4514 }
4515 default:
4516 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
4517 }
4518}
4519
Artem Serov02109dd2016-09-23 17:17:54 +01004520void InstructionCodeGeneratorARMVIXL::HandleIntegerRotate(HRor* ror) {
4521 LocationSummary* locations = ror->GetLocations();
4522 vixl32::Register in = InputRegisterAt(ror, 0);
4523 Location rhs = locations->InAt(1);
4524 vixl32::Register out = OutputRegister(ror);
4525
4526 if (rhs.IsConstant()) {
4527 // Arm32 and Thumb2 assemblers require a rotation on the interval [1,31],
4528 // so map all rotations to a +ve. equivalent in that range.
4529 // (e.g. left *or* right by -2 bits == 30 bits in the same direction.)
4530 uint32_t rot = CodeGenerator::GetInt32ValueOf(rhs.GetConstant()) & 0x1F;
4531 if (rot) {
4532 // Rotate, mapping left rotations to right equivalents if necessary.
4533 // (e.g. left by 2 bits == right by 30.)
4534 __ Ror(out, in, rot);
4535 } else if (!out.Is(in)) {
4536 __ Mov(out, in);
4537 }
4538 } else {
4539 __ Ror(out, in, RegisterFrom(rhs));
4540 }
4541}
4542
4543// Gain some speed by mapping all Long rotates onto equivalent pairs of Integer
4544// rotates by swapping input regs (effectively rotating by the first 32-bits of
4545// a larger rotation) or flipping direction (thus treating larger right/left
4546// rotations as sub-word sized rotations in the other direction) as appropriate.
4547void InstructionCodeGeneratorARMVIXL::HandleLongRotate(HRor* ror) {
4548 LocationSummary* locations = ror->GetLocations();
4549 vixl32::Register in_reg_lo = LowRegisterFrom(locations->InAt(0));
4550 vixl32::Register in_reg_hi = HighRegisterFrom(locations->InAt(0));
4551 Location rhs = locations->InAt(1);
4552 vixl32::Register out_reg_lo = LowRegisterFrom(locations->Out());
4553 vixl32::Register out_reg_hi = HighRegisterFrom(locations->Out());
4554
4555 if (rhs.IsConstant()) {
4556 uint64_t rot = CodeGenerator::GetInt64ValueOf(rhs.GetConstant());
4557 // Map all rotations to +ve. equivalents on the interval [0,63].
4558 rot &= kMaxLongShiftDistance;
4559 // For rotates over a word in size, 'pre-rotate' by 32-bits to keep rotate
4560 // logic below to a simple pair of binary orr.
4561 // (e.g. 34 bits == in_reg swap + 2 bits right.)
4562 if (rot >= kArmBitsPerWord) {
4563 rot -= kArmBitsPerWord;
4564 std::swap(in_reg_hi, in_reg_lo);
4565 }
4566 // Rotate, or mov to out for zero or word size rotations.
4567 if (rot != 0u) {
Scott Wakelingb77051e2016-11-21 19:46:00 +00004568 __ Lsr(out_reg_hi, in_reg_hi, Operand::From(rot));
Artem Serov02109dd2016-09-23 17:17:54 +01004569 __ Orr(out_reg_hi, out_reg_hi, Operand(in_reg_lo, ShiftType::LSL, kArmBitsPerWord - rot));
Scott Wakelingb77051e2016-11-21 19:46:00 +00004570 __ Lsr(out_reg_lo, in_reg_lo, Operand::From(rot));
Artem Serov02109dd2016-09-23 17:17:54 +01004571 __ Orr(out_reg_lo, out_reg_lo, Operand(in_reg_hi, ShiftType::LSL, kArmBitsPerWord - rot));
4572 } else {
4573 __ Mov(out_reg_lo, in_reg_lo);
4574 __ Mov(out_reg_hi, in_reg_hi);
4575 }
4576 } else {
4577 vixl32::Register shift_right = RegisterFrom(locations->GetTemp(0));
4578 vixl32::Register shift_left = RegisterFrom(locations->GetTemp(1));
4579 vixl32::Label end;
4580 vixl32::Label shift_by_32_plus_shift_right;
Anton Kirilov6f644202017-02-27 18:29:45 +00004581 vixl32::Label* final_label = codegen_->GetFinalLabel(ror, &end);
Artem Serov02109dd2016-09-23 17:17:54 +01004582
4583 __ And(shift_right, RegisterFrom(rhs), 0x1F);
4584 __ Lsrs(shift_left, RegisterFrom(rhs), 6);
Scott Wakelingbffdc702016-12-07 17:46:03 +00004585 __ Rsb(LeaveFlags, shift_left, shift_right, Operand::From(kArmBitsPerWord));
Artem Serov517d9f62016-12-12 15:51:15 +00004586 __ B(cc, &shift_by_32_plus_shift_right, /* far_target */ false);
Artem Serov02109dd2016-09-23 17:17:54 +01004587
4588 // out_reg_hi = (reg_hi << shift_left) | (reg_lo >> shift_right).
4589 // out_reg_lo = (reg_lo << shift_left) | (reg_hi >> shift_right).
4590 __ Lsl(out_reg_hi, in_reg_hi, shift_left);
4591 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
4592 __ Add(out_reg_hi, out_reg_hi, out_reg_lo);
4593 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
4594 __ Lsr(shift_left, in_reg_hi, shift_right);
4595 __ Add(out_reg_lo, out_reg_lo, shift_left);
Anton Kirilov6f644202017-02-27 18:29:45 +00004596 __ B(final_label);
Artem Serov02109dd2016-09-23 17:17:54 +01004597
4598 __ Bind(&shift_by_32_plus_shift_right); // Shift by 32+shift_right.
4599 // out_reg_hi = (reg_hi >> shift_right) | (reg_lo << shift_left).
4600 // out_reg_lo = (reg_lo >> shift_right) | (reg_hi << shift_left).
4601 __ Lsr(out_reg_hi, in_reg_hi, shift_right);
4602 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
4603 __ Add(out_reg_hi, out_reg_hi, out_reg_lo);
4604 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
4605 __ Lsl(shift_right, in_reg_hi, shift_left);
4606 __ Add(out_reg_lo, out_reg_lo, shift_right);
4607
Anton Kirilov6f644202017-02-27 18:29:45 +00004608 if (end.IsReferenced()) {
4609 __ Bind(&end);
4610 }
Artem Serov02109dd2016-09-23 17:17:54 +01004611 }
4612}
4613
4614void LocationsBuilderARMVIXL::VisitRor(HRor* ror) {
4615 LocationSummary* locations =
4616 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
4617 switch (ror->GetResultType()) {
4618 case Primitive::kPrimInt: {
4619 locations->SetInAt(0, Location::RequiresRegister());
4620 locations->SetInAt(1, Location::RegisterOrConstant(ror->InputAt(1)));
4621 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4622 break;
4623 }
4624 case Primitive::kPrimLong: {
4625 locations->SetInAt(0, Location::RequiresRegister());
4626 if (ror->InputAt(1)->IsConstant()) {
4627 locations->SetInAt(1, Location::ConstantLocation(ror->InputAt(1)->AsConstant()));
4628 } else {
4629 locations->SetInAt(1, Location::RequiresRegister());
4630 locations->AddTemp(Location::RequiresRegister());
4631 locations->AddTemp(Location::RequiresRegister());
4632 }
4633 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
4634 break;
4635 }
4636 default:
4637 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
4638 }
4639}
4640
4641void InstructionCodeGeneratorARMVIXL::VisitRor(HRor* ror) {
4642 Primitive::Type type = ror->GetResultType();
4643 switch (type) {
4644 case Primitive::kPrimInt: {
4645 HandleIntegerRotate(ror);
4646 break;
4647 }
4648 case Primitive::kPrimLong: {
4649 HandleLongRotate(ror);
4650 break;
4651 }
4652 default:
4653 LOG(FATAL) << "Unexpected operation type " << type;
4654 UNREACHABLE();
4655 }
4656}
4657
Artem Serov02d37832016-10-25 15:25:33 +01004658void LocationsBuilderARMVIXL::HandleShift(HBinaryOperation* op) {
4659 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
4660
4661 LocationSummary* locations =
4662 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
4663
4664 switch (op->GetResultType()) {
4665 case Primitive::kPrimInt: {
4666 locations->SetInAt(0, Location::RequiresRegister());
4667 if (op->InputAt(1)->IsConstant()) {
4668 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
4669 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4670 } else {
4671 locations->SetInAt(1, Location::RequiresRegister());
4672 // Make the output overlap, as it will be used to hold the masked
4673 // second input.
4674 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
4675 }
4676 break;
4677 }
4678 case Primitive::kPrimLong: {
4679 locations->SetInAt(0, Location::RequiresRegister());
4680 if (op->InputAt(1)->IsConstant()) {
4681 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
4682 // For simplicity, use kOutputOverlap even though we only require that low registers
4683 // don't clash with high registers which the register allocator currently guarantees.
4684 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
4685 } else {
4686 locations->SetInAt(1, Location::RequiresRegister());
4687 locations->AddTemp(Location::RequiresRegister());
4688 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
4689 }
4690 break;
4691 }
4692 default:
4693 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
4694 }
4695}
4696
4697void InstructionCodeGeneratorARMVIXL::HandleShift(HBinaryOperation* op) {
4698 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
4699
4700 LocationSummary* locations = op->GetLocations();
4701 Location out = locations->Out();
4702 Location first = locations->InAt(0);
4703 Location second = locations->InAt(1);
4704
4705 Primitive::Type type = op->GetResultType();
4706 switch (type) {
4707 case Primitive::kPrimInt: {
4708 vixl32::Register out_reg = OutputRegister(op);
4709 vixl32::Register first_reg = InputRegisterAt(op, 0);
4710 if (second.IsRegister()) {
4711 vixl32::Register second_reg = RegisterFrom(second);
4712 // ARM doesn't mask the shift count so we need to do it ourselves.
4713 __ And(out_reg, second_reg, kMaxIntShiftDistance);
4714 if (op->IsShl()) {
4715 __ Lsl(out_reg, first_reg, out_reg);
4716 } else if (op->IsShr()) {
4717 __ Asr(out_reg, first_reg, out_reg);
4718 } else {
4719 __ Lsr(out_reg, first_reg, out_reg);
4720 }
4721 } else {
Anton Kirilov644032c2016-12-06 17:51:43 +00004722 int32_t cst = Int32ConstantFrom(second);
Artem Serov02d37832016-10-25 15:25:33 +01004723 uint32_t shift_value = cst & kMaxIntShiftDistance;
4724 if (shift_value == 0) { // ARM does not support shifting with 0 immediate.
4725 __ Mov(out_reg, first_reg);
4726 } else if (op->IsShl()) {
4727 __ Lsl(out_reg, first_reg, shift_value);
4728 } else if (op->IsShr()) {
4729 __ Asr(out_reg, first_reg, shift_value);
4730 } else {
4731 __ Lsr(out_reg, first_reg, shift_value);
4732 }
4733 }
4734 break;
4735 }
4736 case Primitive::kPrimLong: {
4737 vixl32::Register o_h = HighRegisterFrom(out);
4738 vixl32::Register o_l = LowRegisterFrom(out);
4739
4740 vixl32::Register high = HighRegisterFrom(first);
4741 vixl32::Register low = LowRegisterFrom(first);
4742
4743 if (second.IsRegister()) {
4744 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
4745
4746 vixl32::Register second_reg = RegisterFrom(second);
4747
4748 if (op->IsShl()) {
4749 __ And(o_l, second_reg, kMaxLongShiftDistance);
4750 // Shift the high part
4751 __ Lsl(o_h, high, o_l);
4752 // Shift the low part and `or` what overflew on the high part
Scott Wakelingb77051e2016-11-21 19:46:00 +00004753 __ Rsb(temp, o_l, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01004754 __ Lsr(temp, low, temp);
4755 __ Orr(o_h, o_h, temp);
4756 // If the shift is > 32 bits, override the high part
Scott Wakelingb77051e2016-11-21 19:46:00 +00004757 __ Subs(temp, o_l, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01004758 {
Artem Serov0fb37192016-12-06 18:13:40 +00004759 ExactAssemblyScope guard(GetVIXLAssembler(),
4760 2 * vixl32::kMaxInstructionSizeInBytes,
4761 CodeBufferCheckScope::kMaximumSize);
Artem Serov02d37832016-10-25 15:25:33 +01004762 __ it(pl);
4763 __ lsl(pl, o_h, low, temp);
4764 }
4765 // Shift the low part
4766 __ Lsl(o_l, low, o_l);
4767 } else if (op->IsShr()) {
4768 __ And(o_h, second_reg, kMaxLongShiftDistance);
4769 // Shift the low part
4770 __ Lsr(o_l, low, o_h);
4771 // Shift the high part and `or` what underflew on the low part
Scott Wakelingb77051e2016-11-21 19:46:00 +00004772 __ Rsb(temp, o_h, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01004773 __ Lsl(temp, high, temp);
4774 __ Orr(o_l, o_l, temp);
4775 // If the shift is > 32 bits, override the low part
Scott Wakelingb77051e2016-11-21 19:46:00 +00004776 __ Subs(temp, o_h, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01004777 {
Artem Serov0fb37192016-12-06 18:13:40 +00004778 ExactAssemblyScope guard(GetVIXLAssembler(),
4779 2 * vixl32::kMaxInstructionSizeInBytes,
4780 CodeBufferCheckScope::kMaximumSize);
Artem Serov02d37832016-10-25 15:25:33 +01004781 __ it(pl);
4782 __ asr(pl, o_l, high, temp);
4783 }
4784 // Shift the high part
4785 __ Asr(o_h, high, o_h);
4786 } else {
4787 __ And(o_h, second_reg, kMaxLongShiftDistance);
4788 // same as Shr except we use `Lsr`s and not `Asr`s
4789 __ Lsr(o_l, low, o_h);
Scott Wakelingb77051e2016-11-21 19:46:00 +00004790 __ Rsb(temp, o_h, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01004791 __ Lsl(temp, high, temp);
4792 __ Orr(o_l, o_l, temp);
Scott Wakelingb77051e2016-11-21 19:46:00 +00004793 __ Subs(temp, o_h, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01004794 {
Artem Serov0fb37192016-12-06 18:13:40 +00004795 ExactAssemblyScope guard(GetVIXLAssembler(),
4796 2 * vixl32::kMaxInstructionSizeInBytes,
4797 CodeBufferCheckScope::kMaximumSize);
Artem Serov02d37832016-10-25 15:25:33 +01004798 __ it(pl);
4799 __ lsr(pl, o_l, high, temp);
4800 }
4801 __ Lsr(o_h, high, o_h);
4802 }
4803 } else {
4804 // Register allocator doesn't create partial overlap.
4805 DCHECK(!o_l.Is(high));
4806 DCHECK(!o_h.Is(low));
Anton Kirilov644032c2016-12-06 17:51:43 +00004807 int32_t cst = Int32ConstantFrom(second);
Artem Serov02d37832016-10-25 15:25:33 +01004808 uint32_t shift_value = cst & kMaxLongShiftDistance;
4809 if (shift_value > 32) {
4810 if (op->IsShl()) {
4811 __ Lsl(o_h, low, shift_value - 32);
4812 __ Mov(o_l, 0);
4813 } else if (op->IsShr()) {
4814 __ Asr(o_l, high, shift_value - 32);
4815 __ Asr(o_h, high, 31);
4816 } else {
4817 __ Lsr(o_l, high, shift_value - 32);
4818 __ Mov(o_h, 0);
4819 }
4820 } else if (shift_value == 32) {
4821 if (op->IsShl()) {
4822 __ Mov(o_h, low);
4823 __ Mov(o_l, 0);
4824 } else if (op->IsShr()) {
4825 __ Mov(o_l, high);
4826 __ Asr(o_h, high, 31);
4827 } else {
4828 __ Mov(o_l, high);
4829 __ Mov(o_h, 0);
4830 }
4831 } else if (shift_value == 1) {
4832 if (op->IsShl()) {
4833 __ Lsls(o_l, low, 1);
4834 __ Adc(o_h, high, high);
4835 } else if (op->IsShr()) {
4836 __ Asrs(o_h, high, 1);
4837 __ Rrx(o_l, low);
4838 } else {
4839 __ Lsrs(o_h, high, 1);
4840 __ Rrx(o_l, low);
4841 }
4842 } else {
4843 DCHECK(2 <= shift_value && shift_value < 32) << shift_value;
4844 if (op->IsShl()) {
4845 __ Lsl(o_h, high, shift_value);
4846 __ Orr(o_h, o_h, Operand(low, ShiftType::LSR, 32 - shift_value));
4847 __ Lsl(o_l, low, shift_value);
4848 } else if (op->IsShr()) {
4849 __ Lsr(o_l, low, shift_value);
4850 __ Orr(o_l, o_l, Operand(high, ShiftType::LSL, 32 - shift_value));
4851 __ Asr(o_h, high, shift_value);
4852 } else {
4853 __ Lsr(o_l, low, shift_value);
4854 __ Orr(o_l, o_l, Operand(high, ShiftType::LSL, 32 - shift_value));
4855 __ Lsr(o_h, high, shift_value);
4856 }
4857 }
4858 }
4859 break;
4860 }
4861 default:
4862 LOG(FATAL) << "Unexpected operation type " << type;
4863 UNREACHABLE();
4864 }
4865}
4866
4867void LocationsBuilderARMVIXL::VisitShl(HShl* shl) {
4868 HandleShift(shl);
4869}
4870
4871void InstructionCodeGeneratorARMVIXL::VisitShl(HShl* shl) {
4872 HandleShift(shl);
4873}
4874
4875void LocationsBuilderARMVIXL::VisitShr(HShr* shr) {
4876 HandleShift(shr);
4877}
4878
4879void InstructionCodeGeneratorARMVIXL::VisitShr(HShr* shr) {
4880 HandleShift(shr);
4881}
4882
4883void LocationsBuilderARMVIXL::VisitUShr(HUShr* ushr) {
4884 HandleShift(ushr);
4885}
4886
4887void InstructionCodeGeneratorARMVIXL::VisitUShr(HUShr* ushr) {
4888 HandleShift(ushr);
4889}
4890
4891void LocationsBuilderARMVIXL::VisitNewInstance(HNewInstance* instruction) {
4892 LocationSummary* locations =
4893 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
4894 if (instruction->IsStringAlloc()) {
4895 locations->AddTemp(LocationFrom(kMethodRegister));
4896 } else {
4897 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4898 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
Artem Serov02d37832016-10-25 15:25:33 +01004899 }
4900 locations->SetOut(LocationFrom(r0));
4901}
4902
4903void InstructionCodeGeneratorARMVIXL::VisitNewInstance(HNewInstance* instruction) {
4904 // Note: if heap poisoning is enabled, the entry point takes cares
4905 // of poisoning the reference.
4906 if (instruction->IsStringAlloc()) {
4907 // String is allocated through StringFactory. Call NewEmptyString entry point.
4908 vixl32::Register temp = RegisterFrom(instruction->GetLocations()->GetTemp(0));
4909 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize);
4910 GetAssembler()->LoadFromOffset(kLoadWord, temp, tr, QUICK_ENTRY_POINT(pNewEmptyString));
4911 GetAssembler()->LoadFromOffset(kLoadWord, lr, temp, code_offset.Int32Value());
Alexandre Rames374ddf32016-11-04 10:40:49 +00004912 // 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 +00004913 ExactAssemblyScope aas(GetVIXLAssembler(),
4914 vixl32::k16BitT32InstructionSizeInBytes,
4915 CodeBufferCheckScope::kExactSize);
Artem Serov02d37832016-10-25 15:25:33 +01004916 __ blx(lr);
4917 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4918 } else {
4919 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00004920 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
Artem Serov02d37832016-10-25 15:25:33 +01004921 }
4922}
4923
4924void LocationsBuilderARMVIXL::VisitNewArray(HNewArray* instruction) {
4925 LocationSummary* locations =
4926 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
4927 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Artem Serov02d37832016-10-25 15:25:33 +01004928 locations->SetOut(LocationFrom(r0));
Nicolas Geoffray8c7c4f12017-01-26 10:13:11 +00004929 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4930 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
Artem Serov02d37832016-10-25 15:25:33 +01004931}
4932
4933void InstructionCodeGeneratorARMVIXL::VisitNewArray(HNewArray* instruction) {
Artem Serov02d37832016-10-25 15:25:33 +01004934 // Note: if heap poisoning is enabled, the entry point takes cares
4935 // of poisoning the reference.
Artem Serov7b3672e2017-02-03 17:30:34 +00004936 QuickEntrypointEnum entrypoint =
4937 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
4938 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004939 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Artem Serov7b3672e2017-02-03 17:30:34 +00004940 DCHECK(!codegen_->IsLeafMethod());
Artem Serov02d37832016-10-25 15:25:33 +01004941}
4942
4943void LocationsBuilderARMVIXL::VisitParameterValue(HParameterValue* instruction) {
4944 LocationSummary* locations =
4945 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4946 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4947 if (location.IsStackSlot()) {
4948 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4949 } else if (location.IsDoubleStackSlot()) {
4950 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4951 }
4952 locations->SetOut(location);
4953}
4954
4955void InstructionCodeGeneratorARMVIXL::VisitParameterValue(
4956 HParameterValue* instruction ATTRIBUTE_UNUSED) {
4957 // Nothing to do, the parameter is already at its location.
4958}
4959
4960void LocationsBuilderARMVIXL::VisitCurrentMethod(HCurrentMethod* instruction) {
4961 LocationSummary* locations =
4962 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4963 locations->SetOut(LocationFrom(kMethodRegister));
4964}
4965
4966void InstructionCodeGeneratorARMVIXL::VisitCurrentMethod(
4967 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
4968 // Nothing to do, the method is already at its location.
4969}
4970
4971void LocationsBuilderARMVIXL::VisitNot(HNot* not_) {
4972 LocationSummary* locations =
4973 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
4974 locations->SetInAt(0, Location::RequiresRegister());
4975 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4976}
4977
4978void InstructionCodeGeneratorARMVIXL::VisitNot(HNot* not_) {
4979 LocationSummary* locations = not_->GetLocations();
4980 Location out = locations->Out();
4981 Location in = locations->InAt(0);
4982 switch (not_->GetResultType()) {
4983 case Primitive::kPrimInt:
4984 __ Mvn(OutputRegister(not_), InputRegisterAt(not_, 0));
4985 break;
4986
4987 case Primitive::kPrimLong:
4988 __ Mvn(LowRegisterFrom(out), LowRegisterFrom(in));
4989 __ Mvn(HighRegisterFrom(out), HighRegisterFrom(in));
4990 break;
4991
4992 default:
4993 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4994 }
4995}
4996
Scott Wakelingc34dba72016-10-03 10:14:44 +01004997void LocationsBuilderARMVIXL::VisitBooleanNot(HBooleanNot* bool_not) {
4998 LocationSummary* locations =
4999 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
5000 locations->SetInAt(0, Location::RequiresRegister());
5001 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5002}
5003
5004void InstructionCodeGeneratorARMVIXL::VisitBooleanNot(HBooleanNot* bool_not) {
5005 __ Eor(OutputRegister(bool_not), InputRegister(bool_not), 1);
5006}
5007
Artem Serov02d37832016-10-25 15:25:33 +01005008void LocationsBuilderARMVIXL::VisitCompare(HCompare* compare) {
5009 LocationSummary* locations =
5010 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
5011 switch (compare->InputAt(0)->GetType()) {
5012 case Primitive::kPrimBoolean:
5013 case Primitive::kPrimByte:
5014 case Primitive::kPrimShort:
5015 case Primitive::kPrimChar:
5016 case Primitive::kPrimInt:
5017 case Primitive::kPrimLong: {
5018 locations->SetInAt(0, Location::RequiresRegister());
5019 locations->SetInAt(1, Location::RequiresRegister());
5020 // Output overlaps because it is written before doing the low comparison.
5021 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
5022 break;
5023 }
5024 case Primitive::kPrimFloat:
5025 case Primitive::kPrimDouble: {
5026 locations->SetInAt(0, Location::RequiresFpuRegister());
5027 locations->SetInAt(1, ArithmeticZeroOrFpuRegister(compare->InputAt(1)));
5028 locations->SetOut(Location::RequiresRegister());
5029 break;
5030 }
5031 default:
5032 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
5033 }
5034}
5035
5036void InstructionCodeGeneratorARMVIXL::VisitCompare(HCompare* compare) {
5037 LocationSummary* locations = compare->GetLocations();
5038 vixl32::Register out = OutputRegister(compare);
5039 Location left = locations->InAt(0);
5040 Location right = locations->InAt(1);
5041
5042 vixl32::Label less, greater, done;
Anton Kirilov6f644202017-02-27 18:29:45 +00005043 vixl32::Label* final_label = codegen_->GetFinalLabel(compare, &done);
Artem Serov02d37832016-10-25 15:25:33 +01005044 Primitive::Type type = compare->InputAt(0)->GetType();
5045 vixl32::Condition less_cond = vixl32::Condition(kNone);
5046 switch (type) {
5047 case Primitive::kPrimBoolean:
5048 case Primitive::kPrimByte:
5049 case Primitive::kPrimShort:
5050 case Primitive::kPrimChar:
5051 case Primitive::kPrimInt: {
5052 // Emit move to `out` before the `Cmp`, as `Mov` might affect the status flags.
5053 __ Mov(out, 0);
5054 __ Cmp(RegisterFrom(left), RegisterFrom(right)); // Signed compare.
5055 less_cond = lt;
5056 break;
5057 }
5058 case Primitive::kPrimLong: {
5059 __ Cmp(HighRegisterFrom(left), HighRegisterFrom(right)); // Signed compare.
Artem Serov517d9f62016-12-12 15:51:15 +00005060 __ B(lt, &less, /* far_target */ false);
5061 __ B(gt, &greater, /* far_target */ false);
Artem Serov02d37832016-10-25 15:25:33 +01005062 // Emit move to `out` before the last `Cmp`, as `Mov` might affect the status flags.
5063 __ Mov(out, 0);
5064 __ Cmp(LowRegisterFrom(left), LowRegisterFrom(right)); // Unsigned compare.
5065 less_cond = lo;
5066 break;
5067 }
5068 case Primitive::kPrimFloat:
5069 case Primitive::kPrimDouble: {
5070 __ Mov(out, 0);
Donghui Bai426b49c2016-11-08 14:55:38 +08005071 GenerateVcmp(compare, codegen_);
Artem Serov02d37832016-10-25 15:25:33 +01005072 // To branch on the FP compare result we transfer FPSCR to APSR (encoded as PC in VMRS).
5073 __ Vmrs(RegisterOrAPSR_nzcv(kPcCode), FPSCR);
5074 less_cond = ARMFPCondition(kCondLT, compare->IsGtBias());
5075 break;
5076 }
5077 default:
5078 LOG(FATAL) << "Unexpected compare type " << type;
5079 UNREACHABLE();
5080 }
5081
Anton Kirilov6f644202017-02-27 18:29:45 +00005082 __ B(eq, final_label, /* far_target */ false);
Artem Serov517d9f62016-12-12 15:51:15 +00005083 __ B(less_cond, &less, /* far_target */ false);
Artem Serov02d37832016-10-25 15:25:33 +01005084
5085 __ Bind(&greater);
5086 __ Mov(out, 1);
Anton Kirilov6f644202017-02-27 18:29:45 +00005087 __ B(final_label);
Artem Serov02d37832016-10-25 15:25:33 +01005088
5089 __ Bind(&less);
5090 __ Mov(out, -1);
5091
Anton Kirilov6f644202017-02-27 18:29:45 +00005092 if (done.IsReferenced()) {
5093 __ Bind(&done);
5094 }
Artem Serov02d37832016-10-25 15:25:33 +01005095}
5096
5097void LocationsBuilderARMVIXL::VisitPhi(HPhi* instruction) {
5098 LocationSummary* locations =
5099 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5100 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
5101 locations->SetInAt(i, Location::Any());
5102 }
5103 locations->SetOut(Location::Any());
5104}
5105
5106void InstructionCodeGeneratorARMVIXL::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
5107 LOG(FATAL) << "Unreachable";
5108}
5109
5110void CodeGeneratorARMVIXL::GenerateMemoryBarrier(MemBarrierKind kind) {
5111 // TODO (ported from quick): revisit ARM barrier kinds.
5112 DmbOptions flavor = DmbOptions::ISH; // Quiet C++ warnings.
5113 switch (kind) {
5114 case MemBarrierKind::kAnyStore:
5115 case MemBarrierKind::kLoadAny:
5116 case MemBarrierKind::kAnyAny: {
5117 flavor = DmbOptions::ISH;
5118 break;
5119 }
5120 case MemBarrierKind::kStoreStore: {
5121 flavor = DmbOptions::ISHST;
5122 break;
5123 }
5124 default:
5125 LOG(FATAL) << "Unexpected memory barrier " << kind;
5126 }
5127 __ Dmb(flavor);
5128}
5129
5130void InstructionCodeGeneratorARMVIXL::GenerateWideAtomicLoad(vixl32::Register addr,
5131 uint32_t offset,
5132 vixl32::Register out_lo,
5133 vixl32::Register out_hi) {
5134 UseScratchRegisterScope temps(GetVIXLAssembler());
5135 if (offset != 0) {
5136 vixl32::Register temp = temps.Acquire();
5137 __ Add(temp, addr, offset);
5138 addr = temp;
5139 }
Scott Wakelingb77051e2016-11-21 19:46:00 +00005140 __ Ldrexd(out_lo, out_hi, MemOperand(addr));
Artem Serov02d37832016-10-25 15:25:33 +01005141}
5142
5143void InstructionCodeGeneratorARMVIXL::GenerateWideAtomicStore(vixl32::Register addr,
5144 uint32_t offset,
5145 vixl32::Register value_lo,
5146 vixl32::Register value_hi,
5147 vixl32::Register temp1,
5148 vixl32::Register temp2,
5149 HInstruction* instruction) {
5150 UseScratchRegisterScope temps(GetVIXLAssembler());
5151 vixl32::Label fail;
5152 if (offset != 0) {
5153 vixl32::Register temp = temps.Acquire();
5154 __ Add(temp, addr, offset);
5155 addr = temp;
5156 }
5157 __ Bind(&fail);
Alexandre Rames374ddf32016-11-04 10:40:49 +00005158 {
5159 // Ensure the pc position is recorded immediately after the `ldrexd` instruction.
Artem Serov0fb37192016-12-06 18:13:40 +00005160 ExactAssemblyScope aas(GetVIXLAssembler(),
5161 vixl32::kMaxInstructionSizeInBytes,
5162 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00005163 // We need a load followed by store. (The address used in a STREX instruction must
5164 // be the same as the address in the most recently executed LDREX instruction.)
5165 __ ldrexd(temp1, temp2, MemOperand(addr));
5166 codegen_->MaybeRecordImplicitNullCheck(instruction);
5167 }
Scott Wakelingb77051e2016-11-21 19:46:00 +00005168 __ Strexd(temp1, value_lo, value_hi, MemOperand(addr));
xueliang.zhongf51bc622016-11-04 09:23:32 +00005169 __ CompareAndBranchIfNonZero(temp1, &fail);
Artem Serov02d37832016-10-25 15:25:33 +01005170}
Artem Serov02109dd2016-09-23 17:17:54 +01005171
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005172void LocationsBuilderARMVIXL::HandleFieldSet(
5173 HInstruction* instruction, const FieldInfo& field_info) {
5174 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
5175
5176 LocationSummary* locations =
5177 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5178 locations->SetInAt(0, Location::RequiresRegister());
5179
5180 Primitive::Type field_type = field_info.GetFieldType();
5181 if (Primitive::IsFloatingPointType(field_type)) {
5182 locations->SetInAt(1, Location::RequiresFpuRegister());
5183 } else {
5184 locations->SetInAt(1, Location::RequiresRegister());
5185 }
5186
5187 bool is_wide = field_type == Primitive::kPrimLong || field_type == Primitive::kPrimDouble;
5188 bool generate_volatile = field_info.IsVolatile()
5189 && is_wide
5190 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
5191 bool needs_write_barrier =
5192 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
5193 // Temporary registers for the write barrier.
5194 // TODO: consider renaming StoreNeedsWriteBarrier to StoreNeedsGCMark.
5195 if (needs_write_barrier) {
5196 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
5197 locations->AddTemp(Location::RequiresRegister());
5198 } else if (generate_volatile) {
5199 // ARM encoding have some additional constraints for ldrexd/strexd:
5200 // - registers need to be consecutive
5201 // - the first register should be even but not R14.
5202 // We don't test for ARM yet, and the assertion makes sure that we
5203 // revisit this if we ever enable ARM encoding.
5204 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
5205
5206 locations->AddTemp(Location::RequiresRegister());
5207 locations->AddTemp(Location::RequiresRegister());
5208 if (field_type == Primitive::kPrimDouble) {
5209 // For doubles we need two more registers to copy the value.
5210 locations->AddTemp(LocationFrom(r2));
5211 locations->AddTemp(LocationFrom(r3));
5212 }
5213 }
5214}
5215
5216void InstructionCodeGeneratorARMVIXL::HandleFieldSet(HInstruction* instruction,
5217 const FieldInfo& field_info,
5218 bool value_can_be_null) {
5219 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
5220
5221 LocationSummary* locations = instruction->GetLocations();
5222 vixl32::Register base = InputRegisterAt(instruction, 0);
5223 Location value = locations->InAt(1);
5224
5225 bool is_volatile = field_info.IsVolatile();
5226 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
5227 Primitive::Type field_type = field_info.GetFieldType();
5228 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
5229 bool needs_write_barrier =
5230 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
5231
5232 if (is_volatile) {
5233 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
5234 }
5235
5236 switch (field_type) {
5237 case Primitive::kPrimBoolean:
5238 case Primitive::kPrimByte: {
5239 GetAssembler()->StoreToOffset(kStoreByte, RegisterFrom(value), base, offset);
5240 break;
5241 }
5242
5243 case Primitive::kPrimShort:
5244 case Primitive::kPrimChar: {
5245 GetAssembler()->StoreToOffset(kStoreHalfword, RegisterFrom(value), base, offset);
5246 break;
5247 }
5248
5249 case Primitive::kPrimInt:
5250 case Primitive::kPrimNot: {
5251 if (kPoisonHeapReferences && needs_write_barrier) {
5252 // Note that in the case where `value` is a null reference,
5253 // we do not enter this block, as a null reference does not
5254 // need poisoning.
5255 DCHECK_EQ(field_type, Primitive::kPrimNot);
5256 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
5257 __ Mov(temp, RegisterFrom(value));
5258 GetAssembler()->PoisonHeapReference(temp);
5259 GetAssembler()->StoreToOffset(kStoreWord, temp, base, offset);
5260 } else {
5261 GetAssembler()->StoreToOffset(kStoreWord, RegisterFrom(value), base, offset);
5262 }
5263 break;
5264 }
5265
5266 case Primitive::kPrimLong: {
5267 if (is_volatile && !atomic_ldrd_strd) {
5268 GenerateWideAtomicStore(base,
5269 offset,
5270 LowRegisterFrom(value),
5271 HighRegisterFrom(value),
5272 RegisterFrom(locations->GetTemp(0)),
5273 RegisterFrom(locations->GetTemp(1)),
5274 instruction);
5275 } else {
5276 GetAssembler()->StoreToOffset(kStoreWordPair, LowRegisterFrom(value), base, offset);
5277 codegen_->MaybeRecordImplicitNullCheck(instruction);
5278 }
5279 break;
5280 }
5281
5282 case Primitive::kPrimFloat: {
5283 GetAssembler()->StoreSToOffset(SRegisterFrom(value), base, offset);
5284 break;
5285 }
5286
5287 case Primitive::kPrimDouble: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01005288 vixl32::DRegister value_reg = DRegisterFrom(value);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005289 if (is_volatile && !atomic_ldrd_strd) {
5290 vixl32::Register value_reg_lo = RegisterFrom(locations->GetTemp(0));
5291 vixl32::Register value_reg_hi = RegisterFrom(locations->GetTemp(1));
5292
5293 __ Vmov(value_reg_lo, value_reg_hi, value_reg);
5294
5295 GenerateWideAtomicStore(base,
5296 offset,
5297 value_reg_lo,
5298 value_reg_hi,
5299 RegisterFrom(locations->GetTemp(2)),
5300 RegisterFrom(locations->GetTemp(3)),
5301 instruction);
5302 } else {
5303 GetAssembler()->StoreDToOffset(value_reg, base, offset);
5304 codegen_->MaybeRecordImplicitNullCheck(instruction);
5305 }
5306 break;
5307 }
5308
5309 case Primitive::kPrimVoid:
5310 LOG(FATAL) << "Unreachable type " << field_type;
5311 UNREACHABLE();
5312 }
5313
5314 // Longs and doubles are handled in the switch.
5315 if (field_type != Primitive::kPrimLong && field_type != Primitive::kPrimDouble) {
Alexandre Rames374ddf32016-11-04 10:40:49 +00005316 // TODO(VIXL): Here and for other calls to `MaybeRecordImplicitNullCheck` in this method, we
5317 // should use a scope and the assembler to emit the store instruction to guarantee that we
5318 // record the pc at the correct position. But the `Assembler` does not automatically handle
5319 // unencodable offsets. Practically, everything is fine because the helper and VIXL, at the time
5320 // of writing, do generate the store instruction last.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005321 codegen_->MaybeRecordImplicitNullCheck(instruction);
5322 }
5323
5324 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
5325 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
5326 vixl32::Register card = RegisterFrom(locations->GetTemp(1));
5327 codegen_->MarkGCCard(temp, card, base, RegisterFrom(value), value_can_be_null);
5328 }
5329
5330 if (is_volatile) {
5331 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
5332 }
5333}
5334
Artem Serov02d37832016-10-25 15:25:33 +01005335void LocationsBuilderARMVIXL::HandleFieldGet(HInstruction* instruction,
5336 const FieldInfo& field_info) {
5337 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
5338
5339 bool object_field_get_with_read_barrier =
5340 kEmitCompilerReadBarrier && (field_info.GetFieldType() == Primitive::kPrimNot);
5341 LocationSummary* locations =
5342 new (GetGraph()->GetArena()) LocationSummary(instruction,
5343 object_field_get_with_read_barrier ?
5344 LocationSummary::kCallOnSlowPath :
5345 LocationSummary::kNoCall);
5346 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
5347 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
5348 }
5349 locations->SetInAt(0, Location::RequiresRegister());
5350
5351 bool volatile_for_double = field_info.IsVolatile()
5352 && (field_info.GetFieldType() == Primitive::kPrimDouble)
5353 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
5354 // The output overlaps in case of volatile long: we don't want the
5355 // code generated by GenerateWideAtomicLoad to overwrite the
5356 // object's location. Likewise, in the case of an object field get
5357 // with read barriers enabled, we do not want the load to overwrite
5358 // the object's location, as we need it to emit the read barrier.
5359 bool overlap = (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) ||
5360 object_field_get_with_read_barrier;
5361
5362 if (Primitive::IsFloatingPointType(instruction->GetType())) {
5363 locations->SetOut(Location::RequiresFpuRegister());
5364 } else {
5365 locations->SetOut(Location::RequiresRegister(),
5366 (overlap ? Location::kOutputOverlap : Location::kNoOutputOverlap));
5367 }
5368 if (volatile_for_double) {
5369 // ARM encoding have some additional constraints for ldrexd/strexd:
5370 // - registers need to be consecutive
5371 // - the first register should be even but not R14.
5372 // We don't test for ARM yet, and the assertion makes sure that we
5373 // revisit this if we ever enable ARM encoding.
5374 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
5375 locations->AddTemp(Location::RequiresRegister());
5376 locations->AddTemp(Location::RequiresRegister());
5377 } else if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
5378 // We need a temporary register for the read barrier marking slow
Artem Serovc5fcb442016-12-02 19:19:58 +00005379 // path in CodeGeneratorARMVIXL::GenerateFieldLoadWithBakerReadBarrier.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01005380 if (kBakerReadBarrierLinkTimeThunksEnableForFields &&
5381 !Runtime::Current()->UseJitCompilation()) {
5382 // If link-time thunks for the Baker read barrier are enabled, for AOT
5383 // loads we need a temporary only if the offset is too big.
5384 if (field_info.GetFieldOffset().Uint32Value() >= kReferenceLoadMinFarOffset) {
5385 locations->AddTemp(Location::RequiresRegister());
5386 }
5387 // And we always need the reserved entrypoint register.
5388 locations->AddTemp(Location::RegisterLocation(kBakerCcEntrypointRegister.GetCode()));
5389 } else {
5390 locations->AddTemp(Location::RequiresRegister());
5391 }
Artem Serov02d37832016-10-25 15:25:33 +01005392 }
5393}
5394
5395Location LocationsBuilderARMVIXL::ArithmeticZeroOrFpuRegister(HInstruction* input) {
5396 DCHECK(Primitive::IsFloatingPointType(input->GetType())) << input->GetType();
5397 if ((input->IsFloatConstant() && (input->AsFloatConstant()->IsArithmeticZero())) ||
5398 (input->IsDoubleConstant() && (input->AsDoubleConstant()->IsArithmeticZero()))) {
5399 return Location::ConstantLocation(input->AsConstant());
5400 } else {
5401 return Location::RequiresFpuRegister();
5402 }
5403}
5404
Artem Serov02109dd2016-09-23 17:17:54 +01005405Location LocationsBuilderARMVIXL::ArmEncodableConstantOrRegister(HInstruction* constant,
5406 Opcode opcode) {
5407 DCHECK(!Primitive::IsFloatingPointType(constant->GetType()));
5408 if (constant->IsConstant() &&
5409 CanEncodeConstantAsImmediate(constant->AsConstant(), opcode)) {
5410 return Location::ConstantLocation(constant->AsConstant());
5411 }
5412 return Location::RequiresRegister();
5413}
5414
5415bool LocationsBuilderARMVIXL::CanEncodeConstantAsImmediate(HConstant* input_cst,
5416 Opcode opcode) {
5417 uint64_t value = static_cast<uint64_t>(Int64FromConstant(input_cst));
5418 if (Primitive::Is64BitType(input_cst->GetType())) {
5419 Opcode high_opcode = opcode;
5420 SetCc low_set_cc = kCcDontCare;
5421 switch (opcode) {
5422 case SUB:
5423 // Flip the operation to an ADD.
5424 value = -value;
5425 opcode = ADD;
5426 FALLTHROUGH_INTENDED;
5427 case ADD:
5428 if (Low32Bits(value) == 0u) {
5429 return CanEncodeConstantAsImmediate(High32Bits(value), opcode, kCcDontCare);
5430 }
5431 high_opcode = ADC;
5432 low_set_cc = kCcSet;
5433 break;
5434 default:
5435 break;
5436 }
5437 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode, low_set_cc) &&
5438 CanEncodeConstantAsImmediate(High32Bits(value), high_opcode, kCcDontCare);
5439 } else {
5440 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode);
5441 }
5442}
5443
5444// TODO(VIXL): Replace art::arm::SetCc` with `vixl32::FlagsUpdate after flags set optimization
5445// enabled.
5446bool LocationsBuilderARMVIXL::CanEncodeConstantAsImmediate(uint32_t value,
5447 Opcode opcode,
5448 SetCc set_cc) {
5449 ArmVIXLAssembler* assembler = codegen_->GetAssembler();
5450 if (assembler->ShifterOperandCanHold(opcode, value, set_cc)) {
5451 return true;
5452 }
5453 Opcode neg_opcode = kNoOperand;
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00005454 uint32_t neg_value = 0;
Artem Serov02109dd2016-09-23 17:17:54 +01005455 switch (opcode) {
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00005456 case AND: neg_opcode = BIC; neg_value = ~value; break;
5457 case ORR: neg_opcode = ORN; neg_value = ~value; break;
5458 case ADD: neg_opcode = SUB; neg_value = -value; break;
5459 case ADC: neg_opcode = SBC; neg_value = ~value; break;
5460 case SUB: neg_opcode = ADD; neg_value = -value; break;
5461 case SBC: neg_opcode = ADC; neg_value = ~value; break;
5462 case MOV: neg_opcode = MVN; neg_value = ~value; break;
Artem Serov02109dd2016-09-23 17:17:54 +01005463 default:
5464 return false;
5465 }
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00005466
5467 if (assembler->ShifterOperandCanHold(neg_opcode, neg_value, set_cc)) {
5468 return true;
5469 }
5470
5471 return opcode == AND && IsPowerOfTwo(value + 1);
Artem Serov02109dd2016-09-23 17:17:54 +01005472}
5473
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005474void InstructionCodeGeneratorARMVIXL::HandleFieldGet(HInstruction* instruction,
5475 const FieldInfo& field_info) {
5476 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
5477
5478 LocationSummary* locations = instruction->GetLocations();
5479 vixl32::Register base = InputRegisterAt(instruction, 0);
5480 Location out = locations->Out();
5481 bool is_volatile = field_info.IsVolatile();
5482 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
5483 Primitive::Type field_type = field_info.GetFieldType();
5484 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
5485
5486 switch (field_type) {
5487 case Primitive::kPrimBoolean:
5488 GetAssembler()->LoadFromOffset(kLoadUnsignedByte, RegisterFrom(out), base, offset);
5489 break;
5490
5491 case Primitive::kPrimByte:
5492 GetAssembler()->LoadFromOffset(kLoadSignedByte, RegisterFrom(out), base, offset);
5493 break;
5494
5495 case Primitive::kPrimShort:
5496 GetAssembler()->LoadFromOffset(kLoadSignedHalfword, RegisterFrom(out), base, offset);
5497 break;
5498
5499 case Primitive::kPrimChar:
5500 GetAssembler()->LoadFromOffset(kLoadUnsignedHalfword, RegisterFrom(out), base, offset);
5501 break;
5502
5503 case Primitive::kPrimInt:
5504 GetAssembler()->LoadFromOffset(kLoadWord, RegisterFrom(out), base, offset);
5505 break;
5506
5507 case Primitive::kPrimNot: {
5508 // /* HeapReference<Object> */ out = *(base + offset)
5509 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005510 Location temp_loc = locations->GetTemp(0);
5511 // Note that a potential implicit null check is handled in this
5512 // CodeGeneratorARMVIXL::GenerateFieldLoadWithBakerReadBarrier call.
5513 codegen_->GenerateFieldLoadWithBakerReadBarrier(
5514 instruction, out, base, offset, temp_loc, /* needs_null_check */ true);
5515 if (is_volatile) {
5516 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5517 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005518 } else {
5519 GetAssembler()->LoadFromOffset(kLoadWord, RegisterFrom(out), base, offset);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005520 codegen_->MaybeRecordImplicitNullCheck(instruction);
5521 if (is_volatile) {
5522 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5523 }
5524 // If read barriers are enabled, emit read barriers other than
5525 // Baker's using a slow path (and also unpoison the loaded
5526 // reference, if heap poisoning is enabled).
5527 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, locations->InAt(0), offset);
5528 }
5529 break;
5530 }
5531
5532 case Primitive::kPrimLong:
5533 if (is_volatile && !atomic_ldrd_strd) {
5534 GenerateWideAtomicLoad(base, offset, LowRegisterFrom(out), HighRegisterFrom(out));
5535 } else {
5536 GetAssembler()->LoadFromOffset(kLoadWordPair, LowRegisterFrom(out), base, offset);
5537 }
5538 break;
5539
5540 case Primitive::kPrimFloat:
5541 GetAssembler()->LoadSFromOffset(SRegisterFrom(out), base, offset);
5542 break;
5543
5544 case Primitive::kPrimDouble: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01005545 vixl32::DRegister out_dreg = DRegisterFrom(out);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005546 if (is_volatile && !atomic_ldrd_strd) {
5547 vixl32::Register lo = RegisterFrom(locations->GetTemp(0));
5548 vixl32::Register hi = RegisterFrom(locations->GetTemp(1));
5549 GenerateWideAtomicLoad(base, offset, lo, hi);
5550 // TODO(VIXL): Do we need to be immediately after the ldrexd instruction? If so we need a
5551 // scope.
5552 codegen_->MaybeRecordImplicitNullCheck(instruction);
5553 __ Vmov(out_dreg, lo, hi);
5554 } else {
5555 GetAssembler()->LoadDFromOffset(out_dreg, base, offset);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005556 codegen_->MaybeRecordImplicitNullCheck(instruction);
5557 }
5558 break;
5559 }
5560
5561 case Primitive::kPrimVoid:
5562 LOG(FATAL) << "Unreachable type " << field_type;
5563 UNREACHABLE();
5564 }
5565
5566 if (field_type == Primitive::kPrimNot || field_type == Primitive::kPrimDouble) {
5567 // Potential implicit null checks, in the case of reference or
5568 // double fields, are handled in the previous switch statement.
5569 } else {
5570 // Address cases other than reference and double that may require an implicit null check.
Alexandre Rames374ddf32016-11-04 10:40:49 +00005571 // TODO(VIXL): Here and for other calls to `MaybeRecordImplicitNullCheck` in this method, we
5572 // should use a scope and the assembler to emit the load instruction to guarantee that we
5573 // record the pc at the correct position. But the `Assembler` does not automatically handle
5574 // unencodable offsets. Practically, everything is fine because the helper and VIXL, at the time
5575 // of writing, do generate the store instruction last.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005576 codegen_->MaybeRecordImplicitNullCheck(instruction);
5577 }
5578
5579 if (is_volatile) {
5580 if (field_type == Primitive::kPrimNot) {
5581 // Memory barriers, in the case of references, are also handled
5582 // in the previous switch statement.
5583 } else {
5584 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5585 }
5586 }
5587}
5588
5589void LocationsBuilderARMVIXL::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
5590 HandleFieldSet(instruction, instruction->GetFieldInfo());
5591}
5592
5593void InstructionCodeGeneratorARMVIXL::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
5594 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
5595}
5596
5597void LocationsBuilderARMVIXL::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5598 HandleFieldGet(instruction, instruction->GetFieldInfo());
5599}
5600
5601void InstructionCodeGeneratorARMVIXL::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5602 HandleFieldGet(instruction, instruction->GetFieldInfo());
5603}
5604
5605void LocationsBuilderARMVIXL::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5606 HandleFieldGet(instruction, instruction->GetFieldInfo());
5607}
5608
5609void InstructionCodeGeneratorARMVIXL::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5610 HandleFieldGet(instruction, instruction->GetFieldInfo());
5611}
5612
Scott Wakelingc34dba72016-10-03 10:14:44 +01005613void LocationsBuilderARMVIXL::VisitStaticFieldSet(HStaticFieldSet* instruction) {
5614 HandleFieldSet(instruction, instruction->GetFieldInfo());
5615}
5616
5617void InstructionCodeGeneratorARMVIXL::VisitStaticFieldSet(HStaticFieldSet* instruction) {
5618 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
5619}
5620
Artem Serovcfbe9132016-10-14 15:58:56 +01005621void LocationsBuilderARMVIXL::VisitUnresolvedInstanceFieldGet(
5622 HUnresolvedInstanceFieldGet* instruction) {
5623 FieldAccessCallingConventionARMVIXL calling_convention;
5624 codegen_->CreateUnresolvedFieldLocationSummary(
5625 instruction, instruction->GetFieldType(), calling_convention);
5626}
5627
5628void InstructionCodeGeneratorARMVIXL::VisitUnresolvedInstanceFieldGet(
5629 HUnresolvedInstanceFieldGet* instruction) {
5630 FieldAccessCallingConventionARMVIXL calling_convention;
5631 codegen_->GenerateUnresolvedFieldAccess(instruction,
5632 instruction->GetFieldType(),
5633 instruction->GetFieldIndex(),
5634 instruction->GetDexPc(),
5635 calling_convention);
5636}
5637
5638void LocationsBuilderARMVIXL::VisitUnresolvedInstanceFieldSet(
5639 HUnresolvedInstanceFieldSet* instruction) {
5640 FieldAccessCallingConventionARMVIXL calling_convention;
5641 codegen_->CreateUnresolvedFieldLocationSummary(
5642 instruction, instruction->GetFieldType(), calling_convention);
5643}
5644
5645void InstructionCodeGeneratorARMVIXL::VisitUnresolvedInstanceFieldSet(
5646 HUnresolvedInstanceFieldSet* instruction) {
5647 FieldAccessCallingConventionARMVIXL calling_convention;
5648 codegen_->GenerateUnresolvedFieldAccess(instruction,
5649 instruction->GetFieldType(),
5650 instruction->GetFieldIndex(),
5651 instruction->GetDexPc(),
5652 calling_convention);
5653}
5654
5655void LocationsBuilderARMVIXL::VisitUnresolvedStaticFieldGet(
5656 HUnresolvedStaticFieldGet* instruction) {
5657 FieldAccessCallingConventionARMVIXL calling_convention;
5658 codegen_->CreateUnresolvedFieldLocationSummary(
5659 instruction, instruction->GetFieldType(), calling_convention);
5660}
5661
5662void InstructionCodeGeneratorARMVIXL::VisitUnresolvedStaticFieldGet(
5663 HUnresolvedStaticFieldGet* instruction) {
5664 FieldAccessCallingConventionARMVIXL calling_convention;
5665 codegen_->GenerateUnresolvedFieldAccess(instruction,
5666 instruction->GetFieldType(),
5667 instruction->GetFieldIndex(),
5668 instruction->GetDexPc(),
5669 calling_convention);
5670}
5671
5672void LocationsBuilderARMVIXL::VisitUnresolvedStaticFieldSet(
5673 HUnresolvedStaticFieldSet* instruction) {
5674 FieldAccessCallingConventionARMVIXL calling_convention;
5675 codegen_->CreateUnresolvedFieldLocationSummary(
5676 instruction, instruction->GetFieldType(), calling_convention);
5677}
5678
5679void InstructionCodeGeneratorARMVIXL::VisitUnresolvedStaticFieldSet(
5680 HUnresolvedStaticFieldSet* instruction) {
5681 FieldAccessCallingConventionARMVIXL calling_convention;
5682 codegen_->GenerateUnresolvedFieldAccess(instruction,
5683 instruction->GetFieldType(),
5684 instruction->GetFieldIndex(),
5685 instruction->GetDexPc(),
5686 calling_convention);
5687}
5688
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005689void LocationsBuilderARMVIXL::VisitNullCheck(HNullCheck* instruction) {
Artem Serov657022c2016-11-23 14:19:38 +00005690 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005691 locations->SetInAt(0, Location::RequiresRegister());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005692}
5693
5694void CodeGeneratorARMVIXL::GenerateImplicitNullCheck(HNullCheck* instruction) {
5695 if (CanMoveNullCheckToUser(instruction)) {
5696 return;
5697 }
5698
5699 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames374ddf32016-11-04 10:40:49 +00005700 // Ensure the pc position is recorded immediately after the `ldr` instruction.
Artem Serov0fb37192016-12-06 18:13:40 +00005701 ExactAssemblyScope aas(GetVIXLAssembler(),
5702 vixl32::kMaxInstructionSizeInBytes,
5703 CodeBufferCheckScope::kMaximumSize);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005704 __ ldr(temps.Acquire(), MemOperand(InputRegisterAt(instruction, 0)));
5705 RecordPcInfo(instruction, instruction->GetDexPc());
5706}
5707
5708void CodeGeneratorARMVIXL::GenerateExplicitNullCheck(HNullCheck* instruction) {
5709 NullCheckSlowPathARMVIXL* slow_path =
5710 new (GetGraph()->GetArena()) NullCheckSlowPathARMVIXL(instruction);
5711 AddSlowPath(slow_path);
xueliang.zhongf51bc622016-11-04 09:23:32 +00005712 __ CompareAndBranchIfZero(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005713}
5714
5715void InstructionCodeGeneratorARMVIXL::VisitNullCheck(HNullCheck* instruction) {
5716 codegen_->GenerateNullCheck(instruction);
5717}
5718
Scott Wakelingc34dba72016-10-03 10:14:44 +01005719static LoadOperandType GetLoadOperandType(Primitive::Type type) {
5720 switch (type) {
5721 case Primitive::kPrimNot:
5722 return kLoadWord;
5723 case Primitive::kPrimBoolean:
5724 return kLoadUnsignedByte;
5725 case Primitive::kPrimByte:
5726 return kLoadSignedByte;
5727 case Primitive::kPrimChar:
5728 return kLoadUnsignedHalfword;
5729 case Primitive::kPrimShort:
5730 return kLoadSignedHalfword;
5731 case Primitive::kPrimInt:
5732 return kLoadWord;
5733 case Primitive::kPrimLong:
5734 return kLoadWordPair;
5735 case Primitive::kPrimFloat:
5736 return kLoadSWord;
5737 case Primitive::kPrimDouble:
5738 return kLoadDWord;
5739 default:
5740 LOG(FATAL) << "Unreachable type " << type;
5741 UNREACHABLE();
5742 }
5743}
5744
5745static StoreOperandType GetStoreOperandType(Primitive::Type type) {
5746 switch (type) {
5747 case Primitive::kPrimNot:
5748 return kStoreWord;
5749 case Primitive::kPrimBoolean:
5750 case Primitive::kPrimByte:
5751 return kStoreByte;
5752 case Primitive::kPrimChar:
5753 case Primitive::kPrimShort:
5754 return kStoreHalfword;
5755 case Primitive::kPrimInt:
5756 return kStoreWord;
5757 case Primitive::kPrimLong:
5758 return kStoreWordPair;
5759 case Primitive::kPrimFloat:
5760 return kStoreSWord;
5761 case Primitive::kPrimDouble:
5762 return kStoreDWord;
5763 default:
5764 LOG(FATAL) << "Unreachable type " << type;
5765 UNREACHABLE();
5766 }
5767}
5768
5769void CodeGeneratorARMVIXL::LoadFromShiftedRegOffset(Primitive::Type type,
5770 Location out_loc,
5771 vixl32::Register base,
5772 vixl32::Register reg_index,
5773 vixl32::Condition cond) {
5774 uint32_t shift_count = Primitive::ComponentSizeShift(type);
5775 MemOperand mem_address(base, reg_index, vixl32::LSL, shift_count);
5776
5777 switch (type) {
5778 case Primitive::kPrimByte:
5779 __ Ldrsb(cond, RegisterFrom(out_loc), mem_address);
5780 break;
5781 case Primitive::kPrimBoolean:
5782 __ Ldrb(cond, RegisterFrom(out_loc), mem_address);
5783 break;
5784 case Primitive::kPrimShort:
5785 __ Ldrsh(cond, RegisterFrom(out_loc), mem_address);
5786 break;
5787 case Primitive::kPrimChar:
5788 __ Ldrh(cond, RegisterFrom(out_loc), mem_address);
5789 break;
5790 case Primitive::kPrimNot:
5791 case Primitive::kPrimInt:
5792 __ Ldr(cond, RegisterFrom(out_loc), mem_address);
5793 break;
5794 // T32 doesn't support LoadFromShiftedRegOffset mem address mode for these types.
5795 case Primitive::kPrimLong:
5796 case Primitive::kPrimFloat:
5797 case Primitive::kPrimDouble:
5798 default:
5799 LOG(FATAL) << "Unreachable type " << type;
5800 UNREACHABLE();
5801 }
5802}
5803
5804void CodeGeneratorARMVIXL::StoreToShiftedRegOffset(Primitive::Type type,
5805 Location loc,
5806 vixl32::Register base,
5807 vixl32::Register reg_index,
5808 vixl32::Condition cond) {
5809 uint32_t shift_count = Primitive::ComponentSizeShift(type);
5810 MemOperand mem_address(base, reg_index, vixl32::LSL, shift_count);
5811
5812 switch (type) {
5813 case Primitive::kPrimByte:
5814 case Primitive::kPrimBoolean:
5815 __ Strb(cond, RegisterFrom(loc), mem_address);
5816 break;
5817 case Primitive::kPrimShort:
5818 case Primitive::kPrimChar:
5819 __ Strh(cond, RegisterFrom(loc), mem_address);
5820 break;
5821 case Primitive::kPrimNot:
5822 case Primitive::kPrimInt:
5823 __ Str(cond, RegisterFrom(loc), mem_address);
5824 break;
5825 // T32 doesn't support StoreToShiftedRegOffset mem address mode for these types.
5826 case Primitive::kPrimLong:
5827 case Primitive::kPrimFloat:
5828 case Primitive::kPrimDouble:
5829 default:
5830 LOG(FATAL) << "Unreachable type " << type;
5831 UNREACHABLE();
5832 }
5833}
5834
5835void LocationsBuilderARMVIXL::VisitArrayGet(HArrayGet* instruction) {
5836 bool object_array_get_with_read_barrier =
5837 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
5838 LocationSummary* locations =
5839 new (GetGraph()->GetArena()) LocationSummary(instruction,
5840 object_array_get_with_read_barrier ?
5841 LocationSummary::kCallOnSlowPath :
5842 LocationSummary::kNoCall);
5843 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005844 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Scott Wakelingc34dba72016-10-03 10:14:44 +01005845 }
5846 locations->SetInAt(0, Location::RequiresRegister());
5847 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5848 if (Primitive::IsFloatingPointType(instruction->GetType())) {
5849 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5850 } else {
5851 // The output overlaps in the case of an object array get with
5852 // read barriers enabled: we do not want the move to overwrite the
5853 // array's location, as we need it to emit the read barrier.
5854 locations->SetOut(
5855 Location::RequiresRegister(),
5856 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
5857 }
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01005858 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
5859 // We need a temporary register for the read barrier marking slow
5860 // path in CodeGeneratorARMVIXL::GenerateArrayLoadWithBakerReadBarrier.
5861 if (kBakerReadBarrierLinkTimeThunksEnableForFields &&
5862 !Runtime::Current()->UseJitCompilation() &&
5863 instruction->GetIndex()->IsConstant()) {
5864 // Array loads with constant index are treated as field loads.
5865 // If link-time thunks for the Baker read barrier are enabled, for AOT
5866 // constant index loads we need a temporary only if the offset is too big.
5867 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
5868 uint32_t index = instruction->GetIndex()->AsIntConstant()->GetValue();
5869 offset += index << Primitive::ComponentSizeShift(Primitive::kPrimNot);
5870 if (offset >= kReferenceLoadMinFarOffset) {
5871 locations->AddTemp(Location::RequiresRegister());
5872 }
5873 // And we always need the reserved entrypoint register.
5874 locations->AddTemp(Location::RegisterLocation(kBakerCcEntrypointRegister.GetCode()));
5875 } else if (kBakerReadBarrierLinkTimeThunksEnableForArrays &&
5876 !Runtime::Current()->UseJitCompilation() &&
5877 !instruction->GetIndex()->IsConstant()) {
5878 // We need a non-scratch temporary for the array data pointer.
5879 locations->AddTemp(Location::RequiresRegister());
5880 // And we always need the reserved entrypoint register.
5881 locations->AddTemp(Location::RegisterLocation(kBakerCcEntrypointRegister.GetCode()));
5882 } else {
5883 locations->AddTemp(Location::RequiresRegister());
5884 }
5885 } else if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
5886 // Also need a temporary for String compression feature.
Anton Kirilove28d9ae2016-10-25 18:17:23 +01005887 locations->AddTemp(Location::RequiresRegister());
Scott Wakelingc34dba72016-10-03 10:14:44 +01005888 }
5889}
5890
5891void InstructionCodeGeneratorARMVIXL::VisitArrayGet(HArrayGet* instruction) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01005892 LocationSummary* locations = instruction->GetLocations();
5893 Location obj_loc = locations->InAt(0);
5894 vixl32::Register obj = InputRegisterAt(instruction, 0);
5895 Location index = locations->InAt(1);
5896 Location out_loc = locations->Out();
5897 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
5898 Primitive::Type type = instruction->GetType();
5899 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
5900 instruction->IsStringCharAt();
5901 HInstruction* array_instr = instruction->GetArray();
5902 bool has_intermediate_address = array_instr->IsIntermediateAddress();
Scott Wakelingc34dba72016-10-03 10:14:44 +01005903
5904 switch (type) {
5905 case Primitive::kPrimBoolean:
5906 case Primitive::kPrimByte:
5907 case Primitive::kPrimShort:
5908 case Primitive::kPrimChar:
5909 case Primitive::kPrimInt: {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005910 vixl32::Register length;
5911 if (maybe_compressed_char_at) {
5912 length = RegisterFrom(locations->GetTemp(0));
5913 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
5914 GetAssembler()->LoadFromOffset(kLoadWord, length, obj, count_offset);
5915 codegen_->MaybeRecordImplicitNullCheck(instruction);
5916 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01005917 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00005918 int32_t const_index = Int32ConstantFrom(index);
Scott Wakelingc34dba72016-10-03 10:14:44 +01005919 if (maybe_compressed_char_at) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01005920 vixl32::Label uncompressed_load, done;
Anton Kirilov6f644202017-02-27 18:29:45 +00005921 vixl32::Label* final_label = codegen_->GetFinalLabel(instruction, &done);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005922 __ Lsrs(length, length, 1u); // LSRS has a 16-bit encoding, TST (immediate) does not.
5923 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
5924 "Expecting 0=compressed, 1=uncompressed");
Artem Serov517d9f62016-12-12 15:51:15 +00005925 __ B(cs, &uncompressed_load, /* far_target */ false);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01005926 GetAssembler()->LoadFromOffset(kLoadUnsignedByte,
5927 RegisterFrom(out_loc),
5928 obj,
5929 data_offset + const_index);
Anton Kirilov6f644202017-02-27 18:29:45 +00005930 __ B(final_label);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01005931 __ Bind(&uncompressed_load);
5932 GetAssembler()->LoadFromOffset(GetLoadOperandType(Primitive::kPrimChar),
5933 RegisterFrom(out_loc),
5934 obj,
5935 data_offset + (const_index << 1));
Anton Kirilov6f644202017-02-27 18:29:45 +00005936 if (done.IsReferenced()) {
5937 __ Bind(&done);
5938 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01005939 } else {
5940 uint32_t full_offset = data_offset + (const_index << Primitive::ComponentSizeShift(type));
5941
5942 LoadOperandType load_type = GetLoadOperandType(type);
5943 GetAssembler()->LoadFromOffset(load_type, RegisterFrom(out_loc), obj, full_offset);
5944 }
5945 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005946 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01005947 vixl32::Register temp = temps.Acquire();
5948
5949 if (has_intermediate_address) {
Artem Serov2bbc9532016-10-21 11:51:50 +01005950 // We do not need to compute the intermediate address from the array: the
5951 // input instruction has done it already. See the comment in
5952 // `TryExtractArrayAccessAddress()`.
5953 if (kIsDebugBuild) {
5954 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
Anton Kirilov644032c2016-12-06 17:51:43 +00005955 DCHECK_EQ(Uint64ConstantFrom(tmp->GetOffset()), data_offset);
Artem Serov2bbc9532016-10-21 11:51:50 +01005956 }
5957 temp = obj;
Scott Wakelingc34dba72016-10-03 10:14:44 +01005958 } else {
5959 __ Add(temp, obj, data_offset);
5960 }
5961 if (maybe_compressed_char_at) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01005962 vixl32::Label uncompressed_load, done;
Anton Kirilov6f644202017-02-27 18:29:45 +00005963 vixl32::Label* final_label = codegen_->GetFinalLabel(instruction, &done);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005964 __ Lsrs(length, length, 1u); // LSRS has a 16-bit encoding, TST (immediate) does not.
5965 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
5966 "Expecting 0=compressed, 1=uncompressed");
Artem Serov517d9f62016-12-12 15:51:15 +00005967 __ B(cs, &uncompressed_load, /* far_target */ false);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01005968 __ Ldrb(RegisterFrom(out_loc), MemOperand(temp, RegisterFrom(index), vixl32::LSL, 0));
Anton Kirilov6f644202017-02-27 18:29:45 +00005969 __ B(final_label);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01005970 __ Bind(&uncompressed_load);
5971 __ Ldrh(RegisterFrom(out_loc), MemOperand(temp, RegisterFrom(index), vixl32::LSL, 1));
Anton Kirilov6f644202017-02-27 18:29:45 +00005972 if (done.IsReferenced()) {
5973 __ Bind(&done);
5974 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01005975 } else {
5976 codegen_->LoadFromShiftedRegOffset(type, out_loc, temp, RegisterFrom(index));
5977 }
5978 }
5979 break;
5980 }
5981
5982 case Primitive::kPrimNot: {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005983 // The read barrier instrumentation of object ArrayGet
5984 // instructions does not support the HIntermediateAddress
5985 // instruction.
5986 DCHECK(!(has_intermediate_address && kEmitCompilerReadBarrier));
5987
Scott Wakelingc34dba72016-10-03 10:14:44 +01005988 static_assert(
5989 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5990 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
5991 // /* HeapReference<Object> */ out =
5992 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
5993 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005994 Location temp = locations->GetTemp(0);
5995 // Note that a potential implicit null check is handled in this
5996 // CodeGeneratorARMVIXL::GenerateArrayLoadWithBakerReadBarrier call.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01005997 DCHECK(!instruction->CanDoImplicitNullCheckOn(instruction->InputAt(0)));
5998 if (index.IsConstant()) {
5999 // Array load with a constant index can be treated as a field load.
6000 data_offset += Int32ConstantFrom(index) << Primitive::ComponentSizeShift(type);
6001 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6002 out_loc,
6003 obj,
6004 data_offset,
6005 locations->GetTemp(0),
6006 /* needs_null_check */ false);
6007 } else {
6008 codegen_->GenerateArrayLoadWithBakerReadBarrier(
6009 instruction, out_loc, obj, data_offset, index, temp, /* needs_null_check */ false);
6010 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006011 } else {
6012 vixl32::Register out = OutputRegister(instruction);
6013 if (index.IsConstant()) {
6014 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00006015 (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006016 GetAssembler()->LoadFromOffset(kLoadWord, out, obj, offset);
Alexandre Rames374ddf32016-11-04 10:40:49 +00006017 // TODO(VIXL): Here and for other calls to `MaybeRecordImplicitNullCheck` in this method,
6018 // we should use a scope and the assembler to emit the load instruction to guarantee that
6019 // we record the pc at the correct position. But the `Assembler` does not automatically
6020 // handle unencodable offsets. Practically, everything is fine because the helper and
6021 // VIXL, at the time of writing, do generate the store instruction last.
Scott Wakelingc34dba72016-10-03 10:14:44 +01006022 codegen_->MaybeRecordImplicitNullCheck(instruction);
6023 // If read barriers are enabled, emit read barriers other than
6024 // Baker's using a slow path (and also unpoison the loaded
6025 // reference, if heap poisoning is enabled).
6026 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
6027 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006028 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006029 vixl32::Register temp = temps.Acquire();
6030
6031 if (has_intermediate_address) {
Artem Serov2bbc9532016-10-21 11:51:50 +01006032 // We do not need to compute the intermediate address from the array: the
6033 // input instruction has done it already. See the comment in
6034 // `TryExtractArrayAccessAddress()`.
6035 if (kIsDebugBuild) {
6036 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
Anton Kirilov644032c2016-12-06 17:51:43 +00006037 DCHECK_EQ(Uint64ConstantFrom(tmp->GetOffset()), data_offset);
Artem Serov2bbc9532016-10-21 11:51:50 +01006038 }
6039 temp = obj;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006040 } else {
6041 __ Add(temp, obj, data_offset);
6042 }
6043 codegen_->LoadFromShiftedRegOffset(type, out_loc, temp, RegisterFrom(index));
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006044 temps.Close();
Alexandre Rames374ddf32016-11-04 10:40:49 +00006045 // TODO(VIXL): Use a scope to ensure that we record the pc position immediately after the
6046 // load instruction. Practically, everything is fine because the helper and VIXL, at the
6047 // time of writing, do generate the store instruction last.
Scott Wakelingc34dba72016-10-03 10:14:44 +01006048 codegen_->MaybeRecordImplicitNullCheck(instruction);
6049 // If read barriers are enabled, emit read barriers other than
6050 // Baker's using a slow path (and also unpoison the loaded
6051 // reference, if heap poisoning is enabled).
6052 codegen_->MaybeGenerateReadBarrierSlow(
6053 instruction, out_loc, out_loc, obj_loc, data_offset, index);
6054 }
6055 }
6056 break;
6057 }
6058
6059 case Primitive::kPrimLong: {
6060 if (index.IsConstant()) {
6061 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00006062 (Int32ConstantFrom(index) << TIMES_8) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006063 GetAssembler()->LoadFromOffset(kLoadWordPair, LowRegisterFrom(out_loc), obj, offset);
6064 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006065 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006066 vixl32::Register temp = temps.Acquire();
6067 __ Add(temp, obj, Operand(RegisterFrom(index), vixl32::LSL, TIMES_8));
6068 GetAssembler()->LoadFromOffset(kLoadWordPair, LowRegisterFrom(out_loc), temp, data_offset);
6069 }
6070 break;
6071 }
6072
6073 case Primitive::kPrimFloat: {
6074 vixl32::SRegister out = SRegisterFrom(out_loc);
6075 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006076 size_t offset = (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006077 GetAssembler()->LoadSFromOffset(out, obj, offset);
6078 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006079 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006080 vixl32::Register temp = temps.Acquire();
6081 __ Add(temp, obj, Operand(RegisterFrom(index), vixl32::LSL, TIMES_4));
6082 GetAssembler()->LoadSFromOffset(out, temp, data_offset);
6083 }
6084 break;
6085 }
6086
6087 case Primitive::kPrimDouble: {
6088 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006089 size_t offset = (Int32ConstantFrom(index) << TIMES_8) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006090 GetAssembler()->LoadDFromOffset(DRegisterFrom(out_loc), obj, offset);
6091 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006092 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006093 vixl32::Register temp = temps.Acquire();
6094 __ Add(temp, obj, Operand(RegisterFrom(index), vixl32::LSL, TIMES_8));
6095 GetAssembler()->LoadDFromOffset(DRegisterFrom(out_loc), temp, data_offset);
6096 }
6097 break;
6098 }
6099
6100 case Primitive::kPrimVoid:
6101 LOG(FATAL) << "Unreachable type " << type;
6102 UNREACHABLE();
6103 }
6104
6105 if (type == Primitive::kPrimNot) {
6106 // Potential implicit null checks, in the case of reference
6107 // arrays, are handled in the previous switch statement.
6108 } else if (!maybe_compressed_char_at) {
Alexandre Rames374ddf32016-11-04 10:40:49 +00006109 // TODO(VIXL): Use a scope to ensure we record the pc info immediately after
6110 // the preceding load instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01006111 codegen_->MaybeRecordImplicitNullCheck(instruction);
6112 }
6113}
6114
6115void LocationsBuilderARMVIXL::VisitArraySet(HArraySet* instruction) {
6116 Primitive::Type value_type = instruction->GetComponentType();
6117
6118 bool needs_write_barrier =
6119 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
6120 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
6121
6122 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
6123 instruction,
6124 may_need_runtime_call_for_type_check ?
6125 LocationSummary::kCallOnSlowPath :
6126 LocationSummary::kNoCall);
6127
6128 locations->SetInAt(0, Location::RequiresRegister());
6129 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
6130 if (Primitive::IsFloatingPointType(value_type)) {
6131 locations->SetInAt(2, Location::RequiresFpuRegister());
6132 } else {
6133 locations->SetInAt(2, Location::RequiresRegister());
6134 }
6135 if (needs_write_barrier) {
6136 // Temporary registers for the write barrier.
6137 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
6138 locations->AddTemp(Location::RequiresRegister());
6139 }
6140}
6141
6142void InstructionCodeGeneratorARMVIXL::VisitArraySet(HArraySet* instruction) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006143 LocationSummary* locations = instruction->GetLocations();
6144 vixl32::Register array = InputRegisterAt(instruction, 0);
6145 Location index = locations->InAt(1);
6146 Primitive::Type value_type = instruction->GetComponentType();
6147 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
6148 bool needs_write_barrier =
6149 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
6150 uint32_t data_offset =
6151 mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
6152 Location value_loc = locations->InAt(2);
6153 HInstruction* array_instr = instruction->GetArray();
6154 bool has_intermediate_address = array_instr->IsIntermediateAddress();
Scott Wakelingc34dba72016-10-03 10:14:44 +01006155
6156 switch (value_type) {
6157 case Primitive::kPrimBoolean:
6158 case Primitive::kPrimByte:
6159 case Primitive::kPrimShort:
6160 case Primitive::kPrimChar:
6161 case Primitive::kPrimInt: {
6162 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006163 int32_t const_index = Int32ConstantFrom(index);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006164 uint32_t full_offset =
6165 data_offset + (const_index << Primitive::ComponentSizeShift(value_type));
6166 StoreOperandType store_type = GetStoreOperandType(value_type);
6167 GetAssembler()->StoreToOffset(store_type, RegisterFrom(value_loc), array, full_offset);
6168 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006169 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006170 vixl32::Register temp = temps.Acquire();
6171
6172 if (has_intermediate_address) {
Artem Serov2bbc9532016-10-21 11:51:50 +01006173 // We do not need to compute the intermediate address from the array: the
6174 // input instruction has done it already. See the comment in
6175 // `TryExtractArrayAccessAddress()`.
6176 if (kIsDebugBuild) {
6177 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
Anton Kirilov644032c2016-12-06 17:51:43 +00006178 DCHECK_EQ(Uint64ConstantFrom(tmp->GetOffset()), data_offset);
Artem Serov2bbc9532016-10-21 11:51:50 +01006179 }
6180 temp = array;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006181 } else {
6182 __ Add(temp, array, data_offset);
6183 }
6184 codegen_->StoreToShiftedRegOffset(value_type, value_loc, temp, RegisterFrom(index));
6185 }
6186 break;
6187 }
6188
6189 case Primitive::kPrimNot: {
6190 vixl32::Register value = RegisterFrom(value_loc);
6191 // TryExtractArrayAccessAddress optimization is never applied for non-primitive ArraySet.
6192 // See the comment in instruction_simplifier_shared.cc.
6193 DCHECK(!has_intermediate_address);
6194
6195 if (instruction->InputAt(2)->IsNullConstant()) {
6196 // Just setting null.
6197 if (index.IsConstant()) {
6198 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00006199 (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006200 GetAssembler()->StoreToOffset(kStoreWord, value, array, offset);
6201 } else {
6202 DCHECK(index.IsRegister()) << index;
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006203 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006204 vixl32::Register temp = temps.Acquire();
6205 __ Add(temp, array, data_offset);
6206 codegen_->StoreToShiftedRegOffset(value_type, value_loc, temp, RegisterFrom(index));
6207 }
Alexandre Rames374ddf32016-11-04 10:40:49 +00006208 // TODO(VIXL): Use a scope to ensure we record the pc info immediately after the preceding
6209 // store instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01006210 codegen_->MaybeRecordImplicitNullCheck(instruction);
6211 DCHECK(!needs_write_barrier);
6212 DCHECK(!may_need_runtime_call_for_type_check);
6213 break;
6214 }
6215
6216 DCHECK(needs_write_barrier);
6217 Location temp1_loc = locations->GetTemp(0);
6218 vixl32::Register temp1 = RegisterFrom(temp1_loc);
6219 Location temp2_loc = locations->GetTemp(1);
6220 vixl32::Register temp2 = RegisterFrom(temp2_loc);
6221 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6222 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6223 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6224 vixl32::Label done;
Anton Kirilov6f644202017-02-27 18:29:45 +00006225 vixl32::Label* final_label = codegen_->GetFinalLabel(instruction, &done);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006226 SlowPathCodeARMVIXL* slow_path = nullptr;
6227
6228 if (may_need_runtime_call_for_type_check) {
6229 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARMVIXL(instruction);
6230 codegen_->AddSlowPath(slow_path);
6231 if (instruction->GetValueCanBeNull()) {
6232 vixl32::Label non_zero;
xueliang.zhongf51bc622016-11-04 09:23:32 +00006233 __ CompareAndBranchIfNonZero(value, &non_zero);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006234 if (index.IsConstant()) {
6235 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00006236 (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006237 GetAssembler()->StoreToOffset(kStoreWord, value, array, offset);
6238 } else {
6239 DCHECK(index.IsRegister()) << index;
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006240 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006241 vixl32::Register temp = temps.Acquire();
6242 __ Add(temp, array, data_offset);
6243 codegen_->StoreToShiftedRegOffset(value_type, value_loc, temp, RegisterFrom(index));
6244 }
Alexandre Rames374ddf32016-11-04 10:40:49 +00006245 // TODO(VIXL): Use a scope to ensure we record the pc info immediately after the preceding
6246 // store instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01006247 codegen_->MaybeRecordImplicitNullCheck(instruction);
Anton Kirilov6f644202017-02-27 18:29:45 +00006248 __ B(final_label);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006249 __ Bind(&non_zero);
6250 }
6251
6252 // Note that when read barriers are enabled, the type checks
6253 // are performed without read barriers. This is fine, even in
6254 // the case where a class object is in the from-space after
6255 // the flip, as a comparison involving such a type would not
6256 // produce a false positive; it may of course produce a false
6257 // negative, in which case we would take the ArraySet slow
6258 // path.
6259
Alexandre Rames374ddf32016-11-04 10:40:49 +00006260 {
6261 // Ensure we record the pc position immediately after the `ldr` instruction.
Artem Serov0fb37192016-12-06 18:13:40 +00006262 ExactAssemblyScope aas(GetVIXLAssembler(),
6263 vixl32::kMaxInstructionSizeInBytes,
6264 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00006265 // /* HeapReference<Class> */ temp1 = array->klass_
6266 __ ldr(temp1, MemOperand(array, class_offset));
6267 codegen_->MaybeRecordImplicitNullCheck(instruction);
6268 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006269 GetAssembler()->MaybeUnpoisonHeapReference(temp1);
6270
6271 // /* HeapReference<Class> */ temp1 = temp1->component_type_
6272 GetAssembler()->LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
6273 // /* HeapReference<Class> */ temp2 = value->klass_
6274 GetAssembler()->LoadFromOffset(kLoadWord, temp2, value, class_offset);
6275 // If heap poisoning is enabled, no need to unpoison `temp1`
6276 // nor `temp2`, as we are comparing two poisoned references.
6277 __ Cmp(temp1, temp2);
6278
6279 if (instruction->StaticTypeOfArrayIsObjectArray()) {
6280 vixl32::Label do_put;
Artem Serov517d9f62016-12-12 15:51:15 +00006281 __ B(eq, &do_put, /* far_target */ false);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006282 // If heap poisoning is enabled, the `temp1` reference has
6283 // not been unpoisoned yet; unpoison it now.
6284 GetAssembler()->MaybeUnpoisonHeapReference(temp1);
6285
6286 // /* HeapReference<Class> */ temp1 = temp1->super_class_
6287 GetAssembler()->LoadFromOffset(kLoadWord, temp1, temp1, super_offset);
6288 // If heap poisoning is enabled, no need to unpoison
6289 // `temp1`, as we are comparing against null below.
xueliang.zhongf51bc622016-11-04 09:23:32 +00006290 __ CompareAndBranchIfNonZero(temp1, slow_path->GetEntryLabel());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006291 __ Bind(&do_put);
6292 } else {
6293 __ B(ne, slow_path->GetEntryLabel());
6294 }
6295 }
6296
6297 vixl32::Register source = value;
6298 if (kPoisonHeapReferences) {
6299 // Note that in the case where `value` is a null reference,
6300 // we do not enter this block, as a null reference does not
6301 // need poisoning.
6302 DCHECK_EQ(value_type, Primitive::kPrimNot);
6303 __ Mov(temp1, value);
6304 GetAssembler()->PoisonHeapReference(temp1);
6305 source = temp1;
6306 }
6307
6308 if (index.IsConstant()) {
6309 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00006310 (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006311 GetAssembler()->StoreToOffset(kStoreWord, source, array, offset);
6312 } else {
6313 DCHECK(index.IsRegister()) << index;
6314
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006315 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006316 vixl32::Register temp = temps.Acquire();
6317 __ Add(temp, array, data_offset);
6318 codegen_->StoreToShiftedRegOffset(value_type,
6319 LocationFrom(source),
6320 temp,
6321 RegisterFrom(index));
6322 }
6323
6324 if (!may_need_runtime_call_for_type_check) {
Alexandre Rames374ddf32016-11-04 10:40:49 +00006325 // TODO(VIXL): Ensure we record the pc position immediately after the preceding store
6326 // instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01006327 codegen_->MaybeRecordImplicitNullCheck(instruction);
6328 }
6329
6330 codegen_->MarkGCCard(temp1, temp2, array, value, instruction->GetValueCanBeNull());
6331
6332 if (done.IsReferenced()) {
6333 __ Bind(&done);
6334 }
6335
6336 if (slow_path != nullptr) {
6337 __ Bind(slow_path->GetExitLabel());
6338 }
6339
6340 break;
6341 }
6342
6343 case Primitive::kPrimLong: {
6344 Location value = locations->InAt(2);
6345 if (index.IsConstant()) {
6346 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00006347 (Int32ConstantFrom(index) << TIMES_8) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006348 GetAssembler()->StoreToOffset(kStoreWordPair, LowRegisterFrom(value), array, offset);
6349 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006350 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006351 vixl32::Register temp = temps.Acquire();
6352 __ Add(temp, array, Operand(RegisterFrom(index), vixl32::LSL, TIMES_8));
6353 GetAssembler()->StoreToOffset(kStoreWordPair, LowRegisterFrom(value), temp, data_offset);
6354 }
6355 break;
6356 }
6357
6358 case Primitive::kPrimFloat: {
6359 Location value = locations->InAt(2);
6360 DCHECK(value.IsFpuRegister());
6361 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006362 size_t offset = (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006363 GetAssembler()->StoreSToOffset(SRegisterFrom(value), array, offset);
6364 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006365 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006366 vixl32::Register temp = temps.Acquire();
6367 __ Add(temp, array, Operand(RegisterFrom(index), vixl32::LSL, TIMES_4));
6368 GetAssembler()->StoreSToOffset(SRegisterFrom(value), temp, data_offset);
6369 }
6370 break;
6371 }
6372
6373 case Primitive::kPrimDouble: {
6374 Location value = locations->InAt(2);
6375 DCHECK(value.IsFpuRegisterPair());
6376 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006377 size_t offset = (Int32ConstantFrom(index) << TIMES_8) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006378 GetAssembler()->StoreDToOffset(DRegisterFrom(value), array, offset);
6379 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006380 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006381 vixl32::Register temp = temps.Acquire();
6382 __ Add(temp, array, Operand(RegisterFrom(index), vixl32::LSL, TIMES_8));
6383 GetAssembler()->StoreDToOffset(DRegisterFrom(value), temp, data_offset);
6384 }
6385 break;
6386 }
6387
6388 case Primitive::kPrimVoid:
6389 LOG(FATAL) << "Unreachable type " << value_type;
6390 UNREACHABLE();
6391 }
6392
6393 // Objects are handled in the switch.
6394 if (value_type != Primitive::kPrimNot) {
Alexandre Rames374ddf32016-11-04 10:40:49 +00006395 // TODO(VIXL): Ensure we record the pc position immediately after the preceding store
6396 // instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01006397 codegen_->MaybeRecordImplicitNullCheck(instruction);
6398 }
6399}
6400
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006401void LocationsBuilderARMVIXL::VisitArrayLength(HArrayLength* instruction) {
6402 LocationSummary* locations =
6403 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6404 locations->SetInAt(0, Location::RequiresRegister());
6405 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6406}
6407
6408void InstructionCodeGeneratorARMVIXL::VisitArrayLength(HArrayLength* instruction) {
6409 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
6410 vixl32::Register obj = InputRegisterAt(instruction, 0);
6411 vixl32::Register out = OutputRegister(instruction);
Alexandre Rames374ddf32016-11-04 10:40:49 +00006412 {
Artem Serov0fb37192016-12-06 18:13:40 +00006413 ExactAssemblyScope aas(GetVIXLAssembler(),
6414 vixl32::kMaxInstructionSizeInBytes,
6415 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00006416 __ ldr(out, MemOperand(obj, offset));
6417 codegen_->MaybeRecordImplicitNullCheck(instruction);
6418 }
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006419 // Mask out compression flag from String's array length.
6420 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006421 __ Lsr(out, out, 1u);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006422 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006423}
6424
Artem Serov2bbc9532016-10-21 11:51:50 +01006425void LocationsBuilderARMVIXL::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Artem Serov2bbc9532016-10-21 11:51:50 +01006426 LocationSummary* locations =
6427 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6428
6429 locations->SetInAt(0, Location::RequiresRegister());
6430 locations->SetInAt(1, Location::RegisterOrConstant(instruction->GetOffset()));
6431 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6432}
6433
6434void InstructionCodeGeneratorARMVIXL::VisitIntermediateAddress(HIntermediateAddress* instruction) {
6435 vixl32::Register out = OutputRegister(instruction);
6436 vixl32::Register first = InputRegisterAt(instruction, 0);
6437 Location second = instruction->GetLocations()->InAt(1);
6438
Artem Serov2bbc9532016-10-21 11:51:50 +01006439 if (second.IsRegister()) {
6440 __ Add(out, first, RegisterFrom(second));
6441 } else {
Anton Kirilov644032c2016-12-06 17:51:43 +00006442 __ Add(out, first, Int32ConstantFrom(second));
Artem Serov2bbc9532016-10-21 11:51:50 +01006443 }
6444}
6445
Artem Serove1811ed2017-04-27 16:50:47 +01006446void LocationsBuilderARMVIXL::VisitIntermediateAddressIndex(
6447 HIntermediateAddressIndex* instruction) {
6448 LOG(FATAL) << "Unreachable " << instruction->GetId();
6449}
6450
6451void InstructionCodeGeneratorARMVIXL::VisitIntermediateAddressIndex(
6452 HIntermediateAddressIndex* instruction) {
6453 LOG(FATAL) << "Unreachable " << instruction->GetId();
6454}
6455
Scott Wakelingc34dba72016-10-03 10:14:44 +01006456void LocationsBuilderARMVIXL::VisitBoundsCheck(HBoundsCheck* instruction) {
6457 RegisterSet caller_saves = RegisterSet::Empty();
6458 InvokeRuntimeCallingConventionARMVIXL calling_convention;
6459 caller_saves.Add(LocationFrom(calling_convention.GetRegisterAt(0)));
6460 caller_saves.Add(LocationFrom(calling_convention.GetRegisterAt(1)));
6461 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Artem Serov2dd053d2017-03-08 14:54:06 +00006462
6463 HInstruction* index = instruction->InputAt(0);
6464 HInstruction* length = instruction->InputAt(1);
6465 // If both index and length are constants we can statically check the bounds. But if at least one
6466 // of them is not encodable ArmEncodableConstantOrRegister will create
6467 // Location::RequiresRegister() which is not desired to happen. Instead we create constant
6468 // locations.
6469 bool both_const = index->IsConstant() && length->IsConstant();
6470 locations->SetInAt(0, both_const
6471 ? Location::ConstantLocation(index->AsConstant())
6472 : ArmEncodableConstantOrRegister(index, CMP));
6473 locations->SetInAt(1, both_const
6474 ? Location::ConstantLocation(length->AsConstant())
6475 : ArmEncodableConstantOrRegister(length, CMP));
Scott Wakelingc34dba72016-10-03 10:14:44 +01006476}
6477
6478void InstructionCodeGeneratorARMVIXL::VisitBoundsCheck(HBoundsCheck* instruction) {
Artem Serov2dd053d2017-03-08 14:54:06 +00006479 LocationSummary* locations = instruction->GetLocations();
6480 Location index_loc = locations->InAt(0);
6481 Location length_loc = locations->InAt(1);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006482
Artem Serov2dd053d2017-03-08 14:54:06 +00006483 if (length_loc.IsConstant()) {
6484 int32_t length = Int32ConstantFrom(length_loc);
6485 if (index_loc.IsConstant()) {
6486 // BCE will remove the bounds check if we are guaranteed to pass.
6487 int32_t index = Int32ConstantFrom(index_loc);
6488 if (index < 0 || index >= length) {
6489 SlowPathCodeARMVIXL* slow_path =
6490 new (GetGraph()->GetArena()) BoundsCheckSlowPathARMVIXL(instruction);
6491 codegen_->AddSlowPath(slow_path);
6492 __ B(slow_path->GetEntryLabel());
6493 } else {
6494 // Some optimization after BCE may have generated this, and we should not
6495 // generate a bounds check if it is a valid range.
6496 }
6497 return;
6498 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006499
Artem Serov2dd053d2017-03-08 14:54:06 +00006500 SlowPathCodeARMVIXL* slow_path =
6501 new (GetGraph()->GetArena()) BoundsCheckSlowPathARMVIXL(instruction);
6502 __ Cmp(RegisterFrom(index_loc), length);
6503 codegen_->AddSlowPath(slow_path);
6504 __ B(hs, slow_path->GetEntryLabel());
6505 } else {
6506 SlowPathCodeARMVIXL* slow_path =
6507 new (GetGraph()->GetArena()) BoundsCheckSlowPathARMVIXL(instruction);
6508 __ Cmp(RegisterFrom(length_loc), InputOperandAt(instruction, 0));
6509 codegen_->AddSlowPath(slow_path);
6510 __ B(ls, slow_path->GetEntryLabel());
6511 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006512}
6513
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006514void CodeGeneratorARMVIXL::MarkGCCard(vixl32::Register temp,
6515 vixl32::Register card,
6516 vixl32::Register object,
6517 vixl32::Register value,
6518 bool can_be_null) {
6519 vixl32::Label is_null;
6520 if (can_be_null) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00006521 __ CompareAndBranchIfZero(value, &is_null);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006522 }
6523 GetAssembler()->LoadFromOffset(
6524 kLoadWord, card, tr, Thread::CardTableOffset<kArmPointerSize>().Int32Value());
Scott Wakelingb77051e2016-11-21 19:46:00 +00006525 __ Lsr(temp, object, Operand::From(gc::accounting::CardTable::kCardShift));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006526 __ Strb(card, MemOperand(card, temp));
6527 if (can_be_null) {
6528 __ Bind(&is_null);
6529 }
6530}
6531
Scott Wakelingfe885462016-09-22 10:24:38 +01006532void LocationsBuilderARMVIXL::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
6533 LOG(FATAL) << "Unreachable";
6534}
6535
6536void InstructionCodeGeneratorARMVIXL::VisitParallelMove(HParallelMove* instruction) {
6537 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
6538}
6539
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006540void LocationsBuilderARMVIXL::VisitSuspendCheck(HSuspendCheck* instruction) {
Artem Serov657022c2016-11-23 14:19:38 +00006541 LocationSummary* locations =
6542 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
6543 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006544}
6545
6546void InstructionCodeGeneratorARMVIXL::VisitSuspendCheck(HSuspendCheck* instruction) {
6547 HBasicBlock* block = instruction->GetBlock();
6548 if (block->GetLoopInformation() != nullptr) {
6549 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
6550 // The back edge will generate the suspend check.
6551 return;
6552 }
6553 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
6554 // The goto will generate the suspend check.
6555 return;
6556 }
6557 GenerateSuspendCheck(instruction, nullptr);
6558}
6559
6560void InstructionCodeGeneratorARMVIXL::GenerateSuspendCheck(HSuspendCheck* instruction,
6561 HBasicBlock* successor) {
6562 SuspendCheckSlowPathARMVIXL* slow_path =
6563 down_cast<SuspendCheckSlowPathARMVIXL*>(instruction->GetSlowPath());
6564 if (slow_path == nullptr) {
6565 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARMVIXL(instruction, successor);
6566 instruction->SetSlowPath(slow_path);
6567 codegen_->AddSlowPath(slow_path);
6568 if (successor != nullptr) {
6569 DCHECK(successor->IsLoopHeader());
6570 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
6571 }
6572 } else {
6573 DCHECK_EQ(slow_path->GetSuccessor(), successor);
6574 }
6575
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006576 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006577 vixl32::Register temp = temps.Acquire();
6578 GetAssembler()->LoadFromOffset(
6579 kLoadUnsignedHalfword, temp, tr, Thread::ThreadFlagsOffset<kArmPointerSize>().Int32Value());
6580 if (successor == nullptr) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00006581 __ CompareAndBranchIfNonZero(temp, slow_path->GetEntryLabel());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006582 __ Bind(slow_path->GetReturnLabel());
6583 } else {
xueliang.zhongf51bc622016-11-04 09:23:32 +00006584 __ CompareAndBranchIfZero(temp, codegen_->GetLabelOf(successor));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006585 __ B(slow_path->GetEntryLabel());
6586 }
6587}
6588
Scott Wakelingfe885462016-09-22 10:24:38 +01006589ArmVIXLAssembler* ParallelMoveResolverARMVIXL::GetAssembler() const {
6590 return codegen_->GetAssembler();
6591}
6592
6593void ParallelMoveResolverARMVIXL::EmitMove(size_t index) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006594 UseScratchRegisterScope temps(GetAssembler()->GetVIXLAssembler());
Scott Wakelingfe885462016-09-22 10:24:38 +01006595 MoveOperands* move = moves_[index];
6596 Location source = move->GetSource();
6597 Location destination = move->GetDestination();
6598
6599 if (source.IsRegister()) {
6600 if (destination.IsRegister()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006601 __ Mov(RegisterFrom(destination), RegisterFrom(source));
Scott Wakelingfe885462016-09-22 10:24:38 +01006602 } else if (destination.IsFpuRegister()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006603 __ Vmov(SRegisterFrom(destination), RegisterFrom(source));
Scott Wakelingfe885462016-09-22 10:24:38 +01006604 } else {
6605 DCHECK(destination.IsStackSlot());
6606 GetAssembler()->StoreToOffset(kStoreWord,
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006607 RegisterFrom(source),
Scott Wakelingfe885462016-09-22 10:24:38 +01006608 sp,
6609 destination.GetStackIndex());
6610 }
6611 } else if (source.IsStackSlot()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006612 if (destination.IsRegister()) {
6613 GetAssembler()->LoadFromOffset(kLoadWord,
6614 RegisterFrom(destination),
6615 sp,
6616 source.GetStackIndex());
6617 } else if (destination.IsFpuRegister()) {
6618 GetAssembler()->LoadSFromOffset(SRegisterFrom(destination), sp, source.GetStackIndex());
6619 } else {
6620 DCHECK(destination.IsStackSlot());
6621 vixl32::Register temp = temps.Acquire();
6622 GetAssembler()->LoadFromOffset(kLoadWord, temp, sp, source.GetStackIndex());
6623 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
6624 }
Scott Wakelingfe885462016-09-22 10:24:38 +01006625 } else if (source.IsFpuRegister()) {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01006626 if (destination.IsRegister()) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006627 __ Vmov(RegisterFrom(destination), SRegisterFrom(source));
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01006628 } else if (destination.IsFpuRegister()) {
6629 __ Vmov(SRegisterFrom(destination), SRegisterFrom(source));
6630 } else {
6631 DCHECK(destination.IsStackSlot());
6632 GetAssembler()->StoreSToOffset(SRegisterFrom(source), sp, destination.GetStackIndex());
6633 }
Scott Wakelingfe885462016-09-22 10:24:38 +01006634 } else if (source.IsDoubleStackSlot()) {
Alexandre Rames9c19bd62016-10-24 11:50:32 +01006635 if (destination.IsDoubleStackSlot()) {
6636 vixl32::DRegister temp = temps.AcquireD();
6637 GetAssembler()->LoadDFromOffset(temp, sp, source.GetStackIndex());
6638 GetAssembler()->StoreDToOffset(temp, sp, destination.GetStackIndex());
6639 } else if (destination.IsRegisterPair()) {
6640 DCHECK(ExpectedPairLayout(destination));
6641 GetAssembler()->LoadFromOffset(
6642 kLoadWordPair, LowRegisterFrom(destination), sp, source.GetStackIndex());
6643 } else {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01006644 DCHECK(destination.IsFpuRegisterPair()) << destination;
6645 GetAssembler()->LoadDFromOffset(DRegisterFrom(destination), sp, source.GetStackIndex());
Alexandre Rames9c19bd62016-10-24 11:50:32 +01006646 }
Scott Wakelingfe885462016-09-22 10:24:38 +01006647 } else if (source.IsRegisterPair()) {
6648 if (destination.IsRegisterPair()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006649 __ Mov(LowRegisterFrom(destination), LowRegisterFrom(source));
6650 __ Mov(HighRegisterFrom(destination), HighRegisterFrom(source));
Scott Wakelingfe885462016-09-22 10:24:38 +01006651 } else if (destination.IsFpuRegisterPair()) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006652 __ Vmov(DRegisterFrom(destination), LowRegisterFrom(source), HighRegisterFrom(source));
Scott Wakelingfe885462016-09-22 10:24:38 +01006653 } else {
6654 DCHECK(destination.IsDoubleStackSlot()) << destination;
6655 DCHECK(ExpectedPairLayout(source));
6656 GetAssembler()->StoreToOffset(kStoreWordPair,
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006657 LowRegisterFrom(source),
Scott Wakelingfe885462016-09-22 10:24:38 +01006658 sp,
6659 destination.GetStackIndex());
6660 }
6661 } else if (source.IsFpuRegisterPair()) {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01006662 if (destination.IsRegisterPair()) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006663 __ Vmov(LowRegisterFrom(destination), HighRegisterFrom(destination), DRegisterFrom(source));
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01006664 } else if (destination.IsFpuRegisterPair()) {
6665 __ Vmov(DRegisterFrom(destination), DRegisterFrom(source));
6666 } else {
6667 DCHECK(destination.IsDoubleStackSlot()) << destination;
6668 GetAssembler()->StoreDToOffset(DRegisterFrom(source), sp, destination.GetStackIndex());
6669 }
Scott Wakelingfe885462016-09-22 10:24:38 +01006670 } else {
6671 DCHECK(source.IsConstant()) << source;
6672 HConstant* constant = source.GetConstant();
6673 if (constant->IsIntConstant() || constant->IsNullConstant()) {
6674 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
6675 if (destination.IsRegister()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006676 __ Mov(RegisterFrom(destination), value);
Scott Wakelingfe885462016-09-22 10:24:38 +01006677 } else {
6678 DCHECK(destination.IsStackSlot());
Scott Wakelingfe885462016-09-22 10:24:38 +01006679 vixl32::Register temp = temps.Acquire();
6680 __ Mov(temp, value);
6681 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
6682 }
6683 } else if (constant->IsLongConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006684 int64_t value = Int64ConstantFrom(source);
Scott Wakelingfe885462016-09-22 10:24:38 +01006685 if (destination.IsRegisterPair()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006686 __ Mov(LowRegisterFrom(destination), Low32Bits(value));
6687 __ Mov(HighRegisterFrom(destination), High32Bits(value));
Scott Wakelingfe885462016-09-22 10:24:38 +01006688 } else {
6689 DCHECK(destination.IsDoubleStackSlot()) << destination;
Scott Wakelingfe885462016-09-22 10:24:38 +01006690 vixl32::Register temp = temps.Acquire();
6691 __ Mov(temp, Low32Bits(value));
6692 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
6693 __ Mov(temp, High32Bits(value));
6694 GetAssembler()->StoreToOffset(kStoreWord,
6695 temp,
6696 sp,
6697 destination.GetHighStackIndex(kArmWordSize));
6698 }
6699 } else if (constant->IsDoubleConstant()) {
6700 double value = constant->AsDoubleConstant()->GetValue();
6701 if (destination.IsFpuRegisterPair()) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006702 __ Vmov(DRegisterFrom(destination), value);
Scott Wakelingfe885462016-09-22 10:24:38 +01006703 } else {
6704 DCHECK(destination.IsDoubleStackSlot()) << destination;
6705 uint64_t int_value = bit_cast<uint64_t, double>(value);
Scott Wakelingfe885462016-09-22 10:24:38 +01006706 vixl32::Register temp = temps.Acquire();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006707 __ Mov(temp, Low32Bits(int_value));
Scott Wakelingfe885462016-09-22 10:24:38 +01006708 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006709 __ Mov(temp, High32Bits(int_value));
Scott Wakelingfe885462016-09-22 10:24:38 +01006710 GetAssembler()->StoreToOffset(kStoreWord,
6711 temp,
6712 sp,
6713 destination.GetHighStackIndex(kArmWordSize));
6714 }
6715 } else {
6716 DCHECK(constant->IsFloatConstant()) << constant->DebugName();
6717 float value = constant->AsFloatConstant()->GetValue();
6718 if (destination.IsFpuRegister()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006719 __ Vmov(SRegisterFrom(destination), value);
Scott Wakelingfe885462016-09-22 10:24:38 +01006720 } else {
6721 DCHECK(destination.IsStackSlot());
Scott Wakelingfe885462016-09-22 10:24:38 +01006722 vixl32::Register temp = temps.Acquire();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006723 __ Mov(temp, bit_cast<int32_t, float>(value));
Scott Wakelingfe885462016-09-22 10:24:38 +01006724 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
6725 }
6726 }
6727 }
6728}
6729
Alexandre Rames9c19bd62016-10-24 11:50:32 +01006730void ParallelMoveResolverARMVIXL::Exchange(vixl32::Register reg, int mem) {
6731 UseScratchRegisterScope temps(GetAssembler()->GetVIXLAssembler());
6732 vixl32::Register temp = temps.Acquire();
6733 __ Mov(temp, reg);
6734 GetAssembler()->LoadFromOffset(kLoadWord, reg, sp, mem);
6735 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, mem);
Scott Wakelingfe885462016-09-22 10:24:38 +01006736}
6737
Alexandre Rames9c19bd62016-10-24 11:50:32 +01006738void ParallelMoveResolverARMVIXL::Exchange(int mem1, int mem2) {
6739 // TODO(VIXL32): Double check the performance of this implementation.
6740 UseScratchRegisterScope temps(GetAssembler()->GetVIXLAssembler());
Nicolas Geoffray13a797b2017-03-15 16:41:31 +00006741 vixl32::Register temp1 = temps.Acquire();
6742 ScratchRegisterScope ensure_scratch(
6743 this, temp1.GetCode(), r0.GetCode(), codegen_->GetNumberOfCoreRegisters());
6744 vixl32::Register temp2(ensure_scratch.GetRegister());
Alexandre Rames9c19bd62016-10-24 11:50:32 +01006745
Nicolas Geoffray13a797b2017-03-15 16:41:31 +00006746 int stack_offset = ensure_scratch.IsSpilled() ? kArmWordSize : 0;
6747 GetAssembler()->LoadFromOffset(kLoadWord, temp1, sp, mem1 + stack_offset);
6748 GetAssembler()->LoadFromOffset(kLoadWord, temp2, sp, mem2 + stack_offset);
6749 GetAssembler()->StoreToOffset(kStoreWord, temp1, sp, mem2 + stack_offset);
6750 GetAssembler()->StoreToOffset(kStoreWord, temp2, sp, mem1 + stack_offset);
Scott Wakelingfe885462016-09-22 10:24:38 +01006751}
6752
Alexandre Rames9c19bd62016-10-24 11:50:32 +01006753void ParallelMoveResolverARMVIXL::EmitSwap(size_t index) {
6754 MoveOperands* move = moves_[index];
6755 Location source = move->GetSource();
6756 Location destination = move->GetDestination();
6757 UseScratchRegisterScope temps(GetAssembler()->GetVIXLAssembler());
6758
6759 if (source.IsRegister() && destination.IsRegister()) {
6760 vixl32::Register temp = temps.Acquire();
6761 DCHECK(!RegisterFrom(source).Is(temp));
6762 DCHECK(!RegisterFrom(destination).Is(temp));
6763 __ Mov(temp, RegisterFrom(destination));
6764 __ Mov(RegisterFrom(destination), RegisterFrom(source));
6765 __ Mov(RegisterFrom(source), temp);
6766 } else if (source.IsRegister() && destination.IsStackSlot()) {
6767 Exchange(RegisterFrom(source), destination.GetStackIndex());
6768 } else if (source.IsStackSlot() && destination.IsRegister()) {
6769 Exchange(RegisterFrom(destination), source.GetStackIndex());
6770 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Anton Kirilovdda43962016-11-21 19:55:20 +00006771 Exchange(source.GetStackIndex(), destination.GetStackIndex());
Alexandre Rames9c19bd62016-10-24 11:50:32 +01006772 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Nicolas Geoffray13a797b2017-03-15 16:41:31 +00006773 vixl32::Register temp = temps.Acquire();
Anton Kirilovdda43962016-11-21 19:55:20 +00006774 __ Vmov(temp, SRegisterFrom(source));
6775 __ Vmov(SRegisterFrom(source), SRegisterFrom(destination));
6776 __ Vmov(SRegisterFrom(destination), temp);
Alexandre Rames9c19bd62016-10-24 11:50:32 +01006777 } else if (source.IsRegisterPair() && destination.IsRegisterPair()) {
6778 vixl32::DRegister temp = temps.AcquireD();
6779 __ Vmov(temp, LowRegisterFrom(source), HighRegisterFrom(source));
6780 __ Mov(LowRegisterFrom(source), LowRegisterFrom(destination));
6781 __ Mov(HighRegisterFrom(source), HighRegisterFrom(destination));
6782 __ Vmov(LowRegisterFrom(destination), HighRegisterFrom(destination), temp);
6783 } else if (source.IsRegisterPair() || destination.IsRegisterPair()) {
6784 vixl32::Register low_reg = LowRegisterFrom(source.IsRegisterPair() ? source : destination);
6785 int mem = source.IsRegisterPair() ? destination.GetStackIndex() : source.GetStackIndex();
6786 DCHECK(ExpectedPairLayout(source.IsRegisterPair() ? source : destination));
6787 vixl32::DRegister temp = temps.AcquireD();
6788 __ Vmov(temp, low_reg, vixl32::Register(low_reg.GetCode() + 1));
6789 GetAssembler()->LoadFromOffset(kLoadWordPair, low_reg, sp, mem);
6790 GetAssembler()->StoreDToOffset(temp, sp, mem);
6791 } else if (source.IsFpuRegisterPair() && destination.IsFpuRegisterPair()) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006792 vixl32::DRegister first = DRegisterFrom(source);
6793 vixl32::DRegister second = DRegisterFrom(destination);
6794 vixl32::DRegister temp = temps.AcquireD();
6795 __ Vmov(temp, first);
6796 __ Vmov(first, second);
6797 __ Vmov(second, temp);
Alexandre Rames9c19bd62016-10-24 11:50:32 +01006798 } else if (source.IsFpuRegisterPair() || destination.IsFpuRegisterPair()) {
Anton Kirilovdda43962016-11-21 19:55:20 +00006799 vixl32::DRegister reg = source.IsFpuRegisterPair()
6800 ? DRegisterFrom(source)
6801 : DRegisterFrom(destination);
6802 int mem = source.IsFpuRegisterPair()
6803 ? destination.GetStackIndex()
6804 : source.GetStackIndex();
6805 vixl32::DRegister temp = temps.AcquireD();
6806 __ Vmov(temp, reg);
6807 GetAssembler()->LoadDFromOffset(reg, sp, mem);
6808 GetAssembler()->StoreDToOffset(temp, sp, mem);
Alexandre Rames9c19bd62016-10-24 11:50:32 +01006809 } else if (source.IsFpuRegister() || destination.IsFpuRegister()) {
Anton Kirilovdda43962016-11-21 19:55:20 +00006810 vixl32::SRegister reg = source.IsFpuRegister()
6811 ? SRegisterFrom(source)
6812 : SRegisterFrom(destination);
6813 int mem = source.IsFpuRegister()
6814 ? destination.GetStackIndex()
6815 : source.GetStackIndex();
6816 vixl32::Register temp = temps.Acquire();
6817 __ Vmov(temp, reg);
6818 GetAssembler()->LoadSFromOffset(reg, sp, mem);
6819 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, mem);
Alexandre Rames9c19bd62016-10-24 11:50:32 +01006820 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
6821 vixl32::DRegister temp1 = temps.AcquireD();
6822 vixl32::DRegister temp2 = temps.AcquireD();
6823 __ Vldr(temp1, MemOperand(sp, source.GetStackIndex()));
6824 __ Vldr(temp2, MemOperand(sp, destination.GetStackIndex()));
6825 __ Vstr(temp1, MemOperand(sp, destination.GetStackIndex()));
6826 __ Vstr(temp2, MemOperand(sp, source.GetStackIndex()));
6827 } else {
6828 LOG(FATAL) << "Unimplemented" << source << " <-> " << destination;
6829 }
Scott Wakelingfe885462016-09-22 10:24:38 +01006830}
6831
Nicolas Geoffray13a797b2017-03-15 16:41:31 +00006832void ParallelMoveResolverARMVIXL::SpillScratch(int reg) {
6833 __ Push(vixl32::Register(reg));
Scott Wakelingfe885462016-09-22 10:24:38 +01006834}
6835
Nicolas Geoffray13a797b2017-03-15 16:41:31 +00006836void ParallelMoveResolverARMVIXL::RestoreScratch(int reg) {
6837 __ Pop(vixl32::Register(reg));
Scott Wakelingfe885462016-09-22 10:24:38 +01006838}
6839
Artem Serov02d37832016-10-25 15:25:33 +01006840HLoadClass::LoadKind CodeGeneratorARMVIXL::GetSupportedLoadClassKind(
Artem Serovd4cc5b22016-11-04 11:19:09 +00006841 HLoadClass::LoadKind desired_class_load_kind) {
6842 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006843 case HLoadClass::LoadKind::kInvalid:
6844 LOG(FATAL) << "UNREACHABLE";
6845 UNREACHABLE();
Artem Serovd4cc5b22016-11-04 11:19:09 +00006846 case HLoadClass::LoadKind::kReferrersClass:
6847 break;
Artem Serovd4cc5b22016-11-04 11:19:09 +00006848 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006849 case HLoadClass::LoadKind::kBssEntry:
6850 DCHECK(!Runtime::Current()->UseJitCompilation());
6851 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006852 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006853 DCHECK(Runtime::Current()->UseJitCompilation());
Artem Serovc5fcb442016-12-02 19:19:58 +00006854 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01006855 case HLoadClass::LoadKind::kBootImageAddress:
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 }
Artem Serovd4cc5b22016-11-04 11:19:09 +00006944 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006945 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Artem Serovd4cc5b22016-11-04 11:19:09 +00006946 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
6947 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
6948 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
6949 codegen_->EmitMovwMovtPlaceholder(labels, out);
6950 break;
6951 }
6952 case HLoadClass::LoadKind::kBootImageAddress: {
Artem Serovc5fcb442016-12-02 19:19:58 +00006953 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006954 uint32_t address = dchecked_integral_cast<uint32_t>(
6955 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
6956 DCHECK_NE(address, 0u);
Artem Serovc5fcb442016-12-02 19:19:58 +00006957 __ Ldr(out, codegen_->DeduplicateBootImageAddressLiteral(address));
Artem Serovd4cc5b22016-11-04 11:19:09 +00006958 break;
6959 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006960 case HLoadClass::LoadKind::kBssEntry: {
Vladimir Markoea4c1262017-02-06 19:59:33 +00006961 vixl32::Register temp = (!kUseReadBarrier || kUseBakerReadBarrier)
6962 ? RegisterFrom(locations->GetTemp(0))
6963 : out;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006964 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
Vladimir Marko1998cd02017-01-13 13:02:58 +00006965 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex());
Vladimir Markoea4c1262017-02-06 19:59:33 +00006966 codegen_->EmitMovwMovtPlaceholder(labels, temp);
6967 GenerateGcRootFieldLoad(cls, out_loc, temp, /* offset */ 0, read_barrier_option);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006968 generate_null_check = true;
6969 break;
6970 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006971 case HLoadClass::LoadKind::kJitTableAddress: {
Artem Serovc5fcb442016-12-02 19:19:58 +00006972 __ Ldr(out, codegen_->DeduplicateJitClassLiteral(cls->GetDexFile(),
6973 cls->GetTypeIndex(),
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006974 cls->GetClass()));
Artem Serovc5fcb442016-12-02 19:19:58 +00006975 // /* GcRoot<mirror::Class> */ out = *out
Vladimir Markoea4c1262017-02-06 19:59:33 +00006976 GenerateGcRootFieldLoad(cls, out_loc, out, /* offset */ 0, read_barrier_option);
Artem Serovd4cc5b22016-11-04 11:19:09 +00006977 break;
6978 }
Vladimir Marko41559982017-01-06 14:04:23 +00006979 case HLoadClass::LoadKind::kDexCacheViaMethod:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006980 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00006981 LOG(FATAL) << "UNREACHABLE";
6982 UNREACHABLE();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006983 }
6984
6985 if (generate_null_check || cls->MustGenerateClinitCheck()) {
6986 DCHECK(cls->CanCallRuntime());
6987 LoadClassSlowPathARMVIXL* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARMVIXL(
6988 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
6989 codegen_->AddSlowPath(slow_path);
6990 if (generate_null_check) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00006991 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006992 }
6993 if (cls->MustGenerateClinitCheck()) {
6994 GenerateClassInitializationCheck(slow_path, out);
6995 } else {
6996 __ Bind(slow_path->GetExitLabel());
6997 }
6998 }
6999}
7000
Artem Serov02d37832016-10-25 15:25:33 +01007001void LocationsBuilderARMVIXL::VisitClinitCheck(HClinitCheck* check) {
7002 LocationSummary* locations =
7003 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
7004 locations->SetInAt(0, Location::RequiresRegister());
7005 if (check->HasUses()) {
7006 locations->SetOut(Location::SameAsFirstInput());
7007 }
7008}
7009
7010void InstructionCodeGeneratorARMVIXL::VisitClinitCheck(HClinitCheck* check) {
7011 // We assume the class is not null.
7012 LoadClassSlowPathARMVIXL* slow_path =
7013 new (GetGraph()->GetArena()) LoadClassSlowPathARMVIXL(check->GetLoadClass(),
7014 check,
7015 check->GetDexPc(),
7016 /* do_clinit */ true);
7017 codegen_->AddSlowPath(slow_path);
7018 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
7019}
7020
7021void InstructionCodeGeneratorARMVIXL::GenerateClassInitializationCheck(
7022 LoadClassSlowPathARMVIXL* slow_path, vixl32::Register class_reg) {
7023 UseScratchRegisterScope temps(GetVIXLAssembler());
7024 vixl32::Register temp = temps.Acquire();
7025 GetAssembler()->LoadFromOffset(kLoadWord,
7026 temp,
7027 class_reg,
7028 mirror::Class::StatusOffset().Int32Value());
7029 __ Cmp(temp, mirror::Class::kStatusInitialized);
7030 __ B(lt, slow_path->GetEntryLabel());
7031 // Even if the initialized flag is set, we may be in a situation where caches are not synced
7032 // properly. Therefore, we do a memory fence.
7033 __ Dmb(ISH);
7034 __ Bind(slow_path->GetExitLabel());
7035}
7036
Artem Serov02d37832016-10-25 15:25:33 +01007037HLoadString::LoadKind CodeGeneratorARMVIXL::GetSupportedLoadStringKind(
Artem Serovd4cc5b22016-11-04 11:19:09 +00007038 HLoadString::LoadKind desired_string_load_kind) {
7039 switch (desired_string_load_kind) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00007040 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Artem Serovd4cc5b22016-11-04 11:19:09 +00007041 case HLoadString::LoadKind::kBssEntry:
7042 DCHECK(!Runtime::Current()->UseJitCompilation());
7043 break;
7044 case HLoadString::LoadKind::kJitTableAddress:
7045 DCHECK(Runtime::Current()->UseJitCompilation());
Artem Serovc5fcb442016-12-02 19:19:58 +00007046 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01007047 case HLoadString::LoadKind::kBootImageAddress:
Artem Serovd4cc5b22016-11-04 11:19:09 +00007048 case HLoadString::LoadKind::kDexCacheViaMethod:
7049 break;
7050 }
7051 return desired_string_load_kind;
Artem Serov02d37832016-10-25 15:25:33 +01007052}
7053
7054void LocationsBuilderARMVIXL::VisitLoadString(HLoadString* load) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00007055 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Artem Serov02d37832016-10-25 15:25:33 +01007056 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Artem Serov02d37832016-10-25 15:25:33 +01007057 HLoadString::LoadKind load_kind = load->GetLoadKind();
7058 if (load_kind == HLoadString::LoadKind::kDexCacheViaMethod) {
Artem Serov02d37832016-10-25 15:25:33 +01007059 locations->SetOut(LocationFrom(r0));
7060 } else {
7061 locations->SetOut(Location::RequiresRegister());
Artem Serovd4cc5b22016-11-04 11:19:09 +00007062 if (load_kind == HLoadString::LoadKind::kBssEntry) {
7063 if (!kUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00007064 // Rely on the pResolveString and marking to save everything we need, including temps.
7065 // Note that IP may be clobbered by saving/restoring the live register (only one thanks
7066 // to the custom calling convention) or by marking, so we request a different temp.
Artem Serovd4cc5b22016-11-04 11:19:09 +00007067 locations->AddTemp(Location::RequiresRegister());
7068 RegisterSet caller_saves = RegisterSet::Empty();
7069 InvokeRuntimeCallingConventionARMVIXL calling_convention;
7070 caller_saves.Add(LocationFrom(calling_convention.GetRegisterAt(0)));
7071 // TODO: Add GetReturnLocation() to the calling convention so that we can DCHECK()
7072 // that the the kPrimNot result register is the same as the first argument register.
7073 locations->SetCustomSlowPathCallerSaves(caller_saves);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01007074 if (kUseBakerReadBarrier && kBakerReadBarrierLinkTimeThunksEnableForGcRoots) {
7075 locations->AddTemp(Location::RegisterLocation(kBakerCcEntrypointRegister.GetCode()));
7076 }
Artem Serovd4cc5b22016-11-04 11:19:09 +00007077 } else {
7078 // For non-Baker read barrier we have a temp-clobbering call.
7079 }
7080 }
Artem Serov02d37832016-10-25 15:25:33 +01007081 }
7082}
7083
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007084// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
7085// move.
7086void InstructionCodeGeneratorARMVIXL::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Artem Serovd4cc5b22016-11-04 11:19:09 +00007087 LocationSummary* locations = load->GetLocations();
7088 Location out_loc = locations->Out();
7089 vixl32::Register out = OutputRegister(load);
7090 HLoadString::LoadKind load_kind = load->GetLoadKind();
7091
7092 switch (load_kind) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00007093 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
7094 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
7095 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007096 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Artem Serovd4cc5b22016-11-04 11:19:09 +00007097 codegen_->EmitMovwMovtPlaceholder(labels, out);
7098 return; // No dex cache slow path.
7099 }
7100 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007101 uint32_t address = dchecked_integral_cast<uint32_t>(
7102 reinterpret_cast<uintptr_t>(load->GetString().Get()));
7103 DCHECK_NE(address, 0u);
Artem Serovc5fcb442016-12-02 19:19:58 +00007104 __ Ldr(out, codegen_->DeduplicateBootImageAddressLiteral(address));
7105 return; // No dex cache slow path.
Artem Serovd4cc5b22016-11-04 11:19:09 +00007106 }
7107 case HLoadString::LoadKind::kBssEntry: {
7108 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markoea4c1262017-02-06 19:59:33 +00007109 vixl32::Register temp = (!kUseReadBarrier || kUseBakerReadBarrier)
7110 ? RegisterFrom(locations->GetTemp(0))
7111 : out;
Artem Serovd4cc5b22016-11-04 11:19:09 +00007112 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007113 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Artem Serovd4cc5b22016-11-04 11:19:09 +00007114 codegen_->EmitMovwMovtPlaceholder(labels, temp);
7115 GenerateGcRootFieldLoad(load, out_loc, temp, /* offset */ 0, kCompilerReadBarrierOption);
7116 LoadStringSlowPathARMVIXL* slow_path =
7117 new (GetGraph()->GetArena()) LoadStringSlowPathARMVIXL(load);
7118 codegen_->AddSlowPath(slow_path);
7119 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
7120 __ Bind(slow_path->GetExitLabel());
7121 return;
7122 }
7123 case HLoadString::LoadKind::kJitTableAddress: {
Artem Serovc5fcb442016-12-02 19:19:58 +00007124 __ Ldr(out, codegen_->DeduplicateJitStringLiteral(load->GetDexFile(),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007125 load->GetStringIndex(),
7126 load->GetString()));
Artem Serovc5fcb442016-12-02 19:19:58 +00007127 // /* GcRoot<mirror::String> */ out = *out
7128 GenerateGcRootFieldLoad(load, out_loc, out, /* offset */ 0, kCompilerReadBarrierOption);
7129 return;
Artem Serovd4cc5b22016-11-04 11:19:09 +00007130 }
7131 default:
7132 break;
7133 }
Artem Serov02d37832016-10-25 15:25:33 +01007134
7135 // TODO: Re-add the compiler code to do string dex cache lookup again.
7136 DCHECK_EQ(load->GetLoadKind(), HLoadString::LoadKind::kDexCacheViaMethod);
7137 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Andreas Gampe8a0128a2016-11-28 07:38:35 -08007138 __ Mov(calling_convention.GetRegisterAt(0), load->GetStringIndex().index_);
Artem Serov02d37832016-10-25 15:25:33 +01007139 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
7140 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
7141}
7142
7143static int32_t GetExceptionTlsOffset() {
7144 return Thread::ExceptionOffset<kArmPointerSize>().Int32Value();
7145}
7146
7147void LocationsBuilderARMVIXL::VisitLoadException(HLoadException* load) {
7148 LocationSummary* locations =
7149 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
7150 locations->SetOut(Location::RequiresRegister());
7151}
7152
7153void InstructionCodeGeneratorARMVIXL::VisitLoadException(HLoadException* load) {
7154 vixl32::Register out = OutputRegister(load);
7155 GetAssembler()->LoadFromOffset(kLoadWord, out, tr, GetExceptionTlsOffset());
7156}
7157
7158
7159void LocationsBuilderARMVIXL::VisitClearException(HClearException* clear) {
7160 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
7161}
7162
7163void InstructionCodeGeneratorARMVIXL::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
7164 UseScratchRegisterScope temps(GetVIXLAssembler());
7165 vixl32::Register temp = temps.Acquire();
7166 __ Mov(temp, 0);
7167 GetAssembler()->StoreToOffset(kStoreWord, temp, tr, GetExceptionTlsOffset());
7168}
7169
7170void LocationsBuilderARMVIXL::VisitThrow(HThrow* instruction) {
7171 LocationSummary* locations =
7172 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
7173 InvokeRuntimeCallingConventionARMVIXL calling_convention;
7174 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
7175}
7176
7177void InstructionCodeGeneratorARMVIXL::VisitThrow(HThrow* instruction) {
7178 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
7179 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
7180}
7181
Artem Serov657022c2016-11-23 14:19:38 +00007182// Temp is used for read barrier.
7183static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
7184 if (kEmitCompilerReadBarrier &&
7185 (kUseBakerReadBarrier ||
7186 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
7187 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
7188 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
7189 return 1;
7190 }
7191 return 0;
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007192}
7193
Artem Serov657022c2016-11-23 14:19:38 +00007194// Interface case has 3 temps, one for holding the number of interfaces, one for the current
7195// interface pointer, one for loading the current interface.
7196// The other checks have one temp for loading the object's class.
7197static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
7198 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
7199 return 3;
7200 }
7201 return 1 + NumberOfInstanceOfTemps(type_check_kind);
7202}
Artem Serovcfbe9132016-10-14 15:58:56 +01007203
7204void LocationsBuilderARMVIXL::VisitInstanceOf(HInstanceOf* instruction) {
7205 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
7206 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
7207 bool baker_read_barrier_slow_path = false;
7208 switch (type_check_kind) {
7209 case TypeCheckKind::kExactCheck:
7210 case TypeCheckKind::kAbstractClassCheck:
7211 case TypeCheckKind::kClassHierarchyCheck:
7212 case TypeCheckKind::kArrayObjectCheck:
7213 call_kind =
7214 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
7215 baker_read_barrier_slow_path = kUseBakerReadBarrier;
7216 break;
7217 case TypeCheckKind::kArrayCheck:
7218 case TypeCheckKind::kUnresolvedCheck:
7219 case TypeCheckKind::kInterfaceCheck:
7220 call_kind = LocationSummary::kCallOnSlowPath;
7221 break;
7222 }
7223
7224 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
7225 if (baker_read_barrier_slow_path) {
7226 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
7227 }
7228 locations->SetInAt(0, Location::RequiresRegister());
7229 locations->SetInAt(1, Location::RequiresRegister());
7230 // The "out" register is used as a temporary, so it overlaps with the inputs.
7231 // Note that TypeCheckSlowPathARM uses this register too.
7232 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Artem Serov657022c2016-11-23 14:19:38 +00007233 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01007234 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
7235 codegen_->MaybeAddBakerCcEntrypointTempForFields(locations);
7236 }
Artem Serovcfbe9132016-10-14 15:58:56 +01007237}
7238
7239void InstructionCodeGeneratorARMVIXL::VisitInstanceOf(HInstanceOf* instruction) {
7240 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
7241 LocationSummary* locations = instruction->GetLocations();
7242 Location obj_loc = locations->InAt(0);
7243 vixl32::Register obj = InputRegisterAt(instruction, 0);
7244 vixl32::Register cls = InputRegisterAt(instruction, 1);
7245 Location out_loc = locations->Out();
7246 vixl32::Register out = OutputRegister(instruction);
Artem Serov657022c2016-11-23 14:19:38 +00007247 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
7248 DCHECK_LE(num_temps, 1u);
7249 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Artem Serovcfbe9132016-10-14 15:58:56 +01007250 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
7251 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
7252 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
7253 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007254 vixl32::Label done;
7255 vixl32::Label* const final_label = codegen_->GetFinalLabel(instruction, &done);
Artem Serovcfbe9132016-10-14 15:58:56 +01007256 SlowPathCodeARMVIXL* slow_path = nullptr;
7257
7258 // Return 0 if `obj` is null.
7259 // avoid null check if we know obj is not null.
7260 if (instruction->MustDoNullCheck()) {
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007261 DCHECK(!out.Is(obj));
7262 __ Mov(out, 0);
7263 __ CompareAndBranchIfZero(obj, final_label, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007264 }
7265
Artem Serovcfbe9132016-10-14 15:58:56 +01007266 switch (type_check_kind) {
7267 case TypeCheckKind::kExactCheck: {
Mathieu Chartier6beced42016-11-15 15:51:31 -08007268 // /* HeapReference<Class> */ out = obj->klass_
7269 GenerateReferenceLoadTwoRegisters(instruction,
7270 out_loc,
7271 obj_loc,
7272 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00007273 maybe_temp_loc,
7274 kCompilerReadBarrierOption);
Artem Serovcfbe9132016-10-14 15:58:56 +01007275 // Classes must be equal for the instanceof to succeed.
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007276 __ Cmp(out, cls);
7277 // We speculatively set the result to false without changing the condition
7278 // flags, which allows us to avoid some branching later.
7279 __ Mov(LeaveFlags, out, 0);
7280
7281 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
7282 // we check that the output is in a low register, so that a 16-bit MOV
7283 // encoding can be used.
7284 if (out.IsLow()) {
7285 // We use the scope because of the IT block that follows.
7286 ExactAssemblyScope guard(GetVIXLAssembler(),
7287 2 * vixl32::k16BitT32InstructionSizeInBytes,
7288 CodeBufferCheckScope::kExactSize);
7289
7290 __ it(eq);
7291 __ mov(eq, out, 1);
7292 } else {
7293 __ B(ne, final_label, /* far_target */ false);
7294 __ Mov(out, 1);
7295 }
7296
Artem Serovcfbe9132016-10-14 15:58:56 +01007297 break;
7298 }
7299
7300 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier6beced42016-11-15 15:51:31 -08007301 // /* HeapReference<Class> */ out = obj->klass_
7302 GenerateReferenceLoadTwoRegisters(instruction,
7303 out_loc,
7304 obj_loc,
7305 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00007306 maybe_temp_loc,
7307 kCompilerReadBarrierOption);
Artem Serovcfbe9132016-10-14 15:58:56 +01007308 // If the class is abstract, we eagerly fetch the super class of the
7309 // object to avoid doing a comparison we know will fail.
7310 vixl32::Label loop;
7311 __ Bind(&loop);
7312 // /* HeapReference<Class> */ out = out->super_class_
Artem Serov657022c2016-11-23 14:19:38 +00007313 GenerateReferenceLoadOneRegister(instruction,
7314 out_loc,
7315 super_offset,
7316 maybe_temp_loc,
7317 kCompilerReadBarrierOption);
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007318 // If `out` is null, we use it for the result, and jump to the final label.
Anton Kirilov6f644202017-02-27 18:29:45 +00007319 __ CompareAndBranchIfZero(out, final_label, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007320 __ Cmp(out, cls);
Artem Serov517d9f62016-12-12 15:51:15 +00007321 __ B(ne, &loop, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007322 __ Mov(out, 1);
Artem Serovcfbe9132016-10-14 15:58:56 +01007323 break;
7324 }
7325
7326 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier6beced42016-11-15 15:51:31 -08007327 // /* HeapReference<Class> */ out = obj->klass_
7328 GenerateReferenceLoadTwoRegisters(instruction,
7329 out_loc,
7330 obj_loc,
7331 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00007332 maybe_temp_loc,
7333 kCompilerReadBarrierOption);
Artem Serovcfbe9132016-10-14 15:58:56 +01007334 // Walk over the class hierarchy to find a match.
7335 vixl32::Label loop, success;
7336 __ Bind(&loop);
7337 __ Cmp(out, cls);
Artem Serov517d9f62016-12-12 15:51:15 +00007338 __ B(eq, &success, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007339 // /* HeapReference<Class> */ out = out->super_class_
Artem Serov657022c2016-11-23 14:19:38 +00007340 GenerateReferenceLoadOneRegister(instruction,
7341 out_loc,
7342 super_offset,
7343 maybe_temp_loc,
7344 kCompilerReadBarrierOption);
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007345 // This is essentially a null check, but it sets the condition flags to the
7346 // proper value for the code that follows the loop, i.e. not `eq`.
7347 __ Cmp(out, 1);
7348 __ B(hs, &loop, /* far_target */ false);
7349
7350 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
7351 // we check that the output is in a low register, so that a 16-bit MOV
7352 // encoding can be used.
7353 if (out.IsLow()) {
7354 // If `out` is null, we use it for the result, and the condition flags
7355 // have already been set to `ne`, so the IT block that comes afterwards
7356 // (and which handles the successful case) turns into a NOP (instead of
7357 // overwriting `out`).
7358 __ Bind(&success);
7359
7360 // We use the scope because of the IT block that follows.
7361 ExactAssemblyScope guard(GetVIXLAssembler(),
7362 2 * vixl32::k16BitT32InstructionSizeInBytes,
7363 CodeBufferCheckScope::kExactSize);
7364
7365 // There is only one branch to the `success` label (which is bound to this
7366 // IT block), and it has the same condition, `eq`, so in that case the MOV
7367 // is executed.
7368 __ it(eq);
7369 __ mov(eq, out, 1);
7370 } else {
7371 // If `out` is null, we use it for the result, and jump to the final label.
Anton Kirilov6f644202017-02-27 18:29:45 +00007372 __ B(final_label);
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007373 __ Bind(&success);
7374 __ Mov(out, 1);
Artem Serovcfbe9132016-10-14 15:58:56 +01007375 }
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007376
Artem Serovcfbe9132016-10-14 15:58:56 +01007377 break;
7378 }
7379
7380 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier6beced42016-11-15 15:51:31 -08007381 // /* HeapReference<Class> */ out = obj->klass_
7382 GenerateReferenceLoadTwoRegisters(instruction,
7383 out_loc,
7384 obj_loc,
7385 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00007386 maybe_temp_loc,
7387 kCompilerReadBarrierOption);
Artem Serovcfbe9132016-10-14 15:58:56 +01007388 // Do an exact check.
7389 vixl32::Label exact_check;
7390 __ Cmp(out, cls);
Artem Serov517d9f62016-12-12 15:51:15 +00007391 __ B(eq, &exact_check, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007392 // Otherwise, we need to check that the object's class is a non-primitive array.
7393 // /* HeapReference<Class> */ out = out->component_type_
Artem Serov657022c2016-11-23 14:19:38 +00007394 GenerateReferenceLoadOneRegister(instruction,
7395 out_loc,
7396 component_offset,
7397 maybe_temp_loc,
7398 kCompilerReadBarrierOption);
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007399 // If `out` is null, we use it for the result, and jump to the final label.
Anton Kirilov6f644202017-02-27 18:29:45 +00007400 __ CompareAndBranchIfZero(out, final_label, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007401 GetAssembler()->LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
7402 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007403 __ Cmp(out, 0);
7404 // We speculatively set the result to false without changing the condition
7405 // flags, which allows us to avoid some branching later.
7406 __ Mov(LeaveFlags, out, 0);
7407
7408 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
7409 // we check that the output is in a low register, so that a 16-bit MOV
7410 // encoding can be used.
7411 if (out.IsLow()) {
7412 __ Bind(&exact_check);
7413
7414 // We use the scope because of the IT block that follows.
7415 ExactAssemblyScope guard(GetVIXLAssembler(),
7416 2 * vixl32::k16BitT32InstructionSizeInBytes,
7417 CodeBufferCheckScope::kExactSize);
7418
7419 __ it(eq);
7420 __ mov(eq, out, 1);
7421 } else {
7422 __ B(ne, final_label, /* far_target */ false);
7423 __ Bind(&exact_check);
7424 __ Mov(out, 1);
7425 }
7426
Artem Serovcfbe9132016-10-14 15:58:56 +01007427 break;
7428 }
7429
7430 case TypeCheckKind::kArrayCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00007431 // No read barrier since the slow path will retry upon failure.
Mathieu Chartier6beced42016-11-15 15:51:31 -08007432 // /* HeapReference<Class> */ out = obj->klass_
7433 GenerateReferenceLoadTwoRegisters(instruction,
7434 out_loc,
7435 obj_loc,
7436 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00007437 maybe_temp_loc,
7438 kWithoutReadBarrier);
Artem Serovcfbe9132016-10-14 15:58:56 +01007439 __ Cmp(out, cls);
7440 DCHECK(locations->OnlyCallsOnSlowPath());
7441 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARMVIXL(instruction,
7442 /* is_fatal */ false);
7443 codegen_->AddSlowPath(slow_path);
7444 __ B(ne, slow_path->GetEntryLabel());
7445 __ Mov(out, 1);
Artem Serovcfbe9132016-10-14 15:58:56 +01007446 break;
7447 }
7448
7449 case TypeCheckKind::kUnresolvedCheck:
7450 case TypeCheckKind::kInterfaceCheck: {
7451 // Note that we indeed only call on slow path, but we always go
7452 // into the slow path for the unresolved and interface check
7453 // cases.
7454 //
7455 // We cannot directly call the InstanceofNonTrivial runtime
7456 // entry point without resorting to a type checking slow path
7457 // here (i.e. by calling InvokeRuntime directly), as it would
7458 // require to assign fixed registers for the inputs of this
7459 // HInstanceOf instruction (following the runtime calling
7460 // convention), which might be cluttered by the potential first
7461 // read barrier emission at the beginning of this method.
7462 //
7463 // TODO: Introduce a new runtime entry point taking the object
7464 // to test (instead of its class) as argument, and let it deal
7465 // with the read barrier issues. This will let us refactor this
7466 // case of the `switch` code as it was previously (with a direct
7467 // call to the runtime not using a type checking slow path).
7468 // This should also be beneficial for the other cases above.
7469 DCHECK(locations->OnlyCallsOnSlowPath());
7470 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARMVIXL(instruction,
7471 /* is_fatal */ false);
7472 codegen_->AddSlowPath(slow_path);
7473 __ B(slow_path->GetEntryLabel());
Artem Serovcfbe9132016-10-14 15:58:56 +01007474 break;
7475 }
7476 }
7477
Artem Serovcfbe9132016-10-14 15:58:56 +01007478 if (done.IsReferenced()) {
7479 __ Bind(&done);
7480 }
7481
7482 if (slow_path != nullptr) {
7483 __ Bind(slow_path->GetExitLabel());
7484 }
7485}
7486
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007487void LocationsBuilderARMVIXL::VisitCheckCast(HCheckCast* instruction) {
7488 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
7489 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
7490
7491 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
7492 switch (type_check_kind) {
7493 case TypeCheckKind::kExactCheck:
7494 case TypeCheckKind::kAbstractClassCheck:
7495 case TypeCheckKind::kClassHierarchyCheck:
7496 case TypeCheckKind::kArrayObjectCheck:
7497 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
7498 LocationSummary::kCallOnSlowPath :
7499 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
7500 break;
7501 case TypeCheckKind::kArrayCheck:
7502 case TypeCheckKind::kUnresolvedCheck:
7503 case TypeCheckKind::kInterfaceCheck:
7504 call_kind = LocationSummary::kCallOnSlowPath;
7505 break;
7506 }
7507
7508 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
7509 locations->SetInAt(0, Location::RequiresRegister());
7510 locations->SetInAt(1, Location::RequiresRegister());
Artem Serov657022c2016-11-23 14:19:38 +00007511 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007512}
7513
7514void InstructionCodeGeneratorARMVIXL::VisitCheckCast(HCheckCast* instruction) {
7515 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
7516 LocationSummary* locations = instruction->GetLocations();
7517 Location obj_loc = locations->InAt(0);
7518 vixl32::Register obj = InputRegisterAt(instruction, 0);
7519 vixl32::Register cls = InputRegisterAt(instruction, 1);
7520 Location temp_loc = locations->GetTemp(0);
7521 vixl32::Register temp = RegisterFrom(temp_loc);
Artem Serov657022c2016-11-23 14:19:38 +00007522 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
7523 DCHECK_LE(num_temps, 3u);
7524 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
7525 Location maybe_temp3_loc = (num_temps >= 3) ? locations->GetTemp(2) : Location::NoLocation();
7526 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
7527 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
7528 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
7529 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
7530 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
7531 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
7532 const uint32_t object_array_data_offset =
7533 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007534
Artem Serov657022c2016-11-23 14:19:38 +00007535 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
7536 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
7537 // read barriers is done for performance and code size reasons.
7538 bool is_type_check_slow_path_fatal = false;
7539 if (!kEmitCompilerReadBarrier) {
7540 is_type_check_slow_path_fatal =
7541 (type_check_kind == TypeCheckKind::kExactCheck ||
7542 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
7543 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
7544 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
7545 !instruction->CanThrowIntoCatchBlock();
7546 }
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007547 SlowPathCodeARMVIXL* type_check_slow_path =
7548 new (GetGraph()->GetArena()) TypeCheckSlowPathARMVIXL(instruction,
7549 is_type_check_slow_path_fatal);
7550 codegen_->AddSlowPath(type_check_slow_path);
7551
7552 vixl32::Label done;
Anton Kirilov6f644202017-02-27 18:29:45 +00007553 vixl32::Label* final_label = codegen_->GetFinalLabel(instruction, &done);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007554 // Avoid null check if we know obj is not null.
7555 if (instruction->MustDoNullCheck()) {
Anton Kirilov6f644202017-02-27 18:29:45 +00007556 __ CompareAndBranchIfZero(obj, final_label, /* far_target */ false);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007557 }
7558
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007559 switch (type_check_kind) {
7560 case TypeCheckKind::kExactCheck:
7561 case TypeCheckKind::kArrayCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00007562 // /* HeapReference<Class> */ temp = obj->klass_
7563 GenerateReferenceLoadTwoRegisters(instruction,
7564 temp_loc,
7565 obj_loc,
7566 class_offset,
7567 maybe_temp2_loc,
7568 kWithoutReadBarrier);
7569
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007570 __ Cmp(temp, cls);
7571 // Jump to slow path for throwing the exception or doing a
7572 // more involved array check.
7573 __ B(ne, type_check_slow_path->GetEntryLabel());
7574 break;
7575 }
7576
7577 case TypeCheckKind::kAbstractClassCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00007578 // /* HeapReference<Class> */ temp = obj->klass_
7579 GenerateReferenceLoadTwoRegisters(instruction,
7580 temp_loc,
7581 obj_loc,
7582 class_offset,
7583 maybe_temp2_loc,
7584 kWithoutReadBarrier);
7585
Artem Serovcfbe9132016-10-14 15:58:56 +01007586 // If the class is abstract, we eagerly fetch the super class of the
7587 // object to avoid doing a comparison we know will fail.
7588 vixl32::Label loop;
7589 __ Bind(&loop);
7590 // /* HeapReference<Class> */ temp = temp->super_class_
Artem Serov657022c2016-11-23 14:19:38 +00007591 GenerateReferenceLoadOneRegister(instruction,
7592 temp_loc,
7593 super_offset,
7594 maybe_temp2_loc,
7595 kWithoutReadBarrier);
Artem Serovcfbe9132016-10-14 15:58:56 +01007596
7597 // If the class reference currently in `temp` is null, jump to the slow path to throw the
7598 // exception.
xueliang.zhongf51bc622016-11-04 09:23:32 +00007599 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
Artem Serovcfbe9132016-10-14 15:58:56 +01007600
7601 // Otherwise, compare the classes.
7602 __ Cmp(temp, cls);
Artem Serov517d9f62016-12-12 15:51:15 +00007603 __ B(ne, &loop, /* far_target */ false);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007604 break;
7605 }
7606
7607 case TypeCheckKind::kClassHierarchyCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00007608 // /* HeapReference<Class> */ temp = obj->klass_
7609 GenerateReferenceLoadTwoRegisters(instruction,
7610 temp_loc,
7611 obj_loc,
7612 class_offset,
7613 maybe_temp2_loc,
7614 kWithoutReadBarrier);
7615
Artem Serovcfbe9132016-10-14 15:58:56 +01007616 // Walk over the class hierarchy to find a match.
7617 vixl32::Label loop;
7618 __ Bind(&loop);
7619 __ Cmp(temp, cls);
Anton Kirilov6f644202017-02-27 18:29:45 +00007620 __ B(eq, final_label, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007621
7622 // /* HeapReference<Class> */ temp = temp->super_class_
Artem Serov657022c2016-11-23 14:19:38 +00007623 GenerateReferenceLoadOneRegister(instruction,
7624 temp_loc,
7625 super_offset,
7626 maybe_temp2_loc,
7627 kWithoutReadBarrier);
Artem Serovcfbe9132016-10-14 15:58:56 +01007628
7629 // If the class reference currently in `temp` is null, jump to the slow path to throw the
7630 // exception.
xueliang.zhongf51bc622016-11-04 09:23:32 +00007631 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
Artem Serovcfbe9132016-10-14 15:58:56 +01007632 // Otherwise, jump to the beginning of the loop.
7633 __ B(&loop);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007634 break;
7635 }
7636
Artem Serovcfbe9132016-10-14 15:58:56 +01007637 case TypeCheckKind::kArrayObjectCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00007638 // /* HeapReference<Class> */ temp = obj->klass_
7639 GenerateReferenceLoadTwoRegisters(instruction,
7640 temp_loc,
7641 obj_loc,
7642 class_offset,
7643 maybe_temp2_loc,
7644 kWithoutReadBarrier);
7645
Artem Serovcfbe9132016-10-14 15:58:56 +01007646 // Do an exact check.
7647 __ Cmp(temp, cls);
Anton Kirilov6f644202017-02-27 18:29:45 +00007648 __ B(eq, final_label, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007649
7650 // Otherwise, we need to check that the object's class is a non-primitive array.
7651 // /* HeapReference<Class> */ temp = temp->component_type_
Artem Serov657022c2016-11-23 14:19:38 +00007652 GenerateReferenceLoadOneRegister(instruction,
7653 temp_loc,
7654 component_offset,
7655 maybe_temp2_loc,
7656 kWithoutReadBarrier);
Artem Serovcfbe9132016-10-14 15:58:56 +01007657 // If the component type is null, jump to the slow path to throw the exception.
xueliang.zhongf51bc622016-11-04 09:23:32 +00007658 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
Artem Serovcfbe9132016-10-14 15:58:56 +01007659 // Otherwise,the object is indeed an array, jump to label `check_non_primitive_component_type`
7660 // to further check that this component type is not a primitive type.
7661 GetAssembler()->LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
7662 static_assert(Primitive::kPrimNot == 0, "Expected 0 for art::Primitive::kPrimNot");
xueliang.zhongf51bc622016-11-04 09:23:32 +00007663 __ CompareAndBranchIfNonZero(temp, type_check_slow_path->GetEntryLabel());
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007664 break;
7665 }
7666
7667 case TypeCheckKind::kUnresolvedCheck:
Artem Serov657022c2016-11-23 14:19:38 +00007668 // We always go into the type check slow path for the unresolved check case.
Artem Serovcfbe9132016-10-14 15:58:56 +01007669 // We cannot directly call the CheckCast runtime entry point
7670 // without resorting to a type checking slow path here (i.e. by
7671 // calling InvokeRuntime directly), as it would require to
7672 // assign fixed registers for the inputs of this HInstanceOf
7673 // instruction (following the runtime calling convention), which
7674 // might be cluttered by the potential first read barrier
7675 // emission at the beginning of this method.
Artem Serov657022c2016-11-23 14:19:38 +00007676
Artem Serovcfbe9132016-10-14 15:58:56 +01007677 __ B(type_check_slow_path->GetEntryLabel());
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007678 break;
Artem Serov657022c2016-11-23 14:19:38 +00007679
7680 case TypeCheckKind::kInterfaceCheck: {
7681 // Avoid read barriers to improve performance of the fast path. We can not get false
7682 // positives by doing this.
7683 // /* HeapReference<Class> */ temp = obj->klass_
7684 GenerateReferenceLoadTwoRegisters(instruction,
7685 temp_loc,
7686 obj_loc,
7687 class_offset,
7688 maybe_temp2_loc,
7689 kWithoutReadBarrier);
7690
7691 // /* HeapReference<Class> */ temp = temp->iftable_
7692 GenerateReferenceLoadTwoRegisters(instruction,
7693 temp_loc,
7694 temp_loc,
7695 iftable_offset,
7696 maybe_temp2_loc,
7697 kWithoutReadBarrier);
7698 // Iftable is never null.
7699 __ Ldr(RegisterFrom(maybe_temp2_loc), MemOperand(temp, array_length_offset));
7700 // Loop through the iftable and check if any class matches.
7701 vixl32::Label start_loop;
7702 __ Bind(&start_loop);
7703 __ CompareAndBranchIfZero(RegisterFrom(maybe_temp2_loc),
7704 type_check_slow_path->GetEntryLabel());
7705 __ Ldr(RegisterFrom(maybe_temp3_loc), MemOperand(temp, object_array_data_offset));
7706 GetAssembler()->MaybeUnpoisonHeapReference(RegisterFrom(maybe_temp3_loc));
7707 // Go to next interface.
7708 __ Add(temp, temp, Operand::From(2 * kHeapReferenceSize));
7709 __ Sub(RegisterFrom(maybe_temp2_loc), RegisterFrom(maybe_temp2_loc), 2);
7710 // Compare the classes and continue the loop if they do not match.
7711 __ Cmp(cls, RegisterFrom(maybe_temp3_loc));
Artem Serov517d9f62016-12-12 15:51:15 +00007712 __ B(ne, &start_loop, /* far_target */ false);
Artem Serov657022c2016-11-23 14:19:38 +00007713 break;
7714 }
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007715 }
Anton Kirilov6f644202017-02-27 18:29:45 +00007716 if (done.IsReferenced()) {
7717 __ Bind(&done);
7718 }
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007719
7720 __ Bind(type_check_slow_path->GetExitLabel());
7721}
7722
Artem Serov551b28f2016-10-18 19:11:30 +01007723void LocationsBuilderARMVIXL::VisitMonitorOperation(HMonitorOperation* instruction) {
7724 LocationSummary* locations =
7725 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
7726 InvokeRuntimeCallingConventionARMVIXL calling_convention;
7727 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
7728}
7729
7730void InstructionCodeGeneratorARMVIXL::VisitMonitorOperation(HMonitorOperation* instruction) {
7731 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
7732 instruction,
7733 instruction->GetDexPc());
7734 if (instruction->IsEnter()) {
7735 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
7736 } else {
7737 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
7738 }
7739}
7740
Artem Serov02109dd2016-09-23 17:17:54 +01007741void LocationsBuilderARMVIXL::VisitAnd(HAnd* instruction) {
7742 HandleBitwiseOperation(instruction, AND);
7743}
7744
7745void LocationsBuilderARMVIXL::VisitOr(HOr* instruction) {
7746 HandleBitwiseOperation(instruction, ORR);
7747}
7748
7749void LocationsBuilderARMVIXL::VisitXor(HXor* instruction) {
7750 HandleBitwiseOperation(instruction, EOR);
7751}
7752
7753void LocationsBuilderARMVIXL::HandleBitwiseOperation(HBinaryOperation* instruction, Opcode opcode) {
7754 LocationSummary* locations =
7755 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
7756 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
7757 || instruction->GetResultType() == Primitive::kPrimLong);
7758 // Note: GVN reorders commutative operations to have the constant on the right hand side.
7759 locations->SetInAt(0, Location::RequiresRegister());
7760 locations->SetInAt(1, ArmEncodableConstantOrRegister(instruction->InputAt(1), opcode));
7761 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
7762}
7763
7764void InstructionCodeGeneratorARMVIXL::VisitAnd(HAnd* instruction) {
7765 HandleBitwiseOperation(instruction);
7766}
7767
7768void InstructionCodeGeneratorARMVIXL::VisitOr(HOr* instruction) {
7769 HandleBitwiseOperation(instruction);
7770}
7771
7772void InstructionCodeGeneratorARMVIXL::VisitXor(HXor* instruction) {
7773 HandleBitwiseOperation(instruction);
7774}
7775
Artem Serov2bbc9532016-10-21 11:51:50 +01007776void LocationsBuilderARMVIXL::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
7777 LocationSummary* locations =
7778 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
7779 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
7780 || instruction->GetResultType() == Primitive::kPrimLong);
7781
7782 locations->SetInAt(0, Location::RequiresRegister());
7783 locations->SetInAt(1, Location::RequiresRegister());
7784 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
7785}
7786
7787void InstructionCodeGeneratorARMVIXL::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
7788 LocationSummary* locations = instruction->GetLocations();
7789 Location first = locations->InAt(0);
7790 Location second = locations->InAt(1);
7791 Location out = locations->Out();
7792
7793 if (instruction->GetResultType() == Primitive::kPrimInt) {
7794 vixl32::Register first_reg = RegisterFrom(first);
7795 vixl32::Register second_reg = RegisterFrom(second);
7796 vixl32::Register out_reg = RegisterFrom(out);
7797
7798 switch (instruction->GetOpKind()) {
7799 case HInstruction::kAnd:
7800 __ Bic(out_reg, first_reg, second_reg);
7801 break;
7802 case HInstruction::kOr:
7803 __ Orn(out_reg, first_reg, second_reg);
7804 break;
7805 // There is no EON on arm.
7806 case HInstruction::kXor:
7807 default:
7808 LOG(FATAL) << "Unexpected instruction " << instruction->DebugName();
7809 UNREACHABLE();
7810 }
7811 return;
7812
7813 } else {
7814 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
7815 vixl32::Register first_low = LowRegisterFrom(first);
7816 vixl32::Register first_high = HighRegisterFrom(first);
7817 vixl32::Register second_low = LowRegisterFrom(second);
7818 vixl32::Register second_high = HighRegisterFrom(second);
7819 vixl32::Register out_low = LowRegisterFrom(out);
7820 vixl32::Register out_high = HighRegisterFrom(out);
7821
7822 switch (instruction->GetOpKind()) {
7823 case HInstruction::kAnd:
7824 __ Bic(out_low, first_low, second_low);
7825 __ Bic(out_high, first_high, second_high);
7826 break;
7827 case HInstruction::kOr:
7828 __ Orn(out_low, first_low, second_low);
7829 __ Orn(out_high, first_high, second_high);
7830 break;
7831 // There is no EON on arm.
7832 case HInstruction::kXor:
7833 default:
7834 LOG(FATAL) << "Unexpected instruction " << instruction->DebugName();
7835 UNREACHABLE();
7836 }
7837 }
7838}
7839
Anton Kirilov74234da2017-01-13 14:42:47 +00007840void LocationsBuilderARMVIXL::VisitDataProcWithShifterOp(
7841 HDataProcWithShifterOp* instruction) {
7842 DCHECK(instruction->GetType() == Primitive::kPrimInt ||
7843 instruction->GetType() == Primitive::kPrimLong);
7844 LocationSummary* locations =
7845 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
7846 const bool overlap = instruction->GetType() == Primitive::kPrimLong &&
7847 HDataProcWithShifterOp::IsExtensionOp(instruction->GetOpKind());
7848
7849 locations->SetInAt(0, Location::RequiresRegister());
7850 locations->SetInAt(1, Location::RequiresRegister());
7851 locations->SetOut(Location::RequiresRegister(),
7852 overlap ? Location::kOutputOverlap : Location::kNoOutputOverlap);
7853}
7854
7855void InstructionCodeGeneratorARMVIXL::VisitDataProcWithShifterOp(
7856 HDataProcWithShifterOp* instruction) {
7857 const LocationSummary* const locations = instruction->GetLocations();
7858 const HInstruction::InstructionKind kind = instruction->GetInstrKind();
7859 const HDataProcWithShifterOp::OpKind op_kind = instruction->GetOpKind();
7860
7861 if (instruction->GetType() == Primitive::kPrimInt) {
7862 DCHECK(!HDataProcWithShifterOp::IsExtensionOp(op_kind));
7863
7864 const vixl32::Register second = instruction->InputAt(1)->GetType() == Primitive::kPrimLong
7865 ? LowRegisterFrom(locations->InAt(1))
7866 : InputRegisterAt(instruction, 1);
7867
7868 GenerateDataProcInstruction(kind,
7869 OutputRegister(instruction),
7870 InputRegisterAt(instruction, 0),
7871 Operand(second,
7872 ShiftFromOpKind(op_kind),
7873 instruction->GetShiftAmount()),
7874 codegen_);
7875 } else {
7876 DCHECK_EQ(instruction->GetType(), Primitive::kPrimLong);
7877
7878 if (HDataProcWithShifterOp::IsExtensionOp(op_kind)) {
7879 const vixl32::Register second = InputRegisterAt(instruction, 1);
7880
7881 DCHECK(!LowRegisterFrom(locations->Out()).Is(second));
7882 GenerateDataProc(kind,
7883 locations->Out(),
7884 locations->InAt(0),
7885 second,
7886 Operand(second, ShiftType::ASR, 31),
7887 codegen_);
7888 } else {
7889 GenerateLongDataProc(instruction, codegen_);
7890 }
7891 }
7892}
7893
Artem Serov02109dd2016-09-23 17:17:54 +01007894// TODO(VIXL): Remove optimizations in the helper when they are implemented in vixl.
7895void InstructionCodeGeneratorARMVIXL::GenerateAndConst(vixl32::Register out,
7896 vixl32::Register first,
7897 uint32_t value) {
7898 // Optimize special cases for individual halfs of `and-long` (`and` is simplified earlier).
7899 if (value == 0xffffffffu) {
7900 if (!out.Is(first)) {
7901 __ Mov(out, first);
7902 }
7903 return;
7904 }
7905 if (value == 0u) {
7906 __ Mov(out, 0);
7907 return;
7908 }
7909 if (GetAssembler()->ShifterOperandCanHold(AND, value)) {
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00007910 __ And(out, first, value);
7911 } else if (GetAssembler()->ShifterOperandCanHold(BIC, ~value)) {
7912 __ Bic(out, first, ~value);
Artem Serov02109dd2016-09-23 17:17:54 +01007913 } else {
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00007914 DCHECK(IsPowerOfTwo(value + 1));
7915 __ Ubfx(out, first, 0, WhichPowerOf2(value + 1));
Artem Serov02109dd2016-09-23 17:17:54 +01007916 }
7917}
7918
7919// TODO(VIXL): Remove optimizations in the helper when they are implemented in vixl.
7920void InstructionCodeGeneratorARMVIXL::GenerateOrrConst(vixl32::Register out,
7921 vixl32::Register first,
7922 uint32_t value) {
7923 // Optimize special cases for individual halfs of `or-long` (`or` is simplified earlier).
7924 if (value == 0u) {
7925 if (!out.Is(first)) {
7926 __ Mov(out, first);
7927 }
7928 return;
7929 }
7930 if (value == 0xffffffffu) {
7931 __ Mvn(out, 0);
7932 return;
7933 }
7934 if (GetAssembler()->ShifterOperandCanHold(ORR, value)) {
7935 __ Orr(out, first, value);
7936 } else {
7937 DCHECK(GetAssembler()->ShifterOperandCanHold(ORN, ~value));
7938 __ Orn(out, first, ~value);
7939 }
7940}
7941
7942// TODO(VIXL): Remove optimizations in the helper when they are implemented in vixl.
7943void InstructionCodeGeneratorARMVIXL::GenerateEorConst(vixl32::Register out,
7944 vixl32::Register first,
7945 uint32_t value) {
7946 // Optimize special case for individual halfs of `xor-long` (`xor` is simplified earlier).
7947 if (value == 0u) {
7948 if (!out.Is(first)) {
7949 __ Mov(out, first);
7950 }
7951 return;
7952 }
7953 __ Eor(out, first, value);
7954}
7955
Anton Kirilovdda43962016-11-21 19:55:20 +00007956void InstructionCodeGeneratorARMVIXL::GenerateAddLongConst(Location out,
7957 Location first,
7958 uint64_t value) {
7959 vixl32::Register out_low = LowRegisterFrom(out);
7960 vixl32::Register out_high = HighRegisterFrom(out);
7961 vixl32::Register first_low = LowRegisterFrom(first);
7962 vixl32::Register first_high = HighRegisterFrom(first);
7963 uint32_t value_low = Low32Bits(value);
7964 uint32_t value_high = High32Bits(value);
7965 if (value_low == 0u) {
7966 if (!out_low.Is(first_low)) {
7967 __ Mov(out_low, first_low);
7968 }
7969 __ Add(out_high, first_high, value_high);
7970 return;
7971 }
7972 __ Adds(out_low, first_low, value_low);
Scott Wakelingbffdc702016-12-07 17:46:03 +00007973 if (GetAssembler()->ShifterOperandCanHold(ADC, value_high, kCcDontCare)) {
Anton Kirilovdda43962016-11-21 19:55:20 +00007974 __ Adc(out_high, first_high, value_high);
Scott Wakelingbffdc702016-12-07 17:46:03 +00007975 } else if (GetAssembler()->ShifterOperandCanHold(SBC, ~value_high, kCcDontCare)) {
Anton Kirilovdda43962016-11-21 19:55:20 +00007976 __ Sbc(out_high, first_high, ~value_high);
7977 } else {
7978 LOG(FATAL) << "Unexpected constant " << value_high;
7979 UNREACHABLE();
7980 }
7981}
7982
Artem Serov02109dd2016-09-23 17:17:54 +01007983void InstructionCodeGeneratorARMVIXL::HandleBitwiseOperation(HBinaryOperation* instruction) {
7984 LocationSummary* locations = instruction->GetLocations();
7985 Location first = locations->InAt(0);
7986 Location second = locations->InAt(1);
7987 Location out = locations->Out();
7988
7989 if (second.IsConstant()) {
7990 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
7991 uint32_t value_low = Low32Bits(value);
7992 if (instruction->GetResultType() == Primitive::kPrimInt) {
7993 vixl32::Register first_reg = InputRegisterAt(instruction, 0);
7994 vixl32::Register out_reg = OutputRegister(instruction);
7995 if (instruction->IsAnd()) {
7996 GenerateAndConst(out_reg, first_reg, value_low);
7997 } else if (instruction->IsOr()) {
7998 GenerateOrrConst(out_reg, first_reg, value_low);
7999 } else {
8000 DCHECK(instruction->IsXor());
8001 GenerateEorConst(out_reg, first_reg, value_low);
8002 }
8003 } else {
8004 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
8005 uint32_t value_high = High32Bits(value);
8006 vixl32::Register first_low = LowRegisterFrom(first);
8007 vixl32::Register first_high = HighRegisterFrom(first);
8008 vixl32::Register out_low = LowRegisterFrom(out);
8009 vixl32::Register out_high = HighRegisterFrom(out);
8010 if (instruction->IsAnd()) {
8011 GenerateAndConst(out_low, first_low, value_low);
8012 GenerateAndConst(out_high, first_high, value_high);
8013 } else if (instruction->IsOr()) {
8014 GenerateOrrConst(out_low, first_low, value_low);
8015 GenerateOrrConst(out_high, first_high, value_high);
8016 } else {
8017 DCHECK(instruction->IsXor());
8018 GenerateEorConst(out_low, first_low, value_low);
8019 GenerateEorConst(out_high, first_high, value_high);
8020 }
8021 }
8022 return;
8023 }
8024
8025 if (instruction->GetResultType() == Primitive::kPrimInt) {
8026 vixl32::Register first_reg = InputRegisterAt(instruction, 0);
8027 vixl32::Register second_reg = InputRegisterAt(instruction, 1);
8028 vixl32::Register out_reg = OutputRegister(instruction);
8029 if (instruction->IsAnd()) {
8030 __ And(out_reg, first_reg, second_reg);
8031 } else if (instruction->IsOr()) {
8032 __ Orr(out_reg, first_reg, second_reg);
8033 } else {
8034 DCHECK(instruction->IsXor());
8035 __ Eor(out_reg, first_reg, second_reg);
8036 }
8037 } else {
8038 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
8039 vixl32::Register first_low = LowRegisterFrom(first);
8040 vixl32::Register first_high = HighRegisterFrom(first);
8041 vixl32::Register second_low = LowRegisterFrom(second);
8042 vixl32::Register second_high = HighRegisterFrom(second);
8043 vixl32::Register out_low = LowRegisterFrom(out);
8044 vixl32::Register out_high = HighRegisterFrom(out);
8045 if (instruction->IsAnd()) {
8046 __ And(out_low, first_low, second_low);
8047 __ And(out_high, first_high, second_high);
8048 } else if (instruction->IsOr()) {
8049 __ Orr(out_low, first_low, second_low);
8050 __ Orr(out_high, first_high, second_high);
8051 } else {
8052 DCHECK(instruction->IsXor());
8053 __ Eor(out_low, first_low, second_low);
8054 __ Eor(out_high, first_high, second_high);
8055 }
8056 }
8057}
8058
Artem Serovcfbe9132016-10-14 15:58:56 +01008059void InstructionCodeGeneratorARMVIXL::GenerateReferenceLoadOneRegister(
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008060 HInstruction* instruction,
Artem Serovcfbe9132016-10-14 15:58:56 +01008061 Location out,
8062 uint32_t offset,
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008063 Location maybe_temp,
8064 ReadBarrierOption read_barrier_option) {
Artem Serovcfbe9132016-10-14 15:58:56 +01008065 vixl32::Register out_reg = RegisterFrom(out);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008066 if (read_barrier_option == kWithReadBarrier) {
8067 CHECK(kEmitCompilerReadBarrier);
8068 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
8069 if (kUseBakerReadBarrier) {
8070 // Load with fast path based Baker's read barrier.
8071 // /* HeapReference<Object> */ out = *(out + offset)
8072 codegen_->GenerateFieldLoadWithBakerReadBarrier(
8073 instruction, out, out_reg, offset, maybe_temp, /* needs_null_check */ false);
8074 } else {
8075 // Load with slow path based read barrier.
8076 // Save the value of `out` into `maybe_temp` before overwriting it
8077 // in the following move operation, as we will need it for the
8078 // read barrier below.
8079 __ Mov(RegisterFrom(maybe_temp), out_reg);
8080 // /* HeapReference<Object> */ out = *(out + offset)
8081 GetAssembler()->LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
8082 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
8083 }
Artem Serovcfbe9132016-10-14 15:58:56 +01008084 } else {
8085 // Plain load with no read barrier.
8086 // /* HeapReference<Object> */ out = *(out + offset)
8087 GetAssembler()->LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
8088 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
8089 }
8090}
8091
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008092void InstructionCodeGeneratorARMVIXL::GenerateReferenceLoadTwoRegisters(
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008093 HInstruction* instruction,
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008094 Location out,
8095 Location obj,
8096 uint32_t offset,
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008097 Location maybe_temp,
8098 ReadBarrierOption read_barrier_option) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008099 vixl32::Register out_reg = RegisterFrom(out);
8100 vixl32::Register obj_reg = RegisterFrom(obj);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008101 if (read_barrier_option == kWithReadBarrier) {
8102 CHECK(kEmitCompilerReadBarrier);
8103 if (kUseBakerReadBarrier) {
8104 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
8105 // Load with fast path based Baker's read barrier.
8106 // /* HeapReference<Object> */ out = *(obj + offset)
8107 codegen_->GenerateFieldLoadWithBakerReadBarrier(
8108 instruction, out, obj_reg, offset, maybe_temp, /* needs_null_check */ false);
8109 } else {
8110 // Load with slow path based read barrier.
8111 // /* HeapReference<Object> */ out = *(obj + offset)
8112 GetAssembler()->LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
8113 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
8114 }
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008115 } else {
8116 // Plain load with no read barrier.
8117 // /* HeapReference<Object> */ out = *(obj + offset)
8118 GetAssembler()->LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
8119 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
8120 }
8121}
8122
Scott Wakelinga7812ae2016-10-17 10:03:36 +01008123void InstructionCodeGeneratorARMVIXL::GenerateGcRootFieldLoad(
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008124 HInstruction* instruction,
Scott Wakelinga7812ae2016-10-17 10:03:36 +01008125 Location root,
8126 vixl32::Register obj,
8127 uint32_t offset,
Artem Serovd4cc5b22016-11-04 11:19:09 +00008128 ReadBarrierOption read_barrier_option) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01008129 vixl32::Register root_reg = RegisterFrom(root);
Artem Serovd4cc5b22016-11-04 11:19:09 +00008130 if (read_barrier_option == kWithReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008131 DCHECK(kEmitCompilerReadBarrier);
8132 if (kUseBakerReadBarrier) {
8133 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
Roland Levillainba650a42017-03-06 13:52:32 +00008134 // Baker's read barrier are used.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008135 if (kBakerReadBarrierLinkTimeThunksEnableForGcRoots &&
8136 !Runtime::Current()->UseJitCompilation()) {
8137 // Note that we do not actually check the value of `GetIsGcMarking()`
8138 // to decide whether to mark the loaded GC root or not. Instead, we
8139 // load into `temp` (actually kBakerCcEntrypointRegister) the read
8140 // barrier mark introspection entrypoint. If `temp` is null, it means
8141 // that `GetIsGcMarking()` is false, and vice versa.
8142 //
8143 // We use link-time generated thunks for the slow path. That thunk
8144 // checks the reference and jumps to the entrypoint if needed.
8145 //
8146 // temp = Thread::Current()->pReadBarrierMarkIntrospection
8147 // lr = &return_address;
8148 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
8149 // if (temp != nullptr) {
8150 // goto gc_root_thunk<root_reg>(lr)
8151 // }
8152 // return_address:
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008153
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008154 UseScratchRegisterScope temps(GetVIXLAssembler());
8155 ExcludeIPAndBakerCcEntrypointRegister(&temps, instruction);
Vladimir Marko88abba22017-05-03 17:09:25 +01008156 bool narrow = CanEmitNarrowLdr(root_reg, obj, offset);
8157 uint32_t custom_data = linker::Thumb2RelativePatcher::EncodeBakerReadBarrierGcRootData(
8158 root_reg.GetCode(), narrow);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008159 vixl32::Label* bne_label = codegen_->NewBakerReadBarrierPatch(custom_data);
Roland Levillainba650a42017-03-06 13:52:32 +00008160
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008161 // entrypoint_reg =
8162 // Thread::Current()->pReadBarrierMarkReg12, i.e. pReadBarrierMarkIntrospection.
8163 DCHECK_EQ(ip.GetCode(), 12u);
8164 const int32_t entry_point_offset =
8165 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmPointerSize>(ip.GetCode());
8166 __ Ldr(kBakerCcEntrypointRegister, MemOperand(tr, entry_point_offset));
Roland Levillainba650a42017-03-06 13:52:32 +00008167
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008168 vixl::EmissionCheckScope guard(GetVIXLAssembler(),
8169 4 * vixl32::kMaxInstructionSizeInBytes);
8170 vixl32::Label return_address;
8171 EmitAdrCode adr(GetVIXLAssembler(), lr, &return_address);
8172 __ cmp(kBakerCcEntrypointRegister, Operand(0));
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008173 // Currently the offset is always within range. If that changes,
8174 // we shall have to split the load the same way as for fields.
8175 DCHECK_LT(offset, kReferenceLoadMinFarOffset);
Vladimir Marko88abba22017-05-03 17:09:25 +01008176 ptrdiff_t old_offset = GetVIXLAssembler()->GetBuffer()->GetCursorOffset();
8177 __ ldr(EncodingSize(narrow ? Narrow : Wide), root_reg, MemOperand(obj, offset));
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008178 EmitPlaceholderBne(codegen_, bne_label);
8179 __ Bind(&return_address);
Vladimir Marko88abba22017-05-03 17:09:25 +01008180 DCHECK_EQ(old_offset - GetVIXLAssembler()->GetBuffer()->GetCursorOffset(),
8181 narrow ? BAKER_MARK_INTROSPECTION_GC_ROOT_LDR_NARROW_OFFSET
8182 : BAKER_MARK_INTROSPECTION_GC_ROOT_LDR_WIDE_OFFSET);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008183 } else {
8184 // Note that we do not actually check the value of
8185 // `GetIsGcMarking()` to decide whether to mark the loaded GC
8186 // root or not. Instead, we load into `temp` the read barrier
8187 // mark entry point corresponding to register `root`. If `temp`
8188 // is null, it means that `GetIsGcMarking()` is false, and vice
8189 // versa.
8190 //
8191 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
8192 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
8193 // if (temp != nullptr) { // <=> Thread::Current()->GetIsGcMarking()
8194 // // Slow path.
8195 // root = temp(root); // root = ReadBarrier::Mark(root); // Runtime entry point call.
8196 // }
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008197
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008198 // Slow path marking the GC root `root`. The entrypoint will already be loaded in `temp`.
8199 Location temp = LocationFrom(lr);
8200 SlowPathCodeARMVIXL* slow_path =
8201 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARMVIXL(
8202 instruction, root, /* entrypoint */ temp);
8203 codegen_->AddSlowPath(slow_path);
8204
8205 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
8206 const int32_t entry_point_offset =
8207 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmPointerSize>(root.reg());
8208 // Loading the entrypoint does not require a load acquire since it is only changed when
8209 // threads are suspended or running a checkpoint.
8210 GetAssembler()->LoadFromOffset(kLoadWord, RegisterFrom(temp), tr, entry_point_offset);
8211
8212 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
8213 GetAssembler()->LoadFromOffset(kLoadWord, root_reg, obj, offset);
8214 static_assert(
8215 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
8216 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
8217 "have different sizes.");
8218 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
8219 "art::mirror::CompressedReference<mirror::Object> and int32_t "
8220 "have different sizes.");
8221
8222 // The entrypoint is null when the GC is not marking, this prevents one load compared to
8223 // checking GetIsGcMarking.
8224 __ CompareAndBranchIfNonZero(RegisterFrom(temp), slow_path->GetEntryLabel());
8225 __ Bind(slow_path->GetExitLabel());
8226 }
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008227 } else {
8228 // GC root loaded through a slow path for read barriers other
8229 // than Baker's.
8230 // /* GcRoot<mirror::Object>* */ root = obj + offset
8231 __ Add(root_reg, obj, offset);
8232 // /* mirror::Object* */ root = root->Read()
8233 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
8234 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01008235 } else {
8236 // Plain GC root load with no read barrier.
8237 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
8238 GetAssembler()->LoadFromOffset(kLoadWord, root_reg, obj, offset);
8239 // Note that GC roots are not affected by heap poisoning, thus we
8240 // do not have to unpoison `root_reg` here.
8241 }
8242}
8243
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008244void CodeGeneratorARMVIXL::MaybeAddBakerCcEntrypointTempForFields(LocationSummary* locations) {
8245 DCHECK(kEmitCompilerReadBarrier);
8246 DCHECK(kUseBakerReadBarrier);
8247 if (kBakerReadBarrierLinkTimeThunksEnableForFields) {
8248 if (!Runtime::Current()->UseJitCompilation()) {
8249 locations->AddTemp(Location::RegisterLocation(kBakerCcEntrypointRegister.GetCode()));
8250 }
8251 }
8252}
8253
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008254void CodeGeneratorARMVIXL::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
8255 Location ref,
8256 vixl32::Register obj,
8257 uint32_t offset,
8258 Location temp,
8259 bool needs_null_check) {
8260 DCHECK(kEmitCompilerReadBarrier);
8261 DCHECK(kUseBakerReadBarrier);
8262
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008263 if (kBakerReadBarrierLinkTimeThunksEnableForFields &&
8264 !Runtime::Current()->UseJitCompilation()) {
8265 // Note that we do not actually check the value of `GetIsGcMarking()`
8266 // to decide whether to mark the loaded reference or not. Instead, we
8267 // load into `temp` (actually kBakerCcEntrypointRegister) the read
8268 // barrier mark introspection entrypoint. If `temp` is null, it means
8269 // that `GetIsGcMarking()` is false, and vice versa.
8270 //
8271 // We use link-time generated thunks for the slow path. That thunk checks
8272 // the holder and jumps to the entrypoint if needed. If the holder is not
8273 // gray, it creates a fake dependency and returns to the LDR instruction.
8274 //
8275 // temp = Thread::Current()->pReadBarrierMarkIntrospection
8276 // lr = &gray_return_address;
8277 // if (temp != nullptr) {
8278 // goto field_thunk<holder_reg, base_reg>(lr)
8279 // }
8280 // not_gray_return_address:
8281 // // Original reference load. If the offset is too large to fit
8282 // // into LDR, we use an adjusted base register here.
Vladimir Marko88abba22017-05-03 17:09:25 +01008283 // HeapReference<mirror::Object> reference = *(obj+offset);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008284 // gray_return_address:
8285
8286 DCHECK_ALIGNED(offset, sizeof(mirror::HeapReference<mirror::Object>));
Vladimir Marko88abba22017-05-03 17:09:25 +01008287 vixl32::Register ref_reg = RegisterFrom(ref, Primitive::kPrimNot);
8288 bool narrow = CanEmitNarrowLdr(ref_reg, obj, offset);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008289 vixl32::Register base = obj;
8290 if (offset >= kReferenceLoadMinFarOffset) {
8291 base = RegisterFrom(temp);
8292 DCHECK(!base.Is(kBakerCcEntrypointRegister));
8293 static_assert(IsPowerOfTwo(kReferenceLoadMinFarOffset), "Expecting a power of 2.");
8294 __ Add(base, obj, Operand(offset & ~(kReferenceLoadMinFarOffset - 1u)));
8295 offset &= (kReferenceLoadMinFarOffset - 1u);
Vladimir Marko88abba22017-05-03 17:09:25 +01008296 // Use narrow LDR only for small offsets. Generating narrow encoding LDR for the large
8297 // offsets with `(offset & (kReferenceLoadMinFarOffset - 1u)) < 32u` would most likely
8298 // increase the overall code size when taking the generated thunks into account.
8299 DCHECK(!narrow);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008300 }
8301 UseScratchRegisterScope temps(GetVIXLAssembler());
8302 ExcludeIPAndBakerCcEntrypointRegister(&temps, instruction);
8303 uint32_t custom_data = linker::Thumb2RelativePatcher::EncodeBakerReadBarrierFieldData(
Vladimir Marko88abba22017-05-03 17:09:25 +01008304 base.GetCode(), obj.GetCode(), narrow);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008305 vixl32::Label* bne_label = NewBakerReadBarrierPatch(custom_data);
8306
8307 // entrypoint_reg =
8308 // Thread::Current()->pReadBarrierMarkReg12, i.e. pReadBarrierMarkIntrospection.
8309 DCHECK_EQ(ip.GetCode(), 12u);
8310 const int32_t entry_point_offset =
8311 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmPointerSize>(ip.GetCode());
8312 __ Ldr(kBakerCcEntrypointRegister, MemOperand(tr, entry_point_offset));
8313
8314 vixl::EmissionCheckScope guard(
8315 GetVIXLAssembler(),
8316 (kPoisonHeapReferences ? 5u : 4u) * vixl32::kMaxInstructionSizeInBytes);
8317 vixl32::Label return_address;
8318 EmitAdrCode adr(GetVIXLAssembler(), lr, &return_address);
8319 __ cmp(kBakerCcEntrypointRegister, Operand(0));
8320 EmitPlaceholderBne(this, bne_label);
Vladimir Marko88abba22017-05-03 17:09:25 +01008321 ptrdiff_t old_offset = GetVIXLAssembler()->GetBuffer()->GetCursorOffset();
8322 __ ldr(EncodingSize(narrow ? Narrow : Wide), ref_reg, MemOperand(base, offset));
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008323 if (needs_null_check) {
8324 MaybeRecordImplicitNullCheck(instruction);
8325 }
Vladimir Marko88abba22017-05-03 17:09:25 +01008326 // Note: We need a specific width for the unpoisoning NEG.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008327 if (kPoisonHeapReferences) {
Vladimir Marko88abba22017-05-03 17:09:25 +01008328 if (narrow) {
8329 // The only 16-bit encoding is T1 which sets flags outside IT block (i.e. RSBS, not RSB).
8330 __ rsbs(EncodingSize(Narrow), ref_reg, ref_reg, Operand(0));
8331 } else {
8332 __ rsb(EncodingSize(Wide), ref_reg, ref_reg, Operand(0));
8333 }
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008334 }
8335 __ Bind(&return_address);
Vladimir Marko88abba22017-05-03 17:09:25 +01008336 DCHECK_EQ(old_offset - GetVIXLAssembler()->GetBuffer()->GetCursorOffset(),
8337 narrow ? BAKER_MARK_INTROSPECTION_FIELD_LDR_NARROW_OFFSET
8338 : BAKER_MARK_INTROSPECTION_FIELD_LDR_WIDE_OFFSET);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008339 return;
8340 }
8341
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008342 // /* HeapReference<Object> */ ref = *(obj + offset)
8343 Location no_index = Location::NoLocation();
8344 ScaleFactor no_scale_factor = TIMES_1;
8345 GenerateReferenceLoadWithBakerReadBarrier(
8346 instruction, ref, obj, offset, no_index, no_scale_factor, temp, needs_null_check);
Roland Levillain6070e882016-11-03 17:51:58 +00008347}
8348
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008349void CodeGeneratorARMVIXL::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
8350 Location ref,
8351 vixl32::Register obj,
8352 uint32_t data_offset,
8353 Location index,
8354 Location temp,
8355 bool needs_null_check) {
8356 DCHECK(kEmitCompilerReadBarrier);
8357 DCHECK(kUseBakerReadBarrier);
8358
8359 static_assert(
8360 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
8361 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008362 ScaleFactor scale_factor = TIMES_4;
8363
8364 if (kBakerReadBarrierLinkTimeThunksEnableForArrays &&
8365 !Runtime::Current()->UseJitCompilation()) {
8366 // Note that we do not actually check the value of `GetIsGcMarking()`
8367 // to decide whether to mark the loaded reference or not. Instead, we
8368 // load into `temp` (actually kBakerCcEntrypointRegister) the read
8369 // barrier mark introspection entrypoint. If `temp` is null, it means
8370 // that `GetIsGcMarking()` is false, and vice versa.
8371 //
8372 // We use link-time generated thunks for the slow path. That thunk checks
8373 // the holder and jumps to the entrypoint if needed. If the holder is not
8374 // gray, it creates a fake dependency and returns to the LDR instruction.
8375 //
8376 // temp = Thread::Current()->pReadBarrierMarkIntrospection
8377 // lr = &gray_return_address;
8378 // if (temp != nullptr) {
8379 // goto field_thunk<holder_reg, base_reg>(lr)
8380 // }
8381 // not_gray_return_address:
8382 // // Original reference load. If the offset is too large to fit
8383 // // into LDR, we use an adjusted base register here.
Vladimir Marko88abba22017-05-03 17:09:25 +01008384 // HeapReference<mirror::Object> reference = data[index];
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008385 // gray_return_address:
8386
8387 DCHECK(index.IsValid());
8388 vixl32::Register index_reg = RegisterFrom(index, Primitive::kPrimInt);
8389 vixl32::Register ref_reg = RegisterFrom(ref, Primitive::kPrimNot);
8390 vixl32::Register data_reg = RegisterFrom(temp, Primitive::kPrimInt); // Raw pointer.
8391 DCHECK(!data_reg.Is(kBakerCcEntrypointRegister));
8392
8393 UseScratchRegisterScope temps(GetVIXLAssembler());
8394 ExcludeIPAndBakerCcEntrypointRegister(&temps, instruction);
8395 uint32_t custom_data =
8396 linker::Thumb2RelativePatcher::EncodeBakerReadBarrierArrayData(data_reg.GetCode());
8397 vixl32::Label* bne_label = NewBakerReadBarrierPatch(custom_data);
8398
8399 // entrypoint_reg =
8400 // Thread::Current()->pReadBarrierMarkReg16, i.e. pReadBarrierMarkIntrospection.
8401 DCHECK_EQ(ip.GetCode(), 12u);
8402 const int32_t entry_point_offset =
8403 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmPointerSize>(ip.GetCode());
8404 __ Ldr(kBakerCcEntrypointRegister, MemOperand(tr, entry_point_offset));
8405 __ Add(data_reg, obj, Operand(data_offset));
8406
8407 vixl::EmissionCheckScope guard(
8408 GetVIXLAssembler(),
8409 (kPoisonHeapReferences ? 5u : 4u) * vixl32::kMaxInstructionSizeInBytes);
8410 vixl32::Label return_address;
8411 EmitAdrCode adr(GetVIXLAssembler(), lr, &return_address);
8412 __ cmp(kBakerCcEntrypointRegister, Operand(0));
8413 EmitPlaceholderBne(this, bne_label);
Vladimir Marko88abba22017-05-03 17:09:25 +01008414 ptrdiff_t old_offset = GetVIXLAssembler()->GetBuffer()->GetCursorOffset();
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008415 __ ldr(ref_reg, MemOperand(data_reg, index_reg, vixl32::LSL, scale_factor));
8416 DCHECK(!needs_null_check); // The thunk cannot handle the null check.
8417 // Note: We need a Wide NEG for the unpoisoning.
8418 if (kPoisonHeapReferences) {
8419 __ rsb(EncodingSize(Wide), ref_reg, ref_reg, Operand(0));
8420 }
8421 __ Bind(&return_address);
Vladimir Marko88abba22017-05-03 17:09:25 +01008422 DCHECK_EQ(old_offset - GetVIXLAssembler()->GetBuffer()->GetCursorOffset(),
8423 BAKER_MARK_INTROSPECTION_ARRAY_LDR_OFFSET);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008424 return;
8425 }
8426
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008427 // /* HeapReference<Object> */ ref =
8428 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008429 GenerateReferenceLoadWithBakerReadBarrier(
8430 instruction, ref, obj, data_offset, index, scale_factor, temp, needs_null_check);
Roland Levillain6070e882016-11-03 17:51:58 +00008431}
8432
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008433void CodeGeneratorARMVIXL::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
8434 Location ref,
8435 vixl32::Register obj,
8436 uint32_t offset,
8437 Location index,
8438 ScaleFactor scale_factor,
8439 Location temp,
Roland Levillainff487002017-03-07 16:50:01 +00008440 bool needs_null_check) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008441 DCHECK(kEmitCompilerReadBarrier);
8442 DCHECK(kUseBakerReadBarrier);
8443
Roland Levillain54f869e2017-03-06 13:54:11 +00008444 // Query `art::Thread::Current()->GetIsGcMarking()` to decide
8445 // whether we need to enter the slow path to mark the reference.
8446 // Then, in the slow path, check the gray bit in the lock word of
8447 // the reference's holder (`obj`) to decide whether to mark `ref` or
8448 // not.
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008449 //
Roland Levillainba650a42017-03-06 13:52:32 +00008450 // Note that we do not actually check the value of `GetIsGcMarking()`;
Roland Levillainff487002017-03-07 16:50:01 +00008451 // instead, we load into `temp2` the read barrier mark entry point
8452 // corresponding to register `ref`. If `temp2` is null, it means
8453 // that `GetIsGcMarking()` is false, and vice versa.
8454 //
8455 // temp2 = Thread::Current()->pReadBarrierMarkReg ## root.reg()
8456 // if (temp2 != nullptr) { // <=> Thread::Current()->GetIsGcMarking()
8457 // // Slow path.
8458 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
8459 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
8460 // HeapReference<mirror::Object> ref = *src; // Original reference load.
8461 // bool is_gray = (rb_state == ReadBarrier::GrayState());
8462 // if (is_gray) {
8463 // ref = temp2(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
8464 // }
8465 // } else {
8466 // HeapReference<mirror::Object> ref = *src; // Original reference load.
8467 // }
8468
8469 vixl32::Register temp_reg = RegisterFrom(temp);
8470
8471 // Slow path marking the object `ref` when the GC is marking. The
8472 // entrypoint will already be loaded in `temp2`.
8473 Location temp2 = LocationFrom(lr);
8474 SlowPathCodeARMVIXL* slow_path =
8475 new (GetGraph()->GetArena()) LoadReferenceWithBakerReadBarrierSlowPathARMVIXL(
8476 instruction,
8477 ref,
8478 obj,
8479 offset,
8480 index,
8481 scale_factor,
8482 needs_null_check,
8483 temp_reg,
8484 /* entrypoint */ temp2);
8485 AddSlowPath(slow_path);
8486
8487 // temp2 = Thread::Current()->pReadBarrierMarkReg ## ref.reg()
8488 const int32_t entry_point_offset =
8489 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmPointerSize>(ref.reg());
8490 // Loading the entrypoint does not require a load acquire since it is only changed when
8491 // threads are suspended or running a checkpoint.
8492 GetAssembler()->LoadFromOffset(kLoadWord, RegisterFrom(temp2), tr, entry_point_offset);
8493 // The entrypoint is null when the GC is not marking, this prevents one load compared to
8494 // checking GetIsGcMarking.
8495 __ CompareAndBranchIfNonZero(RegisterFrom(temp2), slow_path->GetEntryLabel());
8496 // Fast path: the GC is not marking: just load the reference.
8497 GenerateRawReferenceLoad(instruction, ref, obj, offset, index, scale_factor, needs_null_check);
8498 __ Bind(slow_path->GetExitLabel());
8499}
8500
8501void CodeGeneratorARMVIXL::UpdateReferenceFieldWithBakerReadBarrier(HInstruction* instruction,
8502 Location ref,
8503 vixl32::Register obj,
8504 Location field_offset,
8505 Location temp,
8506 bool needs_null_check,
8507 vixl32::Register temp2) {
8508 DCHECK(kEmitCompilerReadBarrier);
8509 DCHECK(kUseBakerReadBarrier);
8510
8511 // Query `art::Thread::Current()->GetIsGcMarking()` to decide
8512 // whether we need to enter the slow path to update the reference
8513 // field within `obj`. Then, in the slow path, check the gray bit
8514 // in the lock word of the reference's holder (`obj`) to decide
8515 // whether to mark `ref` and update the field or not.
8516 //
8517 // Note that we do not actually check the value of `GetIsGcMarking()`;
Roland Levillainba650a42017-03-06 13:52:32 +00008518 // instead, we load into `temp3` the read barrier mark entry point
8519 // corresponding to register `ref`. If `temp3` is null, it means
8520 // that `GetIsGcMarking()` is false, and vice versa.
8521 //
8522 // temp3 = Thread::Current()->pReadBarrierMarkReg ## root.reg()
Roland Levillainba650a42017-03-06 13:52:32 +00008523 // if (temp3 != nullptr) { // <=> Thread::Current()->GetIsGcMarking()
8524 // // Slow path.
Roland Levillain54f869e2017-03-06 13:54:11 +00008525 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
8526 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
Roland Levillainff487002017-03-07 16:50:01 +00008527 // HeapReference<mirror::Object> ref = *(obj + field_offset); // Reference load.
Roland Levillain54f869e2017-03-06 13:54:11 +00008528 // bool is_gray = (rb_state == ReadBarrier::GrayState());
8529 // if (is_gray) {
Roland Levillainff487002017-03-07 16:50:01 +00008530 // old_ref = ref;
Roland Levillain54f869e2017-03-06 13:54:11 +00008531 // ref = temp3(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
Roland Levillainff487002017-03-07 16:50:01 +00008532 // compareAndSwapObject(obj, field_offset, old_ref, ref);
Roland Levillain54f869e2017-03-06 13:54:11 +00008533 // }
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008534 // }
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008535
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008536 vixl32::Register temp_reg = RegisterFrom(temp);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008537
Roland Levillainff487002017-03-07 16:50:01 +00008538 // Slow path updating the object reference at address `obj + field_offset`
8539 // when the GC is marking. The entrypoint will already be loaded in `temp3`.
Roland Levillainba650a42017-03-06 13:52:32 +00008540 Location temp3 = LocationFrom(lr);
Roland Levillainff487002017-03-07 16:50:01 +00008541 SlowPathCodeARMVIXL* slow_path =
8542 new (GetGraph()->GetArena()) LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARMVIXL(
8543 instruction,
8544 ref,
8545 obj,
8546 /* offset */ 0u,
8547 /* index */ field_offset,
8548 /* scale_factor */ ScaleFactor::TIMES_1,
8549 needs_null_check,
8550 temp_reg,
8551 temp2,
8552 /* entrypoint */ temp3);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008553 AddSlowPath(slow_path);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008554
Roland Levillainba650a42017-03-06 13:52:32 +00008555 // temp3 = Thread::Current()->pReadBarrierMarkReg ## ref.reg()
8556 const int32_t entry_point_offset =
8557 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmPointerSize>(ref.reg());
8558 // Loading the entrypoint does not require a load acquire since it is only changed when
8559 // threads are suspended or running a checkpoint.
8560 GetAssembler()->LoadFromOffset(kLoadWord, RegisterFrom(temp3), tr, entry_point_offset);
Roland Levillainba650a42017-03-06 13:52:32 +00008561 // The entrypoint is null when the GC is not marking, this prevents one load compared to
8562 // checking GetIsGcMarking.
8563 __ CompareAndBranchIfNonZero(RegisterFrom(temp3), slow_path->GetEntryLabel());
Roland Levillainff487002017-03-07 16:50:01 +00008564 // Fast path: the GC is not marking: nothing to do (the field is
8565 // up-to-date, and we don't need to load the reference).
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008566 __ Bind(slow_path->GetExitLabel());
Roland Levillain844e6532016-11-03 16:09:47 +00008567}
Scott Wakelingfe885462016-09-22 10:24:38 +01008568
Roland Levillainba650a42017-03-06 13:52:32 +00008569void CodeGeneratorARMVIXL::GenerateRawReferenceLoad(HInstruction* instruction,
8570 Location ref,
8571 vixl::aarch32::Register obj,
8572 uint32_t offset,
8573 Location index,
8574 ScaleFactor scale_factor,
8575 bool needs_null_check) {
8576 Primitive::Type type = Primitive::kPrimNot;
8577 vixl32::Register ref_reg = RegisterFrom(ref, type);
8578
8579 // If needed, vixl::EmissionCheckScope guards are used to ensure
8580 // that no pools are emitted between the load (macro) instruction
8581 // and MaybeRecordImplicitNullCheck.
8582
Scott Wakelingfe885462016-09-22 10:24:38 +01008583 if (index.IsValid()) {
8584 // Load types involving an "index": ArrayGet,
8585 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
8586 // intrinsics.
Roland Levillainba650a42017-03-06 13:52:32 +00008587 // /* HeapReference<mirror::Object> */ ref = *(obj + offset + (index << scale_factor))
Scott Wakelingfe885462016-09-22 10:24:38 +01008588 if (index.IsConstant()) {
8589 size_t computed_offset =
8590 (Int32ConstantFrom(index) << scale_factor) + offset;
Roland Levillainba650a42017-03-06 13:52:32 +00008591 vixl::EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Scott Wakelingfe885462016-09-22 10:24:38 +01008592 GetAssembler()->LoadFromOffset(kLoadWord, ref_reg, obj, computed_offset);
Roland Levillainba650a42017-03-06 13:52:32 +00008593 if (needs_null_check) {
8594 MaybeRecordImplicitNullCheck(instruction);
8595 }
Scott Wakelingfe885462016-09-22 10:24:38 +01008596 } else {
8597 // Handle the special case of the
8598 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
8599 // intrinsics, which use a register pair as index ("long
8600 // offset"), of which only the low part contains data.
8601 vixl32::Register index_reg = index.IsRegisterPair()
8602 ? LowRegisterFrom(index)
8603 : RegisterFrom(index);
8604 UseScratchRegisterScope temps(GetVIXLAssembler());
Roland Levillainba650a42017-03-06 13:52:32 +00008605 vixl32::Register temp = temps.Acquire();
8606 __ Add(temp, obj, Operand(index_reg, ShiftType::LSL, scale_factor));
8607 {
8608 vixl::EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
8609 GetAssembler()->LoadFromOffset(kLoadWord, ref_reg, temp, offset);
8610 if (needs_null_check) {
8611 MaybeRecordImplicitNullCheck(instruction);
8612 }
8613 }
Scott Wakelingfe885462016-09-22 10:24:38 +01008614 }
8615 } else {
Roland Levillainba650a42017-03-06 13:52:32 +00008616 // /* HeapReference<mirror::Object> */ ref = *(obj + offset)
8617 vixl::EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Scott Wakelingfe885462016-09-22 10:24:38 +01008618 GetAssembler()->LoadFromOffset(kLoadWord, ref_reg, obj, offset);
Roland Levillainba650a42017-03-06 13:52:32 +00008619 if (needs_null_check) {
8620 MaybeRecordImplicitNullCheck(instruction);
8621 }
Scott Wakelingfe885462016-09-22 10:24:38 +01008622 }
8623
Roland Levillain844e6532016-11-03 16:09:47 +00008624 // Object* ref = ref_addr->AsMirrorPtr()
8625 GetAssembler()->MaybeUnpoisonHeapReference(ref_reg);
Roland Levillain844e6532016-11-03 16:09:47 +00008626}
8627
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008628void CodeGeneratorARMVIXL::GenerateReadBarrierSlow(HInstruction* instruction,
8629 Location out,
8630 Location ref,
8631 Location obj,
8632 uint32_t offset,
8633 Location index) {
8634 DCHECK(kEmitCompilerReadBarrier);
8635
8636 // Insert a slow path based read barrier *after* the reference load.
8637 //
8638 // If heap poisoning is enabled, the unpoisoning of the loaded
8639 // reference will be carried out by the runtime within the slow
8640 // path.
8641 //
8642 // Note that `ref` currently does not get unpoisoned (when heap
8643 // poisoning is enabled), which is alright as the `ref` argument is
8644 // not used by the artReadBarrierSlow entry point.
8645 //
8646 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
8647 SlowPathCodeARMVIXL* slow_path = new (GetGraph()->GetArena())
8648 ReadBarrierForHeapReferenceSlowPathARMVIXL(instruction, out, ref, obj, offset, index);
8649 AddSlowPath(slow_path);
8650
8651 __ B(slow_path->GetEntryLabel());
8652 __ Bind(slow_path->GetExitLabel());
8653}
8654
8655void CodeGeneratorARMVIXL::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
Artem Serov02d37832016-10-25 15:25:33 +01008656 Location out,
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008657 Location ref,
8658 Location obj,
8659 uint32_t offset,
8660 Location index) {
Artem Serov02d37832016-10-25 15:25:33 +01008661 if (kEmitCompilerReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008662 // Baker's read barriers shall be handled by the fast path
8663 // (CodeGeneratorARM::GenerateReferenceLoadWithBakerReadBarrier).
Artem Serov02d37832016-10-25 15:25:33 +01008664 DCHECK(!kUseBakerReadBarrier);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008665 // If heap poisoning is enabled, unpoisoning will be taken care of
8666 // by the runtime within the slow path.
8667 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Artem Serov02d37832016-10-25 15:25:33 +01008668 } else if (kPoisonHeapReferences) {
8669 GetAssembler()->UnpoisonHeapReference(RegisterFrom(out));
8670 }
8671}
8672
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008673void CodeGeneratorARMVIXL::GenerateReadBarrierForRootSlow(HInstruction* instruction,
8674 Location out,
8675 Location root) {
8676 DCHECK(kEmitCompilerReadBarrier);
8677
8678 // Insert a slow path based read barrier *after* the GC root load.
8679 //
8680 // Note that GC roots are not affected by heap poisoning, so we do
8681 // not need to do anything special for this here.
8682 SlowPathCodeARMVIXL* slow_path =
8683 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathARMVIXL(instruction, out, root);
8684 AddSlowPath(slow_path);
8685
8686 __ B(slow_path->GetEntryLabel());
8687 __ Bind(slow_path->GetExitLabel());
8688}
8689
Artem Serov02d37832016-10-25 15:25:33 +01008690// Check if the desired_dispatch_info is supported. If it is, return it,
8691// otherwise return a fall-back info that should be used instead.
8692HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARMVIXL::GetSupportedInvokeStaticOrDirectDispatch(
Artem Serovd4cc5b22016-11-04 11:19:09 +00008693 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +00008694 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Nicolas Geoffraye807ff72017-01-23 09:03:12 +00008695 return desired_dispatch_info;
Artem Serov02d37832016-10-25 15:25:33 +01008696}
8697
Scott Wakelingfe885462016-09-22 10:24:38 +01008698vixl32::Register CodeGeneratorARMVIXL::GetInvokeStaticOrDirectExtraParameter(
8699 HInvokeStaticOrDirect* invoke, vixl32::Register temp) {
8700 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
8701 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
8702 if (!invoke->GetLocations()->Intrinsified()) {
8703 return RegisterFrom(location);
8704 }
8705 // For intrinsics we allow any location, so it may be on the stack.
8706 if (!location.IsRegister()) {
8707 GetAssembler()->LoadFromOffset(kLoadWord, temp, sp, location.GetStackIndex());
8708 return temp;
8709 }
8710 // For register locations, check if the register was saved. If so, get it from the stack.
8711 // Note: There is a chance that the register was saved but not overwritten, so we could
8712 // save one load. However, since this is just an intrinsic slow path we prefer this
8713 // simple and more robust approach rather that trying to determine if that's the case.
8714 SlowPathCode* slow_path = GetCurrentSlowPath();
Scott Wakelingd5cd4972017-02-03 11:38:35 +00008715 if (slow_path != nullptr && slow_path->IsCoreRegisterSaved(RegisterFrom(location).GetCode())) {
Scott Wakelingfe885462016-09-22 10:24:38 +01008716 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(RegisterFrom(location).GetCode());
8717 GetAssembler()->LoadFromOffset(kLoadWord, temp, sp, stack_offset);
8718 return temp;
8719 }
8720 return RegisterFrom(location);
8721}
8722
TatWai Chongd8c052a2016-11-02 16:12:48 +08008723Location CodeGeneratorARMVIXL::GenerateCalleeMethodStaticOrDirectCall(
Scott Wakelingfe885462016-09-22 10:24:38 +01008724 HInvokeStaticOrDirect* invoke, Location temp) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00008725 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
Scott Wakelingfe885462016-09-22 10:24:38 +01008726 switch (invoke->GetMethodLoadKind()) {
8727 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
8728 uint32_t offset =
8729 GetThreadOffset<kArmPointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
8730 // temp = thread->string_init_entrypoint
Artem Serovd4cc5b22016-11-04 11:19:09 +00008731 GetAssembler()->LoadFromOffset(kLoadWord, RegisterFrom(temp), tr, offset);
8732 break;
8733 }
8734 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
8735 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
8736 break;
8737 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
8738 __ Mov(RegisterFrom(temp), Operand::From(invoke->GetMethodAddress()));
8739 break;
Artem Serovd4cc5b22016-11-04 11:19:09 +00008740 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
8741 HArmDexCacheArraysBase* base =
8742 invoke->InputAt(invoke->GetSpecialInputIndex())->AsArmDexCacheArraysBase();
8743 vixl32::Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke, RegisterFrom(temp));
8744 int32_t offset = invoke->GetDexCacheArrayOffset() - base->GetElementOffset();
8745 GetAssembler()->LoadFromOffset(kLoadWord, RegisterFrom(temp), base_reg, offset);
Scott Wakelingfe885462016-09-22 10:24:38 +01008746 break;
8747 }
8748 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
8749 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
8750 vixl32::Register method_reg;
Artem Serovd4cc5b22016-11-04 11:19:09 +00008751 vixl32::Register reg = RegisterFrom(temp);
Scott Wakelingfe885462016-09-22 10:24:38 +01008752 if (current_method.IsRegister()) {
8753 method_reg = RegisterFrom(current_method);
8754 } else {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008755 DCHECK(invoke->GetLocations()->Intrinsified());
8756 DCHECK(!current_method.IsValid());
Artem Serovd4cc5b22016-11-04 11:19:09 +00008757 method_reg = reg;
8758 GetAssembler()->LoadFromOffset(kLoadWord, reg, sp, kCurrentMethodStackOffset);
Scott Wakelingfe885462016-09-22 10:24:38 +01008759 }
8760 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
8761 GetAssembler()->LoadFromOffset(
8762 kLoadWord,
Artem Serovd4cc5b22016-11-04 11:19:09 +00008763 reg,
Scott Wakelingfe885462016-09-22 10:24:38 +01008764 method_reg,
8765 ArtMethod::DexCacheResolvedMethodsOffset(kArmPointerSize).Int32Value());
8766 // temp = temp[index_in_cache];
8767 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
8768 uint32_t index_in_cache = invoke->GetDexMethodIndex();
8769 GetAssembler()->LoadFromOffset(
Artem Serovd4cc5b22016-11-04 11:19:09 +00008770 kLoadWord, reg, reg, CodeGenerator::GetCachePointerOffset(index_in_cache));
Scott Wakelingfe885462016-09-22 10:24:38 +01008771 break;
8772 }
Scott Wakelingfe885462016-09-22 10:24:38 +01008773 }
TatWai Chongd8c052a2016-11-02 16:12:48 +08008774 return callee_method;
8775}
8776
8777void CodeGeneratorARMVIXL::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
8778 Location temp) {
8779 Location callee_method = GenerateCalleeMethodStaticOrDirectCall(invoke, temp);
Scott Wakelingfe885462016-09-22 10:24:38 +01008780
Artem Serovd4cc5b22016-11-04 11:19:09 +00008781 switch (invoke->GetCodePtrLocation()) {
8782 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
8783 __ Bl(GetFrameEntryLabel());
8784 break;
Artem Serovd4cc5b22016-11-04 11:19:09 +00008785 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
8786 // LR = callee_method->entry_point_from_quick_compiled_code_
8787 GetAssembler()->LoadFromOffset(
8788 kLoadWord,
8789 lr,
8790 RegisterFrom(callee_method),
8791 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize).Int32Value());
Alexandre Rames374ddf32016-11-04 10:40:49 +00008792 {
8793 // 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 +00008794 ExactAssemblyScope aas(GetVIXLAssembler(),
8795 vixl32::k16BitT32InstructionSizeInBytes,
8796 CodeBufferCheckScope::kExactSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00008797 // LR()
8798 __ blx(lr);
8799 }
Artem Serovd4cc5b22016-11-04 11:19:09 +00008800 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01008801 }
8802
Scott Wakelingfe885462016-09-22 10:24:38 +01008803 DCHECK(!IsLeafMethod());
8804}
8805
8806void CodeGeneratorARMVIXL::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
8807 vixl32::Register temp = RegisterFrom(temp_location);
8808 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
8809 invoke->GetVTableIndex(), kArmPointerSize).Uint32Value();
8810
8811 // Use the calling convention instead of the location of the receiver, as
8812 // intrinsics may have put the receiver in a different register. In the intrinsics
8813 // slow path, the arguments have been moved to the right place, so here we are
8814 // guaranteed that the receiver is the first register of the calling convention.
8815 InvokeDexCallingConventionARMVIXL calling_convention;
8816 vixl32::Register receiver = calling_convention.GetRegisterAt(0);
8817 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Alexandre Rames374ddf32016-11-04 10:40:49 +00008818 {
8819 // Make sure the pc is recorded immediately after the `ldr` instruction.
Artem Serov0fb37192016-12-06 18:13:40 +00008820 ExactAssemblyScope aas(GetVIXLAssembler(),
8821 vixl32::kMaxInstructionSizeInBytes,
8822 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00008823 // /* HeapReference<Class> */ temp = receiver->klass_
8824 __ ldr(temp, MemOperand(receiver, class_offset));
8825 MaybeRecordImplicitNullCheck(invoke);
8826 }
Scott Wakelingfe885462016-09-22 10:24:38 +01008827 // Instead of simply (possibly) unpoisoning `temp` here, we should
8828 // emit a read barrier for the previous class reference load.
8829 // However this is not required in practice, as this is an
8830 // intermediate/temporary reference and because the current
8831 // concurrent copying collector keeps the from-space memory
8832 // intact/accessible until the end of the marking phase (the
8833 // concurrent copying collector may not in the future).
8834 GetAssembler()->MaybeUnpoisonHeapReference(temp);
8835
8836 // temp = temp->GetMethodAt(method_offset);
8837 uint32_t entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(
8838 kArmPointerSize).Int32Value();
8839 GetAssembler()->LoadFromOffset(kLoadWord, temp, temp, method_offset);
8840 // LR = temp->GetEntryPoint();
8841 GetAssembler()->LoadFromOffset(kLoadWord, lr, temp, entry_point);
8842 // LR();
Alexandre Rames374ddf32016-11-04 10:40:49 +00008843 // This `blx` *must* be the *last* instruction generated by this stub, so that calls to
8844 // `RecordPcInfo()` immediately following record the correct pc. Use a scope to help guarantee
8845 // that.
8846 // 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 +00008847 ExactAssemblyScope aas(GetVIXLAssembler(),
8848 vixl32::k16BitT32InstructionSizeInBytes,
8849 CodeBufferCheckScope::kExactSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00008850 __ blx(lr);
Scott Wakelingfe885462016-09-22 10:24:38 +01008851}
8852
Artem Serovd4cc5b22016-11-04 11:19:09 +00008853CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewPcRelativeStringPatch(
Vladimir Marko6bec91c2017-01-09 15:03:12 +00008854 const DexFile& dex_file, dex::StringIndex string_index) {
8855 return NewPcRelativePatch(dex_file, string_index.index_, &pc_relative_string_patches_);
Artem Serovd4cc5b22016-11-04 11:19:09 +00008856}
8857
8858CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewPcRelativeTypePatch(
8859 const DexFile& dex_file, dex::TypeIndex type_index) {
8860 return NewPcRelativePatch(dex_file, type_index.index_, &pc_relative_type_patches_);
8861}
8862
Vladimir Marko1998cd02017-01-13 13:02:58 +00008863CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewTypeBssEntryPatch(
8864 const DexFile& dex_file, dex::TypeIndex type_index) {
8865 return NewPcRelativePatch(dex_file, type_index.index_, &type_bss_entry_patches_);
8866}
8867
Artem Serovd4cc5b22016-11-04 11:19:09 +00008868CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewPcRelativeDexCacheArrayPatch(
8869 const DexFile& dex_file, uint32_t element_offset) {
8870 return NewPcRelativePatch(dex_file, element_offset, &pc_relative_dex_cache_patches_);
8871}
8872
8873CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewPcRelativePatch(
8874 const DexFile& dex_file, uint32_t offset_or_index, ArenaDeque<PcRelativePatchInfo>* patches) {
8875 patches->emplace_back(dex_file, offset_or_index);
8876 return &patches->back();
8877}
8878
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008879vixl::aarch32::Label* CodeGeneratorARMVIXL::NewBakerReadBarrierPatch(uint32_t custom_data) {
8880 baker_read_barrier_patches_.emplace_back(custom_data);
8881 return &baker_read_barrier_patches_.back().label;
8882}
8883
Artem Serovc5fcb442016-12-02 19:19:58 +00008884VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateBootImageAddressLiteral(uint32_t address) {
Richard Uhlerc52f3032017-03-02 13:45:45 +00008885 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), &uint32_literals_);
Artem Serovc5fcb442016-12-02 19:19:58 +00008886}
8887
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00008888VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateJitStringLiteral(
8889 const DexFile& dex_file,
8890 dex::StringIndex string_index,
8891 Handle<mirror::String> handle) {
8892 jit_string_roots_.Overwrite(StringReference(&dex_file, string_index),
8893 reinterpret_cast64<uint64_t>(handle.GetReference()));
Artem Serovc5fcb442016-12-02 19:19:58 +00008894 return jit_string_patches_.GetOrCreate(
8895 StringReference(&dex_file, string_index),
8896 [this]() {
8897 return GetAssembler()->CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u);
8898 });
8899}
8900
8901VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateJitClassLiteral(const DexFile& dex_file,
8902 dex::TypeIndex type_index,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00008903 Handle<mirror::Class> handle) {
8904 jit_class_roots_.Overwrite(TypeReference(&dex_file, type_index),
8905 reinterpret_cast64<uint64_t>(handle.GetReference()));
Artem Serovc5fcb442016-12-02 19:19:58 +00008906 return jit_class_patches_.GetOrCreate(
8907 TypeReference(&dex_file, type_index),
8908 [this]() {
8909 return GetAssembler()->CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u);
8910 });
8911}
8912
Artem Serovd4cc5b22016-11-04 11:19:09 +00008913template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
8914inline void CodeGeneratorARMVIXL::EmitPcRelativeLinkerPatches(
8915 const ArenaDeque<PcRelativePatchInfo>& infos,
8916 ArenaVector<LinkerPatch>* linker_patches) {
8917 for (const PcRelativePatchInfo& info : infos) {
8918 const DexFile& dex_file = info.target_dex_file;
8919 size_t offset_or_index = info.offset_or_index;
8920 DCHECK(info.add_pc_label.IsBound());
8921 uint32_t add_pc_offset = dchecked_integral_cast<uint32_t>(info.add_pc_label.GetLocation());
8922 // Add MOVW patch.
8923 DCHECK(info.movw_label.IsBound());
8924 uint32_t movw_offset = dchecked_integral_cast<uint32_t>(info.movw_label.GetLocation());
8925 linker_patches->push_back(Factory(movw_offset, &dex_file, add_pc_offset, offset_or_index));
8926 // Add MOVT patch.
8927 DCHECK(info.movt_label.IsBound());
8928 uint32_t movt_offset = dchecked_integral_cast<uint32_t>(info.movt_label.GetLocation());
8929 linker_patches->push_back(Factory(movt_offset, &dex_file, add_pc_offset, offset_or_index));
8930 }
8931}
8932
8933void CodeGeneratorARMVIXL::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
8934 DCHECK(linker_patches->empty());
8935 size_t size =
Artem Serovd4cc5b22016-11-04 11:19:09 +00008936 /* MOVW+MOVT for each entry */ 2u * pc_relative_dex_cache_patches_.size() +
8937 /* MOVW+MOVT for each entry */ 2u * pc_relative_string_patches_.size() +
Artem Serovc5fcb442016-12-02 19:19:58 +00008938 /* MOVW+MOVT for each entry */ 2u * pc_relative_type_patches_.size() +
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008939 /* MOVW+MOVT for each entry */ 2u * type_bss_entry_patches_.size() +
8940 baker_read_barrier_patches_.size();
Artem Serovd4cc5b22016-11-04 11:19:09 +00008941 linker_patches->reserve(size);
Artem Serovd4cc5b22016-11-04 11:19:09 +00008942 EmitPcRelativeLinkerPatches<LinkerPatch::DexCacheArrayPatch>(pc_relative_dex_cache_patches_,
8943 linker_patches);
8944 if (!GetCompilerOptions().IsBootImage()) {
Vladimir Marko1998cd02017-01-13 13:02:58 +00008945 DCHECK(pc_relative_type_patches_.empty());
Artem Serovd4cc5b22016-11-04 11:19:09 +00008946 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(pc_relative_string_patches_,
8947 linker_patches);
8948 } else {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00008949 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(pc_relative_type_patches_,
8950 linker_patches);
Artem Serovd4cc5b22016-11-04 11:19:09 +00008951 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(pc_relative_string_patches_,
8952 linker_patches);
8953 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00008954 EmitPcRelativeLinkerPatches<LinkerPatch::TypeBssEntryPatch>(type_bss_entry_patches_,
8955 linker_patches);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008956 for (const BakerReadBarrierPatchInfo& info : baker_read_barrier_patches_) {
8957 linker_patches->push_back(LinkerPatch::BakerReadBarrierBranchPatch(info.label.GetLocation(),
8958 info.custom_data));
8959 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00008960 DCHECK_EQ(size, linker_patches->size());
Artem Serovc5fcb442016-12-02 19:19:58 +00008961}
8962
8963VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateUint32Literal(
8964 uint32_t value,
8965 Uint32ToLiteralMap* map) {
8966 return map->GetOrCreate(
8967 value,
8968 [this, value]() {
8969 return GetAssembler()->CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ value);
8970 });
8971}
8972
Artem Serov2bbc9532016-10-21 11:51:50 +01008973void LocationsBuilderARMVIXL::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
8974 LocationSummary* locations =
8975 new (GetGraph()->GetArena()) LocationSummary(instr, LocationSummary::kNoCall);
8976 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
8977 Location::RequiresRegister());
8978 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
8979 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
8980 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8981}
8982
8983void InstructionCodeGeneratorARMVIXL::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
8984 vixl32::Register res = OutputRegister(instr);
8985 vixl32::Register accumulator =
8986 InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
8987 vixl32::Register mul_left =
8988 InputRegisterAt(instr, HMultiplyAccumulate::kInputMulLeftIndex);
8989 vixl32::Register mul_right =
8990 InputRegisterAt(instr, HMultiplyAccumulate::kInputMulRightIndex);
8991
8992 if (instr->GetOpKind() == HInstruction::kAdd) {
8993 __ Mla(res, mul_left, mul_right, accumulator);
8994 } else {
8995 __ Mls(res, mul_left, mul_right, accumulator);
8996 }
8997}
8998
Artem Serov551b28f2016-10-18 19:11:30 +01008999void LocationsBuilderARMVIXL::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
9000 // Nothing to do, this should be removed during prepare for register allocator.
9001 LOG(FATAL) << "Unreachable";
9002}
9003
9004void InstructionCodeGeneratorARMVIXL::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
9005 // Nothing to do, this should be removed during prepare for register allocator.
9006 LOG(FATAL) << "Unreachable";
9007}
9008
9009// Simple implementation of packed switch - generate cascaded compare/jumps.
9010void LocationsBuilderARMVIXL::VisitPackedSwitch(HPackedSwitch* switch_instr) {
9011 LocationSummary* locations =
9012 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
9013 locations->SetInAt(0, Location::RequiresRegister());
9014 if (switch_instr->GetNumEntries() > kPackedSwitchCompareJumpThreshold &&
9015 codegen_->GetAssembler()->GetVIXLAssembler()->IsUsingT32()) {
9016 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the table base.
9017 if (switch_instr->GetStartValue() != 0) {
9018 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the bias.
9019 }
9020 }
9021}
9022
9023// TODO(VIXL): Investigate and reach the parity with old arm codegen.
9024void InstructionCodeGeneratorARMVIXL::VisitPackedSwitch(HPackedSwitch* switch_instr) {
9025 int32_t lower_bound = switch_instr->GetStartValue();
9026 uint32_t num_entries = switch_instr->GetNumEntries();
9027 LocationSummary* locations = switch_instr->GetLocations();
9028 vixl32::Register value_reg = InputRegisterAt(switch_instr, 0);
9029 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
9030
9031 if (num_entries <= kPackedSwitchCompareJumpThreshold ||
9032 !codegen_->GetAssembler()->GetVIXLAssembler()->IsUsingT32()) {
9033 // Create a series of compare/jumps.
Anton Kirilovedb2ac32016-11-30 15:14:10 +00009034 UseScratchRegisterScope temps(GetVIXLAssembler());
Artem Serov551b28f2016-10-18 19:11:30 +01009035 vixl32::Register temp_reg = temps.Acquire();
9036 // Note: It is fine for the below AddConstantSetFlags() using IP register to temporarily store
9037 // the immediate, because IP is used as the destination register. For the other
9038 // AddConstantSetFlags() and GenerateCompareWithImmediate(), the immediate values are constant,
9039 // and they can be encoded in the instruction without making use of IP register.
9040 __ Adds(temp_reg, value_reg, -lower_bound);
9041
9042 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
9043 // Jump to successors[0] if value == lower_bound.
9044 __ B(eq, codegen_->GetLabelOf(successors[0]));
9045 int32_t last_index = 0;
9046 for (; num_entries - last_index > 2; last_index += 2) {
9047 __ Adds(temp_reg, temp_reg, -2);
9048 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
9049 __ B(lo, codegen_->GetLabelOf(successors[last_index + 1]));
9050 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
9051 __ B(eq, codegen_->GetLabelOf(successors[last_index + 2]));
9052 }
9053 if (num_entries - last_index == 2) {
9054 // The last missing case_value.
9055 __ Cmp(temp_reg, 1);
9056 __ B(eq, codegen_->GetLabelOf(successors[last_index + 1]));
9057 }
9058
9059 // And the default for any other value.
9060 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
9061 __ B(codegen_->GetLabelOf(default_block));
9062 }
9063 } else {
9064 // Create a table lookup.
9065 vixl32::Register table_base = RegisterFrom(locations->GetTemp(0));
9066
9067 JumpTableARMVIXL* jump_table = codegen_->CreateJumpTable(switch_instr);
9068
9069 // Remove the bias.
9070 vixl32::Register key_reg;
9071 if (lower_bound != 0) {
9072 key_reg = RegisterFrom(locations->GetTemp(1));
9073 __ Sub(key_reg, value_reg, lower_bound);
9074 } else {
9075 key_reg = value_reg;
9076 }
9077
9078 // Check whether the value is in the table, jump to default block if not.
9079 __ Cmp(key_reg, num_entries - 1);
9080 __ B(hi, codegen_->GetLabelOf(default_block));
9081
Anton Kirilovedb2ac32016-11-30 15:14:10 +00009082 UseScratchRegisterScope temps(GetVIXLAssembler());
Artem Serov551b28f2016-10-18 19:11:30 +01009083 vixl32::Register jump_offset = temps.Acquire();
9084
9085 // Load jump offset from the table.
Scott Wakeling86e9d262017-01-18 15:59:24 +00009086 {
9087 const size_t jump_size = switch_instr->GetNumEntries() * sizeof(int32_t);
9088 ExactAssemblyScope aas(GetVIXLAssembler(),
9089 (vixl32::kMaxInstructionSizeInBytes * 4) + jump_size,
9090 CodeBufferCheckScope::kMaximumSize);
9091 __ adr(table_base, jump_table->GetTableStartLabel());
9092 __ ldr(jump_offset, MemOperand(table_base, key_reg, vixl32::LSL, 2));
Artem Serov551b28f2016-10-18 19:11:30 +01009093
Scott Wakeling86e9d262017-01-18 15:59:24 +00009094 // Jump to target block by branching to table_base(pc related) + offset.
9095 vixl32::Register target_address = table_base;
9096 __ add(target_address, table_base, jump_offset);
9097 __ bx(target_address);
Artem Serov09a940d2016-11-11 16:15:11 +00009098
Scott Wakeling86e9d262017-01-18 15:59:24 +00009099 jump_table->EmitTable(codegen_);
9100 }
Artem Serov551b28f2016-10-18 19:11:30 +01009101 }
9102}
Artem Serovd4cc5b22016-11-04 11:19:09 +00009103void LocationsBuilderARMVIXL::VisitArmDexCacheArraysBase(HArmDexCacheArraysBase* base) {
9104 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(base);
9105 locations->SetOut(Location::RequiresRegister());
9106}
9107
9108void InstructionCodeGeneratorARMVIXL::VisitArmDexCacheArraysBase(HArmDexCacheArraysBase* base) {
9109 vixl32::Register base_reg = OutputRegister(base);
9110 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
9111 codegen_->NewPcRelativeDexCacheArrayPatch(base->GetDexFile(), base->GetElementOffset());
9112 codegen_->EmitMovwMovtPlaceholder(labels, base_reg);
9113}
Artem Serov551b28f2016-10-18 19:11:30 +01009114
Artem Serov02d37832016-10-25 15:25:33 +01009115// Copy the result of a call into the given target.
Anton Kirilove28d9ae2016-10-25 18:17:23 +01009116void CodeGeneratorARMVIXL::MoveFromReturnRegister(Location trg, Primitive::Type type) {
9117 if (!trg.IsValid()) {
9118 DCHECK_EQ(type, Primitive::kPrimVoid);
9119 return;
9120 }
9121
9122 DCHECK_NE(type, Primitive::kPrimVoid);
9123
Artem Serovd4cc5b22016-11-04 11:19:09 +00009124 Location return_loc = InvokeDexCallingConventionVisitorARMVIXL().GetReturnLocation(type);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01009125 if (return_loc.Equals(trg)) {
9126 return;
9127 }
9128
9129 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
9130 // with the last branch.
9131 if (type == Primitive::kPrimLong) {
9132 TODO_VIXL32(FATAL);
9133 } else if (type == Primitive::kPrimDouble) {
9134 TODO_VIXL32(FATAL);
9135 } else {
9136 // Let the parallel move resolver take care of all of this.
9137 HParallelMove parallel_move(GetGraph()->GetArena());
9138 parallel_move.AddMove(return_loc, trg, type, nullptr);
9139 GetMoveResolver()->EmitNativeCode(&parallel_move);
9140 }
Scott Wakelingfe885462016-09-22 10:24:38 +01009141}
9142
xueliang.zhong8d2c4592016-11-23 17:05:25 +00009143void LocationsBuilderARMVIXL::VisitClassTableGet(HClassTableGet* instruction) {
9144 LocationSummary* locations =
9145 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
9146 locations->SetInAt(0, Location::RequiresRegister());
9147 locations->SetOut(Location::RequiresRegister());
Artem Serov551b28f2016-10-18 19:11:30 +01009148}
9149
xueliang.zhong8d2c4592016-11-23 17:05:25 +00009150void InstructionCodeGeneratorARMVIXL::VisitClassTableGet(HClassTableGet* instruction) {
9151 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
9152 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
9153 instruction->GetIndex(), kArmPointerSize).SizeValue();
9154 GetAssembler()->LoadFromOffset(kLoadWord,
9155 OutputRegister(instruction),
9156 InputRegisterAt(instruction, 0),
9157 method_offset);
9158 } else {
9159 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
9160 instruction->GetIndex(), kArmPointerSize));
9161 GetAssembler()->LoadFromOffset(kLoadWord,
9162 OutputRegister(instruction),
9163 InputRegisterAt(instruction, 0),
9164 mirror::Class::ImtPtrOffset(kArmPointerSize).Uint32Value());
9165 GetAssembler()->LoadFromOffset(kLoadWord,
9166 OutputRegister(instruction),
9167 OutputRegister(instruction),
9168 method_offset);
9169 }
Artem Serov551b28f2016-10-18 19:11:30 +01009170}
9171
Artem Serovc5fcb442016-12-02 19:19:58 +00009172static void PatchJitRootUse(uint8_t* code,
9173 const uint8_t* roots_data,
9174 VIXLUInt32Literal* literal,
9175 uint64_t index_in_table) {
9176 DCHECK(literal->IsBound());
9177 uint32_t literal_offset = literal->GetLocation();
9178 uintptr_t address =
9179 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
9180 uint8_t* data = code + literal_offset;
9181 reinterpret_cast<uint32_t*>(data)[0] = dchecked_integral_cast<uint32_t>(address);
9182}
9183
9184void CodeGeneratorARMVIXL::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
9185 for (const auto& entry : jit_string_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01009186 const StringReference& string_reference = entry.first;
9187 VIXLUInt32Literal* table_entry_literal = entry.second;
9188 const auto it = jit_string_roots_.find(string_reference);
Artem Serovc5fcb442016-12-02 19:19:58 +00009189 DCHECK(it != jit_string_roots_.end());
Vladimir Marko7d157fc2017-05-10 16:29:23 +01009190 uint64_t index_in_table = it->second;
9191 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Artem Serovc5fcb442016-12-02 19:19:58 +00009192 }
9193 for (const auto& entry : jit_class_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01009194 const TypeReference& type_reference = entry.first;
9195 VIXLUInt32Literal* table_entry_literal = entry.second;
9196 const auto it = jit_class_roots_.find(type_reference);
Artem Serovc5fcb442016-12-02 19:19:58 +00009197 DCHECK(it != jit_class_roots_.end());
Vladimir Marko7d157fc2017-05-10 16:29:23 +01009198 uint64_t index_in_table = it->second;
9199 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Artem Serovc5fcb442016-12-02 19:19:58 +00009200 }
9201}
9202
Artem Serovd4cc5b22016-11-04 11:19:09 +00009203void CodeGeneratorARMVIXL::EmitMovwMovtPlaceholder(
9204 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels,
9205 vixl32::Register out) {
Artem Serov0fb37192016-12-06 18:13:40 +00009206 ExactAssemblyScope aas(GetVIXLAssembler(),
9207 3 * vixl32::kMaxInstructionSizeInBytes,
9208 CodeBufferCheckScope::kMaximumSize);
Artem Serovd4cc5b22016-11-04 11:19:09 +00009209 // TODO(VIXL): Think about using mov instead of movw.
9210 __ bind(&labels->movw_label);
9211 __ movw(out, /* placeholder */ 0u);
9212 __ bind(&labels->movt_label);
9213 __ movt(out, /* placeholder */ 0u);
9214 __ bind(&labels->add_pc_label);
9215 __ add(out, out, pc);
9216}
9217
Scott Wakelingfe885462016-09-22 10:24:38 +01009218#undef __
9219#undef QUICK_ENTRY_POINT
9220#undef TODO_VIXL32
9221
9222} // namespace arm
9223} // namespace art