blob: 6d49b32dbcc5d46ee76d6507e62d2089c2fabd09 [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)),
2357 pc_relative_method_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
2358 method_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
2359 pc_relative_type_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
2360 type_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
2361 pc_relative_string_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
2362 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
4693
Scott Wakelingfe885462016-09-22 10:24:38 +01004694void LocationsBuilderARMVIXL::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Artem Serov657022c2016-11-23 14:19:38 +00004695 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Scott Wakelingfe885462016-09-22 10:24:38 +01004696 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Scott Wakelingfe885462016-09-22 10:24:38 +01004697}
4698
4699void InstructionCodeGeneratorARMVIXL::VisitDivZeroCheck(HDivZeroCheck* instruction) {
4700 DivZeroCheckSlowPathARMVIXL* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01004701 new (codegen_->GetScopedAllocator()) DivZeroCheckSlowPathARMVIXL(instruction);
Scott Wakelingfe885462016-09-22 10:24:38 +01004702 codegen_->AddSlowPath(slow_path);
4703
4704 LocationSummary* locations = instruction->GetLocations();
4705 Location value = locations->InAt(0);
4706
4707 switch (instruction->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004708 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004709 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004710 case DataType::Type::kInt8:
4711 case DataType::Type::kUint16:
4712 case DataType::Type::kInt16:
4713 case DataType::Type::kInt32: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004714 if (value.IsRegister()) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00004715 __ CompareAndBranchIfZero(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
Scott Wakelingfe885462016-09-22 10:24:38 +01004716 } else {
4717 DCHECK(value.IsConstant()) << value;
Anton Kirilov644032c2016-12-06 17:51:43 +00004718 if (Int32ConstantFrom(value) == 0) {
Scott Wakelingfe885462016-09-22 10:24:38 +01004719 __ B(slow_path->GetEntryLabel());
4720 }
4721 }
4722 break;
4723 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004724 case DataType::Type::kInt64: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004725 if (value.IsRegisterPair()) {
4726 UseScratchRegisterScope temps(GetVIXLAssembler());
4727 vixl32::Register temp = temps.Acquire();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004728 __ Orrs(temp, LowRegisterFrom(value), HighRegisterFrom(value));
Scott Wakelingfe885462016-09-22 10:24:38 +01004729 __ B(eq, slow_path->GetEntryLabel());
4730 } else {
4731 DCHECK(value.IsConstant()) << value;
Anton Kirilov644032c2016-12-06 17:51:43 +00004732 if (Int64ConstantFrom(value) == 0) {
Scott Wakelingfe885462016-09-22 10:24:38 +01004733 __ B(slow_path->GetEntryLabel());
4734 }
4735 }
4736 break;
4737 }
4738 default:
4739 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
4740 }
4741}
4742
Artem Serov02109dd2016-09-23 17:17:54 +01004743void InstructionCodeGeneratorARMVIXL::HandleIntegerRotate(HRor* ror) {
4744 LocationSummary* locations = ror->GetLocations();
4745 vixl32::Register in = InputRegisterAt(ror, 0);
4746 Location rhs = locations->InAt(1);
4747 vixl32::Register out = OutputRegister(ror);
4748
4749 if (rhs.IsConstant()) {
4750 // Arm32 and Thumb2 assemblers require a rotation on the interval [1,31],
4751 // so map all rotations to a +ve. equivalent in that range.
4752 // (e.g. left *or* right by -2 bits == 30 bits in the same direction.)
4753 uint32_t rot = CodeGenerator::GetInt32ValueOf(rhs.GetConstant()) & 0x1F;
4754 if (rot) {
4755 // Rotate, mapping left rotations to right equivalents if necessary.
4756 // (e.g. left by 2 bits == right by 30.)
4757 __ Ror(out, in, rot);
4758 } else if (!out.Is(in)) {
4759 __ Mov(out, in);
4760 }
4761 } else {
4762 __ Ror(out, in, RegisterFrom(rhs));
4763 }
4764}
4765
4766// Gain some speed by mapping all Long rotates onto equivalent pairs of Integer
4767// rotates by swapping input regs (effectively rotating by the first 32-bits of
4768// a larger rotation) or flipping direction (thus treating larger right/left
4769// rotations as sub-word sized rotations in the other direction) as appropriate.
4770void InstructionCodeGeneratorARMVIXL::HandleLongRotate(HRor* ror) {
4771 LocationSummary* locations = ror->GetLocations();
4772 vixl32::Register in_reg_lo = LowRegisterFrom(locations->InAt(0));
4773 vixl32::Register in_reg_hi = HighRegisterFrom(locations->InAt(0));
4774 Location rhs = locations->InAt(1);
4775 vixl32::Register out_reg_lo = LowRegisterFrom(locations->Out());
4776 vixl32::Register out_reg_hi = HighRegisterFrom(locations->Out());
4777
4778 if (rhs.IsConstant()) {
4779 uint64_t rot = CodeGenerator::GetInt64ValueOf(rhs.GetConstant());
4780 // Map all rotations to +ve. equivalents on the interval [0,63].
4781 rot &= kMaxLongShiftDistance;
4782 // For rotates over a word in size, 'pre-rotate' by 32-bits to keep rotate
4783 // logic below to a simple pair of binary orr.
4784 // (e.g. 34 bits == in_reg swap + 2 bits right.)
4785 if (rot >= kArmBitsPerWord) {
4786 rot -= kArmBitsPerWord;
4787 std::swap(in_reg_hi, in_reg_lo);
4788 }
4789 // Rotate, or mov to out for zero or word size rotations.
4790 if (rot != 0u) {
Scott Wakelingb77051e2016-11-21 19:46:00 +00004791 __ Lsr(out_reg_hi, in_reg_hi, Operand::From(rot));
Artem Serov02109dd2016-09-23 17:17:54 +01004792 __ Orr(out_reg_hi, out_reg_hi, Operand(in_reg_lo, ShiftType::LSL, kArmBitsPerWord - rot));
Scott Wakelingb77051e2016-11-21 19:46:00 +00004793 __ Lsr(out_reg_lo, in_reg_lo, Operand::From(rot));
Artem Serov02109dd2016-09-23 17:17:54 +01004794 __ Orr(out_reg_lo, out_reg_lo, Operand(in_reg_hi, ShiftType::LSL, kArmBitsPerWord - rot));
4795 } else {
4796 __ Mov(out_reg_lo, in_reg_lo);
4797 __ Mov(out_reg_hi, in_reg_hi);
4798 }
4799 } else {
4800 vixl32::Register shift_right = RegisterFrom(locations->GetTemp(0));
4801 vixl32::Register shift_left = RegisterFrom(locations->GetTemp(1));
4802 vixl32::Label end;
4803 vixl32::Label shift_by_32_plus_shift_right;
Anton Kirilov6f644202017-02-27 18:29:45 +00004804 vixl32::Label* final_label = codegen_->GetFinalLabel(ror, &end);
Artem Serov02109dd2016-09-23 17:17:54 +01004805
4806 __ And(shift_right, RegisterFrom(rhs), 0x1F);
4807 __ Lsrs(shift_left, RegisterFrom(rhs), 6);
Scott Wakelingbffdc702016-12-07 17:46:03 +00004808 __ Rsb(LeaveFlags, shift_left, shift_right, Operand::From(kArmBitsPerWord));
Artem Serov517d9f62016-12-12 15:51:15 +00004809 __ B(cc, &shift_by_32_plus_shift_right, /* far_target */ false);
Artem Serov02109dd2016-09-23 17:17:54 +01004810
4811 // out_reg_hi = (reg_hi << shift_left) | (reg_lo >> shift_right).
4812 // out_reg_lo = (reg_lo << shift_left) | (reg_hi >> shift_right).
4813 __ Lsl(out_reg_hi, in_reg_hi, shift_left);
4814 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
4815 __ Add(out_reg_hi, out_reg_hi, out_reg_lo);
4816 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
4817 __ Lsr(shift_left, in_reg_hi, shift_right);
4818 __ Add(out_reg_lo, out_reg_lo, shift_left);
Anton Kirilov6f644202017-02-27 18:29:45 +00004819 __ B(final_label);
Artem Serov02109dd2016-09-23 17:17:54 +01004820
4821 __ Bind(&shift_by_32_plus_shift_right); // Shift by 32+shift_right.
4822 // out_reg_hi = (reg_hi >> shift_right) | (reg_lo << shift_left).
4823 // out_reg_lo = (reg_lo >> shift_right) | (reg_hi << shift_left).
4824 __ Lsr(out_reg_hi, in_reg_hi, shift_right);
4825 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
4826 __ Add(out_reg_hi, out_reg_hi, out_reg_lo);
4827 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
4828 __ Lsl(shift_right, in_reg_hi, shift_left);
4829 __ Add(out_reg_lo, out_reg_lo, shift_right);
4830
Anton Kirilov6f644202017-02-27 18:29:45 +00004831 if (end.IsReferenced()) {
4832 __ Bind(&end);
4833 }
Artem Serov02109dd2016-09-23 17:17:54 +01004834 }
4835}
4836
4837void LocationsBuilderARMVIXL::VisitRor(HRor* ror) {
4838 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004839 new (GetGraph()->GetAllocator()) LocationSummary(ror, LocationSummary::kNoCall);
Artem Serov02109dd2016-09-23 17:17:54 +01004840 switch (ror->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004841 case DataType::Type::kInt32: {
Artem Serov02109dd2016-09-23 17:17:54 +01004842 locations->SetInAt(0, Location::RequiresRegister());
4843 locations->SetInAt(1, Location::RegisterOrConstant(ror->InputAt(1)));
4844 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4845 break;
4846 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004847 case DataType::Type::kInt64: {
Artem Serov02109dd2016-09-23 17:17:54 +01004848 locations->SetInAt(0, Location::RequiresRegister());
4849 if (ror->InputAt(1)->IsConstant()) {
4850 locations->SetInAt(1, Location::ConstantLocation(ror->InputAt(1)->AsConstant()));
4851 } else {
4852 locations->SetInAt(1, Location::RequiresRegister());
4853 locations->AddTemp(Location::RequiresRegister());
4854 locations->AddTemp(Location::RequiresRegister());
4855 }
4856 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
4857 break;
4858 }
4859 default:
4860 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
4861 }
4862}
4863
4864void InstructionCodeGeneratorARMVIXL::VisitRor(HRor* ror) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004865 DataType::Type type = ror->GetResultType();
Artem Serov02109dd2016-09-23 17:17:54 +01004866 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004867 case DataType::Type::kInt32: {
Artem Serov02109dd2016-09-23 17:17:54 +01004868 HandleIntegerRotate(ror);
4869 break;
4870 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004871 case DataType::Type::kInt64: {
Artem Serov02109dd2016-09-23 17:17:54 +01004872 HandleLongRotate(ror);
4873 break;
4874 }
4875 default:
4876 LOG(FATAL) << "Unexpected operation type " << type;
4877 UNREACHABLE();
4878 }
4879}
4880
Artem Serov02d37832016-10-25 15:25:33 +01004881void LocationsBuilderARMVIXL::HandleShift(HBinaryOperation* op) {
4882 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
4883
4884 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004885 new (GetGraph()->GetAllocator()) LocationSummary(op, LocationSummary::kNoCall);
Artem Serov02d37832016-10-25 15:25:33 +01004886
4887 switch (op->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004888 case DataType::Type::kInt32: {
Artem Serov02d37832016-10-25 15:25:33 +01004889 locations->SetInAt(0, Location::RequiresRegister());
4890 if (op->InputAt(1)->IsConstant()) {
4891 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
4892 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4893 } else {
4894 locations->SetInAt(1, Location::RequiresRegister());
4895 // Make the output overlap, as it will be used to hold the masked
4896 // second input.
4897 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
4898 }
4899 break;
4900 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004901 case DataType::Type::kInt64: {
Artem Serov02d37832016-10-25 15:25:33 +01004902 locations->SetInAt(0, Location::RequiresRegister());
4903 if (op->InputAt(1)->IsConstant()) {
4904 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
4905 // For simplicity, use kOutputOverlap even though we only require that low registers
4906 // don't clash with high registers which the register allocator currently guarantees.
4907 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
4908 } else {
4909 locations->SetInAt(1, Location::RequiresRegister());
4910 locations->AddTemp(Location::RequiresRegister());
4911 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
4912 }
4913 break;
4914 }
4915 default:
4916 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
4917 }
4918}
4919
4920void InstructionCodeGeneratorARMVIXL::HandleShift(HBinaryOperation* op) {
4921 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
4922
4923 LocationSummary* locations = op->GetLocations();
4924 Location out = locations->Out();
4925 Location first = locations->InAt(0);
4926 Location second = locations->InAt(1);
4927
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004928 DataType::Type type = op->GetResultType();
Artem Serov02d37832016-10-25 15:25:33 +01004929 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004930 case DataType::Type::kInt32: {
Artem Serov02d37832016-10-25 15:25:33 +01004931 vixl32::Register out_reg = OutputRegister(op);
4932 vixl32::Register first_reg = InputRegisterAt(op, 0);
4933 if (second.IsRegister()) {
4934 vixl32::Register second_reg = RegisterFrom(second);
4935 // ARM doesn't mask the shift count so we need to do it ourselves.
4936 __ And(out_reg, second_reg, kMaxIntShiftDistance);
4937 if (op->IsShl()) {
4938 __ Lsl(out_reg, first_reg, out_reg);
4939 } else if (op->IsShr()) {
4940 __ Asr(out_reg, first_reg, out_reg);
4941 } else {
4942 __ Lsr(out_reg, first_reg, out_reg);
4943 }
4944 } else {
Anton Kirilov644032c2016-12-06 17:51:43 +00004945 int32_t cst = Int32ConstantFrom(second);
Artem Serov02d37832016-10-25 15:25:33 +01004946 uint32_t shift_value = cst & kMaxIntShiftDistance;
4947 if (shift_value == 0) { // ARM does not support shifting with 0 immediate.
4948 __ Mov(out_reg, first_reg);
4949 } else if (op->IsShl()) {
4950 __ Lsl(out_reg, first_reg, shift_value);
4951 } else if (op->IsShr()) {
4952 __ Asr(out_reg, first_reg, shift_value);
4953 } else {
4954 __ Lsr(out_reg, first_reg, shift_value);
4955 }
4956 }
4957 break;
4958 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004959 case DataType::Type::kInt64: {
Artem Serov02d37832016-10-25 15:25:33 +01004960 vixl32::Register o_h = HighRegisterFrom(out);
4961 vixl32::Register o_l = LowRegisterFrom(out);
4962
4963 vixl32::Register high = HighRegisterFrom(first);
4964 vixl32::Register low = LowRegisterFrom(first);
4965
4966 if (second.IsRegister()) {
4967 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
4968
4969 vixl32::Register second_reg = RegisterFrom(second);
4970
4971 if (op->IsShl()) {
4972 __ And(o_l, second_reg, kMaxLongShiftDistance);
4973 // Shift the high part
4974 __ Lsl(o_h, high, o_l);
4975 // Shift the low part and `or` what overflew on the high part
Scott Wakelingb77051e2016-11-21 19:46:00 +00004976 __ Rsb(temp, o_l, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01004977 __ Lsr(temp, low, temp);
4978 __ Orr(o_h, o_h, temp);
4979 // If the shift is > 32 bits, override the high part
Scott Wakelingb77051e2016-11-21 19:46:00 +00004980 __ Subs(temp, o_l, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01004981 {
Artem Serov0fb37192016-12-06 18:13:40 +00004982 ExactAssemblyScope guard(GetVIXLAssembler(),
4983 2 * vixl32::kMaxInstructionSizeInBytes,
4984 CodeBufferCheckScope::kMaximumSize);
Artem Serov02d37832016-10-25 15:25:33 +01004985 __ it(pl);
4986 __ lsl(pl, o_h, low, temp);
4987 }
4988 // Shift the low part
4989 __ Lsl(o_l, low, o_l);
4990 } else if (op->IsShr()) {
4991 __ And(o_h, second_reg, kMaxLongShiftDistance);
4992 // Shift the low part
4993 __ Lsr(o_l, low, o_h);
4994 // Shift the high part and `or` what underflew on the low part
Scott Wakelingb77051e2016-11-21 19:46:00 +00004995 __ Rsb(temp, o_h, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01004996 __ Lsl(temp, high, temp);
4997 __ Orr(o_l, o_l, temp);
4998 // If the shift is > 32 bits, override the low part
Scott Wakelingb77051e2016-11-21 19:46:00 +00004999 __ Subs(temp, o_h, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01005000 {
Artem Serov0fb37192016-12-06 18:13:40 +00005001 ExactAssemblyScope guard(GetVIXLAssembler(),
5002 2 * vixl32::kMaxInstructionSizeInBytes,
5003 CodeBufferCheckScope::kMaximumSize);
Artem Serov02d37832016-10-25 15:25:33 +01005004 __ it(pl);
5005 __ asr(pl, o_l, high, temp);
5006 }
5007 // Shift the high part
5008 __ Asr(o_h, high, o_h);
5009 } else {
5010 __ And(o_h, second_reg, kMaxLongShiftDistance);
5011 // same as Shr except we use `Lsr`s and not `Asr`s
5012 __ Lsr(o_l, low, o_h);
Scott Wakelingb77051e2016-11-21 19:46:00 +00005013 __ Rsb(temp, o_h, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01005014 __ Lsl(temp, high, temp);
5015 __ Orr(o_l, o_l, temp);
Scott Wakelingb77051e2016-11-21 19:46:00 +00005016 __ Subs(temp, o_h, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01005017 {
Artem Serov0fb37192016-12-06 18:13:40 +00005018 ExactAssemblyScope guard(GetVIXLAssembler(),
5019 2 * vixl32::kMaxInstructionSizeInBytes,
5020 CodeBufferCheckScope::kMaximumSize);
Artem Serov02d37832016-10-25 15:25:33 +01005021 __ it(pl);
5022 __ lsr(pl, o_l, high, temp);
5023 }
5024 __ Lsr(o_h, high, o_h);
5025 }
5026 } else {
5027 // Register allocator doesn't create partial overlap.
5028 DCHECK(!o_l.Is(high));
5029 DCHECK(!o_h.Is(low));
Anton Kirilov644032c2016-12-06 17:51:43 +00005030 int32_t cst = Int32ConstantFrom(second);
Artem Serov02d37832016-10-25 15:25:33 +01005031 uint32_t shift_value = cst & kMaxLongShiftDistance;
5032 if (shift_value > 32) {
5033 if (op->IsShl()) {
5034 __ Lsl(o_h, low, shift_value - 32);
5035 __ Mov(o_l, 0);
5036 } else if (op->IsShr()) {
5037 __ Asr(o_l, high, shift_value - 32);
5038 __ Asr(o_h, high, 31);
5039 } else {
5040 __ Lsr(o_l, high, shift_value - 32);
5041 __ Mov(o_h, 0);
5042 }
5043 } else if (shift_value == 32) {
5044 if (op->IsShl()) {
5045 __ Mov(o_h, low);
5046 __ Mov(o_l, 0);
5047 } else if (op->IsShr()) {
5048 __ Mov(o_l, high);
5049 __ Asr(o_h, high, 31);
5050 } else {
5051 __ Mov(o_l, high);
5052 __ Mov(o_h, 0);
5053 }
5054 } else if (shift_value == 1) {
5055 if (op->IsShl()) {
5056 __ Lsls(o_l, low, 1);
5057 __ Adc(o_h, high, high);
5058 } else if (op->IsShr()) {
5059 __ Asrs(o_h, high, 1);
5060 __ Rrx(o_l, low);
5061 } else {
5062 __ Lsrs(o_h, high, 1);
5063 __ Rrx(o_l, low);
5064 }
5065 } else {
5066 DCHECK(2 <= shift_value && shift_value < 32) << shift_value;
5067 if (op->IsShl()) {
5068 __ Lsl(o_h, high, shift_value);
5069 __ Orr(o_h, o_h, Operand(low, ShiftType::LSR, 32 - shift_value));
5070 __ Lsl(o_l, low, shift_value);
5071 } else if (op->IsShr()) {
5072 __ Lsr(o_l, low, shift_value);
5073 __ Orr(o_l, o_l, Operand(high, ShiftType::LSL, 32 - shift_value));
5074 __ Asr(o_h, high, shift_value);
5075 } else {
5076 __ Lsr(o_l, low, shift_value);
5077 __ Orr(o_l, o_l, Operand(high, ShiftType::LSL, 32 - shift_value));
5078 __ Lsr(o_h, high, shift_value);
5079 }
5080 }
5081 }
5082 break;
5083 }
5084 default:
5085 LOG(FATAL) << "Unexpected operation type " << type;
5086 UNREACHABLE();
5087 }
5088}
5089
5090void LocationsBuilderARMVIXL::VisitShl(HShl* shl) {
5091 HandleShift(shl);
5092}
5093
5094void InstructionCodeGeneratorARMVIXL::VisitShl(HShl* shl) {
5095 HandleShift(shl);
5096}
5097
5098void LocationsBuilderARMVIXL::VisitShr(HShr* shr) {
5099 HandleShift(shr);
5100}
5101
5102void InstructionCodeGeneratorARMVIXL::VisitShr(HShr* shr) {
5103 HandleShift(shr);
5104}
5105
5106void LocationsBuilderARMVIXL::VisitUShr(HUShr* ushr) {
5107 HandleShift(ushr);
5108}
5109
5110void InstructionCodeGeneratorARMVIXL::VisitUShr(HUShr* ushr) {
5111 HandleShift(ushr);
5112}
5113
5114void LocationsBuilderARMVIXL::VisitNewInstance(HNewInstance* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005115 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5116 instruction, LocationSummary::kCallOnMainOnly);
Artem Serov02d37832016-10-25 15:25:33 +01005117 if (instruction->IsStringAlloc()) {
5118 locations->AddTemp(LocationFrom(kMethodRegister));
5119 } else {
5120 InvokeRuntimeCallingConventionARMVIXL calling_convention;
5121 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
Artem Serov02d37832016-10-25 15:25:33 +01005122 }
5123 locations->SetOut(LocationFrom(r0));
5124}
5125
5126void InstructionCodeGeneratorARMVIXL::VisitNewInstance(HNewInstance* instruction) {
5127 // Note: if heap poisoning is enabled, the entry point takes cares
5128 // of poisoning the reference.
5129 if (instruction->IsStringAlloc()) {
5130 // String is allocated through StringFactory. Call NewEmptyString entry point.
5131 vixl32::Register temp = RegisterFrom(instruction->GetLocations()->GetTemp(0));
5132 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize);
5133 GetAssembler()->LoadFromOffset(kLoadWord, temp, tr, QUICK_ENTRY_POINT(pNewEmptyString));
5134 GetAssembler()->LoadFromOffset(kLoadWord, lr, temp, code_offset.Int32Value());
Alexandre Rames374ddf32016-11-04 10:40:49 +00005135 // 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 +00005136 ExactAssemblyScope aas(GetVIXLAssembler(),
5137 vixl32::k16BitT32InstructionSizeInBytes,
5138 CodeBufferCheckScope::kExactSize);
Artem Serov02d37832016-10-25 15:25:33 +01005139 __ blx(lr);
5140 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
5141 } else {
5142 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00005143 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
Artem Serov02d37832016-10-25 15:25:33 +01005144 }
Roland Levillain5daa4952017-07-03 17:23:56 +01005145 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 10);
Artem Serov02d37832016-10-25 15:25:33 +01005146}
5147
5148void LocationsBuilderARMVIXL::VisitNewArray(HNewArray* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005149 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5150 instruction, LocationSummary::kCallOnMainOnly);
Artem Serov02d37832016-10-25 15:25:33 +01005151 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Artem Serov02d37832016-10-25 15:25:33 +01005152 locations->SetOut(LocationFrom(r0));
Nicolas Geoffray8c7c4f12017-01-26 10:13:11 +00005153 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5154 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
Artem Serov02d37832016-10-25 15:25:33 +01005155}
5156
5157void InstructionCodeGeneratorARMVIXL::VisitNewArray(HNewArray* instruction) {
Artem Serov02d37832016-10-25 15:25:33 +01005158 // Note: if heap poisoning is enabled, the entry point takes cares
5159 // of poisoning the reference.
Artem Serov7b3672e2017-02-03 17:30:34 +00005160 QuickEntrypointEnum entrypoint =
5161 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
5162 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005163 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Artem Serov7b3672e2017-02-03 17:30:34 +00005164 DCHECK(!codegen_->IsLeafMethod());
Roland Levillain5daa4952017-07-03 17:23:56 +01005165 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 11);
Artem Serov02d37832016-10-25 15:25:33 +01005166}
5167
5168void LocationsBuilderARMVIXL::VisitParameterValue(HParameterValue* instruction) {
5169 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005170 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Artem Serov02d37832016-10-25 15:25:33 +01005171 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
5172 if (location.IsStackSlot()) {
5173 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5174 } else if (location.IsDoubleStackSlot()) {
5175 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5176 }
5177 locations->SetOut(location);
5178}
5179
5180void InstructionCodeGeneratorARMVIXL::VisitParameterValue(
5181 HParameterValue* instruction ATTRIBUTE_UNUSED) {
5182 // Nothing to do, the parameter is already at its location.
5183}
5184
5185void LocationsBuilderARMVIXL::VisitCurrentMethod(HCurrentMethod* instruction) {
5186 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005187 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Artem Serov02d37832016-10-25 15:25:33 +01005188 locations->SetOut(LocationFrom(kMethodRegister));
5189}
5190
5191void InstructionCodeGeneratorARMVIXL::VisitCurrentMethod(
5192 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
5193 // Nothing to do, the method is already at its location.
5194}
5195
5196void LocationsBuilderARMVIXL::VisitNot(HNot* not_) {
5197 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005198 new (GetGraph()->GetAllocator()) LocationSummary(not_, LocationSummary::kNoCall);
Artem Serov02d37832016-10-25 15:25:33 +01005199 locations->SetInAt(0, Location::RequiresRegister());
5200 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5201}
5202
5203void InstructionCodeGeneratorARMVIXL::VisitNot(HNot* not_) {
5204 LocationSummary* locations = not_->GetLocations();
5205 Location out = locations->Out();
5206 Location in = locations->InAt(0);
5207 switch (not_->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005208 case DataType::Type::kInt32:
Artem Serov02d37832016-10-25 15:25:33 +01005209 __ Mvn(OutputRegister(not_), InputRegisterAt(not_, 0));
5210 break;
5211
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005212 case DataType::Type::kInt64:
Artem Serov02d37832016-10-25 15:25:33 +01005213 __ Mvn(LowRegisterFrom(out), LowRegisterFrom(in));
5214 __ Mvn(HighRegisterFrom(out), HighRegisterFrom(in));
5215 break;
5216
5217 default:
5218 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
5219 }
5220}
5221
Scott Wakelingc34dba72016-10-03 10:14:44 +01005222void LocationsBuilderARMVIXL::VisitBooleanNot(HBooleanNot* bool_not) {
5223 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005224 new (GetGraph()->GetAllocator()) LocationSummary(bool_not, LocationSummary::kNoCall);
Scott Wakelingc34dba72016-10-03 10:14:44 +01005225 locations->SetInAt(0, Location::RequiresRegister());
5226 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5227}
5228
5229void InstructionCodeGeneratorARMVIXL::VisitBooleanNot(HBooleanNot* bool_not) {
5230 __ Eor(OutputRegister(bool_not), InputRegister(bool_not), 1);
5231}
5232
Artem Serov02d37832016-10-25 15:25:33 +01005233void LocationsBuilderARMVIXL::VisitCompare(HCompare* compare) {
5234 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005235 new (GetGraph()->GetAllocator()) LocationSummary(compare, LocationSummary::kNoCall);
Artem Serov02d37832016-10-25 15:25:33 +01005236 switch (compare->InputAt(0)->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005237 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005238 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005239 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005240 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005241 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005242 case DataType::Type::kInt32:
5243 case DataType::Type::kInt64: {
Artem Serov02d37832016-10-25 15:25:33 +01005244 locations->SetInAt(0, Location::RequiresRegister());
5245 locations->SetInAt(1, Location::RequiresRegister());
5246 // Output overlaps because it is written before doing the low comparison.
5247 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
5248 break;
5249 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005250 case DataType::Type::kFloat32:
5251 case DataType::Type::kFloat64: {
Artem Serov02d37832016-10-25 15:25:33 +01005252 locations->SetInAt(0, Location::RequiresFpuRegister());
5253 locations->SetInAt(1, ArithmeticZeroOrFpuRegister(compare->InputAt(1)));
5254 locations->SetOut(Location::RequiresRegister());
5255 break;
5256 }
5257 default:
5258 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
5259 }
5260}
5261
5262void InstructionCodeGeneratorARMVIXL::VisitCompare(HCompare* compare) {
5263 LocationSummary* locations = compare->GetLocations();
5264 vixl32::Register out = OutputRegister(compare);
5265 Location left = locations->InAt(0);
5266 Location right = locations->InAt(1);
5267
5268 vixl32::Label less, greater, done;
Anton Kirilov6f644202017-02-27 18:29:45 +00005269 vixl32::Label* final_label = codegen_->GetFinalLabel(compare, &done);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005270 DataType::Type type = compare->InputAt(0)->GetType();
Vladimir Marko33bff252017-11-01 14:35:42 +00005271 vixl32::Condition less_cond = vixl32::Condition::None();
Artem Serov02d37832016-10-25 15:25:33 +01005272 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005273 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005274 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005275 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005276 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005277 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005278 case DataType::Type::kInt32: {
Artem Serov02d37832016-10-25 15:25:33 +01005279 // Emit move to `out` before the `Cmp`, as `Mov` might affect the status flags.
5280 __ Mov(out, 0);
5281 __ Cmp(RegisterFrom(left), RegisterFrom(right)); // Signed compare.
5282 less_cond = lt;
5283 break;
5284 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005285 case DataType::Type::kInt64: {
Artem Serov02d37832016-10-25 15:25:33 +01005286 __ Cmp(HighRegisterFrom(left), HighRegisterFrom(right)); // Signed compare.
Artem Serov517d9f62016-12-12 15:51:15 +00005287 __ B(lt, &less, /* far_target */ false);
5288 __ B(gt, &greater, /* far_target */ false);
Artem Serov02d37832016-10-25 15:25:33 +01005289 // Emit move to `out` before the last `Cmp`, as `Mov` might affect the status flags.
5290 __ Mov(out, 0);
5291 __ Cmp(LowRegisterFrom(left), LowRegisterFrom(right)); // Unsigned compare.
5292 less_cond = lo;
5293 break;
5294 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005295 case DataType::Type::kFloat32:
5296 case DataType::Type::kFloat64: {
Artem Serov02d37832016-10-25 15:25:33 +01005297 __ Mov(out, 0);
Donghui Bai426b49c2016-11-08 14:55:38 +08005298 GenerateVcmp(compare, codegen_);
Artem Serov02d37832016-10-25 15:25:33 +01005299 // To branch on the FP compare result we transfer FPSCR to APSR (encoded as PC in VMRS).
5300 __ Vmrs(RegisterOrAPSR_nzcv(kPcCode), FPSCR);
5301 less_cond = ARMFPCondition(kCondLT, compare->IsGtBias());
5302 break;
5303 }
5304 default:
5305 LOG(FATAL) << "Unexpected compare type " << type;
5306 UNREACHABLE();
5307 }
5308
Anton Kirilov6f644202017-02-27 18:29:45 +00005309 __ B(eq, final_label, /* far_target */ false);
Artem Serov517d9f62016-12-12 15:51:15 +00005310 __ B(less_cond, &less, /* far_target */ false);
Artem Serov02d37832016-10-25 15:25:33 +01005311
5312 __ Bind(&greater);
5313 __ Mov(out, 1);
Anton Kirilov6f644202017-02-27 18:29:45 +00005314 __ B(final_label);
Artem Serov02d37832016-10-25 15:25:33 +01005315
5316 __ Bind(&less);
5317 __ Mov(out, -1);
5318
Anton Kirilov6f644202017-02-27 18:29:45 +00005319 if (done.IsReferenced()) {
5320 __ Bind(&done);
5321 }
Artem Serov02d37832016-10-25 15:25:33 +01005322}
5323
5324void LocationsBuilderARMVIXL::VisitPhi(HPhi* instruction) {
5325 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005326 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Artem Serov02d37832016-10-25 15:25:33 +01005327 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
5328 locations->SetInAt(i, Location::Any());
5329 }
5330 locations->SetOut(Location::Any());
5331}
5332
5333void InstructionCodeGeneratorARMVIXL::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
5334 LOG(FATAL) << "Unreachable";
5335}
5336
5337void CodeGeneratorARMVIXL::GenerateMemoryBarrier(MemBarrierKind kind) {
5338 // TODO (ported from quick): revisit ARM barrier kinds.
5339 DmbOptions flavor = DmbOptions::ISH; // Quiet C++ warnings.
5340 switch (kind) {
5341 case MemBarrierKind::kAnyStore:
5342 case MemBarrierKind::kLoadAny:
5343 case MemBarrierKind::kAnyAny: {
5344 flavor = DmbOptions::ISH;
5345 break;
5346 }
5347 case MemBarrierKind::kStoreStore: {
5348 flavor = DmbOptions::ISHST;
5349 break;
5350 }
5351 default:
5352 LOG(FATAL) << "Unexpected memory barrier " << kind;
5353 }
5354 __ Dmb(flavor);
5355}
5356
5357void InstructionCodeGeneratorARMVIXL::GenerateWideAtomicLoad(vixl32::Register addr,
5358 uint32_t offset,
5359 vixl32::Register out_lo,
5360 vixl32::Register out_hi) {
5361 UseScratchRegisterScope temps(GetVIXLAssembler());
5362 if (offset != 0) {
5363 vixl32::Register temp = temps.Acquire();
5364 __ Add(temp, addr, offset);
5365 addr = temp;
5366 }
Scott Wakelingb77051e2016-11-21 19:46:00 +00005367 __ Ldrexd(out_lo, out_hi, MemOperand(addr));
Artem Serov02d37832016-10-25 15:25:33 +01005368}
5369
5370void InstructionCodeGeneratorARMVIXL::GenerateWideAtomicStore(vixl32::Register addr,
5371 uint32_t offset,
5372 vixl32::Register value_lo,
5373 vixl32::Register value_hi,
5374 vixl32::Register temp1,
5375 vixl32::Register temp2,
5376 HInstruction* instruction) {
5377 UseScratchRegisterScope temps(GetVIXLAssembler());
5378 vixl32::Label fail;
5379 if (offset != 0) {
5380 vixl32::Register temp = temps.Acquire();
5381 __ Add(temp, addr, offset);
5382 addr = temp;
5383 }
5384 __ Bind(&fail);
Alexandre Rames374ddf32016-11-04 10:40:49 +00005385 {
5386 // Ensure the pc position is recorded immediately after the `ldrexd` instruction.
Artem Serov0fb37192016-12-06 18:13:40 +00005387 ExactAssemblyScope aas(GetVIXLAssembler(),
5388 vixl32::kMaxInstructionSizeInBytes,
5389 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00005390 // We need a load followed by store. (The address used in a STREX instruction must
5391 // be the same as the address in the most recently executed LDREX instruction.)
5392 __ ldrexd(temp1, temp2, MemOperand(addr));
5393 codegen_->MaybeRecordImplicitNullCheck(instruction);
5394 }
Scott Wakelingb77051e2016-11-21 19:46:00 +00005395 __ Strexd(temp1, value_lo, value_hi, MemOperand(addr));
xueliang.zhongf51bc622016-11-04 09:23:32 +00005396 __ CompareAndBranchIfNonZero(temp1, &fail);
Artem Serov02d37832016-10-25 15:25:33 +01005397}
Artem Serov02109dd2016-09-23 17:17:54 +01005398
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005399void LocationsBuilderARMVIXL::HandleFieldSet(
5400 HInstruction* instruction, const FieldInfo& field_info) {
5401 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
5402
5403 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005404 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005405 locations->SetInAt(0, Location::RequiresRegister());
5406
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005407 DataType::Type field_type = field_info.GetFieldType();
5408 if (DataType::IsFloatingPointType(field_type)) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005409 locations->SetInAt(1, Location::RequiresFpuRegister());
5410 } else {
5411 locations->SetInAt(1, Location::RequiresRegister());
5412 }
5413
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005414 bool is_wide = field_type == DataType::Type::kInt64 || field_type == DataType::Type::kFloat64;
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005415 bool generate_volatile = field_info.IsVolatile()
5416 && is_wide
5417 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
5418 bool needs_write_barrier =
5419 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
5420 // Temporary registers for the write barrier.
5421 // TODO: consider renaming StoreNeedsWriteBarrier to StoreNeedsGCMark.
5422 if (needs_write_barrier) {
5423 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
5424 locations->AddTemp(Location::RequiresRegister());
5425 } else if (generate_volatile) {
5426 // ARM encoding have some additional constraints for ldrexd/strexd:
5427 // - registers need to be consecutive
5428 // - the first register should be even but not R14.
5429 // We don't test for ARM yet, and the assertion makes sure that we
5430 // revisit this if we ever enable ARM encoding.
5431 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
5432
5433 locations->AddTemp(Location::RequiresRegister());
5434 locations->AddTemp(Location::RequiresRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005435 if (field_type == DataType::Type::kFloat64) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005436 // For doubles we need two more registers to copy the value.
5437 locations->AddTemp(LocationFrom(r2));
5438 locations->AddTemp(LocationFrom(r3));
5439 }
5440 }
5441}
5442
5443void InstructionCodeGeneratorARMVIXL::HandleFieldSet(HInstruction* instruction,
5444 const FieldInfo& field_info,
5445 bool value_can_be_null) {
5446 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
5447
5448 LocationSummary* locations = instruction->GetLocations();
5449 vixl32::Register base = InputRegisterAt(instruction, 0);
5450 Location value = locations->InAt(1);
5451
5452 bool is_volatile = field_info.IsVolatile();
5453 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005454 DataType::Type field_type = field_info.GetFieldType();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005455 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
5456 bool needs_write_barrier =
5457 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
5458
5459 if (is_volatile) {
5460 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
5461 }
5462
5463 switch (field_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005464 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005465 case DataType::Type::kUint8:
5466 case DataType::Type::kInt8:
5467 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005468 case DataType::Type::kInt16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005469 case DataType::Type::kInt32: {
5470 StoreOperandType operand_type = GetStoreOperandType(field_type);
5471 GetAssembler()->StoreToOffset(operand_type, RegisterFrom(value), base, offset);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005472 break;
5473 }
5474
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005475 case DataType::Type::kReference: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005476 if (kPoisonHeapReferences && needs_write_barrier) {
5477 // Note that in the case where `value` is a null reference,
5478 // we do not enter this block, as a null reference does not
5479 // need poisoning.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005480 DCHECK_EQ(field_type, DataType::Type::kReference);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005481 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
5482 __ Mov(temp, RegisterFrom(value));
5483 GetAssembler()->PoisonHeapReference(temp);
5484 GetAssembler()->StoreToOffset(kStoreWord, temp, base, offset);
5485 } else {
5486 GetAssembler()->StoreToOffset(kStoreWord, RegisterFrom(value), base, offset);
5487 }
5488 break;
5489 }
5490
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005491 case DataType::Type::kInt64: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005492 if (is_volatile && !atomic_ldrd_strd) {
5493 GenerateWideAtomicStore(base,
5494 offset,
5495 LowRegisterFrom(value),
5496 HighRegisterFrom(value),
5497 RegisterFrom(locations->GetTemp(0)),
5498 RegisterFrom(locations->GetTemp(1)),
5499 instruction);
5500 } else {
5501 GetAssembler()->StoreToOffset(kStoreWordPair, LowRegisterFrom(value), base, offset);
5502 codegen_->MaybeRecordImplicitNullCheck(instruction);
5503 }
5504 break;
5505 }
5506
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005507 case DataType::Type::kFloat32: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005508 GetAssembler()->StoreSToOffset(SRegisterFrom(value), base, offset);
5509 break;
5510 }
5511
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005512 case DataType::Type::kFloat64: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01005513 vixl32::DRegister value_reg = DRegisterFrom(value);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005514 if (is_volatile && !atomic_ldrd_strd) {
5515 vixl32::Register value_reg_lo = RegisterFrom(locations->GetTemp(0));
5516 vixl32::Register value_reg_hi = RegisterFrom(locations->GetTemp(1));
5517
5518 __ Vmov(value_reg_lo, value_reg_hi, value_reg);
5519
5520 GenerateWideAtomicStore(base,
5521 offset,
5522 value_reg_lo,
5523 value_reg_hi,
5524 RegisterFrom(locations->GetTemp(2)),
5525 RegisterFrom(locations->GetTemp(3)),
5526 instruction);
5527 } else {
5528 GetAssembler()->StoreDToOffset(value_reg, base, offset);
5529 codegen_->MaybeRecordImplicitNullCheck(instruction);
5530 }
5531 break;
5532 }
5533
Aart Bik66c158e2018-01-31 12:55:04 -08005534 case DataType::Type::kUint32:
5535 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005536 case DataType::Type::kVoid:
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005537 LOG(FATAL) << "Unreachable type " << field_type;
5538 UNREACHABLE();
5539 }
5540
5541 // Longs and doubles are handled in the switch.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005542 if (field_type != DataType::Type::kInt64 && field_type != DataType::Type::kFloat64) {
Alexandre Rames374ddf32016-11-04 10:40:49 +00005543 // TODO(VIXL): Here and for other calls to `MaybeRecordImplicitNullCheck` in this method, we
5544 // should use a scope and the assembler to emit the store instruction to guarantee that we
5545 // record the pc at the correct position. But the `Assembler` does not automatically handle
5546 // unencodable offsets. Practically, everything is fine because the helper and VIXL, at the time
5547 // of writing, do generate the store instruction last.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005548 codegen_->MaybeRecordImplicitNullCheck(instruction);
5549 }
5550
5551 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
5552 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
5553 vixl32::Register card = RegisterFrom(locations->GetTemp(1));
5554 codegen_->MarkGCCard(temp, card, base, RegisterFrom(value), value_can_be_null);
5555 }
5556
5557 if (is_volatile) {
5558 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
5559 }
5560}
5561
Artem Serov02d37832016-10-25 15:25:33 +01005562void LocationsBuilderARMVIXL::HandleFieldGet(HInstruction* instruction,
5563 const FieldInfo& field_info) {
5564 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
5565
5566 bool object_field_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005567 kEmitCompilerReadBarrier && (field_info.GetFieldType() == DataType::Type::kReference);
Artem Serov02d37832016-10-25 15:25:33 +01005568 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005569 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
5570 object_field_get_with_read_barrier
5571 ? LocationSummary::kCallOnSlowPath
5572 : LocationSummary::kNoCall);
Artem Serov02d37832016-10-25 15:25:33 +01005573 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
5574 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
5575 }
5576 locations->SetInAt(0, Location::RequiresRegister());
5577
5578 bool volatile_for_double = field_info.IsVolatile()
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005579 && (field_info.GetFieldType() == DataType::Type::kFloat64)
Artem Serov02d37832016-10-25 15:25:33 +01005580 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
5581 // The output overlaps in case of volatile long: we don't want the
5582 // code generated by GenerateWideAtomicLoad to overwrite the
5583 // object's location. Likewise, in the case of an object field get
5584 // with read barriers enabled, we do not want the load to overwrite
5585 // the object's location, as we need it to emit the read barrier.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005586 bool overlap =
5587 (field_info.IsVolatile() && (field_info.GetFieldType() == DataType::Type::kInt64)) ||
Artem Serov02d37832016-10-25 15:25:33 +01005588 object_field_get_with_read_barrier;
5589
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005590 if (DataType::IsFloatingPointType(instruction->GetType())) {
Artem Serov02d37832016-10-25 15:25:33 +01005591 locations->SetOut(Location::RequiresFpuRegister());
5592 } else {
5593 locations->SetOut(Location::RequiresRegister(),
5594 (overlap ? Location::kOutputOverlap : Location::kNoOutputOverlap));
5595 }
5596 if (volatile_for_double) {
5597 // ARM encoding have some additional constraints for ldrexd/strexd:
5598 // - registers need to be consecutive
5599 // - the first register should be even but not R14.
5600 // We don't test for ARM yet, and the assertion makes sure that we
5601 // revisit this if we ever enable ARM encoding.
5602 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
5603 locations->AddTemp(Location::RequiresRegister());
5604 locations->AddTemp(Location::RequiresRegister());
5605 } else if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
5606 // We need a temporary register for the read barrier marking slow
Artem Serovc5fcb442016-12-02 19:19:58 +00005607 // path in CodeGeneratorARMVIXL::GenerateFieldLoadWithBakerReadBarrier.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01005608 if (kBakerReadBarrierLinkTimeThunksEnableForFields &&
5609 !Runtime::Current()->UseJitCompilation()) {
5610 // If link-time thunks for the Baker read barrier are enabled, for AOT
5611 // loads we need a temporary only if the offset is too big.
5612 if (field_info.GetFieldOffset().Uint32Value() >= kReferenceLoadMinFarOffset) {
5613 locations->AddTemp(Location::RequiresRegister());
5614 }
5615 // And we always need the reserved entrypoint register.
5616 locations->AddTemp(Location::RegisterLocation(kBakerCcEntrypointRegister.GetCode()));
5617 } else {
5618 locations->AddTemp(Location::RequiresRegister());
5619 }
Artem Serov02d37832016-10-25 15:25:33 +01005620 }
5621}
5622
5623Location LocationsBuilderARMVIXL::ArithmeticZeroOrFpuRegister(HInstruction* input) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005624 DCHECK(DataType::IsFloatingPointType(input->GetType())) << input->GetType();
Artem Serov02d37832016-10-25 15:25:33 +01005625 if ((input->IsFloatConstant() && (input->AsFloatConstant()->IsArithmeticZero())) ||
5626 (input->IsDoubleConstant() && (input->AsDoubleConstant()->IsArithmeticZero()))) {
5627 return Location::ConstantLocation(input->AsConstant());
5628 } else {
5629 return Location::RequiresFpuRegister();
5630 }
5631}
5632
Artem Serov02109dd2016-09-23 17:17:54 +01005633Location LocationsBuilderARMVIXL::ArmEncodableConstantOrRegister(HInstruction* constant,
5634 Opcode opcode) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005635 DCHECK(!DataType::IsFloatingPointType(constant->GetType()));
Artem Serov02109dd2016-09-23 17:17:54 +01005636 if (constant->IsConstant() &&
5637 CanEncodeConstantAsImmediate(constant->AsConstant(), opcode)) {
5638 return Location::ConstantLocation(constant->AsConstant());
5639 }
5640 return Location::RequiresRegister();
5641}
5642
Vladimir Markof0a6a1d2018-01-08 14:23:56 +00005643static bool CanEncode32BitConstantAsImmediate(
5644 CodeGeneratorARMVIXL* codegen,
5645 uint32_t value,
5646 Opcode opcode,
5647 vixl32::FlagsUpdate flags_update = vixl32::FlagsUpdate::DontCare) {
5648 ArmVIXLAssembler* assembler = codegen->GetAssembler();
5649 if (assembler->ShifterOperandCanHold(opcode, value, flags_update)) {
Artem Serov02109dd2016-09-23 17:17:54 +01005650 return true;
5651 }
5652 Opcode neg_opcode = kNoOperand;
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00005653 uint32_t neg_value = 0;
Artem Serov02109dd2016-09-23 17:17:54 +01005654 switch (opcode) {
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00005655 case AND: neg_opcode = BIC; neg_value = ~value; break;
5656 case ORR: neg_opcode = ORN; neg_value = ~value; break;
5657 case ADD: neg_opcode = SUB; neg_value = -value; break;
5658 case ADC: neg_opcode = SBC; neg_value = ~value; break;
5659 case SUB: neg_opcode = ADD; neg_value = -value; break;
5660 case SBC: neg_opcode = ADC; neg_value = ~value; break;
5661 case MOV: neg_opcode = MVN; neg_value = ~value; break;
Artem Serov02109dd2016-09-23 17:17:54 +01005662 default:
5663 return false;
5664 }
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00005665
Vladimir Markof0a6a1d2018-01-08 14:23:56 +00005666 if (assembler->ShifterOperandCanHold(neg_opcode, neg_value, flags_update)) {
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00005667 return true;
5668 }
5669
5670 return opcode == AND && IsPowerOfTwo(value + 1);
Artem Serov02109dd2016-09-23 17:17:54 +01005671}
5672
Vladimir Markof0a6a1d2018-01-08 14:23:56 +00005673bool LocationsBuilderARMVIXL::CanEncodeConstantAsImmediate(HConstant* input_cst, Opcode opcode) {
5674 uint64_t value = static_cast<uint64_t>(Int64FromConstant(input_cst));
5675 if (DataType::Is64BitType(input_cst->GetType())) {
5676 Opcode high_opcode = opcode;
5677 vixl32::FlagsUpdate low_flags_update = vixl32::FlagsUpdate::DontCare;
5678 switch (opcode) {
5679 case SUB:
5680 // Flip the operation to an ADD.
5681 value = -value;
5682 opcode = ADD;
5683 FALLTHROUGH_INTENDED;
5684 case ADD:
5685 if (Low32Bits(value) == 0u) {
5686 return CanEncode32BitConstantAsImmediate(codegen_, High32Bits(value), opcode);
5687 }
5688 high_opcode = ADC;
5689 low_flags_update = vixl32::FlagsUpdate::SetFlags;
5690 break;
5691 default:
5692 break;
5693 }
5694 return CanEncode32BitConstantAsImmediate(codegen_, High32Bits(value), high_opcode) &&
5695 CanEncode32BitConstantAsImmediate(codegen_, Low32Bits(value), opcode, low_flags_update);
5696 } else {
5697 return CanEncode32BitConstantAsImmediate(codegen_, Low32Bits(value), opcode);
5698 }
5699}
5700
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005701void InstructionCodeGeneratorARMVIXL::HandleFieldGet(HInstruction* instruction,
5702 const FieldInfo& field_info) {
5703 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
5704
5705 LocationSummary* locations = instruction->GetLocations();
5706 vixl32::Register base = InputRegisterAt(instruction, 0);
5707 Location out = locations->Out();
5708 bool is_volatile = field_info.IsVolatile();
5709 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Vladimir Marko61b92282017-10-11 13:23:17 +01005710 DCHECK_EQ(DataType::Size(field_info.GetFieldType()), DataType::Size(instruction->GetType()));
5711 DataType::Type load_type = instruction->GetType();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005712 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
5713
Vladimir Marko61b92282017-10-11 13:23:17 +01005714 switch (load_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005715 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005716 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005717 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005718 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005719 case DataType::Type::kInt16:
5720 case DataType::Type::kInt32: {
Vladimir Marko61b92282017-10-11 13:23:17 +01005721 LoadOperandType operand_type = GetLoadOperandType(load_type);
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005722 GetAssembler()->LoadFromOffset(operand_type, RegisterFrom(out), base, offset);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005723 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005724 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005725
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005726 case DataType::Type::kReference: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005727 // /* HeapReference<Object> */ out = *(base + offset)
5728 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005729 Location temp_loc = locations->GetTemp(0);
5730 // Note that a potential implicit null check is handled in this
5731 // CodeGeneratorARMVIXL::GenerateFieldLoadWithBakerReadBarrier call.
5732 codegen_->GenerateFieldLoadWithBakerReadBarrier(
5733 instruction, out, base, offset, temp_loc, /* needs_null_check */ true);
5734 if (is_volatile) {
5735 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5736 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005737 } else {
5738 GetAssembler()->LoadFromOffset(kLoadWord, RegisterFrom(out), base, offset);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005739 codegen_->MaybeRecordImplicitNullCheck(instruction);
5740 if (is_volatile) {
5741 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5742 }
5743 // If read barriers are enabled, emit read barriers other than
5744 // Baker's using a slow path (and also unpoison the loaded
5745 // reference, if heap poisoning is enabled).
5746 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, locations->InAt(0), offset);
5747 }
5748 break;
5749 }
5750
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005751 case DataType::Type::kInt64:
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005752 if (is_volatile && !atomic_ldrd_strd) {
5753 GenerateWideAtomicLoad(base, offset, LowRegisterFrom(out), HighRegisterFrom(out));
5754 } else {
5755 GetAssembler()->LoadFromOffset(kLoadWordPair, LowRegisterFrom(out), base, offset);
5756 }
5757 break;
5758
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005759 case DataType::Type::kFloat32:
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005760 GetAssembler()->LoadSFromOffset(SRegisterFrom(out), base, offset);
5761 break;
5762
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005763 case DataType::Type::kFloat64: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01005764 vixl32::DRegister out_dreg = DRegisterFrom(out);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005765 if (is_volatile && !atomic_ldrd_strd) {
5766 vixl32::Register lo = RegisterFrom(locations->GetTemp(0));
5767 vixl32::Register hi = RegisterFrom(locations->GetTemp(1));
5768 GenerateWideAtomicLoad(base, offset, lo, hi);
5769 // TODO(VIXL): Do we need to be immediately after the ldrexd instruction? If so we need a
5770 // scope.
5771 codegen_->MaybeRecordImplicitNullCheck(instruction);
5772 __ Vmov(out_dreg, lo, hi);
5773 } else {
5774 GetAssembler()->LoadDFromOffset(out_dreg, base, offset);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005775 codegen_->MaybeRecordImplicitNullCheck(instruction);
5776 }
5777 break;
5778 }
5779
Aart Bik66c158e2018-01-31 12:55:04 -08005780 case DataType::Type::kUint32:
5781 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005782 case DataType::Type::kVoid:
Vladimir Marko61b92282017-10-11 13:23:17 +01005783 LOG(FATAL) << "Unreachable type " << load_type;
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005784 UNREACHABLE();
5785 }
5786
Vladimir Marko61b92282017-10-11 13:23:17 +01005787 if (load_type == DataType::Type::kReference || load_type == DataType::Type::kFloat64) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005788 // Potential implicit null checks, in the case of reference or
5789 // double fields, are handled in the previous switch statement.
5790 } else {
5791 // Address cases other than reference and double that may require an implicit null check.
Alexandre Rames374ddf32016-11-04 10:40:49 +00005792 // TODO(VIXL): Here and for other calls to `MaybeRecordImplicitNullCheck` in this method, we
5793 // should use a scope and the assembler to emit the load instruction to guarantee that we
5794 // record the pc at the correct position. But the `Assembler` does not automatically handle
5795 // unencodable offsets. Practically, everything is fine because the helper and VIXL, at the time
5796 // of writing, do generate the store instruction last.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005797 codegen_->MaybeRecordImplicitNullCheck(instruction);
5798 }
5799
5800 if (is_volatile) {
Vladimir Marko61b92282017-10-11 13:23:17 +01005801 if (load_type == DataType::Type::kReference) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005802 // Memory barriers, in the case of references, are also handled
5803 // in the previous switch statement.
5804 } else {
5805 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5806 }
5807 }
5808}
5809
5810void LocationsBuilderARMVIXL::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
5811 HandleFieldSet(instruction, instruction->GetFieldInfo());
5812}
5813
5814void InstructionCodeGeneratorARMVIXL::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
5815 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
5816}
5817
5818void LocationsBuilderARMVIXL::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5819 HandleFieldGet(instruction, instruction->GetFieldInfo());
5820}
5821
5822void InstructionCodeGeneratorARMVIXL::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5823 HandleFieldGet(instruction, instruction->GetFieldInfo());
5824}
5825
5826void LocationsBuilderARMVIXL::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5827 HandleFieldGet(instruction, instruction->GetFieldInfo());
5828}
5829
5830void InstructionCodeGeneratorARMVIXL::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5831 HandleFieldGet(instruction, instruction->GetFieldInfo());
5832}
5833
Scott Wakelingc34dba72016-10-03 10:14:44 +01005834void LocationsBuilderARMVIXL::VisitStaticFieldSet(HStaticFieldSet* instruction) {
5835 HandleFieldSet(instruction, instruction->GetFieldInfo());
5836}
5837
5838void InstructionCodeGeneratorARMVIXL::VisitStaticFieldSet(HStaticFieldSet* instruction) {
5839 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
5840}
5841
Artem Serovcfbe9132016-10-14 15:58:56 +01005842void LocationsBuilderARMVIXL::VisitUnresolvedInstanceFieldGet(
5843 HUnresolvedInstanceFieldGet* instruction) {
5844 FieldAccessCallingConventionARMVIXL calling_convention;
5845 codegen_->CreateUnresolvedFieldLocationSummary(
5846 instruction, instruction->GetFieldType(), calling_convention);
5847}
5848
5849void InstructionCodeGeneratorARMVIXL::VisitUnresolvedInstanceFieldGet(
5850 HUnresolvedInstanceFieldGet* instruction) {
5851 FieldAccessCallingConventionARMVIXL calling_convention;
5852 codegen_->GenerateUnresolvedFieldAccess(instruction,
5853 instruction->GetFieldType(),
5854 instruction->GetFieldIndex(),
5855 instruction->GetDexPc(),
5856 calling_convention);
5857}
5858
5859void LocationsBuilderARMVIXL::VisitUnresolvedInstanceFieldSet(
5860 HUnresolvedInstanceFieldSet* instruction) {
5861 FieldAccessCallingConventionARMVIXL calling_convention;
5862 codegen_->CreateUnresolvedFieldLocationSummary(
5863 instruction, instruction->GetFieldType(), calling_convention);
5864}
5865
5866void InstructionCodeGeneratorARMVIXL::VisitUnresolvedInstanceFieldSet(
5867 HUnresolvedInstanceFieldSet* instruction) {
5868 FieldAccessCallingConventionARMVIXL calling_convention;
5869 codegen_->GenerateUnresolvedFieldAccess(instruction,
5870 instruction->GetFieldType(),
5871 instruction->GetFieldIndex(),
5872 instruction->GetDexPc(),
5873 calling_convention);
5874}
5875
5876void LocationsBuilderARMVIXL::VisitUnresolvedStaticFieldGet(
5877 HUnresolvedStaticFieldGet* instruction) {
5878 FieldAccessCallingConventionARMVIXL calling_convention;
5879 codegen_->CreateUnresolvedFieldLocationSummary(
5880 instruction, instruction->GetFieldType(), calling_convention);
5881}
5882
5883void InstructionCodeGeneratorARMVIXL::VisitUnresolvedStaticFieldGet(
5884 HUnresolvedStaticFieldGet* instruction) {
5885 FieldAccessCallingConventionARMVIXL calling_convention;
5886 codegen_->GenerateUnresolvedFieldAccess(instruction,
5887 instruction->GetFieldType(),
5888 instruction->GetFieldIndex(),
5889 instruction->GetDexPc(),
5890 calling_convention);
5891}
5892
5893void LocationsBuilderARMVIXL::VisitUnresolvedStaticFieldSet(
5894 HUnresolvedStaticFieldSet* instruction) {
5895 FieldAccessCallingConventionARMVIXL calling_convention;
5896 codegen_->CreateUnresolvedFieldLocationSummary(
5897 instruction, instruction->GetFieldType(), calling_convention);
5898}
5899
5900void InstructionCodeGeneratorARMVIXL::VisitUnresolvedStaticFieldSet(
5901 HUnresolvedStaticFieldSet* instruction) {
5902 FieldAccessCallingConventionARMVIXL calling_convention;
5903 codegen_->GenerateUnresolvedFieldAccess(instruction,
5904 instruction->GetFieldType(),
5905 instruction->GetFieldIndex(),
5906 instruction->GetDexPc(),
5907 calling_convention);
5908}
5909
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005910void LocationsBuilderARMVIXL::VisitNullCheck(HNullCheck* instruction) {
Artem Serov657022c2016-11-23 14:19:38 +00005911 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005912 locations->SetInAt(0, Location::RequiresRegister());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005913}
5914
5915void CodeGeneratorARMVIXL::GenerateImplicitNullCheck(HNullCheck* instruction) {
5916 if (CanMoveNullCheckToUser(instruction)) {
5917 return;
5918 }
5919
5920 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames374ddf32016-11-04 10:40:49 +00005921 // Ensure the pc position is recorded immediately after the `ldr` instruction.
Artem Serov0fb37192016-12-06 18:13:40 +00005922 ExactAssemblyScope aas(GetVIXLAssembler(),
5923 vixl32::kMaxInstructionSizeInBytes,
5924 CodeBufferCheckScope::kMaximumSize);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005925 __ ldr(temps.Acquire(), MemOperand(InputRegisterAt(instruction, 0)));
5926 RecordPcInfo(instruction, instruction->GetDexPc());
5927}
5928
5929void CodeGeneratorARMVIXL::GenerateExplicitNullCheck(HNullCheck* instruction) {
5930 NullCheckSlowPathARMVIXL* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01005931 new (GetScopedAllocator()) NullCheckSlowPathARMVIXL(instruction);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005932 AddSlowPath(slow_path);
xueliang.zhongf51bc622016-11-04 09:23:32 +00005933 __ CompareAndBranchIfZero(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005934}
5935
5936void InstructionCodeGeneratorARMVIXL::VisitNullCheck(HNullCheck* instruction) {
5937 codegen_->GenerateNullCheck(instruction);
5938}
5939
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005940void CodeGeneratorARMVIXL::LoadFromShiftedRegOffset(DataType::Type type,
Scott Wakelingc34dba72016-10-03 10:14:44 +01005941 Location out_loc,
5942 vixl32::Register base,
5943 vixl32::Register reg_index,
5944 vixl32::Condition cond) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005945 uint32_t shift_count = DataType::SizeShift(type);
Scott Wakelingc34dba72016-10-03 10:14:44 +01005946 MemOperand mem_address(base, reg_index, vixl32::LSL, shift_count);
5947
5948 switch (type) {
Vladimir Marko61b92282017-10-11 13:23:17 +01005949 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005950 case DataType::Type::kUint8:
Vladimir Marko61b92282017-10-11 13:23:17 +01005951 __ Ldrb(cond, RegisterFrom(out_loc), mem_address);
5952 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005953 case DataType::Type::kInt8:
Scott Wakelingc34dba72016-10-03 10:14:44 +01005954 __ Ldrsb(cond, RegisterFrom(out_loc), mem_address);
5955 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005956 case DataType::Type::kUint16:
Scott Wakelingc34dba72016-10-03 10:14:44 +01005957 __ Ldrh(cond, RegisterFrom(out_loc), mem_address);
5958 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005959 case DataType::Type::kInt16:
5960 __ Ldrsh(cond, RegisterFrom(out_loc), mem_address);
5961 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005962 case DataType::Type::kReference:
5963 case DataType::Type::kInt32:
Scott Wakelingc34dba72016-10-03 10:14:44 +01005964 __ Ldr(cond, RegisterFrom(out_loc), mem_address);
5965 break;
5966 // T32 doesn't support LoadFromShiftedRegOffset mem address mode for these types.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005967 case DataType::Type::kInt64:
5968 case DataType::Type::kFloat32:
5969 case DataType::Type::kFloat64:
Scott Wakelingc34dba72016-10-03 10:14:44 +01005970 default:
5971 LOG(FATAL) << "Unreachable type " << type;
5972 UNREACHABLE();
5973 }
5974}
5975
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005976void CodeGeneratorARMVIXL::StoreToShiftedRegOffset(DataType::Type type,
Scott Wakelingc34dba72016-10-03 10:14:44 +01005977 Location loc,
5978 vixl32::Register base,
5979 vixl32::Register reg_index,
5980 vixl32::Condition cond) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005981 uint32_t shift_count = DataType::SizeShift(type);
Scott Wakelingc34dba72016-10-03 10:14:44 +01005982 MemOperand mem_address(base, reg_index, vixl32::LSL, shift_count);
5983
5984 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005985 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005986 case DataType::Type::kUint8:
5987 case DataType::Type::kInt8:
Scott Wakelingc34dba72016-10-03 10:14:44 +01005988 __ Strb(cond, RegisterFrom(loc), mem_address);
5989 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005990 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005991 case DataType::Type::kInt16:
Scott Wakelingc34dba72016-10-03 10:14:44 +01005992 __ Strh(cond, RegisterFrom(loc), mem_address);
5993 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005994 case DataType::Type::kReference:
5995 case DataType::Type::kInt32:
Scott Wakelingc34dba72016-10-03 10:14:44 +01005996 __ Str(cond, RegisterFrom(loc), mem_address);
5997 break;
5998 // T32 doesn't support StoreToShiftedRegOffset mem address mode for these types.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005999 case DataType::Type::kInt64:
6000 case DataType::Type::kFloat32:
6001 case DataType::Type::kFloat64:
Scott Wakelingc34dba72016-10-03 10:14:44 +01006002 default:
6003 LOG(FATAL) << "Unreachable type " << type;
6004 UNREACHABLE();
6005 }
6006}
6007
6008void LocationsBuilderARMVIXL::VisitArrayGet(HArrayGet* instruction) {
6009 bool object_array_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006010 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006011 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006012 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
6013 object_array_get_with_read_barrier
6014 ? LocationSummary::kCallOnSlowPath
6015 : LocationSummary::kNoCall);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006016 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006017 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Scott Wakelingc34dba72016-10-03 10:14:44 +01006018 }
6019 locations->SetInAt(0, Location::RequiresRegister());
6020 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006021 if (DataType::IsFloatingPointType(instruction->GetType())) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006022 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
6023 } else {
6024 // The output overlaps in the case of an object array get with
6025 // read barriers enabled: we do not want the move to overwrite the
6026 // array's location, as we need it to emit the read barrier.
6027 locations->SetOut(
6028 Location::RequiresRegister(),
6029 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
6030 }
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01006031 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
6032 // We need a temporary register for the read barrier marking slow
6033 // path in CodeGeneratorARMVIXL::GenerateArrayLoadWithBakerReadBarrier.
6034 if (kBakerReadBarrierLinkTimeThunksEnableForFields &&
6035 !Runtime::Current()->UseJitCompilation() &&
6036 instruction->GetIndex()->IsConstant()) {
6037 // Array loads with constant index are treated as field loads.
6038 // If link-time thunks for the Baker read barrier are enabled, for AOT
6039 // constant index loads we need a temporary only if the offset is too big.
6040 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
6041 uint32_t index = instruction->GetIndex()->AsIntConstant()->GetValue();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006042 offset += index << DataType::SizeShift(DataType::Type::kReference);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01006043 if (offset >= kReferenceLoadMinFarOffset) {
6044 locations->AddTemp(Location::RequiresRegister());
6045 }
6046 // And we always need the reserved entrypoint register.
6047 locations->AddTemp(Location::RegisterLocation(kBakerCcEntrypointRegister.GetCode()));
6048 } else if (kBakerReadBarrierLinkTimeThunksEnableForArrays &&
6049 !Runtime::Current()->UseJitCompilation() &&
6050 !instruction->GetIndex()->IsConstant()) {
6051 // We need a non-scratch temporary for the array data pointer.
6052 locations->AddTemp(Location::RequiresRegister());
6053 // And we always need the reserved entrypoint register.
6054 locations->AddTemp(Location::RegisterLocation(kBakerCcEntrypointRegister.GetCode()));
6055 } else {
6056 locations->AddTemp(Location::RequiresRegister());
6057 }
6058 } else if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
6059 // Also need a temporary for String compression feature.
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006060 locations->AddTemp(Location::RequiresRegister());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006061 }
6062}
6063
6064void InstructionCodeGeneratorARMVIXL::VisitArrayGet(HArrayGet* instruction) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006065 LocationSummary* locations = instruction->GetLocations();
6066 Location obj_loc = locations->InAt(0);
6067 vixl32::Register obj = InputRegisterAt(instruction, 0);
6068 Location index = locations->InAt(1);
6069 Location out_loc = locations->Out();
6070 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006071 DataType::Type type = instruction->GetType();
Scott Wakelingc34dba72016-10-03 10:14:44 +01006072 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
6073 instruction->IsStringCharAt();
6074 HInstruction* array_instr = instruction->GetArray();
6075 bool has_intermediate_address = array_instr->IsIntermediateAddress();
Scott Wakelingc34dba72016-10-03 10:14:44 +01006076
6077 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006078 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006079 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006080 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006081 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006082 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006083 case DataType::Type::kInt32: {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006084 vixl32::Register length;
6085 if (maybe_compressed_char_at) {
6086 length = RegisterFrom(locations->GetTemp(0));
6087 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
6088 GetAssembler()->LoadFromOffset(kLoadWord, length, obj, count_offset);
6089 codegen_->MaybeRecordImplicitNullCheck(instruction);
6090 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006091 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006092 int32_t const_index = Int32ConstantFrom(index);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006093 if (maybe_compressed_char_at) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006094 vixl32::Label uncompressed_load, done;
Anton Kirilov6f644202017-02-27 18:29:45 +00006095 vixl32::Label* final_label = codegen_->GetFinalLabel(instruction, &done);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006096 __ Lsrs(length, length, 1u); // LSRS has a 16-bit encoding, TST (immediate) does not.
6097 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
6098 "Expecting 0=compressed, 1=uncompressed");
Artem Serov517d9f62016-12-12 15:51:15 +00006099 __ B(cs, &uncompressed_load, /* far_target */ false);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006100 GetAssembler()->LoadFromOffset(kLoadUnsignedByte,
6101 RegisterFrom(out_loc),
6102 obj,
6103 data_offset + const_index);
Anton Kirilov6f644202017-02-27 18:29:45 +00006104 __ B(final_label);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006105 __ Bind(&uncompressed_load);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006106 GetAssembler()->LoadFromOffset(GetLoadOperandType(DataType::Type::kUint16),
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006107 RegisterFrom(out_loc),
6108 obj,
6109 data_offset + (const_index << 1));
Anton Kirilov6f644202017-02-27 18:29:45 +00006110 if (done.IsReferenced()) {
6111 __ Bind(&done);
6112 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006113 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006114 uint32_t full_offset = data_offset + (const_index << DataType::SizeShift(type));
Scott Wakelingc34dba72016-10-03 10:14:44 +01006115
6116 LoadOperandType load_type = GetLoadOperandType(type);
6117 GetAssembler()->LoadFromOffset(load_type, RegisterFrom(out_loc), obj, full_offset);
6118 }
6119 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006120 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006121 vixl32::Register temp = temps.Acquire();
6122
6123 if (has_intermediate_address) {
Artem Serov2bbc9532016-10-21 11:51:50 +01006124 // We do not need to compute the intermediate address from the array: the
6125 // input instruction has done it already. See the comment in
6126 // `TryExtractArrayAccessAddress()`.
6127 if (kIsDebugBuild) {
6128 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
Anton Kirilov644032c2016-12-06 17:51:43 +00006129 DCHECK_EQ(Uint64ConstantFrom(tmp->GetOffset()), data_offset);
Artem Serov2bbc9532016-10-21 11:51:50 +01006130 }
6131 temp = obj;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006132 } else {
6133 __ Add(temp, obj, data_offset);
6134 }
6135 if (maybe_compressed_char_at) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006136 vixl32::Label uncompressed_load, done;
Anton Kirilov6f644202017-02-27 18:29:45 +00006137 vixl32::Label* final_label = codegen_->GetFinalLabel(instruction, &done);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006138 __ Lsrs(length, length, 1u); // LSRS has a 16-bit encoding, TST (immediate) does not.
6139 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
6140 "Expecting 0=compressed, 1=uncompressed");
Artem Serov517d9f62016-12-12 15:51:15 +00006141 __ B(cs, &uncompressed_load, /* far_target */ false);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006142 __ Ldrb(RegisterFrom(out_loc), MemOperand(temp, RegisterFrom(index), vixl32::LSL, 0));
Anton Kirilov6f644202017-02-27 18:29:45 +00006143 __ B(final_label);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006144 __ Bind(&uncompressed_load);
6145 __ Ldrh(RegisterFrom(out_loc), MemOperand(temp, RegisterFrom(index), vixl32::LSL, 1));
Anton Kirilov6f644202017-02-27 18:29:45 +00006146 if (done.IsReferenced()) {
6147 __ Bind(&done);
6148 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006149 } else {
6150 codegen_->LoadFromShiftedRegOffset(type, out_loc, temp, RegisterFrom(index));
6151 }
6152 }
6153 break;
6154 }
6155
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006156 case DataType::Type::kReference: {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006157 // The read barrier instrumentation of object ArrayGet
6158 // instructions does not support the HIntermediateAddress
6159 // instruction.
6160 DCHECK(!(has_intermediate_address && kEmitCompilerReadBarrier));
6161
Scott Wakelingc34dba72016-10-03 10:14:44 +01006162 static_assert(
6163 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6164 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
6165 // /* HeapReference<Object> */ out =
6166 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
6167 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006168 Location temp = locations->GetTemp(0);
6169 // Note that a potential implicit null check is handled in this
6170 // CodeGeneratorARMVIXL::GenerateArrayLoadWithBakerReadBarrier call.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01006171 DCHECK(!instruction->CanDoImplicitNullCheckOn(instruction->InputAt(0)));
6172 if (index.IsConstant()) {
6173 // Array load with a constant index can be treated as a field load.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006174 data_offset += Int32ConstantFrom(index) << DataType::SizeShift(type);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01006175 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6176 out_loc,
6177 obj,
6178 data_offset,
6179 locations->GetTemp(0),
6180 /* needs_null_check */ false);
6181 } else {
6182 codegen_->GenerateArrayLoadWithBakerReadBarrier(
6183 instruction, out_loc, obj, data_offset, index, temp, /* needs_null_check */ false);
6184 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006185 } else {
6186 vixl32::Register out = OutputRegister(instruction);
6187 if (index.IsConstant()) {
6188 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00006189 (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006190 GetAssembler()->LoadFromOffset(kLoadWord, out, obj, offset);
Alexandre Rames374ddf32016-11-04 10:40:49 +00006191 // TODO(VIXL): Here and for other calls to `MaybeRecordImplicitNullCheck` in this method,
6192 // we should use a scope and the assembler to emit the load instruction to guarantee that
6193 // we record the pc at the correct position. But the `Assembler` does not automatically
6194 // handle unencodable offsets. Practically, everything is fine because the helper and
6195 // VIXL, at the time of writing, do generate the store instruction last.
Scott Wakelingc34dba72016-10-03 10:14:44 +01006196 codegen_->MaybeRecordImplicitNullCheck(instruction);
6197 // If read barriers are enabled, emit read barriers other than
6198 // Baker's using a slow path (and also unpoison the loaded
6199 // reference, if heap poisoning is enabled).
6200 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
6201 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006202 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006203 vixl32::Register temp = temps.Acquire();
6204
6205 if (has_intermediate_address) {
Artem Serov2bbc9532016-10-21 11:51:50 +01006206 // We do not need to compute the intermediate address from the array: the
6207 // input instruction has done it already. See the comment in
6208 // `TryExtractArrayAccessAddress()`.
6209 if (kIsDebugBuild) {
6210 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
Anton Kirilov644032c2016-12-06 17:51:43 +00006211 DCHECK_EQ(Uint64ConstantFrom(tmp->GetOffset()), data_offset);
Artem Serov2bbc9532016-10-21 11:51:50 +01006212 }
6213 temp = obj;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006214 } else {
6215 __ Add(temp, obj, data_offset);
6216 }
6217 codegen_->LoadFromShiftedRegOffset(type, out_loc, temp, RegisterFrom(index));
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006218 temps.Close();
Alexandre Rames374ddf32016-11-04 10:40:49 +00006219 // TODO(VIXL): Use a scope to ensure that we record the pc position immediately after the
6220 // load instruction. Practically, everything is fine because the helper and VIXL, at the
6221 // time of writing, do generate the store instruction last.
Scott Wakelingc34dba72016-10-03 10:14:44 +01006222 codegen_->MaybeRecordImplicitNullCheck(instruction);
6223 // If read barriers are enabled, emit read barriers other than
6224 // Baker's using a slow path (and also unpoison the loaded
6225 // reference, if heap poisoning is enabled).
6226 codegen_->MaybeGenerateReadBarrierSlow(
6227 instruction, out_loc, out_loc, obj_loc, data_offset, index);
6228 }
6229 }
6230 break;
6231 }
6232
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006233 case DataType::Type::kInt64: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006234 if (index.IsConstant()) {
6235 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00006236 (Int32ConstantFrom(index) << TIMES_8) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006237 GetAssembler()->LoadFromOffset(kLoadWordPair, LowRegisterFrom(out_loc), obj, offset);
6238 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006239 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006240 vixl32::Register temp = temps.Acquire();
6241 __ Add(temp, obj, Operand(RegisterFrom(index), vixl32::LSL, TIMES_8));
6242 GetAssembler()->LoadFromOffset(kLoadWordPair, LowRegisterFrom(out_loc), temp, data_offset);
6243 }
6244 break;
6245 }
6246
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006247 case DataType::Type::kFloat32: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006248 vixl32::SRegister out = SRegisterFrom(out_loc);
6249 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006250 size_t offset = (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006251 GetAssembler()->LoadSFromOffset(out, obj, offset);
6252 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006253 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006254 vixl32::Register temp = temps.Acquire();
6255 __ Add(temp, obj, Operand(RegisterFrom(index), vixl32::LSL, TIMES_4));
6256 GetAssembler()->LoadSFromOffset(out, temp, data_offset);
6257 }
6258 break;
6259 }
6260
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006261 case DataType::Type::kFloat64: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006262 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006263 size_t offset = (Int32ConstantFrom(index) << TIMES_8) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006264 GetAssembler()->LoadDFromOffset(DRegisterFrom(out_loc), obj, offset);
6265 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006266 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006267 vixl32::Register temp = temps.Acquire();
6268 __ Add(temp, obj, Operand(RegisterFrom(index), vixl32::LSL, TIMES_8));
6269 GetAssembler()->LoadDFromOffset(DRegisterFrom(out_loc), temp, data_offset);
6270 }
6271 break;
6272 }
6273
Aart Bik66c158e2018-01-31 12:55:04 -08006274 case DataType::Type::kUint32:
6275 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006276 case DataType::Type::kVoid:
Scott Wakelingc34dba72016-10-03 10:14:44 +01006277 LOG(FATAL) << "Unreachable type " << type;
6278 UNREACHABLE();
6279 }
6280
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006281 if (type == DataType::Type::kReference) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006282 // Potential implicit null checks, in the case of reference
6283 // arrays, are handled in the previous switch statement.
6284 } else if (!maybe_compressed_char_at) {
Alexandre Rames374ddf32016-11-04 10:40:49 +00006285 // TODO(VIXL): Use a scope to ensure we record the pc info immediately after
6286 // the preceding load instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01006287 codegen_->MaybeRecordImplicitNullCheck(instruction);
6288 }
6289}
6290
6291void LocationsBuilderARMVIXL::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006292 DataType::Type value_type = instruction->GetComponentType();
Scott Wakelingc34dba72016-10-03 10:14:44 +01006293
6294 bool needs_write_barrier =
6295 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
6296 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
6297
Vladimir Markoca6fff82017-10-03 14:49:14 +01006298 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Scott Wakelingc34dba72016-10-03 10:14:44 +01006299 instruction,
6300 may_need_runtime_call_for_type_check ?
6301 LocationSummary::kCallOnSlowPath :
6302 LocationSummary::kNoCall);
6303
6304 locations->SetInAt(0, Location::RequiresRegister());
6305 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006306 if (DataType::IsFloatingPointType(value_type)) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006307 locations->SetInAt(2, Location::RequiresFpuRegister());
6308 } else {
6309 locations->SetInAt(2, Location::RequiresRegister());
6310 }
6311 if (needs_write_barrier) {
6312 // Temporary registers for the write barrier.
6313 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
6314 locations->AddTemp(Location::RequiresRegister());
6315 }
6316}
6317
6318void InstructionCodeGeneratorARMVIXL::VisitArraySet(HArraySet* instruction) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006319 LocationSummary* locations = instruction->GetLocations();
6320 vixl32::Register array = InputRegisterAt(instruction, 0);
6321 Location index = locations->InAt(1);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006322 DataType::Type value_type = instruction->GetComponentType();
Scott Wakelingc34dba72016-10-03 10:14:44 +01006323 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
6324 bool needs_write_barrier =
6325 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
6326 uint32_t data_offset =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006327 mirror::Array::DataOffset(DataType::Size(value_type)).Uint32Value();
Scott Wakelingc34dba72016-10-03 10:14:44 +01006328 Location value_loc = locations->InAt(2);
6329 HInstruction* array_instr = instruction->GetArray();
6330 bool has_intermediate_address = array_instr->IsIntermediateAddress();
Scott Wakelingc34dba72016-10-03 10:14:44 +01006331
6332 switch (value_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006333 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006334 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006335 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006336 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006337 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006338 case DataType::Type::kInt32: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006339 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006340 int32_t const_index = Int32ConstantFrom(index);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006341 uint32_t full_offset =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006342 data_offset + (const_index << DataType::SizeShift(value_type));
Scott Wakelingc34dba72016-10-03 10:14:44 +01006343 StoreOperandType store_type = GetStoreOperandType(value_type);
6344 GetAssembler()->StoreToOffset(store_type, RegisterFrom(value_loc), array, full_offset);
6345 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006346 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006347 vixl32::Register temp = temps.Acquire();
6348
6349 if (has_intermediate_address) {
Artem Serov2bbc9532016-10-21 11:51:50 +01006350 // We do not need to compute the intermediate address from the array: the
6351 // input instruction has done it already. See the comment in
6352 // `TryExtractArrayAccessAddress()`.
6353 if (kIsDebugBuild) {
6354 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
Anton Kirilov644032c2016-12-06 17:51:43 +00006355 DCHECK_EQ(Uint64ConstantFrom(tmp->GetOffset()), data_offset);
Artem Serov2bbc9532016-10-21 11:51:50 +01006356 }
6357 temp = array;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006358 } else {
6359 __ Add(temp, array, data_offset);
6360 }
6361 codegen_->StoreToShiftedRegOffset(value_type, value_loc, temp, RegisterFrom(index));
6362 }
6363 break;
6364 }
6365
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006366 case DataType::Type::kReference: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006367 vixl32::Register value = RegisterFrom(value_loc);
6368 // TryExtractArrayAccessAddress optimization is never applied for non-primitive ArraySet.
6369 // See the comment in instruction_simplifier_shared.cc.
6370 DCHECK(!has_intermediate_address);
6371
6372 if (instruction->InputAt(2)->IsNullConstant()) {
6373 // Just setting null.
6374 if (index.IsConstant()) {
6375 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00006376 (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006377 GetAssembler()->StoreToOffset(kStoreWord, value, array, offset);
6378 } else {
6379 DCHECK(index.IsRegister()) << index;
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006380 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006381 vixl32::Register temp = temps.Acquire();
6382 __ Add(temp, array, data_offset);
6383 codegen_->StoreToShiftedRegOffset(value_type, value_loc, temp, RegisterFrom(index));
6384 }
Alexandre Rames374ddf32016-11-04 10:40:49 +00006385 // TODO(VIXL): Use a scope to ensure we record the pc info immediately after the preceding
6386 // store instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01006387 codegen_->MaybeRecordImplicitNullCheck(instruction);
6388 DCHECK(!needs_write_barrier);
6389 DCHECK(!may_need_runtime_call_for_type_check);
6390 break;
6391 }
6392
6393 DCHECK(needs_write_barrier);
6394 Location temp1_loc = locations->GetTemp(0);
6395 vixl32::Register temp1 = RegisterFrom(temp1_loc);
6396 Location temp2_loc = locations->GetTemp(1);
6397 vixl32::Register temp2 = RegisterFrom(temp2_loc);
6398 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6399 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6400 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6401 vixl32::Label done;
Anton Kirilov6f644202017-02-27 18:29:45 +00006402 vixl32::Label* final_label = codegen_->GetFinalLabel(instruction, &done);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006403 SlowPathCodeARMVIXL* slow_path = nullptr;
6404
6405 if (may_need_runtime_call_for_type_check) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01006406 slow_path = new (codegen_->GetScopedAllocator()) ArraySetSlowPathARMVIXL(instruction);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006407 codegen_->AddSlowPath(slow_path);
6408 if (instruction->GetValueCanBeNull()) {
6409 vixl32::Label non_zero;
xueliang.zhongf51bc622016-11-04 09:23:32 +00006410 __ CompareAndBranchIfNonZero(value, &non_zero);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006411 if (index.IsConstant()) {
6412 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00006413 (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006414 GetAssembler()->StoreToOffset(kStoreWord, value, array, offset);
6415 } else {
6416 DCHECK(index.IsRegister()) << index;
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006417 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006418 vixl32::Register temp = temps.Acquire();
6419 __ Add(temp, array, data_offset);
6420 codegen_->StoreToShiftedRegOffset(value_type, value_loc, temp, RegisterFrom(index));
6421 }
Alexandre Rames374ddf32016-11-04 10:40:49 +00006422 // TODO(VIXL): Use a scope to ensure we record the pc info immediately after the preceding
6423 // store instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01006424 codegen_->MaybeRecordImplicitNullCheck(instruction);
Anton Kirilov6f644202017-02-27 18:29:45 +00006425 __ B(final_label);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006426 __ Bind(&non_zero);
6427 }
6428
6429 // Note that when read barriers are enabled, the type checks
6430 // are performed without read barriers. This is fine, even in
6431 // the case where a class object is in the from-space after
6432 // the flip, as a comparison involving such a type would not
6433 // produce a false positive; it may of course produce a false
6434 // negative, in which case we would take the ArraySet slow
6435 // path.
6436
Alexandre Rames374ddf32016-11-04 10:40:49 +00006437 {
6438 // Ensure we record the pc position immediately after the `ldr` instruction.
Artem Serov0fb37192016-12-06 18:13:40 +00006439 ExactAssemblyScope aas(GetVIXLAssembler(),
6440 vixl32::kMaxInstructionSizeInBytes,
6441 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00006442 // /* HeapReference<Class> */ temp1 = array->klass_
6443 __ ldr(temp1, MemOperand(array, class_offset));
6444 codegen_->MaybeRecordImplicitNullCheck(instruction);
6445 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006446 GetAssembler()->MaybeUnpoisonHeapReference(temp1);
6447
6448 // /* HeapReference<Class> */ temp1 = temp1->component_type_
6449 GetAssembler()->LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
6450 // /* HeapReference<Class> */ temp2 = value->klass_
6451 GetAssembler()->LoadFromOffset(kLoadWord, temp2, value, class_offset);
6452 // If heap poisoning is enabled, no need to unpoison `temp1`
6453 // nor `temp2`, as we are comparing two poisoned references.
6454 __ Cmp(temp1, temp2);
6455
6456 if (instruction->StaticTypeOfArrayIsObjectArray()) {
6457 vixl32::Label do_put;
Artem Serov517d9f62016-12-12 15:51:15 +00006458 __ B(eq, &do_put, /* far_target */ false);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006459 // If heap poisoning is enabled, the `temp1` reference has
6460 // not been unpoisoned yet; unpoison it now.
6461 GetAssembler()->MaybeUnpoisonHeapReference(temp1);
6462
6463 // /* HeapReference<Class> */ temp1 = temp1->super_class_
6464 GetAssembler()->LoadFromOffset(kLoadWord, temp1, temp1, super_offset);
6465 // If heap poisoning is enabled, no need to unpoison
6466 // `temp1`, as we are comparing against null below.
xueliang.zhongf51bc622016-11-04 09:23:32 +00006467 __ CompareAndBranchIfNonZero(temp1, slow_path->GetEntryLabel());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006468 __ Bind(&do_put);
6469 } else {
6470 __ B(ne, slow_path->GetEntryLabel());
6471 }
6472 }
6473
6474 vixl32::Register source = value;
6475 if (kPoisonHeapReferences) {
6476 // Note that in the case where `value` is a null reference,
6477 // we do not enter this block, as a null reference does not
6478 // need poisoning.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006479 DCHECK_EQ(value_type, DataType::Type::kReference);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006480 __ Mov(temp1, value);
6481 GetAssembler()->PoisonHeapReference(temp1);
6482 source = temp1;
6483 }
6484
6485 if (index.IsConstant()) {
6486 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00006487 (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006488 GetAssembler()->StoreToOffset(kStoreWord, source, array, offset);
6489 } else {
6490 DCHECK(index.IsRegister()) << index;
6491
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006492 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006493 vixl32::Register temp = temps.Acquire();
6494 __ Add(temp, array, data_offset);
6495 codegen_->StoreToShiftedRegOffset(value_type,
6496 LocationFrom(source),
6497 temp,
6498 RegisterFrom(index));
6499 }
6500
6501 if (!may_need_runtime_call_for_type_check) {
Alexandre Rames374ddf32016-11-04 10:40:49 +00006502 // TODO(VIXL): Ensure we record the pc position immediately after the preceding store
6503 // instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01006504 codegen_->MaybeRecordImplicitNullCheck(instruction);
6505 }
6506
6507 codegen_->MarkGCCard(temp1, temp2, array, value, instruction->GetValueCanBeNull());
6508
6509 if (done.IsReferenced()) {
6510 __ Bind(&done);
6511 }
6512
6513 if (slow_path != nullptr) {
6514 __ Bind(slow_path->GetExitLabel());
6515 }
6516
6517 break;
6518 }
6519
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006520 case DataType::Type::kInt64: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006521 Location value = locations->InAt(2);
6522 if (index.IsConstant()) {
6523 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00006524 (Int32ConstantFrom(index) << TIMES_8) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006525 GetAssembler()->StoreToOffset(kStoreWordPair, LowRegisterFrom(value), array, offset);
6526 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006527 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006528 vixl32::Register temp = temps.Acquire();
6529 __ Add(temp, array, Operand(RegisterFrom(index), vixl32::LSL, TIMES_8));
6530 GetAssembler()->StoreToOffset(kStoreWordPair, LowRegisterFrom(value), temp, data_offset);
6531 }
6532 break;
6533 }
6534
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006535 case DataType::Type::kFloat32: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006536 Location value = locations->InAt(2);
6537 DCHECK(value.IsFpuRegister());
6538 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006539 size_t offset = (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006540 GetAssembler()->StoreSToOffset(SRegisterFrom(value), array, offset);
6541 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006542 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006543 vixl32::Register temp = temps.Acquire();
6544 __ Add(temp, array, Operand(RegisterFrom(index), vixl32::LSL, TIMES_4));
6545 GetAssembler()->StoreSToOffset(SRegisterFrom(value), temp, data_offset);
6546 }
6547 break;
6548 }
6549
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006550 case DataType::Type::kFloat64: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006551 Location value = locations->InAt(2);
6552 DCHECK(value.IsFpuRegisterPair());
6553 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006554 size_t offset = (Int32ConstantFrom(index) << TIMES_8) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006555 GetAssembler()->StoreDToOffset(DRegisterFrom(value), array, offset);
6556 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006557 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006558 vixl32::Register temp = temps.Acquire();
6559 __ Add(temp, array, Operand(RegisterFrom(index), vixl32::LSL, TIMES_8));
6560 GetAssembler()->StoreDToOffset(DRegisterFrom(value), temp, data_offset);
6561 }
6562 break;
6563 }
6564
Aart Bik66c158e2018-01-31 12:55:04 -08006565 case DataType::Type::kUint32:
6566 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006567 case DataType::Type::kVoid:
Scott Wakelingc34dba72016-10-03 10:14:44 +01006568 LOG(FATAL) << "Unreachable type " << value_type;
6569 UNREACHABLE();
6570 }
6571
6572 // Objects are handled in the switch.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006573 if (value_type != DataType::Type::kReference) {
Alexandre Rames374ddf32016-11-04 10:40:49 +00006574 // TODO(VIXL): Ensure we record the pc position immediately after the preceding store
6575 // instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01006576 codegen_->MaybeRecordImplicitNullCheck(instruction);
6577 }
6578}
6579
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006580void LocationsBuilderARMVIXL::VisitArrayLength(HArrayLength* instruction) {
6581 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006582 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006583 locations->SetInAt(0, Location::RequiresRegister());
6584 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6585}
6586
6587void InstructionCodeGeneratorARMVIXL::VisitArrayLength(HArrayLength* instruction) {
6588 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
6589 vixl32::Register obj = InputRegisterAt(instruction, 0);
6590 vixl32::Register out = OutputRegister(instruction);
Alexandre Rames374ddf32016-11-04 10:40:49 +00006591 {
Artem Serov0fb37192016-12-06 18:13:40 +00006592 ExactAssemblyScope aas(GetVIXLAssembler(),
6593 vixl32::kMaxInstructionSizeInBytes,
6594 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00006595 __ ldr(out, MemOperand(obj, offset));
6596 codegen_->MaybeRecordImplicitNullCheck(instruction);
6597 }
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006598 // Mask out compression flag from String's array length.
6599 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006600 __ Lsr(out, out, 1u);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006601 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006602}
6603
Artem Serov2bbc9532016-10-21 11:51:50 +01006604void LocationsBuilderARMVIXL::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Artem Serov2bbc9532016-10-21 11:51:50 +01006605 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006606 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Artem Serov2bbc9532016-10-21 11:51:50 +01006607
6608 locations->SetInAt(0, Location::RequiresRegister());
6609 locations->SetInAt(1, Location::RegisterOrConstant(instruction->GetOffset()));
6610 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6611}
6612
6613void InstructionCodeGeneratorARMVIXL::VisitIntermediateAddress(HIntermediateAddress* instruction) {
6614 vixl32::Register out = OutputRegister(instruction);
6615 vixl32::Register first = InputRegisterAt(instruction, 0);
6616 Location second = instruction->GetLocations()->InAt(1);
6617
Artem Serov2bbc9532016-10-21 11:51:50 +01006618 if (second.IsRegister()) {
6619 __ Add(out, first, RegisterFrom(second));
6620 } else {
Anton Kirilov644032c2016-12-06 17:51:43 +00006621 __ Add(out, first, Int32ConstantFrom(second));
Artem Serov2bbc9532016-10-21 11:51:50 +01006622 }
6623}
6624
Artem Serove1811ed2017-04-27 16:50:47 +01006625void LocationsBuilderARMVIXL::VisitIntermediateAddressIndex(
6626 HIntermediateAddressIndex* instruction) {
6627 LOG(FATAL) << "Unreachable " << instruction->GetId();
6628}
6629
6630void InstructionCodeGeneratorARMVIXL::VisitIntermediateAddressIndex(
6631 HIntermediateAddressIndex* instruction) {
6632 LOG(FATAL) << "Unreachable " << instruction->GetId();
6633}
6634
Scott Wakelingc34dba72016-10-03 10:14:44 +01006635void LocationsBuilderARMVIXL::VisitBoundsCheck(HBoundsCheck* instruction) {
6636 RegisterSet caller_saves = RegisterSet::Empty();
6637 InvokeRuntimeCallingConventionARMVIXL calling_convention;
6638 caller_saves.Add(LocationFrom(calling_convention.GetRegisterAt(0)));
6639 caller_saves.Add(LocationFrom(calling_convention.GetRegisterAt(1)));
6640 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Artem Serov2dd053d2017-03-08 14:54:06 +00006641
6642 HInstruction* index = instruction->InputAt(0);
6643 HInstruction* length = instruction->InputAt(1);
6644 // If both index and length are constants we can statically check the bounds. But if at least one
6645 // of them is not encodable ArmEncodableConstantOrRegister will create
6646 // Location::RequiresRegister() which is not desired to happen. Instead we create constant
6647 // locations.
6648 bool both_const = index->IsConstant() && length->IsConstant();
6649 locations->SetInAt(0, both_const
6650 ? Location::ConstantLocation(index->AsConstant())
6651 : ArmEncodableConstantOrRegister(index, CMP));
6652 locations->SetInAt(1, both_const
6653 ? Location::ConstantLocation(length->AsConstant())
6654 : ArmEncodableConstantOrRegister(length, CMP));
Scott Wakelingc34dba72016-10-03 10:14:44 +01006655}
6656
6657void InstructionCodeGeneratorARMVIXL::VisitBoundsCheck(HBoundsCheck* instruction) {
Artem Serov2dd053d2017-03-08 14:54:06 +00006658 LocationSummary* locations = instruction->GetLocations();
6659 Location index_loc = locations->InAt(0);
6660 Location length_loc = locations->InAt(1);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006661
Artem Serov2dd053d2017-03-08 14:54:06 +00006662 if (length_loc.IsConstant()) {
6663 int32_t length = Int32ConstantFrom(length_loc);
6664 if (index_loc.IsConstant()) {
6665 // BCE will remove the bounds check if we are guaranteed to pass.
6666 int32_t index = Int32ConstantFrom(index_loc);
6667 if (index < 0 || index >= length) {
6668 SlowPathCodeARMVIXL* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006669 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathARMVIXL(instruction);
Artem Serov2dd053d2017-03-08 14:54:06 +00006670 codegen_->AddSlowPath(slow_path);
6671 __ B(slow_path->GetEntryLabel());
6672 } else {
6673 // Some optimization after BCE may have generated this, and we should not
6674 // generate a bounds check if it is a valid range.
6675 }
6676 return;
6677 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006678
Artem Serov2dd053d2017-03-08 14:54:06 +00006679 SlowPathCodeARMVIXL* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006680 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathARMVIXL(instruction);
Artem Serov2dd053d2017-03-08 14:54:06 +00006681 __ Cmp(RegisterFrom(index_loc), length);
6682 codegen_->AddSlowPath(slow_path);
6683 __ B(hs, slow_path->GetEntryLabel());
6684 } else {
6685 SlowPathCodeARMVIXL* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006686 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathARMVIXL(instruction);
Artem Serov2dd053d2017-03-08 14:54:06 +00006687 __ Cmp(RegisterFrom(length_loc), InputOperandAt(instruction, 0));
6688 codegen_->AddSlowPath(slow_path);
6689 __ B(ls, slow_path->GetEntryLabel());
6690 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006691}
6692
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006693void CodeGeneratorARMVIXL::MarkGCCard(vixl32::Register temp,
6694 vixl32::Register card,
6695 vixl32::Register object,
6696 vixl32::Register value,
6697 bool can_be_null) {
6698 vixl32::Label is_null;
6699 if (can_be_null) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00006700 __ CompareAndBranchIfZero(value, &is_null);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006701 }
6702 GetAssembler()->LoadFromOffset(
6703 kLoadWord, card, tr, Thread::CardTableOffset<kArmPointerSize>().Int32Value());
Scott Wakelingb77051e2016-11-21 19:46:00 +00006704 __ Lsr(temp, object, Operand::From(gc::accounting::CardTable::kCardShift));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006705 __ Strb(card, MemOperand(card, temp));
6706 if (can_be_null) {
6707 __ Bind(&is_null);
6708 }
6709}
6710
Scott Wakelingfe885462016-09-22 10:24:38 +01006711void LocationsBuilderARMVIXL::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
6712 LOG(FATAL) << "Unreachable";
6713}
6714
6715void InstructionCodeGeneratorARMVIXL::VisitParallelMove(HParallelMove* instruction) {
Vladimir Markobea75ff2017-10-11 20:39:54 +01006716 if (instruction->GetNext()->IsSuspendCheck() &&
6717 instruction->GetBlock()->GetLoopInformation() != nullptr) {
6718 HSuspendCheck* suspend_check = instruction->GetNext()->AsSuspendCheck();
6719 // The back edge will generate the suspend check.
6720 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(suspend_check, instruction);
6721 }
6722
Scott Wakelingfe885462016-09-22 10:24:38 +01006723 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
6724}
6725
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006726void LocationsBuilderARMVIXL::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006727 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6728 instruction, LocationSummary::kCallOnSlowPath);
Artem Serov657022c2016-11-23 14:19:38 +00006729 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006730}
6731
6732void InstructionCodeGeneratorARMVIXL::VisitSuspendCheck(HSuspendCheck* instruction) {
6733 HBasicBlock* block = instruction->GetBlock();
6734 if (block->GetLoopInformation() != nullptr) {
6735 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
6736 // The back edge will generate the suspend check.
6737 return;
6738 }
6739 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
6740 // The goto will generate the suspend check.
6741 return;
6742 }
6743 GenerateSuspendCheck(instruction, nullptr);
Roland Levillain5daa4952017-07-03 17:23:56 +01006744 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 12);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006745}
6746
6747void InstructionCodeGeneratorARMVIXL::GenerateSuspendCheck(HSuspendCheck* instruction,
6748 HBasicBlock* successor) {
6749 SuspendCheckSlowPathARMVIXL* slow_path =
6750 down_cast<SuspendCheckSlowPathARMVIXL*>(instruction->GetSlowPath());
6751 if (slow_path == nullptr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006752 slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006753 new (codegen_->GetScopedAllocator()) SuspendCheckSlowPathARMVIXL(instruction, successor);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006754 instruction->SetSlowPath(slow_path);
6755 codegen_->AddSlowPath(slow_path);
6756 if (successor != nullptr) {
6757 DCHECK(successor->IsLoopHeader());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006758 }
6759 } else {
6760 DCHECK_EQ(slow_path->GetSuccessor(), successor);
6761 }
6762
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006763 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006764 vixl32::Register temp = temps.Acquire();
6765 GetAssembler()->LoadFromOffset(
6766 kLoadUnsignedHalfword, temp, tr, Thread::ThreadFlagsOffset<kArmPointerSize>().Int32Value());
6767 if (successor == nullptr) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00006768 __ CompareAndBranchIfNonZero(temp, slow_path->GetEntryLabel());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006769 __ Bind(slow_path->GetReturnLabel());
6770 } else {
xueliang.zhongf51bc622016-11-04 09:23:32 +00006771 __ CompareAndBranchIfZero(temp, codegen_->GetLabelOf(successor));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006772 __ B(slow_path->GetEntryLabel());
6773 }
6774}
6775
Scott Wakelingfe885462016-09-22 10:24:38 +01006776ArmVIXLAssembler* ParallelMoveResolverARMVIXL::GetAssembler() const {
6777 return codegen_->GetAssembler();
6778}
6779
6780void ParallelMoveResolverARMVIXL::EmitMove(size_t index) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006781 UseScratchRegisterScope temps(GetAssembler()->GetVIXLAssembler());
Scott Wakelingfe885462016-09-22 10:24:38 +01006782 MoveOperands* move = moves_[index];
6783 Location source = move->GetSource();
6784 Location destination = move->GetDestination();
6785
6786 if (source.IsRegister()) {
6787 if (destination.IsRegister()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006788 __ Mov(RegisterFrom(destination), RegisterFrom(source));
Scott Wakelingfe885462016-09-22 10:24:38 +01006789 } else if (destination.IsFpuRegister()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006790 __ Vmov(SRegisterFrom(destination), RegisterFrom(source));
Scott Wakelingfe885462016-09-22 10:24:38 +01006791 } else {
6792 DCHECK(destination.IsStackSlot());
6793 GetAssembler()->StoreToOffset(kStoreWord,
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006794 RegisterFrom(source),
Scott Wakelingfe885462016-09-22 10:24:38 +01006795 sp,
6796 destination.GetStackIndex());
6797 }
6798 } else if (source.IsStackSlot()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006799 if (destination.IsRegister()) {
6800 GetAssembler()->LoadFromOffset(kLoadWord,
6801 RegisterFrom(destination),
6802 sp,
6803 source.GetStackIndex());
6804 } else if (destination.IsFpuRegister()) {
6805 GetAssembler()->LoadSFromOffset(SRegisterFrom(destination), sp, source.GetStackIndex());
6806 } else {
6807 DCHECK(destination.IsStackSlot());
6808 vixl32::Register temp = temps.Acquire();
6809 GetAssembler()->LoadFromOffset(kLoadWord, temp, sp, source.GetStackIndex());
6810 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
6811 }
Scott Wakelingfe885462016-09-22 10:24:38 +01006812 } else if (source.IsFpuRegister()) {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01006813 if (destination.IsRegister()) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006814 __ Vmov(RegisterFrom(destination), SRegisterFrom(source));
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01006815 } else if (destination.IsFpuRegister()) {
6816 __ Vmov(SRegisterFrom(destination), SRegisterFrom(source));
6817 } else {
6818 DCHECK(destination.IsStackSlot());
6819 GetAssembler()->StoreSToOffset(SRegisterFrom(source), sp, destination.GetStackIndex());
6820 }
Scott Wakelingfe885462016-09-22 10:24:38 +01006821 } else if (source.IsDoubleStackSlot()) {
Alexandre Rames9c19bd62016-10-24 11:50:32 +01006822 if (destination.IsDoubleStackSlot()) {
6823 vixl32::DRegister temp = temps.AcquireD();
6824 GetAssembler()->LoadDFromOffset(temp, sp, source.GetStackIndex());
6825 GetAssembler()->StoreDToOffset(temp, sp, destination.GetStackIndex());
6826 } else if (destination.IsRegisterPair()) {
6827 DCHECK(ExpectedPairLayout(destination));
6828 GetAssembler()->LoadFromOffset(
6829 kLoadWordPair, LowRegisterFrom(destination), sp, source.GetStackIndex());
6830 } else {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01006831 DCHECK(destination.IsFpuRegisterPair()) << destination;
6832 GetAssembler()->LoadDFromOffset(DRegisterFrom(destination), sp, source.GetStackIndex());
Alexandre Rames9c19bd62016-10-24 11:50:32 +01006833 }
Scott Wakelingfe885462016-09-22 10:24:38 +01006834 } else if (source.IsRegisterPair()) {
6835 if (destination.IsRegisterPair()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006836 __ Mov(LowRegisterFrom(destination), LowRegisterFrom(source));
6837 __ Mov(HighRegisterFrom(destination), HighRegisterFrom(source));
Scott Wakelingfe885462016-09-22 10:24:38 +01006838 } else if (destination.IsFpuRegisterPair()) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006839 __ Vmov(DRegisterFrom(destination), LowRegisterFrom(source), HighRegisterFrom(source));
Scott Wakelingfe885462016-09-22 10:24:38 +01006840 } else {
6841 DCHECK(destination.IsDoubleStackSlot()) << destination;
6842 DCHECK(ExpectedPairLayout(source));
6843 GetAssembler()->StoreToOffset(kStoreWordPair,
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006844 LowRegisterFrom(source),
Scott Wakelingfe885462016-09-22 10:24:38 +01006845 sp,
6846 destination.GetStackIndex());
6847 }
6848 } else if (source.IsFpuRegisterPair()) {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01006849 if (destination.IsRegisterPair()) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006850 __ Vmov(LowRegisterFrom(destination), HighRegisterFrom(destination), DRegisterFrom(source));
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01006851 } else if (destination.IsFpuRegisterPair()) {
6852 __ Vmov(DRegisterFrom(destination), DRegisterFrom(source));
6853 } else {
6854 DCHECK(destination.IsDoubleStackSlot()) << destination;
6855 GetAssembler()->StoreDToOffset(DRegisterFrom(source), sp, destination.GetStackIndex());
6856 }
Scott Wakelingfe885462016-09-22 10:24:38 +01006857 } else {
6858 DCHECK(source.IsConstant()) << source;
6859 HConstant* constant = source.GetConstant();
6860 if (constant->IsIntConstant() || constant->IsNullConstant()) {
6861 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
6862 if (destination.IsRegister()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006863 __ Mov(RegisterFrom(destination), value);
Scott Wakelingfe885462016-09-22 10:24:38 +01006864 } else {
6865 DCHECK(destination.IsStackSlot());
Scott Wakelingfe885462016-09-22 10:24:38 +01006866 vixl32::Register temp = temps.Acquire();
6867 __ Mov(temp, value);
6868 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
6869 }
6870 } else if (constant->IsLongConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006871 int64_t value = Int64ConstantFrom(source);
Scott Wakelingfe885462016-09-22 10:24:38 +01006872 if (destination.IsRegisterPair()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006873 __ Mov(LowRegisterFrom(destination), Low32Bits(value));
6874 __ Mov(HighRegisterFrom(destination), High32Bits(value));
Scott Wakelingfe885462016-09-22 10:24:38 +01006875 } else {
6876 DCHECK(destination.IsDoubleStackSlot()) << destination;
Scott Wakelingfe885462016-09-22 10:24:38 +01006877 vixl32::Register temp = temps.Acquire();
6878 __ Mov(temp, Low32Bits(value));
6879 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
6880 __ Mov(temp, High32Bits(value));
6881 GetAssembler()->StoreToOffset(kStoreWord,
6882 temp,
6883 sp,
6884 destination.GetHighStackIndex(kArmWordSize));
6885 }
6886 } else if (constant->IsDoubleConstant()) {
6887 double value = constant->AsDoubleConstant()->GetValue();
6888 if (destination.IsFpuRegisterPair()) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006889 __ Vmov(DRegisterFrom(destination), value);
Scott Wakelingfe885462016-09-22 10:24:38 +01006890 } else {
6891 DCHECK(destination.IsDoubleStackSlot()) << destination;
6892 uint64_t int_value = bit_cast<uint64_t, double>(value);
Scott Wakelingfe885462016-09-22 10:24:38 +01006893 vixl32::Register temp = temps.Acquire();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006894 __ Mov(temp, Low32Bits(int_value));
Scott Wakelingfe885462016-09-22 10:24:38 +01006895 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006896 __ Mov(temp, High32Bits(int_value));
Scott Wakelingfe885462016-09-22 10:24:38 +01006897 GetAssembler()->StoreToOffset(kStoreWord,
6898 temp,
6899 sp,
6900 destination.GetHighStackIndex(kArmWordSize));
6901 }
6902 } else {
6903 DCHECK(constant->IsFloatConstant()) << constant->DebugName();
6904 float value = constant->AsFloatConstant()->GetValue();
6905 if (destination.IsFpuRegister()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006906 __ Vmov(SRegisterFrom(destination), value);
Scott Wakelingfe885462016-09-22 10:24:38 +01006907 } else {
6908 DCHECK(destination.IsStackSlot());
Scott Wakelingfe885462016-09-22 10:24:38 +01006909 vixl32::Register temp = temps.Acquire();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006910 __ Mov(temp, bit_cast<int32_t, float>(value));
Scott Wakelingfe885462016-09-22 10:24:38 +01006911 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
6912 }
6913 }
6914 }
6915}
6916
Alexandre Rames9c19bd62016-10-24 11:50:32 +01006917void ParallelMoveResolverARMVIXL::Exchange(vixl32::Register reg, int mem) {
6918 UseScratchRegisterScope temps(GetAssembler()->GetVIXLAssembler());
6919 vixl32::Register temp = temps.Acquire();
6920 __ Mov(temp, reg);
6921 GetAssembler()->LoadFromOffset(kLoadWord, reg, sp, mem);
6922 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, mem);
Scott Wakelingfe885462016-09-22 10:24:38 +01006923}
6924
Alexandre Rames9c19bd62016-10-24 11:50:32 +01006925void ParallelMoveResolverARMVIXL::Exchange(int mem1, int mem2) {
6926 // TODO(VIXL32): Double check the performance of this implementation.
6927 UseScratchRegisterScope temps(GetAssembler()->GetVIXLAssembler());
Nicolas Geoffray13a797b2017-03-15 16:41:31 +00006928 vixl32::Register temp1 = temps.Acquire();
6929 ScratchRegisterScope ensure_scratch(
6930 this, temp1.GetCode(), r0.GetCode(), codegen_->GetNumberOfCoreRegisters());
6931 vixl32::Register temp2(ensure_scratch.GetRegister());
Alexandre Rames9c19bd62016-10-24 11:50:32 +01006932
Nicolas Geoffray13a797b2017-03-15 16:41:31 +00006933 int stack_offset = ensure_scratch.IsSpilled() ? kArmWordSize : 0;
6934 GetAssembler()->LoadFromOffset(kLoadWord, temp1, sp, mem1 + stack_offset);
6935 GetAssembler()->LoadFromOffset(kLoadWord, temp2, sp, mem2 + stack_offset);
6936 GetAssembler()->StoreToOffset(kStoreWord, temp1, sp, mem2 + stack_offset);
6937 GetAssembler()->StoreToOffset(kStoreWord, temp2, sp, mem1 + stack_offset);
Scott Wakelingfe885462016-09-22 10:24:38 +01006938}
6939
Alexandre Rames9c19bd62016-10-24 11:50:32 +01006940void ParallelMoveResolverARMVIXL::EmitSwap(size_t index) {
6941 MoveOperands* move = moves_[index];
6942 Location source = move->GetSource();
6943 Location destination = move->GetDestination();
6944 UseScratchRegisterScope temps(GetAssembler()->GetVIXLAssembler());
6945
6946 if (source.IsRegister() && destination.IsRegister()) {
6947 vixl32::Register temp = temps.Acquire();
6948 DCHECK(!RegisterFrom(source).Is(temp));
6949 DCHECK(!RegisterFrom(destination).Is(temp));
6950 __ Mov(temp, RegisterFrom(destination));
6951 __ Mov(RegisterFrom(destination), RegisterFrom(source));
6952 __ Mov(RegisterFrom(source), temp);
6953 } else if (source.IsRegister() && destination.IsStackSlot()) {
6954 Exchange(RegisterFrom(source), destination.GetStackIndex());
6955 } else if (source.IsStackSlot() && destination.IsRegister()) {
6956 Exchange(RegisterFrom(destination), source.GetStackIndex());
6957 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Anton Kirilovdda43962016-11-21 19:55:20 +00006958 Exchange(source.GetStackIndex(), destination.GetStackIndex());
Alexandre Rames9c19bd62016-10-24 11:50:32 +01006959 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Nicolas Geoffray13a797b2017-03-15 16:41:31 +00006960 vixl32::Register temp = temps.Acquire();
Anton Kirilovdda43962016-11-21 19:55:20 +00006961 __ Vmov(temp, SRegisterFrom(source));
6962 __ Vmov(SRegisterFrom(source), SRegisterFrom(destination));
6963 __ Vmov(SRegisterFrom(destination), temp);
Alexandre Rames9c19bd62016-10-24 11:50:32 +01006964 } else if (source.IsRegisterPair() && destination.IsRegisterPair()) {
6965 vixl32::DRegister temp = temps.AcquireD();
6966 __ Vmov(temp, LowRegisterFrom(source), HighRegisterFrom(source));
6967 __ Mov(LowRegisterFrom(source), LowRegisterFrom(destination));
6968 __ Mov(HighRegisterFrom(source), HighRegisterFrom(destination));
6969 __ Vmov(LowRegisterFrom(destination), HighRegisterFrom(destination), temp);
6970 } else if (source.IsRegisterPair() || destination.IsRegisterPair()) {
6971 vixl32::Register low_reg = LowRegisterFrom(source.IsRegisterPair() ? source : destination);
6972 int mem = source.IsRegisterPair() ? destination.GetStackIndex() : source.GetStackIndex();
6973 DCHECK(ExpectedPairLayout(source.IsRegisterPair() ? source : destination));
6974 vixl32::DRegister temp = temps.AcquireD();
6975 __ Vmov(temp, low_reg, vixl32::Register(low_reg.GetCode() + 1));
6976 GetAssembler()->LoadFromOffset(kLoadWordPair, low_reg, sp, mem);
6977 GetAssembler()->StoreDToOffset(temp, sp, mem);
6978 } else if (source.IsFpuRegisterPair() && destination.IsFpuRegisterPair()) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006979 vixl32::DRegister first = DRegisterFrom(source);
6980 vixl32::DRegister second = DRegisterFrom(destination);
6981 vixl32::DRegister temp = temps.AcquireD();
6982 __ Vmov(temp, first);
6983 __ Vmov(first, second);
6984 __ Vmov(second, temp);
Alexandre Rames9c19bd62016-10-24 11:50:32 +01006985 } else if (source.IsFpuRegisterPair() || destination.IsFpuRegisterPair()) {
Anton Kirilovdda43962016-11-21 19:55:20 +00006986 vixl32::DRegister reg = source.IsFpuRegisterPair()
6987 ? DRegisterFrom(source)
6988 : DRegisterFrom(destination);
6989 int mem = source.IsFpuRegisterPair()
6990 ? destination.GetStackIndex()
6991 : source.GetStackIndex();
6992 vixl32::DRegister temp = temps.AcquireD();
6993 __ Vmov(temp, reg);
6994 GetAssembler()->LoadDFromOffset(reg, sp, mem);
6995 GetAssembler()->StoreDToOffset(temp, sp, mem);
Alexandre Rames9c19bd62016-10-24 11:50:32 +01006996 } else if (source.IsFpuRegister() || destination.IsFpuRegister()) {
Anton Kirilovdda43962016-11-21 19:55:20 +00006997 vixl32::SRegister reg = source.IsFpuRegister()
6998 ? SRegisterFrom(source)
6999 : SRegisterFrom(destination);
7000 int mem = source.IsFpuRegister()
7001 ? destination.GetStackIndex()
7002 : source.GetStackIndex();
7003 vixl32::Register temp = temps.Acquire();
7004 __ Vmov(temp, reg);
7005 GetAssembler()->LoadSFromOffset(reg, sp, mem);
7006 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, mem);
Alexandre Rames9c19bd62016-10-24 11:50:32 +01007007 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
7008 vixl32::DRegister temp1 = temps.AcquireD();
7009 vixl32::DRegister temp2 = temps.AcquireD();
7010 __ Vldr(temp1, MemOperand(sp, source.GetStackIndex()));
7011 __ Vldr(temp2, MemOperand(sp, destination.GetStackIndex()));
7012 __ Vstr(temp1, MemOperand(sp, destination.GetStackIndex()));
7013 __ Vstr(temp2, MemOperand(sp, source.GetStackIndex()));
7014 } else {
7015 LOG(FATAL) << "Unimplemented" << source << " <-> " << destination;
7016 }
Scott Wakelingfe885462016-09-22 10:24:38 +01007017}
7018
Nicolas Geoffray13a797b2017-03-15 16:41:31 +00007019void ParallelMoveResolverARMVIXL::SpillScratch(int reg) {
7020 __ Push(vixl32::Register(reg));
Scott Wakelingfe885462016-09-22 10:24:38 +01007021}
7022
Nicolas Geoffray13a797b2017-03-15 16:41:31 +00007023void ParallelMoveResolverARMVIXL::RestoreScratch(int reg) {
7024 __ Pop(vixl32::Register(reg));
Scott Wakelingfe885462016-09-22 10:24:38 +01007025}
7026
Artem Serov02d37832016-10-25 15:25:33 +01007027HLoadClass::LoadKind CodeGeneratorARMVIXL::GetSupportedLoadClassKind(
Artem Serovd4cc5b22016-11-04 11:19:09 +00007028 HLoadClass::LoadKind desired_class_load_kind) {
7029 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00007030 case HLoadClass::LoadKind::kInvalid:
7031 LOG(FATAL) << "UNREACHABLE";
7032 UNREACHABLE();
Artem Serovd4cc5b22016-11-04 11:19:09 +00007033 case HLoadClass::LoadKind::kReferrersClass:
7034 break;
Artem Serovd4cc5b22016-11-04 11:19:09 +00007035 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko94ec2db2017-09-06 17:21:03 +01007036 case HLoadClass::LoadKind::kBootImageClassTable:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007037 case HLoadClass::LoadKind::kBssEntry:
7038 DCHECK(!Runtime::Current()->UseJitCompilation());
7039 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007040 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007041 DCHECK(Runtime::Current()->UseJitCompilation());
Artem Serovc5fcb442016-12-02 19:19:58 +00007042 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01007043 case HLoadClass::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007044 case HLoadClass::LoadKind::kRuntimeCall:
Artem Serovd4cc5b22016-11-04 11:19:09 +00007045 break;
7046 }
7047 return desired_class_load_kind;
Artem Serov02d37832016-10-25 15:25:33 +01007048}
7049
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007050void LocationsBuilderARMVIXL::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00007051 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007052 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007053 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00007054 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007055 cls,
7056 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00007057 LocationFrom(r0));
Vladimir Markoea4c1262017-02-06 19:59:33 +00007058 DCHECK(calling_convention.GetRegisterAt(0).Is(r0));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007059 return;
7060 }
Vladimir Marko41559982017-01-06 14:04:23 +00007061 DCHECK(!cls->NeedsAccessCheck());
Scott Wakelingfe885462016-09-22 10:24:38 +01007062
Artem Serovd4cc5b22016-11-04 11:19:09 +00007063 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
7064 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007065 ? LocationSummary::kCallOnSlowPath
7066 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01007067 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(cls, call_kind);
Artem Serovd4cc5b22016-11-04 11:19:09 +00007068 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007069 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Artem Serovd4cc5b22016-11-04 11:19:09 +00007070 }
7071
Vladimir Marko41559982017-01-06 14:04:23 +00007072 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007073 locations->SetInAt(0, Location::RequiresRegister());
7074 }
7075 locations->SetOut(Location::RequiresRegister());
Vladimir Markoea4c1262017-02-06 19:59:33 +00007076 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
7077 if (!kUseReadBarrier || kUseBakerReadBarrier) {
7078 // Rely on the type resolution or initialization and marking to save everything we need.
Vladimir Markoea4c1262017-02-06 19:59:33 +00007079 RegisterSet caller_saves = RegisterSet::Empty();
7080 InvokeRuntimeCallingConventionARMVIXL calling_convention;
7081 caller_saves.Add(LocationFrom(calling_convention.GetRegisterAt(0)));
7082 // TODO: Add GetReturnLocation() to the calling convention so that we can DCHECK()
7083 // that the the kPrimNot result register is the same as the first argument register.
7084 locations->SetCustomSlowPathCallerSaves(caller_saves);
7085 } else {
7086 // For non-Baker read barrier we have a temp-clobbering call.
7087 }
7088 }
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01007089 if (kUseBakerReadBarrier && kBakerReadBarrierLinkTimeThunksEnableForGcRoots) {
7090 if (load_kind == HLoadClass::LoadKind::kBssEntry ||
7091 (load_kind == HLoadClass::LoadKind::kReferrersClass &&
7092 !Runtime::Current()->UseJitCompilation())) {
7093 locations->AddTemp(Location::RegisterLocation(kBakerCcEntrypointRegister.GetCode()));
7094 }
7095 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007096}
7097
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007098// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
7099// move.
7100void InstructionCodeGeneratorARMVIXL::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00007101 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007102 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00007103 codegen_->GenerateLoadClassRuntimeCall(cls);
Roland Levillain5daa4952017-07-03 17:23:56 +01007104 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 13);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007105 return;
7106 }
Vladimir Marko41559982017-01-06 14:04:23 +00007107 DCHECK(!cls->NeedsAccessCheck());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007108
Vladimir Marko41559982017-01-06 14:04:23 +00007109 LocationSummary* locations = cls->GetLocations();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007110 Location out_loc = locations->Out();
7111 vixl32::Register out = OutputRegister(cls);
7112
Artem Serovd4cc5b22016-11-04 11:19:09 +00007113 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
7114 ? kWithoutReadBarrier
7115 : kCompilerReadBarrierOption;
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007116 bool generate_null_check = false;
Vladimir Marko41559982017-01-06 14:04:23 +00007117 switch (load_kind) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007118 case HLoadClass::LoadKind::kReferrersClass: {
7119 DCHECK(!cls->CanCallRuntime());
7120 DCHECK(!cls->MustGenerateClinitCheck());
7121 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
7122 vixl32::Register current_method = InputRegisterAt(cls, 0);
7123 GenerateGcRootFieldLoad(cls,
7124 out_loc,
7125 current_method,
Roland Levillain00468f32016-10-27 18:02:48 +01007126 ArtMethod::DeclaringClassOffset().Int32Value(),
Artem Serovd4cc5b22016-11-04 11:19:09 +00007127 read_barrier_option);
7128 break;
7129 }
Artem Serovd4cc5b22016-11-04 11:19:09 +00007130 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007131 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Artem Serovd4cc5b22016-11-04 11:19:09 +00007132 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
7133 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
7134 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
7135 codegen_->EmitMovwMovtPlaceholder(labels, out);
7136 break;
7137 }
7138 case HLoadClass::LoadKind::kBootImageAddress: {
Artem Serovc5fcb442016-12-02 19:19:58 +00007139 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007140 uint32_t address = dchecked_integral_cast<uint32_t>(
7141 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
7142 DCHECK_NE(address, 0u);
Artem Serovc5fcb442016-12-02 19:19:58 +00007143 __ Ldr(out, codegen_->DeduplicateBootImageAddressLiteral(address));
Artem Serovd4cc5b22016-11-04 11:19:09 +00007144 break;
7145 }
Vladimir Marko94ec2db2017-09-06 17:21:03 +01007146 case HLoadClass::LoadKind::kBootImageClassTable: {
7147 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
7148 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
7149 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
7150 codegen_->EmitMovwMovtPlaceholder(labels, out);
7151 __ Ldr(out, MemOperand(out, /* offset */ 0));
7152 // Extract the reference from the slot data, i.e. clear the hash bits.
7153 int32_t masked_hash = ClassTable::TableSlot::MaskHash(
7154 ComputeModifiedUtf8Hash(cls->GetDexFile().StringByTypeIdx(cls->GetTypeIndex())));
7155 if (masked_hash != 0) {
7156 __ Sub(out, out, Operand(masked_hash));
7157 }
7158 break;
7159 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007160 case HLoadClass::LoadKind::kBssEntry: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007161 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
Vladimir Marko1998cd02017-01-13 13:02:58 +00007162 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex());
Vladimir Markof3c52b42017-11-17 17:32:12 +00007163 codegen_->EmitMovwMovtPlaceholder(labels, out);
7164 GenerateGcRootFieldLoad(cls, out_loc, out, /* offset */ 0, read_barrier_option);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007165 generate_null_check = true;
7166 break;
7167 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007168 case HLoadClass::LoadKind::kJitTableAddress: {
Artem Serovc5fcb442016-12-02 19:19:58 +00007169 __ Ldr(out, codegen_->DeduplicateJitClassLiteral(cls->GetDexFile(),
7170 cls->GetTypeIndex(),
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007171 cls->GetClass()));
Artem Serovc5fcb442016-12-02 19:19:58 +00007172 // /* GcRoot<mirror::Class> */ out = *out
Vladimir Markoea4c1262017-02-06 19:59:33 +00007173 GenerateGcRootFieldLoad(cls, out_loc, out, /* offset */ 0, read_barrier_option);
Artem Serovd4cc5b22016-11-04 11:19:09 +00007174 break;
7175 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007176 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00007177 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00007178 LOG(FATAL) << "UNREACHABLE";
7179 UNREACHABLE();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007180 }
7181
7182 if (generate_null_check || cls->MustGenerateClinitCheck()) {
7183 DCHECK(cls->CanCallRuntime());
Vladimir Marko174b2e22017-10-12 13:34:49 +01007184 LoadClassSlowPathARMVIXL* slow_path =
7185 new (codegen_->GetScopedAllocator()) LoadClassSlowPathARMVIXL(
7186 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007187 codegen_->AddSlowPath(slow_path);
7188 if (generate_null_check) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00007189 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007190 }
7191 if (cls->MustGenerateClinitCheck()) {
7192 GenerateClassInitializationCheck(slow_path, out);
7193 } else {
7194 __ Bind(slow_path->GetExitLabel());
7195 }
Roland Levillain5daa4952017-07-03 17:23:56 +01007196 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 14);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007197 }
7198}
7199
Artem Serov02d37832016-10-25 15:25:33 +01007200void LocationsBuilderARMVIXL::VisitClinitCheck(HClinitCheck* check) {
7201 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007202 new (GetGraph()->GetAllocator()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
Artem Serov02d37832016-10-25 15:25:33 +01007203 locations->SetInAt(0, Location::RequiresRegister());
7204 if (check->HasUses()) {
7205 locations->SetOut(Location::SameAsFirstInput());
7206 }
7207}
7208
7209void InstructionCodeGeneratorARMVIXL::VisitClinitCheck(HClinitCheck* check) {
7210 // We assume the class is not null.
7211 LoadClassSlowPathARMVIXL* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01007212 new (codegen_->GetScopedAllocator()) LoadClassSlowPathARMVIXL(check->GetLoadClass(),
7213 check,
7214 check->GetDexPc(),
7215 /* do_clinit */ true);
Artem Serov02d37832016-10-25 15:25:33 +01007216 codegen_->AddSlowPath(slow_path);
7217 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
7218}
7219
7220void InstructionCodeGeneratorARMVIXL::GenerateClassInitializationCheck(
7221 LoadClassSlowPathARMVIXL* slow_path, vixl32::Register class_reg) {
7222 UseScratchRegisterScope temps(GetVIXLAssembler());
7223 vixl32::Register temp = temps.Acquire();
Vladimir Markodc682aa2018-01-04 18:42:57 +00007224 constexpr size_t status_lsb_position = SubtypeCheckBits::BitStructSizeOf();
7225 const size_t status_byte_offset =
7226 mirror::Class::StatusOffset().SizeValue() + (status_lsb_position / kBitsPerByte);
7227 constexpr uint32_t shifted_initialized_value =
7228 enum_cast<uint32_t>(ClassStatus::kInitialized) << (status_lsb_position % kBitsPerByte);
7229
7230 GetAssembler()->LoadFromOffset(kLoadUnsignedByte, temp, class_reg, status_byte_offset);
7231 __ Cmp(temp, shifted_initialized_value);
Vladimir Marko2c64a832018-01-04 11:31:56 +00007232 __ B(lo, slow_path->GetEntryLabel());
Artem Serov02d37832016-10-25 15:25:33 +01007233 // Even if the initialized flag is set, we may be in a situation where caches are not synced
7234 // properly. Therefore, we do a memory fence.
7235 __ Dmb(ISH);
7236 __ Bind(slow_path->GetExitLabel());
7237}
7238
Artem Serov02d37832016-10-25 15:25:33 +01007239HLoadString::LoadKind CodeGeneratorARMVIXL::GetSupportedLoadStringKind(
Artem Serovd4cc5b22016-11-04 11:19:09 +00007240 HLoadString::LoadKind desired_string_load_kind) {
7241 switch (desired_string_load_kind) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00007242 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007243 case HLoadString::LoadKind::kBootImageInternTable:
Artem Serovd4cc5b22016-11-04 11:19:09 +00007244 case HLoadString::LoadKind::kBssEntry:
7245 DCHECK(!Runtime::Current()->UseJitCompilation());
7246 break;
7247 case HLoadString::LoadKind::kJitTableAddress:
7248 DCHECK(Runtime::Current()->UseJitCompilation());
Artem Serovc5fcb442016-12-02 19:19:58 +00007249 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01007250 case HLoadString::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007251 case HLoadString::LoadKind::kRuntimeCall:
Artem Serovd4cc5b22016-11-04 11:19:09 +00007252 break;
7253 }
7254 return desired_string_load_kind;
Artem Serov02d37832016-10-25 15:25:33 +01007255}
7256
7257void LocationsBuilderARMVIXL::VisitLoadString(HLoadString* load) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00007258 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Vladimir Markoca6fff82017-10-03 14:49:14 +01007259 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(load, call_kind);
Artem Serov02d37832016-10-25 15:25:33 +01007260 HLoadString::LoadKind load_kind = load->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007261 if (load_kind == HLoadString::LoadKind::kRuntimeCall) {
Artem Serov02d37832016-10-25 15:25:33 +01007262 locations->SetOut(LocationFrom(r0));
7263 } else {
7264 locations->SetOut(Location::RequiresRegister());
Artem Serovd4cc5b22016-11-04 11:19:09 +00007265 if (load_kind == HLoadString::LoadKind::kBssEntry) {
7266 if (!kUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00007267 // Rely on the pResolveString and marking to save everything we need, including temps.
Artem Serovd4cc5b22016-11-04 11:19:09 +00007268 RegisterSet caller_saves = RegisterSet::Empty();
7269 InvokeRuntimeCallingConventionARMVIXL calling_convention;
7270 caller_saves.Add(LocationFrom(calling_convention.GetRegisterAt(0)));
7271 // TODO: Add GetReturnLocation() to the calling convention so that we can DCHECK()
7272 // that the the kPrimNot result register is the same as the first argument register.
7273 locations->SetCustomSlowPathCallerSaves(caller_saves);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01007274 if (kUseBakerReadBarrier && kBakerReadBarrierLinkTimeThunksEnableForGcRoots) {
7275 locations->AddTemp(Location::RegisterLocation(kBakerCcEntrypointRegister.GetCode()));
7276 }
Artem Serovd4cc5b22016-11-04 11:19:09 +00007277 } else {
7278 // For non-Baker read barrier we have a temp-clobbering call.
7279 }
7280 }
Artem Serov02d37832016-10-25 15:25:33 +01007281 }
7282}
7283
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007284// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
7285// move.
7286void InstructionCodeGeneratorARMVIXL::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Artem Serovd4cc5b22016-11-04 11:19:09 +00007287 LocationSummary* locations = load->GetLocations();
7288 Location out_loc = locations->Out();
7289 vixl32::Register out = OutputRegister(load);
7290 HLoadString::LoadKind load_kind = load->GetLoadKind();
7291
7292 switch (load_kind) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00007293 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
7294 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
7295 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007296 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Artem Serovd4cc5b22016-11-04 11:19:09 +00007297 codegen_->EmitMovwMovtPlaceholder(labels, out);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007298 return;
Artem Serovd4cc5b22016-11-04 11:19:09 +00007299 }
7300 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007301 uint32_t address = dchecked_integral_cast<uint32_t>(
7302 reinterpret_cast<uintptr_t>(load->GetString().Get()));
7303 DCHECK_NE(address, 0u);
Artem Serovc5fcb442016-12-02 19:19:58 +00007304 __ Ldr(out, codegen_->DeduplicateBootImageAddressLiteral(address));
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007305 return;
7306 }
7307 case HLoadString::LoadKind::kBootImageInternTable: {
7308 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
7309 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
7310 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
7311 codegen_->EmitMovwMovtPlaceholder(labels, out);
7312 __ Ldr(out, MemOperand(out, /* offset */ 0));
7313 return;
Artem Serovd4cc5b22016-11-04 11:19:09 +00007314 }
7315 case HLoadString::LoadKind::kBssEntry: {
7316 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Artem Serovd4cc5b22016-11-04 11:19:09 +00007317 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007318 codegen_->NewStringBssEntryPatch(load->GetDexFile(), load->GetStringIndex());
Vladimir Markof3c52b42017-11-17 17:32:12 +00007319 codegen_->EmitMovwMovtPlaceholder(labels, out);
7320 GenerateGcRootFieldLoad(load, out_loc, out, /* offset */ 0, kCompilerReadBarrierOption);
Artem Serovd4cc5b22016-11-04 11:19:09 +00007321 LoadStringSlowPathARMVIXL* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01007322 new (codegen_->GetScopedAllocator()) LoadStringSlowPathARMVIXL(load);
Artem Serovd4cc5b22016-11-04 11:19:09 +00007323 codegen_->AddSlowPath(slow_path);
7324 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
7325 __ Bind(slow_path->GetExitLabel());
Roland Levillain5daa4952017-07-03 17:23:56 +01007326 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 15);
Artem Serovd4cc5b22016-11-04 11:19:09 +00007327 return;
7328 }
7329 case HLoadString::LoadKind::kJitTableAddress: {
Artem Serovc5fcb442016-12-02 19:19:58 +00007330 __ Ldr(out, codegen_->DeduplicateJitStringLiteral(load->GetDexFile(),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007331 load->GetStringIndex(),
7332 load->GetString()));
Artem Serovc5fcb442016-12-02 19:19:58 +00007333 // /* GcRoot<mirror::String> */ out = *out
7334 GenerateGcRootFieldLoad(load, out_loc, out, /* offset */ 0, kCompilerReadBarrierOption);
7335 return;
Artem Serovd4cc5b22016-11-04 11:19:09 +00007336 }
7337 default:
7338 break;
7339 }
Artem Serov02d37832016-10-25 15:25:33 +01007340
7341 // TODO: Re-add the compiler code to do string dex cache lookup again.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007342 DCHECK_EQ(load->GetLoadKind(), HLoadString::LoadKind::kRuntimeCall);
Artem Serov02d37832016-10-25 15:25:33 +01007343 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Andreas Gampe8a0128a2016-11-28 07:38:35 -08007344 __ Mov(calling_convention.GetRegisterAt(0), load->GetStringIndex().index_);
Artem Serov02d37832016-10-25 15:25:33 +01007345 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
7346 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Roland Levillain5daa4952017-07-03 17:23:56 +01007347 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 16);
Artem Serov02d37832016-10-25 15:25:33 +01007348}
7349
7350static int32_t GetExceptionTlsOffset() {
7351 return Thread::ExceptionOffset<kArmPointerSize>().Int32Value();
7352}
7353
7354void LocationsBuilderARMVIXL::VisitLoadException(HLoadException* load) {
7355 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007356 new (GetGraph()->GetAllocator()) LocationSummary(load, LocationSummary::kNoCall);
Artem Serov02d37832016-10-25 15:25:33 +01007357 locations->SetOut(Location::RequiresRegister());
7358}
7359
7360void InstructionCodeGeneratorARMVIXL::VisitLoadException(HLoadException* load) {
7361 vixl32::Register out = OutputRegister(load);
7362 GetAssembler()->LoadFromOffset(kLoadWord, out, tr, GetExceptionTlsOffset());
7363}
7364
7365
7366void LocationsBuilderARMVIXL::VisitClearException(HClearException* clear) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007367 new (GetGraph()->GetAllocator()) LocationSummary(clear, LocationSummary::kNoCall);
Artem Serov02d37832016-10-25 15:25:33 +01007368}
7369
7370void InstructionCodeGeneratorARMVIXL::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
7371 UseScratchRegisterScope temps(GetVIXLAssembler());
7372 vixl32::Register temp = temps.Acquire();
7373 __ Mov(temp, 0);
7374 GetAssembler()->StoreToOffset(kStoreWord, temp, tr, GetExceptionTlsOffset());
7375}
7376
7377void LocationsBuilderARMVIXL::VisitThrow(HThrow* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007378 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
7379 instruction, LocationSummary::kCallOnMainOnly);
Artem Serov02d37832016-10-25 15:25:33 +01007380 InvokeRuntimeCallingConventionARMVIXL calling_convention;
7381 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
7382}
7383
7384void InstructionCodeGeneratorARMVIXL::VisitThrow(HThrow* instruction) {
7385 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
7386 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
7387}
7388
Artem Serov657022c2016-11-23 14:19:38 +00007389// Temp is used for read barrier.
7390static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
7391 if (kEmitCompilerReadBarrier &&
7392 (kUseBakerReadBarrier ||
7393 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
7394 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
7395 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
7396 return 1;
7397 }
7398 return 0;
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007399}
7400
Artem Serov657022c2016-11-23 14:19:38 +00007401// Interface case has 3 temps, one for holding the number of interfaces, one for the current
7402// interface pointer, one for loading the current interface.
7403// The other checks have one temp for loading the object's class.
7404static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
7405 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
7406 return 3;
7407 }
7408 return 1 + NumberOfInstanceOfTemps(type_check_kind);
7409}
Artem Serovcfbe9132016-10-14 15:58:56 +01007410
7411void LocationsBuilderARMVIXL::VisitInstanceOf(HInstanceOf* instruction) {
7412 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
7413 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
7414 bool baker_read_barrier_slow_path = false;
7415 switch (type_check_kind) {
7416 case TypeCheckKind::kExactCheck:
7417 case TypeCheckKind::kAbstractClassCheck:
7418 case TypeCheckKind::kClassHierarchyCheck:
Vladimir Marko87584542017-12-12 17:47:52 +00007419 case TypeCheckKind::kArrayObjectCheck: {
7420 bool needs_read_barrier = CodeGenerator::InstanceOfNeedsReadBarrier(instruction);
7421 call_kind = needs_read_barrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
7422 baker_read_barrier_slow_path = kUseBakerReadBarrier && needs_read_barrier;
Artem Serovcfbe9132016-10-14 15:58:56 +01007423 break;
Vladimir Marko87584542017-12-12 17:47:52 +00007424 }
Artem Serovcfbe9132016-10-14 15:58:56 +01007425 case TypeCheckKind::kArrayCheck:
7426 case TypeCheckKind::kUnresolvedCheck:
7427 case TypeCheckKind::kInterfaceCheck:
7428 call_kind = LocationSummary::kCallOnSlowPath;
7429 break;
7430 }
7431
Vladimir Markoca6fff82017-10-03 14:49:14 +01007432 LocationSummary* locations =
7433 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Artem Serovcfbe9132016-10-14 15:58:56 +01007434 if (baker_read_barrier_slow_path) {
7435 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
7436 }
7437 locations->SetInAt(0, Location::RequiresRegister());
7438 locations->SetInAt(1, Location::RequiresRegister());
7439 // The "out" register is used as a temporary, so it overlaps with the inputs.
7440 // Note that TypeCheckSlowPathARM uses this register too.
7441 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Artem Serov657022c2016-11-23 14:19:38 +00007442 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01007443 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
7444 codegen_->MaybeAddBakerCcEntrypointTempForFields(locations);
7445 }
Artem Serovcfbe9132016-10-14 15:58:56 +01007446}
7447
7448void InstructionCodeGeneratorARMVIXL::VisitInstanceOf(HInstanceOf* instruction) {
7449 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
7450 LocationSummary* locations = instruction->GetLocations();
7451 Location obj_loc = locations->InAt(0);
7452 vixl32::Register obj = InputRegisterAt(instruction, 0);
7453 vixl32::Register cls = InputRegisterAt(instruction, 1);
7454 Location out_loc = locations->Out();
7455 vixl32::Register out = OutputRegister(instruction);
Artem Serov657022c2016-11-23 14:19:38 +00007456 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
7457 DCHECK_LE(num_temps, 1u);
7458 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Artem Serovcfbe9132016-10-14 15:58:56 +01007459 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
7460 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
7461 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
7462 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007463 vixl32::Label done;
7464 vixl32::Label* const final_label = codegen_->GetFinalLabel(instruction, &done);
Artem Serovcfbe9132016-10-14 15:58:56 +01007465 SlowPathCodeARMVIXL* slow_path = nullptr;
7466
7467 // Return 0 if `obj` is null.
7468 // avoid null check if we know obj is not null.
7469 if (instruction->MustDoNullCheck()) {
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007470 DCHECK(!out.Is(obj));
7471 __ Mov(out, 0);
7472 __ CompareAndBranchIfZero(obj, final_label, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007473 }
7474
Artem Serovcfbe9132016-10-14 15:58:56 +01007475 switch (type_check_kind) {
7476 case TypeCheckKind::kExactCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00007477 ReadBarrierOption read_barrier_option =
7478 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier6beced42016-11-15 15:51:31 -08007479 // /* HeapReference<Class> */ out = obj->klass_
7480 GenerateReferenceLoadTwoRegisters(instruction,
7481 out_loc,
7482 obj_loc,
7483 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00007484 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007485 read_barrier_option);
Artem Serovcfbe9132016-10-14 15:58:56 +01007486 // Classes must be equal for the instanceof to succeed.
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007487 __ Cmp(out, cls);
7488 // We speculatively set the result to false without changing the condition
7489 // flags, which allows us to avoid some branching later.
7490 __ Mov(LeaveFlags, out, 0);
7491
7492 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
7493 // we check that the output is in a low register, so that a 16-bit MOV
7494 // encoding can be used.
7495 if (out.IsLow()) {
7496 // We use the scope because of the IT block that follows.
7497 ExactAssemblyScope guard(GetVIXLAssembler(),
7498 2 * vixl32::k16BitT32InstructionSizeInBytes,
7499 CodeBufferCheckScope::kExactSize);
7500
7501 __ it(eq);
7502 __ mov(eq, out, 1);
7503 } else {
7504 __ B(ne, final_label, /* far_target */ false);
7505 __ Mov(out, 1);
7506 }
7507
Artem Serovcfbe9132016-10-14 15:58:56 +01007508 break;
7509 }
7510
7511 case TypeCheckKind::kAbstractClassCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00007512 ReadBarrierOption read_barrier_option =
7513 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier6beced42016-11-15 15:51:31 -08007514 // /* HeapReference<Class> */ out = obj->klass_
7515 GenerateReferenceLoadTwoRegisters(instruction,
7516 out_loc,
7517 obj_loc,
7518 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00007519 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007520 read_barrier_option);
Artem Serovcfbe9132016-10-14 15:58:56 +01007521 // If the class is abstract, we eagerly fetch the super class of the
7522 // object to avoid doing a comparison we know will fail.
7523 vixl32::Label loop;
7524 __ Bind(&loop);
7525 // /* HeapReference<Class> */ out = out->super_class_
Artem Serov657022c2016-11-23 14:19:38 +00007526 GenerateReferenceLoadOneRegister(instruction,
7527 out_loc,
7528 super_offset,
7529 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007530 read_barrier_option);
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007531 // If `out` is null, we use it for the result, and jump to the final label.
Anton Kirilov6f644202017-02-27 18:29:45 +00007532 __ CompareAndBranchIfZero(out, final_label, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007533 __ Cmp(out, cls);
Artem Serov517d9f62016-12-12 15:51:15 +00007534 __ B(ne, &loop, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007535 __ Mov(out, 1);
Artem Serovcfbe9132016-10-14 15:58:56 +01007536 break;
7537 }
7538
7539 case TypeCheckKind::kClassHierarchyCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00007540 ReadBarrierOption read_barrier_option =
7541 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier6beced42016-11-15 15:51:31 -08007542 // /* HeapReference<Class> */ out = obj->klass_
7543 GenerateReferenceLoadTwoRegisters(instruction,
7544 out_loc,
7545 obj_loc,
7546 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00007547 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007548 read_barrier_option);
Artem Serovcfbe9132016-10-14 15:58:56 +01007549 // Walk over the class hierarchy to find a match.
7550 vixl32::Label loop, success;
7551 __ Bind(&loop);
7552 __ Cmp(out, cls);
Artem Serov517d9f62016-12-12 15:51:15 +00007553 __ B(eq, &success, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007554 // /* HeapReference<Class> */ out = out->super_class_
Artem Serov657022c2016-11-23 14:19:38 +00007555 GenerateReferenceLoadOneRegister(instruction,
7556 out_loc,
7557 super_offset,
7558 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007559 read_barrier_option);
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007560 // This is essentially a null check, but it sets the condition flags to the
7561 // proper value for the code that follows the loop, i.e. not `eq`.
7562 __ Cmp(out, 1);
7563 __ B(hs, &loop, /* far_target */ false);
7564
7565 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
7566 // we check that the output is in a low register, so that a 16-bit MOV
7567 // encoding can be used.
7568 if (out.IsLow()) {
7569 // If `out` is null, we use it for the result, and the condition flags
7570 // have already been set to `ne`, so the IT block that comes afterwards
7571 // (and which handles the successful case) turns into a NOP (instead of
7572 // overwriting `out`).
7573 __ Bind(&success);
7574
7575 // We use the scope because of the IT block that follows.
7576 ExactAssemblyScope guard(GetVIXLAssembler(),
7577 2 * vixl32::k16BitT32InstructionSizeInBytes,
7578 CodeBufferCheckScope::kExactSize);
7579
7580 // There is only one branch to the `success` label (which is bound to this
7581 // IT block), and it has the same condition, `eq`, so in that case the MOV
7582 // is executed.
7583 __ it(eq);
7584 __ mov(eq, out, 1);
7585 } else {
7586 // If `out` is null, we use it for the result, and jump to the final label.
Anton Kirilov6f644202017-02-27 18:29:45 +00007587 __ B(final_label);
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007588 __ Bind(&success);
7589 __ Mov(out, 1);
Artem Serovcfbe9132016-10-14 15:58:56 +01007590 }
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007591
Artem Serovcfbe9132016-10-14 15:58:56 +01007592 break;
7593 }
7594
7595 case TypeCheckKind::kArrayObjectCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00007596 ReadBarrierOption read_barrier_option =
7597 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier6beced42016-11-15 15:51:31 -08007598 // /* HeapReference<Class> */ out = obj->klass_
7599 GenerateReferenceLoadTwoRegisters(instruction,
7600 out_loc,
7601 obj_loc,
7602 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00007603 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007604 read_barrier_option);
Artem Serovcfbe9132016-10-14 15:58:56 +01007605 // Do an exact check.
7606 vixl32::Label exact_check;
7607 __ Cmp(out, cls);
Artem Serov517d9f62016-12-12 15:51:15 +00007608 __ B(eq, &exact_check, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007609 // Otherwise, we need to check that the object's class is a non-primitive array.
7610 // /* HeapReference<Class> */ out = out->component_type_
Artem Serov657022c2016-11-23 14:19:38 +00007611 GenerateReferenceLoadOneRegister(instruction,
7612 out_loc,
7613 component_offset,
7614 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007615 read_barrier_option);
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007616 // If `out` is null, we use it for the result, and jump to the final label.
Anton Kirilov6f644202017-02-27 18:29:45 +00007617 __ CompareAndBranchIfZero(out, final_label, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007618 GetAssembler()->LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
7619 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007620 __ Cmp(out, 0);
7621 // We speculatively set the result to false without changing the condition
7622 // flags, which allows us to avoid some branching later.
7623 __ Mov(LeaveFlags, out, 0);
7624
7625 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
7626 // we check that the output is in a low register, so that a 16-bit MOV
7627 // encoding can be used.
7628 if (out.IsLow()) {
7629 __ Bind(&exact_check);
7630
7631 // We use the scope because of the IT block that follows.
7632 ExactAssemblyScope guard(GetVIXLAssembler(),
7633 2 * vixl32::k16BitT32InstructionSizeInBytes,
7634 CodeBufferCheckScope::kExactSize);
7635
7636 __ it(eq);
7637 __ mov(eq, out, 1);
7638 } else {
7639 __ B(ne, final_label, /* far_target */ false);
7640 __ Bind(&exact_check);
7641 __ Mov(out, 1);
7642 }
7643
Artem Serovcfbe9132016-10-14 15:58:56 +01007644 break;
7645 }
7646
7647 case TypeCheckKind::kArrayCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00007648 // No read barrier since the slow path will retry upon failure.
Mathieu Chartier6beced42016-11-15 15:51:31 -08007649 // /* HeapReference<Class> */ out = obj->klass_
7650 GenerateReferenceLoadTwoRegisters(instruction,
7651 out_loc,
7652 obj_loc,
7653 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00007654 maybe_temp_loc,
7655 kWithoutReadBarrier);
Artem Serovcfbe9132016-10-14 15:58:56 +01007656 __ Cmp(out, cls);
7657 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01007658 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathARMVIXL(
7659 instruction, /* is_fatal */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007660 codegen_->AddSlowPath(slow_path);
7661 __ B(ne, slow_path->GetEntryLabel());
7662 __ Mov(out, 1);
Artem Serovcfbe9132016-10-14 15:58:56 +01007663 break;
7664 }
7665
7666 case TypeCheckKind::kUnresolvedCheck:
7667 case TypeCheckKind::kInterfaceCheck: {
7668 // Note that we indeed only call on slow path, but we always go
7669 // into the slow path for the unresolved and interface check
7670 // cases.
7671 //
7672 // We cannot directly call the InstanceofNonTrivial runtime
7673 // entry point without resorting to a type checking slow path
7674 // here (i.e. by calling InvokeRuntime directly), as it would
7675 // require to assign fixed registers for the inputs of this
7676 // HInstanceOf instruction (following the runtime calling
7677 // convention), which might be cluttered by the potential first
7678 // read barrier emission at the beginning of this method.
7679 //
7680 // TODO: Introduce a new runtime entry point taking the object
7681 // to test (instead of its class) as argument, and let it deal
7682 // with the read barrier issues. This will let us refactor this
7683 // case of the `switch` code as it was previously (with a direct
7684 // call to the runtime not using a type checking slow path).
7685 // This should also be beneficial for the other cases above.
7686 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01007687 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathARMVIXL(
7688 instruction, /* is_fatal */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007689 codegen_->AddSlowPath(slow_path);
7690 __ B(slow_path->GetEntryLabel());
Artem Serovcfbe9132016-10-14 15:58:56 +01007691 break;
7692 }
7693 }
7694
Artem Serovcfbe9132016-10-14 15:58:56 +01007695 if (done.IsReferenced()) {
7696 __ Bind(&done);
7697 }
7698
7699 if (slow_path != nullptr) {
7700 __ Bind(slow_path->GetExitLabel());
7701 }
7702}
7703
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007704void LocationsBuilderARMVIXL::VisitCheckCast(HCheckCast* instruction) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007705 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko87584542017-12-12 17:47:52 +00007706 LocationSummary::CallKind call_kind = CodeGenerator::GetCheckCastCallKind(instruction);
Vladimir Markoca6fff82017-10-03 14:49:14 +01007707 LocationSummary* locations =
7708 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007709 locations->SetInAt(0, Location::RequiresRegister());
7710 locations->SetInAt(1, Location::RequiresRegister());
Artem Serov657022c2016-11-23 14:19:38 +00007711 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007712}
7713
7714void InstructionCodeGeneratorARMVIXL::VisitCheckCast(HCheckCast* instruction) {
7715 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
7716 LocationSummary* locations = instruction->GetLocations();
7717 Location obj_loc = locations->InAt(0);
7718 vixl32::Register obj = InputRegisterAt(instruction, 0);
7719 vixl32::Register cls = InputRegisterAt(instruction, 1);
7720 Location temp_loc = locations->GetTemp(0);
7721 vixl32::Register temp = RegisterFrom(temp_loc);
Artem Serov657022c2016-11-23 14:19:38 +00007722 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
7723 DCHECK_LE(num_temps, 3u);
7724 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
7725 Location maybe_temp3_loc = (num_temps >= 3) ? locations->GetTemp(2) : Location::NoLocation();
7726 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
7727 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
7728 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
7729 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
7730 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
7731 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
7732 const uint32_t object_array_data_offset =
7733 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007734
Vladimir Marko87584542017-12-12 17:47:52 +00007735 bool is_type_check_slow_path_fatal = CodeGenerator::IsTypeCheckSlowPathFatal(instruction);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007736 SlowPathCodeARMVIXL* type_check_slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01007737 new (codegen_->GetScopedAllocator()) TypeCheckSlowPathARMVIXL(
7738 instruction, is_type_check_slow_path_fatal);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007739 codegen_->AddSlowPath(type_check_slow_path);
7740
7741 vixl32::Label done;
Anton Kirilov6f644202017-02-27 18:29:45 +00007742 vixl32::Label* final_label = codegen_->GetFinalLabel(instruction, &done);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007743 // Avoid null check if we know obj is not null.
7744 if (instruction->MustDoNullCheck()) {
Anton Kirilov6f644202017-02-27 18:29:45 +00007745 __ CompareAndBranchIfZero(obj, final_label, /* far_target */ false);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007746 }
7747
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007748 switch (type_check_kind) {
7749 case TypeCheckKind::kExactCheck:
7750 case TypeCheckKind::kArrayCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00007751 // /* HeapReference<Class> */ temp = obj->klass_
7752 GenerateReferenceLoadTwoRegisters(instruction,
7753 temp_loc,
7754 obj_loc,
7755 class_offset,
7756 maybe_temp2_loc,
7757 kWithoutReadBarrier);
7758
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007759 __ Cmp(temp, cls);
7760 // Jump to slow path for throwing the exception or doing a
7761 // more involved array check.
7762 __ B(ne, type_check_slow_path->GetEntryLabel());
7763 break;
7764 }
7765
7766 case TypeCheckKind::kAbstractClassCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00007767 // /* HeapReference<Class> */ temp = obj->klass_
7768 GenerateReferenceLoadTwoRegisters(instruction,
7769 temp_loc,
7770 obj_loc,
7771 class_offset,
7772 maybe_temp2_loc,
7773 kWithoutReadBarrier);
7774
Artem Serovcfbe9132016-10-14 15:58:56 +01007775 // If the class is abstract, we eagerly fetch the super class of the
7776 // object to avoid doing a comparison we know will fail.
7777 vixl32::Label loop;
7778 __ Bind(&loop);
7779 // /* HeapReference<Class> */ temp = temp->super_class_
Artem Serov657022c2016-11-23 14:19:38 +00007780 GenerateReferenceLoadOneRegister(instruction,
7781 temp_loc,
7782 super_offset,
7783 maybe_temp2_loc,
7784 kWithoutReadBarrier);
Artem Serovcfbe9132016-10-14 15:58:56 +01007785
7786 // If the class reference currently in `temp` is null, jump to the slow path to throw the
7787 // exception.
xueliang.zhongf51bc622016-11-04 09:23:32 +00007788 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
Artem Serovcfbe9132016-10-14 15:58:56 +01007789
7790 // Otherwise, compare the classes.
7791 __ Cmp(temp, cls);
Artem Serov517d9f62016-12-12 15:51:15 +00007792 __ B(ne, &loop, /* far_target */ false);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007793 break;
7794 }
7795
7796 case TypeCheckKind::kClassHierarchyCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00007797 // /* HeapReference<Class> */ temp = obj->klass_
7798 GenerateReferenceLoadTwoRegisters(instruction,
7799 temp_loc,
7800 obj_loc,
7801 class_offset,
7802 maybe_temp2_loc,
7803 kWithoutReadBarrier);
7804
Artem Serovcfbe9132016-10-14 15:58:56 +01007805 // Walk over the class hierarchy to find a match.
7806 vixl32::Label loop;
7807 __ Bind(&loop);
7808 __ Cmp(temp, cls);
Anton Kirilov6f644202017-02-27 18:29:45 +00007809 __ B(eq, final_label, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007810
7811 // /* HeapReference<Class> */ temp = temp->super_class_
Artem Serov657022c2016-11-23 14:19:38 +00007812 GenerateReferenceLoadOneRegister(instruction,
7813 temp_loc,
7814 super_offset,
7815 maybe_temp2_loc,
7816 kWithoutReadBarrier);
Artem Serovcfbe9132016-10-14 15:58:56 +01007817
7818 // If the class reference currently in `temp` is null, jump to the slow path to throw the
7819 // exception.
xueliang.zhongf51bc622016-11-04 09:23:32 +00007820 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
Artem Serovcfbe9132016-10-14 15:58:56 +01007821 // Otherwise, jump to the beginning of the loop.
7822 __ B(&loop);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007823 break;
7824 }
7825
Artem Serovcfbe9132016-10-14 15:58:56 +01007826 case TypeCheckKind::kArrayObjectCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00007827 // /* HeapReference<Class> */ temp = obj->klass_
7828 GenerateReferenceLoadTwoRegisters(instruction,
7829 temp_loc,
7830 obj_loc,
7831 class_offset,
7832 maybe_temp2_loc,
7833 kWithoutReadBarrier);
7834
Artem Serovcfbe9132016-10-14 15:58:56 +01007835 // Do an exact check.
7836 __ Cmp(temp, cls);
Anton Kirilov6f644202017-02-27 18:29:45 +00007837 __ B(eq, final_label, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007838
7839 // Otherwise, we need to check that the object's class is a non-primitive array.
7840 // /* HeapReference<Class> */ temp = temp->component_type_
Artem Serov657022c2016-11-23 14:19:38 +00007841 GenerateReferenceLoadOneRegister(instruction,
7842 temp_loc,
7843 component_offset,
7844 maybe_temp2_loc,
7845 kWithoutReadBarrier);
Artem Serovcfbe9132016-10-14 15:58:56 +01007846 // If the component type is null, jump to the slow path to throw the exception.
xueliang.zhongf51bc622016-11-04 09:23:32 +00007847 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
Artem Serovcfbe9132016-10-14 15:58:56 +01007848 // Otherwise,the object is indeed an array, jump to label `check_non_primitive_component_type`
7849 // to further check that this component type is not a primitive type.
7850 GetAssembler()->LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007851 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
xueliang.zhongf51bc622016-11-04 09:23:32 +00007852 __ CompareAndBranchIfNonZero(temp, type_check_slow_path->GetEntryLabel());
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007853 break;
7854 }
7855
7856 case TypeCheckKind::kUnresolvedCheck:
Artem Serov657022c2016-11-23 14:19:38 +00007857 // We always go into the type check slow path for the unresolved check case.
Artem Serovcfbe9132016-10-14 15:58:56 +01007858 // We cannot directly call the CheckCast runtime entry point
7859 // without resorting to a type checking slow path here (i.e. by
7860 // calling InvokeRuntime directly), as it would require to
7861 // assign fixed registers for the inputs of this HInstanceOf
7862 // instruction (following the runtime calling convention), which
7863 // might be cluttered by the potential first read barrier
7864 // emission at the beginning of this method.
Artem Serov657022c2016-11-23 14:19:38 +00007865
Artem Serovcfbe9132016-10-14 15:58:56 +01007866 __ B(type_check_slow_path->GetEntryLabel());
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007867 break;
Artem Serov657022c2016-11-23 14:19:38 +00007868
7869 case TypeCheckKind::kInterfaceCheck: {
7870 // Avoid read barriers to improve performance of the fast path. We can not get false
7871 // positives by doing this.
7872 // /* HeapReference<Class> */ temp = obj->klass_
7873 GenerateReferenceLoadTwoRegisters(instruction,
7874 temp_loc,
7875 obj_loc,
7876 class_offset,
7877 maybe_temp2_loc,
7878 kWithoutReadBarrier);
7879
7880 // /* HeapReference<Class> */ temp = temp->iftable_
7881 GenerateReferenceLoadTwoRegisters(instruction,
7882 temp_loc,
7883 temp_loc,
7884 iftable_offset,
7885 maybe_temp2_loc,
7886 kWithoutReadBarrier);
7887 // Iftable is never null.
7888 __ Ldr(RegisterFrom(maybe_temp2_loc), MemOperand(temp, array_length_offset));
7889 // Loop through the iftable and check if any class matches.
7890 vixl32::Label start_loop;
7891 __ Bind(&start_loop);
7892 __ CompareAndBranchIfZero(RegisterFrom(maybe_temp2_loc),
7893 type_check_slow_path->GetEntryLabel());
7894 __ Ldr(RegisterFrom(maybe_temp3_loc), MemOperand(temp, object_array_data_offset));
7895 GetAssembler()->MaybeUnpoisonHeapReference(RegisterFrom(maybe_temp3_loc));
7896 // Go to next interface.
7897 __ Add(temp, temp, Operand::From(2 * kHeapReferenceSize));
7898 __ Sub(RegisterFrom(maybe_temp2_loc), RegisterFrom(maybe_temp2_loc), 2);
7899 // Compare the classes and continue the loop if they do not match.
7900 __ Cmp(cls, RegisterFrom(maybe_temp3_loc));
Artem Serov517d9f62016-12-12 15:51:15 +00007901 __ B(ne, &start_loop, /* far_target */ false);
Artem Serov657022c2016-11-23 14:19:38 +00007902 break;
7903 }
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007904 }
Anton Kirilov6f644202017-02-27 18:29:45 +00007905 if (done.IsReferenced()) {
7906 __ Bind(&done);
7907 }
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007908
7909 __ Bind(type_check_slow_path->GetExitLabel());
7910}
7911
Artem Serov551b28f2016-10-18 19:11:30 +01007912void LocationsBuilderARMVIXL::VisitMonitorOperation(HMonitorOperation* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007913 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
7914 instruction, LocationSummary::kCallOnMainOnly);
Artem Serov551b28f2016-10-18 19:11:30 +01007915 InvokeRuntimeCallingConventionARMVIXL calling_convention;
7916 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
7917}
7918
7919void InstructionCodeGeneratorARMVIXL::VisitMonitorOperation(HMonitorOperation* instruction) {
7920 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
7921 instruction,
7922 instruction->GetDexPc());
7923 if (instruction->IsEnter()) {
7924 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
7925 } else {
7926 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
7927 }
Roland Levillain5daa4952017-07-03 17:23:56 +01007928 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 17);
Artem Serov551b28f2016-10-18 19:11:30 +01007929}
7930
Artem Serov02109dd2016-09-23 17:17:54 +01007931void LocationsBuilderARMVIXL::VisitAnd(HAnd* instruction) {
7932 HandleBitwiseOperation(instruction, AND);
7933}
7934
7935void LocationsBuilderARMVIXL::VisitOr(HOr* instruction) {
7936 HandleBitwiseOperation(instruction, ORR);
7937}
7938
7939void LocationsBuilderARMVIXL::VisitXor(HXor* instruction) {
7940 HandleBitwiseOperation(instruction, EOR);
7941}
7942
7943void LocationsBuilderARMVIXL::HandleBitwiseOperation(HBinaryOperation* instruction, Opcode opcode) {
7944 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007945 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007946 DCHECK(instruction->GetResultType() == DataType::Type::kInt32
7947 || instruction->GetResultType() == DataType::Type::kInt64);
Artem Serov02109dd2016-09-23 17:17:54 +01007948 // Note: GVN reorders commutative operations to have the constant on the right hand side.
7949 locations->SetInAt(0, Location::RequiresRegister());
7950 locations->SetInAt(1, ArmEncodableConstantOrRegister(instruction->InputAt(1), opcode));
7951 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
7952}
7953
7954void InstructionCodeGeneratorARMVIXL::VisitAnd(HAnd* instruction) {
7955 HandleBitwiseOperation(instruction);
7956}
7957
7958void InstructionCodeGeneratorARMVIXL::VisitOr(HOr* instruction) {
7959 HandleBitwiseOperation(instruction);
7960}
7961
7962void InstructionCodeGeneratorARMVIXL::VisitXor(HXor* instruction) {
7963 HandleBitwiseOperation(instruction);
7964}
7965
Artem Serov2bbc9532016-10-21 11:51:50 +01007966void LocationsBuilderARMVIXL::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
7967 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007968 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007969 DCHECK(instruction->GetResultType() == DataType::Type::kInt32
7970 || instruction->GetResultType() == DataType::Type::kInt64);
Artem Serov2bbc9532016-10-21 11:51:50 +01007971
7972 locations->SetInAt(0, Location::RequiresRegister());
7973 locations->SetInAt(1, Location::RequiresRegister());
7974 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
7975}
7976
7977void InstructionCodeGeneratorARMVIXL::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
7978 LocationSummary* locations = instruction->GetLocations();
7979 Location first = locations->InAt(0);
7980 Location second = locations->InAt(1);
7981 Location out = locations->Out();
7982
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007983 if (instruction->GetResultType() == DataType::Type::kInt32) {
Artem Serov2bbc9532016-10-21 11:51:50 +01007984 vixl32::Register first_reg = RegisterFrom(first);
7985 vixl32::Register second_reg = RegisterFrom(second);
7986 vixl32::Register out_reg = RegisterFrom(out);
7987
7988 switch (instruction->GetOpKind()) {
7989 case HInstruction::kAnd:
7990 __ Bic(out_reg, first_reg, second_reg);
7991 break;
7992 case HInstruction::kOr:
7993 __ Orn(out_reg, first_reg, second_reg);
7994 break;
7995 // There is no EON on arm.
7996 case HInstruction::kXor:
7997 default:
7998 LOG(FATAL) << "Unexpected instruction " << instruction->DebugName();
7999 UNREACHABLE();
8000 }
8001 return;
8002
8003 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008004 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
Artem Serov2bbc9532016-10-21 11:51:50 +01008005 vixl32::Register first_low = LowRegisterFrom(first);
8006 vixl32::Register first_high = HighRegisterFrom(first);
8007 vixl32::Register second_low = LowRegisterFrom(second);
8008 vixl32::Register second_high = HighRegisterFrom(second);
8009 vixl32::Register out_low = LowRegisterFrom(out);
8010 vixl32::Register out_high = HighRegisterFrom(out);
8011
8012 switch (instruction->GetOpKind()) {
8013 case HInstruction::kAnd:
8014 __ Bic(out_low, first_low, second_low);
8015 __ Bic(out_high, first_high, second_high);
8016 break;
8017 case HInstruction::kOr:
8018 __ Orn(out_low, first_low, second_low);
8019 __ Orn(out_high, first_high, second_high);
8020 break;
8021 // There is no EON on arm.
8022 case HInstruction::kXor:
8023 default:
8024 LOG(FATAL) << "Unexpected instruction " << instruction->DebugName();
8025 UNREACHABLE();
8026 }
8027 }
8028}
8029
Anton Kirilov74234da2017-01-13 14:42:47 +00008030void LocationsBuilderARMVIXL::VisitDataProcWithShifterOp(
8031 HDataProcWithShifterOp* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008032 DCHECK(instruction->GetType() == DataType::Type::kInt32 ||
8033 instruction->GetType() == DataType::Type::kInt64);
Anton Kirilov74234da2017-01-13 14:42:47 +00008034 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008035 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008036 const bool overlap = instruction->GetType() == DataType::Type::kInt64 &&
Anton Kirilov74234da2017-01-13 14:42:47 +00008037 HDataProcWithShifterOp::IsExtensionOp(instruction->GetOpKind());
8038
8039 locations->SetInAt(0, Location::RequiresRegister());
8040 locations->SetInAt(1, Location::RequiresRegister());
8041 locations->SetOut(Location::RequiresRegister(),
8042 overlap ? Location::kOutputOverlap : Location::kNoOutputOverlap);
8043}
8044
8045void InstructionCodeGeneratorARMVIXL::VisitDataProcWithShifterOp(
8046 HDataProcWithShifterOp* instruction) {
8047 const LocationSummary* const locations = instruction->GetLocations();
8048 const HInstruction::InstructionKind kind = instruction->GetInstrKind();
8049 const HDataProcWithShifterOp::OpKind op_kind = instruction->GetOpKind();
8050
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008051 if (instruction->GetType() == DataType::Type::kInt32) {
Anton Kirilov420ee302017-02-21 18:10:26 +00008052 const vixl32::Register first = InputRegisterAt(instruction, 0);
8053 const vixl32::Register output = OutputRegister(instruction);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008054 const vixl32::Register second = instruction->InputAt(1)->GetType() == DataType::Type::kInt64
Anton Kirilov74234da2017-01-13 14:42:47 +00008055 ? LowRegisterFrom(locations->InAt(1))
8056 : InputRegisterAt(instruction, 1);
8057
Anton Kirilov420ee302017-02-21 18:10:26 +00008058 if (HDataProcWithShifterOp::IsExtensionOp(op_kind)) {
8059 DCHECK_EQ(kind, HInstruction::kAdd);
8060
8061 switch (op_kind) {
8062 case HDataProcWithShifterOp::kUXTB:
8063 __ Uxtab(output, first, second);
8064 break;
8065 case HDataProcWithShifterOp::kUXTH:
8066 __ Uxtah(output, first, second);
8067 break;
8068 case HDataProcWithShifterOp::kSXTB:
8069 __ Sxtab(output, first, second);
8070 break;
8071 case HDataProcWithShifterOp::kSXTH:
8072 __ Sxtah(output, first, second);
8073 break;
8074 default:
8075 LOG(FATAL) << "Unexpected operation kind: " << op_kind;
8076 UNREACHABLE();
8077 }
8078 } else {
8079 GenerateDataProcInstruction(kind,
8080 output,
8081 first,
8082 Operand(second,
8083 ShiftFromOpKind(op_kind),
8084 instruction->GetShiftAmount()),
8085 codegen_);
8086 }
Anton Kirilov74234da2017-01-13 14:42:47 +00008087 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008088 DCHECK_EQ(instruction->GetType(), DataType::Type::kInt64);
Anton Kirilov74234da2017-01-13 14:42:47 +00008089
8090 if (HDataProcWithShifterOp::IsExtensionOp(op_kind)) {
8091 const vixl32::Register second = InputRegisterAt(instruction, 1);
8092
8093 DCHECK(!LowRegisterFrom(locations->Out()).Is(second));
8094 GenerateDataProc(kind,
8095 locations->Out(),
8096 locations->InAt(0),
8097 second,
8098 Operand(second, ShiftType::ASR, 31),
8099 codegen_);
8100 } else {
8101 GenerateLongDataProc(instruction, codegen_);
8102 }
8103 }
8104}
8105
Artem Serov02109dd2016-09-23 17:17:54 +01008106// TODO(VIXL): Remove optimizations in the helper when they are implemented in vixl.
8107void InstructionCodeGeneratorARMVIXL::GenerateAndConst(vixl32::Register out,
8108 vixl32::Register first,
8109 uint32_t value) {
8110 // Optimize special cases for individual halfs of `and-long` (`and` is simplified earlier).
8111 if (value == 0xffffffffu) {
8112 if (!out.Is(first)) {
8113 __ Mov(out, first);
8114 }
8115 return;
8116 }
8117 if (value == 0u) {
8118 __ Mov(out, 0);
8119 return;
8120 }
8121 if (GetAssembler()->ShifterOperandCanHold(AND, value)) {
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00008122 __ And(out, first, value);
8123 } else if (GetAssembler()->ShifterOperandCanHold(BIC, ~value)) {
8124 __ Bic(out, first, ~value);
Artem Serov02109dd2016-09-23 17:17:54 +01008125 } else {
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00008126 DCHECK(IsPowerOfTwo(value + 1));
8127 __ Ubfx(out, first, 0, WhichPowerOf2(value + 1));
Artem Serov02109dd2016-09-23 17:17:54 +01008128 }
8129}
8130
8131// TODO(VIXL): Remove optimizations in the helper when they are implemented in vixl.
8132void InstructionCodeGeneratorARMVIXL::GenerateOrrConst(vixl32::Register out,
8133 vixl32::Register first,
8134 uint32_t value) {
8135 // Optimize special cases for individual halfs of `or-long` (`or` is simplified earlier).
8136 if (value == 0u) {
8137 if (!out.Is(first)) {
8138 __ Mov(out, first);
8139 }
8140 return;
8141 }
8142 if (value == 0xffffffffu) {
8143 __ Mvn(out, 0);
8144 return;
8145 }
8146 if (GetAssembler()->ShifterOperandCanHold(ORR, value)) {
8147 __ Orr(out, first, value);
8148 } else {
8149 DCHECK(GetAssembler()->ShifterOperandCanHold(ORN, ~value));
8150 __ Orn(out, first, ~value);
8151 }
8152}
8153
8154// TODO(VIXL): Remove optimizations in the helper when they are implemented in vixl.
8155void InstructionCodeGeneratorARMVIXL::GenerateEorConst(vixl32::Register out,
8156 vixl32::Register first,
8157 uint32_t value) {
8158 // Optimize special case for individual halfs of `xor-long` (`xor` is simplified earlier).
8159 if (value == 0u) {
8160 if (!out.Is(first)) {
8161 __ Mov(out, first);
8162 }
8163 return;
8164 }
8165 __ Eor(out, first, value);
8166}
8167
Anton Kirilovdda43962016-11-21 19:55:20 +00008168void InstructionCodeGeneratorARMVIXL::GenerateAddLongConst(Location out,
8169 Location first,
8170 uint64_t value) {
8171 vixl32::Register out_low = LowRegisterFrom(out);
8172 vixl32::Register out_high = HighRegisterFrom(out);
8173 vixl32::Register first_low = LowRegisterFrom(first);
8174 vixl32::Register first_high = HighRegisterFrom(first);
8175 uint32_t value_low = Low32Bits(value);
8176 uint32_t value_high = High32Bits(value);
8177 if (value_low == 0u) {
8178 if (!out_low.Is(first_low)) {
8179 __ Mov(out_low, first_low);
8180 }
8181 __ Add(out_high, first_high, value_high);
8182 return;
8183 }
8184 __ Adds(out_low, first_low, value_low);
Vladimir Markof0a6a1d2018-01-08 14:23:56 +00008185 if (GetAssembler()->ShifterOperandCanHold(ADC, value_high)) {
Anton Kirilovdda43962016-11-21 19:55:20 +00008186 __ Adc(out_high, first_high, value_high);
Anton Kirilovdda43962016-11-21 19:55:20 +00008187 } else {
Vladimir Markof0a6a1d2018-01-08 14:23:56 +00008188 DCHECK(GetAssembler()->ShifterOperandCanHold(SBC, ~value_high));
8189 __ Sbc(out_high, first_high, ~value_high);
Anton Kirilovdda43962016-11-21 19:55:20 +00008190 }
8191}
8192
Artem Serov02109dd2016-09-23 17:17:54 +01008193void InstructionCodeGeneratorARMVIXL::HandleBitwiseOperation(HBinaryOperation* instruction) {
8194 LocationSummary* locations = instruction->GetLocations();
8195 Location first = locations->InAt(0);
8196 Location second = locations->InAt(1);
8197 Location out = locations->Out();
8198
8199 if (second.IsConstant()) {
8200 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
8201 uint32_t value_low = Low32Bits(value);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008202 if (instruction->GetResultType() == DataType::Type::kInt32) {
Artem Serov02109dd2016-09-23 17:17:54 +01008203 vixl32::Register first_reg = InputRegisterAt(instruction, 0);
8204 vixl32::Register out_reg = OutputRegister(instruction);
8205 if (instruction->IsAnd()) {
8206 GenerateAndConst(out_reg, first_reg, value_low);
8207 } else if (instruction->IsOr()) {
8208 GenerateOrrConst(out_reg, first_reg, value_low);
8209 } else {
8210 DCHECK(instruction->IsXor());
8211 GenerateEorConst(out_reg, first_reg, value_low);
8212 }
8213 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008214 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
Artem Serov02109dd2016-09-23 17:17:54 +01008215 uint32_t value_high = High32Bits(value);
8216 vixl32::Register first_low = LowRegisterFrom(first);
8217 vixl32::Register first_high = HighRegisterFrom(first);
8218 vixl32::Register out_low = LowRegisterFrom(out);
8219 vixl32::Register out_high = HighRegisterFrom(out);
8220 if (instruction->IsAnd()) {
8221 GenerateAndConst(out_low, first_low, value_low);
8222 GenerateAndConst(out_high, first_high, value_high);
8223 } else if (instruction->IsOr()) {
8224 GenerateOrrConst(out_low, first_low, value_low);
8225 GenerateOrrConst(out_high, first_high, value_high);
8226 } else {
8227 DCHECK(instruction->IsXor());
8228 GenerateEorConst(out_low, first_low, value_low);
8229 GenerateEorConst(out_high, first_high, value_high);
8230 }
8231 }
8232 return;
8233 }
8234
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008235 if (instruction->GetResultType() == DataType::Type::kInt32) {
Artem Serov02109dd2016-09-23 17:17:54 +01008236 vixl32::Register first_reg = InputRegisterAt(instruction, 0);
8237 vixl32::Register second_reg = InputRegisterAt(instruction, 1);
8238 vixl32::Register out_reg = OutputRegister(instruction);
8239 if (instruction->IsAnd()) {
8240 __ And(out_reg, first_reg, second_reg);
8241 } else if (instruction->IsOr()) {
8242 __ Orr(out_reg, first_reg, second_reg);
8243 } else {
8244 DCHECK(instruction->IsXor());
8245 __ Eor(out_reg, first_reg, second_reg);
8246 }
8247 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008248 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
Artem Serov02109dd2016-09-23 17:17:54 +01008249 vixl32::Register first_low = LowRegisterFrom(first);
8250 vixl32::Register first_high = HighRegisterFrom(first);
8251 vixl32::Register second_low = LowRegisterFrom(second);
8252 vixl32::Register second_high = HighRegisterFrom(second);
8253 vixl32::Register out_low = LowRegisterFrom(out);
8254 vixl32::Register out_high = HighRegisterFrom(out);
8255 if (instruction->IsAnd()) {
8256 __ And(out_low, first_low, second_low);
8257 __ And(out_high, first_high, second_high);
8258 } else if (instruction->IsOr()) {
8259 __ Orr(out_low, first_low, second_low);
8260 __ Orr(out_high, first_high, second_high);
8261 } else {
8262 DCHECK(instruction->IsXor());
8263 __ Eor(out_low, first_low, second_low);
8264 __ Eor(out_high, first_high, second_high);
8265 }
8266 }
8267}
8268
Artem Serovcfbe9132016-10-14 15:58:56 +01008269void InstructionCodeGeneratorARMVIXL::GenerateReferenceLoadOneRegister(
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008270 HInstruction* instruction,
Artem Serovcfbe9132016-10-14 15:58:56 +01008271 Location out,
8272 uint32_t offset,
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008273 Location maybe_temp,
8274 ReadBarrierOption read_barrier_option) {
Artem Serovcfbe9132016-10-14 15:58:56 +01008275 vixl32::Register out_reg = RegisterFrom(out);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008276 if (read_barrier_option == kWithReadBarrier) {
8277 CHECK(kEmitCompilerReadBarrier);
8278 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
8279 if (kUseBakerReadBarrier) {
8280 // Load with fast path based Baker's read barrier.
8281 // /* HeapReference<Object> */ out = *(out + offset)
8282 codegen_->GenerateFieldLoadWithBakerReadBarrier(
8283 instruction, out, out_reg, offset, maybe_temp, /* needs_null_check */ false);
8284 } else {
8285 // Load with slow path based read barrier.
8286 // Save the value of `out` into `maybe_temp` before overwriting it
8287 // in the following move operation, as we will need it for the
8288 // read barrier below.
8289 __ Mov(RegisterFrom(maybe_temp), out_reg);
8290 // /* HeapReference<Object> */ out = *(out + offset)
8291 GetAssembler()->LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
8292 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
8293 }
Artem Serovcfbe9132016-10-14 15:58:56 +01008294 } else {
8295 // Plain load with no read barrier.
8296 // /* HeapReference<Object> */ out = *(out + offset)
8297 GetAssembler()->LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
8298 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
8299 }
8300}
8301
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008302void InstructionCodeGeneratorARMVIXL::GenerateReferenceLoadTwoRegisters(
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008303 HInstruction* instruction,
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008304 Location out,
8305 Location obj,
8306 uint32_t offset,
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008307 Location maybe_temp,
8308 ReadBarrierOption read_barrier_option) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008309 vixl32::Register out_reg = RegisterFrom(out);
8310 vixl32::Register obj_reg = RegisterFrom(obj);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008311 if (read_barrier_option == kWithReadBarrier) {
8312 CHECK(kEmitCompilerReadBarrier);
8313 if (kUseBakerReadBarrier) {
8314 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
8315 // Load with fast path based Baker's read barrier.
8316 // /* HeapReference<Object> */ out = *(obj + offset)
8317 codegen_->GenerateFieldLoadWithBakerReadBarrier(
8318 instruction, out, obj_reg, offset, maybe_temp, /* needs_null_check */ false);
8319 } else {
8320 // Load with slow path based read barrier.
8321 // /* HeapReference<Object> */ out = *(obj + offset)
8322 GetAssembler()->LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
8323 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
8324 }
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008325 } else {
8326 // Plain load with no read barrier.
8327 // /* HeapReference<Object> */ out = *(obj + offset)
8328 GetAssembler()->LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
8329 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
8330 }
8331}
8332
Scott Wakelinga7812ae2016-10-17 10:03:36 +01008333void InstructionCodeGeneratorARMVIXL::GenerateGcRootFieldLoad(
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008334 HInstruction* instruction,
Scott Wakelinga7812ae2016-10-17 10:03:36 +01008335 Location root,
8336 vixl32::Register obj,
8337 uint32_t offset,
Artem Serovd4cc5b22016-11-04 11:19:09 +00008338 ReadBarrierOption read_barrier_option) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01008339 vixl32::Register root_reg = RegisterFrom(root);
Artem Serovd4cc5b22016-11-04 11:19:09 +00008340 if (read_barrier_option == kWithReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008341 DCHECK(kEmitCompilerReadBarrier);
8342 if (kUseBakerReadBarrier) {
8343 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
Roland Levillainba650a42017-03-06 13:52:32 +00008344 // Baker's read barrier are used.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008345 if (kBakerReadBarrierLinkTimeThunksEnableForGcRoots &&
8346 !Runtime::Current()->UseJitCompilation()) {
Roland Levillain6d729a72017-06-30 18:34:01 +01008347 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in
8348 // the Marking Register) to decide whether we need to enter
8349 // the slow path to mark the GC root.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008350 //
8351 // We use link-time generated thunks for the slow path. That thunk
8352 // checks the reference and jumps to the entrypoint if needed.
8353 //
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008354 // lr = &return_address;
8355 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
Roland Levillain6d729a72017-06-30 18:34:01 +01008356 // if (mr) { // Thread::Current()->GetIsGcMarking()
8357 // goto gc_root_thunk<root_reg>(lr)
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008358 // }
8359 // return_address:
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008360
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008361 UseScratchRegisterScope temps(GetVIXLAssembler());
8362 ExcludeIPAndBakerCcEntrypointRegister(&temps, instruction);
Vladimir Marko88abba22017-05-03 17:09:25 +01008363 bool narrow = CanEmitNarrowLdr(root_reg, obj, offset);
8364 uint32_t custom_data = linker::Thumb2RelativePatcher::EncodeBakerReadBarrierGcRootData(
8365 root_reg.GetCode(), narrow);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008366 vixl32::Label* bne_label = codegen_->NewBakerReadBarrierPatch(custom_data);
Roland Levillainba650a42017-03-06 13:52:32 +00008367
Roland Levillain6d729a72017-06-30 18:34:01 +01008368 vixl::EmissionCheckScope guard(GetVIXLAssembler(), 4 * vixl32::kMaxInstructionSizeInBytes);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008369 vixl32::Label return_address;
8370 EmitAdrCode adr(GetVIXLAssembler(), lr, &return_address);
Roland Levillain6d729a72017-06-30 18:34:01 +01008371 __ cmp(mr, Operand(0));
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008372 // Currently the offset is always within range. If that changes,
8373 // we shall have to split the load the same way as for fields.
8374 DCHECK_LT(offset, kReferenceLoadMinFarOffset);
Vladimir Marko88abba22017-05-03 17:09:25 +01008375 ptrdiff_t old_offset = GetVIXLAssembler()->GetBuffer()->GetCursorOffset();
8376 __ ldr(EncodingSize(narrow ? Narrow : Wide), root_reg, MemOperand(obj, offset));
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008377 EmitPlaceholderBne(codegen_, bne_label);
8378 __ Bind(&return_address);
Vladimir Marko88abba22017-05-03 17:09:25 +01008379 DCHECK_EQ(old_offset - GetVIXLAssembler()->GetBuffer()->GetCursorOffset(),
8380 narrow ? BAKER_MARK_INTROSPECTION_GC_ROOT_LDR_NARROW_OFFSET
8381 : BAKER_MARK_INTROSPECTION_GC_ROOT_LDR_WIDE_OFFSET);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008382 } else {
Roland Levillain6d729a72017-06-30 18:34:01 +01008383 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in
8384 // the Marking Register) to decide whether we need to enter
8385 // the slow path to mark the GC root.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008386 //
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008387 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
Roland Levillain6d729a72017-06-30 18:34:01 +01008388 // if (mr) { // Thread::Current()->GetIsGcMarking()
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008389 // // Slow path.
Roland Levillain6d729a72017-06-30 18:34:01 +01008390 // entrypoint = Thread::Current()->pReadBarrierMarkReg ## root.reg()
8391 // root = entrypoint(root); // root = ReadBarrier::Mark(root); // Entry point call.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008392 // }
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008393
Roland Levillain6d729a72017-06-30 18:34:01 +01008394 // Slow path marking the GC root `root`. The entrypoint will
8395 // be loaded by the slow path code.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008396 SlowPathCodeARMVIXL* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01008397 new (codegen_->GetScopedAllocator()) ReadBarrierMarkSlowPathARMVIXL(instruction, root);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008398 codegen_->AddSlowPath(slow_path);
8399
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008400 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
8401 GetAssembler()->LoadFromOffset(kLoadWord, root_reg, obj, offset);
8402 static_assert(
8403 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
8404 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
8405 "have different sizes.");
8406 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
8407 "art::mirror::CompressedReference<mirror::Object> and int32_t "
8408 "have different sizes.");
8409
Roland Levillain6d729a72017-06-30 18:34:01 +01008410 __ CompareAndBranchIfNonZero(mr, slow_path->GetEntryLabel());
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008411 __ Bind(slow_path->GetExitLabel());
8412 }
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008413 } else {
8414 // GC root loaded through a slow path for read barriers other
8415 // than Baker's.
8416 // /* GcRoot<mirror::Object>* */ root = obj + offset
8417 __ Add(root_reg, obj, offset);
8418 // /* mirror::Object* */ root = root->Read()
8419 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
8420 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01008421 } else {
8422 // Plain GC root load with no read barrier.
8423 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
8424 GetAssembler()->LoadFromOffset(kLoadWord, root_reg, obj, offset);
8425 // Note that GC roots are not affected by heap poisoning, thus we
8426 // do not have to unpoison `root_reg` here.
8427 }
Roland Levillain5daa4952017-07-03 17:23:56 +01008428 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 18);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01008429}
8430
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008431void CodeGeneratorARMVIXL::MaybeAddBakerCcEntrypointTempForFields(LocationSummary* locations) {
8432 DCHECK(kEmitCompilerReadBarrier);
8433 DCHECK(kUseBakerReadBarrier);
8434 if (kBakerReadBarrierLinkTimeThunksEnableForFields) {
8435 if (!Runtime::Current()->UseJitCompilation()) {
8436 locations->AddTemp(Location::RegisterLocation(kBakerCcEntrypointRegister.GetCode()));
8437 }
8438 }
8439}
8440
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008441void CodeGeneratorARMVIXL::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
8442 Location ref,
8443 vixl32::Register obj,
8444 uint32_t offset,
8445 Location temp,
8446 bool needs_null_check) {
8447 DCHECK(kEmitCompilerReadBarrier);
8448 DCHECK(kUseBakerReadBarrier);
8449
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008450 if (kBakerReadBarrierLinkTimeThunksEnableForFields &&
8451 !Runtime::Current()->UseJitCompilation()) {
Roland Levillain6d729a72017-06-30 18:34:01 +01008452 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in the
8453 // Marking Register) to decide whether we need to enter the slow
8454 // path to mark the reference. Then, in the slow path, check the
8455 // gray bit in the lock word of the reference's holder (`obj`) to
8456 // decide whether to mark `ref` or not.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008457 //
8458 // We use link-time generated thunks for the slow path. That thunk checks
8459 // the holder and jumps to the entrypoint if needed. If the holder is not
8460 // gray, it creates a fake dependency and returns to the LDR instruction.
8461 //
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008462 // lr = &gray_return_address;
Roland Levillain6d729a72017-06-30 18:34:01 +01008463 // if (mr) { // Thread::Current()->GetIsGcMarking()
8464 // goto field_thunk<holder_reg, base_reg>(lr)
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008465 // }
8466 // not_gray_return_address:
8467 // // Original reference load. If the offset is too large to fit
8468 // // into LDR, we use an adjusted base register here.
Vladimir Marko88abba22017-05-03 17:09:25 +01008469 // HeapReference<mirror::Object> reference = *(obj+offset);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008470 // gray_return_address:
8471
8472 DCHECK_ALIGNED(offset, sizeof(mirror::HeapReference<mirror::Object>));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008473 vixl32::Register ref_reg = RegisterFrom(ref, DataType::Type::kReference);
Vladimir Marko88abba22017-05-03 17:09:25 +01008474 bool narrow = CanEmitNarrowLdr(ref_reg, obj, offset);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008475 vixl32::Register base = obj;
8476 if (offset >= kReferenceLoadMinFarOffset) {
8477 base = RegisterFrom(temp);
8478 DCHECK(!base.Is(kBakerCcEntrypointRegister));
8479 static_assert(IsPowerOfTwo(kReferenceLoadMinFarOffset), "Expecting a power of 2.");
8480 __ Add(base, obj, Operand(offset & ~(kReferenceLoadMinFarOffset - 1u)));
8481 offset &= (kReferenceLoadMinFarOffset - 1u);
Vladimir Marko88abba22017-05-03 17:09:25 +01008482 // Use narrow LDR only for small offsets. Generating narrow encoding LDR for the large
8483 // offsets with `(offset & (kReferenceLoadMinFarOffset - 1u)) < 32u` would most likely
8484 // increase the overall code size when taking the generated thunks into account.
8485 DCHECK(!narrow);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008486 }
8487 UseScratchRegisterScope temps(GetVIXLAssembler());
8488 ExcludeIPAndBakerCcEntrypointRegister(&temps, instruction);
8489 uint32_t custom_data = linker::Thumb2RelativePatcher::EncodeBakerReadBarrierFieldData(
Vladimir Marko88abba22017-05-03 17:09:25 +01008490 base.GetCode(), obj.GetCode(), narrow);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008491 vixl32::Label* bne_label = NewBakerReadBarrierPatch(custom_data);
8492
Roland Levillain5daa4952017-07-03 17:23:56 +01008493 {
8494 vixl::EmissionCheckScope guard(
8495 GetVIXLAssembler(),
8496 (kPoisonHeapReferences ? 5u : 4u) * vixl32::kMaxInstructionSizeInBytes);
8497 vixl32::Label return_address;
8498 EmitAdrCode adr(GetVIXLAssembler(), lr, &return_address);
8499 __ cmp(mr, Operand(0));
8500 EmitPlaceholderBne(this, bne_label);
8501 ptrdiff_t old_offset = GetVIXLAssembler()->GetBuffer()->GetCursorOffset();
8502 __ ldr(EncodingSize(narrow ? Narrow : Wide), ref_reg, MemOperand(base, offset));
8503 if (needs_null_check) {
8504 MaybeRecordImplicitNullCheck(instruction);
Vladimir Marko88abba22017-05-03 17:09:25 +01008505 }
Roland Levillain5daa4952017-07-03 17:23:56 +01008506 // Note: We need a specific width for the unpoisoning NEG.
8507 if (kPoisonHeapReferences) {
8508 if (narrow) {
8509 // The only 16-bit encoding is T1 which sets flags outside IT block (i.e. RSBS, not RSB).
8510 __ rsbs(EncodingSize(Narrow), ref_reg, ref_reg, Operand(0));
8511 } else {
8512 __ rsb(EncodingSize(Wide), ref_reg, ref_reg, Operand(0));
8513 }
8514 }
8515 __ Bind(&return_address);
8516 DCHECK_EQ(old_offset - GetVIXLAssembler()->GetBuffer()->GetCursorOffset(),
8517 narrow ? BAKER_MARK_INTROSPECTION_FIELD_LDR_NARROW_OFFSET
8518 : BAKER_MARK_INTROSPECTION_FIELD_LDR_WIDE_OFFSET);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008519 }
Roland Levillain5daa4952017-07-03 17:23:56 +01008520 MaybeGenerateMarkingRegisterCheck(/* code */ 19, /* temp_loc */ LocationFrom(ip));
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008521 return;
8522 }
8523
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008524 // /* HeapReference<Object> */ ref = *(obj + offset)
8525 Location no_index = Location::NoLocation();
8526 ScaleFactor no_scale_factor = TIMES_1;
8527 GenerateReferenceLoadWithBakerReadBarrier(
8528 instruction, ref, obj, offset, no_index, no_scale_factor, temp, needs_null_check);
Roland Levillain6070e882016-11-03 17:51:58 +00008529}
8530
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008531void CodeGeneratorARMVIXL::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
8532 Location ref,
8533 vixl32::Register obj,
8534 uint32_t data_offset,
8535 Location index,
8536 Location temp,
8537 bool needs_null_check) {
8538 DCHECK(kEmitCompilerReadBarrier);
8539 DCHECK(kUseBakerReadBarrier);
8540
8541 static_assert(
8542 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
8543 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008544 ScaleFactor scale_factor = TIMES_4;
8545
8546 if (kBakerReadBarrierLinkTimeThunksEnableForArrays &&
8547 !Runtime::Current()->UseJitCompilation()) {
Roland Levillain6d729a72017-06-30 18:34:01 +01008548 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in the
8549 // Marking Register) to decide whether we need to enter the slow
8550 // path to mark the reference. Then, in the slow path, check the
8551 // gray bit in the lock word of the reference's holder (`obj`) to
8552 // decide whether to mark `ref` or not.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008553 //
8554 // We use link-time generated thunks for the slow path. That thunk checks
8555 // the holder and jumps to the entrypoint if needed. If the holder is not
8556 // gray, it creates a fake dependency and returns to the LDR instruction.
8557 //
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008558 // lr = &gray_return_address;
Roland Levillain6d729a72017-06-30 18:34:01 +01008559 // if (mr) { // Thread::Current()->GetIsGcMarking()
8560 // goto array_thunk<base_reg>(lr)
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008561 // }
8562 // not_gray_return_address:
8563 // // Original reference load. If the offset is too large to fit
8564 // // into LDR, we use an adjusted base register here.
Vladimir Marko88abba22017-05-03 17:09:25 +01008565 // HeapReference<mirror::Object> reference = data[index];
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008566 // gray_return_address:
8567
8568 DCHECK(index.IsValid());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008569 vixl32::Register index_reg = RegisterFrom(index, DataType::Type::kInt32);
8570 vixl32::Register ref_reg = RegisterFrom(ref, DataType::Type::kReference);
8571 vixl32::Register data_reg = RegisterFrom(temp, DataType::Type::kInt32); // Raw pointer.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008572 DCHECK(!data_reg.Is(kBakerCcEntrypointRegister));
8573
8574 UseScratchRegisterScope temps(GetVIXLAssembler());
8575 ExcludeIPAndBakerCcEntrypointRegister(&temps, instruction);
8576 uint32_t custom_data =
8577 linker::Thumb2RelativePatcher::EncodeBakerReadBarrierArrayData(data_reg.GetCode());
8578 vixl32::Label* bne_label = NewBakerReadBarrierPatch(custom_data);
8579
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008580 __ Add(data_reg, obj, Operand(data_offset));
Roland Levillain5daa4952017-07-03 17:23:56 +01008581 {
8582 vixl::EmissionCheckScope guard(
8583 GetVIXLAssembler(),
8584 (kPoisonHeapReferences ? 5u : 4u) * vixl32::kMaxInstructionSizeInBytes);
8585 vixl32::Label return_address;
8586 EmitAdrCode adr(GetVIXLAssembler(), lr, &return_address);
8587 __ cmp(mr, Operand(0));
8588 EmitPlaceholderBne(this, bne_label);
8589 ptrdiff_t old_offset = GetVIXLAssembler()->GetBuffer()->GetCursorOffset();
8590 __ ldr(ref_reg, MemOperand(data_reg, index_reg, vixl32::LSL, scale_factor));
8591 DCHECK(!needs_null_check); // The thunk cannot handle the null check.
8592 // Note: We need a Wide NEG for the unpoisoning.
8593 if (kPoisonHeapReferences) {
8594 __ rsb(EncodingSize(Wide), ref_reg, ref_reg, Operand(0));
8595 }
8596 __ Bind(&return_address);
8597 DCHECK_EQ(old_offset - GetVIXLAssembler()->GetBuffer()->GetCursorOffset(),
8598 BAKER_MARK_INTROSPECTION_ARRAY_LDR_OFFSET);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008599 }
Roland Levillain5daa4952017-07-03 17:23:56 +01008600 MaybeGenerateMarkingRegisterCheck(/* code */ 20, /* temp_loc */ LocationFrom(ip));
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008601 return;
8602 }
8603
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008604 // /* HeapReference<Object> */ ref =
8605 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008606 GenerateReferenceLoadWithBakerReadBarrier(
8607 instruction, ref, obj, data_offset, index, scale_factor, temp, needs_null_check);
Roland Levillain6070e882016-11-03 17:51:58 +00008608}
8609
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008610void CodeGeneratorARMVIXL::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
8611 Location ref,
8612 vixl32::Register obj,
8613 uint32_t offset,
8614 Location index,
8615 ScaleFactor scale_factor,
8616 Location temp,
Roland Levillainff487002017-03-07 16:50:01 +00008617 bool needs_null_check) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008618 DCHECK(kEmitCompilerReadBarrier);
8619 DCHECK(kUseBakerReadBarrier);
8620
Roland Levillain6d729a72017-06-30 18:34:01 +01008621 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in the
8622 // Marking Register) to decide whether we need to enter the slow
8623 // path to mark the reference. Then, in the slow path, check the
8624 // gray bit in the lock word of the reference's holder (`obj`) to
8625 // decide whether to mark `ref` or not.
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008626 //
Roland Levillain6d729a72017-06-30 18:34:01 +01008627 // if (mr) { // Thread::Current()->GetIsGcMarking()
Roland Levillainff487002017-03-07 16:50:01 +00008628 // // Slow path.
8629 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
8630 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
8631 // HeapReference<mirror::Object> ref = *src; // Original reference load.
8632 // bool is_gray = (rb_state == ReadBarrier::GrayState());
8633 // if (is_gray) {
Roland Levillain6d729a72017-06-30 18:34:01 +01008634 // entrypoint = Thread::Current()->pReadBarrierMarkReg ## root.reg()
8635 // ref = entrypoint(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
Roland Levillainff487002017-03-07 16:50:01 +00008636 // }
8637 // } else {
8638 // HeapReference<mirror::Object> ref = *src; // Original reference load.
8639 // }
8640
8641 vixl32::Register temp_reg = RegisterFrom(temp);
8642
8643 // Slow path marking the object `ref` when the GC is marking. The
Roland Levillain6d729a72017-06-30 18:34:01 +01008644 // entrypoint will be loaded by the slow path code.
Roland Levillainff487002017-03-07 16:50:01 +00008645 SlowPathCodeARMVIXL* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01008646 new (GetScopedAllocator()) LoadReferenceWithBakerReadBarrierSlowPathARMVIXL(
Roland Levillain6d729a72017-06-30 18:34:01 +01008647 instruction, ref, obj, offset, index, scale_factor, needs_null_check, temp_reg);
Roland Levillainff487002017-03-07 16:50:01 +00008648 AddSlowPath(slow_path);
8649
Roland Levillain6d729a72017-06-30 18:34:01 +01008650 __ CompareAndBranchIfNonZero(mr, slow_path->GetEntryLabel());
Roland Levillainff487002017-03-07 16:50:01 +00008651 // Fast path: the GC is not marking: just load the reference.
8652 GenerateRawReferenceLoad(instruction, ref, obj, offset, index, scale_factor, needs_null_check);
8653 __ Bind(slow_path->GetExitLabel());
Roland Levillain5daa4952017-07-03 17:23:56 +01008654 MaybeGenerateMarkingRegisterCheck(/* code */ 21);
Roland Levillainff487002017-03-07 16:50:01 +00008655}
8656
8657void CodeGeneratorARMVIXL::UpdateReferenceFieldWithBakerReadBarrier(HInstruction* instruction,
8658 Location ref,
8659 vixl32::Register obj,
8660 Location field_offset,
8661 Location temp,
8662 bool needs_null_check,
8663 vixl32::Register temp2) {
8664 DCHECK(kEmitCompilerReadBarrier);
8665 DCHECK(kUseBakerReadBarrier);
8666
Roland Levillain6d729a72017-06-30 18:34:01 +01008667 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in the
8668 // Marking Register) to decide whether we need to enter the slow
8669 // path to update the reference field within `obj`. Then, in the
8670 // slow path, check the gray bit in the lock word of the reference's
8671 // holder (`obj`) to decide whether to mark `ref` and update the
8672 // field or not.
Roland Levillainff487002017-03-07 16:50:01 +00008673 //
Roland Levillain6d729a72017-06-30 18:34:01 +01008674 // if (mr) { // Thread::Current()->GetIsGcMarking()
Roland Levillainba650a42017-03-06 13:52:32 +00008675 // // Slow path.
Roland Levillain54f869e2017-03-06 13:54:11 +00008676 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
8677 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
Roland Levillainff487002017-03-07 16:50:01 +00008678 // HeapReference<mirror::Object> ref = *(obj + field_offset); // Reference load.
Roland Levillain54f869e2017-03-06 13:54:11 +00008679 // bool is_gray = (rb_state == ReadBarrier::GrayState());
8680 // if (is_gray) {
Roland Levillainff487002017-03-07 16:50:01 +00008681 // old_ref = ref;
Roland Levillain6d729a72017-06-30 18:34:01 +01008682 // entrypoint = Thread::Current()->pReadBarrierMarkReg ## root.reg()
8683 // ref = entrypoint(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
Roland Levillainff487002017-03-07 16:50:01 +00008684 // compareAndSwapObject(obj, field_offset, old_ref, ref);
Roland Levillain54f869e2017-03-06 13:54:11 +00008685 // }
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008686 // }
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008687
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008688 vixl32::Register temp_reg = RegisterFrom(temp);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008689
Roland Levillainff487002017-03-07 16:50:01 +00008690 // Slow path updating the object reference at address `obj + field_offset`
Roland Levillain6d729a72017-06-30 18:34:01 +01008691 // when the GC is marking. The entrypoint will be loaded by the slow path code.
Vladimir Marko174b2e22017-10-12 13:34:49 +01008692 SlowPathCodeARMVIXL* slow_path =
8693 new (GetScopedAllocator()) LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARMVIXL(
Roland Levillainff487002017-03-07 16:50:01 +00008694 instruction,
8695 ref,
8696 obj,
8697 /* offset */ 0u,
8698 /* index */ field_offset,
8699 /* scale_factor */ ScaleFactor::TIMES_1,
8700 needs_null_check,
8701 temp_reg,
Roland Levillain6d729a72017-06-30 18:34:01 +01008702 temp2);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008703 AddSlowPath(slow_path);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008704
Roland Levillain6d729a72017-06-30 18:34:01 +01008705 __ CompareAndBranchIfNonZero(mr, slow_path->GetEntryLabel());
Roland Levillainff487002017-03-07 16:50:01 +00008706 // Fast path: the GC is not marking: nothing to do (the field is
8707 // up-to-date, and we don't need to load the reference).
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008708 __ Bind(slow_path->GetExitLabel());
Roland Levillain5daa4952017-07-03 17:23:56 +01008709 MaybeGenerateMarkingRegisterCheck(/* code */ 22);
Roland Levillain844e6532016-11-03 16:09:47 +00008710}
Scott Wakelingfe885462016-09-22 10:24:38 +01008711
Roland Levillainba650a42017-03-06 13:52:32 +00008712void CodeGeneratorARMVIXL::GenerateRawReferenceLoad(HInstruction* instruction,
8713 Location ref,
8714 vixl::aarch32::Register obj,
8715 uint32_t offset,
8716 Location index,
8717 ScaleFactor scale_factor,
8718 bool needs_null_check) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008719 DataType::Type type = DataType::Type::kReference;
Roland Levillainba650a42017-03-06 13:52:32 +00008720 vixl32::Register ref_reg = RegisterFrom(ref, type);
8721
8722 // If needed, vixl::EmissionCheckScope guards are used to ensure
8723 // that no pools are emitted between the load (macro) instruction
8724 // and MaybeRecordImplicitNullCheck.
8725
Scott Wakelingfe885462016-09-22 10:24:38 +01008726 if (index.IsValid()) {
8727 // Load types involving an "index": ArrayGet,
8728 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
8729 // intrinsics.
Roland Levillainba650a42017-03-06 13:52:32 +00008730 // /* HeapReference<mirror::Object> */ ref = *(obj + offset + (index << scale_factor))
Scott Wakelingfe885462016-09-22 10:24:38 +01008731 if (index.IsConstant()) {
8732 size_t computed_offset =
8733 (Int32ConstantFrom(index) << scale_factor) + offset;
Roland Levillainba650a42017-03-06 13:52:32 +00008734 vixl::EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Scott Wakelingfe885462016-09-22 10:24:38 +01008735 GetAssembler()->LoadFromOffset(kLoadWord, ref_reg, obj, computed_offset);
Roland Levillainba650a42017-03-06 13:52:32 +00008736 if (needs_null_check) {
8737 MaybeRecordImplicitNullCheck(instruction);
8738 }
Scott Wakelingfe885462016-09-22 10:24:38 +01008739 } else {
8740 // Handle the special case of the
8741 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
8742 // intrinsics, which use a register pair as index ("long
8743 // offset"), of which only the low part contains data.
8744 vixl32::Register index_reg = index.IsRegisterPair()
8745 ? LowRegisterFrom(index)
8746 : RegisterFrom(index);
8747 UseScratchRegisterScope temps(GetVIXLAssembler());
Roland Levillainba650a42017-03-06 13:52:32 +00008748 vixl32::Register temp = temps.Acquire();
8749 __ Add(temp, obj, Operand(index_reg, ShiftType::LSL, scale_factor));
8750 {
8751 vixl::EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
8752 GetAssembler()->LoadFromOffset(kLoadWord, ref_reg, temp, offset);
8753 if (needs_null_check) {
8754 MaybeRecordImplicitNullCheck(instruction);
8755 }
8756 }
Scott Wakelingfe885462016-09-22 10:24:38 +01008757 }
8758 } else {
Roland Levillainba650a42017-03-06 13:52:32 +00008759 // /* HeapReference<mirror::Object> */ ref = *(obj + offset)
8760 vixl::EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Scott Wakelingfe885462016-09-22 10:24:38 +01008761 GetAssembler()->LoadFromOffset(kLoadWord, ref_reg, obj, offset);
Roland Levillainba650a42017-03-06 13:52:32 +00008762 if (needs_null_check) {
8763 MaybeRecordImplicitNullCheck(instruction);
8764 }
Scott Wakelingfe885462016-09-22 10:24:38 +01008765 }
8766
Roland Levillain844e6532016-11-03 16:09:47 +00008767 // Object* ref = ref_addr->AsMirrorPtr()
8768 GetAssembler()->MaybeUnpoisonHeapReference(ref_reg);
Roland Levillain844e6532016-11-03 16:09:47 +00008769}
8770
Roland Levillain5daa4952017-07-03 17:23:56 +01008771void CodeGeneratorARMVIXL::MaybeGenerateMarkingRegisterCheck(int code, Location temp_loc) {
8772 // The following condition is a compile-time one, so it does not have a run-time cost.
8773 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier && kIsDebugBuild) {
8774 // The following condition is a run-time one; it is executed after the
8775 // previous compile-time test, to avoid penalizing non-debug builds.
8776 if (GetCompilerOptions().EmitRunTimeChecksInDebugMode()) {
8777 UseScratchRegisterScope temps(GetVIXLAssembler());
8778 vixl32::Register temp = temp_loc.IsValid() ? RegisterFrom(temp_loc) : temps.Acquire();
8779 GetAssembler()->GenerateMarkingRegisterCheck(temp,
8780 kMarkingRegisterCheckBreakCodeBaseCode + code);
8781 }
8782 }
8783}
8784
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008785void CodeGeneratorARMVIXL::GenerateReadBarrierSlow(HInstruction* instruction,
8786 Location out,
8787 Location ref,
8788 Location obj,
8789 uint32_t offset,
8790 Location index) {
8791 DCHECK(kEmitCompilerReadBarrier);
8792
8793 // Insert a slow path based read barrier *after* the reference load.
8794 //
8795 // If heap poisoning is enabled, the unpoisoning of the loaded
8796 // reference will be carried out by the runtime within the slow
8797 // path.
8798 //
8799 // Note that `ref` currently does not get unpoisoned (when heap
8800 // poisoning is enabled), which is alright as the `ref` argument is
8801 // not used by the artReadBarrierSlow entry point.
8802 //
8803 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Vladimir Marko174b2e22017-10-12 13:34:49 +01008804 SlowPathCodeARMVIXL* slow_path = new (GetScopedAllocator())
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008805 ReadBarrierForHeapReferenceSlowPathARMVIXL(instruction, out, ref, obj, offset, index);
8806 AddSlowPath(slow_path);
8807
8808 __ B(slow_path->GetEntryLabel());
8809 __ Bind(slow_path->GetExitLabel());
8810}
8811
8812void CodeGeneratorARMVIXL::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
Artem Serov02d37832016-10-25 15:25:33 +01008813 Location out,
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008814 Location ref,
8815 Location obj,
8816 uint32_t offset,
8817 Location index) {
Artem Serov02d37832016-10-25 15:25:33 +01008818 if (kEmitCompilerReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008819 // Baker's read barriers shall be handled by the fast path
Roland Levillain9983e302017-07-14 14:34:22 +01008820 // (CodeGeneratorARMVIXL::GenerateReferenceLoadWithBakerReadBarrier).
Artem Serov02d37832016-10-25 15:25:33 +01008821 DCHECK(!kUseBakerReadBarrier);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008822 // If heap poisoning is enabled, unpoisoning will be taken care of
8823 // by the runtime within the slow path.
8824 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Artem Serov02d37832016-10-25 15:25:33 +01008825 } else if (kPoisonHeapReferences) {
8826 GetAssembler()->UnpoisonHeapReference(RegisterFrom(out));
8827 }
8828}
8829
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008830void CodeGeneratorARMVIXL::GenerateReadBarrierForRootSlow(HInstruction* instruction,
8831 Location out,
8832 Location root) {
8833 DCHECK(kEmitCompilerReadBarrier);
8834
8835 // Insert a slow path based read barrier *after* the GC root load.
8836 //
8837 // Note that GC roots are not affected by heap poisoning, so we do
8838 // not need to do anything special for this here.
8839 SlowPathCodeARMVIXL* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01008840 new (GetScopedAllocator()) ReadBarrierForRootSlowPathARMVIXL(instruction, out, root);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008841 AddSlowPath(slow_path);
8842
8843 __ B(slow_path->GetEntryLabel());
8844 __ Bind(slow_path->GetExitLabel());
8845}
8846
Artem Serov02d37832016-10-25 15:25:33 +01008847// Check if the desired_dispatch_info is supported. If it is, return it,
8848// otherwise return a fall-back info that should be used instead.
8849HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARMVIXL::GetSupportedInvokeStaticOrDirectDispatch(
Artem Serovd4cc5b22016-11-04 11:19:09 +00008850 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +00008851 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Nicolas Geoffraye807ff72017-01-23 09:03:12 +00008852 return desired_dispatch_info;
Artem Serov02d37832016-10-25 15:25:33 +01008853}
8854
Scott Wakelingfe885462016-09-22 10:24:38 +01008855vixl32::Register CodeGeneratorARMVIXL::GetInvokeStaticOrDirectExtraParameter(
8856 HInvokeStaticOrDirect* invoke, vixl32::Register temp) {
8857 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
8858 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
8859 if (!invoke->GetLocations()->Intrinsified()) {
8860 return RegisterFrom(location);
8861 }
8862 // For intrinsics we allow any location, so it may be on the stack.
8863 if (!location.IsRegister()) {
8864 GetAssembler()->LoadFromOffset(kLoadWord, temp, sp, location.GetStackIndex());
8865 return temp;
8866 }
8867 // For register locations, check if the register was saved. If so, get it from the stack.
8868 // Note: There is a chance that the register was saved but not overwritten, so we could
8869 // save one load. However, since this is just an intrinsic slow path we prefer this
8870 // simple and more robust approach rather that trying to determine if that's the case.
8871 SlowPathCode* slow_path = GetCurrentSlowPath();
Scott Wakelingd5cd4972017-02-03 11:38:35 +00008872 if (slow_path != nullptr && slow_path->IsCoreRegisterSaved(RegisterFrom(location).GetCode())) {
Scott Wakelingfe885462016-09-22 10:24:38 +01008873 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(RegisterFrom(location).GetCode());
8874 GetAssembler()->LoadFromOffset(kLoadWord, temp, sp, stack_offset);
8875 return temp;
8876 }
8877 return RegisterFrom(location);
8878}
8879
Vladimir Markod254f5c2017-06-02 15:18:36 +00008880void CodeGeneratorARMVIXL::GenerateStaticOrDirectCall(
Vladimir Markoe7197bf2017-06-02 17:00:23 +01008881 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00008882 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
Scott Wakelingfe885462016-09-22 10:24:38 +01008883 switch (invoke->GetMethodLoadKind()) {
8884 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
8885 uint32_t offset =
8886 GetThreadOffset<kArmPointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
8887 // temp = thread->string_init_entrypoint
Artem Serovd4cc5b22016-11-04 11:19:09 +00008888 GetAssembler()->LoadFromOffset(kLoadWord, RegisterFrom(temp), tr, offset);
8889 break;
8890 }
8891 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
8892 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
8893 break;
Vladimir Marko65979462017-05-19 17:25:12 +01008894 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative: {
8895 DCHECK(GetCompilerOptions().IsBootImage());
8896 PcRelativePatchInfo* labels = NewPcRelativeMethodPatch(invoke->GetTargetMethod());
8897 vixl32::Register temp_reg = RegisterFrom(temp);
8898 EmitMovwMovtPlaceholder(labels, temp_reg);
8899 break;
8900 }
Artem Serovd4cc5b22016-11-04 11:19:09 +00008901 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
8902 __ Mov(RegisterFrom(temp), Operand::From(invoke->GetMethodAddress()));
8903 break;
Vladimir Marko0eb882b2017-05-15 13:39:18 +01008904 case HInvokeStaticOrDirect::MethodLoadKind::kBssEntry: {
8905 PcRelativePatchInfo* labels = NewMethodBssEntryPatch(
8906 MethodReference(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex()));
8907 vixl32::Register temp_reg = RegisterFrom(temp);
8908 EmitMovwMovtPlaceholder(labels, temp_reg);
8909 GetAssembler()->LoadFromOffset(kLoadWord, temp_reg, temp_reg, /* offset*/ 0);
Scott Wakelingfe885462016-09-22 10:24:38 +01008910 break;
8911 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01008912 case HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall: {
8913 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
8914 return; // No code pointer retrieval; the runtime performs the call directly.
Scott Wakelingfe885462016-09-22 10:24:38 +01008915 }
Scott Wakelingfe885462016-09-22 10:24:38 +01008916 }
8917
Artem Serovd4cc5b22016-11-04 11:19:09 +00008918 switch (invoke->GetCodePtrLocation()) {
8919 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Vladimir Markoe7197bf2017-06-02 17:00:23 +01008920 {
8921 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
8922 ExactAssemblyScope aas(GetVIXLAssembler(),
8923 vixl32::k32BitT32InstructionSizeInBytes,
8924 CodeBufferCheckScope::kMaximumSize);
8925 __ bl(GetFrameEntryLabel());
8926 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
8927 }
Artem Serovd4cc5b22016-11-04 11:19:09 +00008928 break;
Artem Serovd4cc5b22016-11-04 11:19:09 +00008929 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
8930 // LR = callee_method->entry_point_from_quick_compiled_code_
8931 GetAssembler()->LoadFromOffset(
8932 kLoadWord,
8933 lr,
8934 RegisterFrom(callee_method),
8935 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize).Int32Value());
Alexandre Rames374ddf32016-11-04 10:40:49 +00008936 {
Vladimir Markoe7197bf2017-06-02 17:00:23 +01008937 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
Alexandre Rames374ddf32016-11-04 10:40:49 +00008938 // 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 +00008939 ExactAssemblyScope aas(GetVIXLAssembler(),
8940 vixl32::k16BitT32InstructionSizeInBytes,
8941 CodeBufferCheckScope::kExactSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00008942 // LR()
8943 __ blx(lr);
Vladimir Markoe7197bf2017-06-02 17:00:23 +01008944 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Alexandre Rames374ddf32016-11-04 10:40:49 +00008945 }
Artem Serovd4cc5b22016-11-04 11:19:09 +00008946 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01008947 }
8948
Scott Wakelingfe885462016-09-22 10:24:38 +01008949 DCHECK(!IsLeafMethod());
8950}
8951
Vladimir Markoe7197bf2017-06-02 17:00:23 +01008952void CodeGeneratorARMVIXL::GenerateVirtualCall(
8953 HInvokeVirtual* invoke, Location temp_location, SlowPathCode* slow_path) {
Scott Wakelingfe885462016-09-22 10:24:38 +01008954 vixl32::Register temp = RegisterFrom(temp_location);
8955 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
8956 invoke->GetVTableIndex(), kArmPointerSize).Uint32Value();
8957
8958 // Use the calling convention instead of the location of the receiver, as
8959 // intrinsics may have put the receiver in a different register. In the intrinsics
8960 // slow path, the arguments have been moved to the right place, so here we are
8961 // guaranteed that the receiver is the first register of the calling convention.
8962 InvokeDexCallingConventionARMVIXL calling_convention;
8963 vixl32::Register receiver = calling_convention.GetRegisterAt(0);
8964 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Alexandre Rames374ddf32016-11-04 10:40:49 +00008965 {
8966 // Make sure the pc is recorded immediately after the `ldr` instruction.
Artem Serov0fb37192016-12-06 18:13:40 +00008967 ExactAssemblyScope aas(GetVIXLAssembler(),
8968 vixl32::kMaxInstructionSizeInBytes,
8969 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00008970 // /* HeapReference<Class> */ temp = receiver->klass_
8971 __ ldr(temp, MemOperand(receiver, class_offset));
8972 MaybeRecordImplicitNullCheck(invoke);
8973 }
Scott Wakelingfe885462016-09-22 10:24:38 +01008974 // Instead of simply (possibly) unpoisoning `temp` here, we should
8975 // emit a read barrier for the previous class reference load.
8976 // However this is not required in practice, as this is an
8977 // intermediate/temporary reference and because the current
8978 // concurrent copying collector keeps the from-space memory
8979 // intact/accessible until the end of the marking phase (the
8980 // concurrent copying collector may not in the future).
8981 GetAssembler()->MaybeUnpoisonHeapReference(temp);
8982
8983 // temp = temp->GetMethodAt(method_offset);
8984 uint32_t entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(
8985 kArmPointerSize).Int32Value();
8986 GetAssembler()->LoadFromOffset(kLoadWord, temp, temp, method_offset);
8987 // LR = temp->GetEntryPoint();
8988 GetAssembler()->LoadFromOffset(kLoadWord, lr, temp, entry_point);
Vladimir Markoe7197bf2017-06-02 17:00:23 +01008989 {
8990 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
8991 // blx in T32 has only 16bit encoding that's why a stricter check for the scope is used.
8992 ExactAssemblyScope aas(GetVIXLAssembler(),
8993 vixl32::k16BitT32InstructionSizeInBytes,
8994 CodeBufferCheckScope::kExactSize);
8995 // LR();
8996 __ blx(lr);
8997 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
8998 }
Scott Wakelingfe885462016-09-22 10:24:38 +01008999}
9000
Vladimir Marko65979462017-05-19 17:25:12 +01009001CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewPcRelativeMethodPatch(
9002 MethodReference target_method) {
9003 return NewPcRelativePatch(*target_method.dex_file,
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07009004 target_method.index,
Vladimir Marko65979462017-05-19 17:25:12 +01009005 &pc_relative_method_patches_);
Artem Serovd4cc5b22016-11-04 11:19:09 +00009006}
9007
Vladimir Marko0eb882b2017-05-15 13:39:18 +01009008CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewMethodBssEntryPatch(
9009 MethodReference target_method) {
9010 return NewPcRelativePatch(*target_method.dex_file,
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07009011 target_method.index,
Vladimir Marko0eb882b2017-05-15 13:39:18 +01009012 &method_bss_entry_patches_);
9013}
9014
Artem Serovd4cc5b22016-11-04 11:19:09 +00009015CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewPcRelativeTypePatch(
9016 const DexFile& dex_file, dex::TypeIndex type_index) {
9017 return NewPcRelativePatch(dex_file, type_index.index_, &pc_relative_type_patches_);
9018}
9019
Vladimir Marko1998cd02017-01-13 13:02:58 +00009020CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewTypeBssEntryPatch(
9021 const DexFile& dex_file, dex::TypeIndex type_index) {
9022 return NewPcRelativePatch(dex_file, type_index.index_, &type_bss_entry_patches_);
9023}
9024
Vladimir Marko65979462017-05-19 17:25:12 +01009025CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewPcRelativeStringPatch(
9026 const DexFile& dex_file, dex::StringIndex string_index) {
9027 return NewPcRelativePatch(dex_file, string_index.index_, &pc_relative_string_patches_);
9028}
9029
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01009030CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewStringBssEntryPatch(
9031 const DexFile& dex_file, dex::StringIndex string_index) {
9032 return NewPcRelativePatch(dex_file, string_index.index_, &string_bss_entry_patches_);
9033}
9034
Artem Serovd4cc5b22016-11-04 11:19:09 +00009035CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewPcRelativePatch(
9036 const DexFile& dex_file, uint32_t offset_or_index, ArenaDeque<PcRelativePatchInfo>* patches) {
9037 patches->emplace_back(dex_file, offset_or_index);
9038 return &patches->back();
9039}
9040
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01009041vixl::aarch32::Label* CodeGeneratorARMVIXL::NewBakerReadBarrierPatch(uint32_t custom_data) {
9042 baker_read_barrier_patches_.emplace_back(custom_data);
9043 return &baker_read_barrier_patches_.back().label;
9044}
9045
Artem Serovc5fcb442016-12-02 19:19:58 +00009046VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateBootImageAddressLiteral(uint32_t address) {
Richard Uhlerc52f3032017-03-02 13:45:45 +00009047 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), &uint32_literals_);
Artem Serovc5fcb442016-12-02 19:19:58 +00009048}
9049
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00009050VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateJitStringLiteral(
9051 const DexFile& dex_file,
9052 dex::StringIndex string_index,
9053 Handle<mirror::String> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01009054 ReserveJitStringRoot(StringReference(&dex_file, string_index), handle);
Artem Serovc5fcb442016-12-02 19:19:58 +00009055 return jit_string_patches_.GetOrCreate(
9056 StringReference(&dex_file, string_index),
9057 [this]() {
9058 return GetAssembler()->CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u);
9059 });
9060}
9061
9062VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateJitClassLiteral(const DexFile& dex_file,
9063 dex::TypeIndex type_index,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00009064 Handle<mirror::Class> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01009065 ReserveJitClassRoot(TypeReference(&dex_file, type_index), handle);
Artem Serovc5fcb442016-12-02 19:19:58 +00009066 return jit_class_patches_.GetOrCreate(
9067 TypeReference(&dex_file, type_index),
9068 [this]() {
9069 return GetAssembler()->CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u);
9070 });
9071}
9072
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01009073template <linker::LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
Artem Serovd4cc5b22016-11-04 11:19:09 +00009074inline void CodeGeneratorARMVIXL::EmitPcRelativeLinkerPatches(
9075 const ArenaDeque<PcRelativePatchInfo>& infos,
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01009076 ArenaVector<linker::LinkerPatch>* linker_patches) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00009077 for (const PcRelativePatchInfo& info : infos) {
9078 const DexFile& dex_file = info.target_dex_file;
9079 size_t offset_or_index = info.offset_or_index;
9080 DCHECK(info.add_pc_label.IsBound());
9081 uint32_t add_pc_offset = dchecked_integral_cast<uint32_t>(info.add_pc_label.GetLocation());
9082 // Add MOVW patch.
9083 DCHECK(info.movw_label.IsBound());
9084 uint32_t movw_offset = dchecked_integral_cast<uint32_t>(info.movw_label.GetLocation());
9085 linker_patches->push_back(Factory(movw_offset, &dex_file, add_pc_offset, offset_or_index));
9086 // Add MOVT patch.
9087 DCHECK(info.movt_label.IsBound());
9088 uint32_t movt_offset = dchecked_integral_cast<uint32_t>(info.movt_label.GetLocation());
9089 linker_patches->push_back(Factory(movt_offset, &dex_file, add_pc_offset, offset_or_index));
9090 }
9091}
9092
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01009093void CodeGeneratorARMVIXL::EmitLinkerPatches(ArenaVector<linker::LinkerPatch>* linker_patches) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00009094 DCHECK(linker_patches->empty());
9095 size_t size =
Vladimir Marko65979462017-05-19 17:25:12 +01009096 /* MOVW+MOVT for each entry */ 2u * pc_relative_method_patches_.size() +
Vladimir Marko0eb882b2017-05-15 13:39:18 +01009097 /* MOVW+MOVT for each entry */ 2u * method_bss_entry_patches_.size() +
Artem Serovc5fcb442016-12-02 19:19:58 +00009098 /* MOVW+MOVT for each entry */ 2u * pc_relative_type_patches_.size() +
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01009099 /* MOVW+MOVT for each entry */ 2u * type_bss_entry_patches_.size() +
Vladimir Marko65979462017-05-19 17:25:12 +01009100 /* MOVW+MOVT for each entry */ 2u * pc_relative_string_patches_.size() +
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01009101 /* MOVW+MOVT for each entry */ 2u * string_bss_entry_patches_.size() +
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01009102 baker_read_barrier_patches_.size();
Artem Serovd4cc5b22016-11-04 11:19:09 +00009103 linker_patches->reserve(size);
Vladimir Marko65979462017-05-19 17:25:12 +01009104 if (GetCompilerOptions().IsBootImage()) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01009105 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeMethodPatch>(
9106 pc_relative_method_patches_, linker_patches);
9107 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeTypePatch>(
9108 pc_relative_type_patches_, linker_patches);
9109 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeStringPatch>(
9110 pc_relative_string_patches_, linker_patches);
Vladimir Marko65979462017-05-19 17:25:12 +01009111 } else {
9112 DCHECK(pc_relative_method_patches_.empty());
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01009113 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeClassTablePatch>(
9114 pc_relative_type_patches_, linker_patches);
9115 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringInternTablePatch>(
9116 pc_relative_string_patches_, linker_patches);
Artem Serovd4cc5b22016-11-04 11:19:09 +00009117 }
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01009118 EmitPcRelativeLinkerPatches<linker::LinkerPatch::MethodBssEntryPatch>(
9119 method_bss_entry_patches_, linker_patches);
9120 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeBssEntryPatch>(
9121 type_bss_entry_patches_, linker_patches);
9122 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringBssEntryPatch>(
9123 string_bss_entry_patches_, linker_patches);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01009124 for (const BakerReadBarrierPatchInfo& info : baker_read_barrier_patches_) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01009125 linker_patches->push_back(linker::LinkerPatch::BakerReadBarrierBranchPatch(
9126 info.label.GetLocation(), info.custom_data));
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01009127 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00009128 DCHECK_EQ(size, linker_patches->size());
Artem Serovc5fcb442016-12-02 19:19:58 +00009129}
9130
9131VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateUint32Literal(
9132 uint32_t value,
9133 Uint32ToLiteralMap* map) {
9134 return map->GetOrCreate(
9135 value,
9136 [this, value]() {
9137 return GetAssembler()->CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ value);
9138 });
9139}
9140
Artem Serov2bbc9532016-10-21 11:51:50 +01009141void LocationsBuilderARMVIXL::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
9142 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01009143 new (GetGraph()->GetAllocator()) LocationSummary(instr, LocationSummary::kNoCall);
Artem Serov2bbc9532016-10-21 11:51:50 +01009144 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
9145 Location::RequiresRegister());
9146 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
9147 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
9148 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
9149}
9150
9151void InstructionCodeGeneratorARMVIXL::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
9152 vixl32::Register res = OutputRegister(instr);
9153 vixl32::Register accumulator =
9154 InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
9155 vixl32::Register mul_left =
9156 InputRegisterAt(instr, HMultiplyAccumulate::kInputMulLeftIndex);
9157 vixl32::Register mul_right =
9158 InputRegisterAt(instr, HMultiplyAccumulate::kInputMulRightIndex);
9159
9160 if (instr->GetOpKind() == HInstruction::kAdd) {
9161 __ Mla(res, mul_left, mul_right, accumulator);
9162 } else {
9163 __ Mls(res, mul_left, mul_right, accumulator);
9164 }
9165}
9166
Artem Serov551b28f2016-10-18 19:11:30 +01009167void LocationsBuilderARMVIXL::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
9168 // Nothing to do, this should be removed during prepare for register allocator.
9169 LOG(FATAL) << "Unreachable";
9170}
9171
9172void InstructionCodeGeneratorARMVIXL::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
9173 // Nothing to do, this should be removed during prepare for register allocator.
9174 LOG(FATAL) << "Unreachable";
9175}
9176
9177// Simple implementation of packed switch - generate cascaded compare/jumps.
9178void LocationsBuilderARMVIXL::VisitPackedSwitch(HPackedSwitch* switch_instr) {
9179 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01009180 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Artem Serov551b28f2016-10-18 19:11:30 +01009181 locations->SetInAt(0, Location::RequiresRegister());
9182 if (switch_instr->GetNumEntries() > kPackedSwitchCompareJumpThreshold &&
9183 codegen_->GetAssembler()->GetVIXLAssembler()->IsUsingT32()) {
9184 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the table base.
9185 if (switch_instr->GetStartValue() != 0) {
9186 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the bias.
9187 }
9188 }
9189}
9190
9191// TODO(VIXL): Investigate and reach the parity with old arm codegen.
9192void InstructionCodeGeneratorARMVIXL::VisitPackedSwitch(HPackedSwitch* switch_instr) {
9193 int32_t lower_bound = switch_instr->GetStartValue();
9194 uint32_t num_entries = switch_instr->GetNumEntries();
9195 LocationSummary* locations = switch_instr->GetLocations();
9196 vixl32::Register value_reg = InputRegisterAt(switch_instr, 0);
9197 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
9198
9199 if (num_entries <= kPackedSwitchCompareJumpThreshold ||
9200 !codegen_->GetAssembler()->GetVIXLAssembler()->IsUsingT32()) {
9201 // Create a series of compare/jumps.
Anton Kirilovedb2ac32016-11-30 15:14:10 +00009202 UseScratchRegisterScope temps(GetVIXLAssembler());
Artem Serov551b28f2016-10-18 19:11:30 +01009203 vixl32::Register temp_reg = temps.Acquire();
9204 // Note: It is fine for the below AddConstantSetFlags() using IP register to temporarily store
9205 // the immediate, because IP is used as the destination register. For the other
9206 // AddConstantSetFlags() and GenerateCompareWithImmediate(), the immediate values are constant,
9207 // and they can be encoded in the instruction without making use of IP register.
9208 __ Adds(temp_reg, value_reg, -lower_bound);
9209
9210 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
9211 // Jump to successors[0] if value == lower_bound.
9212 __ B(eq, codegen_->GetLabelOf(successors[0]));
9213 int32_t last_index = 0;
9214 for (; num_entries - last_index > 2; last_index += 2) {
9215 __ Adds(temp_reg, temp_reg, -2);
9216 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
9217 __ B(lo, codegen_->GetLabelOf(successors[last_index + 1]));
9218 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
9219 __ B(eq, codegen_->GetLabelOf(successors[last_index + 2]));
9220 }
9221 if (num_entries - last_index == 2) {
9222 // The last missing case_value.
9223 __ Cmp(temp_reg, 1);
9224 __ B(eq, codegen_->GetLabelOf(successors[last_index + 1]));
9225 }
9226
9227 // And the default for any other value.
9228 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
9229 __ B(codegen_->GetLabelOf(default_block));
9230 }
9231 } else {
9232 // Create a table lookup.
9233 vixl32::Register table_base = RegisterFrom(locations->GetTemp(0));
9234
9235 JumpTableARMVIXL* jump_table = codegen_->CreateJumpTable(switch_instr);
9236
9237 // Remove the bias.
9238 vixl32::Register key_reg;
9239 if (lower_bound != 0) {
9240 key_reg = RegisterFrom(locations->GetTemp(1));
9241 __ Sub(key_reg, value_reg, lower_bound);
9242 } else {
9243 key_reg = value_reg;
9244 }
9245
9246 // Check whether the value is in the table, jump to default block if not.
9247 __ Cmp(key_reg, num_entries - 1);
9248 __ B(hi, codegen_->GetLabelOf(default_block));
9249
Anton Kirilovedb2ac32016-11-30 15:14:10 +00009250 UseScratchRegisterScope temps(GetVIXLAssembler());
Artem Serov551b28f2016-10-18 19:11:30 +01009251 vixl32::Register jump_offset = temps.Acquire();
9252
9253 // Load jump offset from the table.
Scott Wakeling86e9d262017-01-18 15:59:24 +00009254 {
9255 const size_t jump_size = switch_instr->GetNumEntries() * sizeof(int32_t);
9256 ExactAssemblyScope aas(GetVIXLAssembler(),
9257 (vixl32::kMaxInstructionSizeInBytes * 4) + jump_size,
9258 CodeBufferCheckScope::kMaximumSize);
9259 __ adr(table_base, jump_table->GetTableStartLabel());
9260 __ ldr(jump_offset, MemOperand(table_base, key_reg, vixl32::LSL, 2));
Artem Serov551b28f2016-10-18 19:11:30 +01009261
Scott Wakeling86e9d262017-01-18 15:59:24 +00009262 // Jump to target block by branching to table_base(pc related) + offset.
9263 vixl32::Register target_address = table_base;
9264 __ add(target_address, table_base, jump_offset);
9265 __ bx(target_address);
Artem Serov09a940d2016-11-11 16:15:11 +00009266
Scott Wakeling86e9d262017-01-18 15:59:24 +00009267 jump_table->EmitTable(codegen_);
9268 }
Artem Serov551b28f2016-10-18 19:11:30 +01009269 }
9270}
9271
Artem Serov02d37832016-10-25 15:25:33 +01009272// Copy the result of a call into the given target.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009273void CodeGeneratorARMVIXL::MoveFromReturnRegister(Location trg, DataType::Type type) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01009274 if (!trg.IsValid()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009275 DCHECK_EQ(type, DataType::Type::kVoid);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01009276 return;
9277 }
9278
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009279 DCHECK_NE(type, DataType::Type::kVoid);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01009280
Artem Serovd4cc5b22016-11-04 11:19:09 +00009281 Location return_loc = InvokeDexCallingConventionVisitorARMVIXL().GetReturnLocation(type);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01009282 if (return_loc.Equals(trg)) {
9283 return;
9284 }
9285
9286 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
9287 // with the last branch.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009288 if (type == DataType::Type::kInt64) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01009289 TODO_VIXL32(FATAL);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009290 } else if (type == DataType::Type::kFloat64) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01009291 TODO_VIXL32(FATAL);
9292 } else {
9293 // Let the parallel move resolver take care of all of this.
Vladimir Markoca6fff82017-10-03 14:49:14 +01009294 HParallelMove parallel_move(GetGraph()->GetAllocator());
Anton Kirilove28d9ae2016-10-25 18:17:23 +01009295 parallel_move.AddMove(return_loc, trg, type, nullptr);
9296 GetMoveResolver()->EmitNativeCode(&parallel_move);
9297 }
Scott Wakelingfe885462016-09-22 10:24:38 +01009298}
9299
xueliang.zhong8d2c4592016-11-23 17:05:25 +00009300void LocationsBuilderARMVIXL::VisitClassTableGet(HClassTableGet* instruction) {
9301 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01009302 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
xueliang.zhong8d2c4592016-11-23 17:05:25 +00009303 locations->SetInAt(0, Location::RequiresRegister());
9304 locations->SetOut(Location::RequiresRegister());
Artem Serov551b28f2016-10-18 19:11:30 +01009305}
9306
xueliang.zhong8d2c4592016-11-23 17:05:25 +00009307void InstructionCodeGeneratorARMVIXL::VisitClassTableGet(HClassTableGet* instruction) {
9308 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
9309 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
9310 instruction->GetIndex(), kArmPointerSize).SizeValue();
9311 GetAssembler()->LoadFromOffset(kLoadWord,
9312 OutputRegister(instruction),
9313 InputRegisterAt(instruction, 0),
9314 method_offset);
9315 } else {
9316 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
9317 instruction->GetIndex(), kArmPointerSize));
9318 GetAssembler()->LoadFromOffset(kLoadWord,
9319 OutputRegister(instruction),
9320 InputRegisterAt(instruction, 0),
9321 mirror::Class::ImtPtrOffset(kArmPointerSize).Uint32Value());
9322 GetAssembler()->LoadFromOffset(kLoadWord,
9323 OutputRegister(instruction),
9324 OutputRegister(instruction),
9325 method_offset);
9326 }
Artem Serov551b28f2016-10-18 19:11:30 +01009327}
9328
Artem Serovc5fcb442016-12-02 19:19:58 +00009329static void PatchJitRootUse(uint8_t* code,
9330 const uint8_t* roots_data,
9331 VIXLUInt32Literal* literal,
9332 uint64_t index_in_table) {
9333 DCHECK(literal->IsBound());
9334 uint32_t literal_offset = literal->GetLocation();
9335 uintptr_t address =
9336 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
9337 uint8_t* data = code + literal_offset;
9338 reinterpret_cast<uint32_t*>(data)[0] = dchecked_integral_cast<uint32_t>(address);
9339}
9340
9341void CodeGeneratorARMVIXL::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
9342 for (const auto& entry : jit_string_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01009343 const StringReference& string_reference = entry.first;
9344 VIXLUInt32Literal* table_entry_literal = entry.second;
Vladimir Marko174b2e22017-10-12 13:34:49 +01009345 uint64_t index_in_table = GetJitStringRootIndex(string_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01009346 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Artem Serovc5fcb442016-12-02 19:19:58 +00009347 }
9348 for (const auto& entry : jit_class_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01009349 const TypeReference& type_reference = entry.first;
9350 VIXLUInt32Literal* table_entry_literal = entry.second;
Vladimir Marko174b2e22017-10-12 13:34:49 +01009351 uint64_t index_in_table = GetJitClassRootIndex(type_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01009352 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Artem Serovc5fcb442016-12-02 19:19:58 +00009353 }
9354}
9355
Artem Serovd4cc5b22016-11-04 11:19:09 +00009356void CodeGeneratorARMVIXL::EmitMovwMovtPlaceholder(
9357 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels,
9358 vixl32::Register out) {
Artem Serov0fb37192016-12-06 18:13:40 +00009359 ExactAssemblyScope aas(GetVIXLAssembler(),
9360 3 * vixl32::kMaxInstructionSizeInBytes,
9361 CodeBufferCheckScope::kMaximumSize);
Artem Serovd4cc5b22016-11-04 11:19:09 +00009362 // TODO(VIXL): Think about using mov instead of movw.
9363 __ bind(&labels->movw_label);
9364 __ movw(out, /* placeholder */ 0u);
9365 __ bind(&labels->movt_label);
9366 __ movt(out, /* placeholder */ 0u);
9367 __ bind(&labels->add_pc_label);
9368 __ add(out, out, pc);
9369}
9370
Scott Wakelingfe885462016-09-22 10:24:38 +01009371#undef __
9372#undef QUICK_ENTRY_POINT
9373#undef TODO_VIXL32
9374
9375} // namespace arm
9376} // namespace art