blob: 9e7455d488267374e43baed68960f0eeb8d1a2b8 [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();
535 constexpr bool call_saves_everything_except_r0 = (!kUseReadBarrier || kUseBakerReadBarrier);
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100536
537 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
538 __ Bind(GetEntryLabel());
539 SaveLiveRegisters(codegen, locations);
540
541 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Vladimir Markoea4c1262017-02-06 19:59:33 +0000542 // For HLoadClass/kBssEntry/kSaveEverything, make sure we preserve the address of the entry.
543 DCHECK_EQ(instruction_->IsLoadClass(), cls_ == instruction_);
544 bool is_load_class_bss_entry =
545 (cls_ == instruction_) && (cls_->GetLoadKind() == HLoadClass::LoadKind::kBssEntry);
546 vixl32::Register entry_address;
547 if (is_load_class_bss_entry && call_saves_everything_except_r0) {
548 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
549 // In the unlucky case that the `temp` is R0, we preserve the address in `out` across
550 // the kSaveEverything call.
551 bool temp_is_r0 = temp.Is(calling_convention.GetRegisterAt(0));
552 entry_address = temp_is_r0 ? RegisterFrom(out) : temp;
553 DCHECK(!entry_address.Is(calling_convention.GetRegisterAt(0)));
554 if (temp_is_r0) {
555 __ Mov(entry_address, temp);
556 }
557 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000558 dex::TypeIndex type_index = cls_->GetTypeIndex();
559 __ Mov(calling_convention.GetRegisterAt(0), type_index.index_);
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100560 QuickEntrypointEnum entrypoint = do_clinit_ ? kQuickInitializeStaticStorage
561 : kQuickInitializeType;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000562 arm_codegen->InvokeRuntime(entrypoint, instruction_, dex_pc_, this);
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100563 if (do_clinit_) {
564 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
565 } else {
566 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
567 }
568
Vladimir Markoea4c1262017-02-06 19:59:33 +0000569 // For HLoadClass/kBssEntry, store the resolved Class to the BSS entry.
570 if (is_load_class_bss_entry) {
571 if (call_saves_everything_except_r0) {
572 // The class entry address was preserved in `entry_address` thanks to kSaveEverything.
573 __ Str(r0, MemOperand(entry_address));
574 } else {
575 // For non-Baker read barrier, we need to re-calculate the address of the string entry.
576 UseScratchRegisterScope temps(
577 down_cast<CodeGeneratorARMVIXL*>(codegen)->GetVIXLAssembler());
578 vixl32::Register temp = temps.Acquire();
579 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
580 arm_codegen->NewTypeBssEntryPatch(cls_->GetDexFile(), type_index);
581 arm_codegen->EmitMovwMovtPlaceholder(labels, temp);
582 __ Str(r0, MemOperand(temp));
583 }
584 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100585 // Move the class to the desired location.
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100586 if (out.IsValid()) {
587 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
588 arm_codegen->Move32(locations->Out(), LocationFrom(r0));
589 }
590 RestoreLiveRegisters(codegen, locations);
591 __ B(GetExitLabel());
592 }
593
594 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathARMVIXL"; }
595
596 private:
597 // The class this slow path will load.
598 HLoadClass* const cls_;
599
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100600 // The dex PC of `at_`.
601 const uint32_t dex_pc_;
602
603 // Whether to initialize the class.
604 const bool do_clinit_;
605
606 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARMVIXL);
607};
608
Artem Serovd4cc5b22016-11-04 11:19:09 +0000609class LoadStringSlowPathARMVIXL : public SlowPathCodeARMVIXL {
610 public:
611 explicit LoadStringSlowPathARMVIXL(HLoadString* instruction)
612 : SlowPathCodeARMVIXL(instruction) {}
613
614 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Vladimir Markoea4c1262017-02-06 19:59:33 +0000615 DCHECK(instruction_->IsLoadString());
616 DCHECK_EQ(instruction_->AsLoadString()->GetLoadKind(), HLoadString::LoadKind::kBssEntry);
Artem Serovd4cc5b22016-11-04 11:19:09 +0000617 LocationSummary* locations = instruction_->GetLocations();
618 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
619 HLoadString* load = instruction_->AsLoadString();
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000620 const dex::StringIndex string_index = load->GetStringIndex();
Artem Serovd4cc5b22016-11-04 11:19:09 +0000621 vixl32::Register out = OutputRegister(load);
Artem Serovd4cc5b22016-11-04 11:19:09 +0000622 constexpr bool call_saves_everything_except_r0 = (!kUseReadBarrier || kUseBakerReadBarrier);
623
624 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
625 __ Bind(GetEntryLabel());
626 SaveLiveRegisters(codegen, locations);
627
628 InvokeRuntimeCallingConventionARMVIXL calling_convention;
629 // In the unlucky case that the `temp` is R0, we preserve the address in `out` across
Vladimir Markoea4c1262017-02-06 19:59:33 +0000630 // the kSaveEverything call.
631 vixl32::Register entry_address;
632 if (call_saves_everything_except_r0) {
633 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
634 bool temp_is_r0 = (temp.Is(calling_convention.GetRegisterAt(0)));
635 entry_address = temp_is_r0 ? out : temp;
636 DCHECK(!entry_address.Is(calling_convention.GetRegisterAt(0)));
637 if (temp_is_r0) {
638 __ Mov(entry_address, temp);
639 }
Artem Serovd4cc5b22016-11-04 11:19:09 +0000640 }
641
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000642 __ Mov(calling_convention.GetRegisterAt(0), string_index.index_);
Artem Serovd4cc5b22016-11-04 11:19:09 +0000643 arm_codegen->InvokeRuntime(kQuickResolveString, instruction_, instruction_->GetDexPc(), this);
644 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
645
646 // Store the resolved String to the .bss entry.
647 if (call_saves_everything_except_r0) {
648 // The string entry address was preserved in `entry_address` thanks to kSaveEverything.
649 __ Str(r0, MemOperand(entry_address));
650 } else {
651 // For non-Baker read barrier, we need to re-calculate the address of the string entry.
Vladimir Markoea4c1262017-02-06 19:59:33 +0000652 UseScratchRegisterScope temps(
653 down_cast<CodeGeneratorARMVIXL*>(codegen)->GetVIXLAssembler());
654 vixl32::Register temp = temps.Acquire();
Artem Serovd4cc5b22016-11-04 11:19:09 +0000655 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +0100656 arm_codegen->NewStringBssEntryPatch(load->GetDexFile(), string_index);
Vladimir Markoea4c1262017-02-06 19:59:33 +0000657 arm_codegen->EmitMovwMovtPlaceholder(labels, temp);
658 __ Str(r0, MemOperand(temp));
Artem Serovd4cc5b22016-11-04 11:19:09 +0000659 }
660
661 arm_codegen->Move32(locations->Out(), LocationFrom(r0));
662 RestoreLiveRegisters(codegen, locations);
663
664 __ B(GetExitLabel());
665 }
666
667 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathARMVIXL"; }
668
669 private:
670 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARMVIXL);
671};
672
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100673class TypeCheckSlowPathARMVIXL : public SlowPathCodeARMVIXL {
674 public:
675 TypeCheckSlowPathARMVIXL(HInstruction* instruction, bool is_fatal)
676 : SlowPathCodeARMVIXL(instruction), is_fatal_(is_fatal) {}
677
678 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
679 LocationSummary* locations = instruction_->GetLocations();
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100680 DCHECK(instruction_->IsCheckCast()
681 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
682
683 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
684 __ Bind(GetEntryLabel());
685
686 if (!is_fatal_) {
Artem Serovcfbe9132016-10-14 15:58:56 +0100687 SaveLiveRegisters(codegen, locations);
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100688 }
689
690 // We're moving two locations to locations that could overlap, so we need a parallel
691 // move resolver.
692 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100693
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800694 codegen->EmitParallelMoves(locations->InAt(0),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800695 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100696 DataType::Type::kReference,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800697 locations->InAt(1),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800698 LocationFrom(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100699 DataType::Type::kReference);
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100700 if (instruction_->IsInstanceOf()) {
Artem Serovcfbe9132016-10-14 15:58:56 +0100701 arm_codegen->InvokeRuntime(kQuickInstanceofNonTrivial,
702 instruction_,
703 instruction_->GetDexPc(),
704 this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800705 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Artem Serovcfbe9132016-10-14 15:58:56 +0100706 arm_codegen->Move32(locations->Out(), LocationFrom(r0));
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100707 } else {
708 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800709 arm_codegen->InvokeRuntime(kQuickCheckInstanceOf,
710 instruction_,
711 instruction_->GetDexPc(),
712 this);
713 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100714 }
715
716 if (!is_fatal_) {
Artem Serovcfbe9132016-10-14 15:58:56 +0100717 RestoreLiveRegisters(codegen, locations);
718 __ B(GetExitLabel());
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100719 }
720 }
721
722 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathARMVIXL"; }
723
724 bool IsFatal() const OVERRIDE { return is_fatal_; }
725
726 private:
727 const bool is_fatal_;
728
729 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARMVIXL);
730};
731
Scott Wakelingc34dba72016-10-03 10:14:44 +0100732class DeoptimizationSlowPathARMVIXL : public SlowPathCodeARMVIXL {
733 public:
734 explicit DeoptimizationSlowPathARMVIXL(HDeoptimize* instruction)
735 : SlowPathCodeARMVIXL(instruction) {}
736
737 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
738 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
739 __ Bind(GetEntryLabel());
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100740 LocationSummary* locations = instruction_->GetLocations();
741 SaveLiveRegisters(codegen, locations);
742 InvokeRuntimeCallingConventionARMVIXL calling_convention;
743 __ Mov(calling_convention.GetRegisterAt(0),
744 static_cast<uint32_t>(instruction_->AsDeoptimize()->GetDeoptimizationKind()));
745
Scott Wakelingc34dba72016-10-03 10:14:44 +0100746 arm_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100747 CheckEntrypointTypes<kQuickDeoptimize, void, DeoptimizationKind>();
Scott Wakelingc34dba72016-10-03 10:14:44 +0100748 }
749
750 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathARMVIXL"; }
751
752 private:
753 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARMVIXL);
754};
755
756class ArraySetSlowPathARMVIXL : public SlowPathCodeARMVIXL {
757 public:
758 explicit ArraySetSlowPathARMVIXL(HInstruction* instruction) : SlowPathCodeARMVIXL(instruction) {}
759
760 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
761 LocationSummary* locations = instruction_->GetLocations();
762 __ Bind(GetEntryLabel());
763 SaveLiveRegisters(codegen, locations);
764
765 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100766 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Scott Wakelingc34dba72016-10-03 10:14:44 +0100767 parallel_move.AddMove(
768 locations->InAt(0),
769 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100770 DataType::Type::kReference,
Scott Wakelingc34dba72016-10-03 10:14:44 +0100771 nullptr);
772 parallel_move.AddMove(
773 locations->InAt(1),
774 LocationFrom(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100775 DataType::Type::kInt32,
Scott Wakelingc34dba72016-10-03 10:14:44 +0100776 nullptr);
777 parallel_move.AddMove(
778 locations->InAt(2),
779 LocationFrom(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100780 DataType::Type::kReference,
Scott Wakelingc34dba72016-10-03 10:14:44 +0100781 nullptr);
782 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
783
784 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
785 arm_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
786 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
787 RestoreLiveRegisters(codegen, locations);
788 __ B(GetExitLabel());
789 }
790
791 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathARMVIXL"; }
792
793 private:
794 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathARMVIXL);
795};
796
Roland Levillain54f869e2017-03-06 13:54:11 +0000797// Abstract base class for read barrier slow paths marking a reference
798// `ref`.
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000799//
Roland Levillain54f869e2017-03-06 13:54:11 +0000800// Argument `entrypoint` must be a register location holding the read
Roland Levillain6d729a72017-06-30 18:34:01 +0100801// barrier marking runtime entry point to be invoked or an empty
802// location; in the latter case, the read barrier marking runtime
803// entry point will be loaded by the slow path code itself.
Roland Levillain54f869e2017-03-06 13:54:11 +0000804class ReadBarrierMarkSlowPathBaseARMVIXL : public SlowPathCodeARMVIXL {
805 protected:
806 ReadBarrierMarkSlowPathBaseARMVIXL(HInstruction* instruction, Location ref, Location entrypoint)
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000807 : SlowPathCodeARMVIXL(instruction), ref_(ref), entrypoint_(entrypoint) {
808 DCHECK(kEmitCompilerReadBarrier);
809 }
810
Roland Levillain54f869e2017-03-06 13:54:11 +0000811 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathBaseARMVIXL"; }
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000812
Roland Levillain54f869e2017-03-06 13:54:11 +0000813 // Generate assembly code calling the read barrier marking runtime
814 // entry point (ReadBarrierMarkRegX).
815 void GenerateReadBarrierMarkRuntimeCall(CodeGenerator* codegen) {
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000816 vixl32::Register ref_reg = RegisterFrom(ref_);
Roland Levillain47b3ab22017-02-27 14:31:35 +0000817
Roland Levillain47b3ab22017-02-27 14:31:35 +0000818 // No need to save live registers; it's taken care of by the
819 // entrypoint. Also, there is no need to update the stack mask,
820 // as this runtime call will not trigger a garbage collection.
821 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
822 DCHECK(!ref_reg.Is(sp));
823 DCHECK(!ref_reg.Is(lr));
824 DCHECK(!ref_reg.Is(pc));
825 // IP is used internally by the ReadBarrierMarkRegX entry point
826 // as a temporary, it cannot be the entry point's input/output.
827 DCHECK(!ref_reg.Is(ip));
828 DCHECK(ref_reg.IsRegister()) << ref_reg;
829 // "Compact" slow path, saving two moves.
830 //
831 // Instead of using the standard runtime calling convention (input
832 // and output in R0):
833 //
834 // R0 <- ref
835 // R0 <- ReadBarrierMark(R0)
836 // ref <- R0
837 //
838 // we just use rX (the register containing `ref`) as input and output
839 // of a dedicated entrypoint:
840 //
841 // rX <- ReadBarrierMarkRegX(rX)
842 //
843 if (entrypoint_.IsValid()) {
844 arm_codegen->ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction_, this);
845 __ Blx(RegisterFrom(entrypoint_));
846 } else {
Roland Levillain54f869e2017-03-06 13:54:11 +0000847 // Entrypoint is not already loaded, load from the thread.
Roland Levillain47b3ab22017-02-27 14:31:35 +0000848 int32_t entry_point_offset =
Roland Levillain97c46462017-05-11 14:04:03 +0100849 Thread::ReadBarrierMarkEntryPointsOffset<kArmPointerSize>(ref_reg.GetCode());
Roland Levillain47b3ab22017-02-27 14:31:35 +0000850 // This runtime call does not require a stack map.
851 arm_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
852 }
Roland Levillain47b3ab22017-02-27 14:31:35 +0000853 }
854
Roland Levillain47b3ab22017-02-27 14:31:35 +0000855 // The location (register) of the marked object reference.
856 const Location ref_;
857
858 // The location of the entrypoint if already loaded.
859 const Location entrypoint_;
860
Roland Levillain54f869e2017-03-06 13:54:11 +0000861 private:
862 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathBaseARMVIXL);
Roland Levillain47b3ab22017-02-27 14:31:35 +0000863};
864
Scott Wakelingc34dba72016-10-03 10:14:44 +0100865// Slow path marking an object reference `ref` during a read
866// barrier. The field `obj.field` in the object `obj` holding this
Roland Levillain54f869e2017-03-06 13:54:11 +0000867// reference does not get updated by this slow path after marking.
Roland Levillain47b3ab22017-02-27 14:31:35 +0000868//
Scott Wakelingc34dba72016-10-03 10:14:44 +0100869// This means that after the execution of this slow path, `ref` will
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000870// always be up-to-date, but `obj.field` may not; i.e., after the
871// flip, `ref` will be a to-space reference, but `obj.field` will
872// probably still be a from-space reference (unless it gets updated by
873// another thread, or if another thread installed another object
874// reference (different from `ref`) in `obj.field`).
Roland Levillainba650a42017-03-06 13:52:32 +0000875//
Roland Levillain6d729a72017-06-30 18:34:01 +0100876// Argument `entrypoint` must be a register location holding the read
877// barrier marking runtime entry point to be invoked or an empty
878// location; in the latter case, the read barrier marking runtime
879// entry point will be loaded by the slow path code itself.
Roland Levillain54f869e2017-03-06 13:54:11 +0000880class ReadBarrierMarkSlowPathARMVIXL : public ReadBarrierMarkSlowPathBaseARMVIXL {
Roland Levillain47b3ab22017-02-27 14:31:35 +0000881 public:
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000882 ReadBarrierMarkSlowPathARMVIXL(HInstruction* instruction,
883 Location ref,
884 Location entrypoint = Location::NoLocation())
Roland Levillain54f869e2017-03-06 13:54:11 +0000885 : ReadBarrierMarkSlowPathBaseARMVIXL(instruction, ref, entrypoint) {
Roland Levillain47b3ab22017-02-27 14:31:35 +0000886 DCHECK(kEmitCompilerReadBarrier);
887 }
888
Roland Levillain47b3ab22017-02-27 14:31:35 +0000889 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathARMVIXL"; }
890
891 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
892 LocationSummary* locations = instruction_->GetLocations();
Roland Levillain54f869e2017-03-06 13:54:11 +0000893 DCHECK(locations->CanCall());
894 DCHECK(ref_.IsRegister()) << ref_;
895 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_.reg())) << ref_.reg();
896 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
897 << "Unexpected instruction in read barrier marking slow path: "
898 << instruction_->DebugName();
899
900 __ Bind(GetEntryLabel());
901 GenerateReadBarrierMarkRuntimeCall(codegen);
902 __ B(GetExitLabel());
903 }
904
905 private:
906 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathARMVIXL);
907};
908
909// Slow path loading `obj`'s lock word, loading a reference from
910// object `*(obj + offset + (index << scale_factor))` into `ref`, and
911// marking `ref` if `obj` is gray according to the lock word (Baker
912// read barrier). The field `obj.field` in the object `obj` holding
913// this reference does not get updated by this slow path after marking
914// (see LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARMVIXL
915// below for that).
916//
917// This means that after the execution of this slow path, `ref` will
918// always be up-to-date, but `obj.field` may not; i.e., after the
919// flip, `ref` will be a to-space reference, but `obj.field` will
920// probably still be a from-space reference (unless it gets updated by
921// another thread, or if another thread installed another object
922// reference (different from `ref`) in `obj.field`).
923//
924// Argument `entrypoint` must be a register location holding the read
Roland Levillain6d729a72017-06-30 18:34:01 +0100925// barrier marking runtime entry point to be invoked or an empty
926// location; in the latter case, the read barrier marking runtime
927// entry point will be loaded by the slow path code itself.
Roland Levillain54f869e2017-03-06 13:54:11 +0000928class LoadReferenceWithBakerReadBarrierSlowPathARMVIXL : public ReadBarrierMarkSlowPathBaseARMVIXL {
929 public:
930 LoadReferenceWithBakerReadBarrierSlowPathARMVIXL(HInstruction* instruction,
931 Location ref,
932 vixl32::Register obj,
933 uint32_t offset,
934 Location index,
935 ScaleFactor scale_factor,
936 bool needs_null_check,
937 vixl32::Register temp,
Roland Levillain6d729a72017-06-30 18:34:01 +0100938 Location entrypoint = Location::NoLocation())
Roland Levillain54f869e2017-03-06 13:54:11 +0000939 : ReadBarrierMarkSlowPathBaseARMVIXL(instruction, ref, entrypoint),
940 obj_(obj),
941 offset_(offset),
942 index_(index),
943 scale_factor_(scale_factor),
944 needs_null_check_(needs_null_check),
945 temp_(temp) {
946 DCHECK(kEmitCompilerReadBarrier);
947 DCHECK(kUseBakerReadBarrier);
948 }
949
Roland Levillain47b3ab22017-02-27 14:31:35 +0000950 const char* GetDescription() const OVERRIDE {
Roland Levillain54f869e2017-03-06 13:54:11 +0000951 return "LoadReferenceWithBakerReadBarrierSlowPathARMVIXL";
Roland Levillain47b3ab22017-02-27 14:31:35 +0000952 }
953
954 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
955 LocationSummary* locations = instruction_->GetLocations();
956 vixl32::Register ref_reg = RegisterFrom(ref_);
957 DCHECK(locations->CanCall());
958 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg.GetCode())) << ref_reg;
Roland Levillain47b3ab22017-02-27 14:31:35 +0000959 DCHECK(instruction_->IsInstanceFieldGet() ||
960 instruction_->IsStaticFieldGet() ||
961 instruction_->IsArrayGet() ||
962 instruction_->IsArraySet() ||
Roland Levillain47b3ab22017-02-27 14:31:35 +0000963 instruction_->IsInstanceOf() ||
964 instruction_->IsCheckCast() ||
965 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
966 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
967 << "Unexpected instruction in read barrier marking slow path: "
968 << instruction_->DebugName();
969 // The read barrier instrumentation of object ArrayGet
970 // instructions does not support the HIntermediateAddress
971 // instruction.
972 DCHECK(!(instruction_->IsArrayGet() &&
973 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
974
Roland Levillain54f869e2017-03-06 13:54:11 +0000975 // Temporary register `temp_`, used to store the lock word, must
976 // not be IP, as we may use it to emit the reference load (in the
977 // call to GenerateRawReferenceLoad below), and we need the lock
978 // word to still be in `temp_` after the reference load.
979 DCHECK(!temp_.Is(ip));
980
Roland Levillain47b3ab22017-02-27 14:31:35 +0000981 __ Bind(GetEntryLabel());
Roland Levillain54f869e2017-03-06 13:54:11 +0000982
983 // When using MaybeGenerateReadBarrierSlow, the read barrier call is
984 // inserted after the original load. However, in fast path based
985 // Baker's read barriers, we need to perform the load of
986 // mirror::Object::monitor_ *before* the original reference load.
987 // This load-load ordering is required by the read barrier.
Roland Levillainff487002017-03-07 16:50:01 +0000988 // The slow path (for Baker's algorithm) should look like:
Roland Levillain54f869e2017-03-06 13:54:11 +0000989 //
990 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
991 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
992 // HeapReference<mirror::Object> ref = *src; // Original reference load.
993 // bool is_gray = (rb_state == ReadBarrier::GrayState());
994 // if (is_gray) {
995 // ref = entrypoint(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
996 // }
997 //
998 // Note: the original implementation in ReadBarrier::Barrier is
999 // slightly more complex as it performs additional checks that we do
1000 // not do here for performance reasons.
1001
Roland Levillain47b3ab22017-02-27 14:31:35 +00001002 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
Roland Levillain54f869e2017-03-06 13:54:11 +00001003
1004 // /* int32_t */ monitor = obj->monitor_
1005 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
1006 arm_codegen->GetAssembler()->LoadFromOffset(kLoadWord, temp_, obj_, monitor_offset);
1007 if (needs_null_check_) {
1008 codegen->MaybeRecordImplicitNullCheck(instruction_);
Roland Levillain47b3ab22017-02-27 14:31:35 +00001009 }
Roland Levillain54f869e2017-03-06 13:54:11 +00001010 // /* LockWord */ lock_word = LockWord(monitor)
1011 static_assert(sizeof(LockWord) == sizeof(int32_t),
1012 "art::LockWord and int32_t have different sizes.");
1013
1014 // Introduce a dependency on the lock_word including the rb_state,
1015 // which shall prevent load-load reordering without using
1016 // a memory barrier (which would be more expensive).
1017 // `obj` is unchanged by this operation, but its value now depends
1018 // on `temp`.
1019 __ Add(obj_, obj_, Operand(temp_, ShiftType::LSR, 32));
1020
1021 // The actual reference load.
1022 // A possible implicit null check has already been handled above.
1023 arm_codegen->GenerateRawReferenceLoad(
1024 instruction_, ref_, obj_, offset_, index_, scale_factor_, /* needs_null_check */ false);
1025
1026 // Mark the object `ref` when `obj` is gray.
1027 //
1028 // if (rb_state == ReadBarrier::GrayState())
1029 // ref = ReadBarrier::Mark(ref);
1030 //
1031 // Given the numeric representation, it's enough to check the low bit of the
1032 // rb_state. We do that by shifting the bit out of the lock word with LSRS
1033 // which can be a 16-bit instruction unlike the TST immediate.
1034 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
1035 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
1036 __ Lsrs(temp_, temp_, LockWord::kReadBarrierStateShift + 1);
1037 __ B(cc, GetExitLabel()); // Carry flag is the last bit shifted out by LSRS.
1038 GenerateReadBarrierMarkRuntimeCall(codegen);
1039
Roland Levillain47b3ab22017-02-27 14:31:35 +00001040 __ B(GetExitLabel());
1041 }
1042
1043 private:
Roland Levillain54f869e2017-03-06 13:54:11 +00001044 // The register containing the object holding the marked object reference field.
1045 vixl32::Register obj_;
1046 // The offset, index and scale factor to access the reference in `obj_`.
1047 uint32_t offset_;
1048 Location index_;
1049 ScaleFactor scale_factor_;
1050 // Is a null check required?
1051 bool needs_null_check_;
1052 // A temporary register used to hold the lock word of `obj_`.
1053 vixl32::Register temp_;
Roland Levillain47b3ab22017-02-27 14:31:35 +00001054
Roland Levillain54f869e2017-03-06 13:54:11 +00001055 DISALLOW_COPY_AND_ASSIGN(LoadReferenceWithBakerReadBarrierSlowPathARMVIXL);
Roland Levillain47b3ab22017-02-27 14:31:35 +00001056};
1057
Roland Levillain54f869e2017-03-06 13:54:11 +00001058// Slow path loading `obj`'s lock word, loading a reference from
1059// object `*(obj + offset + (index << scale_factor))` into `ref`, and
1060// marking `ref` if `obj` is gray according to the lock word (Baker
1061// read barrier). If needed, this slow path also atomically updates
1062// the field `obj.field` in the object `obj` holding this reference
1063// after marking (contrary to
1064// LoadReferenceWithBakerReadBarrierSlowPathARMVIXL above, which never
1065// tries to update `obj.field`).
Roland Levillain47b3ab22017-02-27 14:31:35 +00001066//
1067// This means that after the execution of this slow path, both `ref`
1068// and `obj.field` will be up-to-date; i.e., after the flip, both will
1069// hold the same to-space reference (unless another thread installed
1070// another object reference (different from `ref`) in `obj.field`).
Roland Levillainba650a42017-03-06 13:52:32 +00001071//
Roland Levillain54f869e2017-03-06 13:54:11 +00001072// Argument `entrypoint` must be a register location holding the read
Roland Levillain6d729a72017-06-30 18:34:01 +01001073// barrier marking runtime entry point to be invoked or an empty
1074// location; in the latter case, the read barrier marking runtime
1075// entry point will be loaded by the slow path code itself.
Roland Levillain54f869e2017-03-06 13:54:11 +00001076class LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARMVIXL
1077 : public ReadBarrierMarkSlowPathBaseARMVIXL {
Roland Levillain47b3ab22017-02-27 14:31:35 +00001078 public:
Roland Levillain6d729a72017-06-30 18:34:01 +01001079 LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARMVIXL(
1080 HInstruction* instruction,
1081 Location ref,
1082 vixl32::Register obj,
1083 uint32_t offset,
1084 Location index,
1085 ScaleFactor scale_factor,
1086 bool needs_null_check,
1087 vixl32::Register temp1,
1088 vixl32::Register temp2,
1089 Location entrypoint = Location::NoLocation())
Roland Levillain54f869e2017-03-06 13:54:11 +00001090 : ReadBarrierMarkSlowPathBaseARMVIXL(instruction, ref, entrypoint),
Roland Levillain47b3ab22017-02-27 14:31:35 +00001091 obj_(obj),
Roland Levillain54f869e2017-03-06 13:54:11 +00001092 offset_(offset),
1093 index_(index),
1094 scale_factor_(scale_factor),
1095 needs_null_check_(needs_null_check),
Roland Levillain47b3ab22017-02-27 14:31:35 +00001096 temp1_(temp1),
Roland Levillain54f869e2017-03-06 13:54:11 +00001097 temp2_(temp2) {
Roland Levillain47b3ab22017-02-27 14:31:35 +00001098 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain54f869e2017-03-06 13:54:11 +00001099 DCHECK(kUseBakerReadBarrier);
Roland Levillain47b3ab22017-02-27 14:31:35 +00001100 }
1101
1102 const char* GetDescription() const OVERRIDE {
Roland Levillain54f869e2017-03-06 13:54:11 +00001103 return "LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARMVIXL";
Roland Levillain47b3ab22017-02-27 14:31:35 +00001104 }
1105
1106 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
1107 LocationSummary* locations = instruction_->GetLocations();
1108 vixl32::Register ref_reg = RegisterFrom(ref_);
1109 DCHECK(locations->CanCall());
1110 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg.GetCode())) << ref_reg;
Roland Levillain54f869e2017-03-06 13:54:11 +00001111 DCHECK_NE(ref_.reg(), LocationFrom(temp1_).reg());
1112
1113 // This slow path is only used by the UnsafeCASObject intrinsic at the moment.
Roland Levillain47b3ab22017-02-27 14:31:35 +00001114 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
1115 << "Unexpected instruction in read barrier marking and field updating slow path: "
1116 << instruction_->DebugName();
1117 DCHECK(instruction_->GetLocations()->Intrinsified());
1118 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
Roland Levillain54f869e2017-03-06 13:54:11 +00001119 DCHECK_EQ(offset_, 0u);
1120 DCHECK_EQ(scale_factor_, ScaleFactor::TIMES_1);
1121 Location field_offset = index_;
1122 DCHECK(field_offset.IsRegisterPair()) << field_offset;
1123
1124 // Temporary register `temp1_`, used to store the lock word, must
1125 // not be IP, as we may use it to emit the reference load (in the
1126 // call to GenerateRawReferenceLoad below), and we need the lock
1127 // word to still be in `temp1_` after the reference load.
1128 DCHECK(!temp1_.Is(ip));
Roland Levillain47b3ab22017-02-27 14:31:35 +00001129
1130 __ Bind(GetEntryLabel());
1131
Roland Levillainff487002017-03-07 16:50:01 +00001132 // The implementation is similar to LoadReferenceWithBakerReadBarrierSlowPathARMVIXL's:
1133 //
1134 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
1135 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
1136 // HeapReference<mirror::Object> ref = *src; // Original reference load.
1137 // bool is_gray = (rb_state == ReadBarrier::GrayState());
1138 // if (is_gray) {
1139 // old_ref = ref;
1140 // ref = entrypoint(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
1141 // compareAndSwapObject(obj, field_offset, old_ref, ref);
1142 // }
1143
Roland Levillain54f869e2017-03-06 13:54:11 +00001144 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
1145
1146 // /* int32_t */ monitor = obj->monitor_
1147 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
1148 arm_codegen->GetAssembler()->LoadFromOffset(kLoadWord, temp1_, obj_, monitor_offset);
1149 if (needs_null_check_) {
1150 codegen->MaybeRecordImplicitNullCheck(instruction_);
1151 }
1152 // /* LockWord */ lock_word = LockWord(monitor)
1153 static_assert(sizeof(LockWord) == sizeof(int32_t),
1154 "art::LockWord and int32_t have different sizes.");
1155
1156 // Introduce a dependency on the lock_word including the rb_state,
1157 // which shall prevent load-load reordering without using
1158 // a memory barrier (which would be more expensive).
1159 // `obj` is unchanged by this operation, but its value now depends
1160 // on `temp`.
1161 __ Add(obj_, obj_, Operand(temp1_, ShiftType::LSR, 32));
1162
1163 // The actual reference load.
1164 // A possible implicit null check has already been handled above.
1165 arm_codegen->GenerateRawReferenceLoad(
1166 instruction_, ref_, obj_, offset_, index_, scale_factor_, /* needs_null_check */ false);
1167
1168 // Mark the object `ref` when `obj` is gray.
1169 //
1170 // if (rb_state == ReadBarrier::GrayState())
1171 // ref = ReadBarrier::Mark(ref);
1172 //
1173 // Given the numeric representation, it's enough to check the low bit of the
1174 // rb_state. We do that by shifting the bit out of the lock word with LSRS
1175 // which can be a 16-bit instruction unlike the TST immediate.
1176 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
1177 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
1178 __ Lsrs(temp1_, temp1_, LockWord::kReadBarrierStateShift + 1);
1179 __ B(cc, GetExitLabel()); // Carry flag is the last bit shifted out by LSRS.
1180
1181 // Save the old value of the reference before marking it.
Roland Levillain47b3ab22017-02-27 14:31:35 +00001182 // Note that we cannot use IP to save the old reference, as IP is
1183 // used internally by the ReadBarrierMarkRegX entry point, and we
1184 // need the old reference after the call to that entry point.
1185 DCHECK(!temp1_.Is(ip));
1186 __ Mov(temp1_, ref_reg);
Roland Levillain27b1f9c2017-01-17 16:56:34 +00001187
Roland Levillain54f869e2017-03-06 13:54:11 +00001188 GenerateReadBarrierMarkRuntimeCall(codegen);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001189
1190 // If the new reference is different from the old reference,
Roland Levillain54f869e2017-03-06 13:54:11 +00001191 // update the field in the holder (`*(obj_ + field_offset)`).
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001192 //
1193 // Note that this field could also hold a different object, if
1194 // another thread had concurrently changed it. In that case, the
1195 // LDREX/SUBS/ITNE sequence of instructions in the compare-and-set
1196 // (CAS) operation below would abort the CAS, leaving the field
1197 // as-is.
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001198 __ Cmp(temp1_, ref_reg);
Roland Levillain54f869e2017-03-06 13:54:11 +00001199 __ B(eq, GetExitLabel());
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001200
1201 // Update the the holder's field atomically. This may fail if
1202 // mutator updates before us, but it's OK. This is achieved
1203 // using a strong compare-and-set (CAS) operation with relaxed
1204 // memory synchronization ordering, where the expected value is
1205 // the old reference and the desired value is the new reference.
1206
1207 UseScratchRegisterScope temps(arm_codegen->GetVIXLAssembler());
1208 // Convenience aliases.
1209 vixl32::Register base = obj_;
1210 // The UnsafeCASObject intrinsic uses a register pair as field
1211 // offset ("long offset"), of which only the low part contains
1212 // data.
Roland Levillain54f869e2017-03-06 13:54:11 +00001213 vixl32::Register offset = LowRegisterFrom(field_offset);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001214 vixl32::Register expected = temp1_;
1215 vixl32::Register value = ref_reg;
1216 vixl32::Register tmp_ptr = temps.Acquire(); // Pointer to actual memory.
1217 vixl32::Register tmp = temp2_; // Value in memory.
1218
1219 __ Add(tmp_ptr, base, offset);
1220
1221 if (kPoisonHeapReferences) {
1222 arm_codegen->GetAssembler()->PoisonHeapReference(expected);
1223 if (value.Is(expected)) {
1224 // Do not poison `value`, as it is the same register as
1225 // `expected`, which has just been poisoned.
1226 } else {
1227 arm_codegen->GetAssembler()->PoisonHeapReference(value);
1228 }
1229 }
1230
1231 // do {
1232 // tmp = [r_ptr] - expected;
1233 // } while (tmp == 0 && failure([r_ptr] <- r_new_value));
1234
1235 vixl32::Label loop_head, exit_loop;
1236 __ Bind(&loop_head);
1237
1238 __ Ldrex(tmp, MemOperand(tmp_ptr));
1239
1240 __ Subs(tmp, tmp, expected);
1241
1242 {
Artem Serov0fb37192016-12-06 18:13:40 +00001243 ExactAssemblyScope aas(arm_codegen->GetVIXLAssembler(),
1244 2 * kMaxInstructionSizeInBytes,
1245 CodeBufferCheckScope::kMaximumSize);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001246
1247 __ it(ne);
1248 __ clrex(ne);
1249 }
1250
Artem Serov517d9f62016-12-12 15:51:15 +00001251 __ B(ne, &exit_loop, /* far_target */ false);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001252
1253 __ Strex(tmp, value, MemOperand(tmp_ptr));
1254 __ Cmp(tmp, 1);
Artem Serov517d9f62016-12-12 15:51:15 +00001255 __ B(eq, &loop_head, /* far_target */ false);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001256
1257 __ Bind(&exit_loop);
1258
1259 if (kPoisonHeapReferences) {
1260 arm_codegen->GetAssembler()->UnpoisonHeapReference(expected);
1261 if (value.Is(expected)) {
1262 // Do not unpoison `value`, as it is the same register as
1263 // `expected`, which has just been unpoisoned.
1264 } else {
1265 arm_codegen->GetAssembler()->UnpoisonHeapReference(value);
1266 }
1267 }
1268
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001269 __ B(GetExitLabel());
1270 }
1271
1272 private:
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001273 // The register containing the object holding the marked object reference field.
1274 const vixl32::Register obj_;
Roland Levillain54f869e2017-03-06 13:54:11 +00001275 // The offset, index and scale factor to access the reference in `obj_`.
1276 uint32_t offset_;
1277 Location index_;
1278 ScaleFactor scale_factor_;
1279 // Is a null check required?
1280 bool needs_null_check_;
1281 // A temporary register used to hold the lock word of `obj_`; and
1282 // also to hold the original reference value, when the reference is
1283 // marked.
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001284 const vixl32::Register temp1_;
Roland Levillain54f869e2017-03-06 13:54:11 +00001285 // A temporary register used in the implementation of the CAS, to
1286 // update the object's reference field.
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001287 const vixl32::Register temp2_;
1288
Roland Levillain54f869e2017-03-06 13:54:11 +00001289 DISALLOW_COPY_AND_ASSIGN(LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARMVIXL);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001290};
1291
1292// Slow path generating a read barrier for a heap reference.
1293class ReadBarrierForHeapReferenceSlowPathARMVIXL : public SlowPathCodeARMVIXL {
1294 public:
1295 ReadBarrierForHeapReferenceSlowPathARMVIXL(HInstruction* instruction,
1296 Location out,
1297 Location ref,
1298 Location obj,
1299 uint32_t offset,
1300 Location index)
1301 : SlowPathCodeARMVIXL(instruction),
1302 out_(out),
1303 ref_(ref),
1304 obj_(obj),
1305 offset_(offset),
1306 index_(index) {
1307 DCHECK(kEmitCompilerReadBarrier);
1308 // If `obj` is equal to `out` or `ref`, it means the initial object
1309 // has been overwritten by (or after) the heap object reference load
1310 // to be instrumented, e.g.:
1311 //
1312 // __ LoadFromOffset(kLoadWord, out, out, offset);
1313 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
1314 //
1315 // In that case, we have lost the information about the original
1316 // object, and the emitted read barrier cannot work properly.
1317 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
1318 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
1319 }
1320
1321 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
1322 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
1323 LocationSummary* locations = instruction_->GetLocations();
1324 vixl32::Register reg_out = RegisterFrom(out_);
1325 DCHECK(locations->CanCall());
1326 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out.GetCode()));
1327 DCHECK(instruction_->IsInstanceFieldGet() ||
1328 instruction_->IsStaticFieldGet() ||
1329 instruction_->IsArrayGet() ||
1330 instruction_->IsInstanceOf() ||
1331 instruction_->IsCheckCast() ||
Andreas Gamped9911ee2017-03-27 13:27:24 -07001332 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001333 << "Unexpected instruction in read barrier for heap reference slow path: "
1334 << instruction_->DebugName();
1335 // The read barrier instrumentation of object ArrayGet
1336 // instructions does not support the HIntermediateAddress
1337 // instruction.
1338 DCHECK(!(instruction_->IsArrayGet() &&
1339 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
1340
1341 __ Bind(GetEntryLabel());
1342 SaveLiveRegisters(codegen, locations);
1343
1344 // We may have to change the index's value, but as `index_` is a
1345 // constant member (like other "inputs" of this slow path),
1346 // introduce a copy of it, `index`.
1347 Location index = index_;
1348 if (index_.IsValid()) {
1349 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
1350 if (instruction_->IsArrayGet()) {
1351 // Compute the actual memory offset and store it in `index`.
1352 vixl32::Register index_reg = RegisterFrom(index_);
1353 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg.GetCode()));
1354 if (codegen->IsCoreCalleeSaveRegister(index_reg.GetCode())) {
1355 // We are about to change the value of `index_reg` (see the
Roland Levillain9983e302017-07-14 14:34:22 +01001356 // calls to art::arm::ArmVIXLMacroAssembler::Lsl and
1357 // art::arm::ArmVIXLMacroAssembler::Add below), but it has
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001358 // not been saved by the previous call to
1359 // art::SlowPathCode::SaveLiveRegisters, as it is a
1360 // callee-save register --
1361 // art::SlowPathCode::SaveLiveRegisters does not consider
1362 // callee-save registers, as it has been designed with the
1363 // assumption that callee-save registers are supposed to be
1364 // handled by the called function. So, as a callee-save
1365 // register, `index_reg` _would_ eventually be saved onto
1366 // the stack, but it would be too late: we would have
1367 // changed its value earlier. Therefore, we manually save
1368 // it here into another freely available register,
1369 // `free_reg`, chosen of course among the caller-save
1370 // registers (as a callee-save `free_reg` register would
1371 // exhibit the same problem).
1372 //
1373 // Note we could have requested a temporary register from
1374 // the register allocator instead; but we prefer not to, as
1375 // this is a slow path, and we know we can find a
1376 // caller-save register that is available.
1377 vixl32::Register free_reg = FindAvailableCallerSaveRegister(codegen);
1378 __ Mov(free_reg, index_reg);
1379 index_reg = free_reg;
1380 index = LocationFrom(index_reg);
1381 } else {
1382 // The initial register stored in `index_` has already been
1383 // saved in the call to art::SlowPathCode::SaveLiveRegisters
1384 // (as it is not a callee-save register), so we can freely
1385 // use it.
1386 }
1387 // Shifting the index value contained in `index_reg` by the scale
1388 // factor (2) cannot overflow in practice, as the runtime is
1389 // unable to allocate object arrays with a size larger than
1390 // 2^26 - 1 (that is, 2^28 - 4 bytes).
1391 __ Lsl(index_reg, index_reg, TIMES_4);
1392 static_assert(
1393 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
1394 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
1395 __ Add(index_reg, index_reg, offset_);
1396 } else {
1397 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
1398 // intrinsics, `index_` is not shifted by a scale factor of 2
1399 // (as in the case of ArrayGet), as it is actually an offset
1400 // to an object field within an object.
1401 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
1402 DCHECK(instruction_->GetLocations()->Intrinsified());
1403 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
1404 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
1405 << instruction_->AsInvoke()->GetIntrinsic();
1406 DCHECK_EQ(offset_, 0U);
1407 DCHECK(index_.IsRegisterPair());
1408 // UnsafeGet's offset location is a register pair, the low
1409 // part contains the correct offset.
1410 index = index_.ToLow();
1411 }
1412 }
1413
1414 // We're moving two or three locations to locations that could
1415 // overlap, so we need a parallel move resolver.
1416 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +01001417 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001418 parallel_move.AddMove(ref_,
1419 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001420 DataType::Type::kReference,
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001421 nullptr);
1422 parallel_move.AddMove(obj_,
1423 LocationFrom(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001424 DataType::Type::kReference,
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001425 nullptr);
1426 if (index.IsValid()) {
1427 parallel_move.AddMove(index,
1428 LocationFrom(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001429 DataType::Type::kInt32,
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001430 nullptr);
1431 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
1432 } else {
1433 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
1434 __ Mov(calling_convention.GetRegisterAt(2), offset_);
1435 }
1436 arm_codegen->InvokeRuntime(kQuickReadBarrierSlow, instruction_, instruction_->GetDexPc(), this);
1437 CheckEntrypointTypes<
1438 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
1439 arm_codegen->Move32(out_, LocationFrom(r0));
1440
1441 RestoreLiveRegisters(codegen, locations);
1442 __ B(GetExitLabel());
1443 }
1444
1445 const char* GetDescription() const OVERRIDE {
1446 return "ReadBarrierForHeapReferenceSlowPathARMVIXL";
1447 }
1448
1449 private:
1450 vixl32::Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
1451 uint32_t ref = RegisterFrom(ref_).GetCode();
1452 uint32_t obj = RegisterFrom(obj_).GetCode();
1453 for (uint32_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
1454 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
1455 return vixl32::Register(i);
1456 }
1457 }
1458 // We shall never fail to find a free caller-save register, as
1459 // there are more than two core caller-save registers on ARM
1460 // (meaning it is possible to find one which is different from
1461 // `ref` and `obj`).
1462 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
1463 LOG(FATAL) << "Could not find a free caller-save register";
1464 UNREACHABLE();
1465 }
1466
1467 const Location out_;
1468 const Location ref_;
1469 const Location obj_;
1470 const uint32_t offset_;
1471 // An additional location containing an index to an array.
1472 // Only used for HArrayGet and the UnsafeGetObject &
1473 // UnsafeGetObjectVolatile intrinsics.
1474 const Location index_;
1475
1476 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathARMVIXL);
1477};
1478
1479// Slow path generating a read barrier for a GC root.
1480class ReadBarrierForRootSlowPathARMVIXL : public SlowPathCodeARMVIXL {
1481 public:
1482 ReadBarrierForRootSlowPathARMVIXL(HInstruction* instruction, Location out, Location root)
1483 : SlowPathCodeARMVIXL(instruction), out_(out), root_(root) {
1484 DCHECK(kEmitCompilerReadBarrier);
1485 }
1486
1487 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
1488 LocationSummary* locations = instruction_->GetLocations();
1489 vixl32::Register reg_out = RegisterFrom(out_);
1490 DCHECK(locations->CanCall());
1491 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out.GetCode()));
1492 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
1493 << "Unexpected instruction in read barrier for GC root slow path: "
1494 << instruction_->DebugName();
1495
1496 __ Bind(GetEntryLabel());
1497 SaveLiveRegisters(codegen, locations);
1498
1499 InvokeRuntimeCallingConventionARMVIXL calling_convention;
1500 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
1501 arm_codegen->Move32(LocationFrom(calling_convention.GetRegisterAt(0)), root_);
1502 arm_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
1503 instruction_,
1504 instruction_->GetDexPc(),
1505 this);
1506 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
1507 arm_codegen->Move32(out_, LocationFrom(r0));
1508
1509 RestoreLiveRegisters(codegen, locations);
1510 __ B(GetExitLabel());
1511 }
1512
1513 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathARMVIXL"; }
1514
1515 private:
1516 const Location out_;
1517 const Location root_;
1518
1519 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathARMVIXL);
1520};
Scott Wakelingc34dba72016-10-03 10:14:44 +01001521
Scott Wakelingfe885462016-09-22 10:24:38 +01001522inline vixl32::Condition ARMCondition(IfCondition cond) {
1523 switch (cond) {
1524 case kCondEQ: return eq;
1525 case kCondNE: return ne;
1526 case kCondLT: return lt;
1527 case kCondLE: return le;
1528 case kCondGT: return gt;
1529 case kCondGE: return ge;
1530 case kCondB: return lo;
1531 case kCondBE: return ls;
1532 case kCondA: return hi;
1533 case kCondAE: return hs;
1534 }
1535 LOG(FATAL) << "Unreachable";
1536 UNREACHABLE();
1537}
1538
1539// Maps signed condition to unsigned condition.
1540inline vixl32::Condition ARMUnsignedCondition(IfCondition cond) {
1541 switch (cond) {
1542 case kCondEQ: return eq;
1543 case kCondNE: return ne;
1544 // Signed to unsigned.
1545 case kCondLT: return lo;
1546 case kCondLE: return ls;
1547 case kCondGT: return hi;
1548 case kCondGE: return hs;
1549 // Unsigned remain unchanged.
1550 case kCondB: return lo;
1551 case kCondBE: return ls;
1552 case kCondA: return hi;
1553 case kCondAE: return hs;
1554 }
1555 LOG(FATAL) << "Unreachable";
1556 UNREACHABLE();
1557}
1558
1559inline vixl32::Condition ARMFPCondition(IfCondition cond, bool gt_bias) {
1560 // The ARM condition codes can express all the necessary branches, see the
1561 // "Meaning (floating-point)" column in the table A8-1 of the ARMv7 reference manual.
1562 // There is no dex instruction or HIR that would need the missing conditions
1563 // "equal or unordered" or "not equal".
1564 switch (cond) {
1565 case kCondEQ: return eq;
1566 case kCondNE: return ne /* unordered */;
1567 case kCondLT: return gt_bias ? cc : lt /* unordered */;
1568 case kCondLE: return gt_bias ? ls : le /* unordered */;
1569 case kCondGT: return gt_bias ? hi /* unordered */ : gt;
1570 case kCondGE: return gt_bias ? cs /* unordered */ : ge;
1571 default:
1572 LOG(FATAL) << "UNREACHABLE";
1573 UNREACHABLE();
1574 }
1575}
1576
Anton Kirilov74234da2017-01-13 14:42:47 +00001577inline ShiftType ShiftFromOpKind(HDataProcWithShifterOp::OpKind op_kind) {
1578 switch (op_kind) {
1579 case HDataProcWithShifterOp::kASR: return ShiftType::ASR;
1580 case HDataProcWithShifterOp::kLSL: return ShiftType::LSL;
1581 case HDataProcWithShifterOp::kLSR: return ShiftType::LSR;
1582 default:
1583 LOG(FATAL) << "Unexpected op kind " << op_kind;
1584 UNREACHABLE();
1585 }
1586}
1587
Scott Wakelingfe885462016-09-22 10:24:38 +01001588void CodeGeneratorARMVIXL::DumpCoreRegister(std::ostream& stream, int reg) const {
1589 stream << vixl32::Register(reg);
1590}
1591
1592void CodeGeneratorARMVIXL::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
1593 stream << vixl32::SRegister(reg);
1594}
1595
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001596static uint32_t ComputeSRegisterListMask(const SRegisterList& regs) {
Scott Wakelingfe885462016-09-22 10:24:38 +01001597 uint32_t mask = 0;
1598 for (uint32_t i = regs.GetFirstSRegister().GetCode();
1599 i <= regs.GetLastSRegister().GetCode();
1600 ++i) {
1601 mask |= (1 << i);
1602 }
1603 return mask;
1604}
1605
Artem Serovd4cc5b22016-11-04 11:19:09 +00001606// Saves the register in the stack. Returns the size taken on stack.
1607size_t CodeGeneratorARMVIXL::SaveCoreRegister(size_t stack_index ATTRIBUTE_UNUSED,
1608 uint32_t reg_id ATTRIBUTE_UNUSED) {
1609 TODO_VIXL32(FATAL);
1610 return 0;
1611}
1612
1613// Restores the register from the stack. Returns the size taken on stack.
1614size_t CodeGeneratorARMVIXL::RestoreCoreRegister(size_t stack_index ATTRIBUTE_UNUSED,
1615 uint32_t reg_id ATTRIBUTE_UNUSED) {
1616 TODO_VIXL32(FATAL);
1617 return 0;
1618}
1619
1620size_t CodeGeneratorARMVIXL::SaveFloatingPointRegister(size_t stack_index ATTRIBUTE_UNUSED,
1621 uint32_t reg_id ATTRIBUTE_UNUSED) {
1622 TODO_VIXL32(FATAL);
1623 return 0;
1624}
1625
1626size_t CodeGeneratorARMVIXL::RestoreFloatingPointRegister(size_t stack_index ATTRIBUTE_UNUSED,
1627 uint32_t reg_id ATTRIBUTE_UNUSED) {
1628 TODO_VIXL32(FATAL);
1629 return 0;
Anton Kirilove28d9ae2016-10-25 18:17:23 +01001630}
1631
Anton Kirilov74234da2017-01-13 14:42:47 +00001632static void GenerateDataProcInstruction(HInstruction::InstructionKind kind,
1633 vixl32::Register out,
1634 vixl32::Register first,
1635 const Operand& second,
1636 CodeGeneratorARMVIXL* codegen) {
1637 if (second.IsImmediate() && second.GetImmediate() == 0) {
1638 const Operand in = kind == HInstruction::kAnd
1639 ? Operand(0)
1640 : Operand(first);
1641
1642 __ Mov(out, in);
1643 } else {
1644 switch (kind) {
1645 case HInstruction::kAdd:
1646 __ Add(out, first, second);
1647 break;
1648 case HInstruction::kAnd:
1649 __ And(out, first, second);
1650 break;
1651 case HInstruction::kOr:
1652 __ Orr(out, first, second);
1653 break;
1654 case HInstruction::kSub:
1655 __ Sub(out, first, second);
1656 break;
1657 case HInstruction::kXor:
1658 __ Eor(out, first, second);
1659 break;
1660 default:
1661 LOG(FATAL) << "Unexpected instruction kind: " << kind;
1662 UNREACHABLE();
1663 }
1664 }
1665}
1666
1667static void GenerateDataProc(HInstruction::InstructionKind kind,
1668 const Location& out,
1669 const Location& first,
1670 const Operand& second_lo,
1671 const Operand& second_hi,
1672 CodeGeneratorARMVIXL* codegen) {
1673 const vixl32::Register first_hi = HighRegisterFrom(first);
1674 const vixl32::Register first_lo = LowRegisterFrom(first);
1675 const vixl32::Register out_hi = HighRegisterFrom(out);
1676 const vixl32::Register out_lo = LowRegisterFrom(out);
1677
1678 if (kind == HInstruction::kAdd) {
1679 __ Adds(out_lo, first_lo, second_lo);
1680 __ Adc(out_hi, first_hi, second_hi);
1681 } else if (kind == HInstruction::kSub) {
1682 __ Subs(out_lo, first_lo, second_lo);
1683 __ Sbc(out_hi, first_hi, second_hi);
1684 } else {
1685 GenerateDataProcInstruction(kind, out_lo, first_lo, second_lo, codegen);
1686 GenerateDataProcInstruction(kind, out_hi, first_hi, second_hi, codegen);
1687 }
1688}
1689
1690static Operand GetShifterOperand(vixl32::Register rm, ShiftType shift, uint32_t shift_imm) {
1691 return shift_imm == 0 ? Operand(rm) : Operand(rm, shift, shift_imm);
1692}
1693
1694static void GenerateLongDataProc(HDataProcWithShifterOp* instruction,
1695 CodeGeneratorARMVIXL* codegen) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001696 DCHECK_EQ(instruction->GetType(), DataType::Type::kInt64);
Anton Kirilov74234da2017-01-13 14:42:47 +00001697 DCHECK(HDataProcWithShifterOp::IsShiftOp(instruction->GetOpKind()));
1698
1699 const LocationSummary* const locations = instruction->GetLocations();
1700 const uint32_t shift_value = instruction->GetShiftAmount();
1701 const HInstruction::InstructionKind kind = instruction->GetInstrKind();
1702 const Location first = locations->InAt(0);
1703 const Location second = locations->InAt(1);
1704 const Location out = locations->Out();
1705 const vixl32::Register first_hi = HighRegisterFrom(first);
1706 const vixl32::Register first_lo = LowRegisterFrom(first);
1707 const vixl32::Register out_hi = HighRegisterFrom(out);
1708 const vixl32::Register out_lo = LowRegisterFrom(out);
1709 const vixl32::Register second_hi = HighRegisterFrom(second);
1710 const vixl32::Register second_lo = LowRegisterFrom(second);
1711 const ShiftType shift = ShiftFromOpKind(instruction->GetOpKind());
1712
1713 if (shift_value >= 32) {
1714 if (shift == ShiftType::LSL) {
1715 GenerateDataProcInstruction(kind,
1716 out_hi,
1717 first_hi,
1718 Operand(second_lo, ShiftType::LSL, shift_value - 32),
1719 codegen);
1720 GenerateDataProcInstruction(kind, out_lo, first_lo, 0, codegen);
1721 } else if (shift == ShiftType::ASR) {
1722 GenerateDataProc(kind,
1723 out,
1724 first,
1725 GetShifterOperand(second_hi, ShiftType::ASR, shift_value - 32),
1726 Operand(second_hi, ShiftType::ASR, 31),
1727 codegen);
1728 } else {
1729 DCHECK_EQ(shift, ShiftType::LSR);
1730 GenerateDataProc(kind,
1731 out,
1732 first,
1733 GetShifterOperand(second_hi, ShiftType::LSR, shift_value - 32),
1734 0,
1735 codegen);
1736 }
1737 } else {
1738 DCHECK_GT(shift_value, 1U);
1739 DCHECK_LT(shift_value, 32U);
1740
1741 UseScratchRegisterScope temps(codegen->GetVIXLAssembler());
1742
1743 if (shift == ShiftType::LSL) {
1744 // We are not doing this for HInstruction::kAdd because the output will require
1745 // Location::kOutputOverlap; not applicable to other cases.
1746 if (kind == HInstruction::kOr || kind == HInstruction::kXor) {
1747 GenerateDataProcInstruction(kind,
1748 out_hi,
1749 first_hi,
1750 Operand(second_hi, ShiftType::LSL, shift_value),
1751 codegen);
1752 GenerateDataProcInstruction(kind,
1753 out_hi,
1754 out_hi,
1755 Operand(second_lo, ShiftType::LSR, 32 - shift_value),
1756 codegen);
1757 GenerateDataProcInstruction(kind,
1758 out_lo,
1759 first_lo,
1760 Operand(second_lo, ShiftType::LSL, shift_value),
1761 codegen);
1762 } else {
1763 const vixl32::Register temp = temps.Acquire();
1764
1765 __ Lsl(temp, second_hi, shift_value);
1766 __ Orr(temp, temp, Operand(second_lo, ShiftType::LSR, 32 - shift_value));
1767 GenerateDataProc(kind,
1768 out,
1769 first,
1770 Operand(second_lo, ShiftType::LSL, shift_value),
1771 temp,
1772 codegen);
1773 }
1774 } else {
1775 DCHECK(shift == ShiftType::ASR || shift == ShiftType::LSR);
1776
1777 // We are not doing this for HInstruction::kAdd because the output will require
1778 // Location::kOutputOverlap; not applicable to other cases.
1779 if (kind == HInstruction::kOr || kind == HInstruction::kXor) {
1780 GenerateDataProcInstruction(kind,
1781 out_lo,
1782 first_lo,
1783 Operand(second_lo, ShiftType::LSR, shift_value),
1784 codegen);
1785 GenerateDataProcInstruction(kind,
1786 out_lo,
1787 out_lo,
1788 Operand(second_hi, ShiftType::LSL, 32 - shift_value),
1789 codegen);
1790 GenerateDataProcInstruction(kind,
1791 out_hi,
1792 first_hi,
1793 Operand(second_hi, shift, shift_value),
1794 codegen);
1795 } else {
1796 const vixl32::Register temp = temps.Acquire();
1797
1798 __ Lsr(temp, second_lo, shift_value);
1799 __ Orr(temp, temp, Operand(second_hi, ShiftType::LSL, 32 - shift_value));
1800 GenerateDataProc(kind,
1801 out,
1802 first,
1803 temp,
1804 Operand(second_hi, shift, shift_value),
1805 codegen);
1806 }
1807 }
1808 }
1809}
1810
Donghui Bai426b49c2016-11-08 14:55:38 +08001811static void GenerateVcmp(HInstruction* instruction, CodeGeneratorARMVIXL* codegen) {
1812 const Location rhs_loc = instruction->GetLocations()->InAt(1);
1813 if (rhs_loc.IsConstant()) {
1814 // 0.0 is the only immediate that can be encoded directly in
1815 // a VCMP instruction.
1816 //
1817 // Both the JLS (section 15.20.1) and the JVMS (section 6.5)
1818 // specify that in a floating-point comparison, positive zero
1819 // and negative zero are considered equal, so we can use the
1820 // literal 0.0 for both cases here.
1821 //
1822 // Note however that some methods (Float.equal, Float.compare,
1823 // Float.compareTo, Double.equal, Double.compare,
1824 // Double.compareTo, Math.max, Math.min, StrictMath.max,
1825 // StrictMath.min) consider 0.0 to be (strictly) greater than
1826 // -0.0. So if we ever translate calls to these methods into a
1827 // HCompare instruction, we must handle the -0.0 case with
1828 // care here.
1829 DCHECK(rhs_loc.GetConstant()->IsArithmeticZero());
1830
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001831 const DataType::Type type = instruction->InputAt(0)->GetType();
Donghui Bai426b49c2016-11-08 14:55:38 +08001832
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001833 if (type == DataType::Type::kFloat32) {
Donghui Bai426b49c2016-11-08 14:55:38 +08001834 __ Vcmp(F32, InputSRegisterAt(instruction, 0), 0.0);
1835 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001836 DCHECK_EQ(type, DataType::Type::kFloat64);
Donghui Bai426b49c2016-11-08 14:55:38 +08001837 __ Vcmp(F64, InputDRegisterAt(instruction, 0), 0.0);
1838 }
1839 } else {
1840 __ Vcmp(InputVRegisterAt(instruction, 0), InputVRegisterAt(instruction, 1));
1841 }
1842}
1843
Anton Kirilov5601d4e2017-05-11 19:33:50 +01001844static int64_t AdjustConstantForCondition(int64_t value,
1845 IfCondition* condition,
1846 IfCondition* opposite) {
1847 if (value == 1) {
1848 if (*condition == kCondB) {
1849 value = 0;
1850 *condition = kCondEQ;
1851 *opposite = kCondNE;
1852 } else if (*condition == kCondAE) {
1853 value = 0;
1854 *condition = kCondNE;
1855 *opposite = kCondEQ;
1856 }
1857 } else if (value == -1) {
1858 if (*condition == kCondGT) {
1859 value = 0;
1860 *condition = kCondGE;
1861 *opposite = kCondLT;
1862 } else if (*condition == kCondLE) {
1863 value = 0;
1864 *condition = kCondLT;
1865 *opposite = kCondGE;
1866 }
1867 }
1868
1869 return value;
1870}
1871
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001872static std::pair<vixl32::Condition, vixl32::Condition> GenerateLongTestConstant(
1873 HCondition* condition,
1874 bool invert,
1875 CodeGeneratorARMVIXL* codegen) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001876 DCHECK_EQ(condition->GetLeft()->GetType(), DataType::Type::kInt64);
Donghui Bai426b49c2016-11-08 14:55:38 +08001877
1878 const LocationSummary* const locations = condition->GetLocations();
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001879 IfCondition cond = condition->GetCondition();
1880 IfCondition opposite = condition->GetOppositeCondition();
1881
1882 if (invert) {
1883 std::swap(cond, opposite);
1884 }
1885
1886 std::pair<vixl32::Condition, vixl32::Condition> ret(eq, ne);
Donghui Bai426b49c2016-11-08 14:55:38 +08001887 const Location left = locations->InAt(0);
1888 const Location right = locations->InAt(1);
1889
1890 DCHECK(right.IsConstant());
1891
1892 const vixl32::Register left_high = HighRegisterFrom(left);
1893 const vixl32::Register left_low = LowRegisterFrom(left);
Anton Kirilov5601d4e2017-05-11 19:33:50 +01001894 int64_t value = AdjustConstantForCondition(Int64ConstantFrom(right), &cond, &opposite);
1895 UseScratchRegisterScope temps(codegen->GetVIXLAssembler());
1896
1897 // Comparisons against 0 are common enough to deserve special attention.
1898 if (value == 0) {
1899 switch (cond) {
1900 case kCondNE:
1901 // x > 0 iff x != 0 when the comparison is unsigned.
1902 case kCondA:
1903 ret = std::make_pair(ne, eq);
1904 FALLTHROUGH_INTENDED;
1905 case kCondEQ:
1906 // x <= 0 iff x == 0 when the comparison is unsigned.
1907 case kCondBE:
1908 __ Orrs(temps.Acquire(), left_low, left_high);
1909 return ret;
1910 case kCondLT:
1911 case kCondGE:
1912 __ Cmp(left_high, 0);
1913 return std::make_pair(ARMCondition(cond), ARMCondition(opposite));
1914 // Trivially true or false.
1915 case kCondB:
1916 ret = std::make_pair(ne, eq);
1917 FALLTHROUGH_INTENDED;
1918 case kCondAE:
1919 __ Cmp(left_low, left_low);
1920 return ret;
1921 default:
1922 break;
1923 }
1924 }
Donghui Bai426b49c2016-11-08 14:55:38 +08001925
1926 switch (cond) {
1927 case kCondEQ:
1928 case kCondNE:
1929 case kCondB:
1930 case kCondBE:
1931 case kCondA:
1932 case kCondAE: {
Anton Kirilov23b752b2017-07-20 14:40:44 +01001933 const uint32_t value_low = Low32Bits(value);
1934 Operand operand_low(value_low);
1935
Donghui Bai426b49c2016-11-08 14:55:38 +08001936 __ Cmp(left_high, High32Bits(value));
1937
Anton Kirilov23b752b2017-07-20 14:40:44 +01001938 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
1939 // we must ensure that the operands corresponding to the least significant
1940 // halves of the inputs fit into a 16-bit CMP encoding.
1941 if (!left_low.IsLow() || !IsUint<8>(value_low)) {
1942 operand_low = Operand(temps.Acquire());
1943 __ Mov(LeaveFlags, operand_low.GetBaseRegister(), value_low);
1944 }
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);
Anton Kirilov23b752b2017-07-20 14:40:44 +01001952 __ cmp(eq, left_low, operand_low);
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 // Trivially true or false.
1959 if (value == std::numeric_limits<int64_t>::max()) {
1960 __ Cmp(left_low, left_low);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001961 ret = cond == kCondLE ? std::make_pair(eq, ne) : std::make_pair(ne, eq);
Donghui Bai426b49c2016-11-08 14:55:38 +08001962 break;
1963 }
1964
1965 if (cond == kCondLE) {
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001966 DCHECK_EQ(opposite, kCondGT);
Donghui Bai426b49c2016-11-08 14:55:38 +08001967 cond = kCondLT;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001968 opposite = kCondGE;
Donghui Bai426b49c2016-11-08 14:55:38 +08001969 } else {
1970 DCHECK_EQ(cond, kCondGT);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001971 DCHECK_EQ(opposite, kCondLE);
Donghui Bai426b49c2016-11-08 14:55:38 +08001972 cond = kCondGE;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001973 opposite = kCondLT;
Donghui Bai426b49c2016-11-08 14:55:38 +08001974 }
1975
1976 value++;
1977 FALLTHROUGH_INTENDED;
1978 case kCondGE:
1979 case kCondLT: {
Donghui Bai426b49c2016-11-08 14:55:38 +08001980 __ Cmp(left_low, Low32Bits(value));
1981 __ Sbcs(temps.Acquire(), left_high, High32Bits(value));
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001982 ret = std::make_pair(ARMCondition(cond), ARMCondition(opposite));
Donghui Bai426b49c2016-11-08 14:55:38 +08001983 break;
1984 }
1985 default:
1986 LOG(FATAL) << "Unreachable";
1987 UNREACHABLE();
1988 }
1989
1990 return ret;
1991}
1992
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001993static std::pair<vixl32::Condition, vixl32::Condition> GenerateLongTest(
1994 HCondition* condition,
1995 bool invert,
1996 CodeGeneratorARMVIXL* codegen) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001997 DCHECK_EQ(condition->GetLeft()->GetType(), DataType::Type::kInt64);
Donghui Bai426b49c2016-11-08 14:55:38 +08001998
1999 const LocationSummary* const locations = condition->GetLocations();
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002000 IfCondition cond = condition->GetCondition();
2001 IfCondition opposite = condition->GetOppositeCondition();
2002
2003 if (invert) {
2004 std::swap(cond, opposite);
2005 }
2006
2007 std::pair<vixl32::Condition, vixl32::Condition> ret(eq, ne);
Donghui Bai426b49c2016-11-08 14:55:38 +08002008 Location left = locations->InAt(0);
2009 Location right = locations->InAt(1);
2010
2011 DCHECK(right.IsRegisterPair());
2012
2013 switch (cond) {
2014 case kCondEQ:
2015 case kCondNE:
2016 case kCondB:
2017 case kCondBE:
2018 case kCondA:
2019 case kCondAE: {
2020 __ Cmp(HighRegisterFrom(left), HighRegisterFrom(right));
2021
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002022 // We use the scope because of the IT block that follows.
Donghui Bai426b49c2016-11-08 14:55:38 +08002023 ExactAssemblyScope guard(codegen->GetVIXLAssembler(),
2024 2 * vixl32::k16BitT32InstructionSizeInBytes,
2025 CodeBufferCheckScope::kExactSize);
2026
2027 __ it(eq);
2028 __ cmp(eq, LowRegisterFrom(left), LowRegisterFrom(right));
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002029 ret = std::make_pair(ARMUnsignedCondition(cond), ARMUnsignedCondition(opposite));
Donghui Bai426b49c2016-11-08 14:55:38 +08002030 break;
2031 }
2032 case kCondLE:
2033 case kCondGT:
2034 if (cond == kCondLE) {
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002035 DCHECK_EQ(opposite, kCondGT);
Donghui Bai426b49c2016-11-08 14:55:38 +08002036 cond = kCondGE;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002037 opposite = kCondLT;
Donghui Bai426b49c2016-11-08 14:55:38 +08002038 } else {
2039 DCHECK_EQ(cond, kCondGT);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002040 DCHECK_EQ(opposite, kCondLE);
Donghui Bai426b49c2016-11-08 14:55:38 +08002041 cond = kCondLT;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002042 opposite = kCondGE;
Donghui Bai426b49c2016-11-08 14:55:38 +08002043 }
2044
2045 std::swap(left, right);
2046 FALLTHROUGH_INTENDED;
2047 case kCondGE:
2048 case kCondLT: {
2049 UseScratchRegisterScope temps(codegen->GetVIXLAssembler());
2050
2051 __ Cmp(LowRegisterFrom(left), LowRegisterFrom(right));
2052 __ Sbcs(temps.Acquire(), HighRegisterFrom(left), HighRegisterFrom(right));
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002053 ret = std::make_pair(ARMCondition(cond), ARMCondition(opposite));
Donghui Bai426b49c2016-11-08 14:55:38 +08002054 break;
2055 }
2056 default:
2057 LOG(FATAL) << "Unreachable";
2058 UNREACHABLE();
2059 }
2060
2061 return ret;
2062}
2063
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002064static std::pair<vixl32::Condition, vixl32::Condition> GenerateTest(HCondition* condition,
2065 bool invert,
2066 CodeGeneratorARMVIXL* codegen) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002067 const DataType::Type type = condition->GetLeft()->GetType();
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002068 IfCondition cond = condition->GetCondition();
2069 IfCondition opposite = condition->GetOppositeCondition();
2070 std::pair<vixl32::Condition, vixl32::Condition> ret(eq, ne);
Donghui Bai426b49c2016-11-08 14:55:38 +08002071
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002072 if (invert) {
2073 std::swap(cond, opposite);
2074 }
Donghui Bai426b49c2016-11-08 14:55:38 +08002075
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002076 if (type == DataType::Type::kInt64) {
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002077 ret = condition->GetLocations()->InAt(1).IsConstant()
2078 ? GenerateLongTestConstant(condition, invert, codegen)
2079 : GenerateLongTest(condition, invert, codegen);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002080 } else if (DataType::IsFloatingPointType(type)) {
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002081 GenerateVcmp(condition, codegen);
2082 __ Vmrs(RegisterOrAPSR_nzcv(kPcCode), FPSCR);
2083 ret = std::make_pair(ARMFPCondition(cond, condition->IsGtBias()),
2084 ARMFPCondition(opposite, condition->IsGtBias()));
Donghui Bai426b49c2016-11-08 14:55:38 +08002085 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002086 DCHECK(DataType::IsIntegralType(type) || type == DataType::Type::kReference) << type;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002087 __ Cmp(InputRegisterAt(condition, 0), InputOperandAt(condition, 1));
2088 ret = std::make_pair(ARMCondition(cond), ARMCondition(opposite));
Donghui Bai426b49c2016-11-08 14:55:38 +08002089 }
2090
2091 return ret;
2092}
2093
Anton Kirilov5601d4e2017-05-11 19:33:50 +01002094static void GenerateConditionGeneric(HCondition* cond, CodeGeneratorARMVIXL* codegen) {
Anton Kirilov5601d4e2017-05-11 19:33:50 +01002095 const vixl32::Register out = OutputRegister(cond);
2096 const auto condition = GenerateTest(cond, false, codegen);
2097
2098 __ Mov(LeaveFlags, out, 0);
2099
2100 if (out.IsLow()) {
2101 // We use the scope because of the IT block that follows.
2102 ExactAssemblyScope guard(codegen->GetVIXLAssembler(),
2103 2 * vixl32::k16BitT32InstructionSizeInBytes,
2104 CodeBufferCheckScope::kExactSize);
2105
2106 __ it(condition.first);
2107 __ mov(condition.first, out, 1);
2108 } else {
2109 vixl32::Label done_label;
2110 vixl32::Label* const final_label = codegen->GetFinalLabel(cond, &done_label);
2111
2112 __ B(condition.second, final_label, /* far_target */ false);
2113 __ Mov(out, 1);
2114
2115 if (done_label.IsReferenced()) {
2116 __ Bind(&done_label);
2117 }
2118 }
2119}
2120
2121static void GenerateEqualLong(HCondition* cond, CodeGeneratorARMVIXL* codegen) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002122 DCHECK_EQ(cond->GetLeft()->GetType(), DataType::Type::kInt64);
Anton Kirilov5601d4e2017-05-11 19:33:50 +01002123
2124 const LocationSummary* const locations = cond->GetLocations();
2125 IfCondition condition = cond->GetCondition();
2126 const vixl32::Register out = OutputRegister(cond);
2127 const Location left = locations->InAt(0);
2128 const Location right = locations->InAt(1);
2129 vixl32::Register left_high = HighRegisterFrom(left);
2130 vixl32::Register left_low = LowRegisterFrom(left);
2131 vixl32::Register temp;
2132 UseScratchRegisterScope temps(codegen->GetVIXLAssembler());
2133
2134 if (right.IsConstant()) {
2135 IfCondition opposite = cond->GetOppositeCondition();
2136 const int64_t value = AdjustConstantForCondition(Int64ConstantFrom(right),
2137 &condition,
2138 &opposite);
2139 Operand right_high = High32Bits(value);
2140 Operand right_low = Low32Bits(value);
2141
2142 // The output uses Location::kNoOutputOverlap.
2143 if (out.Is(left_high)) {
2144 std::swap(left_low, left_high);
2145 std::swap(right_low, right_high);
2146 }
2147
2148 __ Sub(out, left_low, right_low);
2149 temp = temps.Acquire();
2150 __ Sub(temp, left_high, right_high);
2151 } else {
2152 DCHECK(right.IsRegisterPair());
2153 temp = temps.Acquire();
2154 __ Sub(temp, left_high, HighRegisterFrom(right));
2155 __ Sub(out, left_low, LowRegisterFrom(right));
2156 }
2157
2158 // Need to check after calling AdjustConstantForCondition().
2159 DCHECK(condition == kCondEQ || condition == kCondNE) << condition;
2160
2161 if (condition == kCondNE && out.IsLow()) {
2162 __ Orrs(out, out, temp);
2163
2164 // We use the scope because of the IT block that follows.
2165 ExactAssemblyScope guard(codegen->GetVIXLAssembler(),
2166 2 * vixl32::k16BitT32InstructionSizeInBytes,
2167 CodeBufferCheckScope::kExactSize);
2168
2169 __ it(ne);
2170 __ mov(ne, out, 1);
2171 } else {
2172 __ Orr(out, out, temp);
2173 codegen->GenerateConditionWithZero(condition, out, out, temp);
2174 }
2175}
2176
Anton Kirilov5601d4e2017-05-11 19:33:50 +01002177static void GenerateConditionLong(HCondition* cond, CodeGeneratorARMVIXL* codegen) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002178 DCHECK_EQ(cond->GetLeft()->GetType(), DataType::Type::kInt64);
Anton Kirilov5601d4e2017-05-11 19:33:50 +01002179
2180 const LocationSummary* const locations = cond->GetLocations();
2181 IfCondition condition = cond->GetCondition();
2182 const vixl32::Register out = OutputRegister(cond);
2183 const Location left = locations->InAt(0);
2184 const Location right = locations->InAt(1);
2185
2186 if (right.IsConstant()) {
2187 IfCondition opposite = cond->GetOppositeCondition();
2188
2189 // Comparisons against 0 are common enough to deserve special attention.
2190 if (AdjustConstantForCondition(Int64ConstantFrom(right), &condition, &opposite) == 0) {
2191 switch (condition) {
2192 case kCondNE:
2193 case kCondA:
2194 if (out.IsLow()) {
2195 // We only care if both input registers are 0 or not.
2196 __ Orrs(out, LowRegisterFrom(left), HighRegisterFrom(left));
2197
2198 // We use the scope because of the IT block that follows.
2199 ExactAssemblyScope guard(codegen->GetVIXLAssembler(),
2200 2 * vixl32::k16BitT32InstructionSizeInBytes,
2201 CodeBufferCheckScope::kExactSize);
2202
2203 __ it(ne);
2204 __ mov(ne, out, 1);
2205 return;
2206 }
2207
2208 FALLTHROUGH_INTENDED;
2209 case kCondEQ:
2210 case kCondBE:
2211 // We only care if both input registers are 0 or not.
2212 __ Orr(out, LowRegisterFrom(left), HighRegisterFrom(left));
2213 codegen->GenerateConditionWithZero(condition, out, out);
2214 return;
2215 case kCondLT:
2216 case kCondGE:
2217 // We only care about the sign bit.
2218 FALLTHROUGH_INTENDED;
2219 case kCondAE:
2220 case kCondB:
2221 codegen->GenerateConditionWithZero(condition, out, HighRegisterFrom(left));
2222 return;
2223 case kCondLE:
2224 case kCondGT:
2225 default:
2226 break;
2227 }
2228 }
2229 }
2230
Anton Kirilov23b752b2017-07-20 14:40:44 +01002231 // If `out` is a low register, then the GenerateConditionGeneric()
2232 // function generates a shorter code sequence that is still branchless.
2233 if ((condition == kCondEQ || condition == kCondNE) && !out.IsLow()) {
Anton Kirilov5601d4e2017-05-11 19:33:50 +01002234 GenerateEqualLong(cond, codegen);
2235 return;
2236 }
2237
Anton Kirilov23b752b2017-07-20 14:40:44 +01002238 GenerateConditionGeneric(cond, codegen);
Anton Kirilov5601d4e2017-05-11 19:33:50 +01002239}
2240
Roland Levillain6d729a72017-06-30 18:34:01 +01002241static void GenerateConditionIntegralOrNonPrimitive(HCondition* cond,
2242 CodeGeneratorARMVIXL* codegen) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002243 const DataType::Type type = cond->GetLeft()->GetType();
Anton Kirilov5601d4e2017-05-11 19:33:50 +01002244
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002245 DCHECK(DataType::IsIntegralType(type) || type == DataType::Type::kReference) << type;
Anton Kirilov5601d4e2017-05-11 19:33:50 +01002246
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002247 if (type == DataType::Type::kInt64) {
Anton Kirilov5601d4e2017-05-11 19:33:50 +01002248 GenerateConditionLong(cond, codegen);
2249 return;
2250 }
2251
2252 IfCondition condition = cond->GetCondition();
2253 vixl32::Register in = InputRegisterAt(cond, 0);
2254 const vixl32::Register out = OutputRegister(cond);
2255 const Location right = cond->GetLocations()->InAt(1);
2256 int64_t value;
2257
2258 if (right.IsConstant()) {
2259 IfCondition opposite = cond->GetOppositeCondition();
2260
2261 value = AdjustConstantForCondition(Int64ConstantFrom(right), &condition, &opposite);
2262
2263 // Comparisons against 0 are common enough to deserve special attention.
2264 if (value == 0) {
2265 switch (condition) {
2266 case kCondNE:
2267 case kCondA:
2268 if (out.IsLow() && out.Is(in)) {
2269 __ Cmp(out, 0);
2270
2271 // We use the scope because of the IT block that follows.
2272 ExactAssemblyScope guard(codegen->GetVIXLAssembler(),
2273 2 * vixl32::k16BitT32InstructionSizeInBytes,
2274 CodeBufferCheckScope::kExactSize);
2275
2276 __ it(ne);
2277 __ mov(ne, out, 1);
2278 return;
2279 }
2280
2281 FALLTHROUGH_INTENDED;
2282 case kCondEQ:
2283 case kCondBE:
2284 case kCondLT:
2285 case kCondGE:
2286 case kCondAE:
2287 case kCondB:
2288 codegen->GenerateConditionWithZero(condition, out, in);
2289 return;
2290 case kCondLE:
2291 case kCondGT:
2292 default:
2293 break;
2294 }
2295 }
2296 }
2297
2298 if (condition == kCondEQ || condition == kCondNE) {
2299 Operand operand(0);
2300
2301 if (right.IsConstant()) {
2302 operand = Operand::From(value);
2303 } else if (out.Is(RegisterFrom(right))) {
2304 // Avoid 32-bit instructions if possible.
2305 operand = InputOperandAt(cond, 0);
2306 in = RegisterFrom(right);
2307 } else {
2308 operand = InputOperandAt(cond, 1);
2309 }
2310
2311 if (condition == kCondNE && out.IsLow()) {
2312 __ Subs(out, in, operand);
2313
2314 // We use the scope because of the IT block that follows.
2315 ExactAssemblyScope guard(codegen->GetVIXLAssembler(),
2316 2 * vixl32::k16BitT32InstructionSizeInBytes,
2317 CodeBufferCheckScope::kExactSize);
2318
2319 __ it(ne);
2320 __ mov(ne, out, 1);
2321 } else {
2322 __ Sub(out, in, operand);
2323 codegen->GenerateConditionWithZero(condition, out, out);
2324 }
2325
2326 return;
2327 }
2328
2329 GenerateConditionGeneric(cond, codegen);
2330}
2331
Donghui Bai426b49c2016-11-08 14:55:38 +08002332static bool CanEncodeConstantAs8BitImmediate(HConstant* constant) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002333 const DataType::Type type = constant->GetType();
Donghui Bai426b49c2016-11-08 14:55:38 +08002334 bool ret = false;
2335
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002336 DCHECK(DataType::IsIntegralType(type) || type == DataType::Type::kReference) << type;
Donghui Bai426b49c2016-11-08 14:55:38 +08002337
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002338 if (type == DataType::Type::kInt64) {
Donghui Bai426b49c2016-11-08 14:55:38 +08002339 const uint64_t value = Uint64ConstantFrom(constant);
2340
2341 ret = IsUint<8>(Low32Bits(value)) && IsUint<8>(High32Bits(value));
2342 } else {
2343 ret = IsUint<8>(Int32ConstantFrom(constant));
2344 }
2345
2346 return ret;
2347}
2348
2349static Location Arm8BitEncodableConstantOrRegister(HInstruction* constant) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002350 DCHECK(!DataType::IsFloatingPointType(constant->GetType()));
Donghui Bai426b49c2016-11-08 14:55:38 +08002351
2352 if (constant->IsConstant() && CanEncodeConstantAs8BitImmediate(constant->AsConstant())) {
2353 return Location::ConstantLocation(constant->AsConstant());
2354 }
2355
2356 return Location::RequiresRegister();
2357}
2358
2359static bool CanGenerateConditionalMove(const Location& out, const Location& src) {
2360 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
2361 // we check that we are not dealing with floating-point output (there is no
2362 // 16-bit VMOV encoding).
2363 if (!out.IsRegister() && !out.IsRegisterPair()) {
2364 return false;
2365 }
2366
2367 // For constants, we also check that the output is in one or two low registers,
2368 // and that the constants fit in an 8-bit unsigned integer, so that a 16-bit
2369 // MOV encoding can be used.
2370 if (src.IsConstant()) {
2371 if (!CanEncodeConstantAs8BitImmediate(src.GetConstant())) {
2372 return false;
2373 }
2374
2375 if (out.IsRegister()) {
2376 if (!RegisterFrom(out).IsLow()) {
2377 return false;
2378 }
2379 } else {
2380 DCHECK(out.IsRegisterPair());
2381
2382 if (!HighRegisterFrom(out).IsLow()) {
2383 return false;
2384 }
2385 }
2386 }
2387
2388 return true;
2389}
2390
Scott Wakelingfe885462016-09-22 10:24:38 +01002391#undef __
2392
Donghui Bai426b49c2016-11-08 14:55:38 +08002393vixl32::Label* CodeGeneratorARMVIXL::GetFinalLabel(HInstruction* instruction,
2394 vixl32::Label* final_label) {
2395 DCHECK(!instruction->IsControlFlow() && !instruction->IsSuspendCheck());
Anton Kirilov6f644202017-02-27 18:29:45 +00002396 DCHECK(!instruction->IsInvoke() || !instruction->GetLocations()->CanCall());
Donghui Bai426b49c2016-11-08 14:55:38 +08002397
2398 const HBasicBlock* const block = instruction->GetBlock();
2399 const HLoopInformation* const info = block->GetLoopInformation();
2400 HInstruction* const next = instruction->GetNext();
2401
2402 // Avoid a branch to a branch.
2403 if (next->IsGoto() && (info == nullptr ||
2404 !info->IsBackEdge(*block) ||
2405 !info->HasSuspendCheck())) {
2406 final_label = GetLabelOf(next->AsGoto()->GetSuccessor());
2407 }
2408
2409 return final_label;
2410}
2411
Scott Wakelingfe885462016-09-22 10:24:38 +01002412CodeGeneratorARMVIXL::CodeGeneratorARMVIXL(HGraph* graph,
2413 const ArmInstructionSetFeatures& isa_features,
2414 const CompilerOptions& compiler_options,
2415 OptimizingCompilerStats* stats)
2416 : CodeGenerator(graph,
2417 kNumberOfCoreRegisters,
2418 kNumberOfSRegisters,
2419 kNumberOfRegisterPairs,
2420 kCoreCalleeSaves.GetList(),
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002421 ComputeSRegisterListMask(kFpuCalleeSaves),
Scott Wakelingfe885462016-09-22 10:24:38 +01002422 compiler_options,
2423 stats),
Vladimir Markoca6fff82017-10-03 14:49:14 +01002424 block_labels_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
2425 jump_tables_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Scott Wakelingfe885462016-09-22 10:24:38 +01002426 location_builder_(graph, this),
2427 instruction_visitor_(graph, this),
Vladimir Markoca6fff82017-10-03 14:49:14 +01002428 move_resolver_(graph->GetAllocator(), this),
2429 assembler_(graph->GetAllocator()),
Artem Serovd4cc5b22016-11-04 11:19:09 +00002430 isa_features_(isa_features),
Artem Serovc5fcb442016-12-02 19:19:58 +00002431 uint32_literals_(std::less<uint32_t>(),
Vladimir Markoca6fff82017-10-03 14:49:14 +01002432 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
2433 pc_relative_method_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
2434 method_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
2435 pc_relative_type_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
2436 type_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
2437 pc_relative_string_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
2438 string_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
2439 baker_read_barrier_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Artem Serovc5fcb442016-12-02 19:19:58 +00002440 jit_string_patches_(StringReferenceValueComparator(),
Vladimir Markoca6fff82017-10-03 14:49:14 +01002441 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Artem Serovc5fcb442016-12-02 19:19:58 +00002442 jit_class_patches_(TypeReferenceValueComparator(),
Vladimir Markoca6fff82017-10-03 14:49:14 +01002443 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)) {
Scott Wakelingfe885462016-09-22 10:24:38 +01002444 // Always save the LR register to mimic Quick.
2445 AddAllocatedRegister(Location::RegisterLocation(LR));
Nicolas Geoffray13a797b2017-03-15 16:41:31 +00002446 // Give D30 and D31 as scratch register to VIXL. The register allocator only works on
2447 // S0-S31, which alias to D0-D15.
2448 GetVIXLAssembler()->GetScratchVRegisterList()->Combine(d31);
2449 GetVIXLAssembler()->GetScratchVRegisterList()->Combine(d30);
Scott Wakelingfe885462016-09-22 10:24:38 +01002450}
2451
Artem Serov551b28f2016-10-18 19:11:30 +01002452void JumpTableARMVIXL::EmitTable(CodeGeneratorARMVIXL* codegen) {
2453 uint32_t num_entries = switch_instr_->GetNumEntries();
2454 DCHECK_GE(num_entries, kPackedSwitchCompareJumpThreshold);
2455
2456 // We are about to use the assembler to place literals directly. Make sure we have enough
Scott Wakelingb77051e2016-11-21 19:46:00 +00002457 // underlying code buffer and we have generated a jump table of the right size, using
2458 // codegen->GetVIXLAssembler()->GetBuffer().Align();
Artem Serov0fb37192016-12-06 18:13:40 +00002459 ExactAssemblyScope aas(codegen->GetVIXLAssembler(),
2460 num_entries * sizeof(int32_t),
2461 CodeBufferCheckScope::kMaximumSize);
Artem Serov551b28f2016-10-18 19:11:30 +01002462 // TODO(VIXL): Check that using lower case bind is fine here.
2463 codegen->GetVIXLAssembler()->bind(&table_start_);
Artem Serov09a940d2016-11-11 16:15:11 +00002464 for (uint32_t i = 0; i < num_entries; i++) {
2465 codegen->GetVIXLAssembler()->place(bb_addresses_[i].get());
2466 }
2467}
2468
2469void JumpTableARMVIXL::FixTable(CodeGeneratorARMVIXL* codegen) {
2470 uint32_t num_entries = switch_instr_->GetNumEntries();
2471 DCHECK_GE(num_entries, kPackedSwitchCompareJumpThreshold);
2472
Artem Serov551b28f2016-10-18 19:11:30 +01002473 const ArenaVector<HBasicBlock*>& successors = switch_instr_->GetBlock()->GetSuccessors();
2474 for (uint32_t i = 0; i < num_entries; i++) {
2475 vixl32::Label* target_label = codegen->GetLabelOf(successors[i]);
2476 DCHECK(target_label->IsBound());
2477 int32_t jump_offset = target_label->GetLocation() - table_start_.GetLocation();
2478 // When doing BX to address we need to have lower bit set to 1 in T32.
2479 if (codegen->GetVIXLAssembler()->IsUsingT32()) {
2480 jump_offset++;
2481 }
2482 DCHECK_GT(jump_offset, std::numeric_limits<int32_t>::min());
2483 DCHECK_LE(jump_offset, std::numeric_limits<int32_t>::max());
Artem Serov09a940d2016-11-11 16:15:11 +00002484
Scott Wakelingb77051e2016-11-21 19:46:00 +00002485 bb_addresses_[i].get()->UpdateValue(jump_offset, codegen->GetVIXLAssembler()->GetBuffer());
Artem Serov551b28f2016-10-18 19:11:30 +01002486 }
2487}
2488
Artem Serov09a940d2016-11-11 16:15:11 +00002489void CodeGeneratorARMVIXL::FixJumpTables() {
Artem Serov551b28f2016-10-18 19:11:30 +01002490 for (auto&& jump_table : jump_tables_) {
Artem Serov09a940d2016-11-11 16:15:11 +00002491 jump_table->FixTable(this);
Artem Serov551b28f2016-10-18 19:11:30 +01002492 }
2493}
2494
Andreas Gampeca620d72016-11-08 08:09:33 -08002495#define __ reinterpret_cast<ArmVIXLAssembler*>(GetAssembler())->GetVIXLAssembler()-> // NOLINT
Scott Wakelingfe885462016-09-22 10:24:38 +01002496
2497void CodeGeneratorARMVIXL::Finalize(CodeAllocator* allocator) {
Artem Serov09a940d2016-11-11 16:15:11 +00002498 FixJumpTables();
Scott Wakelingfe885462016-09-22 10:24:38 +01002499 GetAssembler()->FinalizeCode();
2500 CodeGenerator::Finalize(allocator);
2501}
2502
2503void CodeGeneratorARMVIXL::SetupBlockedRegisters() const {
Scott Wakelingfe885462016-09-22 10:24:38 +01002504 // Stack register, LR and PC are always reserved.
2505 blocked_core_registers_[SP] = true;
2506 blocked_core_registers_[LR] = true;
2507 blocked_core_registers_[PC] = true;
2508
Roland Levillain6d729a72017-06-30 18:34:01 +01002509 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2510 // Reserve marking register.
2511 blocked_core_registers_[MR] = true;
2512 }
2513
Scott Wakelingfe885462016-09-22 10:24:38 +01002514 // Reserve thread register.
2515 blocked_core_registers_[TR] = true;
2516
2517 // Reserve temp register.
2518 blocked_core_registers_[IP] = true;
2519
2520 if (GetGraph()->IsDebuggable()) {
2521 // Stubs do not save callee-save floating point registers. If the graph
2522 // is debuggable, we need to deal with these registers differently. For
2523 // now, just block them.
2524 for (uint32_t i = kFpuCalleeSaves.GetFirstSRegister().GetCode();
2525 i <= kFpuCalleeSaves.GetLastSRegister().GetCode();
2526 ++i) {
2527 blocked_fpu_registers_[i] = true;
2528 }
2529 }
Scott Wakelingfe885462016-09-22 10:24:38 +01002530}
2531
Scott Wakelingfe885462016-09-22 10:24:38 +01002532InstructionCodeGeneratorARMVIXL::InstructionCodeGeneratorARMVIXL(HGraph* graph,
2533 CodeGeneratorARMVIXL* codegen)
2534 : InstructionCodeGenerator(graph, codegen),
2535 assembler_(codegen->GetAssembler()),
2536 codegen_(codegen) {}
2537
2538void CodeGeneratorARMVIXL::ComputeSpillMask() {
2539 core_spill_mask_ = allocated_registers_.GetCoreRegisters() & core_callee_save_mask_;
2540 DCHECK_NE(core_spill_mask_, 0u) << "At least the return address register must be saved";
2541 // There is no easy instruction to restore just the PC on thumb2. We spill and
2542 // restore another arbitrary register.
2543 core_spill_mask_ |= (1 << kCoreAlwaysSpillRegister.GetCode());
2544 fpu_spill_mask_ = allocated_registers_.GetFloatingPointRegisters() & fpu_callee_save_mask_;
2545 // We use vpush and vpop for saving and restoring floating point registers, which take
2546 // a SRegister and the number of registers to save/restore after that SRegister. We
2547 // therefore update the `fpu_spill_mask_` to also contain those registers not allocated,
2548 // but in the range.
2549 if (fpu_spill_mask_ != 0) {
2550 uint32_t least_significant_bit = LeastSignificantBit(fpu_spill_mask_);
2551 uint32_t most_significant_bit = MostSignificantBit(fpu_spill_mask_);
2552 for (uint32_t i = least_significant_bit + 1 ; i < most_significant_bit; ++i) {
2553 fpu_spill_mask_ |= (1 << i);
2554 }
2555 }
2556}
2557
2558void CodeGeneratorARMVIXL::GenerateFrameEntry() {
2559 bool skip_overflow_check =
2560 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kArm);
2561 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
2562 __ Bind(&frame_entry_label_);
2563
2564 if (HasEmptyFrame()) {
2565 return;
2566 }
2567
Scott Wakelingfe885462016-09-22 10:24:38 +01002568 if (!skip_overflow_check) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002569 UseScratchRegisterScope temps(GetVIXLAssembler());
2570 vixl32::Register temp = temps.Acquire();
Vladimir Marko33bff252017-11-01 14:35:42 +00002571 __ Sub(temp, sp, Operand::From(GetStackOverflowReservedBytes(InstructionSet::kArm)));
Scott Wakelingfe885462016-09-22 10:24:38 +01002572 // The load must immediately precede RecordPcInfo.
Artem Serov0fb37192016-12-06 18:13:40 +00002573 ExactAssemblyScope aas(GetVIXLAssembler(),
2574 vixl32::kMaxInstructionSizeInBytes,
2575 CodeBufferCheckScope::kMaximumSize);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002576 __ ldr(temp, MemOperand(temp));
2577 RecordPcInfo(nullptr, 0);
Scott Wakelingfe885462016-09-22 10:24:38 +01002578 }
2579
2580 __ Push(RegisterList(core_spill_mask_));
2581 GetAssembler()->cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(core_spill_mask_));
2582 GetAssembler()->cfi().RelOffsetForMany(DWARFReg(kMethodRegister),
2583 0,
2584 core_spill_mask_,
2585 kArmWordSize);
2586 if (fpu_spill_mask_ != 0) {
2587 uint32_t first = LeastSignificantBit(fpu_spill_mask_);
2588
2589 // Check that list is contiguous.
2590 DCHECK_EQ(fpu_spill_mask_ >> CTZ(fpu_spill_mask_), ~0u >> (32 - POPCOUNT(fpu_spill_mask_)));
2591
2592 __ Vpush(SRegisterList(vixl32::SRegister(first), POPCOUNT(fpu_spill_mask_)));
2593 GetAssembler()->cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(fpu_spill_mask_));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002594 GetAssembler()->cfi().RelOffsetForMany(DWARFReg(s0), 0, fpu_spill_mask_, kArmWordSize);
Scott Wakelingfe885462016-09-22 10:24:38 +01002595 }
Scott Wakelingbffdc702016-12-07 17:46:03 +00002596
Scott Wakelingfe885462016-09-22 10:24:38 +01002597 int adjust = GetFrameSize() - FrameEntrySpillSize();
2598 __ Sub(sp, sp, adjust);
2599 GetAssembler()->cfi().AdjustCFAOffset(adjust);
Scott Wakelingbffdc702016-12-07 17:46:03 +00002600
2601 // Save the current method if we need it. Note that we do not
2602 // do this in HCurrentMethod, as the instruction might have been removed
2603 // in the SSA graph.
2604 if (RequiresCurrentMethod()) {
2605 GetAssembler()->StoreToOffset(kStoreWord, kMethodRegister, sp, 0);
2606 }
Nicolas Geoffrayf7893532017-06-15 12:34:36 +01002607
2608 if (GetGraph()->HasShouldDeoptimizeFlag()) {
2609 UseScratchRegisterScope temps(GetVIXLAssembler());
2610 vixl32::Register temp = temps.Acquire();
2611 // Initialize should_deoptimize flag to 0.
2612 __ Mov(temp, 0);
2613 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, GetStackOffsetOfShouldDeoptimizeFlag());
2614 }
Roland Levillain5daa4952017-07-03 17:23:56 +01002615
2616 MaybeGenerateMarkingRegisterCheck(/* code */ 1);
Scott Wakelingfe885462016-09-22 10:24:38 +01002617}
2618
2619void CodeGeneratorARMVIXL::GenerateFrameExit() {
2620 if (HasEmptyFrame()) {
2621 __ Bx(lr);
2622 return;
2623 }
2624 GetAssembler()->cfi().RememberState();
2625 int adjust = GetFrameSize() - FrameEntrySpillSize();
2626 __ Add(sp, sp, adjust);
2627 GetAssembler()->cfi().AdjustCFAOffset(-adjust);
2628 if (fpu_spill_mask_ != 0) {
2629 uint32_t first = LeastSignificantBit(fpu_spill_mask_);
2630
2631 // Check that list is contiguous.
2632 DCHECK_EQ(fpu_spill_mask_ >> CTZ(fpu_spill_mask_), ~0u >> (32 - POPCOUNT(fpu_spill_mask_)));
2633
2634 __ Vpop(SRegisterList(vixl32::SRegister(first), POPCOUNT(fpu_spill_mask_)));
2635 GetAssembler()->cfi().AdjustCFAOffset(
2636 -static_cast<int>(kArmWordSize) * POPCOUNT(fpu_spill_mask_));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002637 GetAssembler()->cfi().RestoreMany(DWARFReg(vixl32::SRegister(0)), fpu_spill_mask_);
Scott Wakelingfe885462016-09-22 10:24:38 +01002638 }
2639 // Pop LR into PC to return.
2640 DCHECK_NE(core_spill_mask_ & (1 << kLrCode), 0U);
2641 uint32_t pop_mask = (core_spill_mask_ & (~(1 << kLrCode))) | 1 << kPcCode;
2642 __ Pop(RegisterList(pop_mask));
2643 GetAssembler()->cfi().RestoreState();
2644 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
2645}
2646
2647void CodeGeneratorARMVIXL::Bind(HBasicBlock* block) {
2648 __ Bind(GetLabelOf(block));
2649}
2650
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002651Location InvokeDexCallingConventionVisitorARMVIXL::GetNextLocation(DataType::Type type) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002652 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002653 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002654 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002655 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002656 case DataType::Type::kInt8:
2657 case DataType::Type::kUint16:
2658 case DataType::Type::kInt16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002659 case DataType::Type::kInt32: {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002660 uint32_t index = gp_index_++;
2661 uint32_t stack_index = stack_index_++;
2662 if (index < calling_convention.GetNumberOfRegisters()) {
2663 return LocationFrom(calling_convention.GetRegisterAt(index));
2664 } else {
2665 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
2666 }
2667 }
2668
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002669 case DataType::Type::kInt64: {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002670 uint32_t index = gp_index_;
2671 uint32_t stack_index = stack_index_;
2672 gp_index_ += 2;
2673 stack_index_ += 2;
2674 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
2675 if (calling_convention.GetRegisterAt(index).Is(r1)) {
2676 // Skip R1, and use R2_R3 instead.
2677 gp_index_++;
2678 index++;
2679 }
2680 }
2681 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
2682 DCHECK_EQ(calling_convention.GetRegisterAt(index).GetCode() + 1,
2683 calling_convention.GetRegisterAt(index + 1).GetCode());
2684
2685 return LocationFrom(calling_convention.GetRegisterAt(index),
2686 calling_convention.GetRegisterAt(index + 1));
2687 } else {
2688 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
2689 }
2690 }
2691
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002692 case DataType::Type::kFloat32: {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002693 uint32_t stack_index = stack_index_++;
2694 if (float_index_ % 2 == 0) {
2695 float_index_ = std::max(double_index_, float_index_);
2696 }
2697 if (float_index_ < calling_convention.GetNumberOfFpuRegisters()) {
2698 return LocationFrom(calling_convention.GetFpuRegisterAt(float_index_++));
2699 } else {
2700 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
2701 }
2702 }
2703
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002704 case DataType::Type::kFloat64: {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002705 double_index_ = std::max(double_index_, RoundUp(float_index_, 2));
2706 uint32_t stack_index = stack_index_;
2707 stack_index_ += 2;
2708 if (double_index_ + 1 < calling_convention.GetNumberOfFpuRegisters()) {
2709 uint32_t index = double_index_;
2710 double_index_ += 2;
2711 Location result = LocationFrom(
2712 calling_convention.GetFpuRegisterAt(index),
2713 calling_convention.GetFpuRegisterAt(index + 1));
2714 DCHECK(ExpectedPairLayout(result));
2715 return result;
2716 } else {
2717 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
2718 }
2719 }
2720
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002721 case DataType::Type::kVoid:
Artem Serovd4cc5b22016-11-04 11:19:09 +00002722 LOG(FATAL) << "Unexpected parameter type " << type;
2723 break;
2724 }
2725 return Location::NoLocation();
2726}
2727
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002728Location InvokeDexCallingConventionVisitorARMVIXL::GetReturnLocation(DataType::Type type) const {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002729 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002730 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002731 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002732 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002733 case DataType::Type::kInt8:
2734 case DataType::Type::kUint16:
2735 case DataType::Type::kInt16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002736 case DataType::Type::kInt32: {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002737 return LocationFrom(r0);
2738 }
2739
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002740 case DataType::Type::kFloat32: {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002741 return LocationFrom(s0);
2742 }
2743
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002744 case DataType::Type::kInt64: {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002745 return LocationFrom(r0, r1);
2746 }
2747
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002748 case DataType::Type::kFloat64: {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002749 return LocationFrom(s0, s1);
2750 }
2751
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002752 case DataType::Type::kVoid:
Artem Serovd4cc5b22016-11-04 11:19:09 +00002753 return Location::NoLocation();
2754 }
2755
2756 UNREACHABLE();
2757}
2758
2759Location InvokeDexCallingConventionVisitorARMVIXL::GetMethodLocation() const {
2760 return LocationFrom(kMethodRegister);
2761}
2762
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002763void CodeGeneratorARMVIXL::Move32(Location destination, Location source) {
2764 if (source.Equals(destination)) {
2765 return;
2766 }
2767 if (destination.IsRegister()) {
2768 if (source.IsRegister()) {
2769 __ Mov(RegisterFrom(destination), RegisterFrom(source));
2770 } else if (source.IsFpuRegister()) {
2771 __ Vmov(RegisterFrom(destination), SRegisterFrom(source));
2772 } else {
2773 GetAssembler()->LoadFromOffset(kLoadWord,
2774 RegisterFrom(destination),
2775 sp,
2776 source.GetStackIndex());
2777 }
2778 } else if (destination.IsFpuRegister()) {
2779 if (source.IsRegister()) {
2780 __ Vmov(SRegisterFrom(destination), RegisterFrom(source));
2781 } else if (source.IsFpuRegister()) {
2782 __ Vmov(SRegisterFrom(destination), SRegisterFrom(source));
2783 } else {
2784 GetAssembler()->LoadSFromOffset(SRegisterFrom(destination), sp, source.GetStackIndex());
2785 }
2786 } else {
2787 DCHECK(destination.IsStackSlot()) << destination;
2788 if (source.IsRegister()) {
2789 GetAssembler()->StoreToOffset(kStoreWord,
2790 RegisterFrom(source),
2791 sp,
2792 destination.GetStackIndex());
2793 } else if (source.IsFpuRegister()) {
2794 GetAssembler()->StoreSToOffset(SRegisterFrom(source), sp, destination.GetStackIndex());
2795 } else {
2796 DCHECK(source.IsStackSlot()) << source;
2797 UseScratchRegisterScope temps(GetVIXLAssembler());
2798 vixl32::Register temp = temps.Acquire();
2799 GetAssembler()->LoadFromOffset(kLoadWord, temp, sp, source.GetStackIndex());
2800 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
2801 }
2802 }
2803}
2804
Artem Serovcfbe9132016-10-14 15:58:56 +01002805void CodeGeneratorARMVIXL::MoveConstant(Location location, int32_t value) {
2806 DCHECK(location.IsRegister());
2807 __ Mov(RegisterFrom(location), value);
Scott Wakelingfe885462016-09-22 10:24:38 +01002808}
2809
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002810void CodeGeneratorARMVIXL::MoveLocation(Location dst, Location src, DataType::Type dst_type) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002811 // TODO(VIXL): Maybe refactor to have the 'move' implementation here and use it in
2812 // `ParallelMoveResolverARMVIXL::EmitMove`, as is done in the `arm64` backend.
Vladimir Markoca6fff82017-10-03 14:49:14 +01002813 HParallelMove move(GetGraph()->GetAllocator());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002814 move.AddMove(src, dst, dst_type, nullptr);
2815 GetMoveResolver()->EmitNativeCode(&move);
Scott Wakelingfe885462016-09-22 10:24:38 +01002816}
2817
Artem Serovcfbe9132016-10-14 15:58:56 +01002818void CodeGeneratorARMVIXL::AddLocationAsTemp(Location location, LocationSummary* locations) {
2819 if (location.IsRegister()) {
2820 locations->AddTemp(location);
2821 } else if (location.IsRegisterPair()) {
2822 locations->AddTemp(LocationFrom(LowRegisterFrom(location)));
2823 locations->AddTemp(LocationFrom(HighRegisterFrom(location)));
2824 } else {
2825 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
2826 }
Scott Wakelingfe885462016-09-22 10:24:38 +01002827}
2828
2829void CodeGeneratorARMVIXL::InvokeRuntime(QuickEntrypointEnum entrypoint,
2830 HInstruction* instruction,
2831 uint32_t dex_pc,
2832 SlowPathCode* slow_path) {
2833 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Alexandre Rames374ddf32016-11-04 10:40:49 +00002834 __ Ldr(lr, MemOperand(tr, GetThreadOffset<kArmPointerSize>(entrypoint).Int32Value()));
2835 // Ensure the pc position is recorded immediately after the `blx` instruction.
2836 // 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 +00002837 ExactAssemblyScope aas(GetVIXLAssembler(),
2838 vixl32::k16BitT32InstructionSizeInBytes,
2839 CodeBufferCheckScope::kExactSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00002840 __ blx(lr);
Scott Wakelingfe885462016-09-22 10:24:38 +01002841 if (EntrypointRequiresStackMap(entrypoint)) {
2842 RecordPcInfo(instruction, dex_pc, slow_path);
2843 }
2844}
2845
2846void CodeGeneratorARMVIXL::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
2847 HInstruction* instruction,
2848 SlowPathCode* slow_path) {
2849 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Alexandre Rames374ddf32016-11-04 10:40:49 +00002850 __ Ldr(lr, MemOperand(tr, entry_point_offset));
Scott Wakelingfe885462016-09-22 10:24:38 +01002851 __ Blx(lr);
2852}
2853
Scott Wakelingfe885462016-09-22 10:24:38 +01002854void InstructionCodeGeneratorARMVIXL::HandleGoto(HInstruction* got, HBasicBlock* successor) {
2855 DCHECK(!successor->IsExitBlock());
2856 HBasicBlock* block = got->GetBlock();
2857 HInstruction* previous = got->GetPrevious();
2858 HLoopInformation* info = block->GetLoopInformation();
2859
2860 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Scott Wakelingfe885462016-09-22 10:24:38 +01002861 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2862 return;
2863 }
2864 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2865 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
Roland Levillain5daa4952017-07-03 17:23:56 +01002866 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 2);
Scott Wakelingfe885462016-09-22 10:24:38 +01002867 }
2868 if (!codegen_->GoesToNextBlock(block, successor)) {
2869 __ B(codegen_->GetLabelOf(successor));
2870 }
2871}
2872
2873void LocationsBuilderARMVIXL::VisitGoto(HGoto* got) {
2874 got->SetLocations(nullptr);
2875}
2876
2877void InstructionCodeGeneratorARMVIXL::VisitGoto(HGoto* got) {
2878 HandleGoto(got, got->GetSuccessor());
2879}
2880
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002881void LocationsBuilderARMVIXL::VisitTryBoundary(HTryBoundary* try_boundary) {
2882 try_boundary->SetLocations(nullptr);
2883}
2884
2885void InstructionCodeGeneratorARMVIXL::VisitTryBoundary(HTryBoundary* try_boundary) {
2886 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2887 if (!successor->IsExitBlock()) {
2888 HandleGoto(try_boundary, successor);
2889 }
2890}
2891
Scott Wakelingfe885462016-09-22 10:24:38 +01002892void LocationsBuilderARMVIXL::VisitExit(HExit* exit) {
2893 exit->SetLocations(nullptr);
2894}
2895
2896void InstructionCodeGeneratorARMVIXL::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
2897}
2898
Scott Wakelingfe885462016-09-22 10:24:38 +01002899void InstructionCodeGeneratorARMVIXL::GenerateCompareTestAndBranch(HCondition* condition,
Anton Kirilov23b752b2017-07-20 14:40:44 +01002900 vixl32::Label* true_target,
2901 vixl32::Label* false_target,
Anton Kirilovfd522532017-05-10 12:46:57 +01002902 bool is_far_target) {
Anton Kirilov23b752b2017-07-20 14:40:44 +01002903 if (true_target == false_target) {
2904 DCHECK(true_target != nullptr);
2905 __ B(true_target);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002906 return;
2907 }
2908
Anton Kirilov23b752b2017-07-20 14:40:44 +01002909 vixl32::Label* non_fallthrough_target;
2910 bool invert;
2911 bool emit_both_branches;
Scott Wakelingfe885462016-09-22 10:24:38 +01002912
Anton Kirilov23b752b2017-07-20 14:40:44 +01002913 if (true_target == nullptr) {
2914 // The true target is fallthrough.
2915 DCHECK(false_target != nullptr);
2916 non_fallthrough_target = false_target;
2917 invert = true;
2918 emit_both_branches = false;
2919 } else {
2920 non_fallthrough_target = true_target;
2921 invert = false;
2922 // Either the false target is fallthrough, or there is no fallthrough
2923 // and both branches must be emitted.
2924 emit_both_branches = (false_target != nullptr);
Scott Wakelingfe885462016-09-22 10:24:38 +01002925 }
2926
Anton Kirilov23b752b2017-07-20 14:40:44 +01002927 const auto cond = GenerateTest(condition, invert, codegen_);
2928
2929 __ B(cond.first, non_fallthrough_target, is_far_target);
2930
2931 if (emit_both_branches) {
2932 // No target falls through, we need to branch.
2933 __ B(false_target);
Scott Wakelingfe885462016-09-22 10:24:38 +01002934 }
2935}
2936
2937void InstructionCodeGeneratorARMVIXL::GenerateTestAndBranch(HInstruction* instruction,
2938 size_t condition_input_index,
2939 vixl32::Label* true_target,
xueliang.zhongf51bc622016-11-04 09:23:32 +00002940 vixl32::Label* false_target,
2941 bool far_target) {
Scott Wakelingfe885462016-09-22 10:24:38 +01002942 HInstruction* cond = instruction->InputAt(condition_input_index);
2943
2944 if (true_target == nullptr && false_target == nullptr) {
2945 // Nothing to do. The code always falls through.
2946 return;
2947 } else if (cond->IsIntConstant()) {
2948 // Constant condition, statically compared against "true" (integer value 1).
2949 if (cond->AsIntConstant()->IsTrue()) {
2950 if (true_target != nullptr) {
2951 __ B(true_target);
2952 }
2953 } else {
Anton Kirilov644032c2016-12-06 17:51:43 +00002954 DCHECK(cond->AsIntConstant()->IsFalse()) << Int32ConstantFrom(cond);
Scott Wakelingfe885462016-09-22 10:24:38 +01002955 if (false_target != nullptr) {
2956 __ B(false_target);
2957 }
2958 }
2959 return;
2960 }
2961
2962 // The following code generates these patterns:
2963 // (1) true_target == nullptr && false_target != nullptr
2964 // - opposite condition true => branch to false_target
2965 // (2) true_target != nullptr && false_target == nullptr
2966 // - condition true => branch to true_target
2967 // (3) true_target != nullptr && false_target != nullptr
2968 // - condition true => branch to true_target
2969 // - branch to false_target
2970 if (IsBooleanValueOrMaterializedCondition(cond)) {
2971 // Condition has been materialized, compare the output to 0.
2972 if (kIsDebugBuild) {
2973 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
2974 DCHECK(cond_val.IsRegister());
2975 }
2976 if (true_target == nullptr) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00002977 __ CompareAndBranchIfZero(InputRegisterAt(instruction, condition_input_index),
2978 false_target,
2979 far_target);
Scott Wakelingfe885462016-09-22 10:24:38 +01002980 } else {
xueliang.zhongf51bc622016-11-04 09:23:32 +00002981 __ CompareAndBranchIfNonZero(InputRegisterAt(instruction, condition_input_index),
2982 true_target,
2983 far_target);
Scott Wakelingfe885462016-09-22 10:24:38 +01002984 }
2985 } else {
2986 // Condition has not been materialized. Use its inputs as the comparison and
2987 // its condition as the branch condition.
2988 HCondition* condition = cond->AsCondition();
2989
2990 // If this is a long or FP comparison that has been folded into
2991 // the HCondition, generate the comparison directly.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002992 DataType::Type type = condition->InputAt(0)->GetType();
2993 if (type == DataType::Type::kInt64 || DataType::IsFloatingPointType(type)) {
Anton Kirilovfd522532017-05-10 12:46:57 +01002994 GenerateCompareTestAndBranch(condition, true_target, false_target, far_target);
Scott Wakelingfe885462016-09-22 10:24:38 +01002995 return;
2996 }
2997
Donghui Bai426b49c2016-11-08 14:55:38 +08002998 vixl32::Label* non_fallthrough_target;
2999 vixl32::Condition arm_cond = vixl32::Condition::None();
3000 const vixl32::Register left = InputRegisterAt(cond, 0);
3001 const Operand right = InputOperandAt(cond, 1);
3002
Scott Wakelingfe885462016-09-22 10:24:38 +01003003 if (true_target == nullptr) {
Donghui Bai426b49c2016-11-08 14:55:38 +08003004 arm_cond = ARMCondition(condition->GetOppositeCondition());
3005 non_fallthrough_target = false_target;
Scott Wakelingfe885462016-09-22 10:24:38 +01003006 } else {
Donghui Bai426b49c2016-11-08 14:55:38 +08003007 arm_cond = ARMCondition(condition->GetCondition());
3008 non_fallthrough_target = true_target;
3009 }
3010
3011 if (right.IsImmediate() && right.GetImmediate() == 0 && (arm_cond.Is(ne) || arm_cond.Is(eq))) {
3012 if (arm_cond.Is(eq)) {
Anton Kirilovfd522532017-05-10 12:46:57 +01003013 __ CompareAndBranchIfZero(left, non_fallthrough_target, far_target);
Donghui Bai426b49c2016-11-08 14:55:38 +08003014 } else {
3015 DCHECK(arm_cond.Is(ne));
Anton Kirilovfd522532017-05-10 12:46:57 +01003016 __ CompareAndBranchIfNonZero(left, non_fallthrough_target, far_target);
Donghui Bai426b49c2016-11-08 14:55:38 +08003017 }
3018 } else {
3019 __ Cmp(left, right);
Anton Kirilovfd522532017-05-10 12:46:57 +01003020 __ B(arm_cond, non_fallthrough_target, far_target);
Scott Wakelingfe885462016-09-22 10:24:38 +01003021 }
3022 }
3023
3024 // If neither branch falls through (case 3), the conditional branch to `true_target`
3025 // was already emitted (case 2) and we need to emit a jump to `false_target`.
3026 if (true_target != nullptr && false_target != nullptr) {
3027 __ B(false_target);
3028 }
3029}
3030
3031void LocationsBuilderARMVIXL::VisitIf(HIf* if_instr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003032 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(if_instr);
Scott Wakelingfe885462016-09-22 10:24:38 +01003033 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
3034 locations->SetInAt(0, Location::RequiresRegister());
3035 }
3036}
3037
3038void InstructionCodeGeneratorARMVIXL::VisitIf(HIf* if_instr) {
3039 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
3040 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003041 vixl32::Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
3042 nullptr : codegen_->GetLabelOf(true_successor);
3043 vixl32::Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
3044 nullptr : codegen_->GetLabelOf(false_successor);
Scott Wakelingfe885462016-09-22 10:24:38 +01003045 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
3046}
3047
Scott Wakelingc34dba72016-10-03 10:14:44 +01003048void LocationsBuilderARMVIXL::VisitDeoptimize(HDeoptimize* deoptimize) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003049 LocationSummary* locations = new (GetGraph()->GetAllocator())
Scott Wakelingc34dba72016-10-03 10:14:44 +01003050 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003051 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3052 RegisterSet caller_saves = RegisterSet::Empty();
3053 caller_saves.Add(LocationFrom(calling_convention.GetRegisterAt(0)));
3054 locations->SetCustomSlowPathCallerSaves(caller_saves);
Scott Wakelingc34dba72016-10-03 10:14:44 +01003055 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
3056 locations->SetInAt(0, Location::RequiresRegister());
3057 }
3058}
3059
3060void InstructionCodeGeneratorARMVIXL::VisitDeoptimize(HDeoptimize* deoptimize) {
3061 SlowPathCodeARMVIXL* slow_path =
3062 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARMVIXL>(deoptimize);
3063 GenerateTestAndBranch(deoptimize,
3064 /* condition_input_index */ 0,
3065 slow_path->GetEntryLabel(),
3066 /* false_target */ nullptr);
3067}
3068
Artem Serovd4cc5b22016-11-04 11:19:09 +00003069void LocationsBuilderARMVIXL::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003070 LocationSummary* locations = new (GetGraph()->GetAllocator())
Artem Serovd4cc5b22016-11-04 11:19:09 +00003071 LocationSummary(flag, LocationSummary::kNoCall);
3072 locations->SetOut(Location::RequiresRegister());
3073}
3074
3075void InstructionCodeGeneratorARMVIXL::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
3076 GetAssembler()->LoadFromOffset(kLoadWord,
3077 OutputRegister(flag),
3078 sp,
3079 codegen_->GetStackOffsetOfShouldDeoptimizeFlag());
3080}
3081
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003082void LocationsBuilderARMVIXL::VisitSelect(HSelect* select) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003083 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(select);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003084 const bool is_floating_point = DataType::IsFloatingPointType(select->GetType());
Donghui Bai426b49c2016-11-08 14:55:38 +08003085
3086 if (is_floating_point) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003087 locations->SetInAt(0, Location::RequiresFpuRegister());
Donghui Bai426b49c2016-11-08 14:55:38 +08003088 locations->SetInAt(1, Location::FpuRegisterOrConstant(select->GetTrueValue()));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003089 } else {
3090 locations->SetInAt(0, Location::RequiresRegister());
Donghui Bai426b49c2016-11-08 14:55:38 +08003091 locations->SetInAt(1, Arm8BitEncodableConstantOrRegister(select->GetTrueValue()));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003092 }
Donghui Bai426b49c2016-11-08 14:55:38 +08003093
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003094 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
Donghui Bai426b49c2016-11-08 14:55:38 +08003095 locations->SetInAt(2, Location::RegisterOrConstant(select->GetCondition()));
3096 // The code generator handles overlap with the values, but not with the condition.
3097 locations->SetOut(Location::SameAsFirstInput());
3098 } else if (is_floating_point) {
3099 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3100 } else {
3101 if (!locations->InAt(1).IsConstant()) {
3102 locations->SetInAt(0, Arm8BitEncodableConstantOrRegister(select->GetFalseValue()));
3103 }
3104
3105 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003106 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003107}
3108
3109void InstructionCodeGeneratorARMVIXL::VisitSelect(HSelect* select) {
Donghui Bai426b49c2016-11-08 14:55:38 +08003110 HInstruction* const condition = select->GetCondition();
3111 const LocationSummary* const locations = select->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003112 const DataType::Type type = select->GetType();
Donghui Bai426b49c2016-11-08 14:55:38 +08003113 const Location first = locations->InAt(0);
3114 const Location out = locations->Out();
3115 const Location second = locations->InAt(1);
3116 Location src;
3117
3118 if (condition->IsIntConstant()) {
3119 if (condition->AsIntConstant()->IsFalse()) {
3120 src = first;
3121 } else {
3122 src = second;
3123 }
3124
3125 codegen_->MoveLocation(out, src, type);
3126 return;
3127 }
3128
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003129 if (!DataType::IsFloatingPointType(type)) {
Donghui Bai426b49c2016-11-08 14:55:38 +08003130 bool invert = false;
3131
3132 if (out.Equals(second)) {
3133 src = first;
3134 invert = true;
3135 } else if (out.Equals(first)) {
3136 src = second;
3137 } else if (second.IsConstant()) {
3138 DCHECK(CanEncodeConstantAs8BitImmediate(second.GetConstant()));
3139 src = second;
3140 } else if (first.IsConstant()) {
3141 DCHECK(CanEncodeConstantAs8BitImmediate(first.GetConstant()));
3142 src = first;
3143 invert = true;
3144 } else {
3145 src = second;
3146 }
3147
3148 if (CanGenerateConditionalMove(out, src)) {
3149 if (!out.Equals(first) && !out.Equals(second)) {
3150 codegen_->MoveLocation(out, src.Equals(first) ? second : first, type);
3151 }
3152
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003153 std::pair<vixl32::Condition, vixl32::Condition> cond(eq, ne);
3154
3155 if (IsBooleanValueOrMaterializedCondition(condition)) {
3156 __ Cmp(InputRegisterAt(select, 2), 0);
3157 cond = invert ? std::make_pair(eq, ne) : std::make_pair(ne, eq);
3158 } else {
3159 cond = GenerateTest(condition->AsCondition(), invert, codegen_);
3160 }
3161
Donghui Bai426b49c2016-11-08 14:55:38 +08003162 const size_t instr_count = out.IsRegisterPair() ? 4 : 2;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003163 // We use the scope because of the IT block that follows.
Donghui Bai426b49c2016-11-08 14:55:38 +08003164 ExactAssemblyScope guard(GetVIXLAssembler(),
3165 instr_count * vixl32::k16BitT32InstructionSizeInBytes,
3166 CodeBufferCheckScope::kExactSize);
3167
3168 if (out.IsRegister()) {
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003169 __ it(cond.first);
3170 __ mov(cond.first, RegisterFrom(out), OperandFrom(src, type));
Donghui Bai426b49c2016-11-08 14:55:38 +08003171 } else {
3172 DCHECK(out.IsRegisterPair());
3173
3174 Operand operand_high(0);
3175 Operand operand_low(0);
3176
3177 if (src.IsConstant()) {
3178 const int64_t value = Int64ConstantFrom(src);
3179
3180 operand_high = High32Bits(value);
3181 operand_low = Low32Bits(value);
3182 } else {
3183 DCHECK(src.IsRegisterPair());
3184 operand_high = HighRegisterFrom(src);
3185 operand_low = LowRegisterFrom(src);
3186 }
3187
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003188 __ it(cond.first);
3189 __ mov(cond.first, LowRegisterFrom(out), operand_low);
3190 __ it(cond.first);
3191 __ mov(cond.first, HighRegisterFrom(out), operand_high);
Donghui Bai426b49c2016-11-08 14:55:38 +08003192 }
3193
3194 return;
3195 }
3196 }
3197
3198 vixl32::Label* false_target = nullptr;
3199 vixl32::Label* true_target = nullptr;
3200 vixl32::Label select_end;
3201 vixl32::Label* const target = codegen_->GetFinalLabel(select, &select_end);
3202
3203 if (out.Equals(second)) {
3204 true_target = target;
3205 src = first;
3206 } else {
3207 false_target = target;
3208 src = second;
3209
3210 if (!out.Equals(first)) {
3211 codegen_->MoveLocation(out, first, type);
3212 }
3213 }
3214
3215 GenerateTestAndBranch(select, 2, true_target, false_target, /* far_target */ false);
3216 codegen_->MoveLocation(out, src, type);
3217
3218 if (select_end.IsReferenced()) {
3219 __ Bind(&select_end);
3220 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003221}
3222
Artem Serov551b28f2016-10-18 19:11:30 +01003223void LocationsBuilderARMVIXL::VisitNativeDebugInfo(HNativeDebugInfo* info) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003224 new (GetGraph()->GetAllocator()) LocationSummary(info);
Artem Serov551b28f2016-10-18 19:11:30 +01003225}
3226
3227void InstructionCodeGeneratorARMVIXL::VisitNativeDebugInfo(HNativeDebugInfo*) {
3228 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
3229}
3230
Scott Wakelingfe885462016-09-22 10:24:38 +01003231void CodeGeneratorARMVIXL::GenerateNop() {
3232 __ Nop();
3233}
3234
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003235// `temp` is an extra temporary register that is used for some conditions;
3236// callers may not specify it, in which case the method will use a scratch
3237// register instead.
3238void CodeGeneratorARMVIXL::GenerateConditionWithZero(IfCondition condition,
3239 vixl32::Register out,
3240 vixl32::Register in,
3241 vixl32::Register temp) {
3242 switch (condition) {
3243 case kCondEQ:
3244 // x <= 0 iff x == 0 when the comparison is unsigned.
3245 case kCondBE:
3246 if (!temp.IsValid() || (out.IsLow() && !out.Is(in))) {
3247 temp = out;
3248 }
3249
3250 // Avoid 32-bit instructions if possible; note that `in` and `temp` must be
3251 // different as well.
3252 if (in.IsLow() && temp.IsLow() && !in.Is(temp)) {
3253 // temp = - in; only 0 sets the carry flag.
3254 __ Rsbs(temp, in, 0);
3255
3256 if (out.Is(in)) {
3257 std::swap(in, temp);
3258 }
3259
3260 // out = - in + in + carry = carry
3261 __ Adc(out, temp, in);
3262 } else {
3263 // If `in` is 0, then it has 32 leading zeros, and less than that otherwise.
3264 __ Clz(out, in);
3265 // Any number less than 32 logically shifted right by 5 bits results in 0;
3266 // the same operation on 32 yields 1.
3267 __ Lsr(out, out, 5);
3268 }
3269
3270 break;
3271 case kCondNE:
3272 // x > 0 iff x != 0 when the comparison is unsigned.
3273 case kCondA: {
3274 UseScratchRegisterScope temps(GetVIXLAssembler());
3275
3276 if (out.Is(in)) {
3277 if (!temp.IsValid() || in.Is(temp)) {
3278 temp = temps.Acquire();
3279 }
3280 } else if (!temp.IsValid() || !temp.IsLow()) {
3281 temp = out;
3282 }
3283
3284 // temp = in - 1; only 0 does not set the carry flag.
3285 __ Subs(temp, in, 1);
3286 // out = in + ~temp + carry = in + (-(in - 1) - 1) + carry = in - in + 1 - 1 + carry = carry
3287 __ Sbc(out, in, temp);
3288 break;
3289 }
3290 case kCondGE:
3291 __ Mvn(out, in);
3292 in = out;
3293 FALLTHROUGH_INTENDED;
3294 case kCondLT:
3295 // We only care about the sign bit.
3296 __ Lsr(out, in, 31);
3297 break;
3298 case kCondAE:
3299 // Trivially true.
3300 __ Mov(out, 1);
3301 break;
3302 case kCondB:
3303 // Trivially false.
3304 __ Mov(out, 0);
3305 break;
3306 default:
3307 LOG(FATAL) << "Unexpected condition " << condition;
3308 UNREACHABLE();
3309 }
3310}
3311
Scott Wakelingfe885462016-09-22 10:24:38 +01003312void LocationsBuilderARMVIXL::HandleCondition(HCondition* cond) {
3313 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003314 new (GetGraph()->GetAllocator()) LocationSummary(cond, LocationSummary::kNoCall);
Scott Wakelingfe885462016-09-22 10:24:38 +01003315 // Handle the long/FP comparisons made in instruction simplification.
3316 switch (cond->InputAt(0)->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003317 case DataType::Type::kInt64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003318 locations->SetInAt(0, Location::RequiresRegister());
3319 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
3320 if (!cond->IsEmittedAtUseSite()) {
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003321 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Scott Wakelingfe885462016-09-22 10:24:38 +01003322 }
3323 break;
3324
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003325 case DataType::Type::kFloat32:
3326 case DataType::Type::kFloat64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003327 locations->SetInAt(0, Location::RequiresFpuRegister());
Artem Serov657022c2016-11-23 14:19:38 +00003328 locations->SetInAt(1, ArithmeticZeroOrFpuRegister(cond->InputAt(1)));
Scott Wakelingfe885462016-09-22 10:24:38 +01003329 if (!cond->IsEmittedAtUseSite()) {
3330 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3331 }
3332 break;
3333
3334 default:
3335 locations->SetInAt(0, Location::RequiresRegister());
3336 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
3337 if (!cond->IsEmittedAtUseSite()) {
3338 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3339 }
3340 }
3341}
3342
3343void InstructionCodeGeneratorARMVIXL::HandleCondition(HCondition* cond) {
3344 if (cond->IsEmittedAtUseSite()) {
3345 return;
3346 }
3347
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003348 const DataType::Type type = cond->GetLeft()->GetType();
Scott Wakelingfe885462016-09-22 10:24:38 +01003349
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003350 if (DataType::IsFloatingPointType(type)) {
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003351 GenerateConditionGeneric(cond, codegen_);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003352 return;
Scott Wakelingfe885462016-09-22 10:24:38 +01003353 }
3354
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003355 DCHECK(DataType::IsIntegralType(type) || type == DataType::Type::kReference) << type;
Scott Wakelingfe885462016-09-22 10:24:38 +01003356
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003357 const IfCondition condition = cond->GetCondition();
Scott Wakelingfe885462016-09-22 10:24:38 +01003358
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003359 // A condition with only one boolean input, or two boolean inputs without being equality or
3360 // inequality results from transformations done by the instruction simplifier, and is handled
3361 // as a regular condition with integral inputs.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003362 if (type == DataType::Type::kBool &&
3363 cond->GetRight()->GetType() == DataType::Type::kBool &&
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003364 (condition == kCondEQ || condition == kCondNE)) {
3365 vixl32::Register left = InputRegisterAt(cond, 0);
3366 const vixl32::Register out = OutputRegister(cond);
3367 const Location right_loc = cond->GetLocations()->InAt(1);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003368
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003369 // The constant case is handled by the instruction simplifier.
3370 DCHECK(!right_loc.IsConstant());
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003371
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003372 vixl32::Register right = RegisterFrom(right_loc);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003373
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003374 // Avoid 32-bit instructions if possible.
3375 if (out.Is(right)) {
3376 std::swap(left, right);
3377 }
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003378
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003379 __ Eor(out, left, right);
3380
3381 if (condition == kCondEQ) {
3382 __ Eor(out, out, 1);
3383 }
3384
3385 return;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003386 }
Anton Kirilov6f644202017-02-27 18:29:45 +00003387
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003388 GenerateConditionIntegralOrNonPrimitive(cond, codegen_);
Scott Wakelingfe885462016-09-22 10:24:38 +01003389}
3390
3391void LocationsBuilderARMVIXL::VisitEqual(HEqual* comp) {
3392 HandleCondition(comp);
3393}
3394
3395void InstructionCodeGeneratorARMVIXL::VisitEqual(HEqual* comp) {
3396 HandleCondition(comp);
3397}
3398
3399void LocationsBuilderARMVIXL::VisitNotEqual(HNotEqual* comp) {
3400 HandleCondition(comp);
3401}
3402
3403void InstructionCodeGeneratorARMVIXL::VisitNotEqual(HNotEqual* comp) {
3404 HandleCondition(comp);
3405}
3406
3407void LocationsBuilderARMVIXL::VisitLessThan(HLessThan* comp) {
3408 HandleCondition(comp);
3409}
3410
3411void InstructionCodeGeneratorARMVIXL::VisitLessThan(HLessThan* comp) {
3412 HandleCondition(comp);
3413}
3414
3415void LocationsBuilderARMVIXL::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
3416 HandleCondition(comp);
3417}
3418
3419void InstructionCodeGeneratorARMVIXL::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
3420 HandleCondition(comp);
3421}
3422
3423void LocationsBuilderARMVIXL::VisitGreaterThan(HGreaterThan* comp) {
3424 HandleCondition(comp);
3425}
3426
3427void InstructionCodeGeneratorARMVIXL::VisitGreaterThan(HGreaterThan* comp) {
3428 HandleCondition(comp);
3429}
3430
3431void LocationsBuilderARMVIXL::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
3432 HandleCondition(comp);
3433}
3434
3435void InstructionCodeGeneratorARMVIXL::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
3436 HandleCondition(comp);
3437}
3438
3439void LocationsBuilderARMVIXL::VisitBelow(HBelow* comp) {
3440 HandleCondition(comp);
3441}
3442
3443void InstructionCodeGeneratorARMVIXL::VisitBelow(HBelow* comp) {
3444 HandleCondition(comp);
3445}
3446
3447void LocationsBuilderARMVIXL::VisitBelowOrEqual(HBelowOrEqual* comp) {
3448 HandleCondition(comp);
3449}
3450
3451void InstructionCodeGeneratorARMVIXL::VisitBelowOrEqual(HBelowOrEqual* comp) {
3452 HandleCondition(comp);
3453}
3454
3455void LocationsBuilderARMVIXL::VisitAbove(HAbove* comp) {
3456 HandleCondition(comp);
3457}
3458
3459void InstructionCodeGeneratorARMVIXL::VisitAbove(HAbove* comp) {
3460 HandleCondition(comp);
3461}
3462
3463void LocationsBuilderARMVIXL::VisitAboveOrEqual(HAboveOrEqual* comp) {
3464 HandleCondition(comp);
3465}
3466
3467void InstructionCodeGeneratorARMVIXL::VisitAboveOrEqual(HAboveOrEqual* comp) {
3468 HandleCondition(comp);
3469}
3470
3471void LocationsBuilderARMVIXL::VisitIntConstant(HIntConstant* constant) {
3472 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003473 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Scott Wakelingfe885462016-09-22 10:24:38 +01003474 locations->SetOut(Location::ConstantLocation(constant));
3475}
3476
3477void InstructionCodeGeneratorARMVIXL::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
3478 // Will be generated at use site.
3479}
3480
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003481void LocationsBuilderARMVIXL::VisitNullConstant(HNullConstant* constant) {
3482 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003483 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003484 locations->SetOut(Location::ConstantLocation(constant));
3485}
3486
3487void InstructionCodeGeneratorARMVIXL::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
3488 // Will be generated at use site.
3489}
3490
Scott Wakelingfe885462016-09-22 10:24:38 +01003491void LocationsBuilderARMVIXL::VisitLongConstant(HLongConstant* constant) {
3492 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003493 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Scott Wakelingfe885462016-09-22 10:24:38 +01003494 locations->SetOut(Location::ConstantLocation(constant));
3495}
3496
3497void InstructionCodeGeneratorARMVIXL::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
3498 // Will be generated at use site.
3499}
3500
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01003501void LocationsBuilderARMVIXL::VisitFloatConstant(HFloatConstant* constant) {
3502 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003503 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01003504 locations->SetOut(Location::ConstantLocation(constant));
3505}
3506
Scott Wakelingc34dba72016-10-03 10:14:44 +01003507void InstructionCodeGeneratorARMVIXL::VisitFloatConstant(
3508 HFloatConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01003509 // Will be generated at use site.
3510}
3511
3512void LocationsBuilderARMVIXL::VisitDoubleConstant(HDoubleConstant* constant) {
3513 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003514 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01003515 locations->SetOut(Location::ConstantLocation(constant));
3516}
3517
Scott Wakelingc34dba72016-10-03 10:14:44 +01003518void InstructionCodeGeneratorARMVIXL::VisitDoubleConstant(
3519 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01003520 // Will be generated at use site.
3521}
3522
Igor Murashkind01745e2017-04-05 16:40:31 -07003523void LocationsBuilderARMVIXL::VisitConstructorFence(HConstructorFence* constructor_fence) {
3524 constructor_fence->SetLocations(nullptr);
3525}
3526
3527void InstructionCodeGeneratorARMVIXL::VisitConstructorFence(
3528 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
3529 codegen_->GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
3530}
3531
Scott Wakelingfe885462016-09-22 10:24:38 +01003532void LocationsBuilderARMVIXL::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3533 memory_barrier->SetLocations(nullptr);
3534}
3535
3536void InstructionCodeGeneratorARMVIXL::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3537 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
3538}
3539
3540void LocationsBuilderARMVIXL::VisitReturnVoid(HReturnVoid* ret) {
3541 ret->SetLocations(nullptr);
3542}
3543
3544void InstructionCodeGeneratorARMVIXL::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
3545 codegen_->GenerateFrameExit();
3546}
3547
3548void LocationsBuilderARMVIXL::VisitReturn(HReturn* ret) {
3549 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003550 new (GetGraph()->GetAllocator()) LocationSummary(ret, LocationSummary::kNoCall);
Scott Wakelingfe885462016-09-22 10:24:38 +01003551 locations->SetInAt(0, parameter_visitor_.GetReturnLocation(ret->InputAt(0)->GetType()));
3552}
3553
3554void InstructionCodeGeneratorARMVIXL::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
3555 codegen_->GenerateFrameExit();
3556}
3557
Artem Serovcfbe9132016-10-14 15:58:56 +01003558void LocationsBuilderARMVIXL::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3559 // The trampoline uses the same calling convention as dex calling conventions,
3560 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
3561 // the method_idx.
3562 HandleInvoke(invoke);
3563}
3564
3565void InstructionCodeGeneratorARMVIXL::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3566 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
Roland Levillain5daa4952017-07-03 17:23:56 +01003567 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 3);
Artem Serovcfbe9132016-10-14 15:58:56 +01003568}
3569
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003570void LocationsBuilderARMVIXL::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
3571 // Explicit clinit checks triggered by static invokes must have been pruned by
3572 // art::PrepareForRegisterAllocation.
3573 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
3574
Anton Kirilov5ec62182016-10-13 20:16:02 +01003575 IntrinsicLocationsBuilderARMVIXL intrinsic(codegen_);
3576 if (intrinsic.TryDispatch(invoke)) {
Anton Kirilov5ec62182016-10-13 20:16:02 +01003577 return;
3578 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003579
3580 HandleInvoke(invoke);
3581}
3582
Anton Kirilov5ec62182016-10-13 20:16:02 +01003583static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARMVIXL* codegen) {
3584 if (invoke->GetLocations()->Intrinsified()) {
3585 IntrinsicCodeGeneratorARMVIXL intrinsic(codegen);
3586 intrinsic.Dispatch(invoke);
3587 return true;
3588 }
3589 return false;
3590}
3591
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003592void InstructionCodeGeneratorARMVIXL::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
3593 // Explicit clinit checks triggered by static invokes must have been pruned by
3594 // art::PrepareForRegisterAllocation.
3595 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
3596
Anton Kirilov5ec62182016-10-13 20:16:02 +01003597 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
Roland Levillain5daa4952017-07-03 17:23:56 +01003598 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 4);
Anton Kirilov5ec62182016-10-13 20:16:02 +01003599 return;
3600 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003601
3602 LocationSummary* locations = invoke->GetLocations();
Artem Serovd4cc5b22016-11-04 11:19:09 +00003603 codegen_->GenerateStaticOrDirectCall(
3604 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Roland Levillain5daa4952017-07-03 17:23:56 +01003605
3606 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 5);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003607}
3608
3609void LocationsBuilderARMVIXL::HandleInvoke(HInvoke* invoke) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00003610 InvokeDexCallingConventionVisitorARMVIXL calling_convention_visitor;
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003611 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
3612}
3613
3614void LocationsBuilderARMVIXL::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Anton Kirilov5ec62182016-10-13 20:16:02 +01003615 IntrinsicLocationsBuilderARMVIXL intrinsic(codegen_);
3616 if (intrinsic.TryDispatch(invoke)) {
3617 return;
3618 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003619
3620 HandleInvoke(invoke);
3621}
3622
3623void InstructionCodeGeneratorARMVIXL::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Anton Kirilov5ec62182016-10-13 20:16:02 +01003624 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
Roland Levillain5daa4952017-07-03 17:23:56 +01003625 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 6);
Anton Kirilov5ec62182016-10-13 20:16:02 +01003626 return;
3627 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003628
3629 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexandre Rames374ddf32016-11-04 10:40:49 +00003630 DCHECK(!codegen_->IsLeafMethod());
Roland Levillain5daa4952017-07-03 17:23:56 +01003631
3632 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 7);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003633}
3634
Artem Serovcfbe9132016-10-14 15:58:56 +01003635void LocationsBuilderARMVIXL::VisitInvokeInterface(HInvokeInterface* invoke) {
3636 HandleInvoke(invoke);
3637 // Add the hidden argument.
3638 invoke->GetLocations()->AddTemp(LocationFrom(r12));
3639}
3640
3641void InstructionCodeGeneratorARMVIXL::VisitInvokeInterface(HInvokeInterface* invoke) {
3642 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
3643 LocationSummary* locations = invoke->GetLocations();
3644 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
3645 vixl32::Register hidden_reg = RegisterFrom(locations->GetTemp(1));
3646 Location receiver = locations->InAt(0);
3647 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3648
3649 DCHECK(!receiver.IsStackSlot());
3650
Alexandre Rames374ddf32016-11-04 10:40:49 +00003651 // Ensure the pc position is recorded immediately after the `ldr` instruction.
3652 {
Artem Serov0fb37192016-12-06 18:13:40 +00003653 ExactAssemblyScope aas(GetVIXLAssembler(),
3654 vixl32::kMaxInstructionSizeInBytes,
3655 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00003656 // /* HeapReference<Class> */ temp = receiver->klass_
3657 __ ldr(temp, MemOperand(RegisterFrom(receiver), class_offset));
3658 codegen_->MaybeRecordImplicitNullCheck(invoke);
3659 }
Artem Serovcfbe9132016-10-14 15:58:56 +01003660 // Instead of simply (possibly) unpoisoning `temp` here, we should
3661 // emit a read barrier for the previous class reference load.
3662 // However this is not required in practice, as this is an
3663 // intermediate/temporary reference and because the current
3664 // concurrent copying collector keeps the from-space memory
3665 // intact/accessible until the end of the marking phase (the
3666 // concurrent copying collector may not in the future).
3667 GetAssembler()->MaybeUnpoisonHeapReference(temp);
3668 GetAssembler()->LoadFromOffset(kLoadWord,
3669 temp,
3670 temp,
3671 mirror::Class::ImtPtrOffset(kArmPointerSize).Uint32Value());
3672 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
3673 invoke->GetImtIndex(), kArmPointerSize));
3674 // temp = temp->GetImtEntryAt(method_offset);
3675 GetAssembler()->LoadFromOffset(kLoadWord, temp, temp, method_offset);
3676 uint32_t entry_point =
3677 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize).Int32Value();
3678 // LR = temp->GetEntryPoint();
3679 GetAssembler()->LoadFromOffset(kLoadWord, lr, temp, entry_point);
3680
3681 // Set the hidden (in r12) argument. It is done here, right before a BLX to prevent other
3682 // instruction from clobbering it as they might use r12 as a scratch register.
3683 DCHECK(hidden_reg.Is(r12));
Scott Wakelingb77051e2016-11-21 19:46:00 +00003684
3685 {
3686 // The VIXL macro assembler may clobber any of the scratch registers that are available to it,
3687 // so it checks if the application is using them (by passing them to the macro assembler
3688 // methods). The following application of UseScratchRegisterScope corrects VIXL's notion of
3689 // what is available, and is the opposite of the standard usage: Instead of requesting a
3690 // temporary location, it imposes an external constraint (i.e. a specific register is reserved
3691 // for the hidden argument). Note that this works even if VIXL needs a scratch register itself
3692 // (to materialize the constant), since the destination register becomes available for such use
3693 // internally for the duration of the macro instruction.
3694 UseScratchRegisterScope temps(GetVIXLAssembler());
3695 temps.Exclude(hidden_reg);
3696 __ Mov(hidden_reg, invoke->GetDexMethodIndex());
3697 }
Artem Serovcfbe9132016-10-14 15:58:56 +01003698 {
Alexandre Rames374ddf32016-11-04 10:40:49 +00003699 // Ensure the pc position is recorded immediately after the `blx` instruction.
3700 // 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 +00003701 ExactAssemblyScope aas(GetVIXLAssembler(),
Alexandre Rames374ddf32016-11-04 10:40:49 +00003702 vixl32::k16BitT32InstructionSizeInBytes,
3703 CodeBufferCheckScope::kExactSize);
Artem Serovcfbe9132016-10-14 15:58:56 +01003704 // LR();
3705 __ blx(lr);
Artem Serovcfbe9132016-10-14 15:58:56 +01003706 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Alexandre Rames374ddf32016-11-04 10:40:49 +00003707 DCHECK(!codegen_->IsLeafMethod());
Artem Serovcfbe9132016-10-14 15:58:56 +01003708 }
Roland Levillain5daa4952017-07-03 17:23:56 +01003709
3710 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 8);
Artem Serovcfbe9132016-10-14 15:58:56 +01003711}
3712
Orion Hodsonac141392017-01-13 11:53:47 +00003713void LocationsBuilderARMVIXL::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
3714 HandleInvoke(invoke);
3715}
3716
3717void InstructionCodeGeneratorARMVIXL::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
3718 codegen_->GenerateInvokePolymorphicCall(invoke);
Roland Levillain5daa4952017-07-03 17:23:56 +01003719 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 9);
Orion Hodsonac141392017-01-13 11:53:47 +00003720}
3721
Artem Serov02109dd2016-09-23 17:17:54 +01003722void LocationsBuilderARMVIXL::VisitNeg(HNeg* neg) {
3723 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003724 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Artem Serov02109dd2016-09-23 17:17:54 +01003725 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003726 case DataType::Type::kInt32: {
Artem Serov02109dd2016-09-23 17:17:54 +01003727 locations->SetInAt(0, Location::RequiresRegister());
3728 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3729 break;
3730 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003731 case DataType::Type::kInt64: {
Artem Serov02109dd2016-09-23 17:17:54 +01003732 locations->SetInAt(0, Location::RequiresRegister());
3733 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3734 break;
3735 }
3736
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003737 case DataType::Type::kFloat32:
3738 case DataType::Type::kFloat64:
Artem Serov02109dd2016-09-23 17:17:54 +01003739 locations->SetInAt(0, Location::RequiresFpuRegister());
3740 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3741 break;
3742
3743 default:
3744 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
3745 }
3746}
3747
3748void InstructionCodeGeneratorARMVIXL::VisitNeg(HNeg* neg) {
3749 LocationSummary* locations = neg->GetLocations();
3750 Location out = locations->Out();
3751 Location in = locations->InAt(0);
3752 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003753 case DataType::Type::kInt32:
Artem Serov02109dd2016-09-23 17:17:54 +01003754 __ Rsb(OutputRegister(neg), InputRegisterAt(neg, 0), 0);
3755 break;
3756
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003757 case DataType::Type::kInt64:
Artem Serov02109dd2016-09-23 17:17:54 +01003758 // out.lo = 0 - in.lo (and update the carry/borrow (C) flag)
3759 __ Rsbs(LowRegisterFrom(out), LowRegisterFrom(in), 0);
3760 // We cannot emit an RSC (Reverse Subtract with Carry)
3761 // instruction here, as it does not exist in the Thumb-2
3762 // instruction set. We use the following approach
3763 // using SBC and SUB instead.
3764 //
3765 // out.hi = -C
3766 __ Sbc(HighRegisterFrom(out), HighRegisterFrom(out), HighRegisterFrom(out));
3767 // out.hi = out.hi - in.hi
3768 __ Sub(HighRegisterFrom(out), HighRegisterFrom(out), HighRegisterFrom(in));
3769 break;
3770
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003771 case DataType::Type::kFloat32:
3772 case DataType::Type::kFloat64:
Anton Kirilov644032c2016-12-06 17:51:43 +00003773 __ Vneg(OutputVRegister(neg), InputVRegister(neg));
Artem Serov02109dd2016-09-23 17:17:54 +01003774 break;
3775
3776 default:
3777 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
3778 }
3779}
3780
Scott Wakelingfe885462016-09-22 10:24:38 +01003781void LocationsBuilderARMVIXL::VisitTypeConversion(HTypeConversion* conversion) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003782 DataType::Type result_type = conversion->GetResultType();
3783 DataType::Type input_type = conversion->GetInputType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003784 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
3785 << input_type << " -> " << result_type;
Scott Wakelingfe885462016-09-22 10:24:38 +01003786
3787 // The float-to-long, double-to-long and long-to-float type conversions
3788 // rely on a call to the runtime.
3789 LocationSummary::CallKind call_kind =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003790 (((input_type == DataType::Type::kFloat32 || input_type == DataType::Type::kFloat64)
3791 && result_type == DataType::Type::kInt64)
3792 || (input_type == DataType::Type::kInt64 && result_type == DataType::Type::kFloat32))
Scott Wakelingfe885462016-09-22 10:24:38 +01003793 ? LocationSummary::kCallOnMainOnly
3794 : LocationSummary::kNoCall;
3795 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003796 new (GetGraph()->GetAllocator()) LocationSummary(conversion, call_kind);
Scott Wakelingfe885462016-09-22 10:24:38 +01003797
Scott Wakelingfe885462016-09-22 10:24:38 +01003798 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003799 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003800 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003801 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003802 case DataType::Type::kInt16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003803 DCHECK(DataType::IsIntegralType(input_type)) << input_type;
3804 locations->SetInAt(0, Location::RequiresRegister());
3805 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Scott Wakelingfe885462016-09-22 10:24:38 +01003806 break;
3807
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003808 case DataType::Type::kInt32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003809 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003810 case DataType::Type::kInt64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003811 locations->SetInAt(0, Location::Any());
3812 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3813 break;
3814
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003815 case DataType::Type::kFloat32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003816 locations->SetInAt(0, Location::RequiresFpuRegister());
3817 locations->SetOut(Location::RequiresRegister());
3818 locations->AddTemp(Location::RequiresFpuRegister());
3819 break;
3820
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003821 case DataType::Type::kFloat64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003822 locations->SetInAt(0, Location::RequiresFpuRegister());
3823 locations->SetOut(Location::RequiresRegister());
3824 locations->AddTemp(Location::RequiresFpuRegister());
3825 break;
3826
3827 default:
3828 LOG(FATAL) << "Unexpected type conversion from " << input_type
3829 << " to " << result_type;
3830 }
3831 break;
3832
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003833 case DataType::Type::kInt64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003834 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003835 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003836 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003837 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003838 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003839 case DataType::Type::kInt16:
3840 case DataType::Type::kInt32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003841 locations->SetInAt(0, Location::RequiresRegister());
3842 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3843 break;
3844
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003845 case DataType::Type::kFloat32: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003846 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3847 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
3848 locations->SetOut(LocationFrom(r0, r1));
Scott Wakelingfe885462016-09-22 10:24:38 +01003849 break;
3850 }
3851
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003852 case DataType::Type::kFloat64: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003853 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3854 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0),
3855 calling_convention.GetFpuRegisterAt(1)));
3856 locations->SetOut(LocationFrom(r0, r1));
Scott Wakelingfe885462016-09-22 10:24:38 +01003857 break;
3858 }
3859
3860 default:
3861 LOG(FATAL) << "Unexpected type conversion from " << input_type
3862 << " to " << result_type;
3863 }
3864 break;
3865
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003866 case DataType::Type::kFloat32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003867 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003868 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003869 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003870 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003871 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003872 case DataType::Type::kInt16:
3873 case DataType::Type::kInt32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003874 locations->SetInAt(0, Location::RequiresRegister());
3875 locations->SetOut(Location::RequiresFpuRegister());
3876 break;
3877
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003878 case DataType::Type::kInt64: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003879 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3880 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0),
3881 calling_convention.GetRegisterAt(1)));
3882 locations->SetOut(LocationFrom(calling_convention.GetFpuRegisterAt(0)));
Scott Wakelingfe885462016-09-22 10:24:38 +01003883 break;
3884 }
3885
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003886 case DataType::Type::kFloat64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003887 locations->SetInAt(0, Location::RequiresFpuRegister());
3888 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3889 break;
3890
3891 default:
3892 LOG(FATAL) << "Unexpected type conversion from " << input_type
3893 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08003894 }
Scott Wakelingfe885462016-09-22 10:24:38 +01003895 break;
3896
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003897 case DataType::Type::kFloat64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003898 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003899 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003900 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003901 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003902 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003903 case DataType::Type::kInt16:
3904 case DataType::Type::kInt32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003905 locations->SetInAt(0, Location::RequiresRegister());
3906 locations->SetOut(Location::RequiresFpuRegister());
3907 break;
3908
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003909 case DataType::Type::kInt64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003910 locations->SetInAt(0, Location::RequiresRegister());
3911 locations->SetOut(Location::RequiresFpuRegister());
3912 locations->AddTemp(Location::RequiresFpuRegister());
3913 locations->AddTemp(Location::RequiresFpuRegister());
3914 break;
3915
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003916 case DataType::Type::kFloat32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003917 locations->SetInAt(0, Location::RequiresFpuRegister());
3918 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3919 break;
3920
3921 default:
3922 LOG(FATAL) << "Unexpected type conversion from " << input_type
3923 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08003924 }
Scott Wakelingfe885462016-09-22 10:24:38 +01003925 break;
3926
3927 default:
3928 LOG(FATAL) << "Unexpected type conversion from " << input_type
3929 << " to " << result_type;
3930 }
3931}
3932
3933void InstructionCodeGeneratorARMVIXL::VisitTypeConversion(HTypeConversion* conversion) {
3934 LocationSummary* locations = conversion->GetLocations();
3935 Location out = locations->Out();
3936 Location in = locations->InAt(0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003937 DataType::Type result_type = conversion->GetResultType();
3938 DataType::Type input_type = conversion->GetInputType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003939 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
3940 << input_type << " -> " << result_type;
Scott Wakelingfe885462016-09-22 10:24:38 +01003941 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003942 case DataType::Type::kUint8:
Scott Wakelingfe885462016-09-22 10:24:38 +01003943 switch (input_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003944 case DataType::Type::kInt8:
3945 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003946 case DataType::Type::kInt16:
3947 case DataType::Type::kInt32:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003948 __ Ubfx(OutputRegister(conversion), InputRegisterAt(conversion, 0), 0, 8);
3949 break;
3950 case DataType::Type::kInt64:
3951 __ Ubfx(OutputRegister(conversion), LowRegisterFrom(in), 0, 8);
3952 break;
3953
3954 default:
3955 LOG(FATAL) << "Unexpected type conversion from " << input_type
3956 << " to " << result_type;
3957 }
3958 break;
3959
3960 case DataType::Type::kInt8:
3961 switch (input_type) {
3962 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003963 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003964 case DataType::Type::kInt16:
3965 case DataType::Type::kInt32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003966 __ Sbfx(OutputRegister(conversion), InputRegisterAt(conversion, 0), 0, 8);
3967 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003968 case DataType::Type::kInt64:
3969 __ Sbfx(OutputRegister(conversion), LowRegisterFrom(in), 0, 8);
3970 break;
3971
3972 default:
3973 LOG(FATAL) << "Unexpected type conversion from " << input_type
3974 << " to " << result_type;
3975 }
3976 break;
3977
3978 case DataType::Type::kUint16:
3979 switch (input_type) {
3980 case DataType::Type::kInt8:
3981 case DataType::Type::kInt16:
3982 case DataType::Type::kInt32:
3983 __ Ubfx(OutputRegister(conversion), InputRegisterAt(conversion, 0), 0, 16);
3984 break;
3985 case DataType::Type::kInt64:
3986 __ Ubfx(OutputRegister(conversion), LowRegisterFrom(in), 0, 16);
3987 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01003988
3989 default:
3990 LOG(FATAL) << "Unexpected type conversion from " << input_type
3991 << " to " << result_type;
3992 }
3993 break;
3994
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003995 case DataType::Type::kInt16:
Scott Wakelingfe885462016-09-22 10:24:38 +01003996 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003997 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003998 case DataType::Type::kInt32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003999 __ Sbfx(OutputRegister(conversion), InputRegisterAt(conversion, 0), 0, 16);
4000 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004001 case DataType::Type::kInt64:
4002 __ Sbfx(OutputRegister(conversion), LowRegisterFrom(in), 0, 16);
4003 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01004004
4005 default:
4006 LOG(FATAL) << "Unexpected type conversion from " << input_type
4007 << " to " << result_type;
4008 }
4009 break;
4010
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004011 case DataType::Type::kInt32:
Scott Wakelingfe885462016-09-22 10:24:38 +01004012 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004013 case DataType::Type::kInt64:
Scott Wakelingfe885462016-09-22 10:24:38 +01004014 DCHECK(out.IsRegister());
4015 if (in.IsRegisterPair()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004016 __ Mov(OutputRegister(conversion), LowRegisterFrom(in));
Scott Wakelingfe885462016-09-22 10:24:38 +01004017 } else if (in.IsDoubleStackSlot()) {
4018 GetAssembler()->LoadFromOffset(kLoadWord,
4019 OutputRegister(conversion),
4020 sp,
4021 in.GetStackIndex());
4022 } else {
4023 DCHECK(in.IsConstant());
4024 DCHECK(in.GetConstant()->IsLongConstant());
Vladimir Markoba1a48e2017-04-13 11:50:14 +01004025 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
4026 __ Mov(OutputRegister(conversion), static_cast<int32_t>(value));
Scott Wakelingfe885462016-09-22 10:24:38 +01004027 }
4028 break;
4029
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004030 case DataType::Type::kFloat32: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004031 vixl32::SRegister temp = LowSRegisterFrom(locations->GetTemp(0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01004032 __ Vcvt(S32, F32, temp, InputSRegisterAt(conversion, 0));
Scott Wakelingfe885462016-09-22 10:24:38 +01004033 __ Vmov(OutputRegister(conversion), temp);
4034 break;
4035 }
4036
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004037 case DataType::Type::kFloat64: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004038 vixl32::SRegister temp_s = LowSRegisterFrom(locations->GetTemp(0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01004039 __ Vcvt(S32, F64, temp_s, DRegisterFrom(in));
Scott Wakelingfe885462016-09-22 10:24:38 +01004040 __ Vmov(OutputRegister(conversion), temp_s);
4041 break;
4042 }
4043
4044 default:
4045 LOG(FATAL) << "Unexpected type conversion from " << input_type
4046 << " to " << result_type;
4047 }
4048 break;
4049
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004050 case DataType::Type::kInt64:
Scott Wakelingfe885462016-09-22 10:24:38 +01004051 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004052 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004053 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004054 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004055 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004056 case DataType::Type::kInt16:
4057 case DataType::Type::kInt32:
Scott Wakelingfe885462016-09-22 10:24:38 +01004058 DCHECK(out.IsRegisterPair());
4059 DCHECK(in.IsRegister());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004060 __ Mov(LowRegisterFrom(out), InputRegisterAt(conversion, 0));
Scott Wakelingfe885462016-09-22 10:24:38 +01004061 // Sign extension.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004062 __ Asr(HighRegisterFrom(out), LowRegisterFrom(out), 31);
Scott Wakelingfe885462016-09-22 10:24:38 +01004063 break;
4064
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004065 case DataType::Type::kFloat32:
Scott Wakelingfe885462016-09-22 10:24:38 +01004066 codegen_->InvokeRuntime(kQuickF2l, conversion, conversion->GetDexPc());
4067 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
4068 break;
4069
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004070 case DataType::Type::kFloat64:
Scott Wakelingfe885462016-09-22 10:24:38 +01004071 codegen_->InvokeRuntime(kQuickD2l, conversion, conversion->GetDexPc());
4072 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
4073 break;
4074
4075 default:
4076 LOG(FATAL) << "Unexpected type conversion from " << input_type
4077 << " to " << result_type;
4078 }
4079 break;
4080
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004081 case DataType::Type::kFloat32:
Scott Wakelingfe885462016-09-22 10:24:38 +01004082 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004083 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004084 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004085 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004086 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004087 case DataType::Type::kInt16:
4088 case DataType::Type::kInt32:
Scott Wakelingfe885462016-09-22 10:24:38 +01004089 __ Vmov(OutputSRegister(conversion), InputRegisterAt(conversion, 0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01004090 __ Vcvt(F32, S32, OutputSRegister(conversion), OutputSRegister(conversion));
Scott Wakelingfe885462016-09-22 10:24:38 +01004091 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01004092
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004093 case DataType::Type::kInt64:
Scott Wakelingfe885462016-09-22 10:24:38 +01004094 codegen_->InvokeRuntime(kQuickL2f, conversion, conversion->GetDexPc());
4095 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
4096 break;
4097
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004098 case DataType::Type::kFloat64:
Scott Wakelingc34dba72016-10-03 10:14:44 +01004099 __ Vcvt(F32, F64, OutputSRegister(conversion), DRegisterFrom(in));
Scott Wakelingfe885462016-09-22 10:24:38 +01004100 break;
4101
4102 default:
4103 LOG(FATAL) << "Unexpected type conversion from " << input_type
4104 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08004105 }
Scott Wakelingfe885462016-09-22 10:24:38 +01004106 break;
4107
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004108 case DataType::Type::kFloat64:
Scott Wakelingfe885462016-09-22 10:24:38 +01004109 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004110 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004111 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004112 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004113 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004114 case DataType::Type::kInt16:
4115 case DataType::Type::kInt32:
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004116 __ Vmov(LowSRegisterFrom(out), InputRegisterAt(conversion, 0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01004117 __ Vcvt(F64, S32, DRegisterFrom(out), LowSRegisterFrom(out));
Scott Wakelingfe885462016-09-22 10:24:38 +01004118 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01004119
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004120 case DataType::Type::kInt64: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004121 vixl32::Register low = LowRegisterFrom(in);
4122 vixl32::Register high = HighRegisterFrom(in);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004123 vixl32::SRegister out_s = LowSRegisterFrom(out);
Scott Wakelingc34dba72016-10-03 10:14:44 +01004124 vixl32::DRegister out_d = DRegisterFrom(out);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004125 vixl32::SRegister temp_s = LowSRegisterFrom(locations->GetTemp(0));
Scott Wakelingc34dba72016-10-03 10:14:44 +01004126 vixl32::DRegister temp_d = DRegisterFrom(locations->GetTemp(0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01004127 vixl32::DRegister constant_d = DRegisterFrom(locations->GetTemp(1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004128
4129 // temp_d = int-to-double(high)
4130 __ Vmov(temp_s, high);
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01004131 __ Vcvt(F64, S32, temp_d, temp_s);
Scott Wakelingfe885462016-09-22 10:24:38 +01004132 // constant_d = k2Pow32EncodingForDouble
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004133 __ Vmov(constant_d, bit_cast<double, int64_t>(k2Pow32EncodingForDouble));
Scott Wakelingfe885462016-09-22 10:24:38 +01004134 // out_d = unsigned-to-double(low)
4135 __ Vmov(out_s, low);
4136 __ Vcvt(F64, U32, out_d, out_s);
4137 // out_d += temp_d * constant_d
4138 __ Vmla(F64, out_d, temp_d, constant_d);
4139 break;
4140 }
4141
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004142 case DataType::Type::kFloat32:
Scott Wakelingc34dba72016-10-03 10:14:44 +01004143 __ Vcvt(F64, F32, DRegisterFrom(out), InputSRegisterAt(conversion, 0));
Scott Wakelingfe885462016-09-22 10:24:38 +01004144 break;
4145
4146 default:
4147 LOG(FATAL) << "Unexpected type conversion from " << input_type
4148 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08004149 }
Scott Wakelingfe885462016-09-22 10:24:38 +01004150 break;
4151
4152 default:
4153 LOG(FATAL) << "Unexpected type conversion from " << input_type
4154 << " to " << result_type;
4155 }
4156}
4157
4158void LocationsBuilderARMVIXL::VisitAdd(HAdd* add) {
4159 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004160 new (GetGraph()->GetAllocator()) LocationSummary(add, LocationSummary::kNoCall);
Scott Wakelingfe885462016-09-22 10:24:38 +01004161 switch (add->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004162 case DataType::Type::kInt32: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004163 locations->SetInAt(0, Location::RequiresRegister());
4164 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
4165 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4166 break;
4167 }
4168
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004169 case DataType::Type::kInt64: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004170 locations->SetInAt(0, Location::RequiresRegister());
Anton Kirilovdda43962016-11-21 19:55:20 +00004171 locations->SetInAt(1, ArmEncodableConstantOrRegister(add->InputAt(1), ADD));
Scott Wakelingfe885462016-09-22 10:24:38 +01004172 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4173 break;
4174 }
4175
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004176 case DataType::Type::kFloat32:
4177 case DataType::Type::kFloat64: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004178 locations->SetInAt(0, Location::RequiresFpuRegister());
4179 locations->SetInAt(1, Location::RequiresFpuRegister());
4180 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4181 break;
4182 }
4183
4184 default:
4185 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
4186 }
4187}
4188
4189void InstructionCodeGeneratorARMVIXL::VisitAdd(HAdd* add) {
4190 LocationSummary* locations = add->GetLocations();
4191 Location out = locations->Out();
4192 Location first = locations->InAt(0);
4193 Location second = locations->InAt(1);
4194
4195 switch (add->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004196 case DataType::Type::kInt32: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004197 __ Add(OutputRegister(add), InputRegisterAt(add, 0), InputOperandAt(add, 1));
4198 }
4199 break;
4200
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004201 case DataType::Type::kInt64: {
Anton Kirilovdda43962016-11-21 19:55:20 +00004202 if (second.IsConstant()) {
4203 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
4204 GenerateAddLongConst(out, first, value);
4205 } else {
4206 DCHECK(second.IsRegisterPair());
4207 __ Adds(LowRegisterFrom(out), LowRegisterFrom(first), LowRegisterFrom(second));
4208 __ Adc(HighRegisterFrom(out), HighRegisterFrom(first), HighRegisterFrom(second));
4209 }
Scott Wakelingfe885462016-09-22 10:24:38 +01004210 break;
4211 }
4212
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004213 case DataType::Type::kFloat32:
4214 case DataType::Type::kFloat64:
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004215 __ Vadd(OutputVRegister(add), InputVRegisterAt(add, 0), InputVRegisterAt(add, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004216 break;
4217
4218 default:
4219 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
4220 }
4221}
4222
4223void LocationsBuilderARMVIXL::VisitSub(HSub* sub) {
4224 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004225 new (GetGraph()->GetAllocator()) LocationSummary(sub, LocationSummary::kNoCall);
Scott Wakelingfe885462016-09-22 10:24:38 +01004226 switch (sub->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004227 case DataType::Type::kInt32: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004228 locations->SetInAt(0, Location::RequiresRegister());
4229 locations->SetInAt(1, Location::RegisterOrConstant(sub->InputAt(1)));
4230 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4231 break;
4232 }
4233
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004234 case DataType::Type::kInt64: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004235 locations->SetInAt(0, Location::RequiresRegister());
Anton Kirilovdda43962016-11-21 19:55:20 +00004236 locations->SetInAt(1, ArmEncodableConstantOrRegister(sub->InputAt(1), SUB));
Scott Wakelingfe885462016-09-22 10:24:38 +01004237 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4238 break;
4239 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004240 case DataType::Type::kFloat32:
4241 case DataType::Type::kFloat64: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004242 locations->SetInAt(0, Location::RequiresFpuRegister());
4243 locations->SetInAt(1, Location::RequiresFpuRegister());
4244 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4245 break;
4246 }
4247 default:
4248 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
4249 }
4250}
4251
4252void InstructionCodeGeneratorARMVIXL::VisitSub(HSub* sub) {
4253 LocationSummary* locations = sub->GetLocations();
4254 Location out = locations->Out();
4255 Location first = locations->InAt(0);
4256 Location second = locations->InAt(1);
4257 switch (sub->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004258 case DataType::Type::kInt32: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004259 __ Sub(OutputRegister(sub), InputRegisterAt(sub, 0), InputOperandAt(sub, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004260 break;
4261 }
4262
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004263 case DataType::Type::kInt64: {
Anton Kirilovdda43962016-11-21 19:55:20 +00004264 if (second.IsConstant()) {
4265 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
4266 GenerateAddLongConst(out, first, -value);
4267 } else {
4268 DCHECK(second.IsRegisterPair());
4269 __ Subs(LowRegisterFrom(out), LowRegisterFrom(first), LowRegisterFrom(second));
4270 __ Sbc(HighRegisterFrom(out), HighRegisterFrom(first), HighRegisterFrom(second));
4271 }
Scott Wakelingfe885462016-09-22 10:24:38 +01004272 break;
4273 }
4274
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004275 case DataType::Type::kFloat32:
4276 case DataType::Type::kFloat64:
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004277 __ Vsub(OutputVRegister(sub), InputVRegisterAt(sub, 0), InputVRegisterAt(sub, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004278 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01004279
4280 default:
4281 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
4282 }
4283}
4284
4285void LocationsBuilderARMVIXL::VisitMul(HMul* mul) {
4286 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004287 new (GetGraph()->GetAllocator()) LocationSummary(mul, LocationSummary::kNoCall);
Scott Wakelingfe885462016-09-22 10:24:38 +01004288 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004289 case DataType::Type::kInt32:
4290 case DataType::Type::kInt64: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004291 locations->SetInAt(0, Location::RequiresRegister());
4292 locations->SetInAt(1, Location::RequiresRegister());
4293 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4294 break;
4295 }
4296
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004297 case DataType::Type::kFloat32:
4298 case DataType::Type::kFloat64: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004299 locations->SetInAt(0, Location::RequiresFpuRegister());
4300 locations->SetInAt(1, Location::RequiresFpuRegister());
4301 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4302 break;
4303 }
4304
4305 default:
4306 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4307 }
4308}
4309
4310void InstructionCodeGeneratorARMVIXL::VisitMul(HMul* mul) {
4311 LocationSummary* locations = mul->GetLocations();
4312 Location out = locations->Out();
4313 Location first = locations->InAt(0);
4314 Location second = locations->InAt(1);
4315 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004316 case DataType::Type::kInt32: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004317 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
4318 break;
4319 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004320 case DataType::Type::kInt64: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004321 vixl32::Register out_hi = HighRegisterFrom(out);
4322 vixl32::Register out_lo = LowRegisterFrom(out);
4323 vixl32::Register in1_hi = HighRegisterFrom(first);
4324 vixl32::Register in1_lo = LowRegisterFrom(first);
4325 vixl32::Register in2_hi = HighRegisterFrom(second);
4326 vixl32::Register in2_lo = LowRegisterFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01004327
4328 // Extra checks to protect caused by the existence of R1_R2.
4329 // The algorithm is wrong if out.hi is either in1.lo or in2.lo:
4330 // (e.g. in1=r0_r1, in2=r2_r3 and out=r1_r2);
Anton Kirilov644032c2016-12-06 17:51:43 +00004331 DCHECK(!out_hi.Is(in1_lo));
4332 DCHECK(!out_hi.Is(in2_lo));
Scott Wakelingfe885462016-09-22 10:24:38 +01004333
4334 // input: in1 - 64 bits, in2 - 64 bits
4335 // output: out
4336 // formula: out.hi : out.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
4337 // parts: out.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
4338 // parts: out.lo = (in1.lo * in2.lo)[31:0]
4339
4340 UseScratchRegisterScope temps(GetVIXLAssembler());
4341 vixl32::Register temp = temps.Acquire();
4342 // temp <- in1.lo * in2.hi
4343 __ Mul(temp, in1_lo, in2_hi);
4344 // out.hi <- in1.lo * in2.hi + in1.hi * in2.lo
4345 __ Mla(out_hi, in1_hi, in2_lo, temp);
4346 // out.lo <- (in1.lo * in2.lo)[31:0];
4347 __ Umull(out_lo, temp, in1_lo, in2_lo);
4348 // out.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004349 __ Add(out_hi, out_hi, temp);
Scott Wakelingfe885462016-09-22 10:24:38 +01004350 break;
4351 }
4352
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004353 case DataType::Type::kFloat32:
4354 case DataType::Type::kFloat64:
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004355 __ Vmul(OutputVRegister(mul), InputVRegisterAt(mul, 0), InputVRegisterAt(mul, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004356 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01004357
4358 default:
4359 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4360 }
4361}
4362
Scott Wakelingfe885462016-09-22 10:24:38 +01004363void InstructionCodeGeneratorARMVIXL::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
4364 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004365 DCHECK(instruction->GetResultType() == DataType::Type::kInt32);
Scott Wakelingfe885462016-09-22 10:24:38 +01004366
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004367 Location second = instruction->GetLocations()->InAt(1);
Scott Wakelingfe885462016-09-22 10:24:38 +01004368 DCHECK(second.IsConstant());
4369
4370 vixl32::Register out = OutputRegister(instruction);
4371 vixl32::Register dividend = InputRegisterAt(instruction, 0);
Anton Kirilov644032c2016-12-06 17:51:43 +00004372 int32_t imm = Int32ConstantFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01004373 DCHECK(imm == 1 || imm == -1);
4374
4375 if (instruction->IsRem()) {
4376 __ Mov(out, 0);
4377 } else {
4378 if (imm == 1) {
4379 __ Mov(out, dividend);
4380 } else {
4381 __ Rsb(out, dividend, 0);
4382 }
4383 }
4384}
4385
4386void InstructionCodeGeneratorARMVIXL::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
4387 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004388 DCHECK(instruction->GetResultType() == DataType::Type::kInt32);
Scott Wakelingfe885462016-09-22 10:24:38 +01004389
4390 LocationSummary* locations = instruction->GetLocations();
4391 Location second = locations->InAt(1);
4392 DCHECK(second.IsConstant());
4393
4394 vixl32::Register out = OutputRegister(instruction);
4395 vixl32::Register dividend = InputRegisterAt(instruction, 0);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004396 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
Anton Kirilov644032c2016-12-06 17:51:43 +00004397 int32_t imm = Int32ConstantFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01004398 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
4399 int ctz_imm = CTZ(abs_imm);
4400
4401 if (ctz_imm == 1) {
4402 __ Lsr(temp, dividend, 32 - ctz_imm);
4403 } else {
4404 __ Asr(temp, dividend, 31);
4405 __ Lsr(temp, temp, 32 - ctz_imm);
4406 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004407 __ Add(out, temp, dividend);
Scott Wakelingfe885462016-09-22 10:24:38 +01004408
4409 if (instruction->IsDiv()) {
4410 __ Asr(out, out, ctz_imm);
4411 if (imm < 0) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004412 __ Rsb(out, out, 0);
Scott Wakelingfe885462016-09-22 10:24:38 +01004413 }
4414 } else {
4415 __ Ubfx(out, out, 0, ctz_imm);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004416 __ Sub(out, out, temp);
Scott Wakelingfe885462016-09-22 10:24:38 +01004417 }
4418}
4419
4420void InstructionCodeGeneratorARMVIXL::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
4421 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004422 DCHECK(instruction->GetResultType() == DataType::Type::kInt32);
Scott Wakelingfe885462016-09-22 10:24:38 +01004423
4424 LocationSummary* locations = instruction->GetLocations();
4425 Location second = locations->InAt(1);
4426 DCHECK(second.IsConstant());
4427
4428 vixl32::Register out = OutputRegister(instruction);
4429 vixl32::Register dividend = InputRegisterAt(instruction, 0);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004430 vixl32::Register temp1 = RegisterFrom(locations->GetTemp(0));
4431 vixl32::Register temp2 = RegisterFrom(locations->GetTemp(1));
Scott Wakelingb77051e2016-11-21 19:46:00 +00004432 int32_t imm = Int32ConstantFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01004433
4434 int64_t magic;
4435 int shift;
4436 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
4437
Anton Kirilovdda43962016-11-21 19:55:20 +00004438 // TODO(VIXL): Change the static cast to Operand::From() after VIXL is fixed.
4439 __ Mov(temp1, static_cast<int32_t>(magic));
Scott Wakelingfe885462016-09-22 10:24:38 +01004440 __ Smull(temp2, temp1, dividend, temp1);
4441
4442 if (imm > 0 && magic < 0) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004443 __ Add(temp1, temp1, dividend);
Scott Wakelingfe885462016-09-22 10:24:38 +01004444 } else if (imm < 0 && magic > 0) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004445 __ Sub(temp1, temp1, dividend);
Scott Wakelingfe885462016-09-22 10:24:38 +01004446 }
4447
4448 if (shift != 0) {
4449 __ Asr(temp1, temp1, shift);
4450 }
4451
4452 if (instruction->IsDiv()) {
4453 __ Sub(out, temp1, Operand(temp1, vixl32::Shift(ASR), 31));
4454 } else {
4455 __ Sub(temp1, temp1, Operand(temp1, vixl32::Shift(ASR), 31));
4456 // TODO: Strength reduction for mls.
4457 __ Mov(temp2, imm);
4458 __ Mls(out, temp1, temp2, dividend);
4459 }
4460}
4461
4462void InstructionCodeGeneratorARMVIXL::GenerateDivRemConstantIntegral(
4463 HBinaryOperation* instruction) {
4464 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004465 DCHECK(instruction->GetResultType() == DataType::Type::kInt32);
Scott Wakelingfe885462016-09-22 10:24:38 +01004466
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004467 Location second = instruction->GetLocations()->InAt(1);
Scott Wakelingfe885462016-09-22 10:24:38 +01004468 DCHECK(second.IsConstant());
4469
Anton Kirilov644032c2016-12-06 17:51:43 +00004470 int32_t imm = Int32ConstantFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01004471 if (imm == 0) {
4472 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
4473 } else if (imm == 1 || imm == -1) {
4474 DivRemOneOrMinusOne(instruction);
4475 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
4476 DivRemByPowerOfTwo(instruction);
4477 } else {
4478 DCHECK(imm <= -2 || imm >= 2);
4479 GenerateDivRemWithAnyConstant(instruction);
4480 }
4481}
4482
4483void LocationsBuilderARMVIXL::VisitDiv(HDiv* div) {
4484 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004485 if (div->GetResultType() == DataType::Type::kInt64) {
Scott Wakelingfe885462016-09-22 10:24:38 +01004486 // pLdiv runtime call.
4487 call_kind = LocationSummary::kCallOnMainOnly;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004488 } else if (div->GetResultType() == DataType::Type::kInt32 && div->InputAt(1)->IsConstant()) {
Scott Wakelingfe885462016-09-22 10:24:38 +01004489 // sdiv will be replaced by other instruction sequence.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004490 } else if (div->GetResultType() == DataType::Type::kInt32 &&
Scott Wakelingfe885462016-09-22 10:24:38 +01004491 !codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4492 // pIdivmod runtime call.
4493 call_kind = LocationSummary::kCallOnMainOnly;
4494 }
4495
Vladimir Markoca6fff82017-10-03 14:49:14 +01004496 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(div, call_kind);
Scott Wakelingfe885462016-09-22 10:24:38 +01004497
4498 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004499 case DataType::Type::kInt32: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004500 if (div->InputAt(1)->IsConstant()) {
4501 locations->SetInAt(0, Location::RequiresRegister());
4502 locations->SetInAt(1, Location::ConstantLocation(div->InputAt(1)->AsConstant()));
4503 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Anton Kirilov644032c2016-12-06 17:51:43 +00004504 int32_t value = Int32ConstantFrom(div->InputAt(1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004505 if (value == 1 || value == 0 || value == -1) {
4506 // No temp register required.
4507 } else {
4508 locations->AddTemp(Location::RequiresRegister());
4509 if (!IsPowerOfTwo(AbsOrMin(value))) {
4510 locations->AddTemp(Location::RequiresRegister());
4511 }
4512 }
4513 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4514 locations->SetInAt(0, Location::RequiresRegister());
4515 locations->SetInAt(1, Location::RequiresRegister());
4516 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4517 } else {
Artem Serov551b28f2016-10-18 19:11:30 +01004518 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4519 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4520 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
Roland Levillain5e8d5f02016-10-18 18:03:43 +01004521 // Note: divmod will compute both the quotient and the remainder as the pair R0 and R1, but
Artem Serov551b28f2016-10-18 19:11:30 +01004522 // we only need the former.
4523 locations->SetOut(LocationFrom(r0));
Scott Wakelingfe885462016-09-22 10:24:38 +01004524 }
4525 break;
4526 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004527 case DataType::Type::kInt64: {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01004528 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4529 locations->SetInAt(0, LocationFrom(
4530 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
4531 locations->SetInAt(1, LocationFrom(
4532 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
4533 locations->SetOut(LocationFrom(r0, r1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004534 break;
4535 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004536 case DataType::Type::kFloat32:
4537 case DataType::Type::kFloat64: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004538 locations->SetInAt(0, Location::RequiresFpuRegister());
4539 locations->SetInAt(1, Location::RequiresFpuRegister());
4540 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4541 break;
4542 }
4543
4544 default:
4545 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
4546 }
4547}
4548
4549void InstructionCodeGeneratorARMVIXL::VisitDiv(HDiv* div) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01004550 Location lhs = div->GetLocations()->InAt(0);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004551 Location rhs = div->GetLocations()->InAt(1);
Scott Wakelingfe885462016-09-22 10:24:38 +01004552
4553 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004554 case DataType::Type::kInt32: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004555 if (rhs.IsConstant()) {
Scott Wakelingfe885462016-09-22 10:24:38 +01004556 GenerateDivRemConstantIntegral(div);
4557 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4558 __ Sdiv(OutputRegister(div), InputRegisterAt(div, 0), InputRegisterAt(div, 1));
4559 } else {
Artem Serov551b28f2016-10-18 19:11:30 +01004560 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4561 DCHECK(calling_convention.GetRegisterAt(0).Is(RegisterFrom(lhs)));
4562 DCHECK(calling_convention.GetRegisterAt(1).Is(RegisterFrom(rhs)));
4563 DCHECK(r0.Is(OutputRegister(div)));
4564
4565 codegen_->InvokeRuntime(kQuickIdivmod, div, div->GetDexPc());
4566 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
Scott Wakelingfe885462016-09-22 10:24:38 +01004567 }
4568 break;
4569 }
4570
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004571 case DataType::Type::kInt64: {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01004572 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4573 DCHECK(calling_convention.GetRegisterAt(0).Is(LowRegisterFrom(lhs)));
4574 DCHECK(calling_convention.GetRegisterAt(1).Is(HighRegisterFrom(lhs)));
4575 DCHECK(calling_convention.GetRegisterAt(2).Is(LowRegisterFrom(rhs)));
4576 DCHECK(calling_convention.GetRegisterAt(3).Is(HighRegisterFrom(rhs)));
4577 DCHECK(LowRegisterFrom(div->GetLocations()->Out()).Is(r0));
4578 DCHECK(HighRegisterFrom(div->GetLocations()->Out()).Is(r1));
4579
4580 codegen_->InvokeRuntime(kQuickLdiv, div, div->GetDexPc());
4581 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Scott Wakelingfe885462016-09-22 10:24:38 +01004582 break;
4583 }
4584
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004585 case DataType::Type::kFloat32:
4586 case DataType::Type::kFloat64:
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004587 __ Vdiv(OutputVRegister(div), InputVRegisterAt(div, 0), InputVRegisterAt(div, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004588 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01004589
4590 default:
4591 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
4592 }
4593}
4594
Artem Serov551b28f2016-10-18 19:11:30 +01004595void LocationsBuilderARMVIXL::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004596 DataType::Type type = rem->GetResultType();
Artem Serov551b28f2016-10-18 19:11:30 +01004597
4598 // Most remainders are implemented in the runtime.
4599 LocationSummary::CallKind call_kind = LocationSummary::kCallOnMainOnly;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004600 if (rem->GetResultType() == DataType::Type::kInt32 && rem->InputAt(1)->IsConstant()) {
Artem Serov551b28f2016-10-18 19:11:30 +01004601 // sdiv will be replaced by other instruction sequence.
4602 call_kind = LocationSummary::kNoCall;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004603 } else if ((rem->GetResultType() == DataType::Type::kInt32)
Artem Serov551b28f2016-10-18 19:11:30 +01004604 && codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4605 // Have hardware divide instruction for int, do it with three instructions.
4606 call_kind = LocationSummary::kNoCall;
4607 }
4608
Vladimir Markoca6fff82017-10-03 14:49:14 +01004609 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(rem, call_kind);
Artem Serov551b28f2016-10-18 19:11:30 +01004610
4611 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004612 case DataType::Type::kInt32: {
Artem Serov551b28f2016-10-18 19:11:30 +01004613 if (rem->InputAt(1)->IsConstant()) {
4614 locations->SetInAt(0, Location::RequiresRegister());
4615 locations->SetInAt(1, Location::ConstantLocation(rem->InputAt(1)->AsConstant()));
4616 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Anton Kirilov644032c2016-12-06 17:51:43 +00004617 int32_t value = Int32ConstantFrom(rem->InputAt(1));
Artem Serov551b28f2016-10-18 19:11:30 +01004618 if (value == 1 || value == 0 || value == -1) {
4619 // No temp register required.
4620 } else {
4621 locations->AddTemp(Location::RequiresRegister());
4622 if (!IsPowerOfTwo(AbsOrMin(value))) {
4623 locations->AddTemp(Location::RequiresRegister());
4624 }
4625 }
4626 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4627 locations->SetInAt(0, Location::RequiresRegister());
4628 locations->SetInAt(1, Location::RequiresRegister());
4629 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4630 locations->AddTemp(Location::RequiresRegister());
4631 } else {
4632 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4633 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4634 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
Roland Levillain5e8d5f02016-10-18 18:03:43 +01004635 // Note: divmod will compute both the quotient and the remainder as the pair R0 and R1, but
Artem Serov551b28f2016-10-18 19:11:30 +01004636 // we only need the latter.
4637 locations->SetOut(LocationFrom(r1));
4638 }
4639 break;
4640 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004641 case DataType::Type::kInt64: {
Artem Serov551b28f2016-10-18 19:11:30 +01004642 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4643 locations->SetInAt(0, LocationFrom(
4644 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
4645 locations->SetInAt(1, LocationFrom(
4646 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
4647 // The runtime helper puts the output in R2,R3.
4648 locations->SetOut(LocationFrom(r2, r3));
4649 break;
4650 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004651 case DataType::Type::kFloat32: {
Artem Serov551b28f2016-10-18 19:11:30 +01004652 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4653 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
4654 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
4655 locations->SetOut(LocationFrom(s0));
4656 break;
4657 }
4658
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004659 case DataType::Type::kFloat64: {
Artem Serov551b28f2016-10-18 19:11:30 +01004660 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4661 locations->SetInAt(0, LocationFrom(
4662 calling_convention.GetFpuRegisterAt(0), calling_convention.GetFpuRegisterAt(1)));
4663 locations->SetInAt(1, LocationFrom(
4664 calling_convention.GetFpuRegisterAt(2), calling_convention.GetFpuRegisterAt(3)));
4665 locations->SetOut(LocationFrom(s0, s1));
4666 break;
4667 }
4668
4669 default:
4670 LOG(FATAL) << "Unexpected rem type " << type;
4671 }
4672}
4673
4674void InstructionCodeGeneratorARMVIXL::VisitRem(HRem* rem) {
4675 LocationSummary* locations = rem->GetLocations();
4676 Location second = locations->InAt(1);
4677
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004678 DataType::Type type = rem->GetResultType();
Artem Serov551b28f2016-10-18 19:11:30 +01004679 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004680 case DataType::Type::kInt32: {
Artem Serov551b28f2016-10-18 19:11:30 +01004681 vixl32::Register reg1 = InputRegisterAt(rem, 0);
4682 vixl32::Register out_reg = OutputRegister(rem);
4683 if (second.IsConstant()) {
4684 GenerateDivRemConstantIntegral(rem);
4685 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4686 vixl32::Register reg2 = RegisterFrom(second);
4687 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
4688
4689 // temp = reg1 / reg2 (integer division)
4690 // dest = reg1 - temp * reg2
4691 __ Sdiv(temp, reg1, reg2);
4692 __ Mls(out_reg, temp, reg2, reg1);
4693 } else {
4694 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4695 DCHECK(reg1.Is(calling_convention.GetRegisterAt(0)));
4696 DCHECK(RegisterFrom(second).Is(calling_convention.GetRegisterAt(1)));
4697 DCHECK(out_reg.Is(r1));
4698
4699 codegen_->InvokeRuntime(kQuickIdivmod, rem, rem->GetDexPc());
4700 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
4701 }
4702 break;
4703 }
4704
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004705 case DataType::Type::kInt64: {
Artem Serov551b28f2016-10-18 19:11:30 +01004706 codegen_->InvokeRuntime(kQuickLmod, rem, rem->GetDexPc());
4707 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
4708 break;
4709 }
4710
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004711 case DataType::Type::kFloat32: {
Artem Serov551b28f2016-10-18 19:11:30 +01004712 codegen_->InvokeRuntime(kQuickFmodf, rem, rem->GetDexPc());
4713 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
4714 break;
4715 }
4716
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004717 case DataType::Type::kFloat64: {
Artem Serov551b28f2016-10-18 19:11:30 +01004718 codegen_->InvokeRuntime(kQuickFmod, rem, rem->GetDexPc());
4719 CheckEntrypointTypes<kQuickFmod, double, double, double>();
4720 break;
4721 }
4722
4723 default:
4724 LOG(FATAL) << "Unexpected rem type " << type;
4725 }
4726}
4727
4728
Scott Wakelingfe885462016-09-22 10:24:38 +01004729void LocationsBuilderARMVIXL::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Artem Serov657022c2016-11-23 14:19:38 +00004730 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Scott Wakelingfe885462016-09-22 10:24:38 +01004731 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Scott Wakelingfe885462016-09-22 10:24:38 +01004732}
4733
4734void InstructionCodeGeneratorARMVIXL::VisitDivZeroCheck(HDivZeroCheck* instruction) {
4735 DivZeroCheckSlowPathARMVIXL* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01004736 new (codegen_->GetScopedAllocator()) DivZeroCheckSlowPathARMVIXL(instruction);
Scott Wakelingfe885462016-09-22 10:24:38 +01004737 codegen_->AddSlowPath(slow_path);
4738
4739 LocationSummary* locations = instruction->GetLocations();
4740 Location value = locations->InAt(0);
4741
4742 switch (instruction->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004743 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004744 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004745 case DataType::Type::kInt8:
4746 case DataType::Type::kUint16:
4747 case DataType::Type::kInt16:
4748 case DataType::Type::kInt32: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004749 if (value.IsRegister()) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00004750 __ CompareAndBranchIfZero(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
Scott Wakelingfe885462016-09-22 10:24:38 +01004751 } else {
4752 DCHECK(value.IsConstant()) << value;
Anton Kirilov644032c2016-12-06 17:51:43 +00004753 if (Int32ConstantFrom(value) == 0) {
Scott Wakelingfe885462016-09-22 10:24:38 +01004754 __ B(slow_path->GetEntryLabel());
4755 }
4756 }
4757 break;
4758 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004759 case DataType::Type::kInt64: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004760 if (value.IsRegisterPair()) {
4761 UseScratchRegisterScope temps(GetVIXLAssembler());
4762 vixl32::Register temp = temps.Acquire();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004763 __ Orrs(temp, LowRegisterFrom(value), HighRegisterFrom(value));
Scott Wakelingfe885462016-09-22 10:24:38 +01004764 __ B(eq, slow_path->GetEntryLabel());
4765 } else {
4766 DCHECK(value.IsConstant()) << value;
Anton Kirilov644032c2016-12-06 17:51:43 +00004767 if (Int64ConstantFrom(value) == 0) {
Scott Wakelingfe885462016-09-22 10:24:38 +01004768 __ B(slow_path->GetEntryLabel());
4769 }
4770 }
4771 break;
4772 }
4773 default:
4774 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
4775 }
4776}
4777
Artem Serov02109dd2016-09-23 17:17:54 +01004778void InstructionCodeGeneratorARMVIXL::HandleIntegerRotate(HRor* ror) {
4779 LocationSummary* locations = ror->GetLocations();
4780 vixl32::Register in = InputRegisterAt(ror, 0);
4781 Location rhs = locations->InAt(1);
4782 vixl32::Register out = OutputRegister(ror);
4783
4784 if (rhs.IsConstant()) {
4785 // Arm32 and Thumb2 assemblers require a rotation on the interval [1,31],
4786 // so map all rotations to a +ve. equivalent in that range.
4787 // (e.g. left *or* right by -2 bits == 30 bits in the same direction.)
4788 uint32_t rot = CodeGenerator::GetInt32ValueOf(rhs.GetConstant()) & 0x1F;
4789 if (rot) {
4790 // Rotate, mapping left rotations to right equivalents if necessary.
4791 // (e.g. left by 2 bits == right by 30.)
4792 __ Ror(out, in, rot);
4793 } else if (!out.Is(in)) {
4794 __ Mov(out, in);
4795 }
4796 } else {
4797 __ Ror(out, in, RegisterFrom(rhs));
4798 }
4799}
4800
4801// Gain some speed by mapping all Long rotates onto equivalent pairs of Integer
4802// rotates by swapping input regs (effectively rotating by the first 32-bits of
4803// a larger rotation) or flipping direction (thus treating larger right/left
4804// rotations as sub-word sized rotations in the other direction) as appropriate.
4805void InstructionCodeGeneratorARMVIXL::HandleLongRotate(HRor* ror) {
4806 LocationSummary* locations = ror->GetLocations();
4807 vixl32::Register in_reg_lo = LowRegisterFrom(locations->InAt(0));
4808 vixl32::Register in_reg_hi = HighRegisterFrom(locations->InAt(0));
4809 Location rhs = locations->InAt(1);
4810 vixl32::Register out_reg_lo = LowRegisterFrom(locations->Out());
4811 vixl32::Register out_reg_hi = HighRegisterFrom(locations->Out());
4812
4813 if (rhs.IsConstant()) {
4814 uint64_t rot = CodeGenerator::GetInt64ValueOf(rhs.GetConstant());
4815 // Map all rotations to +ve. equivalents on the interval [0,63].
4816 rot &= kMaxLongShiftDistance;
4817 // For rotates over a word in size, 'pre-rotate' by 32-bits to keep rotate
4818 // logic below to a simple pair of binary orr.
4819 // (e.g. 34 bits == in_reg swap + 2 bits right.)
4820 if (rot >= kArmBitsPerWord) {
4821 rot -= kArmBitsPerWord;
4822 std::swap(in_reg_hi, in_reg_lo);
4823 }
4824 // Rotate, or mov to out for zero or word size rotations.
4825 if (rot != 0u) {
Scott Wakelingb77051e2016-11-21 19:46:00 +00004826 __ Lsr(out_reg_hi, in_reg_hi, Operand::From(rot));
Artem Serov02109dd2016-09-23 17:17:54 +01004827 __ Orr(out_reg_hi, out_reg_hi, Operand(in_reg_lo, ShiftType::LSL, kArmBitsPerWord - rot));
Scott Wakelingb77051e2016-11-21 19:46:00 +00004828 __ Lsr(out_reg_lo, in_reg_lo, Operand::From(rot));
Artem Serov02109dd2016-09-23 17:17:54 +01004829 __ Orr(out_reg_lo, out_reg_lo, Operand(in_reg_hi, ShiftType::LSL, kArmBitsPerWord - rot));
4830 } else {
4831 __ Mov(out_reg_lo, in_reg_lo);
4832 __ Mov(out_reg_hi, in_reg_hi);
4833 }
4834 } else {
4835 vixl32::Register shift_right = RegisterFrom(locations->GetTemp(0));
4836 vixl32::Register shift_left = RegisterFrom(locations->GetTemp(1));
4837 vixl32::Label end;
4838 vixl32::Label shift_by_32_plus_shift_right;
Anton Kirilov6f644202017-02-27 18:29:45 +00004839 vixl32::Label* final_label = codegen_->GetFinalLabel(ror, &end);
Artem Serov02109dd2016-09-23 17:17:54 +01004840
4841 __ And(shift_right, RegisterFrom(rhs), 0x1F);
4842 __ Lsrs(shift_left, RegisterFrom(rhs), 6);
Scott Wakelingbffdc702016-12-07 17:46:03 +00004843 __ Rsb(LeaveFlags, shift_left, shift_right, Operand::From(kArmBitsPerWord));
Artem Serov517d9f62016-12-12 15:51:15 +00004844 __ B(cc, &shift_by_32_plus_shift_right, /* far_target */ false);
Artem Serov02109dd2016-09-23 17:17:54 +01004845
4846 // out_reg_hi = (reg_hi << shift_left) | (reg_lo >> shift_right).
4847 // out_reg_lo = (reg_lo << shift_left) | (reg_hi >> shift_right).
4848 __ Lsl(out_reg_hi, in_reg_hi, shift_left);
4849 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
4850 __ Add(out_reg_hi, out_reg_hi, out_reg_lo);
4851 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
4852 __ Lsr(shift_left, in_reg_hi, shift_right);
4853 __ Add(out_reg_lo, out_reg_lo, shift_left);
Anton Kirilov6f644202017-02-27 18:29:45 +00004854 __ B(final_label);
Artem Serov02109dd2016-09-23 17:17:54 +01004855
4856 __ Bind(&shift_by_32_plus_shift_right); // Shift by 32+shift_right.
4857 // out_reg_hi = (reg_hi >> shift_right) | (reg_lo << shift_left).
4858 // out_reg_lo = (reg_lo >> shift_right) | (reg_hi << shift_left).
4859 __ Lsr(out_reg_hi, in_reg_hi, shift_right);
4860 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
4861 __ Add(out_reg_hi, out_reg_hi, out_reg_lo);
4862 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
4863 __ Lsl(shift_right, in_reg_hi, shift_left);
4864 __ Add(out_reg_lo, out_reg_lo, shift_right);
4865
Anton Kirilov6f644202017-02-27 18:29:45 +00004866 if (end.IsReferenced()) {
4867 __ Bind(&end);
4868 }
Artem Serov02109dd2016-09-23 17:17:54 +01004869 }
4870}
4871
4872void LocationsBuilderARMVIXL::VisitRor(HRor* ror) {
4873 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004874 new (GetGraph()->GetAllocator()) LocationSummary(ror, LocationSummary::kNoCall);
Artem Serov02109dd2016-09-23 17:17:54 +01004875 switch (ror->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004876 case DataType::Type::kInt32: {
Artem Serov02109dd2016-09-23 17:17:54 +01004877 locations->SetInAt(0, Location::RequiresRegister());
4878 locations->SetInAt(1, Location::RegisterOrConstant(ror->InputAt(1)));
4879 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4880 break;
4881 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004882 case DataType::Type::kInt64: {
Artem Serov02109dd2016-09-23 17:17:54 +01004883 locations->SetInAt(0, Location::RequiresRegister());
4884 if (ror->InputAt(1)->IsConstant()) {
4885 locations->SetInAt(1, Location::ConstantLocation(ror->InputAt(1)->AsConstant()));
4886 } else {
4887 locations->SetInAt(1, Location::RequiresRegister());
4888 locations->AddTemp(Location::RequiresRegister());
4889 locations->AddTemp(Location::RequiresRegister());
4890 }
4891 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
4892 break;
4893 }
4894 default:
4895 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
4896 }
4897}
4898
4899void InstructionCodeGeneratorARMVIXL::VisitRor(HRor* ror) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004900 DataType::Type type = ror->GetResultType();
Artem Serov02109dd2016-09-23 17:17:54 +01004901 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004902 case DataType::Type::kInt32: {
Artem Serov02109dd2016-09-23 17:17:54 +01004903 HandleIntegerRotate(ror);
4904 break;
4905 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004906 case DataType::Type::kInt64: {
Artem Serov02109dd2016-09-23 17:17:54 +01004907 HandleLongRotate(ror);
4908 break;
4909 }
4910 default:
4911 LOG(FATAL) << "Unexpected operation type " << type;
4912 UNREACHABLE();
4913 }
4914}
4915
Artem Serov02d37832016-10-25 15:25:33 +01004916void LocationsBuilderARMVIXL::HandleShift(HBinaryOperation* op) {
4917 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
4918
4919 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004920 new (GetGraph()->GetAllocator()) LocationSummary(op, LocationSummary::kNoCall);
Artem Serov02d37832016-10-25 15:25:33 +01004921
4922 switch (op->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004923 case DataType::Type::kInt32: {
Artem Serov02d37832016-10-25 15:25:33 +01004924 locations->SetInAt(0, Location::RequiresRegister());
4925 if (op->InputAt(1)->IsConstant()) {
4926 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
4927 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4928 } else {
4929 locations->SetInAt(1, Location::RequiresRegister());
4930 // Make the output overlap, as it will be used to hold the masked
4931 // second input.
4932 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
4933 }
4934 break;
4935 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004936 case DataType::Type::kInt64: {
Artem Serov02d37832016-10-25 15:25:33 +01004937 locations->SetInAt(0, Location::RequiresRegister());
4938 if (op->InputAt(1)->IsConstant()) {
4939 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
4940 // For simplicity, use kOutputOverlap even though we only require that low registers
4941 // don't clash with high registers which the register allocator currently guarantees.
4942 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
4943 } else {
4944 locations->SetInAt(1, Location::RequiresRegister());
4945 locations->AddTemp(Location::RequiresRegister());
4946 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
4947 }
4948 break;
4949 }
4950 default:
4951 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
4952 }
4953}
4954
4955void InstructionCodeGeneratorARMVIXL::HandleShift(HBinaryOperation* op) {
4956 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
4957
4958 LocationSummary* locations = op->GetLocations();
4959 Location out = locations->Out();
4960 Location first = locations->InAt(0);
4961 Location second = locations->InAt(1);
4962
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004963 DataType::Type type = op->GetResultType();
Artem Serov02d37832016-10-25 15:25:33 +01004964 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004965 case DataType::Type::kInt32: {
Artem Serov02d37832016-10-25 15:25:33 +01004966 vixl32::Register out_reg = OutputRegister(op);
4967 vixl32::Register first_reg = InputRegisterAt(op, 0);
4968 if (second.IsRegister()) {
4969 vixl32::Register second_reg = RegisterFrom(second);
4970 // ARM doesn't mask the shift count so we need to do it ourselves.
4971 __ And(out_reg, second_reg, kMaxIntShiftDistance);
4972 if (op->IsShl()) {
4973 __ Lsl(out_reg, first_reg, out_reg);
4974 } else if (op->IsShr()) {
4975 __ Asr(out_reg, first_reg, out_reg);
4976 } else {
4977 __ Lsr(out_reg, first_reg, out_reg);
4978 }
4979 } else {
Anton Kirilov644032c2016-12-06 17:51:43 +00004980 int32_t cst = Int32ConstantFrom(second);
Artem Serov02d37832016-10-25 15:25:33 +01004981 uint32_t shift_value = cst & kMaxIntShiftDistance;
4982 if (shift_value == 0) { // ARM does not support shifting with 0 immediate.
4983 __ Mov(out_reg, first_reg);
4984 } else if (op->IsShl()) {
4985 __ Lsl(out_reg, first_reg, shift_value);
4986 } else if (op->IsShr()) {
4987 __ Asr(out_reg, first_reg, shift_value);
4988 } else {
4989 __ Lsr(out_reg, first_reg, shift_value);
4990 }
4991 }
4992 break;
4993 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004994 case DataType::Type::kInt64: {
Artem Serov02d37832016-10-25 15:25:33 +01004995 vixl32::Register o_h = HighRegisterFrom(out);
4996 vixl32::Register o_l = LowRegisterFrom(out);
4997
4998 vixl32::Register high = HighRegisterFrom(first);
4999 vixl32::Register low = LowRegisterFrom(first);
5000
5001 if (second.IsRegister()) {
5002 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
5003
5004 vixl32::Register second_reg = RegisterFrom(second);
5005
5006 if (op->IsShl()) {
5007 __ And(o_l, second_reg, kMaxLongShiftDistance);
5008 // Shift the high part
5009 __ Lsl(o_h, high, o_l);
5010 // Shift the low part and `or` what overflew on the high part
Scott Wakelingb77051e2016-11-21 19:46:00 +00005011 __ Rsb(temp, o_l, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01005012 __ Lsr(temp, low, temp);
5013 __ Orr(o_h, o_h, temp);
5014 // If the shift is > 32 bits, override the high part
Scott Wakelingb77051e2016-11-21 19:46:00 +00005015 __ Subs(temp, o_l, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01005016 {
Artem Serov0fb37192016-12-06 18:13:40 +00005017 ExactAssemblyScope guard(GetVIXLAssembler(),
5018 2 * vixl32::kMaxInstructionSizeInBytes,
5019 CodeBufferCheckScope::kMaximumSize);
Artem Serov02d37832016-10-25 15:25:33 +01005020 __ it(pl);
5021 __ lsl(pl, o_h, low, temp);
5022 }
5023 // Shift the low part
5024 __ Lsl(o_l, low, o_l);
5025 } else if (op->IsShr()) {
5026 __ And(o_h, second_reg, kMaxLongShiftDistance);
5027 // Shift the low part
5028 __ Lsr(o_l, low, o_h);
5029 // Shift the high part and `or` what underflew on the low part
Scott Wakelingb77051e2016-11-21 19:46:00 +00005030 __ Rsb(temp, o_h, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01005031 __ Lsl(temp, high, temp);
5032 __ Orr(o_l, o_l, temp);
5033 // If the shift is > 32 bits, override the low part
Scott Wakelingb77051e2016-11-21 19:46:00 +00005034 __ Subs(temp, o_h, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01005035 {
Artem Serov0fb37192016-12-06 18:13:40 +00005036 ExactAssemblyScope guard(GetVIXLAssembler(),
5037 2 * vixl32::kMaxInstructionSizeInBytes,
5038 CodeBufferCheckScope::kMaximumSize);
Artem Serov02d37832016-10-25 15:25:33 +01005039 __ it(pl);
5040 __ asr(pl, o_l, high, temp);
5041 }
5042 // Shift the high part
5043 __ Asr(o_h, high, o_h);
5044 } else {
5045 __ And(o_h, second_reg, kMaxLongShiftDistance);
5046 // same as Shr except we use `Lsr`s and not `Asr`s
5047 __ Lsr(o_l, low, o_h);
Scott Wakelingb77051e2016-11-21 19:46:00 +00005048 __ Rsb(temp, o_h, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01005049 __ Lsl(temp, high, temp);
5050 __ Orr(o_l, o_l, temp);
Scott Wakelingb77051e2016-11-21 19:46:00 +00005051 __ Subs(temp, o_h, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01005052 {
Artem Serov0fb37192016-12-06 18:13:40 +00005053 ExactAssemblyScope guard(GetVIXLAssembler(),
5054 2 * vixl32::kMaxInstructionSizeInBytes,
5055 CodeBufferCheckScope::kMaximumSize);
Artem Serov02d37832016-10-25 15:25:33 +01005056 __ it(pl);
5057 __ lsr(pl, o_l, high, temp);
5058 }
5059 __ Lsr(o_h, high, o_h);
5060 }
5061 } else {
5062 // Register allocator doesn't create partial overlap.
5063 DCHECK(!o_l.Is(high));
5064 DCHECK(!o_h.Is(low));
Anton Kirilov644032c2016-12-06 17:51:43 +00005065 int32_t cst = Int32ConstantFrom(second);
Artem Serov02d37832016-10-25 15:25:33 +01005066 uint32_t shift_value = cst & kMaxLongShiftDistance;
5067 if (shift_value > 32) {
5068 if (op->IsShl()) {
5069 __ Lsl(o_h, low, shift_value - 32);
5070 __ Mov(o_l, 0);
5071 } else if (op->IsShr()) {
5072 __ Asr(o_l, high, shift_value - 32);
5073 __ Asr(o_h, high, 31);
5074 } else {
5075 __ Lsr(o_l, high, shift_value - 32);
5076 __ Mov(o_h, 0);
5077 }
5078 } else if (shift_value == 32) {
5079 if (op->IsShl()) {
5080 __ Mov(o_h, low);
5081 __ Mov(o_l, 0);
5082 } else if (op->IsShr()) {
5083 __ Mov(o_l, high);
5084 __ Asr(o_h, high, 31);
5085 } else {
5086 __ Mov(o_l, high);
5087 __ Mov(o_h, 0);
5088 }
5089 } else if (shift_value == 1) {
5090 if (op->IsShl()) {
5091 __ Lsls(o_l, low, 1);
5092 __ Adc(o_h, high, high);
5093 } else if (op->IsShr()) {
5094 __ Asrs(o_h, high, 1);
5095 __ Rrx(o_l, low);
5096 } else {
5097 __ Lsrs(o_h, high, 1);
5098 __ Rrx(o_l, low);
5099 }
5100 } else {
5101 DCHECK(2 <= shift_value && shift_value < 32) << shift_value;
5102 if (op->IsShl()) {
5103 __ Lsl(o_h, high, shift_value);
5104 __ Orr(o_h, o_h, Operand(low, ShiftType::LSR, 32 - shift_value));
5105 __ Lsl(o_l, low, shift_value);
5106 } else if (op->IsShr()) {
5107 __ Lsr(o_l, low, shift_value);
5108 __ Orr(o_l, o_l, Operand(high, ShiftType::LSL, 32 - shift_value));
5109 __ Asr(o_h, high, shift_value);
5110 } else {
5111 __ Lsr(o_l, low, shift_value);
5112 __ Orr(o_l, o_l, Operand(high, ShiftType::LSL, 32 - shift_value));
5113 __ Lsr(o_h, high, shift_value);
5114 }
5115 }
5116 }
5117 break;
5118 }
5119 default:
5120 LOG(FATAL) << "Unexpected operation type " << type;
5121 UNREACHABLE();
5122 }
5123}
5124
5125void LocationsBuilderARMVIXL::VisitShl(HShl* shl) {
5126 HandleShift(shl);
5127}
5128
5129void InstructionCodeGeneratorARMVIXL::VisitShl(HShl* shl) {
5130 HandleShift(shl);
5131}
5132
5133void LocationsBuilderARMVIXL::VisitShr(HShr* shr) {
5134 HandleShift(shr);
5135}
5136
5137void InstructionCodeGeneratorARMVIXL::VisitShr(HShr* shr) {
5138 HandleShift(shr);
5139}
5140
5141void LocationsBuilderARMVIXL::VisitUShr(HUShr* ushr) {
5142 HandleShift(ushr);
5143}
5144
5145void InstructionCodeGeneratorARMVIXL::VisitUShr(HUShr* ushr) {
5146 HandleShift(ushr);
5147}
5148
5149void LocationsBuilderARMVIXL::VisitNewInstance(HNewInstance* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005150 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5151 instruction, LocationSummary::kCallOnMainOnly);
Artem Serov02d37832016-10-25 15:25:33 +01005152 if (instruction->IsStringAlloc()) {
5153 locations->AddTemp(LocationFrom(kMethodRegister));
5154 } else {
5155 InvokeRuntimeCallingConventionARMVIXL calling_convention;
5156 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
Artem Serov02d37832016-10-25 15:25:33 +01005157 }
5158 locations->SetOut(LocationFrom(r0));
5159}
5160
5161void InstructionCodeGeneratorARMVIXL::VisitNewInstance(HNewInstance* instruction) {
5162 // Note: if heap poisoning is enabled, the entry point takes cares
5163 // of poisoning the reference.
5164 if (instruction->IsStringAlloc()) {
5165 // String is allocated through StringFactory. Call NewEmptyString entry point.
5166 vixl32::Register temp = RegisterFrom(instruction->GetLocations()->GetTemp(0));
5167 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize);
5168 GetAssembler()->LoadFromOffset(kLoadWord, temp, tr, QUICK_ENTRY_POINT(pNewEmptyString));
5169 GetAssembler()->LoadFromOffset(kLoadWord, lr, temp, code_offset.Int32Value());
Alexandre Rames374ddf32016-11-04 10:40:49 +00005170 // 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 +00005171 ExactAssemblyScope aas(GetVIXLAssembler(),
5172 vixl32::k16BitT32InstructionSizeInBytes,
5173 CodeBufferCheckScope::kExactSize);
Artem Serov02d37832016-10-25 15:25:33 +01005174 __ blx(lr);
5175 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
5176 } else {
5177 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00005178 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
Artem Serov02d37832016-10-25 15:25:33 +01005179 }
Roland Levillain5daa4952017-07-03 17:23:56 +01005180 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 10);
Artem Serov02d37832016-10-25 15:25:33 +01005181}
5182
5183void LocationsBuilderARMVIXL::VisitNewArray(HNewArray* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005184 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5185 instruction, LocationSummary::kCallOnMainOnly);
Artem Serov02d37832016-10-25 15:25:33 +01005186 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Artem Serov02d37832016-10-25 15:25:33 +01005187 locations->SetOut(LocationFrom(r0));
Nicolas Geoffray8c7c4f12017-01-26 10:13:11 +00005188 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5189 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
Artem Serov02d37832016-10-25 15:25:33 +01005190}
5191
5192void InstructionCodeGeneratorARMVIXL::VisitNewArray(HNewArray* instruction) {
Artem Serov02d37832016-10-25 15:25:33 +01005193 // Note: if heap poisoning is enabled, the entry point takes cares
5194 // of poisoning the reference.
Artem Serov7b3672e2017-02-03 17:30:34 +00005195 QuickEntrypointEnum entrypoint =
5196 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
5197 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005198 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Artem Serov7b3672e2017-02-03 17:30:34 +00005199 DCHECK(!codegen_->IsLeafMethod());
Roland Levillain5daa4952017-07-03 17:23:56 +01005200 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 11);
Artem Serov02d37832016-10-25 15:25:33 +01005201}
5202
5203void LocationsBuilderARMVIXL::VisitParameterValue(HParameterValue* instruction) {
5204 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005205 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Artem Serov02d37832016-10-25 15:25:33 +01005206 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
5207 if (location.IsStackSlot()) {
5208 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5209 } else if (location.IsDoubleStackSlot()) {
5210 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5211 }
5212 locations->SetOut(location);
5213}
5214
5215void InstructionCodeGeneratorARMVIXL::VisitParameterValue(
5216 HParameterValue* instruction ATTRIBUTE_UNUSED) {
5217 // Nothing to do, the parameter is already at its location.
5218}
5219
5220void LocationsBuilderARMVIXL::VisitCurrentMethod(HCurrentMethod* instruction) {
5221 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005222 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Artem Serov02d37832016-10-25 15:25:33 +01005223 locations->SetOut(LocationFrom(kMethodRegister));
5224}
5225
5226void InstructionCodeGeneratorARMVIXL::VisitCurrentMethod(
5227 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
5228 // Nothing to do, the method is already at its location.
5229}
5230
5231void LocationsBuilderARMVIXL::VisitNot(HNot* not_) {
5232 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005233 new (GetGraph()->GetAllocator()) LocationSummary(not_, LocationSummary::kNoCall);
Artem Serov02d37832016-10-25 15:25:33 +01005234 locations->SetInAt(0, Location::RequiresRegister());
5235 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5236}
5237
5238void InstructionCodeGeneratorARMVIXL::VisitNot(HNot* not_) {
5239 LocationSummary* locations = not_->GetLocations();
5240 Location out = locations->Out();
5241 Location in = locations->InAt(0);
5242 switch (not_->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005243 case DataType::Type::kInt32:
Artem Serov02d37832016-10-25 15:25:33 +01005244 __ Mvn(OutputRegister(not_), InputRegisterAt(not_, 0));
5245 break;
5246
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005247 case DataType::Type::kInt64:
Artem Serov02d37832016-10-25 15:25:33 +01005248 __ Mvn(LowRegisterFrom(out), LowRegisterFrom(in));
5249 __ Mvn(HighRegisterFrom(out), HighRegisterFrom(in));
5250 break;
5251
5252 default:
5253 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
5254 }
5255}
5256
Scott Wakelingc34dba72016-10-03 10:14:44 +01005257void LocationsBuilderARMVIXL::VisitBooleanNot(HBooleanNot* bool_not) {
5258 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005259 new (GetGraph()->GetAllocator()) LocationSummary(bool_not, LocationSummary::kNoCall);
Scott Wakelingc34dba72016-10-03 10:14:44 +01005260 locations->SetInAt(0, Location::RequiresRegister());
5261 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5262}
5263
5264void InstructionCodeGeneratorARMVIXL::VisitBooleanNot(HBooleanNot* bool_not) {
5265 __ Eor(OutputRegister(bool_not), InputRegister(bool_not), 1);
5266}
5267
Artem Serov02d37832016-10-25 15:25:33 +01005268void LocationsBuilderARMVIXL::VisitCompare(HCompare* compare) {
5269 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005270 new (GetGraph()->GetAllocator()) LocationSummary(compare, LocationSummary::kNoCall);
Artem Serov02d37832016-10-25 15:25:33 +01005271 switch (compare->InputAt(0)->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005272 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005273 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005274 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005275 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005276 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005277 case DataType::Type::kInt32:
5278 case DataType::Type::kInt64: {
Artem Serov02d37832016-10-25 15:25:33 +01005279 locations->SetInAt(0, Location::RequiresRegister());
5280 locations->SetInAt(1, Location::RequiresRegister());
5281 // Output overlaps because it is written before doing the low comparison.
5282 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
5283 break;
5284 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005285 case DataType::Type::kFloat32:
5286 case DataType::Type::kFloat64: {
Artem Serov02d37832016-10-25 15:25:33 +01005287 locations->SetInAt(0, Location::RequiresFpuRegister());
5288 locations->SetInAt(1, ArithmeticZeroOrFpuRegister(compare->InputAt(1)));
5289 locations->SetOut(Location::RequiresRegister());
5290 break;
5291 }
5292 default:
5293 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
5294 }
5295}
5296
5297void InstructionCodeGeneratorARMVIXL::VisitCompare(HCompare* compare) {
5298 LocationSummary* locations = compare->GetLocations();
5299 vixl32::Register out = OutputRegister(compare);
5300 Location left = locations->InAt(0);
5301 Location right = locations->InAt(1);
5302
5303 vixl32::Label less, greater, done;
Anton Kirilov6f644202017-02-27 18:29:45 +00005304 vixl32::Label* final_label = codegen_->GetFinalLabel(compare, &done);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005305 DataType::Type type = compare->InputAt(0)->GetType();
Vladimir Marko33bff252017-11-01 14:35:42 +00005306 vixl32::Condition less_cond = vixl32::Condition::None();
Artem Serov02d37832016-10-25 15:25:33 +01005307 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005308 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005309 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005310 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005311 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005312 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005313 case DataType::Type::kInt32: {
Artem Serov02d37832016-10-25 15:25:33 +01005314 // Emit move to `out` before the `Cmp`, as `Mov` might affect the status flags.
5315 __ Mov(out, 0);
5316 __ Cmp(RegisterFrom(left), RegisterFrom(right)); // Signed compare.
5317 less_cond = lt;
5318 break;
5319 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005320 case DataType::Type::kInt64: {
Artem Serov02d37832016-10-25 15:25:33 +01005321 __ Cmp(HighRegisterFrom(left), HighRegisterFrom(right)); // Signed compare.
Artem Serov517d9f62016-12-12 15:51:15 +00005322 __ B(lt, &less, /* far_target */ false);
5323 __ B(gt, &greater, /* far_target */ false);
Artem Serov02d37832016-10-25 15:25:33 +01005324 // Emit move to `out` before the last `Cmp`, as `Mov` might affect the status flags.
5325 __ Mov(out, 0);
5326 __ Cmp(LowRegisterFrom(left), LowRegisterFrom(right)); // Unsigned compare.
5327 less_cond = lo;
5328 break;
5329 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005330 case DataType::Type::kFloat32:
5331 case DataType::Type::kFloat64: {
Artem Serov02d37832016-10-25 15:25:33 +01005332 __ Mov(out, 0);
Donghui Bai426b49c2016-11-08 14:55:38 +08005333 GenerateVcmp(compare, codegen_);
Artem Serov02d37832016-10-25 15:25:33 +01005334 // To branch on the FP compare result we transfer FPSCR to APSR (encoded as PC in VMRS).
5335 __ Vmrs(RegisterOrAPSR_nzcv(kPcCode), FPSCR);
5336 less_cond = ARMFPCondition(kCondLT, compare->IsGtBias());
5337 break;
5338 }
5339 default:
5340 LOG(FATAL) << "Unexpected compare type " << type;
5341 UNREACHABLE();
5342 }
5343
Anton Kirilov6f644202017-02-27 18:29:45 +00005344 __ B(eq, final_label, /* far_target */ false);
Artem Serov517d9f62016-12-12 15:51:15 +00005345 __ B(less_cond, &less, /* far_target */ false);
Artem Serov02d37832016-10-25 15:25:33 +01005346
5347 __ Bind(&greater);
5348 __ Mov(out, 1);
Anton Kirilov6f644202017-02-27 18:29:45 +00005349 __ B(final_label);
Artem Serov02d37832016-10-25 15:25:33 +01005350
5351 __ Bind(&less);
5352 __ Mov(out, -1);
5353
Anton Kirilov6f644202017-02-27 18:29:45 +00005354 if (done.IsReferenced()) {
5355 __ Bind(&done);
5356 }
Artem Serov02d37832016-10-25 15:25:33 +01005357}
5358
5359void LocationsBuilderARMVIXL::VisitPhi(HPhi* instruction) {
5360 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005361 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Artem Serov02d37832016-10-25 15:25:33 +01005362 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
5363 locations->SetInAt(i, Location::Any());
5364 }
5365 locations->SetOut(Location::Any());
5366}
5367
5368void InstructionCodeGeneratorARMVIXL::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
5369 LOG(FATAL) << "Unreachable";
5370}
5371
5372void CodeGeneratorARMVIXL::GenerateMemoryBarrier(MemBarrierKind kind) {
5373 // TODO (ported from quick): revisit ARM barrier kinds.
5374 DmbOptions flavor = DmbOptions::ISH; // Quiet C++ warnings.
5375 switch (kind) {
5376 case MemBarrierKind::kAnyStore:
5377 case MemBarrierKind::kLoadAny:
5378 case MemBarrierKind::kAnyAny: {
5379 flavor = DmbOptions::ISH;
5380 break;
5381 }
5382 case MemBarrierKind::kStoreStore: {
5383 flavor = DmbOptions::ISHST;
5384 break;
5385 }
5386 default:
5387 LOG(FATAL) << "Unexpected memory barrier " << kind;
5388 }
5389 __ Dmb(flavor);
5390}
5391
5392void InstructionCodeGeneratorARMVIXL::GenerateWideAtomicLoad(vixl32::Register addr,
5393 uint32_t offset,
5394 vixl32::Register out_lo,
5395 vixl32::Register out_hi) {
5396 UseScratchRegisterScope temps(GetVIXLAssembler());
5397 if (offset != 0) {
5398 vixl32::Register temp = temps.Acquire();
5399 __ Add(temp, addr, offset);
5400 addr = temp;
5401 }
Scott Wakelingb77051e2016-11-21 19:46:00 +00005402 __ Ldrexd(out_lo, out_hi, MemOperand(addr));
Artem Serov02d37832016-10-25 15:25:33 +01005403}
5404
5405void InstructionCodeGeneratorARMVIXL::GenerateWideAtomicStore(vixl32::Register addr,
5406 uint32_t offset,
5407 vixl32::Register value_lo,
5408 vixl32::Register value_hi,
5409 vixl32::Register temp1,
5410 vixl32::Register temp2,
5411 HInstruction* instruction) {
5412 UseScratchRegisterScope temps(GetVIXLAssembler());
5413 vixl32::Label fail;
5414 if (offset != 0) {
5415 vixl32::Register temp = temps.Acquire();
5416 __ Add(temp, addr, offset);
5417 addr = temp;
5418 }
5419 __ Bind(&fail);
Alexandre Rames374ddf32016-11-04 10:40:49 +00005420 {
5421 // Ensure the pc position is recorded immediately after the `ldrexd` instruction.
Artem Serov0fb37192016-12-06 18:13:40 +00005422 ExactAssemblyScope aas(GetVIXLAssembler(),
5423 vixl32::kMaxInstructionSizeInBytes,
5424 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00005425 // We need a load followed by store. (The address used in a STREX instruction must
5426 // be the same as the address in the most recently executed LDREX instruction.)
5427 __ ldrexd(temp1, temp2, MemOperand(addr));
5428 codegen_->MaybeRecordImplicitNullCheck(instruction);
5429 }
Scott Wakelingb77051e2016-11-21 19:46:00 +00005430 __ Strexd(temp1, value_lo, value_hi, MemOperand(addr));
xueliang.zhongf51bc622016-11-04 09:23:32 +00005431 __ CompareAndBranchIfNonZero(temp1, &fail);
Artem Serov02d37832016-10-25 15:25:33 +01005432}
Artem Serov02109dd2016-09-23 17:17:54 +01005433
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005434void LocationsBuilderARMVIXL::HandleFieldSet(
5435 HInstruction* instruction, const FieldInfo& field_info) {
5436 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
5437
5438 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005439 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005440 locations->SetInAt(0, Location::RequiresRegister());
5441
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005442 DataType::Type field_type = field_info.GetFieldType();
5443 if (DataType::IsFloatingPointType(field_type)) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005444 locations->SetInAt(1, Location::RequiresFpuRegister());
5445 } else {
5446 locations->SetInAt(1, Location::RequiresRegister());
5447 }
5448
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005449 bool is_wide = field_type == DataType::Type::kInt64 || field_type == DataType::Type::kFloat64;
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005450 bool generate_volatile = field_info.IsVolatile()
5451 && is_wide
5452 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
5453 bool needs_write_barrier =
5454 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
5455 // Temporary registers for the write barrier.
5456 // TODO: consider renaming StoreNeedsWriteBarrier to StoreNeedsGCMark.
5457 if (needs_write_barrier) {
5458 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
5459 locations->AddTemp(Location::RequiresRegister());
5460 } else if (generate_volatile) {
5461 // ARM encoding have some additional constraints for ldrexd/strexd:
5462 // - registers need to be consecutive
5463 // - the first register should be even but not R14.
5464 // We don't test for ARM yet, and the assertion makes sure that we
5465 // revisit this if we ever enable ARM encoding.
5466 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
5467
5468 locations->AddTemp(Location::RequiresRegister());
5469 locations->AddTemp(Location::RequiresRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005470 if (field_type == DataType::Type::kFloat64) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005471 // For doubles we need two more registers to copy the value.
5472 locations->AddTemp(LocationFrom(r2));
5473 locations->AddTemp(LocationFrom(r3));
5474 }
5475 }
5476}
5477
5478void InstructionCodeGeneratorARMVIXL::HandleFieldSet(HInstruction* instruction,
5479 const FieldInfo& field_info,
5480 bool value_can_be_null) {
5481 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
5482
5483 LocationSummary* locations = instruction->GetLocations();
5484 vixl32::Register base = InputRegisterAt(instruction, 0);
5485 Location value = locations->InAt(1);
5486
5487 bool is_volatile = field_info.IsVolatile();
5488 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005489 DataType::Type field_type = field_info.GetFieldType();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005490 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
5491 bool needs_write_barrier =
5492 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
5493
5494 if (is_volatile) {
5495 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
5496 }
5497
5498 switch (field_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005499 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005500 case DataType::Type::kUint8:
5501 case DataType::Type::kInt8:
5502 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005503 case DataType::Type::kInt16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005504 case DataType::Type::kInt32: {
5505 StoreOperandType operand_type = GetStoreOperandType(field_type);
5506 GetAssembler()->StoreToOffset(operand_type, RegisterFrom(value), base, offset);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005507 break;
5508 }
5509
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005510 case DataType::Type::kReference: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005511 if (kPoisonHeapReferences && needs_write_barrier) {
5512 // Note that in the case where `value` is a null reference,
5513 // we do not enter this block, as a null reference does not
5514 // need poisoning.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005515 DCHECK_EQ(field_type, DataType::Type::kReference);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005516 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
5517 __ Mov(temp, RegisterFrom(value));
5518 GetAssembler()->PoisonHeapReference(temp);
5519 GetAssembler()->StoreToOffset(kStoreWord, temp, base, offset);
5520 } else {
5521 GetAssembler()->StoreToOffset(kStoreWord, RegisterFrom(value), base, offset);
5522 }
5523 break;
5524 }
5525
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005526 case DataType::Type::kInt64: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005527 if (is_volatile && !atomic_ldrd_strd) {
5528 GenerateWideAtomicStore(base,
5529 offset,
5530 LowRegisterFrom(value),
5531 HighRegisterFrom(value),
5532 RegisterFrom(locations->GetTemp(0)),
5533 RegisterFrom(locations->GetTemp(1)),
5534 instruction);
5535 } else {
5536 GetAssembler()->StoreToOffset(kStoreWordPair, LowRegisterFrom(value), base, offset);
5537 codegen_->MaybeRecordImplicitNullCheck(instruction);
5538 }
5539 break;
5540 }
5541
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005542 case DataType::Type::kFloat32: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005543 GetAssembler()->StoreSToOffset(SRegisterFrom(value), base, offset);
5544 break;
5545 }
5546
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005547 case DataType::Type::kFloat64: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01005548 vixl32::DRegister value_reg = DRegisterFrom(value);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005549 if (is_volatile && !atomic_ldrd_strd) {
5550 vixl32::Register value_reg_lo = RegisterFrom(locations->GetTemp(0));
5551 vixl32::Register value_reg_hi = RegisterFrom(locations->GetTemp(1));
5552
5553 __ Vmov(value_reg_lo, value_reg_hi, value_reg);
5554
5555 GenerateWideAtomicStore(base,
5556 offset,
5557 value_reg_lo,
5558 value_reg_hi,
5559 RegisterFrom(locations->GetTemp(2)),
5560 RegisterFrom(locations->GetTemp(3)),
5561 instruction);
5562 } else {
5563 GetAssembler()->StoreDToOffset(value_reg, base, offset);
5564 codegen_->MaybeRecordImplicitNullCheck(instruction);
5565 }
5566 break;
5567 }
5568
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005569 case DataType::Type::kVoid:
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005570 LOG(FATAL) << "Unreachable type " << field_type;
5571 UNREACHABLE();
5572 }
5573
5574 // Longs and doubles are handled in the switch.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005575 if (field_type != DataType::Type::kInt64 && field_type != DataType::Type::kFloat64) {
Alexandre Rames374ddf32016-11-04 10:40:49 +00005576 // TODO(VIXL): Here and for other calls to `MaybeRecordImplicitNullCheck` in this method, we
5577 // should use a scope and the assembler to emit the store instruction to guarantee that we
5578 // record the pc at the correct position. But the `Assembler` does not automatically handle
5579 // unencodable offsets. Practically, everything is fine because the helper and VIXL, at the time
5580 // of writing, do generate the store instruction last.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005581 codegen_->MaybeRecordImplicitNullCheck(instruction);
5582 }
5583
5584 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
5585 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
5586 vixl32::Register card = RegisterFrom(locations->GetTemp(1));
5587 codegen_->MarkGCCard(temp, card, base, RegisterFrom(value), value_can_be_null);
5588 }
5589
5590 if (is_volatile) {
5591 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
5592 }
5593}
5594
Artem Serov02d37832016-10-25 15:25:33 +01005595void LocationsBuilderARMVIXL::HandleFieldGet(HInstruction* instruction,
5596 const FieldInfo& field_info) {
5597 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
5598
5599 bool object_field_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005600 kEmitCompilerReadBarrier && (field_info.GetFieldType() == DataType::Type::kReference);
Artem Serov02d37832016-10-25 15:25:33 +01005601 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005602 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
5603 object_field_get_with_read_barrier
5604 ? LocationSummary::kCallOnSlowPath
5605 : LocationSummary::kNoCall);
Artem Serov02d37832016-10-25 15:25:33 +01005606 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
5607 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
5608 }
5609 locations->SetInAt(0, Location::RequiresRegister());
5610
5611 bool volatile_for_double = field_info.IsVolatile()
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005612 && (field_info.GetFieldType() == DataType::Type::kFloat64)
Artem Serov02d37832016-10-25 15:25:33 +01005613 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
5614 // The output overlaps in case of volatile long: we don't want the
5615 // code generated by GenerateWideAtomicLoad to overwrite the
5616 // object's location. Likewise, in the case of an object field get
5617 // with read barriers enabled, we do not want the load to overwrite
5618 // the object's location, as we need it to emit the read barrier.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005619 bool overlap =
5620 (field_info.IsVolatile() && (field_info.GetFieldType() == DataType::Type::kInt64)) ||
Artem Serov02d37832016-10-25 15:25:33 +01005621 object_field_get_with_read_barrier;
5622
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005623 if (DataType::IsFloatingPointType(instruction->GetType())) {
Artem Serov02d37832016-10-25 15:25:33 +01005624 locations->SetOut(Location::RequiresFpuRegister());
5625 } else {
5626 locations->SetOut(Location::RequiresRegister(),
5627 (overlap ? Location::kOutputOverlap : Location::kNoOutputOverlap));
5628 }
5629 if (volatile_for_double) {
5630 // ARM encoding have some additional constraints for ldrexd/strexd:
5631 // - registers need to be consecutive
5632 // - the first register should be even but not R14.
5633 // We don't test for ARM yet, and the assertion makes sure that we
5634 // revisit this if we ever enable ARM encoding.
5635 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
5636 locations->AddTemp(Location::RequiresRegister());
5637 locations->AddTemp(Location::RequiresRegister());
5638 } else if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
5639 // We need a temporary register for the read barrier marking slow
Artem Serovc5fcb442016-12-02 19:19:58 +00005640 // path in CodeGeneratorARMVIXL::GenerateFieldLoadWithBakerReadBarrier.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01005641 if (kBakerReadBarrierLinkTimeThunksEnableForFields &&
5642 !Runtime::Current()->UseJitCompilation()) {
5643 // If link-time thunks for the Baker read barrier are enabled, for AOT
5644 // loads we need a temporary only if the offset is too big.
5645 if (field_info.GetFieldOffset().Uint32Value() >= kReferenceLoadMinFarOffset) {
5646 locations->AddTemp(Location::RequiresRegister());
5647 }
5648 // And we always need the reserved entrypoint register.
5649 locations->AddTemp(Location::RegisterLocation(kBakerCcEntrypointRegister.GetCode()));
5650 } else {
5651 locations->AddTemp(Location::RequiresRegister());
5652 }
Artem Serov02d37832016-10-25 15:25:33 +01005653 }
5654}
5655
5656Location LocationsBuilderARMVIXL::ArithmeticZeroOrFpuRegister(HInstruction* input) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005657 DCHECK(DataType::IsFloatingPointType(input->GetType())) << input->GetType();
Artem Serov02d37832016-10-25 15:25:33 +01005658 if ((input->IsFloatConstant() && (input->AsFloatConstant()->IsArithmeticZero())) ||
5659 (input->IsDoubleConstant() && (input->AsDoubleConstant()->IsArithmeticZero()))) {
5660 return Location::ConstantLocation(input->AsConstant());
5661 } else {
5662 return Location::RequiresFpuRegister();
5663 }
5664}
5665
Artem Serov02109dd2016-09-23 17:17:54 +01005666Location LocationsBuilderARMVIXL::ArmEncodableConstantOrRegister(HInstruction* constant,
5667 Opcode opcode) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005668 DCHECK(!DataType::IsFloatingPointType(constant->GetType()));
Artem Serov02109dd2016-09-23 17:17:54 +01005669 if (constant->IsConstant() &&
5670 CanEncodeConstantAsImmediate(constant->AsConstant(), opcode)) {
5671 return Location::ConstantLocation(constant->AsConstant());
5672 }
5673 return Location::RequiresRegister();
5674}
5675
5676bool LocationsBuilderARMVIXL::CanEncodeConstantAsImmediate(HConstant* input_cst,
5677 Opcode opcode) {
5678 uint64_t value = static_cast<uint64_t>(Int64FromConstant(input_cst));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005679 if (DataType::Is64BitType(input_cst->GetType())) {
Artem Serov02109dd2016-09-23 17:17:54 +01005680 Opcode high_opcode = opcode;
5681 SetCc low_set_cc = kCcDontCare;
5682 switch (opcode) {
5683 case SUB:
5684 // Flip the operation to an ADD.
5685 value = -value;
5686 opcode = ADD;
5687 FALLTHROUGH_INTENDED;
5688 case ADD:
5689 if (Low32Bits(value) == 0u) {
5690 return CanEncodeConstantAsImmediate(High32Bits(value), opcode, kCcDontCare);
5691 }
5692 high_opcode = ADC;
5693 low_set_cc = kCcSet;
5694 break;
5695 default:
5696 break;
5697 }
5698 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode, low_set_cc) &&
5699 CanEncodeConstantAsImmediate(High32Bits(value), high_opcode, kCcDontCare);
5700 } else {
5701 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode);
5702 }
5703}
5704
5705// TODO(VIXL): Replace art::arm::SetCc` with `vixl32::FlagsUpdate after flags set optimization
5706// enabled.
5707bool LocationsBuilderARMVIXL::CanEncodeConstantAsImmediate(uint32_t value,
5708 Opcode opcode,
5709 SetCc set_cc) {
5710 ArmVIXLAssembler* assembler = codegen_->GetAssembler();
5711 if (assembler->ShifterOperandCanHold(opcode, value, set_cc)) {
5712 return true;
5713 }
5714 Opcode neg_opcode = kNoOperand;
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00005715 uint32_t neg_value = 0;
Artem Serov02109dd2016-09-23 17:17:54 +01005716 switch (opcode) {
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00005717 case AND: neg_opcode = BIC; neg_value = ~value; break;
5718 case ORR: neg_opcode = ORN; neg_value = ~value; break;
5719 case ADD: neg_opcode = SUB; neg_value = -value; break;
5720 case ADC: neg_opcode = SBC; neg_value = ~value; break;
5721 case SUB: neg_opcode = ADD; neg_value = -value; break;
5722 case SBC: neg_opcode = ADC; neg_value = ~value; break;
5723 case MOV: neg_opcode = MVN; neg_value = ~value; break;
Artem Serov02109dd2016-09-23 17:17:54 +01005724 default:
5725 return false;
5726 }
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00005727
5728 if (assembler->ShifterOperandCanHold(neg_opcode, neg_value, set_cc)) {
5729 return true;
5730 }
5731
5732 return opcode == AND && IsPowerOfTwo(value + 1);
Artem Serov02109dd2016-09-23 17:17:54 +01005733}
5734
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005735void InstructionCodeGeneratorARMVIXL::HandleFieldGet(HInstruction* instruction,
5736 const FieldInfo& field_info) {
5737 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
5738
5739 LocationSummary* locations = instruction->GetLocations();
5740 vixl32::Register base = InputRegisterAt(instruction, 0);
5741 Location out = locations->Out();
5742 bool is_volatile = field_info.IsVolatile();
5743 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Vladimir Marko61b92282017-10-11 13:23:17 +01005744 DCHECK_EQ(DataType::Size(field_info.GetFieldType()), DataType::Size(instruction->GetType()));
5745 DataType::Type load_type = instruction->GetType();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005746 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
5747
Vladimir Marko61b92282017-10-11 13:23:17 +01005748 switch (load_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005749 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005750 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005751 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005752 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005753 case DataType::Type::kInt16:
5754 case DataType::Type::kInt32: {
Vladimir Marko61b92282017-10-11 13:23:17 +01005755 LoadOperandType operand_type = GetLoadOperandType(load_type);
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005756 GetAssembler()->LoadFromOffset(operand_type, RegisterFrom(out), base, offset);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005757 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005758 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005759
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005760 case DataType::Type::kReference: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005761 // /* HeapReference<Object> */ out = *(base + offset)
5762 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005763 Location temp_loc = locations->GetTemp(0);
5764 // Note that a potential implicit null check is handled in this
5765 // CodeGeneratorARMVIXL::GenerateFieldLoadWithBakerReadBarrier call.
5766 codegen_->GenerateFieldLoadWithBakerReadBarrier(
5767 instruction, out, base, offset, temp_loc, /* needs_null_check */ true);
5768 if (is_volatile) {
5769 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5770 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005771 } else {
5772 GetAssembler()->LoadFromOffset(kLoadWord, RegisterFrom(out), base, offset);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005773 codegen_->MaybeRecordImplicitNullCheck(instruction);
5774 if (is_volatile) {
5775 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5776 }
5777 // If read barriers are enabled, emit read barriers other than
5778 // Baker's using a slow path (and also unpoison the loaded
5779 // reference, if heap poisoning is enabled).
5780 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, locations->InAt(0), offset);
5781 }
5782 break;
5783 }
5784
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005785 case DataType::Type::kInt64:
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005786 if (is_volatile && !atomic_ldrd_strd) {
5787 GenerateWideAtomicLoad(base, offset, LowRegisterFrom(out), HighRegisterFrom(out));
5788 } else {
5789 GetAssembler()->LoadFromOffset(kLoadWordPair, LowRegisterFrom(out), base, offset);
5790 }
5791 break;
5792
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005793 case DataType::Type::kFloat32:
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005794 GetAssembler()->LoadSFromOffset(SRegisterFrom(out), base, offset);
5795 break;
5796
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005797 case DataType::Type::kFloat64: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01005798 vixl32::DRegister out_dreg = DRegisterFrom(out);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005799 if (is_volatile && !atomic_ldrd_strd) {
5800 vixl32::Register lo = RegisterFrom(locations->GetTemp(0));
5801 vixl32::Register hi = RegisterFrom(locations->GetTemp(1));
5802 GenerateWideAtomicLoad(base, offset, lo, hi);
5803 // TODO(VIXL): Do we need to be immediately after the ldrexd instruction? If so we need a
5804 // scope.
5805 codegen_->MaybeRecordImplicitNullCheck(instruction);
5806 __ Vmov(out_dreg, lo, hi);
5807 } else {
5808 GetAssembler()->LoadDFromOffset(out_dreg, base, offset);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005809 codegen_->MaybeRecordImplicitNullCheck(instruction);
5810 }
5811 break;
5812 }
5813
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005814 case DataType::Type::kVoid:
Vladimir Marko61b92282017-10-11 13:23:17 +01005815 LOG(FATAL) << "Unreachable type " << load_type;
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005816 UNREACHABLE();
5817 }
5818
Vladimir Marko61b92282017-10-11 13:23:17 +01005819 if (load_type == DataType::Type::kReference || load_type == DataType::Type::kFloat64) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005820 // Potential implicit null checks, in the case of reference or
5821 // double fields, are handled in the previous switch statement.
5822 } else {
5823 // Address cases other than reference and double that may require an implicit null check.
Alexandre Rames374ddf32016-11-04 10:40:49 +00005824 // TODO(VIXL): Here and for other calls to `MaybeRecordImplicitNullCheck` in this method, we
5825 // should use a scope and the assembler to emit the load instruction to guarantee that we
5826 // record the pc at the correct position. But the `Assembler` does not automatically handle
5827 // unencodable offsets. Practically, everything is fine because the helper and VIXL, at the time
5828 // of writing, do generate the store instruction last.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005829 codegen_->MaybeRecordImplicitNullCheck(instruction);
5830 }
5831
5832 if (is_volatile) {
Vladimir Marko61b92282017-10-11 13:23:17 +01005833 if (load_type == DataType::Type::kReference) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005834 // Memory barriers, in the case of references, are also handled
5835 // in the previous switch statement.
5836 } else {
5837 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5838 }
5839 }
5840}
5841
5842void LocationsBuilderARMVIXL::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
5843 HandleFieldSet(instruction, instruction->GetFieldInfo());
5844}
5845
5846void InstructionCodeGeneratorARMVIXL::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
5847 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
5848}
5849
5850void LocationsBuilderARMVIXL::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5851 HandleFieldGet(instruction, instruction->GetFieldInfo());
5852}
5853
5854void InstructionCodeGeneratorARMVIXL::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5855 HandleFieldGet(instruction, instruction->GetFieldInfo());
5856}
5857
5858void LocationsBuilderARMVIXL::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5859 HandleFieldGet(instruction, instruction->GetFieldInfo());
5860}
5861
5862void InstructionCodeGeneratorARMVIXL::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5863 HandleFieldGet(instruction, instruction->GetFieldInfo());
5864}
5865
Scott Wakelingc34dba72016-10-03 10:14:44 +01005866void LocationsBuilderARMVIXL::VisitStaticFieldSet(HStaticFieldSet* instruction) {
5867 HandleFieldSet(instruction, instruction->GetFieldInfo());
5868}
5869
5870void InstructionCodeGeneratorARMVIXL::VisitStaticFieldSet(HStaticFieldSet* instruction) {
5871 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
5872}
5873
Artem Serovcfbe9132016-10-14 15:58:56 +01005874void LocationsBuilderARMVIXL::VisitUnresolvedInstanceFieldGet(
5875 HUnresolvedInstanceFieldGet* instruction) {
5876 FieldAccessCallingConventionARMVIXL calling_convention;
5877 codegen_->CreateUnresolvedFieldLocationSummary(
5878 instruction, instruction->GetFieldType(), calling_convention);
5879}
5880
5881void InstructionCodeGeneratorARMVIXL::VisitUnresolvedInstanceFieldGet(
5882 HUnresolvedInstanceFieldGet* instruction) {
5883 FieldAccessCallingConventionARMVIXL calling_convention;
5884 codegen_->GenerateUnresolvedFieldAccess(instruction,
5885 instruction->GetFieldType(),
5886 instruction->GetFieldIndex(),
5887 instruction->GetDexPc(),
5888 calling_convention);
5889}
5890
5891void LocationsBuilderARMVIXL::VisitUnresolvedInstanceFieldSet(
5892 HUnresolvedInstanceFieldSet* instruction) {
5893 FieldAccessCallingConventionARMVIXL calling_convention;
5894 codegen_->CreateUnresolvedFieldLocationSummary(
5895 instruction, instruction->GetFieldType(), calling_convention);
5896}
5897
5898void InstructionCodeGeneratorARMVIXL::VisitUnresolvedInstanceFieldSet(
5899 HUnresolvedInstanceFieldSet* instruction) {
5900 FieldAccessCallingConventionARMVIXL calling_convention;
5901 codegen_->GenerateUnresolvedFieldAccess(instruction,
5902 instruction->GetFieldType(),
5903 instruction->GetFieldIndex(),
5904 instruction->GetDexPc(),
5905 calling_convention);
5906}
5907
5908void LocationsBuilderARMVIXL::VisitUnresolvedStaticFieldGet(
5909 HUnresolvedStaticFieldGet* instruction) {
5910 FieldAccessCallingConventionARMVIXL calling_convention;
5911 codegen_->CreateUnresolvedFieldLocationSummary(
5912 instruction, instruction->GetFieldType(), calling_convention);
5913}
5914
5915void InstructionCodeGeneratorARMVIXL::VisitUnresolvedStaticFieldGet(
5916 HUnresolvedStaticFieldGet* instruction) {
5917 FieldAccessCallingConventionARMVIXL calling_convention;
5918 codegen_->GenerateUnresolvedFieldAccess(instruction,
5919 instruction->GetFieldType(),
5920 instruction->GetFieldIndex(),
5921 instruction->GetDexPc(),
5922 calling_convention);
5923}
5924
5925void LocationsBuilderARMVIXL::VisitUnresolvedStaticFieldSet(
5926 HUnresolvedStaticFieldSet* instruction) {
5927 FieldAccessCallingConventionARMVIXL calling_convention;
5928 codegen_->CreateUnresolvedFieldLocationSummary(
5929 instruction, instruction->GetFieldType(), calling_convention);
5930}
5931
5932void InstructionCodeGeneratorARMVIXL::VisitUnresolvedStaticFieldSet(
5933 HUnresolvedStaticFieldSet* instruction) {
5934 FieldAccessCallingConventionARMVIXL calling_convention;
5935 codegen_->GenerateUnresolvedFieldAccess(instruction,
5936 instruction->GetFieldType(),
5937 instruction->GetFieldIndex(),
5938 instruction->GetDexPc(),
5939 calling_convention);
5940}
5941
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005942void LocationsBuilderARMVIXL::VisitNullCheck(HNullCheck* instruction) {
Artem Serov657022c2016-11-23 14:19:38 +00005943 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005944 locations->SetInAt(0, Location::RequiresRegister());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005945}
5946
5947void CodeGeneratorARMVIXL::GenerateImplicitNullCheck(HNullCheck* instruction) {
5948 if (CanMoveNullCheckToUser(instruction)) {
5949 return;
5950 }
5951
5952 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames374ddf32016-11-04 10:40:49 +00005953 // Ensure the pc position is recorded immediately after the `ldr` instruction.
Artem Serov0fb37192016-12-06 18:13:40 +00005954 ExactAssemblyScope aas(GetVIXLAssembler(),
5955 vixl32::kMaxInstructionSizeInBytes,
5956 CodeBufferCheckScope::kMaximumSize);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005957 __ ldr(temps.Acquire(), MemOperand(InputRegisterAt(instruction, 0)));
5958 RecordPcInfo(instruction, instruction->GetDexPc());
5959}
5960
5961void CodeGeneratorARMVIXL::GenerateExplicitNullCheck(HNullCheck* instruction) {
5962 NullCheckSlowPathARMVIXL* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01005963 new (GetScopedAllocator()) NullCheckSlowPathARMVIXL(instruction);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005964 AddSlowPath(slow_path);
xueliang.zhongf51bc622016-11-04 09:23:32 +00005965 __ CompareAndBranchIfZero(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005966}
5967
5968void InstructionCodeGeneratorARMVIXL::VisitNullCheck(HNullCheck* instruction) {
5969 codegen_->GenerateNullCheck(instruction);
5970}
5971
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005972void CodeGeneratorARMVIXL::LoadFromShiftedRegOffset(DataType::Type type,
Scott Wakelingc34dba72016-10-03 10:14:44 +01005973 Location out_loc,
5974 vixl32::Register base,
5975 vixl32::Register reg_index,
5976 vixl32::Condition cond) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005977 uint32_t shift_count = DataType::SizeShift(type);
Scott Wakelingc34dba72016-10-03 10:14:44 +01005978 MemOperand mem_address(base, reg_index, vixl32::LSL, shift_count);
5979
5980 switch (type) {
Vladimir Marko61b92282017-10-11 13:23:17 +01005981 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005982 case DataType::Type::kUint8:
Vladimir Marko61b92282017-10-11 13:23:17 +01005983 __ Ldrb(cond, RegisterFrom(out_loc), mem_address);
5984 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005985 case DataType::Type::kInt8:
Scott Wakelingc34dba72016-10-03 10:14:44 +01005986 __ Ldrsb(cond, RegisterFrom(out_loc), mem_address);
5987 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005988 case DataType::Type::kUint16:
Scott Wakelingc34dba72016-10-03 10:14:44 +01005989 __ Ldrh(cond, RegisterFrom(out_loc), mem_address);
5990 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005991 case DataType::Type::kInt16:
5992 __ Ldrsh(cond, RegisterFrom(out_loc), mem_address);
5993 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005994 case DataType::Type::kReference:
5995 case DataType::Type::kInt32:
Scott Wakelingc34dba72016-10-03 10:14:44 +01005996 __ Ldr(cond, RegisterFrom(out_loc), mem_address);
5997 break;
5998 // T32 doesn't support LoadFromShiftedRegOffset mem address mode for these types.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005999 case DataType::Type::kInt64:
6000 case DataType::Type::kFloat32:
6001 case DataType::Type::kFloat64:
Scott Wakelingc34dba72016-10-03 10:14:44 +01006002 default:
6003 LOG(FATAL) << "Unreachable type " << type;
6004 UNREACHABLE();
6005 }
6006}
6007
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006008void CodeGeneratorARMVIXL::StoreToShiftedRegOffset(DataType::Type type,
Scott Wakelingc34dba72016-10-03 10:14:44 +01006009 Location loc,
6010 vixl32::Register base,
6011 vixl32::Register reg_index,
6012 vixl32::Condition cond) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006013 uint32_t shift_count = DataType::SizeShift(type);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006014 MemOperand mem_address(base, reg_index, vixl32::LSL, shift_count);
6015
6016 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006017 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006018 case DataType::Type::kUint8:
6019 case DataType::Type::kInt8:
Scott Wakelingc34dba72016-10-03 10:14:44 +01006020 __ Strb(cond, RegisterFrom(loc), mem_address);
6021 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006022 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006023 case DataType::Type::kInt16:
Scott Wakelingc34dba72016-10-03 10:14:44 +01006024 __ Strh(cond, RegisterFrom(loc), mem_address);
6025 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006026 case DataType::Type::kReference:
6027 case DataType::Type::kInt32:
Scott Wakelingc34dba72016-10-03 10:14:44 +01006028 __ Str(cond, RegisterFrom(loc), mem_address);
6029 break;
6030 // T32 doesn't support StoreToShiftedRegOffset mem address mode for these types.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006031 case DataType::Type::kInt64:
6032 case DataType::Type::kFloat32:
6033 case DataType::Type::kFloat64:
Scott Wakelingc34dba72016-10-03 10:14:44 +01006034 default:
6035 LOG(FATAL) << "Unreachable type " << type;
6036 UNREACHABLE();
6037 }
6038}
6039
6040void LocationsBuilderARMVIXL::VisitArrayGet(HArrayGet* instruction) {
6041 bool object_array_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006042 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006043 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006044 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
6045 object_array_get_with_read_barrier
6046 ? LocationSummary::kCallOnSlowPath
6047 : LocationSummary::kNoCall);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006048 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006049 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Scott Wakelingc34dba72016-10-03 10:14:44 +01006050 }
6051 locations->SetInAt(0, Location::RequiresRegister());
6052 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006053 if (DataType::IsFloatingPointType(instruction->GetType())) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006054 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
6055 } else {
6056 // The output overlaps in the case of an object array get with
6057 // read barriers enabled: we do not want the move to overwrite the
6058 // array's location, as we need it to emit the read barrier.
6059 locations->SetOut(
6060 Location::RequiresRegister(),
6061 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
6062 }
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01006063 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
6064 // We need a temporary register for the read barrier marking slow
6065 // path in CodeGeneratorARMVIXL::GenerateArrayLoadWithBakerReadBarrier.
6066 if (kBakerReadBarrierLinkTimeThunksEnableForFields &&
6067 !Runtime::Current()->UseJitCompilation() &&
6068 instruction->GetIndex()->IsConstant()) {
6069 // Array loads with constant index are treated as field loads.
6070 // If link-time thunks for the Baker read barrier are enabled, for AOT
6071 // constant index loads we need a temporary only if the offset is too big.
6072 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
6073 uint32_t index = instruction->GetIndex()->AsIntConstant()->GetValue();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006074 offset += index << DataType::SizeShift(DataType::Type::kReference);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01006075 if (offset >= kReferenceLoadMinFarOffset) {
6076 locations->AddTemp(Location::RequiresRegister());
6077 }
6078 // And we always need the reserved entrypoint register.
6079 locations->AddTemp(Location::RegisterLocation(kBakerCcEntrypointRegister.GetCode()));
6080 } else if (kBakerReadBarrierLinkTimeThunksEnableForArrays &&
6081 !Runtime::Current()->UseJitCompilation() &&
6082 !instruction->GetIndex()->IsConstant()) {
6083 // We need a non-scratch temporary for the array data pointer.
6084 locations->AddTemp(Location::RequiresRegister());
6085 // And we always need the reserved entrypoint register.
6086 locations->AddTemp(Location::RegisterLocation(kBakerCcEntrypointRegister.GetCode()));
6087 } else {
6088 locations->AddTemp(Location::RequiresRegister());
6089 }
6090 } else if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
6091 // Also need a temporary for String compression feature.
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006092 locations->AddTemp(Location::RequiresRegister());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006093 }
6094}
6095
6096void InstructionCodeGeneratorARMVIXL::VisitArrayGet(HArrayGet* instruction) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006097 LocationSummary* locations = instruction->GetLocations();
6098 Location obj_loc = locations->InAt(0);
6099 vixl32::Register obj = InputRegisterAt(instruction, 0);
6100 Location index = locations->InAt(1);
6101 Location out_loc = locations->Out();
6102 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006103 DataType::Type type = instruction->GetType();
Scott Wakelingc34dba72016-10-03 10:14:44 +01006104 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
6105 instruction->IsStringCharAt();
6106 HInstruction* array_instr = instruction->GetArray();
6107 bool has_intermediate_address = array_instr->IsIntermediateAddress();
Scott Wakelingc34dba72016-10-03 10:14:44 +01006108
6109 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006110 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006111 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006112 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006113 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006114 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006115 case DataType::Type::kInt32: {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006116 vixl32::Register length;
6117 if (maybe_compressed_char_at) {
6118 length = RegisterFrom(locations->GetTemp(0));
6119 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
6120 GetAssembler()->LoadFromOffset(kLoadWord, length, obj, count_offset);
6121 codegen_->MaybeRecordImplicitNullCheck(instruction);
6122 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006123 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006124 int32_t const_index = Int32ConstantFrom(index);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006125 if (maybe_compressed_char_at) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006126 vixl32::Label uncompressed_load, done;
Anton Kirilov6f644202017-02-27 18:29:45 +00006127 vixl32::Label* final_label = codegen_->GetFinalLabel(instruction, &done);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006128 __ Lsrs(length, length, 1u); // LSRS has a 16-bit encoding, TST (immediate) does not.
6129 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
6130 "Expecting 0=compressed, 1=uncompressed");
Artem Serov517d9f62016-12-12 15:51:15 +00006131 __ B(cs, &uncompressed_load, /* far_target */ false);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006132 GetAssembler()->LoadFromOffset(kLoadUnsignedByte,
6133 RegisterFrom(out_loc),
6134 obj,
6135 data_offset + const_index);
Anton Kirilov6f644202017-02-27 18:29:45 +00006136 __ B(final_label);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006137 __ Bind(&uncompressed_load);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006138 GetAssembler()->LoadFromOffset(GetLoadOperandType(DataType::Type::kUint16),
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006139 RegisterFrom(out_loc),
6140 obj,
6141 data_offset + (const_index << 1));
Anton Kirilov6f644202017-02-27 18:29:45 +00006142 if (done.IsReferenced()) {
6143 __ Bind(&done);
6144 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006145 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006146 uint32_t full_offset = data_offset + (const_index << DataType::SizeShift(type));
Scott Wakelingc34dba72016-10-03 10:14:44 +01006147
6148 LoadOperandType load_type = GetLoadOperandType(type);
6149 GetAssembler()->LoadFromOffset(load_type, RegisterFrom(out_loc), obj, full_offset);
6150 }
6151 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006152 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006153 vixl32::Register temp = temps.Acquire();
6154
6155 if (has_intermediate_address) {
Artem Serov2bbc9532016-10-21 11:51:50 +01006156 // We do not need to compute the intermediate address from the array: the
6157 // input instruction has done it already. See the comment in
6158 // `TryExtractArrayAccessAddress()`.
6159 if (kIsDebugBuild) {
6160 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
Anton Kirilov644032c2016-12-06 17:51:43 +00006161 DCHECK_EQ(Uint64ConstantFrom(tmp->GetOffset()), data_offset);
Artem Serov2bbc9532016-10-21 11:51:50 +01006162 }
6163 temp = obj;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006164 } else {
6165 __ Add(temp, obj, data_offset);
6166 }
6167 if (maybe_compressed_char_at) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006168 vixl32::Label uncompressed_load, done;
Anton Kirilov6f644202017-02-27 18:29:45 +00006169 vixl32::Label* final_label = codegen_->GetFinalLabel(instruction, &done);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006170 __ Lsrs(length, length, 1u); // LSRS has a 16-bit encoding, TST (immediate) does not.
6171 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
6172 "Expecting 0=compressed, 1=uncompressed");
Artem Serov517d9f62016-12-12 15:51:15 +00006173 __ B(cs, &uncompressed_load, /* far_target */ false);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006174 __ Ldrb(RegisterFrom(out_loc), MemOperand(temp, RegisterFrom(index), vixl32::LSL, 0));
Anton Kirilov6f644202017-02-27 18:29:45 +00006175 __ B(final_label);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006176 __ Bind(&uncompressed_load);
6177 __ Ldrh(RegisterFrom(out_loc), MemOperand(temp, RegisterFrom(index), vixl32::LSL, 1));
Anton Kirilov6f644202017-02-27 18:29:45 +00006178 if (done.IsReferenced()) {
6179 __ Bind(&done);
6180 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006181 } else {
6182 codegen_->LoadFromShiftedRegOffset(type, out_loc, temp, RegisterFrom(index));
6183 }
6184 }
6185 break;
6186 }
6187
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006188 case DataType::Type::kReference: {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006189 // The read barrier instrumentation of object ArrayGet
6190 // instructions does not support the HIntermediateAddress
6191 // instruction.
6192 DCHECK(!(has_intermediate_address && kEmitCompilerReadBarrier));
6193
Scott Wakelingc34dba72016-10-03 10:14:44 +01006194 static_assert(
6195 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6196 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
6197 // /* HeapReference<Object> */ out =
6198 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
6199 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006200 Location temp = locations->GetTemp(0);
6201 // Note that a potential implicit null check is handled in this
6202 // CodeGeneratorARMVIXL::GenerateArrayLoadWithBakerReadBarrier call.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01006203 DCHECK(!instruction->CanDoImplicitNullCheckOn(instruction->InputAt(0)));
6204 if (index.IsConstant()) {
6205 // Array load with a constant index can be treated as a field load.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006206 data_offset += Int32ConstantFrom(index) << DataType::SizeShift(type);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01006207 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6208 out_loc,
6209 obj,
6210 data_offset,
6211 locations->GetTemp(0),
6212 /* needs_null_check */ false);
6213 } else {
6214 codegen_->GenerateArrayLoadWithBakerReadBarrier(
6215 instruction, out_loc, obj, data_offset, index, temp, /* needs_null_check */ false);
6216 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006217 } else {
6218 vixl32::Register out = OutputRegister(instruction);
6219 if (index.IsConstant()) {
6220 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00006221 (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006222 GetAssembler()->LoadFromOffset(kLoadWord, out, obj, offset);
Alexandre Rames374ddf32016-11-04 10:40:49 +00006223 // TODO(VIXL): Here and for other calls to `MaybeRecordImplicitNullCheck` in this method,
6224 // we should use a scope and the assembler to emit the load instruction to guarantee that
6225 // we record the pc at the correct position. But the `Assembler` does not automatically
6226 // handle unencodable offsets. Practically, everything is fine because the helper and
6227 // VIXL, at the time of writing, do generate the store instruction last.
Scott Wakelingc34dba72016-10-03 10:14:44 +01006228 codegen_->MaybeRecordImplicitNullCheck(instruction);
6229 // If read barriers are enabled, emit read barriers other than
6230 // Baker's using a slow path (and also unpoison the loaded
6231 // reference, if heap poisoning is enabled).
6232 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
6233 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006234 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006235 vixl32::Register temp = temps.Acquire();
6236
6237 if (has_intermediate_address) {
Artem Serov2bbc9532016-10-21 11:51:50 +01006238 // We do not need to compute the intermediate address from the array: the
6239 // input instruction has done it already. See the comment in
6240 // `TryExtractArrayAccessAddress()`.
6241 if (kIsDebugBuild) {
6242 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
Anton Kirilov644032c2016-12-06 17:51:43 +00006243 DCHECK_EQ(Uint64ConstantFrom(tmp->GetOffset()), data_offset);
Artem Serov2bbc9532016-10-21 11:51:50 +01006244 }
6245 temp = obj;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006246 } else {
6247 __ Add(temp, obj, data_offset);
6248 }
6249 codegen_->LoadFromShiftedRegOffset(type, out_loc, temp, RegisterFrom(index));
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006250 temps.Close();
Alexandre Rames374ddf32016-11-04 10:40:49 +00006251 // TODO(VIXL): Use a scope to ensure that we record the pc position immediately after the
6252 // load instruction. Practically, everything is fine because the helper and VIXL, at the
6253 // time of writing, do generate the store instruction last.
Scott Wakelingc34dba72016-10-03 10:14:44 +01006254 codegen_->MaybeRecordImplicitNullCheck(instruction);
6255 // If read barriers are enabled, emit read barriers other than
6256 // Baker's using a slow path (and also unpoison the loaded
6257 // reference, if heap poisoning is enabled).
6258 codegen_->MaybeGenerateReadBarrierSlow(
6259 instruction, out_loc, out_loc, obj_loc, data_offset, index);
6260 }
6261 }
6262 break;
6263 }
6264
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006265 case DataType::Type::kInt64: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006266 if (index.IsConstant()) {
6267 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00006268 (Int32ConstantFrom(index) << TIMES_8) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006269 GetAssembler()->LoadFromOffset(kLoadWordPair, LowRegisterFrom(out_loc), obj, offset);
6270 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006271 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006272 vixl32::Register temp = temps.Acquire();
6273 __ Add(temp, obj, Operand(RegisterFrom(index), vixl32::LSL, TIMES_8));
6274 GetAssembler()->LoadFromOffset(kLoadWordPair, LowRegisterFrom(out_loc), temp, data_offset);
6275 }
6276 break;
6277 }
6278
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006279 case DataType::Type::kFloat32: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006280 vixl32::SRegister out = SRegisterFrom(out_loc);
6281 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006282 size_t offset = (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006283 GetAssembler()->LoadSFromOffset(out, obj, offset);
6284 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006285 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006286 vixl32::Register temp = temps.Acquire();
6287 __ Add(temp, obj, Operand(RegisterFrom(index), vixl32::LSL, TIMES_4));
6288 GetAssembler()->LoadSFromOffset(out, temp, data_offset);
6289 }
6290 break;
6291 }
6292
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006293 case DataType::Type::kFloat64: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006294 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006295 size_t offset = (Int32ConstantFrom(index) << TIMES_8) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006296 GetAssembler()->LoadDFromOffset(DRegisterFrom(out_loc), obj, offset);
6297 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006298 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006299 vixl32::Register temp = temps.Acquire();
6300 __ Add(temp, obj, Operand(RegisterFrom(index), vixl32::LSL, TIMES_8));
6301 GetAssembler()->LoadDFromOffset(DRegisterFrom(out_loc), temp, data_offset);
6302 }
6303 break;
6304 }
6305
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006306 case DataType::Type::kVoid:
Scott Wakelingc34dba72016-10-03 10:14:44 +01006307 LOG(FATAL) << "Unreachable type " << type;
6308 UNREACHABLE();
6309 }
6310
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006311 if (type == DataType::Type::kReference) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006312 // Potential implicit null checks, in the case of reference
6313 // arrays, are handled in the previous switch statement.
6314 } else if (!maybe_compressed_char_at) {
Alexandre Rames374ddf32016-11-04 10:40:49 +00006315 // TODO(VIXL): Use a scope to ensure we record the pc info immediately after
6316 // the preceding load instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01006317 codegen_->MaybeRecordImplicitNullCheck(instruction);
6318 }
6319}
6320
6321void LocationsBuilderARMVIXL::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006322 DataType::Type value_type = instruction->GetComponentType();
Scott Wakelingc34dba72016-10-03 10:14:44 +01006323
6324 bool needs_write_barrier =
6325 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
6326 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
6327
Vladimir Markoca6fff82017-10-03 14:49:14 +01006328 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Scott Wakelingc34dba72016-10-03 10:14:44 +01006329 instruction,
6330 may_need_runtime_call_for_type_check ?
6331 LocationSummary::kCallOnSlowPath :
6332 LocationSummary::kNoCall);
6333
6334 locations->SetInAt(0, Location::RequiresRegister());
6335 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006336 if (DataType::IsFloatingPointType(value_type)) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006337 locations->SetInAt(2, Location::RequiresFpuRegister());
6338 } else {
6339 locations->SetInAt(2, Location::RequiresRegister());
6340 }
6341 if (needs_write_barrier) {
6342 // Temporary registers for the write barrier.
6343 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
6344 locations->AddTemp(Location::RequiresRegister());
6345 }
6346}
6347
6348void InstructionCodeGeneratorARMVIXL::VisitArraySet(HArraySet* instruction) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006349 LocationSummary* locations = instruction->GetLocations();
6350 vixl32::Register array = InputRegisterAt(instruction, 0);
6351 Location index = locations->InAt(1);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006352 DataType::Type value_type = instruction->GetComponentType();
Scott Wakelingc34dba72016-10-03 10:14:44 +01006353 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
6354 bool needs_write_barrier =
6355 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
6356 uint32_t data_offset =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006357 mirror::Array::DataOffset(DataType::Size(value_type)).Uint32Value();
Scott Wakelingc34dba72016-10-03 10:14:44 +01006358 Location value_loc = locations->InAt(2);
6359 HInstruction* array_instr = instruction->GetArray();
6360 bool has_intermediate_address = array_instr->IsIntermediateAddress();
Scott Wakelingc34dba72016-10-03 10:14:44 +01006361
6362 switch (value_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006363 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006364 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006365 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006366 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006367 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006368 case DataType::Type::kInt32: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006369 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006370 int32_t const_index = Int32ConstantFrom(index);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006371 uint32_t full_offset =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006372 data_offset + (const_index << DataType::SizeShift(value_type));
Scott Wakelingc34dba72016-10-03 10:14:44 +01006373 StoreOperandType store_type = GetStoreOperandType(value_type);
6374 GetAssembler()->StoreToOffset(store_type, RegisterFrom(value_loc), array, full_offset);
6375 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006376 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006377 vixl32::Register temp = temps.Acquire();
6378
6379 if (has_intermediate_address) {
Artem Serov2bbc9532016-10-21 11:51:50 +01006380 // We do not need to compute the intermediate address from the array: the
6381 // input instruction has done it already. See the comment in
6382 // `TryExtractArrayAccessAddress()`.
6383 if (kIsDebugBuild) {
6384 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
Anton Kirilov644032c2016-12-06 17:51:43 +00006385 DCHECK_EQ(Uint64ConstantFrom(tmp->GetOffset()), data_offset);
Artem Serov2bbc9532016-10-21 11:51:50 +01006386 }
6387 temp = array;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006388 } else {
6389 __ Add(temp, array, data_offset);
6390 }
6391 codegen_->StoreToShiftedRegOffset(value_type, value_loc, temp, RegisterFrom(index));
6392 }
6393 break;
6394 }
6395
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006396 case DataType::Type::kReference: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006397 vixl32::Register value = RegisterFrom(value_loc);
6398 // TryExtractArrayAccessAddress optimization is never applied for non-primitive ArraySet.
6399 // See the comment in instruction_simplifier_shared.cc.
6400 DCHECK(!has_intermediate_address);
6401
6402 if (instruction->InputAt(2)->IsNullConstant()) {
6403 // Just setting null.
6404 if (index.IsConstant()) {
6405 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00006406 (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006407 GetAssembler()->StoreToOffset(kStoreWord, value, array, offset);
6408 } else {
6409 DCHECK(index.IsRegister()) << index;
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006410 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006411 vixl32::Register temp = temps.Acquire();
6412 __ Add(temp, array, data_offset);
6413 codegen_->StoreToShiftedRegOffset(value_type, value_loc, temp, RegisterFrom(index));
6414 }
Alexandre Rames374ddf32016-11-04 10:40:49 +00006415 // TODO(VIXL): Use a scope to ensure we record the pc info immediately after the preceding
6416 // store instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01006417 codegen_->MaybeRecordImplicitNullCheck(instruction);
6418 DCHECK(!needs_write_barrier);
6419 DCHECK(!may_need_runtime_call_for_type_check);
6420 break;
6421 }
6422
6423 DCHECK(needs_write_barrier);
6424 Location temp1_loc = locations->GetTemp(0);
6425 vixl32::Register temp1 = RegisterFrom(temp1_loc);
6426 Location temp2_loc = locations->GetTemp(1);
6427 vixl32::Register temp2 = RegisterFrom(temp2_loc);
6428 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6429 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6430 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6431 vixl32::Label done;
Anton Kirilov6f644202017-02-27 18:29:45 +00006432 vixl32::Label* final_label = codegen_->GetFinalLabel(instruction, &done);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006433 SlowPathCodeARMVIXL* slow_path = nullptr;
6434
6435 if (may_need_runtime_call_for_type_check) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01006436 slow_path = new (codegen_->GetScopedAllocator()) ArraySetSlowPathARMVIXL(instruction);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006437 codegen_->AddSlowPath(slow_path);
6438 if (instruction->GetValueCanBeNull()) {
6439 vixl32::Label non_zero;
xueliang.zhongf51bc622016-11-04 09:23:32 +00006440 __ CompareAndBranchIfNonZero(value, &non_zero);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006441 if (index.IsConstant()) {
6442 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00006443 (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006444 GetAssembler()->StoreToOffset(kStoreWord, value, array, offset);
6445 } else {
6446 DCHECK(index.IsRegister()) << index;
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006447 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006448 vixl32::Register temp = temps.Acquire();
6449 __ Add(temp, array, data_offset);
6450 codegen_->StoreToShiftedRegOffset(value_type, value_loc, temp, RegisterFrom(index));
6451 }
Alexandre Rames374ddf32016-11-04 10:40:49 +00006452 // TODO(VIXL): Use a scope to ensure we record the pc info immediately after the preceding
6453 // store instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01006454 codegen_->MaybeRecordImplicitNullCheck(instruction);
Anton Kirilov6f644202017-02-27 18:29:45 +00006455 __ B(final_label);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006456 __ Bind(&non_zero);
6457 }
6458
6459 // Note that when read barriers are enabled, the type checks
6460 // are performed without read barriers. This is fine, even in
6461 // the case where a class object is in the from-space after
6462 // the flip, as a comparison involving such a type would not
6463 // produce a false positive; it may of course produce a false
6464 // negative, in which case we would take the ArraySet slow
6465 // path.
6466
Alexandre Rames374ddf32016-11-04 10:40:49 +00006467 {
6468 // Ensure we record the pc position immediately after the `ldr` instruction.
Artem Serov0fb37192016-12-06 18:13:40 +00006469 ExactAssemblyScope aas(GetVIXLAssembler(),
6470 vixl32::kMaxInstructionSizeInBytes,
6471 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00006472 // /* HeapReference<Class> */ temp1 = array->klass_
6473 __ ldr(temp1, MemOperand(array, class_offset));
6474 codegen_->MaybeRecordImplicitNullCheck(instruction);
6475 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006476 GetAssembler()->MaybeUnpoisonHeapReference(temp1);
6477
6478 // /* HeapReference<Class> */ temp1 = temp1->component_type_
6479 GetAssembler()->LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
6480 // /* HeapReference<Class> */ temp2 = value->klass_
6481 GetAssembler()->LoadFromOffset(kLoadWord, temp2, value, class_offset);
6482 // If heap poisoning is enabled, no need to unpoison `temp1`
6483 // nor `temp2`, as we are comparing two poisoned references.
6484 __ Cmp(temp1, temp2);
6485
6486 if (instruction->StaticTypeOfArrayIsObjectArray()) {
6487 vixl32::Label do_put;
Artem Serov517d9f62016-12-12 15:51:15 +00006488 __ B(eq, &do_put, /* far_target */ false);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006489 // If heap poisoning is enabled, the `temp1` reference has
6490 // not been unpoisoned yet; unpoison it now.
6491 GetAssembler()->MaybeUnpoisonHeapReference(temp1);
6492
6493 // /* HeapReference<Class> */ temp1 = temp1->super_class_
6494 GetAssembler()->LoadFromOffset(kLoadWord, temp1, temp1, super_offset);
6495 // If heap poisoning is enabled, no need to unpoison
6496 // `temp1`, as we are comparing against null below.
xueliang.zhongf51bc622016-11-04 09:23:32 +00006497 __ CompareAndBranchIfNonZero(temp1, slow_path->GetEntryLabel());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006498 __ Bind(&do_put);
6499 } else {
6500 __ B(ne, slow_path->GetEntryLabel());
6501 }
6502 }
6503
6504 vixl32::Register source = value;
6505 if (kPoisonHeapReferences) {
6506 // Note that in the case where `value` is a null reference,
6507 // we do not enter this block, as a null reference does not
6508 // need poisoning.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006509 DCHECK_EQ(value_type, DataType::Type::kReference);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006510 __ Mov(temp1, value);
6511 GetAssembler()->PoisonHeapReference(temp1);
6512 source = temp1;
6513 }
6514
6515 if (index.IsConstant()) {
6516 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00006517 (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006518 GetAssembler()->StoreToOffset(kStoreWord, source, array, offset);
6519 } else {
6520 DCHECK(index.IsRegister()) << index;
6521
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006522 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006523 vixl32::Register temp = temps.Acquire();
6524 __ Add(temp, array, data_offset);
6525 codegen_->StoreToShiftedRegOffset(value_type,
6526 LocationFrom(source),
6527 temp,
6528 RegisterFrom(index));
6529 }
6530
6531 if (!may_need_runtime_call_for_type_check) {
Alexandre Rames374ddf32016-11-04 10:40:49 +00006532 // TODO(VIXL): Ensure we record the pc position immediately after the preceding store
6533 // instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01006534 codegen_->MaybeRecordImplicitNullCheck(instruction);
6535 }
6536
6537 codegen_->MarkGCCard(temp1, temp2, array, value, instruction->GetValueCanBeNull());
6538
6539 if (done.IsReferenced()) {
6540 __ Bind(&done);
6541 }
6542
6543 if (slow_path != nullptr) {
6544 __ Bind(slow_path->GetExitLabel());
6545 }
6546
6547 break;
6548 }
6549
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006550 case DataType::Type::kInt64: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006551 Location value = locations->InAt(2);
6552 if (index.IsConstant()) {
6553 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00006554 (Int32ConstantFrom(index) << TIMES_8) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006555 GetAssembler()->StoreToOffset(kStoreWordPair, LowRegisterFrom(value), array, offset);
6556 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006557 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006558 vixl32::Register temp = temps.Acquire();
6559 __ Add(temp, array, Operand(RegisterFrom(index), vixl32::LSL, TIMES_8));
6560 GetAssembler()->StoreToOffset(kStoreWordPair, LowRegisterFrom(value), temp, data_offset);
6561 }
6562 break;
6563 }
6564
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006565 case DataType::Type::kFloat32: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006566 Location value = locations->InAt(2);
6567 DCHECK(value.IsFpuRegister());
6568 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006569 size_t offset = (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006570 GetAssembler()->StoreSToOffset(SRegisterFrom(value), array, offset);
6571 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006572 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006573 vixl32::Register temp = temps.Acquire();
6574 __ Add(temp, array, Operand(RegisterFrom(index), vixl32::LSL, TIMES_4));
6575 GetAssembler()->StoreSToOffset(SRegisterFrom(value), temp, data_offset);
6576 }
6577 break;
6578 }
6579
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006580 case DataType::Type::kFloat64: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006581 Location value = locations->InAt(2);
6582 DCHECK(value.IsFpuRegisterPair());
6583 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006584 size_t offset = (Int32ConstantFrom(index) << TIMES_8) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006585 GetAssembler()->StoreDToOffset(DRegisterFrom(value), array, offset);
6586 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006587 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006588 vixl32::Register temp = temps.Acquire();
6589 __ Add(temp, array, Operand(RegisterFrom(index), vixl32::LSL, TIMES_8));
6590 GetAssembler()->StoreDToOffset(DRegisterFrom(value), temp, data_offset);
6591 }
6592 break;
6593 }
6594
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006595 case DataType::Type::kVoid:
Scott Wakelingc34dba72016-10-03 10:14:44 +01006596 LOG(FATAL) << "Unreachable type " << value_type;
6597 UNREACHABLE();
6598 }
6599
6600 // Objects are handled in the switch.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006601 if (value_type != DataType::Type::kReference) {
Alexandre Rames374ddf32016-11-04 10:40:49 +00006602 // TODO(VIXL): Ensure we record the pc position immediately after the preceding store
6603 // instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01006604 codegen_->MaybeRecordImplicitNullCheck(instruction);
6605 }
6606}
6607
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006608void LocationsBuilderARMVIXL::VisitArrayLength(HArrayLength* instruction) {
6609 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006610 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006611 locations->SetInAt(0, Location::RequiresRegister());
6612 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6613}
6614
6615void InstructionCodeGeneratorARMVIXL::VisitArrayLength(HArrayLength* instruction) {
6616 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
6617 vixl32::Register obj = InputRegisterAt(instruction, 0);
6618 vixl32::Register out = OutputRegister(instruction);
Alexandre Rames374ddf32016-11-04 10:40:49 +00006619 {
Artem Serov0fb37192016-12-06 18:13:40 +00006620 ExactAssemblyScope aas(GetVIXLAssembler(),
6621 vixl32::kMaxInstructionSizeInBytes,
6622 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00006623 __ ldr(out, MemOperand(obj, offset));
6624 codegen_->MaybeRecordImplicitNullCheck(instruction);
6625 }
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006626 // Mask out compression flag from String's array length.
6627 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006628 __ Lsr(out, out, 1u);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006629 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006630}
6631
Artem Serov2bbc9532016-10-21 11:51:50 +01006632void LocationsBuilderARMVIXL::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Artem Serov2bbc9532016-10-21 11:51:50 +01006633 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006634 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Artem Serov2bbc9532016-10-21 11:51:50 +01006635
6636 locations->SetInAt(0, Location::RequiresRegister());
6637 locations->SetInAt(1, Location::RegisterOrConstant(instruction->GetOffset()));
6638 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6639}
6640
6641void InstructionCodeGeneratorARMVIXL::VisitIntermediateAddress(HIntermediateAddress* instruction) {
6642 vixl32::Register out = OutputRegister(instruction);
6643 vixl32::Register first = InputRegisterAt(instruction, 0);
6644 Location second = instruction->GetLocations()->InAt(1);
6645
Artem Serov2bbc9532016-10-21 11:51:50 +01006646 if (second.IsRegister()) {
6647 __ Add(out, first, RegisterFrom(second));
6648 } else {
Anton Kirilov644032c2016-12-06 17:51:43 +00006649 __ Add(out, first, Int32ConstantFrom(second));
Artem Serov2bbc9532016-10-21 11:51:50 +01006650 }
6651}
6652
Artem Serove1811ed2017-04-27 16:50:47 +01006653void LocationsBuilderARMVIXL::VisitIntermediateAddressIndex(
6654 HIntermediateAddressIndex* instruction) {
6655 LOG(FATAL) << "Unreachable " << instruction->GetId();
6656}
6657
6658void InstructionCodeGeneratorARMVIXL::VisitIntermediateAddressIndex(
6659 HIntermediateAddressIndex* instruction) {
6660 LOG(FATAL) << "Unreachable " << instruction->GetId();
6661}
6662
Scott Wakelingc34dba72016-10-03 10:14:44 +01006663void LocationsBuilderARMVIXL::VisitBoundsCheck(HBoundsCheck* instruction) {
6664 RegisterSet caller_saves = RegisterSet::Empty();
6665 InvokeRuntimeCallingConventionARMVIXL calling_convention;
6666 caller_saves.Add(LocationFrom(calling_convention.GetRegisterAt(0)));
6667 caller_saves.Add(LocationFrom(calling_convention.GetRegisterAt(1)));
6668 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Artem Serov2dd053d2017-03-08 14:54:06 +00006669
6670 HInstruction* index = instruction->InputAt(0);
6671 HInstruction* length = instruction->InputAt(1);
6672 // If both index and length are constants we can statically check the bounds. But if at least one
6673 // of them is not encodable ArmEncodableConstantOrRegister will create
6674 // Location::RequiresRegister() which is not desired to happen. Instead we create constant
6675 // locations.
6676 bool both_const = index->IsConstant() && length->IsConstant();
6677 locations->SetInAt(0, both_const
6678 ? Location::ConstantLocation(index->AsConstant())
6679 : ArmEncodableConstantOrRegister(index, CMP));
6680 locations->SetInAt(1, both_const
6681 ? Location::ConstantLocation(length->AsConstant())
6682 : ArmEncodableConstantOrRegister(length, CMP));
Scott Wakelingc34dba72016-10-03 10:14:44 +01006683}
6684
6685void InstructionCodeGeneratorARMVIXL::VisitBoundsCheck(HBoundsCheck* instruction) {
Artem Serov2dd053d2017-03-08 14:54:06 +00006686 LocationSummary* locations = instruction->GetLocations();
6687 Location index_loc = locations->InAt(0);
6688 Location length_loc = locations->InAt(1);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006689
Artem Serov2dd053d2017-03-08 14:54:06 +00006690 if (length_loc.IsConstant()) {
6691 int32_t length = Int32ConstantFrom(length_loc);
6692 if (index_loc.IsConstant()) {
6693 // BCE will remove the bounds check if we are guaranteed to pass.
6694 int32_t index = Int32ConstantFrom(index_loc);
6695 if (index < 0 || index >= length) {
6696 SlowPathCodeARMVIXL* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006697 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathARMVIXL(instruction);
Artem Serov2dd053d2017-03-08 14:54:06 +00006698 codegen_->AddSlowPath(slow_path);
6699 __ B(slow_path->GetEntryLabel());
6700 } else {
6701 // Some optimization after BCE may have generated this, and we should not
6702 // generate a bounds check if it is a valid range.
6703 }
6704 return;
6705 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006706
Artem Serov2dd053d2017-03-08 14:54:06 +00006707 SlowPathCodeARMVIXL* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006708 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathARMVIXL(instruction);
Artem Serov2dd053d2017-03-08 14:54:06 +00006709 __ Cmp(RegisterFrom(index_loc), length);
6710 codegen_->AddSlowPath(slow_path);
6711 __ B(hs, slow_path->GetEntryLabel());
6712 } else {
6713 SlowPathCodeARMVIXL* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006714 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathARMVIXL(instruction);
Artem Serov2dd053d2017-03-08 14:54:06 +00006715 __ Cmp(RegisterFrom(length_loc), InputOperandAt(instruction, 0));
6716 codegen_->AddSlowPath(slow_path);
6717 __ B(ls, slow_path->GetEntryLabel());
6718 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006719}
6720
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006721void CodeGeneratorARMVIXL::MarkGCCard(vixl32::Register temp,
6722 vixl32::Register card,
6723 vixl32::Register object,
6724 vixl32::Register value,
6725 bool can_be_null) {
6726 vixl32::Label is_null;
6727 if (can_be_null) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00006728 __ CompareAndBranchIfZero(value, &is_null);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006729 }
6730 GetAssembler()->LoadFromOffset(
6731 kLoadWord, card, tr, Thread::CardTableOffset<kArmPointerSize>().Int32Value());
Scott Wakelingb77051e2016-11-21 19:46:00 +00006732 __ Lsr(temp, object, Operand::From(gc::accounting::CardTable::kCardShift));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006733 __ Strb(card, MemOperand(card, temp));
6734 if (can_be_null) {
6735 __ Bind(&is_null);
6736 }
6737}
6738
Scott Wakelingfe885462016-09-22 10:24:38 +01006739void LocationsBuilderARMVIXL::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
6740 LOG(FATAL) << "Unreachable";
6741}
6742
6743void InstructionCodeGeneratorARMVIXL::VisitParallelMove(HParallelMove* instruction) {
Vladimir Markobea75ff2017-10-11 20:39:54 +01006744 if (instruction->GetNext()->IsSuspendCheck() &&
6745 instruction->GetBlock()->GetLoopInformation() != nullptr) {
6746 HSuspendCheck* suspend_check = instruction->GetNext()->AsSuspendCheck();
6747 // The back edge will generate the suspend check.
6748 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(suspend_check, instruction);
6749 }
6750
Scott Wakelingfe885462016-09-22 10:24:38 +01006751 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
6752}
6753
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006754void LocationsBuilderARMVIXL::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006755 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6756 instruction, LocationSummary::kCallOnSlowPath);
Artem Serov657022c2016-11-23 14:19:38 +00006757 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006758}
6759
6760void InstructionCodeGeneratorARMVIXL::VisitSuspendCheck(HSuspendCheck* instruction) {
6761 HBasicBlock* block = instruction->GetBlock();
6762 if (block->GetLoopInformation() != nullptr) {
6763 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
6764 // The back edge will generate the suspend check.
6765 return;
6766 }
6767 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
6768 // The goto will generate the suspend check.
6769 return;
6770 }
6771 GenerateSuspendCheck(instruction, nullptr);
Roland Levillain5daa4952017-07-03 17:23:56 +01006772 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 12);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006773}
6774
6775void InstructionCodeGeneratorARMVIXL::GenerateSuspendCheck(HSuspendCheck* instruction,
6776 HBasicBlock* successor) {
6777 SuspendCheckSlowPathARMVIXL* slow_path =
6778 down_cast<SuspendCheckSlowPathARMVIXL*>(instruction->GetSlowPath());
6779 if (slow_path == nullptr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006780 slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006781 new (codegen_->GetScopedAllocator()) SuspendCheckSlowPathARMVIXL(instruction, successor);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006782 instruction->SetSlowPath(slow_path);
6783 codegen_->AddSlowPath(slow_path);
6784 if (successor != nullptr) {
6785 DCHECK(successor->IsLoopHeader());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006786 }
6787 } else {
6788 DCHECK_EQ(slow_path->GetSuccessor(), successor);
6789 }
6790
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006791 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006792 vixl32::Register temp = temps.Acquire();
6793 GetAssembler()->LoadFromOffset(
6794 kLoadUnsignedHalfword, temp, tr, Thread::ThreadFlagsOffset<kArmPointerSize>().Int32Value());
6795 if (successor == nullptr) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00006796 __ CompareAndBranchIfNonZero(temp, slow_path->GetEntryLabel());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006797 __ Bind(slow_path->GetReturnLabel());
6798 } else {
xueliang.zhongf51bc622016-11-04 09:23:32 +00006799 __ CompareAndBranchIfZero(temp, codegen_->GetLabelOf(successor));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006800 __ B(slow_path->GetEntryLabel());
6801 }
6802}
6803
Scott Wakelingfe885462016-09-22 10:24:38 +01006804ArmVIXLAssembler* ParallelMoveResolverARMVIXL::GetAssembler() const {
6805 return codegen_->GetAssembler();
6806}
6807
6808void ParallelMoveResolverARMVIXL::EmitMove(size_t index) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006809 UseScratchRegisterScope temps(GetAssembler()->GetVIXLAssembler());
Scott Wakelingfe885462016-09-22 10:24:38 +01006810 MoveOperands* move = moves_[index];
6811 Location source = move->GetSource();
6812 Location destination = move->GetDestination();
6813
6814 if (source.IsRegister()) {
6815 if (destination.IsRegister()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006816 __ Mov(RegisterFrom(destination), RegisterFrom(source));
Scott Wakelingfe885462016-09-22 10:24:38 +01006817 } else if (destination.IsFpuRegister()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006818 __ Vmov(SRegisterFrom(destination), RegisterFrom(source));
Scott Wakelingfe885462016-09-22 10:24:38 +01006819 } else {
6820 DCHECK(destination.IsStackSlot());
6821 GetAssembler()->StoreToOffset(kStoreWord,
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006822 RegisterFrom(source),
Scott Wakelingfe885462016-09-22 10:24:38 +01006823 sp,
6824 destination.GetStackIndex());
6825 }
6826 } else if (source.IsStackSlot()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006827 if (destination.IsRegister()) {
6828 GetAssembler()->LoadFromOffset(kLoadWord,
6829 RegisterFrom(destination),
6830 sp,
6831 source.GetStackIndex());
6832 } else if (destination.IsFpuRegister()) {
6833 GetAssembler()->LoadSFromOffset(SRegisterFrom(destination), sp, source.GetStackIndex());
6834 } else {
6835 DCHECK(destination.IsStackSlot());
6836 vixl32::Register temp = temps.Acquire();
6837 GetAssembler()->LoadFromOffset(kLoadWord, temp, sp, source.GetStackIndex());
6838 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
6839 }
Scott Wakelingfe885462016-09-22 10:24:38 +01006840 } else if (source.IsFpuRegister()) {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01006841 if (destination.IsRegister()) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006842 __ Vmov(RegisterFrom(destination), SRegisterFrom(source));
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01006843 } else if (destination.IsFpuRegister()) {
6844 __ Vmov(SRegisterFrom(destination), SRegisterFrom(source));
6845 } else {
6846 DCHECK(destination.IsStackSlot());
6847 GetAssembler()->StoreSToOffset(SRegisterFrom(source), sp, destination.GetStackIndex());
6848 }
Scott Wakelingfe885462016-09-22 10:24:38 +01006849 } else if (source.IsDoubleStackSlot()) {
Alexandre Rames9c19bd62016-10-24 11:50:32 +01006850 if (destination.IsDoubleStackSlot()) {
6851 vixl32::DRegister temp = temps.AcquireD();
6852 GetAssembler()->LoadDFromOffset(temp, sp, source.GetStackIndex());
6853 GetAssembler()->StoreDToOffset(temp, sp, destination.GetStackIndex());
6854 } else if (destination.IsRegisterPair()) {
6855 DCHECK(ExpectedPairLayout(destination));
6856 GetAssembler()->LoadFromOffset(
6857 kLoadWordPair, LowRegisterFrom(destination), sp, source.GetStackIndex());
6858 } else {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01006859 DCHECK(destination.IsFpuRegisterPair()) << destination;
6860 GetAssembler()->LoadDFromOffset(DRegisterFrom(destination), sp, source.GetStackIndex());
Alexandre Rames9c19bd62016-10-24 11:50:32 +01006861 }
Scott Wakelingfe885462016-09-22 10:24:38 +01006862 } else if (source.IsRegisterPair()) {
6863 if (destination.IsRegisterPair()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006864 __ Mov(LowRegisterFrom(destination), LowRegisterFrom(source));
6865 __ Mov(HighRegisterFrom(destination), HighRegisterFrom(source));
Scott Wakelingfe885462016-09-22 10:24:38 +01006866 } else if (destination.IsFpuRegisterPair()) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006867 __ Vmov(DRegisterFrom(destination), LowRegisterFrom(source), HighRegisterFrom(source));
Scott Wakelingfe885462016-09-22 10:24:38 +01006868 } else {
6869 DCHECK(destination.IsDoubleStackSlot()) << destination;
6870 DCHECK(ExpectedPairLayout(source));
6871 GetAssembler()->StoreToOffset(kStoreWordPair,
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006872 LowRegisterFrom(source),
Scott Wakelingfe885462016-09-22 10:24:38 +01006873 sp,
6874 destination.GetStackIndex());
6875 }
6876 } else if (source.IsFpuRegisterPair()) {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01006877 if (destination.IsRegisterPair()) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006878 __ Vmov(LowRegisterFrom(destination), HighRegisterFrom(destination), DRegisterFrom(source));
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01006879 } else if (destination.IsFpuRegisterPair()) {
6880 __ Vmov(DRegisterFrom(destination), DRegisterFrom(source));
6881 } else {
6882 DCHECK(destination.IsDoubleStackSlot()) << destination;
6883 GetAssembler()->StoreDToOffset(DRegisterFrom(source), sp, destination.GetStackIndex());
6884 }
Scott Wakelingfe885462016-09-22 10:24:38 +01006885 } else {
6886 DCHECK(source.IsConstant()) << source;
6887 HConstant* constant = source.GetConstant();
6888 if (constant->IsIntConstant() || constant->IsNullConstant()) {
6889 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
6890 if (destination.IsRegister()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006891 __ Mov(RegisterFrom(destination), value);
Scott Wakelingfe885462016-09-22 10:24:38 +01006892 } else {
6893 DCHECK(destination.IsStackSlot());
Scott Wakelingfe885462016-09-22 10:24:38 +01006894 vixl32::Register temp = temps.Acquire();
6895 __ Mov(temp, value);
6896 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
6897 }
6898 } else if (constant->IsLongConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006899 int64_t value = Int64ConstantFrom(source);
Scott Wakelingfe885462016-09-22 10:24:38 +01006900 if (destination.IsRegisterPair()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006901 __ Mov(LowRegisterFrom(destination), Low32Bits(value));
6902 __ Mov(HighRegisterFrom(destination), High32Bits(value));
Scott Wakelingfe885462016-09-22 10:24:38 +01006903 } else {
6904 DCHECK(destination.IsDoubleStackSlot()) << destination;
Scott Wakelingfe885462016-09-22 10:24:38 +01006905 vixl32::Register temp = temps.Acquire();
6906 __ Mov(temp, Low32Bits(value));
6907 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
6908 __ Mov(temp, High32Bits(value));
6909 GetAssembler()->StoreToOffset(kStoreWord,
6910 temp,
6911 sp,
6912 destination.GetHighStackIndex(kArmWordSize));
6913 }
6914 } else if (constant->IsDoubleConstant()) {
6915 double value = constant->AsDoubleConstant()->GetValue();
6916 if (destination.IsFpuRegisterPair()) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006917 __ Vmov(DRegisterFrom(destination), value);
Scott Wakelingfe885462016-09-22 10:24:38 +01006918 } else {
6919 DCHECK(destination.IsDoubleStackSlot()) << destination;
6920 uint64_t int_value = bit_cast<uint64_t, double>(value);
Scott Wakelingfe885462016-09-22 10:24:38 +01006921 vixl32::Register temp = temps.Acquire();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006922 __ Mov(temp, Low32Bits(int_value));
Scott Wakelingfe885462016-09-22 10:24:38 +01006923 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006924 __ Mov(temp, High32Bits(int_value));
Scott Wakelingfe885462016-09-22 10:24:38 +01006925 GetAssembler()->StoreToOffset(kStoreWord,
6926 temp,
6927 sp,
6928 destination.GetHighStackIndex(kArmWordSize));
6929 }
6930 } else {
6931 DCHECK(constant->IsFloatConstant()) << constant->DebugName();
6932 float value = constant->AsFloatConstant()->GetValue();
6933 if (destination.IsFpuRegister()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006934 __ Vmov(SRegisterFrom(destination), value);
Scott Wakelingfe885462016-09-22 10:24:38 +01006935 } else {
6936 DCHECK(destination.IsStackSlot());
Scott Wakelingfe885462016-09-22 10:24:38 +01006937 vixl32::Register temp = temps.Acquire();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006938 __ Mov(temp, bit_cast<int32_t, float>(value));
Scott Wakelingfe885462016-09-22 10:24:38 +01006939 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
6940 }
6941 }
6942 }
6943}
6944
Alexandre Rames9c19bd62016-10-24 11:50:32 +01006945void ParallelMoveResolverARMVIXL::Exchange(vixl32::Register reg, int mem) {
6946 UseScratchRegisterScope temps(GetAssembler()->GetVIXLAssembler());
6947 vixl32::Register temp = temps.Acquire();
6948 __ Mov(temp, reg);
6949 GetAssembler()->LoadFromOffset(kLoadWord, reg, sp, mem);
6950 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, mem);
Scott Wakelingfe885462016-09-22 10:24:38 +01006951}
6952
Alexandre Rames9c19bd62016-10-24 11:50:32 +01006953void ParallelMoveResolverARMVIXL::Exchange(int mem1, int mem2) {
6954 // TODO(VIXL32): Double check the performance of this implementation.
6955 UseScratchRegisterScope temps(GetAssembler()->GetVIXLAssembler());
Nicolas Geoffray13a797b2017-03-15 16:41:31 +00006956 vixl32::Register temp1 = temps.Acquire();
6957 ScratchRegisterScope ensure_scratch(
6958 this, temp1.GetCode(), r0.GetCode(), codegen_->GetNumberOfCoreRegisters());
6959 vixl32::Register temp2(ensure_scratch.GetRegister());
Alexandre Rames9c19bd62016-10-24 11:50:32 +01006960
Nicolas Geoffray13a797b2017-03-15 16:41:31 +00006961 int stack_offset = ensure_scratch.IsSpilled() ? kArmWordSize : 0;
6962 GetAssembler()->LoadFromOffset(kLoadWord, temp1, sp, mem1 + stack_offset);
6963 GetAssembler()->LoadFromOffset(kLoadWord, temp2, sp, mem2 + stack_offset);
6964 GetAssembler()->StoreToOffset(kStoreWord, temp1, sp, mem2 + stack_offset);
6965 GetAssembler()->StoreToOffset(kStoreWord, temp2, sp, mem1 + stack_offset);
Scott Wakelingfe885462016-09-22 10:24:38 +01006966}
6967
Alexandre Rames9c19bd62016-10-24 11:50:32 +01006968void ParallelMoveResolverARMVIXL::EmitSwap(size_t index) {
6969 MoveOperands* move = moves_[index];
6970 Location source = move->GetSource();
6971 Location destination = move->GetDestination();
6972 UseScratchRegisterScope temps(GetAssembler()->GetVIXLAssembler());
6973
6974 if (source.IsRegister() && destination.IsRegister()) {
6975 vixl32::Register temp = temps.Acquire();
6976 DCHECK(!RegisterFrom(source).Is(temp));
6977 DCHECK(!RegisterFrom(destination).Is(temp));
6978 __ Mov(temp, RegisterFrom(destination));
6979 __ Mov(RegisterFrom(destination), RegisterFrom(source));
6980 __ Mov(RegisterFrom(source), temp);
6981 } else if (source.IsRegister() && destination.IsStackSlot()) {
6982 Exchange(RegisterFrom(source), destination.GetStackIndex());
6983 } else if (source.IsStackSlot() && destination.IsRegister()) {
6984 Exchange(RegisterFrom(destination), source.GetStackIndex());
6985 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Anton Kirilovdda43962016-11-21 19:55:20 +00006986 Exchange(source.GetStackIndex(), destination.GetStackIndex());
Alexandre Rames9c19bd62016-10-24 11:50:32 +01006987 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Nicolas Geoffray13a797b2017-03-15 16:41:31 +00006988 vixl32::Register temp = temps.Acquire();
Anton Kirilovdda43962016-11-21 19:55:20 +00006989 __ Vmov(temp, SRegisterFrom(source));
6990 __ Vmov(SRegisterFrom(source), SRegisterFrom(destination));
6991 __ Vmov(SRegisterFrom(destination), temp);
Alexandre Rames9c19bd62016-10-24 11:50:32 +01006992 } else if (source.IsRegisterPair() && destination.IsRegisterPair()) {
6993 vixl32::DRegister temp = temps.AcquireD();
6994 __ Vmov(temp, LowRegisterFrom(source), HighRegisterFrom(source));
6995 __ Mov(LowRegisterFrom(source), LowRegisterFrom(destination));
6996 __ Mov(HighRegisterFrom(source), HighRegisterFrom(destination));
6997 __ Vmov(LowRegisterFrom(destination), HighRegisterFrom(destination), temp);
6998 } else if (source.IsRegisterPair() || destination.IsRegisterPair()) {
6999 vixl32::Register low_reg = LowRegisterFrom(source.IsRegisterPair() ? source : destination);
7000 int mem = source.IsRegisterPair() ? destination.GetStackIndex() : source.GetStackIndex();
7001 DCHECK(ExpectedPairLayout(source.IsRegisterPair() ? source : destination));
7002 vixl32::DRegister temp = temps.AcquireD();
7003 __ Vmov(temp, low_reg, vixl32::Register(low_reg.GetCode() + 1));
7004 GetAssembler()->LoadFromOffset(kLoadWordPair, low_reg, sp, mem);
7005 GetAssembler()->StoreDToOffset(temp, sp, mem);
7006 } else if (source.IsFpuRegisterPair() && destination.IsFpuRegisterPair()) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007007 vixl32::DRegister first = DRegisterFrom(source);
7008 vixl32::DRegister second = DRegisterFrom(destination);
7009 vixl32::DRegister temp = temps.AcquireD();
7010 __ Vmov(temp, first);
7011 __ Vmov(first, second);
7012 __ Vmov(second, temp);
Alexandre Rames9c19bd62016-10-24 11:50:32 +01007013 } else if (source.IsFpuRegisterPair() || destination.IsFpuRegisterPair()) {
Anton Kirilovdda43962016-11-21 19:55:20 +00007014 vixl32::DRegister reg = source.IsFpuRegisterPair()
7015 ? DRegisterFrom(source)
7016 : DRegisterFrom(destination);
7017 int mem = source.IsFpuRegisterPair()
7018 ? destination.GetStackIndex()
7019 : source.GetStackIndex();
7020 vixl32::DRegister temp = temps.AcquireD();
7021 __ Vmov(temp, reg);
7022 GetAssembler()->LoadDFromOffset(reg, sp, mem);
7023 GetAssembler()->StoreDToOffset(temp, sp, mem);
Alexandre Rames9c19bd62016-10-24 11:50:32 +01007024 } else if (source.IsFpuRegister() || destination.IsFpuRegister()) {
Anton Kirilovdda43962016-11-21 19:55:20 +00007025 vixl32::SRegister reg = source.IsFpuRegister()
7026 ? SRegisterFrom(source)
7027 : SRegisterFrom(destination);
7028 int mem = source.IsFpuRegister()
7029 ? destination.GetStackIndex()
7030 : source.GetStackIndex();
7031 vixl32::Register temp = temps.Acquire();
7032 __ Vmov(temp, reg);
7033 GetAssembler()->LoadSFromOffset(reg, sp, mem);
7034 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, mem);
Alexandre Rames9c19bd62016-10-24 11:50:32 +01007035 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
7036 vixl32::DRegister temp1 = temps.AcquireD();
7037 vixl32::DRegister temp2 = temps.AcquireD();
7038 __ Vldr(temp1, MemOperand(sp, source.GetStackIndex()));
7039 __ Vldr(temp2, MemOperand(sp, destination.GetStackIndex()));
7040 __ Vstr(temp1, MemOperand(sp, destination.GetStackIndex()));
7041 __ Vstr(temp2, MemOperand(sp, source.GetStackIndex()));
7042 } else {
7043 LOG(FATAL) << "Unimplemented" << source << " <-> " << destination;
7044 }
Scott Wakelingfe885462016-09-22 10:24:38 +01007045}
7046
Nicolas Geoffray13a797b2017-03-15 16:41:31 +00007047void ParallelMoveResolverARMVIXL::SpillScratch(int reg) {
7048 __ Push(vixl32::Register(reg));
Scott Wakelingfe885462016-09-22 10:24:38 +01007049}
7050
Nicolas Geoffray13a797b2017-03-15 16:41:31 +00007051void ParallelMoveResolverARMVIXL::RestoreScratch(int reg) {
7052 __ Pop(vixl32::Register(reg));
Scott Wakelingfe885462016-09-22 10:24:38 +01007053}
7054
Artem Serov02d37832016-10-25 15:25:33 +01007055HLoadClass::LoadKind CodeGeneratorARMVIXL::GetSupportedLoadClassKind(
Artem Serovd4cc5b22016-11-04 11:19:09 +00007056 HLoadClass::LoadKind desired_class_load_kind) {
7057 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00007058 case HLoadClass::LoadKind::kInvalid:
7059 LOG(FATAL) << "UNREACHABLE";
7060 UNREACHABLE();
Artem Serovd4cc5b22016-11-04 11:19:09 +00007061 case HLoadClass::LoadKind::kReferrersClass:
7062 break;
Artem Serovd4cc5b22016-11-04 11:19:09 +00007063 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko94ec2db2017-09-06 17:21:03 +01007064 case HLoadClass::LoadKind::kBootImageClassTable:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007065 case HLoadClass::LoadKind::kBssEntry:
7066 DCHECK(!Runtime::Current()->UseJitCompilation());
7067 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007068 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007069 DCHECK(Runtime::Current()->UseJitCompilation());
Artem Serovc5fcb442016-12-02 19:19:58 +00007070 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01007071 case HLoadClass::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007072 case HLoadClass::LoadKind::kRuntimeCall:
Artem Serovd4cc5b22016-11-04 11:19:09 +00007073 break;
7074 }
7075 return desired_class_load_kind;
Artem Serov02d37832016-10-25 15:25:33 +01007076}
7077
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007078void LocationsBuilderARMVIXL::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00007079 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007080 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007081 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00007082 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007083 cls,
7084 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00007085 LocationFrom(r0));
Vladimir Markoea4c1262017-02-06 19:59:33 +00007086 DCHECK(calling_convention.GetRegisterAt(0).Is(r0));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007087 return;
7088 }
Vladimir Marko41559982017-01-06 14:04:23 +00007089 DCHECK(!cls->NeedsAccessCheck());
Scott Wakelingfe885462016-09-22 10:24:38 +01007090
Artem Serovd4cc5b22016-11-04 11:19:09 +00007091 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
7092 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007093 ? LocationSummary::kCallOnSlowPath
7094 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01007095 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(cls, call_kind);
Artem Serovd4cc5b22016-11-04 11:19:09 +00007096 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007097 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Artem Serovd4cc5b22016-11-04 11:19:09 +00007098 }
7099
Vladimir Marko41559982017-01-06 14:04:23 +00007100 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007101 locations->SetInAt(0, Location::RequiresRegister());
7102 }
7103 locations->SetOut(Location::RequiresRegister());
Vladimir Markoea4c1262017-02-06 19:59:33 +00007104 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
7105 if (!kUseReadBarrier || kUseBakerReadBarrier) {
7106 // Rely on the type resolution or initialization and marking to save everything we need.
7107 // Note that IP may be clobbered by saving/restoring the live register (only one thanks
7108 // to the custom calling convention) or by marking, so we request a different temp.
7109 locations->AddTemp(Location::RequiresRegister());
7110 RegisterSet caller_saves = RegisterSet::Empty();
7111 InvokeRuntimeCallingConventionARMVIXL calling_convention;
7112 caller_saves.Add(LocationFrom(calling_convention.GetRegisterAt(0)));
7113 // TODO: Add GetReturnLocation() to the calling convention so that we can DCHECK()
7114 // that the the kPrimNot result register is the same as the first argument register.
7115 locations->SetCustomSlowPathCallerSaves(caller_saves);
7116 } else {
7117 // For non-Baker read barrier we have a temp-clobbering call.
7118 }
7119 }
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01007120 if (kUseBakerReadBarrier && kBakerReadBarrierLinkTimeThunksEnableForGcRoots) {
7121 if (load_kind == HLoadClass::LoadKind::kBssEntry ||
7122 (load_kind == HLoadClass::LoadKind::kReferrersClass &&
7123 !Runtime::Current()->UseJitCompilation())) {
7124 locations->AddTemp(Location::RegisterLocation(kBakerCcEntrypointRegister.GetCode()));
7125 }
7126 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007127}
7128
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007129// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
7130// move.
7131void InstructionCodeGeneratorARMVIXL::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00007132 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007133 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00007134 codegen_->GenerateLoadClassRuntimeCall(cls);
Roland Levillain5daa4952017-07-03 17:23:56 +01007135 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 13);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007136 return;
7137 }
Vladimir Marko41559982017-01-06 14:04:23 +00007138 DCHECK(!cls->NeedsAccessCheck());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007139
Vladimir Marko41559982017-01-06 14:04:23 +00007140 LocationSummary* locations = cls->GetLocations();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007141 Location out_loc = locations->Out();
7142 vixl32::Register out = OutputRegister(cls);
7143
Artem Serovd4cc5b22016-11-04 11:19:09 +00007144 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
7145 ? kWithoutReadBarrier
7146 : kCompilerReadBarrierOption;
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007147 bool generate_null_check = false;
Vladimir Marko41559982017-01-06 14:04:23 +00007148 switch (load_kind) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007149 case HLoadClass::LoadKind::kReferrersClass: {
7150 DCHECK(!cls->CanCallRuntime());
7151 DCHECK(!cls->MustGenerateClinitCheck());
7152 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
7153 vixl32::Register current_method = InputRegisterAt(cls, 0);
7154 GenerateGcRootFieldLoad(cls,
7155 out_loc,
7156 current_method,
Roland Levillain00468f32016-10-27 18:02:48 +01007157 ArtMethod::DeclaringClassOffset().Int32Value(),
Artem Serovd4cc5b22016-11-04 11:19:09 +00007158 read_barrier_option);
7159 break;
7160 }
Artem Serovd4cc5b22016-11-04 11:19:09 +00007161 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007162 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Artem Serovd4cc5b22016-11-04 11:19:09 +00007163 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
7164 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
7165 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
7166 codegen_->EmitMovwMovtPlaceholder(labels, out);
7167 break;
7168 }
7169 case HLoadClass::LoadKind::kBootImageAddress: {
Artem Serovc5fcb442016-12-02 19:19:58 +00007170 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007171 uint32_t address = dchecked_integral_cast<uint32_t>(
7172 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
7173 DCHECK_NE(address, 0u);
Artem Serovc5fcb442016-12-02 19:19:58 +00007174 __ Ldr(out, codegen_->DeduplicateBootImageAddressLiteral(address));
Artem Serovd4cc5b22016-11-04 11:19:09 +00007175 break;
7176 }
Vladimir Marko94ec2db2017-09-06 17:21:03 +01007177 case HLoadClass::LoadKind::kBootImageClassTable: {
7178 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
7179 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
7180 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
7181 codegen_->EmitMovwMovtPlaceholder(labels, out);
7182 __ Ldr(out, MemOperand(out, /* offset */ 0));
7183 // Extract the reference from the slot data, i.e. clear the hash bits.
7184 int32_t masked_hash = ClassTable::TableSlot::MaskHash(
7185 ComputeModifiedUtf8Hash(cls->GetDexFile().StringByTypeIdx(cls->GetTypeIndex())));
7186 if (masked_hash != 0) {
7187 __ Sub(out, out, Operand(masked_hash));
7188 }
7189 break;
7190 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007191 case HLoadClass::LoadKind::kBssEntry: {
Vladimir Markoea4c1262017-02-06 19:59:33 +00007192 vixl32::Register temp = (!kUseReadBarrier || kUseBakerReadBarrier)
7193 ? RegisterFrom(locations->GetTemp(0))
7194 : out;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007195 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
Vladimir Marko1998cd02017-01-13 13:02:58 +00007196 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex());
Vladimir Markoea4c1262017-02-06 19:59:33 +00007197 codegen_->EmitMovwMovtPlaceholder(labels, temp);
7198 GenerateGcRootFieldLoad(cls, out_loc, temp, /* offset */ 0, read_barrier_option);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007199 generate_null_check = true;
7200 break;
7201 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007202 case HLoadClass::LoadKind::kJitTableAddress: {
Artem Serovc5fcb442016-12-02 19:19:58 +00007203 __ Ldr(out, codegen_->DeduplicateJitClassLiteral(cls->GetDexFile(),
7204 cls->GetTypeIndex(),
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007205 cls->GetClass()));
Artem Serovc5fcb442016-12-02 19:19:58 +00007206 // /* GcRoot<mirror::Class> */ out = *out
Vladimir Markoea4c1262017-02-06 19:59:33 +00007207 GenerateGcRootFieldLoad(cls, out_loc, out, /* offset */ 0, read_barrier_option);
Artem Serovd4cc5b22016-11-04 11:19:09 +00007208 break;
7209 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007210 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00007211 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00007212 LOG(FATAL) << "UNREACHABLE";
7213 UNREACHABLE();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007214 }
7215
7216 if (generate_null_check || cls->MustGenerateClinitCheck()) {
7217 DCHECK(cls->CanCallRuntime());
Vladimir Marko174b2e22017-10-12 13:34:49 +01007218 LoadClassSlowPathARMVIXL* slow_path =
7219 new (codegen_->GetScopedAllocator()) LoadClassSlowPathARMVIXL(
7220 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007221 codegen_->AddSlowPath(slow_path);
7222 if (generate_null_check) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00007223 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007224 }
7225 if (cls->MustGenerateClinitCheck()) {
7226 GenerateClassInitializationCheck(slow_path, out);
7227 } else {
7228 __ Bind(slow_path->GetExitLabel());
7229 }
Roland Levillain5daa4952017-07-03 17:23:56 +01007230 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 14);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007231 }
7232}
7233
Artem Serov02d37832016-10-25 15:25:33 +01007234void LocationsBuilderARMVIXL::VisitClinitCheck(HClinitCheck* check) {
7235 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007236 new (GetGraph()->GetAllocator()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
Artem Serov02d37832016-10-25 15:25:33 +01007237 locations->SetInAt(0, Location::RequiresRegister());
7238 if (check->HasUses()) {
7239 locations->SetOut(Location::SameAsFirstInput());
7240 }
7241}
7242
7243void InstructionCodeGeneratorARMVIXL::VisitClinitCheck(HClinitCheck* check) {
7244 // We assume the class is not null.
7245 LoadClassSlowPathARMVIXL* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01007246 new (codegen_->GetScopedAllocator()) LoadClassSlowPathARMVIXL(check->GetLoadClass(),
7247 check,
7248 check->GetDexPc(),
7249 /* do_clinit */ true);
Artem Serov02d37832016-10-25 15:25:33 +01007250 codegen_->AddSlowPath(slow_path);
7251 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
7252}
7253
7254void InstructionCodeGeneratorARMVIXL::GenerateClassInitializationCheck(
7255 LoadClassSlowPathARMVIXL* slow_path, vixl32::Register class_reg) {
7256 UseScratchRegisterScope temps(GetVIXLAssembler());
7257 vixl32::Register temp = temps.Acquire();
Igor Murashkin86083f72017-10-27 10:59:04 -07007258 GetAssembler()->LoadFromOffset(kLoadSignedByte,
Artem Serov02d37832016-10-25 15:25:33 +01007259 temp,
7260 class_reg,
7261 mirror::Class::StatusOffset().Int32Value());
7262 __ Cmp(temp, mirror::Class::kStatusInitialized);
7263 __ B(lt, slow_path->GetEntryLabel());
7264 // Even if the initialized flag is set, we may be in a situation where caches are not synced
7265 // properly. Therefore, we do a memory fence.
7266 __ Dmb(ISH);
7267 __ Bind(slow_path->GetExitLabel());
7268}
7269
Artem Serov02d37832016-10-25 15:25:33 +01007270HLoadString::LoadKind CodeGeneratorARMVIXL::GetSupportedLoadStringKind(
Artem Serovd4cc5b22016-11-04 11:19:09 +00007271 HLoadString::LoadKind desired_string_load_kind) {
7272 switch (desired_string_load_kind) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00007273 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007274 case HLoadString::LoadKind::kBootImageInternTable:
Artem Serovd4cc5b22016-11-04 11:19:09 +00007275 case HLoadString::LoadKind::kBssEntry:
7276 DCHECK(!Runtime::Current()->UseJitCompilation());
7277 break;
7278 case HLoadString::LoadKind::kJitTableAddress:
7279 DCHECK(Runtime::Current()->UseJitCompilation());
Artem Serovc5fcb442016-12-02 19:19:58 +00007280 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01007281 case HLoadString::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007282 case HLoadString::LoadKind::kRuntimeCall:
Artem Serovd4cc5b22016-11-04 11:19:09 +00007283 break;
7284 }
7285 return desired_string_load_kind;
Artem Serov02d37832016-10-25 15:25:33 +01007286}
7287
7288void LocationsBuilderARMVIXL::VisitLoadString(HLoadString* load) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00007289 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Vladimir Markoca6fff82017-10-03 14:49:14 +01007290 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(load, call_kind);
Artem Serov02d37832016-10-25 15:25:33 +01007291 HLoadString::LoadKind load_kind = load->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007292 if (load_kind == HLoadString::LoadKind::kRuntimeCall) {
Artem Serov02d37832016-10-25 15:25:33 +01007293 locations->SetOut(LocationFrom(r0));
7294 } else {
7295 locations->SetOut(Location::RequiresRegister());
Artem Serovd4cc5b22016-11-04 11:19:09 +00007296 if (load_kind == HLoadString::LoadKind::kBssEntry) {
7297 if (!kUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00007298 // Rely on the pResolveString and marking to save everything we need, including temps.
7299 // Note that IP may be clobbered by saving/restoring the live register (only one thanks
7300 // to the custom calling convention) or by marking, so we request a different temp.
Artem Serovd4cc5b22016-11-04 11:19:09 +00007301 locations->AddTemp(Location::RequiresRegister());
7302 RegisterSet caller_saves = RegisterSet::Empty();
7303 InvokeRuntimeCallingConventionARMVIXL calling_convention;
7304 caller_saves.Add(LocationFrom(calling_convention.GetRegisterAt(0)));
7305 // TODO: Add GetReturnLocation() to the calling convention so that we can DCHECK()
7306 // that the the kPrimNot result register is the same as the first argument register.
7307 locations->SetCustomSlowPathCallerSaves(caller_saves);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01007308 if (kUseBakerReadBarrier && kBakerReadBarrierLinkTimeThunksEnableForGcRoots) {
7309 locations->AddTemp(Location::RegisterLocation(kBakerCcEntrypointRegister.GetCode()));
7310 }
Artem Serovd4cc5b22016-11-04 11:19:09 +00007311 } else {
7312 // For non-Baker read barrier we have a temp-clobbering call.
7313 }
7314 }
Artem Serov02d37832016-10-25 15:25:33 +01007315 }
7316}
7317
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007318// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
7319// move.
7320void InstructionCodeGeneratorARMVIXL::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Artem Serovd4cc5b22016-11-04 11:19:09 +00007321 LocationSummary* locations = load->GetLocations();
7322 Location out_loc = locations->Out();
7323 vixl32::Register out = OutputRegister(load);
7324 HLoadString::LoadKind load_kind = load->GetLoadKind();
7325
7326 switch (load_kind) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00007327 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
7328 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
7329 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007330 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Artem Serovd4cc5b22016-11-04 11:19:09 +00007331 codegen_->EmitMovwMovtPlaceholder(labels, out);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007332 return;
Artem Serovd4cc5b22016-11-04 11:19:09 +00007333 }
7334 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007335 uint32_t address = dchecked_integral_cast<uint32_t>(
7336 reinterpret_cast<uintptr_t>(load->GetString().Get()));
7337 DCHECK_NE(address, 0u);
Artem Serovc5fcb442016-12-02 19:19:58 +00007338 __ Ldr(out, codegen_->DeduplicateBootImageAddressLiteral(address));
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007339 return;
7340 }
7341 case HLoadString::LoadKind::kBootImageInternTable: {
7342 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
7343 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
7344 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
7345 codegen_->EmitMovwMovtPlaceholder(labels, out);
7346 __ Ldr(out, MemOperand(out, /* offset */ 0));
7347 return;
Artem Serovd4cc5b22016-11-04 11:19:09 +00007348 }
7349 case HLoadString::LoadKind::kBssEntry: {
7350 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markoea4c1262017-02-06 19:59:33 +00007351 vixl32::Register temp = (!kUseReadBarrier || kUseBakerReadBarrier)
7352 ? RegisterFrom(locations->GetTemp(0))
7353 : out;
Artem Serovd4cc5b22016-11-04 11:19:09 +00007354 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007355 codegen_->NewStringBssEntryPatch(load->GetDexFile(), load->GetStringIndex());
Artem Serovd4cc5b22016-11-04 11:19:09 +00007356 codegen_->EmitMovwMovtPlaceholder(labels, temp);
7357 GenerateGcRootFieldLoad(load, out_loc, temp, /* offset */ 0, kCompilerReadBarrierOption);
7358 LoadStringSlowPathARMVIXL* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01007359 new (codegen_->GetScopedAllocator()) LoadStringSlowPathARMVIXL(load);
Artem Serovd4cc5b22016-11-04 11:19:09 +00007360 codegen_->AddSlowPath(slow_path);
7361 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
7362 __ Bind(slow_path->GetExitLabel());
Roland Levillain5daa4952017-07-03 17:23:56 +01007363 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 15);
Artem Serovd4cc5b22016-11-04 11:19:09 +00007364 return;
7365 }
7366 case HLoadString::LoadKind::kJitTableAddress: {
Artem Serovc5fcb442016-12-02 19:19:58 +00007367 __ Ldr(out, codegen_->DeduplicateJitStringLiteral(load->GetDexFile(),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007368 load->GetStringIndex(),
7369 load->GetString()));
Artem Serovc5fcb442016-12-02 19:19:58 +00007370 // /* GcRoot<mirror::String> */ out = *out
7371 GenerateGcRootFieldLoad(load, out_loc, out, /* offset */ 0, kCompilerReadBarrierOption);
7372 return;
Artem Serovd4cc5b22016-11-04 11:19:09 +00007373 }
7374 default:
7375 break;
7376 }
Artem Serov02d37832016-10-25 15:25:33 +01007377
7378 // TODO: Re-add the compiler code to do string dex cache lookup again.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007379 DCHECK_EQ(load->GetLoadKind(), HLoadString::LoadKind::kRuntimeCall);
Artem Serov02d37832016-10-25 15:25:33 +01007380 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Andreas Gampe8a0128a2016-11-28 07:38:35 -08007381 __ Mov(calling_convention.GetRegisterAt(0), load->GetStringIndex().index_);
Artem Serov02d37832016-10-25 15:25:33 +01007382 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
7383 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Roland Levillain5daa4952017-07-03 17:23:56 +01007384 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 16);
Artem Serov02d37832016-10-25 15:25:33 +01007385}
7386
7387static int32_t GetExceptionTlsOffset() {
7388 return Thread::ExceptionOffset<kArmPointerSize>().Int32Value();
7389}
7390
7391void LocationsBuilderARMVIXL::VisitLoadException(HLoadException* load) {
7392 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007393 new (GetGraph()->GetAllocator()) LocationSummary(load, LocationSummary::kNoCall);
Artem Serov02d37832016-10-25 15:25:33 +01007394 locations->SetOut(Location::RequiresRegister());
7395}
7396
7397void InstructionCodeGeneratorARMVIXL::VisitLoadException(HLoadException* load) {
7398 vixl32::Register out = OutputRegister(load);
7399 GetAssembler()->LoadFromOffset(kLoadWord, out, tr, GetExceptionTlsOffset());
7400}
7401
7402
7403void LocationsBuilderARMVIXL::VisitClearException(HClearException* clear) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007404 new (GetGraph()->GetAllocator()) LocationSummary(clear, LocationSummary::kNoCall);
Artem Serov02d37832016-10-25 15:25:33 +01007405}
7406
7407void InstructionCodeGeneratorARMVIXL::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
7408 UseScratchRegisterScope temps(GetVIXLAssembler());
7409 vixl32::Register temp = temps.Acquire();
7410 __ Mov(temp, 0);
7411 GetAssembler()->StoreToOffset(kStoreWord, temp, tr, GetExceptionTlsOffset());
7412}
7413
7414void LocationsBuilderARMVIXL::VisitThrow(HThrow* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007415 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
7416 instruction, LocationSummary::kCallOnMainOnly);
Artem Serov02d37832016-10-25 15:25:33 +01007417 InvokeRuntimeCallingConventionARMVIXL calling_convention;
7418 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
7419}
7420
7421void InstructionCodeGeneratorARMVIXL::VisitThrow(HThrow* instruction) {
7422 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
7423 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
7424}
7425
Artem Serov657022c2016-11-23 14:19:38 +00007426// Temp is used for read barrier.
7427static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
7428 if (kEmitCompilerReadBarrier &&
7429 (kUseBakerReadBarrier ||
7430 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
7431 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
7432 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
7433 return 1;
7434 }
7435 return 0;
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007436}
7437
Artem Serov657022c2016-11-23 14:19:38 +00007438// Interface case has 3 temps, one for holding the number of interfaces, one for the current
7439// interface pointer, one for loading the current interface.
7440// The other checks have one temp for loading the object's class.
7441static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
7442 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
7443 return 3;
7444 }
7445 return 1 + NumberOfInstanceOfTemps(type_check_kind);
7446}
Artem Serovcfbe9132016-10-14 15:58:56 +01007447
7448void LocationsBuilderARMVIXL::VisitInstanceOf(HInstanceOf* instruction) {
7449 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
7450 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
7451 bool baker_read_barrier_slow_path = false;
7452 switch (type_check_kind) {
7453 case TypeCheckKind::kExactCheck:
7454 case TypeCheckKind::kAbstractClassCheck:
7455 case TypeCheckKind::kClassHierarchyCheck:
7456 case TypeCheckKind::kArrayObjectCheck:
7457 call_kind =
7458 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
7459 baker_read_barrier_slow_path = kUseBakerReadBarrier;
7460 break;
7461 case TypeCheckKind::kArrayCheck:
7462 case TypeCheckKind::kUnresolvedCheck:
7463 case TypeCheckKind::kInterfaceCheck:
7464 call_kind = LocationSummary::kCallOnSlowPath;
7465 break;
7466 }
7467
Vladimir Markoca6fff82017-10-03 14:49:14 +01007468 LocationSummary* locations =
7469 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Artem Serovcfbe9132016-10-14 15:58:56 +01007470 if (baker_read_barrier_slow_path) {
7471 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
7472 }
7473 locations->SetInAt(0, Location::RequiresRegister());
7474 locations->SetInAt(1, Location::RequiresRegister());
7475 // The "out" register is used as a temporary, so it overlaps with the inputs.
7476 // Note that TypeCheckSlowPathARM uses this register too.
7477 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Artem Serov657022c2016-11-23 14:19:38 +00007478 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01007479 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
7480 codegen_->MaybeAddBakerCcEntrypointTempForFields(locations);
7481 }
Artem Serovcfbe9132016-10-14 15:58:56 +01007482}
7483
7484void InstructionCodeGeneratorARMVIXL::VisitInstanceOf(HInstanceOf* instruction) {
7485 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
7486 LocationSummary* locations = instruction->GetLocations();
7487 Location obj_loc = locations->InAt(0);
7488 vixl32::Register obj = InputRegisterAt(instruction, 0);
7489 vixl32::Register cls = InputRegisterAt(instruction, 1);
7490 Location out_loc = locations->Out();
7491 vixl32::Register out = OutputRegister(instruction);
Artem Serov657022c2016-11-23 14:19:38 +00007492 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
7493 DCHECK_LE(num_temps, 1u);
7494 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Artem Serovcfbe9132016-10-14 15:58:56 +01007495 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
7496 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
7497 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
7498 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007499 vixl32::Label done;
7500 vixl32::Label* const final_label = codegen_->GetFinalLabel(instruction, &done);
Artem Serovcfbe9132016-10-14 15:58:56 +01007501 SlowPathCodeARMVIXL* slow_path = nullptr;
7502
7503 // Return 0 if `obj` is null.
7504 // avoid null check if we know obj is not null.
7505 if (instruction->MustDoNullCheck()) {
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007506 DCHECK(!out.Is(obj));
7507 __ Mov(out, 0);
7508 __ CompareAndBranchIfZero(obj, final_label, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007509 }
7510
Artem Serovcfbe9132016-10-14 15:58:56 +01007511 switch (type_check_kind) {
7512 case TypeCheckKind::kExactCheck: {
Mathieu Chartier6beced42016-11-15 15:51:31 -08007513 // /* HeapReference<Class> */ out = obj->klass_
7514 GenerateReferenceLoadTwoRegisters(instruction,
7515 out_loc,
7516 obj_loc,
7517 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00007518 maybe_temp_loc,
7519 kCompilerReadBarrierOption);
Artem Serovcfbe9132016-10-14 15:58:56 +01007520 // Classes must be equal for the instanceof to succeed.
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007521 __ Cmp(out, cls);
7522 // We speculatively set the result to false without changing the condition
7523 // flags, which allows us to avoid some branching later.
7524 __ Mov(LeaveFlags, out, 0);
7525
7526 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
7527 // we check that the output is in a low register, so that a 16-bit MOV
7528 // encoding can be used.
7529 if (out.IsLow()) {
7530 // We use the scope because of the IT block that follows.
7531 ExactAssemblyScope guard(GetVIXLAssembler(),
7532 2 * vixl32::k16BitT32InstructionSizeInBytes,
7533 CodeBufferCheckScope::kExactSize);
7534
7535 __ it(eq);
7536 __ mov(eq, out, 1);
7537 } else {
7538 __ B(ne, final_label, /* far_target */ false);
7539 __ Mov(out, 1);
7540 }
7541
Artem Serovcfbe9132016-10-14 15:58:56 +01007542 break;
7543 }
7544
7545 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier6beced42016-11-15 15:51:31 -08007546 // /* HeapReference<Class> */ out = obj->klass_
7547 GenerateReferenceLoadTwoRegisters(instruction,
7548 out_loc,
7549 obj_loc,
7550 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00007551 maybe_temp_loc,
7552 kCompilerReadBarrierOption);
Artem Serovcfbe9132016-10-14 15:58:56 +01007553 // If the class is abstract, we eagerly fetch the super class of the
7554 // object to avoid doing a comparison we know will fail.
7555 vixl32::Label loop;
7556 __ Bind(&loop);
7557 // /* HeapReference<Class> */ out = out->super_class_
Artem Serov657022c2016-11-23 14:19:38 +00007558 GenerateReferenceLoadOneRegister(instruction,
7559 out_loc,
7560 super_offset,
7561 maybe_temp_loc,
7562 kCompilerReadBarrierOption);
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007563 // If `out` is null, we use it for the result, and jump to the final label.
Anton Kirilov6f644202017-02-27 18:29:45 +00007564 __ CompareAndBranchIfZero(out, final_label, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007565 __ Cmp(out, cls);
Artem Serov517d9f62016-12-12 15:51:15 +00007566 __ B(ne, &loop, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007567 __ Mov(out, 1);
Artem Serovcfbe9132016-10-14 15:58:56 +01007568 break;
7569 }
7570
7571 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier6beced42016-11-15 15:51:31 -08007572 // /* HeapReference<Class> */ out = obj->klass_
7573 GenerateReferenceLoadTwoRegisters(instruction,
7574 out_loc,
7575 obj_loc,
7576 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00007577 maybe_temp_loc,
7578 kCompilerReadBarrierOption);
Artem Serovcfbe9132016-10-14 15:58:56 +01007579 // Walk over the class hierarchy to find a match.
7580 vixl32::Label loop, success;
7581 __ Bind(&loop);
7582 __ Cmp(out, cls);
Artem Serov517d9f62016-12-12 15:51:15 +00007583 __ B(eq, &success, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007584 // /* HeapReference<Class> */ out = out->super_class_
Artem Serov657022c2016-11-23 14:19:38 +00007585 GenerateReferenceLoadOneRegister(instruction,
7586 out_loc,
7587 super_offset,
7588 maybe_temp_loc,
7589 kCompilerReadBarrierOption);
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007590 // This is essentially a null check, but it sets the condition flags to the
7591 // proper value for the code that follows the loop, i.e. not `eq`.
7592 __ Cmp(out, 1);
7593 __ B(hs, &loop, /* far_target */ false);
7594
7595 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
7596 // we check that the output is in a low register, so that a 16-bit MOV
7597 // encoding can be used.
7598 if (out.IsLow()) {
7599 // If `out` is null, we use it for the result, and the condition flags
7600 // have already been set to `ne`, so the IT block that comes afterwards
7601 // (and which handles the successful case) turns into a NOP (instead of
7602 // overwriting `out`).
7603 __ Bind(&success);
7604
7605 // We use the scope because of the IT block that follows.
7606 ExactAssemblyScope guard(GetVIXLAssembler(),
7607 2 * vixl32::k16BitT32InstructionSizeInBytes,
7608 CodeBufferCheckScope::kExactSize);
7609
7610 // There is only one branch to the `success` label (which is bound to this
7611 // IT block), and it has the same condition, `eq`, so in that case the MOV
7612 // is executed.
7613 __ it(eq);
7614 __ mov(eq, out, 1);
7615 } else {
7616 // If `out` is null, we use it for the result, and jump to the final label.
Anton Kirilov6f644202017-02-27 18:29:45 +00007617 __ B(final_label);
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007618 __ Bind(&success);
7619 __ Mov(out, 1);
Artem Serovcfbe9132016-10-14 15:58:56 +01007620 }
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007621
Artem Serovcfbe9132016-10-14 15:58:56 +01007622 break;
7623 }
7624
7625 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier6beced42016-11-15 15:51:31 -08007626 // /* HeapReference<Class> */ out = obj->klass_
7627 GenerateReferenceLoadTwoRegisters(instruction,
7628 out_loc,
7629 obj_loc,
7630 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00007631 maybe_temp_loc,
7632 kCompilerReadBarrierOption);
Artem Serovcfbe9132016-10-14 15:58:56 +01007633 // Do an exact check.
7634 vixl32::Label exact_check;
7635 __ Cmp(out, cls);
Artem Serov517d9f62016-12-12 15:51:15 +00007636 __ B(eq, &exact_check, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007637 // Otherwise, we need to check that the object's class is a non-primitive array.
7638 // /* HeapReference<Class> */ out = out->component_type_
Artem Serov657022c2016-11-23 14:19:38 +00007639 GenerateReferenceLoadOneRegister(instruction,
7640 out_loc,
7641 component_offset,
7642 maybe_temp_loc,
7643 kCompilerReadBarrierOption);
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007644 // If `out` is null, we use it for the result, and jump to the final label.
Anton Kirilov6f644202017-02-27 18:29:45 +00007645 __ CompareAndBranchIfZero(out, final_label, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007646 GetAssembler()->LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
7647 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007648 __ Cmp(out, 0);
7649 // We speculatively set the result to false without changing the condition
7650 // flags, which allows us to avoid some branching later.
7651 __ Mov(LeaveFlags, out, 0);
7652
7653 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
7654 // we check that the output is in a low register, so that a 16-bit MOV
7655 // encoding can be used.
7656 if (out.IsLow()) {
7657 __ Bind(&exact_check);
7658
7659 // We use the scope because of the IT block that follows.
7660 ExactAssemblyScope guard(GetVIXLAssembler(),
7661 2 * vixl32::k16BitT32InstructionSizeInBytes,
7662 CodeBufferCheckScope::kExactSize);
7663
7664 __ it(eq);
7665 __ mov(eq, out, 1);
7666 } else {
7667 __ B(ne, final_label, /* far_target */ false);
7668 __ Bind(&exact_check);
7669 __ Mov(out, 1);
7670 }
7671
Artem Serovcfbe9132016-10-14 15:58:56 +01007672 break;
7673 }
7674
7675 case TypeCheckKind::kArrayCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00007676 // No read barrier since the slow path will retry upon failure.
Mathieu Chartier6beced42016-11-15 15:51:31 -08007677 // /* HeapReference<Class> */ out = obj->klass_
7678 GenerateReferenceLoadTwoRegisters(instruction,
7679 out_loc,
7680 obj_loc,
7681 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00007682 maybe_temp_loc,
7683 kWithoutReadBarrier);
Artem Serovcfbe9132016-10-14 15:58:56 +01007684 __ Cmp(out, cls);
7685 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01007686 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathARMVIXL(
7687 instruction, /* is_fatal */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007688 codegen_->AddSlowPath(slow_path);
7689 __ B(ne, slow_path->GetEntryLabel());
7690 __ Mov(out, 1);
Artem Serovcfbe9132016-10-14 15:58:56 +01007691 break;
7692 }
7693
7694 case TypeCheckKind::kUnresolvedCheck:
7695 case TypeCheckKind::kInterfaceCheck: {
7696 // Note that we indeed only call on slow path, but we always go
7697 // into the slow path for the unresolved and interface check
7698 // cases.
7699 //
7700 // We cannot directly call the InstanceofNonTrivial runtime
7701 // entry point without resorting to a type checking slow path
7702 // here (i.e. by calling InvokeRuntime directly), as it would
7703 // require to assign fixed registers for the inputs of this
7704 // HInstanceOf instruction (following the runtime calling
7705 // convention), which might be cluttered by the potential first
7706 // read barrier emission at the beginning of this method.
7707 //
7708 // TODO: Introduce a new runtime entry point taking the object
7709 // to test (instead of its class) as argument, and let it deal
7710 // with the read barrier issues. This will let us refactor this
7711 // case of the `switch` code as it was previously (with a direct
7712 // call to the runtime not using a type checking slow path).
7713 // This should also be beneficial for the other cases above.
7714 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01007715 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathARMVIXL(
7716 instruction, /* is_fatal */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007717 codegen_->AddSlowPath(slow_path);
7718 __ B(slow_path->GetEntryLabel());
Artem Serovcfbe9132016-10-14 15:58:56 +01007719 break;
7720 }
7721 }
7722
Artem Serovcfbe9132016-10-14 15:58:56 +01007723 if (done.IsReferenced()) {
7724 __ Bind(&done);
7725 }
7726
7727 if (slow_path != nullptr) {
7728 __ Bind(slow_path->GetExitLabel());
7729 }
7730}
7731
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007732void LocationsBuilderARMVIXL::VisitCheckCast(HCheckCast* instruction) {
7733 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
7734 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
7735
7736 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
7737 switch (type_check_kind) {
7738 case TypeCheckKind::kExactCheck:
7739 case TypeCheckKind::kAbstractClassCheck:
7740 case TypeCheckKind::kClassHierarchyCheck:
7741 case TypeCheckKind::kArrayObjectCheck:
7742 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
7743 LocationSummary::kCallOnSlowPath :
7744 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
7745 break;
7746 case TypeCheckKind::kArrayCheck:
7747 case TypeCheckKind::kUnresolvedCheck:
7748 case TypeCheckKind::kInterfaceCheck:
7749 call_kind = LocationSummary::kCallOnSlowPath;
7750 break;
7751 }
7752
Vladimir Markoca6fff82017-10-03 14:49:14 +01007753 LocationSummary* locations =
7754 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007755 locations->SetInAt(0, Location::RequiresRegister());
7756 locations->SetInAt(1, Location::RequiresRegister());
Artem Serov657022c2016-11-23 14:19:38 +00007757 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007758}
7759
7760void InstructionCodeGeneratorARMVIXL::VisitCheckCast(HCheckCast* instruction) {
7761 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
7762 LocationSummary* locations = instruction->GetLocations();
7763 Location obj_loc = locations->InAt(0);
7764 vixl32::Register obj = InputRegisterAt(instruction, 0);
7765 vixl32::Register cls = InputRegisterAt(instruction, 1);
7766 Location temp_loc = locations->GetTemp(0);
7767 vixl32::Register temp = RegisterFrom(temp_loc);
Artem Serov657022c2016-11-23 14:19:38 +00007768 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
7769 DCHECK_LE(num_temps, 3u);
7770 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
7771 Location maybe_temp3_loc = (num_temps >= 3) ? locations->GetTemp(2) : Location::NoLocation();
7772 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
7773 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
7774 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
7775 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
7776 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
7777 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
7778 const uint32_t object_array_data_offset =
7779 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007780
Artem Serov657022c2016-11-23 14:19:38 +00007781 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
7782 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
7783 // read barriers is done for performance and code size reasons.
7784 bool is_type_check_slow_path_fatal = false;
7785 if (!kEmitCompilerReadBarrier) {
7786 is_type_check_slow_path_fatal =
7787 (type_check_kind == TypeCheckKind::kExactCheck ||
7788 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
7789 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
7790 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
7791 !instruction->CanThrowIntoCatchBlock();
7792 }
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007793 SlowPathCodeARMVIXL* type_check_slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01007794 new (codegen_->GetScopedAllocator()) TypeCheckSlowPathARMVIXL(
7795 instruction, is_type_check_slow_path_fatal);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007796 codegen_->AddSlowPath(type_check_slow_path);
7797
7798 vixl32::Label done;
Anton Kirilov6f644202017-02-27 18:29:45 +00007799 vixl32::Label* final_label = codegen_->GetFinalLabel(instruction, &done);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007800 // Avoid null check if we know obj is not null.
7801 if (instruction->MustDoNullCheck()) {
Anton Kirilov6f644202017-02-27 18:29:45 +00007802 __ CompareAndBranchIfZero(obj, final_label, /* far_target */ false);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007803 }
7804
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007805 switch (type_check_kind) {
7806 case TypeCheckKind::kExactCheck:
7807 case TypeCheckKind::kArrayCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00007808 // /* HeapReference<Class> */ temp = obj->klass_
7809 GenerateReferenceLoadTwoRegisters(instruction,
7810 temp_loc,
7811 obj_loc,
7812 class_offset,
7813 maybe_temp2_loc,
7814 kWithoutReadBarrier);
7815
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007816 __ Cmp(temp, cls);
7817 // Jump to slow path for throwing the exception or doing a
7818 // more involved array check.
7819 __ B(ne, type_check_slow_path->GetEntryLabel());
7820 break;
7821 }
7822
7823 case TypeCheckKind::kAbstractClassCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00007824 // /* HeapReference<Class> */ temp = obj->klass_
7825 GenerateReferenceLoadTwoRegisters(instruction,
7826 temp_loc,
7827 obj_loc,
7828 class_offset,
7829 maybe_temp2_loc,
7830 kWithoutReadBarrier);
7831
Artem Serovcfbe9132016-10-14 15:58:56 +01007832 // If the class is abstract, we eagerly fetch the super class of the
7833 // object to avoid doing a comparison we know will fail.
7834 vixl32::Label loop;
7835 __ Bind(&loop);
7836 // /* HeapReference<Class> */ temp = temp->super_class_
Artem Serov657022c2016-11-23 14:19:38 +00007837 GenerateReferenceLoadOneRegister(instruction,
7838 temp_loc,
7839 super_offset,
7840 maybe_temp2_loc,
7841 kWithoutReadBarrier);
Artem Serovcfbe9132016-10-14 15:58:56 +01007842
7843 // If the class reference currently in `temp` is null, jump to the slow path to throw the
7844 // exception.
xueliang.zhongf51bc622016-11-04 09:23:32 +00007845 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
Artem Serovcfbe9132016-10-14 15:58:56 +01007846
7847 // Otherwise, compare the classes.
7848 __ Cmp(temp, cls);
Artem Serov517d9f62016-12-12 15:51:15 +00007849 __ B(ne, &loop, /* far_target */ false);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007850 break;
7851 }
7852
7853 case TypeCheckKind::kClassHierarchyCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00007854 // /* HeapReference<Class> */ temp = obj->klass_
7855 GenerateReferenceLoadTwoRegisters(instruction,
7856 temp_loc,
7857 obj_loc,
7858 class_offset,
7859 maybe_temp2_loc,
7860 kWithoutReadBarrier);
7861
Artem Serovcfbe9132016-10-14 15:58:56 +01007862 // Walk over the class hierarchy to find a match.
7863 vixl32::Label loop;
7864 __ Bind(&loop);
7865 __ Cmp(temp, cls);
Anton Kirilov6f644202017-02-27 18:29:45 +00007866 __ B(eq, final_label, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007867
7868 // /* HeapReference<Class> */ temp = temp->super_class_
Artem Serov657022c2016-11-23 14:19:38 +00007869 GenerateReferenceLoadOneRegister(instruction,
7870 temp_loc,
7871 super_offset,
7872 maybe_temp2_loc,
7873 kWithoutReadBarrier);
Artem Serovcfbe9132016-10-14 15:58:56 +01007874
7875 // If the class reference currently in `temp` is null, jump to the slow path to throw the
7876 // exception.
xueliang.zhongf51bc622016-11-04 09:23:32 +00007877 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
Artem Serovcfbe9132016-10-14 15:58:56 +01007878 // Otherwise, jump to the beginning of the loop.
7879 __ B(&loop);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007880 break;
7881 }
7882
Artem Serovcfbe9132016-10-14 15:58:56 +01007883 case TypeCheckKind::kArrayObjectCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00007884 // /* HeapReference<Class> */ temp = obj->klass_
7885 GenerateReferenceLoadTwoRegisters(instruction,
7886 temp_loc,
7887 obj_loc,
7888 class_offset,
7889 maybe_temp2_loc,
7890 kWithoutReadBarrier);
7891
Artem Serovcfbe9132016-10-14 15:58:56 +01007892 // Do an exact check.
7893 __ Cmp(temp, cls);
Anton Kirilov6f644202017-02-27 18:29:45 +00007894 __ B(eq, final_label, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007895
7896 // Otherwise, we need to check that the object's class is a non-primitive array.
7897 // /* HeapReference<Class> */ temp = temp->component_type_
Artem Serov657022c2016-11-23 14:19:38 +00007898 GenerateReferenceLoadOneRegister(instruction,
7899 temp_loc,
7900 component_offset,
7901 maybe_temp2_loc,
7902 kWithoutReadBarrier);
Artem Serovcfbe9132016-10-14 15:58:56 +01007903 // If the component type is null, jump to the slow path to throw the exception.
xueliang.zhongf51bc622016-11-04 09:23:32 +00007904 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
Artem Serovcfbe9132016-10-14 15:58:56 +01007905 // Otherwise,the object is indeed an array, jump to label `check_non_primitive_component_type`
7906 // to further check that this component type is not a primitive type.
7907 GetAssembler()->LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007908 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
xueliang.zhongf51bc622016-11-04 09:23:32 +00007909 __ CompareAndBranchIfNonZero(temp, type_check_slow_path->GetEntryLabel());
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007910 break;
7911 }
7912
7913 case TypeCheckKind::kUnresolvedCheck:
Artem Serov657022c2016-11-23 14:19:38 +00007914 // We always go into the type check slow path for the unresolved check case.
Artem Serovcfbe9132016-10-14 15:58:56 +01007915 // We cannot directly call the CheckCast runtime entry point
7916 // without resorting to a type checking slow path here (i.e. by
7917 // calling InvokeRuntime directly), as it would require to
7918 // assign fixed registers for the inputs of this HInstanceOf
7919 // instruction (following the runtime calling convention), which
7920 // might be cluttered by the potential first read barrier
7921 // emission at the beginning of this method.
Artem Serov657022c2016-11-23 14:19:38 +00007922
Artem Serovcfbe9132016-10-14 15:58:56 +01007923 __ B(type_check_slow_path->GetEntryLabel());
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007924 break;
Artem Serov657022c2016-11-23 14:19:38 +00007925
7926 case TypeCheckKind::kInterfaceCheck: {
7927 // Avoid read barriers to improve performance of the fast path. We can not get false
7928 // positives by doing this.
7929 // /* HeapReference<Class> */ temp = obj->klass_
7930 GenerateReferenceLoadTwoRegisters(instruction,
7931 temp_loc,
7932 obj_loc,
7933 class_offset,
7934 maybe_temp2_loc,
7935 kWithoutReadBarrier);
7936
7937 // /* HeapReference<Class> */ temp = temp->iftable_
7938 GenerateReferenceLoadTwoRegisters(instruction,
7939 temp_loc,
7940 temp_loc,
7941 iftable_offset,
7942 maybe_temp2_loc,
7943 kWithoutReadBarrier);
7944 // Iftable is never null.
7945 __ Ldr(RegisterFrom(maybe_temp2_loc), MemOperand(temp, array_length_offset));
7946 // Loop through the iftable and check if any class matches.
7947 vixl32::Label start_loop;
7948 __ Bind(&start_loop);
7949 __ CompareAndBranchIfZero(RegisterFrom(maybe_temp2_loc),
7950 type_check_slow_path->GetEntryLabel());
7951 __ Ldr(RegisterFrom(maybe_temp3_loc), MemOperand(temp, object_array_data_offset));
7952 GetAssembler()->MaybeUnpoisonHeapReference(RegisterFrom(maybe_temp3_loc));
7953 // Go to next interface.
7954 __ Add(temp, temp, Operand::From(2 * kHeapReferenceSize));
7955 __ Sub(RegisterFrom(maybe_temp2_loc), RegisterFrom(maybe_temp2_loc), 2);
7956 // Compare the classes and continue the loop if they do not match.
7957 __ Cmp(cls, RegisterFrom(maybe_temp3_loc));
Artem Serov517d9f62016-12-12 15:51:15 +00007958 __ B(ne, &start_loop, /* far_target */ false);
Artem Serov657022c2016-11-23 14:19:38 +00007959 break;
7960 }
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007961 }
Anton Kirilov6f644202017-02-27 18:29:45 +00007962 if (done.IsReferenced()) {
7963 __ Bind(&done);
7964 }
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007965
7966 __ Bind(type_check_slow_path->GetExitLabel());
7967}
7968
Artem Serov551b28f2016-10-18 19:11:30 +01007969void LocationsBuilderARMVIXL::VisitMonitorOperation(HMonitorOperation* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007970 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
7971 instruction, LocationSummary::kCallOnMainOnly);
Artem Serov551b28f2016-10-18 19:11:30 +01007972 InvokeRuntimeCallingConventionARMVIXL calling_convention;
7973 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
7974}
7975
7976void InstructionCodeGeneratorARMVIXL::VisitMonitorOperation(HMonitorOperation* instruction) {
7977 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
7978 instruction,
7979 instruction->GetDexPc());
7980 if (instruction->IsEnter()) {
7981 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
7982 } else {
7983 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
7984 }
Roland Levillain5daa4952017-07-03 17:23:56 +01007985 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 17);
Artem Serov551b28f2016-10-18 19:11:30 +01007986}
7987
Artem Serov02109dd2016-09-23 17:17:54 +01007988void LocationsBuilderARMVIXL::VisitAnd(HAnd* instruction) {
7989 HandleBitwiseOperation(instruction, AND);
7990}
7991
7992void LocationsBuilderARMVIXL::VisitOr(HOr* instruction) {
7993 HandleBitwiseOperation(instruction, ORR);
7994}
7995
7996void LocationsBuilderARMVIXL::VisitXor(HXor* instruction) {
7997 HandleBitwiseOperation(instruction, EOR);
7998}
7999
8000void LocationsBuilderARMVIXL::HandleBitwiseOperation(HBinaryOperation* instruction, Opcode opcode) {
8001 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008002 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008003 DCHECK(instruction->GetResultType() == DataType::Type::kInt32
8004 || instruction->GetResultType() == DataType::Type::kInt64);
Artem Serov02109dd2016-09-23 17:17:54 +01008005 // Note: GVN reorders commutative operations to have the constant on the right hand side.
8006 locations->SetInAt(0, Location::RequiresRegister());
8007 locations->SetInAt(1, ArmEncodableConstantOrRegister(instruction->InputAt(1), opcode));
8008 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8009}
8010
8011void InstructionCodeGeneratorARMVIXL::VisitAnd(HAnd* instruction) {
8012 HandleBitwiseOperation(instruction);
8013}
8014
8015void InstructionCodeGeneratorARMVIXL::VisitOr(HOr* instruction) {
8016 HandleBitwiseOperation(instruction);
8017}
8018
8019void InstructionCodeGeneratorARMVIXL::VisitXor(HXor* instruction) {
8020 HandleBitwiseOperation(instruction);
8021}
8022
Artem Serov2bbc9532016-10-21 11:51:50 +01008023void LocationsBuilderARMVIXL::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
8024 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008025 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008026 DCHECK(instruction->GetResultType() == DataType::Type::kInt32
8027 || instruction->GetResultType() == DataType::Type::kInt64);
Artem Serov2bbc9532016-10-21 11:51:50 +01008028
8029 locations->SetInAt(0, Location::RequiresRegister());
8030 locations->SetInAt(1, Location::RequiresRegister());
8031 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8032}
8033
8034void InstructionCodeGeneratorARMVIXL::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
8035 LocationSummary* locations = instruction->GetLocations();
8036 Location first = locations->InAt(0);
8037 Location second = locations->InAt(1);
8038 Location out = locations->Out();
8039
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008040 if (instruction->GetResultType() == DataType::Type::kInt32) {
Artem Serov2bbc9532016-10-21 11:51:50 +01008041 vixl32::Register first_reg = RegisterFrom(first);
8042 vixl32::Register second_reg = RegisterFrom(second);
8043 vixl32::Register out_reg = RegisterFrom(out);
8044
8045 switch (instruction->GetOpKind()) {
8046 case HInstruction::kAnd:
8047 __ Bic(out_reg, first_reg, second_reg);
8048 break;
8049 case HInstruction::kOr:
8050 __ Orn(out_reg, first_reg, second_reg);
8051 break;
8052 // There is no EON on arm.
8053 case HInstruction::kXor:
8054 default:
8055 LOG(FATAL) << "Unexpected instruction " << instruction->DebugName();
8056 UNREACHABLE();
8057 }
8058 return;
8059
8060 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008061 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
Artem Serov2bbc9532016-10-21 11:51:50 +01008062 vixl32::Register first_low = LowRegisterFrom(first);
8063 vixl32::Register first_high = HighRegisterFrom(first);
8064 vixl32::Register second_low = LowRegisterFrom(second);
8065 vixl32::Register second_high = HighRegisterFrom(second);
8066 vixl32::Register out_low = LowRegisterFrom(out);
8067 vixl32::Register out_high = HighRegisterFrom(out);
8068
8069 switch (instruction->GetOpKind()) {
8070 case HInstruction::kAnd:
8071 __ Bic(out_low, first_low, second_low);
8072 __ Bic(out_high, first_high, second_high);
8073 break;
8074 case HInstruction::kOr:
8075 __ Orn(out_low, first_low, second_low);
8076 __ Orn(out_high, first_high, second_high);
8077 break;
8078 // There is no EON on arm.
8079 case HInstruction::kXor:
8080 default:
8081 LOG(FATAL) << "Unexpected instruction " << instruction->DebugName();
8082 UNREACHABLE();
8083 }
8084 }
8085}
8086
Anton Kirilov74234da2017-01-13 14:42:47 +00008087void LocationsBuilderARMVIXL::VisitDataProcWithShifterOp(
8088 HDataProcWithShifterOp* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008089 DCHECK(instruction->GetType() == DataType::Type::kInt32 ||
8090 instruction->GetType() == DataType::Type::kInt64);
Anton Kirilov74234da2017-01-13 14:42:47 +00008091 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008092 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008093 const bool overlap = instruction->GetType() == DataType::Type::kInt64 &&
Anton Kirilov74234da2017-01-13 14:42:47 +00008094 HDataProcWithShifterOp::IsExtensionOp(instruction->GetOpKind());
8095
8096 locations->SetInAt(0, Location::RequiresRegister());
8097 locations->SetInAt(1, Location::RequiresRegister());
8098 locations->SetOut(Location::RequiresRegister(),
8099 overlap ? Location::kOutputOverlap : Location::kNoOutputOverlap);
8100}
8101
8102void InstructionCodeGeneratorARMVIXL::VisitDataProcWithShifterOp(
8103 HDataProcWithShifterOp* instruction) {
8104 const LocationSummary* const locations = instruction->GetLocations();
8105 const HInstruction::InstructionKind kind = instruction->GetInstrKind();
8106 const HDataProcWithShifterOp::OpKind op_kind = instruction->GetOpKind();
8107
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008108 if (instruction->GetType() == DataType::Type::kInt32) {
Anton Kirilov420ee302017-02-21 18:10:26 +00008109 const vixl32::Register first = InputRegisterAt(instruction, 0);
8110 const vixl32::Register output = OutputRegister(instruction);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008111 const vixl32::Register second = instruction->InputAt(1)->GetType() == DataType::Type::kInt64
Anton Kirilov74234da2017-01-13 14:42:47 +00008112 ? LowRegisterFrom(locations->InAt(1))
8113 : InputRegisterAt(instruction, 1);
8114
Anton Kirilov420ee302017-02-21 18:10:26 +00008115 if (HDataProcWithShifterOp::IsExtensionOp(op_kind)) {
8116 DCHECK_EQ(kind, HInstruction::kAdd);
8117
8118 switch (op_kind) {
8119 case HDataProcWithShifterOp::kUXTB:
8120 __ Uxtab(output, first, second);
8121 break;
8122 case HDataProcWithShifterOp::kUXTH:
8123 __ Uxtah(output, first, second);
8124 break;
8125 case HDataProcWithShifterOp::kSXTB:
8126 __ Sxtab(output, first, second);
8127 break;
8128 case HDataProcWithShifterOp::kSXTH:
8129 __ Sxtah(output, first, second);
8130 break;
8131 default:
8132 LOG(FATAL) << "Unexpected operation kind: " << op_kind;
8133 UNREACHABLE();
8134 }
8135 } else {
8136 GenerateDataProcInstruction(kind,
8137 output,
8138 first,
8139 Operand(second,
8140 ShiftFromOpKind(op_kind),
8141 instruction->GetShiftAmount()),
8142 codegen_);
8143 }
Anton Kirilov74234da2017-01-13 14:42:47 +00008144 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008145 DCHECK_EQ(instruction->GetType(), DataType::Type::kInt64);
Anton Kirilov74234da2017-01-13 14:42:47 +00008146
8147 if (HDataProcWithShifterOp::IsExtensionOp(op_kind)) {
8148 const vixl32::Register second = InputRegisterAt(instruction, 1);
8149
8150 DCHECK(!LowRegisterFrom(locations->Out()).Is(second));
8151 GenerateDataProc(kind,
8152 locations->Out(),
8153 locations->InAt(0),
8154 second,
8155 Operand(second, ShiftType::ASR, 31),
8156 codegen_);
8157 } else {
8158 GenerateLongDataProc(instruction, codegen_);
8159 }
8160 }
8161}
8162
Artem Serov02109dd2016-09-23 17:17:54 +01008163// TODO(VIXL): Remove optimizations in the helper when they are implemented in vixl.
8164void InstructionCodeGeneratorARMVIXL::GenerateAndConst(vixl32::Register out,
8165 vixl32::Register first,
8166 uint32_t value) {
8167 // Optimize special cases for individual halfs of `and-long` (`and` is simplified earlier).
8168 if (value == 0xffffffffu) {
8169 if (!out.Is(first)) {
8170 __ Mov(out, first);
8171 }
8172 return;
8173 }
8174 if (value == 0u) {
8175 __ Mov(out, 0);
8176 return;
8177 }
8178 if (GetAssembler()->ShifterOperandCanHold(AND, value)) {
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00008179 __ And(out, first, value);
8180 } else if (GetAssembler()->ShifterOperandCanHold(BIC, ~value)) {
8181 __ Bic(out, first, ~value);
Artem Serov02109dd2016-09-23 17:17:54 +01008182 } else {
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00008183 DCHECK(IsPowerOfTwo(value + 1));
8184 __ Ubfx(out, first, 0, WhichPowerOf2(value + 1));
Artem Serov02109dd2016-09-23 17:17:54 +01008185 }
8186}
8187
8188// TODO(VIXL): Remove optimizations in the helper when they are implemented in vixl.
8189void InstructionCodeGeneratorARMVIXL::GenerateOrrConst(vixl32::Register out,
8190 vixl32::Register first,
8191 uint32_t value) {
8192 // Optimize special cases for individual halfs of `or-long` (`or` is simplified earlier).
8193 if (value == 0u) {
8194 if (!out.Is(first)) {
8195 __ Mov(out, first);
8196 }
8197 return;
8198 }
8199 if (value == 0xffffffffu) {
8200 __ Mvn(out, 0);
8201 return;
8202 }
8203 if (GetAssembler()->ShifterOperandCanHold(ORR, value)) {
8204 __ Orr(out, first, value);
8205 } else {
8206 DCHECK(GetAssembler()->ShifterOperandCanHold(ORN, ~value));
8207 __ Orn(out, first, ~value);
8208 }
8209}
8210
8211// TODO(VIXL): Remove optimizations in the helper when they are implemented in vixl.
8212void InstructionCodeGeneratorARMVIXL::GenerateEorConst(vixl32::Register out,
8213 vixl32::Register first,
8214 uint32_t value) {
8215 // Optimize special case for individual halfs of `xor-long` (`xor` is simplified earlier).
8216 if (value == 0u) {
8217 if (!out.Is(first)) {
8218 __ Mov(out, first);
8219 }
8220 return;
8221 }
8222 __ Eor(out, first, value);
8223}
8224
Anton Kirilovdda43962016-11-21 19:55:20 +00008225void InstructionCodeGeneratorARMVIXL::GenerateAddLongConst(Location out,
8226 Location first,
8227 uint64_t value) {
8228 vixl32::Register out_low = LowRegisterFrom(out);
8229 vixl32::Register out_high = HighRegisterFrom(out);
8230 vixl32::Register first_low = LowRegisterFrom(first);
8231 vixl32::Register first_high = HighRegisterFrom(first);
8232 uint32_t value_low = Low32Bits(value);
8233 uint32_t value_high = High32Bits(value);
8234 if (value_low == 0u) {
8235 if (!out_low.Is(first_low)) {
8236 __ Mov(out_low, first_low);
8237 }
8238 __ Add(out_high, first_high, value_high);
8239 return;
8240 }
8241 __ Adds(out_low, first_low, value_low);
Scott Wakelingbffdc702016-12-07 17:46:03 +00008242 if (GetAssembler()->ShifterOperandCanHold(ADC, value_high, kCcDontCare)) {
Anton Kirilovdda43962016-11-21 19:55:20 +00008243 __ Adc(out_high, first_high, value_high);
Scott Wakelingbffdc702016-12-07 17:46:03 +00008244 } else if (GetAssembler()->ShifterOperandCanHold(SBC, ~value_high, kCcDontCare)) {
Anton Kirilovdda43962016-11-21 19:55:20 +00008245 __ Sbc(out_high, first_high, ~value_high);
8246 } else {
8247 LOG(FATAL) << "Unexpected constant " << value_high;
8248 UNREACHABLE();
8249 }
8250}
8251
Artem Serov02109dd2016-09-23 17:17:54 +01008252void InstructionCodeGeneratorARMVIXL::HandleBitwiseOperation(HBinaryOperation* instruction) {
8253 LocationSummary* locations = instruction->GetLocations();
8254 Location first = locations->InAt(0);
8255 Location second = locations->InAt(1);
8256 Location out = locations->Out();
8257
8258 if (second.IsConstant()) {
8259 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
8260 uint32_t value_low = Low32Bits(value);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008261 if (instruction->GetResultType() == DataType::Type::kInt32) {
Artem Serov02109dd2016-09-23 17:17:54 +01008262 vixl32::Register first_reg = InputRegisterAt(instruction, 0);
8263 vixl32::Register out_reg = OutputRegister(instruction);
8264 if (instruction->IsAnd()) {
8265 GenerateAndConst(out_reg, first_reg, value_low);
8266 } else if (instruction->IsOr()) {
8267 GenerateOrrConst(out_reg, first_reg, value_low);
8268 } else {
8269 DCHECK(instruction->IsXor());
8270 GenerateEorConst(out_reg, first_reg, value_low);
8271 }
8272 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008273 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
Artem Serov02109dd2016-09-23 17:17:54 +01008274 uint32_t value_high = High32Bits(value);
8275 vixl32::Register first_low = LowRegisterFrom(first);
8276 vixl32::Register first_high = HighRegisterFrom(first);
8277 vixl32::Register out_low = LowRegisterFrom(out);
8278 vixl32::Register out_high = HighRegisterFrom(out);
8279 if (instruction->IsAnd()) {
8280 GenerateAndConst(out_low, first_low, value_low);
8281 GenerateAndConst(out_high, first_high, value_high);
8282 } else if (instruction->IsOr()) {
8283 GenerateOrrConst(out_low, first_low, value_low);
8284 GenerateOrrConst(out_high, first_high, value_high);
8285 } else {
8286 DCHECK(instruction->IsXor());
8287 GenerateEorConst(out_low, first_low, value_low);
8288 GenerateEorConst(out_high, first_high, value_high);
8289 }
8290 }
8291 return;
8292 }
8293
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008294 if (instruction->GetResultType() == DataType::Type::kInt32) {
Artem Serov02109dd2016-09-23 17:17:54 +01008295 vixl32::Register first_reg = InputRegisterAt(instruction, 0);
8296 vixl32::Register second_reg = InputRegisterAt(instruction, 1);
8297 vixl32::Register out_reg = OutputRegister(instruction);
8298 if (instruction->IsAnd()) {
8299 __ And(out_reg, first_reg, second_reg);
8300 } else if (instruction->IsOr()) {
8301 __ Orr(out_reg, first_reg, second_reg);
8302 } else {
8303 DCHECK(instruction->IsXor());
8304 __ Eor(out_reg, first_reg, second_reg);
8305 }
8306 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008307 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
Artem Serov02109dd2016-09-23 17:17:54 +01008308 vixl32::Register first_low = LowRegisterFrom(first);
8309 vixl32::Register first_high = HighRegisterFrom(first);
8310 vixl32::Register second_low = LowRegisterFrom(second);
8311 vixl32::Register second_high = HighRegisterFrom(second);
8312 vixl32::Register out_low = LowRegisterFrom(out);
8313 vixl32::Register out_high = HighRegisterFrom(out);
8314 if (instruction->IsAnd()) {
8315 __ And(out_low, first_low, second_low);
8316 __ And(out_high, first_high, second_high);
8317 } else if (instruction->IsOr()) {
8318 __ Orr(out_low, first_low, second_low);
8319 __ Orr(out_high, first_high, second_high);
8320 } else {
8321 DCHECK(instruction->IsXor());
8322 __ Eor(out_low, first_low, second_low);
8323 __ Eor(out_high, first_high, second_high);
8324 }
8325 }
8326}
8327
Artem Serovcfbe9132016-10-14 15:58:56 +01008328void InstructionCodeGeneratorARMVIXL::GenerateReferenceLoadOneRegister(
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008329 HInstruction* instruction,
Artem Serovcfbe9132016-10-14 15:58:56 +01008330 Location out,
8331 uint32_t offset,
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008332 Location maybe_temp,
8333 ReadBarrierOption read_barrier_option) {
Artem Serovcfbe9132016-10-14 15:58:56 +01008334 vixl32::Register out_reg = RegisterFrom(out);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008335 if (read_barrier_option == kWithReadBarrier) {
8336 CHECK(kEmitCompilerReadBarrier);
8337 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
8338 if (kUseBakerReadBarrier) {
8339 // Load with fast path based Baker's read barrier.
8340 // /* HeapReference<Object> */ out = *(out + offset)
8341 codegen_->GenerateFieldLoadWithBakerReadBarrier(
8342 instruction, out, out_reg, offset, maybe_temp, /* needs_null_check */ false);
8343 } else {
8344 // Load with slow path based read barrier.
8345 // Save the value of `out` into `maybe_temp` before overwriting it
8346 // in the following move operation, as we will need it for the
8347 // read barrier below.
8348 __ Mov(RegisterFrom(maybe_temp), out_reg);
8349 // /* HeapReference<Object> */ out = *(out + offset)
8350 GetAssembler()->LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
8351 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
8352 }
Artem Serovcfbe9132016-10-14 15:58:56 +01008353 } else {
8354 // Plain load with no read barrier.
8355 // /* HeapReference<Object> */ out = *(out + offset)
8356 GetAssembler()->LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
8357 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
8358 }
8359}
8360
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008361void InstructionCodeGeneratorARMVIXL::GenerateReferenceLoadTwoRegisters(
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008362 HInstruction* instruction,
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008363 Location out,
8364 Location obj,
8365 uint32_t offset,
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008366 Location maybe_temp,
8367 ReadBarrierOption read_barrier_option) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008368 vixl32::Register out_reg = RegisterFrom(out);
8369 vixl32::Register obj_reg = RegisterFrom(obj);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008370 if (read_barrier_option == kWithReadBarrier) {
8371 CHECK(kEmitCompilerReadBarrier);
8372 if (kUseBakerReadBarrier) {
8373 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
8374 // Load with fast path based Baker's read barrier.
8375 // /* HeapReference<Object> */ out = *(obj + offset)
8376 codegen_->GenerateFieldLoadWithBakerReadBarrier(
8377 instruction, out, obj_reg, offset, maybe_temp, /* needs_null_check */ false);
8378 } else {
8379 // Load with slow path based read barrier.
8380 // /* HeapReference<Object> */ out = *(obj + offset)
8381 GetAssembler()->LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
8382 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
8383 }
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008384 } else {
8385 // Plain load with no read barrier.
8386 // /* HeapReference<Object> */ out = *(obj + offset)
8387 GetAssembler()->LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
8388 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
8389 }
8390}
8391
Scott Wakelinga7812ae2016-10-17 10:03:36 +01008392void InstructionCodeGeneratorARMVIXL::GenerateGcRootFieldLoad(
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008393 HInstruction* instruction,
Scott Wakelinga7812ae2016-10-17 10:03:36 +01008394 Location root,
8395 vixl32::Register obj,
8396 uint32_t offset,
Artem Serovd4cc5b22016-11-04 11:19:09 +00008397 ReadBarrierOption read_barrier_option) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01008398 vixl32::Register root_reg = RegisterFrom(root);
Artem Serovd4cc5b22016-11-04 11:19:09 +00008399 if (read_barrier_option == kWithReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008400 DCHECK(kEmitCompilerReadBarrier);
8401 if (kUseBakerReadBarrier) {
8402 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
Roland Levillainba650a42017-03-06 13:52:32 +00008403 // Baker's read barrier are used.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008404 if (kBakerReadBarrierLinkTimeThunksEnableForGcRoots &&
8405 !Runtime::Current()->UseJitCompilation()) {
Roland Levillain6d729a72017-06-30 18:34:01 +01008406 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in
8407 // the Marking Register) to decide whether we need to enter
8408 // the slow path to mark the GC root.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008409 //
8410 // We use link-time generated thunks for the slow path. That thunk
8411 // checks the reference and jumps to the entrypoint if needed.
8412 //
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008413 // lr = &return_address;
8414 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
Roland Levillain6d729a72017-06-30 18:34:01 +01008415 // if (mr) { // Thread::Current()->GetIsGcMarking()
8416 // goto gc_root_thunk<root_reg>(lr)
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008417 // }
8418 // return_address:
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008419
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008420 UseScratchRegisterScope temps(GetVIXLAssembler());
8421 ExcludeIPAndBakerCcEntrypointRegister(&temps, instruction);
Vladimir Marko88abba22017-05-03 17:09:25 +01008422 bool narrow = CanEmitNarrowLdr(root_reg, obj, offset);
8423 uint32_t custom_data = linker::Thumb2RelativePatcher::EncodeBakerReadBarrierGcRootData(
8424 root_reg.GetCode(), narrow);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008425 vixl32::Label* bne_label = codegen_->NewBakerReadBarrierPatch(custom_data);
Roland Levillainba650a42017-03-06 13:52:32 +00008426
Roland Levillain6d729a72017-06-30 18:34:01 +01008427 vixl::EmissionCheckScope guard(GetVIXLAssembler(), 4 * vixl32::kMaxInstructionSizeInBytes);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008428 vixl32::Label return_address;
8429 EmitAdrCode adr(GetVIXLAssembler(), lr, &return_address);
Roland Levillain6d729a72017-06-30 18:34:01 +01008430 __ cmp(mr, Operand(0));
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008431 // Currently the offset is always within range. If that changes,
8432 // we shall have to split the load the same way as for fields.
8433 DCHECK_LT(offset, kReferenceLoadMinFarOffset);
Vladimir Marko88abba22017-05-03 17:09:25 +01008434 ptrdiff_t old_offset = GetVIXLAssembler()->GetBuffer()->GetCursorOffset();
8435 __ ldr(EncodingSize(narrow ? Narrow : Wide), root_reg, MemOperand(obj, offset));
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008436 EmitPlaceholderBne(codegen_, bne_label);
8437 __ Bind(&return_address);
Vladimir Marko88abba22017-05-03 17:09:25 +01008438 DCHECK_EQ(old_offset - GetVIXLAssembler()->GetBuffer()->GetCursorOffset(),
8439 narrow ? BAKER_MARK_INTROSPECTION_GC_ROOT_LDR_NARROW_OFFSET
8440 : BAKER_MARK_INTROSPECTION_GC_ROOT_LDR_WIDE_OFFSET);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008441 } else {
Roland Levillain6d729a72017-06-30 18:34:01 +01008442 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in
8443 // the Marking Register) to decide whether we need to enter
8444 // the slow path to mark the GC root.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008445 //
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008446 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
Roland Levillain6d729a72017-06-30 18:34:01 +01008447 // if (mr) { // Thread::Current()->GetIsGcMarking()
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008448 // // Slow path.
Roland Levillain6d729a72017-06-30 18:34:01 +01008449 // entrypoint = Thread::Current()->pReadBarrierMarkReg ## root.reg()
8450 // root = entrypoint(root); // root = ReadBarrier::Mark(root); // Entry point call.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008451 // }
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008452
Roland Levillain6d729a72017-06-30 18:34:01 +01008453 // Slow path marking the GC root `root`. The entrypoint will
8454 // be loaded by the slow path code.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008455 SlowPathCodeARMVIXL* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01008456 new (codegen_->GetScopedAllocator()) ReadBarrierMarkSlowPathARMVIXL(instruction, root);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008457 codegen_->AddSlowPath(slow_path);
8458
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008459 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
8460 GetAssembler()->LoadFromOffset(kLoadWord, root_reg, obj, offset);
8461 static_assert(
8462 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
8463 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
8464 "have different sizes.");
8465 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
8466 "art::mirror::CompressedReference<mirror::Object> and int32_t "
8467 "have different sizes.");
8468
Roland Levillain6d729a72017-06-30 18:34:01 +01008469 __ CompareAndBranchIfNonZero(mr, slow_path->GetEntryLabel());
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008470 __ Bind(slow_path->GetExitLabel());
8471 }
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008472 } else {
8473 // GC root loaded through a slow path for read barriers other
8474 // than Baker's.
8475 // /* GcRoot<mirror::Object>* */ root = obj + offset
8476 __ Add(root_reg, obj, offset);
8477 // /* mirror::Object* */ root = root->Read()
8478 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
8479 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01008480 } else {
8481 // Plain GC root load with no read barrier.
8482 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
8483 GetAssembler()->LoadFromOffset(kLoadWord, root_reg, obj, offset);
8484 // Note that GC roots are not affected by heap poisoning, thus we
8485 // do not have to unpoison `root_reg` here.
8486 }
Roland Levillain5daa4952017-07-03 17:23:56 +01008487 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 18);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01008488}
8489
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008490void CodeGeneratorARMVIXL::MaybeAddBakerCcEntrypointTempForFields(LocationSummary* locations) {
8491 DCHECK(kEmitCompilerReadBarrier);
8492 DCHECK(kUseBakerReadBarrier);
8493 if (kBakerReadBarrierLinkTimeThunksEnableForFields) {
8494 if (!Runtime::Current()->UseJitCompilation()) {
8495 locations->AddTemp(Location::RegisterLocation(kBakerCcEntrypointRegister.GetCode()));
8496 }
8497 }
8498}
8499
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008500void CodeGeneratorARMVIXL::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
8501 Location ref,
8502 vixl32::Register obj,
8503 uint32_t offset,
8504 Location temp,
8505 bool needs_null_check) {
8506 DCHECK(kEmitCompilerReadBarrier);
8507 DCHECK(kUseBakerReadBarrier);
8508
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008509 if (kBakerReadBarrierLinkTimeThunksEnableForFields &&
8510 !Runtime::Current()->UseJitCompilation()) {
Roland Levillain6d729a72017-06-30 18:34:01 +01008511 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in the
8512 // Marking Register) to decide whether we need to enter the slow
8513 // path to mark the reference. Then, in the slow path, check the
8514 // gray bit in the lock word of the reference's holder (`obj`) to
8515 // decide whether to mark `ref` or not.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008516 //
8517 // We use link-time generated thunks for the slow path. That thunk checks
8518 // the holder and jumps to the entrypoint if needed. If the holder is not
8519 // gray, it creates a fake dependency and returns to the LDR instruction.
8520 //
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008521 // lr = &gray_return_address;
Roland Levillain6d729a72017-06-30 18:34:01 +01008522 // if (mr) { // Thread::Current()->GetIsGcMarking()
8523 // goto field_thunk<holder_reg, base_reg>(lr)
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008524 // }
8525 // not_gray_return_address:
8526 // // Original reference load. If the offset is too large to fit
8527 // // into LDR, we use an adjusted base register here.
Vladimir Marko88abba22017-05-03 17:09:25 +01008528 // HeapReference<mirror::Object> reference = *(obj+offset);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008529 // gray_return_address:
8530
8531 DCHECK_ALIGNED(offset, sizeof(mirror::HeapReference<mirror::Object>));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008532 vixl32::Register ref_reg = RegisterFrom(ref, DataType::Type::kReference);
Vladimir Marko88abba22017-05-03 17:09:25 +01008533 bool narrow = CanEmitNarrowLdr(ref_reg, obj, offset);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008534 vixl32::Register base = obj;
8535 if (offset >= kReferenceLoadMinFarOffset) {
8536 base = RegisterFrom(temp);
8537 DCHECK(!base.Is(kBakerCcEntrypointRegister));
8538 static_assert(IsPowerOfTwo(kReferenceLoadMinFarOffset), "Expecting a power of 2.");
8539 __ Add(base, obj, Operand(offset & ~(kReferenceLoadMinFarOffset - 1u)));
8540 offset &= (kReferenceLoadMinFarOffset - 1u);
Vladimir Marko88abba22017-05-03 17:09:25 +01008541 // Use narrow LDR only for small offsets. Generating narrow encoding LDR for the large
8542 // offsets with `(offset & (kReferenceLoadMinFarOffset - 1u)) < 32u` would most likely
8543 // increase the overall code size when taking the generated thunks into account.
8544 DCHECK(!narrow);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008545 }
8546 UseScratchRegisterScope temps(GetVIXLAssembler());
8547 ExcludeIPAndBakerCcEntrypointRegister(&temps, instruction);
8548 uint32_t custom_data = linker::Thumb2RelativePatcher::EncodeBakerReadBarrierFieldData(
Vladimir Marko88abba22017-05-03 17:09:25 +01008549 base.GetCode(), obj.GetCode(), narrow);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008550 vixl32::Label* bne_label = NewBakerReadBarrierPatch(custom_data);
8551
Roland Levillain5daa4952017-07-03 17:23:56 +01008552 {
8553 vixl::EmissionCheckScope guard(
8554 GetVIXLAssembler(),
8555 (kPoisonHeapReferences ? 5u : 4u) * vixl32::kMaxInstructionSizeInBytes);
8556 vixl32::Label return_address;
8557 EmitAdrCode adr(GetVIXLAssembler(), lr, &return_address);
8558 __ cmp(mr, Operand(0));
8559 EmitPlaceholderBne(this, bne_label);
8560 ptrdiff_t old_offset = GetVIXLAssembler()->GetBuffer()->GetCursorOffset();
8561 __ ldr(EncodingSize(narrow ? Narrow : Wide), ref_reg, MemOperand(base, offset));
8562 if (needs_null_check) {
8563 MaybeRecordImplicitNullCheck(instruction);
Vladimir Marko88abba22017-05-03 17:09:25 +01008564 }
Roland Levillain5daa4952017-07-03 17:23:56 +01008565 // Note: We need a specific width for the unpoisoning NEG.
8566 if (kPoisonHeapReferences) {
8567 if (narrow) {
8568 // The only 16-bit encoding is T1 which sets flags outside IT block (i.e. RSBS, not RSB).
8569 __ rsbs(EncodingSize(Narrow), ref_reg, ref_reg, Operand(0));
8570 } else {
8571 __ rsb(EncodingSize(Wide), ref_reg, ref_reg, Operand(0));
8572 }
8573 }
8574 __ Bind(&return_address);
8575 DCHECK_EQ(old_offset - GetVIXLAssembler()->GetBuffer()->GetCursorOffset(),
8576 narrow ? BAKER_MARK_INTROSPECTION_FIELD_LDR_NARROW_OFFSET
8577 : BAKER_MARK_INTROSPECTION_FIELD_LDR_WIDE_OFFSET);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008578 }
Roland Levillain5daa4952017-07-03 17:23:56 +01008579 MaybeGenerateMarkingRegisterCheck(/* code */ 19, /* temp_loc */ LocationFrom(ip));
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008580 return;
8581 }
8582
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008583 // /* HeapReference<Object> */ ref = *(obj + offset)
8584 Location no_index = Location::NoLocation();
8585 ScaleFactor no_scale_factor = TIMES_1;
8586 GenerateReferenceLoadWithBakerReadBarrier(
8587 instruction, ref, obj, offset, no_index, no_scale_factor, temp, needs_null_check);
Roland Levillain6070e882016-11-03 17:51:58 +00008588}
8589
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008590void CodeGeneratorARMVIXL::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
8591 Location ref,
8592 vixl32::Register obj,
8593 uint32_t data_offset,
8594 Location index,
8595 Location temp,
8596 bool needs_null_check) {
8597 DCHECK(kEmitCompilerReadBarrier);
8598 DCHECK(kUseBakerReadBarrier);
8599
8600 static_assert(
8601 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
8602 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008603 ScaleFactor scale_factor = TIMES_4;
8604
8605 if (kBakerReadBarrierLinkTimeThunksEnableForArrays &&
8606 !Runtime::Current()->UseJitCompilation()) {
Roland Levillain6d729a72017-06-30 18:34:01 +01008607 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in the
8608 // Marking Register) to decide whether we need to enter the slow
8609 // path to mark the reference. Then, in the slow path, check the
8610 // gray bit in the lock word of the reference's holder (`obj`) to
8611 // decide whether to mark `ref` or not.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008612 //
8613 // We use link-time generated thunks for the slow path. That thunk checks
8614 // the holder and jumps to the entrypoint if needed. If the holder is not
8615 // gray, it creates a fake dependency and returns to the LDR instruction.
8616 //
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008617 // lr = &gray_return_address;
Roland Levillain6d729a72017-06-30 18:34:01 +01008618 // if (mr) { // Thread::Current()->GetIsGcMarking()
8619 // goto array_thunk<base_reg>(lr)
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008620 // }
8621 // not_gray_return_address:
8622 // // Original reference load. If the offset is too large to fit
8623 // // into LDR, we use an adjusted base register here.
Vladimir Marko88abba22017-05-03 17:09:25 +01008624 // HeapReference<mirror::Object> reference = data[index];
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008625 // gray_return_address:
8626
8627 DCHECK(index.IsValid());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008628 vixl32::Register index_reg = RegisterFrom(index, DataType::Type::kInt32);
8629 vixl32::Register ref_reg = RegisterFrom(ref, DataType::Type::kReference);
8630 vixl32::Register data_reg = RegisterFrom(temp, DataType::Type::kInt32); // Raw pointer.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008631 DCHECK(!data_reg.Is(kBakerCcEntrypointRegister));
8632
8633 UseScratchRegisterScope temps(GetVIXLAssembler());
8634 ExcludeIPAndBakerCcEntrypointRegister(&temps, instruction);
8635 uint32_t custom_data =
8636 linker::Thumb2RelativePatcher::EncodeBakerReadBarrierArrayData(data_reg.GetCode());
8637 vixl32::Label* bne_label = NewBakerReadBarrierPatch(custom_data);
8638
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008639 __ Add(data_reg, obj, Operand(data_offset));
Roland Levillain5daa4952017-07-03 17:23:56 +01008640 {
8641 vixl::EmissionCheckScope guard(
8642 GetVIXLAssembler(),
8643 (kPoisonHeapReferences ? 5u : 4u) * vixl32::kMaxInstructionSizeInBytes);
8644 vixl32::Label return_address;
8645 EmitAdrCode adr(GetVIXLAssembler(), lr, &return_address);
8646 __ cmp(mr, Operand(0));
8647 EmitPlaceholderBne(this, bne_label);
8648 ptrdiff_t old_offset = GetVIXLAssembler()->GetBuffer()->GetCursorOffset();
8649 __ ldr(ref_reg, MemOperand(data_reg, index_reg, vixl32::LSL, scale_factor));
8650 DCHECK(!needs_null_check); // The thunk cannot handle the null check.
8651 // Note: We need a Wide NEG for the unpoisoning.
8652 if (kPoisonHeapReferences) {
8653 __ rsb(EncodingSize(Wide), ref_reg, ref_reg, Operand(0));
8654 }
8655 __ Bind(&return_address);
8656 DCHECK_EQ(old_offset - GetVIXLAssembler()->GetBuffer()->GetCursorOffset(),
8657 BAKER_MARK_INTROSPECTION_ARRAY_LDR_OFFSET);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008658 }
Roland Levillain5daa4952017-07-03 17:23:56 +01008659 MaybeGenerateMarkingRegisterCheck(/* code */ 20, /* temp_loc */ LocationFrom(ip));
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008660 return;
8661 }
8662
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008663 // /* HeapReference<Object> */ ref =
8664 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008665 GenerateReferenceLoadWithBakerReadBarrier(
8666 instruction, ref, obj, data_offset, index, scale_factor, temp, needs_null_check);
Roland Levillain6070e882016-11-03 17:51:58 +00008667}
8668
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008669void CodeGeneratorARMVIXL::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
8670 Location ref,
8671 vixl32::Register obj,
8672 uint32_t offset,
8673 Location index,
8674 ScaleFactor scale_factor,
8675 Location temp,
Roland Levillainff487002017-03-07 16:50:01 +00008676 bool needs_null_check) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008677 DCHECK(kEmitCompilerReadBarrier);
8678 DCHECK(kUseBakerReadBarrier);
8679
Roland Levillain6d729a72017-06-30 18:34:01 +01008680 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in the
8681 // Marking Register) to decide whether we need to enter the slow
8682 // path to mark the reference. Then, in the slow path, check the
8683 // gray bit in the lock word of the reference's holder (`obj`) to
8684 // decide whether to mark `ref` or not.
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008685 //
Roland Levillain6d729a72017-06-30 18:34:01 +01008686 // if (mr) { // Thread::Current()->GetIsGcMarking()
Roland Levillainff487002017-03-07 16:50:01 +00008687 // // Slow path.
8688 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
8689 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
8690 // HeapReference<mirror::Object> ref = *src; // Original reference load.
8691 // bool is_gray = (rb_state == ReadBarrier::GrayState());
8692 // if (is_gray) {
Roland Levillain6d729a72017-06-30 18:34:01 +01008693 // entrypoint = Thread::Current()->pReadBarrierMarkReg ## root.reg()
8694 // ref = entrypoint(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
Roland Levillainff487002017-03-07 16:50:01 +00008695 // }
8696 // } else {
8697 // HeapReference<mirror::Object> ref = *src; // Original reference load.
8698 // }
8699
8700 vixl32::Register temp_reg = RegisterFrom(temp);
8701
8702 // Slow path marking the object `ref` when the GC is marking. The
Roland Levillain6d729a72017-06-30 18:34:01 +01008703 // entrypoint will be loaded by the slow path code.
Roland Levillainff487002017-03-07 16:50:01 +00008704 SlowPathCodeARMVIXL* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01008705 new (GetScopedAllocator()) LoadReferenceWithBakerReadBarrierSlowPathARMVIXL(
Roland Levillain6d729a72017-06-30 18:34:01 +01008706 instruction, ref, obj, offset, index, scale_factor, needs_null_check, temp_reg);
Roland Levillainff487002017-03-07 16:50:01 +00008707 AddSlowPath(slow_path);
8708
Roland Levillain6d729a72017-06-30 18:34:01 +01008709 __ CompareAndBranchIfNonZero(mr, slow_path->GetEntryLabel());
Roland Levillainff487002017-03-07 16:50:01 +00008710 // Fast path: the GC is not marking: just load the reference.
8711 GenerateRawReferenceLoad(instruction, ref, obj, offset, index, scale_factor, needs_null_check);
8712 __ Bind(slow_path->GetExitLabel());
Roland Levillain5daa4952017-07-03 17:23:56 +01008713 MaybeGenerateMarkingRegisterCheck(/* code */ 21);
Roland Levillainff487002017-03-07 16:50:01 +00008714}
8715
8716void CodeGeneratorARMVIXL::UpdateReferenceFieldWithBakerReadBarrier(HInstruction* instruction,
8717 Location ref,
8718 vixl32::Register obj,
8719 Location field_offset,
8720 Location temp,
8721 bool needs_null_check,
8722 vixl32::Register temp2) {
8723 DCHECK(kEmitCompilerReadBarrier);
8724 DCHECK(kUseBakerReadBarrier);
8725
Roland Levillain6d729a72017-06-30 18:34:01 +01008726 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in the
8727 // Marking Register) to decide whether we need to enter the slow
8728 // path to update the reference field within `obj`. Then, in the
8729 // slow path, check the gray bit in the lock word of the reference's
8730 // holder (`obj`) to decide whether to mark `ref` and update the
8731 // field or not.
Roland Levillainff487002017-03-07 16:50:01 +00008732 //
Roland Levillain6d729a72017-06-30 18:34:01 +01008733 // if (mr) { // Thread::Current()->GetIsGcMarking()
Roland Levillainba650a42017-03-06 13:52:32 +00008734 // // Slow path.
Roland Levillain54f869e2017-03-06 13:54:11 +00008735 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
8736 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
Roland Levillainff487002017-03-07 16:50:01 +00008737 // HeapReference<mirror::Object> ref = *(obj + field_offset); // Reference load.
Roland Levillain54f869e2017-03-06 13:54:11 +00008738 // bool is_gray = (rb_state == ReadBarrier::GrayState());
8739 // if (is_gray) {
Roland Levillainff487002017-03-07 16:50:01 +00008740 // old_ref = ref;
Roland Levillain6d729a72017-06-30 18:34:01 +01008741 // entrypoint = Thread::Current()->pReadBarrierMarkReg ## root.reg()
8742 // ref = entrypoint(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
Roland Levillainff487002017-03-07 16:50:01 +00008743 // compareAndSwapObject(obj, field_offset, old_ref, ref);
Roland Levillain54f869e2017-03-06 13:54:11 +00008744 // }
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008745 // }
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008746
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008747 vixl32::Register temp_reg = RegisterFrom(temp);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008748
Roland Levillainff487002017-03-07 16:50:01 +00008749 // Slow path updating the object reference at address `obj + field_offset`
Roland Levillain6d729a72017-06-30 18:34:01 +01008750 // when the GC is marking. The entrypoint will be loaded by the slow path code.
Vladimir Marko174b2e22017-10-12 13:34:49 +01008751 SlowPathCodeARMVIXL* slow_path =
8752 new (GetScopedAllocator()) LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARMVIXL(
Roland Levillainff487002017-03-07 16:50:01 +00008753 instruction,
8754 ref,
8755 obj,
8756 /* offset */ 0u,
8757 /* index */ field_offset,
8758 /* scale_factor */ ScaleFactor::TIMES_1,
8759 needs_null_check,
8760 temp_reg,
Roland Levillain6d729a72017-06-30 18:34:01 +01008761 temp2);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008762 AddSlowPath(slow_path);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008763
Roland Levillain6d729a72017-06-30 18:34:01 +01008764 __ CompareAndBranchIfNonZero(mr, slow_path->GetEntryLabel());
Roland Levillainff487002017-03-07 16:50:01 +00008765 // Fast path: the GC is not marking: nothing to do (the field is
8766 // up-to-date, and we don't need to load the reference).
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008767 __ Bind(slow_path->GetExitLabel());
Roland Levillain5daa4952017-07-03 17:23:56 +01008768 MaybeGenerateMarkingRegisterCheck(/* code */ 22);
Roland Levillain844e6532016-11-03 16:09:47 +00008769}
Scott Wakelingfe885462016-09-22 10:24:38 +01008770
Roland Levillainba650a42017-03-06 13:52:32 +00008771void CodeGeneratorARMVIXL::GenerateRawReferenceLoad(HInstruction* instruction,
8772 Location ref,
8773 vixl::aarch32::Register obj,
8774 uint32_t offset,
8775 Location index,
8776 ScaleFactor scale_factor,
8777 bool needs_null_check) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008778 DataType::Type type = DataType::Type::kReference;
Roland Levillainba650a42017-03-06 13:52:32 +00008779 vixl32::Register ref_reg = RegisterFrom(ref, type);
8780
8781 // If needed, vixl::EmissionCheckScope guards are used to ensure
8782 // that no pools are emitted between the load (macro) instruction
8783 // and MaybeRecordImplicitNullCheck.
8784
Scott Wakelingfe885462016-09-22 10:24:38 +01008785 if (index.IsValid()) {
8786 // Load types involving an "index": ArrayGet,
8787 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
8788 // intrinsics.
Roland Levillainba650a42017-03-06 13:52:32 +00008789 // /* HeapReference<mirror::Object> */ ref = *(obj + offset + (index << scale_factor))
Scott Wakelingfe885462016-09-22 10:24:38 +01008790 if (index.IsConstant()) {
8791 size_t computed_offset =
8792 (Int32ConstantFrom(index) << scale_factor) + offset;
Roland Levillainba650a42017-03-06 13:52:32 +00008793 vixl::EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Scott Wakelingfe885462016-09-22 10:24:38 +01008794 GetAssembler()->LoadFromOffset(kLoadWord, ref_reg, obj, computed_offset);
Roland Levillainba650a42017-03-06 13:52:32 +00008795 if (needs_null_check) {
8796 MaybeRecordImplicitNullCheck(instruction);
8797 }
Scott Wakelingfe885462016-09-22 10:24:38 +01008798 } else {
8799 // Handle the special case of the
8800 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
8801 // intrinsics, which use a register pair as index ("long
8802 // offset"), of which only the low part contains data.
8803 vixl32::Register index_reg = index.IsRegisterPair()
8804 ? LowRegisterFrom(index)
8805 : RegisterFrom(index);
8806 UseScratchRegisterScope temps(GetVIXLAssembler());
Roland Levillainba650a42017-03-06 13:52:32 +00008807 vixl32::Register temp = temps.Acquire();
8808 __ Add(temp, obj, Operand(index_reg, ShiftType::LSL, scale_factor));
8809 {
8810 vixl::EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
8811 GetAssembler()->LoadFromOffset(kLoadWord, ref_reg, temp, offset);
8812 if (needs_null_check) {
8813 MaybeRecordImplicitNullCheck(instruction);
8814 }
8815 }
Scott Wakelingfe885462016-09-22 10:24:38 +01008816 }
8817 } else {
Roland Levillainba650a42017-03-06 13:52:32 +00008818 // /* HeapReference<mirror::Object> */ ref = *(obj + offset)
8819 vixl::EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Scott Wakelingfe885462016-09-22 10:24:38 +01008820 GetAssembler()->LoadFromOffset(kLoadWord, ref_reg, obj, offset);
Roland Levillainba650a42017-03-06 13:52:32 +00008821 if (needs_null_check) {
8822 MaybeRecordImplicitNullCheck(instruction);
8823 }
Scott Wakelingfe885462016-09-22 10:24:38 +01008824 }
8825
Roland Levillain844e6532016-11-03 16:09:47 +00008826 // Object* ref = ref_addr->AsMirrorPtr()
8827 GetAssembler()->MaybeUnpoisonHeapReference(ref_reg);
Roland Levillain844e6532016-11-03 16:09:47 +00008828}
8829
Roland Levillain5daa4952017-07-03 17:23:56 +01008830void CodeGeneratorARMVIXL::MaybeGenerateMarkingRegisterCheck(int code, Location temp_loc) {
8831 // The following condition is a compile-time one, so it does not have a run-time cost.
8832 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier && kIsDebugBuild) {
8833 // The following condition is a run-time one; it is executed after the
8834 // previous compile-time test, to avoid penalizing non-debug builds.
8835 if (GetCompilerOptions().EmitRunTimeChecksInDebugMode()) {
8836 UseScratchRegisterScope temps(GetVIXLAssembler());
8837 vixl32::Register temp = temp_loc.IsValid() ? RegisterFrom(temp_loc) : temps.Acquire();
8838 GetAssembler()->GenerateMarkingRegisterCheck(temp,
8839 kMarkingRegisterCheckBreakCodeBaseCode + code);
8840 }
8841 }
8842}
8843
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008844void CodeGeneratorARMVIXL::GenerateReadBarrierSlow(HInstruction* instruction,
8845 Location out,
8846 Location ref,
8847 Location obj,
8848 uint32_t offset,
8849 Location index) {
8850 DCHECK(kEmitCompilerReadBarrier);
8851
8852 // Insert a slow path based read barrier *after* the reference load.
8853 //
8854 // If heap poisoning is enabled, the unpoisoning of the loaded
8855 // reference will be carried out by the runtime within the slow
8856 // path.
8857 //
8858 // Note that `ref` currently does not get unpoisoned (when heap
8859 // poisoning is enabled), which is alright as the `ref` argument is
8860 // not used by the artReadBarrierSlow entry point.
8861 //
8862 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Vladimir Marko174b2e22017-10-12 13:34:49 +01008863 SlowPathCodeARMVIXL* slow_path = new (GetScopedAllocator())
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008864 ReadBarrierForHeapReferenceSlowPathARMVIXL(instruction, out, ref, obj, offset, index);
8865 AddSlowPath(slow_path);
8866
8867 __ B(slow_path->GetEntryLabel());
8868 __ Bind(slow_path->GetExitLabel());
8869}
8870
8871void CodeGeneratorARMVIXL::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
Artem Serov02d37832016-10-25 15:25:33 +01008872 Location out,
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008873 Location ref,
8874 Location obj,
8875 uint32_t offset,
8876 Location index) {
Artem Serov02d37832016-10-25 15:25:33 +01008877 if (kEmitCompilerReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008878 // Baker's read barriers shall be handled by the fast path
Roland Levillain9983e302017-07-14 14:34:22 +01008879 // (CodeGeneratorARMVIXL::GenerateReferenceLoadWithBakerReadBarrier).
Artem Serov02d37832016-10-25 15:25:33 +01008880 DCHECK(!kUseBakerReadBarrier);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008881 // If heap poisoning is enabled, unpoisoning will be taken care of
8882 // by the runtime within the slow path.
8883 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Artem Serov02d37832016-10-25 15:25:33 +01008884 } else if (kPoisonHeapReferences) {
8885 GetAssembler()->UnpoisonHeapReference(RegisterFrom(out));
8886 }
8887}
8888
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008889void CodeGeneratorARMVIXL::GenerateReadBarrierForRootSlow(HInstruction* instruction,
8890 Location out,
8891 Location root) {
8892 DCHECK(kEmitCompilerReadBarrier);
8893
8894 // Insert a slow path based read barrier *after* the GC root load.
8895 //
8896 // Note that GC roots are not affected by heap poisoning, so we do
8897 // not need to do anything special for this here.
8898 SlowPathCodeARMVIXL* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01008899 new (GetScopedAllocator()) ReadBarrierForRootSlowPathARMVIXL(instruction, out, root);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008900 AddSlowPath(slow_path);
8901
8902 __ B(slow_path->GetEntryLabel());
8903 __ Bind(slow_path->GetExitLabel());
8904}
8905
Artem Serov02d37832016-10-25 15:25:33 +01008906// Check if the desired_dispatch_info is supported. If it is, return it,
8907// otherwise return a fall-back info that should be used instead.
8908HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARMVIXL::GetSupportedInvokeStaticOrDirectDispatch(
Artem Serovd4cc5b22016-11-04 11:19:09 +00008909 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +00008910 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Nicolas Geoffraye807ff72017-01-23 09:03:12 +00008911 return desired_dispatch_info;
Artem Serov02d37832016-10-25 15:25:33 +01008912}
8913
Scott Wakelingfe885462016-09-22 10:24:38 +01008914vixl32::Register CodeGeneratorARMVIXL::GetInvokeStaticOrDirectExtraParameter(
8915 HInvokeStaticOrDirect* invoke, vixl32::Register temp) {
8916 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
8917 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
8918 if (!invoke->GetLocations()->Intrinsified()) {
8919 return RegisterFrom(location);
8920 }
8921 // For intrinsics we allow any location, so it may be on the stack.
8922 if (!location.IsRegister()) {
8923 GetAssembler()->LoadFromOffset(kLoadWord, temp, sp, location.GetStackIndex());
8924 return temp;
8925 }
8926 // For register locations, check if the register was saved. If so, get it from the stack.
8927 // Note: There is a chance that the register was saved but not overwritten, so we could
8928 // save one load. However, since this is just an intrinsic slow path we prefer this
8929 // simple and more robust approach rather that trying to determine if that's the case.
8930 SlowPathCode* slow_path = GetCurrentSlowPath();
Scott Wakelingd5cd4972017-02-03 11:38:35 +00008931 if (slow_path != nullptr && slow_path->IsCoreRegisterSaved(RegisterFrom(location).GetCode())) {
Scott Wakelingfe885462016-09-22 10:24:38 +01008932 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(RegisterFrom(location).GetCode());
8933 GetAssembler()->LoadFromOffset(kLoadWord, temp, sp, stack_offset);
8934 return temp;
8935 }
8936 return RegisterFrom(location);
8937}
8938
Vladimir Markod254f5c2017-06-02 15:18:36 +00008939void CodeGeneratorARMVIXL::GenerateStaticOrDirectCall(
Vladimir Markoe7197bf2017-06-02 17:00:23 +01008940 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00008941 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
Scott Wakelingfe885462016-09-22 10:24:38 +01008942 switch (invoke->GetMethodLoadKind()) {
8943 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
8944 uint32_t offset =
8945 GetThreadOffset<kArmPointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
8946 // temp = thread->string_init_entrypoint
Artem Serovd4cc5b22016-11-04 11:19:09 +00008947 GetAssembler()->LoadFromOffset(kLoadWord, RegisterFrom(temp), tr, offset);
8948 break;
8949 }
8950 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
8951 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
8952 break;
Vladimir Marko65979462017-05-19 17:25:12 +01008953 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative: {
8954 DCHECK(GetCompilerOptions().IsBootImage());
8955 PcRelativePatchInfo* labels = NewPcRelativeMethodPatch(invoke->GetTargetMethod());
8956 vixl32::Register temp_reg = RegisterFrom(temp);
8957 EmitMovwMovtPlaceholder(labels, temp_reg);
8958 break;
8959 }
Artem Serovd4cc5b22016-11-04 11:19:09 +00008960 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
8961 __ Mov(RegisterFrom(temp), Operand::From(invoke->GetMethodAddress()));
8962 break;
Vladimir Marko0eb882b2017-05-15 13:39:18 +01008963 case HInvokeStaticOrDirect::MethodLoadKind::kBssEntry: {
8964 PcRelativePatchInfo* labels = NewMethodBssEntryPatch(
8965 MethodReference(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex()));
8966 vixl32::Register temp_reg = RegisterFrom(temp);
8967 EmitMovwMovtPlaceholder(labels, temp_reg);
8968 GetAssembler()->LoadFromOffset(kLoadWord, temp_reg, temp_reg, /* offset*/ 0);
Scott Wakelingfe885462016-09-22 10:24:38 +01008969 break;
8970 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01008971 case HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall: {
8972 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
8973 return; // No code pointer retrieval; the runtime performs the call directly.
Scott Wakelingfe885462016-09-22 10:24:38 +01008974 }
Scott Wakelingfe885462016-09-22 10:24:38 +01008975 }
8976
Artem Serovd4cc5b22016-11-04 11:19:09 +00008977 switch (invoke->GetCodePtrLocation()) {
8978 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Vladimir Markoe7197bf2017-06-02 17:00:23 +01008979 {
8980 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
8981 ExactAssemblyScope aas(GetVIXLAssembler(),
8982 vixl32::k32BitT32InstructionSizeInBytes,
8983 CodeBufferCheckScope::kMaximumSize);
8984 __ bl(GetFrameEntryLabel());
8985 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
8986 }
Artem Serovd4cc5b22016-11-04 11:19:09 +00008987 break;
Artem Serovd4cc5b22016-11-04 11:19:09 +00008988 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
8989 // LR = callee_method->entry_point_from_quick_compiled_code_
8990 GetAssembler()->LoadFromOffset(
8991 kLoadWord,
8992 lr,
8993 RegisterFrom(callee_method),
8994 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize).Int32Value());
Alexandre Rames374ddf32016-11-04 10:40:49 +00008995 {
Vladimir Markoe7197bf2017-06-02 17:00:23 +01008996 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
Alexandre Rames374ddf32016-11-04 10:40:49 +00008997 // 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 +00008998 ExactAssemblyScope aas(GetVIXLAssembler(),
8999 vixl32::k16BitT32InstructionSizeInBytes,
9000 CodeBufferCheckScope::kExactSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00009001 // LR()
9002 __ blx(lr);
Vladimir Markoe7197bf2017-06-02 17:00:23 +01009003 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Alexandre Rames374ddf32016-11-04 10:40:49 +00009004 }
Artem Serovd4cc5b22016-11-04 11:19:09 +00009005 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01009006 }
9007
Scott Wakelingfe885462016-09-22 10:24:38 +01009008 DCHECK(!IsLeafMethod());
9009}
9010
Vladimir Markoe7197bf2017-06-02 17:00:23 +01009011void CodeGeneratorARMVIXL::GenerateVirtualCall(
9012 HInvokeVirtual* invoke, Location temp_location, SlowPathCode* slow_path) {
Scott Wakelingfe885462016-09-22 10:24:38 +01009013 vixl32::Register temp = RegisterFrom(temp_location);
9014 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
9015 invoke->GetVTableIndex(), kArmPointerSize).Uint32Value();
9016
9017 // Use the calling convention instead of the location of the receiver, as
9018 // intrinsics may have put the receiver in a different register. In the intrinsics
9019 // slow path, the arguments have been moved to the right place, so here we are
9020 // guaranteed that the receiver is the first register of the calling convention.
9021 InvokeDexCallingConventionARMVIXL calling_convention;
9022 vixl32::Register receiver = calling_convention.GetRegisterAt(0);
9023 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Alexandre Rames374ddf32016-11-04 10:40:49 +00009024 {
9025 // Make sure the pc is recorded immediately after the `ldr` instruction.
Artem Serov0fb37192016-12-06 18:13:40 +00009026 ExactAssemblyScope aas(GetVIXLAssembler(),
9027 vixl32::kMaxInstructionSizeInBytes,
9028 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00009029 // /* HeapReference<Class> */ temp = receiver->klass_
9030 __ ldr(temp, MemOperand(receiver, class_offset));
9031 MaybeRecordImplicitNullCheck(invoke);
9032 }
Scott Wakelingfe885462016-09-22 10:24:38 +01009033 // Instead of simply (possibly) unpoisoning `temp` here, we should
9034 // emit a read barrier for the previous class reference load.
9035 // However this is not required in practice, as this is an
9036 // intermediate/temporary reference and because the current
9037 // concurrent copying collector keeps the from-space memory
9038 // intact/accessible until the end of the marking phase (the
9039 // concurrent copying collector may not in the future).
9040 GetAssembler()->MaybeUnpoisonHeapReference(temp);
9041
9042 // temp = temp->GetMethodAt(method_offset);
9043 uint32_t entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(
9044 kArmPointerSize).Int32Value();
9045 GetAssembler()->LoadFromOffset(kLoadWord, temp, temp, method_offset);
9046 // LR = temp->GetEntryPoint();
9047 GetAssembler()->LoadFromOffset(kLoadWord, lr, temp, entry_point);
Vladimir Markoe7197bf2017-06-02 17:00:23 +01009048 {
9049 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
9050 // blx in T32 has only 16bit encoding that's why a stricter check for the scope is used.
9051 ExactAssemblyScope aas(GetVIXLAssembler(),
9052 vixl32::k16BitT32InstructionSizeInBytes,
9053 CodeBufferCheckScope::kExactSize);
9054 // LR();
9055 __ blx(lr);
9056 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
9057 }
Scott Wakelingfe885462016-09-22 10:24:38 +01009058}
9059
Vladimir Marko65979462017-05-19 17:25:12 +01009060CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewPcRelativeMethodPatch(
9061 MethodReference target_method) {
9062 return NewPcRelativePatch(*target_method.dex_file,
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07009063 target_method.index,
Vladimir Marko65979462017-05-19 17:25:12 +01009064 &pc_relative_method_patches_);
Artem Serovd4cc5b22016-11-04 11:19:09 +00009065}
9066
Vladimir Marko0eb882b2017-05-15 13:39:18 +01009067CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewMethodBssEntryPatch(
9068 MethodReference target_method) {
9069 return NewPcRelativePatch(*target_method.dex_file,
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07009070 target_method.index,
Vladimir Marko0eb882b2017-05-15 13:39:18 +01009071 &method_bss_entry_patches_);
9072}
9073
Artem Serovd4cc5b22016-11-04 11:19:09 +00009074CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewPcRelativeTypePatch(
9075 const DexFile& dex_file, dex::TypeIndex type_index) {
9076 return NewPcRelativePatch(dex_file, type_index.index_, &pc_relative_type_patches_);
9077}
9078
Vladimir Marko1998cd02017-01-13 13:02:58 +00009079CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewTypeBssEntryPatch(
9080 const DexFile& dex_file, dex::TypeIndex type_index) {
9081 return NewPcRelativePatch(dex_file, type_index.index_, &type_bss_entry_patches_);
9082}
9083
Vladimir Marko65979462017-05-19 17:25:12 +01009084CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewPcRelativeStringPatch(
9085 const DexFile& dex_file, dex::StringIndex string_index) {
9086 return NewPcRelativePatch(dex_file, string_index.index_, &pc_relative_string_patches_);
9087}
9088
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01009089CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewStringBssEntryPatch(
9090 const DexFile& dex_file, dex::StringIndex string_index) {
9091 return NewPcRelativePatch(dex_file, string_index.index_, &string_bss_entry_patches_);
9092}
9093
Artem Serovd4cc5b22016-11-04 11:19:09 +00009094CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewPcRelativePatch(
9095 const DexFile& dex_file, uint32_t offset_or_index, ArenaDeque<PcRelativePatchInfo>* patches) {
9096 patches->emplace_back(dex_file, offset_or_index);
9097 return &patches->back();
9098}
9099
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01009100vixl::aarch32::Label* CodeGeneratorARMVIXL::NewBakerReadBarrierPatch(uint32_t custom_data) {
9101 baker_read_barrier_patches_.emplace_back(custom_data);
9102 return &baker_read_barrier_patches_.back().label;
9103}
9104
Artem Serovc5fcb442016-12-02 19:19:58 +00009105VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateBootImageAddressLiteral(uint32_t address) {
Richard Uhlerc52f3032017-03-02 13:45:45 +00009106 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), &uint32_literals_);
Artem Serovc5fcb442016-12-02 19:19:58 +00009107}
9108
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00009109VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateJitStringLiteral(
9110 const DexFile& dex_file,
9111 dex::StringIndex string_index,
9112 Handle<mirror::String> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01009113 ReserveJitStringRoot(StringReference(&dex_file, string_index), handle);
Artem Serovc5fcb442016-12-02 19:19:58 +00009114 return jit_string_patches_.GetOrCreate(
9115 StringReference(&dex_file, string_index),
9116 [this]() {
9117 return GetAssembler()->CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u);
9118 });
9119}
9120
9121VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateJitClassLiteral(const DexFile& dex_file,
9122 dex::TypeIndex type_index,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00009123 Handle<mirror::Class> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01009124 ReserveJitClassRoot(TypeReference(&dex_file, type_index), handle);
Artem Serovc5fcb442016-12-02 19:19:58 +00009125 return jit_class_patches_.GetOrCreate(
9126 TypeReference(&dex_file, type_index),
9127 [this]() {
9128 return GetAssembler()->CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u);
9129 });
9130}
9131
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01009132template <linker::LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
Artem Serovd4cc5b22016-11-04 11:19:09 +00009133inline void CodeGeneratorARMVIXL::EmitPcRelativeLinkerPatches(
9134 const ArenaDeque<PcRelativePatchInfo>& infos,
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01009135 ArenaVector<linker::LinkerPatch>* linker_patches) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00009136 for (const PcRelativePatchInfo& info : infos) {
9137 const DexFile& dex_file = info.target_dex_file;
9138 size_t offset_or_index = info.offset_or_index;
9139 DCHECK(info.add_pc_label.IsBound());
9140 uint32_t add_pc_offset = dchecked_integral_cast<uint32_t>(info.add_pc_label.GetLocation());
9141 // Add MOVW patch.
9142 DCHECK(info.movw_label.IsBound());
9143 uint32_t movw_offset = dchecked_integral_cast<uint32_t>(info.movw_label.GetLocation());
9144 linker_patches->push_back(Factory(movw_offset, &dex_file, add_pc_offset, offset_or_index));
9145 // Add MOVT patch.
9146 DCHECK(info.movt_label.IsBound());
9147 uint32_t movt_offset = dchecked_integral_cast<uint32_t>(info.movt_label.GetLocation());
9148 linker_patches->push_back(Factory(movt_offset, &dex_file, add_pc_offset, offset_or_index));
9149 }
9150}
9151
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01009152void CodeGeneratorARMVIXL::EmitLinkerPatches(ArenaVector<linker::LinkerPatch>* linker_patches) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00009153 DCHECK(linker_patches->empty());
9154 size_t size =
Vladimir Marko65979462017-05-19 17:25:12 +01009155 /* MOVW+MOVT for each entry */ 2u * pc_relative_method_patches_.size() +
Vladimir Marko0eb882b2017-05-15 13:39:18 +01009156 /* MOVW+MOVT for each entry */ 2u * method_bss_entry_patches_.size() +
Artem Serovc5fcb442016-12-02 19:19:58 +00009157 /* MOVW+MOVT for each entry */ 2u * pc_relative_type_patches_.size() +
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01009158 /* MOVW+MOVT for each entry */ 2u * type_bss_entry_patches_.size() +
Vladimir Marko65979462017-05-19 17:25:12 +01009159 /* MOVW+MOVT for each entry */ 2u * pc_relative_string_patches_.size() +
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01009160 /* MOVW+MOVT for each entry */ 2u * string_bss_entry_patches_.size() +
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01009161 baker_read_barrier_patches_.size();
Artem Serovd4cc5b22016-11-04 11:19:09 +00009162 linker_patches->reserve(size);
Vladimir Marko65979462017-05-19 17:25:12 +01009163 if (GetCompilerOptions().IsBootImage()) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01009164 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeMethodPatch>(
9165 pc_relative_method_patches_, linker_patches);
9166 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeTypePatch>(
9167 pc_relative_type_patches_, linker_patches);
9168 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeStringPatch>(
9169 pc_relative_string_patches_, linker_patches);
Vladimir Marko65979462017-05-19 17:25:12 +01009170 } else {
9171 DCHECK(pc_relative_method_patches_.empty());
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01009172 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeClassTablePatch>(
9173 pc_relative_type_patches_, linker_patches);
9174 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringInternTablePatch>(
9175 pc_relative_string_patches_, linker_patches);
Artem Serovd4cc5b22016-11-04 11:19:09 +00009176 }
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01009177 EmitPcRelativeLinkerPatches<linker::LinkerPatch::MethodBssEntryPatch>(
9178 method_bss_entry_patches_, linker_patches);
9179 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeBssEntryPatch>(
9180 type_bss_entry_patches_, linker_patches);
9181 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringBssEntryPatch>(
9182 string_bss_entry_patches_, linker_patches);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01009183 for (const BakerReadBarrierPatchInfo& info : baker_read_barrier_patches_) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01009184 linker_patches->push_back(linker::LinkerPatch::BakerReadBarrierBranchPatch(
9185 info.label.GetLocation(), info.custom_data));
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01009186 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00009187 DCHECK_EQ(size, linker_patches->size());
Artem Serovc5fcb442016-12-02 19:19:58 +00009188}
9189
9190VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateUint32Literal(
9191 uint32_t value,
9192 Uint32ToLiteralMap* map) {
9193 return map->GetOrCreate(
9194 value,
9195 [this, value]() {
9196 return GetAssembler()->CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ value);
9197 });
9198}
9199
Artem Serov2bbc9532016-10-21 11:51:50 +01009200void LocationsBuilderARMVIXL::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
9201 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01009202 new (GetGraph()->GetAllocator()) LocationSummary(instr, LocationSummary::kNoCall);
Artem Serov2bbc9532016-10-21 11:51:50 +01009203 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
9204 Location::RequiresRegister());
9205 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
9206 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
9207 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
9208}
9209
9210void InstructionCodeGeneratorARMVIXL::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
9211 vixl32::Register res = OutputRegister(instr);
9212 vixl32::Register accumulator =
9213 InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
9214 vixl32::Register mul_left =
9215 InputRegisterAt(instr, HMultiplyAccumulate::kInputMulLeftIndex);
9216 vixl32::Register mul_right =
9217 InputRegisterAt(instr, HMultiplyAccumulate::kInputMulRightIndex);
9218
9219 if (instr->GetOpKind() == HInstruction::kAdd) {
9220 __ Mla(res, mul_left, mul_right, accumulator);
9221 } else {
9222 __ Mls(res, mul_left, mul_right, accumulator);
9223 }
9224}
9225
Artem Serov551b28f2016-10-18 19:11:30 +01009226void LocationsBuilderARMVIXL::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
9227 // Nothing to do, this should be removed during prepare for register allocator.
9228 LOG(FATAL) << "Unreachable";
9229}
9230
9231void InstructionCodeGeneratorARMVIXL::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
9232 // Nothing to do, this should be removed during prepare for register allocator.
9233 LOG(FATAL) << "Unreachable";
9234}
9235
9236// Simple implementation of packed switch - generate cascaded compare/jumps.
9237void LocationsBuilderARMVIXL::VisitPackedSwitch(HPackedSwitch* switch_instr) {
9238 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01009239 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Artem Serov551b28f2016-10-18 19:11:30 +01009240 locations->SetInAt(0, Location::RequiresRegister());
9241 if (switch_instr->GetNumEntries() > kPackedSwitchCompareJumpThreshold &&
9242 codegen_->GetAssembler()->GetVIXLAssembler()->IsUsingT32()) {
9243 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the table base.
9244 if (switch_instr->GetStartValue() != 0) {
9245 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the bias.
9246 }
9247 }
9248}
9249
9250// TODO(VIXL): Investigate and reach the parity with old arm codegen.
9251void InstructionCodeGeneratorARMVIXL::VisitPackedSwitch(HPackedSwitch* switch_instr) {
9252 int32_t lower_bound = switch_instr->GetStartValue();
9253 uint32_t num_entries = switch_instr->GetNumEntries();
9254 LocationSummary* locations = switch_instr->GetLocations();
9255 vixl32::Register value_reg = InputRegisterAt(switch_instr, 0);
9256 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
9257
9258 if (num_entries <= kPackedSwitchCompareJumpThreshold ||
9259 !codegen_->GetAssembler()->GetVIXLAssembler()->IsUsingT32()) {
9260 // Create a series of compare/jumps.
Anton Kirilovedb2ac32016-11-30 15:14:10 +00009261 UseScratchRegisterScope temps(GetVIXLAssembler());
Artem Serov551b28f2016-10-18 19:11:30 +01009262 vixl32::Register temp_reg = temps.Acquire();
9263 // Note: It is fine for the below AddConstantSetFlags() using IP register to temporarily store
9264 // the immediate, because IP is used as the destination register. For the other
9265 // AddConstantSetFlags() and GenerateCompareWithImmediate(), the immediate values are constant,
9266 // and they can be encoded in the instruction without making use of IP register.
9267 __ Adds(temp_reg, value_reg, -lower_bound);
9268
9269 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
9270 // Jump to successors[0] if value == lower_bound.
9271 __ B(eq, codegen_->GetLabelOf(successors[0]));
9272 int32_t last_index = 0;
9273 for (; num_entries - last_index > 2; last_index += 2) {
9274 __ Adds(temp_reg, temp_reg, -2);
9275 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
9276 __ B(lo, codegen_->GetLabelOf(successors[last_index + 1]));
9277 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
9278 __ B(eq, codegen_->GetLabelOf(successors[last_index + 2]));
9279 }
9280 if (num_entries - last_index == 2) {
9281 // The last missing case_value.
9282 __ Cmp(temp_reg, 1);
9283 __ B(eq, codegen_->GetLabelOf(successors[last_index + 1]));
9284 }
9285
9286 // And the default for any other value.
9287 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
9288 __ B(codegen_->GetLabelOf(default_block));
9289 }
9290 } else {
9291 // Create a table lookup.
9292 vixl32::Register table_base = RegisterFrom(locations->GetTemp(0));
9293
9294 JumpTableARMVIXL* jump_table = codegen_->CreateJumpTable(switch_instr);
9295
9296 // Remove the bias.
9297 vixl32::Register key_reg;
9298 if (lower_bound != 0) {
9299 key_reg = RegisterFrom(locations->GetTemp(1));
9300 __ Sub(key_reg, value_reg, lower_bound);
9301 } else {
9302 key_reg = value_reg;
9303 }
9304
9305 // Check whether the value is in the table, jump to default block if not.
9306 __ Cmp(key_reg, num_entries - 1);
9307 __ B(hi, codegen_->GetLabelOf(default_block));
9308
Anton Kirilovedb2ac32016-11-30 15:14:10 +00009309 UseScratchRegisterScope temps(GetVIXLAssembler());
Artem Serov551b28f2016-10-18 19:11:30 +01009310 vixl32::Register jump_offset = temps.Acquire();
9311
9312 // Load jump offset from the table.
Scott Wakeling86e9d262017-01-18 15:59:24 +00009313 {
9314 const size_t jump_size = switch_instr->GetNumEntries() * sizeof(int32_t);
9315 ExactAssemblyScope aas(GetVIXLAssembler(),
9316 (vixl32::kMaxInstructionSizeInBytes * 4) + jump_size,
9317 CodeBufferCheckScope::kMaximumSize);
9318 __ adr(table_base, jump_table->GetTableStartLabel());
9319 __ ldr(jump_offset, MemOperand(table_base, key_reg, vixl32::LSL, 2));
Artem Serov551b28f2016-10-18 19:11:30 +01009320
Scott Wakeling86e9d262017-01-18 15:59:24 +00009321 // Jump to target block by branching to table_base(pc related) + offset.
9322 vixl32::Register target_address = table_base;
9323 __ add(target_address, table_base, jump_offset);
9324 __ bx(target_address);
Artem Serov09a940d2016-11-11 16:15:11 +00009325
Scott Wakeling86e9d262017-01-18 15:59:24 +00009326 jump_table->EmitTable(codegen_);
9327 }
Artem Serov551b28f2016-10-18 19:11:30 +01009328 }
9329}
9330
Artem Serov02d37832016-10-25 15:25:33 +01009331// Copy the result of a call into the given target.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009332void CodeGeneratorARMVIXL::MoveFromReturnRegister(Location trg, DataType::Type type) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01009333 if (!trg.IsValid()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009334 DCHECK_EQ(type, DataType::Type::kVoid);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01009335 return;
9336 }
9337
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009338 DCHECK_NE(type, DataType::Type::kVoid);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01009339
Artem Serovd4cc5b22016-11-04 11:19:09 +00009340 Location return_loc = InvokeDexCallingConventionVisitorARMVIXL().GetReturnLocation(type);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01009341 if (return_loc.Equals(trg)) {
9342 return;
9343 }
9344
9345 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
9346 // with the last branch.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009347 if (type == DataType::Type::kInt64) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01009348 TODO_VIXL32(FATAL);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009349 } else if (type == DataType::Type::kFloat64) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01009350 TODO_VIXL32(FATAL);
9351 } else {
9352 // Let the parallel move resolver take care of all of this.
Vladimir Markoca6fff82017-10-03 14:49:14 +01009353 HParallelMove parallel_move(GetGraph()->GetAllocator());
Anton Kirilove28d9ae2016-10-25 18:17:23 +01009354 parallel_move.AddMove(return_loc, trg, type, nullptr);
9355 GetMoveResolver()->EmitNativeCode(&parallel_move);
9356 }
Scott Wakelingfe885462016-09-22 10:24:38 +01009357}
9358
xueliang.zhong8d2c4592016-11-23 17:05:25 +00009359void LocationsBuilderARMVIXL::VisitClassTableGet(HClassTableGet* instruction) {
9360 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01009361 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
xueliang.zhong8d2c4592016-11-23 17:05:25 +00009362 locations->SetInAt(0, Location::RequiresRegister());
9363 locations->SetOut(Location::RequiresRegister());
Artem Serov551b28f2016-10-18 19:11:30 +01009364}
9365
xueliang.zhong8d2c4592016-11-23 17:05:25 +00009366void InstructionCodeGeneratorARMVIXL::VisitClassTableGet(HClassTableGet* instruction) {
9367 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
9368 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
9369 instruction->GetIndex(), kArmPointerSize).SizeValue();
9370 GetAssembler()->LoadFromOffset(kLoadWord,
9371 OutputRegister(instruction),
9372 InputRegisterAt(instruction, 0),
9373 method_offset);
9374 } else {
9375 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
9376 instruction->GetIndex(), kArmPointerSize));
9377 GetAssembler()->LoadFromOffset(kLoadWord,
9378 OutputRegister(instruction),
9379 InputRegisterAt(instruction, 0),
9380 mirror::Class::ImtPtrOffset(kArmPointerSize).Uint32Value());
9381 GetAssembler()->LoadFromOffset(kLoadWord,
9382 OutputRegister(instruction),
9383 OutputRegister(instruction),
9384 method_offset);
9385 }
Artem Serov551b28f2016-10-18 19:11:30 +01009386}
9387
Artem Serovc5fcb442016-12-02 19:19:58 +00009388static void PatchJitRootUse(uint8_t* code,
9389 const uint8_t* roots_data,
9390 VIXLUInt32Literal* literal,
9391 uint64_t index_in_table) {
9392 DCHECK(literal->IsBound());
9393 uint32_t literal_offset = literal->GetLocation();
9394 uintptr_t address =
9395 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
9396 uint8_t* data = code + literal_offset;
9397 reinterpret_cast<uint32_t*>(data)[0] = dchecked_integral_cast<uint32_t>(address);
9398}
9399
9400void CodeGeneratorARMVIXL::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
9401 for (const auto& entry : jit_string_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01009402 const StringReference& string_reference = entry.first;
9403 VIXLUInt32Literal* table_entry_literal = entry.second;
Vladimir Marko174b2e22017-10-12 13:34:49 +01009404 uint64_t index_in_table = GetJitStringRootIndex(string_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01009405 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Artem Serovc5fcb442016-12-02 19:19:58 +00009406 }
9407 for (const auto& entry : jit_class_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01009408 const TypeReference& type_reference = entry.first;
9409 VIXLUInt32Literal* table_entry_literal = entry.second;
Vladimir Marko174b2e22017-10-12 13:34:49 +01009410 uint64_t index_in_table = GetJitClassRootIndex(type_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01009411 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Artem Serovc5fcb442016-12-02 19:19:58 +00009412 }
9413}
9414
Artem Serovd4cc5b22016-11-04 11:19:09 +00009415void CodeGeneratorARMVIXL::EmitMovwMovtPlaceholder(
9416 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels,
9417 vixl32::Register out) {
Artem Serov0fb37192016-12-06 18:13:40 +00009418 ExactAssemblyScope aas(GetVIXLAssembler(),
9419 3 * vixl32::kMaxInstructionSizeInBytes,
9420 CodeBufferCheckScope::kMaximumSize);
Artem Serovd4cc5b22016-11-04 11:19:09 +00009421 // TODO(VIXL): Think about using mov instead of movw.
9422 __ bind(&labels->movw_label);
9423 __ movw(out, /* placeholder */ 0u);
9424 __ bind(&labels->movt_label);
9425 __ movt(out, /* placeholder */ 0u);
9426 __ bind(&labels->add_pc_label);
9427 __ add(out, out, pc);
9428}
9429
Scott Wakelingfe885462016-09-22 10:24:38 +01009430#undef __
9431#undef QUICK_ENTRY_POINT
9432#undef TODO_VIXL32
9433
9434} // namespace arm
9435} // namespace art