blob: 4d5f88e14ae3db6a5ec4150832785999f597b71b [file] [log] [blame]
Scott Wakelingfe885462016-09-22 10:24:38 +01001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_arm_vixl.h"
18
Vladimir Markoeee1c0e2017-04-21 17:58:41 +010019#include "arch/arm/asm_support_arm.h"
Scott Wakelingfe885462016-09-22 10:24:38 +010020#include "arch/arm/instruction_set_features_arm.h"
21#include "art_method.h"
22#include "code_generator_utils.h"
23#include "common_arm.h"
24#include "compiled_method.h"
25#include "entrypoints/quick/quick_entrypoints.h"
26#include "gc/accounting/card_table.h"
Anton Kirilov5ec62182016-10-13 20:16:02 +010027#include "intrinsics_arm_vixl.h"
Vladimir Markoeee1c0e2017-04-21 17:58:41 +010028#include "linker/arm/relative_patcher_thumb2.h"
Scott Wakelingfe885462016-09-22 10:24:38 +010029#include "mirror/array-inl.h"
30#include "mirror/class-inl.h"
31#include "thread.h"
32#include "utils/arm/assembler_arm_vixl.h"
33#include "utils/arm/managed_register_arm.h"
34#include "utils/assembler.h"
35#include "utils/stack_checks.h"
36
37namespace art {
38namespace arm {
39
40namespace vixl32 = vixl::aarch32;
41using namespace vixl32; // NOLINT(build/namespaces)
42
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +010043using helpers::DRegisterFrom;
Scott Wakelingfe885462016-09-22 10:24:38 +010044using helpers::DWARFReg;
Scott Wakelinga7812ae2016-10-17 10:03:36 +010045using helpers::HighDRegisterFrom;
46using helpers::HighRegisterFrom;
Donghui Bai426b49c2016-11-08 14:55:38 +080047using helpers::InputDRegisterAt;
Scott Wakelingfe885462016-09-22 10:24:38 +010048using helpers::InputOperandAt;
Scott Wakelingc34dba72016-10-03 10:14:44 +010049using helpers::InputRegister;
Scott Wakelinga7812ae2016-10-17 10:03:36 +010050using helpers::InputRegisterAt;
Scott Wakelingfe885462016-09-22 10:24:38 +010051using helpers::InputSRegisterAt;
Anton Kirilov644032c2016-12-06 17:51:43 +000052using helpers::InputVRegister;
Scott Wakelinga7812ae2016-10-17 10:03:36 +010053using helpers::InputVRegisterAt;
Scott Wakelingb77051e2016-11-21 19:46:00 +000054using helpers::Int32ConstantFrom;
Anton Kirilov644032c2016-12-06 17:51:43 +000055using helpers::Int64ConstantFrom;
Scott Wakelinga7812ae2016-10-17 10:03:36 +010056using helpers::LocationFrom;
57using helpers::LowRegisterFrom;
58using helpers::LowSRegisterFrom;
Donghui Bai426b49c2016-11-08 14:55:38 +080059using helpers::OperandFrom;
Scott Wakelinga7812ae2016-10-17 10:03:36 +010060using helpers::OutputRegister;
61using helpers::OutputSRegister;
62using helpers::OutputVRegister;
63using helpers::RegisterFrom;
64using helpers::SRegisterFrom;
Anton Kirilov644032c2016-12-06 17:51:43 +000065using helpers::Uint64ConstantFrom;
Scott Wakelingfe885462016-09-22 10:24:38 +010066
Artem Serov0fb37192016-12-06 18:13:40 +000067using vixl::ExactAssemblyScope;
68using vixl::CodeBufferCheckScope;
69
Scott Wakelingfe885462016-09-22 10:24:38 +010070using RegisterList = vixl32::RegisterList;
71
72static bool ExpectedPairLayout(Location location) {
73 // We expected this for both core and fpu register pairs.
74 return ((location.low() & 1) == 0) && (location.low() + 1 == location.high());
75}
Artem Serovd4cc5b22016-11-04 11:19:09 +000076// Use a local definition to prevent copying mistakes.
77static constexpr size_t kArmWordSize = static_cast<size_t>(kArmPointerSize);
78static constexpr size_t kArmBitsPerWord = kArmWordSize * kBitsPerByte;
Anton Kirilove28d9ae2016-10-25 18:17:23 +010079static constexpr int kCurrentMethodStackOffset = 0;
Artem Serov551b28f2016-10-18 19:11:30 +010080static constexpr uint32_t kPackedSwitchCompareJumpThreshold = 7;
Scott Wakelingfe885462016-09-22 10:24:38 +010081
Vladimir Markoeee1c0e2017-04-21 17:58:41 +010082// Reference load (except object array loads) is using LDR Rt, [Rn, #offset] which can handle
83// offset < 4KiB. For offsets >= 4KiB, the load shall be emitted as two or more instructions.
84// For the Baker read barrier implementation using link-generated thunks we need to split
85// the offset explicitly.
86constexpr uint32_t kReferenceLoadMinFarOffset = 4 * KB;
87
88// Flags controlling the use of link-time generated thunks for Baker read barriers.
89constexpr bool kBakerReadBarrierLinkTimeThunksEnableForFields = true;
90constexpr bool kBakerReadBarrierLinkTimeThunksEnableForArrays = true;
91constexpr bool kBakerReadBarrierLinkTimeThunksEnableForGcRoots = true;
92
93// The reserved entrypoint register for link-time generated thunks.
94const vixl32::Register kBakerCcEntrypointRegister = r4;
95
Scott Wakelingfe885462016-09-22 10:24:38 +010096#ifdef __
97#error "ARM Codegen VIXL macro-assembler macro already defined."
98#endif
99
Scott Wakelingfe885462016-09-22 10:24:38 +0100100// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
101#define __ down_cast<CodeGeneratorARMVIXL*>(codegen)->GetVIXLAssembler()-> // NOLINT
102#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kArmPointerSize, x).Int32Value()
103
104// Marker that code is yet to be, and must, be implemented.
105#define TODO_VIXL32(level) LOG(level) << __PRETTY_FUNCTION__ << " unimplemented "
106
Vladimir Markoeee1c0e2017-04-21 17:58:41 +0100107static inline void ExcludeIPAndBakerCcEntrypointRegister(UseScratchRegisterScope* temps,
108 HInstruction* instruction) {
109 DCHECK(temps->IsAvailable(ip));
110 temps->Exclude(ip);
111 DCHECK(!temps->IsAvailable(kBakerCcEntrypointRegister));
112 DCHECK_EQ(kBakerCcEntrypointRegister.GetCode(),
113 linker::Thumb2RelativePatcher::kBakerCcEntrypointRegister);
114 DCHECK_NE(instruction->GetLocations()->GetTempCount(), 0u);
115 DCHECK(RegisterFrom(instruction->GetLocations()->GetTemp(
116 instruction->GetLocations()->GetTempCount() - 1u)).Is(kBakerCcEntrypointRegister));
117}
118
119static inline void EmitPlaceholderBne(CodeGeneratorARMVIXL* codegen, vixl32::Label* patch_label) {
120 ExactAssemblyScope eas(codegen->GetVIXLAssembler(), kMaxInstructionSizeInBytes);
121 __ bind(patch_label);
122 vixl32::Label placeholder_label;
123 __ b(ne, EncodingSize(Wide), &placeholder_label); // Placeholder, patched at link-time.
124 __ bind(&placeholder_label);
125}
126
Vladimir Marko88abba22017-05-03 17:09:25 +0100127static inline bool CanEmitNarrowLdr(vixl32::Register rt, vixl32::Register rn, uint32_t offset) {
128 return rt.IsLow() && rn.IsLow() && offset < 32u;
129}
130
Vladimir Markoeee1c0e2017-04-21 17:58:41 +0100131class EmitAdrCode {
132 public:
133 EmitAdrCode(ArmVIXLMacroAssembler* assembler, vixl32::Register rd, vixl32::Label* label)
134 : assembler_(assembler), rd_(rd), label_(label) {
135 ExactAssemblyScope aas(assembler, kMaxInstructionSizeInBytes);
136 adr_location_ = assembler->GetCursorOffset();
137 assembler->adr(EncodingSize(Wide), rd, label);
138 }
139
140 ~EmitAdrCode() {
141 DCHECK(label_->IsBound());
142 // The ADR emitted by the assembler does not set the Thumb mode bit we need.
143 // TODO: Maybe extend VIXL to allow ADR for return address?
144 uint8_t* raw_adr = assembler_->GetBuffer()->GetOffsetAddress<uint8_t*>(adr_location_);
145 // Expecting ADR encoding T3 with `(offset & 1) == 0`.
146 DCHECK_EQ(raw_adr[1] & 0xfbu, 0xf2u); // Check bits 24-31, except 26.
147 DCHECK_EQ(raw_adr[0] & 0xffu, 0x0fu); // Check bits 16-23.
148 DCHECK_EQ(raw_adr[3] & 0x8fu, rd_.GetCode()); // Check bits 8-11 and 15.
149 DCHECK_EQ(raw_adr[2] & 0x01u, 0x00u); // Check bit 0, i.e. the `offset & 1`.
150 // Add the Thumb mode bit.
151 raw_adr[2] |= 0x01u;
152 }
153
154 private:
155 ArmVIXLMacroAssembler* const assembler_;
156 vixl32::Register rd_;
157 vixl32::Label* const label_;
158 int32_t adr_location_;
159};
160
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100161// SaveLiveRegisters and RestoreLiveRegisters from SlowPathCodeARM operate on sets of S registers,
162// for each live D registers they treat two corresponding S registers as live ones.
163//
164// Two following functions (SaveContiguousSRegisterList, RestoreContiguousSRegisterList) build
165// from a list of contiguous S registers a list of contiguous D registers (processing first/last
166// S registers corner cases) and save/restore this new list treating them as D registers.
167// - decreasing code size
168// - avoiding hazards on Cortex-A57, when a pair of S registers for an actual live D register is
169// restored and then used in regular non SlowPath code as D register.
170//
171// For the following example (v means the S register is live):
172// D names: | D0 | D1 | D2 | D4 | ...
173// S names: | S0 | S1 | S2 | S3 | S4 | S5 | S6 | S7 | ...
174// Live? | | v | v | v | v | v | v | | ...
175//
176// S1 and S6 will be saved/restored independently; D registers list (D1, D2) will be processed
177// as D registers.
178//
179// TODO(VIXL): All this code should be unnecessary once the VIXL AArch32 backend provides helpers
180// for lists of floating-point registers.
181static size_t SaveContiguousSRegisterList(size_t first,
182 size_t last,
183 CodeGenerator* codegen,
184 size_t stack_offset) {
185 static_assert(kSRegSizeInBytes == kArmWordSize, "Broken assumption on reg/word sizes.");
186 static_assert(kDRegSizeInBytes == 2 * kArmWordSize, "Broken assumption on reg/word sizes.");
187 DCHECK_LE(first, last);
188 if ((first == last) && (first == 0)) {
189 __ Vstr(vixl32::SRegister(first), MemOperand(sp, stack_offset));
190 return stack_offset + kSRegSizeInBytes;
191 }
192 if (first % 2 == 1) {
193 __ Vstr(vixl32::SRegister(first++), MemOperand(sp, stack_offset));
194 stack_offset += kSRegSizeInBytes;
195 }
196
197 bool save_last = false;
198 if (last % 2 == 0) {
199 save_last = true;
200 --last;
201 }
202
203 if (first < last) {
204 vixl32::DRegister d_reg = vixl32::DRegister(first / 2);
205 DCHECK_EQ((last - first + 1) % 2, 0u);
206 size_t number_of_d_regs = (last - first + 1) / 2;
207
208 if (number_of_d_regs == 1) {
209 __ Vstr(d_reg, MemOperand(sp, stack_offset));
210 } else if (number_of_d_regs > 1) {
211 UseScratchRegisterScope temps(down_cast<CodeGeneratorARMVIXL*>(codegen)->GetVIXLAssembler());
212 vixl32::Register base = sp;
213 if (stack_offset != 0) {
214 base = temps.Acquire();
Scott Wakelingb77051e2016-11-21 19:46:00 +0000215 __ Add(base, sp, Operand::From(stack_offset));
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100216 }
217 __ Vstm(F64, base, NO_WRITE_BACK, DRegisterList(d_reg, number_of_d_regs));
218 }
219 stack_offset += number_of_d_regs * kDRegSizeInBytes;
220 }
221
222 if (save_last) {
223 __ Vstr(vixl32::SRegister(last + 1), MemOperand(sp, stack_offset));
224 stack_offset += kSRegSizeInBytes;
225 }
226
227 return stack_offset;
228}
229
230static size_t RestoreContiguousSRegisterList(size_t first,
231 size_t last,
232 CodeGenerator* codegen,
233 size_t stack_offset) {
234 static_assert(kSRegSizeInBytes == kArmWordSize, "Broken assumption on reg/word sizes.");
235 static_assert(kDRegSizeInBytes == 2 * kArmWordSize, "Broken assumption on reg/word sizes.");
236 DCHECK_LE(first, last);
237 if ((first == last) && (first == 0)) {
238 __ Vldr(vixl32::SRegister(first), MemOperand(sp, stack_offset));
239 return stack_offset + kSRegSizeInBytes;
240 }
241 if (first % 2 == 1) {
242 __ Vldr(vixl32::SRegister(first++), MemOperand(sp, stack_offset));
243 stack_offset += kSRegSizeInBytes;
244 }
245
246 bool restore_last = false;
247 if (last % 2 == 0) {
248 restore_last = true;
249 --last;
250 }
251
252 if (first < last) {
253 vixl32::DRegister d_reg = vixl32::DRegister(first / 2);
254 DCHECK_EQ((last - first + 1) % 2, 0u);
255 size_t number_of_d_regs = (last - first + 1) / 2;
256 if (number_of_d_regs == 1) {
257 __ Vldr(d_reg, MemOperand(sp, stack_offset));
258 } else if (number_of_d_regs > 1) {
259 UseScratchRegisterScope temps(down_cast<CodeGeneratorARMVIXL*>(codegen)->GetVIXLAssembler());
260 vixl32::Register base = sp;
261 if (stack_offset != 0) {
262 base = temps.Acquire();
Scott Wakelingb77051e2016-11-21 19:46:00 +0000263 __ Add(base, sp, Operand::From(stack_offset));
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100264 }
265 __ Vldm(F64, base, NO_WRITE_BACK, DRegisterList(d_reg, number_of_d_regs));
266 }
267 stack_offset += number_of_d_regs * kDRegSizeInBytes;
268 }
269
270 if (restore_last) {
271 __ Vldr(vixl32::SRegister(last + 1), MemOperand(sp, stack_offset));
272 stack_offset += kSRegSizeInBytes;
273 }
274
275 return stack_offset;
276}
277
278void SlowPathCodeARMVIXL::SaveLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
279 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
280 size_t orig_offset = stack_offset;
281
282 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ true);
283 for (uint32_t i : LowToHighBits(core_spills)) {
284 // If the register holds an object, update the stack mask.
285 if (locations->RegisterContainsObject(i)) {
286 locations->SetStackBit(stack_offset / kVRegSize);
287 }
288 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
289 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
290 saved_core_stack_offsets_[i] = stack_offset;
291 stack_offset += kArmWordSize;
292 }
293
294 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
295 arm_codegen->GetAssembler()->StoreRegisterList(core_spills, orig_offset);
296
297 uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ false);
298 orig_offset = stack_offset;
299 for (uint32_t i : LowToHighBits(fp_spills)) {
300 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
301 saved_fpu_stack_offsets_[i] = stack_offset;
302 stack_offset += kArmWordSize;
303 }
304
305 stack_offset = orig_offset;
306 while (fp_spills != 0u) {
307 uint32_t begin = CTZ(fp_spills);
308 uint32_t tmp = fp_spills + (1u << begin);
309 fp_spills &= tmp; // Clear the contiguous range of 1s.
310 uint32_t end = (tmp == 0u) ? 32u : CTZ(tmp); // CTZ(0) is undefined.
311 stack_offset = SaveContiguousSRegisterList(begin, end - 1, codegen, stack_offset);
312 }
313 DCHECK_LE(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
314}
315
316void SlowPathCodeARMVIXL::RestoreLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
317 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
318 size_t orig_offset = stack_offset;
319
320 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ true);
321 for (uint32_t i : LowToHighBits(core_spills)) {
322 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
323 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
324 stack_offset += kArmWordSize;
325 }
326
327 // TODO(VIXL): Check the coherency of stack_offset after this with a test.
328 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
329 arm_codegen->GetAssembler()->LoadRegisterList(core_spills, orig_offset);
330
331 uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ false);
332 while (fp_spills != 0u) {
333 uint32_t begin = CTZ(fp_spills);
334 uint32_t tmp = fp_spills + (1u << begin);
335 fp_spills &= tmp; // Clear the contiguous range of 1s.
336 uint32_t end = (tmp == 0u) ? 32u : CTZ(tmp); // CTZ(0) is undefined.
337 stack_offset = RestoreContiguousSRegisterList(begin, end - 1, codegen, stack_offset);
338 }
339 DCHECK_LE(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
340}
341
342class NullCheckSlowPathARMVIXL : public SlowPathCodeARMVIXL {
343 public:
344 explicit NullCheckSlowPathARMVIXL(HNullCheck* instruction) : SlowPathCodeARMVIXL(instruction) {}
345
346 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
347 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
348 __ Bind(GetEntryLabel());
349 if (instruction_->CanThrowIntoCatchBlock()) {
350 // Live registers will be restored in the catch block if caught.
351 SaveLiveRegisters(codegen, instruction_->GetLocations());
352 }
353 arm_codegen->InvokeRuntime(kQuickThrowNullPointer,
354 instruction_,
355 instruction_->GetDexPc(),
356 this);
357 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
358 }
359
360 bool IsFatal() const OVERRIDE { return true; }
361
362 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathARMVIXL"; }
363
364 private:
365 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARMVIXL);
366};
367
Scott Wakelingfe885462016-09-22 10:24:38 +0100368class DivZeroCheckSlowPathARMVIXL : public SlowPathCodeARMVIXL {
369 public:
370 explicit DivZeroCheckSlowPathARMVIXL(HDivZeroCheck* instruction)
371 : SlowPathCodeARMVIXL(instruction) {}
372
373 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100374 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
Scott Wakelingfe885462016-09-22 10:24:38 +0100375 __ Bind(GetEntryLabel());
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100376 arm_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Scott Wakelingfe885462016-09-22 10:24:38 +0100377 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
378 }
379
380 bool IsFatal() const OVERRIDE { return true; }
381
382 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathARMVIXL"; }
383
384 private:
385 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARMVIXL);
386};
387
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100388class SuspendCheckSlowPathARMVIXL : public SlowPathCodeARMVIXL {
389 public:
390 SuspendCheckSlowPathARMVIXL(HSuspendCheck* instruction, HBasicBlock* successor)
391 : SlowPathCodeARMVIXL(instruction), successor_(successor) {}
392
393 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
394 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
395 __ Bind(GetEntryLabel());
396 arm_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
397 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
398 if (successor_ == nullptr) {
399 __ B(GetReturnLabel());
400 } else {
401 __ B(arm_codegen->GetLabelOf(successor_));
402 }
403 }
404
405 vixl32::Label* GetReturnLabel() {
406 DCHECK(successor_ == nullptr);
407 return &return_label_;
408 }
409
410 HBasicBlock* GetSuccessor() const {
411 return successor_;
412 }
413
414 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathARMVIXL"; }
415
416 private:
417 // If not null, the block to branch to after the suspend check.
418 HBasicBlock* const successor_;
419
420 // If `successor_` is null, the label to branch to after the suspend check.
421 vixl32::Label return_label_;
422
423 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARMVIXL);
424};
425
Scott Wakelingc34dba72016-10-03 10:14:44 +0100426class BoundsCheckSlowPathARMVIXL : public SlowPathCodeARMVIXL {
427 public:
428 explicit BoundsCheckSlowPathARMVIXL(HBoundsCheck* instruction)
429 : SlowPathCodeARMVIXL(instruction) {}
430
431 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
432 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
433 LocationSummary* locations = instruction_->GetLocations();
434
435 __ Bind(GetEntryLabel());
436 if (instruction_->CanThrowIntoCatchBlock()) {
437 // Live registers will be restored in the catch block if caught.
438 SaveLiveRegisters(codegen, instruction_->GetLocations());
439 }
440 // We're moving two locations to locations that could overlap, so we need a parallel
441 // move resolver.
442 InvokeRuntimeCallingConventionARMVIXL calling_convention;
443 codegen->EmitParallelMoves(
444 locations->InAt(0),
445 LocationFrom(calling_convention.GetRegisterAt(0)),
446 Primitive::kPrimInt,
447 locations->InAt(1),
448 LocationFrom(calling_convention.GetRegisterAt(1)),
449 Primitive::kPrimInt);
450 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
451 ? kQuickThrowStringBounds
452 : kQuickThrowArrayBounds;
453 arm_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
454 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
455 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
456 }
457
458 bool IsFatal() const OVERRIDE { return true; }
459
460 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathARMVIXL"; }
461
462 private:
463 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARMVIXL);
464};
465
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100466class LoadClassSlowPathARMVIXL : public SlowPathCodeARMVIXL {
467 public:
468 LoadClassSlowPathARMVIXL(HLoadClass* cls, HInstruction* at, uint32_t dex_pc, bool do_clinit)
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000469 : SlowPathCodeARMVIXL(at), cls_(cls), dex_pc_(dex_pc), do_clinit_(do_clinit) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100470 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
471 }
472
473 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000474 LocationSummary* locations = instruction_->GetLocations();
Vladimir Markoea4c1262017-02-06 19:59:33 +0000475 Location out = locations->Out();
476 constexpr bool call_saves_everything_except_r0 = (!kUseReadBarrier || kUseBakerReadBarrier);
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100477
478 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
479 __ Bind(GetEntryLabel());
480 SaveLiveRegisters(codegen, locations);
481
482 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Vladimir Markoea4c1262017-02-06 19:59:33 +0000483 // For HLoadClass/kBssEntry/kSaveEverything, make sure we preserve the address of the entry.
484 DCHECK_EQ(instruction_->IsLoadClass(), cls_ == instruction_);
485 bool is_load_class_bss_entry =
486 (cls_ == instruction_) && (cls_->GetLoadKind() == HLoadClass::LoadKind::kBssEntry);
487 vixl32::Register entry_address;
488 if (is_load_class_bss_entry && call_saves_everything_except_r0) {
489 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
490 // In the unlucky case that the `temp` is R0, we preserve the address in `out` across
491 // the kSaveEverything call.
492 bool temp_is_r0 = temp.Is(calling_convention.GetRegisterAt(0));
493 entry_address = temp_is_r0 ? RegisterFrom(out) : temp;
494 DCHECK(!entry_address.Is(calling_convention.GetRegisterAt(0)));
495 if (temp_is_r0) {
496 __ Mov(entry_address, temp);
497 }
498 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000499 dex::TypeIndex type_index = cls_->GetTypeIndex();
500 __ Mov(calling_convention.GetRegisterAt(0), type_index.index_);
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100501 QuickEntrypointEnum entrypoint = do_clinit_ ? kQuickInitializeStaticStorage
502 : kQuickInitializeType;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000503 arm_codegen->InvokeRuntime(entrypoint, instruction_, dex_pc_, this);
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100504 if (do_clinit_) {
505 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
506 } else {
507 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
508 }
509
Vladimir Markoea4c1262017-02-06 19:59:33 +0000510 // For HLoadClass/kBssEntry, store the resolved Class to the BSS entry.
511 if (is_load_class_bss_entry) {
512 if (call_saves_everything_except_r0) {
513 // The class entry address was preserved in `entry_address` thanks to kSaveEverything.
514 __ Str(r0, MemOperand(entry_address));
515 } else {
516 // For non-Baker read barrier, we need to re-calculate the address of the string entry.
517 UseScratchRegisterScope temps(
518 down_cast<CodeGeneratorARMVIXL*>(codegen)->GetVIXLAssembler());
519 vixl32::Register temp = temps.Acquire();
520 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
521 arm_codegen->NewTypeBssEntryPatch(cls_->GetDexFile(), type_index);
522 arm_codegen->EmitMovwMovtPlaceholder(labels, temp);
523 __ Str(r0, MemOperand(temp));
524 }
525 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100526 // Move the class to the desired location.
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100527 if (out.IsValid()) {
528 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
529 arm_codegen->Move32(locations->Out(), LocationFrom(r0));
530 }
531 RestoreLiveRegisters(codegen, locations);
532 __ B(GetExitLabel());
533 }
534
535 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathARMVIXL"; }
536
537 private:
538 // The class this slow path will load.
539 HLoadClass* const cls_;
540
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100541 // The dex PC of `at_`.
542 const uint32_t dex_pc_;
543
544 // Whether to initialize the class.
545 const bool do_clinit_;
546
547 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARMVIXL);
548};
549
Artem Serovd4cc5b22016-11-04 11:19:09 +0000550class LoadStringSlowPathARMVIXL : public SlowPathCodeARMVIXL {
551 public:
552 explicit LoadStringSlowPathARMVIXL(HLoadString* instruction)
553 : SlowPathCodeARMVIXL(instruction) {}
554
555 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Vladimir Markoea4c1262017-02-06 19:59:33 +0000556 DCHECK(instruction_->IsLoadString());
557 DCHECK_EQ(instruction_->AsLoadString()->GetLoadKind(), HLoadString::LoadKind::kBssEntry);
Artem Serovd4cc5b22016-11-04 11:19:09 +0000558 LocationSummary* locations = instruction_->GetLocations();
559 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
560 HLoadString* load = instruction_->AsLoadString();
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000561 const dex::StringIndex string_index = load->GetStringIndex();
Artem Serovd4cc5b22016-11-04 11:19:09 +0000562 vixl32::Register out = OutputRegister(load);
Artem Serovd4cc5b22016-11-04 11:19:09 +0000563 constexpr bool call_saves_everything_except_r0 = (!kUseReadBarrier || kUseBakerReadBarrier);
564
565 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
566 __ Bind(GetEntryLabel());
567 SaveLiveRegisters(codegen, locations);
568
569 InvokeRuntimeCallingConventionARMVIXL calling_convention;
570 // In the unlucky case that the `temp` is R0, we preserve the address in `out` across
Vladimir Markoea4c1262017-02-06 19:59:33 +0000571 // the kSaveEverything call.
572 vixl32::Register entry_address;
573 if (call_saves_everything_except_r0) {
574 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
575 bool temp_is_r0 = (temp.Is(calling_convention.GetRegisterAt(0)));
576 entry_address = temp_is_r0 ? out : temp;
577 DCHECK(!entry_address.Is(calling_convention.GetRegisterAt(0)));
578 if (temp_is_r0) {
579 __ Mov(entry_address, temp);
580 }
Artem Serovd4cc5b22016-11-04 11:19:09 +0000581 }
582
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000583 __ Mov(calling_convention.GetRegisterAt(0), string_index.index_);
Artem Serovd4cc5b22016-11-04 11:19:09 +0000584 arm_codegen->InvokeRuntime(kQuickResolveString, instruction_, instruction_->GetDexPc(), this);
585 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
586
587 // Store the resolved String to the .bss entry.
588 if (call_saves_everything_except_r0) {
589 // The string entry address was preserved in `entry_address` thanks to kSaveEverything.
590 __ Str(r0, MemOperand(entry_address));
591 } else {
592 // For non-Baker read barrier, we need to re-calculate the address of the string entry.
Vladimir Markoea4c1262017-02-06 19:59:33 +0000593 UseScratchRegisterScope temps(
594 down_cast<CodeGeneratorARMVIXL*>(codegen)->GetVIXLAssembler());
595 vixl32::Register temp = temps.Acquire();
Artem Serovd4cc5b22016-11-04 11:19:09 +0000596 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
597 arm_codegen->NewPcRelativeStringPatch(load->GetDexFile(), string_index);
Vladimir Markoea4c1262017-02-06 19:59:33 +0000598 arm_codegen->EmitMovwMovtPlaceholder(labels, temp);
599 __ Str(r0, MemOperand(temp));
Artem Serovd4cc5b22016-11-04 11:19:09 +0000600 }
601
602 arm_codegen->Move32(locations->Out(), LocationFrom(r0));
603 RestoreLiveRegisters(codegen, locations);
604
605 __ B(GetExitLabel());
606 }
607
608 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathARMVIXL"; }
609
610 private:
611 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARMVIXL);
612};
613
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100614class TypeCheckSlowPathARMVIXL : public SlowPathCodeARMVIXL {
615 public:
616 TypeCheckSlowPathARMVIXL(HInstruction* instruction, bool is_fatal)
617 : SlowPathCodeARMVIXL(instruction), is_fatal_(is_fatal) {}
618
619 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
620 LocationSummary* locations = instruction_->GetLocations();
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100621 DCHECK(instruction_->IsCheckCast()
622 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
623
624 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
625 __ Bind(GetEntryLabel());
626
627 if (!is_fatal_) {
Artem Serovcfbe9132016-10-14 15:58:56 +0100628 SaveLiveRegisters(codegen, locations);
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100629 }
630
631 // We're moving two locations to locations that could overlap, so we need a parallel
632 // move resolver.
633 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100634
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800635 codegen->EmitParallelMoves(locations->InAt(0),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800636 LocationFrom(calling_convention.GetRegisterAt(0)),
637 Primitive::kPrimNot,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800638 locations->InAt(1),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800639 LocationFrom(calling_convention.GetRegisterAt(1)),
640 Primitive::kPrimNot);
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100641 if (instruction_->IsInstanceOf()) {
Artem Serovcfbe9132016-10-14 15:58:56 +0100642 arm_codegen->InvokeRuntime(kQuickInstanceofNonTrivial,
643 instruction_,
644 instruction_->GetDexPc(),
645 this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800646 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Artem Serovcfbe9132016-10-14 15:58:56 +0100647 arm_codegen->Move32(locations->Out(), LocationFrom(r0));
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100648 } else {
649 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800650 arm_codegen->InvokeRuntime(kQuickCheckInstanceOf,
651 instruction_,
652 instruction_->GetDexPc(),
653 this);
654 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100655 }
656
657 if (!is_fatal_) {
Artem Serovcfbe9132016-10-14 15:58:56 +0100658 RestoreLiveRegisters(codegen, locations);
659 __ B(GetExitLabel());
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100660 }
661 }
662
663 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathARMVIXL"; }
664
665 bool IsFatal() const OVERRIDE { return is_fatal_; }
666
667 private:
668 const bool is_fatal_;
669
670 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARMVIXL);
671};
672
Scott Wakelingc34dba72016-10-03 10:14:44 +0100673class DeoptimizationSlowPathARMVIXL : public SlowPathCodeARMVIXL {
674 public:
675 explicit DeoptimizationSlowPathARMVIXL(HDeoptimize* instruction)
676 : SlowPathCodeARMVIXL(instruction) {}
677
678 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
679 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
680 __ Bind(GetEntryLabel());
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100681 LocationSummary* locations = instruction_->GetLocations();
682 SaveLiveRegisters(codegen, locations);
683 InvokeRuntimeCallingConventionARMVIXL calling_convention;
684 __ Mov(calling_convention.GetRegisterAt(0),
685 static_cast<uint32_t>(instruction_->AsDeoptimize()->GetDeoptimizationKind()));
686
Scott Wakelingc34dba72016-10-03 10:14:44 +0100687 arm_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100688 CheckEntrypointTypes<kQuickDeoptimize, void, DeoptimizationKind>();
Scott Wakelingc34dba72016-10-03 10:14:44 +0100689 }
690
691 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathARMVIXL"; }
692
693 private:
694 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARMVIXL);
695};
696
697class ArraySetSlowPathARMVIXL : public SlowPathCodeARMVIXL {
698 public:
699 explicit ArraySetSlowPathARMVIXL(HInstruction* instruction) : SlowPathCodeARMVIXL(instruction) {}
700
701 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
702 LocationSummary* locations = instruction_->GetLocations();
703 __ Bind(GetEntryLabel());
704 SaveLiveRegisters(codegen, locations);
705
706 InvokeRuntimeCallingConventionARMVIXL calling_convention;
707 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
708 parallel_move.AddMove(
709 locations->InAt(0),
710 LocationFrom(calling_convention.GetRegisterAt(0)),
711 Primitive::kPrimNot,
712 nullptr);
713 parallel_move.AddMove(
714 locations->InAt(1),
715 LocationFrom(calling_convention.GetRegisterAt(1)),
716 Primitive::kPrimInt,
717 nullptr);
718 parallel_move.AddMove(
719 locations->InAt(2),
720 LocationFrom(calling_convention.GetRegisterAt(2)),
721 Primitive::kPrimNot,
722 nullptr);
723 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
724
725 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
726 arm_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
727 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
728 RestoreLiveRegisters(codegen, locations);
729 __ B(GetExitLabel());
730 }
731
732 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathARMVIXL"; }
733
734 private:
735 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathARMVIXL);
736};
737
Roland Levillain54f869e2017-03-06 13:54:11 +0000738// Abstract base class for read barrier slow paths marking a reference
739// `ref`.
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000740//
Roland Levillain54f869e2017-03-06 13:54:11 +0000741// Argument `entrypoint` must be a register location holding the read
742// barrier marking runtime entry point to be invoked.
743class ReadBarrierMarkSlowPathBaseARMVIXL : public SlowPathCodeARMVIXL {
744 protected:
745 ReadBarrierMarkSlowPathBaseARMVIXL(HInstruction* instruction, Location ref, Location entrypoint)
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000746 : SlowPathCodeARMVIXL(instruction), ref_(ref), entrypoint_(entrypoint) {
747 DCHECK(kEmitCompilerReadBarrier);
748 }
749
Roland Levillain54f869e2017-03-06 13:54:11 +0000750 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathBaseARMVIXL"; }
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000751
Roland Levillain54f869e2017-03-06 13:54:11 +0000752 // Generate assembly code calling the read barrier marking runtime
753 // entry point (ReadBarrierMarkRegX).
754 void GenerateReadBarrierMarkRuntimeCall(CodeGenerator* codegen) {
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000755 vixl32::Register ref_reg = RegisterFrom(ref_);
Roland Levillain47b3ab22017-02-27 14:31:35 +0000756
Roland Levillain47b3ab22017-02-27 14:31:35 +0000757 // No need to save live registers; it's taken care of by the
758 // entrypoint. Also, there is no need to update the stack mask,
759 // as this runtime call will not trigger a garbage collection.
760 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
761 DCHECK(!ref_reg.Is(sp));
762 DCHECK(!ref_reg.Is(lr));
763 DCHECK(!ref_reg.Is(pc));
764 // IP is used internally by the ReadBarrierMarkRegX entry point
765 // as a temporary, it cannot be the entry point's input/output.
766 DCHECK(!ref_reg.Is(ip));
767 DCHECK(ref_reg.IsRegister()) << ref_reg;
768 // "Compact" slow path, saving two moves.
769 //
770 // Instead of using the standard runtime calling convention (input
771 // and output in R0):
772 //
773 // R0 <- ref
774 // R0 <- ReadBarrierMark(R0)
775 // ref <- R0
776 //
777 // we just use rX (the register containing `ref`) as input and output
778 // of a dedicated entrypoint:
779 //
780 // rX <- ReadBarrierMarkRegX(rX)
781 //
782 if (entrypoint_.IsValid()) {
783 arm_codegen->ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction_, this);
784 __ Blx(RegisterFrom(entrypoint_));
785 } else {
Roland Levillain54f869e2017-03-06 13:54:11 +0000786 // Entrypoint is not already loaded, load from the thread.
Roland Levillain47b3ab22017-02-27 14:31:35 +0000787 int32_t entry_point_offset =
788 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmPointerSize>(ref_reg.GetCode());
789 // This runtime call does not require a stack map.
790 arm_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
791 }
Roland Levillain47b3ab22017-02-27 14:31:35 +0000792 }
793
Roland Levillain47b3ab22017-02-27 14:31:35 +0000794 // The location (register) of the marked object reference.
795 const Location ref_;
796
797 // The location of the entrypoint if already loaded.
798 const Location entrypoint_;
799
Roland Levillain54f869e2017-03-06 13:54:11 +0000800 private:
801 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathBaseARMVIXL);
Roland Levillain47b3ab22017-02-27 14:31:35 +0000802};
803
Scott Wakelingc34dba72016-10-03 10:14:44 +0100804// Slow path marking an object reference `ref` during a read
805// barrier. The field `obj.field` in the object `obj` holding this
Roland Levillain54f869e2017-03-06 13:54:11 +0000806// reference does not get updated by this slow path after marking.
Roland Levillain47b3ab22017-02-27 14:31:35 +0000807//
Scott Wakelingc34dba72016-10-03 10:14:44 +0100808// This means that after the execution of this slow path, `ref` will
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000809// always be up-to-date, but `obj.field` may not; i.e., after the
810// flip, `ref` will be a to-space reference, but `obj.field` will
811// probably still be a from-space reference (unless it gets updated by
812// another thread, or if another thread installed another object
813// reference (different from `ref`) in `obj.field`).
Roland Levillainba650a42017-03-06 13:52:32 +0000814//
815// If `entrypoint` is a valid location it is assumed to already be
816// holding the entrypoint. The case where the entrypoint is passed in
Roland Levillain54f869e2017-03-06 13:54:11 +0000817// is when the decision to mark is based on whether the GC is marking.
818class ReadBarrierMarkSlowPathARMVIXL : public ReadBarrierMarkSlowPathBaseARMVIXL {
Roland Levillain47b3ab22017-02-27 14:31:35 +0000819 public:
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000820 ReadBarrierMarkSlowPathARMVIXL(HInstruction* instruction,
821 Location ref,
822 Location entrypoint = Location::NoLocation())
Roland Levillain54f869e2017-03-06 13:54:11 +0000823 : ReadBarrierMarkSlowPathBaseARMVIXL(instruction, ref, entrypoint) {
Roland Levillain47b3ab22017-02-27 14:31:35 +0000824 DCHECK(kEmitCompilerReadBarrier);
825 }
826
Roland Levillain47b3ab22017-02-27 14:31:35 +0000827 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathARMVIXL"; }
828
829 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
830 LocationSummary* locations = instruction_->GetLocations();
Roland Levillain54f869e2017-03-06 13:54:11 +0000831 DCHECK(locations->CanCall());
832 DCHECK(ref_.IsRegister()) << ref_;
833 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_.reg())) << ref_.reg();
834 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
835 << "Unexpected instruction in read barrier marking slow path: "
836 << instruction_->DebugName();
837
838 __ Bind(GetEntryLabel());
839 GenerateReadBarrierMarkRuntimeCall(codegen);
840 __ B(GetExitLabel());
841 }
842
843 private:
844 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathARMVIXL);
845};
846
847// Slow path loading `obj`'s lock word, loading a reference from
848// object `*(obj + offset + (index << scale_factor))` into `ref`, and
849// marking `ref` if `obj` is gray according to the lock word (Baker
850// read barrier). The field `obj.field` in the object `obj` holding
851// this reference does not get updated by this slow path after marking
852// (see LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARMVIXL
853// below for that).
854//
855// This means that after the execution of this slow path, `ref` will
856// always be up-to-date, but `obj.field` may not; i.e., after the
857// flip, `ref` will be a to-space reference, but `obj.field` will
858// probably still be a from-space reference (unless it gets updated by
859// another thread, or if another thread installed another object
860// reference (different from `ref`) in `obj.field`).
861//
862// Argument `entrypoint` must be a register location holding the read
863// barrier marking runtime entry point to be invoked.
864class LoadReferenceWithBakerReadBarrierSlowPathARMVIXL : public ReadBarrierMarkSlowPathBaseARMVIXL {
865 public:
866 LoadReferenceWithBakerReadBarrierSlowPathARMVIXL(HInstruction* instruction,
867 Location ref,
868 vixl32::Register obj,
869 uint32_t offset,
870 Location index,
871 ScaleFactor scale_factor,
872 bool needs_null_check,
873 vixl32::Register temp,
874 Location entrypoint)
875 : ReadBarrierMarkSlowPathBaseARMVIXL(instruction, ref, entrypoint),
876 obj_(obj),
877 offset_(offset),
878 index_(index),
879 scale_factor_(scale_factor),
880 needs_null_check_(needs_null_check),
881 temp_(temp) {
882 DCHECK(kEmitCompilerReadBarrier);
883 DCHECK(kUseBakerReadBarrier);
884 }
885
Roland Levillain47b3ab22017-02-27 14:31:35 +0000886 const char* GetDescription() const OVERRIDE {
Roland Levillain54f869e2017-03-06 13:54:11 +0000887 return "LoadReferenceWithBakerReadBarrierSlowPathARMVIXL";
Roland Levillain47b3ab22017-02-27 14:31:35 +0000888 }
889
890 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
891 LocationSummary* locations = instruction_->GetLocations();
892 vixl32::Register ref_reg = RegisterFrom(ref_);
893 DCHECK(locations->CanCall());
894 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg.GetCode())) << ref_reg;
Roland Levillain47b3ab22017-02-27 14:31:35 +0000895 DCHECK(instruction_->IsInstanceFieldGet() ||
896 instruction_->IsStaticFieldGet() ||
897 instruction_->IsArrayGet() ||
898 instruction_->IsArraySet() ||
Roland Levillain47b3ab22017-02-27 14:31:35 +0000899 instruction_->IsInstanceOf() ||
900 instruction_->IsCheckCast() ||
901 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
902 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
903 << "Unexpected instruction in read barrier marking slow path: "
904 << instruction_->DebugName();
905 // The read barrier instrumentation of object ArrayGet
906 // instructions does not support the HIntermediateAddress
907 // instruction.
908 DCHECK(!(instruction_->IsArrayGet() &&
909 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
910
Roland Levillain54f869e2017-03-06 13:54:11 +0000911 // Temporary register `temp_`, used to store the lock word, must
912 // not be IP, as we may use it to emit the reference load (in the
913 // call to GenerateRawReferenceLoad below), and we need the lock
914 // word to still be in `temp_` after the reference load.
915 DCHECK(!temp_.Is(ip));
916
Roland Levillain47b3ab22017-02-27 14:31:35 +0000917 __ Bind(GetEntryLabel());
Roland Levillain54f869e2017-03-06 13:54:11 +0000918
919 // When using MaybeGenerateReadBarrierSlow, the read barrier call is
920 // inserted after the original load. However, in fast path based
921 // Baker's read barriers, we need to perform the load of
922 // mirror::Object::monitor_ *before* the original reference load.
923 // This load-load ordering is required by the read barrier.
Roland Levillainff487002017-03-07 16:50:01 +0000924 // The slow path (for Baker's algorithm) should look like:
Roland Levillain54f869e2017-03-06 13:54:11 +0000925 //
926 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
927 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
928 // HeapReference<mirror::Object> ref = *src; // Original reference load.
929 // bool is_gray = (rb_state == ReadBarrier::GrayState());
930 // if (is_gray) {
931 // ref = entrypoint(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
932 // }
933 //
934 // Note: the original implementation in ReadBarrier::Barrier is
935 // slightly more complex as it performs additional checks that we do
936 // not do here for performance reasons.
937
Roland Levillain47b3ab22017-02-27 14:31:35 +0000938 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
Roland Levillain54f869e2017-03-06 13:54:11 +0000939
940 // /* int32_t */ monitor = obj->monitor_
941 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
942 arm_codegen->GetAssembler()->LoadFromOffset(kLoadWord, temp_, obj_, monitor_offset);
943 if (needs_null_check_) {
944 codegen->MaybeRecordImplicitNullCheck(instruction_);
Roland Levillain47b3ab22017-02-27 14:31:35 +0000945 }
Roland Levillain54f869e2017-03-06 13:54:11 +0000946 // /* LockWord */ lock_word = LockWord(monitor)
947 static_assert(sizeof(LockWord) == sizeof(int32_t),
948 "art::LockWord and int32_t have different sizes.");
949
950 // Introduce a dependency on the lock_word including the rb_state,
951 // which shall prevent load-load reordering without using
952 // a memory barrier (which would be more expensive).
953 // `obj` is unchanged by this operation, but its value now depends
954 // on `temp`.
955 __ Add(obj_, obj_, Operand(temp_, ShiftType::LSR, 32));
956
957 // The actual reference load.
958 // A possible implicit null check has already been handled above.
959 arm_codegen->GenerateRawReferenceLoad(
960 instruction_, ref_, obj_, offset_, index_, scale_factor_, /* needs_null_check */ false);
961
962 // Mark the object `ref` when `obj` is gray.
963 //
964 // if (rb_state == ReadBarrier::GrayState())
965 // ref = ReadBarrier::Mark(ref);
966 //
967 // Given the numeric representation, it's enough to check the low bit of the
968 // rb_state. We do that by shifting the bit out of the lock word with LSRS
969 // which can be a 16-bit instruction unlike the TST immediate.
970 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
971 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
972 __ Lsrs(temp_, temp_, LockWord::kReadBarrierStateShift + 1);
973 __ B(cc, GetExitLabel()); // Carry flag is the last bit shifted out by LSRS.
974 GenerateReadBarrierMarkRuntimeCall(codegen);
975
Roland Levillain47b3ab22017-02-27 14:31:35 +0000976 __ B(GetExitLabel());
977 }
978
979 private:
Roland Levillain54f869e2017-03-06 13:54:11 +0000980 // The register containing the object holding the marked object reference field.
981 vixl32::Register obj_;
982 // The offset, index and scale factor to access the reference in `obj_`.
983 uint32_t offset_;
984 Location index_;
985 ScaleFactor scale_factor_;
986 // Is a null check required?
987 bool needs_null_check_;
988 // A temporary register used to hold the lock word of `obj_`.
989 vixl32::Register temp_;
Roland Levillain47b3ab22017-02-27 14:31:35 +0000990
Roland Levillain54f869e2017-03-06 13:54:11 +0000991 DISALLOW_COPY_AND_ASSIGN(LoadReferenceWithBakerReadBarrierSlowPathARMVIXL);
Roland Levillain47b3ab22017-02-27 14:31:35 +0000992};
993
Roland Levillain54f869e2017-03-06 13:54:11 +0000994// Slow path loading `obj`'s lock word, loading a reference from
995// object `*(obj + offset + (index << scale_factor))` into `ref`, and
996// marking `ref` if `obj` is gray according to the lock word (Baker
997// read barrier). If needed, this slow path also atomically updates
998// the field `obj.field` in the object `obj` holding this reference
999// after marking (contrary to
1000// LoadReferenceWithBakerReadBarrierSlowPathARMVIXL above, which never
1001// tries to update `obj.field`).
Roland Levillain47b3ab22017-02-27 14:31:35 +00001002//
1003// This means that after the execution of this slow path, both `ref`
1004// and `obj.field` will be up-to-date; i.e., after the flip, both will
1005// hold the same to-space reference (unless another thread installed
1006// another object reference (different from `ref`) in `obj.field`).
Roland Levillainba650a42017-03-06 13:52:32 +00001007//
Roland Levillain54f869e2017-03-06 13:54:11 +00001008//
1009// Argument `entrypoint` must be a register location holding the read
1010// barrier marking runtime entry point to be invoked.
1011class LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARMVIXL
1012 : public ReadBarrierMarkSlowPathBaseARMVIXL {
Roland Levillain47b3ab22017-02-27 14:31:35 +00001013 public:
Roland Levillain54f869e2017-03-06 13:54:11 +00001014 LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARMVIXL(HInstruction* instruction,
1015 Location ref,
1016 vixl32::Register obj,
1017 uint32_t offset,
1018 Location index,
1019 ScaleFactor scale_factor,
1020 bool needs_null_check,
1021 vixl32::Register temp1,
1022 vixl32::Register temp2,
1023 Location entrypoint)
1024 : ReadBarrierMarkSlowPathBaseARMVIXL(instruction, ref, entrypoint),
Roland Levillain47b3ab22017-02-27 14:31:35 +00001025 obj_(obj),
Roland Levillain54f869e2017-03-06 13:54:11 +00001026 offset_(offset),
1027 index_(index),
1028 scale_factor_(scale_factor),
1029 needs_null_check_(needs_null_check),
Roland Levillain47b3ab22017-02-27 14:31:35 +00001030 temp1_(temp1),
Roland Levillain54f869e2017-03-06 13:54:11 +00001031 temp2_(temp2) {
Roland Levillain47b3ab22017-02-27 14:31:35 +00001032 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain54f869e2017-03-06 13:54:11 +00001033 DCHECK(kUseBakerReadBarrier);
Roland Levillain47b3ab22017-02-27 14:31:35 +00001034 }
1035
1036 const char* GetDescription() const OVERRIDE {
Roland Levillain54f869e2017-03-06 13:54:11 +00001037 return "LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARMVIXL";
Roland Levillain47b3ab22017-02-27 14:31:35 +00001038 }
1039
1040 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
1041 LocationSummary* locations = instruction_->GetLocations();
1042 vixl32::Register ref_reg = RegisterFrom(ref_);
1043 DCHECK(locations->CanCall());
1044 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg.GetCode())) << ref_reg;
Roland Levillain54f869e2017-03-06 13:54:11 +00001045 DCHECK_NE(ref_.reg(), LocationFrom(temp1_).reg());
1046
1047 // This slow path is only used by the UnsafeCASObject intrinsic at the moment.
Roland Levillain47b3ab22017-02-27 14:31:35 +00001048 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
1049 << "Unexpected instruction in read barrier marking and field updating slow path: "
1050 << instruction_->DebugName();
1051 DCHECK(instruction_->GetLocations()->Intrinsified());
1052 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
Roland Levillain54f869e2017-03-06 13:54:11 +00001053 DCHECK_EQ(offset_, 0u);
1054 DCHECK_EQ(scale_factor_, ScaleFactor::TIMES_1);
1055 Location field_offset = index_;
1056 DCHECK(field_offset.IsRegisterPair()) << field_offset;
1057
1058 // Temporary register `temp1_`, used to store the lock word, must
1059 // not be IP, as we may use it to emit the reference load (in the
1060 // call to GenerateRawReferenceLoad below), and we need the lock
1061 // word to still be in `temp1_` after the reference load.
1062 DCHECK(!temp1_.Is(ip));
Roland Levillain47b3ab22017-02-27 14:31:35 +00001063
1064 __ Bind(GetEntryLabel());
1065
Roland Levillainff487002017-03-07 16:50:01 +00001066 // The implementation is similar to LoadReferenceWithBakerReadBarrierSlowPathARMVIXL's:
1067 //
1068 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
1069 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
1070 // HeapReference<mirror::Object> ref = *src; // Original reference load.
1071 // bool is_gray = (rb_state == ReadBarrier::GrayState());
1072 // if (is_gray) {
1073 // old_ref = ref;
1074 // ref = entrypoint(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
1075 // compareAndSwapObject(obj, field_offset, old_ref, ref);
1076 // }
1077
Roland Levillain54f869e2017-03-06 13:54:11 +00001078 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
1079
1080 // /* int32_t */ monitor = obj->monitor_
1081 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
1082 arm_codegen->GetAssembler()->LoadFromOffset(kLoadWord, temp1_, obj_, monitor_offset);
1083 if (needs_null_check_) {
1084 codegen->MaybeRecordImplicitNullCheck(instruction_);
1085 }
1086 // /* LockWord */ lock_word = LockWord(monitor)
1087 static_assert(sizeof(LockWord) == sizeof(int32_t),
1088 "art::LockWord and int32_t have different sizes.");
1089
1090 // Introduce a dependency on the lock_word including the rb_state,
1091 // which shall prevent load-load reordering without using
1092 // a memory barrier (which would be more expensive).
1093 // `obj` is unchanged by this operation, but its value now depends
1094 // on `temp`.
1095 __ Add(obj_, obj_, Operand(temp1_, ShiftType::LSR, 32));
1096
1097 // The actual reference load.
1098 // A possible implicit null check has already been handled above.
1099 arm_codegen->GenerateRawReferenceLoad(
1100 instruction_, ref_, obj_, offset_, index_, scale_factor_, /* needs_null_check */ false);
1101
1102 // Mark the object `ref` when `obj` is gray.
1103 //
1104 // if (rb_state == ReadBarrier::GrayState())
1105 // ref = ReadBarrier::Mark(ref);
1106 //
1107 // Given the numeric representation, it's enough to check the low bit of the
1108 // rb_state. We do that by shifting the bit out of the lock word with LSRS
1109 // which can be a 16-bit instruction unlike the TST immediate.
1110 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
1111 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
1112 __ Lsrs(temp1_, temp1_, LockWord::kReadBarrierStateShift + 1);
1113 __ B(cc, GetExitLabel()); // Carry flag is the last bit shifted out by LSRS.
1114
1115 // Save the old value of the reference before marking it.
Roland Levillain47b3ab22017-02-27 14:31:35 +00001116 // Note that we cannot use IP to save the old reference, as IP is
1117 // used internally by the ReadBarrierMarkRegX entry point, and we
1118 // need the old reference after the call to that entry point.
1119 DCHECK(!temp1_.Is(ip));
1120 __ Mov(temp1_, ref_reg);
Roland Levillain27b1f9c2017-01-17 16:56:34 +00001121
Roland Levillain54f869e2017-03-06 13:54:11 +00001122 GenerateReadBarrierMarkRuntimeCall(codegen);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001123
1124 // If the new reference is different from the old reference,
Roland Levillain54f869e2017-03-06 13:54:11 +00001125 // update the field in the holder (`*(obj_ + field_offset)`).
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001126 //
1127 // Note that this field could also hold a different object, if
1128 // another thread had concurrently changed it. In that case, the
1129 // LDREX/SUBS/ITNE sequence of instructions in the compare-and-set
1130 // (CAS) operation below would abort the CAS, leaving the field
1131 // as-is.
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001132 __ Cmp(temp1_, ref_reg);
Roland Levillain54f869e2017-03-06 13:54:11 +00001133 __ B(eq, GetExitLabel());
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001134
1135 // Update the the holder's field atomically. This may fail if
1136 // mutator updates before us, but it's OK. This is achieved
1137 // using a strong compare-and-set (CAS) operation with relaxed
1138 // memory synchronization ordering, where the expected value is
1139 // the old reference and the desired value is the new reference.
1140
1141 UseScratchRegisterScope temps(arm_codegen->GetVIXLAssembler());
1142 // Convenience aliases.
1143 vixl32::Register base = obj_;
1144 // The UnsafeCASObject intrinsic uses a register pair as field
1145 // offset ("long offset"), of which only the low part contains
1146 // data.
Roland Levillain54f869e2017-03-06 13:54:11 +00001147 vixl32::Register offset = LowRegisterFrom(field_offset);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001148 vixl32::Register expected = temp1_;
1149 vixl32::Register value = ref_reg;
1150 vixl32::Register tmp_ptr = temps.Acquire(); // Pointer to actual memory.
1151 vixl32::Register tmp = temp2_; // Value in memory.
1152
1153 __ Add(tmp_ptr, base, offset);
1154
1155 if (kPoisonHeapReferences) {
1156 arm_codegen->GetAssembler()->PoisonHeapReference(expected);
1157 if (value.Is(expected)) {
1158 // Do not poison `value`, as it is the same register as
1159 // `expected`, which has just been poisoned.
1160 } else {
1161 arm_codegen->GetAssembler()->PoisonHeapReference(value);
1162 }
1163 }
1164
1165 // do {
1166 // tmp = [r_ptr] - expected;
1167 // } while (tmp == 0 && failure([r_ptr] <- r_new_value));
1168
1169 vixl32::Label loop_head, exit_loop;
1170 __ Bind(&loop_head);
1171
1172 __ Ldrex(tmp, MemOperand(tmp_ptr));
1173
1174 __ Subs(tmp, tmp, expected);
1175
1176 {
Artem Serov0fb37192016-12-06 18:13:40 +00001177 ExactAssemblyScope aas(arm_codegen->GetVIXLAssembler(),
1178 2 * kMaxInstructionSizeInBytes,
1179 CodeBufferCheckScope::kMaximumSize);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001180
1181 __ it(ne);
1182 __ clrex(ne);
1183 }
1184
Artem Serov517d9f62016-12-12 15:51:15 +00001185 __ B(ne, &exit_loop, /* far_target */ false);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001186
1187 __ Strex(tmp, value, MemOperand(tmp_ptr));
1188 __ Cmp(tmp, 1);
Artem Serov517d9f62016-12-12 15:51:15 +00001189 __ B(eq, &loop_head, /* far_target */ false);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001190
1191 __ Bind(&exit_loop);
1192
1193 if (kPoisonHeapReferences) {
1194 arm_codegen->GetAssembler()->UnpoisonHeapReference(expected);
1195 if (value.Is(expected)) {
1196 // Do not unpoison `value`, as it is the same register as
1197 // `expected`, which has just been unpoisoned.
1198 } else {
1199 arm_codegen->GetAssembler()->UnpoisonHeapReference(value);
1200 }
1201 }
1202
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001203 __ B(GetExitLabel());
1204 }
1205
1206 private:
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001207 // The register containing the object holding the marked object reference field.
1208 const vixl32::Register obj_;
Roland Levillain54f869e2017-03-06 13:54:11 +00001209 // The offset, index and scale factor to access the reference in `obj_`.
1210 uint32_t offset_;
1211 Location index_;
1212 ScaleFactor scale_factor_;
1213 // Is a null check required?
1214 bool needs_null_check_;
1215 // A temporary register used to hold the lock word of `obj_`; and
1216 // also to hold the original reference value, when the reference is
1217 // marked.
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001218 const vixl32::Register temp1_;
Roland Levillain54f869e2017-03-06 13:54:11 +00001219 // A temporary register used in the implementation of the CAS, to
1220 // update the object's reference field.
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001221 const vixl32::Register temp2_;
1222
Roland Levillain54f869e2017-03-06 13:54:11 +00001223 DISALLOW_COPY_AND_ASSIGN(LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARMVIXL);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001224};
1225
1226// Slow path generating a read barrier for a heap reference.
1227class ReadBarrierForHeapReferenceSlowPathARMVIXL : public SlowPathCodeARMVIXL {
1228 public:
1229 ReadBarrierForHeapReferenceSlowPathARMVIXL(HInstruction* instruction,
1230 Location out,
1231 Location ref,
1232 Location obj,
1233 uint32_t offset,
1234 Location index)
1235 : SlowPathCodeARMVIXL(instruction),
1236 out_(out),
1237 ref_(ref),
1238 obj_(obj),
1239 offset_(offset),
1240 index_(index) {
1241 DCHECK(kEmitCompilerReadBarrier);
1242 // If `obj` is equal to `out` or `ref`, it means the initial object
1243 // has been overwritten by (or after) the heap object reference load
1244 // to be instrumented, e.g.:
1245 //
1246 // __ LoadFromOffset(kLoadWord, out, out, offset);
1247 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
1248 //
1249 // In that case, we have lost the information about the original
1250 // object, and the emitted read barrier cannot work properly.
1251 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
1252 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
1253 }
1254
1255 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
1256 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
1257 LocationSummary* locations = instruction_->GetLocations();
1258 vixl32::Register reg_out = RegisterFrom(out_);
1259 DCHECK(locations->CanCall());
1260 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out.GetCode()));
1261 DCHECK(instruction_->IsInstanceFieldGet() ||
1262 instruction_->IsStaticFieldGet() ||
1263 instruction_->IsArrayGet() ||
1264 instruction_->IsInstanceOf() ||
1265 instruction_->IsCheckCast() ||
Andreas Gamped9911ee2017-03-27 13:27:24 -07001266 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001267 << "Unexpected instruction in read barrier for heap reference slow path: "
1268 << instruction_->DebugName();
1269 // The read barrier instrumentation of object ArrayGet
1270 // instructions does not support the HIntermediateAddress
1271 // instruction.
1272 DCHECK(!(instruction_->IsArrayGet() &&
1273 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
1274
1275 __ Bind(GetEntryLabel());
1276 SaveLiveRegisters(codegen, locations);
1277
1278 // We may have to change the index's value, but as `index_` is a
1279 // constant member (like other "inputs" of this slow path),
1280 // introduce a copy of it, `index`.
1281 Location index = index_;
1282 if (index_.IsValid()) {
1283 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
1284 if (instruction_->IsArrayGet()) {
1285 // Compute the actual memory offset and store it in `index`.
1286 vixl32::Register index_reg = RegisterFrom(index_);
1287 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg.GetCode()));
1288 if (codegen->IsCoreCalleeSaveRegister(index_reg.GetCode())) {
1289 // We are about to change the value of `index_reg` (see the
1290 // calls to art::arm::Thumb2Assembler::Lsl and
1291 // art::arm::Thumb2Assembler::AddConstant below), but it has
1292 // not been saved by the previous call to
1293 // art::SlowPathCode::SaveLiveRegisters, as it is a
1294 // callee-save register --
1295 // art::SlowPathCode::SaveLiveRegisters does not consider
1296 // callee-save registers, as it has been designed with the
1297 // assumption that callee-save registers are supposed to be
1298 // handled by the called function. So, as a callee-save
1299 // register, `index_reg` _would_ eventually be saved onto
1300 // the stack, but it would be too late: we would have
1301 // changed its value earlier. Therefore, we manually save
1302 // it here into another freely available register,
1303 // `free_reg`, chosen of course among the caller-save
1304 // registers (as a callee-save `free_reg` register would
1305 // exhibit the same problem).
1306 //
1307 // Note we could have requested a temporary register from
1308 // the register allocator instead; but we prefer not to, as
1309 // this is a slow path, and we know we can find a
1310 // caller-save register that is available.
1311 vixl32::Register free_reg = FindAvailableCallerSaveRegister(codegen);
1312 __ Mov(free_reg, index_reg);
1313 index_reg = free_reg;
1314 index = LocationFrom(index_reg);
1315 } else {
1316 // The initial register stored in `index_` has already been
1317 // saved in the call to art::SlowPathCode::SaveLiveRegisters
1318 // (as it is not a callee-save register), so we can freely
1319 // use it.
1320 }
1321 // Shifting the index value contained in `index_reg` by the scale
1322 // factor (2) cannot overflow in practice, as the runtime is
1323 // unable to allocate object arrays with a size larger than
1324 // 2^26 - 1 (that is, 2^28 - 4 bytes).
1325 __ Lsl(index_reg, index_reg, TIMES_4);
1326 static_assert(
1327 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
1328 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
1329 __ Add(index_reg, index_reg, offset_);
1330 } else {
1331 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
1332 // intrinsics, `index_` is not shifted by a scale factor of 2
1333 // (as in the case of ArrayGet), as it is actually an offset
1334 // to an object field within an object.
1335 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
1336 DCHECK(instruction_->GetLocations()->Intrinsified());
1337 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
1338 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
1339 << instruction_->AsInvoke()->GetIntrinsic();
1340 DCHECK_EQ(offset_, 0U);
1341 DCHECK(index_.IsRegisterPair());
1342 // UnsafeGet's offset location is a register pair, the low
1343 // part contains the correct offset.
1344 index = index_.ToLow();
1345 }
1346 }
1347
1348 // We're moving two or three locations to locations that could
1349 // overlap, so we need a parallel move resolver.
1350 InvokeRuntimeCallingConventionARMVIXL calling_convention;
1351 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
1352 parallel_move.AddMove(ref_,
1353 LocationFrom(calling_convention.GetRegisterAt(0)),
1354 Primitive::kPrimNot,
1355 nullptr);
1356 parallel_move.AddMove(obj_,
1357 LocationFrom(calling_convention.GetRegisterAt(1)),
1358 Primitive::kPrimNot,
1359 nullptr);
1360 if (index.IsValid()) {
1361 parallel_move.AddMove(index,
1362 LocationFrom(calling_convention.GetRegisterAt(2)),
1363 Primitive::kPrimInt,
1364 nullptr);
1365 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
1366 } else {
1367 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
1368 __ Mov(calling_convention.GetRegisterAt(2), offset_);
1369 }
1370 arm_codegen->InvokeRuntime(kQuickReadBarrierSlow, instruction_, instruction_->GetDexPc(), this);
1371 CheckEntrypointTypes<
1372 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
1373 arm_codegen->Move32(out_, LocationFrom(r0));
1374
1375 RestoreLiveRegisters(codegen, locations);
1376 __ B(GetExitLabel());
1377 }
1378
1379 const char* GetDescription() const OVERRIDE {
1380 return "ReadBarrierForHeapReferenceSlowPathARMVIXL";
1381 }
1382
1383 private:
1384 vixl32::Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
1385 uint32_t ref = RegisterFrom(ref_).GetCode();
1386 uint32_t obj = RegisterFrom(obj_).GetCode();
1387 for (uint32_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
1388 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
1389 return vixl32::Register(i);
1390 }
1391 }
1392 // We shall never fail to find a free caller-save register, as
1393 // there are more than two core caller-save registers on ARM
1394 // (meaning it is possible to find one which is different from
1395 // `ref` and `obj`).
1396 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
1397 LOG(FATAL) << "Could not find a free caller-save register";
1398 UNREACHABLE();
1399 }
1400
1401 const Location out_;
1402 const Location ref_;
1403 const Location obj_;
1404 const uint32_t offset_;
1405 // An additional location containing an index to an array.
1406 // Only used for HArrayGet and the UnsafeGetObject &
1407 // UnsafeGetObjectVolatile intrinsics.
1408 const Location index_;
1409
1410 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathARMVIXL);
1411};
1412
1413// Slow path generating a read barrier for a GC root.
1414class ReadBarrierForRootSlowPathARMVIXL : public SlowPathCodeARMVIXL {
1415 public:
1416 ReadBarrierForRootSlowPathARMVIXL(HInstruction* instruction, Location out, Location root)
1417 : SlowPathCodeARMVIXL(instruction), out_(out), root_(root) {
1418 DCHECK(kEmitCompilerReadBarrier);
1419 }
1420
1421 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
1422 LocationSummary* locations = instruction_->GetLocations();
1423 vixl32::Register reg_out = RegisterFrom(out_);
1424 DCHECK(locations->CanCall());
1425 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out.GetCode()));
1426 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
1427 << "Unexpected instruction in read barrier for GC root slow path: "
1428 << instruction_->DebugName();
1429
1430 __ Bind(GetEntryLabel());
1431 SaveLiveRegisters(codegen, locations);
1432
1433 InvokeRuntimeCallingConventionARMVIXL calling_convention;
1434 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
1435 arm_codegen->Move32(LocationFrom(calling_convention.GetRegisterAt(0)), root_);
1436 arm_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
1437 instruction_,
1438 instruction_->GetDexPc(),
1439 this);
1440 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
1441 arm_codegen->Move32(out_, LocationFrom(r0));
1442
1443 RestoreLiveRegisters(codegen, locations);
1444 __ B(GetExitLabel());
1445 }
1446
1447 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathARMVIXL"; }
1448
1449 private:
1450 const Location out_;
1451 const Location root_;
1452
1453 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathARMVIXL);
1454};
Scott Wakelingc34dba72016-10-03 10:14:44 +01001455
Scott Wakelingfe885462016-09-22 10:24:38 +01001456inline vixl32::Condition ARMCondition(IfCondition cond) {
1457 switch (cond) {
1458 case kCondEQ: return eq;
1459 case kCondNE: return ne;
1460 case kCondLT: return lt;
1461 case kCondLE: return le;
1462 case kCondGT: return gt;
1463 case kCondGE: return ge;
1464 case kCondB: return lo;
1465 case kCondBE: return ls;
1466 case kCondA: return hi;
1467 case kCondAE: return hs;
1468 }
1469 LOG(FATAL) << "Unreachable";
1470 UNREACHABLE();
1471}
1472
1473// Maps signed condition to unsigned condition.
1474inline vixl32::Condition ARMUnsignedCondition(IfCondition cond) {
1475 switch (cond) {
1476 case kCondEQ: return eq;
1477 case kCondNE: return ne;
1478 // Signed to unsigned.
1479 case kCondLT: return lo;
1480 case kCondLE: return ls;
1481 case kCondGT: return hi;
1482 case kCondGE: return hs;
1483 // Unsigned remain unchanged.
1484 case kCondB: return lo;
1485 case kCondBE: return ls;
1486 case kCondA: return hi;
1487 case kCondAE: return hs;
1488 }
1489 LOG(FATAL) << "Unreachable";
1490 UNREACHABLE();
1491}
1492
1493inline vixl32::Condition ARMFPCondition(IfCondition cond, bool gt_bias) {
1494 // The ARM condition codes can express all the necessary branches, see the
1495 // "Meaning (floating-point)" column in the table A8-1 of the ARMv7 reference manual.
1496 // There is no dex instruction or HIR that would need the missing conditions
1497 // "equal or unordered" or "not equal".
1498 switch (cond) {
1499 case kCondEQ: return eq;
1500 case kCondNE: return ne /* unordered */;
1501 case kCondLT: return gt_bias ? cc : lt /* unordered */;
1502 case kCondLE: return gt_bias ? ls : le /* unordered */;
1503 case kCondGT: return gt_bias ? hi /* unordered */ : gt;
1504 case kCondGE: return gt_bias ? cs /* unordered */ : ge;
1505 default:
1506 LOG(FATAL) << "UNREACHABLE";
1507 UNREACHABLE();
1508 }
1509}
1510
Anton Kirilov74234da2017-01-13 14:42:47 +00001511inline ShiftType ShiftFromOpKind(HDataProcWithShifterOp::OpKind op_kind) {
1512 switch (op_kind) {
1513 case HDataProcWithShifterOp::kASR: return ShiftType::ASR;
1514 case HDataProcWithShifterOp::kLSL: return ShiftType::LSL;
1515 case HDataProcWithShifterOp::kLSR: return ShiftType::LSR;
1516 default:
1517 LOG(FATAL) << "Unexpected op kind " << op_kind;
1518 UNREACHABLE();
1519 }
1520}
1521
Scott Wakelingfe885462016-09-22 10:24:38 +01001522void CodeGeneratorARMVIXL::DumpCoreRegister(std::ostream& stream, int reg) const {
1523 stream << vixl32::Register(reg);
1524}
1525
1526void CodeGeneratorARMVIXL::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
1527 stream << vixl32::SRegister(reg);
1528}
1529
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001530static uint32_t ComputeSRegisterListMask(const SRegisterList& regs) {
Scott Wakelingfe885462016-09-22 10:24:38 +01001531 uint32_t mask = 0;
1532 for (uint32_t i = regs.GetFirstSRegister().GetCode();
1533 i <= regs.GetLastSRegister().GetCode();
1534 ++i) {
1535 mask |= (1 << i);
1536 }
1537 return mask;
1538}
1539
Artem Serovd4cc5b22016-11-04 11:19:09 +00001540// Saves the register in the stack. Returns the size taken on stack.
1541size_t CodeGeneratorARMVIXL::SaveCoreRegister(size_t stack_index ATTRIBUTE_UNUSED,
1542 uint32_t reg_id ATTRIBUTE_UNUSED) {
1543 TODO_VIXL32(FATAL);
1544 return 0;
1545}
1546
1547// Restores the register from the stack. Returns the size taken on stack.
1548size_t CodeGeneratorARMVIXL::RestoreCoreRegister(size_t stack_index ATTRIBUTE_UNUSED,
1549 uint32_t reg_id ATTRIBUTE_UNUSED) {
1550 TODO_VIXL32(FATAL);
1551 return 0;
1552}
1553
1554size_t CodeGeneratorARMVIXL::SaveFloatingPointRegister(size_t stack_index ATTRIBUTE_UNUSED,
1555 uint32_t reg_id ATTRIBUTE_UNUSED) {
1556 TODO_VIXL32(FATAL);
1557 return 0;
1558}
1559
1560size_t CodeGeneratorARMVIXL::RestoreFloatingPointRegister(size_t stack_index ATTRIBUTE_UNUSED,
1561 uint32_t reg_id ATTRIBUTE_UNUSED) {
1562 TODO_VIXL32(FATAL);
1563 return 0;
Anton Kirilove28d9ae2016-10-25 18:17:23 +01001564}
1565
Anton Kirilov74234da2017-01-13 14:42:47 +00001566static void GenerateDataProcInstruction(HInstruction::InstructionKind kind,
1567 vixl32::Register out,
1568 vixl32::Register first,
1569 const Operand& second,
1570 CodeGeneratorARMVIXL* codegen) {
1571 if (second.IsImmediate() && second.GetImmediate() == 0) {
1572 const Operand in = kind == HInstruction::kAnd
1573 ? Operand(0)
1574 : Operand(first);
1575
1576 __ Mov(out, in);
1577 } else {
1578 switch (kind) {
1579 case HInstruction::kAdd:
1580 __ Add(out, first, second);
1581 break;
1582 case HInstruction::kAnd:
1583 __ And(out, first, second);
1584 break;
1585 case HInstruction::kOr:
1586 __ Orr(out, first, second);
1587 break;
1588 case HInstruction::kSub:
1589 __ Sub(out, first, second);
1590 break;
1591 case HInstruction::kXor:
1592 __ Eor(out, first, second);
1593 break;
1594 default:
1595 LOG(FATAL) << "Unexpected instruction kind: " << kind;
1596 UNREACHABLE();
1597 }
1598 }
1599}
1600
1601static void GenerateDataProc(HInstruction::InstructionKind kind,
1602 const Location& out,
1603 const Location& first,
1604 const Operand& second_lo,
1605 const Operand& second_hi,
1606 CodeGeneratorARMVIXL* codegen) {
1607 const vixl32::Register first_hi = HighRegisterFrom(first);
1608 const vixl32::Register first_lo = LowRegisterFrom(first);
1609 const vixl32::Register out_hi = HighRegisterFrom(out);
1610 const vixl32::Register out_lo = LowRegisterFrom(out);
1611
1612 if (kind == HInstruction::kAdd) {
1613 __ Adds(out_lo, first_lo, second_lo);
1614 __ Adc(out_hi, first_hi, second_hi);
1615 } else if (kind == HInstruction::kSub) {
1616 __ Subs(out_lo, first_lo, second_lo);
1617 __ Sbc(out_hi, first_hi, second_hi);
1618 } else {
1619 GenerateDataProcInstruction(kind, out_lo, first_lo, second_lo, codegen);
1620 GenerateDataProcInstruction(kind, out_hi, first_hi, second_hi, codegen);
1621 }
1622}
1623
1624static Operand GetShifterOperand(vixl32::Register rm, ShiftType shift, uint32_t shift_imm) {
1625 return shift_imm == 0 ? Operand(rm) : Operand(rm, shift, shift_imm);
1626}
1627
1628static void GenerateLongDataProc(HDataProcWithShifterOp* instruction,
1629 CodeGeneratorARMVIXL* codegen) {
1630 DCHECK_EQ(instruction->GetType(), Primitive::kPrimLong);
1631 DCHECK(HDataProcWithShifterOp::IsShiftOp(instruction->GetOpKind()));
1632
1633 const LocationSummary* const locations = instruction->GetLocations();
1634 const uint32_t shift_value = instruction->GetShiftAmount();
1635 const HInstruction::InstructionKind kind = instruction->GetInstrKind();
1636 const Location first = locations->InAt(0);
1637 const Location second = locations->InAt(1);
1638 const Location out = locations->Out();
1639 const vixl32::Register first_hi = HighRegisterFrom(first);
1640 const vixl32::Register first_lo = LowRegisterFrom(first);
1641 const vixl32::Register out_hi = HighRegisterFrom(out);
1642 const vixl32::Register out_lo = LowRegisterFrom(out);
1643 const vixl32::Register second_hi = HighRegisterFrom(second);
1644 const vixl32::Register second_lo = LowRegisterFrom(second);
1645 const ShiftType shift = ShiftFromOpKind(instruction->GetOpKind());
1646
1647 if (shift_value >= 32) {
1648 if (shift == ShiftType::LSL) {
1649 GenerateDataProcInstruction(kind,
1650 out_hi,
1651 first_hi,
1652 Operand(second_lo, ShiftType::LSL, shift_value - 32),
1653 codegen);
1654 GenerateDataProcInstruction(kind, out_lo, first_lo, 0, codegen);
1655 } else if (shift == ShiftType::ASR) {
1656 GenerateDataProc(kind,
1657 out,
1658 first,
1659 GetShifterOperand(second_hi, ShiftType::ASR, shift_value - 32),
1660 Operand(second_hi, ShiftType::ASR, 31),
1661 codegen);
1662 } else {
1663 DCHECK_EQ(shift, ShiftType::LSR);
1664 GenerateDataProc(kind,
1665 out,
1666 first,
1667 GetShifterOperand(second_hi, ShiftType::LSR, shift_value - 32),
1668 0,
1669 codegen);
1670 }
1671 } else {
1672 DCHECK_GT(shift_value, 1U);
1673 DCHECK_LT(shift_value, 32U);
1674
1675 UseScratchRegisterScope temps(codegen->GetVIXLAssembler());
1676
1677 if (shift == ShiftType::LSL) {
1678 // We are not doing this for HInstruction::kAdd because the output will require
1679 // Location::kOutputOverlap; not applicable to other cases.
1680 if (kind == HInstruction::kOr || kind == HInstruction::kXor) {
1681 GenerateDataProcInstruction(kind,
1682 out_hi,
1683 first_hi,
1684 Operand(second_hi, ShiftType::LSL, shift_value),
1685 codegen);
1686 GenerateDataProcInstruction(kind,
1687 out_hi,
1688 out_hi,
1689 Operand(second_lo, ShiftType::LSR, 32 - shift_value),
1690 codegen);
1691 GenerateDataProcInstruction(kind,
1692 out_lo,
1693 first_lo,
1694 Operand(second_lo, ShiftType::LSL, shift_value),
1695 codegen);
1696 } else {
1697 const vixl32::Register temp = temps.Acquire();
1698
1699 __ Lsl(temp, second_hi, shift_value);
1700 __ Orr(temp, temp, Operand(second_lo, ShiftType::LSR, 32 - shift_value));
1701 GenerateDataProc(kind,
1702 out,
1703 first,
1704 Operand(second_lo, ShiftType::LSL, shift_value),
1705 temp,
1706 codegen);
1707 }
1708 } else {
1709 DCHECK(shift == ShiftType::ASR || shift == ShiftType::LSR);
1710
1711 // We are not doing this for HInstruction::kAdd because the output will require
1712 // Location::kOutputOverlap; not applicable to other cases.
1713 if (kind == HInstruction::kOr || kind == HInstruction::kXor) {
1714 GenerateDataProcInstruction(kind,
1715 out_lo,
1716 first_lo,
1717 Operand(second_lo, ShiftType::LSR, shift_value),
1718 codegen);
1719 GenerateDataProcInstruction(kind,
1720 out_lo,
1721 out_lo,
1722 Operand(second_hi, ShiftType::LSL, 32 - shift_value),
1723 codegen);
1724 GenerateDataProcInstruction(kind,
1725 out_hi,
1726 first_hi,
1727 Operand(second_hi, shift, shift_value),
1728 codegen);
1729 } else {
1730 const vixl32::Register temp = temps.Acquire();
1731
1732 __ Lsr(temp, second_lo, shift_value);
1733 __ Orr(temp, temp, Operand(second_hi, ShiftType::LSL, 32 - shift_value));
1734 GenerateDataProc(kind,
1735 out,
1736 first,
1737 temp,
1738 Operand(second_hi, shift, shift_value),
1739 codegen);
1740 }
1741 }
1742 }
1743}
1744
Donghui Bai426b49c2016-11-08 14:55:38 +08001745static void GenerateVcmp(HInstruction* instruction, CodeGeneratorARMVIXL* codegen) {
1746 const Location rhs_loc = instruction->GetLocations()->InAt(1);
1747 if (rhs_loc.IsConstant()) {
1748 // 0.0 is the only immediate that can be encoded directly in
1749 // a VCMP instruction.
1750 //
1751 // Both the JLS (section 15.20.1) and the JVMS (section 6.5)
1752 // specify that in a floating-point comparison, positive zero
1753 // and negative zero are considered equal, so we can use the
1754 // literal 0.0 for both cases here.
1755 //
1756 // Note however that some methods (Float.equal, Float.compare,
1757 // Float.compareTo, Double.equal, Double.compare,
1758 // Double.compareTo, Math.max, Math.min, StrictMath.max,
1759 // StrictMath.min) consider 0.0 to be (strictly) greater than
1760 // -0.0. So if we ever translate calls to these methods into a
1761 // HCompare instruction, we must handle the -0.0 case with
1762 // care here.
1763 DCHECK(rhs_loc.GetConstant()->IsArithmeticZero());
1764
1765 const Primitive::Type type = instruction->InputAt(0)->GetType();
1766
1767 if (type == Primitive::kPrimFloat) {
1768 __ Vcmp(F32, InputSRegisterAt(instruction, 0), 0.0);
1769 } else {
1770 DCHECK_EQ(type, Primitive::kPrimDouble);
1771 __ Vcmp(F64, InputDRegisterAt(instruction, 0), 0.0);
1772 }
1773 } else {
1774 __ Vcmp(InputVRegisterAt(instruction, 0), InputVRegisterAt(instruction, 1));
1775 }
1776}
1777
Anton Kirilov5601d4e2017-05-11 19:33:50 +01001778static int64_t AdjustConstantForCondition(int64_t value,
1779 IfCondition* condition,
1780 IfCondition* opposite) {
1781 if (value == 1) {
1782 if (*condition == kCondB) {
1783 value = 0;
1784 *condition = kCondEQ;
1785 *opposite = kCondNE;
1786 } else if (*condition == kCondAE) {
1787 value = 0;
1788 *condition = kCondNE;
1789 *opposite = kCondEQ;
1790 }
1791 } else if (value == -1) {
1792 if (*condition == kCondGT) {
1793 value = 0;
1794 *condition = kCondGE;
1795 *opposite = kCondLT;
1796 } else if (*condition == kCondLE) {
1797 value = 0;
1798 *condition = kCondLT;
1799 *opposite = kCondGE;
1800 }
1801 }
1802
1803 return value;
1804}
1805
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001806static std::pair<vixl32::Condition, vixl32::Condition> GenerateLongTestConstant(
1807 HCondition* condition,
1808 bool invert,
1809 CodeGeneratorARMVIXL* codegen) {
Donghui Bai426b49c2016-11-08 14:55:38 +08001810 DCHECK_EQ(condition->GetLeft()->GetType(), Primitive::kPrimLong);
1811
1812 const LocationSummary* const locations = condition->GetLocations();
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001813 IfCondition cond = condition->GetCondition();
1814 IfCondition opposite = condition->GetOppositeCondition();
1815
1816 if (invert) {
1817 std::swap(cond, opposite);
1818 }
1819
1820 std::pair<vixl32::Condition, vixl32::Condition> ret(eq, ne);
Donghui Bai426b49c2016-11-08 14:55:38 +08001821 const Location left = locations->InAt(0);
1822 const Location right = locations->InAt(1);
1823
1824 DCHECK(right.IsConstant());
1825
1826 const vixl32::Register left_high = HighRegisterFrom(left);
1827 const vixl32::Register left_low = LowRegisterFrom(left);
Anton Kirilov5601d4e2017-05-11 19:33:50 +01001828 int64_t value = AdjustConstantForCondition(Int64ConstantFrom(right), &cond, &opposite);
1829 UseScratchRegisterScope temps(codegen->GetVIXLAssembler());
1830
1831 // Comparisons against 0 are common enough to deserve special attention.
1832 if (value == 0) {
1833 switch (cond) {
1834 case kCondNE:
1835 // x > 0 iff x != 0 when the comparison is unsigned.
1836 case kCondA:
1837 ret = std::make_pair(ne, eq);
1838 FALLTHROUGH_INTENDED;
1839 case kCondEQ:
1840 // x <= 0 iff x == 0 when the comparison is unsigned.
1841 case kCondBE:
1842 __ Orrs(temps.Acquire(), left_low, left_high);
1843 return ret;
1844 case kCondLT:
1845 case kCondGE:
1846 __ Cmp(left_high, 0);
1847 return std::make_pair(ARMCondition(cond), ARMCondition(opposite));
1848 // Trivially true or false.
1849 case kCondB:
1850 ret = std::make_pair(ne, eq);
1851 FALLTHROUGH_INTENDED;
1852 case kCondAE:
1853 __ Cmp(left_low, left_low);
1854 return ret;
1855 default:
1856 break;
1857 }
1858 }
Donghui Bai426b49c2016-11-08 14:55:38 +08001859
1860 switch (cond) {
1861 case kCondEQ:
1862 case kCondNE:
1863 case kCondB:
1864 case kCondBE:
1865 case kCondA:
1866 case kCondAE: {
1867 __ Cmp(left_high, High32Bits(value));
1868
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001869 // We use the scope because of the IT block that follows.
Donghui Bai426b49c2016-11-08 14:55:38 +08001870 ExactAssemblyScope guard(codegen->GetVIXLAssembler(),
1871 2 * vixl32::k16BitT32InstructionSizeInBytes,
1872 CodeBufferCheckScope::kExactSize);
1873
1874 __ it(eq);
1875 __ cmp(eq, left_low, Low32Bits(value));
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001876 ret = std::make_pair(ARMUnsignedCondition(cond), ARMUnsignedCondition(opposite));
Donghui Bai426b49c2016-11-08 14:55:38 +08001877 break;
1878 }
1879 case kCondLE:
1880 case kCondGT:
1881 // Trivially true or false.
1882 if (value == std::numeric_limits<int64_t>::max()) {
1883 __ Cmp(left_low, left_low);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001884 ret = cond == kCondLE ? std::make_pair(eq, ne) : std::make_pair(ne, eq);
Donghui Bai426b49c2016-11-08 14:55:38 +08001885 break;
1886 }
1887
1888 if (cond == kCondLE) {
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001889 DCHECK_EQ(opposite, kCondGT);
Donghui Bai426b49c2016-11-08 14:55:38 +08001890 cond = kCondLT;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001891 opposite = kCondGE;
Donghui Bai426b49c2016-11-08 14:55:38 +08001892 } else {
1893 DCHECK_EQ(cond, kCondGT);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001894 DCHECK_EQ(opposite, kCondLE);
Donghui Bai426b49c2016-11-08 14:55:38 +08001895 cond = kCondGE;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001896 opposite = kCondLT;
Donghui Bai426b49c2016-11-08 14:55:38 +08001897 }
1898
1899 value++;
1900 FALLTHROUGH_INTENDED;
1901 case kCondGE:
1902 case kCondLT: {
Donghui Bai426b49c2016-11-08 14:55:38 +08001903 __ Cmp(left_low, Low32Bits(value));
1904 __ Sbcs(temps.Acquire(), left_high, High32Bits(value));
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001905 ret = std::make_pair(ARMCondition(cond), ARMCondition(opposite));
Donghui Bai426b49c2016-11-08 14:55:38 +08001906 break;
1907 }
1908 default:
1909 LOG(FATAL) << "Unreachable";
1910 UNREACHABLE();
1911 }
1912
1913 return ret;
1914}
1915
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001916static std::pair<vixl32::Condition, vixl32::Condition> GenerateLongTest(
1917 HCondition* condition,
1918 bool invert,
1919 CodeGeneratorARMVIXL* codegen) {
Donghui Bai426b49c2016-11-08 14:55:38 +08001920 DCHECK_EQ(condition->GetLeft()->GetType(), Primitive::kPrimLong);
1921
1922 const LocationSummary* const locations = condition->GetLocations();
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001923 IfCondition cond = condition->GetCondition();
1924 IfCondition opposite = condition->GetOppositeCondition();
1925
1926 if (invert) {
1927 std::swap(cond, opposite);
1928 }
1929
1930 std::pair<vixl32::Condition, vixl32::Condition> ret(eq, ne);
Donghui Bai426b49c2016-11-08 14:55:38 +08001931 Location left = locations->InAt(0);
1932 Location right = locations->InAt(1);
1933
1934 DCHECK(right.IsRegisterPair());
1935
1936 switch (cond) {
1937 case kCondEQ:
1938 case kCondNE:
1939 case kCondB:
1940 case kCondBE:
1941 case kCondA:
1942 case kCondAE: {
1943 __ Cmp(HighRegisterFrom(left), HighRegisterFrom(right));
1944
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001945 // We use the scope because of the IT block that follows.
Donghui Bai426b49c2016-11-08 14:55:38 +08001946 ExactAssemblyScope guard(codegen->GetVIXLAssembler(),
1947 2 * vixl32::k16BitT32InstructionSizeInBytes,
1948 CodeBufferCheckScope::kExactSize);
1949
1950 __ it(eq);
1951 __ cmp(eq, LowRegisterFrom(left), LowRegisterFrom(right));
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001952 ret = std::make_pair(ARMUnsignedCondition(cond), ARMUnsignedCondition(opposite));
Donghui Bai426b49c2016-11-08 14:55:38 +08001953 break;
1954 }
1955 case kCondLE:
1956 case kCondGT:
1957 if (cond == kCondLE) {
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001958 DCHECK_EQ(opposite, kCondGT);
Donghui Bai426b49c2016-11-08 14:55:38 +08001959 cond = kCondGE;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001960 opposite = kCondLT;
Donghui Bai426b49c2016-11-08 14:55:38 +08001961 } else {
1962 DCHECK_EQ(cond, kCondGT);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001963 DCHECK_EQ(opposite, kCondLE);
Donghui Bai426b49c2016-11-08 14:55:38 +08001964 cond = kCondLT;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001965 opposite = kCondGE;
Donghui Bai426b49c2016-11-08 14:55:38 +08001966 }
1967
1968 std::swap(left, right);
1969 FALLTHROUGH_INTENDED;
1970 case kCondGE:
1971 case kCondLT: {
1972 UseScratchRegisterScope temps(codegen->GetVIXLAssembler());
1973
1974 __ Cmp(LowRegisterFrom(left), LowRegisterFrom(right));
1975 __ Sbcs(temps.Acquire(), HighRegisterFrom(left), HighRegisterFrom(right));
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001976 ret = std::make_pair(ARMCondition(cond), ARMCondition(opposite));
Donghui Bai426b49c2016-11-08 14:55:38 +08001977 break;
1978 }
1979 default:
1980 LOG(FATAL) << "Unreachable";
1981 UNREACHABLE();
1982 }
1983
1984 return ret;
1985}
1986
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001987static std::pair<vixl32::Condition, vixl32::Condition> GenerateTest(HCondition* condition,
1988 bool invert,
1989 CodeGeneratorARMVIXL* codegen) {
1990 const Primitive::Type type = condition->GetLeft()->GetType();
1991 IfCondition cond = condition->GetCondition();
1992 IfCondition opposite = condition->GetOppositeCondition();
1993 std::pair<vixl32::Condition, vixl32::Condition> ret(eq, ne);
Donghui Bai426b49c2016-11-08 14:55:38 +08001994
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001995 if (invert) {
1996 std::swap(cond, opposite);
1997 }
Donghui Bai426b49c2016-11-08 14:55:38 +08001998
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001999 if (type == Primitive::kPrimLong) {
2000 ret = condition->GetLocations()->InAt(1).IsConstant()
2001 ? GenerateLongTestConstant(condition, invert, codegen)
2002 : GenerateLongTest(condition, invert, codegen);
2003 } else if (Primitive::IsFloatingPointType(type)) {
2004 GenerateVcmp(condition, codegen);
2005 __ Vmrs(RegisterOrAPSR_nzcv(kPcCode), FPSCR);
2006 ret = std::make_pair(ARMFPCondition(cond, condition->IsGtBias()),
2007 ARMFPCondition(opposite, condition->IsGtBias()));
Donghui Bai426b49c2016-11-08 14:55:38 +08002008 } else {
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002009 DCHECK(Primitive::IsIntegralType(type) || type == Primitive::kPrimNot) << type;
2010 __ Cmp(InputRegisterAt(condition, 0), InputOperandAt(condition, 1));
2011 ret = std::make_pair(ARMCondition(cond), ARMCondition(opposite));
Donghui Bai426b49c2016-11-08 14:55:38 +08002012 }
2013
2014 return ret;
2015}
2016
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002017static bool CanGenerateTest(HCondition* condition, ArmVIXLAssembler* assembler) {
2018 if (condition->GetLeft()->GetType() == Primitive::kPrimLong) {
2019 const LocationSummary* const locations = condition->GetLocations();
Donghui Bai426b49c2016-11-08 14:55:38 +08002020
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002021 if (locations->InAt(1).IsConstant()) {
Anton Kirilov5601d4e2017-05-11 19:33:50 +01002022 IfCondition c = condition->GetCondition();
2023 IfCondition opposite = condition->GetOppositeCondition();
2024 const int64_t value =
2025 AdjustConstantForCondition(Int64ConstantFrom(locations->InAt(1)), &c, &opposite);
Donghui Bai426b49c2016-11-08 14:55:38 +08002026
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002027 if (c < kCondLT || c > kCondGE) {
2028 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
2029 // we check that the least significant half of the first input to be compared
2030 // is in a low register (the other half is read outside an IT block), and
2031 // the constant fits in an 8-bit unsigned integer, so that a 16-bit CMP
Anton Kirilov5601d4e2017-05-11 19:33:50 +01002032 // encoding can be used; 0 is always handled, no matter what registers are
2033 // used by the first input.
2034 if (value != 0 &&
2035 (!LowRegisterFrom(locations->InAt(0)).IsLow() || !IsUint<8>(Low32Bits(value)))) {
Donghui Bai426b49c2016-11-08 14:55:38 +08002036 return false;
2037 }
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002038 // TODO(VIXL): The rest of the checks are there to keep the backend in sync with
2039 // the previous one, but are not strictly necessary.
2040 } else if (c == kCondLE || c == kCondGT) {
2041 if (value < std::numeric_limits<int64_t>::max() &&
2042 !assembler->ShifterOperandCanHold(SBC, High32Bits(value + 1), kCcSet)) {
2043 return false;
2044 }
2045 } else if (!assembler->ShifterOperandCanHold(SBC, High32Bits(value), kCcSet)) {
2046 return false;
Donghui Bai426b49c2016-11-08 14:55:38 +08002047 }
2048 }
2049 }
2050
2051 return true;
2052}
2053
Anton Kirilov5601d4e2017-05-11 19:33:50 +01002054static void GenerateConditionGeneric(HCondition* cond, CodeGeneratorARMVIXL* codegen) {
2055 DCHECK(CanGenerateTest(cond, codegen->GetAssembler()));
2056
2057 const vixl32::Register out = OutputRegister(cond);
2058 const auto condition = GenerateTest(cond, false, codegen);
2059
2060 __ Mov(LeaveFlags, out, 0);
2061
2062 if (out.IsLow()) {
2063 // We use the scope because of the IT block that follows.
2064 ExactAssemblyScope guard(codegen->GetVIXLAssembler(),
2065 2 * vixl32::k16BitT32InstructionSizeInBytes,
2066 CodeBufferCheckScope::kExactSize);
2067
2068 __ it(condition.first);
2069 __ mov(condition.first, out, 1);
2070 } else {
2071 vixl32::Label done_label;
2072 vixl32::Label* const final_label = codegen->GetFinalLabel(cond, &done_label);
2073
2074 __ B(condition.second, final_label, /* far_target */ false);
2075 __ Mov(out, 1);
2076
2077 if (done_label.IsReferenced()) {
2078 __ Bind(&done_label);
2079 }
2080 }
2081}
2082
2083static void GenerateEqualLong(HCondition* cond, CodeGeneratorARMVIXL* codegen) {
2084 DCHECK_EQ(cond->GetLeft()->GetType(), Primitive::kPrimLong);
2085
2086 const LocationSummary* const locations = cond->GetLocations();
2087 IfCondition condition = cond->GetCondition();
2088 const vixl32::Register out = OutputRegister(cond);
2089 const Location left = locations->InAt(0);
2090 const Location right = locations->InAt(1);
2091 vixl32::Register left_high = HighRegisterFrom(left);
2092 vixl32::Register left_low = LowRegisterFrom(left);
2093 vixl32::Register temp;
2094 UseScratchRegisterScope temps(codegen->GetVIXLAssembler());
2095
2096 if (right.IsConstant()) {
2097 IfCondition opposite = cond->GetOppositeCondition();
2098 const int64_t value = AdjustConstantForCondition(Int64ConstantFrom(right),
2099 &condition,
2100 &opposite);
2101 Operand right_high = High32Bits(value);
2102 Operand right_low = Low32Bits(value);
2103
2104 // The output uses Location::kNoOutputOverlap.
2105 if (out.Is(left_high)) {
2106 std::swap(left_low, left_high);
2107 std::swap(right_low, right_high);
2108 }
2109
2110 __ Sub(out, left_low, right_low);
2111 temp = temps.Acquire();
2112 __ Sub(temp, left_high, right_high);
2113 } else {
2114 DCHECK(right.IsRegisterPair());
2115 temp = temps.Acquire();
2116 __ Sub(temp, left_high, HighRegisterFrom(right));
2117 __ Sub(out, left_low, LowRegisterFrom(right));
2118 }
2119
2120 // Need to check after calling AdjustConstantForCondition().
2121 DCHECK(condition == kCondEQ || condition == kCondNE) << condition;
2122
2123 if (condition == kCondNE && out.IsLow()) {
2124 __ Orrs(out, out, temp);
2125
2126 // We use the scope because of the IT block that follows.
2127 ExactAssemblyScope guard(codegen->GetVIXLAssembler(),
2128 2 * vixl32::k16BitT32InstructionSizeInBytes,
2129 CodeBufferCheckScope::kExactSize);
2130
2131 __ it(ne);
2132 __ mov(ne, out, 1);
2133 } else {
2134 __ Orr(out, out, temp);
2135 codegen->GenerateConditionWithZero(condition, out, out, temp);
2136 }
2137}
2138
2139static void GenerateLongComparesAndJumps(HCondition* cond,
2140 vixl32::Label* true_label,
2141 vixl32::Label* false_label,
Anton Kirilovfd522532017-05-10 12:46:57 +01002142 CodeGeneratorARMVIXL* codegen,
2143 bool is_far_target = true) {
Anton Kirilov5601d4e2017-05-11 19:33:50 +01002144 LocationSummary* locations = cond->GetLocations();
2145 Location left = locations->InAt(0);
2146 Location right = locations->InAt(1);
2147 IfCondition if_cond = cond->GetCondition();
2148
2149 vixl32::Register left_high = HighRegisterFrom(left);
2150 vixl32::Register left_low = LowRegisterFrom(left);
2151 IfCondition true_high_cond = if_cond;
2152 IfCondition false_high_cond = cond->GetOppositeCondition();
2153 vixl32::Condition final_condition = ARMUnsignedCondition(if_cond); // unsigned on lower part
2154
2155 // Set the conditions for the test, remembering that == needs to be
2156 // decided using the low words.
2157 switch (if_cond) {
2158 case kCondEQ:
2159 case kCondNE:
2160 // Nothing to do.
2161 break;
2162 case kCondLT:
2163 false_high_cond = kCondGT;
2164 break;
2165 case kCondLE:
2166 true_high_cond = kCondLT;
2167 break;
2168 case kCondGT:
2169 false_high_cond = kCondLT;
2170 break;
2171 case kCondGE:
2172 true_high_cond = kCondGT;
2173 break;
2174 case kCondB:
2175 false_high_cond = kCondA;
2176 break;
2177 case kCondBE:
2178 true_high_cond = kCondB;
2179 break;
2180 case kCondA:
2181 false_high_cond = kCondB;
2182 break;
2183 case kCondAE:
2184 true_high_cond = kCondA;
2185 break;
2186 }
2187 if (right.IsConstant()) {
2188 int64_t value = Int64ConstantFrom(right);
2189 int32_t val_low = Low32Bits(value);
2190 int32_t val_high = High32Bits(value);
2191
2192 __ Cmp(left_high, val_high);
2193 if (if_cond == kCondNE) {
Anton Kirilovfd522532017-05-10 12:46:57 +01002194 __ B(ARMCondition(true_high_cond), true_label, is_far_target);
Anton Kirilov5601d4e2017-05-11 19:33:50 +01002195 } else if (if_cond == kCondEQ) {
Anton Kirilovfd522532017-05-10 12:46:57 +01002196 __ B(ARMCondition(false_high_cond), false_label, is_far_target);
Anton Kirilov5601d4e2017-05-11 19:33:50 +01002197 } else {
Anton Kirilovfd522532017-05-10 12:46:57 +01002198 __ B(ARMCondition(true_high_cond), true_label, is_far_target);
2199 __ B(ARMCondition(false_high_cond), false_label, is_far_target);
Anton Kirilov5601d4e2017-05-11 19:33:50 +01002200 }
2201 // Must be equal high, so compare the lows.
2202 __ Cmp(left_low, val_low);
2203 } else {
2204 vixl32::Register right_high = HighRegisterFrom(right);
2205 vixl32::Register right_low = LowRegisterFrom(right);
2206
2207 __ Cmp(left_high, right_high);
2208 if (if_cond == kCondNE) {
Anton Kirilovfd522532017-05-10 12:46:57 +01002209 __ B(ARMCondition(true_high_cond), true_label, is_far_target);
Anton Kirilov5601d4e2017-05-11 19:33:50 +01002210 } else if (if_cond == kCondEQ) {
Anton Kirilovfd522532017-05-10 12:46:57 +01002211 __ B(ARMCondition(false_high_cond), false_label, is_far_target);
Anton Kirilov5601d4e2017-05-11 19:33:50 +01002212 } else {
Anton Kirilovfd522532017-05-10 12:46:57 +01002213 __ B(ARMCondition(true_high_cond), true_label, is_far_target);
2214 __ B(ARMCondition(false_high_cond), false_label, is_far_target);
Anton Kirilov5601d4e2017-05-11 19:33:50 +01002215 }
2216 // Must be equal high, so compare the lows.
2217 __ Cmp(left_low, right_low);
2218 }
2219 // The last comparison might be unsigned.
2220 // TODO: optimize cases where this is always true/false
Anton Kirilovfd522532017-05-10 12:46:57 +01002221 __ B(final_condition, true_label, is_far_target);
Anton Kirilov5601d4e2017-05-11 19:33:50 +01002222}
2223
2224static void GenerateConditionLong(HCondition* cond, CodeGeneratorARMVIXL* codegen) {
2225 DCHECK_EQ(cond->GetLeft()->GetType(), Primitive::kPrimLong);
2226
2227 const LocationSummary* const locations = cond->GetLocations();
2228 IfCondition condition = cond->GetCondition();
2229 const vixl32::Register out = OutputRegister(cond);
2230 const Location left = locations->InAt(0);
2231 const Location right = locations->InAt(1);
2232
2233 if (right.IsConstant()) {
2234 IfCondition opposite = cond->GetOppositeCondition();
2235
2236 // Comparisons against 0 are common enough to deserve special attention.
2237 if (AdjustConstantForCondition(Int64ConstantFrom(right), &condition, &opposite) == 0) {
2238 switch (condition) {
2239 case kCondNE:
2240 case kCondA:
2241 if (out.IsLow()) {
2242 // We only care if both input registers are 0 or not.
2243 __ Orrs(out, LowRegisterFrom(left), HighRegisterFrom(left));
2244
2245 // We use the scope because of the IT block that follows.
2246 ExactAssemblyScope guard(codegen->GetVIXLAssembler(),
2247 2 * vixl32::k16BitT32InstructionSizeInBytes,
2248 CodeBufferCheckScope::kExactSize);
2249
2250 __ it(ne);
2251 __ mov(ne, out, 1);
2252 return;
2253 }
2254
2255 FALLTHROUGH_INTENDED;
2256 case kCondEQ:
2257 case kCondBE:
2258 // We only care if both input registers are 0 or not.
2259 __ Orr(out, LowRegisterFrom(left), HighRegisterFrom(left));
2260 codegen->GenerateConditionWithZero(condition, out, out);
2261 return;
2262 case kCondLT:
2263 case kCondGE:
2264 // We only care about the sign bit.
2265 FALLTHROUGH_INTENDED;
2266 case kCondAE:
2267 case kCondB:
2268 codegen->GenerateConditionWithZero(condition, out, HighRegisterFrom(left));
2269 return;
2270 case kCondLE:
2271 case kCondGT:
2272 default:
2273 break;
2274 }
2275 }
2276 }
2277
2278 if ((condition == kCondEQ || condition == kCondNE) &&
2279 // If `out` is a low register, then the GenerateConditionGeneric()
2280 // function generates a shorter code sequence that is still branchless.
2281 (!out.IsLow() || !CanGenerateTest(cond, codegen->GetAssembler()))) {
2282 GenerateEqualLong(cond, codegen);
2283 return;
2284 }
2285
2286 if (CanGenerateTest(cond, codegen->GetAssembler())) {
2287 GenerateConditionGeneric(cond, codegen);
2288 return;
2289 }
2290
2291 // Convert the jumps into the result.
2292 vixl32::Label done_label;
2293 vixl32::Label* const final_label = codegen->GetFinalLabel(cond, &done_label);
2294 vixl32::Label true_label, false_label;
2295
Anton Kirilovfd522532017-05-10 12:46:57 +01002296 GenerateLongComparesAndJumps(cond, &true_label, &false_label, codegen, /* is_far_target */ false);
Anton Kirilov5601d4e2017-05-11 19:33:50 +01002297
2298 // False case: result = 0.
2299 __ Bind(&false_label);
2300 __ Mov(out, 0);
2301 __ B(final_label);
2302
2303 // True case: result = 1.
2304 __ Bind(&true_label);
2305 __ Mov(out, 1);
2306
2307 if (done_label.IsReferenced()) {
2308 __ Bind(&done_label);
2309 }
2310}
2311
2312static void GenerateConditionIntegralOrNonPrimitive(HCondition* cond, CodeGeneratorARMVIXL* codegen) {
2313 const Primitive::Type type = cond->GetLeft()->GetType();
2314
2315 DCHECK(Primitive::IsIntegralType(type) || type == Primitive::kPrimNot) << type;
2316
2317 if (type == Primitive::kPrimLong) {
2318 GenerateConditionLong(cond, codegen);
2319 return;
2320 }
2321
2322 IfCondition condition = cond->GetCondition();
2323 vixl32::Register in = InputRegisterAt(cond, 0);
2324 const vixl32::Register out = OutputRegister(cond);
2325 const Location right = cond->GetLocations()->InAt(1);
2326 int64_t value;
2327
2328 if (right.IsConstant()) {
2329 IfCondition opposite = cond->GetOppositeCondition();
2330
2331 value = AdjustConstantForCondition(Int64ConstantFrom(right), &condition, &opposite);
2332
2333 // Comparisons against 0 are common enough to deserve special attention.
2334 if (value == 0) {
2335 switch (condition) {
2336 case kCondNE:
2337 case kCondA:
2338 if (out.IsLow() && out.Is(in)) {
2339 __ Cmp(out, 0);
2340
2341 // We use the scope because of the IT block that follows.
2342 ExactAssemblyScope guard(codegen->GetVIXLAssembler(),
2343 2 * vixl32::k16BitT32InstructionSizeInBytes,
2344 CodeBufferCheckScope::kExactSize);
2345
2346 __ it(ne);
2347 __ mov(ne, out, 1);
2348 return;
2349 }
2350
2351 FALLTHROUGH_INTENDED;
2352 case kCondEQ:
2353 case kCondBE:
2354 case kCondLT:
2355 case kCondGE:
2356 case kCondAE:
2357 case kCondB:
2358 codegen->GenerateConditionWithZero(condition, out, in);
2359 return;
2360 case kCondLE:
2361 case kCondGT:
2362 default:
2363 break;
2364 }
2365 }
2366 }
2367
2368 if (condition == kCondEQ || condition == kCondNE) {
2369 Operand operand(0);
2370
2371 if (right.IsConstant()) {
2372 operand = Operand::From(value);
2373 } else if (out.Is(RegisterFrom(right))) {
2374 // Avoid 32-bit instructions if possible.
2375 operand = InputOperandAt(cond, 0);
2376 in = RegisterFrom(right);
2377 } else {
2378 operand = InputOperandAt(cond, 1);
2379 }
2380
2381 if (condition == kCondNE && out.IsLow()) {
2382 __ Subs(out, in, operand);
2383
2384 // We use the scope because of the IT block that follows.
2385 ExactAssemblyScope guard(codegen->GetVIXLAssembler(),
2386 2 * vixl32::k16BitT32InstructionSizeInBytes,
2387 CodeBufferCheckScope::kExactSize);
2388
2389 __ it(ne);
2390 __ mov(ne, out, 1);
2391 } else {
2392 __ Sub(out, in, operand);
2393 codegen->GenerateConditionWithZero(condition, out, out);
2394 }
2395
2396 return;
2397 }
2398
2399 GenerateConditionGeneric(cond, codegen);
2400}
2401
Donghui Bai426b49c2016-11-08 14:55:38 +08002402static bool CanEncodeConstantAs8BitImmediate(HConstant* constant) {
2403 const Primitive::Type type = constant->GetType();
2404 bool ret = false;
2405
2406 DCHECK(Primitive::IsIntegralType(type) || type == Primitive::kPrimNot) << type;
2407
2408 if (type == Primitive::kPrimLong) {
2409 const uint64_t value = Uint64ConstantFrom(constant);
2410
2411 ret = IsUint<8>(Low32Bits(value)) && IsUint<8>(High32Bits(value));
2412 } else {
2413 ret = IsUint<8>(Int32ConstantFrom(constant));
2414 }
2415
2416 return ret;
2417}
2418
2419static Location Arm8BitEncodableConstantOrRegister(HInstruction* constant) {
2420 DCHECK(!Primitive::IsFloatingPointType(constant->GetType()));
2421
2422 if (constant->IsConstant() && CanEncodeConstantAs8BitImmediate(constant->AsConstant())) {
2423 return Location::ConstantLocation(constant->AsConstant());
2424 }
2425
2426 return Location::RequiresRegister();
2427}
2428
2429static bool CanGenerateConditionalMove(const Location& out, const Location& src) {
2430 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
2431 // we check that we are not dealing with floating-point output (there is no
2432 // 16-bit VMOV encoding).
2433 if (!out.IsRegister() && !out.IsRegisterPair()) {
2434 return false;
2435 }
2436
2437 // For constants, we also check that the output is in one or two low registers,
2438 // and that the constants fit in an 8-bit unsigned integer, so that a 16-bit
2439 // MOV encoding can be used.
2440 if (src.IsConstant()) {
2441 if (!CanEncodeConstantAs8BitImmediate(src.GetConstant())) {
2442 return false;
2443 }
2444
2445 if (out.IsRegister()) {
2446 if (!RegisterFrom(out).IsLow()) {
2447 return false;
2448 }
2449 } else {
2450 DCHECK(out.IsRegisterPair());
2451
2452 if (!HighRegisterFrom(out).IsLow()) {
2453 return false;
2454 }
2455 }
2456 }
2457
2458 return true;
2459}
2460
Scott Wakelingfe885462016-09-22 10:24:38 +01002461#undef __
2462
Donghui Bai426b49c2016-11-08 14:55:38 +08002463vixl32::Label* CodeGeneratorARMVIXL::GetFinalLabel(HInstruction* instruction,
2464 vixl32::Label* final_label) {
2465 DCHECK(!instruction->IsControlFlow() && !instruction->IsSuspendCheck());
Anton Kirilov6f644202017-02-27 18:29:45 +00002466 DCHECK(!instruction->IsInvoke() || !instruction->GetLocations()->CanCall());
Donghui Bai426b49c2016-11-08 14:55:38 +08002467
2468 const HBasicBlock* const block = instruction->GetBlock();
2469 const HLoopInformation* const info = block->GetLoopInformation();
2470 HInstruction* const next = instruction->GetNext();
2471
2472 // Avoid a branch to a branch.
2473 if (next->IsGoto() && (info == nullptr ||
2474 !info->IsBackEdge(*block) ||
2475 !info->HasSuspendCheck())) {
2476 final_label = GetLabelOf(next->AsGoto()->GetSuccessor());
2477 }
2478
2479 return final_label;
2480}
2481
Scott Wakelingfe885462016-09-22 10:24:38 +01002482CodeGeneratorARMVIXL::CodeGeneratorARMVIXL(HGraph* graph,
2483 const ArmInstructionSetFeatures& isa_features,
2484 const CompilerOptions& compiler_options,
2485 OptimizingCompilerStats* stats)
2486 : CodeGenerator(graph,
2487 kNumberOfCoreRegisters,
2488 kNumberOfSRegisters,
2489 kNumberOfRegisterPairs,
2490 kCoreCalleeSaves.GetList(),
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002491 ComputeSRegisterListMask(kFpuCalleeSaves),
Scott Wakelingfe885462016-09-22 10:24:38 +01002492 compiler_options,
2493 stats),
2494 block_labels_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Artem Serov551b28f2016-10-18 19:11:30 +01002495 jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Scott Wakelingfe885462016-09-22 10:24:38 +01002496 location_builder_(graph, this),
2497 instruction_visitor_(graph, this),
2498 move_resolver_(graph->GetArena(), this),
2499 assembler_(graph->GetArena()),
Artem Serovd4cc5b22016-11-04 11:19:09 +00002500 isa_features_(isa_features),
Artem Serovc5fcb442016-12-02 19:19:58 +00002501 uint32_literals_(std::less<uint32_t>(),
2502 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Artem Serovd4cc5b22016-11-04 11:19:09 +00002503 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko65979462017-05-19 17:25:12 +01002504 pc_relative_method_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Artem Serovc5fcb442016-12-02 19:19:58 +00002505 pc_relative_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko1998cd02017-01-13 13:02:58 +00002506 type_bss_entry_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko65979462017-05-19 17:25:12 +01002507 pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01002508 baker_read_barrier_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Artem Serovc5fcb442016-12-02 19:19:58 +00002509 jit_string_patches_(StringReferenceValueComparator(),
2510 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
2511 jit_class_patches_(TypeReferenceValueComparator(),
2512 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Scott Wakelingfe885462016-09-22 10:24:38 +01002513 // Always save the LR register to mimic Quick.
2514 AddAllocatedRegister(Location::RegisterLocation(LR));
Nicolas Geoffray13a797b2017-03-15 16:41:31 +00002515 // Give D30 and D31 as scratch register to VIXL. The register allocator only works on
2516 // S0-S31, which alias to D0-D15.
2517 GetVIXLAssembler()->GetScratchVRegisterList()->Combine(d31);
2518 GetVIXLAssembler()->GetScratchVRegisterList()->Combine(d30);
Scott Wakelingfe885462016-09-22 10:24:38 +01002519}
2520
Artem Serov551b28f2016-10-18 19:11:30 +01002521void JumpTableARMVIXL::EmitTable(CodeGeneratorARMVIXL* codegen) {
2522 uint32_t num_entries = switch_instr_->GetNumEntries();
2523 DCHECK_GE(num_entries, kPackedSwitchCompareJumpThreshold);
2524
2525 // We are about to use the assembler to place literals directly. Make sure we have enough
Scott Wakelingb77051e2016-11-21 19:46:00 +00002526 // underlying code buffer and we have generated a jump table of the right size, using
2527 // codegen->GetVIXLAssembler()->GetBuffer().Align();
Artem Serov0fb37192016-12-06 18:13:40 +00002528 ExactAssemblyScope aas(codegen->GetVIXLAssembler(),
2529 num_entries * sizeof(int32_t),
2530 CodeBufferCheckScope::kMaximumSize);
Artem Serov551b28f2016-10-18 19:11:30 +01002531 // TODO(VIXL): Check that using lower case bind is fine here.
2532 codegen->GetVIXLAssembler()->bind(&table_start_);
Artem Serov09a940d2016-11-11 16:15:11 +00002533 for (uint32_t i = 0; i < num_entries; i++) {
2534 codegen->GetVIXLAssembler()->place(bb_addresses_[i].get());
2535 }
2536}
2537
2538void JumpTableARMVIXL::FixTable(CodeGeneratorARMVIXL* codegen) {
2539 uint32_t num_entries = switch_instr_->GetNumEntries();
2540 DCHECK_GE(num_entries, kPackedSwitchCompareJumpThreshold);
2541
Artem Serov551b28f2016-10-18 19:11:30 +01002542 const ArenaVector<HBasicBlock*>& successors = switch_instr_->GetBlock()->GetSuccessors();
2543 for (uint32_t i = 0; i < num_entries; i++) {
2544 vixl32::Label* target_label = codegen->GetLabelOf(successors[i]);
2545 DCHECK(target_label->IsBound());
2546 int32_t jump_offset = target_label->GetLocation() - table_start_.GetLocation();
2547 // When doing BX to address we need to have lower bit set to 1 in T32.
2548 if (codegen->GetVIXLAssembler()->IsUsingT32()) {
2549 jump_offset++;
2550 }
2551 DCHECK_GT(jump_offset, std::numeric_limits<int32_t>::min());
2552 DCHECK_LE(jump_offset, std::numeric_limits<int32_t>::max());
Artem Serov09a940d2016-11-11 16:15:11 +00002553
Scott Wakelingb77051e2016-11-21 19:46:00 +00002554 bb_addresses_[i].get()->UpdateValue(jump_offset, codegen->GetVIXLAssembler()->GetBuffer());
Artem Serov551b28f2016-10-18 19:11:30 +01002555 }
2556}
2557
Artem Serov09a940d2016-11-11 16:15:11 +00002558void CodeGeneratorARMVIXL::FixJumpTables() {
Artem Serov551b28f2016-10-18 19:11:30 +01002559 for (auto&& jump_table : jump_tables_) {
Artem Serov09a940d2016-11-11 16:15:11 +00002560 jump_table->FixTable(this);
Artem Serov551b28f2016-10-18 19:11:30 +01002561 }
2562}
2563
Andreas Gampeca620d72016-11-08 08:09:33 -08002564#define __ reinterpret_cast<ArmVIXLAssembler*>(GetAssembler())->GetVIXLAssembler()-> // NOLINT
Scott Wakelingfe885462016-09-22 10:24:38 +01002565
2566void CodeGeneratorARMVIXL::Finalize(CodeAllocator* allocator) {
Artem Serov09a940d2016-11-11 16:15:11 +00002567 FixJumpTables();
Scott Wakelingfe885462016-09-22 10:24:38 +01002568 GetAssembler()->FinalizeCode();
2569 CodeGenerator::Finalize(allocator);
2570}
2571
2572void CodeGeneratorARMVIXL::SetupBlockedRegisters() const {
Scott Wakelingfe885462016-09-22 10:24:38 +01002573 // Stack register, LR and PC are always reserved.
2574 blocked_core_registers_[SP] = true;
2575 blocked_core_registers_[LR] = true;
2576 blocked_core_registers_[PC] = true;
2577
2578 // Reserve thread register.
2579 blocked_core_registers_[TR] = true;
2580
2581 // Reserve temp register.
2582 blocked_core_registers_[IP] = true;
2583
2584 if (GetGraph()->IsDebuggable()) {
2585 // Stubs do not save callee-save floating point registers. If the graph
2586 // is debuggable, we need to deal with these registers differently. For
2587 // now, just block them.
2588 for (uint32_t i = kFpuCalleeSaves.GetFirstSRegister().GetCode();
2589 i <= kFpuCalleeSaves.GetLastSRegister().GetCode();
2590 ++i) {
2591 blocked_fpu_registers_[i] = true;
2592 }
2593 }
Scott Wakelingfe885462016-09-22 10:24:38 +01002594}
2595
Scott Wakelingfe885462016-09-22 10:24:38 +01002596InstructionCodeGeneratorARMVIXL::InstructionCodeGeneratorARMVIXL(HGraph* graph,
2597 CodeGeneratorARMVIXL* codegen)
2598 : InstructionCodeGenerator(graph, codegen),
2599 assembler_(codegen->GetAssembler()),
2600 codegen_(codegen) {}
2601
2602void CodeGeneratorARMVIXL::ComputeSpillMask() {
2603 core_spill_mask_ = allocated_registers_.GetCoreRegisters() & core_callee_save_mask_;
2604 DCHECK_NE(core_spill_mask_, 0u) << "At least the return address register must be saved";
2605 // There is no easy instruction to restore just the PC on thumb2. We spill and
2606 // restore another arbitrary register.
2607 core_spill_mask_ |= (1 << kCoreAlwaysSpillRegister.GetCode());
2608 fpu_spill_mask_ = allocated_registers_.GetFloatingPointRegisters() & fpu_callee_save_mask_;
2609 // We use vpush and vpop for saving and restoring floating point registers, which take
2610 // a SRegister and the number of registers to save/restore after that SRegister. We
2611 // therefore update the `fpu_spill_mask_` to also contain those registers not allocated,
2612 // but in the range.
2613 if (fpu_spill_mask_ != 0) {
2614 uint32_t least_significant_bit = LeastSignificantBit(fpu_spill_mask_);
2615 uint32_t most_significant_bit = MostSignificantBit(fpu_spill_mask_);
2616 for (uint32_t i = least_significant_bit + 1 ; i < most_significant_bit; ++i) {
2617 fpu_spill_mask_ |= (1 << i);
2618 }
2619 }
2620}
2621
2622void CodeGeneratorARMVIXL::GenerateFrameEntry() {
2623 bool skip_overflow_check =
2624 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kArm);
2625 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
2626 __ Bind(&frame_entry_label_);
2627
2628 if (HasEmptyFrame()) {
2629 return;
2630 }
2631
Scott Wakelingfe885462016-09-22 10:24:38 +01002632 if (!skip_overflow_check) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002633 UseScratchRegisterScope temps(GetVIXLAssembler());
2634 vixl32::Register temp = temps.Acquire();
Anton Kirilov644032c2016-12-06 17:51:43 +00002635 __ Sub(temp, sp, Operand::From(GetStackOverflowReservedBytes(kArm)));
Scott Wakelingfe885462016-09-22 10:24:38 +01002636 // The load must immediately precede RecordPcInfo.
Artem Serov0fb37192016-12-06 18:13:40 +00002637 ExactAssemblyScope aas(GetVIXLAssembler(),
2638 vixl32::kMaxInstructionSizeInBytes,
2639 CodeBufferCheckScope::kMaximumSize);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002640 __ ldr(temp, MemOperand(temp));
2641 RecordPcInfo(nullptr, 0);
Scott Wakelingfe885462016-09-22 10:24:38 +01002642 }
2643
2644 __ Push(RegisterList(core_spill_mask_));
2645 GetAssembler()->cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(core_spill_mask_));
2646 GetAssembler()->cfi().RelOffsetForMany(DWARFReg(kMethodRegister),
2647 0,
2648 core_spill_mask_,
2649 kArmWordSize);
2650 if (fpu_spill_mask_ != 0) {
2651 uint32_t first = LeastSignificantBit(fpu_spill_mask_);
2652
2653 // Check that list is contiguous.
2654 DCHECK_EQ(fpu_spill_mask_ >> CTZ(fpu_spill_mask_), ~0u >> (32 - POPCOUNT(fpu_spill_mask_)));
2655
2656 __ Vpush(SRegisterList(vixl32::SRegister(first), POPCOUNT(fpu_spill_mask_)));
2657 GetAssembler()->cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(fpu_spill_mask_));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002658 GetAssembler()->cfi().RelOffsetForMany(DWARFReg(s0), 0, fpu_spill_mask_, kArmWordSize);
Scott Wakelingfe885462016-09-22 10:24:38 +01002659 }
Scott Wakelingbffdc702016-12-07 17:46:03 +00002660
2661 if (GetGraph()->HasShouldDeoptimizeFlag()) {
2662 UseScratchRegisterScope temps(GetVIXLAssembler());
2663 vixl32::Register temp = temps.Acquire();
2664 // Initialize should_deoptimize flag to 0.
2665 __ Mov(temp, 0);
2666 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, -kShouldDeoptimizeFlagSize);
2667 }
2668
Scott Wakelingfe885462016-09-22 10:24:38 +01002669 int adjust = GetFrameSize() - FrameEntrySpillSize();
2670 __ Sub(sp, sp, adjust);
2671 GetAssembler()->cfi().AdjustCFAOffset(adjust);
Scott Wakelingbffdc702016-12-07 17:46:03 +00002672
2673 // Save the current method if we need it. Note that we do not
2674 // do this in HCurrentMethod, as the instruction might have been removed
2675 // in the SSA graph.
2676 if (RequiresCurrentMethod()) {
2677 GetAssembler()->StoreToOffset(kStoreWord, kMethodRegister, sp, 0);
2678 }
Scott Wakelingfe885462016-09-22 10:24:38 +01002679}
2680
2681void CodeGeneratorARMVIXL::GenerateFrameExit() {
2682 if (HasEmptyFrame()) {
2683 __ Bx(lr);
2684 return;
2685 }
2686 GetAssembler()->cfi().RememberState();
2687 int adjust = GetFrameSize() - FrameEntrySpillSize();
2688 __ Add(sp, sp, adjust);
2689 GetAssembler()->cfi().AdjustCFAOffset(-adjust);
2690 if (fpu_spill_mask_ != 0) {
2691 uint32_t first = LeastSignificantBit(fpu_spill_mask_);
2692
2693 // Check that list is contiguous.
2694 DCHECK_EQ(fpu_spill_mask_ >> CTZ(fpu_spill_mask_), ~0u >> (32 - POPCOUNT(fpu_spill_mask_)));
2695
2696 __ Vpop(SRegisterList(vixl32::SRegister(first), POPCOUNT(fpu_spill_mask_)));
2697 GetAssembler()->cfi().AdjustCFAOffset(
2698 -static_cast<int>(kArmWordSize) * POPCOUNT(fpu_spill_mask_));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002699 GetAssembler()->cfi().RestoreMany(DWARFReg(vixl32::SRegister(0)), fpu_spill_mask_);
Scott Wakelingfe885462016-09-22 10:24:38 +01002700 }
2701 // Pop LR into PC to return.
2702 DCHECK_NE(core_spill_mask_ & (1 << kLrCode), 0U);
2703 uint32_t pop_mask = (core_spill_mask_ & (~(1 << kLrCode))) | 1 << kPcCode;
2704 __ Pop(RegisterList(pop_mask));
2705 GetAssembler()->cfi().RestoreState();
2706 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
2707}
2708
2709void CodeGeneratorARMVIXL::Bind(HBasicBlock* block) {
2710 __ Bind(GetLabelOf(block));
2711}
2712
Artem Serovd4cc5b22016-11-04 11:19:09 +00002713Location InvokeDexCallingConventionVisitorARMVIXL::GetNextLocation(Primitive::Type type) {
2714 switch (type) {
2715 case Primitive::kPrimBoolean:
2716 case Primitive::kPrimByte:
2717 case Primitive::kPrimChar:
2718 case Primitive::kPrimShort:
2719 case Primitive::kPrimInt:
2720 case Primitive::kPrimNot: {
2721 uint32_t index = gp_index_++;
2722 uint32_t stack_index = stack_index_++;
2723 if (index < calling_convention.GetNumberOfRegisters()) {
2724 return LocationFrom(calling_convention.GetRegisterAt(index));
2725 } else {
2726 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
2727 }
2728 }
2729
2730 case Primitive::kPrimLong: {
2731 uint32_t index = gp_index_;
2732 uint32_t stack_index = stack_index_;
2733 gp_index_ += 2;
2734 stack_index_ += 2;
2735 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
2736 if (calling_convention.GetRegisterAt(index).Is(r1)) {
2737 // Skip R1, and use R2_R3 instead.
2738 gp_index_++;
2739 index++;
2740 }
2741 }
2742 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
2743 DCHECK_EQ(calling_convention.GetRegisterAt(index).GetCode() + 1,
2744 calling_convention.GetRegisterAt(index + 1).GetCode());
2745
2746 return LocationFrom(calling_convention.GetRegisterAt(index),
2747 calling_convention.GetRegisterAt(index + 1));
2748 } else {
2749 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
2750 }
2751 }
2752
2753 case Primitive::kPrimFloat: {
2754 uint32_t stack_index = stack_index_++;
2755 if (float_index_ % 2 == 0) {
2756 float_index_ = std::max(double_index_, float_index_);
2757 }
2758 if (float_index_ < calling_convention.GetNumberOfFpuRegisters()) {
2759 return LocationFrom(calling_convention.GetFpuRegisterAt(float_index_++));
2760 } else {
2761 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
2762 }
2763 }
2764
2765 case Primitive::kPrimDouble: {
2766 double_index_ = std::max(double_index_, RoundUp(float_index_, 2));
2767 uint32_t stack_index = stack_index_;
2768 stack_index_ += 2;
2769 if (double_index_ + 1 < calling_convention.GetNumberOfFpuRegisters()) {
2770 uint32_t index = double_index_;
2771 double_index_ += 2;
2772 Location result = LocationFrom(
2773 calling_convention.GetFpuRegisterAt(index),
2774 calling_convention.GetFpuRegisterAt(index + 1));
2775 DCHECK(ExpectedPairLayout(result));
2776 return result;
2777 } else {
2778 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
2779 }
2780 }
2781
2782 case Primitive::kPrimVoid:
2783 LOG(FATAL) << "Unexpected parameter type " << type;
2784 break;
2785 }
2786 return Location::NoLocation();
2787}
2788
2789Location InvokeDexCallingConventionVisitorARMVIXL::GetReturnLocation(Primitive::Type type) const {
2790 switch (type) {
2791 case Primitive::kPrimBoolean:
2792 case Primitive::kPrimByte:
2793 case Primitive::kPrimChar:
2794 case Primitive::kPrimShort:
2795 case Primitive::kPrimInt:
2796 case Primitive::kPrimNot: {
2797 return LocationFrom(r0);
2798 }
2799
2800 case Primitive::kPrimFloat: {
2801 return LocationFrom(s0);
2802 }
2803
2804 case Primitive::kPrimLong: {
2805 return LocationFrom(r0, r1);
2806 }
2807
2808 case Primitive::kPrimDouble: {
2809 return LocationFrom(s0, s1);
2810 }
2811
2812 case Primitive::kPrimVoid:
2813 return Location::NoLocation();
2814 }
2815
2816 UNREACHABLE();
2817}
2818
2819Location InvokeDexCallingConventionVisitorARMVIXL::GetMethodLocation() const {
2820 return LocationFrom(kMethodRegister);
2821}
2822
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002823void CodeGeneratorARMVIXL::Move32(Location destination, Location source) {
2824 if (source.Equals(destination)) {
2825 return;
2826 }
2827 if (destination.IsRegister()) {
2828 if (source.IsRegister()) {
2829 __ Mov(RegisterFrom(destination), RegisterFrom(source));
2830 } else if (source.IsFpuRegister()) {
2831 __ Vmov(RegisterFrom(destination), SRegisterFrom(source));
2832 } else {
2833 GetAssembler()->LoadFromOffset(kLoadWord,
2834 RegisterFrom(destination),
2835 sp,
2836 source.GetStackIndex());
2837 }
2838 } else if (destination.IsFpuRegister()) {
2839 if (source.IsRegister()) {
2840 __ Vmov(SRegisterFrom(destination), RegisterFrom(source));
2841 } else if (source.IsFpuRegister()) {
2842 __ Vmov(SRegisterFrom(destination), SRegisterFrom(source));
2843 } else {
2844 GetAssembler()->LoadSFromOffset(SRegisterFrom(destination), sp, source.GetStackIndex());
2845 }
2846 } else {
2847 DCHECK(destination.IsStackSlot()) << destination;
2848 if (source.IsRegister()) {
2849 GetAssembler()->StoreToOffset(kStoreWord,
2850 RegisterFrom(source),
2851 sp,
2852 destination.GetStackIndex());
2853 } else if (source.IsFpuRegister()) {
2854 GetAssembler()->StoreSToOffset(SRegisterFrom(source), sp, destination.GetStackIndex());
2855 } else {
2856 DCHECK(source.IsStackSlot()) << source;
2857 UseScratchRegisterScope temps(GetVIXLAssembler());
2858 vixl32::Register temp = temps.Acquire();
2859 GetAssembler()->LoadFromOffset(kLoadWord, temp, sp, source.GetStackIndex());
2860 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
2861 }
2862 }
2863}
2864
Artem Serovcfbe9132016-10-14 15:58:56 +01002865void CodeGeneratorARMVIXL::MoveConstant(Location location, int32_t value) {
2866 DCHECK(location.IsRegister());
2867 __ Mov(RegisterFrom(location), value);
Scott Wakelingfe885462016-09-22 10:24:38 +01002868}
2869
2870void CodeGeneratorARMVIXL::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002871 // TODO(VIXL): Maybe refactor to have the 'move' implementation here and use it in
2872 // `ParallelMoveResolverARMVIXL::EmitMove`, as is done in the `arm64` backend.
2873 HParallelMove move(GetGraph()->GetArena());
2874 move.AddMove(src, dst, dst_type, nullptr);
2875 GetMoveResolver()->EmitNativeCode(&move);
Scott Wakelingfe885462016-09-22 10:24:38 +01002876}
2877
Artem Serovcfbe9132016-10-14 15:58:56 +01002878void CodeGeneratorARMVIXL::AddLocationAsTemp(Location location, LocationSummary* locations) {
2879 if (location.IsRegister()) {
2880 locations->AddTemp(location);
2881 } else if (location.IsRegisterPair()) {
2882 locations->AddTemp(LocationFrom(LowRegisterFrom(location)));
2883 locations->AddTemp(LocationFrom(HighRegisterFrom(location)));
2884 } else {
2885 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
2886 }
Scott Wakelingfe885462016-09-22 10:24:38 +01002887}
2888
2889void CodeGeneratorARMVIXL::InvokeRuntime(QuickEntrypointEnum entrypoint,
2890 HInstruction* instruction,
2891 uint32_t dex_pc,
2892 SlowPathCode* slow_path) {
2893 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Alexandre Rames374ddf32016-11-04 10:40:49 +00002894 __ Ldr(lr, MemOperand(tr, GetThreadOffset<kArmPointerSize>(entrypoint).Int32Value()));
2895 // Ensure the pc position is recorded immediately after the `blx` instruction.
2896 // 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 +00002897 ExactAssemblyScope aas(GetVIXLAssembler(),
2898 vixl32::k16BitT32InstructionSizeInBytes,
2899 CodeBufferCheckScope::kExactSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00002900 __ blx(lr);
Scott Wakelingfe885462016-09-22 10:24:38 +01002901 if (EntrypointRequiresStackMap(entrypoint)) {
2902 RecordPcInfo(instruction, dex_pc, slow_path);
2903 }
2904}
2905
2906void CodeGeneratorARMVIXL::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
2907 HInstruction* instruction,
2908 SlowPathCode* slow_path) {
2909 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Alexandre Rames374ddf32016-11-04 10:40:49 +00002910 __ Ldr(lr, MemOperand(tr, entry_point_offset));
Scott Wakelingfe885462016-09-22 10:24:38 +01002911 __ Blx(lr);
2912}
2913
Scott Wakelingfe885462016-09-22 10:24:38 +01002914void InstructionCodeGeneratorARMVIXL::HandleGoto(HInstruction* got, HBasicBlock* successor) {
2915 DCHECK(!successor->IsExitBlock());
2916 HBasicBlock* block = got->GetBlock();
2917 HInstruction* previous = got->GetPrevious();
2918 HLoopInformation* info = block->GetLoopInformation();
2919
2920 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
2921 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
2922 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2923 return;
2924 }
2925 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2926 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
2927 }
2928 if (!codegen_->GoesToNextBlock(block, successor)) {
2929 __ B(codegen_->GetLabelOf(successor));
2930 }
2931}
2932
2933void LocationsBuilderARMVIXL::VisitGoto(HGoto* got) {
2934 got->SetLocations(nullptr);
2935}
2936
2937void InstructionCodeGeneratorARMVIXL::VisitGoto(HGoto* got) {
2938 HandleGoto(got, got->GetSuccessor());
2939}
2940
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002941void LocationsBuilderARMVIXL::VisitTryBoundary(HTryBoundary* try_boundary) {
2942 try_boundary->SetLocations(nullptr);
2943}
2944
2945void InstructionCodeGeneratorARMVIXL::VisitTryBoundary(HTryBoundary* try_boundary) {
2946 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2947 if (!successor->IsExitBlock()) {
2948 HandleGoto(try_boundary, successor);
2949 }
2950}
2951
Scott Wakelingfe885462016-09-22 10:24:38 +01002952void LocationsBuilderARMVIXL::VisitExit(HExit* exit) {
2953 exit->SetLocations(nullptr);
2954}
2955
2956void InstructionCodeGeneratorARMVIXL::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
2957}
2958
Scott Wakelingfe885462016-09-22 10:24:38 +01002959void InstructionCodeGeneratorARMVIXL::GenerateCompareTestAndBranch(HCondition* condition,
2960 vixl32::Label* true_target_in,
Anton Kirilovfd522532017-05-10 12:46:57 +01002961 vixl32::Label* false_target_in,
2962 bool is_far_target) {
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002963 if (CanGenerateTest(condition, codegen_->GetAssembler())) {
2964 vixl32::Label* non_fallthrough_target;
2965 bool invert;
2966
2967 if (true_target_in == nullptr) {
2968 DCHECK(false_target_in != nullptr);
2969 non_fallthrough_target = false_target_in;
2970 invert = true;
2971 } else {
2972 non_fallthrough_target = true_target_in;
2973 invert = false;
2974 }
2975
2976 const auto cond = GenerateTest(condition, invert, codegen_);
2977
Anton Kirilovfd522532017-05-10 12:46:57 +01002978 __ B(cond.first, non_fallthrough_target, is_far_target);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002979
2980 if (false_target_in != nullptr && false_target_in != non_fallthrough_target) {
2981 __ B(false_target_in);
2982 }
2983
2984 return;
2985 }
2986
Scott Wakelingfe885462016-09-22 10:24:38 +01002987 // Generated branching requires both targets to be explicit. If either of the
2988 // targets is nullptr (fallthrough) use and bind `fallthrough` instead.
2989 vixl32::Label fallthrough;
2990 vixl32::Label* true_target = (true_target_in == nullptr) ? &fallthrough : true_target_in;
2991 vixl32::Label* false_target = (false_target_in == nullptr) ? &fallthrough : false_target_in;
2992
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002993 DCHECK_EQ(condition->InputAt(0)->GetType(), Primitive::kPrimLong);
Anton Kirilovfd522532017-05-10 12:46:57 +01002994 GenerateLongComparesAndJumps(condition, true_target, false_target, codegen_, is_far_target);
Scott Wakelingfe885462016-09-22 10:24:38 +01002995
2996 if (false_target != &fallthrough) {
2997 __ B(false_target);
2998 }
2999
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003000 if (fallthrough.IsReferenced()) {
Scott Wakelingfe885462016-09-22 10:24:38 +01003001 __ Bind(&fallthrough);
3002 }
3003}
3004
3005void InstructionCodeGeneratorARMVIXL::GenerateTestAndBranch(HInstruction* instruction,
3006 size_t condition_input_index,
3007 vixl32::Label* true_target,
xueliang.zhongf51bc622016-11-04 09:23:32 +00003008 vixl32::Label* false_target,
3009 bool far_target) {
Scott Wakelingfe885462016-09-22 10:24:38 +01003010 HInstruction* cond = instruction->InputAt(condition_input_index);
3011
3012 if (true_target == nullptr && false_target == nullptr) {
3013 // Nothing to do. The code always falls through.
3014 return;
3015 } else if (cond->IsIntConstant()) {
3016 // Constant condition, statically compared against "true" (integer value 1).
3017 if (cond->AsIntConstant()->IsTrue()) {
3018 if (true_target != nullptr) {
3019 __ B(true_target);
3020 }
3021 } else {
Anton Kirilov644032c2016-12-06 17:51:43 +00003022 DCHECK(cond->AsIntConstant()->IsFalse()) << Int32ConstantFrom(cond);
Scott Wakelingfe885462016-09-22 10:24:38 +01003023 if (false_target != nullptr) {
3024 __ B(false_target);
3025 }
3026 }
3027 return;
3028 }
3029
3030 // The following code generates these patterns:
3031 // (1) true_target == nullptr && false_target != nullptr
3032 // - opposite condition true => branch to false_target
3033 // (2) true_target != nullptr && false_target == nullptr
3034 // - condition true => branch to true_target
3035 // (3) true_target != nullptr && false_target != nullptr
3036 // - condition true => branch to true_target
3037 // - branch to false_target
3038 if (IsBooleanValueOrMaterializedCondition(cond)) {
3039 // Condition has been materialized, compare the output to 0.
3040 if (kIsDebugBuild) {
3041 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
3042 DCHECK(cond_val.IsRegister());
3043 }
3044 if (true_target == nullptr) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00003045 __ CompareAndBranchIfZero(InputRegisterAt(instruction, condition_input_index),
3046 false_target,
3047 far_target);
Scott Wakelingfe885462016-09-22 10:24:38 +01003048 } else {
xueliang.zhongf51bc622016-11-04 09:23:32 +00003049 __ CompareAndBranchIfNonZero(InputRegisterAt(instruction, condition_input_index),
3050 true_target,
3051 far_target);
Scott Wakelingfe885462016-09-22 10:24:38 +01003052 }
3053 } else {
3054 // Condition has not been materialized. Use its inputs as the comparison and
3055 // its condition as the branch condition.
3056 HCondition* condition = cond->AsCondition();
3057
3058 // If this is a long or FP comparison that has been folded into
3059 // the HCondition, generate the comparison directly.
3060 Primitive::Type type = condition->InputAt(0)->GetType();
3061 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
Anton Kirilovfd522532017-05-10 12:46:57 +01003062 GenerateCompareTestAndBranch(condition, true_target, false_target, far_target);
Scott Wakelingfe885462016-09-22 10:24:38 +01003063 return;
3064 }
3065
Donghui Bai426b49c2016-11-08 14:55:38 +08003066 vixl32::Label* non_fallthrough_target;
3067 vixl32::Condition arm_cond = vixl32::Condition::None();
3068 const vixl32::Register left = InputRegisterAt(cond, 0);
3069 const Operand right = InputOperandAt(cond, 1);
3070
Scott Wakelingfe885462016-09-22 10:24:38 +01003071 if (true_target == nullptr) {
Donghui Bai426b49c2016-11-08 14:55:38 +08003072 arm_cond = ARMCondition(condition->GetOppositeCondition());
3073 non_fallthrough_target = false_target;
Scott Wakelingfe885462016-09-22 10:24:38 +01003074 } else {
Donghui Bai426b49c2016-11-08 14:55:38 +08003075 arm_cond = ARMCondition(condition->GetCondition());
3076 non_fallthrough_target = true_target;
3077 }
3078
3079 if (right.IsImmediate() && right.GetImmediate() == 0 && (arm_cond.Is(ne) || arm_cond.Is(eq))) {
3080 if (arm_cond.Is(eq)) {
Anton Kirilovfd522532017-05-10 12:46:57 +01003081 __ CompareAndBranchIfZero(left, non_fallthrough_target, far_target);
Donghui Bai426b49c2016-11-08 14:55:38 +08003082 } else {
3083 DCHECK(arm_cond.Is(ne));
Anton Kirilovfd522532017-05-10 12:46:57 +01003084 __ CompareAndBranchIfNonZero(left, non_fallthrough_target, far_target);
Donghui Bai426b49c2016-11-08 14:55:38 +08003085 }
3086 } else {
3087 __ Cmp(left, right);
Anton Kirilovfd522532017-05-10 12:46:57 +01003088 __ B(arm_cond, non_fallthrough_target, far_target);
Scott Wakelingfe885462016-09-22 10:24:38 +01003089 }
3090 }
3091
3092 // If neither branch falls through (case 3), the conditional branch to `true_target`
3093 // was already emitted (case 2) and we need to emit a jump to `false_target`.
3094 if (true_target != nullptr && false_target != nullptr) {
3095 __ B(false_target);
3096 }
3097}
3098
3099void LocationsBuilderARMVIXL::VisitIf(HIf* if_instr) {
3100 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
3101 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
3102 locations->SetInAt(0, Location::RequiresRegister());
3103 }
3104}
3105
3106void InstructionCodeGeneratorARMVIXL::VisitIf(HIf* if_instr) {
3107 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
3108 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003109 vixl32::Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
3110 nullptr : codegen_->GetLabelOf(true_successor);
3111 vixl32::Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
3112 nullptr : codegen_->GetLabelOf(false_successor);
Scott Wakelingfe885462016-09-22 10:24:38 +01003113 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
3114}
3115
Scott Wakelingc34dba72016-10-03 10:14:44 +01003116void LocationsBuilderARMVIXL::VisitDeoptimize(HDeoptimize* deoptimize) {
3117 LocationSummary* locations = new (GetGraph()->GetArena())
3118 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003119 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3120 RegisterSet caller_saves = RegisterSet::Empty();
3121 caller_saves.Add(LocationFrom(calling_convention.GetRegisterAt(0)));
3122 locations->SetCustomSlowPathCallerSaves(caller_saves);
Scott Wakelingc34dba72016-10-03 10:14:44 +01003123 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
3124 locations->SetInAt(0, Location::RequiresRegister());
3125 }
3126}
3127
3128void InstructionCodeGeneratorARMVIXL::VisitDeoptimize(HDeoptimize* deoptimize) {
3129 SlowPathCodeARMVIXL* slow_path =
3130 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARMVIXL>(deoptimize);
3131 GenerateTestAndBranch(deoptimize,
3132 /* condition_input_index */ 0,
3133 slow_path->GetEntryLabel(),
3134 /* false_target */ nullptr);
3135}
3136
Artem Serovd4cc5b22016-11-04 11:19:09 +00003137void LocationsBuilderARMVIXL::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
3138 LocationSummary* locations = new (GetGraph()->GetArena())
3139 LocationSummary(flag, LocationSummary::kNoCall);
3140 locations->SetOut(Location::RequiresRegister());
3141}
3142
3143void InstructionCodeGeneratorARMVIXL::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
3144 GetAssembler()->LoadFromOffset(kLoadWord,
3145 OutputRegister(flag),
3146 sp,
3147 codegen_->GetStackOffsetOfShouldDeoptimizeFlag());
3148}
3149
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003150void LocationsBuilderARMVIXL::VisitSelect(HSelect* select) {
3151 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Donghui Bai426b49c2016-11-08 14:55:38 +08003152 const bool is_floating_point = Primitive::IsFloatingPointType(select->GetType());
3153
3154 if (is_floating_point) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003155 locations->SetInAt(0, Location::RequiresFpuRegister());
Donghui Bai426b49c2016-11-08 14:55:38 +08003156 locations->SetInAt(1, Location::FpuRegisterOrConstant(select->GetTrueValue()));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003157 } else {
3158 locations->SetInAt(0, Location::RequiresRegister());
Donghui Bai426b49c2016-11-08 14:55:38 +08003159 locations->SetInAt(1, Arm8BitEncodableConstantOrRegister(select->GetTrueValue()));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003160 }
Donghui Bai426b49c2016-11-08 14:55:38 +08003161
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003162 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
Donghui Bai426b49c2016-11-08 14:55:38 +08003163 locations->SetInAt(2, Location::RegisterOrConstant(select->GetCondition()));
3164 // The code generator handles overlap with the values, but not with the condition.
3165 locations->SetOut(Location::SameAsFirstInput());
3166 } else if (is_floating_point) {
3167 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3168 } else {
3169 if (!locations->InAt(1).IsConstant()) {
3170 locations->SetInAt(0, Arm8BitEncodableConstantOrRegister(select->GetFalseValue()));
3171 }
3172
3173 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003174 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003175}
3176
3177void InstructionCodeGeneratorARMVIXL::VisitSelect(HSelect* select) {
Donghui Bai426b49c2016-11-08 14:55:38 +08003178 HInstruction* const condition = select->GetCondition();
3179 const LocationSummary* const locations = select->GetLocations();
3180 const Primitive::Type type = select->GetType();
3181 const Location first = locations->InAt(0);
3182 const Location out = locations->Out();
3183 const Location second = locations->InAt(1);
3184 Location src;
3185
3186 if (condition->IsIntConstant()) {
3187 if (condition->AsIntConstant()->IsFalse()) {
3188 src = first;
3189 } else {
3190 src = second;
3191 }
3192
3193 codegen_->MoveLocation(out, src, type);
3194 return;
3195 }
3196
3197 if (!Primitive::IsFloatingPointType(type) &&
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003198 (IsBooleanValueOrMaterializedCondition(condition) ||
3199 CanGenerateTest(condition->AsCondition(), codegen_->GetAssembler()))) {
Donghui Bai426b49c2016-11-08 14:55:38 +08003200 bool invert = false;
3201
3202 if (out.Equals(second)) {
3203 src = first;
3204 invert = true;
3205 } else if (out.Equals(first)) {
3206 src = second;
3207 } else if (second.IsConstant()) {
3208 DCHECK(CanEncodeConstantAs8BitImmediate(second.GetConstant()));
3209 src = second;
3210 } else if (first.IsConstant()) {
3211 DCHECK(CanEncodeConstantAs8BitImmediate(first.GetConstant()));
3212 src = first;
3213 invert = true;
3214 } else {
3215 src = second;
3216 }
3217
3218 if (CanGenerateConditionalMove(out, src)) {
3219 if (!out.Equals(first) && !out.Equals(second)) {
3220 codegen_->MoveLocation(out, src.Equals(first) ? second : first, type);
3221 }
3222
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003223 std::pair<vixl32::Condition, vixl32::Condition> cond(eq, ne);
3224
3225 if (IsBooleanValueOrMaterializedCondition(condition)) {
3226 __ Cmp(InputRegisterAt(select, 2), 0);
3227 cond = invert ? std::make_pair(eq, ne) : std::make_pair(ne, eq);
3228 } else {
3229 cond = GenerateTest(condition->AsCondition(), invert, codegen_);
3230 }
3231
Donghui Bai426b49c2016-11-08 14:55:38 +08003232 const size_t instr_count = out.IsRegisterPair() ? 4 : 2;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003233 // We use the scope because of the IT block that follows.
Donghui Bai426b49c2016-11-08 14:55:38 +08003234 ExactAssemblyScope guard(GetVIXLAssembler(),
3235 instr_count * vixl32::k16BitT32InstructionSizeInBytes,
3236 CodeBufferCheckScope::kExactSize);
3237
3238 if (out.IsRegister()) {
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003239 __ it(cond.first);
3240 __ mov(cond.first, RegisterFrom(out), OperandFrom(src, type));
Donghui Bai426b49c2016-11-08 14:55:38 +08003241 } else {
3242 DCHECK(out.IsRegisterPair());
3243
3244 Operand operand_high(0);
3245 Operand operand_low(0);
3246
3247 if (src.IsConstant()) {
3248 const int64_t value = Int64ConstantFrom(src);
3249
3250 operand_high = High32Bits(value);
3251 operand_low = Low32Bits(value);
3252 } else {
3253 DCHECK(src.IsRegisterPair());
3254 operand_high = HighRegisterFrom(src);
3255 operand_low = LowRegisterFrom(src);
3256 }
3257
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003258 __ it(cond.first);
3259 __ mov(cond.first, LowRegisterFrom(out), operand_low);
3260 __ it(cond.first);
3261 __ mov(cond.first, HighRegisterFrom(out), operand_high);
Donghui Bai426b49c2016-11-08 14:55:38 +08003262 }
3263
3264 return;
3265 }
3266 }
3267
3268 vixl32::Label* false_target = nullptr;
3269 vixl32::Label* true_target = nullptr;
3270 vixl32::Label select_end;
3271 vixl32::Label* const target = codegen_->GetFinalLabel(select, &select_end);
3272
3273 if (out.Equals(second)) {
3274 true_target = target;
3275 src = first;
3276 } else {
3277 false_target = target;
3278 src = second;
3279
3280 if (!out.Equals(first)) {
3281 codegen_->MoveLocation(out, first, type);
3282 }
3283 }
3284
3285 GenerateTestAndBranch(select, 2, true_target, false_target, /* far_target */ false);
3286 codegen_->MoveLocation(out, src, type);
3287
3288 if (select_end.IsReferenced()) {
3289 __ Bind(&select_end);
3290 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003291}
3292
Artem Serov551b28f2016-10-18 19:11:30 +01003293void LocationsBuilderARMVIXL::VisitNativeDebugInfo(HNativeDebugInfo* info) {
3294 new (GetGraph()->GetArena()) LocationSummary(info);
3295}
3296
3297void InstructionCodeGeneratorARMVIXL::VisitNativeDebugInfo(HNativeDebugInfo*) {
3298 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
3299}
3300
Scott Wakelingfe885462016-09-22 10:24:38 +01003301void CodeGeneratorARMVIXL::GenerateNop() {
3302 __ Nop();
3303}
3304
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003305// `temp` is an extra temporary register that is used for some conditions;
3306// callers may not specify it, in which case the method will use a scratch
3307// register instead.
3308void CodeGeneratorARMVIXL::GenerateConditionWithZero(IfCondition condition,
3309 vixl32::Register out,
3310 vixl32::Register in,
3311 vixl32::Register temp) {
3312 switch (condition) {
3313 case kCondEQ:
3314 // x <= 0 iff x == 0 when the comparison is unsigned.
3315 case kCondBE:
3316 if (!temp.IsValid() || (out.IsLow() && !out.Is(in))) {
3317 temp = out;
3318 }
3319
3320 // Avoid 32-bit instructions if possible; note that `in` and `temp` must be
3321 // different as well.
3322 if (in.IsLow() && temp.IsLow() && !in.Is(temp)) {
3323 // temp = - in; only 0 sets the carry flag.
3324 __ Rsbs(temp, in, 0);
3325
3326 if (out.Is(in)) {
3327 std::swap(in, temp);
3328 }
3329
3330 // out = - in + in + carry = carry
3331 __ Adc(out, temp, in);
3332 } else {
3333 // If `in` is 0, then it has 32 leading zeros, and less than that otherwise.
3334 __ Clz(out, in);
3335 // Any number less than 32 logically shifted right by 5 bits results in 0;
3336 // the same operation on 32 yields 1.
3337 __ Lsr(out, out, 5);
3338 }
3339
3340 break;
3341 case kCondNE:
3342 // x > 0 iff x != 0 when the comparison is unsigned.
3343 case kCondA: {
3344 UseScratchRegisterScope temps(GetVIXLAssembler());
3345
3346 if (out.Is(in)) {
3347 if (!temp.IsValid() || in.Is(temp)) {
3348 temp = temps.Acquire();
3349 }
3350 } else if (!temp.IsValid() || !temp.IsLow()) {
3351 temp = out;
3352 }
3353
3354 // temp = in - 1; only 0 does not set the carry flag.
3355 __ Subs(temp, in, 1);
3356 // out = in + ~temp + carry = in + (-(in - 1) - 1) + carry = in - in + 1 - 1 + carry = carry
3357 __ Sbc(out, in, temp);
3358 break;
3359 }
3360 case kCondGE:
3361 __ Mvn(out, in);
3362 in = out;
3363 FALLTHROUGH_INTENDED;
3364 case kCondLT:
3365 // We only care about the sign bit.
3366 __ Lsr(out, in, 31);
3367 break;
3368 case kCondAE:
3369 // Trivially true.
3370 __ Mov(out, 1);
3371 break;
3372 case kCondB:
3373 // Trivially false.
3374 __ Mov(out, 0);
3375 break;
3376 default:
3377 LOG(FATAL) << "Unexpected condition " << condition;
3378 UNREACHABLE();
3379 }
3380}
3381
Scott Wakelingfe885462016-09-22 10:24:38 +01003382void LocationsBuilderARMVIXL::HandleCondition(HCondition* cond) {
3383 LocationSummary* locations =
3384 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
3385 // Handle the long/FP comparisons made in instruction simplification.
3386 switch (cond->InputAt(0)->GetType()) {
3387 case Primitive::kPrimLong:
3388 locations->SetInAt(0, Location::RequiresRegister());
3389 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
3390 if (!cond->IsEmittedAtUseSite()) {
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003391 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Scott Wakelingfe885462016-09-22 10:24:38 +01003392 }
3393 break;
3394
Scott Wakelingfe885462016-09-22 10:24:38 +01003395 case Primitive::kPrimFloat:
3396 case Primitive::kPrimDouble:
3397 locations->SetInAt(0, Location::RequiresFpuRegister());
Artem Serov657022c2016-11-23 14:19:38 +00003398 locations->SetInAt(1, ArithmeticZeroOrFpuRegister(cond->InputAt(1)));
Scott Wakelingfe885462016-09-22 10:24:38 +01003399 if (!cond->IsEmittedAtUseSite()) {
3400 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3401 }
3402 break;
3403
3404 default:
3405 locations->SetInAt(0, Location::RequiresRegister());
3406 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
3407 if (!cond->IsEmittedAtUseSite()) {
3408 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3409 }
3410 }
3411}
3412
3413void InstructionCodeGeneratorARMVIXL::HandleCondition(HCondition* cond) {
3414 if (cond->IsEmittedAtUseSite()) {
3415 return;
3416 }
3417
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003418 const Primitive::Type type = cond->GetLeft()->GetType();
Scott Wakelingfe885462016-09-22 10:24:38 +01003419
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003420 if (Primitive::IsFloatingPointType(type)) {
3421 GenerateConditionGeneric(cond, codegen_);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003422 return;
Scott Wakelingfe885462016-09-22 10:24:38 +01003423 }
3424
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003425 DCHECK(Primitive::IsIntegralType(type) || type == Primitive::kPrimNot) << type;
Scott Wakelingfe885462016-09-22 10:24:38 +01003426
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003427 const IfCondition condition = cond->GetCondition();
Scott Wakelingfe885462016-09-22 10:24:38 +01003428
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003429 // A condition with only one boolean input, or two boolean inputs without being equality or
3430 // inequality results from transformations done by the instruction simplifier, and is handled
3431 // as a regular condition with integral inputs.
3432 if (type == Primitive::kPrimBoolean &&
3433 cond->GetRight()->GetType() == Primitive::kPrimBoolean &&
3434 (condition == kCondEQ || condition == kCondNE)) {
3435 vixl32::Register left = InputRegisterAt(cond, 0);
3436 const vixl32::Register out = OutputRegister(cond);
3437 const Location right_loc = cond->GetLocations()->InAt(1);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003438
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003439 // The constant case is handled by the instruction simplifier.
3440 DCHECK(!right_loc.IsConstant());
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003441
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003442 vixl32::Register right = RegisterFrom(right_loc);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003443
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003444 // Avoid 32-bit instructions if possible.
3445 if (out.Is(right)) {
3446 std::swap(left, right);
3447 }
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003448
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003449 __ Eor(out, left, right);
3450
3451 if (condition == kCondEQ) {
3452 __ Eor(out, out, 1);
3453 }
3454
3455 return;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003456 }
Anton Kirilov6f644202017-02-27 18:29:45 +00003457
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003458 GenerateConditionIntegralOrNonPrimitive(cond, codegen_);
Scott Wakelingfe885462016-09-22 10:24:38 +01003459}
3460
3461void LocationsBuilderARMVIXL::VisitEqual(HEqual* comp) {
3462 HandleCondition(comp);
3463}
3464
3465void InstructionCodeGeneratorARMVIXL::VisitEqual(HEqual* comp) {
3466 HandleCondition(comp);
3467}
3468
3469void LocationsBuilderARMVIXL::VisitNotEqual(HNotEqual* comp) {
3470 HandleCondition(comp);
3471}
3472
3473void InstructionCodeGeneratorARMVIXL::VisitNotEqual(HNotEqual* comp) {
3474 HandleCondition(comp);
3475}
3476
3477void LocationsBuilderARMVIXL::VisitLessThan(HLessThan* comp) {
3478 HandleCondition(comp);
3479}
3480
3481void InstructionCodeGeneratorARMVIXL::VisitLessThan(HLessThan* comp) {
3482 HandleCondition(comp);
3483}
3484
3485void LocationsBuilderARMVIXL::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
3486 HandleCondition(comp);
3487}
3488
3489void InstructionCodeGeneratorARMVIXL::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
3490 HandleCondition(comp);
3491}
3492
3493void LocationsBuilderARMVIXL::VisitGreaterThan(HGreaterThan* comp) {
3494 HandleCondition(comp);
3495}
3496
3497void InstructionCodeGeneratorARMVIXL::VisitGreaterThan(HGreaterThan* comp) {
3498 HandleCondition(comp);
3499}
3500
3501void LocationsBuilderARMVIXL::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
3502 HandleCondition(comp);
3503}
3504
3505void InstructionCodeGeneratorARMVIXL::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
3506 HandleCondition(comp);
3507}
3508
3509void LocationsBuilderARMVIXL::VisitBelow(HBelow* comp) {
3510 HandleCondition(comp);
3511}
3512
3513void InstructionCodeGeneratorARMVIXL::VisitBelow(HBelow* comp) {
3514 HandleCondition(comp);
3515}
3516
3517void LocationsBuilderARMVIXL::VisitBelowOrEqual(HBelowOrEqual* comp) {
3518 HandleCondition(comp);
3519}
3520
3521void InstructionCodeGeneratorARMVIXL::VisitBelowOrEqual(HBelowOrEqual* comp) {
3522 HandleCondition(comp);
3523}
3524
3525void LocationsBuilderARMVIXL::VisitAbove(HAbove* comp) {
3526 HandleCondition(comp);
3527}
3528
3529void InstructionCodeGeneratorARMVIXL::VisitAbove(HAbove* comp) {
3530 HandleCondition(comp);
3531}
3532
3533void LocationsBuilderARMVIXL::VisitAboveOrEqual(HAboveOrEqual* comp) {
3534 HandleCondition(comp);
3535}
3536
3537void InstructionCodeGeneratorARMVIXL::VisitAboveOrEqual(HAboveOrEqual* comp) {
3538 HandleCondition(comp);
3539}
3540
3541void LocationsBuilderARMVIXL::VisitIntConstant(HIntConstant* constant) {
3542 LocationSummary* locations =
3543 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3544 locations->SetOut(Location::ConstantLocation(constant));
3545}
3546
3547void InstructionCodeGeneratorARMVIXL::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
3548 // Will be generated at use site.
3549}
3550
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003551void LocationsBuilderARMVIXL::VisitNullConstant(HNullConstant* constant) {
3552 LocationSummary* locations =
3553 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3554 locations->SetOut(Location::ConstantLocation(constant));
3555}
3556
3557void InstructionCodeGeneratorARMVIXL::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
3558 // Will be generated at use site.
3559}
3560
Scott Wakelingfe885462016-09-22 10:24:38 +01003561void LocationsBuilderARMVIXL::VisitLongConstant(HLongConstant* constant) {
3562 LocationSummary* locations =
3563 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3564 locations->SetOut(Location::ConstantLocation(constant));
3565}
3566
3567void InstructionCodeGeneratorARMVIXL::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
3568 // Will be generated at use site.
3569}
3570
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01003571void LocationsBuilderARMVIXL::VisitFloatConstant(HFloatConstant* constant) {
3572 LocationSummary* locations =
3573 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3574 locations->SetOut(Location::ConstantLocation(constant));
3575}
3576
Scott Wakelingc34dba72016-10-03 10:14:44 +01003577void InstructionCodeGeneratorARMVIXL::VisitFloatConstant(
3578 HFloatConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01003579 // Will be generated at use site.
3580}
3581
3582void LocationsBuilderARMVIXL::VisitDoubleConstant(HDoubleConstant* constant) {
3583 LocationSummary* locations =
3584 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3585 locations->SetOut(Location::ConstantLocation(constant));
3586}
3587
Scott Wakelingc34dba72016-10-03 10:14:44 +01003588void InstructionCodeGeneratorARMVIXL::VisitDoubleConstant(
3589 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01003590 // Will be generated at use site.
3591}
3592
Igor Murashkind01745e2017-04-05 16:40:31 -07003593void LocationsBuilderARMVIXL::VisitConstructorFence(HConstructorFence* constructor_fence) {
3594 constructor_fence->SetLocations(nullptr);
3595}
3596
3597void InstructionCodeGeneratorARMVIXL::VisitConstructorFence(
3598 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
3599 codegen_->GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
3600}
3601
Scott Wakelingfe885462016-09-22 10:24:38 +01003602void LocationsBuilderARMVIXL::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3603 memory_barrier->SetLocations(nullptr);
3604}
3605
3606void InstructionCodeGeneratorARMVIXL::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3607 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
3608}
3609
3610void LocationsBuilderARMVIXL::VisitReturnVoid(HReturnVoid* ret) {
3611 ret->SetLocations(nullptr);
3612}
3613
3614void InstructionCodeGeneratorARMVIXL::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
3615 codegen_->GenerateFrameExit();
3616}
3617
3618void LocationsBuilderARMVIXL::VisitReturn(HReturn* ret) {
3619 LocationSummary* locations =
3620 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
3621 locations->SetInAt(0, parameter_visitor_.GetReturnLocation(ret->InputAt(0)->GetType()));
3622}
3623
3624void InstructionCodeGeneratorARMVIXL::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
3625 codegen_->GenerateFrameExit();
3626}
3627
Artem Serovcfbe9132016-10-14 15:58:56 +01003628void LocationsBuilderARMVIXL::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3629 // The trampoline uses the same calling convention as dex calling conventions,
3630 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
3631 // the method_idx.
3632 HandleInvoke(invoke);
3633}
3634
3635void InstructionCodeGeneratorARMVIXL::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3636 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
3637}
3638
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003639void LocationsBuilderARMVIXL::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
3640 // Explicit clinit checks triggered by static invokes must have been pruned by
3641 // art::PrepareForRegisterAllocation.
3642 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
3643
Anton Kirilov5ec62182016-10-13 20:16:02 +01003644 IntrinsicLocationsBuilderARMVIXL intrinsic(codegen_);
3645 if (intrinsic.TryDispatch(invoke)) {
3646 if (invoke->GetLocations()->CanCall() && invoke->HasPcRelativeDexCache()) {
3647 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
3648 }
3649 return;
3650 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003651
3652 HandleInvoke(invoke);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01003653
Artem Serovd4cc5b22016-11-04 11:19:09 +00003654 // For PC-relative dex cache the invoke has an extra input, the PC-relative address base.
3655 if (invoke->HasPcRelativeDexCache()) {
3656 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
3657 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003658}
3659
Anton Kirilov5ec62182016-10-13 20:16:02 +01003660static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARMVIXL* codegen) {
3661 if (invoke->GetLocations()->Intrinsified()) {
3662 IntrinsicCodeGeneratorARMVIXL intrinsic(codegen);
3663 intrinsic.Dispatch(invoke);
3664 return true;
3665 }
3666 return false;
3667}
3668
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003669void InstructionCodeGeneratorARMVIXL::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
3670 // Explicit clinit checks triggered by static invokes must have been pruned by
3671 // art::PrepareForRegisterAllocation.
3672 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
3673
Anton Kirilov5ec62182016-10-13 20:16:02 +01003674 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3675 return;
3676 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003677
3678 LocationSummary* locations = invoke->GetLocations();
Artem Serovd4cc5b22016-11-04 11:19:09 +00003679 codegen_->GenerateStaticOrDirectCall(
3680 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003681 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3682}
3683
3684void LocationsBuilderARMVIXL::HandleInvoke(HInvoke* invoke) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00003685 InvokeDexCallingConventionVisitorARMVIXL calling_convention_visitor;
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003686 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
3687}
3688
3689void LocationsBuilderARMVIXL::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Anton Kirilov5ec62182016-10-13 20:16:02 +01003690 IntrinsicLocationsBuilderARMVIXL intrinsic(codegen_);
3691 if (intrinsic.TryDispatch(invoke)) {
3692 return;
3693 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003694
3695 HandleInvoke(invoke);
3696}
3697
3698void InstructionCodeGeneratorARMVIXL::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Anton Kirilov5ec62182016-10-13 20:16:02 +01003699 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3700 return;
3701 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003702
3703 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003704 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Alexandre Rames374ddf32016-11-04 10:40:49 +00003705 DCHECK(!codegen_->IsLeafMethod());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003706}
3707
Artem Serovcfbe9132016-10-14 15:58:56 +01003708void LocationsBuilderARMVIXL::VisitInvokeInterface(HInvokeInterface* invoke) {
3709 HandleInvoke(invoke);
3710 // Add the hidden argument.
3711 invoke->GetLocations()->AddTemp(LocationFrom(r12));
3712}
3713
3714void InstructionCodeGeneratorARMVIXL::VisitInvokeInterface(HInvokeInterface* invoke) {
3715 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
3716 LocationSummary* locations = invoke->GetLocations();
3717 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
3718 vixl32::Register hidden_reg = RegisterFrom(locations->GetTemp(1));
3719 Location receiver = locations->InAt(0);
3720 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3721
3722 DCHECK(!receiver.IsStackSlot());
3723
Alexandre Rames374ddf32016-11-04 10:40:49 +00003724 // Ensure the pc position is recorded immediately after the `ldr` instruction.
3725 {
Artem Serov0fb37192016-12-06 18:13:40 +00003726 ExactAssemblyScope aas(GetVIXLAssembler(),
3727 vixl32::kMaxInstructionSizeInBytes,
3728 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00003729 // /* HeapReference<Class> */ temp = receiver->klass_
3730 __ ldr(temp, MemOperand(RegisterFrom(receiver), class_offset));
3731 codegen_->MaybeRecordImplicitNullCheck(invoke);
3732 }
Artem Serovcfbe9132016-10-14 15:58:56 +01003733 // Instead of simply (possibly) unpoisoning `temp` here, we should
3734 // emit a read barrier for the previous class reference load.
3735 // However this is not required in practice, as this is an
3736 // intermediate/temporary reference and because the current
3737 // concurrent copying collector keeps the from-space memory
3738 // intact/accessible until the end of the marking phase (the
3739 // concurrent copying collector may not in the future).
3740 GetAssembler()->MaybeUnpoisonHeapReference(temp);
3741 GetAssembler()->LoadFromOffset(kLoadWord,
3742 temp,
3743 temp,
3744 mirror::Class::ImtPtrOffset(kArmPointerSize).Uint32Value());
3745 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
3746 invoke->GetImtIndex(), kArmPointerSize));
3747 // temp = temp->GetImtEntryAt(method_offset);
3748 GetAssembler()->LoadFromOffset(kLoadWord, temp, temp, method_offset);
3749 uint32_t entry_point =
3750 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize).Int32Value();
3751 // LR = temp->GetEntryPoint();
3752 GetAssembler()->LoadFromOffset(kLoadWord, lr, temp, entry_point);
3753
3754 // Set the hidden (in r12) argument. It is done here, right before a BLX to prevent other
3755 // instruction from clobbering it as they might use r12 as a scratch register.
3756 DCHECK(hidden_reg.Is(r12));
Scott Wakelingb77051e2016-11-21 19:46:00 +00003757
3758 {
3759 // The VIXL macro assembler may clobber any of the scratch registers that are available to it,
3760 // so it checks if the application is using them (by passing them to the macro assembler
3761 // methods). The following application of UseScratchRegisterScope corrects VIXL's notion of
3762 // what is available, and is the opposite of the standard usage: Instead of requesting a
3763 // temporary location, it imposes an external constraint (i.e. a specific register is reserved
3764 // for the hidden argument). Note that this works even if VIXL needs a scratch register itself
3765 // (to materialize the constant), since the destination register becomes available for such use
3766 // internally for the duration of the macro instruction.
3767 UseScratchRegisterScope temps(GetVIXLAssembler());
3768 temps.Exclude(hidden_reg);
3769 __ Mov(hidden_reg, invoke->GetDexMethodIndex());
3770 }
Artem Serovcfbe9132016-10-14 15:58:56 +01003771 {
Alexandre Rames374ddf32016-11-04 10:40:49 +00003772 // Ensure the pc position is recorded immediately after the `blx` instruction.
3773 // 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 +00003774 ExactAssemblyScope aas(GetVIXLAssembler(),
Alexandre Rames374ddf32016-11-04 10:40:49 +00003775 vixl32::k16BitT32InstructionSizeInBytes,
3776 CodeBufferCheckScope::kExactSize);
Artem Serovcfbe9132016-10-14 15:58:56 +01003777 // LR();
3778 __ blx(lr);
Artem Serovcfbe9132016-10-14 15:58:56 +01003779 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Alexandre Rames374ddf32016-11-04 10:40:49 +00003780 DCHECK(!codegen_->IsLeafMethod());
Artem Serovcfbe9132016-10-14 15:58:56 +01003781 }
3782}
3783
Orion Hodsonac141392017-01-13 11:53:47 +00003784void LocationsBuilderARMVIXL::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
3785 HandleInvoke(invoke);
3786}
3787
3788void InstructionCodeGeneratorARMVIXL::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
3789 codegen_->GenerateInvokePolymorphicCall(invoke);
3790}
3791
Artem Serov02109dd2016-09-23 17:17:54 +01003792void LocationsBuilderARMVIXL::VisitNeg(HNeg* neg) {
3793 LocationSummary* locations =
3794 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
3795 switch (neg->GetResultType()) {
3796 case Primitive::kPrimInt: {
3797 locations->SetInAt(0, Location::RequiresRegister());
3798 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3799 break;
3800 }
3801 case Primitive::kPrimLong: {
3802 locations->SetInAt(0, Location::RequiresRegister());
3803 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3804 break;
3805 }
3806
3807 case Primitive::kPrimFloat:
3808 case Primitive::kPrimDouble:
3809 locations->SetInAt(0, Location::RequiresFpuRegister());
3810 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3811 break;
3812
3813 default:
3814 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
3815 }
3816}
3817
3818void InstructionCodeGeneratorARMVIXL::VisitNeg(HNeg* neg) {
3819 LocationSummary* locations = neg->GetLocations();
3820 Location out = locations->Out();
3821 Location in = locations->InAt(0);
3822 switch (neg->GetResultType()) {
3823 case Primitive::kPrimInt:
3824 __ Rsb(OutputRegister(neg), InputRegisterAt(neg, 0), 0);
3825 break;
3826
3827 case Primitive::kPrimLong:
3828 // out.lo = 0 - in.lo (and update the carry/borrow (C) flag)
3829 __ Rsbs(LowRegisterFrom(out), LowRegisterFrom(in), 0);
3830 // We cannot emit an RSC (Reverse Subtract with Carry)
3831 // instruction here, as it does not exist in the Thumb-2
3832 // instruction set. We use the following approach
3833 // using SBC and SUB instead.
3834 //
3835 // out.hi = -C
3836 __ Sbc(HighRegisterFrom(out), HighRegisterFrom(out), HighRegisterFrom(out));
3837 // out.hi = out.hi - in.hi
3838 __ Sub(HighRegisterFrom(out), HighRegisterFrom(out), HighRegisterFrom(in));
3839 break;
3840
3841 case Primitive::kPrimFloat:
3842 case Primitive::kPrimDouble:
Anton Kirilov644032c2016-12-06 17:51:43 +00003843 __ Vneg(OutputVRegister(neg), InputVRegister(neg));
Artem Serov02109dd2016-09-23 17:17:54 +01003844 break;
3845
3846 default:
3847 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
3848 }
3849}
3850
Scott Wakelingfe885462016-09-22 10:24:38 +01003851void LocationsBuilderARMVIXL::VisitTypeConversion(HTypeConversion* conversion) {
3852 Primitive::Type result_type = conversion->GetResultType();
3853 Primitive::Type input_type = conversion->GetInputType();
3854 DCHECK_NE(result_type, input_type);
3855
3856 // The float-to-long, double-to-long and long-to-float type conversions
3857 // rely on a call to the runtime.
3858 LocationSummary::CallKind call_kind =
3859 (((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
3860 && result_type == Primitive::kPrimLong)
3861 || (input_type == Primitive::kPrimLong && result_type == Primitive::kPrimFloat))
3862 ? LocationSummary::kCallOnMainOnly
3863 : LocationSummary::kNoCall;
3864 LocationSummary* locations =
3865 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
3866
3867 // The Java language does not allow treating boolean as an integral type but
3868 // our bit representation makes it safe.
3869
3870 switch (result_type) {
3871 case Primitive::kPrimByte:
3872 switch (input_type) {
3873 case Primitive::kPrimLong:
3874 // Type conversion from long to byte is a result of code transformations.
3875 case Primitive::kPrimBoolean:
3876 // Boolean input is a result of code transformations.
3877 case Primitive::kPrimShort:
3878 case Primitive::kPrimInt:
3879 case Primitive::kPrimChar:
3880 // Processing a Dex `int-to-byte' instruction.
3881 locations->SetInAt(0, Location::RequiresRegister());
3882 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3883 break;
3884
3885 default:
3886 LOG(FATAL) << "Unexpected type conversion from " << input_type
3887 << " to " << result_type;
3888 }
3889 break;
3890
3891 case Primitive::kPrimShort:
3892 switch (input_type) {
3893 case Primitive::kPrimLong:
3894 // Type conversion from long to short is a result of code transformations.
3895 case Primitive::kPrimBoolean:
3896 // Boolean input is a result of code transformations.
3897 case Primitive::kPrimByte:
3898 case Primitive::kPrimInt:
3899 case Primitive::kPrimChar:
3900 // Processing a Dex `int-to-short' instruction.
3901 locations->SetInAt(0, Location::RequiresRegister());
3902 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3903 break;
3904
3905 default:
3906 LOG(FATAL) << "Unexpected type conversion from " << input_type
3907 << " to " << result_type;
3908 }
3909 break;
3910
3911 case Primitive::kPrimInt:
3912 switch (input_type) {
3913 case Primitive::kPrimLong:
3914 // Processing a Dex `long-to-int' instruction.
3915 locations->SetInAt(0, Location::Any());
3916 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3917 break;
3918
3919 case Primitive::kPrimFloat:
3920 // Processing a Dex `float-to-int' instruction.
3921 locations->SetInAt(0, Location::RequiresFpuRegister());
3922 locations->SetOut(Location::RequiresRegister());
3923 locations->AddTemp(Location::RequiresFpuRegister());
3924 break;
3925
3926 case Primitive::kPrimDouble:
3927 // Processing a Dex `double-to-int' instruction.
3928 locations->SetInAt(0, Location::RequiresFpuRegister());
3929 locations->SetOut(Location::RequiresRegister());
3930 locations->AddTemp(Location::RequiresFpuRegister());
3931 break;
3932
3933 default:
3934 LOG(FATAL) << "Unexpected type conversion from " << input_type
3935 << " to " << result_type;
3936 }
3937 break;
3938
3939 case Primitive::kPrimLong:
3940 switch (input_type) {
3941 case Primitive::kPrimBoolean:
3942 // Boolean input is a result of code transformations.
3943 case Primitive::kPrimByte:
3944 case Primitive::kPrimShort:
3945 case Primitive::kPrimInt:
3946 case Primitive::kPrimChar:
3947 // Processing a Dex `int-to-long' instruction.
3948 locations->SetInAt(0, Location::RequiresRegister());
3949 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3950 break;
3951
3952 case Primitive::kPrimFloat: {
3953 // Processing a Dex `float-to-long' instruction.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003954 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3955 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
3956 locations->SetOut(LocationFrom(r0, r1));
Scott Wakelingfe885462016-09-22 10:24:38 +01003957 break;
3958 }
3959
3960 case Primitive::kPrimDouble: {
3961 // Processing a Dex `double-to-long' instruction.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003962 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3963 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0),
3964 calling_convention.GetFpuRegisterAt(1)));
3965 locations->SetOut(LocationFrom(r0, r1));
Scott Wakelingfe885462016-09-22 10:24:38 +01003966 break;
3967 }
3968
3969 default:
3970 LOG(FATAL) << "Unexpected type conversion from " << input_type
3971 << " to " << result_type;
3972 }
3973 break;
3974
3975 case Primitive::kPrimChar:
3976 switch (input_type) {
3977 case Primitive::kPrimLong:
3978 // Type conversion from long to char is a result of code transformations.
3979 case Primitive::kPrimBoolean:
3980 // Boolean input is a result of code transformations.
3981 case Primitive::kPrimByte:
3982 case Primitive::kPrimShort:
3983 case Primitive::kPrimInt:
3984 // Processing a Dex `int-to-char' instruction.
3985 locations->SetInAt(0, Location::RequiresRegister());
3986 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3987 break;
3988
3989 default:
3990 LOG(FATAL) << "Unexpected type conversion from " << input_type
3991 << " to " << result_type;
3992 }
3993 break;
3994
3995 case Primitive::kPrimFloat:
3996 switch (input_type) {
3997 case Primitive::kPrimBoolean:
3998 // Boolean input is a result of code transformations.
3999 case Primitive::kPrimByte:
4000 case Primitive::kPrimShort:
4001 case Primitive::kPrimInt:
4002 case Primitive::kPrimChar:
4003 // Processing a Dex `int-to-float' instruction.
4004 locations->SetInAt(0, Location::RequiresRegister());
4005 locations->SetOut(Location::RequiresFpuRegister());
4006 break;
4007
4008 case Primitive::kPrimLong: {
4009 // Processing a Dex `long-to-float' instruction.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004010 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4011 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0),
4012 calling_convention.GetRegisterAt(1)));
4013 locations->SetOut(LocationFrom(calling_convention.GetFpuRegisterAt(0)));
Scott Wakelingfe885462016-09-22 10:24:38 +01004014 break;
4015 }
4016
4017 case Primitive::kPrimDouble:
4018 // Processing a Dex `double-to-float' instruction.
4019 locations->SetInAt(0, Location::RequiresFpuRegister());
4020 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4021 break;
4022
4023 default:
4024 LOG(FATAL) << "Unexpected type conversion from " << input_type
4025 << " to " << result_type;
4026 };
4027 break;
4028
4029 case Primitive::kPrimDouble:
4030 switch (input_type) {
4031 case Primitive::kPrimBoolean:
4032 // Boolean input is a result of code transformations.
4033 case Primitive::kPrimByte:
4034 case Primitive::kPrimShort:
4035 case Primitive::kPrimInt:
4036 case Primitive::kPrimChar:
4037 // Processing a Dex `int-to-double' instruction.
4038 locations->SetInAt(0, Location::RequiresRegister());
4039 locations->SetOut(Location::RequiresFpuRegister());
4040 break;
4041
4042 case Primitive::kPrimLong:
4043 // Processing a Dex `long-to-double' instruction.
4044 locations->SetInAt(0, Location::RequiresRegister());
4045 locations->SetOut(Location::RequiresFpuRegister());
4046 locations->AddTemp(Location::RequiresFpuRegister());
4047 locations->AddTemp(Location::RequiresFpuRegister());
4048 break;
4049
4050 case Primitive::kPrimFloat:
4051 // Processing a Dex `float-to-double' instruction.
4052 locations->SetInAt(0, Location::RequiresFpuRegister());
4053 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4054 break;
4055
4056 default:
4057 LOG(FATAL) << "Unexpected type conversion from " << input_type
4058 << " to " << result_type;
4059 };
4060 break;
4061
4062 default:
4063 LOG(FATAL) << "Unexpected type conversion from " << input_type
4064 << " to " << result_type;
4065 }
4066}
4067
4068void InstructionCodeGeneratorARMVIXL::VisitTypeConversion(HTypeConversion* conversion) {
4069 LocationSummary* locations = conversion->GetLocations();
4070 Location out = locations->Out();
4071 Location in = locations->InAt(0);
4072 Primitive::Type result_type = conversion->GetResultType();
4073 Primitive::Type input_type = conversion->GetInputType();
4074 DCHECK_NE(result_type, input_type);
4075 switch (result_type) {
4076 case Primitive::kPrimByte:
4077 switch (input_type) {
4078 case Primitive::kPrimLong:
4079 // Type conversion from long to byte is a result of code transformations.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004080 __ Sbfx(OutputRegister(conversion), LowRegisterFrom(in), 0, 8);
Scott Wakelingfe885462016-09-22 10:24:38 +01004081 break;
4082 case Primitive::kPrimBoolean:
4083 // Boolean input is a result of code transformations.
4084 case Primitive::kPrimShort:
4085 case Primitive::kPrimInt:
4086 case Primitive::kPrimChar:
4087 // Processing a Dex `int-to-byte' instruction.
4088 __ Sbfx(OutputRegister(conversion), InputRegisterAt(conversion, 0), 0, 8);
4089 break;
4090
4091 default:
4092 LOG(FATAL) << "Unexpected type conversion from " << input_type
4093 << " to " << result_type;
4094 }
4095 break;
4096
4097 case Primitive::kPrimShort:
4098 switch (input_type) {
4099 case Primitive::kPrimLong:
4100 // Type conversion from long to short is a result of code transformations.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004101 __ Sbfx(OutputRegister(conversion), LowRegisterFrom(in), 0, 16);
Scott Wakelingfe885462016-09-22 10:24:38 +01004102 break;
4103 case Primitive::kPrimBoolean:
4104 // Boolean input is a result of code transformations.
4105 case Primitive::kPrimByte:
4106 case Primitive::kPrimInt:
4107 case Primitive::kPrimChar:
4108 // Processing a Dex `int-to-short' instruction.
4109 __ Sbfx(OutputRegister(conversion), InputRegisterAt(conversion, 0), 0, 16);
4110 break;
4111
4112 default:
4113 LOG(FATAL) << "Unexpected type conversion from " << input_type
4114 << " to " << result_type;
4115 }
4116 break;
4117
4118 case Primitive::kPrimInt:
4119 switch (input_type) {
4120 case Primitive::kPrimLong:
4121 // Processing a Dex `long-to-int' instruction.
4122 DCHECK(out.IsRegister());
4123 if (in.IsRegisterPair()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004124 __ Mov(OutputRegister(conversion), LowRegisterFrom(in));
Scott Wakelingfe885462016-09-22 10:24:38 +01004125 } else if (in.IsDoubleStackSlot()) {
4126 GetAssembler()->LoadFromOffset(kLoadWord,
4127 OutputRegister(conversion),
4128 sp,
4129 in.GetStackIndex());
4130 } else {
4131 DCHECK(in.IsConstant());
4132 DCHECK(in.GetConstant()->IsLongConstant());
Vladimir Markoba1a48e2017-04-13 11:50:14 +01004133 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
4134 __ Mov(OutputRegister(conversion), static_cast<int32_t>(value));
Scott Wakelingfe885462016-09-22 10:24:38 +01004135 }
4136 break;
4137
4138 case Primitive::kPrimFloat: {
4139 // Processing a Dex `float-to-int' instruction.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004140 vixl32::SRegister temp = LowSRegisterFrom(locations->GetTemp(0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01004141 __ Vcvt(S32, F32, temp, InputSRegisterAt(conversion, 0));
Scott Wakelingfe885462016-09-22 10:24:38 +01004142 __ Vmov(OutputRegister(conversion), temp);
4143 break;
4144 }
4145
4146 case Primitive::kPrimDouble: {
4147 // Processing a Dex `double-to-int' instruction.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004148 vixl32::SRegister temp_s = LowSRegisterFrom(locations->GetTemp(0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01004149 __ Vcvt(S32, F64, temp_s, DRegisterFrom(in));
Scott Wakelingfe885462016-09-22 10:24:38 +01004150 __ Vmov(OutputRegister(conversion), temp_s);
4151 break;
4152 }
4153
4154 default:
4155 LOG(FATAL) << "Unexpected type conversion from " << input_type
4156 << " to " << result_type;
4157 }
4158 break;
4159
4160 case Primitive::kPrimLong:
4161 switch (input_type) {
4162 case Primitive::kPrimBoolean:
4163 // Boolean input is a result of code transformations.
4164 case Primitive::kPrimByte:
4165 case Primitive::kPrimShort:
4166 case Primitive::kPrimInt:
4167 case Primitive::kPrimChar:
4168 // Processing a Dex `int-to-long' instruction.
4169 DCHECK(out.IsRegisterPair());
4170 DCHECK(in.IsRegister());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004171 __ Mov(LowRegisterFrom(out), InputRegisterAt(conversion, 0));
Scott Wakelingfe885462016-09-22 10:24:38 +01004172 // Sign extension.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004173 __ Asr(HighRegisterFrom(out), LowRegisterFrom(out), 31);
Scott Wakelingfe885462016-09-22 10:24:38 +01004174 break;
4175
4176 case Primitive::kPrimFloat:
4177 // Processing a Dex `float-to-long' instruction.
4178 codegen_->InvokeRuntime(kQuickF2l, conversion, conversion->GetDexPc());
4179 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
4180 break;
4181
4182 case Primitive::kPrimDouble:
4183 // Processing a Dex `double-to-long' instruction.
4184 codegen_->InvokeRuntime(kQuickD2l, conversion, conversion->GetDexPc());
4185 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
4186 break;
4187
4188 default:
4189 LOG(FATAL) << "Unexpected type conversion from " << input_type
4190 << " to " << result_type;
4191 }
4192 break;
4193
4194 case Primitive::kPrimChar:
4195 switch (input_type) {
4196 case Primitive::kPrimLong:
4197 // Type conversion from long to char is a result of code transformations.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004198 __ Ubfx(OutputRegister(conversion), LowRegisterFrom(in), 0, 16);
Scott Wakelingfe885462016-09-22 10:24:38 +01004199 break;
4200 case Primitive::kPrimBoolean:
4201 // Boolean input is a result of code transformations.
4202 case Primitive::kPrimByte:
4203 case Primitive::kPrimShort:
4204 case Primitive::kPrimInt:
4205 // Processing a Dex `int-to-char' instruction.
4206 __ Ubfx(OutputRegister(conversion), InputRegisterAt(conversion, 0), 0, 16);
4207 break;
4208
4209 default:
4210 LOG(FATAL) << "Unexpected type conversion from " << input_type
4211 << " to " << result_type;
4212 }
4213 break;
4214
4215 case Primitive::kPrimFloat:
4216 switch (input_type) {
4217 case Primitive::kPrimBoolean:
4218 // Boolean input is a result of code transformations.
4219 case Primitive::kPrimByte:
4220 case Primitive::kPrimShort:
4221 case Primitive::kPrimInt:
4222 case Primitive::kPrimChar: {
4223 // Processing a Dex `int-to-float' instruction.
4224 __ Vmov(OutputSRegister(conversion), InputRegisterAt(conversion, 0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01004225 __ Vcvt(F32, S32, OutputSRegister(conversion), OutputSRegister(conversion));
Scott Wakelingfe885462016-09-22 10:24:38 +01004226 break;
4227 }
4228
4229 case Primitive::kPrimLong:
4230 // Processing a Dex `long-to-float' instruction.
4231 codegen_->InvokeRuntime(kQuickL2f, conversion, conversion->GetDexPc());
4232 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
4233 break;
4234
4235 case Primitive::kPrimDouble:
4236 // Processing a Dex `double-to-float' instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01004237 __ Vcvt(F32, F64, OutputSRegister(conversion), DRegisterFrom(in));
Scott Wakelingfe885462016-09-22 10:24:38 +01004238 break;
4239
4240 default:
4241 LOG(FATAL) << "Unexpected type conversion from " << input_type
4242 << " to " << result_type;
4243 };
4244 break;
4245
4246 case Primitive::kPrimDouble:
4247 switch (input_type) {
4248 case Primitive::kPrimBoolean:
4249 // Boolean input is a result of code transformations.
4250 case Primitive::kPrimByte:
4251 case Primitive::kPrimShort:
4252 case Primitive::kPrimInt:
4253 case Primitive::kPrimChar: {
4254 // Processing a Dex `int-to-double' instruction.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004255 __ Vmov(LowSRegisterFrom(out), InputRegisterAt(conversion, 0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01004256 __ Vcvt(F64, S32, DRegisterFrom(out), LowSRegisterFrom(out));
Scott Wakelingfe885462016-09-22 10:24:38 +01004257 break;
4258 }
4259
4260 case Primitive::kPrimLong: {
4261 // Processing a Dex `long-to-double' instruction.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004262 vixl32::Register low = LowRegisterFrom(in);
4263 vixl32::Register high = HighRegisterFrom(in);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004264 vixl32::SRegister out_s = LowSRegisterFrom(out);
Scott Wakelingc34dba72016-10-03 10:14:44 +01004265 vixl32::DRegister out_d = DRegisterFrom(out);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004266 vixl32::SRegister temp_s = LowSRegisterFrom(locations->GetTemp(0));
Scott Wakelingc34dba72016-10-03 10:14:44 +01004267 vixl32::DRegister temp_d = DRegisterFrom(locations->GetTemp(0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01004268 vixl32::DRegister constant_d = DRegisterFrom(locations->GetTemp(1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004269
4270 // temp_d = int-to-double(high)
4271 __ Vmov(temp_s, high);
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01004272 __ Vcvt(F64, S32, temp_d, temp_s);
Scott Wakelingfe885462016-09-22 10:24:38 +01004273 // constant_d = k2Pow32EncodingForDouble
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004274 __ Vmov(constant_d, bit_cast<double, int64_t>(k2Pow32EncodingForDouble));
Scott Wakelingfe885462016-09-22 10:24:38 +01004275 // out_d = unsigned-to-double(low)
4276 __ Vmov(out_s, low);
4277 __ Vcvt(F64, U32, out_d, out_s);
4278 // out_d += temp_d * constant_d
4279 __ Vmla(F64, out_d, temp_d, constant_d);
4280 break;
4281 }
4282
4283 case Primitive::kPrimFloat:
4284 // Processing a Dex `float-to-double' instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01004285 __ Vcvt(F64, F32, DRegisterFrom(out), InputSRegisterAt(conversion, 0));
Scott Wakelingfe885462016-09-22 10:24:38 +01004286 break;
4287
4288 default:
4289 LOG(FATAL) << "Unexpected type conversion from " << input_type
4290 << " to " << result_type;
4291 };
4292 break;
4293
4294 default:
4295 LOG(FATAL) << "Unexpected type conversion from " << input_type
4296 << " to " << result_type;
4297 }
4298}
4299
4300void LocationsBuilderARMVIXL::VisitAdd(HAdd* add) {
4301 LocationSummary* locations =
4302 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
4303 switch (add->GetResultType()) {
4304 case Primitive::kPrimInt: {
4305 locations->SetInAt(0, Location::RequiresRegister());
4306 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
4307 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4308 break;
4309 }
4310
Scott Wakelingfe885462016-09-22 10:24:38 +01004311 case Primitive::kPrimLong: {
4312 locations->SetInAt(0, Location::RequiresRegister());
Anton Kirilovdda43962016-11-21 19:55:20 +00004313 locations->SetInAt(1, ArmEncodableConstantOrRegister(add->InputAt(1), ADD));
Scott Wakelingfe885462016-09-22 10:24:38 +01004314 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4315 break;
4316 }
4317
4318 case Primitive::kPrimFloat:
4319 case Primitive::kPrimDouble: {
4320 locations->SetInAt(0, Location::RequiresFpuRegister());
4321 locations->SetInAt(1, Location::RequiresFpuRegister());
4322 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4323 break;
4324 }
4325
4326 default:
4327 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
4328 }
4329}
4330
4331void InstructionCodeGeneratorARMVIXL::VisitAdd(HAdd* add) {
4332 LocationSummary* locations = add->GetLocations();
4333 Location out = locations->Out();
4334 Location first = locations->InAt(0);
4335 Location second = locations->InAt(1);
4336
4337 switch (add->GetResultType()) {
4338 case Primitive::kPrimInt: {
4339 __ Add(OutputRegister(add), InputRegisterAt(add, 0), InputOperandAt(add, 1));
4340 }
4341 break;
4342
Scott Wakelingfe885462016-09-22 10:24:38 +01004343 case Primitive::kPrimLong: {
Anton Kirilovdda43962016-11-21 19:55:20 +00004344 if (second.IsConstant()) {
4345 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
4346 GenerateAddLongConst(out, first, value);
4347 } else {
4348 DCHECK(second.IsRegisterPair());
4349 __ Adds(LowRegisterFrom(out), LowRegisterFrom(first), LowRegisterFrom(second));
4350 __ Adc(HighRegisterFrom(out), HighRegisterFrom(first), HighRegisterFrom(second));
4351 }
Scott Wakelingfe885462016-09-22 10:24:38 +01004352 break;
4353 }
4354
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004355 case Primitive::kPrimFloat:
Scott Wakelingfe885462016-09-22 10:24:38 +01004356 case Primitive::kPrimDouble:
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004357 __ Vadd(OutputVRegister(add), InputVRegisterAt(add, 0), InputVRegisterAt(add, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004358 break;
4359
4360 default:
4361 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
4362 }
4363}
4364
4365void LocationsBuilderARMVIXL::VisitSub(HSub* sub) {
4366 LocationSummary* locations =
4367 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
4368 switch (sub->GetResultType()) {
4369 case Primitive::kPrimInt: {
4370 locations->SetInAt(0, Location::RequiresRegister());
4371 locations->SetInAt(1, Location::RegisterOrConstant(sub->InputAt(1)));
4372 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4373 break;
4374 }
4375
Scott Wakelingfe885462016-09-22 10:24:38 +01004376 case Primitive::kPrimLong: {
4377 locations->SetInAt(0, Location::RequiresRegister());
Anton Kirilovdda43962016-11-21 19:55:20 +00004378 locations->SetInAt(1, ArmEncodableConstantOrRegister(sub->InputAt(1), SUB));
Scott Wakelingfe885462016-09-22 10:24:38 +01004379 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4380 break;
4381 }
4382 case Primitive::kPrimFloat:
4383 case Primitive::kPrimDouble: {
4384 locations->SetInAt(0, Location::RequiresFpuRegister());
4385 locations->SetInAt(1, Location::RequiresFpuRegister());
4386 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4387 break;
4388 }
4389 default:
4390 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
4391 }
4392}
4393
4394void InstructionCodeGeneratorARMVIXL::VisitSub(HSub* sub) {
4395 LocationSummary* locations = sub->GetLocations();
4396 Location out = locations->Out();
4397 Location first = locations->InAt(0);
4398 Location second = locations->InAt(1);
4399 switch (sub->GetResultType()) {
4400 case Primitive::kPrimInt: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004401 __ Sub(OutputRegister(sub), InputRegisterAt(sub, 0), InputOperandAt(sub, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004402 break;
4403 }
4404
Scott Wakelingfe885462016-09-22 10:24:38 +01004405 case Primitive::kPrimLong: {
Anton Kirilovdda43962016-11-21 19:55:20 +00004406 if (second.IsConstant()) {
4407 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
4408 GenerateAddLongConst(out, first, -value);
4409 } else {
4410 DCHECK(second.IsRegisterPair());
4411 __ Subs(LowRegisterFrom(out), LowRegisterFrom(first), LowRegisterFrom(second));
4412 __ Sbc(HighRegisterFrom(out), HighRegisterFrom(first), HighRegisterFrom(second));
4413 }
Scott Wakelingfe885462016-09-22 10:24:38 +01004414 break;
4415 }
4416
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004417 case Primitive::kPrimFloat:
4418 case Primitive::kPrimDouble:
4419 __ Vsub(OutputVRegister(sub), InputVRegisterAt(sub, 0), InputVRegisterAt(sub, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004420 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01004421
4422 default:
4423 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
4424 }
4425}
4426
4427void LocationsBuilderARMVIXL::VisitMul(HMul* mul) {
4428 LocationSummary* locations =
4429 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
4430 switch (mul->GetResultType()) {
4431 case Primitive::kPrimInt:
4432 case Primitive::kPrimLong: {
4433 locations->SetInAt(0, Location::RequiresRegister());
4434 locations->SetInAt(1, Location::RequiresRegister());
4435 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4436 break;
4437 }
4438
4439 case Primitive::kPrimFloat:
4440 case Primitive::kPrimDouble: {
4441 locations->SetInAt(0, Location::RequiresFpuRegister());
4442 locations->SetInAt(1, Location::RequiresFpuRegister());
4443 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4444 break;
4445 }
4446
4447 default:
4448 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4449 }
4450}
4451
4452void InstructionCodeGeneratorARMVIXL::VisitMul(HMul* mul) {
4453 LocationSummary* locations = mul->GetLocations();
4454 Location out = locations->Out();
4455 Location first = locations->InAt(0);
4456 Location second = locations->InAt(1);
4457 switch (mul->GetResultType()) {
4458 case Primitive::kPrimInt: {
4459 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
4460 break;
4461 }
4462 case Primitive::kPrimLong: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004463 vixl32::Register out_hi = HighRegisterFrom(out);
4464 vixl32::Register out_lo = LowRegisterFrom(out);
4465 vixl32::Register in1_hi = HighRegisterFrom(first);
4466 vixl32::Register in1_lo = LowRegisterFrom(first);
4467 vixl32::Register in2_hi = HighRegisterFrom(second);
4468 vixl32::Register in2_lo = LowRegisterFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01004469
4470 // Extra checks to protect caused by the existence of R1_R2.
4471 // The algorithm is wrong if out.hi is either in1.lo or in2.lo:
4472 // (e.g. in1=r0_r1, in2=r2_r3 and out=r1_r2);
Anton Kirilov644032c2016-12-06 17:51:43 +00004473 DCHECK(!out_hi.Is(in1_lo));
4474 DCHECK(!out_hi.Is(in2_lo));
Scott Wakelingfe885462016-09-22 10:24:38 +01004475
4476 // input: in1 - 64 bits, in2 - 64 bits
4477 // output: out
4478 // formula: out.hi : out.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
4479 // parts: out.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
4480 // parts: out.lo = (in1.lo * in2.lo)[31:0]
4481
4482 UseScratchRegisterScope temps(GetVIXLAssembler());
4483 vixl32::Register temp = temps.Acquire();
4484 // temp <- in1.lo * in2.hi
4485 __ Mul(temp, in1_lo, in2_hi);
4486 // out.hi <- in1.lo * in2.hi + in1.hi * in2.lo
4487 __ Mla(out_hi, in1_hi, in2_lo, temp);
4488 // out.lo <- (in1.lo * in2.lo)[31:0];
4489 __ Umull(out_lo, temp, in1_lo, in2_lo);
4490 // out.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004491 __ Add(out_hi, out_hi, temp);
Scott Wakelingfe885462016-09-22 10:24:38 +01004492 break;
4493 }
4494
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004495 case Primitive::kPrimFloat:
4496 case Primitive::kPrimDouble:
4497 __ Vmul(OutputVRegister(mul), InputVRegisterAt(mul, 0), InputVRegisterAt(mul, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004498 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01004499
4500 default:
4501 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4502 }
4503}
4504
Scott Wakelingfe885462016-09-22 10:24:38 +01004505void InstructionCodeGeneratorARMVIXL::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
4506 DCHECK(instruction->IsDiv() || instruction->IsRem());
4507 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
4508
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004509 Location second = instruction->GetLocations()->InAt(1);
Scott Wakelingfe885462016-09-22 10:24:38 +01004510 DCHECK(second.IsConstant());
4511
4512 vixl32::Register out = OutputRegister(instruction);
4513 vixl32::Register dividend = InputRegisterAt(instruction, 0);
Anton Kirilov644032c2016-12-06 17:51:43 +00004514 int32_t imm = Int32ConstantFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01004515 DCHECK(imm == 1 || imm == -1);
4516
4517 if (instruction->IsRem()) {
4518 __ Mov(out, 0);
4519 } else {
4520 if (imm == 1) {
4521 __ Mov(out, dividend);
4522 } else {
4523 __ Rsb(out, dividend, 0);
4524 }
4525 }
4526}
4527
4528void InstructionCodeGeneratorARMVIXL::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
4529 DCHECK(instruction->IsDiv() || instruction->IsRem());
4530 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
4531
4532 LocationSummary* locations = instruction->GetLocations();
4533 Location second = locations->InAt(1);
4534 DCHECK(second.IsConstant());
4535
4536 vixl32::Register out = OutputRegister(instruction);
4537 vixl32::Register dividend = InputRegisterAt(instruction, 0);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004538 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
Anton Kirilov644032c2016-12-06 17:51:43 +00004539 int32_t imm = Int32ConstantFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01004540 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
4541 int ctz_imm = CTZ(abs_imm);
4542
4543 if (ctz_imm == 1) {
4544 __ Lsr(temp, dividend, 32 - ctz_imm);
4545 } else {
4546 __ Asr(temp, dividend, 31);
4547 __ Lsr(temp, temp, 32 - ctz_imm);
4548 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004549 __ Add(out, temp, dividend);
Scott Wakelingfe885462016-09-22 10:24:38 +01004550
4551 if (instruction->IsDiv()) {
4552 __ Asr(out, out, ctz_imm);
4553 if (imm < 0) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004554 __ Rsb(out, out, 0);
Scott Wakelingfe885462016-09-22 10:24:38 +01004555 }
4556 } else {
4557 __ Ubfx(out, out, 0, ctz_imm);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004558 __ Sub(out, out, temp);
Scott Wakelingfe885462016-09-22 10:24:38 +01004559 }
4560}
4561
4562void InstructionCodeGeneratorARMVIXL::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
4563 DCHECK(instruction->IsDiv() || instruction->IsRem());
4564 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
4565
4566 LocationSummary* locations = instruction->GetLocations();
4567 Location second = locations->InAt(1);
4568 DCHECK(second.IsConstant());
4569
4570 vixl32::Register out = OutputRegister(instruction);
4571 vixl32::Register dividend = InputRegisterAt(instruction, 0);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004572 vixl32::Register temp1 = RegisterFrom(locations->GetTemp(0));
4573 vixl32::Register temp2 = RegisterFrom(locations->GetTemp(1));
Scott Wakelingb77051e2016-11-21 19:46:00 +00004574 int32_t imm = Int32ConstantFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01004575
4576 int64_t magic;
4577 int shift;
4578 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
4579
Anton Kirilovdda43962016-11-21 19:55:20 +00004580 // TODO(VIXL): Change the static cast to Operand::From() after VIXL is fixed.
4581 __ Mov(temp1, static_cast<int32_t>(magic));
Scott Wakelingfe885462016-09-22 10:24:38 +01004582 __ Smull(temp2, temp1, dividend, temp1);
4583
4584 if (imm > 0 && magic < 0) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004585 __ Add(temp1, temp1, dividend);
Scott Wakelingfe885462016-09-22 10:24:38 +01004586 } else if (imm < 0 && magic > 0) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004587 __ Sub(temp1, temp1, dividend);
Scott Wakelingfe885462016-09-22 10:24:38 +01004588 }
4589
4590 if (shift != 0) {
4591 __ Asr(temp1, temp1, shift);
4592 }
4593
4594 if (instruction->IsDiv()) {
4595 __ Sub(out, temp1, Operand(temp1, vixl32::Shift(ASR), 31));
4596 } else {
4597 __ Sub(temp1, temp1, Operand(temp1, vixl32::Shift(ASR), 31));
4598 // TODO: Strength reduction for mls.
4599 __ Mov(temp2, imm);
4600 __ Mls(out, temp1, temp2, dividend);
4601 }
4602}
4603
4604void InstructionCodeGeneratorARMVIXL::GenerateDivRemConstantIntegral(
4605 HBinaryOperation* instruction) {
4606 DCHECK(instruction->IsDiv() || instruction->IsRem());
4607 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
4608
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004609 Location second = instruction->GetLocations()->InAt(1);
Scott Wakelingfe885462016-09-22 10:24:38 +01004610 DCHECK(second.IsConstant());
4611
Anton Kirilov644032c2016-12-06 17:51:43 +00004612 int32_t imm = Int32ConstantFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01004613 if (imm == 0) {
4614 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
4615 } else if (imm == 1 || imm == -1) {
4616 DivRemOneOrMinusOne(instruction);
4617 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
4618 DivRemByPowerOfTwo(instruction);
4619 } else {
4620 DCHECK(imm <= -2 || imm >= 2);
4621 GenerateDivRemWithAnyConstant(instruction);
4622 }
4623}
4624
4625void LocationsBuilderARMVIXL::VisitDiv(HDiv* div) {
4626 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
4627 if (div->GetResultType() == Primitive::kPrimLong) {
4628 // pLdiv runtime call.
4629 call_kind = LocationSummary::kCallOnMainOnly;
4630 } else if (div->GetResultType() == Primitive::kPrimInt && div->InputAt(1)->IsConstant()) {
4631 // sdiv will be replaced by other instruction sequence.
4632 } else if (div->GetResultType() == Primitive::kPrimInt &&
4633 !codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4634 // pIdivmod runtime call.
4635 call_kind = LocationSummary::kCallOnMainOnly;
4636 }
4637
4638 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
4639
4640 switch (div->GetResultType()) {
4641 case Primitive::kPrimInt: {
4642 if (div->InputAt(1)->IsConstant()) {
4643 locations->SetInAt(0, Location::RequiresRegister());
4644 locations->SetInAt(1, Location::ConstantLocation(div->InputAt(1)->AsConstant()));
4645 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Anton Kirilov644032c2016-12-06 17:51:43 +00004646 int32_t value = Int32ConstantFrom(div->InputAt(1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004647 if (value == 1 || value == 0 || value == -1) {
4648 // No temp register required.
4649 } else {
4650 locations->AddTemp(Location::RequiresRegister());
4651 if (!IsPowerOfTwo(AbsOrMin(value))) {
4652 locations->AddTemp(Location::RequiresRegister());
4653 }
4654 }
4655 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4656 locations->SetInAt(0, Location::RequiresRegister());
4657 locations->SetInAt(1, Location::RequiresRegister());
4658 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4659 } else {
Artem Serov551b28f2016-10-18 19:11:30 +01004660 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4661 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4662 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
Roland Levillain5e8d5f02016-10-18 18:03:43 +01004663 // Note: divmod will compute both the quotient and the remainder as the pair R0 and R1, but
Artem Serov551b28f2016-10-18 19:11:30 +01004664 // we only need the former.
4665 locations->SetOut(LocationFrom(r0));
Scott Wakelingfe885462016-09-22 10:24:38 +01004666 }
4667 break;
4668 }
4669 case Primitive::kPrimLong: {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01004670 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4671 locations->SetInAt(0, LocationFrom(
4672 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
4673 locations->SetInAt(1, LocationFrom(
4674 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
4675 locations->SetOut(LocationFrom(r0, r1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004676 break;
4677 }
4678 case Primitive::kPrimFloat:
4679 case Primitive::kPrimDouble: {
4680 locations->SetInAt(0, Location::RequiresFpuRegister());
4681 locations->SetInAt(1, Location::RequiresFpuRegister());
4682 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4683 break;
4684 }
4685
4686 default:
4687 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
4688 }
4689}
4690
4691void InstructionCodeGeneratorARMVIXL::VisitDiv(HDiv* div) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01004692 Location lhs = div->GetLocations()->InAt(0);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004693 Location rhs = div->GetLocations()->InAt(1);
Scott Wakelingfe885462016-09-22 10:24:38 +01004694
4695 switch (div->GetResultType()) {
4696 case Primitive::kPrimInt: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004697 if (rhs.IsConstant()) {
Scott Wakelingfe885462016-09-22 10:24:38 +01004698 GenerateDivRemConstantIntegral(div);
4699 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4700 __ Sdiv(OutputRegister(div), InputRegisterAt(div, 0), InputRegisterAt(div, 1));
4701 } else {
Artem Serov551b28f2016-10-18 19:11:30 +01004702 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4703 DCHECK(calling_convention.GetRegisterAt(0).Is(RegisterFrom(lhs)));
4704 DCHECK(calling_convention.GetRegisterAt(1).Is(RegisterFrom(rhs)));
4705 DCHECK(r0.Is(OutputRegister(div)));
4706
4707 codegen_->InvokeRuntime(kQuickIdivmod, div, div->GetDexPc());
4708 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
Scott Wakelingfe885462016-09-22 10:24:38 +01004709 }
4710 break;
4711 }
4712
4713 case Primitive::kPrimLong: {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01004714 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4715 DCHECK(calling_convention.GetRegisterAt(0).Is(LowRegisterFrom(lhs)));
4716 DCHECK(calling_convention.GetRegisterAt(1).Is(HighRegisterFrom(lhs)));
4717 DCHECK(calling_convention.GetRegisterAt(2).Is(LowRegisterFrom(rhs)));
4718 DCHECK(calling_convention.GetRegisterAt(3).Is(HighRegisterFrom(rhs)));
4719 DCHECK(LowRegisterFrom(div->GetLocations()->Out()).Is(r0));
4720 DCHECK(HighRegisterFrom(div->GetLocations()->Out()).Is(r1));
4721
4722 codegen_->InvokeRuntime(kQuickLdiv, div, div->GetDexPc());
4723 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Scott Wakelingfe885462016-09-22 10:24:38 +01004724 break;
4725 }
4726
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004727 case Primitive::kPrimFloat:
4728 case Primitive::kPrimDouble:
4729 __ Vdiv(OutputVRegister(div), InputVRegisterAt(div, 0), InputVRegisterAt(div, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004730 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01004731
4732 default:
4733 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
4734 }
4735}
4736
Artem Serov551b28f2016-10-18 19:11:30 +01004737void LocationsBuilderARMVIXL::VisitRem(HRem* rem) {
4738 Primitive::Type type = rem->GetResultType();
4739
4740 // Most remainders are implemented in the runtime.
4741 LocationSummary::CallKind call_kind = LocationSummary::kCallOnMainOnly;
4742 if (rem->GetResultType() == Primitive::kPrimInt && rem->InputAt(1)->IsConstant()) {
4743 // sdiv will be replaced by other instruction sequence.
4744 call_kind = LocationSummary::kNoCall;
4745 } else if ((rem->GetResultType() == Primitive::kPrimInt)
4746 && codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4747 // Have hardware divide instruction for int, do it with three instructions.
4748 call_kind = LocationSummary::kNoCall;
4749 }
4750
4751 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
4752
4753 switch (type) {
4754 case Primitive::kPrimInt: {
4755 if (rem->InputAt(1)->IsConstant()) {
4756 locations->SetInAt(0, Location::RequiresRegister());
4757 locations->SetInAt(1, Location::ConstantLocation(rem->InputAt(1)->AsConstant()));
4758 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Anton Kirilov644032c2016-12-06 17:51:43 +00004759 int32_t value = Int32ConstantFrom(rem->InputAt(1));
Artem Serov551b28f2016-10-18 19:11:30 +01004760 if (value == 1 || value == 0 || value == -1) {
4761 // No temp register required.
4762 } else {
4763 locations->AddTemp(Location::RequiresRegister());
4764 if (!IsPowerOfTwo(AbsOrMin(value))) {
4765 locations->AddTemp(Location::RequiresRegister());
4766 }
4767 }
4768 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4769 locations->SetInAt(0, Location::RequiresRegister());
4770 locations->SetInAt(1, Location::RequiresRegister());
4771 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4772 locations->AddTemp(Location::RequiresRegister());
4773 } else {
4774 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4775 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4776 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
Roland Levillain5e8d5f02016-10-18 18:03:43 +01004777 // Note: divmod will compute both the quotient and the remainder as the pair R0 and R1, but
Artem Serov551b28f2016-10-18 19:11:30 +01004778 // we only need the latter.
4779 locations->SetOut(LocationFrom(r1));
4780 }
4781 break;
4782 }
4783 case Primitive::kPrimLong: {
4784 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4785 locations->SetInAt(0, LocationFrom(
4786 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
4787 locations->SetInAt(1, LocationFrom(
4788 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
4789 // The runtime helper puts the output in R2,R3.
4790 locations->SetOut(LocationFrom(r2, r3));
4791 break;
4792 }
4793 case Primitive::kPrimFloat: {
4794 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4795 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
4796 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
4797 locations->SetOut(LocationFrom(s0));
4798 break;
4799 }
4800
4801 case Primitive::kPrimDouble: {
4802 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4803 locations->SetInAt(0, LocationFrom(
4804 calling_convention.GetFpuRegisterAt(0), calling_convention.GetFpuRegisterAt(1)));
4805 locations->SetInAt(1, LocationFrom(
4806 calling_convention.GetFpuRegisterAt(2), calling_convention.GetFpuRegisterAt(3)));
4807 locations->SetOut(LocationFrom(s0, s1));
4808 break;
4809 }
4810
4811 default:
4812 LOG(FATAL) << "Unexpected rem type " << type;
4813 }
4814}
4815
4816void InstructionCodeGeneratorARMVIXL::VisitRem(HRem* rem) {
4817 LocationSummary* locations = rem->GetLocations();
4818 Location second = locations->InAt(1);
4819
4820 Primitive::Type type = rem->GetResultType();
4821 switch (type) {
4822 case Primitive::kPrimInt: {
4823 vixl32::Register reg1 = InputRegisterAt(rem, 0);
4824 vixl32::Register out_reg = OutputRegister(rem);
4825 if (second.IsConstant()) {
4826 GenerateDivRemConstantIntegral(rem);
4827 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4828 vixl32::Register reg2 = RegisterFrom(second);
4829 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
4830
4831 // temp = reg1 / reg2 (integer division)
4832 // dest = reg1 - temp * reg2
4833 __ Sdiv(temp, reg1, reg2);
4834 __ Mls(out_reg, temp, reg2, reg1);
4835 } else {
4836 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4837 DCHECK(reg1.Is(calling_convention.GetRegisterAt(0)));
4838 DCHECK(RegisterFrom(second).Is(calling_convention.GetRegisterAt(1)));
4839 DCHECK(out_reg.Is(r1));
4840
4841 codegen_->InvokeRuntime(kQuickIdivmod, rem, rem->GetDexPc());
4842 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
4843 }
4844 break;
4845 }
4846
4847 case Primitive::kPrimLong: {
4848 codegen_->InvokeRuntime(kQuickLmod, rem, rem->GetDexPc());
4849 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
4850 break;
4851 }
4852
4853 case Primitive::kPrimFloat: {
4854 codegen_->InvokeRuntime(kQuickFmodf, rem, rem->GetDexPc());
4855 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
4856 break;
4857 }
4858
4859 case Primitive::kPrimDouble: {
4860 codegen_->InvokeRuntime(kQuickFmod, rem, rem->GetDexPc());
4861 CheckEntrypointTypes<kQuickFmod, double, double, double>();
4862 break;
4863 }
4864
4865 default:
4866 LOG(FATAL) << "Unexpected rem type " << type;
4867 }
4868}
4869
4870
Scott Wakelingfe885462016-09-22 10:24:38 +01004871void LocationsBuilderARMVIXL::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Artem Serov657022c2016-11-23 14:19:38 +00004872 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Scott Wakelingfe885462016-09-22 10:24:38 +01004873 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Scott Wakelingfe885462016-09-22 10:24:38 +01004874}
4875
4876void InstructionCodeGeneratorARMVIXL::VisitDivZeroCheck(HDivZeroCheck* instruction) {
4877 DivZeroCheckSlowPathARMVIXL* slow_path =
4878 new (GetGraph()->GetArena()) DivZeroCheckSlowPathARMVIXL(instruction);
4879 codegen_->AddSlowPath(slow_path);
4880
4881 LocationSummary* locations = instruction->GetLocations();
4882 Location value = locations->InAt(0);
4883
4884 switch (instruction->GetType()) {
4885 case Primitive::kPrimBoolean:
4886 case Primitive::kPrimByte:
4887 case Primitive::kPrimChar:
4888 case Primitive::kPrimShort:
4889 case Primitive::kPrimInt: {
4890 if (value.IsRegister()) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00004891 __ CompareAndBranchIfZero(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
Scott Wakelingfe885462016-09-22 10:24:38 +01004892 } else {
4893 DCHECK(value.IsConstant()) << value;
Anton Kirilov644032c2016-12-06 17:51:43 +00004894 if (Int32ConstantFrom(value) == 0) {
Scott Wakelingfe885462016-09-22 10:24:38 +01004895 __ B(slow_path->GetEntryLabel());
4896 }
4897 }
4898 break;
4899 }
4900 case Primitive::kPrimLong: {
4901 if (value.IsRegisterPair()) {
4902 UseScratchRegisterScope temps(GetVIXLAssembler());
4903 vixl32::Register temp = temps.Acquire();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004904 __ Orrs(temp, LowRegisterFrom(value), HighRegisterFrom(value));
Scott Wakelingfe885462016-09-22 10:24:38 +01004905 __ B(eq, slow_path->GetEntryLabel());
4906 } else {
4907 DCHECK(value.IsConstant()) << value;
Anton Kirilov644032c2016-12-06 17:51:43 +00004908 if (Int64ConstantFrom(value) == 0) {
Scott Wakelingfe885462016-09-22 10:24:38 +01004909 __ B(slow_path->GetEntryLabel());
4910 }
4911 }
4912 break;
4913 }
4914 default:
4915 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
4916 }
4917}
4918
Artem Serov02109dd2016-09-23 17:17:54 +01004919void InstructionCodeGeneratorARMVIXL::HandleIntegerRotate(HRor* ror) {
4920 LocationSummary* locations = ror->GetLocations();
4921 vixl32::Register in = InputRegisterAt(ror, 0);
4922 Location rhs = locations->InAt(1);
4923 vixl32::Register out = OutputRegister(ror);
4924
4925 if (rhs.IsConstant()) {
4926 // Arm32 and Thumb2 assemblers require a rotation on the interval [1,31],
4927 // so map all rotations to a +ve. equivalent in that range.
4928 // (e.g. left *or* right by -2 bits == 30 bits in the same direction.)
4929 uint32_t rot = CodeGenerator::GetInt32ValueOf(rhs.GetConstant()) & 0x1F;
4930 if (rot) {
4931 // Rotate, mapping left rotations to right equivalents if necessary.
4932 // (e.g. left by 2 bits == right by 30.)
4933 __ Ror(out, in, rot);
4934 } else if (!out.Is(in)) {
4935 __ Mov(out, in);
4936 }
4937 } else {
4938 __ Ror(out, in, RegisterFrom(rhs));
4939 }
4940}
4941
4942// Gain some speed by mapping all Long rotates onto equivalent pairs of Integer
4943// rotates by swapping input regs (effectively rotating by the first 32-bits of
4944// a larger rotation) or flipping direction (thus treating larger right/left
4945// rotations as sub-word sized rotations in the other direction) as appropriate.
4946void InstructionCodeGeneratorARMVIXL::HandleLongRotate(HRor* ror) {
4947 LocationSummary* locations = ror->GetLocations();
4948 vixl32::Register in_reg_lo = LowRegisterFrom(locations->InAt(0));
4949 vixl32::Register in_reg_hi = HighRegisterFrom(locations->InAt(0));
4950 Location rhs = locations->InAt(1);
4951 vixl32::Register out_reg_lo = LowRegisterFrom(locations->Out());
4952 vixl32::Register out_reg_hi = HighRegisterFrom(locations->Out());
4953
4954 if (rhs.IsConstant()) {
4955 uint64_t rot = CodeGenerator::GetInt64ValueOf(rhs.GetConstant());
4956 // Map all rotations to +ve. equivalents on the interval [0,63].
4957 rot &= kMaxLongShiftDistance;
4958 // For rotates over a word in size, 'pre-rotate' by 32-bits to keep rotate
4959 // logic below to a simple pair of binary orr.
4960 // (e.g. 34 bits == in_reg swap + 2 bits right.)
4961 if (rot >= kArmBitsPerWord) {
4962 rot -= kArmBitsPerWord;
4963 std::swap(in_reg_hi, in_reg_lo);
4964 }
4965 // Rotate, or mov to out for zero or word size rotations.
4966 if (rot != 0u) {
Scott Wakelingb77051e2016-11-21 19:46:00 +00004967 __ Lsr(out_reg_hi, in_reg_hi, Operand::From(rot));
Artem Serov02109dd2016-09-23 17:17:54 +01004968 __ Orr(out_reg_hi, out_reg_hi, Operand(in_reg_lo, ShiftType::LSL, kArmBitsPerWord - rot));
Scott Wakelingb77051e2016-11-21 19:46:00 +00004969 __ Lsr(out_reg_lo, in_reg_lo, Operand::From(rot));
Artem Serov02109dd2016-09-23 17:17:54 +01004970 __ Orr(out_reg_lo, out_reg_lo, Operand(in_reg_hi, ShiftType::LSL, kArmBitsPerWord - rot));
4971 } else {
4972 __ Mov(out_reg_lo, in_reg_lo);
4973 __ Mov(out_reg_hi, in_reg_hi);
4974 }
4975 } else {
4976 vixl32::Register shift_right = RegisterFrom(locations->GetTemp(0));
4977 vixl32::Register shift_left = RegisterFrom(locations->GetTemp(1));
4978 vixl32::Label end;
4979 vixl32::Label shift_by_32_plus_shift_right;
Anton Kirilov6f644202017-02-27 18:29:45 +00004980 vixl32::Label* final_label = codegen_->GetFinalLabel(ror, &end);
Artem Serov02109dd2016-09-23 17:17:54 +01004981
4982 __ And(shift_right, RegisterFrom(rhs), 0x1F);
4983 __ Lsrs(shift_left, RegisterFrom(rhs), 6);
Scott Wakelingbffdc702016-12-07 17:46:03 +00004984 __ Rsb(LeaveFlags, shift_left, shift_right, Operand::From(kArmBitsPerWord));
Artem Serov517d9f62016-12-12 15:51:15 +00004985 __ B(cc, &shift_by_32_plus_shift_right, /* far_target */ false);
Artem Serov02109dd2016-09-23 17:17:54 +01004986
4987 // out_reg_hi = (reg_hi << shift_left) | (reg_lo >> shift_right).
4988 // out_reg_lo = (reg_lo << shift_left) | (reg_hi >> shift_right).
4989 __ Lsl(out_reg_hi, in_reg_hi, shift_left);
4990 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
4991 __ Add(out_reg_hi, out_reg_hi, out_reg_lo);
4992 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
4993 __ Lsr(shift_left, in_reg_hi, shift_right);
4994 __ Add(out_reg_lo, out_reg_lo, shift_left);
Anton Kirilov6f644202017-02-27 18:29:45 +00004995 __ B(final_label);
Artem Serov02109dd2016-09-23 17:17:54 +01004996
4997 __ Bind(&shift_by_32_plus_shift_right); // Shift by 32+shift_right.
4998 // out_reg_hi = (reg_hi >> shift_right) | (reg_lo << shift_left).
4999 // out_reg_lo = (reg_lo >> shift_right) | (reg_hi << shift_left).
5000 __ Lsr(out_reg_hi, in_reg_hi, shift_right);
5001 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
5002 __ Add(out_reg_hi, out_reg_hi, out_reg_lo);
5003 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
5004 __ Lsl(shift_right, in_reg_hi, shift_left);
5005 __ Add(out_reg_lo, out_reg_lo, shift_right);
5006
Anton Kirilov6f644202017-02-27 18:29:45 +00005007 if (end.IsReferenced()) {
5008 __ Bind(&end);
5009 }
Artem Serov02109dd2016-09-23 17:17:54 +01005010 }
5011}
5012
5013void LocationsBuilderARMVIXL::VisitRor(HRor* ror) {
5014 LocationSummary* locations =
5015 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
5016 switch (ror->GetResultType()) {
5017 case Primitive::kPrimInt: {
5018 locations->SetInAt(0, Location::RequiresRegister());
5019 locations->SetInAt(1, Location::RegisterOrConstant(ror->InputAt(1)));
5020 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5021 break;
5022 }
5023 case Primitive::kPrimLong: {
5024 locations->SetInAt(0, Location::RequiresRegister());
5025 if (ror->InputAt(1)->IsConstant()) {
5026 locations->SetInAt(1, Location::ConstantLocation(ror->InputAt(1)->AsConstant()));
5027 } else {
5028 locations->SetInAt(1, Location::RequiresRegister());
5029 locations->AddTemp(Location::RequiresRegister());
5030 locations->AddTemp(Location::RequiresRegister());
5031 }
5032 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
5033 break;
5034 }
5035 default:
5036 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
5037 }
5038}
5039
5040void InstructionCodeGeneratorARMVIXL::VisitRor(HRor* ror) {
5041 Primitive::Type type = ror->GetResultType();
5042 switch (type) {
5043 case Primitive::kPrimInt: {
5044 HandleIntegerRotate(ror);
5045 break;
5046 }
5047 case Primitive::kPrimLong: {
5048 HandleLongRotate(ror);
5049 break;
5050 }
5051 default:
5052 LOG(FATAL) << "Unexpected operation type " << type;
5053 UNREACHABLE();
5054 }
5055}
5056
Artem Serov02d37832016-10-25 15:25:33 +01005057void LocationsBuilderARMVIXL::HandleShift(HBinaryOperation* op) {
5058 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
5059
5060 LocationSummary* locations =
5061 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
5062
5063 switch (op->GetResultType()) {
5064 case Primitive::kPrimInt: {
5065 locations->SetInAt(0, Location::RequiresRegister());
5066 if (op->InputAt(1)->IsConstant()) {
5067 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
5068 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5069 } else {
5070 locations->SetInAt(1, Location::RequiresRegister());
5071 // Make the output overlap, as it will be used to hold the masked
5072 // second input.
5073 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
5074 }
5075 break;
5076 }
5077 case Primitive::kPrimLong: {
5078 locations->SetInAt(0, Location::RequiresRegister());
5079 if (op->InputAt(1)->IsConstant()) {
5080 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
5081 // For simplicity, use kOutputOverlap even though we only require that low registers
5082 // don't clash with high registers which the register allocator currently guarantees.
5083 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
5084 } else {
5085 locations->SetInAt(1, Location::RequiresRegister());
5086 locations->AddTemp(Location::RequiresRegister());
5087 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
5088 }
5089 break;
5090 }
5091 default:
5092 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
5093 }
5094}
5095
5096void InstructionCodeGeneratorARMVIXL::HandleShift(HBinaryOperation* op) {
5097 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
5098
5099 LocationSummary* locations = op->GetLocations();
5100 Location out = locations->Out();
5101 Location first = locations->InAt(0);
5102 Location second = locations->InAt(1);
5103
5104 Primitive::Type type = op->GetResultType();
5105 switch (type) {
5106 case Primitive::kPrimInt: {
5107 vixl32::Register out_reg = OutputRegister(op);
5108 vixl32::Register first_reg = InputRegisterAt(op, 0);
5109 if (second.IsRegister()) {
5110 vixl32::Register second_reg = RegisterFrom(second);
5111 // ARM doesn't mask the shift count so we need to do it ourselves.
5112 __ And(out_reg, second_reg, kMaxIntShiftDistance);
5113 if (op->IsShl()) {
5114 __ Lsl(out_reg, first_reg, out_reg);
5115 } else if (op->IsShr()) {
5116 __ Asr(out_reg, first_reg, out_reg);
5117 } else {
5118 __ Lsr(out_reg, first_reg, out_reg);
5119 }
5120 } else {
Anton Kirilov644032c2016-12-06 17:51:43 +00005121 int32_t cst = Int32ConstantFrom(second);
Artem Serov02d37832016-10-25 15:25:33 +01005122 uint32_t shift_value = cst & kMaxIntShiftDistance;
5123 if (shift_value == 0) { // ARM does not support shifting with 0 immediate.
5124 __ Mov(out_reg, first_reg);
5125 } else if (op->IsShl()) {
5126 __ Lsl(out_reg, first_reg, shift_value);
5127 } else if (op->IsShr()) {
5128 __ Asr(out_reg, first_reg, shift_value);
5129 } else {
5130 __ Lsr(out_reg, first_reg, shift_value);
5131 }
5132 }
5133 break;
5134 }
5135 case Primitive::kPrimLong: {
5136 vixl32::Register o_h = HighRegisterFrom(out);
5137 vixl32::Register o_l = LowRegisterFrom(out);
5138
5139 vixl32::Register high = HighRegisterFrom(first);
5140 vixl32::Register low = LowRegisterFrom(first);
5141
5142 if (second.IsRegister()) {
5143 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
5144
5145 vixl32::Register second_reg = RegisterFrom(second);
5146
5147 if (op->IsShl()) {
5148 __ And(o_l, second_reg, kMaxLongShiftDistance);
5149 // Shift the high part
5150 __ Lsl(o_h, high, o_l);
5151 // Shift the low part and `or` what overflew on the high part
Scott Wakelingb77051e2016-11-21 19:46:00 +00005152 __ Rsb(temp, o_l, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01005153 __ Lsr(temp, low, temp);
5154 __ Orr(o_h, o_h, temp);
5155 // If the shift is > 32 bits, override the high part
Scott Wakelingb77051e2016-11-21 19:46:00 +00005156 __ Subs(temp, o_l, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01005157 {
Artem Serov0fb37192016-12-06 18:13:40 +00005158 ExactAssemblyScope guard(GetVIXLAssembler(),
5159 2 * vixl32::kMaxInstructionSizeInBytes,
5160 CodeBufferCheckScope::kMaximumSize);
Artem Serov02d37832016-10-25 15:25:33 +01005161 __ it(pl);
5162 __ lsl(pl, o_h, low, temp);
5163 }
5164 // Shift the low part
5165 __ Lsl(o_l, low, o_l);
5166 } else if (op->IsShr()) {
5167 __ And(o_h, second_reg, kMaxLongShiftDistance);
5168 // Shift the low part
5169 __ Lsr(o_l, low, o_h);
5170 // Shift the high part and `or` what underflew on the low part
Scott Wakelingb77051e2016-11-21 19:46:00 +00005171 __ Rsb(temp, o_h, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01005172 __ Lsl(temp, high, temp);
5173 __ Orr(o_l, o_l, temp);
5174 // If the shift is > 32 bits, override the low part
Scott Wakelingb77051e2016-11-21 19:46:00 +00005175 __ Subs(temp, o_h, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01005176 {
Artem Serov0fb37192016-12-06 18:13:40 +00005177 ExactAssemblyScope guard(GetVIXLAssembler(),
5178 2 * vixl32::kMaxInstructionSizeInBytes,
5179 CodeBufferCheckScope::kMaximumSize);
Artem Serov02d37832016-10-25 15:25:33 +01005180 __ it(pl);
5181 __ asr(pl, o_l, high, temp);
5182 }
5183 // Shift the high part
5184 __ Asr(o_h, high, o_h);
5185 } else {
5186 __ And(o_h, second_reg, kMaxLongShiftDistance);
5187 // same as Shr except we use `Lsr`s and not `Asr`s
5188 __ Lsr(o_l, low, o_h);
Scott Wakelingb77051e2016-11-21 19:46:00 +00005189 __ Rsb(temp, o_h, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01005190 __ Lsl(temp, high, temp);
5191 __ Orr(o_l, o_l, temp);
Scott Wakelingb77051e2016-11-21 19:46:00 +00005192 __ Subs(temp, o_h, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01005193 {
Artem Serov0fb37192016-12-06 18:13:40 +00005194 ExactAssemblyScope guard(GetVIXLAssembler(),
5195 2 * vixl32::kMaxInstructionSizeInBytes,
5196 CodeBufferCheckScope::kMaximumSize);
Artem Serov02d37832016-10-25 15:25:33 +01005197 __ it(pl);
5198 __ lsr(pl, o_l, high, temp);
5199 }
5200 __ Lsr(o_h, high, o_h);
5201 }
5202 } else {
5203 // Register allocator doesn't create partial overlap.
5204 DCHECK(!o_l.Is(high));
5205 DCHECK(!o_h.Is(low));
Anton Kirilov644032c2016-12-06 17:51:43 +00005206 int32_t cst = Int32ConstantFrom(second);
Artem Serov02d37832016-10-25 15:25:33 +01005207 uint32_t shift_value = cst & kMaxLongShiftDistance;
5208 if (shift_value > 32) {
5209 if (op->IsShl()) {
5210 __ Lsl(o_h, low, shift_value - 32);
5211 __ Mov(o_l, 0);
5212 } else if (op->IsShr()) {
5213 __ Asr(o_l, high, shift_value - 32);
5214 __ Asr(o_h, high, 31);
5215 } else {
5216 __ Lsr(o_l, high, shift_value - 32);
5217 __ Mov(o_h, 0);
5218 }
5219 } else if (shift_value == 32) {
5220 if (op->IsShl()) {
5221 __ Mov(o_h, low);
5222 __ Mov(o_l, 0);
5223 } else if (op->IsShr()) {
5224 __ Mov(o_l, high);
5225 __ Asr(o_h, high, 31);
5226 } else {
5227 __ Mov(o_l, high);
5228 __ Mov(o_h, 0);
5229 }
5230 } else if (shift_value == 1) {
5231 if (op->IsShl()) {
5232 __ Lsls(o_l, low, 1);
5233 __ Adc(o_h, high, high);
5234 } else if (op->IsShr()) {
5235 __ Asrs(o_h, high, 1);
5236 __ Rrx(o_l, low);
5237 } else {
5238 __ Lsrs(o_h, high, 1);
5239 __ Rrx(o_l, low);
5240 }
5241 } else {
5242 DCHECK(2 <= shift_value && shift_value < 32) << shift_value;
5243 if (op->IsShl()) {
5244 __ Lsl(o_h, high, shift_value);
5245 __ Orr(o_h, o_h, Operand(low, ShiftType::LSR, 32 - shift_value));
5246 __ Lsl(o_l, low, shift_value);
5247 } else if (op->IsShr()) {
5248 __ Lsr(o_l, low, shift_value);
5249 __ Orr(o_l, o_l, Operand(high, ShiftType::LSL, 32 - shift_value));
5250 __ Asr(o_h, high, shift_value);
5251 } else {
5252 __ Lsr(o_l, low, shift_value);
5253 __ Orr(o_l, o_l, Operand(high, ShiftType::LSL, 32 - shift_value));
5254 __ Lsr(o_h, high, shift_value);
5255 }
5256 }
5257 }
5258 break;
5259 }
5260 default:
5261 LOG(FATAL) << "Unexpected operation type " << type;
5262 UNREACHABLE();
5263 }
5264}
5265
5266void LocationsBuilderARMVIXL::VisitShl(HShl* shl) {
5267 HandleShift(shl);
5268}
5269
5270void InstructionCodeGeneratorARMVIXL::VisitShl(HShl* shl) {
5271 HandleShift(shl);
5272}
5273
5274void LocationsBuilderARMVIXL::VisitShr(HShr* shr) {
5275 HandleShift(shr);
5276}
5277
5278void InstructionCodeGeneratorARMVIXL::VisitShr(HShr* shr) {
5279 HandleShift(shr);
5280}
5281
5282void LocationsBuilderARMVIXL::VisitUShr(HUShr* ushr) {
5283 HandleShift(ushr);
5284}
5285
5286void InstructionCodeGeneratorARMVIXL::VisitUShr(HUShr* ushr) {
5287 HandleShift(ushr);
5288}
5289
5290void LocationsBuilderARMVIXL::VisitNewInstance(HNewInstance* instruction) {
5291 LocationSummary* locations =
5292 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
5293 if (instruction->IsStringAlloc()) {
5294 locations->AddTemp(LocationFrom(kMethodRegister));
5295 } else {
5296 InvokeRuntimeCallingConventionARMVIXL calling_convention;
5297 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
Artem Serov02d37832016-10-25 15:25:33 +01005298 }
5299 locations->SetOut(LocationFrom(r0));
5300}
5301
5302void InstructionCodeGeneratorARMVIXL::VisitNewInstance(HNewInstance* instruction) {
5303 // Note: if heap poisoning is enabled, the entry point takes cares
5304 // of poisoning the reference.
5305 if (instruction->IsStringAlloc()) {
5306 // String is allocated through StringFactory. Call NewEmptyString entry point.
5307 vixl32::Register temp = RegisterFrom(instruction->GetLocations()->GetTemp(0));
5308 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize);
5309 GetAssembler()->LoadFromOffset(kLoadWord, temp, tr, QUICK_ENTRY_POINT(pNewEmptyString));
5310 GetAssembler()->LoadFromOffset(kLoadWord, lr, temp, code_offset.Int32Value());
Alexandre Rames374ddf32016-11-04 10:40:49 +00005311 // 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 +00005312 ExactAssemblyScope aas(GetVIXLAssembler(),
5313 vixl32::k16BitT32InstructionSizeInBytes,
5314 CodeBufferCheckScope::kExactSize);
Artem Serov02d37832016-10-25 15:25:33 +01005315 __ blx(lr);
5316 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
5317 } else {
5318 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00005319 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
Artem Serov02d37832016-10-25 15:25:33 +01005320 }
5321}
5322
5323void LocationsBuilderARMVIXL::VisitNewArray(HNewArray* instruction) {
5324 LocationSummary* locations =
5325 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
5326 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Artem Serov02d37832016-10-25 15:25:33 +01005327 locations->SetOut(LocationFrom(r0));
Nicolas Geoffray8c7c4f12017-01-26 10:13:11 +00005328 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5329 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
Artem Serov02d37832016-10-25 15:25:33 +01005330}
5331
5332void InstructionCodeGeneratorARMVIXL::VisitNewArray(HNewArray* instruction) {
Artem Serov02d37832016-10-25 15:25:33 +01005333 // Note: if heap poisoning is enabled, the entry point takes cares
5334 // of poisoning the reference.
Artem Serov7b3672e2017-02-03 17:30:34 +00005335 QuickEntrypointEnum entrypoint =
5336 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
5337 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005338 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Artem Serov7b3672e2017-02-03 17:30:34 +00005339 DCHECK(!codegen_->IsLeafMethod());
Artem Serov02d37832016-10-25 15:25:33 +01005340}
5341
5342void LocationsBuilderARMVIXL::VisitParameterValue(HParameterValue* instruction) {
5343 LocationSummary* locations =
5344 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5345 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
5346 if (location.IsStackSlot()) {
5347 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5348 } else if (location.IsDoubleStackSlot()) {
5349 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5350 }
5351 locations->SetOut(location);
5352}
5353
5354void InstructionCodeGeneratorARMVIXL::VisitParameterValue(
5355 HParameterValue* instruction ATTRIBUTE_UNUSED) {
5356 // Nothing to do, the parameter is already at its location.
5357}
5358
5359void LocationsBuilderARMVIXL::VisitCurrentMethod(HCurrentMethod* instruction) {
5360 LocationSummary* locations =
5361 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5362 locations->SetOut(LocationFrom(kMethodRegister));
5363}
5364
5365void InstructionCodeGeneratorARMVIXL::VisitCurrentMethod(
5366 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
5367 // Nothing to do, the method is already at its location.
5368}
5369
5370void LocationsBuilderARMVIXL::VisitNot(HNot* not_) {
5371 LocationSummary* locations =
5372 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
5373 locations->SetInAt(0, Location::RequiresRegister());
5374 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5375}
5376
5377void InstructionCodeGeneratorARMVIXL::VisitNot(HNot* not_) {
5378 LocationSummary* locations = not_->GetLocations();
5379 Location out = locations->Out();
5380 Location in = locations->InAt(0);
5381 switch (not_->GetResultType()) {
5382 case Primitive::kPrimInt:
5383 __ Mvn(OutputRegister(not_), InputRegisterAt(not_, 0));
5384 break;
5385
5386 case Primitive::kPrimLong:
5387 __ Mvn(LowRegisterFrom(out), LowRegisterFrom(in));
5388 __ Mvn(HighRegisterFrom(out), HighRegisterFrom(in));
5389 break;
5390
5391 default:
5392 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
5393 }
5394}
5395
Scott Wakelingc34dba72016-10-03 10:14:44 +01005396void LocationsBuilderARMVIXL::VisitBooleanNot(HBooleanNot* bool_not) {
5397 LocationSummary* locations =
5398 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
5399 locations->SetInAt(0, Location::RequiresRegister());
5400 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5401}
5402
5403void InstructionCodeGeneratorARMVIXL::VisitBooleanNot(HBooleanNot* bool_not) {
5404 __ Eor(OutputRegister(bool_not), InputRegister(bool_not), 1);
5405}
5406
Artem Serov02d37832016-10-25 15:25:33 +01005407void LocationsBuilderARMVIXL::VisitCompare(HCompare* compare) {
5408 LocationSummary* locations =
5409 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
5410 switch (compare->InputAt(0)->GetType()) {
5411 case Primitive::kPrimBoolean:
5412 case Primitive::kPrimByte:
5413 case Primitive::kPrimShort:
5414 case Primitive::kPrimChar:
5415 case Primitive::kPrimInt:
5416 case Primitive::kPrimLong: {
5417 locations->SetInAt(0, Location::RequiresRegister());
5418 locations->SetInAt(1, Location::RequiresRegister());
5419 // Output overlaps because it is written before doing the low comparison.
5420 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
5421 break;
5422 }
5423 case Primitive::kPrimFloat:
5424 case Primitive::kPrimDouble: {
5425 locations->SetInAt(0, Location::RequiresFpuRegister());
5426 locations->SetInAt(1, ArithmeticZeroOrFpuRegister(compare->InputAt(1)));
5427 locations->SetOut(Location::RequiresRegister());
5428 break;
5429 }
5430 default:
5431 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
5432 }
5433}
5434
5435void InstructionCodeGeneratorARMVIXL::VisitCompare(HCompare* compare) {
5436 LocationSummary* locations = compare->GetLocations();
5437 vixl32::Register out = OutputRegister(compare);
5438 Location left = locations->InAt(0);
5439 Location right = locations->InAt(1);
5440
5441 vixl32::Label less, greater, done;
Anton Kirilov6f644202017-02-27 18:29:45 +00005442 vixl32::Label* final_label = codegen_->GetFinalLabel(compare, &done);
Artem Serov02d37832016-10-25 15:25:33 +01005443 Primitive::Type type = compare->InputAt(0)->GetType();
5444 vixl32::Condition less_cond = vixl32::Condition(kNone);
5445 switch (type) {
5446 case Primitive::kPrimBoolean:
5447 case Primitive::kPrimByte:
5448 case Primitive::kPrimShort:
5449 case Primitive::kPrimChar:
5450 case Primitive::kPrimInt: {
5451 // Emit move to `out` before the `Cmp`, as `Mov` might affect the status flags.
5452 __ Mov(out, 0);
5453 __ Cmp(RegisterFrom(left), RegisterFrom(right)); // Signed compare.
5454 less_cond = lt;
5455 break;
5456 }
5457 case Primitive::kPrimLong: {
5458 __ Cmp(HighRegisterFrom(left), HighRegisterFrom(right)); // Signed compare.
Artem Serov517d9f62016-12-12 15:51:15 +00005459 __ B(lt, &less, /* far_target */ false);
5460 __ B(gt, &greater, /* far_target */ false);
Artem Serov02d37832016-10-25 15:25:33 +01005461 // Emit move to `out` before the last `Cmp`, as `Mov` might affect the status flags.
5462 __ Mov(out, 0);
5463 __ Cmp(LowRegisterFrom(left), LowRegisterFrom(right)); // Unsigned compare.
5464 less_cond = lo;
5465 break;
5466 }
5467 case Primitive::kPrimFloat:
5468 case Primitive::kPrimDouble: {
5469 __ Mov(out, 0);
Donghui Bai426b49c2016-11-08 14:55:38 +08005470 GenerateVcmp(compare, codegen_);
Artem Serov02d37832016-10-25 15:25:33 +01005471 // To branch on the FP compare result we transfer FPSCR to APSR (encoded as PC in VMRS).
5472 __ Vmrs(RegisterOrAPSR_nzcv(kPcCode), FPSCR);
5473 less_cond = ARMFPCondition(kCondLT, compare->IsGtBias());
5474 break;
5475 }
5476 default:
5477 LOG(FATAL) << "Unexpected compare type " << type;
5478 UNREACHABLE();
5479 }
5480
Anton Kirilov6f644202017-02-27 18:29:45 +00005481 __ B(eq, final_label, /* far_target */ false);
Artem Serov517d9f62016-12-12 15:51:15 +00005482 __ B(less_cond, &less, /* far_target */ false);
Artem Serov02d37832016-10-25 15:25:33 +01005483
5484 __ Bind(&greater);
5485 __ Mov(out, 1);
Anton Kirilov6f644202017-02-27 18:29:45 +00005486 __ B(final_label);
Artem Serov02d37832016-10-25 15:25:33 +01005487
5488 __ Bind(&less);
5489 __ Mov(out, -1);
5490
Anton Kirilov6f644202017-02-27 18:29:45 +00005491 if (done.IsReferenced()) {
5492 __ Bind(&done);
5493 }
Artem Serov02d37832016-10-25 15:25:33 +01005494}
5495
5496void LocationsBuilderARMVIXL::VisitPhi(HPhi* instruction) {
5497 LocationSummary* locations =
5498 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5499 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
5500 locations->SetInAt(i, Location::Any());
5501 }
5502 locations->SetOut(Location::Any());
5503}
5504
5505void InstructionCodeGeneratorARMVIXL::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
5506 LOG(FATAL) << "Unreachable";
5507}
5508
5509void CodeGeneratorARMVIXL::GenerateMemoryBarrier(MemBarrierKind kind) {
5510 // TODO (ported from quick): revisit ARM barrier kinds.
5511 DmbOptions flavor = DmbOptions::ISH; // Quiet C++ warnings.
5512 switch (kind) {
5513 case MemBarrierKind::kAnyStore:
5514 case MemBarrierKind::kLoadAny:
5515 case MemBarrierKind::kAnyAny: {
5516 flavor = DmbOptions::ISH;
5517 break;
5518 }
5519 case MemBarrierKind::kStoreStore: {
5520 flavor = DmbOptions::ISHST;
5521 break;
5522 }
5523 default:
5524 LOG(FATAL) << "Unexpected memory barrier " << kind;
5525 }
5526 __ Dmb(flavor);
5527}
5528
5529void InstructionCodeGeneratorARMVIXL::GenerateWideAtomicLoad(vixl32::Register addr,
5530 uint32_t offset,
5531 vixl32::Register out_lo,
5532 vixl32::Register out_hi) {
5533 UseScratchRegisterScope temps(GetVIXLAssembler());
5534 if (offset != 0) {
5535 vixl32::Register temp = temps.Acquire();
5536 __ Add(temp, addr, offset);
5537 addr = temp;
5538 }
Scott Wakelingb77051e2016-11-21 19:46:00 +00005539 __ Ldrexd(out_lo, out_hi, MemOperand(addr));
Artem Serov02d37832016-10-25 15:25:33 +01005540}
5541
5542void InstructionCodeGeneratorARMVIXL::GenerateWideAtomicStore(vixl32::Register addr,
5543 uint32_t offset,
5544 vixl32::Register value_lo,
5545 vixl32::Register value_hi,
5546 vixl32::Register temp1,
5547 vixl32::Register temp2,
5548 HInstruction* instruction) {
5549 UseScratchRegisterScope temps(GetVIXLAssembler());
5550 vixl32::Label fail;
5551 if (offset != 0) {
5552 vixl32::Register temp = temps.Acquire();
5553 __ Add(temp, addr, offset);
5554 addr = temp;
5555 }
5556 __ Bind(&fail);
Alexandre Rames374ddf32016-11-04 10:40:49 +00005557 {
5558 // Ensure the pc position is recorded immediately after the `ldrexd` instruction.
Artem Serov0fb37192016-12-06 18:13:40 +00005559 ExactAssemblyScope aas(GetVIXLAssembler(),
5560 vixl32::kMaxInstructionSizeInBytes,
5561 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00005562 // We need a load followed by store. (The address used in a STREX instruction must
5563 // be the same as the address in the most recently executed LDREX instruction.)
5564 __ ldrexd(temp1, temp2, MemOperand(addr));
5565 codegen_->MaybeRecordImplicitNullCheck(instruction);
5566 }
Scott Wakelingb77051e2016-11-21 19:46:00 +00005567 __ Strexd(temp1, value_lo, value_hi, MemOperand(addr));
xueliang.zhongf51bc622016-11-04 09:23:32 +00005568 __ CompareAndBranchIfNonZero(temp1, &fail);
Artem Serov02d37832016-10-25 15:25:33 +01005569}
Artem Serov02109dd2016-09-23 17:17:54 +01005570
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005571void LocationsBuilderARMVIXL::HandleFieldSet(
5572 HInstruction* instruction, const FieldInfo& field_info) {
5573 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
5574
5575 LocationSummary* locations =
5576 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5577 locations->SetInAt(0, Location::RequiresRegister());
5578
5579 Primitive::Type field_type = field_info.GetFieldType();
5580 if (Primitive::IsFloatingPointType(field_type)) {
5581 locations->SetInAt(1, Location::RequiresFpuRegister());
5582 } else {
5583 locations->SetInAt(1, Location::RequiresRegister());
5584 }
5585
5586 bool is_wide = field_type == Primitive::kPrimLong || field_type == Primitive::kPrimDouble;
5587 bool generate_volatile = field_info.IsVolatile()
5588 && is_wide
5589 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
5590 bool needs_write_barrier =
5591 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
5592 // Temporary registers for the write barrier.
5593 // TODO: consider renaming StoreNeedsWriteBarrier to StoreNeedsGCMark.
5594 if (needs_write_barrier) {
5595 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
5596 locations->AddTemp(Location::RequiresRegister());
5597 } else if (generate_volatile) {
5598 // ARM encoding have some additional constraints for ldrexd/strexd:
5599 // - registers need to be consecutive
5600 // - the first register should be even but not R14.
5601 // We don't test for ARM yet, and the assertion makes sure that we
5602 // revisit this if we ever enable ARM encoding.
5603 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
5604
5605 locations->AddTemp(Location::RequiresRegister());
5606 locations->AddTemp(Location::RequiresRegister());
5607 if (field_type == Primitive::kPrimDouble) {
5608 // For doubles we need two more registers to copy the value.
5609 locations->AddTemp(LocationFrom(r2));
5610 locations->AddTemp(LocationFrom(r3));
5611 }
5612 }
5613}
5614
5615void InstructionCodeGeneratorARMVIXL::HandleFieldSet(HInstruction* instruction,
5616 const FieldInfo& field_info,
5617 bool value_can_be_null) {
5618 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
5619
5620 LocationSummary* locations = instruction->GetLocations();
5621 vixl32::Register base = InputRegisterAt(instruction, 0);
5622 Location value = locations->InAt(1);
5623
5624 bool is_volatile = field_info.IsVolatile();
5625 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
5626 Primitive::Type field_type = field_info.GetFieldType();
5627 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
5628 bool needs_write_barrier =
5629 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
5630
5631 if (is_volatile) {
5632 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
5633 }
5634
5635 switch (field_type) {
5636 case Primitive::kPrimBoolean:
5637 case Primitive::kPrimByte: {
5638 GetAssembler()->StoreToOffset(kStoreByte, RegisterFrom(value), base, offset);
5639 break;
5640 }
5641
5642 case Primitive::kPrimShort:
5643 case Primitive::kPrimChar: {
5644 GetAssembler()->StoreToOffset(kStoreHalfword, RegisterFrom(value), base, offset);
5645 break;
5646 }
5647
5648 case Primitive::kPrimInt:
5649 case Primitive::kPrimNot: {
5650 if (kPoisonHeapReferences && needs_write_barrier) {
5651 // Note that in the case where `value` is a null reference,
5652 // we do not enter this block, as a null reference does not
5653 // need poisoning.
5654 DCHECK_EQ(field_type, Primitive::kPrimNot);
5655 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
5656 __ Mov(temp, RegisterFrom(value));
5657 GetAssembler()->PoisonHeapReference(temp);
5658 GetAssembler()->StoreToOffset(kStoreWord, temp, base, offset);
5659 } else {
5660 GetAssembler()->StoreToOffset(kStoreWord, RegisterFrom(value), base, offset);
5661 }
5662 break;
5663 }
5664
5665 case Primitive::kPrimLong: {
5666 if (is_volatile && !atomic_ldrd_strd) {
5667 GenerateWideAtomicStore(base,
5668 offset,
5669 LowRegisterFrom(value),
5670 HighRegisterFrom(value),
5671 RegisterFrom(locations->GetTemp(0)),
5672 RegisterFrom(locations->GetTemp(1)),
5673 instruction);
5674 } else {
5675 GetAssembler()->StoreToOffset(kStoreWordPair, LowRegisterFrom(value), base, offset);
5676 codegen_->MaybeRecordImplicitNullCheck(instruction);
5677 }
5678 break;
5679 }
5680
5681 case Primitive::kPrimFloat: {
5682 GetAssembler()->StoreSToOffset(SRegisterFrom(value), base, offset);
5683 break;
5684 }
5685
5686 case Primitive::kPrimDouble: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01005687 vixl32::DRegister value_reg = DRegisterFrom(value);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005688 if (is_volatile && !atomic_ldrd_strd) {
5689 vixl32::Register value_reg_lo = RegisterFrom(locations->GetTemp(0));
5690 vixl32::Register value_reg_hi = RegisterFrom(locations->GetTemp(1));
5691
5692 __ Vmov(value_reg_lo, value_reg_hi, value_reg);
5693
5694 GenerateWideAtomicStore(base,
5695 offset,
5696 value_reg_lo,
5697 value_reg_hi,
5698 RegisterFrom(locations->GetTemp(2)),
5699 RegisterFrom(locations->GetTemp(3)),
5700 instruction);
5701 } else {
5702 GetAssembler()->StoreDToOffset(value_reg, base, offset);
5703 codegen_->MaybeRecordImplicitNullCheck(instruction);
5704 }
5705 break;
5706 }
5707
5708 case Primitive::kPrimVoid:
5709 LOG(FATAL) << "Unreachable type " << field_type;
5710 UNREACHABLE();
5711 }
5712
5713 // Longs and doubles are handled in the switch.
5714 if (field_type != Primitive::kPrimLong && field_type != Primitive::kPrimDouble) {
Alexandre Rames374ddf32016-11-04 10:40:49 +00005715 // TODO(VIXL): Here and for other calls to `MaybeRecordImplicitNullCheck` in this method, we
5716 // should use a scope and the assembler to emit the store instruction to guarantee that we
5717 // record the pc at the correct position. But the `Assembler` does not automatically handle
5718 // unencodable offsets. Practically, everything is fine because the helper and VIXL, at the time
5719 // of writing, do generate the store instruction last.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005720 codegen_->MaybeRecordImplicitNullCheck(instruction);
5721 }
5722
5723 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
5724 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
5725 vixl32::Register card = RegisterFrom(locations->GetTemp(1));
5726 codegen_->MarkGCCard(temp, card, base, RegisterFrom(value), value_can_be_null);
5727 }
5728
5729 if (is_volatile) {
5730 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
5731 }
5732}
5733
Artem Serov02d37832016-10-25 15:25:33 +01005734void LocationsBuilderARMVIXL::HandleFieldGet(HInstruction* instruction,
5735 const FieldInfo& field_info) {
5736 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
5737
5738 bool object_field_get_with_read_barrier =
5739 kEmitCompilerReadBarrier && (field_info.GetFieldType() == Primitive::kPrimNot);
5740 LocationSummary* locations =
5741 new (GetGraph()->GetArena()) LocationSummary(instruction,
5742 object_field_get_with_read_barrier ?
5743 LocationSummary::kCallOnSlowPath :
5744 LocationSummary::kNoCall);
5745 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
5746 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
5747 }
5748 locations->SetInAt(0, Location::RequiresRegister());
5749
5750 bool volatile_for_double = field_info.IsVolatile()
5751 && (field_info.GetFieldType() == Primitive::kPrimDouble)
5752 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
5753 // The output overlaps in case of volatile long: we don't want the
5754 // code generated by GenerateWideAtomicLoad to overwrite the
5755 // object's location. Likewise, in the case of an object field get
5756 // with read barriers enabled, we do not want the load to overwrite
5757 // the object's location, as we need it to emit the read barrier.
5758 bool overlap = (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) ||
5759 object_field_get_with_read_barrier;
5760
5761 if (Primitive::IsFloatingPointType(instruction->GetType())) {
5762 locations->SetOut(Location::RequiresFpuRegister());
5763 } else {
5764 locations->SetOut(Location::RequiresRegister(),
5765 (overlap ? Location::kOutputOverlap : Location::kNoOutputOverlap));
5766 }
5767 if (volatile_for_double) {
5768 // ARM encoding have some additional constraints for ldrexd/strexd:
5769 // - registers need to be consecutive
5770 // - the first register should be even but not R14.
5771 // We don't test for ARM yet, and the assertion makes sure that we
5772 // revisit this if we ever enable ARM encoding.
5773 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
5774 locations->AddTemp(Location::RequiresRegister());
5775 locations->AddTemp(Location::RequiresRegister());
5776 } else if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
5777 // We need a temporary register for the read barrier marking slow
Artem Serovc5fcb442016-12-02 19:19:58 +00005778 // path in CodeGeneratorARMVIXL::GenerateFieldLoadWithBakerReadBarrier.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01005779 if (kBakerReadBarrierLinkTimeThunksEnableForFields &&
5780 !Runtime::Current()->UseJitCompilation()) {
5781 // If link-time thunks for the Baker read barrier are enabled, for AOT
5782 // loads we need a temporary only if the offset is too big.
5783 if (field_info.GetFieldOffset().Uint32Value() >= kReferenceLoadMinFarOffset) {
5784 locations->AddTemp(Location::RequiresRegister());
5785 }
5786 // And we always need the reserved entrypoint register.
5787 locations->AddTemp(Location::RegisterLocation(kBakerCcEntrypointRegister.GetCode()));
5788 } else {
5789 locations->AddTemp(Location::RequiresRegister());
5790 }
Artem Serov02d37832016-10-25 15:25:33 +01005791 }
5792}
5793
5794Location LocationsBuilderARMVIXL::ArithmeticZeroOrFpuRegister(HInstruction* input) {
5795 DCHECK(Primitive::IsFloatingPointType(input->GetType())) << input->GetType();
5796 if ((input->IsFloatConstant() && (input->AsFloatConstant()->IsArithmeticZero())) ||
5797 (input->IsDoubleConstant() && (input->AsDoubleConstant()->IsArithmeticZero()))) {
5798 return Location::ConstantLocation(input->AsConstant());
5799 } else {
5800 return Location::RequiresFpuRegister();
5801 }
5802}
5803
Artem Serov02109dd2016-09-23 17:17:54 +01005804Location LocationsBuilderARMVIXL::ArmEncodableConstantOrRegister(HInstruction* constant,
5805 Opcode opcode) {
5806 DCHECK(!Primitive::IsFloatingPointType(constant->GetType()));
5807 if (constant->IsConstant() &&
5808 CanEncodeConstantAsImmediate(constant->AsConstant(), opcode)) {
5809 return Location::ConstantLocation(constant->AsConstant());
5810 }
5811 return Location::RequiresRegister();
5812}
5813
5814bool LocationsBuilderARMVIXL::CanEncodeConstantAsImmediate(HConstant* input_cst,
5815 Opcode opcode) {
5816 uint64_t value = static_cast<uint64_t>(Int64FromConstant(input_cst));
5817 if (Primitive::Is64BitType(input_cst->GetType())) {
5818 Opcode high_opcode = opcode;
5819 SetCc low_set_cc = kCcDontCare;
5820 switch (opcode) {
5821 case SUB:
5822 // Flip the operation to an ADD.
5823 value = -value;
5824 opcode = ADD;
5825 FALLTHROUGH_INTENDED;
5826 case ADD:
5827 if (Low32Bits(value) == 0u) {
5828 return CanEncodeConstantAsImmediate(High32Bits(value), opcode, kCcDontCare);
5829 }
5830 high_opcode = ADC;
5831 low_set_cc = kCcSet;
5832 break;
5833 default:
5834 break;
5835 }
5836 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode, low_set_cc) &&
5837 CanEncodeConstantAsImmediate(High32Bits(value), high_opcode, kCcDontCare);
5838 } else {
5839 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode);
5840 }
5841}
5842
5843// TODO(VIXL): Replace art::arm::SetCc` with `vixl32::FlagsUpdate after flags set optimization
5844// enabled.
5845bool LocationsBuilderARMVIXL::CanEncodeConstantAsImmediate(uint32_t value,
5846 Opcode opcode,
5847 SetCc set_cc) {
5848 ArmVIXLAssembler* assembler = codegen_->GetAssembler();
5849 if (assembler->ShifterOperandCanHold(opcode, value, set_cc)) {
5850 return true;
5851 }
5852 Opcode neg_opcode = kNoOperand;
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00005853 uint32_t neg_value = 0;
Artem Serov02109dd2016-09-23 17:17:54 +01005854 switch (opcode) {
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00005855 case AND: neg_opcode = BIC; neg_value = ~value; break;
5856 case ORR: neg_opcode = ORN; neg_value = ~value; break;
5857 case ADD: neg_opcode = SUB; neg_value = -value; break;
5858 case ADC: neg_opcode = SBC; neg_value = ~value; break;
5859 case SUB: neg_opcode = ADD; neg_value = -value; break;
5860 case SBC: neg_opcode = ADC; neg_value = ~value; break;
5861 case MOV: neg_opcode = MVN; neg_value = ~value; break;
Artem Serov02109dd2016-09-23 17:17:54 +01005862 default:
5863 return false;
5864 }
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00005865
5866 if (assembler->ShifterOperandCanHold(neg_opcode, neg_value, set_cc)) {
5867 return true;
5868 }
5869
5870 return opcode == AND && IsPowerOfTwo(value + 1);
Artem Serov02109dd2016-09-23 17:17:54 +01005871}
5872
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005873void InstructionCodeGeneratorARMVIXL::HandleFieldGet(HInstruction* instruction,
5874 const FieldInfo& field_info) {
5875 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
5876
5877 LocationSummary* locations = instruction->GetLocations();
5878 vixl32::Register base = InputRegisterAt(instruction, 0);
5879 Location out = locations->Out();
5880 bool is_volatile = field_info.IsVolatile();
5881 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
5882 Primitive::Type field_type = field_info.GetFieldType();
5883 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
5884
5885 switch (field_type) {
5886 case Primitive::kPrimBoolean:
5887 GetAssembler()->LoadFromOffset(kLoadUnsignedByte, RegisterFrom(out), base, offset);
5888 break;
5889
5890 case Primitive::kPrimByte:
5891 GetAssembler()->LoadFromOffset(kLoadSignedByte, RegisterFrom(out), base, offset);
5892 break;
5893
5894 case Primitive::kPrimShort:
5895 GetAssembler()->LoadFromOffset(kLoadSignedHalfword, RegisterFrom(out), base, offset);
5896 break;
5897
5898 case Primitive::kPrimChar:
5899 GetAssembler()->LoadFromOffset(kLoadUnsignedHalfword, RegisterFrom(out), base, offset);
5900 break;
5901
5902 case Primitive::kPrimInt:
5903 GetAssembler()->LoadFromOffset(kLoadWord, RegisterFrom(out), base, offset);
5904 break;
5905
5906 case Primitive::kPrimNot: {
5907 // /* HeapReference<Object> */ out = *(base + offset)
5908 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005909 Location temp_loc = locations->GetTemp(0);
5910 // Note that a potential implicit null check is handled in this
5911 // CodeGeneratorARMVIXL::GenerateFieldLoadWithBakerReadBarrier call.
5912 codegen_->GenerateFieldLoadWithBakerReadBarrier(
5913 instruction, out, base, offset, temp_loc, /* needs_null_check */ true);
5914 if (is_volatile) {
5915 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5916 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005917 } else {
5918 GetAssembler()->LoadFromOffset(kLoadWord, RegisterFrom(out), base, offset);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005919 codegen_->MaybeRecordImplicitNullCheck(instruction);
5920 if (is_volatile) {
5921 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5922 }
5923 // If read barriers are enabled, emit read barriers other than
5924 // Baker's using a slow path (and also unpoison the loaded
5925 // reference, if heap poisoning is enabled).
5926 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, locations->InAt(0), offset);
5927 }
5928 break;
5929 }
5930
5931 case Primitive::kPrimLong:
5932 if (is_volatile && !atomic_ldrd_strd) {
5933 GenerateWideAtomicLoad(base, offset, LowRegisterFrom(out), HighRegisterFrom(out));
5934 } else {
5935 GetAssembler()->LoadFromOffset(kLoadWordPair, LowRegisterFrom(out), base, offset);
5936 }
5937 break;
5938
5939 case Primitive::kPrimFloat:
5940 GetAssembler()->LoadSFromOffset(SRegisterFrom(out), base, offset);
5941 break;
5942
5943 case Primitive::kPrimDouble: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01005944 vixl32::DRegister out_dreg = DRegisterFrom(out);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005945 if (is_volatile && !atomic_ldrd_strd) {
5946 vixl32::Register lo = RegisterFrom(locations->GetTemp(0));
5947 vixl32::Register hi = RegisterFrom(locations->GetTemp(1));
5948 GenerateWideAtomicLoad(base, offset, lo, hi);
5949 // TODO(VIXL): Do we need to be immediately after the ldrexd instruction? If so we need a
5950 // scope.
5951 codegen_->MaybeRecordImplicitNullCheck(instruction);
5952 __ Vmov(out_dreg, lo, hi);
5953 } else {
5954 GetAssembler()->LoadDFromOffset(out_dreg, base, offset);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005955 codegen_->MaybeRecordImplicitNullCheck(instruction);
5956 }
5957 break;
5958 }
5959
5960 case Primitive::kPrimVoid:
5961 LOG(FATAL) << "Unreachable type " << field_type;
5962 UNREACHABLE();
5963 }
5964
5965 if (field_type == Primitive::kPrimNot || field_type == Primitive::kPrimDouble) {
5966 // Potential implicit null checks, in the case of reference or
5967 // double fields, are handled in the previous switch statement.
5968 } else {
5969 // Address cases other than reference and double that may require an implicit null check.
Alexandre Rames374ddf32016-11-04 10:40:49 +00005970 // TODO(VIXL): Here and for other calls to `MaybeRecordImplicitNullCheck` in this method, we
5971 // should use a scope and the assembler to emit the load instruction to guarantee that we
5972 // record the pc at the correct position. But the `Assembler` does not automatically handle
5973 // unencodable offsets. Practically, everything is fine because the helper and VIXL, at the time
5974 // of writing, do generate the store instruction last.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005975 codegen_->MaybeRecordImplicitNullCheck(instruction);
5976 }
5977
5978 if (is_volatile) {
5979 if (field_type == Primitive::kPrimNot) {
5980 // Memory barriers, in the case of references, are also handled
5981 // in the previous switch statement.
5982 } else {
5983 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5984 }
5985 }
5986}
5987
5988void LocationsBuilderARMVIXL::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
5989 HandleFieldSet(instruction, instruction->GetFieldInfo());
5990}
5991
5992void InstructionCodeGeneratorARMVIXL::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
5993 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
5994}
5995
5996void LocationsBuilderARMVIXL::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5997 HandleFieldGet(instruction, instruction->GetFieldInfo());
5998}
5999
6000void InstructionCodeGeneratorARMVIXL::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
6001 HandleFieldGet(instruction, instruction->GetFieldInfo());
6002}
6003
6004void LocationsBuilderARMVIXL::VisitStaticFieldGet(HStaticFieldGet* instruction) {
6005 HandleFieldGet(instruction, instruction->GetFieldInfo());
6006}
6007
6008void InstructionCodeGeneratorARMVIXL::VisitStaticFieldGet(HStaticFieldGet* instruction) {
6009 HandleFieldGet(instruction, instruction->GetFieldInfo());
6010}
6011
Scott Wakelingc34dba72016-10-03 10:14:44 +01006012void LocationsBuilderARMVIXL::VisitStaticFieldSet(HStaticFieldSet* instruction) {
6013 HandleFieldSet(instruction, instruction->GetFieldInfo());
6014}
6015
6016void InstructionCodeGeneratorARMVIXL::VisitStaticFieldSet(HStaticFieldSet* instruction) {
6017 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
6018}
6019
Artem Serovcfbe9132016-10-14 15:58:56 +01006020void LocationsBuilderARMVIXL::VisitUnresolvedInstanceFieldGet(
6021 HUnresolvedInstanceFieldGet* instruction) {
6022 FieldAccessCallingConventionARMVIXL calling_convention;
6023 codegen_->CreateUnresolvedFieldLocationSummary(
6024 instruction, instruction->GetFieldType(), calling_convention);
6025}
6026
6027void InstructionCodeGeneratorARMVIXL::VisitUnresolvedInstanceFieldGet(
6028 HUnresolvedInstanceFieldGet* instruction) {
6029 FieldAccessCallingConventionARMVIXL calling_convention;
6030 codegen_->GenerateUnresolvedFieldAccess(instruction,
6031 instruction->GetFieldType(),
6032 instruction->GetFieldIndex(),
6033 instruction->GetDexPc(),
6034 calling_convention);
6035}
6036
6037void LocationsBuilderARMVIXL::VisitUnresolvedInstanceFieldSet(
6038 HUnresolvedInstanceFieldSet* instruction) {
6039 FieldAccessCallingConventionARMVIXL calling_convention;
6040 codegen_->CreateUnresolvedFieldLocationSummary(
6041 instruction, instruction->GetFieldType(), calling_convention);
6042}
6043
6044void InstructionCodeGeneratorARMVIXL::VisitUnresolvedInstanceFieldSet(
6045 HUnresolvedInstanceFieldSet* instruction) {
6046 FieldAccessCallingConventionARMVIXL calling_convention;
6047 codegen_->GenerateUnresolvedFieldAccess(instruction,
6048 instruction->GetFieldType(),
6049 instruction->GetFieldIndex(),
6050 instruction->GetDexPc(),
6051 calling_convention);
6052}
6053
6054void LocationsBuilderARMVIXL::VisitUnresolvedStaticFieldGet(
6055 HUnresolvedStaticFieldGet* instruction) {
6056 FieldAccessCallingConventionARMVIXL calling_convention;
6057 codegen_->CreateUnresolvedFieldLocationSummary(
6058 instruction, instruction->GetFieldType(), calling_convention);
6059}
6060
6061void InstructionCodeGeneratorARMVIXL::VisitUnresolvedStaticFieldGet(
6062 HUnresolvedStaticFieldGet* instruction) {
6063 FieldAccessCallingConventionARMVIXL calling_convention;
6064 codegen_->GenerateUnresolvedFieldAccess(instruction,
6065 instruction->GetFieldType(),
6066 instruction->GetFieldIndex(),
6067 instruction->GetDexPc(),
6068 calling_convention);
6069}
6070
6071void LocationsBuilderARMVIXL::VisitUnresolvedStaticFieldSet(
6072 HUnresolvedStaticFieldSet* instruction) {
6073 FieldAccessCallingConventionARMVIXL calling_convention;
6074 codegen_->CreateUnresolvedFieldLocationSummary(
6075 instruction, instruction->GetFieldType(), calling_convention);
6076}
6077
6078void InstructionCodeGeneratorARMVIXL::VisitUnresolvedStaticFieldSet(
6079 HUnresolvedStaticFieldSet* instruction) {
6080 FieldAccessCallingConventionARMVIXL calling_convention;
6081 codegen_->GenerateUnresolvedFieldAccess(instruction,
6082 instruction->GetFieldType(),
6083 instruction->GetFieldIndex(),
6084 instruction->GetDexPc(),
6085 calling_convention);
6086}
6087
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006088void LocationsBuilderARMVIXL::VisitNullCheck(HNullCheck* instruction) {
Artem Serov657022c2016-11-23 14:19:38 +00006089 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006090 locations->SetInAt(0, Location::RequiresRegister());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006091}
6092
6093void CodeGeneratorARMVIXL::GenerateImplicitNullCheck(HNullCheck* instruction) {
6094 if (CanMoveNullCheckToUser(instruction)) {
6095 return;
6096 }
6097
6098 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames374ddf32016-11-04 10:40:49 +00006099 // Ensure the pc position is recorded immediately after the `ldr` instruction.
Artem Serov0fb37192016-12-06 18:13:40 +00006100 ExactAssemblyScope aas(GetVIXLAssembler(),
6101 vixl32::kMaxInstructionSizeInBytes,
6102 CodeBufferCheckScope::kMaximumSize);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006103 __ ldr(temps.Acquire(), MemOperand(InputRegisterAt(instruction, 0)));
6104 RecordPcInfo(instruction, instruction->GetDexPc());
6105}
6106
6107void CodeGeneratorARMVIXL::GenerateExplicitNullCheck(HNullCheck* instruction) {
6108 NullCheckSlowPathARMVIXL* slow_path =
6109 new (GetGraph()->GetArena()) NullCheckSlowPathARMVIXL(instruction);
6110 AddSlowPath(slow_path);
xueliang.zhongf51bc622016-11-04 09:23:32 +00006111 __ CompareAndBranchIfZero(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006112}
6113
6114void InstructionCodeGeneratorARMVIXL::VisitNullCheck(HNullCheck* instruction) {
6115 codegen_->GenerateNullCheck(instruction);
6116}
6117
Scott Wakelingc34dba72016-10-03 10:14:44 +01006118static LoadOperandType GetLoadOperandType(Primitive::Type type) {
6119 switch (type) {
6120 case Primitive::kPrimNot:
6121 return kLoadWord;
6122 case Primitive::kPrimBoolean:
6123 return kLoadUnsignedByte;
6124 case Primitive::kPrimByte:
6125 return kLoadSignedByte;
6126 case Primitive::kPrimChar:
6127 return kLoadUnsignedHalfword;
6128 case Primitive::kPrimShort:
6129 return kLoadSignedHalfword;
6130 case Primitive::kPrimInt:
6131 return kLoadWord;
6132 case Primitive::kPrimLong:
6133 return kLoadWordPair;
6134 case Primitive::kPrimFloat:
6135 return kLoadSWord;
6136 case Primitive::kPrimDouble:
6137 return kLoadDWord;
6138 default:
6139 LOG(FATAL) << "Unreachable type " << type;
6140 UNREACHABLE();
6141 }
6142}
6143
6144static StoreOperandType GetStoreOperandType(Primitive::Type type) {
6145 switch (type) {
6146 case Primitive::kPrimNot:
6147 return kStoreWord;
6148 case Primitive::kPrimBoolean:
6149 case Primitive::kPrimByte:
6150 return kStoreByte;
6151 case Primitive::kPrimChar:
6152 case Primitive::kPrimShort:
6153 return kStoreHalfword;
6154 case Primitive::kPrimInt:
6155 return kStoreWord;
6156 case Primitive::kPrimLong:
6157 return kStoreWordPair;
6158 case Primitive::kPrimFloat:
6159 return kStoreSWord;
6160 case Primitive::kPrimDouble:
6161 return kStoreDWord;
6162 default:
6163 LOG(FATAL) << "Unreachable type " << type;
6164 UNREACHABLE();
6165 }
6166}
6167
6168void CodeGeneratorARMVIXL::LoadFromShiftedRegOffset(Primitive::Type type,
6169 Location out_loc,
6170 vixl32::Register base,
6171 vixl32::Register reg_index,
6172 vixl32::Condition cond) {
6173 uint32_t shift_count = Primitive::ComponentSizeShift(type);
6174 MemOperand mem_address(base, reg_index, vixl32::LSL, shift_count);
6175
6176 switch (type) {
6177 case Primitive::kPrimByte:
6178 __ Ldrsb(cond, RegisterFrom(out_loc), mem_address);
6179 break;
6180 case Primitive::kPrimBoolean:
6181 __ Ldrb(cond, RegisterFrom(out_loc), mem_address);
6182 break;
6183 case Primitive::kPrimShort:
6184 __ Ldrsh(cond, RegisterFrom(out_loc), mem_address);
6185 break;
6186 case Primitive::kPrimChar:
6187 __ Ldrh(cond, RegisterFrom(out_loc), mem_address);
6188 break;
6189 case Primitive::kPrimNot:
6190 case Primitive::kPrimInt:
6191 __ Ldr(cond, RegisterFrom(out_loc), mem_address);
6192 break;
6193 // T32 doesn't support LoadFromShiftedRegOffset mem address mode for these types.
6194 case Primitive::kPrimLong:
6195 case Primitive::kPrimFloat:
6196 case Primitive::kPrimDouble:
6197 default:
6198 LOG(FATAL) << "Unreachable type " << type;
6199 UNREACHABLE();
6200 }
6201}
6202
6203void CodeGeneratorARMVIXL::StoreToShiftedRegOffset(Primitive::Type type,
6204 Location loc,
6205 vixl32::Register base,
6206 vixl32::Register reg_index,
6207 vixl32::Condition cond) {
6208 uint32_t shift_count = Primitive::ComponentSizeShift(type);
6209 MemOperand mem_address(base, reg_index, vixl32::LSL, shift_count);
6210
6211 switch (type) {
6212 case Primitive::kPrimByte:
6213 case Primitive::kPrimBoolean:
6214 __ Strb(cond, RegisterFrom(loc), mem_address);
6215 break;
6216 case Primitive::kPrimShort:
6217 case Primitive::kPrimChar:
6218 __ Strh(cond, RegisterFrom(loc), mem_address);
6219 break;
6220 case Primitive::kPrimNot:
6221 case Primitive::kPrimInt:
6222 __ Str(cond, RegisterFrom(loc), mem_address);
6223 break;
6224 // T32 doesn't support StoreToShiftedRegOffset mem address mode for these types.
6225 case Primitive::kPrimLong:
6226 case Primitive::kPrimFloat:
6227 case Primitive::kPrimDouble:
6228 default:
6229 LOG(FATAL) << "Unreachable type " << type;
6230 UNREACHABLE();
6231 }
6232}
6233
6234void LocationsBuilderARMVIXL::VisitArrayGet(HArrayGet* instruction) {
6235 bool object_array_get_with_read_barrier =
6236 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
6237 LocationSummary* locations =
6238 new (GetGraph()->GetArena()) LocationSummary(instruction,
6239 object_array_get_with_read_barrier ?
6240 LocationSummary::kCallOnSlowPath :
6241 LocationSummary::kNoCall);
6242 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006243 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Scott Wakelingc34dba72016-10-03 10:14:44 +01006244 }
6245 locations->SetInAt(0, Location::RequiresRegister());
6246 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
6247 if (Primitive::IsFloatingPointType(instruction->GetType())) {
6248 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
6249 } else {
6250 // The output overlaps in the case of an object array get with
6251 // read barriers enabled: we do not want the move to overwrite the
6252 // array's location, as we need it to emit the read barrier.
6253 locations->SetOut(
6254 Location::RequiresRegister(),
6255 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
6256 }
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01006257 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
6258 // We need a temporary register for the read barrier marking slow
6259 // path in CodeGeneratorARMVIXL::GenerateArrayLoadWithBakerReadBarrier.
6260 if (kBakerReadBarrierLinkTimeThunksEnableForFields &&
6261 !Runtime::Current()->UseJitCompilation() &&
6262 instruction->GetIndex()->IsConstant()) {
6263 // Array loads with constant index are treated as field loads.
6264 // If link-time thunks for the Baker read barrier are enabled, for AOT
6265 // constant index loads we need a temporary only if the offset is too big.
6266 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
6267 uint32_t index = instruction->GetIndex()->AsIntConstant()->GetValue();
6268 offset += index << Primitive::ComponentSizeShift(Primitive::kPrimNot);
6269 if (offset >= kReferenceLoadMinFarOffset) {
6270 locations->AddTemp(Location::RequiresRegister());
6271 }
6272 // And we always need the reserved entrypoint register.
6273 locations->AddTemp(Location::RegisterLocation(kBakerCcEntrypointRegister.GetCode()));
6274 } else if (kBakerReadBarrierLinkTimeThunksEnableForArrays &&
6275 !Runtime::Current()->UseJitCompilation() &&
6276 !instruction->GetIndex()->IsConstant()) {
6277 // We need a non-scratch temporary for the array data pointer.
6278 locations->AddTemp(Location::RequiresRegister());
6279 // And we always need the reserved entrypoint register.
6280 locations->AddTemp(Location::RegisterLocation(kBakerCcEntrypointRegister.GetCode()));
6281 } else {
6282 locations->AddTemp(Location::RequiresRegister());
6283 }
6284 } else if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
6285 // Also need a temporary for String compression feature.
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006286 locations->AddTemp(Location::RequiresRegister());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006287 }
6288}
6289
6290void InstructionCodeGeneratorARMVIXL::VisitArrayGet(HArrayGet* instruction) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006291 LocationSummary* locations = instruction->GetLocations();
6292 Location obj_loc = locations->InAt(0);
6293 vixl32::Register obj = InputRegisterAt(instruction, 0);
6294 Location index = locations->InAt(1);
6295 Location out_loc = locations->Out();
6296 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
6297 Primitive::Type type = instruction->GetType();
6298 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
6299 instruction->IsStringCharAt();
6300 HInstruction* array_instr = instruction->GetArray();
6301 bool has_intermediate_address = array_instr->IsIntermediateAddress();
Scott Wakelingc34dba72016-10-03 10:14:44 +01006302
6303 switch (type) {
6304 case Primitive::kPrimBoolean:
6305 case Primitive::kPrimByte:
6306 case Primitive::kPrimShort:
6307 case Primitive::kPrimChar:
6308 case Primitive::kPrimInt: {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006309 vixl32::Register length;
6310 if (maybe_compressed_char_at) {
6311 length = RegisterFrom(locations->GetTemp(0));
6312 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
6313 GetAssembler()->LoadFromOffset(kLoadWord, length, obj, count_offset);
6314 codegen_->MaybeRecordImplicitNullCheck(instruction);
6315 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006316 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006317 int32_t const_index = Int32ConstantFrom(index);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006318 if (maybe_compressed_char_at) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006319 vixl32::Label uncompressed_load, done;
Anton Kirilov6f644202017-02-27 18:29:45 +00006320 vixl32::Label* final_label = codegen_->GetFinalLabel(instruction, &done);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006321 __ Lsrs(length, length, 1u); // LSRS has a 16-bit encoding, TST (immediate) does not.
6322 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
6323 "Expecting 0=compressed, 1=uncompressed");
Artem Serov517d9f62016-12-12 15:51:15 +00006324 __ B(cs, &uncompressed_load, /* far_target */ false);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006325 GetAssembler()->LoadFromOffset(kLoadUnsignedByte,
6326 RegisterFrom(out_loc),
6327 obj,
6328 data_offset + const_index);
Anton Kirilov6f644202017-02-27 18:29:45 +00006329 __ B(final_label);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006330 __ Bind(&uncompressed_load);
6331 GetAssembler()->LoadFromOffset(GetLoadOperandType(Primitive::kPrimChar),
6332 RegisterFrom(out_loc),
6333 obj,
6334 data_offset + (const_index << 1));
Anton Kirilov6f644202017-02-27 18:29:45 +00006335 if (done.IsReferenced()) {
6336 __ Bind(&done);
6337 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006338 } else {
6339 uint32_t full_offset = data_offset + (const_index << Primitive::ComponentSizeShift(type));
6340
6341 LoadOperandType load_type = GetLoadOperandType(type);
6342 GetAssembler()->LoadFromOffset(load_type, RegisterFrom(out_loc), obj, full_offset);
6343 }
6344 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006345 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006346 vixl32::Register temp = temps.Acquire();
6347
6348 if (has_intermediate_address) {
Artem Serov2bbc9532016-10-21 11:51:50 +01006349 // We do not need to compute the intermediate address from the array: the
6350 // input instruction has done it already. See the comment in
6351 // `TryExtractArrayAccessAddress()`.
6352 if (kIsDebugBuild) {
6353 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
Anton Kirilov644032c2016-12-06 17:51:43 +00006354 DCHECK_EQ(Uint64ConstantFrom(tmp->GetOffset()), data_offset);
Artem Serov2bbc9532016-10-21 11:51:50 +01006355 }
6356 temp = obj;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006357 } else {
6358 __ Add(temp, obj, data_offset);
6359 }
6360 if (maybe_compressed_char_at) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006361 vixl32::Label uncompressed_load, done;
Anton Kirilov6f644202017-02-27 18:29:45 +00006362 vixl32::Label* final_label = codegen_->GetFinalLabel(instruction, &done);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006363 __ Lsrs(length, length, 1u); // LSRS has a 16-bit encoding, TST (immediate) does not.
6364 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
6365 "Expecting 0=compressed, 1=uncompressed");
Artem Serov517d9f62016-12-12 15:51:15 +00006366 __ B(cs, &uncompressed_load, /* far_target */ false);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006367 __ Ldrb(RegisterFrom(out_loc), MemOperand(temp, RegisterFrom(index), vixl32::LSL, 0));
Anton Kirilov6f644202017-02-27 18:29:45 +00006368 __ B(final_label);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006369 __ Bind(&uncompressed_load);
6370 __ Ldrh(RegisterFrom(out_loc), MemOperand(temp, RegisterFrom(index), vixl32::LSL, 1));
Anton Kirilov6f644202017-02-27 18:29:45 +00006371 if (done.IsReferenced()) {
6372 __ Bind(&done);
6373 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006374 } else {
6375 codegen_->LoadFromShiftedRegOffset(type, out_loc, temp, RegisterFrom(index));
6376 }
6377 }
6378 break;
6379 }
6380
6381 case Primitive::kPrimNot: {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006382 // The read barrier instrumentation of object ArrayGet
6383 // instructions does not support the HIntermediateAddress
6384 // instruction.
6385 DCHECK(!(has_intermediate_address && kEmitCompilerReadBarrier));
6386
Scott Wakelingc34dba72016-10-03 10:14:44 +01006387 static_assert(
6388 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6389 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
6390 // /* HeapReference<Object> */ out =
6391 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
6392 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006393 Location temp = locations->GetTemp(0);
6394 // Note that a potential implicit null check is handled in this
6395 // CodeGeneratorARMVIXL::GenerateArrayLoadWithBakerReadBarrier call.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01006396 DCHECK(!instruction->CanDoImplicitNullCheckOn(instruction->InputAt(0)));
6397 if (index.IsConstant()) {
6398 // Array load with a constant index can be treated as a field load.
6399 data_offset += Int32ConstantFrom(index) << Primitive::ComponentSizeShift(type);
6400 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6401 out_loc,
6402 obj,
6403 data_offset,
6404 locations->GetTemp(0),
6405 /* needs_null_check */ false);
6406 } else {
6407 codegen_->GenerateArrayLoadWithBakerReadBarrier(
6408 instruction, out_loc, obj, data_offset, index, temp, /* needs_null_check */ false);
6409 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006410 } else {
6411 vixl32::Register out = OutputRegister(instruction);
6412 if (index.IsConstant()) {
6413 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00006414 (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006415 GetAssembler()->LoadFromOffset(kLoadWord, out, obj, offset);
Alexandre Rames374ddf32016-11-04 10:40:49 +00006416 // TODO(VIXL): Here and for other calls to `MaybeRecordImplicitNullCheck` in this method,
6417 // we should use a scope and the assembler to emit the load instruction to guarantee that
6418 // we record the pc at the correct position. But the `Assembler` does not automatically
6419 // handle unencodable offsets. Practically, everything is fine because the helper and
6420 // VIXL, at the time of writing, do generate the store instruction last.
Scott Wakelingc34dba72016-10-03 10:14:44 +01006421 codegen_->MaybeRecordImplicitNullCheck(instruction);
6422 // If read barriers are enabled, emit read barriers other than
6423 // Baker's using a slow path (and also unpoison the loaded
6424 // reference, if heap poisoning is enabled).
6425 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
6426 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006427 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006428 vixl32::Register temp = temps.Acquire();
6429
6430 if (has_intermediate_address) {
Artem Serov2bbc9532016-10-21 11:51:50 +01006431 // We do not need to compute the intermediate address from the array: the
6432 // input instruction has done it already. See the comment in
6433 // `TryExtractArrayAccessAddress()`.
6434 if (kIsDebugBuild) {
6435 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
Anton Kirilov644032c2016-12-06 17:51:43 +00006436 DCHECK_EQ(Uint64ConstantFrom(tmp->GetOffset()), data_offset);
Artem Serov2bbc9532016-10-21 11:51:50 +01006437 }
6438 temp = obj;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006439 } else {
6440 __ Add(temp, obj, data_offset);
6441 }
6442 codegen_->LoadFromShiftedRegOffset(type, out_loc, temp, RegisterFrom(index));
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006443 temps.Close();
Alexandre Rames374ddf32016-11-04 10:40:49 +00006444 // TODO(VIXL): Use a scope to ensure that we record the pc position immediately after the
6445 // load instruction. Practically, everything is fine because the helper and VIXL, at the
6446 // time of writing, do generate the store instruction last.
Scott Wakelingc34dba72016-10-03 10:14:44 +01006447 codegen_->MaybeRecordImplicitNullCheck(instruction);
6448 // If read barriers are enabled, emit read barriers other than
6449 // Baker's using a slow path (and also unpoison the loaded
6450 // reference, if heap poisoning is enabled).
6451 codegen_->MaybeGenerateReadBarrierSlow(
6452 instruction, out_loc, out_loc, obj_loc, data_offset, index);
6453 }
6454 }
6455 break;
6456 }
6457
6458 case Primitive::kPrimLong: {
6459 if (index.IsConstant()) {
6460 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00006461 (Int32ConstantFrom(index) << TIMES_8) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006462 GetAssembler()->LoadFromOffset(kLoadWordPair, LowRegisterFrom(out_loc), obj, offset);
6463 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006464 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006465 vixl32::Register temp = temps.Acquire();
6466 __ Add(temp, obj, Operand(RegisterFrom(index), vixl32::LSL, TIMES_8));
6467 GetAssembler()->LoadFromOffset(kLoadWordPair, LowRegisterFrom(out_loc), temp, data_offset);
6468 }
6469 break;
6470 }
6471
6472 case Primitive::kPrimFloat: {
6473 vixl32::SRegister out = SRegisterFrom(out_loc);
6474 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006475 size_t offset = (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006476 GetAssembler()->LoadSFromOffset(out, obj, offset);
6477 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006478 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006479 vixl32::Register temp = temps.Acquire();
6480 __ Add(temp, obj, Operand(RegisterFrom(index), vixl32::LSL, TIMES_4));
6481 GetAssembler()->LoadSFromOffset(out, temp, data_offset);
6482 }
6483 break;
6484 }
6485
6486 case Primitive::kPrimDouble: {
6487 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006488 size_t offset = (Int32ConstantFrom(index) << TIMES_8) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006489 GetAssembler()->LoadDFromOffset(DRegisterFrom(out_loc), obj, offset);
6490 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006491 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006492 vixl32::Register temp = temps.Acquire();
6493 __ Add(temp, obj, Operand(RegisterFrom(index), vixl32::LSL, TIMES_8));
6494 GetAssembler()->LoadDFromOffset(DRegisterFrom(out_loc), temp, data_offset);
6495 }
6496 break;
6497 }
6498
6499 case Primitive::kPrimVoid:
6500 LOG(FATAL) << "Unreachable type " << type;
6501 UNREACHABLE();
6502 }
6503
6504 if (type == Primitive::kPrimNot) {
6505 // Potential implicit null checks, in the case of reference
6506 // arrays, are handled in the previous switch statement.
6507 } else if (!maybe_compressed_char_at) {
Alexandre Rames374ddf32016-11-04 10:40:49 +00006508 // TODO(VIXL): Use a scope to ensure we record the pc info immediately after
6509 // the preceding load instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01006510 codegen_->MaybeRecordImplicitNullCheck(instruction);
6511 }
6512}
6513
6514void LocationsBuilderARMVIXL::VisitArraySet(HArraySet* instruction) {
6515 Primitive::Type value_type = instruction->GetComponentType();
6516
6517 bool needs_write_barrier =
6518 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
6519 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
6520
6521 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
6522 instruction,
6523 may_need_runtime_call_for_type_check ?
6524 LocationSummary::kCallOnSlowPath :
6525 LocationSummary::kNoCall);
6526
6527 locations->SetInAt(0, Location::RequiresRegister());
6528 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
6529 if (Primitive::IsFloatingPointType(value_type)) {
6530 locations->SetInAt(2, Location::RequiresFpuRegister());
6531 } else {
6532 locations->SetInAt(2, Location::RequiresRegister());
6533 }
6534 if (needs_write_barrier) {
6535 // Temporary registers for the write barrier.
6536 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
6537 locations->AddTemp(Location::RequiresRegister());
6538 }
6539}
6540
6541void InstructionCodeGeneratorARMVIXL::VisitArraySet(HArraySet* instruction) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006542 LocationSummary* locations = instruction->GetLocations();
6543 vixl32::Register array = InputRegisterAt(instruction, 0);
6544 Location index = locations->InAt(1);
6545 Primitive::Type value_type = instruction->GetComponentType();
6546 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
6547 bool needs_write_barrier =
6548 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
6549 uint32_t data_offset =
6550 mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
6551 Location value_loc = locations->InAt(2);
6552 HInstruction* array_instr = instruction->GetArray();
6553 bool has_intermediate_address = array_instr->IsIntermediateAddress();
Scott Wakelingc34dba72016-10-03 10:14:44 +01006554
6555 switch (value_type) {
6556 case Primitive::kPrimBoolean:
6557 case Primitive::kPrimByte:
6558 case Primitive::kPrimShort:
6559 case Primitive::kPrimChar:
6560 case Primitive::kPrimInt: {
6561 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006562 int32_t const_index = Int32ConstantFrom(index);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006563 uint32_t full_offset =
6564 data_offset + (const_index << Primitive::ComponentSizeShift(value_type));
6565 StoreOperandType store_type = GetStoreOperandType(value_type);
6566 GetAssembler()->StoreToOffset(store_type, RegisterFrom(value_loc), array, full_offset);
6567 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006568 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006569 vixl32::Register temp = temps.Acquire();
6570
6571 if (has_intermediate_address) {
Artem Serov2bbc9532016-10-21 11:51:50 +01006572 // We do not need to compute the intermediate address from the array: the
6573 // input instruction has done it already. See the comment in
6574 // `TryExtractArrayAccessAddress()`.
6575 if (kIsDebugBuild) {
6576 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
Anton Kirilov644032c2016-12-06 17:51:43 +00006577 DCHECK_EQ(Uint64ConstantFrom(tmp->GetOffset()), data_offset);
Artem Serov2bbc9532016-10-21 11:51:50 +01006578 }
6579 temp = array;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006580 } else {
6581 __ Add(temp, array, data_offset);
6582 }
6583 codegen_->StoreToShiftedRegOffset(value_type, value_loc, temp, RegisterFrom(index));
6584 }
6585 break;
6586 }
6587
6588 case Primitive::kPrimNot: {
6589 vixl32::Register value = RegisterFrom(value_loc);
6590 // TryExtractArrayAccessAddress optimization is never applied for non-primitive ArraySet.
6591 // See the comment in instruction_simplifier_shared.cc.
6592 DCHECK(!has_intermediate_address);
6593
6594 if (instruction->InputAt(2)->IsNullConstant()) {
6595 // Just setting null.
6596 if (index.IsConstant()) {
6597 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00006598 (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006599 GetAssembler()->StoreToOffset(kStoreWord, value, array, offset);
6600 } else {
6601 DCHECK(index.IsRegister()) << index;
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006602 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006603 vixl32::Register temp = temps.Acquire();
6604 __ Add(temp, array, data_offset);
6605 codegen_->StoreToShiftedRegOffset(value_type, value_loc, temp, RegisterFrom(index));
6606 }
Alexandre Rames374ddf32016-11-04 10:40:49 +00006607 // TODO(VIXL): Use a scope to ensure we record the pc info immediately after the preceding
6608 // store instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01006609 codegen_->MaybeRecordImplicitNullCheck(instruction);
6610 DCHECK(!needs_write_barrier);
6611 DCHECK(!may_need_runtime_call_for_type_check);
6612 break;
6613 }
6614
6615 DCHECK(needs_write_barrier);
6616 Location temp1_loc = locations->GetTemp(0);
6617 vixl32::Register temp1 = RegisterFrom(temp1_loc);
6618 Location temp2_loc = locations->GetTemp(1);
6619 vixl32::Register temp2 = RegisterFrom(temp2_loc);
6620 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6621 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6622 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6623 vixl32::Label done;
Anton Kirilov6f644202017-02-27 18:29:45 +00006624 vixl32::Label* final_label = codegen_->GetFinalLabel(instruction, &done);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006625 SlowPathCodeARMVIXL* slow_path = nullptr;
6626
6627 if (may_need_runtime_call_for_type_check) {
6628 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARMVIXL(instruction);
6629 codegen_->AddSlowPath(slow_path);
6630 if (instruction->GetValueCanBeNull()) {
6631 vixl32::Label non_zero;
xueliang.zhongf51bc622016-11-04 09:23:32 +00006632 __ CompareAndBranchIfNonZero(value, &non_zero);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006633 if (index.IsConstant()) {
6634 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00006635 (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006636 GetAssembler()->StoreToOffset(kStoreWord, value, array, offset);
6637 } else {
6638 DCHECK(index.IsRegister()) << index;
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006639 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006640 vixl32::Register temp = temps.Acquire();
6641 __ Add(temp, array, data_offset);
6642 codegen_->StoreToShiftedRegOffset(value_type, value_loc, temp, RegisterFrom(index));
6643 }
Alexandre Rames374ddf32016-11-04 10:40:49 +00006644 // TODO(VIXL): Use a scope to ensure we record the pc info immediately after the preceding
6645 // store instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01006646 codegen_->MaybeRecordImplicitNullCheck(instruction);
Anton Kirilov6f644202017-02-27 18:29:45 +00006647 __ B(final_label);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006648 __ Bind(&non_zero);
6649 }
6650
6651 // Note that when read barriers are enabled, the type checks
6652 // are performed without read barriers. This is fine, even in
6653 // the case where a class object is in the from-space after
6654 // the flip, as a comparison involving such a type would not
6655 // produce a false positive; it may of course produce a false
6656 // negative, in which case we would take the ArraySet slow
6657 // path.
6658
Alexandre Rames374ddf32016-11-04 10:40:49 +00006659 {
6660 // Ensure we record the pc position immediately after the `ldr` instruction.
Artem Serov0fb37192016-12-06 18:13:40 +00006661 ExactAssemblyScope aas(GetVIXLAssembler(),
6662 vixl32::kMaxInstructionSizeInBytes,
6663 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00006664 // /* HeapReference<Class> */ temp1 = array->klass_
6665 __ ldr(temp1, MemOperand(array, class_offset));
6666 codegen_->MaybeRecordImplicitNullCheck(instruction);
6667 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006668 GetAssembler()->MaybeUnpoisonHeapReference(temp1);
6669
6670 // /* HeapReference<Class> */ temp1 = temp1->component_type_
6671 GetAssembler()->LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
6672 // /* HeapReference<Class> */ temp2 = value->klass_
6673 GetAssembler()->LoadFromOffset(kLoadWord, temp2, value, class_offset);
6674 // If heap poisoning is enabled, no need to unpoison `temp1`
6675 // nor `temp2`, as we are comparing two poisoned references.
6676 __ Cmp(temp1, temp2);
6677
6678 if (instruction->StaticTypeOfArrayIsObjectArray()) {
6679 vixl32::Label do_put;
Artem Serov517d9f62016-12-12 15:51:15 +00006680 __ B(eq, &do_put, /* far_target */ false);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006681 // If heap poisoning is enabled, the `temp1` reference has
6682 // not been unpoisoned yet; unpoison it now.
6683 GetAssembler()->MaybeUnpoisonHeapReference(temp1);
6684
6685 // /* HeapReference<Class> */ temp1 = temp1->super_class_
6686 GetAssembler()->LoadFromOffset(kLoadWord, temp1, temp1, super_offset);
6687 // If heap poisoning is enabled, no need to unpoison
6688 // `temp1`, as we are comparing against null below.
xueliang.zhongf51bc622016-11-04 09:23:32 +00006689 __ CompareAndBranchIfNonZero(temp1, slow_path->GetEntryLabel());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006690 __ Bind(&do_put);
6691 } else {
6692 __ B(ne, slow_path->GetEntryLabel());
6693 }
6694 }
6695
6696 vixl32::Register source = value;
6697 if (kPoisonHeapReferences) {
6698 // Note that in the case where `value` is a null reference,
6699 // we do not enter this block, as a null reference does not
6700 // need poisoning.
6701 DCHECK_EQ(value_type, Primitive::kPrimNot);
6702 __ Mov(temp1, value);
6703 GetAssembler()->PoisonHeapReference(temp1);
6704 source = temp1;
6705 }
6706
6707 if (index.IsConstant()) {
6708 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00006709 (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006710 GetAssembler()->StoreToOffset(kStoreWord, source, array, offset);
6711 } else {
6712 DCHECK(index.IsRegister()) << index;
6713
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006714 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006715 vixl32::Register temp = temps.Acquire();
6716 __ Add(temp, array, data_offset);
6717 codegen_->StoreToShiftedRegOffset(value_type,
6718 LocationFrom(source),
6719 temp,
6720 RegisterFrom(index));
6721 }
6722
6723 if (!may_need_runtime_call_for_type_check) {
Alexandre Rames374ddf32016-11-04 10:40:49 +00006724 // TODO(VIXL): Ensure we record the pc position immediately after the preceding store
6725 // instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01006726 codegen_->MaybeRecordImplicitNullCheck(instruction);
6727 }
6728
6729 codegen_->MarkGCCard(temp1, temp2, array, value, instruction->GetValueCanBeNull());
6730
6731 if (done.IsReferenced()) {
6732 __ Bind(&done);
6733 }
6734
6735 if (slow_path != nullptr) {
6736 __ Bind(slow_path->GetExitLabel());
6737 }
6738
6739 break;
6740 }
6741
6742 case Primitive::kPrimLong: {
6743 Location value = locations->InAt(2);
6744 if (index.IsConstant()) {
6745 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00006746 (Int32ConstantFrom(index) << TIMES_8) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006747 GetAssembler()->StoreToOffset(kStoreWordPair, LowRegisterFrom(value), array, offset);
6748 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006749 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006750 vixl32::Register temp = temps.Acquire();
6751 __ Add(temp, array, Operand(RegisterFrom(index), vixl32::LSL, TIMES_8));
6752 GetAssembler()->StoreToOffset(kStoreWordPair, LowRegisterFrom(value), temp, data_offset);
6753 }
6754 break;
6755 }
6756
6757 case Primitive::kPrimFloat: {
6758 Location value = locations->InAt(2);
6759 DCHECK(value.IsFpuRegister());
6760 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006761 size_t offset = (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006762 GetAssembler()->StoreSToOffset(SRegisterFrom(value), array, offset);
6763 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006764 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006765 vixl32::Register temp = temps.Acquire();
6766 __ Add(temp, array, Operand(RegisterFrom(index), vixl32::LSL, TIMES_4));
6767 GetAssembler()->StoreSToOffset(SRegisterFrom(value), temp, data_offset);
6768 }
6769 break;
6770 }
6771
6772 case Primitive::kPrimDouble: {
6773 Location value = locations->InAt(2);
6774 DCHECK(value.IsFpuRegisterPair());
6775 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006776 size_t offset = (Int32ConstantFrom(index) << TIMES_8) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006777 GetAssembler()->StoreDToOffset(DRegisterFrom(value), array, offset);
6778 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006779 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006780 vixl32::Register temp = temps.Acquire();
6781 __ Add(temp, array, Operand(RegisterFrom(index), vixl32::LSL, TIMES_8));
6782 GetAssembler()->StoreDToOffset(DRegisterFrom(value), temp, data_offset);
6783 }
6784 break;
6785 }
6786
6787 case Primitive::kPrimVoid:
6788 LOG(FATAL) << "Unreachable type " << value_type;
6789 UNREACHABLE();
6790 }
6791
6792 // Objects are handled in the switch.
6793 if (value_type != Primitive::kPrimNot) {
Alexandre Rames374ddf32016-11-04 10:40:49 +00006794 // TODO(VIXL): Ensure we record the pc position immediately after the preceding store
6795 // instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01006796 codegen_->MaybeRecordImplicitNullCheck(instruction);
6797 }
6798}
6799
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006800void LocationsBuilderARMVIXL::VisitArrayLength(HArrayLength* instruction) {
6801 LocationSummary* locations =
6802 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6803 locations->SetInAt(0, Location::RequiresRegister());
6804 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6805}
6806
6807void InstructionCodeGeneratorARMVIXL::VisitArrayLength(HArrayLength* instruction) {
6808 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
6809 vixl32::Register obj = InputRegisterAt(instruction, 0);
6810 vixl32::Register out = OutputRegister(instruction);
Alexandre Rames374ddf32016-11-04 10:40:49 +00006811 {
Artem Serov0fb37192016-12-06 18:13:40 +00006812 ExactAssemblyScope aas(GetVIXLAssembler(),
6813 vixl32::kMaxInstructionSizeInBytes,
6814 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00006815 __ ldr(out, MemOperand(obj, offset));
6816 codegen_->MaybeRecordImplicitNullCheck(instruction);
6817 }
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006818 // Mask out compression flag from String's array length.
6819 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006820 __ Lsr(out, out, 1u);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006821 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006822}
6823
Artem Serov2bbc9532016-10-21 11:51:50 +01006824void LocationsBuilderARMVIXL::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Artem Serov2bbc9532016-10-21 11:51:50 +01006825 LocationSummary* locations =
6826 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6827
6828 locations->SetInAt(0, Location::RequiresRegister());
6829 locations->SetInAt(1, Location::RegisterOrConstant(instruction->GetOffset()));
6830 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6831}
6832
6833void InstructionCodeGeneratorARMVIXL::VisitIntermediateAddress(HIntermediateAddress* instruction) {
6834 vixl32::Register out = OutputRegister(instruction);
6835 vixl32::Register first = InputRegisterAt(instruction, 0);
6836 Location second = instruction->GetLocations()->InAt(1);
6837
Artem Serov2bbc9532016-10-21 11:51:50 +01006838 if (second.IsRegister()) {
6839 __ Add(out, first, RegisterFrom(second));
6840 } else {
Anton Kirilov644032c2016-12-06 17:51:43 +00006841 __ Add(out, first, Int32ConstantFrom(second));
Artem Serov2bbc9532016-10-21 11:51:50 +01006842 }
6843}
6844
Artem Serove1811ed2017-04-27 16:50:47 +01006845void LocationsBuilderARMVIXL::VisitIntermediateAddressIndex(
6846 HIntermediateAddressIndex* instruction) {
6847 LOG(FATAL) << "Unreachable " << instruction->GetId();
6848}
6849
6850void InstructionCodeGeneratorARMVIXL::VisitIntermediateAddressIndex(
6851 HIntermediateAddressIndex* instruction) {
6852 LOG(FATAL) << "Unreachable " << instruction->GetId();
6853}
6854
Scott Wakelingc34dba72016-10-03 10:14:44 +01006855void LocationsBuilderARMVIXL::VisitBoundsCheck(HBoundsCheck* instruction) {
6856 RegisterSet caller_saves = RegisterSet::Empty();
6857 InvokeRuntimeCallingConventionARMVIXL calling_convention;
6858 caller_saves.Add(LocationFrom(calling_convention.GetRegisterAt(0)));
6859 caller_saves.Add(LocationFrom(calling_convention.GetRegisterAt(1)));
6860 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Artem Serov2dd053d2017-03-08 14:54:06 +00006861
6862 HInstruction* index = instruction->InputAt(0);
6863 HInstruction* length = instruction->InputAt(1);
6864 // If both index and length are constants we can statically check the bounds. But if at least one
6865 // of them is not encodable ArmEncodableConstantOrRegister will create
6866 // Location::RequiresRegister() which is not desired to happen. Instead we create constant
6867 // locations.
6868 bool both_const = index->IsConstant() && length->IsConstant();
6869 locations->SetInAt(0, both_const
6870 ? Location::ConstantLocation(index->AsConstant())
6871 : ArmEncodableConstantOrRegister(index, CMP));
6872 locations->SetInAt(1, both_const
6873 ? Location::ConstantLocation(length->AsConstant())
6874 : ArmEncodableConstantOrRegister(length, CMP));
Scott Wakelingc34dba72016-10-03 10:14:44 +01006875}
6876
6877void InstructionCodeGeneratorARMVIXL::VisitBoundsCheck(HBoundsCheck* instruction) {
Artem Serov2dd053d2017-03-08 14:54:06 +00006878 LocationSummary* locations = instruction->GetLocations();
6879 Location index_loc = locations->InAt(0);
6880 Location length_loc = locations->InAt(1);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006881
Artem Serov2dd053d2017-03-08 14:54:06 +00006882 if (length_loc.IsConstant()) {
6883 int32_t length = Int32ConstantFrom(length_loc);
6884 if (index_loc.IsConstant()) {
6885 // BCE will remove the bounds check if we are guaranteed to pass.
6886 int32_t index = Int32ConstantFrom(index_loc);
6887 if (index < 0 || index >= length) {
6888 SlowPathCodeARMVIXL* slow_path =
6889 new (GetGraph()->GetArena()) BoundsCheckSlowPathARMVIXL(instruction);
6890 codegen_->AddSlowPath(slow_path);
6891 __ B(slow_path->GetEntryLabel());
6892 } else {
6893 // Some optimization after BCE may have generated this, and we should not
6894 // generate a bounds check if it is a valid range.
6895 }
6896 return;
6897 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006898
Artem Serov2dd053d2017-03-08 14:54:06 +00006899 SlowPathCodeARMVIXL* slow_path =
6900 new (GetGraph()->GetArena()) BoundsCheckSlowPathARMVIXL(instruction);
6901 __ Cmp(RegisterFrom(index_loc), length);
6902 codegen_->AddSlowPath(slow_path);
6903 __ B(hs, slow_path->GetEntryLabel());
6904 } else {
6905 SlowPathCodeARMVIXL* slow_path =
6906 new (GetGraph()->GetArena()) BoundsCheckSlowPathARMVIXL(instruction);
6907 __ Cmp(RegisterFrom(length_loc), InputOperandAt(instruction, 0));
6908 codegen_->AddSlowPath(slow_path);
6909 __ B(ls, slow_path->GetEntryLabel());
6910 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006911}
6912
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006913void CodeGeneratorARMVIXL::MarkGCCard(vixl32::Register temp,
6914 vixl32::Register card,
6915 vixl32::Register object,
6916 vixl32::Register value,
6917 bool can_be_null) {
6918 vixl32::Label is_null;
6919 if (can_be_null) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00006920 __ CompareAndBranchIfZero(value, &is_null);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006921 }
6922 GetAssembler()->LoadFromOffset(
6923 kLoadWord, card, tr, Thread::CardTableOffset<kArmPointerSize>().Int32Value());
Scott Wakelingb77051e2016-11-21 19:46:00 +00006924 __ Lsr(temp, object, Operand::From(gc::accounting::CardTable::kCardShift));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006925 __ Strb(card, MemOperand(card, temp));
6926 if (can_be_null) {
6927 __ Bind(&is_null);
6928 }
6929}
6930
Scott Wakelingfe885462016-09-22 10:24:38 +01006931void LocationsBuilderARMVIXL::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
6932 LOG(FATAL) << "Unreachable";
6933}
6934
6935void InstructionCodeGeneratorARMVIXL::VisitParallelMove(HParallelMove* instruction) {
6936 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
6937}
6938
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006939void LocationsBuilderARMVIXL::VisitSuspendCheck(HSuspendCheck* instruction) {
Artem Serov657022c2016-11-23 14:19:38 +00006940 LocationSummary* locations =
6941 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
6942 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006943}
6944
6945void InstructionCodeGeneratorARMVIXL::VisitSuspendCheck(HSuspendCheck* instruction) {
6946 HBasicBlock* block = instruction->GetBlock();
6947 if (block->GetLoopInformation() != nullptr) {
6948 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
6949 // The back edge will generate the suspend check.
6950 return;
6951 }
6952 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
6953 // The goto will generate the suspend check.
6954 return;
6955 }
6956 GenerateSuspendCheck(instruction, nullptr);
6957}
6958
6959void InstructionCodeGeneratorARMVIXL::GenerateSuspendCheck(HSuspendCheck* instruction,
6960 HBasicBlock* successor) {
6961 SuspendCheckSlowPathARMVIXL* slow_path =
6962 down_cast<SuspendCheckSlowPathARMVIXL*>(instruction->GetSlowPath());
6963 if (slow_path == nullptr) {
6964 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARMVIXL(instruction, successor);
6965 instruction->SetSlowPath(slow_path);
6966 codegen_->AddSlowPath(slow_path);
6967 if (successor != nullptr) {
6968 DCHECK(successor->IsLoopHeader());
6969 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
6970 }
6971 } else {
6972 DCHECK_EQ(slow_path->GetSuccessor(), successor);
6973 }
6974
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006975 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006976 vixl32::Register temp = temps.Acquire();
6977 GetAssembler()->LoadFromOffset(
6978 kLoadUnsignedHalfword, temp, tr, Thread::ThreadFlagsOffset<kArmPointerSize>().Int32Value());
6979 if (successor == nullptr) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00006980 __ CompareAndBranchIfNonZero(temp, slow_path->GetEntryLabel());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006981 __ Bind(slow_path->GetReturnLabel());
6982 } else {
xueliang.zhongf51bc622016-11-04 09:23:32 +00006983 __ CompareAndBranchIfZero(temp, codegen_->GetLabelOf(successor));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006984 __ B(slow_path->GetEntryLabel());
6985 }
6986}
6987
Scott Wakelingfe885462016-09-22 10:24:38 +01006988ArmVIXLAssembler* ParallelMoveResolverARMVIXL::GetAssembler() const {
6989 return codegen_->GetAssembler();
6990}
6991
6992void ParallelMoveResolverARMVIXL::EmitMove(size_t index) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006993 UseScratchRegisterScope temps(GetAssembler()->GetVIXLAssembler());
Scott Wakelingfe885462016-09-22 10:24:38 +01006994 MoveOperands* move = moves_[index];
6995 Location source = move->GetSource();
6996 Location destination = move->GetDestination();
6997
6998 if (source.IsRegister()) {
6999 if (destination.IsRegister()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007000 __ Mov(RegisterFrom(destination), RegisterFrom(source));
Scott Wakelingfe885462016-09-22 10:24:38 +01007001 } else if (destination.IsFpuRegister()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007002 __ Vmov(SRegisterFrom(destination), RegisterFrom(source));
Scott Wakelingfe885462016-09-22 10:24:38 +01007003 } else {
7004 DCHECK(destination.IsStackSlot());
7005 GetAssembler()->StoreToOffset(kStoreWord,
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007006 RegisterFrom(source),
Scott Wakelingfe885462016-09-22 10:24:38 +01007007 sp,
7008 destination.GetStackIndex());
7009 }
7010 } else if (source.IsStackSlot()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007011 if (destination.IsRegister()) {
7012 GetAssembler()->LoadFromOffset(kLoadWord,
7013 RegisterFrom(destination),
7014 sp,
7015 source.GetStackIndex());
7016 } else if (destination.IsFpuRegister()) {
7017 GetAssembler()->LoadSFromOffset(SRegisterFrom(destination), sp, source.GetStackIndex());
7018 } else {
7019 DCHECK(destination.IsStackSlot());
7020 vixl32::Register temp = temps.Acquire();
7021 GetAssembler()->LoadFromOffset(kLoadWord, temp, sp, source.GetStackIndex());
7022 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
7023 }
Scott Wakelingfe885462016-09-22 10:24:38 +01007024 } else if (source.IsFpuRegister()) {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01007025 if (destination.IsRegister()) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01007026 __ Vmov(RegisterFrom(destination), SRegisterFrom(source));
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01007027 } else if (destination.IsFpuRegister()) {
7028 __ Vmov(SRegisterFrom(destination), SRegisterFrom(source));
7029 } else {
7030 DCHECK(destination.IsStackSlot());
7031 GetAssembler()->StoreSToOffset(SRegisterFrom(source), sp, destination.GetStackIndex());
7032 }
Scott Wakelingfe885462016-09-22 10:24:38 +01007033 } else if (source.IsDoubleStackSlot()) {
Alexandre Rames9c19bd62016-10-24 11:50:32 +01007034 if (destination.IsDoubleStackSlot()) {
7035 vixl32::DRegister temp = temps.AcquireD();
7036 GetAssembler()->LoadDFromOffset(temp, sp, source.GetStackIndex());
7037 GetAssembler()->StoreDToOffset(temp, sp, destination.GetStackIndex());
7038 } else if (destination.IsRegisterPair()) {
7039 DCHECK(ExpectedPairLayout(destination));
7040 GetAssembler()->LoadFromOffset(
7041 kLoadWordPair, LowRegisterFrom(destination), sp, source.GetStackIndex());
7042 } else {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01007043 DCHECK(destination.IsFpuRegisterPair()) << destination;
7044 GetAssembler()->LoadDFromOffset(DRegisterFrom(destination), sp, source.GetStackIndex());
Alexandre Rames9c19bd62016-10-24 11:50:32 +01007045 }
Scott Wakelingfe885462016-09-22 10:24:38 +01007046 } else if (source.IsRegisterPair()) {
7047 if (destination.IsRegisterPair()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007048 __ Mov(LowRegisterFrom(destination), LowRegisterFrom(source));
7049 __ Mov(HighRegisterFrom(destination), HighRegisterFrom(source));
Scott Wakelingfe885462016-09-22 10:24:38 +01007050 } else if (destination.IsFpuRegisterPair()) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01007051 __ Vmov(DRegisterFrom(destination), LowRegisterFrom(source), HighRegisterFrom(source));
Scott Wakelingfe885462016-09-22 10:24:38 +01007052 } else {
7053 DCHECK(destination.IsDoubleStackSlot()) << destination;
7054 DCHECK(ExpectedPairLayout(source));
7055 GetAssembler()->StoreToOffset(kStoreWordPair,
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007056 LowRegisterFrom(source),
Scott Wakelingfe885462016-09-22 10:24:38 +01007057 sp,
7058 destination.GetStackIndex());
7059 }
7060 } else if (source.IsFpuRegisterPair()) {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01007061 if (destination.IsRegisterPair()) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01007062 __ Vmov(LowRegisterFrom(destination), HighRegisterFrom(destination), DRegisterFrom(source));
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01007063 } else if (destination.IsFpuRegisterPair()) {
7064 __ Vmov(DRegisterFrom(destination), DRegisterFrom(source));
7065 } else {
7066 DCHECK(destination.IsDoubleStackSlot()) << destination;
7067 GetAssembler()->StoreDToOffset(DRegisterFrom(source), sp, destination.GetStackIndex());
7068 }
Scott Wakelingfe885462016-09-22 10:24:38 +01007069 } else {
7070 DCHECK(source.IsConstant()) << source;
7071 HConstant* constant = source.GetConstant();
7072 if (constant->IsIntConstant() || constant->IsNullConstant()) {
7073 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
7074 if (destination.IsRegister()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007075 __ Mov(RegisterFrom(destination), value);
Scott Wakelingfe885462016-09-22 10:24:38 +01007076 } else {
7077 DCHECK(destination.IsStackSlot());
Scott Wakelingfe885462016-09-22 10:24:38 +01007078 vixl32::Register temp = temps.Acquire();
7079 __ Mov(temp, value);
7080 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
7081 }
7082 } else if (constant->IsLongConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00007083 int64_t value = Int64ConstantFrom(source);
Scott Wakelingfe885462016-09-22 10:24:38 +01007084 if (destination.IsRegisterPair()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007085 __ Mov(LowRegisterFrom(destination), Low32Bits(value));
7086 __ Mov(HighRegisterFrom(destination), High32Bits(value));
Scott Wakelingfe885462016-09-22 10:24:38 +01007087 } else {
7088 DCHECK(destination.IsDoubleStackSlot()) << destination;
Scott Wakelingfe885462016-09-22 10:24:38 +01007089 vixl32::Register temp = temps.Acquire();
7090 __ Mov(temp, Low32Bits(value));
7091 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
7092 __ Mov(temp, High32Bits(value));
7093 GetAssembler()->StoreToOffset(kStoreWord,
7094 temp,
7095 sp,
7096 destination.GetHighStackIndex(kArmWordSize));
7097 }
7098 } else if (constant->IsDoubleConstant()) {
7099 double value = constant->AsDoubleConstant()->GetValue();
7100 if (destination.IsFpuRegisterPair()) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01007101 __ Vmov(DRegisterFrom(destination), value);
Scott Wakelingfe885462016-09-22 10:24:38 +01007102 } else {
7103 DCHECK(destination.IsDoubleStackSlot()) << destination;
7104 uint64_t int_value = bit_cast<uint64_t, double>(value);
Scott Wakelingfe885462016-09-22 10:24:38 +01007105 vixl32::Register temp = temps.Acquire();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007106 __ Mov(temp, Low32Bits(int_value));
Scott Wakelingfe885462016-09-22 10:24:38 +01007107 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007108 __ Mov(temp, High32Bits(int_value));
Scott Wakelingfe885462016-09-22 10:24:38 +01007109 GetAssembler()->StoreToOffset(kStoreWord,
7110 temp,
7111 sp,
7112 destination.GetHighStackIndex(kArmWordSize));
7113 }
7114 } else {
7115 DCHECK(constant->IsFloatConstant()) << constant->DebugName();
7116 float value = constant->AsFloatConstant()->GetValue();
7117 if (destination.IsFpuRegister()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007118 __ Vmov(SRegisterFrom(destination), value);
Scott Wakelingfe885462016-09-22 10:24:38 +01007119 } else {
7120 DCHECK(destination.IsStackSlot());
Scott Wakelingfe885462016-09-22 10:24:38 +01007121 vixl32::Register temp = temps.Acquire();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007122 __ Mov(temp, bit_cast<int32_t, float>(value));
Scott Wakelingfe885462016-09-22 10:24:38 +01007123 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
7124 }
7125 }
7126 }
7127}
7128
Alexandre Rames9c19bd62016-10-24 11:50:32 +01007129void ParallelMoveResolverARMVIXL::Exchange(vixl32::Register reg, int mem) {
7130 UseScratchRegisterScope temps(GetAssembler()->GetVIXLAssembler());
7131 vixl32::Register temp = temps.Acquire();
7132 __ Mov(temp, reg);
7133 GetAssembler()->LoadFromOffset(kLoadWord, reg, sp, mem);
7134 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, mem);
Scott Wakelingfe885462016-09-22 10:24:38 +01007135}
7136
Alexandre Rames9c19bd62016-10-24 11:50:32 +01007137void ParallelMoveResolverARMVIXL::Exchange(int mem1, int mem2) {
7138 // TODO(VIXL32): Double check the performance of this implementation.
7139 UseScratchRegisterScope temps(GetAssembler()->GetVIXLAssembler());
Nicolas Geoffray13a797b2017-03-15 16:41:31 +00007140 vixl32::Register temp1 = temps.Acquire();
7141 ScratchRegisterScope ensure_scratch(
7142 this, temp1.GetCode(), r0.GetCode(), codegen_->GetNumberOfCoreRegisters());
7143 vixl32::Register temp2(ensure_scratch.GetRegister());
Alexandre Rames9c19bd62016-10-24 11:50:32 +01007144
Nicolas Geoffray13a797b2017-03-15 16:41:31 +00007145 int stack_offset = ensure_scratch.IsSpilled() ? kArmWordSize : 0;
7146 GetAssembler()->LoadFromOffset(kLoadWord, temp1, sp, mem1 + stack_offset);
7147 GetAssembler()->LoadFromOffset(kLoadWord, temp2, sp, mem2 + stack_offset);
7148 GetAssembler()->StoreToOffset(kStoreWord, temp1, sp, mem2 + stack_offset);
7149 GetAssembler()->StoreToOffset(kStoreWord, temp2, sp, mem1 + stack_offset);
Scott Wakelingfe885462016-09-22 10:24:38 +01007150}
7151
Alexandre Rames9c19bd62016-10-24 11:50:32 +01007152void ParallelMoveResolverARMVIXL::EmitSwap(size_t index) {
7153 MoveOperands* move = moves_[index];
7154 Location source = move->GetSource();
7155 Location destination = move->GetDestination();
7156 UseScratchRegisterScope temps(GetAssembler()->GetVIXLAssembler());
7157
7158 if (source.IsRegister() && destination.IsRegister()) {
7159 vixl32::Register temp = temps.Acquire();
7160 DCHECK(!RegisterFrom(source).Is(temp));
7161 DCHECK(!RegisterFrom(destination).Is(temp));
7162 __ Mov(temp, RegisterFrom(destination));
7163 __ Mov(RegisterFrom(destination), RegisterFrom(source));
7164 __ Mov(RegisterFrom(source), temp);
7165 } else if (source.IsRegister() && destination.IsStackSlot()) {
7166 Exchange(RegisterFrom(source), destination.GetStackIndex());
7167 } else if (source.IsStackSlot() && destination.IsRegister()) {
7168 Exchange(RegisterFrom(destination), source.GetStackIndex());
7169 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Anton Kirilovdda43962016-11-21 19:55:20 +00007170 Exchange(source.GetStackIndex(), destination.GetStackIndex());
Alexandre Rames9c19bd62016-10-24 11:50:32 +01007171 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Nicolas Geoffray13a797b2017-03-15 16:41:31 +00007172 vixl32::Register temp = temps.Acquire();
Anton Kirilovdda43962016-11-21 19:55:20 +00007173 __ Vmov(temp, SRegisterFrom(source));
7174 __ Vmov(SRegisterFrom(source), SRegisterFrom(destination));
7175 __ Vmov(SRegisterFrom(destination), temp);
Alexandre Rames9c19bd62016-10-24 11:50:32 +01007176 } else if (source.IsRegisterPair() && destination.IsRegisterPair()) {
7177 vixl32::DRegister temp = temps.AcquireD();
7178 __ Vmov(temp, LowRegisterFrom(source), HighRegisterFrom(source));
7179 __ Mov(LowRegisterFrom(source), LowRegisterFrom(destination));
7180 __ Mov(HighRegisterFrom(source), HighRegisterFrom(destination));
7181 __ Vmov(LowRegisterFrom(destination), HighRegisterFrom(destination), temp);
7182 } else if (source.IsRegisterPair() || destination.IsRegisterPair()) {
7183 vixl32::Register low_reg = LowRegisterFrom(source.IsRegisterPair() ? source : destination);
7184 int mem = source.IsRegisterPair() ? destination.GetStackIndex() : source.GetStackIndex();
7185 DCHECK(ExpectedPairLayout(source.IsRegisterPair() ? source : destination));
7186 vixl32::DRegister temp = temps.AcquireD();
7187 __ Vmov(temp, low_reg, vixl32::Register(low_reg.GetCode() + 1));
7188 GetAssembler()->LoadFromOffset(kLoadWordPair, low_reg, sp, mem);
7189 GetAssembler()->StoreDToOffset(temp, sp, mem);
7190 } else if (source.IsFpuRegisterPair() && destination.IsFpuRegisterPair()) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007191 vixl32::DRegister first = DRegisterFrom(source);
7192 vixl32::DRegister second = DRegisterFrom(destination);
7193 vixl32::DRegister temp = temps.AcquireD();
7194 __ Vmov(temp, first);
7195 __ Vmov(first, second);
7196 __ Vmov(second, temp);
Alexandre Rames9c19bd62016-10-24 11:50:32 +01007197 } else if (source.IsFpuRegisterPair() || destination.IsFpuRegisterPair()) {
Anton Kirilovdda43962016-11-21 19:55:20 +00007198 vixl32::DRegister reg = source.IsFpuRegisterPair()
7199 ? DRegisterFrom(source)
7200 : DRegisterFrom(destination);
7201 int mem = source.IsFpuRegisterPair()
7202 ? destination.GetStackIndex()
7203 : source.GetStackIndex();
7204 vixl32::DRegister temp = temps.AcquireD();
7205 __ Vmov(temp, reg);
7206 GetAssembler()->LoadDFromOffset(reg, sp, mem);
7207 GetAssembler()->StoreDToOffset(temp, sp, mem);
Alexandre Rames9c19bd62016-10-24 11:50:32 +01007208 } else if (source.IsFpuRegister() || destination.IsFpuRegister()) {
Anton Kirilovdda43962016-11-21 19:55:20 +00007209 vixl32::SRegister reg = source.IsFpuRegister()
7210 ? SRegisterFrom(source)
7211 : SRegisterFrom(destination);
7212 int mem = source.IsFpuRegister()
7213 ? destination.GetStackIndex()
7214 : source.GetStackIndex();
7215 vixl32::Register temp = temps.Acquire();
7216 __ Vmov(temp, reg);
7217 GetAssembler()->LoadSFromOffset(reg, sp, mem);
7218 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, mem);
Alexandre Rames9c19bd62016-10-24 11:50:32 +01007219 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
7220 vixl32::DRegister temp1 = temps.AcquireD();
7221 vixl32::DRegister temp2 = temps.AcquireD();
7222 __ Vldr(temp1, MemOperand(sp, source.GetStackIndex()));
7223 __ Vldr(temp2, MemOperand(sp, destination.GetStackIndex()));
7224 __ Vstr(temp1, MemOperand(sp, destination.GetStackIndex()));
7225 __ Vstr(temp2, MemOperand(sp, source.GetStackIndex()));
7226 } else {
7227 LOG(FATAL) << "Unimplemented" << source << " <-> " << destination;
7228 }
Scott Wakelingfe885462016-09-22 10:24:38 +01007229}
7230
Nicolas Geoffray13a797b2017-03-15 16:41:31 +00007231void ParallelMoveResolverARMVIXL::SpillScratch(int reg) {
7232 __ Push(vixl32::Register(reg));
Scott Wakelingfe885462016-09-22 10:24:38 +01007233}
7234
Nicolas Geoffray13a797b2017-03-15 16:41:31 +00007235void ParallelMoveResolverARMVIXL::RestoreScratch(int reg) {
7236 __ Pop(vixl32::Register(reg));
Scott Wakelingfe885462016-09-22 10:24:38 +01007237}
7238
Artem Serov02d37832016-10-25 15:25:33 +01007239HLoadClass::LoadKind CodeGeneratorARMVIXL::GetSupportedLoadClassKind(
Artem Serovd4cc5b22016-11-04 11:19:09 +00007240 HLoadClass::LoadKind desired_class_load_kind) {
7241 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00007242 case HLoadClass::LoadKind::kInvalid:
7243 LOG(FATAL) << "UNREACHABLE";
7244 UNREACHABLE();
Artem Serovd4cc5b22016-11-04 11:19:09 +00007245 case HLoadClass::LoadKind::kReferrersClass:
7246 break;
Artem Serovd4cc5b22016-11-04 11:19:09 +00007247 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007248 case HLoadClass::LoadKind::kBssEntry:
7249 DCHECK(!Runtime::Current()->UseJitCompilation());
7250 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007251 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007252 DCHECK(Runtime::Current()->UseJitCompilation());
Artem Serovc5fcb442016-12-02 19:19:58 +00007253 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01007254 case HLoadClass::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007255 case HLoadClass::LoadKind::kRuntimeCall:
Artem Serovd4cc5b22016-11-04 11:19:09 +00007256 break;
7257 }
7258 return desired_class_load_kind;
Artem Serov02d37832016-10-25 15:25:33 +01007259}
7260
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007261void LocationsBuilderARMVIXL::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00007262 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007263 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007264 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00007265 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007266 cls,
7267 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00007268 LocationFrom(r0));
Vladimir Markoea4c1262017-02-06 19:59:33 +00007269 DCHECK(calling_convention.GetRegisterAt(0).Is(r0));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007270 return;
7271 }
Vladimir Marko41559982017-01-06 14:04:23 +00007272 DCHECK(!cls->NeedsAccessCheck());
Scott Wakelingfe885462016-09-22 10:24:38 +01007273
Artem Serovd4cc5b22016-11-04 11:19:09 +00007274 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
7275 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007276 ? LocationSummary::kCallOnSlowPath
7277 : LocationSummary::kNoCall;
7278 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Artem Serovd4cc5b22016-11-04 11:19:09 +00007279 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007280 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Artem Serovd4cc5b22016-11-04 11:19:09 +00007281 }
7282
Vladimir Marko41559982017-01-06 14:04:23 +00007283 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007284 locations->SetInAt(0, Location::RequiresRegister());
7285 }
7286 locations->SetOut(Location::RequiresRegister());
Vladimir Markoea4c1262017-02-06 19:59:33 +00007287 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
7288 if (!kUseReadBarrier || kUseBakerReadBarrier) {
7289 // Rely on the type resolution or initialization and marking to save everything we need.
7290 // Note that IP may be clobbered by saving/restoring the live register (only one thanks
7291 // to the custom calling convention) or by marking, so we request a different temp.
7292 locations->AddTemp(Location::RequiresRegister());
7293 RegisterSet caller_saves = RegisterSet::Empty();
7294 InvokeRuntimeCallingConventionARMVIXL calling_convention;
7295 caller_saves.Add(LocationFrom(calling_convention.GetRegisterAt(0)));
7296 // TODO: Add GetReturnLocation() to the calling convention so that we can DCHECK()
7297 // that the the kPrimNot result register is the same as the first argument register.
7298 locations->SetCustomSlowPathCallerSaves(caller_saves);
7299 } else {
7300 // For non-Baker read barrier we have a temp-clobbering call.
7301 }
7302 }
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01007303 if (kUseBakerReadBarrier && kBakerReadBarrierLinkTimeThunksEnableForGcRoots) {
7304 if (load_kind == HLoadClass::LoadKind::kBssEntry ||
7305 (load_kind == HLoadClass::LoadKind::kReferrersClass &&
7306 !Runtime::Current()->UseJitCompilation())) {
7307 locations->AddTemp(Location::RegisterLocation(kBakerCcEntrypointRegister.GetCode()));
7308 }
7309 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007310}
7311
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007312// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
7313// move.
7314void InstructionCodeGeneratorARMVIXL::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00007315 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007316 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00007317 codegen_->GenerateLoadClassRuntimeCall(cls);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007318 return;
7319 }
Vladimir Marko41559982017-01-06 14:04:23 +00007320 DCHECK(!cls->NeedsAccessCheck());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007321
Vladimir Marko41559982017-01-06 14:04:23 +00007322 LocationSummary* locations = cls->GetLocations();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007323 Location out_loc = locations->Out();
7324 vixl32::Register out = OutputRegister(cls);
7325
Artem Serovd4cc5b22016-11-04 11:19:09 +00007326 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
7327 ? kWithoutReadBarrier
7328 : kCompilerReadBarrierOption;
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007329 bool generate_null_check = false;
Vladimir Marko41559982017-01-06 14:04:23 +00007330 switch (load_kind) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007331 case HLoadClass::LoadKind::kReferrersClass: {
7332 DCHECK(!cls->CanCallRuntime());
7333 DCHECK(!cls->MustGenerateClinitCheck());
7334 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
7335 vixl32::Register current_method = InputRegisterAt(cls, 0);
7336 GenerateGcRootFieldLoad(cls,
7337 out_loc,
7338 current_method,
Roland Levillain00468f32016-10-27 18:02:48 +01007339 ArtMethod::DeclaringClassOffset().Int32Value(),
Artem Serovd4cc5b22016-11-04 11:19:09 +00007340 read_barrier_option);
7341 break;
7342 }
Artem Serovd4cc5b22016-11-04 11:19:09 +00007343 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007344 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Artem Serovd4cc5b22016-11-04 11:19:09 +00007345 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
7346 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
7347 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
7348 codegen_->EmitMovwMovtPlaceholder(labels, out);
7349 break;
7350 }
7351 case HLoadClass::LoadKind::kBootImageAddress: {
Artem Serovc5fcb442016-12-02 19:19:58 +00007352 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007353 uint32_t address = dchecked_integral_cast<uint32_t>(
7354 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
7355 DCHECK_NE(address, 0u);
Artem Serovc5fcb442016-12-02 19:19:58 +00007356 __ Ldr(out, codegen_->DeduplicateBootImageAddressLiteral(address));
Artem Serovd4cc5b22016-11-04 11:19:09 +00007357 break;
7358 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007359 case HLoadClass::LoadKind::kBssEntry: {
Vladimir Markoea4c1262017-02-06 19:59:33 +00007360 vixl32::Register temp = (!kUseReadBarrier || kUseBakerReadBarrier)
7361 ? RegisterFrom(locations->GetTemp(0))
7362 : out;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007363 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
Vladimir Marko1998cd02017-01-13 13:02:58 +00007364 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex());
Vladimir Markoea4c1262017-02-06 19:59:33 +00007365 codegen_->EmitMovwMovtPlaceholder(labels, temp);
7366 GenerateGcRootFieldLoad(cls, out_loc, temp, /* offset */ 0, read_barrier_option);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007367 generate_null_check = true;
7368 break;
7369 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007370 case HLoadClass::LoadKind::kJitTableAddress: {
Artem Serovc5fcb442016-12-02 19:19:58 +00007371 __ Ldr(out, codegen_->DeduplicateJitClassLiteral(cls->GetDexFile(),
7372 cls->GetTypeIndex(),
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007373 cls->GetClass()));
Artem Serovc5fcb442016-12-02 19:19:58 +00007374 // /* GcRoot<mirror::Class> */ out = *out
Vladimir Markoea4c1262017-02-06 19:59:33 +00007375 GenerateGcRootFieldLoad(cls, out_loc, out, /* offset */ 0, read_barrier_option);
Artem Serovd4cc5b22016-11-04 11:19:09 +00007376 break;
7377 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007378 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00007379 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00007380 LOG(FATAL) << "UNREACHABLE";
7381 UNREACHABLE();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007382 }
7383
7384 if (generate_null_check || cls->MustGenerateClinitCheck()) {
7385 DCHECK(cls->CanCallRuntime());
7386 LoadClassSlowPathARMVIXL* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARMVIXL(
7387 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
7388 codegen_->AddSlowPath(slow_path);
7389 if (generate_null_check) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00007390 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007391 }
7392 if (cls->MustGenerateClinitCheck()) {
7393 GenerateClassInitializationCheck(slow_path, out);
7394 } else {
7395 __ Bind(slow_path->GetExitLabel());
7396 }
7397 }
7398}
7399
Artem Serov02d37832016-10-25 15:25:33 +01007400void LocationsBuilderARMVIXL::VisitClinitCheck(HClinitCheck* check) {
7401 LocationSummary* locations =
7402 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
7403 locations->SetInAt(0, Location::RequiresRegister());
7404 if (check->HasUses()) {
7405 locations->SetOut(Location::SameAsFirstInput());
7406 }
7407}
7408
7409void InstructionCodeGeneratorARMVIXL::VisitClinitCheck(HClinitCheck* check) {
7410 // We assume the class is not null.
7411 LoadClassSlowPathARMVIXL* slow_path =
7412 new (GetGraph()->GetArena()) LoadClassSlowPathARMVIXL(check->GetLoadClass(),
7413 check,
7414 check->GetDexPc(),
7415 /* do_clinit */ true);
7416 codegen_->AddSlowPath(slow_path);
7417 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
7418}
7419
7420void InstructionCodeGeneratorARMVIXL::GenerateClassInitializationCheck(
7421 LoadClassSlowPathARMVIXL* slow_path, vixl32::Register class_reg) {
7422 UseScratchRegisterScope temps(GetVIXLAssembler());
7423 vixl32::Register temp = temps.Acquire();
7424 GetAssembler()->LoadFromOffset(kLoadWord,
7425 temp,
7426 class_reg,
7427 mirror::Class::StatusOffset().Int32Value());
7428 __ Cmp(temp, mirror::Class::kStatusInitialized);
7429 __ B(lt, slow_path->GetEntryLabel());
7430 // Even if the initialized flag is set, we may be in a situation where caches are not synced
7431 // properly. Therefore, we do a memory fence.
7432 __ Dmb(ISH);
7433 __ Bind(slow_path->GetExitLabel());
7434}
7435
Artem Serov02d37832016-10-25 15:25:33 +01007436HLoadString::LoadKind CodeGeneratorARMVIXL::GetSupportedLoadStringKind(
Artem Serovd4cc5b22016-11-04 11:19:09 +00007437 HLoadString::LoadKind desired_string_load_kind) {
7438 switch (desired_string_load_kind) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00007439 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Artem Serovd4cc5b22016-11-04 11:19:09 +00007440 case HLoadString::LoadKind::kBssEntry:
7441 DCHECK(!Runtime::Current()->UseJitCompilation());
7442 break;
7443 case HLoadString::LoadKind::kJitTableAddress:
7444 DCHECK(Runtime::Current()->UseJitCompilation());
Artem Serovc5fcb442016-12-02 19:19:58 +00007445 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01007446 case HLoadString::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007447 case HLoadString::LoadKind::kRuntimeCall:
Artem Serovd4cc5b22016-11-04 11:19:09 +00007448 break;
7449 }
7450 return desired_string_load_kind;
Artem Serov02d37832016-10-25 15:25:33 +01007451}
7452
7453void LocationsBuilderARMVIXL::VisitLoadString(HLoadString* load) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00007454 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Artem Serov02d37832016-10-25 15:25:33 +01007455 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Artem Serov02d37832016-10-25 15:25:33 +01007456 HLoadString::LoadKind load_kind = load->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007457 if (load_kind == HLoadString::LoadKind::kRuntimeCall) {
Artem Serov02d37832016-10-25 15:25:33 +01007458 locations->SetOut(LocationFrom(r0));
7459 } else {
7460 locations->SetOut(Location::RequiresRegister());
Artem Serovd4cc5b22016-11-04 11:19:09 +00007461 if (load_kind == HLoadString::LoadKind::kBssEntry) {
7462 if (!kUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00007463 // Rely on the pResolveString and marking to save everything we need, including temps.
7464 // Note that IP may be clobbered by saving/restoring the live register (only one thanks
7465 // to the custom calling convention) or by marking, so we request a different temp.
Artem Serovd4cc5b22016-11-04 11:19:09 +00007466 locations->AddTemp(Location::RequiresRegister());
7467 RegisterSet caller_saves = RegisterSet::Empty();
7468 InvokeRuntimeCallingConventionARMVIXL calling_convention;
7469 caller_saves.Add(LocationFrom(calling_convention.GetRegisterAt(0)));
7470 // TODO: Add GetReturnLocation() to the calling convention so that we can DCHECK()
7471 // that the the kPrimNot result register is the same as the first argument register.
7472 locations->SetCustomSlowPathCallerSaves(caller_saves);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01007473 if (kUseBakerReadBarrier && kBakerReadBarrierLinkTimeThunksEnableForGcRoots) {
7474 locations->AddTemp(Location::RegisterLocation(kBakerCcEntrypointRegister.GetCode()));
7475 }
Artem Serovd4cc5b22016-11-04 11:19:09 +00007476 } else {
7477 // For non-Baker read barrier we have a temp-clobbering call.
7478 }
7479 }
Artem Serov02d37832016-10-25 15:25:33 +01007480 }
7481}
7482
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007483// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
7484// move.
7485void InstructionCodeGeneratorARMVIXL::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Artem Serovd4cc5b22016-11-04 11:19:09 +00007486 LocationSummary* locations = load->GetLocations();
7487 Location out_loc = locations->Out();
7488 vixl32::Register out = OutputRegister(load);
7489 HLoadString::LoadKind load_kind = load->GetLoadKind();
7490
7491 switch (load_kind) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00007492 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
7493 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
7494 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007495 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Artem Serovd4cc5b22016-11-04 11:19:09 +00007496 codegen_->EmitMovwMovtPlaceholder(labels, out);
7497 return; // No dex cache slow path.
7498 }
7499 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007500 uint32_t address = dchecked_integral_cast<uint32_t>(
7501 reinterpret_cast<uintptr_t>(load->GetString().Get()));
7502 DCHECK_NE(address, 0u);
Artem Serovc5fcb442016-12-02 19:19:58 +00007503 __ Ldr(out, codegen_->DeduplicateBootImageAddressLiteral(address));
7504 return; // No dex cache slow path.
Artem Serovd4cc5b22016-11-04 11:19:09 +00007505 }
7506 case HLoadString::LoadKind::kBssEntry: {
7507 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markoea4c1262017-02-06 19:59:33 +00007508 vixl32::Register temp = (!kUseReadBarrier || kUseBakerReadBarrier)
7509 ? RegisterFrom(locations->GetTemp(0))
7510 : out;
Artem Serovd4cc5b22016-11-04 11:19:09 +00007511 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007512 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Artem Serovd4cc5b22016-11-04 11:19:09 +00007513 codegen_->EmitMovwMovtPlaceholder(labels, temp);
7514 GenerateGcRootFieldLoad(load, out_loc, temp, /* offset */ 0, kCompilerReadBarrierOption);
7515 LoadStringSlowPathARMVIXL* slow_path =
7516 new (GetGraph()->GetArena()) LoadStringSlowPathARMVIXL(load);
7517 codegen_->AddSlowPath(slow_path);
7518 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
7519 __ Bind(slow_path->GetExitLabel());
7520 return;
7521 }
7522 case HLoadString::LoadKind::kJitTableAddress: {
Artem Serovc5fcb442016-12-02 19:19:58 +00007523 __ Ldr(out, codegen_->DeduplicateJitStringLiteral(load->GetDexFile(),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007524 load->GetStringIndex(),
7525 load->GetString()));
Artem Serovc5fcb442016-12-02 19:19:58 +00007526 // /* GcRoot<mirror::String> */ out = *out
7527 GenerateGcRootFieldLoad(load, out_loc, out, /* offset */ 0, kCompilerReadBarrierOption);
7528 return;
Artem Serovd4cc5b22016-11-04 11:19:09 +00007529 }
7530 default:
7531 break;
7532 }
Artem Serov02d37832016-10-25 15:25:33 +01007533
7534 // TODO: Re-add the compiler code to do string dex cache lookup again.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007535 DCHECK_EQ(load->GetLoadKind(), HLoadString::LoadKind::kRuntimeCall);
Artem Serov02d37832016-10-25 15:25:33 +01007536 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Andreas Gampe8a0128a2016-11-28 07:38:35 -08007537 __ Mov(calling_convention.GetRegisterAt(0), load->GetStringIndex().index_);
Artem Serov02d37832016-10-25 15:25:33 +01007538 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
7539 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
7540}
7541
7542static int32_t GetExceptionTlsOffset() {
7543 return Thread::ExceptionOffset<kArmPointerSize>().Int32Value();
7544}
7545
7546void LocationsBuilderARMVIXL::VisitLoadException(HLoadException* load) {
7547 LocationSummary* locations =
7548 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
7549 locations->SetOut(Location::RequiresRegister());
7550}
7551
7552void InstructionCodeGeneratorARMVIXL::VisitLoadException(HLoadException* load) {
7553 vixl32::Register out = OutputRegister(load);
7554 GetAssembler()->LoadFromOffset(kLoadWord, out, tr, GetExceptionTlsOffset());
7555}
7556
7557
7558void LocationsBuilderARMVIXL::VisitClearException(HClearException* clear) {
7559 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
7560}
7561
7562void InstructionCodeGeneratorARMVIXL::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
7563 UseScratchRegisterScope temps(GetVIXLAssembler());
7564 vixl32::Register temp = temps.Acquire();
7565 __ Mov(temp, 0);
7566 GetAssembler()->StoreToOffset(kStoreWord, temp, tr, GetExceptionTlsOffset());
7567}
7568
7569void LocationsBuilderARMVIXL::VisitThrow(HThrow* instruction) {
7570 LocationSummary* locations =
7571 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
7572 InvokeRuntimeCallingConventionARMVIXL calling_convention;
7573 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
7574}
7575
7576void InstructionCodeGeneratorARMVIXL::VisitThrow(HThrow* instruction) {
7577 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
7578 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
7579}
7580
Artem Serov657022c2016-11-23 14:19:38 +00007581// Temp is used for read barrier.
7582static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
7583 if (kEmitCompilerReadBarrier &&
7584 (kUseBakerReadBarrier ||
7585 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
7586 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
7587 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
7588 return 1;
7589 }
7590 return 0;
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007591}
7592
Artem Serov657022c2016-11-23 14:19:38 +00007593// Interface case has 3 temps, one for holding the number of interfaces, one for the current
7594// interface pointer, one for loading the current interface.
7595// The other checks have one temp for loading the object's class.
7596static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
7597 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
7598 return 3;
7599 }
7600 return 1 + NumberOfInstanceOfTemps(type_check_kind);
7601}
Artem Serovcfbe9132016-10-14 15:58:56 +01007602
7603void LocationsBuilderARMVIXL::VisitInstanceOf(HInstanceOf* instruction) {
7604 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
7605 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
7606 bool baker_read_barrier_slow_path = false;
7607 switch (type_check_kind) {
7608 case TypeCheckKind::kExactCheck:
7609 case TypeCheckKind::kAbstractClassCheck:
7610 case TypeCheckKind::kClassHierarchyCheck:
7611 case TypeCheckKind::kArrayObjectCheck:
7612 call_kind =
7613 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
7614 baker_read_barrier_slow_path = kUseBakerReadBarrier;
7615 break;
7616 case TypeCheckKind::kArrayCheck:
7617 case TypeCheckKind::kUnresolvedCheck:
7618 case TypeCheckKind::kInterfaceCheck:
7619 call_kind = LocationSummary::kCallOnSlowPath;
7620 break;
7621 }
7622
7623 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
7624 if (baker_read_barrier_slow_path) {
7625 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
7626 }
7627 locations->SetInAt(0, Location::RequiresRegister());
7628 locations->SetInAt(1, Location::RequiresRegister());
7629 // The "out" register is used as a temporary, so it overlaps with the inputs.
7630 // Note that TypeCheckSlowPathARM uses this register too.
7631 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Artem Serov657022c2016-11-23 14:19:38 +00007632 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01007633 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
7634 codegen_->MaybeAddBakerCcEntrypointTempForFields(locations);
7635 }
Artem Serovcfbe9132016-10-14 15:58:56 +01007636}
7637
7638void InstructionCodeGeneratorARMVIXL::VisitInstanceOf(HInstanceOf* instruction) {
7639 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
7640 LocationSummary* locations = instruction->GetLocations();
7641 Location obj_loc = locations->InAt(0);
7642 vixl32::Register obj = InputRegisterAt(instruction, 0);
7643 vixl32::Register cls = InputRegisterAt(instruction, 1);
7644 Location out_loc = locations->Out();
7645 vixl32::Register out = OutputRegister(instruction);
Artem Serov657022c2016-11-23 14:19:38 +00007646 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
7647 DCHECK_LE(num_temps, 1u);
7648 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Artem Serovcfbe9132016-10-14 15:58:56 +01007649 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
7650 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
7651 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
7652 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007653 vixl32::Label done;
7654 vixl32::Label* const final_label = codegen_->GetFinalLabel(instruction, &done);
Artem Serovcfbe9132016-10-14 15:58:56 +01007655 SlowPathCodeARMVIXL* slow_path = nullptr;
7656
7657 // Return 0 if `obj` is null.
7658 // avoid null check if we know obj is not null.
7659 if (instruction->MustDoNullCheck()) {
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007660 DCHECK(!out.Is(obj));
7661 __ Mov(out, 0);
7662 __ CompareAndBranchIfZero(obj, final_label, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007663 }
7664
Artem Serovcfbe9132016-10-14 15:58:56 +01007665 switch (type_check_kind) {
7666 case TypeCheckKind::kExactCheck: {
Mathieu Chartier6beced42016-11-15 15:51:31 -08007667 // /* HeapReference<Class> */ out = obj->klass_
7668 GenerateReferenceLoadTwoRegisters(instruction,
7669 out_loc,
7670 obj_loc,
7671 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00007672 maybe_temp_loc,
7673 kCompilerReadBarrierOption);
Artem Serovcfbe9132016-10-14 15:58:56 +01007674 // Classes must be equal for the instanceof to succeed.
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007675 __ Cmp(out, cls);
7676 // We speculatively set the result to false without changing the condition
7677 // flags, which allows us to avoid some branching later.
7678 __ Mov(LeaveFlags, out, 0);
7679
7680 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
7681 // we check that the output is in a low register, so that a 16-bit MOV
7682 // encoding can be used.
7683 if (out.IsLow()) {
7684 // We use the scope because of the IT block that follows.
7685 ExactAssemblyScope guard(GetVIXLAssembler(),
7686 2 * vixl32::k16BitT32InstructionSizeInBytes,
7687 CodeBufferCheckScope::kExactSize);
7688
7689 __ it(eq);
7690 __ mov(eq, out, 1);
7691 } else {
7692 __ B(ne, final_label, /* far_target */ false);
7693 __ Mov(out, 1);
7694 }
7695
Artem Serovcfbe9132016-10-14 15:58:56 +01007696 break;
7697 }
7698
7699 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier6beced42016-11-15 15:51:31 -08007700 // /* HeapReference<Class> */ out = obj->klass_
7701 GenerateReferenceLoadTwoRegisters(instruction,
7702 out_loc,
7703 obj_loc,
7704 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00007705 maybe_temp_loc,
7706 kCompilerReadBarrierOption);
Artem Serovcfbe9132016-10-14 15:58:56 +01007707 // If the class is abstract, we eagerly fetch the super class of the
7708 // object to avoid doing a comparison we know will fail.
7709 vixl32::Label loop;
7710 __ Bind(&loop);
7711 // /* HeapReference<Class> */ out = out->super_class_
Artem Serov657022c2016-11-23 14:19:38 +00007712 GenerateReferenceLoadOneRegister(instruction,
7713 out_loc,
7714 super_offset,
7715 maybe_temp_loc,
7716 kCompilerReadBarrierOption);
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007717 // If `out` is null, we use it for the result, and jump to the final label.
Anton Kirilov6f644202017-02-27 18:29:45 +00007718 __ CompareAndBranchIfZero(out, final_label, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007719 __ Cmp(out, cls);
Artem Serov517d9f62016-12-12 15:51:15 +00007720 __ B(ne, &loop, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007721 __ Mov(out, 1);
Artem Serovcfbe9132016-10-14 15:58:56 +01007722 break;
7723 }
7724
7725 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier6beced42016-11-15 15:51:31 -08007726 // /* HeapReference<Class> */ out = obj->klass_
7727 GenerateReferenceLoadTwoRegisters(instruction,
7728 out_loc,
7729 obj_loc,
7730 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00007731 maybe_temp_loc,
7732 kCompilerReadBarrierOption);
Artem Serovcfbe9132016-10-14 15:58:56 +01007733 // Walk over the class hierarchy to find a match.
7734 vixl32::Label loop, success;
7735 __ Bind(&loop);
7736 __ Cmp(out, cls);
Artem Serov517d9f62016-12-12 15:51:15 +00007737 __ B(eq, &success, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007738 // /* HeapReference<Class> */ out = out->super_class_
Artem Serov657022c2016-11-23 14:19:38 +00007739 GenerateReferenceLoadOneRegister(instruction,
7740 out_loc,
7741 super_offset,
7742 maybe_temp_loc,
7743 kCompilerReadBarrierOption);
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007744 // This is essentially a null check, but it sets the condition flags to the
7745 // proper value for the code that follows the loop, i.e. not `eq`.
7746 __ Cmp(out, 1);
7747 __ B(hs, &loop, /* far_target */ false);
7748
7749 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
7750 // we check that the output is in a low register, so that a 16-bit MOV
7751 // encoding can be used.
7752 if (out.IsLow()) {
7753 // If `out` is null, we use it for the result, and the condition flags
7754 // have already been set to `ne`, so the IT block that comes afterwards
7755 // (and which handles the successful case) turns into a NOP (instead of
7756 // overwriting `out`).
7757 __ Bind(&success);
7758
7759 // We use the scope because of the IT block that follows.
7760 ExactAssemblyScope guard(GetVIXLAssembler(),
7761 2 * vixl32::k16BitT32InstructionSizeInBytes,
7762 CodeBufferCheckScope::kExactSize);
7763
7764 // There is only one branch to the `success` label (which is bound to this
7765 // IT block), and it has the same condition, `eq`, so in that case the MOV
7766 // is executed.
7767 __ it(eq);
7768 __ mov(eq, out, 1);
7769 } else {
7770 // If `out` is null, we use it for the result, and jump to the final label.
Anton Kirilov6f644202017-02-27 18:29:45 +00007771 __ B(final_label);
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007772 __ Bind(&success);
7773 __ Mov(out, 1);
Artem Serovcfbe9132016-10-14 15:58:56 +01007774 }
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007775
Artem Serovcfbe9132016-10-14 15:58:56 +01007776 break;
7777 }
7778
7779 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier6beced42016-11-15 15:51:31 -08007780 // /* HeapReference<Class> */ out = obj->klass_
7781 GenerateReferenceLoadTwoRegisters(instruction,
7782 out_loc,
7783 obj_loc,
7784 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00007785 maybe_temp_loc,
7786 kCompilerReadBarrierOption);
Artem Serovcfbe9132016-10-14 15:58:56 +01007787 // Do an exact check.
7788 vixl32::Label exact_check;
7789 __ Cmp(out, cls);
Artem Serov517d9f62016-12-12 15:51:15 +00007790 __ B(eq, &exact_check, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007791 // Otherwise, we need to check that the object's class is a non-primitive array.
7792 // /* HeapReference<Class> */ out = out->component_type_
Artem Serov657022c2016-11-23 14:19:38 +00007793 GenerateReferenceLoadOneRegister(instruction,
7794 out_loc,
7795 component_offset,
7796 maybe_temp_loc,
7797 kCompilerReadBarrierOption);
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007798 // If `out` is null, we use it for the result, and jump to the final label.
Anton Kirilov6f644202017-02-27 18:29:45 +00007799 __ CompareAndBranchIfZero(out, final_label, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007800 GetAssembler()->LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
7801 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007802 __ Cmp(out, 0);
7803 // We speculatively set the result to false without changing the condition
7804 // flags, which allows us to avoid some branching later.
7805 __ Mov(LeaveFlags, out, 0);
7806
7807 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
7808 // we check that the output is in a low register, so that a 16-bit MOV
7809 // encoding can be used.
7810 if (out.IsLow()) {
7811 __ Bind(&exact_check);
7812
7813 // We use the scope because of the IT block that follows.
7814 ExactAssemblyScope guard(GetVIXLAssembler(),
7815 2 * vixl32::k16BitT32InstructionSizeInBytes,
7816 CodeBufferCheckScope::kExactSize);
7817
7818 __ it(eq);
7819 __ mov(eq, out, 1);
7820 } else {
7821 __ B(ne, final_label, /* far_target */ false);
7822 __ Bind(&exact_check);
7823 __ Mov(out, 1);
7824 }
7825
Artem Serovcfbe9132016-10-14 15:58:56 +01007826 break;
7827 }
7828
7829 case TypeCheckKind::kArrayCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00007830 // No read barrier since the slow path will retry upon failure.
Mathieu Chartier6beced42016-11-15 15:51:31 -08007831 // /* HeapReference<Class> */ out = obj->klass_
7832 GenerateReferenceLoadTwoRegisters(instruction,
7833 out_loc,
7834 obj_loc,
7835 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00007836 maybe_temp_loc,
7837 kWithoutReadBarrier);
Artem Serovcfbe9132016-10-14 15:58:56 +01007838 __ Cmp(out, cls);
7839 DCHECK(locations->OnlyCallsOnSlowPath());
7840 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARMVIXL(instruction,
7841 /* is_fatal */ false);
7842 codegen_->AddSlowPath(slow_path);
7843 __ B(ne, slow_path->GetEntryLabel());
7844 __ Mov(out, 1);
Artem Serovcfbe9132016-10-14 15:58:56 +01007845 break;
7846 }
7847
7848 case TypeCheckKind::kUnresolvedCheck:
7849 case TypeCheckKind::kInterfaceCheck: {
7850 // Note that we indeed only call on slow path, but we always go
7851 // into the slow path for the unresolved and interface check
7852 // cases.
7853 //
7854 // We cannot directly call the InstanceofNonTrivial runtime
7855 // entry point without resorting to a type checking slow path
7856 // here (i.e. by calling InvokeRuntime directly), as it would
7857 // require to assign fixed registers for the inputs of this
7858 // HInstanceOf instruction (following the runtime calling
7859 // convention), which might be cluttered by the potential first
7860 // read barrier emission at the beginning of this method.
7861 //
7862 // TODO: Introduce a new runtime entry point taking the object
7863 // to test (instead of its class) as argument, and let it deal
7864 // with the read barrier issues. This will let us refactor this
7865 // case of the `switch` code as it was previously (with a direct
7866 // call to the runtime not using a type checking slow path).
7867 // This should also be beneficial for the other cases above.
7868 DCHECK(locations->OnlyCallsOnSlowPath());
7869 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARMVIXL(instruction,
7870 /* is_fatal */ false);
7871 codegen_->AddSlowPath(slow_path);
7872 __ B(slow_path->GetEntryLabel());
Artem Serovcfbe9132016-10-14 15:58:56 +01007873 break;
7874 }
7875 }
7876
Artem Serovcfbe9132016-10-14 15:58:56 +01007877 if (done.IsReferenced()) {
7878 __ Bind(&done);
7879 }
7880
7881 if (slow_path != nullptr) {
7882 __ Bind(slow_path->GetExitLabel());
7883 }
7884}
7885
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007886void LocationsBuilderARMVIXL::VisitCheckCast(HCheckCast* instruction) {
7887 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
7888 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
7889
7890 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
7891 switch (type_check_kind) {
7892 case TypeCheckKind::kExactCheck:
7893 case TypeCheckKind::kAbstractClassCheck:
7894 case TypeCheckKind::kClassHierarchyCheck:
7895 case TypeCheckKind::kArrayObjectCheck:
7896 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
7897 LocationSummary::kCallOnSlowPath :
7898 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
7899 break;
7900 case TypeCheckKind::kArrayCheck:
7901 case TypeCheckKind::kUnresolvedCheck:
7902 case TypeCheckKind::kInterfaceCheck:
7903 call_kind = LocationSummary::kCallOnSlowPath;
7904 break;
7905 }
7906
7907 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
7908 locations->SetInAt(0, Location::RequiresRegister());
7909 locations->SetInAt(1, Location::RequiresRegister());
Artem Serov657022c2016-11-23 14:19:38 +00007910 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007911}
7912
7913void InstructionCodeGeneratorARMVIXL::VisitCheckCast(HCheckCast* instruction) {
7914 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
7915 LocationSummary* locations = instruction->GetLocations();
7916 Location obj_loc = locations->InAt(0);
7917 vixl32::Register obj = InputRegisterAt(instruction, 0);
7918 vixl32::Register cls = InputRegisterAt(instruction, 1);
7919 Location temp_loc = locations->GetTemp(0);
7920 vixl32::Register temp = RegisterFrom(temp_loc);
Artem Serov657022c2016-11-23 14:19:38 +00007921 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
7922 DCHECK_LE(num_temps, 3u);
7923 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
7924 Location maybe_temp3_loc = (num_temps >= 3) ? locations->GetTemp(2) : Location::NoLocation();
7925 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
7926 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
7927 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
7928 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
7929 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
7930 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
7931 const uint32_t object_array_data_offset =
7932 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007933
Artem Serov657022c2016-11-23 14:19:38 +00007934 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
7935 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
7936 // read barriers is done for performance and code size reasons.
7937 bool is_type_check_slow_path_fatal = false;
7938 if (!kEmitCompilerReadBarrier) {
7939 is_type_check_slow_path_fatal =
7940 (type_check_kind == TypeCheckKind::kExactCheck ||
7941 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
7942 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
7943 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
7944 !instruction->CanThrowIntoCatchBlock();
7945 }
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007946 SlowPathCodeARMVIXL* type_check_slow_path =
7947 new (GetGraph()->GetArena()) TypeCheckSlowPathARMVIXL(instruction,
7948 is_type_check_slow_path_fatal);
7949 codegen_->AddSlowPath(type_check_slow_path);
7950
7951 vixl32::Label done;
Anton Kirilov6f644202017-02-27 18:29:45 +00007952 vixl32::Label* final_label = codegen_->GetFinalLabel(instruction, &done);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007953 // Avoid null check if we know obj is not null.
7954 if (instruction->MustDoNullCheck()) {
Anton Kirilov6f644202017-02-27 18:29:45 +00007955 __ CompareAndBranchIfZero(obj, final_label, /* far_target */ false);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007956 }
7957
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007958 switch (type_check_kind) {
7959 case TypeCheckKind::kExactCheck:
7960 case TypeCheckKind::kArrayCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00007961 // /* HeapReference<Class> */ temp = obj->klass_
7962 GenerateReferenceLoadTwoRegisters(instruction,
7963 temp_loc,
7964 obj_loc,
7965 class_offset,
7966 maybe_temp2_loc,
7967 kWithoutReadBarrier);
7968
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007969 __ Cmp(temp, cls);
7970 // Jump to slow path for throwing the exception or doing a
7971 // more involved array check.
7972 __ B(ne, type_check_slow_path->GetEntryLabel());
7973 break;
7974 }
7975
7976 case TypeCheckKind::kAbstractClassCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00007977 // /* HeapReference<Class> */ temp = obj->klass_
7978 GenerateReferenceLoadTwoRegisters(instruction,
7979 temp_loc,
7980 obj_loc,
7981 class_offset,
7982 maybe_temp2_loc,
7983 kWithoutReadBarrier);
7984
Artem Serovcfbe9132016-10-14 15:58:56 +01007985 // If the class is abstract, we eagerly fetch the super class of the
7986 // object to avoid doing a comparison we know will fail.
7987 vixl32::Label loop;
7988 __ Bind(&loop);
7989 // /* HeapReference<Class> */ temp = temp->super_class_
Artem Serov657022c2016-11-23 14:19:38 +00007990 GenerateReferenceLoadOneRegister(instruction,
7991 temp_loc,
7992 super_offset,
7993 maybe_temp2_loc,
7994 kWithoutReadBarrier);
Artem Serovcfbe9132016-10-14 15:58:56 +01007995
7996 // If the class reference currently in `temp` is null, jump to the slow path to throw the
7997 // exception.
xueliang.zhongf51bc622016-11-04 09:23:32 +00007998 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
Artem Serovcfbe9132016-10-14 15:58:56 +01007999
8000 // Otherwise, compare the classes.
8001 __ Cmp(temp, cls);
Artem Serov517d9f62016-12-12 15:51:15 +00008002 __ B(ne, &loop, /* far_target */ false);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008003 break;
8004 }
8005
8006 case TypeCheckKind::kClassHierarchyCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00008007 // /* HeapReference<Class> */ temp = obj->klass_
8008 GenerateReferenceLoadTwoRegisters(instruction,
8009 temp_loc,
8010 obj_loc,
8011 class_offset,
8012 maybe_temp2_loc,
8013 kWithoutReadBarrier);
8014
Artem Serovcfbe9132016-10-14 15:58:56 +01008015 // Walk over the class hierarchy to find a match.
8016 vixl32::Label loop;
8017 __ Bind(&loop);
8018 __ Cmp(temp, cls);
Anton Kirilov6f644202017-02-27 18:29:45 +00008019 __ B(eq, final_label, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01008020
8021 // /* HeapReference<Class> */ temp = temp->super_class_
Artem Serov657022c2016-11-23 14:19:38 +00008022 GenerateReferenceLoadOneRegister(instruction,
8023 temp_loc,
8024 super_offset,
8025 maybe_temp2_loc,
8026 kWithoutReadBarrier);
Artem Serovcfbe9132016-10-14 15:58:56 +01008027
8028 // If the class reference currently in `temp` is null, jump to the slow path to throw the
8029 // exception.
xueliang.zhongf51bc622016-11-04 09:23:32 +00008030 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
Artem Serovcfbe9132016-10-14 15:58:56 +01008031 // Otherwise, jump to the beginning of the loop.
8032 __ B(&loop);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008033 break;
8034 }
8035
Artem Serovcfbe9132016-10-14 15:58:56 +01008036 case TypeCheckKind::kArrayObjectCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00008037 // /* HeapReference<Class> */ temp = obj->klass_
8038 GenerateReferenceLoadTwoRegisters(instruction,
8039 temp_loc,
8040 obj_loc,
8041 class_offset,
8042 maybe_temp2_loc,
8043 kWithoutReadBarrier);
8044
Artem Serovcfbe9132016-10-14 15:58:56 +01008045 // Do an exact check.
8046 __ Cmp(temp, cls);
Anton Kirilov6f644202017-02-27 18:29:45 +00008047 __ B(eq, final_label, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01008048
8049 // Otherwise, we need to check that the object's class is a non-primitive array.
8050 // /* HeapReference<Class> */ temp = temp->component_type_
Artem Serov657022c2016-11-23 14:19:38 +00008051 GenerateReferenceLoadOneRegister(instruction,
8052 temp_loc,
8053 component_offset,
8054 maybe_temp2_loc,
8055 kWithoutReadBarrier);
Artem Serovcfbe9132016-10-14 15:58:56 +01008056 // If the component type is null, jump to the slow path to throw the exception.
xueliang.zhongf51bc622016-11-04 09:23:32 +00008057 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
Artem Serovcfbe9132016-10-14 15:58:56 +01008058 // Otherwise,the object is indeed an array, jump to label `check_non_primitive_component_type`
8059 // to further check that this component type is not a primitive type.
8060 GetAssembler()->LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
8061 static_assert(Primitive::kPrimNot == 0, "Expected 0 for art::Primitive::kPrimNot");
xueliang.zhongf51bc622016-11-04 09:23:32 +00008062 __ CompareAndBranchIfNonZero(temp, type_check_slow_path->GetEntryLabel());
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008063 break;
8064 }
8065
8066 case TypeCheckKind::kUnresolvedCheck:
Artem Serov657022c2016-11-23 14:19:38 +00008067 // We always go into the type check slow path for the unresolved check case.
Artem Serovcfbe9132016-10-14 15:58:56 +01008068 // We cannot directly call the CheckCast runtime entry point
8069 // without resorting to a type checking slow path here (i.e. by
8070 // calling InvokeRuntime directly), as it would require to
8071 // assign fixed registers for the inputs of this HInstanceOf
8072 // instruction (following the runtime calling convention), which
8073 // might be cluttered by the potential first read barrier
8074 // emission at the beginning of this method.
Artem Serov657022c2016-11-23 14:19:38 +00008075
Artem Serovcfbe9132016-10-14 15:58:56 +01008076 __ B(type_check_slow_path->GetEntryLabel());
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008077 break;
Artem Serov657022c2016-11-23 14:19:38 +00008078
8079 case TypeCheckKind::kInterfaceCheck: {
8080 // Avoid read barriers to improve performance of the fast path. We can not get false
8081 // positives by doing this.
8082 // /* HeapReference<Class> */ temp = obj->klass_
8083 GenerateReferenceLoadTwoRegisters(instruction,
8084 temp_loc,
8085 obj_loc,
8086 class_offset,
8087 maybe_temp2_loc,
8088 kWithoutReadBarrier);
8089
8090 // /* HeapReference<Class> */ temp = temp->iftable_
8091 GenerateReferenceLoadTwoRegisters(instruction,
8092 temp_loc,
8093 temp_loc,
8094 iftable_offset,
8095 maybe_temp2_loc,
8096 kWithoutReadBarrier);
8097 // Iftable is never null.
8098 __ Ldr(RegisterFrom(maybe_temp2_loc), MemOperand(temp, array_length_offset));
8099 // Loop through the iftable and check if any class matches.
8100 vixl32::Label start_loop;
8101 __ Bind(&start_loop);
8102 __ CompareAndBranchIfZero(RegisterFrom(maybe_temp2_loc),
8103 type_check_slow_path->GetEntryLabel());
8104 __ Ldr(RegisterFrom(maybe_temp3_loc), MemOperand(temp, object_array_data_offset));
8105 GetAssembler()->MaybeUnpoisonHeapReference(RegisterFrom(maybe_temp3_loc));
8106 // Go to next interface.
8107 __ Add(temp, temp, Operand::From(2 * kHeapReferenceSize));
8108 __ Sub(RegisterFrom(maybe_temp2_loc), RegisterFrom(maybe_temp2_loc), 2);
8109 // Compare the classes and continue the loop if they do not match.
8110 __ Cmp(cls, RegisterFrom(maybe_temp3_loc));
Artem Serov517d9f62016-12-12 15:51:15 +00008111 __ B(ne, &start_loop, /* far_target */ false);
Artem Serov657022c2016-11-23 14:19:38 +00008112 break;
8113 }
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008114 }
Anton Kirilov6f644202017-02-27 18:29:45 +00008115 if (done.IsReferenced()) {
8116 __ Bind(&done);
8117 }
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008118
8119 __ Bind(type_check_slow_path->GetExitLabel());
8120}
8121
Artem Serov551b28f2016-10-18 19:11:30 +01008122void LocationsBuilderARMVIXL::VisitMonitorOperation(HMonitorOperation* instruction) {
8123 LocationSummary* locations =
8124 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
8125 InvokeRuntimeCallingConventionARMVIXL calling_convention;
8126 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
8127}
8128
8129void InstructionCodeGeneratorARMVIXL::VisitMonitorOperation(HMonitorOperation* instruction) {
8130 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
8131 instruction,
8132 instruction->GetDexPc());
8133 if (instruction->IsEnter()) {
8134 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
8135 } else {
8136 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
8137 }
8138}
8139
Artem Serov02109dd2016-09-23 17:17:54 +01008140void LocationsBuilderARMVIXL::VisitAnd(HAnd* instruction) {
8141 HandleBitwiseOperation(instruction, AND);
8142}
8143
8144void LocationsBuilderARMVIXL::VisitOr(HOr* instruction) {
8145 HandleBitwiseOperation(instruction, ORR);
8146}
8147
8148void LocationsBuilderARMVIXL::VisitXor(HXor* instruction) {
8149 HandleBitwiseOperation(instruction, EOR);
8150}
8151
8152void LocationsBuilderARMVIXL::HandleBitwiseOperation(HBinaryOperation* instruction, Opcode opcode) {
8153 LocationSummary* locations =
8154 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
8155 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
8156 || instruction->GetResultType() == Primitive::kPrimLong);
8157 // Note: GVN reorders commutative operations to have the constant on the right hand side.
8158 locations->SetInAt(0, Location::RequiresRegister());
8159 locations->SetInAt(1, ArmEncodableConstantOrRegister(instruction->InputAt(1), opcode));
8160 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8161}
8162
8163void InstructionCodeGeneratorARMVIXL::VisitAnd(HAnd* instruction) {
8164 HandleBitwiseOperation(instruction);
8165}
8166
8167void InstructionCodeGeneratorARMVIXL::VisitOr(HOr* instruction) {
8168 HandleBitwiseOperation(instruction);
8169}
8170
8171void InstructionCodeGeneratorARMVIXL::VisitXor(HXor* instruction) {
8172 HandleBitwiseOperation(instruction);
8173}
8174
Artem Serov2bbc9532016-10-21 11:51:50 +01008175void LocationsBuilderARMVIXL::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
8176 LocationSummary* locations =
8177 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
8178 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
8179 || instruction->GetResultType() == Primitive::kPrimLong);
8180
8181 locations->SetInAt(0, Location::RequiresRegister());
8182 locations->SetInAt(1, Location::RequiresRegister());
8183 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8184}
8185
8186void InstructionCodeGeneratorARMVIXL::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
8187 LocationSummary* locations = instruction->GetLocations();
8188 Location first = locations->InAt(0);
8189 Location second = locations->InAt(1);
8190 Location out = locations->Out();
8191
8192 if (instruction->GetResultType() == Primitive::kPrimInt) {
8193 vixl32::Register first_reg = RegisterFrom(first);
8194 vixl32::Register second_reg = RegisterFrom(second);
8195 vixl32::Register out_reg = RegisterFrom(out);
8196
8197 switch (instruction->GetOpKind()) {
8198 case HInstruction::kAnd:
8199 __ Bic(out_reg, first_reg, second_reg);
8200 break;
8201 case HInstruction::kOr:
8202 __ Orn(out_reg, first_reg, second_reg);
8203 break;
8204 // There is no EON on arm.
8205 case HInstruction::kXor:
8206 default:
8207 LOG(FATAL) << "Unexpected instruction " << instruction->DebugName();
8208 UNREACHABLE();
8209 }
8210 return;
8211
8212 } else {
8213 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
8214 vixl32::Register first_low = LowRegisterFrom(first);
8215 vixl32::Register first_high = HighRegisterFrom(first);
8216 vixl32::Register second_low = LowRegisterFrom(second);
8217 vixl32::Register second_high = HighRegisterFrom(second);
8218 vixl32::Register out_low = LowRegisterFrom(out);
8219 vixl32::Register out_high = HighRegisterFrom(out);
8220
8221 switch (instruction->GetOpKind()) {
8222 case HInstruction::kAnd:
8223 __ Bic(out_low, first_low, second_low);
8224 __ Bic(out_high, first_high, second_high);
8225 break;
8226 case HInstruction::kOr:
8227 __ Orn(out_low, first_low, second_low);
8228 __ Orn(out_high, first_high, second_high);
8229 break;
8230 // There is no EON on arm.
8231 case HInstruction::kXor:
8232 default:
8233 LOG(FATAL) << "Unexpected instruction " << instruction->DebugName();
8234 UNREACHABLE();
8235 }
8236 }
8237}
8238
Anton Kirilov74234da2017-01-13 14:42:47 +00008239void LocationsBuilderARMVIXL::VisitDataProcWithShifterOp(
8240 HDataProcWithShifterOp* instruction) {
8241 DCHECK(instruction->GetType() == Primitive::kPrimInt ||
8242 instruction->GetType() == Primitive::kPrimLong);
8243 LocationSummary* locations =
8244 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
8245 const bool overlap = instruction->GetType() == Primitive::kPrimLong &&
8246 HDataProcWithShifterOp::IsExtensionOp(instruction->GetOpKind());
8247
8248 locations->SetInAt(0, Location::RequiresRegister());
8249 locations->SetInAt(1, Location::RequiresRegister());
8250 locations->SetOut(Location::RequiresRegister(),
8251 overlap ? Location::kOutputOverlap : Location::kNoOutputOverlap);
8252}
8253
8254void InstructionCodeGeneratorARMVIXL::VisitDataProcWithShifterOp(
8255 HDataProcWithShifterOp* instruction) {
8256 const LocationSummary* const locations = instruction->GetLocations();
8257 const HInstruction::InstructionKind kind = instruction->GetInstrKind();
8258 const HDataProcWithShifterOp::OpKind op_kind = instruction->GetOpKind();
8259
8260 if (instruction->GetType() == Primitive::kPrimInt) {
8261 DCHECK(!HDataProcWithShifterOp::IsExtensionOp(op_kind));
8262
8263 const vixl32::Register second = instruction->InputAt(1)->GetType() == Primitive::kPrimLong
8264 ? LowRegisterFrom(locations->InAt(1))
8265 : InputRegisterAt(instruction, 1);
8266
8267 GenerateDataProcInstruction(kind,
8268 OutputRegister(instruction),
8269 InputRegisterAt(instruction, 0),
8270 Operand(second,
8271 ShiftFromOpKind(op_kind),
8272 instruction->GetShiftAmount()),
8273 codegen_);
8274 } else {
8275 DCHECK_EQ(instruction->GetType(), Primitive::kPrimLong);
8276
8277 if (HDataProcWithShifterOp::IsExtensionOp(op_kind)) {
8278 const vixl32::Register second = InputRegisterAt(instruction, 1);
8279
8280 DCHECK(!LowRegisterFrom(locations->Out()).Is(second));
8281 GenerateDataProc(kind,
8282 locations->Out(),
8283 locations->InAt(0),
8284 second,
8285 Operand(second, ShiftType::ASR, 31),
8286 codegen_);
8287 } else {
8288 GenerateLongDataProc(instruction, codegen_);
8289 }
8290 }
8291}
8292
Artem Serov02109dd2016-09-23 17:17:54 +01008293// TODO(VIXL): Remove optimizations in the helper when they are implemented in vixl.
8294void InstructionCodeGeneratorARMVIXL::GenerateAndConst(vixl32::Register out,
8295 vixl32::Register first,
8296 uint32_t value) {
8297 // Optimize special cases for individual halfs of `and-long` (`and` is simplified earlier).
8298 if (value == 0xffffffffu) {
8299 if (!out.Is(first)) {
8300 __ Mov(out, first);
8301 }
8302 return;
8303 }
8304 if (value == 0u) {
8305 __ Mov(out, 0);
8306 return;
8307 }
8308 if (GetAssembler()->ShifterOperandCanHold(AND, value)) {
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00008309 __ And(out, first, value);
8310 } else if (GetAssembler()->ShifterOperandCanHold(BIC, ~value)) {
8311 __ Bic(out, first, ~value);
Artem Serov02109dd2016-09-23 17:17:54 +01008312 } else {
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00008313 DCHECK(IsPowerOfTwo(value + 1));
8314 __ Ubfx(out, first, 0, WhichPowerOf2(value + 1));
Artem Serov02109dd2016-09-23 17:17:54 +01008315 }
8316}
8317
8318// TODO(VIXL): Remove optimizations in the helper when they are implemented in vixl.
8319void InstructionCodeGeneratorARMVIXL::GenerateOrrConst(vixl32::Register out,
8320 vixl32::Register first,
8321 uint32_t value) {
8322 // Optimize special cases for individual halfs of `or-long` (`or` is simplified earlier).
8323 if (value == 0u) {
8324 if (!out.Is(first)) {
8325 __ Mov(out, first);
8326 }
8327 return;
8328 }
8329 if (value == 0xffffffffu) {
8330 __ Mvn(out, 0);
8331 return;
8332 }
8333 if (GetAssembler()->ShifterOperandCanHold(ORR, value)) {
8334 __ Orr(out, first, value);
8335 } else {
8336 DCHECK(GetAssembler()->ShifterOperandCanHold(ORN, ~value));
8337 __ Orn(out, first, ~value);
8338 }
8339}
8340
8341// TODO(VIXL): Remove optimizations in the helper when they are implemented in vixl.
8342void InstructionCodeGeneratorARMVIXL::GenerateEorConst(vixl32::Register out,
8343 vixl32::Register first,
8344 uint32_t value) {
8345 // Optimize special case for individual halfs of `xor-long` (`xor` is simplified earlier).
8346 if (value == 0u) {
8347 if (!out.Is(first)) {
8348 __ Mov(out, first);
8349 }
8350 return;
8351 }
8352 __ Eor(out, first, value);
8353}
8354
Anton Kirilovdda43962016-11-21 19:55:20 +00008355void InstructionCodeGeneratorARMVIXL::GenerateAddLongConst(Location out,
8356 Location first,
8357 uint64_t value) {
8358 vixl32::Register out_low = LowRegisterFrom(out);
8359 vixl32::Register out_high = HighRegisterFrom(out);
8360 vixl32::Register first_low = LowRegisterFrom(first);
8361 vixl32::Register first_high = HighRegisterFrom(first);
8362 uint32_t value_low = Low32Bits(value);
8363 uint32_t value_high = High32Bits(value);
8364 if (value_low == 0u) {
8365 if (!out_low.Is(first_low)) {
8366 __ Mov(out_low, first_low);
8367 }
8368 __ Add(out_high, first_high, value_high);
8369 return;
8370 }
8371 __ Adds(out_low, first_low, value_low);
Scott Wakelingbffdc702016-12-07 17:46:03 +00008372 if (GetAssembler()->ShifterOperandCanHold(ADC, value_high, kCcDontCare)) {
Anton Kirilovdda43962016-11-21 19:55:20 +00008373 __ Adc(out_high, first_high, value_high);
Scott Wakelingbffdc702016-12-07 17:46:03 +00008374 } else if (GetAssembler()->ShifterOperandCanHold(SBC, ~value_high, kCcDontCare)) {
Anton Kirilovdda43962016-11-21 19:55:20 +00008375 __ Sbc(out_high, first_high, ~value_high);
8376 } else {
8377 LOG(FATAL) << "Unexpected constant " << value_high;
8378 UNREACHABLE();
8379 }
8380}
8381
Artem Serov02109dd2016-09-23 17:17:54 +01008382void InstructionCodeGeneratorARMVIXL::HandleBitwiseOperation(HBinaryOperation* instruction) {
8383 LocationSummary* locations = instruction->GetLocations();
8384 Location first = locations->InAt(0);
8385 Location second = locations->InAt(1);
8386 Location out = locations->Out();
8387
8388 if (second.IsConstant()) {
8389 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
8390 uint32_t value_low = Low32Bits(value);
8391 if (instruction->GetResultType() == Primitive::kPrimInt) {
8392 vixl32::Register first_reg = InputRegisterAt(instruction, 0);
8393 vixl32::Register out_reg = OutputRegister(instruction);
8394 if (instruction->IsAnd()) {
8395 GenerateAndConst(out_reg, first_reg, value_low);
8396 } else if (instruction->IsOr()) {
8397 GenerateOrrConst(out_reg, first_reg, value_low);
8398 } else {
8399 DCHECK(instruction->IsXor());
8400 GenerateEorConst(out_reg, first_reg, value_low);
8401 }
8402 } else {
8403 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
8404 uint32_t value_high = High32Bits(value);
8405 vixl32::Register first_low = LowRegisterFrom(first);
8406 vixl32::Register first_high = HighRegisterFrom(first);
8407 vixl32::Register out_low = LowRegisterFrom(out);
8408 vixl32::Register out_high = HighRegisterFrom(out);
8409 if (instruction->IsAnd()) {
8410 GenerateAndConst(out_low, first_low, value_low);
8411 GenerateAndConst(out_high, first_high, value_high);
8412 } else if (instruction->IsOr()) {
8413 GenerateOrrConst(out_low, first_low, value_low);
8414 GenerateOrrConst(out_high, first_high, value_high);
8415 } else {
8416 DCHECK(instruction->IsXor());
8417 GenerateEorConst(out_low, first_low, value_low);
8418 GenerateEorConst(out_high, first_high, value_high);
8419 }
8420 }
8421 return;
8422 }
8423
8424 if (instruction->GetResultType() == Primitive::kPrimInt) {
8425 vixl32::Register first_reg = InputRegisterAt(instruction, 0);
8426 vixl32::Register second_reg = InputRegisterAt(instruction, 1);
8427 vixl32::Register out_reg = OutputRegister(instruction);
8428 if (instruction->IsAnd()) {
8429 __ And(out_reg, first_reg, second_reg);
8430 } else if (instruction->IsOr()) {
8431 __ Orr(out_reg, first_reg, second_reg);
8432 } else {
8433 DCHECK(instruction->IsXor());
8434 __ Eor(out_reg, first_reg, second_reg);
8435 }
8436 } else {
8437 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
8438 vixl32::Register first_low = LowRegisterFrom(first);
8439 vixl32::Register first_high = HighRegisterFrom(first);
8440 vixl32::Register second_low = LowRegisterFrom(second);
8441 vixl32::Register second_high = HighRegisterFrom(second);
8442 vixl32::Register out_low = LowRegisterFrom(out);
8443 vixl32::Register out_high = HighRegisterFrom(out);
8444 if (instruction->IsAnd()) {
8445 __ And(out_low, first_low, second_low);
8446 __ And(out_high, first_high, second_high);
8447 } else if (instruction->IsOr()) {
8448 __ Orr(out_low, first_low, second_low);
8449 __ Orr(out_high, first_high, second_high);
8450 } else {
8451 DCHECK(instruction->IsXor());
8452 __ Eor(out_low, first_low, second_low);
8453 __ Eor(out_high, first_high, second_high);
8454 }
8455 }
8456}
8457
Artem Serovcfbe9132016-10-14 15:58:56 +01008458void InstructionCodeGeneratorARMVIXL::GenerateReferenceLoadOneRegister(
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008459 HInstruction* instruction,
Artem Serovcfbe9132016-10-14 15:58:56 +01008460 Location out,
8461 uint32_t offset,
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008462 Location maybe_temp,
8463 ReadBarrierOption read_barrier_option) {
Artem Serovcfbe9132016-10-14 15:58:56 +01008464 vixl32::Register out_reg = RegisterFrom(out);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008465 if (read_barrier_option == kWithReadBarrier) {
8466 CHECK(kEmitCompilerReadBarrier);
8467 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
8468 if (kUseBakerReadBarrier) {
8469 // Load with fast path based Baker's read barrier.
8470 // /* HeapReference<Object> */ out = *(out + offset)
8471 codegen_->GenerateFieldLoadWithBakerReadBarrier(
8472 instruction, out, out_reg, offset, maybe_temp, /* needs_null_check */ false);
8473 } else {
8474 // Load with slow path based read barrier.
8475 // Save the value of `out` into `maybe_temp` before overwriting it
8476 // in the following move operation, as we will need it for the
8477 // read barrier below.
8478 __ Mov(RegisterFrom(maybe_temp), out_reg);
8479 // /* HeapReference<Object> */ out = *(out + offset)
8480 GetAssembler()->LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
8481 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
8482 }
Artem Serovcfbe9132016-10-14 15:58:56 +01008483 } else {
8484 // Plain load with no read barrier.
8485 // /* HeapReference<Object> */ out = *(out + offset)
8486 GetAssembler()->LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
8487 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
8488 }
8489}
8490
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008491void InstructionCodeGeneratorARMVIXL::GenerateReferenceLoadTwoRegisters(
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008492 HInstruction* instruction,
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008493 Location out,
8494 Location obj,
8495 uint32_t offset,
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008496 Location maybe_temp,
8497 ReadBarrierOption read_barrier_option) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008498 vixl32::Register out_reg = RegisterFrom(out);
8499 vixl32::Register obj_reg = RegisterFrom(obj);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008500 if (read_barrier_option == kWithReadBarrier) {
8501 CHECK(kEmitCompilerReadBarrier);
8502 if (kUseBakerReadBarrier) {
8503 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
8504 // Load with fast path based Baker's read barrier.
8505 // /* HeapReference<Object> */ out = *(obj + offset)
8506 codegen_->GenerateFieldLoadWithBakerReadBarrier(
8507 instruction, out, obj_reg, offset, maybe_temp, /* needs_null_check */ false);
8508 } else {
8509 // Load with slow path based read barrier.
8510 // /* HeapReference<Object> */ out = *(obj + offset)
8511 GetAssembler()->LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
8512 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
8513 }
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008514 } else {
8515 // Plain load with no read barrier.
8516 // /* HeapReference<Object> */ out = *(obj + offset)
8517 GetAssembler()->LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
8518 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
8519 }
8520}
8521
Scott Wakelinga7812ae2016-10-17 10:03:36 +01008522void InstructionCodeGeneratorARMVIXL::GenerateGcRootFieldLoad(
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008523 HInstruction* instruction,
Scott Wakelinga7812ae2016-10-17 10:03:36 +01008524 Location root,
8525 vixl32::Register obj,
8526 uint32_t offset,
Artem Serovd4cc5b22016-11-04 11:19:09 +00008527 ReadBarrierOption read_barrier_option) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01008528 vixl32::Register root_reg = RegisterFrom(root);
Artem Serovd4cc5b22016-11-04 11:19:09 +00008529 if (read_barrier_option == kWithReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008530 DCHECK(kEmitCompilerReadBarrier);
8531 if (kUseBakerReadBarrier) {
8532 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
Roland Levillainba650a42017-03-06 13:52:32 +00008533 // Baker's read barrier are used.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008534 if (kBakerReadBarrierLinkTimeThunksEnableForGcRoots &&
8535 !Runtime::Current()->UseJitCompilation()) {
8536 // Note that we do not actually check the value of `GetIsGcMarking()`
8537 // to decide whether to mark the loaded GC root or not. Instead, we
8538 // load into `temp` (actually kBakerCcEntrypointRegister) the read
8539 // barrier mark introspection entrypoint. If `temp` is null, it means
8540 // that `GetIsGcMarking()` is false, and vice versa.
8541 //
8542 // We use link-time generated thunks for the slow path. That thunk
8543 // checks the reference and jumps to the entrypoint if needed.
8544 //
8545 // temp = Thread::Current()->pReadBarrierMarkIntrospection
8546 // lr = &return_address;
8547 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
8548 // if (temp != nullptr) {
8549 // goto gc_root_thunk<root_reg>(lr)
8550 // }
8551 // return_address:
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008552
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008553 UseScratchRegisterScope temps(GetVIXLAssembler());
8554 ExcludeIPAndBakerCcEntrypointRegister(&temps, instruction);
Vladimir Marko88abba22017-05-03 17:09:25 +01008555 bool narrow = CanEmitNarrowLdr(root_reg, obj, offset);
8556 uint32_t custom_data = linker::Thumb2RelativePatcher::EncodeBakerReadBarrierGcRootData(
8557 root_reg.GetCode(), narrow);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008558 vixl32::Label* bne_label = codegen_->NewBakerReadBarrierPatch(custom_data);
Roland Levillainba650a42017-03-06 13:52:32 +00008559
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008560 // entrypoint_reg =
8561 // Thread::Current()->pReadBarrierMarkReg12, i.e. pReadBarrierMarkIntrospection.
8562 DCHECK_EQ(ip.GetCode(), 12u);
8563 const int32_t entry_point_offset =
8564 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmPointerSize>(ip.GetCode());
8565 __ Ldr(kBakerCcEntrypointRegister, MemOperand(tr, entry_point_offset));
Roland Levillainba650a42017-03-06 13:52:32 +00008566
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008567 vixl::EmissionCheckScope guard(GetVIXLAssembler(),
8568 4 * vixl32::kMaxInstructionSizeInBytes);
8569 vixl32::Label return_address;
8570 EmitAdrCode adr(GetVIXLAssembler(), lr, &return_address);
8571 __ cmp(kBakerCcEntrypointRegister, Operand(0));
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008572 // Currently the offset is always within range. If that changes,
8573 // we shall have to split the load the same way as for fields.
8574 DCHECK_LT(offset, kReferenceLoadMinFarOffset);
Vladimir Marko88abba22017-05-03 17:09:25 +01008575 ptrdiff_t old_offset = GetVIXLAssembler()->GetBuffer()->GetCursorOffset();
8576 __ ldr(EncodingSize(narrow ? Narrow : Wide), root_reg, MemOperand(obj, offset));
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008577 EmitPlaceholderBne(codegen_, bne_label);
8578 __ Bind(&return_address);
Vladimir Marko88abba22017-05-03 17:09:25 +01008579 DCHECK_EQ(old_offset - GetVIXLAssembler()->GetBuffer()->GetCursorOffset(),
8580 narrow ? BAKER_MARK_INTROSPECTION_GC_ROOT_LDR_NARROW_OFFSET
8581 : BAKER_MARK_INTROSPECTION_GC_ROOT_LDR_WIDE_OFFSET);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008582 } else {
8583 // Note that we do not actually check the value of
8584 // `GetIsGcMarking()` to decide whether to mark the loaded GC
8585 // root or not. Instead, we load into `temp` the read barrier
8586 // mark entry point corresponding to register `root`. If `temp`
8587 // is null, it means that `GetIsGcMarking()` is false, and vice
8588 // versa.
8589 //
8590 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
8591 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
8592 // if (temp != nullptr) { // <=> Thread::Current()->GetIsGcMarking()
8593 // // Slow path.
8594 // root = temp(root); // root = ReadBarrier::Mark(root); // Runtime entry point call.
8595 // }
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008596
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008597 // Slow path marking the GC root `root`. The entrypoint will already be loaded in `temp`.
8598 Location temp = LocationFrom(lr);
8599 SlowPathCodeARMVIXL* slow_path =
8600 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARMVIXL(
8601 instruction, root, /* entrypoint */ temp);
8602 codegen_->AddSlowPath(slow_path);
8603
8604 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
8605 const int32_t entry_point_offset =
8606 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmPointerSize>(root.reg());
8607 // Loading the entrypoint does not require a load acquire since it is only changed when
8608 // threads are suspended or running a checkpoint.
8609 GetAssembler()->LoadFromOffset(kLoadWord, RegisterFrom(temp), tr, entry_point_offset);
8610
8611 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
8612 GetAssembler()->LoadFromOffset(kLoadWord, root_reg, obj, offset);
8613 static_assert(
8614 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
8615 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
8616 "have different sizes.");
8617 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
8618 "art::mirror::CompressedReference<mirror::Object> and int32_t "
8619 "have different sizes.");
8620
8621 // The entrypoint is null when the GC is not marking, this prevents one load compared to
8622 // checking GetIsGcMarking.
8623 __ CompareAndBranchIfNonZero(RegisterFrom(temp), slow_path->GetEntryLabel());
8624 __ Bind(slow_path->GetExitLabel());
8625 }
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008626 } else {
8627 // GC root loaded through a slow path for read barriers other
8628 // than Baker's.
8629 // /* GcRoot<mirror::Object>* */ root = obj + offset
8630 __ Add(root_reg, obj, offset);
8631 // /* mirror::Object* */ root = root->Read()
8632 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
8633 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01008634 } else {
8635 // Plain GC root load with no read barrier.
8636 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
8637 GetAssembler()->LoadFromOffset(kLoadWord, root_reg, obj, offset);
8638 // Note that GC roots are not affected by heap poisoning, thus we
8639 // do not have to unpoison `root_reg` here.
8640 }
8641}
8642
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008643void CodeGeneratorARMVIXL::MaybeAddBakerCcEntrypointTempForFields(LocationSummary* locations) {
8644 DCHECK(kEmitCompilerReadBarrier);
8645 DCHECK(kUseBakerReadBarrier);
8646 if (kBakerReadBarrierLinkTimeThunksEnableForFields) {
8647 if (!Runtime::Current()->UseJitCompilation()) {
8648 locations->AddTemp(Location::RegisterLocation(kBakerCcEntrypointRegister.GetCode()));
8649 }
8650 }
8651}
8652
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008653void CodeGeneratorARMVIXL::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
8654 Location ref,
8655 vixl32::Register obj,
8656 uint32_t offset,
8657 Location temp,
8658 bool needs_null_check) {
8659 DCHECK(kEmitCompilerReadBarrier);
8660 DCHECK(kUseBakerReadBarrier);
8661
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008662 if (kBakerReadBarrierLinkTimeThunksEnableForFields &&
8663 !Runtime::Current()->UseJitCompilation()) {
8664 // Note that we do not actually check the value of `GetIsGcMarking()`
8665 // to decide whether to mark the loaded reference or not. Instead, we
8666 // load into `temp` (actually kBakerCcEntrypointRegister) the read
8667 // barrier mark introspection entrypoint. If `temp` is null, it means
8668 // that `GetIsGcMarking()` is false, and vice versa.
8669 //
8670 // We use link-time generated thunks for the slow path. That thunk checks
8671 // the holder and jumps to the entrypoint if needed. If the holder is not
8672 // gray, it creates a fake dependency and returns to the LDR instruction.
8673 //
8674 // temp = Thread::Current()->pReadBarrierMarkIntrospection
8675 // lr = &gray_return_address;
8676 // if (temp != nullptr) {
8677 // goto field_thunk<holder_reg, base_reg>(lr)
8678 // }
8679 // not_gray_return_address:
8680 // // Original reference load. If the offset is too large to fit
8681 // // into LDR, we use an adjusted base register here.
Vladimir Marko88abba22017-05-03 17:09:25 +01008682 // HeapReference<mirror::Object> reference = *(obj+offset);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008683 // gray_return_address:
8684
8685 DCHECK_ALIGNED(offset, sizeof(mirror::HeapReference<mirror::Object>));
Vladimir Marko88abba22017-05-03 17:09:25 +01008686 vixl32::Register ref_reg = RegisterFrom(ref, Primitive::kPrimNot);
8687 bool narrow = CanEmitNarrowLdr(ref_reg, obj, offset);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008688 vixl32::Register base = obj;
8689 if (offset >= kReferenceLoadMinFarOffset) {
8690 base = RegisterFrom(temp);
8691 DCHECK(!base.Is(kBakerCcEntrypointRegister));
8692 static_assert(IsPowerOfTwo(kReferenceLoadMinFarOffset), "Expecting a power of 2.");
8693 __ Add(base, obj, Operand(offset & ~(kReferenceLoadMinFarOffset - 1u)));
8694 offset &= (kReferenceLoadMinFarOffset - 1u);
Vladimir Marko88abba22017-05-03 17:09:25 +01008695 // Use narrow LDR only for small offsets. Generating narrow encoding LDR for the large
8696 // offsets with `(offset & (kReferenceLoadMinFarOffset - 1u)) < 32u` would most likely
8697 // increase the overall code size when taking the generated thunks into account.
8698 DCHECK(!narrow);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008699 }
8700 UseScratchRegisterScope temps(GetVIXLAssembler());
8701 ExcludeIPAndBakerCcEntrypointRegister(&temps, instruction);
8702 uint32_t custom_data = linker::Thumb2RelativePatcher::EncodeBakerReadBarrierFieldData(
Vladimir Marko88abba22017-05-03 17:09:25 +01008703 base.GetCode(), obj.GetCode(), narrow);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008704 vixl32::Label* bne_label = NewBakerReadBarrierPatch(custom_data);
8705
8706 // entrypoint_reg =
8707 // Thread::Current()->pReadBarrierMarkReg12, i.e. pReadBarrierMarkIntrospection.
8708 DCHECK_EQ(ip.GetCode(), 12u);
8709 const int32_t entry_point_offset =
8710 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmPointerSize>(ip.GetCode());
8711 __ Ldr(kBakerCcEntrypointRegister, MemOperand(tr, entry_point_offset));
8712
8713 vixl::EmissionCheckScope guard(
8714 GetVIXLAssembler(),
8715 (kPoisonHeapReferences ? 5u : 4u) * vixl32::kMaxInstructionSizeInBytes);
8716 vixl32::Label return_address;
8717 EmitAdrCode adr(GetVIXLAssembler(), lr, &return_address);
8718 __ cmp(kBakerCcEntrypointRegister, Operand(0));
8719 EmitPlaceholderBne(this, bne_label);
Vladimir Marko88abba22017-05-03 17:09:25 +01008720 ptrdiff_t old_offset = GetVIXLAssembler()->GetBuffer()->GetCursorOffset();
8721 __ ldr(EncodingSize(narrow ? Narrow : Wide), ref_reg, MemOperand(base, offset));
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008722 if (needs_null_check) {
8723 MaybeRecordImplicitNullCheck(instruction);
8724 }
Vladimir Marko88abba22017-05-03 17:09:25 +01008725 // Note: We need a specific width for the unpoisoning NEG.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008726 if (kPoisonHeapReferences) {
Vladimir Marko88abba22017-05-03 17:09:25 +01008727 if (narrow) {
8728 // The only 16-bit encoding is T1 which sets flags outside IT block (i.e. RSBS, not RSB).
8729 __ rsbs(EncodingSize(Narrow), ref_reg, ref_reg, Operand(0));
8730 } else {
8731 __ rsb(EncodingSize(Wide), ref_reg, ref_reg, Operand(0));
8732 }
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008733 }
8734 __ Bind(&return_address);
Vladimir Marko88abba22017-05-03 17:09:25 +01008735 DCHECK_EQ(old_offset - GetVIXLAssembler()->GetBuffer()->GetCursorOffset(),
8736 narrow ? BAKER_MARK_INTROSPECTION_FIELD_LDR_NARROW_OFFSET
8737 : BAKER_MARK_INTROSPECTION_FIELD_LDR_WIDE_OFFSET);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008738 return;
8739 }
8740
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008741 // /* HeapReference<Object> */ ref = *(obj + offset)
8742 Location no_index = Location::NoLocation();
8743 ScaleFactor no_scale_factor = TIMES_1;
8744 GenerateReferenceLoadWithBakerReadBarrier(
8745 instruction, ref, obj, offset, no_index, no_scale_factor, temp, needs_null_check);
Roland Levillain6070e882016-11-03 17:51:58 +00008746}
8747
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008748void CodeGeneratorARMVIXL::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
8749 Location ref,
8750 vixl32::Register obj,
8751 uint32_t data_offset,
8752 Location index,
8753 Location temp,
8754 bool needs_null_check) {
8755 DCHECK(kEmitCompilerReadBarrier);
8756 DCHECK(kUseBakerReadBarrier);
8757
8758 static_assert(
8759 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
8760 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008761 ScaleFactor scale_factor = TIMES_4;
8762
8763 if (kBakerReadBarrierLinkTimeThunksEnableForArrays &&
8764 !Runtime::Current()->UseJitCompilation()) {
8765 // Note that we do not actually check the value of `GetIsGcMarking()`
8766 // to decide whether to mark the loaded reference or not. Instead, we
8767 // load into `temp` (actually kBakerCcEntrypointRegister) the read
8768 // barrier mark introspection entrypoint. If `temp` is null, it means
8769 // that `GetIsGcMarking()` is false, and vice versa.
8770 //
8771 // We use link-time generated thunks for the slow path. That thunk checks
8772 // the holder and jumps to the entrypoint if needed. If the holder is not
8773 // gray, it creates a fake dependency and returns to the LDR instruction.
8774 //
8775 // temp = Thread::Current()->pReadBarrierMarkIntrospection
8776 // lr = &gray_return_address;
8777 // if (temp != nullptr) {
8778 // goto field_thunk<holder_reg, base_reg>(lr)
8779 // }
8780 // not_gray_return_address:
8781 // // Original reference load. If the offset is too large to fit
8782 // // into LDR, we use an adjusted base register here.
Vladimir Marko88abba22017-05-03 17:09:25 +01008783 // HeapReference<mirror::Object> reference = data[index];
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008784 // gray_return_address:
8785
8786 DCHECK(index.IsValid());
8787 vixl32::Register index_reg = RegisterFrom(index, Primitive::kPrimInt);
8788 vixl32::Register ref_reg = RegisterFrom(ref, Primitive::kPrimNot);
8789 vixl32::Register data_reg = RegisterFrom(temp, Primitive::kPrimInt); // Raw pointer.
8790 DCHECK(!data_reg.Is(kBakerCcEntrypointRegister));
8791
8792 UseScratchRegisterScope temps(GetVIXLAssembler());
8793 ExcludeIPAndBakerCcEntrypointRegister(&temps, instruction);
8794 uint32_t custom_data =
8795 linker::Thumb2RelativePatcher::EncodeBakerReadBarrierArrayData(data_reg.GetCode());
8796 vixl32::Label* bne_label = NewBakerReadBarrierPatch(custom_data);
8797
8798 // entrypoint_reg =
8799 // Thread::Current()->pReadBarrierMarkReg16, i.e. pReadBarrierMarkIntrospection.
8800 DCHECK_EQ(ip.GetCode(), 12u);
8801 const int32_t entry_point_offset =
8802 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmPointerSize>(ip.GetCode());
8803 __ Ldr(kBakerCcEntrypointRegister, MemOperand(tr, entry_point_offset));
8804 __ Add(data_reg, obj, Operand(data_offset));
8805
8806 vixl::EmissionCheckScope guard(
8807 GetVIXLAssembler(),
8808 (kPoisonHeapReferences ? 5u : 4u) * vixl32::kMaxInstructionSizeInBytes);
8809 vixl32::Label return_address;
8810 EmitAdrCode adr(GetVIXLAssembler(), lr, &return_address);
8811 __ cmp(kBakerCcEntrypointRegister, Operand(0));
8812 EmitPlaceholderBne(this, bne_label);
Vladimir Marko88abba22017-05-03 17:09:25 +01008813 ptrdiff_t old_offset = GetVIXLAssembler()->GetBuffer()->GetCursorOffset();
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008814 __ ldr(ref_reg, MemOperand(data_reg, index_reg, vixl32::LSL, scale_factor));
8815 DCHECK(!needs_null_check); // The thunk cannot handle the null check.
8816 // Note: We need a Wide NEG for the unpoisoning.
8817 if (kPoisonHeapReferences) {
8818 __ rsb(EncodingSize(Wide), ref_reg, ref_reg, Operand(0));
8819 }
8820 __ Bind(&return_address);
Vladimir Marko88abba22017-05-03 17:09:25 +01008821 DCHECK_EQ(old_offset - GetVIXLAssembler()->GetBuffer()->GetCursorOffset(),
8822 BAKER_MARK_INTROSPECTION_ARRAY_LDR_OFFSET);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008823 return;
8824 }
8825
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008826 // /* HeapReference<Object> */ ref =
8827 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008828 GenerateReferenceLoadWithBakerReadBarrier(
8829 instruction, ref, obj, data_offset, index, scale_factor, temp, needs_null_check);
Roland Levillain6070e882016-11-03 17:51:58 +00008830}
8831
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008832void CodeGeneratorARMVIXL::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
8833 Location ref,
8834 vixl32::Register obj,
8835 uint32_t offset,
8836 Location index,
8837 ScaleFactor scale_factor,
8838 Location temp,
Roland Levillainff487002017-03-07 16:50:01 +00008839 bool needs_null_check) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008840 DCHECK(kEmitCompilerReadBarrier);
8841 DCHECK(kUseBakerReadBarrier);
8842
Roland Levillain54f869e2017-03-06 13:54:11 +00008843 // Query `art::Thread::Current()->GetIsGcMarking()` to decide
8844 // whether we need to enter the slow path to mark the reference.
8845 // Then, in the slow path, check the gray bit in the lock word of
8846 // the reference's holder (`obj`) to decide whether to mark `ref` or
8847 // not.
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008848 //
Roland Levillainba650a42017-03-06 13:52:32 +00008849 // Note that we do not actually check the value of `GetIsGcMarking()`;
Roland Levillainff487002017-03-07 16:50:01 +00008850 // instead, we load into `temp2` the read barrier mark entry point
8851 // corresponding to register `ref`. If `temp2` is null, it means
8852 // that `GetIsGcMarking()` is false, and vice versa.
8853 //
8854 // temp2 = Thread::Current()->pReadBarrierMarkReg ## root.reg()
8855 // if (temp2 != nullptr) { // <=> Thread::Current()->GetIsGcMarking()
8856 // // Slow path.
8857 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
8858 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
8859 // HeapReference<mirror::Object> ref = *src; // Original reference load.
8860 // bool is_gray = (rb_state == ReadBarrier::GrayState());
8861 // if (is_gray) {
8862 // ref = temp2(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
8863 // }
8864 // } else {
8865 // HeapReference<mirror::Object> ref = *src; // Original reference load.
8866 // }
8867
8868 vixl32::Register temp_reg = RegisterFrom(temp);
8869
8870 // Slow path marking the object `ref` when the GC is marking. The
8871 // entrypoint will already be loaded in `temp2`.
8872 Location temp2 = LocationFrom(lr);
8873 SlowPathCodeARMVIXL* slow_path =
8874 new (GetGraph()->GetArena()) LoadReferenceWithBakerReadBarrierSlowPathARMVIXL(
8875 instruction,
8876 ref,
8877 obj,
8878 offset,
8879 index,
8880 scale_factor,
8881 needs_null_check,
8882 temp_reg,
8883 /* entrypoint */ temp2);
8884 AddSlowPath(slow_path);
8885
8886 // temp2 = Thread::Current()->pReadBarrierMarkReg ## ref.reg()
8887 const int32_t entry_point_offset =
8888 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmPointerSize>(ref.reg());
8889 // Loading the entrypoint does not require a load acquire since it is only changed when
8890 // threads are suspended or running a checkpoint.
8891 GetAssembler()->LoadFromOffset(kLoadWord, RegisterFrom(temp2), tr, entry_point_offset);
8892 // The entrypoint is null when the GC is not marking, this prevents one load compared to
8893 // checking GetIsGcMarking.
8894 __ CompareAndBranchIfNonZero(RegisterFrom(temp2), slow_path->GetEntryLabel());
8895 // Fast path: the GC is not marking: just load the reference.
8896 GenerateRawReferenceLoad(instruction, ref, obj, offset, index, scale_factor, needs_null_check);
8897 __ Bind(slow_path->GetExitLabel());
8898}
8899
8900void CodeGeneratorARMVIXL::UpdateReferenceFieldWithBakerReadBarrier(HInstruction* instruction,
8901 Location ref,
8902 vixl32::Register obj,
8903 Location field_offset,
8904 Location temp,
8905 bool needs_null_check,
8906 vixl32::Register temp2) {
8907 DCHECK(kEmitCompilerReadBarrier);
8908 DCHECK(kUseBakerReadBarrier);
8909
8910 // Query `art::Thread::Current()->GetIsGcMarking()` to decide
8911 // whether we need to enter the slow path to update the reference
8912 // field within `obj`. Then, in the slow path, check the gray bit
8913 // in the lock word of the reference's holder (`obj`) to decide
8914 // whether to mark `ref` and update the field or not.
8915 //
8916 // Note that we do not actually check the value of `GetIsGcMarking()`;
Roland Levillainba650a42017-03-06 13:52:32 +00008917 // instead, we load into `temp3` the read barrier mark entry point
8918 // corresponding to register `ref`. If `temp3` is null, it means
8919 // that `GetIsGcMarking()` is false, and vice versa.
8920 //
8921 // temp3 = Thread::Current()->pReadBarrierMarkReg ## root.reg()
Roland Levillainba650a42017-03-06 13:52:32 +00008922 // if (temp3 != nullptr) { // <=> Thread::Current()->GetIsGcMarking()
8923 // // Slow path.
Roland Levillain54f869e2017-03-06 13:54:11 +00008924 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
8925 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
Roland Levillainff487002017-03-07 16:50:01 +00008926 // HeapReference<mirror::Object> ref = *(obj + field_offset); // Reference load.
Roland Levillain54f869e2017-03-06 13:54:11 +00008927 // bool is_gray = (rb_state == ReadBarrier::GrayState());
8928 // if (is_gray) {
Roland Levillainff487002017-03-07 16:50:01 +00008929 // old_ref = ref;
Roland Levillain54f869e2017-03-06 13:54:11 +00008930 // ref = temp3(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
Roland Levillainff487002017-03-07 16:50:01 +00008931 // compareAndSwapObject(obj, field_offset, old_ref, ref);
Roland Levillain54f869e2017-03-06 13:54:11 +00008932 // }
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008933 // }
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008934
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008935 vixl32::Register temp_reg = RegisterFrom(temp);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008936
Roland Levillainff487002017-03-07 16:50:01 +00008937 // Slow path updating the object reference at address `obj + field_offset`
8938 // when the GC is marking. The entrypoint will already be loaded in `temp3`.
Roland Levillainba650a42017-03-06 13:52:32 +00008939 Location temp3 = LocationFrom(lr);
Roland Levillainff487002017-03-07 16:50:01 +00008940 SlowPathCodeARMVIXL* slow_path =
8941 new (GetGraph()->GetArena()) LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARMVIXL(
8942 instruction,
8943 ref,
8944 obj,
8945 /* offset */ 0u,
8946 /* index */ field_offset,
8947 /* scale_factor */ ScaleFactor::TIMES_1,
8948 needs_null_check,
8949 temp_reg,
8950 temp2,
8951 /* entrypoint */ temp3);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008952 AddSlowPath(slow_path);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008953
Roland Levillainba650a42017-03-06 13:52:32 +00008954 // temp3 = Thread::Current()->pReadBarrierMarkReg ## ref.reg()
8955 const int32_t entry_point_offset =
8956 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmPointerSize>(ref.reg());
8957 // Loading the entrypoint does not require a load acquire since it is only changed when
8958 // threads are suspended or running a checkpoint.
8959 GetAssembler()->LoadFromOffset(kLoadWord, RegisterFrom(temp3), tr, entry_point_offset);
Roland Levillainba650a42017-03-06 13:52:32 +00008960 // The entrypoint is null when the GC is not marking, this prevents one load compared to
8961 // checking GetIsGcMarking.
8962 __ CompareAndBranchIfNonZero(RegisterFrom(temp3), slow_path->GetEntryLabel());
Roland Levillainff487002017-03-07 16:50:01 +00008963 // Fast path: the GC is not marking: nothing to do (the field is
8964 // up-to-date, and we don't need to load the reference).
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008965 __ Bind(slow_path->GetExitLabel());
Roland Levillain844e6532016-11-03 16:09:47 +00008966}
Scott Wakelingfe885462016-09-22 10:24:38 +01008967
Roland Levillainba650a42017-03-06 13:52:32 +00008968void CodeGeneratorARMVIXL::GenerateRawReferenceLoad(HInstruction* instruction,
8969 Location ref,
8970 vixl::aarch32::Register obj,
8971 uint32_t offset,
8972 Location index,
8973 ScaleFactor scale_factor,
8974 bool needs_null_check) {
8975 Primitive::Type type = Primitive::kPrimNot;
8976 vixl32::Register ref_reg = RegisterFrom(ref, type);
8977
8978 // If needed, vixl::EmissionCheckScope guards are used to ensure
8979 // that no pools are emitted between the load (macro) instruction
8980 // and MaybeRecordImplicitNullCheck.
8981
Scott Wakelingfe885462016-09-22 10:24:38 +01008982 if (index.IsValid()) {
8983 // Load types involving an "index": ArrayGet,
8984 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
8985 // intrinsics.
Roland Levillainba650a42017-03-06 13:52:32 +00008986 // /* HeapReference<mirror::Object> */ ref = *(obj + offset + (index << scale_factor))
Scott Wakelingfe885462016-09-22 10:24:38 +01008987 if (index.IsConstant()) {
8988 size_t computed_offset =
8989 (Int32ConstantFrom(index) << scale_factor) + offset;
Roland Levillainba650a42017-03-06 13:52:32 +00008990 vixl::EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Scott Wakelingfe885462016-09-22 10:24:38 +01008991 GetAssembler()->LoadFromOffset(kLoadWord, ref_reg, obj, computed_offset);
Roland Levillainba650a42017-03-06 13:52:32 +00008992 if (needs_null_check) {
8993 MaybeRecordImplicitNullCheck(instruction);
8994 }
Scott Wakelingfe885462016-09-22 10:24:38 +01008995 } else {
8996 // Handle the special case of the
8997 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
8998 // intrinsics, which use a register pair as index ("long
8999 // offset"), of which only the low part contains data.
9000 vixl32::Register index_reg = index.IsRegisterPair()
9001 ? LowRegisterFrom(index)
9002 : RegisterFrom(index);
9003 UseScratchRegisterScope temps(GetVIXLAssembler());
Roland Levillainba650a42017-03-06 13:52:32 +00009004 vixl32::Register temp = temps.Acquire();
9005 __ Add(temp, obj, Operand(index_reg, ShiftType::LSL, scale_factor));
9006 {
9007 vixl::EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
9008 GetAssembler()->LoadFromOffset(kLoadWord, ref_reg, temp, offset);
9009 if (needs_null_check) {
9010 MaybeRecordImplicitNullCheck(instruction);
9011 }
9012 }
Scott Wakelingfe885462016-09-22 10:24:38 +01009013 }
9014 } else {
Roland Levillainba650a42017-03-06 13:52:32 +00009015 // /* HeapReference<mirror::Object> */ ref = *(obj + offset)
9016 vixl::EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Scott Wakelingfe885462016-09-22 10:24:38 +01009017 GetAssembler()->LoadFromOffset(kLoadWord, ref_reg, obj, offset);
Roland Levillainba650a42017-03-06 13:52:32 +00009018 if (needs_null_check) {
9019 MaybeRecordImplicitNullCheck(instruction);
9020 }
Scott Wakelingfe885462016-09-22 10:24:38 +01009021 }
9022
Roland Levillain844e6532016-11-03 16:09:47 +00009023 // Object* ref = ref_addr->AsMirrorPtr()
9024 GetAssembler()->MaybeUnpoisonHeapReference(ref_reg);
Roland Levillain844e6532016-11-03 16:09:47 +00009025}
9026
Anton Kirilovedb2ac32016-11-30 15:14:10 +00009027void CodeGeneratorARMVIXL::GenerateReadBarrierSlow(HInstruction* instruction,
9028 Location out,
9029 Location ref,
9030 Location obj,
9031 uint32_t offset,
9032 Location index) {
9033 DCHECK(kEmitCompilerReadBarrier);
9034
9035 // Insert a slow path based read barrier *after* the reference load.
9036 //
9037 // If heap poisoning is enabled, the unpoisoning of the loaded
9038 // reference will be carried out by the runtime within the slow
9039 // path.
9040 //
9041 // Note that `ref` currently does not get unpoisoned (when heap
9042 // poisoning is enabled), which is alright as the `ref` argument is
9043 // not used by the artReadBarrierSlow entry point.
9044 //
9045 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
9046 SlowPathCodeARMVIXL* slow_path = new (GetGraph()->GetArena())
9047 ReadBarrierForHeapReferenceSlowPathARMVIXL(instruction, out, ref, obj, offset, index);
9048 AddSlowPath(slow_path);
9049
9050 __ B(slow_path->GetEntryLabel());
9051 __ Bind(slow_path->GetExitLabel());
9052}
9053
9054void CodeGeneratorARMVIXL::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
Artem Serov02d37832016-10-25 15:25:33 +01009055 Location out,
Anton Kirilovedb2ac32016-11-30 15:14:10 +00009056 Location ref,
9057 Location obj,
9058 uint32_t offset,
9059 Location index) {
Artem Serov02d37832016-10-25 15:25:33 +01009060 if (kEmitCompilerReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00009061 // Baker's read barriers shall be handled by the fast path
9062 // (CodeGeneratorARM::GenerateReferenceLoadWithBakerReadBarrier).
Artem Serov02d37832016-10-25 15:25:33 +01009063 DCHECK(!kUseBakerReadBarrier);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00009064 // If heap poisoning is enabled, unpoisoning will be taken care of
9065 // by the runtime within the slow path.
9066 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Artem Serov02d37832016-10-25 15:25:33 +01009067 } else if (kPoisonHeapReferences) {
9068 GetAssembler()->UnpoisonHeapReference(RegisterFrom(out));
9069 }
9070}
9071
Anton Kirilovedb2ac32016-11-30 15:14:10 +00009072void CodeGeneratorARMVIXL::GenerateReadBarrierForRootSlow(HInstruction* instruction,
9073 Location out,
9074 Location root) {
9075 DCHECK(kEmitCompilerReadBarrier);
9076
9077 // Insert a slow path based read barrier *after* the GC root load.
9078 //
9079 // Note that GC roots are not affected by heap poisoning, so we do
9080 // not need to do anything special for this here.
9081 SlowPathCodeARMVIXL* slow_path =
9082 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathARMVIXL(instruction, out, root);
9083 AddSlowPath(slow_path);
9084
9085 __ B(slow_path->GetEntryLabel());
9086 __ Bind(slow_path->GetExitLabel());
9087}
9088
Artem Serov02d37832016-10-25 15:25:33 +01009089// Check if the desired_dispatch_info is supported. If it is, return it,
9090// otherwise return a fall-back info that should be used instead.
9091HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARMVIXL::GetSupportedInvokeStaticOrDirectDispatch(
Artem Serovd4cc5b22016-11-04 11:19:09 +00009092 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +00009093 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Nicolas Geoffraye807ff72017-01-23 09:03:12 +00009094 return desired_dispatch_info;
Artem Serov02d37832016-10-25 15:25:33 +01009095}
9096
Scott Wakelingfe885462016-09-22 10:24:38 +01009097vixl32::Register CodeGeneratorARMVIXL::GetInvokeStaticOrDirectExtraParameter(
9098 HInvokeStaticOrDirect* invoke, vixl32::Register temp) {
9099 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
9100 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
9101 if (!invoke->GetLocations()->Intrinsified()) {
9102 return RegisterFrom(location);
9103 }
9104 // For intrinsics we allow any location, so it may be on the stack.
9105 if (!location.IsRegister()) {
9106 GetAssembler()->LoadFromOffset(kLoadWord, temp, sp, location.GetStackIndex());
9107 return temp;
9108 }
9109 // For register locations, check if the register was saved. If so, get it from the stack.
9110 // Note: There is a chance that the register was saved but not overwritten, so we could
9111 // save one load. However, since this is just an intrinsic slow path we prefer this
9112 // simple and more robust approach rather that trying to determine if that's the case.
9113 SlowPathCode* slow_path = GetCurrentSlowPath();
Scott Wakelingd5cd4972017-02-03 11:38:35 +00009114 if (slow_path != nullptr && slow_path->IsCoreRegisterSaved(RegisterFrom(location).GetCode())) {
Scott Wakelingfe885462016-09-22 10:24:38 +01009115 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(RegisterFrom(location).GetCode());
9116 GetAssembler()->LoadFromOffset(kLoadWord, temp, sp, stack_offset);
9117 return temp;
9118 }
9119 return RegisterFrom(location);
9120}
9121
Vladimir Markod254f5c2017-06-02 15:18:36 +00009122void CodeGeneratorARMVIXL::GenerateStaticOrDirectCall(
Scott Wakelingfe885462016-09-22 10:24:38 +01009123 HInvokeStaticOrDirect* invoke, Location temp) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00009124 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
Scott Wakelingfe885462016-09-22 10:24:38 +01009125 switch (invoke->GetMethodLoadKind()) {
9126 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
9127 uint32_t offset =
9128 GetThreadOffset<kArmPointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
9129 // temp = thread->string_init_entrypoint
Artem Serovd4cc5b22016-11-04 11:19:09 +00009130 GetAssembler()->LoadFromOffset(kLoadWord, RegisterFrom(temp), tr, offset);
9131 break;
9132 }
9133 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
9134 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
9135 break;
Vladimir Marko65979462017-05-19 17:25:12 +01009136 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative: {
9137 DCHECK(GetCompilerOptions().IsBootImage());
9138 PcRelativePatchInfo* labels = NewPcRelativeMethodPatch(invoke->GetTargetMethod());
9139 vixl32::Register temp_reg = RegisterFrom(temp);
9140 EmitMovwMovtPlaceholder(labels, temp_reg);
9141 break;
9142 }
Artem Serovd4cc5b22016-11-04 11:19:09 +00009143 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
9144 __ Mov(RegisterFrom(temp), Operand::From(invoke->GetMethodAddress()));
9145 break;
Artem Serovd4cc5b22016-11-04 11:19:09 +00009146 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
9147 HArmDexCacheArraysBase* base =
9148 invoke->InputAt(invoke->GetSpecialInputIndex())->AsArmDexCacheArraysBase();
9149 vixl32::Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke, RegisterFrom(temp));
9150 int32_t offset = invoke->GetDexCacheArrayOffset() - base->GetElementOffset();
9151 GetAssembler()->LoadFromOffset(kLoadWord, RegisterFrom(temp), base_reg, offset);
Scott Wakelingfe885462016-09-22 10:24:38 +01009152 break;
9153 }
9154 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
9155 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
9156 vixl32::Register method_reg;
Artem Serovd4cc5b22016-11-04 11:19:09 +00009157 vixl32::Register reg = RegisterFrom(temp);
Scott Wakelingfe885462016-09-22 10:24:38 +01009158 if (current_method.IsRegister()) {
9159 method_reg = RegisterFrom(current_method);
9160 } else {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01009161 DCHECK(invoke->GetLocations()->Intrinsified());
9162 DCHECK(!current_method.IsValid());
Artem Serovd4cc5b22016-11-04 11:19:09 +00009163 method_reg = reg;
9164 GetAssembler()->LoadFromOffset(kLoadWord, reg, sp, kCurrentMethodStackOffset);
Scott Wakelingfe885462016-09-22 10:24:38 +01009165 }
9166 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
9167 GetAssembler()->LoadFromOffset(
9168 kLoadWord,
Artem Serovd4cc5b22016-11-04 11:19:09 +00009169 reg,
Scott Wakelingfe885462016-09-22 10:24:38 +01009170 method_reg,
9171 ArtMethod::DexCacheResolvedMethodsOffset(kArmPointerSize).Int32Value());
9172 // temp = temp[index_in_cache];
9173 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
9174 uint32_t index_in_cache = invoke->GetDexMethodIndex();
9175 GetAssembler()->LoadFromOffset(
Artem Serovd4cc5b22016-11-04 11:19:09 +00009176 kLoadWord, reg, reg, CodeGenerator::GetCachePointerOffset(index_in_cache));
Scott Wakelingfe885462016-09-22 10:24:38 +01009177 break;
9178 }
Scott Wakelingfe885462016-09-22 10:24:38 +01009179 }
9180
Artem Serovd4cc5b22016-11-04 11:19:09 +00009181 switch (invoke->GetCodePtrLocation()) {
9182 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
9183 __ Bl(GetFrameEntryLabel());
9184 break;
Artem Serovd4cc5b22016-11-04 11:19:09 +00009185 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
9186 // LR = callee_method->entry_point_from_quick_compiled_code_
9187 GetAssembler()->LoadFromOffset(
9188 kLoadWord,
9189 lr,
9190 RegisterFrom(callee_method),
9191 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize).Int32Value());
Alexandre Rames374ddf32016-11-04 10:40:49 +00009192 {
9193 // 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 +00009194 ExactAssemblyScope aas(GetVIXLAssembler(),
9195 vixl32::k16BitT32InstructionSizeInBytes,
9196 CodeBufferCheckScope::kExactSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00009197 // LR()
9198 __ blx(lr);
9199 }
Artem Serovd4cc5b22016-11-04 11:19:09 +00009200 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01009201 }
9202
Scott Wakelingfe885462016-09-22 10:24:38 +01009203 DCHECK(!IsLeafMethod());
9204}
9205
9206void CodeGeneratorARMVIXL::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
9207 vixl32::Register temp = RegisterFrom(temp_location);
9208 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
9209 invoke->GetVTableIndex(), kArmPointerSize).Uint32Value();
9210
9211 // Use the calling convention instead of the location of the receiver, as
9212 // intrinsics may have put the receiver in a different register. In the intrinsics
9213 // slow path, the arguments have been moved to the right place, so here we are
9214 // guaranteed that the receiver is the first register of the calling convention.
9215 InvokeDexCallingConventionARMVIXL calling_convention;
9216 vixl32::Register receiver = calling_convention.GetRegisterAt(0);
9217 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Alexandre Rames374ddf32016-11-04 10:40:49 +00009218 {
9219 // Make sure the pc is recorded immediately after the `ldr` instruction.
Artem Serov0fb37192016-12-06 18:13:40 +00009220 ExactAssemblyScope aas(GetVIXLAssembler(),
9221 vixl32::kMaxInstructionSizeInBytes,
9222 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00009223 // /* HeapReference<Class> */ temp = receiver->klass_
9224 __ ldr(temp, MemOperand(receiver, class_offset));
9225 MaybeRecordImplicitNullCheck(invoke);
9226 }
Scott Wakelingfe885462016-09-22 10:24:38 +01009227 // Instead of simply (possibly) unpoisoning `temp` here, we should
9228 // emit a read barrier for the previous class reference load.
9229 // However this is not required in practice, as this is an
9230 // intermediate/temporary reference and because the current
9231 // concurrent copying collector keeps the from-space memory
9232 // intact/accessible until the end of the marking phase (the
9233 // concurrent copying collector may not in the future).
9234 GetAssembler()->MaybeUnpoisonHeapReference(temp);
9235
9236 // temp = temp->GetMethodAt(method_offset);
9237 uint32_t entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(
9238 kArmPointerSize).Int32Value();
9239 GetAssembler()->LoadFromOffset(kLoadWord, temp, temp, method_offset);
9240 // LR = temp->GetEntryPoint();
9241 GetAssembler()->LoadFromOffset(kLoadWord, lr, temp, entry_point);
9242 // LR();
Alexandre Rames374ddf32016-11-04 10:40:49 +00009243 // This `blx` *must* be the *last* instruction generated by this stub, so that calls to
9244 // `RecordPcInfo()` immediately following record the correct pc. Use a scope to help guarantee
9245 // that.
9246 // 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 +00009247 ExactAssemblyScope aas(GetVIXLAssembler(),
9248 vixl32::k16BitT32InstructionSizeInBytes,
9249 CodeBufferCheckScope::kExactSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00009250 __ blx(lr);
Scott Wakelingfe885462016-09-22 10:24:38 +01009251}
9252
Vladimir Marko65979462017-05-19 17:25:12 +01009253CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewPcRelativeMethodPatch(
9254 MethodReference target_method) {
9255 return NewPcRelativePatch(*target_method.dex_file,
9256 target_method.dex_method_index,
9257 &pc_relative_method_patches_);
Artem Serovd4cc5b22016-11-04 11:19:09 +00009258}
9259
9260CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewPcRelativeTypePatch(
9261 const DexFile& dex_file, dex::TypeIndex type_index) {
9262 return NewPcRelativePatch(dex_file, type_index.index_, &pc_relative_type_patches_);
9263}
9264
Vladimir Marko1998cd02017-01-13 13:02:58 +00009265CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewTypeBssEntryPatch(
9266 const DexFile& dex_file, dex::TypeIndex type_index) {
9267 return NewPcRelativePatch(dex_file, type_index.index_, &type_bss_entry_patches_);
9268}
9269
Vladimir Marko65979462017-05-19 17:25:12 +01009270CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewPcRelativeStringPatch(
9271 const DexFile& dex_file, dex::StringIndex string_index) {
9272 return NewPcRelativePatch(dex_file, string_index.index_, &pc_relative_string_patches_);
9273}
9274
Artem Serovd4cc5b22016-11-04 11:19:09 +00009275CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewPcRelativeDexCacheArrayPatch(
9276 const DexFile& dex_file, uint32_t element_offset) {
9277 return NewPcRelativePatch(dex_file, element_offset, &pc_relative_dex_cache_patches_);
9278}
9279
9280CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewPcRelativePatch(
9281 const DexFile& dex_file, uint32_t offset_or_index, ArenaDeque<PcRelativePatchInfo>* patches) {
9282 patches->emplace_back(dex_file, offset_or_index);
9283 return &patches->back();
9284}
9285
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01009286vixl::aarch32::Label* CodeGeneratorARMVIXL::NewBakerReadBarrierPatch(uint32_t custom_data) {
9287 baker_read_barrier_patches_.emplace_back(custom_data);
9288 return &baker_read_barrier_patches_.back().label;
9289}
9290
Artem Serovc5fcb442016-12-02 19:19:58 +00009291VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateBootImageAddressLiteral(uint32_t address) {
Richard Uhlerc52f3032017-03-02 13:45:45 +00009292 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), &uint32_literals_);
Artem Serovc5fcb442016-12-02 19:19:58 +00009293}
9294
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00009295VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateJitStringLiteral(
9296 const DexFile& dex_file,
9297 dex::StringIndex string_index,
9298 Handle<mirror::String> handle) {
9299 jit_string_roots_.Overwrite(StringReference(&dex_file, string_index),
9300 reinterpret_cast64<uint64_t>(handle.GetReference()));
Artem Serovc5fcb442016-12-02 19:19:58 +00009301 return jit_string_patches_.GetOrCreate(
9302 StringReference(&dex_file, string_index),
9303 [this]() {
9304 return GetAssembler()->CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u);
9305 });
9306}
9307
9308VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateJitClassLiteral(const DexFile& dex_file,
9309 dex::TypeIndex type_index,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00009310 Handle<mirror::Class> handle) {
9311 jit_class_roots_.Overwrite(TypeReference(&dex_file, type_index),
9312 reinterpret_cast64<uint64_t>(handle.GetReference()));
Artem Serovc5fcb442016-12-02 19:19:58 +00009313 return jit_class_patches_.GetOrCreate(
9314 TypeReference(&dex_file, type_index),
9315 [this]() {
9316 return GetAssembler()->CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u);
9317 });
9318}
9319
Artem Serovd4cc5b22016-11-04 11:19:09 +00009320template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
9321inline void CodeGeneratorARMVIXL::EmitPcRelativeLinkerPatches(
9322 const ArenaDeque<PcRelativePatchInfo>& infos,
9323 ArenaVector<LinkerPatch>* linker_patches) {
9324 for (const PcRelativePatchInfo& info : infos) {
9325 const DexFile& dex_file = info.target_dex_file;
9326 size_t offset_or_index = info.offset_or_index;
9327 DCHECK(info.add_pc_label.IsBound());
9328 uint32_t add_pc_offset = dchecked_integral_cast<uint32_t>(info.add_pc_label.GetLocation());
9329 // Add MOVW patch.
9330 DCHECK(info.movw_label.IsBound());
9331 uint32_t movw_offset = dchecked_integral_cast<uint32_t>(info.movw_label.GetLocation());
9332 linker_patches->push_back(Factory(movw_offset, &dex_file, add_pc_offset, offset_or_index));
9333 // Add MOVT patch.
9334 DCHECK(info.movt_label.IsBound());
9335 uint32_t movt_offset = dchecked_integral_cast<uint32_t>(info.movt_label.GetLocation());
9336 linker_patches->push_back(Factory(movt_offset, &dex_file, add_pc_offset, offset_or_index));
9337 }
9338}
9339
9340void CodeGeneratorARMVIXL::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
9341 DCHECK(linker_patches->empty());
9342 size_t size =
Artem Serovd4cc5b22016-11-04 11:19:09 +00009343 /* MOVW+MOVT for each entry */ 2u * pc_relative_dex_cache_patches_.size() +
Vladimir Marko65979462017-05-19 17:25:12 +01009344 /* MOVW+MOVT for each entry */ 2u * pc_relative_method_patches_.size() +
Artem Serovc5fcb442016-12-02 19:19:58 +00009345 /* MOVW+MOVT for each entry */ 2u * pc_relative_type_patches_.size() +
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01009346 /* MOVW+MOVT for each entry */ 2u * type_bss_entry_patches_.size() +
Vladimir Marko65979462017-05-19 17:25:12 +01009347 /* MOVW+MOVT for each entry */ 2u * pc_relative_string_patches_.size() +
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01009348 baker_read_barrier_patches_.size();
Artem Serovd4cc5b22016-11-04 11:19:09 +00009349 linker_patches->reserve(size);
Artem Serovd4cc5b22016-11-04 11:19:09 +00009350 EmitPcRelativeLinkerPatches<LinkerPatch::DexCacheArrayPatch>(pc_relative_dex_cache_patches_,
9351 linker_patches);
Vladimir Marko65979462017-05-19 17:25:12 +01009352 if (GetCompilerOptions().IsBootImage()) {
9353 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeMethodPatch>(pc_relative_method_patches_,
Artem Serovd4cc5b22016-11-04 11:19:09 +00009354 linker_patches);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00009355 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(pc_relative_type_patches_,
9356 linker_patches);
Artem Serovd4cc5b22016-11-04 11:19:09 +00009357 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(pc_relative_string_patches_,
9358 linker_patches);
Vladimir Marko65979462017-05-19 17:25:12 +01009359 } else {
9360 DCHECK(pc_relative_method_patches_.empty());
9361 DCHECK(pc_relative_type_patches_.empty());
9362 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(pc_relative_string_patches_,
9363 linker_patches);
Artem Serovd4cc5b22016-11-04 11:19:09 +00009364 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00009365 EmitPcRelativeLinkerPatches<LinkerPatch::TypeBssEntryPatch>(type_bss_entry_patches_,
9366 linker_patches);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01009367 for (const BakerReadBarrierPatchInfo& info : baker_read_barrier_patches_) {
9368 linker_patches->push_back(LinkerPatch::BakerReadBarrierBranchPatch(info.label.GetLocation(),
9369 info.custom_data));
9370 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00009371 DCHECK_EQ(size, linker_patches->size());
Artem Serovc5fcb442016-12-02 19:19:58 +00009372}
9373
9374VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateUint32Literal(
9375 uint32_t value,
9376 Uint32ToLiteralMap* map) {
9377 return map->GetOrCreate(
9378 value,
9379 [this, value]() {
9380 return GetAssembler()->CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ value);
9381 });
9382}
9383
Artem Serov2bbc9532016-10-21 11:51:50 +01009384void LocationsBuilderARMVIXL::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
9385 LocationSummary* locations =
9386 new (GetGraph()->GetArena()) LocationSummary(instr, LocationSummary::kNoCall);
9387 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
9388 Location::RequiresRegister());
9389 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
9390 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
9391 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
9392}
9393
9394void InstructionCodeGeneratorARMVIXL::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
9395 vixl32::Register res = OutputRegister(instr);
9396 vixl32::Register accumulator =
9397 InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
9398 vixl32::Register mul_left =
9399 InputRegisterAt(instr, HMultiplyAccumulate::kInputMulLeftIndex);
9400 vixl32::Register mul_right =
9401 InputRegisterAt(instr, HMultiplyAccumulate::kInputMulRightIndex);
9402
9403 if (instr->GetOpKind() == HInstruction::kAdd) {
9404 __ Mla(res, mul_left, mul_right, accumulator);
9405 } else {
9406 __ Mls(res, mul_left, mul_right, accumulator);
9407 }
9408}
9409
Artem Serov551b28f2016-10-18 19:11:30 +01009410void LocationsBuilderARMVIXL::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
9411 // Nothing to do, this should be removed during prepare for register allocator.
9412 LOG(FATAL) << "Unreachable";
9413}
9414
9415void InstructionCodeGeneratorARMVIXL::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
9416 // Nothing to do, this should be removed during prepare for register allocator.
9417 LOG(FATAL) << "Unreachable";
9418}
9419
9420// Simple implementation of packed switch - generate cascaded compare/jumps.
9421void LocationsBuilderARMVIXL::VisitPackedSwitch(HPackedSwitch* switch_instr) {
9422 LocationSummary* locations =
9423 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
9424 locations->SetInAt(0, Location::RequiresRegister());
9425 if (switch_instr->GetNumEntries() > kPackedSwitchCompareJumpThreshold &&
9426 codegen_->GetAssembler()->GetVIXLAssembler()->IsUsingT32()) {
9427 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the table base.
9428 if (switch_instr->GetStartValue() != 0) {
9429 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the bias.
9430 }
9431 }
9432}
9433
9434// TODO(VIXL): Investigate and reach the parity with old arm codegen.
9435void InstructionCodeGeneratorARMVIXL::VisitPackedSwitch(HPackedSwitch* switch_instr) {
9436 int32_t lower_bound = switch_instr->GetStartValue();
9437 uint32_t num_entries = switch_instr->GetNumEntries();
9438 LocationSummary* locations = switch_instr->GetLocations();
9439 vixl32::Register value_reg = InputRegisterAt(switch_instr, 0);
9440 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
9441
9442 if (num_entries <= kPackedSwitchCompareJumpThreshold ||
9443 !codegen_->GetAssembler()->GetVIXLAssembler()->IsUsingT32()) {
9444 // Create a series of compare/jumps.
Anton Kirilovedb2ac32016-11-30 15:14:10 +00009445 UseScratchRegisterScope temps(GetVIXLAssembler());
Artem Serov551b28f2016-10-18 19:11:30 +01009446 vixl32::Register temp_reg = temps.Acquire();
9447 // Note: It is fine for the below AddConstantSetFlags() using IP register to temporarily store
9448 // the immediate, because IP is used as the destination register. For the other
9449 // AddConstantSetFlags() and GenerateCompareWithImmediate(), the immediate values are constant,
9450 // and they can be encoded in the instruction without making use of IP register.
9451 __ Adds(temp_reg, value_reg, -lower_bound);
9452
9453 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
9454 // Jump to successors[0] if value == lower_bound.
9455 __ B(eq, codegen_->GetLabelOf(successors[0]));
9456 int32_t last_index = 0;
9457 for (; num_entries - last_index > 2; last_index += 2) {
9458 __ Adds(temp_reg, temp_reg, -2);
9459 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
9460 __ B(lo, codegen_->GetLabelOf(successors[last_index + 1]));
9461 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
9462 __ B(eq, codegen_->GetLabelOf(successors[last_index + 2]));
9463 }
9464 if (num_entries - last_index == 2) {
9465 // The last missing case_value.
9466 __ Cmp(temp_reg, 1);
9467 __ B(eq, codegen_->GetLabelOf(successors[last_index + 1]));
9468 }
9469
9470 // And the default for any other value.
9471 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
9472 __ B(codegen_->GetLabelOf(default_block));
9473 }
9474 } else {
9475 // Create a table lookup.
9476 vixl32::Register table_base = RegisterFrom(locations->GetTemp(0));
9477
9478 JumpTableARMVIXL* jump_table = codegen_->CreateJumpTable(switch_instr);
9479
9480 // Remove the bias.
9481 vixl32::Register key_reg;
9482 if (lower_bound != 0) {
9483 key_reg = RegisterFrom(locations->GetTemp(1));
9484 __ Sub(key_reg, value_reg, lower_bound);
9485 } else {
9486 key_reg = value_reg;
9487 }
9488
9489 // Check whether the value is in the table, jump to default block if not.
9490 __ Cmp(key_reg, num_entries - 1);
9491 __ B(hi, codegen_->GetLabelOf(default_block));
9492
Anton Kirilovedb2ac32016-11-30 15:14:10 +00009493 UseScratchRegisterScope temps(GetVIXLAssembler());
Artem Serov551b28f2016-10-18 19:11:30 +01009494 vixl32::Register jump_offset = temps.Acquire();
9495
9496 // Load jump offset from the table.
Scott Wakeling86e9d262017-01-18 15:59:24 +00009497 {
9498 const size_t jump_size = switch_instr->GetNumEntries() * sizeof(int32_t);
9499 ExactAssemblyScope aas(GetVIXLAssembler(),
9500 (vixl32::kMaxInstructionSizeInBytes * 4) + jump_size,
9501 CodeBufferCheckScope::kMaximumSize);
9502 __ adr(table_base, jump_table->GetTableStartLabel());
9503 __ ldr(jump_offset, MemOperand(table_base, key_reg, vixl32::LSL, 2));
Artem Serov551b28f2016-10-18 19:11:30 +01009504
Scott Wakeling86e9d262017-01-18 15:59:24 +00009505 // Jump to target block by branching to table_base(pc related) + offset.
9506 vixl32::Register target_address = table_base;
9507 __ add(target_address, table_base, jump_offset);
9508 __ bx(target_address);
Artem Serov09a940d2016-11-11 16:15:11 +00009509
Scott Wakeling86e9d262017-01-18 15:59:24 +00009510 jump_table->EmitTable(codegen_);
9511 }
Artem Serov551b28f2016-10-18 19:11:30 +01009512 }
9513}
Artem Serovd4cc5b22016-11-04 11:19:09 +00009514void LocationsBuilderARMVIXL::VisitArmDexCacheArraysBase(HArmDexCacheArraysBase* base) {
9515 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(base);
9516 locations->SetOut(Location::RequiresRegister());
9517}
9518
9519void InstructionCodeGeneratorARMVIXL::VisitArmDexCacheArraysBase(HArmDexCacheArraysBase* base) {
9520 vixl32::Register base_reg = OutputRegister(base);
9521 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
9522 codegen_->NewPcRelativeDexCacheArrayPatch(base->GetDexFile(), base->GetElementOffset());
9523 codegen_->EmitMovwMovtPlaceholder(labels, base_reg);
9524}
Artem Serov551b28f2016-10-18 19:11:30 +01009525
Artem Serov02d37832016-10-25 15:25:33 +01009526// Copy the result of a call into the given target.
Anton Kirilove28d9ae2016-10-25 18:17:23 +01009527void CodeGeneratorARMVIXL::MoveFromReturnRegister(Location trg, Primitive::Type type) {
9528 if (!trg.IsValid()) {
9529 DCHECK_EQ(type, Primitive::kPrimVoid);
9530 return;
9531 }
9532
9533 DCHECK_NE(type, Primitive::kPrimVoid);
9534
Artem Serovd4cc5b22016-11-04 11:19:09 +00009535 Location return_loc = InvokeDexCallingConventionVisitorARMVIXL().GetReturnLocation(type);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01009536 if (return_loc.Equals(trg)) {
9537 return;
9538 }
9539
9540 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
9541 // with the last branch.
9542 if (type == Primitive::kPrimLong) {
9543 TODO_VIXL32(FATAL);
9544 } else if (type == Primitive::kPrimDouble) {
9545 TODO_VIXL32(FATAL);
9546 } else {
9547 // Let the parallel move resolver take care of all of this.
9548 HParallelMove parallel_move(GetGraph()->GetArena());
9549 parallel_move.AddMove(return_loc, trg, type, nullptr);
9550 GetMoveResolver()->EmitNativeCode(&parallel_move);
9551 }
Scott Wakelingfe885462016-09-22 10:24:38 +01009552}
9553
xueliang.zhong8d2c4592016-11-23 17:05:25 +00009554void LocationsBuilderARMVIXL::VisitClassTableGet(HClassTableGet* instruction) {
9555 LocationSummary* locations =
9556 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
9557 locations->SetInAt(0, Location::RequiresRegister());
9558 locations->SetOut(Location::RequiresRegister());
Artem Serov551b28f2016-10-18 19:11:30 +01009559}
9560
xueliang.zhong8d2c4592016-11-23 17:05:25 +00009561void InstructionCodeGeneratorARMVIXL::VisitClassTableGet(HClassTableGet* instruction) {
9562 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
9563 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
9564 instruction->GetIndex(), kArmPointerSize).SizeValue();
9565 GetAssembler()->LoadFromOffset(kLoadWord,
9566 OutputRegister(instruction),
9567 InputRegisterAt(instruction, 0),
9568 method_offset);
9569 } else {
9570 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
9571 instruction->GetIndex(), kArmPointerSize));
9572 GetAssembler()->LoadFromOffset(kLoadWord,
9573 OutputRegister(instruction),
9574 InputRegisterAt(instruction, 0),
9575 mirror::Class::ImtPtrOffset(kArmPointerSize).Uint32Value());
9576 GetAssembler()->LoadFromOffset(kLoadWord,
9577 OutputRegister(instruction),
9578 OutputRegister(instruction),
9579 method_offset);
9580 }
Artem Serov551b28f2016-10-18 19:11:30 +01009581}
9582
Artem Serovc5fcb442016-12-02 19:19:58 +00009583static void PatchJitRootUse(uint8_t* code,
9584 const uint8_t* roots_data,
9585 VIXLUInt32Literal* literal,
9586 uint64_t index_in_table) {
9587 DCHECK(literal->IsBound());
9588 uint32_t literal_offset = literal->GetLocation();
9589 uintptr_t address =
9590 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
9591 uint8_t* data = code + literal_offset;
9592 reinterpret_cast<uint32_t*>(data)[0] = dchecked_integral_cast<uint32_t>(address);
9593}
9594
9595void CodeGeneratorARMVIXL::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
9596 for (const auto& entry : jit_string_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01009597 const StringReference& string_reference = entry.first;
9598 VIXLUInt32Literal* table_entry_literal = entry.second;
9599 const auto it = jit_string_roots_.find(string_reference);
Artem Serovc5fcb442016-12-02 19:19:58 +00009600 DCHECK(it != jit_string_roots_.end());
Vladimir Marko7d157fc2017-05-10 16:29:23 +01009601 uint64_t index_in_table = it->second;
9602 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Artem Serovc5fcb442016-12-02 19:19:58 +00009603 }
9604 for (const auto& entry : jit_class_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01009605 const TypeReference& type_reference = entry.first;
9606 VIXLUInt32Literal* table_entry_literal = entry.second;
9607 const auto it = jit_class_roots_.find(type_reference);
Artem Serovc5fcb442016-12-02 19:19:58 +00009608 DCHECK(it != jit_class_roots_.end());
Vladimir Marko7d157fc2017-05-10 16:29:23 +01009609 uint64_t index_in_table = it->second;
9610 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Artem Serovc5fcb442016-12-02 19:19:58 +00009611 }
9612}
9613
Artem Serovd4cc5b22016-11-04 11:19:09 +00009614void CodeGeneratorARMVIXL::EmitMovwMovtPlaceholder(
9615 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels,
9616 vixl32::Register out) {
Artem Serov0fb37192016-12-06 18:13:40 +00009617 ExactAssemblyScope aas(GetVIXLAssembler(),
9618 3 * vixl32::kMaxInstructionSizeInBytes,
9619 CodeBufferCheckScope::kMaximumSize);
Artem Serovd4cc5b22016-11-04 11:19:09 +00009620 // TODO(VIXL): Think about using mov instead of movw.
9621 __ bind(&labels->movw_label);
9622 __ movw(out, /* placeholder */ 0u);
9623 __ bind(&labels->movt_label);
9624 __ movt(out, /* placeholder */ 0u);
9625 __ bind(&labels->add_pc_label);
9626 __ add(out, out, pc);
9627}
9628
Scott Wakelingfe885462016-09-22 10:24:38 +01009629#undef __
9630#undef QUICK_ENTRY_POINT
9631#undef TODO_VIXL32
9632
9633} // namespace arm
9634} // namespace art