blob: b38a00630578fd0dd6ac4f34ba5ff5fa2681f702 [file] [log] [blame]
Scott Wakelingfe885462016-09-22 10:24:38 +01001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_arm_vixl.h"
18
Vladimir Markoeee1c0e2017-04-21 17:58:41 +010019#include "arch/arm/asm_support_arm.h"
Scott Wakelingfe885462016-09-22 10:24:38 +010020#include "arch/arm/instruction_set_features_arm.h"
21#include "art_method.h"
Andreas Gampe5678db52017-06-08 14:11:18 -070022#include "base/bit_utils.h"
23#include "base/bit_utils_iterator.h"
Vladimir Marko94ec2db2017-09-06 17:21:03 +010024#include "class_table.h"
Scott Wakelingfe885462016-09-22 10:24:38 +010025#include "code_generator_utils.h"
26#include "common_arm.h"
27#include "compiled_method.h"
28#include "entrypoints/quick/quick_entrypoints.h"
29#include "gc/accounting/card_table.h"
Andreas Gampe09659c22017-09-18 18:23:32 -070030#include "heap_poisoning.h"
Anton Kirilov5ec62182016-10-13 20:16:02 +010031#include "intrinsics_arm_vixl.h"
Vladimir Markoeee1c0e2017-04-21 17:58:41 +010032#include "linker/arm/relative_patcher_thumb2.h"
Vladimir Markod8dbc8d2017-09-20 13:37:47 +010033#include "linker/linker_patch.h"
Scott Wakelingfe885462016-09-22 10:24:38 +010034#include "mirror/array-inl.h"
35#include "mirror/class-inl.h"
36#include "thread.h"
37#include "utils/arm/assembler_arm_vixl.h"
38#include "utils/arm/managed_register_arm.h"
39#include "utils/assembler.h"
40#include "utils/stack_checks.h"
41
42namespace art {
43namespace arm {
44
45namespace vixl32 = vixl::aarch32;
46using namespace vixl32; // NOLINT(build/namespaces)
47
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +010048using helpers::DRegisterFrom;
Scott Wakelingfe885462016-09-22 10:24:38 +010049using helpers::DWARFReg;
Scott Wakelinga7812ae2016-10-17 10:03:36 +010050using helpers::HighDRegisterFrom;
51using helpers::HighRegisterFrom;
Donghui Bai426b49c2016-11-08 14:55:38 +080052using helpers::InputDRegisterAt;
Scott Wakelingfe885462016-09-22 10:24:38 +010053using helpers::InputOperandAt;
Scott Wakelingc34dba72016-10-03 10:14:44 +010054using helpers::InputRegister;
Scott Wakelinga7812ae2016-10-17 10:03:36 +010055using helpers::InputRegisterAt;
Scott Wakelingfe885462016-09-22 10:24:38 +010056using helpers::InputSRegisterAt;
Anton Kirilov644032c2016-12-06 17:51:43 +000057using helpers::InputVRegister;
Scott Wakelinga7812ae2016-10-17 10:03:36 +010058using helpers::InputVRegisterAt;
Scott Wakelingb77051e2016-11-21 19:46:00 +000059using helpers::Int32ConstantFrom;
Anton Kirilov644032c2016-12-06 17:51:43 +000060using helpers::Int64ConstantFrom;
Scott Wakelinga7812ae2016-10-17 10:03:36 +010061using helpers::LocationFrom;
62using helpers::LowRegisterFrom;
63using helpers::LowSRegisterFrom;
Donghui Bai426b49c2016-11-08 14:55:38 +080064using helpers::OperandFrom;
Scott Wakelinga7812ae2016-10-17 10:03:36 +010065using helpers::OutputRegister;
66using helpers::OutputSRegister;
67using helpers::OutputVRegister;
68using helpers::RegisterFrom;
69using helpers::SRegisterFrom;
Anton Kirilov644032c2016-12-06 17:51:43 +000070using helpers::Uint64ConstantFrom;
Scott Wakelingfe885462016-09-22 10:24:38 +010071
Artem Serov0fb37192016-12-06 18:13:40 +000072using vixl::ExactAssemblyScope;
73using vixl::CodeBufferCheckScope;
74
Scott Wakelingfe885462016-09-22 10:24:38 +010075using RegisterList = vixl32::RegisterList;
76
77static bool ExpectedPairLayout(Location location) {
78 // We expected this for both core and fpu register pairs.
79 return ((location.low() & 1) == 0) && (location.low() + 1 == location.high());
80}
Artem Serovd4cc5b22016-11-04 11:19:09 +000081// Use a local definition to prevent copying mistakes.
82static constexpr size_t kArmWordSize = static_cast<size_t>(kArmPointerSize);
83static constexpr size_t kArmBitsPerWord = kArmWordSize * kBitsPerByte;
Artem Serov551b28f2016-10-18 19:11:30 +010084static constexpr uint32_t kPackedSwitchCompareJumpThreshold = 7;
Scott Wakelingfe885462016-09-22 10:24:38 +010085
Vladimir Markoeee1c0e2017-04-21 17:58:41 +010086// Reference load (except object array loads) is using LDR Rt, [Rn, #offset] which can handle
87// offset < 4KiB. For offsets >= 4KiB, the load shall be emitted as two or more instructions.
88// For the Baker read barrier implementation using link-generated thunks we need to split
89// the offset explicitly.
90constexpr uint32_t kReferenceLoadMinFarOffset = 4 * KB;
91
92// Flags controlling the use of link-time generated thunks for Baker read barriers.
93constexpr bool kBakerReadBarrierLinkTimeThunksEnableForFields = true;
94constexpr bool kBakerReadBarrierLinkTimeThunksEnableForArrays = true;
95constexpr bool kBakerReadBarrierLinkTimeThunksEnableForGcRoots = true;
96
97// The reserved entrypoint register for link-time generated thunks.
98const vixl32::Register kBakerCcEntrypointRegister = r4;
99
Roland Levillain5daa4952017-07-03 17:23:56 +0100100// Using a base helps identify when we hit Marking Register check breakpoints.
101constexpr int kMarkingRegisterCheckBreakCodeBaseCode = 0x10;
102
Scott Wakelingfe885462016-09-22 10:24:38 +0100103#ifdef __
104#error "ARM Codegen VIXL macro-assembler macro already defined."
105#endif
106
Scott Wakelingfe885462016-09-22 10:24:38 +0100107// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
108#define __ down_cast<CodeGeneratorARMVIXL*>(codegen)->GetVIXLAssembler()-> // NOLINT
109#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kArmPointerSize, x).Int32Value()
110
111// Marker that code is yet to be, and must, be implemented.
112#define TODO_VIXL32(level) LOG(level) << __PRETTY_FUNCTION__ << " unimplemented "
113
Vladimir Markoeee1c0e2017-04-21 17:58:41 +0100114static inline void ExcludeIPAndBakerCcEntrypointRegister(UseScratchRegisterScope* temps,
115 HInstruction* instruction) {
116 DCHECK(temps->IsAvailable(ip));
117 temps->Exclude(ip);
118 DCHECK(!temps->IsAvailable(kBakerCcEntrypointRegister));
119 DCHECK_EQ(kBakerCcEntrypointRegister.GetCode(),
120 linker::Thumb2RelativePatcher::kBakerCcEntrypointRegister);
121 DCHECK_NE(instruction->GetLocations()->GetTempCount(), 0u);
122 DCHECK(RegisterFrom(instruction->GetLocations()->GetTemp(
123 instruction->GetLocations()->GetTempCount() - 1u)).Is(kBakerCcEntrypointRegister));
124}
125
126static inline void EmitPlaceholderBne(CodeGeneratorARMVIXL* codegen, vixl32::Label* patch_label) {
127 ExactAssemblyScope eas(codegen->GetVIXLAssembler(), kMaxInstructionSizeInBytes);
128 __ bind(patch_label);
129 vixl32::Label placeholder_label;
130 __ b(ne, EncodingSize(Wide), &placeholder_label); // Placeholder, patched at link-time.
131 __ bind(&placeholder_label);
132}
133
Vladimir Marko88abba22017-05-03 17:09:25 +0100134static inline bool CanEmitNarrowLdr(vixl32::Register rt, vixl32::Register rn, uint32_t offset) {
135 return rt.IsLow() && rn.IsLow() && offset < 32u;
136}
137
Vladimir Markoeee1c0e2017-04-21 17:58:41 +0100138class EmitAdrCode {
139 public:
140 EmitAdrCode(ArmVIXLMacroAssembler* assembler, vixl32::Register rd, vixl32::Label* label)
141 : assembler_(assembler), rd_(rd), label_(label) {
142 ExactAssemblyScope aas(assembler, kMaxInstructionSizeInBytes);
143 adr_location_ = assembler->GetCursorOffset();
144 assembler->adr(EncodingSize(Wide), rd, label);
145 }
146
147 ~EmitAdrCode() {
148 DCHECK(label_->IsBound());
149 // The ADR emitted by the assembler does not set the Thumb mode bit we need.
150 // TODO: Maybe extend VIXL to allow ADR for return address?
151 uint8_t* raw_adr = assembler_->GetBuffer()->GetOffsetAddress<uint8_t*>(adr_location_);
152 // Expecting ADR encoding T3 with `(offset & 1) == 0`.
153 DCHECK_EQ(raw_adr[1] & 0xfbu, 0xf2u); // Check bits 24-31, except 26.
154 DCHECK_EQ(raw_adr[0] & 0xffu, 0x0fu); // Check bits 16-23.
155 DCHECK_EQ(raw_adr[3] & 0x8fu, rd_.GetCode()); // Check bits 8-11 and 15.
156 DCHECK_EQ(raw_adr[2] & 0x01u, 0x00u); // Check bit 0, i.e. the `offset & 1`.
157 // Add the Thumb mode bit.
158 raw_adr[2] |= 0x01u;
159 }
160
161 private:
162 ArmVIXLMacroAssembler* const assembler_;
163 vixl32::Register rd_;
164 vixl32::Label* const label_;
165 int32_t adr_location_;
166};
167
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100168// SaveLiveRegisters and RestoreLiveRegisters from SlowPathCodeARM operate on sets of S registers,
169// for each live D registers they treat two corresponding S registers as live ones.
170//
171// Two following functions (SaveContiguousSRegisterList, RestoreContiguousSRegisterList) build
172// from a list of contiguous S registers a list of contiguous D registers (processing first/last
173// S registers corner cases) and save/restore this new list treating them as D registers.
174// - decreasing code size
175// - avoiding hazards on Cortex-A57, when a pair of S registers for an actual live D register is
176// restored and then used in regular non SlowPath code as D register.
177//
178// For the following example (v means the S register is live):
179// D names: | D0 | D1 | D2 | D4 | ...
180// S names: | S0 | S1 | S2 | S3 | S4 | S5 | S6 | S7 | ...
181// Live? | | v | v | v | v | v | v | | ...
182//
183// S1 and S6 will be saved/restored independently; D registers list (D1, D2) will be processed
184// as D registers.
185//
186// TODO(VIXL): All this code should be unnecessary once the VIXL AArch32 backend provides helpers
187// for lists of floating-point registers.
188static size_t SaveContiguousSRegisterList(size_t first,
189 size_t last,
190 CodeGenerator* codegen,
191 size_t stack_offset) {
192 static_assert(kSRegSizeInBytes == kArmWordSize, "Broken assumption on reg/word sizes.");
193 static_assert(kDRegSizeInBytes == 2 * kArmWordSize, "Broken assumption on reg/word sizes.");
194 DCHECK_LE(first, last);
195 if ((first == last) && (first == 0)) {
196 __ Vstr(vixl32::SRegister(first), MemOperand(sp, stack_offset));
197 return stack_offset + kSRegSizeInBytes;
198 }
199 if (first % 2 == 1) {
200 __ Vstr(vixl32::SRegister(first++), MemOperand(sp, stack_offset));
201 stack_offset += kSRegSizeInBytes;
202 }
203
204 bool save_last = false;
205 if (last % 2 == 0) {
206 save_last = true;
207 --last;
208 }
209
210 if (first < last) {
211 vixl32::DRegister d_reg = vixl32::DRegister(first / 2);
212 DCHECK_EQ((last - first + 1) % 2, 0u);
213 size_t number_of_d_regs = (last - first + 1) / 2;
214
215 if (number_of_d_regs == 1) {
216 __ Vstr(d_reg, MemOperand(sp, stack_offset));
217 } else if (number_of_d_regs > 1) {
218 UseScratchRegisterScope temps(down_cast<CodeGeneratorARMVIXL*>(codegen)->GetVIXLAssembler());
219 vixl32::Register base = sp;
220 if (stack_offset != 0) {
221 base = temps.Acquire();
Scott Wakelingb77051e2016-11-21 19:46:00 +0000222 __ Add(base, sp, Operand::From(stack_offset));
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100223 }
224 __ Vstm(F64, base, NO_WRITE_BACK, DRegisterList(d_reg, number_of_d_regs));
225 }
226 stack_offset += number_of_d_regs * kDRegSizeInBytes;
227 }
228
229 if (save_last) {
230 __ Vstr(vixl32::SRegister(last + 1), MemOperand(sp, stack_offset));
231 stack_offset += kSRegSizeInBytes;
232 }
233
234 return stack_offset;
235}
236
237static size_t RestoreContiguousSRegisterList(size_t first,
238 size_t last,
239 CodeGenerator* codegen,
240 size_t stack_offset) {
241 static_assert(kSRegSizeInBytes == kArmWordSize, "Broken assumption on reg/word sizes.");
242 static_assert(kDRegSizeInBytes == 2 * kArmWordSize, "Broken assumption on reg/word sizes.");
243 DCHECK_LE(first, last);
244 if ((first == last) && (first == 0)) {
245 __ Vldr(vixl32::SRegister(first), MemOperand(sp, stack_offset));
246 return stack_offset + kSRegSizeInBytes;
247 }
248 if (first % 2 == 1) {
249 __ Vldr(vixl32::SRegister(first++), MemOperand(sp, stack_offset));
250 stack_offset += kSRegSizeInBytes;
251 }
252
253 bool restore_last = false;
254 if (last % 2 == 0) {
255 restore_last = true;
256 --last;
257 }
258
259 if (first < last) {
260 vixl32::DRegister d_reg = vixl32::DRegister(first / 2);
261 DCHECK_EQ((last - first + 1) % 2, 0u);
262 size_t number_of_d_regs = (last - first + 1) / 2;
263 if (number_of_d_regs == 1) {
264 __ Vldr(d_reg, MemOperand(sp, stack_offset));
265 } else if (number_of_d_regs > 1) {
266 UseScratchRegisterScope temps(down_cast<CodeGeneratorARMVIXL*>(codegen)->GetVIXLAssembler());
267 vixl32::Register base = sp;
268 if (stack_offset != 0) {
269 base = temps.Acquire();
Scott Wakelingb77051e2016-11-21 19:46:00 +0000270 __ Add(base, sp, Operand::From(stack_offset));
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100271 }
272 __ Vldm(F64, base, NO_WRITE_BACK, DRegisterList(d_reg, number_of_d_regs));
273 }
274 stack_offset += number_of_d_regs * kDRegSizeInBytes;
275 }
276
277 if (restore_last) {
278 __ Vldr(vixl32::SRegister(last + 1), MemOperand(sp, stack_offset));
279 stack_offset += kSRegSizeInBytes;
280 }
281
282 return stack_offset;
283}
284
Vladimir Markod5d2f2c2017-09-26 12:37:26 +0100285static LoadOperandType GetLoadOperandType(DataType::Type type) {
286 switch (type) {
287 case DataType::Type::kReference:
288 return kLoadWord;
289 case DataType::Type::kBool:
290 case DataType::Type::kUint8:
291 return kLoadUnsignedByte;
292 case DataType::Type::kInt8:
293 return kLoadSignedByte;
294 case DataType::Type::kUint16:
295 return kLoadUnsignedHalfword;
296 case DataType::Type::kInt16:
297 return kLoadSignedHalfword;
298 case DataType::Type::kInt32:
299 return kLoadWord;
300 case DataType::Type::kInt64:
301 return kLoadWordPair;
302 case DataType::Type::kFloat32:
303 return kLoadSWord;
304 case DataType::Type::kFloat64:
305 return kLoadDWord;
306 default:
307 LOG(FATAL) << "Unreachable type " << type;
308 UNREACHABLE();
309 }
310}
311
312static StoreOperandType GetStoreOperandType(DataType::Type type) {
313 switch (type) {
314 case DataType::Type::kReference:
315 return kStoreWord;
316 case DataType::Type::kBool:
317 case DataType::Type::kUint8:
318 case DataType::Type::kInt8:
319 return kStoreByte;
320 case DataType::Type::kUint16:
321 case DataType::Type::kInt16:
322 return kStoreHalfword;
323 case DataType::Type::kInt32:
324 return kStoreWord;
325 case DataType::Type::kInt64:
326 return kStoreWordPair;
327 case DataType::Type::kFloat32:
328 return kStoreSWord;
329 case DataType::Type::kFloat64:
330 return kStoreDWord;
331 default:
332 LOG(FATAL) << "Unreachable type " << type;
333 UNREACHABLE();
334 }
335}
336
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100337void SlowPathCodeARMVIXL::SaveLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
338 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
339 size_t orig_offset = stack_offset;
340
341 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ true);
342 for (uint32_t i : LowToHighBits(core_spills)) {
343 // If the register holds an object, update the stack mask.
344 if (locations->RegisterContainsObject(i)) {
345 locations->SetStackBit(stack_offset / kVRegSize);
346 }
347 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
348 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
349 saved_core_stack_offsets_[i] = stack_offset;
350 stack_offset += kArmWordSize;
351 }
352
353 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
354 arm_codegen->GetAssembler()->StoreRegisterList(core_spills, orig_offset);
355
356 uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ false);
357 orig_offset = stack_offset;
358 for (uint32_t i : LowToHighBits(fp_spills)) {
359 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
360 saved_fpu_stack_offsets_[i] = stack_offset;
361 stack_offset += kArmWordSize;
362 }
363
364 stack_offset = orig_offset;
365 while (fp_spills != 0u) {
366 uint32_t begin = CTZ(fp_spills);
367 uint32_t tmp = fp_spills + (1u << begin);
368 fp_spills &= tmp; // Clear the contiguous range of 1s.
369 uint32_t end = (tmp == 0u) ? 32u : CTZ(tmp); // CTZ(0) is undefined.
370 stack_offset = SaveContiguousSRegisterList(begin, end - 1, codegen, stack_offset);
371 }
372 DCHECK_LE(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
373}
374
375void SlowPathCodeARMVIXL::RestoreLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
376 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
377 size_t orig_offset = stack_offset;
378
379 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ true);
380 for (uint32_t i : LowToHighBits(core_spills)) {
381 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
382 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
383 stack_offset += kArmWordSize;
384 }
385
386 // TODO(VIXL): Check the coherency of stack_offset after this with a test.
387 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
388 arm_codegen->GetAssembler()->LoadRegisterList(core_spills, orig_offset);
389
390 uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ false);
391 while (fp_spills != 0u) {
392 uint32_t begin = CTZ(fp_spills);
393 uint32_t tmp = fp_spills + (1u << begin);
394 fp_spills &= tmp; // Clear the contiguous range of 1s.
395 uint32_t end = (tmp == 0u) ? 32u : CTZ(tmp); // CTZ(0) is undefined.
396 stack_offset = RestoreContiguousSRegisterList(begin, end - 1, codegen, stack_offset);
397 }
398 DCHECK_LE(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
399}
400
401class NullCheckSlowPathARMVIXL : public SlowPathCodeARMVIXL {
402 public:
403 explicit NullCheckSlowPathARMVIXL(HNullCheck* instruction) : SlowPathCodeARMVIXL(instruction) {}
404
405 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
406 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
407 __ Bind(GetEntryLabel());
408 if (instruction_->CanThrowIntoCatchBlock()) {
409 // Live registers will be restored in the catch block if caught.
410 SaveLiveRegisters(codegen, instruction_->GetLocations());
411 }
412 arm_codegen->InvokeRuntime(kQuickThrowNullPointer,
413 instruction_,
414 instruction_->GetDexPc(),
415 this);
416 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
417 }
418
419 bool IsFatal() const OVERRIDE { return true; }
420
421 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathARMVIXL"; }
422
423 private:
424 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARMVIXL);
425};
426
Scott Wakelingfe885462016-09-22 10:24:38 +0100427class DivZeroCheckSlowPathARMVIXL : public SlowPathCodeARMVIXL {
428 public:
429 explicit DivZeroCheckSlowPathARMVIXL(HDivZeroCheck* instruction)
430 : SlowPathCodeARMVIXL(instruction) {}
431
432 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100433 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
Scott Wakelingfe885462016-09-22 10:24:38 +0100434 __ Bind(GetEntryLabel());
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100435 arm_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Scott Wakelingfe885462016-09-22 10:24:38 +0100436 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
437 }
438
439 bool IsFatal() const OVERRIDE { return true; }
440
441 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathARMVIXL"; }
442
443 private:
444 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARMVIXL);
445};
446
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100447class SuspendCheckSlowPathARMVIXL : public SlowPathCodeARMVIXL {
448 public:
449 SuspendCheckSlowPathARMVIXL(HSuspendCheck* instruction, HBasicBlock* successor)
450 : SlowPathCodeARMVIXL(instruction), successor_(successor) {}
451
452 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
453 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
454 __ Bind(GetEntryLabel());
455 arm_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
456 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
457 if (successor_ == nullptr) {
458 __ B(GetReturnLabel());
459 } else {
460 __ B(arm_codegen->GetLabelOf(successor_));
461 }
462 }
463
464 vixl32::Label* GetReturnLabel() {
465 DCHECK(successor_ == nullptr);
466 return &return_label_;
467 }
468
469 HBasicBlock* GetSuccessor() const {
470 return successor_;
471 }
472
473 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathARMVIXL"; }
474
475 private:
476 // If not null, the block to branch to after the suspend check.
477 HBasicBlock* const successor_;
478
479 // If `successor_` is null, the label to branch to after the suspend check.
480 vixl32::Label return_label_;
481
482 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARMVIXL);
483};
484
Scott Wakelingc34dba72016-10-03 10:14:44 +0100485class BoundsCheckSlowPathARMVIXL : public SlowPathCodeARMVIXL {
486 public:
487 explicit BoundsCheckSlowPathARMVIXL(HBoundsCheck* instruction)
488 : SlowPathCodeARMVIXL(instruction) {}
489
490 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
491 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
492 LocationSummary* locations = instruction_->GetLocations();
493
494 __ Bind(GetEntryLabel());
495 if (instruction_->CanThrowIntoCatchBlock()) {
496 // Live registers will be restored in the catch block if caught.
497 SaveLiveRegisters(codegen, instruction_->GetLocations());
498 }
499 // We're moving two locations to locations that could overlap, so we need a parallel
500 // move resolver.
501 InvokeRuntimeCallingConventionARMVIXL calling_convention;
502 codegen->EmitParallelMoves(
503 locations->InAt(0),
504 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100505 DataType::Type::kInt32,
Scott Wakelingc34dba72016-10-03 10:14:44 +0100506 locations->InAt(1),
507 LocationFrom(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100508 DataType::Type::kInt32);
Scott Wakelingc34dba72016-10-03 10:14:44 +0100509 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
510 ? kQuickThrowStringBounds
511 : kQuickThrowArrayBounds;
512 arm_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
513 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
514 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
515 }
516
517 bool IsFatal() const OVERRIDE { return true; }
518
519 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathARMVIXL"; }
520
521 private:
522 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARMVIXL);
523};
524
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100525class LoadClassSlowPathARMVIXL : public SlowPathCodeARMVIXL {
526 public:
527 LoadClassSlowPathARMVIXL(HLoadClass* cls, HInstruction* at, uint32_t dex_pc, bool do_clinit)
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000528 : SlowPathCodeARMVIXL(at), cls_(cls), dex_pc_(dex_pc), do_clinit_(do_clinit) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100529 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
530 }
531
532 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000533 LocationSummary* locations = instruction_->GetLocations();
Vladimir Markoea4c1262017-02-06 19:59:33 +0000534 Location out = locations->Out();
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100535
536 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
537 __ Bind(GetEntryLabel());
538 SaveLiveRegisters(codegen, locations);
539
540 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000541 dex::TypeIndex type_index = cls_->GetTypeIndex();
542 __ Mov(calling_convention.GetRegisterAt(0), type_index.index_);
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100543 QuickEntrypointEnum entrypoint = do_clinit_ ? kQuickInitializeStaticStorage
544 : kQuickInitializeType;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000545 arm_codegen->InvokeRuntime(entrypoint, instruction_, dex_pc_, this);
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100546 if (do_clinit_) {
547 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
548 } else {
549 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
550 }
551
552 // Move the class to the desired location.
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100553 if (out.IsValid()) {
554 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
555 arm_codegen->Move32(locations->Out(), LocationFrom(r0));
556 }
557 RestoreLiveRegisters(codegen, locations);
558 __ B(GetExitLabel());
559 }
560
561 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathARMVIXL"; }
562
563 private:
564 // The class this slow path will load.
565 HLoadClass* const cls_;
566
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100567 // The dex PC of `at_`.
568 const uint32_t dex_pc_;
569
570 // Whether to initialize the class.
571 const bool do_clinit_;
572
573 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARMVIXL);
574};
575
Artem Serovd4cc5b22016-11-04 11:19:09 +0000576class LoadStringSlowPathARMVIXL : public SlowPathCodeARMVIXL {
577 public:
578 explicit LoadStringSlowPathARMVIXL(HLoadString* instruction)
579 : SlowPathCodeARMVIXL(instruction) {}
580
581 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Vladimir Markoea4c1262017-02-06 19:59:33 +0000582 DCHECK(instruction_->IsLoadString());
583 DCHECK_EQ(instruction_->AsLoadString()->GetLoadKind(), HLoadString::LoadKind::kBssEntry);
Artem Serovd4cc5b22016-11-04 11:19:09 +0000584 LocationSummary* locations = instruction_->GetLocations();
585 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Vladimir Markof3c52b42017-11-17 17:32:12 +0000586 const dex::StringIndex string_index = instruction_->AsLoadString()->GetStringIndex();
Artem Serovd4cc5b22016-11-04 11:19:09 +0000587
588 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
589 __ Bind(GetEntryLabel());
590 SaveLiveRegisters(codegen, locations);
591
592 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000593 __ Mov(calling_convention.GetRegisterAt(0), string_index.index_);
Artem Serovd4cc5b22016-11-04 11:19:09 +0000594 arm_codegen->InvokeRuntime(kQuickResolveString, instruction_, instruction_->GetDexPc(), this);
595 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
596
Artem Serovd4cc5b22016-11-04 11:19:09 +0000597 arm_codegen->Move32(locations->Out(), LocationFrom(r0));
598 RestoreLiveRegisters(codegen, locations);
599
600 __ B(GetExitLabel());
601 }
602
603 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathARMVIXL"; }
604
605 private:
606 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARMVIXL);
607};
608
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100609class TypeCheckSlowPathARMVIXL : public SlowPathCodeARMVIXL {
610 public:
611 TypeCheckSlowPathARMVIXL(HInstruction* instruction, bool is_fatal)
612 : SlowPathCodeARMVIXL(instruction), is_fatal_(is_fatal) {}
613
614 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
615 LocationSummary* locations = instruction_->GetLocations();
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100616 DCHECK(instruction_->IsCheckCast()
617 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
618
619 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
620 __ Bind(GetEntryLabel());
621
Vladimir Marko87584542017-12-12 17:47:52 +0000622 if (!is_fatal_ || instruction_->CanThrowIntoCatchBlock()) {
Artem Serovcfbe9132016-10-14 15:58:56 +0100623 SaveLiveRegisters(codegen, locations);
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100624 }
625
626 // We're moving two locations to locations that could overlap, so we need a parallel
627 // move resolver.
628 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100629
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800630 codegen->EmitParallelMoves(locations->InAt(0),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800631 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100632 DataType::Type::kReference,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800633 locations->InAt(1),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800634 LocationFrom(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100635 DataType::Type::kReference);
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100636 if (instruction_->IsInstanceOf()) {
Artem Serovcfbe9132016-10-14 15:58:56 +0100637 arm_codegen->InvokeRuntime(kQuickInstanceofNonTrivial,
638 instruction_,
639 instruction_->GetDexPc(),
640 this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800641 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Artem Serovcfbe9132016-10-14 15:58:56 +0100642 arm_codegen->Move32(locations->Out(), LocationFrom(r0));
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100643 } else {
644 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800645 arm_codegen->InvokeRuntime(kQuickCheckInstanceOf,
646 instruction_,
647 instruction_->GetDexPc(),
648 this);
649 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100650 }
651
652 if (!is_fatal_) {
Artem Serovcfbe9132016-10-14 15:58:56 +0100653 RestoreLiveRegisters(codegen, locations);
654 __ B(GetExitLabel());
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100655 }
656 }
657
658 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathARMVIXL"; }
659
660 bool IsFatal() const OVERRIDE { return is_fatal_; }
661
662 private:
663 const bool is_fatal_;
664
665 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARMVIXL);
666};
667
Scott Wakelingc34dba72016-10-03 10:14:44 +0100668class DeoptimizationSlowPathARMVIXL : public SlowPathCodeARMVIXL {
669 public:
670 explicit DeoptimizationSlowPathARMVIXL(HDeoptimize* instruction)
671 : SlowPathCodeARMVIXL(instruction) {}
672
673 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
674 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
675 __ Bind(GetEntryLabel());
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100676 LocationSummary* locations = instruction_->GetLocations();
677 SaveLiveRegisters(codegen, locations);
678 InvokeRuntimeCallingConventionARMVIXL calling_convention;
679 __ Mov(calling_convention.GetRegisterAt(0),
680 static_cast<uint32_t>(instruction_->AsDeoptimize()->GetDeoptimizationKind()));
681
Scott Wakelingc34dba72016-10-03 10:14:44 +0100682 arm_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100683 CheckEntrypointTypes<kQuickDeoptimize, void, DeoptimizationKind>();
Scott Wakelingc34dba72016-10-03 10:14:44 +0100684 }
685
686 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathARMVIXL"; }
687
688 private:
689 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARMVIXL);
690};
691
692class ArraySetSlowPathARMVIXL : public SlowPathCodeARMVIXL {
693 public:
694 explicit ArraySetSlowPathARMVIXL(HInstruction* instruction) : SlowPathCodeARMVIXL(instruction) {}
695
696 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
697 LocationSummary* locations = instruction_->GetLocations();
698 __ Bind(GetEntryLabel());
699 SaveLiveRegisters(codegen, locations);
700
701 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100702 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Scott Wakelingc34dba72016-10-03 10:14:44 +0100703 parallel_move.AddMove(
704 locations->InAt(0),
705 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100706 DataType::Type::kReference,
Scott Wakelingc34dba72016-10-03 10:14:44 +0100707 nullptr);
708 parallel_move.AddMove(
709 locations->InAt(1),
710 LocationFrom(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100711 DataType::Type::kInt32,
Scott Wakelingc34dba72016-10-03 10:14:44 +0100712 nullptr);
713 parallel_move.AddMove(
714 locations->InAt(2),
715 LocationFrom(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100716 DataType::Type::kReference,
Scott Wakelingc34dba72016-10-03 10:14:44 +0100717 nullptr);
718 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
719
720 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
721 arm_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
722 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
723 RestoreLiveRegisters(codegen, locations);
724 __ B(GetExitLabel());
725 }
726
727 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathARMVIXL"; }
728
729 private:
730 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathARMVIXL);
731};
732
Roland Levillain54f869e2017-03-06 13:54:11 +0000733// Abstract base class for read barrier slow paths marking a reference
734// `ref`.
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000735//
Roland Levillain54f869e2017-03-06 13:54:11 +0000736// Argument `entrypoint` must be a register location holding the read
Roland Levillain6d729a72017-06-30 18:34:01 +0100737// barrier marking runtime entry point to be invoked or an empty
738// location; in the latter case, the read barrier marking runtime
739// entry point will be loaded by the slow path code itself.
Roland Levillain54f869e2017-03-06 13:54:11 +0000740class ReadBarrierMarkSlowPathBaseARMVIXL : public SlowPathCodeARMVIXL {
741 protected:
742 ReadBarrierMarkSlowPathBaseARMVIXL(HInstruction* instruction, Location ref, Location entrypoint)
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000743 : SlowPathCodeARMVIXL(instruction), ref_(ref), entrypoint_(entrypoint) {
744 DCHECK(kEmitCompilerReadBarrier);
745 }
746
Roland Levillain54f869e2017-03-06 13:54:11 +0000747 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathBaseARMVIXL"; }
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000748
Roland Levillain54f869e2017-03-06 13:54:11 +0000749 // Generate assembly code calling the read barrier marking runtime
750 // entry point (ReadBarrierMarkRegX).
751 void GenerateReadBarrierMarkRuntimeCall(CodeGenerator* codegen) {
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000752 vixl32::Register ref_reg = RegisterFrom(ref_);
Roland Levillain47b3ab22017-02-27 14:31:35 +0000753
Roland Levillain47b3ab22017-02-27 14:31:35 +0000754 // No need to save live registers; it's taken care of by the
755 // entrypoint. Also, there is no need to update the stack mask,
756 // as this runtime call will not trigger a garbage collection.
757 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
758 DCHECK(!ref_reg.Is(sp));
759 DCHECK(!ref_reg.Is(lr));
760 DCHECK(!ref_reg.Is(pc));
761 // IP is used internally by the ReadBarrierMarkRegX entry point
762 // as a temporary, it cannot be the entry point's input/output.
763 DCHECK(!ref_reg.Is(ip));
764 DCHECK(ref_reg.IsRegister()) << ref_reg;
765 // "Compact" slow path, saving two moves.
766 //
767 // Instead of using the standard runtime calling convention (input
768 // and output in R0):
769 //
770 // R0 <- ref
771 // R0 <- ReadBarrierMark(R0)
772 // ref <- R0
773 //
774 // we just use rX (the register containing `ref`) as input and output
775 // of a dedicated entrypoint:
776 //
777 // rX <- ReadBarrierMarkRegX(rX)
778 //
779 if (entrypoint_.IsValid()) {
780 arm_codegen->ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction_, this);
781 __ Blx(RegisterFrom(entrypoint_));
782 } else {
Roland Levillain54f869e2017-03-06 13:54:11 +0000783 // Entrypoint is not already loaded, load from the thread.
Roland Levillain47b3ab22017-02-27 14:31:35 +0000784 int32_t entry_point_offset =
Roland Levillain97c46462017-05-11 14:04:03 +0100785 Thread::ReadBarrierMarkEntryPointsOffset<kArmPointerSize>(ref_reg.GetCode());
Roland Levillain47b3ab22017-02-27 14:31:35 +0000786 // This runtime call does not require a stack map.
787 arm_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
788 }
Roland Levillain47b3ab22017-02-27 14:31:35 +0000789 }
790
Roland Levillain47b3ab22017-02-27 14:31:35 +0000791 // The location (register) of the marked object reference.
792 const Location ref_;
793
794 // The location of the entrypoint if already loaded.
795 const Location entrypoint_;
796
Roland Levillain54f869e2017-03-06 13:54:11 +0000797 private:
798 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathBaseARMVIXL);
Roland Levillain47b3ab22017-02-27 14:31:35 +0000799};
800
Scott Wakelingc34dba72016-10-03 10:14:44 +0100801// Slow path marking an object reference `ref` during a read
802// barrier. The field `obj.field` in the object `obj` holding this
Roland Levillain54f869e2017-03-06 13:54:11 +0000803// reference does not get updated by this slow path after marking.
Roland Levillain47b3ab22017-02-27 14:31:35 +0000804//
Scott Wakelingc34dba72016-10-03 10:14:44 +0100805// This means that after the execution of this slow path, `ref` will
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000806// always be up-to-date, but `obj.field` may not; i.e., after the
807// flip, `ref` will be a to-space reference, but `obj.field` will
808// probably still be a from-space reference (unless it gets updated by
809// another thread, or if another thread installed another object
810// reference (different from `ref`) in `obj.field`).
Roland Levillainba650a42017-03-06 13:52:32 +0000811//
Roland Levillain6d729a72017-06-30 18:34:01 +0100812// Argument `entrypoint` must be a register location holding the read
813// barrier marking runtime entry point to be invoked or an empty
814// location; in the latter case, the read barrier marking runtime
815// entry point will be loaded by the slow path code itself.
Roland Levillain54f869e2017-03-06 13:54:11 +0000816class ReadBarrierMarkSlowPathARMVIXL : public ReadBarrierMarkSlowPathBaseARMVIXL {
Roland Levillain47b3ab22017-02-27 14:31:35 +0000817 public:
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000818 ReadBarrierMarkSlowPathARMVIXL(HInstruction* instruction,
819 Location ref,
820 Location entrypoint = Location::NoLocation())
Roland Levillain54f869e2017-03-06 13:54:11 +0000821 : ReadBarrierMarkSlowPathBaseARMVIXL(instruction, ref, entrypoint) {
Roland Levillain47b3ab22017-02-27 14:31:35 +0000822 DCHECK(kEmitCompilerReadBarrier);
823 }
824
Roland Levillain47b3ab22017-02-27 14:31:35 +0000825 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathARMVIXL"; }
826
827 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
828 LocationSummary* locations = instruction_->GetLocations();
Roland Levillain54f869e2017-03-06 13:54:11 +0000829 DCHECK(locations->CanCall());
830 DCHECK(ref_.IsRegister()) << ref_;
831 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_.reg())) << ref_.reg();
832 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
833 << "Unexpected instruction in read barrier marking slow path: "
834 << instruction_->DebugName();
835
836 __ Bind(GetEntryLabel());
837 GenerateReadBarrierMarkRuntimeCall(codegen);
838 __ B(GetExitLabel());
839 }
840
841 private:
842 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathARMVIXL);
843};
844
845// Slow path loading `obj`'s lock word, loading a reference from
846// object `*(obj + offset + (index << scale_factor))` into `ref`, and
847// marking `ref` if `obj` is gray according to the lock word (Baker
848// read barrier). The field `obj.field` in the object `obj` holding
849// this reference does not get updated by this slow path after marking
850// (see LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARMVIXL
851// below for that).
852//
853// This means that after the execution of this slow path, `ref` will
854// always be up-to-date, but `obj.field` may not; i.e., after the
855// flip, `ref` will be a to-space reference, but `obj.field` will
856// probably still be a from-space reference (unless it gets updated by
857// another thread, or if another thread installed another object
858// reference (different from `ref`) in `obj.field`).
859//
860// Argument `entrypoint` must be a register location holding the read
Roland Levillain6d729a72017-06-30 18:34:01 +0100861// barrier marking runtime entry point to be invoked or an empty
862// location; in the latter case, the read barrier marking runtime
863// entry point will be loaded by the slow path code itself.
Roland Levillain54f869e2017-03-06 13:54:11 +0000864class LoadReferenceWithBakerReadBarrierSlowPathARMVIXL : public ReadBarrierMarkSlowPathBaseARMVIXL {
865 public:
866 LoadReferenceWithBakerReadBarrierSlowPathARMVIXL(HInstruction* instruction,
867 Location ref,
868 vixl32::Register obj,
869 uint32_t offset,
870 Location index,
871 ScaleFactor scale_factor,
872 bool needs_null_check,
873 vixl32::Register temp,
Roland Levillain6d729a72017-06-30 18:34:01 +0100874 Location entrypoint = Location::NoLocation())
Roland Levillain54f869e2017-03-06 13:54:11 +0000875 : ReadBarrierMarkSlowPathBaseARMVIXL(instruction, ref, entrypoint),
876 obj_(obj),
877 offset_(offset),
878 index_(index),
879 scale_factor_(scale_factor),
880 needs_null_check_(needs_null_check),
881 temp_(temp) {
882 DCHECK(kEmitCompilerReadBarrier);
883 DCHECK(kUseBakerReadBarrier);
884 }
885
Roland Levillain47b3ab22017-02-27 14:31:35 +0000886 const char* GetDescription() const OVERRIDE {
Roland Levillain54f869e2017-03-06 13:54:11 +0000887 return "LoadReferenceWithBakerReadBarrierSlowPathARMVIXL";
Roland Levillain47b3ab22017-02-27 14:31:35 +0000888 }
889
890 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
891 LocationSummary* locations = instruction_->GetLocations();
892 vixl32::Register ref_reg = RegisterFrom(ref_);
893 DCHECK(locations->CanCall());
894 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg.GetCode())) << ref_reg;
Roland Levillain47b3ab22017-02-27 14:31:35 +0000895 DCHECK(instruction_->IsInstanceFieldGet() ||
896 instruction_->IsStaticFieldGet() ||
897 instruction_->IsArrayGet() ||
898 instruction_->IsArraySet() ||
Roland Levillain47b3ab22017-02-27 14:31:35 +0000899 instruction_->IsInstanceOf() ||
900 instruction_->IsCheckCast() ||
901 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
902 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
903 << "Unexpected instruction in read barrier marking slow path: "
904 << instruction_->DebugName();
905 // The read barrier instrumentation of object ArrayGet
906 // instructions does not support the HIntermediateAddress
907 // instruction.
908 DCHECK(!(instruction_->IsArrayGet() &&
909 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
910
Roland Levillain54f869e2017-03-06 13:54:11 +0000911 // Temporary register `temp_`, used to store the lock word, must
912 // not be IP, as we may use it to emit the reference load (in the
913 // call to GenerateRawReferenceLoad below), and we need the lock
914 // word to still be in `temp_` after the reference load.
915 DCHECK(!temp_.Is(ip));
916
Roland Levillain47b3ab22017-02-27 14:31:35 +0000917 __ Bind(GetEntryLabel());
Roland Levillain54f869e2017-03-06 13:54:11 +0000918
919 // When using MaybeGenerateReadBarrierSlow, the read barrier call is
920 // inserted after the original load. However, in fast path based
921 // Baker's read barriers, we need to perform the load of
922 // mirror::Object::monitor_ *before* the original reference load.
923 // This load-load ordering is required by the read barrier.
Roland Levillainff487002017-03-07 16:50:01 +0000924 // The slow path (for Baker's algorithm) should look like:
Roland Levillain54f869e2017-03-06 13:54:11 +0000925 //
926 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
927 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
928 // HeapReference<mirror::Object> ref = *src; // Original reference load.
929 // bool is_gray = (rb_state == ReadBarrier::GrayState());
930 // if (is_gray) {
931 // ref = entrypoint(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
932 // }
933 //
934 // Note: the original implementation in ReadBarrier::Barrier is
935 // slightly more complex as it performs additional checks that we do
936 // not do here for performance reasons.
937
Roland Levillain47b3ab22017-02-27 14:31:35 +0000938 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
Roland Levillain54f869e2017-03-06 13:54:11 +0000939
940 // /* int32_t */ monitor = obj->monitor_
941 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
942 arm_codegen->GetAssembler()->LoadFromOffset(kLoadWord, temp_, obj_, monitor_offset);
943 if (needs_null_check_) {
944 codegen->MaybeRecordImplicitNullCheck(instruction_);
Roland Levillain47b3ab22017-02-27 14:31:35 +0000945 }
Roland Levillain54f869e2017-03-06 13:54:11 +0000946 // /* LockWord */ lock_word = LockWord(monitor)
947 static_assert(sizeof(LockWord) == sizeof(int32_t),
948 "art::LockWord and int32_t have different sizes.");
949
950 // Introduce a dependency on the lock_word including the rb_state,
951 // which shall prevent load-load reordering without using
952 // a memory barrier (which would be more expensive).
953 // `obj` is unchanged by this operation, but its value now depends
954 // on `temp`.
955 __ Add(obj_, obj_, Operand(temp_, ShiftType::LSR, 32));
956
957 // The actual reference load.
958 // A possible implicit null check has already been handled above.
959 arm_codegen->GenerateRawReferenceLoad(
960 instruction_, ref_, obj_, offset_, index_, scale_factor_, /* needs_null_check */ false);
961
962 // Mark the object `ref` when `obj` is gray.
963 //
964 // if (rb_state == ReadBarrier::GrayState())
965 // ref = ReadBarrier::Mark(ref);
966 //
967 // Given the numeric representation, it's enough to check the low bit of the
968 // rb_state. We do that by shifting the bit out of the lock word with LSRS
969 // which can be a 16-bit instruction unlike the TST immediate.
970 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
971 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
972 __ Lsrs(temp_, temp_, LockWord::kReadBarrierStateShift + 1);
973 __ B(cc, GetExitLabel()); // Carry flag is the last bit shifted out by LSRS.
974 GenerateReadBarrierMarkRuntimeCall(codegen);
975
Roland Levillain47b3ab22017-02-27 14:31:35 +0000976 __ B(GetExitLabel());
977 }
978
979 private:
Roland Levillain54f869e2017-03-06 13:54:11 +0000980 // The register containing the object holding the marked object reference field.
981 vixl32::Register obj_;
982 // The offset, index and scale factor to access the reference in `obj_`.
983 uint32_t offset_;
984 Location index_;
985 ScaleFactor scale_factor_;
986 // Is a null check required?
987 bool needs_null_check_;
988 // A temporary register used to hold the lock word of `obj_`.
989 vixl32::Register temp_;
Roland Levillain47b3ab22017-02-27 14:31:35 +0000990
Roland Levillain54f869e2017-03-06 13:54:11 +0000991 DISALLOW_COPY_AND_ASSIGN(LoadReferenceWithBakerReadBarrierSlowPathARMVIXL);
Roland Levillain47b3ab22017-02-27 14:31:35 +0000992};
993
Roland Levillain54f869e2017-03-06 13:54:11 +0000994// Slow path loading `obj`'s lock word, loading a reference from
995// object `*(obj + offset + (index << scale_factor))` into `ref`, and
996// marking `ref` if `obj` is gray according to the lock word (Baker
997// read barrier). If needed, this slow path also atomically updates
998// the field `obj.field` in the object `obj` holding this reference
999// after marking (contrary to
1000// LoadReferenceWithBakerReadBarrierSlowPathARMVIXL above, which never
1001// tries to update `obj.field`).
Roland Levillain47b3ab22017-02-27 14:31:35 +00001002//
1003// This means that after the execution of this slow path, both `ref`
1004// and `obj.field` will be up-to-date; i.e., after the flip, both will
1005// hold the same to-space reference (unless another thread installed
1006// another object reference (different from `ref`) in `obj.field`).
Roland Levillainba650a42017-03-06 13:52:32 +00001007//
Roland Levillain54f869e2017-03-06 13:54:11 +00001008// Argument `entrypoint` must be a register location holding the read
Roland Levillain6d729a72017-06-30 18:34:01 +01001009// barrier marking runtime entry point to be invoked or an empty
1010// location; in the latter case, the read barrier marking runtime
1011// entry point will be loaded by the slow path code itself.
Roland Levillain54f869e2017-03-06 13:54:11 +00001012class LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARMVIXL
1013 : public ReadBarrierMarkSlowPathBaseARMVIXL {
Roland Levillain47b3ab22017-02-27 14:31:35 +00001014 public:
Roland Levillain6d729a72017-06-30 18:34:01 +01001015 LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARMVIXL(
1016 HInstruction* instruction,
1017 Location ref,
1018 vixl32::Register obj,
1019 uint32_t offset,
1020 Location index,
1021 ScaleFactor scale_factor,
1022 bool needs_null_check,
1023 vixl32::Register temp1,
1024 vixl32::Register temp2,
1025 Location entrypoint = Location::NoLocation())
Roland Levillain54f869e2017-03-06 13:54:11 +00001026 : ReadBarrierMarkSlowPathBaseARMVIXL(instruction, ref, entrypoint),
Roland Levillain47b3ab22017-02-27 14:31:35 +00001027 obj_(obj),
Roland Levillain54f869e2017-03-06 13:54:11 +00001028 offset_(offset),
1029 index_(index),
1030 scale_factor_(scale_factor),
1031 needs_null_check_(needs_null_check),
Roland Levillain47b3ab22017-02-27 14:31:35 +00001032 temp1_(temp1),
Roland Levillain54f869e2017-03-06 13:54:11 +00001033 temp2_(temp2) {
Roland Levillain47b3ab22017-02-27 14:31:35 +00001034 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain54f869e2017-03-06 13:54:11 +00001035 DCHECK(kUseBakerReadBarrier);
Roland Levillain47b3ab22017-02-27 14:31:35 +00001036 }
1037
1038 const char* GetDescription() const OVERRIDE {
Roland Levillain54f869e2017-03-06 13:54:11 +00001039 return "LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARMVIXL";
Roland Levillain47b3ab22017-02-27 14:31:35 +00001040 }
1041
1042 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
1043 LocationSummary* locations = instruction_->GetLocations();
1044 vixl32::Register ref_reg = RegisterFrom(ref_);
1045 DCHECK(locations->CanCall());
1046 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg.GetCode())) << ref_reg;
Roland Levillain54f869e2017-03-06 13:54:11 +00001047 DCHECK_NE(ref_.reg(), LocationFrom(temp1_).reg());
1048
1049 // This slow path is only used by the UnsafeCASObject intrinsic at the moment.
Roland Levillain47b3ab22017-02-27 14:31:35 +00001050 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
1051 << "Unexpected instruction in read barrier marking and field updating slow path: "
1052 << instruction_->DebugName();
1053 DCHECK(instruction_->GetLocations()->Intrinsified());
1054 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
Roland Levillain54f869e2017-03-06 13:54:11 +00001055 DCHECK_EQ(offset_, 0u);
1056 DCHECK_EQ(scale_factor_, ScaleFactor::TIMES_1);
1057 Location field_offset = index_;
1058 DCHECK(field_offset.IsRegisterPair()) << field_offset;
1059
1060 // Temporary register `temp1_`, used to store the lock word, must
1061 // not be IP, as we may use it to emit the reference load (in the
1062 // call to GenerateRawReferenceLoad below), and we need the lock
1063 // word to still be in `temp1_` after the reference load.
1064 DCHECK(!temp1_.Is(ip));
Roland Levillain47b3ab22017-02-27 14:31:35 +00001065
1066 __ Bind(GetEntryLabel());
1067
Roland Levillainff487002017-03-07 16:50:01 +00001068 // The implementation is similar to LoadReferenceWithBakerReadBarrierSlowPathARMVIXL's:
1069 //
1070 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
1071 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
1072 // HeapReference<mirror::Object> ref = *src; // Original reference load.
1073 // bool is_gray = (rb_state == ReadBarrier::GrayState());
1074 // if (is_gray) {
1075 // old_ref = ref;
1076 // ref = entrypoint(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
1077 // compareAndSwapObject(obj, field_offset, old_ref, ref);
1078 // }
1079
Roland Levillain54f869e2017-03-06 13:54:11 +00001080 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
1081
1082 // /* int32_t */ monitor = obj->monitor_
1083 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
1084 arm_codegen->GetAssembler()->LoadFromOffset(kLoadWord, temp1_, obj_, monitor_offset);
1085 if (needs_null_check_) {
1086 codegen->MaybeRecordImplicitNullCheck(instruction_);
1087 }
1088 // /* LockWord */ lock_word = LockWord(monitor)
1089 static_assert(sizeof(LockWord) == sizeof(int32_t),
1090 "art::LockWord and int32_t have different sizes.");
1091
1092 // Introduce a dependency on the lock_word including the rb_state,
1093 // which shall prevent load-load reordering without using
1094 // a memory barrier (which would be more expensive).
1095 // `obj` is unchanged by this operation, but its value now depends
1096 // on `temp`.
1097 __ Add(obj_, obj_, Operand(temp1_, ShiftType::LSR, 32));
1098
1099 // The actual reference load.
1100 // A possible implicit null check has already been handled above.
1101 arm_codegen->GenerateRawReferenceLoad(
1102 instruction_, ref_, obj_, offset_, index_, scale_factor_, /* needs_null_check */ false);
1103
1104 // Mark the object `ref` when `obj` is gray.
1105 //
1106 // if (rb_state == ReadBarrier::GrayState())
1107 // ref = ReadBarrier::Mark(ref);
1108 //
1109 // Given the numeric representation, it's enough to check the low bit of the
1110 // rb_state. We do that by shifting the bit out of the lock word with LSRS
1111 // which can be a 16-bit instruction unlike the TST immediate.
1112 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
1113 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
1114 __ Lsrs(temp1_, temp1_, LockWord::kReadBarrierStateShift + 1);
1115 __ B(cc, GetExitLabel()); // Carry flag is the last bit shifted out by LSRS.
1116
1117 // Save the old value of the reference before marking it.
Roland Levillain47b3ab22017-02-27 14:31:35 +00001118 // Note that we cannot use IP to save the old reference, as IP is
1119 // used internally by the ReadBarrierMarkRegX entry point, and we
1120 // need the old reference after the call to that entry point.
1121 DCHECK(!temp1_.Is(ip));
1122 __ Mov(temp1_, ref_reg);
Roland Levillain27b1f9c2017-01-17 16:56:34 +00001123
Roland Levillain54f869e2017-03-06 13:54:11 +00001124 GenerateReadBarrierMarkRuntimeCall(codegen);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001125
1126 // If the new reference is different from the old reference,
Roland Levillain54f869e2017-03-06 13:54:11 +00001127 // update the field in the holder (`*(obj_ + field_offset)`).
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001128 //
1129 // Note that this field could also hold a different object, if
1130 // another thread had concurrently changed it. In that case, the
Anton Kirilov349e61f2017-12-15 17:11:33 +00001131 // LDREX/CMP/BNE sequence of instructions in the compare-and-set
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001132 // (CAS) operation below would abort the CAS, leaving the field
1133 // as-is.
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001134 __ Cmp(temp1_, ref_reg);
Roland Levillain54f869e2017-03-06 13:54:11 +00001135 __ B(eq, GetExitLabel());
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001136
1137 // Update the the holder's field atomically. This may fail if
1138 // mutator updates before us, but it's OK. This is achieved
1139 // using a strong compare-and-set (CAS) operation with relaxed
1140 // memory synchronization ordering, where the expected value is
1141 // the old reference and the desired value is the new reference.
1142
1143 UseScratchRegisterScope temps(arm_codegen->GetVIXLAssembler());
1144 // Convenience aliases.
1145 vixl32::Register base = obj_;
1146 // The UnsafeCASObject intrinsic uses a register pair as field
1147 // offset ("long offset"), of which only the low part contains
1148 // data.
Roland Levillain54f869e2017-03-06 13:54:11 +00001149 vixl32::Register offset = LowRegisterFrom(field_offset);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001150 vixl32::Register expected = temp1_;
1151 vixl32::Register value = ref_reg;
1152 vixl32::Register tmp_ptr = temps.Acquire(); // Pointer to actual memory.
1153 vixl32::Register tmp = temp2_; // Value in memory.
1154
1155 __ Add(tmp_ptr, base, offset);
1156
1157 if (kPoisonHeapReferences) {
1158 arm_codegen->GetAssembler()->PoisonHeapReference(expected);
1159 if (value.Is(expected)) {
1160 // Do not poison `value`, as it is the same register as
1161 // `expected`, which has just been poisoned.
1162 } else {
1163 arm_codegen->GetAssembler()->PoisonHeapReference(value);
1164 }
1165 }
1166
1167 // do {
1168 // tmp = [r_ptr] - expected;
1169 // } while (tmp == 0 && failure([r_ptr] <- r_new_value));
1170
Anton Kirilov349e61f2017-12-15 17:11:33 +00001171 vixl32::Label loop_head, comparison_failed, exit_loop;
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001172 __ Bind(&loop_head);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001173 __ Ldrex(tmp, MemOperand(tmp_ptr));
Anton Kirilov349e61f2017-12-15 17:11:33 +00001174 __ Cmp(tmp, expected);
1175 __ B(ne, &comparison_failed, /* far_target */ false);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001176 __ Strex(tmp, value, MemOperand(tmp_ptr));
Anton Kirilov349e61f2017-12-15 17:11:33 +00001177 __ CompareAndBranchIfZero(tmp, &exit_loop, /* far_target */ false);
1178 __ B(&loop_head);
1179 __ Bind(&comparison_failed);
1180 __ Clrex();
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001181 __ Bind(&exit_loop);
1182
1183 if (kPoisonHeapReferences) {
1184 arm_codegen->GetAssembler()->UnpoisonHeapReference(expected);
1185 if (value.Is(expected)) {
1186 // Do not unpoison `value`, as it is the same register as
1187 // `expected`, which has just been unpoisoned.
1188 } else {
1189 arm_codegen->GetAssembler()->UnpoisonHeapReference(value);
1190 }
1191 }
1192
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001193 __ B(GetExitLabel());
1194 }
1195
1196 private:
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001197 // The register containing the object holding the marked object reference field.
1198 const vixl32::Register obj_;
Roland Levillain54f869e2017-03-06 13:54:11 +00001199 // The offset, index and scale factor to access the reference in `obj_`.
1200 uint32_t offset_;
1201 Location index_;
1202 ScaleFactor scale_factor_;
1203 // Is a null check required?
1204 bool needs_null_check_;
1205 // A temporary register used to hold the lock word of `obj_`; and
1206 // also to hold the original reference value, when the reference is
1207 // marked.
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001208 const vixl32::Register temp1_;
Roland Levillain54f869e2017-03-06 13:54:11 +00001209 // A temporary register used in the implementation of the CAS, to
1210 // update the object's reference field.
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001211 const vixl32::Register temp2_;
1212
Roland Levillain54f869e2017-03-06 13:54:11 +00001213 DISALLOW_COPY_AND_ASSIGN(LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARMVIXL);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001214};
1215
1216// Slow path generating a read barrier for a heap reference.
1217class ReadBarrierForHeapReferenceSlowPathARMVIXL : public SlowPathCodeARMVIXL {
1218 public:
1219 ReadBarrierForHeapReferenceSlowPathARMVIXL(HInstruction* instruction,
1220 Location out,
1221 Location ref,
1222 Location obj,
1223 uint32_t offset,
1224 Location index)
1225 : SlowPathCodeARMVIXL(instruction),
1226 out_(out),
1227 ref_(ref),
1228 obj_(obj),
1229 offset_(offset),
1230 index_(index) {
1231 DCHECK(kEmitCompilerReadBarrier);
1232 // If `obj` is equal to `out` or `ref`, it means the initial object
1233 // has been overwritten by (or after) the heap object reference load
1234 // to be instrumented, e.g.:
1235 //
1236 // __ LoadFromOffset(kLoadWord, out, out, offset);
1237 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
1238 //
1239 // In that case, we have lost the information about the original
1240 // object, and the emitted read barrier cannot work properly.
1241 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
1242 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
1243 }
1244
1245 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
1246 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
1247 LocationSummary* locations = instruction_->GetLocations();
1248 vixl32::Register reg_out = RegisterFrom(out_);
1249 DCHECK(locations->CanCall());
1250 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out.GetCode()));
1251 DCHECK(instruction_->IsInstanceFieldGet() ||
1252 instruction_->IsStaticFieldGet() ||
1253 instruction_->IsArrayGet() ||
1254 instruction_->IsInstanceOf() ||
1255 instruction_->IsCheckCast() ||
Andreas Gamped9911ee2017-03-27 13:27:24 -07001256 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001257 << "Unexpected instruction in read barrier for heap reference slow path: "
1258 << instruction_->DebugName();
1259 // The read barrier instrumentation of object ArrayGet
1260 // instructions does not support the HIntermediateAddress
1261 // instruction.
1262 DCHECK(!(instruction_->IsArrayGet() &&
1263 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
1264
1265 __ Bind(GetEntryLabel());
1266 SaveLiveRegisters(codegen, locations);
1267
1268 // We may have to change the index's value, but as `index_` is a
1269 // constant member (like other "inputs" of this slow path),
1270 // introduce a copy of it, `index`.
1271 Location index = index_;
1272 if (index_.IsValid()) {
1273 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
1274 if (instruction_->IsArrayGet()) {
1275 // Compute the actual memory offset and store it in `index`.
1276 vixl32::Register index_reg = RegisterFrom(index_);
1277 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg.GetCode()));
1278 if (codegen->IsCoreCalleeSaveRegister(index_reg.GetCode())) {
1279 // We are about to change the value of `index_reg` (see the
Roland Levillain9983e302017-07-14 14:34:22 +01001280 // calls to art::arm::ArmVIXLMacroAssembler::Lsl and
1281 // art::arm::ArmVIXLMacroAssembler::Add below), but it has
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001282 // not been saved by the previous call to
1283 // art::SlowPathCode::SaveLiveRegisters, as it is a
1284 // callee-save register --
1285 // art::SlowPathCode::SaveLiveRegisters does not consider
1286 // callee-save registers, as it has been designed with the
1287 // assumption that callee-save registers are supposed to be
1288 // handled by the called function. So, as a callee-save
1289 // register, `index_reg` _would_ eventually be saved onto
1290 // the stack, but it would be too late: we would have
1291 // changed its value earlier. Therefore, we manually save
1292 // it here into another freely available register,
1293 // `free_reg`, chosen of course among the caller-save
1294 // registers (as a callee-save `free_reg` register would
1295 // exhibit the same problem).
1296 //
1297 // Note we could have requested a temporary register from
1298 // the register allocator instead; but we prefer not to, as
1299 // this is a slow path, and we know we can find a
1300 // caller-save register that is available.
1301 vixl32::Register free_reg = FindAvailableCallerSaveRegister(codegen);
1302 __ Mov(free_reg, index_reg);
1303 index_reg = free_reg;
1304 index = LocationFrom(index_reg);
1305 } else {
1306 // The initial register stored in `index_` has already been
1307 // saved in the call to art::SlowPathCode::SaveLiveRegisters
1308 // (as it is not a callee-save register), so we can freely
1309 // use it.
1310 }
1311 // Shifting the index value contained in `index_reg` by the scale
1312 // factor (2) cannot overflow in practice, as the runtime is
1313 // unable to allocate object arrays with a size larger than
1314 // 2^26 - 1 (that is, 2^28 - 4 bytes).
1315 __ Lsl(index_reg, index_reg, TIMES_4);
1316 static_assert(
1317 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
1318 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
1319 __ Add(index_reg, index_reg, offset_);
1320 } else {
1321 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
1322 // intrinsics, `index_` is not shifted by a scale factor of 2
1323 // (as in the case of ArrayGet), as it is actually an offset
1324 // to an object field within an object.
1325 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
1326 DCHECK(instruction_->GetLocations()->Intrinsified());
1327 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
1328 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
1329 << instruction_->AsInvoke()->GetIntrinsic();
1330 DCHECK_EQ(offset_, 0U);
1331 DCHECK(index_.IsRegisterPair());
1332 // UnsafeGet's offset location is a register pair, the low
1333 // part contains the correct offset.
1334 index = index_.ToLow();
1335 }
1336 }
1337
1338 // We're moving two or three locations to locations that could
1339 // overlap, so we need a parallel move resolver.
1340 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +01001341 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001342 parallel_move.AddMove(ref_,
1343 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001344 DataType::Type::kReference,
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001345 nullptr);
1346 parallel_move.AddMove(obj_,
1347 LocationFrom(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001348 DataType::Type::kReference,
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001349 nullptr);
1350 if (index.IsValid()) {
1351 parallel_move.AddMove(index,
1352 LocationFrom(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001353 DataType::Type::kInt32,
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001354 nullptr);
1355 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
1356 } else {
1357 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
1358 __ Mov(calling_convention.GetRegisterAt(2), offset_);
1359 }
1360 arm_codegen->InvokeRuntime(kQuickReadBarrierSlow, instruction_, instruction_->GetDexPc(), this);
1361 CheckEntrypointTypes<
1362 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
1363 arm_codegen->Move32(out_, LocationFrom(r0));
1364
1365 RestoreLiveRegisters(codegen, locations);
1366 __ B(GetExitLabel());
1367 }
1368
1369 const char* GetDescription() const OVERRIDE {
1370 return "ReadBarrierForHeapReferenceSlowPathARMVIXL";
1371 }
1372
1373 private:
1374 vixl32::Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
1375 uint32_t ref = RegisterFrom(ref_).GetCode();
1376 uint32_t obj = RegisterFrom(obj_).GetCode();
1377 for (uint32_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
1378 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
1379 return vixl32::Register(i);
1380 }
1381 }
1382 // We shall never fail to find a free caller-save register, as
1383 // there are more than two core caller-save registers on ARM
1384 // (meaning it is possible to find one which is different from
1385 // `ref` and `obj`).
1386 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
1387 LOG(FATAL) << "Could not find a free caller-save register";
1388 UNREACHABLE();
1389 }
1390
1391 const Location out_;
1392 const Location ref_;
1393 const Location obj_;
1394 const uint32_t offset_;
1395 // An additional location containing an index to an array.
1396 // Only used for HArrayGet and the UnsafeGetObject &
1397 // UnsafeGetObjectVolatile intrinsics.
1398 const Location index_;
1399
1400 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathARMVIXL);
1401};
1402
1403// Slow path generating a read barrier for a GC root.
1404class ReadBarrierForRootSlowPathARMVIXL : public SlowPathCodeARMVIXL {
1405 public:
1406 ReadBarrierForRootSlowPathARMVIXL(HInstruction* instruction, Location out, Location root)
1407 : SlowPathCodeARMVIXL(instruction), out_(out), root_(root) {
1408 DCHECK(kEmitCompilerReadBarrier);
1409 }
1410
1411 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
1412 LocationSummary* locations = instruction_->GetLocations();
1413 vixl32::Register reg_out = RegisterFrom(out_);
1414 DCHECK(locations->CanCall());
1415 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out.GetCode()));
1416 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
1417 << "Unexpected instruction in read barrier for GC root slow path: "
1418 << instruction_->DebugName();
1419
1420 __ Bind(GetEntryLabel());
1421 SaveLiveRegisters(codegen, locations);
1422
1423 InvokeRuntimeCallingConventionARMVIXL calling_convention;
1424 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
1425 arm_codegen->Move32(LocationFrom(calling_convention.GetRegisterAt(0)), root_);
1426 arm_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
1427 instruction_,
1428 instruction_->GetDexPc(),
1429 this);
1430 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
1431 arm_codegen->Move32(out_, LocationFrom(r0));
1432
1433 RestoreLiveRegisters(codegen, locations);
1434 __ B(GetExitLabel());
1435 }
1436
1437 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathARMVIXL"; }
1438
1439 private:
1440 const Location out_;
1441 const Location root_;
1442
1443 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathARMVIXL);
1444};
Scott Wakelingc34dba72016-10-03 10:14:44 +01001445
Scott Wakelingfe885462016-09-22 10:24:38 +01001446inline vixl32::Condition ARMCondition(IfCondition cond) {
1447 switch (cond) {
1448 case kCondEQ: return eq;
1449 case kCondNE: return ne;
1450 case kCondLT: return lt;
1451 case kCondLE: return le;
1452 case kCondGT: return gt;
1453 case kCondGE: return ge;
1454 case kCondB: return lo;
1455 case kCondBE: return ls;
1456 case kCondA: return hi;
1457 case kCondAE: return hs;
1458 }
1459 LOG(FATAL) << "Unreachable";
1460 UNREACHABLE();
1461}
1462
1463// Maps signed condition to unsigned condition.
1464inline vixl32::Condition ARMUnsignedCondition(IfCondition cond) {
1465 switch (cond) {
1466 case kCondEQ: return eq;
1467 case kCondNE: return ne;
1468 // Signed to unsigned.
1469 case kCondLT: return lo;
1470 case kCondLE: return ls;
1471 case kCondGT: return hi;
1472 case kCondGE: return hs;
1473 // Unsigned remain unchanged.
1474 case kCondB: return lo;
1475 case kCondBE: return ls;
1476 case kCondA: return hi;
1477 case kCondAE: return hs;
1478 }
1479 LOG(FATAL) << "Unreachable";
1480 UNREACHABLE();
1481}
1482
1483inline vixl32::Condition ARMFPCondition(IfCondition cond, bool gt_bias) {
1484 // The ARM condition codes can express all the necessary branches, see the
1485 // "Meaning (floating-point)" column in the table A8-1 of the ARMv7 reference manual.
1486 // There is no dex instruction or HIR that would need the missing conditions
1487 // "equal or unordered" or "not equal".
1488 switch (cond) {
1489 case kCondEQ: return eq;
1490 case kCondNE: return ne /* unordered */;
1491 case kCondLT: return gt_bias ? cc : lt /* unordered */;
1492 case kCondLE: return gt_bias ? ls : le /* unordered */;
1493 case kCondGT: return gt_bias ? hi /* unordered */ : gt;
1494 case kCondGE: return gt_bias ? cs /* unordered */ : ge;
1495 default:
1496 LOG(FATAL) << "UNREACHABLE";
1497 UNREACHABLE();
1498 }
1499}
1500
Anton Kirilov74234da2017-01-13 14:42:47 +00001501inline ShiftType ShiftFromOpKind(HDataProcWithShifterOp::OpKind op_kind) {
1502 switch (op_kind) {
1503 case HDataProcWithShifterOp::kASR: return ShiftType::ASR;
1504 case HDataProcWithShifterOp::kLSL: return ShiftType::LSL;
1505 case HDataProcWithShifterOp::kLSR: return ShiftType::LSR;
1506 default:
1507 LOG(FATAL) << "Unexpected op kind " << op_kind;
1508 UNREACHABLE();
1509 }
1510}
1511
Scott Wakelingfe885462016-09-22 10:24:38 +01001512void CodeGeneratorARMVIXL::DumpCoreRegister(std::ostream& stream, int reg) const {
1513 stream << vixl32::Register(reg);
1514}
1515
1516void CodeGeneratorARMVIXL::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
1517 stream << vixl32::SRegister(reg);
1518}
1519
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001520static uint32_t ComputeSRegisterListMask(const SRegisterList& regs) {
Scott Wakelingfe885462016-09-22 10:24:38 +01001521 uint32_t mask = 0;
1522 for (uint32_t i = regs.GetFirstSRegister().GetCode();
1523 i <= regs.GetLastSRegister().GetCode();
1524 ++i) {
1525 mask |= (1 << i);
1526 }
1527 return mask;
1528}
1529
Artem Serovd4cc5b22016-11-04 11:19:09 +00001530// Saves the register in the stack. Returns the size taken on stack.
1531size_t CodeGeneratorARMVIXL::SaveCoreRegister(size_t stack_index ATTRIBUTE_UNUSED,
1532 uint32_t reg_id ATTRIBUTE_UNUSED) {
1533 TODO_VIXL32(FATAL);
1534 return 0;
1535}
1536
1537// Restores the register from the stack. Returns the size taken on stack.
1538size_t CodeGeneratorARMVIXL::RestoreCoreRegister(size_t stack_index ATTRIBUTE_UNUSED,
1539 uint32_t reg_id ATTRIBUTE_UNUSED) {
1540 TODO_VIXL32(FATAL);
1541 return 0;
1542}
1543
1544size_t CodeGeneratorARMVIXL::SaveFloatingPointRegister(size_t stack_index ATTRIBUTE_UNUSED,
1545 uint32_t reg_id ATTRIBUTE_UNUSED) {
1546 TODO_VIXL32(FATAL);
1547 return 0;
1548}
1549
1550size_t CodeGeneratorARMVIXL::RestoreFloatingPointRegister(size_t stack_index ATTRIBUTE_UNUSED,
1551 uint32_t reg_id ATTRIBUTE_UNUSED) {
1552 TODO_VIXL32(FATAL);
1553 return 0;
Anton Kirilove28d9ae2016-10-25 18:17:23 +01001554}
1555
Anton Kirilov74234da2017-01-13 14:42:47 +00001556static void GenerateDataProcInstruction(HInstruction::InstructionKind kind,
1557 vixl32::Register out,
1558 vixl32::Register first,
1559 const Operand& second,
1560 CodeGeneratorARMVIXL* codegen) {
1561 if (second.IsImmediate() && second.GetImmediate() == 0) {
1562 const Operand in = kind == HInstruction::kAnd
1563 ? Operand(0)
1564 : Operand(first);
1565
1566 __ Mov(out, in);
1567 } else {
1568 switch (kind) {
1569 case HInstruction::kAdd:
1570 __ Add(out, first, second);
1571 break;
1572 case HInstruction::kAnd:
1573 __ And(out, first, second);
1574 break;
1575 case HInstruction::kOr:
1576 __ Orr(out, first, second);
1577 break;
1578 case HInstruction::kSub:
1579 __ Sub(out, first, second);
1580 break;
1581 case HInstruction::kXor:
1582 __ Eor(out, first, second);
1583 break;
1584 default:
1585 LOG(FATAL) << "Unexpected instruction kind: " << kind;
1586 UNREACHABLE();
1587 }
1588 }
1589}
1590
1591static void GenerateDataProc(HInstruction::InstructionKind kind,
1592 const Location& out,
1593 const Location& first,
1594 const Operand& second_lo,
1595 const Operand& second_hi,
1596 CodeGeneratorARMVIXL* codegen) {
1597 const vixl32::Register first_hi = HighRegisterFrom(first);
1598 const vixl32::Register first_lo = LowRegisterFrom(first);
1599 const vixl32::Register out_hi = HighRegisterFrom(out);
1600 const vixl32::Register out_lo = LowRegisterFrom(out);
1601
1602 if (kind == HInstruction::kAdd) {
1603 __ Adds(out_lo, first_lo, second_lo);
1604 __ Adc(out_hi, first_hi, second_hi);
1605 } else if (kind == HInstruction::kSub) {
1606 __ Subs(out_lo, first_lo, second_lo);
1607 __ Sbc(out_hi, first_hi, second_hi);
1608 } else {
1609 GenerateDataProcInstruction(kind, out_lo, first_lo, second_lo, codegen);
1610 GenerateDataProcInstruction(kind, out_hi, first_hi, second_hi, codegen);
1611 }
1612}
1613
1614static Operand GetShifterOperand(vixl32::Register rm, ShiftType shift, uint32_t shift_imm) {
1615 return shift_imm == 0 ? Operand(rm) : Operand(rm, shift, shift_imm);
1616}
1617
1618static void GenerateLongDataProc(HDataProcWithShifterOp* instruction,
1619 CodeGeneratorARMVIXL* codegen) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001620 DCHECK_EQ(instruction->GetType(), DataType::Type::kInt64);
Anton Kirilov74234da2017-01-13 14:42:47 +00001621 DCHECK(HDataProcWithShifterOp::IsShiftOp(instruction->GetOpKind()));
1622
1623 const LocationSummary* const locations = instruction->GetLocations();
1624 const uint32_t shift_value = instruction->GetShiftAmount();
1625 const HInstruction::InstructionKind kind = instruction->GetInstrKind();
1626 const Location first = locations->InAt(0);
1627 const Location second = locations->InAt(1);
1628 const Location out = locations->Out();
1629 const vixl32::Register first_hi = HighRegisterFrom(first);
1630 const vixl32::Register first_lo = LowRegisterFrom(first);
1631 const vixl32::Register out_hi = HighRegisterFrom(out);
1632 const vixl32::Register out_lo = LowRegisterFrom(out);
1633 const vixl32::Register second_hi = HighRegisterFrom(second);
1634 const vixl32::Register second_lo = LowRegisterFrom(second);
1635 const ShiftType shift = ShiftFromOpKind(instruction->GetOpKind());
1636
1637 if (shift_value >= 32) {
1638 if (shift == ShiftType::LSL) {
1639 GenerateDataProcInstruction(kind,
1640 out_hi,
1641 first_hi,
1642 Operand(second_lo, ShiftType::LSL, shift_value - 32),
1643 codegen);
1644 GenerateDataProcInstruction(kind, out_lo, first_lo, 0, codegen);
1645 } else if (shift == ShiftType::ASR) {
1646 GenerateDataProc(kind,
1647 out,
1648 first,
1649 GetShifterOperand(second_hi, ShiftType::ASR, shift_value - 32),
1650 Operand(second_hi, ShiftType::ASR, 31),
1651 codegen);
1652 } else {
1653 DCHECK_EQ(shift, ShiftType::LSR);
1654 GenerateDataProc(kind,
1655 out,
1656 first,
1657 GetShifterOperand(second_hi, ShiftType::LSR, shift_value - 32),
1658 0,
1659 codegen);
1660 }
1661 } else {
1662 DCHECK_GT(shift_value, 1U);
1663 DCHECK_LT(shift_value, 32U);
1664
1665 UseScratchRegisterScope temps(codegen->GetVIXLAssembler());
1666
1667 if (shift == ShiftType::LSL) {
1668 // We are not doing this for HInstruction::kAdd because the output will require
1669 // Location::kOutputOverlap; not applicable to other cases.
1670 if (kind == HInstruction::kOr || kind == HInstruction::kXor) {
1671 GenerateDataProcInstruction(kind,
1672 out_hi,
1673 first_hi,
1674 Operand(second_hi, ShiftType::LSL, shift_value),
1675 codegen);
1676 GenerateDataProcInstruction(kind,
1677 out_hi,
1678 out_hi,
1679 Operand(second_lo, ShiftType::LSR, 32 - shift_value),
1680 codegen);
1681 GenerateDataProcInstruction(kind,
1682 out_lo,
1683 first_lo,
1684 Operand(second_lo, ShiftType::LSL, shift_value),
1685 codegen);
1686 } else {
1687 const vixl32::Register temp = temps.Acquire();
1688
1689 __ Lsl(temp, second_hi, shift_value);
1690 __ Orr(temp, temp, Operand(second_lo, ShiftType::LSR, 32 - shift_value));
1691 GenerateDataProc(kind,
1692 out,
1693 first,
1694 Operand(second_lo, ShiftType::LSL, shift_value),
1695 temp,
1696 codegen);
1697 }
1698 } else {
1699 DCHECK(shift == ShiftType::ASR || shift == ShiftType::LSR);
1700
1701 // We are not doing this for HInstruction::kAdd because the output will require
1702 // Location::kOutputOverlap; not applicable to other cases.
1703 if (kind == HInstruction::kOr || kind == HInstruction::kXor) {
1704 GenerateDataProcInstruction(kind,
1705 out_lo,
1706 first_lo,
1707 Operand(second_lo, ShiftType::LSR, shift_value),
1708 codegen);
1709 GenerateDataProcInstruction(kind,
1710 out_lo,
1711 out_lo,
1712 Operand(second_hi, ShiftType::LSL, 32 - shift_value),
1713 codegen);
1714 GenerateDataProcInstruction(kind,
1715 out_hi,
1716 first_hi,
1717 Operand(second_hi, shift, shift_value),
1718 codegen);
1719 } else {
1720 const vixl32::Register temp = temps.Acquire();
1721
1722 __ Lsr(temp, second_lo, shift_value);
1723 __ Orr(temp, temp, Operand(second_hi, ShiftType::LSL, 32 - shift_value));
1724 GenerateDataProc(kind,
1725 out,
1726 first,
1727 temp,
1728 Operand(second_hi, shift, shift_value),
1729 codegen);
1730 }
1731 }
1732 }
1733}
1734
Donghui Bai426b49c2016-11-08 14:55:38 +08001735static void GenerateVcmp(HInstruction* instruction, CodeGeneratorARMVIXL* codegen) {
1736 const Location rhs_loc = instruction->GetLocations()->InAt(1);
1737 if (rhs_loc.IsConstant()) {
1738 // 0.0 is the only immediate that can be encoded directly in
1739 // a VCMP instruction.
1740 //
1741 // Both the JLS (section 15.20.1) and the JVMS (section 6.5)
1742 // specify that in a floating-point comparison, positive zero
1743 // and negative zero are considered equal, so we can use the
1744 // literal 0.0 for both cases here.
1745 //
1746 // Note however that some methods (Float.equal, Float.compare,
1747 // Float.compareTo, Double.equal, Double.compare,
1748 // Double.compareTo, Math.max, Math.min, StrictMath.max,
1749 // StrictMath.min) consider 0.0 to be (strictly) greater than
1750 // -0.0. So if we ever translate calls to these methods into a
1751 // HCompare instruction, we must handle the -0.0 case with
1752 // care here.
1753 DCHECK(rhs_loc.GetConstant()->IsArithmeticZero());
1754
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001755 const DataType::Type type = instruction->InputAt(0)->GetType();
Donghui Bai426b49c2016-11-08 14:55:38 +08001756
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001757 if (type == DataType::Type::kFloat32) {
Donghui Bai426b49c2016-11-08 14:55:38 +08001758 __ Vcmp(F32, InputSRegisterAt(instruction, 0), 0.0);
1759 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001760 DCHECK_EQ(type, DataType::Type::kFloat64);
Donghui Bai426b49c2016-11-08 14:55:38 +08001761 __ Vcmp(F64, InputDRegisterAt(instruction, 0), 0.0);
1762 }
1763 } else {
1764 __ Vcmp(InputVRegisterAt(instruction, 0), InputVRegisterAt(instruction, 1));
1765 }
1766}
1767
Anton Kirilov5601d4e2017-05-11 19:33:50 +01001768static int64_t AdjustConstantForCondition(int64_t value,
1769 IfCondition* condition,
1770 IfCondition* opposite) {
1771 if (value == 1) {
1772 if (*condition == kCondB) {
1773 value = 0;
1774 *condition = kCondEQ;
1775 *opposite = kCondNE;
1776 } else if (*condition == kCondAE) {
1777 value = 0;
1778 *condition = kCondNE;
1779 *opposite = kCondEQ;
1780 }
1781 } else if (value == -1) {
1782 if (*condition == kCondGT) {
1783 value = 0;
1784 *condition = kCondGE;
1785 *opposite = kCondLT;
1786 } else if (*condition == kCondLE) {
1787 value = 0;
1788 *condition = kCondLT;
1789 *opposite = kCondGE;
1790 }
1791 }
1792
1793 return value;
1794}
1795
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001796static std::pair<vixl32::Condition, vixl32::Condition> GenerateLongTestConstant(
1797 HCondition* condition,
1798 bool invert,
1799 CodeGeneratorARMVIXL* codegen) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001800 DCHECK_EQ(condition->GetLeft()->GetType(), DataType::Type::kInt64);
Donghui Bai426b49c2016-11-08 14:55:38 +08001801
1802 const LocationSummary* const locations = condition->GetLocations();
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001803 IfCondition cond = condition->GetCondition();
1804 IfCondition opposite = condition->GetOppositeCondition();
1805
1806 if (invert) {
1807 std::swap(cond, opposite);
1808 }
1809
1810 std::pair<vixl32::Condition, vixl32::Condition> ret(eq, ne);
Donghui Bai426b49c2016-11-08 14:55:38 +08001811 const Location left = locations->InAt(0);
1812 const Location right = locations->InAt(1);
1813
1814 DCHECK(right.IsConstant());
1815
1816 const vixl32::Register left_high = HighRegisterFrom(left);
1817 const vixl32::Register left_low = LowRegisterFrom(left);
Anton Kirilov5601d4e2017-05-11 19:33:50 +01001818 int64_t value = AdjustConstantForCondition(Int64ConstantFrom(right), &cond, &opposite);
1819 UseScratchRegisterScope temps(codegen->GetVIXLAssembler());
1820
1821 // Comparisons against 0 are common enough to deserve special attention.
1822 if (value == 0) {
1823 switch (cond) {
1824 case kCondNE:
1825 // x > 0 iff x != 0 when the comparison is unsigned.
1826 case kCondA:
1827 ret = std::make_pair(ne, eq);
1828 FALLTHROUGH_INTENDED;
1829 case kCondEQ:
1830 // x <= 0 iff x == 0 when the comparison is unsigned.
1831 case kCondBE:
1832 __ Orrs(temps.Acquire(), left_low, left_high);
1833 return ret;
1834 case kCondLT:
1835 case kCondGE:
1836 __ Cmp(left_high, 0);
1837 return std::make_pair(ARMCondition(cond), ARMCondition(opposite));
1838 // Trivially true or false.
1839 case kCondB:
1840 ret = std::make_pair(ne, eq);
1841 FALLTHROUGH_INTENDED;
1842 case kCondAE:
1843 __ Cmp(left_low, left_low);
1844 return ret;
1845 default:
1846 break;
1847 }
1848 }
Donghui Bai426b49c2016-11-08 14:55:38 +08001849
1850 switch (cond) {
1851 case kCondEQ:
1852 case kCondNE:
1853 case kCondB:
1854 case kCondBE:
1855 case kCondA:
1856 case kCondAE: {
Anton Kirilov23b752b2017-07-20 14:40:44 +01001857 const uint32_t value_low = Low32Bits(value);
1858 Operand operand_low(value_low);
1859
Donghui Bai426b49c2016-11-08 14:55:38 +08001860 __ Cmp(left_high, High32Bits(value));
1861
Anton Kirilov23b752b2017-07-20 14:40:44 +01001862 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
1863 // we must ensure that the operands corresponding to the least significant
1864 // halves of the inputs fit into a 16-bit CMP encoding.
1865 if (!left_low.IsLow() || !IsUint<8>(value_low)) {
1866 operand_low = Operand(temps.Acquire());
1867 __ Mov(LeaveFlags, operand_low.GetBaseRegister(), value_low);
1868 }
1869
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001870 // We use the scope because of the IT block that follows.
Donghui Bai426b49c2016-11-08 14:55:38 +08001871 ExactAssemblyScope guard(codegen->GetVIXLAssembler(),
1872 2 * vixl32::k16BitT32InstructionSizeInBytes,
1873 CodeBufferCheckScope::kExactSize);
1874
1875 __ it(eq);
Anton Kirilov23b752b2017-07-20 14:40:44 +01001876 __ cmp(eq, left_low, operand_low);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001877 ret = std::make_pair(ARMUnsignedCondition(cond), ARMUnsignedCondition(opposite));
Donghui Bai426b49c2016-11-08 14:55:38 +08001878 break;
1879 }
1880 case kCondLE:
1881 case kCondGT:
1882 // Trivially true or false.
1883 if (value == std::numeric_limits<int64_t>::max()) {
1884 __ Cmp(left_low, left_low);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001885 ret = cond == kCondLE ? std::make_pair(eq, ne) : std::make_pair(ne, eq);
Donghui Bai426b49c2016-11-08 14:55:38 +08001886 break;
1887 }
1888
1889 if (cond == kCondLE) {
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001890 DCHECK_EQ(opposite, kCondGT);
Donghui Bai426b49c2016-11-08 14:55:38 +08001891 cond = kCondLT;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001892 opposite = kCondGE;
Donghui Bai426b49c2016-11-08 14:55:38 +08001893 } else {
1894 DCHECK_EQ(cond, kCondGT);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001895 DCHECK_EQ(opposite, kCondLE);
Donghui Bai426b49c2016-11-08 14:55:38 +08001896 cond = kCondGE;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001897 opposite = kCondLT;
Donghui Bai426b49c2016-11-08 14:55:38 +08001898 }
1899
1900 value++;
1901 FALLTHROUGH_INTENDED;
1902 case kCondGE:
1903 case kCondLT: {
Donghui Bai426b49c2016-11-08 14:55:38 +08001904 __ Cmp(left_low, Low32Bits(value));
1905 __ Sbcs(temps.Acquire(), left_high, High32Bits(value));
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001906 ret = std::make_pair(ARMCondition(cond), ARMCondition(opposite));
Donghui Bai426b49c2016-11-08 14:55:38 +08001907 break;
1908 }
1909 default:
1910 LOG(FATAL) << "Unreachable";
1911 UNREACHABLE();
1912 }
1913
1914 return ret;
1915}
1916
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001917static std::pair<vixl32::Condition, vixl32::Condition> GenerateLongTest(
1918 HCondition* condition,
1919 bool invert,
1920 CodeGeneratorARMVIXL* codegen) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001921 DCHECK_EQ(condition->GetLeft()->GetType(), DataType::Type::kInt64);
Donghui Bai426b49c2016-11-08 14:55:38 +08001922
1923 const LocationSummary* const locations = condition->GetLocations();
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001924 IfCondition cond = condition->GetCondition();
1925 IfCondition opposite = condition->GetOppositeCondition();
1926
1927 if (invert) {
1928 std::swap(cond, opposite);
1929 }
1930
1931 std::pair<vixl32::Condition, vixl32::Condition> ret(eq, ne);
Donghui Bai426b49c2016-11-08 14:55:38 +08001932 Location left = locations->InAt(0);
1933 Location right = locations->InAt(1);
1934
1935 DCHECK(right.IsRegisterPair());
1936
1937 switch (cond) {
1938 case kCondEQ:
1939 case kCondNE:
1940 case kCondB:
1941 case kCondBE:
1942 case kCondA:
1943 case kCondAE: {
1944 __ Cmp(HighRegisterFrom(left), HighRegisterFrom(right));
1945
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001946 // We use the scope because of the IT block that follows.
Donghui Bai426b49c2016-11-08 14:55:38 +08001947 ExactAssemblyScope guard(codegen->GetVIXLAssembler(),
1948 2 * vixl32::k16BitT32InstructionSizeInBytes,
1949 CodeBufferCheckScope::kExactSize);
1950
1951 __ it(eq);
1952 __ cmp(eq, LowRegisterFrom(left), LowRegisterFrom(right));
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001953 ret = std::make_pair(ARMUnsignedCondition(cond), ARMUnsignedCondition(opposite));
Donghui Bai426b49c2016-11-08 14:55:38 +08001954 break;
1955 }
1956 case kCondLE:
1957 case kCondGT:
1958 if (cond == kCondLE) {
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001959 DCHECK_EQ(opposite, kCondGT);
Donghui Bai426b49c2016-11-08 14:55:38 +08001960 cond = kCondGE;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001961 opposite = kCondLT;
Donghui Bai426b49c2016-11-08 14:55:38 +08001962 } else {
1963 DCHECK_EQ(cond, kCondGT);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001964 DCHECK_EQ(opposite, kCondLE);
Donghui Bai426b49c2016-11-08 14:55:38 +08001965 cond = kCondLT;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001966 opposite = kCondGE;
Donghui Bai426b49c2016-11-08 14:55:38 +08001967 }
1968
1969 std::swap(left, right);
1970 FALLTHROUGH_INTENDED;
1971 case kCondGE:
1972 case kCondLT: {
1973 UseScratchRegisterScope temps(codegen->GetVIXLAssembler());
1974
1975 __ Cmp(LowRegisterFrom(left), LowRegisterFrom(right));
1976 __ Sbcs(temps.Acquire(), HighRegisterFrom(left), HighRegisterFrom(right));
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001977 ret = std::make_pair(ARMCondition(cond), ARMCondition(opposite));
Donghui Bai426b49c2016-11-08 14:55:38 +08001978 break;
1979 }
1980 default:
1981 LOG(FATAL) << "Unreachable";
1982 UNREACHABLE();
1983 }
1984
1985 return ret;
1986}
1987
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001988static std::pair<vixl32::Condition, vixl32::Condition> GenerateTest(HCondition* condition,
1989 bool invert,
1990 CodeGeneratorARMVIXL* codegen) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001991 const DataType::Type type = condition->GetLeft()->GetType();
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001992 IfCondition cond = condition->GetCondition();
1993 IfCondition opposite = condition->GetOppositeCondition();
1994 std::pair<vixl32::Condition, vixl32::Condition> ret(eq, ne);
Donghui Bai426b49c2016-11-08 14:55:38 +08001995
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001996 if (invert) {
1997 std::swap(cond, opposite);
1998 }
Donghui Bai426b49c2016-11-08 14:55:38 +08001999
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002000 if (type == DataType::Type::kInt64) {
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002001 ret = condition->GetLocations()->InAt(1).IsConstant()
2002 ? GenerateLongTestConstant(condition, invert, codegen)
2003 : GenerateLongTest(condition, invert, codegen);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002004 } else if (DataType::IsFloatingPointType(type)) {
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002005 GenerateVcmp(condition, codegen);
2006 __ Vmrs(RegisterOrAPSR_nzcv(kPcCode), FPSCR);
2007 ret = std::make_pair(ARMFPCondition(cond, condition->IsGtBias()),
2008 ARMFPCondition(opposite, condition->IsGtBias()));
Donghui Bai426b49c2016-11-08 14:55:38 +08002009 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002010 DCHECK(DataType::IsIntegralType(type) || type == DataType::Type::kReference) << type;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002011 __ Cmp(InputRegisterAt(condition, 0), InputOperandAt(condition, 1));
2012 ret = std::make_pair(ARMCondition(cond), ARMCondition(opposite));
Donghui Bai426b49c2016-11-08 14:55:38 +08002013 }
2014
2015 return ret;
2016}
2017
Anton Kirilov5601d4e2017-05-11 19:33:50 +01002018static void GenerateConditionGeneric(HCondition* cond, CodeGeneratorARMVIXL* codegen) {
Anton Kirilov5601d4e2017-05-11 19:33:50 +01002019 const vixl32::Register out = OutputRegister(cond);
2020 const auto condition = GenerateTest(cond, false, codegen);
2021
2022 __ Mov(LeaveFlags, out, 0);
2023
2024 if (out.IsLow()) {
2025 // We use the scope because of the IT block that follows.
2026 ExactAssemblyScope guard(codegen->GetVIXLAssembler(),
2027 2 * vixl32::k16BitT32InstructionSizeInBytes,
2028 CodeBufferCheckScope::kExactSize);
2029
2030 __ it(condition.first);
2031 __ mov(condition.first, out, 1);
2032 } else {
2033 vixl32::Label done_label;
2034 vixl32::Label* const final_label = codegen->GetFinalLabel(cond, &done_label);
2035
2036 __ B(condition.second, final_label, /* far_target */ false);
2037 __ Mov(out, 1);
2038
2039 if (done_label.IsReferenced()) {
2040 __ Bind(&done_label);
2041 }
2042 }
2043}
2044
2045static void GenerateEqualLong(HCondition* cond, CodeGeneratorARMVIXL* codegen) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002046 DCHECK_EQ(cond->GetLeft()->GetType(), DataType::Type::kInt64);
Anton Kirilov5601d4e2017-05-11 19:33:50 +01002047
2048 const LocationSummary* const locations = cond->GetLocations();
2049 IfCondition condition = cond->GetCondition();
2050 const vixl32::Register out = OutputRegister(cond);
2051 const Location left = locations->InAt(0);
2052 const Location right = locations->InAt(1);
2053 vixl32::Register left_high = HighRegisterFrom(left);
2054 vixl32::Register left_low = LowRegisterFrom(left);
2055 vixl32::Register temp;
2056 UseScratchRegisterScope temps(codegen->GetVIXLAssembler());
2057
2058 if (right.IsConstant()) {
2059 IfCondition opposite = cond->GetOppositeCondition();
2060 const int64_t value = AdjustConstantForCondition(Int64ConstantFrom(right),
2061 &condition,
2062 &opposite);
2063 Operand right_high = High32Bits(value);
2064 Operand right_low = Low32Bits(value);
2065
2066 // The output uses Location::kNoOutputOverlap.
2067 if (out.Is(left_high)) {
2068 std::swap(left_low, left_high);
2069 std::swap(right_low, right_high);
2070 }
2071
2072 __ Sub(out, left_low, right_low);
2073 temp = temps.Acquire();
2074 __ Sub(temp, left_high, right_high);
2075 } else {
2076 DCHECK(right.IsRegisterPair());
2077 temp = temps.Acquire();
2078 __ Sub(temp, left_high, HighRegisterFrom(right));
2079 __ Sub(out, left_low, LowRegisterFrom(right));
2080 }
2081
2082 // Need to check after calling AdjustConstantForCondition().
2083 DCHECK(condition == kCondEQ || condition == kCondNE) << condition;
2084
2085 if (condition == kCondNE && out.IsLow()) {
2086 __ Orrs(out, out, temp);
2087
2088 // We use the scope because of the IT block that follows.
2089 ExactAssemblyScope guard(codegen->GetVIXLAssembler(),
2090 2 * vixl32::k16BitT32InstructionSizeInBytes,
2091 CodeBufferCheckScope::kExactSize);
2092
2093 __ it(ne);
2094 __ mov(ne, out, 1);
2095 } else {
2096 __ Orr(out, out, temp);
2097 codegen->GenerateConditionWithZero(condition, out, out, temp);
2098 }
2099}
2100
Anton Kirilov5601d4e2017-05-11 19:33:50 +01002101static void GenerateConditionLong(HCondition* cond, CodeGeneratorARMVIXL* codegen) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002102 DCHECK_EQ(cond->GetLeft()->GetType(), DataType::Type::kInt64);
Anton Kirilov5601d4e2017-05-11 19:33:50 +01002103
2104 const LocationSummary* const locations = cond->GetLocations();
2105 IfCondition condition = cond->GetCondition();
2106 const vixl32::Register out = OutputRegister(cond);
2107 const Location left = locations->InAt(0);
2108 const Location right = locations->InAt(1);
2109
2110 if (right.IsConstant()) {
2111 IfCondition opposite = cond->GetOppositeCondition();
2112
2113 // Comparisons against 0 are common enough to deserve special attention.
2114 if (AdjustConstantForCondition(Int64ConstantFrom(right), &condition, &opposite) == 0) {
2115 switch (condition) {
2116 case kCondNE:
2117 case kCondA:
2118 if (out.IsLow()) {
2119 // We only care if both input registers are 0 or not.
2120 __ Orrs(out, LowRegisterFrom(left), HighRegisterFrom(left));
2121
2122 // We use the scope because of the IT block that follows.
2123 ExactAssemblyScope guard(codegen->GetVIXLAssembler(),
2124 2 * vixl32::k16BitT32InstructionSizeInBytes,
2125 CodeBufferCheckScope::kExactSize);
2126
2127 __ it(ne);
2128 __ mov(ne, out, 1);
2129 return;
2130 }
2131
2132 FALLTHROUGH_INTENDED;
2133 case kCondEQ:
2134 case kCondBE:
2135 // We only care if both input registers are 0 or not.
2136 __ Orr(out, LowRegisterFrom(left), HighRegisterFrom(left));
2137 codegen->GenerateConditionWithZero(condition, out, out);
2138 return;
2139 case kCondLT:
2140 case kCondGE:
2141 // We only care about the sign bit.
2142 FALLTHROUGH_INTENDED;
2143 case kCondAE:
2144 case kCondB:
2145 codegen->GenerateConditionWithZero(condition, out, HighRegisterFrom(left));
2146 return;
2147 case kCondLE:
2148 case kCondGT:
2149 default:
2150 break;
2151 }
2152 }
2153 }
2154
Anton Kirilov23b752b2017-07-20 14:40:44 +01002155 // If `out` is a low register, then the GenerateConditionGeneric()
2156 // function generates a shorter code sequence that is still branchless.
2157 if ((condition == kCondEQ || condition == kCondNE) && !out.IsLow()) {
Anton Kirilov5601d4e2017-05-11 19:33:50 +01002158 GenerateEqualLong(cond, codegen);
2159 return;
2160 }
2161
Anton Kirilov23b752b2017-07-20 14:40:44 +01002162 GenerateConditionGeneric(cond, codegen);
Anton Kirilov5601d4e2017-05-11 19:33:50 +01002163}
2164
Roland Levillain6d729a72017-06-30 18:34:01 +01002165static void GenerateConditionIntegralOrNonPrimitive(HCondition* cond,
2166 CodeGeneratorARMVIXL* codegen) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002167 const DataType::Type type = cond->GetLeft()->GetType();
Anton Kirilov5601d4e2017-05-11 19:33:50 +01002168
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002169 DCHECK(DataType::IsIntegralType(type) || type == DataType::Type::kReference) << type;
Anton Kirilov5601d4e2017-05-11 19:33:50 +01002170
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002171 if (type == DataType::Type::kInt64) {
Anton Kirilov5601d4e2017-05-11 19:33:50 +01002172 GenerateConditionLong(cond, codegen);
2173 return;
2174 }
2175
2176 IfCondition condition = cond->GetCondition();
2177 vixl32::Register in = InputRegisterAt(cond, 0);
2178 const vixl32::Register out = OutputRegister(cond);
2179 const Location right = cond->GetLocations()->InAt(1);
2180 int64_t value;
2181
2182 if (right.IsConstant()) {
2183 IfCondition opposite = cond->GetOppositeCondition();
2184
2185 value = AdjustConstantForCondition(Int64ConstantFrom(right), &condition, &opposite);
2186
2187 // Comparisons against 0 are common enough to deserve special attention.
2188 if (value == 0) {
2189 switch (condition) {
2190 case kCondNE:
2191 case kCondA:
2192 if (out.IsLow() && out.Is(in)) {
2193 __ Cmp(out, 0);
2194
2195 // We use the scope because of the IT block that follows.
2196 ExactAssemblyScope guard(codegen->GetVIXLAssembler(),
2197 2 * vixl32::k16BitT32InstructionSizeInBytes,
2198 CodeBufferCheckScope::kExactSize);
2199
2200 __ it(ne);
2201 __ mov(ne, out, 1);
2202 return;
2203 }
2204
2205 FALLTHROUGH_INTENDED;
2206 case kCondEQ:
2207 case kCondBE:
2208 case kCondLT:
2209 case kCondGE:
2210 case kCondAE:
2211 case kCondB:
2212 codegen->GenerateConditionWithZero(condition, out, in);
2213 return;
2214 case kCondLE:
2215 case kCondGT:
2216 default:
2217 break;
2218 }
2219 }
2220 }
2221
2222 if (condition == kCondEQ || condition == kCondNE) {
2223 Operand operand(0);
2224
2225 if (right.IsConstant()) {
2226 operand = Operand::From(value);
2227 } else if (out.Is(RegisterFrom(right))) {
2228 // Avoid 32-bit instructions if possible.
2229 operand = InputOperandAt(cond, 0);
2230 in = RegisterFrom(right);
2231 } else {
2232 operand = InputOperandAt(cond, 1);
2233 }
2234
2235 if (condition == kCondNE && out.IsLow()) {
2236 __ Subs(out, in, operand);
2237
2238 // We use the scope because of the IT block that follows.
2239 ExactAssemblyScope guard(codegen->GetVIXLAssembler(),
2240 2 * vixl32::k16BitT32InstructionSizeInBytes,
2241 CodeBufferCheckScope::kExactSize);
2242
2243 __ it(ne);
2244 __ mov(ne, out, 1);
2245 } else {
2246 __ Sub(out, in, operand);
2247 codegen->GenerateConditionWithZero(condition, out, out);
2248 }
2249
2250 return;
2251 }
2252
2253 GenerateConditionGeneric(cond, codegen);
2254}
2255
Donghui Bai426b49c2016-11-08 14:55:38 +08002256static bool CanEncodeConstantAs8BitImmediate(HConstant* constant) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002257 const DataType::Type type = constant->GetType();
Donghui Bai426b49c2016-11-08 14:55:38 +08002258 bool ret = false;
2259
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002260 DCHECK(DataType::IsIntegralType(type) || type == DataType::Type::kReference) << type;
Donghui Bai426b49c2016-11-08 14:55:38 +08002261
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002262 if (type == DataType::Type::kInt64) {
Donghui Bai426b49c2016-11-08 14:55:38 +08002263 const uint64_t value = Uint64ConstantFrom(constant);
2264
2265 ret = IsUint<8>(Low32Bits(value)) && IsUint<8>(High32Bits(value));
2266 } else {
2267 ret = IsUint<8>(Int32ConstantFrom(constant));
2268 }
2269
2270 return ret;
2271}
2272
2273static Location Arm8BitEncodableConstantOrRegister(HInstruction* constant) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002274 DCHECK(!DataType::IsFloatingPointType(constant->GetType()));
Donghui Bai426b49c2016-11-08 14:55:38 +08002275
2276 if (constant->IsConstant() && CanEncodeConstantAs8BitImmediate(constant->AsConstant())) {
2277 return Location::ConstantLocation(constant->AsConstant());
2278 }
2279
2280 return Location::RequiresRegister();
2281}
2282
2283static bool CanGenerateConditionalMove(const Location& out, const Location& src) {
2284 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
2285 // we check that we are not dealing with floating-point output (there is no
2286 // 16-bit VMOV encoding).
2287 if (!out.IsRegister() && !out.IsRegisterPair()) {
2288 return false;
2289 }
2290
2291 // For constants, we also check that the output is in one or two low registers,
2292 // and that the constants fit in an 8-bit unsigned integer, so that a 16-bit
2293 // MOV encoding can be used.
2294 if (src.IsConstant()) {
2295 if (!CanEncodeConstantAs8BitImmediate(src.GetConstant())) {
2296 return false;
2297 }
2298
2299 if (out.IsRegister()) {
2300 if (!RegisterFrom(out).IsLow()) {
2301 return false;
2302 }
2303 } else {
2304 DCHECK(out.IsRegisterPair());
2305
2306 if (!HighRegisterFrom(out).IsLow()) {
2307 return false;
2308 }
2309 }
2310 }
2311
2312 return true;
2313}
2314
Scott Wakelingfe885462016-09-22 10:24:38 +01002315#undef __
2316
Donghui Bai426b49c2016-11-08 14:55:38 +08002317vixl32::Label* CodeGeneratorARMVIXL::GetFinalLabel(HInstruction* instruction,
2318 vixl32::Label* final_label) {
2319 DCHECK(!instruction->IsControlFlow() && !instruction->IsSuspendCheck());
Anton Kirilov6f644202017-02-27 18:29:45 +00002320 DCHECK(!instruction->IsInvoke() || !instruction->GetLocations()->CanCall());
Donghui Bai426b49c2016-11-08 14:55:38 +08002321
2322 const HBasicBlock* const block = instruction->GetBlock();
2323 const HLoopInformation* const info = block->GetLoopInformation();
2324 HInstruction* const next = instruction->GetNext();
2325
2326 // Avoid a branch to a branch.
2327 if (next->IsGoto() && (info == nullptr ||
2328 !info->IsBackEdge(*block) ||
2329 !info->HasSuspendCheck())) {
2330 final_label = GetLabelOf(next->AsGoto()->GetSuccessor());
2331 }
2332
2333 return final_label;
2334}
2335
Scott Wakelingfe885462016-09-22 10:24:38 +01002336CodeGeneratorARMVIXL::CodeGeneratorARMVIXL(HGraph* graph,
2337 const ArmInstructionSetFeatures& isa_features,
2338 const CompilerOptions& compiler_options,
2339 OptimizingCompilerStats* stats)
2340 : CodeGenerator(graph,
2341 kNumberOfCoreRegisters,
2342 kNumberOfSRegisters,
2343 kNumberOfRegisterPairs,
2344 kCoreCalleeSaves.GetList(),
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002345 ComputeSRegisterListMask(kFpuCalleeSaves),
Scott Wakelingfe885462016-09-22 10:24:38 +01002346 compiler_options,
2347 stats),
Vladimir Markoca6fff82017-10-03 14:49:14 +01002348 block_labels_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
2349 jump_tables_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Scott Wakelingfe885462016-09-22 10:24:38 +01002350 location_builder_(graph, this),
2351 instruction_visitor_(graph, this),
Vladimir Markoca6fff82017-10-03 14:49:14 +01002352 move_resolver_(graph->GetAllocator(), this),
2353 assembler_(graph->GetAllocator()),
Artem Serovd4cc5b22016-11-04 11:19:09 +00002354 isa_features_(isa_features),
Artem Serovc5fcb442016-12-02 19:19:58 +00002355 uint32_literals_(std::less<uint32_t>(),
Vladimir Markoca6fff82017-10-03 14:49:14 +01002356 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko59eb30f2018-02-20 11:52:34 +00002357 boot_image_method_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +01002358 method_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko59eb30f2018-02-20 11:52:34 +00002359 boot_image_type_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +01002360 type_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko59eb30f2018-02-20 11:52:34 +00002361 boot_image_string_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +01002362 string_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
2363 baker_read_barrier_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Artem Serovc5fcb442016-12-02 19:19:58 +00002364 jit_string_patches_(StringReferenceValueComparator(),
Vladimir Markoca6fff82017-10-03 14:49:14 +01002365 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Artem Serovc5fcb442016-12-02 19:19:58 +00002366 jit_class_patches_(TypeReferenceValueComparator(),
Vladimir Markoca6fff82017-10-03 14:49:14 +01002367 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)) {
Scott Wakelingfe885462016-09-22 10:24:38 +01002368 // Always save the LR register to mimic Quick.
2369 AddAllocatedRegister(Location::RegisterLocation(LR));
Nicolas Geoffray13a797b2017-03-15 16:41:31 +00002370 // Give D30 and D31 as scratch register to VIXL. The register allocator only works on
2371 // S0-S31, which alias to D0-D15.
2372 GetVIXLAssembler()->GetScratchVRegisterList()->Combine(d31);
2373 GetVIXLAssembler()->GetScratchVRegisterList()->Combine(d30);
Scott Wakelingfe885462016-09-22 10:24:38 +01002374}
2375
Artem Serov551b28f2016-10-18 19:11:30 +01002376void JumpTableARMVIXL::EmitTable(CodeGeneratorARMVIXL* codegen) {
2377 uint32_t num_entries = switch_instr_->GetNumEntries();
2378 DCHECK_GE(num_entries, kPackedSwitchCompareJumpThreshold);
2379
2380 // We are about to use the assembler to place literals directly. Make sure we have enough
Scott Wakelingb77051e2016-11-21 19:46:00 +00002381 // underlying code buffer and we have generated a jump table of the right size, using
2382 // codegen->GetVIXLAssembler()->GetBuffer().Align();
Artem Serov0fb37192016-12-06 18:13:40 +00002383 ExactAssemblyScope aas(codegen->GetVIXLAssembler(),
2384 num_entries * sizeof(int32_t),
2385 CodeBufferCheckScope::kMaximumSize);
Artem Serov551b28f2016-10-18 19:11:30 +01002386 // TODO(VIXL): Check that using lower case bind is fine here.
2387 codegen->GetVIXLAssembler()->bind(&table_start_);
Artem Serov09a940d2016-11-11 16:15:11 +00002388 for (uint32_t i = 0; i < num_entries; i++) {
2389 codegen->GetVIXLAssembler()->place(bb_addresses_[i].get());
2390 }
2391}
2392
2393void JumpTableARMVIXL::FixTable(CodeGeneratorARMVIXL* codegen) {
2394 uint32_t num_entries = switch_instr_->GetNumEntries();
2395 DCHECK_GE(num_entries, kPackedSwitchCompareJumpThreshold);
2396
Artem Serov551b28f2016-10-18 19:11:30 +01002397 const ArenaVector<HBasicBlock*>& successors = switch_instr_->GetBlock()->GetSuccessors();
2398 for (uint32_t i = 0; i < num_entries; i++) {
2399 vixl32::Label* target_label = codegen->GetLabelOf(successors[i]);
2400 DCHECK(target_label->IsBound());
2401 int32_t jump_offset = target_label->GetLocation() - table_start_.GetLocation();
2402 // When doing BX to address we need to have lower bit set to 1 in T32.
2403 if (codegen->GetVIXLAssembler()->IsUsingT32()) {
2404 jump_offset++;
2405 }
2406 DCHECK_GT(jump_offset, std::numeric_limits<int32_t>::min());
2407 DCHECK_LE(jump_offset, std::numeric_limits<int32_t>::max());
Artem Serov09a940d2016-11-11 16:15:11 +00002408
Scott Wakelingb77051e2016-11-21 19:46:00 +00002409 bb_addresses_[i].get()->UpdateValue(jump_offset, codegen->GetVIXLAssembler()->GetBuffer());
Artem Serov551b28f2016-10-18 19:11:30 +01002410 }
2411}
2412
Artem Serov09a940d2016-11-11 16:15:11 +00002413void CodeGeneratorARMVIXL::FixJumpTables() {
Artem Serov551b28f2016-10-18 19:11:30 +01002414 for (auto&& jump_table : jump_tables_) {
Artem Serov09a940d2016-11-11 16:15:11 +00002415 jump_table->FixTable(this);
Artem Serov551b28f2016-10-18 19:11:30 +01002416 }
2417}
2418
Andreas Gampeca620d72016-11-08 08:09:33 -08002419#define __ reinterpret_cast<ArmVIXLAssembler*>(GetAssembler())->GetVIXLAssembler()-> // NOLINT
Scott Wakelingfe885462016-09-22 10:24:38 +01002420
2421void CodeGeneratorARMVIXL::Finalize(CodeAllocator* allocator) {
Artem Serov09a940d2016-11-11 16:15:11 +00002422 FixJumpTables();
Scott Wakelingfe885462016-09-22 10:24:38 +01002423 GetAssembler()->FinalizeCode();
2424 CodeGenerator::Finalize(allocator);
2425}
2426
2427void CodeGeneratorARMVIXL::SetupBlockedRegisters() const {
Scott Wakelingfe885462016-09-22 10:24:38 +01002428 // Stack register, LR and PC are always reserved.
2429 blocked_core_registers_[SP] = true;
2430 blocked_core_registers_[LR] = true;
2431 blocked_core_registers_[PC] = true;
2432
Roland Levillain6d729a72017-06-30 18:34:01 +01002433 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2434 // Reserve marking register.
2435 blocked_core_registers_[MR] = true;
2436 }
2437
Scott Wakelingfe885462016-09-22 10:24:38 +01002438 // Reserve thread register.
2439 blocked_core_registers_[TR] = true;
2440
2441 // Reserve temp register.
2442 blocked_core_registers_[IP] = true;
2443
2444 if (GetGraph()->IsDebuggable()) {
2445 // Stubs do not save callee-save floating point registers. If the graph
2446 // is debuggable, we need to deal with these registers differently. For
2447 // now, just block them.
2448 for (uint32_t i = kFpuCalleeSaves.GetFirstSRegister().GetCode();
2449 i <= kFpuCalleeSaves.GetLastSRegister().GetCode();
2450 ++i) {
2451 blocked_fpu_registers_[i] = true;
2452 }
2453 }
Scott Wakelingfe885462016-09-22 10:24:38 +01002454}
2455
Scott Wakelingfe885462016-09-22 10:24:38 +01002456InstructionCodeGeneratorARMVIXL::InstructionCodeGeneratorARMVIXL(HGraph* graph,
2457 CodeGeneratorARMVIXL* codegen)
2458 : InstructionCodeGenerator(graph, codegen),
2459 assembler_(codegen->GetAssembler()),
2460 codegen_(codegen) {}
2461
2462void CodeGeneratorARMVIXL::ComputeSpillMask() {
2463 core_spill_mask_ = allocated_registers_.GetCoreRegisters() & core_callee_save_mask_;
2464 DCHECK_NE(core_spill_mask_, 0u) << "At least the return address register must be saved";
2465 // There is no easy instruction to restore just the PC on thumb2. We spill and
2466 // restore another arbitrary register.
2467 core_spill_mask_ |= (1 << kCoreAlwaysSpillRegister.GetCode());
2468 fpu_spill_mask_ = allocated_registers_.GetFloatingPointRegisters() & fpu_callee_save_mask_;
2469 // We use vpush and vpop for saving and restoring floating point registers, which take
2470 // a SRegister and the number of registers to save/restore after that SRegister. We
2471 // therefore update the `fpu_spill_mask_` to also contain those registers not allocated,
2472 // but in the range.
2473 if (fpu_spill_mask_ != 0) {
2474 uint32_t least_significant_bit = LeastSignificantBit(fpu_spill_mask_);
2475 uint32_t most_significant_bit = MostSignificantBit(fpu_spill_mask_);
2476 for (uint32_t i = least_significant_bit + 1 ; i < most_significant_bit; ++i) {
2477 fpu_spill_mask_ |= (1 << i);
2478 }
2479 }
2480}
2481
2482void CodeGeneratorARMVIXL::GenerateFrameEntry() {
2483 bool skip_overflow_check =
2484 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kArm);
2485 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
2486 __ Bind(&frame_entry_label_);
2487
Nicolas Geoffray8d728322018-01-18 22:44:32 +00002488 if (GetCompilerOptions().CountHotnessInCompiledCode()) {
2489 UseScratchRegisterScope temps(GetVIXLAssembler());
2490 vixl32::Register temp = temps.Acquire();
2491 __ Ldrh(temp, MemOperand(kMethodRegister, ArtMethod::HotnessCountOffset().Int32Value()));
2492 __ Add(temp, temp, 1);
2493 __ Strh(temp, MemOperand(kMethodRegister, ArtMethod::HotnessCountOffset().Int32Value()));
2494 }
2495
Scott Wakelingfe885462016-09-22 10:24:38 +01002496 if (HasEmptyFrame()) {
2497 return;
2498 }
2499
Scott Wakelingfe885462016-09-22 10:24:38 +01002500 if (!skip_overflow_check) {
xueliang.zhong10049552018-01-31 17:10:36 +00002501 // Using r4 instead of IP saves 2 bytes.
Nicolas Geoffray1a4f3ca2018-01-25 14:07:15 +00002502 UseScratchRegisterScope temps(GetVIXLAssembler());
xueliang.zhong10049552018-01-31 17:10:36 +00002503 vixl32::Register temp;
2504 // TODO: Remove this check when R4 is made a callee-save register
2505 // in ART compiled code (b/72801708). Currently we need to make
2506 // sure r4 is not blocked, e.g. in special purpose
2507 // TestCodeGeneratorARMVIXL; also asserting that r4 is available
2508 // here.
2509 if (!blocked_core_registers_[R4]) {
2510 for (vixl32::Register reg : kParameterCoreRegistersVIXL) {
2511 DCHECK(!reg.Is(r4));
2512 }
2513 DCHECK(!kCoreCalleeSaves.Includes(r4));
2514 temp = r4;
2515 } else {
2516 temp = temps.Acquire();
2517 }
Vladimir Marko33bff252017-11-01 14:35:42 +00002518 __ Sub(temp, sp, Operand::From(GetStackOverflowReservedBytes(InstructionSet::kArm)));
Scott Wakelingfe885462016-09-22 10:24:38 +01002519 // The load must immediately precede RecordPcInfo.
Artem Serov0fb37192016-12-06 18:13:40 +00002520 ExactAssemblyScope aas(GetVIXLAssembler(),
2521 vixl32::kMaxInstructionSizeInBytes,
2522 CodeBufferCheckScope::kMaximumSize);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002523 __ ldr(temp, MemOperand(temp));
2524 RecordPcInfo(nullptr, 0);
Scott Wakelingfe885462016-09-22 10:24:38 +01002525 }
2526
2527 __ Push(RegisterList(core_spill_mask_));
2528 GetAssembler()->cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(core_spill_mask_));
2529 GetAssembler()->cfi().RelOffsetForMany(DWARFReg(kMethodRegister),
2530 0,
2531 core_spill_mask_,
2532 kArmWordSize);
2533 if (fpu_spill_mask_ != 0) {
2534 uint32_t first = LeastSignificantBit(fpu_spill_mask_);
2535
2536 // Check that list is contiguous.
2537 DCHECK_EQ(fpu_spill_mask_ >> CTZ(fpu_spill_mask_), ~0u >> (32 - POPCOUNT(fpu_spill_mask_)));
2538
2539 __ Vpush(SRegisterList(vixl32::SRegister(first), POPCOUNT(fpu_spill_mask_)));
2540 GetAssembler()->cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(fpu_spill_mask_));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002541 GetAssembler()->cfi().RelOffsetForMany(DWARFReg(s0), 0, fpu_spill_mask_, kArmWordSize);
Scott Wakelingfe885462016-09-22 10:24:38 +01002542 }
Scott Wakelingbffdc702016-12-07 17:46:03 +00002543
Scott Wakelingfe885462016-09-22 10:24:38 +01002544 int adjust = GetFrameSize() - FrameEntrySpillSize();
2545 __ Sub(sp, sp, adjust);
2546 GetAssembler()->cfi().AdjustCFAOffset(adjust);
Scott Wakelingbffdc702016-12-07 17:46:03 +00002547
2548 // Save the current method if we need it. Note that we do not
2549 // do this in HCurrentMethod, as the instruction might have been removed
2550 // in the SSA graph.
2551 if (RequiresCurrentMethod()) {
2552 GetAssembler()->StoreToOffset(kStoreWord, kMethodRegister, sp, 0);
2553 }
Nicolas Geoffrayf7893532017-06-15 12:34:36 +01002554
2555 if (GetGraph()->HasShouldDeoptimizeFlag()) {
2556 UseScratchRegisterScope temps(GetVIXLAssembler());
2557 vixl32::Register temp = temps.Acquire();
2558 // Initialize should_deoptimize flag to 0.
2559 __ Mov(temp, 0);
2560 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, GetStackOffsetOfShouldDeoptimizeFlag());
2561 }
Roland Levillain5daa4952017-07-03 17:23:56 +01002562
2563 MaybeGenerateMarkingRegisterCheck(/* code */ 1);
Scott Wakelingfe885462016-09-22 10:24:38 +01002564}
2565
2566void CodeGeneratorARMVIXL::GenerateFrameExit() {
2567 if (HasEmptyFrame()) {
2568 __ Bx(lr);
2569 return;
2570 }
2571 GetAssembler()->cfi().RememberState();
2572 int adjust = GetFrameSize() - FrameEntrySpillSize();
2573 __ Add(sp, sp, adjust);
2574 GetAssembler()->cfi().AdjustCFAOffset(-adjust);
2575 if (fpu_spill_mask_ != 0) {
2576 uint32_t first = LeastSignificantBit(fpu_spill_mask_);
2577
2578 // Check that list is contiguous.
2579 DCHECK_EQ(fpu_spill_mask_ >> CTZ(fpu_spill_mask_), ~0u >> (32 - POPCOUNT(fpu_spill_mask_)));
2580
2581 __ Vpop(SRegisterList(vixl32::SRegister(first), POPCOUNT(fpu_spill_mask_)));
2582 GetAssembler()->cfi().AdjustCFAOffset(
2583 -static_cast<int>(kArmWordSize) * POPCOUNT(fpu_spill_mask_));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002584 GetAssembler()->cfi().RestoreMany(DWARFReg(vixl32::SRegister(0)), fpu_spill_mask_);
Scott Wakelingfe885462016-09-22 10:24:38 +01002585 }
2586 // Pop LR into PC to return.
2587 DCHECK_NE(core_spill_mask_ & (1 << kLrCode), 0U);
2588 uint32_t pop_mask = (core_spill_mask_ & (~(1 << kLrCode))) | 1 << kPcCode;
2589 __ Pop(RegisterList(pop_mask));
2590 GetAssembler()->cfi().RestoreState();
2591 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
2592}
2593
2594void CodeGeneratorARMVIXL::Bind(HBasicBlock* block) {
2595 __ Bind(GetLabelOf(block));
2596}
2597
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002598Location InvokeDexCallingConventionVisitorARMVIXL::GetNextLocation(DataType::Type type) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002599 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002600 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002601 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002602 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002603 case DataType::Type::kInt8:
2604 case DataType::Type::kUint16:
2605 case DataType::Type::kInt16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002606 case DataType::Type::kInt32: {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002607 uint32_t index = gp_index_++;
2608 uint32_t stack_index = stack_index_++;
2609 if (index < calling_convention.GetNumberOfRegisters()) {
2610 return LocationFrom(calling_convention.GetRegisterAt(index));
2611 } else {
2612 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
2613 }
2614 }
2615
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002616 case DataType::Type::kInt64: {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002617 uint32_t index = gp_index_;
2618 uint32_t stack_index = stack_index_;
2619 gp_index_ += 2;
2620 stack_index_ += 2;
2621 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
2622 if (calling_convention.GetRegisterAt(index).Is(r1)) {
2623 // Skip R1, and use R2_R3 instead.
2624 gp_index_++;
2625 index++;
2626 }
2627 }
2628 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
2629 DCHECK_EQ(calling_convention.GetRegisterAt(index).GetCode() + 1,
2630 calling_convention.GetRegisterAt(index + 1).GetCode());
2631
2632 return LocationFrom(calling_convention.GetRegisterAt(index),
2633 calling_convention.GetRegisterAt(index + 1));
2634 } else {
2635 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
2636 }
2637 }
2638
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002639 case DataType::Type::kFloat32: {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002640 uint32_t stack_index = stack_index_++;
2641 if (float_index_ % 2 == 0) {
2642 float_index_ = std::max(double_index_, float_index_);
2643 }
2644 if (float_index_ < calling_convention.GetNumberOfFpuRegisters()) {
2645 return LocationFrom(calling_convention.GetFpuRegisterAt(float_index_++));
2646 } else {
2647 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
2648 }
2649 }
2650
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002651 case DataType::Type::kFloat64: {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002652 double_index_ = std::max(double_index_, RoundUp(float_index_, 2));
2653 uint32_t stack_index = stack_index_;
2654 stack_index_ += 2;
2655 if (double_index_ + 1 < calling_convention.GetNumberOfFpuRegisters()) {
2656 uint32_t index = double_index_;
2657 double_index_ += 2;
2658 Location result = LocationFrom(
2659 calling_convention.GetFpuRegisterAt(index),
2660 calling_convention.GetFpuRegisterAt(index + 1));
2661 DCHECK(ExpectedPairLayout(result));
2662 return result;
2663 } else {
2664 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
2665 }
2666 }
2667
Aart Bik66c158e2018-01-31 12:55:04 -08002668 case DataType::Type::kUint32:
2669 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002670 case DataType::Type::kVoid:
Artem Serovd4cc5b22016-11-04 11:19:09 +00002671 LOG(FATAL) << "Unexpected parameter type " << type;
2672 break;
2673 }
2674 return Location::NoLocation();
2675}
2676
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002677Location InvokeDexCallingConventionVisitorARMVIXL::GetReturnLocation(DataType::Type type) const {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002678 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002679 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002680 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002681 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002682 case DataType::Type::kInt8:
2683 case DataType::Type::kUint16:
2684 case DataType::Type::kInt16:
Aart Bik66c158e2018-01-31 12:55:04 -08002685 case DataType::Type::kUint32:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002686 case DataType::Type::kInt32: {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002687 return LocationFrom(r0);
2688 }
2689
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002690 case DataType::Type::kFloat32: {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002691 return LocationFrom(s0);
2692 }
2693
Aart Bik66c158e2018-01-31 12:55:04 -08002694 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002695 case DataType::Type::kInt64: {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002696 return LocationFrom(r0, r1);
2697 }
2698
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002699 case DataType::Type::kFloat64: {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002700 return LocationFrom(s0, s1);
2701 }
2702
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002703 case DataType::Type::kVoid:
Artem Serovd4cc5b22016-11-04 11:19:09 +00002704 return Location::NoLocation();
2705 }
2706
2707 UNREACHABLE();
2708}
2709
2710Location InvokeDexCallingConventionVisitorARMVIXL::GetMethodLocation() const {
2711 return LocationFrom(kMethodRegister);
2712}
2713
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002714void CodeGeneratorARMVIXL::Move32(Location destination, Location source) {
2715 if (source.Equals(destination)) {
2716 return;
2717 }
2718 if (destination.IsRegister()) {
2719 if (source.IsRegister()) {
2720 __ Mov(RegisterFrom(destination), RegisterFrom(source));
2721 } else if (source.IsFpuRegister()) {
2722 __ Vmov(RegisterFrom(destination), SRegisterFrom(source));
2723 } else {
2724 GetAssembler()->LoadFromOffset(kLoadWord,
2725 RegisterFrom(destination),
2726 sp,
2727 source.GetStackIndex());
2728 }
2729 } else if (destination.IsFpuRegister()) {
2730 if (source.IsRegister()) {
2731 __ Vmov(SRegisterFrom(destination), RegisterFrom(source));
2732 } else if (source.IsFpuRegister()) {
2733 __ Vmov(SRegisterFrom(destination), SRegisterFrom(source));
2734 } else {
2735 GetAssembler()->LoadSFromOffset(SRegisterFrom(destination), sp, source.GetStackIndex());
2736 }
2737 } else {
2738 DCHECK(destination.IsStackSlot()) << destination;
2739 if (source.IsRegister()) {
2740 GetAssembler()->StoreToOffset(kStoreWord,
2741 RegisterFrom(source),
2742 sp,
2743 destination.GetStackIndex());
2744 } else if (source.IsFpuRegister()) {
2745 GetAssembler()->StoreSToOffset(SRegisterFrom(source), sp, destination.GetStackIndex());
2746 } else {
2747 DCHECK(source.IsStackSlot()) << source;
2748 UseScratchRegisterScope temps(GetVIXLAssembler());
2749 vixl32::Register temp = temps.Acquire();
2750 GetAssembler()->LoadFromOffset(kLoadWord, temp, sp, source.GetStackIndex());
2751 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
2752 }
2753 }
2754}
2755
Artem Serovcfbe9132016-10-14 15:58:56 +01002756void CodeGeneratorARMVIXL::MoveConstant(Location location, int32_t value) {
2757 DCHECK(location.IsRegister());
2758 __ Mov(RegisterFrom(location), value);
Scott Wakelingfe885462016-09-22 10:24:38 +01002759}
2760
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002761void CodeGeneratorARMVIXL::MoveLocation(Location dst, Location src, DataType::Type dst_type) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002762 // TODO(VIXL): Maybe refactor to have the 'move' implementation here and use it in
2763 // `ParallelMoveResolverARMVIXL::EmitMove`, as is done in the `arm64` backend.
Vladimir Markoca6fff82017-10-03 14:49:14 +01002764 HParallelMove move(GetGraph()->GetAllocator());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002765 move.AddMove(src, dst, dst_type, nullptr);
2766 GetMoveResolver()->EmitNativeCode(&move);
Scott Wakelingfe885462016-09-22 10:24:38 +01002767}
2768
Artem Serovcfbe9132016-10-14 15:58:56 +01002769void CodeGeneratorARMVIXL::AddLocationAsTemp(Location location, LocationSummary* locations) {
2770 if (location.IsRegister()) {
2771 locations->AddTemp(location);
2772 } else if (location.IsRegisterPair()) {
2773 locations->AddTemp(LocationFrom(LowRegisterFrom(location)));
2774 locations->AddTemp(LocationFrom(HighRegisterFrom(location)));
2775 } else {
2776 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
2777 }
Scott Wakelingfe885462016-09-22 10:24:38 +01002778}
2779
2780void CodeGeneratorARMVIXL::InvokeRuntime(QuickEntrypointEnum entrypoint,
2781 HInstruction* instruction,
2782 uint32_t dex_pc,
2783 SlowPathCode* slow_path) {
2784 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Alexandre Rames374ddf32016-11-04 10:40:49 +00002785 __ Ldr(lr, MemOperand(tr, GetThreadOffset<kArmPointerSize>(entrypoint).Int32Value()));
2786 // Ensure the pc position is recorded immediately after the `blx` instruction.
2787 // 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 +00002788 ExactAssemblyScope aas(GetVIXLAssembler(),
2789 vixl32::k16BitT32InstructionSizeInBytes,
2790 CodeBufferCheckScope::kExactSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00002791 __ blx(lr);
Scott Wakelingfe885462016-09-22 10:24:38 +01002792 if (EntrypointRequiresStackMap(entrypoint)) {
2793 RecordPcInfo(instruction, dex_pc, slow_path);
2794 }
2795}
2796
2797void CodeGeneratorARMVIXL::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
2798 HInstruction* instruction,
2799 SlowPathCode* slow_path) {
2800 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Alexandre Rames374ddf32016-11-04 10:40:49 +00002801 __ Ldr(lr, MemOperand(tr, entry_point_offset));
Scott Wakelingfe885462016-09-22 10:24:38 +01002802 __ Blx(lr);
2803}
2804
Scott Wakelingfe885462016-09-22 10:24:38 +01002805void InstructionCodeGeneratorARMVIXL::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Aart Bika8b8e9b2018-01-09 11:01:02 -08002806 if (successor->IsExitBlock()) {
2807 DCHECK(got->GetPrevious()->AlwaysThrows());
2808 return; // no code needed
2809 }
2810
Scott Wakelingfe885462016-09-22 10:24:38 +01002811 HBasicBlock* block = got->GetBlock();
2812 HInstruction* previous = got->GetPrevious();
2813 HLoopInformation* info = block->GetLoopInformation();
2814
2815 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray8d728322018-01-18 22:44:32 +00002816 if (codegen_->GetCompilerOptions().CountHotnessInCompiledCode()) {
2817 UseScratchRegisterScope temps(GetVIXLAssembler());
2818 vixl32::Register temp = temps.Acquire();
2819 __ Push(vixl32::Register(kMethodRegister));
2820 GetAssembler()->LoadFromOffset(kLoadWord, kMethodRegister, sp, kArmWordSize);
2821 __ Ldrh(temp, MemOperand(kMethodRegister, ArtMethod::HotnessCountOffset().Int32Value()));
2822 __ Add(temp, temp, 1);
2823 __ Strh(temp, MemOperand(kMethodRegister, ArtMethod::HotnessCountOffset().Int32Value()));
2824 __ Pop(vixl32::Register(kMethodRegister));
2825 }
Scott Wakelingfe885462016-09-22 10:24:38 +01002826 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2827 return;
2828 }
2829 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2830 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
Roland Levillain5daa4952017-07-03 17:23:56 +01002831 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 2);
Scott Wakelingfe885462016-09-22 10:24:38 +01002832 }
2833 if (!codegen_->GoesToNextBlock(block, successor)) {
2834 __ B(codegen_->GetLabelOf(successor));
2835 }
2836}
2837
2838void LocationsBuilderARMVIXL::VisitGoto(HGoto* got) {
2839 got->SetLocations(nullptr);
2840}
2841
2842void InstructionCodeGeneratorARMVIXL::VisitGoto(HGoto* got) {
2843 HandleGoto(got, got->GetSuccessor());
2844}
2845
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002846void LocationsBuilderARMVIXL::VisitTryBoundary(HTryBoundary* try_boundary) {
2847 try_boundary->SetLocations(nullptr);
2848}
2849
2850void InstructionCodeGeneratorARMVIXL::VisitTryBoundary(HTryBoundary* try_boundary) {
2851 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2852 if (!successor->IsExitBlock()) {
2853 HandleGoto(try_boundary, successor);
2854 }
2855}
2856
Scott Wakelingfe885462016-09-22 10:24:38 +01002857void LocationsBuilderARMVIXL::VisitExit(HExit* exit) {
2858 exit->SetLocations(nullptr);
2859}
2860
2861void InstructionCodeGeneratorARMVIXL::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
2862}
2863
Scott Wakelingfe885462016-09-22 10:24:38 +01002864void InstructionCodeGeneratorARMVIXL::GenerateCompareTestAndBranch(HCondition* condition,
Anton Kirilov23b752b2017-07-20 14:40:44 +01002865 vixl32::Label* true_target,
2866 vixl32::Label* false_target,
Anton Kirilovfd522532017-05-10 12:46:57 +01002867 bool is_far_target) {
Anton Kirilov23b752b2017-07-20 14:40:44 +01002868 if (true_target == false_target) {
2869 DCHECK(true_target != nullptr);
2870 __ B(true_target);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002871 return;
2872 }
2873
Anton Kirilov23b752b2017-07-20 14:40:44 +01002874 vixl32::Label* non_fallthrough_target;
2875 bool invert;
2876 bool emit_both_branches;
Scott Wakelingfe885462016-09-22 10:24:38 +01002877
Anton Kirilov23b752b2017-07-20 14:40:44 +01002878 if (true_target == nullptr) {
2879 // The true target is fallthrough.
2880 DCHECK(false_target != nullptr);
2881 non_fallthrough_target = false_target;
2882 invert = true;
2883 emit_both_branches = false;
2884 } else {
2885 non_fallthrough_target = true_target;
2886 invert = false;
2887 // Either the false target is fallthrough, or there is no fallthrough
2888 // and both branches must be emitted.
2889 emit_both_branches = (false_target != nullptr);
Scott Wakelingfe885462016-09-22 10:24:38 +01002890 }
2891
Anton Kirilov23b752b2017-07-20 14:40:44 +01002892 const auto cond = GenerateTest(condition, invert, codegen_);
2893
2894 __ B(cond.first, non_fallthrough_target, is_far_target);
2895
2896 if (emit_both_branches) {
2897 // No target falls through, we need to branch.
2898 __ B(false_target);
Scott Wakelingfe885462016-09-22 10:24:38 +01002899 }
2900}
2901
2902void InstructionCodeGeneratorARMVIXL::GenerateTestAndBranch(HInstruction* instruction,
2903 size_t condition_input_index,
2904 vixl32::Label* true_target,
xueliang.zhongf51bc622016-11-04 09:23:32 +00002905 vixl32::Label* false_target,
2906 bool far_target) {
Scott Wakelingfe885462016-09-22 10:24:38 +01002907 HInstruction* cond = instruction->InputAt(condition_input_index);
2908
2909 if (true_target == nullptr && false_target == nullptr) {
2910 // Nothing to do. The code always falls through.
2911 return;
2912 } else if (cond->IsIntConstant()) {
2913 // Constant condition, statically compared against "true" (integer value 1).
2914 if (cond->AsIntConstant()->IsTrue()) {
2915 if (true_target != nullptr) {
2916 __ B(true_target);
2917 }
2918 } else {
Anton Kirilov644032c2016-12-06 17:51:43 +00002919 DCHECK(cond->AsIntConstant()->IsFalse()) << Int32ConstantFrom(cond);
Scott Wakelingfe885462016-09-22 10:24:38 +01002920 if (false_target != nullptr) {
2921 __ B(false_target);
2922 }
2923 }
2924 return;
2925 }
2926
2927 // The following code generates these patterns:
2928 // (1) true_target == nullptr && false_target != nullptr
2929 // - opposite condition true => branch to false_target
2930 // (2) true_target != nullptr && false_target == nullptr
2931 // - condition true => branch to true_target
2932 // (3) true_target != nullptr && false_target != nullptr
2933 // - condition true => branch to true_target
2934 // - branch to false_target
2935 if (IsBooleanValueOrMaterializedCondition(cond)) {
2936 // Condition has been materialized, compare the output to 0.
2937 if (kIsDebugBuild) {
2938 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
2939 DCHECK(cond_val.IsRegister());
2940 }
2941 if (true_target == nullptr) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00002942 __ CompareAndBranchIfZero(InputRegisterAt(instruction, condition_input_index),
2943 false_target,
2944 far_target);
Scott Wakelingfe885462016-09-22 10:24:38 +01002945 } else {
xueliang.zhongf51bc622016-11-04 09:23:32 +00002946 __ CompareAndBranchIfNonZero(InputRegisterAt(instruction, condition_input_index),
2947 true_target,
2948 far_target);
Scott Wakelingfe885462016-09-22 10:24:38 +01002949 }
2950 } else {
2951 // Condition has not been materialized. Use its inputs as the comparison and
2952 // its condition as the branch condition.
2953 HCondition* condition = cond->AsCondition();
2954
2955 // If this is a long or FP comparison that has been folded into
2956 // the HCondition, generate the comparison directly.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002957 DataType::Type type = condition->InputAt(0)->GetType();
2958 if (type == DataType::Type::kInt64 || DataType::IsFloatingPointType(type)) {
Anton Kirilovfd522532017-05-10 12:46:57 +01002959 GenerateCompareTestAndBranch(condition, true_target, false_target, far_target);
Scott Wakelingfe885462016-09-22 10:24:38 +01002960 return;
2961 }
2962
Donghui Bai426b49c2016-11-08 14:55:38 +08002963 vixl32::Label* non_fallthrough_target;
2964 vixl32::Condition arm_cond = vixl32::Condition::None();
2965 const vixl32::Register left = InputRegisterAt(cond, 0);
2966 const Operand right = InputOperandAt(cond, 1);
2967
Scott Wakelingfe885462016-09-22 10:24:38 +01002968 if (true_target == nullptr) {
Donghui Bai426b49c2016-11-08 14:55:38 +08002969 arm_cond = ARMCondition(condition->GetOppositeCondition());
2970 non_fallthrough_target = false_target;
Scott Wakelingfe885462016-09-22 10:24:38 +01002971 } else {
Donghui Bai426b49c2016-11-08 14:55:38 +08002972 arm_cond = ARMCondition(condition->GetCondition());
2973 non_fallthrough_target = true_target;
2974 }
2975
2976 if (right.IsImmediate() && right.GetImmediate() == 0 && (arm_cond.Is(ne) || arm_cond.Is(eq))) {
2977 if (arm_cond.Is(eq)) {
Anton Kirilovfd522532017-05-10 12:46:57 +01002978 __ CompareAndBranchIfZero(left, non_fallthrough_target, far_target);
Donghui Bai426b49c2016-11-08 14:55:38 +08002979 } else {
2980 DCHECK(arm_cond.Is(ne));
Anton Kirilovfd522532017-05-10 12:46:57 +01002981 __ CompareAndBranchIfNonZero(left, non_fallthrough_target, far_target);
Donghui Bai426b49c2016-11-08 14:55:38 +08002982 }
2983 } else {
2984 __ Cmp(left, right);
Anton Kirilovfd522532017-05-10 12:46:57 +01002985 __ B(arm_cond, non_fallthrough_target, far_target);
Scott Wakelingfe885462016-09-22 10:24:38 +01002986 }
2987 }
2988
2989 // If neither branch falls through (case 3), the conditional branch to `true_target`
2990 // was already emitted (case 2) and we need to emit a jump to `false_target`.
2991 if (true_target != nullptr && false_target != nullptr) {
2992 __ B(false_target);
2993 }
2994}
2995
2996void LocationsBuilderARMVIXL::VisitIf(HIf* if_instr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002997 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(if_instr);
Scott Wakelingfe885462016-09-22 10:24:38 +01002998 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
2999 locations->SetInAt(0, Location::RequiresRegister());
3000 }
3001}
3002
3003void InstructionCodeGeneratorARMVIXL::VisitIf(HIf* if_instr) {
3004 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
3005 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003006 vixl32::Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
3007 nullptr : codegen_->GetLabelOf(true_successor);
3008 vixl32::Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
3009 nullptr : codegen_->GetLabelOf(false_successor);
Scott Wakelingfe885462016-09-22 10:24:38 +01003010 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
3011}
3012
Scott Wakelingc34dba72016-10-03 10:14:44 +01003013void LocationsBuilderARMVIXL::VisitDeoptimize(HDeoptimize* deoptimize) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003014 LocationSummary* locations = new (GetGraph()->GetAllocator())
Scott Wakelingc34dba72016-10-03 10:14:44 +01003015 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003016 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3017 RegisterSet caller_saves = RegisterSet::Empty();
3018 caller_saves.Add(LocationFrom(calling_convention.GetRegisterAt(0)));
3019 locations->SetCustomSlowPathCallerSaves(caller_saves);
Scott Wakelingc34dba72016-10-03 10:14:44 +01003020 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
3021 locations->SetInAt(0, Location::RequiresRegister());
3022 }
3023}
3024
3025void InstructionCodeGeneratorARMVIXL::VisitDeoptimize(HDeoptimize* deoptimize) {
3026 SlowPathCodeARMVIXL* slow_path =
3027 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARMVIXL>(deoptimize);
3028 GenerateTestAndBranch(deoptimize,
3029 /* condition_input_index */ 0,
3030 slow_path->GetEntryLabel(),
3031 /* false_target */ nullptr);
3032}
3033
Artem Serovd4cc5b22016-11-04 11:19:09 +00003034void LocationsBuilderARMVIXL::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003035 LocationSummary* locations = new (GetGraph()->GetAllocator())
Artem Serovd4cc5b22016-11-04 11:19:09 +00003036 LocationSummary(flag, LocationSummary::kNoCall);
3037 locations->SetOut(Location::RequiresRegister());
3038}
3039
3040void InstructionCodeGeneratorARMVIXL::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
3041 GetAssembler()->LoadFromOffset(kLoadWord,
3042 OutputRegister(flag),
3043 sp,
3044 codegen_->GetStackOffsetOfShouldDeoptimizeFlag());
3045}
3046
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003047void LocationsBuilderARMVIXL::VisitSelect(HSelect* select) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003048 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(select);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003049 const bool is_floating_point = DataType::IsFloatingPointType(select->GetType());
Donghui Bai426b49c2016-11-08 14:55:38 +08003050
3051 if (is_floating_point) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003052 locations->SetInAt(0, Location::RequiresFpuRegister());
Donghui Bai426b49c2016-11-08 14:55:38 +08003053 locations->SetInAt(1, Location::FpuRegisterOrConstant(select->GetTrueValue()));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003054 } else {
3055 locations->SetInAt(0, Location::RequiresRegister());
Donghui Bai426b49c2016-11-08 14:55:38 +08003056 locations->SetInAt(1, Arm8BitEncodableConstantOrRegister(select->GetTrueValue()));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003057 }
Donghui Bai426b49c2016-11-08 14:55:38 +08003058
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003059 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
Donghui Bai426b49c2016-11-08 14:55:38 +08003060 locations->SetInAt(2, Location::RegisterOrConstant(select->GetCondition()));
3061 // The code generator handles overlap with the values, but not with the condition.
3062 locations->SetOut(Location::SameAsFirstInput());
3063 } else if (is_floating_point) {
3064 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3065 } else {
3066 if (!locations->InAt(1).IsConstant()) {
3067 locations->SetInAt(0, Arm8BitEncodableConstantOrRegister(select->GetFalseValue()));
3068 }
3069
3070 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003071 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003072}
3073
3074void InstructionCodeGeneratorARMVIXL::VisitSelect(HSelect* select) {
Donghui Bai426b49c2016-11-08 14:55:38 +08003075 HInstruction* const condition = select->GetCondition();
3076 const LocationSummary* const locations = select->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003077 const DataType::Type type = select->GetType();
Donghui Bai426b49c2016-11-08 14:55:38 +08003078 const Location first = locations->InAt(0);
3079 const Location out = locations->Out();
3080 const Location second = locations->InAt(1);
3081 Location src;
3082
3083 if (condition->IsIntConstant()) {
3084 if (condition->AsIntConstant()->IsFalse()) {
3085 src = first;
3086 } else {
3087 src = second;
3088 }
3089
3090 codegen_->MoveLocation(out, src, type);
3091 return;
3092 }
3093
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003094 if (!DataType::IsFloatingPointType(type)) {
Donghui Bai426b49c2016-11-08 14:55:38 +08003095 bool invert = false;
3096
3097 if (out.Equals(second)) {
3098 src = first;
3099 invert = true;
3100 } else if (out.Equals(first)) {
3101 src = second;
3102 } else if (second.IsConstant()) {
3103 DCHECK(CanEncodeConstantAs8BitImmediate(second.GetConstant()));
3104 src = second;
3105 } else if (first.IsConstant()) {
3106 DCHECK(CanEncodeConstantAs8BitImmediate(first.GetConstant()));
3107 src = first;
3108 invert = true;
3109 } else {
3110 src = second;
3111 }
3112
3113 if (CanGenerateConditionalMove(out, src)) {
3114 if (!out.Equals(first) && !out.Equals(second)) {
3115 codegen_->MoveLocation(out, src.Equals(first) ? second : first, type);
3116 }
3117
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003118 std::pair<vixl32::Condition, vixl32::Condition> cond(eq, ne);
3119
3120 if (IsBooleanValueOrMaterializedCondition(condition)) {
3121 __ Cmp(InputRegisterAt(select, 2), 0);
3122 cond = invert ? std::make_pair(eq, ne) : std::make_pair(ne, eq);
3123 } else {
3124 cond = GenerateTest(condition->AsCondition(), invert, codegen_);
3125 }
3126
Donghui Bai426b49c2016-11-08 14:55:38 +08003127 const size_t instr_count = out.IsRegisterPair() ? 4 : 2;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003128 // We use the scope because of the IT block that follows.
Donghui Bai426b49c2016-11-08 14:55:38 +08003129 ExactAssemblyScope guard(GetVIXLAssembler(),
3130 instr_count * vixl32::k16BitT32InstructionSizeInBytes,
3131 CodeBufferCheckScope::kExactSize);
3132
3133 if (out.IsRegister()) {
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003134 __ it(cond.first);
3135 __ mov(cond.first, RegisterFrom(out), OperandFrom(src, type));
Donghui Bai426b49c2016-11-08 14:55:38 +08003136 } else {
3137 DCHECK(out.IsRegisterPair());
3138
3139 Operand operand_high(0);
3140 Operand operand_low(0);
3141
3142 if (src.IsConstant()) {
3143 const int64_t value = Int64ConstantFrom(src);
3144
3145 operand_high = High32Bits(value);
3146 operand_low = Low32Bits(value);
3147 } else {
3148 DCHECK(src.IsRegisterPair());
3149 operand_high = HighRegisterFrom(src);
3150 operand_low = LowRegisterFrom(src);
3151 }
3152
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003153 __ it(cond.first);
3154 __ mov(cond.first, LowRegisterFrom(out), operand_low);
3155 __ it(cond.first);
3156 __ mov(cond.first, HighRegisterFrom(out), operand_high);
Donghui Bai426b49c2016-11-08 14:55:38 +08003157 }
3158
3159 return;
3160 }
3161 }
3162
3163 vixl32::Label* false_target = nullptr;
3164 vixl32::Label* true_target = nullptr;
3165 vixl32::Label select_end;
3166 vixl32::Label* const target = codegen_->GetFinalLabel(select, &select_end);
3167
3168 if (out.Equals(second)) {
3169 true_target = target;
3170 src = first;
3171 } else {
3172 false_target = target;
3173 src = second;
3174
3175 if (!out.Equals(first)) {
3176 codegen_->MoveLocation(out, first, type);
3177 }
3178 }
3179
3180 GenerateTestAndBranch(select, 2, true_target, false_target, /* far_target */ false);
3181 codegen_->MoveLocation(out, src, type);
3182
3183 if (select_end.IsReferenced()) {
3184 __ Bind(&select_end);
3185 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003186}
3187
Artem Serov551b28f2016-10-18 19:11:30 +01003188void LocationsBuilderARMVIXL::VisitNativeDebugInfo(HNativeDebugInfo* info) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003189 new (GetGraph()->GetAllocator()) LocationSummary(info);
Artem Serov551b28f2016-10-18 19:11:30 +01003190}
3191
3192void InstructionCodeGeneratorARMVIXL::VisitNativeDebugInfo(HNativeDebugInfo*) {
3193 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
3194}
3195
Scott Wakelingfe885462016-09-22 10:24:38 +01003196void CodeGeneratorARMVIXL::GenerateNop() {
3197 __ Nop();
3198}
3199
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003200// `temp` is an extra temporary register that is used for some conditions;
3201// callers may not specify it, in which case the method will use a scratch
3202// register instead.
3203void CodeGeneratorARMVIXL::GenerateConditionWithZero(IfCondition condition,
3204 vixl32::Register out,
3205 vixl32::Register in,
3206 vixl32::Register temp) {
3207 switch (condition) {
3208 case kCondEQ:
3209 // x <= 0 iff x == 0 when the comparison is unsigned.
3210 case kCondBE:
3211 if (!temp.IsValid() || (out.IsLow() && !out.Is(in))) {
3212 temp = out;
3213 }
3214
3215 // Avoid 32-bit instructions if possible; note that `in` and `temp` must be
3216 // different as well.
3217 if (in.IsLow() && temp.IsLow() && !in.Is(temp)) {
3218 // temp = - in; only 0 sets the carry flag.
3219 __ Rsbs(temp, in, 0);
3220
3221 if (out.Is(in)) {
3222 std::swap(in, temp);
3223 }
3224
3225 // out = - in + in + carry = carry
3226 __ Adc(out, temp, in);
3227 } else {
3228 // If `in` is 0, then it has 32 leading zeros, and less than that otherwise.
3229 __ Clz(out, in);
3230 // Any number less than 32 logically shifted right by 5 bits results in 0;
3231 // the same operation on 32 yields 1.
3232 __ Lsr(out, out, 5);
3233 }
3234
3235 break;
3236 case kCondNE:
3237 // x > 0 iff x != 0 when the comparison is unsigned.
3238 case kCondA: {
3239 UseScratchRegisterScope temps(GetVIXLAssembler());
3240
3241 if (out.Is(in)) {
3242 if (!temp.IsValid() || in.Is(temp)) {
3243 temp = temps.Acquire();
3244 }
3245 } else if (!temp.IsValid() || !temp.IsLow()) {
3246 temp = out;
3247 }
3248
3249 // temp = in - 1; only 0 does not set the carry flag.
3250 __ Subs(temp, in, 1);
3251 // out = in + ~temp + carry = in + (-(in - 1) - 1) + carry = in - in + 1 - 1 + carry = carry
3252 __ Sbc(out, in, temp);
3253 break;
3254 }
3255 case kCondGE:
3256 __ Mvn(out, in);
3257 in = out;
3258 FALLTHROUGH_INTENDED;
3259 case kCondLT:
3260 // We only care about the sign bit.
3261 __ Lsr(out, in, 31);
3262 break;
3263 case kCondAE:
3264 // Trivially true.
3265 __ Mov(out, 1);
3266 break;
3267 case kCondB:
3268 // Trivially false.
3269 __ Mov(out, 0);
3270 break;
3271 default:
3272 LOG(FATAL) << "Unexpected condition " << condition;
3273 UNREACHABLE();
3274 }
3275}
3276
Scott Wakelingfe885462016-09-22 10:24:38 +01003277void LocationsBuilderARMVIXL::HandleCondition(HCondition* cond) {
3278 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003279 new (GetGraph()->GetAllocator()) LocationSummary(cond, LocationSummary::kNoCall);
Scott Wakelingfe885462016-09-22 10:24:38 +01003280 // Handle the long/FP comparisons made in instruction simplification.
3281 switch (cond->InputAt(0)->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003282 case DataType::Type::kInt64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003283 locations->SetInAt(0, Location::RequiresRegister());
3284 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
3285 if (!cond->IsEmittedAtUseSite()) {
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003286 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Scott Wakelingfe885462016-09-22 10:24:38 +01003287 }
3288 break;
3289
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003290 case DataType::Type::kFloat32:
3291 case DataType::Type::kFloat64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003292 locations->SetInAt(0, Location::RequiresFpuRegister());
Artem Serov657022c2016-11-23 14:19:38 +00003293 locations->SetInAt(1, ArithmeticZeroOrFpuRegister(cond->InputAt(1)));
Scott Wakelingfe885462016-09-22 10:24:38 +01003294 if (!cond->IsEmittedAtUseSite()) {
3295 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3296 }
3297 break;
3298
3299 default:
3300 locations->SetInAt(0, Location::RequiresRegister());
3301 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
3302 if (!cond->IsEmittedAtUseSite()) {
3303 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3304 }
3305 }
3306}
3307
3308void InstructionCodeGeneratorARMVIXL::HandleCondition(HCondition* cond) {
3309 if (cond->IsEmittedAtUseSite()) {
3310 return;
3311 }
3312
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003313 const DataType::Type type = cond->GetLeft()->GetType();
Scott Wakelingfe885462016-09-22 10:24:38 +01003314
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003315 if (DataType::IsFloatingPointType(type)) {
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003316 GenerateConditionGeneric(cond, codegen_);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003317 return;
Scott Wakelingfe885462016-09-22 10:24:38 +01003318 }
3319
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003320 DCHECK(DataType::IsIntegralType(type) || type == DataType::Type::kReference) << type;
Scott Wakelingfe885462016-09-22 10:24:38 +01003321
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003322 const IfCondition condition = cond->GetCondition();
Scott Wakelingfe885462016-09-22 10:24:38 +01003323
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003324 // A condition with only one boolean input, or two boolean inputs without being equality or
3325 // inequality results from transformations done by the instruction simplifier, and is handled
3326 // as a regular condition with integral inputs.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003327 if (type == DataType::Type::kBool &&
3328 cond->GetRight()->GetType() == DataType::Type::kBool &&
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003329 (condition == kCondEQ || condition == kCondNE)) {
3330 vixl32::Register left = InputRegisterAt(cond, 0);
3331 const vixl32::Register out = OutputRegister(cond);
3332 const Location right_loc = cond->GetLocations()->InAt(1);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003333
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003334 // The constant case is handled by the instruction simplifier.
3335 DCHECK(!right_loc.IsConstant());
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003336
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003337 vixl32::Register right = RegisterFrom(right_loc);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003338
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003339 // Avoid 32-bit instructions if possible.
3340 if (out.Is(right)) {
3341 std::swap(left, right);
3342 }
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003343
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003344 __ Eor(out, left, right);
3345
3346 if (condition == kCondEQ) {
3347 __ Eor(out, out, 1);
3348 }
3349
3350 return;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003351 }
Anton Kirilov6f644202017-02-27 18:29:45 +00003352
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003353 GenerateConditionIntegralOrNonPrimitive(cond, codegen_);
Scott Wakelingfe885462016-09-22 10:24:38 +01003354}
3355
3356void LocationsBuilderARMVIXL::VisitEqual(HEqual* comp) {
3357 HandleCondition(comp);
3358}
3359
3360void InstructionCodeGeneratorARMVIXL::VisitEqual(HEqual* comp) {
3361 HandleCondition(comp);
3362}
3363
3364void LocationsBuilderARMVIXL::VisitNotEqual(HNotEqual* comp) {
3365 HandleCondition(comp);
3366}
3367
3368void InstructionCodeGeneratorARMVIXL::VisitNotEqual(HNotEqual* comp) {
3369 HandleCondition(comp);
3370}
3371
3372void LocationsBuilderARMVIXL::VisitLessThan(HLessThan* comp) {
3373 HandleCondition(comp);
3374}
3375
3376void InstructionCodeGeneratorARMVIXL::VisitLessThan(HLessThan* comp) {
3377 HandleCondition(comp);
3378}
3379
3380void LocationsBuilderARMVIXL::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
3381 HandleCondition(comp);
3382}
3383
3384void InstructionCodeGeneratorARMVIXL::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
3385 HandleCondition(comp);
3386}
3387
3388void LocationsBuilderARMVIXL::VisitGreaterThan(HGreaterThan* comp) {
3389 HandleCondition(comp);
3390}
3391
3392void InstructionCodeGeneratorARMVIXL::VisitGreaterThan(HGreaterThan* comp) {
3393 HandleCondition(comp);
3394}
3395
3396void LocationsBuilderARMVIXL::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
3397 HandleCondition(comp);
3398}
3399
3400void InstructionCodeGeneratorARMVIXL::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
3401 HandleCondition(comp);
3402}
3403
3404void LocationsBuilderARMVIXL::VisitBelow(HBelow* comp) {
3405 HandleCondition(comp);
3406}
3407
3408void InstructionCodeGeneratorARMVIXL::VisitBelow(HBelow* comp) {
3409 HandleCondition(comp);
3410}
3411
3412void LocationsBuilderARMVIXL::VisitBelowOrEqual(HBelowOrEqual* comp) {
3413 HandleCondition(comp);
3414}
3415
3416void InstructionCodeGeneratorARMVIXL::VisitBelowOrEqual(HBelowOrEqual* comp) {
3417 HandleCondition(comp);
3418}
3419
3420void LocationsBuilderARMVIXL::VisitAbove(HAbove* comp) {
3421 HandleCondition(comp);
3422}
3423
3424void InstructionCodeGeneratorARMVIXL::VisitAbove(HAbove* comp) {
3425 HandleCondition(comp);
3426}
3427
3428void LocationsBuilderARMVIXL::VisitAboveOrEqual(HAboveOrEqual* comp) {
3429 HandleCondition(comp);
3430}
3431
3432void InstructionCodeGeneratorARMVIXL::VisitAboveOrEqual(HAboveOrEqual* comp) {
3433 HandleCondition(comp);
3434}
3435
3436void LocationsBuilderARMVIXL::VisitIntConstant(HIntConstant* constant) {
3437 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003438 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Scott Wakelingfe885462016-09-22 10:24:38 +01003439 locations->SetOut(Location::ConstantLocation(constant));
3440}
3441
3442void InstructionCodeGeneratorARMVIXL::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
3443 // Will be generated at use site.
3444}
3445
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003446void LocationsBuilderARMVIXL::VisitNullConstant(HNullConstant* constant) {
3447 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003448 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003449 locations->SetOut(Location::ConstantLocation(constant));
3450}
3451
3452void InstructionCodeGeneratorARMVIXL::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
3453 // Will be generated at use site.
3454}
3455
Scott Wakelingfe885462016-09-22 10:24:38 +01003456void LocationsBuilderARMVIXL::VisitLongConstant(HLongConstant* constant) {
3457 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003458 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Scott Wakelingfe885462016-09-22 10:24:38 +01003459 locations->SetOut(Location::ConstantLocation(constant));
3460}
3461
3462void InstructionCodeGeneratorARMVIXL::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
3463 // Will be generated at use site.
3464}
3465
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01003466void LocationsBuilderARMVIXL::VisitFloatConstant(HFloatConstant* constant) {
3467 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003468 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01003469 locations->SetOut(Location::ConstantLocation(constant));
3470}
3471
Scott Wakelingc34dba72016-10-03 10:14:44 +01003472void InstructionCodeGeneratorARMVIXL::VisitFloatConstant(
3473 HFloatConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01003474 // Will be generated at use site.
3475}
3476
3477void LocationsBuilderARMVIXL::VisitDoubleConstant(HDoubleConstant* constant) {
3478 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003479 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01003480 locations->SetOut(Location::ConstantLocation(constant));
3481}
3482
Scott Wakelingc34dba72016-10-03 10:14:44 +01003483void InstructionCodeGeneratorARMVIXL::VisitDoubleConstant(
3484 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01003485 // Will be generated at use site.
3486}
3487
Igor Murashkind01745e2017-04-05 16:40:31 -07003488void LocationsBuilderARMVIXL::VisitConstructorFence(HConstructorFence* constructor_fence) {
3489 constructor_fence->SetLocations(nullptr);
3490}
3491
3492void InstructionCodeGeneratorARMVIXL::VisitConstructorFence(
3493 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
3494 codegen_->GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
3495}
3496
Scott Wakelingfe885462016-09-22 10:24:38 +01003497void LocationsBuilderARMVIXL::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3498 memory_barrier->SetLocations(nullptr);
3499}
3500
3501void InstructionCodeGeneratorARMVIXL::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3502 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
3503}
3504
3505void LocationsBuilderARMVIXL::VisitReturnVoid(HReturnVoid* ret) {
3506 ret->SetLocations(nullptr);
3507}
3508
3509void InstructionCodeGeneratorARMVIXL::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
3510 codegen_->GenerateFrameExit();
3511}
3512
3513void LocationsBuilderARMVIXL::VisitReturn(HReturn* ret) {
3514 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003515 new (GetGraph()->GetAllocator()) LocationSummary(ret, LocationSummary::kNoCall);
Scott Wakelingfe885462016-09-22 10:24:38 +01003516 locations->SetInAt(0, parameter_visitor_.GetReturnLocation(ret->InputAt(0)->GetType()));
3517}
3518
3519void InstructionCodeGeneratorARMVIXL::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
3520 codegen_->GenerateFrameExit();
3521}
3522
Artem Serovcfbe9132016-10-14 15:58:56 +01003523void LocationsBuilderARMVIXL::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3524 // The trampoline uses the same calling convention as dex calling conventions,
3525 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
3526 // the method_idx.
3527 HandleInvoke(invoke);
3528}
3529
3530void InstructionCodeGeneratorARMVIXL::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3531 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
Roland Levillain5daa4952017-07-03 17:23:56 +01003532 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 3);
Artem Serovcfbe9132016-10-14 15:58:56 +01003533}
3534
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003535void LocationsBuilderARMVIXL::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
3536 // Explicit clinit checks triggered by static invokes must have been pruned by
3537 // art::PrepareForRegisterAllocation.
3538 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
3539
Anton Kirilov5ec62182016-10-13 20:16:02 +01003540 IntrinsicLocationsBuilderARMVIXL intrinsic(codegen_);
3541 if (intrinsic.TryDispatch(invoke)) {
Anton Kirilov5ec62182016-10-13 20:16:02 +01003542 return;
3543 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003544
3545 HandleInvoke(invoke);
3546}
3547
Anton Kirilov5ec62182016-10-13 20:16:02 +01003548static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARMVIXL* codegen) {
3549 if (invoke->GetLocations()->Intrinsified()) {
3550 IntrinsicCodeGeneratorARMVIXL intrinsic(codegen);
3551 intrinsic.Dispatch(invoke);
3552 return true;
3553 }
3554 return false;
3555}
3556
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003557void InstructionCodeGeneratorARMVIXL::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
3558 // Explicit clinit checks triggered by static invokes must have been pruned by
3559 // art::PrepareForRegisterAllocation.
3560 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
3561
Anton Kirilov5ec62182016-10-13 20:16:02 +01003562 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
Roland Levillain5daa4952017-07-03 17:23:56 +01003563 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 4);
Anton Kirilov5ec62182016-10-13 20:16:02 +01003564 return;
3565 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003566
3567 LocationSummary* locations = invoke->GetLocations();
Artem Serovd4cc5b22016-11-04 11:19:09 +00003568 codegen_->GenerateStaticOrDirectCall(
3569 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Roland Levillain5daa4952017-07-03 17:23:56 +01003570
3571 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 5);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003572}
3573
3574void LocationsBuilderARMVIXL::HandleInvoke(HInvoke* invoke) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00003575 InvokeDexCallingConventionVisitorARMVIXL calling_convention_visitor;
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003576 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
3577}
3578
3579void LocationsBuilderARMVIXL::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Anton Kirilov5ec62182016-10-13 20:16:02 +01003580 IntrinsicLocationsBuilderARMVIXL intrinsic(codegen_);
3581 if (intrinsic.TryDispatch(invoke)) {
3582 return;
3583 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003584
3585 HandleInvoke(invoke);
3586}
3587
3588void InstructionCodeGeneratorARMVIXL::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Anton Kirilov5ec62182016-10-13 20:16:02 +01003589 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
Roland Levillain5daa4952017-07-03 17:23:56 +01003590 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 6);
Anton Kirilov5ec62182016-10-13 20:16:02 +01003591 return;
3592 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003593
3594 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexandre Rames374ddf32016-11-04 10:40:49 +00003595 DCHECK(!codegen_->IsLeafMethod());
Roland Levillain5daa4952017-07-03 17:23:56 +01003596
3597 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 7);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003598}
3599
Artem Serovcfbe9132016-10-14 15:58:56 +01003600void LocationsBuilderARMVIXL::VisitInvokeInterface(HInvokeInterface* invoke) {
3601 HandleInvoke(invoke);
3602 // Add the hidden argument.
3603 invoke->GetLocations()->AddTemp(LocationFrom(r12));
3604}
3605
3606void InstructionCodeGeneratorARMVIXL::VisitInvokeInterface(HInvokeInterface* invoke) {
3607 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
3608 LocationSummary* locations = invoke->GetLocations();
3609 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
3610 vixl32::Register hidden_reg = RegisterFrom(locations->GetTemp(1));
3611 Location receiver = locations->InAt(0);
3612 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3613
3614 DCHECK(!receiver.IsStackSlot());
3615
Alexandre Rames374ddf32016-11-04 10:40:49 +00003616 // Ensure the pc position is recorded immediately after the `ldr` instruction.
3617 {
Artem Serov0fb37192016-12-06 18:13:40 +00003618 ExactAssemblyScope aas(GetVIXLAssembler(),
3619 vixl32::kMaxInstructionSizeInBytes,
3620 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00003621 // /* HeapReference<Class> */ temp = receiver->klass_
3622 __ ldr(temp, MemOperand(RegisterFrom(receiver), class_offset));
3623 codegen_->MaybeRecordImplicitNullCheck(invoke);
3624 }
Artem Serovcfbe9132016-10-14 15:58:56 +01003625 // Instead of simply (possibly) unpoisoning `temp` here, we should
3626 // emit a read barrier for the previous class reference load.
3627 // However this is not required in practice, as this is an
3628 // intermediate/temporary reference and because the current
3629 // concurrent copying collector keeps the from-space memory
3630 // intact/accessible until the end of the marking phase (the
3631 // concurrent copying collector may not in the future).
3632 GetAssembler()->MaybeUnpoisonHeapReference(temp);
3633 GetAssembler()->LoadFromOffset(kLoadWord,
3634 temp,
3635 temp,
3636 mirror::Class::ImtPtrOffset(kArmPointerSize).Uint32Value());
3637 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
3638 invoke->GetImtIndex(), kArmPointerSize));
3639 // temp = temp->GetImtEntryAt(method_offset);
3640 GetAssembler()->LoadFromOffset(kLoadWord, temp, temp, method_offset);
3641 uint32_t entry_point =
3642 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize).Int32Value();
3643 // LR = temp->GetEntryPoint();
3644 GetAssembler()->LoadFromOffset(kLoadWord, lr, temp, entry_point);
3645
3646 // Set the hidden (in r12) argument. It is done here, right before a BLX to prevent other
3647 // instruction from clobbering it as they might use r12 as a scratch register.
3648 DCHECK(hidden_reg.Is(r12));
Scott Wakelingb77051e2016-11-21 19:46:00 +00003649
3650 {
3651 // The VIXL macro assembler may clobber any of the scratch registers that are available to it,
3652 // so it checks if the application is using them (by passing them to the macro assembler
3653 // methods). The following application of UseScratchRegisterScope corrects VIXL's notion of
3654 // what is available, and is the opposite of the standard usage: Instead of requesting a
3655 // temporary location, it imposes an external constraint (i.e. a specific register is reserved
3656 // for the hidden argument). Note that this works even if VIXL needs a scratch register itself
3657 // (to materialize the constant), since the destination register becomes available for such use
3658 // internally for the duration of the macro instruction.
3659 UseScratchRegisterScope temps(GetVIXLAssembler());
3660 temps.Exclude(hidden_reg);
3661 __ Mov(hidden_reg, invoke->GetDexMethodIndex());
3662 }
Artem Serovcfbe9132016-10-14 15:58:56 +01003663 {
Alexandre Rames374ddf32016-11-04 10:40:49 +00003664 // Ensure the pc position is recorded immediately after the `blx` instruction.
3665 // 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 +00003666 ExactAssemblyScope aas(GetVIXLAssembler(),
Alexandre Rames374ddf32016-11-04 10:40:49 +00003667 vixl32::k16BitT32InstructionSizeInBytes,
3668 CodeBufferCheckScope::kExactSize);
Artem Serovcfbe9132016-10-14 15:58:56 +01003669 // LR();
3670 __ blx(lr);
Artem Serovcfbe9132016-10-14 15:58:56 +01003671 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Alexandre Rames374ddf32016-11-04 10:40:49 +00003672 DCHECK(!codegen_->IsLeafMethod());
Artem Serovcfbe9132016-10-14 15:58:56 +01003673 }
Roland Levillain5daa4952017-07-03 17:23:56 +01003674
3675 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 8);
Artem Serovcfbe9132016-10-14 15:58:56 +01003676}
3677
Orion Hodsonac141392017-01-13 11:53:47 +00003678void LocationsBuilderARMVIXL::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
3679 HandleInvoke(invoke);
3680}
3681
3682void InstructionCodeGeneratorARMVIXL::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
3683 codegen_->GenerateInvokePolymorphicCall(invoke);
Roland Levillain5daa4952017-07-03 17:23:56 +01003684 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 9);
Orion Hodsonac141392017-01-13 11:53:47 +00003685}
3686
Artem Serov02109dd2016-09-23 17:17:54 +01003687void LocationsBuilderARMVIXL::VisitNeg(HNeg* neg) {
3688 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003689 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Artem Serov02109dd2016-09-23 17:17:54 +01003690 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003691 case DataType::Type::kInt32: {
Artem Serov02109dd2016-09-23 17:17:54 +01003692 locations->SetInAt(0, Location::RequiresRegister());
3693 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3694 break;
3695 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003696 case DataType::Type::kInt64: {
Artem Serov02109dd2016-09-23 17:17:54 +01003697 locations->SetInAt(0, Location::RequiresRegister());
3698 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3699 break;
3700 }
3701
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003702 case DataType::Type::kFloat32:
3703 case DataType::Type::kFloat64:
Artem Serov02109dd2016-09-23 17:17:54 +01003704 locations->SetInAt(0, Location::RequiresFpuRegister());
3705 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3706 break;
3707
3708 default:
3709 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
3710 }
3711}
3712
3713void InstructionCodeGeneratorARMVIXL::VisitNeg(HNeg* neg) {
3714 LocationSummary* locations = neg->GetLocations();
3715 Location out = locations->Out();
3716 Location in = locations->InAt(0);
3717 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003718 case DataType::Type::kInt32:
Artem Serov02109dd2016-09-23 17:17:54 +01003719 __ Rsb(OutputRegister(neg), InputRegisterAt(neg, 0), 0);
3720 break;
3721
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003722 case DataType::Type::kInt64:
Artem Serov02109dd2016-09-23 17:17:54 +01003723 // out.lo = 0 - in.lo (and update the carry/borrow (C) flag)
3724 __ Rsbs(LowRegisterFrom(out), LowRegisterFrom(in), 0);
3725 // We cannot emit an RSC (Reverse Subtract with Carry)
3726 // instruction here, as it does not exist in the Thumb-2
3727 // instruction set. We use the following approach
3728 // using SBC and SUB instead.
3729 //
3730 // out.hi = -C
3731 __ Sbc(HighRegisterFrom(out), HighRegisterFrom(out), HighRegisterFrom(out));
3732 // out.hi = out.hi - in.hi
3733 __ Sub(HighRegisterFrom(out), HighRegisterFrom(out), HighRegisterFrom(in));
3734 break;
3735
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003736 case DataType::Type::kFloat32:
3737 case DataType::Type::kFloat64:
Anton Kirilov644032c2016-12-06 17:51:43 +00003738 __ Vneg(OutputVRegister(neg), InputVRegister(neg));
Artem Serov02109dd2016-09-23 17:17:54 +01003739 break;
3740
3741 default:
3742 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
3743 }
3744}
3745
Scott Wakelingfe885462016-09-22 10:24:38 +01003746void LocationsBuilderARMVIXL::VisitTypeConversion(HTypeConversion* conversion) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003747 DataType::Type result_type = conversion->GetResultType();
3748 DataType::Type input_type = conversion->GetInputType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003749 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
3750 << input_type << " -> " << result_type;
Scott Wakelingfe885462016-09-22 10:24:38 +01003751
3752 // The float-to-long, double-to-long and long-to-float type conversions
3753 // rely on a call to the runtime.
3754 LocationSummary::CallKind call_kind =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003755 (((input_type == DataType::Type::kFloat32 || input_type == DataType::Type::kFloat64)
3756 && result_type == DataType::Type::kInt64)
3757 || (input_type == DataType::Type::kInt64 && result_type == DataType::Type::kFloat32))
Scott Wakelingfe885462016-09-22 10:24:38 +01003758 ? LocationSummary::kCallOnMainOnly
3759 : LocationSummary::kNoCall;
3760 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003761 new (GetGraph()->GetAllocator()) LocationSummary(conversion, call_kind);
Scott Wakelingfe885462016-09-22 10:24:38 +01003762
Scott Wakelingfe885462016-09-22 10:24:38 +01003763 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003764 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003765 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003766 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003767 case DataType::Type::kInt16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003768 DCHECK(DataType::IsIntegralType(input_type)) << input_type;
3769 locations->SetInAt(0, Location::RequiresRegister());
3770 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Scott Wakelingfe885462016-09-22 10:24:38 +01003771 break;
3772
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003773 case DataType::Type::kInt32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003774 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003775 case DataType::Type::kInt64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003776 locations->SetInAt(0, Location::Any());
3777 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3778 break;
3779
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003780 case DataType::Type::kFloat32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003781 locations->SetInAt(0, Location::RequiresFpuRegister());
3782 locations->SetOut(Location::RequiresRegister());
3783 locations->AddTemp(Location::RequiresFpuRegister());
3784 break;
3785
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003786 case DataType::Type::kFloat64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003787 locations->SetInAt(0, Location::RequiresFpuRegister());
3788 locations->SetOut(Location::RequiresRegister());
3789 locations->AddTemp(Location::RequiresFpuRegister());
3790 break;
3791
3792 default:
3793 LOG(FATAL) << "Unexpected type conversion from " << input_type
3794 << " to " << result_type;
3795 }
3796 break;
3797
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003798 case DataType::Type::kInt64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003799 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003800 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003801 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003802 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003803 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003804 case DataType::Type::kInt16:
3805 case DataType::Type::kInt32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003806 locations->SetInAt(0, Location::RequiresRegister());
3807 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3808 break;
3809
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003810 case DataType::Type::kFloat32: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003811 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3812 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
3813 locations->SetOut(LocationFrom(r0, r1));
Scott Wakelingfe885462016-09-22 10:24:38 +01003814 break;
3815 }
3816
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003817 case DataType::Type::kFloat64: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003818 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3819 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0),
3820 calling_convention.GetFpuRegisterAt(1)));
3821 locations->SetOut(LocationFrom(r0, r1));
Scott Wakelingfe885462016-09-22 10:24:38 +01003822 break;
3823 }
3824
3825 default:
3826 LOG(FATAL) << "Unexpected type conversion from " << input_type
3827 << " to " << result_type;
3828 }
3829 break;
3830
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003831 case DataType::Type::kFloat32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003832 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003833 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003834 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003835 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003836 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003837 case DataType::Type::kInt16:
3838 case DataType::Type::kInt32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003839 locations->SetInAt(0, Location::RequiresRegister());
3840 locations->SetOut(Location::RequiresFpuRegister());
3841 break;
3842
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003843 case DataType::Type::kInt64: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003844 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3845 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0),
3846 calling_convention.GetRegisterAt(1)));
3847 locations->SetOut(LocationFrom(calling_convention.GetFpuRegisterAt(0)));
Scott Wakelingfe885462016-09-22 10:24:38 +01003848 break;
3849 }
3850
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003851 case DataType::Type::kFloat64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003852 locations->SetInAt(0, Location::RequiresFpuRegister());
3853 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3854 break;
3855
3856 default:
3857 LOG(FATAL) << "Unexpected type conversion from " << input_type
3858 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08003859 }
Scott Wakelingfe885462016-09-22 10:24:38 +01003860 break;
3861
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003862 case DataType::Type::kFloat64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003863 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003864 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003865 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003866 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003867 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003868 case DataType::Type::kInt16:
3869 case DataType::Type::kInt32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003870 locations->SetInAt(0, Location::RequiresRegister());
3871 locations->SetOut(Location::RequiresFpuRegister());
3872 break;
3873
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003874 case DataType::Type::kInt64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003875 locations->SetInAt(0, Location::RequiresRegister());
3876 locations->SetOut(Location::RequiresFpuRegister());
3877 locations->AddTemp(Location::RequiresFpuRegister());
3878 locations->AddTemp(Location::RequiresFpuRegister());
3879 break;
3880
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003881 case DataType::Type::kFloat32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003882 locations->SetInAt(0, Location::RequiresFpuRegister());
3883 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3884 break;
3885
3886 default:
3887 LOG(FATAL) << "Unexpected type conversion from " << input_type
3888 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08003889 }
Scott Wakelingfe885462016-09-22 10:24:38 +01003890 break;
3891
3892 default:
3893 LOG(FATAL) << "Unexpected type conversion from " << input_type
3894 << " to " << result_type;
3895 }
3896}
3897
3898void InstructionCodeGeneratorARMVIXL::VisitTypeConversion(HTypeConversion* conversion) {
3899 LocationSummary* locations = conversion->GetLocations();
3900 Location out = locations->Out();
3901 Location in = locations->InAt(0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003902 DataType::Type result_type = conversion->GetResultType();
3903 DataType::Type input_type = conversion->GetInputType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003904 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
3905 << input_type << " -> " << result_type;
Scott Wakelingfe885462016-09-22 10:24:38 +01003906 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003907 case DataType::Type::kUint8:
Scott Wakelingfe885462016-09-22 10:24:38 +01003908 switch (input_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003909 case DataType::Type::kInt8:
3910 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003911 case DataType::Type::kInt16:
3912 case DataType::Type::kInt32:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003913 __ Ubfx(OutputRegister(conversion), InputRegisterAt(conversion, 0), 0, 8);
3914 break;
3915 case DataType::Type::kInt64:
3916 __ Ubfx(OutputRegister(conversion), LowRegisterFrom(in), 0, 8);
3917 break;
3918
3919 default:
3920 LOG(FATAL) << "Unexpected type conversion from " << input_type
3921 << " to " << result_type;
3922 }
3923 break;
3924
3925 case DataType::Type::kInt8:
3926 switch (input_type) {
3927 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003928 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003929 case DataType::Type::kInt16:
3930 case DataType::Type::kInt32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003931 __ Sbfx(OutputRegister(conversion), InputRegisterAt(conversion, 0), 0, 8);
3932 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003933 case DataType::Type::kInt64:
3934 __ Sbfx(OutputRegister(conversion), LowRegisterFrom(in), 0, 8);
3935 break;
3936
3937 default:
3938 LOG(FATAL) << "Unexpected type conversion from " << input_type
3939 << " to " << result_type;
3940 }
3941 break;
3942
3943 case DataType::Type::kUint16:
3944 switch (input_type) {
3945 case DataType::Type::kInt8:
3946 case DataType::Type::kInt16:
3947 case DataType::Type::kInt32:
3948 __ Ubfx(OutputRegister(conversion), InputRegisterAt(conversion, 0), 0, 16);
3949 break;
3950 case DataType::Type::kInt64:
3951 __ Ubfx(OutputRegister(conversion), LowRegisterFrom(in), 0, 16);
3952 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01003953
3954 default:
3955 LOG(FATAL) << "Unexpected type conversion from " << input_type
3956 << " to " << result_type;
3957 }
3958 break;
3959
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003960 case DataType::Type::kInt16:
Scott Wakelingfe885462016-09-22 10:24:38 +01003961 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003962 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003963 case DataType::Type::kInt32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003964 __ Sbfx(OutputRegister(conversion), InputRegisterAt(conversion, 0), 0, 16);
3965 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003966 case DataType::Type::kInt64:
3967 __ Sbfx(OutputRegister(conversion), LowRegisterFrom(in), 0, 16);
3968 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01003969
3970 default:
3971 LOG(FATAL) << "Unexpected type conversion from " << input_type
3972 << " to " << result_type;
3973 }
3974 break;
3975
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003976 case DataType::Type::kInt32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003977 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003978 case DataType::Type::kInt64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003979 DCHECK(out.IsRegister());
3980 if (in.IsRegisterPair()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003981 __ Mov(OutputRegister(conversion), LowRegisterFrom(in));
Scott Wakelingfe885462016-09-22 10:24:38 +01003982 } else if (in.IsDoubleStackSlot()) {
3983 GetAssembler()->LoadFromOffset(kLoadWord,
3984 OutputRegister(conversion),
3985 sp,
3986 in.GetStackIndex());
3987 } else {
3988 DCHECK(in.IsConstant());
3989 DCHECK(in.GetConstant()->IsLongConstant());
Vladimir Markoba1a48e2017-04-13 11:50:14 +01003990 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
3991 __ Mov(OutputRegister(conversion), static_cast<int32_t>(value));
Scott Wakelingfe885462016-09-22 10:24:38 +01003992 }
3993 break;
3994
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003995 case DataType::Type::kFloat32: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003996 vixl32::SRegister temp = LowSRegisterFrom(locations->GetTemp(0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01003997 __ Vcvt(S32, F32, temp, InputSRegisterAt(conversion, 0));
Scott Wakelingfe885462016-09-22 10:24:38 +01003998 __ Vmov(OutputRegister(conversion), temp);
3999 break;
4000 }
4001
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004002 case DataType::Type::kFloat64: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004003 vixl32::SRegister temp_s = LowSRegisterFrom(locations->GetTemp(0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01004004 __ Vcvt(S32, F64, temp_s, DRegisterFrom(in));
Scott Wakelingfe885462016-09-22 10:24:38 +01004005 __ Vmov(OutputRegister(conversion), temp_s);
4006 break;
4007 }
4008
4009 default:
4010 LOG(FATAL) << "Unexpected type conversion from " << input_type
4011 << " to " << result_type;
4012 }
4013 break;
4014
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004015 case DataType::Type::kInt64:
Scott Wakelingfe885462016-09-22 10:24:38 +01004016 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004017 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004018 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004019 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004020 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004021 case DataType::Type::kInt16:
4022 case DataType::Type::kInt32:
Scott Wakelingfe885462016-09-22 10:24:38 +01004023 DCHECK(out.IsRegisterPair());
4024 DCHECK(in.IsRegister());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004025 __ Mov(LowRegisterFrom(out), InputRegisterAt(conversion, 0));
Scott Wakelingfe885462016-09-22 10:24:38 +01004026 // Sign extension.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004027 __ Asr(HighRegisterFrom(out), LowRegisterFrom(out), 31);
Scott Wakelingfe885462016-09-22 10:24:38 +01004028 break;
4029
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004030 case DataType::Type::kFloat32:
Scott Wakelingfe885462016-09-22 10:24:38 +01004031 codegen_->InvokeRuntime(kQuickF2l, conversion, conversion->GetDexPc());
4032 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
4033 break;
4034
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004035 case DataType::Type::kFloat64:
Scott Wakelingfe885462016-09-22 10:24:38 +01004036 codegen_->InvokeRuntime(kQuickD2l, conversion, conversion->GetDexPc());
4037 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
4038 break;
4039
4040 default:
4041 LOG(FATAL) << "Unexpected type conversion from " << input_type
4042 << " to " << result_type;
4043 }
4044 break;
4045
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004046 case DataType::Type::kFloat32:
Scott Wakelingfe885462016-09-22 10:24:38 +01004047 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004048 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004049 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004050 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004051 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004052 case DataType::Type::kInt16:
4053 case DataType::Type::kInt32:
Scott Wakelingfe885462016-09-22 10:24:38 +01004054 __ Vmov(OutputSRegister(conversion), InputRegisterAt(conversion, 0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01004055 __ Vcvt(F32, S32, OutputSRegister(conversion), OutputSRegister(conversion));
Scott Wakelingfe885462016-09-22 10:24:38 +01004056 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01004057
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004058 case DataType::Type::kInt64:
Scott Wakelingfe885462016-09-22 10:24:38 +01004059 codegen_->InvokeRuntime(kQuickL2f, conversion, conversion->GetDexPc());
4060 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
4061 break;
4062
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004063 case DataType::Type::kFloat64:
Scott Wakelingc34dba72016-10-03 10:14:44 +01004064 __ Vcvt(F32, F64, OutputSRegister(conversion), DRegisterFrom(in));
Scott Wakelingfe885462016-09-22 10:24:38 +01004065 break;
4066
4067 default:
4068 LOG(FATAL) << "Unexpected type conversion from " << input_type
4069 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08004070 }
Scott Wakelingfe885462016-09-22 10:24:38 +01004071 break;
4072
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004073 case DataType::Type::kFloat64:
Scott Wakelingfe885462016-09-22 10:24:38 +01004074 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004075 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004076 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004077 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004078 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004079 case DataType::Type::kInt16:
4080 case DataType::Type::kInt32:
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004081 __ Vmov(LowSRegisterFrom(out), InputRegisterAt(conversion, 0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01004082 __ Vcvt(F64, S32, DRegisterFrom(out), LowSRegisterFrom(out));
Scott Wakelingfe885462016-09-22 10:24:38 +01004083 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01004084
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004085 case DataType::Type::kInt64: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004086 vixl32::Register low = LowRegisterFrom(in);
4087 vixl32::Register high = HighRegisterFrom(in);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004088 vixl32::SRegister out_s = LowSRegisterFrom(out);
Scott Wakelingc34dba72016-10-03 10:14:44 +01004089 vixl32::DRegister out_d = DRegisterFrom(out);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004090 vixl32::SRegister temp_s = LowSRegisterFrom(locations->GetTemp(0));
Scott Wakelingc34dba72016-10-03 10:14:44 +01004091 vixl32::DRegister temp_d = DRegisterFrom(locations->GetTemp(0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01004092 vixl32::DRegister constant_d = DRegisterFrom(locations->GetTemp(1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004093
4094 // temp_d = int-to-double(high)
4095 __ Vmov(temp_s, high);
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01004096 __ Vcvt(F64, S32, temp_d, temp_s);
Scott Wakelingfe885462016-09-22 10:24:38 +01004097 // constant_d = k2Pow32EncodingForDouble
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004098 __ Vmov(constant_d, bit_cast<double, int64_t>(k2Pow32EncodingForDouble));
Scott Wakelingfe885462016-09-22 10:24:38 +01004099 // out_d = unsigned-to-double(low)
4100 __ Vmov(out_s, low);
4101 __ Vcvt(F64, U32, out_d, out_s);
4102 // out_d += temp_d * constant_d
4103 __ Vmla(F64, out_d, temp_d, constant_d);
4104 break;
4105 }
4106
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004107 case DataType::Type::kFloat32:
Scott Wakelingc34dba72016-10-03 10:14:44 +01004108 __ Vcvt(F64, F32, DRegisterFrom(out), InputSRegisterAt(conversion, 0));
Scott Wakelingfe885462016-09-22 10:24:38 +01004109 break;
4110
4111 default:
4112 LOG(FATAL) << "Unexpected type conversion from " << input_type
4113 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08004114 }
Scott Wakelingfe885462016-09-22 10:24:38 +01004115 break;
4116
4117 default:
4118 LOG(FATAL) << "Unexpected type conversion from " << input_type
4119 << " to " << result_type;
4120 }
4121}
4122
4123void LocationsBuilderARMVIXL::VisitAdd(HAdd* add) {
4124 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004125 new (GetGraph()->GetAllocator()) LocationSummary(add, LocationSummary::kNoCall);
Scott Wakelingfe885462016-09-22 10:24:38 +01004126 switch (add->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004127 case DataType::Type::kInt32: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004128 locations->SetInAt(0, Location::RequiresRegister());
4129 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
4130 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4131 break;
4132 }
4133
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004134 case DataType::Type::kInt64: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004135 locations->SetInAt(0, Location::RequiresRegister());
Anton Kirilovdda43962016-11-21 19:55:20 +00004136 locations->SetInAt(1, ArmEncodableConstantOrRegister(add->InputAt(1), ADD));
Scott Wakelingfe885462016-09-22 10:24:38 +01004137 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4138 break;
4139 }
4140
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004141 case DataType::Type::kFloat32:
4142 case DataType::Type::kFloat64: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004143 locations->SetInAt(0, Location::RequiresFpuRegister());
4144 locations->SetInAt(1, Location::RequiresFpuRegister());
4145 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4146 break;
4147 }
4148
4149 default:
4150 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
4151 }
4152}
4153
4154void InstructionCodeGeneratorARMVIXL::VisitAdd(HAdd* add) {
4155 LocationSummary* locations = add->GetLocations();
4156 Location out = locations->Out();
4157 Location first = locations->InAt(0);
4158 Location second = locations->InAt(1);
4159
4160 switch (add->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004161 case DataType::Type::kInt32: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004162 __ Add(OutputRegister(add), InputRegisterAt(add, 0), InputOperandAt(add, 1));
4163 }
4164 break;
4165
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004166 case DataType::Type::kInt64: {
Anton Kirilovdda43962016-11-21 19:55:20 +00004167 if (second.IsConstant()) {
4168 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
4169 GenerateAddLongConst(out, first, value);
4170 } else {
4171 DCHECK(second.IsRegisterPair());
4172 __ Adds(LowRegisterFrom(out), LowRegisterFrom(first), LowRegisterFrom(second));
4173 __ Adc(HighRegisterFrom(out), HighRegisterFrom(first), HighRegisterFrom(second));
4174 }
Scott Wakelingfe885462016-09-22 10:24:38 +01004175 break;
4176 }
4177
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004178 case DataType::Type::kFloat32:
4179 case DataType::Type::kFloat64:
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004180 __ Vadd(OutputVRegister(add), InputVRegisterAt(add, 0), InputVRegisterAt(add, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004181 break;
4182
4183 default:
4184 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
4185 }
4186}
4187
4188void LocationsBuilderARMVIXL::VisitSub(HSub* sub) {
4189 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004190 new (GetGraph()->GetAllocator()) LocationSummary(sub, LocationSummary::kNoCall);
Scott Wakelingfe885462016-09-22 10:24:38 +01004191 switch (sub->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004192 case DataType::Type::kInt32: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004193 locations->SetInAt(0, Location::RequiresRegister());
4194 locations->SetInAt(1, Location::RegisterOrConstant(sub->InputAt(1)));
4195 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4196 break;
4197 }
4198
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004199 case DataType::Type::kInt64: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004200 locations->SetInAt(0, Location::RequiresRegister());
Anton Kirilovdda43962016-11-21 19:55:20 +00004201 locations->SetInAt(1, ArmEncodableConstantOrRegister(sub->InputAt(1), SUB));
Scott Wakelingfe885462016-09-22 10:24:38 +01004202 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4203 break;
4204 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004205 case DataType::Type::kFloat32:
4206 case DataType::Type::kFloat64: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004207 locations->SetInAt(0, Location::RequiresFpuRegister());
4208 locations->SetInAt(1, Location::RequiresFpuRegister());
4209 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4210 break;
4211 }
4212 default:
4213 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
4214 }
4215}
4216
4217void InstructionCodeGeneratorARMVIXL::VisitSub(HSub* sub) {
4218 LocationSummary* locations = sub->GetLocations();
4219 Location out = locations->Out();
4220 Location first = locations->InAt(0);
4221 Location second = locations->InAt(1);
4222 switch (sub->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004223 case DataType::Type::kInt32: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004224 __ Sub(OutputRegister(sub), InputRegisterAt(sub, 0), InputOperandAt(sub, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004225 break;
4226 }
4227
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004228 case DataType::Type::kInt64: {
Anton Kirilovdda43962016-11-21 19:55:20 +00004229 if (second.IsConstant()) {
4230 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
4231 GenerateAddLongConst(out, first, -value);
4232 } else {
4233 DCHECK(second.IsRegisterPair());
4234 __ Subs(LowRegisterFrom(out), LowRegisterFrom(first), LowRegisterFrom(second));
4235 __ Sbc(HighRegisterFrom(out), HighRegisterFrom(first), HighRegisterFrom(second));
4236 }
Scott Wakelingfe885462016-09-22 10:24:38 +01004237 break;
4238 }
4239
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004240 case DataType::Type::kFloat32:
4241 case DataType::Type::kFloat64:
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004242 __ Vsub(OutputVRegister(sub), InputVRegisterAt(sub, 0), InputVRegisterAt(sub, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004243 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01004244
4245 default:
4246 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
4247 }
4248}
4249
4250void LocationsBuilderARMVIXL::VisitMul(HMul* mul) {
4251 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004252 new (GetGraph()->GetAllocator()) LocationSummary(mul, LocationSummary::kNoCall);
Scott Wakelingfe885462016-09-22 10:24:38 +01004253 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004254 case DataType::Type::kInt32:
4255 case DataType::Type::kInt64: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004256 locations->SetInAt(0, Location::RequiresRegister());
4257 locations->SetInAt(1, Location::RequiresRegister());
4258 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4259 break;
4260 }
4261
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004262 case DataType::Type::kFloat32:
4263 case DataType::Type::kFloat64: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004264 locations->SetInAt(0, Location::RequiresFpuRegister());
4265 locations->SetInAt(1, Location::RequiresFpuRegister());
4266 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4267 break;
4268 }
4269
4270 default:
4271 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4272 }
4273}
4274
4275void InstructionCodeGeneratorARMVIXL::VisitMul(HMul* mul) {
4276 LocationSummary* locations = mul->GetLocations();
4277 Location out = locations->Out();
4278 Location first = locations->InAt(0);
4279 Location second = locations->InAt(1);
4280 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004281 case DataType::Type::kInt32: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004282 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
4283 break;
4284 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004285 case DataType::Type::kInt64: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004286 vixl32::Register out_hi = HighRegisterFrom(out);
4287 vixl32::Register out_lo = LowRegisterFrom(out);
4288 vixl32::Register in1_hi = HighRegisterFrom(first);
4289 vixl32::Register in1_lo = LowRegisterFrom(first);
4290 vixl32::Register in2_hi = HighRegisterFrom(second);
4291 vixl32::Register in2_lo = LowRegisterFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01004292
4293 // Extra checks to protect caused by the existence of R1_R2.
4294 // The algorithm is wrong if out.hi is either in1.lo or in2.lo:
4295 // (e.g. in1=r0_r1, in2=r2_r3 and out=r1_r2);
Anton Kirilov644032c2016-12-06 17:51:43 +00004296 DCHECK(!out_hi.Is(in1_lo));
4297 DCHECK(!out_hi.Is(in2_lo));
Scott Wakelingfe885462016-09-22 10:24:38 +01004298
4299 // input: in1 - 64 bits, in2 - 64 bits
4300 // output: out
4301 // formula: out.hi : out.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
4302 // parts: out.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
4303 // parts: out.lo = (in1.lo * in2.lo)[31:0]
4304
4305 UseScratchRegisterScope temps(GetVIXLAssembler());
4306 vixl32::Register temp = temps.Acquire();
4307 // temp <- in1.lo * in2.hi
4308 __ Mul(temp, in1_lo, in2_hi);
4309 // out.hi <- in1.lo * in2.hi + in1.hi * in2.lo
4310 __ Mla(out_hi, in1_hi, in2_lo, temp);
4311 // out.lo <- (in1.lo * in2.lo)[31:0];
4312 __ Umull(out_lo, temp, in1_lo, in2_lo);
4313 // out.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004314 __ Add(out_hi, out_hi, temp);
Scott Wakelingfe885462016-09-22 10:24:38 +01004315 break;
4316 }
4317
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004318 case DataType::Type::kFloat32:
4319 case DataType::Type::kFloat64:
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004320 __ Vmul(OutputVRegister(mul), InputVRegisterAt(mul, 0), InputVRegisterAt(mul, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004321 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01004322
4323 default:
4324 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4325 }
4326}
4327
Scott Wakelingfe885462016-09-22 10:24:38 +01004328void InstructionCodeGeneratorARMVIXL::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
4329 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004330 DCHECK(instruction->GetResultType() == DataType::Type::kInt32);
Scott Wakelingfe885462016-09-22 10:24:38 +01004331
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004332 Location second = instruction->GetLocations()->InAt(1);
Scott Wakelingfe885462016-09-22 10:24:38 +01004333 DCHECK(second.IsConstant());
4334
4335 vixl32::Register out = OutputRegister(instruction);
4336 vixl32::Register dividend = InputRegisterAt(instruction, 0);
Anton Kirilov644032c2016-12-06 17:51:43 +00004337 int32_t imm = Int32ConstantFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01004338 DCHECK(imm == 1 || imm == -1);
4339
4340 if (instruction->IsRem()) {
4341 __ Mov(out, 0);
4342 } else {
4343 if (imm == 1) {
4344 __ Mov(out, dividend);
4345 } else {
4346 __ Rsb(out, dividend, 0);
4347 }
4348 }
4349}
4350
4351void InstructionCodeGeneratorARMVIXL::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
4352 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004353 DCHECK(instruction->GetResultType() == DataType::Type::kInt32);
Scott Wakelingfe885462016-09-22 10:24:38 +01004354
4355 LocationSummary* locations = instruction->GetLocations();
4356 Location second = locations->InAt(1);
4357 DCHECK(second.IsConstant());
4358
4359 vixl32::Register out = OutputRegister(instruction);
4360 vixl32::Register dividend = InputRegisterAt(instruction, 0);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004361 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
Anton Kirilov644032c2016-12-06 17:51:43 +00004362 int32_t imm = Int32ConstantFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01004363 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
4364 int ctz_imm = CTZ(abs_imm);
4365
4366 if (ctz_imm == 1) {
4367 __ Lsr(temp, dividend, 32 - ctz_imm);
4368 } else {
4369 __ Asr(temp, dividend, 31);
4370 __ Lsr(temp, temp, 32 - ctz_imm);
4371 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004372 __ Add(out, temp, dividend);
Scott Wakelingfe885462016-09-22 10:24:38 +01004373
4374 if (instruction->IsDiv()) {
4375 __ Asr(out, out, ctz_imm);
4376 if (imm < 0) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004377 __ Rsb(out, out, 0);
Scott Wakelingfe885462016-09-22 10:24:38 +01004378 }
4379 } else {
4380 __ Ubfx(out, out, 0, ctz_imm);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004381 __ Sub(out, out, temp);
Scott Wakelingfe885462016-09-22 10:24:38 +01004382 }
4383}
4384
4385void InstructionCodeGeneratorARMVIXL::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
4386 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004387 DCHECK(instruction->GetResultType() == DataType::Type::kInt32);
Scott Wakelingfe885462016-09-22 10:24:38 +01004388
4389 LocationSummary* locations = instruction->GetLocations();
4390 Location second = locations->InAt(1);
4391 DCHECK(second.IsConstant());
4392
4393 vixl32::Register out = OutputRegister(instruction);
4394 vixl32::Register dividend = InputRegisterAt(instruction, 0);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004395 vixl32::Register temp1 = RegisterFrom(locations->GetTemp(0));
4396 vixl32::Register temp2 = RegisterFrom(locations->GetTemp(1));
Scott Wakelingb77051e2016-11-21 19:46:00 +00004397 int32_t imm = Int32ConstantFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01004398
4399 int64_t magic;
4400 int shift;
4401 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
4402
Anton Kirilovdda43962016-11-21 19:55:20 +00004403 // TODO(VIXL): Change the static cast to Operand::From() after VIXL is fixed.
4404 __ Mov(temp1, static_cast<int32_t>(magic));
Scott Wakelingfe885462016-09-22 10:24:38 +01004405 __ Smull(temp2, temp1, dividend, temp1);
4406
4407 if (imm > 0 && magic < 0) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004408 __ Add(temp1, temp1, dividend);
Scott Wakelingfe885462016-09-22 10:24:38 +01004409 } else if (imm < 0 && magic > 0) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004410 __ Sub(temp1, temp1, dividend);
Scott Wakelingfe885462016-09-22 10:24:38 +01004411 }
4412
4413 if (shift != 0) {
4414 __ Asr(temp1, temp1, shift);
4415 }
4416
4417 if (instruction->IsDiv()) {
4418 __ Sub(out, temp1, Operand(temp1, vixl32::Shift(ASR), 31));
4419 } else {
4420 __ Sub(temp1, temp1, Operand(temp1, vixl32::Shift(ASR), 31));
4421 // TODO: Strength reduction for mls.
4422 __ Mov(temp2, imm);
4423 __ Mls(out, temp1, temp2, dividend);
4424 }
4425}
4426
4427void InstructionCodeGeneratorARMVIXL::GenerateDivRemConstantIntegral(
4428 HBinaryOperation* instruction) {
4429 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004430 DCHECK(instruction->GetResultType() == DataType::Type::kInt32);
Scott Wakelingfe885462016-09-22 10:24:38 +01004431
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004432 Location second = instruction->GetLocations()->InAt(1);
Scott Wakelingfe885462016-09-22 10:24:38 +01004433 DCHECK(second.IsConstant());
4434
Anton Kirilov644032c2016-12-06 17:51:43 +00004435 int32_t imm = Int32ConstantFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01004436 if (imm == 0) {
4437 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
4438 } else if (imm == 1 || imm == -1) {
4439 DivRemOneOrMinusOne(instruction);
4440 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
4441 DivRemByPowerOfTwo(instruction);
4442 } else {
4443 DCHECK(imm <= -2 || imm >= 2);
4444 GenerateDivRemWithAnyConstant(instruction);
4445 }
4446}
4447
4448void LocationsBuilderARMVIXL::VisitDiv(HDiv* div) {
4449 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004450 if (div->GetResultType() == DataType::Type::kInt64) {
Scott Wakelingfe885462016-09-22 10:24:38 +01004451 // pLdiv runtime call.
4452 call_kind = LocationSummary::kCallOnMainOnly;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004453 } else if (div->GetResultType() == DataType::Type::kInt32 && div->InputAt(1)->IsConstant()) {
Scott Wakelingfe885462016-09-22 10:24:38 +01004454 // sdiv will be replaced by other instruction sequence.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004455 } else if (div->GetResultType() == DataType::Type::kInt32 &&
Scott Wakelingfe885462016-09-22 10:24:38 +01004456 !codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4457 // pIdivmod runtime call.
4458 call_kind = LocationSummary::kCallOnMainOnly;
4459 }
4460
Vladimir Markoca6fff82017-10-03 14:49:14 +01004461 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(div, call_kind);
Scott Wakelingfe885462016-09-22 10:24:38 +01004462
4463 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004464 case DataType::Type::kInt32: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004465 if (div->InputAt(1)->IsConstant()) {
4466 locations->SetInAt(0, Location::RequiresRegister());
4467 locations->SetInAt(1, Location::ConstantLocation(div->InputAt(1)->AsConstant()));
4468 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Anton Kirilov644032c2016-12-06 17:51:43 +00004469 int32_t value = Int32ConstantFrom(div->InputAt(1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004470 if (value == 1 || value == 0 || value == -1) {
4471 // No temp register required.
4472 } else {
4473 locations->AddTemp(Location::RequiresRegister());
4474 if (!IsPowerOfTwo(AbsOrMin(value))) {
4475 locations->AddTemp(Location::RequiresRegister());
4476 }
4477 }
4478 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4479 locations->SetInAt(0, Location::RequiresRegister());
4480 locations->SetInAt(1, Location::RequiresRegister());
4481 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4482 } else {
Artem Serov551b28f2016-10-18 19:11:30 +01004483 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4484 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4485 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
Roland Levillain5e8d5f02016-10-18 18:03:43 +01004486 // Note: divmod will compute both the quotient and the remainder as the pair R0 and R1, but
Artem Serov551b28f2016-10-18 19:11:30 +01004487 // we only need the former.
4488 locations->SetOut(LocationFrom(r0));
Scott Wakelingfe885462016-09-22 10:24:38 +01004489 }
4490 break;
4491 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004492 case DataType::Type::kInt64: {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01004493 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4494 locations->SetInAt(0, LocationFrom(
4495 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
4496 locations->SetInAt(1, LocationFrom(
4497 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
4498 locations->SetOut(LocationFrom(r0, r1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004499 break;
4500 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004501 case DataType::Type::kFloat32:
4502 case DataType::Type::kFloat64: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004503 locations->SetInAt(0, Location::RequiresFpuRegister());
4504 locations->SetInAt(1, Location::RequiresFpuRegister());
4505 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4506 break;
4507 }
4508
4509 default:
4510 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
4511 }
4512}
4513
4514void InstructionCodeGeneratorARMVIXL::VisitDiv(HDiv* div) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01004515 Location lhs = div->GetLocations()->InAt(0);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004516 Location rhs = div->GetLocations()->InAt(1);
Scott Wakelingfe885462016-09-22 10:24:38 +01004517
4518 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004519 case DataType::Type::kInt32: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004520 if (rhs.IsConstant()) {
Scott Wakelingfe885462016-09-22 10:24:38 +01004521 GenerateDivRemConstantIntegral(div);
4522 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4523 __ Sdiv(OutputRegister(div), InputRegisterAt(div, 0), InputRegisterAt(div, 1));
4524 } else {
Artem Serov551b28f2016-10-18 19:11:30 +01004525 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4526 DCHECK(calling_convention.GetRegisterAt(0).Is(RegisterFrom(lhs)));
4527 DCHECK(calling_convention.GetRegisterAt(1).Is(RegisterFrom(rhs)));
4528 DCHECK(r0.Is(OutputRegister(div)));
4529
4530 codegen_->InvokeRuntime(kQuickIdivmod, div, div->GetDexPc());
4531 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
Scott Wakelingfe885462016-09-22 10:24:38 +01004532 }
4533 break;
4534 }
4535
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004536 case DataType::Type::kInt64: {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01004537 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4538 DCHECK(calling_convention.GetRegisterAt(0).Is(LowRegisterFrom(lhs)));
4539 DCHECK(calling_convention.GetRegisterAt(1).Is(HighRegisterFrom(lhs)));
4540 DCHECK(calling_convention.GetRegisterAt(2).Is(LowRegisterFrom(rhs)));
4541 DCHECK(calling_convention.GetRegisterAt(3).Is(HighRegisterFrom(rhs)));
4542 DCHECK(LowRegisterFrom(div->GetLocations()->Out()).Is(r0));
4543 DCHECK(HighRegisterFrom(div->GetLocations()->Out()).Is(r1));
4544
4545 codegen_->InvokeRuntime(kQuickLdiv, div, div->GetDexPc());
4546 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Scott Wakelingfe885462016-09-22 10:24:38 +01004547 break;
4548 }
4549
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004550 case DataType::Type::kFloat32:
4551 case DataType::Type::kFloat64:
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004552 __ Vdiv(OutputVRegister(div), InputVRegisterAt(div, 0), InputVRegisterAt(div, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004553 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01004554
4555 default:
4556 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
4557 }
4558}
4559
Artem Serov551b28f2016-10-18 19:11:30 +01004560void LocationsBuilderARMVIXL::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004561 DataType::Type type = rem->GetResultType();
Artem Serov551b28f2016-10-18 19:11:30 +01004562
4563 // Most remainders are implemented in the runtime.
4564 LocationSummary::CallKind call_kind = LocationSummary::kCallOnMainOnly;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004565 if (rem->GetResultType() == DataType::Type::kInt32 && rem->InputAt(1)->IsConstant()) {
Artem Serov551b28f2016-10-18 19:11:30 +01004566 // sdiv will be replaced by other instruction sequence.
4567 call_kind = LocationSummary::kNoCall;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004568 } else if ((rem->GetResultType() == DataType::Type::kInt32)
Artem Serov551b28f2016-10-18 19:11:30 +01004569 && codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4570 // Have hardware divide instruction for int, do it with three instructions.
4571 call_kind = LocationSummary::kNoCall;
4572 }
4573
Vladimir Markoca6fff82017-10-03 14:49:14 +01004574 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(rem, call_kind);
Artem Serov551b28f2016-10-18 19:11:30 +01004575
4576 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004577 case DataType::Type::kInt32: {
Artem Serov551b28f2016-10-18 19:11:30 +01004578 if (rem->InputAt(1)->IsConstant()) {
4579 locations->SetInAt(0, Location::RequiresRegister());
4580 locations->SetInAt(1, Location::ConstantLocation(rem->InputAt(1)->AsConstant()));
4581 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Anton Kirilov644032c2016-12-06 17:51:43 +00004582 int32_t value = Int32ConstantFrom(rem->InputAt(1));
Artem Serov551b28f2016-10-18 19:11:30 +01004583 if (value == 1 || value == 0 || value == -1) {
4584 // No temp register required.
4585 } else {
4586 locations->AddTemp(Location::RequiresRegister());
4587 if (!IsPowerOfTwo(AbsOrMin(value))) {
4588 locations->AddTemp(Location::RequiresRegister());
4589 }
4590 }
4591 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4592 locations->SetInAt(0, Location::RequiresRegister());
4593 locations->SetInAt(1, Location::RequiresRegister());
4594 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4595 locations->AddTemp(Location::RequiresRegister());
4596 } else {
4597 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4598 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4599 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
Roland Levillain5e8d5f02016-10-18 18:03:43 +01004600 // Note: divmod will compute both the quotient and the remainder as the pair R0 and R1, but
Artem Serov551b28f2016-10-18 19:11:30 +01004601 // we only need the latter.
4602 locations->SetOut(LocationFrom(r1));
4603 }
4604 break;
4605 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004606 case DataType::Type::kInt64: {
Artem Serov551b28f2016-10-18 19:11:30 +01004607 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4608 locations->SetInAt(0, LocationFrom(
4609 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
4610 locations->SetInAt(1, LocationFrom(
4611 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
4612 // The runtime helper puts the output in R2,R3.
4613 locations->SetOut(LocationFrom(r2, r3));
4614 break;
4615 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004616 case DataType::Type::kFloat32: {
Artem Serov551b28f2016-10-18 19:11:30 +01004617 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4618 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
4619 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
4620 locations->SetOut(LocationFrom(s0));
4621 break;
4622 }
4623
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004624 case DataType::Type::kFloat64: {
Artem Serov551b28f2016-10-18 19:11:30 +01004625 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4626 locations->SetInAt(0, LocationFrom(
4627 calling_convention.GetFpuRegisterAt(0), calling_convention.GetFpuRegisterAt(1)));
4628 locations->SetInAt(1, LocationFrom(
4629 calling_convention.GetFpuRegisterAt(2), calling_convention.GetFpuRegisterAt(3)));
4630 locations->SetOut(LocationFrom(s0, s1));
4631 break;
4632 }
4633
4634 default:
4635 LOG(FATAL) << "Unexpected rem type " << type;
4636 }
4637}
4638
4639void InstructionCodeGeneratorARMVIXL::VisitRem(HRem* rem) {
4640 LocationSummary* locations = rem->GetLocations();
4641 Location second = locations->InAt(1);
4642
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004643 DataType::Type type = rem->GetResultType();
Artem Serov551b28f2016-10-18 19:11:30 +01004644 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004645 case DataType::Type::kInt32: {
Artem Serov551b28f2016-10-18 19:11:30 +01004646 vixl32::Register reg1 = InputRegisterAt(rem, 0);
4647 vixl32::Register out_reg = OutputRegister(rem);
4648 if (second.IsConstant()) {
4649 GenerateDivRemConstantIntegral(rem);
4650 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4651 vixl32::Register reg2 = RegisterFrom(second);
4652 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
4653
4654 // temp = reg1 / reg2 (integer division)
4655 // dest = reg1 - temp * reg2
4656 __ Sdiv(temp, reg1, reg2);
4657 __ Mls(out_reg, temp, reg2, reg1);
4658 } else {
4659 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4660 DCHECK(reg1.Is(calling_convention.GetRegisterAt(0)));
4661 DCHECK(RegisterFrom(second).Is(calling_convention.GetRegisterAt(1)));
4662 DCHECK(out_reg.Is(r1));
4663
4664 codegen_->InvokeRuntime(kQuickIdivmod, rem, rem->GetDexPc());
4665 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
4666 }
4667 break;
4668 }
4669
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004670 case DataType::Type::kInt64: {
Artem Serov551b28f2016-10-18 19:11:30 +01004671 codegen_->InvokeRuntime(kQuickLmod, rem, rem->GetDexPc());
4672 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
4673 break;
4674 }
4675
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004676 case DataType::Type::kFloat32: {
Artem Serov551b28f2016-10-18 19:11:30 +01004677 codegen_->InvokeRuntime(kQuickFmodf, rem, rem->GetDexPc());
4678 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
4679 break;
4680 }
4681
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004682 case DataType::Type::kFloat64: {
Artem Serov551b28f2016-10-18 19:11:30 +01004683 codegen_->InvokeRuntime(kQuickFmod, rem, rem->GetDexPc());
4684 CheckEntrypointTypes<kQuickFmod, double, double, double>();
4685 break;
4686 }
4687
4688 default:
4689 LOG(FATAL) << "Unexpected rem type " << type;
4690 }
4691}
4692
Aart Bik1f8d51b2018-02-15 10:42:37 -08004693static void CreateMinMaxLocations(ArenaAllocator* allocator, HBinaryOperation* minmax) {
4694 LocationSummary* locations = new (allocator) LocationSummary(minmax);
4695 switch (minmax->GetResultType()) {
4696 case DataType::Type::kInt32:
4697 locations->SetInAt(0, Location::RequiresRegister());
4698 locations->SetInAt(1, Location::RequiresRegister());
4699 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4700 break;
4701 case DataType::Type::kInt64:
4702 locations->SetInAt(0, Location::RequiresRegister());
4703 locations->SetInAt(1, Location::RequiresRegister());
4704 locations->SetOut(Location::SameAsFirstInput());
4705 break;
4706 case DataType::Type::kFloat32:
4707 locations->SetInAt(0, Location::RequiresFpuRegister());
4708 locations->SetInAt(1, Location::RequiresFpuRegister());
4709 locations->SetOut(Location::SameAsFirstInput());
4710 locations->AddTemp(Location::RequiresRegister());
4711 break;
4712 case DataType::Type::kFloat64:
4713 locations->SetInAt(0, Location::RequiresFpuRegister());
4714 locations->SetInAt(1, Location::RequiresFpuRegister());
4715 locations->SetOut(Location::SameAsFirstInput());
4716 break;
4717 default:
4718 LOG(FATAL) << "Unexpected type for HMinMax " << minmax->GetResultType();
4719 }
4720}
4721
Aart Bik351df3e2018-03-07 11:54:57 -08004722void InstructionCodeGeneratorARMVIXL::GenerateMinMaxInt(LocationSummary* locations, bool is_min) {
Aart Bik1f8d51b2018-02-15 10:42:37 -08004723 Location op1_loc = locations->InAt(0);
4724 Location op2_loc = locations->InAt(1);
4725 Location out_loc = locations->Out();
4726
4727 vixl32::Register op1 = RegisterFrom(op1_loc);
4728 vixl32::Register op2 = RegisterFrom(op2_loc);
4729 vixl32::Register out = RegisterFrom(out_loc);
4730
4731 __ Cmp(op1, op2);
4732
4733 {
4734 ExactAssemblyScope aas(GetVIXLAssembler(),
4735 3 * kMaxInstructionSizeInBytes,
4736 CodeBufferCheckScope::kMaximumSize);
4737
4738 __ ite(is_min ? lt : gt);
4739 __ mov(is_min ? lt : gt, out, op1);
4740 __ mov(is_min ? ge : le, out, op2);
4741 }
4742}
4743
4744void InstructionCodeGeneratorARMVIXL::GenerateMinMaxLong(LocationSummary* locations, bool is_min) {
4745 Location op1_loc = locations->InAt(0);
4746 Location op2_loc = locations->InAt(1);
4747 Location out_loc = locations->Out();
4748
4749 // Optimization: don't generate any code if inputs are the same.
4750 if (op1_loc.Equals(op2_loc)) {
4751 DCHECK(out_loc.Equals(op1_loc)); // out_loc is set as SameAsFirstInput() in location builder.
4752 return;
4753 }
4754
4755 vixl32::Register op1_lo = LowRegisterFrom(op1_loc);
4756 vixl32::Register op1_hi = HighRegisterFrom(op1_loc);
4757 vixl32::Register op2_lo = LowRegisterFrom(op2_loc);
4758 vixl32::Register op2_hi = HighRegisterFrom(op2_loc);
4759 vixl32::Register out_lo = LowRegisterFrom(out_loc);
4760 vixl32::Register out_hi = HighRegisterFrom(out_loc);
4761 UseScratchRegisterScope temps(GetVIXLAssembler());
4762 const vixl32::Register temp = temps.Acquire();
4763
4764 DCHECK(op1_lo.Is(out_lo));
4765 DCHECK(op1_hi.Is(out_hi));
4766
4767 // Compare op1 >= op2, or op1 < op2.
4768 __ Cmp(out_lo, op2_lo);
4769 __ Sbcs(temp, out_hi, op2_hi);
4770
4771 // Now GE/LT condition code is correct for the long comparison.
4772 {
4773 vixl32::ConditionType cond = is_min ? ge : lt;
4774 ExactAssemblyScope it_scope(GetVIXLAssembler(),
4775 3 * kMaxInstructionSizeInBytes,
4776 CodeBufferCheckScope::kMaximumSize);
4777 __ itt(cond);
4778 __ mov(cond, out_lo, op2_lo);
4779 __ mov(cond, out_hi, op2_hi);
4780 }
4781}
4782
Aart Bik351df3e2018-03-07 11:54:57 -08004783void InstructionCodeGeneratorARMVIXL::GenerateMinMaxFloat(HInstruction* minmax, bool is_min) {
4784 LocationSummary* locations = minmax->GetLocations();
Aart Bik1f8d51b2018-02-15 10:42:37 -08004785 Location op1_loc = locations->InAt(0);
4786 Location op2_loc = locations->InAt(1);
4787 Location out_loc = locations->Out();
4788
4789 // Optimization: don't generate any code if inputs are the same.
4790 if (op1_loc.Equals(op2_loc)) {
4791 DCHECK(out_loc.Equals(op1_loc)); // out_loc is set as SameAsFirstInput() in location builder.
4792 return;
4793 }
4794
4795 vixl32::SRegister op1 = SRegisterFrom(op1_loc);
4796 vixl32::SRegister op2 = SRegisterFrom(op2_loc);
4797 vixl32::SRegister out = SRegisterFrom(out_loc);
4798
4799 UseScratchRegisterScope temps(GetVIXLAssembler());
4800 const vixl32::Register temp1 = temps.Acquire();
4801 vixl32::Register temp2 = RegisterFrom(locations->GetTemp(0));
4802 vixl32::Label nan, done;
Aart Bik351df3e2018-03-07 11:54:57 -08004803 vixl32::Label* final_label = codegen_->GetFinalLabel(minmax, &done);
Aart Bik1f8d51b2018-02-15 10:42:37 -08004804
4805 DCHECK(op1.Is(out));
4806
4807 __ Vcmp(op1, op2);
4808 __ Vmrs(RegisterOrAPSR_nzcv(kPcCode), FPSCR);
4809 __ B(vs, &nan, /* far_target */ false); // if un-ordered, go to NaN handling.
4810
4811 // op1 <> op2
4812 vixl32::ConditionType cond = is_min ? gt : lt;
4813 {
4814 ExactAssemblyScope it_scope(GetVIXLAssembler(),
4815 2 * kMaxInstructionSizeInBytes,
4816 CodeBufferCheckScope::kMaximumSize);
4817 __ it(cond);
4818 __ vmov(cond, F32, out, op2);
4819 }
4820 // for <>(not equal), we've done min/max calculation.
4821 __ B(ne, final_label, /* far_target */ false);
4822
4823 // handle op1 == op2, max(+0.0,-0.0), min(+0.0,-0.0).
4824 __ Vmov(temp1, op1);
4825 __ Vmov(temp2, op2);
4826 if (is_min) {
4827 __ Orr(temp1, temp1, temp2);
4828 } else {
4829 __ And(temp1, temp1, temp2);
4830 }
4831 __ Vmov(out, temp1);
4832 __ B(final_label);
4833
4834 // handle NaN input.
4835 __ Bind(&nan);
4836 __ Movt(temp1, High16Bits(kNanFloat)); // 0x7FC0xxxx is a NaN.
4837 __ Vmov(out, temp1);
4838
4839 if (done.IsReferenced()) {
4840 __ Bind(&done);
4841 }
4842}
4843
Aart Bik351df3e2018-03-07 11:54:57 -08004844void InstructionCodeGeneratorARMVIXL::GenerateMinMaxDouble(HInstruction* minmax, bool is_min) {
4845 LocationSummary* locations = minmax->GetLocations();
Aart Bik1f8d51b2018-02-15 10:42:37 -08004846 Location op1_loc = locations->InAt(0);
4847 Location op2_loc = locations->InAt(1);
4848 Location out_loc = locations->Out();
4849
4850 // Optimization: don't generate any code if inputs are the same.
4851 if (op1_loc.Equals(op2_loc)) {
4852 DCHECK(out_loc.Equals(op1_loc)); // out_loc is set as SameAsFirstInput() in.
4853 return;
4854 }
4855
4856 vixl32::DRegister op1 = DRegisterFrom(op1_loc);
4857 vixl32::DRegister op2 = DRegisterFrom(op2_loc);
4858 vixl32::DRegister out = DRegisterFrom(out_loc);
4859 vixl32::Label handle_nan_eq, done;
Aart Bik351df3e2018-03-07 11:54:57 -08004860 vixl32::Label* final_label = codegen_->GetFinalLabel(minmax, &done);
Aart Bik1f8d51b2018-02-15 10:42:37 -08004861
4862 DCHECK(op1.Is(out));
4863
4864 __ Vcmp(op1, op2);
4865 __ Vmrs(RegisterOrAPSR_nzcv(kPcCode), FPSCR);
4866 __ B(vs, &handle_nan_eq, /* far_target */ false); // if un-ordered, go to NaN handling.
4867
4868 // op1 <> op2
4869 vixl32::ConditionType cond = is_min ? gt : lt;
4870 {
4871 ExactAssemblyScope it_scope(GetVIXLAssembler(),
4872 2 * kMaxInstructionSizeInBytes,
4873 CodeBufferCheckScope::kMaximumSize);
4874 __ it(cond);
4875 __ vmov(cond, F64, out, op2);
4876 }
4877 // for <>(not equal), we've done min/max calculation.
4878 __ B(ne, final_label, /* far_target */ false);
4879
4880 // handle op1 == op2, max(+0.0,-0.0).
4881 if (!is_min) {
4882 __ Vand(F64, out, op1, op2);
4883 __ B(final_label);
4884 }
4885
4886 // handle op1 == op2, min(+0.0,-0.0), NaN input.
4887 __ Bind(&handle_nan_eq);
4888 __ Vorr(F64, out, op1, op2); // assemble op1/-0.0/NaN.
4889
4890 if (done.IsReferenced()) {
4891 __ Bind(&done);
4892 }
4893}
4894
Aart Bik351df3e2018-03-07 11:54:57 -08004895void InstructionCodeGeneratorARMVIXL::GenerateMinMax(HBinaryOperation* minmax, bool is_min) {
4896 DataType::Type type = minmax->GetResultType();
4897 switch (type) {
4898 case DataType::Type::kInt32:
4899 GenerateMinMaxInt(minmax->GetLocations(), is_min);
4900 break;
4901 case DataType::Type::kInt64:
4902 GenerateMinMaxLong(minmax->GetLocations(), is_min);
4903 break;
4904 case DataType::Type::kFloat32:
4905 GenerateMinMaxFloat(minmax, is_min);
4906 break;
4907 case DataType::Type::kFloat64:
4908 GenerateMinMaxDouble(minmax, is_min);
4909 break;
4910 default:
4911 LOG(FATAL) << "Unexpected type for HMinMax " << type;
4912 }
4913}
4914
Aart Bik1f8d51b2018-02-15 10:42:37 -08004915void LocationsBuilderARMVIXL::VisitMin(HMin* min) {
4916 CreateMinMaxLocations(GetGraph()->GetAllocator(), min);
4917}
4918
4919void InstructionCodeGeneratorARMVIXL::VisitMin(HMin* min) {
Aart Bik351df3e2018-03-07 11:54:57 -08004920 GenerateMinMax(min, /*is_min*/ true);
Aart Bik1f8d51b2018-02-15 10:42:37 -08004921}
4922
4923void LocationsBuilderARMVIXL::VisitMax(HMax* max) {
4924 CreateMinMaxLocations(GetGraph()->GetAllocator(), max);
4925}
4926
4927void InstructionCodeGeneratorARMVIXL::VisitMax(HMax* max) {
Aart Bik351df3e2018-03-07 11:54:57 -08004928 GenerateMinMax(max, /*is_min*/ false);
Aart Bik1f8d51b2018-02-15 10:42:37 -08004929}
4930
Aart Bik3dad3412018-02-28 12:01:46 -08004931void LocationsBuilderARMVIXL::VisitAbs(HAbs* abs) {
4932 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(abs);
4933 switch (abs->GetResultType()) {
4934 case DataType::Type::kInt32:
4935 case DataType::Type::kInt64:
4936 locations->SetInAt(0, Location::RequiresRegister());
4937 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4938 locations->AddTemp(Location::RequiresRegister());
4939 break;
4940 case DataType::Type::kFloat32:
4941 case DataType::Type::kFloat64:
4942 locations->SetInAt(0, Location::RequiresFpuRegister());
4943 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4944 break;
4945 default:
4946 LOG(FATAL) << "Unexpected type for abs operation " << abs->GetResultType();
4947 }
4948}
4949
4950void InstructionCodeGeneratorARMVIXL::VisitAbs(HAbs* abs) {
4951 LocationSummary* locations = abs->GetLocations();
4952 switch (abs->GetResultType()) {
4953 case DataType::Type::kInt32: {
4954 vixl32::Register in_reg = RegisterFrom(locations->InAt(0));
4955 vixl32::Register out_reg = RegisterFrom(locations->Out());
4956 vixl32::Register mask = RegisterFrom(locations->GetTemp(0));
4957 __ Asr(mask, in_reg, 31);
4958 __ Add(out_reg, in_reg, mask);
4959 __ Eor(out_reg, out_reg, mask);
4960 break;
4961 }
4962 case DataType::Type::kInt64: {
4963 Location in = locations->InAt(0);
4964 vixl32::Register in_reg_lo = LowRegisterFrom(in);
4965 vixl32::Register in_reg_hi = HighRegisterFrom(in);
4966 Location output = locations->Out();
4967 vixl32::Register out_reg_lo = LowRegisterFrom(output);
4968 vixl32::Register out_reg_hi = HighRegisterFrom(output);
4969 DCHECK(!out_reg_lo.Is(in_reg_hi)) << "Diagonal overlap unexpected.";
4970 vixl32::Register mask = RegisterFrom(locations->GetTemp(0));
4971 __ Asr(mask, in_reg_hi, 31);
4972 __ Adds(out_reg_lo, in_reg_lo, mask);
4973 __ Adc(out_reg_hi, in_reg_hi, mask);
4974 __ Eor(out_reg_lo, out_reg_lo, mask);
4975 __ Eor(out_reg_hi, out_reg_hi, mask);
4976 break;
4977 }
4978 case DataType::Type::kFloat32:
4979 case DataType::Type::kFloat64:
4980 __ Vabs(OutputVRegister(abs), InputVRegisterAt(abs, 0));
4981 break;
4982 default:
4983 LOG(FATAL) << "Unexpected type for abs operation " << abs->GetResultType();
4984 }
4985}
Artem Serov551b28f2016-10-18 19:11:30 +01004986
Scott Wakelingfe885462016-09-22 10:24:38 +01004987void LocationsBuilderARMVIXL::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Artem Serov657022c2016-11-23 14:19:38 +00004988 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Scott Wakelingfe885462016-09-22 10:24:38 +01004989 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Scott Wakelingfe885462016-09-22 10:24:38 +01004990}
4991
4992void InstructionCodeGeneratorARMVIXL::VisitDivZeroCheck(HDivZeroCheck* instruction) {
4993 DivZeroCheckSlowPathARMVIXL* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01004994 new (codegen_->GetScopedAllocator()) DivZeroCheckSlowPathARMVIXL(instruction);
Scott Wakelingfe885462016-09-22 10:24:38 +01004995 codegen_->AddSlowPath(slow_path);
4996
4997 LocationSummary* locations = instruction->GetLocations();
4998 Location value = locations->InAt(0);
4999
5000 switch (instruction->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005001 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005002 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005003 case DataType::Type::kInt8:
5004 case DataType::Type::kUint16:
5005 case DataType::Type::kInt16:
5006 case DataType::Type::kInt32: {
Scott Wakelingfe885462016-09-22 10:24:38 +01005007 if (value.IsRegister()) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00005008 __ CompareAndBranchIfZero(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
Scott Wakelingfe885462016-09-22 10:24:38 +01005009 } else {
5010 DCHECK(value.IsConstant()) << value;
Anton Kirilov644032c2016-12-06 17:51:43 +00005011 if (Int32ConstantFrom(value) == 0) {
Scott Wakelingfe885462016-09-22 10:24:38 +01005012 __ B(slow_path->GetEntryLabel());
5013 }
5014 }
5015 break;
5016 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005017 case DataType::Type::kInt64: {
Scott Wakelingfe885462016-09-22 10:24:38 +01005018 if (value.IsRegisterPair()) {
5019 UseScratchRegisterScope temps(GetVIXLAssembler());
5020 vixl32::Register temp = temps.Acquire();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005021 __ Orrs(temp, LowRegisterFrom(value), HighRegisterFrom(value));
Scott Wakelingfe885462016-09-22 10:24:38 +01005022 __ B(eq, slow_path->GetEntryLabel());
5023 } else {
5024 DCHECK(value.IsConstant()) << value;
Anton Kirilov644032c2016-12-06 17:51:43 +00005025 if (Int64ConstantFrom(value) == 0) {
Scott Wakelingfe885462016-09-22 10:24:38 +01005026 __ B(slow_path->GetEntryLabel());
5027 }
5028 }
5029 break;
5030 }
5031 default:
5032 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
5033 }
5034}
5035
Artem Serov02109dd2016-09-23 17:17:54 +01005036void InstructionCodeGeneratorARMVIXL::HandleIntegerRotate(HRor* ror) {
5037 LocationSummary* locations = ror->GetLocations();
5038 vixl32::Register in = InputRegisterAt(ror, 0);
5039 Location rhs = locations->InAt(1);
5040 vixl32::Register out = OutputRegister(ror);
5041
5042 if (rhs.IsConstant()) {
5043 // Arm32 and Thumb2 assemblers require a rotation on the interval [1,31],
5044 // so map all rotations to a +ve. equivalent in that range.
5045 // (e.g. left *or* right by -2 bits == 30 bits in the same direction.)
5046 uint32_t rot = CodeGenerator::GetInt32ValueOf(rhs.GetConstant()) & 0x1F;
5047 if (rot) {
5048 // Rotate, mapping left rotations to right equivalents if necessary.
5049 // (e.g. left by 2 bits == right by 30.)
5050 __ Ror(out, in, rot);
5051 } else if (!out.Is(in)) {
5052 __ Mov(out, in);
5053 }
5054 } else {
5055 __ Ror(out, in, RegisterFrom(rhs));
5056 }
5057}
5058
5059// Gain some speed by mapping all Long rotates onto equivalent pairs of Integer
5060// rotates by swapping input regs (effectively rotating by the first 32-bits of
5061// a larger rotation) or flipping direction (thus treating larger right/left
5062// rotations as sub-word sized rotations in the other direction) as appropriate.
5063void InstructionCodeGeneratorARMVIXL::HandleLongRotate(HRor* ror) {
5064 LocationSummary* locations = ror->GetLocations();
5065 vixl32::Register in_reg_lo = LowRegisterFrom(locations->InAt(0));
5066 vixl32::Register in_reg_hi = HighRegisterFrom(locations->InAt(0));
5067 Location rhs = locations->InAt(1);
5068 vixl32::Register out_reg_lo = LowRegisterFrom(locations->Out());
5069 vixl32::Register out_reg_hi = HighRegisterFrom(locations->Out());
5070
5071 if (rhs.IsConstant()) {
5072 uint64_t rot = CodeGenerator::GetInt64ValueOf(rhs.GetConstant());
5073 // Map all rotations to +ve. equivalents on the interval [0,63].
5074 rot &= kMaxLongShiftDistance;
5075 // For rotates over a word in size, 'pre-rotate' by 32-bits to keep rotate
5076 // logic below to a simple pair of binary orr.
5077 // (e.g. 34 bits == in_reg swap + 2 bits right.)
5078 if (rot >= kArmBitsPerWord) {
5079 rot -= kArmBitsPerWord;
5080 std::swap(in_reg_hi, in_reg_lo);
5081 }
5082 // Rotate, or mov to out for zero or word size rotations.
5083 if (rot != 0u) {
Scott Wakelingb77051e2016-11-21 19:46:00 +00005084 __ Lsr(out_reg_hi, in_reg_hi, Operand::From(rot));
Artem Serov02109dd2016-09-23 17:17:54 +01005085 __ Orr(out_reg_hi, out_reg_hi, Operand(in_reg_lo, ShiftType::LSL, kArmBitsPerWord - rot));
Scott Wakelingb77051e2016-11-21 19:46:00 +00005086 __ Lsr(out_reg_lo, in_reg_lo, Operand::From(rot));
Artem Serov02109dd2016-09-23 17:17:54 +01005087 __ Orr(out_reg_lo, out_reg_lo, Operand(in_reg_hi, ShiftType::LSL, kArmBitsPerWord - rot));
5088 } else {
5089 __ Mov(out_reg_lo, in_reg_lo);
5090 __ Mov(out_reg_hi, in_reg_hi);
5091 }
5092 } else {
5093 vixl32::Register shift_right = RegisterFrom(locations->GetTemp(0));
5094 vixl32::Register shift_left = RegisterFrom(locations->GetTemp(1));
5095 vixl32::Label end;
5096 vixl32::Label shift_by_32_plus_shift_right;
Anton Kirilov6f644202017-02-27 18:29:45 +00005097 vixl32::Label* final_label = codegen_->GetFinalLabel(ror, &end);
Artem Serov02109dd2016-09-23 17:17:54 +01005098
5099 __ And(shift_right, RegisterFrom(rhs), 0x1F);
5100 __ Lsrs(shift_left, RegisterFrom(rhs), 6);
Scott Wakelingbffdc702016-12-07 17:46:03 +00005101 __ Rsb(LeaveFlags, shift_left, shift_right, Operand::From(kArmBitsPerWord));
Artem Serov517d9f62016-12-12 15:51:15 +00005102 __ B(cc, &shift_by_32_plus_shift_right, /* far_target */ false);
Artem Serov02109dd2016-09-23 17:17:54 +01005103
5104 // out_reg_hi = (reg_hi << shift_left) | (reg_lo >> shift_right).
5105 // out_reg_lo = (reg_lo << shift_left) | (reg_hi >> shift_right).
5106 __ Lsl(out_reg_hi, in_reg_hi, shift_left);
5107 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
5108 __ Add(out_reg_hi, out_reg_hi, out_reg_lo);
5109 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
5110 __ Lsr(shift_left, in_reg_hi, shift_right);
5111 __ Add(out_reg_lo, out_reg_lo, shift_left);
Anton Kirilov6f644202017-02-27 18:29:45 +00005112 __ B(final_label);
Artem Serov02109dd2016-09-23 17:17:54 +01005113
5114 __ Bind(&shift_by_32_plus_shift_right); // Shift by 32+shift_right.
5115 // out_reg_hi = (reg_hi >> shift_right) | (reg_lo << shift_left).
5116 // out_reg_lo = (reg_lo >> shift_right) | (reg_hi << shift_left).
5117 __ Lsr(out_reg_hi, in_reg_hi, shift_right);
5118 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
5119 __ Add(out_reg_hi, out_reg_hi, out_reg_lo);
5120 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
5121 __ Lsl(shift_right, in_reg_hi, shift_left);
5122 __ Add(out_reg_lo, out_reg_lo, shift_right);
5123
Anton Kirilov6f644202017-02-27 18:29:45 +00005124 if (end.IsReferenced()) {
5125 __ Bind(&end);
5126 }
Artem Serov02109dd2016-09-23 17:17:54 +01005127 }
5128}
5129
5130void LocationsBuilderARMVIXL::VisitRor(HRor* ror) {
5131 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005132 new (GetGraph()->GetAllocator()) LocationSummary(ror, LocationSummary::kNoCall);
Artem Serov02109dd2016-09-23 17:17:54 +01005133 switch (ror->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005134 case DataType::Type::kInt32: {
Artem Serov02109dd2016-09-23 17:17:54 +01005135 locations->SetInAt(0, Location::RequiresRegister());
5136 locations->SetInAt(1, Location::RegisterOrConstant(ror->InputAt(1)));
5137 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5138 break;
5139 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005140 case DataType::Type::kInt64: {
Artem Serov02109dd2016-09-23 17:17:54 +01005141 locations->SetInAt(0, Location::RequiresRegister());
5142 if (ror->InputAt(1)->IsConstant()) {
5143 locations->SetInAt(1, Location::ConstantLocation(ror->InputAt(1)->AsConstant()));
5144 } else {
5145 locations->SetInAt(1, Location::RequiresRegister());
5146 locations->AddTemp(Location::RequiresRegister());
5147 locations->AddTemp(Location::RequiresRegister());
5148 }
5149 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
5150 break;
5151 }
5152 default:
5153 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
5154 }
5155}
5156
5157void InstructionCodeGeneratorARMVIXL::VisitRor(HRor* ror) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005158 DataType::Type type = ror->GetResultType();
Artem Serov02109dd2016-09-23 17:17:54 +01005159 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005160 case DataType::Type::kInt32: {
Artem Serov02109dd2016-09-23 17:17:54 +01005161 HandleIntegerRotate(ror);
5162 break;
5163 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005164 case DataType::Type::kInt64: {
Artem Serov02109dd2016-09-23 17:17:54 +01005165 HandleLongRotate(ror);
5166 break;
5167 }
5168 default:
5169 LOG(FATAL) << "Unexpected operation type " << type;
5170 UNREACHABLE();
5171 }
5172}
5173
Artem Serov02d37832016-10-25 15:25:33 +01005174void LocationsBuilderARMVIXL::HandleShift(HBinaryOperation* op) {
5175 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
5176
5177 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005178 new (GetGraph()->GetAllocator()) LocationSummary(op, LocationSummary::kNoCall);
Artem Serov02d37832016-10-25 15:25:33 +01005179
5180 switch (op->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005181 case DataType::Type::kInt32: {
Artem Serov02d37832016-10-25 15:25:33 +01005182 locations->SetInAt(0, Location::RequiresRegister());
5183 if (op->InputAt(1)->IsConstant()) {
5184 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
5185 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5186 } else {
5187 locations->SetInAt(1, Location::RequiresRegister());
5188 // Make the output overlap, as it will be used to hold the masked
5189 // second input.
5190 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
5191 }
5192 break;
5193 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005194 case DataType::Type::kInt64: {
Artem Serov02d37832016-10-25 15:25:33 +01005195 locations->SetInAt(0, Location::RequiresRegister());
5196 if (op->InputAt(1)->IsConstant()) {
5197 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
5198 // For simplicity, use kOutputOverlap even though we only require that low registers
5199 // don't clash with high registers which the register allocator currently guarantees.
5200 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
5201 } else {
5202 locations->SetInAt(1, Location::RequiresRegister());
5203 locations->AddTemp(Location::RequiresRegister());
5204 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
5205 }
5206 break;
5207 }
5208 default:
5209 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
5210 }
5211}
5212
5213void InstructionCodeGeneratorARMVIXL::HandleShift(HBinaryOperation* op) {
5214 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
5215
5216 LocationSummary* locations = op->GetLocations();
5217 Location out = locations->Out();
5218 Location first = locations->InAt(0);
5219 Location second = locations->InAt(1);
5220
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005221 DataType::Type type = op->GetResultType();
Artem Serov02d37832016-10-25 15:25:33 +01005222 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005223 case DataType::Type::kInt32: {
Artem Serov02d37832016-10-25 15:25:33 +01005224 vixl32::Register out_reg = OutputRegister(op);
5225 vixl32::Register first_reg = InputRegisterAt(op, 0);
5226 if (second.IsRegister()) {
5227 vixl32::Register second_reg = RegisterFrom(second);
5228 // ARM doesn't mask the shift count so we need to do it ourselves.
5229 __ And(out_reg, second_reg, kMaxIntShiftDistance);
5230 if (op->IsShl()) {
5231 __ Lsl(out_reg, first_reg, out_reg);
5232 } else if (op->IsShr()) {
5233 __ Asr(out_reg, first_reg, out_reg);
5234 } else {
5235 __ Lsr(out_reg, first_reg, out_reg);
5236 }
5237 } else {
Anton Kirilov644032c2016-12-06 17:51:43 +00005238 int32_t cst = Int32ConstantFrom(second);
Artem Serov02d37832016-10-25 15:25:33 +01005239 uint32_t shift_value = cst & kMaxIntShiftDistance;
5240 if (shift_value == 0) { // ARM does not support shifting with 0 immediate.
5241 __ Mov(out_reg, first_reg);
5242 } else if (op->IsShl()) {
5243 __ Lsl(out_reg, first_reg, shift_value);
5244 } else if (op->IsShr()) {
5245 __ Asr(out_reg, first_reg, shift_value);
5246 } else {
5247 __ Lsr(out_reg, first_reg, shift_value);
5248 }
5249 }
5250 break;
5251 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005252 case DataType::Type::kInt64: {
Artem Serov02d37832016-10-25 15:25:33 +01005253 vixl32::Register o_h = HighRegisterFrom(out);
5254 vixl32::Register o_l = LowRegisterFrom(out);
5255
5256 vixl32::Register high = HighRegisterFrom(first);
5257 vixl32::Register low = LowRegisterFrom(first);
5258
5259 if (second.IsRegister()) {
5260 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
5261
5262 vixl32::Register second_reg = RegisterFrom(second);
5263
5264 if (op->IsShl()) {
5265 __ And(o_l, second_reg, kMaxLongShiftDistance);
5266 // Shift the high part
5267 __ Lsl(o_h, high, o_l);
5268 // Shift the low part and `or` what overflew on the high part
Scott Wakelingb77051e2016-11-21 19:46:00 +00005269 __ Rsb(temp, o_l, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01005270 __ Lsr(temp, low, temp);
5271 __ Orr(o_h, o_h, temp);
5272 // If the shift is > 32 bits, override the high part
Scott Wakelingb77051e2016-11-21 19:46:00 +00005273 __ Subs(temp, o_l, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01005274 {
Artem Serov0fb37192016-12-06 18:13:40 +00005275 ExactAssemblyScope guard(GetVIXLAssembler(),
5276 2 * vixl32::kMaxInstructionSizeInBytes,
5277 CodeBufferCheckScope::kMaximumSize);
Artem Serov02d37832016-10-25 15:25:33 +01005278 __ it(pl);
5279 __ lsl(pl, o_h, low, temp);
5280 }
5281 // Shift the low part
5282 __ Lsl(o_l, low, o_l);
5283 } else if (op->IsShr()) {
5284 __ And(o_h, second_reg, kMaxLongShiftDistance);
5285 // Shift the low part
5286 __ Lsr(o_l, low, o_h);
5287 // Shift the high part and `or` what underflew on the low part
Scott Wakelingb77051e2016-11-21 19:46:00 +00005288 __ Rsb(temp, o_h, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01005289 __ Lsl(temp, high, temp);
5290 __ Orr(o_l, o_l, temp);
5291 // If the shift is > 32 bits, override the low part
Scott Wakelingb77051e2016-11-21 19:46:00 +00005292 __ Subs(temp, o_h, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01005293 {
Artem Serov0fb37192016-12-06 18:13:40 +00005294 ExactAssemblyScope guard(GetVIXLAssembler(),
5295 2 * vixl32::kMaxInstructionSizeInBytes,
5296 CodeBufferCheckScope::kMaximumSize);
Artem Serov02d37832016-10-25 15:25:33 +01005297 __ it(pl);
5298 __ asr(pl, o_l, high, temp);
5299 }
5300 // Shift the high part
5301 __ Asr(o_h, high, o_h);
5302 } else {
5303 __ And(o_h, second_reg, kMaxLongShiftDistance);
5304 // same as Shr except we use `Lsr`s and not `Asr`s
5305 __ Lsr(o_l, low, o_h);
Scott Wakelingb77051e2016-11-21 19:46:00 +00005306 __ Rsb(temp, o_h, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01005307 __ Lsl(temp, high, temp);
5308 __ Orr(o_l, o_l, temp);
Scott Wakelingb77051e2016-11-21 19:46:00 +00005309 __ Subs(temp, o_h, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01005310 {
Artem Serov0fb37192016-12-06 18:13:40 +00005311 ExactAssemblyScope guard(GetVIXLAssembler(),
5312 2 * vixl32::kMaxInstructionSizeInBytes,
5313 CodeBufferCheckScope::kMaximumSize);
Artem Serov02d37832016-10-25 15:25:33 +01005314 __ it(pl);
5315 __ lsr(pl, o_l, high, temp);
5316 }
5317 __ Lsr(o_h, high, o_h);
5318 }
5319 } else {
5320 // Register allocator doesn't create partial overlap.
5321 DCHECK(!o_l.Is(high));
5322 DCHECK(!o_h.Is(low));
Anton Kirilov644032c2016-12-06 17:51:43 +00005323 int32_t cst = Int32ConstantFrom(second);
Artem Serov02d37832016-10-25 15:25:33 +01005324 uint32_t shift_value = cst & kMaxLongShiftDistance;
5325 if (shift_value > 32) {
5326 if (op->IsShl()) {
5327 __ Lsl(o_h, low, shift_value - 32);
5328 __ Mov(o_l, 0);
5329 } else if (op->IsShr()) {
5330 __ Asr(o_l, high, shift_value - 32);
5331 __ Asr(o_h, high, 31);
5332 } else {
5333 __ Lsr(o_l, high, shift_value - 32);
5334 __ Mov(o_h, 0);
5335 }
5336 } else if (shift_value == 32) {
5337 if (op->IsShl()) {
5338 __ Mov(o_h, low);
5339 __ Mov(o_l, 0);
5340 } else if (op->IsShr()) {
5341 __ Mov(o_l, high);
5342 __ Asr(o_h, high, 31);
5343 } else {
5344 __ Mov(o_l, high);
5345 __ Mov(o_h, 0);
5346 }
5347 } else if (shift_value == 1) {
5348 if (op->IsShl()) {
5349 __ Lsls(o_l, low, 1);
5350 __ Adc(o_h, high, high);
5351 } else if (op->IsShr()) {
5352 __ Asrs(o_h, high, 1);
5353 __ Rrx(o_l, low);
5354 } else {
5355 __ Lsrs(o_h, high, 1);
5356 __ Rrx(o_l, low);
5357 }
5358 } else {
5359 DCHECK(2 <= shift_value && shift_value < 32) << shift_value;
5360 if (op->IsShl()) {
5361 __ Lsl(o_h, high, shift_value);
5362 __ Orr(o_h, o_h, Operand(low, ShiftType::LSR, 32 - shift_value));
5363 __ Lsl(o_l, low, shift_value);
5364 } else if (op->IsShr()) {
5365 __ Lsr(o_l, low, shift_value);
5366 __ Orr(o_l, o_l, Operand(high, ShiftType::LSL, 32 - shift_value));
5367 __ Asr(o_h, high, shift_value);
5368 } else {
5369 __ Lsr(o_l, low, shift_value);
5370 __ Orr(o_l, o_l, Operand(high, ShiftType::LSL, 32 - shift_value));
5371 __ Lsr(o_h, high, shift_value);
5372 }
5373 }
5374 }
5375 break;
5376 }
5377 default:
5378 LOG(FATAL) << "Unexpected operation type " << type;
5379 UNREACHABLE();
5380 }
5381}
5382
5383void LocationsBuilderARMVIXL::VisitShl(HShl* shl) {
5384 HandleShift(shl);
5385}
5386
5387void InstructionCodeGeneratorARMVIXL::VisitShl(HShl* shl) {
5388 HandleShift(shl);
5389}
5390
5391void LocationsBuilderARMVIXL::VisitShr(HShr* shr) {
5392 HandleShift(shr);
5393}
5394
5395void InstructionCodeGeneratorARMVIXL::VisitShr(HShr* shr) {
5396 HandleShift(shr);
5397}
5398
5399void LocationsBuilderARMVIXL::VisitUShr(HUShr* ushr) {
5400 HandleShift(ushr);
5401}
5402
5403void InstructionCodeGeneratorARMVIXL::VisitUShr(HUShr* ushr) {
5404 HandleShift(ushr);
5405}
5406
5407void LocationsBuilderARMVIXL::VisitNewInstance(HNewInstance* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005408 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5409 instruction, LocationSummary::kCallOnMainOnly);
Artem Serov02d37832016-10-25 15:25:33 +01005410 if (instruction->IsStringAlloc()) {
5411 locations->AddTemp(LocationFrom(kMethodRegister));
5412 } else {
5413 InvokeRuntimeCallingConventionARMVIXL calling_convention;
5414 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
Artem Serov02d37832016-10-25 15:25:33 +01005415 }
5416 locations->SetOut(LocationFrom(r0));
5417}
5418
5419void InstructionCodeGeneratorARMVIXL::VisitNewInstance(HNewInstance* instruction) {
5420 // Note: if heap poisoning is enabled, the entry point takes cares
5421 // of poisoning the reference.
5422 if (instruction->IsStringAlloc()) {
5423 // String is allocated through StringFactory. Call NewEmptyString entry point.
5424 vixl32::Register temp = RegisterFrom(instruction->GetLocations()->GetTemp(0));
5425 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize);
5426 GetAssembler()->LoadFromOffset(kLoadWord, temp, tr, QUICK_ENTRY_POINT(pNewEmptyString));
5427 GetAssembler()->LoadFromOffset(kLoadWord, lr, temp, code_offset.Int32Value());
Alexandre Rames374ddf32016-11-04 10:40:49 +00005428 // 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 +00005429 ExactAssemblyScope aas(GetVIXLAssembler(),
5430 vixl32::k16BitT32InstructionSizeInBytes,
5431 CodeBufferCheckScope::kExactSize);
Artem Serov02d37832016-10-25 15:25:33 +01005432 __ blx(lr);
5433 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
5434 } else {
5435 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00005436 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
Artem Serov02d37832016-10-25 15:25:33 +01005437 }
Roland Levillain5daa4952017-07-03 17:23:56 +01005438 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 10);
Artem Serov02d37832016-10-25 15:25:33 +01005439}
5440
5441void LocationsBuilderARMVIXL::VisitNewArray(HNewArray* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005442 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5443 instruction, LocationSummary::kCallOnMainOnly);
Artem Serov02d37832016-10-25 15:25:33 +01005444 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Artem Serov02d37832016-10-25 15:25:33 +01005445 locations->SetOut(LocationFrom(r0));
Nicolas Geoffray8c7c4f12017-01-26 10:13:11 +00005446 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5447 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
Artem Serov02d37832016-10-25 15:25:33 +01005448}
5449
5450void InstructionCodeGeneratorARMVIXL::VisitNewArray(HNewArray* instruction) {
Artem Serov02d37832016-10-25 15:25:33 +01005451 // Note: if heap poisoning is enabled, the entry point takes cares
5452 // of poisoning the reference.
Artem Serov7b3672e2017-02-03 17:30:34 +00005453 QuickEntrypointEnum entrypoint =
5454 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
5455 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005456 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Artem Serov7b3672e2017-02-03 17:30:34 +00005457 DCHECK(!codegen_->IsLeafMethod());
Roland Levillain5daa4952017-07-03 17:23:56 +01005458 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 11);
Artem Serov02d37832016-10-25 15:25:33 +01005459}
5460
5461void LocationsBuilderARMVIXL::VisitParameterValue(HParameterValue* instruction) {
5462 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005463 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Artem Serov02d37832016-10-25 15:25:33 +01005464 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
5465 if (location.IsStackSlot()) {
5466 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5467 } else if (location.IsDoubleStackSlot()) {
5468 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5469 }
5470 locations->SetOut(location);
5471}
5472
5473void InstructionCodeGeneratorARMVIXL::VisitParameterValue(
5474 HParameterValue* instruction ATTRIBUTE_UNUSED) {
5475 // Nothing to do, the parameter is already at its location.
5476}
5477
5478void LocationsBuilderARMVIXL::VisitCurrentMethod(HCurrentMethod* instruction) {
5479 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005480 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Artem Serov02d37832016-10-25 15:25:33 +01005481 locations->SetOut(LocationFrom(kMethodRegister));
5482}
5483
5484void InstructionCodeGeneratorARMVIXL::VisitCurrentMethod(
5485 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
5486 // Nothing to do, the method is already at its location.
5487}
5488
5489void LocationsBuilderARMVIXL::VisitNot(HNot* not_) {
5490 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005491 new (GetGraph()->GetAllocator()) LocationSummary(not_, LocationSummary::kNoCall);
Artem Serov02d37832016-10-25 15:25:33 +01005492 locations->SetInAt(0, Location::RequiresRegister());
5493 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5494}
5495
5496void InstructionCodeGeneratorARMVIXL::VisitNot(HNot* not_) {
5497 LocationSummary* locations = not_->GetLocations();
5498 Location out = locations->Out();
5499 Location in = locations->InAt(0);
5500 switch (not_->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005501 case DataType::Type::kInt32:
Artem Serov02d37832016-10-25 15:25:33 +01005502 __ Mvn(OutputRegister(not_), InputRegisterAt(not_, 0));
5503 break;
5504
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005505 case DataType::Type::kInt64:
Artem Serov02d37832016-10-25 15:25:33 +01005506 __ Mvn(LowRegisterFrom(out), LowRegisterFrom(in));
5507 __ Mvn(HighRegisterFrom(out), HighRegisterFrom(in));
5508 break;
5509
5510 default:
5511 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
5512 }
5513}
5514
Scott Wakelingc34dba72016-10-03 10:14:44 +01005515void LocationsBuilderARMVIXL::VisitBooleanNot(HBooleanNot* bool_not) {
5516 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005517 new (GetGraph()->GetAllocator()) LocationSummary(bool_not, LocationSummary::kNoCall);
Scott Wakelingc34dba72016-10-03 10:14:44 +01005518 locations->SetInAt(0, Location::RequiresRegister());
5519 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5520}
5521
5522void InstructionCodeGeneratorARMVIXL::VisitBooleanNot(HBooleanNot* bool_not) {
5523 __ Eor(OutputRegister(bool_not), InputRegister(bool_not), 1);
5524}
5525
Artem Serov02d37832016-10-25 15:25:33 +01005526void LocationsBuilderARMVIXL::VisitCompare(HCompare* compare) {
5527 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005528 new (GetGraph()->GetAllocator()) LocationSummary(compare, LocationSummary::kNoCall);
Artem Serov02d37832016-10-25 15:25:33 +01005529 switch (compare->InputAt(0)->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005530 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005531 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005532 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005533 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005534 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005535 case DataType::Type::kInt32:
5536 case DataType::Type::kInt64: {
Artem Serov02d37832016-10-25 15:25:33 +01005537 locations->SetInAt(0, Location::RequiresRegister());
5538 locations->SetInAt(1, Location::RequiresRegister());
5539 // Output overlaps because it is written before doing the low comparison.
5540 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
5541 break;
5542 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005543 case DataType::Type::kFloat32:
5544 case DataType::Type::kFloat64: {
Artem Serov02d37832016-10-25 15:25:33 +01005545 locations->SetInAt(0, Location::RequiresFpuRegister());
5546 locations->SetInAt(1, ArithmeticZeroOrFpuRegister(compare->InputAt(1)));
5547 locations->SetOut(Location::RequiresRegister());
5548 break;
5549 }
5550 default:
5551 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
5552 }
5553}
5554
5555void InstructionCodeGeneratorARMVIXL::VisitCompare(HCompare* compare) {
5556 LocationSummary* locations = compare->GetLocations();
5557 vixl32::Register out = OutputRegister(compare);
5558 Location left = locations->InAt(0);
5559 Location right = locations->InAt(1);
5560
5561 vixl32::Label less, greater, done;
Anton Kirilov6f644202017-02-27 18:29:45 +00005562 vixl32::Label* final_label = codegen_->GetFinalLabel(compare, &done);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005563 DataType::Type type = compare->InputAt(0)->GetType();
Vladimir Marko33bff252017-11-01 14:35:42 +00005564 vixl32::Condition less_cond = vixl32::Condition::None();
Artem Serov02d37832016-10-25 15:25:33 +01005565 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005566 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005567 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005568 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005569 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005570 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005571 case DataType::Type::kInt32: {
Artem Serov02d37832016-10-25 15:25:33 +01005572 // Emit move to `out` before the `Cmp`, as `Mov` might affect the status flags.
5573 __ Mov(out, 0);
5574 __ Cmp(RegisterFrom(left), RegisterFrom(right)); // Signed compare.
5575 less_cond = lt;
5576 break;
5577 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005578 case DataType::Type::kInt64: {
Artem Serov02d37832016-10-25 15:25:33 +01005579 __ Cmp(HighRegisterFrom(left), HighRegisterFrom(right)); // Signed compare.
Artem Serov517d9f62016-12-12 15:51:15 +00005580 __ B(lt, &less, /* far_target */ false);
5581 __ B(gt, &greater, /* far_target */ false);
Artem Serov02d37832016-10-25 15:25:33 +01005582 // Emit move to `out` before the last `Cmp`, as `Mov` might affect the status flags.
5583 __ Mov(out, 0);
5584 __ Cmp(LowRegisterFrom(left), LowRegisterFrom(right)); // Unsigned compare.
5585 less_cond = lo;
5586 break;
5587 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005588 case DataType::Type::kFloat32:
5589 case DataType::Type::kFloat64: {
Artem Serov02d37832016-10-25 15:25:33 +01005590 __ Mov(out, 0);
Donghui Bai426b49c2016-11-08 14:55:38 +08005591 GenerateVcmp(compare, codegen_);
Artem Serov02d37832016-10-25 15:25:33 +01005592 // To branch on the FP compare result we transfer FPSCR to APSR (encoded as PC in VMRS).
5593 __ Vmrs(RegisterOrAPSR_nzcv(kPcCode), FPSCR);
5594 less_cond = ARMFPCondition(kCondLT, compare->IsGtBias());
5595 break;
5596 }
5597 default:
5598 LOG(FATAL) << "Unexpected compare type " << type;
5599 UNREACHABLE();
5600 }
5601
Anton Kirilov6f644202017-02-27 18:29:45 +00005602 __ B(eq, final_label, /* far_target */ false);
Artem Serov517d9f62016-12-12 15:51:15 +00005603 __ B(less_cond, &less, /* far_target */ false);
Artem Serov02d37832016-10-25 15:25:33 +01005604
5605 __ Bind(&greater);
5606 __ Mov(out, 1);
Anton Kirilov6f644202017-02-27 18:29:45 +00005607 __ B(final_label);
Artem Serov02d37832016-10-25 15:25:33 +01005608
5609 __ Bind(&less);
5610 __ Mov(out, -1);
5611
Anton Kirilov6f644202017-02-27 18:29:45 +00005612 if (done.IsReferenced()) {
5613 __ Bind(&done);
5614 }
Artem Serov02d37832016-10-25 15:25:33 +01005615}
5616
5617void LocationsBuilderARMVIXL::VisitPhi(HPhi* instruction) {
5618 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005619 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Artem Serov02d37832016-10-25 15:25:33 +01005620 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
5621 locations->SetInAt(i, Location::Any());
5622 }
5623 locations->SetOut(Location::Any());
5624}
5625
5626void InstructionCodeGeneratorARMVIXL::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
5627 LOG(FATAL) << "Unreachable";
5628}
5629
5630void CodeGeneratorARMVIXL::GenerateMemoryBarrier(MemBarrierKind kind) {
5631 // TODO (ported from quick): revisit ARM barrier kinds.
5632 DmbOptions flavor = DmbOptions::ISH; // Quiet C++ warnings.
5633 switch (kind) {
5634 case MemBarrierKind::kAnyStore:
5635 case MemBarrierKind::kLoadAny:
5636 case MemBarrierKind::kAnyAny: {
5637 flavor = DmbOptions::ISH;
5638 break;
5639 }
5640 case MemBarrierKind::kStoreStore: {
5641 flavor = DmbOptions::ISHST;
5642 break;
5643 }
5644 default:
5645 LOG(FATAL) << "Unexpected memory barrier " << kind;
5646 }
5647 __ Dmb(flavor);
5648}
5649
5650void InstructionCodeGeneratorARMVIXL::GenerateWideAtomicLoad(vixl32::Register addr,
5651 uint32_t offset,
5652 vixl32::Register out_lo,
5653 vixl32::Register out_hi) {
5654 UseScratchRegisterScope temps(GetVIXLAssembler());
5655 if (offset != 0) {
5656 vixl32::Register temp = temps.Acquire();
5657 __ Add(temp, addr, offset);
5658 addr = temp;
5659 }
Scott Wakelingb77051e2016-11-21 19:46:00 +00005660 __ Ldrexd(out_lo, out_hi, MemOperand(addr));
Artem Serov02d37832016-10-25 15:25:33 +01005661}
5662
5663void InstructionCodeGeneratorARMVIXL::GenerateWideAtomicStore(vixl32::Register addr,
5664 uint32_t offset,
5665 vixl32::Register value_lo,
5666 vixl32::Register value_hi,
5667 vixl32::Register temp1,
5668 vixl32::Register temp2,
5669 HInstruction* instruction) {
5670 UseScratchRegisterScope temps(GetVIXLAssembler());
5671 vixl32::Label fail;
5672 if (offset != 0) {
5673 vixl32::Register temp = temps.Acquire();
5674 __ Add(temp, addr, offset);
5675 addr = temp;
5676 }
5677 __ Bind(&fail);
Alexandre Rames374ddf32016-11-04 10:40:49 +00005678 {
5679 // Ensure the pc position is recorded immediately after the `ldrexd` instruction.
Artem Serov0fb37192016-12-06 18:13:40 +00005680 ExactAssemblyScope aas(GetVIXLAssembler(),
5681 vixl32::kMaxInstructionSizeInBytes,
5682 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00005683 // We need a load followed by store. (The address used in a STREX instruction must
5684 // be the same as the address in the most recently executed LDREX instruction.)
5685 __ ldrexd(temp1, temp2, MemOperand(addr));
5686 codegen_->MaybeRecordImplicitNullCheck(instruction);
5687 }
Scott Wakelingb77051e2016-11-21 19:46:00 +00005688 __ Strexd(temp1, value_lo, value_hi, MemOperand(addr));
xueliang.zhongf51bc622016-11-04 09:23:32 +00005689 __ CompareAndBranchIfNonZero(temp1, &fail);
Artem Serov02d37832016-10-25 15:25:33 +01005690}
Artem Serov02109dd2016-09-23 17:17:54 +01005691
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005692void LocationsBuilderARMVIXL::HandleFieldSet(
5693 HInstruction* instruction, const FieldInfo& field_info) {
5694 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
5695
5696 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005697 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005698 locations->SetInAt(0, Location::RequiresRegister());
5699
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005700 DataType::Type field_type = field_info.GetFieldType();
5701 if (DataType::IsFloatingPointType(field_type)) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005702 locations->SetInAt(1, Location::RequiresFpuRegister());
5703 } else {
5704 locations->SetInAt(1, Location::RequiresRegister());
5705 }
5706
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005707 bool is_wide = field_type == DataType::Type::kInt64 || field_type == DataType::Type::kFloat64;
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005708 bool generate_volatile = field_info.IsVolatile()
5709 && is_wide
5710 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
5711 bool needs_write_barrier =
5712 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
5713 // Temporary registers for the write barrier.
5714 // TODO: consider renaming StoreNeedsWriteBarrier to StoreNeedsGCMark.
5715 if (needs_write_barrier) {
5716 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
5717 locations->AddTemp(Location::RequiresRegister());
5718 } else if (generate_volatile) {
5719 // ARM encoding have some additional constraints for ldrexd/strexd:
5720 // - registers need to be consecutive
5721 // - the first register should be even but not R14.
5722 // We don't test for ARM yet, and the assertion makes sure that we
5723 // revisit this if we ever enable ARM encoding.
5724 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
5725
5726 locations->AddTemp(Location::RequiresRegister());
5727 locations->AddTemp(Location::RequiresRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005728 if (field_type == DataType::Type::kFloat64) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005729 // For doubles we need two more registers to copy the value.
5730 locations->AddTemp(LocationFrom(r2));
5731 locations->AddTemp(LocationFrom(r3));
5732 }
5733 }
5734}
5735
5736void InstructionCodeGeneratorARMVIXL::HandleFieldSet(HInstruction* instruction,
5737 const FieldInfo& field_info,
5738 bool value_can_be_null) {
5739 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
5740
5741 LocationSummary* locations = instruction->GetLocations();
5742 vixl32::Register base = InputRegisterAt(instruction, 0);
5743 Location value = locations->InAt(1);
5744
5745 bool is_volatile = field_info.IsVolatile();
5746 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005747 DataType::Type field_type = field_info.GetFieldType();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005748 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
5749 bool needs_write_barrier =
5750 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
5751
5752 if (is_volatile) {
5753 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
5754 }
5755
5756 switch (field_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005757 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005758 case DataType::Type::kUint8:
5759 case DataType::Type::kInt8:
5760 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005761 case DataType::Type::kInt16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005762 case DataType::Type::kInt32: {
5763 StoreOperandType operand_type = GetStoreOperandType(field_type);
5764 GetAssembler()->StoreToOffset(operand_type, RegisterFrom(value), base, offset);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005765 break;
5766 }
5767
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005768 case DataType::Type::kReference: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005769 if (kPoisonHeapReferences && needs_write_barrier) {
5770 // Note that in the case where `value` is a null reference,
5771 // we do not enter this block, as a null reference does not
5772 // need poisoning.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005773 DCHECK_EQ(field_type, DataType::Type::kReference);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005774 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
5775 __ Mov(temp, RegisterFrom(value));
5776 GetAssembler()->PoisonHeapReference(temp);
5777 GetAssembler()->StoreToOffset(kStoreWord, temp, base, offset);
5778 } else {
5779 GetAssembler()->StoreToOffset(kStoreWord, RegisterFrom(value), base, offset);
5780 }
5781 break;
5782 }
5783
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005784 case DataType::Type::kInt64: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005785 if (is_volatile && !atomic_ldrd_strd) {
5786 GenerateWideAtomicStore(base,
5787 offset,
5788 LowRegisterFrom(value),
5789 HighRegisterFrom(value),
5790 RegisterFrom(locations->GetTemp(0)),
5791 RegisterFrom(locations->GetTemp(1)),
5792 instruction);
5793 } else {
5794 GetAssembler()->StoreToOffset(kStoreWordPair, LowRegisterFrom(value), base, offset);
5795 codegen_->MaybeRecordImplicitNullCheck(instruction);
5796 }
5797 break;
5798 }
5799
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005800 case DataType::Type::kFloat32: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005801 GetAssembler()->StoreSToOffset(SRegisterFrom(value), base, offset);
5802 break;
5803 }
5804
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005805 case DataType::Type::kFloat64: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01005806 vixl32::DRegister value_reg = DRegisterFrom(value);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005807 if (is_volatile && !atomic_ldrd_strd) {
5808 vixl32::Register value_reg_lo = RegisterFrom(locations->GetTemp(0));
5809 vixl32::Register value_reg_hi = RegisterFrom(locations->GetTemp(1));
5810
5811 __ Vmov(value_reg_lo, value_reg_hi, value_reg);
5812
5813 GenerateWideAtomicStore(base,
5814 offset,
5815 value_reg_lo,
5816 value_reg_hi,
5817 RegisterFrom(locations->GetTemp(2)),
5818 RegisterFrom(locations->GetTemp(3)),
5819 instruction);
5820 } else {
5821 GetAssembler()->StoreDToOffset(value_reg, base, offset);
5822 codegen_->MaybeRecordImplicitNullCheck(instruction);
5823 }
5824 break;
5825 }
5826
Aart Bik66c158e2018-01-31 12:55:04 -08005827 case DataType::Type::kUint32:
5828 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005829 case DataType::Type::kVoid:
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005830 LOG(FATAL) << "Unreachable type " << field_type;
5831 UNREACHABLE();
5832 }
5833
5834 // Longs and doubles are handled in the switch.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005835 if (field_type != DataType::Type::kInt64 && field_type != DataType::Type::kFloat64) {
Alexandre Rames374ddf32016-11-04 10:40:49 +00005836 // TODO(VIXL): Here and for other calls to `MaybeRecordImplicitNullCheck` in this method, we
5837 // should use a scope and the assembler to emit the store instruction to guarantee that we
5838 // record the pc at the correct position. But the `Assembler` does not automatically handle
5839 // unencodable offsets. Practically, everything is fine because the helper and VIXL, at the time
5840 // of writing, do generate the store instruction last.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005841 codegen_->MaybeRecordImplicitNullCheck(instruction);
5842 }
5843
5844 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
5845 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
5846 vixl32::Register card = RegisterFrom(locations->GetTemp(1));
5847 codegen_->MarkGCCard(temp, card, base, RegisterFrom(value), value_can_be_null);
5848 }
5849
5850 if (is_volatile) {
5851 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
5852 }
5853}
5854
Artem Serov02d37832016-10-25 15:25:33 +01005855void LocationsBuilderARMVIXL::HandleFieldGet(HInstruction* instruction,
5856 const FieldInfo& field_info) {
5857 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
5858
5859 bool object_field_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005860 kEmitCompilerReadBarrier && (field_info.GetFieldType() == DataType::Type::kReference);
Artem Serov02d37832016-10-25 15:25:33 +01005861 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005862 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
5863 object_field_get_with_read_barrier
5864 ? LocationSummary::kCallOnSlowPath
5865 : LocationSummary::kNoCall);
Artem Serov02d37832016-10-25 15:25:33 +01005866 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
5867 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
5868 }
5869 locations->SetInAt(0, Location::RequiresRegister());
5870
5871 bool volatile_for_double = field_info.IsVolatile()
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005872 && (field_info.GetFieldType() == DataType::Type::kFloat64)
Artem Serov02d37832016-10-25 15:25:33 +01005873 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
5874 // The output overlaps in case of volatile long: we don't want the
5875 // code generated by GenerateWideAtomicLoad to overwrite the
5876 // object's location. Likewise, in the case of an object field get
5877 // with read barriers enabled, we do not want the load to overwrite
5878 // the object's location, as we need it to emit the read barrier.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005879 bool overlap =
5880 (field_info.IsVolatile() && (field_info.GetFieldType() == DataType::Type::kInt64)) ||
Artem Serov02d37832016-10-25 15:25:33 +01005881 object_field_get_with_read_barrier;
5882
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005883 if (DataType::IsFloatingPointType(instruction->GetType())) {
Artem Serov02d37832016-10-25 15:25:33 +01005884 locations->SetOut(Location::RequiresFpuRegister());
5885 } else {
5886 locations->SetOut(Location::RequiresRegister(),
5887 (overlap ? Location::kOutputOverlap : Location::kNoOutputOverlap));
5888 }
5889 if (volatile_for_double) {
5890 // ARM encoding have some additional constraints for ldrexd/strexd:
5891 // - registers need to be consecutive
5892 // - the first register should be even but not R14.
5893 // We don't test for ARM yet, and the assertion makes sure that we
5894 // revisit this if we ever enable ARM encoding.
5895 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
5896 locations->AddTemp(Location::RequiresRegister());
5897 locations->AddTemp(Location::RequiresRegister());
5898 } else if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
5899 // We need a temporary register for the read barrier marking slow
Artem Serovc5fcb442016-12-02 19:19:58 +00005900 // path in CodeGeneratorARMVIXL::GenerateFieldLoadWithBakerReadBarrier.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01005901 if (kBakerReadBarrierLinkTimeThunksEnableForFields &&
5902 !Runtime::Current()->UseJitCompilation()) {
5903 // If link-time thunks for the Baker read barrier are enabled, for AOT
5904 // loads we need a temporary only if the offset is too big.
5905 if (field_info.GetFieldOffset().Uint32Value() >= kReferenceLoadMinFarOffset) {
5906 locations->AddTemp(Location::RequiresRegister());
5907 }
5908 // And we always need the reserved entrypoint register.
5909 locations->AddTemp(Location::RegisterLocation(kBakerCcEntrypointRegister.GetCode()));
5910 } else {
5911 locations->AddTemp(Location::RequiresRegister());
5912 }
Artem Serov02d37832016-10-25 15:25:33 +01005913 }
5914}
5915
5916Location LocationsBuilderARMVIXL::ArithmeticZeroOrFpuRegister(HInstruction* input) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005917 DCHECK(DataType::IsFloatingPointType(input->GetType())) << input->GetType();
Artem Serov02d37832016-10-25 15:25:33 +01005918 if ((input->IsFloatConstant() && (input->AsFloatConstant()->IsArithmeticZero())) ||
5919 (input->IsDoubleConstant() && (input->AsDoubleConstant()->IsArithmeticZero()))) {
5920 return Location::ConstantLocation(input->AsConstant());
5921 } else {
5922 return Location::RequiresFpuRegister();
5923 }
5924}
5925
Artem Serov02109dd2016-09-23 17:17:54 +01005926Location LocationsBuilderARMVIXL::ArmEncodableConstantOrRegister(HInstruction* constant,
5927 Opcode opcode) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005928 DCHECK(!DataType::IsFloatingPointType(constant->GetType()));
Artem Serov02109dd2016-09-23 17:17:54 +01005929 if (constant->IsConstant() &&
5930 CanEncodeConstantAsImmediate(constant->AsConstant(), opcode)) {
5931 return Location::ConstantLocation(constant->AsConstant());
5932 }
5933 return Location::RequiresRegister();
5934}
5935
Vladimir Markof0a6a1d2018-01-08 14:23:56 +00005936static bool CanEncode32BitConstantAsImmediate(
5937 CodeGeneratorARMVIXL* codegen,
5938 uint32_t value,
5939 Opcode opcode,
5940 vixl32::FlagsUpdate flags_update = vixl32::FlagsUpdate::DontCare) {
5941 ArmVIXLAssembler* assembler = codegen->GetAssembler();
5942 if (assembler->ShifterOperandCanHold(opcode, value, flags_update)) {
Artem Serov02109dd2016-09-23 17:17:54 +01005943 return true;
5944 }
5945 Opcode neg_opcode = kNoOperand;
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00005946 uint32_t neg_value = 0;
Artem Serov02109dd2016-09-23 17:17:54 +01005947 switch (opcode) {
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00005948 case AND: neg_opcode = BIC; neg_value = ~value; break;
5949 case ORR: neg_opcode = ORN; neg_value = ~value; break;
5950 case ADD: neg_opcode = SUB; neg_value = -value; break;
5951 case ADC: neg_opcode = SBC; neg_value = ~value; break;
5952 case SUB: neg_opcode = ADD; neg_value = -value; break;
5953 case SBC: neg_opcode = ADC; neg_value = ~value; break;
5954 case MOV: neg_opcode = MVN; neg_value = ~value; break;
Artem Serov02109dd2016-09-23 17:17:54 +01005955 default:
5956 return false;
5957 }
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00005958
Vladimir Markof0a6a1d2018-01-08 14:23:56 +00005959 if (assembler->ShifterOperandCanHold(neg_opcode, neg_value, flags_update)) {
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00005960 return true;
5961 }
5962
5963 return opcode == AND && IsPowerOfTwo(value + 1);
Artem Serov02109dd2016-09-23 17:17:54 +01005964}
5965
Vladimir Markof0a6a1d2018-01-08 14:23:56 +00005966bool LocationsBuilderARMVIXL::CanEncodeConstantAsImmediate(HConstant* input_cst, Opcode opcode) {
5967 uint64_t value = static_cast<uint64_t>(Int64FromConstant(input_cst));
5968 if (DataType::Is64BitType(input_cst->GetType())) {
5969 Opcode high_opcode = opcode;
5970 vixl32::FlagsUpdate low_flags_update = vixl32::FlagsUpdate::DontCare;
5971 switch (opcode) {
5972 case SUB:
5973 // Flip the operation to an ADD.
5974 value = -value;
5975 opcode = ADD;
5976 FALLTHROUGH_INTENDED;
5977 case ADD:
5978 if (Low32Bits(value) == 0u) {
5979 return CanEncode32BitConstantAsImmediate(codegen_, High32Bits(value), opcode);
5980 }
5981 high_opcode = ADC;
5982 low_flags_update = vixl32::FlagsUpdate::SetFlags;
5983 break;
5984 default:
5985 break;
5986 }
5987 return CanEncode32BitConstantAsImmediate(codegen_, High32Bits(value), high_opcode) &&
5988 CanEncode32BitConstantAsImmediate(codegen_, Low32Bits(value), opcode, low_flags_update);
5989 } else {
5990 return CanEncode32BitConstantAsImmediate(codegen_, Low32Bits(value), opcode);
5991 }
5992}
5993
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005994void InstructionCodeGeneratorARMVIXL::HandleFieldGet(HInstruction* instruction,
5995 const FieldInfo& field_info) {
5996 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
5997
5998 LocationSummary* locations = instruction->GetLocations();
5999 vixl32::Register base = InputRegisterAt(instruction, 0);
6000 Location out = locations->Out();
6001 bool is_volatile = field_info.IsVolatile();
6002 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Vladimir Marko61b92282017-10-11 13:23:17 +01006003 DCHECK_EQ(DataType::Size(field_info.GetFieldType()), DataType::Size(instruction->GetType()));
6004 DataType::Type load_type = instruction->GetType();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006005 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
6006
Vladimir Marko61b92282017-10-11 13:23:17 +01006007 switch (load_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006008 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006009 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006010 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006011 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006012 case DataType::Type::kInt16:
6013 case DataType::Type::kInt32: {
Vladimir Marko61b92282017-10-11 13:23:17 +01006014 LoadOperandType operand_type = GetLoadOperandType(load_type);
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006015 GetAssembler()->LoadFromOffset(operand_type, RegisterFrom(out), base, offset);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006016 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006017 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006018
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006019 case DataType::Type::kReference: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006020 // /* HeapReference<Object> */ out = *(base + offset)
6021 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006022 Location temp_loc = locations->GetTemp(0);
6023 // Note that a potential implicit null check is handled in this
6024 // CodeGeneratorARMVIXL::GenerateFieldLoadWithBakerReadBarrier call.
6025 codegen_->GenerateFieldLoadWithBakerReadBarrier(
6026 instruction, out, base, offset, temp_loc, /* needs_null_check */ true);
6027 if (is_volatile) {
6028 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6029 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006030 } else {
6031 GetAssembler()->LoadFromOffset(kLoadWord, RegisterFrom(out), base, offset);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006032 codegen_->MaybeRecordImplicitNullCheck(instruction);
6033 if (is_volatile) {
6034 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6035 }
6036 // If read barriers are enabled, emit read barriers other than
6037 // Baker's using a slow path (and also unpoison the loaded
6038 // reference, if heap poisoning is enabled).
6039 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, locations->InAt(0), offset);
6040 }
6041 break;
6042 }
6043
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006044 case DataType::Type::kInt64:
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006045 if (is_volatile && !atomic_ldrd_strd) {
6046 GenerateWideAtomicLoad(base, offset, LowRegisterFrom(out), HighRegisterFrom(out));
6047 } else {
6048 GetAssembler()->LoadFromOffset(kLoadWordPair, LowRegisterFrom(out), base, offset);
6049 }
6050 break;
6051
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006052 case DataType::Type::kFloat32:
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006053 GetAssembler()->LoadSFromOffset(SRegisterFrom(out), base, offset);
6054 break;
6055
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006056 case DataType::Type::kFloat64: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006057 vixl32::DRegister out_dreg = DRegisterFrom(out);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006058 if (is_volatile && !atomic_ldrd_strd) {
6059 vixl32::Register lo = RegisterFrom(locations->GetTemp(0));
6060 vixl32::Register hi = RegisterFrom(locations->GetTemp(1));
6061 GenerateWideAtomicLoad(base, offset, lo, hi);
6062 // TODO(VIXL): Do we need to be immediately after the ldrexd instruction? If so we need a
6063 // scope.
6064 codegen_->MaybeRecordImplicitNullCheck(instruction);
6065 __ Vmov(out_dreg, lo, hi);
6066 } else {
6067 GetAssembler()->LoadDFromOffset(out_dreg, base, offset);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006068 codegen_->MaybeRecordImplicitNullCheck(instruction);
6069 }
6070 break;
6071 }
6072
Aart Bik66c158e2018-01-31 12:55:04 -08006073 case DataType::Type::kUint32:
6074 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006075 case DataType::Type::kVoid:
Vladimir Marko61b92282017-10-11 13:23:17 +01006076 LOG(FATAL) << "Unreachable type " << load_type;
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006077 UNREACHABLE();
6078 }
6079
Vladimir Marko61b92282017-10-11 13:23:17 +01006080 if (load_type == DataType::Type::kReference || load_type == DataType::Type::kFloat64) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006081 // Potential implicit null checks, in the case of reference or
6082 // double fields, are handled in the previous switch statement.
6083 } else {
6084 // Address cases other than reference and double that may require an implicit null check.
Alexandre Rames374ddf32016-11-04 10:40:49 +00006085 // TODO(VIXL): Here and for other calls to `MaybeRecordImplicitNullCheck` in this method, we
6086 // should use a scope and the assembler to emit the load instruction to guarantee that we
6087 // record the pc at the correct position. But the `Assembler` does not automatically handle
6088 // unencodable offsets. Practically, everything is fine because the helper and VIXL, at the time
6089 // of writing, do generate the store instruction last.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006090 codegen_->MaybeRecordImplicitNullCheck(instruction);
6091 }
6092
6093 if (is_volatile) {
Vladimir Marko61b92282017-10-11 13:23:17 +01006094 if (load_type == DataType::Type::kReference) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006095 // Memory barriers, in the case of references, are also handled
6096 // in the previous switch statement.
6097 } else {
6098 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6099 }
6100 }
6101}
6102
6103void LocationsBuilderARMVIXL::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
6104 HandleFieldSet(instruction, instruction->GetFieldInfo());
6105}
6106
6107void InstructionCodeGeneratorARMVIXL::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
6108 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
6109}
6110
6111void LocationsBuilderARMVIXL::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
6112 HandleFieldGet(instruction, instruction->GetFieldInfo());
6113}
6114
6115void InstructionCodeGeneratorARMVIXL::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
6116 HandleFieldGet(instruction, instruction->GetFieldInfo());
6117}
6118
6119void LocationsBuilderARMVIXL::VisitStaticFieldGet(HStaticFieldGet* instruction) {
6120 HandleFieldGet(instruction, instruction->GetFieldInfo());
6121}
6122
6123void InstructionCodeGeneratorARMVIXL::VisitStaticFieldGet(HStaticFieldGet* instruction) {
6124 HandleFieldGet(instruction, instruction->GetFieldInfo());
6125}
6126
Scott Wakelingc34dba72016-10-03 10:14:44 +01006127void LocationsBuilderARMVIXL::VisitStaticFieldSet(HStaticFieldSet* instruction) {
6128 HandleFieldSet(instruction, instruction->GetFieldInfo());
6129}
6130
6131void InstructionCodeGeneratorARMVIXL::VisitStaticFieldSet(HStaticFieldSet* instruction) {
6132 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
6133}
6134
Artem Serovcfbe9132016-10-14 15:58:56 +01006135void LocationsBuilderARMVIXL::VisitUnresolvedInstanceFieldGet(
6136 HUnresolvedInstanceFieldGet* instruction) {
6137 FieldAccessCallingConventionARMVIXL calling_convention;
6138 codegen_->CreateUnresolvedFieldLocationSummary(
6139 instruction, instruction->GetFieldType(), calling_convention);
6140}
6141
6142void InstructionCodeGeneratorARMVIXL::VisitUnresolvedInstanceFieldGet(
6143 HUnresolvedInstanceFieldGet* instruction) {
6144 FieldAccessCallingConventionARMVIXL calling_convention;
6145 codegen_->GenerateUnresolvedFieldAccess(instruction,
6146 instruction->GetFieldType(),
6147 instruction->GetFieldIndex(),
6148 instruction->GetDexPc(),
6149 calling_convention);
6150}
6151
6152void LocationsBuilderARMVIXL::VisitUnresolvedInstanceFieldSet(
6153 HUnresolvedInstanceFieldSet* instruction) {
6154 FieldAccessCallingConventionARMVIXL calling_convention;
6155 codegen_->CreateUnresolvedFieldLocationSummary(
6156 instruction, instruction->GetFieldType(), calling_convention);
6157}
6158
6159void InstructionCodeGeneratorARMVIXL::VisitUnresolvedInstanceFieldSet(
6160 HUnresolvedInstanceFieldSet* instruction) {
6161 FieldAccessCallingConventionARMVIXL calling_convention;
6162 codegen_->GenerateUnresolvedFieldAccess(instruction,
6163 instruction->GetFieldType(),
6164 instruction->GetFieldIndex(),
6165 instruction->GetDexPc(),
6166 calling_convention);
6167}
6168
6169void LocationsBuilderARMVIXL::VisitUnresolvedStaticFieldGet(
6170 HUnresolvedStaticFieldGet* instruction) {
6171 FieldAccessCallingConventionARMVIXL calling_convention;
6172 codegen_->CreateUnresolvedFieldLocationSummary(
6173 instruction, instruction->GetFieldType(), calling_convention);
6174}
6175
6176void InstructionCodeGeneratorARMVIXL::VisitUnresolvedStaticFieldGet(
6177 HUnresolvedStaticFieldGet* instruction) {
6178 FieldAccessCallingConventionARMVIXL calling_convention;
6179 codegen_->GenerateUnresolvedFieldAccess(instruction,
6180 instruction->GetFieldType(),
6181 instruction->GetFieldIndex(),
6182 instruction->GetDexPc(),
6183 calling_convention);
6184}
6185
6186void LocationsBuilderARMVIXL::VisitUnresolvedStaticFieldSet(
6187 HUnresolvedStaticFieldSet* instruction) {
6188 FieldAccessCallingConventionARMVIXL calling_convention;
6189 codegen_->CreateUnresolvedFieldLocationSummary(
6190 instruction, instruction->GetFieldType(), calling_convention);
6191}
6192
6193void InstructionCodeGeneratorARMVIXL::VisitUnresolvedStaticFieldSet(
6194 HUnresolvedStaticFieldSet* instruction) {
6195 FieldAccessCallingConventionARMVIXL calling_convention;
6196 codegen_->GenerateUnresolvedFieldAccess(instruction,
6197 instruction->GetFieldType(),
6198 instruction->GetFieldIndex(),
6199 instruction->GetDexPc(),
6200 calling_convention);
6201}
6202
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006203void LocationsBuilderARMVIXL::VisitNullCheck(HNullCheck* instruction) {
Artem Serov657022c2016-11-23 14:19:38 +00006204 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006205 locations->SetInAt(0, Location::RequiresRegister());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006206}
6207
6208void CodeGeneratorARMVIXL::GenerateImplicitNullCheck(HNullCheck* instruction) {
6209 if (CanMoveNullCheckToUser(instruction)) {
6210 return;
6211 }
6212
6213 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames374ddf32016-11-04 10:40:49 +00006214 // Ensure the pc position is recorded immediately after the `ldr` instruction.
Artem Serov0fb37192016-12-06 18:13:40 +00006215 ExactAssemblyScope aas(GetVIXLAssembler(),
6216 vixl32::kMaxInstructionSizeInBytes,
6217 CodeBufferCheckScope::kMaximumSize);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006218 __ ldr(temps.Acquire(), MemOperand(InputRegisterAt(instruction, 0)));
6219 RecordPcInfo(instruction, instruction->GetDexPc());
6220}
6221
6222void CodeGeneratorARMVIXL::GenerateExplicitNullCheck(HNullCheck* instruction) {
6223 NullCheckSlowPathARMVIXL* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006224 new (GetScopedAllocator()) NullCheckSlowPathARMVIXL(instruction);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006225 AddSlowPath(slow_path);
xueliang.zhongf51bc622016-11-04 09:23:32 +00006226 __ CompareAndBranchIfZero(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006227}
6228
6229void InstructionCodeGeneratorARMVIXL::VisitNullCheck(HNullCheck* instruction) {
6230 codegen_->GenerateNullCheck(instruction);
6231}
6232
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006233void CodeGeneratorARMVIXL::LoadFromShiftedRegOffset(DataType::Type type,
Scott Wakelingc34dba72016-10-03 10:14:44 +01006234 Location out_loc,
6235 vixl32::Register base,
6236 vixl32::Register reg_index,
6237 vixl32::Condition cond) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006238 uint32_t shift_count = DataType::SizeShift(type);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006239 MemOperand mem_address(base, reg_index, vixl32::LSL, shift_count);
6240
6241 switch (type) {
Vladimir Marko61b92282017-10-11 13:23:17 +01006242 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006243 case DataType::Type::kUint8:
Vladimir Marko61b92282017-10-11 13:23:17 +01006244 __ Ldrb(cond, RegisterFrom(out_loc), mem_address);
6245 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006246 case DataType::Type::kInt8:
Scott Wakelingc34dba72016-10-03 10:14:44 +01006247 __ Ldrsb(cond, RegisterFrom(out_loc), mem_address);
6248 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006249 case DataType::Type::kUint16:
Scott Wakelingc34dba72016-10-03 10:14:44 +01006250 __ Ldrh(cond, RegisterFrom(out_loc), mem_address);
6251 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006252 case DataType::Type::kInt16:
6253 __ Ldrsh(cond, RegisterFrom(out_loc), mem_address);
6254 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006255 case DataType::Type::kReference:
6256 case DataType::Type::kInt32:
Scott Wakelingc34dba72016-10-03 10:14:44 +01006257 __ Ldr(cond, RegisterFrom(out_loc), mem_address);
6258 break;
6259 // T32 doesn't support LoadFromShiftedRegOffset mem address mode for these types.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006260 case DataType::Type::kInt64:
6261 case DataType::Type::kFloat32:
6262 case DataType::Type::kFloat64:
Scott Wakelingc34dba72016-10-03 10:14:44 +01006263 default:
6264 LOG(FATAL) << "Unreachable type " << type;
6265 UNREACHABLE();
6266 }
6267}
6268
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006269void CodeGeneratorARMVIXL::StoreToShiftedRegOffset(DataType::Type type,
Scott Wakelingc34dba72016-10-03 10:14:44 +01006270 Location loc,
6271 vixl32::Register base,
6272 vixl32::Register reg_index,
6273 vixl32::Condition cond) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006274 uint32_t shift_count = DataType::SizeShift(type);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006275 MemOperand mem_address(base, reg_index, vixl32::LSL, shift_count);
6276
6277 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006278 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006279 case DataType::Type::kUint8:
6280 case DataType::Type::kInt8:
Scott Wakelingc34dba72016-10-03 10:14:44 +01006281 __ Strb(cond, RegisterFrom(loc), mem_address);
6282 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006283 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006284 case DataType::Type::kInt16:
Scott Wakelingc34dba72016-10-03 10:14:44 +01006285 __ Strh(cond, RegisterFrom(loc), mem_address);
6286 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006287 case DataType::Type::kReference:
6288 case DataType::Type::kInt32:
Scott Wakelingc34dba72016-10-03 10:14:44 +01006289 __ Str(cond, RegisterFrom(loc), mem_address);
6290 break;
6291 // T32 doesn't support StoreToShiftedRegOffset mem address mode for these types.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006292 case DataType::Type::kInt64:
6293 case DataType::Type::kFloat32:
6294 case DataType::Type::kFloat64:
Scott Wakelingc34dba72016-10-03 10:14:44 +01006295 default:
6296 LOG(FATAL) << "Unreachable type " << type;
6297 UNREACHABLE();
6298 }
6299}
6300
6301void LocationsBuilderARMVIXL::VisitArrayGet(HArrayGet* instruction) {
6302 bool object_array_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006303 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006304 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006305 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
6306 object_array_get_with_read_barrier
6307 ? LocationSummary::kCallOnSlowPath
6308 : LocationSummary::kNoCall);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006309 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006310 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Scott Wakelingc34dba72016-10-03 10:14:44 +01006311 }
6312 locations->SetInAt(0, Location::RequiresRegister());
6313 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006314 if (DataType::IsFloatingPointType(instruction->GetType())) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006315 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
6316 } else {
6317 // The output overlaps in the case of an object array get with
6318 // read barriers enabled: we do not want the move to overwrite the
6319 // array's location, as we need it to emit the read barrier.
6320 locations->SetOut(
6321 Location::RequiresRegister(),
6322 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
6323 }
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01006324 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
6325 // We need a temporary register for the read barrier marking slow
6326 // path in CodeGeneratorARMVIXL::GenerateArrayLoadWithBakerReadBarrier.
6327 if (kBakerReadBarrierLinkTimeThunksEnableForFields &&
6328 !Runtime::Current()->UseJitCompilation() &&
6329 instruction->GetIndex()->IsConstant()) {
6330 // Array loads with constant index are treated as field loads.
6331 // If link-time thunks for the Baker read barrier are enabled, for AOT
6332 // constant index loads we need a temporary only if the offset is too big.
6333 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
6334 uint32_t index = instruction->GetIndex()->AsIntConstant()->GetValue();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006335 offset += index << DataType::SizeShift(DataType::Type::kReference);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01006336 if (offset >= kReferenceLoadMinFarOffset) {
6337 locations->AddTemp(Location::RequiresRegister());
6338 }
6339 // And we always need the reserved entrypoint register.
6340 locations->AddTemp(Location::RegisterLocation(kBakerCcEntrypointRegister.GetCode()));
6341 } else if (kBakerReadBarrierLinkTimeThunksEnableForArrays &&
6342 !Runtime::Current()->UseJitCompilation() &&
6343 !instruction->GetIndex()->IsConstant()) {
6344 // We need a non-scratch temporary for the array data pointer.
6345 locations->AddTemp(Location::RequiresRegister());
6346 // And we always need the reserved entrypoint register.
6347 locations->AddTemp(Location::RegisterLocation(kBakerCcEntrypointRegister.GetCode()));
6348 } else {
6349 locations->AddTemp(Location::RequiresRegister());
6350 }
6351 } else if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
6352 // Also need a temporary for String compression feature.
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006353 locations->AddTemp(Location::RequiresRegister());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006354 }
6355}
6356
6357void InstructionCodeGeneratorARMVIXL::VisitArrayGet(HArrayGet* instruction) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006358 LocationSummary* locations = instruction->GetLocations();
6359 Location obj_loc = locations->InAt(0);
6360 vixl32::Register obj = InputRegisterAt(instruction, 0);
6361 Location index = locations->InAt(1);
6362 Location out_loc = locations->Out();
6363 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006364 DataType::Type type = instruction->GetType();
Scott Wakelingc34dba72016-10-03 10:14:44 +01006365 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
6366 instruction->IsStringCharAt();
6367 HInstruction* array_instr = instruction->GetArray();
6368 bool has_intermediate_address = array_instr->IsIntermediateAddress();
Scott Wakelingc34dba72016-10-03 10:14:44 +01006369
6370 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006371 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006372 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006373 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006374 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006375 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006376 case DataType::Type::kInt32: {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006377 vixl32::Register length;
6378 if (maybe_compressed_char_at) {
6379 length = RegisterFrom(locations->GetTemp(0));
6380 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
6381 GetAssembler()->LoadFromOffset(kLoadWord, length, obj, count_offset);
6382 codegen_->MaybeRecordImplicitNullCheck(instruction);
6383 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006384 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006385 int32_t const_index = Int32ConstantFrom(index);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006386 if (maybe_compressed_char_at) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006387 vixl32::Label uncompressed_load, done;
Anton Kirilov6f644202017-02-27 18:29:45 +00006388 vixl32::Label* final_label = codegen_->GetFinalLabel(instruction, &done);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006389 __ Lsrs(length, length, 1u); // LSRS has a 16-bit encoding, TST (immediate) does not.
6390 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
6391 "Expecting 0=compressed, 1=uncompressed");
Artem Serov517d9f62016-12-12 15:51:15 +00006392 __ B(cs, &uncompressed_load, /* far_target */ false);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006393 GetAssembler()->LoadFromOffset(kLoadUnsignedByte,
6394 RegisterFrom(out_loc),
6395 obj,
6396 data_offset + const_index);
Anton Kirilov6f644202017-02-27 18:29:45 +00006397 __ B(final_label);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006398 __ Bind(&uncompressed_load);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006399 GetAssembler()->LoadFromOffset(GetLoadOperandType(DataType::Type::kUint16),
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006400 RegisterFrom(out_loc),
6401 obj,
6402 data_offset + (const_index << 1));
Anton Kirilov6f644202017-02-27 18:29:45 +00006403 if (done.IsReferenced()) {
6404 __ Bind(&done);
6405 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006406 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006407 uint32_t full_offset = data_offset + (const_index << DataType::SizeShift(type));
Scott Wakelingc34dba72016-10-03 10:14:44 +01006408
6409 LoadOperandType load_type = GetLoadOperandType(type);
6410 GetAssembler()->LoadFromOffset(load_type, RegisterFrom(out_loc), obj, full_offset);
6411 }
6412 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006413 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006414 vixl32::Register temp = temps.Acquire();
6415
6416 if (has_intermediate_address) {
Artem Serov2bbc9532016-10-21 11:51:50 +01006417 // We do not need to compute the intermediate address from the array: the
6418 // input instruction has done it already. See the comment in
6419 // `TryExtractArrayAccessAddress()`.
6420 if (kIsDebugBuild) {
6421 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
Anton Kirilov644032c2016-12-06 17:51:43 +00006422 DCHECK_EQ(Uint64ConstantFrom(tmp->GetOffset()), data_offset);
Artem Serov2bbc9532016-10-21 11:51:50 +01006423 }
6424 temp = obj;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006425 } else {
6426 __ Add(temp, obj, data_offset);
6427 }
6428 if (maybe_compressed_char_at) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006429 vixl32::Label uncompressed_load, done;
Anton Kirilov6f644202017-02-27 18:29:45 +00006430 vixl32::Label* final_label = codegen_->GetFinalLabel(instruction, &done);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006431 __ Lsrs(length, length, 1u); // LSRS has a 16-bit encoding, TST (immediate) does not.
6432 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
6433 "Expecting 0=compressed, 1=uncompressed");
Artem Serov517d9f62016-12-12 15:51:15 +00006434 __ B(cs, &uncompressed_load, /* far_target */ false);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006435 __ Ldrb(RegisterFrom(out_loc), MemOperand(temp, RegisterFrom(index), vixl32::LSL, 0));
Anton Kirilov6f644202017-02-27 18:29:45 +00006436 __ B(final_label);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006437 __ Bind(&uncompressed_load);
6438 __ Ldrh(RegisterFrom(out_loc), MemOperand(temp, RegisterFrom(index), vixl32::LSL, 1));
Anton Kirilov6f644202017-02-27 18:29:45 +00006439 if (done.IsReferenced()) {
6440 __ Bind(&done);
6441 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006442 } else {
6443 codegen_->LoadFromShiftedRegOffset(type, out_loc, temp, RegisterFrom(index));
6444 }
6445 }
6446 break;
6447 }
6448
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006449 case DataType::Type::kReference: {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006450 // The read barrier instrumentation of object ArrayGet
6451 // instructions does not support the HIntermediateAddress
6452 // instruction.
6453 DCHECK(!(has_intermediate_address && kEmitCompilerReadBarrier));
6454
Scott Wakelingc34dba72016-10-03 10:14:44 +01006455 static_assert(
6456 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6457 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
6458 // /* HeapReference<Object> */ out =
6459 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
6460 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006461 Location temp = locations->GetTemp(0);
6462 // Note that a potential implicit null check is handled in this
6463 // CodeGeneratorARMVIXL::GenerateArrayLoadWithBakerReadBarrier call.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01006464 DCHECK(!instruction->CanDoImplicitNullCheckOn(instruction->InputAt(0)));
6465 if (index.IsConstant()) {
6466 // Array load with a constant index can be treated as a field load.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006467 data_offset += Int32ConstantFrom(index) << DataType::SizeShift(type);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01006468 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6469 out_loc,
6470 obj,
6471 data_offset,
6472 locations->GetTemp(0),
6473 /* needs_null_check */ false);
6474 } else {
6475 codegen_->GenerateArrayLoadWithBakerReadBarrier(
6476 instruction, out_loc, obj, data_offset, index, temp, /* needs_null_check */ false);
6477 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006478 } else {
6479 vixl32::Register out = OutputRegister(instruction);
6480 if (index.IsConstant()) {
6481 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00006482 (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006483 GetAssembler()->LoadFromOffset(kLoadWord, out, obj, offset);
Alexandre Rames374ddf32016-11-04 10:40:49 +00006484 // TODO(VIXL): Here and for other calls to `MaybeRecordImplicitNullCheck` in this method,
6485 // we should use a scope and the assembler to emit the load instruction to guarantee that
6486 // we record the pc at the correct position. But the `Assembler` does not automatically
6487 // handle unencodable offsets. Practically, everything is fine because the helper and
6488 // VIXL, at the time of writing, do generate the store instruction last.
Scott Wakelingc34dba72016-10-03 10:14:44 +01006489 codegen_->MaybeRecordImplicitNullCheck(instruction);
6490 // If read barriers are enabled, emit read barriers other than
6491 // Baker's using a slow path (and also unpoison the loaded
6492 // reference, if heap poisoning is enabled).
6493 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
6494 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006495 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006496 vixl32::Register temp = temps.Acquire();
6497
6498 if (has_intermediate_address) {
Artem Serov2bbc9532016-10-21 11:51:50 +01006499 // We do not need to compute the intermediate address from the array: the
6500 // input instruction has done it already. See the comment in
6501 // `TryExtractArrayAccessAddress()`.
6502 if (kIsDebugBuild) {
6503 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
Anton Kirilov644032c2016-12-06 17:51:43 +00006504 DCHECK_EQ(Uint64ConstantFrom(tmp->GetOffset()), data_offset);
Artem Serov2bbc9532016-10-21 11:51:50 +01006505 }
6506 temp = obj;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006507 } else {
6508 __ Add(temp, obj, data_offset);
6509 }
6510 codegen_->LoadFromShiftedRegOffset(type, out_loc, temp, RegisterFrom(index));
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006511 temps.Close();
Alexandre Rames374ddf32016-11-04 10:40:49 +00006512 // TODO(VIXL): Use a scope to ensure that we record the pc position immediately after the
6513 // load instruction. Practically, everything is fine because the helper and VIXL, at the
6514 // time of writing, do generate the store instruction last.
Scott Wakelingc34dba72016-10-03 10:14:44 +01006515 codegen_->MaybeRecordImplicitNullCheck(instruction);
6516 // If read barriers are enabled, emit read barriers other than
6517 // Baker's using a slow path (and also unpoison the loaded
6518 // reference, if heap poisoning is enabled).
6519 codegen_->MaybeGenerateReadBarrierSlow(
6520 instruction, out_loc, out_loc, obj_loc, data_offset, index);
6521 }
6522 }
6523 break;
6524 }
6525
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006526 case DataType::Type::kInt64: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006527 if (index.IsConstant()) {
6528 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00006529 (Int32ConstantFrom(index) << TIMES_8) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006530 GetAssembler()->LoadFromOffset(kLoadWordPair, LowRegisterFrom(out_loc), obj, offset);
6531 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006532 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006533 vixl32::Register temp = temps.Acquire();
6534 __ Add(temp, obj, Operand(RegisterFrom(index), vixl32::LSL, TIMES_8));
6535 GetAssembler()->LoadFromOffset(kLoadWordPair, LowRegisterFrom(out_loc), temp, data_offset);
6536 }
6537 break;
6538 }
6539
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006540 case DataType::Type::kFloat32: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006541 vixl32::SRegister out = SRegisterFrom(out_loc);
6542 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006543 size_t offset = (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006544 GetAssembler()->LoadSFromOffset(out, obj, offset);
6545 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006546 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006547 vixl32::Register temp = temps.Acquire();
6548 __ Add(temp, obj, Operand(RegisterFrom(index), vixl32::LSL, TIMES_4));
6549 GetAssembler()->LoadSFromOffset(out, temp, data_offset);
6550 }
6551 break;
6552 }
6553
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006554 case DataType::Type::kFloat64: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006555 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006556 size_t offset = (Int32ConstantFrom(index) << TIMES_8) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006557 GetAssembler()->LoadDFromOffset(DRegisterFrom(out_loc), obj, offset);
6558 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006559 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006560 vixl32::Register temp = temps.Acquire();
6561 __ Add(temp, obj, Operand(RegisterFrom(index), vixl32::LSL, TIMES_8));
6562 GetAssembler()->LoadDFromOffset(DRegisterFrom(out_loc), temp, data_offset);
6563 }
6564 break;
6565 }
6566
Aart Bik66c158e2018-01-31 12:55:04 -08006567 case DataType::Type::kUint32:
6568 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006569 case DataType::Type::kVoid:
Scott Wakelingc34dba72016-10-03 10:14:44 +01006570 LOG(FATAL) << "Unreachable type " << type;
6571 UNREACHABLE();
6572 }
6573
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006574 if (type == DataType::Type::kReference) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006575 // Potential implicit null checks, in the case of reference
6576 // arrays, are handled in the previous switch statement.
6577 } else if (!maybe_compressed_char_at) {
Alexandre Rames374ddf32016-11-04 10:40:49 +00006578 // TODO(VIXL): Use a scope to ensure we record the pc info immediately after
6579 // the preceding load instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01006580 codegen_->MaybeRecordImplicitNullCheck(instruction);
6581 }
6582}
6583
6584void LocationsBuilderARMVIXL::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006585 DataType::Type value_type = instruction->GetComponentType();
Scott Wakelingc34dba72016-10-03 10:14:44 +01006586
6587 bool needs_write_barrier =
6588 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
6589 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
6590
Vladimir Markoca6fff82017-10-03 14:49:14 +01006591 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Scott Wakelingc34dba72016-10-03 10:14:44 +01006592 instruction,
6593 may_need_runtime_call_for_type_check ?
6594 LocationSummary::kCallOnSlowPath :
6595 LocationSummary::kNoCall);
6596
6597 locations->SetInAt(0, Location::RequiresRegister());
6598 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006599 if (DataType::IsFloatingPointType(value_type)) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006600 locations->SetInAt(2, Location::RequiresFpuRegister());
6601 } else {
6602 locations->SetInAt(2, Location::RequiresRegister());
6603 }
6604 if (needs_write_barrier) {
6605 // Temporary registers for the write barrier.
6606 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
6607 locations->AddTemp(Location::RequiresRegister());
6608 }
6609}
6610
6611void InstructionCodeGeneratorARMVIXL::VisitArraySet(HArraySet* instruction) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006612 LocationSummary* locations = instruction->GetLocations();
6613 vixl32::Register array = InputRegisterAt(instruction, 0);
6614 Location index = locations->InAt(1);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006615 DataType::Type value_type = instruction->GetComponentType();
Scott Wakelingc34dba72016-10-03 10:14:44 +01006616 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
6617 bool needs_write_barrier =
6618 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
6619 uint32_t data_offset =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006620 mirror::Array::DataOffset(DataType::Size(value_type)).Uint32Value();
Scott Wakelingc34dba72016-10-03 10:14:44 +01006621 Location value_loc = locations->InAt(2);
6622 HInstruction* array_instr = instruction->GetArray();
6623 bool has_intermediate_address = array_instr->IsIntermediateAddress();
Scott Wakelingc34dba72016-10-03 10:14:44 +01006624
6625 switch (value_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006626 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006627 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006628 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006629 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006630 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006631 case DataType::Type::kInt32: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006632 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006633 int32_t const_index = Int32ConstantFrom(index);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006634 uint32_t full_offset =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006635 data_offset + (const_index << DataType::SizeShift(value_type));
Scott Wakelingc34dba72016-10-03 10:14:44 +01006636 StoreOperandType store_type = GetStoreOperandType(value_type);
6637 GetAssembler()->StoreToOffset(store_type, RegisterFrom(value_loc), array, full_offset);
6638 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006639 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006640 vixl32::Register temp = temps.Acquire();
6641
6642 if (has_intermediate_address) {
Artem Serov2bbc9532016-10-21 11:51:50 +01006643 // We do not need to compute the intermediate address from the array: the
6644 // input instruction has done it already. See the comment in
6645 // `TryExtractArrayAccessAddress()`.
6646 if (kIsDebugBuild) {
6647 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
Anton Kirilov644032c2016-12-06 17:51:43 +00006648 DCHECK_EQ(Uint64ConstantFrom(tmp->GetOffset()), data_offset);
Artem Serov2bbc9532016-10-21 11:51:50 +01006649 }
6650 temp = array;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006651 } else {
6652 __ Add(temp, array, data_offset);
6653 }
6654 codegen_->StoreToShiftedRegOffset(value_type, value_loc, temp, RegisterFrom(index));
6655 }
6656 break;
6657 }
6658
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006659 case DataType::Type::kReference: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006660 vixl32::Register value = RegisterFrom(value_loc);
6661 // TryExtractArrayAccessAddress optimization is never applied for non-primitive ArraySet.
6662 // See the comment in instruction_simplifier_shared.cc.
6663 DCHECK(!has_intermediate_address);
6664
6665 if (instruction->InputAt(2)->IsNullConstant()) {
6666 // Just setting null.
6667 if (index.IsConstant()) {
6668 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00006669 (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006670 GetAssembler()->StoreToOffset(kStoreWord, value, array, offset);
6671 } else {
6672 DCHECK(index.IsRegister()) << index;
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006673 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006674 vixl32::Register temp = temps.Acquire();
6675 __ Add(temp, array, data_offset);
6676 codegen_->StoreToShiftedRegOffset(value_type, value_loc, temp, RegisterFrom(index));
6677 }
Alexandre Rames374ddf32016-11-04 10:40:49 +00006678 // TODO(VIXL): Use a scope to ensure we record the pc info immediately after the preceding
6679 // store instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01006680 codegen_->MaybeRecordImplicitNullCheck(instruction);
6681 DCHECK(!needs_write_barrier);
6682 DCHECK(!may_need_runtime_call_for_type_check);
6683 break;
6684 }
6685
6686 DCHECK(needs_write_barrier);
6687 Location temp1_loc = locations->GetTemp(0);
6688 vixl32::Register temp1 = RegisterFrom(temp1_loc);
6689 Location temp2_loc = locations->GetTemp(1);
6690 vixl32::Register temp2 = RegisterFrom(temp2_loc);
6691 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6692 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6693 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6694 vixl32::Label done;
Anton Kirilov6f644202017-02-27 18:29:45 +00006695 vixl32::Label* final_label = codegen_->GetFinalLabel(instruction, &done);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006696 SlowPathCodeARMVIXL* slow_path = nullptr;
6697
6698 if (may_need_runtime_call_for_type_check) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01006699 slow_path = new (codegen_->GetScopedAllocator()) ArraySetSlowPathARMVIXL(instruction);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006700 codegen_->AddSlowPath(slow_path);
6701 if (instruction->GetValueCanBeNull()) {
6702 vixl32::Label non_zero;
xueliang.zhongf51bc622016-11-04 09:23:32 +00006703 __ CompareAndBranchIfNonZero(value, &non_zero);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006704 if (index.IsConstant()) {
6705 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00006706 (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006707 GetAssembler()->StoreToOffset(kStoreWord, value, array, offset);
6708 } else {
6709 DCHECK(index.IsRegister()) << index;
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006710 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006711 vixl32::Register temp = temps.Acquire();
6712 __ Add(temp, array, data_offset);
6713 codegen_->StoreToShiftedRegOffset(value_type, value_loc, temp, RegisterFrom(index));
6714 }
Alexandre Rames374ddf32016-11-04 10:40:49 +00006715 // TODO(VIXL): Use a scope to ensure we record the pc info immediately after the preceding
6716 // store instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01006717 codegen_->MaybeRecordImplicitNullCheck(instruction);
Anton Kirilov6f644202017-02-27 18:29:45 +00006718 __ B(final_label);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006719 __ Bind(&non_zero);
6720 }
6721
6722 // Note that when read barriers are enabled, the type checks
6723 // are performed without read barriers. This is fine, even in
6724 // the case where a class object is in the from-space after
6725 // the flip, as a comparison involving such a type would not
6726 // produce a false positive; it may of course produce a false
6727 // negative, in which case we would take the ArraySet slow
6728 // path.
6729
Alexandre Rames374ddf32016-11-04 10:40:49 +00006730 {
6731 // Ensure we record the pc position immediately after the `ldr` instruction.
Artem Serov0fb37192016-12-06 18:13:40 +00006732 ExactAssemblyScope aas(GetVIXLAssembler(),
6733 vixl32::kMaxInstructionSizeInBytes,
6734 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00006735 // /* HeapReference<Class> */ temp1 = array->klass_
6736 __ ldr(temp1, MemOperand(array, class_offset));
6737 codegen_->MaybeRecordImplicitNullCheck(instruction);
6738 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006739 GetAssembler()->MaybeUnpoisonHeapReference(temp1);
6740
6741 // /* HeapReference<Class> */ temp1 = temp1->component_type_
6742 GetAssembler()->LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
6743 // /* HeapReference<Class> */ temp2 = value->klass_
6744 GetAssembler()->LoadFromOffset(kLoadWord, temp2, value, class_offset);
6745 // If heap poisoning is enabled, no need to unpoison `temp1`
6746 // nor `temp2`, as we are comparing two poisoned references.
6747 __ Cmp(temp1, temp2);
6748
6749 if (instruction->StaticTypeOfArrayIsObjectArray()) {
6750 vixl32::Label do_put;
Artem Serov517d9f62016-12-12 15:51:15 +00006751 __ B(eq, &do_put, /* far_target */ false);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006752 // If heap poisoning is enabled, the `temp1` reference has
6753 // not been unpoisoned yet; unpoison it now.
6754 GetAssembler()->MaybeUnpoisonHeapReference(temp1);
6755
6756 // /* HeapReference<Class> */ temp1 = temp1->super_class_
6757 GetAssembler()->LoadFromOffset(kLoadWord, temp1, temp1, super_offset);
6758 // If heap poisoning is enabled, no need to unpoison
6759 // `temp1`, as we are comparing against null below.
xueliang.zhongf51bc622016-11-04 09:23:32 +00006760 __ CompareAndBranchIfNonZero(temp1, slow_path->GetEntryLabel());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006761 __ Bind(&do_put);
6762 } else {
6763 __ B(ne, slow_path->GetEntryLabel());
6764 }
6765 }
6766
6767 vixl32::Register source = value;
6768 if (kPoisonHeapReferences) {
6769 // Note that in the case where `value` is a null reference,
6770 // we do not enter this block, as a null reference does not
6771 // need poisoning.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006772 DCHECK_EQ(value_type, DataType::Type::kReference);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006773 __ Mov(temp1, value);
6774 GetAssembler()->PoisonHeapReference(temp1);
6775 source = temp1;
6776 }
6777
6778 if (index.IsConstant()) {
6779 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00006780 (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006781 GetAssembler()->StoreToOffset(kStoreWord, source, array, offset);
6782 } else {
6783 DCHECK(index.IsRegister()) << index;
6784
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006785 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006786 vixl32::Register temp = temps.Acquire();
6787 __ Add(temp, array, data_offset);
6788 codegen_->StoreToShiftedRegOffset(value_type,
6789 LocationFrom(source),
6790 temp,
6791 RegisterFrom(index));
6792 }
6793
6794 if (!may_need_runtime_call_for_type_check) {
Alexandre Rames374ddf32016-11-04 10:40:49 +00006795 // TODO(VIXL): Ensure we record the pc position immediately after the preceding store
6796 // instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01006797 codegen_->MaybeRecordImplicitNullCheck(instruction);
6798 }
6799
6800 codegen_->MarkGCCard(temp1, temp2, array, value, instruction->GetValueCanBeNull());
6801
6802 if (done.IsReferenced()) {
6803 __ Bind(&done);
6804 }
6805
6806 if (slow_path != nullptr) {
6807 __ Bind(slow_path->GetExitLabel());
6808 }
6809
6810 break;
6811 }
6812
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006813 case DataType::Type::kInt64: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006814 Location value = locations->InAt(2);
6815 if (index.IsConstant()) {
6816 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00006817 (Int32ConstantFrom(index) << TIMES_8) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006818 GetAssembler()->StoreToOffset(kStoreWordPair, LowRegisterFrom(value), array, offset);
6819 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006820 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006821 vixl32::Register temp = temps.Acquire();
6822 __ Add(temp, array, Operand(RegisterFrom(index), vixl32::LSL, TIMES_8));
6823 GetAssembler()->StoreToOffset(kStoreWordPair, LowRegisterFrom(value), temp, data_offset);
6824 }
6825 break;
6826 }
6827
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006828 case DataType::Type::kFloat32: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006829 Location value = locations->InAt(2);
6830 DCHECK(value.IsFpuRegister());
6831 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006832 size_t offset = (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006833 GetAssembler()->StoreSToOffset(SRegisterFrom(value), array, offset);
6834 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006835 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006836 vixl32::Register temp = temps.Acquire();
6837 __ Add(temp, array, Operand(RegisterFrom(index), vixl32::LSL, TIMES_4));
6838 GetAssembler()->StoreSToOffset(SRegisterFrom(value), temp, data_offset);
6839 }
6840 break;
6841 }
6842
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006843 case DataType::Type::kFloat64: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006844 Location value = locations->InAt(2);
6845 DCHECK(value.IsFpuRegisterPair());
6846 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006847 size_t offset = (Int32ConstantFrom(index) << TIMES_8) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006848 GetAssembler()->StoreDToOffset(DRegisterFrom(value), array, offset);
6849 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006850 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006851 vixl32::Register temp = temps.Acquire();
6852 __ Add(temp, array, Operand(RegisterFrom(index), vixl32::LSL, TIMES_8));
6853 GetAssembler()->StoreDToOffset(DRegisterFrom(value), temp, data_offset);
6854 }
6855 break;
6856 }
6857
Aart Bik66c158e2018-01-31 12:55:04 -08006858 case DataType::Type::kUint32:
6859 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006860 case DataType::Type::kVoid:
Scott Wakelingc34dba72016-10-03 10:14:44 +01006861 LOG(FATAL) << "Unreachable type " << value_type;
6862 UNREACHABLE();
6863 }
6864
6865 // Objects are handled in the switch.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006866 if (value_type != DataType::Type::kReference) {
Alexandre Rames374ddf32016-11-04 10:40:49 +00006867 // TODO(VIXL): Ensure we record the pc position immediately after the preceding store
6868 // instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01006869 codegen_->MaybeRecordImplicitNullCheck(instruction);
6870 }
6871}
6872
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006873void LocationsBuilderARMVIXL::VisitArrayLength(HArrayLength* instruction) {
6874 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006875 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006876 locations->SetInAt(0, Location::RequiresRegister());
6877 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6878}
6879
6880void InstructionCodeGeneratorARMVIXL::VisitArrayLength(HArrayLength* instruction) {
6881 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
6882 vixl32::Register obj = InputRegisterAt(instruction, 0);
6883 vixl32::Register out = OutputRegister(instruction);
Alexandre Rames374ddf32016-11-04 10:40:49 +00006884 {
Artem Serov0fb37192016-12-06 18:13:40 +00006885 ExactAssemblyScope aas(GetVIXLAssembler(),
6886 vixl32::kMaxInstructionSizeInBytes,
6887 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00006888 __ ldr(out, MemOperand(obj, offset));
6889 codegen_->MaybeRecordImplicitNullCheck(instruction);
6890 }
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006891 // Mask out compression flag from String's array length.
6892 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006893 __ Lsr(out, out, 1u);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006894 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006895}
6896
Artem Serov2bbc9532016-10-21 11:51:50 +01006897void LocationsBuilderARMVIXL::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Artem Serov2bbc9532016-10-21 11:51:50 +01006898 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006899 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Artem Serov2bbc9532016-10-21 11:51:50 +01006900
6901 locations->SetInAt(0, Location::RequiresRegister());
6902 locations->SetInAt(1, Location::RegisterOrConstant(instruction->GetOffset()));
6903 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6904}
6905
6906void InstructionCodeGeneratorARMVIXL::VisitIntermediateAddress(HIntermediateAddress* instruction) {
6907 vixl32::Register out = OutputRegister(instruction);
6908 vixl32::Register first = InputRegisterAt(instruction, 0);
6909 Location second = instruction->GetLocations()->InAt(1);
6910
Artem Serov2bbc9532016-10-21 11:51:50 +01006911 if (second.IsRegister()) {
6912 __ Add(out, first, RegisterFrom(second));
6913 } else {
Anton Kirilov644032c2016-12-06 17:51:43 +00006914 __ Add(out, first, Int32ConstantFrom(second));
Artem Serov2bbc9532016-10-21 11:51:50 +01006915 }
6916}
6917
Artem Serove1811ed2017-04-27 16:50:47 +01006918void LocationsBuilderARMVIXL::VisitIntermediateAddressIndex(
6919 HIntermediateAddressIndex* instruction) {
6920 LOG(FATAL) << "Unreachable " << instruction->GetId();
6921}
6922
6923void InstructionCodeGeneratorARMVIXL::VisitIntermediateAddressIndex(
6924 HIntermediateAddressIndex* instruction) {
6925 LOG(FATAL) << "Unreachable " << instruction->GetId();
6926}
6927
Scott Wakelingc34dba72016-10-03 10:14:44 +01006928void LocationsBuilderARMVIXL::VisitBoundsCheck(HBoundsCheck* instruction) {
6929 RegisterSet caller_saves = RegisterSet::Empty();
6930 InvokeRuntimeCallingConventionARMVIXL calling_convention;
6931 caller_saves.Add(LocationFrom(calling_convention.GetRegisterAt(0)));
6932 caller_saves.Add(LocationFrom(calling_convention.GetRegisterAt(1)));
6933 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Artem Serov2dd053d2017-03-08 14:54:06 +00006934
6935 HInstruction* index = instruction->InputAt(0);
6936 HInstruction* length = instruction->InputAt(1);
6937 // If both index and length are constants we can statically check the bounds. But if at least one
6938 // of them is not encodable ArmEncodableConstantOrRegister will create
6939 // Location::RequiresRegister() which is not desired to happen. Instead we create constant
6940 // locations.
6941 bool both_const = index->IsConstant() && length->IsConstant();
6942 locations->SetInAt(0, both_const
6943 ? Location::ConstantLocation(index->AsConstant())
6944 : ArmEncodableConstantOrRegister(index, CMP));
6945 locations->SetInAt(1, both_const
6946 ? Location::ConstantLocation(length->AsConstant())
6947 : ArmEncodableConstantOrRegister(length, CMP));
Scott Wakelingc34dba72016-10-03 10:14:44 +01006948}
6949
6950void InstructionCodeGeneratorARMVIXL::VisitBoundsCheck(HBoundsCheck* instruction) {
Artem Serov2dd053d2017-03-08 14:54:06 +00006951 LocationSummary* locations = instruction->GetLocations();
6952 Location index_loc = locations->InAt(0);
6953 Location length_loc = locations->InAt(1);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006954
Artem Serov2dd053d2017-03-08 14:54:06 +00006955 if (length_loc.IsConstant()) {
6956 int32_t length = Int32ConstantFrom(length_loc);
6957 if (index_loc.IsConstant()) {
6958 // BCE will remove the bounds check if we are guaranteed to pass.
6959 int32_t index = Int32ConstantFrom(index_loc);
6960 if (index < 0 || index >= length) {
6961 SlowPathCodeARMVIXL* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006962 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathARMVIXL(instruction);
Artem Serov2dd053d2017-03-08 14:54:06 +00006963 codegen_->AddSlowPath(slow_path);
6964 __ B(slow_path->GetEntryLabel());
6965 } else {
6966 // Some optimization after BCE may have generated this, and we should not
6967 // generate a bounds check if it is a valid range.
6968 }
6969 return;
6970 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006971
Artem Serov2dd053d2017-03-08 14:54:06 +00006972 SlowPathCodeARMVIXL* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006973 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathARMVIXL(instruction);
Artem Serov2dd053d2017-03-08 14:54:06 +00006974 __ Cmp(RegisterFrom(index_loc), length);
6975 codegen_->AddSlowPath(slow_path);
6976 __ B(hs, slow_path->GetEntryLabel());
6977 } else {
6978 SlowPathCodeARMVIXL* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006979 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathARMVIXL(instruction);
Artem Serov2dd053d2017-03-08 14:54:06 +00006980 __ Cmp(RegisterFrom(length_loc), InputOperandAt(instruction, 0));
6981 codegen_->AddSlowPath(slow_path);
6982 __ B(ls, slow_path->GetEntryLabel());
6983 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006984}
6985
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006986void CodeGeneratorARMVIXL::MarkGCCard(vixl32::Register temp,
6987 vixl32::Register card,
6988 vixl32::Register object,
6989 vixl32::Register value,
6990 bool can_be_null) {
6991 vixl32::Label is_null;
6992 if (can_be_null) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00006993 __ CompareAndBranchIfZero(value, &is_null);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006994 }
6995 GetAssembler()->LoadFromOffset(
6996 kLoadWord, card, tr, Thread::CardTableOffset<kArmPointerSize>().Int32Value());
Scott Wakelingb77051e2016-11-21 19:46:00 +00006997 __ Lsr(temp, object, Operand::From(gc::accounting::CardTable::kCardShift));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006998 __ Strb(card, MemOperand(card, temp));
6999 if (can_be_null) {
7000 __ Bind(&is_null);
7001 }
7002}
7003
Scott Wakelingfe885462016-09-22 10:24:38 +01007004void LocationsBuilderARMVIXL::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
7005 LOG(FATAL) << "Unreachable";
7006}
7007
7008void InstructionCodeGeneratorARMVIXL::VisitParallelMove(HParallelMove* instruction) {
Vladimir Markobea75ff2017-10-11 20:39:54 +01007009 if (instruction->GetNext()->IsSuspendCheck() &&
7010 instruction->GetBlock()->GetLoopInformation() != nullptr) {
7011 HSuspendCheck* suspend_check = instruction->GetNext()->AsSuspendCheck();
7012 // The back edge will generate the suspend check.
7013 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(suspend_check, instruction);
7014 }
7015
Scott Wakelingfe885462016-09-22 10:24:38 +01007016 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
7017}
7018
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007019void LocationsBuilderARMVIXL::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007020 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
7021 instruction, LocationSummary::kCallOnSlowPath);
Artem Serov657022c2016-11-23 14:19:38 +00007022 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007023}
7024
7025void InstructionCodeGeneratorARMVIXL::VisitSuspendCheck(HSuspendCheck* instruction) {
7026 HBasicBlock* block = instruction->GetBlock();
7027 if (block->GetLoopInformation() != nullptr) {
7028 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
7029 // The back edge will generate the suspend check.
7030 return;
7031 }
7032 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
7033 // The goto will generate the suspend check.
7034 return;
7035 }
7036 GenerateSuspendCheck(instruction, nullptr);
Roland Levillain5daa4952017-07-03 17:23:56 +01007037 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 12);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007038}
7039
7040void InstructionCodeGeneratorARMVIXL::GenerateSuspendCheck(HSuspendCheck* instruction,
7041 HBasicBlock* successor) {
7042 SuspendCheckSlowPathARMVIXL* slow_path =
7043 down_cast<SuspendCheckSlowPathARMVIXL*>(instruction->GetSlowPath());
7044 if (slow_path == nullptr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007045 slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01007046 new (codegen_->GetScopedAllocator()) SuspendCheckSlowPathARMVIXL(instruction, successor);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007047 instruction->SetSlowPath(slow_path);
7048 codegen_->AddSlowPath(slow_path);
7049 if (successor != nullptr) {
7050 DCHECK(successor->IsLoopHeader());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007051 }
7052 } else {
7053 DCHECK_EQ(slow_path->GetSuccessor(), successor);
7054 }
7055
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007056 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007057 vixl32::Register temp = temps.Acquire();
7058 GetAssembler()->LoadFromOffset(
7059 kLoadUnsignedHalfword, temp, tr, Thread::ThreadFlagsOffset<kArmPointerSize>().Int32Value());
7060 if (successor == nullptr) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00007061 __ CompareAndBranchIfNonZero(temp, slow_path->GetEntryLabel());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007062 __ Bind(slow_path->GetReturnLabel());
7063 } else {
xueliang.zhongf51bc622016-11-04 09:23:32 +00007064 __ CompareAndBranchIfZero(temp, codegen_->GetLabelOf(successor));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007065 __ B(slow_path->GetEntryLabel());
7066 }
7067}
7068
Scott Wakelingfe885462016-09-22 10:24:38 +01007069ArmVIXLAssembler* ParallelMoveResolverARMVIXL::GetAssembler() const {
7070 return codegen_->GetAssembler();
7071}
7072
7073void ParallelMoveResolverARMVIXL::EmitMove(size_t index) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007074 UseScratchRegisterScope temps(GetAssembler()->GetVIXLAssembler());
Scott Wakelingfe885462016-09-22 10:24:38 +01007075 MoveOperands* move = moves_[index];
7076 Location source = move->GetSource();
7077 Location destination = move->GetDestination();
7078
7079 if (source.IsRegister()) {
7080 if (destination.IsRegister()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007081 __ Mov(RegisterFrom(destination), RegisterFrom(source));
Scott Wakelingfe885462016-09-22 10:24:38 +01007082 } else if (destination.IsFpuRegister()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007083 __ Vmov(SRegisterFrom(destination), RegisterFrom(source));
Scott Wakelingfe885462016-09-22 10:24:38 +01007084 } else {
7085 DCHECK(destination.IsStackSlot());
7086 GetAssembler()->StoreToOffset(kStoreWord,
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007087 RegisterFrom(source),
Scott Wakelingfe885462016-09-22 10:24:38 +01007088 sp,
7089 destination.GetStackIndex());
7090 }
7091 } else if (source.IsStackSlot()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007092 if (destination.IsRegister()) {
7093 GetAssembler()->LoadFromOffset(kLoadWord,
7094 RegisterFrom(destination),
7095 sp,
7096 source.GetStackIndex());
7097 } else if (destination.IsFpuRegister()) {
7098 GetAssembler()->LoadSFromOffset(SRegisterFrom(destination), sp, source.GetStackIndex());
7099 } else {
7100 DCHECK(destination.IsStackSlot());
7101 vixl32::Register temp = temps.Acquire();
7102 GetAssembler()->LoadFromOffset(kLoadWord, temp, sp, source.GetStackIndex());
7103 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
7104 }
Scott Wakelingfe885462016-09-22 10:24:38 +01007105 } else if (source.IsFpuRegister()) {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01007106 if (destination.IsRegister()) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01007107 __ Vmov(RegisterFrom(destination), SRegisterFrom(source));
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01007108 } else if (destination.IsFpuRegister()) {
7109 __ Vmov(SRegisterFrom(destination), SRegisterFrom(source));
7110 } else {
7111 DCHECK(destination.IsStackSlot());
7112 GetAssembler()->StoreSToOffset(SRegisterFrom(source), sp, destination.GetStackIndex());
7113 }
Scott Wakelingfe885462016-09-22 10:24:38 +01007114 } else if (source.IsDoubleStackSlot()) {
Alexandre Rames9c19bd62016-10-24 11:50:32 +01007115 if (destination.IsDoubleStackSlot()) {
7116 vixl32::DRegister temp = temps.AcquireD();
7117 GetAssembler()->LoadDFromOffset(temp, sp, source.GetStackIndex());
7118 GetAssembler()->StoreDToOffset(temp, sp, destination.GetStackIndex());
7119 } else if (destination.IsRegisterPair()) {
7120 DCHECK(ExpectedPairLayout(destination));
7121 GetAssembler()->LoadFromOffset(
7122 kLoadWordPair, LowRegisterFrom(destination), sp, source.GetStackIndex());
7123 } else {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01007124 DCHECK(destination.IsFpuRegisterPair()) << destination;
7125 GetAssembler()->LoadDFromOffset(DRegisterFrom(destination), sp, source.GetStackIndex());
Alexandre Rames9c19bd62016-10-24 11:50:32 +01007126 }
Scott Wakelingfe885462016-09-22 10:24:38 +01007127 } else if (source.IsRegisterPair()) {
7128 if (destination.IsRegisterPair()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007129 __ Mov(LowRegisterFrom(destination), LowRegisterFrom(source));
7130 __ Mov(HighRegisterFrom(destination), HighRegisterFrom(source));
Scott Wakelingfe885462016-09-22 10:24:38 +01007131 } else if (destination.IsFpuRegisterPair()) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01007132 __ Vmov(DRegisterFrom(destination), LowRegisterFrom(source), HighRegisterFrom(source));
Scott Wakelingfe885462016-09-22 10:24:38 +01007133 } else {
7134 DCHECK(destination.IsDoubleStackSlot()) << destination;
7135 DCHECK(ExpectedPairLayout(source));
7136 GetAssembler()->StoreToOffset(kStoreWordPair,
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007137 LowRegisterFrom(source),
Scott Wakelingfe885462016-09-22 10:24:38 +01007138 sp,
7139 destination.GetStackIndex());
7140 }
7141 } else if (source.IsFpuRegisterPair()) {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01007142 if (destination.IsRegisterPair()) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01007143 __ Vmov(LowRegisterFrom(destination), HighRegisterFrom(destination), DRegisterFrom(source));
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01007144 } else if (destination.IsFpuRegisterPair()) {
7145 __ Vmov(DRegisterFrom(destination), DRegisterFrom(source));
7146 } else {
7147 DCHECK(destination.IsDoubleStackSlot()) << destination;
7148 GetAssembler()->StoreDToOffset(DRegisterFrom(source), sp, destination.GetStackIndex());
7149 }
Scott Wakelingfe885462016-09-22 10:24:38 +01007150 } else {
7151 DCHECK(source.IsConstant()) << source;
7152 HConstant* constant = source.GetConstant();
7153 if (constant->IsIntConstant() || constant->IsNullConstant()) {
7154 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
7155 if (destination.IsRegister()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007156 __ Mov(RegisterFrom(destination), value);
Scott Wakelingfe885462016-09-22 10:24:38 +01007157 } else {
7158 DCHECK(destination.IsStackSlot());
Scott Wakelingfe885462016-09-22 10:24:38 +01007159 vixl32::Register temp = temps.Acquire();
7160 __ Mov(temp, value);
7161 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
7162 }
7163 } else if (constant->IsLongConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00007164 int64_t value = Int64ConstantFrom(source);
Scott Wakelingfe885462016-09-22 10:24:38 +01007165 if (destination.IsRegisterPair()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007166 __ Mov(LowRegisterFrom(destination), Low32Bits(value));
7167 __ Mov(HighRegisterFrom(destination), High32Bits(value));
Scott Wakelingfe885462016-09-22 10:24:38 +01007168 } else {
7169 DCHECK(destination.IsDoubleStackSlot()) << destination;
Scott Wakelingfe885462016-09-22 10:24:38 +01007170 vixl32::Register temp = temps.Acquire();
7171 __ Mov(temp, Low32Bits(value));
7172 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
7173 __ Mov(temp, High32Bits(value));
7174 GetAssembler()->StoreToOffset(kStoreWord,
7175 temp,
7176 sp,
7177 destination.GetHighStackIndex(kArmWordSize));
7178 }
7179 } else if (constant->IsDoubleConstant()) {
7180 double value = constant->AsDoubleConstant()->GetValue();
7181 if (destination.IsFpuRegisterPair()) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01007182 __ Vmov(DRegisterFrom(destination), value);
Scott Wakelingfe885462016-09-22 10:24:38 +01007183 } else {
7184 DCHECK(destination.IsDoubleStackSlot()) << destination;
7185 uint64_t int_value = bit_cast<uint64_t, double>(value);
Scott Wakelingfe885462016-09-22 10:24:38 +01007186 vixl32::Register temp = temps.Acquire();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007187 __ Mov(temp, Low32Bits(int_value));
Scott Wakelingfe885462016-09-22 10:24:38 +01007188 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007189 __ Mov(temp, High32Bits(int_value));
Scott Wakelingfe885462016-09-22 10:24:38 +01007190 GetAssembler()->StoreToOffset(kStoreWord,
7191 temp,
7192 sp,
7193 destination.GetHighStackIndex(kArmWordSize));
7194 }
7195 } else {
7196 DCHECK(constant->IsFloatConstant()) << constant->DebugName();
7197 float value = constant->AsFloatConstant()->GetValue();
7198 if (destination.IsFpuRegister()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007199 __ Vmov(SRegisterFrom(destination), value);
Scott Wakelingfe885462016-09-22 10:24:38 +01007200 } else {
7201 DCHECK(destination.IsStackSlot());
Scott Wakelingfe885462016-09-22 10:24:38 +01007202 vixl32::Register temp = temps.Acquire();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007203 __ Mov(temp, bit_cast<int32_t, float>(value));
Scott Wakelingfe885462016-09-22 10:24:38 +01007204 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
7205 }
7206 }
7207 }
7208}
7209
Alexandre Rames9c19bd62016-10-24 11:50:32 +01007210void ParallelMoveResolverARMVIXL::Exchange(vixl32::Register reg, int mem) {
7211 UseScratchRegisterScope temps(GetAssembler()->GetVIXLAssembler());
7212 vixl32::Register temp = temps.Acquire();
7213 __ Mov(temp, reg);
7214 GetAssembler()->LoadFromOffset(kLoadWord, reg, sp, mem);
7215 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, mem);
Scott Wakelingfe885462016-09-22 10:24:38 +01007216}
7217
Alexandre Rames9c19bd62016-10-24 11:50:32 +01007218void ParallelMoveResolverARMVIXL::Exchange(int mem1, int mem2) {
7219 // TODO(VIXL32): Double check the performance of this implementation.
7220 UseScratchRegisterScope temps(GetAssembler()->GetVIXLAssembler());
Nicolas Geoffray13a797b2017-03-15 16:41:31 +00007221 vixl32::Register temp1 = temps.Acquire();
7222 ScratchRegisterScope ensure_scratch(
7223 this, temp1.GetCode(), r0.GetCode(), codegen_->GetNumberOfCoreRegisters());
7224 vixl32::Register temp2(ensure_scratch.GetRegister());
Alexandre Rames9c19bd62016-10-24 11:50:32 +01007225
Nicolas Geoffray13a797b2017-03-15 16:41:31 +00007226 int stack_offset = ensure_scratch.IsSpilled() ? kArmWordSize : 0;
7227 GetAssembler()->LoadFromOffset(kLoadWord, temp1, sp, mem1 + stack_offset);
7228 GetAssembler()->LoadFromOffset(kLoadWord, temp2, sp, mem2 + stack_offset);
7229 GetAssembler()->StoreToOffset(kStoreWord, temp1, sp, mem2 + stack_offset);
7230 GetAssembler()->StoreToOffset(kStoreWord, temp2, sp, mem1 + stack_offset);
Scott Wakelingfe885462016-09-22 10:24:38 +01007231}
7232
Alexandre Rames9c19bd62016-10-24 11:50:32 +01007233void ParallelMoveResolverARMVIXL::EmitSwap(size_t index) {
7234 MoveOperands* move = moves_[index];
7235 Location source = move->GetSource();
7236 Location destination = move->GetDestination();
7237 UseScratchRegisterScope temps(GetAssembler()->GetVIXLAssembler());
7238
7239 if (source.IsRegister() && destination.IsRegister()) {
7240 vixl32::Register temp = temps.Acquire();
7241 DCHECK(!RegisterFrom(source).Is(temp));
7242 DCHECK(!RegisterFrom(destination).Is(temp));
7243 __ Mov(temp, RegisterFrom(destination));
7244 __ Mov(RegisterFrom(destination), RegisterFrom(source));
7245 __ Mov(RegisterFrom(source), temp);
7246 } else if (source.IsRegister() && destination.IsStackSlot()) {
7247 Exchange(RegisterFrom(source), destination.GetStackIndex());
7248 } else if (source.IsStackSlot() && destination.IsRegister()) {
7249 Exchange(RegisterFrom(destination), source.GetStackIndex());
7250 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Anton Kirilovdda43962016-11-21 19:55:20 +00007251 Exchange(source.GetStackIndex(), destination.GetStackIndex());
Alexandre Rames9c19bd62016-10-24 11:50:32 +01007252 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Nicolas Geoffray13a797b2017-03-15 16:41:31 +00007253 vixl32::Register temp = temps.Acquire();
Anton Kirilovdda43962016-11-21 19:55:20 +00007254 __ Vmov(temp, SRegisterFrom(source));
7255 __ Vmov(SRegisterFrom(source), SRegisterFrom(destination));
7256 __ Vmov(SRegisterFrom(destination), temp);
Alexandre Rames9c19bd62016-10-24 11:50:32 +01007257 } else if (source.IsRegisterPair() && destination.IsRegisterPair()) {
7258 vixl32::DRegister temp = temps.AcquireD();
7259 __ Vmov(temp, LowRegisterFrom(source), HighRegisterFrom(source));
7260 __ Mov(LowRegisterFrom(source), LowRegisterFrom(destination));
7261 __ Mov(HighRegisterFrom(source), HighRegisterFrom(destination));
7262 __ Vmov(LowRegisterFrom(destination), HighRegisterFrom(destination), temp);
7263 } else if (source.IsRegisterPair() || destination.IsRegisterPair()) {
7264 vixl32::Register low_reg = LowRegisterFrom(source.IsRegisterPair() ? source : destination);
7265 int mem = source.IsRegisterPair() ? destination.GetStackIndex() : source.GetStackIndex();
7266 DCHECK(ExpectedPairLayout(source.IsRegisterPair() ? source : destination));
7267 vixl32::DRegister temp = temps.AcquireD();
7268 __ Vmov(temp, low_reg, vixl32::Register(low_reg.GetCode() + 1));
7269 GetAssembler()->LoadFromOffset(kLoadWordPair, low_reg, sp, mem);
7270 GetAssembler()->StoreDToOffset(temp, sp, mem);
7271 } else if (source.IsFpuRegisterPair() && destination.IsFpuRegisterPair()) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007272 vixl32::DRegister first = DRegisterFrom(source);
7273 vixl32::DRegister second = DRegisterFrom(destination);
7274 vixl32::DRegister temp = temps.AcquireD();
7275 __ Vmov(temp, first);
7276 __ Vmov(first, second);
7277 __ Vmov(second, temp);
Alexandre Rames9c19bd62016-10-24 11:50:32 +01007278 } else if (source.IsFpuRegisterPair() || destination.IsFpuRegisterPair()) {
Anton Kirilovdda43962016-11-21 19:55:20 +00007279 vixl32::DRegister reg = source.IsFpuRegisterPair()
7280 ? DRegisterFrom(source)
7281 : DRegisterFrom(destination);
7282 int mem = source.IsFpuRegisterPair()
7283 ? destination.GetStackIndex()
7284 : source.GetStackIndex();
7285 vixl32::DRegister temp = temps.AcquireD();
7286 __ Vmov(temp, reg);
7287 GetAssembler()->LoadDFromOffset(reg, sp, mem);
7288 GetAssembler()->StoreDToOffset(temp, sp, mem);
Alexandre Rames9c19bd62016-10-24 11:50:32 +01007289 } else if (source.IsFpuRegister() || destination.IsFpuRegister()) {
Anton Kirilovdda43962016-11-21 19:55:20 +00007290 vixl32::SRegister reg = source.IsFpuRegister()
7291 ? SRegisterFrom(source)
7292 : SRegisterFrom(destination);
7293 int mem = source.IsFpuRegister()
7294 ? destination.GetStackIndex()
7295 : source.GetStackIndex();
7296 vixl32::Register temp = temps.Acquire();
7297 __ Vmov(temp, reg);
7298 GetAssembler()->LoadSFromOffset(reg, sp, mem);
7299 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, mem);
Alexandre Rames9c19bd62016-10-24 11:50:32 +01007300 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
7301 vixl32::DRegister temp1 = temps.AcquireD();
7302 vixl32::DRegister temp2 = temps.AcquireD();
7303 __ Vldr(temp1, MemOperand(sp, source.GetStackIndex()));
7304 __ Vldr(temp2, MemOperand(sp, destination.GetStackIndex()));
7305 __ Vstr(temp1, MemOperand(sp, destination.GetStackIndex()));
7306 __ Vstr(temp2, MemOperand(sp, source.GetStackIndex()));
7307 } else {
7308 LOG(FATAL) << "Unimplemented" << source << " <-> " << destination;
7309 }
Scott Wakelingfe885462016-09-22 10:24:38 +01007310}
7311
Nicolas Geoffray13a797b2017-03-15 16:41:31 +00007312void ParallelMoveResolverARMVIXL::SpillScratch(int reg) {
7313 __ Push(vixl32::Register(reg));
Scott Wakelingfe885462016-09-22 10:24:38 +01007314}
7315
Nicolas Geoffray13a797b2017-03-15 16:41:31 +00007316void ParallelMoveResolverARMVIXL::RestoreScratch(int reg) {
7317 __ Pop(vixl32::Register(reg));
Scott Wakelingfe885462016-09-22 10:24:38 +01007318}
7319
Artem Serov02d37832016-10-25 15:25:33 +01007320HLoadClass::LoadKind CodeGeneratorARMVIXL::GetSupportedLoadClassKind(
Artem Serovd4cc5b22016-11-04 11:19:09 +00007321 HLoadClass::LoadKind desired_class_load_kind) {
7322 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00007323 case HLoadClass::LoadKind::kInvalid:
7324 LOG(FATAL) << "UNREACHABLE";
7325 UNREACHABLE();
Artem Serovd4cc5b22016-11-04 11:19:09 +00007326 case HLoadClass::LoadKind::kReferrersClass:
7327 break;
Artem Serovd4cc5b22016-11-04 11:19:09 +00007328 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00007329 case HLoadClass::LoadKind::kBootImageRelRo:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007330 case HLoadClass::LoadKind::kBssEntry:
7331 DCHECK(!Runtime::Current()->UseJitCompilation());
7332 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007333 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007334 DCHECK(Runtime::Current()->UseJitCompilation());
Artem Serovc5fcb442016-12-02 19:19:58 +00007335 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01007336 case HLoadClass::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007337 case HLoadClass::LoadKind::kRuntimeCall:
Artem Serovd4cc5b22016-11-04 11:19:09 +00007338 break;
7339 }
7340 return desired_class_load_kind;
Artem Serov02d37832016-10-25 15:25:33 +01007341}
7342
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007343void LocationsBuilderARMVIXL::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00007344 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007345 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007346 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00007347 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007348 cls,
7349 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00007350 LocationFrom(r0));
Vladimir Markoea4c1262017-02-06 19:59:33 +00007351 DCHECK(calling_convention.GetRegisterAt(0).Is(r0));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007352 return;
7353 }
Vladimir Marko41559982017-01-06 14:04:23 +00007354 DCHECK(!cls->NeedsAccessCheck());
Scott Wakelingfe885462016-09-22 10:24:38 +01007355
Artem Serovd4cc5b22016-11-04 11:19:09 +00007356 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
7357 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007358 ? LocationSummary::kCallOnSlowPath
7359 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01007360 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(cls, call_kind);
Artem Serovd4cc5b22016-11-04 11:19:09 +00007361 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007362 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Artem Serovd4cc5b22016-11-04 11:19:09 +00007363 }
7364
Vladimir Marko41559982017-01-06 14:04:23 +00007365 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007366 locations->SetInAt(0, Location::RequiresRegister());
7367 }
7368 locations->SetOut(Location::RequiresRegister());
Vladimir Markoea4c1262017-02-06 19:59:33 +00007369 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
7370 if (!kUseReadBarrier || kUseBakerReadBarrier) {
7371 // Rely on the type resolution or initialization and marking to save everything we need.
Vladimir Markoea4c1262017-02-06 19:59:33 +00007372 RegisterSet caller_saves = RegisterSet::Empty();
7373 InvokeRuntimeCallingConventionARMVIXL calling_convention;
7374 caller_saves.Add(LocationFrom(calling_convention.GetRegisterAt(0)));
7375 // TODO: Add GetReturnLocation() to the calling convention so that we can DCHECK()
7376 // that the the kPrimNot result register is the same as the first argument register.
7377 locations->SetCustomSlowPathCallerSaves(caller_saves);
7378 } else {
7379 // For non-Baker read barrier we have a temp-clobbering call.
7380 }
7381 }
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01007382 if (kUseBakerReadBarrier && kBakerReadBarrierLinkTimeThunksEnableForGcRoots) {
7383 if (load_kind == HLoadClass::LoadKind::kBssEntry ||
7384 (load_kind == HLoadClass::LoadKind::kReferrersClass &&
7385 !Runtime::Current()->UseJitCompilation())) {
7386 locations->AddTemp(Location::RegisterLocation(kBakerCcEntrypointRegister.GetCode()));
7387 }
7388 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007389}
7390
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007391// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
7392// move.
7393void InstructionCodeGeneratorARMVIXL::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00007394 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007395 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00007396 codegen_->GenerateLoadClassRuntimeCall(cls);
Roland Levillain5daa4952017-07-03 17:23:56 +01007397 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 13);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007398 return;
7399 }
Vladimir Marko41559982017-01-06 14:04:23 +00007400 DCHECK(!cls->NeedsAccessCheck());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007401
Vladimir Marko41559982017-01-06 14:04:23 +00007402 LocationSummary* locations = cls->GetLocations();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007403 Location out_loc = locations->Out();
7404 vixl32::Register out = OutputRegister(cls);
7405
Artem Serovd4cc5b22016-11-04 11:19:09 +00007406 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
7407 ? kWithoutReadBarrier
7408 : kCompilerReadBarrierOption;
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007409 bool generate_null_check = false;
Vladimir Marko41559982017-01-06 14:04:23 +00007410 switch (load_kind) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007411 case HLoadClass::LoadKind::kReferrersClass: {
7412 DCHECK(!cls->CanCallRuntime());
7413 DCHECK(!cls->MustGenerateClinitCheck());
7414 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
7415 vixl32::Register current_method = InputRegisterAt(cls, 0);
7416 GenerateGcRootFieldLoad(cls,
7417 out_loc,
7418 current_method,
Roland Levillain00468f32016-10-27 18:02:48 +01007419 ArtMethod::DeclaringClassOffset().Int32Value(),
Artem Serovd4cc5b22016-11-04 11:19:09 +00007420 read_barrier_option);
7421 break;
7422 }
Artem Serovd4cc5b22016-11-04 11:19:09 +00007423 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007424 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Artem Serovd4cc5b22016-11-04 11:19:09 +00007425 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
7426 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00007427 codegen_->NewBootImageTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
Artem Serovd4cc5b22016-11-04 11:19:09 +00007428 codegen_->EmitMovwMovtPlaceholder(labels, out);
7429 break;
7430 }
7431 case HLoadClass::LoadKind::kBootImageAddress: {
Artem Serovc5fcb442016-12-02 19:19:58 +00007432 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007433 uint32_t address = dchecked_integral_cast<uint32_t>(
7434 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
7435 DCHECK_NE(address, 0u);
Artem Serovc5fcb442016-12-02 19:19:58 +00007436 __ Ldr(out, codegen_->DeduplicateBootImageAddressLiteral(address));
Artem Serovd4cc5b22016-11-04 11:19:09 +00007437 break;
7438 }
Vladimir Markoe47f60c2018-02-21 13:43:28 +00007439 case HLoadClass::LoadKind::kBootImageRelRo: {
Vladimir Marko94ec2db2017-09-06 17:21:03 +01007440 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
7441 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
Vladimir Markoe47f60c2018-02-21 13:43:28 +00007442 codegen_->NewBootImageRelRoPatch(codegen_->GetBootImageOffset(cls));
Vladimir Marko94ec2db2017-09-06 17:21:03 +01007443 codegen_->EmitMovwMovtPlaceholder(labels, out);
7444 __ Ldr(out, MemOperand(out, /* offset */ 0));
Vladimir Marko94ec2db2017-09-06 17:21:03 +01007445 break;
7446 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007447 case HLoadClass::LoadKind::kBssEntry: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007448 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
Vladimir Marko1998cd02017-01-13 13:02:58 +00007449 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex());
Vladimir Markof3c52b42017-11-17 17:32:12 +00007450 codegen_->EmitMovwMovtPlaceholder(labels, out);
7451 GenerateGcRootFieldLoad(cls, out_loc, out, /* offset */ 0, read_barrier_option);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007452 generate_null_check = true;
7453 break;
7454 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007455 case HLoadClass::LoadKind::kJitTableAddress: {
Artem Serovc5fcb442016-12-02 19:19:58 +00007456 __ Ldr(out, codegen_->DeduplicateJitClassLiteral(cls->GetDexFile(),
7457 cls->GetTypeIndex(),
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007458 cls->GetClass()));
Artem Serovc5fcb442016-12-02 19:19:58 +00007459 // /* GcRoot<mirror::Class> */ out = *out
Vladimir Markoea4c1262017-02-06 19:59:33 +00007460 GenerateGcRootFieldLoad(cls, out_loc, out, /* offset */ 0, read_barrier_option);
Artem Serovd4cc5b22016-11-04 11:19:09 +00007461 break;
7462 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007463 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00007464 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00007465 LOG(FATAL) << "UNREACHABLE";
7466 UNREACHABLE();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007467 }
7468
7469 if (generate_null_check || cls->MustGenerateClinitCheck()) {
7470 DCHECK(cls->CanCallRuntime());
Vladimir Marko174b2e22017-10-12 13:34:49 +01007471 LoadClassSlowPathARMVIXL* slow_path =
7472 new (codegen_->GetScopedAllocator()) LoadClassSlowPathARMVIXL(
7473 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007474 codegen_->AddSlowPath(slow_path);
7475 if (generate_null_check) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00007476 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007477 }
7478 if (cls->MustGenerateClinitCheck()) {
7479 GenerateClassInitializationCheck(slow_path, out);
7480 } else {
7481 __ Bind(slow_path->GetExitLabel());
7482 }
Roland Levillain5daa4952017-07-03 17:23:56 +01007483 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 14);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007484 }
7485}
7486
Artem Serov02d37832016-10-25 15:25:33 +01007487void LocationsBuilderARMVIXL::VisitClinitCheck(HClinitCheck* check) {
7488 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007489 new (GetGraph()->GetAllocator()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
Artem Serov02d37832016-10-25 15:25:33 +01007490 locations->SetInAt(0, Location::RequiresRegister());
7491 if (check->HasUses()) {
7492 locations->SetOut(Location::SameAsFirstInput());
7493 }
7494}
7495
7496void InstructionCodeGeneratorARMVIXL::VisitClinitCheck(HClinitCheck* check) {
7497 // We assume the class is not null.
7498 LoadClassSlowPathARMVIXL* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01007499 new (codegen_->GetScopedAllocator()) LoadClassSlowPathARMVIXL(check->GetLoadClass(),
7500 check,
7501 check->GetDexPc(),
7502 /* do_clinit */ true);
Artem Serov02d37832016-10-25 15:25:33 +01007503 codegen_->AddSlowPath(slow_path);
7504 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
7505}
7506
7507void InstructionCodeGeneratorARMVIXL::GenerateClassInitializationCheck(
7508 LoadClassSlowPathARMVIXL* slow_path, vixl32::Register class_reg) {
7509 UseScratchRegisterScope temps(GetVIXLAssembler());
7510 vixl32::Register temp = temps.Acquire();
Vladimir Markodc682aa2018-01-04 18:42:57 +00007511 constexpr size_t status_lsb_position = SubtypeCheckBits::BitStructSizeOf();
7512 const size_t status_byte_offset =
7513 mirror::Class::StatusOffset().SizeValue() + (status_lsb_position / kBitsPerByte);
7514 constexpr uint32_t shifted_initialized_value =
7515 enum_cast<uint32_t>(ClassStatus::kInitialized) << (status_lsb_position % kBitsPerByte);
7516
7517 GetAssembler()->LoadFromOffset(kLoadUnsignedByte, temp, class_reg, status_byte_offset);
7518 __ Cmp(temp, shifted_initialized_value);
Vladimir Marko2c64a832018-01-04 11:31:56 +00007519 __ B(lo, slow_path->GetEntryLabel());
Artem Serov02d37832016-10-25 15:25:33 +01007520 // Even if the initialized flag is set, we may be in a situation where caches are not synced
7521 // properly. Therefore, we do a memory fence.
7522 __ Dmb(ISH);
7523 __ Bind(slow_path->GetExitLabel());
7524}
7525
Vladimir Marko175e7862018-03-27 09:03:13 +00007526void InstructionCodeGeneratorARMVIXL::GenerateBitstringTypeCheckCompare(
7527 HTypeCheckInstruction* check,
7528 vixl32::Register temp,
7529 vixl32::FlagsUpdate flags_update) {
7530 uint32_t path_to_root = check->GetBitstringPathToRoot();
7531 uint32_t mask = check->GetBitstringMask();
7532 DCHECK(IsPowerOfTwo(mask + 1));
7533 size_t mask_bits = WhichPowerOf2(mask + 1);
7534
7535 // Note that HInstanceOf shall check for zero value in `temp` but HCheckCast needs
7536 // the Z flag for BNE. This is indicated by the `flags_update` parameter.
7537 if (mask_bits == 16u) {
7538 // Load only the bitstring part of the status word.
7539 __ Ldrh(temp, MemOperand(temp, mirror::Class::StatusOffset().Int32Value()));
7540 // Check if the bitstring bits are equal to `path_to_root`.
7541 if (flags_update == SetFlags) {
7542 __ Cmp(temp, path_to_root);
7543 } else {
7544 __ Sub(temp, temp, path_to_root);
7545 }
7546 } else {
7547 // /* uint32_t */ temp = temp->status_
7548 __ Ldr(temp, MemOperand(temp, mirror::Class::StatusOffset().Int32Value()));
7549 if (GetAssembler()->ShifterOperandCanHold(SUB, path_to_root)) {
7550 // Compare the bitstring bits using SUB.
7551 __ Sub(temp, temp, path_to_root);
7552 // Shift out bits that do not contribute to the comparison.
7553 __ Lsl(flags_update, temp, temp, dchecked_integral_cast<uint32_t>(32u - mask_bits));
7554 } else if (IsUint<16>(path_to_root)) {
7555 if (temp.IsLow()) {
7556 // Note: Optimized for size but contains one more dependent instruction than necessary.
7557 // MOVW+SUB(register) would be 8 bytes unless we find a low-reg temporary but the
7558 // macro assembler would use the high reg IP for the constant by default.
7559 // Compare the bitstring bits using SUB.
7560 __ Sub(temp, temp, path_to_root & 0x00ffu); // 16-bit SUB (immediate) T2
7561 __ Sub(temp, temp, path_to_root & 0xff00u); // 32-bit SUB (immediate) T3
7562 // Shift out bits that do not contribute to the comparison.
7563 __ Lsl(flags_update, temp, temp, dchecked_integral_cast<uint32_t>(32u - mask_bits));
7564 } else {
7565 // Extract the bitstring bits.
7566 __ Ubfx(temp, temp, 0, mask_bits);
7567 // Check if the bitstring bits are equal to `path_to_root`.
7568 if (flags_update == SetFlags) {
7569 __ Cmp(temp, path_to_root);
7570 } else {
7571 __ Sub(temp, temp, path_to_root);
7572 }
7573 }
7574 } else {
7575 // Shift out bits that do not contribute to the comparison.
7576 __ Lsl(temp, temp, dchecked_integral_cast<uint32_t>(32u - mask_bits));
7577 // Check if the shifted bitstring bits are equal to `path_to_root << (32u - mask_bits)`.
7578 if (flags_update == SetFlags) {
7579 __ Cmp(temp, path_to_root << (32u - mask_bits));
7580 } else {
7581 __ Sub(temp, temp, path_to_root << (32u - mask_bits));
7582 }
7583 }
7584 }
7585}
7586
Artem Serov02d37832016-10-25 15:25:33 +01007587HLoadString::LoadKind CodeGeneratorARMVIXL::GetSupportedLoadStringKind(
Artem Serovd4cc5b22016-11-04 11:19:09 +00007588 HLoadString::LoadKind desired_string_load_kind) {
7589 switch (desired_string_load_kind) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00007590 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00007591 case HLoadString::LoadKind::kBootImageRelRo:
Artem Serovd4cc5b22016-11-04 11:19:09 +00007592 case HLoadString::LoadKind::kBssEntry:
7593 DCHECK(!Runtime::Current()->UseJitCompilation());
7594 break;
7595 case HLoadString::LoadKind::kJitTableAddress:
7596 DCHECK(Runtime::Current()->UseJitCompilation());
Artem Serovc5fcb442016-12-02 19:19:58 +00007597 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01007598 case HLoadString::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007599 case HLoadString::LoadKind::kRuntimeCall:
Artem Serovd4cc5b22016-11-04 11:19:09 +00007600 break;
7601 }
7602 return desired_string_load_kind;
Artem Serov02d37832016-10-25 15:25:33 +01007603}
7604
7605void LocationsBuilderARMVIXL::VisitLoadString(HLoadString* load) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00007606 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Vladimir Markoca6fff82017-10-03 14:49:14 +01007607 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(load, call_kind);
Artem Serov02d37832016-10-25 15:25:33 +01007608 HLoadString::LoadKind load_kind = load->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007609 if (load_kind == HLoadString::LoadKind::kRuntimeCall) {
Artem Serov02d37832016-10-25 15:25:33 +01007610 locations->SetOut(LocationFrom(r0));
7611 } else {
7612 locations->SetOut(Location::RequiresRegister());
Artem Serovd4cc5b22016-11-04 11:19:09 +00007613 if (load_kind == HLoadString::LoadKind::kBssEntry) {
7614 if (!kUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00007615 // Rely on the pResolveString and marking to save everything we need, including temps.
Artem Serovd4cc5b22016-11-04 11:19:09 +00007616 RegisterSet caller_saves = RegisterSet::Empty();
7617 InvokeRuntimeCallingConventionARMVIXL calling_convention;
7618 caller_saves.Add(LocationFrom(calling_convention.GetRegisterAt(0)));
7619 // TODO: Add GetReturnLocation() to the calling convention so that we can DCHECK()
7620 // that the the kPrimNot result register is the same as the first argument register.
7621 locations->SetCustomSlowPathCallerSaves(caller_saves);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01007622 if (kUseBakerReadBarrier && kBakerReadBarrierLinkTimeThunksEnableForGcRoots) {
7623 locations->AddTemp(Location::RegisterLocation(kBakerCcEntrypointRegister.GetCode()));
7624 }
Artem Serovd4cc5b22016-11-04 11:19:09 +00007625 } else {
7626 // For non-Baker read barrier we have a temp-clobbering call.
7627 }
7628 }
Artem Serov02d37832016-10-25 15:25:33 +01007629 }
7630}
7631
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007632// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
7633// move.
7634void InstructionCodeGeneratorARMVIXL::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Artem Serovd4cc5b22016-11-04 11:19:09 +00007635 LocationSummary* locations = load->GetLocations();
7636 Location out_loc = locations->Out();
7637 vixl32::Register out = OutputRegister(load);
7638 HLoadString::LoadKind load_kind = load->GetLoadKind();
7639
7640 switch (load_kind) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00007641 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
7642 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
7643 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00007644 codegen_->NewBootImageStringPatch(load->GetDexFile(), load->GetStringIndex());
Artem Serovd4cc5b22016-11-04 11:19:09 +00007645 codegen_->EmitMovwMovtPlaceholder(labels, out);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007646 return;
Artem Serovd4cc5b22016-11-04 11:19:09 +00007647 }
7648 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007649 uint32_t address = dchecked_integral_cast<uint32_t>(
7650 reinterpret_cast<uintptr_t>(load->GetString().Get()));
7651 DCHECK_NE(address, 0u);
Artem Serovc5fcb442016-12-02 19:19:58 +00007652 __ Ldr(out, codegen_->DeduplicateBootImageAddressLiteral(address));
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007653 return;
7654 }
Vladimir Markoe47f60c2018-02-21 13:43:28 +00007655 case HLoadString::LoadKind::kBootImageRelRo: {
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007656 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
7657 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
Vladimir Markoe47f60c2018-02-21 13:43:28 +00007658 codegen_->NewBootImageRelRoPatch(codegen_->GetBootImageOffset(load));
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007659 codegen_->EmitMovwMovtPlaceholder(labels, out);
7660 __ Ldr(out, MemOperand(out, /* offset */ 0));
7661 return;
Artem Serovd4cc5b22016-11-04 11:19:09 +00007662 }
7663 case HLoadString::LoadKind::kBssEntry: {
7664 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Artem Serovd4cc5b22016-11-04 11:19:09 +00007665 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007666 codegen_->NewStringBssEntryPatch(load->GetDexFile(), load->GetStringIndex());
Vladimir Markof3c52b42017-11-17 17:32:12 +00007667 codegen_->EmitMovwMovtPlaceholder(labels, out);
7668 GenerateGcRootFieldLoad(load, out_loc, out, /* offset */ 0, kCompilerReadBarrierOption);
Artem Serovd4cc5b22016-11-04 11:19:09 +00007669 LoadStringSlowPathARMVIXL* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01007670 new (codegen_->GetScopedAllocator()) LoadStringSlowPathARMVIXL(load);
Artem Serovd4cc5b22016-11-04 11:19:09 +00007671 codegen_->AddSlowPath(slow_path);
7672 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
7673 __ Bind(slow_path->GetExitLabel());
Roland Levillain5daa4952017-07-03 17:23:56 +01007674 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 15);
Artem Serovd4cc5b22016-11-04 11:19:09 +00007675 return;
7676 }
7677 case HLoadString::LoadKind::kJitTableAddress: {
Artem Serovc5fcb442016-12-02 19:19:58 +00007678 __ Ldr(out, codegen_->DeduplicateJitStringLiteral(load->GetDexFile(),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007679 load->GetStringIndex(),
7680 load->GetString()));
Artem Serovc5fcb442016-12-02 19:19:58 +00007681 // /* GcRoot<mirror::String> */ out = *out
7682 GenerateGcRootFieldLoad(load, out_loc, out, /* offset */ 0, kCompilerReadBarrierOption);
7683 return;
Artem Serovd4cc5b22016-11-04 11:19:09 +00007684 }
7685 default:
7686 break;
7687 }
Artem Serov02d37832016-10-25 15:25:33 +01007688
7689 // TODO: Re-add the compiler code to do string dex cache lookup again.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007690 DCHECK_EQ(load->GetLoadKind(), HLoadString::LoadKind::kRuntimeCall);
Artem Serov02d37832016-10-25 15:25:33 +01007691 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Andreas Gampe8a0128a2016-11-28 07:38:35 -08007692 __ Mov(calling_convention.GetRegisterAt(0), load->GetStringIndex().index_);
Artem Serov02d37832016-10-25 15:25:33 +01007693 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
7694 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Roland Levillain5daa4952017-07-03 17:23:56 +01007695 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 16);
Artem Serov02d37832016-10-25 15:25:33 +01007696}
7697
7698static int32_t GetExceptionTlsOffset() {
7699 return Thread::ExceptionOffset<kArmPointerSize>().Int32Value();
7700}
7701
7702void LocationsBuilderARMVIXL::VisitLoadException(HLoadException* load) {
7703 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007704 new (GetGraph()->GetAllocator()) LocationSummary(load, LocationSummary::kNoCall);
Artem Serov02d37832016-10-25 15:25:33 +01007705 locations->SetOut(Location::RequiresRegister());
7706}
7707
7708void InstructionCodeGeneratorARMVIXL::VisitLoadException(HLoadException* load) {
7709 vixl32::Register out = OutputRegister(load);
7710 GetAssembler()->LoadFromOffset(kLoadWord, out, tr, GetExceptionTlsOffset());
7711}
7712
7713
7714void LocationsBuilderARMVIXL::VisitClearException(HClearException* clear) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007715 new (GetGraph()->GetAllocator()) LocationSummary(clear, LocationSummary::kNoCall);
Artem Serov02d37832016-10-25 15:25:33 +01007716}
7717
7718void InstructionCodeGeneratorARMVIXL::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
7719 UseScratchRegisterScope temps(GetVIXLAssembler());
7720 vixl32::Register temp = temps.Acquire();
7721 __ Mov(temp, 0);
7722 GetAssembler()->StoreToOffset(kStoreWord, temp, tr, GetExceptionTlsOffset());
7723}
7724
7725void LocationsBuilderARMVIXL::VisitThrow(HThrow* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007726 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
7727 instruction, LocationSummary::kCallOnMainOnly);
Artem Serov02d37832016-10-25 15:25:33 +01007728 InvokeRuntimeCallingConventionARMVIXL calling_convention;
7729 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
7730}
7731
7732void InstructionCodeGeneratorARMVIXL::VisitThrow(HThrow* instruction) {
7733 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
7734 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
7735}
7736
Artem Serov657022c2016-11-23 14:19:38 +00007737// Temp is used for read barrier.
7738static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
7739 if (kEmitCompilerReadBarrier &&
7740 (kUseBakerReadBarrier ||
7741 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
7742 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
7743 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
7744 return 1;
7745 }
7746 return 0;
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007747}
7748
Artem Serov657022c2016-11-23 14:19:38 +00007749// Interface case has 3 temps, one for holding the number of interfaces, one for the current
7750// interface pointer, one for loading the current interface.
7751// The other checks have one temp for loading the object's class.
7752static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
7753 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
7754 return 3;
7755 }
7756 return 1 + NumberOfInstanceOfTemps(type_check_kind);
7757}
Artem Serovcfbe9132016-10-14 15:58:56 +01007758
7759void LocationsBuilderARMVIXL::VisitInstanceOf(HInstanceOf* instruction) {
7760 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
7761 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
7762 bool baker_read_barrier_slow_path = false;
7763 switch (type_check_kind) {
7764 case TypeCheckKind::kExactCheck:
7765 case TypeCheckKind::kAbstractClassCheck:
7766 case TypeCheckKind::kClassHierarchyCheck:
Vladimir Marko87584542017-12-12 17:47:52 +00007767 case TypeCheckKind::kArrayObjectCheck: {
7768 bool needs_read_barrier = CodeGenerator::InstanceOfNeedsReadBarrier(instruction);
7769 call_kind = needs_read_barrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
7770 baker_read_barrier_slow_path = kUseBakerReadBarrier && needs_read_barrier;
Artem Serovcfbe9132016-10-14 15:58:56 +01007771 break;
Vladimir Marko87584542017-12-12 17:47:52 +00007772 }
Artem Serovcfbe9132016-10-14 15:58:56 +01007773 case TypeCheckKind::kArrayCheck:
7774 case TypeCheckKind::kUnresolvedCheck:
7775 case TypeCheckKind::kInterfaceCheck:
7776 call_kind = LocationSummary::kCallOnSlowPath;
7777 break;
Vladimir Marko175e7862018-03-27 09:03:13 +00007778 case TypeCheckKind::kBitstringCheck:
7779 break;
Artem Serovcfbe9132016-10-14 15:58:56 +01007780 }
7781
Vladimir Markoca6fff82017-10-03 14:49:14 +01007782 LocationSummary* locations =
7783 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Artem Serovcfbe9132016-10-14 15:58:56 +01007784 if (baker_read_barrier_slow_path) {
7785 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
7786 }
7787 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko175e7862018-03-27 09:03:13 +00007788 if (type_check_kind == TypeCheckKind::kBitstringCheck) {
7789 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
7790 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
7791 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
7792 } else {
7793 locations->SetInAt(1, Location::RequiresRegister());
7794 }
Artem Serovcfbe9132016-10-14 15:58:56 +01007795 // The "out" register is used as a temporary, so it overlaps with the inputs.
7796 // Note that TypeCheckSlowPathARM uses this register too.
7797 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Artem Serov657022c2016-11-23 14:19:38 +00007798 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01007799 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
7800 codegen_->MaybeAddBakerCcEntrypointTempForFields(locations);
7801 }
Artem Serovcfbe9132016-10-14 15:58:56 +01007802}
7803
7804void InstructionCodeGeneratorARMVIXL::VisitInstanceOf(HInstanceOf* instruction) {
7805 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
7806 LocationSummary* locations = instruction->GetLocations();
7807 Location obj_loc = locations->InAt(0);
7808 vixl32::Register obj = InputRegisterAt(instruction, 0);
Vladimir Marko175e7862018-03-27 09:03:13 +00007809 vixl32::Register cls = (type_check_kind == TypeCheckKind::kBitstringCheck)
7810 ? vixl32::Register()
7811 : InputRegisterAt(instruction, 1);
Artem Serovcfbe9132016-10-14 15:58:56 +01007812 Location out_loc = locations->Out();
7813 vixl32::Register out = OutputRegister(instruction);
Artem Serov657022c2016-11-23 14:19:38 +00007814 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
7815 DCHECK_LE(num_temps, 1u);
7816 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Artem Serovcfbe9132016-10-14 15:58:56 +01007817 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
7818 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
7819 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
7820 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007821 vixl32::Label done;
7822 vixl32::Label* const final_label = codegen_->GetFinalLabel(instruction, &done);
Artem Serovcfbe9132016-10-14 15:58:56 +01007823 SlowPathCodeARMVIXL* slow_path = nullptr;
7824
7825 // Return 0 if `obj` is null.
7826 // avoid null check if we know obj is not null.
7827 if (instruction->MustDoNullCheck()) {
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007828 DCHECK(!out.Is(obj));
7829 __ Mov(out, 0);
7830 __ CompareAndBranchIfZero(obj, final_label, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007831 }
7832
Artem Serovcfbe9132016-10-14 15:58:56 +01007833 switch (type_check_kind) {
7834 case TypeCheckKind::kExactCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00007835 ReadBarrierOption read_barrier_option =
7836 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier6beced42016-11-15 15:51:31 -08007837 // /* HeapReference<Class> */ out = obj->klass_
7838 GenerateReferenceLoadTwoRegisters(instruction,
7839 out_loc,
7840 obj_loc,
7841 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00007842 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007843 read_barrier_option);
Artem Serovcfbe9132016-10-14 15:58:56 +01007844 // Classes must be equal for the instanceof to succeed.
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007845 __ Cmp(out, cls);
7846 // We speculatively set the result to false without changing the condition
7847 // flags, which allows us to avoid some branching later.
7848 __ Mov(LeaveFlags, out, 0);
7849
7850 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
7851 // we check that the output is in a low register, so that a 16-bit MOV
7852 // encoding can be used.
7853 if (out.IsLow()) {
7854 // We use the scope because of the IT block that follows.
7855 ExactAssemblyScope guard(GetVIXLAssembler(),
7856 2 * vixl32::k16BitT32InstructionSizeInBytes,
7857 CodeBufferCheckScope::kExactSize);
7858
7859 __ it(eq);
7860 __ mov(eq, out, 1);
7861 } else {
7862 __ B(ne, final_label, /* far_target */ false);
7863 __ Mov(out, 1);
7864 }
7865
Artem Serovcfbe9132016-10-14 15:58:56 +01007866 break;
7867 }
7868
7869 case TypeCheckKind::kAbstractClassCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00007870 ReadBarrierOption read_barrier_option =
7871 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier6beced42016-11-15 15:51:31 -08007872 // /* HeapReference<Class> */ out = obj->klass_
7873 GenerateReferenceLoadTwoRegisters(instruction,
7874 out_loc,
7875 obj_loc,
7876 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00007877 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007878 read_barrier_option);
Artem Serovcfbe9132016-10-14 15:58:56 +01007879 // If the class is abstract, we eagerly fetch the super class of the
7880 // object to avoid doing a comparison we know will fail.
7881 vixl32::Label loop;
7882 __ Bind(&loop);
7883 // /* HeapReference<Class> */ out = out->super_class_
Artem Serov657022c2016-11-23 14:19:38 +00007884 GenerateReferenceLoadOneRegister(instruction,
7885 out_loc,
7886 super_offset,
7887 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007888 read_barrier_option);
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007889 // If `out` is null, we use it for the result, and jump to the final label.
Anton Kirilov6f644202017-02-27 18:29:45 +00007890 __ CompareAndBranchIfZero(out, final_label, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007891 __ Cmp(out, cls);
Artem Serov517d9f62016-12-12 15:51:15 +00007892 __ B(ne, &loop, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007893 __ Mov(out, 1);
Artem Serovcfbe9132016-10-14 15:58:56 +01007894 break;
7895 }
7896
7897 case TypeCheckKind::kClassHierarchyCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00007898 ReadBarrierOption read_barrier_option =
7899 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier6beced42016-11-15 15:51:31 -08007900 // /* HeapReference<Class> */ out = obj->klass_
7901 GenerateReferenceLoadTwoRegisters(instruction,
7902 out_loc,
7903 obj_loc,
7904 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00007905 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007906 read_barrier_option);
Artem Serovcfbe9132016-10-14 15:58:56 +01007907 // Walk over the class hierarchy to find a match.
7908 vixl32::Label loop, success;
7909 __ Bind(&loop);
7910 __ Cmp(out, cls);
Artem Serov517d9f62016-12-12 15:51:15 +00007911 __ B(eq, &success, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007912 // /* HeapReference<Class> */ out = out->super_class_
Artem Serov657022c2016-11-23 14:19:38 +00007913 GenerateReferenceLoadOneRegister(instruction,
7914 out_loc,
7915 super_offset,
7916 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007917 read_barrier_option);
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007918 // This is essentially a null check, but it sets the condition flags to the
7919 // proper value for the code that follows the loop, i.e. not `eq`.
7920 __ Cmp(out, 1);
7921 __ B(hs, &loop, /* far_target */ false);
7922
7923 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
7924 // we check that the output is in a low register, so that a 16-bit MOV
7925 // encoding can be used.
7926 if (out.IsLow()) {
7927 // If `out` is null, we use it for the result, and the condition flags
7928 // have already been set to `ne`, so the IT block that comes afterwards
7929 // (and which handles the successful case) turns into a NOP (instead of
7930 // overwriting `out`).
7931 __ Bind(&success);
7932
7933 // We use the scope because of the IT block that follows.
7934 ExactAssemblyScope guard(GetVIXLAssembler(),
7935 2 * vixl32::k16BitT32InstructionSizeInBytes,
7936 CodeBufferCheckScope::kExactSize);
7937
7938 // There is only one branch to the `success` label (which is bound to this
7939 // IT block), and it has the same condition, `eq`, so in that case the MOV
7940 // is executed.
7941 __ it(eq);
7942 __ mov(eq, out, 1);
7943 } else {
7944 // If `out` is null, we use it for the result, and jump to the final label.
Anton Kirilov6f644202017-02-27 18:29:45 +00007945 __ B(final_label);
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007946 __ Bind(&success);
7947 __ Mov(out, 1);
Artem Serovcfbe9132016-10-14 15:58:56 +01007948 }
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007949
Artem Serovcfbe9132016-10-14 15:58:56 +01007950 break;
7951 }
7952
7953 case TypeCheckKind::kArrayObjectCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00007954 ReadBarrierOption read_barrier_option =
7955 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier6beced42016-11-15 15:51:31 -08007956 // /* HeapReference<Class> */ out = obj->klass_
7957 GenerateReferenceLoadTwoRegisters(instruction,
7958 out_loc,
7959 obj_loc,
7960 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00007961 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007962 read_barrier_option);
Artem Serovcfbe9132016-10-14 15:58:56 +01007963 // Do an exact check.
7964 vixl32::Label exact_check;
7965 __ Cmp(out, cls);
Artem Serov517d9f62016-12-12 15:51:15 +00007966 __ B(eq, &exact_check, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007967 // Otherwise, we need to check that the object's class is a non-primitive array.
7968 // /* HeapReference<Class> */ out = out->component_type_
Artem Serov657022c2016-11-23 14:19:38 +00007969 GenerateReferenceLoadOneRegister(instruction,
7970 out_loc,
7971 component_offset,
7972 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007973 read_barrier_option);
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007974 // If `out` is null, we use it for the result, and jump to the final label.
Anton Kirilov6f644202017-02-27 18:29:45 +00007975 __ CompareAndBranchIfZero(out, final_label, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007976 GetAssembler()->LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
7977 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007978 __ Cmp(out, 0);
7979 // We speculatively set the result to false without changing the condition
7980 // flags, which allows us to avoid some branching later.
7981 __ Mov(LeaveFlags, out, 0);
7982
7983 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
7984 // we check that the output is in a low register, so that a 16-bit MOV
7985 // encoding can be used.
7986 if (out.IsLow()) {
7987 __ Bind(&exact_check);
7988
7989 // We use the scope because of the IT block that follows.
7990 ExactAssemblyScope guard(GetVIXLAssembler(),
7991 2 * vixl32::k16BitT32InstructionSizeInBytes,
7992 CodeBufferCheckScope::kExactSize);
7993
7994 __ it(eq);
7995 __ mov(eq, out, 1);
7996 } else {
7997 __ B(ne, final_label, /* far_target */ false);
7998 __ Bind(&exact_check);
7999 __ Mov(out, 1);
8000 }
8001
Artem Serovcfbe9132016-10-14 15:58:56 +01008002 break;
8003 }
8004
8005 case TypeCheckKind::kArrayCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00008006 // No read barrier since the slow path will retry upon failure.
Mathieu Chartier6beced42016-11-15 15:51:31 -08008007 // /* HeapReference<Class> */ out = obj->klass_
8008 GenerateReferenceLoadTwoRegisters(instruction,
8009 out_loc,
8010 obj_loc,
8011 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00008012 maybe_temp_loc,
8013 kWithoutReadBarrier);
Artem Serovcfbe9132016-10-14 15:58:56 +01008014 __ Cmp(out, cls);
8015 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01008016 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathARMVIXL(
8017 instruction, /* is_fatal */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01008018 codegen_->AddSlowPath(slow_path);
8019 __ B(ne, slow_path->GetEntryLabel());
8020 __ Mov(out, 1);
Artem Serovcfbe9132016-10-14 15:58:56 +01008021 break;
8022 }
8023
8024 case TypeCheckKind::kUnresolvedCheck:
8025 case TypeCheckKind::kInterfaceCheck: {
8026 // Note that we indeed only call on slow path, but we always go
8027 // into the slow path for the unresolved and interface check
8028 // cases.
8029 //
8030 // We cannot directly call the InstanceofNonTrivial runtime
8031 // entry point without resorting to a type checking slow path
8032 // here (i.e. by calling InvokeRuntime directly), as it would
8033 // require to assign fixed registers for the inputs of this
8034 // HInstanceOf instruction (following the runtime calling
8035 // convention), which might be cluttered by the potential first
8036 // read barrier emission at the beginning of this method.
8037 //
8038 // TODO: Introduce a new runtime entry point taking the object
8039 // to test (instead of its class) as argument, and let it deal
8040 // with the read barrier issues. This will let us refactor this
8041 // case of the `switch` code as it was previously (with a direct
8042 // call to the runtime not using a type checking slow path).
8043 // This should also be beneficial for the other cases above.
8044 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01008045 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathARMVIXL(
8046 instruction, /* is_fatal */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01008047 codegen_->AddSlowPath(slow_path);
8048 __ B(slow_path->GetEntryLabel());
Artem Serovcfbe9132016-10-14 15:58:56 +01008049 break;
8050 }
Vladimir Marko175e7862018-03-27 09:03:13 +00008051
8052 case TypeCheckKind::kBitstringCheck: {
8053 // /* HeapReference<Class> */ temp = obj->klass_
8054 GenerateReferenceLoadTwoRegisters(instruction,
8055 out_loc,
8056 obj_loc,
8057 class_offset,
8058 maybe_temp_loc,
8059 kWithoutReadBarrier);
8060
8061 GenerateBitstringTypeCheckCompare(instruction, out, DontCare);
8062 // If `out` is a low reg and we would have another low reg temp, we could
8063 // optimize this as RSBS+ADC, see GenerateConditionWithZero().
8064 //
8065 // Also, in some cases when `out` is a low reg and we're loading a constant to IP
8066 // it would make sense to use CMP+MOV+IT+MOV instead of SUB+CLZ+LSR as the code size
8067 // would be the same and we would have fewer direct data dependencies.
8068 codegen_->GenerateConditionWithZero(kCondEQ, out, out); // CLZ+LSR
8069 break;
8070 }
Artem Serovcfbe9132016-10-14 15:58:56 +01008071 }
8072
Artem Serovcfbe9132016-10-14 15:58:56 +01008073 if (done.IsReferenced()) {
8074 __ Bind(&done);
8075 }
8076
8077 if (slow_path != nullptr) {
8078 __ Bind(slow_path->GetExitLabel());
8079 }
8080}
8081
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008082void LocationsBuilderARMVIXL::VisitCheckCast(HCheckCast* instruction) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008083 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko87584542017-12-12 17:47:52 +00008084 LocationSummary::CallKind call_kind = CodeGenerator::GetCheckCastCallKind(instruction);
Vladimir Markoca6fff82017-10-03 14:49:14 +01008085 LocationSummary* locations =
8086 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008087 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko175e7862018-03-27 09:03:13 +00008088 if (type_check_kind == TypeCheckKind::kBitstringCheck) {
8089 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
8090 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
8091 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
8092 } else {
8093 locations->SetInAt(1, Location::RequiresRegister());
8094 }
Artem Serov657022c2016-11-23 14:19:38 +00008095 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008096}
8097
8098void InstructionCodeGeneratorARMVIXL::VisitCheckCast(HCheckCast* instruction) {
8099 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
8100 LocationSummary* locations = instruction->GetLocations();
8101 Location obj_loc = locations->InAt(0);
8102 vixl32::Register obj = InputRegisterAt(instruction, 0);
Vladimir Marko175e7862018-03-27 09:03:13 +00008103 vixl32::Register cls = (type_check_kind == TypeCheckKind::kBitstringCheck)
8104 ? vixl32::Register()
8105 : InputRegisterAt(instruction, 1);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008106 Location temp_loc = locations->GetTemp(0);
8107 vixl32::Register temp = RegisterFrom(temp_loc);
Artem Serov657022c2016-11-23 14:19:38 +00008108 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
8109 DCHECK_LE(num_temps, 3u);
8110 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
8111 Location maybe_temp3_loc = (num_temps >= 3) ? locations->GetTemp(2) : Location::NoLocation();
8112 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
8113 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
8114 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
8115 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
8116 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
8117 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
8118 const uint32_t object_array_data_offset =
8119 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008120
Vladimir Marko87584542017-12-12 17:47:52 +00008121 bool is_type_check_slow_path_fatal = CodeGenerator::IsTypeCheckSlowPathFatal(instruction);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008122 SlowPathCodeARMVIXL* type_check_slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01008123 new (codegen_->GetScopedAllocator()) TypeCheckSlowPathARMVIXL(
8124 instruction, is_type_check_slow_path_fatal);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008125 codegen_->AddSlowPath(type_check_slow_path);
8126
8127 vixl32::Label done;
Anton Kirilov6f644202017-02-27 18:29:45 +00008128 vixl32::Label* final_label = codegen_->GetFinalLabel(instruction, &done);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008129 // Avoid null check if we know obj is not null.
8130 if (instruction->MustDoNullCheck()) {
Anton Kirilov6f644202017-02-27 18:29:45 +00008131 __ CompareAndBranchIfZero(obj, final_label, /* far_target */ false);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008132 }
8133
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008134 switch (type_check_kind) {
8135 case TypeCheckKind::kExactCheck:
8136 case TypeCheckKind::kArrayCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00008137 // /* HeapReference<Class> */ temp = obj->klass_
8138 GenerateReferenceLoadTwoRegisters(instruction,
8139 temp_loc,
8140 obj_loc,
8141 class_offset,
8142 maybe_temp2_loc,
8143 kWithoutReadBarrier);
8144
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008145 __ Cmp(temp, cls);
8146 // Jump to slow path for throwing the exception or doing a
8147 // more involved array check.
8148 __ B(ne, type_check_slow_path->GetEntryLabel());
8149 break;
8150 }
8151
8152 case TypeCheckKind::kAbstractClassCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00008153 // /* HeapReference<Class> */ temp = obj->klass_
8154 GenerateReferenceLoadTwoRegisters(instruction,
8155 temp_loc,
8156 obj_loc,
8157 class_offset,
8158 maybe_temp2_loc,
8159 kWithoutReadBarrier);
8160
Artem Serovcfbe9132016-10-14 15:58:56 +01008161 // If the class is abstract, we eagerly fetch the super class of the
8162 // object to avoid doing a comparison we know will fail.
8163 vixl32::Label loop;
8164 __ Bind(&loop);
8165 // /* HeapReference<Class> */ temp = temp->super_class_
Artem Serov657022c2016-11-23 14:19:38 +00008166 GenerateReferenceLoadOneRegister(instruction,
8167 temp_loc,
8168 super_offset,
8169 maybe_temp2_loc,
8170 kWithoutReadBarrier);
Artem Serovcfbe9132016-10-14 15:58:56 +01008171
8172 // If the class reference currently in `temp` is null, jump to the slow path to throw the
8173 // exception.
xueliang.zhongf51bc622016-11-04 09:23:32 +00008174 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
Artem Serovcfbe9132016-10-14 15:58:56 +01008175
8176 // Otherwise, compare the classes.
8177 __ Cmp(temp, cls);
Artem Serov517d9f62016-12-12 15:51:15 +00008178 __ B(ne, &loop, /* far_target */ false);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008179 break;
8180 }
8181
8182 case TypeCheckKind::kClassHierarchyCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00008183 // /* HeapReference<Class> */ temp = obj->klass_
8184 GenerateReferenceLoadTwoRegisters(instruction,
8185 temp_loc,
8186 obj_loc,
8187 class_offset,
8188 maybe_temp2_loc,
8189 kWithoutReadBarrier);
8190
Artem Serovcfbe9132016-10-14 15:58:56 +01008191 // Walk over the class hierarchy to find a match.
8192 vixl32::Label loop;
8193 __ Bind(&loop);
8194 __ Cmp(temp, cls);
Anton Kirilov6f644202017-02-27 18:29:45 +00008195 __ B(eq, final_label, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01008196
8197 // /* HeapReference<Class> */ temp = temp->super_class_
Artem Serov657022c2016-11-23 14:19:38 +00008198 GenerateReferenceLoadOneRegister(instruction,
8199 temp_loc,
8200 super_offset,
8201 maybe_temp2_loc,
8202 kWithoutReadBarrier);
Artem Serovcfbe9132016-10-14 15:58:56 +01008203
8204 // If the class reference currently in `temp` is null, jump to the slow path to throw the
8205 // exception.
xueliang.zhongf51bc622016-11-04 09:23:32 +00008206 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
Artem Serovcfbe9132016-10-14 15:58:56 +01008207 // Otherwise, jump to the beginning of the loop.
8208 __ B(&loop);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008209 break;
8210 }
8211
Artem Serovcfbe9132016-10-14 15:58:56 +01008212 case TypeCheckKind::kArrayObjectCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00008213 // /* HeapReference<Class> */ temp = obj->klass_
8214 GenerateReferenceLoadTwoRegisters(instruction,
8215 temp_loc,
8216 obj_loc,
8217 class_offset,
8218 maybe_temp2_loc,
8219 kWithoutReadBarrier);
8220
Artem Serovcfbe9132016-10-14 15:58:56 +01008221 // Do an exact check.
8222 __ Cmp(temp, cls);
Anton Kirilov6f644202017-02-27 18:29:45 +00008223 __ B(eq, final_label, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01008224
8225 // Otherwise, we need to check that the object's class is a non-primitive array.
8226 // /* HeapReference<Class> */ temp = temp->component_type_
Artem Serov657022c2016-11-23 14:19:38 +00008227 GenerateReferenceLoadOneRegister(instruction,
8228 temp_loc,
8229 component_offset,
8230 maybe_temp2_loc,
8231 kWithoutReadBarrier);
Artem Serovcfbe9132016-10-14 15:58:56 +01008232 // If the component type is null, jump to the slow path to throw the exception.
xueliang.zhongf51bc622016-11-04 09:23:32 +00008233 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
Artem Serovcfbe9132016-10-14 15:58:56 +01008234 // Otherwise,the object is indeed an array, jump to label `check_non_primitive_component_type`
8235 // to further check that this component type is not a primitive type.
8236 GetAssembler()->LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008237 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
xueliang.zhongf51bc622016-11-04 09:23:32 +00008238 __ CompareAndBranchIfNonZero(temp, type_check_slow_path->GetEntryLabel());
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008239 break;
8240 }
8241
8242 case TypeCheckKind::kUnresolvedCheck:
Artem Serov657022c2016-11-23 14:19:38 +00008243 // We always go into the type check slow path for the unresolved check case.
Artem Serovcfbe9132016-10-14 15:58:56 +01008244 // We cannot directly call the CheckCast runtime entry point
8245 // without resorting to a type checking slow path here (i.e. by
8246 // calling InvokeRuntime directly), as it would require to
8247 // assign fixed registers for the inputs of this HInstanceOf
8248 // instruction (following the runtime calling convention), which
8249 // might be cluttered by the potential first read barrier
8250 // emission at the beginning of this method.
Artem Serov657022c2016-11-23 14:19:38 +00008251
Artem Serovcfbe9132016-10-14 15:58:56 +01008252 __ B(type_check_slow_path->GetEntryLabel());
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008253 break;
Artem Serov657022c2016-11-23 14:19:38 +00008254
8255 case TypeCheckKind::kInterfaceCheck: {
8256 // Avoid read barriers to improve performance of the fast path. We can not get false
8257 // positives by doing this.
8258 // /* HeapReference<Class> */ temp = obj->klass_
8259 GenerateReferenceLoadTwoRegisters(instruction,
8260 temp_loc,
8261 obj_loc,
8262 class_offset,
8263 maybe_temp2_loc,
8264 kWithoutReadBarrier);
8265
8266 // /* HeapReference<Class> */ temp = temp->iftable_
8267 GenerateReferenceLoadTwoRegisters(instruction,
8268 temp_loc,
8269 temp_loc,
8270 iftable_offset,
8271 maybe_temp2_loc,
8272 kWithoutReadBarrier);
8273 // Iftable is never null.
8274 __ Ldr(RegisterFrom(maybe_temp2_loc), MemOperand(temp, array_length_offset));
8275 // Loop through the iftable and check if any class matches.
8276 vixl32::Label start_loop;
8277 __ Bind(&start_loop);
8278 __ CompareAndBranchIfZero(RegisterFrom(maybe_temp2_loc),
8279 type_check_slow_path->GetEntryLabel());
8280 __ Ldr(RegisterFrom(maybe_temp3_loc), MemOperand(temp, object_array_data_offset));
8281 GetAssembler()->MaybeUnpoisonHeapReference(RegisterFrom(maybe_temp3_loc));
8282 // Go to next interface.
8283 __ Add(temp, temp, Operand::From(2 * kHeapReferenceSize));
8284 __ Sub(RegisterFrom(maybe_temp2_loc), RegisterFrom(maybe_temp2_loc), 2);
8285 // Compare the classes and continue the loop if they do not match.
8286 __ Cmp(cls, RegisterFrom(maybe_temp3_loc));
Artem Serov517d9f62016-12-12 15:51:15 +00008287 __ B(ne, &start_loop, /* far_target */ false);
Artem Serov657022c2016-11-23 14:19:38 +00008288 break;
8289 }
Vladimir Marko175e7862018-03-27 09:03:13 +00008290
8291 case TypeCheckKind::kBitstringCheck: {
8292 // /* HeapReference<Class> */ temp = obj->klass_
8293 GenerateReferenceLoadTwoRegisters(instruction,
8294 temp_loc,
8295 obj_loc,
8296 class_offset,
8297 maybe_temp2_loc,
8298 kWithoutReadBarrier);
8299
8300 GenerateBitstringTypeCheckCompare(instruction, temp, SetFlags);
8301 __ B(ne, type_check_slow_path->GetEntryLabel());
8302 break;
8303 }
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008304 }
Anton Kirilov6f644202017-02-27 18:29:45 +00008305 if (done.IsReferenced()) {
8306 __ Bind(&done);
8307 }
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008308
8309 __ Bind(type_check_slow_path->GetExitLabel());
8310}
8311
Artem Serov551b28f2016-10-18 19:11:30 +01008312void LocationsBuilderARMVIXL::VisitMonitorOperation(HMonitorOperation* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008313 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
8314 instruction, LocationSummary::kCallOnMainOnly);
Artem Serov551b28f2016-10-18 19:11:30 +01008315 InvokeRuntimeCallingConventionARMVIXL calling_convention;
8316 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
8317}
8318
8319void InstructionCodeGeneratorARMVIXL::VisitMonitorOperation(HMonitorOperation* instruction) {
8320 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
8321 instruction,
8322 instruction->GetDexPc());
8323 if (instruction->IsEnter()) {
8324 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
8325 } else {
8326 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
8327 }
Roland Levillain5daa4952017-07-03 17:23:56 +01008328 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 17);
Artem Serov551b28f2016-10-18 19:11:30 +01008329}
8330
Artem Serov02109dd2016-09-23 17:17:54 +01008331void LocationsBuilderARMVIXL::VisitAnd(HAnd* instruction) {
8332 HandleBitwiseOperation(instruction, AND);
8333}
8334
8335void LocationsBuilderARMVIXL::VisitOr(HOr* instruction) {
8336 HandleBitwiseOperation(instruction, ORR);
8337}
8338
8339void LocationsBuilderARMVIXL::VisitXor(HXor* instruction) {
8340 HandleBitwiseOperation(instruction, EOR);
8341}
8342
8343void LocationsBuilderARMVIXL::HandleBitwiseOperation(HBinaryOperation* instruction, Opcode opcode) {
8344 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008345 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008346 DCHECK(instruction->GetResultType() == DataType::Type::kInt32
8347 || instruction->GetResultType() == DataType::Type::kInt64);
Artem Serov02109dd2016-09-23 17:17:54 +01008348 // Note: GVN reorders commutative operations to have the constant on the right hand side.
8349 locations->SetInAt(0, Location::RequiresRegister());
8350 locations->SetInAt(1, ArmEncodableConstantOrRegister(instruction->InputAt(1), opcode));
8351 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8352}
8353
8354void InstructionCodeGeneratorARMVIXL::VisitAnd(HAnd* instruction) {
8355 HandleBitwiseOperation(instruction);
8356}
8357
8358void InstructionCodeGeneratorARMVIXL::VisitOr(HOr* instruction) {
8359 HandleBitwiseOperation(instruction);
8360}
8361
8362void InstructionCodeGeneratorARMVIXL::VisitXor(HXor* instruction) {
8363 HandleBitwiseOperation(instruction);
8364}
8365
Artem Serov2bbc9532016-10-21 11:51:50 +01008366void LocationsBuilderARMVIXL::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
8367 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008368 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008369 DCHECK(instruction->GetResultType() == DataType::Type::kInt32
8370 || instruction->GetResultType() == DataType::Type::kInt64);
Artem Serov2bbc9532016-10-21 11:51:50 +01008371
8372 locations->SetInAt(0, Location::RequiresRegister());
8373 locations->SetInAt(1, Location::RequiresRegister());
8374 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8375}
8376
8377void InstructionCodeGeneratorARMVIXL::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
8378 LocationSummary* locations = instruction->GetLocations();
8379 Location first = locations->InAt(0);
8380 Location second = locations->InAt(1);
8381 Location out = locations->Out();
8382
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008383 if (instruction->GetResultType() == DataType::Type::kInt32) {
Artem Serov2bbc9532016-10-21 11:51:50 +01008384 vixl32::Register first_reg = RegisterFrom(first);
8385 vixl32::Register second_reg = RegisterFrom(second);
8386 vixl32::Register out_reg = RegisterFrom(out);
8387
8388 switch (instruction->GetOpKind()) {
8389 case HInstruction::kAnd:
8390 __ Bic(out_reg, first_reg, second_reg);
8391 break;
8392 case HInstruction::kOr:
8393 __ Orn(out_reg, first_reg, second_reg);
8394 break;
8395 // There is no EON on arm.
8396 case HInstruction::kXor:
8397 default:
8398 LOG(FATAL) << "Unexpected instruction " << instruction->DebugName();
8399 UNREACHABLE();
8400 }
8401 return;
8402
8403 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008404 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
Artem Serov2bbc9532016-10-21 11:51:50 +01008405 vixl32::Register first_low = LowRegisterFrom(first);
8406 vixl32::Register first_high = HighRegisterFrom(first);
8407 vixl32::Register second_low = LowRegisterFrom(second);
8408 vixl32::Register second_high = HighRegisterFrom(second);
8409 vixl32::Register out_low = LowRegisterFrom(out);
8410 vixl32::Register out_high = HighRegisterFrom(out);
8411
8412 switch (instruction->GetOpKind()) {
8413 case HInstruction::kAnd:
8414 __ Bic(out_low, first_low, second_low);
8415 __ Bic(out_high, first_high, second_high);
8416 break;
8417 case HInstruction::kOr:
8418 __ Orn(out_low, first_low, second_low);
8419 __ Orn(out_high, first_high, second_high);
8420 break;
8421 // There is no EON on arm.
8422 case HInstruction::kXor:
8423 default:
8424 LOG(FATAL) << "Unexpected instruction " << instruction->DebugName();
8425 UNREACHABLE();
8426 }
8427 }
8428}
8429
Anton Kirilov74234da2017-01-13 14:42:47 +00008430void LocationsBuilderARMVIXL::VisitDataProcWithShifterOp(
8431 HDataProcWithShifterOp* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008432 DCHECK(instruction->GetType() == DataType::Type::kInt32 ||
8433 instruction->GetType() == DataType::Type::kInt64);
Anton Kirilov74234da2017-01-13 14:42:47 +00008434 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008435 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008436 const bool overlap = instruction->GetType() == DataType::Type::kInt64 &&
Anton Kirilov74234da2017-01-13 14:42:47 +00008437 HDataProcWithShifterOp::IsExtensionOp(instruction->GetOpKind());
8438
8439 locations->SetInAt(0, Location::RequiresRegister());
8440 locations->SetInAt(1, Location::RequiresRegister());
8441 locations->SetOut(Location::RequiresRegister(),
8442 overlap ? Location::kOutputOverlap : Location::kNoOutputOverlap);
8443}
8444
8445void InstructionCodeGeneratorARMVIXL::VisitDataProcWithShifterOp(
8446 HDataProcWithShifterOp* instruction) {
8447 const LocationSummary* const locations = instruction->GetLocations();
8448 const HInstruction::InstructionKind kind = instruction->GetInstrKind();
8449 const HDataProcWithShifterOp::OpKind op_kind = instruction->GetOpKind();
8450
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008451 if (instruction->GetType() == DataType::Type::kInt32) {
Anton Kirilov420ee302017-02-21 18:10:26 +00008452 const vixl32::Register first = InputRegisterAt(instruction, 0);
8453 const vixl32::Register output = OutputRegister(instruction);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008454 const vixl32::Register second = instruction->InputAt(1)->GetType() == DataType::Type::kInt64
Anton Kirilov74234da2017-01-13 14:42:47 +00008455 ? LowRegisterFrom(locations->InAt(1))
8456 : InputRegisterAt(instruction, 1);
8457
Anton Kirilov420ee302017-02-21 18:10:26 +00008458 if (HDataProcWithShifterOp::IsExtensionOp(op_kind)) {
8459 DCHECK_EQ(kind, HInstruction::kAdd);
8460
8461 switch (op_kind) {
8462 case HDataProcWithShifterOp::kUXTB:
8463 __ Uxtab(output, first, second);
8464 break;
8465 case HDataProcWithShifterOp::kUXTH:
8466 __ Uxtah(output, first, second);
8467 break;
8468 case HDataProcWithShifterOp::kSXTB:
8469 __ Sxtab(output, first, second);
8470 break;
8471 case HDataProcWithShifterOp::kSXTH:
8472 __ Sxtah(output, first, second);
8473 break;
8474 default:
8475 LOG(FATAL) << "Unexpected operation kind: " << op_kind;
8476 UNREACHABLE();
8477 }
8478 } else {
8479 GenerateDataProcInstruction(kind,
8480 output,
8481 first,
8482 Operand(second,
8483 ShiftFromOpKind(op_kind),
8484 instruction->GetShiftAmount()),
8485 codegen_);
8486 }
Anton Kirilov74234da2017-01-13 14:42:47 +00008487 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008488 DCHECK_EQ(instruction->GetType(), DataType::Type::kInt64);
Anton Kirilov74234da2017-01-13 14:42:47 +00008489
8490 if (HDataProcWithShifterOp::IsExtensionOp(op_kind)) {
8491 const vixl32::Register second = InputRegisterAt(instruction, 1);
8492
8493 DCHECK(!LowRegisterFrom(locations->Out()).Is(second));
8494 GenerateDataProc(kind,
8495 locations->Out(),
8496 locations->InAt(0),
8497 second,
8498 Operand(second, ShiftType::ASR, 31),
8499 codegen_);
8500 } else {
8501 GenerateLongDataProc(instruction, codegen_);
8502 }
8503 }
8504}
8505
Artem Serov02109dd2016-09-23 17:17:54 +01008506// TODO(VIXL): Remove optimizations in the helper when they are implemented in vixl.
8507void InstructionCodeGeneratorARMVIXL::GenerateAndConst(vixl32::Register out,
8508 vixl32::Register first,
8509 uint32_t value) {
8510 // Optimize special cases for individual halfs of `and-long` (`and` is simplified earlier).
8511 if (value == 0xffffffffu) {
8512 if (!out.Is(first)) {
8513 __ Mov(out, first);
8514 }
8515 return;
8516 }
8517 if (value == 0u) {
8518 __ Mov(out, 0);
8519 return;
8520 }
8521 if (GetAssembler()->ShifterOperandCanHold(AND, value)) {
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00008522 __ And(out, first, value);
8523 } else if (GetAssembler()->ShifterOperandCanHold(BIC, ~value)) {
8524 __ Bic(out, first, ~value);
Artem Serov02109dd2016-09-23 17:17:54 +01008525 } else {
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00008526 DCHECK(IsPowerOfTwo(value + 1));
8527 __ Ubfx(out, first, 0, WhichPowerOf2(value + 1));
Artem Serov02109dd2016-09-23 17:17:54 +01008528 }
8529}
8530
8531// TODO(VIXL): Remove optimizations in the helper when they are implemented in vixl.
8532void InstructionCodeGeneratorARMVIXL::GenerateOrrConst(vixl32::Register out,
8533 vixl32::Register first,
8534 uint32_t value) {
8535 // Optimize special cases for individual halfs of `or-long` (`or` is simplified earlier).
8536 if (value == 0u) {
8537 if (!out.Is(first)) {
8538 __ Mov(out, first);
8539 }
8540 return;
8541 }
8542 if (value == 0xffffffffu) {
8543 __ Mvn(out, 0);
8544 return;
8545 }
8546 if (GetAssembler()->ShifterOperandCanHold(ORR, value)) {
8547 __ Orr(out, first, value);
8548 } else {
8549 DCHECK(GetAssembler()->ShifterOperandCanHold(ORN, ~value));
8550 __ Orn(out, first, ~value);
8551 }
8552}
8553
8554// TODO(VIXL): Remove optimizations in the helper when they are implemented in vixl.
8555void InstructionCodeGeneratorARMVIXL::GenerateEorConst(vixl32::Register out,
8556 vixl32::Register first,
8557 uint32_t value) {
8558 // Optimize special case for individual halfs of `xor-long` (`xor` is simplified earlier).
8559 if (value == 0u) {
8560 if (!out.Is(first)) {
8561 __ Mov(out, first);
8562 }
8563 return;
8564 }
8565 __ Eor(out, first, value);
8566}
8567
Anton Kirilovdda43962016-11-21 19:55:20 +00008568void InstructionCodeGeneratorARMVIXL::GenerateAddLongConst(Location out,
8569 Location first,
8570 uint64_t value) {
8571 vixl32::Register out_low = LowRegisterFrom(out);
8572 vixl32::Register out_high = HighRegisterFrom(out);
8573 vixl32::Register first_low = LowRegisterFrom(first);
8574 vixl32::Register first_high = HighRegisterFrom(first);
8575 uint32_t value_low = Low32Bits(value);
8576 uint32_t value_high = High32Bits(value);
8577 if (value_low == 0u) {
8578 if (!out_low.Is(first_low)) {
8579 __ Mov(out_low, first_low);
8580 }
8581 __ Add(out_high, first_high, value_high);
8582 return;
8583 }
8584 __ Adds(out_low, first_low, value_low);
Vladimir Markof0a6a1d2018-01-08 14:23:56 +00008585 if (GetAssembler()->ShifterOperandCanHold(ADC, value_high)) {
Anton Kirilovdda43962016-11-21 19:55:20 +00008586 __ Adc(out_high, first_high, value_high);
Anton Kirilovdda43962016-11-21 19:55:20 +00008587 } else {
Vladimir Markof0a6a1d2018-01-08 14:23:56 +00008588 DCHECK(GetAssembler()->ShifterOperandCanHold(SBC, ~value_high));
8589 __ Sbc(out_high, first_high, ~value_high);
Anton Kirilovdda43962016-11-21 19:55:20 +00008590 }
8591}
8592
Artem Serov02109dd2016-09-23 17:17:54 +01008593void InstructionCodeGeneratorARMVIXL::HandleBitwiseOperation(HBinaryOperation* instruction) {
8594 LocationSummary* locations = instruction->GetLocations();
8595 Location first = locations->InAt(0);
8596 Location second = locations->InAt(1);
8597 Location out = locations->Out();
8598
8599 if (second.IsConstant()) {
8600 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
8601 uint32_t value_low = Low32Bits(value);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008602 if (instruction->GetResultType() == DataType::Type::kInt32) {
Artem Serov02109dd2016-09-23 17:17:54 +01008603 vixl32::Register first_reg = InputRegisterAt(instruction, 0);
8604 vixl32::Register out_reg = OutputRegister(instruction);
8605 if (instruction->IsAnd()) {
8606 GenerateAndConst(out_reg, first_reg, value_low);
8607 } else if (instruction->IsOr()) {
8608 GenerateOrrConst(out_reg, first_reg, value_low);
8609 } else {
8610 DCHECK(instruction->IsXor());
8611 GenerateEorConst(out_reg, first_reg, value_low);
8612 }
8613 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008614 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
Artem Serov02109dd2016-09-23 17:17:54 +01008615 uint32_t value_high = High32Bits(value);
8616 vixl32::Register first_low = LowRegisterFrom(first);
8617 vixl32::Register first_high = HighRegisterFrom(first);
8618 vixl32::Register out_low = LowRegisterFrom(out);
8619 vixl32::Register out_high = HighRegisterFrom(out);
8620 if (instruction->IsAnd()) {
8621 GenerateAndConst(out_low, first_low, value_low);
8622 GenerateAndConst(out_high, first_high, value_high);
8623 } else if (instruction->IsOr()) {
8624 GenerateOrrConst(out_low, first_low, value_low);
8625 GenerateOrrConst(out_high, first_high, value_high);
8626 } else {
8627 DCHECK(instruction->IsXor());
8628 GenerateEorConst(out_low, first_low, value_low);
8629 GenerateEorConst(out_high, first_high, value_high);
8630 }
8631 }
8632 return;
8633 }
8634
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008635 if (instruction->GetResultType() == DataType::Type::kInt32) {
Artem Serov02109dd2016-09-23 17:17:54 +01008636 vixl32::Register first_reg = InputRegisterAt(instruction, 0);
8637 vixl32::Register second_reg = InputRegisterAt(instruction, 1);
8638 vixl32::Register out_reg = OutputRegister(instruction);
8639 if (instruction->IsAnd()) {
8640 __ And(out_reg, first_reg, second_reg);
8641 } else if (instruction->IsOr()) {
8642 __ Orr(out_reg, first_reg, second_reg);
8643 } else {
8644 DCHECK(instruction->IsXor());
8645 __ Eor(out_reg, first_reg, second_reg);
8646 }
8647 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008648 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
Artem Serov02109dd2016-09-23 17:17:54 +01008649 vixl32::Register first_low = LowRegisterFrom(first);
8650 vixl32::Register first_high = HighRegisterFrom(first);
8651 vixl32::Register second_low = LowRegisterFrom(second);
8652 vixl32::Register second_high = HighRegisterFrom(second);
8653 vixl32::Register out_low = LowRegisterFrom(out);
8654 vixl32::Register out_high = HighRegisterFrom(out);
8655 if (instruction->IsAnd()) {
8656 __ And(out_low, first_low, second_low);
8657 __ And(out_high, first_high, second_high);
8658 } else if (instruction->IsOr()) {
8659 __ Orr(out_low, first_low, second_low);
8660 __ Orr(out_high, first_high, second_high);
8661 } else {
8662 DCHECK(instruction->IsXor());
8663 __ Eor(out_low, first_low, second_low);
8664 __ Eor(out_high, first_high, second_high);
8665 }
8666 }
8667}
8668
Artem Serovcfbe9132016-10-14 15:58:56 +01008669void InstructionCodeGeneratorARMVIXL::GenerateReferenceLoadOneRegister(
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008670 HInstruction* instruction,
Artem Serovcfbe9132016-10-14 15:58:56 +01008671 Location out,
8672 uint32_t offset,
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008673 Location maybe_temp,
8674 ReadBarrierOption read_barrier_option) {
Artem Serovcfbe9132016-10-14 15:58:56 +01008675 vixl32::Register out_reg = RegisterFrom(out);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008676 if (read_barrier_option == kWithReadBarrier) {
8677 CHECK(kEmitCompilerReadBarrier);
8678 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
8679 if (kUseBakerReadBarrier) {
8680 // Load with fast path based Baker's read barrier.
8681 // /* HeapReference<Object> */ out = *(out + offset)
8682 codegen_->GenerateFieldLoadWithBakerReadBarrier(
8683 instruction, out, out_reg, offset, maybe_temp, /* needs_null_check */ false);
8684 } else {
8685 // Load with slow path based read barrier.
8686 // Save the value of `out` into `maybe_temp` before overwriting it
8687 // in the following move operation, as we will need it for the
8688 // read barrier below.
8689 __ Mov(RegisterFrom(maybe_temp), out_reg);
8690 // /* HeapReference<Object> */ out = *(out + offset)
8691 GetAssembler()->LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
8692 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
8693 }
Artem Serovcfbe9132016-10-14 15:58:56 +01008694 } else {
8695 // Plain load with no read barrier.
8696 // /* HeapReference<Object> */ out = *(out + offset)
8697 GetAssembler()->LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
8698 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
8699 }
8700}
8701
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008702void InstructionCodeGeneratorARMVIXL::GenerateReferenceLoadTwoRegisters(
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008703 HInstruction* instruction,
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008704 Location out,
8705 Location obj,
8706 uint32_t offset,
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008707 Location maybe_temp,
8708 ReadBarrierOption read_barrier_option) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008709 vixl32::Register out_reg = RegisterFrom(out);
8710 vixl32::Register obj_reg = RegisterFrom(obj);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008711 if (read_barrier_option == kWithReadBarrier) {
8712 CHECK(kEmitCompilerReadBarrier);
8713 if (kUseBakerReadBarrier) {
8714 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
8715 // Load with fast path based Baker's read barrier.
8716 // /* HeapReference<Object> */ out = *(obj + offset)
8717 codegen_->GenerateFieldLoadWithBakerReadBarrier(
8718 instruction, out, obj_reg, offset, maybe_temp, /* needs_null_check */ false);
8719 } else {
8720 // Load with slow path based read barrier.
8721 // /* HeapReference<Object> */ out = *(obj + offset)
8722 GetAssembler()->LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
8723 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
8724 }
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008725 } else {
8726 // Plain load with no read barrier.
8727 // /* HeapReference<Object> */ out = *(obj + offset)
8728 GetAssembler()->LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
8729 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
8730 }
8731}
8732
Scott Wakelinga7812ae2016-10-17 10:03:36 +01008733void InstructionCodeGeneratorARMVIXL::GenerateGcRootFieldLoad(
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008734 HInstruction* instruction,
Scott Wakelinga7812ae2016-10-17 10:03:36 +01008735 Location root,
8736 vixl32::Register obj,
8737 uint32_t offset,
Artem Serovd4cc5b22016-11-04 11:19:09 +00008738 ReadBarrierOption read_barrier_option) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01008739 vixl32::Register root_reg = RegisterFrom(root);
Artem Serovd4cc5b22016-11-04 11:19:09 +00008740 if (read_barrier_option == kWithReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008741 DCHECK(kEmitCompilerReadBarrier);
8742 if (kUseBakerReadBarrier) {
8743 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
Roland Levillainba650a42017-03-06 13:52:32 +00008744 // Baker's read barrier are used.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008745 if (kBakerReadBarrierLinkTimeThunksEnableForGcRoots &&
8746 !Runtime::Current()->UseJitCompilation()) {
Roland Levillain6d729a72017-06-30 18:34:01 +01008747 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in
8748 // the Marking Register) to decide whether we need to enter
8749 // the slow path to mark the GC root.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008750 //
8751 // We use link-time generated thunks for the slow path. That thunk
8752 // checks the reference and jumps to the entrypoint if needed.
8753 //
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008754 // lr = &return_address;
8755 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
Roland Levillain6d729a72017-06-30 18:34:01 +01008756 // if (mr) { // Thread::Current()->GetIsGcMarking()
8757 // goto gc_root_thunk<root_reg>(lr)
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008758 // }
8759 // return_address:
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008760
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008761 UseScratchRegisterScope temps(GetVIXLAssembler());
8762 ExcludeIPAndBakerCcEntrypointRegister(&temps, instruction);
Vladimir Marko88abba22017-05-03 17:09:25 +01008763 bool narrow = CanEmitNarrowLdr(root_reg, obj, offset);
8764 uint32_t custom_data = linker::Thumb2RelativePatcher::EncodeBakerReadBarrierGcRootData(
8765 root_reg.GetCode(), narrow);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008766 vixl32::Label* bne_label = codegen_->NewBakerReadBarrierPatch(custom_data);
Roland Levillainba650a42017-03-06 13:52:32 +00008767
Roland Levillain6d729a72017-06-30 18:34:01 +01008768 vixl::EmissionCheckScope guard(GetVIXLAssembler(), 4 * vixl32::kMaxInstructionSizeInBytes);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008769 vixl32::Label return_address;
8770 EmitAdrCode adr(GetVIXLAssembler(), lr, &return_address);
Roland Levillain6d729a72017-06-30 18:34:01 +01008771 __ cmp(mr, Operand(0));
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008772 // Currently the offset is always within range. If that changes,
8773 // we shall have to split the load the same way as for fields.
8774 DCHECK_LT(offset, kReferenceLoadMinFarOffset);
Vladimir Marko88abba22017-05-03 17:09:25 +01008775 ptrdiff_t old_offset = GetVIXLAssembler()->GetBuffer()->GetCursorOffset();
8776 __ ldr(EncodingSize(narrow ? Narrow : Wide), root_reg, MemOperand(obj, offset));
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008777 EmitPlaceholderBne(codegen_, bne_label);
8778 __ Bind(&return_address);
Vladimir Marko88abba22017-05-03 17:09:25 +01008779 DCHECK_EQ(old_offset - GetVIXLAssembler()->GetBuffer()->GetCursorOffset(),
8780 narrow ? BAKER_MARK_INTROSPECTION_GC_ROOT_LDR_NARROW_OFFSET
8781 : BAKER_MARK_INTROSPECTION_GC_ROOT_LDR_WIDE_OFFSET);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008782 } else {
Roland Levillain6d729a72017-06-30 18:34:01 +01008783 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in
8784 // the Marking Register) to decide whether we need to enter
8785 // the slow path to mark the GC root.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008786 //
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008787 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
Roland Levillain6d729a72017-06-30 18:34:01 +01008788 // if (mr) { // Thread::Current()->GetIsGcMarking()
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008789 // // Slow path.
Roland Levillain6d729a72017-06-30 18:34:01 +01008790 // entrypoint = Thread::Current()->pReadBarrierMarkReg ## root.reg()
8791 // root = entrypoint(root); // root = ReadBarrier::Mark(root); // Entry point call.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008792 // }
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008793
Roland Levillain6d729a72017-06-30 18:34:01 +01008794 // Slow path marking the GC root `root`. The entrypoint will
8795 // be loaded by the slow path code.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008796 SlowPathCodeARMVIXL* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01008797 new (codegen_->GetScopedAllocator()) ReadBarrierMarkSlowPathARMVIXL(instruction, root);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008798 codegen_->AddSlowPath(slow_path);
8799
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008800 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
8801 GetAssembler()->LoadFromOffset(kLoadWord, root_reg, obj, offset);
8802 static_assert(
8803 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
8804 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
8805 "have different sizes.");
8806 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
8807 "art::mirror::CompressedReference<mirror::Object> and int32_t "
8808 "have different sizes.");
8809
Roland Levillain6d729a72017-06-30 18:34:01 +01008810 __ CompareAndBranchIfNonZero(mr, slow_path->GetEntryLabel());
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008811 __ Bind(slow_path->GetExitLabel());
8812 }
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008813 } else {
8814 // GC root loaded through a slow path for read barriers other
8815 // than Baker's.
8816 // /* GcRoot<mirror::Object>* */ root = obj + offset
8817 __ Add(root_reg, obj, offset);
8818 // /* mirror::Object* */ root = root->Read()
8819 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
8820 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01008821 } else {
8822 // Plain GC root load with no read barrier.
8823 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
8824 GetAssembler()->LoadFromOffset(kLoadWord, root_reg, obj, offset);
8825 // Note that GC roots are not affected by heap poisoning, thus we
8826 // do not have to unpoison `root_reg` here.
8827 }
Roland Levillain5daa4952017-07-03 17:23:56 +01008828 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 18);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01008829}
8830
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008831void CodeGeneratorARMVIXL::MaybeAddBakerCcEntrypointTempForFields(LocationSummary* locations) {
8832 DCHECK(kEmitCompilerReadBarrier);
8833 DCHECK(kUseBakerReadBarrier);
8834 if (kBakerReadBarrierLinkTimeThunksEnableForFields) {
8835 if (!Runtime::Current()->UseJitCompilation()) {
8836 locations->AddTemp(Location::RegisterLocation(kBakerCcEntrypointRegister.GetCode()));
8837 }
8838 }
8839}
8840
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008841void CodeGeneratorARMVIXL::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
8842 Location ref,
8843 vixl32::Register obj,
8844 uint32_t offset,
8845 Location temp,
8846 bool needs_null_check) {
8847 DCHECK(kEmitCompilerReadBarrier);
8848 DCHECK(kUseBakerReadBarrier);
8849
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008850 if (kBakerReadBarrierLinkTimeThunksEnableForFields &&
8851 !Runtime::Current()->UseJitCompilation()) {
Roland Levillain6d729a72017-06-30 18:34:01 +01008852 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in the
8853 // Marking Register) to decide whether we need to enter the slow
8854 // path to mark the reference. Then, in the slow path, check the
8855 // gray bit in the lock word of the reference's holder (`obj`) to
8856 // decide whether to mark `ref` or not.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008857 //
8858 // We use link-time generated thunks for the slow path. That thunk checks
8859 // the holder and jumps to the entrypoint if needed. If the holder is not
8860 // gray, it creates a fake dependency and returns to the LDR instruction.
8861 //
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008862 // lr = &gray_return_address;
Roland Levillain6d729a72017-06-30 18:34:01 +01008863 // if (mr) { // Thread::Current()->GetIsGcMarking()
8864 // goto field_thunk<holder_reg, base_reg>(lr)
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008865 // }
8866 // not_gray_return_address:
8867 // // Original reference load. If the offset is too large to fit
8868 // // into LDR, we use an adjusted base register here.
Vladimir Marko88abba22017-05-03 17:09:25 +01008869 // HeapReference<mirror::Object> reference = *(obj+offset);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008870 // gray_return_address:
8871
8872 DCHECK_ALIGNED(offset, sizeof(mirror::HeapReference<mirror::Object>));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008873 vixl32::Register ref_reg = RegisterFrom(ref, DataType::Type::kReference);
Vladimir Marko88abba22017-05-03 17:09:25 +01008874 bool narrow = CanEmitNarrowLdr(ref_reg, obj, offset);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008875 vixl32::Register base = obj;
8876 if (offset >= kReferenceLoadMinFarOffset) {
8877 base = RegisterFrom(temp);
8878 DCHECK(!base.Is(kBakerCcEntrypointRegister));
8879 static_assert(IsPowerOfTwo(kReferenceLoadMinFarOffset), "Expecting a power of 2.");
8880 __ Add(base, obj, Operand(offset & ~(kReferenceLoadMinFarOffset - 1u)));
8881 offset &= (kReferenceLoadMinFarOffset - 1u);
Vladimir Marko88abba22017-05-03 17:09:25 +01008882 // Use narrow LDR only for small offsets. Generating narrow encoding LDR for the large
8883 // offsets with `(offset & (kReferenceLoadMinFarOffset - 1u)) < 32u` would most likely
8884 // increase the overall code size when taking the generated thunks into account.
8885 DCHECK(!narrow);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008886 }
8887 UseScratchRegisterScope temps(GetVIXLAssembler());
8888 ExcludeIPAndBakerCcEntrypointRegister(&temps, instruction);
8889 uint32_t custom_data = linker::Thumb2RelativePatcher::EncodeBakerReadBarrierFieldData(
Vladimir Marko88abba22017-05-03 17:09:25 +01008890 base.GetCode(), obj.GetCode(), narrow);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008891 vixl32::Label* bne_label = NewBakerReadBarrierPatch(custom_data);
8892
Roland Levillain5daa4952017-07-03 17:23:56 +01008893 {
8894 vixl::EmissionCheckScope guard(
8895 GetVIXLAssembler(),
8896 (kPoisonHeapReferences ? 5u : 4u) * vixl32::kMaxInstructionSizeInBytes);
8897 vixl32::Label return_address;
8898 EmitAdrCode adr(GetVIXLAssembler(), lr, &return_address);
8899 __ cmp(mr, Operand(0));
8900 EmitPlaceholderBne(this, bne_label);
8901 ptrdiff_t old_offset = GetVIXLAssembler()->GetBuffer()->GetCursorOffset();
8902 __ ldr(EncodingSize(narrow ? Narrow : Wide), ref_reg, MemOperand(base, offset));
8903 if (needs_null_check) {
8904 MaybeRecordImplicitNullCheck(instruction);
Vladimir Marko88abba22017-05-03 17:09:25 +01008905 }
Roland Levillain5daa4952017-07-03 17:23:56 +01008906 // Note: We need a specific width for the unpoisoning NEG.
8907 if (kPoisonHeapReferences) {
8908 if (narrow) {
8909 // The only 16-bit encoding is T1 which sets flags outside IT block (i.e. RSBS, not RSB).
8910 __ rsbs(EncodingSize(Narrow), ref_reg, ref_reg, Operand(0));
8911 } else {
8912 __ rsb(EncodingSize(Wide), ref_reg, ref_reg, Operand(0));
8913 }
8914 }
8915 __ Bind(&return_address);
8916 DCHECK_EQ(old_offset - GetVIXLAssembler()->GetBuffer()->GetCursorOffset(),
8917 narrow ? BAKER_MARK_INTROSPECTION_FIELD_LDR_NARROW_OFFSET
8918 : BAKER_MARK_INTROSPECTION_FIELD_LDR_WIDE_OFFSET);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008919 }
Roland Levillain5daa4952017-07-03 17:23:56 +01008920 MaybeGenerateMarkingRegisterCheck(/* code */ 19, /* temp_loc */ LocationFrom(ip));
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008921 return;
8922 }
8923
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008924 // /* HeapReference<Object> */ ref = *(obj + offset)
8925 Location no_index = Location::NoLocation();
8926 ScaleFactor no_scale_factor = TIMES_1;
8927 GenerateReferenceLoadWithBakerReadBarrier(
8928 instruction, ref, obj, offset, no_index, no_scale_factor, temp, needs_null_check);
Roland Levillain6070e882016-11-03 17:51:58 +00008929}
8930
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008931void CodeGeneratorARMVIXL::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
8932 Location ref,
8933 vixl32::Register obj,
8934 uint32_t data_offset,
8935 Location index,
8936 Location temp,
8937 bool needs_null_check) {
8938 DCHECK(kEmitCompilerReadBarrier);
8939 DCHECK(kUseBakerReadBarrier);
8940
8941 static_assert(
8942 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
8943 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008944 ScaleFactor scale_factor = TIMES_4;
8945
8946 if (kBakerReadBarrierLinkTimeThunksEnableForArrays &&
8947 !Runtime::Current()->UseJitCompilation()) {
Roland Levillain6d729a72017-06-30 18:34:01 +01008948 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in the
8949 // Marking Register) to decide whether we need to enter the slow
8950 // path to mark the reference. Then, in the slow path, check the
8951 // gray bit in the lock word of the reference's holder (`obj`) to
8952 // decide whether to mark `ref` or not.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008953 //
8954 // We use link-time generated thunks for the slow path. That thunk checks
8955 // the holder and jumps to the entrypoint if needed. If the holder is not
8956 // gray, it creates a fake dependency and returns to the LDR instruction.
8957 //
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008958 // lr = &gray_return_address;
Roland Levillain6d729a72017-06-30 18:34:01 +01008959 // if (mr) { // Thread::Current()->GetIsGcMarking()
8960 // goto array_thunk<base_reg>(lr)
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008961 // }
8962 // not_gray_return_address:
8963 // // Original reference load. If the offset is too large to fit
8964 // // into LDR, we use an adjusted base register here.
Vladimir Marko88abba22017-05-03 17:09:25 +01008965 // HeapReference<mirror::Object> reference = data[index];
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008966 // gray_return_address:
8967
8968 DCHECK(index.IsValid());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008969 vixl32::Register index_reg = RegisterFrom(index, DataType::Type::kInt32);
8970 vixl32::Register ref_reg = RegisterFrom(ref, DataType::Type::kReference);
8971 vixl32::Register data_reg = RegisterFrom(temp, DataType::Type::kInt32); // Raw pointer.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008972 DCHECK(!data_reg.Is(kBakerCcEntrypointRegister));
8973
8974 UseScratchRegisterScope temps(GetVIXLAssembler());
8975 ExcludeIPAndBakerCcEntrypointRegister(&temps, instruction);
8976 uint32_t custom_data =
8977 linker::Thumb2RelativePatcher::EncodeBakerReadBarrierArrayData(data_reg.GetCode());
8978 vixl32::Label* bne_label = NewBakerReadBarrierPatch(custom_data);
8979
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008980 __ Add(data_reg, obj, Operand(data_offset));
Roland Levillain5daa4952017-07-03 17:23:56 +01008981 {
8982 vixl::EmissionCheckScope guard(
8983 GetVIXLAssembler(),
8984 (kPoisonHeapReferences ? 5u : 4u) * vixl32::kMaxInstructionSizeInBytes);
8985 vixl32::Label return_address;
8986 EmitAdrCode adr(GetVIXLAssembler(), lr, &return_address);
8987 __ cmp(mr, Operand(0));
8988 EmitPlaceholderBne(this, bne_label);
8989 ptrdiff_t old_offset = GetVIXLAssembler()->GetBuffer()->GetCursorOffset();
8990 __ ldr(ref_reg, MemOperand(data_reg, index_reg, vixl32::LSL, scale_factor));
8991 DCHECK(!needs_null_check); // The thunk cannot handle the null check.
8992 // Note: We need a Wide NEG for the unpoisoning.
8993 if (kPoisonHeapReferences) {
8994 __ rsb(EncodingSize(Wide), ref_reg, ref_reg, Operand(0));
8995 }
8996 __ Bind(&return_address);
8997 DCHECK_EQ(old_offset - GetVIXLAssembler()->GetBuffer()->GetCursorOffset(),
8998 BAKER_MARK_INTROSPECTION_ARRAY_LDR_OFFSET);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008999 }
Roland Levillain5daa4952017-07-03 17:23:56 +01009000 MaybeGenerateMarkingRegisterCheck(/* code */ 20, /* temp_loc */ LocationFrom(ip));
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01009001 return;
9002 }
9003
Anton Kirilovedb2ac32016-11-30 15:14:10 +00009004 // /* HeapReference<Object> */ ref =
9005 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Anton Kirilovedb2ac32016-11-30 15:14:10 +00009006 GenerateReferenceLoadWithBakerReadBarrier(
9007 instruction, ref, obj, data_offset, index, scale_factor, temp, needs_null_check);
Roland Levillain6070e882016-11-03 17:51:58 +00009008}
9009
Anton Kirilovedb2ac32016-11-30 15:14:10 +00009010void CodeGeneratorARMVIXL::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
9011 Location ref,
9012 vixl32::Register obj,
9013 uint32_t offset,
9014 Location index,
9015 ScaleFactor scale_factor,
9016 Location temp,
Roland Levillainff487002017-03-07 16:50:01 +00009017 bool needs_null_check) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00009018 DCHECK(kEmitCompilerReadBarrier);
9019 DCHECK(kUseBakerReadBarrier);
9020
Roland Levillain6d729a72017-06-30 18:34:01 +01009021 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in the
9022 // Marking Register) to decide whether we need to enter the slow
9023 // path to mark the reference. Then, in the slow path, check the
9024 // gray bit in the lock word of the reference's holder (`obj`) to
9025 // decide whether to mark `ref` or not.
Anton Kirilovedb2ac32016-11-30 15:14:10 +00009026 //
Roland Levillain6d729a72017-06-30 18:34:01 +01009027 // if (mr) { // Thread::Current()->GetIsGcMarking()
Roland Levillainff487002017-03-07 16:50:01 +00009028 // // Slow path.
9029 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
9030 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
9031 // HeapReference<mirror::Object> ref = *src; // Original reference load.
9032 // bool is_gray = (rb_state == ReadBarrier::GrayState());
9033 // if (is_gray) {
Roland Levillain6d729a72017-06-30 18:34:01 +01009034 // entrypoint = Thread::Current()->pReadBarrierMarkReg ## root.reg()
9035 // ref = entrypoint(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
Roland Levillainff487002017-03-07 16:50:01 +00009036 // }
9037 // } else {
9038 // HeapReference<mirror::Object> ref = *src; // Original reference load.
9039 // }
9040
9041 vixl32::Register temp_reg = RegisterFrom(temp);
9042
9043 // Slow path marking the object `ref` when the GC is marking. The
Roland Levillain6d729a72017-06-30 18:34:01 +01009044 // entrypoint will be loaded by the slow path code.
Roland Levillainff487002017-03-07 16:50:01 +00009045 SlowPathCodeARMVIXL* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01009046 new (GetScopedAllocator()) LoadReferenceWithBakerReadBarrierSlowPathARMVIXL(
Roland Levillain6d729a72017-06-30 18:34:01 +01009047 instruction, ref, obj, offset, index, scale_factor, needs_null_check, temp_reg);
Roland Levillainff487002017-03-07 16:50:01 +00009048 AddSlowPath(slow_path);
9049
Roland Levillain6d729a72017-06-30 18:34:01 +01009050 __ CompareAndBranchIfNonZero(mr, slow_path->GetEntryLabel());
Roland Levillainff487002017-03-07 16:50:01 +00009051 // Fast path: the GC is not marking: just load the reference.
9052 GenerateRawReferenceLoad(instruction, ref, obj, offset, index, scale_factor, needs_null_check);
9053 __ Bind(slow_path->GetExitLabel());
Roland Levillain5daa4952017-07-03 17:23:56 +01009054 MaybeGenerateMarkingRegisterCheck(/* code */ 21);
Roland Levillainff487002017-03-07 16:50:01 +00009055}
9056
9057void CodeGeneratorARMVIXL::UpdateReferenceFieldWithBakerReadBarrier(HInstruction* instruction,
9058 Location ref,
9059 vixl32::Register obj,
9060 Location field_offset,
9061 Location temp,
9062 bool needs_null_check,
9063 vixl32::Register temp2) {
9064 DCHECK(kEmitCompilerReadBarrier);
9065 DCHECK(kUseBakerReadBarrier);
9066
Roland Levillain6d729a72017-06-30 18:34:01 +01009067 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in the
9068 // Marking Register) to decide whether we need to enter the slow
9069 // path to update the reference field within `obj`. Then, in the
9070 // slow path, check the gray bit in the lock word of the reference's
9071 // holder (`obj`) to decide whether to mark `ref` and update the
9072 // field or not.
Roland Levillainff487002017-03-07 16:50:01 +00009073 //
Roland Levillain6d729a72017-06-30 18:34:01 +01009074 // if (mr) { // Thread::Current()->GetIsGcMarking()
Roland Levillainba650a42017-03-06 13:52:32 +00009075 // // Slow path.
Roland Levillain54f869e2017-03-06 13:54:11 +00009076 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
9077 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
Roland Levillainff487002017-03-07 16:50:01 +00009078 // HeapReference<mirror::Object> ref = *(obj + field_offset); // Reference load.
Roland Levillain54f869e2017-03-06 13:54:11 +00009079 // bool is_gray = (rb_state == ReadBarrier::GrayState());
9080 // if (is_gray) {
Roland Levillainff487002017-03-07 16:50:01 +00009081 // old_ref = ref;
Roland Levillain6d729a72017-06-30 18:34:01 +01009082 // entrypoint = Thread::Current()->pReadBarrierMarkReg ## root.reg()
9083 // ref = entrypoint(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
Roland Levillainff487002017-03-07 16:50:01 +00009084 // compareAndSwapObject(obj, field_offset, old_ref, ref);
Roland Levillain54f869e2017-03-06 13:54:11 +00009085 // }
Anton Kirilovedb2ac32016-11-30 15:14:10 +00009086 // }
Anton Kirilovedb2ac32016-11-30 15:14:10 +00009087
Anton Kirilovedb2ac32016-11-30 15:14:10 +00009088 vixl32::Register temp_reg = RegisterFrom(temp);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00009089
Roland Levillainff487002017-03-07 16:50:01 +00009090 // Slow path updating the object reference at address `obj + field_offset`
Roland Levillain6d729a72017-06-30 18:34:01 +01009091 // when the GC is marking. The entrypoint will be loaded by the slow path code.
Vladimir Marko174b2e22017-10-12 13:34:49 +01009092 SlowPathCodeARMVIXL* slow_path =
9093 new (GetScopedAllocator()) LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARMVIXL(
Roland Levillainff487002017-03-07 16:50:01 +00009094 instruction,
9095 ref,
9096 obj,
9097 /* offset */ 0u,
9098 /* index */ field_offset,
9099 /* scale_factor */ ScaleFactor::TIMES_1,
9100 needs_null_check,
9101 temp_reg,
Roland Levillain6d729a72017-06-30 18:34:01 +01009102 temp2);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00009103 AddSlowPath(slow_path);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00009104
Roland Levillain6d729a72017-06-30 18:34:01 +01009105 __ CompareAndBranchIfNonZero(mr, slow_path->GetEntryLabel());
Roland Levillainff487002017-03-07 16:50:01 +00009106 // Fast path: the GC is not marking: nothing to do (the field is
9107 // up-to-date, and we don't need to load the reference).
Anton Kirilovedb2ac32016-11-30 15:14:10 +00009108 __ Bind(slow_path->GetExitLabel());
Roland Levillain5daa4952017-07-03 17:23:56 +01009109 MaybeGenerateMarkingRegisterCheck(/* code */ 22);
Roland Levillain844e6532016-11-03 16:09:47 +00009110}
Scott Wakelingfe885462016-09-22 10:24:38 +01009111
Roland Levillainba650a42017-03-06 13:52:32 +00009112void CodeGeneratorARMVIXL::GenerateRawReferenceLoad(HInstruction* instruction,
9113 Location ref,
9114 vixl::aarch32::Register obj,
9115 uint32_t offset,
9116 Location index,
9117 ScaleFactor scale_factor,
9118 bool needs_null_check) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009119 DataType::Type type = DataType::Type::kReference;
Roland Levillainba650a42017-03-06 13:52:32 +00009120 vixl32::Register ref_reg = RegisterFrom(ref, type);
9121
9122 // If needed, vixl::EmissionCheckScope guards are used to ensure
9123 // that no pools are emitted between the load (macro) instruction
9124 // and MaybeRecordImplicitNullCheck.
9125
Scott Wakelingfe885462016-09-22 10:24:38 +01009126 if (index.IsValid()) {
9127 // Load types involving an "index": ArrayGet,
9128 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
9129 // intrinsics.
Roland Levillainba650a42017-03-06 13:52:32 +00009130 // /* HeapReference<mirror::Object> */ ref = *(obj + offset + (index << scale_factor))
Scott Wakelingfe885462016-09-22 10:24:38 +01009131 if (index.IsConstant()) {
9132 size_t computed_offset =
9133 (Int32ConstantFrom(index) << scale_factor) + offset;
Roland Levillainba650a42017-03-06 13:52:32 +00009134 vixl::EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Scott Wakelingfe885462016-09-22 10:24:38 +01009135 GetAssembler()->LoadFromOffset(kLoadWord, ref_reg, obj, computed_offset);
Roland Levillainba650a42017-03-06 13:52:32 +00009136 if (needs_null_check) {
9137 MaybeRecordImplicitNullCheck(instruction);
9138 }
Scott Wakelingfe885462016-09-22 10:24:38 +01009139 } else {
9140 // Handle the special case of the
9141 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
9142 // intrinsics, which use a register pair as index ("long
9143 // offset"), of which only the low part contains data.
9144 vixl32::Register index_reg = index.IsRegisterPair()
9145 ? LowRegisterFrom(index)
9146 : RegisterFrom(index);
9147 UseScratchRegisterScope temps(GetVIXLAssembler());
Roland Levillainba650a42017-03-06 13:52:32 +00009148 vixl32::Register temp = temps.Acquire();
9149 __ Add(temp, obj, Operand(index_reg, ShiftType::LSL, scale_factor));
9150 {
9151 vixl::EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
9152 GetAssembler()->LoadFromOffset(kLoadWord, ref_reg, temp, offset);
9153 if (needs_null_check) {
9154 MaybeRecordImplicitNullCheck(instruction);
9155 }
9156 }
Scott Wakelingfe885462016-09-22 10:24:38 +01009157 }
9158 } else {
Roland Levillainba650a42017-03-06 13:52:32 +00009159 // /* HeapReference<mirror::Object> */ ref = *(obj + offset)
9160 vixl::EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Scott Wakelingfe885462016-09-22 10:24:38 +01009161 GetAssembler()->LoadFromOffset(kLoadWord, ref_reg, obj, offset);
Roland Levillainba650a42017-03-06 13:52:32 +00009162 if (needs_null_check) {
9163 MaybeRecordImplicitNullCheck(instruction);
9164 }
Scott Wakelingfe885462016-09-22 10:24:38 +01009165 }
9166
Roland Levillain844e6532016-11-03 16:09:47 +00009167 // Object* ref = ref_addr->AsMirrorPtr()
9168 GetAssembler()->MaybeUnpoisonHeapReference(ref_reg);
Roland Levillain844e6532016-11-03 16:09:47 +00009169}
9170
Roland Levillain5daa4952017-07-03 17:23:56 +01009171void CodeGeneratorARMVIXL::MaybeGenerateMarkingRegisterCheck(int code, Location temp_loc) {
9172 // The following condition is a compile-time one, so it does not have a run-time cost.
9173 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier && kIsDebugBuild) {
9174 // The following condition is a run-time one; it is executed after the
9175 // previous compile-time test, to avoid penalizing non-debug builds.
9176 if (GetCompilerOptions().EmitRunTimeChecksInDebugMode()) {
9177 UseScratchRegisterScope temps(GetVIXLAssembler());
9178 vixl32::Register temp = temp_loc.IsValid() ? RegisterFrom(temp_loc) : temps.Acquire();
9179 GetAssembler()->GenerateMarkingRegisterCheck(temp,
9180 kMarkingRegisterCheckBreakCodeBaseCode + code);
9181 }
9182 }
9183}
9184
Anton Kirilovedb2ac32016-11-30 15:14:10 +00009185void CodeGeneratorARMVIXL::GenerateReadBarrierSlow(HInstruction* instruction,
9186 Location out,
9187 Location ref,
9188 Location obj,
9189 uint32_t offset,
9190 Location index) {
9191 DCHECK(kEmitCompilerReadBarrier);
9192
9193 // Insert a slow path based read barrier *after* the reference load.
9194 //
9195 // If heap poisoning is enabled, the unpoisoning of the loaded
9196 // reference will be carried out by the runtime within the slow
9197 // path.
9198 //
9199 // Note that `ref` currently does not get unpoisoned (when heap
9200 // poisoning is enabled), which is alright as the `ref` argument is
9201 // not used by the artReadBarrierSlow entry point.
9202 //
9203 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Vladimir Marko174b2e22017-10-12 13:34:49 +01009204 SlowPathCodeARMVIXL* slow_path = new (GetScopedAllocator())
Anton Kirilovedb2ac32016-11-30 15:14:10 +00009205 ReadBarrierForHeapReferenceSlowPathARMVIXL(instruction, out, ref, obj, offset, index);
9206 AddSlowPath(slow_path);
9207
9208 __ B(slow_path->GetEntryLabel());
9209 __ Bind(slow_path->GetExitLabel());
9210}
9211
9212void CodeGeneratorARMVIXL::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
Artem Serov02d37832016-10-25 15:25:33 +01009213 Location out,
Anton Kirilovedb2ac32016-11-30 15:14:10 +00009214 Location ref,
9215 Location obj,
9216 uint32_t offset,
9217 Location index) {
Artem Serov02d37832016-10-25 15:25:33 +01009218 if (kEmitCompilerReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00009219 // Baker's read barriers shall be handled by the fast path
Roland Levillain9983e302017-07-14 14:34:22 +01009220 // (CodeGeneratorARMVIXL::GenerateReferenceLoadWithBakerReadBarrier).
Artem Serov02d37832016-10-25 15:25:33 +01009221 DCHECK(!kUseBakerReadBarrier);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00009222 // If heap poisoning is enabled, unpoisoning will be taken care of
9223 // by the runtime within the slow path.
9224 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Artem Serov02d37832016-10-25 15:25:33 +01009225 } else if (kPoisonHeapReferences) {
9226 GetAssembler()->UnpoisonHeapReference(RegisterFrom(out));
9227 }
9228}
9229
Anton Kirilovedb2ac32016-11-30 15:14:10 +00009230void CodeGeneratorARMVIXL::GenerateReadBarrierForRootSlow(HInstruction* instruction,
9231 Location out,
9232 Location root) {
9233 DCHECK(kEmitCompilerReadBarrier);
9234
9235 // Insert a slow path based read barrier *after* the GC root load.
9236 //
9237 // Note that GC roots are not affected by heap poisoning, so we do
9238 // not need to do anything special for this here.
9239 SlowPathCodeARMVIXL* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01009240 new (GetScopedAllocator()) ReadBarrierForRootSlowPathARMVIXL(instruction, out, root);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00009241 AddSlowPath(slow_path);
9242
9243 __ B(slow_path->GetEntryLabel());
9244 __ Bind(slow_path->GetExitLabel());
9245}
9246
Artem Serov02d37832016-10-25 15:25:33 +01009247// Check if the desired_dispatch_info is supported. If it is, return it,
9248// otherwise return a fall-back info that should be used instead.
9249HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARMVIXL::GetSupportedInvokeStaticOrDirectDispatch(
Artem Serovd4cc5b22016-11-04 11:19:09 +00009250 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +00009251 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Nicolas Geoffraye807ff72017-01-23 09:03:12 +00009252 return desired_dispatch_info;
Artem Serov02d37832016-10-25 15:25:33 +01009253}
9254
Scott Wakelingfe885462016-09-22 10:24:38 +01009255vixl32::Register CodeGeneratorARMVIXL::GetInvokeStaticOrDirectExtraParameter(
9256 HInvokeStaticOrDirect* invoke, vixl32::Register temp) {
9257 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
9258 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
9259 if (!invoke->GetLocations()->Intrinsified()) {
9260 return RegisterFrom(location);
9261 }
9262 // For intrinsics we allow any location, so it may be on the stack.
9263 if (!location.IsRegister()) {
9264 GetAssembler()->LoadFromOffset(kLoadWord, temp, sp, location.GetStackIndex());
9265 return temp;
9266 }
9267 // For register locations, check if the register was saved. If so, get it from the stack.
9268 // Note: There is a chance that the register was saved but not overwritten, so we could
9269 // save one load. However, since this is just an intrinsic slow path we prefer this
9270 // simple and more robust approach rather that trying to determine if that's the case.
9271 SlowPathCode* slow_path = GetCurrentSlowPath();
Scott Wakelingd5cd4972017-02-03 11:38:35 +00009272 if (slow_path != nullptr && slow_path->IsCoreRegisterSaved(RegisterFrom(location).GetCode())) {
Scott Wakelingfe885462016-09-22 10:24:38 +01009273 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(RegisterFrom(location).GetCode());
9274 GetAssembler()->LoadFromOffset(kLoadWord, temp, sp, stack_offset);
9275 return temp;
9276 }
9277 return RegisterFrom(location);
9278}
9279
Vladimir Markod254f5c2017-06-02 15:18:36 +00009280void CodeGeneratorARMVIXL::GenerateStaticOrDirectCall(
Vladimir Markoe7197bf2017-06-02 17:00:23 +01009281 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00009282 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
Scott Wakelingfe885462016-09-22 10:24:38 +01009283 switch (invoke->GetMethodLoadKind()) {
9284 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
9285 uint32_t offset =
9286 GetThreadOffset<kArmPointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
9287 // temp = thread->string_init_entrypoint
Artem Serovd4cc5b22016-11-04 11:19:09 +00009288 GetAssembler()->LoadFromOffset(kLoadWord, RegisterFrom(temp), tr, offset);
9289 break;
9290 }
9291 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
9292 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
9293 break;
Vladimir Marko65979462017-05-19 17:25:12 +01009294 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative: {
9295 DCHECK(GetCompilerOptions().IsBootImage());
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009296 PcRelativePatchInfo* labels = NewBootImageMethodPatch(invoke->GetTargetMethod());
Vladimir Marko65979462017-05-19 17:25:12 +01009297 vixl32::Register temp_reg = RegisterFrom(temp);
9298 EmitMovwMovtPlaceholder(labels, temp_reg);
9299 break;
9300 }
Artem Serovd4cc5b22016-11-04 11:19:09 +00009301 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
9302 __ Mov(RegisterFrom(temp), Operand::From(invoke->GetMethodAddress()));
9303 break;
Vladimir Markob066d432018-01-03 13:14:37 +00009304 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageRelRo: {
Vladimir Markoe47f60c2018-02-21 13:43:28 +00009305 uint32_t boot_image_offset = GetBootImageOffset(invoke);
Vladimir Markob066d432018-01-03 13:14:37 +00009306 PcRelativePatchInfo* labels = NewBootImageRelRoPatch(boot_image_offset);
9307 vixl32::Register temp_reg = RegisterFrom(temp);
9308 EmitMovwMovtPlaceholder(labels, temp_reg);
9309 GetAssembler()->LoadFromOffset(kLoadWord, temp_reg, temp_reg, /* offset*/ 0);
9310 break;
9311 }
Vladimir Marko0eb882b2017-05-15 13:39:18 +01009312 case HInvokeStaticOrDirect::MethodLoadKind::kBssEntry: {
9313 PcRelativePatchInfo* labels = NewMethodBssEntryPatch(
9314 MethodReference(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex()));
9315 vixl32::Register temp_reg = RegisterFrom(temp);
9316 EmitMovwMovtPlaceholder(labels, temp_reg);
9317 GetAssembler()->LoadFromOffset(kLoadWord, temp_reg, temp_reg, /* offset*/ 0);
Scott Wakelingfe885462016-09-22 10:24:38 +01009318 break;
9319 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01009320 case HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall: {
9321 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
9322 return; // No code pointer retrieval; the runtime performs the call directly.
Scott Wakelingfe885462016-09-22 10:24:38 +01009323 }
Scott Wakelingfe885462016-09-22 10:24:38 +01009324 }
9325
Artem Serovd4cc5b22016-11-04 11:19:09 +00009326 switch (invoke->GetCodePtrLocation()) {
9327 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Vladimir Markoe7197bf2017-06-02 17:00:23 +01009328 {
9329 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
9330 ExactAssemblyScope aas(GetVIXLAssembler(),
9331 vixl32::k32BitT32InstructionSizeInBytes,
9332 CodeBufferCheckScope::kMaximumSize);
9333 __ bl(GetFrameEntryLabel());
9334 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
9335 }
Artem Serovd4cc5b22016-11-04 11:19:09 +00009336 break;
Artem Serovd4cc5b22016-11-04 11:19:09 +00009337 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
9338 // LR = callee_method->entry_point_from_quick_compiled_code_
9339 GetAssembler()->LoadFromOffset(
9340 kLoadWord,
9341 lr,
9342 RegisterFrom(callee_method),
9343 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize).Int32Value());
Alexandre Rames374ddf32016-11-04 10:40:49 +00009344 {
Vladimir Markoe7197bf2017-06-02 17:00:23 +01009345 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
Alexandre Rames374ddf32016-11-04 10:40:49 +00009346 // 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 +00009347 ExactAssemblyScope aas(GetVIXLAssembler(),
9348 vixl32::k16BitT32InstructionSizeInBytes,
9349 CodeBufferCheckScope::kExactSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00009350 // LR()
9351 __ blx(lr);
Vladimir Markoe7197bf2017-06-02 17:00:23 +01009352 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Alexandre Rames374ddf32016-11-04 10:40:49 +00009353 }
Artem Serovd4cc5b22016-11-04 11:19:09 +00009354 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01009355 }
9356
Scott Wakelingfe885462016-09-22 10:24:38 +01009357 DCHECK(!IsLeafMethod());
9358}
9359
Vladimir Markoe7197bf2017-06-02 17:00:23 +01009360void CodeGeneratorARMVIXL::GenerateVirtualCall(
9361 HInvokeVirtual* invoke, Location temp_location, SlowPathCode* slow_path) {
Scott Wakelingfe885462016-09-22 10:24:38 +01009362 vixl32::Register temp = RegisterFrom(temp_location);
9363 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
9364 invoke->GetVTableIndex(), kArmPointerSize).Uint32Value();
9365
9366 // Use the calling convention instead of the location of the receiver, as
9367 // intrinsics may have put the receiver in a different register. In the intrinsics
9368 // slow path, the arguments have been moved to the right place, so here we are
9369 // guaranteed that the receiver is the first register of the calling convention.
9370 InvokeDexCallingConventionARMVIXL calling_convention;
9371 vixl32::Register receiver = calling_convention.GetRegisterAt(0);
9372 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Alexandre Rames374ddf32016-11-04 10:40:49 +00009373 {
9374 // Make sure the pc is recorded immediately after the `ldr` instruction.
Artem Serov0fb37192016-12-06 18:13:40 +00009375 ExactAssemblyScope aas(GetVIXLAssembler(),
9376 vixl32::kMaxInstructionSizeInBytes,
9377 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00009378 // /* HeapReference<Class> */ temp = receiver->klass_
9379 __ ldr(temp, MemOperand(receiver, class_offset));
9380 MaybeRecordImplicitNullCheck(invoke);
9381 }
Scott Wakelingfe885462016-09-22 10:24:38 +01009382 // Instead of simply (possibly) unpoisoning `temp` here, we should
9383 // emit a read barrier for the previous class reference load.
9384 // However this is not required in practice, as this is an
9385 // intermediate/temporary reference and because the current
9386 // concurrent copying collector keeps the from-space memory
9387 // intact/accessible until the end of the marking phase (the
9388 // concurrent copying collector may not in the future).
9389 GetAssembler()->MaybeUnpoisonHeapReference(temp);
9390
9391 // temp = temp->GetMethodAt(method_offset);
9392 uint32_t entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(
9393 kArmPointerSize).Int32Value();
9394 GetAssembler()->LoadFromOffset(kLoadWord, temp, temp, method_offset);
9395 // LR = temp->GetEntryPoint();
9396 GetAssembler()->LoadFromOffset(kLoadWord, lr, temp, entry_point);
Vladimir Markoe7197bf2017-06-02 17:00:23 +01009397 {
9398 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
9399 // blx in T32 has only 16bit encoding that's why a stricter check for the scope is used.
9400 ExactAssemblyScope aas(GetVIXLAssembler(),
9401 vixl32::k16BitT32InstructionSizeInBytes,
9402 CodeBufferCheckScope::kExactSize);
9403 // LR();
9404 __ blx(lr);
9405 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
9406 }
Scott Wakelingfe885462016-09-22 10:24:38 +01009407}
9408
Vladimir Markob066d432018-01-03 13:14:37 +00009409CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewBootImageRelRoPatch(
9410 uint32_t boot_image_offset) {
9411 return NewPcRelativePatch(/* dex_file */ nullptr,
9412 boot_image_offset,
9413 &boot_image_method_patches_);
9414}
9415
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009416CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewBootImageMethodPatch(
Vladimir Marko65979462017-05-19 17:25:12 +01009417 MethodReference target_method) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009418 return NewPcRelativePatch(
9419 target_method.dex_file, target_method.index, &boot_image_method_patches_);
Artem Serovd4cc5b22016-11-04 11:19:09 +00009420}
9421
Vladimir Marko0eb882b2017-05-15 13:39:18 +01009422CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewMethodBssEntryPatch(
9423 MethodReference target_method) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009424 return NewPcRelativePatch(
9425 target_method.dex_file, target_method.index, &method_bss_entry_patches_);
Vladimir Marko0eb882b2017-05-15 13:39:18 +01009426}
9427
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009428CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewBootImageTypePatch(
Artem Serovd4cc5b22016-11-04 11:19:09 +00009429 const DexFile& dex_file, dex::TypeIndex type_index) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009430 return NewPcRelativePatch(&dex_file, type_index.index_, &boot_image_type_patches_);
Artem Serovd4cc5b22016-11-04 11:19:09 +00009431}
9432
Vladimir Marko1998cd02017-01-13 13:02:58 +00009433CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewTypeBssEntryPatch(
9434 const DexFile& dex_file, dex::TypeIndex type_index) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009435 return NewPcRelativePatch(&dex_file, type_index.index_, &type_bss_entry_patches_);
Vladimir Marko1998cd02017-01-13 13:02:58 +00009436}
9437
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009438CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewBootImageStringPatch(
Vladimir Marko65979462017-05-19 17:25:12 +01009439 const DexFile& dex_file, dex::StringIndex string_index) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009440 return NewPcRelativePatch(&dex_file, string_index.index_, &boot_image_string_patches_);
Vladimir Marko65979462017-05-19 17:25:12 +01009441}
9442
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01009443CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewStringBssEntryPatch(
9444 const DexFile& dex_file, dex::StringIndex string_index) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009445 return NewPcRelativePatch(&dex_file, string_index.index_, &string_bss_entry_patches_);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01009446}
9447
Artem Serovd4cc5b22016-11-04 11:19:09 +00009448CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewPcRelativePatch(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009449 const DexFile* dex_file, uint32_t offset_or_index, ArenaDeque<PcRelativePatchInfo>* patches) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00009450 patches->emplace_back(dex_file, offset_or_index);
9451 return &patches->back();
9452}
9453
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01009454vixl::aarch32::Label* CodeGeneratorARMVIXL::NewBakerReadBarrierPatch(uint32_t custom_data) {
9455 baker_read_barrier_patches_.emplace_back(custom_data);
9456 return &baker_read_barrier_patches_.back().label;
9457}
9458
Artem Serovc5fcb442016-12-02 19:19:58 +00009459VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateBootImageAddressLiteral(uint32_t address) {
Richard Uhlerc52f3032017-03-02 13:45:45 +00009460 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), &uint32_literals_);
Artem Serovc5fcb442016-12-02 19:19:58 +00009461}
9462
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00009463VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateJitStringLiteral(
9464 const DexFile& dex_file,
9465 dex::StringIndex string_index,
9466 Handle<mirror::String> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01009467 ReserveJitStringRoot(StringReference(&dex_file, string_index), handle);
Artem Serovc5fcb442016-12-02 19:19:58 +00009468 return jit_string_patches_.GetOrCreate(
9469 StringReference(&dex_file, string_index),
9470 [this]() {
9471 return GetAssembler()->CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u);
9472 });
9473}
9474
9475VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateJitClassLiteral(const DexFile& dex_file,
9476 dex::TypeIndex type_index,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00009477 Handle<mirror::Class> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01009478 ReserveJitClassRoot(TypeReference(&dex_file, type_index), handle);
Artem Serovc5fcb442016-12-02 19:19:58 +00009479 return jit_class_patches_.GetOrCreate(
9480 TypeReference(&dex_file, type_index),
9481 [this]() {
9482 return GetAssembler()->CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u);
9483 });
9484}
9485
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01009486template <linker::LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
Artem Serovd4cc5b22016-11-04 11:19:09 +00009487inline void CodeGeneratorARMVIXL::EmitPcRelativeLinkerPatches(
9488 const ArenaDeque<PcRelativePatchInfo>& infos,
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01009489 ArenaVector<linker::LinkerPatch>* linker_patches) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00009490 for (const PcRelativePatchInfo& info : infos) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009491 const DexFile* dex_file = info.target_dex_file;
Artem Serovd4cc5b22016-11-04 11:19:09 +00009492 size_t offset_or_index = info.offset_or_index;
9493 DCHECK(info.add_pc_label.IsBound());
9494 uint32_t add_pc_offset = dchecked_integral_cast<uint32_t>(info.add_pc_label.GetLocation());
9495 // Add MOVW patch.
9496 DCHECK(info.movw_label.IsBound());
9497 uint32_t movw_offset = dchecked_integral_cast<uint32_t>(info.movw_label.GetLocation());
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009498 linker_patches->push_back(Factory(movw_offset, dex_file, add_pc_offset, offset_or_index));
Artem Serovd4cc5b22016-11-04 11:19:09 +00009499 // Add MOVT patch.
9500 DCHECK(info.movt_label.IsBound());
9501 uint32_t movt_offset = dchecked_integral_cast<uint32_t>(info.movt_label.GetLocation());
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009502 linker_patches->push_back(Factory(movt_offset, dex_file, add_pc_offset, offset_or_index));
Artem Serovd4cc5b22016-11-04 11:19:09 +00009503 }
9504}
9505
Vladimir Markob066d432018-01-03 13:14:37 +00009506linker::LinkerPatch DataBimgRelRoPatchAdapter(size_t literal_offset,
9507 const DexFile* target_dex_file,
9508 uint32_t pc_insn_offset,
9509 uint32_t boot_image_offset) {
9510 DCHECK(target_dex_file == nullptr); // Unused for DataBimgRelRoPatch(), should be null.
9511 return linker::LinkerPatch::DataBimgRelRoPatch(literal_offset, pc_insn_offset, boot_image_offset);
9512}
9513
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01009514void CodeGeneratorARMVIXL::EmitLinkerPatches(ArenaVector<linker::LinkerPatch>* linker_patches) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00009515 DCHECK(linker_patches->empty());
9516 size_t size =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009517 /* MOVW+MOVT for each entry */ 2u * boot_image_method_patches_.size() +
Vladimir Marko0eb882b2017-05-15 13:39:18 +01009518 /* MOVW+MOVT for each entry */ 2u * method_bss_entry_patches_.size() +
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009519 /* MOVW+MOVT for each entry */ 2u * boot_image_type_patches_.size() +
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01009520 /* MOVW+MOVT for each entry */ 2u * type_bss_entry_patches_.size() +
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009521 /* MOVW+MOVT for each entry */ 2u * boot_image_string_patches_.size() +
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01009522 /* MOVW+MOVT for each entry */ 2u * string_bss_entry_patches_.size() +
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01009523 baker_read_barrier_patches_.size();
Artem Serovd4cc5b22016-11-04 11:19:09 +00009524 linker_patches->reserve(size);
Vladimir Marko65979462017-05-19 17:25:12 +01009525 if (GetCompilerOptions().IsBootImage()) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01009526 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeMethodPatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009527 boot_image_method_patches_, linker_patches);
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01009528 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeTypePatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009529 boot_image_type_patches_, linker_patches);
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01009530 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeStringPatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009531 boot_image_string_patches_, linker_patches);
Vladimir Marko65979462017-05-19 17:25:12 +01009532 } else {
Vladimir Markob066d432018-01-03 13:14:37 +00009533 EmitPcRelativeLinkerPatches<DataBimgRelRoPatchAdapter>(
9534 boot_image_method_patches_, linker_patches);
Vladimir Markoe47f60c2018-02-21 13:43:28 +00009535 DCHECK(boot_image_type_patches_.empty());
9536 DCHECK(boot_image_string_patches_.empty());
Artem Serovd4cc5b22016-11-04 11:19:09 +00009537 }
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01009538 EmitPcRelativeLinkerPatches<linker::LinkerPatch::MethodBssEntryPatch>(
9539 method_bss_entry_patches_, linker_patches);
9540 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeBssEntryPatch>(
9541 type_bss_entry_patches_, linker_patches);
9542 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringBssEntryPatch>(
9543 string_bss_entry_patches_, linker_patches);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01009544 for (const BakerReadBarrierPatchInfo& info : baker_read_barrier_patches_) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01009545 linker_patches->push_back(linker::LinkerPatch::BakerReadBarrierBranchPatch(
9546 info.label.GetLocation(), info.custom_data));
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01009547 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00009548 DCHECK_EQ(size, linker_patches->size());
Artem Serovc5fcb442016-12-02 19:19:58 +00009549}
9550
9551VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateUint32Literal(
9552 uint32_t value,
9553 Uint32ToLiteralMap* map) {
9554 return map->GetOrCreate(
9555 value,
9556 [this, value]() {
9557 return GetAssembler()->CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ value);
9558 });
9559}
9560
Artem Serov2bbc9532016-10-21 11:51:50 +01009561void LocationsBuilderARMVIXL::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
9562 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01009563 new (GetGraph()->GetAllocator()) LocationSummary(instr, LocationSummary::kNoCall);
Artem Serov2bbc9532016-10-21 11:51:50 +01009564 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
9565 Location::RequiresRegister());
9566 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
9567 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
9568 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
9569}
9570
9571void InstructionCodeGeneratorARMVIXL::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
9572 vixl32::Register res = OutputRegister(instr);
9573 vixl32::Register accumulator =
9574 InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
9575 vixl32::Register mul_left =
9576 InputRegisterAt(instr, HMultiplyAccumulate::kInputMulLeftIndex);
9577 vixl32::Register mul_right =
9578 InputRegisterAt(instr, HMultiplyAccumulate::kInputMulRightIndex);
9579
9580 if (instr->GetOpKind() == HInstruction::kAdd) {
9581 __ Mla(res, mul_left, mul_right, accumulator);
9582 } else {
9583 __ Mls(res, mul_left, mul_right, accumulator);
9584 }
9585}
9586
Artem Serov551b28f2016-10-18 19:11:30 +01009587void LocationsBuilderARMVIXL::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
9588 // Nothing to do, this should be removed during prepare for register allocator.
9589 LOG(FATAL) << "Unreachable";
9590}
9591
9592void InstructionCodeGeneratorARMVIXL::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
9593 // Nothing to do, this should be removed during prepare for register allocator.
9594 LOG(FATAL) << "Unreachable";
9595}
9596
9597// Simple implementation of packed switch - generate cascaded compare/jumps.
9598void LocationsBuilderARMVIXL::VisitPackedSwitch(HPackedSwitch* switch_instr) {
9599 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01009600 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Artem Serov551b28f2016-10-18 19:11:30 +01009601 locations->SetInAt(0, Location::RequiresRegister());
9602 if (switch_instr->GetNumEntries() > kPackedSwitchCompareJumpThreshold &&
9603 codegen_->GetAssembler()->GetVIXLAssembler()->IsUsingT32()) {
9604 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the table base.
9605 if (switch_instr->GetStartValue() != 0) {
9606 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the bias.
9607 }
9608 }
9609}
9610
9611// TODO(VIXL): Investigate and reach the parity with old arm codegen.
9612void InstructionCodeGeneratorARMVIXL::VisitPackedSwitch(HPackedSwitch* switch_instr) {
9613 int32_t lower_bound = switch_instr->GetStartValue();
9614 uint32_t num_entries = switch_instr->GetNumEntries();
9615 LocationSummary* locations = switch_instr->GetLocations();
9616 vixl32::Register value_reg = InputRegisterAt(switch_instr, 0);
9617 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
9618
9619 if (num_entries <= kPackedSwitchCompareJumpThreshold ||
9620 !codegen_->GetAssembler()->GetVIXLAssembler()->IsUsingT32()) {
9621 // Create a series of compare/jumps.
Anton Kirilovedb2ac32016-11-30 15:14:10 +00009622 UseScratchRegisterScope temps(GetVIXLAssembler());
Artem Serov551b28f2016-10-18 19:11:30 +01009623 vixl32::Register temp_reg = temps.Acquire();
9624 // Note: It is fine for the below AddConstantSetFlags() using IP register to temporarily store
9625 // the immediate, because IP is used as the destination register. For the other
9626 // AddConstantSetFlags() and GenerateCompareWithImmediate(), the immediate values are constant,
9627 // and they can be encoded in the instruction without making use of IP register.
9628 __ Adds(temp_reg, value_reg, -lower_bound);
9629
9630 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
9631 // Jump to successors[0] if value == lower_bound.
9632 __ B(eq, codegen_->GetLabelOf(successors[0]));
9633 int32_t last_index = 0;
9634 for (; num_entries - last_index > 2; last_index += 2) {
9635 __ Adds(temp_reg, temp_reg, -2);
9636 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
9637 __ B(lo, codegen_->GetLabelOf(successors[last_index + 1]));
9638 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
9639 __ B(eq, codegen_->GetLabelOf(successors[last_index + 2]));
9640 }
9641 if (num_entries - last_index == 2) {
9642 // The last missing case_value.
9643 __ Cmp(temp_reg, 1);
9644 __ B(eq, codegen_->GetLabelOf(successors[last_index + 1]));
9645 }
9646
9647 // And the default for any other value.
9648 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
9649 __ B(codegen_->GetLabelOf(default_block));
9650 }
9651 } else {
9652 // Create a table lookup.
9653 vixl32::Register table_base = RegisterFrom(locations->GetTemp(0));
9654
9655 JumpTableARMVIXL* jump_table = codegen_->CreateJumpTable(switch_instr);
9656
9657 // Remove the bias.
9658 vixl32::Register key_reg;
9659 if (lower_bound != 0) {
9660 key_reg = RegisterFrom(locations->GetTemp(1));
9661 __ Sub(key_reg, value_reg, lower_bound);
9662 } else {
9663 key_reg = value_reg;
9664 }
9665
9666 // Check whether the value is in the table, jump to default block if not.
9667 __ Cmp(key_reg, num_entries - 1);
9668 __ B(hi, codegen_->GetLabelOf(default_block));
9669
Anton Kirilovedb2ac32016-11-30 15:14:10 +00009670 UseScratchRegisterScope temps(GetVIXLAssembler());
Artem Serov551b28f2016-10-18 19:11:30 +01009671 vixl32::Register jump_offset = temps.Acquire();
9672
9673 // Load jump offset from the table.
Scott Wakeling86e9d262017-01-18 15:59:24 +00009674 {
9675 const size_t jump_size = switch_instr->GetNumEntries() * sizeof(int32_t);
9676 ExactAssemblyScope aas(GetVIXLAssembler(),
9677 (vixl32::kMaxInstructionSizeInBytes * 4) + jump_size,
9678 CodeBufferCheckScope::kMaximumSize);
9679 __ adr(table_base, jump_table->GetTableStartLabel());
9680 __ ldr(jump_offset, MemOperand(table_base, key_reg, vixl32::LSL, 2));
Artem Serov551b28f2016-10-18 19:11:30 +01009681
Scott Wakeling86e9d262017-01-18 15:59:24 +00009682 // Jump to target block by branching to table_base(pc related) + offset.
9683 vixl32::Register target_address = table_base;
9684 __ add(target_address, table_base, jump_offset);
9685 __ bx(target_address);
Artem Serov09a940d2016-11-11 16:15:11 +00009686
Scott Wakeling86e9d262017-01-18 15:59:24 +00009687 jump_table->EmitTable(codegen_);
9688 }
Artem Serov551b28f2016-10-18 19:11:30 +01009689 }
9690}
9691
Artem Serov02d37832016-10-25 15:25:33 +01009692// Copy the result of a call into the given target.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009693void CodeGeneratorARMVIXL::MoveFromReturnRegister(Location trg, DataType::Type type) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01009694 if (!trg.IsValid()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009695 DCHECK_EQ(type, DataType::Type::kVoid);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01009696 return;
9697 }
9698
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009699 DCHECK_NE(type, DataType::Type::kVoid);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01009700
Artem Serovd4cc5b22016-11-04 11:19:09 +00009701 Location return_loc = InvokeDexCallingConventionVisitorARMVIXL().GetReturnLocation(type);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01009702 if (return_loc.Equals(trg)) {
9703 return;
9704 }
9705
9706 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
9707 // with the last branch.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009708 if (type == DataType::Type::kInt64) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01009709 TODO_VIXL32(FATAL);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009710 } else if (type == DataType::Type::kFloat64) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01009711 TODO_VIXL32(FATAL);
9712 } else {
9713 // Let the parallel move resolver take care of all of this.
Vladimir Markoca6fff82017-10-03 14:49:14 +01009714 HParallelMove parallel_move(GetGraph()->GetAllocator());
Anton Kirilove28d9ae2016-10-25 18:17:23 +01009715 parallel_move.AddMove(return_loc, trg, type, nullptr);
9716 GetMoveResolver()->EmitNativeCode(&parallel_move);
9717 }
Scott Wakelingfe885462016-09-22 10:24:38 +01009718}
9719
xueliang.zhong8d2c4592016-11-23 17:05:25 +00009720void LocationsBuilderARMVIXL::VisitClassTableGet(HClassTableGet* instruction) {
9721 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01009722 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
xueliang.zhong8d2c4592016-11-23 17:05:25 +00009723 locations->SetInAt(0, Location::RequiresRegister());
9724 locations->SetOut(Location::RequiresRegister());
Artem Serov551b28f2016-10-18 19:11:30 +01009725}
9726
xueliang.zhong8d2c4592016-11-23 17:05:25 +00009727void InstructionCodeGeneratorARMVIXL::VisitClassTableGet(HClassTableGet* instruction) {
9728 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
9729 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
9730 instruction->GetIndex(), kArmPointerSize).SizeValue();
9731 GetAssembler()->LoadFromOffset(kLoadWord,
9732 OutputRegister(instruction),
9733 InputRegisterAt(instruction, 0),
9734 method_offset);
9735 } else {
9736 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
9737 instruction->GetIndex(), kArmPointerSize));
9738 GetAssembler()->LoadFromOffset(kLoadWord,
9739 OutputRegister(instruction),
9740 InputRegisterAt(instruction, 0),
9741 mirror::Class::ImtPtrOffset(kArmPointerSize).Uint32Value());
9742 GetAssembler()->LoadFromOffset(kLoadWord,
9743 OutputRegister(instruction),
9744 OutputRegister(instruction),
9745 method_offset);
9746 }
Artem Serov551b28f2016-10-18 19:11:30 +01009747}
9748
Artem Serovc5fcb442016-12-02 19:19:58 +00009749static void PatchJitRootUse(uint8_t* code,
9750 const uint8_t* roots_data,
9751 VIXLUInt32Literal* literal,
9752 uint64_t index_in_table) {
9753 DCHECK(literal->IsBound());
9754 uint32_t literal_offset = literal->GetLocation();
9755 uintptr_t address =
9756 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
9757 uint8_t* data = code + literal_offset;
9758 reinterpret_cast<uint32_t*>(data)[0] = dchecked_integral_cast<uint32_t>(address);
9759}
9760
9761void CodeGeneratorARMVIXL::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
9762 for (const auto& entry : jit_string_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01009763 const StringReference& string_reference = entry.first;
9764 VIXLUInt32Literal* table_entry_literal = entry.second;
Vladimir Marko174b2e22017-10-12 13:34:49 +01009765 uint64_t index_in_table = GetJitStringRootIndex(string_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01009766 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Artem Serovc5fcb442016-12-02 19:19:58 +00009767 }
9768 for (const auto& entry : jit_class_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01009769 const TypeReference& type_reference = entry.first;
9770 VIXLUInt32Literal* table_entry_literal = entry.second;
Vladimir Marko174b2e22017-10-12 13:34:49 +01009771 uint64_t index_in_table = GetJitClassRootIndex(type_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01009772 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Artem Serovc5fcb442016-12-02 19:19:58 +00009773 }
9774}
9775
Artem Serovd4cc5b22016-11-04 11:19:09 +00009776void CodeGeneratorARMVIXL::EmitMovwMovtPlaceholder(
9777 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels,
9778 vixl32::Register out) {
Artem Serov0fb37192016-12-06 18:13:40 +00009779 ExactAssemblyScope aas(GetVIXLAssembler(),
9780 3 * vixl32::kMaxInstructionSizeInBytes,
9781 CodeBufferCheckScope::kMaximumSize);
Artem Serovd4cc5b22016-11-04 11:19:09 +00009782 // TODO(VIXL): Think about using mov instead of movw.
9783 __ bind(&labels->movw_label);
9784 __ movw(out, /* placeholder */ 0u);
9785 __ bind(&labels->movt_label);
9786 __ movt(out, /* placeholder */ 0u);
9787 __ bind(&labels->add_pc_label);
9788 __ add(out, out, pc);
9789}
9790
Scott Wakelingfe885462016-09-22 10:24:38 +01009791#undef __
9792#undef QUICK_ENTRY_POINT
9793#undef TODO_VIXL32
9794
9795} // namespace arm
9796} // namespace art