blob: 8288141954cb635a720add18834852681d37728d [file] [log] [blame]
Scott Wakelingfe885462016-09-22 10:24:38 +01001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_arm_vixl.h"
18
Vladimir Markoeee1c0e2017-04-21 17:58:41 +010019#include "arch/arm/asm_support_arm.h"
Scott Wakelingfe885462016-09-22 10:24:38 +010020#include "arch/arm/instruction_set_features_arm.h"
21#include "art_method.h"
Andreas Gampe5678db52017-06-08 14:11:18 -070022#include "base/bit_utils.h"
23#include "base/bit_utils_iterator.h"
Scott Wakelingfe885462016-09-22 10:24:38 +010024#include "code_generator_utils.h"
25#include "common_arm.h"
26#include "compiled_method.h"
27#include "entrypoints/quick/quick_entrypoints.h"
28#include "gc/accounting/card_table.h"
Anton Kirilov5ec62182016-10-13 20:16:02 +010029#include "intrinsics_arm_vixl.h"
Vladimir Markoeee1c0e2017-04-21 17:58:41 +010030#include "linker/arm/relative_patcher_thumb2.h"
Scott Wakelingfe885462016-09-22 10:24:38 +010031#include "mirror/array-inl.h"
32#include "mirror/class-inl.h"
33#include "thread.h"
34#include "utils/arm/assembler_arm_vixl.h"
35#include "utils/arm/managed_register_arm.h"
36#include "utils/assembler.h"
37#include "utils/stack_checks.h"
38
39namespace art {
40namespace arm {
41
42namespace vixl32 = vixl::aarch32;
43using namespace vixl32; // NOLINT(build/namespaces)
44
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +010045using helpers::DRegisterFrom;
Scott Wakelingfe885462016-09-22 10:24:38 +010046using helpers::DWARFReg;
Scott Wakelinga7812ae2016-10-17 10:03:36 +010047using helpers::HighDRegisterFrom;
48using helpers::HighRegisterFrom;
Donghui Bai426b49c2016-11-08 14:55:38 +080049using helpers::InputDRegisterAt;
Scott Wakelingfe885462016-09-22 10:24:38 +010050using helpers::InputOperandAt;
Scott Wakelingc34dba72016-10-03 10:14:44 +010051using helpers::InputRegister;
Scott Wakelinga7812ae2016-10-17 10:03:36 +010052using helpers::InputRegisterAt;
Scott Wakelingfe885462016-09-22 10:24:38 +010053using helpers::InputSRegisterAt;
Anton Kirilov644032c2016-12-06 17:51:43 +000054using helpers::InputVRegister;
Scott Wakelinga7812ae2016-10-17 10:03:36 +010055using helpers::InputVRegisterAt;
Scott Wakelingb77051e2016-11-21 19:46:00 +000056using helpers::Int32ConstantFrom;
Anton Kirilov644032c2016-12-06 17:51:43 +000057using helpers::Int64ConstantFrom;
Scott Wakelinga7812ae2016-10-17 10:03:36 +010058using helpers::LocationFrom;
59using helpers::LowRegisterFrom;
60using helpers::LowSRegisterFrom;
Donghui Bai426b49c2016-11-08 14:55:38 +080061using helpers::OperandFrom;
Scott Wakelinga7812ae2016-10-17 10:03:36 +010062using helpers::OutputRegister;
63using helpers::OutputSRegister;
64using helpers::OutputVRegister;
65using helpers::RegisterFrom;
66using helpers::SRegisterFrom;
Anton Kirilov644032c2016-12-06 17:51:43 +000067using helpers::Uint64ConstantFrom;
Scott Wakelingfe885462016-09-22 10:24:38 +010068
Artem Serov0fb37192016-12-06 18:13:40 +000069using vixl::ExactAssemblyScope;
70using vixl::CodeBufferCheckScope;
71
Scott Wakelingfe885462016-09-22 10:24:38 +010072using RegisterList = vixl32::RegisterList;
73
74static bool ExpectedPairLayout(Location location) {
75 // We expected this for both core and fpu register pairs.
76 return ((location.low() & 1) == 0) && (location.low() + 1 == location.high());
77}
Artem Serovd4cc5b22016-11-04 11:19:09 +000078// Use a local definition to prevent copying mistakes.
79static constexpr size_t kArmWordSize = static_cast<size_t>(kArmPointerSize);
80static constexpr size_t kArmBitsPerWord = kArmWordSize * kBitsPerByte;
Artem Serov551b28f2016-10-18 19:11:30 +010081static constexpr uint32_t kPackedSwitchCompareJumpThreshold = 7;
Scott Wakelingfe885462016-09-22 10:24:38 +010082
Vladimir Markoeee1c0e2017-04-21 17:58:41 +010083// Reference load (except object array loads) is using LDR Rt, [Rn, #offset] which can handle
84// offset < 4KiB. For offsets >= 4KiB, the load shall be emitted as two or more instructions.
85// For the Baker read barrier implementation using link-generated thunks we need to split
86// the offset explicitly.
87constexpr uint32_t kReferenceLoadMinFarOffset = 4 * KB;
88
89// Flags controlling the use of link-time generated thunks for Baker read barriers.
90constexpr bool kBakerReadBarrierLinkTimeThunksEnableForFields = true;
91constexpr bool kBakerReadBarrierLinkTimeThunksEnableForArrays = true;
92constexpr bool kBakerReadBarrierLinkTimeThunksEnableForGcRoots = true;
93
94// The reserved entrypoint register for link-time generated thunks.
95const vixl32::Register kBakerCcEntrypointRegister = r4;
96
Roland Levillain5daa4952017-07-03 17:23:56 +010097// Using a base helps identify when we hit Marking Register check breakpoints.
98constexpr int kMarkingRegisterCheckBreakCodeBaseCode = 0x10;
99
Scott Wakelingfe885462016-09-22 10:24:38 +0100100#ifdef __
101#error "ARM Codegen VIXL macro-assembler macro already defined."
102#endif
103
Scott Wakelingfe885462016-09-22 10:24:38 +0100104// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
105#define __ down_cast<CodeGeneratorARMVIXL*>(codegen)->GetVIXLAssembler()-> // NOLINT
106#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kArmPointerSize, x).Int32Value()
107
108// Marker that code is yet to be, and must, be implemented.
109#define TODO_VIXL32(level) LOG(level) << __PRETTY_FUNCTION__ << " unimplemented "
110
Vladimir Markoeee1c0e2017-04-21 17:58:41 +0100111static inline void ExcludeIPAndBakerCcEntrypointRegister(UseScratchRegisterScope* temps,
112 HInstruction* instruction) {
113 DCHECK(temps->IsAvailable(ip));
114 temps->Exclude(ip);
115 DCHECK(!temps->IsAvailable(kBakerCcEntrypointRegister));
116 DCHECK_EQ(kBakerCcEntrypointRegister.GetCode(),
117 linker::Thumb2RelativePatcher::kBakerCcEntrypointRegister);
118 DCHECK_NE(instruction->GetLocations()->GetTempCount(), 0u);
119 DCHECK(RegisterFrom(instruction->GetLocations()->GetTemp(
120 instruction->GetLocations()->GetTempCount() - 1u)).Is(kBakerCcEntrypointRegister));
121}
122
123static inline void EmitPlaceholderBne(CodeGeneratorARMVIXL* codegen, vixl32::Label* patch_label) {
124 ExactAssemblyScope eas(codegen->GetVIXLAssembler(), kMaxInstructionSizeInBytes);
125 __ bind(patch_label);
126 vixl32::Label placeholder_label;
127 __ b(ne, EncodingSize(Wide), &placeholder_label); // Placeholder, patched at link-time.
128 __ bind(&placeholder_label);
129}
130
Vladimir Marko88abba22017-05-03 17:09:25 +0100131static inline bool CanEmitNarrowLdr(vixl32::Register rt, vixl32::Register rn, uint32_t offset) {
132 return rt.IsLow() && rn.IsLow() && offset < 32u;
133}
134
Vladimir Markoeee1c0e2017-04-21 17:58:41 +0100135class EmitAdrCode {
136 public:
137 EmitAdrCode(ArmVIXLMacroAssembler* assembler, vixl32::Register rd, vixl32::Label* label)
138 : assembler_(assembler), rd_(rd), label_(label) {
139 ExactAssemblyScope aas(assembler, kMaxInstructionSizeInBytes);
140 adr_location_ = assembler->GetCursorOffset();
141 assembler->adr(EncodingSize(Wide), rd, label);
142 }
143
144 ~EmitAdrCode() {
145 DCHECK(label_->IsBound());
146 // The ADR emitted by the assembler does not set the Thumb mode bit we need.
147 // TODO: Maybe extend VIXL to allow ADR for return address?
148 uint8_t* raw_adr = assembler_->GetBuffer()->GetOffsetAddress<uint8_t*>(adr_location_);
149 // Expecting ADR encoding T3 with `(offset & 1) == 0`.
150 DCHECK_EQ(raw_adr[1] & 0xfbu, 0xf2u); // Check bits 24-31, except 26.
151 DCHECK_EQ(raw_adr[0] & 0xffu, 0x0fu); // Check bits 16-23.
152 DCHECK_EQ(raw_adr[3] & 0x8fu, rd_.GetCode()); // Check bits 8-11 and 15.
153 DCHECK_EQ(raw_adr[2] & 0x01u, 0x00u); // Check bit 0, i.e. the `offset & 1`.
154 // Add the Thumb mode bit.
155 raw_adr[2] |= 0x01u;
156 }
157
158 private:
159 ArmVIXLMacroAssembler* const assembler_;
160 vixl32::Register rd_;
161 vixl32::Label* const label_;
162 int32_t adr_location_;
163};
164
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100165// SaveLiveRegisters and RestoreLiveRegisters from SlowPathCodeARM operate on sets of S registers,
166// for each live D registers they treat two corresponding S registers as live ones.
167//
168// Two following functions (SaveContiguousSRegisterList, RestoreContiguousSRegisterList) build
169// from a list of contiguous S registers a list of contiguous D registers (processing first/last
170// S registers corner cases) and save/restore this new list treating them as D registers.
171// - decreasing code size
172// - avoiding hazards on Cortex-A57, when a pair of S registers for an actual live D register is
173// restored and then used in regular non SlowPath code as D register.
174//
175// For the following example (v means the S register is live):
176// D names: | D0 | D1 | D2 | D4 | ...
177// S names: | S0 | S1 | S2 | S3 | S4 | S5 | S6 | S7 | ...
178// Live? | | v | v | v | v | v | v | | ...
179//
180// S1 and S6 will be saved/restored independently; D registers list (D1, D2) will be processed
181// as D registers.
182//
183// TODO(VIXL): All this code should be unnecessary once the VIXL AArch32 backend provides helpers
184// for lists of floating-point registers.
185static size_t SaveContiguousSRegisterList(size_t first,
186 size_t last,
187 CodeGenerator* codegen,
188 size_t stack_offset) {
189 static_assert(kSRegSizeInBytes == kArmWordSize, "Broken assumption on reg/word sizes.");
190 static_assert(kDRegSizeInBytes == 2 * kArmWordSize, "Broken assumption on reg/word sizes.");
191 DCHECK_LE(first, last);
192 if ((first == last) && (first == 0)) {
193 __ Vstr(vixl32::SRegister(first), MemOperand(sp, stack_offset));
194 return stack_offset + kSRegSizeInBytes;
195 }
196 if (first % 2 == 1) {
197 __ Vstr(vixl32::SRegister(first++), MemOperand(sp, stack_offset));
198 stack_offset += kSRegSizeInBytes;
199 }
200
201 bool save_last = false;
202 if (last % 2 == 0) {
203 save_last = true;
204 --last;
205 }
206
207 if (first < last) {
208 vixl32::DRegister d_reg = vixl32::DRegister(first / 2);
209 DCHECK_EQ((last - first + 1) % 2, 0u);
210 size_t number_of_d_regs = (last - first + 1) / 2;
211
212 if (number_of_d_regs == 1) {
213 __ Vstr(d_reg, MemOperand(sp, stack_offset));
214 } else if (number_of_d_regs > 1) {
215 UseScratchRegisterScope temps(down_cast<CodeGeneratorARMVIXL*>(codegen)->GetVIXLAssembler());
216 vixl32::Register base = sp;
217 if (stack_offset != 0) {
218 base = temps.Acquire();
Scott Wakelingb77051e2016-11-21 19:46:00 +0000219 __ Add(base, sp, Operand::From(stack_offset));
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100220 }
221 __ Vstm(F64, base, NO_WRITE_BACK, DRegisterList(d_reg, number_of_d_regs));
222 }
223 stack_offset += number_of_d_regs * kDRegSizeInBytes;
224 }
225
226 if (save_last) {
227 __ Vstr(vixl32::SRegister(last + 1), MemOperand(sp, stack_offset));
228 stack_offset += kSRegSizeInBytes;
229 }
230
231 return stack_offset;
232}
233
234static size_t RestoreContiguousSRegisterList(size_t first,
235 size_t last,
236 CodeGenerator* codegen,
237 size_t stack_offset) {
238 static_assert(kSRegSizeInBytes == kArmWordSize, "Broken assumption on reg/word sizes.");
239 static_assert(kDRegSizeInBytes == 2 * kArmWordSize, "Broken assumption on reg/word sizes.");
240 DCHECK_LE(first, last);
241 if ((first == last) && (first == 0)) {
242 __ Vldr(vixl32::SRegister(first), MemOperand(sp, stack_offset));
243 return stack_offset + kSRegSizeInBytes;
244 }
245 if (first % 2 == 1) {
246 __ Vldr(vixl32::SRegister(first++), MemOperand(sp, stack_offset));
247 stack_offset += kSRegSizeInBytes;
248 }
249
250 bool restore_last = false;
251 if (last % 2 == 0) {
252 restore_last = true;
253 --last;
254 }
255
256 if (first < last) {
257 vixl32::DRegister d_reg = vixl32::DRegister(first / 2);
258 DCHECK_EQ((last - first + 1) % 2, 0u);
259 size_t number_of_d_regs = (last - first + 1) / 2;
260 if (number_of_d_regs == 1) {
261 __ Vldr(d_reg, MemOperand(sp, stack_offset));
262 } else if (number_of_d_regs > 1) {
263 UseScratchRegisterScope temps(down_cast<CodeGeneratorARMVIXL*>(codegen)->GetVIXLAssembler());
264 vixl32::Register base = sp;
265 if (stack_offset != 0) {
266 base = temps.Acquire();
Scott Wakelingb77051e2016-11-21 19:46:00 +0000267 __ Add(base, sp, Operand::From(stack_offset));
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100268 }
269 __ Vldm(F64, base, NO_WRITE_BACK, DRegisterList(d_reg, number_of_d_regs));
270 }
271 stack_offset += number_of_d_regs * kDRegSizeInBytes;
272 }
273
274 if (restore_last) {
275 __ Vldr(vixl32::SRegister(last + 1), MemOperand(sp, stack_offset));
276 stack_offset += kSRegSizeInBytes;
277 }
278
279 return stack_offset;
280}
281
282void SlowPathCodeARMVIXL::SaveLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
283 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
284 size_t orig_offset = stack_offset;
285
286 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ true);
287 for (uint32_t i : LowToHighBits(core_spills)) {
288 // If the register holds an object, update the stack mask.
289 if (locations->RegisterContainsObject(i)) {
290 locations->SetStackBit(stack_offset / kVRegSize);
291 }
292 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
293 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
294 saved_core_stack_offsets_[i] = stack_offset;
295 stack_offset += kArmWordSize;
296 }
297
298 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
299 arm_codegen->GetAssembler()->StoreRegisterList(core_spills, orig_offset);
300
301 uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ false);
302 orig_offset = stack_offset;
303 for (uint32_t i : LowToHighBits(fp_spills)) {
304 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
305 saved_fpu_stack_offsets_[i] = stack_offset;
306 stack_offset += kArmWordSize;
307 }
308
309 stack_offset = orig_offset;
310 while (fp_spills != 0u) {
311 uint32_t begin = CTZ(fp_spills);
312 uint32_t tmp = fp_spills + (1u << begin);
313 fp_spills &= tmp; // Clear the contiguous range of 1s.
314 uint32_t end = (tmp == 0u) ? 32u : CTZ(tmp); // CTZ(0) is undefined.
315 stack_offset = SaveContiguousSRegisterList(begin, end - 1, codegen, stack_offset);
316 }
317 DCHECK_LE(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
318}
319
320void SlowPathCodeARMVIXL::RestoreLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
321 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
322 size_t orig_offset = stack_offset;
323
324 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ true);
325 for (uint32_t i : LowToHighBits(core_spills)) {
326 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
327 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
328 stack_offset += kArmWordSize;
329 }
330
331 // TODO(VIXL): Check the coherency of stack_offset after this with a test.
332 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
333 arm_codegen->GetAssembler()->LoadRegisterList(core_spills, orig_offset);
334
335 uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ false);
336 while (fp_spills != 0u) {
337 uint32_t begin = CTZ(fp_spills);
338 uint32_t tmp = fp_spills + (1u << begin);
339 fp_spills &= tmp; // Clear the contiguous range of 1s.
340 uint32_t end = (tmp == 0u) ? 32u : CTZ(tmp); // CTZ(0) is undefined.
341 stack_offset = RestoreContiguousSRegisterList(begin, end - 1, codegen, stack_offset);
342 }
343 DCHECK_LE(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
344}
345
346class NullCheckSlowPathARMVIXL : public SlowPathCodeARMVIXL {
347 public:
348 explicit NullCheckSlowPathARMVIXL(HNullCheck* instruction) : SlowPathCodeARMVIXL(instruction) {}
349
350 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
351 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
352 __ Bind(GetEntryLabel());
353 if (instruction_->CanThrowIntoCatchBlock()) {
354 // Live registers will be restored in the catch block if caught.
355 SaveLiveRegisters(codegen, instruction_->GetLocations());
356 }
357 arm_codegen->InvokeRuntime(kQuickThrowNullPointer,
358 instruction_,
359 instruction_->GetDexPc(),
360 this);
361 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
362 }
363
364 bool IsFatal() const OVERRIDE { return true; }
365
366 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathARMVIXL"; }
367
368 private:
369 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARMVIXL);
370};
371
Scott Wakelingfe885462016-09-22 10:24:38 +0100372class DivZeroCheckSlowPathARMVIXL : public SlowPathCodeARMVIXL {
373 public:
374 explicit DivZeroCheckSlowPathARMVIXL(HDivZeroCheck* instruction)
375 : SlowPathCodeARMVIXL(instruction) {}
376
377 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100378 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
Scott Wakelingfe885462016-09-22 10:24:38 +0100379 __ Bind(GetEntryLabel());
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100380 arm_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Scott Wakelingfe885462016-09-22 10:24:38 +0100381 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
382 }
383
384 bool IsFatal() const OVERRIDE { return true; }
385
386 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathARMVIXL"; }
387
388 private:
389 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARMVIXL);
390};
391
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100392class SuspendCheckSlowPathARMVIXL : public SlowPathCodeARMVIXL {
393 public:
394 SuspendCheckSlowPathARMVIXL(HSuspendCheck* instruction, HBasicBlock* successor)
395 : SlowPathCodeARMVIXL(instruction), successor_(successor) {}
396
397 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
398 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
399 __ Bind(GetEntryLabel());
400 arm_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
401 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
402 if (successor_ == nullptr) {
403 __ B(GetReturnLabel());
404 } else {
405 __ B(arm_codegen->GetLabelOf(successor_));
406 }
407 }
408
409 vixl32::Label* GetReturnLabel() {
410 DCHECK(successor_ == nullptr);
411 return &return_label_;
412 }
413
414 HBasicBlock* GetSuccessor() const {
415 return successor_;
416 }
417
418 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathARMVIXL"; }
419
420 private:
421 // If not null, the block to branch to after the suspend check.
422 HBasicBlock* const successor_;
423
424 // If `successor_` is null, the label to branch to after the suspend check.
425 vixl32::Label return_label_;
426
427 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARMVIXL);
428};
429
Scott Wakelingc34dba72016-10-03 10:14:44 +0100430class BoundsCheckSlowPathARMVIXL : public SlowPathCodeARMVIXL {
431 public:
432 explicit BoundsCheckSlowPathARMVIXL(HBoundsCheck* instruction)
433 : SlowPathCodeARMVIXL(instruction) {}
434
435 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
436 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
437 LocationSummary* locations = instruction_->GetLocations();
438
439 __ Bind(GetEntryLabel());
440 if (instruction_->CanThrowIntoCatchBlock()) {
441 // Live registers will be restored in the catch block if caught.
442 SaveLiveRegisters(codegen, instruction_->GetLocations());
443 }
444 // We're moving two locations to locations that could overlap, so we need a parallel
445 // move resolver.
446 InvokeRuntimeCallingConventionARMVIXL calling_convention;
447 codegen->EmitParallelMoves(
448 locations->InAt(0),
449 LocationFrom(calling_convention.GetRegisterAt(0)),
450 Primitive::kPrimInt,
451 locations->InAt(1),
452 LocationFrom(calling_convention.GetRegisterAt(1)),
453 Primitive::kPrimInt);
454 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
455 ? kQuickThrowStringBounds
456 : kQuickThrowArrayBounds;
457 arm_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
458 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
459 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
460 }
461
462 bool IsFatal() const OVERRIDE { return true; }
463
464 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathARMVIXL"; }
465
466 private:
467 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARMVIXL);
468};
469
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100470class LoadClassSlowPathARMVIXL : public SlowPathCodeARMVIXL {
471 public:
472 LoadClassSlowPathARMVIXL(HLoadClass* cls, HInstruction* at, uint32_t dex_pc, bool do_clinit)
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000473 : SlowPathCodeARMVIXL(at), cls_(cls), dex_pc_(dex_pc), do_clinit_(do_clinit) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100474 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
475 }
476
477 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000478 LocationSummary* locations = instruction_->GetLocations();
Vladimir Markoea4c1262017-02-06 19:59:33 +0000479 Location out = locations->Out();
480 constexpr bool call_saves_everything_except_r0 = (!kUseReadBarrier || kUseBakerReadBarrier);
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100481
482 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
483 __ Bind(GetEntryLabel());
484 SaveLiveRegisters(codegen, locations);
485
486 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Vladimir Markoea4c1262017-02-06 19:59:33 +0000487 // For HLoadClass/kBssEntry/kSaveEverything, make sure we preserve the address of the entry.
488 DCHECK_EQ(instruction_->IsLoadClass(), cls_ == instruction_);
489 bool is_load_class_bss_entry =
490 (cls_ == instruction_) && (cls_->GetLoadKind() == HLoadClass::LoadKind::kBssEntry);
491 vixl32::Register entry_address;
492 if (is_load_class_bss_entry && call_saves_everything_except_r0) {
493 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
494 // In the unlucky case that the `temp` is R0, we preserve the address in `out` across
495 // the kSaveEverything call.
496 bool temp_is_r0 = temp.Is(calling_convention.GetRegisterAt(0));
497 entry_address = temp_is_r0 ? RegisterFrom(out) : temp;
498 DCHECK(!entry_address.Is(calling_convention.GetRegisterAt(0)));
499 if (temp_is_r0) {
500 __ Mov(entry_address, temp);
501 }
502 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000503 dex::TypeIndex type_index = cls_->GetTypeIndex();
504 __ Mov(calling_convention.GetRegisterAt(0), type_index.index_);
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100505 QuickEntrypointEnum entrypoint = do_clinit_ ? kQuickInitializeStaticStorage
506 : kQuickInitializeType;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000507 arm_codegen->InvokeRuntime(entrypoint, instruction_, dex_pc_, this);
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100508 if (do_clinit_) {
509 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
510 } else {
511 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
512 }
513
Vladimir Markoea4c1262017-02-06 19:59:33 +0000514 // For HLoadClass/kBssEntry, store the resolved Class to the BSS entry.
515 if (is_load_class_bss_entry) {
516 if (call_saves_everything_except_r0) {
517 // The class entry address was preserved in `entry_address` thanks to kSaveEverything.
518 __ Str(r0, MemOperand(entry_address));
519 } else {
520 // For non-Baker read barrier, we need to re-calculate the address of the string entry.
521 UseScratchRegisterScope temps(
522 down_cast<CodeGeneratorARMVIXL*>(codegen)->GetVIXLAssembler());
523 vixl32::Register temp = temps.Acquire();
524 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
525 arm_codegen->NewTypeBssEntryPatch(cls_->GetDexFile(), type_index);
526 arm_codegen->EmitMovwMovtPlaceholder(labels, temp);
527 __ Str(r0, MemOperand(temp));
528 }
529 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100530 // Move the class to the desired location.
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100531 if (out.IsValid()) {
532 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
533 arm_codegen->Move32(locations->Out(), LocationFrom(r0));
534 }
535 RestoreLiveRegisters(codegen, locations);
536 __ B(GetExitLabel());
537 }
538
539 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathARMVIXL"; }
540
541 private:
542 // The class this slow path will load.
543 HLoadClass* const cls_;
544
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100545 // The dex PC of `at_`.
546 const uint32_t dex_pc_;
547
548 // Whether to initialize the class.
549 const bool do_clinit_;
550
551 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARMVIXL);
552};
553
Artem Serovd4cc5b22016-11-04 11:19:09 +0000554class LoadStringSlowPathARMVIXL : public SlowPathCodeARMVIXL {
555 public:
556 explicit LoadStringSlowPathARMVIXL(HLoadString* instruction)
557 : SlowPathCodeARMVIXL(instruction) {}
558
559 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Vladimir Markoea4c1262017-02-06 19:59:33 +0000560 DCHECK(instruction_->IsLoadString());
561 DCHECK_EQ(instruction_->AsLoadString()->GetLoadKind(), HLoadString::LoadKind::kBssEntry);
Artem Serovd4cc5b22016-11-04 11:19:09 +0000562 LocationSummary* locations = instruction_->GetLocations();
563 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
564 HLoadString* load = instruction_->AsLoadString();
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000565 const dex::StringIndex string_index = load->GetStringIndex();
Artem Serovd4cc5b22016-11-04 11:19:09 +0000566 vixl32::Register out = OutputRegister(load);
Artem Serovd4cc5b22016-11-04 11:19:09 +0000567 constexpr bool call_saves_everything_except_r0 = (!kUseReadBarrier || kUseBakerReadBarrier);
568
569 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
570 __ Bind(GetEntryLabel());
571 SaveLiveRegisters(codegen, locations);
572
573 InvokeRuntimeCallingConventionARMVIXL calling_convention;
574 // In the unlucky case that the `temp` is R0, we preserve the address in `out` across
Vladimir Markoea4c1262017-02-06 19:59:33 +0000575 // the kSaveEverything call.
576 vixl32::Register entry_address;
577 if (call_saves_everything_except_r0) {
578 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
579 bool temp_is_r0 = (temp.Is(calling_convention.GetRegisterAt(0)));
580 entry_address = temp_is_r0 ? out : temp;
581 DCHECK(!entry_address.Is(calling_convention.GetRegisterAt(0)));
582 if (temp_is_r0) {
583 __ Mov(entry_address, temp);
584 }
Artem Serovd4cc5b22016-11-04 11:19:09 +0000585 }
586
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000587 __ Mov(calling_convention.GetRegisterAt(0), string_index.index_);
Artem Serovd4cc5b22016-11-04 11:19:09 +0000588 arm_codegen->InvokeRuntime(kQuickResolveString, instruction_, instruction_->GetDexPc(), this);
589 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
590
591 // Store the resolved String to the .bss entry.
592 if (call_saves_everything_except_r0) {
593 // The string entry address was preserved in `entry_address` thanks to kSaveEverything.
594 __ Str(r0, MemOperand(entry_address));
595 } else {
596 // For non-Baker read barrier, we need to re-calculate the address of the string entry.
Vladimir Markoea4c1262017-02-06 19:59:33 +0000597 UseScratchRegisterScope temps(
598 down_cast<CodeGeneratorARMVIXL*>(codegen)->GetVIXLAssembler());
599 vixl32::Register temp = temps.Acquire();
Artem Serovd4cc5b22016-11-04 11:19:09 +0000600 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +0100601 arm_codegen->NewStringBssEntryPatch(load->GetDexFile(), string_index);
Vladimir Markoea4c1262017-02-06 19:59:33 +0000602 arm_codegen->EmitMovwMovtPlaceholder(labels, temp);
603 __ Str(r0, MemOperand(temp));
Artem Serovd4cc5b22016-11-04 11:19:09 +0000604 }
605
606 arm_codegen->Move32(locations->Out(), LocationFrom(r0));
607 RestoreLiveRegisters(codegen, locations);
608
609 __ B(GetExitLabel());
610 }
611
612 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathARMVIXL"; }
613
614 private:
615 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARMVIXL);
616};
617
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100618class TypeCheckSlowPathARMVIXL : public SlowPathCodeARMVIXL {
619 public:
620 TypeCheckSlowPathARMVIXL(HInstruction* instruction, bool is_fatal)
621 : SlowPathCodeARMVIXL(instruction), is_fatal_(is_fatal) {}
622
623 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
624 LocationSummary* locations = instruction_->GetLocations();
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100625 DCHECK(instruction_->IsCheckCast()
626 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
627
628 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
629 __ Bind(GetEntryLabel());
630
631 if (!is_fatal_) {
Artem Serovcfbe9132016-10-14 15:58:56 +0100632 SaveLiveRegisters(codegen, locations);
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100633 }
634
635 // We're moving two locations to locations that could overlap, so we need a parallel
636 // move resolver.
637 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100638
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800639 codegen->EmitParallelMoves(locations->InAt(0),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800640 LocationFrom(calling_convention.GetRegisterAt(0)),
641 Primitive::kPrimNot,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800642 locations->InAt(1),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800643 LocationFrom(calling_convention.GetRegisterAt(1)),
644 Primitive::kPrimNot);
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100645 if (instruction_->IsInstanceOf()) {
Artem Serovcfbe9132016-10-14 15:58:56 +0100646 arm_codegen->InvokeRuntime(kQuickInstanceofNonTrivial,
647 instruction_,
648 instruction_->GetDexPc(),
649 this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800650 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Artem Serovcfbe9132016-10-14 15:58:56 +0100651 arm_codegen->Move32(locations->Out(), LocationFrom(r0));
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100652 } else {
653 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800654 arm_codegen->InvokeRuntime(kQuickCheckInstanceOf,
655 instruction_,
656 instruction_->GetDexPc(),
657 this);
658 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100659 }
660
661 if (!is_fatal_) {
Artem Serovcfbe9132016-10-14 15:58:56 +0100662 RestoreLiveRegisters(codegen, locations);
663 __ B(GetExitLabel());
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100664 }
665 }
666
667 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathARMVIXL"; }
668
669 bool IsFatal() const OVERRIDE { return is_fatal_; }
670
671 private:
672 const bool is_fatal_;
673
674 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARMVIXL);
675};
676
Scott Wakelingc34dba72016-10-03 10:14:44 +0100677class DeoptimizationSlowPathARMVIXL : public SlowPathCodeARMVIXL {
678 public:
679 explicit DeoptimizationSlowPathARMVIXL(HDeoptimize* instruction)
680 : SlowPathCodeARMVIXL(instruction) {}
681
682 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
683 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
684 __ Bind(GetEntryLabel());
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100685 LocationSummary* locations = instruction_->GetLocations();
686 SaveLiveRegisters(codegen, locations);
687 InvokeRuntimeCallingConventionARMVIXL calling_convention;
688 __ Mov(calling_convention.GetRegisterAt(0),
689 static_cast<uint32_t>(instruction_->AsDeoptimize()->GetDeoptimizationKind()));
690
Scott Wakelingc34dba72016-10-03 10:14:44 +0100691 arm_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100692 CheckEntrypointTypes<kQuickDeoptimize, void, DeoptimizationKind>();
Scott Wakelingc34dba72016-10-03 10:14:44 +0100693 }
694
695 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathARMVIXL"; }
696
697 private:
698 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARMVIXL);
699};
700
701class ArraySetSlowPathARMVIXL : public SlowPathCodeARMVIXL {
702 public:
703 explicit ArraySetSlowPathARMVIXL(HInstruction* instruction) : SlowPathCodeARMVIXL(instruction) {}
704
705 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
706 LocationSummary* locations = instruction_->GetLocations();
707 __ Bind(GetEntryLabel());
708 SaveLiveRegisters(codegen, locations);
709
710 InvokeRuntimeCallingConventionARMVIXL calling_convention;
711 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
712 parallel_move.AddMove(
713 locations->InAt(0),
714 LocationFrom(calling_convention.GetRegisterAt(0)),
715 Primitive::kPrimNot,
716 nullptr);
717 parallel_move.AddMove(
718 locations->InAt(1),
719 LocationFrom(calling_convention.GetRegisterAt(1)),
720 Primitive::kPrimInt,
721 nullptr);
722 parallel_move.AddMove(
723 locations->InAt(2),
724 LocationFrom(calling_convention.GetRegisterAt(2)),
725 Primitive::kPrimNot,
726 nullptr);
727 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
728
729 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
730 arm_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
731 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
732 RestoreLiveRegisters(codegen, locations);
733 __ B(GetExitLabel());
734 }
735
736 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathARMVIXL"; }
737
738 private:
739 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathARMVIXL);
740};
741
Roland Levillain54f869e2017-03-06 13:54:11 +0000742// Abstract base class for read barrier slow paths marking a reference
743// `ref`.
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000744//
Roland Levillain54f869e2017-03-06 13:54:11 +0000745// Argument `entrypoint` must be a register location holding the read
Roland Levillain6d729a72017-06-30 18:34:01 +0100746// barrier marking runtime entry point to be invoked or an empty
747// location; in the latter case, the read barrier marking runtime
748// entry point will be loaded by the slow path code itself.
Roland Levillain54f869e2017-03-06 13:54:11 +0000749class ReadBarrierMarkSlowPathBaseARMVIXL : public SlowPathCodeARMVIXL {
750 protected:
751 ReadBarrierMarkSlowPathBaseARMVIXL(HInstruction* instruction, Location ref, Location entrypoint)
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000752 : SlowPathCodeARMVIXL(instruction), ref_(ref), entrypoint_(entrypoint) {
753 DCHECK(kEmitCompilerReadBarrier);
754 }
755
Roland Levillain54f869e2017-03-06 13:54:11 +0000756 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathBaseARMVIXL"; }
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000757
Roland Levillain54f869e2017-03-06 13:54:11 +0000758 // Generate assembly code calling the read barrier marking runtime
759 // entry point (ReadBarrierMarkRegX).
760 void GenerateReadBarrierMarkRuntimeCall(CodeGenerator* codegen) {
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000761 vixl32::Register ref_reg = RegisterFrom(ref_);
Roland Levillain47b3ab22017-02-27 14:31:35 +0000762
Roland Levillain47b3ab22017-02-27 14:31:35 +0000763 // No need to save live registers; it's taken care of by the
764 // entrypoint. Also, there is no need to update the stack mask,
765 // as this runtime call will not trigger a garbage collection.
766 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
767 DCHECK(!ref_reg.Is(sp));
768 DCHECK(!ref_reg.Is(lr));
769 DCHECK(!ref_reg.Is(pc));
770 // IP is used internally by the ReadBarrierMarkRegX entry point
771 // as a temporary, it cannot be the entry point's input/output.
772 DCHECK(!ref_reg.Is(ip));
773 DCHECK(ref_reg.IsRegister()) << ref_reg;
774 // "Compact" slow path, saving two moves.
775 //
776 // Instead of using the standard runtime calling convention (input
777 // and output in R0):
778 //
779 // R0 <- ref
780 // R0 <- ReadBarrierMark(R0)
781 // ref <- R0
782 //
783 // we just use rX (the register containing `ref`) as input and output
784 // of a dedicated entrypoint:
785 //
786 // rX <- ReadBarrierMarkRegX(rX)
787 //
788 if (entrypoint_.IsValid()) {
789 arm_codegen->ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction_, this);
790 __ Blx(RegisterFrom(entrypoint_));
791 } else {
Roland Levillain54f869e2017-03-06 13:54:11 +0000792 // Entrypoint is not already loaded, load from the thread.
Roland Levillain47b3ab22017-02-27 14:31:35 +0000793 int32_t entry_point_offset =
Roland Levillain97c46462017-05-11 14:04:03 +0100794 Thread::ReadBarrierMarkEntryPointsOffset<kArmPointerSize>(ref_reg.GetCode());
Roland Levillain47b3ab22017-02-27 14:31:35 +0000795 // This runtime call does not require a stack map.
796 arm_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
797 }
Roland Levillain47b3ab22017-02-27 14:31:35 +0000798 }
799
Roland Levillain47b3ab22017-02-27 14:31:35 +0000800 // The location (register) of the marked object reference.
801 const Location ref_;
802
803 // The location of the entrypoint if already loaded.
804 const Location entrypoint_;
805
Roland Levillain54f869e2017-03-06 13:54:11 +0000806 private:
807 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathBaseARMVIXL);
Roland Levillain47b3ab22017-02-27 14:31:35 +0000808};
809
Scott Wakelingc34dba72016-10-03 10:14:44 +0100810// Slow path marking an object reference `ref` during a read
811// barrier. The field `obj.field` in the object `obj` holding this
Roland Levillain54f869e2017-03-06 13:54:11 +0000812// reference does not get updated by this slow path after marking.
Roland Levillain47b3ab22017-02-27 14:31:35 +0000813//
Scott Wakelingc34dba72016-10-03 10:14:44 +0100814// This means that after the execution of this slow path, `ref` will
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000815// always be up-to-date, but `obj.field` may not; i.e., after the
816// flip, `ref` will be a to-space reference, but `obj.field` will
817// probably still be a from-space reference (unless it gets updated by
818// another thread, or if another thread installed another object
819// reference (different from `ref`) in `obj.field`).
Roland Levillainba650a42017-03-06 13:52:32 +0000820//
Roland Levillain6d729a72017-06-30 18:34:01 +0100821// Argument `entrypoint` must be a register location holding the read
822// barrier marking runtime entry point to be invoked or an empty
823// location; in the latter case, the read barrier marking runtime
824// entry point will be loaded by the slow path code itself.
Roland Levillain54f869e2017-03-06 13:54:11 +0000825class ReadBarrierMarkSlowPathARMVIXL : public ReadBarrierMarkSlowPathBaseARMVIXL {
Roland Levillain47b3ab22017-02-27 14:31:35 +0000826 public:
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000827 ReadBarrierMarkSlowPathARMVIXL(HInstruction* instruction,
828 Location ref,
829 Location entrypoint = Location::NoLocation())
Roland Levillain54f869e2017-03-06 13:54:11 +0000830 : ReadBarrierMarkSlowPathBaseARMVIXL(instruction, ref, entrypoint) {
Roland Levillain47b3ab22017-02-27 14:31:35 +0000831 DCHECK(kEmitCompilerReadBarrier);
832 }
833
Roland Levillain47b3ab22017-02-27 14:31:35 +0000834 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathARMVIXL"; }
835
836 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
837 LocationSummary* locations = instruction_->GetLocations();
Roland Levillain54f869e2017-03-06 13:54:11 +0000838 DCHECK(locations->CanCall());
839 DCHECK(ref_.IsRegister()) << ref_;
840 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_.reg())) << ref_.reg();
841 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
842 << "Unexpected instruction in read barrier marking slow path: "
843 << instruction_->DebugName();
844
845 __ Bind(GetEntryLabel());
846 GenerateReadBarrierMarkRuntimeCall(codegen);
847 __ B(GetExitLabel());
848 }
849
850 private:
851 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathARMVIXL);
852};
853
854// Slow path loading `obj`'s lock word, loading a reference from
855// object `*(obj + offset + (index << scale_factor))` into `ref`, and
856// marking `ref` if `obj` is gray according to the lock word (Baker
857// read barrier). The field `obj.field` in the object `obj` holding
858// this reference does not get updated by this slow path after marking
859// (see LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARMVIXL
860// below for that).
861//
862// This means that after the execution of this slow path, `ref` will
863// always be up-to-date, but `obj.field` may not; i.e., after the
864// flip, `ref` will be a to-space reference, but `obj.field` will
865// probably still be a from-space reference (unless it gets updated by
866// another thread, or if another thread installed another object
867// reference (different from `ref`) in `obj.field`).
868//
869// Argument `entrypoint` must be a register location holding the read
Roland Levillain6d729a72017-06-30 18:34:01 +0100870// barrier marking runtime entry point to be invoked or an empty
871// location; in the latter case, the read barrier marking runtime
872// entry point will be loaded by the slow path code itself.
Roland Levillain54f869e2017-03-06 13:54:11 +0000873class LoadReferenceWithBakerReadBarrierSlowPathARMVIXL : public ReadBarrierMarkSlowPathBaseARMVIXL {
874 public:
875 LoadReferenceWithBakerReadBarrierSlowPathARMVIXL(HInstruction* instruction,
876 Location ref,
877 vixl32::Register obj,
878 uint32_t offset,
879 Location index,
880 ScaleFactor scale_factor,
881 bool needs_null_check,
882 vixl32::Register temp,
Roland Levillain6d729a72017-06-30 18:34:01 +0100883 Location entrypoint = Location::NoLocation())
Roland Levillain54f869e2017-03-06 13:54:11 +0000884 : ReadBarrierMarkSlowPathBaseARMVIXL(instruction, ref, entrypoint),
885 obj_(obj),
886 offset_(offset),
887 index_(index),
888 scale_factor_(scale_factor),
889 needs_null_check_(needs_null_check),
890 temp_(temp) {
891 DCHECK(kEmitCompilerReadBarrier);
892 DCHECK(kUseBakerReadBarrier);
893 }
894
Roland Levillain47b3ab22017-02-27 14:31:35 +0000895 const char* GetDescription() const OVERRIDE {
Roland Levillain54f869e2017-03-06 13:54:11 +0000896 return "LoadReferenceWithBakerReadBarrierSlowPathARMVIXL";
Roland Levillain47b3ab22017-02-27 14:31:35 +0000897 }
898
899 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
900 LocationSummary* locations = instruction_->GetLocations();
901 vixl32::Register ref_reg = RegisterFrom(ref_);
902 DCHECK(locations->CanCall());
903 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg.GetCode())) << ref_reg;
Roland Levillain47b3ab22017-02-27 14:31:35 +0000904 DCHECK(instruction_->IsInstanceFieldGet() ||
905 instruction_->IsStaticFieldGet() ||
906 instruction_->IsArrayGet() ||
907 instruction_->IsArraySet() ||
Roland Levillain47b3ab22017-02-27 14:31:35 +0000908 instruction_->IsInstanceOf() ||
909 instruction_->IsCheckCast() ||
910 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
911 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
912 << "Unexpected instruction in read barrier marking slow path: "
913 << instruction_->DebugName();
914 // The read barrier instrumentation of object ArrayGet
915 // instructions does not support the HIntermediateAddress
916 // instruction.
917 DCHECK(!(instruction_->IsArrayGet() &&
918 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
919
Roland Levillain54f869e2017-03-06 13:54:11 +0000920 // Temporary register `temp_`, used to store the lock word, must
921 // not be IP, as we may use it to emit the reference load (in the
922 // call to GenerateRawReferenceLoad below), and we need the lock
923 // word to still be in `temp_` after the reference load.
924 DCHECK(!temp_.Is(ip));
925
Roland Levillain47b3ab22017-02-27 14:31:35 +0000926 __ Bind(GetEntryLabel());
Roland Levillain54f869e2017-03-06 13:54:11 +0000927
928 // When using MaybeGenerateReadBarrierSlow, the read barrier call is
929 // inserted after the original load. However, in fast path based
930 // Baker's read barriers, we need to perform the load of
931 // mirror::Object::monitor_ *before* the original reference load.
932 // This load-load ordering is required by the read barrier.
Roland Levillainff487002017-03-07 16:50:01 +0000933 // The slow path (for Baker's algorithm) should look like:
Roland Levillain54f869e2017-03-06 13:54:11 +0000934 //
935 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
936 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
937 // HeapReference<mirror::Object> ref = *src; // Original reference load.
938 // bool is_gray = (rb_state == ReadBarrier::GrayState());
939 // if (is_gray) {
940 // ref = entrypoint(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
941 // }
942 //
943 // Note: the original implementation in ReadBarrier::Barrier is
944 // slightly more complex as it performs additional checks that we do
945 // not do here for performance reasons.
946
Roland Levillain47b3ab22017-02-27 14:31:35 +0000947 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
Roland Levillain54f869e2017-03-06 13:54:11 +0000948
949 // /* int32_t */ monitor = obj->monitor_
950 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
951 arm_codegen->GetAssembler()->LoadFromOffset(kLoadWord, temp_, obj_, monitor_offset);
952 if (needs_null_check_) {
953 codegen->MaybeRecordImplicitNullCheck(instruction_);
Roland Levillain47b3ab22017-02-27 14:31:35 +0000954 }
Roland Levillain54f869e2017-03-06 13:54:11 +0000955 // /* LockWord */ lock_word = LockWord(monitor)
956 static_assert(sizeof(LockWord) == sizeof(int32_t),
957 "art::LockWord and int32_t have different sizes.");
958
959 // Introduce a dependency on the lock_word including the rb_state,
960 // which shall prevent load-load reordering without using
961 // a memory barrier (which would be more expensive).
962 // `obj` is unchanged by this operation, but its value now depends
963 // on `temp`.
964 __ Add(obj_, obj_, Operand(temp_, ShiftType::LSR, 32));
965
966 // The actual reference load.
967 // A possible implicit null check has already been handled above.
968 arm_codegen->GenerateRawReferenceLoad(
969 instruction_, ref_, obj_, offset_, index_, scale_factor_, /* needs_null_check */ false);
970
971 // Mark the object `ref` when `obj` is gray.
972 //
973 // if (rb_state == ReadBarrier::GrayState())
974 // ref = ReadBarrier::Mark(ref);
975 //
976 // Given the numeric representation, it's enough to check the low bit of the
977 // rb_state. We do that by shifting the bit out of the lock word with LSRS
978 // which can be a 16-bit instruction unlike the TST immediate.
979 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
980 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
981 __ Lsrs(temp_, temp_, LockWord::kReadBarrierStateShift + 1);
982 __ B(cc, GetExitLabel()); // Carry flag is the last bit shifted out by LSRS.
983 GenerateReadBarrierMarkRuntimeCall(codegen);
984
Roland Levillain47b3ab22017-02-27 14:31:35 +0000985 __ B(GetExitLabel());
986 }
987
988 private:
Roland Levillain54f869e2017-03-06 13:54:11 +0000989 // The register containing the object holding the marked object reference field.
990 vixl32::Register obj_;
991 // The offset, index and scale factor to access the reference in `obj_`.
992 uint32_t offset_;
993 Location index_;
994 ScaleFactor scale_factor_;
995 // Is a null check required?
996 bool needs_null_check_;
997 // A temporary register used to hold the lock word of `obj_`.
998 vixl32::Register temp_;
Roland Levillain47b3ab22017-02-27 14:31:35 +0000999
Roland Levillain54f869e2017-03-06 13:54:11 +00001000 DISALLOW_COPY_AND_ASSIGN(LoadReferenceWithBakerReadBarrierSlowPathARMVIXL);
Roland Levillain47b3ab22017-02-27 14:31:35 +00001001};
1002
Roland Levillain54f869e2017-03-06 13:54:11 +00001003// Slow path loading `obj`'s lock word, loading a reference from
1004// object `*(obj + offset + (index << scale_factor))` into `ref`, and
1005// marking `ref` if `obj` is gray according to the lock word (Baker
1006// read barrier). If needed, this slow path also atomically updates
1007// the field `obj.field` in the object `obj` holding this reference
1008// after marking (contrary to
1009// LoadReferenceWithBakerReadBarrierSlowPathARMVIXL above, which never
1010// tries to update `obj.field`).
Roland Levillain47b3ab22017-02-27 14:31:35 +00001011//
1012// This means that after the execution of this slow path, both `ref`
1013// and `obj.field` will be up-to-date; i.e., after the flip, both will
1014// hold the same to-space reference (unless another thread installed
1015// another object reference (different from `ref`) in `obj.field`).
Roland Levillainba650a42017-03-06 13:52:32 +00001016//
Roland Levillain54f869e2017-03-06 13:54:11 +00001017// Argument `entrypoint` must be a register location holding the read
Roland Levillain6d729a72017-06-30 18:34:01 +01001018// barrier marking runtime entry point to be invoked or an empty
1019// location; in the latter case, the read barrier marking runtime
1020// entry point will be loaded by the slow path code itself.
Roland Levillain54f869e2017-03-06 13:54:11 +00001021class LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARMVIXL
1022 : public ReadBarrierMarkSlowPathBaseARMVIXL {
Roland Levillain47b3ab22017-02-27 14:31:35 +00001023 public:
Roland Levillain6d729a72017-06-30 18:34:01 +01001024 LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARMVIXL(
1025 HInstruction* instruction,
1026 Location ref,
1027 vixl32::Register obj,
1028 uint32_t offset,
1029 Location index,
1030 ScaleFactor scale_factor,
1031 bool needs_null_check,
1032 vixl32::Register temp1,
1033 vixl32::Register temp2,
1034 Location entrypoint = Location::NoLocation())
Roland Levillain54f869e2017-03-06 13:54:11 +00001035 : ReadBarrierMarkSlowPathBaseARMVIXL(instruction, ref, entrypoint),
Roland Levillain47b3ab22017-02-27 14:31:35 +00001036 obj_(obj),
Roland Levillain54f869e2017-03-06 13:54:11 +00001037 offset_(offset),
1038 index_(index),
1039 scale_factor_(scale_factor),
1040 needs_null_check_(needs_null_check),
Roland Levillain47b3ab22017-02-27 14:31:35 +00001041 temp1_(temp1),
Roland Levillain54f869e2017-03-06 13:54:11 +00001042 temp2_(temp2) {
Roland Levillain47b3ab22017-02-27 14:31:35 +00001043 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain54f869e2017-03-06 13:54:11 +00001044 DCHECK(kUseBakerReadBarrier);
Roland Levillain47b3ab22017-02-27 14:31:35 +00001045 }
1046
1047 const char* GetDescription() const OVERRIDE {
Roland Levillain54f869e2017-03-06 13:54:11 +00001048 return "LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARMVIXL";
Roland Levillain47b3ab22017-02-27 14:31:35 +00001049 }
1050
1051 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
1052 LocationSummary* locations = instruction_->GetLocations();
1053 vixl32::Register ref_reg = RegisterFrom(ref_);
1054 DCHECK(locations->CanCall());
1055 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg.GetCode())) << ref_reg;
Roland Levillain54f869e2017-03-06 13:54:11 +00001056 DCHECK_NE(ref_.reg(), LocationFrom(temp1_).reg());
1057
1058 // This slow path is only used by the UnsafeCASObject intrinsic at the moment.
Roland Levillain47b3ab22017-02-27 14:31:35 +00001059 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
1060 << "Unexpected instruction in read barrier marking and field updating slow path: "
1061 << instruction_->DebugName();
1062 DCHECK(instruction_->GetLocations()->Intrinsified());
1063 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
Roland Levillain54f869e2017-03-06 13:54:11 +00001064 DCHECK_EQ(offset_, 0u);
1065 DCHECK_EQ(scale_factor_, ScaleFactor::TIMES_1);
1066 Location field_offset = index_;
1067 DCHECK(field_offset.IsRegisterPair()) << field_offset;
1068
1069 // Temporary register `temp1_`, used to store the lock word, must
1070 // not be IP, as we may use it to emit the reference load (in the
1071 // call to GenerateRawReferenceLoad below), and we need the lock
1072 // word to still be in `temp1_` after the reference load.
1073 DCHECK(!temp1_.Is(ip));
Roland Levillain47b3ab22017-02-27 14:31:35 +00001074
1075 __ Bind(GetEntryLabel());
1076
Roland Levillainff487002017-03-07 16:50:01 +00001077 // The implementation is similar to LoadReferenceWithBakerReadBarrierSlowPathARMVIXL's:
1078 //
1079 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
1080 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
1081 // HeapReference<mirror::Object> ref = *src; // Original reference load.
1082 // bool is_gray = (rb_state == ReadBarrier::GrayState());
1083 // if (is_gray) {
1084 // old_ref = ref;
1085 // ref = entrypoint(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
1086 // compareAndSwapObject(obj, field_offset, old_ref, ref);
1087 // }
1088
Roland Levillain54f869e2017-03-06 13:54:11 +00001089 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
1090
1091 // /* int32_t */ monitor = obj->monitor_
1092 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
1093 arm_codegen->GetAssembler()->LoadFromOffset(kLoadWord, temp1_, obj_, monitor_offset);
1094 if (needs_null_check_) {
1095 codegen->MaybeRecordImplicitNullCheck(instruction_);
1096 }
1097 // /* LockWord */ lock_word = LockWord(monitor)
1098 static_assert(sizeof(LockWord) == sizeof(int32_t),
1099 "art::LockWord and int32_t have different sizes.");
1100
1101 // Introduce a dependency on the lock_word including the rb_state,
1102 // which shall prevent load-load reordering without using
1103 // a memory barrier (which would be more expensive).
1104 // `obj` is unchanged by this operation, but its value now depends
1105 // on `temp`.
1106 __ Add(obj_, obj_, Operand(temp1_, ShiftType::LSR, 32));
1107
1108 // The actual reference load.
1109 // A possible implicit null check has already been handled above.
1110 arm_codegen->GenerateRawReferenceLoad(
1111 instruction_, ref_, obj_, offset_, index_, scale_factor_, /* needs_null_check */ false);
1112
1113 // Mark the object `ref` when `obj` is gray.
1114 //
1115 // if (rb_state == ReadBarrier::GrayState())
1116 // ref = ReadBarrier::Mark(ref);
1117 //
1118 // Given the numeric representation, it's enough to check the low bit of the
1119 // rb_state. We do that by shifting the bit out of the lock word with LSRS
1120 // which can be a 16-bit instruction unlike the TST immediate.
1121 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
1122 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
1123 __ Lsrs(temp1_, temp1_, LockWord::kReadBarrierStateShift + 1);
1124 __ B(cc, GetExitLabel()); // Carry flag is the last bit shifted out by LSRS.
1125
1126 // Save the old value of the reference before marking it.
Roland Levillain47b3ab22017-02-27 14:31:35 +00001127 // Note that we cannot use IP to save the old reference, as IP is
1128 // used internally by the ReadBarrierMarkRegX entry point, and we
1129 // need the old reference after the call to that entry point.
1130 DCHECK(!temp1_.Is(ip));
1131 __ Mov(temp1_, ref_reg);
Roland Levillain27b1f9c2017-01-17 16:56:34 +00001132
Roland Levillain54f869e2017-03-06 13:54:11 +00001133 GenerateReadBarrierMarkRuntimeCall(codegen);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001134
1135 // If the new reference is different from the old reference,
Roland Levillain54f869e2017-03-06 13:54:11 +00001136 // update the field in the holder (`*(obj_ + field_offset)`).
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001137 //
1138 // Note that this field could also hold a different object, if
1139 // another thread had concurrently changed it. In that case, the
1140 // LDREX/SUBS/ITNE sequence of instructions in the compare-and-set
1141 // (CAS) operation below would abort the CAS, leaving the field
1142 // as-is.
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001143 __ Cmp(temp1_, ref_reg);
Roland Levillain54f869e2017-03-06 13:54:11 +00001144 __ B(eq, GetExitLabel());
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001145
1146 // Update the the holder's field atomically. This may fail if
1147 // mutator updates before us, but it's OK. This is achieved
1148 // using a strong compare-and-set (CAS) operation with relaxed
1149 // memory synchronization ordering, where the expected value is
1150 // the old reference and the desired value is the new reference.
1151
1152 UseScratchRegisterScope temps(arm_codegen->GetVIXLAssembler());
1153 // Convenience aliases.
1154 vixl32::Register base = obj_;
1155 // The UnsafeCASObject intrinsic uses a register pair as field
1156 // offset ("long offset"), of which only the low part contains
1157 // data.
Roland Levillain54f869e2017-03-06 13:54:11 +00001158 vixl32::Register offset = LowRegisterFrom(field_offset);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001159 vixl32::Register expected = temp1_;
1160 vixl32::Register value = ref_reg;
1161 vixl32::Register tmp_ptr = temps.Acquire(); // Pointer to actual memory.
1162 vixl32::Register tmp = temp2_; // Value in memory.
1163
1164 __ Add(tmp_ptr, base, offset);
1165
1166 if (kPoisonHeapReferences) {
1167 arm_codegen->GetAssembler()->PoisonHeapReference(expected);
1168 if (value.Is(expected)) {
1169 // Do not poison `value`, as it is the same register as
1170 // `expected`, which has just been poisoned.
1171 } else {
1172 arm_codegen->GetAssembler()->PoisonHeapReference(value);
1173 }
1174 }
1175
1176 // do {
1177 // tmp = [r_ptr] - expected;
1178 // } while (tmp == 0 && failure([r_ptr] <- r_new_value));
1179
1180 vixl32::Label loop_head, exit_loop;
1181 __ Bind(&loop_head);
1182
1183 __ Ldrex(tmp, MemOperand(tmp_ptr));
1184
1185 __ Subs(tmp, tmp, expected);
1186
1187 {
Artem Serov0fb37192016-12-06 18:13:40 +00001188 ExactAssemblyScope aas(arm_codegen->GetVIXLAssembler(),
1189 2 * kMaxInstructionSizeInBytes,
1190 CodeBufferCheckScope::kMaximumSize);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001191
1192 __ it(ne);
1193 __ clrex(ne);
1194 }
1195
Artem Serov517d9f62016-12-12 15:51:15 +00001196 __ B(ne, &exit_loop, /* far_target */ false);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001197
1198 __ Strex(tmp, value, MemOperand(tmp_ptr));
1199 __ Cmp(tmp, 1);
Artem Serov517d9f62016-12-12 15:51:15 +00001200 __ B(eq, &loop_head, /* far_target */ false);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001201
1202 __ Bind(&exit_loop);
1203
1204 if (kPoisonHeapReferences) {
1205 arm_codegen->GetAssembler()->UnpoisonHeapReference(expected);
1206 if (value.Is(expected)) {
1207 // Do not unpoison `value`, as it is the same register as
1208 // `expected`, which has just been unpoisoned.
1209 } else {
1210 arm_codegen->GetAssembler()->UnpoisonHeapReference(value);
1211 }
1212 }
1213
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001214 __ B(GetExitLabel());
1215 }
1216
1217 private:
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001218 // The register containing the object holding the marked object reference field.
1219 const vixl32::Register obj_;
Roland Levillain54f869e2017-03-06 13:54:11 +00001220 // The offset, index and scale factor to access the reference in `obj_`.
1221 uint32_t offset_;
1222 Location index_;
1223 ScaleFactor scale_factor_;
1224 // Is a null check required?
1225 bool needs_null_check_;
1226 // A temporary register used to hold the lock word of `obj_`; and
1227 // also to hold the original reference value, when the reference is
1228 // marked.
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001229 const vixl32::Register temp1_;
Roland Levillain54f869e2017-03-06 13:54:11 +00001230 // A temporary register used in the implementation of the CAS, to
1231 // update the object's reference field.
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001232 const vixl32::Register temp2_;
1233
Roland Levillain54f869e2017-03-06 13:54:11 +00001234 DISALLOW_COPY_AND_ASSIGN(LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARMVIXL);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001235};
1236
1237// Slow path generating a read barrier for a heap reference.
1238class ReadBarrierForHeapReferenceSlowPathARMVIXL : public SlowPathCodeARMVIXL {
1239 public:
1240 ReadBarrierForHeapReferenceSlowPathARMVIXL(HInstruction* instruction,
1241 Location out,
1242 Location ref,
1243 Location obj,
1244 uint32_t offset,
1245 Location index)
1246 : SlowPathCodeARMVIXL(instruction),
1247 out_(out),
1248 ref_(ref),
1249 obj_(obj),
1250 offset_(offset),
1251 index_(index) {
1252 DCHECK(kEmitCompilerReadBarrier);
1253 // If `obj` is equal to `out` or `ref`, it means the initial object
1254 // has been overwritten by (or after) the heap object reference load
1255 // to be instrumented, e.g.:
1256 //
1257 // __ LoadFromOffset(kLoadWord, out, out, offset);
1258 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
1259 //
1260 // In that case, we have lost the information about the original
1261 // object, and the emitted read barrier cannot work properly.
1262 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
1263 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
1264 }
1265
1266 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
1267 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
1268 LocationSummary* locations = instruction_->GetLocations();
1269 vixl32::Register reg_out = RegisterFrom(out_);
1270 DCHECK(locations->CanCall());
1271 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out.GetCode()));
1272 DCHECK(instruction_->IsInstanceFieldGet() ||
1273 instruction_->IsStaticFieldGet() ||
1274 instruction_->IsArrayGet() ||
1275 instruction_->IsInstanceOf() ||
1276 instruction_->IsCheckCast() ||
Andreas Gamped9911ee2017-03-27 13:27:24 -07001277 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001278 << "Unexpected instruction in read barrier for heap reference slow path: "
1279 << instruction_->DebugName();
1280 // The read barrier instrumentation of object ArrayGet
1281 // instructions does not support the HIntermediateAddress
1282 // instruction.
1283 DCHECK(!(instruction_->IsArrayGet() &&
1284 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
1285
1286 __ Bind(GetEntryLabel());
1287 SaveLiveRegisters(codegen, locations);
1288
1289 // We may have to change the index's value, but as `index_` is a
1290 // constant member (like other "inputs" of this slow path),
1291 // introduce a copy of it, `index`.
1292 Location index = index_;
1293 if (index_.IsValid()) {
1294 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
1295 if (instruction_->IsArrayGet()) {
1296 // Compute the actual memory offset and store it in `index`.
1297 vixl32::Register index_reg = RegisterFrom(index_);
1298 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg.GetCode()));
1299 if (codegen->IsCoreCalleeSaveRegister(index_reg.GetCode())) {
1300 // We are about to change the value of `index_reg` (see the
Roland Levillain9983e302017-07-14 14:34:22 +01001301 // calls to art::arm::ArmVIXLMacroAssembler::Lsl and
1302 // art::arm::ArmVIXLMacroAssembler::Add below), but it has
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001303 // not been saved by the previous call to
1304 // art::SlowPathCode::SaveLiveRegisters, as it is a
1305 // callee-save register --
1306 // art::SlowPathCode::SaveLiveRegisters does not consider
1307 // callee-save registers, as it has been designed with the
1308 // assumption that callee-save registers are supposed to be
1309 // handled by the called function. So, as a callee-save
1310 // register, `index_reg` _would_ eventually be saved onto
1311 // the stack, but it would be too late: we would have
1312 // changed its value earlier. Therefore, we manually save
1313 // it here into another freely available register,
1314 // `free_reg`, chosen of course among the caller-save
1315 // registers (as a callee-save `free_reg` register would
1316 // exhibit the same problem).
1317 //
1318 // Note we could have requested a temporary register from
1319 // the register allocator instead; but we prefer not to, as
1320 // this is a slow path, and we know we can find a
1321 // caller-save register that is available.
1322 vixl32::Register free_reg = FindAvailableCallerSaveRegister(codegen);
1323 __ Mov(free_reg, index_reg);
1324 index_reg = free_reg;
1325 index = LocationFrom(index_reg);
1326 } else {
1327 // The initial register stored in `index_` has already been
1328 // saved in the call to art::SlowPathCode::SaveLiveRegisters
1329 // (as it is not a callee-save register), so we can freely
1330 // use it.
1331 }
1332 // Shifting the index value contained in `index_reg` by the scale
1333 // factor (2) cannot overflow in practice, as the runtime is
1334 // unable to allocate object arrays with a size larger than
1335 // 2^26 - 1 (that is, 2^28 - 4 bytes).
1336 __ Lsl(index_reg, index_reg, TIMES_4);
1337 static_assert(
1338 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
1339 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
1340 __ Add(index_reg, index_reg, offset_);
1341 } else {
1342 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
1343 // intrinsics, `index_` is not shifted by a scale factor of 2
1344 // (as in the case of ArrayGet), as it is actually an offset
1345 // to an object field within an object.
1346 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
1347 DCHECK(instruction_->GetLocations()->Intrinsified());
1348 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
1349 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
1350 << instruction_->AsInvoke()->GetIntrinsic();
1351 DCHECK_EQ(offset_, 0U);
1352 DCHECK(index_.IsRegisterPair());
1353 // UnsafeGet's offset location is a register pair, the low
1354 // part contains the correct offset.
1355 index = index_.ToLow();
1356 }
1357 }
1358
1359 // We're moving two or three locations to locations that could
1360 // overlap, so we need a parallel move resolver.
1361 InvokeRuntimeCallingConventionARMVIXL calling_convention;
1362 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
1363 parallel_move.AddMove(ref_,
1364 LocationFrom(calling_convention.GetRegisterAt(0)),
1365 Primitive::kPrimNot,
1366 nullptr);
1367 parallel_move.AddMove(obj_,
1368 LocationFrom(calling_convention.GetRegisterAt(1)),
1369 Primitive::kPrimNot,
1370 nullptr);
1371 if (index.IsValid()) {
1372 parallel_move.AddMove(index,
1373 LocationFrom(calling_convention.GetRegisterAt(2)),
1374 Primitive::kPrimInt,
1375 nullptr);
1376 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
1377 } else {
1378 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
1379 __ Mov(calling_convention.GetRegisterAt(2), offset_);
1380 }
1381 arm_codegen->InvokeRuntime(kQuickReadBarrierSlow, instruction_, instruction_->GetDexPc(), this);
1382 CheckEntrypointTypes<
1383 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
1384 arm_codegen->Move32(out_, LocationFrom(r0));
1385
1386 RestoreLiveRegisters(codegen, locations);
1387 __ B(GetExitLabel());
1388 }
1389
1390 const char* GetDescription() const OVERRIDE {
1391 return "ReadBarrierForHeapReferenceSlowPathARMVIXL";
1392 }
1393
1394 private:
1395 vixl32::Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
1396 uint32_t ref = RegisterFrom(ref_).GetCode();
1397 uint32_t obj = RegisterFrom(obj_).GetCode();
1398 for (uint32_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
1399 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
1400 return vixl32::Register(i);
1401 }
1402 }
1403 // We shall never fail to find a free caller-save register, as
1404 // there are more than two core caller-save registers on ARM
1405 // (meaning it is possible to find one which is different from
1406 // `ref` and `obj`).
1407 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
1408 LOG(FATAL) << "Could not find a free caller-save register";
1409 UNREACHABLE();
1410 }
1411
1412 const Location out_;
1413 const Location ref_;
1414 const Location obj_;
1415 const uint32_t offset_;
1416 // An additional location containing an index to an array.
1417 // Only used for HArrayGet and the UnsafeGetObject &
1418 // UnsafeGetObjectVolatile intrinsics.
1419 const Location index_;
1420
1421 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathARMVIXL);
1422};
1423
1424// Slow path generating a read barrier for a GC root.
1425class ReadBarrierForRootSlowPathARMVIXL : public SlowPathCodeARMVIXL {
1426 public:
1427 ReadBarrierForRootSlowPathARMVIXL(HInstruction* instruction, Location out, Location root)
1428 : SlowPathCodeARMVIXL(instruction), out_(out), root_(root) {
1429 DCHECK(kEmitCompilerReadBarrier);
1430 }
1431
1432 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
1433 LocationSummary* locations = instruction_->GetLocations();
1434 vixl32::Register reg_out = RegisterFrom(out_);
1435 DCHECK(locations->CanCall());
1436 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out.GetCode()));
1437 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
1438 << "Unexpected instruction in read barrier for GC root slow path: "
1439 << instruction_->DebugName();
1440
1441 __ Bind(GetEntryLabel());
1442 SaveLiveRegisters(codegen, locations);
1443
1444 InvokeRuntimeCallingConventionARMVIXL calling_convention;
1445 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
1446 arm_codegen->Move32(LocationFrom(calling_convention.GetRegisterAt(0)), root_);
1447 arm_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
1448 instruction_,
1449 instruction_->GetDexPc(),
1450 this);
1451 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
1452 arm_codegen->Move32(out_, LocationFrom(r0));
1453
1454 RestoreLiveRegisters(codegen, locations);
1455 __ B(GetExitLabel());
1456 }
1457
1458 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathARMVIXL"; }
1459
1460 private:
1461 const Location out_;
1462 const Location root_;
1463
1464 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathARMVIXL);
1465};
Scott Wakelingc34dba72016-10-03 10:14:44 +01001466
Scott Wakelingfe885462016-09-22 10:24:38 +01001467inline vixl32::Condition ARMCondition(IfCondition cond) {
1468 switch (cond) {
1469 case kCondEQ: return eq;
1470 case kCondNE: return ne;
1471 case kCondLT: return lt;
1472 case kCondLE: return le;
1473 case kCondGT: return gt;
1474 case kCondGE: return ge;
1475 case kCondB: return lo;
1476 case kCondBE: return ls;
1477 case kCondA: return hi;
1478 case kCondAE: return hs;
1479 }
1480 LOG(FATAL) << "Unreachable";
1481 UNREACHABLE();
1482}
1483
1484// Maps signed condition to unsigned condition.
1485inline vixl32::Condition ARMUnsignedCondition(IfCondition cond) {
1486 switch (cond) {
1487 case kCondEQ: return eq;
1488 case kCondNE: return ne;
1489 // Signed to unsigned.
1490 case kCondLT: return lo;
1491 case kCondLE: return ls;
1492 case kCondGT: return hi;
1493 case kCondGE: return hs;
1494 // Unsigned remain unchanged.
1495 case kCondB: return lo;
1496 case kCondBE: return ls;
1497 case kCondA: return hi;
1498 case kCondAE: return hs;
1499 }
1500 LOG(FATAL) << "Unreachable";
1501 UNREACHABLE();
1502}
1503
1504inline vixl32::Condition ARMFPCondition(IfCondition cond, bool gt_bias) {
1505 // The ARM condition codes can express all the necessary branches, see the
1506 // "Meaning (floating-point)" column in the table A8-1 of the ARMv7 reference manual.
1507 // There is no dex instruction or HIR that would need the missing conditions
1508 // "equal or unordered" or "not equal".
1509 switch (cond) {
1510 case kCondEQ: return eq;
1511 case kCondNE: return ne /* unordered */;
1512 case kCondLT: return gt_bias ? cc : lt /* unordered */;
1513 case kCondLE: return gt_bias ? ls : le /* unordered */;
1514 case kCondGT: return gt_bias ? hi /* unordered */ : gt;
1515 case kCondGE: return gt_bias ? cs /* unordered */ : ge;
1516 default:
1517 LOG(FATAL) << "UNREACHABLE";
1518 UNREACHABLE();
1519 }
1520}
1521
Anton Kirilov74234da2017-01-13 14:42:47 +00001522inline ShiftType ShiftFromOpKind(HDataProcWithShifterOp::OpKind op_kind) {
1523 switch (op_kind) {
1524 case HDataProcWithShifterOp::kASR: return ShiftType::ASR;
1525 case HDataProcWithShifterOp::kLSL: return ShiftType::LSL;
1526 case HDataProcWithShifterOp::kLSR: return ShiftType::LSR;
1527 default:
1528 LOG(FATAL) << "Unexpected op kind " << op_kind;
1529 UNREACHABLE();
1530 }
1531}
1532
Scott Wakelingfe885462016-09-22 10:24:38 +01001533void CodeGeneratorARMVIXL::DumpCoreRegister(std::ostream& stream, int reg) const {
1534 stream << vixl32::Register(reg);
1535}
1536
1537void CodeGeneratorARMVIXL::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
1538 stream << vixl32::SRegister(reg);
1539}
1540
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001541static uint32_t ComputeSRegisterListMask(const SRegisterList& regs) {
Scott Wakelingfe885462016-09-22 10:24:38 +01001542 uint32_t mask = 0;
1543 for (uint32_t i = regs.GetFirstSRegister().GetCode();
1544 i <= regs.GetLastSRegister().GetCode();
1545 ++i) {
1546 mask |= (1 << i);
1547 }
1548 return mask;
1549}
1550
Artem Serovd4cc5b22016-11-04 11:19:09 +00001551// Saves the register in the stack. Returns the size taken on stack.
1552size_t CodeGeneratorARMVIXL::SaveCoreRegister(size_t stack_index ATTRIBUTE_UNUSED,
1553 uint32_t reg_id ATTRIBUTE_UNUSED) {
1554 TODO_VIXL32(FATAL);
1555 return 0;
1556}
1557
1558// Restores the register from the stack. Returns the size taken on stack.
1559size_t CodeGeneratorARMVIXL::RestoreCoreRegister(size_t stack_index ATTRIBUTE_UNUSED,
1560 uint32_t reg_id ATTRIBUTE_UNUSED) {
1561 TODO_VIXL32(FATAL);
1562 return 0;
1563}
1564
1565size_t CodeGeneratorARMVIXL::SaveFloatingPointRegister(size_t stack_index ATTRIBUTE_UNUSED,
1566 uint32_t reg_id ATTRIBUTE_UNUSED) {
1567 TODO_VIXL32(FATAL);
1568 return 0;
1569}
1570
1571size_t CodeGeneratorARMVIXL::RestoreFloatingPointRegister(size_t stack_index ATTRIBUTE_UNUSED,
1572 uint32_t reg_id ATTRIBUTE_UNUSED) {
1573 TODO_VIXL32(FATAL);
1574 return 0;
Anton Kirilove28d9ae2016-10-25 18:17:23 +01001575}
1576
Anton Kirilov74234da2017-01-13 14:42:47 +00001577static void GenerateDataProcInstruction(HInstruction::InstructionKind kind,
1578 vixl32::Register out,
1579 vixl32::Register first,
1580 const Operand& second,
1581 CodeGeneratorARMVIXL* codegen) {
1582 if (second.IsImmediate() && second.GetImmediate() == 0) {
1583 const Operand in = kind == HInstruction::kAnd
1584 ? Operand(0)
1585 : Operand(first);
1586
1587 __ Mov(out, in);
1588 } else {
1589 switch (kind) {
1590 case HInstruction::kAdd:
1591 __ Add(out, first, second);
1592 break;
1593 case HInstruction::kAnd:
1594 __ And(out, first, second);
1595 break;
1596 case HInstruction::kOr:
1597 __ Orr(out, first, second);
1598 break;
1599 case HInstruction::kSub:
1600 __ Sub(out, first, second);
1601 break;
1602 case HInstruction::kXor:
1603 __ Eor(out, first, second);
1604 break;
1605 default:
1606 LOG(FATAL) << "Unexpected instruction kind: " << kind;
1607 UNREACHABLE();
1608 }
1609 }
1610}
1611
1612static void GenerateDataProc(HInstruction::InstructionKind kind,
1613 const Location& out,
1614 const Location& first,
1615 const Operand& second_lo,
1616 const Operand& second_hi,
1617 CodeGeneratorARMVIXL* codegen) {
1618 const vixl32::Register first_hi = HighRegisterFrom(first);
1619 const vixl32::Register first_lo = LowRegisterFrom(first);
1620 const vixl32::Register out_hi = HighRegisterFrom(out);
1621 const vixl32::Register out_lo = LowRegisterFrom(out);
1622
1623 if (kind == HInstruction::kAdd) {
1624 __ Adds(out_lo, first_lo, second_lo);
1625 __ Adc(out_hi, first_hi, second_hi);
1626 } else if (kind == HInstruction::kSub) {
1627 __ Subs(out_lo, first_lo, second_lo);
1628 __ Sbc(out_hi, first_hi, second_hi);
1629 } else {
1630 GenerateDataProcInstruction(kind, out_lo, first_lo, second_lo, codegen);
1631 GenerateDataProcInstruction(kind, out_hi, first_hi, second_hi, codegen);
1632 }
1633}
1634
1635static Operand GetShifterOperand(vixl32::Register rm, ShiftType shift, uint32_t shift_imm) {
1636 return shift_imm == 0 ? Operand(rm) : Operand(rm, shift, shift_imm);
1637}
1638
1639static void GenerateLongDataProc(HDataProcWithShifterOp* instruction,
1640 CodeGeneratorARMVIXL* codegen) {
1641 DCHECK_EQ(instruction->GetType(), Primitive::kPrimLong);
1642 DCHECK(HDataProcWithShifterOp::IsShiftOp(instruction->GetOpKind()));
1643
1644 const LocationSummary* const locations = instruction->GetLocations();
1645 const uint32_t shift_value = instruction->GetShiftAmount();
1646 const HInstruction::InstructionKind kind = instruction->GetInstrKind();
1647 const Location first = locations->InAt(0);
1648 const Location second = locations->InAt(1);
1649 const Location out = locations->Out();
1650 const vixl32::Register first_hi = HighRegisterFrom(first);
1651 const vixl32::Register first_lo = LowRegisterFrom(first);
1652 const vixl32::Register out_hi = HighRegisterFrom(out);
1653 const vixl32::Register out_lo = LowRegisterFrom(out);
1654 const vixl32::Register second_hi = HighRegisterFrom(second);
1655 const vixl32::Register second_lo = LowRegisterFrom(second);
1656 const ShiftType shift = ShiftFromOpKind(instruction->GetOpKind());
1657
1658 if (shift_value >= 32) {
1659 if (shift == ShiftType::LSL) {
1660 GenerateDataProcInstruction(kind,
1661 out_hi,
1662 first_hi,
1663 Operand(second_lo, ShiftType::LSL, shift_value - 32),
1664 codegen);
1665 GenerateDataProcInstruction(kind, out_lo, first_lo, 0, codegen);
1666 } else if (shift == ShiftType::ASR) {
1667 GenerateDataProc(kind,
1668 out,
1669 first,
1670 GetShifterOperand(second_hi, ShiftType::ASR, shift_value - 32),
1671 Operand(second_hi, ShiftType::ASR, 31),
1672 codegen);
1673 } else {
1674 DCHECK_EQ(shift, ShiftType::LSR);
1675 GenerateDataProc(kind,
1676 out,
1677 first,
1678 GetShifterOperand(second_hi, ShiftType::LSR, shift_value - 32),
1679 0,
1680 codegen);
1681 }
1682 } else {
1683 DCHECK_GT(shift_value, 1U);
1684 DCHECK_LT(shift_value, 32U);
1685
1686 UseScratchRegisterScope temps(codegen->GetVIXLAssembler());
1687
1688 if (shift == ShiftType::LSL) {
1689 // We are not doing this for HInstruction::kAdd because the output will require
1690 // Location::kOutputOverlap; not applicable to other cases.
1691 if (kind == HInstruction::kOr || kind == HInstruction::kXor) {
1692 GenerateDataProcInstruction(kind,
1693 out_hi,
1694 first_hi,
1695 Operand(second_hi, ShiftType::LSL, shift_value),
1696 codegen);
1697 GenerateDataProcInstruction(kind,
1698 out_hi,
1699 out_hi,
1700 Operand(second_lo, ShiftType::LSR, 32 - shift_value),
1701 codegen);
1702 GenerateDataProcInstruction(kind,
1703 out_lo,
1704 first_lo,
1705 Operand(second_lo, ShiftType::LSL, shift_value),
1706 codegen);
1707 } else {
1708 const vixl32::Register temp = temps.Acquire();
1709
1710 __ Lsl(temp, second_hi, shift_value);
1711 __ Orr(temp, temp, Operand(second_lo, ShiftType::LSR, 32 - shift_value));
1712 GenerateDataProc(kind,
1713 out,
1714 first,
1715 Operand(second_lo, ShiftType::LSL, shift_value),
1716 temp,
1717 codegen);
1718 }
1719 } else {
1720 DCHECK(shift == ShiftType::ASR || shift == ShiftType::LSR);
1721
1722 // We are not doing this for HInstruction::kAdd because the output will require
1723 // Location::kOutputOverlap; not applicable to other cases.
1724 if (kind == HInstruction::kOr || kind == HInstruction::kXor) {
1725 GenerateDataProcInstruction(kind,
1726 out_lo,
1727 first_lo,
1728 Operand(second_lo, ShiftType::LSR, shift_value),
1729 codegen);
1730 GenerateDataProcInstruction(kind,
1731 out_lo,
1732 out_lo,
1733 Operand(second_hi, ShiftType::LSL, 32 - shift_value),
1734 codegen);
1735 GenerateDataProcInstruction(kind,
1736 out_hi,
1737 first_hi,
1738 Operand(second_hi, shift, shift_value),
1739 codegen);
1740 } else {
1741 const vixl32::Register temp = temps.Acquire();
1742
1743 __ Lsr(temp, second_lo, shift_value);
1744 __ Orr(temp, temp, Operand(second_hi, ShiftType::LSL, 32 - shift_value));
1745 GenerateDataProc(kind,
1746 out,
1747 first,
1748 temp,
1749 Operand(second_hi, shift, shift_value),
1750 codegen);
1751 }
1752 }
1753 }
1754}
1755
Donghui Bai426b49c2016-11-08 14:55:38 +08001756static void GenerateVcmp(HInstruction* instruction, CodeGeneratorARMVIXL* codegen) {
1757 const Location rhs_loc = instruction->GetLocations()->InAt(1);
1758 if (rhs_loc.IsConstant()) {
1759 // 0.0 is the only immediate that can be encoded directly in
1760 // a VCMP instruction.
1761 //
1762 // Both the JLS (section 15.20.1) and the JVMS (section 6.5)
1763 // specify that in a floating-point comparison, positive zero
1764 // and negative zero are considered equal, so we can use the
1765 // literal 0.0 for both cases here.
1766 //
1767 // Note however that some methods (Float.equal, Float.compare,
1768 // Float.compareTo, Double.equal, Double.compare,
1769 // Double.compareTo, Math.max, Math.min, StrictMath.max,
1770 // StrictMath.min) consider 0.0 to be (strictly) greater than
1771 // -0.0. So if we ever translate calls to these methods into a
1772 // HCompare instruction, we must handle the -0.0 case with
1773 // care here.
1774 DCHECK(rhs_loc.GetConstant()->IsArithmeticZero());
1775
1776 const Primitive::Type type = instruction->InputAt(0)->GetType();
1777
1778 if (type == Primitive::kPrimFloat) {
1779 __ Vcmp(F32, InputSRegisterAt(instruction, 0), 0.0);
1780 } else {
1781 DCHECK_EQ(type, Primitive::kPrimDouble);
1782 __ Vcmp(F64, InputDRegisterAt(instruction, 0), 0.0);
1783 }
1784 } else {
1785 __ Vcmp(InputVRegisterAt(instruction, 0), InputVRegisterAt(instruction, 1));
1786 }
1787}
1788
Anton Kirilov5601d4e2017-05-11 19:33:50 +01001789static int64_t AdjustConstantForCondition(int64_t value,
1790 IfCondition* condition,
1791 IfCondition* opposite) {
1792 if (value == 1) {
1793 if (*condition == kCondB) {
1794 value = 0;
1795 *condition = kCondEQ;
1796 *opposite = kCondNE;
1797 } else if (*condition == kCondAE) {
1798 value = 0;
1799 *condition = kCondNE;
1800 *opposite = kCondEQ;
1801 }
1802 } else if (value == -1) {
1803 if (*condition == kCondGT) {
1804 value = 0;
1805 *condition = kCondGE;
1806 *opposite = kCondLT;
1807 } else if (*condition == kCondLE) {
1808 value = 0;
1809 *condition = kCondLT;
1810 *opposite = kCondGE;
1811 }
1812 }
1813
1814 return value;
1815}
1816
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001817static std::pair<vixl32::Condition, vixl32::Condition> GenerateLongTestConstant(
1818 HCondition* condition,
1819 bool invert,
1820 CodeGeneratorARMVIXL* codegen) {
Donghui Bai426b49c2016-11-08 14:55:38 +08001821 DCHECK_EQ(condition->GetLeft()->GetType(), Primitive::kPrimLong);
1822
1823 const LocationSummary* const locations = condition->GetLocations();
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001824 IfCondition cond = condition->GetCondition();
1825 IfCondition opposite = condition->GetOppositeCondition();
1826
1827 if (invert) {
1828 std::swap(cond, opposite);
1829 }
1830
1831 std::pair<vixl32::Condition, vixl32::Condition> ret(eq, ne);
Donghui Bai426b49c2016-11-08 14:55:38 +08001832 const Location left = locations->InAt(0);
1833 const Location right = locations->InAt(1);
1834
1835 DCHECK(right.IsConstant());
1836
1837 const vixl32::Register left_high = HighRegisterFrom(left);
1838 const vixl32::Register left_low = LowRegisterFrom(left);
Anton Kirilov5601d4e2017-05-11 19:33:50 +01001839 int64_t value = AdjustConstantForCondition(Int64ConstantFrom(right), &cond, &opposite);
1840 UseScratchRegisterScope temps(codegen->GetVIXLAssembler());
1841
1842 // Comparisons against 0 are common enough to deserve special attention.
1843 if (value == 0) {
1844 switch (cond) {
1845 case kCondNE:
1846 // x > 0 iff x != 0 when the comparison is unsigned.
1847 case kCondA:
1848 ret = std::make_pair(ne, eq);
1849 FALLTHROUGH_INTENDED;
1850 case kCondEQ:
1851 // x <= 0 iff x == 0 when the comparison is unsigned.
1852 case kCondBE:
1853 __ Orrs(temps.Acquire(), left_low, left_high);
1854 return ret;
1855 case kCondLT:
1856 case kCondGE:
1857 __ Cmp(left_high, 0);
1858 return std::make_pair(ARMCondition(cond), ARMCondition(opposite));
1859 // Trivially true or false.
1860 case kCondB:
1861 ret = std::make_pair(ne, eq);
1862 FALLTHROUGH_INTENDED;
1863 case kCondAE:
1864 __ Cmp(left_low, left_low);
1865 return ret;
1866 default:
1867 break;
1868 }
1869 }
Donghui Bai426b49c2016-11-08 14:55:38 +08001870
1871 switch (cond) {
1872 case kCondEQ:
1873 case kCondNE:
1874 case kCondB:
1875 case kCondBE:
1876 case kCondA:
1877 case kCondAE: {
Anton Kirilov23b752b2017-07-20 14:40:44 +01001878 const uint32_t value_low = Low32Bits(value);
1879 Operand operand_low(value_low);
1880
Donghui Bai426b49c2016-11-08 14:55:38 +08001881 __ Cmp(left_high, High32Bits(value));
1882
Anton Kirilov23b752b2017-07-20 14:40:44 +01001883 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
1884 // we must ensure that the operands corresponding to the least significant
1885 // halves of the inputs fit into a 16-bit CMP encoding.
1886 if (!left_low.IsLow() || !IsUint<8>(value_low)) {
1887 operand_low = Operand(temps.Acquire());
1888 __ Mov(LeaveFlags, operand_low.GetBaseRegister(), value_low);
1889 }
1890
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001891 // We use the scope because of the IT block that follows.
Donghui Bai426b49c2016-11-08 14:55:38 +08001892 ExactAssemblyScope guard(codegen->GetVIXLAssembler(),
1893 2 * vixl32::k16BitT32InstructionSizeInBytes,
1894 CodeBufferCheckScope::kExactSize);
1895
1896 __ it(eq);
Anton Kirilov23b752b2017-07-20 14:40:44 +01001897 __ cmp(eq, left_low, operand_low);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001898 ret = std::make_pair(ARMUnsignedCondition(cond), ARMUnsignedCondition(opposite));
Donghui Bai426b49c2016-11-08 14:55:38 +08001899 break;
1900 }
1901 case kCondLE:
1902 case kCondGT:
1903 // Trivially true or false.
1904 if (value == std::numeric_limits<int64_t>::max()) {
1905 __ Cmp(left_low, left_low);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001906 ret = cond == kCondLE ? std::make_pair(eq, ne) : std::make_pair(ne, eq);
Donghui Bai426b49c2016-11-08 14:55:38 +08001907 break;
1908 }
1909
1910 if (cond == kCondLE) {
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001911 DCHECK_EQ(opposite, kCondGT);
Donghui Bai426b49c2016-11-08 14:55:38 +08001912 cond = kCondLT;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001913 opposite = kCondGE;
Donghui Bai426b49c2016-11-08 14:55:38 +08001914 } else {
1915 DCHECK_EQ(cond, kCondGT);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001916 DCHECK_EQ(opposite, kCondLE);
Donghui Bai426b49c2016-11-08 14:55:38 +08001917 cond = kCondGE;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001918 opposite = kCondLT;
Donghui Bai426b49c2016-11-08 14:55:38 +08001919 }
1920
1921 value++;
1922 FALLTHROUGH_INTENDED;
1923 case kCondGE:
1924 case kCondLT: {
Donghui Bai426b49c2016-11-08 14:55:38 +08001925 __ Cmp(left_low, Low32Bits(value));
1926 __ Sbcs(temps.Acquire(), left_high, High32Bits(value));
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001927 ret = std::make_pair(ARMCondition(cond), ARMCondition(opposite));
Donghui Bai426b49c2016-11-08 14:55:38 +08001928 break;
1929 }
1930 default:
1931 LOG(FATAL) << "Unreachable";
1932 UNREACHABLE();
1933 }
1934
1935 return ret;
1936}
1937
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001938static std::pair<vixl32::Condition, vixl32::Condition> GenerateLongTest(
1939 HCondition* condition,
1940 bool invert,
1941 CodeGeneratorARMVIXL* codegen) {
Donghui Bai426b49c2016-11-08 14:55:38 +08001942 DCHECK_EQ(condition->GetLeft()->GetType(), Primitive::kPrimLong);
1943
1944 const LocationSummary* const locations = condition->GetLocations();
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001945 IfCondition cond = condition->GetCondition();
1946 IfCondition opposite = condition->GetOppositeCondition();
1947
1948 if (invert) {
1949 std::swap(cond, opposite);
1950 }
1951
1952 std::pair<vixl32::Condition, vixl32::Condition> ret(eq, ne);
Donghui Bai426b49c2016-11-08 14:55:38 +08001953 Location left = locations->InAt(0);
1954 Location right = locations->InAt(1);
1955
1956 DCHECK(right.IsRegisterPair());
1957
1958 switch (cond) {
1959 case kCondEQ:
1960 case kCondNE:
1961 case kCondB:
1962 case kCondBE:
1963 case kCondA:
1964 case kCondAE: {
1965 __ Cmp(HighRegisterFrom(left), HighRegisterFrom(right));
1966
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001967 // We use the scope because of the IT block that follows.
Donghui Bai426b49c2016-11-08 14:55:38 +08001968 ExactAssemblyScope guard(codegen->GetVIXLAssembler(),
1969 2 * vixl32::k16BitT32InstructionSizeInBytes,
1970 CodeBufferCheckScope::kExactSize);
1971
1972 __ it(eq);
1973 __ cmp(eq, LowRegisterFrom(left), LowRegisterFrom(right));
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001974 ret = std::make_pair(ARMUnsignedCondition(cond), ARMUnsignedCondition(opposite));
Donghui Bai426b49c2016-11-08 14:55:38 +08001975 break;
1976 }
1977 case kCondLE:
1978 case kCondGT:
1979 if (cond == kCondLE) {
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001980 DCHECK_EQ(opposite, kCondGT);
Donghui Bai426b49c2016-11-08 14:55:38 +08001981 cond = kCondGE;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001982 opposite = kCondLT;
Donghui Bai426b49c2016-11-08 14:55:38 +08001983 } else {
1984 DCHECK_EQ(cond, kCondGT);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001985 DCHECK_EQ(opposite, kCondLE);
Donghui Bai426b49c2016-11-08 14:55:38 +08001986 cond = kCondLT;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001987 opposite = kCondGE;
Donghui Bai426b49c2016-11-08 14:55:38 +08001988 }
1989
1990 std::swap(left, right);
1991 FALLTHROUGH_INTENDED;
1992 case kCondGE:
1993 case kCondLT: {
1994 UseScratchRegisterScope temps(codegen->GetVIXLAssembler());
1995
1996 __ Cmp(LowRegisterFrom(left), LowRegisterFrom(right));
1997 __ Sbcs(temps.Acquire(), HighRegisterFrom(left), HighRegisterFrom(right));
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001998 ret = std::make_pair(ARMCondition(cond), ARMCondition(opposite));
Donghui Bai426b49c2016-11-08 14:55:38 +08001999 break;
2000 }
2001 default:
2002 LOG(FATAL) << "Unreachable";
2003 UNREACHABLE();
2004 }
2005
2006 return ret;
2007}
2008
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002009static std::pair<vixl32::Condition, vixl32::Condition> GenerateTest(HCondition* condition,
2010 bool invert,
2011 CodeGeneratorARMVIXL* codegen) {
2012 const Primitive::Type type = condition->GetLeft()->GetType();
2013 IfCondition cond = condition->GetCondition();
2014 IfCondition opposite = condition->GetOppositeCondition();
2015 std::pair<vixl32::Condition, vixl32::Condition> ret(eq, ne);
Donghui Bai426b49c2016-11-08 14:55:38 +08002016
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002017 if (invert) {
2018 std::swap(cond, opposite);
2019 }
Donghui Bai426b49c2016-11-08 14:55:38 +08002020
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002021 if (type == Primitive::kPrimLong) {
2022 ret = condition->GetLocations()->InAt(1).IsConstant()
2023 ? GenerateLongTestConstant(condition, invert, codegen)
2024 : GenerateLongTest(condition, invert, codegen);
2025 } else if (Primitive::IsFloatingPointType(type)) {
2026 GenerateVcmp(condition, codegen);
2027 __ Vmrs(RegisterOrAPSR_nzcv(kPcCode), FPSCR);
2028 ret = std::make_pair(ARMFPCondition(cond, condition->IsGtBias()),
2029 ARMFPCondition(opposite, condition->IsGtBias()));
Donghui Bai426b49c2016-11-08 14:55:38 +08002030 } else {
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002031 DCHECK(Primitive::IsIntegralType(type) || type == Primitive::kPrimNot) << type;
2032 __ Cmp(InputRegisterAt(condition, 0), InputOperandAt(condition, 1));
2033 ret = std::make_pair(ARMCondition(cond), ARMCondition(opposite));
Donghui Bai426b49c2016-11-08 14:55:38 +08002034 }
2035
2036 return ret;
2037}
2038
Anton Kirilov5601d4e2017-05-11 19:33:50 +01002039static void GenerateConditionGeneric(HCondition* cond, CodeGeneratorARMVIXL* codegen) {
Anton Kirilov5601d4e2017-05-11 19:33:50 +01002040 const vixl32::Register out = OutputRegister(cond);
2041 const auto condition = GenerateTest(cond, false, codegen);
2042
2043 __ Mov(LeaveFlags, out, 0);
2044
2045 if (out.IsLow()) {
2046 // We use the scope because of the IT block that follows.
2047 ExactAssemblyScope guard(codegen->GetVIXLAssembler(),
2048 2 * vixl32::k16BitT32InstructionSizeInBytes,
2049 CodeBufferCheckScope::kExactSize);
2050
2051 __ it(condition.first);
2052 __ mov(condition.first, out, 1);
2053 } else {
2054 vixl32::Label done_label;
2055 vixl32::Label* const final_label = codegen->GetFinalLabel(cond, &done_label);
2056
2057 __ B(condition.second, final_label, /* far_target */ false);
2058 __ Mov(out, 1);
2059
2060 if (done_label.IsReferenced()) {
2061 __ Bind(&done_label);
2062 }
2063 }
2064}
2065
2066static void GenerateEqualLong(HCondition* cond, CodeGeneratorARMVIXL* codegen) {
2067 DCHECK_EQ(cond->GetLeft()->GetType(), Primitive::kPrimLong);
2068
2069 const LocationSummary* const locations = cond->GetLocations();
2070 IfCondition condition = cond->GetCondition();
2071 const vixl32::Register out = OutputRegister(cond);
2072 const Location left = locations->InAt(0);
2073 const Location right = locations->InAt(1);
2074 vixl32::Register left_high = HighRegisterFrom(left);
2075 vixl32::Register left_low = LowRegisterFrom(left);
2076 vixl32::Register temp;
2077 UseScratchRegisterScope temps(codegen->GetVIXLAssembler());
2078
2079 if (right.IsConstant()) {
2080 IfCondition opposite = cond->GetOppositeCondition();
2081 const int64_t value = AdjustConstantForCondition(Int64ConstantFrom(right),
2082 &condition,
2083 &opposite);
2084 Operand right_high = High32Bits(value);
2085 Operand right_low = Low32Bits(value);
2086
2087 // The output uses Location::kNoOutputOverlap.
2088 if (out.Is(left_high)) {
2089 std::swap(left_low, left_high);
2090 std::swap(right_low, right_high);
2091 }
2092
2093 __ Sub(out, left_low, right_low);
2094 temp = temps.Acquire();
2095 __ Sub(temp, left_high, right_high);
2096 } else {
2097 DCHECK(right.IsRegisterPair());
2098 temp = temps.Acquire();
2099 __ Sub(temp, left_high, HighRegisterFrom(right));
2100 __ Sub(out, left_low, LowRegisterFrom(right));
2101 }
2102
2103 // Need to check after calling AdjustConstantForCondition().
2104 DCHECK(condition == kCondEQ || condition == kCondNE) << condition;
2105
2106 if (condition == kCondNE && out.IsLow()) {
2107 __ Orrs(out, out, temp);
2108
2109 // We use the scope because of the IT block that follows.
2110 ExactAssemblyScope guard(codegen->GetVIXLAssembler(),
2111 2 * vixl32::k16BitT32InstructionSizeInBytes,
2112 CodeBufferCheckScope::kExactSize);
2113
2114 __ it(ne);
2115 __ mov(ne, out, 1);
2116 } else {
2117 __ Orr(out, out, temp);
2118 codegen->GenerateConditionWithZero(condition, out, out, temp);
2119 }
2120}
2121
Anton Kirilov5601d4e2017-05-11 19:33:50 +01002122static void GenerateConditionLong(HCondition* cond, CodeGeneratorARMVIXL* codegen) {
2123 DCHECK_EQ(cond->GetLeft()->GetType(), Primitive::kPrimLong);
2124
2125 const LocationSummary* const locations = cond->GetLocations();
2126 IfCondition condition = cond->GetCondition();
2127 const vixl32::Register out = OutputRegister(cond);
2128 const Location left = locations->InAt(0);
2129 const Location right = locations->InAt(1);
2130
2131 if (right.IsConstant()) {
2132 IfCondition opposite = cond->GetOppositeCondition();
2133
2134 // Comparisons against 0 are common enough to deserve special attention.
2135 if (AdjustConstantForCondition(Int64ConstantFrom(right), &condition, &opposite) == 0) {
2136 switch (condition) {
2137 case kCondNE:
2138 case kCondA:
2139 if (out.IsLow()) {
2140 // We only care if both input registers are 0 or not.
2141 __ Orrs(out, LowRegisterFrom(left), HighRegisterFrom(left));
2142
2143 // We use the scope because of the IT block that follows.
2144 ExactAssemblyScope guard(codegen->GetVIXLAssembler(),
2145 2 * vixl32::k16BitT32InstructionSizeInBytes,
2146 CodeBufferCheckScope::kExactSize);
2147
2148 __ it(ne);
2149 __ mov(ne, out, 1);
2150 return;
2151 }
2152
2153 FALLTHROUGH_INTENDED;
2154 case kCondEQ:
2155 case kCondBE:
2156 // We only care if both input registers are 0 or not.
2157 __ Orr(out, LowRegisterFrom(left), HighRegisterFrom(left));
2158 codegen->GenerateConditionWithZero(condition, out, out);
2159 return;
2160 case kCondLT:
2161 case kCondGE:
2162 // We only care about the sign bit.
2163 FALLTHROUGH_INTENDED;
2164 case kCondAE:
2165 case kCondB:
2166 codegen->GenerateConditionWithZero(condition, out, HighRegisterFrom(left));
2167 return;
2168 case kCondLE:
2169 case kCondGT:
2170 default:
2171 break;
2172 }
2173 }
2174 }
2175
Anton Kirilov23b752b2017-07-20 14:40:44 +01002176 // If `out` is a low register, then the GenerateConditionGeneric()
2177 // function generates a shorter code sequence that is still branchless.
2178 if ((condition == kCondEQ || condition == kCondNE) && !out.IsLow()) {
Anton Kirilov5601d4e2017-05-11 19:33:50 +01002179 GenerateEqualLong(cond, codegen);
2180 return;
2181 }
2182
Anton Kirilov23b752b2017-07-20 14:40:44 +01002183 GenerateConditionGeneric(cond, codegen);
Anton Kirilov5601d4e2017-05-11 19:33:50 +01002184}
2185
Roland Levillain6d729a72017-06-30 18:34:01 +01002186static void GenerateConditionIntegralOrNonPrimitive(HCondition* cond,
2187 CodeGeneratorARMVIXL* codegen) {
Anton Kirilov5601d4e2017-05-11 19:33:50 +01002188 const Primitive::Type type = cond->GetLeft()->GetType();
2189
2190 DCHECK(Primitive::IsIntegralType(type) || type == Primitive::kPrimNot) << type;
2191
2192 if (type == Primitive::kPrimLong) {
2193 GenerateConditionLong(cond, codegen);
2194 return;
2195 }
2196
2197 IfCondition condition = cond->GetCondition();
2198 vixl32::Register in = InputRegisterAt(cond, 0);
2199 const vixl32::Register out = OutputRegister(cond);
2200 const Location right = cond->GetLocations()->InAt(1);
2201 int64_t value;
2202
2203 if (right.IsConstant()) {
2204 IfCondition opposite = cond->GetOppositeCondition();
2205
2206 value = AdjustConstantForCondition(Int64ConstantFrom(right), &condition, &opposite);
2207
2208 // Comparisons against 0 are common enough to deserve special attention.
2209 if (value == 0) {
2210 switch (condition) {
2211 case kCondNE:
2212 case kCondA:
2213 if (out.IsLow() && out.Is(in)) {
2214 __ Cmp(out, 0);
2215
2216 // We use the scope because of the IT block that follows.
2217 ExactAssemblyScope guard(codegen->GetVIXLAssembler(),
2218 2 * vixl32::k16BitT32InstructionSizeInBytes,
2219 CodeBufferCheckScope::kExactSize);
2220
2221 __ it(ne);
2222 __ mov(ne, out, 1);
2223 return;
2224 }
2225
2226 FALLTHROUGH_INTENDED;
2227 case kCondEQ:
2228 case kCondBE:
2229 case kCondLT:
2230 case kCondGE:
2231 case kCondAE:
2232 case kCondB:
2233 codegen->GenerateConditionWithZero(condition, out, in);
2234 return;
2235 case kCondLE:
2236 case kCondGT:
2237 default:
2238 break;
2239 }
2240 }
2241 }
2242
2243 if (condition == kCondEQ || condition == kCondNE) {
2244 Operand operand(0);
2245
2246 if (right.IsConstant()) {
2247 operand = Operand::From(value);
2248 } else if (out.Is(RegisterFrom(right))) {
2249 // Avoid 32-bit instructions if possible.
2250 operand = InputOperandAt(cond, 0);
2251 in = RegisterFrom(right);
2252 } else {
2253 operand = InputOperandAt(cond, 1);
2254 }
2255
2256 if (condition == kCondNE && out.IsLow()) {
2257 __ Subs(out, in, operand);
2258
2259 // We use the scope because of the IT block that follows.
2260 ExactAssemblyScope guard(codegen->GetVIXLAssembler(),
2261 2 * vixl32::k16BitT32InstructionSizeInBytes,
2262 CodeBufferCheckScope::kExactSize);
2263
2264 __ it(ne);
2265 __ mov(ne, out, 1);
2266 } else {
2267 __ Sub(out, in, operand);
2268 codegen->GenerateConditionWithZero(condition, out, out);
2269 }
2270
2271 return;
2272 }
2273
2274 GenerateConditionGeneric(cond, codegen);
2275}
2276
Donghui Bai426b49c2016-11-08 14:55:38 +08002277static bool CanEncodeConstantAs8BitImmediate(HConstant* constant) {
2278 const Primitive::Type type = constant->GetType();
2279 bool ret = false;
2280
2281 DCHECK(Primitive::IsIntegralType(type) || type == Primitive::kPrimNot) << type;
2282
2283 if (type == Primitive::kPrimLong) {
2284 const uint64_t value = Uint64ConstantFrom(constant);
2285
2286 ret = IsUint<8>(Low32Bits(value)) && IsUint<8>(High32Bits(value));
2287 } else {
2288 ret = IsUint<8>(Int32ConstantFrom(constant));
2289 }
2290
2291 return ret;
2292}
2293
2294static Location Arm8BitEncodableConstantOrRegister(HInstruction* constant) {
2295 DCHECK(!Primitive::IsFloatingPointType(constant->GetType()));
2296
2297 if (constant->IsConstant() && CanEncodeConstantAs8BitImmediate(constant->AsConstant())) {
2298 return Location::ConstantLocation(constant->AsConstant());
2299 }
2300
2301 return Location::RequiresRegister();
2302}
2303
2304static bool CanGenerateConditionalMove(const Location& out, const Location& src) {
2305 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
2306 // we check that we are not dealing with floating-point output (there is no
2307 // 16-bit VMOV encoding).
2308 if (!out.IsRegister() && !out.IsRegisterPair()) {
2309 return false;
2310 }
2311
2312 // For constants, we also check that the output is in one or two low registers,
2313 // and that the constants fit in an 8-bit unsigned integer, so that a 16-bit
2314 // MOV encoding can be used.
2315 if (src.IsConstant()) {
2316 if (!CanEncodeConstantAs8BitImmediate(src.GetConstant())) {
2317 return false;
2318 }
2319
2320 if (out.IsRegister()) {
2321 if (!RegisterFrom(out).IsLow()) {
2322 return false;
2323 }
2324 } else {
2325 DCHECK(out.IsRegisterPair());
2326
2327 if (!HighRegisterFrom(out).IsLow()) {
2328 return false;
2329 }
2330 }
2331 }
2332
2333 return true;
2334}
2335
Scott Wakelingfe885462016-09-22 10:24:38 +01002336#undef __
2337
Donghui Bai426b49c2016-11-08 14:55:38 +08002338vixl32::Label* CodeGeneratorARMVIXL::GetFinalLabel(HInstruction* instruction,
2339 vixl32::Label* final_label) {
2340 DCHECK(!instruction->IsControlFlow() && !instruction->IsSuspendCheck());
Anton Kirilov6f644202017-02-27 18:29:45 +00002341 DCHECK(!instruction->IsInvoke() || !instruction->GetLocations()->CanCall());
Donghui Bai426b49c2016-11-08 14:55:38 +08002342
2343 const HBasicBlock* const block = instruction->GetBlock();
2344 const HLoopInformation* const info = block->GetLoopInformation();
2345 HInstruction* const next = instruction->GetNext();
2346
2347 // Avoid a branch to a branch.
2348 if (next->IsGoto() && (info == nullptr ||
2349 !info->IsBackEdge(*block) ||
2350 !info->HasSuspendCheck())) {
2351 final_label = GetLabelOf(next->AsGoto()->GetSuccessor());
2352 }
2353
2354 return final_label;
2355}
2356
Scott Wakelingfe885462016-09-22 10:24:38 +01002357CodeGeneratorARMVIXL::CodeGeneratorARMVIXL(HGraph* graph,
2358 const ArmInstructionSetFeatures& isa_features,
2359 const CompilerOptions& compiler_options,
2360 OptimizingCompilerStats* stats)
2361 : CodeGenerator(graph,
2362 kNumberOfCoreRegisters,
2363 kNumberOfSRegisters,
2364 kNumberOfRegisterPairs,
2365 kCoreCalleeSaves.GetList(),
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002366 ComputeSRegisterListMask(kFpuCalleeSaves),
Scott Wakelingfe885462016-09-22 10:24:38 +01002367 compiler_options,
2368 stats),
2369 block_labels_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Artem Serov551b28f2016-10-18 19:11:30 +01002370 jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Scott Wakelingfe885462016-09-22 10:24:38 +01002371 location_builder_(graph, this),
2372 instruction_visitor_(graph, this),
2373 move_resolver_(graph->GetArena(), this),
2374 assembler_(graph->GetArena()),
Artem Serovd4cc5b22016-11-04 11:19:09 +00002375 isa_features_(isa_features),
Artem Serovc5fcb442016-12-02 19:19:58 +00002376 uint32_literals_(std::less<uint32_t>(),
2377 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko65979462017-05-19 17:25:12 +01002378 pc_relative_method_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko0eb882b2017-05-15 13:39:18 +01002379 method_bss_entry_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Artem Serovc5fcb442016-12-02 19:19:58 +00002380 pc_relative_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko1998cd02017-01-13 13:02:58 +00002381 type_bss_entry_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko65979462017-05-19 17:25:12 +01002382 pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01002383 string_bss_entry_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01002384 baker_read_barrier_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Artem Serovc5fcb442016-12-02 19:19:58 +00002385 jit_string_patches_(StringReferenceValueComparator(),
2386 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
2387 jit_class_patches_(TypeReferenceValueComparator(),
2388 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Scott Wakelingfe885462016-09-22 10:24:38 +01002389 // Always save the LR register to mimic Quick.
2390 AddAllocatedRegister(Location::RegisterLocation(LR));
Nicolas Geoffray13a797b2017-03-15 16:41:31 +00002391 // Give D30 and D31 as scratch register to VIXL. The register allocator only works on
2392 // S0-S31, which alias to D0-D15.
2393 GetVIXLAssembler()->GetScratchVRegisterList()->Combine(d31);
2394 GetVIXLAssembler()->GetScratchVRegisterList()->Combine(d30);
Scott Wakelingfe885462016-09-22 10:24:38 +01002395}
2396
Artem Serov551b28f2016-10-18 19:11:30 +01002397void JumpTableARMVIXL::EmitTable(CodeGeneratorARMVIXL* codegen) {
2398 uint32_t num_entries = switch_instr_->GetNumEntries();
2399 DCHECK_GE(num_entries, kPackedSwitchCompareJumpThreshold);
2400
2401 // We are about to use the assembler to place literals directly. Make sure we have enough
Scott Wakelingb77051e2016-11-21 19:46:00 +00002402 // underlying code buffer and we have generated a jump table of the right size, using
2403 // codegen->GetVIXLAssembler()->GetBuffer().Align();
Artem Serov0fb37192016-12-06 18:13:40 +00002404 ExactAssemblyScope aas(codegen->GetVIXLAssembler(),
2405 num_entries * sizeof(int32_t),
2406 CodeBufferCheckScope::kMaximumSize);
Artem Serov551b28f2016-10-18 19:11:30 +01002407 // TODO(VIXL): Check that using lower case bind is fine here.
2408 codegen->GetVIXLAssembler()->bind(&table_start_);
Artem Serov09a940d2016-11-11 16:15:11 +00002409 for (uint32_t i = 0; i < num_entries; i++) {
2410 codegen->GetVIXLAssembler()->place(bb_addresses_[i].get());
2411 }
2412}
2413
2414void JumpTableARMVIXL::FixTable(CodeGeneratorARMVIXL* codegen) {
2415 uint32_t num_entries = switch_instr_->GetNumEntries();
2416 DCHECK_GE(num_entries, kPackedSwitchCompareJumpThreshold);
2417
Artem Serov551b28f2016-10-18 19:11:30 +01002418 const ArenaVector<HBasicBlock*>& successors = switch_instr_->GetBlock()->GetSuccessors();
2419 for (uint32_t i = 0; i < num_entries; i++) {
2420 vixl32::Label* target_label = codegen->GetLabelOf(successors[i]);
2421 DCHECK(target_label->IsBound());
2422 int32_t jump_offset = target_label->GetLocation() - table_start_.GetLocation();
2423 // When doing BX to address we need to have lower bit set to 1 in T32.
2424 if (codegen->GetVIXLAssembler()->IsUsingT32()) {
2425 jump_offset++;
2426 }
2427 DCHECK_GT(jump_offset, std::numeric_limits<int32_t>::min());
2428 DCHECK_LE(jump_offset, std::numeric_limits<int32_t>::max());
Artem Serov09a940d2016-11-11 16:15:11 +00002429
Scott Wakelingb77051e2016-11-21 19:46:00 +00002430 bb_addresses_[i].get()->UpdateValue(jump_offset, codegen->GetVIXLAssembler()->GetBuffer());
Artem Serov551b28f2016-10-18 19:11:30 +01002431 }
2432}
2433
Artem Serov09a940d2016-11-11 16:15:11 +00002434void CodeGeneratorARMVIXL::FixJumpTables() {
Artem Serov551b28f2016-10-18 19:11:30 +01002435 for (auto&& jump_table : jump_tables_) {
Artem Serov09a940d2016-11-11 16:15:11 +00002436 jump_table->FixTable(this);
Artem Serov551b28f2016-10-18 19:11:30 +01002437 }
2438}
2439
Andreas Gampeca620d72016-11-08 08:09:33 -08002440#define __ reinterpret_cast<ArmVIXLAssembler*>(GetAssembler())->GetVIXLAssembler()-> // NOLINT
Scott Wakelingfe885462016-09-22 10:24:38 +01002441
2442void CodeGeneratorARMVIXL::Finalize(CodeAllocator* allocator) {
Artem Serov09a940d2016-11-11 16:15:11 +00002443 FixJumpTables();
Scott Wakelingfe885462016-09-22 10:24:38 +01002444 GetAssembler()->FinalizeCode();
2445 CodeGenerator::Finalize(allocator);
2446}
2447
2448void CodeGeneratorARMVIXL::SetupBlockedRegisters() const {
Scott Wakelingfe885462016-09-22 10:24:38 +01002449 // Stack register, LR and PC are always reserved.
2450 blocked_core_registers_[SP] = true;
2451 blocked_core_registers_[LR] = true;
2452 blocked_core_registers_[PC] = true;
2453
Roland Levillain6d729a72017-06-30 18:34:01 +01002454 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2455 // Reserve marking register.
2456 blocked_core_registers_[MR] = true;
2457 }
2458
Scott Wakelingfe885462016-09-22 10:24:38 +01002459 // Reserve thread register.
2460 blocked_core_registers_[TR] = true;
2461
2462 // Reserve temp register.
2463 blocked_core_registers_[IP] = true;
2464
2465 if (GetGraph()->IsDebuggable()) {
2466 // Stubs do not save callee-save floating point registers. If the graph
2467 // is debuggable, we need to deal with these registers differently. For
2468 // now, just block them.
2469 for (uint32_t i = kFpuCalleeSaves.GetFirstSRegister().GetCode();
2470 i <= kFpuCalleeSaves.GetLastSRegister().GetCode();
2471 ++i) {
2472 blocked_fpu_registers_[i] = true;
2473 }
2474 }
Scott Wakelingfe885462016-09-22 10:24:38 +01002475}
2476
Scott Wakelingfe885462016-09-22 10:24:38 +01002477InstructionCodeGeneratorARMVIXL::InstructionCodeGeneratorARMVIXL(HGraph* graph,
2478 CodeGeneratorARMVIXL* codegen)
2479 : InstructionCodeGenerator(graph, codegen),
2480 assembler_(codegen->GetAssembler()),
2481 codegen_(codegen) {}
2482
2483void CodeGeneratorARMVIXL::ComputeSpillMask() {
2484 core_spill_mask_ = allocated_registers_.GetCoreRegisters() & core_callee_save_mask_;
2485 DCHECK_NE(core_spill_mask_, 0u) << "At least the return address register must be saved";
2486 // There is no easy instruction to restore just the PC on thumb2. We spill and
2487 // restore another arbitrary register.
2488 core_spill_mask_ |= (1 << kCoreAlwaysSpillRegister.GetCode());
2489 fpu_spill_mask_ = allocated_registers_.GetFloatingPointRegisters() & fpu_callee_save_mask_;
2490 // We use vpush and vpop for saving and restoring floating point registers, which take
2491 // a SRegister and the number of registers to save/restore after that SRegister. We
2492 // therefore update the `fpu_spill_mask_` to also contain those registers not allocated,
2493 // but in the range.
2494 if (fpu_spill_mask_ != 0) {
2495 uint32_t least_significant_bit = LeastSignificantBit(fpu_spill_mask_);
2496 uint32_t most_significant_bit = MostSignificantBit(fpu_spill_mask_);
2497 for (uint32_t i = least_significant_bit + 1 ; i < most_significant_bit; ++i) {
2498 fpu_spill_mask_ |= (1 << i);
2499 }
2500 }
2501}
2502
2503void CodeGeneratorARMVIXL::GenerateFrameEntry() {
2504 bool skip_overflow_check =
2505 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kArm);
2506 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
2507 __ Bind(&frame_entry_label_);
2508
2509 if (HasEmptyFrame()) {
2510 return;
2511 }
2512
Scott Wakelingfe885462016-09-22 10:24:38 +01002513 if (!skip_overflow_check) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002514 UseScratchRegisterScope temps(GetVIXLAssembler());
2515 vixl32::Register temp = temps.Acquire();
Anton Kirilov644032c2016-12-06 17:51:43 +00002516 __ Sub(temp, sp, Operand::From(GetStackOverflowReservedBytes(kArm)));
Scott Wakelingfe885462016-09-22 10:24:38 +01002517 // The load must immediately precede RecordPcInfo.
Artem Serov0fb37192016-12-06 18:13:40 +00002518 ExactAssemblyScope aas(GetVIXLAssembler(),
2519 vixl32::kMaxInstructionSizeInBytes,
2520 CodeBufferCheckScope::kMaximumSize);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002521 __ ldr(temp, MemOperand(temp));
2522 RecordPcInfo(nullptr, 0);
Scott Wakelingfe885462016-09-22 10:24:38 +01002523 }
2524
2525 __ Push(RegisterList(core_spill_mask_));
2526 GetAssembler()->cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(core_spill_mask_));
2527 GetAssembler()->cfi().RelOffsetForMany(DWARFReg(kMethodRegister),
2528 0,
2529 core_spill_mask_,
2530 kArmWordSize);
2531 if (fpu_spill_mask_ != 0) {
2532 uint32_t first = LeastSignificantBit(fpu_spill_mask_);
2533
2534 // Check that list is contiguous.
2535 DCHECK_EQ(fpu_spill_mask_ >> CTZ(fpu_spill_mask_), ~0u >> (32 - POPCOUNT(fpu_spill_mask_)));
2536
2537 __ Vpush(SRegisterList(vixl32::SRegister(first), POPCOUNT(fpu_spill_mask_)));
2538 GetAssembler()->cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(fpu_spill_mask_));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002539 GetAssembler()->cfi().RelOffsetForMany(DWARFReg(s0), 0, fpu_spill_mask_, kArmWordSize);
Scott Wakelingfe885462016-09-22 10:24:38 +01002540 }
Scott Wakelingbffdc702016-12-07 17:46:03 +00002541
Scott Wakelingfe885462016-09-22 10:24:38 +01002542 int adjust = GetFrameSize() - FrameEntrySpillSize();
2543 __ Sub(sp, sp, adjust);
2544 GetAssembler()->cfi().AdjustCFAOffset(adjust);
Scott Wakelingbffdc702016-12-07 17:46:03 +00002545
2546 // Save the current method if we need it. Note that we do not
2547 // do this in HCurrentMethod, as the instruction might have been removed
2548 // in the SSA graph.
2549 if (RequiresCurrentMethod()) {
2550 GetAssembler()->StoreToOffset(kStoreWord, kMethodRegister, sp, 0);
2551 }
Nicolas Geoffrayf7893532017-06-15 12:34:36 +01002552
2553 if (GetGraph()->HasShouldDeoptimizeFlag()) {
2554 UseScratchRegisterScope temps(GetVIXLAssembler());
2555 vixl32::Register temp = temps.Acquire();
2556 // Initialize should_deoptimize flag to 0.
2557 __ Mov(temp, 0);
2558 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, GetStackOffsetOfShouldDeoptimizeFlag());
2559 }
Roland Levillain5daa4952017-07-03 17:23:56 +01002560
2561 MaybeGenerateMarkingRegisterCheck(/* code */ 1);
Scott Wakelingfe885462016-09-22 10:24:38 +01002562}
2563
2564void CodeGeneratorARMVIXL::GenerateFrameExit() {
2565 if (HasEmptyFrame()) {
2566 __ Bx(lr);
2567 return;
2568 }
2569 GetAssembler()->cfi().RememberState();
2570 int adjust = GetFrameSize() - FrameEntrySpillSize();
2571 __ Add(sp, sp, adjust);
2572 GetAssembler()->cfi().AdjustCFAOffset(-adjust);
2573 if (fpu_spill_mask_ != 0) {
2574 uint32_t first = LeastSignificantBit(fpu_spill_mask_);
2575
2576 // Check that list is contiguous.
2577 DCHECK_EQ(fpu_spill_mask_ >> CTZ(fpu_spill_mask_), ~0u >> (32 - POPCOUNT(fpu_spill_mask_)));
2578
2579 __ Vpop(SRegisterList(vixl32::SRegister(first), POPCOUNT(fpu_spill_mask_)));
2580 GetAssembler()->cfi().AdjustCFAOffset(
2581 -static_cast<int>(kArmWordSize) * POPCOUNT(fpu_spill_mask_));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002582 GetAssembler()->cfi().RestoreMany(DWARFReg(vixl32::SRegister(0)), fpu_spill_mask_);
Scott Wakelingfe885462016-09-22 10:24:38 +01002583 }
2584 // Pop LR into PC to return.
2585 DCHECK_NE(core_spill_mask_ & (1 << kLrCode), 0U);
2586 uint32_t pop_mask = (core_spill_mask_ & (~(1 << kLrCode))) | 1 << kPcCode;
2587 __ Pop(RegisterList(pop_mask));
2588 GetAssembler()->cfi().RestoreState();
2589 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
2590}
2591
2592void CodeGeneratorARMVIXL::Bind(HBasicBlock* block) {
2593 __ Bind(GetLabelOf(block));
2594}
2595
Artem Serovd4cc5b22016-11-04 11:19:09 +00002596Location InvokeDexCallingConventionVisitorARMVIXL::GetNextLocation(Primitive::Type type) {
2597 switch (type) {
2598 case Primitive::kPrimBoolean:
2599 case Primitive::kPrimByte:
2600 case Primitive::kPrimChar:
2601 case Primitive::kPrimShort:
2602 case Primitive::kPrimInt:
2603 case Primitive::kPrimNot: {
2604 uint32_t index = gp_index_++;
2605 uint32_t stack_index = stack_index_++;
2606 if (index < calling_convention.GetNumberOfRegisters()) {
2607 return LocationFrom(calling_convention.GetRegisterAt(index));
2608 } else {
2609 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
2610 }
2611 }
2612
2613 case Primitive::kPrimLong: {
2614 uint32_t index = gp_index_;
2615 uint32_t stack_index = stack_index_;
2616 gp_index_ += 2;
2617 stack_index_ += 2;
2618 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
2619 if (calling_convention.GetRegisterAt(index).Is(r1)) {
2620 // Skip R1, and use R2_R3 instead.
2621 gp_index_++;
2622 index++;
2623 }
2624 }
2625 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
2626 DCHECK_EQ(calling_convention.GetRegisterAt(index).GetCode() + 1,
2627 calling_convention.GetRegisterAt(index + 1).GetCode());
2628
2629 return LocationFrom(calling_convention.GetRegisterAt(index),
2630 calling_convention.GetRegisterAt(index + 1));
2631 } else {
2632 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
2633 }
2634 }
2635
2636 case Primitive::kPrimFloat: {
2637 uint32_t stack_index = stack_index_++;
2638 if (float_index_ % 2 == 0) {
2639 float_index_ = std::max(double_index_, float_index_);
2640 }
2641 if (float_index_ < calling_convention.GetNumberOfFpuRegisters()) {
2642 return LocationFrom(calling_convention.GetFpuRegisterAt(float_index_++));
2643 } else {
2644 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
2645 }
2646 }
2647
2648 case Primitive::kPrimDouble: {
2649 double_index_ = std::max(double_index_, RoundUp(float_index_, 2));
2650 uint32_t stack_index = stack_index_;
2651 stack_index_ += 2;
2652 if (double_index_ + 1 < calling_convention.GetNumberOfFpuRegisters()) {
2653 uint32_t index = double_index_;
2654 double_index_ += 2;
2655 Location result = LocationFrom(
2656 calling_convention.GetFpuRegisterAt(index),
2657 calling_convention.GetFpuRegisterAt(index + 1));
2658 DCHECK(ExpectedPairLayout(result));
2659 return result;
2660 } else {
2661 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
2662 }
2663 }
2664
2665 case Primitive::kPrimVoid:
2666 LOG(FATAL) << "Unexpected parameter type " << type;
2667 break;
2668 }
2669 return Location::NoLocation();
2670}
2671
2672Location InvokeDexCallingConventionVisitorARMVIXL::GetReturnLocation(Primitive::Type type) const {
2673 switch (type) {
2674 case Primitive::kPrimBoolean:
2675 case Primitive::kPrimByte:
2676 case Primitive::kPrimChar:
2677 case Primitive::kPrimShort:
2678 case Primitive::kPrimInt:
2679 case Primitive::kPrimNot: {
2680 return LocationFrom(r0);
2681 }
2682
2683 case Primitive::kPrimFloat: {
2684 return LocationFrom(s0);
2685 }
2686
2687 case Primitive::kPrimLong: {
2688 return LocationFrom(r0, r1);
2689 }
2690
2691 case Primitive::kPrimDouble: {
2692 return LocationFrom(s0, s1);
2693 }
2694
2695 case Primitive::kPrimVoid:
2696 return Location::NoLocation();
2697 }
2698
2699 UNREACHABLE();
2700}
2701
2702Location InvokeDexCallingConventionVisitorARMVIXL::GetMethodLocation() const {
2703 return LocationFrom(kMethodRegister);
2704}
2705
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002706void CodeGeneratorARMVIXL::Move32(Location destination, Location source) {
2707 if (source.Equals(destination)) {
2708 return;
2709 }
2710 if (destination.IsRegister()) {
2711 if (source.IsRegister()) {
2712 __ Mov(RegisterFrom(destination), RegisterFrom(source));
2713 } else if (source.IsFpuRegister()) {
2714 __ Vmov(RegisterFrom(destination), SRegisterFrom(source));
2715 } else {
2716 GetAssembler()->LoadFromOffset(kLoadWord,
2717 RegisterFrom(destination),
2718 sp,
2719 source.GetStackIndex());
2720 }
2721 } else if (destination.IsFpuRegister()) {
2722 if (source.IsRegister()) {
2723 __ Vmov(SRegisterFrom(destination), RegisterFrom(source));
2724 } else if (source.IsFpuRegister()) {
2725 __ Vmov(SRegisterFrom(destination), SRegisterFrom(source));
2726 } else {
2727 GetAssembler()->LoadSFromOffset(SRegisterFrom(destination), sp, source.GetStackIndex());
2728 }
2729 } else {
2730 DCHECK(destination.IsStackSlot()) << destination;
2731 if (source.IsRegister()) {
2732 GetAssembler()->StoreToOffset(kStoreWord,
2733 RegisterFrom(source),
2734 sp,
2735 destination.GetStackIndex());
2736 } else if (source.IsFpuRegister()) {
2737 GetAssembler()->StoreSToOffset(SRegisterFrom(source), sp, destination.GetStackIndex());
2738 } else {
2739 DCHECK(source.IsStackSlot()) << source;
2740 UseScratchRegisterScope temps(GetVIXLAssembler());
2741 vixl32::Register temp = temps.Acquire();
2742 GetAssembler()->LoadFromOffset(kLoadWord, temp, sp, source.GetStackIndex());
2743 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
2744 }
2745 }
2746}
2747
Artem Serovcfbe9132016-10-14 15:58:56 +01002748void CodeGeneratorARMVIXL::MoveConstant(Location location, int32_t value) {
2749 DCHECK(location.IsRegister());
2750 __ Mov(RegisterFrom(location), value);
Scott Wakelingfe885462016-09-22 10:24:38 +01002751}
2752
2753void CodeGeneratorARMVIXL::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002754 // TODO(VIXL): Maybe refactor to have the 'move' implementation here and use it in
2755 // `ParallelMoveResolverARMVIXL::EmitMove`, as is done in the `arm64` backend.
2756 HParallelMove move(GetGraph()->GetArena());
2757 move.AddMove(src, dst, dst_type, nullptr);
2758 GetMoveResolver()->EmitNativeCode(&move);
Scott Wakelingfe885462016-09-22 10:24:38 +01002759}
2760
Artem Serovcfbe9132016-10-14 15:58:56 +01002761void CodeGeneratorARMVIXL::AddLocationAsTemp(Location location, LocationSummary* locations) {
2762 if (location.IsRegister()) {
2763 locations->AddTemp(location);
2764 } else if (location.IsRegisterPair()) {
2765 locations->AddTemp(LocationFrom(LowRegisterFrom(location)));
2766 locations->AddTemp(LocationFrom(HighRegisterFrom(location)));
2767 } else {
2768 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
2769 }
Scott Wakelingfe885462016-09-22 10:24:38 +01002770}
2771
2772void CodeGeneratorARMVIXL::InvokeRuntime(QuickEntrypointEnum entrypoint,
2773 HInstruction* instruction,
2774 uint32_t dex_pc,
2775 SlowPathCode* slow_path) {
2776 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Alexandre Rames374ddf32016-11-04 10:40:49 +00002777 __ Ldr(lr, MemOperand(tr, GetThreadOffset<kArmPointerSize>(entrypoint).Int32Value()));
2778 // Ensure the pc position is recorded immediately after the `blx` instruction.
2779 // 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 +00002780 ExactAssemblyScope aas(GetVIXLAssembler(),
2781 vixl32::k16BitT32InstructionSizeInBytes,
2782 CodeBufferCheckScope::kExactSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00002783 __ blx(lr);
Scott Wakelingfe885462016-09-22 10:24:38 +01002784 if (EntrypointRequiresStackMap(entrypoint)) {
2785 RecordPcInfo(instruction, dex_pc, slow_path);
2786 }
2787}
2788
2789void CodeGeneratorARMVIXL::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
2790 HInstruction* instruction,
2791 SlowPathCode* slow_path) {
2792 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Alexandre Rames374ddf32016-11-04 10:40:49 +00002793 __ Ldr(lr, MemOperand(tr, entry_point_offset));
Scott Wakelingfe885462016-09-22 10:24:38 +01002794 __ Blx(lr);
2795}
2796
Scott Wakelingfe885462016-09-22 10:24:38 +01002797void InstructionCodeGeneratorARMVIXL::HandleGoto(HInstruction* got, HBasicBlock* successor) {
2798 DCHECK(!successor->IsExitBlock());
2799 HBasicBlock* block = got->GetBlock();
2800 HInstruction* previous = got->GetPrevious();
2801 HLoopInformation* info = block->GetLoopInformation();
2802
2803 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
2804 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
2805 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2806 return;
2807 }
2808 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2809 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
Roland Levillain5daa4952017-07-03 17:23:56 +01002810 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 2);
Scott Wakelingfe885462016-09-22 10:24:38 +01002811 }
2812 if (!codegen_->GoesToNextBlock(block, successor)) {
2813 __ B(codegen_->GetLabelOf(successor));
2814 }
2815}
2816
2817void LocationsBuilderARMVIXL::VisitGoto(HGoto* got) {
2818 got->SetLocations(nullptr);
2819}
2820
2821void InstructionCodeGeneratorARMVIXL::VisitGoto(HGoto* got) {
2822 HandleGoto(got, got->GetSuccessor());
2823}
2824
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002825void LocationsBuilderARMVIXL::VisitTryBoundary(HTryBoundary* try_boundary) {
2826 try_boundary->SetLocations(nullptr);
2827}
2828
2829void InstructionCodeGeneratorARMVIXL::VisitTryBoundary(HTryBoundary* try_boundary) {
2830 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2831 if (!successor->IsExitBlock()) {
2832 HandleGoto(try_boundary, successor);
2833 }
2834}
2835
Scott Wakelingfe885462016-09-22 10:24:38 +01002836void LocationsBuilderARMVIXL::VisitExit(HExit* exit) {
2837 exit->SetLocations(nullptr);
2838}
2839
2840void InstructionCodeGeneratorARMVIXL::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
2841}
2842
Scott Wakelingfe885462016-09-22 10:24:38 +01002843void InstructionCodeGeneratorARMVIXL::GenerateCompareTestAndBranch(HCondition* condition,
Anton Kirilov23b752b2017-07-20 14:40:44 +01002844 vixl32::Label* true_target,
2845 vixl32::Label* false_target,
Anton Kirilovfd522532017-05-10 12:46:57 +01002846 bool is_far_target) {
Anton Kirilov23b752b2017-07-20 14:40:44 +01002847 if (true_target == false_target) {
2848 DCHECK(true_target != nullptr);
2849 __ B(true_target);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002850 return;
2851 }
2852
Anton Kirilov23b752b2017-07-20 14:40:44 +01002853 vixl32::Label* non_fallthrough_target;
2854 bool invert;
2855 bool emit_both_branches;
Scott Wakelingfe885462016-09-22 10:24:38 +01002856
Anton Kirilov23b752b2017-07-20 14:40:44 +01002857 if (true_target == nullptr) {
2858 // The true target is fallthrough.
2859 DCHECK(false_target != nullptr);
2860 non_fallthrough_target = false_target;
2861 invert = true;
2862 emit_both_branches = false;
2863 } else {
2864 non_fallthrough_target = true_target;
2865 invert = false;
2866 // Either the false target is fallthrough, or there is no fallthrough
2867 // and both branches must be emitted.
2868 emit_both_branches = (false_target != nullptr);
Scott Wakelingfe885462016-09-22 10:24:38 +01002869 }
2870
Anton Kirilov23b752b2017-07-20 14:40:44 +01002871 const auto cond = GenerateTest(condition, invert, codegen_);
2872
2873 __ B(cond.first, non_fallthrough_target, is_far_target);
2874
2875 if (emit_both_branches) {
2876 // No target falls through, we need to branch.
2877 __ B(false_target);
Scott Wakelingfe885462016-09-22 10:24:38 +01002878 }
2879}
2880
2881void InstructionCodeGeneratorARMVIXL::GenerateTestAndBranch(HInstruction* instruction,
2882 size_t condition_input_index,
2883 vixl32::Label* true_target,
xueliang.zhongf51bc622016-11-04 09:23:32 +00002884 vixl32::Label* false_target,
2885 bool far_target) {
Scott Wakelingfe885462016-09-22 10:24:38 +01002886 HInstruction* cond = instruction->InputAt(condition_input_index);
2887
2888 if (true_target == nullptr && false_target == nullptr) {
2889 // Nothing to do. The code always falls through.
2890 return;
2891 } else if (cond->IsIntConstant()) {
2892 // Constant condition, statically compared against "true" (integer value 1).
2893 if (cond->AsIntConstant()->IsTrue()) {
2894 if (true_target != nullptr) {
2895 __ B(true_target);
2896 }
2897 } else {
Anton Kirilov644032c2016-12-06 17:51:43 +00002898 DCHECK(cond->AsIntConstant()->IsFalse()) << Int32ConstantFrom(cond);
Scott Wakelingfe885462016-09-22 10:24:38 +01002899 if (false_target != nullptr) {
2900 __ B(false_target);
2901 }
2902 }
2903 return;
2904 }
2905
2906 // The following code generates these patterns:
2907 // (1) true_target == nullptr && false_target != nullptr
2908 // - opposite condition true => branch to false_target
2909 // (2) true_target != nullptr && false_target == nullptr
2910 // - condition true => branch to true_target
2911 // (3) true_target != nullptr && false_target != nullptr
2912 // - condition true => branch to true_target
2913 // - branch to false_target
2914 if (IsBooleanValueOrMaterializedCondition(cond)) {
2915 // Condition has been materialized, compare the output to 0.
2916 if (kIsDebugBuild) {
2917 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
2918 DCHECK(cond_val.IsRegister());
2919 }
2920 if (true_target == nullptr) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00002921 __ CompareAndBranchIfZero(InputRegisterAt(instruction, condition_input_index),
2922 false_target,
2923 far_target);
Scott Wakelingfe885462016-09-22 10:24:38 +01002924 } else {
xueliang.zhongf51bc622016-11-04 09:23:32 +00002925 __ CompareAndBranchIfNonZero(InputRegisterAt(instruction, condition_input_index),
2926 true_target,
2927 far_target);
Scott Wakelingfe885462016-09-22 10:24:38 +01002928 }
2929 } else {
2930 // Condition has not been materialized. Use its inputs as the comparison and
2931 // its condition as the branch condition.
2932 HCondition* condition = cond->AsCondition();
2933
2934 // If this is a long or FP comparison that has been folded into
2935 // the HCondition, generate the comparison directly.
2936 Primitive::Type type = condition->InputAt(0)->GetType();
2937 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
Anton Kirilovfd522532017-05-10 12:46:57 +01002938 GenerateCompareTestAndBranch(condition, true_target, false_target, far_target);
Scott Wakelingfe885462016-09-22 10:24:38 +01002939 return;
2940 }
2941
Donghui Bai426b49c2016-11-08 14:55:38 +08002942 vixl32::Label* non_fallthrough_target;
2943 vixl32::Condition arm_cond = vixl32::Condition::None();
2944 const vixl32::Register left = InputRegisterAt(cond, 0);
2945 const Operand right = InputOperandAt(cond, 1);
2946
Scott Wakelingfe885462016-09-22 10:24:38 +01002947 if (true_target == nullptr) {
Donghui Bai426b49c2016-11-08 14:55:38 +08002948 arm_cond = ARMCondition(condition->GetOppositeCondition());
2949 non_fallthrough_target = false_target;
Scott Wakelingfe885462016-09-22 10:24:38 +01002950 } else {
Donghui Bai426b49c2016-11-08 14:55:38 +08002951 arm_cond = ARMCondition(condition->GetCondition());
2952 non_fallthrough_target = true_target;
2953 }
2954
2955 if (right.IsImmediate() && right.GetImmediate() == 0 && (arm_cond.Is(ne) || arm_cond.Is(eq))) {
2956 if (arm_cond.Is(eq)) {
Anton Kirilovfd522532017-05-10 12:46:57 +01002957 __ CompareAndBranchIfZero(left, non_fallthrough_target, far_target);
Donghui Bai426b49c2016-11-08 14:55:38 +08002958 } else {
2959 DCHECK(arm_cond.Is(ne));
Anton Kirilovfd522532017-05-10 12:46:57 +01002960 __ CompareAndBranchIfNonZero(left, non_fallthrough_target, far_target);
Donghui Bai426b49c2016-11-08 14:55:38 +08002961 }
2962 } else {
2963 __ Cmp(left, right);
Anton Kirilovfd522532017-05-10 12:46:57 +01002964 __ B(arm_cond, non_fallthrough_target, far_target);
Scott Wakelingfe885462016-09-22 10:24:38 +01002965 }
2966 }
2967
2968 // If neither branch falls through (case 3), the conditional branch to `true_target`
2969 // was already emitted (case 2) and we need to emit a jump to `false_target`.
2970 if (true_target != nullptr && false_target != nullptr) {
2971 __ B(false_target);
2972 }
2973}
2974
2975void LocationsBuilderARMVIXL::VisitIf(HIf* if_instr) {
2976 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
2977 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
2978 locations->SetInAt(0, Location::RequiresRegister());
2979 }
2980}
2981
2982void InstructionCodeGeneratorARMVIXL::VisitIf(HIf* if_instr) {
2983 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
2984 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002985 vixl32::Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
2986 nullptr : codegen_->GetLabelOf(true_successor);
2987 vixl32::Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
2988 nullptr : codegen_->GetLabelOf(false_successor);
Scott Wakelingfe885462016-09-22 10:24:38 +01002989 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
2990}
2991
Scott Wakelingc34dba72016-10-03 10:14:44 +01002992void LocationsBuilderARMVIXL::VisitDeoptimize(HDeoptimize* deoptimize) {
2993 LocationSummary* locations = new (GetGraph()->GetArena())
2994 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01002995 InvokeRuntimeCallingConventionARMVIXL calling_convention;
2996 RegisterSet caller_saves = RegisterSet::Empty();
2997 caller_saves.Add(LocationFrom(calling_convention.GetRegisterAt(0)));
2998 locations->SetCustomSlowPathCallerSaves(caller_saves);
Scott Wakelingc34dba72016-10-03 10:14:44 +01002999 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
3000 locations->SetInAt(0, Location::RequiresRegister());
3001 }
3002}
3003
3004void InstructionCodeGeneratorARMVIXL::VisitDeoptimize(HDeoptimize* deoptimize) {
3005 SlowPathCodeARMVIXL* slow_path =
3006 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARMVIXL>(deoptimize);
3007 GenerateTestAndBranch(deoptimize,
3008 /* condition_input_index */ 0,
3009 slow_path->GetEntryLabel(),
3010 /* false_target */ nullptr);
3011}
3012
Artem Serovd4cc5b22016-11-04 11:19:09 +00003013void LocationsBuilderARMVIXL::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
3014 LocationSummary* locations = new (GetGraph()->GetArena())
3015 LocationSummary(flag, LocationSummary::kNoCall);
3016 locations->SetOut(Location::RequiresRegister());
3017}
3018
3019void InstructionCodeGeneratorARMVIXL::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
3020 GetAssembler()->LoadFromOffset(kLoadWord,
3021 OutputRegister(flag),
3022 sp,
3023 codegen_->GetStackOffsetOfShouldDeoptimizeFlag());
3024}
3025
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003026void LocationsBuilderARMVIXL::VisitSelect(HSelect* select) {
3027 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Donghui Bai426b49c2016-11-08 14:55:38 +08003028 const bool is_floating_point = Primitive::IsFloatingPointType(select->GetType());
3029
3030 if (is_floating_point) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003031 locations->SetInAt(0, Location::RequiresFpuRegister());
Donghui Bai426b49c2016-11-08 14:55:38 +08003032 locations->SetInAt(1, Location::FpuRegisterOrConstant(select->GetTrueValue()));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003033 } else {
3034 locations->SetInAt(0, Location::RequiresRegister());
Donghui Bai426b49c2016-11-08 14:55:38 +08003035 locations->SetInAt(1, Arm8BitEncodableConstantOrRegister(select->GetTrueValue()));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003036 }
Donghui Bai426b49c2016-11-08 14:55:38 +08003037
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003038 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
Donghui Bai426b49c2016-11-08 14:55:38 +08003039 locations->SetInAt(2, Location::RegisterOrConstant(select->GetCondition()));
3040 // The code generator handles overlap with the values, but not with the condition.
3041 locations->SetOut(Location::SameAsFirstInput());
3042 } else if (is_floating_point) {
3043 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3044 } else {
3045 if (!locations->InAt(1).IsConstant()) {
3046 locations->SetInAt(0, Arm8BitEncodableConstantOrRegister(select->GetFalseValue()));
3047 }
3048
3049 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003050 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003051}
3052
3053void InstructionCodeGeneratorARMVIXL::VisitSelect(HSelect* select) {
Donghui Bai426b49c2016-11-08 14:55:38 +08003054 HInstruction* const condition = select->GetCondition();
3055 const LocationSummary* const locations = select->GetLocations();
3056 const Primitive::Type type = select->GetType();
3057 const Location first = locations->InAt(0);
3058 const Location out = locations->Out();
3059 const Location second = locations->InAt(1);
3060 Location src;
3061
3062 if (condition->IsIntConstant()) {
3063 if (condition->AsIntConstant()->IsFalse()) {
3064 src = first;
3065 } else {
3066 src = second;
3067 }
3068
3069 codegen_->MoveLocation(out, src, type);
3070 return;
3071 }
3072
Anton Kirilov23b752b2017-07-20 14:40:44 +01003073 if (!Primitive::IsFloatingPointType(type)) {
Donghui Bai426b49c2016-11-08 14:55:38 +08003074 bool invert = false;
3075
3076 if (out.Equals(second)) {
3077 src = first;
3078 invert = true;
3079 } else if (out.Equals(first)) {
3080 src = second;
3081 } else if (second.IsConstant()) {
3082 DCHECK(CanEncodeConstantAs8BitImmediate(second.GetConstant()));
3083 src = second;
3084 } else if (first.IsConstant()) {
3085 DCHECK(CanEncodeConstantAs8BitImmediate(first.GetConstant()));
3086 src = first;
3087 invert = true;
3088 } else {
3089 src = second;
3090 }
3091
3092 if (CanGenerateConditionalMove(out, src)) {
3093 if (!out.Equals(first) && !out.Equals(second)) {
3094 codegen_->MoveLocation(out, src.Equals(first) ? second : first, type);
3095 }
3096
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003097 std::pair<vixl32::Condition, vixl32::Condition> cond(eq, ne);
3098
3099 if (IsBooleanValueOrMaterializedCondition(condition)) {
3100 __ Cmp(InputRegisterAt(select, 2), 0);
3101 cond = invert ? std::make_pair(eq, ne) : std::make_pair(ne, eq);
3102 } else {
3103 cond = GenerateTest(condition->AsCondition(), invert, codegen_);
3104 }
3105
Donghui Bai426b49c2016-11-08 14:55:38 +08003106 const size_t instr_count = out.IsRegisterPair() ? 4 : 2;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003107 // We use the scope because of the IT block that follows.
Donghui Bai426b49c2016-11-08 14:55:38 +08003108 ExactAssemblyScope guard(GetVIXLAssembler(),
3109 instr_count * vixl32::k16BitT32InstructionSizeInBytes,
3110 CodeBufferCheckScope::kExactSize);
3111
3112 if (out.IsRegister()) {
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003113 __ it(cond.first);
3114 __ mov(cond.first, RegisterFrom(out), OperandFrom(src, type));
Donghui Bai426b49c2016-11-08 14:55:38 +08003115 } else {
3116 DCHECK(out.IsRegisterPair());
3117
3118 Operand operand_high(0);
3119 Operand operand_low(0);
3120
3121 if (src.IsConstant()) {
3122 const int64_t value = Int64ConstantFrom(src);
3123
3124 operand_high = High32Bits(value);
3125 operand_low = Low32Bits(value);
3126 } else {
3127 DCHECK(src.IsRegisterPair());
3128 operand_high = HighRegisterFrom(src);
3129 operand_low = LowRegisterFrom(src);
3130 }
3131
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003132 __ it(cond.first);
3133 __ mov(cond.first, LowRegisterFrom(out), operand_low);
3134 __ it(cond.first);
3135 __ mov(cond.first, HighRegisterFrom(out), operand_high);
Donghui Bai426b49c2016-11-08 14:55:38 +08003136 }
3137
3138 return;
3139 }
3140 }
3141
3142 vixl32::Label* false_target = nullptr;
3143 vixl32::Label* true_target = nullptr;
3144 vixl32::Label select_end;
3145 vixl32::Label* const target = codegen_->GetFinalLabel(select, &select_end);
3146
3147 if (out.Equals(second)) {
3148 true_target = target;
3149 src = first;
3150 } else {
3151 false_target = target;
3152 src = second;
3153
3154 if (!out.Equals(first)) {
3155 codegen_->MoveLocation(out, first, type);
3156 }
3157 }
3158
3159 GenerateTestAndBranch(select, 2, true_target, false_target, /* far_target */ false);
3160 codegen_->MoveLocation(out, src, type);
3161
3162 if (select_end.IsReferenced()) {
3163 __ Bind(&select_end);
3164 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003165}
3166
Artem Serov551b28f2016-10-18 19:11:30 +01003167void LocationsBuilderARMVIXL::VisitNativeDebugInfo(HNativeDebugInfo* info) {
3168 new (GetGraph()->GetArena()) LocationSummary(info);
3169}
3170
3171void InstructionCodeGeneratorARMVIXL::VisitNativeDebugInfo(HNativeDebugInfo*) {
3172 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
3173}
3174
Scott Wakelingfe885462016-09-22 10:24:38 +01003175void CodeGeneratorARMVIXL::GenerateNop() {
3176 __ Nop();
3177}
3178
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003179// `temp` is an extra temporary register that is used for some conditions;
3180// callers may not specify it, in which case the method will use a scratch
3181// register instead.
3182void CodeGeneratorARMVIXL::GenerateConditionWithZero(IfCondition condition,
3183 vixl32::Register out,
3184 vixl32::Register in,
3185 vixl32::Register temp) {
3186 switch (condition) {
3187 case kCondEQ:
3188 // x <= 0 iff x == 0 when the comparison is unsigned.
3189 case kCondBE:
3190 if (!temp.IsValid() || (out.IsLow() && !out.Is(in))) {
3191 temp = out;
3192 }
3193
3194 // Avoid 32-bit instructions if possible; note that `in` and `temp` must be
3195 // different as well.
3196 if (in.IsLow() && temp.IsLow() && !in.Is(temp)) {
3197 // temp = - in; only 0 sets the carry flag.
3198 __ Rsbs(temp, in, 0);
3199
3200 if (out.Is(in)) {
3201 std::swap(in, temp);
3202 }
3203
3204 // out = - in + in + carry = carry
3205 __ Adc(out, temp, in);
3206 } else {
3207 // If `in` is 0, then it has 32 leading zeros, and less than that otherwise.
3208 __ Clz(out, in);
3209 // Any number less than 32 logically shifted right by 5 bits results in 0;
3210 // the same operation on 32 yields 1.
3211 __ Lsr(out, out, 5);
3212 }
3213
3214 break;
3215 case kCondNE:
3216 // x > 0 iff x != 0 when the comparison is unsigned.
3217 case kCondA: {
3218 UseScratchRegisterScope temps(GetVIXLAssembler());
3219
3220 if (out.Is(in)) {
3221 if (!temp.IsValid() || in.Is(temp)) {
3222 temp = temps.Acquire();
3223 }
3224 } else if (!temp.IsValid() || !temp.IsLow()) {
3225 temp = out;
3226 }
3227
3228 // temp = in - 1; only 0 does not set the carry flag.
3229 __ Subs(temp, in, 1);
3230 // out = in + ~temp + carry = in + (-(in - 1) - 1) + carry = in - in + 1 - 1 + carry = carry
3231 __ Sbc(out, in, temp);
3232 break;
3233 }
3234 case kCondGE:
3235 __ Mvn(out, in);
3236 in = out;
3237 FALLTHROUGH_INTENDED;
3238 case kCondLT:
3239 // We only care about the sign bit.
3240 __ Lsr(out, in, 31);
3241 break;
3242 case kCondAE:
3243 // Trivially true.
3244 __ Mov(out, 1);
3245 break;
3246 case kCondB:
3247 // Trivially false.
3248 __ Mov(out, 0);
3249 break;
3250 default:
3251 LOG(FATAL) << "Unexpected condition " << condition;
3252 UNREACHABLE();
3253 }
3254}
3255
Scott Wakelingfe885462016-09-22 10:24:38 +01003256void LocationsBuilderARMVIXL::HandleCondition(HCondition* cond) {
3257 LocationSummary* locations =
3258 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
3259 // Handle the long/FP comparisons made in instruction simplification.
3260 switch (cond->InputAt(0)->GetType()) {
3261 case Primitive::kPrimLong:
3262 locations->SetInAt(0, Location::RequiresRegister());
3263 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
3264 if (!cond->IsEmittedAtUseSite()) {
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003265 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Scott Wakelingfe885462016-09-22 10:24:38 +01003266 }
3267 break;
3268
Scott Wakelingfe885462016-09-22 10:24:38 +01003269 case Primitive::kPrimFloat:
3270 case Primitive::kPrimDouble:
3271 locations->SetInAt(0, Location::RequiresFpuRegister());
Artem Serov657022c2016-11-23 14:19:38 +00003272 locations->SetInAt(1, ArithmeticZeroOrFpuRegister(cond->InputAt(1)));
Scott Wakelingfe885462016-09-22 10:24:38 +01003273 if (!cond->IsEmittedAtUseSite()) {
3274 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3275 }
3276 break;
3277
3278 default:
3279 locations->SetInAt(0, Location::RequiresRegister());
3280 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
3281 if (!cond->IsEmittedAtUseSite()) {
3282 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3283 }
3284 }
3285}
3286
3287void InstructionCodeGeneratorARMVIXL::HandleCondition(HCondition* cond) {
3288 if (cond->IsEmittedAtUseSite()) {
3289 return;
3290 }
3291
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003292 const Primitive::Type type = cond->GetLeft()->GetType();
Scott Wakelingfe885462016-09-22 10:24:38 +01003293
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003294 if (Primitive::IsFloatingPointType(type)) {
3295 GenerateConditionGeneric(cond, codegen_);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003296 return;
Scott Wakelingfe885462016-09-22 10:24:38 +01003297 }
3298
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003299 DCHECK(Primitive::IsIntegralType(type) || type == Primitive::kPrimNot) << type;
Scott Wakelingfe885462016-09-22 10:24:38 +01003300
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003301 const IfCondition condition = cond->GetCondition();
Scott Wakelingfe885462016-09-22 10:24:38 +01003302
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003303 // A condition with only one boolean input, or two boolean inputs without being equality or
3304 // inequality results from transformations done by the instruction simplifier, and is handled
3305 // as a regular condition with integral inputs.
3306 if (type == Primitive::kPrimBoolean &&
3307 cond->GetRight()->GetType() == Primitive::kPrimBoolean &&
3308 (condition == kCondEQ || condition == kCondNE)) {
3309 vixl32::Register left = InputRegisterAt(cond, 0);
3310 const vixl32::Register out = OutputRegister(cond);
3311 const Location right_loc = cond->GetLocations()->InAt(1);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003312
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003313 // The constant case is handled by the instruction simplifier.
3314 DCHECK(!right_loc.IsConstant());
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003315
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003316 vixl32::Register right = RegisterFrom(right_loc);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003317
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003318 // Avoid 32-bit instructions if possible.
3319 if (out.Is(right)) {
3320 std::swap(left, right);
3321 }
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003322
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003323 __ Eor(out, left, right);
3324
3325 if (condition == kCondEQ) {
3326 __ Eor(out, out, 1);
3327 }
3328
3329 return;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003330 }
Anton Kirilov6f644202017-02-27 18:29:45 +00003331
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003332 GenerateConditionIntegralOrNonPrimitive(cond, codegen_);
Scott Wakelingfe885462016-09-22 10:24:38 +01003333}
3334
3335void LocationsBuilderARMVIXL::VisitEqual(HEqual* comp) {
3336 HandleCondition(comp);
3337}
3338
3339void InstructionCodeGeneratorARMVIXL::VisitEqual(HEqual* comp) {
3340 HandleCondition(comp);
3341}
3342
3343void LocationsBuilderARMVIXL::VisitNotEqual(HNotEqual* comp) {
3344 HandleCondition(comp);
3345}
3346
3347void InstructionCodeGeneratorARMVIXL::VisitNotEqual(HNotEqual* comp) {
3348 HandleCondition(comp);
3349}
3350
3351void LocationsBuilderARMVIXL::VisitLessThan(HLessThan* comp) {
3352 HandleCondition(comp);
3353}
3354
3355void InstructionCodeGeneratorARMVIXL::VisitLessThan(HLessThan* comp) {
3356 HandleCondition(comp);
3357}
3358
3359void LocationsBuilderARMVIXL::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
3360 HandleCondition(comp);
3361}
3362
3363void InstructionCodeGeneratorARMVIXL::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
3364 HandleCondition(comp);
3365}
3366
3367void LocationsBuilderARMVIXL::VisitGreaterThan(HGreaterThan* comp) {
3368 HandleCondition(comp);
3369}
3370
3371void InstructionCodeGeneratorARMVIXL::VisitGreaterThan(HGreaterThan* comp) {
3372 HandleCondition(comp);
3373}
3374
3375void LocationsBuilderARMVIXL::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
3376 HandleCondition(comp);
3377}
3378
3379void InstructionCodeGeneratorARMVIXL::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
3380 HandleCondition(comp);
3381}
3382
3383void LocationsBuilderARMVIXL::VisitBelow(HBelow* comp) {
3384 HandleCondition(comp);
3385}
3386
3387void InstructionCodeGeneratorARMVIXL::VisitBelow(HBelow* comp) {
3388 HandleCondition(comp);
3389}
3390
3391void LocationsBuilderARMVIXL::VisitBelowOrEqual(HBelowOrEqual* comp) {
3392 HandleCondition(comp);
3393}
3394
3395void InstructionCodeGeneratorARMVIXL::VisitBelowOrEqual(HBelowOrEqual* comp) {
3396 HandleCondition(comp);
3397}
3398
3399void LocationsBuilderARMVIXL::VisitAbove(HAbove* comp) {
3400 HandleCondition(comp);
3401}
3402
3403void InstructionCodeGeneratorARMVIXL::VisitAbove(HAbove* comp) {
3404 HandleCondition(comp);
3405}
3406
3407void LocationsBuilderARMVIXL::VisitAboveOrEqual(HAboveOrEqual* comp) {
3408 HandleCondition(comp);
3409}
3410
3411void InstructionCodeGeneratorARMVIXL::VisitAboveOrEqual(HAboveOrEqual* comp) {
3412 HandleCondition(comp);
3413}
3414
3415void LocationsBuilderARMVIXL::VisitIntConstant(HIntConstant* constant) {
3416 LocationSummary* locations =
3417 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3418 locations->SetOut(Location::ConstantLocation(constant));
3419}
3420
3421void InstructionCodeGeneratorARMVIXL::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
3422 // Will be generated at use site.
3423}
3424
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003425void LocationsBuilderARMVIXL::VisitNullConstant(HNullConstant* constant) {
3426 LocationSummary* locations =
3427 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3428 locations->SetOut(Location::ConstantLocation(constant));
3429}
3430
3431void InstructionCodeGeneratorARMVIXL::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
3432 // Will be generated at use site.
3433}
3434
Scott Wakelingfe885462016-09-22 10:24:38 +01003435void LocationsBuilderARMVIXL::VisitLongConstant(HLongConstant* constant) {
3436 LocationSummary* locations =
3437 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3438 locations->SetOut(Location::ConstantLocation(constant));
3439}
3440
3441void InstructionCodeGeneratorARMVIXL::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
3442 // Will be generated at use site.
3443}
3444
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01003445void LocationsBuilderARMVIXL::VisitFloatConstant(HFloatConstant* constant) {
3446 LocationSummary* locations =
3447 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3448 locations->SetOut(Location::ConstantLocation(constant));
3449}
3450
Scott Wakelingc34dba72016-10-03 10:14:44 +01003451void InstructionCodeGeneratorARMVIXL::VisitFloatConstant(
3452 HFloatConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01003453 // Will be generated at use site.
3454}
3455
3456void LocationsBuilderARMVIXL::VisitDoubleConstant(HDoubleConstant* constant) {
3457 LocationSummary* locations =
3458 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3459 locations->SetOut(Location::ConstantLocation(constant));
3460}
3461
Scott Wakelingc34dba72016-10-03 10:14:44 +01003462void InstructionCodeGeneratorARMVIXL::VisitDoubleConstant(
3463 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01003464 // Will be generated at use site.
3465}
3466
Igor Murashkind01745e2017-04-05 16:40:31 -07003467void LocationsBuilderARMVIXL::VisitConstructorFence(HConstructorFence* constructor_fence) {
3468 constructor_fence->SetLocations(nullptr);
3469}
3470
3471void InstructionCodeGeneratorARMVIXL::VisitConstructorFence(
3472 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
3473 codegen_->GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
3474}
3475
Scott Wakelingfe885462016-09-22 10:24:38 +01003476void LocationsBuilderARMVIXL::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3477 memory_barrier->SetLocations(nullptr);
3478}
3479
3480void InstructionCodeGeneratorARMVIXL::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3481 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
3482}
3483
3484void LocationsBuilderARMVIXL::VisitReturnVoid(HReturnVoid* ret) {
3485 ret->SetLocations(nullptr);
3486}
3487
3488void InstructionCodeGeneratorARMVIXL::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
3489 codegen_->GenerateFrameExit();
3490}
3491
3492void LocationsBuilderARMVIXL::VisitReturn(HReturn* ret) {
3493 LocationSummary* locations =
3494 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
3495 locations->SetInAt(0, parameter_visitor_.GetReturnLocation(ret->InputAt(0)->GetType()));
3496}
3497
3498void InstructionCodeGeneratorARMVIXL::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
3499 codegen_->GenerateFrameExit();
3500}
3501
Artem Serovcfbe9132016-10-14 15:58:56 +01003502void LocationsBuilderARMVIXL::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3503 // The trampoline uses the same calling convention as dex calling conventions,
3504 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
3505 // the method_idx.
3506 HandleInvoke(invoke);
3507}
3508
3509void InstructionCodeGeneratorARMVIXL::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3510 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
Roland Levillain5daa4952017-07-03 17:23:56 +01003511 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 3);
Artem Serovcfbe9132016-10-14 15:58:56 +01003512}
3513
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003514void LocationsBuilderARMVIXL::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
3515 // Explicit clinit checks triggered by static invokes must have been pruned by
3516 // art::PrepareForRegisterAllocation.
3517 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
3518
Anton Kirilov5ec62182016-10-13 20:16:02 +01003519 IntrinsicLocationsBuilderARMVIXL intrinsic(codegen_);
3520 if (intrinsic.TryDispatch(invoke)) {
Anton Kirilov5ec62182016-10-13 20:16:02 +01003521 return;
3522 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003523
3524 HandleInvoke(invoke);
3525}
3526
Anton Kirilov5ec62182016-10-13 20:16:02 +01003527static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARMVIXL* codegen) {
3528 if (invoke->GetLocations()->Intrinsified()) {
3529 IntrinsicCodeGeneratorARMVIXL intrinsic(codegen);
3530 intrinsic.Dispatch(invoke);
3531 return true;
3532 }
3533 return false;
3534}
3535
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003536void InstructionCodeGeneratorARMVIXL::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
3537 // Explicit clinit checks triggered by static invokes must have been pruned by
3538 // art::PrepareForRegisterAllocation.
3539 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
3540
Anton Kirilov5ec62182016-10-13 20:16:02 +01003541 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
Roland Levillain5daa4952017-07-03 17:23:56 +01003542 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 4);
Anton Kirilov5ec62182016-10-13 20:16:02 +01003543 return;
3544 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003545
3546 LocationSummary* locations = invoke->GetLocations();
Artem Serovd4cc5b22016-11-04 11:19:09 +00003547 codegen_->GenerateStaticOrDirectCall(
3548 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Roland Levillain5daa4952017-07-03 17:23:56 +01003549
3550 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 5);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003551}
3552
3553void LocationsBuilderARMVIXL::HandleInvoke(HInvoke* invoke) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00003554 InvokeDexCallingConventionVisitorARMVIXL calling_convention_visitor;
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003555 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
3556}
3557
3558void LocationsBuilderARMVIXL::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Anton Kirilov5ec62182016-10-13 20:16:02 +01003559 IntrinsicLocationsBuilderARMVIXL intrinsic(codegen_);
3560 if (intrinsic.TryDispatch(invoke)) {
3561 return;
3562 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003563
3564 HandleInvoke(invoke);
3565}
3566
3567void InstructionCodeGeneratorARMVIXL::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Anton Kirilov5ec62182016-10-13 20:16:02 +01003568 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
Roland Levillain5daa4952017-07-03 17:23:56 +01003569 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 6);
Anton Kirilov5ec62182016-10-13 20:16:02 +01003570 return;
3571 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003572
3573 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexandre Rames374ddf32016-11-04 10:40:49 +00003574 DCHECK(!codegen_->IsLeafMethod());
Roland Levillain5daa4952017-07-03 17:23:56 +01003575
3576 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 7);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003577}
3578
Artem Serovcfbe9132016-10-14 15:58:56 +01003579void LocationsBuilderARMVIXL::VisitInvokeInterface(HInvokeInterface* invoke) {
3580 HandleInvoke(invoke);
3581 // Add the hidden argument.
3582 invoke->GetLocations()->AddTemp(LocationFrom(r12));
3583}
3584
3585void InstructionCodeGeneratorARMVIXL::VisitInvokeInterface(HInvokeInterface* invoke) {
3586 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
3587 LocationSummary* locations = invoke->GetLocations();
3588 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
3589 vixl32::Register hidden_reg = RegisterFrom(locations->GetTemp(1));
3590 Location receiver = locations->InAt(0);
3591 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3592
3593 DCHECK(!receiver.IsStackSlot());
3594
Alexandre Rames374ddf32016-11-04 10:40:49 +00003595 // Ensure the pc position is recorded immediately after the `ldr` instruction.
3596 {
Artem Serov0fb37192016-12-06 18:13:40 +00003597 ExactAssemblyScope aas(GetVIXLAssembler(),
3598 vixl32::kMaxInstructionSizeInBytes,
3599 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00003600 // /* HeapReference<Class> */ temp = receiver->klass_
3601 __ ldr(temp, MemOperand(RegisterFrom(receiver), class_offset));
3602 codegen_->MaybeRecordImplicitNullCheck(invoke);
3603 }
Artem Serovcfbe9132016-10-14 15:58:56 +01003604 // Instead of simply (possibly) unpoisoning `temp` here, we should
3605 // emit a read barrier for the previous class reference load.
3606 // However this is not required in practice, as this is an
3607 // intermediate/temporary reference and because the current
3608 // concurrent copying collector keeps the from-space memory
3609 // intact/accessible until the end of the marking phase (the
3610 // concurrent copying collector may not in the future).
3611 GetAssembler()->MaybeUnpoisonHeapReference(temp);
3612 GetAssembler()->LoadFromOffset(kLoadWord,
3613 temp,
3614 temp,
3615 mirror::Class::ImtPtrOffset(kArmPointerSize).Uint32Value());
3616 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
3617 invoke->GetImtIndex(), kArmPointerSize));
3618 // temp = temp->GetImtEntryAt(method_offset);
3619 GetAssembler()->LoadFromOffset(kLoadWord, temp, temp, method_offset);
3620 uint32_t entry_point =
3621 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize).Int32Value();
3622 // LR = temp->GetEntryPoint();
3623 GetAssembler()->LoadFromOffset(kLoadWord, lr, temp, entry_point);
3624
3625 // Set the hidden (in r12) argument. It is done here, right before a BLX to prevent other
3626 // instruction from clobbering it as they might use r12 as a scratch register.
3627 DCHECK(hidden_reg.Is(r12));
Scott Wakelingb77051e2016-11-21 19:46:00 +00003628
3629 {
3630 // The VIXL macro assembler may clobber any of the scratch registers that are available to it,
3631 // so it checks if the application is using them (by passing them to the macro assembler
3632 // methods). The following application of UseScratchRegisterScope corrects VIXL's notion of
3633 // what is available, and is the opposite of the standard usage: Instead of requesting a
3634 // temporary location, it imposes an external constraint (i.e. a specific register is reserved
3635 // for the hidden argument). Note that this works even if VIXL needs a scratch register itself
3636 // (to materialize the constant), since the destination register becomes available for such use
3637 // internally for the duration of the macro instruction.
3638 UseScratchRegisterScope temps(GetVIXLAssembler());
3639 temps.Exclude(hidden_reg);
3640 __ Mov(hidden_reg, invoke->GetDexMethodIndex());
3641 }
Artem Serovcfbe9132016-10-14 15:58:56 +01003642 {
Alexandre Rames374ddf32016-11-04 10:40:49 +00003643 // Ensure the pc position is recorded immediately after the `blx` instruction.
3644 // 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 +00003645 ExactAssemblyScope aas(GetVIXLAssembler(),
Alexandre Rames374ddf32016-11-04 10:40:49 +00003646 vixl32::k16BitT32InstructionSizeInBytes,
3647 CodeBufferCheckScope::kExactSize);
Artem Serovcfbe9132016-10-14 15:58:56 +01003648 // LR();
3649 __ blx(lr);
Artem Serovcfbe9132016-10-14 15:58:56 +01003650 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Alexandre Rames374ddf32016-11-04 10:40:49 +00003651 DCHECK(!codegen_->IsLeafMethod());
Artem Serovcfbe9132016-10-14 15:58:56 +01003652 }
Roland Levillain5daa4952017-07-03 17:23:56 +01003653
3654 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 8);
Artem Serovcfbe9132016-10-14 15:58:56 +01003655}
3656
Orion Hodsonac141392017-01-13 11:53:47 +00003657void LocationsBuilderARMVIXL::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
3658 HandleInvoke(invoke);
3659}
3660
3661void InstructionCodeGeneratorARMVIXL::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
3662 codegen_->GenerateInvokePolymorphicCall(invoke);
Roland Levillain5daa4952017-07-03 17:23:56 +01003663 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 9);
Orion Hodsonac141392017-01-13 11:53:47 +00003664}
3665
Artem Serov02109dd2016-09-23 17:17:54 +01003666void LocationsBuilderARMVIXL::VisitNeg(HNeg* neg) {
3667 LocationSummary* locations =
3668 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
3669 switch (neg->GetResultType()) {
3670 case Primitive::kPrimInt: {
3671 locations->SetInAt(0, Location::RequiresRegister());
3672 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3673 break;
3674 }
3675 case Primitive::kPrimLong: {
3676 locations->SetInAt(0, Location::RequiresRegister());
3677 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3678 break;
3679 }
3680
3681 case Primitive::kPrimFloat:
3682 case Primitive::kPrimDouble:
3683 locations->SetInAt(0, Location::RequiresFpuRegister());
3684 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3685 break;
3686
3687 default:
3688 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
3689 }
3690}
3691
3692void InstructionCodeGeneratorARMVIXL::VisitNeg(HNeg* neg) {
3693 LocationSummary* locations = neg->GetLocations();
3694 Location out = locations->Out();
3695 Location in = locations->InAt(0);
3696 switch (neg->GetResultType()) {
3697 case Primitive::kPrimInt:
3698 __ Rsb(OutputRegister(neg), InputRegisterAt(neg, 0), 0);
3699 break;
3700
3701 case Primitive::kPrimLong:
3702 // out.lo = 0 - in.lo (and update the carry/borrow (C) flag)
3703 __ Rsbs(LowRegisterFrom(out), LowRegisterFrom(in), 0);
3704 // We cannot emit an RSC (Reverse Subtract with Carry)
3705 // instruction here, as it does not exist in the Thumb-2
3706 // instruction set. We use the following approach
3707 // using SBC and SUB instead.
3708 //
3709 // out.hi = -C
3710 __ Sbc(HighRegisterFrom(out), HighRegisterFrom(out), HighRegisterFrom(out));
3711 // out.hi = out.hi - in.hi
3712 __ Sub(HighRegisterFrom(out), HighRegisterFrom(out), HighRegisterFrom(in));
3713 break;
3714
3715 case Primitive::kPrimFloat:
3716 case Primitive::kPrimDouble:
Anton Kirilov644032c2016-12-06 17:51:43 +00003717 __ Vneg(OutputVRegister(neg), InputVRegister(neg));
Artem Serov02109dd2016-09-23 17:17:54 +01003718 break;
3719
3720 default:
3721 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
3722 }
3723}
3724
Scott Wakelingfe885462016-09-22 10:24:38 +01003725void LocationsBuilderARMVIXL::VisitTypeConversion(HTypeConversion* conversion) {
3726 Primitive::Type result_type = conversion->GetResultType();
3727 Primitive::Type input_type = conversion->GetInputType();
3728 DCHECK_NE(result_type, input_type);
3729
3730 // The float-to-long, double-to-long and long-to-float type conversions
3731 // rely on a call to the runtime.
3732 LocationSummary::CallKind call_kind =
3733 (((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
3734 && result_type == Primitive::kPrimLong)
3735 || (input_type == Primitive::kPrimLong && result_type == Primitive::kPrimFloat))
3736 ? LocationSummary::kCallOnMainOnly
3737 : LocationSummary::kNoCall;
3738 LocationSummary* locations =
3739 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
3740
3741 // The Java language does not allow treating boolean as an integral type but
3742 // our bit representation makes it safe.
3743
3744 switch (result_type) {
3745 case Primitive::kPrimByte:
3746 switch (input_type) {
3747 case Primitive::kPrimLong:
3748 // Type conversion from long to byte is a result of code transformations.
3749 case Primitive::kPrimBoolean:
3750 // Boolean input is a result of code transformations.
3751 case Primitive::kPrimShort:
3752 case Primitive::kPrimInt:
3753 case Primitive::kPrimChar:
3754 // Processing a Dex `int-to-byte' instruction.
3755 locations->SetInAt(0, Location::RequiresRegister());
3756 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3757 break;
3758
3759 default:
3760 LOG(FATAL) << "Unexpected type conversion from " << input_type
3761 << " to " << result_type;
3762 }
3763 break;
3764
3765 case Primitive::kPrimShort:
3766 switch (input_type) {
3767 case Primitive::kPrimLong:
3768 // Type conversion from long to short is a result of code transformations.
3769 case Primitive::kPrimBoolean:
3770 // Boolean input is a result of code transformations.
3771 case Primitive::kPrimByte:
3772 case Primitive::kPrimInt:
3773 case Primitive::kPrimChar:
3774 // Processing a Dex `int-to-short' instruction.
3775 locations->SetInAt(0, Location::RequiresRegister());
3776 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3777 break;
3778
3779 default:
3780 LOG(FATAL) << "Unexpected type conversion from " << input_type
3781 << " to " << result_type;
3782 }
3783 break;
3784
3785 case Primitive::kPrimInt:
3786 switch (input_type) {
3787 case Primitive::kPrimLong:
3788 // Processing a Dex `long-to-int' instruction.
3789 locations->SetInAt(0, Location::Any());
3790 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3791 break;
3792
3793 case Primitive::kPrimFloat:
3794 // Processing a Dex `float-to-int' instruction.
3795 locations->SetInAt(0, Location::RequiresFpuRegister());
3796 locations->SetOut(Location::RequiresRegister());
3797 locations->AddTemp(Location::RequiresFpuRegister());
3798 break;
3799
3800 case Primitive::kPrimDouble:
3801 // Processing a Dex `double-to-int' instruction.
3802 locations->SetInAt(0, Location::RequiresFpuRegister());
3803 locations->SetOut(Location::RequiresRegister());
3804 locations->AddTemp(Location::RequiresFpuRegister());
3805 break;
3806
3807 default:
3808 LOG(FATAL) << "Unexpected type conversion from " << input_type
3809 << " to " << result_type;
3810 }
3811 break;
3812
3813 case Primitive::kPrimLong:
3814 switch (input_type) {
3815 case Primitive::kPrimBoolean:
3816 // Boolean input is a result of code transformations.
3817 case Primitive::kPrimByte:
3818 case Primitive::kPrimShort:
3819 case Primitive::kPrimInt:
3820 case Primitive::kPrimChar:
3821 // Processing a Dex `int-to-long' instruction.
3822 locations->SetInAt(0, Location::RequiresRegister());
3823 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3824 break;
3825
3826 case Primitive::kPrimFloat: {
3827 // Processing a Dex `float-to-long' instruction.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003828 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3829 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
3830 locations->SetOut(LocationFrom(r0, r1));
Scott Wakelingfe885462016-09-22 10:24:38 +01003831 break;
3832 }
3833
3834 case Primitive::kPrimDouble: {
3835 // Processing a Dex `double-to-long' instruction.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003836 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3837 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0),
3838 calling_convention.GetFpuRegisterAt(1)));
3839 locations->SetOut(LocationFrom(r0, r1));
Scott Wakelingfe885462016-09-22 10:24:38 +01003840 break;
3841 }
3842
3843 default:
3844 LOG(FATAL) << "Unexpected type conversion from " << input_type
3845 << " to " << result_type;
3846 }
3847 break;
3848
3849 case Primitive::kPrimChar:
3850 switch (input_type) {
3851 case Primitive::kPrimLong:
3852 // Type conversion from long to char is a result of code transformations.
3853 case Primitive::kPrimBoolean:
3854 // Boolean input is a result of code transformations.
3855 case Primitive::kPrimByte:
3856 case Primitive::kPrimShort:
3857 case Primitive::kPrimInt:
3858 // Processing a Dex `int-to-char' instruction.
3859 locations->SetInAt(0, Location::RequiresRegister());
3860 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3861 break;
3862
3863 default:
3864 LOG(FATAL) << "Unexpected type conversion from " << input_type
3865 << " to " << result_type;
3866 }
3867 break;
3868
3869 case Primitive::kPrimFloat:
3870 switch (input_type) {
3871 case Primitive::kPrimBoolean:
3872 // Boolean input is a result of code transformations.
3873 case Primitive::kPrimByte:
3874 case Primitive::kPrimShort:
3875 case Primitive::kPrimInt:
3876 case Primitive::kPrimChar:
3877 // Processing a Dex `int-to-float' instruction.
3878 locations->SetInAt(0, Location::RequiresRegister());
3879 locations->SetOut(Location::RequiresFpuRegister());
3880 break;
3881
3882 case Primitive::kPrimLong: {
3883 // Processing a Dex `long-to-float' instruction.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003884 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3885 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0),
3886 calling_convention.GetRegisterAt(1)));
3887 locations->SetOut(LocationFrom(calling_convention.GetFpuRegisterAt(0)));
Scott Wakelingfe885462016-09-22 10:24:38 +01003888 break;
3889 }
3890
3891 case Primitive::kPrimDouble:
3892 // Processing a Dex `double-to-float' instruction.
3893 locations->SetInAt(0, Location::RequiresFpuRegister());
3894 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3895 break;
3896
3897 default:
3898 LOG(FATAL) << "Unexpected type conversion from " << input_type
3899 << " to " << result_type;
3900 };
3901 break;
3902
3903 case Primitive::kPrimDouble:
3904 switch (input_type) {
3905 case Primitive::kPrimBoolean:
3906 // Boolean input is a result of code transformations.
3907 case Primitive::kPrimByte:
3908 case Primitive::kPrimShort:
3909 case Primitive::kPrimInt:
3910 case Primitive::kPrimChar:
3911 // Processing a Dex `int-to-double' instruction.
3912 locations->SetInAt(0, Location::RequiresRegister());
3913 locations->SetOut(Location::RequiresFpuRegister());
3914 break;
3915
3916 case Primitive::kPrimLong:
3917 // Processing a Dex `long-to-double' instruction.
3918 locations->SetInAt(0, Location::RequiresRegister());
3919 locations->SetOut(Location::RequiresFpuRegister());
3920 locations->AddTemp(Location::RequiresFpuRegister());
3921 locations->AddTemp(Location::RequiresFpuRegister());
3922 break;
3923
3924 case Primitive::kPrimFloat:
3925 // Processing a Dex `float-to-double' instruction.
3926 locations->SetInAt(0, Location::RequiresFpuRegister());
3927 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3928 break;
3929
3930 default:
3931 LOG(FATAL) << "Unexpected type conversion from " << input_type
3932 << " to " << result_type;
3933 };
3934 break;
3935
3936 default:
3937 LOG(FATAL) << "Unexpected type conversion from " << input_type
3938 << " to " << result_type;
3939 }
3940}
3941
3942void InstructionCodeGeneratorARMVIXL::VisitTypeConversion(HTypeConversion* conversion) {
3943 LocationSummary* locations = conversion->GetLocations();
3944 Location out = locations->Out();
3945 Location in = locations->InAt(0);
3946 Primitive::Type result_type = conversion->GetResultType();
3947 Primitive::Type input_type = conversion->GetInputType();
3948 DCHECK_NE(result_type, input_type);
3949 switch (result_type) {
3950 case Primitive::kPrimByte:
3951 switch (input_type) {
3952 case Primitive::kPrimLong:
3953 // Type conversion from long to byte is a result of code transformations.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003954 __ Sbfx(OutputRegister(conversion), LowRegisterFrom(in), 0, 8);
Scott Wakelingfe885462016-09-22 10:24:38 +01003955 break;
3956 case Primitive::kPrimBoolean:
3957 // Boolean input is a result of code transformations.
3958 case Primitive::kPrimShort:
3959 case Primitive::kPrimInt:
3960 case Primitive::kPrimChar:
3961 // Processing a Dex `int-to-byte' instruction.
3962 __ Sbfx(OutputRegister(conversion), InputRegisterAt(conversion, 0), 0, 8);
3963 break;
3964
3965 default:
3966 LOG(FATAL) << "Unexpected type conversion from " << input_type
3967 << " to " << result_type;
3968 }
3969 break;
3970
3971 case Primitive::kPrimShort:
3972 switch (input_type) {
3973 case Primitive::kPrimLong:
3974 // Type conversion from long to short is a result of code transformations.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003975 __ Sbfx(OutputRegister(conversion), LowRegisterFrom(in), 0, 16);
Scott Wakelingfe885462016-09-22 10:24:38 +01003976 break;
3977 case Primitive::kPrimBoolean:
3978 // Boolean input is a result of code transformations.
3979 case Primitive::kPrimByte:
3980 case Primitive::kPrimInt:
3981 case Primitive::kPrimChar:
3982 // Processing a Dex `int-to-short' instruction.
3983 __ Sbfx(OutputRegister(conversion), InputRegisterAt(conversion, 0), 0, 16);
3984 break;
3985
3986 default:
3987 LOG(FATAL) << "Unexpected type conversion from " << input_type
3988 << " to " << result_type;
3989 }
3990 break;
3991
3992 case Primitive::kPrimInt:
3993 switch (input_type) {
3994 case Primitive::kPrimLong:
3995 // Processing a Dex `long-to-int' instruction.
3996 DCHECK(out.IsRegister());
3997 if (in.IsRegisterPair()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003998 __ Mov(OutputRegister(conversion), LowRegisterFrom(in));
Scott Wakelingfe885462016-09-22 10:24:38 +01003999 } else if (in.IsDoubleStackSlot()) {
4000 GetAssembler()->LoadFromOffset(kLoadWord,
4001 OutputRegister(conversion),
4002 sp,
4003 in.GetStackIndex());
4004 } else {
4005 DCHECK(in.IsConstant());
4006 DCHECK(in.GetConstant()->IsLongConstant());
Vladimir Markoba1a48e2017-04-13 11:50:14 +01004007 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
4008 __ Mov(OutputRegister(conversion), static_cast<int32_t>(value));
Scott Wakelingfe885462016-09-22 10:24:38 +01004009 }
4010 break;
4011
4012 case Primitive::kPrimFloat: {
4013 // Processing a Dex `float-to-int' instruction.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004014 vixl32::SRegister temp = LowSRegisterFrom(locations->GetTemp(0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01004015 __ Vcvt(S32, F32, temp, InputSRegisterAt(conversion, 0));
Scott Wakelingfe885462016-09-22 10:24:38 +01004016 __ Vmov(OutputRegister(conversion), temp);
4017 break;
4018 }
4019
4020 case Primitive::kPrimDouble: {
4021 // Processing a Dex `double-to-int' instruction.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004022 vixl32::SRegister temp_s = LowSRegisterFrom(locations->GetTemp(0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01004023 __ Vcvt(S32, F64, temp_s, DRegisterFrom(in));
Scott Wakelingfe885462016-09-22 10:24:38 +01004024 __ Vmov(OutputRegister(conversion), temp_s);
4025 break;
4026 }
4027
4028 default:
4029 LOG(FATAL) << "Unexpected type conversion from " << input_type
4030 << " to " << result_type;
4031 }
4032 break;
4033
4034 case Primitive::kPrimLong:
4035 switch (input_type) {
4036 case Primitive::kPrimBoolean:
4037 // Boolean input is a result of code transformations.
4038 case Primitive::kPrimByte:
4039 case Primitive::kPrimShort:
4040 case Primitive::kPrimInt:
4041 case Primitive::kPrimChar:
4042 // Processing a Dex `int-to-long' instruction.
4043 DCHECK(out.IsRegisterPair());
4044 DCHECK(in.IsRegister());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004045 __ Mov(LowRegisterFrom(out), InputRegisterAt(conversion, 0));
Scott Wakelingfe885462016-09-22 10:24:38 +01004046 // Sign extension.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004047 __ Asr(HighRegisterFrom(out), LowRegisterFrom(out), 31);
Scott Wakelingfe885462016-09-22 10:24:38 +01004048 break;
4049
4050 case Primitive::kPrimFloat:
4051 // Processing a Dex `float-to-long' instruction.
4052 codegen_->InvokeRuntime(kQuickF2l, conversion, conversion->GetDexPc());
4053 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
4054 break;
4055
4056 case Primitive::kPrimDouble:
4057 // Processing a Dex `double-to-long' instruction.
4058 codegen_->InvokeRuntime(kQuickD2l, conversion, conversion->GetDexPc());
4059 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
4060 break;
4061
4062 default:
4063 LOG(FATAL) << "Unexpected type conversion from " << input_type
4064 << " to " << result_type;
4065 }
4066 break;
4067
4068 case Primitive::kPrimChar:
4069 switch (input_type) {
4070 case Primitive::kPrimLong:
4071 // Type conversion from long to char is a result of code transformations.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004072 __ Ubfx(OutputRegister(conversion), LowRegisterFrom(in), 0, 16);
Scott Wakelingfe885462016-09-22 10:24:38 +01004073 break;
4074 case Primitive::kPrimBoolean:
4075 // Boolean input is a result of code transformations.
4076 case Primitive::kPrimByte:
4077 case Primitive::kPrimShort:
4078 case Primitive::kPrimInt:
4079 // Processing a Dex `int-to-char' instruction.
4080 __ Ubfx(OutputRegister(conversion), InputRegisterAt(conversion, 0), 0, 16);
4081 break;
4082
4083 default:
4084 LOG(FATAL) << "Unexpected type conversion from " << input_type
4085 << " to " << result_type;
4086 }
4087 break;
4088
4089 case Primitive::kPrimFloat:
4090 switch (input_type) {
4091 case Primitive::kPrimBoolean:
4092 // Boolean input is a result of code transformations.
4093 case Primitive::kPrimByte:
4094 case Primitive::kPrimShort:
4095 case Primitive::kPrimInt:
4096 case Primitive::kPrimChar: {
4097 // Processing a Dex `int-to-float' instruction.
4098 __ Vmov(OutputSRegister(conversion), InputRegisterAt(conversion, 0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01004099 __ Vcvt(F32, S32, OutputSRegister(conversion), OutputSRegister(conversion));
Scott Wakelingfe885462016-09-22 10:24:38 +01004100 break;
4101 }
4102
4103 case Primitive::kPrimLong:
4104 // Processing a Dex `long-to-float' instruction.
4105 codegen_->InvokeRuntime(kQuickL2f, conversion, conversion->GetDexPc());
4106 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
4107 break;
4108
4109 case Primitive::kPrimDouble:
4110 // Processing a Dex `double-to-float' instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01004111 __ Vcvt(F32, F64, OutputSRegister(conversion), DRegisterFrom(in));
Scott Wakelingfe885462016-09-22 10:24:38 +01004112 break;
4113
4114 default:
4115 LOG(FATAL) << "Unexpected type conversion from " << input_type
4116 << " to " << result_type;
4117 };
4118 break;
4119
4120 case Primitive::kPrimDouble:
4121 switch (input_type) {
4122 case Primitive::kPrimBoolean:
4123 // Boolean input is a result of code transformations.
4124 case Primitive::kPrimByte:
4125 case Primitive::kPrimShort:
4126 case Primitive::kPrimInt:
4127 case Primitive::kPrimChar: {
4128 // Processing a Dex `int-to-double' instruction.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004129 __ Vmov(LowSRegisterFrom(out), InputRegisterAt(conversion, 0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01004130 __ Vcvt(F64, S32, DRegisterFrom(out), LowSRegisterFrom(out));
Scott Wakelingfe885462016-09-22 10:24:38 +01004131 break;
4132 }
4133
4134 case Primitive::kPrimLong: {
4135 // Processing a Dex `long-to-double' instruction.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004136 vixl32::Register low = LowRegisterFrom(in);
4137 vixl32::Register high = HighRegisterFrom(in);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004138 vixl32::SRegister out_s = LowSRegisterFrom(out);
Scott Wakelingc34dba72016-10-03 10:14:44 +01004139 vixl32::DRegister out_d = DRegisterFrom(out);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004140 vixl32::SRegister temp_s = LowSRegisterFrom(locations->GetTemp(0));
Scott Wakelingc34dba72016-10-03 10:14:44 +01004141 vixl32::DRegister temp_d = DRegisterFrom(locations->GetTemp(0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01004142 vixl32::DRegister constant_d = DRegisterFrom(locations->GetTemp(1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004143
4144 // temp_d = int-to-double(high)
4145 __ Vmov(temp_s, high);
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01004146 __ Vcvt(F64, S32, temp_d, temp_s);
Scott Wakelingfe885462016-09-22 10:24:38 +01004147 // constant_d = k2Pow32EncodingForDouble
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004148 __ Vmov(constant_d, bit_cast<double, int64_t>(k2Pow32EncodingForDouble));
Scott Wakelingfe885462016-09-22 10:24:38 +01004149 // out_d = unsigned-to-double(low)
4150 __ Vmov(out_s, low);
4151 __ Vcvt(F64, U32, out_d, out_s);
4152 // out_d += temp_d * constant_d
4153 __ Vmla(F64, out_d, temp_d, constant_d);
4154 break;
4155 }
4156
4157 case Primitive::kPrimFloat:
4158 // Processing a Dex `float-to-double' instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01004159 __ Vcvt(F64, F32, DRegisterFrom(out), InputSRegisterAt(conversion, 0));
Scott Wakelingfe885462016-09-22 10:24:38 +01004160 break;
4161
4162 default:
4163 LOG(FATAL) << "Unexpected type conversion from " << input_type
4164 << " to " << result_type;
4165 };
4166 break;
4167
4168 default:
4169 LOG(FATAL) << "Unexpected type conversion from " << input_type
4170 << " to " << result_type;
4171 }
4172}
4173
4174void LocationsBuilderARMVIXL::VisitAdd(HAdd* add) {
4175 LocationSummary* locations =
4176 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
4177 switch (add->GetResultType()) {
4178 case Primitive::kPrimInt: {
4179 locations->SetInAt(0, Location::RequiresRegister());
4180 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
4181 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4182 break;
4183 }
4184
Scott Wakelingfe885462016-09-22 10:24:38 +01004185 case Primitive::kPrimLong: {
4186 locations->SetInAt(0, Location::RequiresRegister());
Anton Kirilovdda43962016-11-21 19:55:20 +00004187 locations->SetInAt(1, ArmEncodableConstantOrRegister(add->InputAt(1), ADD));
Scott Wakelingfe885462016-09-22 10:24:38 +01004188 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4189 break;
4190 }
4191
4192 case Primitive::kPrimFloat:
4193 case Primitive::kPrimDouble: {
4194 locations->SetInAt(0, Location::RequiresFpuRegister());
4195 locations->SetInAt(1, Location::RequiresFpuRegister());
4196 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4197 break;
4198 }
4199
4200 default:
4201 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
4202 }
4203}
4204
4205void InstructionCodeGeneratorARMVIXL::VisitAdd(HAdd* add) {
4206 LocationSummary* locations = add->GetLocations();
4207 Location out = locations->Out();
4208 Location first = locations->InAt(0);
4209 Location second = locations->InAt(1);
4210
4211 switch (add->GetResultType()) {
4212 case Primitive::kPrimInt: {
4213 __ Add(OutputRegister(add), InputRegisterAt(add, 0), InputOperandAt(add, 1));
4214 }
4215 break;
4216
Scott Wakelingfe885462016-09-22 10:24:38 +01004217 case Primitive::kPrimLong: {
Anton Kirilovdda43962016-11-21 19:55:20 +00004218 if (second.IsConstant()) {
4219 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
4220 GenerateAddLongConst(out, first, value);
4221 } else {
4222 DCHECK(second.IsRegisterPair());
4223 __ Adds(LowRegisterFrom(out), LowRegisterFrom(first), LowRegisterFrom(second));
4224 __ Adc(HighRegisterFrom(out), HighRegisterFrom(first), HighRegisterFrom(second));
4225 }
Scott Wakelingfe885462016-09-22 10:24:38 +01004226 break;
4227 }
4228
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004229 case Primitive::kPrimFloat:
Scott Wakelingfe885462016-09-22 10:24:38 +01004230 case Primitive::kPrimDouble:
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004231 __ Vadd(OutputVRegister(add), InputVRegisterAt(add, 0), InputVRegisterAt(add, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004232 break;
4233
4234 default:
4235 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
4236 }
4237}
4238
4239void LocationsBuilderARMVIXL::VisitSub(HSub* sub) {
4240 LocationSummary* locations =
4241 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
4242 switch (sub->GetResultType()) {
4243 case Primitive::kPrimInt: {
4244 locations->SetInAt(0, Location::RequiresRegister());
4245 locations->SetInAt(1, Location::RegisterOrConstant(sub->InputAt(1)));
4246 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4247 break;
4248 }
4249
Scott Wakelingfe885462016-09-22 10:24:38 +01004250 case Primitive::kPrimLong: {
4251 locations->SetInAt(0, Location::RequiresRegister());
Anton Kirilovdda43962016-11-21 19:55:20 +00004252 locations->SetInAt(1, ArmEncodableConstantOrRegister(sub->InputAt(1), SUB));
Scott Wakelingfe885462016-09-22 10:24:38 +01004253 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4254 break;
4255 }
4256 case Primitive::kPrimFloat:
4257 case Primitive::kPrimDouble: {
4258 locations->SetInAt(0, Location::RequiresFpuRegister());
4259 locations->SetInAt(1, Location::RequiresFpuRegister());
4260 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4261 break;
4262 }
4263 default:
4264 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
4265 }
4266}
4267
4268void InstructionCodeGeneratorARMVIXL::VisitSub(HSub* sub) {
4269 LocationSummary* locations = sub->GetLocations();
4270 Location out = locations->Out();
4271 Location first = locations->InAt(0);
4272 Location second = locations->InAt(1);
4273 switch (sub->GetResultType()) {
4274 case Primitive::kPrimInt: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004275 __ Sub(OutputRegister(sub), InputRegisterAt(sub, 0), InputOperandAt(sub, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004276 break;
4277 }
4278
Scott Wakelingfe885462016-09-22 10:24:38 +01004279 case Primitive::kPrimLong: {
Anton Kirilovdda43962016-11-21 19:55:20 +00004280 if (second.IsConstant()) {
4281 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
4282 GenerateAddLongConst(out, first, -value);
4283 } else {
4284 DCHECK(second.IsRegisterPair());
4285 __ Subs(LowRegisterFrom(out), LowRegisterFrom(first), LowRegisterFrom(second));
4286 __ Sbc(HighRegisterFrom(out), HighRegisterFrom(first), HighRegisterFrom(second));
4287 }
Scott Wakelingfe885462016-09-22 10:24:38 +01004288 break;
4289 }
4290
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004291 case Primitive::kPrimFloat:
4292 case Primitive::kPrimDouble:
4293 __ Vsub(OutputVRegister(sub), InputVRegisterAt(sub, 0), InputVRegisterAt(sub, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004294 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01004295
4296 default:
4297 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
4298 }
4299}
4300
4301void LocationsBuilderARMVIXL::VisitMul(HMul* mul) {
4302 LocationSummary* locations =
4303 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
4304 switch (mul->GetResultType()) {
4305 case Primitive::kPrimInt:
4306 case Primitive::kPrimLong: {
4307 locations->SetInAt(0, Location::RequiresRegister());
4308 locations->SetInAt(1, Location::RequiresRegister());
4309 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4310 break;
4311 }
4312
4313 case Primitive::kPrimFloat:
4314 case Primitive::kPrimDouble: {
4315 locations->SetInAt(0, Location::RequiresFpuRegister());
4316 locations->SetInAt(1, Location::RequiresFpuRegister());
4317 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4318 break;
4319 }
4320
4321 default:
4322 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4323 }
4324}
4325
4326void InstructionCodeGeneratorARMVIXL::VisitMul(HMul* mul) {
4327 LocationSummary* locations = mul->GetLocations();
4328 Location out = locations->Out();
4329 Location first = locations->InAt(0);
4330 Location second = locations->InAt(1);
4331 switch (mul->GetResultType()) {
4332 case Primitive::kPrimInt: {
4333 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
4334 break;
4335 }
4336 case Primitive::kPrimLong: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004337 vixl32::Register out_hi = HighRegisterFrom(out);
4338 vixl32::Register out_lo = LowRegisterFrom(out);
4339 vixl32::Register in1_hi = HighRegisterFrom(first);
4340 vixl32::Register in1_lo = LowRegisterFrom(first);
4341 vixl32::Register in2_hi = HighRegisterFrom(second);
4342 vixl32::Register in2_lo = LowRegisterFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01004343
4344 // Extra checks to protect caused by the existence of R1_R2.
4345 // The algorithm is wrong if out.hi is either in1.lo or in2.lo:
4346 // (e.g. in1=r0_r1, in2=r2_r3 and out=r1_r2);
Anton Kirilov644032c2016-12-06 17:51:43 +00004347 DCHECK(!out_hi.Is(in1_lo));
4348 DCHECK(!out_hi.Is(in2_lo));
Scott Wakelingfe885462016-09-22 10:24:38 +01004349
4350 // input: in1 - 64 bits, in2 - 64 bits
4351 // output: out
4352 // formula: out.hi : out.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
4353 // parts: out.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
4354 // parts: out.lo = (in1.lo * in2.lo)[31:0]
4355
4356 UseScratchRegisterScope temps(GetVIXLAssembler());
4357 vixl32::Register temp = temps.Acquire();
4358 // temp <- in1.lo * in2.hi
4359 __ Mul(temp, in1_lo, in2_hi);
4360 // out.hi <- in1.lo * in2.hi + in1.hi * in2.lo
4361 __ Mla(out_hi, in1_hi, in2_lo, temp);
4362 // out.lo <- (in1.lo * in2.lo)[31:0];
4363 __ Umull(out_lo, temp, in1_lo, in2_lo);
4364 // out.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004365 __ Add(out_hi, out_hi, temp);
Scott Wakelingfe885462016-09-22 10:24:38 +01004366 break;
4367 }
4368
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004369 case Primitive::kPrimFloat:
4370 case Primitive::kPrimDouble:
4371 __ Vmul(OutputVRegister(mul), InputVRegisterAt(mul, 0), InputVRegisterAt(mul, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004372 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01004373
4374 default:
4375 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4376 }
4377}
4378
Scott Wakelingfe885462016-09-22 10:24:38 +01004379void InstructionCodeGeneratorARMVIXL::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
4380 DCHECK(instruction->IsDiv() || instruction->IsRem());
4381 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
4382
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004383 Location second = instruction->GetLocations()->InAt(1);
Scott Wakelingfe885462016-09-22 10:24:38 +01004384 DCHECK(second.IsConstant());
4385
4386 vixl32::Register out = OutputRegister(instruction);
4387 vixl32::Register dividend = InputRegisterAt(instruction, 0);
Anton Kirilov644032c2016-12-06 17:51:43 +00004388 int32_t imm = Int32ConstantFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01004389 DCHECK(imm == 1 || imm == -1);
4390
4391 if (instruction->IsRem()) {
4392 __ Mov(out, 0);
4393 } else {
4394 if (imm == 1) {
4395 __ Mov(out, dividend);
4396 } else {
4397 __ Rsb(out, dividend, 0);
4398 }
4399 }
4400}
4401
4402void InstructionCodeGeneratorARMVIXL::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
4403 DCHECK(instruction->IsDiv() || instruction->IsRem());
4404 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
4405
4406 LocationSummary* locations = instruction->GetLocations();
4407 Location second = locations->InAt(1);
4408 DCHECK(second.IsConstant());
4409
4410 vixl32::Register out = OutputRegister(instruction);
4411 vixl32::Register dividend = InputRegisterAt(instruction, 0);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004412 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
Anton Kirilov644032c2016-12-06 17:51:43 +00004413 int32_t imm = Int32ConstantFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01004414 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
4415 int ctz_imm = CTZ(abs_imm);
4416
4417 if (ctz_imm == 1) {
4418 __ Lsr(temp, dividend, 32 - ctz_imm);
4419 } else {
4420 __ Asr(temp, dividend, 31);
4421 __ Lsr(temp, temp, 32 - ctz_imm);
4422 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004423 __ Add(out, temp, dividend);
Scott Wakelingfe885462016-09-22 10:24:38 +01004424
4425 if (instruction->IsDiv()) {
4426 __ Asr(out, out, ctz_imm);
4427 if (imm < 0) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004428 __ Rsb(out, out, 0);
Scott Wakelingfe885462016-09-22 10:24:38 +01004429 }
4430 } else {
4431 __ Ubfx(out, out, 0, ctz_imm);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004432 __ Sub(out, out, temp);
Scott Wakelingfe885462016-09-22 10:24:38 +01004433 }
4434}
4435
4436void InstructionCodeGeneratorARMVIXL::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
4437 DCHECK(instruction->IsDiv() || instruction->IsRem());
4438 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
4439
4440 LocationSummary* locations = instruction->GetLocations();
4441 Location second = locations->InAt(1);
4442 DCHECK(second.IsConstant());
4443
4444 vixl32::Register out = OutputRegister(instruction);
4445 vixl32::Register dividend = InputRegisterAt(instruction, 0);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004446 vixl32::Register temp1 = RegisterFrom(locations->GetTemp(0));
4447 vixl32::Register temp2 = RegisterFrom(locations->GetTemp(1));
Scott Wakelingb77051e2016-11-21 19:46:00 +00004448 int32_t imm = Int32ConstantFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01004449
4450 int64_t magic;
4451 int shift;
4452 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
4453
Anton Kirilovdda43962016-11-21 19:55:20 +00004454 // TODO(VIXL): Change the static cast to Operand::From() after VIXL is fixed.
4455 __ Mov(temp1, static_cast<int32_t>(magic));
Scott Wakelingfe885462016-09-22 10:24:38 +01004456 __ Smull(temp2, temp1, dividend, temp1);
4457
4458 if (imm > 0 && magic < 0) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004459 __ Add(temp1, temp1, dividend);
Scott Wakelingfe885462016-09-22 10:24:38 +01004460 } else if (imm < 0 && magic > 0) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004461 __ Sub(temp1, temp1, dividend);
Scott Wakelingfe885462016-09-22 10:24:38 +01004462 }
4463
4464 if (shift != 0) {
4465 __ Asr(temp1, temp1, shift);
4466 }
4467
4468 if (instruction->IsDiv()) {
4469 __ Sub(out, temp1, Operand(temp1, vixl32::Shift(ASR), 31));
4470 } else {
4471 __ Sub(temp1, temp1, Operand(temp1, vixl32::Shift(ASR), 31));
4472 // TODO: Strength reduction for mls.
4473 __ Mov(temp2, imm);
4474 __ Mls(out, temp1, temp2, dividend);
4475 }
4476}
4477
4478void InstructionCodeGeneratorARMVIXL::GenerateDivRemConstantIntegral(
4479 HBinaryOperation* instruction) {
4480 DCHECK(instruction->IsDiv() || instruction->IsRem());
4481 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
4482
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004483 Location second = instruction->GetLocations()->InAt(1);
Scott Wakelingfe885462016-09-22 10:24:38 +01004484 DCHECK(second.IsConstant());
4485
Anton Kirilov644032c2016-12-06 17:51:43 +00004486 int32_t imm = Int32ConstantFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01004487 if (imm == 0) {
4488 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
4489 } else if (imm == 1 || imm == -1) {
4490 DivRemOneOrMinusOne(instruction);
4491 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
4492 DivRemByPowerOfTwo(instruction);
4493 } else {
4494 DCHECK(imm <= -2 || imm >= 2);
4495 GenerateDivRemWithAnyConstant(instruction);
4496 }
4497}
4498
4499void LocationsBuilderARMVIXL::VisitDiv(HDiv* div) {
4500 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
4501 if (div->GetResultType() == Primitive::kPrimLong) {
4502 // pLdiv runtime call.
4503 call_kind = LocationSummary::kCallOnMainOnly;
4504 } else if (div->GetResultType() == Primitive::kPrimInt && div->InputAt(1)->IsConstant()) {
4505 // sdiv will be replaced by other instruction sequence.
4506 } else if (div->GetResultType() == Primitive::kPrimInt &&
4507 !codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4508 // pIdivmod runtime call.
4509 call_kind = LocationSummary::kCallOnMainOnly;
4510 }
4511
4512 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
4513
4514 switch (div->GetResultType()) {
4515 case Primitive::kPrimInt: {
4516 if (div->InputAt(1)->IsConstant()) {
4517 locations->SetInAt(0, Location::RequiresRegister());
4518 locations->SetInAt(1, Location::ConstantLocation(div->InputAt(1)->AsConstant()));
4519 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Anton Kirilov644032c2016-12-06 17:51:43 +00004520 int32_t value = Int32ConstantFrom(div->InputAt(1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004521 if (value == 1 || value == 0 || value == -1) {
4522 // No temp register required.
4523 } else {
4524 locations->AddTemp(Location::RequiresRegister());
4525 if (!IsPowerOfTwo(AbsOrMin(value))) {
4526 locations->AddTemp(Location::RequiresRegister());
4527 }
4528 }
4529 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4530 locations->SetInAt(0, Location::RequiresRegister());
4531 locations->SetInAt(1, Location::RequiresRegister());
4532 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4533 } else {
Artem Serov551b28f2016-10-18 19:11:30 +01004534 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4535 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4536 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
Roland Levillain5e8d5f02016-10-18 18:03:43 +01004537 // Note: divmod will compute both the quotient and the remainder as the pair R0 and R1, but
Artem Serov551b28f2016-10-18 19:11:30 +01004538 // we only need the former.
4539 locations->SetOut(LocationFrom(r0));
Scott Wakelingfe885462016-09-22 10:24:38 +01004540 }
4541 break;
4542 }
4543 case Primitive::kPrimLong: {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01004544 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4545 locations->SetInAt(0, LocationFrom(
4546 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
4547 locations->SetInAt(1, LocationFrom(
4548 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
4549 locations->SetOut(LocationFrom(r0, r1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004550 break;
4551 }
4552 case Primitive::kPrimFloat:
4553 case Primitive::kPrimDouble: {
4554 locations->SetInAt(0, Location::RequiresFpuRegister());
4555 locations->SetInAt(1, Location::RequiresFpuRegister());
4556 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4557 break;
4558 }
4559
4560 default:
4561 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
4562 }
4563}
4564
4565void InstructionCodeGeneratorARMVIXL::VisitDiv(HDiv* div) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01004566 Location lhs = div->GetLocations()->InAt(0);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004567 Location rhs = div->GetLocations()->InAt(1);
Scott Wakelingfe885462016-09-22 10:24:38 +01004568
4569 switch (div->GetResultType()) {
4570 case Primitive::kPrimInt: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004571 if (rhs.IsConstant()) {
Scott Wakelingfe885462016-09-22 10:24:38 +01004572 GenerateDivRemConstantIntegral(div);
4573 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4574 __ Sdiv(OutputRegister(div), InputRegisterAt(div, 0), InputRegisterAt(div, 1));
4575 } else {
Artem Serov551b28f2016-10-18 19:11:30 +01004576 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4577 DCHECK(calling_convention.GetRegisterAt(0).Is(RegisterFrom(lhs)));
4578 DCHECK(calling_convention.GetRegisterAt(1).Is(RegisterFrom(rhs)));
4579 DCHECK(r0.Is(OutputRegister(div)));
4580
4581 codegen_->InvokeRuntime(kQuickIdivmod, div, div->GetDexPc());
4582 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
Scott Wakelingfe885462016-09-22 10:24:38 +01004583 }
4584 break;
4585 }
4586
4587 case Primitive::kPrimLong: {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01004588 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4589 DCHECK(calling_convention.GetRegisterAt(0).Is(LowRegisterFrom(lhs)));
4590 DCHECK(calling_convention.GetRegisterAt(1).Is(HighRegisterFrom(lhs)));
4591 DCHECK(calling_convention.GetRegisterAt(2).Is(LowRegisterFrom(rhs)));
4592 DCHECK(calling_convention.GetRegisterAt(3).Is(HighRegisterFrom(rhs)));
4593 DCHECK(LowRegisterFrom(div->GetLocations()->Out()).Is(r0));
4594 DCHECK(HighRegisterFrom(div->GetLocations()->Out()).Is(r1));
4595
4596 codegen_->InvokeRuntime(kQuickLdiv, div, div->GetDexPc());
4597 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Scott Wakelingfe885462016-09-22 10:24:38 +01004598 break;
4599 }
4600
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004601 case Primitive::kPrimFloat:
4602 case Primitive::kPrimDouble:
4603 __ Vdiv(OutputVRegister(div), InputVRegisterAt(div, 0), InputVRegisterAt(div, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004604 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01004605
4606 default:
4607 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
4608 }
4609}
4610
Artem Serov551b28f2016-10-18 19:11:30 +01004611void LocationsBuilderARMVIXL::VisitRem(HRem* rem) {
4612 Primitive::Type type = rem->GetResultType();
4613
4614 // Most remainders are implemented in the runtime.
4615 LocationSummary::CallKind call_kind = LocationSummary::kCallOnMainOnly;
4616 if (rem->GetResultType() == Primitive::kPrimInt && rem->InputAt(1)->IsConstant()) {
4617 // sdiv will be replaced by other instruction sequence.
4618 call_kind = LocationSummary::kNoCall;
4619 } else if ((rem->GetResultType() == Primitive::kPrimInt)
4620 && codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4621 // Have hardware divide instruction for int, do it with three instructions.
4622 call_kind = LocationSummary::kNoCall;
4623 }
4624
4625 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
4626
4627 switch (type) {
4628 case Primitive::kPrimInt: {
4629 if (rem->InputAt(1)->IsConstant()) {
4630 locations->SetInAt(0, Location::RequiresRegister());
4631 locations->SetInAt(1, Location::ConstantLocation(rem->InputAt(1)->AsConstant()));
4632 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Anton Kirilov644032c2016-12-06 17:51:43 +00004633 int32_t value = Int32ConstantFrom(rem->InputAt(1));
Artem Serov551b28f2016-10-18 19:11:30 +01004634 if (value == 1 || value == 0 || value == -1) {
4635 // No temp register required.
4636 } else {
4637 locations->AddTemp(Location::RequiresRegister());
4638 if (!IsPowerOfTwo(AbsOrMin(value))) {
4639 locations->AddTemp(Location::RequiresRegister());
4640 }
4641 }
4642 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4643 locations->SetInAt(0, Location::RequiresRegister());
4644 locations->SetInAt(1, Location::RequiresRegister());
4645 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4646 locations->AddTemp(Location::RequiresRegister());
4647 } else {
4648 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4649 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4650 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
Roland Levillain5e8d5f02016-10-18 18:03:43 +01004651 // Note: divmod will compute both the quotient and the remainder as the pair R0 and R1, but
Artem Serov551b28f2016-10-18 19:11:30 +01004652 // we only need the latter.
4653 locations->SetOut(LocationFrom(r1));
4654 }
4655 break;
4656 }
4657 case Primitive::kPrimLong: {
4658 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4659 locations->SetInAt(0, LocationFrom(
4660 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
4661 locations->SetInAt(1, LocationFrom(
4662 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
4663 // The runtime helper puts the output in R2,R3.
4664 locations->SetOut(LocationFrom(r2, r3));
4665 break;
4666 }
4667 case Primitive::kPrimFloat: {
4668 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4669 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
4670 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
4671 locations->SetOut(LocationFrom(s0));
4672 break;
4673 }
4674
4675 case Primitive::kPrimDouble: {
4676 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4677 locations->SetInAt(0, LocationFrom(
4678 calling_convention.GetFpuRegisterAt(0), calling_convention.GetFpuRegisterAt(1)));
4679 locations->SetInAt(1, LocationFrom(
4680 calling_convention.GetFpuRegisterAt(2), calling_convention.GetFpuRegisterAt(3)));
4681 locations->SetOut(LocationFrom(s0, s1));
4682 break;
4683 }
4684
4685 default:
4686 LOG(FATAL) << "Unexpected rem type " << type;
4687 }
4688}
4689
4690void InstructionCodeGeneratorARMVIXL::VisitRem(HRem* rem) {
4691 LocationSummary* locations = rem->GetLocations();
4692 Location second = locations->InAt(1);
4693
4694 Primitive::Type type = rem->GetResultType();
4695 switch (type) {
4696 case Primitive::kPrimInt: {
4697 vixl32::Register reg1 = InputRegisterAt(rem, 0);
4698 vixl32::Register out_reg = OutputRegister(rem);
4699 if (second.IsConstant()) {
4700 GenerateDivRemConstantIntegral(rem);
4701 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4702 vixl32::Register reg2 = RegisterFrom(second);
4703 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
4704
4705 // temp = reg1 / reg2 (integer division)
4706 // dest = reg1 - temp * reg2
4707 __ Sdiv(temp, reg1, reg2);
4708 __ Mls(out_reg, temp, reg2, reg1);
4709 } else {
4710 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4711 DCHECK(reg1.Is(calling_convention.GetRegisterAt(0)));
4712 DCHECK(RegisterFrom(second).Is(calling_convention.GetRegisterAt(1)));
4713 DCHECK(out_reg.Is(r1));
4714
4715 codegen_->InvokeRuntime(kQuickIdivmod, rem, rem->GetDexPc());
4716 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
4717 }
4718 break;
4719 }
4720
4721 case Primitive::kPrimLong: {
4722 codegen_->InvokeRuntime(kQuickLmod, rem, rem->GetDexPc());
4723 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
4724 break;
4725 }
4726
4727 case Primitive::kPrimFloat: {
4728 codegen_->InvokeRuntime(kQuickFmodf, rem, rem->GetDexPc());
4729 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
4730 break;
4731 }
4732
4733 case Primitive::kPrimDouble: {
4734 codegen_->InvokeRuntime(kQuickFmod, rem, rem->GetDexPc());
4735 CheckEntrypointTypes<kQuickFmod, double, double, double>();
4736 break;
4737 }
4738
4739 default:
4740 LOG(FATAL) << "Unexpected rem type " << type;
4741 }
4742}
4743
4744
Scott Wakelingfe885462016-09-22 10:24:38 +01004745void LocationsBuilderARMVIXL::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Artem Serov657022c2016-11-23 14:19:38 +00004746 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Scott Wakelingfe885462016-09-22 10:24:38 +01004747 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Scott Wakelingfe885462016-09-22 10:24:38 +01004748}
4749
4750void InstructionCodeGeneratorARMVIXL::VisitDivZeroCheck(HDivZeroCheck* instruction) {
4751 DivZeroCheckSlowPathARMVIXL* slow_path =
4752 new (GetGraph()->GetArena()) DivZeroCheckSlowPathARMVIXL(instruction);
4753 codegen_->AddSlowPath(slow_path);
4754
4755 LocationSummary* locations = instruction->GetLocations();
4756 Location value = locations->InAt(0);
4757
4758 switch (instruction->GetType()) {
4759 case Primitive::kPrimBoolean:
4760 case Primitive::kPrimByte:
4761 case Primitive::kPrimChar:
4762 case Primitive::kPrimShort:
4763 case Primitive::kPrimInt: {
4764 if (value.IsRegister()) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00004765 __ CompareAndBranchIfZero(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
Scott Wakelingfe885462016-09-22 10:24:38 +01004766 } else {
4767 DCHECK(value.IsConstant()) << value;
Anton Kirilov644032c2016-12-06 17:51:43 +00004768 if (Int32ConstantFrom(value) == 0) {
Scott Wakelingfe885462016-09-22 10:24:38 +01004769 __ B(slow_path->GetEntryLabel());
4770 }
4771 }
4772 break;
4773 }
4774 case Primitive::kPrimLong: {
4775 if (value.IsRegisterPair()) {
4776 UseScratchRegisterScope temps(GetVIXLAssembler());
4777 vixl32::Register temp = temps.Acquire();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004778 __ Orrs(temp, LowRegisterFrom(value), HighRegisterFrom(value));
Scott Wakelingfe885462016-09-22 10:24:38 +01004779 __ B(eq, slow_path->GetEntryLabel());
4780 } else {
4781 DCHECK(value.IsConstant()) << value;
Anton Kirilov644032c2016-12-06 17:51:43 +00004782 if (Int64ConstantFrom(value) == 0) {
Scott Wakelingfe885462016-09-22 10:24:38 +01004783 __ B(slow_path->GetEntryLabel());
4784 }
4785 }
4786 break;
4787 }
4788 default:
4789 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
4790 }
4791}
4792
Artem Serov02109dd2016-09-23 17:17:54 +01004793void InstructionCodeGeneratorARMVIXL::HandleIntegerRotate(HRor* ror) {
4794 LocationSummary* locations = ror->GetLocations();
4795 vixl32::Register in = InputRegisterAt(ror, 0);
4796 Location rhs = locations->InAt(1);
4797 vixl32::Register out = OutputRegister(ror);
4798
4799 if (rhs.IsConstant()) {
4800 // Arm32 and Thumb2 assemblers require a rotation on the interval [1,31],
4801 // so map all rotations to a +ve. equivalent in that range.
4802 // (e.g. left *or* right by -2 bits == 30 bits in the same direction.)
4803 uint32_t rot = CodeGenerator::GetInt32ValueOf(rhs.GetConstant()) & 0x1F;
4804 if (rot) {
4805 // Rotate, mapping left rotations to right equivalents if necessary.
4806 // (e.g. left by 2 bits == right by 30.)
4807 __ Ror(out, in, rot);
4808 } else if (!out.Is(in)) {
4809 __ Mov(out, in);
4810 }
4811 } else {
4812 __ Ror(out, in, RegisterFrom(rhs));
4813 }
4814}
4815
4816// Gain some speed by mapping all Long rotates onto equivalent pairs of Integer
4817// rotates by swapping input regs (effectively rotating by the first 32-bits of
4818// a larger rotation) or flipping direction (thus treating larger right/left
4819// rotations as sub-word sized rotations in the other direction) as appropriate.
4820void InstructionCodeGeneratorARMVIXL::HandleLongRotate(HRor* ror) {
4821 LocationSummary* locations = ror->GetLocations();
4822 vixl32::Register in_reg_lo = LowRegisterFrom(locations->InAt(0));
4823 vixl32::Register in_reg_hi = HighRegisterFrom(locations->InAt(0));
4824 Location rhs = locations->InAt(1);
4825 vixl32::Register out_reg_lo = LowRegisterFrom(locations->Out());
4826 vixl32::Register out_reg_hi = HighRegisterFrom(locations->Out());
4827
4828 if (rhs.IsConstant()) {
4829 uint64_t rot = CodeGenerator::GetInt64ValueOf(rhs.GetConstant());
4830 // Map all rotations to +ve. equivalents on the interval [0,63].
4831 rot &= kMaxLongShiftDistance;
4832 // For rotates over a word in size, 'pre-rotate' by 32-bits to keep rotate
4833 // logic below to a simple pair of binary orr.
4834 // (e.g. 34 bits == in_reg swap + 2 bits right.)
4835 if (rot >= kArmBitsPerWord) {
4836 rot -= kArmBitsPerWord;
4837 std::swap(in_reg_hi, in_reg_lo);
4838 }
4839 // Rotate, or mov to out for zero or word size rotations.
4840 if (rot != 0u) {
Scott Wakelingb77051e2016-11-21 19:46:00 +00004841 __ Lsr(out_reg_hi, in_reg_hi, Operand::From(rot));
Artem Serov02109dd2016-09-23 17:17:54 +01004842 __ Orr(out_reg_hi, out_reg_hi, Operand(in_reg_lo, ShiftType::LSL, kArmBitsPerWord - rot));
Scott Wakelingb77051e2016-11-21 19:46:00 +00004843 __ Lsr(out_reg_lo, in_reg_lo, Operand::From(rot));
Artem Serov02109dd2016-09-23 17:17:54 +01004844 __ Orr(out_reg_lo, out_reg_lo, Operand(in_reg_hi, ShiftType::LSL, kArmBitsPerWord - rot));
4845 } else {
4846 __ Mov(out_reg_lo, in_reg_lo);
4847 __ Mov(out_reg_hi, in_reg_hi);
4848 }
4849 } else {
4850 vixl32::Register shift_right = RegisterFrom(locations->GetTemp(0));
4851 vixl32::Register shift_left = RegisterFrom(locations->GetTemp(1));
4852 vixl32::Label end;
4853 vixl32::Label shift_by_32_plus_shift_right;
Anton Kirilov6f644202017-02-27 18:29:45 +00004854 vixl32::Label* final_label = codegen_->GetFinalLabel(ror, &end);
Artem Serov02109dd2016-09-23 17:17:54 +01004855
4856 __ And(shift_right, RegisterFrom(rhs), 0x1F);
4857 __ Lsrs(shift_left, RegisterFrom(rhs), 6);
Scott Wakelingbffdc702016-12-07 17:46:03 +00004858 __ Rsb(LeaveFlags, shift_left, shift_right, Operand::From(kArmBitsPerWord));
Artem Serov517d9f62016-12-12 15:51:15 +00004859 __ B(cc, &shift_by_32_plus_shift_right, /* far_target */ false);
Artem Serov02109dd2016-09-23 17:17:54 +01004860
4861 // out_reg_hi = (reg_hi << shift_left) | (reg_lo >> shift_right).
4862 // out_reg_lo = (reg_lo << shift_left) | (reg_hi >> shift_right).
4863 __ Lsl(out_reg_hi, in_reg_hi, shift_left);
4864 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
4865 __ Add(out_reg_hi, out_reg_hi, out_reg_lo);
4866 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
4867 __ Lsr(shift_left, in_reg_hi, shift_right);
4868 __ Add(out_reg_lo, out_reg_lo, shift_left);
Anton Kirilov6f644202017-02-27 18:29:45 +00004869 __ B(final_label);
Artem Serov02109dd2016-09-23 17:17:54 +01004870
4871 __ Bind(&shift_by_32_plus_shift_right); // Shift by 32+shift_right.
4872 // out_reg_hi = (reg_hi >> shift_right) | (reg_lo << shift_left).
4873 // out_reg_lo = (reg_lo >> shift_right) | (reg_hi << shift_left).
4874 __ Lsr(out_reg_hi, in_reg_hi, shift_right);
4875 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
4876 __ Add(out_reg_hi, out_reg_hi, out_reg_lo);
4877 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
4878 __ Lsl(shift_right, in_reg_hi, shift_left);
4879 __ Add(out_reg_lo, out_reg_lo, shift_right);
4880
Anton Kirilov6f644202017-02-27 18:29:45 +00004881 if (end.IsReferenced()) {
4882 __ Bind(&end);
4883 }
Artem Serov02109dd2016-09-23 17:17:54 +01004884 }
4885}
4886
4887void LocationsBuilderARMVIXL::VisitRor(HRor* ror) {
4888 LocationSummary* locations =
4889 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
4890 switch (ror->GetResultType()) {
4891 case Primitive::kPrimInt: {
4892 locations->SetInAt(0, Location::RequiresRegister());
4893 locations->SetInAt(1, Location::RegisterOrConstant(ror->InputAt(1)));
4894 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4895 break;
4896 }
4897 case Primitive::kPrimLong: {
4898 locations->SetInAt(0, Location::RequiresRegister());
4899 if (ror->InputAt(1)->IsConstant()) {
4900 locations->SetInAt(1, Location::ConstantLocation(ror->InputAt(1)->AsConstant()));
4901 } else {
4902 locations->SetInAt(1, Location::RequiresRegister());
4903 locations->AddTemp(Location::RequiresRegister());
4904 locations->AddTemp(Location::RequiresRegister());
4905 }
4906 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
4907 break;
4908 }
4909 default:
4910 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
4911 }
4912}
4913
4914void InstructionCodeGeneratorARMVIXL::VisitRor(HRor* ror) {
4915 Primitive::Type type = ror->GetResultType();
4916 switch (type) {
4917 case Primitive::kPrimInt: {
4918 HandleIntegerRotate(ror);
4919 break;
4920 }
4921 case Primitive::kPrimLong: {
4922 HandleLongRotate(ror);
4923 break;
4924 }
4925 default:
4926 LOG(FATAL) << "Unexpected operation type " << type;
4927 UNREACHABLE();
4928 }
4929}
4930
Artem Serov02d37832016-10-25 15:25:33 +01004931void LocationsBuilderARMVIXL::HandleShift(HBinaryOperation* op) {
4932 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
4933
4934 LocationSummary* locations =
4935 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
4936
4937 switch (op->GetResultType()) {
4938 case Primitive::kPrimInt: {
4939 locations->SetInAt(0, Location::RequiresRegister());
4940 if (op->InputAt(1)->IsConstant()) {
4941 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
4942 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4943 } else {
4944 locations->SetInAt(1, Location::RequiresRegister());
4945 // Make the output overlap, as it will be used to hold the masked
4946 // second input.
4947 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
4948 }
4949 break;
4950 }
4951 case Primitive::kPrimLong: {
4952 locations->SetInAt(0, Location::RequiresRegister());
4953 if (op->InputAt(1)->IsConstant()) {
4954 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
4955 // For simplicity, use kOutputOverlap even though we only require that low registers
4956 // don't clash with high registers which the register allocator currently guarantees.
4957 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
4958 } else {
4959 locations->SetInAt(1, Location::RequiresRegister());
4960 locations->AddTemp(Location::RequiresRegister());
4961 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
4962 }
4963 break;
4964 }
4965 default:
4966 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
4967 }
4968}
4969
4970void InstructionCodeGeneratorARMVIXL::HandleShift(HBinaryOperation* op) {
4971 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
4972
4973 LocationSummary* locations = op->GetLocations();
4974 Location out = locations->Out();
4975 Location first = locations->InAt(0);
4976 Location second = locations->InAt(1);
4977
4978 Primitive::Type type = op->GetResultType();
4979 switch (type) {
4980 case Primitive::kPrimInt: {
4981 vixl32::Register out_reg = OutputRegister(op);
4982 vixl32::Register first_reg = InputRegisterAt(op, 0);
4983 if (second.IsRegister()) {
4984 vixl32::Register second_reg = RegisterFrom(second);
4985 // ARM doesn't mask the shift count so we need to do it ourselves.
4986 __ And(out_reg, second_reg, kMaxIntShiftDistance);
4987 if (op->IsShl()) {
4988 __ Lsl(out_reg, first_reg, out_reg);
4989 } else if (op->IsShr()) {
4990 __ Asr(out_reg, first_reg, out_reg);
4991 } else {
4992 __ Lsr(out_reg, first_reg, out_reg);
4993 }
4994 } else {
Anton Kirilov644032c2016-12-06 17:51:43 +00004995 int32_t cst = Int32ConstantFrom(second);
Artem Serov02d37832016-10-25 15:25:33 +01004996 uint32_t shift_value = cst & kMaxIntShiftDistance;
4997 if (shift_value == 0) { // ARM does not support shifting with 0 immediate.
4998 __ Mov(out_reg, first_reg);
4999 } else if (op->IsShl()) {
5000 __ Lsl(out_reg, first_reg, shift_value);
5001 } else if (op->IsShr()) {
5002 __ Asr(out_reg, first_reg, shift_value);
5003 } else {
5004 __ Lsr(out_reg, first_reg, shift_value);
5005 }
5006 }
5007 break;
5008 }
5009 case Primitive::kPrimLong: {
5010 vixl32::Register o_h = HighRegisterFrom(out);
5011 vixl32::Register o_l = LowRegisterFrom(out);
5012
5013 vixl32::Register high = HighRegisterFrom(first);
5014 vixl32::Register low = LowRegisterFrom(first);
5015
5016 if (second.IsRegister()) {
5017 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
5018
5019 vixl32::Register second_reg = RegisterFrom(second);
5020
5021 if (op->IsShl()) {
5022 __ And(o_l, second_reg, kMaxLongShiftDistance);
5023 // Shift the high part
5024 __ Lsl(o_h, high, o_l);
5025 // Shift the low part and `or` what overflew on the high part
Scott Wakelingb77051e2016-11-21 19:46:00 +00005026 __ Rsb(temp, o_l, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01005027 __ Lsr(temp, low, temp);
5028 __ Orr(o_h, o_h, temp);
5029 // If the shift is > 32 bits, override the high part
Scott Wakelingb77051e2016-11-21 19:46:00 +00005030 __ Subs(temp, o_l, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01005031 {
Artem Serov0fb37192016-12-06 18:13:40 +00005032 ExactAssemblyScope guard(GetVIXLAssembler(),
5033 2 * vixl32::kMaxInstructionSizeInBytes,
5034 CodeBufferCheckScope::kMaximumSize);
Artem Serov02d37832016-10-25 15:25:33 +01005035 __ it(pl);
5036 __ lsl(pl, o_h, low, temp);
5037 }
5038 // Shift the low part
5039 __ Lsl(o_l, low, o_l);
5040 } else if (op->IsShr()) {
5041 __ And(o_h, second_reg, kMaxLongShiftDistance);
5042 // Shift the low part
5043 __ Lsr(o_l, low, o_h);
5044 // Shift the high part and `or` what underflew on the low part
Scott Wakelingb77051e2016-11-21 19:46:00 +00005045 __ Rsb(temp, o_h, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01005046 __ Lsl(temp, high, temp);
5047 __ Orr(o_l, o_l, temp);
5048 // If the shift is > 32 bits, override the low part
Scott Wakelingb77051e2016-11-21 19:46:00 +00005049 __ Subs(temp, o_h, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01005050 {
Artem Serov0fb37192016-12-06 18:13:40 +00005051 ExactAssemblyScope guard(GetVIXLAssembler(),
5052 2 * vixl32::kMaxInstructionSizeInBytes,
5053 CodeBufferCheckScope::kMaximumSize);
Artem Serov02d37832016-10-25 15:25:33 +01005054 __ it(pl);
5055 __ asr(pl, o_l, high, temp);
5056 }
5057 // Shift the high part
5058 __ Asr(o_h, high, o_h);
5059 } else {
5060 __ And(o_h, second_reg, kMaxLongShiftDistance);
5061 // same as Shr except we use `Lsr`s and not `Asr`s
5062 __ Lsr(o_l, low, o_h);
Scott Wakelingb77051e2016-11-21 19:46:00 +00005063 __ Rsb(temp, o_h, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01005064 __ Lsl(temp, high, temp);
5065 __ Orr(o_l, o_l, temp);
Scott Wakelingb77051e2016-11-21 19:46:00 +00005066 __ Subs(temp, o_h, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01005067 {
Artem Serov0fb37192016-12-06 18:13:40 +00005068 ExactAssemblyScope guard(GetVIXLAssembler(),
5069 2 * vixl32::kMaxInstructionSizeInBytes,
5070 CodeBufferCheckScope::kMaximumSize);
Artem Serov02d37832016-10-25 15:25:33 +01005071 __ it(pl);
5072 __ lsr(pl, o_l, high, temp);
5073 }
5074 __ Lsr(o_h, high, o_h);
5075 }
5076 } else {
5077 // Register allocator doesn't create partial overlap.
5078 DCHECK(!o_l.Is(high));
5079 DCHECK(!o_h.Is(low));
Anton Kirilov644032c2016-12-06 17:51:43 +00005080 int32_t cst = Int32ConstantFrom(second);
Artem Serov02d37832016-10-25 15:25:33 +01005081 uint32_t shift_value = cst & kMaxLongShiftDistance;
5082 if (shift_value > 32) {
5083 if (op->IsShl()) {
5084 __ Lsl(o_h, low, shift_value - 32);
5085 __ Mov(o_l, 0);
5086 } else if (op->IsShr()) {
5087 __ Asr(o_l, high, shift_value - 32);
5088 __ Asr(o_h, high, 31);
5089 } else {
5090 __ Lsr(o_l, high, shift_value - 32);
5091 __ Mov(o_h, 0);
5092 }
5093 } else if (shift_value == 32) {
5094 if (op->IsShl()) {
5095 __ Mov(o_h, low);
5096 __ Mov(o_l, 0);
5097 } else if (op->IsShr()) {
5098 __ Mov(o_l, high);
5099 __ Asr(o_h, high, 31);
5100 } else {
5101 __ Mov(o_l, high);
5102 __ Mov(o_h, 0);
5103 }
5104 } else if (shift_value == 1) {
5105 if (op->IsShl()) {
5106 __ Lsls(o_l, low, 1);
5107 __ Adc(o_h, high, high);
5108 } else if (op->IsShr()) {
5109 __ Asrs(o_h, high, 1);
5110 __ Rrx(o_l, low);
5111 } else {
5112 __ Lsrs(o_h, high, 1);
5113 __ Rrx(o_l, low);
5114 }
5115 } else {
5116 DCHECK(2 <= shift_value && shift_value < 32) << shift_value;
5117 if (op->IsShl()) {
5118 __ Lsl(o_h, high, shift_value);
5119 __ Orr(o_h, o_h, Operand(low, ShiftType::LSR, 32 - shift_value));
5120 __ Lsl(o_l, low, shift_value);
5121 } else if (op->IsShr()) {
5122 __ Lsr(o_l, low, shift_value);
5123 __ Orr(o_l, o_l, Operand(high, ShiftType::LSL, 32 - shift_value));
5124 __ Asr(o_h, high, shift_value);
5125 } else {
5126 __ Lsr(o_l, low, shift_value);
5127 __ Orr(o_l, o_l, Operand(high, ShiftType::LSL, 32 - shift_value));
5128 __ Lsr(o_h, high, shift_value);
5129 }
5130 }
5131 }
5132 break;
5133 }
5134 default:
5135 LOG(FATAL) << "Unexpected operation type " << type;
5136 UNREACHABLE();
5137 }
5138}
5139
5140void LocationsBuilderARMVIXL::VisitShl(HShl* shl) {
5141 HandleShift(shl);
5142}
5143
5144void InstructionCodeGeneratorARMVIXL::VisitShl(HShl* shl) {
5145 HandleShift(shl);
5146}
5147
5148void LocationsBuilderARMVIXL::VisitShr(HShr* shr) {
5149 HandleShift(shr);
5150}
5151
5152void InstructionCodeGeneratorARMVIXL::VisitShr(HShr* shr) {
5153 HandleShift(shr);
5154}
5155
5156void LocationsBuilderARMVIXL::VisitUShr(HUShr* ushr) {
5157 HandleShift(ushr);
5158}
5159
5160void InstructionCodeGeneratorARMVIXL::VisitUShr(HUShr* ushr) {
5161 HandleShift(ushr);
5162}
5163
5164void LocationsBuilderARMVIXL::VisitNewInstance(HNewInstance* instruction) {
5165 LocationSummary* locations =
5166 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
5167 if (instruction->IsStringAlloc()) {
5168 locations->AddTemp(LocationFrom(kMethodRegister));
5169 } else {
5170 InvokeRuntimeCallingConventionARMVIXL calling_convention;
5171 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
Artem Serov02d37832016-10-25 15:25:33 +01005172 }
5173 locations->SetOut(LocationFrom(r0));
5174}
5175
5176void InstructionCodeGeneratorARMVIXL::VisitNewInstance(HNewInstance* instruction) {
5177 // Note: if heap poisoning is enabled, the entry point takes cares
5178 // of poisoning the reference.
5179 if (instruction->IsStringAlloc()) {
5180 // String is allocated through StringFactory. Call NewEmptyString entry point.
5181 vixl32::Register temp = RegisterFrom(instruction->GetLocations()->GetTemp(0));
5182 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize);
5183 GetAssembler()->LoadFromOffset(kLoadWord, temp, tr, QUICK_ENTRY_POINT(pNewEmptyString));
5184 GetAssembler()->LoadFromOffset(kLoadWord, lr, temp, code_offset.Int32Value());
Alexandre Rames374ddf32016-11-04 10:40:49 +00005185 // 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 +00005186 ExactAssemblyScope aas(GetVIXLAssembler(),
5187 vixl32::k16BitT32InstructionSizeInBytes,
5188 CodeBufferCheckScope::kExactSize);
Artem Serov02d37832016-10-25 15:25:33 +01005189 __ blx(lr);
5190 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
5191 } else {
5192 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00005193 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
Artem Serov02d37832016-10-25 15:25:33 +01005194 }
Roland Levillain5daa4952017-07-03 17:23:56 +01005195 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 10);
Artem Serov02d37832016-10-25 15:25:33 +01005196}
5197
5198void LocationsBuilderARMVIXL::VisitNewArray(HNewArray* instruction) {
5199 LocationSummary* locations =
5200 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
5201 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Artem Serov02d37832016-10-25 15:25:33 +01005202 locations->SetOut(LocationFrom(r0));
Nicolas Geoffray8c7c4f12017-01-26 10:13:11 +00005203 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5204 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
Artem Serov02d37832016-10-25 15:25:33 +01005205}
5206
5207void InstructionCodeGeneratorARMVIXL::VisitNewArray(HNewArray* instruction) {
Artem Serov02d37832016-10-25 15:25:33 +01005208 // Note: if heap poisoning is enabled, the entry point takes cares
5209 // of poisoning the reference.
Artem Serov7b3672e2017-02-03 17:30:34 +00005210 QuickEntrypointEnum entrypoint =
5211 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
5212 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005213 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Artem Serov7b3672e2017-02-03 17:30:34 +00005214 DCHECK(!codegen_->IsLeafMethod());
Roland Levillain5daa4952017-07-03 17:23:56 +01005215 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 11);
Artem Serov02d37832016-10-25 15:25:33 +01005216}
5217
5218void LocationsBuilderARMVIXL::VisitParameterValue(HParameterValue* instruction) {
5219 LocationSummary* locations =
5220 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5221 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
5222 if (location.IsStackSlot()) {
5223 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5224 } else if (location.IsDoubleStackSlot()) {
5225 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5226 }
5227 locations->SetOut(location);
5228}
5229
5230void InstructionCodeGeneratorARMVIXL::VisitParameterValue(
5231 HParameterValue* instruction ATTRIBUTE_UNUSED) {
5232 // Nothing to do, the parameter is already at its location.
5233}
5234
5235void LocationsBuilderARMVIXL::VisitCurrentMethod(HCurrentMethod* instruction) {
5236 LocationSummary* locations =
5237 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5238 locations->SetOut(LocationFrom(kMethodRegister));
5239}
5240
5241void InstructionCodeGeneratorARMVIXL::VisitCurrentMethod(
5242 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
5243 // Nothing to do, the method is already at its location.
5244}
5245
5246void LocationsBuilderARMVIXL::VisitNot(HNot* not_) {
5247 LocationSummary* locations =
5248 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
5249 locations->SetInAt(0, Location::RequiresRegister());
5250 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5251}
5252
5253void InstructionCodeGeneratorARMVIXL::VisitNot(HNot* not_) {
5254 LocationSummary* locations = not_->GetLocations();
5255 Location out = locations->Out();
5256 Location in = locations->InAt(0);
5257 switch (not_->GetResultType()) {
5258 case Primitive::kPrimInt:
5259 __ Mvn(OutputRegister(not_), InputRegisterAt(not_, 0));
5260 break;
5261
5262 case Primitive::kPrimLong:
5263 __ Mvn(LowRegisterFrom(out), LowRegisterFrom(in));
5264 __ Mvn(HighRegisterFrom(out), HighRegisterFrom(in));
5265 break;
5266
5267 default:
5268 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
5269 }
5270}
5271
Scott Wakelingc34dba72016-10-03 10:14:44 +01005272void LocationsBuilderARMVIXL::VisitBooleanNot(HBooleanNot* bool_not) {
5273 LocationSummary* locations =
5274 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
5275 locations->SetInAt(0, Location::RequiresRegister());
5276 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5277}
5278
5279void InstructionCodeGeneratorARMVIXL::VisitBooleanNot(HBooleanNot* bool_not) {
5280 __ Eor(OutputRegister(bool_not), InputRegister(bool_not), 1);
5281}
5282
Artem Serov02d37832016-10-25 15:25:33 +01005283void LocationsBuilderARMVIXL::VisitCompare(HCompare* compare) {
5284 LocationSummary* locations =
5285 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
5286 switch (compare->InputAt(0)->GetType()) {
5287 case Primitive::kPrimBoolean:
5288 case Primitive::kPrimByte:
5289 case Primitive::kPrimShort:
5290 case Primitive::kPrimChar:
5291 case Primitive::kPrimInt:
5292 case Primitive::kPrimLong: {
5293 locations->SetInAt(0, Location::RequiresRegister());
5294 locations->SetInAt(1, Location::RequiresRegister());
5295 // Output overlaps because it is written before doing the low comparison.
5296 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
5297 break;
5298 }
5299 case Primitive::kPrimFloat:
5300 case Primitive::kPrimDouble: {
5301 locations->SetInAt(0, Location::RequiresFpuRegister());
5302 locations->SetInAt(1, ArithmeticZeroOrFpuRegister(compare->InputAt(1)));
5303 locations->SetOut(Location::RequiresRegister());
5304 break;
5305 }
5306 default:
5307 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
5308 }
5309}
5310
5311void InstructionCodeGeneratorARMVIXL::VisitCompare(HCompare* compare) {
5312 LocationSummary* locations = compare->GetLocations();
5313 vixl32::Register out = OutputRegister(compare);
5314 Location left = locations->InAt(0);
5315 Location right = locations->InAt(1);
5316
5317 vixl32::Label less, greater, done;
Anton Kirilov6f644202017-02-27 18:29:45 +00005318 vixl32::Label* final_label = codegen_->GetFinalLabel(compare, &done);
Artem Serov02d37832016-10-25 15:25:33 +01005319 Primitive::Type type = compare->InputAt(0)->GetType();
5320 vixl32::Condition less_cond = vixl32::Condition(kNone);
5321 switch (type) {
5322 case Primitive::kPrimBoolean:
5323 case Primitive::kPrimByte:
5324 case Primitive::kPrimShort:
5325 case Primitive::kPrimChar:
5326 case Primitive::kPrimInt: {
5327 // Emit move to `out` before the `Cmp`, as `Mov` might affect the status flags.
5328 __ Mov(out, 0);
5329 __ Cmp(RegisterFrom(left), RegisterFrom(right)); // Signed compare.
5330 less_cond = lt;
5331 break;
5332 }
5333 case Primitive::kPrimLong: {
5334 __ Cmp(HighRegisterFrom(left), HighRegisterFrom(right)); // Signed compare.
Artem Serov517d9f62016-12-12 15:51:15 +00005335 __ B(lt, &less, /* far_target */ false);
5336 __ B(gt, &greater, /* far_target */ false);
Artem Serov02d37832016-10-25 15:25:33 +01005337 // Emit move to `out` before the last `Cmp`, as `Mov` might affect the status flags.
5338 __ Mov(out, 0);
5339 __ Cmp(LowRegisterFrom(left), LowRegisterFrom(right)); // Unsigned compare.
5340 less_cond = lo;
5341 break;
5342 }
5343 case Primitive::kPrimFloat:
5344 case Primitive::kPrimDouble: {
5345 __ Mov(out, 0);
Donghui Bai426b49c2016-11-08 14:55:38 +08005346 GenerateVcmp(compare, codegen_);
Artem Serov02d37832016-10-25 15:25:33 +01005347 // To branch on the FP compare result we transfer FPSCR to APSR (encoded as PC in VMRS).
5348 __ Vmrs(RegisterOrAPSR_nzcv(kPcCode), FPSCR);
5349 less_cond = ARMFPCondition(kCondLT, compare->IsGtBias());
5350 break;
5351 }
5352 default:
5353 LOG(FATAL) << "Unexpected compare type " << type;
5354 UNREACHABLE();
5355 }
5356
Anton Kirilov6f644202017-02-27 18:29:45 +00005357 __ B(eq, final_label, /* far_target */ false);
Artem Serov517d9f62016-12-12 15:51:15 +00005358 __ B(less_cond, &less, /* far_target */ false);
Artem Serov02d37832016-10-25 15:25:33 +01005359
5360 __ Bind(&greater);
5361 __ Mov(out, 1);
Anton Kirilov6f644202017-02-27 18:29:45 +00005362 __ B(final_label);
Artem Serov02d37832016-10-25 15:25:33 +01005363
5364 __ Bind(&less);
5365 __ Mov(out, -1);
5366
Anton Kirilov6f644202017-02-27 18:29:45 +00005367 if (done.IsReferenced()) {
5368 __ Bind(&done);
5369 }
Artem Serov02d37832016-10-25 15:25:33 +01005370}
5371
5372void LocationsBuilderARMVIXL::VisitPhi(HPhi* instruction) {
5373 LocationSummary* locations =
5374 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5375 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
5376 locations->SetInAt(i, Location::Any());
5377 }
5378 locations->SetOut(Location::Any());
5379}
5380
5381void InstructionCodeGeneratorARMVIXL::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
5382 LOG(FATAL) << "Unreachable";
5383}
5384
5385void CodeGeneratorARMVIXL::GenerateMemoryBarrier(MemBarrierKind kind) {
5386 // TODO (ported from quick): revisit ARM barrier kinds.
5387 DmbOptions flavor = DmbOptions::ISH; // Quiet C++ warnings.
5388 switch (kind) {
5389 case MemBarrierKind::kAnyStore:
5390 case MemBarrierKind::kLoadAny:
5391 case MemBarrierKind::kAnyAny: {
5392 flavor = DmbOptions::ISH;
5393 break;
5394 }
5395 case MemBarrierKind::kStoreStore: {
5396 flavor = DmbOptions::ISHST;
5397 break;
5398 }
5399 default:
5400 LOG(FATAL) << "Unexpected memory barrier " << kind;
5401 }
5402 __ Dmb(flavor);
5403}
5404
5405void InstructionCodeGeneratorARMVIXL::GenerateWideAtomicLoad(vixl32::Register addr,
5406 uint32_t offset,
5407 vixl32::Register out_lo,
5408 vixl32::Register out_hi) {
5409 UseScratchRegisterScope temps(GetVIXLAssembler());
5410 if (offset != 0) {
5411 vixl32::Register temp = temps.Acquire();
5412 __ Add(temp, addr, offset);
5413 addr = temp;
5414 }
Scott Wakelingb77051e2016-11-21 19:46:00 +00005415 __ Ldrexd(out_lo, out_hi, MemOperand(addr));
Artem Serov02d37832016-10-25 15:25:33 +01005416}
5417
5418void InstructionCodeGeneratorARMVIXL::GenerateWideAtomicStore(vixl32::Register addr,
5419 uint32_t offset,
5420 vixl32::Register value_lo,
5421 vixl32::Register value_hi,
5422 vixl32::Register temp1,
5423 vixl32::Register temp2,
5424 HInstruction* instruction) {
5425 UseScratchRegisterScope temps(GetVIXLAssembler());
5426 vixl32::Label fail;
5427 if (offset != 0) {
5428 vixl32::Register temp = temps.Acquire();
5429 __ Add(temp, addr, offset);
5430 addr = temp;
5431 }
5432 __ Bind(&fail);
Alexandre Rames374ddf32016-11-04 10:40:49 +00005433 {
5434 // Ensure the pc position is recorded immediately after the `ldrexd` instruction.
Artem Serov0fb37192016-12-06 18:13:40 +00005435 ExactAssemblyScope aas(GetVIXLAssembler(),
5436 vixl32::kMaxInstructionSizeInBytes,
5437 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00005438 // We need a load followed by store. (The address used in a STREX instruction must
5439 // be the same as the address in the most recently executed LDREX instruction.)
5440 __ ldrexd(temp1, temp2, MemOperand(addr));
5441 codegen_->MaybeRecordImplicitNullCheck(instruction);
5442 }
Scott Wakelingb77051e2016-11-21 19:46:00 +00005443 __ Strexd(temp1, value_lo, value_hi, MemOperand(addr));
xueliang.zhongf51bc622016-11-04 09:23:32 +00005444 __ CompareAndBranchIfNonZero(temp1, &fail);
Artem Serov02d37832016-10-25 15:25:33 +01005445}
Artem Serov02109dd2016-09-23 17:17:54 +01005446
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005447void LocationsBuilderARMVIXL::HandleFieldSet(
5448 HInstruction* instruction, const FieldInfo& field_info) {
5449 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
5450
5451 LocationSummary* locations =
5452 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5453 locations->SetInAt(0, Location::RequiresRegister());
5454
5455 Primitive::Type field_type = field_info.GetFieldType();
5456 if (Primitive::IsFloatingPointType(field_type)) {
5457 locations->SetInAt(1, Location::RequiresFpuRegister());
5458 } else {
5459 locations->SetInAt(1, Location::RequiresRegister());
5460 }
5461
5462 bool is_wide = field_type == Primitive::kPrimLong || field_type == Primitive::kPrimDouble;
5463 bool generate_volatile = field_info.IsVolatile()
5464 && is_wide
5465 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
5466 bool needs_write_barrier =
5467 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
5468 // Temporary registers for the write barrier.
5469 // TODO: consider renaming StoreNeedsWriteBarrier to StoreNeedsGCMark.
5470 if (needs_write_barrier) {
5471 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
5472 locations->AddTemp(Location::RequiresRegister());
5473 } else if (generate_volatile) {
5474 // ARM encoding have some additional constraints for ldrexd/strexd:
5475 // - registers need to be consecutive
5476 // - the first register should be even but not R14.
5477 // We don't test for ARM yet, and the assertion makes sure that we
5478 // revisit this if we ever enable ARM encoding.
5479 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
5480
5481 locations->AddTemp(Location::RequiresRegister());
5482 locations->AddTemp(Location::RequiresRegister());
5483 if (field_type == Primitive::kPrimDouble) {
5484 // For doubles we need two more registers to copy the value.
5485 locations->AddTemp(LocationFrom(r2));
5486 locations->AddTemp(LocationFrom(r3));
5487 }
5488 }
5489}
5490
5491void InstructionCodeGeneratorARMVIXL::HandleFieldSet(HInstruction* instruction,
5492 const FieldInfo& field_info,
5493 bool value_can_be_null) {
5494 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
5495
5496 LocationSummary* locations = instruction->GetLocations();
5497 vixl32::Register base = InputRegisterAt(instruction, 0);
5498 Location value = locations->InAt(1);
5499
5500 bool is_volatile = field_info.IsVolatile();
5501 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
5502 Primitive::Type field_type = field_info.GetFieldType();
5503 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
5504 bool needs_write_barrier =
5505 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
5506
5507 if (is_volatile) {
5508 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
5509 }
5510
5511 switch (field_type) {
5512 case Primitive::kPrimBoolean:
5513 case Primitive::kPrimByte: {
5514 GetAssembler()->StoreToOffset(kStoreByte, RegisterFrom(value), base, offset);
5515 break;
5516 }
5517
5518 case Primitive::kPrimShort:
5519 case Primitive::kPrimChar: {
5520 GetAssembler()->StoreToOffset(kStoreHalfword, RegisterFrom(value), base, offset);
5521 break;
5522 }
5523
5524 case Primitive::kPrimInt:
5525 case Primitive::kPrimNot: {
5526 if (kPoisonHeapReferences && needs_write_barrier) {
5527 // Note that in the case where `value` is a null reference,
5528 // we do not enter this block, as a null reference does not
5529 // need poisoning.
5530 DCHECK_EQ(field_type, Primitive::kPrimNot);
5531 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
5532 __ Mov(temp, RegisterFrom(value));
5533 GetAssembler()->PoisonHeapReference(temp);
5534 GetAssembler()->StoreToOffset(kStoreWord, temp, base, offset);
5535 } else {
5536 GetAssembler()->StoreToOffset(kStoreWord, RegisterFrom(value), base, offset);
5537 }
5538 break;
5539 }
5540
5541 case Primitive::kPrimLong: {
5542 if (is_volatile && !atomic_ldrd_strd) {
5543 GenerateWideAtomicStore(base,
5544 offset,
5545 LowRegisterFrom(value),
5546 HighRegisterFrom(value),
5547 RegisterFrom(locations->GetTemp(0)),
5548 RegisterFrom(locations->GetTemp(1)),
5549 instruction);
5550 } else {
5551 GetAssembler()->StoreToOffset(kStoreWordPair, LowRegisterFrom(value), base, offset);
5552 codegen_->MaybeRecordImplicitNullCheck(instruction);
5553 }
5554 break;
5555 }
5556
5557 case Primitive::kPrimFloat: {
5558 GetAssembler()->StoreSToOffset(SRegisterFrom(value), base, offset);
5559 break;
5560 }
5561
5562 case Primitive::kPrimDouble: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01005563 vixl32::DRegister value_reg = DRegisterFrom(value);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005564 if (is_volatile && !atomic_ldrd_strd) {
5565 vixl32::Register value_reg_lo = RegisterFrom(locations->GetTemp(0));
5566 vixl32::Register value_reg_hi = RegisterFrom(locations->GetTemp(1));
5567
5568 __ Vmov(value_reg_lo, value_reg_hi, value_reg);
5569
5570 GenerateWideAtomicStore(base,
5571 offset,
5572 value_reg_lo,
5573 value_reg_hi,
5574 RegisterFrom(locations->GetTemp(2)),
5575 RegisterFrom(locations->GetTemp(3)),
5576 instruction);
5577 } else {
5578 GetAssembler()->StoreDToOffset(value_reg, base, offset);
5579 codegen_->MaybeRecordImplicitNullCheck(instruction);
5580 }
5581 break;
5582 }
5583
5584 case Primitive::kPrimVoid:
5585 LOG(FATAL) << "Unreachable type " << field_type;
5586 UNREACHABLE();
5587 }
5588
5589 // Longs and doubles are handled in the switch.
5590 if (field_type != Primitive::kPrimLong && field_type != Primitive::kPrimDouble) {
Alexandre Rames374ddf32016-11-04 10:40:49 +00005591 // TODO(VIXL): Here and for other calls to `MaybeRecordImplicitNullCheck` in this method, we
5592 // should use a scope and the assembler to emit the store instruction to guarantee that we
5593 // record the pc at the correct position. But the `Assembler` does not automatically handle
5594 // unencodable offsets. Practically, everything is fine because the helper and VIXL, at the time
5595 // of writing, do generate the store instruction last.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005596 codegen_->MaybeRecordImplicitNullCheck(instruction);
5597 }
5598
5599 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
5600 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
5601 vixl32::Register card = RegisterFrom(locations->GetTemp(1));
5602 codegen_->MarkGCCard(temp, card, base, RegisterFrom(value), value_can_be_null);
5603 }
5604
5605 if (is_volatile) {
5606 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
5607 }
5608}
5609
Artem Serov02d37832016-10-25 15:25:33 +01005610void LocationsBuilderARMVIXL::HandleFieldGet(HInstruction* instruction,
5611 const FieldInfo& field_info) {
5612 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
5613
5614 bool object_field_get_with_read_barrier =
5615 kEmitCompilerReadBarrier && (field_info.GetFieldType() == Primitive::kPrimNot);
5616 LocationSummary* locations =
5617 new (GetGraph()->GetArena()) LocationSummary(instruction,
5618 object_field_get_with_read_barrier ?
5619 LocationSummary::kCallOnSlowPath :
5620 LocationSummary::kNoCall);
5621 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
5622 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
5623 }
5624 locations->SetInAt(0, Location::RequiresRegister());
5625
5626 bool volatile_for_double = field_info.IsVolatile()
5627 && (field_info.GetFieldType() == Primitive::kPrimDouble)
5628 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
5629 // The output overlaps in case of volatile long: we don't want the
5630 // code generated by GenerateWideAtomicLoad to overwrite the
5631 // object's location. Likewise, in the case of an object field get
5632 // with read barriers enabled, we do not want the load to overwrite
5633 // the object's location, as we need it to emit the read barrier.
5634 bool overlap = (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) ||
5635 object_field_get_with_read_barrier;
5636
5637 if (Primitive::IsFloatingPointType(instruction->GetType())) {
5638 locations->SetOut(Location::RequiresFpuRegister());
5639 } else {
5640 locations->SetOut(Location::RequiresRegister(),
5641 (overlap ? Location::kOutputOverlap : Location::kNoOutputOverlap));
5642 }
5643 if (volatile_for_double) {
5644 // ARM encoding have some additional constraints for ldrexd/strexd:
5645 // - registers need to be consecutive
5646 // - the first register should be even but not R14.
5647 // We don't test for ARM yet, and the assertion makes sure that we
5648 // revisit this if we ever enable ARM encoding.
5649 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
5650 locations->AddTemp(Location::RequiresRegister());
5651 locations->AddTemp(Location::RequiresRegister());
5652 } else if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
5653 // We need a temporary register for the read barrier marking slow
Artem Serovc5fcb442016-12-02 19:19:58 +00005654 // path in CodeGeneratorARMVIXL::GenerateFieldLoadWithBakerReadBarrier.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01005655 if (kBakerReadBarrierLinkTimeThunksEnableForFields &&
5656 !Runtime::Current()->UseJitCompilation()) {
5657 // If link-time thunks for the Baker read barrier are enabled, for AOT
5658 // loads we need a temporary only if the offset is too big.
5659 if (field_info.GetFieldOffset().Uint32Value() >= kReferenceLoadMinFarOffset) {
5660 locations->AddTemp(Location::RequiresRegister());
5661 }
5662 // And we always need the reserved entrypoint register.
5663 locations->AddTemp(Location::RegisterLocation(kBakerCcEntrypointRegister.GetCode()));
5664 } else {
5665 locations->AddTemp(Location::RequiresRegister());
5666 }
Artem Serov02d37832016-10-25 15:25:33 +01005667 }
5668}
5669
5670Location LocationsBuilderARMVIXL::ArithmeticZeroOrFpuRegister(HInstruction* input) {
5671 DCHECK(Primitive::IsFloatingPointType(input->GetType())) << input->GetType();
5672 if ((input->IsFloatConstant() && (input->AsFloatConstant()->IsArithmeticZero())) ||
5673 (input->IsDoubleConstant() && (input->AsDoubleConstant()->IsArithmeticZero()))) {
5674 return Location::ConstantLocation(input->AsConstant());
5675 } else {
5676 return Location::RequiresFpuRegister();
5677 }
5678}
5679
Artem Serov02109dd2016-09-23 17:17:54 +01005680Location LocationsBuilderARMVIXL::ArmEncodableConstantOrRegister(HInstruction* constant,
5681 Opcode opcode) {
5682 DCHECK(!Primitive::IsFloatingPointType(constant->GetType()));
5683 if (constant->IsConstant() &&
5684 CanEncodeConstantAsImmediate(constant->AsConstant(), opcode)) {
5685 return Location::ConstantLocation(constant->AsConstant());
5686 }
5687 return Location::RequiresRegister();
5688}
5689
5690bool LocationsBuilderARMVIXL::CanEncodeConstantAsImmediate(HConstant* input_cst,
5691 Opcode opcode) {
5692 uint64_t value = static_cast<uint64_t>(Int64FromConstant(input_cst));
5693 if (Primitive::Is64BitType(input_cst->GetType())) {
5694 Opcode high_opcode = opcode;
5695 SetCc low_set_cc = kCcDontCare;
5696 switch (opcode) {
5697 case SUB:
5698 // Flip the operation to an ADD.
5699 value = -value;
5700 opcode = ADD;
5701 FALLTHROUGH_INTENDED;
5702 case ADD:
5703 if (Low32Bits(value) == 0u) {
5704 return CanEncodeConstantAsImmediate(High32Bits(value), opcode, kCcDontCare);
5705 }
5706 high_opcode = ADC;
5707 low_set_cc = kCcSet;
5708 break;
5709 default:
5710 break;
5711 }
5712 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode, low_set_cc) &&
5713 CanEncodeConstantAsImmediate(High32Bits(value), high_opcode, kCcDontCare);
5714 } else {
5715 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode);
5716 }
5717}
5718
5719// TODO(VIXL): Replace art::arm::SetCc` with `vixl32::FlagsUpdate after flags set optimization
5720// enabled.
5721bool LocationsBuilderARMVIXL::CanEncodeConstantAsImmediate(uint32_t value,
5722 Opcode opcode,
5723 SetCc set_cc) {
5724 ArmVIXLAssembler* assembler = codegen_->GetAssembler();
5725 if (assembler->ShifterOperandCanHold(opcode, value, set_cc)) {
5726 return true;
5727 }
5728 Opcode neg_opcode = kNoOperand;
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00005729 uint32_t neg_value = 0;
Artem Serov02109dd2016-09-23 17:17:54 +01005730 switch (opcode) {
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00005731 case AND: neg_opcode = BIC; neg_value = ~value; break;
5732 case ORR: neg_opcode = ORN; neg_value = ~value; break;
5733 case ADD: neg_opcode = SUB; neg_value = -value; break;
5734 case ADC: neg_opcode = SBC; neg_value = ~value; break;
5735 case SUB: neg_opcode = ADD; neg_value = -value; break;
5736 case SBC: neg_opcode = ADC; neg_value = ~value; break;
5737 case MOV: neg_opcode = MVN; neg_value = ~value; break;
Artem Serov02109dd2016-09-23 17:17:54 +01005738 default:
5739 return false;
5740 }
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00005741
5742 if (assembler->ShifterOperandCanHold(neg_opcode, neg_value, set_cc)) {
5743 return true;
5744 }
5745
5746 return opcode == AND && IsPowerOfTwo(value + 1);
Artem Serov02109dd2016-09-23 17:17:54 +01005747}
5748
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005749void InstructionCodeGeneratorARMVIXL::HandleFieldGet(HInstruction* instruction,
5750 const FieldInfo& field_info) {
5751 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
5752
5753 LocationSummary* locations = instruction->GetLocations();
5754 vixl32::Register base = InputRegisterAt(instruction, 0);
5755 Location out = locations->Out();
5756 bool is_volatile = field_info.IsVolatile();
5757 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
5758 Primitive::Type field_type = field_info.GetFieldType();
5759 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
5760
5761 switch (field_type) {
5762 case Primitive::kPrimBoolean:
5763 GetAssembler()->LoadFromOffset(kLoadUnsignedByte, RegisterFrom(out), base, offset);
5764 break;
5765
5766 case Primitive::kPrimByte:
5767 GetAssembler()->LoadFromOffset(kLoadSignedByte, RegisterFrom(out), base, offset);
5768 break;
5769
5770 case Primitive::kPrimShort:
5771 GetAssembler()->LoadFromOffset(kLoadSignedHalfword, RegisterFrom(out), base, offset);
5772 break;
5773
5774 case Primitive::kPrimChar:
5775 GetAssembler()->LoadFromOffset(kLoadUnsignedHalfword, RegisterFrom(out), base, offset);
5776 break;
5777
5778 case Primitive::kPrimInt:
5779 GetAssembler()->LoadFromOffset(kLoadWord, RegisterFrom(out), base, offset);
5780 break;
5781
5782 case Primitive::kPrimNot: {
5783 // /* HeapReference<Object> */ out = *(base + offset)
5784 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005785 Location temp_loc = locations->GetTemp(0);
5786 // Note that a potential implicit null check is handled in this
5787 // CodeGeneratorARMVIXL::GenerateFieldLoadWithBakerReadBarrier call.
5788 codegen_->GenerateFieldLoadWithBakerReadBarrier(
5789 instruction, out, base, offset, temp_loc, /* needs_null_check */ true);
5790 if (is_volatile) {
5791 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5792 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005793 } else {
5794 GetAssembler()->LoadFromOffset(kLoadWord, RegisterFrom(out), base, offset);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005795 codegen_->MaybeRecordImplicitNullCheck(instruction);
5796 if (is_volatile) {
5797 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5798 }
5799 // If read barriers are enabled, emit read barriers other than
5800 // Baker's using a slow path (and also unpoison the loaded
5801 // reference, if heap poisoning is enabled).
5802 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, locations->InAt(0), offset);
5803 }
5804 break;
5805 }
5806
5807 case Primitive::kPrimLong:
5808 if (is_volatile && !atomic_ldrd_strd) {
5809 GenerateWideAtomicLoad(base, offset, LowRegisterFrom(out), HighRegisterFrom(out));
5810 } else {
5811 GetAssembler()->LoadFromOffset(kLoadWordPair, LowRegisterFrom(out), base, offset);
5812 }
5813 break;
5814
5815 case Primitive::kPrimFloat:
5816 GetAssembler()->LoadSFromOffset(SRegisterFrom(out), base, offset);
5817 break;
5818
5819 case Primitive::kPrimDouble: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01005820 vixl32::DRegister out_dreg = DRegisterFrom(out);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005821 if (is_volatile && !atomic_ldrd_strd) {
5822 vixl32::Register lo = RegisterFrom(locations->GetTemp(0));
5823 vixl32::Register hi = RegisterFrom(locations->GetTemp(1));
5824 GenerateWideAtomicLoad(base, offset, lo, hi);
5825 // TODO(VIXL): Do we need to be immediately after the ldrexd instruction? If so we need a
5826 // scope.
5827 codegen_->MaybeRecordImplicitNullCheck(instruction);
5828 __ Vmov(out_dreg, lo, hi);
5829 } else {
5830 GetAssembler()->LoadDFromOffset(out_dreg, base, offset);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005831 codegen_->MaybeRecordImplicitNullCheck(instruction);
5832 }
5833 break;
5834 }
5835
5836 case Primitive::kPrimVoid:
5837 LOG(FATAL) << "Unreachable type " << field_type;
5838 UNREACHABLE();
5839 }
5840
5841 if (field_type == Primitive::kPrimNot || field_type == Primitive::kPrimDouble) {
5842 // Potential implicit null checks, in the case of reference or
5843 // double fields, are handled in the previous switch statement.
5844 } else {
5845 // Address cases other than reference and double that may require an implicit null check.
Alexandre Rames374ddf32016-11-04 10:40:49 +00005846 // TODO(VIXL): Here and for other calls to `MaybeRecordImplicitNullCheck` in this method, we
5847 // should use a scope and the assembler to emit the load instruction to guarantee that we
5848 // record the pc at the correct position. But the `Assembler` does not automatically handle
5849 // unencodable offsets. Practically, everything is fine because the helper and VIXL, at the time
5850 // of writing, do generate the store instruction last.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005851 codegen_->MaybeRecordImplicitNullCheck(instruction);
5852 }
5853
5854 if (is_volatile) {
5855 if (field_type == Primitive::kPrimNot) {
5856 // Memory barriers, in the case of references, are also handled
5857 // in the previous switch statement.
5858 } else {
5859 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5860 }
5861 }
5862}
5863
5864void LocationsBuilderARMVIXL::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
5865 HandleFieldSet(instruction, instruction->GetFieldInfo());
5866}
5867
5868void InstructionCodeGeneratorARMVIXL::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
5869 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
5870}
5871
5872void LocationsBuilderARMVIXL::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5873 HandleFieldGet(instruction, instruction->GetFieldInfo());
5874}
5875
5876void InstructionCodeGeneratorARMVIXL::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5877 HandleFieldGet(instruction, instruction->GetFieldInfo());
5878}
5879
5880void LocationsBuilderARMVIXL::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5881 HandleFieldGet(instruction, instruction->GetFieldInfo());
5882}
5883
5884void InstructionCodeGeneratorARMVIXL::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5885 HandleFieldGet(instruction, instruction->GetFieldInfo());
5886}
5887
Scott Wakelingc34dba72016-10-03 10:14:44 +01005888void LocationsBuilderARMVIXL::VisitStaticFieldSet(HStaticFieldSet* instruction) {
5889 HandleFieldSet(instruction, instruction->GetFieldInfo());
5890}
5891
5892void InstructionCodeGeneratorARMVIXL::VisitStaticFieldSet(HStaticFieldSet* instruction) {
5893 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
5894}
5895
Artem Serovcfbe9132016-10-14 15:58:56 +01005896void LocationsBuilderARMVIXL::VisitUnresolvedInstanceFieldGet(
5897 HUnresolvedInstanceFieldGet* instruction) {
5898 FieldAccessCallingConventionARMVIXL calling_convention;
5899 codegen_->CreateUnresolvedFieldLocationSummary(
5900 instruction, instruction->GetFieldType(), calling_convention);
5901}
5902
5903void InstructionCodeGeneratorARMVIXL::VisitUnresolvedInstanceFieldGet(
5904 HUnresolvedInstanceFieldGet* instruction) {
5905 FieldAccessCallingConventionARMVIXL calling_convention;
5906 codegen_->GenerateUnresolvedFieldAccess(instruction,
5907 instruction->GetFieldType(),
5908 instruction->GetFieldIndex(),
5909 instruction->GetDexPc(),
5910 calling_convention);
5911}
5912
5913void LocationsBuilderARMVIXL::VisitUnresolvedInstanceFieldSet(
5914 HUnresolvedInstanceFieldSet* instruction) {
5915 FieldAccessCallingConventionARMVIXL calling_convention;
5916 codegen_->CreateUnresolvedFieldLocationSummary(
5917 instruction, instruction->GetFieldType(), calling_convention);
5918}
5919
5920void InstructionCodeGeneratorARMVIXL::VisitUnresolvedInstanceFieldSet(
5921 HUnresolvedInstanceFieldSet* instruction) {
5922 FieldAccessCallingConventionARMVIXL calling_convention;
5923 codegen_->GenerateUnresolvedFieldAccess(instruction,
5924 instruction->GetFieldType(),
5925 instruction->GetFieldIndex(),
5926 instruction->GetDexPc(),
5927 calling_convention);
5928}
5929
5930void LocationsBuilderARMVIXL::VisitUnresolvedStaticFieldGet(
5931 HUnresolvedStaticFieldGet* instruction) {
5932 FieldAccessCallingConventionARMVIXL calling_convention;
5933 codegen_->CreateUnresolvedFieldLocationSummary(
5934 instruction, instruction->GetFieldType(), calling_convention);
5935}
5936
5937void InstructionCodeGeneratorARMVIXL::VisitUnresolvedStaticFieldGet(
5938 HUnresolvedStaticFieldGet* instruction) {
5939 FieldAccessCallingConventionARMVIXL calling_convention;
5940 codegen_->GenerateUnresolvedFieldAccess(instruction,
5941 instruction->GetFieldType(),
5942 instruction->GetFieldIndex(),
5943 instruction->GetDexPc(),
5944 calling_convention);
5945}
5946
5947void LocationsBuilderARMVIXL::VisitUnresolvedStaticFieldSet(
5948 HUnresolvedStaticFieldSet* instruction) {
5949 FieldAccessCallingConventionARMVIXL calling_convention;
5950 codegen_->CreateUnresolvedFieldLocationSummary(
5951 instruction, instruction->GetFieldType(), calling_convention);
5952}
5953
5954void InstructionCodeGeneratorARMVIXL::VisitUnresolvedStaticFieldSet(
5955 HUnresolvedStaticFieldSet* instruction) {
5956 FieldAccessCallingConventionARMVIXL calling_convention;
5957 codegen_->GenerateUnresolvedFieldAccess(instruction,
5958 instruction->GetFieldType(),
5959 instruction->GetFieldIndex(),
5960 instruction->GetDexPc(),
5961 calling_convention);
5962}
5963
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005964void LocationsBuilderARMVIXL::VisitNullCheck(HNullCheck* instruction) {
Artem Serov657022c2016-11-23 14:19:38 +00005965 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005966 locations->SetInAt(0, Location::RequiresRegister());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005967}
5968
5969void CodeGeneratorARMVIXL::GenerateImplicitNullCheck(HNullCheck* instruction) {
5970 if (CanMoveNullCheckToUser(instruction)) {
5971 return;
5972 }
5973
5974 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames374ddf32016-11-04 10:40:49 +00005975 // Ensure the pc position is recorded immediately after the `ldr` instruction.
Artem Serov0fb37192016-12-06 18:13:40 +00005976 ExactAssemblyScope aas(GetVIXLAssembler(),
5977 vixl32::kMaxInstructionSizeInBytes,
5978 CodeBufferCheckScope::kMaximumSize);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005979 __ ldr(temps.Acquire(), MemOperand(InputRegisterAt(instruction, 0)));
5980 RecordPcInfo(instruction, instruction->GetDexPc());
5981}
5982
5983void CodeGeneratorARMVIXL::GenerateExplicitNullCheck(HNullCheck* instruction) {
5984 NullCheckSlowPathARMVIXL* slow_path =
5985 new (GetGraph()->GetArena()) NullCheckSlowPathARMVIXL(instruction);
5986 AddSlowPath(slow_path);
xueliang.zhongf51bc622016-11-04 09:23:32 +00005987 __ CompareAndBranchIfZero(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005988}
5989
5990void InstructionCodeGeneratorARMVIXL::VisitNullCheck(HNullCheck* instruction) {
5991 codegen_->GenerateNullCheck(instruction);
5992}
5993
Scott Wakelingc34dba72016-10-03 10:14:44 +01005994static LoadOperandType GetLoadOperandType(Primitive::Type type) {
5995 switch (type) {
5996 case Primitive::kPrimNot:
5997 return kLoadWord;
5998 case Primitive::kPrimBoolean:
5999 return kLoadUnsignedByte;
6000 case Primitive::kPrimByte:
6001 return kLoadSignedByte;
6002 case Primitive::kPrimChar:
6003 return kLoadUnsignedHalfword;
6004 case Primitive::kPrimShort:
6005 return kLoadSignedHalfword;
6006 case Primitive::kPrimInt:
6007 return kLoadWord;
6008 case Primitive::kPrimLong:
6009 return kLoadWordPair;
6010 case Primitive::kPrimFloat:
6011 return kLoadSWord;
6012 case Primitive::kPrimDouble:
6013 return kLoadDWord;
6014 default:
6015 LOG(FATAL) << "Unreachable type " << type;
6016 UNREACHABLE();
6017 }
6018}
6019
6020static StoreOperandType GetStoreOperandType(Primitive::Type type) {
6021 switch (type) {
6022 case Primitive::kPrimNot:
6023 return kStoreWord;
6024 case Primitive::kPrimBoolean:
6025 case Primitive::kPrimByte:
6026 return kStoreByte;
6027 case Primitive::kPrimChar:
6028 case Primitive::kPrimShort:
6029 return kStoreHalfword;
6030 case Primitive::kPrimInt:
6031 return kStoreWord;
6032 case Primitive::kPrimLong:
6033 return kStoreWordPair;
6034 case Primitive::kPrimFloat:
6035 return kStoreSWord;
6036 case Primitive::kPrimDouble:
6037 return kStoreDWord;
6038 default:
6039 LOG(FATAL) << "Unreachable type " << type;
6040 UNREACHABLE();
6041 }
6042}
6043
6044void CodeGeneratorARMVIXL::LoadFromShiftedRegOffset(Primitive::Type type,
6045 Location out_loc,
6046 vixl32::Register base,
6047 vixl32::Register reg_index,
6048 vixl32::Condition cond) {
6049 uint32_t shift_count = Primitive::ComponentSizeShift(type);
6050 MemOperand mem_address(base, reg_index, vixl32::LSL, shift_count);
6051
6052 switch (type) {
6053 case Primitive::kPrimByte:
6054 __ Ldrsb(cond, RegisterFrom(out_loc), mem_address);
6055 break;
6056 case Primitive::kPrimBoolean:
6057 __ Ldrb(cond, RegisterFrom(out_loc), mem_address);
6058 break;
6059 case Primitive::kPrimShort:
6060 __ Ldrsh(cond, RegisterFrom(out_loc), mem_address);
6061 break;
6062 case Primitive::kPrimChar:
6063 __ Ldrh(cond, RegisterFrom(out_loc), mem_address);
6064 break;
6065 case Primitive::kPrimNot:
6066 case Primitive::kPrimInt:
6067 __ Ldr(cond, RegisterFrom(out_loc), mem_address);
6068 break;
6069 // T32 doesn't support LoadFromShiftedRegOffset mem address mode for these types.
6070 case Primitive::kPrimLong:
6071 case Primitive::kPrimFloat:
6072 case Primitive::kPrimDouble:
6073 default:
6074 LOG(FATAL) << "Unreachable type " << type;
6075 UNREACHABLE();
6076 }
6077}
6078
6079void CodeGeneratorARMVIXL::StoreToShiftedRegOffset(Primitive::Type type,
6080 Location loc,
6081 vixl32::Register base,
6082 vixl32::Register reg_index,
6083 vixl32::Condition cond) {
6084 uint32_t shift_count = Primitive::ComponentSizeShift(type);
6085 MemOperand mem_address(base, reg_index, vixl32::LSL, shift_count);
6086
6087 switch (type) {
6088 case Primitive::kPrimByte:
6089 case Primitive::kPrimBoolean:
6090 __ Strb(cond, RegisterFrom(loc), mem_address);
6091 break;
6092 case Primitive::kPrimShort:
6093 case Primitive::kPrimChar:
6094 __ Strh(cond, RegisterFrom(loc), mem_address);
6095 break;
6096 case Primitive::kPrimNot:
6097 case Primitive::kPrimInt:
6098 __ Str(cond, RegisterFrom(loc), mem_address);
6099 break;
6100 // T32 doesn't support StoreToShiftedRegOffset mem address mode for these types.
6101 case Primitive::kPrimLong:
6102 case Primitive::kPrimFloat:
6103 case Primitive::kPrimDouble:
6104 default:
6105 LOG(FATAL) << "Unreachable type " << type;
6106 UNREACHABLE();
6107 }
6108}
6109
6110void LocationsBuilderARMVIXL::VisitArrayGet(HArrayGet* instruction) {
6111 bool object_array_get_with_read_barrier =
6112 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
6113 LocationSummary* locations =
6114 new (GetGraph()->GetArena()) LocationSummary(instruction,
6115 object_array_get_with_read_barrier ?
6116 LocationSummary::kCallOnSlowPath :
6117 LocationSummary::kNoCall);
6118 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006119 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Scott Wakelingc34dba72016-10-03 10:14:44 +01006120 }
6121 locations->SetInAt(0, Location::RequiresRegister());
6122 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
6123 if (Primitive::IsFloatingPointType(instruction->GetType())) {
6124 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
6125 } else {
6126 // The output overlaps in the case of an object array get with
6127 // read barriers enabled: we do not want the move to overwrite the
6128 // array's location, as we need it to emit the read barrier.
6129 locations->SetOut(
6130 Location::RequiresRegister(),
6131 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
6132 }
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01006133 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
6134 // We need a temporary register for the read barrier marking slow
6135 // path in CodeGeneratorARMVIXL::GenerateArrayLoadWithBakerReadBarrier.
6136 if (kBakerReadBarrierLinkTimeThunksEnableForFields &&
6137 !Runtime::Current()->UseJitCompilation() &&
6138 instruction->GetIndex()->IsConstant()) {
6139 // Array loads with constant index are treated as field loads.
6140 // If link-time thunks for the Baker read barrier are enabled, for AOT
6141 // constant index loads we need a temporary only if the offset is too big.
6142 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
6143 uint32_t index = instruction->GetIndex()->AsIntConstant()->GetValue();
6144 offset += index << Primitive::ComponentSizeShift(Primitive::kPrimNot);
6145 if (offset >= kReferenceLoadMinFarOffset) {
6146 locations->AddTemp(Location::RequiresRegister());
6147 }
6148 // And we always need the reserved entrypoint register.
6149 locations->AddTemp(Location::RegisterLocation(kBakerCcEntrypointRegister.GetCode()));
6150 } else if (kBakerReadBarrierLinkTimeThunksEnableForArrays &&
6151 !Runtime::Current()->UseJitCompilation() &&
6152 !instruction->GetIndex()->IsConstant()) {
6153 // We need a non-scratch temporary for the array data pointer.
6154 locations->AddTemp(Location::RequiresRegister());
6155 // And we always need the reserved entrypoint register.
6156 locations->AddTemp(Location::RegisterLocation(kBakerCcEntrypointRegister.GetCode()));
6157 } else {
6158 locations->AddTemp(Location::RequiresRegister());
6159 }
6160 } else if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
6161 // Also need a temporary for String compression feature.
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006162 locations->AddTemp(Location::RequiresRegister());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006163 }
6164}
6165
6166void InstructionCodeGeneratorARMVIXL::VisitArrayGet(HArrayGet* instruction) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006167 LocationSummary* locations = instruction->GetLocations();
6168 Location obj_loc = locations->InAt(0);
6169 vixl32::Register obj = InputRegisterAt(instruction, 0);
6170 Location index = locations->InAt(1);
6171 Location out_loc = locations->Out();
6172 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
6173 Primitive::Type type = instruction->GetType();
6174 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
6175 instruction->IsStringCharAt();
6176 HInstruction* array_instr = instruction->GetArray();
6177 bool has_intermediate_address = array_instr->IsIntermediateAddress();
Scott Wakelingc34dba72016-10-03 10:14:44 +01006178
6179 switch (type) {
6180 case Primitive::kPrimBoolean:
6181 case Primitive::kPrimByte:
6182 case Primitive::kPrimShort:
6183 case Primitive::kPrimChar:
6184 case Primitive::kPrimInt: {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006185 vixl32::Register length;
6186 if (maybe_compressed_char_at) {
6187 length = RegisterFrom(locations->GetTemp(0));
6188 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
6189 GetAssembler()->LoadFromOffset(kLoadWord, length, obj, count_offset);
6190 codegen_->MaybeRecordImplicitNullCheck(instruction);
6191 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006192 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006193 int32_t const_index = Int32ConstantFrom(index);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006194 if (maybe_compressed_char_at) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006195 vixl32::Label uncompressed_load, done;
Anton Kirilov6f644202017-02-27 18:29:45 +00006196 vixl32::Label* final_label = codegen_->GetFinalLabel(instruction, &done);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006197 __ Lsrs(length, length, 1u); // LSRS has a 16-bit encoding, TST (immediate) does not.
6198 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
6199 "Expecting 0=compressed, 1=uncompressed");
Artem Serov517d9f62016-12-12 15:51:15 +00006200 __ B(cs, &uncompressed_load, /* far_target */ false);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006201 GetAssembler()->LoadFromOffset(kLoadUnsignedByte,
6202 RegisterFrom(out_loc),
6203 obj,
6204 data_offset + const_index);
Anton Kirilov6f644202017-02-27 18:29:45 +00006205 __ B(final_label);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006206 __ Bind(&uncompressed_load);
6207 GetAssembler()->LoadFromOffset(GetLoadOperandType(Primitive::kPrimChar),
6208 RegisterFrom(out_loc),
6209 obj,
6210 data_offset + (const_index << 1));
Anton Kirilov6f644202017-02-27 18:29:45 +00006211 if (done.IsReferenced()) {
6212 __ Bind(&done);
6213 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006214 } else {
6215 uint32_t full_offset = data_offset + (const_index << Primitive::ComponentSizeShift(type));
6216
6217 LoadOperandType load_type = GetLoadOperandType(type);
6218 GetAssembler()->LoadFromOffset(load_type, RegisterFrom(out_loc), obj, full_offset);
6219 }
6220 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006221 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006222 vixl32::Register temp = temps.Acquire();
6223
6224 if (has_intermediate_address) {
Artem Serov2bbc9532016-10-21 11:51:50 +01006225 // We do not need to compute the intermediate address from the array: the
6226 // input instruction has done it already. See the comment in
6227 // `TryExtractArrayAccessAddress()`.
6228 if (kIsDebugBuild) {
6229 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
Anton Kirilov644032c2016-12-06 17:51:43 +00006230 DCHECK_EQ(Uint64ConstantFrom(tmp->GetOffset()), data_offset);
Artem Serov2bbc9532016-10-21 11:51:50 +01006231 }
6232 temp = obj;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006233 } else {
6234 __ Add(temp, obj, data_offset);
6235 }
6236 if (maybe_compressed_char_at) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006237 vixl32::Label uncompressed_load, done;
Anton Kirilov6f644202017-02-27 18:29:45 +00006238 vixl32::Label* final_label = codegen_->GetFinalLabel(instruction, &done);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006239 __ Lsrs(length, length, 1u); // LSRS has a 16-bit encoding, TST (immediate) does not.
6240 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
6241 "Expecting 0=compressed, 1=uncompressed");
Artem Serov517d9f62016-12-12 15:51:15 +00006242 __ B(cs, &uncompressed_load, /* far_target */ false);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006243 __ Ldrb(RegisterFrom(out_loc), MemOperand(temp, RegisterFrom(index), vixl32::LSL, 0));
Anton Kirilov6f644202017-02-27 18:29:45 +00006244 __ B(final_label);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006245 __ Bind(&uncompressed_load);
6246 __ Ldrh(RegisterFrom(out_loc), MemOperand(temp, RegisterFrom(index), vixl32::LSL, 1));
Anton Kirilov6f644202017-02-27 18:29:45 +00006247 if (done.IsReferenced()) {
6248 __ Bind(&done);
6249 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006250 } else {
6251 codegen_->LoadFromShiftedRegOffset(type, out_loc, temp, RegisterFrom(index));
6252 }
6253 }
6254 break;
6255 }
6256
6257 case Primitive::kPrimNot: {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006258 // The read barrier instrumentation of object ArrayGet
6259 // instructions does not support the HIntermediateAddress
6260 // instruction.
6261 DCHECK(!(has_intermediate_address && kEmitCompilerReadBarrier));
6262
Scott Wakelingc34dba72016-10-03 10:14:44 +01006263 static_assert(
6264 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6265 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
6266 // /* HeapReference<Object> */ out =
6267 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
6268 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006269 Location temp = locations->GetTemp(0);
6270 // Note that a potential implicit null check is handled in this
6271 // CodeGeneratorARMVIXL::GenerateArrayLoadWithBakerReadBarrier call.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01006272 DCHECK(!instruction->CanDoImplicitNullCheckOn(instruction->InputAt(0)));
6273 if (index.IsConstant()) {
6274 // Array load with a constant index can be treated as a field load.
6275 data_offset += Int32ConstantFrom(index) << Primitive::ComponentSizeShift(type);
6276 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6277 out_loc,
6278 obj,
6279 data_offset,
6280 locations->GetTemp(0),
6281 /* needs_null_check */ false);
6282 } else {
6283 codegen_->GenerateArrayLoadWithBakerReadBarrier(
6284 instruction, out_loc, obj, data_offset, index, temp, /* needs_null_check */ false);
6285 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006286 } else {
6287 vixl32::Register out = OutputRegister(instruction);
6288 if (index.IsConstant()) {
6289 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00006290 (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006291 GetAssembler()->LoadFromOffset(kLoadWord, out, obj, offset);
Alexandre Rames374ddf32016-11-04 10:40:49 +00006292 // TODO(VIXL): Here and for other calls to `MaybeRecordImplicitNullCheck` in this method,
6293 // we should use a scope and the assembler to emit the load instruction to guarantee that
6294 // we record the pc at the correct position. But the `Assembler` does not automatically
6295 // handle unencodable offsets. Practically, everything is fine because the helper and
6296 // VIXL, at the time of writing, do generate the store instruction last.
Scott Wakelingc34dba72016-10-03 10:14:44 +01006297 codegen_->MaybeRecordImplicitNullCheck(instruction);
6298 // If read barriers are enabled, emit read barriers other than
6299 // Baker's using a slow path (and also unpoison the loaded
6300 // reference, if heap poisoning is enabled).
6301 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
6302 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006303 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006304 vixl32::Register temp = temps.Acquire();
6305
6306 if (has_intermediate_address) {
Artem Serov2bbc9532016-10-21 11:51:50 +01006307 // We do not need to compute the intermediate address from the array: the
6308 // input instruction has done it already. See the comment in
6309 // `TryExtractArrayAccessAddress()`.
6310 if (kIsDebugBuild) {
6311 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
Anton Kirilov644032c2016-12-06 17:51:43 +00006312 DCHECK_EQ(Uint64ConstantFrom(tmp->GetOffset()), data_offset);
Artem Serov2bbc9532016-10-21 11:51:50 +01006313 }
6314 temp = obj;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006315 } else {
6316 __ Add(temp, obj, data_offset);
6317 }
6318 codegen_->LoadFromShiftedRegOffset(type, out_loc, temp, RegisterFrom(index));
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006319 temps.Close();
Alexandre Rames374ddf32016-11-04 10:40:49 +00006320 // TODO(VIXL): Use a scope to ensure that we record the pc position immediately after the
6321 // load instruction. Practically, everything is fine because the helper and VIXL, at the
6322 // time of writing, do generate the store instruction last.
Scott Wakelingc34dba72016-10-03 10:14:44 +01006323 codegen_->MaybeRecordImplicitNullCheck(instruction);
6324 // If read barriers are enabled, emit read barriers other than
6325 // Baker's using a slow path (and also unpoison the loaded
6326 // reference, if heap poisoning is enabled).
6327 codegen_->MaybeGenerateReadBarrierSlow(
6328 instruction, out_loc, out_loc, obj_loc, data_offset, index);
6329 }
6330 }
6331 break;
6332 }
6333
6334 case Primitive::kPrimLong: {
6335 if (index.IsConstant()) {
6336 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00006337 (Int32ConstantFrom(index) << TIMES_8) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006338 GetAssembler()->LoadFromOffset(kLoadWordPair, LowRegisterFrom(out_loc), obj, offset);
6339 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006340 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006341 vixl32::Register temp = temps.Acquire();
6342 __ Add(temp, obj, Operand(RegisterFrom(index), vixl32::LSL, TIMES_8));
6343 GetAssembler()->LoadFromOffset(kLoadWordPair, LowRegisterFrom(out_loc), temp, data_offset);
6344 }
6345 break;
6346 }
6347
6348 case Primitive::kPrimFloat: {
6349 vixl32::SRegister out = SRegisterFrom(out_loc);
6350 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006351 size_t offset = (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006352 GetAssembler()->LoadSFromOffset(out, obj, offset);
6353 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006354 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006355 vixl32::Register temp = temps.Acquire();
6356 __ Add(temp, obj, Operand(RegisterFrom(index), vixl32::LSL, TIMES_4));
6357 GetAssembler()->LoadSFromOffset(out, temp, data_offset);
6358 }
6359 break;
6360 }
6361
6362 case Primitive::kPrimDouble: {
6363 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006364 size_t offset = (Int32ConstantFrom(index) << TIMES_8) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006365 GetAssembler()->LoadDFromOffset(DRegisterFrom(out_loc), obj, offset);
6366 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006367 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006368 vixl32::Register temp = temps.Acquire();
6369 __ Add(temp, obj, Operand(RegisterFrom(index), vixl32::LSL, TIMES_8));
6370 GetAssembler()->LoadDFromOffset(DRegisterFrom(out_loc), temp, data_offset);
6371 }
6372 break;
6373 }
6374
6375 case Primitive::kPrimVoid:
6376 LOG(FATAL) << "Unreachable type " << type;
6377 UNREACHABLE();
6378 }
6379
6380 if (type == Primitive::kPrimNot) {
6381 // Potential implicit null checks, in the case of reference
6382 // arrays, are handled in the previous switch statement.
6383 } else if (!maybe_compressed_char_at) {
Alexandre Rames374ddf32016-11-04 10:40:49 +00006384 // TODO(VIXL): Use a scope to ensure we record the pc info immediately after
6385 // the preceding load instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01006386 codegen_->MaybeRecordImplicitNullCheck(instruction);
6387 }
6388}
6389
6390void LocationsBuilderARMVIXL::VisitArraySet(HArraySet* instruction) {
6391 Primitive::Type value_type = instruction->GetComponentType();
6392
6393 bool needs_write_barrier =
6394 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
6395 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
6396
6397 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
6398 instruction,
6399 may_need_runtime_call_for_type_check ?
6400 LocationSummary::kCallOnSlowPath :
6401 LocationSummary::kNoCall);
6402
6403 locations->SetInAt(0, Location::RequiresRegister());
6404 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
6405 if (Primitive::IsFloatingPointType(value_type)) {
6406 locations->SetInAt(2, Location::RequiresFpuRegister());
6407 } else {
6408 locations->SetInAt(2, Location::RequiresRegister());
6409 }
6410 if (needs_write_barrier) {
6411 // Temporary registers for the write barrier.
6412 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
6413 locations->AddTemp(Location::RequiresRegister());
6414 }
6415}
6416
6417void InstructionCodeGeneratorARMVIXL::VisitArraySet(HArraySet* instruction) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006418 LocationSummary* locations = instruction->GetLocations();
6419 vixl32::Register array = InputRegisterAt(instruction, 0);
6420 Location index = locations->InAt(1);
6421 Primitive::Type value_type = instruction->GetComponentType();
6422 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
6423 bool needs_write_barrier =
6424 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
6425 uint32_t data_offset =
6426 mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
6427 Location value_loc = locations->InAt(2);
6428 HInstruction* array_instr = instruction->GetArray();
6429 bool has_intermediate_address = array_instr->IsIntermediateAddress();
Scott Wakelingc34dba72016-10-03 10:14:44 +01006430
6431 switch (value_type) {
6432 case Primitive::kPrimBoolean:
6433 case Primitive::kPrimByte:
6434 case Primitive::kPrimShort:
6435 case Primitive::kPrimChar:
6436 case Primitive::kPrimInt: {
6437 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006438 int32_t const_index = Int32ConstantFrom(index);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006439 uint32_t full_offset =
6440 data_offset + (const_index << Primitive::ComponentSizeShift(value_type));
6441 StoreOperandType store_type = GetStoreOperandType(value_type);
6442 GetAssembler()->StoreToOffset(store_type, RegisterFrom(value_loc), array, full_offset);
6443 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006444 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006445 vixl32::Register temp = temps.Acquire();
6446
6447 if (has_intermediate_address) {
Artem Serov2bbc9532016-10-21 11:51:50 +01006448 // We do not need to compute the intermediate address from the array: the
6449 // input instruction has done it already. See the comment in
6450 // `TryExtractArrayAccessAddress()`.
6451 if (kIsDebugBuild) {
6452 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
Anton Kirilov644032c2016-12-06 17:51:43 +00006453 DCHECK_EQ(Uint64ConstantFrom(tmp->GetOffset()), data_offset);
Artem Serov2bbc9532016-10-21 11:51:50 +01006454 }
6455 temp = array;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006456 } else {
6457 __ Add(temp, array, data_offset);
6458 }
6459 codegen_->StoreToShiftedRegOffset(value_type, value_loc, temp, RegisterFrom(index));
6460 }
6461 break;
6462 }
6463
6464 case Primitive::kPrimNot: {
6465 vixl32::Register value = RegisterFrom(value_loc);
6466 // TryExtractArrayAccessAddress optimization is never applied for non-primitive ArraySet.
6467 // See the comment in instruction_simplifier_shared.cc.
6468 DCHECK(!has_intermediate_address);
6469
6470 if (instruction->InputAt(2)->IsNullConstant()) {
6471 // Just setting null.
6472 if (index.IsConstant()) {
6473 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00006474 (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006475 GetAssembler()->StoreToOffset(kStoreWord, value, array, offset);
6476 } else {
6477 DCHECK(index.IsRegister()) << index;
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, array, data_offset);
6481 codegen_->StoreToShiftedRegOffset(value_type, value_loc, temp, RegisterFrom(index));
6482 }
Alexandre Rames374ddf32016-11-04 10:40:49 +00006483 // TODO(VIXL): Use a scope to ensure we record the pc info immediately after the preceding
6484 // store instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01006485 codegen_->MaybeRecordImplicitNullCheck(instruction);
6486 DCHECK(!needs_write_barrier);
6487 DCHECK(!may_need_runtime_call_for_type_check);
6488 break;
6489 }
6490
6491 DCHECK(needs_write_barrier);
6492 Location temp1_loc = locations->GetTemp(0);
6493 vixl32::Register temp1 = RegisterFrom(temp1_loc);
6494 Location temp2_loc = locations->GetTemp(1);
6495 vixl32::Register temp2 = RegisterFrom(temp2_loc);
6496 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6497 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6498 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6499 vixl32::Label done;
Anton Kirilov6f644202017-02-27 18:29:45 +00006500 vixl32::Label* final_label = codegen_->GetFinalLabel(instruction, &done);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006501 SlowPathCodeARMVIXL* slow_path = nullptr;
6502
6503 if (may_need_runtime_call_for_type_check) {
6504 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARMVIXL(instruction);
6505 codegen_->AddSlowPath(slow_path);
6506 if (instruction->GetValueCanBeNull()) {
6507 vixl32::Label non_zero;
xueliang.zhongf51bc622016-11-04 09:23:32 +00006508 __ CompareAndBranchIfNonZero(value, &non_zero);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006509 if (index.IsConstant()) {
6510 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00006511 (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006512 GetAssembler()->StoreToOffset(kStoreWord, value, array, offset);
6513 } else {
6514 DCHECK(index.IsRegister()) << index;
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006515 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006516 vixl32::Register temp = temps.Acquire();
6517 __ Add(temp, array, data_offset);
6518 codegen_->StoreToShiftedRegOffset(value_type, value_loc, temp, RegisterFrom(index));
6519 }
Alexandre Rames374ddf32016-11-04 10:40:49 +00006520 // TODO(VIXL): Use a scope to ensure we record the pc info immediately after the preceding
6521 // store instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01006522 codegen_->MaybeRecordImplicitNullCheck(instruction);
Anton Kirilov6f644202017-02-27 18:29:45 +00006523 __ B(final_label);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006524 __ Bind(&non_zero);
6525 }
6526
6527 // Note that when read barriers are enabled, the type checks
6528 // are performed without read barriers. This is fine, even in
6529 // the case where a class object is in the from-space after
6530 // the flip, as a comparison involving such a type would not
6531 // produce a false positive; it may of course produce a false
6532 // negative, in which case we would take the ArraySet slow
6533 // path.
6534
Alexandre Rames374ddf32016-11-04 10:40:49 +00006535 {
6536 // Ensure we record the pc position immediately after the `ldr` instruction.
Artem Serov0fb37192016-12-06 18:13:40 +00006537 ExactAssemblyScope aas(GetVIXLAssembler(),
6538 vixl32::kMaxInstructionSizeInBytes,
6539 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00006540 // /* HeapReference<Class> */ temp1 = array->klass_
6541 __ ldr(temp1, MemOperand(array, class_offset));
6542 codegen_->MaybeRecordImplicitNullCheck(instruction);
6543 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006544 GetAssembler()->MaybeUnpoisonHeapReference(temp1);
6545
6546 // /* HeapReference<Class> */ temp1 = temp1->component_type_
6547 GetAssembler()->LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
6548 // /* HeapReference<Class> */ temp2 = value->klass_
6549 GetAssembler()->LoadFromOffset(kLoadWord, temp2, value, class_offset);
6550 // If heap poisoning is enabled, no need to unpoison `temp1`
6551 // nor `temp2`, as we are comparing two poisoned references.
6552 __ Cmp(temp1, temp2);
6553
6554 if (instruction->StaticTypeOfArrayIsObjectArray()) {
6555 vixl32::Label do_put;
Artem Serov517d9f62016-12-12 15:51:15 +00006556 __ B(eq, &do_put, /* far_target */ false);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006557 // If heap poisoning is enabled, the `temp1` reference has
6558 // not been unpoisoned yet; unpoison it now.
6559 GetAssembler()->MaybeUnpoisonHeapReference(temp1);
6560
6561 // /* HeapReference<Class> */ temp1 = temp1->super_class_
6562 GetAssembler()->LoadFromOffset(kLoadWord, temp1, temp1, super_offset);
6563 // If heap poisoning is enabled, no need to unpoison
6564 // `temp1`, as we are comparing against null below.
xueliang.zhongf51bc622016-11-04 09:23:32 +00006565 __ CompareAndBranchIfNonZero(temp1, slow_path->GetEntryLabel());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006566 __ Bind(&do_put);
6567 } else {
6568 __ B(ne, slow_path->GetEntryLabel());
6569 }
6570 }
6571
6572 vixl32::Register source = value;
6573 if (kPoisonHeapReferences) {
6574 // Note that in the case where `value` is a null reference,
6575 // we do not enter this block, as a null reference does not
6576 // need poisoning.
6577 DCHECK_EQ(value_type, Primitive::kPrimNot);
6578 __ Mov(temp1, value);
6579 GetAssembler()->PoisonHeapReference(temp1);
6580 source = temp1;
6581 }
6582
6583 if (index.IsConstant()) {
6584 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00006585 (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006586 GetAssembler()->StoreToOffset(kStoreWord, source, array, offset);
6587 } else {
6588 DCHECK(index.IsRegister()) << index;
6589
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006590 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006591 vixl32::Register temp = temps.Acquire();
6592 __ Add(temp, array, data_offset);
6593 codegen_->StoreToShiftedRegOffset(value_type,
6594 LocationFrom(source),
6595 temp,
6596 RegisterFrom(index));
6597 }
6598
6599 if (!may_need_runtime_call_for_type_check) {
Alexandre Rames374ddf32016-11-04 10:40:49 +00006600 // TODO(VIXL): Ensure we record the pc position immediately after the preceding store
6601 // instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01006602 codegen_->MaybeRecordImplicitNullCheck(instruction);
6603 }
6604
6605 codegen_->MarkGCCard(temp1, temp2, array, value, instruction->GetValueCanBeNull());
6606
6607 if (done.IsReferenced()) {
6608 __ Bind(&done);
6609 }
6610
6611 if (slow_path != nullptr) {
6612 __ Bind(slow_path->GetExitLabel());
6613 }
6614
6615 break;
6616 }
6617
6618 case Primitive::kPrimLong: {
6619 Location value = locations->InAt(2);
6620 if (index.IsConstant()) {
6621 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00006622 (Int32ConstantFrom(index) << TIMES_8) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006623 GetAssembler()->StoreToOffset(kStoreWordPair, LowRegisterFrom(value), array, offset);
6624 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006625 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006626 vixl32::Register temp = temps.Acquire();
6627 __ Add(temp, array, Operand(RegisterFrom(index), vixl32::LSL, TIMES_8));
6628 GetAssembler()->StoreToOffset(kStoreWordPair, LowRegisterFrom(value), temp, data_offset);
6629 }
6630 break;
6631 }
6632
6633 case Primitive::kPrimFloat: {
6634 Location value = locations->InAt(2);
6635 DCHECK(value.IsFpuRegister());
6636 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006637 size_t offset = (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006638 GetAssembler()->StoreSToOffset(SRegisterFrom(value), array, offset);
6639 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006640 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006641 vixl32::Register temp = temps.Acquire();
6642 __ Add(temp, array, Operand(RegisterFrom(index), vixl32::LSL, TIMES_4));
6643 GetAssembler()->StoreSToOffset(SRegisterFrom(value), temp, data_offset);
6644 }
6645 break;
6646 }
6647
6648 case Primitive::kPrimDouble: {
6649 Location value = locations->InAt(2);
6650 DCHECK(value.IsFpuRegisterPair());
6651 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006652 size_t offset = (Int32ConstantFrom(index) << TIMES_8) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006653 GetAssembler()->StoreDToOffset(DRegisterFrom(value), array, offset);
6654 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006655 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006656 vixl32::Register temp = temps.Acquire();
6657 __ Add(temp, array, Operand(RegisterFrom(index), vixl32::LSL, TIMES_8));
6658 GetAssembler()->StoreDToOffset(DRegisterFrom(value), temp, data_offset);
6659 }
6660 break;
6661 }
6662
6663 case Primitive::kPrimVoid:
6664 LOG(FATAL) << "Unreachable type " << value_type;
6665 UNREACHABLE();
6666 }
6667
6668 // Objects are handled in the switch.
6669 if (value_type != Primitive::kPrimNot) {
Alexandre Rames374ddf32016-11-04 10:40:49 +00006670 // TODO(VIXL): Ensure we record the pc position immediately after the preceding store
6671 // instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01006672 codegen_->MaybeRecordImplicitNullCheck(instruction);
6673 }
6674}
6675
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006676void LocationsBuilderARMVIXL::VisitArrayLength(HArrayLength* instruction) {
6677 LocationSummary* locations =
6678 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6679 locations->SetInAt(0, Location::RequiresRegister());
6680 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6681}
6682
6683void InstructionCodeGeneratorARMVIXL::VisitArrayLength(HArrayLength* instruction) {
6684 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
6685 vixl32::Register obj = InputRegisterAt(instruction, 0);
6686 vixl32::Register out = OutputRegister(instruction);
Alexandre Rames374ddf32016-11-04 10:40:49 +00006687 {
Artem Serov0fb37192016-12-06 18:13:40 +00006688 ExactAssemblyScope aas(GetVIXLAssembler(),
6689 vixl32::kMaxInstructionSizeInBytes,
6690 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00006691 __ ldr(out, MemOperand(obj, offset));
6692 codegen_->MaybeRecordImplicitNullCheck(instruction);
6693 }
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006694 // Mask out compression flag from String's array length.
6695 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006696 __ Lsr(out, out, 1u);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006697 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006698}
6699
Artem Serov2bbc9532016-10-21 11:51:50 +01006700void LocationsBuilderARMVIXL::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Artem Serov2bbc9532016-10-21 11:51:50 +01006701 LocationSummary* locations =
6702 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6703
6704 locations->SetInAt(0, Location::RequiresRegister());
6705 locations->SetInAt(1, Location::RegisterOrConstant(instruction->GetOffset()));
6706 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6707}
6708
6709void InstructionCodeGeneratorARMVIXL::VisitIntermediateAddress(HIntermediateAddress* instruction) {
6710 vixl32::Register out = OutputRegister(instruction);
6711 vixl32::Register first = InputRegisterAt(instruction, 0);
6712 Location second = instruction->GetLocations()->InAt(1);
6713
Artem Serov2bbc9532016-10-21 11:51:50 +01006714 if (second.IsRegister()) {
6715 __ Add(out, first, RegisterFrom(second));
6716 } else {
Anton Kirilov644032c2016-12-06 17:51:43 +00006717 __ Add(out, first, Int32ConstantFrom(second));
Artem Serov2bbc9532016-10-21 11:51:50 +01006718 }
6719}
6720
Artem Serove1811ed2017-04-27 16:50:47 +01006721void LocationsBuilderARMVIXL::VisitIntermediateAddressIndex(
6722 HIntermediateAddressIndex* instruction) {
6723 LOG(FATAL) << "Unreachable " << instruction->GetId();
6724}
6725
6726void InstructionCodeGeneratorARMVIXL::VisitIntermediateAddressIndex(
6727 HIntermediateAddressIndex* instruction) {
6728 LOG(FATAL) << "Unreachable " << instruction->GetId();
6729}
6730
Scott Wakelingc34dba72016-10-03 10:14:44 +01006731void LocationsBuilderARMVIXL::VisitBoundsCheck(HBoundsCheck* instruction) {
6732 RegisterSet caller_saves = RegisterSet::Empty();
6733 InvokeRuntimeCallingConventionARMVIXL calling_convention;
6734 caller_saves.Add(LocationFrom(calling_convention.GetRegisterAt(0)));
6735 caller_saves.Add(LocationFrom(calling_convention.GetRegisterAt(1)));
6736 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Artem Serov2dd053d2017-03-08 14:54:06 +00006737
6738 HInstruction* index = instruction->InputAt(0);
6739 HInstruction* length = instruction->InputAt(1);
6740 // If both index and length are constants we can statically check the bounds. But if at least one
6741 // of them is not encodable ArmEncodableConstantOrRegister will create
6742 // Location::RequiresRegister() which is not desired to happen. Instead we create constant
6743 // locations.
6744 bool both_const = index->IsConstant() && length->IsConstant();
6745 locations->SetInAt(0, both_const
6746 ? Location::ConstantLocation(index->AsConstant())
6747 : ArmEncodableConstantOrRegister(index, CMP));
6748 locations->SetInAt(1, both_const
6749 ? Location::ConstantLocation(length->AsConstant())
6750 : ArmEncodableConstantOrRegister(length, CMP));
Scott Wakelingc34dba72016-10-03 10:14:44 +01006751}
6752
6753void InstructionCodeGeneratorARMVIXL::VisitBoundsCheck(HBoundsCheck* instruction) {
Artem Serov2dd053d2017-03-08 14:54:06 +00006754 LocationSummary* locations = instruction->GetLocations();
6755 Location index_loc = locations->InAt(0);
6756 Location length_loc = locations->InAt(1);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006757
Artem Serov2dd053d2017-03-08 14:54:06 +00006758 if (length_loc.IsConstant()) {
6759 int32_t length = Int32ConstantFrom(length_loc);
6760 if (index_loc.IsConstant()) {
6761 // BCE will remove the bounds check if we are guaranteed to pass.
6762 int32_t index = Int32ConstantFrom(index_loc);
6763 if (index < 0 || index >= length) {
6764 SlowPathCodeARMVIXL* slow_path =
6765 new (GetGraph()->GetArena()) BoundsCheckSlowPathARMVIXL(instruction);
6766 codegen_->AddSlowPath(slow_path);
6767 __ B(slow_path->GetEntryLabel());
6768 } else {
6769 // Some optimization after BCE may have generated this, and we should not
6770 // generate a bounds check if it is a valid range.
6771 }
6772 return;
6773 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006774
Artem Serov2dd053d2017-03-08 14:54:06 +00006775 SlowPathCodeARMVIXL* slow_path =
6776 new (GetGraph()->GetArena()) BoundsCheckSlowPathARMVIXL(instruction);
6777 __ Cmp(RegisterFrom(index_loc), length);
6778 codegen_->AddSlowPath(slow_path);
6779 __ B(hs, slow_path->GetEntryLabel());
6780 } else {
6781 SlowPathCodeARMVIXL* slow_path =
6782 new (GetGraph()->GetArena()) BoundsCheckSlowPathARMVIXL(instruction);
6783 __ Cmp(RegisterFrom(length_loc), InputOperandAt(instruction, 0));
6784 codegen_->AddSlowPath(slow_path);
6785 __ B(ls, slow_path->GetEntryLabel());
6786 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006787}
6788
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006789void CodeGeneratorARMVIXL::MarkGCCard(vixl32::Register temp,
6790 vixl32::Register card,
6791 vixl32::Register object,
6792 vixl32::Register value,
6793 bool can_be_null) {
6794 vixl32::Label is_null;
6795 if (can_be_null) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00006796 __ CompareAndBranchIfZero(value, &is_null);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006797 }
6798 GetAssembler()->LoadFromOffset(
6799 kLoadWord, card, tr, Thread::CardTableOffset<kArmPointerSize>().Int32Value());
Scott Wakelingb77051e2016-11-21 19:46:00 +00006800 __ Lsr(temp, object, Operand::From(gc::accounting::CardTable::kCardShift));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006801 __ Strb(card, MemOperand(card, temp));
6802 if (can_be_null) {
6803 __ Bind(&is_null);
6804 }
6805}
6806
Scott Wakelingfe885462016-09-22 10:24:38 +01006807void LocationsBuilderARMVIXL::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
6808 LOG(FATAL) << "Unreachable";
6809}
6810
6811void InstructionCodeGeneratorARMVIXL::VisitParallelMove(HParallelMove* instruction) {
6812 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
6813}
6814
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006815void LocationsBuilderARMVIXL::VisitSuspendCheck(HSuspendCheck* instruction) {
Artem Serov657022c2016-11-23 14:19:38 +00006816 LocationSummary* locations =
6817 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
6818 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006819}
6820
6821void InstructionCodeGeneratorARMVIXL::VisitSuspendCheck(HSuspendCheck* instruction) {
6822 HBasicBlock* block = instruction->GetBlock();
6823 if (block->GetLoopInformation() != nullptr) {
6824 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
6825 // The back edge will generate the suspend check.
6826 return;
6827 }
6828 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
6829 // The goto will generate the suspend check.
6830 return;
6831 }
6832 GenerateSuspendCheck(instruction, nullptr);
Roland Levillain5daa4952017-07-03 17:23:56 +01006833 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 12);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006834}
6835
6836void InstructionCodeGeneratorARMVIXL::GenerateSuspendCheck(HSuspendCheck* instruction,
6837 HBasicBlock* successor) {
6838 SuspendCheckSlowPathARMVIXL* slow_path =
6839 down_cast<SuspendCheckSlowPathARMVIXL*>(instruction->GetSlowPath());
6840 if (slow_path == nullptr) {
6841 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARMVIXL(instruction, successor);
6842 instruction->SetSlowPath(slow_path);
6843 codegen_->AddSlowPath(slow_path);
6844 if (successor != nullptr) {
6845 DCHECK(successor->IsLoopHeader());
6846 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
6847 }
6848 } else {
6849 DCHECK_EQ(slow_path->GetSuccessor(), successor);
6850 }
6851
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006852 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006853 vixl32::Register temp = temps.Acquire();
6854 GetAssembler()->LoadFromOffset(
6855 kLoadUnsignedHalfword, temp, tr, Thread::ThreadFlagsOffset<kArmPointerSize>().Int32Value());
6856 if (successor == nullptr) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00006857 __ CompareAndBranchIfNonZero(temp, slow_path->GetEntryLabel());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006858 __ Bind(slow_path->GetReturnLabel());
6859 } else {
xueliang.zhongf51bc622016-11-04 09:23:32 +00006860 __ CompareAndBranchIfZero(temp, codegen_->GetLabelOf(successor));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006861 __ B(slow_path->GetEntryLabel());
6862 }
6863}
6864
Scott Wakelingfe885462016-09-22 10:24:38 +01006865ArmVIXLAssembler* ParallelMoveResolverARMVIXL::GetAssembler() const {
6866 return codegen_->GetAssembler();
6867}
6868
6869void ParallelMoveResolverARMVIXL::EmitMove(size_t index) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006870 UseScratchRegisterScope temps(GetAssembler()->GetVIXLAssembler());
Scott Wakelingfe885462016-09-22 10:24:38 +01006871 MoveOperands* move = moves_[index];
6872 Location source = move->GetSource();
6873 Location destination = move->GetDestination();
6874
6875 if (source.IsRegister()) {
6876 if (destination.IsRegister()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006877 __ Mov(RegisterFrom(destination), RegisterFrom(source));
Scott Wakelingfe885462016-09-22 10:24:38 +01006878 } else if (destination.IsFpuRegister()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006879 __ Vmov(SRegisterFrom(destination), RegisterFrom(source));
Scott Wakelingfe885462016-09-22 10:24:38 +01006880 } else {
6881 DCHECK(destination.IsStackSlot());
6882 GetAssembler()->StoreToOffset(kStoreWord,
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006883 RegisterFrom(source),
Scott Wakelingfe885462016-09-22 10:24:38 +01006884 sp,
6885 destination.GetStackIndex());
6886 }
6887 } else if (source.IsStackSlot()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006888 if (destination.IsRegister()) {
6889 GetAssembler()->LoadFromOffset(kLoadWord,
6890 RegisterFrom(destination),
6891 sp,
6892 source.GetStackIndex());
6893 } else if (destination.IsFpuRegister()) {
6894 GetAssembler()->LoadSFromOffset(SRegisterFrom(destination), sp, source.GetStackIndex());
6895 } else {
6896 DCHECK(destination.IsStackSlot());
6897 vixl32::Register temp = temps.Acquire();
6898 GetAssembler()->LoadFromOffset(kLoadWord, temp, sp, source.GetStackIndex());
6899 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
6900 }
Scott Wakelingfe885462016-09-22 10:24:38 +01006901 } else if (source.IsFpuRegister()) {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01006902 if (destination.IsRegister()) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006903 __ Vmov(RegisterFrom(destination), SRegisterFrom(source));
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01006904 } else if (destination.IsFpuRegister()) {
6905 __ Vmov(SRegisterFrom(destination), SRegisterFrom(source));
6906 } else {
6907 DCHECK(destination.IsStackSlot());
6908 GetAssembler()->StoreSToOffset(SRegisterFrom(source), sp, destination.GetStackIndex());
6909 }
Scott Wakelingfe885462016-09-22 10:24:38 +01006910 } else if (source.IsDoubleStackSlot()) {
Alexandre Rames9c19bd62016-10-24 11:50:32 +01006911 if (destination.IsDoubleStackSlot()) {
6912 vixl32::DRegister temp = temps.AcquireD();
6913 GetAssembler()->LoadDFromOffset(temp, sp, source.GetStackIndex());
6914 GetAssembler()->StoreDToOffset(temp, sp, destination.GetStackIndex());
6915 } else if (destination.IsRegisterPair()) {
6916 DCHECK(ExpectedPairLayout(destination));
6917 GetAssembler()->LoadFromOffset(
6918 kLoadWordPair, LowRegisterFrom(destination), sp, source.GetStackIndex());
6919 } else {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01006920 DCHECK(destination.IsFpuRegisterPair()) << destination;
6921 GetAssembler()->LoadDFromOffset(DRegisterFrom(destination), sp, source.GetStackIndex());
Alexandre Rames9c19bd62016-10-24 11:50:32 +01006922 }
Scott Wakelingfe885462016-09-22 10:24:38 +01006923 } else if (source.IsRegisterPair()) {
6924 if (destination.IsRegisterPair()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006925 __ Mov(LowRegisterFrom(destination), LowRegisterFrom(source));
6926 __ Mov(HighRegisterFrom(destination), HighRegisterFrom(source));
Scott Wakelingfe885462016-09-22 10:24:38 +01006927 } else if (destination.IsFpuRegisterPair()) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006928 __ Vmov(DRegisterFrom(destination), LowRegisterFrom(source), HighRegisterFrom(source));
Scott Wakelingfe885462016-09-22 10:24:38 +01006929 } else {
6930 DCHECK(destination.IsDoubleStackSlot()) << destination;
6931 DCHECK(ExpectedPairLayout(source));
6932 GetAssembler()->StoreToOffset(kStoreWordPair,
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006933 LowRegisterFrom(source),
Scott Wakelingfe885462016-09-22 10:24:38 +01006934 sp,
6935 destination.GetStackIndex());
6936 }
6937 } else if (source.IsFpuRegisterPair()) {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01006938 if (destination.IsRegisterPair()) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006939 __ Vmov(LowRegisterFrom(destination), HighRegisterFrom(destination), DRegisterFrom(source));
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01006940 } else if (destination.IsFpuRegisterPair()) {
6941 __ Vmov(DRegisterFrom(destination), DRegisterFrom(source));
6942 } else {
6943 DCHECK(destination.IsDoubleStackSlot()) << destination;
6944 GetAssembler()->StoreDToOffset(DRegisterFrom(source), sp, destination.GetStackIndex());
6945 }
Scott Wakelingfe885462016-09-22 10:24:38 +01006946 } else {
6947 DCHECK(source.IsConstant()) << source;
6948 HConstant* constant = source.GetConstant();
6949 if (constant->IsIntConstant() || constant->IsNullConstant()) {
6950 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
6951 if (destination.IsRegister()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006952 __ Mov(RegisterFrom(destination), value);
Scott Wakelingfe885462016-09-22 10:24:38 +01006953 } else {
6954 DCHECK(destination.IsStackSlot());
Scott Wakelingfe885462016-09-22 10:24:38 +01006955 vixl32::Register temp = temps.Acquire();
6956 __ Mov(temp, value);
6957 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
6958 }
6959 } else if (constant->IsLongConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006960 int64_t value = Int64ConstantFrom(source);
Scott Wakelingfe885462016-09-22 10:24:38 +01006961 if (destination.IsRegisterPair()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006962 __ Mov(LowRegisterFrom(destination), Low32Bits(value));
6963 __ Mov(HighRegisterFrom(destination), High32Bits(value));
Scott Wakelingfe885462016-09-22 10:24:38 +01006964 } else {
6965 DCHECK(destination.IsDoubleStackSlot()) << destination;
Scott Wakelingfe885462016-09-22 10:24:38 +01006966 vixl32::Register temp = temps.Acquire();
6967 __ Mov(temp, Low32Bits(value));
6968 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
6969 __ Mov(temp, High32Bits(value));
6970 GetAssembler()->StoreToOffset(kStoreWord,
6971 temp,
6972 sp,
6973 destination.GetHighStackIndex(kArmWordSize));
6974 }
6975 } else if (constant->IsDoubleConstant()) {
6976 double value = constant->AsDoubleConstant()->GetValue();
6977 if (destination.IsFpuRegisterPair()) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006978 __ Vmov(DRegisterFrom(destination), value);
Scott Wakelingfe885462016-09-22 10:24:38 +01006979 } else {
6980 DCHECK(destination.IsDoubleStackSlot()) << destination;
6981 uint64_t int_value = bit_cast<uint64_t, double>(value);
Scott Wakelingfe885462016-09-22 10:24:38 +01006982 vixl32::Register temp = temps.Acquire();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006983 __ Mov(temp, Low32Bits(int_value));
Scott Wakelingfe885462016-09-22 10:24:38 +01006984 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006985 __ Mov(temp, High32Bits(int_value));
Scott Wakelingfe885462016-09-22 10:24:38 +01006986 GetAssembler()->StoreToOffset(kStoreWord,
6987 temp,
6988 sp,
6989 destination.GetHighStackIndex(kArmWordSize));
6990 }
6991 } else {
6992 DCHECK(constant->IsFloatConstant()) << constant->DebugName();
6993 float value = constant->AsFloatConstant()->GetValue();
6994 if (destination.IsFpuRegister()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006995 __ Vmov(SRegisterFrom(destination), value);
Scott Wakelingfe885462016-09-22 10:24:38 +01006996 } else {
6997 DCHECK(destination.IsStackSlot());
Scott Wakelingfe885462016-09-22 10:24:38 +01006998 vixl32::Register temp = temps.Acquire();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006999 __ Mov(temp, bit_cast<int32_t, float>(value));
Scott Wakelingfe885462016-09-22 10:24:38 +01007000 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
7001 }
7002 }
7003 }
7004}
7005
Alexandre Rames9c19bd62016-10-24 11:50:32 +01007006void ParallelMoveResolverARMVIXL::Exchange(vixl32::Register reg, int mem) {
7007 UseScratchRegisterScope temps(GetAssembler()->GetVIXLAssembler());
7008 vixl32::Register temp = temps.Acquire();
7009 __ Mov(temp, reg);
7010 GetAssembler()->LoadFromOffset(kLoadWord, reg, sp, mem);
7011 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, mem);
Scott Wakelingfe885462016-09-22 10:24:38 +01007012}
7013
Alexandre Rames9c19bd62016-10-24 11:50:32 +01007014void ParallelMoveResolverARMVIXL::Exchange(int mem1, int mem2) {
7015 // TODO(VIXL32): Double check the performance of this implementation.
7016 UseScratchRegisterScope temps(GetAssembler()->GetVIXLAssembler());
Nicolas Geoffray13a797b2017-03-15 16:41:31 +00007017 vixl32::Register temp1 = temps.Acquire();
7018 ScratchRegisterScope ensure_scratch(
7019 this, temp1.GetCode(), r0.GetCode(), codegen_->GetNumberOfCoreRegisters());
7020 vixl32::Register temp2(ensure_scratch.GetRegister());
Alexandre Rames9c19bd62016-10-24 11:50:32 +01007021
Nicolas Geoffray13a797b2017-03-15 16:41:31 +00007022 int stack_offset = ensure_scratch.IsSpilled() ? kArmWordSize : 0;
7023 GetAssembler()->LoadFromOffset(kLoadWord, temp1, sp, mem1 + stack_offset);
7024 GetAssembler()->LoadFromOffset(kLoadWord, temp2, sp, mem2 + stack_offset);
7025 GetAssembler()->StoreToOffset(kStoreWord, temp1, sp, mem2 + stack_offset);
7026 GetAssembler()->StoreToOffset(kStoreWord, temp2, sp, mem1 + stack_offset);
Scott Wakelingfe885462016-09-22 10:24:38 +01007027}
7028
Alexandre Rames9c19bd62016-10-24 11:50:32 +01007029void ParallelMoveResolverARMVIXL::EmitSwap(size_t index) {
7030 MoveOperands* move = moves_[index];
7031 Location source = move->GetSource();
7032 Location destination = move->GetDestination();
7033 UseScratchRegisterScope temps(GetAssembler()->GetVIXLAssembler());
7034
7035 if (source.IsRegister() && destination.IsRegister()) {
7036 vixl32::Register temp = temps.Acquire();
7037 DCHECK(!RegisterFrom(source).Is(temp));
7038 DCHECK(!RegisterFrom(destination).Is(temp));
7039 __ Mov(temp, RegisterFrom(destination));
7040 __ Mov(RegisterFrom(destination), RegisterFrom(source));
7041 __ Mov(RegisterFrom(source), temp);
7042 } else if (source.IsRegister() && destination.IsStackSlot()) {
7043 Exchange(RegisterFrom(source), destination.GetStackIndex());
7044 } else if (source.IsStackSlot() && destination.IsRegister()) {
7045 Exchange(RegisterFrom(destination), source.GetStackIndex());
7046 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Anton Kirilovdda43962016-11-21 19:55:20 +00007047 Exchange(source.GetStackIndex(), destination.GetStackIndex());
Alexandre Rames9c19bd62016-10-24 11:50:32 +01007048 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Nicolas Geoffray13a797b2017-03-15 16:41:31 +00007049 vixl32::Register temp = temps.Acquire();
Anton Kirilovdda43962016-11-21 19:55:20 +00007050 __ Vmov(temp, SRegisterFrom(source));
7051 __ Vmov(SRegisterFrom(source), SRegisterFrom(destination));
7052 __ Vmov(SRegisterFrom(destination), temp);
Alexandre Rames9c19bd62016-10-24 11:50:32 +01007053 } else if (source.IsRegisterPair() && destination.IsRegisterPair()) {
7054 vixl32::DRegister temp = temps.AcquireD();
7055 __ Vmov(temp, LowRegisterFrom(source), HighRegisterFrom(source));
7056 __ Mov(LowRegisterFrom(source), LowRegisterFrom(destination));
7057 __ Mov(HighRegisterFrom(source), HighRegisterFrom(destination));
7058 __ Vmov(LowRegisterFrom(destination), HighRegisterFrom(destination), temp);
7059 } else if (source.IsRegisterPair() || destination.IsRegisterPair()) {
7060 vixl32::Register low_reg = LowRegisterFrom(source.IsRegisterPair() ? source : destination);
7061 int mem = source.IsRegisterPair() ? destination.GetStackIndex() : source.GetStackIndex();
7062 DCHECK(ExpectedPairLayout(source.IsRegisterPair() ? source : destination));
7063 vixl32::DRegister temp = temps.AcquireD();
7064 __ Vmov(temp, low_reg, vixl32::Register(low_reg.GetCode() + 1));
7065 GetAssembler()->LoadFromOffset(kLoadWordPair, low_reg, sp, mem);
7066 GetAssembler()->StoreDToOffset(temp, sp, mem);
7067 } else if (source.IsFpuRegisterPair() && destination.IsFpuRegisterPair()) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007068 vixl32::DRegister first = DRegisterFrom(source);
7069 vixl32::DRegister second = DRegisterFrom(destination);
7070 vixl32::DRegister temp = temps.AcquireD();
7071 __ Vmov(temp, first);
7072 __ Vmov(first, second);
7073 __ Vmov(second, temp);
Alexandre Rames9c19bd62016-10-24 11:50:32 +01007074 } else if (source.IsFpuRegisterPair() || destination.IsFpuRegisterPair()) {
Anton Kirilovdda43962016-11-21 19:55:20 +00007075 vixl32::DRegister reg = source.IsFpuRegisterPair()
7076 ? DRegisterFrom(source)
7077 : DRegisterFrom(destination);
7078 int mem = source.IsFpuRegisterPair()
7079 ? destination.GetStackIndex()
7080 : source.GetStackIndex();
7081 vixl32::DRegister temp = temps.AcquireD();
7082 __ Vmov(temp, reg);
7083 GetAssembler()->LoadDFromOffset(reg, sp, mem);
7084 GetAssembler()->StoreDToOffset(temp, sp, mem);
Alexandre Rames9c19bd62016-10-24 11:50:32 +01007085 } else if (source.IsFpuRegister() || destination.IsFpuRegister()) {
Anton Kirilovdda43962016-11-21 19:55:20 +00007086 vixl32::SRegister reg = source.IsFpuRegister()
7087 ? SRegisterFrom(source)
7088 : SRegisterFrom(destination);
7089 int mem = source.IsFpuRegister()
7090 ? destination.GetStackIndex()
7091 : source.GetStackIndex();
7092 vixl32::Register temp = temps.Acquire();
7093 __ Vmov(temp, reg);
7094 GetAssembler()->LoadSFromOffset(reg, sp, mem);
7095 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, mem);
Alexandre Rames9c19bd62016-10-24 11:50:32 +01007096 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
7097 vixl32::DRegister temp1 = temps.AcquireD();
7098 vixl32::DRegister temp2 = temps.AcquireD();
7099 __ Vldr(temp1, MemOperand(sp, source.GetStackIndex()));
7100 __ Vldr(temp2, MemOperand(sp, destination.GetStackIndex()));
7101 __ Vstr(temp1, MemOperand(sp, destination.GetStackIndex()));
7102 __ Vstr(temp2, MemOperand(sp, source.GetStackIndex()));
7103 } else {
7104 LOG(FATAL) << "Unimplemented" << source << " <-> " << destination;
7105 }
Scott Wakelingfe885462016-09-22 10:24:38 +01007106}
7107
Nicolas Geoffray13a797b2017-03-15 16:41:31 +00007108void ParallelMoveResolverARMVIXL::SpillScratch(int reg) {
7109 __ Push(vixl32::Register(reg));
Scott Wakelingfe885462016-09-22 10:24:38 +01007110}
7111
Nicolas Geoffray13a797b2017-03-15 16:41:31 +00007112void ParallelMoveResolverARMVIXL::RestoreScratch(int reg) {
7113 __ Pop(vixl32::Register(reg));
Scott Wakelingfe885462016-09-22 10:24:38 +01007114}
7115
Artem Serov02d37832016-10-25 15:25:33 +01007116HLoadClass::LoadKind CodeGeneratorARMVIXL::GetSupportedLoadClassKind(
Artem Serovd4cc5b22016-11-04 11:19:09 +00007117 HLoadClass::LoadKind desired_class_load_kind) {
7118 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00007119 case HLoadClass::LoadKind::kInvalid:
7120 LOG(FATAL) << "UNREACHABLE";
7121 UNREACHABLE();
Artem Serovd4cc5b22016-11-04 11:19:09 +00007122 case HLoadClass::LoadKind::kReferrersClass:
7123 break;
Artem Serovd4cc5b22016-11-04 11:19:09 +00007124 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007125 case HLoadClass::LoadKind::kBssEntry:
7126 DCHECK(!Runtime::Current()->UseJitCompilation());
7127 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007128 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007129 DCHECK(Runtime::Current()->UseJitCompilation());
Artem Serovc5fcb442016-12-02 19:19:58 +00007130 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01007131 case HLoadClass::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007132 case HLoadClass::LoadKind::kRuntimeCall:
Artem Serovd4cc5b22016-11-04 11:19:09 +00007133 break;
7134 }
7135 return desired_class_load_kind;
Artem Serov02d37832016-10-25 15:25:33 +01007136}
7137
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007138void LocationsBuilderARMVIXL::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00007139 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007140 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007141 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00007142 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007143 cls,
7144 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00007145 LocationFrom(r0));
Vladimir Markoea4c1262017-02-06 19:59:33 +00007146 DCHECK(calling_convention.GetRegisterAt(0).Is(r0));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007147 return;
7148 }
Vladimir Marko41559982017-01-06 14:04:23 +00007149 DCHECK(!cls->NeedsAccessCheck());
Scott Wakelingfe885462016-09-22 10:24:38 +01007150
Artem Serovd4cc5b22016-11-04 11:19:09 +00007151 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
7152 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007153 ? LocationSummary::kCallOnSlowPath
7154 : LocationSummary::kNoCall;
7155 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Artem Serovd4cc5b22016-11-04 11:19:09 +00007156 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007157 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Artem Serovd4cc5b22016-11-04 11:19:09 +00007158 }
7159
Vladimir Marko41559982017-01-06 14:04:23 +00007160 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007161 locations->SetInAt(0, Location::RequiresRegister());
7162 }
7163 locations->SetOut(Location::RequiresRegister());
Vladimir Markoea4c1262017-02-06 19:59:33 +00007164 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
7165 if (!kUseReadBarrier || kUseBakerReadBarrier) {
7166 // Rely on the type resolution or initialization and marking to save everything we need.
7167 // Note that IP may be clobbered by saving/restoring the live register (only one thanks
7168 // to the custom calling convention) or by marking, so we request a different temp.
7169 locations->AddTemp(Location::RequiresRegister());
7170 RegisterSet caller_saves = RegisterSet::Empty();
7171 InvokeRuntimeCallingConventionARMVIXL calling_convention;
7172 caller_saves.Add(LocationFrom(calling_convention.GetRegisterAt(0)));
7173 // TODO: Add GetReturnLocation() to the calling convention so that we can DCHECK()
7174 // that the the kPrimNot result register is the same as the first argument register.
7175 locations->SetCustomSlowPathCallerSaves(caller_saves);
7176 } else {
7177 // For non-Baker read barrier we have a temp-clobbering call.
7178 }
7179 }
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01007180 if (kUseBakerReadBarrier && kBakerReadBarrierLinkTimeThunksEnableForGcRoots) {
7181 if (load_kind == HLoadClass::LoadKind::kBssEntry ||
7182 (load_kind == HLoadClass::LoadKind::kReferrersClass &&
7183 !Runtime::Current()->UseJitCompilation())) {
7184 locations->AddTemp(Location::RegisterLocation(kBakerCcEntrypointRegister.GetCode()));
7185 }
7186 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007187}
7188
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007189// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
7190// move.
7191void InstructionCodeGeneratorARMVIXL::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00007192 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007193 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00007194 codegen_->GenerateLoadClassRuntimeCall(cls);
Roland Levillain5daa4952017-07-03 17:23:56 +01007195 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 13);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007196 return;
7197 }
Vladimir Marko41559982017-01-06 14:04:23 +00007198 DCHECK(!cls->NeedsAccessCheck());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007199
Vladimir Marko41559982017-01-06 14:04:23 +00007200 LocationSummary* locations = cls->GetLocations();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007201 Location out_loc = locations->Out();
7202 vixl32::Register out = OutputRegister(cls);
7203
Artem Serovd4cc5b22016-11-04 11:19:09 +00007204 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
7205 ? kWithoutReadBarrier
7206 : kCompilerReadBarrierOption;
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007207 bool generate_null_check = false;
Vladimir Marko41559982017-01-06 14:04:23 +00007208 switch (load_kind) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007209 case HLoadClass::LoadKind::kReferrersClass: {
7210 DCHECK(!cls->CanCallRuntime());
7211 DCHECK(!cls->MustGenerateClinitCheck());
7212 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
7213 vixl32::Register current_method = InputRegisterAt(cls, 0);
7214 GenerateGcRootFieldLoad(cls,
7215 out_loc,
7216 current_method,
Roland Levillain00468f32016-10-27 18:02:48 +01007217 ArtMethod::DeclaringClassOffset().Int32Value(),
Artem Serovd4cc5b22016-11-04 11:19:09 +00007218 read_barrier_option);
7219 break;
7220 }
Artem Serovd4cc5b22016-11-04 11:19:09 +00007221 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007222 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Artem Serovd4cc5b22016-11-04 11:19:09 +00007223 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
7224 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
7225 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
7226 codegen_->EmitMovwMovtPlaceholder(labels, out);
7227 break;
7228 }
7229 case HLoadClass::LoadKind::kBootImageAddress: {
Artem Serovc5fcb442016-12-02 19:19:58 +00007230 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007231 uint32_t address = dchecked_integral_cast<uint32_t>(
7232 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
7233 DCHECK_NE(address, 0u);
Artem Serovc5fcb442016-12-02 19:19:58 +00007234 __ Ldr(out, codegen_->DeduplicateBootImageAddressLiteral(address));
Artem Serovd4cc5b22016-11-04 11:19:09 +00007235 break;
7236 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007237 case HLoadClass::LoadKind::kBssEntry: {
Vladimir Markoea4c1262017-02-06 19:59:33 +00007238 vixl32::Register temp = (!kUseReadBarrier || kUseBakerReadBarrier)
7239 ? RegisterFrom(locations->GetTemp(0))
7240 : out;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007241 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
Vladimir Marko1998cd02017-01-13 13:02:58 +00007242 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex());
Vladimir Markoea4c1262017-02-06 19:59:33 +00007243 codegen_->EmitMovwMovtPlaceholder(labels, temp);
7244 GenerateGcRootFieldLoad(cls, out_loc, temp, /* offset */ 0, read_barrier_option);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007245 generate_null_check = true;
7246 break;
7247 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007248 case HLoadClass::LoadKind::kJitTableAddress: {
Artem Serovc5fcb442016-12-02 19:19:58 +00007249 __ Ldr(out, codegen_->DeduplicateJitClassLiteral(cls->GetDexFile(),
7250 cls->GetTypeIndex(),
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007251 cls->GetClass()));
Artem Serovc5fcb442016-12-02 19:19:58 +00007252 // /* GcRoot<mirror::Class> */ out = *out
Vladimir Markoea4c1262017-02-06 19:59:33 +00007253 GenerateGcRootFieldLoad(cls, out_loc, out, /* offset */ 0, read_barrier_option);
Artem Serovd4cc5b22016-11-04 11:19:09 +00007254 break;
7255 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007256 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00007257 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00007258 LOG(FATAL) << "UNREACHABLE";
7259 UNREACHABLE();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007260 }
7261
7262 if (generate_null_check || cls->MustGenerateClinitCheck()) {
7263 DCHECK(cls->CanCallRuntime());
7264 LoadClassSlowPathARMVIXL* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARMVIXL(
7265 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
7266 codegen_->AddSlowPath(slow_path);
7267 if (generate_null_check) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00007268 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007269 }
7270 if (cls->MustGenerateClinitCheck()) {
7271 GenerateClassInitializationCheck(slow_path, out);
7272 } else {
7273 __ Bind(slow_path->GetExitLabel());
7274 }
Roland Levillain5daa4952017-07-03 17:23:56 +01007275 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 14);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007276 }
7277}
7278
Artem Serov02d37832016-10-25 15:25:33 +01007279void LocationsBuilderARMVIXL::VisitClinitCheck(HClinitCheck* check) {
7280 LocationSummary* locations =
7281 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
7282 locations->SetInAt(0, Location::RequiresRegister());
7283 if (check->HasUses()) {
7284 locations->SetOut(Location::SameAsFirstInput());
7285 }
7286}
7287
7288void InstructionCodeGeneratorARMVIXL::VisitClinitCheck(HClinitCheck* check) {
7289 // We assume the class is not null.
7290 LoadClassSlowPathARMVIXL* slow_path =
7291 new (GetGraph()->GetArena()) LoadClassSlowPathARMVIXL(check->GetLoadClass(),
7292 check,
7293 check->GetDexPc(),
7294 /* do_clinit */ true);
7295 codegen_->AddSlowPath(slow_path);
7296 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
7297}
7298
7299void InstructionCodeGeneratorARMVIXL::GenerateClassInitializationCheck(
7300 LoadClassSlowPathARMVIXL* slow_path, vixl32::Register class_reg) {
7301 UseScratchRegisterScope temps(GetVIXLAssembler());
7302 vixl32::Register temp = temps.Acquire();
7303 GetAssembler()->LoadFromOffset(kLoadWord,
7304 temp,
7305 class_reg,
7306 mirror::Class::StatusOffset().Int32Value());
7307 __ Cmp(temp, mirror::Class::kStatusInitialized);
7308 __ B(lt, slow_path->GetEntryLabel());
7309 // Even if the initialized flag is set, we may be in a situation where caches are not synced
7310 // properly. Therefore, we do a memory fence.
7311 __ Dmb(ISH);
7312 __ Bind(slow_path->GetExitLabel());
7313}
7314
Artem Serov02d37832016-10-25 15:25:33 +01007315HLoadString::LoadKind CodeGeneratorARMVIXL::GetSupportedLoadStringKind(
Artem Serovd4cc5b22016-11-04 11:19:09 +00007316 HLoadString::LoadKind desired_string_load_kind) {
7317 switch (desired_string_load_kind) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00007318 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007319 case HLoadString::LoadKind::kBootImageInternTable:
Artem Serovd4cc5b22016-11-04 11:19:09 +00007320 case HLoadString::LoadKind::kBssEntry:
7321 DCHECK(!Runtime::Current()->UseJitCompilation());
7322 break;
7323 case HLoadString::LoadKind::kJitTableAddress:
7324 DCHECK(Runtime::Current()->UseJitCompilation());
Artem Serovc5fcb442016-12-02 19:19:58 +00007325 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01007326 case HLoadString::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007327 case HLoadString::LoadKind::kRuntimeCall:
Artem Serovd4cc5b22016-11-04 11:19:09 +00007328 break;
7329 }
7330 return desired_string_load_kind;
Artem Serov02d37832016-10-25 15:25:33 +01007331}
7332
7333void LocationsBuilderARMVIXL::VisitLoadString(HLoadString* load) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00007334 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Artem Serov02d37832016-10-25 15:25:33 +01007335 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Artem Serov02d37832016-10-25 15:25:33 +01007336 HLoadString::LoadKind load_kind = load->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007337 if (load_kind == HLoadString::LoadKind::kRuntimeCall) {
Artem Serov02d37832016-10-25 15:25:33 +01007338 locations->SetOut(LocationFrom(r0));
7339 } else {
7340 locations->SetOut(Location::RequiresRegister());
Artem Serovd4cc5b22016-11-04 11:19:09 +00007341 if (load_kind == HLoadString::LoadKind::kBssEntry) {
7342 if (!kUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00007343 // Rely on the pResolveString and marking to save everything we need, including temps.
7344 // Note that IP may be clobbered by saving/restoring the live register (only one thanks
7345 // to the custom calling convention) or by marking, so we request a different temp.
Artem Serovd4cc5b22016-11-04 11:19:09 +00007346 locations->AddTemp(Location::RequiresRegister());
7347 RegisterSet caller_saves = RegisterSet::Empty();
7348 InvokeRuntimeCallingConventionARMVIXL calling_convention;
7349 caller_saves.Add(LocationFrom(calling_convention.GetRegisterAt(0)));
7350 // TODO: Add GetReturnLocation() to the calling convention so that we can DCHECK()
7351 // that the the kPrimNot result register is the same as the first argument register.
7352 locations->SetCustomSlowPathCallerSaves(caller_saves);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01007353 if (kUseBakerReadBarrier && kBakerReadBarrierLinkTimeThunksEnableForGcRoots) {
7354 locations->AddTemp(Location::RegisterLocation(kBakerCcEntrypointRegister.GetCode()));
7355 }
Artem Serovd4cc5b22016-11-04 11:19:09 +00007356 } else {
7357 // For non-Baker read barrier we have a temp-clobbering call.
7358 }
7359 }
Artem Serov02d37832016-10-25 15:25:33 +01007360 }
7361}
7362
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007363// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
7364// move.
7365void InstructionCodeGeneratorARMVIXL::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Artem Serovd4cc5b22016-11-04 11:19:09 +00007366 LocationSummary* locations = load->GetLocations();
7367 Location out_loc = locations->Out();
7368 vixl32::Register out = OutputRegister(load);
7369 HLoadString::LoadKind load_kind = load->GetLoadKind();
7370
7371 switch (load_kind) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00007372 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
7373 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
7374 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007375 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Artem Serovd4cc5b22016-11-04 11:19:09 +00007376 codegen_->EmitMovwMovtPlaceholder(labels, out);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007377 return;
Artem Serovd4cc5b22016-11-04 11:19:09 +00007378 }
7379 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007380 uint32_t address = dchecked_integral_cast<uint32_t>(
7381 reinterpret_cast<uintptr_t>(load->GetString().Get()));
7382 DCHECK_NE(address, 0u);
Artem Serovc5fcb442016-12-02 19:19:58 +00007383 __ Ldr(out, codegen_->DeduplicateBootImageAddressLiteral(address));
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007384 return;
7385 }
7386 case HLoadString::LoadKind::kBootImageInternTable: {
7387 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
7388 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
7389 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
7390 codegen_->EmitMovwMovtPlaceholder(labels, out);
7391 __ Ldr(out, MemOperand(out, /* offset */ 0));
7392 return;
Artem Serovd4cc5b22016-11-04 11:19:09 +00007393 }
7394 case HLoadString::LoadKind::kBssEntry: {
7395 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markoea4c1262017-02-06 19:59:33 +00007396 vixl32::Register temp = (!kUseReadBarrier || kUseBakerReadBarrier)
7397 ? RegisterFrom(locations->GetTemp(0))
7398 : out;
Artem Serovd4cc5b22016-11-04 11:19:09 +00007399 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007400 codegen_->NewStringBssEntryPatch(load->GetDexFile(), load->GetStringIndex());
Artem Serovd4cc5b22016-11-04 11:19:09 +00007401 codegen_->EmitMovwMovtPlaceholder(labels, temp);
7402 GenerateGcRootFieldLoad(load, out_loc, temp, /* offset */ 0, kCompilerReadBarrierOption);
7403 LoadStringSlowPathARMVIXL* slow_path =
7404 new (GetGraph()->GetArena()) LoadStringSlowPathARMVIXL(load);
7405 codegen_->AddSlowPath(slow_path);
7406 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
7407 __ Bind(slow_path->GetExitLabel());
Roland Levillain5daa4952017-07-03 17:23:56 +01007408 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 15);
Artem Serovd4cc5b22016-11-04 11:19:09 +00007409 return;
7410 }
7411 case HLoadString::LoadKind::kJitTableAddress: {
Artem Serovc5fcb442016-12-02 19:19:58 +00007412 __ Ldr(out, codegen_->DeduplicateJitStringLiteral(load->GetDexFile(),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007413 load->GetStringIndex(),
7414 load->GetString()));
Artem Serovc5fcb442016-12-02 19:19:58 +00007415 // /* GcRoot<mirror::String> */ out = *out
7416 GenerateGcRootFieldLoad(load, out_loc, out, /* offset */ 0, kCompilerReadBarrierOption);
7417 return;
Artem Serovd4cc5b22016-11-04 11:19:09 +00007418 }
7419 default:
7420 break;
7421 }
Artem Serov02d37832016-10-25 15:25:33 +01007422
7423 // TODO: Re-add the compiler code to do string dex cache lookup again.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007424 DCHECK_EQ(load->GetLoadKind(), HLoadString::LoadKind::kRuntimeCall);
Artem Serov02d37832016-10-25 15:25:33 +01007425 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Andreas Gampe8a0128a2016-11-28 07:38:35 -08007426 __ Mov(calling_convention.GetRegisterAt(0), load->GetStringIndex().index_);
Artem Serov02d37832016-10-25 15:25:33 +01007427 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
7428 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Roland Levillain5daa4952017-07-03 17:23:56 +01007429 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 16);
Artem Serov02d37832016-10-25 15:25:33 +01007430}
7431
7432static int32_t GetExceptionTlsOffset() {
7433 return Thread::ExceptionOffset<kArmPointerSize>().Int32Value();
7434}
7435
7436void LocationsBuilderARMVIXL::VisitLoadException(HLoadException* load) {
7437 LocationSummary* locations =
7438 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
7439 locations->SetOut(Location::RequiresRegister());
7440}
7441
7442void InstructionCodeGeneratorARMVIXL::VisitLoadException(HLoadException* load) {
7443 vixl32::Register out = OutputRegister(load);
7444 GetAssembler()->LoadFromOffset(kLoadWord, out, tr, GetExceptionTlsOffset());
7445}
7446
7447
7448void LocationsBuilderARMVIXL::VisitClearException(HClearException* clear) {
7449 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
7450}
7451
7452void InstructionCodeGeneratorARMVIXL::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
7453 UseScratchRegisterScope temps(GetVIXLAssembler());
7454 vixl32::Register temp = temps.Acquire();
7455 __ Mov(temp, 0);
7456 GetAssembler()->StoreToOffset(kStoreWord, temp, tr, GetExceptionTlsOffset());
7457}
7458
7459void LocationsBuilderARMVIXL::VisitThrow(HThrow* instruction) {
7460 LocationSummary* locations =
7461 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
7462 InvokeRuntimeCallingConventionARMVIXL calling_convention;
7463 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
7464}
7465
7466void InstructionCodeGeneratorARMVIXL::VisitThrow(HThrow* instruction) {
7467 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
7468 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
7469}
7470
Artem Serov657022c2016-11-23 14:19:38 +00007471// Temp is used for read barrier.
7472static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
7473 if (kEmitCompilerReadBarrier &&
7474 (kUseBakerReadBarrier ||
7475 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
7476 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
7477 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
7478 return 1;
7479 }
7480 return 0;
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007481}
7482
Artem Serov657022c2016-11-23 14:19:38 +00007483// Interface case has 3 temps, one for holding the number of interfaces, one for the current
7484// interface pointer, one for loading the current interface.
7485// The other checks have one temp for loading the object's class.
7486static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
7487 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
7488 return 3;
7489 }
7490 return 1 + NumberOfInstanceOfTemps(type_check_kind);
7491}
Artem Serovcfbe9132016-10-14 15:58:56 +01007492
7493void LocationsBuilderARMVIXL::VisitInstanceOf(HInstanceOf* instruction) {
7494 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
7495 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
7496 bool baker_read_barrier_slow_path = false;
7497 switch (type_check_kind) {
7498 case TypeCheckKind::kExactCheck:
7499 case TypeCheckKind::kAbstractClassCheck:
7500 case TypeCheckKind::kClassHierarchyCheck:
7501 case TypeCheckKind::kArrayObjectCheck:
7502 call_kind =
7503 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
7504 baker_read_barrier_slow_path = kUseBakerReadBarrier;
7505 break;
7506 case TypeCheckKind::kArrayCheck:
7507 case TypeCheckKind::kUnresolvedCheck:
7508 case TypeCheckKind::kInterfaceCheck:
7509 call_kind = LocationSummary::kCallOnSlowPath;
7510 break;
7511 }
7512
7513 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
7514 if (baker_read_barrier_slow_path) {
7515 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
7516 }
7517 locations->SetInAt(0, Location::RequiresRegister());
7518 locations->SetInAt(1, Location::RequiresRegister());
7519 // The "out" register is used as a temporary, so it overlaps with the inputs.
7520 // Note that TypeCheckSlowPathARM uses this register too.
7521 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Artem Serov657022c2016-11-23 14:19:38 +00007522 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01007523 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
7524 codegen_->MaybeAddBakerCcEntrypointTempForFields(locations);
7525 }
Artem Serovcfbe9132016-10-14 15:58:56 +01007526}
7527
7528void InstructionCodeGeneratorARMVIXL::VisitInstanceOf(HInstanceOf* instruction) {
7529 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
7530 LocationSummary* locations = instruction->GetLocations();
7531 Location obj_loc = locations->InAt(0);
7532 vixl32::Register obj = InputRegisterAt(instruction, 0);
7533 vixl32::Register cls = InputRegisterAt(instruction, 1);
7534 Location out_loc = locations->Out();
7535 vixl32::Register out = OutputRegister(instruction);
Artem Serov657022c2016-11-23 14:19:38 +00007536 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
7537 DCHECK_LE(num_temps, 1u);
7538 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Artem Serovcfbe9132016-10-14 15:58:56 +01007539 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
7540 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
7541 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
7542 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007543 vixl32::Label done;
7544 vixl32::Label* const final_label = codegen_->GetFinalLabel(instruction, &done);
Artem Serovcfbe9132016-10-14 15:58:56 +01007545 SlowPathCodeARMVIXL* slow_path = nullptr;
7546
7547 // Return 0 if `obj` is null.
7548 // avoid null check if we know obj is not null.
7549 if (instruction->MustDoNullCheck()) {
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007550 DCHECK(!out.Is(obj));
7551 __ Mov(out, 0);
7552 __ CompareAndBranchIfZero(obj, final_label, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007553 }
7554
Artem Serovcfbe9132016-10-14 15:58:56 +01007555 switch (type_check_kind) {
7556 case TypeCheckKind::kExactCheck: {
Mathieu Chartier6beced42016-11-15 15:51:31 -08007557 // /* HeapReference<Class> */ out = obj->klass_
7558 GenerateReferenceLoadTwoRegisters(instruction,
7559 out_loc,
7560 obj_loc,
7561 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00007562 maybe_temp_loc,
7563 kCompilerReadBarrierOption);
Artem Serovcfbe9132016-10-14 15:58:56 +01007564 // Classes must be equal for the instanceof to succeed.
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007565 __ Cmp(out, cls);
7566 // We speculatively set the result to false without changing the condition
7567 // flags, which allows us to avoid some branching later.
7568 __ Mov(LeaveFlags, out, 0);
7569
7570 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
7571 // we check that the output is in a low register, so that a 16-bit MOV
7572 // encoding can be used.
7573 if (out.IsLow()) {
7574 // We use the scope because of the IT block that follows.
7575 ExactAssemblyScope guard(GetVIXLAssembler(),
7576 2 * vixl32::k16BitT32InstructionSizeInBytes,
7577 CodeBufferCheckScope::kExactSize);
7578
7579 __ it(eq);
7580 __ mov(eq, out, 1);
7581 } else {
7582 __ B(ne, final_label, /* far_target */ false);
7583 __ Mov(out, 1);
7584 }
7585
Artem Serovcfbe9132016-10-14 15:58:56 +01007586 break;
7587 }
7588
7589 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier6beced42016-11-15 15:51:31 -08007590 // /* HeapReference<Class> */ out = obj->klass_
7591 GenerateReferenceLoadTwoRegisters(instruction,
7592 out_loc,
7593 obj_loc,
7594 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00007595 maybe_temp_loc,
7596 kCompilerReadBarrierOption);
Artem Serovcfbe9132016-10-14 15:58:56 +01007597 // If the class is abstract, we eagerly fetch the super class of the
7598 // object to avoid doing a comparison we know will fail.
7599 vixl32::Label loop;
7600 __ Bind(&loop);
7601 // /* HeapReference<Class> */ out = out->super_class_
Artem Serov657022c2016-11-23 14:19:38 +00007602 GenerateReferenceLoadOneRegister(instruction,
7603 out_loc,
7604 super_offset,
7605 maybe_temp_loc,
7606 kCompilerReadBarrierOption);
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007607 // If `out` is null, we use it for the result, and jump to the final label.
Anton Kirilov6f644202017-02-27 18:29:45 +00007608 __ CompareAndBranchIfZero(out, final_label, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007609 __ Cmp(out, cls);
Artem Serov517d9f62016-12-12 15:51:15 +00007610 __ B(ne, &loop, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007611 __ Mov(out, 1);
Artem Serovcfbe9132016-10-14 15:58:56 +01007612 break;
7613 }
7614
7615 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier6beced42016-11-15 15:51:31 -08007616 // /* HeapReference<Class> */ out = obj->klass_
7617 GenerateReferenceLoadTwoRegisters(instruction,
7618 out_loc,
7619 obj_loc,
7620 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00007621 maybe_temp_loc,
7622 kCompilerReadBarrierOption);
Artem Serovcfbe9132016-10-14 15:58:56 +01007623 // Walk over the class hierarchy to find a match.
7624 vixl32::Label loop, success;
7625 __ Bind(&loop);
7626 __ Cmp(out, cls);
Artem Serov517d9f62016-12-12 15:51:15 +00007627 __ B(eq, &success, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007628 // /* HeapReference<Class> */ out = out->super_class_
Artem Serov657022c2016-11-23 14:19:38 +00007629 GenerateReferenceLoadOneRegister(instruction,
7630 out_loc,
7631 super_offset,
7632 maybe_temp_loc,
7633 kCompilerReadBarrierOption);
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007634 // This is essentially a null check, but it sets the condition flags to the
7635 // proper value for the code that follows the loop, i.e. not `eq`.
7636 __ Cmp(out, 1);
7637 __ B(hs, &loop, /* far_target */ false);
7638
7639 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
7640 // we check that the output is in a low register, so that a 16-bit MOV
7641 // encoding can be used.
7642 if (out.IsLow()) {
7643 // If `out` is null, we use it for the result, and the condition flags
7644 // have already been set to `ne`, so the IT block that comes afterwards
7645 // (and which handles the successful case) turns into a NOP (instead of
7646 // overwriting `out`).
7647 __ Bind(&success);
7648
7649 // We use the scope because of the IT block that follows.
7650 ExactAssemblyScope guard(GetVIXLAssembler(),
7651 2 * vixl32::k16BitT32InstructionSizeInBytes,
7652 CodeBufferCheckScope::kExactSize);
7653
7654 // There is only one branch to the `success` label (which is bound to this
7655 // IT block), and it has the same condition, `eq`, so in that case the MOV
7656 // is executed.
7657 __ it(eq);
7658 __ mov(eq, out, 1);
7659 } else {
7660 // If `out` is null, we use it for the result, and jump to the final label.
Anton Kirilov6f644202017-02-27 18:29:45 +00007661 __ B(final_label);
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007662 __ Bind(&success);
7663 __ Mov(out, 1);
Artem Serovcfbe9132016-10-14 15:58:56 +01007664 }
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007665
Artem Serovcfbe9132016-10-14 15:58:56 +01007666 break;
7667 }
7668
7669 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier6beced42016-11-15 15:51:31 -08007670 // /* HeapReference<Class> */ out = obj->klass_
7671 GenerateReferenceLoadTwoRegisters(instruction,
7672 out_loc,
7673 obj_loc,
7674 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00007675 maybe_temp_loc,
7676 kCompilerReadBarrierOption);
Artem Serovcfbe9132016-10-14 15:58:56 +01007677 // Do an exact check.
7678 vixl32::Label exact_check;
7679 __ Cmp(out, cls);
Artem Serov517d9f62016-12-12 15:51:15 +00007680 __ B(eq, &exact_check, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007681 // Otherwise, we need to check that the object's class is a non-primitive array.
7682 // /* HeapReference<Class> */ out = out->component_type_
Artem Serov657022c2016-11-23 14:19:38 +00007683 GenerateReferenceLoadOneRegister(instruction,
7684 out_loc,
7685 component_offset,
7686 maybe_temp_loc,
7687 kCompilerReadBarrierOption);
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007688 // If `out` is null, we use it for the result, and jump to the final label.
Anton Kirilov6f644202017-02-27 18:29:45 +00007689 __ CompareAndBranchIfZero(out, final_label, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007690 GetAssembler()->LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
7691 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007692 __ Cmp(out, 0);
7693 // We speculatively set the result to false without changing the condition
7694 // flags, which allows us to avoid some branching later.
7695 __ Mov(LeaveFlags, out, 0);
7696
7697 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
7698 // we check that the output is in a low register, so that a 16-bit MOV
7699 // encoding can be used.
7700 if (out.IsLow()) {
7701 __ Bind(&exact_check);
7702
7703 // We use the scope because of the IT block that follows.
7704 ExactAssemblyScope guard(GetVIXLAssembler(),
7705 2 * vixl32::k16BitT32InstructionSizeInBytes,
7706 CodeBufferCheckScope::kExactSize);
7707
7708 __ it(eq);
7709 __ mov(eq, out, 1);
7710 } else {
7711 __ B(ne, final_label, /* far_target */ false);
7712 __ Bind(&exact_check);
7713 __ Mov(out, 1);
7714 }
7715
Artem Serovcfbe9132016-10-14 15:58:56 +01007716 break;
7717 }
7718
7719 case TypeCheckKind::kArrayCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00007720 // No read barrier since the slow path will retry upon failure.
Mathieu Chartier6beced42016-11-15 15:51:31 -08007721 // /* HeapReference<Class> */ out = obj->klass_
7722 GenerateReferenceLoadTwoRegisters(instruction,
7723 out_loc,
7724 obj_loc,
7725 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00007726 maybe_temp_loc,
7727 kWithoutReadBarrier);
Artem Serovcfbe9132016-10-14 15:58:56 +01007728 __ Cmp(out, cls);
7729 DCHECK(locations->OnlyCallsOnSlowPath());
7730 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARMVIXL(instruction,
7731 /* is_fatal */ false);
7732 codegen_->AddSlowPath(slow_path);
7733 __ B(ne, slow_path->GetEntryLabel());
7734 __ Mov(out, 1);
Artem Serovcfbe9132016-10-14 15:58:56 +01007735 break;
7736 }
7737
7738 case TypeCheckKind::kUnresolvedCheck:
7739 case TypeCheckKind::kInterfaceCheck: {
7740 // Note that we indeed only call on slow path, but we always go
7741 // into the slow path for the unresolved and interface check
7742 // cases.
7743 //
7744 // We cannot directly call the InstanceofNonTrivial runtime
7745 // entry point without resorting to a type checking slow path
7746 // here (i.e. by calling InvokeRuntime directly), as it would
7747 // require to assign fixed registers for the inputs of this
7748 // HInstanceOf instruction (following the runtime calling
7749 // convention), which might be cluttered by the potential first
7750 // read barrier emission at the beginning of this method.
7751 //
7752 // TODO: Introduce a new runtime entry point taking the object
7753 // to test (instead of its class) as argument, and let it deal
7754 // with the read barrier issues. This will let us refactor this
7755 // case of the `switch` code as it was previously (with a direct
7756 // call to the runtime not using a type checking slow path).
7757 // This should also be beneficial for the other cases above.
7758 DCHECK(locations->OnlyCallsOnSlowPath());
7759 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARMVIXL(instruction,
7760 /* is_fatal */ false);
7761 codegen_->AddSlowPath(slow_path);
7762 __ B(slow_path->GetEntryLabel());
Artem Serovcfbe9132016-10-14 15:58:56 +01007763 break;
7764 }
7765 }
7766
Artem Serovcfbe9132016-10-14 15:58:56 +01007767 if (done.IsReferenced()) {
7768 __ Bind(&done);
7769 }
7770
7771 if (slow_path != nullptr) {
7772 __ Bind(slow_path->GetExitLabel());
7773 }
7774}
7775
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007776void LocationsBuilderARMVIXL::VisitCheckCast(HCheckCast* instruction) {
7777 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
7778 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
7779
7780 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
7781 switch (type_check_kind) {
7782 case TypeCheckKind::kExactCheck:
7783 case TypeCheckKind::kAbstractClassCheck:
7784 case TypeCheckKind::kClassHierarchyCheck:
7785 case TypeCheckKind::kArrayObjectCheck:
7786 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
7787 LocationSummary::kCallOnSlowPath :
7788 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
7789 break;
7790 case TypeCheckKind::kArrayCheck:
7791 case TypeCheckKind::kUnresolvedCheck:
7792 case TypeCheckKind::kInterfaceCheck:
7793 call_kind = LocationSummary::kCallOnSlowPath;
7794 break;
7795 }
7796
7797 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
7798 locations->SetInAt(0, Location::RequiresRegister());
7799 locations->SetInAt(1, Location::RequiresRegister());
Artem Serov657022c2016-11-23 14:19:38 +00007800 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007801}
7802
7803void InstructionCodeGeneratorARMVIXL::VisitCheckCast(HCheckCast* instruction) {
7804 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
7805 LocationSummary* locations = instruction->GetLocations();
7806 Location obj_loc = locations->InAt(0);
7807 vixl32::Register obj = InputRegisterAt(instruction, 0);
7808 vixl32::Register cls = InputRegisterAt(instruction, 1);
7809 Location temp_loc = locations->GetTemp(0);
7810 vixl32::Register temp = RegisterFrom(temp_loc);
Artem Serov657022c2016-11-23 14:19:38 +00007811 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
7812 DCHECK_LE(num_temps, 3u);
7813 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
7814 Location maybe_temp3_loc = (num_temps >= 3) ? locations->GetTemp(2) : Location::NoLocation();
7815 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
7816 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
7817 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
7818 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
7819 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
7820 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
7821 const uint32_t object_array_data_offset =
7822 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007823
Artem Serov657022c2016-11-23 14:19:38 +00007824 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
7825 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
7826 // read barriers is done for performance and code size reasons.
7827 bool is_type_check_slow_path_fatal = false;
7828 if (!kEmitCompilerReadBarrier) {
7829 is_type_check_slow_path_fatal =
7830 (type_check_kind == TypeCheckKind::kExactCheck ||
7831 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
7832 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
7833 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
7834 !instruction->CanThrowIntoCatchBlock();
7835 }
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007836 SlowPathCodeARMVIXL* type_check_slow_path =
7837 new (GetGraph()->GetArena()) TypeCheckSlowPathARMVIXL(instruction,
7838 is_type_check_slow_path_fatal);
7839 codegen_->AddSlowPath(type_check_slow_path);
7840
7841 vixl32::Label done;
Anton Kirilov6f644202017-02-27 18:29:45 +00007842 vixl32::Label* final_label = codegen_->GetFinalLabel(instruction, &done);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007843 // Avoid null check if we know obj is not null.
7844 if (instruction->MustDoNullCheck()) {
Anton Kirilov6f644202017-02-27 18:29:45 +00007845 __ CompareAndBranchIfZero(obj, final_label, /* far_target */ false);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007846 }
7847
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007848 switch (type_check_kind) {
7849 case TypeCheckKind::kExactCheck:
7850 case TypeCheckKind::kArrayCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00007851 // /* HeapReference<Class> */ temp = obj->klass_
7852 GenerateReferenceLoadTwoRegisters(instruction,
7853 temp_loc,
7854 obj_loc,
7855 class_offset,
7856 maybe_temp2_loc,
7857 kWithoutReadBarrier);
7858
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007859 __ Cmp(temp, cls);
7860 // Jump to slow path for throwing the exception or doing a
7861 // more involved array check.
7862 __ B(ne, type_check_slow_path->GetEntryLabel());
7863 break;
7864 }
7865
7866 case TypeCheckKind::kAbstractClassCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00007867 // /* HeapReference<Class> */ temp = obj->klass_
7868 GenerateReferenceLoadTwoRegisters(instruction,
7869 temp_loc,
7870 obj_loc,
7871 class_offset,
7872 maybe_temp2_loc,
7873 kWithoutReadBarrier);
7874
Artem Serovcfbe9132016-10-14 15:58:56 +01007875 // If the class is abstract, we eagerly fetch the super class of the
7876 // object to avoid doing a comparison we know will fail.
7877 vixl32::Label loop;
7878 __ Bind(&loop);
7879 // /* HeapReference<Class> */ temp = temp->super_class_
Artem Serov657022c2016-11-23 14:19:38 +00007880 GenerateReferenceLoadOneRegister(instruction,
7881 temp_loc,
7882 super_offset,
7883 maybe_temp2_loc,
7884 kWithoutReadBarrier);
Artem Serovcfbe9132016-10-14 15:58:56 +01007885
7886 // If the class reference currently in `temp` is null, jump to the slow path to throw the
7887 // exception.
xueliang.zhongf51bc622016-11-04 09:23:32 +00007888 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
Artem Serovcfbe9132016-10-14 15:58:56 +01007889
7890 // Otherwise, compare the classes.
7891 __ Cmp(temp, cls);
Artem Serov517d9f62016-12-12 15:51:15 +00007892 __ B(ne, &loop, /* far_target */ false);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007893 break;
7894 }
7895
7896 case TypeCheckKind::kClassHierarchyCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00007897 // /* HeapReference<Class> */ temp = obj->klass_
7898 GenerateReferenceLoadTwoRegisters(instruction,
7899 temp_loc,
7900 obj_loc,
7901 class_offset,
7902 maybe_temp2_loc,
7903 kWithoutReadBarrier);
7904
Artem Serovcfbe9132016-10-14 15:58:56 +01007905 // Walk over the class hierarchy to find a match.
7906 vixl32::Label loop;
7907 __ Bind(&loop);
7908 __ Cmp(temp, cls);
Anton Kirilov6f644202017-02-27 18:29:45 +00007909 __ B(eq, final_label, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007910
7911 // /* HeapReference<Class> */ temp = temp->super_class_
Artem Serov657022c2016-11-23 14:19:38 +00007912 GenerateReferenceLoadOneRegister(instruction,
7913 temp_loc,
7914 super_offset,
7915 maybe_temp2_loc,
7916 kWithoutReadBarrier);
Artem Serovcfbe9132016-10-14 15:58:56 +01007917
7918 // If the class reference currently in `temp` is null, jump to the slow path to throw the
7919 // exception.
xueliang.zhongf51bc622016-11-04 09:23:32 +00007920 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
Artem Serovcfbe9132016-10-14 15:58:56 +01007921 // Otherwise, jump to the beginning of the loop.
7922 __ B(&loop);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007923 break;
7924 }
7925
Artem Serovcfbe9132016-10-14 15:58:56 +01007926 case TypeCheckKind::kArrayObjectCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00007927 // /* HeapReference<Class> */ temp = obj->klass_
7928 GenerateReferenceLoadTwoRegisters(instruction,
7929 temp_loc,
7930 obj_loc,
7931 class_offset,
7932 maybe_temp2_loc,
7933 kWithoutReadBarrier);
7934
Artem Serovcfbe9132016-10-14 15:58:56 +01007935 // Do an exact check.
7936 __ Cmp(temp, cls);
Anton Kirilov6f644202017-02-27 18:29:45 +00007937 __ B(eq, final_label, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007938
7939 // Otherwise, we need to check that the object's class is a non-primitive array.
7940 // /* HeapReference<Class> */ temp = temp->component_type_
Artem Serov657022c2016-11-23 14:19:38 +00007941 GenerateReferenceLoadOneRegister(instruction,
7942 temp_loc,
7943 component_offset,
7944 maybe_temp2_loc,
7945 kWithoutReadBarrier);
Artem Serovcfbe9132016-10-14 15:58:56 +01007946 // If the component type is null, jump to the slow path to throw the exception.
xueliang.zhongf51bc622016-11-04 09:23:32 +00007947 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
Artem Serovcfbe9132016-10-14 15:58:56 +01007948 // Otherwise,the object is indeed an array, jump to label `check_non_primitive_component_type`
7949 // to further check that this component type is not a primitive type.
7950 GetAssembler()->LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
7951 static_assert(Primitive::kPrimNot == 0, "Expected 0 for art::Primitive::kPrimNot");
xueliang.zhongf51bc622016-11-04 09:23:32 +00007952 __ CompareAndBranchIfNonZero(temp, type_check_slow_path->GetEntryLabel());
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007953 break;
7954 }
7955
7956 case TypeCheckKind::kUnresolvedCheck:
Artem Serov657022c2016-11-23 14:19:38 +00007957 // We always go into the type check slow path for the unresolved check case.
Artem Serovcfbe9132016-10-14 15:58:56 +01007958 // We cannot directly call the CheckCast runtime entry point
7959 // without resorting to a type checking slow path here (i.e. by
7960 // calling InvokeRuntime directly), as it would require to
7961 // assign fixed registers for the inputs of this HInstanceOf
7962 // instruction (following the runtime calling convention), which
7963 // might be cluttered by the potential first read barrier
7964 // emission at the beginning of this method.
Artem Serov657022c2016-11-23 14:19:38 +00007965
Artem Serovcfbe9132016-10-14 15:58:56 +01007966 __ B(type_check_slow_path->GetEntryLabel());
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007967 break;
Artem Serov657022c2016-11-23 14:19:38 +00007968
7969 case TypeCheckKind::kInterfaceCheck: {
7970 // Avoid read barriers to improve performance of the fast path. We can not get false
7971 // positives by doing this.
7972 // /* HeapReference<Class> */ temp = obj->klass_
7973 GenerateReferenceLoadTwoRegisters(instruction,
7974 temp_loc,
7975 obj_loc,
7976 class_offset,
7977 maybe_temp2_loc,
7978 kWithoutReadBarrier);
7979
7980 // /* HeapReference<Class> */ temp = temp->iftable_
7981 GenerateReferenceLoadTwoRegisters(instruction,
7982 temp_loc,
7983 temp_loc,
7984 iftable_offset,
7985 maybe_temp2_loc,
7986 kWithoutReadBarrier);
7987 // Iftable is never null.
7988 __ Ldr(RegisterFrom(maybe_temp2_loc), MemOperand(temp, array_length_offset));
7989 // Loop through the iftable and check if any class matches.
7990 vixl32::Label start_loop;
7991 __ Bind(&start_loop);
7992 __ CompareAndBranchIfZero(RegisterFrom(maybe_temp2_loc),
7993 type_check_slow_path->GetEntryLabel());
7994 __ Ldr(RegisterFrom(maybe_temp3_loc), MemOperand(temp, object_array_data_offset));
7995 GetAssembler()->MaybeUnpoisonHeapReference(RegisterFrom(maybe_temp3_loc));
7996 // Go to next interface.
7997 __ Add(temp, temp, Operand::From(2 * kHeapReferenceSize));
7998 __ Sub(RegisterFrom(maybe_temp2_loc), RegisterFrom(maybe_temp2_loc), 2);
7999 // Compare the classes and continue the loop if they do not match.
8000 __ Cmp(cls, RegisterFrom(maybe_temp3_loc));
Artem Serov517d9f62016-12-12 15:51:15 +00008001 __ B(ne, &start_loop, /* far_target */ false);
Artem Serov657022c2016-11-23 14:19:38 +00008002 break;
8003 }
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008004 }
Anton Kirilov6f644202017-02-27 18:29:45 +00008005 if (done.IsReferenced()) {
8006 __ Bind(&done);
8007 }
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008008
8009 __ Bind(type_check_slow_path->GetExitLabel());
8010}
8011
Artem Serov551b28f2016-10-18 19:11:30 +01008012void LocationsBuilderARMVIXL::VisitMonitorOperation(HMonitorOperation* instruction) {
8013 LocationSummary* locations =
8014 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
8015 InvokeRuntimeCallingConventionARMVIXL calling_convention;
8016 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
8017}
8018
8019void InstructionCodeGeneratorARMVIXL::VisitMonitorOperation(HMonitorOperation* instruction) {
8020 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
8021 instruction,
8022 instruction->GetDexPc());
8023 if (instruction->IsEnter()) {
8024 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
8025 } else {
8026 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
8027 }
Roland Levillain5daa4952017-07-03 17:23:56 +01008028 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 17);
Artem Serov551b28f2016-10-18 19:11:30 +01008029}
8030
Artem Serov02109dd2016-09-23 17:17:54 +01008031void LocationsBuilderARMVIXL::VisitAnd(HAnd* instruction) {
8032 HandleBitwiseOperation(instruction, AND);
8033}
8034
8035void LocationsBuilderARMVIXL::VisitOr(HOr* instruction) {
8036 HandleBitwiseOperation(instruction, ORR);
8037}
8038
8039void LocationsBuilderARMVIXL::VisitXor(HXor* instruction) {
8040 HandleBitwiseOperation(instruction, EOR);
8041}
8042
8043void LocationsBuilderARMVIXL::HandleBitwiseOperation(HBinaryOperation* instruction, Opcode opcode) {
8044 LocationSummary* locations =
8045 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
8046 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
8047 || instruction->GetResultType() == Primitive::kPrimLong);
8048 // Note: GVN reorders commutative operations to have the constant on the right hand side.
8049 locations->SetInAt(0, Location::RequiresRegister());
8050 locations->SetInAt(1, ArmEncodableConstantOrRegister(instruction->InputAt(1), opcode));
8051 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8052}
8053
8054void InstructionCodeGeneratorARMVIXL::VisitAnd(HAnd* instruction) {
8055 HandleBitwiseOperation(instruction);
8056}
8057
8058void InstructionCodeGeneratorARMVIXL::VisitOr(HOr* instruction) {
8059 HandleBitwiseOperation(instruction);
8060}
8061
8062void InstructionCodeGeneratorARMVIXL::VisitXor(HXor* instruction) {
8063 HandleBitwiseOperation(instruction);
8064}
8065
Artem Serov2bbc9532016-10-21 11:51:50 +01008066void LocationsBuilderARMVIXL::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
8067 LocationSummary* locations =
8068 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
8069 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
8070 || instruction->GetResultType() == Primitive::kPrimLong);
8071
8072 locations->SetInAt(0, Location::RequiresRegister());
8073 locations->SetInAt(1, Location::RequiresRegister());
8074 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8075}
8076
8077void InstructionCodeGeneratorARMVIXL::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
8078 LocationSummary* locations = instruction->GetLocations();
8079 Location first = locations->InAt(0);
8080 Location second = locations->InAt(1);
8081 Location out = locations->Out();
8082
8083 if (instruction->GetResultType() == Primitive::kPrimInt) {
8084 vixl32::Register first_reg = RegisterFrom(first);
8085 vixl32::Register second_reg = RegisterFrom(second);
8086 vixl32::Register out_reg = RegisterFrom(out);
8087
8088 switch (instruction->GetOpKind()) {
8089 case HInstruction::kAnd:
8090 __ Bic(out_reg, first_reg, second_reg);
8091 break;
8092 case HInstruction::kOr:
8093 __ Orn(out_reg, first_reg, second_reg);
8094 break;
8095 // There is no EON on arm.
8096 case HInstruction::kXor:
8097 default:
8098 LOG(FATAL) << "Unexpected instruction " << instruction->DebugName();
8099 UNREACHABLE();
8100 }
8101 return;
8102
8103 } else {
8104 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
8105 vixl32::Register first_low = LowRegisterFrom(first);
8106 vixl32::Register first_high = HighRegisterFrom(first);
8107 vixl32::Register second_low = LowRegisterFrom(second);
8108 vixl32::Register second_high = HighRegisterFrom(second);
8109 vixl32::Register out_low = LowRegisterFrom(out);
8110 vixl32::Register out_high = HighRegisterFrom(out);
8111
8112 switch (instruction->GetOpKind()) {
8113 case HInstruction::kAnd:
8114 __ Bic(out_low, first_low, second_low);
8115 __ Bic(out_high, first_high, second_high);
8116 break;
8117 case HInstruction::kOr:
8118 __ Orn(out_low, first_low, second_low);
8119 __ Orn(out_high, first_high, second_high);
8120 break;
8121 // There is no EON on arm.
8122 case HInstruction::kXor:
8123 default:
8124 LOG(FATAL) << "Unexpected instruction " << instruction->DebugName();
8125 UNREACHABLE();
8126 }
8127 }
8128}
8129
Anton Kirilov74234da2017-01-13 14:42:47 +00008130void LocationsBuilderARMVIXL::VisitDataProcWithShifterOp(
8131 HDataProcWithShifterOp* instruction) {
8132 DCHECK(instruction->GetType() == Primitive::kPrimInt ||
8133 instruction->GetType() == Primitive::kPrimLong);
8134 LocationSummary* locations =
8135 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
8136 const bool overlap = instruction->GetType() == Primitive::kPrimLong &&
8137 HDataProcWithShifterOp::IsExtensionOp(instruction->GetOpKind());
8138
8139 locations->SetInAt(0, Location::RequiresRegister());
8140 locations->SetInAt(1, Location::RequiresRegister());
8141 locations->SetOut(Location::RequiresRegister(),
8142 overlap ? Location::kOutputOverlap : Location::kNoOutputOverlap);
8143}
8144
8145void InstructionCodeGeneratorARMVIXL::VisitDataProcWithShifterOp(
8146 HDataProcWithShifterOp* instruction) {
8147 const LocationSummary* const locations = instruction->GetLocations();
8148 const HInstruction::InstructionKind kind = instruction->GetInstrKind();
8149 const HDataProcWithShifterOp::OpKind op_kind = instruction->GetOpKind();
8150
8151 if (instruction->GetType() == Primitive::kPrimInt) {
Anton Kirilov420ee302017-02-21 18:10:26 +00008152 const vixl32::Register first = InputRegisterAt(instruction, 0);
8153 const vixl32::Register output = OutputRegister(instruction);
Anton Kirilov74234da2017-01-13 14:42:47 +00008154 const vixl32::Register second = instruction->InputAt(1)->GetType() == Primitive::kPrimLong
8155 ? LowRegisterFrom(locations->InAt(1))
8156 : InputRegisterAt(instruction, 1);
8157
Anton Kirilov420ee302017-02-21 18:10:26 +00008158 if (HDataProcWithShifterOp::IsExtensionOp(op_kind)) {
8159 DCHECK_EQ(kind, HInstruction::kAdd);
8160
8161 switch (op_kind) {
8162 case HDataProcWithShifterOp::kUXTB:
8163 __ Uxtab(output, first, second);
8164 break;
8165 case HDataProcWithShifterOp::kUXTH:
8166 __ Uxtah(output, first, second);
8167 break;
8168 case HDataProcWithShifterOp::kSXTB:
8169 __ Sxtab(output, first, second);
8170 break;
8171 case HDataProcWithShifterOp::kSXTH:
8172 __ Sxtah(output, first, second);
8173 break;
8174 default:
8175 LOG(FATAL) << "Unexpected operation kind: " << op_kind;
8176 UNREACHABLE();
8177 }
8178 } else {
8179 GenerateDataProcInstruction(kind,
8180 output,
8181 first,
8182 Operand(second,
8183 ShiftFromOpKind(op_kind),
8184 instruction->GetShiftAmount()),
8185 codegen_);
8186 }
Anton Kirilov74234da2017-01-13 14:42:47 +00008187 } else {
8188 DCHECK_EQ(instruction->GetType(), Primitive::kPrimLong);
8189
8190 if (HDataProcWithShifterOp::IsExtensionOp(op_kind)) {
8191 const vixl32::Register second = InputRegisterAt(instruction, 1);
8192
8193 DCHECK(!LowRegisterFrom(locations->Out()).Is(second));
8194 GenerateDataProc(kind,
8195 locations->Out(),
8196 locations->InAt(0),
8197 second,
8198 Operand(second, ShiftType::ASR, 31),
8199 codegen_);
8200 } else {
8201 GenerateLongDataProc(instruction, codegen_);
8202 }
8203 }
8204}
8205
Artem Serov02109dd2016-09-23 17:17:54 +01008206// TODO(VIXL): Remove optimizations in the helper when they are implemented in vixl.
8207void InstructionCodeGeneratorARMVIXL::GenerateAndConst(vixl32::Register out,
8208 vixl32::Register first,
8209 uint32_t value) {
8210 // Optimize special cases for individual halfs of `and-long` (`and` is simplified earlier).
8211 if (value == 0xffffffffu) {
8212 if (!out.Is(first)) {
8213 __ Mov(out, first);
8214 }
8215 return;
8216 }
8217 if (value == 0u) {
8218 __ Mov(out, 0);
8219 return;
8220 }
8221 if (GetAssembler()->ShifterOperandCanHold(AND, value)) {
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00008222 __ And(out, first, value);
8223 } else if (GetAssembler()->ShifterOperandCanHold(BIC, ~value)) {
8224 __ Bic(out, first, ~value);
Artem Serov02109dd2016-09-23 17:17:54 +01008225 } else {
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00008226 DCHECK(IsPowerOfTwo(value + 1));
8227 __ Ubfx(out, first, 0, WhichPowerOf2(value + 1));
Artem Serov02109dd2016-09-23 17:17:54 +01008228 }
8229}
8230
8231// TODO(VIXL): Remove optimizations in the helper when they are implemented in vixl.
8232void InstructionCodeGeneratorARMVIXL::GenerateOrrConst(vixl32::Register out,
8233 vixl32::Register first,
8234 uint32_t value) {
8235 // Optimize special cases for individual halfs of `or-long` (`or` is simplified earlier).
8236 if (value == 0u) {
8237 if (!out.Is(first)) {
8238 __ Mov(out, first);
8239 }
8240 return;
8241 }
8242 if (value == 0xffffffffu) {
8243 __ Mvn(out, 0);
8244 return;
8245 }
8246 if (GetAssembler()->ShifterOperandCanHold(ORR, value)) {
8247 __ Orr(out, first, value);
8248 } else {
8249 DCHECK(GetAssembler()->ShifterOperandCanHold(ORN, ~value));
8250 __ Orn(out, first, ~value);
8251 }
8252}
8253
8254// TODO(VIXL): Remove optimizations in the helper when they are implemented in vixl.
8255void InstructionCodeGeneratorARMVIXL::GenerateEorConst(vixl32::Register out,
8256 vixl32::Register first,
8257 uint32_t value) {
8258 // Optimize special case for individual halfs of `xor-long` (`xor` is simplified earlier).
8259 if (value == 0u) {
8260 if (!out.Is(first)) {
8261 __ Mov(out, first);
8262 }
8263 return;
8264 }
8265 __ Eor(out, first, value);
8266}
8267
Anton Kirilovdda43962016-11-21 19:55:20 +00008268void InstructionCodeGeneratorARMVIXL::GenerateAddLongConst(Location out,
8269 Location first,
8270 uint64_t value) {
8271 vixl32::Register out_low = LowRegisterFrom(out);
8272 vixl32::Register out_high = HighRegisterFrom(out);
8273 vixl32::Register first_low = LowRegisterFrom(first);
8274 vixl32::Register first_high = HighRegisterFrom(first);
8275 uint32_t value_low = Low32Bits(value);
8276 uint32_t value_high = High32Bits(value);
8277 if (value_low == 0u) {
8278 if (!out_low.Is(first_low)) {
8279 __ Mov(out_low, first_low);
8280 }
8281 __ Add(out_high, first_high, value_high);
8282 return;
8283 }
8284 __ Adds(out_low, first_low, value_low);
Scott Wakelingbffdc702016-12-07 17:46:03 +00008285 if (GetAssembler()->ShifterOperandCanHold(ADC, value_high, kCcDontCare)) {
Anton Kirilovdda43962016-11-21 19:55:20 +00008286 __ Adc(out_high, first_high, value_high);
Scott Wakelingbffdc702016-12-07 17:46:03 +00008287 } else if (GetAssembler()->ShifterOperandCanHold(SBC, ~value_high, kCcDontCare)) {
Anton Kirilovdda43962016-11-21 19:55:20 +00008288 __ Sbc(out_high, first_high, ~value_high);
8289 } else {
8290 LOG(FATAL) << "Unexpected constant " << value_high;
8291 UNREACHABLE();
8292 }
8293}
8294
Artem Serov02109dd2016-09-23 17:17:54 +01008295void InstructionCodeGeneratorARMVIXL::HandleBitwiseOperation(HBinaryOperation* instruction) {
8296 LocationSummary* locations = instruction->GetLocations();
8297 Location first = locations->InAt(0);
8298 Location second = locations->InAt(1);
8299 Location out = locations->Out();
8300
8301 if (second.IsConstant()) {
8302 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
8303 uint32_t value_low = Low32Bits(value);
8304 if (instruction->GetResultType() == Primitive::kPrimInt) {
8305 vixl32::Register first_reg = InputRegisterAt(instruction, 0);
8306 vixl32::Register out_reg = OutputRegister(instruction);
8307 if (instruction->IsAnd()) {
8308 GenerateAndConst(out_reg, first_reg, value_low);
8309 } else if (instruction->IsOr()) {
8310 GenerateOrrConst(out_reg, first_reg, value_low);
8311 } else {
8312 DCHECK(instruction->IsXor());
8313 GenerateEorConst(out_reg, first_reg, value_low);
8314 }
8315 } else {
8316 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
8317 uint32_t value_high = High32Bits(value);
8318 vixl32::Register first_low = LowRegisterFrom(first);
8319 vixl32::Register first_high = HighRegisterFrom(first);
8320 vixl32::Register out_low = LowRegisterFrom(out);
8321 vixl32::Register out_high = HighRegisterFrom(out);
8322 if (instruction->IsAnd()) {
8323 GenerateAndConst(out_low, first_low, value_low);
8324 GenerateAndConst(out_high, first_high, value_high);
8325 } else if (instruction->IsOr()) {
8326 GenerateOrrConst(out_low, first_low, value_low);
8327 GenerateOrrConst(out_high, first_high, value_high);
8328 } else {
8329 DCHECK(instruction->IsXor());
8330 GenerateEorConst(out_low, first_low, value_low);
8331 GenerateEorConst(out_high, first_high, value_high);
8332 }
8333 }
8334 return;
8335 }
8336
8337 if (instruction->GetResultType() == Primitive::kPrimInt) {
8338 vixl32::Register first_reg = InputRegisterAt(instruction, 0);
8339 vixl32::Register second_reg = InputRegisterAt(instruction, 1);
8340 vixl32::Register out_reg = OutputRegister(instruction);
8341 if (instruction->IsAnd()) {
8342 __ And(out_reg, first_reg, second_reg);
8343 } else if (instruction->IsOr()) {
8344 __ Orr(out_reg, first_reg, second_reg);
8345 } else {
8346 DCHECK(instruction->IsXor());
8347 __ Eor(out_reg, first_reg, second_reg);
8348 }
8349 } else {
8350 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
8351 vixl32::Register first_low = LowRegisterFrom(first);
8352 vixl32::Register first_high = HighRegisterFrom(first);
8353 vixl32::Register second_low = LowRegisterFrom(second);
8354 vixl32::Register second_high = HighRegisterFrom(second);
8355 vixl32::Register out_low = LowRegisterFrom(out);
8356 vixl32::Register out_high = HighRegisterFrom(out);
8357 if (instruction->IsAnd()) {
8358 __ And(out_low, first_low, second_low);
8359 __ And(out_high, first_high, second_high);
8360 } else if (instruction->IsOr()) {
8361 __ Orr(out_low, first_low, second_low);
8362 __ Orr(out_high, first_high, second_high);
8363 } else {
8364 DCHECK(instruction->IsXor());
8365 __ Eor(out_low, first_low, second_low);
8366 __ Eor(out_high, first_high, second_high);
8367 }
8368 }
8369}
8370
Artem Serovcfbe9132016-10-14 15:58:56 +01008371void InstructionCodeGeneratorARMVIXL::GenerateReferenceLoadOneRegister(
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008372 HInstruction* instruction,
Artem Serovcfbe9132016-10-14 15:58:56 +01008373 Location out,
8374 uint32_t offset,
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008375 Location maybe_temp,
8376 ReadBarrierOption read_barrier_option) {
Artem Serovcfbe9132016-10-14 15:58:56 +01008377 vixl32::Register out_reg = RegisterFrom(out);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008378 if (read_barrier_option == kWithReadBarrier) {
8379 CHECK(kEmitCompilerReadBarrier);
8380 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
8381 if (kUseBakerReadBarrier) {
8382 // Load with fast path based Baker's read barrier.
8383 // /* HeapReference<Object> */ out = *(out + offset)
8384 codegen_->GenerateFieldLoadWithBakerReadBarrier(
8385 instruction, out, out_reg, offset, maybe_temp, /* needs_null_check */ false);
8386 } else {
8387 // Load with slow path based read barrier.
8388 // Save the value of `out` into `maybe_temp` before overwriting it
8389 // in the following move operation, as we will need it for the
8390 // read barrier below.
8391 __ Mov(RegisterFrom(maybe_temp), out_reg);
8392 // /* HeapReference<Object> */ out = *(out + offset)
8393 GetAssembler()->LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
8394 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
8395 }
Artem Serovcfbe9132016-10-14 15:58:56 +01008396 } else {
8397 // Plain load with no read barrier.
8398 // /* HeapReference<Object> */ out = *(out + offset)
8399 GetAssembler()->LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
8400 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
8401 }
8402}
8403
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008404void InstructionCodeGeneratorARMVIXL::GenerateReferenceLoadTwoRegisters(
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008405 HInstruction* instruction,
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008406 Location out,
8407 Location obj,
8408 uint32_t offset,
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008409 Location maybe_temp,
8410 ReadBarrierOption read_barrier_option) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008411 vixl32::Register out_reg = RegisterFrom(out);
8412 vixl32::Register obj_reg = RegisterFrom(obj);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008413 if (read_barrier_option == kWithReadBarrier) {
8414 CHECK(kEmitCompilerReadBarrier);
8415 if (kUseBakerReadBarrier) {
8416 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
8417 // Load with fast path based Baker's read barrier.
8418 // /* HeapReference<Object> */ out = *(obj + offset)
8419 codegen_->GenerateFieldLoadWithBakerReadBarrier(
8420 instruction, out, obj_reg, offset, maybe_temp, /* needs_null_check */ false);
8421 } else {
8422 // Load with slow path based read barrier.
8423 // /* HeapReference<Object> */ out = *(obj + offset)
8424 GetAssembler()->LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
8425 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
8426 }
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008427 } else {
8428 // Plain load with no read barrier.
8429 // /* HeapReference<Object> */ out = *(obj + offset)
8430 GetAssembler()->LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
8431 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
8432 }
8433}
8434
Scott Wakelinga7812ae2016-10-17 10:03:36 +01008435void InstructionCodeGeneratorARMVIXL::GenerateGcRootFieldLoad(
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008436 HInstruction* instruction,
Scott Wakelinga7812ae2016-10-17 10:03:36 +01008437 Location root,
8438 vixl32::Register obj,
8439 uint32_t offset,
Artem Serovd4cc5b22016-11-04 11:19:09 +00008440 ReadBarrierOption read_barrier_option) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01008441 vixl32::Register root_reg = RegisterFrom(root);
Artem Serovd4cc5b22016-11-04 11:19:09 +00008442 if (read_barrier_option == kWithReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008443 DCHECK(kEmitCompilerReadBarrier);
8444 if (kUseBakerReadBarrier) {
8445 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
Roland Levillainba650a42017-03-06 13:52:32 +00008446 // Baker's read barrier are used.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008447 if (kBakerReadBarrierLinkTimeThunksEnableForGcRoots &&
8448 !Runtime::Current()->UseJitCompilation()) {
Roland Levillain6d729a72017-06-30 18:34:01 +01008449 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in
8450 // the Marking Register) to decide whether we need to enter
8451 // the slow path to mark the GC root.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008452 //
8453 // We use link-time generated thunks for the slow path. That thunk
8454 // checks the reference and jumps to the entrypoint if needed.
8455 //
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008456 // lr = &return_address;
8457 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
Roland Levillain6d729a72017-06-30 18:34:01 +01008458 // if (mr) { // Thread::Current()->GetIsGcMarking()
8459 // goto gc_root_thunk<root_reg>(lr)
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008460 // }
8461 // return_address:
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008462
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008463 UseScratchRegisterScope temps(GetVIXLAssembler());
8464 ExcludeIPAndBakerCcEntrypointRegister(&temps, instruction);
Vladimir Marko88abba22017-05-03 17:09:25 +01008465 bool narrow = CanEmitNarrowLdr(root_reg, obj, offset);
8466 uint32_t custom_data = linker::Thumb2RelativePatcher::EncodeBakerReadBarrierGcRootData(
8467 root_reg.GetCode(), narrow);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008468 vixl32::Label* bne_label = codegen_->NewBakerReadBarrierPatch(custom_data);
Roland Levillainba650a42017-03-06 13:52:32 +00008469
Roland Levillain6d729a72017-06-30 18:34:01 +01008470 vixl::EmissionCheckScope guard(GetVIXLAssembler(), 4 * vixl32::kMaxInstructionSizeInBytes);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008471 vixl32::Label return_address;
8472 EmitAdrCode adr(GetVIXLAssembler(), lr, &return_address);
Roland Levillain6d729a72017-06-30 18:34:01 +01008473 __ cmp(mr, Operand(0));
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008474 // Currently the offset is always within range. If that changes,
8475 // we shall have to split the load the same way as for fields.
8476 DCHECK_LT(offset, kReferenceLoadMinFarOffset);
Vladimir Marko88abba22017-05-03 17:09:25 +01008477 ptrdiff_t old_offset = GetVIXLAssembler()->GetBuffer()->GetCursorOffset();
8478 __ ldr(EncodingSize(narrow ? Narrow : Wide), root_reg, MemOperand(obj, offset));
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008479 EmitPlaceholderBne(codegen_, bne_label);
8480 __ Bind(&return_address);
Vladimir Marko88abba22017-05-03 17:09:25 +01008481 DCHECK_EQ(old_offset - GetVIXLAssembler()->GetBuffer()->GetCursorOffset(),
8482 narrow ? BAKER_MARK_INTROSPECTION_GC_ROOT_LDR_NARROW_OFFSET
8483 : BAKER_MARK_INTROSPECTION_GC_ROOT_LDR_WIDE_OFFSET);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008484 } else {
Roland Levillain6d729a72017-06-30 18:34:01 +01008485 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in
8486 // the Marking Register) to decide whether we need to enter
8487 // the slow path to mark the GC root.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008488 //
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008489 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
Roland Levillain6d729a72017-06-30 18:34:01 +01008490 // if (mr) { // Thread::Current()->GetIsGcMarking()
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008491 // // Slow path.
Roland Levillain6d729a72017-06-30 18:34:01 +01008492 // entrypoint = Thread::Current()->pReadBarrierMarkReg ## root.reg()
8493 // root = entrypoint(root); // root = ReadBarrier::Mark(root); // Entry point call.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008494 // }
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008495
Roland Levillain6d729a72017-06-30 18:34:01 +01008496 // Slow path marking the GC root `root`. The entrypoint will
8497 // be loaded by the slow path code.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008498 SlowPathCodeARMVIXL* slow_path =
Roland Levillain6d729a72017-06-30 18:34:01 +01008499 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARMVIXL(instruction, root);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008500 codegen_->AddSlowPath(slow_path);
8501
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008502 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
8503 GetAssembler()->LoadFromOffset(kLoadWord, root_reg, obj, offset);
8504 static_assert(
8505 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
8506 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
8507 "have different sizes.");
8508 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
8509 "art::mirror::CompressedReference<mirror::Object> and int32_t "
8510 "have different sizes.");
8511
Roland Levillain6d729a72017-06-30 18:34:01 +01008512 __ CompareAndBranchIfNonZero(mr, slow_path->GetEntryLabel());
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008513 __ Bind(slow_path->GetExitLabel());
8514 }
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008515 } else {
8516 // GC root loaded through a slow path for read barriers other
8517 // than Baker's.
8518 // /* GcRoot<mirror::Object>* */ root = obj + offset
8519 __ Add(root_reg, obj, offset);
8520 // /* mirror::Object* */ root = root->Read()
8521 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
8522 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01008523 } else {
8524 // Plain GC root load with no read barrier.
8525 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
8526 GetAssembler()->LoadFromOffset(kLoadWord, root_reg, obj, offset);
8527 // Note that GC roots are not affected by heap poisoning, thus we
8528 // do not have to unpoison `root_reg` here.
8529 }
Roland Levillain5daa4952017-07-03 17:23:56 +01008530 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 18);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01008531}
8532
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008533void CodeGeneratorARMVIXL::MaybeAddBakerCcEntrypointTempForFields(LocationSummary* locations) {
8534 DCHECK(kEmitCompilerReadBarrier);
8535 DCHECK(kUseBakerReadBarrier);
8536 if (kBakerReadBarrierLinkTimeThunksEnableForFields) {
8537 if (!Runtime::Current()->UseJitCompilation()) {
8538 locations->AddTemp(Location::RegisterLocation(kBakerCcEntrypointRegister.GetCode()));
8539 }
8540 }
8541}
8542
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008543void CodeGeneratorARMVIXL::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
8544 Location ref,
8545 vixl32::Register obj,
8546 uint32_t offset,
8547 Location temp,
8548 bool needs_null_check) {
8549 DCHECK(kEmitCompilerReadBarrier);
8550 DCHECK(kUseBakerReadBarrier);
8551
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008552 if (kBakerReadBarrierLinkTimeThunksEnableForFields &&
8553 !Runtime::Current()->UseJitCompilation()) {
Roland Levillain6d729a72017-06-30 18:34:01 +01008554 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in the
8555 // Marking Register) to decide whether we need to enter the slow
8556 // path to mark the reference. Then, in the slow path, check the
8557 // gray bit in the lock word of the reference's holder (`obj`) to
8558 // decide whether to mark `ref` or not.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008559 //
8560 // We use link-time generated thunks for the slow path. That thunk checks
8561 // the holder and jumps to the entrypoint if needed. If the holder is not
8562 // gray, it creates a fake dependency and returns to the LDR instruction.
8563 //
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008564 // lr = &gray_return_address;
Roland Levillain6d729a72017-06-30 18:34:01 +01008565 // if (mr) { // Thread::Current()->GetIsGcMarking()
8566 // goto field_thunk<holder_reg, base_reg>(lr)
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008567 // }
8568 // not_gray_return_address:
8569 // // Original reference load. If the offset is too large to fit
8570 // // into LDR, we use an adjusted base register here.
Vladimir Marko88abba22017-05-03 17:09:25 +01008571 // HeapReference<mirror::Object> reference = *(obj+offset);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008572 // gray_return_address:
8573
8574 DCHECK_ALIGNED(offset, sizeof(mirror::HeapReference<mirror::Object>));
Vladimir Marko88abba22017-05-03 17:09:25 +01008575 vixl32::Register ref_reg = RegisterFrom(ref, Primitive::kPrimNot);
8576 bool narrow = CanEmitNarrowLdr(ref_reg, obj, offset);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008577 vixl32::Register base = obj;
8578 if (offset >= kReferenceLoadMinFarOffset) {
8579 base = RegisterFrom(temp);
8580 DCHECK(!base.Is(kBakerCcEntrypointRegister));
8581 static_assert(IsPowerOfTwo(kReferenceLoadMinFarOffset), "Expecting a power of 2.");
8582 __ Add(base, obj, Operand(offset & ~(kReferenceLoadMinFarOffset - 1u)));
8583 offset &= (kReferenceLoadMinFarOffset - 1u);
Vladimir Marko88abba22017-05-03 17:09:25 +01008584 // Use narrow LDR only for small offsets. Generating narrow encoding LDR for the large
8585 // offsets with `(offset & (kReferenceLoadMinFarOffset - 1u)) < 32u` would most likely
8586 // increase the overall code size when taking the generated thunks into account.
8587 DCHECK(!narrow);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008588 }
8589 UseScratchRegisterScope temps(GetVIXLAssembler());
8590 ExcludeIPAndBakerCcEntrypointRegister(&temps, instruction);
8591 uint32_t custom_data = linker::Thumb2RelativePatcher::EncodeBakerReadBarrierFieldData(
Vladimir Marko88abba22017-05-03 17:09:25 +01008592 base.GetCode(), obj.GetCode(), narrow);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008593 vixl32::Label* bne_label = NewBakerReadBarrierPatch(custom_data);
8594
Roland Levillain5daa4952017-07-03 17:23:56 +01008595 {
8596 vixl::EmissionCheckScope guard(
8597 GetVIXLAssembler(),
8598 (kPoisonHeapReferences ? 5u : 4u) * vixl32::kMaxInstructionSizeInBytes);
8599 vixl32::Label return_address;
8600 EmitAdrCode adr(GetVIXLAssembler(), lr, &return_address);
8601 __ cmp(mr, Operand(0));
8602 EmitPlaceholderBne(this, bne_label);
8603 ptrdiff_t old_offset = GetVIXLAssembler()->GetBuffer()->GetCursorOffset();
8604 __ ldr(EncodingSize(narrow ? Narrow : Wide), ref_reg, MemOperand(base, offset));
8605 if (needs_null_check) {
8606 MaybeRecordImplicitNullCheck(instruction);
Vladimir Marko88abba22017-05-03 17:09:25 +01008607 }
Roland Levillain5daa4952017-07-03 17:23:56 +01008608 // Note: We need a specific width for the unpoisoning NEG.
8609 if (kPoisonHeapReferences) {
8610 if (narrow) {
8611 // The only 16-bit encoding is T1 which sets flags outside IT block (i.e. RSBS, not RSB).
8612 __ rsbs(EncodingSize(Narrow), ref_reg, ref_reg, Operand(0));
8613 } else {
8614 __ rsb(EncodingSize(Wide), ref_reg, ref_reg, Operand(0));
8615 }
8616 }
8617 __ Bind(&return_address);
8618 DCHECK_EQ(old_offset - GetVIXLAssembler()->GetBuffer()->GetCursorOffset(),
8619 narrow ? BAKER_MARK_INTROSPECTION_FIELD_LDR_NARROW_OFFSET
8620 : BAKER_MARK_INTROSPECTION_FIELD_LDR_WIDE_OFFSET);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008621 }
Roland Levillain5daa4952017-07-03 17:23:56 +01008622 MaybeGenerateMarkingRegisterCheck(/* code */ 19, /* temp_loc */ LocationFrom(ip));
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008623 return;
8624 }
8625
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008626 // /* HeapReference<Object> */ ref = *(obj + offset)
8627 Location no_index = Location::NoLocation();
8628 ScaleFactor no_scale_factor = TIMES_1;
8629 GenerateReferenceLoadWithBakerReadBarrier(
8630 instruction, ref, obj, offset, no_index, no_scale_factor, temp, needs_null_check);
Roland Levillain6070e882016-11-03 17:51:58 +00008631}
8632
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008633void CodeGeneratorARMVIXL::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
8634 Location ref,
8635 vixl32::Register obj,
8636 uint32_t data_offset,
8637 Location index,
8638 Location temp,
8639 bool needs_null_check) {
8640 DCHECK(kEmitCompilerReadBarrier);
8641 DCHECK(kUseBakerReadBarrier);
8642
8643 static_assert(
8644 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
8645 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008646 ScaleFactor scale_factor = TIMES_4;
8647
8648 if (kBakerReadBarrierLinkTimeThunksEnableForArrays &&
8649 !Runtime::Current()->UseJitCompilation()) {
Roland Levillain6d729a72017-06-30 18:34:01 +01008650 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in the
8651 // Marking Register) to decide whether we need to enter the slow
8652 // path to mark the reference. Then, in the slow path, check the
8653 // gray bit in the lock word of the reference's holder (`obj`) to
8654 // decide whether to mark `ref` or not.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008655 //
8656 // We use link-time generated thunks for the slow path. That thunk checks
8657 // the holder and jumps to the entrypoint if needed. If the holder is not
8658 // gray, it creates a fake dependency and returns to the LDR instruction.
8659 //
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008660 // lr = &gray_return_address;
Roland Levillain6d729a72017-06-30 18:34:01 +01008661 // if (mr) { // Thread::Current()->GetIsGcMarking()
8662 // goto array_thunk<base_reg>(lr)
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008663 // }
8664 // not_gray_return_address:
8665 // // Original reference load. If the offset is too large to fit
8666 // // into LDR, we use an adjusted base register here.
Vladimir Marko88abba22017-05-03 17:09:25 +01008667 // HeapReference<mirror::Object> reference = data[index];
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008668 // gray_return_address:
8669
8670 DCHECK(index.IsValid());
8671 vixl32::Register index_reg = RegisterFrom(index, Primitive::kPrimInt);
8672 vixl32::Register ref_reg = RegisterFrom(ref, Primitive::kPrimNot);
8673 vixl32::Register data_reg = RegisterFrom(temp, Primitive::kPrimInt); // Raw pointer.
8674 DCHECK(!data_reg.Is(kBakerCcEntrypointRegister));
8675
8676 UseScratchRegisterScope temps(GetVIXLAssembler());
8677 ExcludeIPAndBakerCcEntrypointRegister(&temps, instruction);
8678 uint32_t custom_data =
8679 linker::Thumb2RelativePatcher::EncodeBakerReadBarrierArrayData(data_reg.GetCode());
8680 vixl32::Label* bne_label = NewBakerReadBarrierPatch(custom_data);
8681
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008682 __ Add(data_reg, obj, Operand(data_offset));
Roland Levillain5daa4952017-07-03 17:23:56 +01008683 {
8684 vixl::EmissionCheckScope guard(
8685 GetVIXLAssembler(),
8686 (kPoisonHeapReferences ? 5u : 4u) * vixl32::kMaxInstructionSizeInBytes);
8687 vixl32::Label return_address;
8688 EmitAdrCode adr(GetVIXLAssembler(), lr, &return_address);
8689 __ cmp(mr, Operand(0));
8690 EmitPlaceholderBne(this, bne_label);
8691 ptrdiff_t old_offset = GetVIXLAssembler()->GetBuffer()->GetCursorOffset();
8692 __ ldr(ref_reg, MemOperand(data_reg, index_reg, vixl32::LSL, scale_factor));
8693 DCHECK(!needs_null_check); // The thunk cannot handle the null check.
8694 // Note: We need a Wide NEG for the unpoisoning.
8695 if (kPoisonHeapReferences) {
8696 __ rsb(EncodingSize(Wide), ref_reg, ref_reg, Operand(0));
8697 }
8698 __ Bind(&return_address);
8699 DCHECK_EQ(old_offset - GetVIXLAssembler()->GetBuffer()->GetCursorOffset(),
8700 BAKER_MARK_INTROSPECTION_ARRAY_LDR_OFFSET);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008701 }
Roland Levillain5daa4952017-07-03 17:23:56 +01008702 MaybeGenerateMarkingRegisterCheck(/* code */ 20, /* temp_loc */ LocationFrom(ip));
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008703 return;
8704 }
8705
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008706 // /* HeapReference<Object> */ ref =
8707 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008708 GenerateReferenceLoadWithBakerReadBarrier(
8709 instruction, ref, obj, data_offset, index, scale_factor, temp, needs_null_check);
Roland Levillain6070e882016-11-03 17:51:58 +00008710}
8711
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008712void CodeGeneratorARMVIXL::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
8713 Location ref,
8714 vixl32::Register obj,
8715 uint32_t offset,
8716 Location index,
8717 ScaleFactor scale_factor,
8718 Location temp,
Roland Levillainff487002017-03-07 16:50:01 +00008719 bool needs_null_check) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008720 DCHECK(kEmitCompilerReadBarrier);
8721 DCHECK(kUseBakerReadBarrier);
8722
Roland Levillain6d729a72017-06-30 18:34:01 +01008723 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in the
8724 // Marking Register) to decide whether we need to enter the slow
8725 // path to mark the reference. Then, in the slow path, check the
8726 // gray bit in the lock word of the reference's holder (`obj`) to
8727 // decide whether to mark `ref` or not.
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008728 //
Roland Levillain6d729a72017-06-30 18:34:01 +01008729 // if (mr) { // Thread::Current()->GetIsGcMarking()
Roland Levillainff487002017-03-07 16:50:01 +00008730 // // Slow path.
8731 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
8732 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
8733 // HeapReference<mirror::Object> ref = *src; // Original reference load.
8734 // bool is_gray = (rb_state == ReadBarrier::GrayState());
8735 // if (is_gray) {
Roland Levillain6d729a72017-06-30 18:34:01 +01008736 // entrypoint = Thread::Current()->pReadBarrierMarkReg ## root.reg()
8737 // ref = entrypoint(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
Roland Levillainff487002017-03-07 16:50:01 +00008738 // }
8739 // } else {
8740 // HeapReference<mirror::Object> ref = *src; // Original reference load.
8741 // }
8742
8743 vixl32::Register temp_reg = RegisterFrom(temp);
8744
8745 // Slow path marking the object `ref` when the GC is marking. The
Roland Levillain6d729a72017-06-30 18:34:01 +01008746 // entrypoint will be loaded by the slow path code.
Roland Levillainff487002017-03-07 16:50:01 +00008747 SlowPathCodeARMVIXL* slow_path =
8748 new (GetGraph()->GetArena()) LoadReferenceWithBakerReadBarrierSlowPathARMVIXL(
Roland Levillain6d729a72017-06-30 18:34:01 +01008749 instruction, ref, obj, offset, index, scale_factor, needs_null_check, temp_reg);
Roland Levillainff487002017-03-07 16:50:01 +00008750 AddSlowPath(slow_path);
8751
Roland Levillain6d729a72017-06-30 18:34:01 +01008752 __ CompareAndBranchIfNonZero(mr, slow_path->GetEntryLabel());
Roland Levillainff487002017-03-07 16:50:01 +00008753 // Fast path: the GC is not marking: just load the reference.
8754 GenerateRawReferenceLoad(instruction, ref, obj, offset, index, scale_factor, needs_null_check);
8755 __ Bind(slow_path->GetExitLabel());
Roland Levillain5daa4952017-07-03 17:23:56 +01008756 MaybeGenerateMarkingRegisterCheck(/* code */ 21);
Roland Levillainff487002017-03-07 16:50:01 +00008757}
8758
8759void CodeGeneratorARMVIXL::UpdateReferenceFieldWithBakerReadBarrier(HInstruction* instruction,
8760 Location ref,
8761 vixl32::Register obj,
8762 Location field_offset,
8763 Location temp,
8764 bool needs_null_check,
8765 vixl32::Register temp2) {
8766 DCHECK(kEmitCompilerReadBarrier);
8767 DCHECK(kUseBakerReadBarrier);
8768
Roland Levillain6d729a72017-06-30 18:34:01 +01008769 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in the
8770 // Marking Register) to decide whether we need to enter the slow
8771 // path to update the reference field within `obj`. Then, in the
8772 // slow path, check the gray bit in the lock word of the reference's
8773 // holder (`obj`) to decide whether to mark `ref` and update the
8774 // field or not.
Roland Levillainff487002017-03-07 16:50:01 +00008775 //
Roland Levillain6d729a72017-06-30 18:34:01 +01008776 // if (mr) { // Thread::Current()->GetIsGcMarking()
Roland Levillainba650a42017-03-06 13:52:32 +00008777 // // Slow path.
Roland Levillain54f869e2017-03-06 13:54:11 +00008778 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
8779 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
Roland Levillainff487002017-03-07 16:50:01 +00008780 // HeapReference<mirror::Object> ref = *(obj + field_offset); // Reference load.
Roland Levillain54f869e2017-03-06 13:54:11 +00008781 // bool is_gray = (rb_state == ReadBarrier::GrayState());
8782 // if (is_gray) {
Roland Levillainff487002017-03-07 16:50:01 +00008783 // old_ref = ref;
Roland Levillain6d729a72017-06-30 18:34:01 +01008784 // entrypoint = Thread::Current()->pReadBarrierMarkReg ## root.reg()
8785 // ref = entrypoint(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
Roland Levillainff487002017-03-07 16:50:01 +00008786 // compareAndSwapObject(obj, field_offset, old_ref, ref);
Roland Levillain54f869e2017-03-06 13:54:11 +00008787 // }
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008788 // }
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008789
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008790 vixl32::Register temp_reg = RegisterFrom(temp);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008791
Roland Levillainff487002017-03-07 16:50:01 +00008792 // Slow path updating the object reference at address `obj + field_offset`
Roland Levillain6d729a72017-06-30 18:34:01 +01008793 // when the GC is marking. The entrypoint will be loaded by the slow path code.
Roland Levillainff487002017-03-07 16:50:01 +00008794 SlowPathCodeARMVIXL* slow_path =
8795 new (GetGraph()->GetArena()) LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARMVIXL(
8796 instruction,
8797 ref,
8798 obj,
8799 /* offset */ 0u,
8800 /* index */ field_offset,
8801 /* scale_factor */ ScaleFactor::TIMES_1,
8802 needs_null_check,
8803 temp_reg,
Roland Levillain6d729a72017-06-30 18:34:01 +01008804 temp2);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008805 AddSlowPath(slow_path);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008806
Roland Levillain6d729a72017-06-30 18:34:01 +01008807 __ CompareAndBranchIfNonZero(mr, slow_path->GetEntryLabel());
Roland Levillainff487002017-03-07 16:50:01 +00008808 // Fast path: the GC is not marking: nothing to do (the field is
8809 // up-to-date, and we don't need to load the reference).
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008810 __ Bind(slow_path->GetExitLabel());
Roland Levillain5daa4952017-07-03 17:23:56 +01008811 MaybeGenerateMarkingRegisterCheck(/* code */ 22);
Roland Levillain844e6532016-11-03 16:09:47 +00008812}
Scott Wakelingfe885462016-09-22 10:24:38 +01008813
Roland Levillainba650a42017-03-06 13:52:32 +00008814void CodeGeneratorARMVIXL::GenerateRawReferenceLoad(HInstruction* instruction,
8815 Location ref,
8816 vixl::aarch32::Register obj,
8817 uint32_t offset,
8818 Location index,
8819 ScaleFactor scale_factor,
8820 bool needs_null_check) {
8821 Primitive::Type type = Primitive::kPrimNot;
8822 vixl32::Register ref_reg = RegisterFrom(ref, type);
8823
8824 // If needed, vixl::EmissionCheckScope guards are used to ensure
8825 // that no pools are emitted between the load (macro) instruction
8826 // and MaybeRecordImplicitNullCheck.
8827
Scott Wakelingfe885462016-09-22 10:24:38 +01008828 if (index.IsValid()) {
8829 // Load types involving an "index": ArrayGet,
8830 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
8831 // intrinsics.
Roland Levillainba650a42017-03-06 13:52:32 +00008832 // /* HeapReference<mirror::Object> */ ref = *(obj + offset + (index << scale_factor))
Scott Wakelingfe885462016-09-22 10:24:38 +01008833 if (index.IsConstant()) {
8834 size_t computed_offset =
8835 (Int32ConstantFrom(index) << scale_factor) + offset;
Roland Levillainba650a42017-03-06 13:52:32 +00008836 vixl::EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Scott Wakelingfe885462016-09-22 10:24:38 +01008837 GetAssembler()->LoadFromOffset(kLoadWord, ref_reg, obj, computed_offset);
Roland Levillainba650a42017-03-06 13:52:32 +00008838 if (needs_null_check) {
8839 MaybeRecordImplicitNullCheck(instruction);
8840 }
Scott Wakelingfe885462016-09-22 10:24:38 +01008841 } else {
8842 // Handle the special case of the
8843 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
8844 // intrinsics, which use a register pair as index ("long
8845 // offset"), of which only the low part contains data.
8846 vixl32::Register index_reg = index.IsRegisterPair()
8847 ? LowRegisterFrom(index)
8848 : RegisterFrom(index);
8849 UseScratchRegisterScope temps(GetVIXLAssembler());
Roland Levillainba650a42017-03-06 13:52:32 +00008850 vixl32::Register temp = temps.Acquire();
8851 __ Add(temp, obj, Operand(index_reg, ShiftType::LSL, scale_factor));
8852 {
8853 vixl::EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
8854 GetAssembler()->LoadFromOffset(kLoadWord, ref_reg, temp, offset);
8855 if (needs_null_check) {
8856 MaybeRecordImplicitNullCheck(instruction);
8857 }
8858 }
Scott Wakelingfe885462016-09-22 10:24:38 +01008859 }
8860 } else {
Roland Levillainba650a42017-03-06 13:52:32 +00008861 // /* HeapReference<mirror::Object> */ ref = *(obj + offset)
8862 vixl::EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Scott Wakelingfe885462016-09-22 10:24:38 +01008863 GetAssembler()->LoadFromOffset(kLoadWord, ref_reg, obj, offset);
Roland Levillainba650a42017-03-06 13:52:32 +00008864 if (needs_null_check) {
8865 MaybeRecordImplicitNullCheck(instruction);
8866 }
Scott Wakelingfe885462016-09-22 10:24:38 +01008867 }
8868
Roland Levillain844e6532016-11-03 16:09:47 +00008869 // Object* ref = ref_addr->AsMirrorPtr()
8870 GetAssembler()->MaybeUnpoisonHeapReference(ref_reg);
Roland Levillain844e6532016-11-03 16:09:47 +00008871}
8872
Roland Levillain5daa4952017-07-03 17:23:56 +01008873void CodeGeneratorARMVIXL::MaybeGenerateMarkingRegisterCheck(int code, Location temp_loc) {
8874 // The following condition is a compile-time one, so it does not have a run-time cost.
8875 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier && kIsDebugBuild) {
8876 // The following condition is a run-time one; it is executed after the
8877 // previous compile-time test, to avoid penalizing non-debug builds.
8878 if (GetCompilerOptions().EmitRunTimeChecksInDebugMode()) {
8879 UseScratchRegisterScope temps(GetVIXLAssembler());
8880 vixl32::Register temp = temp_loc.IsValid() ? RegisterFrom(temp_loc) : temps.Acquire();
8881 GetAssembler()->GenerateMarkingRegisterCheck(temp,
8882 kMarkingRegisterCheckBreakCodeBaseCode + code);
8883 }
8884 }
8885}
8886
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008887void CodeGeneratorARMVIXL::GenerateReadBarrierSlow(HInstruction* instruction,
8888 Location out,
8889 Location ref,
8890 Location obj,
8891 uint32_t offset,
8892 Location index) {
8893 DCHECK(kEmitCompilerReadBarrier);
8894
8895 // Insert a slow path based read barrier *after* the reference load.
8896 //
8897 // If heap poisoning is enabled, the unpoisoning of the loaded
8898 // reference will be carried out by the runtime within the slow
8899 // path.
8900 //
8901 // Note that `ref` currently does not get unpoisoned (when heap
8902 // poisoning is enabled), which is alright as the `ref` argument is
8903 // not used by the artReadBarrierSlow entry point.
8904 //
8905 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
8906 SlowPathCodeARMVIXL* slow_path = new (GetGraph()->GetArena())
8907 ReadBarrierForHeapReferenceSlowPathARMVIXL(instruction, out, ref, obj, offset, index);
8908 AddSlowPath(slow_path);
8909
8910 __ B(slow_path->GetEntryLabel());
8911 __ Bind(slow_path->GetExitLabel());
8912}
8913
8914void CodeGeneratorARMVIXL::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
Artem Serov02d37832016-10-25 15:25:33 +01008915 Location out,
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008916 Location ref,
8917 Location obj,
8918 uint32_t offset,
8919 Location index) {
Artem Serov02d37832016-10-25 15:25:33 +01008920 if (kEmitCompilerReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008921 // Baker's read barriers shall be handled by the fast path
Roland Levillain9983e302017-07-14 14:34:22 +01008922 // (CodeGeneratorARMVIXL::GenerateReferenceLoadWithBakerReadBarrier).
Artem Serov02d37832016-10-25 15:25:33 +01008923 DCHECK(!kUseBakerReadBarrier);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008924 // If heap poisoning is enabled, unpoisoning will be taken care of
8925 // by the runtime within the slow path.
8926 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Artem Serov02d37832016-10-25 15:25:33 +01008927 } else if (kPoisonHeapReferences) {
8928 GetAssembler()->UnpoisonHeapReference(RegisterFrom(out));
8929 }
8930}
8931
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008932void CodeGeneratorARMVIXL::GenerateReadBarrierForRootSlow(HInstruction* instruction,
8933 Location out,
8934 Location root) {
8935 DCHECK(kEmitCompilerReadBarrier);
8936
8937 // Insert a slow path based read barrier *after* the GC root load.
8938 //
8939 // Note that GC roots are not affected by heap poisoning, so we do
8940 // not need to do anything special for this here.
8941 SlowPathCodeARMVIXL* slow_path =
8942 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathARMVIXL(instruction, out, root);
8943 AddSlowPath(slow_path);
8944
8945 __ B(slow_path->GetEntryLabel());
8946 __ Bind(slow_path->GetExitLabel());
8947}
8948
Artem Serov02d37832016-10-25 15:25:33 +01008949// Check if the desired_dispatch_info is supported. If it is, return it,
8950// otherwise return a fall-back info that should be used instead.
8951HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARMVIXL::GetSupportedInvokeStaticOrDirectDispatch(
Artem Serovd4cc5b22016-11-04 11:19:09 +00008952 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +00008953 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Nicolas Geoffraye807ff72017-01-23 09:03:12 +00008954 return desired_dispatch_info;
Artem Serov02d37832016-10-25 15:25:33 +01008955}
8956
Scott Wakelingfe885462016-09-22 10:24:38 +01008957vixl32::Register CodeGeneratorARMVIXL::GetInvokeStaticOrDirectExtraParameter(
8958 HInvokeStaticOrDirect* invoke, vixl32::Register temp) {
8959 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
8960 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
8961 if (!invoke->GetLocations()->Intrinsified()) {
8962 return RegisterFrom(location);
8963 }
8964 // For intrinsics we allow any location, so it may be on the stack.
8965 if (!location.IsRegister()) {
8966 GetAssembler()->LoadFromOffset(kLoadWord, temp, sp, location.GetStackIndex());
8967 return temp;
8968 }
8969 // For register locations, check if the register was saved. If so, get it from the stack.
8970 // Note: There is a chance that the register was saved but not overwritten, so we could
8971 // save one load. However, since this is just an intrinsic slow path we prefer this
8972 // simple and more robust approach rather that trying to determine if that's the case.
8973 SlowPathCode* slow_path = GetCurrentSlowPath();
Scott Wakelingd5cd4972017-02-03 11:38:35 +00008974 if (slow_path != nullptr && slow_path->IsCoreRegisterSaved(RegisterFrom(location).GetCode())) {
Scott Wakelingfe885462016-09-22 10:24:38 +01008975 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(RegisterFrom(location).GetCode());
8976 GetAssembler()->LoadFromOffset(kLoadWord, temp, sp, stack_offset);
8977 return temp;
8978 }
8979 return RegisterFrom(location);
8980}
8981
Vladimir Markod254f5c2017-06-02 15:18:36 +00008982void CodeGeneratorARMVIXL::GenerateStaticOrDirectCall(
Vladimir Markoe7197bf2017-06-02 17:00:23 +01008983 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00008984 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
Scott Wakelingfe885462016-09-22 10:24:38 +01008985 switch (invoke->GetMethodLoadKind()) {
8986 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
8987 uint32_t offset =
8988 GetThreadOffset<kArmPointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
8989 // temp = thread->string_init_entrypoint
Artem Serovd4cc5b22016-11-04 11:19:09 +00008990 GetAssembler()->LoadFromOffset(kLoadWord, RegisterFrom(temp), tr, offset);
8991 break;
8992 }
8993 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
8994 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
8995 break;
Vladimir Marko65979462017-05-19 17:25:12 +01008996 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative: {
8997 DCHECK(GetCompilerOptions().IsBootImage());
8998 PcRelativePatchInfo* labels = NewPcRelativeMethodPatch(invoke->GetTargetMethod());
8999 vixl32::Register temp_reg = RegisterFrom(temp);
9000 EmitMovwMovtPlaceholder(labels, temp_reg);
9001 break;
9002 }
Artem Serovd4cc5b22016-11-04 11:19:09 +00009003 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
9004 __ Mov(RegisterFrom(temp), Operand::From(invoke->GetMethodAddress()));
9005 break;
Vladimir Marko0eb882b2017-05-15 13:39:18 +01009006 case HInvokeStaticOrDirect::MethodLoadKind::kBssEntry: {
9007 PcRelativePatchInfo* labels = NewMethodBssEntryPatch(
9008 MethodReference(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex()));
9009 vixl32::Register temp_reg = RegisterFrom(temp);
9010 EmitMovwMovtPlaceholder(labels, temp_reg);
9011 GetAssembler()->LoadFromOffset(kLoadWord, temp_reg, temp_reg, /* offset*/ 0);
Scott Wakelingfe885462016-09-22 10:24:38 +01009012 break;
9013 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01009014 case HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall: {
9015 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
9016 return; // No code pointer retrieval; the runtime performs the call directly.
Scott Wakelingfe885462016-09-22 10:24:38 +01009017 }
Scott Wakelingfe885462016-09-22 10:24:38 +01009018 }
9019
Artem Serovd4cc5b22016-11-04 11:19:09 +00009020 switch (invoke->GetCodePtrLocation()) {
9021 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Vladimir Markoe7197bf2017-06-02 17:00:23 +01009022 {
9023 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
9024 ExactAssemblyScope aas(GetVIXLAssembler(),
9025 vixl32::k32BitT32InstructionSizeInBytes,
9026 CodeBufferCheckScope::kMaximumSize);
9027 __ bl(GetFrameEntryLabel());
9028 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
9029 }
Artem Serovd4cc5b22016-11-04 11:19:09 +00009030 break;
Artem Serovd4cc5b22016-11-04 11:19:09 +00009031 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
9032 // LR = callee_method->entry_point_from_quick_compiled_code_
9033 GetAssembler()->LoadFromOffset(
9034 kLoadWord,
9035 lr,
9036 RegisterFrom(callee_method),
9037 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize).Int32Value());
Alexandre Rames374ddf32016-11-04 10:40:49 +00009038 {
Vladimir Markoe7197bf2017-06-02 17:00:23 +01009039 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
Alexandre Rames374ddf32016-11-04 10:40:49 +00009040 // 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 +00009041 ExactAssemblyScope aas(GetVIXLAssembler(),
9042 vixl32::k16BitT32InstructionSizeInBytes,
9043 CodeBufferCheckScope::kExactSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00009044 // LR()
9045 __ blx(lr);
Vladimir Markoe7197bf2017-06-02 17:00:23 +01009046 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Alexandre Rames374ddf32016-11-04 10:40:49 +00009047 }
Artem Serovd4cc5b22016-11-04 11:19:09 +00009048 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01009049 }
9050
Scott Wakelingfe885462016-09-22 10:24:38 +01009051 DCHECK(!IsLeafMethod());
9052}
9053
Vladimir Markoe7197bf2017-06-02 17:00:23 +01009054void CodeGeneratorARMVIXL::GenerateVirtualCall(
9055 HInvokeVirtual* invoke, Location temp_location, SlowPathCode* slow_path) {
Scott Wakelingfe885462016-09-22 10:24:38 +01009056 vixl32::Register temp = RegisterFrom(temp_location);
9057 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
9058 invoke->GetVTableIndex(), kArmPointerSize).Uint32Value();
9059
9060 // Use the calling convention instead of the location of the receiver, as
9061 // intrinsics may have put the receiver in a different register. In the intrinsics
9062 // slow path, the arguments have been moved to the right place, so here we are
9063 // guaranteed that the receiver is the first register of the calling convention.
9064 InvokeDexCallingConventionARMVIXL calling_convention;
9065 vixl32::Register receiver = calling_convention.GetRegisterAt(0);
9066 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Alexandre Rames374ddf32016-11-04 10:40:49 +00009067 {
9068 // Make sure the pc is recorded immediately after the `ldr` instruction.
Artem Serov0fb37192016-12-06 18:13:40 +00009069 ExactAssemblyScope aas(GetVIXLAssembler(),
9070 vixl32::kMaxInstructionSizeInBytes,
9071 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00009072 // /* HeapReference<Class> */ temp = receiver->klass_
9073 __ ldr(temp, MemOperand(receiver, class_offset));
9074 MaybeRecordImplicitNullCheck(invoke);
9075 }
Scott Wakelingfe885462016-09-22 10:24:38 +01009076 // Instead of simply (possibly) unpoisoning `temp` here, we should
9077 // emit a read barrier for the previous class reference load.
9078 // However this is not required in practice, as this is an
9079 // intermediate/temporary reference and because the current
9080 // concurrent copying collector keeps the from-space memory
9081 // intact/accessible until the end of the marking phase (the
9082 // concurrent copying collector may not in the future).
9083 GetAssembler()->MaybeUnpoisonHeapReference(temp);
9084
9085 // temp = temp->GetMethodAt(method_offset);
9086 uint32_t entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(
9087 kArmPointerSize).Int32Value();
9088 GetAssembler()->LoadFromOffset(kLoadWord, temp, temp, method_offset);
9089 // LR = temp->GetEntryPoint();
9090 GetAssembler()->LoadFromOffset(kLoadWord, lr, temp, entry_point);
Vladimir Markoe7197bf2017-06-02 17:00:23 +01009091 {
9092 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
9093 // blx in T32 has only 16bit encoding that's why a stricter check for the scope is used.
9094 ExactAssemblyScope aas(GetVIXLAssembler(),
9095 vixl32::k16BitT32InstructionSizeInBytes,
9096 CodeBufferCheckScope::kExactSize);
9097 // LR();
9098 __ blx(lr);
9099 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
9100 }
Scott Wakelingfe885462016-09-22 10:24:38 +01009101}
9102
Vladimir Marko65979462017-05-19 17:25:12 +01009103CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewPcRelativeMethodPatch(
9104 MethodReference target_method) {
9105 return NewPcRelativePatch(*target_method.dex_file,
9106 target_method.dex_method_index,
9107 &pc_relative_method_patches_);
Artem Serovd4cc5b22016-11-04 11:19:09 +00009108}
9109
Vladimir Marko0eb882b2017-05-15 13:39:18 +01009110CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewMethodBssEntryPatch(
9111 MethodReference target_method) {
9112 return NewPcRelativePatch(*target_method.dex_file,
9113 target_method.dex_method_index,
9114 &method_bss_entry_patches_);
9115}
9116
Artem Serovd4cc5b22016-11-04 11:19:09 +00009117CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewPcRelativeTypePatch(
9118 const DexFile& dex_file, dex::TypeIndex type_index) {
9119 return NewPcRelativePatch(dex_file, type_index.index_, &pc_relative_type_patches_);
9120}
9121
Vladimir Marko1998cd02017-01-13 13:02:58 +00009122CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewTypeBssEntryPatch(
9123 const DexFile& dex_file, dex::TypeIndex type_index) {
9124 return NewPcRelativePatch(dex_file, type_index.index_, &type_bss_entry_patches_);
9125}
9126
Vladimir Marko65979462017-05-19 17:25:12 +01009127CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewPcRelativeStringPatch(
9128 const DexFile& dex_file, dex::StringIndex string_index) {
9129 return NewPcRelativePatch(dex_file, string_index.index_, &pc_relative_string_patches_);
9130}
9131
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01009132CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewStringBssEntryPatch(
9133 const DexFile& dex_file, dex::StringIndex string_index) {
9134 return NewPcRelativePatch(dex_file, string_index.index_, &string_bss_entry_patches_);
9135}
9136
Artem Serovd4cc5b22016-11-04 11:19:09 +00009137CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewPcRelativePatch(
9138 const DexFile& dex_file, uint32_t offset_or_index, ArenaDeque<PcRelativePatchInfo>* patches) {
9139 patches->emplace_back(dex_file, offset_or_index);
9140 return &patches->back();
9141}
9142
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01009143vixl::aarch32::Label* CodeGeneratorARMVIXL::NewBakerReadBarrierPatch(uint32_t custom_data) {
9144 baker_read_barrier_patches_.emplace_back(custom_data);
9145 return &baker_read_barrier_patches_.back().label;
9146}
9147
Artem Serovc5fcb442016-12-02 19:19:58 +00009148VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateBootImageAddressLiteral(uint32_t address) {
Richard Uhlerc52f3032017-03-02 13:45:45 +00009149 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), &uint32_literals_);
Artem Serovc5fcb442016-12-02 19:19:58 +00009150}
9151
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00009152VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateJitStringLiteral(
9153 const DexFile& dex_file,
9154 dex::StringIndex string_index,
9155 Handle<mirror::String> handle) {
9156 jit_string_roots_.Overwrite(StringReference(&dex_file, string_index),
9157 reinterpret_cast64<uint64_t>(handle.GetReference()));
Artem Serovc5fcb442016-12-02 19:19:58 +00009158 return jit_string_patches_.GetOrCreate(
9159 StringReference(&dex_file, string_index),
9160 [this]() {
9161 return GetAssembler()->CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u);
9162 });
9163}
9164
9165VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateJitClassLiteral(const DexFile& dex_file,
9166 dex::TypeIndex type_index,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00009167 Handle<mirror::Class> handle) {
9168 jit_class_roots_.Overwrite(TypeReference(&dex_file, type_index),
9169 reinterpret_cast64<uint64_t>(handle.GetReference()));
Artem Serovc5fcb442016-12-02 19:19:58 +00009170 return jit_class_patches_.GetOrCreate(
9171 TypeReference(&dex_file, type_index),
9172 [this]() {
9173 return GetAssembler()->CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u);
9174 });
9175}
9176
Artem Serovd4cc5b22016-11-04 11:19:09 +00009177template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
9178inline void CodeGeneratorARMVIXL::EmitPcRelativeLinkerPatches(
9179 const ArenaDeque<PcRelativePatchInfo>& infos,
9180 ArenaVector<LinkerPatch>* linker_patches) {
9181 for (const PcRelativePatchInfo& info : infos) {
9182 const DexFile& dex_file = info.target_dex_file;
9183 size_t offset_or_index = info.offset_or_index;
9184 DCHECK(info.add_pc_label.IsBound());
9185 uint32_t add_pc_offset = dchecked_integral_cast<uint32_t>(info.add_pc_label.GetLocation());
9186 // Add MOVW patch.
9187 DCHECK(info.movw_label.IsBound());
9188 uint32_t movw_offset = dchecked_integral_cast<uint32_t>(info.movw_label.GetLocation());
9189 linker_patches->push_back(Factory(movw_offset, &dex_file, add_pc_offset, offset_or_index));
9190 // Add MOVT patch.
9191 DCHECK(info.movt_label.IsBound());
9192 uint32_t movt_offset = dchecked_integral_cast<uint32_t>(info.movt_label.GetLocation());
9193 linker_patches->push_back(Factory(movt_offset, &dex_file, add_pc_offset, offset_or_index));
9194 }
9195}
9196
9197void CodeGeneratorARMVIXL::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
9198 DCHECK(linker_patches->empty());
9199 size_t size =
Vladimir Marko65979462017-05-19 17:25:12 +01009200 /* MOVW+MOVT for each entry */ 2u * pc_relative_method_patches_.size() +
Vladimir Marko0eb882b2017-05-15 13:39:18 +01009201 /* MOVW+MOVT for each entry */ 2u * method_bss_entry_patches_.size() +
Artem Serovc5fcb442016-12-02 19:19:58 +00009202 /* MOVW+MOVT for each entry */ 2u * pc_relative_type_patches_.size() +
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01009203 /* MOVW+MOVT for each entry */ 2u * type_bss_entry_patches_.size() +
Vladimir Marko65979462017-05-19 17:25:12 +01009204 /* MOVW+MOVT for each entry */ 2u * pc_relative_string_patches_.size() +
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01009205 /* MOVW+MOVT for each entry */ 2u * string_bss_entry_patches_.size() +
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01009206 baker_read_barrier_patches_.size();
Artem Serovd4cc5b22016-11-04 11:19:09 +00009207 linker_patches->reserve(size);
Vladimir Marko65979462017-05-19 17:25:12 +01009208 if (GetCompilerOptions().IsBootImage()) {
9209 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeMethodPatch>(pc_relative_method_patches_,
Artem Serovd4cc5b22016-11-04 11:19:09 +00009210 linker_patches);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00009211 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(pc_relative_type_patches_,
9212 linker_patches);
Artem Serovd4cc5b22016-11-04 11:19:09 +00009213 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(pc_relative_string_patches_,
9214 linker_patches);
Vladimir Marko65979462017-05-19 17:25:12 +01009215 } else {
9216 DCHECK(pc_relative_method_patches_.empty());
9217 DCHECK(pc_relative_type_patches_.empty());
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01009218 EmitPcRelativeLinkerPatches<LinkerPatch::StringInternTablePatch>(pc_relative_string_patches_,
9219 linker_patches);
Artem Serovd4cc5b22016-11-04 11:19:09 +00009220 }
Vladimir Marko0eb882b2017-05-15 13:39:18 +01009221 EmitPcRelativeLinkerPatches<LinkerPatch::MethodBssEntryPatch>(method_bss_entry_patches_,
9222 linker_patches);
Vladimir Marko1998cd02017-01-13 13:02:58 +00009223 EmitPcRelativeLinkerPatches<LinkerPatch::TypeBssEntryPatch>(type_bss_entry_patches_,
9224 linker_patches);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01009225 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(string_bss_entry_patches_,
9226 linker_patches);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01009227 for (const BakerReadBarrierPatchInfo& info : baker_read_barrier_patches_) {
9228 linker_patches->push_back(LinkerPatch::BakerReadBarrierBranchPatch(info.label.GetLocation(),
9229 info.custom_data));
9230 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00009231 DCHECK_EQ(size, linker_patches->size());
Artem Serovc5fcb442016-12-02 19:19:58 +00009232}
9233
9234VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateUint32Literal(
9235 uint32_t value,
9236 Uint32ToLiteralMap* map) {
9237 return map->GetOrCreate(
9238 value,
9239 [this, value]() {
9240 return GetAssembler()->CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ value);
9241 });
9242}
9243
Artem Serov2bbc9532016-10-21 11:51:50 +01009244void LocationsBuilderARMVIXL::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
9245 LocationSummary* locations =
9246 new (GetGraph()->GetArena()) LocationSummary(instr, LocationSummary::kNoCall);
9247 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
9248 Location::RequiresRegister());
9249 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
9250 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
9251 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
9252}
9253
9254void InstructionCodeGeneratorARMVIXL::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
9255 vixl32::Register res = OutputRegister(instr);
9256 vixl32::Register accumulator =
9257 InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
9258 vixl32::Register mul_left =
9259 InputRegisterAt(instr, HMultiplyAccumulate::kInputMulLeftIndex);
9260 vixl32::Register mul_right =
9261 InputRegisterAt(instr, HMultiplyAccumulate::kInputMulRightIndex);
9262
9263 if (instr->GetOpKind() == HInstruction::kAdd) {
9264 __ Mla(res, mul_left, mul_right, accumulator);
9265 } else {
9266 __ Mls(res, mul_left, mul_right, accumulator);
9267 }
9268}
9269
Artem Serov551b28f2016-10-18 19:11:30 +01009270void LocationsBuilderARMVIXL::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
9271 // Nothing to do, this should be removed during prepare for register allocator.
9272 LOG(FATAL) << "Unreachable";
9273}
9274
9275void InstructionCodeGeneratorARMVIXL::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
9276 // Nothing to do, this should be removed during prepare for register allocator.
9277 LOG(FATAL) << "Unreachable";
9278}
9279
9280// Simple implementation of packed switch - generate cascaded compare/jumps.
9281void LocationsBuilderARMVIXL::VisitPackedSwitch(HPackedSwitch* switch_instr) {
9282 LocationSummary* locations =
9283 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
9284 locations->SetInAt(0, Location::RequiresRegister());
9285 if (switch_instr->GetNumEntries() > kPackedSwitchCompareJumpThreshold &&
9286 codegen_->GetAssembler()->GetVIXLAssembler()->IsUsingT32()) {
9287 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the table base.
9288 if (switch_instr->GetStartValue() != 0) {
9289 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the bias.
9290 }
9291 }
9292}
9293
9294// TODO(VIXL): Investigate and reach the parity with old arm codegen.
9295void InstructionCodeGeneratorARMVIXL::VisitPackedSwitch(HPackedSwitch* switch_instr) {
9296 int32_t lower_bound = switch_instr->GetStartValue();
9297 uint32_t num_entries = switch_instr->GetNumEntries();
9298 LocationSummary* locations = switch_instr->GetLocations();
9299 vixl32::Register value_reg = InputRegisterAt(switch_instr, 0);
9300 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
9301
9302 if (num_entries <= kPackedSwitchCompareJumpThreshold ||
9303 !codegen_->GetAssembler()->GetVIXLAssembler()->IsUsingT32()) {
9304 // Create a series of compare/jumps.
Anton Kirilovedb2ac32016-11-30 15:14:10 +00009305 UseScratchRegisterScope temps(GetVIXLAssembler());
Artem Serov551b28f2016-10-18 19:11:30 +01009306 vixl32::Register temp_reg = temps.Acquire();
9307 // Note: It is fine for the below AddConstantSetFlags() using IP register to temporarily store
9308 // the immediate, because IP is used as the destination register. For the other
9309 // AddConstantSetFlags() and GenerateCompareWithImmediate(), the immediate values are constant,
9310 // and they can be encoded in the instruction without making use of IP register.
9311 __ Adds(temp_reg, value_reg, -lower_bound);
9312
9313 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
9314 // Jump to successors[0] if value == lower_bound.
9315 __ B(eq, codegen_->GetLabelOf(successors[0]));
9316 int32_t last_index = 0;
9317 for (; num_entries - last_index > 2; last_index += 2) {
9318 __ Adds(temp_reg, temp_reg, -2);
9319 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
9320 __ B(lo, codegen_->GetLabelOf(successors[last_index + 1]));
9321 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
9322 __ B(eq, codegen_->GetLabelOf(successors[last_index + 2]));
9323 }
9324 if (num_entries - last_index == 2) {
9325 // The last missing case_value.
9326 __ Cmp(temp_reg, 1);
9327 __ B(eq, codegen_->GetLabelOf(successors[last_index + 1]));
9328 }
9329
9330 // And the default for any other value.
9331 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
9332 __ B(codegen_->GetLabelOf(default_block));
9333 }
9334 } else {
9335 // Create a table lookup.
9336 vixl32::Register table_base = RegisterFrom(locations->GetTemp(0));
9337
9338 JumpTableARMVIXL* jump_table = codegen_->CreateJumpTable(switch_instr);
9339
9340 // Remove the bias.
9341 vixl32::Register key_reg;
9342 if (lower_bound != 0) {
9343 key_reg = RegisterFrom(locations->GetTemp(1));
9344 __ Sub(key_reg, value_reg, lower_bound);
9345 } else {
9346 key_reg = value_reg;
9347 }
9348
9349 // Check whether the value is in the table, jump to default block if not.
9350 __ Cmp(key_reg, num_entries - 1);
9351 __ B(hi, codegen_->GetLabelOf(default_block));
9352
Anton Kirilovedb2ac32016-11-30 15:14:10 +00009353 UseScratchRegisterScope temps(GetVIXLAssembler());
Artem Serov551b28f2016-10-18 19:11:30 +01009354 vixl32::Register jump_offset = temps.Acquire();
9355
9356 // Load jump offset from the table.
Scott Wakeling86e9d262017-01-18 15:59:24 +00009357 {
9358 const size_t jump_size = switch_instr->GetNumEntries() * sizeof(int32_t);
9359 ExactAssemblyScope aas(GetVIXLAssembler(),
9360 (vixl32::kMaxInstructionSizeInBytes * 4) + jump_size,
9361 CodeBufferCheckScope::kMaximumSize);
9362 __ adr(table_base, jump_table->GetTableStartLabel());
9363 __ ldr(jump_offset, MemOperand(table_base, key_reg, vixl32::LSL, 2));
Artem Serov551b28f2016-10-18 19:11:30 +01009364
Scott Wakeling86e9d262017-01-18 15:59:24 +00009365 // Jump to target block by branching to table_base(pc related) + offset.
9366 vixl32::Register target_address = table_base;
9367 __ add(target_address, table_base, jump_offset);
9368 __ bx(target_address);
Artem Serov09a940d2016-11-11 16:15:11 +00009369
Scott Wakeling86e9d262017-01-18 15:59:24 +00009370 jump_table->EmitTable(codegen_);
9371 }
Artem Serov551b28f2016-10-18 19:11:30 +01009372 }
9373}
9374
Artem Serov02d37832016-10-25 15:25:33 +01009375// Copy the result of a call into the given target.
Anton Kirilove28d9ae2016-10-25 18:17:23 +01009376void CodeGeneratorARMVIXL::MoveFromReturnRegister(Location trg, Primitive::Type type) {
9377 if (!trg.IsValid()) {
9378 DCHECK_EQ(type, Primitive::kPrimVoid);
9379 return;
9380 }
9381
9382 DCHECK_NE(type, Primitive::kPrimVoid);
9383
Artem Serovd4cc5b22016-11-04 11:19:09 +00009384 Location return_loc = InvokeDexCallingConventionVisitorARMVIXL().GetReturnLocation(type);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01009385 if (return_loc.Equals(trg)) {
9386 return;
9387 }
9388
9389 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
9390 // with the last branch.
9391 if (type == Primitive::kPrimLong) {
9392 TODO_VIXL32(FATAL);
9393 } else if (type == Primitive::kPrimDouble) {
9394 TODO_VIXL32(FATAL);
9395 } else {
9396 // Let the parallel move resolver take care of all of this.
9397 HParallelMove parallel_move(GetGraph()->GetArena());
9398 parallel_move.AddMove(return_loc, trg, type, nullptr);
9399 GetMoveResolver()->EmitNativeCode(&parallel_move);
9400 }
Scott Wakelingfe885462016-09-22 10:24:38 +01009401}
9402
xueliang.zhong8d2c4592016-11-23 17:05:25 +00009403void LocationsBuilderARMVIXL::VisitClassTableGet(HClassTableGet* instruction) {
9404 LocationSummary* locations =
9405 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
9406 locations->SetInAt(0, Location::RequiresRegister());
9407 locations->SetOut(Location::RequiresRegister());
Artem Serov551b28f2016-10-18 19:11:30 +01009408}
9409
xueliang.zhong8d2c4592016-11-23 17:05:25 +00009410void InstructionCodeGeneratorARMVIXL::VisitClassTableGet(HClassTableGet* instruction) {
9411 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
9412 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
9413 instruction->GetIndex(), kArmPointerSize).SizeValue();
9414 GetAssembler()->LoadFromOffset(kLoadWord,
9415 OutputRegister(instruction),
9416 InputRegisterAt(instruction, 0),
9417 method_offset);
9418 } else {
9419 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
9420 instruction->GetIndex(), kArmPointerSize));
9421 GetAssembler()->LoadFromOffset(kLoadWord,
9422 OutputRegister(instruction),
9423 InputRegisterAt(instruction, 0),
9424 mirror::Class::ImtPtrOffset(kArmPointerSize).Uint32Value());
9425 GetAssembler()->LoadFromOffset(kLoadWord,
9426 OutputRegister(instruction),
9427 OutputRegister(instruction),
9428 method_offset);
9429 }
Artem Serov551b28f2016-10-18 19:11:30 +01009430}
9431
Artem Serovc5fcb442016-12-02 19:19:58 +00009432static void PatchJitRootUse(uint8_t* code,
9433 const uint8_t* roots_data,
9434 VIXLUInt32Literal* literal,
9435 uint64_t index_in_table) {
9436 DCHECK(literal->IsBound());
9437 uint32_t literal_offset = literal->GetLocation();
9438 uintptr_t address =
9439 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
9440 uint8_t* data = code + literal_offset;
9441 reinterpret_cast<uint32_t*>(data)[0] = dchecked_integral_cast<uint32_t>(address);
9442}
9443
9444void CodeGeneratorARMVIXL::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
9445 for (const auto& entry : jit_string_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01009446 const StringReference& string_reference = entry.first;
9447 VIXLUInt32Literal* table_entry_literal = entry.second;
9448 const auto it = jit_string_roots_.find(string_reference);
Artem Serovc5fcb442016-12-02 19:19:58 +00009449 DCHECK(it != jit_string_roots_.end());
Vladimir Marko7d157fc2017-05-10 16:29:23 +01009450 uint64_t index_in_table = it->second;
9451 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Artem Serovc5fcb442016-12-02 19:19:58 +00009452 }
9453 for (const auto& entry : jit_class_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01009454 const TypeReference& type_reference = entry.first;
9455 VIXLUInt32Literal* table_entry_literal = entry.second;
9456 const auto it = jit_class_roots_.find(type_reference);
Artem Serovc5fcb442016-12-02 19:19:58 +00009457 DCHECK(it != jit_class_roots_.end());
Vladimir Marko7d157fc2017-05-10 16:29:23 +01009458 uint64_t index_in_table = it->second;
9459 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Artem Serovc5fcb442016-12-02 19:19:58 +00009460 }
9461}
9462
Artem Serovd4cc5b22016-11-04 11:19:09 +00009463void CodeGeneratorARMVIXL::EmitMovwMovtPlaceholder(
9464 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels,
9465 vixl32::Register out) {
Artem Serov0fb37192016-12-06 18:13:40 +00009466 ExactAssemblyScope aas(GetVIXLAssembler(),
9467 3 * vixl32::kMaxInstructionSizeInBytes,
9468 CodeBufferCheckScope::kMaximumSize);
Artem Serovd4cc5b22016-11-04 11:19:09 +00009469 // TODO(VIXL): Think about using mov instead of movw.
9470 __ bind(&labels->movw_label);
9471 __ movw(out, /* placeholder */ 0u);
9472 __ bind(&labels->movt_label);
9473 __ movt(out, /* placeholder */ 0u);
9474 __ bind(&labels->add_pc_label);
9475 __ add(out, out, pc);
9476}
9477
Scott Wakelingfe885462016-09-22 10:24:38 +01009478#undef __
9479#undef QUICK_ENTRY_POINT
9480#undef TODO_VIXL32
9481
9482} // namespace arm
9483} // namespace art