blob: 8b9495d56486c2d86812237a9d41916f70fb3d3f [file] [log] [blame]
Scott Wakelingfe885462016-09-22 10:24:38 +01001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_arm_vixl.h"
18
Vladimir Markoeee1c0e2017-04-21 17:58:41 +010019#include "arch/arm/asm_support_arm.h"
Scott Wakelingfe885462016-09-22 10:24:38 +010020#include "arch/arm/instruction_set_features_arm.h"
21#include "art_method.h"
Andreas Gampe5678db52017-06-08 14:11:18 -070022#include "base/bit_utils.h"
23#include "base/bit_utils_iterator.h"
Vladimir Marko94ec2db2017-09-06 17:21:03 +010024#include "class_table.h"
Scott Wakelingfe885462016-09-22 10:24:38 +010025#include "code_generator_utils.h"
26#include "common_arm.h"
27#include "compiled_method.h"
28#include "entrypoints/quick/quick_entrypoints.h"
29#include "gc/accounting/card_table.h"
Anton Kirilov5ec62182016-10-13 20:16:02 +010030#include "intrinsics_arm_vixl.h"
Vladimir Markoeee1c0e2017-04-21 17:58:41 +010031#include "linker/arm/relative_patcher_thumb2.h"
Scott Wakelingfe885462016-09-22 10:24:38 +010032#include "mirror/array-inl.h"
33#include "mirror/class-inl.h"
34#include "thread.h"
35#include "utils/arm/assembler_arm_vixl.h"
36#include "utils/arm/managed_register_arm.h"
37#include "utils/assembler.h"
38#include "utils/stack_checks.h"
39
40namespace art {
41namespace arm {
42
43namespace vixl32 = vixl::aarch32;
44using namespace vixl32; // NOLINT(build/namespaces)
45
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +010046using helpers::DRegisterFrom;
Scott Wakelingfe885462016-09-22 10:24:38 +010047using helpers::DWARFReg;
Scott Wakelinga7812ae2016-10-17 10:03:36 +010048using helpers::HighDRegisterFrom;
49using helpers::HighRegisterFrom;
Donghui Bai426b49c2016-11-08 14:55:38 +080050using helpers::InputDRegisterAt;
Scott Wakelingfe885462016-09-22 10:24:38 +010051using helpers::InputOperandAt;
Scott Wakelingc34dba72016-10-03 10:14:44 +010052using helpers::InputRegister;
Scott Wakelinga7812ae2016-10-17 10:03:36 +010053using helpers::InputRegisterAt;
Scott Wakelingfe885462016-09-22 10:24:38 +010054using helpers::InputSRegisterAt;
Anton Kirilov644032c2016-12-06 17:51:43 +000055using helpers::InputVRegister;
Scott Wakelinga7812ae2016-10-17 10:03:36 +010056using helpers::InputVRegisterAt;
Scott Wakelingb77051e2016-11-21 19:46:00 +000057using helpers::Int32ConstantFrom;
Anton Kirilov644032c2016-12-06 17:51:43 +000058using helpers::Int64ConstantFrom;
Scott Wakelinga7812ae2016-10-17 10:03:36 +010059using helpers::LocationFrom;
60using helpers::LowRegisterFrom;
61using helpers::LowSRegisterFrom;
Donghui Bai426b49c2016-11-08 14:55:38 +080062using helpers::OperandFrom;
Scott Wakelinga7812ae2016-10-17 10:03:36 +010063using helpers::OutputRegister;
64using helpers::OutputSRegister;
65using helpers::OutputVRegister;
66using helpers::RegisterFrom;
67using helpers::SRegisterFrom;
Anton Kirilov644032c2016-12-06 17:51:43 +000068using helpers::Uint64ConstantFrom;
Scott Wakelingfe885462016-09-22 10:24:38 +010069
Artem Serov0fb37192016-12-06 18:13:40 +000070using vixl::ExactAssemblyScope;
71using vixl::CodeBufferCheckScope;
72
Scott Wakelingfe885462016-09-22 10:24:38 +010073using RegisterList = vixl32::RegisterList;
74
75static bool ExpectedPairLayout(Location location) {
76 // We expected this for both core and fpu register pairs.
77 return ((location.low() & 1) == 0) && (location.low() + 1 == location.high());
78}
Artem Serovd4cc5b22016-11-04 11:19:09 +000079// Use a local definition to prevent copying mistakes.
80static constexpr size_t kArmWordSize = static_cast<size_t>(kArmPointerSize);
81static constexpr size_t kArmBitsPerWord = kArmWordSize * kBitsPerByte;
Artem Serov551b28f2016-10-18 19:11:30 +010082static constexpr uint32_t kPackedSwitchCompareJumpThreshold = 7;
Scott Wakelingfe885462016-09-22 10:24:38 +010083
Vladimir Markoeee1c0e2017-04-21 17:58:41 +010084// Reference load (except object array loads) is using LDR Rt, [Rn, #offset] which can handle
85// offset < 4KiB. For offsets >= 4KiB, the load shall be emitted as two or more instructions.
86// For the Baker read barrier implementation using link-generated thunks we need to split
87// the offset explicitly.
88constexpr uint32_t kReferenceLoadMinFarOffset = 4 * KB;
89
90// Flags controlling the use of link-time generated thunks for Baker read barriers.
91constexpr bool kBakerReadBarrierLinkTimeThunksEnableForFields = true;
92constexpr bool kBakerReadBarrierLinkTimeThunksEnableForArrays = true;
93constexpr bool kBakerReadBarrierLinkTimeThunksEnableForGcRoots = true;
94
95// The reserved entrypoint register for link-time generated thunks.
96const vixl32::Register kBakerCcEntrypointRegister = r4;
97
Roland Levillain5daa4952017-07-03 17:23:56 +010098// Using a base helps identify when we hit Marking Register check breakpoints.
99constexpr int kMarkingRegisterCheckBreakCodeBaseCode = 0x10;
100
Scott Wakelingfe885462016-09-22 10:24:38 +0100101#ifdef __
102#error "ARM Codegen VIXL macro-assembler macro already defined."
103#endif
104
Scott Wakelingfe885462016-09-22 10:24:38 +0100105// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
106#define __ down_cast<CodeGeneratorARMVIXL*>(codegen)->GetVIXLAssembler()-> // NOLINT
107#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kArmPointerSize, x).Int32Value()
108
109// Marker that code is yet to be, and must, be implemented.
110#define TODO_VIXL32(level) LOG(level) << __PRETTY_FUNCTION__ << " unimplemented "
111
Vladimir Markoeee1c0e2017-04-21 17:58:41 +0100112static inline void ExcludeIPAndBakerCcEntrypointRegister(UseScratchRegisterScope* temps,
113 HInstruction* instruction) {
114 DCHECK(temps->IsAvailable(ip));
115 temps->Exclude(ip);
116 DCHECK(!temps->IsAvailable(kBakerCcEntrypointRegister));
117 DCHECK_EQ(kBakerCcEntrypointRegister.GetCode(),
118 linker::Thumb2RelativePatcher::kBakerCcEntrypointRegister);
119 DCHECK_NE(instruction->GetLocations()->GetTempCount(), 0u);
120 DCHECK(RegisterFrom(instruction->GetLocations()->GetTemp(
121 instruction->GetLocations()->GetTempCount() - 1u)).Is(kBakerCcEntrypointRegister));
122}
123
124static inline void EmitPlaceholderBne(CodeGeneratorARMVIXL* codegen, vixl32::Label* patch_label) {
125 ExactAssemblyScope eas(codegen->GetVIXLAssembler(), kMaxInstructionSizeInBytes);
126 __ bind(patch_label);
127 vixl32::Label placeholder_label;
128 __ b(ne, EncodingSize(Wide), &placeholder_label); // Placeholder, patched at link-time.
129 __ bind(&placeholder_label);
130}
131
Vladimir Marko88abba22017-05-03 17:09:25 +0100132static inline bool CanEmitNarrowLdr(vixl32::Register rt, vixl32::Register rn, uint32_t offset) {
133 return rt.IsLow() && rn.IsLow() && offset < 32u;
134}
135
Vladimir Markoeee1c0e2017-04-21 17:58:41 +0100136class EmitAdrCode {
137 public:
138 EmitAdrCode(ArmVIXLMacroAssembler* assembler, vixl32::Register rd, vixl32::Label* label)
139 : assembler_(assembler), rd_(rd), label_(label) {
140 ExactAssemblyScope aas(assembler, kMaxInstructionSizeInBytes);
141 adr_location_ = assembler->GetCursorOffset();
142 assembler->adr(EncodingSize(Wide), rd, label);
143 }
144
145 ~EmitAdrCode() {
146 DCHECK(label_->IsBound());
147 // The ADR emitted by the assembler does not set the Thumb mode bit we need.
148 // TODO: Maybe extend VIXL to allow ADR for return address?
149 uint8_t* raw_adr = assembler_->GetBuffer()->GetOffsetAddress<uint8_t*>(adr_location_);
150 // Expecting ADR encoding T3 with `(offset & 1) == 0`.
151 DCHECK_EQ(raw_adr[1] & 0xfbu, 0xf2u); // Check bits 24-31, except 26.
152 DCHECK_EQ(raw_adr[0] & 0xffu, 0x0fu); // Check bits 16-23.
153 DCHECK_EQ(raw_adr[3] & 0x8fu, rd_.GetCode()); // Check bits 8-11 and 15.
154 DCHECK_EQ(raw_adr[2] & 0x01u, 0x00u); // Check bit 0, i.e. the `offset & 1`.
155 // Add the Thumb mode bit.
156 raw_adr[2] |= 0x01u;
157 }
158
159 private:
160 ArmVIXLMacroAssembler* const assembler_;
161 vixl32::Register rd_;
162 vixl32::Label* const label_;
163 int32_t adr_location_;
164};
165
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100166// SaveLiveRegisters and RestoreLiveRegisters from SlowPathCodeARM operate on sets of S registers,
167// for each live D registers they treat two corresponding S registers as live ones.
168//
169// Two following functions (SaveContiguousSRegisterList, RestoreContiguousSRegisterList) build
170// from a list of contiguous S registers a list of contiguous D registers (processing first/last
171// S registers corner cases) and save/restore this new list treating them as D registers.
172// - decreasing code size
173// - avoiding hazards on Cortex-A57, when a pair of S registers for an actual live D register is
174// restored and then used in regular non SlowPath code as D register.
175//
176// For the following example (v means the S register is live):
177// D names: | D0 | D1 | D2 | D4 | ...
178// S names: | S0 | S1 | S2 | S3 | S4 | S5 | S6 | S7 | ...
179// Live? | | v | v | v | v | v | v | | ...
180//
181// S1 and S6 will be saved/restored independently; D registers list (D1, D2) will be processed
182// as D registers.
183//
184// TODO(VIXL): All this code should be unnecessary once the VIXL AArch32 backend provides helpers
185// for lists of floating-point registers.
186static size_t SaveContiguousSRegisterList(size_t first,
187 size_t last,
188 CodeGenerator* codegen,
189 size_t stack_offset) {
190 static_assert(kSRegSizeInBytes == kArmWordSize, "Broken assumption on reg/word sizes.");
191 static_assert(kDRegSizeInBytes == 2 * kArmWordSize, "Broken assumption on reg/word sizes.");
192 DCHECK_LE(first, last);
193 if ((first == last) && (first == 0)) {
194 __ Vstr(vixl32::SRegister(first), MemOperand(sp, stack_offset));
195 return stack_offset + kSRegSizeInBytes;
196 }
197 if (first % 2 == 1) {
198 __ Vstr(vixl32::SRegister(first++), MemOperand(sp, stack_offset));
199 stack_offset += kSRegSizeInBytes;
200 }
201
202 bool save_last = false;
203 if (last % 2 == 0) {
204 save_last = true;
205 --last;
206 }
207
208 if (first < last) {
209 vixl32::DRegister d_reg = vixl32::DRegister(first / 2);
210 DCHECK_EQ((last - first + 1) % 2, 0u);
211 size_t number_of_d_regs = (last - first + 1) / 2;
212
213 if (number_of_d_regs == 1) {
214 __ Vstr(d_reg, MemOperand(sp, stack_offset));
215 } else if (number_of_d_regs > 1) {
216 UseScratchRegisterScope temps(down_cast<CodeGeneratorARMVIXL*>(codegen)->GetVIXLAssembler());
217 vixl32::Register base = sp;
218 if (stack_offset != 0) {
219 base = temps.Acquire();
Scott Wakelingb77051e2016-11-21 19:46:00 +0000220 __ Add(base, sp, Operand::From(stack_offset));
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100221 }
222 __ Vstm(F64, base, NO_WRITE_BACK, DRegisterList(d_reg, number_of_d_regs));
223 }
224 stack_offset += number_of_d_regs * kDRegSizeInBytes;
225 }
226
227 if (save_last) {
228 __ Vstr(vixl32::SRegister(last + 1), MemOperand(sp, stack_offset));
229 stack_offset += kSRegSizeInBytes;
230 }
231
232 return stack_offset;
233}
234
235static size_t RestoreContiguousSRegisterList(size_t first,
236 size_t last,
237 CodeGenerator* codegen,
238 size_t stack_offset) {
239 static_assert(kSRegSizeInBytes == kArmWordSize, "Broken assumption on reg/word sizes.");
240 static_assert(kDRegSizeInBytes == 2 * kArmWordSize, "Broken assumption on reg/word sizes.");
241 DCHECK_LE(first, last);
242 if ((first == last) && (first == 0)) {
243 __ Vldr(vixl32::SRegister(first), MemOperand(sp, stack_offset));
244 return stack_offset + kSRegSizeInBytes;
245 }
246 if (first % 2 == 1) {
247 __ Vldr(vixl32::SRegister(first++), MemOperand(sp, stack_offset));
248 stack_offset += kSRegSizeInBytes;
249 }
250
251 bool restore_last = false;
252 if (last % 2 == 0) {
253 restore_last = true;
254 --last;
255 }
256
257 if (first < last) {
258 vixl32::DRegister d_reg = vixl32::DRegister(first / 2);
259 DCHECK_EQ((last - first + 1) % 2, 0u);
260 size_t number_of_d_regs = (last - first + 1) / 2;
261 if (number_of_d_regs == 1) {
262 __ Vldr(d_reg, MemOperand(sp, stack_offset));
263 } else if (number_of_d_regs > 1) {
264 UseScratchRegisterScope temps(down_cast<CodeGeneratorARMVIXL*>(codegen)->GetVIXLAssembler());
265 vixl32::Register base = sp;
266 if (stack_offset != 0) {
267 base = temps.Acquire();
Scott Wakelingb77051e2016-11-21 19:46:00 +0000268 __ Add(base, sp, Operand::From(stack_offset));
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100269 }
270 __ Vldm(F64, base, NO_WRITE_BACK, DRegisterList(d_reg, number_of_d_regs));
271 }
272 stack_offset += number_of_d_regs * kDRegSizeInBytes;
273 }
274
275 if (restore_last) {
276 __ Vldr(vixl32::SRegister(last + 1), MemOperand(sp, stack_offset));
277 stack_offset += kSRegSizeInBytes;
278 }
279
280 return stack_offset;
281}
282
283void SlowPathCodeARMVIXL::SaveLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
284 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
285 size_t orig_offset = stack_offset;
286
287 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ true);
288 for (uint32_t i : LowToHighBits(core_spills)) {
289 // If the register holds an object, update the stack mask.
290 if (locations->RegisterContainsObject(i)) {
291 locations->SetStackBit(stack_offset / kVRegSize);
292 }
293 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
294 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
295 saved_core_stack_offsets_[i] = stack_offset;
296 stack_offset += kArmWordSize;
297 }
298
299 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
300 arm_codegen->GetAssembler()->StoreRegisterList(core_spills, orig_offset);
301
302 uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ false);
303 orig_offset = stack_offset;
304 for (uint32_t i : LowToHighBits(fp_spills)) {
305 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
306 saved_fpu_stack_offsets_[i] = stack_offset;
307 stack_offset += kArmWordSize;
308 }
309
310 stack_offset = orig_offset;
311 while (fp_spills != 0u) {
312 uint32_t begin = CTZ(fp_spills);
313 uint32_t tmp = fp_spills + (1u << begin);
314 fp_spills &= tmp; // Clear the contiguous range of 1s.
315 uint32_t end = (tmp == 0u) ? 32u : CTZ(tmp); // CTZ(0) is undefined.
316 stack_offset = SaveContiguousSRegisterList(begin, end - 1, codegen, stack_offset);
317 }
318 DCHECK_LE(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
319}
320
321void SlowPathCodeARMVIXL::RestoreLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
322 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
323 size_t orig_offset = stack_offset;
324
325 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ true);
326 for (uint32_t i : LowToHighBits(core_spills)) {
327 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
328 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
329 stack_offset += kArmWordSize;
330 }
331
332 // TODO(VIXL): Check the coherency of stack_offset after this with a test.
333 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
334 arm_codegen->GetAssembler()->LoadRegisterList(core_spills, orig_offset);
335
336 uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ false);
337 while (fp_spills != 0u) {
338 uint32_t begin = CTZ(fp_spills);
339 uint32_t tmp = fp_spills + (1u << begin);
340 fp_spills &= tmp; // Clear the contiguous range of 1s.
341 uint32_t end = (tmp == 0u) ? 32u : CTZ(tmp); // CTZ(0) is undefined.
342 stack_offset = RestoreContiguousSRegisterList(begin, end - 1, codegen, stack_offset);
343 }
344 DCHECK_LE(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
345}
346
347class NullCheckSlowPathARMVIXL : public SlowPathCodeARMVIXL {
348 public:
349 explicit NullCheckSlowPathARMVIXL(HNullCheck* instruction) : SlowPathCodeARMVIXL(instruction) {}
350
351 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
352 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
353 __ Bind(GetEntryLabel());
354 if (instruction_->CanThrowIntoCatchBlock()) {
355 // Live registers will be restored in the catch block if caught.
356 SaveLiveRegisters(codegen, instruction_->GetLocations());
357 }
358 arm_codegen->InvokeRuntime(kQuickThrowNullPointer,
359 instruction_,
360 instruction_->GetDexPc(),
361 this);
362 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
363 }
364
365 bool IsFatal() const OVERRIDE { return true; }
366
367 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathARMVIXL"; }
368
369 private:
370 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARMVIXL);
371};
372
Scott Wakelingfe885462016-09-22 10:24:38 +0100373class DivZeroCheckSlowPathARMVIXL : public SlowPathCodeARMVIXL {
374 public:
375 explicit DivZeroCheckSlowPathARMVIXL(HDivZeroCheck* instruction)
376 : SlowPathCodeARMVIXL(instruction) {}
377
378 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100379 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
Scott Wakelingfe885462016-09-22 10:24:38 +0100380 __ Bind(GetEntryLabel());
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100381 arm_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Scott Wakelingfe885462016-09-22 10:24:38 +0100382 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
383 }
384
385 bool IsFatal() const OVERRIDE { return true; }
386
387 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathARMVIXL"; }
388
389 private:
390 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARMVIXL);
391};
392
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100393class SuspendCheckSlowPathARMVIXL : public SlowPathCodeARMVIXL {
394 public:
395 SuspendCheckSlowPathARMVIXL(HSuspendCheck* instruction, HBasicBlock* successor)
396 : SlowPathCodeARMVIXL(instruction), successor_(successor) {}
397
398 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
399 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
400 __ Bind(GetEntryLabel());
401 arm_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
402 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
403 if (successor_ == nullptr) {
404 __ B(GetReturnLabel());
405 } else {
406 __ B(arm_codegen->GetLabelOf(successor_));
407 }
408 }
409
410 vixl32::Label* GetReturnLabel() {
411 DCHECK(successor_ == nullptr);
412 return &return_label_;
413 }
414
415 HBasicBlock* GetSuccessor() const {
416 return successor_;
417 }
418
419 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathARMVIXL"; }
420
421 private:
422 // If not null, the block to branch to after the suspend check.
423 HBasicBlock* const successor_;
424
425 // If `successor_` is null, the label to branch to after the suspend check.
426 vixl32::Label return_label_;
427
428 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARMVIXL);
429};
430
Scott Wakelingc34dba72016-10-03 10:14:44 +0100431class BoundsCheckSlowPathARMVIXL : public SlowPathCodeARMVIXL {
432 public:
433 explicit BoundsCheckSlowPathARMVIXL(HBoundsCheck* instruction)
434 : SlowPathCodeARMVIXL(instruction) {}
435
436 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
437 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
438 LocationSummary* locations = instruction_->GetLocations();
439
440 __ Bind(GetEntryLabel());
441 if (instruction_->CanThrowIntoCatchBlock()) {
442 // Live registers will be restored in the catch block if caught.
443 SaveLiveRegisters(codegen, instruction_->GetLocations());
444 }
445 // We're moving two locations to locations that could overlap, so we need a parallel
446 // move resolver.
447 InvokeRuntimeCallingConventionARMVIXL calling_convention;
448 codegen->EmitParallelMoves(
449 locations->InAt(0),
450 LocationFrom(calling_convention.GetRegisterAt(0)),
451 Primitive::kPrimInt,
452 locations->InAt(1),
453 LocationFrom(calling_convention.GetRegisterAt(1)),
454 Primitive::kPrimInt);
455 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
456 ? kQuickThrowStringBounds
457 : kQuickThrowArrayBounds;
458 arm_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
459 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
460 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
461 }
462
463 bool IsFatal() const OVERRIDE { return true; }
464
465 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathARMVIXL"; }
466
467 private:
468 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARMVIXL);
469};
470
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100471class LoadClassSlowPathARMVIXL : public SlowPathCodeARMVIXL {
472 public:
473 LoadClassSlowPathARMVIXL(HLoadClass* cls, HInstruction* at, uint32_t dex_pc, bool do_clinit)
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000474 : SlowPathCodeARMVIXL(at), cls_(cls), dex_pc_(dex_pc), do_clinit_(do_clinit) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100475 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
476 }
477
478 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000479 LocationSummary* locations = instruction_->GetLocations();
Vladimir Markoea4c1262017-02-06 19:59:33 +0000480 Location out = locations->Out();
481 constexpr bool call_saves_everything_except_r0 = (!kUseReadBarrier || kUseBakerReadBarrier);
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100482
483 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
484 __ Bind(GetEntryLabel());
485 SaveLiveRegisters(codegen, locations);
486
487 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Vladimir Markoea4c1262017-02-06 19:59:33 +0000488 // For HLoadClass/kBssEntry/kSaveEverything, make sure we preserve the address of the entry.
489 DCHECK_EQ(instruction_->IsLoadClass(), cls_ == instruction_);
490 bool is_load_class_bss_entry =
491 (cls_ == instruction_) && (cls_->GetLoadKind() == HLoadClass::LoadKind::kBssEntry);
492 vixl32::Register entry_address;
493 if (is_load_class_bss_entry && call_saves_everything_except_r0) {
494 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
495 // In the unlucky case that the `temp` is R0, we preserve the address in `out` across
496 // the kSaveEverything call.
497 bool temp_is_r0 = temp.Is(calling_convention.GetRegisterAt(0));
498 entry_address = temp_is_r0 ? RegisterFrom(out) : temp;
499 DCHECK(!entry_address.Is(calling_convention.GetRegisterAt(0)));
500 if (temp_is_r0) {
501 __ Mov(entry_address, temp);
502 }
503 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000504 dex::TypeIndex type_index = cls_->GetTypeIndex();
505 __ Mov(calling_convention.GetRegisterAt(0), type_index.index_);
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100506 QuickEntrypointEnum entrypoint = do_clinit_ ? kQuickInitializeStaticStorage
507 : kQuickInitializeType;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000508 arm_codegen->InvokeRuntime(entrypoint, instruction_, dex_pc_, this);
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100509 if (do_clinit_) {
510 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
511 } else {
512 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
513 }
514
Vladimir Markoea4c1262017-02-06 19:59:33 +0000515 // For HLoadClass/kBssEntry, store the resolved Class to the BSS entry.
516 if (is_load_class_bss_entry) {
517 if (call_saves_everything_except_r0) {
518 // The class entry address was preserved in `entry_address` thanks to kSaveEverything.
519 __ Str(r0, MemOperand(entry_address));
520 } else {
521 // For non-Baker read barrier, we need to re-calculate the address of the string entry.
522 UseScratchRegisterScope temps(
523 down_cast<CodeGeneratorARMVIXL*>(codegen)->GetVIXLAssembler());
524 vixl32::Register temp = temps.Acquire();
525 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
526 arm_codegen->NewTypeBssEntryPatch(cls_->GetDexFile(), type_index);
527 arm_codegen->EmitMovwMovtPlaceholder(labels, temp);
528 __ Str(r0, MemOperand(temp));
529 }
530 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100531 // Move the class to the desired location.
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100532 if (out.IsValid()) {
533 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
534 arm_codegen->Move32(locations->Out(), LocationFrom(r0));
535 }
536 RestoreLiveRegisters(codegen, locations);
537 __ B(GetExitLabel());
538 }
539
540 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathARMVIXL"; }
541
542 private:
543 // The class this slow path will load.
544 HLoadClass* const cls_;
545
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100546 // The dex PC of `at_`.
547 const uint32_t dex_pc_;
548
549 // Whether to initialize the class.
550 const bool do_clinit_;
551
552 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARMVIXL);
553};
554
Artem Serovd4cc5b22016-11-04 11:19:09 +0000555class LoadStringSlowPathARMVIXL : public SlowPathCodeARMVIXL {
556 public:
557 explicit LoadStringSlowPathARMVIXL(HLoadString* instruction)
558 : SlowPathCodeARMVIXL(instruction) {}
559
560 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Vladimir Markoea4c1262017-02-06 19:59:33 +0000561 DCHECK(instruction_->IsLoadString());
562 DCHECK_EQ(instruction_->AsLoadString()->GetLoadKind(), HLoadString::LoadKind::kBssEntry);
Artem Serovd4cc5b22016-11-04 11:19:09 +0000563 LocationSummary* locations = instruction_->GetLocations();
564 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
565 HLoadString* load = instruction_->AsLoadString();
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000566 const dex::StringIndex string_index = load->GetStringIndex();
Artem Serovd4cc5b22016-11-04 11:19:09 +0000567 vixl32::Register out = OutputRegister(load);
Artem Serovd4cc5b22016-11-04 11:19:09 +0000568 constexpr bool call_saves_everything_except_r0 = (!kUseReadBarrier || kUseBakerReadBarrier);
569
570 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
571 __ Bind(GetEntryLabel());
572 SaveLiveRegisters(codegen, locations);
573
574 InvokeRuntimeCallingConventionARMVIXL calling_convention;
575 // In the unlucky case that the `temp` is R0, we preserve the address in `out` across
Vladimir Markoea4c1262017-02-06 19:59:33 +0000576 // the kSaveEverything call.
577 vixl32::Register entry_address;
578 if (call_saves_everything_except_r0) {
579 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
580 bool temp_is_r0 = (temp.Is(calling_convention.GetRegisterAt(0)));
581 entry_address = temp_is_r0 ? out : temp;
582 DCHECK(!entry_address.Is(calling_convention.GetRegisterAt(0)));
583 if (temp_is_r0) {
584 __ Mov(entry_address, temp);
585 }
Artem Serovd4cc5b22016-11-04 11:19:09 +0000586 }
587
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000588 __ Mov(calling_convention.GetRegisterAt(0), string_index.index_);
Artem Serovd4cc5b22016-11-04 11:19:09 +0000589 arm_codegen->InvokeRuntime(kQuickResolveString, instruction_, instruction_->GetDexPc(), this);
590 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
591
592 // Store the resolved String to the .bss entry.
593 if (call_saves_everything_except_r0) {
594 // The string entry address was preserved in `entry_address` thanks to kSaveEverything.
595 __ Str(r0, MemOperand(entry_address));
596 } else {
597 // For non-Baker read barrier, we need to re-calculate the address of the string entry.
Vladimir Markoea4c1262017-02-06 19:59:33 +0000598 UseScratchRegisterScope temps(
599 down_cast<CodeGeneratorARMVIXL*>(codegen)->GetVIXLAssembler());
600 vixl32::Register temp = temps.Acquire();
Artem Serovd4cc5b22016-11-04 11:19:09 +0000601 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +0100602 arm_codegen->NewStringBssEntryPatch(load->GetDexFile(), string_index);
Vladimir Markoea4c1262017-02-06 19:59:33 +0000603 arm_codegen->EmitMovwMovtPlaceholder(labels, temp);
604 __ Str(r0, MemOperand(temp));
Artem Serovd4cc5b22016-11-04 11:19:09 +0000605 }
606
607 arm_codegen->Move32(locations->Out(), LocationFrom(r0));
608 RestoreLiveRegisters(codegen, locations);
609
610 __ B(GetExitLabel());
611 }
612
613 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathARMVIXL"; }
614
615 private:
616 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARMVIXL);
617};
618
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100619class TypeCheckSlowPathARMVIXL : public SlowPathCodeARMVIXL {
620 public:
621 TypeCheckSlowPathARMVIXL(HInstruction* instruction, bool is_fatal)
622 : SlowPathCodeARMVIXL(instruction), is_fatal_(is_fatal) {}
623
624 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
625 LocationSummary* locations = instruction_->GetLocations();
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100626 DCHECK(instruction_->IsCheckCast()
627 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
628
629 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
630 __ Bind(GetEntryLabel());
631
632 if (!is_fatal_) {
Artem Serovcfbe9132016-10-14 15:58:56 +0100633 SaveLiveRegisters(codegen, locations);
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100634 }
635
636 // We're moving two locations to locations that could overlap, so we need a parallel
637 // move resolver.
638 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100639
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800640 codegen->EmitParallelMoves(locations->InAt(0),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800641 LocationFrom(calling_convention.GetRegisterAt(0)),
642 Primitive::kPrimNot,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800643 locations->InAt(1),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800644 LocationFrom(calling_convention.GetRegisterAt(1)),
645 Primitive::kPrimNot);
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100646 if (instruction_->IsInstanceOf()) {
Artem Serovcfbe9132016-10-14 15:58:56 +0100647 arm_codegen->InvokeRuntime(kQuickInstanceofNonTrivial,
648 instruction_,
649 instruction_->GetDexPc(),
650 this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800651 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Artem Serovcfbe9132016-10-14 15:58:56 +0100652 arm_codegen->Move32(locations->Out(), LocationFrom(r0));
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100653 } else {
654 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800655 arm_codegen->InvokeRuntime(kQuickCheckInstanceOf,
656 instruction_,
657 instruction_->GetDexPc(),
658 this);
659 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100660 }
661
662 if (!is_fatal_) {
Artem Serovcfbe9132016-10-14 15:58:56 +0100663 RestoreLiveRegisters(codegen, locations);
664 __ B(GetExitLabel());
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100665 }
666 }
667
668 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathARMVIXL"; }
669
670 bool IsFatal() const OVERRIDE { return is_fatal_; }
671
672 private:
673 const bool is_fatal_;
674
675 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARMVIXL);
676};
677
Scott Wakelingc34dba72016-10-03 10:14:44 +0100678class DeoptimizationSlowPathARMVIXL : public SlowPathCodeARMVIXL {
679 public:
680 explicit DeoptimizationSlowPathARMVIXL(HDeoptimize* instruction)
681 : SlowPathCodeARMVIXL(instruction) {}
682
683 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
684 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
685 __ Bind(GetEntryLabel());
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100686 LocationSummary* locations = instruction_->GetLocations();
687 SaveLiveRegisters(codegen, locations);
688 InvokeRuntimeCallingConventionARMVIXL calling_convention;
689 __ Mov(calling_convention.GetRegisterAt(0),
690 static_cast<uint32_t>(instruction_->AsDeoptimize()->GetDeoptimizationKind()));
691
Scott Wakelingc34dba72016-10-03 10:14:44 +0100692 arm_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100693 CheckEntrypointTypes<kQuickDeoptimize, void, DeoptimizationKind>();
Scott Wakelingc34dba72016-10-03 10:14:44 +0100694 }
695
696 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathARMVIXL"; }
697
698 private:
699 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARMVIXL);
700};
701
702class ArraySetSlowPathARMVIXL : public SlowPathCodeARMVIXL {
703 public:
704 explicit ArraySetSlowPathARMVIXL(HInstruction* instruction) : SlowPathCodeARMVIXL(instruction) {}
705
706 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
707 LocationSummary* locations = instruction_->GetLocations();
708 __ Bind(GetEntryLabel());
709 SaveLiveRegisters(codegen, locations);
710
711 InvokeRuntimeCallingConventionARMVIXL calling_convention;
712 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
713 parallel_move.AddMove(
714 locations->InAt(0),
715 LocationFrom(calling_convention.GetRegisterAt(0)),
716 Primitive::kPrimNot,
717 nullptr);
718 parallel_move.AddMove(
719 locations->InAt(1),
720 LocationFrom(calling_convention.GetRegisterAt(1)),
721 Primitive::kPrimInt,
722 nullptr);
723 parallel_move.AddMove(
724 locations->InAt(2),
725 LocationFrom(calling_convention.GetRegisterAt(2)),
726 Primitive::kPrimNot,
727 nullptr);
728 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
729
730 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
731 arm_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
732 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
733 RestoreLiveRegisters(codegen, locations);
734 __ B(GetExitLabel());
735 }
736
737 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathARMVIXL"; }
738
739 private:
740 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathARMVIXL);
741};
742
Roland Levillain54f869e2017-03-06 13:54:11 +0000743// Abstract base class for read barrier slow paths marking a reference
744// `ref`.
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000745//
Roland Levillain54f869e2017-03-06 13:54:11 +0000746// Argument `entrypoint` must be a register location holding the read
Roland Levillain6d729a72017-06-30 18:34:01 +0100747// barrier marking runtime entry point to be invoked or an empty
748// location; in the latter case, the read barrier marking runtime
749// entry point will be loaded by the slow path code itself.
Roland Levillain54f869e2017-03-06 13:54:11 +0000750class ReadBarrierMarkSlowPathBaseARMVIXL : public SlowPathCodeARMVIXL {
751 protected:
752 ReadBarrierMarkSlowPathBaseARMVIXL(HInstruction* instruction, Location ref, Location entrypoint)
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000753 : SlowPathCodeARMVIXL(instruction), ref_(ref), entrypoint_(entrypoint) {
754 DCHECK(kEmitCompilerReadBarrier);
755 }
756
Roland Levillain54f869e2017-03-06 13:54:11 +0000757 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathBaseARMVIXL"; }
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000758
Roland Levillain54f869e2017-03-06 13:54:11 +0000759 // Generate assembly code calling the read barrier marking runtime
760 // entry point (ReadBarrierMarkRegX).
761 void GenerateReadBarrierMarkRuntimeCall(CodeGenerator* codegen) {
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000762 vixl32::Register ref_reg = RegisterFrom(ref_);
Roland Levillain47b3ab22017-02-27 14:31:35 +0000763
Roland Levillain47b3ab22017-02-27 14:31:35 +0000764 // No need to save live registers; it's taken care of by the
765 // entrypoint. Also, there is no need to update the stack mask,
766 // as this runtime call will not trigger a garbage collection.
767 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
768 DCHECK(!ref_reg.Is(sp));
769 DCHECK(!ref_reg.Is(lr));
770 DCHECK(!ref_reg.Is(pc));
771 // IP is used internally by the ReadBarrierMarkRegX entry point
772 // as a temporary, it cannot be the entry point's input/output.
773 DCHECK(!ref_reg.Is(ip));
774 DCHECK(ref_reg.IsRegister()) << ref_reg;
775 // "Compact" slow path, saving two moves.
776 //
777 // Instead of using the standard runtime calling convention (input
778 // and output in R0):
779 //
780 // R0 <- ref
781 // R0 <- ReadBarrierMark(R0)
782 // ref <- R0
783 //
784 // we just use rX (the register containing `ref`) as input and output
785 // of a dedicated entrypoint:
786 //
787 // rX <- ReadBarrierMarkRegX(rX)
788 //
789 if (entrypoint_.IsValid()) {
790 arm_codegen->ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction_, this);
791 __ Blx(RegisterFrom(entrypoint_));
792 } else {
Roland Levillain54f869e2017-03-06 13:54:11 +0000793 // Entrypoint is not already loaded, load from the thread.
Roland Levillain47b3ab22017-02-27 14:31:35 +0000794 int32_t entry_point_offset =
Roland Levillain97c46462017-05-11 14:04:03 +0100795 Thread::ReadBarrierMarkEntryPointsOffset<kArmPointerSize>(ref_reg.GetCode());
Roland Levillain47b3ab22017-02-27 14:31:35 +0000796 // This runtime call does not require a stack map.
797 arm_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
798 }
Roland Levillain47b3ab22017-02-27 14:31:35 +0000799 }
800
Roland Levillain47b3ab22017-02-27 14:31:35 +0000801 // The location (register) of the marked object reference.
802 const Location ref_;
803
804 // The location of the entrypoint if already loaded.
805 const Location entrypoint_;
806
Roland Levillain54f869e2017-03-06 13:54:11 +0000807 private:
808 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathBaseARMVIXL);
Roland Levillain47b3ab22017-02-27 14:31:35 +0000809};
810
Scott Wakelingc34dba72016-10-03 10:14:44 +0100811// Slow path marking an object reference `ref` during a read
812// barrier. The field `obj.field` in the object `obj` holding this
Roland Levillain54f869e2017-03-06 13:54:11 +0000813// reference does not get updated by this slow path after marking.
Roland Levillain47b3ab22017-02-27 14:31:35 +0000814//
Scott Wakelingc34dba72016-10-03 10:14:44 +0100815// This means that after the execution of this slow path, `ref` will
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000816// always be up-to-date, but `obj.field` may not; i.e., after the
817// flip, `ref` will be a to-space reference, but `obj.field` will
818// probably still be a from-space reference (unless it gets updated by
819// another thread, or if another thread installed another object
820// reference (different from `ref`) in `obj.field`).
Roland Levillainba650a42017-03-06 13:52:32 +0000821//
Roland Levillain6d729a72017-06-30 18:34:01 +0100822// Argument `entrypoint` must be a register location holding the read
823// barrier marking runtime entry point to be invoked or an empty
824// location; in the latter case, the read barrier marking runtime
825// entry point will be loaded by the slow path code itself.
Roland Levillain54f869e2017-03-06 13:54:11 +0000826class ReadBarrierMarkSlowPathARMVIXL : public ReadBarrierMarkSlowPathBaseARMVIXL {
Roland Levillain47b3ab22017-02-27 14:31:35 +0000827 public:
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000828 ReadBarrierMarkSlowPathARMVIXL(HInstruction* instruction,
829 Location ref,
830 Location entrypoint = Location::NoLocation())
Roland Levillain54f869e2017-03-06 13:54:11 +0000831 : ReadBarrierMarkSlowPathBaseARMVIXL(instruction, ref, entrypoint) {
Roland Levillain47b3ab22017-02-27 14:31:35 +0000832 DCHECK(kEmitCompilerReadBarrier);
833 }
834
Roland Levillain47b3ab22017-02-27 14:31:35 +0000835 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathARMVIXL"; }
836
837 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
838 LocationSummary* locations = instruction_->GetLocations();
Roland Levillain54f869e2017-03-06 13:54:11 +0000839 DCHECK(locations->CanCall());
840 DCHECK(ref_.IsRegister()) << ref_;
841 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_.reg())) << ref_.reg();
842 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
843 << "Unexpected instruction in read barrier marking slow path: "
844 << instruction_->DebugName();
845
846 __ Bind(GetEntryLabel());
847 GenerateReadBarrierMarkRuntimeCall(codegen);
848 __ B(GetExitLabel());
849 }
850
851 private:
852 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathARMVIXL);
853};
854
855// Slow path loading `obj`'s lock word, loading a reference from
856// object `*(obj + offset + (index << scale_factor))` into `ref`, and
857// marking `ref` if `obj` is gray according to the lock word (Baker
858// read barrier). The field `obj.field` in the object `obj` holding
859// this reference does not get updated by this slow path after marking
860// (see LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARMVIXL
861// below for that).
862//
863// This means that after the execution of this slow path, `ref` will
864// always be up-to-date, but `obj.field` may not; i.e., after the
865// flip, `ref` will be a to-space reference, but `obj.field` will
866// probably still be a from-space reference (unless it gets updated by
867// another thread, or if another thread installed another object
868// reference (different from `ref`) in `obj.field`).
869//
870// Argument `entrypoint` must be a register location holding the read
Roland Levillain6d729a72017-06-30 18:34:01 +0100871// barrier marking runtime entry point to be invoked or an empty
872// location; in the latter case, the read barrier marking runtime
873// entry point will be loaded by the slow path code itself.
Roland Levillain54f869e2017-03-06 13:54:11 +0000874class LoadReferenceWithBakerReadBarrierSlowPathARMVIXL : public ReadBarrierMarkSlowPathBaseARMVIXL {
875 public:
876 LoadReferenceWithBakerReadBarrierSlowPathARMVIXL(HInstruction* instruction,
877 Location ref,
878 vixl32::Register obj,
879 uint32_t offset,
880 Location index,
881 ScaleFactor scale_factor,
882 bool needs_null_check,
883 vixl32::Register temp,
Roland Levillain6d729a72017-06-30 18:34:01 +0100884 Location entrypoint = Location::NoLocation())
Roland Levillain54f869e2017-03-06 13:54:11 +0000885 : ReadBarrierMarkSlowPathBaseARMVIXL(instruction, ref, entrypoint),
886 obj_(obj),
887 offset_(offset),
888 index_(index),
889 scale_factor_(scale_factor),
890 needs_null_check_(needs_null_check),
891 temp_(temp) {
892 DCHECK(kEmitCompilerReadBarrier);
893 DCHECK(kUseBakerReadBarrier);
894 }
895
Roland Levillain47b3ab22017-02-27 14:31:35 +0000896 const char* GetDescription() const OVERRIDE {
Roland Levillain54f869e2017-03-06 13:54:11 +0000897 return "LoadReferenceWithBakerReadBarrierSlowPathARMVIXL";
Roland Levillain47b3ab22017-02-27 14:31:35 +0000898 }
899
900 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
901 LocationSummary* locations = instruction_->GetLocations();
902 vixl32::Register ref_reg = RegisterFrom(ref_);
903 DCHECK(locations->CanCall());
904 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg.GetCode())) << ref_reg;
Roland Levillain47b3ab22017-02-27 14:31:35 +0000905 DCHECK(instruction_->IsInstanceFieldGet() ||
906 instruction_->IsStaticFieldGet() ||
907 instruction_->IsArrayGet() ||
908 instruction_->IsArraySet() ||
Roland Levillain47b3ab22017-02-27 14:31:35 +0000909 instruction_->IsInstanceOf() ||
910 instruction_->IsCheckCast() ||
911 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
912 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
913 << "Unexpected instruction in read barrier marking slow path: "
914 << instruction_->DebugName();
915 // The read barrier instrumentation of object ArrayGet
916 // instructions does not support the HIntermediateAddress
917 // instruction.
918 DCHECK(!(instruction_->IsArrayGet() &&
919 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
920
Roland Levillain54f869e2017-03-06 13:54:11 +0000921 // Temporary register `temp_`, used to store the lock word, must
922 // not be IP, as we may use it to emit the reference load (in the
923 // call to GenerateRawReferenceLoad below), and we need the lock
924 // word to still be in `temp_` after the reference load.
925 DCHECK(!temp_.Is(ip));
926
Roland Levillain47b3ab22017-02-27 14:31:35 +0000927 __ Bind(GetEntryLabel());
Roland Levillain54f869e2017-03-06 13:54:11 +0000928
929 // When using MaybeGenerateReadBarrierSlow, the read barrier call is
930 // inserted after the original load. However, in fast path based
931 // Baker's read barriers, we need to perform the load of
932 // mirror::Object::monitor_ *before* the original reference load.
933 // This load-load ordering is required by the read barrier.
Roland Levillainff487002017-03-07 16:50:01 +0000934 // The slow path (for Baker's algorithm) should look like:
Roland Levillain54f869e2017-03-06 13:54:11 +0000935 //
936 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
937 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
938 // HeapReference<mirror::Object> ref = *src; // Original reference load.
939 // bool is_gray = (rb_state == ReadBarrier::GrayState());
940 // if (is_gray) {
941 // ref = entrypoint(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
942 // }
943 //
944 // Note: the original implementation in ReadBarrier::Barrier is
945 // slightly more complex as it performs additional checks that we do
946 // not do here for performance reasons.
947
Roland Levillain47b3ab22017-02-27 14:31:35 +0000948 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
Roland Levillain54f869e2017-03-06 13:54:11 +0000949
950 // /* int32_t */ monitor = obj->monitor_
951 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
952 arm_codegen->GetAssembler()->LoadFromOffset(kLoadWord, temp_, obj_, monitor_offset);
953 if (needs_null_check_) {
954 codegen->MaybeRecordImplicitNullCheck(instruction_);
Roland Levillain47b3ab22017-02-27 14:31:35 +0000955 }
Roland Levillain54f869e2017-03-06 13:54:11 +0000956 // /* LockWord */ lock_word = LockWord(monitor)
957 static_assert(sizeof(LockWord) == sizeof(int32_t),
958 "art::LockWord and int32_t have different sizes.");
959
960 // Introduce a dependency on the lock_word including the rb_state,
961 // which shall prevent load-load reordering without using
962 // a memory barrier (which would be more expensive).
963 // `obj` is unchanged by this operation, but its value now depends
964 // on `temp`.
965 __ Add(obj_, obj_, Operand(temp_, ShiftType::LSR, 32));
966
967 // The actual reference load.
968 // A possible implicit null check has already been handled above.
969 arm_codegen->GenerateRawReferenceLoad(
970 instruction_, ref_, obj_, offset_, index_, scale_factor_, /* needs_null_check */ false);
971
972 // Mark the object `ref` when `obj` is gray.
973 //
974 // if (rb_state == ReadBarrier::GrayState())
975 // ref = ReadBarrier::Mark(ref);
976 //
977 // Given the numeric representation, it's enough to check the low bit of the
978 // rb_state. We do that by shifting the bit out of the lock word with LSRS
979 // which can be a 16-bit instruction unlike the TST immediate.
980 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
981 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
982 __ Lsrs(temp_, temp_, LockWord::kReadBarrierStateShift + 1);
983 __ B(cc, GetExitLabel()); // Carry flag is the last bit shifted out by LSRS.
984 GenerateReadBarrierMarkRuntimeCall(codegen);
985
Roland Levillain47b3ab22017-02-27 14:31:35 +0000986 __ B(GetExitLabel());
987 }
988
989 private:
Roland Levillain54f869e2017-03-06 13:54:11 +0000990 // The register containing the object holding the marked object reference field.
991 vixl32::Register obj_;
992 // The offset, index and scale factor to access the reference in `obj_`.
993 uint32_t offset_;
994 Location index_;
995 ScaleFactor scale_factor_;
996 // Is a null check required?
997 bool needs_null_check_;
998 // A temporary register used to hold the lock word of `obj_`.
999 vixl32::Register temp_;
Roland Levillain47b3ab22017-02-27 14:31:35 +00001000
Roland Levillain54f869e2017-03-06 13:54:11 +00001001 DISALLOW_COPY_AND_ASSIGN(LoadReferenceWithBakerReadBarrierSlowPathARMVIXL);
Roland Levillain47b3ab22017-02-27 14:31:35 +00001002};
1003
Roland Levillain54f869e2017-03-06 13:54:11 +00001004// Slow path loading `obj`'s lock word, loading a reference from
1005// object `*(obj + offset + (index << scale_factor))` into `ref`, and
1006// marking `ref` if `obj` is gray according to the lock word (Baker
1007// read barrier). If needed, this slow path also atomically updates
1008// the field `obj.field` in the object `obj` holding this reference
1009// after marking (contrary to
1010// LoadReferenceWithBakerReadBarrierSlowPathARMVIXL above, which never
1011// tries to update `obj.field`).
Roland Levillain47b3ab22017-02-27 14:31:35 +00001012//
1013// This means that after the execution of this slow path, both `ref`
1014// and `obj.field` will be up-to-date; i.e., after the flip, both will
1015// hold the same to-space reference (unless another thread installed
1016// another object reference (different from `ref`) in `obj.field`).
Roland Levillainba650a42017-03-06 13:52:32 +00001017//
Roland Levillain54f869e2017-03-06 13:54:11 +00001018// Argument `entrypoint` must be a register location holding the read
Roland Levillain6d729a72017-06-30 18:34:01 +01001019// barrier marking runtime entry point to be invoked or an empty
1020// location; in the latter case, the read barrier marking runtime
1021// entry point will be loaded by the slow path code itself.
Roland Levillain54f869e2017-03-06 13:54:11 +00001022class LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARMVIXL
1023 : public ReadBarrierMarkSlowPathBaseARMVIXL {
Roland Levillain47b3ab22017-02-27 14:31:35 +00001024 public:
Roland Levillain6d729a72017-06-30 18:34:01 +01001025 LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARMVIXL(
1026 HInstruction* instruction,
1027 Location ref,
1028 vixl32::Register obj,
1029 uint32_t offset,
1030 Location index,
1031 ScaleFactor scale_factor,
1032 bool needs_null_check,
1033 vixl32::Register temp1,
1034 vixl32::Register temp2,
1035 Location entrypoint = Location::NoLocation())
Roland Levillain54f869e2017-03-06 13:54:11 +00001036 : ReadBarrierMarkSlowPathBaseARMVIXL(instruction, ref, entrypoint),
Roland Levillain47b3ab22017-02-27 14:31:35 +00001037 obj_(obj),
Roland Levillain54f869e2017-03-06 13:54:11 +00001038 offset_(offset),
1039 index_(index),
1040 scale_factor_(scale_factor),
1041 needs_null_check_(needs_null_check),
Roland Levillain47b3ab22017-02-27 14:31:35 +00001042 temp1_(temp1),
Roland Levillain54f869e2017-03-06 13:54:11 +00001043 temp2_(temp2) {
Roland Levillain47b3ab22017-02-27 14:31:35 +00001044 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain54f869e2017-03-06 13:54:11 +00001045 DCHECK(kUseBakerReadBarrier);
Roland Levillain47b3ab22017-02-27 14:31:35 +00001046 }
1047
1048 const char* GetDescription() const OVERRIDE {
Roland Levillain54f869e2017-03-06 13:54:11 +00001049 return "LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARMVIXL";
Roland Levillain47b3ab22017-02-27 14:31:35 +00001050 }
1051
1052 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
1053 LocationSummary* locations = instruction_->GetLocations();
1054 vixl32::Register ref_reg = RegisterFrom(ref_);
1055 DCHECK(locations->CanCall());
1056 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg.GetCode())) << ref_reg;
Roland Levillain54f869e2017-03-06 13:54:11 +00001057 DCHECK_NE(ref_.reg(), LocationFrom(temp1_).reg());
1058
1059 // This slow path is only used by the UnsafeCASObject intrinsic at the moment.
Roland Levillain47b3ab22017-02-27 14:31:35 +00001060 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
1061 << "Unexpected instruction in read barrier marking and field updating slow path: "
1062 << instruction_->DebugName();
1063 DCHECK(instruction_->GetLocations()->Intrinsified());
1064 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
Roland Levillain54f869e2017-03-06 13:54:11 +00001065 DCHECK_EQ(offset_, 0u);
1066 DCHECK_EQ(scale_factor_, ScaleFactor::TIMES_1);
1067 Location field_offset = index_;
1068 DCHECK(field_offset.IsRegisterPair()) << field_offset;
1069
1070 // Temporary register `temp1_`, used to store the lock word, must
1071 // not be IP, as we may use it to emit the reference load (in the
1072 // call to GenerateRawReferenceLoad below), and we need the lock
1073 // word to still be in `temp1_` after the reference load.
1074 DCHECK(!temp1_.Is(ip));
Roland Levillain47b3ab22017-02-27 14:31:35 +00001075
1076 __ Bind(GetEntryLabel());
1077
Roland Levillainff487002017-03-07 16:50:01 +00001078 // The implementation is similar to LoadReferenceWithBakerReadBarrierSlowPathARMVIXL's:
1079 //
1080 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
1081 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
1082 // HeapReference<mirror::Object> ref = *src; // Original reference load.
1083 // bool is_gray = (rb_state == ReadBarrier::GrayState());
1084 // if (is_gray) {
1085 // old_ref = ref;
1086 // ref = entrypoint(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
1087 // compareAndSwapObject(obj, field_offset, old_ref, ref);
1088 // }
1089
Roland Levillain54f869e2017-03-06 13:54:11 +00001090 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
1091
1092 // /* int32_t */ monitor = obj->monitor_
1093 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
1094 arm_codegen->GetAssembler()->LoadFromOffset(kLoadWord, temp1_, obj_, monitor_offset);
1095 if (needs_null_check_) {
1096 codegen->MaybeRecordImplicitNullCheck(instruction_);
1097 }
1098 // /* LockWord */ lock_word = LockWord(monitor)
1099 static_assert(sizeof(LockWord) == sizeof(int32_t),
1100 "art::LockWord and int32_t have different sizes.");
1101
1102 // Introduce a dependency on the lock_word including the rb_state,
1103 // which shall prevent load-load reordering without using
1104 // a memory barrier (which would be more expensive).
1105 // `obj` is unchanged by this operation, but its value now depends
1106 // on `temp`.
1107 __ Add(obj_, obj_, Operand(temp1_, ShiftType::LSR, 32));
1108
1109 // The actual reference load.
1110 // A possible implicit null check has already been handled above.
1111 arm_codegen->GenerateRawReferenceLoad(
1112 instruction_, ref_, obj_, offset_, index_, scale_factor_, /* needs_null_check */ false);
1113
1114 // Mark the object `ref` when `obj` is gray.
1115 //
1116 // if (rb_state == ReadBarrier::GrayState())
1117 // ref = ReadBarrier::Mark(ref);
1118 //
1119 // Given the numeric representation, it's enough to check the low bit of the
1120 // rb_state. We do that by shifting the bit out of the lock word with LSRS
1121 // which can be a 16-bit instruction unlike the TST immediate.
1122 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
1123 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
1124 __ Lsrs(temp1_, temp1_, LockWord::kReadBarrierStateShift + 1);
1125 __ B(cc, GetExitLabel()); // Carry flag is the last bit shifted out by LSRS.
1126
1127 // Save the old value of the reference before marking it.
Roland Levillain47b3ab22017-02-27 14:31:35 +00001128 // Note that we cannot use IP to save the old reference, as IP is
1129 // used internally by the ReadBarrierMarkRegX entry point, and we
1130 // need the old reference after the call to that entry point.
1131 DCHECK(!temp1_.Is(ip));
1132 __ Mov(temp1_, ref_reg);
Roland Levillain27b1f9c2017-01-17 16:56:34 +00001133
Roland Levillain54f869e2017-03-06 13:54:11 +00001134 GenerateReadBarrierMarkRuntimeCall(codegen);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001135
1136 // If the new reference is different from the old reference,
Roland Levillain54f869e2017-03-06 13:54:11 +00001137 // update the field in the holder (`*(obj_ + field_offset)`).
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001138 //
1139 // Note that this field could also hold a different object, if
1140 // another thread had concurrently changed it. In that case, the
1141 // LDREX/SUBS/ITNE sequence of instructions in the compare-and-set
1142 // (CAS) operation below would abort the CAS, leaving the field
1143 // as-is.
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001144 __ Cmp(temp1_, ref_reg);
Roland Levillain54f869e2017-03-06 13:54:11 +00001145 __ B(eq, GetExitLabel());
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001146
1147 // Update the the holder's field atomically. This may fail if
1148 // mutator updates before us, but it's OK. This is achieved
1149 // using a strong compare-and-set (CAS) operation with relaxed
1150 // memory synchronization ordering, where the expected value is
1151 // the old reference and the desired value is the new reference.
1152
1153 UseScratchRegisterScope temps(arm_codegen->GetVIXLAssembler());
1154 // Convenience aliases.
1155 vixl32::Register base = obj_;
1156 // The UnsafeCASObject intrinsic uses a register pair as field
1157 // offset ("long offset"), of which only the low part contains
1158 // data.
Roland Levillain54f869e2017-03-06 13:54:11 +00001159 vixl32::Register offset = LowRegisterFrom(field_offset);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001160 vixl32::Register expected = temp1_;
1161 vixl32::Register value = ref_reg;
1162 vixl32::Register tmp_ptr = temps.Acquire(); // Pointer to actual memory.
1163 vixl32::Register tmp = temp2_; // Value in memory.
1164
1165 __ Add(tmp_ptr, base, offset);
1166
1167 if (kPoisonHeapReferences) {
1168 arm_codegen->GetAssembler()->PoisonHeapReference(expected);
1169 if (value.Is(expected)) {
1170 // Do not poison `value`, as it is the same register as
1171 // `expected`, which has just been poisoned.
1172 } else {
1173 arm_codegen->GetAssembler()->PoisonHeapReference(value);
1174 }
1175 }
1176
1177 // do {
1178 // tmp = [r_ptr] - expected;
1179 // } while (tmp == 0 && failure([r_ptr] <- r_new_value));
1180
1181 vixl32::Label loop_head, exit_loop;
1182 __ Bind(&loop_head);
1183
1184 __ Ldrex(tmp, MemOperand(tmp_ptr));
1185
1186 __ Subs(tmp, tmp, expected);
1187
1188 {
Artem Serov0fb37192016-12-06 18:13:40 +00001189 ExactAssemblyScope aas(arm_codegen->GetVIXLAssembler(),
1190 2 * kMaxInstructionSizeInBytes,
1191 CodeBufferCheckScope::kMaximumSize);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001192
1193 __ it(ne);
1194 __ clrex(ne);
1195 }
1196
Artem Serov517d9f62016-12-12 15:51:15 +00001197 __ B(ne, &exit_loop, /* far_target */ false);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001198
1199 __ Strex(tmp, value, MemOperand(tmp_ptr));
1200 __ Cmp(tmp, 1);
Artem Serov517d9f62016-12-12 15:51:15 +00001201 __ B(eq, &loop_head, /* far_target */ false);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001202
1203 __ Bind(&exit_loop);
1204
1205 if (kPoisonHeapReferences) {
1206 arm_codegen->GetAssembler()->UnpoisonHeapReference(expected);
1207 if (value.Is(expected)) {
1208 // Do not unpoison `value`, as it is the same register as
1209 // `expected`, which has just been unpoisoned.
1210 } else {
1211 arm_codegen->GetAssembler()->UnpoisonHeapReference(value);
1212 }
1213 }
1214
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001215 __ B(GetExitLabel());
1216 }
1217
1218 private:
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001219 // The register containing the object holding the marked object reference field.
1220 const vixl32::Register obj_;
Roland Levillain54f869e2017-03-06 13:54:11 +00001221 // The offset, index and scale factor to access the reference in `obj_`.
1222 uint32_t offset_;
1223 Location index_;
1224 ScaleFactor scale_factor_;
1225 // Is a null check required?
1226 bool needs_null_check_;
1227 // A temporary register used to hold the lock word of `obj_`; and
1228 // also to hold the original reference value, when the reference is
1229 // marked.
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001230 const vixl32::Register temp1_;
Roland Levillain54f869e2017-03-06 13:54:11 +00001231 // A temporary register used in the implementation of the CAS, to
1232 // update the object's reference field.
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001233 const vixl32::Register temp2_;
1234
Roland Levillain54f869e2017-03-06 13:54:11 +00001235 DISALLOW_COPY_AND_ASSIGN(LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARMVIXL);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001236};
1237
1238// Slow path generating a read barrier for a heap reference.
1239class ReadBarrierForHeapReferenceSlowPathARMVIXL : public SlowPathCodeARMVIXL {
1240 public:
1241 ReadBarrierForHeapReferenceSlowPathARMVIXL(HInstruction* instruction,
1242 Location out,
1243 Location ref,
1244 Location obj,
1245 uint32_t offset,
1246 Location index)
1247 : SlowPathCodeARMVIXL(instruction),
1248 out_(out),
1249 ref_(ref),
1250 obj_(obj),
1251 offset_(offset),
1252 index_(index) {
1253 DCHECK(kEmitCompilerReadBarrier);
1254 // If `obj` is equal to `out` or `ref`, it means the initial object
1255 // has been overwritten by (or after) the heap object reference load
1256 // to be instrumented, e.g.:
1257 //
1258 // __ LoadFromOffset(kLoadWord, out, out, offset);
1259 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
1260 //
1261 // In that case, we have lost the information about the original
1262 // object, and the emitted read barrier cannot work properly.
1263 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
1264 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
1265 }
1266
1267 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
1268 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
1269 LocationSummary* locations = instruction_->GetLocations();
1270 vixl32::Register reg_out = RegisterFrom(out_);
1271 DCHECK(locations->CanCall());
1272 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out.GetCode()));
1273 DCHECK(instruction_->IsInstanceFieldGet() ||
1274 instruction_->IsStaticFieldGet() ||
1275 instruction_->IsArrayGet() ||
1276 instruction_->IsInstanceOf() ||
1277 instruction_->IsCheckCast() ||
Andreas Gamped9911ee2017-03-27 13:27:24 -07001278 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001279 << "Unexpected instruction in read barrier for heap reference slow path: "
1280 << instruction_->DebugName();
1281 // The read barrier instrumentation of object ArrayGet
1282 // instructions does not support the HIntermediateAddress
1283 // instruction.
1284 DCHECK(!(instruction_->IsArrayGet() &&
1285 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
1286
1287 __ Bind(GetEntryLabel());
1288 SaveLiveRegisters(codegen, locations);
1289
1290 // We may have to change the index's value, but as `index_` is a
1291 // constant member (like other "inputs" of this slow path),
1292 // introduce a copy of it, `index`.
1293 Location index = index_;
1294 if (index_.IsValid()) {
1295 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
1296 if (instruction_->IsArrayGet()) {
1297 // Compute the actual memory offset and store it in `index`.
1298 vixl32::Register index_reg = RegisterFrom(index_);
1299 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg.GetCode()));
1300 if (codegen->IsCoreCalleeSaveRegister(index_reg.GetCode())) {
1301 // We are about to change the value of `index_reg` (see the
Roland Levillain9983e302017-07-14 14:34:22 +01001302 // calls to art::arm::ArmVIXLMacroAssembler::Lsl and
1303 // art::arm::ArmVIXLMacroAssembler::Add below), but it has
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001304 // not been saved by the previous call to
1305 // art::SlowPathCode::SaveLiveRegisters, as it is a
1306 // callee-save register --
1307 // art::SlowPathCode::SaveLiveRegisters does not consider
1308 // callee-save registers, as it has been designed with the
1309 // assumption that callee-save registers are supposed to be
1310 // handled by the called function. So, as a callee-save
1311 // register, `index_reg` _would_ eventually be saved onto
1312 // the stack, but it would be too late: we would have
1313 // changed its value earlier. Therefore, we manually save
1314 // it here into another freely available register,
1315 // `free_reg`, chosen of course among the caller-save
1316 // registers (as a callee-save `free_reg` register would
1317 // exhibit the same problem).
1318 //
1319 // Note we could have requested a temporary register from
1320 // the register allocator instead; but we prefer not to, as
1321 // this is a slow path, and we know we can find a
1322 // caller-save register that is available.
1323 vixl32::Register free_reg = FindAvailableCallerSaveRegister(codegen);
1324 __ Mov(free_reg, index_reg);
1325 index_reg = free_reg;
1326 index = LocationFrom(index_reg);
1327 } else {
1328 // The initial register stored in `index_` has already been
1329 // saved in the call to art::SlowPathCode::SaveLiveRegisters
1330 // (as it is not a callee-save register), so we can freely
1331 // use it.
1332 }
1333 // Shifting the index value contained in `index_reg` by the scale
1334 // factor (2) cannot overflow in practice, as the runtime is
1335 // unable to allocate object arrays with a size larger than
1336 // 2^26 - 1 (that is, 2^28 - 4 bytes).
1337 __ Lsl(index_reg, index_reg, TIMES_4);
1338 static_assert(
1339 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
1340 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
1341 __ Add(index_reg, index_reg, offset_);
1342 } else {
1343 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
1344 // intrinsics, `index_` is not shifted by a scale factor of 2
1345 // (as in the case of ArrayGet), as it is actually an offset
1346 // to an object field within an object.
1347 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
1348 DCHECK(instruction_->GetLocations()->Intrinsified());
1349 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
1350 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
1351 << instruction_->AsInvoke()->GetIntrinsic();
1352 DCHECK_EQ(offset_, 0U);
1353 DCHECK(index_.IsRegisterPair());
1354 // UnsafeGet's offset location is a register pair, the low
1355 // part contains the correct offset.
1356 index = index_.ToLow();
1357 }
1358 }
1359
1360 // We're moving two or three locations to locations that could
1361 // overlap, so we need a parallel move resolver.
1362 InvokeRuntimeCallingConventionARMVIXL calling_convention;
1363 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
1364 parallel_move.AddMove(ref_,
1365 LocationFrom(calling_convention.GetRegisterAt(0)),
1366 Primitive::kPrimNot,
1367 nullptr);
1368 parallel_move.AddMove(obj_,
1369 LocationFrom(calling_convention.GetRegisterAt(1)),
1370 Primitive::kPrimNot,
1371 nullptr);
1372 if (index.IsValid()) {
1373 parallel_move.AddMove(index,
1374 LocationFrom(calling_convention.GetRegisterAt(2)),
1375 Primitive::kPrimInt,
1376 nullptr);
1377 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
1378 } else {
1379 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
1380 __ Mov(calling_convention.GetRegisterAt(2), offset_);
1381 }
1382 arm_codegen->InvokeRuntime(kQuickReadBarrierSlow, instruction_, instruction_->GetDexPc(), this);
1383 CheckEntrypointTypes<
1384 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
1385 arm_codegen->Move32(out_, LocationFrom(r0));
1386
1387 RestoreLiveRegisters(codegen, locations);
1388 __ B(GetExitLabel());
1389 }
1390
1391 const char* GetDescription() const OVERRIDE {
1392 return "ReadBarrierForHeapReferenceSlowPathARMVIXL";
1393 }
1394
1395 private:
1396 vixl32::Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
1397 uint32_t ref = RegisterFrom(ref_).GetCode();
1398 uint32_t obj = RegisterFrom(obj_).GetCode();
1399 for (uint32_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
1400 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
1401 return vixl32::Register(i);
1402 }
1403 }
1404 // We shall never fail to find a free caller-save register, as
1405 // there are more than two core caller-save registers on ARM
1406 // (meaning it is possible to find one which is different from
1407 // `ref` and `obj`).
1408 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
1409 LOG(FATAL) << "Could not find a free caller-save register";
1410 UNREACHABLE();
1411 }
1412
1413 const Location out_;
1414 const Location ref_;
1415 const Location obj_;
1416 const uint32_t offset_;
1417 // An additional location containing an index to an array.
1418 // Only used for HArrayGet and the UnsafeGetObject &
1419 // UnsafeGetObjectVolatile intrinsics.
1420 const Location index_;
1421
1422 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathARMVIXL);
1423};
1424
1425// Slow path generating a read barrier for a GC root.
1426class ReadBarrierForRootSlowPathARMVIXL : public SlowPathCodeARMVIXL {
1427 public:
1428 ReadBarrierForRootSlowPathARMVIXL(HInstruction* instruction, Location out, Location root)
1429 : SlowPathCodeARMVIXL(instruction), out_(out), root_(root) {
1430 DCHECK(kEmitCompilerReadBarrier);
1431 }
1432
1433 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
1434 LocationSummary* locations = instruction_->GetLocations();
1435 vixl32::Register reg_out = RegisterFrom(out_);
1436 DCHECK(locations->CanCall());
1437 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out.GetCode()));
1438 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
1439 << "Unexpected instruction in read barrier for GC root slow path: "
1440 << instruction_->DebugName();
1441
1442 __ Bind(GetEntryLabel());
1443 SaveLiveRegisters(codegen, locations);
1444
1445 InvokeRuntimeCallingConventionARMVIXL calling_convention;
1446 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
1447 arm_codegen->Move32(LocationFrom(calling_convention.GetRegisterAt(0)), root_);
1448 arm_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
1449 instruction_,
1450 instruction_->GetDexPc(),
1451 this);
1452 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
1453 arm_codegen->Move32(out_, LocationFrom(r0));
1454
1455 RestoreLiveRegisters(codegen, locations);
1456 __ B(GetExitLabel());
1457 }
1458
1459 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathARMVIXL"; }
1460
1461 private:
1462 const Location out_;
1463 const Location root_;
1464
1465 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathARMVIXL);
1466};
Scott Wakelingc34dba72016-10-03 10:14:44 +01001467
Scott Wakelingfe885462016-09-22 10:24:38 +01001468inline vixl32::Condition ARMCondition(IfCondition cond) {
1469 switch (cond) {
1470 case kCondEQ: return eq;
1471 case kCondNE: return ne;
1472 case kCondLT: return lt;
1473 case kCondLE: return le;
1474 case kCondGT: return gt;
1475 case kCondGE: return ge;
1476 case kCondB: return lo;
1477 case kCondBE: return ls;
1478 case kCondA: return hi;
1479 case kCondAE: return hs;
1480 }
1481 LOG(FATAL) << "Unreachable";
1482 UNREACHABLE();
1483}
1484
1485// Maps signed condition to unsigned condition.
1486inline vixl32::Condition ARMUnsignedCondition(IfCondition cond) {
1487 switch (cond) {
1488 case kCondEQ: return eq;
1489 case kCondNE: return ne;
1490 // Signed to unsigned.
1491 case kCondLT: return lo;
1492 case kCondLE: return ls;
1493 case kCondGT: return hi;
1494 case kCondGE: return hs;
1495 // Unsigned remain unchanged.
1496 case kCondB: return lo;
1497 case kCondBE: return ls;
1498 case kCondA: return hi;
1499 case kCondAE: return hs;
1500 }
1501 LOG(FATAL) << "Unreachable";
1502 UNREACHABLE();
1503}
1504
1505inline vixl32::Condition ARMFPCondition(IfCondition cond, bool gt_bias) {
1506 // The ARM condition codes can express all the necessary branches, see the
1507 // "Meaning (floating-point)" column in the table A8-1 of the ARMv7 reference manual.
1508 // There is no dex instruction or HIR that would need the missing conditions
1509 // "equal or unordered" or "not equal".
1510 switch (cond) {
1511 case kCondEQ: return eq;
1512 case kCondNE: return ne /* unordered */;
1513 case kCondLT: return gt_bias ? cc : lt /* unordered */;
1514 case kCondLE: return gt_bias ? ls : le /* unordered */;
1515 case kCondGT: return gt_bias ? hi /* unordered */ : gt;
1516 case kCondGE: return gt_bias ? cs /* unordered */ : ge;
1517 default:
1518 LOG(FATAL) << "UNREACHABLE";
1519 UNREACHABLE();
1520 }
1521}
1522
Anton Kirilov74234da2017-01-13 14:42:47 +00001523inline ShiftType ShiftFromOpKind(HDataProcWithShifterOp::OpKind op_kind) {
1524 switch (op_kind) {
1525 case HDataProcWithShifterOp::kASR: return ShiftType::ASR;
1526 case HDataProcWithShifterOp::kLSL: return ShiftType::LSL;
1527 case HDataProcWithShifterOp::kLSR: return ShiftType::LSR;
1528 default:
1529 LOG(FATAL) << "Unexpected op kind " << op_kind;
1530 UNREACHABLE();
1531 }
1532}
1533
Scott Wakelingfe885462016-09-22 10:24:38 +01001534void CodeGeneratorARMVIXL::DumpCoreRegister(std::ostream& stream, int reg) const {
1535 stream << vixl32::Register(reg);
1536}
1537
1538void CodeGeneratorARMVIXL::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
1539 stream << vixl32::SRegister(reg);
1540}
1541
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001542static uint32_t ComputeSRegisterListMask(const SRegisterList& regs) {
Scott Wakelingfe885462016-09-22 10:24:38 +01001543 uint32_t mask = 0;
1544 for (uint32_t i = regs.GetFirstSRegister().GetCode();
1545 i <= regs.GetLastSRegister().GetCode();
1546 ++i) {
1547 mask |= (1 << i);
1548 }
1549 return mask;
1550}
1551
Artem Serovd4cc5b22016-11-04 11:19:09 +00001552// Saves the register in the stack. Returns the size taken on stack.
1553size_t CodeGeneratorARMVIXL::SaveCoreRegister(size_t stack_index ATTRIBUTE_UNUSED,
1554 uint32_t reg_id ATTRIBUTE_UNUSED) {
1555 TODO_VIXL32(FATAL);
1556 return 0;
1557}
1558
1559// Restores the register from the stack. Returns the size taken on stack.
1560size_t CodeGeneratorARMVIXL::RestoreCoreRegister(size_t stack_index ATTRIBUTE_UNUSED,
1561 uint32_t reg_id ATTRIBUTE_UNUSED) {
1562 TODO_VIXL32(FATAL);
1563 return 0;
1564}
1565
1566size_t CodeGeneratorARMVIXL::SaveFloatingPointRegister(size_t stack_index ATTRIBUTE_UNUSED,
1567 uint32_t reg_id ATTRIBUTE_UNUSED) {
1568 TODO_VIXL32(FATAL);
1569 return 0;
1570}
1571
1572size_t CodeGeneratorARMVIXL::RestoreFloatingPointRegister(size_t stack_index ATTRIBUTE_UNUSED,
1573 uint32_t reg_id ATTRIBUTE_UNUSED) {
1574 TODO_VIXL32(FATAL);
1575 return 0;
Anton Kirilove28d9ae2016-10-25 18:17:23 +01001576}
1577
Anton Kirilov74234da2017-01-13 14:42:47 +00001578static void GenerateDataProcInstruction(HInstruction::InstructionKind kind,
1579 vixl32::Register out,
1580 vixl32::Register first,
1581 const Operand& second,
1582 CodeGeneratorARMVIXL* codegen) {
1583 if (second.IsImmediate() && second.GetImmediate() == 0) {
1584 const Operand in = kind == HInstruction::kAnd
1585 ? Operand(0)
1586 : Operand(first);
1587
1588 __ Mov(out, in);
1589 } else {
1590 switch (kind) {
1591 case HInstruction::kAdd:
1592 __ Add(out, first, second);
1593 break;
1594 case HInstruction::kAnd:
1595 __ And(out, first, second);
1596 break;
1597 case HInstruction::kOr:
1598 __ Orr(out, first, second);
1599 break;
1600 case HInstruction::kSub:
1601 __ Sub(out, first, second);
1602 break;
1603 case HInstruction::kXor:
1604 __ Eor(out, first, second);
1605 break;
1606 default:
1607 LOG(FATAL) << "Unexpected instruction kind: " << kind;
1608 UNREACHABLE();
1609 }
1610 }
1611}
1612
1613static void GenerateDataProc(HInstruction::InstructionKind kind,
1614 const Location& out,
1615 const Location& first,
1616 const Operand& second_lo,
1617 const Operand& second_hi,
1618 CodeGeneratorARMVIXL* codegen) {
1619 const vixl32::Register first_hi = HighRegisterFrom(first);
1620 const vixl32::Register first_lo = LowRegisterFrom(first);
1621 const vixl32::Register out_hi = HighRegisterFrom(out);
1622 const vixl32::Register out_lo = LowRegisterFrom(out);
1623
1624 if (kind == HInstruction::kAdd) {
1625 __ Adds(out_lo, first_lo, second_lo);
1626 __ Adc(out_hi, first_hi, second_hi);
1627 } else if (kind == HInstruction::kSub) {
1628 __ Subs(out_lo, first_lo, second_lo);
1629 __ Sbc(out_hi, first_hi, second_hi);
1630 } else {
1631 GenerateDataProcInstruction(kind, out_lo, first_lo, second_lo, codegen);
1632 GenerateDataProcInstruction(kind, out_hi, first_hi, second_hi, codegen);
1633 }
1634}
1635
1636static Operand GetShifterOperand(vixl32::Register rm, ShiftType shift, uint32_t shift_imm) {
1637 return shift_imm == 0 ? Operand(rm) : Operand(rm, shift, shift_imm);
1638}
1639
1640static void GenerateLongDataProc(HDataProcWithShifterOp* instruction,
1641 CodeGeneratorARMVIXL* codegen) {
1642 DCHECK_EQ(instruction->GetType(), Primitive::kPrimLong);
1643 DCHECK(HDataProcWithShifterOp::IsShiftOp(instruction->GetOpKind()));
1644
1645 const LocationSummary* const locations = instruction->GetLocations();
1646 const uint32_t shift_value = instruction->GetShiftAmount();
1647 const HInstruction::InstructionKind kind = instruction->GetInstrKind();
1648 const Location first = locations->InAt(0);
1649 const Location second = locations->InAt(1);
1650 const Location out = locations->Out();
1651 const vixl32::Register first_hi = HighRegisterFrom(first);
1652 const vixl32::Register first_lo = LowRegisterFrom(first);
1653 const vixl32::Register out_hi = HighRegisterFrom(out);
1654 const vixl32::Register out_lo = LowRegisterFrom(out);
1655 const vixl32::Register second_hi = HighRegisterFrom(second);
1656 const vixl32::Register second_lo = LowRegisterFrom(second);
1657 const ShiftType shift = ShiftFromOpKind(instruction->GetOpKind());
1658
1659 if (shift_value >= 32) {
1660 if (shift == ShiftType::LSL) {
1661 GenerateDataProcInstruction(kind,
1662 out_hi,
1663 first_hi,
1664 Operand(second_lo, ShiftType::LSL, shift_value - 32),
1665 codegen);
1666 GenerateDataProcInstruction(kind, out_lo, first_lo, 0, codegen);
1667 } else if (shift == ShiftType::ASR) {
1668 GenerateDataProc(kind,
1669 out,
1670 first,
1671 GetShifterOperand(second_hi, ShiftType::ASR, shift_value - 32),
1672 Operand(second_hi, ShiftType::ASR, 31),
1673 codegen);
1674 } else {
1675 DCHECK_EQ(shift, ShiftType::LSR);
1676 GenerateDataProc(kind,
1677 out,
1678 first,
1679 GetShifterOperand(second_hi, ShiftType::LSR, shift_value - 32),
1680 0,
1681 codegen);
1682 }
1683 } else {
1684 DCHECK_GT(shift_value, 1U);
1685 DCHECK_LT(shift_value, 32U);
1686
1687 UseScratchRegisterScope temps(codegen->GetVIXLAssembler());
1688
1689 if (shift == ShiftType::LSL) {
1690 // We are not doing this for HInstruction::kAdd because the output will require
1691 // Location::kOutputOverlap; not applicable to other cases.
1692 if (kind == HInstruction::kOr || kind == HInstruction::kXor) {
1693 GenerateDataProcInstruction(kind,
1694 out_hi,
1695 first_hi,
1696 Operand(second_hi, ShiftType::LSL, shift_value),
1697 codegen);
1698 GenerateDataProcInstruction(kind,
1699 out_hi,
1700 out_hi,
1701 Operand(second_lo, ShiftType::LSR, 32 - shift_value),
1702 codegen);
1703 GenerateDataProcInstruction(kind,
1704 out_lo,
1705 first_lo,
1706 Operand(second_lo, ShiftType::LSL, shift_value),
1707 codegen);
1708 } else {
1709 const vixl32::Register temp = temps.Acquire();
1710
1711 __ Lsl(temp, second_hi, shift_value);
1712 __ Orr(temp, temp, Operand(second_lo, ShiftType::LSR, 32 - shift_value));
1713 GenerateDataProc(kind,
1714 out,
1715 first,
1716 Operand(second_lo, ShiftType::LSL, shift_value),
1717 temp,
1718 codegen);
1719 }
1720 } else {
1721 DCHECK(shift == ShiftType::ASR || shift == ShiftType::LSR);
1722
1723 // We are not doing this for HInstruction::kAdd because the output will require
1724 // Location::kOutputOverlap; not applicable to other cases.
1725 if (kind == HInstruction::kOr || kind == HInstruction::kXor) {
1726 GenerateDataProcInstruction(kind,
1727 out_lo,
1728 first_lo,
1729 Operand(second_lo, ShiftType::LSR, shift_value),
1730 codegen);
1731 GenerateDataProcInstruction(kind,
1732 out_lo,
1733 out_lo,
1734 Operand(second_hi, ShiftType::LSL, 32 - shift_value),
1735 codegen);
1736 GenerateDataProcInstruction(kind,
1737 out_hi,
1738 first_hi,
1739 Operand(second_hi, shift, shift_value),
1740 codegen);
1741 } else {
1742 const vixl32::Register temp = temps.Acquire();
1743
1744 __ Lsr(temp, second_lo, shift_value);
1745 __ Orr(temp, temp, Operand(second_hi, ShiftType::LSL, 32 - shift_value));
1746 GenerateDataProc(kind,
1747 out,
1748 first,
1749 temp,
1750 Operand(second_hi, shift, shift_value),
1751 codegen);
1752 }
1753 }
1754 }
1755}
1756
Donghui Bai426b49c2016-11-08 14:55:38 +08001757static void GenerateVcmp(HInstruction* instruction, CodeGeneratorARMVIXL* codegen) {
1758 const Location rhs_loc = instruction->GetLocations()->InAt(1);
1759 if (rhs_loc.IsConstant()) {
1760 // 0.0 is the only immediate that can be encoded directly in
1761 // a VCMP instruction.
1762 //
1763 // Both the JLS (section 15.20.1) and the JVMS (section 6.5)
1764 // specify that in a floating-point comparison, positive zero
1765 // and negative zero are considered equal, so we can use the
1766 // literal 0.0 for both cases here.
1767 //
1768 // Note however that some methods (Float.equal, Float.compare,
1769 // Float.compareTo, Double.equal, Double.compare,
1770 // Double.compareTo, Math.max, Math.min, StrictMath.max,
1771 // StrictMath.min) consider 0.0 to be (strictly) greater than
1772 // -0.0. So if we ever translate calls to these methods into a
1773 // HCompare instruction, we must handle the -0.0 case with
1774 // care here.
1775 DCHECK(rhs_loc.GetConstant()->IsArithmeticZero());
1776
1777 const Primitive::Type type = instruction->InputAt(0)->GetType();
1778
1779 if (type == Primitive::kPrimFloat) {
1780 __ Vcmp(F32, InputSRegisterAt(instruction, 0), 0.0);
1781 } else {
1782 DCHECK_EQ(type, Primitive::kPrimDouble);
1783 __ Vcmp(F64, InputDRegisterAt(instruction, 0), 0.0);
1784 }
1785 } else {
1786 __ Vcmp(InputVRegisterAt(instruction, 0), InputVRegisterAt(instruction, 1));
1787 }
1788}
1789
Anton Kirilov5601d4e2017-05-11 19:33:50 +01001790static int64_t AdjustConstantForCondition(int64_t value,
1791 IfCondition* condition,
1792 IfCondition* opposite) {
1793 if (value == 1) {
1794 if (*condition == kCondB) {
1795 value = 0;
1796 *condition = kCondEQ;
1797 *opposite = kCondNE;
1798 } else if (*condition == kCondAE) {
1799 value = 0;
1800 *condition = kCondNE;
1801 *opposite = kCondEQ;
1802 }
1803 } else if (value == -1) {
1804 if (*condition == kCondGT) {
1805 value = 0;
1806 *condition = kCondGE;
1807 *opposite = kCondLT;
1808 } else if (*condition == kCondLE) {
1809 value = 0;
1810 *condition = kCondLT;
1811 *opposite = kCondGE;
1812 }
1813 }
1814
1815 return value;
1816}
1817
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001818static std::pair<vixl32::Condition, vixl32::Condition> GenerateLongTestConstant(
1819 HCondition* condition,
1820 bool invert,
1821 CodeGeneratorARMVIXL* codegen) {
Donghui Bai426b49c2016-11-08 14:55:38 +08001822 DCHECK_EQ(condition->GetLeft()->GetType(), Primitive::kPrimLong);
1823
1824 const LocationSummary* const locations = condition->GetLocations();
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001825 IfCondition cond = condition->GetCondition();
1826 IfCondition opposite = condition->GetOppositeCondition();
1827
1828 if (invert) {
1829 std::swap(cond, opposite);
1830 }
1831
1832 std::pair<vixl32::Condition, vixl32::Condition> ret(eq, ne);
Donghui Bai426b49c2016-11-08 14:55:38 +08001833 const Location left = locations->InAt(0);
1834 const Location right = locations->InAt(1);
1835
1836 DCHECK(right.IsConstant());
1837
1838 const vixl32::Register left_high = HighRegisterFrom(left);
1839 const vixl32::Register left_low = LowRegisterFrom(left);
Anton Kirilov5601d4e2017-05-11 19:33:50 +01001840 int64_t value = AdjustConstantForCondition(Int64ConstantFrom(right), &cond, &opposite);
1841 UseScratchRegisterScope temps(codegen->GetVIXLAssembler());
1842
1843 // Comparisons against 0 are common enough to deserve special attention.
1844 if (value == 0) {
1845 switch (cond) {
1846 case kCondNE:
1847 // x > 0 iff x != 0 when the comparison is unsigned.
1848 case kCondA:
1849 ret = std::make_pair(ne, eq);
1850 FALLTHROUGH_INTENDED;
1851 case kCondEQ:
1852 // x <= 0 iff x == 0 when the comparison is unsigned.
1853 case kCondBE:
1854 __ Orrs(temps.Acquire(), left_low, left_high);
1855 return ret;
1856 case kCondLT:
1857 case kCondGE:
1858 __ Cmp(left_high, 0);
1859 return std::make_pair(ARMCondition(cond), ARMCondition(opposite));
1860 // Trivially true or false.
1861 case kCondB:
1862 ret = std::make_pair(ne, eq);
1863 FALLTHROUGH_INTENDED;
1864 case kCondAE:
1865 __ Cmp(left_low, left_low);
1866 return ret;
1867 default:
1868 break;
1869 }
1870 }
Donghui Bai426b49c2016-11-08 14:55:38 +08001871
1872 switch (cond) {
1873 case kCondEQ:
1874 case kCondNE:
1875 case kCondB:
1876 case kCondBE:
1877 case kCondA:
1878 case kCondAE: {
Anton Kirilov23b752b2017-07-20 14:40:44 +01001879 const uint32_t value_low = Low32Bits(value);
1880 Operand operand_low(value_low);
1881
Donghui Bai426b49c2016-11-08 14:55:38 +08001882 __ Cmp(left_high, High32Bits(value));
1883
Anton Kirilov23b752b2017-07-20 14:40:44 +01001884 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
1885 // we must ensure that the operands corresponding to the least significant
1886 // halves of the inputs fit into a 16-bit CMP encoding.
1887 if (!left_low.IsLow() || !IsUint<8>(value_low)) {
1888 operand_low = Operand(temps.Acquire());
1889 __ Mov(LeaveFlags, operand_low.GetBaseRegister(), value_low);
1890 }
1891
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001892 // We use the scope because of the IT block that follows.
Donghui Bai426b49c2016-11-08 14:55:38 +08001893 ExactAssemblyScope guard(codegen->GetVIXLAssembler(),
1894 2 * vixl32::k16BitT32InstructionSizeInBytes,
1895 CodeBufferCheckScope::kExactSize);
1896
1897 __ it(eq);
Anton Kirilov23b752b2017-07-20 14:40:44 +01001898 __ cmp(eq, left_low, operand_low);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001899 ret = std::make_pair(ARMUnsignedCondition(cond), ARMUnsignedCondition(opposite));
Donghui Bai426b49c2016-11-08 14:55:38 +08001900 break;
1901 }
1902 case kCondLE:
1903 case kCondGT:
1904 // Trivially true or false.
1905 if (value == std::numeric_limits<int64_t>::max()) {
1906 __ Cmp(left_low, left_low);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001907 ret = cond == kCondLE ? std::make_pair(eq, ne) : std::make_pair(ne, eq);
Donghui Bai426b49c2016-11-08 14:55:38 +08001908 break;
1909 }
1910
1911 if (cond == kCondLE) {
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001912 DCHECK_EQ(opposite, kCondGT);
Donghui Bai426b49c2016-11-08 14:55:38 +08001913 cond = kCondLT;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001914 opposite = kCondGE;
Donghui Bai426b49c2016-11-08 14:55:38 +08001915 } else {
1916 DCHECK_EQ(cond, kCondGT);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001917 DCHECK_EQ(opposite, kCondLE);
Donghui Bai426b49c2016-11-08 14:55:38 +08001918 cond = kCondGE;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001919 opposite = kCondLT;
Donghui Bai426b49c2016-11-08 14:55:38 +08001920 }
1921
1922 value++;
1923 FALLTHROUGH_INTENDED;
1924 case kCondGE:
1925 case kCondLT: {
Donghui Bai426b49c2016-11-08 14:55:38 +08001926 __ Cmp(left_low, Low32Bits(value));
1927 __ Sbcs(temps.Acquire(), left_high, High32Bits(value));
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001928 ret = std::make_pair(ARMCondition(cond), ARMCondition(opposite));
Donghui Bai426b49c2016-11-08 14:55:38 +08001929 break;
1930 }
1931 default:
1932 LOG(FATAL) << "Unreachable";
1933 UNREACHABLE();
1934 }
1935
1936 return ret;
1937}
1938
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001939static std::pair<vixl32::Condition, vixl32::Condition> GenerateLongTest(
1940 HCondition* condition,
1941 bool invert,
1942 CodeGeneratorARMVIXL* codegen) {
Donghui Bai426b49c2016-11-08 14:55:38 +08001943 DCHECK_EQ(condition->GetLeft()->GetType(), Primitive::kPrimLong);
1944
1945 const LocationSummary* const locations = condition->GetLocations();
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001946 IfCondition cond = condition->GetCondition();
1947 IfCondition opposite = condition->GetOppositeCondition();
1948
1949 if (invert) {
1950 std::swap(cond, opposite);
1951 }
1952
1953 std::pair<vixl32::Condition, vixl32::Condition> ret(eq, ne);
Donghui Bai426b49c2016-11-08 14:55:38 +08001954 Location left = locations->InAt(0);
1955 Location right = locations->InAt(1);
1956
1957 DCHECK(right.IsRegisterPair());
1958
1959 switch (cond) {
1960 case kCondEQ:
1961 case kCondNE:
1962 case kCondB:
1963 case kCondBE:
1964 case kCondA:
1965 case kCondAE: {
1966 __ Cmp(HighRegisterFrom(left), HighRegisterFrom(right));
1967
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001968 // We use the scope because of the IT block that follows.
Donghui Bai426b49c2016-11-08 14:55:38 +08001969 ExactAssemblyScope guard(codegen->GetVIXLAssembler(),
1970 2 * vixl32::k16BitT32InstructionSizeInBytes,
1971 CodeBufferCheckScope::kExactSize);
1972
1973 __ it(eq);
1974 __ cmp(eq, LowRegisterFrom(left), LowRegisterFrom(right));
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001975 ret = std::make_pair(ARMUnsignedCondition(cond), ARMUnsignedCondition(opposite));
Donghui Bai426b49c2016-11-08 14:55:38 +08001976 break;
1977 }
1978 case kCondLE:
1979 case kCondGT:
1980 if (cond == kCondLE) {
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001981 DCHECK_EQ(opposite, kCondGT);
Donghui Bai426b49c2016-11-08 14:55:38 +08001982 cond = kCondGE;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001983 opposite = kCondLT;
Donghui Bai426b49c2016-11-08 14:55:38 +08001984 } else {
1985 DCHECK_EQ(cond, kCondGT);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001986 DCHECK_EQ(opposite, kCondLE);
Donghui Bai426b49c2016-11-08 14:55:38 +08001987 cond = kCondLT;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001988 opposite = kCondGE;
Donghui Bai426b49c2016-11-08 14:55:38 +08001989 }
1990
1991 std::swap(left, right);
1992 FALLTHROUGH_INTENDED;
1993 case kCondGE:
1994 case kCondLT: {
1995 UseScratchRegisterScope temps(codegen->GetVIXLAssembler());
1996
1997 __ Cmp(LowRegisterFrom(left), LowRegisterFrom(right));
1998 __ Sbcs(temps.Acquire(), HighRegisterFrom(left), HighRegisterFrom(right));
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001999 ret = std::make_pair(ARMCondition(cond), ARMCondition(opposite));
Donghui Bai426b49c2016-11-08 14:55:38 +08002000 break;
2001 }
2002 default:
2003 LOG(FATAL) << "Unreachable";
2004 UNREACHABLE();
2005 }
2006
2007 return ret;
2008}
2009
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002010static std::pair<vixl32::Condition, vixl32::Condition> GenerateTest(HCondition* condition,
2011 bool invert,
2012 CodeGeneratorARMVIXL* codegen) {
2013 const Primitive::Type type = condition->GetLeft()->GetType();
2014 IfCondition cond = condition->GetCondition();
2015 IfCondition opposite = condition->GetOppositeCondition();
2016 std::pair<vixl32::Condition, vixl32::Condition> ret(eq, ne);
Donghui Bai426b49c2016-11-08 14:55:38 +08002017
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002018 if (invert) {
2019 std::swap(cond, opposite);
2020 }
Donghui Bai426b49c2016-11-08 14:55:38 +08002021
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002022 if (type == Primitive::kPrimLong) {
2023 ret = condition->GetLocations()->InAt(1).IsConstant()
2024 ? GenerateLongTestConstant(condition, invert, codegen)
2025 : GenerateLongTest(condition, invert, codegen);
2026 } else if (Primitive::IsFloatingPointType(type)) {
2027 GenerateVcmp(condition, codegen);
2028 __ Vmrs(RegisterOrAPSR_nzcv(kPcCode), FPSCR);
2029 ret = std::make_pair(ARMFPCondition(cond, condition->IsGtBias()),
2030 ARMFPCondition(opposite, condition->IsGtBias()));
Donghui Bai426b49c2016-11-08 14:55:38 +08002031 } else {
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002032 DCHECK(Primitive::IsIntegralType(type) || type == Primitive::kPrimNot) << type;
2033 __ Cmp(InputRegisterAt(condition, 0), InputOperandAt(condition, 1));
2034 ret = std::make_pair(ARMCondition(cond), ARMCondition(opposite));
Donghui Bai426b49c2016-11-08 14:55:38 +08002035 }
2036
2037 return ret;
2038}
2039
Anton Kirilov5601d4e2017-05-11 19:33:50 +01002040static void GenerateConditionGeneric(HCondition* cond, CodeGeneratorARMVIXL* codegen) {
Anton Kirilov5601d4e2017-05-11 19:33:50 +01002041 const vixl32::Register out = OutputRegister(cond);
2042 const auto condition = GenerateTest(cond, false, codegen);
2043
2044 __ Mov(LeaveFlags, out, 0);
2045
2046 if (out.IsLow()) {
2047 // We use the scope because of the IT block that follows.
2048 ExactAssemblyScope guard(codegen->GetVIXLAssembler(),
2049 2 * vixl32::k16BitT32InstructionSizeInBytes,
2050 CodeBufferCheckScope::kExactSize);
2051
2052 __ it(condition.first);
2053 __ mov(condition.first, out, 1);
2054 } else {
2055 vixl32::Label done_label;
2056 vixl32::Label* const final_label = codegen->GetFinalLabel(cond, &done_label);
2057
2058 __ B(condition.second, final_label, /* far_target */ false);
2059 __ Mov(out, 1);
2060
2061 if (done_label.IsReferenced()) {
2062 __ Bind(&done_label);
2063 }
2064 }
2065}
2066
2067static void GenerateEqualLong(HCondition* cond, CodeGeneratorARMVIXL* codegen) {
2068 DCHECK_EQ(cond->GetLeft()->GetType(), Primitive::kPrimLong);
2069
2070 const LocationSummary* const locations = cond->GetLocations();
2071 IfCondition condition = cond->GetCondition();
2072 const vixl32::Register out = OutputRegister(cond);
2073 const Location left = locations->InAt(0);
2074 const Location right = locations->InAt(1);
2075 vixl32::Register left_high = HighRegisterFrom(left);
2076 vixl32::Register left_low = LowRegisterFrom(left);
2077 vixl32::Register temp;
2078 UseScratchRegisterScope temps(codegen->GetVIXLAssembler());
2079
2080 if (right.IsConstant()) {
2081 IfCondition opposite = cond->GetOppositeCondition();
2082 const int64_t value = AdjustConstantForCondition(Int64ConstantFrom(right),
2083 &condition,
2084 &opposite);
2085 Operand right_high = High32Bits(value);
2086 Operand right_low = Low32Bits(value);
2087
2088 // The output uses Location::kNoOutputOverlap.
2089 if (out.Is(left_high)) {
2090 std::swap(left_low, left_high);
2091 std::swap(right_low, right_high);
2092 }
2093
2094 __ Sub(out, left_low, right_low);
2095 temp = temps.Acquire();
2096 __ Sub(temp, left_high, right_high);
2097 } else {
2098 DCHECK(right.IsRegisterPair());
2099 temp = temps.Acquire();
2100 __ Sub(temp, left_high, HighRegisterFrom(right));
2101 __ Sub(out, left_low, LowRegisterFrom(right));
2102 }
2103
2104 // Need to check after calling AdjustConstantForCondition().
2105 DCHECK(condition == kCondEQ || condition == kCondNE) << condition;
2106
2107 if (condition == kCondNE && out.IsLow()) {
2108 __ Orrs(out, out, temp);
2109
2110 // We use the scope because of the IT block that follows.
2111 ExactAssemblyScope guard(codegen->GetVIXLAssembler(),
2112 2 * vixl32::k16BitT32InstructionSizeInBytes,
2113 CodeBufferCheckScope::kExactSize);
2114
2115 __ it(ne);
2116 __ mov(ne, out, 1);
2117 } else {
2118 __ Orr(out, out, temp);
2119 codegen->GenerateConditionWithZero(condition, out, out, temp);
2120 }
2121}
2122
Anton Kirilov5601d4e2017-05-11 19:33:50 +01002123static void GenerateConditionLong(HCondition* cond, CodeGeneratorARMVIXL* codegen) {
2124 DCHECK_EQ(cond->GetLeft()->GetType(), Primitive::kPrimLong);
2125
2126 const LocationSummary* const locations = cond->GetLocations();
2127 IfCondition condition = cond->GetCondition();
2128 const vixl32::Register out = OutputRegister(cond);
2129 const Location left = locations->InAt(0);
2130 const Location right = locations->InAt(1);
2131
2132 if (right.IsConstant()) {
2133 IfCondition opposite = cond->GetOppositeCondition();
2134
2135 // Comparisons against 0 are common enough to deserve special attention.
2136 if (AdjustConstantForCondition(Int64ConstantFrom(right), &condition, &opposite) == 0) {
2137 switch (condition) {
2138 case kCondNE:
2139 case kCondA:
2140 if (out.IsLow()) {
2141 // We only care if both input registers are 0 or not.
2142 __ Orrs(out, LowRegisterFrom(left), HighRegisterFrom(left));
2143
2144 // We use the scope because of the IT block that follows.
2145 ExactAssemblyScope guard(codegen->GetVIXLAssembler(),
2146 2 * vixl32::k16BitT32InstructionSizeInBytes,
2147 CodeBufferCheckScope::kExactSize);
2148
2149 __ it(ne);
2150 __ mov(ne, out, 1);
2151 return;
2152 }
2153
2154 FALLTHROUGH_INTENDED;
2155 case kCondEQ:
2156 case kCondBE:
2157 // We only care if both input registers are 0 or not.
2158 __ Orr(out, LowRegisterFrom(left), HighRegisterFrom(left));
2159 codegen->GenerateConditionWithZero(condition, out, out);
2160 return;
2161 case kCondLT:
2162 case kCondGE:
2163 // We only care about the sign bit.
2164 FALLTHROUGH_INTENDED;
2165 case kCondAE:
2166 case kCondB:
2167 codegen->GenerateConditionWithZero(condition, out, HighRegisterFrom(left));
2168 return;
2169 case kCondLE:
2170 case kCondGT:
2171 default:
2172 break;
2173 }
2174 }
2175 }
2176
Anton Kirilov23b752b2017-07-20 14:40:44 +01002177 // If `out` is a low register, then the GenerateConditionGeneric()
2178 // function generates a shorter code sequence that is still branchless.
2179 if ((condition == kCondEQ || condition == kCondNE) && !out.IsLow()) {
Anton Kirilov5601d4e2017-05-11 19:33:50 +01002180 GenerateEqualLong(cond, codegen);
2181 return;
2182 }
2183
Anton Kirilov23b752b2017-07-20 14:40:44 +01002184 GenerateConditionGeneric(cond, codegen);
Anton Kirilov5601d4e2017-05-11 19:33:50 +01002185}
2186
Roland Levillain6d729a72017-06-30 18:34:01 +01002187static void GenerateConditionIntegralOrNonPrimitive(HCondition* cond,
2188 CodeGeneratorARMVIXL* codegen) {
Anton Kirilov5601d4e2017-05-11 19:33:50 +01002189 const Primitive::Type type = cond->GetLeft()->GetType();
2190
2191 DCHECK(Primitive::IsIntegralType(type) || type == Primitive::kPrimNot) << type;
2192
2193 if (type == Primitive::kPrimLong) {
2194 GenerateConditionLong(cond, codegen);
2195 return;
2196 }
2197
2198 IfCondition condition = cond->GetCondition();
2199 vixl32::Register in = InputRegisterAt(cond, 0);
2200 const vixl32::Register out = OutputRegister(cond);
2201 const Location right = cond->GetLocations()->InAt(1);
2202 int64_t value;
2203
2204 if (right.IsConstant()) {
2205 IfCondition opposite = cond->GetOppositeCondition();
2206
2207 value = AdjustConstantForCondition(Int64ConstantFrom(right), &condition, &opposite);
2208
2209 // Comparisons against 0 are common enough to deserve special attention.
2210 if (value == 0) {
2211 switch (condition) {
2212 case kCondNE:
2213 case kCondA:
2214 if (out.IsLow() && out.Is(in)) {
2215 __ Cmp(out, 0);
2216
2217 // We use the scope because of the IT block that follows.
2218 ExactAssemblyScope guard(codegen->GetVIXLAssembler(),
2219 2 * vixl32::k16BitT32InstructionSizeInBytes,
2220 CodeBufferCheckScope::kExactSize);
2221
2222 __ it(ne);
2223 __ mov(ne, out, 1);
2224 return;
2225 }
2226
2227 FALLTHROUGH_INTENDED;
2228 case kCondEQ:
2229 case kCondBE:
2230 case kCondLT:
2231 case kCondGE:
2232 case kCondAE:
2233 case kCondB:
2234 codegen->GenerateConditionWithZero(condition, out, in);
2235 return;
2236 case kCondLE:
2237 case kCondGT:
2238 default:
2239 break;
2240 }
2241 }
2242 }
2243
2244 if (condition == kCondEQ || condition == kCondNE) {
2245 Operand operand(0);
2246
2247 if (right.IsConstant()) {
2248 operand = Operand::From(value);
2249 } else if (out.Is(RegisterFrom(right))) {
2250 // Avoid 32-bit instructions if possible.
2251 operand = InputOperandAt(cond, 0);
2252 in = RegisterFrom(right);
2253 } else {
2254 operand = InputOperandAt(cond, 1);
2255 }
2256
2257 if (condition == kCondNE && out.IsLow()) {
2258 __ Subs(out, in, operand);
2259
2260 // We use the scope because of the IT block that follows.
2261 ExactAssemblyScope guard(codegen->GetVIXLAssembler(),
2262 2 * vixl32::k16BitT32InstructionSizeInBytes,
2263 CodeBufferCheckScope::kExactSize);
2264
2265 __ it(ne);
2266 __ mov(ne, out, 1);
2267 } else {
2268 __ Sub(out, in, operand);
2269 codegen->GenerateConditionWithZero(condition, out, out);
2270 }
2271
2272 return;
2273 }
2274
2275 GenerateConditionGeneric(cond, codegen);
2276}
2277
Donghui Bai426b49c2016-11-08 14:55:38 +08002278static bool CanEncodeConstantAs8BitImmediate(HConstant* constant) {
2279 const Primitive::Type type = constant->GetType();
2280 bool ret = false;
2281
2282 DCHECK(Primitive::IsIntegralType(type) || type == Primitive::kPrimNot) << type;
2283
2284 if (type == Primitive::kPrimLong) {
2285 const uint64_t value = Uint64ConstantFrom(constant);
2286
2287 ret = IsUint<8>(Low32Bits(value)) && IsUint<8>(High32Bits(value));
2288 } else {
2289 ret = IsUint<8>(Int32ConstantFrom(constant));
2290 }
2291
2292 return ret;
2293}
2294
2295static Location Arm8BitEncodableConstantOrRegister(HInstruction* constant) {
2296 DCHECK(!Primitive::IsFloatingPointType(constant->GetType()));
2297
2298 if (constant->IsConstant() && CanEncodeConstantAs8BitImmediate(constant->AsConstant())) {
2299 return Location::ConstantLocation(constant->AsConstant());
2300 }
2301
2302 return Location::RequiresRegister();
2303}
2304
2305static bool CanGenerateConditionalMove(const Location& out, const Location& src) {
2306 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
2307 // we check that we are not dealing with floating-point output (there is no
2308 // 16-bit VMOV encoding).
2309 if (!out.IsRegister() && !out.IsRegisterPair()) {
2310 return false;
2311 }
2312
2313 // For constants, we also check that the output is in one or two low registers,
2314 // and that the constants fit in an 8-bit unsigned integer, so that a 16-bit
2315 // MOV encoding can be used.
2316 if (src.IsConstant()) {
2317 if (!CanEncodeConstantAs8BitImmediate(src.GetConstant())) {
2318 return false;
2319 }
2320
2321 if (out.IsRegister()) {
2322 if (!RegisterFrom(out).IsLow()) {
2323 return false;
2324 }
2325 } else {
2326 DCHECK(out.IsRegisterPair());
2327
2328 if (!HighRegisterFrom(out).IsLow()) {
2329 return false;
2330 }
2331 }
2332 }
2333
2334 return true;
2335}
2336
Scott Wakelingfe885462016-09-22 10:24:38 +01002337#undef __
2338
Donghui Bai426b49c2016-11-08 14:55:38 +08002339vixl32::Label* CodeGeneratorARMVIXL::GetFinalLabel(HInstruction* instruction,
2340 vixl32::Label* final_label) {
2341 DCHECK(!instruction->IsControlFlow() && !instruction->IsSuspendCheck());
Anton Kirilov6f644202017-02-27 18:29:45 +00002342 DCHECK(!instruction->IsInvoke() || !instruction->GetLocations()->CanCall());
Donghui Bai426b49c2016-11-08 14:55:38 +08002343
2344 const HBasicBlock* const block = instruction->GetBlock();
2345 const HLoopInformation* const info = block->GetLoopInformation();
2346 HInstruction* const next = instruction->GetNext();
2347
2348 // Avoid a branch to a branch.
2349 if (next->IsGoto() && (info == nullptr ||
2350 !info->IsBackEdge(*block) ||
2351 !info->HasSuspendCheck())) {
2352 final_label = GetLabelOf(next->AsGoto()->GetSuccessor());
2353 }
2354
2355 return final_label;
2356}
2357
Scott Wakelingfe885462016-09-22 10:24:38 +01002358CodeGeneratorARMVIXL::CodeGeneratorARMVIXL(HGraph* graph,
2359 const ArmInstructionSetFeatures& isa_features,
2360 const CompilerOptions& compiler_options,
2361 OptimizingCompilerStats* stats)
2362 : CodeGenerator(graph,
2363 kNumberOfCoreRegisters,
2364 kNumberOfSRegisters,
2365 kNumberOfRegisterPairs,
2366 kCoreCalleeSaves.GetList(),
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002367 ComputeSRegisterListMask(kFpuCalleeSaves),
Scott Wakelingfe885462016-09-22 10:24:38 +01002368 compiler_options,
2369 stats),
2370 block_labels_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Artem Serov551b28f2016-10-18 19:11:30 +01002371 jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Scott Wakelingfe885462016-09-22 10:24:38 +01002372 location_builder_(graph, this),
2373 instruction_visitor_(graph, this),
2374 move_resolver_(graph->GetArena(), this),
2375 assembler_(graph->GetArena()),
Artem Serovd4cc5b22016-11-04 11:19:09 +00002376 isa_features_(isa_features),
Artem Serovc5fcb442016-12-02 19:19:58 +00002377 uint32_literals_(std::less<uint32_t>(),
2378 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko65979462017-05-19 17:25:12 +01002379 pc_relative_method_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko0eb882b2017-05-15 13:39:18 +01002380 method_bss_entry_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Artem Serovc5fcb442016-12-02 19:19:58 +00002381 pc_relative_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko1998cd02017-01-13 13:02:58 +00002382 type_bss_entry_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko65979462017-05-19 17:25:12 +01002383 pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01002384 string_bss_entry_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01002385 baker_read_barrier_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Artem Serovc5fcb442016-12-02 19:19:58 +00002386 jit_string_patches_(StringReferenceValueComparator(),
2387 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
2388 jit_class_patches_(TypeReferenceValueComparator(),
2389 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Scott Wakelingfe885462016-09-22 10:24:38 +01002390 // Always save the LR register to mimic Quick.
2391 AddAllocatedRegister(Location::RegisterLocation(LR));
Nicolas Geoffray13a797b2017-03-15 16:41:31 +00002392 // Give D30 and D31 as scratch register to VIXL. The register allocator only works on
2393 // S0-S31, which alias to D0-D15.
2394 GetVIXLAssembler()->GetScratchVRegisterList()->Combine(d31);
2395 GetVIXLAssembler()->GetScratchVRegisterList()->Combine(d30);
Scott Wakelingfe885462016-09-22 10:24:38 +01002396}
2397
Artem Serov551b28f2016-10-18 19:11:30 +01002398void JumpTableARMVIXL::EmitTable(CodeGeneratorARMVIXL* codegen) {
2399 uint32_t num_entries = switch_instr_->GetNumEntries();
2400 DCHECK_GE(num_entries, kPackedSwitchCompareJumpThreshold);
2401
2402 // We are about to use the assembler to place literals directly. Make sure we have enough
Scott Wakelingb77051e2016-11-21 19:46:00 +00002403 // underlying code buffer and we have generated a jump table of the right size, using
2404 // codegen->GetVIXLAssembler()->GetBuffer().Align();
Artem Serov0fb37192016-12-06 18:13:40 +00002405 ExactAssemblyScope aas(codegen->GetVIXLAssembler(),
2406 num_entries * sizeof(int32_t),
2407 CodeBufferCheckScope::kMaximumSize);
Artem Serov551b28f2016-10-18 19:11:30 +01002408 // TODO(VIXL): Check that using lower case bind is fine here.
2409 codegen->GetVIXLAssembler()->bind(&table_start_);
Artem Serov09a940d2016-11-11 16:15:11 +00002410 for (uint32_t i = 0; i < num_entries; i++) {
2411 codegen->GetVIXLAssembler()->place(bb_addresses_[i].get());
2412 }
2413}
2414
2415void JumpTableARMVIXL::FixTable(CodeGeneratorARMVIXL* codegen) {
2416 uint32_t num_entries = switch_instr_->GetNumEntries();
2417 DCHECK_GE(num_entries, kPackedSwitchCompareJumpThreshold);
2418
Artem Serov551b28f2016-10-18 19:11:30 +01002419 const ArenaVector<HBasicBlock*>& successors = switch_instr_->GetBlock()->GetSuccessors();
2420 for (uint32_t i = 0; i < num_entries; i++) {
2421 vixl32::Label* target_label = codegen->GetLabelOf(successors[i]);
2422 DCHECK(target_label->IsBound());
2423 int32_t jump_offset = target_label->GetLocation() - table_start_.GetLocation();
2424 // When doing BX to address we need to have lower bit set to 1 in T32.
2425 if (codegen->GetVIXLAssembler()->IsUsingT32()) {
2426 jump_offset++;
2427 }
2428 DCHECK_GT(jump_offset, std::numeric_limits<int32_t>::min());
2429 DCHECK_LE(jump_offset, std::numeric_limits<int32_t>::max());
Artem Serov09a940d2016-11-11 16:15:11 +00002430
Scott Wakelingb77051e2016-11-21 19:46:00 +00002431 bb_addresses_[i].get()->UpdateValue(jump_offset, codegen->GetVIXLAssembler()->GetBuffer());
Artem Serov551b28f2016-10-18 19:11:30 +01002432 }
2433}
2434
Artem Serov09a940d2016-11-11 16:15:11 +00002435void CodeGeneratorARMVIXL::FixJumpTables() {
Artem Serov551b28f2016-10-18 19:11:30 +01002436 for (auto&& jump_table : jump_tables_) {
Artem Serov09a940d2016-11-11 16:15:11 +00002437 jump_table->FixTable(this);
Artem Serov551b28f2016-10-18 19:11:30 +01002438 }
2439}
2440
Andreas Gampeca620d72016-11-08 08:09:33 -08002441#define __ reinterpret_cast<ArmVIXLAssembler*>(GetAssembler())->GetVIXLAssembler()-> // NOLINT
Scott Wakelingfe885462016-09-22 10:24:38 +01002442
2443void CodeGeneratorARMVIXL::Finalize(CodeAllocator* allocator) {
Artem Serov09a940d2016-11-11 16:15:11 +00002444 FixJumpTables();
Scott Wakelingfe885462016-09-22 10:24:38 +01002445 GetAssembler()->FinalizeCode();
2446 CodeGenerator::Finalize(allocator);
2447}
2448
2449void CodeGeneratorARMVIXL::SetupBlockedRegisters() const {
Scott Wakelingfe885462016-09-22 10:24:38 +01002450 // Stack register, LR and PC are always reserved.
2451 blocked_core_registers_[SP] = true;
2452 blocked_core_registers_[LR] = true;
2453 blocked_core_registers_[PC] = true;
2454
Roland Levillain6d729a72017-06-30 18:34:01 +01002455 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2456 // Reserve marking register.
2457 blocked_core_registers_[MR] = true;
2458 }
2459
Scott Wakelingfe885462016-09-22 10:24:38 +01002460 // Reserve thread register.
2461 blocked_core_registers_[TR] = true;
2462
2463 // Reserve temp register.
2464 blocked_core_registers_[IP] = true;
2465
2466 if (GetGraph()->IsDebuggable()) {
2467 // Stubs do not save callee-save floating point registers. If the graph
2468 // is debuggable, we need to deal with these registers differently. For
2469 // now, just block them.
2470 for (uint32_t i = kFpuCalleeSaves.GetFirstSRegister().GetCode();
2471 i <= kFpuCalleeSaves.GetLastSRegister().GetCode();
2472 ++i) {
2473 blocked_fpu_registers_[i] = true;
2474 }
2475 }
Scott Wakelingfe885462016-09-22 10:24:38 +01002476}
2477
Scott Wakelingfe885462016-09-22 10:24:38 +01002478InstructionCodeGeneratorARMVIXL::InstructionCodeGeneratorARMVIXL(HGraph* graph,
2479 CodeGeneratorARMVIXL* codegen)
2480 : InstructionCodeGenerator(graph, codegen),
2481 assembler_(codegen->GetAssembler()),
2482 codegen_(codegen) {}
2483
2484void CodeGeneratorARMVIXL::ComputeSpillMask() {
2485 core_spill_mask_ = allocated_registers_.GetCoreRegisters() & core_callee_save_mask_;
2486 DCHECK_NE(core_spill_mask_, 0u) << "At least the return address register must be saved";
2487 // There is no easy instruction to restore just the PC on thumb2. We spill and
2488 // restore another arbitrary register.
2489 core_spill_mask_ |= (1 << kCoreAlwaysSpillRegister.GetCode());
2490 fpu_spill_mask_ = allocated_registers_.GetFloatingPointRegisters() & fpu_callee_save_mask_;
2491 // We use vpush and vpop for saving and restoring floating point registers, which take
2492 // a SRegister and the number of registers to save/restore after that SRegister. We
2493 // therefore update the `fpu_spill_mask_` to also contain those registers not allocated,
2494 // but in the range.
2495 if (fpu_spill_mask_ != 0) {
2496 uint32_t least_significant_bit = LeastSignificantBit(fpu_spill_mask_);
2497 uint32_t most_significant_bit = MostSignificantBit(fpu_spill_mask_);
2498 for (uint32_t i = least_significant_bit + 1 ; i < most_significant_bit; ++i) {
2499 fpu_spill_mask_ |= (1 << i);
2500 }
2501 }
2502}
2503
2504void CodeGeneratorARMVIXL::GenerateFrameEntry() {
2505 bool skip_overflow_check =
2506 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kArm);
2507 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
2508 __ Bind(&frame_entry_label_);
2509
2510 if (HasEmptyFrame()) {
2511 return;
2512 }
2513
Scott Wakelingfe885462016-09-22 10:24:38 +01002514 if (!skip_overflow_check) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002515 UseScratchRegisterScope temps(GetVIXLAssembler());
2516 vixl32::Register temp = temps.Acquire();
Anton Kirilov644032c2016-12-06 17:51:43 +00002517 __ Sub(temp, sp, Operand::From(GetStackOverflowReservedBytes(kArm)));
Scott Wakelingfe885462016-09-22 10:24:38 +01002518 // The load must immediately precede RecordPcInfo.
Artem Serov0fb37192016-12-06 18:13:40 +00002519 ExactAssemblyScope aas(GetVIXLAssembler(),
2520 vixl32::kMaxInstructionSizeInBytes,
2521 CodeBufferCheckScope::kMaximumSize);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002522 __ ldr(temp, MemOperand(temp));
2523 RecordPcInfo(nullptr, 0);
Scott Wakelingfe885462016-09-22 10:24:38 +01002524 }
2525
2526 __ Push(RegisterList(core_spill_mask_));
2527 GetAssembler()->cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(core_spill_mask_));
2528 GetAssembler()->cfi().RelOffsetForMany(DWARFReg(kMethodRegister),
2529 0,
2530 core_spill_mask_,
2531 kArmWordSize);
2532 if (fpu_spill_mask_ != 0) {
2533 uint32_t first = LeastSignificantBit(fpu_spill_mask_);
2534
2535 // Check that list is contiguous.
2536 DCHECK_EQ(fpu_spill_mask_ >> CTZ(fpu_spill_mask_), ~0u >> (32 - POPCOUNT(fpu_spill_mask_)));
2537
2538 __ Vpush(SRegisterList(vixl32::SRegister(first), POPCOUNT(fpu_spill_mask_)));
2539 GetAssembler()->cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(fpu_spill_mask_));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002540 GetAssembler()->cfi().RelOffsetForMany(DWARFReg(s0), 0, fpu_spill_mask_, kArmWordSize);
Scott Wakelingfe885462016-09-22 10:24:38 +01002541 }
Scott Wakelingbffdc702016-12-07 17:46:03 +00002542
Scott Wakelingfe885462016-09-22 10:24:38 +01002543 int adjust = GetFrameSize() - FrameEntrySpillSize();
2544 __ Sub(sp, sp, adjust);
2545 GetAssembler()->cfi().AdjustCFAOffset(adjust);
Scott Wakelingbffdc702016-12-07 17:46:03 +00002546
2547 // Save the current method if we need it. Note that we do not
2548 // do this in HCurrentMethod, as the instruction might have been removed
2549 // in the SSA graph.
2550 if (RequiresCurrentMethod()) {
2551 GetAssembler()->StoreToOffset(kStoreWord, kMethodRegister, sp, 0);
2552 }
Nicolas Geoffrayf7893532017-06-15 12:34:36 +01002553
2554 if (GetGraph()->HasShouldDeoptimizeFlag()) {
2555 UseScratchRegisterScope temps(GetVIXLAssembler());
2556 vixl32::Register temp = temps.Acquire();
2557 // Initialize should_deoptimize flag to 0.
2558 __ Mov(temp, 0);
2559 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, GetStackOffsetOfShouldDeoptimizeFlag());
2560 }
Roland Levillain5daa4952017-07-03 17:23:56 +01002561
2562 MaybeGenerateMarkingRegisterCheck(/* code */ 1);
Scott Wakelingfe885462016-09-22 10:24:38 +01002563}
2564
2565void CodeGeneratorARMVIXL::GenerateFrameExit() {
2566 if (HasEmptyFrame()) {
2567 __ Bx(lr);
2568 return;
2569 }
2570 GetAssembler()->cfi().RememberState();
2571 int adjust = GetFrameSize() - FrameEntrySpillSize();
2572 __ Add(sp, sp, adjust);
2573 GetAssembler()->cfi().AdjustCFAOffset(-adjust);
2574 if (fpu_spill_mask_ != 0) {
2575 uint32_t first = LeastSignificantBit(fpu_spill_mask_);
2576
2577 // Check that list is contiguous.
2578 DCHECK_EQ(fpu_spill_mask_ >> CTZ(fpu_spill_mask_), ~0u >> (32 - POPCOUNT(fpu_spill_mask_)));
2579
2580 __ Vpop(SRegisterList(vixl32::SRegister(first), POPCOUNT(fpu_spill_mask_)));
2581 GetAssembler()->cfi().AdjustCFAOffset(
2582 -static_cast<int>(kArmWordSize) * POPCOUNT(fpu_spill_mask_));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002583 GetAssembler()->cfi().RestoreMany(DWARFReg(vixl32::SRegister(0)), fpu_spill_mask_);
Scott Wakelingfe885462016-09-22 10:24:38 +01002584 }
2585 // Pop LR into PC to return.
2586 DCHECK_NE(core_spill_mask_ & (1 << kLrCode), 0U);
2587 uint32_t pop_mask = (core_spill_mask_ & (~(1 << kLrCode))) | 1 << kPcCode;
2588 __ Pop(RegisterList(pop_mask));
2589 GetAssembler()->cfi().RestoreState();
2590 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
2591}
2592
2593void CodeGeneratorARMVIXL::Bind(HBasicBlock* block) {
2594 __ Bind(GetLabelOf(block));
2595}
2596
Artem Serovd4cc5b22016-11-04 11:19:09 +00002597Location InvokeDexCallingConventionVisitorARMVIXL::GetNextLocation(Primitive::Type type) {
2598 switch (type) {
2599 case Primitive::kPrimBoolean:
2600 case Primitive::kPrimByte:
2601 case Primitive::kPrimChar:
2602 case Primitive::kPrimShort:
2603 case Primitive::kPrimInt:
2604 case Primitive::kPrimNot: {
2605 uint32_t index = gp_index_++;
2606 uint32_t stack_index = stack_index_++;
2607 if (index < calling_convention.GetNumberOfRegisters()) {
2608 return LocationFrom(calling_convention.GetRegisterAt(index));
2609 } else {
2610 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
2611 }
2612 }
2613
2614 case Primitive::kPrimLong: {
2615 uint32_t index = gp_index_;
2616 uint32_t stack_index = stack_index_;
2617 gp_index_ += 2;
2618 stack_index_ += 2;
2619 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
2620 if (calling_convention.GetRegisterAt(index).Is(r1)) {
2621 // Skip R1, and use R2_R3 instead.
2622 gp_index_++;
2623 index++;
2624 }
2625 }
2626 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
2627 DCHECK_EQ(calling_convention.GetRegisterAt(index).GetCode() + 1,
2628 calling_convention.GetRegisterAt(index + 1).GetCode());
2629
2630 return LocationFrom(calling_convention.GetRegisterAt(index),
2631 calling_convention.GetRegisterAt(index + 1));
2632 } else {
2633 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
2634 }
2635 }
2636
2637 case Primitive::kPrimFloat: {
2638 uint32_t stack_index = stack_index_++;
2639 if (float_index_ % 2 == 0) {
2640 float_index_ = std::max(double_index_, float_index_);
2641 }
2642 if (float_index_ < calling_convention.GetNumberOfFpuRegisters()) {
2643 return LocationFrom(calling_convention.GetFpuRegisterAt(float_index_++));
2644 } else {
2645 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
2646 }
2647 }
2648
2649 case Primitive::kPrimDouble: {
2650 double_index_ = std::max(double_index_, RoundUp(float_index_, 2));
2651 uint32_t stack_index = stack_index_;
2652 stack_index_ += 2;
2653 if (double_index_ + 1 < calling_convention.GetNumberOfFpuRegisters()) {
2654 uint32_t index = double_index_;
2655 double_index_ += 2;
2656 Location result = LocationFrom(
2657 calling_convention.GetFpuRegisterAt(index),
2658 calling_convention.GetFpuRegisterAt(index + 1));
2659 DCHECK(ExpectedPairLayout(result));
2660 return result;
2661 } else {
2662 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
2663 }
2664 }
2665
2666 case Primitive::kPrimVoid:
2667 LOG(FATAL) << "Unexpected parameter type " << type;
2668 break;
2669 }
2670 return Location::NoLocation();
2671}
2672
2673Location InvokeDexCallingConventionVisitorARMVIXL::GetReturnLocation(Primitive::Type type) const {
2674 switch (type) {
2675 case Primitive::kPrimBoolean:
2676 case Primitive::kPrimByte:
2677 case Primitive::kPrimChar:
2678 case Primitive::kPrimShort:
2679 case Primitive::kPrimInt:
2680 case Primitive::kPrimNot: {
2681 return LocationFrom(r0);
2682 }
2683
2684 case Primitive::kPrimFloat: {
2685 return LocationFrom(s0);
2686 }
2687
2688 case Primitive::kPrimLong: {
2689 return LocationFrom(r0, r1);
2690 }
2691
2692 case Primitive::kPrimDouble: {
2693 return LocationFrom(s0, s1);
2694 }
2695
2696 case Primitive::kPrimVoid:
2697 return Location::NoLocation();
2698 }
2699
2700 UNREACHABLE();
2701}
2702
2703Location InvokeDexCallingConventionVisitorARMVIXL::GetMethodLocation() const {
2704 return LocationFrom(kMethodRegister);
2705}
2706
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002707void CodeGeneratorARMVIXL::Move32(Location destination, Location source) {
2708 if (source.Equals(destination)) {
2709 return;
2710 }
2711 if (destination.IsRegister()) {
2712 if (source.IsRegister()) {
2713 __ Mov(RegisterFrom(destination), RegisterFrom(source));
2714 } else if (source.IsFpuRegister()) {
2715 __ Vmov(RegisterFrom(destination), SRegisterFrom(source));
2716 } else {
2717 GetAssembler()->LoadFromOffset(kLoadWord,
2718 RegisterFrom(destination),
2719 sp,
2720 source.GetStackIndex());
2721 }
2722 } else if (destination.IsFpuRegister()) {
2723 if (source.IsRegister()) {
2724 __ Vmov(SRegisterFrom(destination), RegisterFrom(source));
2725 } else if (source.IsFpuRegister()) {
2726 __ Vmov(SRegisterFrom(destination), SRegisterFrom(source));
2727 } else {
2728 GetAssembler()->LoadSFromOffset(SRegisterFrom(destination), sp, source.GetStackIndex());
2729 }
2730 } else {
2731 DCHECK(destination.IsStackSlot()) << destination;
2732 if (source.IsRegister()) {
2733 GetAssembler()->StoreToOffset(kStoreWord,
2734 RegisterFrom(source),
2735 sp,
2736 destination.GetStackIndex());
2737 } else if (source.IsFpuRegister()) {
2738 GetAssembler()->StoreSToOffset(SRegisterFrom(source), sp, destination.GetStackIndex());
2739 } else {
2740 DCHECK(source.IsStackSlot()) << source;
2741 UseScratchRegisterScope temps(GetVIXLAssembler());
2742 vixl32::Register temp = temps.Acquire();
2743 GetAssembler()->LoadFromOffset(kLoadWord, temp, sp, source.GetStackIndex());
2744 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
2745 }
2746 }
2747}
2748
Artem Serovcfbe9132016-10-14 15:58:56 +01002749void CodeGeneratorARMVIXL::MoveConstant(Location location, int32_t value) {
2750 DCHECK(location.IsRegister());
2751 __ Mov(RegisterFrom(location), value);
Scott Wakelingfe885462016-09-22 10:24:38 +01002752}
2753
2754void CodeGeneratorARMVIXL::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002755 // TODO(VIXL): Maybe refactor to have the 'move' implementation here and use it in
2756 // `ParallelMoveResolverARMVIXL::EmitMove`, as is done in the `arm64` backend.
2757 HParallelMove move(GetGraph()->GetArena());
2758 move.AddMove(src, dst, dst_type, nullptr);
2759 GetMoveResolver()->EmitNativeCode(&move);
Scott Wakelingfe885462016-09-22 10:24:38 +01002760}
2761
Artem Serovcfbe9132016-10-14 15:58:56 +01002762void CodeGeneratorARMVIXL::AddLocationAsTemp(Location location, LocationSummary* locations) {
2763 if (location.IsRegister()) {
2764 locations->AddTemp(location);
2765 } else if (location.IsRegisterPair()) {
2766 locations->AddTemp(LocationFrom(LowRegisterFrom(location)));
2767 locations->AddTemp(LocationFrom(HighRegisterFrom(location)));
2768 } else {
2769 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
2770 }
Scott Wakelingfe885462016-09-22 10:24:38 +01002771}
2772
2773void CodeGeneratorARMVIXL::InvokeRuntime(QuickEntrypointEnum entrypoint,
2774 HInstruction* instruction,
2775 uint32_t dex_pc,
2776 SlowPathCode* slow_path) {
2777 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Alexandre Rames374ddf32016-11-04 10:40:49 +00002778 __ Ldr(lr, MemOperand(tr, GetThreadOffset<kArmPointerSize>(entrypoint).Int32Value()));
2779 // Ensure the pc position is recorded immediately after the `blx` instruction.
2780 // 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 +00002781 ExactAssemblyScope aas(GetVIXLAssembler(),
2782 vixl32::k16BitT32InstructionSizeInBytes,
2783 CodeBufferCheckScope::kExactSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00002784 __ blx(lr);
Scott Wakelingfe885462016-09-22 10:24:38 +01002785 if (EntrypointRequiresStackMap(entrypoint)) {
2786 RecordPcInfo(instruction, dex_pc, slow_path);
2787 }
2788}
2789
2790void CodeGeneratorARMVIXL::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
2791 HInstruction* instruction,
2792 SlowPathCode* slow_path) {
2793 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Alexandre Rames374ddf32016-11-04 10:40:49 +00002794 __ Ldr(lr, MemOperand(tr, entry_point_offset));
Scott Wakelingfe885462016-09-22 10:24:38 +01002795 __ Blx(lr);
2796}
2797
Scott Wakelingfe885462016-09-22 10:24:38 +01002798void InstructionCodeGeneratorARMVIXL::HandleGoto(HInstruction* got, HBasicBlock* successor) {
2799 DCHECK(!successor->IsExitBlock());
2800 HBasicBlock* block = got->GetBlock();
2801 HInstruction* previous = got->GetPrevious();
2802 HLoopInformation* info = block->GetLoopInformation();
2803
2804 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
2805 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
2806 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2807 return;
2808 }
2809 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2810 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
Roland Levillain5daa4952017-07-03 17:23:56 +01002811 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 2);
Scott Wakelingfe885462016-09-22 10:24:38 +01002812 }
2813 if (!codegen_->GoesToNextBlock(block, successor)) {
2814 __ B(codegen_->GetLabelOf(successor));
2815 }
2816}
2817
2818void LocationsBuilderARMVIXL::VisitGoto(HGoto* got) {
2819 got->SetLocations(nullptr);
2820}
2821
2822void InstructionCodeGeneratorARMVIXL::VisitGoto(HGoto* got) {
2823 HandleGoto(got, got->GetSuccessor());
2824}
2825
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002826void LocationsBuilderARMVIXL::VisitTryBoundary(HTryBoundary* try_boundary) {
2827 try_boundary->SetLocations(nullptr);
2828}
2829
2830void InstructionCodeGeneratorARMVIXL::VisitTryBoundary(HTryBoundary* try_boundary) {
2831 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2832 if (!successor->IsExitBlock()) {
2833 HandleGoto(try_boundary, successor);
2834 }
2835}
2836
Scott Wakelingfe885462016-09-22 10:24:38 +01002837void LocationsBuilderARMVIXL::VisitExit(HExit* exit) {
2838 exit->SetLocations(nullptr);
2839}
2840
2841void InstructionCodeGeneratorARMVIXL::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
2842}
2843
Scott Wakelingfe885462016-09-22 10:24:38 +01002844void InstructionCodeGeneratorARMVIXL::GenerateCompareTestAndBranch(HCondition* condition,
Anton Kirilov23b752b2017-07-20 14:40:44 +01002845 vixl32::Label* true_target,
2846 vixl32::Label* false_target,
Anton Kirilovfd522532017-05-10 12:46:57 +01002847 bool is_far_target) {
Anton Kirilov23b752b2017-07-20 14:40:44 +01002848 if (true_target == false_target) {
2849 DCHECK(true_target != nullptr);
2850 __ B(true_target);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002851 return;
2852 }
2853
Anton Kirilov23b752b2017-07-20 14:40:44 +01002854 vixl32::Label* non_fallthrough_target;
2855 bool invert;
2856 bool emit_both_branches;
Scott Wakelingfe885462016-09-22 10:24:38 +01002857
Anton Kirilov23b752b2017-07-20 14:40:44 +01002858 if (true_target == nullptr) {
2859 // The true target is fallthrough.
2860 DCHECK(false_target != nullptr);
2861 non_fallthrough_target = false_target;
2862 invert = true;
2863 emit_both_branches = false;
2864 } else {
2865 non_fallthrough_target = true_target;
2866 invert = false;
2867 // Either the false target is fallthrough, or there is no fallthrough
2868 // and both branches must be emitted.
2869 emit_both_branches = (false_target != nullptr);
Scott Wakelingfe885462016-09-22 10:24:38 +01002870 }
2871
Anton Kirilov23b752b2017-07-20 14:40:44 +01002872 const auto cond = GenerateTest(condition, invert, codegen_);
2873
2874 __ B(cond.first, non_fallthrough_target, is_far_target);
2875
2876 if (emit_both_branches) {
2877 // No target falls through, we need to branch.
2878 __ B(false_target);
Scott Wakelingfe885462016-09-22 10:24:38 +01002879 }
2880}
2881
2882void InstructionCodeGeneratorARMVIXL::GenerateTestAndBranch(HInstruction* instruction,
2883 size_t condition_input_index,
2884 vixl32::Label* true_target,
xueliang.zhongf51bc622016-11-04 09:23:32 +00002885 vixl32::Label* false_target,
2886 bool far_target) {
Scott Wakelingfe885462016-09-22 10:24:38 +01002887 HInstruction* cond = instruction->InputAt(condition_input_index);
2888
2889 if (true_target == nullptr && false_target == nullptr) {
2890 // Nothing to do. The code always falls through.
2891 return;
2892 } else if (cond->IsIntConstant()) {
2893 // Constant condition, statically compared against "true" (integer value 1).
2894 if (cond->AsIntConstant()->IsTrue()) {
2895 if (true_target != nullptr) {
2896 __ B(true_target);
2897 }
2898 } else {
Anton Kirilov644032c2016-12-06 17:51:43 +00002899 DCHECK(cond->AsIntConstant()->IsFalse()) << Int32ConstantFrom(cond);
Scott Wakelingfe885462016-09-22 10:24:38 +01002900 if (false_target != nullptr) {
2901 __ B(false_target);
2902 }
2903 }
2904 return;
2905 }
2906
2907 // The following code generates these patterns:
2908 // (1) true_target == nullptr && false_target != nullptr
2909 // - opposite condition true => branch to false_target
2910 // (2) true_target != nullptr && false_target == nullptr
2911 // - condition true => branch to true_target
2912 // (3) true_target != nullptr && false_target != nullptr
2913 // - condition true => branch to true_target
2914 // - branch to false_target
2915 if (IsBooleanValueOrMaterializedCondition(cond)) {
2916 // Condition has been materialized, compare the output to 0.
2917 if (kIsDebugBuild) {
2918 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
2919 DCHECK(cond_val.IsRegister());
2920 }
2921 if (true_target == nullptr) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00002922 __ CompareAndBranchIfZero(InputRegisterAt(instruction, condition_input_index),
2923 false_target,
2924 far_target);
Scott Wakelingfe885462016-09-22 10:24:38 +01002925 } else {
xueliang.zhongf51bc622016-11-04 09:23:32 +00002926 __ CompareAndBranchIfNonZero(InputRegisterAt(instruction, condition_input_index),
2927 true_target,
2928 far_target);
Scott Wakelingfe885462016-09-22 10:24:38 +01002929 }
2930 } else {
2931 // Condition has not been materialized. Use its inputs as the comparison and
2932 // its condition as the branch condition.
2933 HCondition* condition = cond->AsCondition();
2934
2935 // If this is a long or FP comparison that has been folded into
2936 // the HCondition, generate the comparison directly.
2937 Primitive::Type type = condition->InputAt(0)->GetType();
2938 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
Anton Kirilovfd522532017-05-10 12:46:57 +01002939 GenerateCompareTestAndBranch(condition, true_target, false_target, far_target);
Scott Wakelingfe885462016-09-22 10:24:38 +01002940 return;
2941 }
2942
Donghui Bai426b49c2016-11-08 14:55:38 +08002943 vixl32::Label* non_fallthrough_target;
2944 vixl32::Condition arm_cond = vixl32::Condition::None();
2945 const vixl32::Register left = InputRegisterAt(cond, 0);
2946 const Operand right = InputOperandAt(cond, 1);
2947
Scott Wakelingfe885462016-09-22 10:24:38 +01002948 if (true_target == nullptr) {
Donghui Bai426b49c2016-11-08 14:55:38 +08002949 arm_cond = ARMCondition(condition->GetOppositeCondition());
2950 non_fallthrough_target = false_target;
Scott Wakelingfe885462016-09-22 10:24:38 +01002951 } else {
Donghui Bai426b49c2016-11-08 14:55:38 +08002952 arm_cond = ARMCondition(condition->GetCondition());
2953 non_fallthrough_target = true_target;
2954 }
2955
2956 if (right.IsImmediate() && right.GetImmediate() == 0 && (arm_cond.Is(ne) || arm_cond.Is(eq))) {
2957 if (arm_cond.Is(eq)) {
Anton Kirilovfd522532017-05-10 12:46:57 +01002958 __ CompareAndBranchIfZero(left, non_fallthrough_target, far_target);
Donghui Bai426b49c2016-11-08 14:55:38 +08002959 } else {
2960 DCHECK(arm_cond.Is(ne));
Anton Kirilovfd522532017-05-10 12:46:57 +01002961 __ CompareAndBranchIfNonZero(left, non_fallthrough_target, far_target);
Donghui Bai426b49c2016-11-08 14:55:38 +08002962 }
2963 } else {
2964 __ Cmp(left, right);
Anton Kirilovfd522532017-05-10 12:46:57 +01002965 __ B(arm_cond, non_fallthrough_target, far_target);
Scott Wakelingfe885462016-09-22 10:24:38 +01002966 }
2967 }
2968
2969 // If neither branch falls through (case 3), the conditional branch to `true_target`
2970 // was already emitted (case 2) and we need to emit a jump to `false_target`.
2971 if (true_target != nullptr && false_target != nullptr) {
2972 __ B(false_target);
2973 }
2974}
2975
2976void LocationsBuilderARMVIXL::VisitIf(HIf* if_instr) {
2977 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
2978 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
2979 locations->SetInAt(0, Location::RequiresRegister());
2980 }
2981}
2982
2983void InstructionCodeGeneratorARMVIXL::VisitIf(HIf* if_instr) {
2984 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
2985 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002986 vixl32::Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
2987 nullptr : codegen_->GetLabelOf(true_successor);
2988 vixl32::Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
2989 nullptr : codegen_->GetLabelOf(false_successor);
Scott Wakelingfe885462016-09-22 10:24:38 +01002990 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
2991}
2992
Scott Wakelingc34dba72016-10-03 10:14:44 +01002993void LocationsBuilderARMVIXL::VisitDeoptimize(HDeoptimize* deoptimize) {
2994 LocationSummary* locations = new (GetGraph()->GetArena())
2995 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01002996 InvokeRuntimeCallingConventionARMVIXL calling_convention;
2997 RegisterSet caller_saves = RegisterSet::Empty();
2998 caller_saves.Add(LocationFrom(calling_convention.GetRegisterAt(0)));
2999 locations->SetCustomSlowPathCallerSaves(caller_saves);
Scott Wakelingc34dba72016-10-03 10:14:44 +01003000 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
3001 locations->SetInAt(0, Location::RequiresRegister());
3002 }
3003}
3004
3005void InstructionCodeGeneratorARMVIXL::VisitDeoptimize(HDeoptimize* deoptimize) {
3006 SlowPathCodeARMVIXL* slow_path =
3007 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARMVIXL>(deoptimize);
3008 GenerateTestAndBranch(deoptimize,
3009 /* condition_input_index */ 0,
3010 slow_path->GetEntryLabel(),
3011 /* false_target */ nullptr);
3012}
3013
Artem Serovd4cc5b22016-11-04 11:19:09 +00003014void LocationsBuilderARMVIXL::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
3015 LocationSummary* locations = new (GetGraph()->GetArena())
3016 LocationSummary(flag, LocationSummary::kNoCall);
3017 locations->SetOut(Location::RequiresRegister());
3018}
3019
3020void InstructionCodeGeneratorARMVIXL::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
3021 GetAssembler()->LoadFromOffset(kLoadWord,
3022 OutputRegister(flag),
3023 sp,
3024 codegen_->GetStackOffsetOfShouldDeoptimizeFlag());
3025}
3026
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003027void LocationsBuilderARMVIXL::VisitSelect(HSelect* select) {
3028 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Donghui Bai426b49c2016-11-08 14:55:38 +08003029 const bool is_floating_point = Primitive::IsFloatingPointType(select->GetType());
3030
3031 if (is_floating_point) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003032 locations->SetInAt(0, Location::RequiresFpuRegister());
Donghui Bai426b49c2016-11-08 14:55:38 +08003033 locations->SetInAt(1, Location::FpuRegisterOrConstant(select->GetTrueValue()));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003034 } else {
3035 locations->SetInAt(0, Location::RequiresRegister());
Donghui Bai426b49c2016-11-08 14:55:38 +08003036 locations->SetInAt(1, Arm8BitEncodableConstantOrRegister(select->GetTrueValue()));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003037 }
Donghui Bai426b49c2016-11-08 14:55:38 +08003038
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003039 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
Donghui Bai426b49c2016-11-08 14:55:38 +08003040 locations->SetInAt(2, Location::RegisterOrConstant(select->GetCondition()));
3041 // The code generator handles overlap with the values, but not with the condition.
3042 locations->SetOut(Location::SameAsFirstInput());
3043 } else if (is_floating_point) {
3044 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3045 } else {
3046 if (!locations->InAt(1).IsConstant()) {
3047 locations->SetInAt(0, Arm8BitEncodableConstantOrRegister(select->GetFalseValue()));
3048 }
3049
3050 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003051 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003052}
3053
3054void InstructionCodeGeneratorARMVIXL::VisitSelect(HSelect* select) {
Donghui Bai426b49c2016-11-08 14:55:38 +08003055 HInstruction* const condition = select->GetCondition();
3056 const LocationSummary* const locations = select->GetLocations();
3057 const Primitive::Type type = select->GetType();
3058 const Location first = locations->InAt(0);
3059 const Location out = locations->Out();
3060 const Location second = locations->InAt(1);
3061 Location src;
3062
3063 if (condition->IsIntConstant()) {
3064 if (condition->AsIntConstant()->IsFalse()) {
3065 src = first;
3066 } else {
3067 src = second;
3068 }
3069
3070 codegen_->MoveLocation(out, src, type);
3071 return;
3072 }
3073
Anton Kirilov23b752b2017-07-20 14:40:44 +01003074 if (!Primitive::IsFloatingPointType(type)) {
Donghui Bai426b49c2016-11-08 14:55:38 +08003075 bool invert = false;
3076
3077 if (out.Equals(second)) {
3078 src = first;
3079 invert = true;
3080 } else if (out.Equals(first)) {
3081 src = second;
3082 } else if (second.IsConstant()) {
3083 DCHECK(CanEncodeConstantAs8BitImmediate(second.GetConstant()));
3084 src = second;
3085 } else if (first.IsConstant()) {
3086 DCHECK(CanEncodeConstantAs8BitImmediate(first.GetConstant()));
3087 src = first;
3088 invert = true;
3089 } else {
3090 src = second;
3091 }
3092
3093 if (CanGenerateConditionalMove(out, src)) {
3094 if (!out.Equals(first) && !out.Equals(second)) {
3095 codegen_->MoveLocation(out, src.Equals(first) ? second : first, type);
3096 }
3097
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003098 std::pair<vixl32::Condition, vixl32::Condition> cond(eq, ne);
3099
3100 if (IsBooleanValueOrMaterializedCondition(condition)) {
3101 __ Cmp(InputRegisterAt(select, 2), 0);
3102 cond = invert ? std::make_pair(eq, ne) : std::make_pair(ne, eq);
3103 } else {
3104 cond = GenerateTest(condition->AsCondition(), invert, codegen_);
3105 }
3106
Donghui Bai426b49c2016-11-08 14:55:38 +08003107 const size_t instr_count = out.IsRegisterPair() ? 4 : 2;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003108 // We use the scope because of the IT block that follows.
Donghui Bai426b49c2016-11-08 14:55:38 +08003109 ExactAssemblyScope guard(GetVIXLAssembler(),
3110 instr_count * vixl32::k16BitT32InstructionSizeInBytes,
3111 CodeBufferCheckScope::kExactSize);
3112
3113 if (out.IsRegister()) {
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003114 __ it(cond.first);
3115 __ mov(cond.first, RegisterFrom(out), OperandFrom(src, type));
Donghui Bai426b49c2016-11-08 14:55:38 +08003116 } else {
3117 DCHECK(out.IsRegisterPair());
3118
3119 Operand operand_high(0);
3120 Operand operand_low(0);
3121
3122 if (src.IsConstant()) {
3123 const int64_t value = Int64ConstantFrom(src);
3124
3125 operand_high = High32Bits(value);
3126 operand_low = Low32Bits(value);
3127 } else {
3128 DCHECK(src.IsRegisterPair());
3129 operand_high = HighRegisterFrom(src);
3130 operand_low = LowRegisterFrom(src);
3131 }
3132
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003133 __ it(cond.first);
3134 __ mov(cond.first, LowRegisterFrom(out), operand_low);
3135 __ it(cond.first);
3136 __ mov(cond.first, HighRegisterFrom(out), operand_high);
Donghui Bai426b49c2016-11-08 14:55:38 +08003137 }
3138
3139 return;
3140 }
3141 }
3142
3143 vixl32::Label* false_target = nullptr;
3144 vixl32::Label* true_target = nullptr;
3145 vixl32::Label select_end;
3146 vixl32::Label* const target = codegen_->GetFinalLabel(select, &select_end);
3147
3148 if (out.Equals(second)) {
3149 true_target = target;
3150 src = first;
3151 } else {
3152 false_target = target;
3153 src = second;
3154
3155 if (!out.Equals(first)) {
3156 codegen_->MoveLocation(out, first, type);
3157 }
3158 }
3159
3160 GenerateTestAndBranch(select, 2, true_target, false_target, /* far_target */ false);
3161 codegen_->MoveLocation(out, src, type);
3162
3163 if (select_end.IsReferenced()) {
3164 __ Bind(&select_end);
3165 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003166}
3167
Artem Serov551b28f2016-10-18 19:11:30 +01003168void LocationsBuilderARMVIXL::VisitNativeDebugInfo(HNativeDebugInfo* info) {
3169 new (GetGraph()->GetArena()) LocationSummary(info);
3170}
3171
3172void InstructionCodeGeneratorARMVIXL::VisitNativeDebugInfo(HNativeDebugInfo*) {
3173 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
3174}
3175
Scott Wakelingfe885462016-09-22 10:24:38 +01003176void CodeGeneratorARMVIXL::GenerateNop() {
3177 __ Nop();
3178}
3179
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003180// `temp` is an extra temporary register that is used for some conditions;
3181// callers may not specify it, in which case the method will use a scratch
3182// register instead.
3183void CodeGeneratorARMVIXL::GenerateConditionWithZero(IfCondition condition,
3184 vixl32::Register out,
3185 vixl32::Register in,
3186 vixl32::Register temp) {
3187 switch (condition) {
3188 case kCondEQ:
3189 // x <= 0 iff x == 0 when the comparison is unsigned.
3190 case kCondBE:
3191 if (!temp.IsValid() || (out.IsLow() && !out.Is(in))) {
3192 temp = out;
3193 }
3194
3195 // Avoid 32-bit instructions if possible; note that `in` and `temp` must be
3196 // different as well.
3197 if (in.IsLow() && temp.IsLow() && !in.Is(temp)) {
3198 // temp = - in; only 0 sets the carry flag.
3199 __ Rsbs(temp, in, 0);
3200
3201 if (out.Is(in)) {
3202 std::swap(in, temp);
3203 }
3204
3205 // out = - in + in + carry = carry
3206 __ Adc(out, temp, in);
3207 } else {
3208 // If `in` is 0, then it has 32 leading zeros, and less than that otherwise.
3209 __ Clz(out, in);
3210 // Any number less than 32 logically shifted right by 5 bits results in 0;
3211 // the same operation on 32 yields 1.
3212 __ Lsr(out, out, 5);
3213 }
3214
3215 break;
3216 case kCondNE:
3217 // x > 0 iff x != 0 when the comparison is unsigned.
3218 case kCondA: {
3219 UseScratchRegisterScope temps(GetVIXLAssembler());
3220
3221 if (out.Is(in)) {
3222 if (!temp.IsValid() || in.Is(temp)) {
3223 temp = temps.Acquire();
3224 }
3225 } else if (!temp.IsValid() || !temp.IsLow()) {
3226 temp = out;
3227 }
3228
3229 // temp = in - 1; only 0 does not set the carry flag.
3230 __ Subs(temp, in, 1);
3231 // out = in + ~temp + carry = in + (-(in - 1) - 1) + carry = in - in + 1 - 1 + carry = carry
3232 __ Sbc(out, in, temp);
3233 break;
3234 }
3235 case kCondGE:
3236 __ Mvn(out, in);
3237 in = out;
3238 FALLTHROUGH_INTENDED;
3239 case kCondLT:
3240 // We only care about the sign bit.
3241 __ Lsr(out, in, 31);
3242 break;
3243 case kCondAE:
3244 // Trivially true.
3245 __ Mov(out, 1);
3246 break;
3247 case kCondB:
3248 // Trivially false.
3249 __ Mov(out, 0);
3250 break;
3251 default:
3252 LOG(FATAL) << "Unexpected condition " << condition;
3253 UNREACHABLE();
3254 }
3255}
3256
Scott Wakelingfe885462016-09-22 10:24:38 +01003257void LocationsBuilderARMVIXL::HandleCondition(HCondition* cond) {
3258 LocationSummary* locations =
3259 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
3260 // Handle the long/FP comparisons made in instruction simplification.
3261 switch (cond->InputAt(0)->GetType()) {
3262 case Primitive::kPrimLong:
3263 locations->SetInAt(0, Location::RequiresRegister());
3264 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
3265 if (!cond->IsEmittedAtUseSite()) {
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003266 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Scott Wakelingfe885462016-09-22 10:24:38 +01003267 }
3268 break;
3269
Scott Wakelingfe885462016-09-22 10:24:38 +01003270 case Primitive::kPrimFloat:
3271 case Primitive::kPrimDouble:
3272 locations->SetInAt(0, Location::RequiresFpuRegister());
Artem Serov657022c2016-11-23 14:19:38 +00003273 locations->SetInAt(1, ArithmeticZeroOrFpuRegister(cond->InputAt(1)));
Scott Wakelingfe885462016-09-22 10:24:38 +01003274 if (!cond->IsEmittedAtUseSite()) {
3275 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3276 }
3277 break;
3278
3279 default:
3280 locations->SetInAt(0, Location::RequiresRegister());
3281 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
3282 if (!cond->IsEmittedAtUseSite()) {
3283 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3284 }
3285 }
3286}
3287
3288void InstructionCodeGeneratorARMVIXL::HandleCondition(HCondition* cond) {
3289 if (cond->IsEmittedAtUseSite()) {
3290 return;
3291 }
3292
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003293 const Primitive::Type type = cond->GetLeft()->GetType();
Scott Wakelingfe885462016-09-22 10:24:38 +01003294
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003295 if (Primitive::IsFloatingPointType(type)) {
3296 GenerateConditionGeneric(cond, codegen_);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003297 return;
Scott Wakelingfe885462016-09-22 10:24:38 +01003298 }
3299
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003300 DCHECK(Primitive::IsIntegralType(type) || type == Primitive::kPrimNot) << type;
Scott Wakelingfe885462016-09-22 10:24:38 +01003301
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003302 const IfCondition condition = cond->GetCondition();
Scott Wakelingfe885462016-09-22 10:24:38 +01003303
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003304 // A condition with only one boolean input, or two boolean inputs without being equality or
3305 // inequality results from transformations done by the instruction simplifier, and is handled
3306 // as a regular condition with integral inputs.
3307 if (type == Primitive::kPrimBoolean &&
3308 cond->GetRight()->GetType() == Primitive::kPrimBoolean &&
3309 (condition == kCondEQ || condition == kCondNE)) {
3310 vixl32::Register left = InputRegisterAt(cond, 0);
3311 const vixl32::Register out = OutputRegister(cond);
3312 const Location right_loc = cond->GetLocations()->InAt(1);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003313
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003314 // The constant case is handled by the instruction simplifier.
3315 DCHECK(!right_loc.IsConstant());
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003316
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003317 vixl32::Register right = RegisterFrom(right_loc);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003318
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003319 // Avoid 32-bit instructions if possible.
3320 if (out.Is(right)) {
3321 std::swap(left, right);
3322 }
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003323
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003324 __ Eor(out, left, right);
3325
3326 if (condition == kCondEQ) {
3327 __ Eor(out, out, 1);
3328 }
3329
3330 return;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003331 }
Anton Kirilov6f644202017-02-27 18:29:45 +00003332
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003333 GenerateConditionIntegralOrNonPrimitive(cond, codegen_);
Scott Wakelingfe885462016-09-22 10:24:38 +01003334}
3335
3336void LocationsBuilderARMVIXL::VisitEqual(HEqual* comp) {
3337 HandleCondition(comp);
3338}
3339
3340void InstructionCodeGeneratorARMVIXL::VisitEqual(HEqual* comp) {
3341 HandleCondition(comp);
3342}
3343
3344void LocationsBuilderARMVIXL::VisitNotEqual(HNotEqual* comp) {
3345 HandleCondition(comp);
3346}
3347
3348void InstructionCodeGeneratorARMVIXL::VisitNotEqual(HNotEqual* comp) {
3349 HandleCondition(comp);
3350}
3351
3352void LocationsBuilderARMVIXL::VisitLessThan(HLessThan* comp) {
3353 HandleCondition(comp);
3354}
3355
3356void InstructionCodeGeneratorARMVIXL::VisitLessThan(HLessThan* comp) {
3357 HandleCondition(comp);
3358}
3359
3360void LocationsBuilderARMVIXL::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
3361 HandleCondition(comp);
3362}
3363
3364void InstructionCodeGeneratorARMVIXL::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
3365 HandleCondition(comp);
3366}
3367
3368void LocationsBuilderARMVIXL::VisitGreaterThan(HGreaterThan* comp) {
3369 HandleCondition(comp);
3370}
3371
3372void InstructionCodeGeneratorARMVIXL::VisitGreaterThan(HGreaterThan* comp) {
3373 HandleCondition(comp);
3374}
3375
3376void LocationsBuilderARMVIXL::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
3377 HandleCondition(comp);
3378}
3379
3380void InstructionCodeGeneratorARMVIXL::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
3381 HandleCondition(comp);
3382}
3383
3384void LocationsBuilderARMVIXL::VisitBelow(HBelow* comp) {
3385 HandleCondition(comp);
3386}
3387
3388void InstructionCodeGeneratorARMVIXL::VisitBelow(HBelow* comp) {
3389 HandleCondition(comp);
3390}
3391
3392void LocationsBuilderARMVIXL::VisitBelowOrEqual(HBelowOrEqual* comp) {
3393 HandleCondition(comp);
3394}
3395
3396void InstructionCodeGeneratorARMVIXL::VisitBelowOrEqual(HBelowOrEqual* comp) {
3397 HandleCondition(comp);
3398}
3399
3400void LocationsBuilderARMVIXL::VisitAbove(HAbove* comp) {
3401 HandleCondition(comp);
3402}
3403
3404void InstructionCodeGeneratorARMVIXL::VisitAbove(HAbove* comp) {
3405 HandleCondition(comp);
3406}
3407
3408void LocationsBuilderARMVIXL::VisitAboveOrEqual(HAboveOrEqual* comp) {
3409 HandleCondition(comp);
3410}
3411
3412void InstructionCodeGeneratorARMVIXL::VisitAboveOrEqual(HAboveOrEqual* comp) {
3413 HandleCondition(comp);
3414}
3415
3416void LocationsBuilderARMVIXL::VisitIntConstant(HIntConstant* constant) {
3417 LocationSummary* locations =
3418 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3419 locations->SetOut(Location::ConstantLocation(constant));
3420}
3421
3422void InstructionCodeGeneratorARMVIXL::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
3423 // Will be generated at use site.
3424}
3425
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003426void LocationsBuilderARMVIXL::VisitNullConstant(HNullConstant* constant) {
3427 LocationSummary* locations =
3428 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3429 locations->SetOut(Location::ConstantLocation(constant));
3430}
3431
3432void InstructionCodeGeneratorARMVIXL::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
3433 // Will be generated at use site.
3434}
3435
Scott Wakelingfe885462016-09-22 10:24:38 +01003436void LocationsBuilderARMVIXL::VisitLongConstant(HLongConstant* constant) {
3437 LocationSummary* locations =
3438 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3439 locations->SetOut(Location::ConstantLocation(constant));
3440}
3441
3442void InstructionCodeGeneratorARMVIXL::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
3443 // Will be generated at use site.
3444}
3445
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01003446void LocationsBuilderARMVIXL::VisitFloatConstant(HFloatConstant* constant) {
3447 LocationSummary* locations =
3448 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3449 locations->SetOut(Location::ConstantLocation(constant));
3450}
3451
Scott Wakelingc34dba72016-10-03 10:14:44 +01003452void InstructionCodeGeneratorARMVIXL::VisitFloatConstant(
3453 HFloatConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01003454 // Will be generated at use site.
3455}
3456
3457void LocationsBuilderARMVIXL::VisitDoubleConstant(HDoubleConstant* constant) {
3458 LocationSummary* locations =
3459 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3460 locations->SetOut(Location::ConstantLocation(constant));
3461}
3462
Scott Wakelingc34dba72016-10-03 10:14:44 +01003463void InstructionCodeGeneratorARMVIXL::VisitDoubleConstant(
3464 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01003465 // Will be generated at use site.
3466}
3467
Igor Murashkind01745e2017-04-05 16:40:31 -07003468void LocationsBuilderARMVIXL::VisitConstructorFence(HConstructorFence* constructor_fence) {
3469 constructor_fence->SetLocations(nullptr);
3470}
3471
3472void InstructionCodeGeneratorARMVIXL::VisitConstructorFence(
3473 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
3474 codegen_->GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
3475}
3476
Scott Wakelingfe885462016-09-22 10:24:38 +01003477void LocationsBuilderARMVIXL::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3478 memory_barrier->SetLocations(nullptr);
3479}
3480
3481void InstructionCodeGeneratorARMVIXL::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3482 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
3483}
3484
3485void LocationsBuilderARMVIXL::VisitReturnVoid(HReturnVoid* ret) {
3486 ret->SetLocations(nullptr);
3487}
3488
3489void InstructionCodeGeneratorARMVIXL::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
3490 codegen_->GenerateFrameExit();
3491}
3492
3493void LocationsBuilderARMVIXL::VisitReturn(HReturn* ret) {
3494 LocationSummary* locations =
3495 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
3496 locations->SetInAt(0, parameter_visitor_.GetReturnLocation(ret->InputAt(0)->GetType()));
3497}
3498
3499void InstructionCodeGeneratorARMVIXL::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
3500 codegen_->GenerateFrameExit();
3501}
3502
Artem Serovcfbe9132016-10-14 15:58:56 +01003503void LocationsBuilderARMVIXL::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3504 // The trampoline uses the same calling convention as dex calling conventions,
3505 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
3506 // the method_idx.
3507 HandleInvoke(invoke);
3508}
3509
3510void InstructionCodeGeneratorARMVIXL::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3511 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
Roland Levillain5daa4952017-07-03 17:23:56 +01003512 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 3);
Artem Serovcfbe9132016-10-14 15:58:56 +01003513}
3514
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003515void LocationsBuilderARMVIXL::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
3516 // Explicit clinit checks triggered by static invokes must have been pruned by
3517 // art::PrepareForRegisterAllocation.
3518 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
3519
Anton Kirilov5ec62182016-10-13 20:16:02 +01003520 IntrinsicLocationsBuilderARMVIXL intrinsic(codegen_);
3521 if (intrinsic.TryDispatch(invoke)) {
Anton Kirilov5ec62182016-10-13 20:16:02 +01003522 return;
3523 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003524
3525 HandleInvoke(invoke);
3526}
3527
Anton Kirilov5ec62182016-10-13 20:16:02 +01003528static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARMVIXL* codegen) {
3529 if (invoke->GetLocations()->Intrinsified()) {
3530 IntrinsicCodeGeneratorARMVIXL intrinsic(codegen);
3531 intrinsic.Dispatch(invoke);
3532 return true;
3533 }
3534 return false;
3535}
3536
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003537void InstructionCodeGeneratorARMVIXL::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
3538 // Explicit clinit checks triggered by static invokes must have been pruned by
3539 // art::PrepareForRegisterAllocation.
3540 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
3541
Anton Kirilov5ec62182016-10-13 20:16:02 +01003542 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
Roland Levillain5daa4952017-07-03 17:23:56 +01003543 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 4);
Anton Kirilov5ec62182016-10-13 20:16:02 +01003544 return;
3545 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003546
3547 LocationSummary* locations = invoke->GetLocations();
Artem Serovd4cc5b22016-11-04 11:19:09 +00003548 codegen_->GenerateStaticOrDirectCall(
3549 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Roland Levillain5daa4952017-07-03 17:23:56 +01003550
3551 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 5);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003552}
3553
3554void LocationsBuilderARMVIXL::HandleInvoke(HInvoke* invoke) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00003555 InvokeDexCallingConventionVisitorARMVIXL calling_convention_visitor;
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003556 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
3557}
3558
3559void LocationsBuilderARMVIXL::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Anton Kirilov5ec62182016-10-13 20:16:02 +01003560 IntrinsicLocationsBuilderARMVIXL intrinsic(codegen_);
3561 if (intrinsic.TryDispatch(invoke)) {
3562 return;
3563 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003564
3565 HandleInvoke(invoke);
3566}
3567
3568void InstructionCodeGeneratorARMVIXL::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Anton Kirilov5ec62182016-10-13 20:16:02 +01003569 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
Roland Levillain5daa4952017-07-03 17:23:56 +01003570 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 6);
Anton Kirilov5ec62182016-10-13 20:16:02 +01003571 return;
3572 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003573
3574 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexandre Rames374ddf32016-11-04 10:40:49 +00003575 DCHECK(!codegen_->IsLeafMethod());
Roland Levillain5daa4952017-07-03 17:23:56 +01003576
3577 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 7);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003578}
3579
Artem Serovcfbe9132016-10-14 15:58:56 +01003580void LocationsBuilderARMVIXL::VisitInvokeInterface(HInvokeInterface* invoke) {
3581 HandleInvoke(invoke);
3582 // Add the hidden argument.
3583 invoke->GetLocations()->AddTemp(LocationFrom(r12));
3584}
3585
3586void InstructionCodeGeneratorARMVIXL::VisitInvokeInterface(HInvokeInterface* invoke) {
3587 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
3588 LocationSummary* locations = invoke->GetLocations();
3589 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
3590 vixl32::Register hidden_reg = RegisterFrom(locations->GetTemp(1));
3591 Location receiver = locations->InAt(0);
3592 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3593
3594 DCHECK(!receiver.IsStackSlot());
3595
Alexandre Rames374ddf32016-11-04 10:40:49 +00003596 // Ensure the pc position is recorded immediately after the `ldr` instruction.
3597 {
Artem Serov0fb37192016-12-06 18:13:40 +00003598 ExactAssemblyScope aas(GetVIXLAssembler(),
3599 vixl32::kMaxInstructionSizeInBytes,
3600 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00003601 // /* HeapReference<Class> */ temp = receiver->klass_
3602 __ ldr(temp, MemOperand(RegisterFrom(receiver), class_offset));
3603 codegen_->MaybeRecordImplicitNullCheck(invoke);
3604 }
Artem Serovcfbe9132016-10-14 15:58:56 +01003605 // Instead of simply (possibly) unpoisoning `temp` here, we should
3606 // emit a read barrier for the previous class reference load.
3607 // However this is not required in practice, as this is an
3608 // intermediate/temporary reference and because the current
3609 // concurrent copying collector keeps the from-space memory
3610 // intact/accessible until the end of the marking phase (the
3611 // concurrent copying collector may not in the future).
3612 GetAssembler()->MaybeUnpoisonHeapReference(temp);
3613 GetAssembler()->LoadFromOffset(kLoadWord,
3614 temp,
3615 temp,
3616 mirror::Class::ImtPtrOffset(kArmPointerSize).Uint32Value());
3617 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
3618 invoke->GetImtIndex(), kArmPointerSize));
3619 // temp = temp->GetImtEntryAt(method_offset);
3620 GetAssembler()->LoadFromOffset(kLoadWord, temp, temp, method_offset);
3621 uint32_t entry_point =
3622 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize).Int32Value();
3623 // LR = temp->GetEntryPoint();
3624 GetAssembler()->LoadFromOffset(kLoadWord, lr, temp, entry_point);
3625
3626 // Set the hidden (in r12) argument. It is done here, right before a BLX to prevent other
3627 // instruction from clobbering it as they might use r12 as a scratch register.
3628 DCHECK(hidden_reg.Is(r12));
Scott Wakelingb77051e2016-11-21 19:46:00 +00003629
3630 {
3631 // The VIXL macro assembler may clobber any of the scratch registers that are available to it,
3632 // so it checks if the application is using them (by passing them to the macro assembler
3633 // methods). The following application of UseScratchRegisterScope corrects VIXL's notion of
3634 // what is available, and is the opposite of the standard usage: Instead of requesting a
3635 // temporary location, it imposes an external constraint (i.e. a specific register is reserved
3636 // for the hidden argument). Note that this works even if VIXL needs a scratch register itself
3637 // (to materialize the constant), since the destination register becomes available for such use
3638 // internally for the duration of the macro instruction.
3639 UseScratchRegisterScope temps(GetVIXLAssembler());
3640 temps.Exclude(hidden_reg);
3641 __ Mov(hidden_reg, invoke->GetDexMethodIndex());
3642 }
Artem Serovcfbe9132016-10-14 15:58:56 +01003643 {
Alexandre Rames374ddf32016-11-04 10:40:49 +00003644 // Ensure the pc position is recorded immediately after the `blx` instruction.
3645 // 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 +00003646 ExactAssemblyScope aas(GetVIXLAssembler(),
Alexandre Rames374ddf32016-11-04 10:40:49 +00003647 vixl32::k16BitT32InstructionSizeInBytes,
3648 CodeBufferCheckScope::kExactSize);
Artem Serovcfbe9132016-10-14 15:58:56 +01003649 // LR();
3650 __ blx(lr);
Artem Serovcfbe9132016-10-14 15:58:56 +01003651 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Alexandre Rames374ddf32016-11-04 10:40:49 +00003652 DCHECK(!codegen_->IsLeafMethod());
Artem Serovcfbe9132016-10-14 15:58:56 +01003653 }
Roland Levillain5daa4952017-07-03 17:23:56 +01003654
3655 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 8);
Artem Serovcfbe9132016-10-14 15:58:56 +01003656}
3657
Orion Hodsonac141392017-01-13 11:53:47 +00003658void LocationsBuilderARMVIXL::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
3659 HandleInvoke(invoke);
3660}
3661
3662void InstructionCodeGeneratorARMVIXL::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
3663 codegen_->GenerateInvokePolymorphicCall(invoke);
Roland Levillain5daa4952017-07-03 17:23:56 +01003664 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 9);
Orion Hodsonac141392017-01-13 11:53:47 +00003665}
3666
Artem Serov02109dd2016-09-23 17:17:54 +01003667void LocationsBuilderARMVIXL::VisitNeg(HNeg* neg) {
3668 LocationSummary* locations =
3669 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
3670 switch (neg->GetResultType()) {
3671 case Primitive::kPrimInt: {
3672 locations->SetInAt(0, Location::RequiresRegister());
3673 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3674 break;
3675 }
3676 case Primitive::kPrimLong: {
3677 locations->SetInAt(0, Location::RequiresRegister());
3678 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3679 break;
3680 }
3681
3682 case Primitive::kPrimFloat:
3683 case Primitive::kPrimDouble:
3684 locations->SetInAt(0, Location::RequiresFpuRegister());
3685 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3686 break;
3687
3688 default:
3689 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
3690 }
3691}
3692
3693void InstructionCodeGeneratorARMVIXL::VisitNeg(HNeg* neg) {
3694 LocationSummary* locations = neg->GetLocations();
3695 Location out = locations->Out();
3696 Location in = locations->InAt(0);
3697 switch (neg->GetResultType()) {
3698 case Primitive::kPrimInt:
3699 __ Rsb(OutputRegister(neg), InputRegisterAt(neg, 0), 0);
3700 break;
3701
3702 case Primitive::kPrimLong:
3703 // out.lo = 0 - in.lo (and update the carry/borrow (C) flag)
3704 __ Rsbs(LowRegisterFrom(out), LowRegisterFrom(in), 0);
3705 // We cannot emit an RSC (Reverse Subtract with Carry)
3706 // instruction here, as it does not exist in the Thumb-2
3707 // instruction set. We use the following approach
3708 // using SBC and SUB instead.
3709 //
3710 // out.hi = -C
3711 __ Sbc(HighRegisterFrom(out), HighRegisterFrom(out), HighRegisterFrom(out));
3712 // out.hi = out.hi - in.hi
3713 __ Sub(HighRegisterFrom(out), HighRegisterFrom(out), HighRegisterFrom(in));
3714 break;
3715
3716 case Primitive::kPrimFloat:
3717 case Primitive::kPrimDouble:
Anton Kirilov644032c2016-12-06 17:51:43 +00003718 __ Vneg(OutputVRegister(neg), InputVRegister(neg));
Artem Serov02109dd2016-09-23 17:17:54 +01003719 break;
3720
3721 default:
3722 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
3723 }
3724}
3725
Scott Wakelingfe885462016-09-22 10:24:38 +01003726void LocationsBuilderARMVIXL::VisitTypeConversion(HTypeConversion* conversion) {
3727 Primitive::Type result_type = conversion->GetResultType();
3728 Primitive::Type input_type = conversion->GetInputType();
3729 DCHECK_NE(result_type, input_type);
3730
3731 // The float-to-long, double-to-long and long-to-float type conversions
3732 // rely on a call to the runtime.
3733 LocationSummary::CallKind call_kind =
3734 (((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
3735 && result_type == Primitive::kPrimLong)
3736 || (input_type == Primitive::kPrimLong && result_type == Primitive::kPrimFloat))
3737 ? LocationSummary::kCallOnMainOnly
3738 : LocationSummary::kNoCall;
3739 LocationSummary* locations =
3740 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
3741
3742 // The Java language does not allow treating boolean as an integral type but
3743 // our bit representation makes it safe.
3744
3745 switch (result_type) {
3746 case Primitive::kPrimByte:
3747 switch (input_type) {
3748 case Primitive::kPrimLong:
3749 // Type conversion from long to byte is a result of code transformations.
3750 case Primitive::kPrimBoolean:
3751 // Boolean input is a result of code transformations.
3752 case Primitive::kPrimShort:
3753 case Primitive::kPrimInt:
3754 case Primitive::kPrimChar:
3755 // Processing a Dex `int-to-byte' instruction.
3756 locations->SetInAt(0, Location::RequiresRegister());
3757 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3758 break;
3759
3760 default:
3761 LOG(FATAL) << "Unexpected type conversion from " << input_type
3762 << " to " << result_type;
3763 }
3764 break;
3765
3766 case Primitive::kPrimShort:
3767 switch (input_type) {
3768 case Primitive::kPrimLong:
3769 // Type conversion from long to short is a result of code transformations.
3770 case Primitive::kPrimBoolean:
3771 // Boolean input is a result of code transformations.
3772 case Primitive::kPrimByte:
3773 case Primitive::kPrimInt:
3774 case Primitive::kPrimChar:
3775 // Processing a Dex `int-to-short' instruction.
3776 locations->SetInAt(0, Location::RequiresRegister());
3777 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3778 break;
3779
3780 default:
3781 LOG(FATAL) << "Unexpected type conversion from " << input_type
3782 << " to " << result_type;
3783 }
3784 break;
3785
3786 case Primitive::kPrimInt:
3787 switch (input_type) {
3788 case Primitive::kPrimLong:
3789 // Processing a Dex `long-to-int' instruction.
3790 locations->SetInAt(0, Location::Any());
3791 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3792 break;
3793
3794 case Primitive::kPrimFloat:
3795 // Processing a Dex `float-to-int' instruction.
3796 locations->SetInAt(0, Location::RequiresFpuRegister());
3797 locations->SetOut(Location::RequiresRegister());
3798 locations->AddTemp(Location::RequiresFpuRegister());
3799 break;
3800
3801 case Primitive::kPrimDouble:
3802 // Processing a Dex `double-to-int' instruction.
3803 locations->SetInAt(0, Location::RequiresFpuRegister());
3804 locations->SetOut(Location::RequiresRegister());
3805 locations->AddTemp(Location::RequiresFpuRegister());
3806 break;
3807
3808 default:
3809 LOG(FATAL) << "Unexpected type conversion from " << input_type
3810 << " to " << result_type;
3811 }
3812 break;
3813
3814 case Primitive::kPrimLong:
3815 switch (input_type) {
3816 case Primitive::kPrimBoolean:
3817 // Boolean input is a result of code transformations.
3818 case Primitive::kPrimByte:
3819 case Primitive::kPrimShort:
3820 case Primitive::kPrimInt:
3821 case Primitive::kPrimChar:
3822 // Processing a Dex `int-to-long' instruction.
3823 locations->SetInAt(0, Location::RequiresRegister());
3824 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3825 break;
3826
3827 case Primitive::kPrimFloat: {
3828 // Processing a Dex `float-to-long' instruction.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003829 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3830 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
3831 locations->SetOut(LocationFrom(r0, r1));
Scott Wakelingfe885462016-09-22 10:24:38 +01003832 break;
3833 }
3834
3835 case Primitive::kPrimDouble: {
3836 // Processing a Dex `double-to-long' instruction.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003837 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3838 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0),
3839 calling_convention.GetFpuRegisterAt(1)));
3840 locations->SetOut(LocationFrom(r0, r1));
Scott Wakelingfe885462016-09-22 10:24:38 +01003841 break;
3842 }
3843
3844 default:
3845 LOG(FATAL) << "Unexpected type conversion from " << input_type
3846 << " to " << result_type;
3847 }
3848 break;
3849
3850 case Primitive::kPrimChar:
3851 switch (input_type) {
3852 case Primitive::kPrimLong:
3853 // Type conversion from long to char is a result of code transformations.
3854 case Primitive::kPrimBoolean:
3855 // Boolean input is a result of code transformations.
3856 case Primitive::kPrimByte:
3857 case Primitive::kPrimShort:
3858 case Primitive::kPrimInt:
3859 // Processing a Dex `int-to-char' instruction.
3860 locations->SetInAt(0, Location::RequiresRegister());
3861 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3862 break;
3863
3864 default:
3865 LOG(FATAL) << "Unexpected type conversion from " << input_type
3866 << " to " << result_type;
3867 }
3868 break;
3869
3870 case Primitive::kPrimFloat:
3871 switch (input_type) {
3872 case Primitive::kPrimBoolean:
3873 // Boolean input is a result of code transformations.
3874 case Primitive::kPrimByte:
3875 case Primitive::kPrimShort:
3876 case Primitive::kPrimInt:
3877 case Primitive::kPrimChar:
3878 // Processing a Dex `int-to-float' instruction.
3879 locations->SetInAt(0, Location::RequiresRegister());
3880 locations->SetOut(Location::RequiresFpuRegister());
3881 break;
3882
3883 case Primitive::kPrimLong: {
3884 // Processing a Dex `long-to-float' instruction.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003885 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3886 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0),
3887 calling_convention.GetRegisterAt(1)));
3888 locations->SetOut(LocationFrom(calling_convention.GetFpuRegisterAt(0)));
Scott Wakelingfe885462016-09-22 10:24:38 +01003889 break;
3890 }
3891
3892 case Primitive::kPrimDouble:
3893 // Processing a Dex `double-to-float' instruction.
3894 locations->SetInAt(0, Location::RequiresFpuRegister());
3895 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3896 break;
3897
3898 default:
3899 LOG(FATAL) << "Unexpected type conversion from " << input_type
3900 << " to " << result_type;
3901 };
3902 break;
3903
3904 case Primitive::kPrimDouble:
3905 switch (input_type) {
3906 case Primitive::kPrimBoolean:
3907 // Boolean input is a result of code transformations.
3908 case Primitive::kPrimByte:
3909 case Primitive::kPrimShort:
3910 case Primitive::kPrimInt:
3911 case Primitive::kPrimChar:
3912 // Processing a Dex `int-to-double' instruction.
3913 locations->SetInAt(0, Location::RequiresRegister());
3914 locations->SetOut(Location::RequiresFpuRegister());
3915 break;
3916
3917 case Primitive::kPrimLong:
3918 // Processing a Dex `long-to-double' instruction.
3919 locations->SetInAt(0, Location::RequiresRegister());
3920 locations->SetOut(Location::RequiresFpuRegister());
3921 locations->AddTemp(Location::RequiresFpuRegister());
3922 locations->AddTemp(Location::RequiresFpuRegister());
3923 break;
3924
3925 case Primitive::kPrimFloat:
3926 // Processing a Dex `float-to-double' instruction.
3927 locations->SetInAt(0, Location::RequiresFpuRegister());
3928 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3929 break;
3930
3931 default:
3932 LOG(FATAL) << "Unexpected type conversion from " << input_type
3933 << " to " << result_type;
3934 };
3935 break;
3936
3937 default:
3938 LOG(FATAL) << "Unexpected type conversion from " << input_type
3939 << " to " << result_type;
3940 }
3941}
3942
3943void InstructionCodeGeneratorARMVIXL::VisitTypeConversion(HTypeConversion* conversion) {
3944 LocationSummary* locations = conversion->GetLocations();
3945 Location out = locations->Out();
3946 Location in = locations->InAt(0);
3947 Primitive::Type result_type = conversion->GetResultType();
3948 Primitive::Type input_type = conversion->GetInputType();
3949 DCHECK_NE(result_type, input_type);
3950 switch (result_type) {
3951 case Primitive::kPrimByte:
3952 switch (input_type) {
3953 case Primitive::kPrimLong:
3954 // Type conversion from long to byte is a result of code transformations.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003955 __ Sbfx(OutputRegister(conversion), LowRegisterFrom(in), 0, 8);
Scott Wakelingfe885462016-09-22 10:24:38 +01003956 break;
3957 case Primitive::kPrimBoolean:
3958 // Boolean input is a result of code transformations.
3959 case Primitive::kPrimShort:
3960 case Primitive::kPrimInt:
3961 case Primitive::kPrimChar:
3962 // Processing a Dex `int-to-byte' instruction.
3963 __ Sbfx(OutputRegister(conversion), InputRegisterAt(conversion, 0), 0, 8);
3964 break;
3965
3966 default:
3967 LOG(FATAL) << "Unexpected type conversion from " << input_type
3968 << " to " << result_type;
3969 }
3970 break;
3971
3972 case Primitive::kPrimShort:
3973 switch (input_type) {
3974 case Primitive::kPrimLong:
3975 // Type conversion from long to short is a result of code transformations.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003976 __ Sbfx(OutputRegister(conversion), LowRegisterFrom(in), 0, 16);
Scott Wakelingfe885462016-09-22 10:24:38 +01003977 break;
3978 case Primitive::kPrimBoolean:
3979 // Boolean input is a result of code transformations.
3980 case Primitive::kPrimByte:
3981 case Primitive::kPrimInt:
3982 case Primitive::kPrimChar:
3983 // Processing a Dex `int-to-short' instruction.
3984 __ Sbfx(OutputRegister(conversion), InputRegisterAt(conversion, 0), 0, 16);
3985 break;
3986
3987 default:
3988 LOG(FATAL) << "Unexpected type conversion from " << input_type
3989 << " to " << result_type;
3990 }
3991 break;
3992
3993 case Primitive::kPrimInt:
3994 switch (input_type) {
3995 case Primitive::kPrimLong:
3996 // Processing a Dex `long-to-int' instruction.
3997 DCHECK(out.IsRegister());
3998 if (in.IsRegisterPair()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003999 __ Mov(OutputRegister(conversion), LowRegisterFrom(in));
Scott Wakelingfe885462016-09-22 10:24:38 +01004000 } else if (in.IsDoubleStackSlot()) {
4001 GetAssembler()->LoadFromOffset(kLoadWord,
4002 OutputRegister(conversion),
4003 sp,
4004 in.GetStackIndex());
4005 } else {
4006 DCHECK(in.IsConstant());
4007 DCHECK(in.GetConstant()->IsLongConstant());
Vladimir Markoba1a48e2017-04-13 11:50:14 +01004008 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
4009 __ Mov(OutputRegister(conversion), static_cast<int32_t>(value));
Scott Wakelingfe885462016-09-22 10:24:38 +01004010 }
4011 break;
4012
4013 case Primitive::kPrimFloat: {
4014 // Processing a Dex `float-to-int' instruction.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004015 vixl32::SRegister temp = LowSRegisterFrom(locations->GetTemp(0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01004016 __ Vcvt(S32, F32, temp, InputSRegisterAt(conversion, 0));
Scott Wakelingfe885462016-09-22 10:24:38 +01004017 __ Vmov(OutputRegister(conversion), temp);
4018 break;
4019 }
4020
4021 case Primitive::kPrimDouble: {
4022 // Processing a Dex `double-to-int' instruction.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004023 vixl32::SRegister temp_s = LowSRegisterFrom(locations->GetTemp(0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01004024 __ Vcvt(S32, F64, temp_s, DRegisterFrom(in));
Scott Wakelingfe885462016-09-22 10:24:38 +01004025 __ Vmov(OutputRegister(conversion), temp_s);
4026 break;
4027 }
4028
4029 default:
4030 LOG(FATAL) << "Unexpected type conversion from " << input_type
4031 << " to " << result_type;
4032 }
4033 break;
4034
4035 case Primitive::kPrimLong:
4036 switch (input_type) {
4037 case Primitive::kPrimBoolean:
4038 // Boolean input is a result of code transformations.
4039 case Primitive::kPrimByte:
4040 case Primitive::kPrimShort:
4041 case Primitive::kPrimInt:
4042 case Primitive::kPrimChar:
4043 // Processing a Dex `int-to-long' instruction.
4044 DCHECK(out.IsRegisterPair());
4045 DCHECK(in.IsRegister());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004046 __ Mov(LowRegisterFrom(out), InputRegisterAt(conversion, 0));
Scott Wakelingfe885462016-09-22 10:24:38 +01004047 // Sign extension.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004048 __ Asr(HighRegisterFrom(out), LowRegisterFrom(out), 31);
Scott Wakelingfe885462016-09-22 10:24:38 +01004049 break;
4050
4051 case Primitive::kPrimFloat:
4052 // Processing a Dex `float-to-long' instruction.
4053 codegen_->InvokeRuntime(kQuickF2l, conversion, conversion->GetDexPc());
4054 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
4055 break;
4056
4057 case Primitive::kPrimDouble:
4058 // Processing a Dex `double-to-long' instruction.
4059 codegen_->InvokeRuntime(kQuickD2l, conversion, conversion->GetDexPc());
4060 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
4061 break;
4062
4063 default:
4064 LOG(FATAL) << "Unexpected type conversion from " << input_type
4065 << " to " << result_type;
4066 }
4067 break;
4068
4069 case Primitive::kPrimChar:
4070 switch (input_type) {
4071 case Primitive::kPrimLong:
4072 // Type conversion from long to char is a result of code transformations.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004073 __ Ubfx(OutputRegister(conversion), LowRegisterFrom(in), 0, 16);
Scott Wakelingfe885462016-09-22 10:24:38 +01004074 break;
4075 case Primitive::kPrimBoolean:
4076 // Boolean input is a result of code transformations.
4077 case Primitive::kPrimByte:
4078 case Primitive::kPrimShort:
4079 case Primitive::kPrimInt:
4080 // Processing a Dex `int-to-char' instruction.
4081 __ Ubfx(OutputRegister(conversion), InputRegisterAt(conversion, 0), 0, 16);
4082 break;
4083
4084 default:
4085 LOG(FATAL) << "Unexpected type conversion from " << input_type
4086 << " to " << result_type;
4087 }
4088 break;
4089
4090 case Primitive::kPrimFloat:
4091 switch (input_type) {
4092 case Primitive::kPrimBoolean:
4093 // Boolean input is a result of code transformations.
4094 case Primitive::kPrimByte:
4095 case Primitive::kPrimShort:
4096 case Primitive::kPrimInt:
4097 case Primitive::kPrimChar: {
4098 // Processing a Dex `int-to-float' instruction.
4099 __ Vmov(OutputSRegister(conversion), InputRegisterAt(conversion, 0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01004100 __ Vcvt(F32, S32, OutputSRegister(conversion), OutputSRegister(conversion));
Scott Wakelingfe885462016-09-22 10:24:38 +01004101 break;
4102 }
4103
4104 case Primitive::kPrimLong:
4105 // Processing a Dex `long-to-float' instruction.
4106 codegen_->InvokeRuntime(kQuickL2f, conversion, conversion->GetDexPc());
4107 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
4108 break;
4109
4110 case Primitive::kPrimDouble:
4111 // Processing a Dex `double-to-float' instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01004112 __ Vcvt(F32, F64, OutputSRegister(conversion), DRegisterFrom(in));
Scott Wakelingfe885462016-09-22 10:24:38 +01004113 break;
4114
4115 default:
4116 LOG(FATAL) << "Unexpected type conversion from " << input_type
4117 << " to " << result_type;
4118 };
4119 break;
4120
4121 case Primitive::kPrimDouble:
4122 switch (input_type) {
4123 case Primitive::kPrimBoolean:
4124 // Boolean input is a result of code transformations.
4125 case Primitive::kPrimByte:
4126 case Primitive::kPrimShort:
4127 case Primitive::kPrimInt:
4128 case Primitive::kPrimChar: {
4129 // Processing a Dex `int-to-double' instruction.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004130 __ Vmov(LowSRegisterFrom(out), InputRegisterAt(conversion, 0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01004131 __ Vcvt(F64, S32, DRegisterFrom(out), LowSRegisterFrom(out));
Scott Wakelingfe885462016-09-22 10:24:38 +01004132 break;
4133 }
4134
4135 case Primitive::kPrimLong: {
4136 // Processing a Dex `long-to-double' instruction.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004137 vixl32::Register low = LowRegisterFrom(in);
4138 vixl32::Register high = HighRegisterFrom(in);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004139 vixl32::SRegister out_s = LowSRegisterFrom(out);
Scott Wakelingc34dba72016-10-03 10:14:44 +01004140 vixl32::DRegister out_d = DRegisterFrom(out);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004141 vixl32::SRegister temp_s = LowSRegisterFrom(locations->GetTemp(0));
Scott Wakelingc34dba72016-10-03 10:14:44 +01004142 vixl32::DRegister temp_d = DRegisterFrom(locations->GetTemp(0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01004143 vixl32::DRegister constant_d = DRegisterFrom(locations->GetTemp(1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004144
4145 // temp_d = int-to-double(high)
4146 __ Vmov(temp_s, high);
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01004147 __ Vcvt(F64, S32, temp_d, temp_s);
Scott Wakelingfe885462016-09-22 10:24:38 +01004148 // constant_d = k2Pow32EncodingForDouble
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004149 __ Vmov(constant_d, bit_cast<double, int64_t>(k2Pow32EncodingForDouble));
Scott Wakelingfe885462016-09-22 10:24:38 +01004150 // out_d = unsigned-to-double(low)
4151 __ Vmov(out_s, low);
4152 __ Vcvt(F64, U32, out_d, out_s);
4153 // out_d += temp_d * constant_d
4154 __ Vmla(F64, out_d, temp_d, constant_d);
4155 break;
4156 }
4157
4158 case Primitive::kPrimFloat:
4159 // Processing a Dex `float-to-double' instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01004160 __ Vcvt(F64, F32, DRegisterFrom(out), InputSRegisterAt(conversion, 0));
Scott Wakelingfe885462016-09-22 10:24:38 +01004161 break;
4162
4163 default:
4164 LOG(FATAL) << "Unexpected type conversion from " << input_type
4165 << " to " << result_type;
4166 };
4167 break;
4168
4169 default:
4170 LOG(FATAL) << "Unexpected type conversion from " << input_type
4171 << " to " << result_type;
4172 }
4173}
4174
4175void LocationsBuilderARMVIXL::VisitAdd(HAdd* add) {
4176 LocationSummary* locations =
4177 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
4178 switch (add->GetResultType()) {
4179 case Primitive::kPrimInt: {
4180 locations->SetInAt(0, Location::RequiresRegister());
4181 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
4182 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4183 break;
4184 }
4185
Scott Wakelingfe885462016-09-22 10:24:38 +01004186 case Primitive::kPrimLong: {
4187 locations->SetInAt(0, Location::RequiresRegister());
Anton Kirilovdda43962016-11-21 19:55:20 +00004188 locations->SetInAt(1, ArmEncodableConstantOrRegister(add->InputAt(1), ADD));
Scott Wakelingfe885462016-09-22 10:24:38 +01004189 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4190 break;
4191 }
4192
4193 case Primitive::kPrimFloat:
4194 case Primitive::kPrimDouble: {
4195 locations->SetInAt(0, Location::RequiresFpuRegister());
4196 locations->SetInAt(1, Location::RequiresFpuRegister());
4197 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4198 break;
4199 }
4200
4201 default:
4202 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
4203 }
4204}
4205
4206void InstructionCodeGeneratorARMVIXL::VisitAdd(HAdd* add) {
4207 LocationSummary* locations = add->GetLocations();
4208 Location out = locations->Out();
4209 Location first = locations->InAt(0);
4210 Location second = locations->InAt(1);
4211
4212 switch (add->GetResultType()) {
4213 case Primitive::kPrimInt: {
4214 __ Add(OutputRegister(add), InputRegisterAt(add, 0), InputOperandAt(add, 1));
4215 }
4216 break;
4217
Scott Wakelingfe885462016-09-22 10:24:38 +01004218 case Primitive::kPrimLong: {
Anton Kirilovdda43962016-11-21 19:55:20 +00004219 if (second.IsConstant()) {
4220 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
4221 GenerateAddLongConst(out, first, value);
4222 } else {
4223 DCHECK(second.IsRegisterPair());
4224 __ Adds(LowRegisterFrom(out), LowRegisterFrom(first), LowRegisterFrom(second));
4225 __ Adc(HighRegisterFrom(out), HighRegisterFrom(first), HighRegisterFrom(second));
4226 }
Scott Wakelingfe885462016-09-22 10:24:38 +01004227 break;
4228 }
4229
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004230 case Primitive::kPrimFloat:
Scott Wakelingfe885462016-09-22 10:24:38 +01004231 case Primitive::kPrimDouble:
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004232 __ Vadd(OutputVRegister(add), InputVRegisterAt(add, 0), InputVRegisterAt(add, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004233 break;
4234
4235 default:
4236 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
4237 }
4238}
4239
4240void LocationsBuilderARMVIXL::VisitSub(HSub* sub) {
4241 LocationSummary* locations =
4242 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
4243 switch (sub->GetResultType()) {
4244 case Primitive::kPrimInt: {
4245 locations->SetInAt(0, Location::RequiresRegister());
4246 locations->SetInAt(1, Location::RegisterOrConstant(sub->InputAt(1)));
4247 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4248 break;
4249 }
4250
Scott Wakelingfe885462016-09-22 10:24:38 +01004251 case Primitive::kPrimLong: {
4252 locations->SetInAt(0, Location::RequiresRegister());
Anton Kirilovdda43962016-11-21 19:55:20 +00004253 locations->SetInAt(1, ArmEncodableConstantOrRegister(sub->InputAt(1), SUB));
Scott Wakelingfe885462016-09-22 10:24:38 +01004254 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4255 break;
4256 }
4257 case Primitive::kPrimFloat:
4258 case Primitive::kPrimDouble: {
4259 locations->SetInAt(0, Location::RequiresFpuRegister());
4260 locations->SetInAt(1, Location::RequiresFpuRegister());
4261 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4262 break;
4263 }
4264 default:
4265 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
4266 }
4267}
4268
4269void InstructionCodeGeneratorARMVIXL::VisitSub(HSub* sub) {
4270 LocationSummary* locations = sub->GetLocations();
4271 Location out = locations->Out();
4272 Location first = locations->InAt(0);
4273 Location second = locations->InAt(1);
4274 switch (sub->GetResultType()) {
4275 case Primitive::kPrimInt: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004276 __ Sub(OutputRegister(sub), InputRegisterAt(sub, 0), InputOperandAt(sub, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004277 break;
4278 }
4279
Scott Wakelingfe885462016-09-22 10:24:38 +01004280 case Primitive::kPrimLong: {
Anton Kirilovdda43962016-11-21 19:55:20 +00004281 if (second.IsConstant()) {
4282 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
4283 GenerateAddLongConst(out, first, -value);
4284 } else {
4285 DCHECK(second.IsRegisterPair());
4286 __ Subs(LowRegisterFrom(out), LowRegisterFrom(first), LowRegisterFrom(second));
4287 __ Sbc(HighRegisterFrom(out), HighRegisterFrom(first), HighRegisterFrom(second));
4288 }
Scott Wakelingfe885462016-09-22 10:24:38 +01004289 break;
4290 }
4291
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004292 case Primitive::kPrimFloat:
4293 case Primitive::kPrimDouble:
4294 __ Vsub(OutputVRegister(sub), InputVRegisterAt(sub, 0), InputVRegisterAt(sub, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004295 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01004296
4297 default:
4298 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
4299 }
4300}
4301
4302void LocationsBuilderARMVIXL::VisitMul(HMul* mul) {
4303 LocationSummary* locations =
4304 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
4305 switch (mul->GetResultType()) {
4306 case Primitive::kPrimInt:
4307 case Primitive::kPrimLong: {
4308 locations->SetInAt(0, Location::RequiresRegister());
4309 locations->SetInAt(1, Location::RequiresRegister());
4310 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4311 break;
4312 }
4313
4314 case Primitive::kPrimFloat:
4315 case Primitive::kPrimDouble: {
4316 locations->SetInAt(0, Location::RequiresFpuRegister());
4317 locations->SetInAt(1, Location::RequiresFpuRegister());
4318 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4319 break;
4320 }
4321
4322 default:
4323 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4324 }
4325}
4326
4327void InstructionCodeGeneratorARMVIXL::VisitMul(HMul* mul) {
4328 LocationSummary* locations = mul->GetLocations();
4329 Location out = locations->Out();
4330 Location first = locations->InAt(0);
4331 Location second = locations->InAt(1);
4332 switch (mul->GetResultType()) {
4333 case Primitive::kPrimInt: {
4334 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
4335 break;
4336 }
4337 case Primitive::kPrimLong: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004338 vixl32::Register out_hi = HighRegisterFrom(out);
4339 vixl32::Register out_lo = LowRegisterFrom(out);
4340 vixl32::Register in1_hi = HighRegisterFrom(first);
4341 vixl32::Register in1_lo = LowRegisterFrom(first);
4342 vixl32::Register in2_hi = HighRegisterFrom(second);
4343 vixl32::Register in2_lo = LowRegisterFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01004344
4345 // Extra checks to protect caused by the existence of R1_R2.
4346 // The algorithm is wrong if out.hi is either in1.lo or in2.lo:
4347 // (e.g. in1=r0_r1, in2=r2_r3 and out=r1_r2);
Anton Kirilov644032c2016-12-06 17:51:43 +00004348 DCHECK(!out_hi.Is(in1_lo));
4349 DCHECK(!out_hi.Is(in2_lo));
Scott Wakelingfe885462016-09-22 10:24:38 +01004350
4351 // input: in1 - 64 bits, in2 - 64 bits
4352 // output: out
4353 // formula: out.hi : out.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
4354 // parts: out.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
4355 // parts: out.lo = (in1.lo * in2.lo)[31:0]
4356
4357 UseScratchRegisterScope temps(GetVIXLAssembler());
4358 vixl32::Register temp = temps.Acquire();
4359 // temp <- in1.lo * in2.hi
4360 __ Mul(temp, in1_lo, in2_hi);
4361 // out.hi <- in1.lo * in2.hi + in1.hi * in2.lo
4362 __ Mla(out_hi, in1_hi, in2_lo, temp);
4363 // out.lo <- (in1.lo * in2.lo)[31:0];
4364 __ Umull(out_lo, temp, in1_lo, in2_lo);
4365 // out.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004366 __ Add(out_hi, out_hi, temp);
Scott Wakelingfe885462016-09-22 10:24:38 +01004367 break;
4368 }
4369
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004370 case Primitive::kPrimFloat:
4371 case Primitive::kPrimDouble:
4372 __ Vmul(OutputVRegister(mul), InputVRegisterAt(mul, 0), InputVRegisterAt(mul, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004373 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01004374
4375 default:
4376 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4377 }
4378}
4379
Scott Wakelingfe885462016-09-22 10:24:38 +01004380void InstructionCodeGeneratorARMVIXL::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
4381 DCHECK(instruction->IsDiv() || instruction->IsRem());
4382 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
4383
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004384 Location second = instruction->GetLocations()->InAt(1);
Scott Wakelingfe885462016-09-22 10:24:38 +01004385 DCHECK(second.IsConstant());
4386
4387 vixl32::Register out = OutputRegister(instruction);
4388 vixl32::Register dividend = InputRegisterAt(instruction, 0);
Anton Kirilov644032c2016-12-06 17:51:43 +00004389 int32_t imm = Int32ConstantFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01004390 DCHECK(imm == 1 || imm == -1);
4391
4392 if (instruction->IsRem()) {
4393 __ Mov(out, 0);
4394 } else {
4395 if (imm == 1) {
4396 __ Mov(out, dividend);
4397 } else {
4398 __ Rsb(out, dividend, 0);
4399 }
4400 }
4401}
4402
4403void InstructionCodeGeneratorARMVIXL::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
4404 DCHECK(instruction->IsDiv() || instruction->IsRem());
4405 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
4406
4407 LocationSummary* locations = instruction->GetLocations();
4408 Location second = locations->InAt(1);
4409 DCHECK(second.IsConstant());
4410
4411 vixl32::Register out = OutputRegister(instruction);
4412 vixl32::Register dividend = InputRegisterAt(instruction, 0);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004413 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
Anton Kirilov644032c2016-12-06 17:51:43 +00004414 int32_t imm = Int32ConstantFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01004415 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
4416 int ctz_imm = CTZ(abs_imm);
4417
4418 if (ctz_imm == 1) {
4419 __ Lsr(temp, dividend, 32 - ctz_imm);
4420 } else {
4421 __ Asr(temp, dividend, 31);
4422 __ Lsr(temp, temp, 32 - ctz_imm);
4423 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004424 __ Add(out, temp, dividend);
Scott Wakelingfe885462016-09-22 10:24:38 +01004425
4426 if (instruction->IsDiv()) {
4427 __ Asr(out, out, ctz_imm);
4428 if (imm < 0) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004429 __ Rsb(out, out, 0);
Scott Wakelingfe885462016-09-22 10:24:38 +01004430 }
4431 } else {
4432 __ Ubfx(out, out, 0, ctz_imm);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004433 __ Sub(out, out, temp);
Scott Wakelingfe885462016-09-22 10:24:38 +01004434 }
4435}
4436
4437void InstructionCodeGeneratorARMVIXL::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
4438 DCHECK(instruction->IsDiv() || instruction->IsRem());
4439 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
4440
4441 LocationSummary* locations = instruction->GetLocations();
4442 Location second = locations->InAt(1);
4443 DCHECK(second.IsConstant());
4444
4445 vixl32::Register out = OutputRegister(instruction);
4446 vixl32::Register dividend = InputRegisterAt(instruction, 0);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004447 vixl32::Register temp1 = RegisterFrom(locations->GetTemp(0));
4448 vixl32::Register temp2 = RegisterFrom(locations->GetTemp(1));
Scott Wakelingb77051e2016-11-21 19:46:00 +00004449 int32_t imm = Int32ConstantFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01004450
4451 int64_t magic;
4452 int shift;
4453 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
4454
Anton Kirilovdda43962016-11-21 19:55:20 +00004455 // TODO(VIXL): Change the static cast to Operand::From() after VIXL is fixed.
4456 __ Mov(temp1, static_cast<int32_t>(magic));
Scott Wakelingfe885462016-09-22 10:24:38 +01004457 __ Smull(temp2, temp1, dividend, temp1);
4458
4459 if (imm > 0 && magic < 0) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004460 __ Add(temp1, temp1, dividend);
Scott Wakelingfe885462016-09-22 10:24:38 +01004461 } else if (imm < 0 && magic > 0) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004462 __ Sub(temp1, temp1, dividend);
Scott Wakelingfe885462016-09-22 10:24:38 +01004463 }
4464
4465 if (shift != 0) {
4466 __ Asr(temp1, temp1, shift);
4467 }
4468
4469 if (instruction->IsDiv()) {
4470 __ Sub(out, temp1, Operand(temp1, vixl32::Shift(ASR), 31));
4471 } else {
4472 __ Sub(temp1, temp1, Operand(temp1, vixl32::Shift(ASR), 31));
4473 // TODO: Strength reduction for mls.
4474 __ Mov(temp2, imm);
4475 __ Mls(out, temp1, temp2, dividend);
4476 }
4477}
4478
4479void InstructionCodeGeneratorARMVIXL::GenerateDivRemConstantIntegral(
4480 HBinaryOperation* instruction) {
4481 DCHECK(instruction->IsDiv() || instruction->IsRem());
4482 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
4483
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004484 Location second = instruction->GetLocations()->InAt(1);
Scott Wakelingfe885462016-09-22 10:24:38 +01004485 DCHECK(second.IsConstant());
4486
Anton Kirilov644032c2016-12-06 17:51:43 +00004487 int32_t imm = Int32ConstantFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01004488 if (imm == 0) {
4489 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
4490 } else if (imm == 1 || imm == -1) {
4491 DivRemOneOrMinusOne(instruction);
4492 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
4493 DivRemByPowerOfTwo(instruction);
4494 } else {
4495 DCHECK(imm <= -2 || imm >= 2);
4496 GenerateDivRemWithAnyConstant(instruction);
4497 }
4498}
4499
4500void LocationsBuilderARMVIXL::VisitDiv(HDiv* div) {
4501 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
4502 if (div->GetResultType() == Primitive::kPrimLong) {
4503 // pLdiv runtime call.
4504 call_kind = LocationSummary::kCallOnMainOnly;
4505 } else if (div->GetResultType() == Primitive::kPrimInt && div->InputAt(1)->IsConstant()) {
4506 // sdiv will be replaced by other instruction sequence.
4507 } else if (div->GetResultType() == Primitive::kPrimInt &&
4508 !codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4509 // pIdivmod runtime call.
4510 call_kind = LocationSummary::kCallOnMainOnly;
4511 }
4512
4513 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
4514
4515 switch (div->GetResultType()) {
4516 case Primitive::kPrimInt: {
4517 if (div->InputAt(1)->IsConstant()) {
4518 locations->SetInAt(0, Location::RequiresRegister());
4519 locations->SetInAt(1, Location::ConstantLocation(div->InputAt(1)->AsConstant()));
4520 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Anton Kirilov644032c2016-12-06 17:51:43 +00004521 int32_t value = Int32ConstantFrom(div->InputAt(1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004522 if (value == 1 || value == 0 || value == -1) {
4523 // No temp register required.
4524 } else {
4525 locations->AddTemp(Location::RequiresRegister());
4526 if (!IsPowerOfTwo(AbsOrMin(value))) {
4527 locations->AddTemp(Location::RequiresRegister());
4528 }
4529 }
4530 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4531 locations->SetInAt(0, Location::RequiresRegister());
4532 locations->SetInAt(1, Location::RequiresRegister());
4533 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4534 } else {
Artem Serov551b28f2016-10-18 19:11:30 +01004535 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4536 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4537 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
Roland Levillain5e8d5f02016-10-18 18:03:43 +01004538 // Note: divmod will compute both the quotient and the remainder as the pair R0 and R1, but
Artem Serov551b28f2016-10-18 19:11:30 +01004539 // we only need the former.
4540 locations->SetOut(LocationFrom(r0));
Scott Wakelingfe885462016-09-22 10:24:38 +01004541 }
4542 break;
4543 }
4544 case Primitive::kPrimLong: {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01004545 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4546 locations->SetInAt(0, LocationFrom(
4547 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
4548 locations->SetInAt(1, LocationFrom(
4549 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
4550 locations->SetOut(LocationFrom(r0, r1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004551 break;
4552 }
4553 case Primitive::kPrimFloat:
4554 case Primitive::kPrimDouble: {
4555 locations->SetInAt(0, Location::RequiresFpuRegister());
4556 locations->SetInAt(1, Location::RequiresFpuRegister());
4557 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4558 break;
4559 }
4560
4561 default:
4562 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
4563 }
4564}
4565
4566void InstructionCodeGeneratorARMVIXL::VisitDiv(HDiv* div) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01004567 Location lhs = div->GetLocations()->InAt(0);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004568 Location rhs = div->GetLocations()->InAt(1);
Scott Wakelingfe885462016-09-22 10:24:38 +01004569
4570 switch (div->GetResultType()) {
4571 case Primitive::kPrimInt: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004572 if (rhs.IsConstant()) {
Scott Wakelingfe885462016-09-22 10:24:38 +01004573 GenerateDivRemConstantIntegral(div);
4574 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4575 __ Sdiv(OutputRegister(div), InputRegisterAt(div, 0), InputRegisterAt(div, 1));
4576 } else {
Artem Serov551b28f2016-10-18 19:11:30 +01004577 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4578 DCHECK(calling_convention.GetRegisterAt(0).Is(RegisterFrom(lhs)));
4579 DCHECK(calling_convention.GetRegisterAt(1).Is(RegisterFrom(rhs)));
4580 DCHECK(r0.Is(OutputRegister(div)));
4581
4582 codegen_->InvokeRuntime(kQuickIdivmod, div, div->GetDexPc());
4583 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
Scott Wakelingfe885462016-09-22 10:24:38 +01004584 }
4585 break;
4586 }
4587
4588 case Primitive::kPrimLong: {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01004589 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4590 DCHECK(calling_convention.GetRegisterAt(0).Is(LowRegisterFrom(lhs)));
4591 DCHECK(calling_convention.GetRegisterAt(1).Is(HighRegisterFrom(lhs)));
4592 DCHECK(calling_convention.GetRegisterAt(2).Is(LowRegisterFrom(rhs)));
4593 DCHECK(calling_convention.GetRegisterAt(3).Is(HighRegisterFrom(rhs)));
4594 DCHECK(LowRegisterFrom(div->GetLocations()->Out()).Is(r0));
4595 DCHECK(HighRegisterFrom(div->GetLocations()->Out()).Is(r1));
4596
4597 codegen_->InvokeRuntime(kQuickLdiv, div, div->GetDexPc());
4598 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Scott Wakelingfe885462016-09-22 10:24:38 +01004599 break;
4600 }
4601
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004602 case Primitive::kPrimFloat:
4603 case Primitive::kPrimDouble:
4604 __ Vdiv(OutputVRegister(div), InputVRegisterAt(div, 0), InputVRegisterAt(div, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004605 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01004606
4607 default:
4608 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
4609 }
4610}
4611
Artem Serov551b28f2016-10-18 19:11:30 +01004612void LocationsBuilderARMVIXL::VisitRem(HRem* rem) {
4613 Primitive::Type type = rem->GetResultType();
4614
4615 // Most remainders are implemented in the runtime.
4616 LocationSummary::CallKind call_kind = LocationSummary::kCallOnMainOnly;
4617 if (rem->GetResultType() == Primitive::kPrimInt && rem->InputAt(1)->IsConstant()) {
4618 // sdiv will be replaced by other instruction sequence.
4619 call_kind = LocationSummary::kNoCall;
4620 } else if ((rem->GetResultType() == Primitive::kPrimInt)
4621 && codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4622 // Have hardware divide instruction for int, do it with three instructions.
4623 call_kind = LocationSummary::kNoCall;
4624 }
4625
4626 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
4627
4628 switch (type) {
4629 case Primitive::kPrimInt: {
4630 if (rem->InputAt(1)->IsConstant()) {
4631 locations->SetInAt(0, Location::RequiresRegister());
4632 locations->SetInAt(1, Location::ConstantLocation(rem->InputAt(1)->AsConstant()));
4633 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Anton Kirilov644032c2016-12-06 17:51:43 +00004634 int32_t value = Int32ConstantFrom(rem->InputAt(1));
Artem Serov551b28f2016-10-18 19:11:30 +01004635 if (value == 1 || value == 0 || value == -1) {
4636 // No temp register required.
4637 } else {
4638 locations->AddTemp(Location::RequiresRegister());
4639 if (!IsPowerOfTwo(AbsOrMin(value))) {
4640 locations->AddTemp(Location::RequiresRegister());
4641 }
4642 }
4643 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4644 locations->SetInAt(0, Location::RequiresRegister());
4645 locations->SetInAt(1, Location::RequiresRegister());
4646 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4647 locations->AddTemp(Location::RequiresRegister());
4648 } else {
4649 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4650 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4651 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
Roland Levillain5e8d5f02016-10-18 18:03:43 +01004652 // Note: divmod will compute both the quotient and the remainder as the pair R0 and R1, but
Artem Serov551b28f2016-10-18 19:11:30 +01004653 // we only need the latter.
4654 locations->SetOut(LocationFrom(r1));
4655 }
4656 break;
4657 }
4658 case Primitive::kPrimLong: {
4659 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4660 locations->SetInAt(0, LocationFrom(
4661 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
4662 locations->SetInAt(1, LocationFrom(
4663 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
4664 // The runtime helper puts the output in R2,R3.
4665 locations->SetOut(LocationFrom(r2, r3));
4666 break;
4667 }
4668 case Primitive::kPrimFloat: {
4669 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4670 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
4671 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
4672 locations->SetOut(LocationFrom(s0));
4673 break;
4674 }
4675
4676 case Primitive::kPrimDouble: {
4677 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4678 locations->SetInAt(0, LocationFrom(
4679 calling_convention.GetFpuRegisterAt(0), calling_convention.GetFpuRegisterAt(1)));
4680 locations->SetInAt(1, LocationFrom(
4681 calling_convention.GetFpuRegisterAt(2), calling_convention.GetFpuRegisterAt(3)));
4682 locations->SetOut(LocationFrom(s0, s1));
4683 break;
4684 }
4685
4686 default:
4687 LOG(FATAL) << "Unexpected rem type " << type;
4688 }
4689}
4690
4691void InstructionCodeGeneratorARMVIXL::VisitRem(HRem* rem) {
4692 LocationSummary* locations = rem->GetLocations();
4693 Location second = locations->InAt(1);
4694
4695 Primitive::Type type = rem->GetResultType();
4696 switch (type) {
4697 case Primitive::kPrimInt: {
4698 vixl32::Register reg1 = InputRegisterAt(rem, 0);
4699 vixl32::Register out_reg = OutputRegister(rem);
4700 if (second.IsConstant()) {
4701 GenerateDivRemConstantIntegral(rem);
4702 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4703 vixl32::Register reg2 = RegisterFrom(second);
4704 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
4705
4706 // temp = reg1 / reg2 (integer division)
4707 // dest = reg1 - temp * reg2
4708 __ Sdiv(temp, reg1, reg2);
4709 __ Mls(out_reg, temp, reg2, reg1);
4710 } else {
4711 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4712 DCHECK(reg1.Is(calling_convention.GetRegisterAt(0)));
4713 DCHECK(RegisterFrom(second).Is(calling_convention.GetRegisterAt(1)));
4714 DCHECK(out_reg.Is(r1));
4715
4716 codegen_->InvokeRuntime(kQuickIdivmod, rem, rem->GetDexPc());
4717 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
4718 }
4719 break;
4720 }
4721
4722 case Primitive::kPrimLong: {
4723 codegen_->InvokeRuntime(kQuickLmod, rem, rem->GetDexPc());
4724 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
4725 break;
4726 }
4727
4728 case Primitive::kPrimFloat: {
4729 codegen_->InvokeRuntime(kQuickFmodf, rem, rem->GetDexPc());
4730 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
4731 break;
4732 }
4733
4734 case Primitive::kPrimDouble: {
4735 codegen_->InvokeRuntime(kQuickFmod, rem, rem->GetDexPc());
4736 CheckEntrypointTypes<kQuickFmod, double, double, double>();
4737 break;
4738 }
4739
4740 default:
4741 LOG(FATAL) << "Unexpected rem type " << type;
4742 }
4743}
4744
4745
Scott Wakelingfe885462016-09-22 10:24:38 +01004746void LocationsBuilderARMVIXL::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Artem Serov657022c2016-11-23 14:19:38 +00004747 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Scott Wakelingfe885462016-09-22 10:24:38 +01004748 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Scott Wakelingfe885462016-09-22 10:24:38 +01004749}
4750
4751void InstructionCodeGeneratorARMVIXL::VisitDivZeroCheck(HDivZeroCheck* instruction) {
4752 DivZeroCheckSlowPathARMVIXL* slow_path =
4753 new (GetGraph()->GetArena()) DivZeroCheckSlowPathARMVIXL(instruction);
4754 codegen_->AddSlowPath(slow_path);
4755
4756 LocationSummary* locations = instruction->GetLocations();
4757 Location value = locations->InAt(0);
4758
4759 switch (instruction->GetType()) {
4760 case Primitive::kPrimBoolean:
4761 case Primitive::kPrimByte:
4762 case Primitive::kPrimChar:
4763 case Primitive::kPrimShort:
4764 case Primitive::kPrimInt: {
4765 if (value.IsRegister()) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00004766 __ CompareAndBranchIfZero(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
Scott Wakelingfe885462016-09-22 10:24:38 +01004767 } else {
4768 DCHECK(value.IsConstant()) << value;
Anton Kirilov644032c2016-12-06 17:51:43 +00004769 if (Int32ConstantFrom(value) == 0) {
Scott Wakelingfe885462016-09-22 10:24:38 +01004770 __ B(slow_path->GetEntryLabel());
4771 }
4772 }
4773 break;
4774 }
4775 case Primitive::kPrimLong: {
4776 if (value.IsRegisterPair()) {
4777 UseScratchRegisterScope temps(GetVIXLAssembler());
4778 vixl32::Register temp = temps.Acquire();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004779 __ Orrs(temp, LowRegisterFrom(value), HighRegisterFrom(value));
Scott Wakelingfe885462016-09-22 10:24:38 +01004780 __ B(eq, slow_path->GetEntryLabel());
4781 } else {
4782 DCHECK(value.IsConstant()) << value;
Anton Kirilov644032c2016-12-06 17:51:43 +00004783 if (Int64ConstantFrom(value) == 0) {
Scott Wakelingfe885462016-09-22 10:24:38 +01004784 __ B(slow_path->GetEntryLabel());
4785 }
4786 }
4787 break;
4788 }
4789 default:
4790 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
4791 }
4792}
4793
Artem Serov02109dd2016-09-23 17:17:54 +01004794void InstructionCodeGeneratorARMVIXL::HandleIntegerRotate(HRor* ror) {
4795 LocationSummary* locations = ror->GetLocations();
4796 vixl32::Register in = InputRegisterAt(ror, 0);
4797 Location rhs = locations->InAt(1);
4798 vixl32::Register out = OutputRegister(ror);
4799
4800 if (rhs.IsConstant()) {
4801 // Arm32 and Thumb2 assemblers require a rotation on the interval [1,31],
4802 // so map all rotations to a +ve. equivalent in that range.
4803 // (e.g. left *or* right by -2 bits == 30 bits in the same direction.)
4804 uint32_t rot = CodeGenerator::GetInt32ValueOf(rhs.GetConstant()) & 0x1F;
4805 if (rot) {
4806 // Rotate, mapping left rotations to right equivalents if necessary.
4807 // (e.g. left by 2 bits == right by 30.)
4808 __ Ror(out, in, rot);
4809 } else if (!out.Is(in)) {
4810 __ Mov(out, in);
4811 }
4812 } else {
4813 __ Ror(out, in, RegisterFrom(rhs));
4814 }
4815}
4816
4817// Gain some speed by mapping all Long rotates onto equivalent pairs of Integer
4818// rotates by swapping input regs (effectively rotating by the first 32-bits of
4819// a larger rotation) or flipping direction (thus treating larger right/left
4820// rotations as sub-word sized rotations in the other direction) as appropriate.
4821void InstructionCodeGeneratorARMVIXL::HandleLongRotate(HRor* ror) {
4822 LocationSummary* locations = ror->GetLocations();
4823 vixl32::Register in_reg_lo = LowRegisterFrom(locations->InAt(0));
4824 vixl32::Register in_reg_hi = HighRegisterFrom(locations->InAt(0));
4825 Location rhs = locations->InAt(1);
4826 vixl32::Register out_reg_lo = LowRegisterFrom(locations->Out());
4827 vixl32::Register out_reg_hi = HighRegisterFrom(locations->Out());
4828
4829 if (rhs.IsConstant()) {
4830 uint64_t rot = CodeGenerator::GetInt64ValueOf(rhs.GetConstant());
4831 // Map all rotations to +ve. equivalents on the interval [0,63].
4832 rot &= kMaxLongShiftDistance;
4833 // For rotates over a word in size, 'pre-rotate' by 32-bits to keep rotate
4834 // logic below to a simple pair of binary orr.
4835 // (e.g. 34 bits == in_reg swap + 2 bits right.)
4836 if (rot >= kArmBitsPerWord) {
4837 rot -= kArmBitsPerWord;
4838 std::swap(in_reg_hi, in_reg_lo);
4839 }
4840 // Rotate, or mov to out for zero or word size rotations.
4841 if (rot != 0u) {
Scott Wakelingb77051e2016-11-21 19:46:00 +00004842 __ Lsr(out_reg_hi, in_reg_hi, Operand::From(rot));
Artem Serov02109dd2016-09-23 17:17:54 +01004843 __ Orr(out_reg_hi, out_reg_hi, Operand(in_reg_lo, ShiftType::LSL, kArmBitsPerWord - rot));
Scott Wakelingb77051e2016-11-21 19:46:00 +00004844 __ Lsr(out_reg_lo, in_reg_lo, Operand::From(rot));
Artem Serov02109dd2016-09-23 17:17:54 +01004845 __ Orr(out_reg_lo, out_reg_lo, Operand(in_reg_hi, ShiftType::LSL, kArmBitsPerWord - rot));
4846 } else {
4847 __ Mov(out_reg_lo, in_reg_lo);
4848 __ Mov(out_reg_hi, in_reg_hi);
4849 }
4850 } else {
4851 vixl32::Register shift_right = RegisterFrom(locations->GetTemp(0));
4852 vixl32::Register shift_left = RegisterFrom(locations->GetTemp(1));
4853 vixl32::Label end;
4854 vixl32::Label shift_by_32_plus_shift_right;
Anton Kirilov6f644202017-02-27 18:29:45 +00004855 vixl32::Label* final_label = codegen_->GetFinalLabel(ror, &end);
Artem Serov02109dd2016-09-23 17:17:54 +01004856
4857 __ And(shift_right, RegisterFrom(rhs), 0x1F);
4858 __ Lsrs(shift_left, RegisterFrom(rhs), 6);
Scott Wakelingbffdc702016-12-07 17:46:03 +00004859 __ Rsb(LeaveFlags, shift_left, shift_right, Operand::From(kArmBitsPerWord));
Artem Serov517d9f62016-12-12 15:51:15 +00004860 __ B(cc, &shift_by_32_plus_shift_right, /* far_target */ false);
Artem Serov02109dd2016-09-23 17:17:54 +01004861
4862 // out_reg_hi = (reg_hi << shift_left) | (reg_lo >> shift_right).
4863 // out_reg_lo = (reg_lo << shift_left) | (reg_hi >> shift_right).
4864 __ Lsl(out_reg_hi, in_reg_hi, shift_left);
4865 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
4866 __ Add(out_reg_hi, out_reg_hi, out_reg_lo);
4867 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
4868 __ Lsr(shift_left, in_reg_hi, shift_right);
4869 __ Add(out_reg_lo, out_reg_lo, shift_left);
Anton Kirilov6f644202017-02-27 18:29:45 +00004870 __ B(final_label);
Artem Serov02109dd2016-09-23 17:17:54 +01004871
4872 __ Bind(&shift_by_32_plus_shift_right); // Shift by 32+shift_right.
4873 // out_reg_hi = (reg_hi >> shift_right) | (reg_lo << shift_left).
4874 // out_reg_lo = (reg_lo >> shift_right) | (reg_hi << shift_left).
4875 __ Lsr(out_reg_hi, in_reg_hi, shift_right);
4876 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
4877 __ Add(out_reg_hi, out_reg_hi, out_reg_lo);
4878 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
4879 __ Lsl(shift_right, in_reg_hi, shift_left);
4880 __ Add(out_reg_lo, out_reg_lo, shift_right);
4881
Anton Kirilov6f644202017-02-27 18:29:45 +00004882 if (end.IsReferenced()) {
4883 __ Bind(&end);
4884 }
Artem Serov02109dd2016-09-23 17:17:54 +01004885 }
4886}
4887
4888void LocationsBuilderARMVIXL::VisitRor(HRor* ror) {
4889 LocationSummary* locations =
4890 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
4891 switch (ror->GetResultType()) {
4892 case Primitive::kPrimInt: {
4893 locations->SetInAt(0, Location::RequiresRegister());
4894 locations->SetInAt(1, Location::RegisterOrConstant(ror->InputAt(1)));
4895 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4896 break;
4897 }
4898 case Primitive::kPrimLong: {
4899 locations->SetInAt(0, Location::RequiresRegister());
4900 if (ror->InputAt(1)->IsConstant()) {
4901 locations->SetInAt(1, Location::ConstantLocation(ror->InputAt(1)->AsConstant()));
4902 } else {
4903 locations->SetInAt(1, Location::RequiresRegister());
4904 locations->AddTemp(Location::RequiresRegister());
4905 locations->AddTemp(Location::RequiresRegister());
4906 }
4907 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
4908 break;
4909 }
4910 default:
4911 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
4912 }
4913}
4914
4915void InstructionCodeGeneratorARMVIXL::VisitRor(HRor* ror) {
4916 Primitive::Type type = ror->GetResultType();
4917 switch (type) {
4918 case Primitive::kPrimInt: {
4919 HandleIntegerRotate(ror);
4920 break;
4921 }
4922 case Primitive::kPrimLong: {
4923 HandleLongRotate(ror);
4924 break;
4925 }
4926 default:
4927 LOG(FATAL) << "Unexpected operation type " << type;
4928 UNREACHABLE();
4929 }
4930}
4931
Artem Serov02d37832016-10-25 15:25:33 +01004932void LocationsBuilderARMVIXL::HandleShift(HBinaryOperation* op) {
4933 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
4934
4935 LocationSummary* locations =
4936 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
4937
4938 switch (op->GetResultType()) {
4939 case Primitive::kPrimInt: {
4940 locations->SetInAt(0, Location::RequiresRegister());
4941 if (op->InputAt(1)->IsConstant()) {
4942 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
4943 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4944 } else {
4945 locations->SetInAt(1, Location::RequiresRegister());
4946 // Make the output overlap, as it will be used to hold the masked
4947 // second input.
4948 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
4949 }
4950 break;
4951 }
4952 case Primitive::kPrimLong: {
4953 locations->SetInAt(0, Location::RequiresRegister());
4954 if (op->InputAt(1)->IsConstant()) {
4955 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
4956 // For simplicity, use kOutputOverlap even though we only require that low registers
4957 // don't clash with high registers which the register allocator currently guarantees.
4958 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
4959 } else {
4960 locations->SetInAt(1, Location::RequiresRegister());
4961 locations->AddTemp(Location::RequiresRegister());
4962 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
4963 }
4964 break;
4965 }
4966 default:
4967 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
4968 }
4969}
4970
4971void InstructionCodeGeneratorARMVIXL::HandleShift(HBinaryOperation* op) {
4972 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
4973
4974 LocationSummary* locations = op->GetLocations();
4975 Location out = locations->Out();
4976 Location first = locations->InAt(0);
4977 Location second = locations->InAt(1);
4978
4979 Primitive::Type type = op->GetResultType();
4980 switch (type) {
4981 case Primitive::kPrimInt: {
4982 vixl32::Register out_reg = OutputRegister(op);
4983 vixl32::Register first_reg = InputRegisterAt(op, 0);
4984 if (second.IsRegister()) {
4985 vixl32::Register second_reg = RegisterFrom(second);
4986 // ARM doesn't mask the shift count so we need to do it ourselves.
4987 __ And(out_reg, second_reg, kMaxIntShiftDistance);
4988 if (op->IsShl()) {
4989 __ Lsl(out_reg, first_reg, out_reg);
4990 } else if (op->IsShr()) {
4991 __ Asr(out_reg, first_reg, out_reg);
4992 } else {
4993 __ Lsr(out_reg, first_reg, out_reg);
4994 }
4995 } else {
Anton Kirilov644032c2016-12-06 17:51:43 +00004996 int32_t cst = Int32ConstantFrom(second);
Artem Serov02d37832016-10-25 15:25:33 +01004997 uint32_t shift_value = cst & kMaxIntShiftDistance;
4998 if (shift_value == 0) { // ARM does not support shifting with 0 immediate.
4999 __ Mov(out_reg, first_reg);
5000 } else if (op->IsShl()) {
5001 __ Lsl(out_reg, first_reg, shift_value);
5002 } else if (op->IsShr()) {
5003 __ Asr(out_reg, first_reg, shift_value);
5004 } else {
5005 __ Lsr(out_reg, first_reg, shift_value);
5006 }
5007 }
5008 break;
5009 }
5010 case Primitive::kPrimLong: {
5011 vixl32::Register o_h = HighRegisterFrom(out);
5012 vixl32::Register o_l = LowRegisterFrom(out);
5013
5014 vixl32::Register high = HighRegisterFrom(first);
5015 vixl32::Register low = LowRegisterFrom(first);
5016
5017 if (second.IsRegister()) {
5018 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
5019
5020 vixl32::Register second_reg = RegisterFrom(second);
5021
5022 if (op->IsShl()) {
5023 __ And(o_l, second_reg, kMaxLongShiftDistance);
5024 // Shift the high part
5025 __ Lsl(o_h, high, o_l);
5026 // Shift the low part and `or` what overflew on the high part
Scott Wakelingb77051e2016-11-21 19:46:00 +00005027 __ Rsb(temp, o_l, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01005028 __ Lsr(temp, low, temp);
5029 __ Orr(o_h, o_h, temp);
5030 // If the shift is > 32 bits, override the high part
Scott Wakelingb77051e2016-11-21 19:46:00 +00005031 __ Subs(temp, o_l, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01005032 {
Artem Serov0fb37192016-12-06 18:13:40 +00005033 ExactAssemblyScope guard(GetVIXLAssembler(),
5034 2 * vixl32::kMaxInstructionSizeInBytes,
5035 CodeBufferCheckScope::kMaximumSize);
Artem Serov02d37832016-10-25 15:25:33 +01005036 __ it(pl);
5037 __ lsl(pl, o_h, low, temp);
5038 }
5039 // Shift the low part
5040 __ Lsl(o_l, low, o_l);
5041 } else if (op->IsShr()) {
5042 __ And(o_h, second_reg, kMaxLongShiftDistance);
5043 // Shift the low part
5044 __ Lsr(o_l, low, o_h);
5045 // Shift the high part and `or` what underflew on the low part
Scott Wakelingb77051e2016-11-21 19:46:00 +00005046 __ Rsb(temp, o_h, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01005047 __ Lsl(temp, high, temp);
5048 __ Orr(o_l, o_l, temp);
5049 // If the shift is > 32 bits, override the low part
Scott Wakelingb77051e2016-11-21 19:46:00 +00005050 __ Subs(temp, o_h, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01005051 {
Artem Serov0fb37192016-12-06 18:13:40 +00005052 ExactAssemblyScope guard(GetVIXLAssembler(),
5053 2 * vixl32::kMaxInstructionSizeInBytes,
5054 CodeBufferCheckScope::kMaximumSize);
Artem Serov02d37832016-10-25 15:25:33 +01005055 __ it(pl);
5056 __ asr(pl, o_l, high, temp);
5057 }
5058 // Shift the high part
5059 __ Asr(o_h, high, o_h);
5060 } else {
5061 __ And(o_h, second_reg, kMaxLongShiftDistance);
5062 // same as Shr except we use `Lsr`s and not `Asr`s
5063 __ Lsr(o_l, low, o_h);
Scott Wakelingb77051e2016-11-21 19:46:00 +00005064 __ Rsb(temp, o_h, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01005065 __ Lsl(temp, high, temp);
5066 __ Orr(o_l, o_l, temp);
Scott Wakelingb77051e2016-11-21 19:46:00 +00005067 __ Subs(temp, o_h, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01005068 {
Artem Serov0fb37192016-12-06 18:13:40 +00005069 ExactAssemblyScope guard(GetVIXLAssembler(),
5070 2 * vixl32::kMaxInstructionSizeInBytes,
5071 CodeBufferCheckScope::kMaximumSize);
Artem Serov02d37832016-10-25 15:25:33 +01005072 __ it(pl);
5073 __ lsr(pl, o_l, high, temp);
5074 }
5075 __ Lsr(o_h, high, o_h);
5076 }
5077 } else {
5078 // Register allocator doesn't create partial overlap.
5079 DCHECK(!o_l.Is(high));
5080 DCHECK(!o_h.Is(low));
Anton Kirilov644032c2016-12-06 17:51:43 +00005081 int32_t cst = Int32ConstantFrom(second);
Artem Serov02d37832016-10-25 15:25:33 +01005082 uint32_t shift_value = cst & kMaxLongShiftDistance;
5083 if (shift_value > 32) {
5084 if (op->IsShl()) {
5085 __ Lsl(o_h, low, shift_value - 32);
5086 __ Mov(o_l, 0);
5087 } else if (op->IsShr()) {
5088 __ Asr(o_l, high, shift_value - 32);
5089 __ Asr(o_h, high, 31);
5090 } else {
5091 __ Lsr(o_l, high, shift_value - 32);
5092 __ Mov(o_h, 0);
5093 }
5094 } else if (shift_value == 32) {
5095 if (op->IsShl()) {
5096 __ Mov(o_h, low);
5097 __ Mov(o_l, 0);
5098 } else if (op->IsShr()) {
5099 __ Mov(o_l, high);
5100 __ Asr(o_h, high, 31);
5101 } else {
5102 __ Mov(o_l, high);
5103 __ Mov(o_h, 0);
5104 }
5105 } else if (shift_value == 1) {
5106 if (op->IsShl()) {
5107 __ Lsls(o_l, low, 1);
5108 __ Adc(o_h, high, high);
5109 } else if (op->IsShr()) {
5110 __ Asrs(o_h, high, 1);
5111 __ Rrx(o_l, low);
5112 } else {
5113 __ Lsrs(o_h, high, 1);
5114 __ Rrx(o_l, low);
5115 }
5116 } else {
5117 DCHECK(2 <= shift_value && shift_value < 32) << shift_value;
5118 if (op->IsShl()) {
5119 __ Lsl(o_h, high, shift_value);
5120 __ Orr(o_h, o_h, Operand(low, ShiftType::LSR, 32 - shift_value));
5121 __ Lsl(o_l, low, shift_value);
5122 } else if (op->IsShr()) {
5123 __ Lsr(o_l, low, shift_value);
5124 __ Orr(o_l, o_l, Operand(high, ShiftType::LSL, 32 - shift_value));
5125 __ Asr(o_h, high, shift_value);
5126 } else {
5127 __ Lsr(o_l, low, shift_value);
5128 __ Orr(o_l, o_l, Operand(high, ShiftType::LSL, 32 - shift_value));
5129 __ Lsr(o_h, high, shift_value);
5130 }
5131 }
5132 }
5133 break;
5134 }
5135 default:
5136 LOG(FATAL) << "Unexpected operation type " << type;
5137 UNREACHABLE();
5138 }
5139}
5140
5141void LocationsBuilderARMVIXL::VisitShl(HShl* shl) {
5142 HandleShift(shl);
5143}
5144
5145void InstructionCodeGeneratorARMVIXL::VisitShl(HShl* shl) {
5146 HandleShift(shl);
5147}
5148
5149void LocationsBuilderARMVIXL::VisitShr(HShr* shr) {
5150 HandleShift(shr);
5151}
5152
5153void InstructionCodeGeneratorARMVIXL::VisitShr(HShr* shr) {
5154 HandleShift(shr);
5155}
5156
5157void LocationsBuilderARMVIXL::VisitUShr(HUShr* ushr) {
5158 HandleShift(ushr);
5159}
5160
5161void InstructionCodeGeneratorARMVIXL::VisitUShr(HUShr* ushr) {
5162 HandleShift(ushr);
5163}
5164
5165void LocationsBuilderARMVIXL::VisitNewInstance(HNewInstance* instruction) {
5166 LocationSummary* locations =
5167 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
5168 if (instruction->IsStringAlloc()) {
5169 locations->AddTemp(LocationFrom(kMethodRegister));
5170 } else {
5171 InvokeRuntimeCallingConventionARMVIXL calling_convention;
5172 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
Artem Serov02d37832016-10-25 15:25:33 +01005173 }
5174 locations->SetOut(LocationFrom(r0));
5175}
5176
5177void InstructionCodeGeneratorARMVIXL::VisitNewInstance(HNewInstance* instruction) {
5178 // Note: if heap poisoning is enabled, the entry point takes cares
5179 // of poisoning the reference.
5180 if (instruction->IsStringAlloc()) {
5181 // String is allocated through StringFactory. Call NewEmptyString entry point.
5182 vixl32::Register temp = RegisterFrom(instruction->GetLocations()->GetTemp(0));
5183 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize);
5184 GetAssembler()->LoadFromOffset(kLoadWord, temp, tr, QUICK_ENTRY_POINT(pNewEmptyString));
5185 GetAssembler()->LoadFromOffset(kLoadWord, lr, temp, code_offset.Int32Value());
Alexandre Rames374ddf32016-11-04 10:40:49 +00005186 // 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 +00005187 ExactAssemblyScope aas(GetVIXLAssembler(),
5188 vixl32::k16BitT32InstructionSizeInBytes,
5189 CodeBufferCheckScope::kExactSize);
Artem Serov02d37832016-10-25 15:25:33 +01005190 __ blx(lr);
5191 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
5192 } else {
5193 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00005194 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
Artem Serov02d37832016-10-25 15:25:33 +01005195 }
Roland Levillain5daa4952017-07-03 17:23:56 +01005196 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 10);
Artem Serov02d37832016-10-25 15:25:33 +01005197}
5198
5199void LocationsBuilderARMVIXL::VisitNewArray(HNewArray* instruction) {
5200 LocationSummary* locations =
5201 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
5202 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Artem Serov02d37832016-10-25 15:25:33 +01005203 locations->SetOut(LocationFrom(r0));
Nicolas Geoffray8c7c4f12017-01-26 10:13:11 +00005204 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5205 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
Artem Serov02d37832016-10-25 15:25:33 +01005206}
5207
5208void InstructionCodeGeneratorARMVIXL::VisitNewArray(HNewArray* instruction) {
Artem Serov02d37832016-10-25 15:25:33 +01005209 // Note: if heap poisoning is enabled, the entry point takes cares
5210 // of poisoning the reference.
Artem Serov7b3672e2017-02-03 17:30:34 +00005211 QuickEntrypointEnum entrypoint =
5212 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
5213 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005214 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Artem Serov7b3672e2017-02-03 17:30:34 +00005215 DCHECK(!codegen_->IsLeafMethod());
Roland Levillain5daa4952017-07-03 17:23:56 +01005216 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 11);
Artem Serov02d37832016-10-25 15:25:33 +01005217}
5218
5219void LocationsBuilderARMVIXL::VisitParameterValue(HParameterValue* instruction) {
5220 LocationSummary* locations =
5221 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5222 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
5223 if (location.IsStackSlot()) {
5224 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5225 } else if (location.IsDoubleStackSlot()) {
5226 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5227 }
5228 locations->SetOut(location);
5229}
5230
5231void InstructionCodeGeneratorARMVIXL::VisitParameterValue(
5232 HParameterValue* instruction ATTRIBUTE_UNUSED) {
5233 // Nothing to do, the parameter is already at its location.
5234}
5235
5236void LocationsBuilderARMVIXL::VisitCurrentMethod(HCurrentMethod* instruction) {
5237 LocationSummary* locations =
5238 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5239 locations->SetOut(LocationFrom(kMethodRegister));
5240}
5241
5242void InstructionCodeGeneratorARMVIXL::VisitCurrentMethod(
5243 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
5244 // Nothing to do, the method is already at its location.
5245}
5246
5247void LocationsBuilderARMVIXL::VisitNot(HNot* not_) {
5248 LocationSummary* locations =
5249 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
5250 locations->SetInAt(0, Location::RequiresRegister());
5251 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5252}
5253
5254void InstructionCodeGeneratorARMVIXL::VisitNot(HNot* not_) {
5255 LocationSummary* locations = not_->GetLocations();
5256 Location out = locations->Out();
5257 Location in = locations->InAt(0);
5258 switch (not_->GetResultType()) {
5259 case Primitive::kPrimInt:
5260 __ Mvn(OutputRegister(not_), InputRegisterAt(not_, 0));
5261 break;
5262
5263 case Primitive::kPrimLong:
5264 __ Mvn(LowRegisterFrom(out), LowRegisterFrom(in));
5265 __ Mvn(HighRegisterFrom(out), HighRegisterFrom(in));
5266 break;
5267
5268 default:
5269 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
5270 }
5271}
5272
Scott Wakelingc34dba72016-10-03 10:14:44 +01005273void LocationsBuilderARMVIXL::VisitBooleanNot(HBooleanNot* bool_not) {
5274 LocationSummary* locations =
5275 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
5276 locations->SetInAt(0, Location::RequiresRegister());
5277 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5278}
5279
5280void InstructionCodeGeneratorARMVIXL::VisitBooleanNot(HBooleanNot* bool_not) {
5281 __ Eor(OutputRegister(bool_not), InputRegister(bool_not), 1);
5282}
5283
Artem Serov02d37832016-10-25 15:25:33 +01005284void LocationsBuilderARMVIXL::VisitCompare(HCompare* compare) {
5285 LocationSummary* locations =
5286 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
5287 switch (compare->InputAt(0)->GetType()) {
5288 case Primitive::kPrimBoolean:
5289 case Primitive::kPrimByte:
5290 case Primitive::kPrimShort:
5291 case Primitive::kPrimChar:
5292 case Primitive::kPrimInt:
5293 case Primitive::kPrimLong: {
5294 locations->SetInAt(0, Location::RequiresRegister());
5295 locations->SetInAt(1, Location::RequiresRegister());
5296 // Output overlaps because it is written before doing the low comparison.
5297 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
5298 break;
5299 }
5300 case Primitive::kPrimFloat:
5301 case Primitive::kPrimDouble: {
5302 locations->SetInAt(0, Location::RequiresFpuRegister());
5303 locations->SetInAt(1, ArithmeticZeroOrFpuRegister(compare->InputAt(1)));
5304 locations->SetOut(Location::RequiresRegister());
5305 break;
5306 }
5307 default:
5308 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
5309 }
5310}
5311
5312void InstructionCodeGeneratorARMVIXL::VisitCompare(HCompare* compare) {
5313 LocationSummary* locations = compare->GetLocations();
5314 vixl32::Register out = OutputRegister(compare);
5315 Location left = locations->InAt(0);
5316 Location right = locations->InAt(1);
5317
5318 vixl32::Label less, greater, done;
Anton Kirilov6f644202017-02-27 18:29:45 +00005319 vixl32::Label* final_label = codegen_->GetFinalLabel(compare, &done);
Artem Serov02d37832016-10-25 15:25:33 +01005320 Primitive::Type type = compare->InputAt(0)->GetType();
5321 vixl32::Condition less_cond = vixl32::Condition(kNone);
5322 switch (type) {
5323 case Primitive::kPrimBoolean:
5324 case Primitive::kPrimByte:
5325 case Primitive::kPrimShort:
5326 case Primitive::kPrimChar:
5327 case Primitive::kPrimInt: {
5328 // Emit move to `out` before the `Cmp`, as `Mov` might affect the status flags.
5329 __ Mov(out, 0);
5330 __ Cmp(RegisterFrom(left), RegisterFrom(right)); // Signed compare.
5331 less_cond = lt;
5332 break;
5333 }
5334 case Primitive::kPrimLong: {
5335 __ Cmp(HighRegisterFrom(left), HighRegisterFrom(right)); // Signed compare.
Artem Serov517d9f62016-12-12 15:51:15 +00005336 __ B(lt, &less, /* far_target */ false);
5337 __ B(gt, &greater, /* far_target */ false);
Artem Serov02d37832016-10-25 15:25:33 +01005338 // Emit move to `out` before the last `Cmp`, as `Mov` might affect the status flags.
5339 __ Mov(out, 0);
5340 __ Cmp(LowRegisterFrom(left), LowRegisterFrom(right)); // Unsigned compare.
5341 less_cond = lo;
5342 break;
5343 }
5344 case Primitive::kPrimFloat:
5345 case Primitive::kPrimDouble: {
5346 __ Mov(out, 0);
Donghui Bai426b49c2016-11-08 14:55:38 +08005347 GenerateVcmp(compare, codegen_);
Artem Serov02d37832016-10-25 15:25:33 +01005348 // To branch on the FP compare result we transfer FPSCR to APSR (encoded as PC in VMRS).
5349 __ Vmrs(RegisterOrAPSR_nzcv(kPcCode), FPSCR);
5350 less_cond = ARMFPCondition(kCondLT, compare->IsGtBias());
5351 break;
5352 }
5353 default:
5354 LOG(FATAL) << "Unexpected compare type " << type;
5355 UNREACHABLE();
5356 }
5357
Anton Kirilov6f644202017-02-27 18:29:45 +00005358 __ B(eq, final_label, /* far_target */ false);
Artem Serov517d9f62016-12-12 15:51:15 +00005359 __ B(less_cond, &less, /* far_target */ false);
Artem Serov02d37832016-10-25 15:25:33 +01005360
5361 __ Bind(&greater);
5362 __ Mov(out, 1);
Anton Kirilov6f644202017-02-27 18:29:45 +00005363 __ B(final_label);
Artem Serov02d37832016-10-25 15:25:33 +01005364
5365 __ Bind(&less);
5366 __ Mov(out, -1);
5367
Anton Kirilov6f644202017-02-27 18:29:45 +00005368 if (done.IsReferenced()) {
5369 __ Bind(&done);
5370 }
Artem Serov02d37832016-10-25 15:25:33 +01005371}
5372
5373void LocationsBuilderARMVIXL::VisitPhi(HPhi* instruction) {
5374 LocationSummary* locations =
5375 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5376 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
5377 locations->SetInAt(i, Location::Any());
5378 }
5379 locations->SetOut(Location::Any());
5380}
5381
5382void InstructionCodeGeneratorARMVIXL::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
5383 LOG(FATAL) << "Unreachable";
5384}
5385
5386void CodeGeneratorARMVIXL::GenerateMemoryBarrier(MemBarrierKind kind) {
5387 // TODO (ported from quick): revisit ARM barrier kinds.
5388 DmbOptions flavor = DmbOptions::ISH; // Quiet C++ warnings.
5389 switch (kind) {
5390 case MemBarrierKind::kAnyStore:
5391 case MemBarrierKind::kLoadAny:
5392 case MemBarrierKind::kAnyAny: {
5393 flavor = DmbOptions::ISH;
5394 break;
5395 }
5396 case MemBarrierKind::kStoreStore: {
5397 flavor = DmbOptions::ISHST;
5398 break;
5399 }
5400 default:
5401 LOG(FATAL) << "Unexpected memory barrier " << kind;
5402 }
5403 __ Dmb(flavor);
5404}
5405
5406void InstructionCodeGeneratorARMVIXL::GenerateWideAtomicLoad(vixl32::Register addr,
5407 uint32_t offset,
5408 vixl32::Register out_lo,
5409 vixl32::Register out_hi) {
5410 UseScratchRegisterScope temps(GetVIXLAssembler());
5411 if (offset != 0) {
5412 vixl32::Register temp = temps.Acquire();
5413 __ Add(temp, addr, offset);
5414 addr = temp;
5415 }
Scott Wakelingb77051e2016-11-21 19:46:00 +00005416 __ Ldrexd(out_lo, out_hi, MemOperand(addr));
Artem Serov02d37832016-10-25 15:25:33 +01005417}
5418
5419void InstructionCodeGeneratorARMVIXL::GenerateWideAtomicStore(vixl32::Register addr,
5420 uint32_t offset,
5421 vixl32::Register value_lo,
5422 vixl32::Register value_hi,
5423 vixl32::Register temp1,
5424 vixl32::Register temp2,
5425 HInstruction* instruction) {
5426 UseScratchRegisterScope temps(GetVIXLAssembler());
5427 vixl32::Label fail;
5428 if (offset != 0) {
5429 vixl32::Register temp = temps.Acquire();
5430 __ Add(temp, addr, offset);
5431 addr = temp;
5432 }
5433 __ Bind(&fail);
Alexandre Rames374ddf32016-11-04 10:40:49 +00005434 {
5435 // Ensure the pc position is recorded immediately after the `ldrexd` instruction.
Artem Serov0fb37192016-12-06 18:13:40 +00005436 ExactAssemblyScope aas(GetVIXLAssembler(),
5437 vixl32::kMaxInstructionSizeInBytes,
5438 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00005439 // We need a load followed by store. (The address used in a STREX instruction must
5440 // be the same as the address in the most recently executed LDREX instruction.)
5441 __ ldrexd(temp1, temp2, MemOperand(addr));
5442 codegen_->MaybeRecordImplicitNullCheck(instruction);
5443 }
Scott Wakelingb77051e2016-11-21 19:46:00 +00005444 __ Strexd(temp1, value_lo, value_hi, MemOperand(addr));
xueliang.zhongf51bc622016-11-04 09:23:32 +00005445 __ CompareAndBranchIfNonZero(temp1, &fail);
Artem Serov02d37832016-10-25 15:25:33 +01005446}
Artem Serov02109dd2016-09-23 17:17:54 +01005447
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005448void LocationsBuilderARMVIXL::HandleFieldSet(
5449 HInstruction* instruction, const FieldInfo& field_info) {
5450 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
5451
5452 LocationSummary* locations =
5453 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5454 locations->SetInAt(0, Location::RequiresRegister());
5455
5456 Primitive::Type field_type = field_info.GetFieldType();
5457 if (Primitive::IsFloatingPointType(field_type)) {
5458 locations->SetInAt(1, Location::RequiresFpuRegister());
5459 } else {
5460 locations->SetInAt(1, Location::RequiresRegister());
5461 }
5462
5463 bool is_wide = field_type == Primitive::kPrimLong || field_type == Primitive::kPrimDouble;
5464 bool generate_volatile = field_info.IsVolatile()
5465 && is_wide
5466 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
5467 bool needs_write_barrier =
5468 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
5469 // Temporary registers for the write barrier.
5470 // TODO: consider renaming StoreNeedsWriteBarrier to StoreNeedsGCMark.
5471 if (needs_write_barrier) {
5472 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
5473 locations->AddTemp(Location::RequiresRegister());
5474 } else if (generate_volatile) {
5475 // ARM encoding have some additional constraints for ldrexd/strexd:
5476 // - registers need to be consecutive
5477 // - the first register should be even but not R14.
5478 // We don't test for ARM yet, and the assertion makes sure that we
5479 // revisit this if we ever enable ARM encoding.
5480 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
5481
5482 locations->AddTemp(Location::RequiresRegister());
5483 locations->AddTemp(Location::RequiresRegister());
5484 if (field_type == Primitive::kPrimDouble) {
5485 // For doubles we need two more registers to copy the value.
5486 locations->AddTemp(LocationFrom(r2));
5487 locations->AddTemp(LocationFrom(r3));
5488 }
5489 }
5490}
5491
5492void InstructionCodeGeneratorARMVIXL::HandleFieldSet(HInstruction* instruction,
5493 const FieldInfo& field_info,
5494 bool value_can_be_null) {
5495 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
5496
5497 LocationSummary* locations = instruction->GetLocations();
5498 vixl32::Register base = InputRegisterAt(instruction, 0);
5499 Location value = locations->InAt(1);
5500
5501 bool is_volatile = field_info.IsVolatile();
5502 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
5503 Primitive::Type field_type = field_info.GetFieldType();
5504 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
5505 bool needs_write_barrier =
5506 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
5507
5508 if (is_volatile) {
5509 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
5510 }
5511
5512 switch (field_type) {
5513 case Primitive::kPrimBoolean:
5514 case Primitive::kPrimByte: {
5515 GetAssembler()->StoreToOffset(kStoreByte, RegisterFrom(value), base, offset);
5516 break;
5517 }
5518
5519 case Primitive::kPrimShort:
5520 case Primitive::kPrimChar: {
5521 GetAssembler()->StoreToOffset(kStoreHalfword, RegisterFrom(value), base, offset);
5522 break;
5523 }
5524
5525 case Primitive::kPrimInt:
5526 case Primitive::kPrimNot: {
5527 if (kPoisonHeapReferences && needs_write_barrier) {
5528 // Note that in the case where `value` is a null reference,
5529 // we do not enter this block, as a null reference does not
5530 // need poisoning.
5531 DCHECK_EQ(field_type, Primitive::kPrimNot);
5532 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
5533 __ Mov(temp, RegisterFrom(value));
5534 GetAssembler()->PoisonHeapReference(temp);
5535 GetAssembler()->StoreToOffset(kStoreWord, temp, base, offset);
5536 } else {
5537 GetAssembler()->StoreToOffset(kStoreWord, RegisterFrom(value), base, offset);
5538 }
5539 break;
5540 }
5541
5542 case Primitive::kPrimLong: {
5543 if (is_volatile && !atomic_ldrd_strd) {
5544 GenerateWideAtomicStore(base,
5545 offset,
5546 LowRegisterFrom(value),
5547 HighRegisterFrom(value),
5548 RegisterFrom(locations->GetTemp(0)),
5549 RegisterFrom(locations->GetTemp(1)),
5550 instruction);
5551 } else {
5552 GetAssembler()->StoreToOffset(kStoreWordPair, LowRegisterFrom(value), base, offset);
5553 codegen_->MaybeRecordImplicitNullCheck(instruction);
5554 }
5555 break;
5556 }
5557
5558 case Primitive::kPrimFloat: {
5559 GetAssembler()->StoreSToOffset(SRegisterFrom(value), base, offset);
5560 break;
5561 }
5562
5563 case Primitive::kPrimDouble: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01005564 vixl32::DRegister value_reg = DRegisterFrom(value);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005565 if (is_volatile && !atomic_ldrd_strd) {
5566 vixl32::Register value_reg_lo = RegisterFrom(locations->GetTemp(0));
5567 vixl32::Register value_reg_hi = RegisterFrom(locations->GetTemp(1));
5568
5569 __ Vmov(value_reg_lo, value_reg_hi, value_reg);
5570
5571 GenerateWideAtomicStore(base,
5572 offset,
5573 value_reg_lo,
5574 value_reg_hi,
5575 RegisterFrom(locations->GetTemp(2)),
5576 RegisterFrom(locations->GetTemp(3)),
5577 instruction);
5578 } else {
5579 GetAssembler()->StoreDToOffset(value_reg, base, offset);
5580 codegen_->MaybeRecordImplicitNullCheck(instruction);
5581 }
5582 break;
5583 }
5584
5585 case Primitive::kPrimVoid:
5586 LOG(FATAL) << "Unreachable type " << field_type;
5587 UNREACHABLE();
5588 }
5589
5590 // Longs and doubles are handled in the switch.
5591 if (field_type != Primitive::kPrimLong && field_type != Primitive::kPrimDouble) {
Alexandre Rames374ddf32016-11-04 10:40:49 +00005592 // TODO(VIXL): Here and for other calls to `MaybeRecordImplicitNullCheck` in this method, we
5593 // should use a scope and the assembler to emit the store instruction to guarantee that we
5594 // record the pc at the correct position. But the `Assembler` does not automatically handle
5595 // unencodable offsets. Practically, everything is fine because the helper and VIXL, at the time
5596 // of writing, do generate the store instruction last.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005597 codegen_->MaybeRecordImplicitNullCheck(instruction);
5598 }
5599
5600 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
5601 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
5602 vixl32::Register card = RegisterFrom(locations->GetTemp(1));
5603 codegen_->MarkGCCard(temp, card, base, RegisterFrom(value), value_can_be_null);
5604 }
5605
5606 if (is_volatile) {
5607 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
5608 }
5609}
5610
Artem Serov02d37832016-10-25 15:25:33 +01005611void LocationsBuilderARMVIXL::HandleFieldGet(HInstruction* instruction,
5612 const FieldInfo& field_info) {
5613 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
5614
5615 bool object_field_get_with_read_barrier =
5616 kEmitCompilerReadBarrier && (field_info.GetFieldType() == Primitive::kPrimNot);
5617 LocationSummary* locations =
5618 new (GetGraph()->GetArena()) LocationSummary(instruction,
5619 object_field_get_with_read_barrier ?
5620 LocationSummary::kCallOnSlowPath :
5621 LocationSummary::kNoCall);
5622 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
5623 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
5624 }
5625 locations->SetInAt(0, Location::RequiresRegister());
5626
5627 bool volatile_for_double = field_info.IsVolatile()
5628 && (field_info.GetFieldType() == Primitive::kPrimDouble)
5629 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
5630 // The output overlaps in case of volatile long: we don't want the
5631 // code generated by GenerateWideAtomicLoad to overwrite the
5632 // object's location. Likewise, in the case of an object field get
5633 // with read barriers enabled, we do not want the load to overwrite
5634 // the object's location, as we need it to emit the read barrier.
5635 bool overlap = (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) ||
5636 object_field_get_with_read_barrier;
5637
5638 if (Primitive::IsFloatingPointType(instruction->GetType())) {
5639 locations->SetOut(Location::RequiresFpuRegister());
5640 } else {
5641 locations->SetOut(Location::RequiresRegister(),
5642 (overlap ? Location::kOutputOverlap : Location::kNoOutputOverlap));
5643 }
5644 if (volatile_for_double) {
5645 // ARM encoding have some additional constraints for ldrexd/strexd:
5646 // - registers need to be consecutive
5647 // - the first register should be even but not R14.
5648 // We don't test for ARM yet, and the assertion makes sure that we
5649 // revisit this if we ever enable ARM encoding.
5650 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
5651 locations->AddTemp(Location::RequiresRegister());
5652 locations->AddTemp(Location::RequiresRegister());
5653 } else if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
5654 // We need a temporary register for the read barrier marking slow
Artem Serovc5fcb442016-12-02 19:19:58 +00005655 // path in CodeGeneratorARMVIXL::GenerateFieldLoadWithBakerReadBarrier.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01005656 if (kBakerReadBarrierLinkTimeThunksEnableForFields &&
5657 !Runtime::Current()->UseJitCompilation()) {
5658 // If link-time thunks for the Baker read barrier are enabled, for AOT
5659 // loads we need a temporary only if the offset is too big.
5660 if (field_info.GetFieldOffset().Uint32Value() >= kReferenceLoadMinFarOffset) {
5661 locations->AddTemp(Location::RequiresRegister());
5662 }
5663 // And we always need the reserved entrypoint register.
5664 locations->AddTemp(Location::RegisterLocation(kBakerCcEntrypointRegister.GetCode()));
5665 } else {
5666 locations->AddTemp(Location::RequiresRegister());
5667 }
Artem Serov02d37832016-10-25 15:25:33 +01005668 }
5669}
5670
5671Location LocationsBuilderARMVIXL::ArithmeticZeroOrFpuRegister(HInstruction* input) {
5672 DCHECK(Primitive::IsFloatingPointType(input->GetType())) << input->GetType();
5673 if ((input->IsFloatConstant() && (input->AsFloatConstant()->IsArithmeticZero())) ||
5674 (input->IsDoubleConstant() && (input->AsDoubleConstant()->IsArithmeticZero()))) {
5675 return Location::ConstantLocation(input->AsConstant());
5676 } else {
5677 return Location::RequiresFpuRegister();
5678 }
5679}
5680
Artem Serov02109dd2016-09-23 17:17:54 +01005681Location LocationsBuilderARMVIXL::ArmEncodableConstantOrRegister(HInstruction* constant,
5682 Opcode opcode) {
5683 DCHECK(!Primitive::IsFloatingPointType(constant->GetType()));
5684 if (constant->IsConstant() &&
5685 CanEncodeConstantAsImmediate(constant->AsConstant(), opcode)) {
5686 return Location::ConstantLocation(constant->AsConstant());
5687 }
5688 return Location::RequiresRegister();
5689}
5690
5691bool LocationsBuilderARMVIXL::CanEncodeConstantAsImmediate(HConstant* input_cst,
5692 Opcode opcode) {
5693 uint64_t value = static_cast<uint64_t>(Int64FromConstant(input_cst));
5694 if (Primitive::Is64BitType(input_cst->GetType())) {
5695 Opcode high_opcode = opcode;
5696 SetCc low_set_cc = kCcDontCare;
5697 switch (opcode) {
5698 case SUB:
5699 // Flip the operation to an ADD.
5700 value = -value;
5701 opcode = ADD;
5702 FALLTHROUGH_INTENDED;
5703 case ADD:
5704 if (Low32Bits(value) == 0u) {
5705 return CanEncodeConstantAsImmediate(High32Bits(value), opcode, kCcDontCare);
5706 }
5707 high_opcode = ADC;
5708 low_set_cc = kCcSet;
5709 break;
5710 default:
5711 break;
5712 }
5713 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode, low_set_cc) &&
5714 CanEncodeConstantAsImmediate(High32Bits(value), high_opcode, kCcDontCare);
5715 } else {
5716 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode);
5717 }
5718}
5719
5720// TODO(VIXL): Replace art::arm::SetCc` with `vixl32::FlagsUpdate after flags set optimization
5721// enabled.
5722bool LocationsBuilderARMVIXL::CanEncodeConstantAsImmediate(uint32_t value,
5723 Opcode opcode,
5724 SetCc set_cc) {
5725 ArmVIXLAssembler* assembler = codegen_->GetAssembler();
5726 if (assembler->ShifterOperandCanHold(opcode, value, set_cc)) {
5727 return true;
5728 }
5729 Opcode neg_opcode = kNoOperand;
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00005730 uint32_t neg_value = 0;
Artem Serov02109dd2016-09-23 17:17:54 +01005731 switch (opcode) {
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00005732 case AND: neg_opcode = BIC; neg_value = ~value; break;
5733 case ORR: neg_opcode = ORN; neg_value = ~value; break;
5734 case ADD: neg_opcode = SUB; neg_value = -value; break;
5735 case ADC: neg_opcode = SBC; neg_value = ~value; break;
5736 case SUB: neg_opcode = ADD; neg_value = -value; break;
5737 case SBC: neg_opcode = ADC; neg_value = ~value; break;
5738 case MOV: neg_opcode = MVN; neg_value = ~value; break;
Artem Serov02109dd2016-09-23 17:17:54 +01005739 default:
5740 return false;
5741 }
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00005742
5743 if (assembler->ShifterOperandCanHold(neg_opcode, neg_value, set_cc)) {
5744 return true;
5745 }
5746
5747 return opcode == AND && IsPowerOfTwo(value + 1);
Artem Serov02109dd2016-09-23 17:17:54 +01005748}
5749
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005750void InstructionCodeGeneratorARMVIXL::HandleFieldGet(HInstruction* instruction,
5751 const FieldInfo& field_info) {
5752 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
5753
5754 LocationSummary* locations = instruction->GetLocations();
5755 vixl32::Register base = InputRegisterAt(instruction, 0);
5756 Location out = locations->Out();
5757 bool is_volatile = field_info.IsVolatile();
5758 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
5759 Primitive::Type field_type = field_info.GetFieldType();
5760 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
5761
5762 switch (field_type) {
5763 case Primitive::kPrimBoolean:
5764 GetAssembler()->LoadFromOffset(kLoadUnsignedByte, RegisterFrom(out), base, offset);
5765 break;
5766
5767 case Primitive::kPrimByte:
5768 GetAssembler()->LoadFromOffset(kLoadSignedByte, RegisterFrom(out), base, offset);
5769 break;
5770
5771 case Primitive::kPrimShort:
5772 GetAssembler()->LoadFromOffset(kLoadSignedHalfword, RegisterFrom(out), base, offset);
5773 break;
5774
5775 case Primitive::kPrimChar:
5776 GetAssembler()->LoadFromOffset(kLoadUnsignedHalfword, RegisterFrom(out), base, offset);
5777 break;
5778
5779 case Primitive::kPrimInt:
5780 GetAssembler()->LoadFromOffset(kLoadWord, RegisterFrom(out), base, offset);
5781 break;
5782
5783 case Primitive::kPrimNot: {
5784 // /* HeapReference<Object> */ out = *(base + offset)
5785 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005786 Location temp_loc = locations->GetTemp(0);
5787 // Note that a potential implicit null check is handled in this
5788 // CodeGeneratorARMVIXL::GenerateFieldLoadWithBakerReadBarrier call.
5789 codegen_->GenerateFieldLoadWithBakerReadBarrier(
5790 instruction, out, base, offset, temp_loc, /* needs_null_check */ true);
5791 if (is_volatile) {
5792 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5793 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005794 } else {
5795 GetAssembler()->LoadFromOffset(kLoadWord, RegisterFrom(out), base, offset);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005796 codegen_->MaybeRecordImplicitNullCheck(instruction);
5797 if (is_volatile) {
5798 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5799 }
5800 // If read barriers are enabled, emit read barriers other than
5801 // Baker's using a slow path (and also unpoison the loaded
5802 // reference, if heap poisoning is enabled).
5803 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, locations->InAt(0), offset);
5804 }
5805 break;
5806 }
5807
5808 case Primitive::kPrimLong:
5809 if (is_volatile && !atomic_ldrd_strd) {
5810 GenerateWideAtomicLoad(base, offset, LowRegisterFrom(out), HighRegisterFrom(out));
5811 } else {
5812 GetAssembler()->LoadFromOffset(kLoadWordPair, LowRegisterFrom(out), base, offset);
5813 }
5814 break;
5815
5816 case Primitive::kPrimFloat:
5817 GetAssembler()->LoadSFromOffset(SRegisterFrom(out), base, offset);
5818 break;
5819
5820 case Primitive::kPrimDouble: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01005821 vixl32::DRegister out_dreg = DRegisterFrom(out);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005822 if (is_volatile && !atomic_ldrd_strd) {
5823 vixl32::Register lo = RegisterFrom(locations->GetTemp(0));
5824 vixl32::Register hi = RegisterFrom(locations->GetTemp(1));
5825 GenerateWideAtomicLoad(base, offset, lo, hi);
5826 // TODO(VIXL): Do we need to be immediately after the ldrexd instruction? If so we need a
5827 // scope.
5828 codegen_->MaybeRecordImplicitNullCheck(instruction);
5829 __ Vmov(out_dreg, lo, hi);
5830 } else {
5831 GetAssembler()->LoadDFromOffset(out_dreg, base, offset);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005832 codegen_->MaybeRecordImplicitNullCheck(instruction);
5833 }
5834 break;
5835 }
5836
5837 case Primitive::kPrimVoid:
5838 LOG(FATAL) << "Unreachable type " << field_type;
5839 UNREACHABLE();
5840 }
5841
5842 if (field_type == Primitive::kPrimNot || field_type == Primitive::kPrimDouble) {
5843 // Potential implicit null checks, in the case of reference or
5844 // double fields, are handled in the previous switch statement.
5845 } else {
5846 // Address cases other than reference and double that may require an implicit null check.
Alexandre Rames374ddf32016-11-04 10:40:49 +00005847 // TODO(VIXL): Here and for other calls to `MaybeRecordImplicitNullCheck` in this method, we
5848 // should use a scope and the assembler to emit the load instruction to guarantee that we
5849 // record the pc at the correct position. But the `Assembler` does not automatically handle
5850 // unencodable offsets. Practically, everything is fine because the helper and VIXL, at the time
5851 // of writing, do generate the store instruction last.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005852 codegen_->MaybeRecordImplicitNullCheck(instruction);
5853 }
5854
5855 if (is_volatile) {
5856 if (field_type == Primitive::kPrimNot) {
5857 // Memory barriers, in the case of references, are also handled
5858 // in the previous switch statement.
5859 } else {
5860 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5861 }
5862 }
5863}
5864
5865void LocationsBuilderARMVIXL::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
5866 HandleFieldSet(instruction, instruction->GetFieldInfo());
5867}
5868
5869void InstructionCodeGeneratorARMVIXL::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
5870 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
5871}
5872
5873void LocationsBuilderARMVIXL::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5874 HandleFieldGet(instruction, instruction->GetFieldInfo());
5875}
5876
5877void InstructionCodeGeneratorARMVIXL::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5878 HandleFieldGet(instruction, instruction->GetFieldInfo());
5879}
5880
5881void LocationsBuilderARMVIXL::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5882 HandleFieldGet(instruction, instruction->GetFieldInfo());
5883}
5884
5885void InstructionCodeGeneratorARMVIXL::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5886 HandleFieldGet(instruction, instruction->GetFieldInfo());
5887}
5888
Scott Wakelingc34dba72016-10-03 10:14:44 +01005889void LocationsBuilderARMVIXL::VisitStaticFieldSet(HStaticFieldSet* instruction) {
5890 HandleFieldSet(instruction, instruction->GetFieldInfo());
5891}
5892
5893void InstructionCodeGeneratorARMVIXL::VisitStaticFieldSet(HStaticFieldSet* instruction) {
5894 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
5895}
5896
Artem Serovcfbe9132016-10-14 15:58:56 +01005897void LocationsBuilderARMVIXL::VisitUnresolvedInstanceFieldGet(
5898 HUnresolvedInstanceFieldGet* instruction) {
5899 FieldAccessCallingConventionARMVIXL calling_convention;
5900 codegen_->CreateUnresolvedFieldLocationSummary(
5901 instruction, instruction->GetFieldType(), calling_convention);
5902}
5903
5904void InstructionCodeGeneratorARMVIXL::VisitUnresolvedInstanceFieldGet(
5905 HUnresolvedInstanceFieldGet* instruction) {
5906 FieldAccessCallingConventionARMVIXL calling_convention;
5907 codegen_->GenerateUnresolvedFieldAccess(instruction,
5908 instruction->GetFieldType(),
5909 instruction->GetFieldIndex(),
5910 instruction->GetDexPc(),
5911 calling_convention);
5912}
5913
5914void LocationsBuilderARMVIXL::VisitUnresolvedInstanceFieldSet(
5915 HUnresolvedInstanceFieldSet* instruction) {
5916 FieldAccessCallingConventionARMVIXL calling_convention;
5917 codegen_->CreateUnresolvedFieldLocationSummary(
5918 instruction, instruction->GetFieldType(), calling_convention);
5919}
5920
5921void InstructionCodeGeneratorARMVIXL::VisitUnresolvedInstanceFieldSet(
5922 HUnresolvedInstanceFieldSet* instruction) {
5923 FieldAccessCallingConventionARMVIXL calling_convention;
5924 codegen_->GenerateUnresolvedFieldAccess(instruction,
5925 instruction->GetFieldType(),
5926 instruction->GetFieldIndex(),
5927 instruction->GetDexPc(),
5928 calling_convention);
5929}
5930
5931void LocationsBuilderARMVIXL::VisitUnresolvedStaticFieldGet(
5932 HUnresolvedStaticFieldGet* instruction) {
5933 FieldAccessCallingConventionARMVIXL calling_convention;
5934 codegen_->CreateUnresolvedFieldLocationSummary(
5935 instruction, instruction->GetFieldType(), calling_convention);
5936}
5937
5938void InstructionCodeGeneratorARMVIXL::VisitUnresolvedStaticFieldGet(
5939 HUnresolvedStaticFieldGet* instruction) {
5940 FieldAccessCallingConventionARMVIXL calling_convention;
5941 codegen_->GenerateUnresolvedFieldAccess(instruction,
5942 instruction->GetFieldType(),
5943 instruction->GetFieldIndex(),
5944 instruction->GetDexPc(),
5945 calling_convention);
5946}
5947
5948void LocationsBuilderARMVIXL::VisitUnresolvedStaticFieldSet(
5949 HUnresolvedStaticFieldSet* instruction) {
5950 FieldAccessCallingConventionARMVIXL calling_convention;
5951 codegen_->CreateUnresolvedFieldLocationSummary(
5952 instruction, instruction->GetFieldType(), calling_convention);
5953}
5954
5955void InstructionCodeGeneratorARMVIXL::VisitUnresolvedStaticFieldSet(
5956 HUnresolvedStaticFieldSet* instruction) {
5957 FieldAccessCallingConventionARMVIXL calling_convention;
5958 codegen_->GenerateUnresolvedFieldAccess(instruction,
5959 instruction->GetFieldType(),
5960 instruction->GetFieldIndex(),
5961 instruction->GetDexPc(),
5962 calling_convention);
5963}
5964
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005965void LocationsBuilderARMVIXL::VisitNullCheck(HNullCheck* instruction) {
Artem Serov657022c2016-11-23 14:19:38 +00005966 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005967 locations->SetInAt(0, Location::RequiresRegister());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005968}
5969
5970void CodeGeneratorARMVIXL::GenerateImplicitNullCheck(HNullCheck* instruction) {
5971 if (CanMoveNullCheckToUser(instruction)) {
5972 return;
5973 }
5974
5975 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames374ddf32016-11-04 10:40:49 +00005976 // Ensure the pc position is recorded immediately after the `ldr` instruction.
Artem Serov0fb37192016-12-06 18:13:40 +00005977 ExactAssemblyScope aas(GetVIXLAssembler(),
5978 vixl32::kMaxInstructionSizeInBytes,
5979 CodeBufferCheckScope::kMaximumSize);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005980 __ ldr(temps.Acquire(), MemOperand(InputRegisterAt(instruction, 0)));
5981 RecordPcInfo(instruction, instruction->GetDexPc());
5982}
5983
5984void CodeGeneratorARMVIXL::GenerateExplicitNullCheck(HNullCheck* instruction) {
5985 NullCheckSlowPathARMVIXL* slow_path =
5986 new (GetGraph()->GetArena()) NullCheckSlowPathARMVIXL(instruction);
5987 AddSlowPath(slow_path);
xueliang.zhongf51bc622016-11-04 09:23:32 +00005988 __ CompareAndBranchIfZero(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005989}
5990
5991void InstructionCodeGeneratorARMVIXL::VisitNullCheck(HNullCheck* instruction) {
5992 codegen_->GenerateNullCheck(instruction);
5993}
5994
Scott Wakelingc34dba72016-10-03 10:14:44 +01005995static LoadOperandType GetLoadOperandType(Primitive::Type type) {
5996 switch (type) {
5997 case Primitive::kPrimNot:
5998 return kLoadWord;
5999 case Primitive::kPrimBoolean:
6000 return kLoadUnsignedByte;
6001 case Primitive::kPrimByte:
6002 return kLoadSignedByte;
6003 case Primitive::kPrimChar:
6004 return kLoadUnsignedHalfword;
6005 case Primitive::kPrimShort:
6006 return kLoadSignedHalfword;
6007 case Primitive::kPrimInt:
6008 return kLoadWord;
6009 case Primitive::kPrimLong:
6010 return kLoadWordPair;
6011 case Primitive::kPrimFloat:
6012 return kLoadSWord;
6013 case Primitive::kPrimDouble:
6014 return kLoadDWord;
6015 default:
6016 LOG(FATAL) << "Unreachable type " << type;
6017 UNREACHABLE();
6018 }
6019}
6020
6021static StoreOperandType GetStoreOperandType(Primitive::Type type) {
6022 switch (type) {
6023 case Primitive::kPrimNot:
6024 return kStoreWord;
6025 case Primitive::kPrimBoolean:
6026 case Primitive::kPrimByte:
6027 return kStoreByte;
6028 case Primitive::kPrimChar:
6029 case Primitive::kPrimShort:
6030 return kStoreHalfword;
6031 case Primitive::kPrimInt:
6032 return kStoreWord;
6033 case Primitive::kPrimLong:
6034 return kStoreWordPair;
6035 case Primitive::kPrimFloat:
6036 return kStoreSWord;
6037 case Primitive::kPrimDouble:
6038 return kStoreDWord;
6039 default:
6040 LOG(FATAL) << "Unreachable type " << type;
6041 UNREACHABLE();
6042 }
6043}
6044
6045void CodeGeneratorARMVIXL::LoadFromShiftedRegOffset(Primitive::Type type,
6046 Location out_loc,
6047 vixl32::Register base,
6048 vixl32::Register reg_index,
6049 vixl32::Condition cond) {
6050 uint32_t shift_count = Primitive::ComponentSizeShift(type);
6051 MemOperand mem_address(base, reg_index, vixl32::LSL, shift_count);
6052
6053 switch (type) {
6054 case Primitive::kPrimByte:
6055 __ Ldrsb(cond, RegisterFrom(out_loc), mem_address);
6056 break;
6057 case Primitive::kPrimBoolean:
6058 __ Ldrb(cond, RegisterFrom(out_loc), mem_address);
6059 break;
6060 case Primitive::kPrimShort:
6061 __ Ldrsh(cond, RegisterFrom(out_loc), mem_address);
6062 break;
6063 case Primitive::kPrimChar:
6064 __ Ldrh(cond, RegisterFrom(out_loc), mem_address);
6065 break;
6066 case Primitive::kPrimNot:
6067 case Primitive::kPrimInt:
6068 __ Ldr(cond, RegisterFrom(out_loc), mem_address);
6069 break;
6070 // T32 doesn't support LoadFromShiftedRegOffset mem address mode for these types.
6071 case Primitive::kPrimLong:
6072 case Primitive::kPrimFloat:
6073 case Primitive::kPrimDouble:
6074 default:
6075 LOG(FATAL) << "Unreachable type " << type;
6076 UNREACHABLE();
6077 }
6078}
6079
6080void CodeGeneratorARMVIXL::StoreToShiftedRegOffset(Primitive::Type type,
6081 Location loc,
6082 vixl32::Register base,
6083 vixl32::Register reg_index,
6084 vixl32::Condition cond) {
6085 uint32_t shift_count = Primitive::ComponentSizeShift(type);
6086 MemOperand mem_address(base, reg_index, vixl32::LSL, shift_count);
6087
6088 switch (type) {
6089 case Primitive::kPrimByte:
6090 case Primitive::kPrimBoolean:
6091 __ Strb(cond, RegisterFrom(loc), mem_address);
6092 break;
6093 case Primitive::kPrimShort:
6094 case Primitive::kPrimChar:
6095 __ Strh(cond, RegisterFrom(loc), mem_address);
6096 break;
6097 case Primitive::kPrimNot:
6098 case Primitive::kPrimInt:
6099 __ Str(cond, RegisterFrom(loc), mem_address);
6100 break;
6101 // T32 doesn't support StoreToShiftedRegOffset mem address mode for these types.
6102 case Primitive::kPrimLong:
6103 case Primitive::kPrimFloat:
6104 case Primitive::kPrimDouble:
6105 default:
6106 LOG(FATAL) << "Unreachable type " << type;
6107 UNREACHABLE();
6108 }
6109}
6110
6111void LocationsBuilderARMVIXL::VisitArrayGet(HArrayGet* instruction) {
6112 bool object_array_get_with_read_barrier =
6113 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
6114 LocationSummary* locations =
6115 new (GetGraph()->GetArena()) LocationSummary(instruction,
6116 object_array_get_with_read_barrier ?
6117 LocationSummary::kCallOnSlowPath :
6118 LocationSummary::kNoCall);
6119 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006120 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Scott Wakelingc34dba72016-10-03 10:14:44 +01006121 }
6122 locations->SetInAt(0, Location::RequiresRegister());
6123 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
6124 if (Primitive::IsFloatingPointType(instruction->GetType())) {
6125 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
6126 } else {
6127 // The output overlaps in the case of an object array get with
6128 // read barriers enabled: we do not want the move to overwrite the
6129 // array's location, as we need it to emit the read barrier.
6130 locations->SetOut(
6131 Location::RequiresRegister(),
6132 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
6133 }
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01006134 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
6135 // We need a temporary register for the read barrier marking slow
6136 // path in CodeGeneratorARMVIXL::GenerateArrayLoadWithBakerReadBarrier.
6137 if (kBakerReadBarrierLinkTimeThunksEnableForFields &&
6138 !Runtime::Current()->UseJitCompilation() &&
6139 instruction->GetIndex()->IsConstant()) {
6140 // Array loads with constant index are treated as field loads.
6141 // If link-time thunks for the Baker read barrier are enabled, for AOT
6142 // constant index loads we need a temporary only if the offset is too big.
6143 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
6144 uint32_t index = instruction->GetIndex()->AsIntConstant()->GetValue();
6145 offset += index << Primitive::ComponentSizeShift(Primitive::kPrimNot);
6146 if (offset >= kReferenceLoadMinFarOffset) {
6147 locations->AddTemp(Location::RequiresRegister());
6148 }
6149 // And we always need the reserved entrypoint register.
6150 locations->AddTemp(Location::RegisterLocation(kBakerCcEntrypointRegister.GetCode()));
6151 } else if (kBakerReadBarrierLinkTimeThunksEnableForArrays &&
6152 !Runtime::Current()->UseJitCompilation() &&
6153 !instruction->GetIndex()->IsConstant()) {
6154 // We need a non-scratch temporary for the array data pointer.
6155 locations->AddTemp(Location::RequiresRegister());
6156 // And we always need the reserved entrypoint register.
6157 locations->AddTemp(Location::RegisterLocation(kBakerCcEntrypointRegister.GetCode()));
6158 } else {
6159 locations->AddTemp(Location::RequiresRegister());
6160 }
6161 } else if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
6162 // Also need a temporary for String compression feature.
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006163 locations->AddTemp(Location::RequiresRegister());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006164 }
6165}
6166
6167void InstructionCodeGeneratorARMVIXL::VisitArrayGet(HArrayGet* instruction) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006168 LocationSummary* locations = instruction->GetLocations();
6169 Location obj_loc = locations->InAt(0);
6170 vixl32::Register obj = InputRegisterAt(instruction, 0);
6171 Location index = locations->InAt(1);
6172 Location out_loc = locations->Out();
6173 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
6174 Primitive::Type type = instruction->GetType();
6175 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
6176 instruction->IsStringCharAt();
6177 HInstruction* array_instr = instruction->GetArray();
6178 bool has_intermediate_address = array_instr->IsIntermediateAddress();
Scott Wakelingc34dba72016-10-03 10:14:44 +01006179
6180 switch (type) {
6181 case Primitive::kPrimBoolean:
6182 case Primitive::kPrimByte:
6183 case Primitive::kPrimShort:
6184 case Primitive::kPrimChar:
6185 case Primitive::kPrimInt: {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006186 vixl32::Register length;
6187 if (maybe_compressed_char_at) {
6188 length = RegisterFrom(locations->GetTemp(0));
6189 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
6190 GetAssembler()->LoadFromOffset(kLoadWord, length, obj, count_offset);
6191 codegen_->MaybeRecordImplicitNullCheck(instruction);
6192 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006193 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006194 int32_t const_index = Int32ConstantFrom(index);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006195 if (maybe_compressed_char_at) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006196 vixl32::Label uncompressed_load, done;
Anton Kirilov6f644202017-02-27 18:29:45 +00006197 vixl32::Label* final_label = codegen_->GetFinalLabel(instruction, &done);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006198 __ Lsrs(length, length, 1u); // LSRS has a 16-bit encoding, TST (immediate) does not.
6199 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
6200 "Expecting 0=compressed, 1=uncompressed");
Artem Serov517d9f62016-12-12 15:51:15 +00006201 __ B(cs, &uncompressed_load, /* far_target */ false);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006202 GetAssembler()->LoadFromOffset(kLoadUnsignedByte,
6203 RegisterFrom(out_loc),
6204 obj,
6205 data_offset + const_index);
Anton Kirilov6f644202017-02-27 18:29:45 +00006206 __ B(final_label);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006207 __ Bind(&uncompressed_load);
6208 GetAssembler()->LoadFromOffset(GetLoadOperandType(Primitive::kPrimChar),
6209 RegisterFrom(out_loc),
6210 obj,
6211 data_offset + (const_index << 1));
Anton Kirilov6f644202017-02-27 18:29:45 +00006212 if (done.IsReferenced()) {
6213 __ Bind(&done);
6214 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006215 } else {
6216 uint32_t full_offset = data_offset + (const_index << Primitive::ComponentSizeShift(type));
6217
6218 LoadOperandType load_type = GetLoadOperandType(type);
6219 GetAssembler()->LoadFromOffset(load_type, RegisterFrom(out_loc), obj, full_offset);
6220 }
6221 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006222 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006223 vixl32::Register temp = temps.Acquire();
6224
6225 if (has_intermediate_address) {
Artem Serov2bbc9532016-10-21 11:51:50 +01006226 // We do not need to compute the intermediate address from the array: the
6227 // input instruction has done it already. See the comment in
6228 // `TryExtractArrayAccessAddress()`.
6229 if (kIsDebugBuild) {
6230 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
Anton Kirilov644032c2016-12-06 17:51:43 +00006231 DCHECK_EQ(Uint64ConstantFrom(tmp->GetOffset()), data_offset);
Artem Serov2bbc9532016-10-21 11:51:50 +01006232 }
6233 temp = obj;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006234 } else {
6235 __ Add(temp, obj, data_offset);
6236 }
6237 if (maybe_compressed_char_at) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006238 vixl32::Label uncompressed_load, done;
Anton Kirilov6f644202017-02-27 18:29:45 +00006239 vixl32::Label* final_label = codegen_->GetFinalLabel(instruction, &done);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006240 __ Lsrs(length, length, 1u); // LSRS has a 16-bit encoding, TST (immediate) does not.
6241 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
6242 "Expecting 0=compressed, 1=uncompressed");
Artem Serov517d9f62016-12-12 15:51:15 +00006243 __ B(cs, &uncompressed_load, /* far_target */ false);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006244 __ Ldrb(RegisterFrom(out_loc), MemOperand(temp, RegisterFrom(index), vixl32::LSL, 0));
Anton Kirilov6f644202017-02-27 18:29:45 +00006245 __ B(final_label);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006246 __ Bind(&uncompressed_load);
6247 __ Ldrh(RegisterFrom(out_loc), MemOperand(temp, RegisterFrom(index), vixl32::LSL, 1));
Anton Kirilov6f644202017-02-27 18:29:45 +00006248 if (done.IsReferenced()) {
6249 __ Bind(&done);
6250 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006251 } else {
6252 codegen_->LoadFromShiftedRegOffset(type, out_loc, temp, RegisterFrom(index));
6253 }
6254 }
6255 break;
6256 }
6257
6258 case Primitive::kPrimNot: {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006259 // The read barrier instrumentation of object ArrayGet
6260 // instructions does not support the HIntermediateAddress
6261 // instruction.
6262 DCHECK(!(has_intermediate_address && kEmitCompilerReadBarrier));
6263
Scott Wakelingc34dba72016-10-03 10:14:44 +01006264 static_assert(
6265 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6266 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
6267 // /* HeapReference<Object> */ out =
6268 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
6269 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006270 Location temp = locations->GetTemp(0);
6271 // Note that a potential implicit null check is handled in this
6272 // CodeGeneratorARMVIXL::GenerateArrayLoadWithBakerReadBarrier call.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01006273 DCHECK(!instruction->CanDoImplicitNullCheckOn(instruction->InputAt(0)));
6274 if (index.IsConstant()) {
6275 // Array load with a constant index can be treated as a field load.
6276 data_offset += Int32ConstantFrom(index) << Primitive::ComponentSizeShift(type);
6277 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6278 out_loc,
6279 obj,
6280 data_offset,
6281 locations->GetTemp(0),
6282 /* needs_null_check */ false);
6283 } else {
6284 codegen_->GenerateArrayLoadWithBakerReadBarrier(
6285 instruction, out_loc, obj, data_offset, index, temp, /* needs_null_check */ false);
6286 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006287 } else {
6288 vixl32::Register out = OutputRegister(instruction);
6289 if (index.IsConstant()) {
6290 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00006291 (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006292 GetAssembler()->LoadFromOffset(kLoadWord, out, obj, offset);
Alexandre Rames374ddf32016-11-04 10:40:49 +00006293 // TODO(VIXL): Here and for other calls to `MaybeRecordImplicitNullCheck` in this method,
6294 // we should use a scope and the assembler to emit the load instruction to guarantee that
6295 // we record the pc at the correct position. But the `Assembler` does not automatically
6296 // handle unencodable offsets. Practically, everything is fine because the helper and
6297 // VIXL, at the time of writing, do generate the store instruction last.
Scott Wakelingc34dba72016-10-03 10:14:44 +01006298 codegen_->MaybeRecordImplicitNullCheck(instruction);
6299 // If read barriers are enabled, emit read barriers other than
6300 // Baker's using a slow path (and also unpoison the loaded
6301 // reference, if heap poisoning is enabled).
6302 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
6303 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006304 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006305 vixl32::Register temp = temps.Acquire();
6306
6307 if (has_intermediate_address) {
Artem Serov2bbc9532016-10-21 11:51:50 +01006308 // We do not need to compute the intermediate address from the array: the
6309 // input instruction has done it already. See the comment in
6310 // `TryExtractArrayAccessAddress()`.
6311 if (kIsDebugBuild) {
6312 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
Anton Kirilov644032c2016-12-06 17:51:43 +00006313 DCHECK_EQ(Uint64ConstantFrom(tmp->GetOffset()), data_offset);
Artem Serov2bbc9532016-10-21 11:51:50 +01006314 }
6315 temp = obj;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006316 } else {
6317 __ Add(temp, obj, data_offset);
6318 }
6319 codegen_->LoadFromShiftedRegOffset(type, out_loc, temp, RegisterFrom(index));
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006320 temps.Close();
Alexandre Rames374ddf32016-11-04 10:40:49 +00006321 // TODO(VIXL): Use a scope to ensure that we record the pc position immediately after the
6322 // load instruction. Practically, everything is fine because the helper and VIXL, at the
6323 // time of writing, do generate the store instruction last.
Scott Wakelingc34dba72016-10-03 10:14:44 +01006324 codegen_->MaybeRecordImplicitNullCheck(instruction);
6325 // If read barriers are enabled, emit read barriers other than
6326 // Baker's using a slow path (and also unpoison the loaded
6327 // reference, if heap poisoning is enabled).
6328 codegen_->MaybeGenerateReadBarrierSlow(
6329 instruction, out_loc, out_loc, obj_loc, data_offset, index);
6330 }
6331 }
6332 break;
6333 }
6334
6335 case Primitive::kPrimLong: {
6336 if (index.IsConstant()) {
6337 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00006338 (Int32ConstantFrom(index) << TIMES_8) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006339 GetAssembler()->LoadFromOffset(kLoadWordPair, LowRegisterFrom(out_loc), obj, offset);
6340 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006341 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006342 vixl32::Register temp = temps.Acquire();
6343 __ Add(temp, obj, Operand(RegisterFrom(index), vixl32::LSL, TIMES_8));
6344 GetAssembler()->LoadFromOffset(kLoadWordPair, LowRegisterFrom(out_loc), temp, data_offset);
6345 }
6346 break;
6347 }
6348
6349 case Primitive::kPrimFloat: {
6350 vixl32::SRegister out = SRegisterFrom(out_loc);
6351 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006352 size_t offset = (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006353 GetAssembler()->LoadSFromOffset(out, obj, offset);
6354 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006355 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006356 vixl32::Register temp = temps.Acquire();
6357 __ Add(temp, obj, Operand(RegisterFrom(index), vixl32::LSL, TIMES_4));
6358 GetAssembler()->LoadSFromOffset(out, temp, data_offset);
6359 }
6360 break;
6361 }
6362
6363 case Primitive::kPrimDouble: {
6364 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006365 size_t offset = (Int32ConstantFrom(index) << TIMES_8) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006366 GetAssembler()->LoadDFromOffset(DRegisterFrom(out_loc), obj, offset);
6367 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006368 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006369 vixl32::Register temp = temps.Acquire();
6370 __ Add(temp, obj, Operand(RegisterFrom(index), vixl32::LSL, TIMES_8));
6371 GetAssembler()->LoadDFromOffset(DRegisterFrom(out_loc), temp, data_offset);
6372 }
6373 break;
6374 }
6375
6376 case Primitive::kPrimVoid:
6377 LOG(FATAL) << "Unreachable type " << type;
6378 UNREACHABLE();
6379 }
6380
6381 if (type == Primitive::kPrimNot) {
6382 // Potential implicit null checks, in the case of reference
6383 // arrays, are handled in the previous switch statement.
6384 } else if (!maybe_compressed_char_at) {
Alexandre Rames374ddf32016-11-04 10:40:49 +00006385 // TODO(VIXL): Use a scope to ensure we record the pc info immediately after
6386 // the preceding load instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01006387 codegen_->MaybeRecordImplicitNullCheck(instruction);
6388 }
6389}
6390
6391void LocationsBuilderARMVIXL::VisitArraySet(HArraySet* instruction) {
6392 Primitive::Type value_type = instruction->GetComponentType();
6393
6394 bool needs_write_barrier =
6395 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
6396 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
6397
6398 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
6399 instruction,
6400 may_need_runtime_call_for_type_check ?
6401 LocationSummary::kCallOnSlowPath :
6402 LocationSummary::kNoCall);
6403
6404 locations->SetInAt(0, Location::RequiresRegister());
6405 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
6406 if (Primitive::IsFloatingPointType(value_type)) {
6407 locations->SetInAt(2, Location::RequiresFpuRegister());
6408 } else {
6409 locations->SetInAt(2, Location::RequiresRegister());
6410 }
6411 if (needs_write_barrier) {
6412 // Temporary registers for the write barrier.
6413 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
6414 locations->AddTemp(Location::RequiresRegister());
6415 }
6416}
6417
6418void InstructionCodeGeneratorARMVIXL::VisitArraySet(HArraySet* instruction) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006419 LocationSummary* locations = instruction->GetLocations();
6420 vixl32::Register array = InputRegisterAt(instruction, 0);
6421 Location index = locations->InAt(1);
6422 Primitive::Type value_type = instruction->GetComponentType();
6423 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
6424 bool needs_write_barrier =
6425 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
6426 uint32_t data_offset =
6427 mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
6428 Location value_loc = locations->InAt(2);
6429 HInstruction* array_instr = instruction->GetArray();
6430 bool has_intermediate_address = array_instr->IsIntermediateAddress();
Scott Wakelingc34dba72016-10-03 10:14:44 +01006431
6432 switch (value_type) {
6433 case Primitive::kPrimBoolean:
6434 case Primitive::kPrimByte:
6435 case Primitive::kPrimShort:
6436 case Primitive::kPrimChar:
6437 case Primitive::kPrimInt: {
6438 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006439 int32_t const_index = Int32ConstantFrom(index);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006440 uint32_t full_offset =
6441 data_offset + (const_index << Primitive::ComponentSizeShift(value_type));
6442 StoreOperandType store_type = GetStoreOperandType(value_type);
6443 GetAssembler()->StoreToOffset(store_type, RegisterFrom(value_loc), array, full_offset);
6444 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006445 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006446 vixl32::Register temp = temps.Acquire();
6447
6448 if (has_intermediate_address) {
Artem Serov2bbc9532016-10-21 11:51:50 +01006449 // We do not need to compute the intermediate address from the array: the
6450 // input instruction has done it already. See the comment in
6451 // `TryExtractArrayAccessAddress()`.
6452 if (kIsDebugBuild) {
6453 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
Anton Kirilov644032c2016-12-06 17:51:43 +00006454 DCHECK_EQ(Uint64ConstantFrom(tmp->GetOffset()), data_offset);
Artem Serov2bbc9532016-10-21 11:51:50 +01006455 }
6456 temp = array;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006457 } else {
6458 __ Add(temp, array, data_offset);
6459 }
6460 codegen_->StoreToShiftedRegOffset(value_type, value_loc, temp, RegisterFrom(index));
6461 }
6462 break;
6463 }
6464
6465 case Primitive::kPrimNot: {
6466 vixl32::Register value = RegisterFrom(value_loc);
6467 // TryExtractArrayAccessAddress optimization is never applied for non-primitive ArraySet.
6468 // See the comment in instruction_simplifier_shared.cc.
6469 DCHECK(!has_intermediate_address);
6470
6471 if (instruction->InputAt(2)->IsNullConstant()) {
6472 // Just setting null.
6473 if (index.IsConstant()) {
6474 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00006475 (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006476 GetAssembler()->StoreToOffset(kStoreWord, value, array, offset);
6477 } else {
6478 DCHECK(index.IsRegister()) << index;
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006479 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006480 vixl32::Register temp = temps.Acquire();
6481 __ Add(temp, array, data_offset);
6482 codegen_->StoreToShiftedRegOffset(value_type, value_loc, temp, RegisterFrom(index));
6483 }
Alexandre Rames374ddf32016-11-04 10:40:49 +00006484 // TODO(VIXL): Use a scope to ensure we record the pc info immediately after the preceding
6485 // store instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01006486 codegen_->MaybeRecordImplicitNullCheck(instruction);
6487 DCHECK(!needs_write_barrier);
6488 DCHECK(!may_need_runtime_call_for_type_check);
6489 break;
6490 }
6491
6492 DCHECK(needs_write_barrier);
6493 Location temp1_loc = locations->GetTemp(0);
6494 vixl32::Register temp1 = RegisterFrom(temp1_loc);
6495 Location temp2_loc = locations->GetTemp(1);
6496 vixl32::Register temp2 = RegisterFrom(temp2_loc);
6497 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6498 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6499 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6500 vixl32::Label done;
Anton Kirilov6f644202017-02-27 18:29:45 +00006501 vixl32::Label* final_label = codegen_->GetFinalLabel(instruction, &done);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006502 SlowPathCodeARMVIXL* slow_path = nullptr;
6503
6504 if (may_need_runtime_call_for_type_check) {
6505 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARMVIXL(instruction);
6506 codegen_->AddSlowPath(slow_path);
6507 if (instruction->GetValueCanBeNull()) {
6508 vixl32::Label non_zero;
xueliang.zhongf51bc622016-11-04 09:23:32 +00006509 __ CompareAndBranchIfNonZero(value, &non_zero);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006510 if (index.IsConstant()) {
6511 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00006512 (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006513 GetAssembler()->StoreToOffset(kStoreWord, value, array, offset);
6514 } else {
6515 DCHECK(index.IsRegister()) << index;
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006516 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006517 vixl32::Register temp = temps.Acquire();
6518 __ Add(temp, array, data_offset);
6519 codegen_->StoreToShiftedRegOffset(value_type, value_loc, temp, RegisterFrom(index));
6520 }
Alexandre Rames374ddf32016-11-04 10:40:49 +00006521 // TODO(VIXL): Use a scope to ensure we record the pc info immediately after the preceding
6522 // store instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01006523 codegen_->MaybeRecordImplicitNullCheck(instruction);
Anton Kirilov6f644202017-02-27 18:29:45 +00006524 __ B(final_label);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006525 __ Bind(&non_zero);
6526 }
6527
6528 // Note that when read barriers are enabled, the type checks
6529 // are performed without read barriers. This is fine, even in
6530 // the case where a class object is in the from-space after
6531 // the flip, as a comparison involving such a type would not
6532 // produce a false positive; it may of course produce a false
6533 // negative, in which case we would take the ArraySet slow
6534 // path.
6535
Alexandre Rames374ddf32016-11-04 10:40:49 +00006536 {
6537 // Ensure we record the pc position immediately after the `ldr` instruction.
Artem Serov0fb37192016-12-06 18:13:40 +00006538 ExactAssemblyScope aas(GetVIXLAssembler(),
6539 vixl32::kMaxInstructionSizeInBytes,
6540 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00006541 // /* HeapReference<Class> */ temp1 = array->klass_
6542 __ ldr(temp1, MemOperand(array, class_offset));
6543 codegen_->MaybeRecordImplicitNullCheck(instruction);
6544 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006545 GetAssembler()->MaybeUnpoisonHeapReference(temp1);
6546
6547 // /* HeapReference<Class> */ temp1 = temp1->component_type_
6548 GetAssembler()->LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
6549 // /* HeapReference<Class> */ temp2 = value->klass_
6550 GetAssembler()->LoadFromOffset(kLoadWord, temp2, value, class_offset);
6551 // If heap poisoning is enabled, no need to unpoison `temp1`
6552 // nor `temp2`, as we are comparing two poisoned references.
6553 __ Cmp(temp1, temp2);
6554
6555 if (instruction->StaticTypeOfArrayIsObjectArray()) {
6556 vixl32::Label do_put;
Artem Serov517d9f62016-12-12 15:51:15 +00006557 __ B(eq, &do_put, /* far_target */ false);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006558 // If heap poisoning is enabled, the `temp1` reference has
6559 // not been unpoisoned yet; unpoison it now.
6560 GetAssembler()->MaybeUnpoisonHeapReference(temp1);
6561
6562 // /* HeapReference<Class> */ temp1 = temp1->super_class_
6563 GetAssembler()->LoadFromOffset(kLoadWord, temp1, temp1, super_offset);
6564 // If heap poisoning is enabled, no need to unpoison
6565 // `temp1`, as we are comparing against null below.
xueliang.zhongf51bc622016-11-04 09:23:32 +00006566 __ CompareAndBranchIfNonZero(temp1, slow_path->GetEntryLabel());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006567 __ Bind(&do_put);
6568 } else {
6569 __ B(ne, slow_path->GetEntryLabel());
6570 }
6571 }
6572
6573 vixl32::Register source = value;
6574 if (kPoisonHeapReferences) {
6575 // Note that in the case where `value` is a null reference,
6576 // we do not enter this block, as a null reference does not
6577 // need poisoning.
6578 DCHECK_EQ(value_type, Primitive::kPrimNot);
6579 __ Mov(temp1, value);
6580 GetAssembler()->PoisonHeapReference(temp1);
6581 source = temp1;
6582 }
6583
6584 if (index.IsConstant()) {
6585 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00006586 (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006587 GetAssembler()->StoreToOffset(kStoreWord, source, array, offset);
6588 } else {
6589 DCHECK(index.IsRegister()) << index;
6590
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006591 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006592 vixl32::Register temp = temps.Acquire();
6593 __ Add(temp, array, data_offset);
6594 codegen_->StoreToShiftedRegOffset(value_type,
6595 LocationFrom(source),
6596 temp,
6597 RegisterFrom(index));
6598 }
6599
6600 if (!may_need_runtime_call_for_type_check) {
Alexandre Rames374ddf32016-11-04 10:40:49 +00006601 // TODO(VIXL): Ensure we record the pc position immediately after the preceding store
6602 // instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01006603 codegen_->MaybeRecordImplicitNullCheck(instruction);
6604 }
6605
6606 codegen_->MarkGCCard(temp1, temp2, array, value, instruction->GetValueCanBeNull());
6607
6608 if (done.IsReferenced()) {
6609 __ Bind(&done);
6610 }
6611
6612 if (slow_path != nullptr) {
6613 __ Bind(slow_path->GetExitLabel());
6614 }
6615
6616 break;
6617 }
6618
6619 case Primitive::kPrimLong: {
6620 Location value = locations->InAt(2);
6621 if (index.IsConstant()) {
6622 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00006623 (Int32ConstantFrom(index) << TIMES_8) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006624 GetAssembler()->StoreToOffset(kStoreWordPair, LowRegisterFrom(value), array, offset);
6625 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006626 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006627 vixl32::Register temp = temps.Acquire();
6628 __ Add(temp, array, Operand(RegisterFrom(index), vixl32::LSL, TIMES_8));
6629 GetAssembler()->StoreToOffset(kStoreWordPair, LowRegisterFrom(value), temp, data_offset);
6630 }
6631 break;
6632 }
6633
6634 case Primitive::kPrimFloat: {
6635 Location value = locations->InAt(2);
6636 DCHECK(value.IsFpuRegister());
6637 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006638 size_t offset = (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006639 GetAssembler()->StoreSToOffset(SRegisterFrom(value), array, offset);
6640 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006641 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006642 vixl32::Register temp = temps.Acquire();
6643 __ Add(temp, array, Operand(RegisterFrom(index), vixl32::LSL, TIMES_4));
6644 GetAssembler()->StoreSToOffset(SRegisterFrom(value), temp, data_offset);
6645 }
6646 break;
6647 }
6648
6649 case Primitive::kPrimDouble: {
6650 Location value = locations->InAt(2);
6651 DCHECK(value.IsFpuRegisterPair());
6652 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006653 size_t offset = (Int32ConstantFrom(index) << TIMES_8) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006654 GetAssembler()->StoreDToOffset(DRegisterFrom(value), array, offset);
6655 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006656 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006657 vixl32::Register temp = temps.Acquire();
6658 __ Add(temp, array, Operand(RegisterFrom(index), vixl32::LSL, TIMES_8));
6659 GetAssembler()->StoreDToOffset(DRegisterFrom(value), temp, data_offset);
6660 }
6661 break;
6662 }
6663
6664 case Primitive::kPrimVoid:
6665 LOG(FATAL) << "Unreachable type " << value_type;
6666 UNREACHABLE();
6667 }
6668
6669 // Objects are handled in the switch.
6670 if (value_type != Primitive::kPrimNot) {
Alexandre Rames374ddf32016-11-04 10:40:49 +00006671 // TODO(VIXL): Ensure we record the pc position immediately after the preceding store
6672 // instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01006673 codegen_->MaybeRecordImplicitNullCheck(instruction);
6674 }
6675}
6676
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006677void LocationsBuilderARMVIXL::VisitArrayLength(HArrayLength* instruction) {
6678 LocationSummary* locations =
6679 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6680 locations->SetInAt(0, Location::RequiresRegister());
6681 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6682}
6683
6684void InstructionCodeGeneratorARMVIXL::VisitArrayLength(HArrayLength* instruction) {
6685 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
6686 vixl32::Register obj = InputRegisterAt(instruction, 0);
6687 vixl32::Register out = OutputRegister(instruction);
Alexandre Rames374ddf32016-11-04 10:40:49 +00006688 {
Artem Serov0fb37192016-12-06 18:13:40 +00006689 ExactAssemblyScope aas(GetVIXLAssembler(),
6690 vixl32::kMaxInstructionSizeInBytes,
6691 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00006692 __ ldr(out, MemOperand(obj, offset));
6693 codegen_->MaybeRecordImplicitNullCheck(instruction);
6694 }
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006695 // Mask out compression flag from String's array length.
6696 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006697 __ Lsr(out, out, 1u);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006698 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006699}
6700
Artem Serov2bbc9532016-10-21 11:51:50 +01006701void LocationsBuilderARMVIXL::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Artem Serov2bbc9532016-10-21 11:51:50 +01006702 LocationSummary* locations =
6703 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6704
6705 locations->SetInAt(0, Location::RequiresRegister());
6706 locations->SetInAt(1, Location::RegisterOrConstant(instruction->GetOffset()));
6707 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6708}
6709
6710void InstructionCodeGeneratorARMVIXL::VisitIntermediateAddress(HIntermediateAddress* instruction) {
6711 vixl32::Register out = OutputRegister(instruction);
6712 vixl32::Register first = InputRegisterAt(instruction, 0);
6713 Location second = instruction->GetLocations()->InAt(1);
6714
Artem Serov2bbc9532016-10-21 11:51:50 +01006715 if (second.IsRegister()) {
6716 __ Add(out, first, RegisterFrom(second));
6717 } else {
Anton Kirilov644032c2016-12-06 17:51:43 +00006718 __ Add(out, first, Int32ConstantFrom(second));
Artem Serov2bbc9532016-10-21 11:51:50 +01006719 }
6720}
6721
Artem Serove1811ed2017-04-27 16:50:47 +01006722void LocationsBuilderARMVIXL::VisitIntermediateAddressIndex(
6723 HIntermediateAddressIndex* instruction) {
6724 LOG(FATAL) << "Unreachable " << instruction->GetId();
6725}
6726
6727void InstructionCodeGeneratorARMVIXL::VisitIntermediateAddressIndex(
6728 HIntermediateAddressIndex* instruction) {
6729 LOG(FATAL) << "Unreachable " << instruction->GetId();
6730}
6731
Scott Wakelingc34dba72016-10-03 10:14:44 +01006732void LocationsBuilderARMVIXL::VisitBoundsCheck(HBoundsCheck* instruction) {
6733 RegisterSet caller_saves = RegisterSet::Empty();
6734 InvokeRuntimeCallingConventionARMVIXL calling_convention;
6735 caller_saves.Add(LocationFrom(calling_convention.GetRegisterAt(0)));
6736 caller_saves.Add(LocationFrom(calling_convention.GetRegisterAt(1)));
6737 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Artem Serov2dd053d2017-03-08 14:54:06 +00006738
6739 HInstruction* index = instruction->InputAt(0);
6740 HInstruction* length = instruction->InputAt(1);
6741 // If both index and length are constants we can statically check the bounds. But if at least one
6742 // of them is not encodable ArmEncodableConstantOrRegister will create
6743 // Location::RequiresRegister() which is not desired to happen. Instead we create constant
6744 // locations.
6745 bool both_const = index->IsConstant() && length->IsConstant();
6746 locations->SetInAt(0, both_const
6747 ? Location::ConstantLocation(index->AsConstant())
6748 : ArmEncodableConstantOrRegister(index, CMP));
6749 locations->SetInAt(1, both_const
6750 ? Location::ConstantLocation(length->AsConstant())
6751 : ArmEncodableConstantOrRegister(length, CMP));
Scott Wakelingc34dba72016-10-03 10:14:44 +01006752}
6753
6754void InstructionCodeGeneratorARMVIXL::VisitBoundsCheck(HBoundsCheck* instruction) {
Artem Serov2dd053d2017-03-08 14:54:06 +00006755 LocationSummary* locations = instruction->GetLocations();
6756 Location index_loc = locations->InAt(0);
6757 Location length_loc = locations->InAt(1);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006758
Artem Serov2dd053d2017-03-08 14:54:06 +00006759 if (length_loc.IsConstant()) {
6760 int32_t length = Int32ConstantFrom(length_loc);
6761 if (index_loc.IsConstant()) {
6762 // BCE will remove the bounds check if we are guaranteed to pass.
6763 int32_t index = Int32ConstantFrom(index_loc);
6764 if (index < 0 || index >= length) {
6765 SlowPathCodeARMVIXL* slow_path =
6766 new (GetGraph()->GetArena()) BoundsCheckSlowPathARMVIXL(instruction);
6767 codegen_->AddSlowPath(slow_path);
6768 __ B(slow_path->GetEntryLabel());
6769 } else {
6770 // Some optimization after BCE may have generated this, and we should not
6771 // generate a bounds check if it is a valid range.
6772 }
6773 return;
6774 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006775
Artem Serov2dd053d2017-03-08 14:54:06 +00006776 SlowPathCodeARMVIXL* slow_path =
6777 new (GetGraph()->GetArena()) BoundsCheckSlowPathARMVIXL(instruction);
6778 __ Cmp(RegisterFrom(index_loc), length);
6779 codegen_->AddSlowPath(slow_path);
6780 __ B(hs, slow_path->GetEntryLabel());
6781 } else {
6782 SlowPathCodeARMVIXL* slow_path =
6783 new (GetGraph()->GetArena()) BoundsCheckSlowPathARMVIXL(instruction);
6784 __ Cmp(RegisterFrom(length_loc), InputOperandAt(instruction, 0));
6785 codegen_->AddSlowPath(slow_path);
6786 __ B(ls, slow_path->GetEntryLabel());
6787 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006788}
6789
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006790void CodeGeneratorARMVIXL::MarkGCCard(vixl32::Register temp,
6791 vixl32::Register card,
6792 vixl32::Register object,
6793 vixl32::Register value,
6794 bool can_be_null) {
6795 vixl32::Label is_null;
6796 if (can_be_null) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00006797 __ CompareAndBranchIfZero(value, &is_null);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006798 }
6799 GetAssembler()->LoadFromOffset(
6800 kLoadWord, card, tr, Thread::CardTableOffset<kArmPointerSize>().Int32Value());
Scott Wakelingb77051e2016-11-21 19:46:00 +00006801 __ Lsr(temp, object, Operand::From(gc::accounting::CardTable::kCardShift));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006802 __ Strb(card, MemOperand(card, temp));
6803 if (can_be_null) {
6804 __ Bind(&is_null);
6805 }
6806}
6807
Scott Wakelingfe885462016-09-22 10:24:38 +01006808void LocationsBuilderARMVIXL::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
6809 LOG(FATAL) << "Unreachable";
6810}
6811
6812void InstructionCodeGeneratorARMVIXL::VisitParallelMove(HParallelMove* instruction) {
6813 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
6814}
6815
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006816void LocationsBuilderARMVIXL::VisitSuspendCheck(HSuspendCheck* instruction) {
Artem Serov657022c2016-11-23 14:19:38 +00006817 LocationSummary* locations =
6818 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
6819 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006820}
6821
6822void InstructionCodeGeneratorARMVIXL::VisitSuspendCheck(HSuspendCheck* instruction) {
6823 HBasicBlock* block = instruction->GetBlock();
6824 if (block->GetLoopInformation() != nullptr) {
6825 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
6826 // The back edge will generate the suspend check.
6827 return;
6828 }
6829 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
6830 // The goto will generate the suspend check.
6831 return;
6832 }
6833 GenerateSuspendCheck(instruction, nullptr);
Roland Levillain5daa4952017-07-03 17:23:56 +01006834 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 12);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006835}
6836
6837void InstructionCodeGeneratorARMVIXL::GenerateSuspendCheck(HSuspendCheck* instruction,
6838 HBasicBlock* successor) {
6839 SuspendCheckSlowPathARMVIXL* slow_path =
6840 down_cast<SuspendCheckSlowPathARMVIXL*>(instruction->GetSlowPath());
6841 if (slow_path == nullptr) {
6842 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARMVIXL(instruction, successor);
6843 instruction->SetSlowPath(slow_path);
6844 codegen_->AddSlowPath(slow_path);
6845 if (successor != nullptr) {
6846 DCHECK(successor->IsLoopHeader());
6847 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
6848 }
6849 } else {
6850 DCHECK_EQ(slow_path->GetSuccessor(), successor);
6851 }
6852
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006853 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006854 vixl32::Register temp = temps.Acquire();
6855 GetAssembler()->LoadFromOffset(
6856 kLoadUnsignedHalfword, temp, tr, Thread::ThreadFlagsOffset<kArmPointerSize>().Int32Value());
6857 if (successor == nullptr) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00006858 __ CompareAndBranchIfNonZero(temp, slow_path->GetEntryLabel());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006859 __ Bind(slow_path->GetReturnLabel());
6860 } else {
xueliang.zhongf51bc622016-11-04 09:23:32 +00006861 __ CompareAndBranchIfZero(temp, codegen_->GetLabelOf(successor));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006862 __ B(slow_path->GetEntryLabel());
6863 }
6864}
6865
Scott Wakelingfe885462016-09-22 10:24:38 +01006866ArmVIXLAssembler* ParallelMoveResolverARMVIXL::GetAssembler() const {
6867 return codegen_->GetAssembler();
6868}
6869
6870void ParallelMoveResolverARMVIXL::EmitMove(size_t index) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006871 UseScratchRegisterScope temps(GetAssembler()->GetVIXLAssembler());
Scott Wakelingfe885462016-09-22 10:24:38 +01006872 MoveOperands* move = moves_[index];
6873 Location source = move->GetSource();
6874 Location destination = move->GetDestination();
6875
6876 if (source.IsRegister()) {
6877 if (destination.IsRegister()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006878 __ Mov(RegisterFrom(destination), RegisterFrom(source));
Scott Wakelingfe885462016-09-22 10:24:38 +01006879 } else if (destination.IsFpuRegister()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006880 __ Vmov(SRegisterFrom(destination), RegisterFrom(source));
Scott Wakelingfe885462016-09-22 10:24:38 +01006881 } else {
6882 DCHECK(destination.IsStackSlot());
6883 GetAssembler()->StoreToOffset(kStoreWord,
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006884 RegisterFrom(source),
Scott Wakelingfe885462016-09-22 10:24:38 +01006885 sp,
6886 destination.GetStackIndex());
6887 }
6888 } else if (source.IsStackSlot()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006889 if (destination.IsRegister()) {
6890 GetAssembler()->LoadFromOffset(kLoadWord,
6891 RegisterFrom(destination),
6892 sp,
6893 source.GetStackIndex());
6894 } else if (destination.IsFpuRegister()) {
6895 GetAssembler()->LoadSFromOffset(SRegisterFrom(destination), sp, source.GetStackIndex());
6896 } else {
6897 DCHECK(destination.IsStackSlot());
6898 vixl32::Register temp = temps.Acquire();
6899 GetAssembler()->LoadFromOffset(kLoadWord, temp, sp, source.GetStackIndex());
6900 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
6901 }
Scott Wakelingfe885462016-09-22 10:24:38 +01006902 } else if (source.IsFpuRegister()) {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01006903 if (destination.IsRegister()) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006904 __ Vmov(RegisterFrom(destination), SRegisterFrom(source));
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01006905 } else if (destination.IsFpuRegister()) {
6906 __ Vmov(SRegisterFrom(destination), SRegisterFrom(source));
6907 } else {
6908 DCHECK(destination.IsStackSlot());
6909 GetAssembler()->StoreSToOffset(SRegisterFrom(source), sp, destination.GetStackIndex());
6910 }
Scott Wakelingfe885462016-09-22 10:24:38 +01006911 } else if (source.IsDoubleStackSlot()) {
Alexandre Rames9c19bd62016-10-24 11:50:32 +01006912 if (destination.IsDoubleStackSlot()) {
6913 vixl32::DRegister temp = temps.AcquireD();
6914 GetAssembler()->LoadDFromOffset(temp, sp, source.GetStackIndex());
6915 GetAssembler()->StoreDToOffset(temp, sp, destination.GetStackIndex());
6916 } else if (destination.IsRegisterPair()) {
6917 DCHECK(ExpectedPairLayout(destination));
6918 GetAssembler()->LoadFromOffset(
6919 kLoadWordPair, LowRegisterFrom(destination), sp, source.GetStackIndex());
6920 } else {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01006921 DCHECK(destination.IsFpuRegisterPair()) << destination;
6922 GetAssembler()->LoadDFromOffset(DRegisterFrom(destination), sp, source.GetStackIndex());
Alexandre Rames9c19bd62016-10-24 11:50:32 +01006923 }
Scott Wakelingfe885462016-09-22 10:24:38 +01006924 } else if (source.IsRegisterPair()) {
6925 if (destination.IsRegisterPair()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006926 __ Mov(LowRegisterFrom(destination), LowRegisterFrom(source));
6927 __ Mov(HighRegisterFrom(destination), HighRegisterFrom(source));
Scott Wakelingfe885462016-09-22 10:24:38 +01006928 } else if (destination.IsFpuRegisterPair()) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006929 __ Vmov(DRegisterFrom(destination), LowRegisterFrom(source), HighRegisterFrom(source));
Scott Wakelingfe885462016-09-22 10:24:38 +01006930 } else {
6931 DCHECK(destination.IsDoubleStackSlot()) << destination;
6932 DCHECK(ExpectedPairLayout(source));
6933 GetAssembler()->StoreToOffset(kStoreWordPair,
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006934 LowRegisterFrom(source),
Scott Wakelingfe885462016-09-22 10:24:38 +01006935 sp,
6936 destination.GetStackIndex());
6937 }
6938 } else if (source.IsFpuRegisterPair()) {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01006939 if (destination.IsRegisterPair()) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006940 __ Vmov(LowRegisterFrom(destination), HighRegisterFrom(destination), DRegisterFrom(source));
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01006941 } else if (destination.IsFpuRegisterPair()) {
6942 __ Vmov(DRegisterFrom(destination), DRegisterFrom(source));
6943 } else {
6944 DCHECK(destination.IsDoubleStackSlot()) << destination;
6945 GetAssembler()->StoreDToOffset(DRegisterFrom(source), sp, destination.GetStackIndex());
6946 }
Scott Wakelingfe885462016-09-22 10:24:38 +01006947 } else {
6948 DCHECK(source.IsConstant()) << source;
6949 HConstant* constant = source.GetConstant();
6950 if (constant->IsIntConstant() || constant->IsNullConstant()) {
6951 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
6952 if (destination.IsRegister()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006953 __ Mov(RegisterFrom(destination), value);
Scott Wakelingfe885462016-09-22 10:24:38 +01006954 } else {
6955 DCHECK(destination.IsStackSlot());
Scott Wakelingfe885462016-09-22 10:24:38 +01006956 vixl32::Register temp = temps.Acquire();
6957 __ Mov(temp, value);
6958 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
6959 }
6960 } else if (constant->IsLongConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006961 int64_t value = Int64ConstantFrom(source);
Scott Wakelingfe885462016-09-22 10:24:38 +01006962 if (destination.IsRegisterPair()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006963 __ Mov(LowRegisterFrom(destination), Low32Bits(value));
6964 __ Mov(HighRegisterFrom(destination), High32Bits(value));
Scott Wakelingfe885462016-09-22 10:24:38 +01006965 } else {
6966 DCHECK(destination.IsDoubleStackSlot()) << destination;
Scott Wakelingfe885462016-09-22 10:24:38 +01006967 vixl32::Register temp = temps.Acquire();
6968 __ Mov(temp, Low32Bits(value));
6969 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
6970 __ Mov(temp, High32Bits(value));
6971 GetAssembler()->StoreToOffset(kStoreWord,
6972 temp,
6973 sp,
6974 destination.GetHighStackIndex(kArmWordSize));
6975 }
6976 } else if (constant->IsDoubleConstant()) {
6977 double value = constant->AsDoubleConstant()->GetValue();
6978 if (destination.IsFpuRegisterPair()) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006979 __ Vmov(DRegisterFrom(destination), value);
Scott Wakelingfe885462016-09-22 10:24:38 +01006980 } else {
6981 DCHECK(destination.IsDoubleStackSlot()) << destination;
6982 uint64_t int_value = bit_cast<uint64_t, double>(value);
Scott Wakelingfe885462016-09-22 10:24:38 +01006983 vixl32::Register temp = temps.Acquire();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006984 __ Mov(temp, Low32Bits(int_value));
Scott Wakelingfe885462016-09-22 10:24:38 +01006985 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006986 __ Mov(temp, High32Bits(int_value));
Scott Wakelingfe885462016-09-22 10:24:38 +01006987 GetAssembler()->StoreToOffset(kStoreWord,
6988 temp,
6989 sp,
6990 destination.GetHighStackIndex(kArmWordSize));
6991 }
6992 } else {
6993 DCHECK(constant->IsFloatConstant()) << constant->DebugName();
6994 float value = constant->AsFloatConstant()->GetValue();
6995 if (destination.IsFpuRegister()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006996 __ Vmov(SRegisterFrom(destination), value);
Scott Wakelingfe885462016-09-22 10:24:38 +01006997 } else {
6998 DCHECK(destination.IsStackSlot());
Scott Wakelingfe885462016-09-22 10:24:38 +01006999 vixl32::Register temp = temps.Acquire();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007000 __ Mov(temp, bit_cast<int32_t, float>(value));
Scott Wakelingfe885462016-09-22 10:24:38 +01007001 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
7002 }
7003 }
7004 }
7005}
7006
Alexandre Rames9c19bd62016-10-24 11:50:32 +01007007void ParallelMoveResolverARMVIXL::Exchange(vixl32::Register reg, int mem) {
7008 UseScratchRegisterScope temps(GetAssembler()->GetVIXLAssembler());
7009 vixl32::Register temp = temps.Acquire();
7010 __ Mov(temp, reg);
7011 GetAssembler()->LoadFromOffset(kLoadWord, reg, sp, mem);
7012 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, mem);
Scott Wakelingfe885462016-09-22 10:24:38 +01007013}
7014
Alexandre Rames9c19bd62016-10-24 11:50:32 +01007015void ParallelMoveResolverARMVIXL::Exchange(int mem1, int mem2) {
7016 // TODO(VIXL32): Double check the performance of this implementation.
7017 UseScratchRegisterScope temps(GetAssembler()->GetVIXLAssembler());
Nicolas Geoffray13a797b2017-03-15 16:41:31 +00007018 vixl32::Register temp1 = temps.Acquire();
7019 ScratchRegisterScope ensure_scratch(
7020 this, temp1.GetCode(), r0.GetCode(), codegen_->GetNumberOfCoreRegisters());
7021 vixl32::Register temp2(ensure_scratch.GetRegister());
Alexandre Rames9c19bd62016-10-24 11:50:32 +01007022
Nicolas Geoffray13a797b2017-03-15 16:41:31 +00007023 int stack_offset = ensure_scratch.IsSpilled() ? kArmWordSize : 0;
7024 GetAssembler()->LoadFromOffset(kLoadWord, temp1, sp, mem1 + stack_offset);
7025 GetAssembler()->LoadFromOffset(kLoadWord, temp2, sp, mem2 + stack_offset);
7026 GetAssembler()->StoreToOffset(kStoreWord, temp1, sp, mem2 + stack_offset);
7027 GetAssembler()->StoreToOffset(kStoreWord, temp2, sp, mem1 + stack_offset);
Scott Wakelingfe885462016-09-22 10:24:38 +01007028}
7029
Alexandre Rames9c19bd62016-10-24 11:50:32 +01007030void ParallelMoveResolverARMVIXL::EmitSwap(size_t index) {
7031 MoveOperands* move = moves_[index];
7032 Location source = move->GetSource();
7033 Location destination = move->GetDestination();
7034 UseScratchRegisterScope temps(GetAssembler()->GetVIXLAssembler());
7035
7036 if (source.IsRegister() && destination.IsRegister()) {
7037 vixl32::Register temp = temps.Acquire();
7038 DCHECK(!RegisterFrom(source).Is(temp));
7039 DCHECK(!RegisterFrom(destination).Is(temp));
7040 __ Mov(temp, RegisterFrom(destination));
7041 __ Mov(RegisterFrom(destination), RegisterFrom(source));
7042 __ Mov(RegisterFrom(source), temp);
7043 } else if (source.IsRegister() && destination.IsStackSlot()) {
7044 Exchange(RegisterFrom(source), destination.GetStackIndex());
7045 } else if (source.IsStackSlot() && destination.IsRegister()) {
7046 Exchange(RegisterFrom(destination), source.GetStackIndex());
7047 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Anton Kirilovdda43962016-11-21 19:55:20 +00007048 Exchange(source.GetStackIndex(), destination.GetStackIndex());
Alexandre Rames9c19bd62016-10-24 11:50:32 +01007049 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Nicolas Geoffray13a797b2017-03-15 16:41:31 +00007050 vixl32::Register temp = temps.Acquire();
Anton Kirilovdda43962016-11-21 19:55:20 +00007051 __ Vmov(temp, SRegisterFrom(source));
7052 __ Vmov(SRegisterFrom(source), SRegisterFrom(destination));
7053 __ Vmov(SRegisterFrom(destination), temp);
Alexandre Rames9c19bd62016-10-24 11:50:32 +01007054 } else if (source.IsRegisterPair() && destination.IsRegisterPair()) {
7055 vixl32::DRegister temp = temps.AcquireD();
7056 __ Vmov(temp, LowRegisterFrom(source), HighRegisterFrom(source));
7057 __ Mov(LowRegisterFrom(source), LowRegisterFrom(destination));
7058 __ Mov(HighRegisterFrom(source), HighRegisterFrom(destination));
7059 __ Vmov(LowRegisterFrom(destination), HighRegisterFrom(destination), temp);
7060 } else if (source.IsRegisterPair() || destination.IsRegisterPair()) {
7061 vixl32::Register low_reg = LowRegisterFrom(source.IsRegisterPair() ? source : destination);
7062 int mem = source.IsRegisterPair() ? destination.GetStackIndex() : source.GetStackIndex();
7063 DCHECK(ExpectedPairLayout(source.IsRegisterPair() ? source : destination));
7064 vixl32::DRegister temp = temps.AcquireD();
7065 __ Vmov(temp, low_reg, vixl32::Register(low_reg.GetCode() + 1));
7066 GetAssembler()->LoadFromOffset(kLoadWordPair, low_reg, sp, mem);
7067 GetAssembler()->StoreDToOffset(temp, sp, mem);
7068 } else if (source.IsFpuRegisterPair() && destination.IsFpuRegisterPair()) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007069 vixl32::DRegister first = DRegisterFrom(source);
7070 vixl32::DRegister second = DRegisterFrom(destination);
7071 vixl32::DRegister temp = temps.AcquireD();
7072 __ Vmov(temp, first);
7073 __ Vmov(first, second);
7074 __ Vmov(second, temp);
Alexandre Rames9c19bd62016-10-24 11:50:32 +01007075 } else if (source.IsFpuRegisterPair() || destination.IsFpuRegisterPair()) {
Anton Kirilovdda43962016-11-21 19:55:20 +00007076 vixl32::DRegister reg = source.IsFpuRegisterPair()
7077 ? DRegisterFrom(source)
7078 : DRegisterFrom(destination);
7079 int mem = source.IsFpuRegisterPair()
7080 ? destination.GetStackIndex()
7081 : source.GetStackIndex();
7082 vixl32::DRegister temp = temps.AcquireD();
7083 __ Vmov(temp, reg);
7084 GetAssembler()->LoadDFromOffset(reg, sp, mem);
7085 GetAssembler()->StoreDToOffset(temp, sp, mem);
Alexandre Rames9c19bd62016-10-24 11:50:32 +01007086 } else if (source.IsFpuRegister() || destination.IsFpuRegister()) {
Anton Kirilovdda43962016-11-21 19:55:20 +00007087 vixl32::SRegister reg = source.IsFpuRegister()
7088 ? SRegisterFrom(source)
7089 : SRegisterFrom(destination);
7090 int mem = source.IsFpuRegister()
7091 ? destination.GetStackIndex()
7092 : source.GetStackIndex();
7093 vixl32::Register temp = temps.Acquire();
7094 __ Vmov(temp, reg);
7095 GetAssembler()->LoadSFromOffset(reg, sp, mem);
7096 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, mem);
Alexandre Rames9c19bd62016-10-24 11:50:32 +01007097 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
7098 vixl32::DRegister temp1 = temps.AcquireD();
7099 vixl32::DRegister temp2 = temps.AcquireD();
7100 __ Vldr(temp1, MemOperand(sp, source.GetStackIndex()));
7101 __ Vldr(temp2, MemOperand(sp, destination.GetStackIndex()));
7102 __ Vstr(temp1, MemOperand(sp, destination.GetStackIndex()));
7103 __ Vstr(temp2, MemOperand(sp, source.GetStackIndex()));
7104 } else {
7105 LOG(FATAL) << "Unimplemented" << source << " <-> " << destination;
7106 }
Scott Wakelingfe885462016-09-22 10:24:38 +01007107}
7108
Nicolas Geoffray13a797b2017-03-15 16:41:31 +00007109void ParallelMoveResolverARMVIXL::SpillScratch(int reg) {
7110 __ Push(vixl32::Register(reg));
Scott Wakelingfe885462016-09-22 10:24:38 +01007111}
7112
Nicolas Geoffray13a797b2017-03-15 16:41:31 +00007113void ParallelMoveResolverARMVIXL::RestoreScratch(int reg) {
7114 __ Pop(vixl32::Register(reg));
Scott Wakelingfe885462016-09-22 10:24:38 +01007115}
7116
Artem Serov02d37832016-10-25 15:25:33 +01007117HLoadClass::LoadKind CodeGeneratorARMVIXL::GetSupportedLoadClassKind(
Artem Serovd4cc5b22016-11-04 11:19:09 +00007118 HLoadClass::LoadKind desired_class_load_kind) {
7119 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00007120 case HLoadClass::LoadKind::kInvalid:
7121 LOG(FATAL) << "UNREACHABLE";
7122 UNREACHABLE();
Artem Serovd4cc5b22016-11-04 11:19:09 +00007123 case HLoadClass::LoadKind::kReferrersClass:
7124 break;
Artem Serovd4cc5b22016-11-04 11:19:09 +00007125 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko94ec2db2017-09-06 17:21:03 +01007126 case HLoadClass::LoadKind::kBootImageClassTable:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007127 case HLoadClass::LoadKind::kBssEntry:
7128 DCHECK(!Runtime::Current()->UseJitCompilation());
7129 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007130 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007131 DCHECK(Runtime::Current()->UseJitCompilation());
Artem Serovc5fcb442016-12-02 19:19:58 +00007132 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01007133 case HLoadClass::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007134 case HLoadClass::LoadKind::kRuntimeCall:
Artem Serovd4cc5b22016-11-04 11:19:09 +00007135 break;
7136 }
7137 return desired_class_load_kind;
Artem Serov02d37832016-10-25 15:25:33 +01007138}
7139
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007140void LocationsBuilderARMVIXL::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00007141 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007142 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007143 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00007144 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007145 cls,
7146 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00007147 LocationFrom(r0));
Vladimir Markoea4c1262017-02-06 19:59:33 +00007148 DCHECK(calling_convention.GetRegisterAt(0).Is(r0));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007149 return;
7150 }
Vladimir Marko41559982017-01-06 14:04:23 +00007151 DCHECK(!cls->NeedsAccessCheck());
Scott Wakelingfe885462016-09-22 10:24:38 +01007152
Artem Serovd4cc5b22016-11-04 11:19:09 +00007153 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
7154 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007155 ? LocationSummary::kCallOnSlowPath
7156 : LocationSummary::kNoCall;
7157 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Artem Serovd4cc5b22016-11-04 11:19:09 +00007158 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007159 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Artem Serovd4cc5b22016-11-04 11:19:09 +00007160 }
7161
Vladimir Marko41559982017-01-06 14:04:23 +00007162 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007163 locations->SetInAt(0, Location::RequiresRegister());
7164 }
7165 locations->SetOut(Location::RequiresRegister());
Vladimir Markoea4c1262017-02-06 19:59:33 +00007166 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
7167 if (!kUseReadBarrier || kUseBakerReadBarrier) {
7168 // Rely on the type resolution or initialization and marking to save everything we need.
7169 // Note that IP may be clobbered by saving/restoring the live register (only one thanks
7170 // to the custom calling convention) or by marking, so we request a different temp.
7171 locations->AddTemp(Location::RequiresRegister());
7172 RegisterSet caller_saves = RegisterSet::Empty();
7173 InvokeRuntimeCallingConventionARMVIXL calling_convention;
7174 caller_saves.Add(LocationFrom(calling_convention.GetRegisterAt(0)));
7175 // TODO: Add GetReturnLocation() to the calling convention so that we can DCHECK()
7176 // that the the kPrimNot result register is the same as the first argument register.
7177 locations->SetCustomSlowPathCallerSaves(caller_saves);
7178 } else {
7179 // For non-Baker read barrier we have a temp-clobbering call.
7180 }
7181 }
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01007182 if (kUseBakerReadBarrier && kBakerReadBarrierLinkTimeThunksEnableForGcRoots) {
7183 if (load_kind == HLoadClass::LoadKind::kBssEntry ||
7184 (load_kind == HLoadClass::LoadKind::kReferrersClass &&
7185 !Runtime::Current()->UseJitCompilation())) {
7186 locations->AddTemp(Location::RegisterLocation(kBakerCcEntrypointRegister.GetCode()));
7187 }
7188 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007189}
7190
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007191// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
7192// move.
7193void InstructionCodeGeneratorARMVIXL::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00007194 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007195 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00007196 codegen_->GenerateLoadClassRuntimeCall(cls);
Roland Levillain5daa4952017-07-03 17:23:56 +01007197 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 13);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007198 return;
7199 }
Vladimir Marko41559982017-01-06 14:04:23 +00007200 DCHECK(!cls->NeedsAccessCheck());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007201
Vladimir Marko41559982017-01-06 14:04:23 +00007202 LocationSummary* locations = cls->GetLocations();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007203 Location out_loc = locations->Out();
7204 vixl32::Register out = OutputRegister(cls);
7205
Artem Serovd4cc5b22016-11-04 11:19:09 +00007206 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
7207 ? kWithoutReadBarrier
7208 : kCompilerReadBarrierOption;
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007209 bool generate_null_check = false;
Vladimir Marko41559982017-01-06 14:04:23 +00007210 switch (load_kind) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007211 case HLoadClass::LoadKind::kReferrersClass: {
7212 DCHECK(!cls->CanCallRuntime());
7213 DCHECK(!cls->MustGenerateClinitCheck());
7214 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
7215 vixl32::Register current_method = InputRegisterAt(cls, 0);
7216 GenerateGcRootFieldLoad(cls,
7217 out_loc,
7218 current_method,
Roland Levillain00468f32016-10-27 18:02:48 +01007219 ArtMethod::DeclaringClassOffset().Int32Value(),
Artem Serovd4cc5b22016-11-04 11:19:09 +00007220 read_barrier_option);
7221 break;
7222 }
Artem Serovd4cc5b22016-11-04 11:19:09 +00007223 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007224 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Artem Serovd4cc5b22016-11-04 11:19:09 +00007225 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
7226 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
7227 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
7228 codegen_->EmitMovwMovtPlaceholder(labels, out);
7229 break;
7230 }
7231 case HLoadClass::LoadKind::kBootImageAddress: {
Artem Serovc5fcb442016-12-02 19:19:58 +00007232 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007233 uint32_t address = dchecked_integral_cast<uint32_t>(
7234 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
7235 DCHECK_NE(address, 0u);
Artem Serovc5fcb442016-12-02 19:19:58 +00007236 __ Ldr(out, codegen_->DeduplicateBootImageAddressLiteral(address));
Artem Serovd4cc5b22016-11-04 11:19:09 +00007237 break;
7238 }
Vladimir Marko94ec2db2017-09-06 17:21:03 +01007239 case HLoadClass::LoadKind::kBootImageClassTable: {
7240 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
7241 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
7242 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
7243 codegen_->EmitMovwMovtPlaceholder(labels, out);
7244 __ Ldr(out, MemOperand(out, /* offset */ 0));
7245 // Extract the reference from the slot data, i.e. clear the hash bits.
7246 int32_t masked_hash = ClassTable::TableSlot::MaskHash(
7247 ComputeModifiedUtf8Hash(cls->GetDexFile().StringByTypeIdx(cls->GetTypeIndex())));
7248 if (masked_hash != 0) {
7249 __ Sub(out, out, Operand(masked_hash));
7250 }
7251 break;
7252 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007253 case HLoadClass::LoadKind::kBssEntry: {
Vladimir Markoea4c1262017-02-06 19:59:33 +00007254 vixl32::Register temp = (!kUseReadBarrier || kUseBakerReadBarrier)
7255 ? RegisterFrom(locations->GetTemp(0))
7256 : out;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007257 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
Vladimir Marko1998cd02017-01-13 13:02:58 +00007258 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex());
Vladimir Markoea4c1262017-02-06 19:59:33 +00007259 codegen_->EmitMovwMovtPlaceholder(labels, temp);
7260 GenerateGcRootFieldLoad(cls, out_loc, temp, /* offset */ 0, read_barrier_option);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007261 generate_null_check = true;
7262 break;
7263 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007264 case HLoadClass::LoadKind::kJitTableAddress: {
Artem Serovc5fcb442016-12-02 19:19:58 +00007265 __ Ldr(out, codegen_->DeduplicateJitClassLiteral(cls->GetDexFile(),
7266 cls->GetTypeIndex(),
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007267 cls->GetClass()));
Artem Serovc5fcb442016-12-02 19:19:58 +00007268 // /* GcRoot<mirror::Class> */ out = *out
Vladimir Markoea4c1262017-02-06 19:59:33 +00007269 GenerateGcRootFieldLoad(cls, out_loc, out, /* offset */ 0, read_barrier_option);
Artem Serovd4cc5b22016-11-04 11:19:09 +00007270 break;
7271 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007272 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00007273 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00007274 LOG(FATAL) << "UNREACHABLE";
7275 UNREACHABLE();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007276 }
7277
7278 if (generate_null_check || cls->MustGenerateClinitCheck()) {
7279 DCHECK(cls->CanCallRuntime());
7280 LoadClassSlowPathARMVIXL* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARMVIXL(
7281 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
7282 codegen_->AddSlowPath(slow_path);
7283 if (generate_null_check) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00007284 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007285 }
7286 if (cls->MustGenerateClinitCheck()) {
7287 GenerateClassInitializationCheck(slow_path, out);
7288 } else {
7289 __ Bind(slow_path->GetExitLabel());
7290 }
Roland Levillain5daa4952017-07-03 17:23:56 +01007291 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 14);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007292 }
7293}
7294
Artem Serov02d37832016-10-25 15:25:33 +01007295void LocationsBuilderARMVIXL::VisitClinitCheck(HClinitCheck* check) {
7296 LocationSummary* locations =
7297 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
7298 locations->SetInAt(0, Location::RequiresRegister());
7299 if (check->HasUses()) {
7300 locations->SetOut(Location::SameAsFirstInput());
7301 }
7302}
7303
7304void InstructionCodeGeneratorARMVIXL::VisitClinitCheck(HClinitCheck* check) {
7305 // We assume the class is not null.
7306 LoadClassSlowPathARMVIXL* slow_path =
7307 new (GetGraph()->GetArena()) LoadClassSlowPathARMVIXL(check->GetLoadClass(),
7308 check,
7309 check->GetDexPc(),
7310 /* do_clinit */ true);
7311 codegen_->AddSlowPath(slow_path);
7312 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
7313}
7314
7315void InstructionCodeGeneratorARMVIXL::GenerateClassInitializationCheck(
7316 LoadClassSlowPathARMVIXL* slow_path, vixl32::Register class_reg) {
7317 UseScratchRegisterScope temps(GetVIXLAssembler());
7318 vixl32::Register temp = temps.Acquire();
7319 GetAssembler()->LoadFromOffset(kLoadWord,
7320 temp,
7321 class_reg,
7322 mirror::Class::StatusOffset().Int32Value());
7323 __ Cmp(temp, mirror::Class::kStatusInitialized);
7324 __ B(lt, slow_path->GetEntryLabel());
7325 // Even if the initialized flag is set, we may be in a situation where caches are not synced
7326 // properly. Therefore, we do a memory fence.
7327 __ Dmb(ISH);
7328 __ Bind(slow_path->GetExitLabel());
7329}
7330
Artem Serov02d37832016-10-25 15:25:33 +01007331HLoadString::LoadKind CodeGeneratorARMVIXL::GetSupportedLoadStringKind(
Artem Serovd4cc5b22016-11-04 11:19:09 +00007332 HLoadString::LoadKind desired_string_load_kind) {
7333 switch (desired_string_load_kind) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00007334 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007335 case HLoadString::LoadKind::kBootImageInternTable:
Artem Serovd4cc5b22016-11-04 11:19:09 +00007336 case HLoadString::LoadKind::kBssEntry:
7337 DCHECK(!Runtime::Current()->UseJitCompilation());
7338 break;
7339 case HLoadString::LoadKind::kJitTableAddress:
7340 DCHECK(Runtime::Current()->UseJitCompilation());
Artem Serovc5fcb442016-12-02 19:19:58 +00007341 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01007342 case HLoadString::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007343 case HLoadString::LoadKind::kRuntimeCall:
Artem Serovd4cc5b22016-11-04 11:19:09 +00007344 break;
7345 }
7346 return desired_string_load_kind;
Artem Serov02d37832016-10-25 15:25:33 +01007347}
7348
7349void LocationsBuilderARMVIXL::VisitLoadString(HLoadString* load) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00007350 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Artem Serov02d37832016-10-25 15:25:33 +01007351 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Artem Serov02d37832016-10-25 15:25:33 +01007352 HLoadString::LoadKind load_kind = load->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007353 if (load_kind == HLoadString::LoadKind::kRuntimeCall) {
Artem Serov02d37832016-10-25 15:25:33 +01007354 locations->SetOut(LocationFrom(r0));
7355 } else {
7356 locations->SetOut(Location::RequiresRegister());
Artem Serovd4cc5b22016-11-04 11:19:09 +00007357 if (load_kind == HLoadString::LoadKind::kBssEntry) {
7358 if (!kUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00007359 // Rely on the pResolveString and marking to save everything we need, including temps.
7360 // Note that IP may be clobbered by saving/restoring the live register (only one thanks
7361 // to the custom calling convention) or by marking, so we request a different temp.
Artem Serovd4cc5b22016-11-04 11:19:09 +00007362 locations->AddTemp(Location::RequiresRegister());
7363 RegisterSet caller_saves = RegisterSet::Empty();
7364 InvokeRuntimeCallingConventionARMVIXL calling_convention;
7365 caller_saves.Add(LocationFrom(calling_convention.GetRegisterAt(0)));
7366 // TODO: Add GetReturnLocation() to the calling convention so that we can DCHECK()
7367 // that the the kPrimNot result register is the same as the first argument register.
7368 locations->SetCustomSlowPathCallerSaves(caller_saves);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01007369 if (kUseBakerReadBarrier && kBakerReadBarrierLinkTimeThunksEnableForGcRoots) {
7370 locations->AddTemp(Location::RegisterLocation(kBakerCcEntrypointRegister.GetCode()));
7371 }
Artem Serovd4cc5b22016-11-04 11:19:09 +00007372 } else {
7373 // For non-Baker read barrier we have a temp-clobbering call.
7374 }
7375 }
Artem Serov02d37832016-10-25 15:25:33 +01007376 }
7377}
7378
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007379// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
7380// move.
7381void InstructionCodeGeneratorARMVIXL::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Artem Serovd4cc5b22016-11-04 11:19:09 +00007382 LocationSummary* locations = load->GetLocations();
7383 Location out_loc = locations->Out();
7384 vixl32::Register out = OutputRegister(load);
7385 HLoadString::LoadKind load_kind = load->GetLoadKind();
7386
7387 switch (load_kind) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00007388 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
7389 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
7390 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007391 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Artem Serovd4cc5b22016-11-04 11:19:09 +00007392 codegen_->EmitMovwMovtPlaceholder(labels, out);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007393 return;
Artem Serovd4cc5b22016-11-04 11:19:09 +00007394 }
7395 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007396 uint32_t address = dchecked_integral_cast<uint32_t>(
7397 reinterpret_cast<uintptr_t>(load->GetString().Get()));
7398 DCHECK_NE(address, 0u);
Artem Serovc5fcb442016-12-02 19:19:58 +00007399 __ Ldr(out, codegen_->DeduplicateBootImageAddressLiteral(address));
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007400 return;
7401 }
7402 case HLoadString::LoadKind::kBootImageInternTable: {
7403 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
7404 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
7405 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
7406 codegen_->EmitMovwMovtPlaceholder(labels, out);
7407 __ Ldr(out, MemOperand(out, /* offset */ 0));
7408 return;
Artem Serovd4cc5b22016-11-04 11:19:09 +00007409 }
7410 case HLoadString::LoadKind::kBssEntry: {
7411 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markoea4c1262017-02-06 19:59:33 +00007412 vixl32::Register temp = (!kUseReadBarrier || kUseBakerReadBarrier)
7413 ? RegisterFrom(locations->GetTemp(0))
7414 : out;
Artem Serovd4cc5b22016-11-04 11:19:09 +00007415 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007416 codegen_->NewStringBssEntryPatch(load->GetDexFile(), load->GetStringIndex());
Artem Serovd4cc5b22016-11-04 11:19:09 +00007417 codegen_->EmitMovwMovtPlaceholder(labels, temp);
7418 GenerateGcRootFieldLoad(load, out_loc, temp, /* offset */ 0, kCompilerReadBarrierOption);
7419 LoadStringSlowPathARMVIXL* slow_path =
7420 new (GetGraph()->GetArena()) LoadStringSlowPathARMVIXL(load);
7421 codegen_->AddSlowPath(slow_path);
7422 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
7423 __ Bind(slow_path->GetExitLabel());
Roland Levillain5daa4952017-07-03 17:23:56 +01007424 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 15);
Artem Serovd4cc5b22016-11-04 11:19:09 +00007425 return;
7426 }
7427 case HLoadString::LoadKind::kJitTableAddress: {
Artem Serovc5fcb442016-12-02 19:19:58 +00007428 __ Ldr(out, codegen_->DeduplicateJitStringLiteral(load->GetDexFile(),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007429 load->GetStringIndex(),
7430 load->GetString()));
Artem Serovc5fcb442016-12-02 19:19:58 +00007431 // /* GcRoot<mirror::String> */ out = *out
7432 GenerateGcRootFieldLoad(load, out_loc, out, /* offset */ 0, kCompilerReadBarrierOption);
7433 return;
Artem Serovd4cc5b22016-11-04 11:19:09 +00007434 }
7435 default:
7436 break;
7437 }
Artem Serov02d37832016-10-25 15:25:33 +01007438
7439 // TODO: Re-add the compiler code to do string dex cache lookup again.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007440 DCHECK_EQ(load->GetLoadKind(), HLoadString::LoadKind::kRuntimeCall);
Artem Serov02d37832016-10-25 15:25:33 +01007441 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Andreas Gampe8a0128a2016-11-28 07:38:35 -08007442 __ Mov(calling_convention.GetRegisterAt(0), load->GetStringIndex().index_);
Artem Serov02d37832016-10-25 15:25:33 +01007443 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
7444 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Roland Levillain5daa4952017-07-03 17:23:56 +01007445 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 16);
Artem Serov02d37832016-10-25 15:25:33 +01007446}
7447
7448static int32_t GetExceptionTlsOffset() {
7449 return Thread::ExceptionOffset<kArmPointerSize>().Int32Value();
7450}
7451
7452void LocationsBuilderARMVIXL::VisitLoadException(HLoadException* load) {
7453 LocationSummary* locations =
7454 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
7455 locations->SetOut(Location::RequiresRegister());
7456}
7457
7458void InstructionCodeGeneratorARMVIXL::VisitLoadException(HLoadException* load) {
7459 vixl32::Register out = OutputRegister(load);
7460 GetAssembler()->LoadFromOffset(kLoadWord, out, tr, GetExceptionTlsOffset());
7461}
7462
7463
7464void LocationsBuilderARMVIXL::VisitClearException(HClearException* clear) {
7465 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
7466}
7467
7468void InstructionCodeGeneratorARMVIXL::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
7469 UseScratchRegisterScope temps(GetVIXLAssembler());
7470 vixl32::Register temp = temps.Acquire();
7471 __ Mov(temp, 0);
7472 GetAssembler()->StoreToOffset(kStoreWord, temp, tr, GetExceptionTlsOffset());
7473}
7474
7475void LocationsBuilderARMVIXL::VisitThrow(HThrow* instruction) {
7476 LocationSummary* locations =
7477 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
7478 InvokeRuntimeCallingConventionARMVIXL calling_convention;
7479 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
7480}
7481
7482void InstructionCodeGeneratorARMVIXL::VisitThrow(HThrow* instruction) {
7483 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
7484 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
7485}
7486
Artem Serov657022c2016-11-23 14:19:38 +00007487// Temp is used for read barrier.
7488static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
7489 if (kEmitCompilerReadBarrier &&
7490 (kUseBakerReadBarrier ||
7491 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
7492 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
7493 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
7494 return 1;
7495 }
7496 return 0;
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007497}
7498
Artem Serov657022c2016-11-23 14:19:38 +00007499// Interface case has 3 temps, one for holding the number of interfaces, one for the current
7500// interface pointer, one for loading the current interface.
7501// The other checks have one temp for loading the object's class.
7502static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
7503 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
7504 return 3;
7505 }
7506 return 1 + NumberOfInstanceOfTemps(type_check_kind);
7507}
Artem Serovcfbe9132016-10-14 15:58:56 +01007508
7509void LocationsBuilderARMVIXL::VisitInstanceOf(HInstanceOf* instruction) {
7510 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
7511 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
7512 bool baker_read_barrier_slow_path = false;
7513 switch (type_check_kind) {
7514 case TypeCheckKind::kExactCheck:
7515 case TypeCheckKind::kAbstractClassCheck:
7516 case TypeCheckKind::kClassHierarchyCheck:
7517 case TypeCheckKind::kArrayObjectCheck:
7518 call_kind =
7519 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
7520 baker_read_barrier_slow_path = kUseBakerReadBarrier;
7521 break;
7522 case TypeCheckKind::kArrayCheck:
7523 case TypeCheckKind::kUnresolvedCheck:
7524 case TypeCheckKind::kInterfaceCheck:
7525 call_kind = LocationSummary::kCallOnSlowPath;
7526 break;
7527 }
7528
7529 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
7530 if (baker_read_barrier_slow_path) {
7531 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
7532 }
7533 locations->SetInAt(0, Location::RequiresRegister());
7534 locations->SetInAt(1, Location::RequiresRegister());
7535 // The "out" register is used as a temporary, so it overlaps with the inputs.
7536 // Note that TypeCheckSlowPathARM uses this register too.
7537 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Artem Serov657022c2016-11-23 14:19:38 +00007538 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01007539 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
7540 codegen_->MaybeAddBakerCcEntrypointTempForFields(locations);
7541 }
Artem Serovcfbe9132016-10-14 15:58:56 +01007542}
7543
7544void InstructionCodeGeneratorARMVIXL::VisitInstanceOf(HInstanceOf* instruction) {
7545 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
7546 LocationSummary* locations = instruction->GetLocations();
7547 Location obj_loc = locations->InAt(0);
7548 vixl32::Register obj = InputRegisterAt(instruction, 0);
7549 vixl32::Register cls = InputRegisterAt(instruction, 1);
7550 Location out_loc = locations->Out();
7551 vixl32::Register out = OutputRegister(instruction);
Artem Serov657022c2016-11-23 14:19:38 +00007552 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
7553 DCHECK_LE(num_temps, 1u);
7554 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Artem Serovcfbe9132016-10-14 15:58:56 +01007555 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
7556 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
7557 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
7558 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007559 vixl32::Label done;
7560 vixl32::Label* const final_label = codegen_->GetFinalLabel(instruction, &done);
Artem Serovcfbe9132016-10-14 15:58:56 +01007561 SlowPathCodeARMVIXL* slow_path = nullptr;
7562
7563 // Return 0 if `obj` is null.
7564 // avoid null check if we know obj is not null.
7565 if (instruction->MustDoNullCheck()) {
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007566 DCHECK(!out.Is(obj));
7567 __ Mov(out, 0);
7568 __ CompareAndBranchIfZero(obj, final_label, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007569 }
7570
Artem Serovcfbe9132016-10-14 15:58:56 +01007571 switch (type_check_kind) {
7572 case TypeCheckKind::kExactCheck: {
Mathieu Chartier6beced42016-11-15 15:51:31 -08007573 // /* HeapReference<Class> */ out = obj->klass_
7574 GenerateReferenceLoadTwoRegisters(instruction,
7575 out_loc,
7576 obj_loc,
7577 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00007578 maybe_temp_loc,
7579 kCompilerReadBarrierOption);
Artem Serovcfbe9132016-10-14 15:58:56 +01007580 // Classes must be equal for the instanceof to succeed.
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007581 __ Cmp(out, cls);
7582 // We speculatively set the result to false without changing the condition
7583 // flags, which allows us to avoid some branching later.
7584 __ Mov(LeaveFlags, out, 0);
7585
7586 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
7587 // we check that the output is in a low register, so that a 16-bit MOV
7588 // encoding can be used.
7589 if (out.IsLow()) {
7590 // We use the scope because of the IT block that follows.
7591 ExactAssemblyScope guard(GetVIXLAssembler(),
7592 2 * vixl32::k16BitT32InstructionSizeInBytes,
7593 CodeBufferCheckScope::kExactSize);
7594
7595 __ it(eq);
7596 __ mov(eq, out, 1);
7597 } else {
7598 __ B(ne, final_label, /* far_target */ false);
7599 __ Mov(out, 1);
7600 }
7601
Artem Serovcfbe9132016-10-14 15:58:56 +01007602 break;
7603 }
7604
7605 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier6beced42016-11-15 15:51:31 -08007606 // /* HeapReference<Class> */ out = obj->klass_
7607 GenerateReferenceLoadTwoRegisters(instruction,
7608 out_loc,
7609 obj_loc,
7610 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00007611 maybe_temp_loc,
7612 kCompilerReadBarrierOption);
Artem Serovcfbe9132016-10-14 15:58:56 +01007613 // If the class is abstract, we eagerly fetch the super class of the
7614 // object to avoid doing a comparison we know will fail.
7615 vixl32::Label loop;
7616 __ Bind(&loop);
7617 // /* HeapReference<Class> */ out = out->super_class_
Artem Serov657022c2016-11-23 14:19:38 +00007618 GenerateReferenceLoadOneRegister(instruction,
7619 out_loc,
7620 super_offset,
7621 maybe_temp_loc,
7622 kCompilerReadBarrierOption);
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007623 // If `out` is null, we use it for the result, and jump to the final label.
Anton Kirilov6f644202017-02-27 18:29:45 +00007624 __ CompareAndBranchIfZero(out, final_label, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007625 __ Cmp(out, cls);
Artem Serov517d9f62016-12-12 15:51:15 +00007626 __ B(ne, &loop, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007627 __ Mov(out, 1);
Artem Serovcfbe9132016-10-14 15:58:56 +01007628 break;
7629 }
7630
7631 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier6beced42016-11-15 15:51:31 -08007632 // /* HeapReference<Class> */ out = obj->klass_
7633 GenerateReferenceLoadTwoRegisters(instruction,
7634 out_loc,
7635 obj_loc,
7636 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00007637 maybe_temp_loc,
7638 kCompilerReadBarrierOption);
Artem Serovcfbe9132016-10-14 15:58:56 +01007639 // Walk over the class hierarchy to find a match.
7640 vixl32::Label loop, success;
7641 __ Bind(&loop);
7642 __ Cmp(out, cls);
Artem Serov517d9f62016-12-12 15:51:15 +00007643 __ B(eq, &success, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007644 // /* HeapReference<Class> */ out = out->super_class_
Artem Serov657022c2016-11-23 14:19:38 +00007645 GenerateReferenceLoadOneRegister(instruction,
7646 out_loc,
7647 super_offset,
7648 maybe_temp_loc,
7649 kCompilerReadBarrierOption);
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007650 // This is essentially a null check, but it sets the condition flags to the
7651 // proper value for the code that follows the loop, i.e. not `eq`.
7652 __ Cmp(out, 1);
7653 __ B(hs, &loop, /* far_target */ false);
7654
7655 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
7656 // we check that the output is in a low register, so that a 16-bit MOV
7657 // encoding can be used.
7658 if (out.IsLow()) {
7659 // If `out` is null, we use it for the result, and the condition flags
7660 // have already been set to `ne`, so the IT block that comes afterwards
7661 // (and which handles the successful case) turns into a NOP (instead of
7662 // overwriting `out`).
7663 __ Bind(&success);
7664
7665 // We use the scope because of the IT block that follows.
7666 ExactAssemblyScope guard(GetVIXLAssembler(),
7667 2 * vixl32::k16BitT32InstructionSizeInBytes,
7668 CodeBufferCheckScope::kExactSize);
7669
7670 // There is only one branch to the `success` label (which is bound to this
7671 // IT block), and it has the same condition, `eq`, so in that case the MOV
7672 // is executed.
7673 __ it(eq);
7674 __ mov(eq, out, 1);
7675 } else {
7676 // If `out` is null, we use it for the result, and jump to the final label.
Anton Kirilov6f644202017-02-27 18:29:45 +00007677 __ B(final_label);
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007678 __ Bind(&success);
7679 __ Mov(out, 1);
Artem Serovcfbe9132016-10-14 15:58:56 +01007680 }
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007681
Artem Serovcfbe9132016-10-14 15:58:56 +01007682 break;
7683 }
7684
7685 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier6beced42016-11-15 15:51:31 -08007686 // /* HeapReference<Class> */ out = obj->klass_
7687 GenerateReferenceLoadTwoRegisters(instruction,
7688 out_loc,
7689 obj_loc,
7690 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00007691 maybe_temp_loc,
7692 kCompilerReadBarrierOption);
Artem Serovcfbe9132016-10-14 15:58:56 +01007693 // Do an exact check.
7694 vixl32::Label exact_check;
7695 __ Cmp(out, cls);
Artem Serov517d9f62016-12-12 15:51:15 +00007696 __ B(eq, &exact_check, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007697 // Otherwise, we need to check that the object's class is a non-primitive array.
7698 // /* HeapReference<Class> */ out = out->component_type_
Artem Serov657022c2016-11-23 14:19:38 +00007699 GenerateReferenceLoadOneRegister(instruction,
7700 out_loc,
7701 component_offset,
7702 maybe_temp_loc,
7703 kCompilerReadBarrierOption);
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007704 // If `out` is null, we use it for the result, and jump to the final label.
Anton Kirilov6f644202017-02-27 18:29:45 +00007705 __ CompareAndBranchIfZero(out, final_label, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007706 GetAssembler()->LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
7707 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007708 __ Cmp(out, 0);
7709 // We speculatively set the result to false without changing the condition
7710 // flags, which allows us to avoid some branching later.
7711 __ Mov(LeaveFlags, out, 0);
7712
7713 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
7714 // we check that the output is in a low register, so that a 16-bit MOV
7715 // encoding can be used.
7716 if (out.IsLow()) {
7717 __ Bind(&exact_check);
7718
7719 // We use the scope because of the IT block that follows.
7720 ExactAssemblyScope guard(GetVIXLAssembler(),
7721 2 * vixl32::k16BitT32InstructionSizeInBytes,
7722 CodeBufferCheckScope::kExactSize);
7723
7724 __ it(eq);
7725 __ mov(eq, out, 1);
7726 } else {
7727 __ B(ne, final_label, /* far_target */ false);
7728 __ Bind(&exact_check);
7729 __ Mov(out, 1);
7730 }
7731
Artem Serovcfbe9132016-10-14 15:58:56 +01007732 break;
7733 }
7734
7735 case TypeCheckKind::kArrayCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00007736 // No read barrier since the slow path will retry upon failure.
Mathieu Chartier6beced42016-11-15 15:51:31 -08007737 // /* HeapReference<Class> */ out = obj->klass_
7738 GenerateReferenceLoadTwoRegisters(instruction,
7739 out_loc,
7740 obj_loc,
7741 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00007742 maybe_temp_loc,
7743 kWithoutReadBarrier);
Artem Serovcfbe9132016-10-14 15:58:56 +01007744 __ Cmp(out, cls);
7745 DCHECK(locations->OnlyCallsOnSlowPath());
7746 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARMVIXL(instruction,
7747 /* is_fatal */ false);
7748 codegen_->AddSlowPath(slow_path);
7749 __ B(ne, slow_path->GetEntryLabel());
7750 __ Mov(out, 1);
Artem Serovcfbe9132016-10-14 15:58:56 +01007751 break;
7752 }
7753
7754 case TypeCheckKind::kUnresolvedCheck:
7755 case TypeCheckKind::kInterfaceCheck: {
7756 // Note that we indeed only call on slow path, but we always go
7757 // into the slow path for the unresolved and interface check
7758 // cases.
7759 //
7760 // We cannot directly call the InstanceofNonTrivial runtime
7761 // entry point without resorting to a type checking slow path
7762 // here (i.e. by calling InvokeRuntime directly), as it would
7763 // require to assign fixed registers for the inputs of this
7764 // HInstanceOf instruction (following the runtime calling
7765 // convention), which might be cluttered by the potential first
7766 // read barrier emission at the beginning of this method.
7767 //
7768 // TODO: Introduce a new runtime entry point taking the object
7769 // to test (instead of its class) as argument, and let it deal
7770 // with the read barrier issues. This will let us refactor this
7771 // case of the `switch` code as it was previously (with a direct
7772 // call to the runtime not using a type checking slow path).
7773 // This should also be beneficial for the other cases above.
7774 DCHECK(locations->OnlyCallsOnSlowPath());
7775 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARMVIXL(instruction,
7776 /* is_fatal */ false);
7777 codegen_->AddSlowPath(slow_path);
7778 __ B(slow_path->GetEntryLabel());
Artem Serovcfbe9132016-10-14 15:58:56 +01007779 break;
7780 }
7781 }
7782
Artem Serovcfbe9132016-10-14 15:58:56 +01007783 if (done.IsReferenced()) {
7784 __ Bind(&done);
7785 }
7786
7787 if (slow_path != nullptr) {
7788 __ Bind(slow_path->GetExitLabel());
7789 }
7790}
7791
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007792void LocationsBuilderARMVIXL::VisitCheckCast(HCheckCast* instruction) {
7793 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
7794 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
7795
7796 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
7797 switch (type_check_kind) {
7798 case TypeCheckKind::kExactCheck:
7799 case TypeCheckKind::kAbstractClassCheck:
7800 case TypeCheckKind::kClassHierarchyCheck:
7801 case TypeCheckKind::kArrayObjectCheck:
7802 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
7803 LocationSummary::kCallOnSlowPath :
7804 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
7805 break;
7806 case TypeCheckKind::kArrayCheck:
7807 case TypeCheckKind::kUnresolvedCheck:
7808 case TypeCheckKind::kInterfaceCheck:
7809 call_kind = LocationSummary::kCallOnSlowPath;
7810 break;
7811 }
7812
7813 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
7814 locations->SetInAt(0, Location::RequiresRegister());
7815 locations->SetInAt(1, Location::RequiresRegister());
Artem Serov657022c2016-11-23 14:19:38 +00007816 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007817}
7818
7819void InstructionCodeGeneratorARMVIXL::VisitCheckCast(HCheckCast* instruction) {
7820 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
7821 LocationSummary* locations = instruction->GetLocations();
7822 Location obj_loc = locations->InAt(0);
7823 vixl32::Register obj = InputRegisterAt(instruction, 0);
7824 vixl32::Register cls = InputRegisterAt(instruction, 1);
7825 Location temp_loc = locations->GetTemp(0);
7826 vixl32::Register temp = RegisterFrom(temp_loc);
Artem Serov657022c2016-11-23 14:19:38 +00007827 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
7828 DCHECK_LE(num_temps, 3u);
7829 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
7830 Location maybe_temp3_loc = (num_temps >= 3) ? locations->GetTemp(2) : Location::NoLocation();
7831 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
7832 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
7833 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
7834 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
7835 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
7836 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
7837 const uint32_t object_array_data_offset =
7838 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007839
Artem Serov657022c2016-11-23 14:19:38 +00007840 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
7841 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
7842 // read barriers is done for performance and code size reasons.
7843 bool is_type_check_slow_path_fatal = false;
7844 if (!kEmitCompilerReadBarrier) {
7845 is_type_check_slow_path_fatal =
7846 (type_check_kind == TypeCheckKind::kExactCheck ||
7847 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
7848 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
7849 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
7850 !instruction->CanThrowIntoCatchBlock();
7851 }
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007852 SlowPathCodeARMVIXL* type_check_slow_path =
7853 new (GetGraph()->GetArena()) TypeCheckSlowPathARMVIXL(instruction,
7854 is_type_check_slow_path_fatal);
7855 codegen_->AddSlowPath(type_check_slow_path);
7856
7857 vixl32::Label done;
Anton Kirilov6f644202017-02-27 18:29:45 +00007858 vixl32::Label* final_label = codegen_->GetFinalLabel(instruction, &done);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007859 // Avoid null check if we know obj is not null.
7860 if (instruction->MustDoNullCheck()) {
Anton Kirilov6f644202017-02-27 18:29:45 +00007861 __ CompareAndBranchIfZero(obj, final_label, /* far_target */ false);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007862 }
7863
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007864 switch (type_check_kind) {
7865 case TypeCheckKind::kExactCheck:
7866 case TypeCheckKind::kArrayCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00007867 // /* HeapReference<Class> */ temp = obj->klass_
7868 GenerateReferenceLoadTwoRegisters(instruction,
7869 temp_loc,
7870 obj_loc,
7871 class_offset,
7872 maybe_temp2_loc,
7873 kWithoutReadBarrier);
7874
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007875 __ Cmp(temp, cls);
7876 // Jump to slow path for throwing the exception or doing a
7877 // more involved array check.
7878 __ B(ne, type_check_slow_path->GetEntryLabel());
7879 break;
7880 }
7881
7882 case TypeCheckKind::kAbstractClassCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00007883 // /* HeapReference<Class> */ temp = obj->klass_
7884 GenerateReferenceLoadTwoRegisters(instruction,
7885 temp_loc,
7886 obj_loc,
7887 class_offset,
7888 maybe_temp2_loc,
7889 kWithoutReadBarrier);
7890
Artem Serovcfbe9132016-10-14 15:58:56 +01007891 // If the class is abstract, we eagerly fetch the super class of the
7892 // object to avoid doing a comparison we know will fail.
7893 vixl32::Label loop;
7894 __ Bind(&loop);
7895 // /* HeapReference<Class> */ temp = temp->super_class_
Artem Serov657022c2016-11-23 14:19:38 +00007896 GenerateReferenceLoadOneRegister(instruction,
7897 temp_loc,
7898 super_offset,
7899 maybe_temp2_loc,
7900 kWithoutReadBarrier);
Artem Serovcfbe9132016-10-14 15:58:56 +01007901
7902 // If the class reference currently in `temp` is null, jump to the slow path to throw the
7903 // exception.
xueliang.zhongf51bc622016-11-04 09:23:32 +00007904 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
Artem Serovcfbe9132016-10-14 15:58:56 +01007905
7906 // Otherwise, compare the classes.
7907 __ Cmp(temp, cls);
Artem Serov517d9f62016-12-12 15:51:15 +00007908 __ B(ne, &loop, /* far_target */ false);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007909 break;
7910 }
7911
7912 case TypeCheckKind::kClassHierarchyCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00007913 // /* HeapReference<Class> */ temp = obj->klass_
7914 GenerateReferenceLoadTwoRegisters(instruction,
7915 temp_loc,
7916 obj_loc,
7917 class_offset,
7918 maybe_temp2_loc,
7919 kWithoutReadBarrier);
7920
Artem Serovcfbe9132016-10-14 15:58:56 +01007921 // Walk over the class hierarchy to find a match.
7922 vixl32::Label loop;
7923 __ Bind(&loop);
7924 __ Cmp(temp, cls);
Anton Kirilov6f644202017-02-27 18:29:45 +00007925 __ B(eq, final_label, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007926
7927 // /* HeapReference<Class> */ temp = temp->super_class_
Artem Serov657022c2016-11-23 14:19:38 +00007928 GenerateReferenceLoadOneRegister(instruction,
7929 temp_loc,
7930 super_offset,
7931 maybe_temp2_loc,
7932 kWithoutReadBarrier);
Artem Serovcfbe9132016-10-14 15:58:56 +01007933
7934 // If the class reference currently in `temp` is null, jump to the slow path to throw the
7935 // exception.
xueliang.zhongf51bc622016-11-04 09:23:32 +00007936 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
Artem Serovcfbe9132016-10-14 15:58:56 +01007937 // Otherwise, jump to the beginning of the loop.
7938 __ B(&loop);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007939 break;
7940 }
7941
Artem Serovcfbe9132016-10-14 15:58:56 +01007942 case TypeCheckKind::kArrayObjectCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00007943 // /* HeapReference<Class> */ temp = obj->klass_
7944 GenerateReferenceLoadTwoRegisters(instruction,
7945 temp_loc,
7946 obj_loc,
7947 class_offset,
7948 maybe_temp2_loc,
7949 kWithoutReadBarrier);
7950
Artem Serovcfbe9132016-10-14 15:58:56 +01007951 // Do an exact check.
7952 __ Cmp(temp, cls);
Anton Kirilov6f644202017-02-27 18:29:45 +00007953 __ B(eq, final_label, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007954
7955 // Otherwise, we need to check that the object's class is a non-primitive array.
7956 // /* HeapReference<Class> */ temp = temp->component_type_
Artem Serov657022c2016-11-23 14:19:38 +00007957 GenerateReferenceLoadOneRegister(instruction,
7958 temp_loc,
7959 component_offset,
7960 maybe_temp2_loc,
7961 kWithoutReadBarrier);
Artem Serovcfbe9132016-10-14 15:58:56 +01007962 // If the component type is null, jump to the slow path to throw the exception.
xueliang.zhongf51bc622016-11-04 09:23:32 +00007963 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
Artem Serovcfbe9132016-10-14 15:58:56 +01007964 // Otherwise,the object is indeed an array, jump to label `check_non_primitive_component_type`
7965 // to further check that this component type is not a primitive type.
7966 GetAssembler()->LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
7967 static_assert(Primitive::kPrimNot == 0, "Expected 0 for art::Primitive::kPrimNot");
xueliang.zhongf51bc622016-11-04 09:23:32 +00007968 __ CompareAndBranchIfNonZero(temp, type_check_slow_path->GetEntryLabel());
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007969 break;
7970 }
7971
7972 case TypeCheckKind::kUnresolvedCheck:
Artem Serov657022c2016-11-23 14:19:38 +00007973 // We always go into the type check slow path for the unresolved check case.
Artem Serovcfbe9132016-10-14 15:58:56 +01007974 // We cannot directly call the CheckCast runtime entry point
7975 // without resorting to a type checking slow path here (i.e. by
7976 // calling InvokeRuntime directly), as it would require to
7977 // assign fixed registers for the inputs of this HInstanceOf
7978 // instruction (following the runtime calling convention), which
7979 // might be cluttered by the potential first read barrier
7980 // emission at the beginning of this method.
Artem Serov657022c2016-11-23 14:19:38 +00007981
Artem Serovcfbe9132016-10-14 15:58:56 +01007982 __ B(type_check_slow_path->GetEntryLabel());
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007983 break;
Artem Serov657022c2016-11-23 14:19:38 +00007984
7985 case TypeCheckKind::kInterfaceCheck: {
7986 // Avoid read barriers to improve performance of the fast path. We can not get false
7987 // positives by doing this.
7988 // /* HeapReference<Class> */ temp = obj->klass_
7989 GenerateReferenceLoadTwoRegisters(instruction,
7990 temp_loc,
7991 obj_loc,
7992 class_offset,
7993 maybe_temp2_loc,
7994 kWithoutReadBarrier);
7995
7996 // /* HeapReference<Class> */ temp = temp->iftable_
7997 GenerateReferenceLoadTwoRegisters(instruction,
7998 temp_loc,
7999 temp_loc,
8000 iftable_offset,
8001 maybe_temp2_loc,
8002 kWithoutReadBarrier);
8003 // Iftable is never null.
8004 __ Ldr(RegisterFrom(maybe_temp2_loc), MemOperand(temp, array_length_offset));
8005 // Loop through the iftable and check if any class matches.
8006 vixl32::Label start_loop;
8007 __ Bind(&start_loop);
8008 __ CompareAndBranchIfZero(RegisterFrom(maybe_temp2_loc),
8009 type_check_slow_path->GetEntryLabel());
8010 __ Ldr(RegisterFrom(maybe_temp3_loc), MemOperand(temp, object_array_data_offset));
8011 GetAssembler()->MaybeUnpoisonHeapReference(RegisterFrom(maybe_temp3_loc));
8012 // Go to next interface.
8013 __ Add(temp, temp, Operand::From(2 * kHeapReferenceSize));
8014 __ Sub(RegisterFrom(maybe_temp2_loc), RegisterFrom(maybe_temp2_loc), 2);
8015 // Compare the classes and continue the loop if they do not match.
8016 __ Cmp(cls, RegisterFrom(maybe_temp3_loc));
Artem Serov517d9f62016-12-12 15:51:15 +00008017 __ B(ne, &start_loop, /* far_target */ false);
Artem Serov657022c2016-11-23 14:19:38 +00008018 break;
8019 }
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008020 }
Anton Kirilov6f644202017-02-27 18:29:45 +00008021 if (done.IsReferenced()) {
8022 __ Bind(&done);
8023 }
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008024
8025 __ Bind(type_check_slow_path->GetExitLabel());
8026}
8027
Artem Serov551b28f2016-10-18 19:11:30 +01008028void LocationsBuilderARMVIXL::VisitMonitorOperation(HMonitorOperation* instruction) {
8029 LocationSummary* locations =
8030 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
8031 InvokeRuntimeCallingConventionARMVIXL calling_convention;
8032 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
8033}
8034
8035void InstructionCodeGeneratorARMVIXL::VisitMonitorOperation(HMonitorOperation* instruction) {
8036 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
8037 instruction,
8038 instruction->GetDexPc());
8039 if (instruction->IsEnter()) {
8040 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
8041 } else {
8042 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
8043 }
Roland Levillain5daa4952017-07-03 17:23:56 +01008044 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 17);
Artem Serov551b28f2016-10-18 19:11:30 +01008045}
8046
Artem Serov02109dd2016-09-23 17:17:54 +01008047void LocationsBuilderARMVIXL::VisitAnd(HAnd* instruction) {
8048 HandleBitwiseOperation(instruction, AND);
8049}
8050
8051void LocationsBuilderARMVIXL::VisitOr(HOr* instruction) {
8052 HandleBitwiseOperation(instruction, ORR);
8053}
8054
8055void LocationsBuilderARMVIXL::VisitXor(HXor* instruction) {
8056 HandleBitwiseOperation(instruction, EOR);
8057}
8058
8059void LocationsBuilderARMVIXL::HandleBitwiseOperation(HBinaryOperation* instruction, Opcode opcode) {
8060 LocationSummary* locations =
8061 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
8062 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
8063 || instruction->GetResultType() == Primitive::kPrimLong);
8064 // Note: GVN reorders commutative operations to have the constant on the right hand side.
8065 locations->SetInAt(0, Location::RequiresRegister());
8066 locations->SetInAt(1, ArmEncodableConstantOrRegister(instruction->InputAt(1), opcode));
8067 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8068}
8069
8070void InstructionCodeGeneratorARMVIXL::VisitAnd(HAnd* instruction) {
8071 HandleBitwiseOperation(instruction);
8072}
8073
8074void InstructionCodeGeneratorARMVIXL::VisitOr(HOr* instruction) {
8075 HandleBitwiseOperation(instruction);
8076}
8077
8078void InstructionCodeGeneratorARMVIXL::VisitXor(HXor* instruction) {
8079 HandleBitwiseOperation(instruction);
8080}
8081
Artem Serov2bbc9532016-10-21 11:51:50 +01008082void LocationsBuilderARMVIXL::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
8083 LocationSummary* locations =
8084 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
8085 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
8086 || instruction->GetResultType() == Primitive::kPrimLong);
8087
8088 locations->SetInAt(0, Location::RequiresRegister());
8089 locations->SetInAt(1, Location::RequiresRegister());
8090 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8091}
8092
8093void InstructionCodeGeneratorARMVIXL::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
8094 LocationSummary* locations = instruction->GetLocations();
8095 Location first = locations->InAt(0);
8096 Location second = locations->InAt(1);
8097 Location out = locations->Out();
8098
8099 if (instruction->GetResultType() == Primitive::kPrimInt) {
8100 vixl32::Register first_reg = RegisterFrom(first);
8101 vixl32::Register second_reg = RegisterFrom(second);
8102 vixl32::Register out_reg = RegisterFrom(out);
8103
8104 switch (instruction->GetOpKind()) {
8105 case HInstruction::kAnd:
8106 __ Bic(out_reg, first_reg, second_reg);
8107 break;
8108 case HInstruction::kOr:
8109 __ Orn(out_reg, first_reg, second_reg);
8110 break;
8111 // There is no EON on arm.
8112 case HInstruction::kXor:
8113 default:
8114 LOG(FATAL) << "Unexpected instruction " << instruction->DebugName();
8115 UNREACHABLE();
8116 }
8117 return;
8118
8119 } else {
8120 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
8121 vixl32::Register first_low = LowRegisterFrom(first);
8122 vixl32::Register first_high = HighRegisterFrom(first);
8123 vixl32::Register second_low = LowRegisterFrom(second);
8124 vixl32::Register second_high = HighRegisterFrom(second);
8125 vixl32::Register out_low = LowRegisterFrom(out);
8126 vixl32::Register out_high = HighRegisterFrom(out);
8127
8128 switch (instruction->GetOpKind()) {
8129 case HInstruction::kAnd:
8130 __ Bic(out_low, first_low, second_low);
8131 __ Bic(out_high, first_high, second_high);
8132 break;
8133 case HInstruction::kOr:
8134 __ Orn(out_low, first_low, second_low);
8135 __ Orn(out_high, first_high, second_high);
8136 break;
8137 // There is no EON on arm.
8138 case HInstruction::kXor:
8139 default:
8140 LOG(FATAL) << "Unexpected instruction " << instruction->DebugName();
8141 UNREACHABLE();
8142 }
8143 }
8144}
8145
Anton Kirilov74234da2017-01-13 14:42:47 +00008146void LocationsBuilderARMVIXL::VisitDataProcWithShifterOp(
8147 HDataProcWithShifterOp* instruction) {
8148 DCHECK(instruction->GetType() == Primitive::kPrimInt ||
8149 instruction->GetType() == Primitive::kPrimLong);
8150 LocationSummary* locations =
8151 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
8152 const bool overlap = instruction->GetType() == Primitive::kPrimLong &&
8153 HDataProcWithShifterOp::IsExtensionOp(instruction->GetOpKind());
8154
8155 locations->SetInAt(0, Location::RequiresRegister());
8156 locations->SetInAt(1, Location::RequiresRegister());
8157 locations->SetOut(Location::RequiresRegister(),
8158 overlap ? Location::kOutputOverlap : Location::kNoOutputOverlap);
8159}
8160
8161void InstructionCodeGeneratorARMVIXL::VisitDataProcWithShifterOp(
8162 HDataProcWithShifterOp* instruction) {
8163 const LocationSummary* const locations = instruction->GetLocations();
8164 const HInstruction::InstructionKind kind = instruction->GetInstrKind();
8165 const HDataProcWithShifterOp::OpKind op_kind = instruction->GetOpKind();
8166
8167 if (instruction->GetType() == Primitive::kPrimInt) {
Anton Kirilov420ee302017-02-21 18:10:26 +00008168 const vixl32::Register first = InputRegisterAt(instruction, 0);
8169 const vixl32::Register output = OutputRegister(instruction);
Anton Kirilov74234da2017-01-13 14:42:47 +00008170 const vixl32::Register second = instruction->InputAt(1)->GetType() == Primitive::kPrimLong
8171 ? LowRegisterFrom(locations->InAt(1))
8172 : InputRegisterAt(instruction, 1);
8173
Anton Kirilov420ee302017-02-21 18:10:26 +00008174 if (HDataProcWithShifterOp::IsExtensionOp(op_kind)) {
8175 DCHECK_EQ(kind, HInstruction::kAdd);
8176
8177 switch (op_kind) {
8178 case HDataProcWithShifterOp::kUXTB:
8179 __ Uxtab(output, first, second);
8180 break;
8181 case HDataProcWithShifterOp::kUXTH:
8182 __ Uxtah(output, first, second);
8183 break;
8184 case HDataProcWithShifterOp::kSXTB:
8185 __ Sxtab(output, first, second);
8186 break;
8187 case HDataProcWithShifterOp::kSXTH:
8188 __ Sxtah(output, first, second);
8189 break;
8190 default:
8191 LOG(FATAL) << "Unexpected operation kind: " << op_kind;
8192 UNREACHABLE();
8193 }
8194 } else {
8195 GenerateDataProcInstruction(kind,
8196 output,
8197 first,
8198 Operand(second,
8199 ShiftFromOpKind(op_kind),
8200 instruction->GetShiftAmount()),
8201 codegen_);
8202 }
Anton Kirilov74234da2017-01-13 14:42:47 +00008203 } else {
8204 DCHECK_EQ(instruction->GetType(), Primitive::kPrimLong);
8205
8206 if (HDataProcWithShifterOp::IsExtensionOp(op_kind)) {
8207 const vixl32::Register second = InputRegisterAt(instruction, 1);
8208
8209 DCHECK(!LowRegisterFrom(locations->Out()).Is(second));
8210 GenerateDataProc(kind,
8211 locations->Out(),
8212 locations->InAt(0),
8213 second,
8214 Operand(second, ShiftType::ASR, 31),
8215 codegen_);
8216 } else {
8217 GenerateLongDataProc(instruction, codegen_);
8218 }
8219 }
8220}
8221
Artem Serov02109dd2016-09-23 17:17:54 +01008222// TODO(VIXL): Remove optimizations in the helper when they are implemented in vixl.
8223void InstructionCodeGeneratorARMVIXL::GenerateAndConst(vixl32::Register out,
8224 vixl32::Register first,
8225 uint32_t value) {
8226 // Optimize special cases for individual halfs of `and-long` (`and` is simplified earlier).
8227 if (value == 0xffffffffu) {
8228 if (!out.Is(first)) {
8229 __ Mov(out, first);
8230 }
8231 return;
8232 }
8233 if (value == 0u) {
8234 __ Mov(out, 0);
8235 return;
8236 }
8237 if (GetAssembler()->ShifterOperandCanHold(AND, value)) {
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00008238 __ And(out, first, value);
8239 } else if (GetAssembler()->ShifterOperandCanHold(BIC, ~value)) {
8240 __ Bic(out, first, ~value);
Artem Serov02109dd2016-09-23 17:17:54 +01008241 } else {
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00008242 DCHECK(IsPowerOfTwo(value + 1));
8243 __ Ubfx(out, first, 0, WhichPowerOf2(value + 1));
Artem Serov02109dd2016-09-23 17:17:54 +01008244 }
8245}
8246
8247// TODO(VIXL): Remove optimizations in the helper when they are implemented in vixl.
8248void InstructionCodeGeneratorARMVIXL::GenerateOrrConst(vixl32::Register out,
8249 vixl32::Register first,
8250 uint32_t value) {
8251 // Optimize special cases for individual halfs of `or-long` (`or` is simplified earlier).
8252 if (value == 0u) {
8253 if (!out.Is(first)) {
8254 __ Mov(out, first);
8255 }
8256 return;
8257 }
8258 if (value == 0xffffffffu) {
8259 __ Mvn(out, 0);
8260 return;
8261 }
8262 if (GetAssembler()->ShifterOperandCanHold(ORR, value)) {
8263 __ Orr(out, first, value);
8264 } else {
8265 DCHECK(GetAssembler()->ShifterOperandCanHold(ORN, ~value));
8266 __ Orn(out, first, ~value);
8267 }
8268}
8269
8270// TODO(VIXL): Remove optimizations in the helper when they are implemented in vixl.
8271void InstructionCodeGeneratorARMVIXL::GenerateEorConst(vixl32::Register out,
8272 vixl32::Register first,
8273 uint32_t value) {
8274 // Optimize special case for individual halfs of `xor-long` (`xor` is simplified earlier).
8275 if (value == 0u) {
8276 if (!out.Is(first)) {
8277 __ Mov(out, first);
8278 }
8279 return;
8280 }
8281 __ Eor(out, first, value);
8282}
8283
Anton Kirilovdda43962016-11-21 19:55:20 +00008284void InstructionCodeGeneratorARMVIXL::GenerateAddLongConst(Location out,
8285 Location first,
8286 uint64_t value) {
8287 vixl32::Register out_low = LowRegisterFrom(out);
8288 vixl32::Register out_high = HighRegisterFrom(out);
8289 vixl32::Register first_low = LowRegisterFrom(first);
8290 vixl32::Register first_high = HighRegisterFrom(first);
8291 uint32_t value_low = Low32Bits(value);
8292 uint32_t value_high = High32Bits(value);
8293 if (value_low == 0u) {
8294 if (!out_low.Is(first_low)) {
8295 __ Mov(out_low, first_low);
8296 }
8297 __ Add(out_high, first_high, value_high);
8298 return;
8299 }
8300 __ Adds(out_low, first_low, value_low);
Scott Wakelingbffdc702016-12-07 17:46:03 +00008301 if (GetAssembler()->ShifterOperandCanHold(ADC, value_high, kCcDontCare)) {
Anton Kirilovdda43962016-11-21 19:55:20 +00008302 __ Adc(out_high, first_high, value_high);
Scott Wakelingbffdc702016-12-07 17:46:03 +00008303 } else if (GetAssembler()->ShifterOperandCanHold(SBC, ~value_high, kCcDontCare)) {
Anton Kirilovdda43962016-11-21 19:55:20 +00008304 __ Sbc(out_high, first_high, ~value_high);
8305 } else {
8306 LOG(FATAL) << "Unexpected constant " << value_high;
8307 UNREACHABLE();
8308 }
8309}
8310
Artem Serov02109dd2016-09-23 17:17:54 +01008311void InstructionCodeGeneratorARMVIXL::HandleBitwiseOperation(HBinaryOperation* instruction) {
8312 LocationSummary* locations = instruction->GetLocations();
8313 Location first = locations->InAt(0);
8314 Location second = locations->InAt(1);
8315 Location out = locations->Out();
8316
8317 if (second.IsConstant()) {
8318 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
8319 uint32_t value_low = Low32Bits(value);
8320 if (instruction->GetResultType() == Primitive::kPrimInt) {
8321 vixl32::Register first_reg = InputRegisterAt(instruction, 0);
8322 vixl32::Register out_reg = OutputRegister(instruction);
8323 if (instruction->IsAnd()) {
8324 GenerateAndConst(out_reg, first_reg, value_low);
8325 } else if (instruction->IsOr()) {
8326 GenerateOrrConst(out_reg, first_reg, value_low);
8327 } else {
8328 DCHECK(instruction->IsXor());
8329 GenerateEorConst(out_reg, first_reg, value_low);
8330 }
8331 } else {
8332 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
8333 uint32_t value_high = High32Bits(value);
8334 vixl32::Register first_low = LowRegisterFrom(first);
8335 vixl32::Register first_high = HighRegisterFrom(first);
8336 vixl32::Register out_low = LowRegisterFrom(out);
8337 vixl32::Register out_high = HighRegisterFrom(out);
8338 if (instruction->IsAnd()) {
8339 GenerateAndConst(out_low, first_low, value_low);
8340 GenerateAndConst(out_high, first_high, value_high);
8341 } else if (instruction->IsOr()) {
8342 GenerateOrrConst(out_low, first_low, value_low);
8343 GenerateOrrConst(out_high, first_high, value_high);
8344 } else {
8345 DCHECK(instruction->IsXor());
8346 GenerateEorConst(out_low, first_low, value_low);
8347 GenerateEorConst(out_high, first_high, value_high);
8348 }
8349 }
8350 return;
8351 }
8352
8353 if (instruction->GetResultType() == Primitive::kPrimInt) {
8354 vixl32::Register first_reg = InputRegisterAt(instruction, 0);
8355 vixl32::Register second_reg = InputRegisterAt(instruction, 1);
8356 vixl32::Register out_reg = OutputRegister(instruction);
8357 if (instruction->IsAnd()) {
8358 __ And(out_reg, first_reg, second_reg);
8359 } else if (instruction->IsOr()) {
8360 __ Orr(out_reg, first_reg, second_reg);
8361 } else {
8362 DCHECK(instruction->IsXor());
8363 __ Eor(out_reg, first_reg, second_reg);
8364 }
8365 } else {
8366 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
8367 vixl32::Register first_low = LowRegisterFrom(first);
8368 vixl32::Register first_high = HighRegisterFrom(first);
8369 vixl32::Register second_low = LowRegisterFrom(second);
8370 vixl32::Register second_high = HighRegisterFrom(second);
8371 vixl32::Register out_low = LowRegisterFrom(out);
8372 vixl32::Register out_high = HighRegisterFrom(out);
8373 if (instruction->IsAnd()) {
8374 __ And(out_low, first_low, second_low);
8375 __ And(out_high, first_high, second_high);
8376 } else if (instruction->IsOr()) {
8377 __ Orr(out_low, first_low, second_low);
8378 __ Orr(out_high, first_high, second_high);
8379 } else {
8380 DCHECK(instruction->IsXor());
8381 __ Eor(out_low, first_low, second_low);
8382 __ Eor(out_high, first_high, second_high);
8383 }
8384 }
8385}
8386
Artem Serovcfbe9132016-10-14 15:58:56 +01008387void InstructionCodeGeneratorARMVIXL::GenerateReferenceLoadOneRegister(
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008388 HInstruction* instruction,
Artem Serovcfbe9132016-10-14 15:58:56 +01008389 Location out,
8390 uint32_t offset,
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008391 Location maybe_temp,
8392 ReadBarrierOption read_barrier_option) {
Artem Serovcfbe9132016-10-14 15:58:56 +01008393 vixl32::Register out_reg = RegisterFrom(out);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008394 if (read_barrier_option == kWithReadBarrier) {
8395 CHECK(kEmitCompilerReadBarrier);
8396 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
8397 if (kUseBakerReadBarrier) {
8398 // Load with fast path based Baker's read barrier.
8399 // /* HeapReference<Object> */ out = *(out + offset)
8400 codegen_->GenerateFieldLoadWithBakerReadBarrier(
8401 instruction, out, out_reg, offset, maybe_temp, /* needs_null_check */ false);
8402 } else {
8403 // Load with slow path based read barrier.
8404 // Save the value of `out` into `maybe_temp` before overwriting it
8405 // in the following move operation, as we will need it for the
8406 // read barrier below.
8407 __ Mov(RegisterFrom(maybe_temp), out_reg);
8408 // /* HeapReference<Object> */ out = *(out + offset)
8409 GetAssembler()->LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
8410 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
8411 }
Artem Serovcfbe9132016-10-14 15:58:56 +01008412 } else {
8413 // Plain load with no read barrier.
8414 // /* HeapReference<Object> */ out = *(out + offset)
8415 GetAssembler()->LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
8416 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
8417 }
8418}
8419
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008420void InstructionCodeGeneratorARMVIXL::GenerateReferenceLoadTwoRegisters(
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008421 HInstruction* instruction,
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008422 Location out,
8423 Location obj,
8424 uint32_t offset,
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008425 Location maybe_temp,
8426 ReadBarrierOption read_barrier_option) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008427 vixl32::Register out_reg = RegisterFrom(out);
8428 vixl32::Register obj_reg = RegisterFrom(obj);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008429 if (read_barrier_option == kWithReadBarrier) {
8430 CHECK(kEmitCompilerReadBarrier);
8431 if (kUseBakerReadBarrier) {
8432 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
8433 // Load with fast path based Baker's read barrier.
8434 // /* HeapReference<Object> */ out = *(obj + offset)
8435 codegen_->GenerateFieldLoadWithBakerReadBarrier(
8436 instruction, out, obj_reg, offset, maybe_temp, /* needs_null_check */ false);
8437 } else {
8438 // Load with slow path based read barrier.
8439 // /* HeapReference<Object> */ out = *(obj + offset)
8440 GetAssembler()->LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
8441 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
8442 }
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008443 } else {
8444 // Plain load with no read barrier.
8445 // /* HeapReference<Object> */ out = *(obj + offset)
8446 GetAssembler()->LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
8447 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
8448 }
8449}
8450
Scott Wakelinga7812ae2016-10-17 10:03:36 +01008451void InstructionCodeGeneratorARMVIXL::GenerateGcRootFieldLoad(
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008452 HInstruction* instruction,
Scott Wakelinga7812ae2016-10-17 10:03:36 +01008453 Location root,
8454 vixl32::Register obj,
8455 uint32_t offset,
Artem Serovd4cc5b22016-11-04 11:19:09 +00008456 ReadBarrierOption read_barrier_option) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01008457 vixl32::Register root_reg = RegisterFrom(root);
Artem Serovd4cc5b22016-11-04 11:19:09 +00008458 if (read_barrier_option == kWithReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008459 DCHECK(kEmitCompilerReadBarrier);
8460 if (kUseBakerReadBarrier) {
8461 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
Roland Levillainba650a42017-03-06 13:52:32 +00008462 // Baker's read barrier are used.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008463 if (kBakerReadBarrierLinkTimeThunksEnableForGcRoots &&
8464 !Runtime::Current()->UseJitCompilation()) {
Roland Levillain6d729a72017-06-30 18:34:01 +01008465 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in
8466 // the Marking Register) to decide whether we need to enter
8467 // the slow path to mark the GC root.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008468 //
8469 // We use link-time generated thunks for the slow path. That thunk
8470 // checks the reference and jumps to the entrypoint if needed.
8471 //
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008472 // lr = &return_address;
8473 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
Roland Levillain6d729a72017-06-30 18:34:01 +01008474 // if (mr) { // Thread::Current()->GetIsGcMarking()
8475 // goto gc_root_thunk<root_reg>(lr)
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008476 // }
8477 // return_address:
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008478
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008479 UseScratchRegisterScope temps(GetVIXLAssembler());
8480 ExcludeIPAndBakerCcEntrypointRegister(&temps, instruction);
Vladimir Marko88abba22017-05-03 17:09:25 +01008481 bool narrow = CanEmitNarrowLdr(root_reg, obj, offset);
8482 uint32_t custom_data = linker::Thumb2RelativePatcher::EncodeBakerReadBarrierGcRootData(
8483 root_reg.GetCode(), narrow);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008484 vixl32::Label* bne_label = codegen_->NewBakerReadBarrierPatch(custom_data);
Roland Levillainba650a42017-03-06 13:52:32 +00008485
Roland Levillain6d729a72017-06-30 18:34:01 +01008486 vixl::EmissionCheckScope guard(GetVIXLAssembler(), 4 * vixl32::kMaxInstructionSizeInBytes);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008487 vixl32::Label return_address;
8488 EmitAdrCode adr(GetVIXLAssembler(), lr, &return_address);
Roland Levillain6d729a72017-06-30 18:34:01 +01008489 __ cmp(mr, Operand(0));
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008490 // Currently the offset is always within range. If that changes,
8491 // we shall have to split the load the same way as for fields.
8492 DCHECK_LT(offset, kReferenceLoadMinFarOffset);
Vladimir Marko88abba22017-05-03 17:09:25 +01008493 ptrdiff_t old_offset = GetVIXLAssembler()->GetBuffer()->GetCursorOffset();
8494 __ ldr(EncodingSize(narrow ? Narrow : Wide), root_reg, MemOperand(obj, offset));
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008495 EmitPlaceholderBne(codegen_, bne_label);
8496 __ Bind(&return_address);
Vladimir Marko88abba22017-05-03 17:09:25 +01008497 DCHECK_EQ(old_offset - GetVIXLAssembler()->GetBuffer()->GetCursorOffset(),
8498 narrow ? BAKER_MARK_INTROSPECTION_GC_ROOT_LDR_NARROW_OFFSET
8499 : BAKER_MARK_INTROSPECTION_GC_ROOT_LDR_WIDE_OFFSET);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008500 } else {
Roland Levillain6d729a72017-06-30 18:34:01 +01008501 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in
8502 // the Marking Register) to decide whether we need to enter
8503 // the slow path to mark the GC root.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008504 //
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008505 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
Roland Levillain6d729a72017-06-30 18:34:01 +01008506 // if (mr) { // Thread::Current()->GetIsGcMarking()
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008507 // // Slow path.
Roland Levillain6d729a72017-06-30 18:34:01 +01008508 // entrypoint = Thread::Current()->pReadBarrierMarkReg ## root.reg()
8509 // root = entrypoint(root); // root = ReadBarrier::Mark(root); // Entry point call.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008510 // }
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008511
Roland Levillain6d729a72017-06-30 18:34:01 +01008512 // Slow path marking the GC root `root`. The entrypoint will
8513 // be loaded by the slow path code.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008514 SlowPathCodeARMVIXL* slow_path =
Roland Levillain6d729a72017-06-30 18:34:01 +01008515 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARMVIXL(instruction, root);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008516 codegen_->AddSlowPath(slow_path);
8517
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008518 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
8519 GetAssembler()->LoadFromOffset(kLoadWord, root_reg, obj, offset);
8520 static_assert(
8521 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
8522 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
8523 "have different sizes.");
8524 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
8525 "art::mirror::CompressedReference<mirror::Object> and int32_t "
8526 "have different sizes.");
8527
Roland Levillain6d729a72017-06-30 18:34:01 +01008528 __ CompareAndBranchIfNonZero(mr, slow_path->GetEntryLabel());
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008529 __ Bind(slow_path->GetExitLabel());
8530 }
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008531 } else {
8532 // GC root loaded through a slow path for read barriers other
8533 // than Baker's.
8534 // /* GcRoot<mirror::Object>* */ root = obj + offset
8535 __ Add(root_reg, obj, offset);
8536 // /* mirror::Object* */ root = root->Read()
8537 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
8538 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01008539 } else {
8540 // Plain GC root load with no read barrier.
8541 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
8542 GetAssembler()->LoadFromOffset(kLoadWord, root_reg, obj, offset);
8543 // Note that GC roots are not affected by heap poisoning, thus we
8544 // do not have to unpoison `root_reg` here.
8545 }
Roland Levillain5daa4952017-07-03 17:23:56 +01008546 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 18);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01008547}
8548
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008549void CodeGeneratorARMVIXL::MaybeAddBakerCcEntrypointTempForFields(LocationSummary* locations) {
8550 DCHECK(kEmitCompilerReadBarrier);
8551 DCHECK(kUseBakerReadBarrier);
8552 if (kBakerReadBarrierLinkTimeThunksEnableForFields) {
8553 if (!Runtime::Current()->UseJitCompilation()) {
8554 locations->AddTemp(Location::RegisterLocation(kBakerCcEntrypointRegister.GetCode()));
8555 }
8556 }
8557}
8558
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008559void CodeGeneratorARMVIXL::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
8560 Location ref,
8561 vixl32::Register obj,
8562 uint32_t offset,
8563 Location temp,
8564 bool needs_null_check) {
8565 DCHECK(kEmitCompilerReadBarrier);
8566 DCHECK(kUseBakerReadBarrier);
8567
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008568 if (kBakerReadBarrierLinkTimeThunksEnableForFields &&
8569 !Runtime::Current()->UseJitCompilation()) {
Roland Levillain6d729a72017-06-30 18:34:01 +01008570 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in the
8571 // Marking Register) to decide whether we need to enter the slow
8572 // path to mark the reference. Then, in the slow path, check the
8573 // gray bit in the lock word of the reference's holder (`obj`) to
8574 // decide whether to mark `ref` or not.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008575 //
8576 // We use link-time generated thunks for the slow path. That thunk checks
8577 // the holder and jumps to the entrypoint if needed. If the holder is not
8578 // gray, it creates a fake dependency and returns to the LDR instruction.
8579 //
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008580 // lr = &gray_return_address;
Roland Levillain6d729a72017-06-30 18:34:01 +01008581 // if (mr) { // Thread::Current()->GetIsGcMarking()
8582 // goto field_thunk<holder_reg, base_reg>(lr)
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008583 // }
8584 // not_gray_return_address:
8585 // // Original reference load. If the offset is too large to fit
8586 // // into LDR, we use an adjusted base register here.
Vladimir Marko88abba22017-05-03 17:09:25 +01008587 // HeapReference<mirror::Object> reference = *(obj+offset);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008588 // gray_return_address:
8589
8590 DCHECK_ALIGNED(offset, sizeof(mirror::HeapReference<mirror::Object>));
Vladimir Marko88abba22017-05-03 17:09:25 +01008591 vixl32::Register ref_reg = RegisterFrom(ref, Primitive::kPrimNot);
8592 bool narrow = CanEmitNarrowLdr(ref_reg, obj, offset);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008593 vixl32::Register base = obj;
8594 if (offset >= kReferenceLoadMinFarOffset) {
8595 base = RegisterFrom(temp);
8596 DCHECK(!base.Is(kBakerCcEntrypointRegister));
8597 static_assert(IsPowerOfTwo(kReferenceLoadMinFarOffset), "Expecting a power of 2.");
8598 __ Add(base, obj, Operand(offset & ~(kReferenceLoadMinFarOffset - 1u)));
8599 offset &= (kReferenceLoadMinFarOffset - 1u);
Vladimir Marko88abba22017-05-03 17:09:25 +01008600 // Use narrow LDR only for small offsets. Generating narrow encoding LDR for the large
8601 // offsets with `(offset & (kReferenceLoadMinFarOffset - 1u)) < 32u` would most likely
8602 // increase the overall code size when taking the generated thunks into account.
8603 DCHECK(!narrow);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008604 }
8605 UseScratchRegisterScope temps(GetVIXLAssembler());
8606 ExcludeIPAndBakerCcEntrypointRegister(&temps, instruction);
8607 uint32_t custom_data = linker::Thumb2RelativePatcher::EncodeBakerReadBarrierFieldData(
Vladimir Marko88abba22017-05-03 17:09:25 +01008608 base.GetCode(), obj.GetCode(), narrow);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008609 vixl32::Label* bne_label = NewBakerReadBarrierPatch(custom_data);
8610
Roland Levillain5daa4952017-07-03 17:23:56 +01008611 {
8612 vixl::EmissionCheckScope guard(
8613 GetVIXLAssembler(),
8614 (kPoisonHeapReferences ? 5u : 4u) * vixl32::kMaxInstructionSizeInBytes);
8615 vixl32::Label return_address;
8616 EmitAdrCode adr(GetVIXLAssembler(), lr, &return_address);
8617 __ cmp(mr, Operand(0));
8618 EmitPlaceholderBne(this, bne_label);
8619 ptrdiff_t old_offset = GetVIXLAssembler()->GetBuffer()->GetCursorOffset();
8620 __ ldr(EncodingSize(narrow ? Narrow : Wide), ref_reg, MemOperand(base, offset));
8621 if (needs_null_check) {
8622 MaybeRecordImplicitNullCheck(instruction);
Vladimir Marko88abba22017-05-03 17:09:25 +01008623 }
Roland Levillain5daa4952017-07-03 17:23:56 +01008624 // Note: We need a specific width for the unpoisoning NEG.
8625 if (kPoisonHeapReferences) {
8626 if (narrow) {
8627 // The only 16-bit encoding is T1 which sets flags outside IT block (i.e. RSBS, not RSB).
8628 __ rsbs(EncodingSize(Narrow), ref_reg, ref_reg, Operand(0));
8629 } else {
8630 __ rsb(EncodingSize(Wide), ref_reg, ref_reg, Operand(0));
8631 }
8632 }
8633 __ Bind(&return_address);
8634 DCHECK_EQ(old_offset - GetVIXLAssembler()->GetBuffer()->GetCursorOffset(),
8635 narrow ? BAKER_MARK_INTROSPECTION_FIELD_LDR_NARROW_OFFSET
8636 : BAKER_MARK_INTROSPECTION_FIELD_LDR_WIDE_OFFSET);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008637 }
Roland Levillain5daa4952017-07-03 17:23:56 +01008638 MaybeGenerateMarkingRegisterCheck(/* code */ 19, /* temp_loc */ LocationFrom(ip));
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008639 return;
8640 }
8641
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008642 // /* HeapReference<Object> */ ref = *(obj + offset)
8643 Location no_index = Location::NoLocation();
8644 ScaleFactor no_scale_factor = TIMES_1;
8645 GenerateReferenceLoadWithBakerReadBarrier(
8646 instruction, ref, obj, offset, no_index, no_scale_factor, temp, needs_null_check);
Roland Levillain6070e882016-11-03 17:51:58 +00008647}
8648
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008649void CodeGeneratorARMVIXL::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
8650 Location ref,
8651 vixl32::Register obj,
8652 uint32_t data_offset,
8653 Location index,
8654 Location temp,
8655 bool needs_null_check) {
8656 DCHECK(kEmitCompilerReadBarrier);
8657 DCHECK(kUseBakerReadBarrier);
8658
8659 static_assert(
8660 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
8661 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008662 ScaleFactor scale_factor = TIMES_4;
8663
8664 if (kBakerReadBarrierLinkTimeThunksEnableForArrays &&
8665 !Runtime::Current()->UseJitCompilation()) {
Roland Levillain6d729a72017-06-30 18:34:01 +01008666 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in the
8667 // Marking Register) to decide whether we need to enter the slow
8668 // path to mark the reference. Then, in the slow path, check the
8669 // gray bit in the lock word of the reference's holder (`obj`) to
8670 // decide whether to mark `ref` or not.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008671 //
8672 // We use link-time generated thunks for the slow path. That thunk checks
8673 // the holder and jumps to the entrypoint if needed. If the holder is not
8674 // gray, it creates a fake dependency and returns to the LDR instruction.
8675 //
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008676 // lr = &gray_return_address;
Roland Levillain6d729a72017-06-30 18:34:01 +01008677 // if (mr) { // Thread::Current()->GetIsGcMarking()
8678 // goto array_thunk<base_reg>(lr)
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008679 // }
8680 // not_gray_return_address:
8681 // // Original reference load. If the offset is too large to fit
8682 // // into LDR, we use an adjusted base register here.
Vladimir Marko88abba22017-05-03 17:09:25 +01008683 // HeapReference<mirror::Object> reference = data[index];
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008684 // gray_return_address:
8685
8686 DCHECK(index.IsValid());
8687 vixl32::Register index_reg = RegisterFrom(index, Primitive::kPrimInt);
8688 vixl32::Register ref_reg = RegisterFrom(ref, Primitive::kPrimNot);
8689 vixl32::Register data_reg = RegisterFrom(temp, Primitive::kPrimInt); // Raw pointer.
8690 DCHECK(!data_reg.Is(kBakerCcEntrypointRegister));
8691
8692 UseScratchRegisterScope temps(GetVIXLAssembler());
8693 ExcludeIPAndBakerCcEntrypointRegister(&temps, instruction);
8694 uint32_t custom_data =
8695 linker::Thumb2RelativePatcher::EncodeBakerReadBarrierArrayData(data_reg.GetCode());
8696 vixl32::Label* bne_label = NewBakerReadBarrierPatch(custom_data);
8697
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008698 __ Add(data_reg, obj, Operand(data_offset));
Roland Levillain5daa4952017-07-03 17:23:56 +01008699 {
8700 vixl::EmissionCheckScope guard(
8701 GetVIXLAssembler(),
8702 (kPoisonHeapReferences ? 5u : 4u) * vixl32::kMaxInstructionSizeInBytes);
8703 vixl32::Label return_address;
8704 EmitAdrCode adr(GetVIXLAssembler(), lr, &return_address);
8705 __ cmp(mr, Operand(0));
8706 EmitPlaceholderBne(this, bne_label);
8707 ptrdiff_t old_offset = GetVIXLAssembler()->GetBuffer()->GetCursorOffset();
8708 __ ldr(ref_reg, MemOperand(data_reg, index_reg, vixl32::LSL, scale_factor));
8709 DCHECK(!needs_null_check); // The thunk cannot handle the null check.
8710 // Note: We need a Wide NEG for the unpoisoning.
8711 if (kPoisonHeapReferences) {
8712 __ rsb(EncodingSize(Wide), ref_reg, ref_reg, Operand(0));
8713 }
8714 __ Bind(&return_address);
8715 DCHECK_EQ(old_offset - GetVIXLAssembler()->GetBuffer()->GetCursorOffset(),
8716 BAKER_MARK_INTROSPECTION_ARRAY_LDR_OFFSET);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008717 }
Roland Levillain5daa4952017-07-03 17:23:56 +01008718 MaybeGenerateMarkingRegisterCheck(/* code */ 20, /* temp_loc */ LocationFrom(ip));
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008719 return;
8720 }
8721
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008722 // /* HeapReference<Object> */ ref =
8723 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008724 GenerateReferenceLoadWithBakerReadBarrier(
8725 instruction, ref, obj, data_offset, index, scale_factor, temp, needs_null_check);
Roland Levillain6070e882016-11-03 17:51:58 +00008726}
8727
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008728void CodeGeneratorARMVIXL::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
8729 Location ref,
8730 vixl32::Register obj,
8731 uint32_t offset,
8732 Location index,
8733 ScaleFactor scale_factor,
8734 Location temp,
Roland Levillainff487002017-03-07 16:50:01 +00008735 bool needs_null_check) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008736 DCHECK(kEmitCompilerReadBarrier);
8737 DCHECK(kUseBakerReadBarrier);
8738
Roland Levillain6d729a72017-06-30 18:34:01 +01008739 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in the
8740 // Marking Register) to decide whether we need to enter the slow
8741 // path to mark the reference. Then, in the slow path, check the
8742 // gray bit in the lock word of the reference's holder (`obj`) to
8743 // decide whether to mark `ref` or not.
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008744 //
Roland Levillain6d729a72017-06-30 18:34:01 +01008745 // if (mr) { // Thread::Current()->GetIsGcMarking()
Roland Levillainff487002017-03-07 16:50:01 +00008746 // // Slow path.
8747 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
8748 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
8749 // HeapReference<mirror::Object> ref = *src; // Original reference load.
8750 // bool is_gray = (rb_state == ReadBarrier::GrayState());
8751 // if (is_gray) {
Roland Levillain6d729a72017-06-30 18:34:01 +01008752 // entrypoint = Thread::Current()->pReadBarrierMarkReg ## root.reg()
8753 // ref = entrypoint(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
Roland Levillainff487002017-03-07 16:50:01 +00008754 // }
8755 // } else {
8756 // HeapReference<mirror::Object> ref = *src; // Original reference load.
8757 // }
8758
8759 vixl32::Register temp_reg = RegisterFrom(temp);
8760
8761 // Slow path marking the object `ref` when the GC is marking. The
Roland Levillain6d729a72017-06-30 18:34:01 +01008762 // entrypoint will be loaded by the slow path code.
Roland Levillainff487002017-03-07 16:50:01 +00008763 SlowPathCodeARMVIXL* slow_path =
8764 new (GetGraph()->GetArena()) LoadReferenceWithBakerReadBarrierSlowPathARMVIXL(
Roland Levillain6d729a72017-06-30 18:34:01 +01008765 instruction, ref, obj, offset, index, scale_factor, needs_null_check, temp_reg);
Roland Levillainff487002017-03-07 16:50:01 +00008766 AddSlowPath(slow_path);
8767
Roland Levillain6d729a72017-06-30 18:34:01 +01008768 __ CompareAndBranchIfNonZero(mr, slow_path->GetEntryLabel());
Roland Levillainff487002017-03-07 16:50:01 +00008769 // Fast path: the GC is not marking: just load the reference.
8770 GenerateRawReferenceLoad(instruction, ref, obj, offset, index, scale_factor, needs_null_check);
8771 __ Bind(slow_path->GetExitLabel());
Roland Levillain5daa4952017-07-03 17:23:56 +01008772 MaybeGenerateMarkingRegisterCheck(/* code */ 21);
Roland Levillainff487002017-03-07 16:50:01 +00008773}
8774
8775void CodeGeneratorARMVIXL::UpdateReferenceFieldWithBakerReadBarrier(HInstruction* instruction,
8776 Location ref,
8777 vixl32::Register obj,
8778 Location field_offset,
8779 Location temp,
8780 bool needs_null_check,
8781 vixl32::Register temp2) {
8782 DCHECK(kEmitCompilerReadBarrier);
8783 DCHECK(kUseBakerReadBarrier);
8784
Roland Levillain6d729a72017-06-30 18:34:01 +01008785 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in the
8786 // Marking Register) to decide whether we need to enter the slow
8787 // path to update the reference field within `obj`. Then, in the
8788 // slow path, check the gray bit in the lock word of the reference's
8789 // holder (`obj`) to decide whether to mark `ref` and update the
8790 // field or not.
Roland Levillainff487002017-03-07 16:50:01 +00008791 //
Roland Levillain6d729a72017-06-30 18:34:01 +01008792 // if (mr) { // Thread::Current()->GetIsGcMarking()
Roland Levillainba650a42017-03-06 13:52:32 +00008793 // // Slow path.
Roland Levillain54f869e2017-03-06 13:54:11 +00008794 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
8795 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
Roland Levillainff487002017-03-07 16:50:01 +00008796 // HeapReference<mirror::Object> ref = *(obj + field_offset); // Reference load.
Roland Levillain54f869e2017-03-06 13:54:11 +00008797 // bool is_gray = (rb_state == ReadBarrier::GrayState());
8798 // if (is_gray) {
Roland Levillainff487002017-03-07 16:50:01 +00008799 // old_ref = ref;
Roland Levillain6d729a72017-06-30 18:34:01 +01008800 // entrypoint = Thread::Current()->pReadBarrierMarkReg ## root.reg()
8801 // ref = entrypoint(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
Roland Levillainff487002017-03-07 16:50:01 +00008802 // compareAndSwapObject(obj, field_offset, old_ref, ref);
Roland Levillain54f869e2017-03-06 13:54:11 +00008803 // }
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008804 // }
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008805
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008806 vixl32::Register temp_reg = RegisterFrom(temp);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008807
Roland Levillainff487002017-03-07 16:50:01 +00008808 // Slow path updating the object reference at address `obj + field_offset`
Roland Levillain6d729a72017-06-30 18:34:01 +01008809 // when the GC is marking. The entrypoint will be loaded by the slow path code.
Roland Levillainff487002017-03-07 16:50:01 +00008810 SlowPathCodeARMVIXL* slow_path =
8811 new (GetGraph()->GetArena()) LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARMVIXL(
8812 instruction,
8813 ref,
8814 obj,
8815 /* offset */ 0u,
8816 /* index */ field_offset,
8817 /* scale_factor */ ScaleFactor::TIMES_1,
8818 needs_null_check,
8819 temp_reg,
Roland Levillain6d729a72017-06-30 18:34:01 +01008820 temp2);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008821 AddSlowPath(slow_path);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008822
Roland Levillain6d729a72017-06-30 18:34:01 +01008823 __ CompareAndBranchIfNonZero(mr, slow_path->GetEntryLabel());
Roland Levillainff487002017-03-07 16:50:01 +00008824 // Fast path: the GC is not marking: nothing to do (the field is
8825 // up-to-date, and we don't need to load the reference).
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008826 __ Bind(slow_path->GetExitLabel());
Roland Levillain5daa4952017-07-03 17:23:56 +01008827 MaybeGenerateMarkingRegisterCheck(/* code */ 22);
Roland Levillain844e6532016-11-03 16:09:47 +00008828}
Scott Wakelingfe885462016-09-22 10:24:38 +01008829
Roland Levillainba650a42017-03-06 13:52:32 +00008830void CodeGeneratorARMVIXL::GenerateRawReferenceLoad(HInstruction* instruction,
8831 Location ref,
8832 vixl::aarch32::Register obj,
8833 uint32_t offset,
8834 Location index,
8835 ScaleFactor scale_factor,
8836 bool needs_null_check) {
8837 Primitive::Type type = Primitive::kPrimNot;
8838 vixl32::Register ref_reg = RegisterFrom(ref, type);
8839
8840 // If needed, vixl::EmissionCheckScope guards are used to ensure
8841 // that no pools are emitted between the load (macro) instruction
8842 // and MaybeRecordImplicitNullCheck.
8843
Scott Wakelingfe885462016-09-22 10:24:38 +01008844 if (index.IsValid()) {
8845 // Load types involving an "index": ArrayGet,
8846 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
8847 // intrinsics.
Roland Levillainba650a42017-03-06 13:52:32 +00008848 // /* HeapReference<mirror::Object> */ ref = *(obj + offset + (index << scale_factor))
Scott Wakelingfe885462016-09-22 10:24:38 +01008849 if (index.IsConstant()) {
8850 size_t computed_offset =
8851 (Int32ConstantFrom(index) << scale_factor) + offset;
Roland Levillainba650a42017-03-06 13:52:32 +00008852 vixl::EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Scott Wakelingfe885462016-09-22 10:24:38 +01008853 GetAssembler()->LoadFromOffset(kLoadWord, ref_reg, obj, computed_offset);
Roland Levillainba650a42017-03-06 13:52:32 +00008854 if (needs_null_check) {
8855 MaybeRecordImplicitNullCheck(instruction);
8856 }
Scott Wakelingfe885462016-09-22 10:24:38 +01008857 } else {
8858 // Handle the special case of the
8859 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
8860 // intrinsics, which use a register pair as index ("long
8861 // offset"), of which only the low part contains data.
8862 vixl32::Register index_reg = index.IsRegisterPair()
8863 ? LowRegisterFrom(index)
8864 : RegisterFrom(index);
8865 UseScratchRegisterScope temps(GetVIXLAssembler());
Roland Levillainba650a42017-03-06 13:52:32 +00008866 vixl32::Register temp = temps.Acquire();
8867 __ Add(temp, obj, Operand(index_reg, ShiftType::LSL, scale_factor));
8868 {
8869 vixl::EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
8870 GetAssembler()->LoadFromOffset(kLoadWord, ref_reg, temp, offset);
8871 if (needs_null_check) {
8872 MaybeRecordImplicitNullCheck(instruction);
8873 }
8874 }
Scott Wakelingfe885462016-09-22 10:24:38 +01008875 }
8876 } else {
Roland Levillainba650a42017-03-06 13:52:32 +00008877 // /* HeapReference<mirror::Object> */ ref = *(obj + offset)
8878 vixl::EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Scott Wakelingfe885462016-09-22 10:24:38 +01008879 GetAssembler()->LoadFromOffset(kLoadWord, ref_reg, obj, offset);
Roland Levillainba650a42017-03-06 13:52:32 +00008880 if (needs_null_check) {
8881 MaybeRecordImplicitNullCheck(instruction);
8882 }
Scott Wakelingfe885462016-09-22 10:24:38 +01008883 }
8884
Roland Levillain844e6532016-11-03 16:09:47 +00008885 // Object* ref = ref_addr->AsMirrorPtr()
8886 GetAssembler()->MaybeUnpoisonHeapReference(ref_reg);
Roland Levillain844e6532016-11-03 16:09:47 +00008887}
8888
Roland Levillain5daa4952017-07-03 17:23:56 +01008889void CodeGeneratorARMVIXL::MaybeGenerateMarkingRegisterCheck(int code, Location temp_loc) {
8890 // The following condition is a compile-time one, so it does not have a run-time cost.
8891 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier && kIsDebugBuild) {
8892 // The following condition is a run-time one; it is executed after the
8893 // previous compile-time test, to avoid penalizing non-debug builds.
8894 if (GetCompilerOptions().EmitRunTimeChecksInDebugMode()) {
8895 UseScratchRegisterScope temps(GetVIXLAssembler());
8896 vixl32::Register temp = temp_loc.IsValid() ? RegisterFrom(temp_loc) : temps.Acquire();
8897 GetAssembler()->GenerateMarkingRegisterCheck(temp,
8898 kMarkingRegisterCheckBreakCodeBaseCode + code);
8899 }
8900 }
8901}
8902
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008903void CodeGeneratorARMVIXL::GenerateReadBarrierSlow(HInstruction* instruction,
8904 Location out,
8905 Location ref,
8906 Location obj,
8907 uint32_t offset,
8908 Location index) {
8909 DCHECK(kEmitCompilerReadBarrier);
8910
8911 // Insert a slow path based read barrier *after* the reference load.
8912 //
8913 // If heap poisoning is enabled, the unpoisoning of the loaded
8914 // reference will be carried out by the runtime within the slow
8915 // path.
8916 //
8917 // Note that `ref` currently does not get unpoisoned (when heap
8918 // poisoning is enabled), which is alright as the `ref` argument is
8919 // not used by the artReadBarrierSlow entry point.
8920 //
8921 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
8922 SlowPathCodeARMVIXL* slow_path = new (GetGraph()->GetArena())
8923 ReadBarrierForHeapReferenceSlowPathARMVIXL(instruction, out, ref, obj, offset, index);
8924 AddSlowPath(slow_path);
8925
8926 __ B(slow_path->GetEntryLabel());
8927 __ Bind(slow_path->GetExitLabel());
8928}
8929
8930void CodeGeneratorARMVIXL::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
Artem Serov02d37832016-10-25 15:25:33 +01008931 Location out,
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008932 Location ref,
8933 Location obj,
8934 uint32_t offset,
8935 Location index) {
Artem Serov02d37832016-10-25 15:25:33 +01008936 if (kEmitCompilerReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008937 // Baker's read barriers shall be handled by the fast path
Roland Levillain9983e302017-07-14 14:34:22 +01008938 // (CodeGeneratorARMVIXL::GenerateReferenceLoadWithBakerReadBarrier).
Artem Serov02d37832016-10-25 15:25:33 +01008939 DCHECK(!kUseBakerReadBarrier);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008940 // If heap poisoning is enabled, unpoisoning will be taken care of
8941 // by the runtime within the slow path.
8942 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Artem Serov02d37832016-10-25 15:25:33 +01008943 } else if (kPoisonHeapReferences) {
8944 GetAssembler()->UnpoisonHeapReference(RegisterFrom(out));
8945 }
8946}
8947
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008948void CodeGeneratorARMVIXL::GenerateReadBarrierForRootSlow(HInstruction* instruction,
8949 Location out,
8950 Location root) {
8951 DCHECK(kEmitCompilerReadBarrier);
8952
8953 // Insert a slow path based read barrier *after* the GC root load.
8954 //
8955 // Note that GC roots are not affected by heap poisoning, so we do
8956 // not need to do anything special for this here.
8957 SlowPathCodeARMVIXL* slow_path =
8958 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathARMVIXL(instruction, out, root);
8959 AddSlowPath(slow_path);
8960
8961 __ B(slow_path->GetEntryLabel());
8962 __ Bind(slow_path->GetExitLabel());
8963}
8964
Artem Serov02d37832016-10-25 15:25:33 +01008965// Check if the desired_dispatch_info is supported. If it is, return it,
8966// otherwise return a fall-back info that should be used instead.
8967HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARMVIXL::GetSupportedInvokeStaticOrDirectDispatch(
Artem Serovd4cc5b22016-11-04 11:19:09 +00008968 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +00008969 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Nicolas Geoffraye807ff72017-01-23 09:03:12 +00008970 return desired_dispatch_info;
Artem Serov02d37832016-10-25 15:25:33 +01008971}
8972
Scott Wakelingfe885462016-09-22 10:24:38 +01008973vixl32::Register CodeGeneratorARMVIXL::GetInvokeStaticOrDirectExtraParameter(
8974 HInvokeStaticOrDirect* invoke, vixl32::Register temp) {
8975 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
8976 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
8977 if (!invoke->GetLocations()->Intrinsified()) {
8978 return RegisterFrom(location);
8979 }
8980 // For intrinsics we allow any location, so it may be on the stack.
8981 if (!location.IsRegister()) {
8982 GetAssembler()->LoadFromOffset(kLoadWord, temp, sp, location.GetStackIndex());
8983 return temp;
8984 }
8985 // For register locations, check if the register was saved. If so, get it from the stack.
8986 // Note: There is a chance that the register was saved but not overwritten, so we could
8987 // save one load. However, since this is just an intrinsic slow path we prefer this
8988 // simple and more robust approach rather that trying to determine if that's the case.
8989 SlowPathCode* slow_path = GetCurrentSlowPath();
Scott Wakelingd5cd4972017-02-03 11:38:35 +00008990 if (slow_path != nullptr && slow_path->IsCoreRegisterSaved(RegisterFrom(location).GetCode())) {
Scott Wakelingfe885462016-09-22 10:24:38 +01008991 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(RegisterFrom(location).GetCode());
8992 GetAssembler()->LoadFromOffset(kLoadWord, temp, sp, stack_offset);
8993 return temp;
8994 }
8995 return RegisterFrom(location);
8996}
8997
Vladimir Markod254f5c2017-06-02 15:18:36 +00008998void CodeGeneratorARMVIXL::GenerateStaticOrDirectCall(
Vladimir Markoe7197bf2017-06-02 17:00:23 +01008999 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00009000 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
Scott Wakelingfe885462016-09-22 10:24:38 +01009001 switch (invoke->GetMethodLoadKind()) {
9002 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
9003 uint32_t offset =
9004 GetThreadOffset<kArmPointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
9005 // temp = thread->string_init_entrypoint
Artem Serovd4cc5b22016-11-04 11:19:09 +00009006 GetAssembler()->LoadFromOffset(kLoadWord, RegisterFrom(temp), tr, offset);
9007 break;
9008 }
9009 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
9010 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
9011 break;
Vladimir Marko65979462017-05-19 17:25:12 +01009012 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative: {
9013 DCHECK(GetCompilerOptions().IsBootImage());
9014 PcRelativePatchInfo* labels = NewPcRelativeMethodPatch(invoke->GetTargetMethod());
9015 vixl32::Register temp_reg = RegisterFrom(temp);
9016 EmitMovwMovtPlaceholder(labels, temp_reg);
9017 break;
9018 }
Artem Serovd4cc5b22016-11-04 11:19:09 +00009019 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
9020 __ Mov(RegisterFrom(temp), Operand::From(invoke->GetMethodAddress()));
9021 break;
Vladimir Marko0eb882b2017-05-15 13:39:18 +01009022 case HInvokeStaticOrDirect::MethodLoadKind::kBssEntry: {
9023 PcRelativePatchInfo* labels = NewMethodBssEntryPatch(
9024 MethodReference(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex()));
9025 vixl32::Register temp_reg = RegisterFrom(temp);
9026 EmitMovwMovtPlaceholder(labels, temp_reg);
9027 GetAssembler()->LoadFromOffset(kLoadWord, temp_reg, temp_reg, /* offset*/ 0);
Scott Wakelingfe885462016-09-22 10:24:38 +01009028 break;
9029 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01009030 case HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall: {
9031 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
9032 return; // No code pointer retrieval; the runtime performs the call directly.
Scott Wakelingfe885462016-09-22 10:24:38 +01009033 }
Scott Wakelingfe885462016-09-22 10:24:38 +01009034 }
9035
Artem Serovd4cc5b22016-11-04 11:19:09 +00009036 switch (invoke->GetCodePtrLocation()) {
9037 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Vladimir Markoe7197bf2017-06-02 17:00:23 +01009038 {
9039 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
9040 ExactAssemblyScope aas(GetVIXLAssembler(),
9041 vixl32::k32BitT32InstructionSizeInBytes,
9042 CodeBufferCheckScope::kMaximumSize);
9043 __ bl(GetFrameEntryLabel());
9044 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
9045 }
Artem Serovd4cc5b22016-11-04 11:19:09 +00009046 break;
Artem Serovd4cc5b22016-11-04 11:19:09 +00009047 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
9048 // LR = callee_method->entry_point_from_quick_compiled_code_
9049 GetAssembler()->LoadFromOffset(
9050 kLoadWord,
9051 lr,
9052 RegisterFrom(callee_method),
9053 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize).Int32Value());
Alexandre Rames374ddf32016-11-04 10:40:49 +00009054 {
Vladimir Markoe7197bf2017-06-02 17:00:23 +01009055 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
Alexandre Rames374ddf32016-11-04 10:40:49 +00009056 // 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 +00009057 ExactAssemblyScope aas(GetVIXLAssembler(),
9058 vixl32::k16BitT32InstructionSizeInBytes,
9059 CodeBufferCheckScope::kExactSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00009060 // LR()
9061 __ blx(lr);
Vladimir Markoe7197bf2017-06-02 17:00:23 +01009062 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Alexandre Rames374ddf32016-11-04 10:40:49 +00009063 }
Artem Serovd4cc5b22016-11-04 11:19:09 +00009064 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01009065 }
9066
Scott Wakelingfe885462016-09-22 10:24:38 +01009067 DCHECK(!IsLeafMethod());
9068}
9069
Vladimir Markoe7197bf2017-06-02 17:00:23 +01009070void CodeGeneratorARMVIXL::GenerateVirtualCall(
9071 HInvokeVirtual* invoke, Location temp_location, SlowPathCode* slow_path) {
Scott Wakelingfe885462016-09-22 10:24:38 +01009072 vixl32::Register temp = RegisterFrom(temp_location);
9073 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
9074 invoke->GetVTableIndex(), kArmPointerSize).Uint32Value();
9075
9076 // Use the calling convention instead of the location of the receiver, as
9077 // intrinsics may have put the receiver in a different register. In the intrinsics
9078 // slow path, the arguments have been moved to the right place, so here we are
9079 // guaranteed that the receiver is the first register of the calling convention.
9080 InvokeDexCallingConventionARMVIXL calling_convention;
9081 vixl32::Register receiver = calling_convention.GetRegisterAt(0);
9082 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Alexandre Rames374ddf32016-11-04 10:40:49 +00009083 {
9084 // Make sure the pc is recorded immediately after the `ldr` instruction.
Artem Serov0fb37192016-12-06 18:13:40 +00009085 ExactAssemblyScope aas(GetVIXLAssembler(),
9086 vixl32::kMaxInstructionSizeInBytes,
9087 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00009088 // /* HeapReference<Class> */ temp = receiver->klass_
9089 __ ldr(temp, MemOperand(receiver, class_offset));
9090 MaybeRecordImplicitNullCheck(invoke);
9091 }
Scott Wakelingfe885462016-09-22 10:24:38 +01009092 // Instead of simply (possibly) unpoisoning `temp` here, we should
9093 // emit a read barrier for the previous class reference load.
9094 // However this is not required in practice, as this is an
9095 // intermediate/temporary reference and because the current
9096 // concurrent copying collector keeps the from-space memory
9097 // intact/accessible until the end of the marking phase (the
9098 // concurrent copying collector may not in the future).
9099 GetAssembler()->MaybeUnpoisonHeapReference(temp);
9100
9101 // temp = temp->GetMethodAt(method_offset);
9102 uint32_t entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(
9103 kArmPointerSize).Int32Value();
9104 GetAssembler()->LoadFromOffset(kLoadWord, temp, temp, method_offset);
9105 // LR = temp->GetEntryPoint();
9106 GetAssembler()->LoadFromOffset(kLoadWord, lr, temp, entry_point);
Vladimir Markoe7197bf2017-06-02 17:00:23 +01009107 {
9108 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
9109 // blx in T32 has only 16bit encoding that's why a stricter check for the scope is used.
9110 ExactAssemblyScope aas(GetVIXLAssembler(),
9111 vixl32::k16BitT32InstructionSizeInBytes,
9112 CodeBufferCheckScope::kExactSize);
9113 // LR();
9114 __ blx(lr);
9115 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
9116 }
Scott Wakelingfe885462016-09-22 10:24:38 +01009117}
9118
Vladimir Marko65979462017-05-19 17:25:12 +01009119CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewPcRelativeMethodPatch(
9120 MethodReference target_method) {
9121 return NewPcRelativePatch(*target_method.dex_file,
9122 target_method.dex_method_index,
9123 &pc_relative_method_patches_);
Artem Serovd4cc5b22016-11-04 11:19:09 +00009124}
9125
Vladimir Marko0eb882b2017-05-15 13:39:18 +01009126CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewMethodBssEntryPatch(
9127 MethodReference target_method) {
9128 return NewPcRelativePatch(*target_method.dex_file,
9129 target_method.dex_method_index,
9130 &method_bss_entry_patches_);
9131}
9132
Artem Serovd4cc5b22016-11-04 11:19:09 +00009133CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewPcRelativeTypePatch(
9134 const DexFile& dex_file, dex::TypeIndex type_index) {
9135 return NewPcRelativePatch(dex_file, type_index.index_, &pc_relative_type_patches_);
9136}
9137
Vladimir Marko1998cd02017-01-13 13:02:58 +00009138CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewTypeBssEntryPatch(
9139 const DexFile& dex_file, dex::TypeIndex type_index) {
9140 return NewPcRelativePatch(dex_file, type_index.index_, &type_bss_entry_patches_);
9141}
9142
Vladimir Marko65979462017-05-19 17:25:12 +01009143CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewPcRelativeStringPatch(
9144 const DexFile& dex_file, dex::StringIndex string_index) {
9145 return NewPcRelativePatch(dex_file, string_index.index_, &pc_relative_string_patches_);
9146}
9147
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01009148CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewStringBssEntryPatch(
9149 const DexFile& dex_file, dex::StringIndex string_index) {
9150 return NewPcRelativePatch(dex_file, string_index.index_, &string_bss_entry_patches_);
9151}
9152
Artem Serovd4cc5b22016-11-04 11:19:09 +00009153CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewPcRelativePatch(
9154 const DexFile& dex_file, uint32_t offset_or_index, ArenaDeque<PcRelativePatchInfo>* patches) {
9155 patches->emplace_back(dex_file, offset_or_index);
9156 return &patches->back();
9157}
9158
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01009159vixl::aarch32::Label* CodeGeneratorARMVIXL::NewBakerReadBarrierPatch(uint32_t custom_data) {
9160 baker_read_barrier_patches_.emplace_back(custom_data);
9161 return &baker_read_barrier_patches_.back().label;
9162}
9163
Artem Serovc5fcb442016-12-02 19:19:58 +00009164VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateBootImageAddressLiteral(uint32_t address) {
Richard Uhlerc52f3032017-03-02 13:45:45 +00009165 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), &uint32_literals_);
Artem Serovc5fcb442016-12-02 19:19:58 +00009166}
9167
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00009168VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateJitStringLiteral(
9169 const DexFile& dex_file,
9170 dex::StringIndex string_index,
9171 Handle<mirror::String> handle) {
9172 jit_string_roots_.Overwrite(StringReference(&dex_file, string_index),
9173 reinterpret_cast64<uint64_t>(handle.GetReference()));
Artem Serovc5fcb442016-12-02 19:19:58 +00009174 return jit_string_patches_.GetOrCreate(
9175 StringReference(&dex_file, string_index),
9176 [this]() {
9177 return GetAssembler()->CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u);
9178 });
9179}
9180
9181VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateJitClassLiteral(const DexFile& dex_file,
9182 dex::TypeIndex type_index,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00009183 Handle<mirror::Class> handle) {
9184 jit_class_roots_.Overwrite(TypeReference(&dex_file, type_index),
9185 reinterpret_cast64<uint64_t>(handle.GetReference()));
Artem Serovc5fcb442016-12-02 19:19:58 +00009186 return jit_class_patches_.GetOrCreate(
9187 TypeReference(&dex_file, type_index),
9188 [this]() {
9189 return GetAssembler()->CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u);
9190 });
9191}
9192
Artem Serovd4cc5b22016-11-04 11:19:09 +00009193template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
9194inline void CodeGeneratorARMVIXL::EmitPcRelativeLinkerPatches(
9195 const ArenaDeque<PcRelativePatchInfo>& infos,
9196 ArenaVector<LinkerPatch>* linker_patches) {
9197 for (const PcRelativePatchInfo& info : infos) {
9198 const DexFile& dex_file = info.target_dex_file;
9199 size_t offset_or_index = info.offset_or_index;
9200 DCHECK(info.add_pc_label.IsBound());
9201 uint32_t add_pc_offset = dchecked_integral_cast<uint32_t>(info.add_pc_label.GetLocation());
9202 // Add MOVW patch.
9203 DCHECK(info.movw_label.IsBound());
9204 uint32_t movw_offset = dchecked_integral_cast<uint32_t>(info.movw_label.GetLocation());
9205 linker_patches->push_back(Factory(movw_offset, &dex_file, add_pc_offset, offset_or_index));
9206 // Add MOVT patch.
9207 DCHECK(info.movt_label.IsBound());
9208 uint32_t movt_offset = dchecked_integral_cast<uint32_t>(info.movt_label.GetLocation());
9209 linker_patches->push_back(Factory(movt_offset, &dex_file, add_pc_offset, offset_or_index));
9210 }
9211}
9212
9213void CodeGeneratorARMVIXL::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
9214 DCHECK(linker_patches->empty());
9215 size_t size =
Vladimir Marko65979462017-05-19 17:25:12 +01009216 /* MOVW+MOVT for each entry */ 2u * pc_relative_method_patches_.size() +
Vladimir Marko0eb882b2017-05-15 13:39:18 +01009217 /* MOVW+MOVT for each entry */ 2u * method_bss_entry_patches_.size() +
Artem Serovc5fcb442016-12-02 19:19:58 +00009218 /* MOVW+MOVT for each entry */ 2u * pc_relative_type_patches_.size() +
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01009219 /* MOVW+MOVT for each entry */ 2u * type_bss_entry_patches_.size() +
Vladimir Marko65979462017-05-19 17:25:12 +01009220 /* MOVW+MOVT for each entry */ 2u * pc_relative_string_patches_.size() +
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01009221 /* MOVW+MOVT for each entry */ 2u * string_bss_entry_patches_.size() +
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01009222 baker_read_barrier_patches_.size();
Artem Serovd4cc5b22016-11-04 11:19:09 +00009223 linker_patches->reserve(size);
Vladimir Marko65979462017-05-19 17:25:12 +01009224 if (GetCompilerOptions().IsBootImage()) {
9225 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeMethodPatch>(pc_relative_method_patches_,
Artem Serovd4cc5b22016-11-04 11:19:09 +00009226 linker_patches);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00009227 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(pc_relative_type_patches_,
9228 linker_patches);
Artem Serovd4cc5b22016-11-04 11:19:09 +00009229 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(pc_relative_string_patches_,
9230 linker_patches);
Vladimir Marko65979462017-05-19 17:25:12 +01009231 } else {
9232 DCHECK(pc_relative_method_patches_.empty());
Vladimir Marko94ec2db2017-09-06 17:21:03 +01009233 EmitPcRelativeLinkerPatches<LinkerPatch::TypeClassTablePatch>(pc_relative_type_patches_,
9234 linker_patches);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01009235 EmitPcRelativeLinkerPatches<LinkerPatch::StringInternTablePatch>(pc_relative_string_patches_,
9236 linker_patches);
Artem Serovd4cc5b22016-11-04 11:19:09 +00009237 }
Vladimir Marko0eb882b2017-05-15 13:39:18 +01009238 EmitPcRelativeLinkerPatches<LinkerPatch::MethodBssEntryPatch>(method_bss_entry_patches_,
9239 linker_patches);
Vladimir Marko1998cd02017-01-13 13:02:58 +00009240 EmitPcRelativeLinkerPatches<LinkerPatch::TypeBssEntryPatch>(type_bss_entry_patches_,
9241 linker_patches);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01009242 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(string_bss_entry_patches_,
9243 linker_patches);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01009244 for (const BakerReadBarrierPatchInfo& info : baker_read_barrier_patches_) {
9245 linker_patches->push_back(LinkerPatch::BakerReadBarrierBranchPatch(info.label.GetLocation(),
9246 info.custom_data));
9247 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00009248 DCHECK_EQ(size, linker_patches->size());
Artem Serovc5fcb442016-12-02 19:19:58 +00009249}
9250
9251VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateUint32Literal(
9252 uint32_t value,
9253 Uint32ToLiteralMap* map) {
9254 return map->GetOrCreate(
9255 value,
9256 [this, value]() {
9257 return GetAssembler()->CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ value);
9258 });
9259}
9260
Artem Serov2bbc9532016-10-21 11:51:50 +01009261void LocationsBuilderARMVIXL::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
9262 LocationSummary* locations =
9263 new (GetGraph()->GetArena()) LocationSummary(instr, LocationSummary::kNoCall);
9264 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
9265 Location::RequiresRegister());
9266 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
9267 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
9268 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
9269}
9270
9271void InstructionCodeGeneratorARMVIXL::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
9272 vixl32::Register res = OutputRegister(instr);
9273 vixl32::Register accumulator =
9274 InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
9275 vixl32::Register mul_left =
9276 InputRegisterAt(instr, HMultiplyAccumulate::kInputMulLeftIndex);
9277 vixl32::Register mul_right =
9278 InputRegisterAt(instr, HMultiplyAccumulate::kInputMulRightIndex);
9279
9280 if (instr->GetOpKind() == HInstruction::kAdd) {
9281 __ Mla(res, mul_left, mul_right, accumulator);
9282 } else {
9283 __ Mls(res, mul_left, mul_right, accumulator);
9284 }
9285}
9286
Artem Serov551b28f2016-10-18 19:11:30 +01009287void LocationsBuilderARMVIXL::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
9288 // Nothing to do, this should be removed during prepare for register allocator.
9289 LOG(FATAL) << "Unreachable";
9290}
9291
9292void InstructionCodeGeneratorARMVIXL::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
9293 // Nothing to do, this should be removed during prepare for register allocator.
9294 LOG(FATAL) << "Unreachable";
9295}
9296
9297// Simple implementation of packed switch - generate cascaded compare/jumps.
9298void LocationsBuilderARMVIXL::VisitPackedSwitch(HPackedSwitch* switch_instr) {
9299 LocationSummary* locations =
9300 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
9301 locations->SetInAt(0, Location::RequiresRegister());
9302 if (switch_instr->GetNumEntries() > kPackedSwitchCompareJumpThreshold &&
9303 codegen_->GetAssembler()->GetVIXLAssembler()->IsUsingT32()) {
9304 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the table base.
9305 if (switch_instr->GetStartValue() != 0) {
9306 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the bias.
9307 }
9308 }
9309}
9310
9311// TODO(VIXL): Investigate and reach the parity with old arm codegen.
9312void InstructionCodeGeneratorARMVIXL::VisitPackedSwitch(HPackedSwitch* switch_instr) {
9313 int32_t lower_bound = switch_instr->GetStartValue();
9314 uint32_t num_entries = switch_instr->GetNumEntries();
9315 LocationSummary* locations = switch_instr->GetLocations();
9316 vixl32::Register value_reg = InputRegisterAt(switch_instr, 0);
9317 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
9318
9319 if (num_entries <= kPackedSwitchCompareJumpThreshold ||
9320 !codegen_->GetAssembler()->GetVIXLAssembler()->IsUsingT32()) {
9321 // Create a series of compare/jumps.
Anton Kirilovedb2ac32016-11-30 15:14:10 +00009322 UseScratchRegisterScope temps(GetVIXLAssembler());
Artem Serov551b28f2016-10-18 19:11:30 +01009323 vixl32::Register temp_reg = temps.Acquire();
9324 // Note: It is fine for the below AddConstantSetFlags() using IP register to temporarily store
9325 // the immediate, because IP is used as the destination register. For the other
9326 // AddConstantSetFlags() and GenerateCompareWithImmediate(), the immediate values are constant,
9327 // and they can be encoded in the instruction without making use of IP register.
9328 __ Adds(temp_reg, value_reg, -lower_bound);
9329
9330 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
9331 // Jump to successors[0] if value == lower_bound.
9332 __ B(eq, codegen_->GetLabelOf(successors[0]));
9333 int32_t last_index = 0;
9334 for (; num_entries - last_index > 2; last_index += 2) {
9335 __ Adds(temp_reg, temp_reg, -2);
9336 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
9337 __ B(lo, codegen_->GetLabelOf(successors[last_index + 1]));
9338 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
9339 __ B(eq, codegen_->GetLabelOf(successors[last_index + 2]));
9340 }
9341 if (num_entries - last_index == 2) {
9342 // The last missing case_value.
9343 __ Cmp(temp_reg, 1);
9344 __ B(eq, codegen_->GetLabelOf(successors[last_index + 1]));
9345 }
9346
9347 // And the default for any other value.
9348 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
9349 __ B(codegen_->GetLabelOf(default_block));
9350 }
9351 } else {
9352 // Create a table lookup.
9353 vixl32::Register table_base = RegisterFrom(locations->GetTemp(0));
9354
9355 JumpTableARMVIXL* jump_table = codegen_->CreateJumpTable(switch_instr);
9356
9357 // Remove the bias.
9358 vixl32::Register key_reg;
9359 if (lower_bound != 0) {
9360 key_reg = RegisterFrom(locations->GetTemp(1));
9361 __ Sub(key_reg, value_reg, lower_bound);
9362 } else {
9363 key_reg = value_reg;
9364 }
9365
9366 // Check whether the value is in the table, jump to default block if not.
9367 __ Cmp(key_reg, num_entries - 1);
9368 __ B(hi, codegen_->GetLabelOf(default_block));
9369
Anton Kirilovedb2ac32016-11-30 15:14:10 +00009370 UseScratchRegisterScope temps(GetVIXLAssembler());
Artem Serov551b28f2016-10-18 19:11:30 +01009371 vixl32::Register jump_offset = temps.Acquire();
9372
9373 // Load jump offset from the table.
Scott Wakeling86e9d262017-01-18 15:59:24 +00009374 {
9375 const size_t jump_size = switch_instr->GetNumEntries() * sizeof(int32_t);
9376 ExactAssemblyScope aas(GetVIXLAssembler(),
9377 (vixl32::kMaxInstructionSizeInBytes * 4) + jump_size,
9378 CodeBufferCheckScope::kMaximumSize);
9379 __ adr(table_base, jump_table->GetTableStartLabel());
9380 __ ldr(jump_offset, MemOperand(table_base, key_reg, vixl32::LSL, 2));
Artem Serov551b28f2016-10-18 19:11:30 +01009381
Scott Wakeling86e9d262017-01-18 15:59:24 +00009382 // Jump to target block by branching to table_base(pc related) + offset.
9383 vixl32::Register target_address = table_base;
9384 __ add(target_address, table_base, jump_offset);
9385 __ bx(target_address);
Artem Serov09a940d2016-11-11 16:15:11 +00009386
Scott Wakeling86e9d262017-01-18 15:59:24 +00009387 jump_table->EmitTable(codegen_);
9388 }
Artem Serov551b28f2016-10-18 19:11:30 +01009389 }
9390}
9391
Artem Serov02d37832016-10-25 15:25:33 +01009392// Copy the result of a call into the given target.
Anton Kirilove28d9ae2016-10-25 18:17:23 +01009393void CodeGeneratorARMVIXL::MoveFromReturnRegister(Location trg, Primitive::Type type) {
9394 if (!trg.IsValid()) {
9395 DCHECK_EQ(type, Primitive::kPrimVoid);
9396 return;
9397 }
9398
9399 DCHECK_NE(type, Primitive::kPrimVoid);
9400
Artem Serovd4cc5b22016-11-04 11:19:09 +00009401 Location return_loc = InvokeDexCallingConventionVisitorARMVIXL().GetReturnLocation(type);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01009402 if (return_loc.Equals(trg)) {
9403 return;
9404 }
9405
9406 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
9407 // with the last branch.
9408 if (type == Primitive::kPrimLong) {
9409 TODO_VIXL32(FATAL);
9410 } else if (type == Primitive::kPrimDouble) {
9411 TODO_VIXL32(FATAL);
9412 } else {
9413 // Let the parallel move resolver take care of all of this.
9414 HParallelMove parallel_move(GetGraph()->GetArena());
9415 parallel_move.AddMove(return_loc, trg, type, nullptr);
9416 GetMoveResolver()->EmitNativeCode(&parallel_move);
9417 }
Scott Wakelingfe885462016-09-22 10:24:38 +01009418}
9419
xueliang.zhong8d2c4592016-11-23 17:05:25 +00009420void LocationsBuilderARMVIXL::VisitClassTableGet(HClassTableGet* instruction) {
9421 LocationSummary* locations =
9422 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
9423 locations->SetInAt(0, Location::RequiresRegister());
9424 locations->SetOut(Location::RequiresRegister());
Artem Serov551b28f2016-10-18 19:11:30 +01009425}
9426
xueliang.zhong8d2c4592016-11-23 17:05:25 +00009427void InstructionCodeGeneratorARMVIXL::VisitClassTableGet(HClassTableGet* instruction) {
9428 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
9429 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
9430 instruction->GetIndex(), kArmPointerSize).SizeValue();
9431 GetAssembler()->LoadFromOffset(kLoadWord,
9432 OutputRegister(instruction),
9433 InputRegisterAt(instruction, 0),
9434 method_offset);
9435 } else {
9436 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
9437 instruction->GetIndex(), kArmPointerSize));
9438 GetAssembler()->LoadFromOffset(kLoadWord,
9439 OutputRegister(instruction),
9440 InputRegisterAt(instruction, 0),
9441 mirror::Class::ImtPtrOffset(kArmPointerSize).Uint32Value());
9442 GetAssembler()->LoadFromOffset(kLoadWord,
9443 OutputRegister(instruction),
9444 OutputRegister(instruction),
9445 method_offset);
9446 }
Artem Serov551b28f2016-10-18 19:11:30 +01009447}
9448
Artem Serovc5fcb442016-12-02 19:19:58 +00009449static void PatchJitRootUse(uint8_t* code,
9450 const uint8_t* roots_data,
9451 VIXLUInt32Literal* literal,
9452 uint64_t index_in_table) {
9453 DCHECK(literal->IsBound());
9454 uint32_t literal_offset = literal->GetLocation();
9455 uintptr_t address =
9456 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
9457 uint8_t* data = code + literal_offset;
9458 reinterpret_cast<uint32_t*>(data)[0] = dchecked_integral_cast<uint32_t>(address);
9459}
9460
9461void CodeGeneratorARMVIXL::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
9462 for (const auto& entry : jit_string_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01009463 const StringReference& string_reference = entry.first;
9464 VIXLUInt32Literal* table_entry_literal = entry.second;
9465 const auto it = jit_string_roots_.find(string_reference);
Artem Serovc5fcb442016-12-02 19:19:58 +00009466 DCHECK(it != jit_string_roots_.end());
Vladimir Marko7d157fc2017-05-10 16:29:23 +01009467 uint64_t index_in_table = it->second;
9468 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Artem Serovc5fcb442016-12-02 19:19:58 +00009469 }
9470 for (const auto& entry : jit_class_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01009471 const TypeReference& type_reference = entry.first;
9472 VIXLUInt32Literal* table_entry_literal = entry.second;
9473 const auto it = jit_class_roots_.find(type_reference);
Artem Serovc5fcb442016-12-02 19:19:58 +00009474 DCHECK(it != jit_class_roots_.end());
Vladimir Marko7d157fc2017-05-10 16:29:23 +01009475 uint64_t index_in_table = it->second;
9476 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Artem Serovc5fcb442016-12-02 19:19:58 +00009477 }
9478}
9479
Artem Serovd4cc5b22016-11-04 11:19:09 +00009480void CodeGeneratorARMVIXL::EmitMovwMovtPlaceholder(
9481 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels,
9482 vixl32::Register out) {
Artem Serov0fb37192016-12-06 18:13:40 +00009483 ExactAssemblyScope aas(GetVIXLAssembler(),
9484 3 * vixl32::kMaxInstructionSizeInBytes,
9485 CodeBufferCheckScope::kMaximumSize);
Artem Serovd4cc5b22016-11-04 11:19:09 +00009486 // TODO(VIXL): Think about using mov instead of movw.
9487 __ bind(&labels->movw_label);
9488 __ movw(out, /* placeholder */ 0u);
9489 __ bind(&labels->movt_label);
9490 __ movt(out, /* placeholder */ 0u);
9491 __ bind(&labels->add_pc_label);
9492 __ add(out, out, pc);
9493}
9494
Scott Wakelingfe885462016-09-22 10:24:38 +01009495#undef __
9496#undef QUICK_ENTRY_POINT
9497#undef TODO_VIXL32
9498
9499} // namespace arm
9500} // namespace art