blob: 4fef027e6d1c966240fcc7a02c75e16de42065c0 [file] [log] [blame]
Scott Wakelingfe885462016-09-22 10:24:38 +01001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_arm_vixl.h"
18
Vladimir Markoeee1c0e2017-04-21 17:58:41 +010019#include "arch/arm/asm_support_arm.h"
Scott Wakelingfe885462016-09-22 10:24:38 +010020#include "arch/arm/instruction_set_features_arm.h"
21#include "art_method.h"
Andreas Gampe5678db52017-06-08 14:11:18 -070022#include "base/bit_utils.h"
23#include "base/bit_utils_iterator.h"
Vladimir Marko94ec2db2017-09-06 17:21:03 +010024#include "class_table.h"
Scott Wakelingfe885462016-09-22 10:24:38 +010025#include "code_generator_utils.h"
26#include "common_arm.h"
27#include "compiled_method.h"
28#include "entrypoints/quick/quick_entrypoints.h"
29#include "gc/accounting/card_table.h"
Andreas Gampe09659c22017-09-18 18:23:32 -070030#include "heap_poisoning.h"
Anton Kirilov5ec62182016-10-13 20:16:02 +010031#include "intrinsics_arm_vixl.h"
Vladimir Markoeee1c0e2017-04-21 17:58:41 +010032#include "linker/arm/relative_patcher_thumb2.h"
Vladimir Markod8dbc8d2017-09-20 13:37:47 +010033#include "linker/linker_patch.h"
Scott Wakelingfe885462016-09-22 10:24:38 +010034#include "mirror/array-inl.h"
35#include "mirror/class-inl.h"
36#include "thread.h"
37#include "utils/arm/assembler_arm_vixl.h"
38#include "utils/arm/managed_register_arm.h"
39#include "utils/assembler.h"
40#include "utils/stack_checks.h"
41
42namespace art {
43namespace arm {
44
45namespace vixl32 = vixl::aarch32;
46using namespace vixl32; // NOLINT(build/namespaces)
47
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +010048using helpers::DRegisterFrom;
Scott Wakelingfe885462016-09-22 10:24:38 +010049using helpers::DWARFReg;
Scott Wakelinga7812ae2016-10-17 10:03:36 +010050using helpers::HighDRegisterFrom;
51using helpers::HighRegisterFrom;
Donghui Bai426b49c2016-11-08 14:55:38 +080052using helpers::InputDRegisterAt;
Scott Wakelingfe885462016-09-22 10:24:38 +010053using helpers::InputOperandAt;
Scott Wakelingc34dba72016-10-03 10:14:44 +010054using helpers::InputRegister;
Scott Wakelinga7812ae2016-10-17 10:03:36 +010055using helpers::InputRegisterAt;
Scott Wakelingfe885462016-09-22 10:24:38 +010056using helpers::InputSRegisterAt;
Anton Kirilov644032c2016-12-06 17:51:43 +000057using helpers::InputVRegister;
Scott Wakelinga7812ae2016-10-17 10:03:36 +010058using helpers::InputVRegisterAt;
Scott Wakelingb77051e2016-11-21 19:46:00 +000059using helpers::Int32ConstantFrom;
Anton Kirilov644032c2016-12-06 17:51:43 +000060using helpers::Int64ConstantFrom;
Scott Wakelinga7812ae2016-10-17 10:03:36 +010061using helpers::LocationFrom;
62using helpers::LowRegisterFrom;
63using helpers::LowSRegisterFrom;
Donghui Bai426b49c2016-11-08 14:55:38 +080064using helpers::OperandFrom;
Scott Wakelinga7812ae2016-10-17 10:03:36 +010065using helpers::OutputRegister;
66using helpers::OutputSRegister;
67using helpers::OutputVRegister;
68using helpers::RegisterFrom;
69using helpers::SRegisterFrom;
Anton Kirilov644032c2016-12-06 17:51:43 +000070using helpers::Uint64ConstantFrom;
Scott Wakelingfe885462016-09-22 10:24:38 +010071
Artem Serov0fb37192016-12-06 18:13:40 +000072using vixl::ExactAssemblyScope;
73using vixl::CodeBufferCheckScope;
74
Scott Wakelingfe885462016-09-22 10:24:38 +010075using RegisterList = vixl32::RegisterList;
76
77static bool ExpectedPairLayout(Location location) {
78 // We expected this for both core and fpu register pairs.
79 return ((location.low() & 1) == 0) && (location.low() + 1 == location.high());
80}
Artem Serovd4cc5b22016-11-04 11:19:09 +000081// Use a local definition to prevent copying mistakes.
82static constexpr size_t kArmWordSize = static_cast<size_t>(kArmPointerSize);
83static constexpr size_t kArmBitsPerWord = kArmWordSize * kBitsPerByte;
Artem Serov551b28f2016-10-18 19:11:30 +010084static constexpr uint32_t kPackedSwitchCompareJumpThreshold = 7;
Scott Wakelingfe885462016-09-22 10:24:38 +010085
Vladimir Markoeee1c0e2017-04-21 17:58:41 +010086// Reference load (except object array loads) is using LDR Rt, [Rn, #offset] which can handle
87// offset < 4KiB. For offsets >= 4KiB, the load shall be emitted as two or more instructions.
88// For the Baker read barrier implementation using link-generated thunks we need to split
89// the offset explicitly.
90constexpr uint32_t kReferenceLoadMinFarOffset = 4 * KB;
91
92// Flags controlling the use of link-time generated thunks for Baker read barriers.
93constexpr bool kBakerReadBarrierLinkTimeThunksEnableForFields = true;
94constexpr bool kBakerReadBarrierLinkTimeThunksEnableForArrays = true;
95constexpr bool kBakerReadBarrierLinkTimeThunksEnableForGcRoots = true;
96
97// The reserved entrypoint register for link-time generated thunks.
98const vixl32::Register kBakerCcEntrypointRegister = r4;
99
Roland Levillain5daa4952017-07-03 17:23:56 +0100100// Using a base helps identify when we hit Marking Register check breakpoints.
101constexpr int kMarkingRegisterCheckBreakCodeBaseCode = 0x10;
102
Scott Wakelingfe885462016-09-22 10:24:38 +0100103#ifdef __
104#error "ARM Codegen VIXL macro-assembler macro already defined."
105#endif
106
Scott Wakelingfe885462016-09-22 10:24:38 +0100107// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
108#define __ down_cast<CodeGeneratorARMVIXL*>(codegen)->GetVIXLAssembler()-> // NOLINT
109#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kArmPointerSize, x).Int32Value()
110
111// Marker that code is yet to be, and must, be implemented.
112#define TODO_VIXL32(level) LOG(level) << __PRETTY_FUNCTION__ << " unimplemented "
113
Vladimir Markoeee1c0e2017-04-21 17:58:41 +0100114static inline void ExcludeIPAndBakerCcEntrypointRegister(UseScratchRegisterScope* temps,
115 HInstruction* instruction) {
116 DCHECK(temps->IsAvailable(ip));
117 temps->Exclude(ip);
118 DCHECK(!temps->IsAvailable(kBakerCcEntrypointRegister));
119 DCHECK_EQ(kBakerCcEntrypointRegister.GetCode(),
120 linker::Thumb2RelativePatcher::kBakerCcEntrypointRegister);
121 DCHECK_NE(instruction->GetLocations()->GetTempCount(), 0u);
122 DCHECK(RegisterFrom(instruction->GetLocations()->GetTemp(
123 instruction->GetLocations()->GetTempCount() - 1u)).Is(kBakerCcEntrypointRegister));
124}
125
126static inline void EmitPlaceholderBne(CodeGeneratorARMVIXL* codegen, vixl32::Label* patch_label) {
127 ExactAssemblyScope eas(codegen->GetVIXLAssembler(), kMaxInstructionSizeInBytes);
128 __ bind(patch_label);
129 vixl32::Label placeholder_label;
130 __ b(ne, EncodingSize(Wide), &placeholder_label); // Placeholder, patched at link-time.
131 __ bind(&placeholder_label);
132}
133
Vladimir Marko88abba22017-05-03 17:09:25 +0100134static inline bool CanEmitNarrowLdr(vixl32::Register rt, vixl32::Register rn, uint32_t offset) {
135 return rt.IsLow() && rn.IsLow() && offset < 32u;
136}
137
Vladimir Markoeee1c0e2017-04-21 17:58:41 +0100138class EmitAdrCode {
139 public:
140 EmitAdrCode(ArmVIXLMacroAssembler* assembler, vixl32::Register rd, vixl32::Label* label)
141 : assembler_(assembler), rd_(rd), label_(label) {
142 ExactAssemblyScope aas(assembler, kMaxInstructionSizeInBytes);
143 adr_location_ = assembler->GetCursorOffset();
144 assembler->adr(EncodingSize(Wide), rd, label);
145 }
146
147 ~EmitAdrCode() {
148 DCHECK(label_->IsBound());
149 // The ADR emitted by the assembler does not set the Thumb mode bit we need.
150 // TODO: Maybe extend VIXL to allow ADR for return address?
151 uint8_t* raw_adr = assembler_->GetBuffer()->GetOffsetAddress<uint8_t*>(adr_location_);
152 // Expecting ADR encoding T3 with `(offset & 1) == 0`.
153 DCHECK_EQ(raw_adr[1] & 0xfbu, 0xf2u); // Check bits 24-31, except 26.
154 DCHECK_EQ(raw_adr[0] & 0xffu, 0x0fu); // Check bits 16-23.
155 DCHECK_EQ(raw_adr[3] & 0x8fu, rd_.GetCode()); // Check bits 8-11 and 15.
156 DCHECK_EQ(raw_adr[2] & 0x01u, 0x00u); // Check bit 0, i.e. the `offset & 1`.
157 // Add the Thumb mode bit.
158 raw_adr[2] |= 0x01u;
159 }
160
161 private:
162 ArmVIXLMacroAssembler* const assembler_;
163 vixl32::Register rd_;
164 vixl32::Label* const label_;
165 int32_t adr_location_;
166};
167
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100168// SaveLiveRegisters and RestoreLiveRegisters from SlowPathCodeARM operate on sets of S registers,
169// for each live D registers they treat two corresponding S registers as live ones.
170//
171// Two following functions (SaveContiguousSRegisterList, RestoreContiguousSRegisterList) build
172// from a list of contiguous S registers a list of contiguous D registers (processing first/last
173// S registers corner cases) and save/restore this new list treating them as D registers.
174// - decreasing code size
175// - avoiding hazards on Cortex-A57, when a pair of S registers for an actual live D register is
176// restored and then used in regular non SlowPath code as D register.
177//
178// For the following example (v means the S register is live):
179// D names: | D0 | D1 | D2 | D4 | ...
180// S names: | S0 | S1 | S2 | S3 | S4 | S5 | S6 | S7 | ...
181// Live? | | v | v | v | v | v | v | | ...
182//
183// S1 and S6 will be saved/restored independently; D registers list (D1, D2) will be processed
184// as D registers.
185//
186// TODO(VIXL): All this code should be unnecessary once the VIXL AArch32 backend provides helpers
187// for lists of floating-point registers.
188static size_t SaveContiguousSRegisterList(size_t first,
189 size_t last,
190 CodeGenerator* codegen,
191 size_t stack_offset) {
192 static_assert(kSRegSizeInBytes == kArmWordSize, "Broken assumption on reg/word sizes.");
193 static_assert(kDRegSizeInBytes == 2 * kArmWordSize, "Broken assumption on reg/word sizes.");
194 DCHECK_LE(first, last);
195 if ((first == last) && (first == 0)) {
196 __ Vstr(vixl32::SRegister(first), MemOperand(sp, stack_offset));
197 return stack_offset + kSRegSizeInBytes;
198 }
199 if (first % 2 == 1) {
200 __ Vstr(vixl32::SRegister(first++), MemOperand(sp, stack_offset));
201 stack_offset += kSRegSizeInBytes;
202 }
203
204 bool save_last = false;
205 if (last % 2 == 0) {
206 save_last = true;
207 --last;
208 }
209
210 if (first < last) {
211 vixl32::DRegister d_reg = vixl32::DRegister(first / 2);
212 DCHECK_EQ((last - first + 1) % 2, 0u);
213 size_t number_of_d_regs = (last - first + 1) / 2;
214
215 if (number_of_d_regs == 1) {
216 __ Vstr(d_reg, MemOperand(sp, stack_offset));
217 } else if (number_of_d_regs > 1) {
218 UseScratchRegisterScope temps(down_cast<CodeGeneratorARMVIXL*>(codegen)->GetVIXLAssembler());
219 vixl32::Register base = sp;
220 if (stack_offset != 0) {
221 base = temps.Acquire();
Scott Wakelingb77051e2016-11-21 19:46:00 +0000222 __ Add(base, sp, Operand::From(stack_offset));
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100223 }
224 __ Vstm(F64, base, NO_WRITE_BACK, DRegisterList(d_reg, number_of_d_regs));
225 }
226 stack_offset += number_of_d_regs * kDRegSizeInBytes;
227 }
228
229 if (save_last) {
230 __ Vstr(vixl32::SRegister(last + 1), MemOperand(sp, stack_offset));
231 stack_offset += kSRegSizeInBytes;
232 }
233
234 return stack_offset;
235}
236
237static size_t RestoreContiguousSRegisterList(size_t first,
238 size_t last,
239 CodeGenerator* codegen,
240 size_t stack_offset) {
241 static_assert(kSRegSizeInBytes == kArmWordSize, "Broken assumption on reg/word sizes.");
242 static_assert(kDRegSizeInBytes == 2 * kArmWordSize, "Broken assumption on reg/word sizes.");
243 DCHECK_LE(first, last);
244 if ((first == last) && (first == 0)) {
245 __ Vldr(vixl32::SRegister(first), MemOperand(sp, stack_offset));
246 return stack_offset + kSRegSizeInBytes;
247 }
248 if (first % 2 == 1) {
249 __ Vldr(vixl32::SRegister(first++), MemOperand(sp, stack_offset));
250 stack_offset += kSRegSizeInBytes;
251 }
252
253 bool restore_last = false;
254 if (last % 2 == 0) {
255 restore_last = true;
256 --last;
257 }
258
259 if (first < last) {
260 vixl32::DRegister d_reg = vixl32::DRegister(first / 2);
261 DCHECK_EQ((last - first + 1) % 2, 0u);
262 size_t number_of_d_regs = (last - first + 1) / 2;
263 if (number_of_d_regs == 1) {
264 __ Vldr(d_reg, MemOperand(sp, stack_offset));
265 } else if (number_of_d_regs > 1) {
266 UseScratchRegisterScope temps(down_cast<CodeGeneratorARMVIXL*>(codegen)->GetVIXLAssembler());
267 vixl32::Register base = sp;
268 if (stack_offset != 0) {
269 base = temps.Acquire();
Scott Wakelingb77051e2016-11-21 19:46:00 +0000270 __ Add(base, sp, Operand::From(stack_offset));
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100271 }
272 __ Vldm(F64, base, NO_WRITE_BACK, DRegisterList(d_reg, number_of_d_regs));
273 }
274 stack_offset += number_of_d_regs * kDRegSizeInBytes;
275 }
276
277 if (restore_last) {
278 __ Vldr(vixl32::SRegister(last + 1), MemOperand(sp, stack_offset));
279 stack_offset += kSRegSizeInBytes;
280 }
281
282 return stack_offset;
283}
284
Vladimir Markod5d2f2c2017-09-26 12:37:26 +0100285static LoadOperandType GetLoadOperandType(DataType::Type type) {
286 switch (type) {
287 case DataType::Type::kReference:
288 return kLoadWord;
289 case DataType::Type::kBool:
290 case DataType::Type::kUint8:
291 return kLoadUnsignedByte;
292 case DataType::Type::kInt8:
293 return kLoadSignedByte;
294 case DataType::Type::kUint16:
295 return kLoadUnsignedHalfword;
296 case DataType::Type::kInt16:
297 return kLoadSignedHalfword;
298 case DataType::Type::kInt32:
299 return kLoadWord;
300 case DataType::Type::kInt64:
301 return kLoadWordPair;
302 case DataType::Type::kFloat32:
303 return kLoadSWord;
304 case DataType::Type::kFloat64:
305 return kLoadDWord;
306 default:
307 LOG(FATAL) << "Unreachable type " << type;
308 UNREACHABLE();
309 }
310}
311
312static StoreOperandType GetStoreOperandType(DataType::Type type) {
313 switch (type) {
314 case DataType::Type::kReference:
315 return kStoreWord;
316 case DataType::Type::kBool:
317 case DataType::Type::kUint8:
318 case DataType::Type::kInt8:
319 return kStoreByte;
320 case DataType::Type::kUint16:
321 case DataType::Type::kInt16:
322 return kStoreHalfword;
323 case DataType::Type::kInt32:
324 return kStoreWord;
325 case DataType::Type::kInt64:
326 return kStoreWordPair;
327 case DataType::Type::kFloat32:
328 return kStoreSWord;
329 case DataType::Type::kFloat64:
330 return kStoreDWord;
331 default:
332 LOG(FATAL) << "Unreachable type " << type;
333 UNREACHABLE();
334 }
335}
336
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100337void SlowPathCodeARMVIXL::SaveLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
338 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
339 size_t orig_offset = stack_offset;
340
341 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ true);
342 for (uint32_t i : LowToHighBits(core_spills)) {
343 // If the register holds an object, update the stack mask.
344 if (locations->RegisterContainsObject(i)) {
345 locations->SetStackBit(stack_offset / kVRegSize);
346 }
347 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
348 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
349 saved_core_stack_offsets_[i] = stack_offset;
350 stack_offset += kArmWordSize;
351 }
352
353 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
354 arm_codegen->GetAssembler()->StoreRegisterList(core_spills, orig_offset);
355
356 uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ false);
357 orig_offset = stack_offset;
358 for (uint32_t i : LowToHighBits(fp_spills)) {
359 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
360 saved_fpu_stack_offsets_[i] = stack_offset;
361 stack_offset += kArmWordSize;
362 }
363
364 stack_offset = orig_offset;
365 while (fp_spills != 0u) {
366 uint32_t begin = CTZ(fp_spills);
367 uint32_t tmp = fp_spills + (1u << begin);
368 fp_spills &= tmp; // Clear the contiguous range of 1s.
369 uint32_t end = (tmp == 0u) ? 32u : CTZ(tmp); // CTZ(0) is undefined.
370 stack_offset = SaveContiguousSRegisterList(begin, end - 1, codegen, stack_offset);
371 }
372 DCHECK_LE(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
373}
374
375void SlowPathCodeARMVIXL::RestoreLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
376 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
377 size_t orig_offset = stack_offset;
378
379 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ true);
380 for (uint32_t i : LowToHighBits(core_spills)) {
381 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
382 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
383 stack_offset += kArmWordSize;
384 }
385
386 // TODO(VIXL): Check the coherency of stack_offset after this with a test.
387 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
388 arm_codegen->GetAssembler()->LoadRegisterList(core_spills, orig_offset);
389
390 uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ false);
391 while (fp_spills != 0u) {
392 uint32_t begin = CTZ(fp_spills);
393 uint32_t tmp = fp_spills + (1u << begin);
394 fp_spills &= tmp; // Clear the contiguous range of 1s.
395 uint32_t end = (tmp == 0u) ? 32u : CTZ(tmp); // CTZ(0) is undefined.
396 stack_offset = RestoreContiguousSRegisterList(begin, end - 1, codegen, stack_offset);
397 }
398 DCHECK_LE(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
399}
400
401class NullCheckSlowPathARMVIXL : public SlowPathCodeARMVIXL {
402 public:
403 explicit NullCheckSlowPathARMVIXL(HNullCheck* instruction) : SlowPathCodeARMVIXL(instruction) {}
404
405 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
406 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
407 __ Bind(GetEntryLabel());
408 if (instruction_->CanThrowIntoCatchBlock()) {
409 // Live registers will be restored in the catch block if caught.
410 SaveLiveRegisters(codegen, instruction_->GetLocations());
411 }
412 arm_codegen->InvokeRuntime(kQuickThrowNullPointer,
413 instruction_,
414 instruction_->GetDexPc(),
415 this);
416 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
417 }
418
419 bool IsFatal() const OVERRIDE { return true; }
420
421 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathARMVIXL"; }
422
423 private:
424 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARMVIXL);
425};
426
Scott Wakelingfe885462016-09-22 10:24:38 +0100427class DivZeroCheckSlowPathARMVIXL : public SlowPathCodeARMVIXL {
428 public:
429 explicit DivZeroCheckSlowPathARMVIXL(HDivZeroCheck* instruction)
430 : SlowPathCodeARMVIXL(instruction) {}
431
432 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100433 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
Scott Wakelingfe885462016-09-22 10:24:38 +0100434 __ Bind(GetEntryLabel());
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100435 arm_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Scott Wakelingfe885462016-09-22 10:24:38 +0100436 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
437 }
438
439 bool IsFatal() const OVERRIDE { return true; }
440
441 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathARMVIXL"; }
442
443 private:
444 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARMVIXL);
445};
446
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100447class SuspendCheckSlowPathARMVIXL : public SlowPathCodeARMVIXL {
448 public:
449 SuspendCheckSlowPathARMVIXL(HSuspendCheck* instruction, HBasicBlock* successor)
450 : SlowPathCodeARMVIXL(instruction), successor_(successor) {}
451
452 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
453 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
454 __ Bind(GetEntryLabel());
455 arm_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
456 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
457 if (successor_ == nullptr) {
458 __ B(GetReturnLabel());
459 } else {
460 __ B(arm_codegen->GetLabelOf(successor_));
461 }
462 }
463
464 vixl32::Label* GetReturnLabel() {
465 DCHECK(successor_ == nullptr);
466 return &return_label_;
467 }
468
469 HBasicBlock* GetSuccessor() const {
470 return successor_;
471 }
472
473 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathARMVIXL"; }
474
475 private:
476 // If not null, the block to branch to after the suspend check.
477 HBasicBlock* const successor_;
478
479 // If `successor_` is null, the label to branch to after the suspend check.
480 vixl32::Label return_label_;
481
482 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARMVIXL);
483};
484
Scott Wakelingc34dba72016-10-03 10:14:44 +0100485class BoundsCheckSlowPathARMVIXL : public SlowPathCodeARMVIXL {
486 public:
487 explicit BoundsCheckSlowPathARMVIXL(HBoundsCheck* instruction)
488 : SlowPathCodeARMVIXL(instruction) {}
489
490 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
491 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
492 LocationSummary* locations = instruction_->GetLocations();
493
494 __ Bind(GetEntryLabel());
495 if (instruction_->CanThrowIntoCatchBlock()) {
496 // Live registers will be restored in the catch block if caught.
497 SaveLiveRegisters(codegen, instruction_->GetLocations());
498 }
499 // We're moving two locations to locations that could overlap, so we need a parallel
500 // move resolver.
501 InvokeRuntimeCallingConventionARMVIXL calling_convention;
502 codegen->EmitParallelMoves(
503 locations->InAt(0),
504 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100505 DataType::Type::kInt32,
Scott Wakelingc34dba72016-10-03 10:14:44 +0100506 locations->InAt(1),
507 LocationFrom(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100508 DataType::Type::kInt32);
Scott Wakelingc34dba72016-10-03 10:14:44 +0100509 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
510 ? kQuickThrowStringBounds
511 : kQuickThrowArrayBounds;
512 arm_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
513 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
514 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
515 }
516
517 bool IsFatal() const OVERRIDE { return true; }
518
519 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathARMVIXL"; }
520
521 private:
522 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARMVIXL);
523};
524
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100525class LoadClassSlowPathARMVIXL : public SlowPathCodeARMVIXL {
526 public:
527 LoadClassSlowPathARMVIXL(HLoadClass* cls, HInstruction* at, uint32_t dex_pc, bool do_clinit)
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000528 : SlowPathCodeARMVIXL(at), cls_(cls), dex_pc_(dex_pc), do_clinit_(do_clinit) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100529 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
530 }
531
532 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000533 LocationSummary* locations = instruction_->GetLocations();
Vladimir Markoea4c1262017-02-06 19:59:33 +0000534 Location out = locations->Out();
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100535
536 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
537 __ Bind(GetEntryLabel());
538 SaveLiveRegisters(codegen, locations);
539
540 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000541 dex::TypeIndex type_index = cls_->GetTypeIndex();
542 __ Mov(calling_convention.GetRegisterAt(0), type_index.index_);
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100543 QuickEntrypointEnum entrypoint = do_clinit_ ? kQuickInitializeStaticStorage
544 : kQuickInitializeType;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000545 arm_codegen->InvokeRuntime(entrypoint, instruction_, dex_pc_, this);
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100546 if (do_clinit_) {
547 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
548 } else {
549 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
550 }
551
552 // Move the class to the desired location.
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100553 if (out.IsValid()) {
554 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
555 arm_codegen->Move32(locations->Out(), LocationFrom(r0));
556 }
557 RestoreLiveRegisters(codegen, locations);
558 __ B(GetExitLabel());
559 }
560
561 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathARMVIXL"; }
562
563 private:
564 // The class this slow path will load.
565 HLoadClass* const cls_;
566
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100567 // The dex PC of `at_`.
568 const uint32_t dex_pc_;
569
570 // Whether to initialize the class.
571 const bool do_clinit_;
572
573 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARMVIXL);
574};
575
Artem Serovd4cc5b22016-11-04 11:19:09 +0000576class LoadStringSlowPathARMVIXL : public SlowPathCodeARMVIXL {
577 public:
578 explicit LoadStringSlowPathARMVIXL(HLoadString* instruction)
579 : SlowPathCodeARMVIXL(instruction) {}
580
581 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Vladimir Markoea4c1262017-02-06 19:59:33 +0000582 DCHECK(instruction_->IsLoadString());
583 DCHECK_EQ(instruction_->AsLoadString()->GetLoadKind(), HLoadString::LoadKind::kBssEntry);
Artem Serovd4cc5b22016-11-04 11:19:09 +0000584 LocationSummary* locations = instruction_->GetLocations();
585 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Vladimir Markof3c52b42017-11-17 17:32:12 +0000586 const dex::StringIndex string_index = instruction_->AsLoadString()->GetStringIndex();
Artem Serovd4cc5b22016-11-04 11:19:09 +0000587
588 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
589 __ Bind(GetEntryLabel());
590 SaveLiveRegisters(codegen, locations);
591
592 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000593 __ Mov(calling_convention.GetRegisterAt(0), string_index.index_);
Artem Serovd4cc5b22016-11-04 11:19:09 +0000594 arm_codegen->InvokeRuntime(kQuickResolveString, instruction_, instruction_->GetDexPc(), this);
595 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
596
Artem Serovd4cc5b22016-11-04 11:19:09 +0000597 arm_codegen->Move32(locations->Out(), LocationFrom(r0));
598 RestoreLiveRegisters(codegen, locations);
599
600 __ B(GetExitLabel());
601 }
602
603 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathARMVIXL"; }
604
605 private:
606 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARMVIXL);
607};
608
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100609class TypeCheckSlowPathARMVIXL : public SlowPathCodeARMVIXL {
610 public:
611 TypeCheckSlowPathARMVIXL(HInstruction* instruction, bool is_fatal)
612 : SlowPathCodeARMVIXL(instruction), is_fatal_(is_fatal) {}
613
614 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
615 LocationSummary* locations = instruction_->GetLocations();
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100616 DCHECK(instruction_->IsCheckCast()
617 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
618
619 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
620 __ Bind(GetEntryLabel());
621
Vladimir Marko87584542017-12-12 17:47:52 +0000622 if (!is_fatal_ || instruction_->CanThrowIntoCatchBlock()) {
Artem Serovcfbe9132016-10-14 15:58:56 +0100623 SaveLiveRegisters(codegen, locations);
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100624 }
625
626 // We're moving two locations to locations that could overlap, so we need a parallel
627 // move resolver.
628 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100629
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800630 codegen->EmitParallelMoves(locations->InAt(0),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800631 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100632 DataType::Type::kReference,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800633 locations->InAt(1),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800634 LocationFrom(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100635 DataType::Type::kReference);
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100636 if (instruction_->IsInstanceOf()) {
Artem Serovcfbe9132016-10-14 15:58:56 +0100637 arm_codegen->InvokeRuntime(kQuickInstanceofNonTrivial,
638 instruction_,
639 instruction_->GetDexPc(),
640 this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800641 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Artem Serovcfbe9132016-10-14 15:58:56 +0100642 arm_codegen->Move32(locations->Out(), LocationFrom(r0));
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100643 } else {
644 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800645 arm_codegen->InvokeRuntime(kQuickCheckInstanceOf,
646 instruction_,
647 instruction_->GetDexPc(),
648 this);
649 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100650 }
651
652 if (!is_fatal_) {
Artem Serovcfbe9132016-10-14 15:58:56 +0100653 RestoreLiveRegisters(codegen, locations);
654 __ B(GetExitLabel());
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100655 }
656 }
657
658 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathARMVIXL"; }
659
660 bool IsFatal() const OVERRIDE { return is_fatal_; }
661
662 private:
663 const bool is_fatal_;
664
665 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARMVIXL);
666};
667
Scott Wakelingc34dba72016-10-03 10:14:44 +0100668class DeoptimizationSlowPathARMVIXL : public SlowPathCodeARMVIXL {
669 public:
670 explicit DeoptimizationSlowPathARMVIXL(HDeoptimize* instruction)
671 : SlowPathCodeARMVIXL(instruction) {}
672
673 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
674 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
675 __ Bind(GetEntryLabel());
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100676 LocationSummary* locations = instruction_->GetLocations();
677 SaveLiveRegisters(codegen, locations);
678 InvokeRuntimeCallingConventionARMVIXL calling_convention;
679 __ Mov(calling_convention.GetRegisterAt(0),
680 static_cast<uint32_t>(instruction_->AsDeoptimize()->GetDeoptimizationKind()));
681
Scott Wakelingc34dba72016-10-03 10:14:44 +0100682 arm_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100683 CheckEntrypointTypes<kQuickDeoptimize, void, DeoptimizationKind>();
Scott Wakelingc34dba72016-10-03 10:14:44 +0100684 }
685
686 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathARMVIXL"; }
687
688 private:
689 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARMVIXL);
690};
691
692class ArraySetSlowPathARMVIXL : public SlowPathCodeARMVIXL {
693 public:
694 explicit ArraySetSlowPathARMVIXL(HInstruction* instruction) : SlowPathCodeARMVIXL(instruction) {}
695
696 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
697 LocationSummary* locations = instruction_->GetLocations();
698 __ Bind(GetEntryLabel());
699 SaveLiveRegisters(codegen, locations);
700
701 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100702 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Scott Wakelingc34dba72016-10-03 10:14:44 +0100703 parallel_move.AddMove(
704 locations->InAt(0),
705 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100706 DataType::Type::kReference,
Scott Wakelingc34dba72016-10-03 10:14:44 +0100707 nullptr);
708 parallel_move.AddMove(
709 locations->InAt(1),
710 LocationFrom(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100711 DataType::Type::kInt32,
Scott Wakelingc34dba72016-10-03 10:14:44 +0100712 nullptr);
713 parallel_move.AddMove(
714 locations->InAt(2),
715 LocationFrom(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100716 DataType::Type::kReference,
Scott Wakelingc34dba72016-10-03 10:14:44 +0100717 nullptr);
718 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
719
720 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
721 arm_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
722 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
723 RestoreLiveRegisters(codegen, locations);
724 __ B(GetExitLabel());
725 }
726
727 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathARMVIXL"; }
728
729 private:
730 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathARMVIXL);
731};
732
Roland Levillain54f869e2017-03-06 13:54:11 +0000733// Abstract base class for read barrier slow paths marking a reference
734// `ref`.
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000735//
Roland Levillain54f869e2017-03-06 13:54:11 +0000736// Argument `entrypoint` must be a register location holding the read
Roland Levillain6d729a72017-06-30 18:34:01 +0100737// barrier marking runtime entry point to be invoked or an empty
738// location; in the latter case, the read barrier marking runtime
739// entry point will be loaded by the slow path code itself.
Roland Levillain54f869e2017-03-06 13:54:11 +0000740class ReadBarrierMarkSlowPathBaseARMVIXL : public SlowPathCodeARMVIXL {
741 protected:
742 ReadBarrierMarkSlowPathBaseARMVIXL(HInstruction* instruction, Location ref, Location entrypoint)
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000743 : SlowPathCodeARMVIXL(instruction), ref_(ref), entrypoint_(entrypoint) {
744 DCHECK(kEmitCompilerReadBarrier);
745 }
746
Roland Levillain54f869e2017-03-06 13:54:11 +0000747 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathBaseARMVIXL"; }
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000748
Roland Levillain54f869e2017-03-06 13:54:11 +0000749 // Generate assembly code calling the read barrier marking runtime
750 // entry point (ReadBarrierMarkRegX).
751 void GenerateReadBarrierMarkRuntimeCall(CodeGenerator* codegen) {
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000752 vixl32::Register ref_reg = RegisterFrom(ref_);
Roland Levillain47b3ab22017-02-27 14:31:35 +0000753
Roland Levillain47b3ab22017-02-27 14:31:35 +0000754 // No need to save live registers; it's taken care of by the
755 // entrypoint. Also, there is no need to update the stack mask,
756 // as this runtime call will not trigger a garbage collection.
757 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
758 DCHECK(!ref_reg.Is(sp));
759 DCHECK(!ref_reg.Is(lr));
760 DCHECK(!ref_reg.Is(pc));
761 // IP is used internally by the ReadBarrierMarkRegX entry point
762 // as a temporary, it cannot be the entry point's input/output.
763 DCHECK(!ref_reg.Is(ip));
764 DCHECK(ref_reg.IsRegister()) << ref_reg;
765 // "Compact" slow path, saving two moves.
766 //
767 // Instead of using the standard runtime calling convention (input
768 // and output in R0):
769 //
770 // R0 <- ref
771 // R0 <- ReadBarrierMark(R0)
772 // ref <- R0
773 //
774 // we just use rX (the register containing `ref`) as input and output
775 // of a dedicated entrypoint:
776 //
777 // rX <- ReadBarrierMarkRegX(rX)
778 //
779 if (entrypoint_.IsValid()) {
780 arm_codegen->ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction_, this);
781 __ Blx(RegisterFrom(entrypoint_));
782 } else {
Roland Levillain54f869e2017-03-06 13:54:11 +0000783 // Entrypoint is not already loaded, load from the thread.
Roland Levillain47b3ab22017-02-27 14:31:35 +0000784 int32_t entry_point_offset =
Roland Levillain97c46462017-05-11 14:04:03 +0100785 Thread::ReadBarrierMarkEntryPointsOffset<kArmPointerSize>(ref_reg.GetCode());
Roland Levillain47b3ab22017-02-27 14:31:35 +0000786 // This runtime call does not require a stack map.
787 arm_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
788 }
Roland Levillain47b3ab22017-02-27 14:31:35 +0000789 }
790
Roland Levillain47b3ab22017-02-27 14:31:35 +0000791 // The location (register) of the marked object reference.
792 const Location ref_;
793
794 // The location of the entrypoint if already loaded.
795 const Location entrypoint_;
796
Roland Levillain54f869e2017-03-06 13:54:11 +0000797 private:
798 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathBaseARMVIXL);
Roland Levillain47b3ab22017-02-27 14:31:35 +0000799};
800
Scott Wakelingc34dba72016-10-03 10:14:44 +0100801// Slow path marking an object reference `ref` during a read
802// barrier. The field `obj.field` in the object `obj` holding this
Roland Levillain54f869e2017-03-06 13:54:11 +0000803// reference does not get updated by this slow path after marking.
Roland Levillain47b3ab22017-02-27 14:31:35 +0000804//
Scott Wakelingc34dba72016-10-03 10:14:44 +0100805// This means that after the execution of this slow path, `ref` will
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000806// always be up-to-date, but `obj.field` may not; i.e., after the
807// flip, `ref` will be a to-space reference, but `obj.field` will
808// probably still be a from-space reference (unless it gets updated by
809// another thread, or if another thread installed another object
810// reference (different from `ref`) in `obj.field`).
Roland Levillainba650a42017-03-06 13:52:32 +0000811//
Roland Levillain6d729a72017-06-30 18:34:01 +0100812// Argument `entrypoint` must be a register location holding the read
813// barrier marking runtime entry point to be invoked or an empty
814// location; in the latter case, the read barrier marking runtime
815// entry point will be loaded by the slow path code itself.
Roland Levillain54f869e2017-03-06 13:54:11 +0000816class ReadBarrierMarkSlowPathARMVIXL : public ReadBarrierMarkSlowPathBaseARMVIXL {
Roland Levillain47b3ab22017-02-27 14:31:35 +0000817 public:
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000818 ReadBarrierMarkSlowPathARMVIXL(HInstruction* instruction,
819 Location ref,
820 Location entrypoint = Location::NoLocation())
Roland Levillain54f869e2017-03-06 13:54:11 +0000821 : ReadBarrierMarkSlowPathBaseARMVIXL(instruction, ref, entrypoint) {
Roland Levillain47b3ab22017-02-27 14:31:35 +0000822 DCHECK(kEmitCompilerReadBarrier);
823 }
824
Roland Levillain47b3ab22017-02-27 14:31:35 +0000825 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathARMVIXL"; }
826
827 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
828 LocationSummary* locations = instruction_->GetLocations();
Roland Levillain54f869e2017-03-06 13:54:11 +0000829 DCHECK(locations->CanCall());
830 DCHECK(ref_.IsRegister()) << ref_;
831 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_.reg())) << ref_.reg();
832 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
833 << "Unexpected instruction in read barrier marking slow path: "
834 << instruction_->DebugName();
835
836 __ Bind(GetEntryLabel());
837 GenerateReadBarrierMarkRuntimeCall(codegen);
838 __ B(GetExitLabel());
839 }
840
841 private:
842 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathARMVIXL);
843};
844
845// Slow path loading `obj`'s lock word, loading a reference from
846// object `*(obj + offset + (index << scale_factor))` into `ref`, and
847// marking `ref` if `obj` is gray according to the lock word (Baker
848// read barrier). The field `obj.field` in the object `obj` holding
849// this reference does not get updated by this slow path after marking
850// (see LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARMVIXL
851// below for that).
852//
853// This means that after the execution of this slow path, `ref` will
854// always be up-to-date, but `obj.field` may not; i.e., after the
855// flip, `ref` will be a to-space reference, but `obj.field` will
856// probably still be a from-space reference (unless it gets updated by
857// another thread, or if another thread installed another object
858// reference (different from `ref`) in `obj.field`).
859//
860// Argument `entrypoint` must be a register location holding the read
Roland Levillain6d729a72017-06-30 18:34:01 +0100861// barrier marking runtime entry point to be invoked or an empty
862// location; in the latter case, the read barrier marking runtime
863// entry point will be loaded by the slow path code itself.
Roland Levillain54f869e2017-03-06 13:54:11 +0000864class LoadReferenceWithBakerReadBarrierSlowPathARMVIXL : public ReadBarrierMarkSlowPathBaseARMVIXL {
865 public:
866 LoadReferenceWithBakerReadBarrierSlowPathARMVIXL(HInstruction* instruction,
867 Location ref,
868 vixl32::Register obj,
869 uint32_t offset,
870 Location index,
871 ScaleFactor scale_factor,
872 bool needs_null_check,
873 vixl32::Register temp,
Roland Levillain6d729a72017-06-30 18:34:01 +0100874 Location entrypoint = Location::NoLocation())
Roland Levillain54f869e2017-03-06 13:54:11 +0000875 : ReadBarrierMarkSlowPathBaseARMVIXL(instruction, ref, entrypoint),
876 obj_(obj),
877 offset_(offset),
878 index_(index),
879 scale_factor_(scale_factor),
880 needs_null_check_(needs_null_check),
881 temp_(temp) {
882 DCHECK(kEmitCompilerReadBarrier);
883 DCHECK(kUseBakerReadBarrier);
884 }
885
Roland Levillain47b3ab22017-02-27 14:31:35 +0000886 const char* GetDescription() const OVERRIDE {
Roland Levillain54f869e2017-03-06 13:54:11 +0000887 return "LoadReferenceWithBakerReadBarrierSlowPathARMVIXL";
Roland Levillain47b3ab22017-02-27 14:31:35 +0000888 }
889
890 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
891 LocationSummary* locations = instruction_->GetLocations();
892 vixl32::Register ref_reg = RegisterFrom(ref_);
893 DCHECK(locations->CanCall());
894 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg.GetCode())) << ref_reg;
Roland Levillain47b3ab22017-02-27 14:31:35 +0000895 DCHECK(instruction_->IsInstanceFieldGet() ||
896 instruction_->IsStaticFieldGet() ||
897 instruction_->IsArrayGet() ||
898 instruction_->IsArraySet() ||
Roland Levillain47b3ab22017-02-27 14:31:35 +0000899 instruction_->IsInstanceOf() ||
900 instruction_->IsCheckCast() ||
901 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
902 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
903 << "Unexpected instruction in read barrier marking slow path: "
904 << instruction_->DebugName();
905 // The read barrier instrumentation of object ArrayGet
906 // instructions does not support the HIntermediateAddress
907 // instruction.
908 DCHECK(!(instruction_->IsArrayGet() &&
909 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
910
Roland Levillain54f869e2017-03-06 13:54:11 +0000911 // Temporary register `temp_`, used to store the lock word, must
912 // not be IP, as we may use it to emit the reference load (in the
913 // call to GenerateRawReferenceLoad below), and we need the lock
914 // word to still be in `temp_` after the reference load.
915 DCHECK(!temp_.Is(ip));
916
Roland Levillain47b3ab22017-02-27 14:31:35 +0000917 __ Bind(GetEntryLabel());
Roland Levillain54f869e2017-03-06 13:54:11 +0000918
919 // When using MaybeGenerateReadBarrierSlow, the read barrier call is
920 // inserted after the original load. However, in fast path based
921 // Baker's read barriers, we need to perform the load of
922 // mirror::Object::monitor_ *before* the original reference load.
923 // This load-load ordering is required by the read barrier.
Roland Levillainff487002017-03-07 16:50:01 +0000924 // The slow path (for Baker's algorithm) should look like:
Roland Levillain54f869e2017-03-06 13:54:11 +0000925 //
926 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
927 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
928 // HeapReference<mirror::Object> ref = *src; // Original reference load.
929 // bool is_gray = (rb_state == ReadBarrier::GrayState());
930 // if (is_gray) {
931 // ref = entrypoint(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
932 // }
933 //
934 // Note: the original implementation in ReadBarrier::Barrier is
935 // slightly more complex as it performs additional checks that we do
936 // not do here for performance reasons.
937
Roland Levillain47b3ab22017-02-27 14:31:35 +0000938 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
Roland Levillain54f869e2017-03-06 13:54:11 +0000939
940 // /* int32_t */ monitor = obj->monitor_
941 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
942 arm_codegen->GetAssembler()->LoadFromOffset(kLoadWord, temp_, obj_, monitor_offset);
943 if (needs_null_check_) {
944 codegen->MaybeRecordImplicitNullCheck(instruction_);
Roland Levillain47b3ab22017-02-27 14:31:35 +0000945 }
Roland Levillain54f869e2017-03-06 13:54:11 +0000946 // /* LockWord */ lock_word = LockWord(monitor)
947 static_assert(sizeof(LockWord) == sizeof(int32_t),
948 "art::LockWord and int32_t have different sizes.");
949
950 // Introduce a dependency on the lock_word including the rb_state,
951 // which shall prevent load-load reordering without using
952 // a memory barrier (which would be more expensive).
953 // `obj` is unchanged by this operation, but its value now depends
954 // on `temp`.
955 __ Add(obj_, obj_, Operand(temp_, ShiftType::LSR, 32));
956
957 // The actual reference load.
958 // A possible implicit null check has already been handled above.
959 arm_codegen->GenerateRawReferenceLoad(
960 instruction_, ref_, obj_, offset_, index_, scale_factor_, /* needs_null_check */ false);
961
962 // Mark the object `ref` when `obj` is gray.
963 //
964 // if (rb_state == ReadBarrier::GrayState())
965 // ref = ReadBarrier::Mark(ref);
966 //
967 // Given the numeric representation, it's enough to check the low bit of the
968 // rb_state. We do that by shifting the bit out of the lock word with LSRS
969 // which can be a 16-bit instruction unlike the TST immediate.
970 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
971 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
972 __ Lsrs(temp_, temp_, LockWord::kReadBarrierStateShift + 1);
973 __ B(cc, GetExitLabel()); // Carry flag is the last bit shifted out by LSRS.
974 GenerateReadBarrierMarkRuntimeCall(codegen);
975
Roland Levillain47b3ab22017-02-27 14:31:35 +0000976 __ B(GetExitLabel());
977 }
978
979 private:
Roland Levillain54f869e2017-03-06 13:54:11 +0000980 // The register containing the object holding the marked object reference field.
981 vixl32::Register obj_;
982 // The offset, index and scale factor to access the reference in `obj_`.
983 uint32_t offset_;
984 Location index_;
985 ScaleFactor scale_factor_;
986 // Is a null check required?
987 bool needs_null_check_;
988 // A temporary register used to hold the lock word of `obj_`.
989 vixl32::Register temp_;
Roland Levillain47b3ab22017-02-27 14:31:35 +0000990
Roland Levillain54f869e2017-03-06 13:54:11 +0000991 DISALLOW_COPY_AND_ASSIGN(LoadReferenceWithBakerReadBarrierSlowPathARMVIXL);
Roland Levillain47b3ab22017-02-27 14:31:35 +0000992};
993
Roland Levillain54f869e2017-03-06 13:54:11 +0000994// Slow path loading `obj`'s lock word, loading a reference from
995// object `*(obj + offset + (index << scale_factor))` into `ref`, and
996// marking `ref` if `obj` is gray according to the lock word (Baker
997// read barrier). If needed, this slow path also atomically updates
998// the field `obj.field` in the object `obj` holding this reference
999// after marking (contrary to
1000// LoadReferenceWithBakerReadBarrierSlowPathARMVIXL above, which never
1001// tries to update `obj.field`).
Roland Levillain47b3ab22017-02-27 14:31:35 +00001002//
1003// This means that after the execution of this slow path, both `ref`
1004// and `obj.field` will be up-to-date; i.e., after the flip, both will
1005// hold the same to-space reference (unless another thread installed
1006// another object reference (different from `ref`) in `obj.field`).
Roland Levillainba650a42017-03-06 13:52:32 +00001007//
Roland Levillain54f869e2017-03-06 13:54:11 +00001008// Argument `entrypoint` must be a register location holding the read
Roland Levillain6d729a72017-06-30 18:34:01 +01001009// barrier marking runtime entry point to be invoked or an empty
1010// location; in the latter case, the read barrier marking runtime
1011// entry point will be loaded by the slow path code itself.
Roland Levillain54f869e2017-03-06 13:54:11 +00001012class LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARMVIXL
1013 : public ReadBarrierMarkSlowPathBaseARMVIXL {
Roland Levillain47b3ab22017-02-27 14:31:35 +00001014 public:
Roland Levillain6d729a72017-06-30 18:34:01 +01001015 LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARMVIXL(
1016 HInstruction* instruction,
1017 Location ref,
1018 vixl32::Register obj,
1019 uint32_t offset,
1020 Location index,
1021 ScaleFactor scale_factor,
1022 bool needs_null_check,
1023 vixl32::Register temp1,
1024 vixl32::Register temp2,
1025 Location entrypoint = Location::NoLocation())
Roland Levillain54f869e2017-03-06 13:54:11 +00001026 : ReadBarrierMarkSlowPathBaseARMVIXL(instruction, ref, entrypoint),
Roland Levillain47b3ab22017-02-27 14:31:35 +00001027 obj_(obj),
Roland Levillain54f869e2017-03-06 13:54:11 +00001028 offset_(offset),
1029 index_(index),
1030 scale_factor_(scale_factor),
1031 needs_null_check_(needs_null_check),
Roland Levillain47b3ab22017-02-27 14:31:35 +00001032 temp1_(temp1),
Roland Levillain54f869e2017-03-06 13:54:11 +00001033 temp2_(temp2) {
Roland Levillain47b3ab22017-02-27 14:31:35 +00001034 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain54f869e2017-03-06 13:54:11 +00001035 DCHECK(kUseBakerReadBarrier);
Roland Levillain47b3ab22017-02-27 14:31:35 +00001036 }
1037
1038 const char* GetDescription() const OVERRIDE {
Roland Levillain54f869e2017-03-06 13:54:11 +00001039 return "LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARMVIXL";
Roland Levillain47b3ab22017-02-27 14:31:35 +00001040 }
1041
1042 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
1043 LocationSummary* locations = instruction_->GetLocations();
1044 vixl32::Register ref_reg = RegisterFrom(ref_);
1045 DCHECK(locations->CanCall());
1046 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg.GetCode())) << ref_reg;
Roland Levillain54f869e2017-03-06 13:54:11 +00001047 DCHECK_NE(ref_.reg(), LocationFrom(temp1_).reg());
1048
1049 // This slow path is only used by the UnsafeCASObject intrinsic at the moment.
Roland Levillain47b3ab22017-02-27 14:31:35 +00001050 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
1051 << "Unexpected instruction in read barrier marking and field updating slow path: "
1052 << instruction_->DebugName();
1053 DCHECK(instruction_->GetLocations()->Intrinsified());
1054 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
Roland Levillain54f869e2017-03-06 13:54:11 +00001055 DCHECK_EQ(offset_, 0u);
1056 DCHECK_EQ(scale_factor_, ScaleFactor::TIMES_1);
1057 Location field_offset = index_;
1058 DCHECK(field_offset.IsRegisterPair()) << field_offset;
1059
1060 // Temporary register `temp1_`, used to store the lock word, must
1061 // not be IP, as we may use it to emit the reference load (in the
1062 // call to GenerateRawReferenceLoad below), and we need the lock
1063 // word to still be in `temp1_` after the reference load.
1064 DCHECK(!temp1_.Is(ip));
Roland Levillain47b3ab22017-02-27 14:31:35 +00001065
1066 __ Bind(GetEntryLabel());
1067
Roland Levillainff487002017-03-07 16:50:01 +00001068 // The implementation is similar to LoadReferenceWithBakerReadBarrierSlowPathARMVIXL's:
1069 //
1070 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
1071 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
1072 // HeapReference<mirror::Object> ref = *src; // Original reference load.
1073 // bool is_gray = (rb_state == ReadBarrier::GrayState());
1074 // if (is_gray) {
1075 // old_ref = ref;
1076 // ref = entrypoint(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
1077 // compareAndSwapObject(obj, field_offset, old_ref, ref);
1078 // }
1079
Roland Levillain54f869e2017-03-06 13:54:11 +00001080 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
1081
1082 // /* int32_t */ monitor = obj->monitor_
1083 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
1084 arm_codegen->GetAssembler()->LoadFromOffset(kLoadWord, temp1_, obj_, monitor_offset);
1085 if (needs_null_check_) {
1086 codegen->MaybeRecordImplicitNullCheck(instruction_);
1087 }
1088 // /* LockWord */ lock_word = LockWord(monitor)
1089 static_assert(sizeof(LockWord) == sizeof(int32_t),
1090 "art::LockWord and int32_t have different sizes.");
1091
1092 // Introduce a dependency on the lock_word including the rb_state,
1093 // which shall prevent load-load reordering without using
1094 // a memory barrier (which would be more expensive).
1095 // `obj` is unchanged by this operation, but its value now depends
1096 // on `temp`.
1097 __ Add(obj_, obj_, Operand(temp1_, ShiftType::LSR, 32));
1098
1099 // The actual reference load.
1100 // A possible implicit null check has already been handled above.
1101 arm_codegen->GenerateRawReferenceLoad(
1102 instruction_, ref_, obj_, offset_, index_, scale_factor_, /* needs_null_check */ false);
1103
1104 // Mark the object `ref` when `obj` is gray.
1105 //
1106 // if (rb_state == ReadBarrier::GrayState())
1107 // ref = ReadBarrier::Mark(ref);
1108 //
1109 // Given the numeric representation, it's enough to check the low bit of the
1110 // rb_state. We do that by shifting the bit out of the lock word with LSRS
1111 // which can be a 16-bit instruction unlike the TST immediate.
1112 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
1113 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
1114 __ Lsrs(temp1_, temp1_, LockWord::kReadBarrierStateShift + 1);
1115 __ B(cc, GetExitLabel()); // Carry flag is the last bit shifted out by LSRS.
1116
1117 // Save the old value of the reference before marking it.
Roland Levillain47b3ab22017-02-27 14:31:35 +00001118 // Note that we cannot use IP to save the old reference, as IP is
1119 // used internally by the ReadBarrierMarkRegX entry point, and we
1120 // need the old reference after the call to that entry point.
1121 DCHECK(!temp1_.Is(ip));
1122 __ Mov(temp1_, ref_reg);
Roland Levillain27b1f9c2017-01-17 16:56:34 +00001123
Roland Levillain54f869e2017-03-06 13:54:11 +00001124 GenerateReadBarrierMarkRuntimeCall(codegen);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001125
1126 // If the new reference is different from the old reference,
Roland Levillain54f869e2017-03-06 13:54:11 +00001127 // update the field in the holder (`*(obj_ + field_offset)`).
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001128 //
1129 // Note that this field could also hold a different object, if
1130 // another thread had concurrently changed it. In that case, the
Anton Kirilov349e61f2017-12-15 17:11:33 +00001131 // LDREX/CMP/BNE sequence of instructions in the compare-and-set
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001132 // (CAS) operation below would abort the CAS, leaving the field
1133 // as-is.
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001134 __ Cmp(temp1_, ref_reg);
Roland Levillain54f869e2017-03-06 13:54:11 +00001135 __ B(eq, GetExitLabel());
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001136
1137 // Update the the holder's field atomically. This may fail if
1138 // mutator updates before us, but it's OK. This is achieved
1139 // using a strong compare-and-set (CAS) operation with relaxed
1140 // memory synchronization ordering, where the expected value is
1141 // the old reference and the desired value is the new reference.
1142
1143 UseScratchRegisterScope temps(arm_codegen->GetVIXLAssembler());
1144 // Convenience aliases.
1145 vixl32::Register base = obj_;
1146 // The UnsafeCASObject intrinsic uses a register pair as field
1147 // offset ("long offset"), of which only the low part contains
1148 // data.
Roland Levillain54f869e2017-03-06 13:54:11 +00001149 vixl32::Register offset = LowRegisterFrom(field_offset);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001150 vixl32::Register expected = temp1_;
1151 vixl32::Register value = ref_reg;
1152 vixl32::Register tmp_ptr = temps.Acquire(); // Pointer to actual memory.
1153 vixl32::Register tmp = temp2_; // Value in memory.
1154
1155 __ Add(tmp_ptr, base, offset);
1156
1157 if (kPoisonHeapReferences) {
1158 arm_codegen->GetAssembler()->PoisonHeapReference(expected);
1159 if (value.Is(expected)) {
1160 // Do not poison `value`, as it is the same register as
1161 // `expected`, which has just been poisoned.
1162 } else {
1163 arm_codegen->GetAssembler()->PoisonHeapReference(value);
1164 }
1165 }
1166
1167 // do {
1168 // tmp = [r_ptr] - expected;
1169 // } while (tmp == 0 && failure([r_ptr] <- r_new_value));
1170
Anton Kirilov349e61f2017-12-15 17:11:33 +00001171 vixl32::Label loop_head, comparison_failed, exit_loop;
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001172 __ Bind(&loop_head);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001173 __ Ldrex(tmp, MemOperand(tmp_ptr));
Anton Kirilov349e61f2017-12-15 17:11:33 +00001174 __ Cmp(tmp, expected);
1175 __ B(ne, &comparison_failed, /* far_target */ false);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001176 __ Strex(tmp, value, MemOperand(tmp_ptr));
Anton Kirilov349e61f2017-12-15 17:11:33 +00001177 __ CompareAndBranchIfZero(tmp, &exit_loop, /* far_target */ false);
1178 __ B(&loop_head);
1179 __ Bind(&comparison_failed);
1180 __ Clrex();
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001181 __ Bind(&exit_loop);
1182
1183 if (kPoisonHeapReferences) {
1184 arm_codegen->GetAssembler()->UnpoisonHeapReference(expected);
1185 if (value.Is(expected)) {
1186 // Do not unpoison `value`, as it is the same register as
1187 // `expected`, which has just been unpoisoned.
1188 } else {
1189 arm_codegen->GetAssembler()->UnpoisonHeapReference(value);
1190 }
1191 }
1192
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001193 __ B(GetExitLabel());
1194 }
1195
1196 private:
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001197 // The register containing the object holding the marked object reference field.
1198 const vixl32::Register obj_;
Roland Levillain54f869e2017-03-06 13:54:11 +00001199 // The offset, index and scale factor to access the reference in `obj_`.
1200 uint32_t offset_;
1201 Location index_;
1202 ScaleFactor scale_factor_;
1203 // Is a null check required?
1204 bool needs_null_check_;
1205 // A temporary register used to hold the lock word of `obj_`; and
1206 // also to hold the original reference value, when the reference is
1207 // marked.
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001208 const vixl32::Register temp1_;
Roland Levillain54f869e2017-03-06 13:54:11 +00001209 // A temporary register used in the implementation of the CAS, to
1210 // update the object's reference field.
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001211 const vixl32::Register temp2_;
1212
Roland Levillain54f869e2017-03-06 13:54:11 +00001213 DISALLOW_COPY_AND_ASSIGN(LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARMVIXL);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001214};
1215
1216// Slow path generating a read barrier for a heap reference.
1217class ReadBarrierForHeapReferenceSlowPathARMVIXL : public SlowPathCodeARMVIXL {
1218 public:
1219 ReadBarrierForHeapReferenceSlowPathARMVIXL(HInstruction* instruction,
1220 Location out,
1221 Location ref,
1222 Location obj,
1223 uint32_t offset,
1224 Location index)
1225 : SlowPathCodeARMVIXL(instruction),
1226 out_(out),
1227 ref_(ref),
1228 obj_(obj),
1229 offset_(offset),
1230 index_(index) {
1231 DCHECK(kEmitCompilerReadBarrier);
1232 // If `obj` is equal to `out` or `ref`, it means the initial object
1233 // has been overwritten by (or after) the heap object reference load
1234 // to be instrumented, e.g.:
1235 //
1236 // __ LoadFromOffset(kLoadWord, out, out, offset);
1237 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
1238 //
1239 // In that case, we have lost the information about the original
1240 // object, and the emitted read barrier cannot work properly.
1241 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
1242 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
1243 }
1244
1245 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
1246 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
1247 LocationSummary* locations = instruction_->GetLocations();
1248 vixl32::Register reg_out = RegisterFrom(out_);
1249 DCHECK(locations->CanCall());
1250 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out.GetCode()));
1251 DCHECK(instruction_->IsInstanceFieldGet() ||
1252 instruction_->IsStaticFieldGet() ||
1253 instruction_->IsArrayGet() ||
1254 instruction_->IsInstanceOf() ||
1255 instruction_->IsCheckCast() ||
Andreas Gamped9911ee2017-03-27 13:27:24 -07001256 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001257 << "Unexpected instruction in read barrier for heap reference slow path: "
1258 << instruction_->DebugName();
1259 // The read barrier instrumentation of object ArrayGet
1260 // instructions does not support the HIntermediateAddress
1261 // instruction.
1262 DCHECK(!(instruction_->IsArrayGet() &&
1263 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
1264
1265 __ Bind(GetEntryLabel());
1266 SaveLiveRegisters(codegen, locations);
1267
1268 // We may have to change the index's value, but as `index_` is a
1269 // constant member (like other "inputs" of this slow path),
1270 // introduce a copy of it, `index`.
1271 Location index = index_;
1272 if (index_.IsValid()) {
1273 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
1274 if (instruction_->IsArrayGet()) {
1275 // Compute the actual memory offset and store it in `index`.
1276 vixl32::Register index_reg = RegisterFrom(index_);
1277 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg.GetCode()));
1278 if (codegen->IsCoreCalleeSaveRegister(index_reg.GetCode())) {
1279 // We are about to change the value of `index_reg` (see the
Roland Levillain9983e302017-07-14 14:34:22 +01001280 // calls to art::arm::ArmVIXLMacroAssembler::Lsl and
1281 // art::arm::ArmVIXLMacroAssembler::Add below), but it has
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001282 // not been saved by the previous call to
1283 // art::SlowPathCode::SaveLiveRegisters, as it is a
1284 // callee-save register --
1285 // art::SlowPathCode::SaveLiveRegisters does not consider
1286 // callee-save registers, as it has been designed with the
1287 // assumption that callee-save registers are supposed to be
1288 // handled by the called function. So, as a callee-save
1289 // register, `index_reg` _would_ eventually be saved onto
1290 // the stack, but it would be too late: we would have
1291 // changed its value earlier. Therefore, we manually save
1292 // it here into another freely available register,
1293 // `free_reg`, chosen of course among the caller-save
1294 // registers (as a callee-save `free_reg` register would
1295 // exhibit the same problem).
1296 //
1297 // Note we could have requested a temporary register from
1298 // the register allocator instead; but we prefer not to, as
1299 // this is a slow path, and we know we can find a
1300 // caller-save register that is available.
1301 vixl32::Register free_reg = FindAvailableCallerSaveRegister(codegen);
1302 __ Mov(free_reg, index_reg);
1303 index_reg = free_reg;
1304 index = LocationFrom(index_reg);
1305 } else {
1306 // The initial register stored in `index_` has already been
1307 // saved in the call to art::SlowPathCode::SaveLiveRegisters
1308 // (as it is not a callee-save register), so we can freely
1309 // use it.
1310 }
1311 // Shifting the index value contained in `index_reg` by the scale
1312 // factor (2) cannot overflow in practice, as the runtime is
1313 // unable to allocate object arrays with a size larger than
1314 // 2^26 - 1 (that is, 2^28 - 4 bytes).
1315 __ Lsl(index_reg, index_reg, TIMES_4);
1316 static_assert(
1317 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
1318 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
1319 __ Add(index_reg, index_reg, offset_);
1320 } else {
1321 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
1322 // intrinsics, `index_` is not shifted by a scale factor of 2
1323 // (as in the case of ArrayGet), as it is actually an offset
1324 // to an object field within an object.
1325 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
1326 DCHECK(instruction_->GetLocations()->Intrinsified());
1327 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
1328 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
1329 << instruction_->AsInvoke()->GetIntrinsic();
1330 DCHECK_EQ(offset_, 0U);
1331 DCHECK(index_.IsRegisterPair());
1332 // UnsafeGet's offset location is a register pair, the low
1333 // part contains the correct offset.
1334 index = index_.ToLow();
1335 }
1336 }
1337
1338 // We're moving two or three locations to locations that could
1339 // overlap, so we need a parallel move resolver.
1340 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +01001341 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001342 parallel_move.AddMove(ref_,
1343 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001344 DataType::Type::kReference,
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001345 nullptr);
1346 parallel_move.AddMove(obj_,
1347 LocationFrom(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001348 DataType::Type::kReference,
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001349 nullptr);
1350 if (index.IsValid()) {
1351 parallel_move.AddMove(index,
1352 LocationFrom(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001353 DataType::Type::kInt32,
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001354 nullptr);
1355 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
1356 } else {
1357 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
1358 __ Mov(calling_convention.GetRegisterAt(2), offset_);
1359 }
1360 arm_codegen->InvokeRuntime(kQuickReadBarrierSlow, instruction_, instruction_->GetDexPc(), this);
1361 CheckEntrypointTypes<
1362 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
1363 arm_codegen->Move32(out_, LocationFrom(r0));
1364
1365 RestoreLiveRegisters(codegen, locations);
1366 __ B(GetExitLabel());
1367 }
1368
1369 const char* GetDescription() const OVERRIDE {
1370 return "ReadBarrierForHeapReferenceSlowPathARMVIXL";
1371 }
1372
1373 private:
1374 vixl32::Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
1375 uint32_t ref = RegisterFrom(ref_).GetCode();
1376 uint32_t obj = RegisterFrom(obj_).GetCode();
1377 for (uint32_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
1378 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
1379 return vixl32::Register(i);
1380 }
1381 }
1382 // We shall never fail to find a free caller-save register, as
1383 // there are more than two core caller-save registers on ARM
1384 // (meaning it is possible to find one which is different from
1385 // `ref` and `obj`).
1386 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
1387 LOG(FATAL) << "Could not find a free caller-save register";
1388 UNREACHABLE();
1389 }
1390
1391 const Location out_;
1392 const Location ref_;
1393 const Location obj_;
1394 const uint32_t offset_;
1395 // An additional location containing an index to an array.
1396 // Only used for HArrayGet and the UnsafeGetObject &
1397 // UnsafeGetObjectVolatile intrinsics.
1398 const Location index_;
1399
1400 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathARMVIXL);
1401};
1402
1403// Slow path generating a read barrier for a GC root.
1404class ReadBarrierForRootSlowPathARMVIXL : public SlowPathCodeARMVIXL {
1405 public:
1406 ReadBarrierForRootSlowPathARMVIXL(HInstruction* instruction, Location out, Location root)
1407 : SlowPathCodeARMVIXL(instruction), out_(out), root_(root) {
1408 DCHECK(kEmitCompilerReadBarrier);
1409 }
1410
1411 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
1412 LocationSummary* locations = instruction_->GetLocations();
1413 vixl32::Register reg_out = RegisterFrom(out_);
1414 DCHECK(locations->CanCall());
1415 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out.GetCode()));
1416 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
1417 << "Unexpected instruction in read barrier for GC root slow path: "
1418 << instruction_->DebugName();
1419
1420 __ Bind(GetEntryLabel());
1421 SaveLiveRegisters(codegen, locations);
1422
1423 InvokeRuntimeCallingConventionARMVIXL calling_convention;
1424 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
1425 arm_codegen->Move32(LocationFrom(calling_convention.GetRegisterAt(0)), root_);
1426 arm_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
1427 instruction_,
1428 instruction_->GetDexPc(),
1429 this);
1430 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
1431 arm_codegen->Move32(out_, LocationFrom(r0));
1432
1433 RestoreLiveRegisters(codegen, locations);
1434 __ B(GetExitLabel());
1435 }
1436
1437 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathARMVIXL"; }
1438
1439 private:
1440 const Location out_;
1441 const Location root_;
1442
1443 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathARMVIXL);
1444};
Scott Wakelingc34dba72016-10-03 10:14:44 +01001445
Scott Wakelingfe885462016-09-22 10:24:38 +01001446inline vixl32::Condition ARMCondition(IfCondition cond) {
1447 switch (cond) {
1448 case kCondEQ: return eq;
1449 case kCondNE: return ne;
1450 case kCondLT: return lt;
1451 case kCondLE: return le;
1452 case kCondGT: return gt;
1453 case kCondGE: return ge;
1454 case kCondB: return lo;
1455 case kCondBE: return ls;
1456 case kCondA: return hi;
1457 case kCondAE: return hs;
1458 }
1459 LOG(FATAL) << "Unreachable";
1460 UNREACHABLE();
1461}
1462
1463// Maps signed condition to unsigned condition.
1464inline vixl32::Condition ARMUnsignedCondition(IfCondition cond) {
1465 switch (cond) {
1466 case kCondEQ: return eq;
1467 case kCondNE: return ne;
1468 // Signed to unsigned.
1469 case kCondLT: return lo;
1470 case kCondLE: return ls;
1471 case kCondGT: return hi;
1472 case kCondGE: return hs;
1473 // Unsigned remain unchanged.
1474 case kCondB: return lo;
1475 case kCondBE: return ls;
1476 case kCondA: return hi;
1477 case kCondAE: return hs;
1478 }
1479 LOG(FATAL) << "Unreachable";
1480 UNREACHABLE();
1481}
1482
1483inline vixl32::Condition ARMFPCondition(IfCondition cond, bool gt_bias) {
1484 // The ARM condition codes can express all the necessary branches, see the
1485 // "Meaning (floating-point)" column in the table A8-1 of the ARMv7 reference manual.
1486 // There is no dex instruction or HIR that would need the missing conditions
1487 // "equal or unordered" or "not equal".
1488 switch (cond) {
1489 case kCondEQ: return eq;
1490 case kCondNE: return ne /* unordered */;
1491 case kCondLT: return gt_bias ? cc : lt /* unordered */;
1492 case kCondLE: return gt_bias ? ls : le /* unordered */;
1493 case kCondGT: return gt_bias ? hi /* unordered */ : gt;
1494 case kCondGE: return gt_bias ? cs /* unordered */ : ge;
1495 default:
1496 LOG(FATAL) << "UNREACHABLE";
1497 UNREACHABLE();
1498 }
1499}
1500
Anton Kirilov74234da2017-01-13 14:42:47 +00001501inline ShiftType ShiftFromOpKind(HDataProcWithShifterOp::OpKind op_kind) {
1502 switch (op_kind) {
1503 case HDataProcWithShifterOp::kASR: return ShiftType::ASR;
1504 case HDataProcWithShifterOp::kLSL: return ShiftType::LSL;
1505 case HDataProcWithShifterOp::kLSR: return ShiftType::LSR;
1506 default:
1507 LOG(FATAL) << "Unexpected op kind " << op_kind;
1508 UNREACHABLE();
1509 }
1510}
1511
Scott Wakelingfe885462016-09-22 10:24:38 +01001512void CodeGeneratorARMVIXL::DumpCoreRegister(std::ostream& stream, int reg) const {
1513 stream << vixl32::Register(reg);
1514}
1515
1516void CodeGeneratorARMVIXL::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
1517 stream << vixl32::SRegister(reg);
1518}
1519
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001520static uint32_t ComputeSRegisterListMask(const SRegisterList& regs) {
Scott Wakelingfe885462016-09-22 10:24:38 +01001521 uint32_t mask = 0;
1522 for (uint32_t i = regs.GetFirstSRegister().GetCode();
1523 i <= regs.GetLastSRegister().GetCode();
1524 ++i) {
1525 mask |= (1 << i);
1526 }
1527 return mask;
1528}
1529
Artem Serovd4cc5b22016-11-04 11:19:09 +00001530// Saves the register in the stack. Returns the size taken on stack.
1531size_t CodeGeneratorARMVIXL::SaveCoreRegister(size_t stack_index ATTRIBUTE_UNUSED,
1532 uint32_t reg_id ATTRIBUTE_UNUSED) {
1533 TODO_VIXL32(FATAL);
1534 return 0;
1535}
1536
1537// Restores the register from the stack. Returns the size taken on stack.
1538size_t CodeGeneratorARMVIXL::RestoreCoreRegister(size_t stack_index ATTRIBUTE_UNUSED,
1539 uint32_t reg_id ATTRIBUTE_UNUSED) {
1540 TODO_VIXL32(FATAL);
1541 return 0;
1542}
1543
1544size_t CodeGeneratorARMVIXL::SaveFloatingPointRegister(size_t stack_index ATTRIBUTE_UNUSED,
1545 uint32_t reg_id ATTRIBUTE_UNUSED) {
1546 TODO_VIXL32(FATAL);
1547 return 0;
1548}
1549
1550size_t CodeGeneratorARMVIXL::RestoreFloatingPointRegister(size_t stack_index ATTRIBUTE_UNUSED,
1551 uint32_t reg_id ATTRIBUTE_UNUSED) {
1552 TODO_VIXL32(FATAL);
1553 return 0;
Anton Kirilove28d9ae2016-10-25 18:17:23 +01001554}
1555
Anton Kirilov74234da2017-01-13 14:42:47 +00001556static void GenerateDataProcInstruction(HInstruction::InstructionKind kind,
1557 vixl32::Register out,
1558 vixl32::Register first,
1559 const Operand& second,
1560 CodeGeneratorARMVIXL* codegen) {
1561 if (second.IsImmediate() && second.GetImmediate() == 0) {
1562 const Operand in = kind == HInstruction::kAnd
1563 ? Operand(0)
1564 : Operand(first);
1565
1566 __ Mov(out, in);
1567 } else {
1568 switch (kind) {
1569 case HInstruction::kAdd:
1570 __ Add(out, first, second);
1571 break;
1572 case HInstruction::kAnd:
1573 __ And(out, first, second);
1574 break;
1575 case HInstruction::kOr:
1576 __ Orr(out, first, second);
1577 break;
1578 case HInstruction::kSub:
1579 __ Sub(out, first, second);
1580 break;
1581 case HInstruction::kXor:
1582 __ Eor(out, first, second);
1583 break;
1584 default:
1585 LOG(FATAL) << "Unexpected instruction kind: " << kind;
1586 UNREACHABLE();
1587 }
1588 }
1589}
1590
1591static void GenerateDataProc(HInstruction::InstructionKind kind,
1592 const Location& out,
1593 const Location& first,
1594 const Operand& second_lo,
1595 const Operand& second_hi,
1596 CodeGeneratorARMVIXL* codegen) {
1597 const vixl32::Register first_hi = HighRegisterFrom(first);
1598 const vixl32::Register first_lo = LowRegisterFrom(first);
1599 const vixl32::Register out_hi = HighRegisterFrom(out);
1600 const vixl32::Register out_lo = LowRegisterFrom(out);
1601
1602 if (kind == HInstruction::kAdd) {
1603 __ Adds(out_lo, first_lo, second_lo);
1604 __ Adc(out_hi, first_hi, second_hi);
1605 } else if (kind == HInstruction::kSub) {
1606 __ Subs(out_lo, first_lo, second_lo);
1607 __ Sbc(out_hi, first_hi, second_hi);
1608 } else {
1609 GenerateDataProcInstruction(kind, out_lo, first_lo, second_lo, codegen);
1610 GenerateDataProcInstruction(kind, out_hi, first_hi, second_hi, codegen);
1611 }
1612}
1613
1614static Operand GetShifterOperand(vixl32::Register rm, ShiftType shift, uint32_t shift_imm) {
1615 return shift_imm == 0 ? Operand(rm) : Operand(rm, shift, shift_imm);
1616}
1617
1618static void GenerateLongDataProc(HDataProcWithShifterOp* instruction,
1619 CodeGeneratorARMVIXL* codegen) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001620 DCHECK_EQ(instruction->GetType(), DataType::Type::kInt64);
Anton Kirilov74234da2017-01-13 14:42:47 +00001621 DCHECK(HDataProcWithShifterOp::IsShiftOp(instruction->GetOpKind()));
1622
1623 const LocationSummary* const locations = instruction->GetLocations();
1624 const uint32_t shift_value = instruction->GetShiftAmount();
1625 const HInstruction::InstructionKind kind = instruction->GetInstrKind();
1626 const Location first = locations->InAt(0);
1627 const Location second = locations->InAt(1);
1628 const Location out = locations->Out();
1629 const vixl32::Register first_hi = HighRegisterFrom(first);
1630 const vixl32::Register first_lo = LowRegisterFrom(first);
1631 const vixl32::Register out_hi = HighRegisterFrom(out);
1632 const vixl32::Register out_lo = LowRegisterFrom(out);
1633 const vixl32::Register second_hi = HighRegisterFrom(second);
1634 const vixl32::Register second_lo = LowRegisterFrom(second);
1635 const ShiftType shift = ShiftFromOpKind(instruction->GetOpKind());
1636
1637 if (shift_value >= 32) {
1638 if (shift == ShiftType::LSL) {
1639 GenerateDataProcInstruction(kind,
1640 out_hi,
1641 first_hi,
1642 Operand(second_lo, ShiftType::LSL, shift_value - 32),
1643 codegen);
1644 GenerateDataProcInstruction(kind, out_lo, first_lo, 0, codegen);
1645 } else if (shift == ShiftType::ASR) {
1646 GenerateDataProc(kind,
1647 out,
1648 first,
1649 GetShifterOperand(second_hi, ShiftType::ASR, shift_value - 32),
1650 Operand(second_hi, ShiftType::ASR, 31),
1651 codegen);
1652 } else {
1653 DCHECK_EQ(shift, ShiftType::LSR);
1654 GenerateDataProc(kind,
1655 out,
1656 first,
1657 GetShifterOperand(second_hi, ShiftType::LSR, shift_value - 32),
1658 0,
1659 codegen);
1660 }
1661 } else {
1662 DCHECK_GT(shift_value, 1U);
1663 DCHECK_LT(shift_value, 32U);
1664
1665 UseScratchRegisterScope temps(codegen->GetVIXLAssembler());
1666
1667 if (shift == ShiftType::LSL) {
1668 // We are not doing this for HInstruction::kAdd because the output will require
1669 // Location::kOutputOverlap; not applicable to other cases.
1670 if (kind == HInstruction::kOr || kind == HInstruction::kXor) {
1671 GenerateDataProcInstruction(kind,
1672 out_hi,
1673 first_hi,
1674 Operand(second_hi, ShiftType::LSL, shift_value),
1675 codegen);
1676 GenerateDataProcInstruction(kind,
1677 out_hi,
1678 out_hi,
1679 Operand(second_lo, ShiftType::LSR, 32 - shift_value),
1680 codegen);
1681 GenerateDataProcInstruction(kind,
1682 out_lo,
1683 first_lo,
1684 Operand(second_lo, ShiftType::LSL, shift_value),
1685 codegen);
1686 } else {
1687 const vixl32::Register temp = temps.Acquire();
1688
1689 __ Lsl(temp, second_hi, shift_value);
1690 __ Orr(temp, temp, Operand(second_lo, ShiftType::LSR, 32 - shift_value));
1691 GenerateDataProc(kind,
1692 out,
1693 first,
1694 Operand(second_lo, ShiftType::LSL, shift_value),
1695 temp,
1696 codegen);
1697 }
1698 } else {
1699 DCHECK(shift == ShiftType::ASR || shift == ShiftType::LSR);
1700
1701 // We are not doing this for HInstruction::kAdd because the output will require
1702 // Location::kOutputOverlap; not applicable to other cases.
1703 if (kind == HInstruction::kOr || kind == HInstruction::kXor) {
1704 GenerateDataProcInstruction(kind,
1705 out_lo,
1706 first_lo,
1707 Operand(second_lo, ShiftType::LSR, shift_value),
1708 codegen);
1709 GenerateDataProcInstruction(kind,
1710 out_lo,
1711 out_lo,
1712 Operand(second_hi, ShiftType::LSL, 32 - shift_value),
1713 codegen);
1714 GenerateDataProcInstruction(kind,
1715 out_hi,
1716 first_hi,
1717 Operand(second_hi, shift, shift_value),
1718 codegen);
1719 } else {
1720 const vixl32::Register temp = temps.Acquire();
1721
1722 __ Lsr(temp, second_lo, shift_value);
1723 __ Orr(temp, temp, Operand(second_hi, ShiftType::LSL, 32 - shift_value));
1724 GenerateDataProc(kind,
1725 out,
1726 first,
1727 temp,
1728 Operand(second_hi, shift, shift_value),
1729 codegen);
1730 }
1731 }
1732 }
1733}
1734
Donghui Bai426b49c2016-11-08 14:55:38 +08001735static void GenerateVcmp(HInstruction* instruction, CodeGeneratorARMVIXL* codegen) {
1736 const Location rhs_loc = instruction->GetLocations()->InAt(1);
1737 if (rhs_loc.IsConstant()) {
1738 // 0.0 is the only immediate that can be encoded directly in
1739 // a VCMP instruction.
1740 //
1741 // Both the JLS (section 15.20.1) and the JVMS (section 6.5)
1742 // specify that in a floating-point comparison, positive zero
1743 // and negative zero are considered equal, so we can use the
1744 // literal 0.0 for both cases here.
1745 //
1746 // Note however that some methods (Float.equal, Float.compare,
1747 // Float.compareTo, Double.equal, Double.compare,
1748 // Double.compareTo, Math.max, Math.min, StrictMath.max,
1749 // StrictMath.min) consider 0.0 to be (strictly) greater than
1750 // -0.0. So if we ever translate calls to these methods into a
1751 // HCompare instruction, we must handle the -0.0 case with
1752 // care here.
1753 DCHECK(rhs_loc.GetConstant()->IsArithmeticZero());
1754
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001755 const DataType::Type type = instruction->InputAt(0)->GetType();
Donghui Bai426b49c2016-11-08 14:55:38 +08001756
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001757 if (type == DataType::Type::kFloat32) {
Donghui Bai426b49c2016-11-08 14:55:38 +08001758 __ Vcmp(F32, InputSRegisterAt(instruction, 0), 0.0);
1759 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001760 DCHECK_EQ(type, DataType::Type::kFloat64);
Donghui Bai426b49c2016-11-08 14:55:38 +08001761 __ Vcmp(F64, InputDRegisterAt(instruction, 0), 0.0);
1762 }
1763 } else {
1764 __ Vcmp(InputVRegisterAt(instruction, 0), InputVRegisterAt(instruction, 1));
1765 }
1766}
1767
Anton Kirilov5601d4e2017-05-11 19:33:50 +01001768static int64_t AdjustConstantForCondition(int64_t value,
1769 IfCondition* condition,
1770 IfCondition* opposite) {
1771 if (value == 1) {
1772 if (*condition == kCondB) {
1773 value = 0;
1774 *condition = kCondEQ;
1775 *opposite = kCondNE;
1776 } else if (*condition == kCondAE) {
1777 value = 0;
1778 *condition = kCondNE;
1779 *opposite = kCondEQ;
1780 }
1781 } else if (value == -1) {
1782 if (*condition == kCondGT) {
1783 value = 0;
1784 *condition = kCondGE;
1785 *opposite = kCondLT;
1786 } else if (*condition == kCondLE) {
1787 value = 0;
1788 *condition = kCondLT;
1789 *opposite = kCondGE;
1790 }
1791 }
1792
1793 return value;
1794}
1795
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001796static std::pair<vixl32::Condition, vixl32::Condition> GenerateLongTestConstant(
1797 HCondition* condition,
1798 bool invert,
1799 CodeGeneratorARMVIXL* codegen) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001800 DCHECK_EQ(condition->GetLeft()->GetType(), DataType::Type::kInt64);
Donghui Bai426b49c2016-11-08 14:55:38 +08001801
1802 const LocationSummary* const locations = condition->GetLocations();
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001803 IfCondition cond = condition->GetCondition();
1804 IfCondition opposite = condition->GetOppositeCondition();
1805
1806 if (invert) {
1807 std::swap(cond, opposite);
1808 }
1809
1810 std::pair<vixl32::Condition, vixl32::Condition> ret(eq, ne);
Donghui Bai426b49c2016-11-08 14:55:38 +08001811 const Location left = locations->InAt(0);
1812 const Location right = locations->InAt(1);
1813
1814 DCHECK(right.IsConstant());
1815
1816 const vixl32::Register left_high = HighRegisterFrom(left);
1817 const vixl32::Register left_low = LowRegisterFrom(left);
Anton Kirilov5601d4e2017-05-11 19:33:50 +01001818 int64_t value = AdjustConstantForCondition(Int64ConstantFrom(right), &cond, &opposite);
1819 UseScratchRegisterScope temps(codegen->GetVIXLAssembler());
1820
1821 // Comparisons against 0 are common enough to deserve special attention.
1822 if (value == 0) {
1823 switch (cond) {
1824 case kCondNE:
1825 // x > 0 iff x != 0 when the comparison is unsigned.
1826 case kCondA:
1827 ret = std::make_pair(ne, eq);
1828 FALLTHROUGH_INTENDED;
1829 case kCondEQ:
1830 // x <= 0 iff x == 0 when the comparison is unsigned.
1831 case kCondBE:
1832 __ Orrs(temps.Acquire(), left_low, left_high);
1833 return ret;
1834 case kCondLT:
1835 case kCondGE:
1836 __ Cmp(left_high, 0);
1837 return std::make_pair(ARMCondition(cond), ARMCondition(opposite));
1838 // Trivially true or false.
1839 case kCondB:
1840 ret = std::make_pair(ne, eq);
1841 FALLTHROUGH_INTENDED;
1842 case kCondAE:
1843 __ Cmp(left_low, left_low);
1844 return ret;
1845 default:
1846 break;
1847 }
1848 }
Donghui Bai426b49c2016-11-08 14:55:38 +08001849
1850 switch (cond) {
1851 case kCondEQ:
1852 case kCondNE:
1853 case kCondB:
1854 case kCondBE:
1855 case kCondA:
1856 case kCondAE: {
Anton Kirilov23b752b2017-07-20 14:40:44 +01001857 const uint32_t value_low = Low32Bits(value);
1858 Operand operand_low(value_low);
1859
Donghui Bai426b49c2016-11-08 14:55:38 +08001860 __ Cmp(left_high, High32Bits(value));
1861
Anton Kirilov23b752b2017-07-20 14:40:44 +01001862 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
1863 // we must ensure that the operands corresponding to the least significant
1864 // halves of the inputs fit into a 16-bit CMP encoding.
1865 if (!left_low.IsLow() || !IsUint<8>(value_low)) {
1866 operand_low = Operand(temps.Acquire());
1867 __ Mov(LeaveFlags, operand_low.GetBaseRegister(), value_low);
1868 }
1869
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001870 // We use the scope because of the IT block that follows.
Donghui Bai426b49c2016-11-08 14:55:38 +08001871 ExactAssemblyScope guard(codegen->GetVIXLAssembler(),
1872 2 * vixl32::k16BitT32InstructionSizeInBytes,
1873 CodeBufferCheckScope::kExactSize);
1874
1875 __ it(eq);
Anton Kirilov23b752b2017-07-20 14:40:44 +01001876 __ cmp(eq, left_low, operand_low);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001877 ret = std::make_pair(ARMUnsignedCondition(cond), ARMUnsignedCondition(opposite));
Donghui Bai426b49c2016-11-08 14:55:38 +08001878 break;
1879 }
1880 case kCondLE:
1881 case kCondGT:
1882 // Trivially true or false.
1883 if (value == std::numeric_limits<int64_t>::max()) {
1884 __ Cmp(left_low, left_low);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001885 ret = cond == kCondLE ? std::make_pair(eq, ne) : std::make_pair(ne, eq);
Donghui Bai426b49c2016-11-08 14:55:38 +08001886 break;
1887 }
1888
1889 if (cond == kCondLE) {
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001890 DCHECK_EQ(opposite, kCondGT);
Donghui Bai426b49c2016-11-08 14:55:38 +08001891 cond = kCondLT;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001892 opposite = kCondGE;
Donghui Bai426b49c2016-11-08 14:55:38 +08001893 } else {
1894 DCHECK_EQ(cond, kCondGT);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001895 DCHECK_EQ(opposite, kCondLE);
Donghui Bai426b49c2016-11-08 14:55:38 +08001896 cond = kCondGE;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001897 opposite = kCondLT;
Donghui Bai426b49c2016-11-08 14:55:38 +08001898 }
1899
1900 value++;
1901 FALLTHROUGH_INTENDED;
1902 case kCondGE:
1903 case kCondLT: {
Donghui Bai426b49c2016-11-08 14:55:38 +08001904 __ Cmp(left_low, Low32Bits(value));
1905 __ Sbcs(temps.Acquire(), left_high, High32Bits(value));
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001906 ret = std::make_pair(ARMCondition(cond), ARMCondition(opposite));
Donghui Bai426b49c2016-11-08 14:55:38 +08001907 break;
1908 }
1909 default:
1910 LOG(FATAL) << "Unreachable";
1911 UNREACHABLE();
1912 }
1913
1914 return ret;
1915}
1916
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001917static std::pair<vixl32::Condition, vixl32::Condition> GenerateLongTest(
1918 HCondition* condition,
1919 bool invert,
1920 CodeGeneratorARMVIXL* codegen) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001921 DCHECK_EQ(condition->GetLeft()->GetType(), DataType::Type::kInt64);
Donghui Bai426b49c2016-11-08 14:55:38 +08001922
1923 const LocationSummary* const locations = condition->GetLocations();
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001924 IfCondition cond = condition->GetCondition();
1925 IfCondition opposite = condition->GetOppositeCondition();
1926
1927 if (invert) {
1928 std::swap(cond, opposite);
1929 }
1930
1931 std::pair<vixl32::Condition, vixl32::Condition> ret(eq, ne);
Donghui Bai426b49c2016-11-08 14:55:38 +08001932 Location left = locations->InAt(0);
1933 Location right = locations->InAt(1);
1934
1935 DCHECK(right.IsRegisterPair());
1936
1937 switch (cond) {
1938 case kCondEQ:
1939 case kCondNE:
1940 case kCondB:
1941 case kCondBE:
1942 case kCondA:
1943 case kCondAE: {
1944 __ Cmp(HighRegisterFrom(left), HighRegisterFrom(right));
1945
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001946 // We use the scope because of the IT block that follows.
Donghui Bai426b49c2016-11-08 14:55:38 +08001947 ExactAssemblyScope guard(codegen->GetVIXLAssembler(),
1948 2 * vixl32::k16BitT32InstructionSizeInBytes,
1949 CodeBufferCheckScope::kExactSize);
1950
1951 __ it(eq);
1952 __ cmp(eq, LowRegisterFrom(left), LowRegisterFrom(right));
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001953 ret = std::make_pair(ARMUnsignedCondition(cond), ARMUnsignedCondition(opposite));
Donghui Bai426b49c2016-11-08 14:55:38 +08001954 break;
1955 }
1956 case kCondLE:
1957 case kCondGT:
1958 if (cond == kCondLE) {
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001959 DCHECK_EQ(opposite, kCondGT);
Donghui Bai426b49c2016-11-08 14:55:38 +08001960 cond = kCondGE;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001961 opposite = kCondLT;
Donghui Bai426b49c2016-11-08 14:55:38 +08001962 } else {
1963 DCHECK_EQ(cond, kCondGT);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001964 DCHECK_EQ(opposite, kCondLE);
Donghui Bai426b49c2016-11-08 14:55:38 +08001965 cond = kCondLT;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001966 opposite = kCondGE;
Donghui Bai426b49c2016-11-08 14:55:38 +08001967 }
1968
1969 std::swap(left, right);
1970 FALLTHROUGH_INTENDED;
1971 case kCondGE:
1972 case kCondLT: {
1973 UseScratchRegisterScope temps(codegen->GetVIXLAssembler());
1974
1975 __ Cmp(LowRegisterFrom(left), LowRegisterFrom(right));
1976 __ Sbcs(temps.Acquire(), HighRegisterFrom(left), HighRegisterFrom(right));
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001977 ret = std::make_pair(ARMCondition(cond), ARMCondition(opposite));
Donghui Bai426b49c2016-11-08 14:55:38 +08001978 break;
1979 }
1980 default:
1981 LOG(FATAL) << "Unreachable";
1982 UNREACHABLE();
1983 }
1984
1985 return ret;
1986}
1987
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001988static std::pair<vixl32::Condition, vixl32::Condition> GenerateTest(HCondition* condition,
1989 bool invert,
1990 CodeGeneratorARMVIXL* codegen) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001991 const DataType::Type type = condition->GetLeft()->GetType();
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001992 IfCondition cond = condition->GetCondition();
1993 IfCondition opposite = condition->GetOppositeCondition();
1994 std::pair<vixl32::Condition, vixl32::Condition> ret(eq, ne);
Donghui Bai426b49c2016-11-08 14:55:38 +08001995
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001996 if (invert) {
1997 std::swap(cond, opposite);
1998 }
Donghui Bai426b49c2016-11-08 14:55:38 +08001999
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002000 if (type == DataType::Type::kInt64) {
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002001 ret = condition->GetLocations()->InAt(1).IsConstant()
2002 ? GenerateLongTestConstant(condition, invert, codegen)
2003 : GenerateLongTest(condition, invert, codegen);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002004 } else if (DataType::IsFloatingPointType(type)) {
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002005 GenerateVcmp(condition, codegen);
2006 __ Vmrs(RegisterOrAPSR_nzcv(kPcCode), FPSCR);
2007 ret = std::make_pair(ARMFPCondition(cond, condition->IsGtBias()),
2008 ARMFPCondition(opposite, condition->IsGtBias()));
Donghui Bai426b49c2016-11-08 14:55:38 +08002009 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002010 DCHECK(DataType::IsIntegralType(type) || type == DataType::Type::kReference) << type;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002011 __ Cmp(InputRegisterAt(condition, 0), InputOperandAt(condition, 1));
2012 ret = std::make_pair(ARMCondition(cond), ARMCondition(opposite));
Donghui Bai426b49c2016-11-08 14:55:38 +08002013 }
2014
2015 return ret;
2016}
2017
Anton Kirilov5601d4e2017-05-11 19:33:50 +01002018static void GenerateConditionGeneric(HCondition* cond, CodeGeneratorARMVIXL* codegen) {
Anton Kirilov5601d4e2017-05-11 19:33:50 +01002019 const vixl32::Register out = OutputRegister(cond);
2020 const auto condition = GenerateTest(cond, false, codegen);
2021
2022 __ Mov(LeaveFlags, out, 0);
2023
2024 if (out.IsLow()) {
2025 // We use the scope because of the IT block that follows.
2026 ExactAssemblyScope guard(codegen->GetVIXLAssembler(),
2027 2 * vixl32::k16BitT32InstructionSizeInBytes,
2028 CodeBufferCheckScope::kExactSize);
2029
2030 __ it(condition.first);
2031 __ mov(condition.first, out, 1);
2032 } else {
2033 vixl32::Label done_label;
2034 vixl32::Label* const final_label = codegen->GetFinalLabel(cond, &done_label);
2035
2036 __ B(condition.second, final_label, /* far_target */ false);
2037 __ Mov(out, 1);
2038
2039 if (done_label.IsReferenced()) {
2040 __ Bind(&done_label);
2041 }
2042 }
2043}
2044
2045static void GenerateEqualLong(HCondition* cond, CodeGeneratorARMVIXL* codegen) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002046 DCHECK_EQ(cond->GetLeft()->GetType(), DataType::Type::kInt64);
Anton Kirilov5601d4e2017-05-11 19:33:50 +01002047
2048 const LocationSummary* const locations = cond->GetLocations();
2049 IfCondition condition = cond->GetCondition();
2050 const vixl32::Register out = OutputRegister(cond);
2051 const Location left = locations->InAt(0);
2052 const Location right = locations->InAt(1);
2053 vixl32::Register left_high = HighRegisterFrom(left);
2054 vixl32::Register left_low = LowRegisterFrom(left);
2055 vixl32::Register temp;
2056 UseScratchRegisterScope temps(codegen->GetVIXLAssembler());
2057
2058 if (right.IsConstant()) {
2059 IfCondition opposite = cond->GetOppositeCondition();
2060 const int64_t value = AdjustConstantForCondition(Int64ConstantFrom(right),
2061 &condition,
2062 &opposite);
2063 Operand right_high = High32Bits(value);
2064 Operand right_low = Low32Bits(value);
2065
2066 // The output uses Location::kNoOutputOverlap.
2067 if (out.Is(left_high)) {
2068 std::swap(left_low, left_high);
2069 std::swap(right_low, right_high);
2070 }
2071
2072 __ Sub(out, left_low, right_low);
2073 temp = temps.Acquire();
2074 __ Sub(temp, left_high, right_high);
2075 } else {
2076 DCHECK(right.IsRegisterPair());
2077 temp = temps.Acquire();
2078 __ Sub(temp, left_high, HighRegisterFrom(right));
2079 __ Sub(out, left_low, LowRegisterFrom(right));
2080 }
2081
2082 // Need to check after calling AdjustConstantForCondition().
2083 DCHECK(condition == kCondEQ || condition == kCondNE) << condition;
2084
2085 if (condition == kCondNE && out.IsLow()) {
2086 __ Orrs(out, out, temp);
2087
2088 // We use the scope because of the IT block that follows.
2089 ExactAssemblyScope guard(codegen->GetVIXLAssembler(),
2090 2 * vixl32::k16BitT32InstructionSizeInBytes,
2091 CodeBufferCheckScope::kExactSize);
2092
2093 __ it(ne);
2094 __ mov(ne, out, 1);
2095 } else {
2096 __ Orr(out, out, temp);
2097 codegen->GenerateConditionWithZero(condition, out, out, temp);
2098 }
2099}
2100
Anton Kirilov5601d4e2017-05-11 19:33:50 +01002101static void GenerateConditionLong(HCondition* cond, CodeGeneratorARMVIXL* codegen) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002102 DCHECK_EQ(cond->GetLeft()->GetType(), DataType::Type::kInt64);
Anton Kirilov5601d4e2017-05-11 19:33:50 +01002103
2104 const LocationSummary* const locations = cond->GetLocations();
2105 IfCondition condition = cond->GetCondition();
2106 const vixl32::Register out = OutputRegister(cond);
2107 const Location left = locations->InAt(0);
2108 const Location right = locations->InAt(1);
2109
2110 if (right.IsConstant()) {
2111 IfCondition opposite = cond->GetOppositeCondition();
2112
2113 // Comparisons against 0 are common enough to deserve special attention.
2114 if (AdjustConstantForCondition(Int64ConstantFrom(right), &condition, &opposite) == 0) {
2115 switch (condition) {
2116 case kCondNE:
2117 case kCondA:
2118 if (out.IsLow()) {
2119 // We only care if both input registers are 0 or not.
2120 __ Orrs(out, LowRegisterFrom(left), HighRegisterFrom(left));
2121
2122 // We use the scope because of the IT block that follows.
2123 ExactAssemblyScope guard(codegen->GetVIXLAssembler(),
2124 2 * vixl32::k16BitT32InstructionSizeInBytes,
2125 CodeBufferCheckScope::kExactSize);
2126
2127 __ it(ne);
2128 __ mov(ne, out, 1);
2129 return;
2130 }
2131
2132 FALLTHROUGH_INTENDED;
2133 case kCondEQ:
2134 case kCondBE:
2135 // We only care if both input registers are 0 or not.
2136 __ Orr(out, LowRegisterFrom(left), HighRegisterFrom(left));
2137 codegen->GenerateConditionWithZero(condition, out, out);
2138 return;
2139 case kCondLT:
2140 case kCondGE:
2141 // We only care about the sign bit.
2142 FALLTHROUGH_INTENDED;
2143 case kCondAE:
2144 case kCondB:
2145 codegen->GenerateConditionWithZero(condition, out, HighRegisterFrom(left));
2146 return;
2147 case kCondLE:
2148 case kCondGT:
2149 default:
2150 break;
2151 }
2152 }
2153 }
2154
Anton Kirilov23b752b2017-07-20 14:40:44 +01002155 // If `out` is a low register, then the GenerateConditionGeneric()
2156 // function generates a shorter code sequence that is still branchless.
2157 if ((condition == kCondEQ || condition == kCondNE) && !out.IsLow()) {
Anton Kirilov5601d4e2017-05-11 19:33:50 +01002158 GenerateEqualLong(cond, codegen);
2159 return;
2160 }
2161
Anton Kirilov23b752b2017-07-20 14:40:44 +01002162 GenerateConditionGeneric(cond, codegen);
Anton Kirilov5601d4e2017-05-11 19:33:50 +01002163}
2164
Roland Levillain6d729a72017-06-30 18:34:01 +01002165static void GenerateConditionIntegralOrNonPrimitive(HCondition* cond,
2166 CodeGeneratorARMVIXL* codegen) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002167 const DataType::Type type = cond->GetLeft()->GetType();
Anton Kirilov5601d4e2017-05-11 19:33:50 +01002168
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002169 DCHECK(DataType::IsIntegralType(type) || type == DataType::Type::kReference) << type;
Anton Kirilov5601d4e2017-05-11 19:33:50 +01002170
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002171 if (type == DataType::Type::kInt64) {
Anton Kirilov5601d4e2017-05-11 19:33:50 +01002172 GenerateConditionLong(cond, codegen);
2173 return;
2174 }
2175
2176 IfCondition condition = cond->GetCondition();
2177 vixl32::Register in = InputRegisterAt(cond, 0);
2178 const vixl32::Register out = OutputRegister(cond);
2179 const Location right = cond->GetLocations()->InAt(1);
2180 int64_t value;
2181
2182 if (right.IsConstant()) {
2183 IfCondition opposite = cond->GetOppositeCondition();
2184
2185 value = AdjustConstantForCondition(Int64ConstantFrom(right), &condition, &opposite);
2186
2187 // Comparisons against 0 are common enough to deserve special attention.
2188 if (value == 0) {
2189 switch (condition) {
2190 case kCondNE:
2191 case kCondA:
2192 if (out.IsLow() && out.Is(in)) {
2193 __ Cmp(out, 0);
2194
2195 // We use the scope because of the IT block that follows.
2196 ExactAssemblyScope guard(codegen->GetVIXLAssembler(),
2197 2 * vixl32::k16BitT32InstructionSizeInBytes,
2198 CodeBufferCheckScope::kExactSize);
2199
2200 __ it(ne);
2201 __ mov(ne, out, 1);
2202 return;
2203 }
2204
2205 FALLTHROUGH_INTENDED;
2206 case kCondEQ:
2207 case kCondBE:
2208 case kCondLT:
2209 case kCondGE:
2210 case kCondAE:
2211 case kCondB:
2212 codegen->GenerateConditionWithZero(condition, out, in);
2213 return;
2214 case kCondLE:
2215 case kCondGT:
2216 default:
2217 break;
2218 }
2219 }
2220 }
2221
2222 if (condition == kCondEQ || condition == kCondNE) {
2223 Operand operand(0);
2224
2225 if (right.IsConstant()) {
2226 operand = Operand::From(value);
2227 } else if (out.Is(RegisterFrom(right))) {
2228 // Avoid 32-bit instructions if possible.
2229 operand = InputOperandAt(cond, 0);
2230 in = RegisterFrom(right);
2231 } else {
2232 operand = InputOperandAt(cond, 1);
2233 }
2234
2235 if (condition == kCondNE && out.IsLow()) {
2236 __ Subs(out, in, operand);
2237
2238 // We use the scope because of the IT block that follows.
2239 ExactAssemblyScope guard(codegen->GetVIXLAssembler(),
2240 2 * vixl32::k16BitT32InstructionSizeInBytes,
2241 CodeBufferCheckScope::kExactSize);
2242
2243 __ it(ne);
2244 __ mov(ne, out, 1);
2245 } else {
2246 __ Sub(out, in, operand);
2247 codegen->GenerateConditionWithZero(condition, out, out);
2248 }
2249
2250 return;
2251 }
2252
2253 GenerateConditionGeneric(cond, codegen);
2254}
2255
Donghui Bai426b49c2016-11-08 14:55:38 +08002256static bool CanEncodeConstantAs8BitImmediate(HConstant* constant) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002257 const DataType::Type type = constant->GetType();
Donghui Bai426b49c2016-11-08 14:55:38 +08002258 bool ret = false;
2259
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002260 DCHECK(DataType::IsIntegralType(type) || type == DataType::Type::kReference) << type;
Donghui Bai426b49c2016-11-08 14:55:38 +08002261
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002262 if (type == DataType::Type::kInt64) {
Donghui Bai426b49c2016-11-08 14:55:38 +08002263 const uint64_t value = Uint64ConstantFrom(constant);
2264
2265 ret = IsUint<8>(Low32Bits(value)) && IsUint<8>(High32Bits(value));
2266 } else {
2267 ret = IsUint<8>(Int32ConstantFrom(constant));
2268 }
2269
2270 return ret;
2271}
2272
2273static Location Arm8BitEncodableConstantOrRegister(HInstruction* constant) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002274 DCHECK(!DataType::IsFloatingPointType(constant->GetType()));
Donghui Bai426b49c2016-11-08 14:55:38 +08002275
2276 if (constant->IsConstant() && CanEncodeConstantAs8BitImmediate(constant->AsConstant())) {
2277 return Location::ConstantLocation(constant->AsConstant());
2278 }
2279
2280 return Location::RequiresRegister();
2281}
2282
2283static bool CanGenerateConditionalMove(const Location& out, const Location& src) {
2284 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
2285 // we check that we are not dealing with floating-point output (there is no
2286 // 16-bit VMOV encoding).
2287 if (!out.IsRegister() && !out.IsRegisterPair()) {
2288 return false;
2289 }
2290
2291 // For constants, we also check that the output is in one or two low registers,
2292 // and that the constants fit in an 8-bit unsigned integer, so that a 16-bit
2293 // MOV encoding can be used.
2294 if (src.IsConstant()) {
2295 if (!CanEncodeConstantAs8BitImmediate(src.GetConstant())) {
2296 return false;
2297 }
2298
2299 if (out.IsRegister()) {
2300 if (!RegisterFrom(out).IsLow()) {
2301 return false;
2302 }
2303 } else {
2304 DCHECK(out.IsRegisterPair());
2305
2306 if (!HighRegisterFrom(out).IsLow()) {
2307 return false;
2308 }
2309 }
2310 }
2311
2312 return true;
2313}
2314
Scott Wakelingfe885462016-09-22 10:24:38 +01002315#undef __
2316
Donghui Bai426b49c2016-11-08 14:55:38 +08002317vixl32::Label* CodeGeneratorARMVIXL::GetFinalLabel(HInstruction* instruction,
2318 vixl32::Label* final_label) {
2319 DCHECK(!instruction->IsControlFlow() && !instruction->IsSuspendCheck());
Anton Kirilov6f644202017-02-27 18:29:45 +00002320 DCHECK(!instruction->IsInvoke() || !instruction->GetLocations()->CanCall());
Donghui Bai426b49c2016-11-08 14:55:38 +08002321
2322 const HBasicBlock* const block = instruction->GetBlock();
2323 const HLoopInformation* const info = block->GetLoopInformation();
2324 HInstruction* const next = instruction->GetNext();
2325
2326 // Avoid a branch to a branch.
2327 if (next->IsGoto() && (info == nullptr ||
2328 !info->IsBackEdge(*block) ||
2329 !info->HasSuspendCheck())) {
2330 final_label = GetLabelOf(next->AsGoto()->GetSuccessor());
2331 }
2332
2333 return final_label;
2334}
2335
Scott Wakelingfe885462016-09-22 10:24:38 +01002336CodeGeneratorARMVIXL::CodeGeneratorARMVIXL(HGraph* graph,
2337 const ArmInstructionSetFeatures& isa_features,
2338 const CompilerOptions& compiler_options,
2339 OptimizingCompilerStats* stats)
2340 : CodeGenerator(graph,
2341 kNumberOfCoreRegisters,
2342 kNumberOfSRegisters,
2343 kNumberOfRegisterPairs,
2344 kCoreCalleeSaves.GetList(),
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002345 ComputeSRegisterListMask(kFpuCalleeSaves),
Scott Wakelingfe885462016-09-22 10:24:38 +01002346 compiler_options,
2347 stats),
Vladimir Markoca6fff82017-10-03 14:49:14 +01002348 block_labels_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
2349 jump_tables_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Scott Wakelingfe885462016-09-22 10:24:38 +01002350 location_builder_(graph, this),
2351 instruction_visitor_(graph, this),
Vladimir Markoca6fff82017-10-03 14:49:14 +01002352 move_resolver_(graph->GetAllocator(), this),
2353 assembler_(graph->GetAllocator()),
Artem Serovd4cc5b22016-11-04 11:19:09 +00002354 isa_features_(isa_features),
Artem Serovc5fcb442016-12-02 19:19:58 +00002355 uint32_literals_(std::less<uint32_t>(),
Vladimir Markoca6fff82017-10-03 14:49:14 +01002356 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko59eb30f2018-02-20 11:52:34 +00002357 boot_image_method_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +01002358 method_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko59eb30f2018-02-20 11:52:34 +00002359 boot_image_type_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +01002360 type_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko59eb30f2018-02-20 11:52:34 +00002361 boot_image_string_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +01002362 string_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
2363 baker_read_barrier_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Artem Serovc5fcb442016-12-02 19:19:58 +00002364 jit_string_patches_(StringReferenceValueComparator(),
Vladimir Markoca6fff82017-10-03 14:49:14 +01002365 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Artem Serovc5fcb442016-12-02 19:19:58 +00002366 jit_class_patches_(TypeReferenceValueComparator(),
Vladimir Markoca6fff82017-10-03 14:49:14 +01002367 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)) {
Scott Wakelingfe885462016-09-22 10:24:38 +01002368 // Always save the LR register to mimic Quick.
2369 AddAllocatedRegister(Location::RegisterLocation(LR));
Nicolas Geoffray13a797b2017-03-15 16:41:31 +00002370 // Give D30 and D31 as scratch register to VIXL. The register allocator only works on
2371 // S0-S31, which alias to D0-D15.
2372 GetVIXLAssembler()->GetScratchVRegisterList()->Combine(d31);
2373 GetVIXLAssembler()->GetScratchVRegisterList()->Combine(d30);
Scott Wakelingfe885462016-09-22 10:24:38 +01002374}
2375
Artem Serov551b28f2016-10-18 19:11:30 +01002376void JumpTableARMVIXL::EmitTable(CodeGeneratorARMVIXL* codegen) {
2377 uint32_t num_entries = switch_instr_->GetNumEntries();
2378 DCHECK_GE(num_entries, kPackedSwitchCompareJumpThreshold);
2379
2380 // We are about to use the assembler to place literals directly. Make sure we have enough
Scott Wakelingb77051e2016-11-21 19:46:00 +00002381 // underlying code buffer and we have generated a jump table of the right size, using
2382 // codegen->GetVIXLAssembler()->GetBuffer().Align();
Artem Serov0fb37192016-12-06 18:13:40 +00002383 ExactAssemblyScope aas(codegen->GetVIXLAssembler(),
2384 num_entries * sizeof(int32_t),
2385 CodeBufferCheckScope::kMaximumSize);
Artem Serov551b28f2016-10-18 19:11:30 +01002386 // TODO(VIXL): Check that using lower case bind is fine here.
2387 codegen->GetVIXLAssembler()->bind(&table_start_);
Artem Serov09a940d2016-11-11 16:15:11 +00002388 for (uint32_t i = 0; i < num_entries; i++) {
2389 codegen->GetVIXLAssembler()->place(bb_addresses_[i].get());
2390 }
2391}
2392
2393void JumpTableARMVIXL::FixTable(CodeGeneratorARMVIXL* codegen) {
2394 uint32_t num_entries = switch_instr_->GetNumEntries();
2395 DCHECK_GE(num_entries, kPackedSwitchCompareJumpThreshold);
2396
Artem Serov551b28f2016-10-18 19:11:30 +01002397 const ArenaVector<HBasicBlock*>& successors = switch_instr_->GetBlock()->GetSuccessors();
2398 for (uint32_t i = 0; i < num_entries; i++) {
2399 vixl32::Label* target_label = codegen->GetLabelOf(successors[i]);
2400 DCHECK(target_label->IsBound());
2401 int32_t jump_offset = target_label->GetLocation() - table_start_.GetLocation();
2402 // When doing BX to address we need to have lower bit set to 1 in T32.
2403 if (codegen->GetVIXLAssembler()->IsUsingT32()) {
2404 jump_offset++;
2405 }
2406 DCHECK_GT(jump_offset, std::numeric_limits<int32_t>::min());
2407 DCHECK_LE(jump_offset, std::numeric_limits<int32_t>::max());
Artem Serov09a940d2016-11-11 16:15:11 +00002408
Scott Wakelingb77051e2016-11-21 19:46:00 +00002409 bb_addresses_[i].get()->UpdateValue(jump_offset, codegen->GetVIXLAssembler()->GetBuffer());
Artem Serov551b28f2016-10-18 19:11:30 +01002410 }
2411}
2412
Artem Serov09a940d2016-11-11 16:15:11 +00002413void CodeGeneratorARMVIXL::FixJumpTables() {
Artem Serov551b28f2016-10-18 19:11:30 +01002414 for (auto&& jump_table : jump_tables_) {
Artem Serov09a940d2016-11-11 16:15:11 +00002415 jump_table->FixTable(this);
Artem Serov551b28f2016-10-18 19:11:30 +01002416 }
2417}
2418
Andreas Gampeca620d72016-11-08 08:09:33 -08002419#define __ reinterpret_cast<ArmVIXLAssembler*>(GetAssembler())->GetVIXLAssembler()-> // NOLINT
Scott Wakelingfe885462016-09-22 10:24:38 +01002420
2421void CodeGeneratorARMVIXL::Finalize(CodeAllocator* allocator) {
Artem Serov09a940d2016-11-11 16:15:11 +00002422 FixJumpTables();
Scott Wakelingfe885462016-09-22 10:24:38 +01002423 GetAssembler()->FinalizeCode();
2424 CodeGenerator::Finalize(allocator);
2425}
2426
2427void CodeGeneratorARMVIXL::SetupBlockedRegisters() const {
Scott Wakelingfe885462016-09-22 10:24:38 +01002428 // Stack register, LR and PC are always reserved.
2429 blocked_core_registers_[SP] = true;
2430 blocked_core_registers_[LR] = true;
2431 blocked_core_registers_[PC] = true;
2432
Roland Levillain6d729a72017-06-30 18:34:01 +01002433 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2434 // Reserve marking register.
2435 blocked_core_registers_[MR] = true;
2436 }
2437
Scott Wakelingfe885462016-09-22 10:24:38 +01002438 // Reserve thread register.
2439 blocked_core_registers_[TR] = true;
2440
2441 // Reserve temp register.
2442 blocked_core_registers_[IP] = true;
2443
2444 if (GetGraph()->IsDebuggable()) {
2445 // Stubs do not save callee-save floating point registers. If the graph
2446 // is debuggable, we need to deal with these registers differently. For
2447 // now, just block them.
2448 for (uint32_t i = kFpuCalleeSaves.GetFirstSRegister().GetCode();
2449 i <= kFpuCalleeSaves.GetLastSRegister().GetCode();
2450 ++i) {
2451 blocked_fpu_registers_[i] = true;
2452 }
2453 }
Scott Wakelingfe885462016-09-22 10:24:38 +01002454}
2455
Scott Wakelingfe885462016-09-22 10:24:38 +01002456InstructionCodeGeneratorARMVIXL::InstructionCodeGeneratorARMVIXL(HGraph* graph,
2457 CodeGeneratorARMVIXL* codegen)
2458 : InstructionCodeGenerator(graph, codegen),
2459 assembler_(codegen->GetAssembler()),
2460 codegen_(codegen) {}
2461
2462void CodeGeneratorARMVIXL::ComputeSpillMask() {
2463 core_spill_mask_ = allocated_registers_.GetCoreRegisters() & core_callee_save_mask_;
2464 DCHECK_NE(core_spill_mask_, 0u) << "At least the return address register must be saved";
2465 // There is no easy instruction to restore just the PC on thumb2. We spill and
2466 // restore another arbitrary register.
2467 core_spill_mask_ |= (1 << kCoreAlwaysSpillRegister.GetCode());
2468 fpu_spill_mask_ = allocated_registers_.GetFloatingPointRegisters() & fpu_callee_save_mask_;
2469 // We use vpush and vpop for saving and restoring floating point registers, which take
2470 // a SRegister and the number of registers to save/restore after that SRegister. We
2471 // therefore update the `fpu_spill_mask_` to also contain those registers not allocated,
2472 // but in the range.
2473 if (fpu_spill_mask_ != 0) {
2474 uint32_t least_significant_bit = LeastSignificantBit(fpu_spill_mask_);
2475 uint32_t most_significant_bit = MostSignificantBit(fpu_spill_mask_);
2476 for (uint32_t i = least_significant_bit + 1 ; i < most_significant_bit; ++i) {
2477 fpu_spill_mask_ |= (1 << i);
2478 }
2479 }
2480}
2481
2482void CodeGeneratorARMVIXL::GenerateFrameEntry() {
2483 bool skip_overflow_check =
2484 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kArm);
2485 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
2486 __ Bind(&frame_entry_label_);
2487
Nicolas Geoffray8d728322018-01-18 22:44:32 +00002488 if (GetCompilerOptions().CountHotnessInCompiledCode()) {
2489 UseScratchRegisterScope temps(GetVIXLAssembler());
2490 vixl32::Register temp = temps.Acquire();
2491 __ Ldrh(temp, MemOperand(kMethodRegister, ArtMethod::HotnessCountOffset().Int32Value()));
2492 __ Add(temp, temp, 1);
2493 __ Strh(temp, MemOperand(kMethodRegister, ArtMethod::HotnessCountOffset().Int32Value()));
2494 }
2495
Scott Wakelingfe885462016-09-22 10:24:38 +01002496 if (HasEmptyFrame()) {
2497 return;
2498 }
2499
Scott Wakelingfe885462016-09-22 10:24:38 +01002500 if (!skip_overflow_check) {
xueliang.zhong10049552018-01-31 17:10:36 +00002501 // Using r4 instead of IP saves 2 bytes.
Nicolas Geoffray1a4f3ca2018-01-25 14:07:15 +00002502 UseScratchRegisterScope temps(GetVIXLAssembler());
xueliang.zhong10049552018-01-31 17:10:36 +00002503 vixl32::Register temp;
2504 // TODO: Remove this check when R4 is made a callee-save register
2505 // in ART compiled code (b/72801708). Currently we need to make
2506 // sure r4 is not blocked, e.g. in special purpose
2507 // TestCodeGeneratorARMVIXL; also asserting that r4 is available
2508 // here.
2509 if (!blocked_core_registers_[R4]) {
2510 for (vixl32::Register reg : kParameterCoreRegistersVIXL) {
2511 DCHECK(!reg.Is(r4));
2512 }
2513 DCHECK(!kCoreCalleeSaves.Includes(r4));
2514 temp = r4;
2515 } else {
2516 temp = temps.Acquire();
2517 }
Vladimir Marko33bff252017-11-01 14:35:42 +00002518 __ Sub(temp, sp, Operand::From(GetStackOverflowReservedBytes(InstructionSet::kArm)));
Scott Wakelingfe885462016-09-22 10:24:38 +01002519 // The load must immediately precede RecordPcInfo.
Artem Serov0fb37192016-12-06 18:13:40 +00002520 ExactAssemblyScope aas(GetVIXLAssembler(),
2521 vixl32::kMaxInstructionSizeInBytes,
2522 CodeBufferCheckScope::kMaximumSize);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002523 __ ldr(temp, MemOperand(temp));
2524 RecordPcInfo(nullptr, 0);
Scott Wakelingfe885462016-09-22 10:24:38 +01002525 }
2526
2527 __ Push(RegisterList(core_spill_mask_));
2528 GetAssembler()->cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(core_spill_mask_));
2529 GetAssembler()->cfi().RelOffsetForMany(DWARFReg(kMethodRegister),
2530 0,
2531 core_spill_mask_,
2532 kArmWordSize);
2533 if (fpu_spill_mask_ != 0) {
2534 uint32_t first = LeastSignificantBit(fpu_spill_mask_);
2535
2536 // Check that list is contiguous.
2537 DCHECK_EQ(fpu_spill_mask_ >> CTZ(fpu_spill_mask_), ~0u >> (32 - POPCOUNT(fpu_spill_mask_)));
2538
2539 __ Vpush(SRegisterList(vixl32::SRegister(first), POPCOUNT(fpu_spill_mask_)));
2540 GetAssembler()->cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(fpu_spill_mask_));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002541 GetAssembler()->cfi().RelOffsetForMany(DWARFReg(s0), 0, fpu_spill_mask_, kArmWordSize);
Scott Wakelingfe885462016-09-22 10:24:38 +01002542 }
Scott Wakelingbffdc702016-12-07 17:46:03 +00002543
Scott Wakelingfe885462016-09-22 10:24:38 +01002544 int adjust = GetFrameSize() - FrameEntrySpillSize();
2545 __ Sub(sp, sp, adjust);
2546 GetAssembler()->cfi().AdjustCFAOffset(adjust);
Scott Wakelingbffdc702016-12-07 17:46:03 +00002547
2548 // Save the current method if we need it. Note that we do not
2549 // do this in HCurrentMethod, as the instruction might have been removed
2550 // in the SSA graph.
2551 if (RequiresCurrentMethod()) {
2552 GetAssembler()->StoreToOffset(kStoreWord, kMethodRegister, sp, 0);
2553 }
Nicolas Geoffrayf7893532017-06-15 12:34:36 +01002554
2555 if (GetGraph()->HasShouldDeoptimizeFlag()) {
2556 UseScratchRegisterScope temps(GetVIXLAssembler());
2557 vixl32::Register temp = temps.Acquire();
2558 // Initialize should_deoptimize flag to 0.
2559 __ Mov(temp, 0);
2560 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, GetStackOffsetOfShouldDeoptimizeFlag());
2561 }
Roland Levillain5daa4952017-07-03 17:23:56 +01002562
2563 MaybeGenerateMarkingRegisterCheck(/* code */ 1);
Scott Wakelingfe885462016-09-22 10:24:38 +01002564}
2565
2566void CodeGeneratorARMVIXL::GenerateFrameExit() {
2567 if (HasEmptyFrame()) {
2568 __ Bx(lr);
2569 return;
2570 }
2571 GetAssembler()->cfi().RememberState();
2572 int adjust = GetFrameSize() - FrameEntrySpillSize();
2573 __ Add(sp, sp, adjust);
2574 GetAssembler()->cfi().AdjustCFAOffset(-adjust);
2575 if (fpu_spill_mask_ != 0) {
2576 uint32_t first = LeastSignificantBit(fpu_spill_mask_);
2577
2578 // Check that list is contiguous.
2579 DCHECK_EQ(fpu_spill_mask_ >> CTZ(fpu_spill_mask_), ~0u >> (32 - POPCOUNT(fpu_spill_mask_)));
2580
2581 __ Vpop(SRegisterList(vixl32::SRegister(first), POPCOUNT(fpu_spill_mask_)));
2582 GetAssembler()->cfi().AdjustCFAOffset(
2583 -static_cast<int>(kArmWordSize) * POPCOUNT(fpu_spill_mask_));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002584 GetAssembler()->cfi().RestoreMany(DWARFReg(vixl32::SRegister(0)), fpu_spill_mask_);
Scott Wakelingfe885462016-09-22 10:24:38 +01002585 }
2586 // Pop LR into PC to return.
2587 DCHECK_NE(core_spill_mask_ & (1 << kLrCode), 0U);
2588 uint32_t pop_mask = (core_spill_mask_ & (~(1 << kLrCode))) | 1 << kPcCode;
2589 __ Pop(RegisterList(pop_mask));
2590 GetAssembler()->cfi().RestoreState();
2591 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
2592}
2593
2594void CodeGeneratorARMVIXL::Bind(HBasicBlock* block) {
2595 __ Bind(GetLabelOf(block));
2596}
2597
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002598Location InvokeDexCallingConventionVisitorARMVIXL::GetNextLocation(DataType::Type type) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002599 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002600 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002601 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002602 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002603 case DataType::Type::kInt8:
2604 case DataType::Type::kUint16:
2605 case DataType::Type::kInt16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002606 case DataType::Type::kInt32: {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002607 uint32_t index = gp_index_++;
2608 uint32_t stack_index = stack_index_++;
2609 if (index < calling_convention.GetNumberOfRegisters()) {
2610 return LocationFrom(calling_convention.GetRegisterAt(index));
2611 } else {
2612 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
2613 }
2614 }
2615
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002616 case DataType::Type::kInt64: {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002617 uint32_t index = gp_index_;
2618 uint32_t stack_index = stack_index_;
2619 gp_index_ += 2;
2620 stack_index_ += 2;
2621 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
2622 if (calling_convention.GetRegisterAt(index).Is(r1)) {
2623 // Skip R1, and use R2_R3 instead.
2624 gp_index_++;
2625 index++;
2626 }
2627 }
2628 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
2629 DCHECK_EQ(calling_convention.GetRegisterAt(index).GetCode() + 1,
2630 calling_convention.GetRegisterAt(index + 1).GetCode());
2631
2632 return LocationFrom(calling_convention.GetRegisterAt(index),
2633 calling_convention.GetRegisterAt(index + 1));
2634 } else {
2635 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
2636 }
2637 }
2638
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002639 case DataType::Type::kFloat32: {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002640 uint32_t stack_index = stack_index_++;
2641 if (float_index_ % 2 == 0) {
2642 float_index_ = std::max(double_index_, float_index_);
2643 }
2644 if (float_index_ < calling_convention.GetNumberOfFpuRegisters()) {
2645 return LocationFrom(calling_convention.GetFpuRegisterAt(float_index_++));
2646 } else {
2647 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
2648 }
2649 }
2650
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002651 case DataType::Type::kFloat64: {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002652 double_index_ = std::max(double_index_, RoundUp(float_index_, 2));
2653 uint32_t stack_index = stack_index_;
2654 stack_index_ += 2;
2655 if (double_index_ + 1 < calling_convention.GetNumberOfFpuRegisters()) {
2656 uint32_t index = double_index_;
2657 double_index_ += 2;
2658 Location result = LocationFrom(
2659 calling_convention.GetFpuRegisterAt(index),
2660 calling_convention.GetFpuRegisterAt(index + 1));
2661 DCHECK(ExpectedPairLayout(result));
2662 return result;
2663 } else {
2664 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
2665 }
2666 }
2667
Aart Bik66c158e2018-01-31 12:55:04 -08002668 case DataType::Type::kUint32:
2669 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002670 case DataType::Type::kVoid:
Artem Serovd4cc5b22016-11-04 11:19:09 +00002671 LOG(FATAL) << "Unexpected parameter type " << type;
2672 break;
2673 }
2674 return Location::NoLocation();
2675}
2676
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002677Location InvokeDexCallingConventionVisitorARMVIXL::GetReturnLocation(DataType::Type type) const {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002678 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002679 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002680 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002681 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002682 case DataType::Type::kInt8:
2683 case DataType::Type::kUint16:
2684 case DataType::Type::kInt16:
Aart Bik66c158e2018-01-31 12:55:04 -08002685 case DataType::Type::kUint32:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002686 case DataType::Type::kInt32: {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002687 return LocationFrom(r0);
2688 }
2689
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002690 case DataType::Type::kFloat32: {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002691 return LocationFrom(s0);
2692 }
2693
Aart Bik66c158e2018-01-31 12:55:04 -08002694 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002695 case DataType::Type::kInt64: {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002696 return LocationFrom(r0, r1);
2697 }
2698
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002699 case DataType::Type::kFloat64: {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002700 return LocationFrom(s0, s1);
2701 }
2702
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002703 case DataType::Type::kVoid:
Artem Serovd4cc5b22016-11-04 11:19:09 +00002704 return Location::NoLocation();
2705 }
2706
2707 UNREACHABLE();
2708}
2709
2710Location InvokeDexCallingConventionVisitorARMVIXL::GetMethodLocation() const {
2711 return LocationFrom(kMethodRegister);
2712}
2713
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002714void CodeGeneratorARMVIXL::Move32(Location destination, Location source) {
2715 if (source.Equals(destination)) {
2716 return;
2717 }
2718 if (destination.IsRegister()) {
2719 if (source.IsRegister()) {
2720 __ Mov(RegisterFrom(destination), RegisterFrom(source));
2721 } else if (source.IsFpuRegister()) {
2722 __ Vmov(RegisterFrom(destination), SRegisterFrom(source));
2723 } else {
2724 GetAssembler()->LoadFromOffset(kLoadWord,
2725 RegisterFrom(destination),
2726 sp,
2727 source.GetStackIndex());
2728 }
2729 } else if (destination.IsFpuRegister()) {
2730 if (source.IsRegister()) {
2731 __ Vmov(SRegisterFrom(destination), RegisterFrom(source));
2732 } else if (source.IsFpuRegister()) {
2733 __ Vmov(SRegisterFrom(destination), SRegisterFrom(source));
2734 } else {
2735 GetAssembler()->LoadSFromOffset(SRegisterFrom(destination), sp, source.GetStackIndex());
2736 }
2737 } else {
2738 DCHECK(destination.IsStackSlot()) << destination;
2739 if (source.IsRegister()) {
2740 GetAssembler()->StoreToOffset(kStoreWord,
2741 RegisterFrom(source),
2742 sp,
2743 destination.GetStackIndex());
2744 } else if (source.IsFpuRegister()) {
2745 GetAssembler()->StoreSToOffset(SRegisterFrom(source), sp, destination.GetStackIndex());
2746 } else {
2747 DCHECK(source.IsStackSlot()) << source;
2748 UseScratchRegisterScope temps(GetVIXLAssembler());
2749 vixl32::Register temp = temps.Acquire();
2750 GetAssembler()->LoadFromOffset(kLoadWord, temp, sp, source.GetStackIndex());
2751 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
2752 }
2753 }
2754}
2755
Artem Serovcfbe9132016-10-14 15:58:56 +01002756void CodeGeneratorARMVIXL::MoveConstant(Location location, int32_t value) {
2757 DCHECK(location.IsRegister());
2758 __ Mov(RegisterFrom(location), value);
Scott Wakelingfe885462016-09-22 10:24:38 +01002759}
2760
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002761void CodeGeneratorARMVIXL::MoveLocation(Location dst, Location src, DataType::Type dst_type) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002762 // TODO(VIXL): Maybe refactor to have the 'move' implementation here and use it in
2763 // `ParallelMoveResolverARMVIXL::EmitMove`, as is done in the `arm64` backend.
Vladimir Markoca6fff82017-10-03 14:49:14 +01002764 HParallelMove move(GetGraph()->GetAllocator());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002765 move.AddMove(src, dst, dst_type, nullptr);
2766 GetMoveResolver()->EmitNativeCode(&move);
Scott Wakelingfe885462016-09-22 10:24:38 +01002767}
2768
Artem Serovcfbe9132016-10-14 15:58:56 +01002769void CodeGeneratorARMVIXL::AddLocationAsTemp(Location location, LocationSummary* locations) {
2770 if (location.IsRegister()) {
2771 locations->AddTemp(location);
2772 } else if (location.IsRegisterPair()) {
2773 locations->AddTemp(LocationFrom(LowRegisterFrom(location)));
2774 locations->AddTemp(LocationFrom(HighRegisterFrom(location)));
2775 } else {
2776 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
2777 }
Scott Wakelingfe885462016-09-22 10:24:38 +01002778}
2779
2780void CodeGeneratorARMVIXL::InvokeRuntime(QuickEntrypointEnum entrypoint,
2781 HInstruction* instruction,
2782 uint32_t dex_pc,
2783 SlowPathCode* slow_path) {
2784 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Alexandre Rames374ddf32016-11-04 10:40:49 +00002785 __ Ldr(lr, MemOperand(tr, GetThreadOffset<kArmPointerSize>(entrypoint).Int32Value()));
2786 // Ensure the pc position is recorded immediately after the `blx` instruction.
2787 // blx in T32 has only 16bit encoding that's why a stricter check for the scope is used.
Artem Serov0fb37192016-12-06 18:13:40 +00002788 ExactAssemblyScope aas(GetVIXLAssembler(),
2789 vixl32::k16BitT32InstructionSizeInBytes,
2790 CodeBufferCheckScope::kExactSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00002791 __ blx(lr);
Scott Wakelingfe885462016-09-22 10:24:38 +01002792 if (EntrypointRequiresStackMap(entrypoint)) {
2793 RecordPcInfo(instruction, dex_pc, slow_path);
2794 }
2795}
2796
2797void CodeGeneratorARMVIXL::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
2798 HInstruction* instruction,
2799 SlowPathCode* slow_path) {
2800 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Alexandre Rames374ddf32016-11-04 10:40:49 +00002801 __ Ldr(lr, MemOperand(tr, entry_point_offset));
Scott Wakelingfe885462016-09-22 10:24:38 +01002802 __ Blx(lr);
2803}
2804
Scott Wakelingfe885462016-09-22 10:24:38 +01002805void InstructionCodeGeneratorARMVIXL::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Aart Bika8b8e9b2018-01-09 11:01:02 -08002806 if (successor->IsExitBlock()) {
2807 DCHECK(got->GetPrevious()->AlwaysThrows());
2808 return; // no code needed
2809 }
2810
Scott Wakelingfe885462016-09-22 10:24:38 +01002811 HBasicBlock* block = got->GetBlock();
2812 HInstruction* previous = got->GetPrevious();
2813 HLoopInformation* info = block->GetLoopInformation();
2814
2815 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray8d728322018-01-18 22:44:32 +00002816 if (codegen_->GetCompilerOptions().CountHotnessInCompiledCode()) {
2817 UseScratchRegisterScope temps(GetVIXLAssembler());
2818 vixl32::Register temp = temps.Acquire();
2819 __ Push(vixl32::Register(kMethodRegister));
2820 GetAssembler()->LoadFromOffset(kLoadWord, kMethodRegister, sp, kArmWordSize);
2821 __ Ldrh(temp, MemOperand(kMethodRegister, ArtMethod::HotnessCountOffset().Int32Value()));
2822 __ Add(temp, temp, 1);
2823 __ Strh(temp, MemOperand(kMethodRegister, ArtMethod::HotnessCountOffset().Int32Value()));
2824 __ Pop(vixl32::Register(kMethodRegister));
2825 }
Scott Wakelingfe885462016-09-22 10:24:38 +01002826 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2827 return;
2828 }
2829 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2830 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
Roland Levillain5daa4952017-07-03 17:23:56 +01002831 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 2);
Scott Wakelingfe885462016-09-22 10:24:38 +01002832 }
2833 if (!codegen_->GoesToNextBlock(block, successor)) {
2834 __ B(codegen_->GetLabelOf(successor));
2835 }
2836}
2837
2838void LocationsBuilderARMVIXL::VisitGoto(HGoto* got) {
2839 got->SetLocations(nullptr);
2840}
2841
2842void InstructionCodeGeneratorARMVIXL::VisitGoto(HGoto* got) {
2843 HandleGoto(got, got->GetSuccessor());
2844}
2845
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002846void LocationsBuilderARMVIXL::VisitTryBoundary(HTryBoundary* try_boundary) {
2847 try_boundary->SetLocations(nullptr);
2848}
2849
2850void InstructionCodeGeneratorARMVIXL::VisitTryBoundary(HTryBoundary* try_boundary) {
2851 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2852 if (!successor->IsExitBlock()) {
2853 HandleGoto(try_boundary, successor);
2854 }
2855}
2856
Scott Wakelingfe885462016-09-22 10:24:38 +01002857void LocationsBuilderARMVIXL::VisitExit(HExit* exit) {
2858 exit->SetLocations(nullptr);
2859}
2860
2861void InstructionCodeGeneratorARMVIXL::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
2862}
2863
Scott Wakelingfe885462016-09-22 10:24:38 +01002864void InstructionCodeGeneratorARMVIXL::GenerateCompareTestAndBranch(HCondition* condition,
Anton Kirilov23b752b2017-07-20 14:40:44 +01002865 vixl32::Label* true_target,
2866 vixl32::Label* false_target,
Anton Kirilovfd522532017-05-10 12:46:57 +01002867 bool is_far_target) {
Anton Kirilov23b752b2017-07-20 14:40:44 +01002868 if (true_target == false_target) {
2869 DCHECK(true_target != nullptr);
2870 __ B(true_target);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002871 return;
2872 }
2873
Anton Kirilov23b752b2017-07-20 14:40:44 +01002874 vixl32::Label* non_fallthrough_target;
2875 bool invert;
2876 bool emit_both_branches;
Scott Wakelingfe885462016-09-22 10:24:38 +01002877
Anton Kirilov23b752b2017-07-20 14:40:44 +01002878 if (true_target == nullptr) {
2879 // The true target is fallthrough.
2880 DCHECK(false_target != nullptr);
2881 non_fallthrough_target = false_target;
2882 invert = true;
2883 emit_both_branches = false;
2884 } else {
2885 non_fallthrough_target = true_target;
2886 invert = false;
2887 // Either the false target is fallthrough, or there is no fallthrough
2888 // and both branches must be emitted.
2889 emit_both_branches = (false_target != nullptr);
Scott Wakelingfe885462016-09-22 10:24:38 +01002890 }
2891
Anton Kirilov23b752b2017-07-20 14:40:44 +01002892 const auto cond = GenerateTest(condition, invert, codegen_);
2893
2894 __ B(cond.first, non_fallthrough_target, is_far_target);
2895
2896 if (emit_both_branches) {
2897 // No target falls through, we need to branch.
2898 __ B(false_target);
Scott Wakelingfe885462016-09-22 10:24:38 +01002899 }
2900}
2901
2902void InstructionCodeGeneratorARMVIXL::GenerateTestAndBranch(HInstruction* instruction,
2903 size_t condition_input_index,
2904 vixl32::Label* true_target,
xueliang.zhongf51bc622016-11-04 09:23:32 +00002905 vixl32::Label* false_target,
2906 bool far_target) {
Scott Wakelingfe885462016-09-22 10:24:38 +01002907 HInstruction* cond = instruction->InputAt(condition_input_index);
2908
2909 if (true_target == nullptr && false_target == nullptr) {
2910 // Nothing to do. The code always falls through.
2911 return;
2912 } else if (cond->IsIntConstant()) {
2913 // Constant condition, statically compared against "true" (integer value 1).
2914 if (cond->AsIntConstant()->IsTrue()) {
2915 if (true_target != nullptr) {
2916 __ B(true_target);
2917 }
2918 } else {
Anton Kirilov644032c2016-12-06 17:51:43 +00002919 DCHECK(cond->AsIntConstant()->IsFalse()) << Int32ConstantFrom(cond);
Scott Wakelingfe885462016-09-22 10:24:38 +01002920 if (false_target != nullptr) {
2921 __ B(false_target);
2922 }
2923 }
2924 return;
2925 }
2926
2927 // The following code generates these patterns:
2928 // (1) true_target == nullptr && false_target != nullptr
2929 // - opposite condition true => branch to false_target
2930 // (2) true_target != nullptr && false_target == nullptr
2931 // - condition true => branch to true_target
2932 // (3) true_target != nullptr && false_target != nullptr
2933 // - condition true => branch to true_target
2934 // - branch to false_target
2935 if (IsBooleanValueOrMaterializedCondition(cond)) {
2936 // Condition has been materialized, compare the output to 0.
2937 if (kIsDebugBuild) {
2938 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
2939 DCHECK(cond_val.IsRegister());
2940 }
2941 if (true_target == nullptr) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00002942 __ CompareAndBranchIfZero(InputRegisterAt(instruction, condition_input_index),
2943 false_target,
2944 far_target);
Scott Wakelingfe885462016-09-22 10:24:38 +01002945 } else {
xueliang.zhongf51bc622016-11-04 09:23:32 +00002946 __ CompareAndBranchIfNonZero(InputRegisterAt(instruction, condition_input_index),
2947 true_target,
2948 far_target);
Scott Wakelingfe885462016-09-22 10:24:38 +01002949 }
2950 } else {
2951 // Condition has not been materialized. Use its inputs as the comparison and
2952 // its condition as the branch condition.
2953 HCondition* condition = cond->AsCondition();
2954
2955 // If this is a long or FP comparison that has been folded into
2956 // the HCondition, generate the comparison directly.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002957 DataType::Type type = condition->InputAt(0)->GetType();
2958 if (type == DataType::Type::kInt64 || DataType::IsFloatingPointType(type)) {
Anton Kirilovfd522532017-05-10 12:46:57 +01002959 GenerateCompareTestAndBranch(condition, true_target, false_target, far_target);
Scott Wakelingfe885462016-09-22 10:24:38 +01002960 return;
2961 }
2962
Donghui Bai426b49c2016-11-08 14:55:38 +08002963 vixl32::Label* non_fallthrough_target;
2964 vixl32::Condition arm_cond = vixl32::Condition::None();
2965 const vixl32::Register left = InputRegisterAt(cond, 0);
2966 const Operand right = InputOperandAt(cond, 1);
2967
Scott Wakelingfe885462016-09-22 10:24:38 +01002968 if (true_target == nullptr) {
Donghui Bai426b49c2016-11-08 14:55:38 +08002969 arm_cond = ARMCondition(condition->GetOppositeCondition());
2970 non_fallthrough_target = false_target;
Scott Wakelingfe885462016-09-22 10:24:38 +01002971 } else {
Donghui Bai426b49c2016-11-08 14:55:38 +08002972 arm_cond = ARMCondition(condition->GetCondition());
2973 non_fallthrough_target = true_target;
2974 }
2975
2976 if (right.IsImmediate() && right.GetImmediate() == 0 && (arm_cond.Is(ne) || arm_cond.Is(eq))) {
2977 if (arm_cond.Is(eq)) {
Anton Kirilovfd522532017-05-10 12:46:57 +01002978 __ CompareAndBranchIfZero(left, non_fallthrough_target, far_target);
Donghui Bai426b49c2016-11-08 14:55:38 +08002979 } else {
2980 DCHECK(arm_cond.Is(ne));
Anton Kirilovfd522532017-05-10 12:46:57 +01002981 __ CompareAndBranchIfNonZero(left, non_fallthrough_target, far_target);
Donghui Bai426b49c2016-11-08 14:55:38 +08002982 }
2983 } else {
2984 __ Cmp(left, right);
Anton Kirilovfd522532017-05-10 12:46:57 +01002985 __ B(arm_cond, non_fallthrough_target, far_target);
Scott Wakelingfe885462016-09-22 10:24:38 +01002986 }
2987 }
2988
2989 // If neither branch falls through (case 3), the conditional branch to `true_target`
2990 // was already emitted (case 2) and we need to emit a jump to `false_target`.
2991 if (true_target != nullptr && false_target != nullptr) {
2992 __ B(false_target);
2993 }
2994}
2995
2996void LocationsBuilderARMVIXL::VisitIf(HIf* if_instr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002997 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(if_instr);
Scott Wakelingfe885462016-09-22 10:24:38 +01002998 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
2999 locations->SetInAt(0, Location::RequiresRegister());
3000 }
3001}
3002
3003void InstructionCodeGeneratorARMVIXL::VisitIf(HIf* if_instr) {
3004 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
3005 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003006 vixl32::Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
3007 nullptr : codegen_->GetLabelOf(true_successor);
3008 vixl32::Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
3009 nullptr : codegen_->GetLabelOf(false_successor);
Scott Wakelingfe885462016-09-22 10:24:38 +01003010 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
3011}
3012
Scott Wakelingc34dba72016-10-03 10:14:44 +01003013void LocationsBuilderARMVIXL::VisitDeoptimize(HDeoptimize* deoptimize) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003014 LocationSummary* locations = new (GetGraph()->GetAllocator())
Scott Wakelingc34dba72016-10-03 10:14:44 +01003015 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003016 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3017 RegisterSet caller_saves = RegisterSet::Empty();
3018 caller_saves.Add(LocationFrom(calling_convention.GetRegisterAt(0)));
3019 locations->SetCustomSlowPathCallerSaves(caller_saves);
Scott Wakelingc34dba72016-10-03 10:14:44 +01003020 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
3021 locations->SetInAt(0, Location::RequiresRegister());
3022 }
3023}
3024
3025void InstructionCodeGeneratorARMVIXL::VisitDeoptimize(HDeoptimize* deoptimize) {
3026 SlowPathCodeARMVIXL* slow_path =
3027 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARMVIXL>(deoptimize);
3028 GenerateTestAndBranch(deoptimize,
3029 /* condition_input_index */ 0,
3030 slow_path->GetEntryLabel(),
3031 /* false_target */ nullptr);
3032}
3033
Artem Serovd4cc5b22016-11-04 11:19:09 +00003034void LocationsBuilderARMVIXL::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003035 LocationSummary* locations = new (GetGraph()->GetAllocator())
Artem Serovd4cc5b22016-11-04 11:19:09 +00003036 LocationSummary(flag, LocationSummary::kNoCall);
3037 locations->SetOut(Location::RequiresRegister());
3038}
3039
3040void InstructionCodeGeneratorARMVIXL::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
3041 GetAssembler()->LoadFromOffset(kLoadWord,
3042 OutputRegister(flag),
3043 sp,
3044 codegen_->GetStackOffsetOfShouldDeoptimizeFlag());
3045}
3046
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003047void LocationsBuilderARMVIXL::VisitSelect(HSelect* select) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003048 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(select);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003049 const bool is_floating_point = DataType::IsFloatingPointType(select->GetType());
Donghui Bai426b49c2016-11-08 14:55:38 +08003050
3051 if (is_floating_point) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003052 locations->SetInAt(0, Location::RequiresFpuRegister());
Donghui Bai426b49c2016-11-08 14:55:38 +08003053 locations->SetInAt(1, Location::FpuRegisterOrConstant(select->GetTrueValue()));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003054 } else {
3055 locations->SetInAt(0, Location::RequiresRegister());
Donghui Bai426b49c2016-11-08 14:55:38 +08003056 locations->SetInAt(1, Arm8BitEncodableConstantOrRegister(select->GetTrueValue()));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003057 }
Donghui Bai426b49c2016-11-08 14:55:38 +08003058
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003059 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
Donghui Bai426b49c2016-11-08 14:55:38 +08003060 locations->SetInAt(2, Location::RegisterOrConstant(select->GetCondition()));
3061 // The code generator handles overlap with the values, but not with the condition.
3062 locations->SetOut(Location::SameAsFirstInput());
3063 } else if (is_floating_point) {
3064 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3065 } else {
3066 if (!locations->InAt(1).IsConstant()) {
3067 locations->SetInAt(0, Arm8BitEncodableConstantOrRegister(select->GetFalseValue()));
3068 }
3069
3070 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003071 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003072}
3073
3074void InstructionCodeGeneratorARMVIXL::VisitSelect(HSelect* select) {
Donghui Bai426b49c2016-11-08 14:55:38 +08003075 HInstruction* const condition = select->GetCondition();
3076 const LocationSummary* const locations = select->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003077 const DataType::Type type = select->GetType();
Donghui Bai426b49c2016-11-08 14:55:38 +08003078 const Location first = locations->InAt(0);
3079 const Location out = locations->Out();
3080 const Location second = locations->InAt(1);
3081 Location src;
3082
3083 if (condition->IsIntConstant()) {
3084 if (condition->AsIntConstant()->IsFalse()) {
3085 src = first;
3086 } else {
3087 src = second;
3088 }
3089
3090 codegen_->MoveLocation(out, src, type);
3091 return;
3092 }
3093
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003094 if (!DataType::IsFloatingPointType(type)) {
Donghui Bai426b49c2016-11-08 14:55:38 +08003095 bool invert = false;
3096
3097 if (out.Equals(second)) {
3098 src = first;
3099 invert = true;
3100 } else if (out.Equals(first)) {
3101 src = second;
3102 } else if (second.IsConstant()) {
3103 DCHECK(CanEncodeConstantAs8BitImmediate(second.GetConstant()));
3104 src = second;
3105 } else if (first.IsConstant()) {
3106 DCHECK(CanEncodeConstantAs8BitImmediate(first.GetConstant()));
3107 src = first;
3108 invert = true;
3109 } else {
3110 src = second;
3111 }
3112
3113 if (CanGenerateConditionalMove(out, src)) {
3114 if (!out.Equals(first) && !out.Equals(second)) {
3115 codegen_->MoveLocation(out, src.Equals(first) ? second : first, type);
3116 }
3117
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003118 std::pair<vixl32::Condition, vixl32::Condition> cond(eq, ne);
3119
3120 if (IsBooleanValueOrMaterializedCondition(condition)) {
3121 __ Cmp(InputRegisterAt(select, 2), 0);
3122 cond = invert ? std::make_pair(eq, ne) : std::make_pair(ne, eq);
3123 } else {
3124 cond = GenerateTest(condition->AsCondition(), invert, codegen_);
3125 }
3126
Donghui Bai426b49c2016-11-08 14:55:38 +08003127 const size_t instr_count = out.IsRegisterPair() ? 4 : 2;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003128 // We use the scope because of the IT block that follows.
Donghui Bai426b49c2016-11-08 14:55:38 +08003129 ExactAssemblyScope guard(GetVIXLAssembler(),
3130 instr_count * vixl32::k16BitT32InstructionSizeInBytes,
3131 CodeBufferCheckScope::kExactSize);
3132
3133 if (out.IsRegister()) {
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003134 __ it(cond.first);
3135 __ mov(cond.first, RegisterFrom(out), OperandFrom(src, type));
Donghui Bai426b49c2016-11-08 14:55:38 +08003136 } else {
3137 DCHECK(out.IsRegisterPair());
3138
3139 Operand operand_high(0);
3140 Operand operand_low(0);
3141
3142 if (src.IsConstant()) {
3143 const int64_t value = Int64ConstantFrom(src);
3144
3145 operand_high = High32Bits(value);
3146 operand_low = Low32Bits(value);
3147 } else {
3148 DCHECK(src.IsRegisterPair());
3149 operand_high = HighRegisterFrom(src);
3150 operand_low = LowRegisterFrom(src);
3151 }
3152
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003153 __ it(cond.first);
3154 __ mov(cond.first, LowRegisterFrom(out), operand_low);
3155 __ it(cond.first);
3156 __ mov(cond.first, HighRegisterFrom(out), operand_high);
Donghui Bai426b49c2016-11-08 14:55:38 +08003157 }
3158
3159 return;
3160 }
3161 }
3162
3163 vixl32::Label* false_target = nullptr;
3164 vixl32::Label* true_target = nullptr;
3165 vixl32::Label select_end;
3166 vixl32::Label* const target = codegen_->GetFinalLabel(select, &select_end);
3167
3168 if (out.Equals(second)) {
3169 true_target = target;
3170 src = first;
3171 } else {
3172 false_target = target;
3173 src = second;
3174
3175 if (!out.Equals(first)) {
3176 codegen_->MoveLocation(out, first, type);
3177 }
3178 }
3179
3180 GenerateTestAndBranch(select, 2, true_target, false_target, /* far_target */ false);
3181 codegen_->MoveLocation(out, src, type);
3182
3183 if (select_end.IsReferenced()) {
3184 __ Bind(&select_end);
3185 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003186}
3187
Artem Serov551b28f2016-10-18 19:11:30 +01003188void LocationsBuilderARMVIXL::VisitNativeDebugInfo(HNativeDebugInfo* info) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003189 new (GetGraph()->GetAllocator()) LocationSummary(info);
Artem Serov551b28f2016-10-18 19:11:30 +01003190}
3191
3192void InstructionCodeGeneratorARMVIXL::VisitNativeDebugInfo(HNativeDebugInfo*) {
3193 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
3194}
3195
Scott Wakelingfe885462016-09-22 10:24:38 +01003196void CodeGeneratorARMVIXL::GenerateNop() {
3197 __ Nop();
3198}
3199
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003200// `temp` is an extra temporary register that is used for some conditions;
3201// callers may not specify it, in which case the method will use a scratch
3202// register instead.
3203void CodeGeneratorARMVIXL::GenerateConditionWithZero(IfCondition condition,
3204 vixl32::Register out,
3205 vixl32::Register in,
3206 vixl32::Register temp) {
3207 switch (condition) {
3208 case kCondEQ:
3209 // x <= 0 iff x == 0 when the comparison is unsigned.
3210 case kCondBE:
3211 if (!temp.IsValid() || (out.IsLow() && !out.Is(in))) {
3212 temp = out;
3213 }
3214
3215 // Avoid 32-bit instructions if possible; note that `in` and `temp` must be
3216 // different as well.
3217 if (in.IsLow() && temp.IsLow() && !in.Is(temp)) {
3218 // temp = - in; only 0 sets the carry flag.
3219 __ Rsbs(temp, in, 0);
3220
3221 if (out.Is(in)) {
3222 std::swap(in, temp);
3223 }
3224
3225 // out = - in + in + carry = carry
3226 __ Adc(out, temp, in);
3227 } else {
3228 // If `in` is 0, then it has 32 leading zeros, and less than that otherwise.
3229 __ Clz(out, in);
3230 // Any number less than 32 logically shifted right by 5 bits results in 0;
3231 // the same operation on 32 yields 1.
3232 __ Lsr(out, out, 5);
3233 }
3234
3235 break;
3236 case kCondNE:
3237 // x > 0 iff x != 0 when the comparison is unsigned.
3238 case kCondA: {
3239 UseScratchRegisterScope temps(GetVIXLAssembler());
3240
3241 if (out.Is(in)) {
3242 if (!temp.IsValid() || in.Is(temp)) {
3243 temp = temps.Acquire();
3244 }
3245 } else if (!temp.IsValid() || !temp.IsLow()) {
3246 temp = out;
3247 }
3248
3249 // temp = in - 1; only 0 does not set the carry flag.
3250 __ Subs(temp, in, 1);
3251 // out = in + ~temp + carry = in + (-(in - 1) - 1) + carry = in - in + 1 - 1 + carry = carry
3252 __ Sbc(out, in, temp);
3253 break;
3254 }
3255 case kCondGE:
3256 __ Mvn(out, in);
3257 in = out;
3258 FALLTHROUGH_INTENDED;
3259 case kCondLT:
3260 // We only care about the sign bit.
3261 __ Lsr(out, in, 31);
3262 break;
3263 case kCondAE:
3264 // Trivially true.
3265 __ Mov(out, 1);
3266 break;
3267 case kCondB:
3268 // Trivially false.
3269 __ Mov(out, 0);
3270 break;
3271 default:
3272 LOG(FATAL) << "Unexpected condition " << condition;
3273 UNREACHABLE();
3274 }
3275}
3276
Scott Wakelingfe885462016-09-22 10:24:38 +01003277void LocationsBuilderARMVIXL::HandleCondition(HCondition* cond) {
3278 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003279 new (GetGraph()->GetAllocator()) LocationSummary(cond, LocationSummary::kNoCall);
Scott Wakelingfe885462016-09-22 10:24:38 +01003280 // Handle the long/FP comparisons made in instruction simplification.
3281 switch (cond->InputAt(0)->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003282 case DataType::Type::kInt64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003283 locations->SetInAt(0, Location::RequiresRegister());
3284 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
3285 if (!cond->IsEmittedAtUseSite()) {
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003286 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Scott Wakelingfe885462016-09-22 10:24:38 +01003287 }
3288 break;
3289
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003290 case DataType::Type::kFloat32:
3291 case DataType::Type::kFloat64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003292 locations->SetInAt(0, Location::RequiresFpuRegister());
Artem Serov657022c2016-11-23 14:19:38 +00003293 locations->SetInAt(1, ArithmeticZeroOrFpuRegister(cond->InputAt(1)));
Scott Wakelingfe885462016-09-22 10:24:38 +01003294 if (!cond->IsEmittedAtUseSite()) {
3295 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3296 }
3297 break;
3298
3299 default:
3300 locations->SetInAt(0, Location::RequiresRegister());
3301 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
3302 if (!cond->IsEmittedAtUseSite()) {
3303 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3304 }
3305 }
3306}
3307
3308void InstructionCodeGeneratorARMVIXL::HandleCondition(HCondition* cond) {
3309 if (cond->IsEmittedAtUseSite()) {
3310 return;
3311 }
3312
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003313 const DataType::Type type = cond->GetLeft()->GetType();
Scott Wakelingfe885462016-09-22 10:24:38 +01003314
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003315 if (DataType::IsFloatingPointType(type)) {
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003316 GenerateConditionGeneric(cond, codegen_);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003317 return;
Scott Wakelingfe885462016-09-22 10:24:38 +01003318 }
3319
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003320 DCHECK(DataType::IsIntegralType(type) || type == DataType::Type::kReference) << type;
Scott Wakelingfe885462016-09-22 10:24:38 +01003321
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003322 const IfCondition condition = cond->GetCondition();
Scott Wakelingfe885462016-09-22 10:24:38 +01003323
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003324 // A condition with only one boolean input, or two boolean inputs without being equality or
3325 // inequality results from transformations done by the instruction simplifier, and is handled
3326 // as a regular condition with integral inputs.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003327 if (type == DataType::Type::kBool &&
3328 cond->GetRight()->GetType() == DataType::Type::kBool &&
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003329 (condition == kCondEQ || condition == kCondNE)) {
3330 vixl32::Register left = InputRegisterAt(cond, 0);
3331 const vixl32::Register out = OutputRegister(cond);
3332 const Location right_loc = cond->GetLocations()->InAt(1);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003333
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003334 // The constant case is handled by the instruction simplifier.
3335 DCHECK(!right_loc.IsConstant());
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003336
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003337 vixl32::Register right = RegisterFrom(right_loc);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003338
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003339 // Avoid 32-bit instructions if possible.
3340 if (out.Is(right)) {
3341 std::swap(left, right);
3342 }
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003343
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003344 __ Eor(out, left, right);
3345
3346 if (condition == kCondEQ) {
3347 __ Eor(out, out, 1);
3348 }
3349
3350 return;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003351 }
Anton Kirilov6f644202017-02-27 18:29:45 +00003352
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003353 GenerateConditionIntegralOrNonPrimitive(cond, codegen_);
Scott Wakelingfe885462016-09-22 10:24:38 +01003354}
3355
3356void LocationsBuilderARMVIXL::VisitEqual(HEqual* comp) {
3357 HandleCondition(comp);
3358}
3359
3360void InstructionCodeGeneratorARMVIXL::VisitEqual(HEqual* comp) {
3361 HandleCondition(comp);
3362}
3363
3364void LocationsBuilderARMVIXL::VisitNotEqual(HNotEqual* comp) {
3365 HandleCondition(comp);
3366}
3367
3368void InstructionCodeGeneratorARMVIXL::VisitNotEqual(HNotEqual* comp) {
3369 HandleCondition(comp);
3370}
3371
3372void LocationsBuilderARMVIXL::VisitLessThan(HLessThan* comp) {
3373 HandleCondition(comp);
3374}
3375
3376void InstructionCodeGeneratorARMVIXL::VisitLessThan(HLessThan* comp) {
3377 HandleCondition(comp);
3378}
3379
3380void LocationsBuilderARMVIXL::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
3381 HandleCondition(comp);
3382}
3383
3384void InstructionCodeGeneratorARMVIXL::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
3385 HandleCondition(comp);
3386}
3387
3388void LocationsBuilderARMVIXL::VisitGreaterThan(HGreaterThan* comp) {
3389 HandleCondition(comp);
3390}
3391
3392void InstructionCodeGeneratorARMVIXL::VisitGreaterThan(HGreaterThan* comp) {
3393 HandleCondition(comp);
3394}
3395
3396void LocationsBuilderARMVIXL::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
3397 HandleCondition(comp);
3398}
3399
3400void InstructionCodeGeneratorARMVIXL::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
3401 HandleCondition(comp);
3402}
3403
3404void LocationsBuilderARMVIXL::VisitBelow(HBelow* comp) {
3405 HandleCondition(comp);
3406}
3407
3408void InstructionCodeGeneratorARMVIXL::VisitBelow(HBelow* comp) {
3409 HandleCondition(comp);
3410}
3411
3412void LocationsBuilderARMVIXL::VisitBelowOrEqual(HBelowOrEqual* comp) {
3413 HandleCondition(comp);
3414}
3415
3416void InstructionCodeGeneratorARMVIXL::VisitBelowOrEqual(HBelowOrEqual* comp) {
3417 HandleCondition(comp);
3418}
3419
3420void LocationsBuilderARMVIXL::VisitAbove(HAbove* comp) {
3421 HandleCondition(comp);
3422}
3423
3424void InstructionCodeGeneratorARMVIXL::VisitAbove(HAbove* comp) {
3425 HandleCondition(comp);
3426}
3427
3428void LocationsBuilderARMVIXL::VisitAboveOrEqual(HAboveOrEqual* comp) {
3429 HandleCondition(comp);
3430}
3431
3432void InstructionCodeGeneratorARMVIXL::VisitAboveOrEqual(HAboveOrEqual* comp) {
3433 HandleCondition(comp);
3434}
3435
3436void LocationsBuilderARMVIXL::VisitIntConstant(HIntConstant* constant) {
3437 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003438 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Scott Wakelingfe885462016-09-22 10:24:38 +01003439 locations->SetOut(Location::ConstantLocation(constant));
3440}
3441
3442void InstructionCodeGeneratorARMVIXL::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
3443 // Will be generated at use site.
3444}
3445
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003446void LocationsBuilderARMVIXL::VisitNullConstant(HNullConstant* constant) {
3447 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003448 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003449 locations->SetOut(Location::ConstantLocation(constant));
3450}
3451
3452void InstructionCodeGeneratorARMVIXL::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
3453 // Will be generated at use site.
3454}
3455
Scott Wakelingfe885462016-09-22 10:24:38 +01003456void LocationsBuilderARMVIXL::VisitLongConstant(HLongConstant* constant) {
3457 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003458 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Scott Wakelingfe885462016-09-22 10:24:38 +01003459 locations->SetOut(Location::ConstantLocation(constant));
3460}
3461
3462void InstructionCodeGeneratorARMVIXL::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
3463 // Will be generated at use site.
3464}
3465
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01003466void LocationsBuilderARMVIXL::VisitFloatConstant(HFloatConstant* constant) {
3467 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003468 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01003469 locations->SetOut(Location::ConstantLocation(constant));
3470}
3471
Scott Wakelingc34dba72016-10-03 10:14:44 +01003472void InstructionCodeGeneratorARMVIXL::VisitFloatConstant(
3473 HFloatConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01003474 // Will be generated at use site.
3475}
3476
3477void LocationsBuilderARMVIXL::VisitDoubleConstant(HDoubleConstant* constant) {
3478 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003479 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01003480 locations->SetOut(Location::ConstantLocation(constant));
3481}
3482
Scott Wakelingc34dba72016-10-03 10:14:44 +01003483void InstructionCodeGeneratorARMVIXL::VisitDoubleConstant(
3484 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01003485 // Will be generated at use site.
3486}
3487
Igor Murashkind01745e2017-04-05 16:40:31 -07003488void LocationsBuilderARMVIXL::VisitConstructorFence(HConstructorFence* constructor_fence) {
3489 constructor_fence->SetLocations(nullptr);
3490}
3491
3492void InstructionCodeGeneratorARMVIXL::VisitConstructorFence(
3493 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
3494 codegen_->GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
3495}
3496
Scott Wakelingfe885462016-09-22 10:24:38 +01003497void LocationsBuilderARMVIXL::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3498 memory_barrier->SetLocations(nullptr);
3499}
3500
3501void InstructionCodeGeneratorARMVIXL::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3502 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
3503}
3504
3505void LocationsBuilderARMVIXL::VisitReturnVoid(HReturnVoid* ret) {
3506 ret->SetLocations(nullptr);
3507}
3508
3509void InstructionCodeGeneratorARMVIXL::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
3510 codegen_->GenerateFrameExit();
3511}
3512
3513void LocationsBuilderARMVIXL::VisitReturn(HReturn* ret) {
3514 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003515 new (GetGraph()->GetAllocator()) LocationSummary(ret, LocationSummary::kNoCall);
Scott Wakelingfe885462016-09-22 10:24:38 +01003516 locations->SetInAt(0, parameter_visitor_.GetReturnLocation(ret->InputAt(0)->GetType()));
3517}
3518
3519void InstructionCodeGeneratorARMVIXL::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
3520 codegen_->GenerateFrameExit();
3521}
3522
Artem Serovcfbe9132016-10-14 15:58:56 +01003523void LocationsBuilderARMVIXL::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3524 // The trampoline uses the same calling convention as dex calling conventions,
3525 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
3526 // the method_idx.
3527 HandleInvoke(invoke);
3528}
3529
3530void InstructionCodeGeneratorARMVIXL::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3531 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
Roland Levillain5daa4952017-07-03 17:23:56 +01003532 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 3);
Artem Serovcfbe9132016-10-14 15:58:56 +01003533}
3534
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003535void LocationsBuilderARMVIXL::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
3536 // Explicit clinit checks triggered by static invokes must have been pruned by
3537 // art::PrepareForRegisterAllocation.
3538 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
3539
Anton Kirilov5ec62182016-10-13 20:16:02 +01003540 IntrinsicLocationsBuilderARMVIXL intrinsic(codegen_);
3541 if (intrinsic.TryDispatch(invoke)) {
Anton Kirilov5ec62182016-10-13 20:16:02 +01003542 return;
3543 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003544
3545 HandleInvoke(invoke);
3546}
3547
Anton Kirilov5ec62182016-10-13 20:16:02 +01003548static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARMVIXL* codegen) {
3549 if (invoke->GetLocations()->Intrinsified()) {
3550 IntrinsicCodeGeneratorARMVIXL intrinsic(codegen);
3551 intrinsic.Dispatch(invoke);
3552 return true;
3553 }
3554 return false;
3555}
3556
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003557void InstructionCodeGeneratorARMVIXL::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
3558 // Explicit clinit checks triggered by static invokes must have been pruned by
3559 // art::PrepareForRegisterAllocation.
3560 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
3561
Anton Kirilov5ec62182016-10-13 20:16:02 +01003562 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
Roland Levillain5daa4952017-07-03 17:23:56 +01003563 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 4);
Anton Kirilov5ec62182016-10-13 20:16:02 +01003564 return;
3565 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003566
3567 LocationSummary* locations = invoke->GetLocations();
Artem Serovd4cc5b22016-11-04 11:19:09 +00003568 codegen_->GenerateStaticOrDirectCall(
3569 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Roland Levillain5daa4952017-07-03 17:23:56 +01003570
3571 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 5);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003572}
3573
3574void LocationsBuilderARMVIXL::HandleInvoke(HInvoke* invoke) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00003575 InvokeDexCallingConventionVisitorARMVIXL calling_convention_visitor;
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003576 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
3577}
3578
3579void LocationsBuilderARMVIXL::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Anton Kirilov5ec62182016-10-13 20:16:02 +01003580 IntrinsicLocationsBuilderARMVIXL intrinsic(codegen_);
3581 if (intrinsic.TryDispatch(invoke)) {
3582 return;
3583 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003584
3585 HandleInvoke(invoke);
3586}
3587
3588void InstructionCodeGeneratorARMVIXL::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Anton Kirilov5ec62182016-10-13 20:16:02 +01003589 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
Roland Levillain5daa4952017-07-03 17:23:56 +01003590 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 6);
Anton Kirilov5ec62182016-10-13 20:16:02 +01003591 return;
3592 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003593
3594 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexandre Rames374ddf32016-11-04 10:40:49 +00003595 DCHECK(!codegen_->IsLeafMethod());
Roland Levillain5daa4952017-07-03 17:23:56 +01003596
3597 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 7);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003598}
3599
Artem Serovcfbe9132016-10-14 15:58:56 +01003600void LocationsBuilderARMVIXL::VisitInvokeInterface(HInvokeInterface* invoke) {
3601 HandleInvoke(invoke);
3602 // Add the hidden argument.
3603 invoke->GetLocations()->AddTemp(LocationFrom(r12));
3604}
3605
3606void InstructionCodeGeneratorARMVIXL::VisitInvokeInterface(HInvokeInterface* invoke) {
3607 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
3608 LocationSummary* locations = invoke->GetLocations();
3609 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
3610 vixl32::Register hidden_reg = RegisterFrom(locations->GetTemp(1));
3611 Location receiver = locations->InAt(0);
3612 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3613
3614 DCHECK(!receiver.IsStackSlot());
3615
Alexandre Rames374ddf32016-11-04 10:40:49 +00003616 // Ensure the pc position is recorded immediately after the `ldr` instruction.
3617 {
Artem Serov0fb37192016-12-06 18:13:40 +00003618 ExactAssemblyScope aas(GetVIXLAssembler(),
3619 vixl32::kMaxInstructionSizeInBytes,
3620 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00003621 // /* HeapReference<Class> */ temp = receiver->klass_
3622 __ ldr(temp, MemOperand(RegisterFrom(receiver), class_offset));
3623 codegen_->MaybeRecordImplicitNullCheck(invoke);
3624 }
Artem Serovcfbe9132016-10-14 15:58:56 +01003625 // Instead of simply (possibly) unpoisoning `temp` here, we should
3626 // emit a read barrier for the previous class reference load.
3627 // However this is not required in practice, as this is an
3628 // intermediate/temporary reference and because the current
3629 // concurrent copying collector keeps the from-space memory
3630 // intact/accessible until the end of the marking phase (the
3631 // concurrent copying collector may not in the future).
3632 GetAssembler()->MaybeUnpoisonHeapReference(temp);
3633 GetAssembler()->LoadFromOffset(kLoadWord,
3634 temp,
3635 temp,
3636 mirror::Class::ImtPtrOffset(kArmPointerSize).Uint32Value());
3637 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
3638 invoke->GetImtIndex(), kArmPointerSize));
3639 // temp = temp->GetImtEntryAt(method_offset);
3640 GetAssembler()->LoadFromOffset(kLoadWord, temp, temp, method_offset);
3641 uint32_t entry_point =
3642 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize).Int32Value();
3643 // LR = temp->GetEntryPoint();
3644 GetAssembler()->LoadFromOffset(kLoadWord, lr, temp, entry_point);
3645
3646 // Set the hidden (in r12) argument. It is done here, right before a BLX to prevent other
3647 // instruction from clobbering it as they might use r12 as a scratch register.
3648 DCHECK(hidden_reg.Is(r12));
Scott Wakelingb77051e2016-11-21 19:46:00 +00003649
3650 {
3651 // The VIXL macro assembler may clobber any of the scratch registers that are available to it,
3652 // so it checks if the application is using them (by passing them to the macro assembler
3653 // methods). The following application of UseScratchRegisterScope corrects VIXL's notion of
3654 // what is available, and is the opposite of the standard usage: Instead of requesting a
3655 // temporary location, it imposes an external constraint (i.e. a specific register is reserved
3656 // for the hidden argument). Note that this works even if VIXL needs a scratch register itself
3657 // (to materialize the constant), since the destination register becomes available for such use
3658 // internally for the duration of the macro instruction.
3659 UseScratchRegisterScope temps(GetVIXLAssembler());
3660 temps.Exclude(hidden_reg);
3661 __ Mov(hidden_reg, invoke->GetDexMethodIndex());
3662 }
Artem Serovcfbe9132016-10-14 15:58:56 +01003663 {
Alexandre Rames374ddf32016-11-04 10:40:49 +00003664 // Ensure the pc position is recorded immediately after the `blx` instruction.
3665 // blx in T32 has only 16bit encoding that's why a stricter check for the scope is used.
Artem Serov0fb37192016-12-06 18:13:40 +00003666 ExactAssemblyScope aas(GetVIXLAssembler(),
Alexandre Rames374ddf32016-11-04 10:40:49 +00003667 vixl32::k16BitT32InstructionSizeInBytes,
3668 CodeBufferCheckScope::kExactSize);
Artem Serovcfbe9132016-10-14 15:58:56 +01003669 // LR();
3670 __ blx(lr);
Artem Serovcfbe9132016-10-14 15:58:56 +01003671 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Alexandre Rames374ddf32016-11-04 10:40:49 +00003672 DCHECK(!codegen_->IsLeafMethod());
Artem Serovcfbe9132016-10-14 15:58:56 +01003673 }
Roland Levillain5daa4952017-07-03 17:23:56 +01003674
3675 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 8);
Artem Serovcfbe9132016-10-14 15:58:56 +01003676}
3677
Orion Hodsonac141392017-01-13 11:53:47 +00003678void LocationsBuilderARMVIXL::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
3679 HandleInvoke(invoke);
3680}
3681
3682void InstructionCodeGeneratorARMVIXL::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
3683 codegen_->GenerateInvokePolymorphicCall(invoke);
Roland Levillain5daa4952017-07-03 17:23:56 +01003684 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 9);
Orion Hodsonac141392017-01-13 11:53:47 +00003685}
3686
Artem Serov02109dd2016-09-23 17:17:54 +01003687void LocationsBuilderARMVIXL::VisitNeg(HNeg* neg) {
3688 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003689 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Artem Serov02109dd2016-09-23 17:17:54 +01003690 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003691 case DataType::Type::kInt32: {
Artem Serov02109dd2016-09-23 17:17:54 +01003692 locations->SetInAt(0, Location::RequiresRegister());
3693 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3694 break;
3695 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003696 case DataType::Type::kInt64: {
Artem Serov02109dd2016-09-23 17:17:54 +01003697 locations->SetInAt(0, Location::RequiresRegister());
3698 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3699 break;
3700 }
3701
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003702 case DataType::Type::kFloat32:
3703 case DataType::Type::kFloat64:
Artem Serov02109dd2016-09-23 17:17:54 +01003704 locations->SetInAt(0, Location::RequiresFpuRegister());
3705 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3706 break;
3707
3708 default:
3709 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
3710 }
3711}
3712
3713void InstructionCodeGeneratorARMVIXL::VisitNeg(HNeg* neg) {
3714 LocationSummary* locations = neg->GetLocations();
3715 Location out = locations->Out();
3716 Location in = locations->InAt(0);
3717 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003718 case DataType::Type::kInt32:
Artem Serov02109dd2016-09-23 17:17:54 +01003719 __ Rsb(OutputRegister(neg), InputRegisterAt(neg, 0), 0);
3720 break;
3721
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003722 case DataType::Type::kInt64:
Artem Serov02109dd2016-09-23 17:17:54 +01003723 // out.lo = 0 - in.lo (and update the carry/borrow (C) flag)
3724 __ Rsbs(LowRegisterFrom(out), LowRegisterFrom(in), 0);
3725 // We cannot emit an RSC (Reverse Subtract with Carry)
3726 // instruction here, as it does not exist in the Thumb-2
3727 // instruction set. We use the following approach
3728 // using SBC and SUB instead.
3729 //
3730 // out.hi = -C
3731 __ Sbc(HighRegisterFrom(out), HighRegisterFrom(out), HighRegisterFrom(out));
3732 // out.hi = out.hi - in.hi
3733 __ Sub(HighRegisterFrom(out), HighRegisterFrom(out), HighRegisterFrom(in));
3734 break;
3735
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003736 case DataType::Type::kFloat32:
3737 case DataType::Type::kFloat64:
Anton Kirilov644032c2016-12-06 17:51:43 +00003738 __ Vneg(OutputVRegister(neg), InputVRegister(neg));
Artem Serov02109dd2016-09-23 17:17:54 +01003739 break;
3740
3741 default:
3742 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
3743 }
3744}
3745
Scott Wakelingfe885462016-09-22 10:24:38 +01003746void LocationsBuilderARMVIXL::VisitTypeConversion(HTypeConversion* conversion) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003747 DataType::Type result_type = conversion->GetResultType();
3748 DataType::Type input_type = conversion->GetInputType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003749 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
3750 << input_type << " -> " << result_type;
Scott Wakelingfe885462016-09-22 10:24:38 +01003751
3752 // The float-to-long, double-to-long and long-to-float type conversions
3753 // rely on a call to the runtime.
3754 LocationSummary::CallKind call_kind =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003755 (((input_type == DataType::Type::kFloat32 || input_type == DataType::Type::kFloat64)
3756 && result_type == DataType::Type::kInt64)
3757 || (input_type == DataType::Type::kInt64 && result_type == DataType::Type::kFloat32))
Scott Wakelingfe885462016-09-22 10:24:38 +01003758 ? LocationSummary::kCallOnMainOnly
3759 : LocationSummary::kNoCall;
3760 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003761 new (GetGraph()->GetAllocator()) LocationSummary(conversion, call_kind);
Scott Wakelingfe885462016-09-22 10:24:38 +01003762
Scott Wakelingfe885462016-09-22 10:24:38 +01003763 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003764 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003765 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003766 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003767 case DataType::Type::kInt16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003768 DCHECK(DataType::IsIntegralType(input_type)) << input_type;
3769 locations->SetInAt(0, Location::RequiresRegister());
3770 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Scott Wakelingfe885462016-09-22 10:24:38 +01003771 break;
3772
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003773 case DataType::Type::kInt32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003774 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003775 case DataType::Type::kInt64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003776 locations->SetInAt(0, Location::Any());
3777 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3778 break;
3779
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003780 case DataType::Type::kFloat32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003781 locations->SetInAt(0, Location::RequiresFpuRegister());
3782 locations->SetOut(Location::RequiresRegister());
3783 locations->AddTemp(Location::RequiresFpuRegister());
3784 break;
3785
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003786 case DataType::Type::kFloat64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003787 locations->SetInAt(0, Location::RequiresFpuRegister());
3788 locations->SetOut(Location::RequiresRegister());
3789 locations->AddTemp(Location::RequiresFpuRegister());
3790 break;
3791
3792 default:
3793 LOG(FATAL) << "Unexpected type conversion from " << input_type
3794 << " to " << result_type;
3795 }
3796 break;
3797
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003798 case DataType::Type::kInt64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003799 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003800 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003801 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003802 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003803 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003804 case DataType::Type::kInt16:
3805 case DataType::Type::kInt32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003806 locations->SetInAt(0, Location::RequiresRegister());
3807 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3808 break;
3809
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003810 case DataType::Type::kFloat32: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003811 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3812 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
3813 locations->SetOut(LocationFrom(r0, r1));
Scott Wakelingfe885462016-09-22 10:24:38 +01003814 break;
3815 }
3816
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003817 case DataType::Type::kFloat64: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003818 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3819 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0),
3820 calling_convention.GetFpuRegisterAt(1)));
3821 locations->SetOut(LocationFrom(r0, r1));
Scott Wakelingfe885462016-09-22 10:24:38 +01003822 break;
3823 }
3824
3825 default:
3826 LOG(FATAL) << "Unexpected type conversion from " << input_type
3827 << " to " << result_type;
3828 }
3829 break;
3830
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003831 case DataType::Type::kFloat32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003832 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003833 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003834 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003835 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003836 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003837 case DataType::Type::kInt16:
3838 case DataType::Type::kInt32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003839 locations->SetInAt(0, Location::RequiresRegister());
3840 locations->SetOut(Location::RequiresFpuRegister());
3841 break;
3842
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003843 case DataType::Type::kInt64: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003844 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3845 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0),
3846 calling_convention.GetRegisterAt(1)));
3847 locations->SetOut(LocationFrom(calling_convention.GetFpuRegisterAt(0)));
Scott Wakelingfe885462016-09-22 10:24:38 +01003848 break;
3849 }
3850
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003851 case DataType::Type::kFloat64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003852 locations->SetInAt(0, Location::RequiresFpuRegister());
3853 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3854 break;
3855
3856 default:
3857 LOG(FATAL) << "Unexpected type conversion from " << input_type
3858 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08003859 }
Scott Wakelingfe885462016-09-22 10:24:38 +01003860 break;
3861
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003862 case DataType::Type::kFloat64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003863 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003864 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003865 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003866 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003867 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003868 case DataType::Type::kInt16:
3869 case DataType::Type::kInt32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003870 locations->SetInAt(0, Location::RequiresRegister());
3871 locations->SetOut(Location::RequiresFpuRegister());
3872 break;
3873
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003874 case DataType::Type::kInt64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003875 locations->SetInAt(0, Location::RequiresRegister());
3876 locations->SetOut(Location::RequiresFpuRegister());
3877 locations->AddTemp(Location::RequiresFpuRegister());
3878 locations->AddTemp(Location::RequiresFpuRegister());
3879 break;
3880
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003881 case DataType::Type::kFloat32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003882 locations->SetInAt(0, Location::RequiresFpuRegister());
3883 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3884 break;
3885
3886 default:
3887 LOG(FATAL) << "Unexpected type conversion from " << input_type
3888 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08003889 }
Scott Wakelingfe885462016-09-22 10:24:38 +01003890 break;
3891
3892 default:
3893 LOG(FATAL) << "Unexpected type conversion from " << input_type
3894 << " to " << result_type;
3895 }
3896}
3897
3898void InstructionCodeGeneratorARMVIXL::VisitTypeConversion(HTypeConversion* conversion) {
3899 LocationSummary* locations = conversion->GetLocations();
3900 Location out = locations->Out();
3901 Location in = locations->InAt(0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003902 DataType::Type result_type = conversion->GetResultType();
3903 DataType::Type input_type = conversion->GetInputType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003904 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
3905 << input_type << " -> " << result_type;
Scott Wakelingfe885462016-09-22 10:24:38 +01003906 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003907 case DataType::Type::kUint8:
Scott Wakelingfe885462016-09-22 10:24:38 +01003908 switch (input_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003909 case DataType::Type::kInt8:
3910 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003911 case DataType::Type::kInt16:
3912 case DataType::Type::kInt32:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003913 __ Ubfx(OutputRegister(conversion), InputRegisterAt(conversion, 0), 0, 8);
3914 break;
3915 case DataType::Type::kInt64:
3916 __ Ubfx(OutputRegister(conversion), LowRegisterFrom(in), 0, 8);
3917 break;
3918
3919 default:
3920 LOG(FATAL) << "Unexpected type conversion from " << input_type
3921 << " to " << result_type;
3922 }
3923 break;
3924
3925 case DataType::Type::kInt8:
3926 switch (input_type) {
3927 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003928 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003929 case DataType::Type::kInt16:
3930 case DataType::Type::kInt32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003931 __ Sbfx(OutputRegister(conversion), InputRegisterAt(conversion, 0), 0, 8);
3932 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003933 case DataType::Type::kInt64:
3934 __ Sbfx(OutputRegister(conversion), LowRegisterFrom(in), 0, 8);
3935 break;
3936
3937 default:
3938 LOG(FATAL) << "Unexpected type conversion from " << input_type
3939 << " to " << result_type;
3940 }
3941 break;
3942
3943 case DataType::Type::kUint16:
3944 switch (input_type) {
3945 case DataType::Type::kInt8:
3946 case DataType::Type::kInt16:
3947 case DataType::Type::kInt32:
3948 __ Ubfx(OutputRegister(conversion), InputRegisterAt(conversion, 0), 0, 16);
3949 break;
3950 case DataType::Type::kInt64:
3951 __ Ubfx(OutputRegister(conversion), LowRegisterFrom(in), 0, 16);
3952 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01003953
3954 default:
3955 LOG(FATAL) << "Unexpected type conversion from " << input_type
3956 << " to " << result_type;
3957 }
3958 break;
3959
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003960 case DataType::Type::kInt16:
Scott Wakelingfe885462016-09-22 10:24:38 +01003961 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003962 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003963 case DataType::Type::kInt32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003964 __ Sbfx(OutputRegister(conversion), InputRegisterAt(conversion, 0), 0, 16);
3965 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003966 case DataType::Type::kInt64:
3967 __ Sbfx(OutputRegister(conversion), LowRegisterFrom(in), 0, 16);
3968 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01003969
3970 default:
3971 LOG(FATAL) << "Unexpected type conversion from " << input_type
3972 << " to " << result_type;
3973 }
3974 break;
3975
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003976 case DataType::Type::kInt32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003977 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003978 case DataType::Type::kInt64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003979 DCHECK(out.IsRegister());
3980 if (in.IsRegisterPair()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003981 __ Mov(OutputRegister(conversion), LowRegisterFrom(in));
Scott Wakelingfe885462016-09-22 10:24:38 +01003982 } else if (in.IsDoubleStackSlot()) {
3983 GetAssembler()->LoadFromOffset(kLoadWord,
3984 OutputRegister(conversion),
3985 sp,
3986 in.GetStackIndex());
3987 } else {
3988 DCHECK(in.IsConstant());
3989 DCHECK(in.GetConstant()->IsLongConstant());
Vladimir Markoba1a48e2017-04-13 11:50:14 +01003990 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
3991 __ Mov(OutputRegister(conversion), static_cast<int32_t>(value));
Scott Wakelingfe885462016-09-22 10:24:38 +01003992 }
3993 break;
3994
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003995 case DataType::Type::kFloat32: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003996 vixl32::SRegister temp = LowSRegisterFrom(locations->GetTemp(0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01003997 __ Vcvt(S32, F32, temp, InputSRegisterAt(conversion, 0));
Scott Wakelingfe885462016-09-22 10:24:38 +01003998 __ Vmov(OutputRegister(conversion), temp);
3999 break;
4000 }
4001
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004002 case DataType::Type::kFloat64: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004003 vixl32::SRegister temp_s = LowSRegisterFrom(locations->GetTemp(0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01004004 __ Vcvt(S32, F64, temp_s, DRegisterFrom(in));
Scott Wakelingfe885462016-09-22 10:24:38 +01004005 __ Vmov(OutputRegister(conversion), temp_s);
4006 break;
4007 }
4008
4009 default:
4010 LOG(FATAL) << "Unexpected type conversion from " << input_type
4011 << " to " << result_type;
4012 }
4013 break;
4014
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004015 case DataType::Type::kInt64:
Scott Wakelingfe885462016-09-22 10:24:38 +01004016 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004017 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004018 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004019 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004020 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004021 case DataType::Type::kInt16:
4022 case DataType::Type::kInt32:
Scott Wakelingfe885462016-09-22 10:24:38 +01004023 DCHECK(out.IsRegisterPair());
4024 DCHECK(in.IsRegister());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004025 __ Mov(LowRegisterFrom(out), InputRegisterAt(conversion, 0));
Scott Wakelingfe885462016-09-22 10:24:38 +01004026 // Sign extension.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004027 __ Asr(HighRegisterFrom(out), LowRegisterFrom(out), 31);
Scott Wakelingfe885462016-09-22 10:24:38 +01004028 break;
4029
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004030 case DataType::Type::kFloat32:
Scott Wakelingfe885462016-09-22 10:24:38 +01004031 codegen_->InvokeRuntime(kQuickF2l, conversion, conversion->GetDexPc());
4032 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
4033 break;
4034
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004035 case DataType::Type::kFloat64:
Scott Wakelingfe885462016-09-22 10:24:38 +01004036 codegen_->InvokeRuntime(kQuickD2l, conversion, conversion->GetDexPc());
4037 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
4038 break;
4039
4040 default:
4041 LOG(FATAL) << "Unexpected type conversion from " << input_type
4042 << " to " << result_type;
4043 }
4044 break;
4045
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004046 case DataType::Type::kFloat32:
Scott Wakelingfe885462016-09-22 10:24:38 +01004047 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004048 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004049 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004050 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004051 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004052 case DataType::Type::kInt16:
4053 case DataType::Type::kInt32:
Scott Wakelingfe885462016-09-22 10:24:38 +01004054 __ Vmov(OutputSRegister(conversion), InputRegisterAt(conversion, 0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01004055 __ Vcvt(F32, S32, OutputSRegister(conversion), OutputSRegister(conversion));
Scott Wakelingfe885462016-09-22 10:24:38 +01004056 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01004057
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004058 case DataType::Type::kInt64:
Scott Wakelingfe885462016-09-22 10:24:38 +01004059 codegen_->InvokeRuntime(kQuickL2f, conversion, conversion->GetDexPc());
4060 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
4061 break;
4062
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004063 case DataType::Type::kFloat64:
Scott Wakelingc34dba72016-10-03 10:14:44 +01004064 __ Vcvt(F32, F64, OutputSRegister(conversion), DRegisterFrom(in));
Scott Wakelingfe885462016-09-22 10:24:38 +01004065 break;
4066
4067 default:
4068 LOG(FATAL) << "Unexpected type conversion from " << input_type
4069 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08004070 }
Scott Wakelingfe885462016-09-22 10:24:38 +01004071 break;
4072
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004073 case DataType::Type::kFloat64:
Scott Wakelingfe885462016-09-22 10:24:38 +01004074 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004075 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004076 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004077 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004078 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004079 case DataType::Type::kInt16:
4080 case DataType::Type::kInt32:
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004081 __ Vmov(LowSRegisterFrom(out), InputRegisterAt(conversion, 0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01004082 __ Vcvt(F64, S32, DRegisterFrom(out), LowSRegisterFrom(out));
Scott Wakelingfe885462016-09-22 10:24:38 +01004083 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01004084
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004085 case DataType::Type::kInt64: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004086 vixl32::Register low = LowRegisterFrom(in);
4087 vixl32::Register high = HighRegisterFrom(in);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004088 vixl32::SRegister out_s = LowSRegisterFrom(out);
Scott Wakelingc34dba72016-10-03 10:14:44 +01004089 vixl32::DRegister out_d = DRegisterFrom(out);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004090 vixl32::SRegister temp_s = LowSRegisterFrom(locations->GetTemp(0));
Scott Wakelingc34dba72016-10-03 10:14:44 +01004091 vixl32::DRegister temp_d = DRegisterFrom(locations->GetTemp(0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01004092 vixl32::DRegister constant_d = DRegisterFrom(locations->GetTemp(1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004093
4094 // temp_d = int-to-double(high)
4095 __ Vmov(temp_s, high);
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01004096 __ Vcvt(F64, S32, temp_d, temp_s);
Scott Wakelingfe885462016-09-22 10:24:38 +01004097 // constant_d = k2Pow32EncodingForDouble
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004098 __ Vmov(constant_d, bit_cast<double, int64_t>(k2Pow32EncodingForDouble));
Scott Wakelingfe885462016-09-22 10:24:38 +01004099 // out_d = unsigned-to-double(low)
4100 __ Vmov(out_s, low);
4101 __ Vcvt(F64, U32, out_d, out_s);
4102 // out_d += temp_d * constant_d
4103 __ Vmla(F64, out_d, temp_d, constant_d);
4104 break;
4105 }
4106
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004107 case DataType::Type::kFloat32:
Scott Wakelingc34dba72016-10-03 10:14:44 +01004108 __ Vcvt(F64, F32, DRegisterFrom(out), InputSRegisterAt(conversion, 0));
Scott Wakelingfe885462016-09-22 10:24:38 +01004109 break;
4110
4111 default:
4112 LOG(FATAL) << "Unexpected type conversion from " << input_type
4113 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08004114 }
Scott Wakelingfe885462016-09-22 10:24:38 +01004115 break;
4116
4117 default:
4118 LOG(FATAL) << "Unexpected type conversion from " << input_type
4119 << " to " << result_type;
4120 }
4121}
4122
4123void LocationsBuilderARMVIXL::VisitAdd(HAdd* add) {
4124 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004125 new (GetGraph()->GetAllocator()) LocationSummary(add, LocationSummary::kNoCall);
Scott Wakelingfe885462016-09-22 10:24:38 +01004126 switch (add->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004127 case DataType::Type::kInt32: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004128 locations->SetInAt(0, Location::RequiresRegister());
4129 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
4130 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4131 break;
4132 }
4133
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004134 case DataType::Type::kInt64: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004135 locations->SetInAt(0, Location::RequiresRegister());
Anton Kirilovdda43962016-11-21 19:55:20 +00004136 locations->SetInAt(1, ArmEncodableConstantOrRegister(add->InputAt(1), ADD));
Scott Wakelingfe885462016-09-22 10:24:38 +01004137 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4138 break;
4139 }
4140
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004141 case DataType::Type::kFloat32:
4142 case DataType::Type::kFloat64: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004143 locations->SetInAt(0, Location::RequiresFpuRegister());
4144 locations->SetInAt(1, Location::RequiresFpuRegister());
4145 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4146 break;
4147 }
4148
4149 default:
4150 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
4151 }
4152}
4153
4154void InstructionCodeGeneratorARMVIXL::VisitAdd(HAdd* add) {
4155 LocationSummary* locations = add->GetLocations();
4156 Location out = locations->Out();
4157 Location first = locations->InAt(0);
4158 Location second = locations->InAt(1);
4159
4160 switch (add->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004161 case DataType::Type::kInt32: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004162 __ Add(OutputRegister(add), InputRegisterAt(add, 0), InputOperandAt(add, 1));
4163 }
4164 break;
4165
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004166 case DataType::Type::kInt64: {
Anton Kirilovdda43962016-11-21 19:55:20 +00004167 if (second.IsConstant()) {
4168 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
4169 GenerateAddLongConst(out, first, value);
4170 } else {
4171 DCHECK(second.IsRegisterPair());
4172 __ Adds(LowRegisterFrom(out), LowRegisterFrom(first), LowRegisterFrom(second));
4173 __ Adc(HighRegisterFrom(out), HighRegisterFrom(first), HighRegisterFrom(second));
4174 }
Scott Wakelingfe885462016-09-22 10:24:38 +01004175 break;
4176 }
4177
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004178 case DataType::Type::kFloat32:
4179 case DataType::Type::kFloat64:
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004180 __ Vadd(OutputVRegister(add), InputVRegisterAt(add, 0), InputVRegisterAt(add, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004181 break;
4182
4183 default:
4184 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
4185 }
4186}
4187
4188void LocationsBuilderARMVIXL::VisitSub(HSub* sub) {
4189 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004190 new (GetGraph()->GetAllocator()) LocationSummary(sub, LocationSummary::kNoCall);
Scott Wakelingfe885462016-09-22 10:24:38 +01004191 switch (sub->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004192 case DataType::Type::kInt32: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004193 locations->SetInAt(0, Location::RequiresRegister());
4194 locations->SetInAt(1, Location::RegisterOrConstant(sub->InputAt(1)));
4195 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4196 break;
4197 }
4198
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004199 case DataType::Type::kInt64: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004200 locations->SetInAt(0, Location::RequiresRegister());
Anton Kirilovdda43962016-11-21 19:55:20 +00004201 locations->SetInAt(1, ArmEncodableConstantOrRegister(sub->InputAt(1), SUB));
Scott Wakelingfe885462016-09-22 10:24:38 +01004202 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4203 break;
4204 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004205 case DataType::Type::kFloat32:
4206 case DataType::Type::kFloat64: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004207 locations->SetInAt(0, Location::RequiresFpuRegister());
4208 locations->SetInAt(1, Location::RequiresFpuRegister());
4209 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4210 break;
4211 }
4212 default:
4213 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
4214 }
4215}
4216
4217void InstructionCodeGeneratorARMVIXL::VisitSub(HSub* sub) {
4218 LocationSummary* locations = sub->GetLocations();
4219 Location out = locations->Out();
4220 Location first = locations->InAt(0);
4221 Location second = locations->InAt(1);
4222 switch (sub->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004223 case DataType::Type::kInt32: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004224 __ Sub(OutputRegister(sub), InputRegisterAt(sub, 0), InputOperandAt(sub, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004225 break;
4226 }
4227
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004228 case DataType::Type::kInt64: {
Anton Kirilovdda43962016-11-21 19:55:20 +00004229 if (second.IsConstant()) {
4230 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
4231 GenerateAddLongConst(out, first, -value);
4232 } else {
4233 DCHECK(second.IsRegisterPair());
4234 __ Subs(LowRegisterFrom(out), LowRegisterFrom(first), LowRegisterFrom(second));
4235 __ Sbc(HighRegisterFrom(out), HighRegisterFrom(first), HighRegisterFrom(second));
4236 }
Scott Wakelingfe885462016-09-22 10:24:38 +01004237 break;
4238 }
4239
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004240 case DataType::Type::kFloat32:
4241 case DataType::Type::kFloat64:
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004242 __ Vsub(OutputVRegister(sub), InputVRegisterAt(sub, 0), InputVRegisterAt(sub, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004243 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01004244
4245 default:
4246 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
4247 }
4248}
4249
4250void LocationsBuilderARMVIXL::VisitMul(HMul* mul) {
4251 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004252 new (GetGraph()->GetAllocator()) LocationSummary(mul, LocationSummary::kNoCall);
Scott Wakelingfe885462016-09-22 10:24:38 +01004253 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004254 case DataType::Type::kInt32:
4255 case DataType::Type::kInt64: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004256 locations->SetInAt(0, Location::RequiresRegister());
4257 locations->SetInAt(1, Location::RequiresRegister());
4258 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4259 break;
4260 }
4261
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004262 case DataType::Type::kFloat32:
4263 case DataType::Type::kFloat64: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004264 locations->SetInAt(0, Location::RequiresFpuRegister());
4265 locations->SetInAt(1, Location::RequiresFpuRegister());
4266 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4267 break;
4268 }
4269
4270 default:
4271 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4272 }
4273}
4274
4275void InstructionCodeGeneratorARMVIXL::VisitMul(HMul* mul) {
4276 LocationSummary* locations = mul->GetLocations();
4277 Location out = locations->Out();
4278 Location first = locations->InAt(0);
4279 Location second = locations->InAt(1);
4280 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004281 case DataType::Type::kInt32: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004282 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
4283 break;
4284 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004285 case DataType::Type::kInt64: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004286 vixl32::Register out_hi = HighRegisterFrom(out);
4287 vixl32::Register out_lo = LowRegisterFrom(out);
4288 vixl32::Register in1_hi = HighRegisterFrom(first);
4289 vixl32::Register in1_lo = LowRegisterFrom(first);
4290 vixl32::Register in2_hi = HighRegisterFrom(second);
4291 vixl32::Register in2_lo = LowRegisterFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01004292
4293 // Extra checks to protect caused by the existence of R1_R2.
4294 // The algorithm is wrong if out.hi is either in1.lo or in2.lo:
4295 // (e.g. in1=r0_r1, in2=r2_r3 and out=r1_r2);
Anton Kirilov644032c2016-12-06 17:51:43 +00004296 DCHECK(!out_hi.Is(in1_lo));
4297 DCHECK(!out_hi.Is(in2_lo));
Scott Wakelingfe885462016-09-22 10:24:38 +01004298
4299 // input: in1 - 64 bits, in2 - 64 bits
4300 // output: out
4301 // formula: out.hi : out.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
4302 // parts: out.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
4303 // parts: out.lo = (in1.lo * in2.lo)[31:0]
4304
4305 UseScratchRegisterScope temps(GetVIXLAssembler());
4306 vixl32::Register temp = temps.Acquire();
4307 // temp <- in1.lo * in2.hi
4308 __ Mul(temp, in1_lo, in2_hi);
4309 // out.hi <- in1.lo * in2.hi + in1.hi * in2.lo
4310 __ Mla(out_hi, in1_hi, in2_lo, temp);
4311 // out.lo <- (in1.lo * in2.lo)[31:0];
4312 __ Umull(out_lo, temp, in1_lo, in2_lo);
4313 // out.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004314 __ Add(out_hi, out_hi, temp);
Scott Wakelingfe885462016-09-22 10:24:38 +01004315 break;
4316 }
4317
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004318 case DataType::Type::kFloat32:
4319 case DataType::Type::kFloat64:
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004320 __ Vmul(OutputVRegister(mul), InputVRegisterAt(mul, 0), InputVRegisterAt(mul, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004321 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01004322
4323 default:
4324 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4325 }
4326}
4327
Scott Wakelingfe885462016-09-22 10:24:38 +01004328void InstructionCodeGeneratorARMVIXL::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
4329 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004330 DCHECK(instruction->GetResultType() == DataType::Type::kInt32);
Scott Wakelingfe885462016-09-22 10:24:38 +01004331
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004332 Location second = instruction->GetLocations()->InAt(1);
Scott Wakelingfe885462016-09-22 10:24:38 +01004333 DCHECK(second.IsConstant());
4334
4335 vixl32::Register out = OutputRegister(instruction);
4336 vixl32::Register dividend = InputRegisterAt(instruction, 0);
Anton Kirilov644032c2016-12-06 17:51:43 +00004337 int32_t imm = Int32ConstantFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01004338 DCHECK(imm == 1 || imm == -1);
4339
4340 if (instruction->IsRem()) {
4341 __ Mov(out, 0);
4342 } else {
4343 if (imm == 1) {
4344 __ Mov(out, dividend);
4345 } else {
4346 __ Rsb(out, dividend, 0);
4347 }
4348 }
4349}
4350
4351void InstructionCodeGeneratorARMVIXL::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
4352 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004353 DCHECK(instruction->GetResultType() == DataType::Type::kInt32);
Scott Wakelingfe885462016-09-22 10:24:38 +01004354
4355 LocationSummary* locations = instruction->GetLocations();
4356 Location second = locations->InAt(1);
4357 DCHECK(second.IsConstant());
4358
4359 vixl32::Register out = OutputRegister(instruction);
4360 vixl32::Register dividend = InputRegisterAt(instruction, 0);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004361 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
Anton Kirilov644032c2016-12-06 17:51:43 +00004362 int32_t imm = Int32ConstantFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01004363 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
4364 int ctz_imm = CTZ(abs_imm);
4365
4366 if (ctz_imm == 1) {
4367 __ Lsr(temp, dividend, 32 - ctz_imm);
4368 } else {
4369 __ Asr(temp, dividend, 31);
4370 __ Lsr(temp, temp, 32 - ctz_imm);
4371 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004372 __ Add(out, temp, dividend);
Scott Wakelingfe885462016-09-22 10:24:38 +01004373
4374 if (instruction->IsDiv()) {
4375 __ Asr(out, out, ctz_imm);
4376 if (imm < 0) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004377 __ Rsb(out, out, 0);
Scott Wakelingfe885462016-09-22 10:24:38 +01004378 }
4379 } else {
4380 __ Ubfx(out, out, 0, ctz_imm);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004381 __ Sub(out, out, temp);
Scott Wakelingfe885462016-09-22 10:24:38 +01004382 }
4383}
4384
4385void InstructionCodeGeneratorARMVIXL::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
4386 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004387 DCHECK(instruction->GetResultType() == DataType::Type::kInt32);
Scott Wakelingfe885462016-09-22 10:24:38 +01004388
4389 LocationSummary* locations = instruction->GetLocations();
4390 Location second = locations->InAt(1);
4391 DCHECK(second.IsConstant());
4392
4393 vixl32::Register out = OutputRegister(instruction);
4394 vixl32::Register dividend = InputRegisterAt(instruction, 0);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004395 vixl32::Register temp1 = RegisterFrom(locations->GetTemp(0));
4396 vixl32::Register temp2 = RegisterFrom(locations->GetTemp(1));
Scott Wakelingb77051e2016-11-21 19:46:00 +00004397 int32_t imm = Int32ConstantFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01004398
4399 int64_t magic;
4400 int shift;
4401 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
4402
Anton Kirilovdda43962016-11-21 19:55:20 +00004403 // TODO(VIXL): Change the static cast to Operand::From() after VIXL is fixed.
4404 __ Mov(temp1, static_cast<int32_t>(magic));
Scott Wakelingfe885462016-09-22 10:24:38 +01004405 __ Smull(temp2, temp1, dividend, temp1);
4406
4407 if (imm > 0 && magic < 0) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004408 __ Add(temp1, temp1, dividend);
Scott Wakelingfe885462016-09-22 10:24:38 +01004409 } else if (imm < 0 && magic > 0) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004410 __ Sub(temp1, temp1, dividend);
Scott Wakelingfe885462016-09-22 10:24:38 +01004411 }
4412
4413 if (shift != 0) {
4414 __ Asr(temp1, temp1, shift);
4415 }
4416
4417 if (instruction->IsDiv()) {
4418 __ Sub(out, temp1, Operand(temp1, vixl32::Shift(ASR), 31));
4419 } else {
4420 __ Sub(temp1, temp1, Operand(temp1, vixl32::Shift(ASR), 31));
4421 // TODO: Strength reduction for mls.
4422 __ Mov(temp2, imm);
4423 __ Mls(out, temp1, temp2, dividend);
4424 }
4425}
4426
4427void InstructionCodeGeneratorARMVIXL::GenerateDivRemConstantIntegral(
4428 HBinaryOperation* instruction) {
4429 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004430 DCHECK(instruction->GetResultType() == DataType::Type::kInt32);
Scott Wakelingfe885462016-09-22 10:24:38 +01004431
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004432 Location second = instruction->GetLocations()->InAt(1);
Scott Wakelingfe885462016-09-22 10:24:38 +01004433 DCHECK(second.IsConstant());
4434
Anton Kirilov644032c2016-12-06 17:51:43 +00004435 int32_t imm = Int32ConstantFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01004436 if (imm == 0) {
4437 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
4438 } else if (imm == 1 || imm == -1) {
4439 DivRemOneOrMinusOne(instruction);
4440 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
4441 DivRemByPowerOfTwo(instruction);
4442 } else {
4443 DCHECK(imm <= -2 || imm >= 2);
4444 GenerateDivRemWithAnyConstant(instruction);
4445 }
4446}
4447
4448void LocationsBuilderARMVIXL::VisitDiv(HDiv* div) {
4449 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004450 if (div->GetResultType() == DataType::Type::kInt64) {
Scott Wakelingfe885462016-09-22 10:24:38 +01004451 // pLdiv runtime call.
4452 call_kind = LocationSummary::kCallOnMainOnly;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004453 } else if (div->GetResultType() == DataType::Type::kInt32 && div->InputAt(1)->IsConstant()) {
Scott Wakelingfe885462016-09-22 10:24:38 +01004454 // sdiv will be replaced by other instruction sequence.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004455 } else if (div->GetResultType() == DataType::Type::kInt32 &&
Scott Wakelingfe885462016-09-22 10:24:38 +01004456 !codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4457 // pIdivmod runtime call.
4458 call_kind = LocationSummary::kCallOnMainOnly;
4459 }
4460
Vladimir Markoca6fff82017-10-03 14:49:14 +01004461 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(div, call_kind);
Scott Wakelingfe885462016-09-22 10:24:38 +01004462
4463 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004464 case DataType::Type::kInt32: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004465 if (div->InputAt(1)->IsConstant()) {
4466 locations->SetInAt(0, Location::RequiresRegister());
4467 locations->SetInAt(1, Location::ConstantLocation(div->InputAt(1)->AsConstant()));
4468 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Anton Kirilov644032c2016-12-06 17:51:43 +00004469 int32_t value = Int32ConstantFrom(div->InputAt(1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004470 if (value == 1 || value == 0 || value == -1) {
4471 // No temp register required.
4472 } else {
4473 locations->AddTemp(Location::RequiresRegister());
4474 if (!IsPowerOfTwo(AbsOrMin(value))) {
4475 locations->AddTemp(Location::RequiresRegister());
4476 }
4477 }
4478 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4479 locations->SetInAt(0, Location::RequiresRegister());
4480 locations->SetInAt(1, Location::RequiresRegister());
4481 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4482 } else {
Artem Serov551b28f2016-10-18 19:11:30 +01004483 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4484 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4485 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
Roland Levillain5e8d5f02016-10-18 18:03:43 +01004486 // Note: divmod will compute both the quotient and the remainder as the pair R0 and R1, but
Artem Serov551b28f2016-10-18 19:11:30 +01004487 // we only need the former.
4488 locations->SetOut(LocationFrom(r0));
Scott Wakelingfe885462016-09-22 10:24:38 +01004489 }
4490 break;
4491 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004492 case DataType::Type::kInt64: {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01004493 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4494 locations->SetInAt(0, LocationFrom(
4495 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
4496 locations->SetInAt(1, LocationFrom(
4497 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
4498 locations->SetOut(LocationFrom(r0, r1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004499 break;
4500 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004501 case DataType::Type::kFloat32:
4502 case DataType::Type::kFloat64: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004503 locations->SetInAt(0, Location::RequiresFpuRegister());
4504 locations->SetInAt(1, Location::RequiresFpuRegister());
4505 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4506 break;
4507 }
4508
4509 default:
4510 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
4511 }
4512}
4513
4514void InstructionCodeGeneratorARMVIXL::VisitDiv(HDiv* div) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01004515 Location lhs = div->GetLocations()->InAt(0);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004516 Location rhs = div->GetLocations()->InAt(1);
Scott Wakelingfe885462016-09-22 10:24:38 +01004517
4518 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004519 case DataType::Type::kInt32: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004520 if (rhs.IsConstant()) {
Scott Wakelingfe885462016-09-22 10:24:38 +01004521 GenerateDivRemConstantIntegral(div);
4522 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4523 __ Sdiv(OutputRegister(div), InputRegisterAt(div, 0), InputRegisterAt(div, 1));
4524 } else {
Artem Serov551b28f2016-10-18 19:11:30 +01004525 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4526 DCHECK(calling_convention.GetRegisterAt(0).Is(RegisterFrom(lhs)));
4527 DCHECK(calling_convention.GetRegisterAt(1).Is(RegisterFrom(rhs)));
4528 DCHECK(r0.Is(OutputRegister(div)));
4529
4530 codegen_->InvokeRuntime(kQuickIdivmod, div, div->GetDexPc());
4531 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
Scott Wakelingfe885462016-09-22 10:24:38 +01004532 }
4533 break;
4534 }
4535
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004536 case DataType::Type::kInt64: {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01004537 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4538 DCHECK(calling_convention.GetRegisterAt(0).Is(LowRegisterFrom(lhs)));
4539 DCHECK(calling_convention.GetRegisterAt(1).Is(HighRegisterFrom(lhs)));
4540 DCHECK(calling_convention.GetRegisterAt(2).Is(LowRegisterFrom(rhs)));
4541 DCHECK(calling_convention.GetRegisterAt(3).Is(HighRegisterFrom(rhs)));
4542 DCHECK(LowRegisterFrom(div->GetLocations()->Out()).Is(r0));
4543 DCHECK(HighRegisterFrom(div->GetLocations()->Out()).Is(r1));
4544
4545 codegen_->InvokeRuntime(kQuickLdiv, div, div->GetDexPc());
4546 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Scott Wakelingfe885462016-09-22 10:24:38 +01004547 break;
4548 }
4549
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004550 case DataType::Type::kFloat32:
4551 case DataType::Type::kFloat64:
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004552 __ Vdiv(OutputVRegister(div), InputVRegisterAt(div, 0), InputVRegisterAt(div, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004553 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01004554
4555 default:
4556 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
4557 }
4558}
4559
Artem Serov551b28f2016-10-18 19:11:30 +01004560void LocationsBuilderARMVIXL::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004561 DataType::Type type = rem->GetResultType();
Artem Serov551b28f2016-10-18 19:11:30 +01004562
4563 // Most remainders are implemented in the runtime.
4564 LocationSummary::CallKind call_kind = LocationSummary::kCallOnMainOnly;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004565 if (rem->GetResultType() == DataType::Type::kInt32 && rem->InputAt(1)->IsConstant()) {
Artem Serov551b28f2016-10-18 19:11:30 +01004566 // sdiv will be replaced by other instruction sequence.
4567 call_kind = LocationSummary::kNoCall;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004568 } else if ((rem->GetResultType() == DataType::Type::kInt32)
Artem Serov551b28f2016-10-18 19:11:30 +01004569 && codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4570 // Have hardware divide instruction for int, do it with three instructions.
4571 call_kind = LocationSummary::kNoCall;
4572 }
4573
Vladimir Markoca6fff82017-10-03 14:49:14 +01004574 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(rem, call_kind);
Artem Serov551b28f2016-10-18 19:11:30 +01004575
4576 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004577 case DataType::Type::kInt32: {
Artem Serov551b28f2016-10-18 19:11:30 +01004578 if (rem->InputAt(1)->IsConstant()) {
4579 locations->SetInAt(0, Location::RequiresRegister());
4580 locations->SetInAt(1, Location::ConstantLocation(rem->InputAt(1)->AsConstant()));
4581 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Anton Kirilov644032c2016-12-06 17:51:43 +00004582 int32_t value = Int32ConstantFrom(rem->InputAt(1));
Artem Serov551b28f2016-10-18 19:11:30 +01004583 if (value == 1 || value == 0 || value == -1) {
4584 // No temp register required.
4585 } else {
4586 locations->AddTemp(Location::RequiresRegister());
4587 if (!IsPowerOfTwo(AbsOrMin(value))) {
4588 locations->AddTemp(Location::RequiresRegister());
4589 }
4590 }
4591 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4592 locations->SetInAt(0, Location::RequiresRegister());
4593 locations->SetInAt(1, Location::RequiresRegister());
4594 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4595 locations->AddTemp(Location::RequiresRegister());
4596 } else {
4597 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4598 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4599 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
Roland Levillain5e8d5f02016-10-18 18:03:43 +01004600 // Note: divmod will compute both the quotient and the remainder as the pair R0 and R1, but
Artem Serov551b28f2016-10-18 19:11:30 +01004601 // we only need the latter.
4602 locations->SetOut(LocationFrom(r1));
4603 }
4604 break;
4605 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004606 case DataType::Type::kInt64: {
Artem Serov551b28f2016-10-18 19:11:30 +01004607 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4608 locations->SetInAt(0, LocationFrom(
4609 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
4610 locations->SetInAt(1, LocationFrom(
4611 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
4612 // The runtime helper puts the output in R2,R3.
4613 locations->SetOut(LocationFrom(r2, r3));
4614 break;
4615 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004616 case DataType::Type::kFloat32: {
Artem Serov551b28f2016-10-18 19:11:30 +01004617 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4618 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
4619 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
4620 locations->SetOut(LocationFrom(s0));
4621 break;
4622 }
4623
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004624 case DataType::Type::kFloat64: {
Artem Serov551b28f2016-10-18 19:11:30 +01004625 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4626 locations->SetInAt(0, LocationFrom(
4627 calling_convention.GetFpuRegisterAt(0), calling_convention.GetFpuRegisterAt(1)));
4628 locations->SetInAt(1, LocationFrom(
4629 calling_convention.GetFpuRegisterAt(2), calling_convention.GetFpuRegisterAt(3)));
4630 locations->SetOut(LocationFrom(s0, s1));
4631 break;
4632 }
4633
4634 default:
4635 LOG(FATAL) << "Unexpected rem type " << type;
4636 }
4637}
4638
4639void InstructionCodeGeneratorARMVIXL::VisitRem(HRem* rem) {
4640 LocationSummary* locations = rem->GetLocations();
4641 Location second = locations->InAt(1);
4642
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004643 DataType::Type type = rem->GetResultType();
Artem Serov551b28f2016-10-18 19:11:30 +01004644 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004645 case DataType::Type::kInt32: {
Artem Serov551b28f2016-10-18 19:11:30 +01004646 vixl32::Register reg1 = InputRegisterAt(rem, 0);
4647 vixl32::Register out_reg = OutputRegister(rem);
4648 if (second.IsConstant()) {
4649 GenerateDivRemConstantIntegral(rem);
4650 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4651 vixl32::Register reg2 = RegisterFrom(second);
4652 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
4653
4654 // temp = reg1 / reg2 (integer division)
4655 // dest = reg1 - temp * reg2
4656 __ Sdiv(temp, reg1, reg2);
4657 __ Mls(out_reg, temp, reg2, reg1);
4658 } else {
4659 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4660 DCHECK(reg1.Is(calling_convention.GetRegisterAt(0)));
4661 DCHECK(RegisterFrom(second).Is(calling_convention.GetRegisterAt(1)));
4662 DCHECK(out_reg.Is(r1));
4663
4664 codegen_->InvokeRuntime(kQuickIdivmod, rem, rem->GetDexPc());
4665 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
4666 }
4667 break;
4668 }
4669
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004670 case DataType::Type::kInt64: {
Artem Serov551b28f2016-10-18 19:11:30 +01004671 codegen_->InvokeRuntime(kQuickLmod, rem, rem->GetDexPc());
4672 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
4673 break;
4674 }
4675
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004676 case DataType::Type::kFloat32: {
Artem Serov551b28f2016-10-18 19:11:30 +01004677 codegen_->InvokeRuntime(kQuickFmodf, rem, rem->GetDexPc());
4678 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
4679 break;
4680 }
4681
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004682 case DataType::Type::kFloat64: {
Artem Serov551b28f2016-10-18 19:11:30 +01004683 codegen_->InvokeRuntime(kQuickFmod, rem, rem->GetDexPc());
4684 CheckEntrypointTypes<kQuickFmod, double, double, double>();
4685 break;
4686 }
4687
4688 default:
4689 LOG(FATAL) << "Unexpected rem type " << type;
4690 }
4691}
4692
Aart Bik1f8d51b2018-02-15 10:42:37 -08004693static void CreateMinMaxLocations(ArenaAllocator* allocator, HBinaryOperation* minmax) {
4694 LocationSummary* locations = new (allocator) LocationSummary(minmax);
4695 switch (minmax->GetResultType()) {
4696 case DataType::Type::kInt32:
4697 locations->SetInAt(0, Location::RequiresRegister());
4698 locations->SetInAt(1, Location::RequiresRegister());
4699 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4700 break;
4701 case DataType::Type::kInt64:
4702 locations->SetInAt(0, Location::RequiresRegister());
4703 locations->SetInAt(1, Location::RequiresRegister());
4704 locations->SetOut(Location::SameAsFirstInput());
4705 break;
4706 case DataType::Type::kFloat32:
4707 locations->SetInAt(0, Location::RequiresFpuRegister());
4708 locations->SetInAt(1, Location::RequiresFpuRegister());
4709 locations->SetOut(Location::SameAsFirstInput());
4710 locations->AddTemp(Location::RequiresRegister());
4711 break;
4712 case DataType::Type::kFloat64:
4713 locations->SetInAt(0, Location::RequiresFpuRegister());
4714 locations->SetInAt(1, Location::RequiresFpuRegister());
4715 locations->SetOut(Location::SameAsFirstInput());
4716 break;
4717 default:
4718 LOG(FATAL) << "Unexpected type for HMinMax " << minmax->GetResultType();
4719 }
4720}
4721
4722void InstructionCodeGeneratorARMVIXL::GenerateMinMax(LocationSummary* locations, bool is_min) {
4723 Location op1_loc = locations->InAt(0);
4724 Location op2_loc = locations->InAt(1);
4725 Location out_loc = locations->Out();
4726
4727 vixl32::Register op1 = RegisterFrom(op1_loc);
4728 vixl32::Register op2 = RegisterFrom(op2_loc);
4729 vixl32::Register out = RegisterFrom(out_loc);
4730
4731 __ Cmp(op1, op2);
4732
4733 {
4734 ExactAssemblyScope aas(GetVIXLAssembler(),
4735 3 * kMaxInstructionSizeInBytes,
4736 CodeBufferCheckScope::kMaximumSize);
4737
4738 __ ite(is_min ? lt : gt);
4739 __ mov(is_min ? lt : gt, out, op1);
4740 __ mov(is_min ? ge : le, out, op2);
4741 }
4742}
4743
4744void InstructionCodeGeneratorARMVIXL::GenerateMinMaxLong(LocationSummary* locations, bool is_min) {
4745 Location op1_loc = locations->InAt(0);
4746 Location op2_loc = locations->InAt(1);
4747 Location out_loc = locations->Out();
4748
4749 // Optimization: don't generate any code if inputs are the same.
4750 if (op1_loc.Equals(op2_loc)) {
4751 DCHECK(out_loc.Equals(op1_loc)); // out_loc is set as SameAsFirstInput() in location builder.
4752 return;
4753 }
4754
4755 vixl32::Register op1_lo = LowRegisterFrom(op1_loc);
4756 vixl32::Register op1_hi = HighRegisterFrom(op1_loc);
4757 vixl32::Register op2_lo = LowRegisterFrom(op2_loc);
4758 vixl32::Register op2_hi = HighRegisterFrom(op2_loc);
4759 vixl32::Register out_lo = LowRegisterFrom(out_loc);
4760 vixl32::Register out_hi = HighRegisterFrom(out_loc);
4761 UseScratchRegisterScope temps(GetVIXLAssembler());
4762 const vixl32::Register temp = temps.Acquire();
4763
4764 DCHECK(op1_lo.Is(out_lo));
4765 DCHECK(op1_hi.Is(out_hi));
4766
4767 // Compare op1 >= op2, or op1 < op2.
4768 __ Cmp(out_lo, op2_lo);
4769 __ Sbcs(temp, out_hi, op2_hi);
4770
4771 // Now GE/LT condition code is correct for the long comparison.
4772 {
4773 vixl32::ConditionType cond = is_min ? ge : lt;
4774 ExactAssemblyScope it_scope(GetVIXLAssembler(),
4775 3 * kMaxInstructionSizeInBytes,
4776 CodeBufferCheckScope::kMaximumSize);
4777 __ itt(cond);
4778 __ mov(cond, out_lo, op2_lo);
4779 __ mov(cond, out_hi, op2_hi);
4780 }
4781}
4782
4783void InstructionCodeGeneratorARMVIXL::GenerateMinMaxFloat(HInstruction* min_max, bool is_min) {
4784 LocationSummary* locations = min_max->GetLocations();
4785 Location op1_loc = locations->InAt(0);
4786 Location op2_loc = locations->InAt(1);
4787 Location out_loc = locations->Out();
4788
4789 // Optimization: don't generate any code if inputs are the same.
4790 if (op1_loc.Equals(op2_loc)) {
4791 DCHECK(out_loc.Equals(op1_loc)); // out_loc is set as SameAsFirstInput() in location builder.
4792 return;
4793 }
4794
4795 vixl32::SRegister op1 = SRegisterFrom(op1_loc);
4796 vixl32::SRegister op2 = SRegisterFrom(op2_loc);
4797 vixl32::SRegister out = SRegisterFrom(out_loc);
4798
4799 UseScratchRegisterScope temps(GetVIXLAssembler());
4800 const vixl32::Register temp1 = temps.Acquire();
4801 vixl32::Register temp2 = RegisterFrom(locations->GetTemp(0));
4802 vixl32::Label nan, done;
4803 vixl32::Label* final_label = codegen_->GetFinalLabel(min_max, &done);
4804
4805 DCHECK(op1.Is(out));
4806
4807 __ Vcmp(op1, op2);
4808 __ Vmrs(RegisterOrAPSR_nzcv(kPcCode), FPSCR);
4809 __ B(vs, &nan, /* far_target */ false); // if un-ordered, go to NaN handling.
4810
4811 // op1 <> op2
4812 vixl32::ConditionType cond = is_min ? gt : lt;
4813 {
4814 ExactAssemblyScope it_scope(GetVIXLAssembler(),
4815 2 * kMaxInstructionSizeInBytes,
4816 CodeBufferCheckScope::kMaximumSize);
4817 __ it(cond);
4818 __ vmov(cond, F32, out, op2);
4819 }
4820 // for <>(not equal), we've done min/max calculation.
4821 __ B(ne, final_label, /* far_target */ false);
4822
4823 // handle op1 == op2, max(+0.0,-0.0), min(+0.0,-0.0).
4824 __ Vmov(temp1, op1);
4825 __ Vmov(temp2, op2);
4826 if (is_min) {
4827 __ Orr(temp1, temp1, temp2);
4828 } else {
4829 __ And(temp1, temp1, temp2);
4830 }
4831 __ Vmov(out, temp1);
4832 __ B(final_label);
4833
4834 // handle NaN input.
4835 __ Bind(&nan);
4836 __ Movt(temp1, High16Bits(kNanFloat)); // 0x7FC0xxxx is a NaN.
4837 __ Vmov(out, temp1);
4838
4839 if (done.IsReferenced()) {
4840 __ Bind(&done);
4841 }
4842}
4843
4844void InstructionCodeGeneratorARMVIXL::GenerateMinMaxDouble(HInstruction* min_max, bool is_min) {
4845 LocationSummary* locations = min_max->GetLocations();
4846 Location op1_loc = locations->InAt(0);
4847 Location op2_loc = locations->InAt(1);
4848 Location out_loc = locations->Out();
4849
4850 // Optimization: don't generate any code if inputs are the same.
4851 if (op1_loc.Equals(op2_loc)) {
4852 DCHECK(out_loc.Equals(op1_loc)); // out_loc is set as SameAsFirstInput() in.
4853 return;
4854 }
4855
4856 vixl32::DRegister op1 = DRegisterFrom(op1_loc);
4857 vixl32::DRegister op2 = DRegisterFrom(op2_loc);
4858 vixl32::DRegister out = DRegisterFrom(out_loc);
4859 vixl32::Label handle_nan_eq, done;
4860 vixl32::Label* final_label = codegen_->GetFinalLabel(min_max, &done);
4861
4862 DCHECK(op1.Is(out));
4863
4864 __ Vcmp(op1, op2);
4865 __ Vmrs(RegisterOrAPSR_nzcv(kPcCode), FPSCR);
4866 __ B(vs, &handle_nan_eq, /* far_target */ false); // if un-ordered, go to NaN handling.
4867
4868 // op1 <> op2
4869 vixl32::ConditionType cond = is_min ? gt : lt;
4870 {
4871 ExactAssemblyScope it_scope(GetVIXLAssembler(),
4872 2 * kMaxInstructionSizeInBytes,
4873 CodeBufferCheckScope::kMaximumSize);
4874 __ it(cond);
4875 __ vmov(cond, F64, out, op2);
4876 }
4877 // for <>(not equal), we've done min/max calculation.
4878 __ B(ne, final_label, /* far_target */ false);
4879
4880 // handle op1 == op2, max(+0.0,-0.0).
4881 if (!is_min) {
4882 __ Vand(F64, out, op1, op2);
4883 __ B(final_label);
4884 }
4885
4886 // handle op1 == op2, min(+0.0,-0.0), NaN input.
4887 __ Bind(&handle_nan_eq);
4888 __ Vorr(F64, out, op1, op2); // assemble op1/-0.0/NaN.
4889
4890 if (done.IsReferenced()) {
4891 __ Bind(&done);
4892 }
4893}
4894
4895void LocationsBuilderARMVIXL::VisitMin(HMin* min) {
4896 CreateMinMaxLocations(GetGraph()->GetAllocator(), min);
4897}
4898
4899void InstructionCodeGeneratorARMVIXL::VisitMin(HMin* min) {
4900 switch (min->GetResultType()) {
4901 case DataType::Type::kInt32:
4902 GenerateMinMax(min->GetLocations(), /*is_min*/ true);
4903 break;
4904 case DataType::Type::kInt64:
4905 GenerateMinMaxLong(min->GetLocations(), /*is_min*/ true);
4906 break;
4907 case DataType::Type::kFloat32:
4908 GenerateMinMaxFloat(min, /*is_min*/ true);
4909 break;
4910 case DataType::Type::kFloat64:
4911 GenerateMinMaxDouble(min, /*is_min*/ true);
4912 break;
4913 default:
4914 LOG(FATAL) << "Unexpected type for HMin " << min->GetResultType();
4915 }
4916}
4917
4918void LocationsBuilderARMVIXL::VisitMax(HMax* max) {
4919 CreateMinMaxLocations(GetGraph()->GetAllocator(), max);
4920}
4921
4922void InstructionCodeGeneratorARMVIXL::VisitMax(HMax* max) {
4923 switch (max->GetResultType()) {
4924 case DataType::Type::kInt32:
4925 GenerateMinMax(max->GetLocations(), /*is_min*/ false);
4926 break;
4927 case DataType::Type::kInt64:
4928 GenerateMinMaxLong(max->GetLocations(), /*is_min*/ false);
4929 break;
4930 case DataType::Type::kFloat32:
4931 GenerateMinMaxFloat(max, /*is_min*/ false);
4932 break;
4933 case DataType::Type::kFloat64:
4934 GenerateMinMaxDouble(max, /*is_min*/ false);
4935 break;
4936 default:
4937 LOG(FATAL) << "Unexpected type for HMax " << max->GetResultType();
4938 }
4939}
4940
Aart Bik3dad3412018-02-28 12:01:46 -08004941void LocationsBuilderARMVIXL::VisitAbs(HAbs* abs) {
4942 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(abs);
4943 switch (abs->GetResultType()) {
4944 case DataType::Type::kInt32:
4945 case DataType::Type::kInt64:
4946 locations->SetInAt(0, Location::RequiresRegister());
4947 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4948 locations->AddTemp(Location::RequiresRegister());
4949 break;
4950 case DataType::Type::kFloat32:
4951 case DataType::Type::kFloat64:
4952 locations->SetInAt(0, Location::RequiresFpuRegister());
4953 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4954 break;
4955 default:
4956 LOG(FATAL) << "Unexpected type for abs operation " << abs->GetResultType();
4957 }
4958}
4959
4960void InstructionCodeGeneratorARMVIXL::VisitAbs(HAbs* abs) {
4961 LocationSummary* locations = abs->GetLocations();
4962 switch (abs->GetResultType()) {
4963 case DataType::Type::kInt32: {
4964 vixl32::Register in_reg = RegisterFrom(locations->InAt(0));
4965 vixl32::Register out_reg = RegisterFrom(locations->Out());
4966 vixl32::Register mask = RegisterFrom(locations->GetTemp(0));
4967 __ Asr(mask, in_reg, 31);
4968 __ Add(out_reg, in_reg, mask);
4969 __ Eor(out_reg, out_reg, mask);
4970 break;
4971 }
4972 case DataType::Type::kInt64: {
4973 Location in = locations->InAt(0);
4974 vixl32::Register in_reg_lo = LowRegisterFrom(in);
4975 vixl32::Register in_reg_hi = HighRegisterFrom(in);
4976 Location output = locations->Out();
4977 vixl32::Register out_reg_lo = LowRegisterFrom(output);
4978 vixl32::Register out_reg_hi = HighRegisterFrom(output);
4979 DCHECK(!out_reg_lo.Is(in_reg_hi)) << "Diagonal overlap unexpected.";
4980 vixl32::Register mask = RegisterFrom(locations->GetTemp(0));
4981 __ Asr(mask, in_reg_hi, 31);
4982 __ Adds(out_reg_lo, in_reg_lo, mask);
4983 __ Adc(out_reg_hi, in_reg_hi, mask);
4984 __ Eor(out_reg_lo, out_reg_lo, mask);
4985 __ Eor(out_reg_hi, out_reg_hi, mask);
4986 break;
4987 }
4988 case DataType::Type::kFloat32:
4989 case DataType::Type::kFloat64:
4990 __ Vabs(OutputVRegister(abs), InputVRegisterAt(abs, 0));
4991 break;
4992 default:
4993 LOG(FATAL) << "Unexpected type for abs operation " << abs->GetResultType();
4994 }
4995}
Artem Serov551b28f2016-10-18 19:11:30 +01004996
Scott Wakelingfe885462016-09-22 10:24:38 +01004997void LocationsBuilderARMVIXL::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Artem Serov657022c2016-11-23 14:19:38 +00004998 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Scott Wakelingfe885462016-09-22 10:24:38 +01004999 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Scott Wakelingfe885462016-09-22 10:24:38 +01005000}
5001
5002void InstructionCodeGeneratorARMVIXL::VisitDivZeroCheck(HDivZeroCheck* instruction) {
5003 DivZeroCheckSlowPathARMVIXL* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01005004 new (codegen_->GetScopedAllocator()) DivZeroCheckSlowPathARMVIXL(instruction);
Scott Wakelingfe885462016-09-22 10:24:38 +01005005 codegen_->AddSlowPath(slow_path);
5006
5007 LocationSummary* locations = instruction->GetLocations();
5008 Location value = locations->InAt(0);
5009
5010 switch (instruction->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005011 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005012 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005013 case DataType::Type::kInt8:
5014 case DataType::Type::kUint16:
5015 case DataType::Type::kInt16:
5016 case DataType::Type::kInt32: {
Scott Wakelingfe885462016-09-22 10:24:38 +01005017 if (value.IsRegister()) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00005018 __ CompareAndBranchIfZero(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
Scott Wakelingfe885462016-09-22 10:24:38 +01005019 } else {
5020 DCHECK(value.IsConstant()) << value;
Anton Kirilov644032c2016-12-06 17:51:43 +00005021 if (Int32ConstantFrom(value) == 0) {
Scott Wakelingfe885462016-09-22 10:24:38 +01005022 __ B(slow_path->GetEntryLabel());
5023 }
5024 }
5025 break;
5026 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005027 case DataType::Type::kInt64: {
Scott Wakelingfe885462016-09-22 10:24:38 +01005028 if (value.IsRegisterPair()) {
5029 UseScratchRegisterScope temps(GetVIXLAssembler());
5030 vixl32::Register temp = temps.Acquire();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005031 __ Orrs(temp, LowRegisterFrom(value), HighRegisterFrom(value));
Scott Wakelingfe885462016-09-22 10:24:38 +01005032 __ B(eq, slow_path->GetEntryLabel());
5033 } else {
5034 DCHECK(value.IsConstant()) << value;
Anton Kirilov644032c2016-12-06 17:51:43 +00005035 if (Int64ConstantFrom(value) == 0) {
Scott Wakelingfe885462016-09-22 10:24:38 +01005036 __ B(slow_path->GetEntryLabel());
5037 }
5038 }
5039 break;
5040 }
5041 default:
5042 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
5043 }
5044}
5045
Artem Serov02109dd2016-09-23 17:17:54 +01005046void InstructionCodeGeneratorARMVIXL::HandleIntegerRotate(HRor* ror) {
5047 LocationSummary* locations = ror->GetLocations();
5048 vixl32::Register in = InputRegisterAt(ror, 0);
5049 Location rhs = locations->InAt(1);
5050 vixl32::Register out = OutputRegister(ror);
5051
5052 if (rhs.IsConstant()) {
5053 // Arm32 and Thumb2 assemblers require a rotation on the interval [1,31],
5054 // so map all rotations to a +ve. equivalent in that range.
5055 // (e.g. left *or* right by -2 bits == 30 bits in the same direction.)
5056 uint32_t rot = CodeGenerator::GetInt32ValueOf(rhs.GetConstant()) & 0x1F;
5057 if (rot) {
5058 // Rotate, mapping left rotations to right equivalents if necessary.
5059 // (e.g. left by 2 bits == right by 30.)
5060 __ Ror(out, in, rot);
5061 } else if (!out.Is(in)) {
5062 __ Mov(out, in);
5063 }
5064 } else {
5065 __ Ror(out, in, RegisterFrom(rhs));
5066 }
5067}
5068
5069// Gain some speed by mapping all Long rotates onto equivalent pairs of Integer
5070// rotates by swapping input regs (effectively rotating by the first 32-bits of
5071// a larger rotation) or flipping direction (thus treating larger right/left
5072// rotations as sub-word sized rotations in the other direction) as appropriate.
5073void InstructionCodeGeneratorARMVIXL::HandleLongRotate(HRor* ror) {
5074 LocationSummary* locations = ror->GetLocations();
5075 vixl32::Register in_reg_lo = LowRegisterFrom(locations->InAt(0));
5076 vixl32::Register in_reg_hi = HighRegisterFrom(locations->InAt(0));
5077 Location rhs = locations->InAt(1);
5078 vixl32::Register out_reg_lo = LowRegisterFrom(locations->Out());
5079 vixl32::Register out_reg_hi = HighRegisterFrom(locations->Out());
5080
5081 if (rhs.IsConstant()) {
5082 uint64_t rot = CodeGenerator::GetInt64ValueOf(rhs.GetConstant());
5083 // Map all rotations to +ve. equivalents on the interval [0,63].
5084 rot &= kMaxLongShiftDistance;
5085 // For rotates over a word in size, 'pre-rotate' by 32-bits to keep rotate
5086 // logic below to a simple pair of binary orr.
5087 // (e.g. 34 bits == in_reg swap + 2 bits right.)
5088 if (rot >= kArmBitsPerWord) {
5089 rot -= kArmBitsPerWord;
5090 std::swap(in_reg_hi, in_reg_lo);
5091 }
5092 // Rotate, or mov to out for zero or word size rotations.
5093 if (rot != 0u) {
Scott Wakelingb77051e2016-11-21 19:46:00 +00005094 __ Lsr(out_reg_hi, in_reg_hi, Operand::From(rot));
Artem Serov02109dd2016-09-23 17:17:54 +01005095 __ Orr(out_reg_hi, out_reg_hi, Operand(in_reg_lo, ShiftType::LSL, kArmBitsPerWord - rot));
Scott Wakelingb77051e2016-11-21 19:46:00 +00005096 __ Lsr(out_reg_lo, in_reg_lo, Operand::From(rot));
Artem Serov02109dd2016-09-23 17:17:54 +01005097 __ Orr(out_reg_lo, out_reg_lo, Operand(in_reg_hi, ShiftType::LSL, kArmBitsPerWord - rot));
5098 } else {
5099 __ Mov(out_reg_lo, in_reg_lo);
5100 __ Mov(out_reg_hi, in_reg_hi);
5101 }
5102 } else {
5103 vixl32::Register shift_right = RegisterFrom(locations->GetTemp(0));
5104 vixl32::Register shift_left = RegisterFrom(locations->GetTemp(1));
5105 vixl32::Label end;
5106 vixl32::Label shift_by_32_plus_shift_right;
Anton Kirilov6f644202017-02-27 18:29:45 +00005107 vixl32::Label* final_label = codegen_->GetFinalLabel(ror, &end);
Artem Serov02109dd2016-09-23 17:17:54 +01005108
5109 __ And(shift_right, RegisterFrom(rhs), 0x1F);
5110 __ Lsrs(shift_left, RegisterFrom(rhs), 6);
Scott Wakelingbffdc702016-12-07 17:46:03 +00005111 __ Rsb(LeaveFlags, shift_left, shift_right, Operand::From(kArmBitsPerWord));
Artem Serov517d9f62016-12-12 15:51:15 +00005112 __ B(cc, &shift_by_32_plus_shift_right, /* far_target */ false);
Artem Serov02109dd2016-09-23 17:17:54 +01005113
5114 // out_reg_hi = (reg_hi << shift_left) | (reg_lo >> shift_right).
5115 // out_reg_lo = (reg_lo << shift_left) | (reg_hi >> shift_right).
5116 __ Lsl(out_reg_hi, in_reg_hi, shift_left);
5117 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
5118 __ Add(out_reg_hi, out_reg_hi, out_reg_lo);
5119 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
5120 __ Lsr(shift_left, in_reg_hi, shift_right);
5121 __ Add(out_reg_lo, out_reg_lo, shift_left);
Anton Kirilov6f644202017-02-27 18:29:45 +00005122 __ B(final_label);
Artem Serov02109dd2016-09-23 17:17:54 +01005123
5124 __ Bind(&shift_by_32_plus_shift_right); // Shift by 32+shift_right.
5125 // out_reg_hi = (reg_hi >> shift_right) | (reg_lo << shift_left).
5126 // out_reg_lo = (reg_lo >> shift_right) | (reg_hi << shift_left).
5127 __ Lsr(out_reg_hi, in_reg_hi, shift_right);
5128 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
5129 __ Add(out_reg_hi, out_reg_hi, out_reg_lo);
5130 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
5131 __ Lsl(shift_right, in_reg_hi, shift_left);
5132 __ Add(out_reg_lo, out_reg_lo, shift_right);
5133
Anton Kirilov6f644202017-02-27 18:29:45 +00005134 if (end.IsReferenced()) {
5135 __ Bind(&end);
5136 }
Artem Serov02109dd2016-09-23 17:17:54 +01005137 }
5138}
5139
5140void LocationsBuilderARMVIXL::VisitRor(HRor* ror) {
5141 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005142 new (GetGraph()->GetAllocator()) LocationSummary(ror, LocationSummary::kNoCall);
Artem Serov02109dd2016-09-23 17:17:54 +01005143 switch (ror->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005144 case DataType::Type::kInt32: {
Artem Serov02109dd2016-09-23 17:17:54 +01005145 locations->SetInAt(0, Location::RequiresRegister());
5146 locations->SetInAt(1, Location::RegisterOrConstant(ror->InputAt(1)));
5147 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5148 break;
5149 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005150 case DataType::Type::kInt64: {
Artem Serov02109dd2016-09-23 17:17:54 +01005151 locations->SetInAt(0, Location::RequiresRegister());
5152 if (ror->InputAt(1)->IsConstant()) {
5153 locations->SetInAt(1, Location::ConstantLocation(ror->InputAt(1)->AsConstant()));
5154 } else {
5155 locations->SetInAt(1, Location::RequiresRegister());
5156 locations->AddTemp(Location::RequiresRegister());
5157 locations->AddTemp(Location::RequiresRegister());
5158 }
5159 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
5160 break;
5161 }
5162 default:
5163 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
5164 }
5165}
5166
5167void InstructionCodeGeneratorARMVIXL::VisitRor(HRor* ror) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005168 DataType::Type type = ror->GetResultType();
Artem Serov02109dd2016-09-23 17:17:54 +01005169 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005170 case DataType::Type::kInt32: {
Artem Serov02109dd2016-09-23 17:17:54 +01005171 HandleIntegerRotate(ror);
5172 break;
5173 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005174 case DataType::Type::kInt64: {
Artem Serov02109dd2016-09-23 17:17:54 +01005175 HandleLongRotate(ror);
5176 break;
5177 }
5178 default:
5179 LOG(FATAL) << "Unexpected operation type " << type;
5180 UNREACHABLE();
5181 }
5182}
5183
Artem Serov02d37832016-10-25 15:25:33 +01005184void LocationsBuilderARMVIXL::HandleShift(HBinaryOperation* op) {
5185 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
5186
5187 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005188 new (GetGraph()->GetAllocator()) LocationSummary(op, LocationSummary::kNoCall);
Artem Serov02d37832016-10-25 15:25:33 +01005189
5190 switch (op->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005191 case DataType::Type::kInt32: {
Artem Serov02d37832016-10-25 15:25:33 +01005192 locations->SetInAt(0, Location::RequiresRegister());
5193 if (op->InputAt(1)->IsConstant()) {
5194 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
5195 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5196 } else {
5197 locations->SetInAt(1, Location::RequiresRegister());
5198 // Make the output overlap, as it will be used to hold the masked
5199 // second input.
5200 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
5201 }
5202 break;
5203 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005204 case DataType::Type::kInt64: {
Artem Serov02d37832016-10-25 15:25:33 +01005205 locations->SetInAt(0, Location::RequiresRegister());
5206 if (op->InputAt(1)->IsConstant()) {
5207 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
5208 // For simplicity, use kOutputOverlap even though we only require that low registers
5209 // don't clash with high registers which the register allocator currently guarantees.
5210 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
5211 } else {
5212 locations->SetInAt(1, Location::RequiresRegister());
5213 locations->AddTemp(Location::RequiresRegister());
5214 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
5215 }
5216 break;
5217 }
5218 default:
5219 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
5220 }
5221}
5222
5223void InstructionCodeGeneratorARMVIXL::HandleShift(HBinaryOperation* op) {
5224 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
5225
5226 LocationSummary* locations = op->GetLocations();
5227 Location out = locations->Out();
5228 Location first = locations->InAt(0);
5229 Location second = locations->InAt(1);
5230
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005231 DataType::Type type = op->GetResultType();
Artem Serov02d37832016-10-25 15:25:33 +01005232 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005233 case DataType::Type::kInt32: {
Artem Serov02d37832016-10-25 15:25:33 +01005234 vixl32::Register out_reg = OutputRegister(op);
5235 vixl32::Register first_reg = InputRegisterAt(op, 0);
5236 if (second.IsRegister()) {
5237 vixl32::Register second_reg = RegisterFrom(second);
5238 // ARM doesn't mask the shift count so we need to do it ourselves.
5239 __ And(out_reg, second_reg, kMaxIntShiftDistance);
5240 if (op->IsShl()) {
5241 __ Lsl(out_reg, first_reg, out_reg);
5242 } else if (op->IsShr()) {
5243 __ Asr(out_reg, first_reg, out_reg);
5244 } else {
5245 __ Lsr(out_reg, first_reg, out_reg);
5246 }
5247 } else {
Anton Kirilov644032c2016-12-06 17:51:43 +00005248 int32_t cst = Int32ConstantFrom(second);
Artem Serov02d37832016-10-25 15:25:33 +01005249 uint32_t shift_value = cst & kMaxIntShiftDistance;
5250 if (shift_value == 0) { // ARM does not support shifting with 0 immediate.
5251 __ Mov(out_reg, first_reg);
5252 } else if (op->IsShl()) {
5253 __ Lsl(out_reg, first_reg, shift_value);
5254 } else if (op->IsShr()) {
5255 __ Asr(out_reg, first_reg, shift_value);
5256 } else {
5257 __ Lsr(out_reg, first_reg, shift_value);
5258 }
5259 }
5260 break;
5261 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005262 case DataType::Type::kInt64: {
Artem Serov02d37832016-10-25 15:25:33 +01005263 vixl32::Register o_h = HighRegisterFrom(out);
5264 vixl32::Register o_l = LowRegisterFrom(out);
5265
5266 vixl32::Register high = HighRegisterFrom(first);
5267 vixl32::Register low = LowRegisterFrom(first);
5268
5269 if (second.IsRegister()) {
5270 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
5271
5272 vixl32::Register second_reg = RegisterFrom(second);
5273
5274 if (op->IsShl()) {
5275 __ And(o_l, second_reg, kMaxLongShiftDistance);
5276 // Shift the high part
5277 __ Lsl(o_h, high, o_l);
5278 // Shift the low part and `or` what overflew on the high part
Scott Wakelingb77051e2016-11-21 19:46:00 +00005279 __ Rsb(temp, o_l, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01005280 __ Lsr(temp, low, temp);
5281 __ Orr(o_h, o_h, temp);
5282 // If the shift is > 32 bits, override the high part
Scott Wakelingb77051e2016-11-21 19:46:00 +00005283 __ Subs(temp, o_l, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01005284 {
Artem Serov0fb37192016-12-06 18:13:40 +00005285 ExactAssemblyScope guard(GetVIXLAssembler(),
5286 2 * vixl32::kMaxInstructionSizeInBytes,
5287 CodeBufferCheckScope::kMaximumSize);
Artem Serov02d37832016-10-25 15:25:33 +01005288 __ it(pl);
5289 __ lsl(pl, o_h, low, temp);
5290 }
5291 // Shift the low part
5292 __ Lsl(o_l, low, o_l);
5293 } else if (op->IsShr()) {
5294 __ And(o_h, second_reg, kMaxLongShiftDistance);
5295 // Shift the low part
5296 __ Lsr(o_l, low, o_h);
5297 // Shift the high part and `or` what underflew on the low part
Scott Wakelingb77051e2016-11-21 19:46:00 +00005298 __ Rsb(temp, o_h, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01005299 __ Lsl(temp, high, temp);
5300 __ Orr(o_l, o_l, temp);
5301 // If the shift is > 32 bits, override the low part
Scott Wakelingb77051e2016-11-21 19:46:00 +00005302 __ Subs(temp, o_h, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01005303 {
Artem Serov0fb37192016-12-06 18:13:40 +00005304 ExactAssemblyScope guard(GetVIXLAssembler(),
5305 2 * vixl32::kMaxInstructionSizeInBytes,
5306 CodeBufferCheckScope::kMaximumSize);
Artem Serov02d37832016-10-25 15:25:33 +01005307 __ it(pl);
5308 __ asr(pl, o_l, high, temp);
5309 }
5310 // Shift the high part
5311 __ Asr(o_h, high, o_h);
5312 } else {
5313 __ And(o_h, second_reg, kMaxLongShiftDistance);
5314 // same as Shr except we use `Lsr`s and not `Asr`s
5315 __ Lsr(o_l, low, o_h);
Scott Wakelingb77051e2016-11-21 19:46:00 +00005316 __ Rsb(temp, o_h, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01005317 __ Lsl(temp, high, temp);
5318 __ Orr(o_l, o_l, temp);
Scott Wakelingb77051e2016-11-21 19:46:00 +00005319 __ Subs(temp, o_h, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01005320 {
Artem Serov0fb37192016-12-06 18:13:40 +00005321 ExactAssemblyScope guard(GetVIXLAssembler(),
5322 2 * vixl32::kMaxInstructionSizeInBytes,
5323 CodeBufferCheckScope::kMaximumSize);
Artem Serov02d37832016-10-25 15:25:33 +01005324 __ it(pl);
5325 __ lsr(pl, o_l, high, temp);
5326 }
5327 __ Lsr(o_h, high, o_h);
5328 }
5329 } else {
5330 // Register allocator doesn't create partial overlap.
5331 DCHECK(!o_l.Is(high));
5332 DCHECK(!o_h.Is(low));
Anton Kirilov644032c2016-12-06 17:51:43 +00005333 int32_t cst = Int32ConstantFrom(second);
Artem Serov02d37832016-10-25 15:25:33 +01005334 uint32_t shift_value = cst & kMaxLongShiftDistance;
5335 if (shift_value > 32) {
5336 if (op->IsShl()) {
5337 __ Lsl(o_h, low, shift_value - 32);
5338 __ Mov(o_l, 0);
5339 } else if (op->IsShr()) {
5340 __ Asr(o_l, high, shift_value - 32);
5341 __ Asr(o_h, high, 31);
5342 } else {
5343 __ Lsr(o_l, high, shift_value - 32);
5344 __ Mov(o_h, 0);
5345 }
5346 } else if (shift_value == 32) {
5347 if (op->IsShl()) {
5348 __ Mov(o_h, low);
5349 __ Mov(o_l, 0);
5350 } else if (op->IsShr()) {
5351 __ Mov(o_l, high);
5352 __ Asr(o_h, high, 31);
5353 } else {
5354 __ Mov(o_l, high);
5355 __ Mov(o_h, 0);
5356 }
5357 } else if (shift_value == 1) {
5358 if (op->IsShl()) {
5359 __ Lsls(o_l, low, 1);
5360 __ Adc(o_h, high, high);
5361 } else if (op->IsShr()) {
5362 __ Asrs(o_h, high, 1);
5363 __ Rrx(o_l, low);
5364 } else {
5365 __ Lsrs(o_h, high, 1);
5366 __ Rrx(o_l, low);
5367 }
5368 } else {
5369 DCHECK(2 <= shift_value && shift_value < 32) << shift_value;
5370 if (op->IsShl()) {
5371 __ Lsl(o_h, high, shift_value);
5372 __ Orr(o_h, o_h, Operand(low, ShiftType::LSR, 32 - shift_value));
5373 __ Lsl(o_l, low, shift_value);
5374 } else if (op->IsShr()) {
5375 __ Lsr(o_l, low, shift_value);
5376 __ Orr(o_l, o_l, Operand(high, ShiftType::LSL, 32 - shift_value));
5377 __ Asr(o_h, high, shift_value);
5378 } else {
5379 __ Lsr(o_l, low, shift_value);
5380 __ Orr(o_l, o_l, Operand(high, ShiftType::LSL, 32 - shift_value));
5381 __ Lsr(o_h, high, shift_value);
5382 }
5383 }
5384 }
5385 break;
5386 }
5387 default:
5388 LOG(FATAL) << "Unexpected operation type " << type;
5389 UNREACHABLE();
5390 }
5391}
5392
5393void LocationsBuilderARMVIXL::VisitShl(HShl* shl) {
5394 HandleShift(shl);
5395}
5396
5397void InstructionCodeGeneratorARMVIXL::VisitShl(HShl* shl) {
5398 HandleShift(shl);
5399}
5400
5401void LocationsBuilderARMVIXL::VisitShr(HShr* shr) {
5402 HandleShift(shr);
5403}
5404
5405void InstructionCodeGeneratorARMVIXL::VisitShr(HShr* shr) {
5406 HandleShift(shr);
5407}
5408
5409void LocationsBuilderARMVIXL::VisitUShr(HUShr* ushr) {
5410 HandleShift(ushr);
5411}
5412
5413void InstructionCodeGeneratorARMVIXL::VisitUShr(HUShr* ushr) {
5414 HandleShift(ushr);
5415}
5416
5417void LocationsBuilderARMVIXL::VisitNewInstance(HNewInstance* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005418 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5419 instruction, LocationSummary::kCallOnMainOnly);
Artem Serov02d37832016-10-25 15:25:33 +01005420 if (instruction->IsStringAlloc()) {
5421 locations->AddTemp(LocationFrom(kMethodRegister));
5422 } else {
5423 InvokeRuntimeCallingConventionARMVIXL calling_convention;
5424 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
Artem Serov02d37832016-10-25 15:25:33 +01005425 }
5426 locations->SetOut(LocationFrom(r0));
5427}
5428
5429void InstructionCodeGeneratorARMVIXL::VisitNewInstance(HNewInstance* instruction) {
5430 // Note: if heap poisoning is enabled, the entry point takes cares
5431 // of poisoning the reference.
5432 if (instruction->IsStringAlloc()) {
5433 // String is allocated through StringFactory. Call NewEmptyString entry point.
5434 vixl32::Register temp = RegisterFrom(instruction->GetLocations()->GetTemp(0));
5435 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize);
5436 GetAssembler()->LoadFromOffset(kLoadWord, temp, tr, QUICK_ENTRY_POINT(pNewEmptyString));
5437 GetAssembler()->LoadFromOffset(kLoadWord, lr, temp, code_offset.Int32Value());
Alexandre Rames374ddf32016-11-04 10:40:49 +00005438 // 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 +00005439 ExactAssemblyScope aas(GetVIXLAssembler(),
5440 vixl32::k16BitT32InstructionSizeInBytes,
5441 CodeBufferCheckScope::kExactSize);
Artem Serov02d37832016-10-25 15:25:33 +01005442 __ blx(lr);
5443 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
5444 } else {
5445 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00005446 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
Artem Serov02d37832016-10-25 15:25:33 +01005447 }
Roland Levillain5daa4952017-07-03 17:23:56 +01005448 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 10);
Artem Serov02d37832016-10-25 15:25:33 +01005449}
5450
5451void LocationsBuilderARMVIXL::VisitNewArray(HNewArray* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005452 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5453 instruction, LocationSummary::kCallOnMainOnly);
Artem Serov02d37832016-10-25 15:25:33 +01005454 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Artem Serov02d37832016-10-25 15:25:33 +01005455 locations->SetOut(LocationFrom(r0));
Nicolas Geoffray8c7c4f12017-01-26 10:13:11 +00005456 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5457 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
Artem Serov02d37832016-10-25 15:25:33 +01005458}
5459
5460void InstructionCodeGeneratorARMVIXL::VisitNewArray(HNewArray* instruction) {
Artem Serov02d37832016-10-25 15:25:33 +01005461 // Note: if heap poisoning is enabled, the entry point takes cares
5462 // of poisoning the reference.
Artem Serov7b3672e2017-02-03 17:30:34 +00005463 QuickEntrypointEnum entrypoint =
5464 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
5465 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005466 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Artem Serov7b3672e2017-02-03 17:30:34 +00005467 DCHECK(!codegen_->IsLeafMethod());
Roland Levillain5daa4952017-07-03 17:23:56 +01005468 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 11);
Artem Serov02d37832016-10-25 15:25:33 +01005469}
5470
5471void LocationsBuilderARMVIXL::VisitParameterValue(HParameterValue* instruction) {
5472 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005473 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Artem Serov02d37832016-10-25 15:25:33 +01005474 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
5475 if (location.IsStackSlot()) {
5476 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5477 } else if (location.IsDoubleStackSlot()) {
5478 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5479 }
5480 locations->SetOut(location);
5481}
5482
5483void InstructionCodeGeneratorARMVIXL::VisitParameterValue(
5484 HParameterValue* instruction ATTRIBUTE_UNUSED) {
5485 // Nothing to do, the parameter is already at its location.
5486}
5487
5488void LocationsBuilderARMVIXL::VisitCurrentMethod(HCurrentMethod* instruction) {
5489 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005490 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Artem Serov02d37832016-10-25 15:25:33 +01005491 locations->SetOut(LocationFrom(kMethodRegister));
5492}
5493
5494void InstructionCodeGeneratorARMVIXL::VisitCurrentMethod(
5495 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
5496 // Nothing to do, the method is already at its location.
5497}
5498
5499void LocationsBuilderARMVIXL::VisitNot(HNot* not_) {
5500 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005501 new (GetGraph()->GetAllocator()) LocationSummary(not_, LocationSummary::kNoCall);
Artem Serov02d37832016-10-25 15:25:33 +01005502 locations->SetInAt(0, Location::RequiresRegister());
5503 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5504}
5505
5506void InstructionCodeGeneratorARMVIXL::VisitNot(HNot* not_) {
5507 LocationSummary* locations = not_->GetLocations();
5508 Location out = locations->Out();
5509 Location in = locations->InAt(0);
5510 switch (not_->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005511 case DataType::Type::kInt32:
Artem Serov02d37832016-10-25 15:25:33 +01005512 __ Mvn(OutputRegister(not_), InputRegisterAt(not_, 0));
5513 break;
5514
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005515 case DataType::Type::kInt64:
Artem Serov02d37832016-10-25 15:25:33 +01005516 __ Mvn(LowRegisterFrom(out), LowRegisterFrom(in));
5517 __ Mvn(HighRegisterFrom(out), HighRegisterFrom(in));
5518 break;
5519
5520 default:
5521 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
5522 }
5523}
5524
Scott Wakelingc34dba72016-10-03 10:14:44 +01005525void LocationsBuilderARMVIXL::VisitBooleanNot(HBooleanNot* bool_not) {
5526 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005527 new (GetGraph()->GetAllocator()) LocationSummary(bool_not, LocationSummary::kNoCall);
Scott Wakelingc34dba72016-10-03 10:14:44 +01005528 locations->SetInAt(0, Location::RequiresRegister());
5529 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5530}
5531
5532void InstructionCodeGeneratorARMVIXL::VisitBooleanNot(HBooleanNot* bool_not) {
5533 __ Eor(OutputRegister(bool_not), InputRegister(bool_not), 1);
5534}
5535
Artem Serov02d37832016-10-25 15:25:33 +01005536void LocationsBuilderARMVIXL::VisitCompare(HCompare* compare) {
5537 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005538 new (GetGraph()->GetAllocator()) LocationSummary(compare, LocationSummary::kNoCall);
Artem Serov02d37832016-10-25 15:25:33 +01005539 switch (compare->InputAt(0)->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005540 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005541 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005542 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005543 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005544 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005545 case DataType::Type::kInt32:
5546 case DataType::Type::kInt64: {
Artem Serov02d37832016-10-25 15:25:33 +01005547 locations->SetInAt(0, Location::RequiresRegister());
5548 locations->SetInAt(1, Location::RequiresRegister());
5549 // Output overlaps because it is written before doing the low comparison.
5550 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
5551 break;
5552 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005553 case DataType::Type::kFloat32:
5554 case DataType::Type::kFloat64: {
Artem Serov02d37832016-10-25 15:25:33 +01005555 locations->SetInAt(0, Location::RequiresFpuRegister());
5556 locations->SetInAt(1, ArithmeticZeroOrFpuRegister(compare->InputAt(1)));
5557 locations->SetOut(Location::RequiresRegister());
5558 break;
5559 }
5560 default:
5561 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
5562 }
5563}
5564
5565void InstructionCodeGeneratorARMVIXL::VisitCompare(HCompare* compare) {
5566 LocationSummary* locations = compare->GetLocations();
5567 vixl32::Register out = OutputRegister(compare);
5568 Location left = locations->InAt(0);
5569 Location right = locations->InAt(1);
5570
5571 vixl32::Label less, greater, done;
Anton Kirilov6f644202017-02-27 18:29:45 +00005572 vixl32::Label* final_label = codegen_->GetFinalLabel(compare, &done);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005573 DataType::Type type = compare->InputAt(0)->GetType();
Vladimir Marko33bff252017-11-01 14:35:42 +00005574 vixl32::Condition less_cond = vixl32::Condition::None();
Artem Serov02d37832016-10-25 15:25:33 +01005575 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005576 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005577 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005578 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005579 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005580 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005581 case DataType::Type::kInt32: {
Artem Serov02d37832016-10-25 15:25:33 +01005582 // Emit move to `out` before the `Cmp`, as `Mov` might affect the status flags.
5583 __ Mov(out, 0);
5584 __ Cmp(RegisterFrom(left), RegisterFrom(right)); // Signed compare.
5585 less_cond = lt;
5586 break;
5587 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005588 case DataType::Type::kInt64: {
Artem Serov02d37832016-10-25 15:25:33 +01005589 __ Cmp(HighRegisterFrom(left), HighRegisterFrom(right)); // Signed compare.
Artem Serov517d9f62016-12-12 15:51:15 +00005590 __ B(lt, &less, /* far_target */ false);
5591 __ B(gt, &greater, /* far_target */ false);
Artem Serov02d37832016-10-25 15:25:33 +01005592 // Emit move to `out` before the last `Cmp`, as `Mov` might affect the status flags.
5593 __ Mov(out, 0);
5594 __ Cmp(LowRegisterFrom(left), LowRegisterFrom(right)); // Unsigned compare.
5595 less_cond = lo;
5596 break;
5597 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005598 case DataType::Type::kFloat32:
5599 case DataType::Type::kFloat64: {
Artem Serov02d37832016-10-25 15:25:33 +01005600 __ Mov(out, 0);
Donghui Bai426b49c2016-11-08 14:55:38 +08005601 GenerateVcmp(compare, codegen_);
Artem Serov02d37832016-10-25 15:25:33 +01005602 // To branch on the FP compare result we transfer FPSCR to APSR (encoded as PC in VMRS).
5603 __ Vmrs(RegisterOrAPSR_nzcv(kPcCode), FPSCR);
5604 less_cond = ARMFPCondition(kCondLT, compare->IsGtBias());
5605 break;
5606 }
5607 default:
5608 LOG(FATAL) << "Unexpected compare type " << type;
5609 UNREACHABLE();
5610 }
5611
Anton Kirilov6f644202017-02-27 18:29:45 +00005612 __ B(eq, final_label, /* far_target */ false);
Artem Serov517d9f62016-12-12 15:51:15 +00005613 __ B(less_cond, &less, /* far_target */ false);
Artem Serov02d37832016-10-25 15:25:33 +01005614
5615 __ Bind(&greater);
5616 __ Mov(out, 1);
Anton Kirilov6f644202017-02-27 18:29:45 +00005617 __ B(final_label);
Artem Serov02d37832016-10-25 15:25:33 +01005618
5619 __ Bind(&less);
5620 __ Mov(out, -1);
5621
Anton Kirilov6f644202017-02-27 18:29:45 +00005622 if (done.IsReferenced()) {
5623 __ Bind(&done);
5624 }
Artem Serov02d37832016-10-25 15:25:33 +01005625}
5626
5627void LocationsBuilderARMVIXL::VisitPhi(HPhi* instruction) {
5628 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005629 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Artem Serov02d37832016-10-25 15:25:33 +01005630 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
5631 locations->SetInAt(i, Location::Any());
5632 }
5633 locations->SetOut(Location::Any());
5634}
5635
5636void InstructionCodeGeneratorARMVIXL::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
5637 LOG(FATAL) << "Unreachable";
5638}
5639
5640void CodeGeneratorARMVIXL::GenerateMemoryBarrier(MemBarrierKind kind) {
5641 // TODO (ported from quick): revisit ARM barrier kinds.
5642 DmbOptions flavor = DmbOptions::ISH; // Quiet C++ warnings.
5643 switch (kind) {
5644 case MemBarrierKind::kAnyStore:
5645 case MemBarrierKind::kLoadAny:
5646 case MemBarrierKind::kAnyAny: {
5647 flavor = DmbOptions::ISH;
5648 break;
5649 }
5650 case MemBarrierKind::kStoreStore: {
5651 flavor = DmbOptions::ISHST;
5652 break;
5653 }
5654 default:
5655 LOG(FATAL) << "Unexpected memory barrier " << kind;
5656 }
5657 __ Dmb(flavor);
5658}
5659
5660void InstructionCodeGeneratorARMVIXL::GenerateWideAtomicLoad(vixl32::Register addr,
5661 uint32_t offset,
5662 vixl32::Register out_lo,
5663 vixl32::Register out_hi) {
5664 UseScratchRegisterScope temps(GetVIXLAssembler());
5665 if (offset != 0) {
5666 vixl32::Register temp = temps.Acquire();
5667 __ Add(temp, addr, offset);
5668 addr = temp;
5669 }
Scott Wakelingb77051e2016-11-21 19:46:00 +00005670 __ Ldrexd(out_lo, out_hi, MemOperand(addr));
Artem Serov02d37832016-10-25 15:25:33 +01005671}
5672
5673void InstructionCodeGeneratorARMVIXL::GenerateWideAtomicStore(vixl32::Register addr,
5674 uint32_t offset,
5675 vixl32::Register value_lo,
5676 vixl32::Register value_hi,
5677 vixl32::Register temp1,
5678 vixl32::Register temp2,
5679 HInstruction* instruction) {
5680 UseScratchRegisterScope temps(GetVIXLAssembler());
5681 vixl32::Label fail;
5682 if (offset != 0) {
5683 vixl32::Register temp = temps.Acquire();
5684 __ Add(temp, addr, offset);
5685 addr = temp;
5686 }
5687 __ Bind(&fail);
Alexandre Rames374ddf32016-11-04 10:40:49 +00005688 {
5689 // Ensure the pc position is recorded immediately after the `ldrexd` instruction.
Artem Serov0fb37192016-12-06 18:13:40 +00005690 ExactAssemblyScope aas(GetVIXLAssembler(),
5691 vixl32::kMaxInstructionSizeInBytes,
5692 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00005693 // We need a load followed by store. (The address used in a STREX instruction must
5694 // be the same as the address in the most recently executed LDREX instruction.)
5695 __ ldrexd(temp1, temp2, MemOperand(addr));
5696 codegen_->MaybeRecordImplicitNullCheck(instruction);
5697 }
Scott Wakelingb77051e2016-11-21 19:46:00 +00005698 __ Strexd(temp1, value_lo, value_hi, MemOperand(addr));
xueliang.zhongf51bc622016-11-04 09:23:32 +00005699 __ CompareAndBranchIfNonZero(temp1, &fail);
Artem Serov02d37832016-10-25 15:25:33 +01005700}
Artem Serov02109dd2016-09-23 17:17:54 +01005701
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005702void LocationsBuilderARMVIXL::HandleFieldSet(
5703 HInstruction* instruction, const FieldInfo& field_info) {
5704 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
5705
5706 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005707 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005708 locations->SetInAt(0, Location::RequiresRegister());
5709
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005710 DataType::Type field_type = field_info.GetFieldType();
5711 if (DataType::IsFloatingPointType(field_type)) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005712 locations->SetInAt(1, Location::RequiresFpuRegister());
5713 } else {
5714 locations->SetInAt(1, Location::RequiresRegister());
5715 }
5716
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005717 bool is_wide = field_type == DataType::Type::kInt64 || field_type == DataType::Type::kFloat64;
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005718 bool generate_volatile = field_info.IsVolatile()
5719 && is_wide
5720 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
5721 bool needs_write_barrier =
5722 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
5723 // Temporary registers for the write barrier.
5724 // TODO: consider renaming StoreNeedsWriteBarrier to StoreNeedsGCMark.
5725 if (needs_write_barrier) {
5726 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
5727 locations->AddTemp(Location::RequiresRegister());
5728 } else if (generate_volatile) {
5729 // ARM encoding have some additional constraints for ldrexd/strexd:
5730 // - registers need to be consecutive
5731 // - the first register should be even but not R14.
5732 // We don't test for ARM yet, and the assertion makes sure that we
5733 // revisit this if we ever enable ARM encoding.
5734 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
5735
5736 locations->AddTemp(Location::RequiresRegister());
5737 locations->AddTemp(Location::RequiresRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005738 if (field_type == DataType::Type::kFloat64) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005739 // For doubles we need two more registers to copy the value.
5740 locations->AddTemp(LocationFrom(r2));
5741 locations->AddTemp(LocationFrom(r3));
5742 }
5743 }
5744}
5745
5746void InstructionCodeGeneratorARMVIXL::HandleFieldSet(HInstruction* instruction,
5747 const FieldInfo& field_info,
5748 bool value_can_be_null) {
5749 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
5750
5751 LocationSummary* locations = instruction->GetLocations();
5752 vixl32::Register base = InputRegisterAt(instruction, 0);
5753 Location value = locations->InAt(1);
5754
5755 bool is_volatile = field_info.IsVolatile();
5756 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005757 DataType::Type field_type = field_info.GetFieldType();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005758 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
5759 bool needs_write_barrier =
5760 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
5761
5762 if (is_volatile) {
5763 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
5764 }
5765
5766 switch (field_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005767 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005768 case DataType::Type::kUint8:
5769 case DataType::Type::kInt8:
5770 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005771 case DataType::Type::kInt16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005772 case DataType::Type::kInt32: {
5773 StoreOperandType operand_type = GetStoreOperandType(field_type);
5774 GetAssembler()->StoreToOffset(operand_type, RegisterFrom(value), base, offset);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005775 break;
5776 }
5777
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005778 case DataType::Type::kReference: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005779 if (kPoisonHeapReferences && needs_write_barrier) {
5780 // Note that in the case where `value` is a null reference,
5781 // we do not enter this block, as a null reference does not
5782 // need poisoning.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005783 DCHECK_EQ(field_type, DataType::Type::kReference);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005784 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
5785 __ Mov(temp, RegisterFrom(value));
5786 GetAssembler()->PoisonHeapReference(temp);
5787 GetAssembler()->StoreToOffset(kStoreWord, temp, base, offset);
5788 } else {
5789 GetAssembler()->StoreToOffset(kStoreWord, RegisterFrom(value), base, offset);
5790 }
5791 break;
5792 }
5793
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005794 case DataType::Type::kInt64: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005795 if (is_volatile && !atomic_ldrd_strd) {
5796 GenerateWideAtomicStore(base,
5797 offset,
5798 LowRegisterFrom(value),
5799 HighRegisterFrom(value),
5800 RegisterFrom(locations->GetTemp(0)),
5801 RegisterFrom(locations->GetTemp(1)),
5802 instruction);
5803 } else {
5804 GetAssembler()->StoreToOffset(kStoreWordPair, LowRegisterFrom(value), base, offset);
5805 codegen_->MaybeRecordImplicitNullCheck(instruction);
5806 }
5807 break;
5808 }
5809
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005810 case DataType::Type::kFloat32: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005811 GetAssembler()->StoreSToOffset(SRegisterFrom(value), base, offset);
5812 break;
5813 }
5814
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005815 case DataType::Type::kFloat64: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01005816 vixl32::DRegister value_reg = DRegisterFrom(value);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005817 if (is_volatile && !atomic_ldrd_strd) {
5818 vixl32::Register value_reg_lo = RegisterFrom(locations->GetTemp(0));
5819 vixl32::Register value_reg_hi = RegisterFrom(locations->GetTemp(1));
5820
5821 __ Vmov(value_reg_lo, value_reg_hi, value_reg);
5822
5823 GenerateWideAtomicStore(base,
5824 offset,
5825 value_reg_lo,
5826 value_reg_hi,
5827 RegisterFrom(locations->GetTemp(2)),
5828 RegisterFrom(locations->GetTemp(3)),
5829 instruction);
5830 } else {
5831 GetAssembler()->StoreDToOffset(value_reg, base, offset);
5832 codegen_->MaybeRecordImplicitNullCheck(instruction);
5833 }
5834 break;
5835 }
5836
Aart Bik66c158e2018-01-31 12:55:04 -08005837 case DataType::Type::kUint32:
5838 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005839 case DataType::Type::kVoid:
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005840 LOG(FATAL) << "Unreachable type " << field_type;
5841 UNREACHABLE();
5842 }
5843
5844 // Longs and doubles are handled in the switch.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005845 if (field_type != DataType::Type::kInt64 && field_type != DataType::Type::kFloat64) {
Alexandre Rames374ddf32016-11-04 10:40:49 +00005846 // TODO(VIXL): Here and for other calls to `MaybeRecordImplicitNullCheck` in this method, we
5847 // should use a scope and the assembler to emit the store instruction to guarantee that we
5848 // record the pc at the correct position. But the `Assembler` does not automatically handle
5849 // unencodable offsets. Practically, everything is fine because the helper and VIXL, at the time
5850 // of writing, do generate the store instruction last.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005851 codegen_->MaybeRecordImplicitNullCheck(instruction);
5852 }
5853
5854 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
5855 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
5856 vixl32::Register card = RegisterFrom(locations->GetTemp(1));
5857 codegen_->MarkGCCard(temp, card, base, RegisterFrom(value), value_can_be_null);
5858 }
5859
5860 if (is_volatile) {
5861 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
5862 }
5863}
5864
Artem Serov02d37832016-10-25 15:25:33 +01005865void LocationsBuilderARMVIXL::HandleFieldGet(HInstruction* instruction,
5866 const FieldInfo& field_info) {
5867 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
5868
5869 bool object_field_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005870 kEmitCompilerReadBarrier && (field_info.GetFieldType() == DataType::Type::kReference);
Artem Serov02d37832016-10-25 15:25:33 +01005871 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005872 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
5873 object_field_get_with_read_barrier
5874 ? LocationSummary::kCallOnSlowPath
5875 : LocationSummary::kNoCall);
Artem Serov02d37832016-10-25 15:25:33 +01005876 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
5877 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
5878 }
5879 locations->SetInAt(0, Location::RequiresRegister());
5880
5881 bool volatile_for_double = field_info.IsVolatile()
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005882 && (field_info.GetFieldType() == DataType::Type::kFloat64)
Artem Serov02d37832016-10-25 15:25:33 +01005883 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
5884 // The output overlaps in case of volatile long: we don't want the
5885 // code generated by GenerateWideAtomicLoad to overwrite the
5886 // object's location. Likewise, in the case of an object field get
5887 // with read barriers enabled, we do not want the load to overwrite
5888 // the object's location, as we need it to emit the read barrier.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005889 bool overlap =
5890 (field_info.IsVolatile() && (field_info.GetFieldType() == DataType::Type::kInt64)) ||
Artem Serov02d37832016-10-25 15:25:33 +01005891 object_field_get_with_read_barrier;
5892
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005893 if (DataType::IsFloatingPointType(instruction->GetType())) {
Artem Serov02d37832016-10-25 15:25:33 +01005894 locations->SetOut(Location::RequiresFpuRegister());
5895 } else {
5896 locations->SetOut(Location::RequiresRegister(),
5897 (overlap ? Location::kOutputOverlap : Location::kNoOutputOverlap));
5898 }
5899 if (volatile_for_double) {
5900 // ARM encoding have some additional constraints for ldrexd/strexd:
5901 // - registers need to be consecutive
5902 // - the first register should be even but not R14.
5903 // We don't test for ARM yet, and the assertion makes sure that we
5904 // revisit this if we ever enable ARM encoding.
5905 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
5906 locations->AddTemp(Location::RequiresRegister());
5907 locations->AddTemp(Location::RequiresRegister());
5908 } else if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
5909 // We need a temporary register for the read barrier marking slow
Artem Serovc5fcb442016-12-02 19:19:58 +00005910 // path in CodeGeneratorARMVIXL::GenerateFieldLoadWithBakerReadBarrier.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01005911 if (kBakerReadBarrierLinkTimeThunksEnableForFields &&
5912 !Runtime::Current()->UseJitCompilation()) {
5913 // If link-time thunks for the Baker read barrier are enabled, for AOT
5914 // loads we need a temporary only if the offset is too big.
5915 if (field_info.GetFieldOffset().Uint32Value() >= kReferenceLoadMinFarOffset) {
5916 locations->AddTemp(Location::RequiresRegister());
5917 }
5918 // And we always need the reserved entrypoint register.
5919 locations->AddTemp(Location::RegisterLocation(kBakerCcEntrypointRegister.GetCode()));
5920 } else {
5921 locations->AddTemp(Location::RequiresRegister());
5922 }
Artem Serov02d37832016-10-25 15:25:33 +01005923 }
5924}
5925
5926Location LocationsBuilderARMVIXL::ArithmeticZeroOrFpuRegister(HInstruction* input) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005927 DCHECK(DataType::IsFloatingPointType(input->GetType())) << input->GetType();
Artem Serov02d37832016-10-25 15:25:33 +01005928 if ((input->IsFloatConstant() && (input->AsFloatConstant()->IsArithmeticZero())) ||
5929 (input->IsDoubleConstant() && (input->AsDoubleConstant()->IsArithmeticZero()))) {
5930 return Location::ConstantLocation(input->AsConstant());
5931 } else {
5932 return Location::RequiresFpuRegister();
5933 }
5934}
5935
Artem Serov02109dd2016-09-23 17:17:54 +01005936Location LocationsBuilderARMVIXL::ArmEncodableConstantOrRegister(HInstruction* constant,
5937 Opcode opcode) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005938 DCHECK(!DataType::IsFloatingPointType(constant->GetType()));
Artem Serov02109dd2016-09-23 17:17:54 +01005939 if (constant->IsConstant() &&
5940 CanEncodeConstantAsImmediate(constant->AsConstant(), opcode)) {
5941 return Location::ConstantLocation(constant->AsConstant());
5942 }
5943 return Location::RequiresRegister();
5944}
5945
Vladimir Markof0a6a1d2018-01-08 14:23:56 +00005946static bool CanEncode32BitConstantAsImmediate(
5947 CodeGeneratorARMVIXL* codegen,
5948 uint32_t value,
5949 Opcode opcode,
5950 vixl32::FlagsUpdate flags_update = vixl32::FlagsUpdate::DontCare) {
5951 ArmVIXLAssembler* assembler = codegen->GetAssembler();
5952 if (assembler->ShifterOperandCanHold(opcode, value, flags_update)) {
Artem Serov02109dd2016-09-23 17:17:54 +01005953 return true;
5954 }
5955 Opcode neg_opcode = kNoOperand;
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00005956 uint32_t neg_value = 0;
Artem Serov02109dd2016-09-23 17:17:54 +01005957 switch (opcode) {
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00005958 case AND: neg_opcode = BIC; neg_value = ~value; break;
5959 case ORR: neg_opcode = ORN; neg_value = ~value; break;
5960 case ADD: neg_opcode = SUB; neg_value = -value; break;
5961 case ADC: neg_opcode = SBC; neg_value = ~value; break;
5962 case SUB: neg_opcode = ADD; neg_value = -value; break;
5963 case SBC: neg_opcode = ADC; neg_value = ~value; break;
5964 case MOV: neg_opcode = MVN; neg_value = ~value; break;
Artem Serov02109dd2016-09-23 17:17:54 +01005965 default:
5966 return false;
5967 }
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00005968
Vladimir Markof0a6a1d2018-01-08 14:23:56 +00005969 if (assembler->ShifterOperandCanHold(neg_opcode, neg_value, flags_update)) {
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00005970 return true;
5971 }
5972
5973 return opcode == AND && IsPowerOfTwo(value + 1);
Artem Serov02109dd2016-09-23 17:17:54 +01005974}
5975
Vladimir Markof0a6a1d2018-01-08 14:23:56 +00005976bool LocationsBuilderARMVIXL::CanEncodeConstantAsImmediate(HConstant* input_cst, Opcode opcode) {
5977 uint64_t value = static_cast<uint64_t>(Int64FromConstant(input_cst));
5978 if (DataType::Is64BitType(input_cst->GetType())) {
5979 Opcode high_opcode = opcode;
5980 vixl32::FlagsUpdate low_flags_update = vixl32::FlagsUpdate::DontCare;
5981 switch (opcode) {
5982 case SUB:
5983 // Flip the operation to an ADD.
5984 value = -value;
5985 opcode = ADD;
5986 FALLTHROUGH_INTENDED;
5987 case ADD:
5988 if (Low32Bits(value) == 0u) {
5989 return CanEncode32BitConstantAsImmediate(codegen_, High32Bits(value), opcode);
5990 }
5991 high_opcode = ADC;
5992 low_flags_update = vixl32::FlagsUpdate::SetFlags;
5993 break;
5994 default:
5995 break;
5996 }
5997 return CanEncode32BitConstantAsImmediate(codegen_, High32Bits(value), high_opcode) &&
5998 CanEncode32BitConstantAsImmediate(codegen_, Low32Bits(value), opcode, low_flags_update);
5999 } else {
6000 return CanEncode32BitConstantAsImmediate(codegen_, Low32Bits(value), opcode);
6001 }
6002}
6003
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006004void InstructionCodeGeneratorARMVIXL::HandleFieldGet(HInstruction* instruction,
6005 const FieldInfo& field_info) {
6006 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
6007
6008 LocationSummary* locations = instruction->GetLocations();
6009 vixl32::Register base = InputRegisterAt(instruction, 0);
6010 Location out = locations->Out();
6011 bool is_volatile = field_info.IsVolatile();
6012 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Vladimir Marko61b92282017-10-11 13:23:17 +01006013 DCHECK_EQ(DataType::Size(field_info.GetFieldType()), DataType::Size(instruction->GetType()));
6014 DataType::Type load_type = instruction->GetType();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006015 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
6016
Vladimir Marko61b92282017-10-11 13:23:17 +01006017 switch (load_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006018 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006019 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006020 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006021 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006022 case DataType::Type::kInt16:
6023 case DataType::Type::kInt32: {
Vladimir Marko61b92282017-10-11 13:23:17 +01006024 LoadOperandType operand_type = GetLoadOperandType(load_type);
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006025 GetAssembler()->LoadFromOffset(operand_type, RegisterFrom(out), base, offset);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006026 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006027 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006028
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006029 case DataType::Type::kReference: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006030 // /* HeapReference<Object> */ out = *(base + offset)
6031 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006032 Location temp_loc = locations->GetTemp(0);
6033 // Note that a potential implicit null check is handled in this
6034 // CodeGeneratorARMVIXL::GenerateFieldLoadWithBakerReadBarrier call.
6035 codegen_->GenerateFieldLoadWithBakerReadBarrier(
6036 instruction, out, base, offset, temp_loc, /* needs_null_check */ true);
6037 if (is_volatile) {
6038 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6039 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006040 } else {
6041 GetAssembler()->LoadFromOffset(kLoadWord, RegisterFrom(out), base, offset);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006042 codegen_->MaybeRecordImplicitNullCheck(instruction);
6043 if (is_volatile) {
6044 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6045 }
6046 // If read barriers are enabled, emit read barriers other than
6047 // Baker's using a slow path (and also unpoison the loaded
6048 // reference, if heap poisoning is enabled).
6049 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, locations->InAt(0), offset);
6050 }
6051 break;
6052 }
6053
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006054 case DataType::Type::kInt64:
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006055 if (is_volatile && !atomic_ldrd_strd) {
6056 GenerateWideAtomicLoad(base, offset, LowRegisterFrom(out), HighRegisterFrom(out));
6057 } else {
6058 GetAssembler()->LoadFromOffset(kLoadWordPair, LowRegisterFrom(out), base, offset);
6059 }
6060 break;
6061
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006062 case DataType::Type::kFloat32:
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006063 GetAssembler()->LoadSFromOffset(SRegisterFrom(out), base, offset);
6064 break;
6065
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006066 case DataType::Type::kFloat64: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006067 vixl32::DRegister out_dreg = DRegisterFrom(out);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006068 if (is_volatile && !atomic_ldrd_strd) {
6069 vixl32::Register lo = RegisterFrom(locations->GetTemp(0));
6070 vixl32::Register hi = RegisterFrom(locations->GetTemp(1));
6071 GenerateWideAtomicLoad(base, offset, lo, hi);
6072 // TODO(VIXL): Do we need to be immediately after the ldrexd instruction? If so we need a
6073 // scope.
6074 codegen_->MaybeRecordImplicitNullCheck(instruction);
6075 __ Vmov(out_dreg, lo, hi);
6076 } else {
6077 GetAssembler()->LoadDFromOffset(out_dreg, base, offset);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006078 codegen_->MaybeRecordImplicitNullCheck(instruction);
6079 }
6080 break;
6081 }
6082
Aart Bik66c158e2018-01-31 12:55:04 -08006083 case DataType::Type::kUint32:
6084 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006085 case DataType::Type::kVoid:
Vladimir Marko61b92282017-10-11 13:23:17 +01006086 LOG(FATAL) << "Unreachable type " << load_type;
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006087 UNREACHABLE();
6088 }
6089
Vladimir Marko61b92282017-10-11 13:23:17 +01006090 if (load_type == DataType::Type::kReference || load_type == DataType::Type::kFloat64) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006091 // Potential implicit null checks, in the case of reference or
6092 // double fields, are handled in the previous switch statement.
6093 } else {
6094 // Address cases other than reference and double that may require an implicit null check.
Alexandre Rames374ddf32016-11-04 10:40:49 +00006095 // TODO(VIXL): Here and for other calls to `MaybeRecordImplicitNullCheck` in this method, we
6096 // should use a scope and the assembler to emit the load instruction to guarantee that we
6097 // record the pc at the correct position. But the `Assembler` does not automatically handle
6098 // unencodable offsets. Practically, everything is fine because the helper and VIXL, at the time
6099 // of writing, do generate the store instruction last.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006100 codegen_->MaybeRecordImplicitNullCheck(instruction);
6101 }
6102
6103 if (is_volatile) {
Vladimir Marko61b92282017-10-11 13:23:17 +01006104 if (load_type == DataType::Type::kReference) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006105 // Memory barriers, in the case of references, are also handled
6106 // in the previous switch statement.
6107 } else {
6108 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6109 }
6110 }
6111}
6112
6113void LocationsBuilderARMVIXL::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
6114 HandleFieldSet(instruction, instruction->GetFieldInfo());
6115}
6116
6117void InstructionCodeGeneratorARMVIXL::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
6118 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
6119}
6120
6121void LocationsBuilderARMVIXL::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
6122 HandleFieldGet(instruction, instruction->GetFieldInfo());
6123}
6124
6125void InstructionCodeGeneratorARMVIXL::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
6126 HandleFieldGet(instruction, instruction->GetFieldInfo());
6127}
6128
6129void LocationsBuilderARMVIXL::VisitStaticFieldGet(HStaticFieldGet* instruction) {
6130 HandleFieldGet(instruction, instruction->GetFieldInfo());
6131}
6132
6133void InstructionCodeGeneratorARMVIXL::VisitStaticFieldGet(HStaticFieldGet* instruction) {
6134 HandleFieldGet(instruction, instruction->GetFieldInfo());
6135}
6136
Scott Wakelingc34dba72016-10-03 10:14:44 +01006137void LocationsBuilderARMVIXL::VisitStaticFieldSet(HStaticFieldSet* instruction) {
6138 HandleFieldSet(instruction, instruction->GetFieldInfo());
6139}
6140
6141void InstructionCodeGeneratorARMVIXL::VisitStaticFieldSet(HStaticFieldSet* instruction) {
6142 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
6143}
6144
Artem Serovcfbe9132016-10-14 15:58:56 +01006145void LocationsBuilderARMVIXL::VisitUnresolvedInstanceFieldGet(
6146 HUnresolvedInstanceFieldGet* instruction) {
6147 FieldAccessCallingConventionARMVIXL calling_convention;
6148 codegen_->CreateUnresolvedFieldLocationSummary(
6149 instruction, instruction->GetFieldType(), calling_convention);
6150}
6151
6152void InstructionCodeGeneratorARMVIXL::VisitUnresolvedInstanceFieldGet(
6153 HUnresolvedInstanceFieldGet* instruction) {
6154 FieldAccessCallingConventionARMVIXL calling_convention;
6155 codegen_->GenerateUnresolvedFieldAccess(instruction,
6156 instruction->GetFieldType(),
6157 instruction->GetFieldIndex(),
6158 instruction->GetDexPc(),
6159 calling_convention);
6160}
6161
6162void LocationsBuilderARMVIXL::VisitUnresolvedInstanceFieldSet(
6163 HUnresolvedInstanceFieldSet* instruction) {
6164 FieldAccessCallingConventionARMVIXL calling_convention;
6165 codegen_->CreateUnresolvedFieldLocationSummary(
6166 instruction, instruction->GetFieldType(), calling_convention);
6167}
6168
6169void InstructionCodeGeneratorARMVIXL::VisitUnresolvedInstanceFieldSet(
6170 HUnresolvedInstanceFieldSet* instruction) {
6171 FieldAccessCallingConventionARMVIXL calling_convention;
6172 codegen_->GenerateUnresolvedFieldAccess(instruction,
6173 instruction->GetFieldType(),
6174 instruction->GetFieldIndex(),
6175 instruction->GetDexPc(),
6176 calling_convention);
6177}
6178
6179void LocationsBuilderARMVIXL::VisitUnresolvedStaticFieldGet(
6180 HUnresolvedStaticFieldGet* instruction) {
6181 FieldAccessCallingConventionARMVIXL calling_convention;
6182 codegen_->CreateUnresolvedFieldLocationSummary(
6183 instruction, instruction->GetFieldType(), calling_convention);
6184}
6185
6186void InstructionCodeGeneratorARMVIXL::VisitUnresolvedStaticFieldGet(
6187 HUnresolvedStaticFieldGet* instruction) {
6188 FieldAccessCallingConventionARMVIXL calling_convention;
6189 codegen_->GenerateUnresolvedFieldAccess(instruction,
6190 instruction->GetFieldType(),
6191 instruction->GetFieldIndex(),
6192 instruction->GetDexPc(),
6193 calling_convention);
6194}
6195
6196void LocationsBuilderARMVIXL::VisitUnresolvedStaticFieldSet(
6197 HUnresolvedStaticFieldSet* instruction) {
6198 FieldAccessCallingConventionARMVIXL calling_convention;
6199 codegen_->CreateUnresolvedFieldLocationSummary(
6200 instruction, instruction->GetFieldType(), calling_convention);
6201}
6202
6203void InstructionCodeGeneratorARMVIXL::VisitUnresolvedStaticFieldSet(
6204 HUnresolvedStaticFieldSet* instruction) {
6205 FieldAccessCallingConventionARMVIXL calling_convention;
6206 codegen_->GenerateUnresolvedFieldAccess(instruction,
6207 instruction->GetFieldType(),
6208 instruction->GetFieldIndex(),
6209 instruction->GetDexPc(),
6210 calling_convention);
6211}
6212
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006213void LocationsBuilderARMVIXL::VisitNullCheck(HNullCheck* instruction) {
Artem Serov657022c2016-11-23 14:19:38 +00006214 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006215 locations->SetInAt(0, Location::RequiresRegister());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006216}
6217
6218void CodeGeneratorARMVIXL::GenerateImplicitNullCheck(HNullCheck* instruction) {
6219 if (CanMoveNullCheckToUser(instruction)) {
6220 return;
6221 }
6222
6223 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames374ddf32016-11-04 10:40:49 +00006224 // Ensure the pc position is recorded immediately after the `ldr` instruction.
Artem Serov0fb37192016-12-06 18:13:40 +00006225 ExactAssemblyScope aas(GetVIXLAssembler(),
6226 vixl32::kMaxInstructionSizeInBytes,
6227 CodeBufferCheckScope::kMaximumSize);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006228 __ ldr(temps.Acquire(), MemOperand(InputRegisterAt(instruction, 0)));
6229 RecordPcInfo(instruction, instruction->GetDexPc());
6230}
6231
6232void CodeGeneratorARMVIXL::GenerateExplicitNullCheck(HNullCheck* instruction) {
6233 NullCheckSlowPathARMVIXL* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006234 new (GetScopedAllocator()) NullCheckSlowPathARMVIXL(instruction);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006235 AddSlowPath(slow_path);
xueliang.zhongf51bc622016-11-04 09:23:32 +00006236 __ CompareAndBranchIfZero(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006237}
6238
6239void InstructionCodeGeneratorARMVIXL::VisitNullCheck(HNullCheck* instruction) {
6240 codegen_->GenerateNullCheck(instruction);
6241}
6242
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006243void CodeGeneratorARMVIXL::LoadFromShiftedRegOffset(DataType::Type type,
Scott Wakelingc34dba72016-10-03 10:14:44 +01006244 Location out_loc,
6245 vixl32::Register base,
6246 vixl32::Register reg_index,
6247 vixl32::Condition cond) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006248 uint32_t shift_count = DataType::SizeShift(type);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006249 MemOperand mem_address(base, reg_index, vixl32::LSL, shift_count);
6250
6251 switch (type) {
Vladimir Marko61b92282017-10-11 13:23:17 +01006252 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006253 case DataType::Type::kUint8:
Vladimir Marko61b92282017-10-11 13:23:17 +01006254 __ Ldrb(cond, RegisterFrom(out_loc), mem_address);
6255 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006256 case DataType::Type::kInt8:
Scott Wakelingc34dba72016-10-03 10:14:44 +01006257 __ Ldrsb(cond, RegisterFrom(out_loc), mem_address);
6258 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006259 case DataType::Type::kUint16:
Scott Wakelingc34dba72016-10-03 10:14:44 +01006260 __ Ldrh(cond, RegisterFrom(out_loc), mem_address);
6261 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006262 case DataType::Type::kInt16:
6263 __ Ldrsh(cond, RegisterFrom(out_loc), mem_address);
6264 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006265 case DataType::Type::kReference:
6266 case DataType::Type::kInt32:
Scott Wakelingc34dba72016-10-03 10:14:44 +01006267 __ Ldr(cond, RegisterFrom(out_loc), mem_address);
6268 break;
6269 // T32 doesn't support LoadFromShiftedRegOffset mem address mode for these types.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006270 case DataType::Type::kInt64:
6271 case DataType::Type::kFloat32:
6272 case DataType::Type::kFloat64:
Scott Wakelingc34dba72016-10-03 10:14:44 +01006273 default:
6274 LOG(FATAL) << "Unreachable type " << type;
6275 UNREACHABLE();
6276 }
6277}
6278
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006279void CodeGeneratorARMVIXL::StoreToShiftedRegOffset(DataType::Type type,
Scott Wakelingc34dba72016-10-03 10:14:44 +01006280 Location loc,
6281 vixl32::Register base,
6282 vixl32::Register reg_index,
6283 vixl32::Condition cond) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006284 uint32_t shift_count = DataType::SizeShift(type);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006285 MemOperand mem_address(base, reg_index, vixl32::LSL, shift_count);
6286
6287 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006288 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006289 case DataType::Type::kUint8:
6290 case DataType::Type::kInt8:
Scott Wakelingc34dba72016-10-03 10:14:44 +01006291 __ Strb(cond, RegisterFrom(loc), mem_address);
6292 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006293 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006294 case DataType::Type::kInt16:
Scott Wakelingc34dba72016-10-03 10:14:44 +01006295 __ Strh(cond, RegisterFrom(loc), mem_address);
6296 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006297 case DataType::Type::kReference:
6298 case DataType::Type::kInt32:
Scott Wakelingc34dba72016-10-03 10:14:44 +01006299 __ Str(cond, RegisterFrom(loc), mem_address);
6300 break;
6301 // T32 doesn't support StoreToShiftedRegOffset mem address mode for these types.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006302 case DataType::Type::kInt64:
6303 case DataType::Type::kFloat32:
6304 case DataType::Type::kFloat64:
Scott Wakelingc34dba72016-10-03 10:14:44 +01006305 default:
6306 LOG(FATAL) << "Unreachable type " << type;
6307 UNREACHABLE();
6308 }
6309}
6310
6311void LocationsBuilderARMVIXL::VisitArrayGet(HArrayGet* instruction) {
6312 bool object_array_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006313 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006314 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006315 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
6316 object_array_get_with_read_barrier
6317 ? LocationSummary::kCallOnSlowPath
6318 : LocationSummary::kNoCall);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006319 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006320 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Scott Wakelingc34dba72016-10-03 10:14:44 +01006321 }
6322 locations->SetInAt(0, Location::RequiresRegister());
6323 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006324 if (DataType::IsFloatingPointType(instruction->GetType())) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006325 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
6326 } else {
6327 // The output overlaps in the case of an object array get with
6328 // read barriers enabled: we do not want the move to overwrite the
6329 // array's location, as we need it to emit the read barrier.
6330 locations->SetOut(
6331 Location::RequiresRegister(),
6332 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
6333 }
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01006334 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
6335 // We need a temporary register for the read barrier marking slow
6336 // path in CodeGeneratorARMVIXL::GenerateArrayLoadWithBakerReadBarrier.
6337 if (kBakerReadBarrierLinkTimeThunksEnableForFields &&
6338 !Runtime::Current()->UseJitCompilation() &&
6339 instruction->GetIndex()->IsConstant()) {
6340 // Array loads with constant index are treated as field loads.
6341 // If link-time thunks for the Baker read barrier are enabled, for AOT
6342 // constant index loads we need a temporary only if the offset is too big.
6343 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
6344 uint32_t index = instruction->GetIndex()->AsIntConstant()->GetValue();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006345 offset += index << DataType::SizeShift(DataType::Type::kReference);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01006346 if (offset >= kReferenceLoadMinFarOffset) {
6347 locations->AddTemp(Location::RequiresRegister());
6348 }
6349 // And we always need the reserved entrypoint register.
6350 locations->AddTemp(Location::RegisterLocation(kBakerCcEntrypointRegister.GetCode()));
6351 } else if (kBakerReadBarrierLinkTimeThunksEnableForArrays &&
6352 !Runtime::Current()->UseJitCompilation() &&
6353 !instruction->GetIndex()->IsConstant()) {
6354 // We need a non-scratch temporary for the array data pointer.
6355 locations->AddTemp(Location::RequiresRegister());
6356 // And we always need the reserved entrypoint register.
6357 locations->AddTemp(Location::RegisterLocation(kBakerCcEntrypointRegister.GetCode()));
6358 } else {
6359 locations->AddTemp(Location::RequiresRegister());
6360 }
6361 } else if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
6362 // Also need a temporary for String compression feature.
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006363 locations->AddTemp(Location::RequiresRegister());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006364 }
6365}
6366
6367void InstructionCodeGeneratorARMVIXL::VisitArrayGet(HArrayGet* instruction) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006368 LocationSummary* locations = instruction->GetLocations();
6369 Location obj_loc = locations->InAt(0);
6370 vixl32::Register obj = InputRegisterAt(instruction, 0);
6371 Location index = locations->InAt(1);
6372 Location out_loc = locations->Out();
6373 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006374 DataType::Type type = instruction->GetType();
Scott Wakelingc34dba72016-10-03 10:14:44 +01006375 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
6376 instruction->IsStringCharAt();
6377 HInstruction* array_instr = instruction->GetArray();
6378 bool has_intermediate_address = array_instr->IsIntermediateAddress();
Scott Wakelingc34dba72016-10-03 10:14:44 +01006379
6380 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006381 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006382 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006383 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006384 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006385 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006386 case DataType::Type::kInt32: {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006387 vixl32::Register length;
6388 if (maybe_compressed_char_at) {
6389 length = RegisterFrom(locations->GetTemp(0));
6390 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
6391 GetAssembler()->LoadFromOffset(kLoadWord, length, obj, count_offset);
6392 codegen_->MaybeRecordImplicitNullCheck(instruction);
6393 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006394 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006395 int32_t const_index = Int32ConstantFrom(index);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006396 if (maybe_compressed_char_at) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006397 vixl32::Label uncompressed_load, done;
Anton Kirilov6f644202017-02-27 18:29:45 +00006398 vixl32::Label* final_label = codegen_->GetFinalLabel(instruction, &done);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006399 __ Lsrs(length, length, 1u); // LSRS has a 16-bit encoding, TST (immediate) does not.
6400 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
6401 "Expecting 0=compressed, 1=uncompressed");
Artem Serov517d9f62016-12-12 15:51:15 +00006402 __ B(cs, &uncompressed_load, /* far_target */ false);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006403 GetAssembler()->LoadFromOffset(kLoadUnsignedByte,
6404 RegisterFrom(out_loc),
6405 obj,
6406 data_offset + const_index);
Anton Kirilov6f644202017-02-27 18:29:45 +00006407 __ B(final_label);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006408 __ Bind(&uncompressed_load);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006409 GetAssembler()->LoadFromOffset(GetLoadOperandType(DataType::Type::kUint16),
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006410 RegisterFrom(out_loc),
6411 obj,
6412 data_offset + (const_index << 1));
Anton Kirilov6f644202017-02-27 18:29:45 +00006413 if (done.IsReferenced()) {
6414 __ Bind(&done);
6415 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006416 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006417 uint32_t full_offset = data_offset + (const_index << DataType::SizeShift(type));
Scott Wakelingc34dba72016-10-03 10:14:44 +01006418
6419 LoadOperandType load_type = GetLoadOperandType(type);
6420 GetAssembler()->LoadFromOffset(load_type, RegisterFrom(out_loc), obj, full_offset);
6421 }
6422 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006423 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006424 vixl32::Register temp = temps.Acquire();
6425
6426 if (has_intermediate_address) {
Artem Serov2bbc9532016-10-21 11:51:50 +01006427 // We do not need to compute the intermediate address from the array: the
6428 // input instruction has done it already. See the comment in
6429 // `TryExtractArrayAccessAddress()`.
6430 if (kIsDebugBuild) {
6431 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
Anton Kirilov644032c2016-12-06 17:51:43 +00006432 DCHECK_EQ(Uint64ConstantFrom(tmp->GetOffset()), data_offset);
Artem Serov2bbc9532016-10-21 11:51:50 +01006433 }
6434 temp = obj;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006435 } else {
6436 __ Add(temp, obj, data_offset);
6437 }
6438 if (maybe_compressed_char_at) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006439 vixl32::Label uncompressed_load, done;
Anton Kirilov6f644202017-02-27 18:29:45 +00006440 vixl32::Label* final_label = codegen_->GetFinalLabel(instruction, &done);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006441 __ Lsrs(length, length, 1u); // LSRS has a 16-bit encoding, TST (immediate) does not.
6442 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
6443 "Expecting 0=compressed, 1=uncompressed");
Artem Serov517d9f62016-12-12 15:51:15 +00006444 __ B(cs, &uncompressed_load, /* far_target */ false);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006445 __ Ldrb(RegisterFrom(out_loc), MemOperand(temp, RegisterFrom(index), vixl32::LSL, 0));
Anton Kirilov6f644202017-02-27 18:29:45 +00006446 __ B(final_label);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006447 __ Bind(&uncompressed_load);
6448 __ Ldrh(RegisterFrom(out_loc), MemOperand(temp, RegisterFrom(index), vixl32::LSL, 1));
Anton Kirilov6f644202017-02-27 18:29:45 +00006449 if (done.IsReferenced()) {
6450 __ Bind(&done);
6451 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006452 } else {
6453 codegen_->LoadFromShiftedRegOffset(type, out_loc, temp, RegisterFrom(index));
6454 }
6455 }
6456 break;
6457 }
6458
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006459 case DataType::Type::kReference: {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006460 // The read barrier instrumentation of object ArrayGet
6461 // instructions does not support the HIntermediateAddress
6462 // instruction.
6463 DCHECK(!(has_intermediate_address && kEmitCompilerReadBarrier));
6464
Scott Wakelingc34dba72016-10-03 10:14:44 +01006465 static_assert(
6466 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6467 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
6468 // /* HeapReference<Object> */ out =
6469 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
6470 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006471 Location temp = locations->GetTemp(0);
6472 // Note that a potential implicit null check is handled in this
6473 // CodeGeneratorARMVIXL::GenerateArrayLoadWithBakerReadBarrier call.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01006474 DCHECK(!instruction->CanDoImplicitNullCheckOn(instruction->InputAt(0)));
6475 if (index.IsConstant()) {
6476 // Array load with a constant index can be treated as a field load.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006477 data_offset += Int32ConstantFrom(index) << DataType::SizeShift(type);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01006478 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6479 out_loc,
6480 obj,
6481 data_offset,
6482 locations->GetTemp(0),
6483 /* needs_null_check */ false);
6484 } else {
6485 codegen_->GenerateArrayLoadWithBakerReadBarrier(
6486 instruction, out_loc, obj, data_offset, index, temp, /* needs_null_check */ false);
6487 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006488 } else {
6489 vixl32::Register out = OutputRegister(instruction);
6490 if (index.IsConstant()) {
6491 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00006492 (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006493 GetAssembler()->LoadFromOffset(kLoadWord, out, obj, offset);
Alexandre Rames374ddf32016-11-04 10:40:49 +00006494 // TODO(VIXL): Here and for other calls to `MaybeRecordImplicitNullCheck` in this method,
6495 // we should use a scope and the assembler to emit the load instruction to guarantee that
6496 // we record the pc at the correct position. But the `Assembler` does not automatically
6497 // handle unencodable offsets. Practically, everything is fine because the helper and
6498 // VIXL, at the time of writing, do generate the store instruction last.
Scott Wakelingc34dba72016-10-03 10:14:44 +01006499 codegen_->MaybeRecordImplicitNullCheck(instruction);
6500 // If read barriers are enabled, emit read barriers other than
6501 // Baker's using a slow path (and also unpoison the loaded
6502 // reference, if heap poisoning is enabled).
6503 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
6504 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006505 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006506 vixl32::Register temp = temps.Acquire();
6507
6508 if (has_intermediate_address) {
Artem Serov2bbc9532016-10-21 11:51:50 +01006509 // We do not need to compute the intermediate address from the array: the
6510 // input instruction has done it already. See the comment in
6511 // `TryExtractArrayAccessAddress()`.
6512 if (kIsDebugBuild) {
6513 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
Anton Kirilov644032c2016-12-06 17:51:43 +00006514 DCHECK_EQ(Uint64ConstantFrom(tmp->GetOffset()), data_offset);
Artem Serov2bbc9532016-10-21 11:51:50 +01006515 }
6516 temp = obj;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006517 } else {
6518 __ Add(temp, obj, data_offset);
6519 }
6520 codegen_->LoadFromShiftedRegOffset(type, out_loc, temp, RegisterFrom(index));
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006521 temps.Close();
Alexandre Rames374ddf32016-11-04 10:40:49 +00006522 // TODO(VIXL): Use a scope to ensure that we record the pc position immediately after the
6523 // load instruction. Practically, everything is fine because the helper and VIXL, at the
6524 // time of writing, do generate the store instruction last.
Scott Wakelingc34dba72016-10-03 10:14:44 +01006525 codegen_->MaybeRecordImplicitNullCheck(instruction);
6526 // If read barriers are enabled, emit read barriers other than
6527 // Baker's using a slow path (and also unpoison the loaded
6528 // reference, if heap poisoning is enabled).
6529 codegen_->MaybeGenerateReadBarrierSlow(
6530 instruction, out_loc, out_loc, obj_loc, data_offset, index);
6531 }
6532 }
6533 break;
6534 }
6535
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006536 case DataType::Type::kInt64: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006537 if (index.IsConstant()) {
6538 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00006539 (Int32ConstantFrom(index) << TIMES_8) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006540 GetAssembler()->LoadFromOffset(kLoadWordPair, LowRegisterFrom(out_loc), obj, offset);
6541 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006542 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006543 vixl32::Register temp = temps.Acquire();
6544 __ Add(temp, obj, Operand(RegisterFrom(index), vixl32::LSL, TIMES_8));
6545 GetAssembler()->LoadFromOffset(kLoadWordPair, LowRegisterFrom(out_loc), temp, data_offset);
6546 }
6547 break;
6548 }
6549
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006550 case DataType::Type::kFloat32: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006551 vixl32::SRegister out = SRegisterFrom(out_loc);
6552 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006553 size_t offset = (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006554 GetAssembler()->LoadSFromOffset(out, obj, offset);
6555 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006556 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006557 vixl32::Register temp = temps.Acquire();
6558 __ Add(temp, obj, Operand(RegisterFrom(index), vixl32::LSL, TIMES_4));
6559 GetAssembler()->LoadSFromOffset(out, temp, data_offset);
6560 }
6561 break;
6562 }
6563
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006564 case DataType::Type::kFloat64: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006565 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006566 size_t offset = (Int32ConstantFrom(index) << TIMES_8) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006567 GetAssembler()->LoadDFromOffset(DRegisterFrom(out_loc), obj, offset);
6568 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006569 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006570 vixl32::Register temp = temps.Acquire();
6571 __ Add(temp, obj, Operand(RegisterFrom(index), vixl32::LSL, TIMES_8));
6572 GetAssembler()->LoadDFromOffset(DRegisterFrom(out_loc), temp, data_offset);
6573 }
6574 break;
6575 }
6576
Aart Bik66c158e2018-01-31 12:55:04 -08006577 case DataType::Type::kUint32:
6578 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006579 case DataType::Type::kVoid:
Scott Wakelingc34dba72016-10-03 10:14:44 +01006580 LOG(FATAL) << "Unreachable type " << type;
6581 UNREACHABLE();
6582 }
6583
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006584 if (type == DataType::Type::kReference) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006585 // Potential implicit null checks, in the case of reference
6586 // arrays, are handled in the previous switch statement.
6587 } else if (!maybe_compressed_char_at) {
Alexandre Rames374ddf32016-11-04 10:40:49 +00006588 // TODO(VIXL): Use a scope to ensure we record the pc info immediately after
6589 // the preceding load instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01006590 codegen_->MaybeRecordImplicitNullCheck(instruction);
6591 }
6592}
6593
6594void LocationsBuilderARMVIXL::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006595 DataType::Type value_type = instruction->GetComponentType();
Scott Wakelingc34dba72016-10-03 10:14:44 +01006596
6597 bool needs_write_barrier =
6598 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
6599 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
6600
Vladimir Markoca6fff82017-10-03 14:49:14 +01006601 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Scott Wakelingc34dba72016-10-03 10:14:44 +01006602 instruction,
6603 may_need_runtime_call_for_type_check ?
6604 LocationSummary::kCallOnSlowPath :
6605 LocationSummary::kNoCall);
6606
6607 locations->SetInAt(0, Location::RequiresRegister());
6608 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006609 if (DataType::IsFloatingPointType(value_type)) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006610 locations->SetInAt(2, Location::RequiresFpuRegister());
6611 } else {
6612 locations->SetInAt(2, Location::RequiresRegister());
6613 }
6614 if (needs_write_barrier) {
6615 // Temporary registers for the write barrier.
6616 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
6617 locations->AddTemp(Location::RequiresRegister());
6618 }
6619}
6620
6621void InstructionCodeGeneratorARMVIXL::VisitArraySet(HArraySet* instruction) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006622 LocationSummary* locations = instruction->GetLocations();
6623 vixl32::Register array = InputRegisterAt(instruction, 0);
6624 Location index = locations->InAt(1);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006625 DataType::Type value_type = instruction->GetComponentType();
Scott Wakelingc34dba72016-10-03 10:14:44 +01006626 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
6627 bool needs_write_barrier =
6628 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
6629 uint32_t data_offset =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006630 mirror::Array::DataOffset(DataType::Size(value_type)).Uint32Value();
Scott Wakelingc34dba72016-10-03 10:14:44 +01006631 Location value_loc = locations->InAt(2);
6632 HInstruction* array_instr = instruction->GetArray();
6633 bool has_intermediate_address = array_instr->IsIntermediateAddress();
Scott Wakelingc34dba72016-10-03 10:14:44 +01006634
6635 switch (value_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006636 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006637 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006638 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006639 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006640 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006641 case DataType::Type::kInt32: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006642 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006643 int32_t const_index = Int32ConstantFrom(index);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006644 uint32_t full_offset =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006645 data_offset + (const_index << DataType::SizeShift(value_type));
Scott Wakelingc34dba72016-10-03 10:14:44 +01006646 StoreOperandType store_type = GetStoreOperandType(value_type);
6647 GetAssembler()->StoreToOffset(store_type, RegisterFrom(value_loc), array, full_offset);
6648 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006649 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006650 vixl32::Register temp = temps.Acquire();
6651
6652 if (has_intermediate_address) {
Artem Serov2bbc9532016-10-21 11:51:50 +01006653 // We do not need to compute the intermediate address from the array: the
6654 // input instruction has done it already. See the comment in
6655 // `TryExtractArrayAccessAddress()`.
6656 if (kIsDebugBuild) {
6657 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
Anton Kirilov644032c2016-12-06 17:51:43 +00006658 DCHECK_EQ(Uint64ConstantFrom(tmp->GetOffset()), data_offset);
Artem Serov2bbc9532016-10-21 11:51:50 +01006659 }
6660 temp = array;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006661 } else {
6662 __ Add(temp, array, data_offset);
6663 }
6664 codegen_->StoreToShiftedRegOffset(value_type, value_loc, temp, RegisterFrom(index));
6665 }
6666 break;
6667 }
6668
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006669 case DataType::Type::kReference: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006670 vixl32::Register value = RegisterFrom(value_loc);
6671 // TryExtractArrayAccessAddress optimization is never applied for non-primitive ArraySet.
6672 // See the comment in instruction_simplifier_shared.cc.
6673 DCHECK(!has_intermediate_address);
6674
6675 if (instruction->InputAt(2)->IsNullConstant()) {
6676 // Just setting null.
6677 if (index.IsConstant()) {
6678 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00006679 (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006680 GetAssembler()->StoreToOffset(kStoreWord, value, array, offset);
6681 } else {
6682 DCHECK(index.IsRegister()) << index;
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006683 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006684 vixl32::Register temp = temps.Acquire();
6685 __ Add(temp, array, data_offset);
6686 codegen_->StoreToShiftedRegOffset(value_type, value_loc, temp, RegisterFrom(index));
6687 }
Alexandre Rames374ddf32016-11-04 10:40:49 +00006688 // TODO(VIXL): Use a scope to ensure we record the pc info immediately after the preceding
6689 // store instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01006690 codegen_->MaybeRecordImplicitNullCheck(instruction);
6691 DCHECK(!needs_write_barrier);
6692 DCHECK(!may_need_runtime_call_for_type_check);
6693 break;
6694 }
6695
6696 DCHECK(needs_write_barrier);
6697 Location temp1_loc = locations->GetTemp(0);
6698 vixl32::Register temp1 = RegisterFrom(temp1_loc);
6699 Location temp2_loc = locations->GetTemp(1);
6700 vixl32::Register temp2 = RegisterFrom(temp2_loc);
6701 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6702 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6703 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6704 vixl32::Label done;
Anton Kirilov6f644202017-02-27 18:29:45 +00006705 vixl32::Label* final_label = codegen_->GetFinalLabel(instruction, &done);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006706 SlowPathCodeARMVIXL* slow_path = nullptr;
6707
6708 if (may_need_runtime_call_for_type_check) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01006709 slow_path = new (codegen_->GetScopedAllocator()) ArraySetSlowPathARMVIXL(instruction);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006710 codegen_->AddSlowPath(slow_path);
6711 if (instruction->GetValueCanBeNull()) {
6712 vixl32::Label non_zero;
xueliang.zhongf51bc622016-11-04 09:23:32 +00006713 __ CompareAndBranchIfNonZero(value, &non_zero);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006714 if (index.IsConstant()) {
6715 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00006716 (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006717 GetAssembler()->StoreToOffset(kStoreWord, value, array, offset);
6718 } else {
6719 DCHECK(index.IsRegister()) << index;
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006720 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006721 vixl32::Register temp = temps.Acquire();
6722 __ Add(temp, array, data_offset);
6723 codegen_->StoreToShiftedRegOffset(value_type, value_loc, temp, RegisterFrom(index));
6724 }
Alexandre Rames374ddf32016-11-04 10:40:49 +00006725 // TODO(VIXL): Use a scope to ensure we record the pc info immediately after the preceding
6726 // store instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01006727 codegen_->MaybeRecordImplicitNullCheck(instruction);
Anton Kirilov6f644202017-02-27 18:29:45 +00006728 __ B(final_label);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006729 __ Bind(&non_zero);
6730 }
6731
6732 // Note that when read barriers are enabled, the type checks
6733 // are performed without read barriers. This is fine, even in
6734 // the case where a class object is in the from-space after
6735 // the flip, as a comparison involving such a type would not
6736 // produce a false positive; it may of course produce a false
6737 // negative, in which case we would take the ArraySet slow
6738 // path.
6739
Alexandre Rames374ddf32016-11-04 10:40:49 +00006740 {
6741 // Ensure we record the pc position immediately after the `ldr` instruction.
Artem Serov0fb37192016-12-06 18:13:40 +00006742 ExactAssemblyScope aas(GetVIXLAssembler(),
6743 vixl32::kMaxInstructionSizeInBytes,
6744 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00006745 // /* HeapReference<Class> */ temp1 = array->klass_
6746 __ ldr(temp1, MemOperand(array, class_offset));
6747 codegen_->MaybeRecordImplicitNullCheck(instruction);
6748 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006749 GetAssembler()->MaybeUnpoisonHeapReference(temp1);
6750
6751 // /* HeapReference<Class> */ temp1 = temp1->component_type_
6752 GetAssembler()->LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
6753 // /* HeapReference<Class> */ temp2 = value->klass_
6754 GetAssembler()->LoadFromOffset(kLoadWord, temp2, value, class_offset);
6755 // If heap poisoning is enabled, no need to unpoison `temp1`
6756 // nor `temp2`, as we are comparing two poisoned references.
6757 __ Cmp(temp1, temp2);
6758
6759 if (instruction->StaticTypeOfArrayIsObjectArray()) {
6760 vixl32::Label do_put;
Artem Serov517d9f62016-12-12 15:51:15 +00006761 __ B(eq, &do_put, /* far_target */ false);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006762 // If heap poisoning is enabled, the `temp1` reference has
6763 // not been unpoisoned yet; unpoison it now.
6764 GetAssembler()->MaybeUnpoisonHeapReference(temp1);
6765
6766 // /* HeapReference<Class> */ temp1 = temp1->super_class_
6767 GetAssembler()->LoadFromOffset(kLoadWord, temp1, temp1, super_offset);
6768 // If heap poisoning is enabled, no need to unpoison
6769 // `temp1`, as we are comparing against null below.
xueliang.zhongf51bc622016-11-04 09:23:32 +00006770 __ CompareAndBranchIfNonZero(temp1, slow_path->GetEntryLabel());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006771 __ Bind(&do_put);
6772 } else {
6773 __ B(ne, slow_path->GetEntryLabel());
6774 }
6775 }
6776
6777 vixl32::Register source = value;
6778 if (kPoisonHeapReferences) {
6779 // Note that in the case where `value` is a null reference,
6780 // we do not enter this block, as a null reference does not
6781 // need poisoning.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006782 DCHECK_EQ(value_type, DataType::Type::kReference);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006783 __ Mov(temp1, value);
6784 GetAssembler()->PoisonHeapReference(temp1);
6785 source = temp1;
6786 }
6787
6788 if (index.IsConstant()) {
6789 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00006790 (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006791 GetAssembler()->StoreToOffset(kStoreWord, source, array, offset);
6792 } else {
6793 DCHECK(index.IsRegister()) << index;
6794
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006795 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006796 vixl32::Register temp = temps.Acquire();
6797 __ Add(temp, array, data_offset);
6798 codegen_->StoreToShiftedRegOffset(value_type,
6799 LocationFrom(source),
6800 temp,
6801 RegisterFrom(index));
6802 }
6803
6804 if (!may_need_runtime_call_for_type_check) {
Alexandre Rames374ddf32016-11-04 10:40:49 +00006805 // TODO(VIXL): Ensure we record the pc position immediately after the preceding store
6806 // instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01006807 codegen_->MaybeRecordImplicitNullCheck(instruction);
6808 }
6809
6810 codegen_->MarkGCCard(temp1, temp2, array, value, instruction->GetValueCanBeNull());
6811
6812 if (done.IsReferenced()) {
6813 __ Bind(&done);
6814 }
6815
6816 if (slow_path != nullptr) {
6817 __ Bind(slow_path->GetExitLabel());
6818 }
6819
6820 break;
6821 }
6822
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006823 case DataType::Type::kInt64: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006824 Location value = locations->InAt(2);
6825 if (index.IsConstant()) {
6826 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00006827 (Int32ConstantFrom(index) << TIMES_8) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006828 GetAssembler()->StoreToOffset(kStoreWordPair, LowRegisterFrom(value), array, offset);
6829 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006830 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006831 vixl32::Register temp = temps.Acquire();
6832 __ Add(temp, array, Operand(RegisterFrom(index), vixl32::LSL, TIMES_8));
6833 GetAssembler()->StoreToOffset(kStoreWordPair, LowRegisterFrom(value), temp, data_offset);
6834 }
6835 break;
6836 }
6837
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006838 case DataType::Type::kFloat32: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006839 Location value = locations->InAt(2);
6840 DCHECK(value.IsFpuRegister());
6841 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006842 size_t offset = (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006843 GetAssembler()->StoreSToOffset(SRegisterFrom(value), array, offset);
6844 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006845 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006846 vixl32::Register temp = temps.Acquire();
6847 __ Add(temp, array, Operand(RegisterFrom(index), vixl32::LSL, TIMES_4));
6848 GetAssembler()->StoreSToOffset(SRegisterFrom(value), temp, data_offset);
6849 }
6850 break;
6851 }
6852
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006853 case DataType::Type::kFloat64: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006854 Location value = locations->InAt(2);
6855 DCHECK(value.IsFpuRegisterPair());
6856 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006857 size_t offset = (Int32ConstantFrom(index) << TIMES_8) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006858 GetAssembler()->StoreDToOffset(DRegisterFrom(value), array, offset);
6859 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006860 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006861 vixl32::Register temp = temps.Acquire();
6862 __ Add(temp, array, Operand(RegisterFrom(index), vixl32::LSL, TIMES_8));
6863 GetAssembler()->StoreDToOffset(DRegisterFrom(value), temp, data_offset);
6864 }
6865 break;
6866 }
6867
Aart Bik66c158e2018-01-31 12:55:04 -08006868 case DataType::Type::kUint32:
6869 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006870 case DataType::Type::kVoid:
Scott Wakelingc34dba72016-10-03 10:14:44 +01006871 LOG(FATAL) << "Unreachable type " << value_type;
6872 UNREACHABLE();
6873 }
6874
6875 // Objects are handled in the switch.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006876 if (value_type != DataType::Type::kReference) {
Alexandre Rames374ddf32016-11-04 10:40:49 +00006877 // TODO(VIXL): Ensure we record the pc position immediately after the preceding store
6878 // instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01006879 codegen_->MaybeRecordImplicitNullCheck(instruction);
6880 }
6881}
6882
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006883void LocationsBuilderARMVIXL::VisitArrayLength(HArrayLength* instruction) {
6884 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006885 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006886 locations->SetInAt(0, Location::RequiresRegister());
6887 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6888}
6889
6890void InstructionCodeGeneratorARMVIXL::VisitArrayLength(HArrayLength* instruction) {
6891 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
6892 vixl32::Register obj = InputRegisterAt(instruction, 0);
6893 vixl32::Register out = OutputRegister(instruction);
Alexandre Rames374ddf32016-11-04 10:40:49 +00006894 {
Artem Serov0fb37192016-12-06 18:13:40 +00006895 ExactAssemblyScope aas(GetVIXLAssembler(),
6896 vixl32::kMaxInstructionSizeInBytes,
6897 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00006898 __ ldr(out, MemOperand(obj, offset));
6899 codegen_->MaybeRecordImplicitNullCheck(instruction);
6900 }
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006901 // Mask out compression flag from String's array length.
6902 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006903 __ Lsr(out, out, 1u);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006904 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006905}
6906
Artem Serov2bbc9532016-10-21 11:51:50 +01006907void LocationsBuilderARMVIXL::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Artem Serov2bbc9532016-10-21 11:51:50 +01006908 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006909 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Artem Serov2bbc9532016-10-21 11:51:50 +01006910
6911 locations->SetInAt(0, Location::RequiresRegister());
6912 locations->SetInAt(1, Location::RegisterOrConstant(instruction->GetOffset()));
6913 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6914}
6915
6916void InstructionCodeGeneratorARMVIXL::VisitIntermediateAddress(HIntermediateAddress* instruction) {
6917 vixl32::Register out = OutputRegister(instruction);
6918 vixl32::Register first = InputRegisterAt(instruction, 0);
6919 Location second = instruction->GetLocations()->InAt(1);
6920
Artem Serov2bbc9532016-10-21 11:51:50 +01006921 if (second.IsRegister()) {
6922 __ Add(out, first, RegisterFrom(second));
6923 } else {
Anton Kirilov644032c2016-12-06 17:51:43 +00006924 __ Add(out, first, Int32ConstantFrom(second));
Artem Serov2bbc9532016-10-21 11:51:50 +01006925 }
6926}
6927
Artem Serove1811ed2017-04-27 16:50:47 +01006928void LocationsBuilderARMVIXL::VisitIntermediateAddressIndex(
6929 HIntermediateAddressIndex* instruction) {
6930 LOG(FATAL) << "Unreachable " << instruction->GetId();
6931}
6932
6933void InstructionCodeGeneratorARMVIXL::VisitIntermediateAddressIndex(
6934 HIntermediateAddressIndex* instruction) {
6935 LOG(FATAL) << "Unreachable " << instruction->GetId();
6936}
6937
Scott Wakelingc34dba72016-10-03 10:14:44 +01006938void LocationsBuilderARMVIXL::VisitBoundsCheck(HBoundsCheck* instruction) {
6939 RegisterSet caller_saves = RegisterSet::Empty();
6940 InvokeRuntimeCallingConventionARMVIXL calling_convention;
6941 caller_saves.Add(LocationFrom(calling_convention.GetRegisterAt(0)));
6942 caller_saves.Add(LocationFrom(calling_convention.GetRegisterAt(1)));
6943 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Artem Serov2dd053d2017-03-08 14:54:06 +00006944
6945 HInstruction* index = instruction->InputAt(0);
6946 HInstruction* length = instruction->InputAt(1);
6947 // If both index and length are constants we can statically check the bounds. But if at least one
6948 // of them is not encodable ArmEncodableConstantOrRegister will create
6949 // Location::RequiresRegister() which is not desired to happen. Instead we create constant
6950 // locations.
6951 bool both_const = index->IsConstant() && length->IsConstant();
6952 locations->SetInAt(0, both_const
6953 ? Location::ConstantLocation(index->AsConstant())
6954 : ArmEncodableConstantOrRegister(index, CMP));
6955 locations->SetInAt(1, both_const
6956 ? Location::ConstantLocation(length->AsConstant())
6957 : ArmEncodableConstantOrRegister(length, CMP));
Scott Wakelingc34dba72016-10-03 10:14:44 +01006958}
6959
6960void InstructionCodeGeneratorARMVIXL::VisitBoundsCheck(HBoundsCheck* instruction) {
Artem Serov2dd053d2017-03-08 14:54:06 +00006961 LocationSummary* locations = instruction->GetLocations();
6962 Location index_loc = locations->InAt(0);
6963 Location length_loc = locations->InAt(1);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006964
Artem Serov2dd053d2017-03-08 14:54:06 +00006965 if (length_loc.IsConstant()) {
6966 int32_t length = Int32ConstantFrom(length_loc);
6967 if (index_loc.IsConstant()) {
6968 // BCE will remove the bounds check if we are guaranteed to pass.
6969 int32_t index = Int32ConstantFrom(index_loc);
6970 if (index < 0 || index >= length) {
6971 SlowPathCodeARMVIXL* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006972 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathARMVIXL(instruction);
Artem Serov2dd053d2017-03-08 14:54:06 +00006973 codegen_->AddSlowPath(slow_path);
6974 __ B(slow_path->GetEntryLabel());
6975 } else {
6976 // Some optimization after BCE may have generated this, and we should not
6977 // generate a bounds check if it is a valid range.
6978 }
6979 return;
6980 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006981
Artem Serov2dd053d2017-03-08 14:54:06 +00006982 SlowPathCodeARMVIXL* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006983 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathARMVIXL(instruction);
Artem Serov2dd053d2017-03-08 14:54:06 +00006984 __ Cmp(RegisterFrom(index_loc), length);
6985 codegen_->AddSlowPath(slow_path);
6986 __ B(hs, slow_path->GetEntryLabel());
6987 } else {
6988 SlowPathCodeARMVIXL* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006989 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathARMVIXL(instruction);
Artem Serov2dd053d2017-03-08 14:54:06 +00006990 __ Cmp(RegisterFrom(length_loc), InputOperandAt(instruction, 0));
6991 codegen_->AddSlowPath(slow_path);
6992 __ B(ls, slow_path->GetEntryLabel());
6993 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006994}
6995
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006996void CodeGeneratorARMVIXL::MarkGCCard(vixl32::Register temp,
6997 vixl32::Register card,
6998 vixl32::Register object,
6999 vixl32::Register value,
7000 bool can_be_null) {
7001 vixl32::Label is_null;
7002 if (can_be_null) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00007003 __ CompareAndBranchIfZero(value, &is_null);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007004 }
7005 GetAssembler()->LoadFromOffset(
7006 kLoadWord, card, tr, Thread::CardTableOffset<kArmPointerSize>().Int32Value());
Scott Wakelingb77051e2016-11-21 19:46:00 +00007007 __ Lsr(temp, object, Operand::From(gc::accounting::CardTable::kCardShift));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007008 __ Strb(card, MemOperand(card, temp));
7009 if (can_be_null) {
7010 __ Bind(&is_null);
7011 }
7012}
7013
Scott Wakelingfe885462016-09-22 10:24:38 +01007014void LocationsBuilderARMVIXL::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
7015 LOG(FATAL) << "Unreachable";
7016}
7017
7018void InstructionCodeGeneratorARMVIXL::VisitParallelMove(HParallelMove* instruction) {
Vladimir Markobea75ff2017-10-11 20:39:54 +01007019 if (instruction->GetNext()->IsSuspendCheck() &&
7020 instruction->GetBlock()->GetLoopInformation() != nullptr) {
7021 HSuspendCheck* suspend_check = instruction->GetNext()->AsSuspendCheck();
7022 // The back edge will generate the suspend check.
7023 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(suspend_check, instruction);
7024 }
7025
Scott Wakelingfe885462016-09-22 10:24:38 +01007026 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
7027}
7028
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007029void LocationsBuilderARMVIXL::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007030 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
7031 instruction, LocationSummary::kCallOnSlowPath);
Artem Serov657022c2016-11-23 14:19:38 +00007032 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007033}
7034
7035void InstructionCodeGeneratorARMVIXL::VisitSuspendCheck(HSuspendCheck* instruction) {
7036 HBasicBlock* block = instruction->GetBlock();
7037 if (block->GetLoopInformation() != nullptr) {
7038 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
7039 // The back edge will generate the suspend check.
7040 return;
7041 }
7042 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
7043 // The goto will generate the suspend check.
7044 return;
7045 }
7046 GenerateSuspendCheck(instruction, nullptr);
Roland Levillain5daa4952017-07-03 17:23:56 +01007047 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 12);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007048}
7049
7050void InstructionCodeGeneratorARMVIXL::GenerateSuspendCheck(HSuspendCheck* instruction,
7051 HBasicBlock* successor) {
7052 SuspendCheckSlowPathARMVIXL* slow_path =
7053 down_cast<SuspendCheckSlowPathARMVIXL*>(instruction->GetSlowPath());
7054 if (slow_path == nullptr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007055 slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01007056 new (codegen_->GetScopedAllocator()) SuspendCheckSlowPathARMVIXL(instruction, successor);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007057 instruction->SetSlowPath(slow_path);
7058 codegen_->AddSlowPath(slow_path);
7059 if (successor != nullptr) {
7060 DCHECK(successor->IsLoopHeader());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007061 }
7062 } else {
7063 DCHECK_EQ(slow_path->GetSuccessor(), successor);
7064 }
7065
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007066 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007067 vixl32::Register temp = temps.Acquire();
7068 GetAssembler()->LoadFromOffset(
7069 kLoadUnsignedHalfword, temp, tr, Thread::ThreadFlagsOffset<kArmPointerSize>().Int32Value());
7070 if (successor == nullptr) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00007071 __ CompareAndBranchIfNonZero(temp, slow_path->GetEntryLabel());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007072 __ Bind(slow_path->GetReturnLabel());
7073 } else {
xueliang.zhongf51bc622016-11-04 09:23:32 +00007074 __ CompareAndBranchIfZero(temp, codegen_->GetLabelOf(successor));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007075 __ B(slow_path->GetEntryLabel());
7076 }
7077}
7078
Scott Wakelingfe885462016-09-22 10:24:38 +01007079ArmVIXLAssembler* ParallelMoveResolverARMVIXL::GetAssembler() const {
7080 return codegen_->GetAssembler();
7081}
7082
7083void ParallelMoveResolverARMVIXL::EmitMove(size_t index) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007084 UseScratchRegisterScope temps(GetAssembler()->GetVIXLAssembler());
Scott Wakelingfe885462016-09-22 10:24:38 +01007085 MoveOperands* move = moves_[index];
7086 Location source = move->GetSource();
7087 Location destination = move->GetDestination();
7088
7089 if (source.IsRegister()) {
7090 if (destination.IsRegister()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007091 __ Mov(RegisterFrom(destination), RegisterFrom(source));
Scott Wakelingfe885462016-09-22 10:24:38 +01007092 } else if (destination.IsFpuRegister()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007093 __ Vmov(SRegisterFrom(destination), RegisterFrom(source));
Scott Wakelingfe885462016-09-22 10:24:38 +01007094 } else {
7095 DCHECK(destination.IsStackSlot());
7096 GetAssembler()->StoreToOffset(kStoreWord,
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007097 RegisterFrom(source),
Scott Wakelingfe885462016-09-22 10:24:38 +01007098 sp,
7099 destination.GetStackIndex());
7100 }
7101 } else if (source.IsStackSlot()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007102 if (destination.IsRegister()) {
7103 GetAssembler()->LoadFromOffset(kLoadWord,
7104 RegisterFrom(destination),
7105 sp,
7106 source.GetStackIndex());
7107 } else if (destination.IsFpuRegister()) {
7108 GetAssembler()->LoadSFromOffset(SRegisterFrom(destination), sp, source.GetStackIndex());
7109 } else {
7110 DCHECK(destination.IsStackSlot());
7111 vixl32::Register temp = temps.Acquire();
7112 GetAssembler()->LoadFromOffset(kLoadWord, temp, sp, source.GetStackIndex());
7113 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
7114 }
Scott Wakelingfe885462016-09-22 10:24:38 +01007115 } else if (source.IsFpuRegister()) {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01007116 if (destination.IsRegister()) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01007117 __ Vmov(RegisterFrom(destination), SRegisterFrom(source));
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01007118 } else if (destination.IsFpuRegister()) {
7119 __ Vmov(SRegisterFrom(destination), SRegisterFrom(source));
7120 } else {
7121 DCHECK(destination.IsStackSlot());
7122 GetAssembler()->StoreSToOffset(SRegisterFrom(source), sp, destination.GetStackIndex());
7123 }
Scott Wakelingfe885462016-09-22 10:24:38 +01007124 } else if (source.IsDoubleStackSlot()) {
Alexandre Rames9c19bd62016-10-24 11:50:32 +01007125 if (destination.IsDoubleStackSlot()) {
7126 vixl32::DRegister temp = temps.AcquireD();
7127 GetAssembler()->LoadDFromOffset(temp, sp, source.GetStackIndex());
7128 GetAssembler()->StoreDToOffset(temp, sp, destination.GetStackIndex());
7129 } else if (destination.IsRegisterPair()) {
7130 DCHECK(ExpectedPairLayout(destination));
7131 GetAssembler()->LoadFromOffset(
7132 kLoadWordPair, LowRegisterFrom(destination), sp, source.GetStackIndex());
7133 } else {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01007134 DCHECK(destination.IsFpuRegisterPair()) << destination;
7135 GetAssembler()->LoadDFromOffset(DRegisterFrom(destination), sp, source.GetStackIndex());
Alexandre Rames9c19bd62016-10-24 11:50:32 +01007136 }
Scott Wakelingfe885462016-09-22 10:24:38 +01007137 } else if (source.IsRegisterPair()) {
7138 if (destination.IsRegisterPair()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007139 __ Mov(LowRegisterFrom(destination), LowRegisterFrom(source));
7140 __ Mov(HighRegisterFrom(destination), HighRegisterFrom(source));
Scott Wakelingfe885462016-09-22 10:24:38 +01007141 } else if (destination.IsFpuRegisterPair()) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01007142 __ Vmov(DRegisterFrom(destination), LowRegisterFrom(source), HighRegisterFrom(source));
Scott Wakelingfe885462016-09-22 10:24:38 +01007143 } else {
7144 DCHECK(destination.IsDoubleStackSlot()) << destination;
7145 DCHECK(ExpectedPairLayout(source));
7146 GetAssembler()->StoreToOffset(kStoreWordPair,
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007147 LowRegisterFrom(source),
Scott Wakelingfe885462016-09-22 10:24:38 +01007148 sp,
7149 destination.GetStackIndex());
7150 }
7151 } else if (source.IsFpuRegisterPair()) {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01007152 if (destination.IsRegisterPair()) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01007153 __ Vmov(LowRegisterFrom(destination), HighRegisterFrom(destination), DRegisterFrom(source));
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01007154 } else if (destination.IsFpuRegisterPair()) {
7155 __ Vmov(DRegisterFrom(destination), DRegisterFrom(source));
7156 } else {
7157 DCHECK(destination.IsDoubleStackSlot()) << destination;
7158 GetAssembler()->StoreDToOffset(DRegisterFrom(source), sp, destination.GetStackIndex());
7159 }
Scott Wakelingfe885462016-09-22 10:24:38 +01007160 } else {
7161 DCHECK(source.IsConstant()) << source;
7162 HConstant* constant = source.GetConstant();
7163 if (constant->IsIntConstant() || constant->IsNullConstant()) {
7164 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
7165 if (destination.IsRegister()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007166 __ Mov(RegisterFrom(destination), value);
Scott Wakelingfe885462016-09-22 10:24:38 +01007167 } else {
7168 DCHECK(destination.IsStackSlot());
Scott Wakelingfe885462016-09-22 10:24:38 +01007169 vixl32::Register temp = temps.Acquire();
7170 __ Mov(temp, value);
7171 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
7172 }
7173 } else if (constant->IsLongConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00007174 int64_t value = Int64ConstantFrom(source);
Scott Wakelingfe885462016-09-22 10:24:38 +01007175 if (destination.IsRegisterPair()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007176 __ Mov(LowRegisterFrom(destination), Low32Bits(value));
7177 __ Mov(HighRegisterFrom(destination), High32Bits(value));
Scott Wakelingfe885462016-09-22 10:24:38 +01007178 } else {
7179 DCHECK(destination.IsDoubleStackSlot()) << destination;
Scott Wakelingfe885462016-09-22 10:24:38 +01007180 vixl32::Register temp = temps.Acquire();
7181 __ Mov(temp, Low32Bits(value));
7182 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
7183 __ Mov(temp, High32Bits(value));
7184 GetAssembler()->StoreToOffset(kStoreWord,
7185 temp,
7186 sp,
7187 destination.GetHighStackIndex(kArmWordSize));
7188 }
7189 } else if (constant->IsDoubleConstant()) {
7190 double value = constant->AsDoubleConstant()->GetValue();
7191 if (destination.IsFpuRegisterPair()) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01007192 __ Vmov(DRegisterFrom(destination), value);
Scott Wakelingfe885462016-09-22 10:24:38 +01007193 } else {
7194 DCHECK(destination.IsDoubleStackSlot()) << destination;
7195 uint64_t int_value = bit_cast<uint64_t, double>(value);
Scott Wakelingfe885462016-09-22 10:24:38 +01007196 vixl32::Register temp = temps.Acquire();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007197 __ Mov(temp, Low32Bits(int_value));
Scott Wakelingfe885462016-09-22 10:24:38 +01007198 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007199 __ Mov(temp, High32Bits(int_value));
Scott Wakelingfe885462016-09-22 10:24:38 +01007200 GetAssembler()->StoreToOffset(kStoreWord,
7201 temp,
7202 sp,
7203 destination.GetHighStackIndex(kArmWordSize));
7204 }
7205 } else {
7206 DCHECK(constant->IsFloatConstant()) << constant->DebugName();
7207 float value = constant->AsFloatConstant()->GetValue();
7208 if (destination.IsFpuRegister()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007209 __ Vmov(SRegisterFrom(destination), value);
Scott Wakelingfe885462016-09-22 10:24:38 +01007210 } else {
7211 DCHECK(destination.IsStackSlot());
Scott Wakelingfe885462016-09-22 10:24:38 +01007212 vixl32::Register temp = temps.Acquire();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007213 __ Mov(temp, bit_cast<int32_t, float>(value));
Scott Wakelingfe885462016-09-22 10:24:38 +01007214 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
7215 }
7216 }
7217 }
7218}
7219
Alexandre Rames9c19bd62016-10-24 11:50:32 +01007220void ParallelMoveResolverARMVIXL::Exchange(vixl32::Register reg, int mem) {
7221 UseScratchRegisterScope temps(GetAssembler()->GetVIXLAssembler());
7222 vixl32::Register temp = temps.Acquire();
7223 __ Mov(temp, reg);
7224 GetAssembler()->LoadFromOffset(kLoadWord, reg, sp, mem);
7225 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, mem);
Scott Wakelingfe885462016-09-22 10:24:38 +01007226}
7227
Alexandre Rames9c19bd62016-10-24 11:50:32 +01007228void ParallelMoveResolverARMVIXL::Exchange(int mem1, int mem2) {
7229 // TODO(VIXL32): Double check the performance of this implementation.
7230 UseScratchRegisterScope temps(GetAssembler()->GetVIXLAssembler());
Nicolas Geoffray13a797b2017-03-15 16:41:31 +00007231 vixl32::Register temp1 = temps.Acquire();
7232 ScratchRegisterScope ensure_scratch(
7233 this, temp1.GetCode(), r0.GetCode(), codegen_->GetNumberOfCoreRegisters());
7234 vixl32::Register temp2(ensure_scratch.GetRegister());
Alexandre Rames9c19bd62016-10-24 11:50:32 +01007235
Nicolas Geoffray13a797b2017-03-15 16:41:31 +00007236 int stack_offset = ensure_scratch.IsSpilled() ? kArmWordSize : 0;
7237 GetAssembler()->LoadFromOffset(kLoadWord, temp1, sp, mem1 + stack_offset);
7238 GetAssembler()->LoadFromOffset(kLoadWord, temp2, sp, mem2 + stack_offset);
7239 GetAssembler()->StoreToOffset(kStoreWord, temp1, sp, mem2 + stack_offset);
7240 GetAssembler()->StoreToOffset(kStoreWord, temp2, sp, mem1 + stack_offset);
Scott Wakelingfe885462016-09-22 10:24:38 +01007241}
7242
Alexandre Rames9c19bd62016-10-24 11:50:32 +01007243void ParallelMoveResolverARMVIXL::EmitSwap(size_t index) {
7244 MoveOperands* move = moves_[index];
7245 Location source = move->GetSource();
7246 Location destination = move->GetDestination();
7247 UseScratchRegisterScope temps(GetAssembler()->GetVIXLAssembler());
7248
7249 if (source.IsRegister() && destination.IsRegister()) {
7250 vixl32::Register temp = temps.Acquire();
7251 DCHECK(!RegisterFrom(source).Is(temp));
7252 DCHECK(!RegisterFrom(destination).Is(temp));
7253 __ Mov(temp, RegisterFrom(destination));
7254 __ Mov(RegisterFrom(destination), RegisterFrom(source));
7255 __ Mov(RegisterFrom(source), temp);
7256 } else if (source.IsRegister() && destination.IsStackSlot()) {
7257 Exchange(RegisterFrom(source), destination.GetStackIndex());
7258 } else if (source.IsStackSlot() && destination.IsRegister()) {
7259 Exchange(RegisterFrom(destination), source.GetStackIndex());
7260 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Anton Kirilovdda43962016-11-21 19:55:20 +00007261 Exchange(source.GetStackIndex(), destination.GetStackIndex());
Alexandre Rames9c19bd62016-10-24 11:50:32 +01007262 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Nicolas Geoffray13a797b2017-03-15 16:41:31 +00007263 vixl32::Register temp = temps.Acquire();
Anton Kirilovdda43962016-11-21 19:55:20 +00007264 __ Vmov(temp, SRegisterFrom(source));
7265 __ Vmov(SRegisterFrom(source), SRegisterFrom(destination));
7266 __ Vmov(SRegisterFrom(destination), temp);
Alexandre Rames9c19bd62016-10-24 11:50:32 +01007267 } else if (source.IsRegisterPair() && destination.IsRegisterPair()) {
7268 vixl32::DRegister temp = temps.AcquireD();
7269 __ Vmov(temp, LowRegisterFrom(source), HighRegisterFrom(source));
7270 __ Mov(LowRegisterFrom(source), LowRegisterFrom(destination));
7271 __ Mov(HighRegisterFrom(source), HighRegisterFrom(destination));
7272 __ Vmov(LowRegisterFrom(destination), HighRegisterFrom(destination), temp);
7273 } else if (source.IsRegisterPair() || destination.IsRegisterPair()) {
7274 vixl32::Register low_reg = LowRegisterFrom(source.IsRegisterPair() ? source : destination);
7275 int mem = source.IsRegisterPair() ? destination.GetStackIndex() : source.GetStackIndex();
7276 DCHECK(ExpectedPairLayout(source.IsRegisterPair() ? source : destination));
7277 vixl32::DRegister temp = temps.AcquireD();
7278 __ Vmov(temp, low_reg, vixl32::Register(low_reg.GetCode() + 1));
7279 GetAssembler()->LoadFromOffset(kLoadWordPair, low_reg, sp, mem);
7280 GetAssembler()->StoreDToOffset(temp, sp, mem);
7281 } else if (source.IsFpuRegisterPair() && destination.IsFpuRegisterPair()) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007282 vixl32::DRegister first = DRegisterFrom(source);
7283 vixl32::DRegister second = DRegisterFrom(destination);
7284 vixl32::DRegister temp = temps.AcquireD();
7285 __ Vmov(temp, first);
7286 __ Vmov(first, second);
7287 __ Vmov(second, temp);
Alexandre Rames9c19bd62016-10-24 11:50:32 +01007288 } else if (source.IsFpuRegisterPair() || destination.IsFpuRegisterPair()) {
Anton Kirilovdda43962016-11-21 19:55:20 +00007289 vixl32::DRegister reg = source.IsFpuRegisterPair()
7290 ? DRegisterFrom(source)
7291 : DRegisterFrom(destination);
7292 int mem = source.IsFpuRegisterPair()
7293 ? destination.GetStackIndex()
7294 : source.GetStackIndex();
7295 vixl32::DRegister temp = temps.AcquireD();
7296 __ Vmov(temp, reg);
7297 GetAssembler()->LoadDFromOffset(reg, sp, mem);
7298 GetAssembler()->StoreDToOffset(temp, sp, mem);
Alexandre Rames9c19bd62016-10-24 11:50:32 +01007299 } else if (source.IsFpuRegister() || destination.IsFpuRegister()) {
Anton Kirilovdda43962016-11-21 19:55:20 +00007300 vixl32::SRegister reg = source.IsFpuRegister()
7301 ? SRegisterFrom(source)
7302 : SRegisterFrom(destination);
7303 int mem = source.IsFpuRegister()
7304 ? destination.GetStackIndex()
7305 : source.GetStackIndex();
7306 vixl32::Register temp = temps.Acquire();
7307 __ Vmov(temp, reg);
7308 GetAssembler()->LoadSFromOffset(reg, sp, mem);
7309 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, mem);
Alexandre Rames9c19bd62016-10-24 11:50:32 +01007310 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
7311 vixl32::DRegister temp1 = temps.AcquireD();
7312 vixl32::DRegister temp2 = temps.AcquireD();
7313 __ Vldr(temp1, MemOperand(sp, source.GetStackIndex()));
7314 __ Vldr(temp2, MemOperand(sp, destination.GetStackIndex()));
7315 __ Vstr(temp1, MemOperand(sp, destination.GetStackIndex()));
7316 __ Vstr(temp2, MemOperand(sp, source.GetStackIndex()));
7317 } else {
7318 LOG(FATAL) << "Unimplemented" << source << " <-> " << destination;
7319 }
Scott Wakelingfe885462016-09-22 10:24:38 +01007320}
7321
Nicolas Geoffray13a797b2017-03-15 16:41:31 +00007322void ParallelMoveResolverARMVIXL::SpillScratch(int reg) {
7323 __ Push(vixl32::Register(reg));
Scott Wakelingfe885462016-09-22 10:24:38 +01007324}
7325
Nicolas Geoffray13a797b2017-03-15 16:41:31 +00007326void ParallelMoveResolverARMVIXL::RestoreScratch(int reg) {
7327 __ Pop(vixl32::Register(reg));
Scott Wakelingfe885462016-09-22 10:24:38 +01007328}
7329
Artem Serov02d37832016-10-25 15:25:33 +01007330HLoadClass::LoadKind CodeGeneratorARMVIXL::GetSupportedLoadClassKind(
Artem Serovd4cc5b22016-11-04 11:19:09 +00007331 HLoadClass::LoadKind desired_class_load_kind) {
7332 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00007333 case HLoadClass::LoadKind::kInvalid:
7334 LOG(FATAL) << "UNREACHABLE";
7335 UNREACHABLE();
Artem Serovd4cc5b22016-11-04 11:19:09 +00007336 case HLoadClass::LoadKind::kReferrersClass:
7337 break;
Artem Serovd4cc5b22016-11-04 11:19:09 +00007338 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko94ec2db2017-09-06 17:21:03 +01007339 case HLoadClass::LoadKind::kBootImageClassTable:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007340 case HLoadClass::LoadKind::kBssEntry:
7341 DCHECK(!Runtime::Current()->UseJitCompilation());
7342 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007343 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007344 DCHECK(Runtime::Current()->UseJitCompilation());
Artem Serovc5fcb442016-12-02 19:19:58 +00007345 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01007346 case HLoadClass::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007347 case HLoadClass::LoadKind::kRuntimeCall:
Artem Serovd4cc5b22016-11-04 11:19:09 +00007348 break;
7349 }
7350 return desired_class_load_kind;
Artem Serov02d37832016-10-25 15:25:33 +01007351}
7352
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007353void LocationsBuilderARMVIXL::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00007354 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007355 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007356 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00007357 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007358 cls,
7359 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00007360 LocationFrom(r0));
Vladimir Markoea4c1262017-02-06 19:59:33 +00007361 DCHECK(calling_convention.GetRegisterAt(0).Is(r0));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007362 return;
7363 }
Vladimir Marko41559982017-01-06 14:04:23 +00007364 DCHECK(!cls->NeedsAccessCheck());
Scott Wakelingfe885462016-09-22 10:24:38 +01007365
Artem Serovd4cc5b22016-11-04 11:19:09 +00007366 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
7367 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007368 ? LocationSummary::kCallOnSlowPath
7369 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01007370 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(cls, call_kind);
Artem Serovd4cc5b22016-11-04 11:19:09 +00007371 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007372 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Artem Serovd4cc5b22016-11-04 11:19:09 +00007373 }
7374
Vladimir Marko41559982017-01-06 14:04:23 +00007375 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007376 locations->SetInAt(0, Location::RequiresRegister());
7377 }
7378 locations->SetOut(Location::RequiresRegister());
Vladimir Markoea4c1262017-02-06 19:59:33 +00007379 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
7380 if (!kUseReadBarrier || kUseBakerReadBarrier) {
7381 // Rely on the type resolution or initialization and marking to save everything we need.
Vladimir Markoea4c1262017-02-06 19:59:33 +00007382 RegisterSet caller_saves = RegisterSet::Empty();
7383 InvokeRuntimeCallingConventionARMVIXL calling_convention;
7384 caller_saves.Add(LocationFrom(calling_convention.GetRegisterAt(0)));
7385 // TODO: Add GetReturnLocation() to the calling convention so that we can DCHECK()
7386 // that the the kPrimNot result register is the same as the first argument register.
7387 locations->SetCustomSlowPathCallerSaves(caller_saves);
7388 } else {
7389 // For non-Baker read barrier we have a temp-clobbering call.
7390 }
7391 }
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01007392 if (kUseBakerReadBarrier && kBakerReadBarrierLinkTimeThunksEnableForGcRoots) {
7393 if (load_kind == HLoadClass::LoadKind::kBssEntry ||
7394 (load_kind == HLoadClass::LoadKind::kReferrersClass &&
7395 !Runtime::Current()->UseJitCompilation())) {
7396 locations->AddTemp(Location::RegisterLocation(kBakerCcEntrypointRegister.GetCode()));
7397 }
7398 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007399}
7400
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007401// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
7402// move.
7403void InstructionCodeGeneratorARMVIXL::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00007404 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007405 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00007406 codegen_->GenerateLoadClassRuntimeCall(cls);
Roland Levillain5daa4952017-07-03 17:23:56 +01007407 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 13);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007408 return;
7409 }
Vladimir Marko41559982017-01-06 14:04:23 +00007410 DCHECK(!cls->NeedsAccessCheck());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007411
Vladimir Marko41559982017-01-06 14:04:23 +00007412 LocationSummary* locations = cls->GetLocations();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007413 Location out_loc = locations->Out();
7414 vixl32::Register out = OutputRegister(cls);
7415
Artem Serovd4cc5b22016-11-04 11:19:09 +00007416 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
7417 ? kWithoutReadBarrier
7418 : kCompilerReadBarrierOption;
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007419 bool generate_null_check = false;
Vladimir Marko41559982017-01-06 14:04:23 +00007420 switch (load_kind) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007421 case HLoadClass::LoadKind::kReferrersClass: {
7422 DCHECK(!cls->CanCallRuntime());
7423 DCHECK(!cls->MustGenerateClinitCheck());
7424 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
7425 vixl32::Register current_method = InputRegisterAt(cls, 0);
7426 GenerateGcRootFieldLoad(cls,
7427 out_loc,
7428 current_method,
Roland Levillain00468f32016-10-27 18:02:48 +01007429 ArtMethod::DeclaringClassOffset().Int32Value(),
Artem Serovd4cc5b22016-11-04 11:19:09 +00007430 read_barrier_option);
7431 break;
7432 }
Artem Serovd4cc5b22016-11-04 11:19:09 +00007433 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007434 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Artem Serovd4cc5b22016-11-04 11:19:09 +00007435 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
7436 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00007437 codegen_->NewBootImageTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
Artem Serovd4cc5b22016-11-04 11:19:09 +00007438 codegen_->EmitMovwMovtPlaceholder(labels, out);
7439 break;
7440 }
7441 case HLoadClass::LoadKind::kBootImageAddress: {
Artem Serovc5fcb442016-12-02 19:19:58 +00007442 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007443 uint32_t address = dchecked_integral_cast<uint32_t>(
7444 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
7445 DCHECK_NE(address, 0u);
Artem Serovc5fcb442016-12-02 19:19:58 +00007446 __ Ldr(out, codegen_->DeduplicateBootImageAddressLiteral(address));
Artem Serovd4cc5b22016-11-04 11:19:09 +00007447 break;
7448 }
Vladimir Marko94ec2db2017-09-06 17:21:03 +01007449 case HLoadClass::LoadKind::kBootImageClassTable: {
7450 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
7451 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00007452 codegen_->NewBootImageTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
Vladimir Marko94ec2db2017-09-06 17:21:03 +01007453 codegen_->EmitMovwMovtPlaceholder(labels, out);
7454 __ Ldr(out, MemOperand(out, /* offset */ 0));
7455 // Extract the reference from the slot data, i.e. clear the hash bits.
7456 int32_t masked_hash = ClassTable::TableSlot::MaskHash(
7457 ComputeModifiedUtf8Hash(cls->GetDexFile().StringByTypeIdx(cls->GetTypeIndex())));
7458 if (masked_hash != 0) {
7459 __ Sub(out, out, Operand(masked_hash));
7460 }
7461 break;
7462 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007463 case HLoadClass::LoadKind::kBssEntry: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007464 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
Vladimir Marko1998cd02017-01-13 13:02:58 +00007465 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex());
Vladimir Markof3c52b42017-11-17 17:32:12 +00007466 codegen_->EmitMovwMovtPlaceholder(labels, out);
7467 GenerateGcRootFieldLoad(cls, out_loc, out, /* offset */ 0, read_barrier_option);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007468 generate_null_check = true;
7469 break;
7470 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007471 case HLoadClass::LoadKind::kJitTableAddress: {
Artem Serovc5fcb442016-12-02 19:19:58 +00007472 __ Ldr(out, codegen_->DeduplicateJitClassLiteral(cls->GetDexFile(),
7473 cls->GetTypeIndex(),
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007474 cls->GetClass()));
Artem Serovc5fcb442016-12-02 19:19:58 +00007475 // /* GcRoot<mirror::Class> */ out = *out
Vladimir Markoea4c1262017-02-06 19:59:33 +00007476 GenerateGcRootFieldLoad(cls, out_loc, out, /* offset */ 0, read_barrier_option);
Artem Serovd4cc5b22016-11-04 11:19:09 +00007477 break;
7478 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007479 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00007480 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00007481 LOG(FATAL) << "UNREACHABLE";
7482 UNREACHABLE();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007483 }
7484
7485 if (generate_null_check || cls->MustGenerateClinitCheck()) {
7486 DCHECK(cls->CanCallRuntime());
Vladimir Marko174b2e22017-10-12 13:34:49 +01007487 LoadClassSlowPathARMVIXL* slow_path =
7488 new (codegen_->GetScopedAllocator()) LoadClassSlowPathARMVIXL(
7489 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007490 codegen_->AddSlowPath(slow_path);
7491 if (generate_null_check) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00007492 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007493 }
7494 if (cls->MustGenerateClinitCheck()) {
7495 GenerateClassInitializationCheck(slow_path, out);
7496 } else {
7497 __ Bind(slow_path->GetExitLabel());
7498 }
Roland Levillain5daa4952017-07-03 17:23:56 +01007499 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 14);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007500 }
7501}
7502
Artem Serov02d37832016-10-25 15:25:33 +01007503void LocationsBuilderARMVIXL::VisitClinitCheck(HClinitCheck* check) {
7504 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007505 new (GetGraph()->GetAllocator()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
Artem Serov02d37832016-10-25 15:25:33 +01007506 locations->SetInAt(0, Location::RequiresRegister());
7507 if (check->HasUses()) {
7508 locations->SetOut(Location::SameAsFirstInput());
7509 }
7510}
7511
7512void InstructionCodeGeneratorARMVIXL::VisitClinitCheck(HClinitCheck* check) {
7513 // We assume the class is not null.
7514 LoadClassSlowPathARMVIXL* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01007515 new (codegen_->GetScopedAllocator()) LoadClassSlowPathARMVIXL(check->GetLoadClass(),
7516 check,
7517 check->GetDexPc(),
7518 /* do_clinit */ true);
Artem Serov02d37832016-10-25 15:25:33 +01007519 codegen_->AddSlowPath(slow_path);
7520 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
7521}
7522
7523void InstructionCodeGeneratorARMVIXL::GenerateClassInitializationCheck(
7524 LoadClassSlowPathARMVIXL* slow_path, vixl32::Register class_reg) {
7525 UseScratchRegisterScope temps(GetVIXLAssembler());
7526 vixl32::Register temp = temps.Acquire();
Vladimir Markodc682aa2018-01-04 18:42:57 +00007527 constexpr size_t status_lsb_position = SubtypeCheckBits::BitStructSizeOf();
7528 const size_t status_byte_offset =
7529 mirror::Class::StatusOffset().SizeValue() + (status_lsb_position / kBitsPerByte);
7530 constexpr uint32_t shifted_initialized_value =
7531 enum_cast<uint32_t>(ClassStatus::kInitialized) << (status_lsb_position % kBitsPerByte);
7532
7533 GetAssembler()->LoadFromOffset(kLoadUnsignedByte, temp, class_reg, status_byte_offset);
7534 __ Cmp(temp, shifted_initialized_value);
Vladimir Marko2c64a832018-01-04 11:31:56 +00007535 __ B(lo, slow_path->GetEntryLabel());
Artem Serov02d37832016-10-25 15:25:33 +01007536 // Even if the initialized flag is set, we may be in a situation where caches are not synced
7537 // properly. Therefore, we do a memory fence.
7538 __ Dmb(ISH);
7539 __ Bind(slow_path->GetExitLabel());
7540}
7541
Artem Serov02d37832016-10-25 15:25:33 +01007542HLoadString::LoadKind CodeGeneratorARMVIXL::GetSupportedLoadStringKind(
Artem Serovd4cc5b22016-11-04 11:19:09 +00007543 HLoadString::LoadKind desired_string_load_kind) {
7544 switch (desired_string_load_kind) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00007545 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007546 case HLoadString::LoadKind::kBootImageInternTable:
Artem Serovd4cc5b22016-11-04 11:19:09 +00007547 case HLoadString::LoadKind::kBssEntry:
7548 DCHECK(!Runtime::Current()->UseJitCompilation());
7549 break;
7550 case HLoadString::LoadKind::kJitTableAddress:
7551 DCHECK(Runtime::Current()->UseJitCompilation());
Artem Serovc5fcb442016-12-02 19:19:58 +00007552 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01007553 case HLoadString::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007554 case HLoadString::LoadKind::kRuntimeCall:
Artem Serovd4cc5b22016-11-04 11:19:09 +00007555 break;
7556 }
7557 return desired_string_load_kind;
Artem Serov02d37832016-10-25 15:25:33 +01007558}
7559
7560void LocationsBuilderARMVIXL::VisitLoadString(HLoadString* load) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00007561 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Vladimir Markoca6fff82017-10-03 14:49:14 +01007562 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(load, call_kind);
Artem Serov02d37832016-10-25 15:25:33 +01007563 HLoadString::LoadKind load_kind = load->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007564 if (load_kind == HLoadString::LoadKind::kRuntimeCall) {
Artem Serov02d37832016-10-25 15:25:33 +01007565 locations->SetOut(LocationFrom(r0));
7566 } else {
7567 locations->SetOut(Location::RequiresRegister());
Artem Serovd4cc5b22016-11-04 11:19:09 +00007568 if (load_kind == HLoadString::LoadKind::kBssEntry) {
7569 if (!kUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00007570 // Rely on the pResolveString and marking to save everything we need, including temps.
Artem Serovd4cc5b22016-11-04 11:19:09 +00007571 RegisterSet caller_saves = RegisterSet::Empty();
7572 InvokeRuntimeCallingConventionARMVIXL calling_convention;
7573 caller_saves.Add(LocationFrom(calling_convention.GetRegisterAt(0)));
7574 // TODO: Add GetReturnLocation() to the calling convention so that we can DCHECK()
7575 // that the the kPrimNot result register is the same as the first argument register.
7576 locations->SetCustomSlowPathCallerSaves(caller_saves);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01007577 if (kUseBakerReadBarrier && kBakerReadBarrierLinkTimeThunksEnableForGcRoots) {
7578 locations->AddTemp(Location::RegisterLocation(kBakerCcEntrypointRegister.GetCode()));
7579 }
Artem Serovd4cc5b22016-11-04 11:19:09 +00007580 } else {
7581 // For non-Baker read barrier we have a temp-clobbering call.
7582 }
7583 }
Artem Serov02d37832016-10-25 15:25:33 +01007584 }
7585}
7586
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007587// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
7588// move.
7589void InstructionCodeGeneratorARMVIXL::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Artem Serovd4cc5b22016-11-04 11:19:09 +00007590 LocationSummary* locations = load->GetLocations();
7591 Location out_loc = locations->Out();
7592 vixl32::Register out = OutputRegister(load);
7593 HLoadString::LoadKind load_kind = load->GetLoadKind();
7594
7595 switch (load_kind) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00007596 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
7597 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
7598 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00007599 codegen_->NewBootImageStringPatch(load->GetDexFile(), load->GetStringIndex());
Artem Serovd4cc5b22016-11-04 11:19:09 +00007600 codegen_->EmitMovwMovtPlaceholder(labels, out);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007601 return;
Artem Serovd4cc5b22016-11-04 11:19:09 +00007602 }
7603 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007604 uint32_t address = dchecked_integral_cast<uint32_t>(
7605 reinterpret_cast<uintptr_t>(load->GetString().Get()));
7606 DCHECK_NE(address, 0u);
Artem Serovc5fcb442016-12-02 19:19:58 +00007607 __ Ldr(out, codegen_->DeduplicateBootImageAddressLiteral(address));
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007608 return;
7609 }
7610 case HLoadString::LoadKind::kBootImageInternTable: {
7611 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
7612 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00007613 codegen_->NewBootImageStringPatch(load->GetDexFile(), load->GetStringIndex());
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007614 codegen_->EmitMovwMovtPlaceholder(labels, out);
7615 __ Ldr(out, MemOperand(out, /* offset */ 0));
7616 return;
Artem Serovd4cc5b22016-11-04 11:19:09 +00007617 }
7618 case HLoadString::LoadKind::kBssEntry: {
7619 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Artem Serovd4cc5b22016-11-04 11:19:09 +00007620 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007621 codegen_->NewStringBssEntryPatch(load->GetDexFile(), load->GetStringIndex());
Vladimir Markof3c52b42017-11-17 17:32:12 +00007622 codegen_->EmitMovwMovtPlaceholder(labels, out);
7623 GenerateGcRootFieldLoad(load, out_loc, out, /* offset */ 0, kCompilerReadBarrierOption);
Artem Serovd4cc5b22016-11-04 11:19:09 +00007624 LoadStringSlowPathARMVIXL* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01007625 new (codegen_->GetScopedAllocator()) LoadStringSlowPathARMVIXL(load);
Artem Serovd4cc5b22016-11-04 11:19:09 +00007626 codegen_->AddSlowPath(slow_path);
7627 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
7628 __ Bind(slow_path->GetExitLabel());
Roland Levillain5daa4952017-07-03 17:23:56 +01007629 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 15);
Artem Serovd4cc5b22016-11-04 11:19:09 +00007630 return;
7631 }
7632 case HLoadString::LoadKind::kJitTableAddress: {
Artem Serovc5fcb442016-12-02 19:19:58 +00007633 __ Ldr(out, codegen_->DeduplicateJitStringLiteral(load->GetDexFile(),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007634 load->GetStringIndex(),
7635 load->GetString()));
Artem Serovc5fcb442016-12-02 19:19:58 +00007636 // /* GcRoot<mirror::String> */ out = *out
7637 GenerateGcRootFieldLoad(load, out_loc, out, /* offset */ 0, kCompilerReadBarrierOption);
7638 return;
Artem Serovd4cc5b22016-11-04 11:19:09 +00007639 }
7640 default:
7641 break;
7642 }
Artem Serov02d37832016-10-25 15:25:33 +01007643
7644 // TODO: Re-add the compiler code to do string dex cache lookup again.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007645 DCHECK_EQ(load->GetLoadKind(), HLoadString::LoadKind::kRuntimeCall);
Artem Serov02d37832016-10-25 15:25:33 +01007646 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Andreas Gampe8a0128a2016-11-28 07:38:35 -08007647 __ Mov(calling_convention.GetRegisterAt(0), load->GetStringIndex().index_);
Artem Serov02d37832016-10-25 15:25:33 +01007648 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
7649 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Roland Levillain5daa4952017-07-03 17:23:56 +01007650 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 16);
Artem Serov02d37832016-10-25 15:25:33 +01007651}
7652
7653static int32_t GetExceptionTlsOffset() {
7654 return Thread::ExceptionOffset<kArmPointerSize>().Int32Value();
7655}
7656
7657void LocationsBuilderARMVIXL::VisitLoadException(HLoadException* load) {
7658 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007659 new (GetGraph()->GetAllocator()) LocationSummary(load, LocationSummary::kNoCall);
Artem Serov02d37832016-10-25 15:25:33 +01007660 locations->SetOut(Location::RequiresRegister());
7661}
7662
7663void InstructionCodeGeneratorARMVIXL::VisitLoadException(HLoadException* load) {
7664 vixl32::Register out = OutputRegister(load);
7665 GetAssembler()->LoadFromOffset(kLoadWord, out, tr, GetExceptionTlsOffset());
7666}
7667
7668
7669void LocationsBuilderARMVIXL::VisitClearException(HClearException* clear) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007670 new (GetGraph()->GetAllocator()) LocationSummary(clear, LocationSummary::kNoCall);
Artem Serov02d37832016-10-25 15:25:33 +01007671}
7672
7673void InstructionCodeGeneratorARMVIXL::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
7674 UseScratchRegisterScope temps(GetVIXLAssembler());
7675 vixl32::Register temp = temps.Acquire();
7676 __ Mov(temp, 0);
7677 GetAssembler()->StoreToOffset(kStoreWord, temp, tr, GetExceptionTlsOffset());
7678}
7679
7680void LocationsBuilderARMVIXL::VisitThrow(HThrow* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007681 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
7682 instruction, LocationSummary::kCallOnMainOnly);
Artem Serov02d37832016-10-25 15:25:33 +01007683 InvokeRuntimeCallingConventionARMVIXL calling_convention;
7684 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
7685}
7686
7687void InstructionCodeGeneratorARMVIXL::VisitThrow(HThrow* instruction) {
7688 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
7689 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
7690}
7691
Artem Serov657022c2016-11-23 14:19:38 +00007692// Temp is used for read barrier.
7693static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
7694 if (kEmitCompilerReadBarrier &&
7695 (kUseBakerReadBarrier ||
7696 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
7697 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
7698 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
7699 return 1;
7700 }
7701 return 0;
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007702}
7703
Artem Serov657022c2016-11-23 14:19:38 +00007704// Interface case has 3 temps, one for holding the number of interfaces, one for the current
7705// interface pointer, one for loading the current interface.
7706// The other checks have one temp for loading the object's class.
7707static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
7708 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
7709 return 3;
7710 }
7711 return 1 + NumberOfInstanceOfTemps(type_check_kind);
7712}
Artem Serovcfbe9132016-10-14 15:58:56 +01007713
7714void LocationsBuilderARMVIXL::VisitInstanceOf(HInstanceOf* instruction) {
7715 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
7716 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
7717 bool baker_read_barrier_slow_path = false;
7718 switch (type_check_kind) {
7719 case TypeCheckKind::kExactCheck:
7720 case TypeCheckKind::kAbstractClassCheck:
7721 case TypeCheckKind::kClassHierarchyCheck:
Vladimir Marko87584542017-12-12 17:47:52 +00007722 case TypeCheckKind::kArrayObjectCheck: {
7723 bool needs_read_barrier = CodeGenerator::InstanceOfNeedsReadBarrier(instruction);
7724 call_kind = needs_read_barrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
7725 baker_read_barrier_slow_path = kUseBakerReadBarrier && needs_read_barrier;
Artem Serovcfbe9132016-10-14 15:58:56 +01007726 break;
Vladimir Marko87584542017-12-12 17:47:52 +00007727 }
Artem Serovcfbe9132016-10-14 15:58:56 +01007728 case TypeCheckKind::kArrayCheck:
7729 case TypeCheckKind::kUnresolvedCheck:
7730 case TypeCheckKind::kInterfaceCheck:
7731 call_kind = LocationSummary::kCallOnSlowPath;
7732 break;
7733 }
7734
Vladimir Markoca6fff82017-10-03 14:49:14 +01007735 LocationSummary* locations =
7736 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Artem Serovcfbe9132016-10-14 15:58:56 +01007737 if (baker_read_barrier_slow_path) {
7738 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
7739 }
7740 locations->SetInAt(0, Location::RequiresRegister());
7741 locations->SetInAt(1, Location::RequiresRegister());
7742 // The "out" register is used as a temporary, so it overlaps with the inputs.
7743 // Note that TypeCheckSlowPathARM uses this register too.
7744 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Artem Serov657022c2016-11-23 14:19:38 +00007745 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01007746 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
7747 codegen_->MaybeAddBakerCcEntrypointTempForFields(locations);
7748 }
Artem Serovcfbe9132016-10-14 15:58:56 +01007749}
7750
7751void InstructionCodeGeneratorARMVIXL::VisitInstanceOf(HInstanceOf* instruction) {
7752 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
7753 LocationSummary* locations = instruction->GetLocations();
7754 Location obj_loc = locations->InAt(0);
7755 vixl32::Register obj = InputRegisterAt(instruction, 0);
7756 vixl32::Register cls = InputRegisterAt(instruction, 1);
7757 Location out_loc = locations->Out();
7758 vixl32::Register out = OutputRegister(instruction);
Artem Serov657022c2016-11-23 14:19:38 +00007759 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
7760 DCHECK_LE(num_temps, 1u);
7761 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Artem Serovcfbe9132016-10-14 15:58:56 +01007762 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
7763 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
7764 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
7765 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007766 vixl32::Label done;
7767 vixl32::Label* const final_label = codegen_->GetFinalLabel(instruction, &done);
Artem Serovcfbe9132016-10-14 15:58:56 +01007768 SlowPathCodeARMVIXL* slow_path = nullptr;
7769
7770 // Return 0 if `obj` is null.
7771 // avoid null check if we know obj is not null.
7772 if (instruction->MustDoNullCheck()) {
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007773 DCHECK(!out.Is(obj));
7774 __ Mov(out, 0);
7775 __ CompareAndBranchIfZero(obj, final_label, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007776 }
7777
Artem Serovcfbe9132016-10-14 15:58:56 +01007778 switch (type_check_kind) {
7779 case TypeCheckKind::kExactCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00007780 ReadBarrierOption read_barrier_option =
7781 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier6beced42016-11-15 15:51:31 -08007782 // /* HeapReference<Class> */ out = obj->klass_
7783 GenerateReferenceLoadTwoRegisters(instruction,
7784 out_loc,
7785 obj_loc,
7786 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00007787 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007788 read_barrier_option);
Artem Serovcfbe9132016-10-14 15:58:56 +01007789 // Classes must be equal for the instanceof to succeed.
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007790 __ Cmp(out, cls);
7791 // We speculatively set the result to false without changing the condition
7792 // flags, which allows us to avoid some branching later.
7793 __ Mov(LeaveFlags, out, 0);
7794
7795 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
7796 // we check that the output is in a low register, so that a 16-bit MOV
7797 // encoding can be used.
7798 if (out.IsLow()) {
7799 // We use the scope because of the IT block that follows.
7800 ExactAssemblyScope guard(GetVIXLAssembler(),
7801 2 * vixl32::k16BitT32InstructionSizeInBytes,
7802 CodeBufferCheckScope::kExactSize);
7803
7804 __ it(eq);
7805 __ mov(eq, out, 1);
7806 } else {
7807 __ B(ne, final_label, /* far_target */ false);
7808 __ Mov(out, 1);
7809 }
7810
Artem Serovcfbe9132016-10-14 15:58:56 +01007811 break;
7812 }
7813
7814 case TypeCheckKind::kAbstractClassCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00007815 ReadBarrierOption read_barrier_option =
7816 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier6beced42016-11-15 15:51:31 -08007817 // /* HeapReference<Class> */ out = obj->klass_
7818 GenerateReferenceLoadTwoRegisters(instruction,
7819 out_loc,
7820 obj_loc,
7821 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00007822 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007823 read_barrier_option);
Artem Serovcfbe9132016-10-14 15:58:56 +01007824 // If the class is abstract, we eagerly fetch the super class of the
7825 // object to avoid doing a comparison we know will fail.
7826 vixl32::Label loop;
7827 __ Bind(&loop);
7828 // /* HeapReference<Class> */ out = out->super_class_
Artem Serov657022c2016-11-23 14:19:38 +00007829 GenerateReferenceLoadOneRegister(instruction,
7830 out_loc,
7831 super_offset,
7832 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007833 read_barrier_option);
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007834 // If `out` is null, we use it for the result, and jump to the final label.
Anton Kirilov6f644202017-02-27 18:29:45 +00007835 __ CompareAndBranchIfZero(out, final_label, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007836 __ Cmp(out, cls);
Artem Serov517d9f62016-12-12 15:51:15 +00007837 __ B(ne, &loop, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007838 __ Mov(out, 1);
Artem Serovcfbe9132016-10-14 15:58:56 +01007839 break;
7840 }
7841
7842 case TypeCheckKind::kClassHierarchyCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00007843 ReadBarrierOption read_barrier_option =
7844 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier6beced42016-11-15 15:51:31 -08007845 // /* HeapReference<Class> */ out = obj->klass_
7846 GenerateReferenceLoadTwoRegisters(instruction,
7847 out_loc,
7848 obj_loc,
7849 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00007850 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007851 read_barrier_option);
Artem Serovcfbe9132016-10-14 15:58:56 +01007852 // Walk over the class hierarchy to find a match.
7853 vixl32::Label loop, success;
7854 __ Bind(&loop);
7855 __ Cmp(out, cls);
Artem Serov517d9f62016-12-12 15:51:15 +00007856 __ B(eq, &success, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007857 // /* HeapReference<Class> */ out = out->super_class_
Artem Serov657022c2016-11-23 14:19:38 +00007858 GenerateReferenceLoadOneRegister(instruction,
7859 out_loc,
7860 super_offset,
7861 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007862 read_barrier_option);
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007863 // This is essentially a null check, but it sets the condition flags to the
7864 // proper value for the code that follows the loop, i.e. not `eq`.
7865 __ Cmp(out, 1);
7866 __ B(hs, &loop, /* far_target */ false);
7867
7868 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
7869 // we check that the output is in a low register, so that a 16-bit MOV
7870 // encoding can be used.
7871 if (out.IsLow()) {
7872 // If `out` is null, we use it for the result, and the condition flags
7873 // have already been set to `ne`, so the IT block that comes afterwards
7874 // (and which handles the successful case) turns into a NOP (instead of
7875 // overwriting `out`).
7876 __ Bind(&success);
7877
7878 // We use the scope because of the IT block that follows.
7879 ExactAssemblyScope guard(GetVIXLAssembler(),
7880 2 * vixl32::k16BitT32InstructionSizeInBytes,
7881 CodeBufferCheckScope::kExactSize);
7882
7883 // There is only one branch to the `success` label (which is bound to this
7884 // IT block), and it has the same condition, `eq`, so in that case the MOV
7885 // is executed.
7886 __ it(eq);
7887 __ mov(eq, out, 1);
7888 } else {
7889 // If `out` is null, we use it for the result, and jump to the final label.
Anton Kirilov6f644202017-02-27 18:29:45 +00007890 __ B(final_label);
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007891 __ Bind(&success);
7892 __ Mov(out, 1);
Artem Serovcfbe9132016-10-14 15:58:56 +01007893 }
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007894
Artem Serovcfbe9132016-10-14 15:58:56 +01007895 break;
7896 }
7897
7898 case TypeCheckKind::kArrayObjectCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00007899 ReadBarrierOption read_barrier_option =
7900 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier6beced42016-11-15 15:51:31 -08007901 // /* HeapReference<Class> */ out = obj->klass_
7902 GenerateReferenceLoadTwoRegisters(instruction,
7903 out_loc,
7904 obj_loc,
7905 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00007906 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007907 read_barrier_option);
Artem Serovcfbe9132016-10-14 15:58:56 +01007908 // Do an exact check.
7909 vixl32::Label exact_check;
7910 __ Cmp(out, cls);
Artem Serov517d9f62016-12-12 15:51:15 +00007911 __ B(eq, &exact_check, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007912 // Otherwise, we need to check that the object's class is a non-primitive array.
7913 // /* HeapReference<Class> */ out = out->component_type_
Artem Serov657022c2016-11-23 14:19:38 +00007914 GenerateReferenceLoadOneRegister(instruction,
7915 out_loc,
7916 component_offset,
7917 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007918 read_barrier_option);
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007919 // If `out` is null, we use it for the result, and jump to the final label.
Anton Kirilov6f644202017-02-27 18:29:45 +00007920 __ CompareAndBranchIfZero(out, final_label, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007921 GetAssembler()->LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
7922 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007923 __ Cmp(out, 0);
7924 // We speculatively set the result to false without changing the condition
7925 // flags, which allows us to avoid some branching later.
7926 __ Mov(LeaveFlags, out, 0);
7927
7928 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
7929 // we check that the output is in a low register, so that a 16-bit MOV
7930 // encoding can be used.
7931 if (out.IsLow()) {
7932 __ Bind(&exact_check);
7933
7934 // We use the scope because of the IT block that follows.
7935 ExactAssemblyScope guard(GetVIXLAssembler(),
7936 2 * vixl32::k16BitT32InstructionSizeInBytes,
7937 CodeBufferCheckScope::kExactSize);
7938
7939 __ it(eq);
7940 __ mov(eq, out, 1);
7941 } else {
7942 __ B(ne, final_label, /* far_target */ false);
7943 __ Bind(&exact_check);
7944 __ Mov(out, 1);
7945 }
7946
Artem Serovcfbe9132016-10-14 15:58:56 +01007947 break;
7948 }
7949
7950 case TypeCheckKind::kArrayCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00007951 // No read barrier since the slow path will retry upon failure.
Mathieu Chartier6beced42016-11-15 15:51:31 -08007952 // /* HeapReference<Class> */ out = obj->klass_
7953 GenerateReferenceLoadTwoRegisters(instruction,
7954 out_loc,
7955 obj_loc,
7956 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00007957 maybe_temp_loc,
7958 kWithoutReadBarrier);
Artem Serovcfbe9132016-10-14 15:58:56 +01007959 __ Cmp(out, cls);
7960 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01007961 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathARMVIXL(
7962 instruction, /* is_fatal */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007963 codegen_->AddSlowPath(slow_path);
7964 __ B(ne, slow_path->GetEntryLabel());
7965 __ Mov(out, 1);
Artem Serovcfbe9132016-10-14 15:58:56 +01007966 break;
7967 }
7968
7969 case TypeCheckKind::kUnresolvedCheck:
7970 case TypeCheckKind::kInterfaceCheck: {
7971 // Note that we indeed only call on slow path, but we always go
7972 // into the slow path for the unresolved and interface check
7973 // cases.
7974 //
7975 // We cannot directly call the InstanceofNonTrivial runtime
7976 // entry point without resorting to a type checking slow path
7977 // here (i.e. by calling InvokeRuntime directly), as it would
7978 // require to assign fixed registers for the inputs of this
7979 // HInstanceOf instruction (following the runtime calling
7980 // convention), which might be cluttered by the potential first
7981 // read barrier emission at the beginning of this method.
7982 //
7983 // TODO: Introduce a new runtime entry point taking the object
7984 // to test (instead of its class) as argument, and let it deal
7985 // with the read barrier issues. This will let us refactor this
7986 // case of the `switch` code as it was previously (with a direct
7987 // call to the runtime not using a type checking slow path).
7988 // This should also be beneficial for the other cases above.
7989 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01007990 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathARMVIXL(
7991 instruction, /* is_fatal */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007992 codegen_->AddSlowPath(slow_path);
7993 __ B(slow_path->GetEntryLabel());
Artem Serovcfbe9132016-10-14 15:58:56 +01007994 break;
7995 }
7996 }
7997
Artem Serovcfbe9132016-10-14 15:58:56 +01007998 if (done.IsReferenced()) {
7999 __ Bind(&done);
8000 }
8001
8002 if (slow_path != nullptr) {
8003 __ Bind(slow_path->GetExitLabel());
8004 }
8005}
8006
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008007void LocationsBuilderARMVIXL::VisitCheckCast(HCheckCast* instruction) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008008 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko87584542017-12-12 17:47:52 +00008009 LocationSummary::CallKind call_kind = CodeGenerator::GetCheckCastCallKind(instruction);
Vladimir Markoca6fff82017-10-03 14:49:14 +01008010 LocationSummary* locations =
8011 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008012 locations->SetInAt(0, Location::RequiresRegister());
8013 locations->SetInAt(1, Location::RequiresRegister());
Artem Serov657022c2016-11-23 14:19:38 +00008014 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008015}
8016
8017void InstructionCodeGeneratorARMVIXL::VisitCheckCast(HCheckCast* instruction) {
8018 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
8019 LocationSummary* locations = instruction->GetLocations();
8020 Location obj_loc = locations->InAt(0);
8021 vixl32::Register obj = InputRegisterAt(instruction, 0);
8022 vixl32::Register cls = InputRegisterAt(instruction, 1);
8023 Location temp_loc = locations->GetTemp(0);
8024 vixl32::Register temp = RegisterFrom(temp_loc);
Artem Serov657022c2016-11-23 14:19:38 +00008025 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
8026 DCHECK_LE(num_temps, 3u);
8027 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
8028 Location maybe_temp3_loc = (num_temps >= 3) ? locations->GetTemp(2) : Location::NoLocation();
8029 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
8030 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
8031 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
8032 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
8033 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
8034 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
8035 const uint32_t object_array_data_offset =
8036 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008037
Vladimir Marko87584542017-12-12 17:47:52 +00008038 bool is_type_check_slow_path_fatal = CodeGenerator::IsTypeCheckSlowPathFatal(instruction);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008039 SlowPathCodeARMVIXL* type_check_slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01008040 new (codegen_->GetScopedAllocator()) TypeCheckSlowPathARMVIXL(
8041 instruction, is_type_check_slow_path_fatal);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008042 codegen_->AddSlowPath(type_check_slow_path);
8043
8044 vixl32::Label done;
Anton Kirilov6f644202017-02-27 18:29:45 +00008045 vixl32::Label* final_label = codegen_->GetFinalLabel(instruction, &done);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008046 // Avoid null check if we know obj is not null.
8047 if (instruction->MustDoNullCheck()) {
Anton Kirilov6f644202017-02-27 18:29:45 +00008048 __ CompareAndBranchIfZero(obj, final_label, /* far_target */ false);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008049 }
8050
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008051 switch (type_check_kind) {
8052 case TypeCheckKind::kExactCheck:
8053 case TypeCheckKind::kArrayCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00008054 // /* HeapReference<Class> */ temp = obj->klass_
8055 GenerateReferenceLoadTwoRegisters(instruction,
8056 temp_loc,
8057 obj_loc,
8058 class_offset,
8059 maybe_temp2_loc,
8060 kWithoutReadBarrier);
8061
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008062 __ Cmp(temp, cls);
8063 // Jump to slow path for throwing the exception or doing a
8064 // more involved array check.
8065 __ B(ne, type_check_slow_path->GetEntryLabel());
8066 break;
8067 }
8068
8069 case TypeCheckKind::kAbstractClassCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00008070 // /* HeapReference<Class> */ temp = obj->klass_
8071 GenerateReferenceLoadTwoRegisters(instruction,
8072 temp_loc,
8073 obj_loc,
8074 class_offset,
8075 maybe_temp2_loc,
8076 kWithoutReadBarrier);
8077
Artem Serovcfbe9132016-10-14 15:58:56 +01008078 // If the class is abstract, we eagerly fetch the super class of the
8079 // object to avoid doing a comparison we know will fail.
8080 vixl32::Label loop;
8081 __ Bind(&loop);
8082 // /* HeapReference<Class> */ temp = temp->super_class_
Artem Serov657022c2016-11-23 14:19:38 +00008083 GenerateReferenceLoadOneRegister(instruction,
8084 temp_loc,
8085 super_offset,
8086 maybe_temp2_loc,
8087 kWithoutReadBarrier);
Artem Serovcfbe9132016-10-14 15:58:56 +01008088
8089 // If the class reference currently in `temp` is null, jump to the slow path to throw the
8090 // exception.
xueliang.zhongf51bc622016-11-04 09:23:32 +00008091 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
Artem Serovcfbe9132016-10-14 15:58:56 +01008092
8093 // Otherwise, compare the classes.
8094 __ Cmp(temp, cls);
Artem Serov517d9f62016-12-12 15:51:15 +00008095 __ B(ne, &loop, /* far_target */ false);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008096 break;
8097 }
8098
8099 case TypeCheckKind::kClassHierarchyCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00008100 // /* HeapReference<Class> */ temp = obj->klass_
8101 GenerateReferenceLoadTwoRegisters(instruction,
8102 temp_loc,
8103 obj_loc,
8104 class_offset,
8105 maybe_temp2_loc,
8106 kWithoutReadBarrier);
8107
Artem Serovcfbe9132016-10-14 15:58:56 +01008108 // Walk over the class hierarchy to find a match.
8109 vixl32::Label loop;
8110 __ Bind(&loop);
8111 __ Cmp(temp, cls);
Anton Kirilov6f644202017-02-27 18:29:45 +00008112 __ B(eq, final_label, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01008113
8114 // /* HeapReference<Class> */ temp = temp->super_class_
Artem Serov657022c2016-11-23 14:19:38 +00008115 GenerateReferenceLoadOneRegister(instruction,
8116 temp_loc,
8117 super_offset,
8118 maybe_temp2_loc,
8119 kWithoutReadBarrier);
Artem Serovcfbe9132016-10-14 15:58:56 +01008120
8121 // If the class reference currently in `temp` is null, jump to the slow path to throw the
8122 // exception.
xueliang.zhongf51bc622016-11-04 09:23:32 +00008123 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
Artem Serovcfbe9132016-10-14 15:58:56 +01008124 // Otherwise, jump to the beginning of the loop.
8125 __ B(&loop);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008126 break;
8127 }
8128
Artem Serovcfbe9132016-10-14 15:58:56 +01008129 case TypeCheckKind::kArrayObjectCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00008130 // /* HeapReference<Class> */ temp = obj->klass_
8131 GenerateReferenceLoadTwoRegisters(instruction,
8132 temp_loc,
8133 obj_loc,
8134 class_offset,
8135 maybe_temp2_loc,
8136 kWithoutReadBarrier);
8137
Artem Serovcfbe9132016-10-14 15:58:56 +01008138 // Do an exact check.
8139 __ Cmp(temp, cls);
Anton Kirilov6f644202017-02-27 18:29:45 +00008140 __ B(eq, final_label, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01008141
8142 // Otherwise, we need to check that the object's class is a non-primitive array.
8143 // /* HeapReference<Class> */ temp = temp->component_type_
Artem Serov657022c2016-11-23 14:19:38 +00008144 GenerateReferenceLoadOneRegister(instruction,
8145 temp_loc,
8146 component_offset,
8147 maybe_temp2_loc,
8148 kWithoutReadBarrier);
Artem Serovcfbe9132016-10-14 15:58:56 +01008149 // If the component type is null, jump to the slow path to throw the exception.
xueliang.zhongf51bc622016-11-04 09:23:32 +00008150 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
Artem Serovcfbe9132016-10-14 15:58:56 +01008151 // Otherwise,the object is indeed an array, jump to label `check_non_primitive_component_type`
8152 // to further check that this component type is not a primitive type.
8153 GetAssembler()->LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008154 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
xueliang.zhongf51bc622016-11-04 09:23:32 +00008155 __ CompareAndBranchIfNonZero(temp, type_check_slow_path->GetEntryLabel());
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008156 break;
8157 }
8158
8159 case TypeCheckKind::kUnresolvedCheck:
Artem Serov657022c2016-11-23 14:19:38 +00008160 // We always go into the type check slow path for the unresolved check case.
Artem Serovcfbe9132016-10-14 15:58:56 +01008161 // We cannot directly call the CheckCast runtime entry point
8162 // without resorting to a type checking slow path here (i.e. by
8163 // calling InvokeRuntime directly), as it would require to
8164 // assign fixed registers for the inputs of this HInstanceOf
8165 // instruction (following the runtime calling convention), which
8166 // might be cluttered by the potential first read barrier
8167 // emission at the beginning of this method.
Artem Serov657022c2016-11-23 14:19:38 +00008168
Artem Serovcfbe9132016-10-14 15:58:56 +01008169 __ B(type_check_slow_path->GetEntryLabel());
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008170 break;
Artem Serov657022c2016-11-23 14:19:38 +00008171
8172 case TypeCheckKind::kInterfaceCheck: {
8173 // Avoid read barriers to improve performance of the fast path. We can not get false
8174 // positives by doing this.
8175 // /* HeapReference<Class> */ temp = obj->klass_
8176 GenerateReferenceLoadTwoRegisters(instruction,
8177 temp_loc,
8178 obj_loc,
8179 class_offset,
8180 maybe_temp2_loc,
8181 kWithoutReadBarrier);
8182
8183 // /* HeapReference<Class> */ temp = temp->iftable_
8184 GenerateReferenceLoadTwoRegisters(instruction,
8185 temp_loc,
8186 temp_loc,
8187 iftable_offset,
8188 maybe_temp2_loc,
8189 kWithoutReadBarrier);
8190 // Iftable is never null.
8191 __ Ldr(RegisterFrom(maybe_temp2_loc), MemOperand(temp, array_length_offset));
8192 // Loop through the iftable and check if any class matches.
8193 vixl32::Label start_loop;
8194 __ Bind(&start_loop);
8195 __ CompareAndBranchIfZero(RegisterFrom(maybe_temp2_loc),
8196 type_check_slow_path->GetEntryLabel());
8197 __ Ldr(RegisterFrom(maybe_temp3_loc), MemOperand(temp, object_array_data_offset));
8198 GetAssembler()->MaybeUnpoisonHeapReference(RegisterFrom(maybe_temp3_loc));
8199 // Go to next interface.
8200 __ Add(temp, temp, Operand::From(2 * kHeapReferenceSize));
8201 __ Sub(RegisterFrom(maybe_temp2_loc), RegisterFrom(maybe_temp2_loc), 2);
8202 // Compare the classes and continue the loop if they do not match.
8203 __ Cmp(cls, RegisterFrom(maybe_temp3_loc));
Artem Serov517d9f62016-12-12 15:51:15 +00008204 __ B(ne, &start_loop, /* far_target */ false);
Artem Serov657022c2016-11-23 14:19:38 +00008205 break;
8206 }
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008207 }
Anton Kirilov6f644202017-02-27 18:29:45 +00008208 if (done.IsReferenced()) {
8209 __ Bind(&done);
8210 }
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008211
8212 __ Bind(type_check_slow_path->GetExitLabel());
8213}
8214
Artem Serov551b28f2016-10-18 19:11:30 +01008215void LocationsBuilderARMVIXL::VisitMonitorOperation(HMonitorOperation* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008216 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
8217 instruction, LocationSummary::kCallOnMainOnly);
Artem Serov551b28f2016-10-18 19:11:30 +01008218 InvokeRuntimeCallingConventionARMVIXL calling_convention;
8219 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
8220}
8221
8222void InstructionCodeGeneratorARMVIXL::VisitMonitorOperation(HMonitorOperation* instruction) {
8223 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
8224 instruction,
8225 instruction->GetDexPc());
8226 if (instruction->IsEnter()) {
8227 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
8228 } else {
8229 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
8230 }
Roland Levillain5daa4952017-07-03 17:23:56 +01008231 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 17);
Artem Serov551b28f2016-10-18 19:11:30 +01008232}
8233
Artem Serov02109dd2016-09-23 17:17:54 +01008234void LocationsBuilderARMVIXL::VisitAnd(HAnd* instruction) {
8235 HandleBitwiseOperation(instruction, AND);
8236}
8237
8238void LocationsBuilderARMVIXL::VisitOr(HOr* instruction) {
8239 HandleBitwiseOperation(instruction, ORR);
8240}
8241
8242void LocationsBuilderARMVIXL::VisitXor(HXor* instruction) {
8243 HandleBitwiseOperation(instruction, EOR);
8244}
8245
8246void LocationsBuilderARMVIXL::HandleBitwiseOperation(HBinaryOperation* instruction, Opcode opcode) {
8247 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008248 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008249 DCHECK(instruction->GetResultType() == DataType::Type::kInt32
8250 || instruction->GetResultType() == DataType::Type::kInt64);
Artem Serov02109dd2016-09-23 17:17:54 +01008251 // Note: GVN reorders commutative operations to have the constant on the right hand side.
8252 locations->SetInAt(0, Location::RequiresRegister());
8253 locations->SetInAt(1, ArmEncodableConstantOrRegister(instruction->InputAt(1), opcode));
8254 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8255}
8256
8257void InstructionCodeGeneratorARMVIXL::VisitAnd(HAnd* instruction) {
8258 HandleBitwiseOperation(instruction);
8259}
8260
8261void InstructionCodeGeneratorARMVIXL::VisitOr(HOr* instruction) {
8262 HandleBitwiseOperation(instruction);
8263}
8264
8265void InstructionCodeGeneratorARMVIXL::VisitXor(HXor* instruction) {
8266 HandleBitwiseOperation(instruction);
8267}
8268
Artem Serov2bbc9532016-10-21 11:51:50 +01008269void LocationsBuilderARMVIXL::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
8270 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008271 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008272 DCHECK(instruction->GetResultType() == DataType::Type::kInt32
8273 || instruction->GetResultType() == DataType::Type::kInt64);
Artem Serov2bbc9532016-10-21 11:51:50 +01008274
8275 locations->SetInAt(0, Location::RequiresRegister());
8276 locations->SetInAt(1, Location::RequiresRegister());
8277 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8278}
8279
8280void InstructionCodeGeneratorARMVIXL::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
8281 LocationSummary* locations = instruction->GetLocations();
8282 Location first = locations->InAt(0);
8283 Location second = locations->InAt(1);
8284 Location out = locations->Out();
8285
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008286 if (instruction->GetResultType() == DataType::Type::kInt32) {
Artem Serov2bbc9532016-10-21 11:51:50 +01008287 vixl32::Register first_reg = RegisterFrom(first);
8288 vixl32::Register second_reg = RegisterFrom(second);
8289 vixl32::Register out_reg = RegisterFrom(out);
8290
8291 switch (instruction->GetOpKind()) {
8292 case HInstruction::kAnd:
8293 __ Bic(out_reg, first_reg, second_reg);
8294 break;
8295 case HInstruction::kOr:
8296 __ Orn(out_reg, first_reg, second_reg);
8297 break;
8298 // There is no EON on arm.
8299 case HInstruction::kXor:
8300 default:
8301 LOG(FATAL) << "Unexpected instruction " << instruction->DebugName();
8302 UNREACHABLE();
8303 }
8304 return;
8305
8306 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008307 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
Artem Serov2bbc9532016-10-21 11:51:50 +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
8315 switch (instruction->GetOpKind()) {
8316 case HInstruction::kAnd:
8317 __ Bic(out_low, first_low, second_low);
8318 __ Bic(out_high, first_high, second_high);
8319 break;
8320 case HInstruction::kOr:
8321 __ Orn(out_low, first_low, second_low);
8322 __ Orn(out_high, first_high, second_high);
8323 break;
8324 // There is no EON on arm.
8325 case HInstruction::kXor:
8326 default:
8327 LOG(FATAL) << "Unexpected instruction " << instruction->DebugName();
8328 UNREACHABLE();
8329 }
8330 }
8331}
8332
Anton Kirilov74234da2017-01-13 14:42:47 +00008333void LocationsBuilderARMVIXL::VisitDataProcWithShifterOp(
8334 HDataProcWithShifterOp* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008335 DCHECK(instruction->GetType() == DataType::Type::kInt32 ||
8336 instruction->GetType() == DataType::Type::kInt64);
Anton Kirilov74234da2017-01-13 14:42:47 +00008337 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008338 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008339 const bool overlap = instruction->GetType() == DataType::Type::kInt64 &&
Anton Kirilov74234da2017-01-13 14:42:47 +00008340 HDataProcWithShifterOp::IsExtensionOp(instruction->GetOpKind());
8341
8342 locations->SetInAt(0, Location::RequiresRegister());
8343 locations->SetInAt(1, Location::RequiresRegister());
8344 locations->SetOut(Location::RequiresRegister(),
8345 overlap ? Location::kOutputOverlap : Location::kNoOutputOverlap);
8346}
8347
8348void InstructionCodeGeneratorARMVIXL::VisitDataProcWithShifterOp(
8349 HDataProcWithShifterOp* instruction) {
8350 const LocationSummary* const locations = instruction->GetLocations();
8351 const HInstruction::InstructionKind kind = instruction->GetInstrKind();
8352 const HDataProcWithShifterOp::OpKind op_kind = instruction->GetOpKind();
8353
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008354 if (instruction->GetType() == DataType::Type::kInt32) {
Anton Kirilov420ee302017-02-21 18:10:26 +00008355 const vixl32::Register first = InputRegisterAt(instruction, 0);
8356 const vixl32::Register output = OutputRegister(instruction);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008357 const vixl32::Register second = instruction->InputAt(1)->GetType() == DataType::Type::kInt64
Anton Kirilov74234da2017-01-13 14:42:47 +00008358 ? LowRegisterFrom(locations->InAt(1))
8359 : InputRegisterAt(instruction, 1);
8360
Anton Kirilov420ee302017-02-21 18:10:26 +00008361 if (HDataProcWithShifterOp::IsExtensionOp(op_kind)) {
8362 DCHECK_EQ(kind, HInstruction::kAdd);
8363
8364 switch (op_kind) {
8365 case HDataProcWithShifterOp::kUXTB:
8366 __ Uxtab(output, first, second);
8367 break;
8368 case HDataProcWithShifterOp::kUXTH:
8369 __ Uxtah(output, first, second);
8370 break;
8371 case HDataProcWithShifterOp::kSXTB:
8372 __ Sxtab(output, first, second);
8373 break;
8374 case HDataProcWithShifterOp::kSXTH:
8375 __ Sxtah(output, first, second);
8376 break;
8377 default:
8378 LOG(FATAL) << "Unexpected operation kind: " << op_kind;
8379 UNREACHABLE();
8380 }
8381 } else {
8382 GenerateDataProcInstruction(kind,
8383 output,
8384 first,
8385 Operand(second,
8386 ShiftFromOpKind(op_kind),
8387 instruction->GetShiftAmount()),
8388 codegen_);
8389 }
Anton Kirilov74234da2017-01-13 14:42:47 +00008390 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008391 DCHECK_EQ(instruction->GetType(), DataType::Type::kInt64);
Anton Kirilov74234da2017-01-13 14:42:47 +00008392
8393 if (HDataProcWithShifterOp::IsExtensionOp(op_kind)) {
8394 const vixl32::Register second = InputRegisterAt(instruction, 1);
8395
8396 DCHECK(!LowRegisterFrom(locations->Out()).Is(second));
8397 GenerateDataProc(kind,
8398 locations->Out(),
8399 locations->InAt(0),
8400 second,
8401 Operand(second, ShiftType::ASR, 31),
8402 codegen_);
8403 } else {
8404 GenerateLongDataProc(instruction, codegen_);
8405 }
8406 }
8407}
8408
Artem Serov02109dd2016-09-23 17:17:54 +01008409// TODO(VIXL): Remove optimizations in the helper when they are implemented in vixl.
8410void InstructionCodeGeneratorARMVIXL::GenerateAndConst(vixl32::Register out,
8411 vixl32::Register first,
8412 uint32_t value) {
8413 // Optimize special cases for individual halfs of `and-long` (`and` is simplified earlier).
8414 if (value == 0xffffffffu) {
8415 if (!out.Is(first)) {
8416 __ Mov(out, first);
8417 }
8418 return;
8419 }
8420 if (value == 0u) {
8421 __ Mov(out, 0);
8422 return;
8423 }
8424 if (GetAssembler()->ShifterOperandCanHold(AND, value)) {
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00008425 __ And(out, first, value);
8426 } else if (GetAssembler()->ShifterOperandCanHold(BIC, ~value)) {
8427 __ Bic(out, first, ~value);
Artem Serov02109dd2016-09-23 17:17:54 +01008428 } else {
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00008429 DCHECK(IsPowerOfTwo(value + 1));
8430 __ Ubfx(out, first, 0, WhichPowerOf2(value + 1));
Artem Serov02109dd2016-09-23 17:17:54 +01008431 }
8432}
8433
8434// TODO(VIXL): Remove optimizations in the helper when they are implemented in vixl.
8435void InstructionCodeGeneratorARMVIXL::GenerateOrrConst(vixl32::Register out,
8436 vixl32::Register first,
8437 uint32_t value) {
8438 // Optimize special cases for individual halfs of `or-long` (`or` is simplified earlier).
8439 if (value == 0u) {
8440 if (!out.Is(first)) {
8441 __ Mov(out, first);
8442 }
8443 return;
8444 }
8445 if (value == 0xffffffffu) {
8446 __ Mvn(out, 0);
8447 return;
8448 }
8449 if (GetAssembler()->ShifterOperandCanHold(ORR, value)) {
8450 __ Orr(out, first, value);
8451 } else {
8452 DCHECK(GetAssembler()->ShifterOperandCanHold(ORN, ~value));
8453 __ Orn(out, first, ~value);
8454 }
8455}
8456
8457// TODO(VIXL): Remove optimizations in the helper when they are implemented in vixl.
8458void InstructionCodeGeneratorARMVIXL::GenerateEorConst(vixl32::Register out,
8459 vixl32::Register first,
8460 uint32_t value) {
8461 // Optimize special case for individual halfs of `xor-long` (`xor` is simplified earlier).
8462 if (value == 0u) {
8463 if (!out.Is(first)) {
8464 __ Mov(out, first);
8465 }
8466 return;
8467 }
8468 __ Eor(out, first, value);
8469}
8470
Anton Kirilovdda43962016-11-21 19:55:20 +00008471void InstructionCodeGeneratorARMVIXL::GenerateAddLongConst(Location out,
8472 Location first,
8473 uint64_t value) {
8474 vixl32::Register out_low = LowRegisterFrom(out);
8475 vixl32::Register out_high = HighRegisterFrom(out);
8476 vixl32::Register first_low = LowRegisterFrom(first);
8477 vixl32::Register first_high = HighRegisterFrom(first);
8478 uint32_t value_low = Low32Bits(value);
8479 uint32_t value_high = High32Bits(value);
8480 if (value_low == 0u) {
8481 if (!out_low.Is(first_low)) {
8482 __ Mov(out_low, first_low);
8483 }
8484 __ Add(out_high, first_high, value_high);
8485 return;
8486 }
8487 __ Adds(out_low, first_low, value_low);
Vladimir Markof0a6a1d2018-01-08 14:23:56 +00008488 if (GetAssembler()->ShifterOperandCanHold(ADC, value_high)) {
Anton Kirilovdda43962016-11-21 19:55:20 +00008489 __ Adc(out_high, first_high, value_high);
Anton Kirilovdda43962016-11-21 19:55:20 +00008490 } else {
Vladimir Markof0a6a1d2018-01-08 14:23:56 +00008491 DCHECK(GetAssembler()->ShifterOperandCanHold(SBC, ~value_high));
8492 __ Sbc(out_high, first_high, ~value_high);
Anton Kirilovdda43962016-11-21 19:55:20 +00008493 }
8494}
8495
Artem Serov02109dd2016-09-23 17:17:54 +01008496void InstructionCodeGeneratorARMVIXL::HandleBitwiseOperation(HBinaryOperation* instruction) {
8497 LocationSummary* locations = instruction->GetLocations();
8498 Location first = locations->InAt(0);
8499 Location second = locations->InAt(1);
8500 Location out = locations->Out();
8501
8502 if (second.IsConstant()) {
8503 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
8504 uint32_t value_low = Low32Bits(value);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008505 if (instruction->GetResultType() == DataType::Type::kInt32) {
Artem Serov02109dd2016-09-23 17:17:54 +01008506 vixl32::Register first_reg = InputRegisterAt(instruction, 0);
8507 vixl32::Register out_reg = OutputRegister(instruction);
8508 if (instruction->IsAnd()) {
8509 GenerateAndConst(out_reg, first_reg, value_low);
8510 } else if (instruction->IsOr()) {
8511 GenerateOrrConst(out_reg, first_reg, value_low);
8512 } else {
8513 DCHECK(instruction->IsXor());
8514 GenerateEorConst(out_reg, first_reg, value_low);
8515 }
8516 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008517 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
Artem Serov02109dd2016-09-23 17:17:54 +01008518 uint32_t value_high = High32Bits(value);
8519 vixl32::Register first_low = LowRegisterFrom(first);
8520 vixl32::Register first_high = HighRegisterFrom(first);
8521 vixl32::Register out_low = LowRegisterFrom(out);
8522 vixl32::Register out_high = HighRegisterFrom(out);
8523 if (instruction->IsAnd()) {
8524 GenerateAndConst(out_low, first_low, value_low);
8525 GenerateAndConst(out_high, first_high, value_high);
8526 } else if (instruction->IsOr()) {
8527 GenerateOrrConst(out_low, first_low, value_low);
8528 GenerateOrrConst(out_high, first_high, value_high);
8529 } else {
8530 DCHECK(instruction->IsXor());
8531 GenerateEorConst(out_low, first_low, value_low);
8532 GenerateEorConst(out_high, first_high, value_high);
8533 }
8534 }
8535 return;
8536 }
8537
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008538 if (instruction->GetResultType() == DataType::Type::kInt32) {
Artem Serov02109dd2016-09-23 17:17:54 +01008539 vixl32::Register first_reg = InputRegisterAt(instruction, 0);
8540 vixl32::Register second_reg = InputRegisterAt(instruction, 1);
8541 vixl32::Register out_reg = OutputRegister(instruction);
8542 if (instruction->IsAnd()) {
8543 __ And(out_reg, first_reg, second_reg);
8544 } else if (instruction->IsOr()) {
8545 __ Orr(out_reg, first_reg, second_reg);
8546 } else {
8547 DCHECK(instruction->IsXor());
8548 __ Eor(out_reg, first_reg, second_reg);
8549 }
8550 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008551 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
Artem Serov02109dd2016-09-23 17:17:54 +01008552 vixl32::Register first_low = LowRegisterFrom(first);
8553 vixl32::Register first_high = HighRegisterFrom(first);
8554 vixl32::Register second_low = LowRegisterFrom(second);
8555 vixl32::Register second_high = HighRegisterFrom(second);
8556 vixl32::Register out_low = LowRegisterFrom(out);
8557 vixl32::Register out_high = HighRegisterFrom(out);
8558 if (instruction->IsAnd()) {
8559 __ And(out_low, first_low, second_low);
8560 __ And(out_high, first_high, second_high);
8561 } else if (instruction->IsOr()) {
8562 __ Orr(out_low, first_low, second_low);
8563 __ Orr(out_high, first_high, second_high);
8564 } else {
8565 DCHECK(instruction->IsXor());
8566 __ Eor(out_low, first_low, second_low);
8567 __ Eor(out_high, first_high, second_high);
8568 }
8569 }
8570}
8571
Artem Serovcfbe9132016-10-14 15:58:56 +01008572void InstructionCodeGeneratorARMVIXL::GenerateReferenceLoadOneRegister(
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008573 HInstruction* instruction,
Artem Serovcfbe9132016-10-14 15:58:56 +01008574 Location out,
8575 uint32_t offset,
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008576 Location maybe_temp,
8577 ReadBarrierOption read_barrier_option) {
Artem Serovcfbe9132016-10-14 15:58:56 +01008578 vixl32::Register out_reg = RegisterFrom(out);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008579 if (read_barrier_option == kWithReadBarrier) {
8580 CHECK(kEmitCompilerReadBarrier);
8581 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
8582 if (kUseBakerReadBarrier) {
8583 // Load with fast path based Baker's read barrier.
8584 // /* HeapReference<Object> */ out = *(out + offset)
8585 codegen_->GenerateFieldLoadWithBakerReadBarrier(
8586 instruction, out, out_reg, offset, maybe_temp, /* needs_null_check */ false);
8587 } else {
8588 // Load with slow path based read barrier.
8589 // Save the value of `out` into `maybe_temp` before overwriting it
8590 // in the following move operation, as we will need it for the
8591 // read barrier below.
8592 __ Mov(RegisterFrom(maybe_temp), out_reg);
8593 // /* HeapReference<Object> */ out = *(out + offset)
8594 GetAssembler()->LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
8595 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
8596 }
Artem Serovcfbe9132016-10-14 15:58:56 +01008597 } else {
8598 // Plain load with no read barrier.
8599 // /* HeapReference<Object> */ out = *(out + offset)
8600 GetAssembler()->LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
8601 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
8602 }
8603}
8604
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008605void InstructionCodeGeneratorARMVIXL::GenerateReferenceLoadTwoRegisters(
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008606 HInstruction* instruction,
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008607 Location out,
8608 Location obj,
8609 uint32_t offset,
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008610 Location maybe_temp,
8611 ReadBarrierOption read_barrier_option) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008612 vixl32::Register out_reg = RegisterFrom(out);
8613 vixl32::Register obj_reg = RegisterFrom(obj);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008614 if (read_barrier_option == kWithReadBarrier) {
8615 CHECK(kEmitCompilerReadBarrier);
8616 if (kUseBakerReadBarrier) {
8617 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
8618 // Load with fast path based Baker's read barrier.
8619 // /* HeapReference<Object> */ out = *(obj + offset)
8620 codegen_->GenerateFieldLoadWithBakerReadBarrier(
8621 instruction, out, obj_reg, offset, maybe_temp, /* needs_null_check */ false);
8622 } else {
8623 // Load with slow path based read barrier.
8624 // /* HeapReference<Object> */ out = *(obj + offset)
8625 GetAssembler()->LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
8626 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
8627 }
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008628 } else {
8629 // Plain load with no read barrier.
8630 // /* HeapReference<Object> */ out = *(obj + offset)
8631 GetAssembler()->LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
8632 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
8633 }
8634}
8635
Scott Wakelinga7812ae2016-10-17 10:03:36 +01008636void InstructionCodeGeneratorARMVIXL::GenerateGcRootFieldLoad(
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008637 HInstruction* instruction,
Scott Wakelinga7812ae2016-10-17 10:03:36 +01008638 Location root,
8639 vixl32::Register obj,
8640 uint32_t offset,
Artem Serovd4cc5b22016-11-04 11:19:09 +00008641 ReadBarrierOption read_barrier_option) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01008642 vixl32::Register root_reg = RegisterFrom(root);
Artem Serovd4cc5b22016-11-04 11:19:09 +00008643 if (read_barrier_option == kWithReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008644 DCHECK(kEmitCompilerReadBarrier);
8645 if (kUseBakerReadBarrier) {
8646 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
Roland Levillainba650a42017-03-06 13:52:32 +00008647 // Baker's read barrier are used.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008648 if (kBakerReadBarrierLinkTimeThunksEnableForGcRoots &&
8649 !Runtime::Current()->UseJitCompilation()) {
Roland Levillain6d729a72017-06-30 18:34:01 +01008650 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in
8651 // the Marking Register) to decide whether we need to enter
8652 // the slow path to mark the GC root.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008653 //
8654 // We use link-time generated thunks for the slow path. That thunk
8655 // checks the reference and jumps to the entrypoint if needed.
8656 //
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008657 // lr = &return_address;
8658 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
Roland Levillain6d729a72017-06-30 18:34:01 +01008659 // if (mr) { // Thread::Current()->GetIsGcMarking()
8660 // goto gc_root_thunk<root_reg>(lr)
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008661 // }
8662 // return_address:
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008663
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008664 UseScratchRegisterScope temps(GetVIXLAssembler());
8665 ExcludeIPAndBakerCcEntrypointRegister(&temps, instruction);
Vladimir Marko88abba22017-05-03 17:09:25 +01008666 bool narrow = CanEmitNarrowLdr(root_reg, obj, offset);
8667 uint32_t custom_data = linker::Thumb2RelativePatcher::EncodeBakerReadBarrierGcRootData(
8668 root_reg.GetCode(), narrow);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008669 vixl32::Label* bne_label = codegen_->NewBakerReadBarrierPatch(custom_data);
Roland Levillainba650a42017-03-06 13:52:32 +00008670
Roland Levillain6d729a72017-06-30 18:34:01 +01008671 vixl::EmissionCheckScope guard(GetVIXLAssembler(), 4 * vixl32::kMaxInstructionSizeInBytes);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008672 vixl32::Label return_address;
8673 EmitAdrCode adr(GetVIXLAssembler(), lr, &return_address);
Roland Levillain6d729a72017-06-30 18:34:01 +01008674 __ cmp(mr, Operand(0));
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008675 // Currently the offset is always within range. If that changes,
8676 // we shall have to split the load the same way as for fields.
8677 DCHECK_LT(offset, kReferenceLoadMinFarOffset);
Vladimir Marko88abba22017-05-03 17:09:25 +01008678 ptrdiff_t old_offset = GetVIXLAssembler()->GetBuffer()->GetCursorOffset();
8679 __ ldr(EncodingSize(narrow ? Narrow : Wide), root_reg, MemOperand(obj, offset));
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008680 EmitPlaceholderBne(codegen_, bne_label);
8681 __ Bind(&return_address);
Vladimir Marko88abba22017-05-03 17:09:25 +01008682 DCHECK_EQ(old_offset - GetVIXLAssembler()->GetBuffer()->GetCursorOffset(),
8683 narrow ? BAKER_MARK_INTROSPECTION_GC_ROOT_LDR_NARROW_OFFSET
8684 : BAKER_MARK_INTROSPECTION_GC_ROOT_LDR_WIDE_OFFSET);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008685 } else {
Roland Levillain6d729a72017-06-30 18:34:01 +01008686 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in
8687 // the Marking Register) to decide whether we need to enter
8688 // the slow path to mark the GC root.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008689 //
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008690 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
Roland Levillain6d729a72017-06-30 18:34:01 +01008691 // if (mr) { // Thread::Current()->GetIsGcMarking()
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008692 // // Slow path.
Roland Levillain6d729a72017-06-30 18:34:01 +01008693 // entrypoint = Thread::Current()->pReadBarrierMarkReg ## root.reg()
8694 // root = entrypoint(root); // root = ReadBarrier::Mark(root); // Entry point call.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008695 // }
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008696
Roland Levillain6d729a72017-06-30 18:34:01 +01008697 // Slow path marking the GC root `root`. The entrypoint will
8698 // be loaded by the slow path code.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008699 SlowPathCodeARMVIXL* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01008700 new (codegen_->GetScopedAllocator()) ReadBarrierMarkSlowPathARMVIXL(instruction, root);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008701 codegen_->AddSlowPath(slow_path);
8702
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008703 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
8704 GetAssembler()->LoadFromOffset(kLoadWord, root_reg, obj, offset);
8705 static_assert(
8706 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
8707 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
8708 "have different sizes.");
8709 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
8710 "art::mirror::CompressedReference<mirror::Object> and int32_t "
8711 "have different sizes.");
8712
Roland Levillain6d729a72017-06-30 18:34:01 +01008713 __ CompareAndBranchIfNonZero(mr, slow_path->GetEntryLabel());
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008714 __ Bind(slow_path->GetExitLabel());
8715 }
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008716 } else {
8717 // GC root loaded through a slow path for read barriers other
8718 // than Baker's.
8719 // /* GcRoot<mirror::Object>* */ root = obj + offset
8720 __ Add(root_reg, obj, offset);
8721 // /* mirror::Object* */ root = root->Read()
8722 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
8723 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01008724 } else {
8725 // Plain GC root load with no read barrier.
8726 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
8727 GetAssembler()->LoadFromOffset(kLoadWord, root_reg, obj, offset);
8728 // Note that GC roots are not affected by heap poisoning, thus we
8729 // do not have to unpoison `root_reg` here.
8730 }
Roland Levillain5daa4952017-07-03 17:23:56 +01008731 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 18);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01008732}
8733
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008734void CodeGeneratorARMVIXL::MaybeAddBakerCcEntrypointTempForFields(LocationSummary* locations) {
8735 DCHECK(kEmitCompilerReadBarrier);
8736 DCHECK(kUseBakerReadBarrier);
8737 if (kBakerReadBarrierLinkTimeThunksEnableForFields) {
8738 if (!Runtime::Current()->UseJitCompilation()) {
8739 locations->AddTemp(Location::RegisterLocation(kBakerCcEntrypointRegister.GetCode()));
8740 }
8741 }
8742}
8743
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008744void CodeGeneratorARMVIXL::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
8745 Location ref,
8746 vixl32::Register obj,
8747 uint32_t offset,
8748 Location temp,
8749 bool needs_null_check) {
8750 DCHECK(kEmitCompilerReadBarrier);
8751 DCHECK(kUseBakerReadBarrier);
8752
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008753 if (kBakerReadBarrierLinkTimeThunksEnableForFields &&
8754 !Runtime::Current()->UseJitCompilation()) {
Roland Levillain6d729a72017-06-30 18:34:01 +01008755 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in the
8756 // Marking Register) to decide whether we need to enter the slow
8757 // path to mark the reference. Then, in the slow path, check the
8758 // gray bit in the lock word of the reference's holder (`obj`) to
8759 // decide whether to mark `ref` or not.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008760 //
8761 // We use link-time generated thunks for the slow path. That thunk checks
8762 // the holder and jumps to the entrypoint if needed. If the holder is not
8763 // gray, it creates a fake dependency and returns to the LDR instruction.
8764 //
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008765 // lr = &gray_return_address;
Roland Levillain6d729a72017-06-30 18:34:01 +01008766 // if (mr) { // Thread::Current()->GetIsGcMarking()
8767 // goto field_thunk<holder_reg, base_reg>(lr)
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008768 // }
8769 // not_gray_return_address:
8770 // // Original reference load. If the offset is too large to fit
8771 // // into LDR, we use an adjusted base register here.
Vladimir Marko88abba22017-05-03 17:09:25 +01008772 // HeapReference<mirror::Object> reference = *(obj+offset);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008773 // gray_return_address:
8774
8775 DCHECK_ALIGNED(offset, sizeof(mirror::HeapReference<mirror::Object>));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008776 vixl32::Register ref_reg = RegisterFrom(ref, DataType::Type::kReference);
Vladimir Marko88abba22017-05-03 17:09:25 +01008777 bool narrow = CanEmitNarrowLdr(ref_reg, obj, offset);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008778 vixl32::Register base = obj;
8779 if (offset >= kReferenceLoadMinFarOffset) {
8780 base = RegisterFrom(temp);
8781 DCHECK(!base.Is(kBakerCcEntrypointRegister));
8782 static_assert(IsPowerOfTwo(kReferenceLoadMinFarOffset), "Expecting a power of 2.");
8783 __ Add(base, obj, Operand(offset & ~(kReferenceLoadMinFarOffset - 1u)));
8784 offset &= (kReferenceLoadMinFarOffset - 1u);
Vladimir Marko88abba22017-05-03 17:09:25 +01008785 // Use narrow LDR only for small offsets. Generating narrow encoding LDR for the large
8786 // offsets with `(offset & (kReferenceLoadMinFarOffset - 1u)) < 32u` would most likely
8787 // increase the overall code size when taking the generated thunks into account.
8788 DCHECK(!narrow);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008789 }
8790 UseScratchRegisterScope temps(GetVIXLAssembler());
8791 ExcludeIPAndBakerCcEntrypointRegister(&temps, instruction);
8792 uint32_t custom_data = linker::Thumb2RelativePatcher::EncodeBakerReadBarrierFieldData(
Vladimir Marko88abba22017-05-03 17:09:25 +01008793 base.GetCode(), obj.GetCode(), narrow);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008794 vixl32::Label* bne_label = NewBakerReadBarrierPatch(custom_data);
8795
Roland Levillain5daa4952017-07-03 17:23:56 +01008796 {
8797 vixl::EmissionCheckScope guard(
8798 GetVIXLAssembler(),
8799 (kPoisonHeapReferences ? 5u : 4u) * vixl32::kMaxInstructionSizeInBytes);
8800 vixl32::Label return_address;
8801 EmitAdrCode adr(GetVIXLAssembler(), lr, &return_address);
8802 __ cmp(mr, Operand(0));
8803 EmitPlaceholderBne(this, bne_label);
8804 ptrdiff_t old_offset = GetVIXLAssembler()->GetBuffer()->GetCursorOffset();
8805 __ ldr(EncodingSize(narrow ? Narrow : Wide), ref_reg, MemOperand(base, offset));
8806 if (needs_null_check) {
8807 MaybeRecordImplicitNullCheck(instruction);
Vladimir Marko88abba22017-05-03 17:09:25 +01008808 }
Roland Levillain5daa4952017-07-03 17:23:56 +01008809 // Note: We need a specific width for the unpoisoning NEG.
8810 if (kPoisonHeapReferences) {
8811 if (narrow) {
8812 // The only 16-bit encoding is T1 which sets flags outside IT block (i.e. RSBS, not RSB).
8813 __ rsbs(EncodingSize(Narrow), ref_reg, ref_reg, Operand(0));
8814 } else {
8815 __ rsb(EncodingSize(Wide), ref_reg, ref_reg, Operand(0));
8816 }
8817 }
8818 __ Bind(&return_address);
8819 DCHECK_EQ(old_offset - GetVIXLAssembler()->GetBuffer()->GetCursorOffset(),
8820 narrow ? BAKER_MARK_INTROSPECTION_FIELD_LDR_NARROW_OFFSET
8821 : BAKER_MARK_INTROSPECTION_FIELD_LDR_WIDE_OFFSET);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008822 }
Roland Levillain5daa4952017-07-03 17:23:56 +01008823 MaybeGenerateMarkingRegisterCheck(/* code */ 19, /* temp_loc */ LocationFrom(ip));
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008824 return;
8825 }
8826
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008827 // /* HeapReference<Object> */ ref = *(obj + offset)
8828 Location no_index = Location::NoLocation();
8829 ScaleFactor no_scale_factor = TIMES_1;
8830 GenerateReferenceLoadWithBakerReadBarrier(
8831 instruction, ref, obj, offset, no_index, no_scale_factor, temp, needs_null_check);
Roland Levillain6070e882016-11-03 17:51:58 +00008832}
8833
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008834void CodeGeneratorARMVIXL::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
8835 Location ref,
8836 vixl32::Register obj,
8837 uint32_t data_offset,
8838 Location index,
8839 Location temp,
8840 bool needs_null_check) {
8841 DCHECK(kEmitCompilerReadBarrier);
8842 DCHECK(kUseBakerReadBarrier);
8843
8844 static_assert(
8845 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
8846 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008847 ScaleFactor scale_factor = TIMES_4;
8848
8849 if (kBakerReadBarrierLinkTimeThunksEnableForArrays &&
8850 !Runtime::Current()->UseJitCompilation()) {
Roland Levillain6d729a72017-06-30 18:34:01 +01008851 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in the
8852 // Marking Register) to decide whether we need to enter the slow
8853 // path to mark the reference. Then, in the slow path, check the
8854 // gray bit in the lock word of the reference's holder (`obj`) to
8855 // decide whether to mark `ref` or not.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008856 //
8857 // We use link-time generated thunks for the slow path. That thunk checks
8858 // the holder and jumps to the entrypoint if needed. If the holder is not
8859 // gray, it creates a fake dependency and returns to the LDR instruction.
8860 //
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008861 // lr = &gray_return_address;
Roland Levillain6d729a72017-06-30 18:34:01 +01008862 // if (mr) { // Thread::Current()->GetIsGcMarking()
8863 // goto array_thunk<base_reg>(lr)
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008864 // }
8865 // not_gray_return_address:
8866 // // Original reference load. If the offset is too large to fit
8867 // // into LDR, we use an adjusted base register here.
Vladimir Marko88abba22017-05-03 17:09:25 +01008868 // HeapReference<mirror::Object> reference = data[index];
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008869 // gray_return_address:
8870
8871 DCHECK(index.IsValid());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008872 vixl32::Register index_reg = RegisterFrom(index, DataType::Type::kInt32);
8873 vixl32::Register ref_reg = RegisterFrom(ref, DataType::Type::kReference);
8874 vixl32::Register data_reg = RegisterFrom(temp, DataType::Type::kInt32); // Raw pointer.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008875 DCHECK(!data_reg.Is(kBakerCcEntrypointRegister));
8876
8877 UseScratchRegisterScope temps(GetVIXLAssembler());
8878 ExcludeIPAndBakerCcEntrypointRegister(&temps, instruction);
8879 uint32_t custom_data =
8880 linker::Thumb2RelativePatcher::EncodeBakerReadBarrierArrayData(data_reg.GetCode());
8881 vixl32::Label* bne_label = NewBakerReadBarrierPatch(custom_data);
8882
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008883 __ Add(data_reg, obj, Operand(data_offset));
Roland Levillain5daa4952017-07-03 17:23:56 +01008884 {
8885 vixl::EmissionCheckScope guard(
8886 GetVIXLAssembler(),
8887 (kPoisonHeapReferences ? 5u : 4u) * vixl32::kMaxInstructionSizeInBytes);
8888 vixl32::Label return_address;
8889 EmitAdrCode adr(GetVIXLAssembler(), lr, &return_address);
8890 __ cmp(mr, Operand(0));
8891 EmitPlaceholderBne(this, bne_label);
8892 ptrdiff_t old_offset = GetVIXLAssembler()->GetBuffer()->GetCursorOffset();
8893 __ ldr(ref_reg, MemOperand(data_reg, index_reg, vixl32::LSL, scale_factor));
8894 DCHECK(!needs_null_check); // The thunk cannot handle the null check.
8895 // Note: We need a Wide NEG for the unpoisoning.
8896 if (kPoisonHeapReferences) {
8897 __ rsb(EncodingSize(Wide), ref_reg, ref_reg, Operand(0));
8898 }
8899 __ Bind(&return_address);
8900 DCHECK_EQ(old_offset - GetVIXLAssembler()->GetBuffer()->GetCursorOffset(),
8901 BAKER_MARK_INTROSPECTION_ARRAY_LDR_OFFSET);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008902 }
Roland Levillain5daa4952017-07-03 17:23:56 +01008903 MaybeGenerateMarkingRegisterCheck(/* code */ 20, /* temp_loc */ LocationFrom(ip));
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008904 return;
8905 }
8906
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008907 // /* HeapReference<Object> */ ref =
8908 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008909 GenerateReferenceLoadWithBakerReadBarrier(
8910 instruction, ref, obj, data_offset, index, scale_factor, temp, needs_null_check);
Roland Levillain6070e882016-11-03 17:51:58 +00008911}
8912
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008913void CodeGeneratorARMVIXL::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
8914 Location ref,
8915 vixl32::Register obj,
8916 uint32_t offset,
8917 Location index,
8918 ScaleFactor scale_factor,
8919 Location temp,
Roland Levillainff487002017-03-07 16:50:01 +00008920 bool needs_null_check) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008921 DCHECK(kEmitCompilerReadBarrier);
8922 DCHECK(kUseBakerReadBarrier);
8923
Roland Levillain6d729a72017-06-30 18:34:01 +01008924 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in the
8925 // Marking Register) to decide whether we need to enter the slow
8926 // path to mark the reference. Then, in the slow path, check the
8927 // gray bit in the lock word of the reference's holder (`obj`) to
8928 // decide whether to mark `ref` or not.
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008929 //
Roland Levillain6d729a72017-06-30 18:34:01 +01008930 // if (mr) { // Thread::Current()->GetIsGcMarking()
Roland Levillainff487002017-03-07 16:50:01 +00008931 // // Slow path.
8932 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
8933 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
8934 // HeapReference<mirror::Object> ref = *src; // Original reference load.
8935 // bool is_gray = (rb_state == ReadBarrier::GrayState());
8936 // if (is_gray) {
Roland Levillain6d729a72017-06-30 18:34:01 +01008937 // entrypoint = Thread::Current()->pReadBarrierMarkReg ## root.reg()
8938 // ref = entrypoint(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
Roland Levillainff487002017-03-07 16:50:01 +00008939 // }
8940 // } else {
8941 // HeapReference<mirror::Object> ref = *src; // Original reference load.
8942 // }
8943
8944 vixl32::Register temp_reg = RegisterFrom(temp);
8945
8946 // Slow path marking the object `ref` when the GC is marking. The
Roland Levillain6d729a72017-06-30 18:34:01 +01008947 // entrypoint will be loaded by the slow path code.
Roland Levillainff487002017-03-07 16:50:01 +00008948 SlowPathCodeARMVIXL* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01008949 new (GetScopedAllocator()) LoadReferenceWithBakerReadBarrierSlowPathARMVIXL(
Roland Levillain6d729a72017-06-30 18:34:01 +01008950 instruction, ref, obj, offset, index, scale_factor, needs_null_check, temp_reg);
Roland Levillainff487002017-03-07 16:50:01 +00008951 AddSlowPath(slow_path);
8952
Roland Levillain6d729a72017-06-30 18:34:01 +01008953 __ CompareAndBranchIfNonZero(mr, slow_path->GetEntryLabel());
Roland Levillainff487002017-03-07 16:50:01 +00008954 // Fast path: the GC is not marking: just load the reference.
8955 GenerateRawReferenceLoad(instruction, ref, obj, offset, index, scale_factor, needs_null_check);
8956 __ Bind(slow_path->GetExitLabel());
Roland Levillain5daa4952017-07-03 17:23:56 +01008957 MaybeGenerateMarkingRegisterCheck(/* code */ 21);
Roland Levillainff487002017-03-07 16:50:01 +00008958}
8959
8960void CodeGeneratorARMVIXL::UpdateReferenceFieldWithBakerReadBarrier(HInstruction* instruction,
8961 Location ref,
8962 vixl32::Register obj,
8963 Location field_offset,
8964 Location temp,
8965 bool needs_null_check,
8966 vixl32::Register temp2) {
8967 DCHECK(kEmitCompilerReadBarrier);
8968 DCHECK(kUseBakerReadBarrier);
8969
Roland Levillain6d729a72017-06-30 18:34:01 +01008970 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in the
8971 // Marking Register) to decide whether we need to enter the slow
8972 // path to update the reference field within `obj`. Then, in the
8973 // slow path, check the gray bit in the lock word of the reference's
8974 // holder (`obj`) to decide whether to mark `ref` and update the
8975 // field or not.
Roland Levillainff487002017-03-07 16:50:01 +00008976 //
Roland Levillain6d729a72017-06-30 18:34:01 +01008977 // if (mr) { // Thread::Current()->GetIsGcMarking()
Roland Levillainba650a42017-03-06 13:52:32 +00008978 // // Slow path.
Roland Levillain54f869e2017-03-06 13:54:11 +00008979 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
8980 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
Roland Levillainff487002017-03-07 16:50:01 +00008981 // HeapReference<mirror::Object> ref = *(obj + field_offset); // Reference load.
Roland Levillain54f869e2017-03-06 13:54:11 +00008982 // bool is_gray = (rb_state == ReadBarrier::GrayState());
8983 // if (is_gray) {
Roland Levillainff487002017-03-07 16:50:01 +00008984 // old_ref = ref;
Roland Levillain6d729a72017-06-30 18:34:01 +01008985 // entrypoint = Thread::Current()->pReadBarrierMarkReg ## root.reg()
8986 // ref = entrypoint(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
Roland Levillainff487002017-03-07 16:50:01 +00008987 // compareAndSwapObject(obj, field_offset, old_ref, ref);
Roland Levillain54f869e2017-03-06 13:54:11 +00008988 // }
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008989 // }
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008990
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008991 vixl32::Register temp_reg = RegisterFrom(temp);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008992
Roland Levillainff487002017-03-07 16:50:01 +00008993 // Slow path updating the object reference at address `obj + field_offset`
Roland Levillain6d729a72017-06-30 18:34:01 +01008994 // when the GC is marking. The entrypoint will be loaded by the slow path code.
Vladimir Marko174b2e22017-10-12 13:34:49 +01008995 SlowPathCodeARMVIXL* slow_path =
8996 new (GetScopedAllocator()) LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARMVIXL(
Roland Levillainff487002017-03-07 16:50:01 +00008997 instruction,
8998 ref,
8999 obj,
9000 /* offset */ 0u,
9001 /* index */ field_offset,
9002 /* scale_factor */ ScaleFactor::TIMES_1,
9003 needs_null_check,
9004 temp_reg,
Roland Levillain6d729a72017-06-30 18:34:01 +01009005 temp2);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00009006 AddSlowPath(slow_path);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00009007
Roland Levillain6d729a72017-06-30 18:34:01 +01009008 __ CompareAndBranchIfNonZero(mr, slow_path->GetEntryLabel());
Roland Levillainff487002017-03-07 16:50:01 +00009009 // Fast path: the GC is not marking: nothing to do (the field is
9010 // up-to-date, and we don't need to load the reference).
Anton Kirilovedb2ac32016-11-30 15:14:10 +00009011 __ Bind(slow_path->GetExitLabel());
Roland Levillain5daa4952017-07-03 17:23:56 +01009012 MaybeGenerateMarkingRegisterCheck(/* code */ 22);
Roland Levillain844e6532016-11-03 16:09:47 +00009013}
Scott Wakelingfe885462016-09-22 10:24:38 +01009014
Roland Levillainba650a42017-03-06 13:52:32 +00009015void CodeGeneratorARMVIXL::GenerateRawReferenceLoad(HInstruction* instruction,
9016 Location ref,
9017 vixl::aarch32::Register obj,
9018 uint32_t offset,
9019 Location index,
9020 ScaleFactor scale_factor,
9021 bool needs_null_check) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009022 DataType::Type type = DataType::Type::kReference;
Roland Levillainba650a42017-03-06 13:52:32 +00009023 vixl32::Register ref_reg = RegisterFrom(ref, type);
9024
9025 // If needed, vixl::EmissionCheckScope guards are used to ensure
9026 // that no pools are emitted between the load (macro) instruction
9027 // and MaybeRecordImplicitNullCheck.
9028
Scott Wakelingfe885462016-09-22 10:24:38 +01009029 if (index.IsValid()) {
9030 // Load types involving an "index": ArrayGet,
9031 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
9032 // intrinsics.
Roland Levillainba650a42017-03-06 13:52:32 +00009033 // /* HeapReference<mirror::Object> */ ref = *(obj + offset + (index << scale_factor))
Scott Wakelingfe885462016-09-22 10:24:38 +01009034 if (index.IsConstant()) {
9035 size_t computed_offset =
9036 (Int32ConstantFrom(index) << scale_factor) + offset;
Roland Levillainba650a42017-03-06 13:52:32 +00009037 vixl::EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Scott Wakelingfe885462016-09-22 10:24:38 +01009038 GetAssembler()->LoadFromOffset(kLoadWord, ref_reg, obj, computed_offset);
Roland Levillainba650a42017-03-06 13:52:32 +00009039 if (needs_null_check) {
9040 MaybeRecordImplicitNullCheck(instruction);
9041 }
Scott Wakelingfe885462016-09-22 10:24:38 +01009042 } else {
9043 // Handle the special case of the
9044 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
9045 // intrinsics, which use a register pair as index ("long
9046 // offset"), of which only the low part contains data.
9047 vixl32::Register index_reg = index.IsRegisterPair()
9048 ? LowRegisterFrom(index)
9049 : RegisterFrom(index);
9050 UseScratchRegisterScope temps(GetVIXLAssembler());
Roland Levillainba650a42017-03-06 13:52:32 +00009051 vixl32::Register temp = temps.Acquire();
9052 __ Add(temp, obj, Operand(index_reg, ShiftType::LSL, scale_factor));
9053 {
9054 vixl::EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
9055 GetAssembler()->LoadFromOffset(kLoadWord, ref_reg, temp, offset);
9056 if (needs_null_check) {
9057 MaybeRecordImplicitNullCheck(instruction);
9058 }
9059 }
Scott Wakelingfe885462016-09-22 10:24:38 +01009060 }
9061 } else {
Roland Levillainba650a42017-03-06 13:52:32 +00009062 // /* HeapReference<mirror::Object> */ ref = *(obj + offset)
9063 vixl::EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Scott Wakelingfe885462016-09-22 10:24:38 +01009064 GetAssembler()->LoadFromOffset(kLoadWord, ref_reg, obj, offset);
Roland Levillainba650a42017-03-06 13:52:32 +00009065 if (needs_null_check) {
9066 MaybeRecordImplicitNullCheck(instruction);
9067 }
Scott Wakelingfe885462016-09-22 10:24:38 +01009068 }
9069
Roland Levillain844e6532016-11-03 16:09:47 +00009070 // Object* ref = ref_addr->AsMirrorPtr()
9071 GetAssembler()->MaybeUnpoisonHeapReference(ref_reg);
Roland Levillain844e6532016-11-03 16:09:47 +00009072}
9073
Roland Levillain5daa4952017-07-03 17:23:56 +01009074void CodeGeneratorARMVIXL::MaybeGenerateMarkingRegisterCheck(int code, Location temp_loc) {
9075 // The following condition is a compile-time one, so it does not have a run-time cost.
9076 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier && kIsDebugBuild) {
9077 // The following condition is a run-time one; it is executed after the
9078 // previous compile-time test, to avoid penalizing non-debug builds.
9079 if (GetCompilerOptions().EmitRunTimeChecksInDebugMode()) {
9080 UseScratchRegisterScope temps(GetVIXLAssembler());
9081 vixl32::Register temp = temp_loc.IsValid() ? RegisterFrom(temp_loc) : temps.Acquire();
9082 GetAssembler()->GenerateMarkingRegisterCheck(temp,
9083 kMarkingRegisterCheckBreakCodeBaseCode + code);
9084 }
9085 }
9086}
9087
Anton Kirilovedb2ac32016-11-30 15:14:10 +00009088void CodeGeneratorARMVIXL::GenerateReadBarrierSlow(HInstruction* instruction,
9089 Location out,
9090 Location ref,
9091 Location obj,
9092 uint32_t offset,
9093 Location index) {
9094 DCHECK(kEmitCompilerReadBarrier);
9095
9096 // Insert a slow path based read barrier *after* the reference load.
9097 //
9098 // If heap poisoning is enabled, the unpoisoning of the loaded
9099 // reference will be carried out by the runtime within the slow
9100 // path.
9101 //
9102 // Note that `ref` currently does not get unpoisoned (when heap
9103 // poisoning is enabled), which is alright as the `ref` argument is
9104 // not used by the artReadBarrierSlow entry point.
9105 //
9106 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Vladimir Marko174b2e22017-10-12 13:34:49 +01009107 SlowPathCodeARMVIXL* slow_path = new (GetScopedAllocator())
Anton Kirilovedb2ac32016-11-30 15:14:10 +00009108 ReadBarrierForHeapReferenceSlowPathARMVIXL(instruction, out, ref, obj, offset, index);
9109 AddSlowPath(slow_path);
9110
9111 __ B(slow_path->GetEntryLabel());
9112 __ Bind(slow_path->GetExitLabel());
9113}
9114
9115void CodeGeneratorARMVIXL::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
Artem Serov02d37832016-10-25 15:25:33 +01009116 Location out,
Anton Kirilovedb2ac32016-11-30 15:14:10 +00009117 Location ref,
9118 Location obj,
9119 uint32_t offset,
9120 Location index) {
Artem Serov02d37832016-10-25 15:25:33 +01009121 if (kEmitCompilerReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00009122 // Baker's read barriers shall be handled by the fast path
Roland Levillain9983e302017-07-14 14:34:22 +01009123 // (CodeGeneratorARMVIXL::GenerateReferenceLoadWithBakerReadBarrier).
Artem Serov02d37832016-10-25 15:25:33 +01009124 DCHECK(!kUseBakerReadBarrier);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00009125 // If heap poisoning is enabled, unpoisoning will be taken care of
9126 // by the runtime within the slow path.
9127 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Artem Serov02d37832016-10-25 15:25:33 +01009128 } else if (kPoisonHeapReferences) {
9129 GetAssembler()->UnpoisonHeapReference(RegisterFrom(out));
9130 }
9131}
9132
Anton Kirilovedb2ac32016-11-30 15:14:10 +00009133void CodeGeneratorARMVIXL::GenerateReadBarrierForRootSlow(HInstruction* instruction,
9134 Location out,
9135 Location root) {
9136 DCHECK(kEmitCompilerReadBarrier);
9137
9138 // Insert a slow path based read barrier *after* the GC root load.
9139 //
9140 // Note that GC roots are not affected by heap poisoning, so we do
9141 // not need to do anything special for this here.
9142 SlowPathCodeARMVIXL* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01009143 new (GetScopedAllocator()) ReadBarrierForRootSlowPathARMVIXL(instruction, out, root);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00009144 AddSlowPath(slow_path);
9145
9146 __ B(slow_path->GetEntryLabel());
9147 __ Bind(slow_path->GetExitLabel());
9148}
9149
Artem Serov02d37832016-10-25 15:25:33 +01009150// Check if the desired_dispatch_info is supported. If it is, return it,
9151// otherwise return a fall-back info that should be used instead.
9152HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARMVIXL::GetSupportedInvokeStaticOrDirectDispatch(
Artem Serovd4cc5b22016-11-04 11:19:09 +00009153 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +00009154 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Nicolas Geoffraye807ff72017-01-23 09:03:12 +00009155 return desired_dispatch_info;
Artem Serov02d37832016-10-25 15:25:33 +01009156}
9157
Scott Wakelingfe885462016-09-22 10:24:38 +01009158vixl32::Register CodeGeneratorARMVIXL::GetInvokeStaticOrDirectExtraParameter(
9159 HInvokeStaticOrDirect* invoke, vixl32::Register temp) {
9160 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
9161 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
9162 if (!invoke->GetLocations()->Intrinsified()) {
9163 return RegisterFrom(location);
9164 }
9165 // For intrinsics we allow any location, so it may be on the stack.
9166 if (!location.IsRegister()) {
9167 GetAssembler()->LoadFromOffset(kLoadWord, temp, sp, location.GetStackIndex());
9168 return temp;
9169 }
9170 // For register locations, check if the register was saved. If so, get it from the stack.
9171 // Note: There is a chance that the register was saved but not overwritten, so we could
9172 // save one load. However, since this is just an intrinsic slow path we prefer this
9173 // simple and more robust approach rather that trying to determine if that's the case.
9174 SlowPathCode* slow_path = GetCurrentSlowPath();
Scott Wakelingd5cd4972017-02-03 11:38:35 +00009175 if (slow_path != nullptr && slow_path->IsCoreRegisterSaved(RegisterFrom(location).GetCode())) {
Scott Wakelingfe885462016-09-22 10:24:38 +01009176 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(RegisterFrom(location).GetCode());
9177 GetAssembler()->LoadFromOffset(kLoadWord, temp, sp, stack_offset);
9178 return temp;
9179 }
9180 return RegisterFrom(location);
9181}
9182
Vladimir Markod254f5c2017-06-02 15:18:36 +00009183void CodeGeneratorARMVIXL::GenerateStaticOrDirectCall(
Vladimir Markoe7197bf2017-06-02 17:00:23 +01009184 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00009185 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
Scott Wakelingfe885462016-09-22 10:24:38 +01009186 switch (invoke->GetMethodLoadKind()) {
9187 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
9188 uint32_t offset =
9189 GetThreadOffset<kArmPointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
9190 // temp = thread->string_init_entrypoint
Artem Serovd4cc5b22016-11-04 11:19:09 +00009191 GetAssembler()->LoadFromOffset(kLoadWord, RegisterFrom(temp), tr, offset);
9192 break;
9193 }
9194 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
9195 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
9196 break;
Vladimir Marko65979462017-05-19 17:25:12 +01009197 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative: {
9198 DCHECK(GetCompilerOptions().IsBootImage());
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009199 PcRelativePatchInfo* labels = NewBootImageMethodPatch(invoke->GetTargetMethod());
Vladimir Marko65979462017-05-19 17:25:12 +01009200 vixl32::Register temp_reg = RegisterFrom(temp);
9201 EmitMovwMovtPlaceholder(labels, temp_reg);
9202 break;
9203 }
Artem Serovd4cc5b22016-11-04 11:19:09 +00009204 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
9205 __ Mov(RegisterFrom(temp), Operand::From(invoke->GetMethodAddress()));
9206 break;
Vladimir Marko0eb882b2017-05-15 13:39:18 +01009207 case HInvokeStaticOrDirect::MethodLoadKind::kBssEntry: {
9208 PcRelativePatchInfo* labels = NewMethodBssEntryPatch(
9209 MethodReference(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex()));
9210 vixl32::Register temp_reg = RegisterFrom(temp);
9211 EmitMovwMovtPlaceholder(labels, temp_reg);
9212 GetAssembler()->LoadFromOffset(kLoadWord, temp_reg, temp_reg, /* offset*/ 0);
Scott Wakelingfe885462016-09-22 10:24:38 +01009213 break;
9214 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01009215 case HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall: {
9216 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
9217 return; // No code pointer retrieval; the runtime performs the call directly.
Scott Wakelingfe885462016-09-22 10:24:38 +01009218 }
Scott Wakelingfe885462016-09-22 10:24:38 +01009219 }
9220
Artem Serovd4cc5b22016-11-04 11:19:09 +00009221 switch (invoke->GetCodePtrLocation()) {
9222 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Vladimir Markoe7197bf2017-06-02 17:00:23 +01009223 {
9224 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
9225 ExactAssemblyScope aas(GetVIXLAssembler(),
9226 vixl32::k32BitT32InstructionSizeInBytes,
9227 CodeBufferCheckScope::kMaximumSize);
9228 __ bl(GetFrameEntryLabel());
9229 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
9230 }
Artem Serovd4cc5b22016-11-04 11:19:09 +00009231 break;
Artem Serovd4cc5b22016-11-04 11:19:09 +00009232 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
9233 // LR = callee_method->entry_point_from_quick_compiled_code_
9234 GetAssembler()->LoadFromOffset(
9235 kLoadWord,
9236 lr,
9237 RegisterFrom(callee_method),
9238 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize).Int32Value());
Alexandre Rames374ddf32016-11-04 10:40:49 +00009239 {
Vladimir Markoe7197bf2017-06-02 17:00:23 +01009240 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
Alexandre Rames374ddf32016-11-04 10:40:49 +00009241 // 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 +00009242 ExactAssemblyScope aas(GetVIXLAssembler(),
9243 vixl32::k16BitT32InstructionSizeInBytes,
9244 CodeBufferCheckScope::kExactSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00009245 // LR()
9246 __ blx(lr);
Vladimir Markoe7197bf2017-06-02 17:00:23 +01009247 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Alexandre Rames374ddf32016-11-04 10:40:49 +00009248 }
Artem Serovd4cc5b22016-11-04 11:19:09 +00009249 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01009250 }
9251
Scott Wakelingfe885462016-09-22 10:24:38 +01009252 DCHECK(!IsLeafMethod());
9253}
9254
Vladimir Markoe7197bf2017-06-02 17:00:23 +01009255void CodeGeneratorARMVIXL::GenerateVirtualCall(
9256 HInvokeVirtual* invoke, Location temp_location, SlowPathCode* slow_path) {
Scott Wakelingfe885462016-09-22 10:24:38 +01009257 vixl32::Register temp = RegisterFrom(temp_location);
9258 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
9259 invoke->GetVTableIndex(), kArmPointerSize).Uint32Value();
9260
9261 // Use the calling convention instead of the location of the receiver, as
9262 // intrinsics may have put the receiver in a different register. In the intrinsics
9263 // slow path, the arguments have been moved to the right place, so here we are
9264 // guaranteed that the receiver is the first register of the calling convention.
9265 InvokeDexCallingConventionARMVIXL calling_convention;
9266 vixl32::Register receiver = calling_convention.GetRegisterAt(0);
9267 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Alexandre Rames374ddf32016-11-04 10:40:49 +00009268 {
9269 // Make sure the pc is recorded immediately after the `ldr` instruction.
Artem Serov0fb37192016-12-06 18:13:40 +00009270 ExactAssemblyScope aas(GetVIXLAssembler(),
9271 vixl32::kMaxInstructionSizeInBytes,
9272 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00009273 // /* HeapReference<Class> */ temp = receiver->klass_
9274 __ ldr(temp, MemOperand(receiver, class_offset));
9275 MaybeRecordImplicitNullCheck(invoke);
9276 }
Scott Wakelingfe885462016-09-22 10:24:38 +01009277 // Instead of simply (possibly) unpoisoning `temp` here, we should
9278 // emit a read barrier for the previous class reference load.
9279 // However this is not required in practice, as this is an
9280 // intermediate/temporary reference and because the current
9281 // concurrent copying collector keeps the from-space memory
9282 // intact/accessible until the end of the marking phase (the
9283 // concurrent copying collector may not in the future).
9284 GetAssembler()->MaybeUnpoisonHeapReference(temp);
9285
9286 // temp = temp->GetMethodAt(method_offset);
9287 uint32_t entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(
9288 kArmPointerSize).Int32Value();
9289 GetAssembler()->LoadFromOffset(kLoadWord, temp, temp, method_offset);
9290 // LR = temp->GetEntryPoint();
9291 GetAssembler()->LoadFromOffset(kLoadWord, lr, temp, entry_point);
Vladimir Markoe7197bf2017-06-02 17:00:23 +01009292 {
9293 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
9294 // blx in T32 has only 16bit encoding that's why a stricter check for the scope is used.
9295 ExactAssemblyScope aas(GetVIXLAssembler(),
9296 vixl32::k16BitT32InstructionSizeInBytes,
9297 CodeBufferCheckScope::kExactSize);
9298 // LR();
9299 __ blx(lr);
9300 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
9301 }
Scott Wakelingfe885462016-09-22 10:24:38 +01009302}
9303
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009304CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewBootImageMethodPatch(
Vladimir Marko65979462017-05-19 17:25:12 +01009305 MethodReference target_method) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009306 return NewPcRelativePatch(
9307 target_method.dex_file, target_method.index, &boot_image_method_patches_);
Artem Serovd4cc5b22016-11-04 11:19:09 +00009308}
9309
Vladimir Marko0eb882b2017-05-15 13:39:18 +01009310CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewMethodBssEntryPatch(
9311 MethodReference target_method) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009312 return NewPcRelativePatch(
9313 target_method.dex_file, target_method.index, &method_bss_entry_patches_);
Vladimir Marko0eb882b2017-05-15 13:39:18 +01009314}
9315
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009316CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewBootImageTypePatch(
Artem Serovd4cc5b22016-11-04 11:19:09 +00009317 const DexFile& dex_file, dex::TypeIndex type_index) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009318 return NewPcRelativePatch(&dex_file, type_index.index_, &boot_image_type_patches_);
Artem Serovd4cc5b22016-11-04 11:19:09 +00009319}
9320
Vladimir Marko1998cd02017-01-13 13:02:58 +00009321CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewTypeBssEntryPatch(
9322 const DexFile& dex_file, dex::TypeIndex type_index) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009323 return NewPcRelativePatch(&dex_file, type_index.index_, &type_bss_entry_patches_);
Vladimir Marko1998cd02017-01-13 13:02:58 +00009324}
9325
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009326CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewBootImageStringPatch(
Vladimir Marko65979462017-05-19 17:25:12 +01009327 const DexFile& dex_file, dex::StringIndex string_index) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009328 return NewPcRelativePatch(&dex_file, string_index.index_, &boot_image_string_patches_);
Vladimir Marko65979462017-05-19 17:25:12 +01009329}
9330
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01009331CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewStringBssEntryPatch(
9332 const DexFile& dex_file, dex::StringIndex string_index) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009333 return NewPcRelativePatch(&dex_file, string_index.index_, &string_bss_entry_patches_);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01009334}
9335
Artem Serovd4cc5b22016-11-04 11:19:09 +00009336CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewPcRelativePatch(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009337 const DexFile* dex_file, uint32_t offset_or_index, ArenaDeque<PcRelativePatchInfo>* patches) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00009338 patches->emplace_back(dex_file, offset_or_index);
9339 return &patches->back();
9340}
9341
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01009342vixl::aarch32::Label* CodeGeneratorARMVIXL::NewBakerReadBarrierPatch(uint32_t custom_data) {
9343 baker_read_barrier_patches_.emplace_back(custom_data);
9344 return &baker_read_barrier_patches_.back().label;
9345}
9346
Artem Serovc5fcb442016-12-02 19:19:58 +00009347VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateBootImageAddressLiteral(uint32_t address) {
Richard Uhlerc52f3032017-03-02 13:45:45 +00009348 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), &uint32_literals_);
Artem Serovc5fcb442016-12-02 19:19:58 +00009349}
9350
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00009351VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateJitStringLiteral(
9352 const DexFile& dex_file,
9353 dex::StringIndex string_index,
9354 Handle<mirror::String> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01009355 ReserveJitStringRoot(StringReference(&dex_file, string_index), handle);
Artem Serovc5fcb442016-12-02 19:19:58 +00009356 return jit_string_patches_.GetOrCreate(
9357 StringReference(&dex_file, string_index),
9358 [this]() {
9359 return GetAssembler()->CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u);
9360 });
9361}
9362
9363VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateJitClassLiteral(const DexFile& dex_file,
9364 dex::TypeIndex type_index,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00009365 Handle<mirror::Class> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01009366 ReserveJitClassRoot(TypeReference(&dex_file, type_index), handle);
Artem Serovc5fcb442016-12-02 19:19:58 +00009367 return jit_class_patches_.GetOrCreate(
9368 TypeReference(&dex_file, type_index),
9369 [this]() {
9370 return GetAssembler()->CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u);
9371 });
9372}
9373
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01009374template <linker::LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
Artem Serovd4cc5b22016-11-04 11:19:09 +00009375inline void CodeGeneratorARMVIXL::EmitPcRelativeLinkerPatches(
9376 const ArenaDeque<PcRelativePatchInfo>& infos,
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01009377 ArenaVector<linker::LinkerPatch>* linker_patches) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00009378 for (const PcRelativePatchInfo& info : infos) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009379 const DexFile* dex_file = info.target_dex_file;
Artem Serovd4cc5b22016-11-04 11:19:09 +00009380 size_t offset_or_index = info.offset_or_index;
9381 DCHECK(info.add_pc_label.IsBound());
9382 uint32_t add_pc_offset = dchecked_integral_cast<uint32_t>(info.add_pc_label.GetLocation());
9383 // Add MOVW patch.
9384 DCHECK(info.movw_label.IsBound());
9385 uint32_t movw_offset = dchecked_integral_cast<uint32_t>(info.movw_label.GetLocation());
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009386 linker_patches->push_back(Factory(movw_offset, dex_file, add_pc_offset, offset_or_index));
Artem Serovd4cc5b22016-11-04 11:19:09 +00009387 // Add MOVT patch.
9388 DCHECK(info.movt_label.IsBound());
9389 uint32_t movt_offset = dchecked_integral_cast<uint32_t>(info.movt_label.GetLocation());
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009390 linker_patches->push_back(Factory(movt_offset, dex_file, add_pc_offset, offset_or_index));
Artem Serovd4cc5b22016-11-04 11:19:09 +00009391 }
9392}
9393
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01009394void CodeGeneratorARMVIXL::EmitLinkerPatches(ArenaVector<linker::LinkerPatch>* linker_patches) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00009395 DCHECK(linker_patches->empty());
9396 size_t size =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009397 /* MOVW+MOVT for each entry */ 2u * boot_image_method_patches_.size() +
Vladimir Marko0eb882b2017-05-15 13:39:18 +01009398 /* MOVW+MOVT for each entry */ 2u * method_bss_entry_patches_.size() +
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009399 /* MOVW+MOVT for each entry */ 2u * boot_image_type_patches_.size() +
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01009400 /* MOVW+MOVT for each entry */ 2u * type_bss_entry_patches_.size() +
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009401 /* MOVW+MOVT for each entry */ 2u * boot_image_string_patches_.size() +
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01009402 /* MOVW+MOVT for each entry */ 2u * string_bss_entry_patches_.size() +
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01009403 baker_read_barrier_patches_.size();
Artem Serovd4cc5b22016-11-04 11:19:09 +00009404 linker_patches->reserve(size);
Vladimir Marko65979462017-05-19 17:25:12 +01009405 if (GetCompilerOptions().IsBootImage()) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01009406 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeMethodPatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009407 boot_image_method_patches_, linker_patches);
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01009408 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeTypePatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009409 boot_image_type_patches_, linker_patches);
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01009410 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeStringPatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009411 boot_image_string_patches_, linker_patches);
Vladimir Marko65979462017-05-19 17:25:12 +01009412 } else {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009413 DCHECK(boot_image_method_patches_.empty());
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01009414 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeClassTablePatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009415 boot_image_type_patches_, linker_patches);
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01009416 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringInternTablePatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009417 boot_image_string_patches_, linker_patches);
Artem Serovd4cc5b22016-11-04 11:19:09 +00009418 }
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01009419 EmitPcRelativeLinkerPatches<linker::LinkerPatch::MethodBssEntryPatch>(
9420 method_bss_entry_patches_, linker_patches);
9421 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeBssEntryPatch>(
9422 type_bss_entry_patches_, linker_patches);
9423 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringBssEntryPatch>(
9424 string_bss_entry_patches_, linker_patches);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01009425 for (const BakerReadBarrierPatchInfo& info : baker_read_barrier_patches_) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01009426 linker_patches->push_back(linker::LinkerPatch::BakerReadBarrierBranchPatch(
9427 info.label.GetLocation(), info.custom_data));
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01009428 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00009429 DCHECK_EQ(size, linker_patches->size());
Artem Serovc5fcb442016-12-02 19:19:58 +00009430}
9431
9432VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateUint32Literal(
9433 uint32_t value,
9434 Uint32ToLiteralMap* map) {
9435 return map->GetOrCreate(
9436 value,
9437 [this, value]() {
9438 return GetAssembler()->CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ value);
9439 });
9440}
9441
Artem Serov2bbc9532016-10-21 11:51:50 +01009442void LocationsBuilderARMVIXL::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
9443 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01009444 new (GetGraph()->GetAllocator()) LocationSummary(instr, LocationSummary::kNoCall);
Artem Serov2bbc9532016-10-21 11:51:50 +01009445 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
9446 Location::RequiresRegister());
9447 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
9448 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
9449 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
9450}
9451
9452void InstructionCodeGeneratorARMVIXL::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
9453 vixl32::Register res = OutputRegister(instr);
9454 vixl32::Register accumulator =
9455 InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
9456 vixl32::Register mul_left =
9457 InputRegisterAt(instr, HMultiplyAccumulate::kInputMulLeftIndex);
9458 vixl32::Register mul_right =
9459 InputRegisterAt(instr, HMultiplyAccumulate::kInputMulRightIndex);
9460
9461 if (instr->GetOpKind() == HInstruction::kAdd) {
9462 __ Mla(res, mul_left, mul_right, accumulator);
9463 } else {
9464 __ Mls(res, mul_left, mul_right, accumulator);
9465 }
9466}
9467
Artem Serov551b28f2016-10-18 19:11:30 +01009468void LocationsBuilderARMVIXL::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
9469 // Nothing to do, this should be removed during prepare for register allocator.
9470 LOG(FATAL) << "Unreachable";
9471}
9472
9473void InstructionCodeGeneratorARMVIXL::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
9474 // Nothing to do, this should be removed during prepare for register allocator.
9475 LOG(FATAL) << "Unreachable";
9476}
9477
9478// Simple implementation of packed switch - generate cascaded compare/jumps.
9479void LocationsBuilderARMVIXL::VisitPackedSwitch(HPackedSwitch* switch_instr) {
9480 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01009481 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Artem Serov551b28f2016-10-18 19:11:30 +01009482 locations->SetInAt(0, Location::RequiresRegister());
9483 if (switch_instr->GetNumEntries() > kPackedSwitchCompareJumpThreshold &&
9484 codegen_->GetAssembler()->GetVIXLAssembler()->IsUsingT32()) {
9485 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the table base.
9486 if (switch_instr->GetStartValue() != 0) {
9487 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the bias.
9488 }
9489 }
9490}
9491
9492// TODO(VIXL): Investigate and reach the parity with old arm codegen.
9493void InstructionCodeGeneratorARMVIXL::VisitPackedSwitch(HPackedSwitch* switch_instr) {
9494 int32_t lower_bound = switch_instr->GetStartValue();
9495 uint32_t num_entries = switch_instr->GetNumEntries();
9496 LocationSummary* locations = switch_instr->GetLocations();
9497 vixl32::Register value_reg = InputRegisterAt(switch_instr, 0);
9498 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
9499
9500 if (num_entries <= kPackedSwitchCompareJumpThreshold ||
9501 !codegen_->GetAssembler()->GetVIXLAssembler()->IsUsingT32()) {
9502 // Create a series of compare/jumps.
Anton Kirilovedb2ac32016-11-30 15:14:10 +00009503 UseScratchRegisterScope temps(GetVIXLAssembler());
Artem Serov551b28f2016-10-18 19:11:30 +01009504 vixl32::Register temp_reg = temps.Acquire();
9505 // Note: It is fine for the below AddConstantSetFlags() using IP register to temporarily store
9506 // the immediate, because IP is used as the destination register. For the other
9507 // AddConstantSetFlags() and GenerateCompareWithImmediate(), the immediate values are constant,
9508 // and they can be encoded in the instruction without making use of IP register.
9509 __ Adds(temp_reg, value_reg, -lower_bound);
9510
9511 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
9512 // Jump to successors[0] if value == lower_bound.
9513 __ B(eq, codegen_->GetLabelOf(successors[0]));
9514 int32_t last_index = 0;
9515 for (; num_entries - last_index > 2; last_index += 2) {
9516 __ Adds(temp_reg, temp_reg, -2);
9517 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
9518 __ B(lo, codegen_->GetLabelOf(successors[last_index + 1]));
9519 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
9520 __ B(eq, codegen_->GetLabelOf(successors[last_index + 2]));
9521 }
9522 if (num_entries - last_index == 2) {
9523 // The last missing case_value.
9524 __ Cmp(temp_reg, 1);
9525 __ B(eq, codegen_->GetLabelOf(successors[last_index + 1]));
9526 }
9527
9528 // And the default for any other value.
9529 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
9530 __ B(codegen_->GetLabelOf(default_block));
9531 }
9532 } else {
9533 // Create a table lookup.
9534 vixl32::Register table_base = RegisterFrom(locations->GetTemp(0));
9535
9536 JumpTableARMVIXL* jump_table = codegen_->CreateJumpTable(switch_instr);
9537
9538 // Remove the bias.
9539 vixl32::Register key_reg;
9540 if (lower_bound != 0) {
9541 key_reg = RegisterFrom(locations->GetTemp(1));
9542 __ Sub(key_reg, value_reg, lower_bound);
9543 } else {
9544 key_reg = value_reg;
9545 }
9546
9547 // Check whether the value is in the table, jump to default block if not.
9548 __ Cmp(key_reg, num_entries - 1);
9549 __ B(hi, codegen_->GetLabelOf(default_block));
9550
Anton Kirilovedb2ac32016-11-30 15:14:10 +00009551 UseScratchRegisterScope temps(GetVIXLAssembler());
Artem Serov551b28f2016-10-18 19:11:30 +01009552 vixl32::Register jump_offset = temps.Acquire();
9553
9554 // Load jump offset from the table.
Scott Wakeling86e9d262017-01-18 15:59:24 +00009555 {
9556 const size_t jump_size = switch_instr->GetNumEntries() * sizeof(int32_t);
9557 ExactAssemblyScope aas(GetVIXLAssembler(),
9558 (vixl32::kMaxInstructionSizeInBytes * 4) + jump_size,
9559 CodeBufferCheckScope::kMaximumSize);
9560 __ adr(table_base, jump_table->GetTableStartLabel());
9561 __ ldr(jump_offset, MemOperand(table_base, key_reg, vixl32::LSL, 2));
Artem Serov551b28f2016-10-18 19:11:30 +01009562
Scott Wakeling86e9d262017-01-18 15:59:24 +00009563 // Jump to target block by branching to table_base(pc related) + offset.
9564 vixl32::Register target_address = table_base;
9565 __ add(target_address, table_base, jump_offset);
9566 __ bx(target_address);
Artem Serov09a940d2016-11-11 16:15:11 +00009567
Scott Wakeling86e9d262017-01-18 15:59:24 +00009568 jump_table->EmitTable(codegen_);
9569 }
Artem Serov551b28f2016-10-18 19:11:30 +01009570 }
9571}
9572
Artem Serov02d37832016-10-25 15:25:33 +01009573// Copy the result of a call into the given target.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009574void CodeGeneratorARMVIXL::MoveFromReturnRegister(Location trg, DataType::Type type) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01009575 if (!trg.IsValid()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009576 DCHECK_EQ(type, DataType::Type::kVoid);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01009577 return;
9578 }
9579
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009580 DCHECK_NE(type, DataType::Type::kVoid);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01009581
Artem Serovd4cc5b22016-11-04 11:19:09 +00009582 Location return_loc = InvokeDexCallingConventionVisitorARMVIXL().GetReturnLocation(type);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01009583 if (return_loc.Equals(trg)) {
9584 return;
9585 }
9586
9587 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
9588 // with the last branch.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009589 if (type == DataType::Type::kInt64) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01009590 TODO_VIXL32(FATAL);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009591 } else if (type == DataType::Type::kFloat64) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01009592 TODO_VIXL32(FATAL);
9593 } else {
9594 // Let the parallel move resolver take care of all of this.
Vladimir Markoca6fff82017-10-03 14:49:14 +01009595 HParallelMove parallel_move(GetGraph()->GetAllocator());
Anton Kirilove28d9ae2016-10-25 18:17:23 +01009596 parallel_move.AddMove(return_loc, trg, type, nullptr);
9597 GetMoveResolver()->EmitNativeCode(&parallel_move);
9598 }
Scott Wakelingfe885462016-09-22 10:24:38 +01009599}
9600
xueliang.zhong8d2c4592016-11-23 17:05:25 +00009601void LocationsBuilderARMVIXL::VisitClassTableGet(HClassTableGet* instruction) {
9602 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01009603 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
xueliang.zhong8d2c4592016-11-23 17:05:25 +00009604 locations->SetInAt(0, Location::RequiresRegister());
9605 locations->SetOut(Location::RequiresRegister());
Artem Serov551b28f2016-10-18 19:11:30 +01009606}
9607
xueliang.zhong8d2c4592016-11-23 17:05:25 +00009608void InstructionCodeGeneratorARMVIXL::VisitClassTableGet(HClassTableGet* instruction) {
9609 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
9610 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
9611 instruction->GetIndex(), kArmPointerSize).SizeValue();
9612 GetAssembler()->LoadFromOffset(kLoadWord,
9613 OutputRegister(instruction),
9614 InputRegisterAt(instruction, 0),
9615 method_offset);
9616 } else {
9617 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
9618 instruction->GetIndex(), kArmPointerSize));
9619 GetAssembler()->LoadFromOffset(kLoadWord,
9620 OutputRegister(instruction),
9621 InputRegisterAt(instruction, 0),
9622 mirror::Class::ImtPtrOffset(kArmPointerSize).Uint32Value());
9623 GetAssembler()->LoadFromOffset(kLoadWord,
9624 OutputRegister(instruction),
9625 OutputRegister(instruction),
9626 method_offset);
9627 }
Artem Serov551b28f2016-10-18 19:11:30 +01009628}
9629
Artem Serovc5fcb442016-12-02 19:19:58 +00009630static void PatchJitRootUse(uint8_t* code,
9631 const uint8_t* roots_data,
9632 VIXLUInt32Literal* literal,
9633 uint64_t index_in_table) {
9634 DCHECK(literal->IsBound());
9635 uint32_t literal_offset = literal->GetLocation();
9636 uintptr_t address =
9637 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
9638 uint8_t* data = code + literal_offset;
9639 reinterpret_cast<uint32_t*>(data)[0] = dchecked_integral_cast<uint32_t>(address);
9640}
9641
9642void CodeGeneratorARMVIXL::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
9643 for (const auto& entry : jit_string_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01009644 const StringReference& string_reference = entry.first;
9645 VIXLUInt32Literal* table_entry_literal = entry.second;
Vladimir Marko174b2e22017-10-12 13:34:49 +01009646 uint64_t index_in_table = GetJitStringRootIndex(string_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01009647 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Artem Serovc5fcb442016-12-02 19:19:58 +00009648 }
9649 for (const auto& entry : jit_class_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01009650 const TypeReference& type_reference = entry.first;
9651 VIXLUInt32Literal* table_entry_literal = entry.second;
Vladimir Marko174b2e22017-10-12 13:34:49 +01009652 uint64_t index_in_table = GetJitClassRootIndex(type_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01009653 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Artem Serovc5fcb442016-12-02 19:19:58 +00009654 }
9655}
9656
Artem Serovd4cc5b22016-11-04 11:19:09 +00009657void CodeGeneratorARMVIXL::EmitMovwMovtPlaceholder(
9658 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels,
9659 vixl32::Register out) {
Artem Serov0fb37192016-12-06 18:13:40 +00009660 ExactAssemblyScope aas(GetVIXLAssembler(),
9661 3 * vixl32::kMaxInstructionSizeInBytes,
9662 CodeBufferCheckScope::kMaximumSize);
Artem Serovd4cc5b22016-11-04 11:19:09 +00009663 // TODO(VIXL): Think about using mov instead of movw.
9664 __ bind(&labels->movw_label);
9665 __ movw(out, /* placeholder */ 0u);
9666 __ bind(&labels->movt_label);
9667 __ movt(out, /* placeholder */ 0u);
9668 __ bind(&labels->add_pc_label);
9669 __ add(out, out, pc);
9670}
9671
Scott Wakelingfe885462016-09-22 10:24:38 +01009672#undef __
9673#undef QUICK_ENTRY_POINT
9674#undef TODO_VIXL32
9675
9676} // namespace arm
9677} // namespace art