blob: f1ad4e187e29d57f96aa2c870669e8b783e91721 [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
2488 if (HasEmptyFrame()) {
2489 return;
2490 }
2491
Scott Wakelingfe885462016-09-22 10:24:38 +01002492 if (!skip_overflow_check) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002493 UseScratchRegisterScope temps(GetVIXLAssembler());
2494 vixl32::Register temp = temps.Acquire();
Vladimir Marko33bff252017-11-01 14:35:42 +00002495 __ Sub(temp, sp, Operand::From(GetStackOverflowReservedBytes(InstructionSet::kArm)));
Scott Wakelingfe885462016-09-22 10:24:38 +01002496 // The load must immediately precede RecordPcInfo.
Artem Serov0fb37192016-12-06 18:13:40 +00002497 ExactAssemblyScope aas(GetVIXLAssembler(),
2498 vixl32::kMaxInstructionSizeInBytes,
2499 CodeBufferCheckScope::kMaximumSize);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002500 __ ldr(temp, MemOperand(temp));
2501 RecordPcInfo(nullptr, 0);
Scott Wakelingfe885462016-09-22 10:24:38 +01002502 }
2503
2504 __ Push(RegisterList(core_spill_mask_));
2505 GetAssembler()->cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(core_spill_mask_));
2506 GetAssembler()->cfi().RelOffsetForMany(DWARFReg(kMethodRegister),
2507 0,
2508 core_spill_mask_,
2509 kArmWordSize);
2510 if (fpu_spill_mask_ != 0) {
2511 uint32_t first = LeastSignificantBit(fpu_spill_mask_);
2512
2513 // Check that list is contiguous.
2514 DCHECK_EQ(fpu_spill_mask_ >> CTZ(fpu_spill_mask_), ~0u >> (32 - POPCOUNT(fpu_spill_mask_)));
2515
2516 __ Vpush(SRegisterList(vixl32::SRegister(first), POPCOUNT(fpu_spill_mask_)));
2517 GetAssembler()->cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(fpu_spill_mask_));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002518 GetAssembler()->cfi().RelOffsetForMany(DWARFReg(s0), 0, fpu_spill_mask_, kArmWordSize);
Scott Wakelingfe885462016-09-22 10:24:38 +01002519 }
Scott Wakelingbffdc702016-12-07 17:46:03 +00002520
Scott Wakelingfe885462016-09-22 10:24:38 +01002521 int adjust = GetFrameSize() - FrameEntrySpillSize();
2522 __ Sub(sp, sp, adjust);
2523 GetAssembler()->cfi().AdjustCFAOffset(adjust);
Scott Wakelingbffdc702016-12-07 17:46:03 +00002524
2525 // Save the current method if we need it. Note that we do not
2526 // do this in HCurrentMethod, as the instruction might have been removed
2527 // in the SSA graph.
2528 if (RequiresCurrentMethod()) {
2529 GetAssembler()->StoreToOffset(kStoreWord, kMethodRegister, sp, 0);
2530 }
Nicolas Geoffrayf7893532017-06-15 12:34:36 +01002531
2532 if (GetGraph()->HasShouldDeoptimizeFlag()) {
2533 UseScratchRegisterScope temps(GetVIXLAssembler());
2534 vixl32::Register temp = temps.Acquire();
2535 // Initialize should_deoptimize flag to 0.
2536 __ Mov(temp, 0);
2537 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, GetStackOffsetOfShouldDeoptimizeFlag());
2538 }
Roland Levillain5daa4952017-07-03 17:23:56 +01002539
2540 MaybeGenerateMarkingRegisterCheck(/* code */ 1);
Scott Wakelingfe885462016-09-22 10:24:38 +01002541}
2542
2543void CodeGeneratorARMVIXL::GenerateFrameExit() {
2544 if (HasEmptyFrame()) {
2545 __ Bx(lr);
2546 return;
2547 }
2548 GetAssembler()->cfi().RememberState();
2549 int adjust = GetFrameSize() - FrameEntrySpillSize();
2550 __ Add(sp, sp, adjust);
2551 GetAssembler()->cfi().AdjustCFAOffset(-adjust);
2552 if (fpu_spill_mask_ != 0) {
2553 uint32_t first = LeastSignificantBit(fpu_spill_mask_);
2554
2555 // Check that list is contiguous.
2556 DCHECK_EQ(fpu_spill_mask_ >> CTZ(fpu_spill_mask_), ~0u >> (32 - POPCOUNT(fpu_spill_mask_)));
2557
2558 __ Vpop(SRegisterList(vixl32::SRegister(first), POPCOUNT(fpu_spill_mask_)));
2559 GetAssembler()->cfi().AdjustCFAOffset(
2560 -static_cast<int>(kArmWordSize) * POPCOUNT(fpu_spill_mask_));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002561 GetAssembler()->cfi().RestoreMany(DWARFReg(vixl32::SRegister(0)), fpu_spill_mask_);
Scott Wakelingfe885462016-09-22 10:24:38 +01002562 }
2563 // Pop LR into PC to return.
2564 DCHECK_NE(core_spill_mask_ & (1 << kLrCode), 0U);
2565 uint32_t pop_mask = (core_spill_mask_ & (~(1 << kLrCode))) | 1 << kPcCode;
2566 __ Pop(RegisterList(pop_mask));
2567 GetAssembler()->cfi().RestoreState();
2568 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
2569}
2570
2571void CodeGeneratorARMVIXL::Bind(HBasicBlock* block) {
2572 __ Bind(GetLabelOf(block));
2573}
2574
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002575Location InvokeDexCallingConventionVisitorARMVIXL::GetNextLocation(DataType::Type type) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002576 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002577 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002578 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002579 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002580 case DataType::Type::kInt8:
2581 case DataType::Type::kUint16:
2582 case DataType::Type::kInt16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002583 case DataType::Type::kInt32: {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002584 uint32_t index = gp_index_++;
2585 uint32_t stack_index = stack_index_++;
2586 if (index < calling_convention.GetNumberOfRegisters()) {
2587 return LocationFrom(calling_convention.GetRegisterAt(index));
2588 } else {
2589 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
2590 }
2591 }
2592
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002593 case DataType::Type::kInt64: {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002594 uint32_t index = gp_index_;
2595 uint32_t stack_index = stack_index_;
2596 gp_index_ += 2;
2597 stack_index_ += 2;
2598 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
2599 if (calling_convention.GetRegisterAt(index).Is(r1)) {
2600 // Skip R1, and use R2_R3 instead.
2601 gp_index_++;
2602 index++;
2603 }
2604 }
2605 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
2606 DCHECK_EQ(calling_convention.GetRegisterAt(index).GetCode() + 1,
2607 calling_convention.GetRegisterAt(index + 1).GetCode());
2608
2609 return LocationFrom(calling_convention.GetRegisterAt(index),
2610 calling_convention.GetRegisterAt(index + 1));
2611 } else {
2612 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
2613 }
2614 }
2615
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002616 case DataType::Type::kFloat32: {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002617 uint32_t stack_index = stack_index_++;
2618 if (float_index_ % 2 == 0) {
2619 float_index_ = std::max(double_index_, float_index_);
2620 }
2621 if (float_index_ < calling_convention.GetNumberOfFpuRegisters()) {
2622 return LocationFrom(calling_convention.GetFpuRegisterAt(float_index_++));
2623 } else {
2624 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
2625 }
2626 }
2627
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002628 case DataType::Type::kFloat64: {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002629 double_index_ = std::max(double_index_, RoundUp(float_index_, 2));
2630 uint32_t stack_index = stack_index_;
2631 stack_index_ += 2;
2632 if (double_index_ + 1 < calling_convention.GetNumberOfFpuRegisters()) {
2633 uint32_t index = double_index_;
2634 double_index_ += 2;
2635 Location result = LocationFrom(
2636 calling_convention.GetFpuRegisterAt(index),
2637 calling_convention.GetFpuRegisterAt(index + 1));
2638 DCHECK(ExpectedPairLayout(result));
2639 return result;
2640 } else {
2641 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
2642 }
2643 }
2644
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002645 case DataType::Type::kVoid:
Artem Serovd4cc5b22016-11-04 11:19:09 +00002646 LOG(FATAL) << "Unexpected parameter type " << type;
2647 break;
2648 }
2649 return Location::NoLocation();
2650}
2651
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002652Location InvokeDexCallingConventionVisitorARMVIXL::GetReturnLocation(DataType::Type type) const {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002653 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002654 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002655 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002656 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002657 case DataType::Type::kInt8:
2658 case DataType::Type::kUint16:
2659 case DataType::Type::kInt16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002660 case DataType::Type::kInt32: {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002661 return LocationFrom(r0);
2662 }
2663
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002664 case DataType::Type::kFloat32: {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002665 return LocationFrom(s0);
2666 }
2667
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002668 case DataType::Type::kInt64: {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002669 return LocationFrom(r0, r1);
2670 }
2671
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002672 case DataType::Type::kFloat64: {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002673 return LocationFrom(s0, s1);
2674 }
2675
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002676 case DataType::Type::kVoid:
Artem Serovd4cc5b22016-11-04 11:19:09 +00002677 return Location::NoLocation();
2678 }
2679
2680 UNREACHABLE();
2681}
2682
2683Location InvokeDexCallingConventionVisitorARMVIXL::GetMethodLocation() const {
2684 return LocationFrom(kMethodRegister);
2685}
2686
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002687void CodeGeneratorARMVIXL::Move32(Location destination, Location source) {
2688 if (source.Equals(destination)) {
2689 return;
2690 }
2691 if (destination.IsRegister()) {
2692 if (source.IsRegister()) {
2693 __ Mov(RegisterFrom(destination), RegisterFrom(source));
2694 } else if (source.IsFpuRegister()) {
2695 __ Vmov(RegisterFrom(destination), SRegisterFrom(source));
2696 } else {
2697 GetAssembler()->LoadFromOffset(kLoadWord,
2698 RegisterFrom(destination),
2699 sp,
2700 source.GetStackIndex());
2701 }
2702 } else if (destination.IsFpuRegister()) {
2703 if (source.IsRegister()) {
2704 __ Vmov(SRegisterFrom(destination), RegisterFrom(source));
2705 } else if (source.IsFpuRegister()) {
2706 __ Vmov(SRegisterFrom(destination), SRegisterFrom(source));
2707 } else {
2708 GetAssembler()->LoadSFromOffset(SRegisterFrom(destination), sp, source.GetStackIndex());
2709 }
2710 } else {
2711 DCHECK(destination.IsStackSlot()) << destination;
2712 if (source.IsRegister()) {
2713 GetAssembler()->StoreToOffset(kStoreWord,
2714 RegisterFrom(source),
2715 sp,
2716 destination.GetStackIndex());
2717 } else if (source.IsFpuRegister()) {
2718 GetAssembler()->StoreSToOffset(SRegisterFrom(source), sp, destination.GetStackIndex());
2719 } else {
2720 DCHECK(source.IsStackSlot()) << source;
2721 UseScratchRegisterScope temps(GetVIXLAssembler());
2722 vixl32::Register temp = temps.Acquire();
2723 GetAssembler()->LoadFromOffset(kLoadWord, temp, sp, source.GetStackIndex());
2724 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
2725 }
2726 }
2727}
2728
Artem Serovcfbe9132016-10-14 15:58:56 +01002729void CodeGeneratorARMVIXL::MoveConstant(Location location, int32_t value) {
2730 DCHECK(location.IsRegister());
2731 __ Mov(RegisterFrom(location), value);
Scott Wakelingfe885462016-09-22 10:24:38 +01002732}
2733
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002734void CodeGeneratorARMVIXL::MoveLocation(Location dst, Location src, DataType::Type dst_type) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002735 // TODO(VIXL): Maybe refactor to have the 'move' implementation here and use it in
2736 // `ParallelMoveResolverARMVIXL::EmitMove`, as is done in the `arm64` backend.
Vladimir Markoca6fff82017-10-03 14:49:14 +01002737 HParallelMove move(GetGraph()->GetAllocator());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002738 move.AddMove(src, dst, dst_type, nullptr);
2739 GetMoveResolver()->EmitNativeCode(&move);
Scott Wakelingfe885462016-09-22 10:24:38 +01002740}
2741
Artem Serovcfbe9132016-10-14 15:58:56 +01002742void CodeGeneratorARMVIXL::AddLocationAsTemp(Location location, LocationSummary* locations) {
2743 if (location.IsRegister()) {
2744 locations->AddTemp(location);
2745 } else if (location.IsRegisterPair()) {
2746 locations->AddTemp(LocationFrom(LowRegisterFrom(location)));
2747 locations->AddTemp(LocationFrom(HighRegisterFrom(location)));
2748 } else {
2749 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
2750 }
Scott Wakelingfe885462016-09-22 10:24:38 +01002751}
2752
2753void CodeGeneratorARMVIXL::InvokeRuntime(QuickEntrypointEnum entrypoint,
2754 HInstruction* instruction,
2755 uint32_t dex_pc,
2756 SlowPathCode* slow_path) {
2757 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Alexandre Rames374ddf32016-11-04 10:40:49 +00002758 __ Ldr(lr, MemOperand(tr, GetThreadOffset<kArmPointerSize>(entrypoint).Int32Value()));
2759 // Ensure the pc position is recorded immediately after the `blx` instruction.
2760 // 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 +00002761 ExactAssemblyScope aas(GetVIXLAssembler(),
2762 vixl32::k16BitT32InstructionSizeInBytes,
2763 CodeBufferCheckScope::kExactSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00002764 __ blx(lr);
Scott Wakelingfe885462016-09-22 10:24:38 +01002765 if (EntrypointRequiresStackMap(entrypoint)) {
2766 RecordPcInfo(instruction, dex_pc, slow_path);
2767 }
2768}
2769
2770void CodeGeneratorARMVIXL::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
2771 HInstruction* instruction,
2772 SlowPathCode* slow_path) {
2773 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Alexandre Rames374ddf32016-11-04 10:40:49 +00002774 __ Ldr(lr, MemOperand(tr, entry_point_offset));
Scott Wakelingfe885462016-09-22 10:24:38 +01002775 __ Blx(lr);
2776}
2777
Scott Wakelingfe885462016-09-22 10:24:38 +01002778void InstructionCodeGeneratorARMVIXL::HandleGoto(HInstruction* got, HBasicBlock* successor) {
2779 DCHECK(!successor->IsExitBlock());
2780 HBasicBlock* block = got->GetBlock();
2781 HInstruction* previous = got->GetPrevious();
2782 HLoopInformation* info = block->GetLoopInformation();
2783
2784 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Scott Wakelingfe885462016-09-22 10:24:38 +01002785 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2786 return;
2787 }
2788 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2789 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
Roland Levillain5daa4952017-07-03 17:23:56 +01002790 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 2);
Scott Wakelingfe885462016-09-22 10:24:38 +01002791 }
2792 if (!codegen_->GoesToNextBlock(block, successor)) {
2793 __ B(codegen_->GetLabelOf(successor));
2794 }
2795}
2796
2797void LocationsBuilderARMVIXL::VisitGoto(HGoto* got) {
2798 got->SetLocations(nullptr);
2799}
2800
2801void InstructionCodeGeneratorARMVIXL::VisitGoto(HGoto* got) {
2802 HandleGoto(got, got->GetSuccessor());
2803}
2804
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002805void LocationsBuilderARMVIXL::VisitTryBoundary(HTryBoundary* try_boundary) {
2806 try_boundary->SetLocations(nullptr);
2807}
2808
2809void InstructionCodeGeneratorARMVIXL::VisitTryBoundary(HTryBoundary* try_boundary) {
2810 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2811 if (!successor->IsExitBlock()) {
2812 HandleGoto(try_boundary, successor);
2813 }
2814}
2815
Scott Wakelingfe885462016-09-22 10:24:38 +01002816void LocationsBuilderARMVIXL::VisitExit(HExit* exit) {
2817 exit->SetLocations(nullptr);
2818}
2819
2820void InstructionCodeGeneratorARMVIXL::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
2821}
2822
Scott Wakelingfe885462016-09-22 10:24:38 +01002823void InstructionCodeGeneratorARMVIXL::GenerateCompareTestAndBranch(HCondition* condition,
Anton Kirilov23b752b2017-07-20 14:40:44 +01002824 vixl32::Label* true_target,
2825 vixl32::Label* false_target,
Anton Kirilovfd522532017-05-10 12:46:57 +01002826 bool is_far_target) {
Anton Kirilov23b752b2017-07-20 14:40:44 +01002827 if (true_target == false_target) {
2828 DCHECK(true_target != nullptr);
2829 __ B(true_target);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002830 return;
2831 }
2832
Anton Kirilov23b752b2017-07-20 14:40:44 +01002833 vixl32::Label* non_fallthrough_target;
2834 bool invert;
2835 bool emit_both_branches;
Scott Wakelingfe885462016-09-22 10:24:38 +01002836
Anton Kirilov23b752b2017-07-20 14:40:44 +01002837 if (true_target == nullptr) {
2838 // The true target is fallthrough.
2839 DCHECK(false_target != nullptr);
2840 non_fallthrough_target = false_target;
2841 invert = true;
2842 emit_both_branches = false;
2843 } else {
2844 non_fallthrough_target = true_target;
2845 invert = false;
2846 // Either the false target is fallthrough, or there is no fallthrough
2847 // and both branches must be emitted.
2848 emit_both_branches = (false_target != nullptr);
Scott Wakelingfe885462016-09-22 10:24:38 +01002849 }
2850
Anton Kirilov23b752b2017-07-20 14:40:44 +01002851 const auto cond = GenerateTest(condition, invert, codegen_);
2852
2853 __ B(cond.first, non_fallthrough_target, is_far_target);
2854
2855 if (emit_both_branches) {
2856 // No target falls through, we need to branch.
2857 __ B(false_target);
Scott Wakelingfe885462016-09-22 10:24:38 +01002858 }
2859}
2860
2861void InstructionCodeGeneratorARMVIXL::GenerateTestAndBranch(HInstruction* instruction,
2862 size_t condition_input_index,
2863 vixl32::Label* true_target,
xueliang.zhongf51bc622016-11-04 09:23:32 +00002864 vixl32::Label* false_target,
2865 bool far_target) {
Scott Wakelingfe885462016-09-22 10:24:38 +01002866 HInstruction* cond = instruction->InputAt(condition_input_index);
2867
2868 if (true_target == nullptr && false_target == nullptr) {
2869 // Nothing to do. The code always falls through.
2870 return;
2871 } else if (cond->IsIntConstant()) {
2872 // Constant condition, statically compared against "true" (integer value 1).
2873 if (cond->AsIntConstant()->IsTrue()) {
2874 if (true_target != nullptr) {
2875 __ B(true_target);
2876 }
2877 } else {
Anton Kirilov644032c2016-12-06 17:51:43 +00002878 DCHECK(cond->AsIntConstant()->IsFalse()) << Int32ConstantFrom(cond);
Scott Wakelingfe885462016-09-22 10:24:38 +01002879 if (false_target != nullptr) {
2880 __ B(false_target);
2881 }
2882 }
2883 return;
2884 }
2885
2886 // The following code generates these patterns:
2887 // (1) true_target == nullptr && false_target != nullptr
2888 // - opposite condition true => branch to false_target
2889 // (2) true_target != nullptr && false_target == nullptr
2890 // - condition true => branch to true_target
2891 // (3) true_target != nullptr && false_target != nullptr
2892 // - condition true => branch to true_target
2893 // - branch to false_target
2894 if (IsBooleanValueOrMaterializedCondition(cond)) {
2895 // Condition has been materialized, compare the output to 0.
2896 if (kIsDebugBuild) {
2897 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
2898 DCHECK(cond_val.IsRegister());
2899 }
2900 if (true_target == nullptr) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00002901 __ CompareAndBranchIfZero(InputRegisterAt(instruction, condition_input_index),
2902 false_target,
2903 far_target);
Scott Wakelingfe885462016-09-22 10:24:38 +01002904 } else {
xueliang.zhongf51bc622016-11-04 09:23:32 +00002905 __ CompareAndBranchIfNonZero(InputRegisterAt(instruction, condition_input_index),
2906 true_target,
2907 far_target);
Scott Wakelingfe885462016-09-22 10:24:38 +01002908 }
2909 } else {
2910 // Condition has not been materialized. Use its inputs as the comparison and
2911 // its condition as the branch condition.
2912 HCondition* condition = cond->AsCondition();
2913
2914 // If this is a long or FP comparison that has been folded into
2915 // the HCondition, generate the comparison directly.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002916 DataType::Type type = condition->InputAt(0)->GetType();
2917 if (type == DataType::Type::kInt64 || DataType::IsFloatingPointType(type)) {
Anton Kirilovfd522532017-05-10 12:46:57 +01002918 GenerateCompareTestAndBranch(condition, true_target, false_target, far_target);
Scott Wakelingfe885462016-09-22 10:24:38 +01002919 return;
2920 }
2921
Donghui Bai426b49c2016-11-08 14:55:38 +08002922 vixl32::Label* non_fallthrough_target;
2923 vixl32::Condition arm_cond = vixl32::Condition::None();
2924 const vixl32::Register left = InputRegisterAt(cond, 0);
2925 const Operand right = InputOperandAt(cond, 1);
2926
Scott Wakelingfe885462016-09-22 10:24:38 +01002927 if (true_target == nullptr) {
Donghui Bai426b49c2016-11-08 14:55:38 +08002928 arm_cond = ARMCondition(condition->GetOppositeCondition());
2929 non_fallthrough_target = false_target;
Scott Wakelingfe885462016-09-22 10:24:38 +01002930 } else {
Donghui Bai426b49c2016-11-08 14:55:38 +08002931 arm_cond = ARMCondition(condition->GetCondition());
2932 non_fallthrough_target = true_target;
2933 }
2934
2935 if (right.IsImmediate() && right.GetImmediate() == 0 && (arm_cond.Is(ne) || arm_cond.Is(eq))) {
2936 if (arm_cond.Is(eq)) {
Anton Kirilovfd522532017-05-10 12:46:57 +01002937 __ CompareAndBranchIfZero(left, non_fallthrough_target, far_target);
Donghui Bai426b49c2016-11-08 14:55:38 +08002938 } else {
2939 DCHECK(arm_cond.Is(ne));
Anton Kirilovfd522532017-05-10 12:46:57 +01002940 __ CompareAndBranchIfNonZero(left, non_fallthrough_target, far_target);
Donghui Bai426b49c2016-11-08 14:55:38 +08002941 }
2942 } else {
2943 __ Cmp(left, right);
Anton Kirilovfd522532017-05-10 12:46:57 +01002944 __ B(arm_cond, non_fallthrough_target, far_target);
Scott Wakelingfe885462016-09-22 10:24:38 +01002945 }
2946 }
2947
2948 // If neither branch falls through (case 3), the conditional branch to `true_target`
2949 // was already emitted (case 2) and we need to emit a jump to `false_target`.
2950 if (true_target != nullptr && false_target != nullptr) {
2951 __ B(false_target);
2952 }
2953}
2954
2955void LocationsBuilderARMVIXL::VisitIf(HIf* if_instr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002956 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(if_instr);
Scott Wakelingfe885462016-09-22 10:24:38 +01002957 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
2958 locations->SetInAt(0, Location::RequiresRegister());
2959 }
2960}
2961
2962void InstructionCodeGeneratorARMVIXL::VisitIf(HIf* if_instr) {
2963 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
2964 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002965 vixl32::Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
2966 nullptr : codegen_->GetLabelOf(true_successor);
2967 vixl32::Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
2968 nullptr : codegen_->GetLabelOf(false_successor);
Scott Wakelingfe885462016-09-22 10:24:38 +01002969 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
2970}
2971
Scott Wakelingc34dba72016-10-03 10:14:44 +01002972void LocationsBuilderARMVIXL::VisitDeoptimize(HDeoptimize* deoptimize) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002973 LocationSummary* locations = new (GetGraph()->GetAllocator())
Scott Wakelingc34dba72016-10-03 10:14:44 +01002974 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01002975 InvokeRuntimeCallingConventionARMVIXL calling_convention;
2976 RegisterSet caller_saves = RegisterSet::Empty();
2977 caller_saves.Add(LocationFrom(calling_convention.GetRegisterAt(0)));
2978 locations->SetCustomSlowPathCallerSaves(caller_saves);
Scott Wakelingc34dba72016-10-03 10:14:44 +01002979 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
2980 locations->SetInAt(0, Location::RequiresRegister());
2981 }
2982}
2983
2984void InstructionCodeGeneratorARMVIXL::VisitDeoptimize(HDeoptimize* deoptimize) {
2985 SlowPathCodeARMVIXL* slow_path =
2986 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARMVIXL>(deoptimize);
2987 GenerateTestAndBranch(deoptimize,
2988 /* condition_input_index */ 0,
2989 slow_path->GetEntryLabel(),
2990 /* false_target */ nullptr);
2991}
2992
Artem Serovd4cc5b22016-11-04 11:19:09 +00002993void LocationsBuilderARMVIXL::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002994 LocationSummary* locations = new (GetGraph()->GetAllocator())
Artem Serovd4cc5b22016-11-04 11:19:09 +00002995 LocationSummary(flag, LocationSummary::kNoCall);
2996 locations->SetOut(Location::RequiresRegister());
2997}
2998
2999void InstructionCodeGeneratorARMVIXL::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
3000 GetAssembler()->LoadFromOffset(kLoadWord,
3001 OutputRegister(flag),
3002 sp,
3003 codegen_->GetStackOffsetOfShouldDeoptimizeFlag());
3004}
3005
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003006void LocationsBuilderARMVIXL::VisitSelect(HSelect* select) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003007 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(select);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003008 const bool is_floating_point = DataType::IsFloatingPointType(select->GetType());
Donghui Bai426b49c2016-11-08 14:55:38 +08003009
3010 if (is_floating_point) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003011 locations->SetInAt(0, Location::RequiresFpuRegister());
Donghui Bai426b49c2016-11-08 14:55:38 +08003012 locations->SetInAt(1, Location::FpuRegisterOrConstant(select->GetTrueValue()));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003013 } else {
3014 locations->SetInAt(0, Location::RequiresRegister());
Donghui Bai426b49c2016-11-08 14:55:38 +08003015 locations->SetInAt(1, Arm8BitEncodableConstantOrRegister(select->GetTrueValue()));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003016 }
Donghui Bai426b49c2016-11-08 14:55:38 +08003017
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003018 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
Donghui Bai426b49c2016-11-08 14:55:38 +08003019 locations->SetInAt(2, Location::RegisterOrConstant(select->GetCondition()));
3020 // The code generator handles overlap with the values, but not with the condition.
3021 locations->SetOut(Location::SameAsFirstInput());
3022 } else if (is_floating_point) {
3023 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3024 } else {
3025 if (!locations->InAt(1).IsConstant()) {
3026 locations->SetInAt(0, Arm8BitEncodableConstantOrRegister(select->GetFalseValue()));
3027 }
3028
3029 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003030 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003031}
3032
3033void InstructionCodeGeneratorARMVIXL::VisitSelect(HSelect* select) {
Donghui Bai426b49c2016-11-08 14:55:38 +08003034 HInstruction* const condition = select->GetCondition();
3035 const LocationSummary* const locations = select->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003036 const DataType::Type type = select->GetType();
Donghui Bai426b49c2016-11-08 14:55:38 +08003037 const Location first = locations->InAt(0);
3038 const Location out = locations->Out();
3039 const Location second = locations->InAt(1);
3040 Location src;
3041
3042 if (condition->IsIntConstant()) {
3043 if (condition->AsIntConstant()->IsFalse()) {
3044 src = first;
3045 } else {
3046 src = second;
3047 }
3048
3049 codegen_->MoveLocation(out, src, type);
3050 return;
3051 }
3052
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003053 if (!DataType::IsFloatingPointType(type)) {
Donghui Bai426b49c2016-11-08 14:55:38 +08003054 bool invert = false;
3055
3056 if (out.Equals(second)) {
3057 src = first;
3058 invert = true;
3059 } else if (out.Equals(first)) {
3060 src = second;
3061 } else if (second.IsConstant()) {
3062 DCHECK(CanEncodeConstantAs8BitImmediate(second.GetConstant()));
3063 src = second;
3064 } else if (first.IsConstant()) {
3065 DCHECK(CanEncodeConstantAs8BitImmediate(first.GetConstant()));
3066 src = first;
3067 invert = true;
3068 } else {
3069 src = second;
3070 }
3071
3072 if (CanGenerateConditionalMove(out, src)) {
3073 if (!out.Equals(first) && !out.Equals(second)) {
3074 codegen_->MoveLocation(out, src.Equals(first) ? second : first, type);
3075 }
3076
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003077 std::pair<vixl32::Condition, vixl32::Condition> cond(eq, ne);
3078
3079 if (IsBooleanValueOrMaterializedCondition(condition)) {
3080 __ Cmp(InputRegisterAt(select, 2), 0);
3081 cond = invert ? std::make_pair(eq, ne) : std::make_pair(ne, eq);
3082 } else {
3083 cond = GenerateTest(condition->AsCondition(), invert, codegen_);
3084 }
3085
Donghui Bai426b49c2016-11-08 14:55:38 +08003086 const size_t instr_count = out.IsRegisterPair() ? 4 : 2;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003087 // We use the scope because of the IT block that follows.
Donghui Bai426b49c2016-11-08 14:55:38 +08003088 ExactAssemblyScope guard(GetVIXLAssembler(),
3089 instr_count * vixl32::k16BitT32InstructionSizeInBytes,
3090 CodeBufferCheckScope::kExactSize);
3091
3092 if (out.IsRegister()) {
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003093 __ it(cond.first);
3094 __ mov(cond.first, RegisterFrom(out), OperandFrom(src, type));
Donghui Bai426b49c2016-11-08 14:55:38 +08003095 } else {
3096 DCHECK(out.IsRegisterPair());
3097
3098 Operand operand_high(0);
3099 Operand operand_low(0);
3100
3101 if (src.IsConstant()) {
3102 const int64_t value = Int64ConstantFrom(src);
3103
3104 operand_high = High32Bits(value);
3105 operand_low = Low32Bits(value);
3106 } else {
3107 DCHECK(src.IsRegisterPair());
3108 operand_high = HighRegisterFrom(src);
3109 operand_low = LowRegisterFrom(src);
3110 }
3111
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003112 __ it(cond.first);
3113 __ mov(cond.first, LowRegisterFrom(out), operand_low);
3114 __ it(cond.first);
3115 __ mov(cond.first, HighRegisterFrom(out), operand_high);
Donghui Bai426b49c2016-11-08 14:55:38 +08003116 }
3117
3118 return;
3119 }
3120 }
3121
3122 vixl32::Label* false_target = nullptr;
3123 vixl32::Label* true_target = nullptr;
3124 vixl32::Label select_end;
3125 vixl32::Label* const target = codegen_->GetFinalLabel(select, &select_end);
3126
3127 if (out.Equals(second)) {
3128 true_target = target;
3129 src = first;
3130 } else {
3131 false_target = target;
3132 src = second;
3133
3134 if (!out.Equals(first)) {
3135 codegen_->MoveLocation(out, first, type);
3136 }
3137 }
3138
3139 GenerateTestAndBranch(select, 2, true_target, false_target, /* far_target */ false);
3140 codegen_->MoveLocation(out, src, type);
3141
3142 if (select_end.IsReferenced()) {
3143 __ Bind(&select_end);
3144 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003145}
3146
Artem Serov551b28f2016-10-18 19:11:30 +01003147void LocationsBuilderARMVIXL::VisitNativeDebugInfo(HNativeDebugInfo* info) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003148 new (GetGraph()->GetAllocator()) LocationSummary(info);
Artem Serov551b28f2016-10-18 19:11:30 +01003149}
3150
3151void InstructionCodeGeneratorARMVIXL::VisitNativeDebugInfo(HNativeDebugInfo*) {
3152 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
3153}
3154
Scott Wakelingfe885462016-09-22 10:24:38 +01003155void CodeGeneratorARMVIXL::GenerateNop() {
3156 __ Nop();
3157}
3158
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003159// `temp` is an extra temporary register that is used for some conditions;
3160// callers may not specify it, in which case the method will use a scratch
3161// register instead.
3162void CodeGeneratorARMVIXL::GenerateConditionWithZero(IfCondition condition,
3163 vixl32::Register out,
3164 vixl32::Register in,
3165 vixl32::Register temp) {
3166 switch (condition) {
3167 case kCondEQ:
3168 // x <= 0 iff x == 0 when the comparison is unsigned.
3169 case kCondBE:
3170 if (!temp.IsValid() || (out.IsLow() && !out.Is(in))) {
3171 temp = out;
3172 }
3173
3174 // Avoid 32-bit instructions if possible; note that `in` and `temp` must be
3175 // different as well.
3176 if (in.IsLow() && temp.IsLow() && !in.Is(temp)) {
3177 // temp = - in; only 0 sets the carry flag.
3178 __ Rsbs(temp, in, 0);
3179
3180 if (out.Is(in)) {
3181 std::swap(in, temp);
3182 }
3183
3184 // out = - in + in + carry = carry
3185 __ Adc(out, temp, in);
3186 } else {
3187 // If `in` is 0, then it has 32 leading zeros, and less than that otherwise.
3188 __ Clz(out, in);
3189 // Any number less than 32 logically shifted right by 5 bits results in 0;
3190 // the same operation on 32 yields 1.
3191 __ Lsr(out, out, 5);
3192 }
3193
3194 break;
3195 case kCondNE:
3196 // x > 0 iff x != 0 when the comparison is unsigned.
3197 case kCondA: {
3198 UseScratchRegisterScope temps(GetVIXLAssembler());
3199
3200 if (out.Is(in)) {
3201 if (!temp.IsValid() || in.Is(temp)) {
3202 temp = temps.Acquire();
3203 }
3204 } else if (!temp.IsValid() || !temp.IsLow()) {
3205 temp = out;
3206 }
3207
3208 // temp = in - 1; only 0 does not set the carry flag.
3209 __ Subs(temp, in, 1);
3210 // out = in + ~temp + carry = in + (-(in - 1) - 1) + carry = in - in + 1 - 1 + carry = carry
3211 __ Sbc(out, in, temp);
3212 break;
3213 }
3214 case kCondGE:
3215 __ Mvn(out, in);
3216 in = out;
3217 FALLTHROUGH_INTENDED;
3218 case kCondLT:
3219 // We only care about the sign bit.
3220 __ Lsr(out, in, 31);
3221 break;
3222 case kCondAE:
3223 // Trivially true.
3224 __ Mov(out, 1);
3225 break;
3226 case kCondB:
3227 // Trivially false.
3228 __ Mov(out, 0);
3229 break;
3230 default:
3231 LOG(FATAL) << "Unexpected condition " << condition;
3232 UNREACHABLE();
3233 }
3234}
3235
Scott Wakelingfe885462016-09-22 10:24:38 +01003236void LocationsBuilderARMVIXL::HandleCondition(HCondition* cond) {
3237 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003238 new (GetGraph()->GetAllocator()) LocationSummary(cond, LocationSummary::kNoCall);
Scott Wakelingfe885462016-09-22 10:24:38 +01003239 // Handle the long/FP comparisons made in instruction simplification.
3240 switch (cond->InputAt(0)->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003241 case DataType::Type::kInt64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003242 locations->SetInAt(0, Location::RequiresRegister());
3243 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
3244 if (!cond->IsEmittedAtUseSite()) {
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003245 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Scott Wakelingfe885462016-09-22 10:24:38 +01003246 }
3247 break;
3248
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003249 case DataType::Type::kFloat32:
3250 case DataType::Type::kFloat64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003251 locations->SetInAt(0, Location::RequiresFpuRegister());
Artem Serov657022c2016-11-23 14:19:38 +00003252 locations->SetInAt(1, ArithmeticZeroOrFpuRegister(cond->InputAt(1)));
Scott Wakelingfe885462016-09-22 10:24:38 +01003253 if (!cond->IsEmittedAtUseSite()) {
3254 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3255 }
3256 break;
3257
3258 default:
3259 locations->SetInAt(0, Location::RequiresRegister());
3260 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
3261 if (!cond->IsEmittedAtUseSite()) {
3262 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3263 }
3264 }
3265}
3266
3267void InstructionCodeGeneratorARMVIXL::HandleCondition(HCondition* cond) {
3268 if (cond->IsEmittedAtUseSite()) {
3269 return;
3270 }
3271
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003272 const DataType::Type type = cond->GetLeft()->GetType();
Scott Wakelingfe885462016-09-22 10:24:38 +01003273
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003274 if (DataType::IsFloatingPointType(type)) {
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003275 GenerateConditionGeneric(cond, codegen_);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003276 return;
Scott Wakelingfe885462016-09-22 10:24:38 +01003277 }
3278
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003279 DCHECK(DataType::IsIntegralType(type) || type == DataType::Type::kReference) << type;
Scott Wakelingfe885462016-09-22 10:24:38 +01003280
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003281 const IfCondition condition = cond->GetCondition();
Scott Wakelingfe885462016-09-22 10:24:38 +01003282
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003283 // A condition with only one boolean input, or two boolean inputs without being equality or
3284 // inequality results from transformations done by the instruction simplifier, and is handled
3285 // as a regular condition with integral inputs.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003286 if (type == DataType::Type::kBool &&
3287 cond->GetRight()->GetType() == DataType::Type::kBool &&
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003288 (condition == kCondEQ || condition == kCondNE)) {
3289 vixl32::Register left = InputRegisterAt(cond, 0);
3290 const vixl32::Register out = OutputRegister(cond);
3291 const Location right_loc = cond->GetLocations()->InAt(1);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003292
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003293 // The constant case is handled by the instruction simplifier.
3294 DCHECK(!right_loc.IsConstant());
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003295
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003296 vixl32::Register right = RegisterFrom(right_loc);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003297
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003298 // Avoid 32-bit instructions if possible.
3299 if (out.Is(right)) {
3300 std::swap(left, right);
3301 }
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003302
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003303 __ Eor(out, left, right);
3304
3305 if (condition == kCondEQ) {
3306 __ Eor(out, out, 1);
3307 }
3308
3309 return;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003310 }
Anton Kirilov6f644202017-02-27 18:29:45 +00003311
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003312 GenerateConditionIntegralOrNonPrimitive(cond, codegen_);
Scott Wakelingfe885462016-09-22 10:24:38 +01003313}
3314
3315void LocationsBuilderARMVIXL::VisitEqual(HEqual* comp) {
3316 HandleCondition(comp);
3317}
3318
3319void InstructionCodeGeneratorARMVIXL::VisitEqual(HEqual* comp) {
3320 HandleCondition(comp);
3321}
3322
3323void LocationsBuilderARMVIXL::VisitNotEqual(HNotEqual* comp) {
3324 HandleCondition(comp);
3325}
3326
3327void InstructionCodeGeneratorARMVIXL::VisitNotEqual(HNotEqual* comp) {
3328 HandleCondition(comp);
3329}
3330
3331void LocationsBuilderARMVIXL::VisitLessThan(HLessThan* comp) {
3332 HandleCondition(comp);
3333}
3334
3335void InstructionCodeGeneratorARMVIXL::VisitLessThan(HLessThan* comp) {
3336 HandleCondition(comp);
3337}
3338
3339void LocationsBuilderARMVIXL::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
3340 HandleCondition(comp);
3341}
3342
3343void InstructionCodeGeneratorARMVIXL::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
3344 HandleCondition(comp);
3345}
3346
3347void LocationsBuilderARMVIXL::VisitGreaterThan(HGreaterThan* comp) {
3348 HandleCondition(comp);
3349}
3350
3351void InstructionCodeGeneratorARMVIXL::VisitGreaterThan(HGreaterThan* comp) {
3352 HandleCondition(comp);
3353}
3354
3355void LocationsBuilderARMVIXL::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
3356 HandleCondition(comp);
3357}
3358
3359void InstructionCodeGeneratorARMVIXL::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
3360 HandleCondition(comp);
3361}
3362
3363void LocationsBuilderARMVIXL::VisitBelow(HBelow* comp) {
3364 HandleCondition(comp);
3365}
3366
3367void InstructionCodeGeneratorARMVIXL::VisitBelow(HBelow* comp) {
3368 HandleCondition(comp);
3369}
3370
3371void LocationsBuilderARMVIXL::VisitBelowOrEqual(HBelowOrEqual* comp) {
3372 HandleCondition(comp);
3373}
3374
3375void InstructionCodeGeneratorARMVIXL::VisitBelowOrEqual(HBelowOrEqual* comp) {
3376 HandleCondition(comp);
3377}
3378
3379void LocationsBuilderARMVIXL::VisitAbove(HAbove* comp) {
3380 HandleCondition(comp);
3381}
3382
3383void InstructionCodeGeneratorARMVIXL::VisitAbove(HAbove* comp) {
3384 HandleCondition(comp);
3385}
3386
3387void LocationsBuilderARMVIXL::VisitAboveOrEqual(HAboveOrEqual* comp) {
3388 HandleCondition(comp);
3389}
3390
3391void InstructionCodeGeneratorARMVIXL::VisitAboveOrEqual(HAboveOrEqual* comp) {
3392 HandleCondition(comp);
3393}
3394
3395void LocationsBuilderARMVIXL::VisitIntConstant(HIntConstant* constant) {
3396 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003397 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Scott Wakelingfe885462016-09-22 10:24:38 +01003398 locations->SetOut(Location::ConstantLocation(constant));
3399}
3400
3401void InstructionCodeGeneratorARMVIXL::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
3402 // Will be generated at use site.
3403}
3404
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003405void LocationsBuilderARMVIXL::VisitNullConstant(HNullConstant* constant) {
3406 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003407 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003408 locations->SetOut(Location::ConstantLocation(constant));
3409}
3410
3411void InstructionCodeGeneratorARMVIXL::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
3412 // Will be generated at use site.
3413}
3414
Scott Wakelingfe885462016-09-22 10:24:38 +01003415void LocationsBuilderARMVIXL::VisitLongConstant(HLongConstant* constant) {
3416 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003417 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Scott Wakelingfe885462016-09-22 10:24:38 +01003418 locations->SetOut(Location::ConstantLocation(constant));
3419}
3420
3421void InstructionCodeGeneratorARMVIXL::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
3422 // Will be generated at use site.
3423}
3424
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01003425void LocationsBuilderARMVIXL::VisitFloatConstant(HFloatConstant* constant) {
3426 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003427 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01003428 locations->SetOut(Location::ConstantLocation(constant));
3429}
3430
Scott Wakelingc34dba72016-10-03 10:14:44 +01003431void InstructionCodeGeneratorARMVIXL::VisitFloatConstant(
3432 HFloatConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01003433 // Will be generated at use site.
3434}
3435
3436void LocationsBuilderARMVIXL::VisitDoubleConstant(HDoubleConstant* constant) {
3437 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003438 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01003439 locations->SetOut(Location::ConstantLocation(constant));
3440}
3441
Scott Wakelingc34dba72016-10-03 10:14:44 +01003442void InstructionCodeGeneratorARMVIXL::VisitDoubleConstant(
3443 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01003444 // Will be generated at use site.
3445}
3446
Igor Murashkind01745e2017-04-05 16:40:31 -07003447void LocationsBuilderARMVIXL::VisitConstructorFence(HConstructorFence* constructor_fence) {
3448 constructor_fence->SetLocations(nullptr);
3449}
3450
3451void InstructionCodeGeneratorARMVIXL::VisitConstructorFence(
3452 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
3453 codegen_->GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
3454}
3455
Scott Wakelingfe885462016-09-22 10:24:38 +01003456void LocationsBuilderARMVIXL::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3457 memory_barrier->SetLocations(nullptr);
3458}
3459
3460void InstructionCodeGeneratorARMVIXL::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3461 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
3462}
3463
3464void LocationsBuilderARMVIXL::VisitReturnVoid(HReturnVoid* ret) {
3465 ret->SetLocations(nullptr);
3466}
3467
3468void InstructionCodeGeneratorARMVIXL::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
3469 codegen_->GenerateFrameExit();
3470}
3471
3472void LocationsBuilderARMVIXL::VisitReturn(HReturn* ret) {
3473 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003474 new (GetGraph()->GetAllocator()) LocationSummary(ret, LocationSummary::kNoCall);
Scott Wakelingfe885462016-09-22 10:24:38 +01003475 locations->SetInAt(0, parameter_visitor_.GetReturnLocation(ret->InputAt(0)->GetType()));
3476}
3477
3478void InstructionCodeGeneratorARMVIXL::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
3479 codegen_->GenerateFrameExit();
3480}
3481
Artem Serovcfbe9132016-10-14 15:58:56 +01003482void LocationsBuilderARMVIXL::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3483 // The trampoline uses the same calling convention as dex calling conventions,
3484 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
3485 // the method_idx.
3486 HandleInvoke(invoke);
3487}
3488
3489void InstructionCodeGeneratorARMVIXL::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3490 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
Roland Levillain5daa4952017-07-03 17:23:56 +01003491 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 3);
Artem Serovcfbe9132016-10-14 15:58:56 +01003492}
3493
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003494void LocationsBuilderARMVIXL::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
3495 // Explicit clinit checks triggered by static invokes must have been pruned by
3496 // art::PrepareForRegisterAllocation.
3497 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
3498
Anton Kirilov5ec62182016-10-13 20:16:02 +01003499 IntrinsicLocationsBuilderARMVIXL intrinsic(codegen_);
3500 if (intrinsic.TryDispatch(invoke)) {
Anton Kirilov5ec62182016-10-13 20:16:02 +01003501 return;
3502 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003503
3504 HandleInvoke(invoke);
3505}
3506
Anton Kirilov5ec62182016-10-13 20:16:02 +01003507static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARMVIXL* codegen) {
3508 if (invoke->GetLocations()->Intrinsified()) {
3509 IntrinsicCodeGeneratorARMVIXL intrinsic(codegen);
3510 intrinsic.Dispatch(invoke);
3511 return true;
3512 }
3513 return false;
3514}
3515
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003516void InstructionCodeGeneratorARMVIXL::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
3517 // Explicit clinit checks triggered by static invokes must have been pruned by
3518 // art::PrepareForRegisterAllocation.
3519 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
3520
Anton Kirilov5ec62182016-10-13 20:16:02 +01003521 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
Roland Levillain5daa4952017-07-03 17:23:56 +01003522 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 4);
Anton Kirilov5ec62182016-10-13 20:16:02 +01003523 return;
3524 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003525
3526 LocationSummary* locations = invoke->GetLocations();
Artem Serovd4cc5b22016-11-04 11:19:09 +00003527 codegen_->GenerateStaticOrDirectCall(
3528 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Roland Levillain5daa4952017-07-03 17:23:56 +01003529
3530 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 5);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003531}
3532
3533void LocationsBuilderARMVIXL::HandleInvoke(HInvoke* invoke) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00003534 InvokeDexCallingConventionVisitorARMVIXL calling_convention_visitor;
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003535 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
3536}
3537
3538void LocationsBuilderARMVIXL::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Anton Kirilov5ec62182016-10-13 20:16:02 +01003539 IntrinsicLocationsBuilderARMVIXL intrinsic(codegen_);
3540 if (intrinsic.TryDispatch(invoke)) {
3541 return;
3542 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003543
3544 HandleInvoke(invoke);
3545}
3546
3547void InstructionCodeGeneratorARMVIXL::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Anton Kirilov5ec62182016-10-13 20:16:02 +01003548 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
Roland Levillain5daa4952017-07-03 17:23:56 +01003549 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 6);
Anton Kirilov5ec62182016-10-13 20:16:02 +01003550 return;
3551 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003552
3553 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexandre Rames374ddf32016-11-04 10:40:49 +00003554 DCHECK(!codegen_->IsLeafMethod());
Roland Levillain5daa4952017-07-03 17:23:56 +01003555
3556 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 7);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003557}
3558
Artem Serovcfbe9132016-10-14 15:58:56 +01003559void LocationsBuilderARMVIXL::VisitInvokeInterface(HInvokeInterface* invoke) {
3560 HandleInvoke(invoke);
3561 // Add the hidden argument.
3562 invoke->GetLocations()->AddTemp(LocationFrom(r12));
3563}
3564
3565void InstructionCodeGeneratorARMVIXL::VisitInvokeInterface(HInvokeInterface* invoke) {
3566 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
3567 LocationSummary* locations = invoke->GetLocations();
3568 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
3569 vixl32::Register hidden_reg = RegisterFrom(locations->GetTemp(1));
3570 Location receiver = locations->InAt(0);
3571 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3572
3573 DCHECK(!receiver.IsStackSlot());
3574
Alexandre Rames374ddf32016-11-04 10:40:49 +00003575 // Ensure the pc position is recorded immediately after the `ldr` instruction.
3576 {
Artem Serov0fb37192016-12-06 18:13:40 +00003577 ExactAssemblyScope aas(GetVIXLAssembler(),
3578 vixl32::kMaxInstructionSizeInBytes,
3579 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00003580 // /* HeapReference<Class> */ temp = receiver->klass_
3581 __ ldr(temp, MemOperand(RegisterFrom(receiver), class_offset));
3582 codegen_->MaybeRecordImplicitNullCheck(invoke);
3583 }
Artem Serovcfbe9132016-10-14 15:58:56 +01003584 // Instead of simply (possibly) unpoisoning `temp` here, we should
3585 // emit a read barrier for the previous class reference load.
3586 // However this is not required in practice, as this is an
3587 // intermediate/temporary reference and because the current
3588 // concurrent copying collector keeps the from-space memory
3589 // intact/accessible until the end of the marking phase (the
3590 // concurrent copying collector may not in the future).
3591 GetAssembler()->MaybeUnpoisonHeapReference(temp);
3592 GetAssembler()->LoadFromOffset(kLoadWord,
3593 temp,
3594 temp,
3595 mirror::Class::ImtPtrOffset(kArmPointerSize).Uint32Value());
3596 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
3597 invoke->GetImtIndex(), kArmPointerSize));
3598 // temp = temp->GetImtEntryAt(method_offset);
3599 GetAssembler()->LoadFromOffset(kLoadWord, temp, temp, method_offset);
3600 uint32_t entry_point =
3601 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize).Int32Value();
3602 // LR = temp->GetEntryPoint();
3603 GetAssembler()->LoadFromOffset(kLoadWord, lr, temp, entry_point);
3604
3605 // Set the hidden (in r12) argument. It is done here, right before a BLX to prevent other
3606 // instruction from clobbering it as they might use r12 as a scratch register.
3607 DCHECK(hidden_reg.Is(r12));
Scott Wakelingb77051e2016-11-21 19:46:00 +00003608
3609 {
3610 // The VIXL macro assembler may clobber any of the scratch registers that are available to it,
3611 // so it checks if the application is using them (by passing them to the macro assembler
3612 // methods). The following application of UseScratchRegisterScope corrects VIXL's notion of
3613 // what is available, and is the opposite of the standard usage: Instead of requesting a
3614 // temporary location, it imposes an external constraint (i.e. a specific register is reserved
3615 // for the hidden argument). Note that this works even if VIXL needs a scratch register itself
3616 // (to materialize the constant), since the destination register becomes available for such use
3617 // internally for the duration of the macro instruction.
3618 UseScratchRegisterScope temps(GetVIXLAssembler());
3619 temps.Exclude(hidden_reg);
3620 __ Mov(hidden_reg, invoke->GetDexMethodIndex());
3621 }
Artem Serovcfbe9132016-10-14 15:58:56 +01003622 {
Alexandre Rames374ddf32016-11-04 10:40:49 +00003623 // Ensure the pc position is recorded immediately after the `blx` instruction.
3624 // 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 +00003625 ExactAssemblyScope aas(GetVIXLAssembler(),
Alexandre Rames374ddf32016-11-04 10:40:49 +00003626 vixl32::k16BitT32InstructionSizeInBytes,
3627 CodeBufferCheckScope::kExactSize);
Artem Serovcfbe9132016-10-14 15:58:56 +01003628 // LR();
3629 __ blx(lr);
Artem Serovcfbe9132016-10-14 15:58:56 +01003630 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Alexandre Rames374ddf32016-11-04 10:40:49 +00003631 DCHECK(!codegen_->IsLeafMethod());
Artem Serovcfbe9132016-10-14 15:58:56 +01003632 }
Roland Levillain5daa4952017-07-03 17:23:56 +01003633
3634 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 8);
Artem Serovcfbe9132016-10-14 15:58:56 +01003635}
3636
Orion Hodsonac141392017-01-13 11:53:47 +00003637void LocationsBuilderARMVIXL::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
3638 HandleInvoke(invoke);
3639}
3640
3641void InstructionCodeGeneratorARMVIXL::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
3642 codegen_->GenerateInvokePolymorphicCall(invoke);
Roland Levillain5daa4952017-07-03 17:23:56 +01003643 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 9);
Orion Hodsonac141392017-01-13 11:53:47 +00003644}
3645
Artem Serov02109dd2016-09-23 17:17:54 +01003646void LocationsBuilderARMVIXL::VisitNeg(HNeg* neg) {
3647 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003648 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Artem Serov02109dd2016-09-23 17:17:54 +01003649 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003650 case DataType::Type::kInt32: {
Artem Serov02109dd2016-09-23 17:17:54 +01003651 locations->SetInAt(0, Location::RequiresRegister());
3652 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3653 break;
3654 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003655 case DataType::Type::kInt64: {
Artem Serov02109dd2016-09-23 17:17:54 +01003656 locations->SetInAt(0, Location::RequiresRegister());
3657 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3658 break;
3659 }
3660
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003661 case DataType::Type::kFloat32:
3662 case DataType::Type::kFloat64:
Artem Serov02109dd2016-09-23 17:17:54 +01003663 locations->SetInAt(0, Location::RequiresFpuRegister());
3664 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3665 break;
3666
3667 default:
3668 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
3669 }
3670}
3671
3672void InstructionCodeGeneratorARMVIXL::VisitNeg(HNeg* neg) {
3673 LocationSummary* locations = neg->GetLocations();
3674 Location out = locations->Out();
3675 Location in = locations->InAt(0);
3676 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003677 case DataType::Type::kInt32:
Artem Serov02109dd2016-09-23 17:17:54 +01003678 __ Rsb(OutputRegister(neg), InputRegisterAt(neg, 0), 0);
3679 break;
3680
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003681 case DataType::Type::kInt64:
Artem Serov02109dd2016-09-23 17:17:54 +01003682 // out.lo = 0 - in.lo (and update the carry/borrow (C) flag)
3683 __ Rsbs(LowRegisterFrom(out), LowRegisterFrom(in), 0);
3684 // We cannot emit an RSC (Reverse Subtract with Carry)
3685 // instruction here, as it does not exist in the Thumb-2
3686 // instruction set. We use the following approach
3687 // using SBC and SUB instead.
3688 //
3689 // out.hi = -C
3690 __ Sbc(HighRegisterFrom(out), HighRegisterFrom(out), HighRegisterFrom(out));
3691 // out.hi = out.hi - in.hi
3692 __ Sub(HighRegisterFrom(out), HighRegisterFrom(out), HighRegisterFrom(in));
3693 break;
3694
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003695 case DataType::Type::kFloat32:
3696 case DataType::Type::kFloat64:
Anton Kirilov644032c2016-12-06 17:51:43 +00003697 __ Vneg(OutputVRegister(neg), InputVRegister(neg));
Artem Serov02109dd2016-09-23 17:17:54 +01003698 break;
3699
3700 default:
3701 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
3702 }
3703}
3704
Scott Wakelingfe885462016-09-22 10:24:38 +01003705void LocationsBuilderARMVIXL::VisitTypeConversion(HTypeConversion* conversion) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003706 DataType::Type result_type = conversion->GetResultType();
3707 DataType::Type input_type = conversion->GetInputType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003708 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
3709 << input_type << " -> " << result_type;
Scott Wakelingfe885462016-09-22 10:24:38 +01003710
3711 // The float-to-long, double-to-long and long-to-float type conversions
3712 // rely on a call to the runtime.
3713 LocationSummary::CallKind call_kind =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003714 (((input_type == DataType::Type::kFloat32 || input_type == DataType::Type::kFloat64)
3715 && result_type == DataType::Type::kInt64)
3716 || (input_type == DataType::Type::kInt64 && result_type == DataType::Type::kFloat32))
Scott Wakelingfe885462016-09-22 10:24:38 +01003717 ? LocationSummary::kCallOnMainOnly
3718 : LocationSummary::kNoCall;
3719 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003720 new (GetGraph()->GetAllocator()) LocationSummary(conversion, call_kind);
Scott Wakelingfe885462016-09-22 10:24:38 +01003721
Scott Wakelingfe885462016-09-22 10:24:38 +01003722 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003723 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003724 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003725 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003726 case DataType::Type::kInt16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003727 DCHECK(DataType::IsIntegralType(input_type)) << input_type;
3728 locations->SetInAt(0, Location::RequiresRegister());
3729 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Scott Wakelingfe885462016-09-22 10:24:38 +01003730 break;
3731
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003732 case DataType::Type::kInt32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003733 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003734 case DataType::Type::kInt64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003735 locations->SetInAt(0, Location::Any());
3736 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3737 break;
3738
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003739 case DataType::Type::kFloat32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003740 locations->SetInAt(0, Location::RequiresFpuRegister());
3741 locations->SetOut(Location::RequiresRegister());
3742 locations->AddTemp(Location::RequiresFpuRegister());
3743 break;
3744
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003745 case DataType::Type::kFloat64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003746 locations->SetInAt(0, Location::RequiresFpuRegister());
3747 locations->SetOut(Location::RequiresRegister());
3748 locations->AddTemp(Location::RequiresFpuRegister());
3749 break;
3750
3751 default:
3752 LOG(FATAL) << "Unexpected type conversion from " << input_type
3753 << " to " << result_type;
3754 }
3755 break;
3756
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003757 case DataType::Type::kInt64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003758 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003759 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003760 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003761 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003762 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003763 case DataType::Type::kInt16:
3764 case DataType::Type::kInt32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003765 locations->SetInAt(0, Location::RequiresRegister());
3766 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3767 break;
3768
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003769 case DataType::Type::kFloat32: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003770 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3771 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
3772 locations->SetOut(LocationFrom(r0, r1));
Scott Wakelingfe885462016-09-22 10:24:38 +01003773 break;
3774 }
3775
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003776 case DataType::Type::kFloat64: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003777 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3778 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0),
3779 calling_convention.GetFpuRegisterAt(1)));
3780 locations->SetOut(LocationFrom(r0, r1));
Scott Wakelingfe885462016-09-22 10:24:38 +01003781 break;
3782 }
3783
3784 default:
3785 LOG(FATAL) << "Unexpected type conversion from " << input_type
3786 << " to " << result_type;
3787 }
3788 break;
3789
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003790 case DataType::Type::kFloat32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003791 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003792 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003793 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003794 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003795 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003796 case DataType::Type::kInt16:
3797 case DataType::Type::kInt32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003798 locations->SetInAt(0, Location::RequiresRegister());
3799 locations->SetOut(Location::RequiresFpuRegister());
3800 break;
3801
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003802 case DataType::Type::kInt64: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003803 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3804 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0),
3805 calling_convention.GetRegisterAt(1)));
3806 locations->SetOut(LocationFrom(calling_convention.GetFpuRegisterAt(0)));
Scott Wakelingfe885462016-09-22 10:24:38 +01003807 break;
3808 }
3809
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003810 case DataType::Type::kFloat64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003811 locations->SetInAt(0, Location::RequiresFpuRegister());
3812 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3813 break;
3814
3815 default:
3816 LOG(FATAL) << "Unexpected type conversion from " << input_type
3817 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08003818 }
Scott Wakelingfe885462016-09-22 10:24:38 +01003819 break;
3820
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003821 case DataType::Type::kFloat64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003822 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003823 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003824 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003825 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003826 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003827 case DataType::Type::kInt16:
3828 case DataType::Type::kInt32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003829 locations->SetInAt(0, Location::RequiresRegister());
3830 locations->SetOut(Location::RequiresFpuRegister());
3831 break;
3832
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003833 case DataType::Type::kInt64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003834 locations->SetInAt(0, Location::RequiresRegister());
3835 locations->SetOut(Location::RequiresFpuRegister());
3836 locations->AddTemp(Location::RequiresFpuRegister());
3837 locations->AddTemp(Location::RequiresFpuRegister());
3838 break;
3839
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003840 case DataType::Type::kFloat32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003841 locations->SetInAt(0, Location::RequiresFpuRegister());
3842 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3843 break;
3844
3845 default:
3846 LOG(FATAL) << "Unexpected type conversion from " << input_type
3847 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08003848 }
Scott Wakelingfe885462016-09-22 10:24:38 +01003849 break;
3850
3851 default:
3852 LOG(FATAL) << "Unexpected type conversion from " << input_type
3853 << " to " << result_type;
3854 }
3855}
3856
3857void InstructionCodeGeneratorARMVIXL::VisitTypeConversion(HTypeConversion* conversion) {
3858 LocationSummary* locations = conversion->GetLocations();
3859 Location out = locations->Out();
3860 Location in = locations->InAt(0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003861 DataType::Type result_type = conversion->GetResultType();
3862 DataType::Type input_type = conversion->GetInputType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003863 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
3864 << input_type << " -> " << result_type;
Scott Wakelingfe885462016-09-22 10:24:38 +01003865 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003866 case DataType::Type::kUint8:
Scott Wakelingfe885462016-09-22 10:24:38 +01003867 switch (input_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003868 case DataType::Type::kInt8:
3869 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003870 case DataType::Type::kInt16:
3871 case DataType::Type::kInt32:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003872 __ Ubfx(OutputRegister(conversion), InputRegisterAt(conversion, 0), 0, 8);
3873 break;
3874 case DataType::Type::kInt64:
3875 __ Ubfx(OutputRegister(conversion), LowRegisterFrom(in), 0, 8);
3876 break;
3877
3878 default:
3879 LOG(FATAL) << "Unexpected type conversion from " << input_type
3880 << " to " << result_type;
3881 }
3882 break;
3883
3884 case DataType::Type::kInt8:
3885 switch (input_type) {
3886 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003887 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003888 case DataType::Type::kInt16:
3889 case DataType::Type::kInt32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003890 __ Sbfx(OutputRegister(conversion), InputRegisterAt(conversion, 0), 0, 8);
3891 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003892 case DataType::Type::kInt64:
3893 __ Sbfx(OutputRegister(conversion), LowRegisterFrom(in), 0, 8);
3894 break;
3895
3896 default:
3897 LOG(FATAL) << "Unexpected type conversion from " << input_type
3898 << " to " << result_type;
3899 }
3900 break;
3901
3902 case DataType::Type::kUint16:
3903 switch (input_type) {
3904 case DataType::Type::kInt8:
3905 case DataType::Type::kInt16:
3906 case DataType::Type::kInt32:
3907 __ Ubfx(OutputRegister(conversion), InputRegisterAt(conversion, 0), 0, 16);
3908 break;
3909 case DataType::Type::kInt64:
3910 __ Ubfx(OutputRegister(conversion), LowRegisterFrom(in), 0, 16);
3911 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01003912
3913 default:
3914 LOG(FATAL) << "Unexpected type conversion from " << input_type
3915 << " to " << result_type;
3916 }
3917 break;
3918
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003919 case DataType::Type::kInt16:
Scott Wakelingfe885462016-09-22 10:24:38 +01003920 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003921 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003922 case DataType::Type::kInt32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003923 __ Sbfx(OutputRegister(conversion), InputRegisterAt(conversion, 0), 0, 16);
3924 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003925 case DataType::Type::kInt64:
3926 __ Sbfx(OutputRegister(conversion), LowRegisterFrom(in), 0, 16);
3927 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01003928
3929 default:
3930 LOG(FATAL) << "Unexpected type conversion from " << input_type
3931 << " to " << result_type;
3932 }
3933 break;
3934
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003935 case DataType::Type::kInt32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003936 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003937 case DataType::Type::kInt64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003938 DCHECK(out.IsRegister());
3939 if (in.IsRegisterPair()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003940 __ Mov(OutputRegister(conversion), LowRegisterFrom(in));
Scott Wakelingfe885462016-09-22 10:24:38 +01003941 } else if (in.IsDoubleStackSlot()) {
3942 GetAssembler()->LoadFromOffset(kLoadWord,
3943 OutputRegister(conversion),
3944 sp,
3945 in.GetStackIndex());
3946 } else {
3947 DCHECK(in.IsConstant());
3948 DCHECK(in.GetConstant()->IsLongConstant());
Vladimir Markoba1a48e2017-04-13 11:50:14 +01003949 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
3950 __ Mov(OutputRegister(conversion), static_cast<int32_t>(value));
Scott Wakelingfe885462016-09-22 10:24:38 +01003951 }
3952 break;
3953
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003954 case DataType::Type::kFloat32: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003955 vixl32::SRegister temp = LowSRegisterFrom(locations->GetTemp(0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01003956 __ Vcvt(S32, F32, temp, InputSRegisterAt(conversion, 0));
Scott Wakelingfe885462016-09-22 10:24:38 +01003957 __ Vmov(OutputRegister(conversion), temp);
3958 break;
3959 }
3960
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003961 case DataType::Type::kFloat64: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003962 vixl32::SRegister temp_s = LowSRegisterFrom(locations->GetTemp(0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01003963 __ Vcvt(S32, F64, temp_s, DRegisterFrom(in));
Scott Wakelingfe885462016-09-22 10:24:38 +01003964 __ Vmov(OutputRegister(conversion), temp_s);
3965 break;
3966 }
3967
3968 default:
3969 LOG(FATAL) << "Unexpected type conversion from " << input_type
3970 << " to " << result_type;
3971 }
3972 break;
3973
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003974 case DataType::Type::kInt64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003975 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003976 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003977 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003978 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003979 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003980 case DataType::Type::kInt16:
3981 case DataType::Type::kInt32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003982 DCHECK(out.IsRegisterPair());
3983 DCHECK(in.IsRegister());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003984 __ Mov(LowRegisterFrom(out), InputRegisterAt(conversion, 0));
Scott Wakelingfe885462016-09-22 10:24:38 +01003985 // Sign extension.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003986 __ Asr(HighRegisterFrom(out), LowRegisterFrom(out), 31);
Scott Wakelingfe885462016-09-22 10:24:38 +01003987 break;
3988
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003989 case DataType::Type::kFloat32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003990 codegen_->InvokeRuntime(kQuickF2l, conversion, conversion->GetDexPc());
3991 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
3992 break;
3993
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003994 case DataType::Type::kFloat64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003995 codegen_->InvokeRuntime(kQuickD2l, conversion, conversion->GetDexPc());
3996 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
3997 break;
3998
3999 default:
4000 LOG(FATAL) << "Unexpected type conversion from " << input_type
4001 << " to " << result_type;
4002 }
4003 break;
4004
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004005 case DataType::Type::kFloat32:
Scott Wakelingfe885462016-09-22 10:24:38 +01004006 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004007 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004008 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004009 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004010 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004011 case DataType::Type::kInt16:
4012 case DataType::Type::kInt32:
Scott Wakelingfe885462016-09-22 10:24:38 +01004013 __ Vmov(OutputSRegister(conversion), InputRegisterAt(conversion, 0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01004014 __ Vcvt(F32, S32, OutputSRegister(conversion), OutputSRegister(conversion));
Scott Wakelingfe885462016-09-22 10:24:38 +01004015 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01004016
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004017 case DataType::Type::kInt64:
Scott Wakelingfe885462016-09-22 10:24:38 +01004018 codegen_->InvokeRuntime(kQuickL2f, conversion, conversion->GetDexPc());
4019 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
4020 break;
4021
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004022 case DataType::Type::kFloat64:
Scott Wakelingc34dba72016-10-03 10:14:44 +01004023 __ Vcvt(F32, F64, OutputSRegister(conversion), DRegisterFrom(in));
Scott Wakelingfe885462016-09-22 10:24:38 +01004024 break;
4025
4026 default:
4027 LOG(FATAL) << "Unexpected type conversion from " << input_type
4028 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08004029 }
Scott Wakelingfe885462016-09-22 10:24:38 +01004030 break;
4031
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004032 case DataType::Type::kFloat64:
Scott Wakelingfe885462016-09-22 10:24:38 +01004033 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004034 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004035 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004036 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004037 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004038 case DataType::Type::kInt16:
4039 case DataType::Type::kInt32:
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004040 __ Vmov(LowSRegisterFrom(out), InputRegisterAt(conversion, 0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01004041 __ Vcvt(F64, S32, DRegisterFrom(out), LowSRegisterFrom(out));
Scott Wakelingfe885462016-09-22 10:24:38 +01004042 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01004043
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004044 case DataType::Type::kInt64: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004045 vixl32::Register low = LowRegisterFrom(in);
4046 vixl32::Register high = HighRegisterFrom(in);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004047 vixl32::SRegister out_s = LowSRegisterFrom(out);
Scott Wakelingc34dba72016-10-03 10:14:44 +01004048 vixl32::DRegister out_d = DRegisterFrom(out);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004049 vixl32::SRegister temp_s = LowSRegisterFrom(locations->GetTemp(0));
Scott Wakelingc34dba72016-10-03 10:14:44 +01004050 vixl32::DRegister temp_d = DRegisterFrom(locations->GetTemp(0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01004051 vixl32::DRegister constant_d = DRegisterFrom(locations->GetTemp(1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004052
4053 // temp_d = int-to-double(high)
4054 __ Vmov(temp_s, high);
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01004055 __ Vcvt(F64, S32, temp_d, temp_s);
Scott Wakelingfe885462016-09-22 10:24:38 +01004056 // constant_d = k2Pow32EncodingForDouble
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004057 __ Vmov(constant_d, bit_cast<double, int64_t>(k2Pow32EncodingForDouble));
Scott Wakelingfe885462016-09-22 10:24:38 +01004058 // out_d = unsigned-to-double(low)
4059 __ Vmov(out_s, low);
4060 __ Vcvt(F64, U32, out_d, out_s);
4061 // out_d += temp_d * constant_d
4062 __ Vmla(F64, out_d, temp_d, constant_d);
4063 break;
4064 }
4065
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004066 case DataType::Type::kFloat32:
Scott Wakelingc34dba72016-10-03 10:14:44 +01004067 __ Vcvt(F64, F32, DRegisterFrom(out), InputSRegisterAt(conversion, 0));
Scott Wakelingfe885462016-09-22 10:24:38 +01004068 break;
4069
4070 default:
4071 LOG(FATAL) << "Unexpected type conversion from " << input_type
4072 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08004073 }
Scott Wakelingfe885462016-09-22 10:24:38 +01004074 break;
4075
4076 default:
4077 LOG(FATAL) << "Unexpected type conversion from " << input_type
4078 << " to " << result_type;
4079 }
4080}
4081
4082void LocationsBuilderARMVIXL::VisitAdd(HAdd* add) {
4083 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004084 new (GetGraph()->GetAllocator()) LocationSummary(add, LocationSummary::kNoCall);
Scott Wakelingfe885462016-09-22 10:24:38 +01004085 switch (add->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004086 case DataType::Type::kInt32: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004087 locations->SetInAt(0, Location::RequiresRegister());
4088 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
4089 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4090 break;
4091 }
4092
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004093 case DataType::Type::kInt64: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004094 locations->SetInAt(0, Location::RequiresRegister());
Anton Kirilovdda43962016-11-21 19:55:20 +00004095 locations->SetInAt(1, ArmEncodableConstantOrRegister(add->InputAt(1), ADD));
Scott Wakelingfe885462016-09-22 10:24:38 +01004096 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4097 break;
4098 }
4099
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004100 case DataType::Type::kFloat32:
4101 case DataType::Type::kFloat64: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004102 locations->SetInAt(0, Location::RequiresFpuRegister());
4103 locations->SetInAt(1, Location::RequiresFpuRegister());
4104 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4105 break;
4106 }
4107
4108 default:
4109 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
4110 }
4111}
4112
4113void InstructionCodeGeneratorARMVIXL::VisitAdd(HAdd* add) {
4114 LocationSummary* locations = add->GetLocations();
4115 Location out = locations->Out();
4116 Location first = locations->InAt(0);
4117 Location second = locations->InAt(1);
4118
4119 switch (add->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004120 case DataType::Type::kInt32: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004121 __ Add(OutputRegister(add), InputRegisterAt(add, 0), InputOperandAt(add, 1));
4122 }
4123 break;
4124
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004125 case DataType::Type::kInt64: {
Anton Kirilovdda43962016-11-21 19:55:20 +00004126 if (second.IsConstant()) {
4127 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
4128 GenerateAddLongConst(out, first, value);
4129 } else {
4130 DCHECK(second.IsRegisterPair());
4131 __ Adds(LowRegisterFrom(out), LowRegisterFrom(first), LowRegisterFrom(second));
4132 __ Adc(HighRegisterFrom(out), HighRegisterFrom(first), HighRegisterFrom(second));
4133 }
Scott Wakelingfe885462016-09-22 10:24:38 +01004134 break;
4135 }
4136
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004137 case DataType::Type::kFloat32:
4138 case DataType::Type::kFloat64:
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004139 __ Vadd(OutputVRegister(add), InputVRegisterAt(add, 0), InputVRegisterAt(add, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004140 break;
4141
4142 default:
4143 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
4144 }
4145}
4146
4147void LocationsBuilderARMVIXL::VisitSub(HSub* sub) {
4148 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004149 new (GetGraph()->GetAllocator()) LocationSummary(sub, LocationSummary::kNoCall);
Scott Wakelingfe885462016-09-22 10:24:38 +01004150 switch (sub->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004151 case DataType::Type::kInt32: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004152 locations->SetInAt(0, Location::RequiresRegister());
4153 locations->SetInAt(1, Location::RegisterOrConstant(sub->InputAt(1)));
4154 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4155 break;
4156 }
4157
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004158 case DataType::Type::kInt64: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004159 locations->SetInAt(0, Location::RequiresRegister());
Anton Kirilovdda43962016-11-21 19:55:20 +00004160 locations->SetInAt(1, ArmEncodableConstantOrRegister(sub->InputAt(1), SUB));
Scott Wakelingfe885462016-09-22 10:24:38 +01004161 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4162 break;
4163 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004164 case DataType::Type::kFloat32:
4165 case DataType::Type::kFloat64: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004166 locations->SetInAt(0, Location::RequiresFpuRegister());
4167 locations->SetInAt(1, Location::RequiresFpuRegister());
4168 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4169 break;
4170 }
4171 default:
4172 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
4173 }
4174}
4175
4176void InstructionCodeGeneratorARMVIXL::VisitSub(HSub* sub) {
4177 LocationSummary* locations = sub->GetLocations();
4178 Location out = locations->Out();
4179 Location first = locations->InAt(0);
4180 Location second = locations->InAt(1);
4181 switch (sub->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004182 case DataType::Type::kInt32: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004183 __ Sub(OutputRegister(sub), InputRegisterAt(sub, 0), InputOperandAt(sub, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004184 break;
4185 }
4186
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004187 case DataType::Type::kInt64: {
Anton Kirilovdda43962016-11-21 19:55:20 +00004188 if (second.IsConstant()) {
4189 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
4190 GenerateAddLongConst(out, first, -value);
4191 } else {
4192 DCHECK(second.IsRegisterPair());
4193 __ Subs(LowRegisterFrom(out), LowRegisterFrom(first), LowRegisterFrom(second));
4194 __ Sbc(HighRegisterFrom(out), HighRegisterFrom(first), HighRegisterFrom(second));
4195 }
Scott Wakelingfe885462016-09-22 10:24:38 +01004196 break;
4197 }
4198
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004199 case DataType::Type::kFloat32:
4200 case DataType::Type::kFloat64:
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004201 __ Vsub(OutputVRegister(sub), InputVRegisterAt(sub, 0), InputVRegisterAt(sub, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004202 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01004203
4204 default:
4205 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
4206 }
4207}
4208
4209void LocationsBuilderARMVIXL::VisitMul(HMul* mul) {
4210 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004211 new (GetGraph()->GetAllocator()) LocationSummary(mul, LocationSummary::kNoCall);
Scott Wakelingfe885462016-09-22 10:24:38 +01004212 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004213 case DataType::Type::kInt32:
4214 case DataType::Type::kInt64: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004215 locations->SetInAt(0, Location::RequiresRegister());
4216 locations->SetInAt(1, Location::RequiresRegister());
4217 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4218 break;
4219 }
4220
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004221 case DataType::Type::kFloat32:
4222 case DataType::Type::kFloat64: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004223 locations->SetInAt(0, Location::RequiresFpuRegister());
4224 locations->SetInAt(1, Location::RequiresFpuRegister());
4225 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4226 break;
4227 }
4228
4229 default:
4230 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4231 }
4232}
4233
4234void InstructionCodeGeneratorARMVIXL::VisitMul(HMul* mul) {
4235 LocationSummary* locations = mul->GetLocations();
4236 Location out = locations->Out();
4237 Location first = locations->InAt(0);
4238 Location second = locations->InAt(1);
4239 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004240 case DataType::Type::kInt32: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004241 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
4242 break;
4243 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004244 case DataType::Type::kInt64: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004245 vixl32::Register out_hi = HighRegisterFrom(out);
4246 vixl32::Register out_lo = LowRegisterFrom(out);
4247 vixl32::Register in1_hi = HighRegisterFrom(first);
4248 vixl32::Register in1_lo = LowRegisterFrom(first);
4249 vixl32::Register in2_hi = HighRegisterFrom(second);
4250 vixl32::Register in2_lo = LowRegisterFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01004251
4252 // Extra checks to protect caused by the existence of R1_R2.
4253 // The algorithm is wrong if out.hi is either in1.lo or in2.lo:
4254 // (e.g. in1=r0_r1, in2=r2_r3 and out=r1_r2);
Anton Kirilov644032c2016-12-06 17:51:43 +00004255 DCHECK(!out_hi.Is(in1_lo));
4256 DCHECK(!out_hi.Is(in2_lo));
Scott Wakelingfe885462016-09-22 10:24:38 +01004257
4258 // input: in1 - 64 bits, in2 - 64 bits
4259 // output: out
4260 // formula: out.hi : out.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
4261 // parts: out.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
4262 // parts: out.lo = (in1.lo * in2.lo)[31:0]
4263
4264 UseScratchRegisterScope temps(GetVIXLAssembler());
4265 vixl32::Register temp = temps.Acquire();
4266 // temp <- in1.lo * in2.hi
4267 __ Mul(temp, in1_lo, in2_hi);
4268 // out.hi <- in1.lo * in2.hi + in1.hi * in2.lo
4269 __ Mla(out_hi, in1_hi, in2_lo, temp);
4270 // out.lo <- (in1.lo * in2.lo)[31:0];
4271 __ Umull(out_lo, temp, in1_lo, in2_lo);
4272 // out.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004273 __ Add(out_hi, out_hi, temp);
Scott Wakelingfe885462016-09-22 10:24:38 +01004274 break;
4275 }
4276
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004277 case DataType::Type::kFloat32:
4278 case DataType::Type::kFloat64:
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004279 __ Vmul(OutputVRegister(mul), InputVRegisterAt(mul, 0), InputVRegisterAt(mul, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004280 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01004281
4282 default:
4283 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4284 }
4285}
4286
Scott Wakelingfe885462016-09-22 10:24:38 +01004287void InstructionCodeGeneratorARMVIXL::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
4288 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004289 DCHECK(instruction->GetResultType() == DataType::Type::kInt32);
Scott Wakelingfe885462016-09-22 10:24:38 +01004290
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004291 Location second = instruction->GetLocations()->InAt(1);
Scott Wakelingfe885462016-09-22 10:24:38 +01004292 DCHECK(second.IsConstant());
4293
4294 vixl32::Register out = OutputRegister(instruction);
4295 vixl32::Register dividend = InputRegisterAt(instruction, 0);
Anton Kirilov644032c2016-12-06 17:51:43 +00004296 int32_t imm = Int32ConstantFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01004297 DCHECK(imm == 1 || imm == -1);
4298
4299 if (instruction->IsRem()) {
4300 __ Mov(out, 0);
4301 } else {
4302 if (imm == 1) {
4303 __ Mov(out, dividend);
4304 } else {
4305 __ Rsb(out, dividend, 0);
4306 }
4307 }
4308}
4309
4310void InstructionCodeGeneratorARMVIXL::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
4311 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004312 DCHECK(instruction->GetResultType() == DataType::Type::kInt32);
Scott Wakelingfe885462016-09-22 10:24:38 +01004313
4314 LocationSummary* locations = instruction->GetLocations();
4315 Location second = locations->InAt(1);
4316 DCHECK(second.IsConstant());
4317
4318 vixl32::Register out = OutputRegister(instruction);
4319 vixl32::Register dividend = InputRegisterAt(instruction, 0);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004320 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
Anton Kirilov644032c2016-12-06 17:51:43 +00004321 int32_t imm = Int32ConstantFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01004322 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
4323 int ctz_imm = CTZ(abs_imm);
4324
4325 if (ctz_imm == 1) {
4326 __ Lsr(temp, dividend, 32 - ctz_imm);
4327 } else {
4328 __ Asr(temp, dividend, 31);
4329 __ Lsr(temp, temp, 32 - ctz_imm);
4330 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004331 __ Add(out, temp, dividend);
Scott Wakelingfe885462016-09-22 10:24:38 +01004332
4333 if (instruction->IsDiv()) {
4334 __ Asr(out, out, ctz_imm);
4335 if (imm < 0) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004336 __ Rsb(out, out, 0);
Scott Wakelingfe885462016-09-22 10:24:38 +01004337 }
4338 } else {
4339 __ Ubfx(out, out, 0, ctz_imm);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004340 __ Sub(out, out, temp);
Scott Wakelingfe885462016-09-22 10:24:38 +01004341 }
4342}
4343
4344void InstructionCodeGeneratorARMVIXL::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
4345 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004346 DCHECK(instruction->GetResultType() == DataType::Type::kInt32);
Scott Wakelingfe885462016-09-22 10:24:38 +01004347
4348 LocationSummary* locations = instruction->GetLocations();
4349 Location second = locations->InAt(1);
4350 DCHECK(second.IsConstant());
4351
4352 vixl32::Register out = OutputRegister(instruction);
4353 vixl32::Register dividend = InputRegisterAt(instruction, 0);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004354 vixl32::Register temp1 = RegisterFrom(locations->GetTemp(0));
4355 vixl32::Register temp2 = RegisterFrom(locations->GetTemp(1));
Scott Wakelingb77051e2016-11-21 19:46:00 +00004356 int32_t imm = Int32ConstantFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01004357
4358 int64_t magic;
4359 int shift;
4360 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
4361
Anton Kirilovdda43962016-11-21 19:55:20 +00004362 // TODO(VIXL): Change the static cast to Operand::From() after VIXL is fixed.
4363 __ Mov(temp1, static_cast<int32_t>(magic));
Scott Wakelingfe885462016-09-22 10:24:38 +01004364 __ Smull(temp2, temp1, dividend, temp1);
4365
4366 if (imm > 0 && magic < 0) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004367 __ Add(temp1, temp1, dividend);
Scott Wakelingfe885462016-09-22 10:24:38 +01004368 } else if (imm < 0 && magic > 0) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004369 __ Sub(temp1, temp1, dividend);
Scott Wakelingfe885462016-09-22 10:24:38 +01004370 }
4371
4372 if (shift != 0) {
4373 __ Asr(temp1, temp1, shift);
4374 }
4375
4376 if (instruction->IsDiv()) {
4377 __ Sub(out, temp1, Operand(temp1, vixl32::Shift(ASR), 31));
4378 } else {
4379 __ Sub(temp1, temp1, Operand(temp1, vixl32::Shift(ASR), 31));
4380 // TODO: Strength reduction for mls.
4381 __ Mov(temp2, imm);
4382 __ Mls(out, temp1, temp2, dividend);
4383 }
4384}
4385
4386void InstructionCodeGeneratorARMVIXL::GenerateDivRemConstantIntegral(
4387 HBinaryOperation* instruction) {
4388 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004389 DCHECK(instruction->GetResultType() == DataType::Type::kInt32);
Scott Wakelingfe885462016-09-22 10:24:38 +01004390
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004391 Location second = instruction->GetLocations()->InAt(1);
Scott Wakelingfe885462016-09-22 10:24:38 +01004392 DCHECK(second.IsConstant());
4393
Anton Kirilov644032c2016-12-06 17:51:43 +00004394 int32_t imm = Int32ConstantFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01004395 if (imm == 0) {
4396 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
4397 } else if (imm == 1 || imm == -1) {
4398 DivRemOneOrMinusOne(instruction);
4399 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
4400 DivRemByPowerOfTwo(instruction);
4401 } else {
4402 DCHECK(imm <= -2 || imm >= 2);
4403 GenerateDivRemWithAnyConstant(instruction);
4404 }
4405}
4406
4407void LocationsBuilderARMVIXL::VisitDiv(HDiv* div) {
4408 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004409 if (div->GetResultType() == DataType::Type::kInt64) {
Scott Wakelingfe885462016-09-22 10:24:38 +01004410 // pLdiv runtime call.
4411 call_kind = LocationSummary::kCallOnMainOnly;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004412 } else if (div->GetResultType() == DataType::Type::kInt32 && div->InputAt(1)->IsConstant()) {
Scott Wakelingfe885462016-09-22 10:24:38 +01004413 // sdiv will be replaced by other instruction sequence.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004414 } else if (div->GetResultType() == DataType::Type::kInt32 &&
Scott Wakelingfe885462016-09-22 10:24:38 +01004415 !codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4416 // pIdivmod runtime call.
4417 call_kind = LocationSummary::kCallOnMainOnly;
4418 }
4419
Vladimir Markoca6fff82017-10-03 14:49:14 +01004420 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(div, call_kind);
Scott Wakelingfe885462016-09-22 10:24:38 +01004421
4422 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004423 case DataType::Type::kInt32: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004424 if (div->InputAt(1)->IsConstant()) {
4425 locations->SetInAt(0, Location::RequiresRegister());
4426 locations->SetInAt(1, Location::ConstantLocation(div->InputAt(1)->AsConstant()));
4427 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Anton Kirilov644032c2016-12-06 17:51:43 +00004428 int32_t value = Int32ConstantFrom(div->InputAt(1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004429 if (value == 1 || value == 0 || value == -1) {
4430 // No temp register required.
4431 } else {
4432 locations->AddTemp(Location::RequiresRegister());
4433 if (!IsPowerOfTwo(AbsOrMin(value))) {
4434 locations->AddTemp(Location::RequiresRegister());
4435 }
4436 }
4437 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4438 locations->SetInAt(0, Location::RequiresRegister());
4439 locations->SetInAt(1, Location::RequiresRegister());
4440 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4441 } else {
Artem Serov551b28f2016-10-18 19:11:30 +01004442 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4443 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4444 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
Roland Levillain5e8d5f02016-10-18 18:03:43 +01004445 // Note: divmod will compute both the quotient and the remainder as the pair R0 and R1, but
Artem Serov551b28f2016-10-18 19:11:30 +01004446 // we only need the former.
4447 locations->SetOut(LocationFrom(r0));
Scott Wakelingfe885462016-09-22 10:24:38 +01004448 }
4449 break;
4450 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004451 case DataType::Type::kInt64: {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01004452 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4453 locations->SetInAt(0, LocationFrom(
4454 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
4455 locations->SetInAt(1, LocationFrom(
4456 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
4457 locations->SetOut(LocationFrom(r0, r1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004458 break;
4459 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004460 case DataType::Type::kFloat32:
4461 case DataType::Type::kFloat64: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004462 locations->SetInAt(0, Location::RequiresFpuRegister());
4463 locations->SetInAt(1, Location::RequiresFpuRegister());
4464 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4465 break;
4466 }
4467
4468 default:
4469 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
4470 }
4471}
4472
4473void InstructionCodeGeneratorARMVIXL::VisitDiv(HDiv* div) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01004474 Location lhs = div->GetLocations()->InAt(0);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004475 Location rhs = div->GetLocations()->InAt(1);
Scott Wakelingfe885462016-09-22 10:24:38 +01004476
4477 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004478 case DataType::Type::kInt32: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004479 if (rhs.IsConstant()) {
Scott Wakelingfe885462016-09-22 10:24:38 +01004480 GenerateDivRemConstantIntegral(div);
4481 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4482 __ Sdiv(OutputRegister(div), InputRegisterAt(div, 0), InputRegisterAt(div, 1));
4483 } else {
Artem Serov551b28f2016-10-18 19:11:30 +01004484 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4485 DCHECK(calling_convention.GetRegisterAt(0).Is(RegisterFrom(lhs)));
4486 DCHECK(calling_convention.GetRegisterAt(1).Is(RegisterFrom(rhs)));
4487 DCHECK(r0.Is(OutputRegister(div)));
4488
4489 codegen_->InvokeRuntime(kQuickIdivmod, div, div->GetDexPc());
4490 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
Scott Wakelingfe885462016-09-22 10:24:38 +01004491 }
4492 break;
4493 }
4494
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004495 case DataType::Type::kInt64: {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01004496 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4497 DCHECK(calling_convention.GetRegisterAt(0).Is(LowRegisterFrom(lhs)));
4498 DCHECK(calling_convention.GetRegisterAt(1).Is(HighRegisterFrom(lhs)));
4499 DCHECK(calling_convention.GetRegisterAt(2).Is(LowRegisterFrom(rhs)));
4500 DCHECK(calling_convention.GetRegisterAt(3).Is(HighRegisterFrom(rhs)));
4501 DCHECK(LowRegisterFrom(div->GetLocations()->Out()).Is(r0));
4502 DCHECK(HighRegisterFrom(div->GetLocations()->Out()).Is(r1));
4503
4504 codegen_->InvokeRuntime(kQuickLdiv, div, div->GetDexPc());
4505 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Scott Wakelingfe885462016-09-22 10:24:38 +01004506 break;
4507 }
4508
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004509 case DataType::Type::kFloat32:
4510 case DataType::Type::kFloat64:
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004511 __ Vdiv(OutputVRegister(div), InputVRegisterAt(div, 0), InputVRegisterAt(div, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004512 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01004513
4514 default:
4515 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
4516 }
4517}
4518
Artem Serov551b28f2016-10-18 19:11:30 +01004519void LocationsBuilderARMVIXL::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004520 DataType::Type type = rem->GetResultType();
Artem Serov551b28f2016-10-18 19:11:30 +01004521
4522 // Most remainders are implemented in the runtime.
4523 LocationSummary::CallKind call_kind = LocationSummary::kCallOnMainOnly;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004524 if (rem->GetResultType() == DataType::Type::kInt32 && rem->InputAt(1)->IsConstant()) {
Artem Serov551b28f2016-10-18 19:11:30 +01004525 // sdiv will be replaced by other instruction sequence.
4526 call_kind = LocationSummary::kNoCall;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004527 } else if ((rem->GetResultType() == DataType::Type::kInt32)
Artem Serov551b28f2016-10-18 19:11:30 +01004528 && codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4529 // Have hardware divide instruction for int, do it with three instructions.
4530 call_kind = LocationSummary::kNoCall;
4531 }
4532
Vladimir Markoca6fff82017-10-03 14:49:14 +01004533 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(rem, call_kind);
Artem Serov551b28f2016-10-18 19:11:30 +01004534
4535 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004536 case DataType::Type::kInt32: {
Artem Serov551b28f2016-10-18 19:11:30 +01004537 if (rem->InputAt(1)->IsConstant()) {
4538 locations->SetInAt(0, Location::RequiresRegister());
4539 locations->SetInAt(1, Location::ConstantLocation(rem->InputAt(1)->AsConstant()));
4540 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Anton Kirilov644032c2016-12-06 17:51:43 +00004541 int32_t value = Int32ConstantFrom(rem->InputAt(1));
Artem Serov551b28f2016-10-18 19:11:30 +01004542 if (value == 1 || value == 0 || value == -1) {
4543 // No temp register required.
4544 } else {
4545 locations->AddTemp(Location::RequiresRegister());
4546 if (!IsPowerOfTwo(AbsOrMin(value))) {
4547 locations->AddTemp(Location::RequiresRegister());
4548 }
4549 }
4550 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4551 locations->SetInAt(0, Location::RequiresRegister());
4552 locations->SetInAt(1, Location::RequiresRegister());
4553 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4554 locations->AddTemp(Location::RequiresRegister());
4555 } else {
4556 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4557 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4558 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
Roland Levillain5e8d5f02016-10-18 18:03:43 +01004559 // Note: divmod will compute both the quotient and the remainder as the pair R0 and R1, but
Artem Serov551b28f2016-10-18 19:11:30 +01004560 // we only need the latter.
4561 locations->SetOut(LocationFrom(r1));
4562 }
4563 break;
4564 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004565 case DataType::Type::kInt64: {
Artem Serov551b28f2016-10-18 19:11:30 +01004566 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4567 locations->SetInAt(0, LocationFrom(
4568 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
4569 locations->SetInAt(1, LocationFrom(
4570 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
4571 // The runtime helper puts the output in R2,R3.
4572 locations->SetOut(LocationFrom(r2, r3));
4573 break;
4574 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004575 case DataType::Type::kFloat32: {
Artem Serov551b28f2016-10-18 19:11:30 +01004576 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4577 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
4578 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
4579 locations->SetOut(LocationFrom(s0));
4580 break;
4581 }
4582
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004583 case DataType::Type::kFloat64: {
Artem Serov551b28f2016-10-18 19:11:30 +01004584 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4585 locations->SetInAt(0, LocationFrom(
4586 calling_convention.GetFpuRegisterAt(0), calling_convention.GetFpuRegisterAt(1)));
4587 locations->SetInAt(1, LocationFrom(
4588 calling_convention.GetFpuRegisterAt(2), calling_convention.GetFpuRegisterAt(3)));
4589 locations->SetOut(LocationFrom(s0, s1));
4590 break;
4591 }
4592
4593 default:
4594 LOG(FATAL) << "Unexpected rem type " << type;
4595 }
4596}
4597
4598void InstructionCodeGeneratorARMVIXL::VisitRem(HRem* rem) {
4599 LocationSummary* locations = rem->GetLocations();
4600 Location second = locations->InAt(1);
4601
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004602 DataType::Type type = rem->GetResultType();
Artem Serov551b28f2016-10-18 19:11:30 +01004603 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004604 case DataType::Type::kInt32: {
Artem Serov551b28f2016-10-18 19:11:30 +01004605 vixl32::Register reg1 = InputRegisterAt(rem, 0);
4606 vixl32::Register out_reg = OutputRegister(rem);
4607 if (second.IsConstant()) {
4608 GenerateDivRemConstantIntegral(rem);
4609 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4610 vixl32::Register reg2 = RegisterFrom(second);
4611 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
4612
4613 // temp = reg1 / reg2 (integer division)
4614 // dest = reg1 - temp * reg2
4615 __ Sdiv(temp, reg1, reg2);
4616 __ Mls(out_reg, temp, reg2, reg1);
4617 } else {
4618 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4619 DCHECK(reg1.Is(calling_convention.GetRegisterAt(0)));
4620 DCHECK(RegisterFrom(second).Is(calling_convention.GetRegisterAt(1)));
4621 DCHECK(out_reg.Is(r1));
4622
4623 codegen_->InvokeRuntime(kQuickIdivmod, rem, rem->GetDexPc());
4624 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
4625 }
4626 break;
4627 }
4628
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004629 case DataType::Type::kInt64: {
Artem Serov551b28f2016-10-18 19:11:30 +01004630 codegen_->InvokeRuntime(kQuickLmod, rem, rem->GetDexPc());
4631 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
4632 break;
4633 }
4634
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004635 case DataType::Type::kFloat32: {
Artem Serov551b28f2016-10-18 19:11:30 +01004636 codegen_->InvokeRuntime(kQuickFmodf, rem, rem->GetDexPc());
4637 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
4638 break;
4639 }
4640
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004641 case DataType::Type::kFloat64: {
Artem Serov551b28f2016-10-18 19:11:30 +01004642 codegen_->InvokeRuntime(kQuickFmod, rem, rem->GetDexPc());
4643 CheckEntrypointTypes<kQuickFmod, double, double, double>();
4644 break;
4645 }
4646
4647 default:
4648 LOG(FATAL) << "Unexpected rem type " << type;
4649 }
4650}
4651
4652
Scott Wakelingfe885462016-09-22 10:24:38 +01004653void LocationsBuilderARMVIXL::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Artem Serov657022c2016-11-23 14:19:38 +00004654 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Scott Wakelingfe885462016-09-22 10:24:38 +01004655 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Scott Wakelingfe885462016-09-22 10:24:38 +01004656}
4657
4658void InstructionCodeGeneratorARMVIXL::VisitDivZeroCheck(HDivZeroCheck* instruction) {
4659 DivZeroCheckSlowPathARMVIXL* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01004660 new (codegen_->GetScopedAllocator()) DivZeroCheckSlowPathARMVIXL(instruction);
Scott Wakelingfe885462016-09-22 10:24:38 +01004661 codegen_->AddSlowPath(slow_path);
4662
4663 LocationSummary* locations = instruction->GetLocations();
4664 Location value = locations->InAt(0);
4665
4666 switch (instruction->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004667 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004668 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004669 case DataType::Type::kInt8:
4670 case DataType::Type::kUint16:
4671 case DataType::Type::kInt16:
4672 case DataType::Type::kInt32: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004673 if (value.IsRegister()) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00004674 __ CompareAndBranchIfZero(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
Scott Wakelingfe885462016-09-22 10:24:38 +01004675 } else {
4676 DCHECK(value.IsConstant()) << value;
Anton Kirilov644032c2016-12-06 17:51:43 +00004677 if (Int32ConstantFrom(value) == 0) {
Scott Wakelingfe885462016-09-22 10:24:38 +01004678 __ B(slow_path->GetEntryLabel());
4679 }
4680 }
4681 break;
4682 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004683 case DataType::Type::kInt64: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004684 if (value.IsRegisterPair()) {
4685 UseScratchRegisterScope temps(GetVIXLAssembler());
4686 vixl32::Register temp = temps.Acquire();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004687 __ Orrs(temp, LowRegisterFrom(value), HighRegisterFrom(value));
Scott Wakelingfe885462016-09-22 10:24:38 +01004688 __ B(eq, slow_path->GetEntryLabel());
4689 } else {
4690 DCHECK(value.IsConstant()) << value;
Anton Kirilov644032c2016-12-06 17:51:43 +00004691 if (Int64ConstantFrom(value) == 0) {
Scott Wakelingfe885462016-09-22 10:24:38 +01004692 __ B(slow_path->GetEntryLabel());
4693 }
4694 }
4695 break;
4696 }
4697 default:
4698 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
4699 }
4700}
4701
Artem Serov02109dd2016-09-23 17:17:54 +01004702void InstructionCodeGeneratorARMVIXL::HandleIntegerRotate(HRor* ror) {
4703 LocationSummary* locations = ror->GetLocations();
4704 vixl32::Register in = InputRegisterAt(ror, 0);
4705 Location rhs = locations->InAt(1);
4706 vixl32::Register out = OutputRegister(ror);
4707
4708 if (rhs.IsConstant()) {
4709 // Arm32 and Thumb2 assemblers require a rotation on the interval [1,31],
4710 // so map all rotations to a +ve. equivalent in that range.
4711 // (e.g. left *or* right by -2 bits == 30 bits in the same direction.)
4712 uint32_t rot = CodeGenerator::GetInt32ValueOf(rhs.GetConstant()) & 0x1F;
4713 if (rot) {
4714 // Rotate, mapping left rotations to right equivalents if necessary.
4715 // (e.g. left by 2 bits == right by 30.)
4716 __ Ror(out, in, rot);
4717 } else if (!out.Is(in)) {
4718 __ Mov(out, in);
4719 }
4720 } else {
4721 __ Ror(out, in, RegisterFrom(rhs));
4722 }
4723}
4724
4725// Gain some speed by mapping all Long rotates onto equivalent pairs of Integer
4726// rotates by swapping input regs (effectively rotating by the first 32-bits of
4727// a larger rotation) or flipping direction (thus treating larger right/left
4728// rotations as sub-word sized rotations in the other direction) as appropriate.
4729void InstructionCodeGeneratorARMVIXL::HandleLongRotate(HRor* ror) {
4730 LocationSummary* locations = ror->GetLocations();
4731 vixl32::Register in_reg_lo = LowRegisterFrom(locations->InAt(0));
4732 vixl32::Register in_reg_hi = HighRegisterFrom(locations->InAt(0));
4733 Location rhs = locations->InAt(1);
4734 vixl32::Register out_reg_lo = LowRegisterFrom(locations->Out());
4735 vixl32::Register out_reg_hi = HighRegisterFrom(locations->Out());
4736
4737 if (rhs.IsConstant()) {
4738 uint64_t rot = CodeGenerator::GetInt64ValueOf(rhs.GetConstant());
4739 // Map all rotations to +ve. equivalents on the interval [0,63].
4740 rot &= kMaxLongShiftDistance;
4741 // For rotates over a word in size, 'pre-rotate' by 32-bits to keep rotate
4742 // logic below to a simple pair of binary orr.
4743 // (e.g. 34 bits == in_reg swap + 2 bits right.)
4744 if (rot >= kArmBitsPerWord) {
4745 rot -= kArmBitsPerWord;
4746 std::swap(in_reg_hi, in_reg_lo);
4747 }
4748 // Rotate, or mov to out for zero or word size rotations.
4749 if (rot != 0u) {
Scott Wakelingb77051e2016-11-21 19:46:00 +00004750 __ Lsr(out_reg_hi, in_reg_hi, Operand::From(rot));
Artem Serov02109dd2016-09-23 17:17:54 +01004751 __ Orr(out_reg_hi, out_reg_hi, Operand(in_reg_lo, ShiftType::LSL, kArmBitsPerWord - rot));
Scott Wakelingb77051e2016-11-21 19:46:00 +00004752 __ Lsr(out_reg_lo, in_reg_lo, Operand::From(rot));
Artem Serov02109dd2016-09-23 17:17:54 +01004753 __ Orr(out_reg_lo, out_reg_lo, Operand(in_reg_hi, ShiftType::LSL, kArmBitsPerWord - rot));
4754 } else {
4755 __ Mov(out_reg_lo, in_reg_lo);
4756 __ Mov(out_reg_hi, in_reg_hi);
4757 }
4758 } else {
4759 vixl32::Register shift_right = RegisterFrom(locations->GetTemp(0));
4760 vixl32::Register shift_left = RegisterFrom(locations->GetTemp(1));
4761 vixl32::Label end;
4762 vixl32::Label shift_by_32_plus_shift_right;
Anton Kirilov6f644202017-02-27 18:29:45 +00004763 vixl32::Label* final_label = codegen_->GetFinalLabel(ror, &end);
Artem Serov02109dd2016-09-23 17:17:54 +01004764
4765 __ And(shift_right, RegisterFrom(rhs), 0x1F);
4766 __ Lsrs(shift_left, RegisterFrom(rhs), 6);
Scott Wakelingbffdc702016-12-07 17:46:03 +00004767 __ Rsb(LeaveFlags, shift_left, shift_right, Operand::From(kArmBitsPerWord));
Artem Serov517d9f62016-12-12 15:51:15 +00004768 __ B(cc, &shift_by_32_plus_shift_right, /* far_target */ false);
Artem Serov02109dd2016-09-23 17:17:54 +01004769
4770 // out_reg_hi = (reg_hi << shift_left) | (reg_lo >> shift_right).
4771 // out_reg_lo = (reg_lo << shift_left) | (reg_hi >> shift_right).
4772 __ Lsl(out_reg_hi, in_reg_hi, shift_left);
4773 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
4774 __ Add(out_reg_hi, out_reg_hi, out_reg_lo);
4775 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
4776 __ Lsr(shift_left, in_reg_hi, shift_right);
4777 __ Add(out_reg_lo, out_reg_lo, shift_left);
Anton Kirilov6f644202017-02-27 18:29:45 +00004778 __ B(final_label);
Artem Serov02109dd2016-09-23 17:17:54 +01004779
4780 __ Bind(&shift_by_32_plus_shift_right); // Shift by 32+shift_right.
4781 // out_reg_hi = (reg_hi >> shift_right) | (reg_lo << shift_left).
4782 // out_reg_lo = (reg_lo >> shift_right) | (reg_hi << shift_left).
4783 __ Lsr(out_reg_hi, in_reg_hi, shift_right);
4784 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
4785 __ Add(out_reg_hi, out_reg_hi, out_reg_lo);
4786 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
4787 __ Lsl(shift_right, in_reg_hi, shift_left);
4788 __ Add(out_reg_lo, out_reg_lo, shift_right);
4789
Anton Kirilov6f644202017-02-27 18:29:45 +00004790 if (end.IsReferenced()) {
4791 __ Bind(&end);
4792 }
Artem Serov02109dd2016-09-23 17:17:54 +01004793 }
4794}
4795
4796void LocationsBuilderARMVIXL::VisitRor(HRor* ror) {
4797 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004798 new (GetGraph()->GetAllocator()) LocationSummary(ror, LocationSummary::kNoCall);
Artem Serov02109dd2016-09-23 17:17:54 +01004799 switch (ror->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004800 case DataType::Type::kInt32: {
Artem Serov02109dd2016-09-23 17:17:54 +01004801 locations->SetInAt(0, Location::RequiresRegister());
4802 locations->SetInAt(1, Location::RegisterOrConstant(ror->InputAt(1)));
4803 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4804 break;
4805 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004806 case DataType::Type::kInt64: {
Artem Serov02109dd2016-09-23 17:17:54 +01004807 locations->SetInAt(0, Location::RequiresRegister());
4808 if (ror->InputAt(1)->IsConstant()) {
4809 locations->SetInAt(1, Location::ConstantLocation(ror->InputAt(1)->AsConstant()));
4810 } else {
4811 locations->SetInAt(1, Location::RequiresRegister());
4812 locations->AddTemp(Location::RequiresRegister());
4813 locations->AddTemp(Location::RequiresRegister());
4814 }
4815 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
4816 break;
4817 }
4818 default:
4819 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
4820 }
4821}
4822
4823void InstructionCodeGeneratorARMVIXL::VisitRor(HRor* ror) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004824 DataType::Type type = ror->GetResultType();
Artem Serov02109dd2016-09-23 17:17:54 +01004825 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004826 case DataType::Type::kInt32: {
Artem Serov02109dd2016-09-23 17:17:54 +01004827 HandleIntegerRotate(ror);
4828 break;
4829 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004830 case DataType::Type::kInt64: {
Artem Serov02109dd2016-09-23 17:17:54 +01004831 HandleLongRotate(ror);
4832 break;
4833 }
4834 default:
4835 LOG(FATAL) << "Unexpected operation type " << type;
4836 UNREACHABLE();
4837 }
4838}
4839
Artem Serov02d37832016-10-25 15:25:33 +01004840void LocationsBuilderARMVIXL::HandleShift(HBinaryOperation* op) {
4841 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
4842
4843 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004844 new (GetGraph()->GetAllocator()) LocationSummary(op, LocationSummary::kNoCall);
Artem Serov02d37832016-10-25 15:25:33 +01004845
4846 switch (op->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004847 case DataType::Type::kInt32: {
Artem Serov02d37832016-10-25 15:25:33 +01004848 locations->SetInAt(0, Location::RequiresRegister());
4849 if (op->InputAt(1)->IsConstant()) {
4850 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
4851 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4852 } else {
4853 locations->SetInAt(1, Location::RequiresRegister());
4854 // Make the output overlap, as it will be used to hold the masked
4855 // second input.
4856 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
4857 }
4858 break;
4859 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004860 case DataType::Type::kInt64: {
Artem Serov02d37832016-10-25 15:25:33 +01004861 locations->SetInAt(0, Location::RequiresRegister());
4862 if (op->InputAt(1)->IsConstant()) {
4863 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
4864 // For simplicity, use kOutputOverlap even though we only require that low registers
4865 // don't clash with high registers which the register allocator currently guarantees.
4866 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
4867 } else {
4868 locations->SetInAt(1, Location::RequiresRegister());
4869 locations->AddTemp(Location::RequiresRegister());
4870 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
4871 }
4872 break;
4873 }
4874 default:
4875 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
4876 }
4877}
4878
4879void InstructionCodeGeneratorARMVIXL::HandleShift(HBinaryOperation* op) {
4880 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
4881
4882 LocationSummary* locations = op->GetLocations();
4883 Location out = locations->Out();
4884 Location first = locations->InAt(0);
4885 Location second = locations->InAt(1);
4886
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004887 DataType::Type type = op->GetResultType();
Artem Serov02d37832016-10-25 15:25:33 +01004888 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004889 case DataType::Type::kInt32: {
Artem Serov02d37832016-10-25 15:25:33 +01004890 vixl32::Register out_reg = OutputRegister(op);
4891 vixl32::Register first_reg = InputRegisterAt(op, 0);
4892 if (second.IsRegister()) {
4893 vixl32::Register second_reg = RegisterFrom(second);
4894 // ARM doesn't mask the shift count so we need to do it ourselves.
4895 __ And(out_reg, second_reg, kMaxIntShiftDistance);
4896 if (op->IsShl()) {
4897 __ Lsl(out_reg, first_reg, out_reg);
4898 } else if (op->IsShr()) {
4899 __ Asr(out_reg, first_reg, out_reg);
4900 } else {
4901 __ Lsr(out_reg, first_reg, out_reg);
4902 }
4903 } else {
Anton Kirilov644032c2016-12-06 17:51:43 +00004904 int32_t cst = Int32ConstantFrom(second);
Artem Serov02d37832016-10-25 15:25:33 +01004905 uint32_t shift_value = cst & kMaxIntShiftDistance;
4906 if (shift_value == 0) { // ARM does not support shifting with 0 immediate.
4907 __ Mov(out_reg, first_reg);
4908 } else if (op->IsShl()) {
4909 __ Lsl(out_reg, first_reg, shift_value);
4910 } else if (op->IsShr()) {
4911 __ Asr(out_reg, first_reg, shift_value);
4912 } else {
4913 __ Lsr(out_reg, first_reg, shift_value);
4914 }
4915 }
4916 break;
4917 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004918 case DataType::Type::kInt64: {
Artem Serov02d37832016-10-25 15:25:33 +01004919 vixl32::Register o_h = HighRegisterFrom(out);
4920 vixl32::Register o_l = LowRegisterFrom(out);
4921
4922 vixl32::Register high = HighRegisterFrom(first);
4923 vixl32::Register low = LowRegisterFrom(first);
4924
4925 if (second.IsRegister()) {
4926 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
4927
4928 vixl32::Register second_reg = RegisterFrom(second);
4929
4930 if (op->IsShl()) {
4931 __ And(o_l, second_reg, kMaxLongShiftDistance);
4932 // Shift the high part
4933 __ Lsl(o_h, high, o_l);
4934 // Shift the low part and `or` what overflew on the high part
Scott Wakelingb77051e2016-11-21 19:46:00 +00004935 __ Rsb(temp, o_l, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01004936 __ Lsr(temp, low, temp);
4937 __ Orr(o_h, o_h, temp);
4938 // If the shift is > 32 bits, override the high part
Scott Wakelingb77051e2016-11-21 19:46:00 +00004939 __ Subs(temp, o_l, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01004940 {
Artem Serov0fb37192016-12-06 18:13:40 +00004941 ExactAssemblyScope guard(GetVIXLAssembler(),
4942 2 * vixl32::kMaxInstructionSizeInBytes,
4943 CodeBufferCheckScope::kMaximumSize);
Artem Serov02d37832016-10-25 15:25:33 +01004944 __ it(pl);
4945 __ lsl(pl, o_h, low, temp);
4946 }
4947 // Shift the low part
4948 __ Lsl(o_l, low, o_l);
4949 } else if (op->IsShr()) {
4950 __ And(o_h, second_reg, kMaxLongShiftDistance);
4951 // Shift the low part
4952 __ Lsr(o_l, low, o_h);
4953 // Shift the high part and `or` what underflew on the low part
Scott Wakelingb77051e2016-11-21 19:46:00 +00004954 __ Rsb(temp, o_h, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01004955 __ Lsl(temp, high, temp);
4956 __ Orr(o_l, o_l, temp);
4957 // If the shift is > 32 bits, override the low part
Scott Wakelingb77051e2016-11-21 19:46:00 +00004958 __ Subs(temp, o_h, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01004959 {
Artem Serov0fb37192016-12-06 18:13:40 +00004960 ExactAssemblyScope guard(GetVIXLAssembler(),
4961 2 * vixl32::kMaxInstructionSizeInBytes,
4962 CodeBufferCheckScope::kMaximumSize);
Artem Serov02d37832016-10-25 15:25:33 +01004963 __ it(pl);
4964 __ asr(pl, o_l, high, temp);
4965 }
4966 // Shift the high part
4967 __ Asr(o_h, high, o_h);
4968 } else {
4969 __ And(o_h, second_reg, kMaxLongShiftDistance);
4970 // same as Shr except we use `Lsr`s and not `Asr`s
4971 __ Lsr(o_l, low, o_h);
Scott Wakelingb77051e2016-11-21 19:46:00 +00004972 __ Rsb(temp, o_h, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01004973 __ Lsl(temp, high, temp);
4974 __ Orr(o_l, o_l, temp);
Scott Wakelingb77051e2016-11-21 19:46:00 +00004975 __ Subs(temp, o_h, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01004976 {
Artem Serov0fb37192016-12-06 18:13:40 +00004977 ExactAssemblyScope guard(GetVIXLAssembler(),
4978 2 * vixl32::kMaxInstructionSizeInBytes,
4979 CodeBufferCheckScope::kMaximumSize);
Artem Serov02d37832016-10-25 15:25:33 +01004980 __ it(pl);
4981 __ lsr(pl, o_l, high, temp);
4982 }
4983 __ Lsr(o_h, high, o_h);
4984 }
4985 } else {
4986 // Register allocator doesn't create partial overlap.
4987 DCHECK(!o_l.Is(high));
4988 DCHECK(!o_h.Is(low));
Anton Kirilov644032c2016-12-06 17:51:43 +00004989 int32_t cst = Int32ConstantFrom(second);
Artem Serov02d37832016-10-25 15:25:33 +01004990 uint32_t shift_value = cst & kMaxLongShiftDistance;
4991 if (shift_value > 32) {
4992 if (op->IsShl()) {
4993 __ Lsl(o_h, low, shift_value - 32);
4994 __ Mov(o_l, 0);
4995 } else if (op->IsShr()) {
4996 __ Asr(o_l, high, shift_value - 32);
4997 __ Asr(o_h, high, 31);
4998 } else {
4999 __ Lsr(o_l, high, shift_value - 32);
5000 __ Mov(o_h, 0);
5001 }
5002 } else if (shift_value == 32) {
5003 if (op->IsShl()) {
5004 __ Mov(o_h, low);
5005 __ Mov(o_l, 0);
5006 } else if (op->IsShr()) {
5007 __ Mov(o_l, high);
5008 __ Asr(o_h, high, 31);
5009 } else {
5010 __ Mov(o_l, high);
5011 __ Mov(o_h, 0);
5012 }
5013 } else if (shift_value == 1) {
5014 if (op->IsShl()) {
5015 __ Lsls(o_l, low, 1);
5016 __ Adc(o_h, high, high);
5017 } else if (op->IsShr()) {
5018 __ Asrs(o_h, high, 1);
5019 __ Rrx(o_l, low);
5020 } else {
5021 __ Lsrs(o_h, high, 1);
5022 __ Rrx(o_l, low);
5023 }
5024 } else {
5025 DCHECK(2 <= shift_value && shift_value < 32) << shift_value;
5026 if (op->IsShl()) {
5027 __ Lsl(o_h, high, shift_value);
5028 __ Orr(o_h, o_h, Operand(low, ShiftType::LSR, 32 - shift_value));
5029 __ Lsl(o_l, low, shift_value);
5030 } else if (op->IsShr()) {
5031 __ Lsr(o_l, low, shift_value);
5032 __ Orr(o_l, o_l, Operand(high, ShiftType::LSL, 32 - shift_value));
5033 __ Asr(o_h, high, shift_value);
5034 } else {
5035 __ Lsr(o_l, low, shift_value);
5036 __ Orr(o_l, o_l, Operand(high, ShiftType::LSL, 32 - shift_value));
5037 __ Lsr(o_h, high, shift_value);
5038 }
5039 }
5040 }
5041 break;
5042 }
5043 default:
5044 LOG(FATAL) << "Unexpected operation type " << type;
5045 UNREACHABLE();
5046 }
5047}
5048
5049void LocationsBuilderARMVIXL::VisitShl(HShl* shl) {
5050 HandleShift(shl);
5051}
5052
5053void InstructionCodeGeneratorARMVIXL::VisitShl(HShl* shl) {
5054 HandleShift(shl);
5055}
5056
5057void LocationsBuilderARMVIXL::VisitShr(HShr* shr) {
5058 HandleShift(shr);
5059}
5060
5061void InstructionCodeGeneratorARMVIXL::VisitShr(HShr* shr) {
5062 HandleShift(shr);
5063}
5064
5065void LocationsBuilderARMVIXL::VisitUShr(HUShr* ushr) {
5066 HandleShift(ushr);
5067}
5068
5069void InstructionCodeGeneratorARMVIXL::VisitUShr(HUShr* ushr) {
5070 HandleShift(ushr);
5071}
5072
5073void LocationsBuilderARMVIXL::VisitNewInstance(HNewInstance* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005074 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5075 instruction, LocationSummary::kCallOnMainOnly);
Artem Serov02d37832016-10-25 15:25:33 +01005076 if (instruction->IsStringAlloc()) {
5077 locations->AddTemp(LocationFrom(kMethodRegister));
5078 } else {
5079 InvokeRuntimeCallingConventionARMVIXL calling_convention;
5080 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
Artem Serov02d37832016-10-25 15:25:33 +01005081 }
5082 locations->SetOut(LocationFrom(r0));
5083}
5084
5085void InstructionCodeGeneratorARMVIXL::VisitNewInstance(HNewInstance* instruction) {
5086 // Note: if heap poisoning is enabled, the entry point takes cares
5087 // of poisoning the reference.
5088 if (instruction->IsStringAlloc()) {
5089 // String is allocated through StringFactory. Call NewEmptyString entry point.
5090 vixl32::Register temp = RegisterFrom(instruction->GetLocations()->GetTemp(0));
5091 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize);
5092 GetAssembler()->LoadFromOffset(kLoadWord, temp, tr, QUICK_ENTRY_POINT(pNewEmptyString));
5093 GetAssembler()->LoadFromOffset(kLoadWord, lr, temp, code_offset.Int32Value());
Alexandre Rames374ddf32016-11-04 10:40:49 +00005094 // 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 +00005095 ExactAssemblyScope aas(GetVIXLAssembler(),
5096 vixl32::k16BitT32InstructionSizeInBytes,
5097 CodeBufferCheckScope::kExactSize);
Artem Serov02d37832016-10-25 15:25:33 +01005098 __ blx(lr);
5099 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
5100 } else {
5101 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00005102 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
Artem Serov02d37832016-10-25 15:25:33 +01005103 }
Roland Levillain5daa4952017-07-03 17:23:56 +01005104 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 10);
Artem Serov02d37832016-10-25 15:25:33 +01005105}
5106
5107void LocationsBuilderARMVIXL::VisitNewArray(HNewArray* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005108 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5109 instruction, LocationSummary::kCallOnMainOnly);
Artem Serov02d37832016-10-25 15:25:33 +01005110 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Artem Serov02d37832016-10-25 15:25:33 +01005111 locations->SetOut(LocationFrom(r0));
Nicolas Geoffray8c7c4f12017-01-26 10:13:11 +00005112 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5113 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
Artem Serov02d37832016-10-25 15:25:33 +01005114}
5115
5116void InstructionCodeGeneratorARMVIXL::VisitNewArray(HNewArray* instruction) {
Artem Serov02d37832016-10-25 15:25:33 +01005117 // Note: if heap poisoning is enabled, the entry point takes cares
5118 // of poisoning the reference.
Artem Serov7b3672e2017-02-03 17:30:34 +00005119 QuickEntrypointEnum entrypoint =
5120 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
5121 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005122 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Artem Serov7b3672e2017-02-03 17:30:34 +00005123 DCHECK(!codegen_->IsLeafMethod());
Roland Levillain5daa4952017-07-03 17:23:56 +01005124 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 11);
Artem Serov02d37832016-10-25 15:25:33 +01005125}
5126
5127void LocationsBuilderARMVIXL::VisitParameterValue(HParameterValue* instruction) {
5128 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005129 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Artem Serov02d37832016-10-25 15:25:33 +01005130 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
5131 if (location.IsStackSlot()) {
5132 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5133 } else if (location.IsDoubleStackSlot()) {
5134 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5135 }
5136 locations->SetOut(location);
5137}
5138
5139void InstructionCodeGeneratorARMVIXL::VisitParameterValue(
5140 HParameterValue* instruction ATTRIBUTE_UNUSED) {
5141 // Nothing to do, the parameter is already at its location.
5142}
5143
5144void LocationsBuilderARMVIXL::VisitCurrentMethod(HCurrentMethod* instruction) {
5145 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005146 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Artem Serov02d37832016-10-25 15:25:33 +01005147 locations->SetOut(LocationFrom(kMethodRegister));
5148}
5149
5150void InstructionCodeGeneratorARMVIXL::VisitCurrentMethod(
5151 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
5152 // Nothing to do, the method is already at its location.
5153}
5154
5155void LocationsBuilderARMVIXL::VisitNot(HNot* not_) {
5156 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005157 new (GetGraph()->GetAllocator()) LocationSummary(not_, LocationSummary::kNoCall);
Artem Serov02d37832016-10-25 15:25:33 +01005158 locations->SetInAt(0, Location::RequiresRegister());
5159 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5160}
5161
5162void InstructionCodeGeneratorARMVIXL::VisitNot(HNot* not_) {
5163 LocationSummary* locations = not_->GetLocations();
5164 Location out = locations->Out();
5165 Location in = locations->InAt(0);
5166 switch (not_->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005167 case DataType::Type::kInt32:
Artem Serov02d37832016-10-25 15:25:33 +01005168 __ Mvn(OutputRegister(not_), InputRegisterAt(not_, 0));
5169 break;
5170
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005171 case DataType::Type::kInt64:
Artem Serov02d37832016-10-25 15:25:33 +01005172 __ Mvn(LowRegisterFrom(out), LowRegisterFrom(in));
5173 __ Mvn(HighRegisterFrom(out), HighRegisterFrom(in));
5174 break;
5175
5176 default:
5177 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
5178 }
5179}
5180
Scott Wakelingc34dba72016-10-03 10:14:44 +01005181void LocationsBuilderARMVIXL::VisitBooleanNot(HBooleanNot* bool_not) {
5182 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005183 new (GetGraph()->GetAllocator()) LocationSummary(bool_not, LocationSummary::kNoCall);
Scott Wakelingc34dba72016-10-03 10:14:44 +01005184 locations->SetInAt(0, Location::RequiresRegister());
5185 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5186}
5187
5188void InstructionCodeGeneratorARMVIXL::VisitBooleanNot(HBooleanNot* bool_not) {
5189 __ Eor(OutputRegister(bool_not), InputRegister(bool_not), 1);
5190}
5191
Artem Serov02d37832016-10-25 15:25:33 +01005192void LocationsBuilderARMVIXL::VisitCompare(HCompare* compare) {
5193 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005194 new (GetGraph()->GetAllocator()) LocationSummary(compare, LocationSummary::kNoCall);
Artem Serov02d37832016-10-25 15:25:33 +01005195 switch (compare->InputAt(0)->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005196 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005197 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005198 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005199 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005200 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005201 case DataType::Type::kInt32:
5202 case DataType::Type::kInt64: {
Artem Serov02d37832016-10-25 15:25:33 +01005203 locations->SetInAt(0, Location::RequiresRegister());
5204 locations->SetInAt(1, Location::RequiresRegister());
5205 // Output overlaps because it is written before doing the low comparison.
5206 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
5207 break;
5208 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005209 case DataType::Type::kFloat32:
5210 case DataType::Type::kFloat64: {
Artem Serov02d37832016-10-25 15:25:33 +01005211 locations->SetInAt(0, Location::RequiresFpuRegister());
5212 locations->SetInAt(1, ArithmeticZeroOrFpuRegister(compare->InputAt(1)));
5213 locations->SetOut(Location::RequiresRegister());
5214 break;
5215 }
5216 default:
5217 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
5218 }
5219}
5220
5221void InstructionCodeGeneratorARMVIXL::VisitCompare(HCompare* compare) {
5222 LocationSummary* locations = compare->GetLocations();
5223 vixl32::Register out = OutputRegister(compare);
5224 Location left = locations->InAt(0);
5225 Location right = locations->InAt(1);
5226
5227 vixl32::Label less, greater, done;
Anton Kirilov6f644202017-02-27 18:29:45 +00005228 vixl32::Label* final_label = codegen_->GetFinalLabel(compare, &done);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005229 DataType::Type type = compare->InputAt(0)->GetType();
Vladimir Marko33bff252017-11-01 14:35:42 +00005230 vixl32::Condition less_cond = vixl32::Condition::None();
Artem Serov02d37832016-10-25 15:25:33 +01005231 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005232 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005233 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005234 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005235 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005236 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005237 case DataType::Type::kInt32: {
Artem Serov02d37832016-10-25 15:25:33 +01005238 // Emit move to `out` before the `Cmp`, as `Mov` might affect the status flags.
5239 __ Mov(out, 0);
5240 __ Cmp(RegisterFrom(left), RegisterFrom(right)); // Signed compare.
5241 less_cond = lt;
5242 break;
5243 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005244 case DataType::Type::kInt64: {
Artem Serov02d37832016-10-25 15:25:33 +01005245 __ Cmp(HighRegisterFrom(left), HighRegisterFrom(right)); // Signed compare.
Artem Serov517d9f62016-12-12 15:51:15 +00005246 __ B(lt, &less, /* far_target */ false);
5247 __ B(gt, &greater, /* far_target */ false);
Artem Serov02d37832016-10-25 15:25:33 +01005248 // Emit move to `out` before the last `Cmp`, as `Mov` might affect the status flags.
5249 __ Mov(out, 0);
5250 __ Cmp(LowRegisterFrom(left), LowRegisterFrom(right)); // Unsigned compare.
5251 less_cond = lo;
5252 break;
5253 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005254 case DataType::Type::kFloat32:
5255 case DataType::Type::kFloat64: {
Artem Serov02d37832016-10-25 15:25:33 +01005256 __ Mov(out, 0);
Donghui Bai426b49c2016-11-08 14:55:38 +08005257 GenerateVcmp(compare, codegen_);
Artem Serov02d37832016-10-25 15:25:33 +01005258 // To branch on the FP compare result we transfer FPSCR to APSR (encoded as PC in VMRS).
5259 __ Vmrs(RegisterOrAPSR_nzcv(kPcCode), FPSCR);
5260 less_cond = ARMFPCondition(kCondLT, compare->IsGtBias());
5261 break;
5262 }
5263 default:
5264 LOG(FATAL) << "Unexpected compare type " << type;
5265 UNREACHABLE();
5266 }
5267
Anton Kirilov6f644202017-02-27 18:29:45 +00005268 __ B(eq, final_label, /* far_target */ false);
Artem Serov517d9f62016-12-12 15:51:15 +00005269 __ B(less_cond, &less, /* far_target */ false);
Artem Serov02d37832016-10-25 15:25:33 +01005270
5271 __ Bind(&greater);
5272 __ Mov(out, 1);
Anton Kirilov6f644202017-02-27 18:29:45 +00005273 __ B(final_label);
Artem Serov02d37832016-10-25 15:25:33 +01005274
5275 __ Bind(&less);
5276 __ Mov(out, -1);
5277
Anton Kirilov6f644202017-02-27 18:29:45 +00005278 if (done.IsReferenced()) {
5279 __ Bind(&done);
5280 }
Artem Serov02d37832016-10-25 15:25:33 +01005281}
5282
5283void LocationsBuilderARMVIXL::VisitPhi(HPhi* instruction) {
5284 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005285 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Artem Serov02d37832016-10-25 15:25:33 +01005286 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
5287 locations->SetInAt(i, Location::Any());
5288 }
5289 locations->SetOut(Location::Any());
5290}
5291
5292void InstructionCodeGeneratorARMVIXL::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
5293 LOG(FATAL) << "Unreachable";
5294}
5295
5296void CodeGeneratorARMVIXL::GenerateMemoryBarrier(MemBarrierKind kind) {
5297 // TODO (ported from quick): revisit ARM barrier kinds.
5298 DmbOptions flavor = DmbOptions::ISH; // Quiet C++ warnings.
5299 switch (kind) {
5300 case MemBarrierKind::kAnyStore:
5301 case MemBarrierKind::kLoadAny:
5302 case MemBarrierKind::kAnyAny: {
5303 flavor = DmbOptions::ISH;
5304 break;
5305 }
5306 case MemBarrierKind::kStoreStore: {
5307 flavor = DmbOptions::ISHST;
5308 break;
5309 }
5310 default:
5311 LOG(FATAL) << "Unexpected memory barrier " << kind;
5312 }
5313 __ Dmb(flavor);
5314}
5315
5316void InstructionCodeGeneratorARMVIXL::GenerateWideAtomicLoad(vixl32::Register addr,
5317 uint32_t offset,
5318 vixl32::Register out_lo,
5319 vixl32::Register out_hi) {
5320 UseScratchRegisterScope temps(GetVIXLAssembler());
5321 if (offset != 0) {
5322 vixl32::Register temp = temps.Acquire();
5323 __ Add(temp, addr, offset);
5324 addr = temp;
5325 }
Scott Wakelingb77051e2016-11-21 19:46:00 +00005326 __ Ldrexd(out_lo, out_hi, MemOperand(addr));
Artem Serov02d37832016-10-25 15:25:33 +01005327}
5328
5329void InstructionCodeGeneratorARMVIXL::GenerateWideAtomicStore(vixl32::Register addr,
5330 uint32_t offset,
5331 vixl32::Register value_lo,
5332 vixl32::Register value_hi,
5333 vixl32::Register temp1,
5334 vixl32::Register temp2,
5335 HInstruction* instruction) {
5336 UseScratchRegisterScope temps(GetVIXLAssembler());
5337 vixl32::Label fail;
5338 if (offset != 0) {
5339 vixl32::Register temp = temps.Acquire();
5340 __ Add(temp, addr, offset);
5341 addr = temp;
5342 }
5343 __ Bind(&fail);
Alexandre Rames374ddf32016-11-04 10:40:49 +00005344 {
5345 // Ensure the pc position is recorded immediately after the `ldrexd` instruction.
Artem Serov0fb37192016-12-06 18:13:40 +00005346 ExactAssemblyScope aas(GetVIXLAssembler(),
5347 vixl32::kMaxInstructionSizeInBytes,
5348 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00005349 // We need a load followed by store. (The address used in a STREX instruction must
5350 // be the same as the address in the most recently executed LDREX instruction.)
5351 __ ldrexd(temp1, temp2, MemOperand(addr));
5352 codegen_->MaybeRecordImplicitNullCheck(instruction);
5353 }
Scott Wakelingb77051e2016-11-21 19:46:00 +00005354 __ Strexd(temp1, value_lo, value_hi, MemOperand(addr));
xueliang.zhongf51bc622016-11-04 09:23:32 +00005355 __ CompareAndBranchIfNonZero(temp1, &fail);
Artem Serov02d37832016-10-25 15:25:33 +01005356}
Artem Serov02109dd2016-09-23 17:17:54 +01005357
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005358void LocationsBuilderARMVIXL::HandleFieldSet(
5359 HInstruction* instruction, const FieldInfo& field_info) {
5360 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
5361
5362 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005363 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005364 locations->SetInAt(0, Location::RequiresRegister());
5365
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005366 DataType::Type field_type = field_info.GetFieldType();
5367 if (DataType::IsFloatingPointType(field_type)) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005368 locations->SetInAt(1, Location::RequiresFpuRegister());
5369 } else {
5370 locations->SetInAt(1, Location::RequiresRegister());
5371 }
5372
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005373 bool is_wide = field_type == DataType::Type::kInt64 || field_type == DataType::Type::kFloat64;
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005374 bool generate_volatile = field_info.IsVolatile()
5375 && is_wide
5376 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
5377 bool needs_write_barrier =
5378 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
5379 // Temporary registers for the write barrier.
5380 // TODO: consider renaming StoreNeedsWriteBarrier to StoreNeedsGCMark.
5381 if (needs_write_barrier) {
5382 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
5383 locations->AddTemp(Location::RequiresRegister());
5384 } else if (generate_volatile) {
5385 // ARM encoding have some additional constraints for ldrexd/strexd:
5386 // - registers need to be consecutive
5387 // - the first register should be even but not R14.
5388 // We don't test for ARM yet, and the assertion makes sure that we
5389 // revisit this if we ever enable ARM encoding.
5390 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
5391
5392 locations->AddTemp(Location::RequiresRegister());
5393 locations->AddTemp(Location::RequiresRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005394 if (field_type == DataType::Type::kFloat64) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005395 // For doubles we need two more registers to copy the value.
5396 locations->AddTemp(LocationFrom(r2));
5397 locations->AddTemp(LocationFrom(r3));
5398 }
5399 }
5400}
5401
5402void InstructionCodeGeneratorARMVIXL::HandleFieldSet(HInstruction* instruction,
5403 const FieldInfo& field_info,
5404 bool value_can_be_null) {
5405 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
5406
5407 LocationSummary* locations = instruction->GetLocations();
5408 vixl32::Register base = InputRegisterAt(instruction, 0);
5409 Location value = locations->InAt(1);
5410
5411 bool is_volatile = field_info.IsVolatile();
5412 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005413 DataType::Type field_type = field_info.GetFieldType();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005414 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
5415 bool needs_write_barrier =
5416 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
5417
5418 if (is_volatile) {
5419 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
5420 }
5421
5422 switch (field_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005423 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005424 case DataType::Type::kUint8:
5425 case DataType::Type::kInt8:
5426 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005427 case DataType::Type::kInt16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005428 case DataType::Type::kInt32: {
5429 StoreOperandType operand_type = GetStoreOperandType(field_type);
5430 GetAssembler()->StoreToOffset(operand_type, RegisterFrom(value), base, offset);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005431 break;
5432 }
5433
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005434 case DataType::Type::kReference: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005435 if (kPoisonHeapReferences && needs_write_barrier) {
5436 // Note that in the case where `value` is a null reference,
5437 // we do not enter this block, as a null reference does not
5438 // need poisoning.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005439 DCHECK_EQ(field_type, DataType::Type::kReference);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005440 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
5441 __ Mov(temp, RegisterFrom(value));
5442 GetAssembler()->PoisonHeapReference(temp);
5443 GetAssembler()->StoreToOffset(kStoreWord, temp, base, offset);
5444 } else {
5445 GetAssembler()->StoreToOffset(kStoreWord, RegisterFrom(value), base, offset);
5446 }
5447 break;
5448 }
5449
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005450 case DataType::Type::kInt64: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005451 if (is_volatile && !atomic_ldrd_strd) {
5452 GenerateWideAtomicStore(base,
5453 offset,
5454 LowRegisterFrom(value),
5455 HighRegisterFrom(value),
5456 RegisterFrom(locations->GetTemp(0)),
5457 RegisterFrom(locations->GetTemp(1)),
5458 instruction);
5459 } else {
5460 GetAssembler()->StoreToOffset(kStoreWordPair, LowRegisterFrom(value), base, offset);
5461 codegen_->MaybeRecordImplicitNullCheck(instruction);
5462 }
5463 break;
5464 }
5465
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005466 case DataType::Type::kFloat32: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005467 GetAssembler()->StoreSToOffset(SRegisterFrom(value), base, offset);
5468 break;
5469 }
5470
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005471 case DataType::Type::kFloat64: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01005472 vixl32::DRegister value_reg = DRegisterFrom(value);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005473 if (is_volatile && !atomic_ldrd_strd) {
5474 vixl32::Register value_reg_lo = RegisterFrom(locations->GetTemp(0));
5475 vixl32::Register value_reg_hi = RegisterFrom(locations->GetTemp(1));
5476
5477 __ Vmov(value_reg_lo, value_reg_hi, value_reg);
5478
5479 GenerateWideAtomicStore(base,
5480 offset,
5481 value_reg_lo,
5482 value_reg_hi,
5483 RegisterFrom(locations->GetTemp(2)),
5484 RegisterFrom(locations->GetTemp(3)),
5485 instruction);
5486 } else {
5487 GetAssembler()->StoreDToOffset(value_reg, base, offset);
5488 codegen_->MaybeRecordImplicitNullCheck(instruction);
5489 }
5490 break;
5491 }
5492
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005493 case DataType::Type::kVoid:
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005494 LOG(FATAL) << "Unreachable type " << field_type;
5495 UNREACHABLE();
5496 }
5497
5498 // Longs and doubles are handled in the switch.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005499 if (field_type != DataType::Type::kInt64 && field_type != DataType::Type::kFloat64) {
Alexandre Rames374ddf32016-11-04 10:40:49 +00005500 // TODO(VIXL): Here and for other calls to `MaybeRecordImplicitNullCheck` in this method, we
5501 // should use a scope and the assembler to emit the store instruction to guarantee that we
5502 // record the pc at the correct position. But the `Assembler` does not automatically handle
5503 // unencodable offsets. Practically, everything is fine because the helper and VIXL, at the time
5504 // of writing, do generate the store instruction last.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005505 codegen_->MaybeRecordImplicitNullCheck(instruction);
5506 }
5507
5508 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
5509 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
5510 vixl32::Register card = RegisterFrom(locations->GetTemp(1));
5511 codegen_->MarkGCCard(temp, card, base, RegisterFrom(value), value_can_be_null);
5512 }
5513
5514 if (is_volatile) {
5515 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
5516 }
5517}
5518
Artem Serov02d37832016-10-25 15:25:33 +01005519void LocationsBuilderARMVIXL::HandleFieldGet(HInstruction* instruction,
5520 const FieldInfo& field_info) {
5521 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
5522
5523 bool object_field_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005524 kEmitCompilerReadBarrier && (field_info.GetFieldType() == DataType::Type::kReference);
Artem Serov02d37832016-10-25 15:25:33 +01005525 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005526 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
5527 object_field_get_with_read_barrier
5528 ? LocationSummary::kCallOnSlowPath
5529 : LocationSummary::kNoCall);
Artem Serov02d37832016-10-25 15:25:33 +01005530 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
5531 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
5532 }
5533 locations->SetInAt(0, Location::RequiresRegister());
5534
5535 bool volatile_for_double = field_info.IsVolatile()
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005536 && (field_info.GetFieldType() == DataType::Type::kFloat64)
Artem Serov02d37832016-10-25 15:25:33 +01005537 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
5538 // The output overlaps in case of volatile long: we don't want the
5539 // code generated by GenerateWideAtomicLoad to overwrite the
5540 // object's location. Likewise, in the case of an object field get
5541 // with read barriers enabled, we do not want the load to overwrite
5542 // the object's location, as we need it to emit the read barrier.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005543 bool overlap =
5544 (field_info.IsVolatile() && (field_info.GetFieldType() == DataType::Type::kInt64)) ||
Artem Serov02d37832016-10-25 15:25:33 +01005545 object_field_get_with_read_barrier;
5546
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005547 if (DataType::IsFloatingPointType(instruction->GetType())) {
Artem Serov02d37832016-10-25 15:25:33 +01005548 locations->SetOut(Location::RequiresFpuRegister());
5549 } else {
5550 locations->SetOut(Location::RequiresRegister(),
5551 (overlap ? Location::kOutputOverlap : Location::kNoOutputOverlap));
5552 }
5553 if (volatile_for_double) {
5554 // ARM encoding have some additional constraints for ldrexd/strexd:
5555 // - registers need to be consecutive
5556 // - the first register should be even but not R14.
5557 // We don't test for ARM yet, and the assertion makes sure that we
5558 // revisit this if we ever enable ARM encoding.
5559 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
5560 locations->AddTemp(Location::RequiresRegister());
5561 locations->AddTemp(Location::RequiresRegister());
5562 } else if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
5563 // We need a temporary register for the read barrier marking slow
Artem Serovc5fcb442016-12-02 19:19:58 +00005564 // path in CodeGeneratorARMVIXL::GenerateFieldLoadWithBakerReadBarrier.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01005565 if (kBakerReadBarrierLinkTimeThunksEnableForFields &&
5566 !Runtime::Current()->UseJitCompilation()) {
5567 // If link-time thunks for the Baker read barrier are enabled, for AOT
5568 // loads we need a temporary only if the offset is too big.
5569 if (field_info.GetFieldOffset().Uint32Value() >= kReferenceLoadMinFarOffset) {
5570 locations->AddTemp(Location::RequiresRegister());
5571 }
5572 // And we always need the reserved entrypoint register.
5573 locations->AddTemp(Location::RegisterLocation(kBakerCcEntrypointRegister.GetCode()));
5574 } else {
5575 locations->AddTemp(Location::RequiresRegister());
5576 }
Artem Serov02d37832016-10-25 15:25:33 +01005577 }
5578}
5579
5580Location LocationsBuilderARMVIXL::ArithmeticZeroOrFpuRegister(HInstruction* input) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005581 DCHECK(DataType::IsFloatingPointType(input->GetType())) << input->GetType();
Artem Serov02d37832016-10-25 15:25:33 +01005582 if ((input->IsFloatConstant() && (input->AsFloatConstant()->IsArithmeticZero())) ||
5583 (input->IsDoubleConstant() && (input->AsDoubleConstant()->IsArithmeticZero()))) {
5584 return Location::ConstantLocation(input->AsConstant());
5585 } else {
5586 return Location::RequiresFpuRegister();
5587 }
5588}
5589
Artem Serov02109dd2016-09-23 17:17:54 +01005590Location LocationsBuilderARMVIXL::ArmEncodableConstantOrRegister(HInstruction* constant,
5591 Opcode opcode) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005592 DCHECK(!DataType::IsFloatingPointType(constant->GetType()));
Artem Serov02109dd2016-09-23 17:17:54 +01005593 if (constant->IsConstant() &&
5594 CanEncodeConstantAsImmediate(constant->AsConstant(), opcode)) {
5595 return Location::ConstantLocation(constant->AsConstant());
5596 }
5597 return Location::RequiresRegister();
5598}
5599
Vladimir Markof0a6a1d2018-01-08 14:23:56 +00005600static bool CanEncode32BitConstantAsImmediate(
5601 CodeGeneratorARMVIXL* codegen,
5602 uint32_t value,
5603 Opcode opcode,
5604 vixl32::FlagsUpdate flags_update = vixl32::FlagsUpdate::DontCare) {
5605 ArmVIXLAssembler* assembler = codegen->GetAssembler();
5606 if (assembler->ShifterOperandCanHold(opcode, value, flags_update)) {
Artem Serov02109dd2016-09-23 17:17:54 +01005607 return true;
5608 }
5609 Opcode neg_opcode = kNoOperand;
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00005610 uint32_t neg_value = 0;
Artem Serov02109dd2016-09-23 17:17:54 +01005611 switch (opcode) {
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00005612 case AND: neg_opcode = BIC; neg_value = ~value; break;
5613 case ORR: neg_opcode = ORN; neg_value = ~value; break;
5614 case ADD: neg_opcode = SUB; neg_value = -value; break;
5615 case ADC: neg_opcode = SBC; neg_value = ~value; break;
5616 case SUB: neg_opcode = ADD; neg_value = -value; break;
5617 case SBC: neg_opcode = ADC; neg_value = ~value; break;
5618 case MOV: neg_opcode = MVN; neg_value = ~value; break;
Artem Serov02109dd2016-09-23 17:17:54 +01005619 default:
5620 return false;
5621 }
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00005622
Vladimir Markof0a6a1d2018-01-08 14:23:56 +00005623 if (assembler->ShifterOperandCanHold(neg_opcode, neg_value, flags_update)) {
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00005624 return true;
5625 }
5626
5627 return opcode == AND && IsPowerOfTwo(value + 1);
Artem Serov02109dd2016-09-23 17:17:54 +01005628}
5629
Vladimir Markof0a6a1d2018-01-08 14:23:56 +00005630bool LocationsBuilderARMVIXL::CanEncodeConstantAsImmediate(HConstant* input_cst, Opcode opcode) {
5631 uint64_t value = static_cast<uint64_t>(Int64FromConstant(input_cst));
5632 if (DataType::Is64BitType(input_cst->GetType())) {
5633 Opcode high_opcode = opcode;
5634 vixl32::FlagsUpdate low_flags_update = vixl32::FlagsUpdate::DontCare;
5635 switch (opcode) {
5636 case SUB:
5637 // Flip the operation to an ADD.
5638 value = -value;
5639 opcode = ADD;
5640 FALLTHROUGH_INTENDED;
5641 case ADD:
5642 if (Low32Bits(value) == 0u) {
5643 return CanEncode32BitConstantAsImmediate(codegen_, High32Bits(value), opcode);
5644 }
5645 high_opcode = ADC;
5646 low_flags_update = vixl32::FlagsUpdate::SetFlags;
5647 break;
5648 default:
5649 break;
5650 }
5651 return CanEncode32BitConstantAsImmediate(codegen_, High32Bits(value), high_opcode) &&
5652 CanEncode32BitConstantAsImmediate(codegen_, Low32Bits(value), opcode, low_flags_update);
5653 } else {
5654 return CanEncode32BitConstantAsImmediate(codegen_, Low32Bits(value), opcode);
5655 }
5656}
5657
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005658void InstructionCodeGeneratorARMVIXL::HandleFieldGet(HInstruction* instruction,
5659 const FieldInfo& field_info) {
5660 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
5661
5662 LocationSummary* locations = instruction->GetLocations();
5663 vixl32::Register base = InputRegisterAt(instruction, 0);
5664 Location out = locations->Out();
5665 bool is_volatile = field_info.IsVolatile();
5666 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Vladimir Marko61b92282017-10-11 13:23:17 +01005667 DCHECK_EQ(DataType::Size(field_info.GetFieldType()), DataType::Size(instruction->GetType()));
5668 DataType::Type load_type = instruction->GetType();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005669 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
5670
Vladimir Marko61b92282017-10-11 13:23:17 +01005671 switch (load_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005672 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005673 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005674 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005675 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005676 case DataType::Type::kInt16:
5677 case DataType::Type::kInt32: {
Vladimir Marko61b92282017-10-11 13:23:17 +01005678 LoadOperandType operand_type = GetLoadOperandType(load_type);
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005679 GetAssembler()->LoadFromOffset(operand_type, RegisterFrom(out), base, offset);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005680 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005681 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005682
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005683 case DataType::Type::kReference: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005684 // /* HeapReference<Object> */ out = *(base + offset)
5685 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005686 Location temp_loc = locations->GetTemp(0);
5687 // Note that a potential implicit null check is handled in this
5688 // CodeGeneratorARMVIXL::GenerateFieldLoadWithBakerReadBarrier call.
5689 codegen_->GenerateFieldLoadWithBakerReadBarrier(
5690 instruction, out, base, offset, temp_loc, /* needs_null_check */ true);
5691 if (is_volatile) {
5692 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5693 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005694 } else {
5695 GetAssembler()->LoadFromOffset(kLoadWord, RegisterFrom(out), base, offset);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005696 codegen_->MaybeRecordImplicitNullCheck(instruction);
5697 if (is_volatile) {
5698 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5699 }
5700 // If read barriers are enabled, emit read barriers other than
5701 // Baker's using a slow path (and also unpoison the loaded
5702 // reference, if heap poisoning is enabled).
5703 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, locations->InAt(0), offset);
5704 }
5705 break;
5706 }
5707
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005708 case DataType::Type::kInt64:
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005709 if (is_volatile && !atomic_ldrd_strd) {
5710 GenerateWideAtomicLoad(base, offset, LowRegisterFrom(out), HighRegisterFrom(out));
5711 } else {
5712 GetAssembler()->LoadFromOffset(kLoadWordPair, LowRegisterFrom(out), base, offset);
5713 }
5714 break;
5715
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005716 case DataType::Type::kFloat32:
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005717 GetAssembler()->LoadSFromOffset(SRegisterFrom(out), base, offset);
5718 break;
5719
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005720 case DataType::Type::kFloat64: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01005721 vixl32::DRegister out_dreg = DRegisterFrom(out);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005722 if (is_volatile && !atomic_ldrd_strd) {
5723 vixl32::Register lo = RegisterFrom(locations->GetTemp(0));
5724 vixl32::Register hi = RegisterFrom(locations->GetTemp(1));
5725 GenerateWideAtomicLoad(base, offset, lo, hi);
5726 // TODO(VIXL): Do we need to be immediately after the ldrexd instruction? If so we need a
5727 // scope.
5728 codegen_->MaybeRecordImplicitNullCheck(instruction);
5729 __ Vmov(out_dreg, lo, hi);
5730 } else {
5731 GetAssembler()->LoadDFromOffset(out_dreg, base, offset);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005732 codegen_->MaybeRecordImplicitNullCheck(instruction);
5733 }
5734 break;
5735 }
5736
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005737 case DataType::Type::kVoid:
Vladimir Marko61b92282017-10-11 13:23:17 +01005738 LOG(FATAL) << "Unreachable type " << load_type;
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005739 UNREACHABLE();
5740 }
5741
Vladimir Marko61b92282017-10-11 13:23:17 +01005742 if (load_type == DataType::Type::kReference || load_type == DataType::Type::kFloat64) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005743 // Potential implicit null checks, in the case of reference or
5744 // double fields, are handled in the previous switch statement.
5745 } else {
5746 // Address cases other than reference and double that may require an implicit null check.
Alexandre Rames374ddf32016-11-04 10:40:49 +00005747 // TODO(VIXL): Here and for other calls to `MaybeRecordImplicitNullCheck` in this method, we
5748 // should use a scope and the assembler to emit the load instruction to guarantee that we
5749 // record the pc at the correct position. But the `Assembler` does not automatically handle
5750 // unencodable offsets. Practically, everything is fine because the helper and VIXL, at the time
5751 // of writing, do generate the store instruction last.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005752 codegen_->MaybeRecordImplicitNullCheck(instruction);
5753 }
5754
5755 if (is_volatile) {
Vladimir Marko61b92282017-10-11 13:23:17 +01005756 if (load_type == DataType::Type::kReference) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005757 // Memory barriers, in the case of references, are also handled
5758 // in the previous switch statement.
5759 } else {
5760 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5761 }
5762 }
5763}
5764
5765void LocationsBuilderARMVIXL::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
5766 HandleFieldSet(instruction, instruction->GetFieldInfo());
5767}
5768
5769void InstructionCodeGeneratorARMVIXL::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
5770 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
5771}
5772
5773void LocationsBuilderARMVIXL::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5774 HandleFieldGet(instruction, instruction->GetFieldInfo());
5775}
5776
5777void InstructionCodeGeneratorARMVIXL::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5778 HandleFieldGet(instruction, instruction->GetFieldInfo());
5779}
5780
5781void LocationsBuilderARMVIXL::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5782 HandleFieldGet(instruction, instruction->GetFieldInfo());
5783}
5784
5785void InstructionCodeGeneratorARMVIXL::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5786 HandleFieldGet(instruction, instruction->GetFieldInfo());
5787}
5788
Scott Wakelingc34dba72016-10-03 10:14:44 +01005789void LocationsBuilderARMVIXL::VisitStaticFieldSet(HStaticFieldSet* instruction) {
5790 HandleFieldSet(instruction, instruction->GetFieldInfo());
5791}
5792
5793void InstructionCodeGeneratorARMVIXL::VisitStaticFieldSet(HStaticFieldSet* instruction) {
5794 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
5795}
5796
Artem Serovcfbe9132016-10-14 15:58:56 +01005797void LocationsBuilderARMVIXL::VisitUnresolvedInstanceFieldGet(
5798 HUnresolvedInstanceFieldGet* instruction) {
5799 FieldAccessCallingConventionARMVIXL calling_convention;
5800 codegen_->CreateUnresolvedFieldLocationSummary(
5801 instruction, instruction->GetFieldType(), calling_convention);
5802}
5803
5804void InstructionCodeGeneratorARMVIXL::VisitUnresolvedInstanceFieldGet(
5805 HUnresolvedInstanceFieldGet* instruction) {
5806 FieldAccessCallingConventionARMVIXL calling_convention;
5807 codegen_->GenerateUnresolvedFieldAccess(instruction,
5808 instruction->GetFieldType(),
5809 instruction->GetFieldIndex(),
5810 instruction->GetDexPc(),
5811 calling_convention);
5812}
5813
5814void LocationsBuilderARMVIXL::VisitUnresolvedInstanceFieldSet(
5815 HUnresolvedInstanceFieldSet* instruction) {
5816 FieldAccessCallingConventionARMVIXL calling_convention;
5817 codegen_->CreateUnresolvedFieldLocationSummary(
5818 instruction, instruction->GetFieldType(), calling_convention);
5819}
5820
5821void InstructionCodeGeneratorARMVIXL::VisitUnresolvedInstanceFieldSet(
5822 HUnresolvedInstanceFieldSet* instruction) {
5823 FieldAccessCallingConventionARMVIXL calling_convention;
5824 codegen_->GenerateUnresolvedFieldAccess(instruction,
5825 instruction->GetFieldType(),
5826 instruction->GetFieldIndex(),
5827 instruction->GetDexPc(),
5828 calling_convention);
5829}
5830
5831void LocationsBuilderARMVIXL::VisitUnresolvedStaticFieldGet(
5832 HUnresolvedStaticFieldGet* instruction) {
5833 FieldAccessCallingConventionARMVIXL calling_convention;
5834 codegen_->CreateUnresolvedFieldLocationSummary(
5835 instruction, instruction->GetFieldType(), calling_convention);
5836}
5837
5838void InstructionCodeGeneratorARMVIXL::VisitUnresolvedStaticFieldGet(
5839 HUnresolvedStaticFieldGet* instruction) {
5840 FieldAccessCallingConventionARMVIXL calling_convention;
5841 codegen_->GenerateUnresolvedFieldAccess(instruction,
5842 instruction->GetFieldType(),
5843 instruction->GetFieldIndex(),
5844 instruction->GetDexPc(),
5845 calling_convention);
5846}
5847
5848void LocationsBuilderARMVIXL::VisitUnresolvedStaticFieldSet(
5849 HUnresolvedStaticFieldSet* instruction) {
5850 FieldAccessCallingConventionARMVIXL calling_convention;
5851 codegen_->CreateUnresolvedFieldLocationSummary(
5852 instruction, instruction->GetFieldType(), calling_convention);
5853}
5854
5855void InstructionCodeGeneratorARMVIXL::VisitUnresolvedStaticFieldSet(
5856 HUnresolvedStaticFieldSet* instruction) {
5857 FieldAccessCallingConventionARMVIXL calling_convention;
5858 codegen_->GenerateUnresolvedFieldAccess(instruction,
5859 instruction->GetFieldType(),
5860 instruction->GetFieldIndex(),
5861 instruction->GetDexPc(),
5862 calling_convention);
5863}
5864
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005865void LocationsBuilderARMVIXL::VisitNullCheck(HNullCheck* instruction) {
Artem Serov657022c2016-11-23 14:19:38 +00005866 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005867 locations->SetInAt(0, Location::RequiresRegister());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005868}
5869
5870void CodeGeneratorARMVIXL::GenerateImplicitNullCheck(HNullCheck* instruction) {
5871 if (CanMoveNullCheckToUser(instruction)) {
5872 return;
5873 }
5874
5875 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames374ddf32016-11-04 10:40:49 +00005876 // Ensure the pc position is recorded immediately after the `ldr` instruction.
Artem Serov0fb37192016-12-06 18:13:40 +00005877 ExactAssemblyScope aas(GetVIXLAssembler(),
5878 vixl32::kMaxInstructionSizeInBytes,
5879 CodeBufferCheckScope::kMaximumSize);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005880 __ ldr(temps.Acquire(), MemOperand(InputRegisterAt(instruction, 0)));
5881 RecordPcInfo(instruction, instruction->GetDexPc());
5882}
5883
5884void CodeGeneratorARMVIXL::GenerateExplicitNullCheck(HNullCheck* instruction) {
5885 NullCheckSlowPathARMVIXL* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01005886 new (GetScopedAllocator()) NullCheckSlowPathARMVIXL(instruction);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005887 AddSlowPath(slow_path);
xueliang.zhongf51bc622016-11-04 09:23:32 +00005888 __ CompareAndBranchIfZero(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005889}
5890
5891void InstructionCodeGeneratorARMVIXL::VisitNullCheck(HNullCheck* instruction) {
5892 codegen_->GenerateNullCheck(instruction);
5893}
5894
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005895void CodeGeneratorARMVIXL::LoadFromShiftedRegOffset(DataType::Type type,
Scott Wakelingc34dba72016-10-03 10:14:44 +01005896 Location out_loc,
5897 vixl32::Register base,
5898 vixl32::Register reg_index,
5899 vixl32::Condition cond) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005900 uint32_t shift_count = DataType::SizeShift(type);
Scott Wakelingc34dba72016-10-03 10:14:44 +01005901 MemOperand mem_address(base, reg_index, vixl32::LSL, shift_count);
5902
5903 switch (type) {
Vladimir Marko61b92282017-10-11 13:23:17 +01005904 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005905 case DataType::Type::kUint8:
Vladimir Marko61b92282017-10-11 13:23:17 +01005906 __ Ldrb(cond, RegisterFrom(out_loc), mem_address);
5907 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005908 case DataType::Type::kInt8:
Scott Wakelingc34dba72016-10-03 10:14:44 +01005909 __ Ldrsb(cond, RegisterFrom(out_loc), mem_address);
5910 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005911 case DataType::Type::kUint16:
Scott Wakelingc34dba72016-10-03 10:14:44 +01005912 __ Ldrh(cond, RegisterFrom(out_loc), mem_address);
5913 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005914 case DataType::Type::kInt16:
5915 __ Ldrsh(cond, RegisterFrom(out_loc), mem_address);
5916 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005917 case DataType::Type::kReference:
5918 case DataType::Type::kInt32:
Scott Wakelingc34dba72016-10-03 10:14:44 +01005919 __ Ldr(cond, RegisterFrom(out_loc), mem_address);
5920 break;
5921 // T32 doesn't support LoadFromShiftedRegOffset mem address mode for these types.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005922 case DataType::Type::kInt64:
5923 case DataType::Type::kFloat32:
5924 case DataType::Type::kFloat64:
Scott Wakelingc34dba72016-10-03 10:14:44 +01005925 default:
5926 LOG(FATAL) << "Unreachable type " << type;
5927 UNREACHABLE();
5928 }
5929}
5930
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005931void CodeGeneratorARMVIXL::StoreToShiftedRegOffset(DataType::Type type,
Scott Wakelingc34dba72016-10-03 10:14:44 +01005932 Location loc,
5933 vixl32::Register base,
5934 vixl32::Register reg_index,
5935 vixl32::Condition cond) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005936 uint32_t shift_count = DataType::SizeShift(type);
Scott Wakelingc34dba72016-10-03 10:14:44 +01005937 MemOperand mem_address(base, reg_index, vixl32::LSL, shift_count);
5938
5939 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005940 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005941 case DataType::Type::kUint8:
5942 case DataType::Type::kInt8:
Scott Wakelingc34dba72016-10-03 10:14:44 +01005943 __ Strb(cond, RegisterFrom(loc), mem_address);
5944 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005945 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005946 case DataType::Type::kInt16:
Scott Wakelingc34dba72016-10-03 10:14:44 +01005947 __ Strh(cond, RegisterFrom(loc), mem_address);
5948 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005949 case DataType::Type::kReference:
5950 case DataType::Type::kInt32:
Scott Wakelingc34dba72016-10-03 10:14:44 +01005951 __ Str(cond, RegisterFrom(loc), mem_address);
5952 break;
5953 // T32 doesn't support StoreToShiftedRegOffset mem address mode for these types.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005954 case DataType::Type::kInt64:
5955 case DataType::Type::kFloat32:
5956 case DataType::Type::kFloat64:
Scott Wakelingc34dba72016-10-03 10:14:44 +01005957 default:
5958 LOG(FATAL) << "Unreachable type " << type;
5959 UNREACHABLE();
5960 }
5961}
5962
5963void LocationsBuilderARMVIXL::VisitArrayGet(HArrayGet* instruction) {
5964 bool object_array_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005965 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Scott Wakelingc34dba72016-10-03 10:14:44 +01005966 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005967 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
5968 object_array_get_with_read_barrier
5969 ? LocationSummary::kCallOnSlowPath
5970 : LocationSummary::kNoCall);
Scott Wakelingc34dba72016-10-03 10:14:44 +01005971 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005972 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Scott Wakelingc34dba72016-10-03 10:14:44 +01005973 }
5974 locations->SetInAt(0, Location::RequiresRegister());
5975 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005976 if (DataType::IsFloatingPointType(instruction->GetType())) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01005977 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5978 } else {
5979 // The output overlaps in the case of an object array get with
5980 // read barriers enabled: we do not want the move to overwrite the
5981 // array's location, as we need it to emit the read barrier.
5982 locations->SetOut(
5983 Location::RequiresRegister(),
5984 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
5985 }
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01005986 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
5987 // We need a temporary register for the read barrier marking slow
5988 // path in CodeGeneratorARMVIXL::GenerateArrayLoadWithBakerReadBarrier.
5989 if (kBakerReadBarrierLinkTimeThunksEnableForFields &&
5990 !Runtime::Current()->UseJitCompilation() &&
5991 instruction->GetIndex()->IsConstant()) {
5992 // Array loads with constant index are treated as field loads.
5993 // If link-time thunks for the Baker read barrier are enabled, for AOT
5994 // constant index loads we need a temporary only if the offset is too big.
5995 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
5996 uint32_t index = instruction->GetIndex()->AsIntConstant()->GetValue();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005997 offset += index << DataType::SizeShift(DataType::Type::kReference);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01005998 if (offset >= kReferenceLoadMinFarOffset) {
5999 locations->AddTemp(Location::RequiresRegister());
6000 }
6001 // And we always need the reserved entrypoint register.
6002 locations->AddTemp(Location::RegisterLocation(kBakerCcEntrypointRegister.GetCode()));
6003 } else if (kBakerReadBarrierLinkTimeThunksEnableForArrays &&
6004 !Runtime::Current()->UseJitCompilation() &&
6005 !instruction->GetIndex()->IsConstant()) {
6006 // We need a non-scratch temporary for the array data pointer.
6007 locations->AddTemp(Location::RequiresRegister());
6008 // And we always need the reserved entrypoint register.
6009 locations->AddTemp(Location::RegisterLocation(kBakerCcEntrypointRegister.GetCode()));
6010 } else {
6011 locations->AddTemp(Location::RequiresRegister());
6012 }
6013 } else if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
6014 // Also need a temporary for String compression feature.
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006015 locations->AddTemp(Location::RequiresRegister());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006016 }
6017}
6018
6019void InstructionCodeGeneratorARMVIXL::VisitArrayGet(HArrayGet* instruction) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006020 LocationSummary* locations = instruction->GetLocations();
6021 Location obj_loc = locations->InAt(0);
6022 vixl32::Register obj = InputRegisterAt(instruction, 0);
6023 Location index = locations->InAt(1);
6024 Location out_loc = locations->Out();
6025 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006026 DataType::Type type = instruction->GetType();
Scott Wakelingc34dba72016-10-03 10:14:44 +01006027 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
6028 instruction->IsStringCharAt();
6029 HInstruction* array_instr = instruction->GetArray();
6030 bool has_intermediate_address = array_instr->IsIntermediateAddress();
Scott Wakelingc34dba72016-10-03 10:14:44 +01006031
6032 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006033 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006034 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006035 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006036 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006037 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006038 case DataType::Type::kInt32: {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006039 vixl32::Register length;
6040 if (maybe_compressed_char_at) {
6041 length = RegisterFrom(locations->GetTemp(0));
6042 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
6043 GetAssembler()->LoadFromOffset(kLoadWord, length, obj, count_offset);
6044 codegen_->MaybeRecordImplicitNullCheck(instruction);
6045 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006046 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006047 int32_t const_index = Int32ConstantFrom(index);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006048 if (maybe_compressed_char_at) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006049 vixl32::Label uncompressed_load, done;
Anton Kirilov6f644202017-02-27 18:29:45 +00006050 vixl32::Label* final_label = codegen_->GetFinalLabel(instruction, &done);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006051 __ Lsrs(length, length, 1u); // LSRS has a 16-bit encoding, TST (immediate) does not.
6052 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
6053 "Expecting 0=compressed, 1=uncompressed");
Artem Serov517d9f62016-12-12 15:51:15 +00006054 __ B(cs, &uncompressed_load, /* far_target */ false);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006055 GetAssembler()->LoadFromOffset(kLoadUnsignedByte,
6056 RegisterFrom(out_loc),
6057 obj,
6058 data_offset + const_index);
Anton Kirilov6f644202017-02-27 18:29:45 +00006059 __ B(final_label);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006060 __ Bind(&uncompressed_load);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006061 GetAssembler()->LoadFromOffset(GetLoadOperandType(DataType::Type::kUint16),
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006062 RegisterFrom(out_loc),
6063 obj,
6064 data_offset + (const_index << 1));
Anton Kirilov6f644202017-02-27 18:29:45 +00006065 if (done.IsReferenced()) {
6066 __ Bind(&done);
6067 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006068 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006069 uint32_t full_offset = data_offset + (const_index << DataType::SizeShift(type));
Scott Wakelingc34dba72016-10-03 10:14:44 +01006070
6071 LoadOperandType load_type = GetLoadOperandType(type);
6072 GetAssembler()->LoadFromOffset(load_type, RegisterFrom(out_loc), obj, full_offset);
6073 }
6074 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006075 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006076 vixl32::Register temp = temps.Acquire();
6077
6078 if (has_intermediate_address) {
Artem Serov2bbc9532016-10-21 11:51:50 +01006079 // We do not need to compute the intermediate address from the array: the
6080 // input instruction has done it already. See the comment in
6081 // `TryExtractArrayAccessAddress()`.
6082 if (kIsDebugBuild) {
6083 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
Anton Kirilov644032c2016-12-06 17:51:43 +00006084 DCHECK_EQ(Uint64ConstantFrom(tmp->GetOffset()), data_offset);
Artem Serov2bbc9532016-10-21 11:51:50 +01006085 }
6086 temp = obj;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006087 } else {
6088 __ Add(temp, obj, data_offset);
6089 }
6090 if (maybe_compressed_char_at) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006091 vixl32::Label uncompressed_load, done;
Anton Kirilov6f644202017-02-27 18:29:45 +00006092 vixl32::Label* final_label = codegen_->GetFinalLabel(instruction, &done);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006093 __ Lsrs(length, length, 1u); // LSRS has a 16-bit encoding, TST (immediate) does not.
6094 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
6095 "Expecting 0=compressed, 1=uncompressed");
Artem Serov517d9f62016-12-12 15:51:15 +00006096 __ B(cs, &uncompressed_load, /* far_target */ false);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006097 __ Ldrb(RegisterFrom(out_loc), MemOperand(temp, RegisterFrom(index), vixl32::LSL, 0));
Anton Kirilov6f644202017-02-27 18:29:45 +00006098 __ B(final_label);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006099 __ Bind(&uncompressed_load);
6100 __ Ldrh(RegisterFrom(out_loc), MemOperand(temp, RegisterFrom(index), vixl32::LSL, 1));
Anton Kirilov6f644202017-02-27 18:29:45 +00006101 if (done.IsReferenced()) {
6102 __ Bind(&done);
6103 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006104 } else {
6105 codegen_->LoadFromShiftedRegOffset(type, out_loc, temp, RegisterFrom(index));
6106 }
6107 }
6108 break;
6109 }
6110
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006111 case DataType::Type::kReference: {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006112 // The read barrier instrumentation of object ArrayGet
6113 // instructions does not support the HIntermediateAddress
6114 // instruction.
6115 DCHECK(!(has_intermediate_address && kEmitCompilerReadBarrier));
6116
Scott Wakelingc34dba72016-10-03 10:14:44 +01006117 static_assert(
6118 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6119 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
6120 // /* HeapReference<Object> */ out =
6121 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
6122 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006123 Location temp = locations->GetTemp(0);
6124 // Note that a potential implicit null check is handled in this
6125 // CodeGeneratorARMVIXL::GenerateArrayLoadWithBakerReadBarrier call.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01006126 DCHECK(!instruction->CanDoImplicitNullCheckOn(instruction->InputAt(0)));
6127 if (index.IsConstant()) {
6128 // Array load with a constant index can be treated as a field load.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006129 data_offset += Int32ConstantFrom(index) << DataType::SizeShift(type);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01006130 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6131 out_loc,
6132 obj,
6133 data_offset,
6134 locations->GetTemp(0),
6135 /* needs_null_check */ false);
6136 } else {
6137 codegen_->GenerateArrayLoadWithBakerReadBarrier(
6138 instruction, out_loc, obj, data_offset, index, temp, /* needs_null_check */ false);
6139 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006140 } else {
6141 vixl32::Register out = OutputRegister(instruction);
6142 if (index.IsConstant()) {
6143 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00006144 (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006145 GetAssembler()->LoadFromOffset(kLoadWord, out, obj, offset);
Alexandre Rames374ddf32016-11-04 10:40:49 +00006146 // TODO(VIXL): Here and for other calls to `MaybeRecordImplicitNullCheck` in this method,
6147 // we should use a scope and the assembler to emit the load instruction to guarantee that
6148 // we record the pc at the correct position. But the `Assembler` does not automatically
6149 // handle unencodable offsets. Practically, everything is fine because the helper and
6150 // VIXL, at the time of writing, do generate the store instruction last.
Scott Wakelingc34dba72016-10-03 10:14:44 +01006151 codegen_->MaybeRecordImplicitNullCheck(instruction);
6152 // If read barriers are enabled, emit read barriers other than
6153 // Baker's using a slow path (and also unpoison the loaded
6154 // reference, if heap poisoning is enabled).
6155 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
6156 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006157 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006158 vixl32::Register temp = temps.Acquire();
6159
6160 if (has_intermediate_address) {
Artem Serov2bbc9532016-10-21 11:51:50 +01006161 // We do not need to compute the intermediate address from the array: the
6162 // input instruction has done it already. See the comment in
6163 // `TryExtractArrayAccessAddress()`.
6164 if (kIsDebugBuild) {
6165 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
Anton Kirilov644032c2016-12-06 17:51:43 +00006166 DCHECK_EQ(Uint64ConstantFrom(tmp->GetOffset()), data_offset);
Artem Serov2bbc9532016-10-21 11:51:50 +01006167 }
6168 temp = obj;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006169 } else {
6170 __ Add(temp, obj, data_offset);
6171 }
6172 codegen_->LoadFromShiftedRegOffset(type, out_loc, temp, RegisterFrom(index));
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006173 temps.Close();
Alexandre Rames374ddf32016-11-04 10:40:49 +00006174 // TODO(VIXL): Use a scope to ensure that we record the pc position immediately after the
6175 // load instruction. Practically, everything is fine because the helper and VIXL, at the
6176 // time of writing, do generate the store instruction last.
Scott Wakelingc34dba72016-10-03 10:14:44 +01006177 codegen_->MaybeRecordImplicitNullCheck(instruction);
6178 // If read barriers are enabled, emit read barriers other than
6179 // Baker's using a slow path (and also unpoison the loaded
6180 // reference, if heap poisoning is enabled).
6181 codegen_->MaybeGenerateReadBarrierSlow(
6182 instruction, out_loc, out_loc, obj_loc, data_offset, index);
6183 }
6184 }
6185 break;
6186 }
6187
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006188 case DataType::Type::kInt64: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006189 if (index.IsConstant()) {
6190 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00006191 (Int32ConstantFrom(index) << TIMES_8) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006192 GetAssembler()->LoadFromOffset(kLoadWordPair, LowRegisterFrom(out_loc), obj, offset);
6193 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006194 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006195 vixl32::Register temp = temps.Acquire();
6196 __ Add(temp, obj, Operand(RegisterFrom(index), vixl32::LSL, TIMES_8));
6197 GetAssembler()->LoadFromOffset(kLoadWordPair, LowRegisterFrom(out_loc), temp, data_offset);
6198 }
6199 break;
6200 }
6201
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006202 case DataType::Type::kFloat32: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006203 vixl32::SRegister out = SRegisterFrom(out_loc);
6204 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006205 size_t offset = (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006206 GetAssembler()->LoadSFromOffset(out, obj, offset);
6207 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006208 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006209 vixl32::Register temp = temps.Acquire();
6210 __ Add(temp, obj, Operand(RegisterFrom(index), vixl32::LSL, TIMES_4));
6211 GetAssembler()->LoadSFromOffset(out, temp, data_offset);
6212 }
6213 break;
6214 }
6215
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006216 case DataType::Type::kFloat64: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006217 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006218 size_t offset = (Int32ConstantFrom(index) << TIMES_8) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006219 GetAssembler()->LoadDFromOffset(DRegisterFrom(out_loc), obj, offset);
6220 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006221 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006222 vixl32::Register temp = temps.Acquire();
6223 __ Add(temp, obj, Operand(RegisterFrom(index), vixl32::LSL, TIMES_8));
6224 GetAssembler()->LoadDFromOffset(DRegisterFrom(out_loc), temp, data_offset);
6225 }
6226 break;
6227 }
6228
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006229 case DataType::Type::kVoid:
Scott Wakelingc34dba72016-10-03 10:14:44 +01006230 LOG(FATAL) << "Unreachable type " << type;
6231 UNREACHABLE();
6232 }
6233
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006234 if (type == DataType::Type::kReference) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006235 // Potential implicit null checks, in the case of reference
6236 // arrays, are handled in the previous switch statement.
6237 } else if (!maybe_compressed_char_at) {
Alexandre Rames374ddf32016-11-04 10:40:49 +00006238 // TODO(VIXL): Use a scope to ensure we record the pc info immediately after
6239 // the preceding load instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01006240 codegen_->MaybeRecordImplicitNullCheck(instruction);
6241 }
6242}
6243
6244void LocationsBuilderARMVIXL::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006245 DataType::Type value_type = instruction->GetComponentType();
Scott Wakelingc34dba72016-10-03 10:14:44 +01006246
6247 bool needs_write_barrier =
6248 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
6249 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
6250
Vladimir Markoca6fff82017-10-03 14:49:14 +01006251 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Scott Wakelingc34dba72016-10-03 10:14:44 +01006252 instruction,
6253 may_need_runtime_call_for_type_check ?
6254 LocationSummary::kCallOnSlowPath :
6255 LocationSummary::kNoCall);
6256
6257 locations->SetInAt(0, Location::RequiresRegister());
6258 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006259 if (DataType::IsFloatingPointType(value_type)) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006260 locations->SetInAt(2, Location::RequiresFpuRegister());
6261 } else {
6262 locations->SetInAt(2, Location::RequiresRegister());
6263 }
6264 if (needs_write_barrier) {
6265 // Temporary registers for the write barrier.
6266 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
6267 locations->AddTemp(Location::RequiresRegister());
6268 }
6269}
6270
6271void InstructionCodeGeneratorARMVIXL::VisitArraySet(HArraySet* instruction) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006272 LocationSummary* locations = instruction->GetLocations();
6273 vixl32::Register array = InputRegisterAt(instruction, 0);
6274 Location index = locations->InAt(1);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006275 DataType::Type value_type = instruction->GetComponentType();
Scott Wakelingc34dba72016-10-03 10:14:44 +01006276 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
6277 bool needs_write_barrier =
6278 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
6279 uint32_t data_offset =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006280 mirror::Array::DataOffset(DataType::Size(value_type)).Uint32Value();
Scott Wakelingc34dba72016-10-03 10:14:44 +01006281 Location value_loc = locations->InAt(2);
6282 HInstruction* array_instr = instruction->GetArray();
6283 bool has_intermediate_address = array_instr->IsIntermediateAddress();
Scott Wakelingc34dba72016-10-03 10:14:44 +01006284
6285 switch (value_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006286 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006287 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006288 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006289 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006290 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006291 case DataType::Type::kInt32: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006292 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006293 int32_t const_index = Int32ConstantFrom(index);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006294 uint32_t full_offset =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006295 data_offset + (const_index << DataType::SizeShift(value_type));
Scott Wakelingc34dba72016-10-03 10:14:44 +01006296 StoreOperandType store_type = GetStoreOperandType(value_type);
6297 GetAssembler()->StoreToOffset(store_type, RegisterFrom(value_loc), array, full_offset);
6298 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006299 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006300 vixl32::Register temp = temps.Acquire();
6301
6302 if (has_intermediate_address) {
Artem Serov2bbc9532016-10-21 11:51:50 +01006303 // We do not need to compute the intermediate address from the array: the
6304 // input instruction has done it already. See the comment in
6305 // `TryExtractArrayAccessAddress()`.
6306 if (kIsDebugBuild) {
6307 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
Anton Kirilov644032c2016-12-06 17:51:43 +00006308 DCHECK_EQ(Uint64ConstantFrom(tmp->GetOffset()), data_offset);
Artem Serov2bbc9532016-10-21 11:51:50 +01006309 }
6310 temp = array;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006311 } else {
6312 __ Add(temp, array, data_offset);
6313 }
6314 codegen_->StoreToShiftedRegOffset(value_type, value_loc, temp, RegisterFrom(index));
6315 }
6316 break;
6317 }
6318
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006319 case DataType::Type::kReference: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006320 vixl32::Register value = RegisterFrom(value_loc);
6321 // TryExtractArrayAccessAddress optimization is never applied for non-primitive ArraySet.
6322 // See the comment in instruction_simplifier_shared.cc.
6323 DCHECK(!has_intermediate_address);
6324
6325 if (instruction->InputAt(2)->IsNullConstant()) {
6326 // Just setting null.
6327 if (index.IsConstant()) {
6328 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00006329 (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006330 GetAssembler()->StoreToOffset(kStoreWord, value, array, offset);
6331 } else {
6332 DCHECK(index.IsRegister()) << index;
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006333 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006334 vixl32::Register temp = temps.Acquire();
6335 __ Add(temp, array, data_offset);
6336 codegen_->StoreToShiftedRegOffset(value_type, value_loc, temp, RegisterFrom(index));
6337 }
Alexandre Rames374ddf32016-11-04 10:40:49 +00006338 // TODO(VIXL): Use a scope to ensure we record the pc info immediately after the preceding
6339 // store instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01006340 codegen_->MaybeRecordImplicitNullCheck(instruction);
6341 DCHECK(!needs_write_barrier);
6342 DCHECK(!may_need_runtime_call_for_type_check);
6343 break;
6344 }
6345
6346 DCHECK(needs_write_barrier);
6347 Location temp1_loc = locations->GetTemp(0);
6348 vixl32::Register temp1 = RegisterFrom(temp1_loc);
6349 Location temp2_loc = locations->GetTemp(1);
6350 vixl32::Register temp2 = RegisterFrom(temp2_loc);
6351 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6352 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6353 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6354 vixl32::Label done;
Anton Kirilov6f644202017-02-27 18:29:45 +00006355 vixl32::Label* final_label = codegen_->GetFinalLabel(instruction, &done);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006356 SlowPathCodeARMVIXL* slow_path = nullptr;
6357
6358 if (may_need_runtime_call_for_type_check) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01006359 slow_path = new (codegen_->GetScopedAllocator()) ArraySetSlowPathARMVIXL(instruction);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006360 codegen_->AddSlowPath(slow_path);
6361 if (instruction->GetValueCanBeNull()) {
6362 vixl32::Label non_zero;
xueliang.zhongf51bc622016-11-04 09:23:32 +00006363 __ CompareAndBranchIfNonZero(value, &non_zero);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006364 if (index.IsConstant()) {
6365 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00006366 (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006367 GetAssembler()->StoreToOffset(kStoreWord, value, array, offset);
6368 } else {
6369 DCHECK(index.IsRegister()) << index;
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006370 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006371 vixl32::Register temp = temps.Acquire();
6372 __ Add(temp, array, data_offset);
6373 codegen_->StoreToShiftedRegOffset(value_type, value_loc, temp, RegisterFrom(index));
6374 }
Alexandre Rames374ddf32016-11-04 10:40:49 +00006375 // TODO(VIXL): Use a scope to ensure we record the pc info immediately after the preceding
6376 // store instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01006377 codegen_->MaybeRecordImplicitNullCheck(instruction);
Anton Kirilov6f644202017-02-27 18:29:45 +00006378 __ B(final_label);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006379 __ Bind(&non_zero);
6380 }
6381
6382 // Note that when read barriers are enabled, the type checks
6383 // are performed without read barriers. This is fine, even in
6384 // the case where a class object is in the from-space after
6385 // the flip, as a comparison involving such a type would not
6386 // produce a false positive; it may of course produce a false
6387 // negative, in which case we would take the ArraySet slow
6388 // path.
6389
Alexandre Rames374ddf32016-11-04 10:40:49 +00006390 {
6391 // Ensure we record the pc position immediately after the `ldr` instruction.
Artem Serov0fb37192016-12-06 18:13:40 +00006392 ExactAssemblyScope aas(GetVIXLAssembler(),
6393 vixl32::kMaxInstructionSizeInBytes,
6394 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00006395 // /* HeapReference<Class> */ temp1 = array->klass_
6396 __ ldr(temp1, MemOperand(array, class_offset));
6397 codegen_->MaybeRecordImplicitNullCheck(instruction);
6398 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006399 GetAssembler()->MaybeUnpoisonHeapReference(temp1);
6400
6401 // /* HeapReference<Class> */ temp1 = temp1->component_type_
6402 GetAssembler()->LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
6403 // /* HeapReference<Class> */ temp2 = value->klass_
6404 GetAssembler()->LoadFromOffset(kLoadWord, temp2, value, class_offset);
6405 // If heap poisoning is enabled, no need to unpoison `temp1`
6406 // nor `temp2`, as we are comparing two poisoned references.
6407 __ Cmp(temp1, temp2);
6408
6409 if (instruction->StaticTypeOfArrayIsObjectArray()) {
6410 vixl32::Label do_put;
Artem Serov517d9f62016-12-12 15:51:15 +00006411 __ B(eq, &do_put, /* far_target */ false);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006412 // If heap poisoning is enabled, the `temp1` reference has
6413 // not been unpoisoned yet; unpoison it now.
6414 GetAssembler()->MaybeUnpoisonHeapReference(temp1);
6415
6416 // /* HeapReference<Class> */ temp1 = temp1->super_class_
6417 GetAssembler()->LoadFromOffset(kLoadWord, temp1, temp1, super_offset);
6418 // If heap poisoning is enabled, no need to unpoison
6419 // `temp1`, as we are comparing against null below.
xueliang.zhongf51bc622016-11-04 09:23:32 +00006420 __ CompareAndBranchIfNonZero(temp1, slow_path->GetEntryLabel());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006421 __ Bind(&do_put);
6422 } else {
6423 __ B(ne, slow_path->GetEntryLabel());
6424 }
6425 }
6426
6427 vixl32::Register source = value;
6428 if (kPoisonHeapReferences) {
6429 // Note that in the case where `value` is a null reference,
6430 // we do not enter this block, as a null reference does not
6431 // need poisoning.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006432 DCHECK_EQ(value_type, DataType::Type::kReference);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006433 __ Mov(temp1, value);
6434 GetAssembler()->PoisonHeapReference(temp1);
6435 source = temp1;
6436 }
6437
6438 if (index.IsConstant()) {
6439 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00006440 (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006441 GetAssembler()->StoreToOffset(kStoreWord, source, array, offset);
6442 } else {
6443 DCHECK(index.IsRegister()) << index;
6444
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006445 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006446 vixl32::Register temp = temps.Acquire();
6447 __ Add(temp, array, data_offset);
6448 codegen_->StoreToShiftedRegOffset(value_type,
6449 LocationFrom(source),
6450 temp,
6451 RegisterFrom(index));
6452 }
6453
6454 if (!may_need_runtime_call_for_type_check) {
Alexandre Rames374ddf32016-11-04 10:40:49 +00006455 // TODO(VIXL): Ensure we record the pc position immediately after the preceding store
6456 // instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01006457 codegen_->MaybeRecordImplicitNullCheck(instruction);
6458 }
6459
6460 codegen_->MarkGCCard(temp1, temp2, array, value, instruction->GetValueCanBeNull());
6461
6462 if (done.IsReferenced()) {
6463 __ Bind(&done);
6464 }
6465
6466 if (slow_path != nullptr) {
6467 __ Bind(slow_path->GetExitLabel());
6468 }
6469
6470 break;
6471 }
6472
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006473 case DataType::Type::kInt64: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006474 Location value = locations->InAt(2);
6475 if (index.IsConstant()) {
6476 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00006477 (Int32ConstantFrom(index) << TIMES_8) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006478 GetAssembler()->StoreToOffset(kStoreWordPair, LowRegisterFrom(value), array, offset);
6479 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006480 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006481 vixl32::Register temp = temps.Acquire();
6482 __ Add(temp, array, Operand(RegisterFrom(index), vixl32::LSL, TIMES_8));
6483 GetAssembler()->StoreToOffset(kStoreWordPair, LowRegisterFrom(value), temp, data_offset);
6484 }
6485 break;
6486 }
6487
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006488 case DataType::Type::kFloat32: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006489 Location value = locations->InAt(2);
6490 DCHECK(value.IsFpuRegister());
6491 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006492 size_t offset = (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006493 GetAssembler()->StoreSToOffset(SRegisterFrom(value), array, offset);
6494 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006495 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006496 vixl32::Register temp = temps.Acquire();
6497 __ Add(temp, array, Operand(RegisterFrom(index), vixl32::LSL, TIMES_4));
6498 GetAssembler()->StoreSToOffset(SRegisterFrom(value), temp, data_offset);
6499 }
6500 break;
6501 }
6502
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006503 case DataType::Type::kFloat64: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006504 Location value = locations->InAt(2);
6505 DCHECK(value.IsFpuRegisterPair());
6506 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006507 size_t offset = (Int32ConstantFrom(index) << TIMES_8) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006508 GetAssembler()->StoreDToOffset(DRegisterFrom(value), array, offset);
6509 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006510 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006511 vixl32::Register temp = temps.Acquire();
6512 __ Add(temp, array, Operand(RegisterFrom(index), vixl32::LSL, TIMES_8));
6513 GetAssembler()->StoreDToOffset(DRegisterFrom(value), temp, data_offset);
6514 }
6515 break;
6516 }
6517
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006518 case DataType::Type::kVoid:
Scott Wakelingc34dba72016-10-03 10:14:44 +01006519 LOG(FATAL) << "Unreachable type " << value_type;
6520 UNREACHABLE();
6521 }
6522
6523 // Objects are handled in the switch.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006524 if (value_type != DataType::Type::kReference) {
Alexandre Rames374ddf32016-11-04 10:40:49 +00006525 // TODO(VIXL): Ensure we record the pc position immediately after the preceding store
6526 // instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01006527 codegen_->MaybeRecordImplicitNullCheck(instruction);
6528 }
6529}
6530
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006531void LocationsBuilderARMVIXL::VisitArrayLength(HArrayLength* instruction) {
6532 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006533 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006534 locations->SetInAt(0, Location::RequiresRegister());
6535 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6536}
6537
6538void InstructionCodeGeneratorARMVIXL::VisitArrayLength(HArrayLength* instruction) {
6539 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
6540 vixl32::Register obj = InputRegisterAt(instruction, 0);
6541 vixl32::Register out = OutputRegister(instruction);
Alexandre Rames374ddf32016-11-04 10:40:49 +00006542 {
Artem Serov0fb37192016-12-06 18:13:40 +00006543 ExactAssemblyScope aas(GetVIXLAssembler(),
6544 vixl32::kMaxInstructionSizeInBytes,
6545 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00006546 __ ldr(out, MemOperand(obj, offset));
6547 codegen_->MaybeRecordImplicitNullCheck(instruction);
6548 }
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006549 // Mask out compression flag from String's array length.
6550 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006551 __ Lsr(out, out, 1u);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006552 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006553}
6554
Artem Serov2bbc9532016-10-21 11:51:50 +01006555void LocationsBuilderARMVIXL::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Artem Serov2bbc9532016-10-21 11:51:50 +01006556 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006557 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Artem Serov2bbc9532016-10-21 11:51:50 +01006558
6559 locations->SetInAt(0, Location::RequiresRegister());
6560 locations->SetInAt(1, Location::RegisterOrConstant(instruction->GetOffset()));
6561 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6562}
6563
6564void InstructionCodeGeneratorARMVIXL::VisitIntermediateAddress(HIntermediateAddress* instruction) {
6565 vixl32::Register out = OutputRegister(instruction);
6566 vixl32::Register first = InputRegisterAt(instruction, 0);
6567 Location second = instruction->GetLocations()->InAt(1);
6568
Artem Serov2bbc9532016-10-21 11:51:50 +01006569 if (second.IsRegister()) {
6570 __ Add(out, first, RegisterFrom(second));
6571 } else {
Anton Kirilov644032c2016-12-06 17:51:43 +00006572 __ Add(out, first, Int32ConstantFrom(second));
Artem Serov2bbc9532016-10-21 11:51:50 +01006573 }
6574}
6575
Artem Serove1811ed2017-04-27 16:50:47 +01006576void LocationsBuilderARMVIXL::VisitIntermediateAddressIndex(
6577 HIntermediateAddressIndex* instruction) {
6578 LOG(FATAL) << "Unreachable " << instruction->GetId();
6579}
6580
6581void InstructionCodeGeneratorARMVIXL::VisitIntermediateAddressIndex(
6582 HIntermediateAddressIndex* instruction) {
6583 LOG(FATAL) << "Unreachable " << instruction->GetId();
6584}
6585
Scott Wakelingc34dba72016-10-03 10:14:44 +01006586void LocationsBuilderARMVIXL::VisitBoundsCheck(HBoundsCheck* instruction) {
6587 RegisterSet caller_saves = RegisterSet::Empty();
6588 InvokeRuntimeCallingConventionARMVIXL calling_convention;
6589 caller_saves.Add(LocationFrom(calling_convention.GetRegisterAt(0)));
6590 caller_saves.Add(LocationFrom(calling_convention.GetRegisterAt(1)));
6591 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Artem Serov2dd053d2017-03-08 14:54:06 +00006592
6593 HInstruction* index = instruction->InputAt(0);
6594 HInstruction* length = instruction->InputAt(1);
6595 // If both index and length are constants we can statically check the bounds. But if at least one
6596 // of them is not encodable ArmEncodableConstantOrRegister will create
6597 // Location::RequiresRegister() which is not desired to happen. Instead we create constant
6598 // locations.
6599 bool both_const = index->IsConstant() && length->IsConstant();
6600 locations->SetInAt(0, both_const
6601 ? Location::ConstantLocation(index->AsConstant())
6602 : ArmEncodableConstantOrRegister(index, CMP));
6603 locations->SetInAt(1, both_const
6604 ? Location::ConstantLocation(length->AsConstant())
6605 : ArmEncodableConstantOrRegister(length, CMP));
Scott Wakelingc34dba72016-10-03 10:14:44 +01006606}
6607
6608void InstructionCodeGeneratorARMVIXL::VisitBoundsCheck(HBoundsCheck* instruction) {
Artem Serov2dd053d2017-03-08 14:54:06 +00006609 LocationSummary* locations = instruction->GetLocations();
6610 Location index_loc = locations->InAt(0);
6611 Location length_loc = locations->InAt(1);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006612
Artem Serov2dd053d2017-03-08 14:54:06 +00006613 if (length_loc.IsConstant()) {
6614 int32_t length = Int32ConstantFrom(length_loc);
6615 if (index_loc.IsConstant()) {
6616 // BCE will remove the bounds check if we are guaranteed to pass.
6617 int32_t index = Int32ConstantFrom(index_loc);
6618 if (index < 0 || index >= length) {
6619 SlowPathCodeARMVIXL* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006620 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathARMVIXL(instruction);
Artem Serov2dd053d2017-03-08 14:54:06 +00006621 codegen_->AddSlowPath(slow_path);
6622 __ B(slow_path->GetEntryLabel());
6623 } else {
6624 // Some optimization after BCE may have generated this, and we should not
6625 // generate a bounds check if it is a valid range.
6626 }
6627 return;
6628 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006629
Artem Serov2dd053d2017-03-08 14:54:06 +00006630 SlowPathCodeARMVIXL* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006631 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathARMVIXL(instruction);
Artem Serov2dd053d2017-03-08 14:54:06 +00006632 __ Cmp(RegisterFrom(index_loc), length);
6633 codegen_->AddSlowPath(slow_path);
6634 __ B(hs, slow_path->GetEntryLabel());
6635 } else {
6636 SlowPathCodeARMVIXL* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006637 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathARMVIXL(instruction);
Artem Serov2dd053d2017-03-08 14:54:06 +00006638 __ Cmp(RegisterFrom(length_loc), InputOperandAt(instruction, 0));
6639 codegen_->AddSlowPath(slow_path);
6640 __ B(ls, slow_path->GetEntryLabel());
6641 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006642}
6643
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006644void CodeGeneratorARMVIXL::MarkGCCard(vixl32::Register temp,
6645 vixl32::Register card,
6646 vixl32::Register object,
6647 vixl32::Register value,
6648 bool can_be_null) {
6649 vixl32::Label is_null;
6650 if (can_be_null) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00006651 __ CompareAndBranchIfZero(value, &is_null);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006652 }
6653 GetAssembler()->LoadFromOffset(
6654 kLoadWord, card, tr, Thread::CardTableOffset<kArmPointerSize>().Int32Value());
Scott Wakelingb77051e2016-11-21 19:46:00 +00006655 __ Lsr(temp, object, Operand::From(gc::accounting::CardTable::kCardShift));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006656 __ Strb(card, MemOperand(card, temp));
6657 if (can_be_null) {
6658 __ Bind(&is_null);
6659 }
6660}
6661
Scott Wakelingfe885462016-09-22 10:24:38 +01006662void LocationsBuilderARMVIXL::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
6663 LOG(FATAL) << "Unreachable";
6664}
6665
6666void InstructionCodeGeneratorARMVIXL::VisitParallelMove(HParallelMove* instruction) {
Vladimir Markobea75ff2017-10-11 20:39:54 +01006667 if (instruction->GetNext()->IsSuspendCheck() &&
6668 instruction->GetBlock()->GetLoopInformation() != nullptr) {
6669 HSuspendCheck* suspend_check = instruction->GetNext()->AsSuspendCheck();
6670 // The back edge will generate the suspend check.
6671 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(suspend_check, instruction);
6672 }
6673
Scott Wakelingfe885462016-09-22 10:24:38 +01006674 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
6675}
6676
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006677void LocationsBuilderARMVIXL::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006678 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6679 instruction, LocationSummary::kCallOnSlowPath);
Artem Serov657022c2016-11-23 14:19:38 +00006680 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006681}
6682
6683void InstructionCodeGeneratorARMVIXL::VisitSuspendCheck(HSuspendCheck* instruction) {
6684 HBasicBlock* block = instruction->GetBlock();
6685 if (block->GetLoopInformation() != nullptr) {
6686 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
6687 // The back edge will generate the suspend check.
6688 return;
6689 }
6690 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
6691 // The goto will generate the suspend check.
6692 return;
6693 }
6694 GenerateSuspendCheck(instruction, nullptr);
Roland Levillain5daa4952017-07-03 17:23:56 +01006695 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 12);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006696}
6697
6698void InstructionCodeGeneratorARMVIXL::GenerateSuspendCheck(HSuspendCheck* instruction,
6699 HBasicBlock* successor) {
6700 SuspendCheckSlowPathARMVIXL* slow_path =
6701 down_cast<SuspendCheckSlowPathARMVIXL*>(instruction->GetSlowPath());
6702 if (slow_path == nullptr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006703 slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006704 new (codegen_->GetScopedAllocator()) SuspendCheckSlowPathARMVIXL(instruction, successor);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006705 instruction->SetSlowPath(slow_path);
6706 codegen_->AddSlowPath(slow_path);
6707 if (successor != nullptr) {
6708 DCHECK(successor->IsLoopHeader());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006709 }
6710 } else {
6711 DCHECK_EQ(slow_path->GetSuccessor(), successor);
6712 }
6713
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006714 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006715 vixl32::Register temp = temps.Acquire();
6716 GetAssembler()->LoadFromOffset(
6717 kLoadUnsignedHalfword, temp, tr, Thread::ThreadFlagsOffset<kArmPointerSize>().Int32Value());
6718 if (successor == nullptr) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00006719 __ CompareAndBranchIfNonZero(temp, slow_path->GetEntryLabel());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006720 __ Bind(slow_path->GetReturnLabel());
6721 } else {
xueliang.zhongf51bc622016-11-04 09:23:32 +00006722 __ CompareAndBranchIfZero(temp, codegen_->GetLabelOf(successor));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006723 __ B(slow_path->GetEntryLabel());
6724 }
6725}
6726
Scott Wakelingfe885462016-09-22 10:24:38 +01006727ArmVIXLAssembler* ParallelMoveResolverARMVIXL::GetAssembler() const {
6728 return codegen_->GetAssembler();
6729}
6730
6731void ParallelMoveResolverARMVIXL::EmitMove(size_t index) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006732 UseScratchRegisterScope temps(GetAssembler()->GetVIXLAssembler());
Scott Wakelingfe885462016-09-22 10:24:38 +01006733 MoveOperands* move = moves_[index];
6734 Location source = move->GetSource();
6735 Location destination = move->GetDestination();
6736
6737 if (source.IsRegister()) {
6738 if (destination.IsRegister()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006739 __ Mov(RegisterFrom(destination), RegisterFrom(source));
Scott Wakelingfe885462016-09-22 10:24:38 +01006740 } else if (destination.IsFpuRegister()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006741 __ Vmov(SRegisterFrom(destination), RegisterFrom(source));
Scott Wakelingfe885462016-09-22 10:24:38 +01006742 } else {
6743 DCHECK(destination.IsStackSlot());
6744 GetAssembler()->StoreToOffset(kStoreWord,
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006745 RegisterFrom(source),
Scott Wakelingfe885462016-09-22 10:24:38 +01006746 sp,
6747 destination.GetStackIndex());
6748 }
6749 } else if (source.IsStackSlot()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006750 if (destination.IsRegister()) {
6751 GetAssembler()->LoadFromOffset(kLoadWord,
6752 RegisterFrom(destination),
6753 sp,
6754 source.GetStackIndex());
6755 } else if (destination.IsFpuRegister()) {
6756 GetAssembler()->LoadSFromOffset(SRegisterFrom(destination), sp, source.GetStackIndex());
6757 } else {
6758 DCHECK(destination.IsStackSlot());
6759 vixl32::Register temp = temps.Acquire();
6760 GetAssembler()->LoadFromOffset(kLoadWord, temp, sp, source.GetStackIndex());
6761 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
6762 }
Scott Wakelingfe885462016-09-22 10:24:38 +01006763 } else if (source.IsFpuRegister()) {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01006764 if (destination.IsRegister()) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006765 __ Vmov(RegisterFrom(destination), SRegisterFrom(source));
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01006766 } else if (destination.IsFpuRegister()) {
6767 __ Vmov(SRegisterFrom(destination), SRegisterFrom(source));
6768 } else {
6769 DCHECK(destination.IsStackSlot());
6770 GetAssembler()->StoreSToOffset(SRegisterFrom(source), sp, destination.GetStackIndex());
6771 }
Scott Wakelingfe885462016-09-22 10:24:38 +01006772 } else if (source.IsDoubleStackSlot()) {
Alexandre Rames9c19bd62016-10-24 11:50:32 +01006773 if (destination.IsDoubleStackSlot()) {
6774 vixl32::DRegister temp = temps.AcquireD();
6775 GetAssembler()->LoadDFromOffset(temp, sp, source.GetStackIndex());
6776 GetAssembler()->StoreDToOffset(temp, sp, destination.GetStackIndex());
6777 } else if (destination.IsRegisterPair()) {
6778 DCHECK(ExpectedPairLayout(destination));
6779 GetAssembler()->LoadFromOffset(
6780 kLoadWordPair, LowRegisterFrom(destination), sp, source.GetStackIndex());
6781 } else {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01006782 DCHECK(destination.IsFpuRegisterPair()) << destination;
6783 GetAssembler()->LoadDFromOffset(DRegisterFrom(destination), sp, source.GetStackIndex());
Alexandre Rames9c19bd62016-10-24 11:50:32 +01006784 }
Scott Wakelingfe885462016-09-22 10:24:38 +01006785 } else if (source.IsRegisterPair()) {
6786 if (destination.IsRegisterPair()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006787 __ Mov(LowRegisterFrom(destination), LowRegisterFrom(source));
6788 __ Mov(HighRegisterFrom(destination), HighRegisterFrom(source));
Scott Wakelingfe885462016-09-22 10:24:38 +01006789 } else if (destination.IsFpuRegisterPair()) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006790 __ Vmov(DRegisterFrom(destination), LowRegisterFrom(source), HighRegisterFrom(source));
Scott Wakelingfe885462016-09-22 10:24:38 +01006791 } else {
6792 DCHECK(destination.IsDoubleStackSlot()) << destination;
6793 DCHECK(ExpectedPairLayout(source));
6794 GetAssembler()->StoreToOffset(kStoreWordPair,
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006795 LowRegisterFrom(source),
Scott Wakelingfe885462016-09-22 10:24:38 +01006796 sp,
6797 destination.GetStackIndex());
6798 }
6799 } else if (source.IsFpuRegisterPair()) {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01006800 if (destination.IsRegisterPair()) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006801 __ Vmov(LowRegisterFrom(destination), HighRegisterFrom(destination), DRegisterFrom(source));
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01006802 } else if (destination.IsFpuRegisterPair()) {
6803 __ Vmov(DRegisterFrom(destination), DRegisterFrom(source));
6804 } else {
6805 DCHECK(destination.IsDoubleStackSlot()) << destination;
6806 GetAssembler()->StoreDToOffset(DRegisterFrom(source), sp, destination.GetStackIndex());
6807 }
Scott Wakelingfe885462016-09-22 10:24:38 +01006808 } else {
6809 DCHECK(source.IsConstant()) << source;
6810 HConstant* constant = source.GetConstant();
6811 if (constant->IsIntConstant() || constant->IsNullConstant()) {
6812 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
6813 if (destination.IsRegister()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006814 __ Mov(RegisterFrom(destination), value);
Scott Wakelingfe885462016-09-22 10:24:38 +01006815 } else {
6816 DCHECK(destination.IsStackSlot());
Scott Wakelingfe885462016-09-22 10:24:38 +01006817 vixl32::Register temp = temps.Acquire();
6818 __ Mov(temp, value);
6819 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
6820 }
6821 } else if (constant->IsLongConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006822 int64_t value = Int64ConstantFrom(source);
Scott Wakelingfe885462016-09-22 10:24:38 +01006823 if (destination.IsRegisterPair()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006824 __ Mov(LowRegisterFrom(destination), Low32Bits(value));
6825 __ Mov(HighRegisterFrom(destination), High32Bits(value));
Scott Wakelingfe885462016-09-22 10:24:38 +01006826 } else {
6827 DCHECK(destination.IsDoubleStackSlot()) << destination;
Scott Wakelingfe885462016-09-22 10:24:38 +01006828 vixl32::Register temp = temps.Acquire();
6829 __ Mov(temp, Low32Bits(value));
6830 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
6831 __ Mov(temp, High32Bits(value));
6832 GetAssembler()->StoreToOffset(kStoreWord,
6833 temp,
6834 sp,
6835 destination.GetHighStackIndex(kArmWordSize));
6836 }
6837 } else if (constant->IsDoubleConstant()) {
6838 double value = constant->AsDoubleConstant()->GetValue();
6839 if (destination.IsFpuRegisterPair()) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006840 __ Vmov(DRegisterFrom(destination), value);
Scott Wakelingfe885462016-09-22 10:24:38 +01006841 } else {
6842 DCHECK(destination.IsDoubleStackSlot()) << destination;
6843 uint64_t int_value = bit_cast<uint64_t, double>(value);
Scott Wakelingfe885462016-09-22 10:24:38 +01006844 vixl32::Register temp = temps.Acquire();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006845 __ Mov(temp, Low32Bits(int_value));
Scott Wakelingfe885462016-09-22 10:24:38 +01006846 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006847 __ Mov(temp, High32Bits(int_value));
Scott Wakelingfe885462016-09-22 10:24:38 +01006848 GetAssembler()->StoreToOffset(kStoreWord,
6849 temp,
6850 sp,
6851 destination.GetHighStackIndex(kArmWordSize));
6852 }
6853 } else {
6854 DCHECK(constant->IsFloatConstant()) << constant->DebugName();
6855 float value = constant->AsFloatConstant()->GetValue();
6856 if (destination.IsFpuRegister()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006857 __ Vmov(SRegisterFrom(destination), value);
Scott Wakelingfe885462016-09-22 10:24:38 +01006858 } else {
6859 DCHECK(destination.IsStackSlot());
Scott Wakelingfe885462016-09-22 10:24:38 +01006860 vixl32::Register temp = temps.Acquire();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006861 __ Mov(temp, bit_cast<int32_t, float>(value));
Scott Wakelingfe885462016-09-22 10:24:38 +01006862 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
6863 }
6864 }
6865 }
6866}
6867
Alexandre Rames9c19bd62016-10-24 11:50:32 +01006868void ParallelMoveResolverARMVIXL::Exchange(vixl32::Register reg, int mem) {
6869 UseScratchRegisterScope temps(GetAssembler()->GetVIXLAssembler());
6870 vixl32::Register temp = temps.Acquire();
6871 __ Mov(temp, reg);
6872 GetAssembler()->LoadFromOffset(kLoadWord, reg, sp, mem);
6873 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, mem);
Scott Wakelingfe885462016-09-22 10:24:38 +01006874}
6875
Alexandre Rames9c19bd62016-10-24 11:50:32 +01006876void ParallelMoveResolverARMVIXL::Exchange(int mem1, int mem2) {
6877 // TODO(VIXL32): Double check the performance of this implementation.
6878 UseScratchRegisterScope temps(GetAssembler()->GetVIXLAssembler());
Nicolas Geoffray13a797b2017-03-15 16:41:31 +00006879 vixl32::Register temp1 = temps.Acquire();
6880 ScratchRegisterScope ensure_scratch(
6881 this, temp1.GetCode(), r0.GetCode(), codegen_->GetNumberOfCoreRegisters());
6882 vixl32::Register temp2(ensure_scratch.GetRegister());
Alexandre Rames9c19bd62016-10-24 11:50:32 +01006883
Nicolas Geoffray13a797b2017-03-15 16:41:31 +00006884 int stack_offset = ensure_scratch.IsSpilled() ? kArmWordSize : 0;
6885 GetAssembler()->LoadFromOffset(kLoadWord, temp1, sp, mem1 + stack_offset);
6886 GetAssembler()->LoadFromOffset(kLoadWord, temp2, sp, mem2 + stack_offset);
6887 GetAssembler()->StoreToOffset(kStoreWord, temp1, sp, mem2 + stack_offset);
6888 GetAssembler()->StoreToOffset(kStoreWord, temp2, sp, mem1 + stack_offset);
Scott Wakelingfe885462016-09-22 10:24:38 +01006889}
6890
Alexandre Rames9c19bd62016-10-24 11:50:32 +01006891void ParallelMoveResolverARMVIXL::EmitSwap(size_t index) {
6892 MoveOperands* move = moves_[index];
6893 Location source = move->GetSource();
6894 Location destination = move->GetDestination();
6895 UseScratchRegisterScope temps(GetAssembler()->GetVIXLAssembler());
6896
6897 if (source.IsRegister() && destination.IsRegister()) {
6898 vixl32::Register temp = temps.Acquire();
6899 DCHECK(!RegisterFrom(source).Is(temp));
6900 DCHECK(!RegisterFrom(destination).Is(temp));
6901 __ Mov(temp, RegisterFrom(destination));
6902 __ Mov(RegisterFrom(destination), RegisterFrom(source));
6903 __ Mov(RegisterFrom(source), temp);
6904 } else if (source.IsRegister() && destination.IsStackSlot()) {
6905 Exchange(RegisterFrom(source), destination.GetStackIndex());
6906 } else if (source.IsStackSlot() && destination.IsRegister()) {
6907 Exchange(RegisterFrom(destination), source.GetStackIndex());
6908 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Anton Kirilovdda43962016-11-21 19:55:20 +00006909 Exchange(source.GetStackIndex(), destination.GetStackIndex());
Alexandre Rames9c19bd62016-10-24 11:50:32 +01006910 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Nicolas Geoffray13a797b2017-03-15 16:41:31 +00006911 vixl32::Register temp = temps.Acquire();
Anton Kirilovdda43962016-11-21 19:55:20 +00006912 __ Vmov(temp, SRegisterFrom(source));
6913 __ Vmov(SRegisterFrom(source), SRegisterFrom(destination));
6914 __ Vmov(SRegisterFrom(destination), temp);
Alexandre Rames9c19bd62016-10-24 11:50:32 +01006915 } else if (source.IsRegisterPair() && destination.IsRegisterPair()) {
6916 vixl32::DRegister temp = temps.AcquireD();
6917 __ Vmov(temp, LowRegisterFrom(source), HighRegisterFrom(source));
6918 __ Mov(LowRegisterFrom(source), LowRegisterFrom(destination));
6919 __ Mov(HighRegisterFrom(source), HighRegisterFrom(destination));
6920 __ Vmov(LowRegisterFrom(destination), HighRegisterFrom(destination), temp);
6921 } else if (source.IsRegisterPair() || destination.IsRegisterPair()) {
6922 vixl32::Register low_reg = LowRegisterFrom(source.IsRegisterPair() ? source : destination);
6923 int mem = source.IsRegisterPair() ? destination.GetStackIndex() : source.GetStackIndex();
6924 DCHECK(ExpectedPairLayout(source.IsRegisterPair() ? source : destination));
6925 vixl32::DRegister temp = temps.AcquireD();
6926 __ Vmov(temp, low_reg, vixl32::Register(low_reg.GetCode() + 1));
6927 GetAssembler()->LoadFromOffset(kLoadWordPair, low_reg, sp, mem);
6928 GetAssembler()->StoreDToOffset(temp, sp, mem);
6929 } else if (source.IsFpuRegisterPair() && destination.IsFpuRegisterPair()) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006930 vixl32::DRegister first = DRegisterFrom(source);
6931 vixl32::DRegister second = DRegisterFrom(destination);
6932 vixl32::DRegister temp = temps.AcquireD();
6933 __ Vmov(temp, first);
6934 __ Vmov(first, second);
6935 __ Vmov(second, temp);
Alexandre Rames9c19bd62016-10-24 11:50:32 +01006936 } else if (source.IsFpuRegisterPair() || destination.IsFpuRegisterPair()) {
Anton Kirilovdda43962016-11-21 19:55:20 +00006937 vixl32::DRegister reg = source.IsFpuRegisterPair()
6938 ? DRegisterFrom(source)
6939 : DRegisterFrom(destination);
6940 int mem = source.IsFpuRegisterPair()
6941 ? destination.GetStackIndex()
6942 : source.GetStackIndex();
6943 vixl32::DRegister temp = temps.AcquireD();
6944 __ Vmov(temp, reg);
6945 GetAssembler()->LoadDFromOffset(reg, sp, mem);
6946 GetAssembler()->StoreDToOffset(temp, sp, mem);
Alexandre Rames9c19bd62016-10-24 11:50:32 +01006947 } else if (source.IsFpuRegister() || destination.IsFpuRegister()) {
Anton Kirilovdda43962016-11-21 19:55:20 +00006948 vixl32::SRegister reg = source.IsFpuRegister()
6949 ? SRegisterFrom(source)
6950 : SRegisterFrom(destination);
6951 int mem = source.IsFpuRegister()
6952 ? destination.GetStackIndex()
6953 : source.GetStackIndex();
6954 vixl32::Register temp = temps.Acquire();
6955 __ Vmov(temp, reg);
6956 GetAssembler()->LoadSFromOffset(reg, sp, mem);
6957 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, mem);
Alexandre Rames9c19bd62016-10-24 11:50:32 +01006958 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
6959 vixl32::DRegister temp1 = temps.AcquireD();
6960 vixl32::DRegister temp2 = temps.AcquireD();
6961 __ Vldr(temp1, MemOperand(sp, source.GetStackIndex()));
6962 __ Vldr(temp2, MemOperand(sp, destination.GetStackIndex()));
6963 __ Vstr(temp1, MemOperand(sp, destination.GetStackIndex()));
6964 __ Vstr(temp2, MemOperand(sp, source.GetStackIndex()));
6965 } else {
6966 LOG(FATAL) << "Unimplemented" << source << " <-> " << destination;
6967 }
Scott Wakelingfe885462016-09-22 10:24:38 +01006968}
6969
Nicolas Geoffray13a797b2017-03-15 16:41:31 +00006970void ParallelMoveResolverARMVIXL::SpillScratch(int reg) {
6971 __ Push(vixl32::Register(reg));
Scott Wakelingfe885462016-09-22 10:24:38 +01006972}
6973
Nicolas Geoffray13a797b2017-03-15 16:41:31 +00006974void ParallelMoveResolverARMVIXL::RestoreScratch(int reg) {
6975 __ Pop(vixl32::Register(reg));
Scott Wakelingfe885462016-09-22 10:24:38 +01006976}
6977
Artem Serov02d37832016-10-25 15:25:33 +01006978HLoadClass::LoadKind CodeGeneratorARMVIXL::GetSupportedLoadClassKind(
Artem Serovd4cc5b22016-11-04 11:19:09 +00006979 HLoadClass::LoadKind desired_class_load_kind) {
6980 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006981 case HLoadClass::LoadKind::kInvalid:
6982 LOG(FATAL) << "UNREACHABLE";
6983 UNREACHABLE();
Artem Serovd4cc5b22016-11-04 11:19:09 +00006984 case HLoadClass::LoadKind::kReferrersClass:
6985 break;
Artem Serovd4cc5b22016-11-04 11:19:09 +00006986 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006987 case HLoadClass::LoadKind::kBootImageClassTable:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006988 case HLoadClass::LoadKind::kBssEntry:
6989 DCHECK(!Runtime::Current()->UseJitCompilation());
6990 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006991 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006992 DCHECK(Runtime::Current()->UseJitCompilation());
Artem Serovc5fcb442016-12-02 19:19:58 +00006993 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01006994 case HLoadClass::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006995 case HLoadClass::LoadKind::kRuntimeCall:
Artem Serovd4cc5b22016-11-04 11:19:09 +00006996 break;
6997 }
6998 return desired_class_load_kind;
Artem Serov02d37832016-10-25 15:25:33 +01006999}
7000
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007001void LocationsBuilderARMVIXL::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00007002 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007003 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007004 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00007005 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007006 cls,
7007 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00007008 LocationFrom(r0));
Vladimir Markoea4c1262017-02-06 19:59:33 +00007009 DCHECK(calling_convention.GetRegisterAt(0).Is(r0));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007010 return;
7011 }
Vladimir Marko41559982017-01-06 14:04:23 +00007012 DCHECK(!cls->NeedsAccessCheck());
Scott Wakelingfe885462016-09-22 10:24:38 +01007013
Artem Serovd4cc5b22016-11-04 11:19:09 +00007014 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
7015 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007016 ? LocationSummary::kCallOnSlowPath
7017 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01007018 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(cls, call_kind);
Artem Serovd4cc5b22016-11-04 11:19:09 +00007019 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007020 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Artem Serovd4cc5b22016-11-04 11:19:09 +00007021 }
7022
Vladimir Marko41559982017-01-06 14:04:23 +00007023 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007024 locations->SetInAt(0, Location::RequiresRegister());
7025 }
7026 locations->SetOut(Location::RequiresRegister());
Vladimir Markoea4c1262017-02-06 19:59:33 +00007027 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
7028 if (!kUseReadBarrier || kUseBakerReadBarrier) {
7029 // Rely on the type resolution or initialization and marking to save everything we need.
Vladimir Markoea4c1262017-02-06 19:59:33 +00007030 RegisterSet caller_saves = RegisterSet::Empty();
7031 InvokeRuntimeCallingConventionARMVIXL calling_convention;
7032 caller_saves.Add(LocationFrom(calling_convention.GetRegisterAt(0)));
7033 // TODO: Add GetReturnLocation() to the calling convention so that we can DCHECK()
7034 // that the the kPrimNot result register is the same as the first argument register.
7035 locations->SetCustomSlowPathCallerSaves(caller_saves);
7036 } else {
7037 // For non-Baker read barrier we have a temp-clobbering call.
7038 }
7039 }
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01007040 if (kUseBakerReadBarrier && kBakerReadBarrierLinkTimeThunksEnableForGcRoots) {
7041 if (load_kind == HLoadClass::LoadKind::kBssEntry ||
7042 (load_kind == HLoadClass::LoadKind::kReferrersClass &&
7043 !Runtime::Current()->UseJitCompilation())) {
7044 locations->AddTemp(Location::RegisterLocation(kBakerCcEntrypointRegister.GetCode()));
7045 }
7046 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007047}
7048
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007049// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
7050// move.
7051void InstructionCodeGeneratorARMVIXL::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00007052 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007053 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00007054 codegen_->GenerateLoadClassRuntimeCall(cls);
Roland Levillain5daa4952017-07-03 17:23:56 +01007055 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 13);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007056 return;
7057 }
Vladimir Marko41559982017-01-06 14:04:23 +00007058 DCHECK(!cls->NeedsAccessCheck());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007059
Vladimir Marko41559982017-01-06 14:04:23 +00007060 LocationSummary* locations = cls->GetLocations();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007061 Location out_loc = locations->Out();
7062 vixl32::Register out = OutputRegister(cls);
7063
Artem Serovd4cc5b22016-11-04 11:19:09 +00007064 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
7065 ? kWithoutReadBarrier
7066 : kCompilerReadBarrierOption;
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007067 bool generate_null_check = false;
Vladimir Marko41559982017-01-06 14:04:23 +00007068 switch (load_kind) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007069 case HLoadClass::LoadKind::kReferrersClass: {
7070 DCHECK(!cls->CanCallRuntime());
7071 DCHECK(!cls->MustGenerateClinitCheck());
7072 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
7073 vixl32::Register current_method = InputRegisterAt(cls, 0);
7074 GenerateGcRootFieldLoad(cls,
7075 out_loc,
7076 current_method,
Roland Levillain00468f32016-10-27 18:02:48 +01007077 ArtMethod::DeclaringClassOffset().Int32Value(),
Artem Serovd4cc5b22016-11-04 11:19:09 +00007078 read_barrier_option);
7079 break;
7080 }
Artem Serovd4cc5b22016-11-04 11:19:09 +00007081 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007082 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Artem Serovd4cc5b22016-11-04 11:19:09 +00007083 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
7084 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
7085 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
7086 codegen_->EmitMovwMovtPlaceholder(labels, out);
7087 break;
7088 }
7089 case HLoadClass::LoadKind::kBootImageAddress: {
Artem Serovc5fcb442016-12-02 19:19:58 +00007090 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007091 uint32_t address = dchecked_integral_cast<uint32_t>(
7092 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
7093 DCHECK_NE(address, 0u);
Artem Serovc5fcb442016-12-02 19:19:58 +00007094 __ Ldr(out, codegen_->DeduplicateBootImageAddressLiteral(address));
Artem Serovd4cc5b22016-11-04 11:19:09 +00007095 break;
7096 }
Vladimir Marko94ec2db2017-09-06 17:21:03 +01007097 case HLoadClass::LoadKind::kBootImageClassTable: {
7098 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
7099 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
7100 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
7101 codegen_->EmitMovwMovtPlaceholder(labels, out);
7102 __ Ldr(out, MemOperand(out, /* offset */ 0));
7103 // Extract the reference from the slot data, i.e. clear the hash bits.
7104 int32_t masked_hash = ClassTable::TableSlot::MaskHash(
7105 ComputeModifiedUtf8Hash(cls->GetDexFile().StringByTypeIdx(cls->GetTypeIndex())));
7106 if (masked_hash != 0) {
7107 __ Sub(out, out, Operand(masked_hash));
7108 }
7109 break;
7110 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007111 case HLoadClass::LoadKind::kBssEntry: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007112 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
Vladimir Marko1998cd02017-01-13 13:02:58 +00007113 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex());
Vladimir Markof3c52b42017-11-17 17:32:12 +00007114 codegen_->EmitMovwMovtPlaceholder(labels, out);
7115 GenerateGcRootFieldLoad(cls, out_loc, out, /* offset */ 0, read_barrier_option);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007116 generate_null_check = true;
7117 break;
7118 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007119 case HLoadClass::LoadKind::kJitTableAddress: {
Artem Serovc5fcb442016-12-02 19:19:58 +00007120 __ Ldr(out, codegen_->DeduplicateJitClassLiteral(cls->GetDexFile(),
7121 cls->GetTypeIndex(),
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007122 cls->GetClass()));
Artem Serovc5fcb442016-12-02 19:19:58 +00007123 // /* GcRoot<mirror::Class> */ out = *out
Vladimir Markoea4c1262017-02-06 19:59:33 +00007124 GenerateGcRootFieldLoad(cls, out_loc, out, /* offset */ 0, read_barrier_option);
Artem Serovd4cc5b22016-11-04 11:19:09 +00007125 break;
7126 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007127 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00007128 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00007129 LOG(FATAL) << "UNREACHABLE";
7130 UNREACHABLE();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007131 }
7132
7133 if (generate_null_check || cls->MustGenerateClinitCheck()) {
7134 DCHECK(cls->CanCallRuntime());
Vladimir Marko174b2e22017-10-12 13:34:49 +01007135 LoadClassSlowPathARMVIXL* slow_path =
7136 new (codegen_->GetScopedAllocator()) LoadClassSlowPathARMVIXL(
7137 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007138 codegen_->AddSlowPath(slow_path);
7139 if (generate_null_check) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00007140 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007141 }
7142 if (cls->MustGenerateClinitCheck()) {
7143 GenerateClassInitializationCheck(slow_path, out);
7144 } else {
7145 __ Bind(slow_path->GetExitLabel());
7146 }
Roland Levillain5daa4952017-07-03 17:23:56 +01007147 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 14);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007148 }
7149}
7150
Artem Serov02d37832016-10-25 15:25:33 +01007151void LocationsBuilderARMVIXL::VisitClinitCheck(HClinitCheck* check) {
7152 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007153 new (GetGraph()->GetAllocator()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
Artem Serov02d37832016-10-25 15:25:33 +01007154 locations->SetInAt(0, Location::RequiresRegister());
7155 if (check->HasUses()) {
7156 locations->SetOut(Location::SameAsFirstInput());
7157 }
7158}
7159
7160void InstructionCodeGeneratorARMVIXL::VisitClinitCheck(HClinitCheck* check) {
7161 // We assume the class is not null.
7162 LoadClassSlowPathARMVIXL* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01007163 new (codegen_->GetScopedAllocator()) LoadClassSlowPathARMVIXL(check->GetLoadClass(),
7164 check,
7165 check->GetDexPc(),
7166 /* do_clinit */ true);
Artem Serov02d37832016-10-25 15:25:33 +01007167 codegen_->AddSlowPath(slow_path);
7168 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
7169}
7170
7171void InstructionCodeGeneratorARMVIXL::GenerateClassInitializationCheck(
7172 LoadClassSlowPathARMVIXL* slow_path, vixl32::Register class_reg) {
7173 UseScratchRegisterScope temps(GetVIXLAssembler());
7174 vixl32::Register temp = temps.Acquire();
Vladimir Markodc682aa2018-01-04 18:42:57 +00007175 constexpr size_t status_lsb_position = SubtypeCheckBits::BitStructSizeOf();
7176 const size_t status_byte_offset =
7177 mirror::Class::StatusOffset().SizeValue() + (status_lsb_position / kBitsPerByte);
7178 constexpr uint32_t shifted_initialized_value =
7179 enum_cast<uint32_t>(ClassStatus::kInitialized) << (status_lsb_position % kBitsPerByte);
7180
7181 GetAssembler()->LoadFromOffset(kLoadUnsignedByte, temp, class_reg, status_byte_offset);
7182 __ Cmp(temp, shifted_initialized_value);
Vladimir Marko2c64a832018-01-04 11:31:56 +00007183 __ B(lo, slow_path->GetEntryLabel());
Artem Serov02d37832016-10-25 15:25:33 +01007184 // Even if the initialized flag is set, we may be in a situation where caches are not synced
7185 // properly. Therefore, we do a memory fence.
7186 __ Dmb(ISH);
7187 __ Bind(slow_path->GetExitLabel());
7188}
7189
Artem Serov02d37832016-10-25 15:25:33 +01007190HLoadString::LoadKind CodeGeneratorARMVIXL::GetSupportedLoadStringKind(
Artem Serovd4cc5b22016-11-04 11:19:09 +00007191 HLoadString::LoadKind desired_string_load_kind) {
7192 switch (desired_string_load_kind) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00007193 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007194 case HLoadString::LoadKind::kBootImageInternTable:
Artem Serovd4cc5b22016-11-04 11:19:09 +00007195 case HLoadString::LoadKind::kBssEntry:
7196 DCHECK(!Runtime::Current()->UseJitCompilation());
7197 break;
7198 case HLoadString::LoadKind::kJitTableAddress:
7199 DCHECK(Runtime::Current()->UseJitCompilation());
Artem Serovc5fcb442016-12-02 19:19:58 +00007200 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01007201 case HLoadString::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007202 case HLoadString::LoadKind::kRuntimeCall:
Artem Serovd4cc5b22016-11-04 11:19:09 +00007203 break;
7204 }
7205 return desired_string_load_kind;
Artem Serov02d37832016-10-25 15:25:33 +01007206}
7207
7208void LocationsBuilderARMVIXL::VisitLoadString(HLoadString* load) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00007209 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Vladimir Markoca6fff82017-10-03 14:49:14 +01007210 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(load, call_kind);
Artem Serov02d37832016-10-25 15:25:33 +01007211 HLoadString::LoadKind load_kind = load->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007212 if (load_kind == HLoadString::LoadKind::kRuntimeCall) {
Artem Serov02d37832016-10-25 15:25:33 +01007213 locations->SetOut(LocationFrom(r0));
7214 } else {
7215 locations->SetOut(Location::RequiresRegister());
Artem Serovd4cc5b22016-11-04 11:19:09 +00007216 if (load_kind == HLoadString::LoadKind::kBssEntry) {
7217 if (!kUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00007218 // Rely on the pResolveString and marking to save everything we need, including temps.
Artem Serovd4cc5b22016-11-04 11:19:09 +00007219 RegisterSet caller_saves = RegisterSet::Empty();
7220 InvokeRuntimeCallingConventionARMVIXL calling_convention;
7221 caller_saves.Add(LocationFrom(calling_convention.GetRegisterAt(0)));
7222 // TODO: Add GetReturnLocation() to the calling convention so that we can DCHECK()
7223 // that the the kPrimNot result register is the same as the first argument register.
7224 locations->SetCustomSlowPathCallerSaves(caller_saves);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01007225 if (kUseBakerReadBarrier && kBakerReadBarrierLinkTimeThunksEnableForGcRoots) {
7226 locations->AddTemp(Location::RegisterLocation(kBakerCcEntrypointRegister.GetCode()));
7227 }
Artem Serovd4cc5b22016-11-04 11:19:09 +00007228 } else {
7229 // For non-Baker read barrier we have a temp-clobbering call.
7230 }
7231 }
Artem Serov02d37832016-10-25 15:25:33 +01007232 }
7233}
7234
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007235// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
7236// move.
7237void InstructionCodeGeneratorARMVIXL::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Artem Serovd4cc5b22016-11-04 11:19:09 +00007238 LocationSummary* locations = load->GetLocations();
7239 Location out_loc = locations->Out();
7240 vixl32::Register out = OutputRegister(load);
7241 HLoadString::LoadKind load_kind = load->GetLoadKind();
7242
7243 switch (load_kind) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00007244 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
7245 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
7246 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007247 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Artem Serovd4cc5b22016-11-04 11:19:09 +00007248 codegen_->EmitMovwMovtPlaceholder(labels, out);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007249 return;
Artem Serovd4cc5b22016-11-04 11:19:09 +00007250 }
7251 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007252 uint32_t address = dchecked_integral_cast<uint32_t>(
7253 reinterpret_cast<uintptr_t>(load->GetString().Get()));
7254 DCHECK_NE(address, 0u);
Artem Serovc5fcb442016-12-02 19:19:58 +00007255 __ Ldr(out, codegen_->DeduplicateBootImageAddressLiteral(address));
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007256 return;
7257 }
7258 case HLoadString::LoadKind::kBootImageInternTable: {
7259 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
7260 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
7261 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
7262 codegen_->EmitMovwMovtPlaceholder(labels, out);
7263 __ Ldr(out, MemOperand(out, /* offset */ 0));
7264 return;
Artem Serovd4cc5b22016-11-04 11:19:09 +00007265 }
7266 case HLoadString::LoadKind::kBssEntry: {
7267 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Artem Serovd4cc5b22016-11-04 11:19:09 +00007268 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007269 codegen_->NewStringBssEntryPatch(load->GetDexFile(), load->GetStringIndex());
Vladimir Markof3c52b42017-11-17 17:32:12 +00007270 codegen_->EmitMovwMovtPlaceholder(labels, out);
7271 GenerateGcRootFieldLoad(load, out_loc, out, /* offset */ 0, kCompilerReadBarrierOption);
Artem Serovd4cc5b22016-11-04 11:19:09 +00007272 LoadStringSlowPathARMVIXL* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01007273 new (codegen_->GetScopedAllocator()) LoadStringSlowPathARMVIXL(load);
Artem Serovd4cc5b22016-11-04 11:19:09 +00007274 codegen_->AddSlowPath(slow_path);
7275 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
7276 __ Bind(slow_path->GetExitLabel());
Roland Levillain5daa4952017-07-03 17:23:56 +01007277 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 15);
Artem Serovd4cc5b22016-11-04 11:19:09 +00007278 return;
7279 }
7280 case HLoadString::LoadKind::kJitTableAddress: {
Artem Serovc5fcb442016-12-02 19:19:58 +00007281 __ Ldr(out, codegen_->DeduplicateJitStringLiteral(load->GetDexFile(),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007282 load->GetStringIndex(),
7283 load->GetString()));
Artem Serovc5fcb442016-12-02 19:19:58 +00007284 // /* GcRoot<mirror::String> */ out = *out
7285 GenerateGcRootFieldLoad(load, out_loc, out, /* offset */ 0, kCompilerReadBarrierOption);
7286 return;
Artem Serovd4cc5b22016-11-04 11:19:09 +00007287 }
7288 default:
7289 break;
7290 }
Artem Serov02d37832016-10-25 15:25:33 +01007291
7292 // TODO: Re-add the compiler code to do string dex cache lookup again.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007293 DCHECK_EQ(load->GetLoadKind(), HLoadString::LoadKind::kRuntimeCall);
Artem Serov02d37832016-10-25 15:25:33 +01007294 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Andreas Gampe8a0128a2016-11-28 07:38:35 -08007295 __ Mov(calling_convention.GetRegisterAt(0), load->GetStringIndex().index_);
Artem Serov02d37832016-10-25 15:25:33 +01007296 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
7297 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Roland Levillain5daa4952017-07-03 17:23:56 +01007298 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 16);
Artem Serov02d37832016-10-25 15:25:33 +01007299}
7300
7301static int32_t GetExceptionTlsOffset() {
7302 return Thread::ExceptionOffset<kArmPointerSize>().Int32Value();
7303}
7304
7305void LocationsBuilderARMVIXL::VisitLoadException(HLoadException* load) {
7306 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007307 new (GetGraph()->GetAllocator()) LocationSummary(load, LocationSummary::kNoCall);
Artem Serov02d37832016-10-25 15:25:33 +01007308 locations->SetOut(Location::RequiresRegister());
7309}
7310
7311void InstructionCodeGeneratorARMVIXL::VisitLoadException(HLoadException* load) {
7312 vixl32::Register out = OutputRegister(load);
7313 GetAssembler()->LoadFromOffset(kLoadWord, out, tr, GetExceptionTlsOffset());
7314}
7315
7316
7317void LocationsBuilderARMVIXL::VisitClearException(HClearException* clear) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007318 new (GetGraph()->GetAllocator()) LocationSummary(clear, LocationSummary::kNoCall);
Artem Serov02d37832016-10-25 15:25:33 +01007319}
7320
7321void InstructionCodeGeneratorARMVIXL::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
7322 UseScratchRegisterScope temps(GetVIXLAssembler());
7323 vixl32::Register temp = temps.Acquire();
7324 __ Mov(temp, 0);
7325 GetAssembler()->StoreToOffset(kStoreWord, temp, tr, GetExceptionTlsOffset());
7326}
7327
7328void LocationsBuilderARMVIXL::VisitThrow(HThrow* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007329 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
7330 instruction, LocationSummary::kCallOnMainOnly);
Artem Serov02d37832016-10-25 15:25:33 +01007331 InvokeRuntimeCallingConventionARMVIXL calling_convention;
7332 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
7333}
7334
7335void InstructionCodeGeneratorARMVIXL::VisitThrow(HThrow* instruction) {
7336 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
7337 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
7338}
7339
Artem Serov657022c2016-11-23 14:19:38 +00007340// Temp is used for read barrier.
7341static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
7342 if (kEmitCompilerReadBarrier &&
7343 (kUseBakerReadBarrier ||
7344 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
7345 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
7346 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
7347 return 1;
7348 }
7349 return 0;
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007350}
7351
Artem Serov657022c2016-11-23 14:19:38 +00007352// Interface case has 3 temps, one for holding the number of interfaces, one for the current
7353// interface pointer, one for loading the current interface.
7354// The other checks have one temp for loading the object's class.
7355static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
7356 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
7357 return 3;
7358 }
7359 return 1 + NumberOfInstanceOfTemps(type_check_kind);
7360}
Artem Serovcfbe9132016-10-14 15:58:56 +01007361
7362void LocationsBuilderARMVIXL::VisitInstanceOf(HInstanceOf* instruction) {
7363 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
7364 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
7365 bool baker_read_barrier_slow_path = false;
7366 switch (type_check_kind) {
7367 case TypeCheckKind::kExactCheck:
7368 case TypeCheckKind::kAbstractClassCheck:
7369 case TypeCheckKind::kClassHierarchyCheck:
Vladimir Marko87584542017-12-12 17:47:52 +00007370 case TypeCheckKind::kArrayObjectCheck: {
7371 bool needs_read_barrier = CodeGenerator::InstanceOfNeedsReadBarrier(instruction);
7372 call_kind = needs_read_barrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
7373 baker_read_barrier_slow_path = kUseBakerReadBarrier && needs_read_barrier;
Artem Serovcfbe9132016-10-14 15:58:56 +01007374 break;
Vladimir Marko87584542017-12-12 17:47:52 +00007375 }
Artem Serovcfbe9132016-10-14 15:58:56 +01007376 case TypeCheckKind::kArrayCheck:
7377 case TypeCheckKind::kUnresolvedCheck:
7378 case TypeCheckKind::kInterfaceCheck:
7379 call_kind = LocationSummary::kCallOnSlowPath;
7380 break;
7381 }
7382
Vladimir Markoca6fff82017-10-03 14:49:14 +01007383 LocationSummary* locations =
7384 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Artem Serovcfbe9132016-10-14 15:58:56 +01007385 if (baker_read_barrier_slow_path) {
7386 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
7387 }
7388 locations->SetInAt(0, Location::RequiresRegister());
7389 locations->SetInAt(1, Location::RequiresRegister());
7390 // The "out" register is used as a temporary, so it overlaps with the inputs.
7391 // Note that TypeCheckSlowPathARM uses this register too.
7392 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Artem Serov657022c2016-11-23 14:19:38 +00007393 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01007394 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
7395 codegen_->MaybeAddBakerCcEntrypointTempForFields(locations);
7396 }
Artem Serovcfbe9132016-10-14 15:58:56 +01007397}
7398
7399void InstructionCodeGeneratorARMVIXL::VisitInstanceOf(HInstanceOf* instruction) {
7400 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
7401 LocationSummary* locations = instruction->GetLocations();
7402 Location obj_loc = locations->InAt(0);
7403 vixl32::Register obj = InputRegisterAt(instruction, 0);
7404 vixl32::Register cls = InputRegisterAt(instruction, 1);
7405 Location out_loc = locations->Out();
7406 vixl32::Register out = OutputRegister(instruction);
Artem Serov657022c2016-11-23 14:19:38 +00007407 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
7408 DCHECK_LE(num_temps, 1u);
7409 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Artem Serovcfbe9132016-10-14 15:58:56 +01007410 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
7411 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
7412 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
7413 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007414 vixl32::Label done;
7415 vixl32::Label* const final_label = codegen_->GetFinalLabel(instruction, &done);
Artem Serovcfbe9132016-10-14 15:58:56 +01007416 SlowPathCodeARMVIXL* slow_path = nullptr;
7417
7418 // Return 0 if `obj` is null.
7419 // avoid null check if we know obj is not null.
7420 if (instruction->MustDoNullCheck()) {
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007421 DCHECK(!out.Is(obj));
7422 __ Mov(out, 0);
7423 __ CompareAndBranchIfZero(obj, final_label, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007424 }
7425
Artem Serovcfbe9132016-10-14 15:58:56 +01007426 switch (type_check_kind) {
7427 case TypeCheckKind::kExactCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00007428 ReadBarrierOption read_barrier_option =
7429 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier6beced42016-11-15 15:51:31 -08007430 // /* HeapReference<Class> */ out = obj->klass_
7431 GenerateReferenceLoadTwoRegisters(instruction,
7432 out_loc,
7433 obj_loc,
7434 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00007435 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007436 read_barrier_option);
Artem Serovcfbe9132016-10-14 15:58:56 +01007437 // Classes must be equal for the instanceof to succeed.
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007438 __ Cmp(out, cls);
7439 // We speculatively set the result to false without changing the condition
7440 // flags, which allows us to avoid some branching later.
7441 __ Mov(LeaveFlags, out, 0);
7442
7443 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
7444 // we check that the output is in a low register, so that a 16-bit MOV
7445 // encoding can be used.
7446 if (out.IsLow()) {
7447 // We use the scope because of the IT block that follows.
7448 ExactAssemblyScope guard(GetVIXLAssembler(),
7449 2 * vixl32::k16BitT32InstructionSizeInBytes,
7450 CodeBufferCheckScope::kExactSize);
7451
7452 __ it(eq);
7453 __ mov(eq, out, 1);
7454 } else {
7455 __ B(ne, final_label, /* far_target */ false);
7456 __ Mov(out, 1);
7457 }
7458
Artem Serovcfbe9132016-10-14 15:58:56 +01007459 break;
7460 }
7461
7462 case TypeCheckKind::kAbstractClassCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00007463 ReadBarrierOption read_barrier_option =
7464 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier6beced42016-11-15 15:51:31 -08007465 // /* HeapReference<Class> */ out = obj->klass_
7466 GenerateReferenceLoadTwoRegisters(instruction,
7467 out_loc,
7468 obj_loc,
7469 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00007470 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007471 read_barrier_option);
Artem Serovcfbe9132016-10-14 15:58:56 +01007472 // If the class is abstract, we eagerly fetch the super class of the
7473 // object to avoid doing a comparison we know will fail.
7474 vixl32::Label loop;
7475 __ Bind(&loop);
7476 // /* HeapReference<Class> */ out = out->super_class_
Artem Serov657022c2016-11-23 14:19:38 +00007477 GenerateReferenceLoadOneRegister(instruction,
7478 out_loc,
7479 super_offset,
7480 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007481 read_barrier_option);
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007482 // If `out` is null, we use it for the result, and jump to the final label.
Anton Kirilov6f644202017-02-27 18:29:45 +00007483 __ CompareAndBranchIfZero(out, final_label, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007484 __ Cmp(out, cls);
Artem Serov517d9f62016-12-12 15:51:15 +00007485 __ B(ne, &loop, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007486 __ Mov(out, 1);
Artem Serovcfbe9132016-10-14 15:58:56 +01007487 break;
7488 }
7489
7490 case TypeCheckKind::kClassHierarchyCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00007491 ReadBarrierOption read_barrier_option =
7492 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier6beced42016-11-15 15:51:31 -08007493 // /* HeapReference<Class> */ out = obj->klass_
7494 GenerateReferenceLoadTwoRegisters(instruction,
7495 out_loc,
7496 obj_loc,
7497 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00007498 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007499 read_barrier_option);
Artem Serovcfbe9132016-10-14 15:58:56 +01007500 // Walk over the class hierarchy to find a match.
7501 vixl32::Label loop, success;
7502 __ Bind(&loop);
7503 __ Cmp(out, cls);
Artem Serov517d9f62016-12-12 15:51:15 +00007504 __ B(eq, &success, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007505 // /* HeapReference<Class> */ out = out->super_class_
Artem Serov657022c2016-11-23 14:19:38 +00007506 GenerateReferenceLoadOneRegister(instruction,
7507 out_loc,
7508 super_offset,
7509 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007510 read_barrier_option);
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007511 // This is essentially a null check, but it sets the condition flags to the
7512 // proper value for the code that follows the loop, i.e. not `eq`.
7513 __ Cmp(out, 1);
7514 __ B(hs, &loop, /* far_target */ false);
7515
7516 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
7517 // we check that the output is in a low register, so that a 16-bit MOV
7518 // encoding can be used.
7519 if (out.IsLow()) {
7520 // If `out` is null, we use it for the result, and the condition flags
7521 // have already been set to `ne`, so the IT block that comes afterwards
7522 // (and which handles the successful case) turns into a NOP (instead of
7523 // overwriting `out`).
7524 __ Bind(&success);
7525
7526 // We use the scope because of the IT block that follows.
7527 ExactAssemblyScope guard(GetVIXLAssembler(),
7528 2 * vixl32::k16BitT32InstructionSizeInBytes,
7529 CodeBufferCheckScope::kExactSize);
7530
7531 // There is only one branch to the `success` label (which is bound to this
7532 // IT block), and it has the same condition, `eq`, so in that case the MOV
7533 // is executed.
7534 __ it(eq);
7535 __ mov(eq, out, 1);
7536 } else {
7537 // If `out` is null, we use it for the result, and jump to the final label.
Anton Kirilov6f644202017-02-27 18:29:45 +00007538 __ B(final_label);
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007539 __ Bind(&success);
7540 __ Mov(out, 1);
Artem Serovcfbe9132016-10-14 15:58:56 +01007541 }
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007542
Artem Serovcfbe9132016-10-14 15:58:56 +01007543 break;
7544 }
7545
7546 case TypeCheckKind::kArrayObjectCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00007547 ReadBarrierOption read_barrier_option =
7548 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier6beced42016-11-15 15:51:31 -08007549 // /* HeapReference<Class> */ out = obj->klass_
7550 GenerateReferenceLoadTwoRegisters(instruction,
7551 out_loc,
7552 obj_loc,
7553 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00007554 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007555 read_barrier_option);
Artem Serovcfbe9132016-10-14 15:58:56 +01007556 // Do an exact check.
7557 vixl32::Label exact_check;
7558 __ Cmp(out, cls);
Artem Serov517d9f62016-12-12 15:51:15 +00007559 __ B(eq, &exact_check, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007560 // Otherwise, we need to check that the object's class is a non-primitive array.
7561 // /* HeapReference<Class> */ out = out->component_type_
Artem Serov657022c2016-11-23 14:19:38 +00007562 GenerateReferenceLoadOneRegister(instruction,
7563 out_loc,
7564 component_offset,
7565 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007566 read_barrier_option);
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007567 // If `out` is null, we use it for the result, and jump to the final label.
Anton Kirilov6f644202017-02-27 18:29:45 +00007568 __ CompareAndBranchIfZero(out, final_label, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007569 GetAssembler()->LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
7570 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007571 __ Cmp(out, 0);
7572 // We speculatively set the result to false without changing the condition
7573 // flags, which allows us to avoid some branching later.
7574 __ Mov(LeaveFlags, out, 0);
7575
7576 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
7577 // we check that the output is in a low register, so that a 16-bit MOV
7578 // encoding can be used.
7579 if (out.IsLow()) {
7580 __ Bind(&exact_check);
7581
7582 // We use the scope because of the IT block that follows.
7583 ExactAssemblyScope guard(GetVIXLAssembler(),
7584 2 * vixl32::k16BitT32InstructionSizeInBytes,
7585 CodeBufferCheckScope::kExactSize);
7586
7587 __ it(eq);
7588 __ mov(eq, out, 1);
7589 } else {
7590 __ B(ne, final_label, /* far_target */ false);
7591 __ Bind(&exact_check);
7592 __ Mov(out, 1);
7593 }
7594
Artem Serovcfbe9132016-10-14 15:58:56 +01007595 break;
7596 }
7597
7598 case TypeCheckKind::kArrayCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00007599 // No read barrier since the slow path will retry upon failure.
Mathieu Chartier6beced42016-11-15 15:51:31 -08007600 // /* HeapReference<Class> */ out = obj->klass_
7601 GenerateReferenceLoadTwoRegisters(instruction,
7602 out_loc,
7603 obj_loc,
7604 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00007605 maybe_temp_loc,
7606 kWithoutReadBarrier);
Artem Serovcfbe9132016-10-14 15:58:56 +01007607 __ Cmp(out, cls);
7608 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01007609 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathARMVIXL(
7610 instruction, /* is_fatal */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007611 codegen_->AddSlowPath(slow_path);
7612 __ B(ne, slow_path->GetEntryLabel());
7613 __ Mov(out, 1);
Artem Serovcfbe9132016-10-14 15:58:56 +01007614 break;
7615 }
7616
7617 case TypeCheckKind::kUnresolvedCheck:
7618 case TypeCheckKind::kInterfaceCheck: {
7619 // Note that we indeed only call on slow path, but we always go
7620 // into the slow path for the unresolved and interface check
7621 // cases.
7622 //
7623 // We cannot directly call the InstanceofNonTrivial runtime
7624 // entry point without resorting to a type checking slow path
7625 // here (i.e. by calling InvokeRuntime directly), as it would
7626 // require to assign fixed registers for the inputs of this
7627 // HInstanceOf instruction (following the runtime calling
7628 // convention), which might be cluttered by the potential first
7629 // read barrier emission at the beginning of this method.
7630 //
7631 // TODO: Introduce a new runtime entry point taking the object
7632 // to test (instead of its class) as argument, and let it deal
7633 // with the read barrier issues. This will let us refactor this
7634 // case of the `switch` code as it was previously (with a direct
7635 // call to the runtime not using a type checking slow path).
7636 // This should also be beneficial for the other cases above.
7637 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01007638 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathARMVIXL(
7639 instruction, /* is_fatal */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007640 codegen_->AddSlowPath(slow_path);
7641 __ B(slow_path->GetEntryLabel());
Artem Serovcfbe9132016-10-14 15:58:56 +01007642 break;
7643 }
7644 }
7645
Artem Serovcfbe9132016-10-14 15:58:56 +01007646 if (done.IsReferenced()) {
7647 __ Bind(&done);
7648 }
7649
7650 if (slow_path != nullptr) {
7651 __ Bind(slow_path->GetExitLabel());
7652 }
7653}
7654
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007655void LocationsBuilderARMVIXL::VisitCheckCast(HCheckCast* instruction) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007656 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko87584542017-12-12 17:47:52 +00007657 LocationSummary::CallKind call_kind = CodeGenerator::GetCheckCastCallKind(instruction);
Vladimir Markoca6fff82017-10-03 14:49:14 +01007658 LocationSummary* locations =
7659 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007660 locations->SetInAt(0, Location::RequiresRegister());
7661 locations->SetInAt(1, Location::RequiresRegister());
Artem Serov657022c2016-11-23 14:19:38 +00007662 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007663}
7664
7665void InstructionCodeGeneratorARMVIXL::VisitCheckCast(HCheckCast* instruction) {
7666 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
7667 LocationSummary* locations = instruction->GetLocations();
7668 Location obj_loc = locations->InAt(0);
7669 vixl32::Register obj = InputRegisterAt(instruction, 0);
7670 vixl32::Register cls = InputRegisterAt(instruction, 1);
7671 Location temp_loc = locations->GetTemp(0);
7672 vixl32::Register temp = RegisterFrom(temp_loc);
Artem Serov657022c2016-11-23 14:19:38 +00007673 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
7674 DCHECK_LE(num_temps, 3u);
7675 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
7676 Location maybe_temp3_loc = (num_temps >= 3) ? locations->GetTemp(2) : Location::NoLocation();
7677 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
7678 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
7679 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
7680 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
7681 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
7682 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
7683 const uint32_t object_array_data_offset =
7684 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007685
Vladimir Marko87584542017-12-12 17:47:52 +00007686 bool is_type_check_slow_path_fatal = CodeGenerator::IsTypeCheckSlowPathFatal(instruction);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007687 SlowPathCodeARMVIXL* type_check_slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01007688 new (codegen_->GetScopedAllocator()) TypeCheckSlowPathARMVIXL(
7689 instruction, is_type_check_slow_path_fatal);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007690 codegen_->AddSlowPath(type_check_slow_path);
7691
7692 vixl32::Label done;
Anton Kirilov6f644202017-02-27 18:29:45 +00007693 vixl32::Label* final_label = codegen_->GetFinalLabel(instruction, &done);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007694 // Avoid null check if we know obj is not null.
7695 if (instruction->MustDoNullCheck()) {
Anton Kirilov6f644202017-02-27 18:29:45 +00007696 __ CompareAndBranchIfZero(obj, final_label, /* far_target */ false);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007697 }
7698
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007699 switch (type_check_kind) {
7700 case TypeCheckKind::kExactCheck:
7701 case TypeCheckKind::kArrayCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00007702 // /* HeapReference<Class> */ temp = obj->klass_
7703 GenerateReferenceLoadTwoRegisters(instruction,
7704 temp_loc,
7705 obj_loc,
7706 class_offset,
7707 maybe_temp2_loc,
7708 kWithoutReadBarrier);
7709
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007710 __ Cmp(temp, cls);
7711 // Jump to slow path for throwing the exception or doing a
7712 // more involved array check.
7713 __ B(ne, type_check_slow_path->GetEntryLabel());
7714 break;
7715 }
7716
7717 case TypeCheckKind::kAbstractClassCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00007718 // /* HeapReference<Class> */ temp = obj->klass_
7719 GenerateReferenceLoadTwoRegisters(instruction,
7720 temp_loc,
7721 obj_loc,
7722 class_offset,
7723 maybe_temp2_loc,
7724 kWithoutReadBarrier);
7725
Artem Serovcfbe9132016-10-14 15:58:56 +01007726 // If the class is abstract, we eagerly fetch the super class of the
7727 // object to avoid doing a comparison we know will fail.
7728 vixl32::Label loop;
7729 __ Bind(&loop);
7730 // /* HeapReference<Class> */ temp = temp->super_class_
Artem Serov657022c2016-11-23 14:19:38 +00007731 GenerateReferenceLoadOneRegister(instruction,
7732 temp_loc,
7733 super_offset,
7734 maybe_temp2_loc,
7735 kWithoutReadBarrier);
Artem Serovcfbe9132016-10-14 15:58:56 +01007736
7737 // If the class reference currently in `temp` is null, jump to the slow path to throw the
7738 // exception.
xueliang.zhongf51bc622016-11-04 09:23:32 +00007739 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
Artem Serovcfbe9132016-10-14 15:58:56 +01007740
7741 // Otherwise, compare the classes.
7742 __ Cmp(temp, cls);
Artem Serov517d9f62016-12-12 15:51:15 +00007743 __ B(ne, &loop, /* far_target */ false);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007744 break;
7745 }
7746
7747 case TypeCheckKind::kClassHierarchyCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00007748 // /* HeapReference<Class> */ temp = obj->klass_
7749 GenerateReferenceLoadTwoRegisters(instruction,
7750 temp_loc,
7751 obj_loc,
7752 class_offset,
7753 maybe_temp2_loc,
7754 kWithoutReadBarrier);
7755
Artem Serovcfbe9132016-10-14 15:58:56 +01007756 // Walk over the class hierarchy to find a match.
7757 vixl32::Label loop;
7758 __ Bind(&loop);
7759 __ Cmp(temp, cls);
Anton Kirilov6f644202017-02-27 18:29:45 +00007760 __ B(eq, final_label, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007761
7762 // /* HeapReference<Class> */ temp = temp->super_class_
Artem Serov657022c2016-11-23 14:19:38 +00007763 GenerateReferenceLoadOneRegister(instruction,
7764 temp_loc,
7765 super_offset,
7766 maybe_temp2_loc,
7767 kWithoutReadBarrier);
Artem Serovcfbe9132016-10-14 15:58:56 +01007768
7769 // If the class reference currently in `temp` is null, jump to the slow path to throw the
7770 // exception.
xueliang.zhongf51bc622016-11-04 09:23:32 +00007771 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
Artem Serovcfbe9132016-10-14 15:58:56 +01007772 // Otherwise, jump to the beginning of the loop.
7773 __ B(&loop);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007774 break;
7775 }
7776
Artem Serovcfbe9132016-10-14 15:58:56 +01007777 case TypeCheckKind::kArrayObjectCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00007778 // /* HeapReference<Class> */ temp = obj->klass_
7779 GenerateReferenceLoadTwoRegisters(instruction,
7780 temp_loc,
7781 obj_loc,
7782 class_offset,
7783 maybe_temp2_loc,
7784 kWithoutReadBarrier);
7785
Artem Serovcfbe9132016-10-14 15:58:56 +01007786 // Do an exact check.
7787 __ Cmp(temp, cls);
Anton Kirilov6f644202017-02-27 18:29:45 +00007788 __ B(eq, final_label, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007789
7790 // Otherwise, we need to check that the object's class is a non-primitive array.
7791 // /* HeapReference<Class> */ temp = temp->component_type_
Artem Serov657022c2016-11-23 14:19:38 +00007792 GenerateReferenceLoadOneRegister(instruction,
7793 temp_loc,
7794 component_offset,
7795 maybe_temp2_loc,
7796 kWithoutReadBarrier);
Artem Serovcfbe9132016-10-14 15:58:56 +01007797 // If the component type is null, jump to the slow path to throw the exception.
xueliang.zhongf51bc622016-11-04 09:23:32 +00007798 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
Artem Serovcfbe9132016-10-14 15:58:56 +01007799 // Otherwise,the object is indeed an array, jump to label `check_non_primitive_component_type`
7800 // to further check that this component type is not a primitive type.
7801 GetAssembler()->LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007802 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
xueliang.zhongf51bc622016-11-04 09:23:32 +00007803 __ CompareAndBranchIfNonZero(temp, type_check_slow_path->GetEntryLabel());
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007804 break;
7805 }
7806
7807 case TypeCheckKind::kUnresolvedCheck:
Artem Serov657022c2016-11-23 14:19:38 +00007808 // We always go into the type check slow path for the unresolved check case.
Artem Serovcfbe9132016-10-14 15:58:56 +01007809 // We cannot directly call the CheckCast runtime entry point
7810 // without resorting to a type checking slow path here (i.e. by
7811 // calling InvokeRuntime directly), as it would require to
7812 // assign fixed registers for the inputs of this HInstanceOf
7813 // instruction (following the runtime calling convention), which
7814 // might be cluttered by the potential first read barrier
7815 // emission at the beginning of this method.
Artem Serov657022c2016-11-23 14:19:38 +00007816
Artem Serovcfbe9132016-10-14 15:58:56 +01007817 __ B(type_check_slow_path->GetEntryLabel());
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007818 break;
Artem Serov657022c2016-11-23 14:19:38 +00007819
7820 case TypeCheckKind::kInterfaceCheck: {
7821 // Avoid read barriers to improve performance of the fast path. We can not get false
7822 // positives by doing this.
7823 // /* HeapReference<Class> */ temp = obj->klass_
7824 GenerateReferenceLoadTwoRegisters(instruction,
7825 temp_loc,
7826 obj_loc,
7827 class_offset,
7828 maybe_temp2_loc,
7829 kWithoutReadBarrier);
7830
7831 // /* HeapReference<Class> */ temp = temp->iftable_
7832 GenerateReferenceLoadTwoRegisters(instruction,
7833 temp_loc,
7834 temp_loc,
7835 iftable_offset,
7836 maybe_temp2_loc,
7837 kWithoutReadBarrier);
7838 // Iftable is never null.
7839 __ Ldr(RegisterFrom(maybe_temp2_loc), MemOperand(temp, array_length_offset));
7840 // Loop through the iftable and check if any class matches.
7841 vixl32::Label start_loop;
7842 __ Bind(&start_loop);
7843 __ CompareAndBranchIfZero(RegisterFrom(maybe_temp2_loc),
7844 type_check_slow_path->GetEntryLabel());
7845 __ Ldr(RegisterFrom(maybe_temp3_loc), MemOperand(temp, object_array_data_offset));
7846 GetAssembler()->MaybeUnpoisonHeapReference(RegisterFrom(maybe_temp3_loc));
7847 // Go to next interface.
7848 __ Add(temp, temp, Operand::From(2 * kHeapReferenceSize));
7849 __ Sub(RegisterFrom(maybe_temp2_loc), RegisterFrom(maybe_temp2_loc), 2);
7850 // Compare the classes and continue the loop if they do not match.
7851 __ Cmp(cls, RegisterFrom(maybe_temp3_loc));
Artem Serov517d9f62016-12-12 15:51:15 +00007852 __ B(ne, &start_loop, /* far_target */ false);
Artem Serov657022c2016-11-23 14:19:38 +00007853 break;
7854 }
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007855 }
Anton Kirilov6f644202017-02-27 18:29:45 +00007856 if (done.IsReferenced()) {
7857 __ Bind(&done);
7858 }
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007859
7860 __ Bind(type_check_slow_path->GetExitLabel());
7861}
7862
Artem Serov551b28f2016-10-18 19:11:30 +01007863void LocationsBuilderARMVIXL::VisitMonitorOperation(HMonitorOperation* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007864 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
7865 instruction, LocationSummary::kCallOnMainOnly);
Artem Serov551b28f2016-10-18 19:11:30 +01007866 InvokeRuntimeCallingConventionARMVIXL calling_convention;
7867 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
7868}
7869
7870void InstructionCodeGeneratorARMVIXL::VisitMonitorOperation(HMonitorOperation* instruction) {
7871 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
7872 instruction,
7873 instruction->GetDexPc());
7874 if (instruction->IsEnter()) {
7875 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
7876 } else {
7877 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
7878 }
Roland Levillain5daa4952017-07-03 17:23:56 +01007879 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 17);
Artem Serov551b28f2016-10-18 19:11:30 +01007880}
7881
Artem Serov02109dd2016-09-23 17:17:54 +01007882void LocationsBuilderARMVIXL::VisitAnd(HAnd* instruction) {
7883 HandleBitwiseOperation(instruction, AND);
7884}
7885
7886void LocationsBuilderARMVIXL::VisitOr(HOr* instruction) {
7887 HandleBitwiseOperation(instruction, ORR);
7888}
7889
7890void LocationsBuilderARMVIXL::VisitXor(HXor* instruction) {
7891 HandleBitwiseOperation(instruction, EOR);
7892}
7893
7894void LocationsBuilderARMVIXL::HandleBitwiseOperation(HBinaryOperation* instruction, Opcode opcode) {
7895 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007896 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007897 DCHECK(instruction->GetResultType() == DataType::Type::kInt32
7898 || instruction->GetResultType() == DataType::Type::kInt64);
Artem Serov02109dd2016-09-23 17:17:54 +01007899 // Note: GVN reorders commutative operations to have the constant on the right hand side.
7900 locations->SetInAt(0, Location::RequiresRegister());
7901 locations->SetInAt(1, ArmEncodableConstantOrRegister(instruction->InputAt(1), opcode));
7902 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
7903}
7904
7905void InstructionCodeGeneratorARMVIXL::VisitAnd(HAnd* instruction) {
7906 HandleBitwiseOperation(instruction);
7907}
7908
7909void InstructionCodeGeneratorARMVIXL::VisitOr(HOr* instruction) {
7910 HandleBitwiseOperation(instruction);
7911}
7912
7913void InstructionCodeGeneratorARMVIXL::VisitXor(HXor* instruction) {
7914 HandleBitwiseOperation(instruction);
7915}
7916
Artem Serov2bbc9532016-10-21 11:51:50 +01007917void LocationsBuilderARMVIXL::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
7918 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007919 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007920 DCHECK(instruction->GetResultType() == DataType::Type::kInt32
7921 || instruction->GetResultType() == DataType::Type::kInt64);
Artem Serov2bbc9532016-10-21 11:51:50 +01007922
7923 locations->SetInAt(0, Location::RequiresRegister());
7924 locations->SetInAt(1, Location::RequiresRegister());
7925 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
7926}
7927
7928void InstructionCodeGeneratorARMVIXL::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
7929 LocationSummary* locations = instruction->GetLocations();
7930 Location first = locations->InAt(0);
7931 Location second = locations->InAt(1);
7932 Location out = locations->Out();
7933
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007934 if (instruction->GetResultType() == DataType::Type::kInt32) {
Artem Serov2bbc9532016-10-21 11:51:50 +01007935 vixl32::Register first_reg = RegisterFrom(first);
7936 vixl32::Register second_reg = RegisterFrom(second);
7937 vixl32::Register out_reg = RegisterFrom(out);
7938
7939 switch (instruction->GetOpKind()) {
7940 case HInstruction::kAnd:
7941 __ Bic(out_reg, first_reg, second_reg);
7942 break;
7943 case HInstruction::kOr:
7944 __ Orn(out_reg, first_reg, second_reg);
7945 break;
7946 // There is no EON on arm.
7947 case HInstruction::kXor:
7948 default:
7949 LOG(FATAL) << "Unexpected instruction " << instruction->DebugName();
7950 UNREACHABLE();
7951 }
7952 return;
7953
7954 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007955 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
Artem Serov2bbc9532016-10-21 11:51:50 +01007956 vixl32::Register first_low = LowRegisterFrom(first);
7957 vixl32::Register first_high = HighRegisterFrom(first);
7958 vixl32::Register second_low = LowRegisterFrom(second);
7959 vixl32::Register second_high = HighRegisterFrom(second);
7960 vixl32::Register out_low = LowRegisterFrom(out);
7961 vixl32::Register out_high = HighRegisterFrom(out);
7962
7963 switch (instruction->GetOpKind()) {
7964 case HInstruction::kAnd:
7965 __ Bic(out_low, first_low, second_low);
7966 __ Bic(out_high, first_high, second_high);
7967 break;
7968 case HInstruction::kOr:
7969 __ Orn(out_low, first_low, second_low);
7970 __ Orn(out_high, first_high, second_high);
7971 break;
7972 // There is no EON on arm.
7973 case HInstruction::kXor:
7974 default:
7975 LOG(FATAL) << "Unexpected instruction " << instruction->DebugName();
7976 UNREACHABLE();
7977 }
7978 }
7979}
7980
Anton Kirilov74234da2017-01-13 14:42:47 +00007981void LocationsBuilderARMVIXL::VisitDataProcWithShifterOp(
7982 HDataProcWithShifterOp* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007983 DCHECK(instruction->GetType() == DataType::Type::kInt32 ||
7984 instruction->GetType() == DataType::Type::kInt64);
Anton Kirilov74234da2017-01-13 14:42:47 +00007985 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007986 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007987 const bool overlap = instruction->GetType() == DataType::Type::kInt64 &&
Anton Kirilov74234da2017-01-13 14:42:47 +00007988 HDataProcWithShifterOp::IsExtensionOp(instruction->GetOpKind());
7989
7990 locations->SetInAt(0, Location::RequiresRegister());
7991 locations->SetInAt(1, Location::RequiresRegister());
7992 locations->SetOut(Location::RequiresRegister(),
7993 overlap ? Location::kOutputOverlap : Location::kNoOutputOverlap);
7994}
7995
7996void InstructionCodeGeneratorARMVIXL::VisitDataProcWithShifterOp(
7997 HDataProcWithShifterOp* instruction) {
7998 const LocationSummary* const locations = instruction->GetLocations();
7999 const HInstruction::InstructionKind kind = instruction->GetInstrKind();
8000 const HDataProcWithShifterOp::OpKind op_kind = instruction->GetOpKind();
8001
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008002 if (instruction->GetType() == DataType::Type::kInt32) {
Anton Kirilov420ee302017-02-21 18:10:26 +00008003 const vixl32::Register first = InputRegisterAt(instruction, 0);
8004 const vixl32::Register output = OutputRegister(instruction);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008005 const vixl32::Register second = instruction->InputAt(1)->GetType() == DataType::Type::kInt64
Anton Kirilov74234da2017-01-13 14:42:47 +00008006 ? LowRegisterFrom(locations->InAt(1))
8007 : InputRegisterAt(instruction, 1);
8008
Anton Kirilov420ee302017-02-21 18:10:26 +00008009 if (HDataProcWithShifterOp::IsExtensionOp(op_kind)) {
8010 DCHECK_EQ(kind, HInstruction::kAdd);
8011
8012 switch (op_kind) {
8013 case HDataProcWithShifterOp::kUXTB:
8014 __ Uxtab(output, first, second);
8015 break;
8016 case HDataProcWithShifterOp::kUXTH:
8017 __ Uxtah(output, first, second);
8018 break;
8019 case HDataProcWithShifterOp::kSXTB:
8020 __ Sxtab(output, first, second);
8021 break;
8022 case HDataProcWithShifterOp::kSXTH:
8023 __ Sxtah(output, first, second);
8024 break;
8025 default:
8026 LOG(FATAL) << "Unexpected operation kind: " << op_kind;
8027 UNREACHABLE();
8028 }
8029 } else {
8030 GenerateDataProcInstruction(kind,
8031 output,
8032 first,
8033 Operand(second,
8034 ShiftFromOpKind(op_kind),
8035 instruction->GetShiftAmount()),
8036 codegen_);
8037 }
Anton Kirilov74234da2017-01-13 14:42:47 +00008038 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008039 DCHECK_EQ(instruction->GetType(), DataType::Type::kInt64);
Anton Kirilov74234da2017-01-13 14:42:47 +00008040
8041 if (HDataProcWithShifterOp::IsExtensionOp(op_kind)) {
8042 const vixl32::Register second = InputRegisterAt(instruction, 1);
8043
8044 DCHECK(!LowRegisterFrom(locations->Out()).Is(second));
8045 GenerateDataProc(kind,
8046 locations->Out(),
8047 locations->InAt(0),
8048 second,
8049 Operand(second, ShiftType::ASR, 31),
8050 codegen_);
8051 } else {
8052 GenerateLongDataProc(instruction, codegen_);
8053 }
8054 }
8055}
8056
Artem Serov02109dd2016-09-23 17:17:54 +01008057// TODO(VIXL): Remove optimizations in the helper when they are implemented in vixl.
8058void InstructionCodeGeneratorARMVIXL::GenerateAndConst(vixl32::Register out,
8059 vixl32::Register first,
8060 uint32_t value) {
8061 // Optimize special cases for individual halfs of `and-long` (`and` is simplified earlier).
8062 if (value == 0xffffffffu) {
8063 if (!out.Is(first)) {
8064 __ Mov(out, first);
8065 }
8066 return;
8067 }
8068 if (value == 0u) {
8069 __ Mov(out, 0);
8070 return;
8071 }
8072 if (GetAssembler()->ShifterOperandCanHold(AND, value)) {
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00008073 __ And(out, first, value);
8074 } else if (GetAssembler()->ShifterOperandCanHold(BIC, ~value)) {
8075 __ Bic(out, first, ~value);
Artem Serov02109dd2016-09-23 17:17:54 +01008076 } else {
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00008077 DCHECK(IsPowerOfTwo(value + 1));
8078 __ Ubfx(out, first, 0, WhichPowerOf2(value + 1));
Artem Serov02109dd2016-09-23 17:17:54 +01008079 }
8080}
8081
8082// TODO(VIXL): Remove optimizations in the helper when they are implemented in vixl.
8083void InstructionCodeGeneratorARMVIXL::GenerateOrrConst(vixl32::Register out,
8084 vixl32::Register first,
8085 uint32_t value) {
8086 // Optimize special cases for individual halfs of `or-long` (`or` is simplified earlier).
8087 if (value == 0u) {
8088 if (!out.Is(first)) {
8089 __ Mov(out, first);
8090 }
8091 return;
8092 }
8093 if (value == 0xffffffffu) {
8094 __ Mvn(out, 0);
8095 return;
8096 }
8097 if (GetAssembler()->ShifterOperandCanHold(ORR, value)) {
8098 __ Orr(out, first, value);
8099 } else {
8100 DCHECK(GetAssembler()->ShifterOperandCanHold(ORN, ~value));
8101 __ Orn(out, first, ~value);
8102 }
8103}
8104
8105// TODO(VIXL): Remove optimizations in the helper when they are implemented in vixl.
8106void InstructionCodeGeneratorARMVIXL::GenerateEorConst(vixl32::Register out,
8107 vixl32::Register first,
8108 uint32_t value) {
8109 // Optimize special case for individual halfs of `xor-long` (`xor` is simplified earlier).
8110 if (value == 0u) {
8111 if (!out.Is(first)) {
8112 __ Mov(out, first);
8113 }
8114 return;
8115 }
8116 __ Eor(out, first, value);
8117}
8118
Anton Kirilovdda43962016-11-21 19:55:20 +00008119void InstructionCodeGeneratorARMVIXL::GenerateAddLongConst(Location out,
8120 Location first,
8121 uint64_t value) {
8122 vixl32::Register out_low = LowRegisterFrom(out);
8123 vixl32::Register out_high = HighRegisterFrom(out);
8124 vixl32::Register first_low = LowRegisterFrom(first);
8125 vixl32::Register first_high = HighRegisterFrom(first);
8126 uint32_t value_low = Low32Bits(value);
8127 uint32_t value_high = High32Bits(value);
8128 if (value_low == 0u) {
8129 if (!out_low.Is(first_low)) {
8130 __ Mov(out_low, first_low);
8131 }
8132 __ Add(out_high, first_high, value_high);
8133 return;
8134 }
8135 __ Adds(out_low, first_low, value_low);
Vladimir Markof0a6a1d2018-01-08 14:23:56 +00008136 if (GetAssembler()->ShifterOperandCanHold(ADC, value_high)) {
Anton Kirilovdda43962016-11-21 19:55:20 +00008137 __ Adc(out_high, first_high, value_high);
Anton Kirilovdda43962016-11-21 19:55:20 +00008138 } else {
Vladimir Markof0a6a1d2018-01-08 14:23:56 +00008139 DCHECK(GetAssembler()->ShifterOperandCanHold(SBC, ~value_high));
8140 __ Sbc(out_high, first_high, ~value_high);
Anton Kirilovdda43962016-11-21 19:55:20 +00008141 }
8142}
8143
Artem Serov02109dd2016-09-23 17:17:54 +01008144void InstructionCodeGeneratorARMVIXL::HandleBitwiseOperation(HBinaryOperation* instruction) {
8145 LocationSummary* locations = instruction->GetLocations();
8146 Location first = locations->InAt(0);
8147 Location second = locations->InAt(1);
8148 Location out = locations->Out();
8149
8150 if (second.IsConstant()) {
8151 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
8152 uint32_t value_low = Low32Bits(value);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008153 if (instruction->GetResultType() == DataType::Type::kInt32) {
Artem Serov02109dd2016-09-23 17:17:54 +01008154 vixl32::Register first_reg = InputRegisterAt(instruction, 0);
8155 vixl32::Register out_reg = OutputRegister(instruction);
8156 if (instruction->IsAnd()) {
8157 GenerateAndConst(out_reg, first_reg, value_low);
8158 } else if (instruction->IsOr()) {
8159 GenerateOrrConst(out_reg, first_reg, value_low);
8160 } else {
8161 DCHECK(instruction->IsXor());
8162 GenerateEorConst(out_reg, first_reg, value_low);
8163 }
8164 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008165 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
Artem Serov02109dd2016-09-23 17:17:54 +01008166 uint32_t value_high = High32Bits(value);
8167 vixl32::Register first_low = LowRegisterFrom(first);
8168 vixl32::Register first_high = HighRegisterFrom(first);
8169 vixl32::Register out_low = LowRegisterFrom(out);
8170 vixl32::Register out_high = HighRegisterFrom(out);
8171 if (instruction->IsAnd()) {
8172 GenerateAndConst(out_low, first_low, value_low);
8173 GenerateAndConst(out_high, first_high, value_high);
8174 } else if (instruction->IsOr()) {
8175 GenerateOrrConst(out_low, first_low, value_low);
8176 GenerateOrrConst(out_high, first_high, value_high);
8177 } else {
8178 DCHECK(instruction->IsXor());
8179 GenerateEorConst(out_low, first_low, value_low);
8180 GenerateEorConst(out_high, first_high, value_high);
8181 }
8182 }
8183 return;
8184 }
8185
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008186 if (instruction->GetResultType() == DataType::Type::kInt32) {
Artem Serov02109dd2016-09-23 17:17:54 +01008187 vixl32::Register first_reg = InputRegisterAt(instruction, 0);
8188 vixl32::Register second_reg = InputRegisterAt(instruction, 1);
8189 vixl32::Register out_reg = OutputRegister(instruction);
8190 if (instruction->IsAnd()) {
8191 __ And(out_reg, first_reg, second_reg);
8192 } else if (instruction->IsOr()) {
8193 __ Orr(out_reg, first_reg, second_reg);
8194 } else {
8195 DCHECK(instruction->IsXor());
8196 __ Eor(out_reg, first_reg, second_reg);
8197 }
8198 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008199 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
Artem Serov02109dd2016-09-23 17:17:54 +01008200 vixl32::Register first_low = LowRegisterFrom(first);
8201 vixl32::Register first_high = HighRegisterFrom(first);
8202 vixl32::Register second_low = LowRegisterFrom(second);
8203 vixl32::Register second_high = HighRegisterFrom(second);
8204 vixl32::Register out_low = LowRegisterFrom(out);
8205 vixl32::Register out_high = HighRegisterFrom(out);
8206 if (instruction->IsAnd()) {
8207 __ And(out_low, first_low, second_low);
8208 __ And(out_high, first_high, second_high);
8209 } else if (instruction->IsOr()) {
8210 __ Orr(out_low, first_low, second_low);
8211 __ Orr(out_high, first_high, second_high);
8212 } else {
8213 DCHECK(instruction->IsXor());
8214 __ Eor(out_low, first_low, second_low);
8215 __ Eor(out_high, first_high, second_high);
8216 }
8217 }
8218}
8219
Artem Serovcfbe9132016-10-14 15:58:56 +01008220void InstructionCodeGeneratorARMVIXL::GenerateReferenceLoadOneRegister(
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008221 HInstruction* instruction,
Artem Serovcfbe9132016-10-14 15:58:56 +01008222 Location out,
8223 uint32_t offset,
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008224 Location maybe_temp,
8225 ReadBarrierOption read_barrier_option) {
Artem Serovcfbe9132016-10-14 15:58:56 +01008226 vixl32::Register out_reg = RegisterFrom(out);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008227 if (read_barrier_option == kWithReadBarrier) {
8228 CHECK(kEmitCompilerReadBarrier);
8229 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
8230 if (kUseBakerReadBarrier) {
8231 // Load with fast path based Baker's read barrier.
8232 // /* HeapReference<Object> */ out = *(out + offset)
8233 codegen_->GenerateFieldLoadWithBakerReadBarrier(
8234 instruction, out, out_reg, offset, maybe_temp, /* needs_null_check */ false);
8235 } else {
8236 // Load with slow path based read barrier.
8237 // Save the value of `out` into `maybe_temp` before overwriting it
8238 // in the following move operation, as we will need it for the
8239 // read barrier below.
8240 __ Mov(RegisterFrom(maybe_temp), out_reg);
8241 // /* HeapReference<Object> */ out = *(out + offset)
8242 GetAssembler()->LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
8243 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
8244 }
Artem Serovcfbe9132016-10-14 15:58:56 +01008245 } else {
8246 // Plain load with no read barrier.
8247 // /* HeapReference<Object> */ out = *(out + offset)
8248 GetAssembler()->LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
8249 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
8250 }
8251}
8252
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008253void InstructionCodeGeneratorARMVIXL::GenerateReferenceLoadTwoRegisters(
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008254 HInstruction* instruction,
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008255 Location out,
8256 Location obj,
8257 uint32_t offset,
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008258 Location maybe_temp,
8259 ReadBarrierOption read_barrier_option) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008260 vixl32::Register out_reg = RegisterFrom(out);
8261 vixl32::Register obj_reg = RegisterFrom(obj);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008262 if (read_barrier_option == kWithReadBarrier) {
8263 CHECK(kEmitCompilerReadBarrier);
8264 if (kUseBakerReadBarrier) {
8265 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
8266 // Load with fast path based Baker's read barrier.
8267 // /* HeapReference<Object> */ out = *(obj + offset)
8268 codegen_->GenerateFieldLoadWithBakerReadBarrier(
8269 instruction, out, obj_reg, offset, maybe_temp, /* needs_null_check */ false);
8270 } else {
8271 // Load with slow path based read barrier.
8272 // /* HeapReference<Object> */ out = *(obj + offset)
8273 GetAssembler()->LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
8274 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
8275 }
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008276 } else {
8277 // Plain load with no read barrier.
8278 // /* HeapReference<Object> */ out = *(obj + offset)
8279 GetAssembler()->LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
8280 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
8281 }
8282}
8283
Scott Wakelinga7812ae2016-10-17 10:03:36 +01008284void InstructionCodeGeneratorARMVIXL::GenerateGcRootFieldLoad(
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008285 HInstruction* instruction,
Scott Wakelinga7812ae2016-10-17 10:03:36 +01008286 Location root,
8287 vixl32::Register obj,
8288 uint32_t offset,
Artem Serovd4cc5b22016-11-04 11:19:09 +00008289 ReadBarrierOption read_barrier_option) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01008290 vixl32::Register root_reg = RegisterFrom(root);
Artem Serovd4cc5b22016-11-04 11:19:09 +00008291 if (read_barrier_option == kWithReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008292 DCHECK(kEmitCompilerReadBarrier);
8293 if (kUseBakerReadBarrier) {
8294 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
Roland Levillainba650a42017-03-06 13:52:32 +00008295 // Baker's read barrier are used.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008296 if (kBakerReadBarrierLinkTimeThunksEnableForGcRoots &&
8297 !Runtime::Current()->UseJitCompilation()) {
Roland Levillain6d729a72017-06-30 18:34:01 +01008298 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in
8299 // the Marking Register) to decide whether we need to enter
8300 // the slow path to mark the GC root.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008301 //
8302 // We use link-time generated thunks for the slow path. That thunk
8303 // checks the reference and jumps to the entrypoint if needed.
8304 //
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008305 // lr = &return_address;
8306 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
Roland Levillain6d729a72017-06-30 18:34:01 +01008307 // if (mr) { // Thread::Current()->GetIsGcMarking()
8308 // goto gc_root_thunk<root_reg>(lr)
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008309 // }
8310 // return_address:
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008311
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008312 UseScratchRegisterScope temps(GetVIXLAssembler());
8313 ExcludeIPAndBakerCcEntrypointRegister(&temps, instruction);
Vladimir Marko88abba22017-05-03 17:09:25 +01008314 bool narrow = CanEmitNarrowLdr(root_reg, obj, offset);
8315 uint32_t custom_data = linker::Thumb2RelativePatcher::EncodeBakerReadBarrierGcRootData(
8316 root_reg.GetCode(), narrow);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008317 vixl32::Label* bne_label = codegen_->NewBakerReadBarrierPatch(custom_data);
Roland Levillainba650a42017-03-06 13:52:32 +00008318
Roland Levillain6d729a72017-06-30 18:34:01 +01008319 vixl::EmissionCheckScope guard(GetVIXLAssembler(), 4 * vixl32::kMaxInstructionSizeInBytes);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008320 vixl32::Label return_address;
8321 EmitAdrCode adr(GetVIXLAssembler(), lr, &return_address);
Roland Levillain6d729a72017-06-30 18:34:01 +01008322 __ cmp(mr, Operand(0));
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008323 // Currently the offset is always within range. If that changes,
8324 // we shall have to split the load the same way as for fields.
8325 DCHECK_LT(offset, kReferenceLoadMinFarOffset);
Vladimir Marko88abba22017-05-03 17:09:25 +01008326 ptrdiff_t old_offset = GetVIXLAssembler()->GetBuffer()->GetCursorOffset();
8327 __ ldr(EncodingSize(narrow ? Narrow : Wide), root_reg, MemOperand(obj, offset));
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008328 EmitPlaceholderBne(codegen_, bne_label);
8329 __ Bind(&return_address);
Vladimir Marko88abba22017-05-03 17:09:25 +01008330 DCHECK_EQ(old_offset - GetVIXLAssembler()->GetBuffer()->GetCursorOffset(),
8331 narrow ? BAKER_MARK_INTROSPECTION_GC_ROOT_LDR_NARROW_OFFSET
8332 : BAKER_MARK_INTROSPECTION_GC_ROOT_LDR_WIDE_OFFSET);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008333 } else {
Roland Levillain6d729a72017-06-30 18:34:01 +01008334 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in
8335 // the Marking Register) to decide whether we need to enter
8336 // the slow path to mark the GC root.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008337 //
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008338 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
Roland Levillain6d729a72017-06-30 18:34:01 +01008339 // if (mr) { // Thread::Current()->GetIsGcMarking()
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008340 // // Slow path.
Roland Levillain6d729a72017-06-30 18:34:01 +01008341 // entrypoint = Thread::Current()->pReadBarrierMarkReg ## root.reg()
8342 // root = entrypoint(root); // root = ReadBarrier::Mark(root); // Entry point call.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008343 // }
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008344
Roland Levillain6d729a72017-06-30 18:34:01 +01008345 // Slow path marking the GC root `root`. The entrypoint will
8346 // be loaded by the slow path code.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008347 SlowPathCodeARMVIXL* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01008348 new (codegen_->GetScopedAllocator()) ReadBarrierMarkSlowPathARMVIXL(instruction, root);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008349 codegen_->AddSlowPath(slow_path);
8350
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008351 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
8352 GetAssembler()->LoadFromOffset(kLoadWord, root_reg, obj, offset);
8353 static_assert(
8354 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
8355 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
8356 "have different sizes.");
8357 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
8358 "art::mirror::CompressedReference<mirror::Object> and int32_t "
8359 "have different sizes.");
8360
Roland Levillain6d729a72017-06-30 18:34:01 +01008361 __ CompareAndBranchIfNonZero(mr, slow_path->GetEntryLabel());
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008362 __ Bind(slow_path->GetExitLabel());
8363 }
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008364 } else {
8365 // GC root loaded through a slow path for read barriers other
8366 // than Baker's.
8367 // /* GcRoot<mirror::Object>* */ root = obj + offset
8368 __ Add(root_reg, obj, offset);
8369 // /* mirror::Object* */ root = root->Read()
8370 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
8371 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01008372 } else {
8373 // Plain GC root load with no read barrier.
8374 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
8375 GetAssembler()->LoadFromOffset(kLoadWord, root_reg, obj, offset);
8376 // Note that GC roots are not affected by heap poisoning, thus we
8377 // do not have to unpoison `root_reg` here.
8378 }
Roland Levillain5daa4952017-07-03 17:23:56 +01008379 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 18);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01008380}
8381
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008382void CodeGeneratorARMVIXL::MaybeAddBakerCcEntrypointTempForFields(LocationSummary* locations) {
8383 DCHECK(kEmitCompilerReadBarrier);
8384 DCHECK(kUseBakerReadBarrier);
8385 if (kBakerReadBarrierLinkTimeThunksEnableForFields) {
8386 if (!Runtime::Current()->UseJitCompilation()) {
8387 locations->AddTemp(Location::RegisterLocation(kBakerCcEntrypointRegister.GetCode()));
8388 }
8389 }
8390}
8391
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008392void CodeGeneratorARMVIXL::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
8393 Location ref,
8394 vixl32::Register obj,
8395 uint32_t offset,
8396 Location temp,
8397 bool needs_null_check) {
8398 DCHECK(kEmitCompilerReadBarrier);
8399 DCHECK(kUseBakerReadBarrier);
8400
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008401 if (kBakerReadBarrierLinkTimeThunksEnableForFields &&
8402 !Runtime::Current()->UseJitCompilation()) {
Roland Levillain6d729a72017-06-30 18:34:01 +01008403 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in the
8404 // Marking Register) to decide whether we need to enter the slow
8405 // path to mark the reference. Then, in the slow path, check the
8406 // gray bit in the lock word of the reference's holder (`obj`) to
8407 // decide whether to mark `ref` or not.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008408 //
8409 // We use link-time generated thunks for the slow path. That thunk checks
8410 // the holder and jumps to the entrypoint if needed. If the holder is not
8411 // gray, it creates a fake dependency and returns to the LDR instruction.
8412 //
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008413 // lr = &gray_return_address;
Roland Levillain6d729a72017-06-30 18:34:01 +01008414 // if (mr) { // Thread::Current()->GetIsGcMarking()
8415 // goto field_thunk<holder_reg, base_reg>(lr)
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008416 // }
8417 // not_gray_return_address:
8418 // // Original reference load. If the offset is too large to fit
8419 // // into LDR, we use an adjusted base register here.
Vladimir Marko88abba22017-05-03 17:09:25 +01008420 // HeapReference<mirror::Object> reference = *(obj+offset);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008421 // gray_return_address:
8422
8423 DCHECK_ALIGNED(offset, sizeof(mirror::HeapReference<mirror::Object>));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008424 vixl32::Register ref_reg = RegisterFrom(ref, DataType::Type::kReference);
Vladimir Marko88abba22017-05-03 17:09:25 +01008425 bool narrow = CanEmitNarrowLdr(ref_reg, obj, offset);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008426 vixl32::Register base = obj;
8427 if (offset >= kReferenceLoadMinFarOffset) {
8428 base = RegisterFrom(temp);
8429 DCHECK(!base.Is(kBakerCcEntrypointRegister));
8430 static_assert(IsPowerOfTwo(kReferenceLoadMinFarOffset), "Expecting a power of 2.");
8431 __ Add(base, obj, Operand(offset & ~(kReferenceLoadMinFarOffset - 1u)));
8432 offset &= (kReferenceLoadMinFarOffset - 1u);
Vladimir Marko88abba22017-05-03 17:09:25 +01008433 // Use narrow LDR only for small offsets. Generating narrow encoding LDR for the large
8434 // offsets with `(offset & (kReferenceLoadMinFarOffset - 1u)) < 32u` would most likely
8435 // increase the overall code size when taking the generated thunks into account.
8436 DCHECK(!narrow);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008437 }
8438 UseScratchRegisterScope temps(GetVIXLAssembler());
8439 ExcludeIPAndBakerCcEntrypointRegister(&temps, instruction);
8440 uint32_t custom_data = linker::Thumb2RelativePatcher::EncodeBakerReadBarrierFieldData(
Vladimir Marko88abba22017-05-03 17:09:25 +01008441 base.GetCode(), obj.GetCode(), narrow);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008442 vixl32::Label* bne_label = NewBakerReadBarrierPatch(custom_data);
8443
Roland Levillain5daa4952017-07-03 17:23:56 +01008444 {
8445 vixl::EmissionCheckScope guard(
8446 GetVIXLAssembler(),
8447 (kPoisonHeapReferences ? 5u : 4u) * vixl32::kMaxInstructionSizeInBytes);
8448 vixl32::Label return_address;
8449 EmitAdrCode adr(GetVIXLAssembler(), lr, &return_address);
8450 __ cmp(mr, Operand(0));
8451 EmitPlaceholderBne(this, bne_label);
8452 ptrdiff_t old_offset = GetVIXLAssembler()->GetBuffer()->GetCursorOffset();
8453 __ ldr(EncodingSize(narrow ? Narrow : Wide), ref_reg, MemOperand(base, offset));
8454 if (needs_null_check) {
8455 MaybeRecordImplicitNullCheck(instruction);
Vladimir Marko88abba22017-05-03 17:09:25 +01008456 }
Roland Levillain5daa4952017-07-03 17:23:56 +01008457 // Note: We need a specific width for the unpoisoning NEG.
8458 if (kPoisonHeapReferences) {
8459 if (narrow) {
8460 // The only 16-bit encoding is T1 which sets flags outside IT block (i.e. RSBS, not RSB).
8461 __ rsbs(EncodingSize(Narrow), ref_reg, ref_reg, Operand(0));
8462 } else {
8463 __ rsb(EncodingSize(Wide), ref_reg, ref_reg, Operand(0));
8464 }
8465 }
8466 __ Bind(&return_address);
8467 DCHECK_EQ(old_offset - GetVIXLAssembler()->GetBuffer()->GetCursorOffset(),
8468 narrow ? BAKER_MARK_INTROSPECTION_FIELD_LDR_NARROW_OFFSET
8469 : BAKER_MARK_INTROSPECTION_FIELD_LDR_WIDE_OFFSET);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008470 }
Roland Levillain5daa4952017-07-03 17:23:56 +01008471 MaybeGenerateMarkingRegisterCheck(/* code */ 19, /* temp_loc */ LocationFrom(ip));
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008472 return;
8473 }
8474
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008475 // /* HeapReference<Object> */ ref = *(obj + offset)
8476 Location no_index = Location::NoLocation();
8477 ScaleFactor no_scale_factor = TIMES_1;
8478 GenerateReferenceLoadWithBakerReadBarrier(
8479 instruction, ref, obj, offset, no_index, no_scale_factor, temp, needs_null_check);
Roland Levillain6070e882016-11-03 17:51:58 +00008480}
8481
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008482void CodeGeneratorARMVIXL::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
8483 Location ref,
8484 vixl32::Register obj,
8485 uint32_t data_offset,
8486 Location index,
8487 Location temp,
8488 bool needs_null_check) {
8489 DCHECK(kEmitCompilerReadBarrier);
8490 DCHECK(kUseBakerReadBarrier);
8491
8492 static_assert(
8493 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
8494 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008495 ScaleFactor scale_factor = TIMES_4;
8496
8497 if (kBakerReadBarrierLinkTimeThunksEnableForArrays &&
8498 !Runtime::Current()->UseJitCompilation()) {
Roland Levillain6d729a72017-06-30 18:34:01 +01008499 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in the
8500 // Marking Register) to decide whether we need to enter the slow
8501 // path to mark the reference. Then, in the slow path, check the
8502 // gray bit in the lock word of the reference's holder (`obj`) to
8503 // decide whether to mark `ref` or not.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008504 //
8505 // We use link-time generated thunks for the slow path. That thunk checks
8506 // the holder and jumps to the entrypoint if needed. If the holder is not
8507 // gray, it creates a fake dependency and returns to the LDR instruction.
8508 //
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008509 // lr = &gray_return_address;
Roland Levillain6d729a72017-06-30 18:34:01 +01008510 // if (mr) { // Thread::Current()->GetIsGcMarking()
8511 // goto array_thunk<base_reg>(lr)
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008512 // }
8513 // not_gray_return_address:
8514 // // Original reference load. If the offset is too large to fit
8515 // // into LDR, we use an adjusted base register here.
Vladimir Marko88abba22017-05-03 17:09:25 +01008516 // HeapReference<mirror::Object> reference = data[index];
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008517 // gray_return_address:
8518
8519 DCHECK(index.IsValid());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008520 vixl32::Register index_reg = RegisterFrom(index, DataType::Type::kInt32);
8521 vixl32::Register ref_reg = RegisterFrom(ref, DataType::Type::kReference);
8522 vixl32::Register data_reg = RegisterFrom(temp, DataType::Type::kInt32); // Raw pointer.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008523 DCHECK(!data_reg.Is(kBakerCcEntrypointRegister));
8524
8525 UseScratchRegisterScope temps(GetVIXLAssembler());
8526 ExcludeIPAndBakerCcEntrypointRegister(&temps, instruction);
8527 uint32_t custom_data =
8528 linker::Thumb2RelativePatcher::EncodeBakerReadBarrierArrayData(data_reg.GetCode());
8529 vixl32::Label* bne_label = NewBakerReadBarrierPatch(custom_data);
8530
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008531 __ Add(data_reg, obj, Operand(data_offset));
Roland Levillain5daa4952017-07-03 17:23:56 +01008532 {
8533 vixl::EmissionCheckScope guard(
8534 GetVIXLAssembler(),
8535 (kPoisonHeapReferences ? 5u : 4u) * vixl32::kMaxInstructionSizeInBytes);
8536 vixl32::Label return_address;
8537 EmitAdrCode adr(GetVIXLAssembler(), lr, &return_address);
8538 __ cmp(mr, Operand(0));
8539 EmitPlaceholderBne(this, bne_label);
8540 ptrdiff_t old_offset = GetVIXLAssembler()->GetBuffer()->GetCursorOffset();
8541 __ ldr(ref_reg, MemOperand(data_reg, index_reg, vixl32::LSL, scale_factor));
8542 DCHECK(!needs_null_check); // The thunk cannot handle the null check.
8543 // Note: We need a Wide NEG for the unpoisoning.
8544 if (kPoisonHeapReferences) {
8545 __ rsb(EncodingSize(Wide), ref_reg, ref_reg, Operand(0));
8546 }
8547 __ Bind(&return_address);
8548 DCHECK_EQ(old_offset - GetVIXLAssembler()->GetBuffer()->GetCursorOffset(),
8549 BAKER_MARK_INTROSPECTION_ARRAY_LDR_OFFSET);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008550 }
Roland Levillain5daa4952017-07-03 17:23:56 +01008551 MaybeGenerateMarkingRegisterCheck(/* code */ 20, /* temp_loc */ LocationFrom(ip));
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008552 return;
8553 }
8554
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008555 // /* HeapReference<Object> */ ref =
8556 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008557 GenerateReferenceLoadWithBakerReadBarrier(
8558 instruction, ref, obj, data_offset, index, scale_factor, temp, needs_null_check);
Roland Levillain6070e882016-11-03 17:51:58 +00008559}
8560
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008561void CodeGeneratorARMVIXL::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
8562 Location ref,
8563 vixl32::Register obj,
8564 uint32_t offset,
8565 Location index,
8566 ScaleFactor scale_factor,
8567 Location temp,
Roland Levillainff487002017-03-07 16:50:01 +00008568 bool needs_null_check) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008569 DCHECK(kEmitCompilerReadBarrier);
8570 DCHECK(kUseBakerReadBarrier);
8571
Roland Levillain6d729a72017-06-30 18:34:01 +01008572 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in the
8573 // Marking Register) to decide whether we need to enter the slow
8574 // path to mark the reference. Then, in the slow path, check the
8575 // gray bit in the lock word of the reference's holder (`obj`) to
8576 // decide whether to mark `ref` or not.
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008577 //
Roland Levillain6d729a72017-06-30 18:34:01 +01008578 // if (mr) { // Thread::Current()->GetIsGcMarking()
Roland Levillainff487002017-03-07 16:50:01 +00008579 // // Slow path.
8580 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
8581 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
8582 // HeapReference<mirror::Object> ref = *src; // Original reference load.
8583 // bool is_gray = (rb_state == ReadBarrier::GrayState());
8584 // if (is_gray) {
Roland Levillain6d729a72017-06-30 18:34:01 +01008585 // entrypoint = Thread::Current()->pReadBarrierMarkReg ## root.reg()
8586 // ref = entrypoint(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
Roland Levillainff487002017-03-07 16:50:01 +00008587 // }
8588 // } else {
8589 // HeapReference<mirror::Object> ref = *src; // Original reference load.
8590 // }
8591
8592 vixl32::Register temp_reg = RegisterFrom(temp);
8593
8594 // Slow path marking the object `ref` when the GC is marking. The
Roland Levillain6d729a72017-06-30 18:34:01 +01008595 // entrypoint will be loaded by the slow path code.
Roland Levillainff487002017-03-07 16:50:01 +00008596 SlowPathCodeARMVIXL* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01008597 new (GetScopedAllocator()) LoadReferenceWithBakerReadBarrierSlowPathARMVIXL(
Roland Levillain6d729a72017-06-30 18:34:01 +01008598 instruction, ref, obj, offset, index, scale_factor, needs_null_check, temp_reg);
Roland Levillainff487002017-03-07 16:50:01 +00008599 AddSlowPath(slow_path);
8600
Roland Levillain6d729a72017-06-30 18:34:01 +01008601 __ CompareAndBranchIfNonZero(mr, slow_path->GetEntryLabel());
Roland Levillainff487002017-03-07 16:50:01 +00008602 // Fast path: the GC is not marking: just load the reference.
8603 GenerateRawReferenceLoad(instruction, ref, obj, offset, index, scale_factor, needs_null_check);
8604 __ Bind(slow_path->GetExitLabel());
Roland Levillain5daa4952017-07-03 17:23:56 +01008605 MaybeGenerateMarkingRegisterCheck(/* code */ 21);
Roland Levillainff487002017-03-07 16:50:01 +00008606}
8607
8608void CodeGeneratorARMVIXL::UpdateReferenceFieldWithBakerReadBarrier(HInstruction* instruction,
8609 Location ref,
8610 vixl32::Register obj,
8611 Location field_offset,
8612 Location temp,
8613 bool needs_null_check,
8614 vixl32::Register temp2) {
8615 DCHECK(kEmitCompilerReadBarrier);
8616 DCHECK(kUseBakerReadBarrier);
8617
Roland Levillain6d729a72017-06-30 18:34:01 +01008618 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in the
8619 // Marking Register) to decide whether we need to enter the slow
8620 // path to update the reference field within `obj`. Then, in the
8621 // slow path, check the gray bit in the lock word of the reference's
8622 // holder (`obj`) to decide whether to mark `ref` and update the
8623 // field or not.
Roland Levillainff487002017-03-07 16:50:01 +00008624 //
Roland Levillain6d729a72017-06-30 18:34:01 +01008625 // if (mr) { // Thread::Current()->GetIsGcMarking()
Roland Levillainba650a42017-03-06 13:52:32 +00008626 // // Slow path.
Roland Levillain54f869e2017-03-06 13:54:11 +00008627 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
8628 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
Roland Levillainff487002017-03-07 16:50:01 +00008629 // HeapReference<mirror::Object> ref = *(obj + field_offset); // Reference load.
Roland Levillain54f869e2017-03-06 13:54:11 +00008630 // bool is_gray = (rb_state == ReadBarrier::GrayState());
8631 // if (is_gray) {
Roland Levillainff487002017-03-07 16:50:01 +00008632 // old_ref = ref;
Roland Levillain6d729a72017-06-30 18:34:01 +01008633 // entrypoint = Thread::Current()->pReadBarrierMarkReg ## root.reg()
8634 // ref = entrypoint(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
Roland Levillainff487002017-03-07 16:50:01 +00008635 // compareAndSwapObject(obj, field_offset, old_ref, ref);
Roland Levillain54f869e2017-03-06 13:54:11 +00008636 // }
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008637 // }
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008638
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008639 vixl32::Register temp_reg = RegisterFrom(temp);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008640
Roland Levillainff487002017-03-07 16:50:01 +00008641 // Slow path updating the object reference at address `obj + field_offset`
Roland Levillain6d729a72017-06-30 18:34:01 +01008642 // when the GC is marking. The entrypoint will be loaded by the slow path code.
Vladimir Marko174b2e22017-10-12 13:34:49 +01008643 SlowPathCodeARMVIXL* slow_path =
8644 new (GetScopedAllocator()) LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARMVIXL(
Roland Levillainff487002017-03-07 16:50:01 +00008645 instruction,
8646 ref,
8647 obj,
8648 /* offset */ 0u,
8649 /* index */ field_offset,
8650 /* scale_factor */ ScaleFactor::TIMES_1,
8651 needs_null_check,
8652 temp_reg,
Roland Levillain6d729a72017-06-30 18:34:01 +01008653 temp2);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008654 AddSlowPath(slow_path);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008655
Roland Levillain6d729a72017-06-30 18:34:01 +01008656 __ CompareAndBranchIfNonZero(mr, slow_path->GetEntryLabel());
Roland Levillainff487002017-03-07 16:50:01 +00008657 // Fast path: the GC is not marking: nothing to do (the field is
8658 // up-to-date, and we don't need to load the reference).
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008659 __ Bind(slow_path->GetExitLabel());
Roland Levillain5daa4952017-07-03 17:23:56 +01008660 MaybeGenerateMarkingRegisterCheck(/* code */ 22);
Roland Levillain844e6532016-11-03 16:09:47 +00008661}
Scott Wakelingfe885462016-09-22 10:24:38 +01008662
Roland Levillainba650a42017-03-06 13:52:32 +00008663void CodeGeneratorARMVIXL::GenerateRawReferenceLoad(HInstruction* instruction,
8664 Location ref,
8665 vixl::aarch32::Register obj,
8666 uint32_t offset,
8667 Location index,
8668 ScaleFactor scale_factor,
8669 bool needs_null_check) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008670 DataType::Type type = DataType::Type::kReference;
Roland Levillainba650a42017-03-06 13:52:32 +00008671 vixl32::Register ref_reg = RegisterFrom(ref, type);
8672
8673 // If needed, vixl::EmissionCheckScope guards are used to ensure
8674 // that no pools are emitted between the load (macro) instruction
8675 // and MaybeRecordImplicitNullCheck.
8676
Scott Wakelingfe885462016-09-22 10:24:38 +01008677 if (index.IsValid()) {
8678 // Load types involving an "index": ArrayGet,
8679 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
8680 // intrinsics.
Roland Levillainba650a42017-03-06 13:52:32 +00008681 // /* HeapReference<mirror::Object> */ ref = *(obj + offset + (index << scale_factor))
Scott Wakelingfe885462016-09-22 10:24:38 +01008682 if (index.IsConstant()) {
8683 size_t computed_offset =
8684 (Int32ConstantFrom(index) << scale_factor) + offset;
Roland Levillainba650a42017-03-06 13:52:32 +00008685 vixl::EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Scott Wakelingfe885462016-09-22 10:24:38 +01008686 GetAssembler()->LoadFromOffset(kLoadWord, ref_reg, obj, computed_offset);
Roland Levillainba650a42017-03-06 13:52:32 +00008687 if (needs_null_check) {
8688 MaybeRecordImplicitNullCheck(instruction);
8689 }
Scott Wakelingfe885462016-09-22 10:24:38 +01008690 } else {
8691 // Handle the special case of the
8692 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
8693 // intrinsics, which use a register pair as index ("long
8694 // offset"), of which only the low part contains data.
8695 vixl32::Register index_reg = index.IsRegisterPair()
8696 ? LowRegisterFrom(index)
8697 : RegisterFrom(index);
8698 UseScratchRegisterScope temps(GetVIXLAssembler());
Roland Levillainba650a42017-03-06 13:52:32 +00008699 vixl32::Register temp = temps.Acquire();
8700 __ Add(temp, obj, Operand(index_reg, ShiftType::LSL, scale_factor));
8701 {
8702 vixl::EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
8703 GetAssembler()->LoadFromOffset(kLoadWord, ref_reg, temp, offset);
8704 if (needs_null_check) {
8705 MaybeRecordImplicitNullCheck(instruction);
8706 }
8707 }
Scott Wakelingfe885462016-09-22 10:24:38 +01008708 }
8709 } else {
Roland Levillainba650a42017-03-06 13:52:32 +00008710 // /* HeapReference<mirror::Object> */ ref = *(obj + offset)
8711 vixl::EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Scott Wakelingfe885462016-09-22 10:24:38 +01008712 GetAssembler()->LoadFromOffset(kLoadWord, ref_reg, obj, offset);
Roland Levillainba650a42017-03-06 13:52:32 +00008713 if (needs_null_check) {
8714 MaybeRecordImplicitNullCheck(instruction);
8715 }
Scott Wakelingfe885462016-09-22 10:24:38 +01008716 }
8717
Roland Levillain844e6532016-11-03 16:09:47 +00008718 // Object* ref = ref_addr->AsMirrorPtr()
8719 GetAssembler()->MaybeUnpoisonHeapReference(ref_reg);
Roland Levillain844e6532016-11-03 16:09:47 +00008720}
8721
Roland Levillain5daa4952017-07-03 17:23:56 +01008722void CodeGeneratorARMVIXL::MaybeGenerateMarkingRegisterCheck(int code, Location temp_loc) {
8723 // The following condition is a compile-time one, so it does not have a run-time cost.
8724 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier && kIsDebugBuild) {
8725 // The following condition is a run-time one; it is executed after the
8726 // previous compile-time test, to avoid penalizing non-debug builds.
8727 if (GetCompilerOptions().EmitRunTimeChecksInDebugMode()) {
8728 UseScratchRegisterScope temps(GetVIXLAssembler());
8729 vixl32::Register temp = temp_loc.IsValid() ? RegisterFrom(temp_loc) : temps.Acquire();
8730 GetAssembler()->GenerateMarkingRegisterCheck(temp,
8731 kMarkingRegisterCheckBreakCodeBaseCode + code);
8732 }
8733 }
8734}
8735
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008736void CodeGeneratorARMVIXL::GenerateReadBarrierSlow(HInstruction* instruction,
8737 Location out,
8738 Location ref,
8739 Location obj,
8740 uint32_t offset,
8741 Location index) {
8742 DCHECK(kEmitCompilerReadBarrier);
8743
8744 // Insert a slow path based read barrier *after* the reference load.
8745 //
8746 // If heap poisoning is enabled, the unpoisoning of the loaded
8747 // reference will be carried out by the runtime within the slow
8748 // path.
8749 //
8750 // Note that `ref` currently does not get unpoisoned (when heap
8751 // poisoning is enabled), which is alright as the `ref` argument is
8752 // not used by the artReadBarrierSlow entry point.
8753 //
8754 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Vladimir Marko174b2e22017-10-12 13:34:49 +01008755 SlowPathCodeARMVIXL* slow_path = new (GetScopedAllocator())
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008756 ReadBarrierForHeapReferenceSlowPathARMVIXL(instruction, out, ref, obj, offset, index);
8757 AddSlowPath(slow_path);
8758
8759 __ B(slow_path->GetEntryLabel());
8760 __ Bind(slow_path->GetExitLabel());
8761}
8762
8763void CodeGeneratorARMVIXL::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
Artem Serov02d37832016-10-25 15:25:33 +01008764 Location out,
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008765 Location ref,
8766 Location obj,
8767 uint32_t offset,
8768 Location index) {
Artem Serov02d37832016-10-25 15:25:33 +01008769 if (kEmitCompilerReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008770 // Baker's read barriers shall be handled by the fast path
Roland Levillain9983e302017-07-14 14:34:22 +01008771 // (CodeGeneratorARMVIXL::GenerateReferenceLoadWithBakerReadBarrier).
Artem Serov02d37832016-10-25 15:25:33 +01008772 DCHECK(!kUseBakerReadBarrier);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008773 // If heap poisoning is enabled, unpoisoning will be taken care of
8774 // by the runtime within the slow path.
8775 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Artem Serov02d37832016-10-25 15:25:33 +01008776 } else if (kPoisonHeapReferences) {
8777 GetAssembler()->UnpoisonHeapReference(RegisterFrom(out));
8778 }
8779}
8780
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008781void CodeGeneratorARMVIXL::GenerateReadBarrierForRootSlow(HInstruction* instruction,
8782 Location out,
8783 Location root) {
8784 DCHECK(kEmitCompilerReadBarrier);
8785
8786 // Insert a slow path based read barrier *after* the GC root load.
8787 //
8788 // Note that GC roots are not affected by heap poisoning, so we do
8789 // not need to do anything special for this here.
8790 SlowPathCodeARMVIXL* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01008791 new (GetScopedAllocator()) ReadBarrierForRootSlowPathARMVIXL(instruction, out, root);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008792 AddSlowPath(slow_path);
8793
8794 __ B(slow_path->GetEntryLabel());
8795 __ Bind(slow_path->GetExitLabel());
8796}
8797
Artem Serov02d37832016-10-25 15:25:33 +01008798// Check if the desired_dispatch_info is supported. If it is, return it,
8799// otherwise return a fall-back info that should be used instead.
8800HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARMVIXL::GetSupportedInvokeStaticOrDirectDispatch(
Artem Serovd4cc5b22016-11-04 11:19:09 +00008801 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +00008802 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Nicolas Geoffraye807ff72017-01-23 09:03:12 +00008803 return desired_dispatch_info;
Artem Serov02d37832016-10-25 15:25:33 +01008804}
8805
Scott Wakelingfe885462016-09-22 10:24:38 +01008806vixl32::Register CodeGeneratorARMVIXL::GetInvokeStaticOrDirectExtraParameter(
8807 HInvokeStaticOrDirect* invoke, vixl32::Register temp) {
8808 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
8809 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
8810 if (!invoke->GetLocations()->Intrinsified()) {
8811 return RegisterFrom(location);
8812 }
8813 // For intrinsics we allow any location, so it may be on the stack.
8814 if (!location.IsRegister()) {
8815 GetAssembler()->LoadFromOffset(kLoadWord, temp, sp, location.GetStackIndex());
8816 return temp;
8817 }
8818 // For register locations, check if the register was saved. If so, get it from the stack.
8819 // Note: There is a chance that the register was saved but not overwritten, so we could
8820 // save one load. However, since this is just an intrinsic slow path we prefer this
8821 // simple and more robust approach rather that trying to determine if that's the case.
8822 SlowPathCode* slow_path = GetCurrentSlowPath();
Scott Wakelingd5cd4972017-02-03 11:38:35 +00008823 if (slow_path != nullptr && slow_path->IsCoreRegisterSaved(RegisterFrom(location).GetCode())) {
Scott Wakelingfe885462016-09-22 10:24:38 +01008824 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(RegisterFrom(location).GetCode());
8825 GetAssembler()->LoadFromOffset(kLoadWord, temp, sp, stack_offset);
8826 return temp;
8827 }
8828 return RegisterFrom(location);
8829}
8830
Vladimir Markod254f5c2017-06-02 15:18:36 +00008831void CodeGeneratorARMVIXL::GenerateStaticOrDirectCall(
Vladimir Markoe7197bf2017-06-02 17:00:23 +01008832 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00008833 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
Scott Wakelingfe885462016-09-22 10:24:38 +01008834 switch (invoke->GetMethodLoadKind()) {
8835 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
8836 uint32_t offset =
8837 GetThreadOffset<kArmPointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
8838 // temp = thread->string_init_entrypoint
Artem Serovd4cc5b22016-11-04 11:19:09 +00008839 GetAssembler()->LoadFromOffset(kLoadWord, RegisterFrom(temp), tr, offset);
8840 break;
8841 }
8842 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
8843 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
8844 break;
Vladimir Marko65979462017-05-19 17:25:12 +01008845 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative: {
8846 DCHECK(GetCompilerOptions().IsBootImage());
8847 PcRelativePatchInfo* labels = NewPcRelativeMethodPatch(invoke->GetTargetMethod());
8848 vixl32::Register temp_reg = RegisterFrom(temp);
8849 EmitMovwMovtPlaceholder(labels, temp_reg);
8850 break;
8851 }
Artem Serovd4cc5b22016-11-04 11:19:09 +00008852 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
8853 __ Mov(RegisterFrom(temp), Operand::From(invoke->GetMethodAddress()));
8854 break;
Vladimir Marko0eb882b2017-05-15 13:39:18 +01008855 case HInvokeStaticOrDirect::MethodLoadKind::kBssEntry: {
8856 PcRelativePatchInfo* labels = NewMethodBssEntryPatch(
8857 MethodReference(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex()));
8858 vixl32::Register temp_reg = RegisterFrom(temp);
8859 EmitMovwMovtPlaceholder(labels, temp_reg);
8860 GetAssembler()->LoadFromOffset(kLoadWord, temp_reg, temp_reg, /* offset*/ 0);
Scott Wakelingfe885462016-09-22 10:24:38 +01008861 break;
8862 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01008863 case HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall: {
8864 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
8865 return; // No code pointer retrieval; the runtime performs the call directly.
Scott Wakelingfe885462016-09-22 10:24:38 +01008866 }
Scott Wakelingfe885462016-09-22 10:24:38 +01008867 }
8868
Artem Serovd4cc5b22016-11-04 11:19:09 +00008869 switch (invoke->GetCodePtrLocation()) {
8870 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Vladimir Markoe7197bf2017-06-02 17:00:23 +01008871 {
8872 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
8873 ExactAssemblyScope aas(GetVIXLAssembler(),
8874 vixl32::k32BitT32InstructionSizeInBytes,
8875 CodeBufferCheckScope::kMaximumSize);
8876 __ bl(GetFrameEntryLabel());
8877 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
8878 }
Artem Serovd4cc5b22016-11-04 11:19:09 +00008879 break;
Artem Serovd4cc5b22016-11-04 11:19:09 +00008880 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
8881 // LR = callee_method->entry_point_from_quick_compiled_code_
8882 GetAssembler()->LoadFromOffset(
8883 kLoadWord,
8884 lr,
8885 RegisterFrom(callee_method),
8886 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize).Int32Value());
Alexandre Rames374ddf32016-11-04 10:40:49 +00008887 {
Vladimir Markoe7197bf2017-06-02 17:00:23 +01008888 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
Alexandre Rames374ddf32016-11-04 10:40:49 +00008889 // 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 +00008890 ExactAssemblyScope aas(GetVIXLAssembler(),
8891 vixl32::k16BitT32InstructionSizeInBytes,
8892 CodeBufferCheckScope::kExactSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00008893 // LR()
8894 __ blx(lr);
Vladimir Markoe7197bf2017-06-02 17:00:23 +01008895 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Alexandre Rames374ddf32016-11-04 10:40:49 +00008896 }
Artem Serovd4cc5b22016-11-04 11:19:09 +00008897 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01008898 }
8899
Scott Wakelingfe885462016-09-22 10:24:38 +01008900 DCHECK(!IsLeafMethod());
8901}
8902
Vladimir Markoe7197bf2017-06-02 17:00:23 +01008903void CodeGeneratorARMVIXL::GenerateVirtualCall(
8904 HInvokeVirtual* invoke, Location temp_location, SlowPathCode* slow_path) {
Scott Wakelingfe885462016-09-22 10:24:38 +01008905 vixl32::Register temp = RegisterFrom(temp_location);
8906 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
8907 invoke->GetVTableIndex(), kArmPointerSize).Uint32Value();
8908
8909 // Use the calling convention instead of the location of the receiver, as
8910 // intrinsics may have put the receiver in a different register. In the intrinsics
8911 // slow path, the arguments have been moved to the right place, so here we are
8912 // guaranteed that the receiver is the first register of the calling convention.
8913 InvokeDexCallingConventionARMVIXL calling_convention;
8914 vixl32::Register receiver = calling_convention.GetRegisterAt(0);
8915 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Alexandre Rames374ddf32016-11-04 10:40:49 +00008916 {
8917 // Make sure the pc is recorded immediately after the `ldr` instruction.
Artem Serov0fb37192016-12-06 18:13:40 +00008918 ExactAssemblyScope aas(GetVIXLAssembler(),
8919 vixl32::kMaxInstructionSizeInBytes,
8920 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00008921 // /* HeapReference<Class> */ temp = receiver->klass_
8922 __ ldr(temp, MemOperand(receiver, class_offset));
8923 MaybeRecordImplicitNullCheck(invoke);
8924 }
Scott Wakelingfe885462016-09-22 10:24:38 +01008925 // Instead of simply (possibly) unpoisoning `temp` here, we should
8926 // emit a read barrier for the previous class reference load.
8927 // However this is not required in practice, as this is an
8928 // intermediate/temporary reference and because the current
8929 // concurrent copying collector keeps the from-space memory
8930 // intact/accessible until the end of the marking phase (the
8931 // concurrent copying collector may not in the future).
8932 GetAssembler()->MaybeUnpoisonHeapReference(temp);
8933
8934 // temp = temp->GetMethodAt(method_offset);
8935 uint32_t entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(
8936 kArmPointerSize).Int32Value();
8937 GetAssembler()->LoadFromOffset(kLoadWord, temp, temp, method_offset);
8938 // LR = temp->GetEntryPoint();
8939 GetAssembler()->LoadFromOffset(kLoadWord, lr, temp, entry_point);
Vladimir Markoe7197bf2017-06-02 17:00:23 +01008940 {
8941 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
8942 // blx in T32 has only 16bit encoding that's why a stricter check for the scope is used.
8943 ExactAssemblyScope aas(GetVIXLAssembler(),
8944 vixl32::k16BitT32InstructionSizeInBytes,
8945 CodeBufferCheckScope::kExactSize);
8946 // LR();
8947 __ blx(lr);
8948 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
8949 }
Scott Wakelingfe885462016-09-22 10:24:38 +01008950}
8951
Vladimir Marko65979462017-05-19 17:25:12 +01008952CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewPcRelativeMethodPatch(
8953 MethodReference target_method) {
8954 return NewPcRelativePatch(*target_method.dex_file,
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07008955 target_method.index,
Vladimir Marko65979462017-05-19 17:25:12 +01008956 &pc_relative_method_patches_);
Artem Serovd4cc5b22016-11-04 11:19:09 +00008957}
8958
Vladimir Marko0eb882b2017-05-15 13:39:18 +01008959CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewMethodBssEntryPatch(
8960 MethodReference target_method) {
8961 return NewPcRelativePatch(*target_method.dex_file,
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07008962 target_method.index,
Vladimir Marko0eb882b2017-05-15 13:39:18 +01008963 &method_bss_entry_patches_);
8964}
8965
Artem Serovd4cc5b22016-11-04 11:19:09 +00008966CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewPcRelativeTypePatch(
8967 const DexFile& dex_file, dex::TypeIndex type_index) {
8968 return NewPcRelativePatch(dex_file, type_index.index_, &pc_relative_type_patches_);
8969}
8970
Vladimir Marko1998cd02017-01-13 13:02:58 +00008971CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewTypeBssEntryPatch(
8972 const DexFile& dex_file, dex::TypeIndex type_index) {
8973 return NewPcRelativePatch(dex_file, type_index.index_, &type_bss_entry_patches_);
8974}
8975
Vladimir Marko65979462017-05-19 17:25:12 +01008976CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewPcRelativeStringPatch(
8977 const DexFile& dex_file, dex::StringIndex string_index) {
8978 return NewPcRelativePatch(dex_file, string_index.index_, &pc_relative_string_patches_);
8979}
8980
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01008981CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewStringBssEntryPatch(
8982 const DexFile& dex_file, dex::StringIndex string_index) {
8983 return NewPcRelativePatch(dex_file, string_index.index_, &string_bss_entry_patches_);
8984}
8985
Artem Serovd4cc5b22016-11-04 11:19:09 +00008986CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewPcRelativePatch(
8987 const DexFile& dex_file, uint32_t offset_or_index, ArenaDeque<PcRelativePatchInfo>* patches) {
8988 patches->emplace_back(dex_file, offset_or_index);
8989 return &patches->back();
8990}
8991
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008992vixl::aarch32::Label* CodeGeneratorARMVIXL::NewBakerReadBarrierPatch(uint32_t custom_data) {
8993 baker_read_barrier_patches_.emplace_back(custom_data);
8994 return &baker_read_barrier_patches_.back().label;
8995}
8996
Artem Serovc5fcb442016-12-02 19:19:58 +00008997VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateBootImageAddressLiteral(uint32_t address) {
Richard Uhlerc52f3032017-03-02 13:45:45 +00008998 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), &uint32_literals_);
Artem Serovc5fcb442016-12-02 19:19:58 +00008999}
9000
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00009001VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateJitStringLiteral(
9002 const DexFile& dex_file,
9003 dex::StringIndex string_index,
9004 Handle<mirror::String> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01009005 ReserveJitStringRoot(StringReference(&dex_file, string_index), handle);
Artem Serovc5fcb442016-12-02 19:19:58 +00009006 return jit_string_patches_.GetOrCreate(
9007 StringReference(&dex_file, string_index),
9008 [this]() {
9009 return GetAssembler()->CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u);
9010 });
9011}
9012
9013VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateJitClassLiteral(const DexFile& dex_file,
9014 dex::TypeIndex type_index,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00009015 Handle<mirror::Class> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01009016 ReserveJitClassRoot(TypeReference(&dex_file, type_index), handle);
Artem Serovc5fcb442016-12-02 19:19:58 +00009017 return jit_class_patches_.GetOrCreate(
9018 TypeReference(&dex_file, type_index),
9019 [this]() {
9020 return GetAssembler()->CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u);
9021 });
9022}
9023
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01009024template <linker::LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
Artem Serovd4cc5b22016-11-04 11:19:09 +00009025inline void CodeGeneratorARMVIXL::EmitPcRelativeLinkerPatches(
9026 const ArenaDeque<PcRelativePatchInfo>& infos,
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01009027 ArenaVector<linker::LinkerPatch>* linker_patches) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00009028 for (const PcRelativePatchInfo& info : infos) {
9029 const DexFile& dex_file = info.target_dex_file;
9030 size_t offset_or_index = info.offset_or_index;
9031 DCHECK(info.add_pc_label.IsBound());
9032 uint32_t add_pc_offset = dchecked_integral_cast<uint32_t>(info.add_pc_label.GetLocation());
9033 // Add MOVW patch.
9034 DCHECK(info.movw_label.IsBound());
9035 uint32_t movw_offset = dchecked_integral_cast<uint32_t>(info.movw_label.GetLocation());
9036 linker_patches->push_back(Factory(movw_offset, &dex_file, add_pc_offset, offset_or_index));
9037 // Add MOVT patch.
9038 DCHECK(info.movt_label.IsBound());
9039 uint32_t movt_offset = dchecked_integral_cast<uint32_t>(info.movt_label.GetLocation());
9040 linker_patches->push_back(Factory(movt_offset, &dex_file, add_pc_offset, offset_or_index));
9041 }
9042}
9043
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01009044void CodeGeneratorARMVIXL::EmitLinkerPatches(ArenaVector<linker::LinkerPatch>* linker_patches) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00009045 DCHECK(linker_patches->empty());
9046 size_t size =
Vladimir Marko65979462017-05-19 17:25:12 +01009047 /* MOVW+MOVT for each entry */ 2u * pc_relative_method_patches_.size() +
Vladimir Marko0eb882b2017-05-15 13:39:18 +01009048 /* MOVW+MOVT for each entry */ 2u * method_bss_entry_patches_.size() +
Artem Serovc5fcb442016-12-02 19:19:58 +00009049 /* MOVW+MOVT for each entry */ 2u * pc_relative_type_patches_.size() +
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01009050 /* MOVW+MOVT for each entry */ 2u * type_bss_entry_patches_.size() +
Vladimir Marko65979462017-05-19 17:25:12 +01009051 /* MOVW+MOVT for each entry */ 2u * pc_relative_string_patches_.size() +
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01009052 /* MOVW+MOVT for each entry */ 2u * string_bss_entry_patches_.size() +
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01009053 baker_read_barrier_patches_.size();
Artem Serovd4cc5b22016-11-04 11:19:09 +00009054 linker_patches->reserve(size);
Vladimir Marko65979462017-05-19 17:25:12 +01009055 if (GetCompilerOptions().IsBootImage()) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01009056 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeMethodPatch>(
9057 pc_relative_method_patches_, linker_patches);
9058 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeTypePatch>(
9059 pc_relative_type_patches_, linker_patches);
9060 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeStringPatch>(
9061 pc_relative_string_patches_, linker_patches);
Vladimir Marko65979462017-05-19 17:25:12 +01009062 } else {
9063 DCHECK(pc_relative_method_patches_.empty());
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01009064 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeClassTablePatch>(
9065 pc_relative_type_patches_, linker_patches);
9066 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringInternTablePatch>(
9067 pc_relative_string_patches_, linker_patches);
Artem Serovd4cc5b22016-11-04 11:19:09 +00009068 }
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01009069 EmitPcRelativeLinkerPatches<linker::LinkerPatch::MethodBssEntryPatch>(
9070 method_bss_entry_patches_, linker_patches);
9071 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeBssEntryPatch>(
9072 type_bss_entry_patches_, linker_patches);
9073 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringBssEntryPatch>(
9074 string_bss_entry_patches_, linker_patches);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01009075 for (const BakerReadBarrierPatchInfo& info : baker_read_barrier_patches_) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01009076 linker_patches->push_back(linker::LinkerPatch::BakerReadBarrierBranchPatch(
9077 info.label.GetLocation(), info.custom_data));
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01009078 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00009079 DCHECK_EQ(size, linker_patches->size());
Artem Serovc5fcb442016-12-02 19:19:58 +00009080}
9081
9082VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateUint32Literal(
9083 uint32_t value,
9084 Uint32ToLiteralMap* map) {
9085 return map->GetOrCreate(
9086 value,
9087 [this, value]() {
9088 return GetAssembler()->CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ value);
9089 });
9090}
9091
Artem Serov2bbc9532016-10-21 11:51:50 +01009092void LocationsBuilderARMVIXL::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
9093 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01009094 new (GetGraph()->GetAllocator()) LocationSummary(instr, LocationSummary::kNoCall);
Artem Serov2bbc9532016-10-21 11:51:50 +01009095 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
9096 Location::RequiresRegister());
9097 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
9098 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
9099 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
9100}
9101
9102void InstructionCodeGeneratorARMVIXL::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
9103 vixl32::Register res = OutputRegister(instr);
9104 vixl32::Register accumulator =
9105 InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
9106 vixl32::Register mul_left =
9107 InputRegisterAt(instr, HMultiplyAccumulate::kInputMulLeftIndex);
9108 vixl32::Register mul_right =
9109 InputRegisterAt(instr, HMultiplyAccumulate::kInputMulRightIndex);
9110
9111 if (instr->GetOpKind() == HInstruction::kAdd) {
9112 __ Mla(res, mul_left, mul_right, accumulator);
9113 } else {
9114 __ Mls(res, mul_left, mul_right, accumulator);
9115 }
9116}
9117
Artem Serov551b28f2016-10-18 19:11:30 +01009118void LocationsBuilderARMVIXL::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
9119 // Nothing to do, this should be removed during prepare for register allocator.
9120 LOG(FATAL) << "Unreachable";
9121}
9122
9123void InstructionCodeGeneratorARMVIXL::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
9124 // Nothing to do, this should be removed during prepare for register allocator.
9125 LOG(FATAL) << "Unreachable";
9126}
9127
9128// Simple implementation of packed switch - generate cascaded compare/jumps.
9129void LocationsBuilderARMVIXL::VisitPackedSwitch(HPackedSwitch* switch_instr) {
9130 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01009131 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Artem Serov551b28f2016-10-18 19:11:30 +01009132 locations->SetInAt(0, Location::RequiresRegister());
9133 if (switch_instr->GetNumEntries() > kPackedSwitchCompareJumpThreshold &&
9134 codegen_->GetAssembler()->GetVIXLAssembler()->IsUsingT32()) {
9135 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the table base.
9136 if (switch_instr->GetStartValue() != 0) {
9137 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the bias.
9138 }
9139 }
9140}
9141
9142// TODO(VIXL): Investigate and reach the parity with old arm codegen.
9143void InstructionCodeGeneratorARMVIXL::VisitPackedSwitch(HPackedSwitch* switch_instr) {
9144 int32_t lower_bound = switch_instr->GetStartValue();
9145 uint32_t num_entries = switch_instr->GetNumEntries();
9146 LocationSummary* locations = switch_instr->GetLocations();
9147 vixl32::Register value_reg = InputRegisterAt(switch_instr, 0);
9148 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
9149
9150 if (num_entries <= kPackedSwitchCompareJumpThreshold ||
9151 !codegen_->GetAssembler()->GetVIXLAssembler()->IsUsingT32()) {
9152 // Create a series of compare/jumps.
Anton Kirilovedb2ac32016-11-30 15:14:10 +00009153 UseScratchRegisterScope temps(GetVIXLAssembler());
Artem Serov551b28f2016-10-18 19:11:30 +01009154 vixl32::Register temp_reg = temps.Acquire();
9155 // Note: It is fine for the below AddConstantSetFlags() using IP register to temporarily store
9156 // the immediate, because IP is used as the destination register. For the other
9157 // AddConstantSetFlags() and GenerateCompareWithImmediate(), the immediate values are constant,
9158 // and they can be encoded in the instruction without making use of IP register.
9159 __ Adds(temp_reg, value_reg, -lower_bound);
9160
9161 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
9162 // Jump to successors[0] if value == lower_bound.
9163 __ B(eq, codegen_->GetLabelOf(successors[0]));
9164 int32_t last_index = 0;
9165 for (; num_entries - last_index > 2; last_index += 2) {
9166 __ Adds(temp_reg, temp_reg, -2);
9167 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
9168 __ B(lo, codegen_->GetLabelOf(successors[last_index + 1]));
9169 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
9170 __ B(eq, codegen_->GetLabelOf(successors[last_index + 2]));
9171 }
9172 if (num_entries - last_index == 2) {
9173 // The last missing case_value.
9174 __ Cmp(temp_reg, 1);
9175 __ B(eq, codegen_->GetLabelOf(successors[last_index + 1]));
9176 }
9177
9178 // And the default for any other value.
9179 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
9180 __ B(codegen_->GetLabelOf(default_block));
9181 }
9182 } else {
9183 // Create a table lookup.
9184 vixl32::Register table_base = RegisterFrom(locations->GetTemp(0));
9185
9186 JumpTableARMVIXL* jump_table = codegen_->CreateJumpTable(switch_instr);
9187
9188 // Remove the bias.
9189 vixl32::Register key_reg;
9190 if (lower_bound != 0) {
9191 key_reg = RegisterFrom(locations->GetTemp(1));
9192 __ Sub(key_reg, value_reg, lower_bound);
9193 } else {
9194 key_reg = value_reg;
9195 }
9196
9197 // Check whether the value is in the table, jump to default block if not.
9198 __ Cmp(key_reg, num_entries - 1);
9199 __ B(hi, codegen_->GetLabelOf(default_block));
9200
Anton Kirilovedb2ac32016-11-30 15:14:10 +00009201 UseScratchRegisterScope temps(GetVIXLAssembler());
Artem Serov551b28f2016-10-18 19:11:30 +01009202 vixl32::Register jump_offset = temps.Acquire();
9203
9204 // Load jump offset from the table.
Scott Wakeling86e9d262017-01-18 15:59:24 +00009205 {
9206 const size_t jump_size = switch_instr->GetNumEntries() * sizeof(int32_t);
9207 ExactAssemblyScope aas(GetVIXLAssembler(),
9208 (vixl32::kMaxInstructionSizeInBytes * 4) + jump_size,
9209 CodeBufferCheckScope::kMaximumSize);
9210 __ adr(table_base, jump_table->GetTableStartLabel());
9211 __ ldr(jump_offset, MemOperand(table_base, key_reg, vixl32::LSL, 2));
Artem Serov551b28f2016-10-18 19:11:30 +01009212
Scott Wakeling86e9d262017-01-18 15:59:24 +00009213 // Jump to target block by branching to table_base(pc related) + offset.
9214 vixl32::Register target_address = table_base;
9215 __ add(target_address, table_base, jump_offset);
9216 __ bx(target_address);
Artem Serov09a940d2016-11-11 16:15:11 +00009217
Scott Wakeling86e9d262017-01-18 15:59:24 +00009218 jump_table->EmitTable(codegen_);
9219 }
Artem Serov551b28f2016-10-18 19:11:30 +01009220 }
9221}
9222
Artem Serov02d37832016-10-25 15:25:33 +01009223// Copy the result of a call into the given target.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009224void CodeGeneratorARMVIXL::MoveFromReturnRegister(Location trg, DataType::Type type) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01009225 if (!trg.IsValid()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009226 DCHECK_EQ(type, DataType::Type::kVoid);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01009227 return;
9228 }
9229
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009230 DCHECK_NE(type, DataType::Type::kVoid);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01009231
Artem Serovd4cc5b22016-11-04 11:19:09 +00009232 Location return_loc = InvokeDexCallingConventionVisitorARMVIXL().GetReturnLocation(type);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01009233 if (return_loc.Equals(trg)) {
9234 return;
9235 }
9236
9237 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
9238 // with the last branch.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009239 if (type == DataType::Type::kInt64) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01009240 TODO_VIXL32(FATAL);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009241 } else if (type == DataType::Type::kFloat64) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01009242 TODO_VIXL32(FATAL);
9243 } else {
9244 // Let the parallel move resolver take care of all of this.
Vladimir Markoca6fff82017-10-03 14:49:14 +01009245 HParallelMove parallel_move(GetGraph()->GetAllocator());
Anton Kirilove28d9ae2016-10-25 18:17:23 +01009246 parallel_move.AddMove(return_loc, trg, type, nullptr);
9247 GetMoveResolver()->EmitNativeCode(&parallel_move);
9248 }
Scott Wakelingfe885462016-09-22 10:24:38 +01009249}
9250
xueliang.zhong8d2c4592016-11-23 17:05:25 +00009251void LocationsBuilderARMVIXL::VisitClassTableGet(HClassTableGet* instruction) {
9252 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01009253 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
xueliang.zhong8d2c4592016-11-23 17:05:25 +00009254 locations->SetInAt(0, Location::RequiresRegister());
9255 locations->SetOut(Location::RequiresRegister());
Artem Serov551b28f2016-10-18 19:11:30 +01009256}
9257
xueliang.zhong8d2c4592016-11-23 17:05:25 +00009258void InstructionCodeGeneratorARMVIXL::VisitClassTableGet(HClassTableGet* instruction) {
9259 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
9260 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
9261 instruction->GetIndex(), kArmPointerSize).SizeValue();
9262 GetAssembler()->LoadFromOffset(kLoadWord,
9263 OutputRegister(instruction),
9264 InputRegisterAt(instruction, 0),
9265 method_offset);
9266 } else {
9267 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
9268 instruction->GetIndex(), kArmPointerSize));
9269 GetAssembler()->LoadFromOffset(kLoadWord,
9270 OutputRegister(instruction),
9271 InputRegisterAt(instruction, 0),
9272 mirror::Class::ImtPtrOffset(kArmPointerSize).Uint32Value());
9273 GetAssembler()->LoadFromOffset(kLoadWord,
9274 OutputRegister(instruction),
9275 OutputRegister(instruction),
9276 method_offset);
9277 }
Artem Serov551b28f2016-10-18 19:11:30 +01009278}
9279
Artem Serovc5fcb442016-12-02 19:19:58 +00009280static void PatchJitRootUse(uint8_t* code,
9281 const uint8_t* roots_data,
9282 VIXLUInt32Literal* literal,
9283 uint64_t index_in_table) {
9284 DCHECK(literal->IsBound());
9285 uint32_t literal_offset = literal->GetLocation();
9286 uintptr_t address =
9287 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
9288 uint8_t* data = code + literal_offset;
9289 reinterpret_cast<uint32_t*>(data)[0] = dchecked_integral_cast<uint32_t>(address);
9290}
9291
9292void CodeGeneratorARMVIXL::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
9293 for (const auto& entry : jit_string_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01009294 const StringReference& string_reference = entry.first;
9295 VIXLUInt32Literal* table_entry_literal = entry.second;
Vladimir Marko174b2e22017-10-12 13:34:49 +01009296 uint64_t index_in_table = GetJitStringRootIndex(string_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01009297 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Artem Serovc5fcb442016-12-02 19:19:58 +00009298 }
9299 for (const auto& entry : jit_class_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01009300 const TypeReference& type_reference = entry.first;
9301 VIXLUInt32Literal* table_entry_literal = entry.second;
Vladimir Marko174b2e22017-10-12 13:34:49 +01009302 uint64_t index_in_table = GetJitClassRootIndex(type_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01009303 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Artem Serovc5fcb442016-12-02 19:19:58 +00009304 }
9305}
9306
Artem Serovd4cc5b22016-11-04 11:19:09 +00009307void CodeGeneratorARMVIXL::EmitMovwMovtPlaceholder(
9308 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels,
9309 vixl32::Register out) {
Artem Serov0fb37192016-12-06 18:13:40 +00009310 ExactAssemblyScope aas(GetVIXLAssembler(),
9311 3 * vixl32::kMaxInstructionSizeInBytes,
9312 CodeBufferCheckScope::kMaximumSize);
Artem Serovd4cc5b22016-11-04 11:19:09 +00009313 // TODO(VIXL): Think about using mov instead of movw.
9314 __ bind(&labels->movw_label);
9315 __ movw(out, /* placeholder */ 0u);
9316 __ bind(&labels->movt_label);
9317 __ movt(out, /* placeholder */ 0u);
9318 __ bind(&labels->add_pc_label);
9319 __ add(out, out, pc);
9320}
9321
Scott Wakelingfe885462016-09-22 10:24:38 +01009322#undef __
9323#undef QUICK_ENTRY_POINT
9324#undef TODO_VIXL32
9325
9326} // namespace arm
9327} // namespace art