blob: d4fb064107a5c9a099b7984769832f4a6dd9bfbf [file] [log] [blame]
Scott Wakelingfe885462016-09-22 10:24:38 +01001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_arm_vixl.h"
18
Vladimir Markoeee1c0e2017-04-21 17:58:41 +010019#include "arch/arm/asm_support_arm.h"
Scott Wakelingfe885462016-09-22 10:24:38 +010020#include "arch/arm/instruction_set_features_arm.h"
21#include "art_method.h"
Andreas Gampe5678db52017-06-08 14:11:18 -070022#include "base/bit_utils.h"
23#include "base/bit_utils_iterator.h"
Vladimir Marko94ec2db2017-09-06 17:21:03 +010024#include "class_table.h"
Scott Wakelingfe885462016-09-22 10:24:38 +010025#include "code_generator_utils.h"
26#include "common_arm.h"
27#include "compiled_method.h"
28#include "entrypoints/quick/quick_entrypoints.h"
29#include "gc/accounting/card_table.h"
Andreas Gampe09659c22017-09-18 18:23:32 -070030#include "heap_poisoning.h"
Anton Kirilov5ec62182016-10-13 20:16:02 +010031#include "intrinsics_arm_vixl.h"
Vladimir Markoeee1c0e2017-04-21 17:58:41 +010032#include "linker/arm/relative_patcher_thumb2.h"
Vladimir Markod8dbc8d2017-09-20 13:37:47 +010033#include "linker/linker_patch.h"
Scott Wakelingfe885462016-09-22 10:24:38 +010034#include "mirror/array-inl.h"
35#include "mirror/class-inl.h"
36#include "thread.h"
37#include "utils/arm/assembler_arm_vixl.h"
38#include "utils/arm/managed_register_arm.h"
39#include "utils/assembler.h"
40#include "utils/stack_checks.h"
41
42namespace art {
43namespace arm {
44
45namespace vixl32 = vixl::aarch32;
46using namespace vixl32; // NOLINT(build/namespaces)
47
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +010048using helpers::DRegisterFrom;
Scott Wakelingfe885462016-09-22 10:24:38 +010049using helpers::DWARFReg;
Scott Wakelinga7812ae2016-10-17 10:03:36 +010050using helpers::HighDRegisterFrom;
51using helpers::HighRegisterFrom;
Donghui Bai426b49c2016-11-08 14:55:38 +080052using helpers::InputDRegisterAt;
Scott Wakelingfe885462016-09-22 10:24:38 +010053using helpers::InputOperandAt;
Scott Wakelingc34dba72016-10-03 10:14:44 +010054using helpers::InputRegister;
Scott Wakelinga7812ae2016-10-17 10:03:36 +010055using helpers::InputRegisterAt;
Scott Wakelingfe885462016-09-22 10:24:38 +010056using helpers::InputSRegisterAt;
Anton Kirilov644032c2016-12-06 17:51:43 +000057using helpers::InputVRegister;
Scott Wakelinga7812ae2016-10-17 10:03:36 +010058using helpers::InputVRegisterAt;
Scott Wakelingb77051e2016-11-21 19:46:00 +000059using helpers::Int32ConstantFrom;
Anton Kirilov644032c2016-12-06 17:51:43 +000060using helpers::Int64ConstantFrom;
Scott Wakelinga7812ae2016-10-17 10:03:36 +010061using helpers::LocationFrom;
62using helpers::LowRegisterFrom;
63using helpers::LowSRegisterFrom;
Donghui Bai426b49c2016-11-08 14:55:38 +080064using helpers::OperandFrom;
Scott Wakelinga7812ae2016-10-17 10:03:36 +010065using helpers::OutputRegister;
66using helpers::OutputSRegister;
67using helpers::OutputVRegister;
68using helpers::RegisterFrom;
69using helpers::SRegisterFrom;
Anton Kirilov644032c2016-12-06 17:51:43 +000070using helpers::Uint64ConstantFrom;
Scott Wakelingfe885462016-09-22 10:24:38 +010071
Artem Serov0fb37192016-12-06 18:13:40 +000072using vixl::ExactAssemblyScope;
73using vixl::CodeBufferCheckScope;
74
Scott Wakelingfe885462016-09-22 10:24:38 +010075using RegisterList = vixl32::RegisterList;
76
77static bool ExpectedPairLayout(Location location) {
78 // We expected this for both core and fpu register pairs.
79 return ((location.low() & 1) == 0) && (location.low() + 1 == location.high());
80}
Artem Serovd4cc5b22016-11-04 11:19:09 +000081// Use a local definition to prevent copying mistakes.
82static constexpr size_t kArmWordSize = static_cast<size_t>(kArmPointerSize);
83static constexpr size_t kArmBitsPerWord = kArmWordSize * kBitsPerByte;
Artem Serov551b28f2016-10-18 19:11:30 +010084static constexpr uint32_t kPackedSwitchCompareJumpThreshold = 7;
Scott Wakelingfe885462016-09-22 10:24:38 +010085
Vladimir Markoeee1c0e2017-04-21 17:58:41 +010086// Reference load (except object array loads) is using LDR Rt, [Rn, #offset] which can handle
87// offset < 4KiB. For offsets >= 4KiB, the load shall be emitted as two or more instructions.
88// For the Baker read barrier implementation using link-generated thunks we need to split
89// the offset explicitly.
90constexpr uint32_t kReferenceLoadMinFarOffset = 4 * KB;
91
92// Flags controlling the use of link-time generated thunks for Baker read barriers.
93constexpr bool kBakerReadBarrierLinkTimeThunksEnableForFields = true;
94constexpr bool kBakerReadBarrierLinkTimeThunksEnableForArrays = true;
95constexpr bool kBakerReadBarrierLinkTimeThunksEnableForGcRoots = true;
96
97// The reserved entrypoint register for link-time generated thunks.
98const vixl32::Register kBakerCcEntrypointRegister = r4;
99
Roland Levillain5daa4952017-07-03 17:23:56 +0100100// Using a base helps identify when we hit Marking Register check breakpoints.
101constexpr int kMarkingRegisterCheckBreakCodeBaseCode = 0x10;
102
Scott Wakelingfe885462016-09-22 10:24:38 +0100103#ifdef __
104#error "ARM Codegen VIXL macro-assembler macro already defined."
105#endif
106
Scott Wakelingfe885462016-09-22 10:24:38 +0100107// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
108#define __ down_cast<CodeGeneratorARMVIXL*>(codegen)->GetVIXLAssembler()-> // NOLINT
109#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kArmPointerSize, x).Int32Value()
110
111// Marker that code is yet to be, and must, be implemented.
112#define TODO_VIXL32(level) LOG(level) << __PRETTY_FUNCTION__ << " unimplemented "
113
Vladimir Markoeee1c0e2017-04-21 17:58:41 +0100114static inline void ExcludeIPAndBakerCcEntrypointRegister(UseScratchRegisterScope* temps,
115 HInstruction* instruction) {
116 DCHECK(temps->IsAvailable(ip));
117 temps->Exclude(ip);
118 DCHECK(!temps->IsAvailable(kBakerCcEntrypointRegister));
119 DCHECK_EQ(kBakerCcEntrypointRegister.GetCode(),
120 linker::Thumb2RelativePatcher::kBakerCcEntrypointRegister);
121 DCHECK_NE(instruction->GetLocations()->GetTempCount(), 0u);
122 DCHECK(RegisterFrom(instruction->GetLocations()->GetTemp(
123 instruction->GetLocations()->GetTempCount() - 1u)).Is(kBakerCcEntrypointRegister));
124}
125
126static inline void EmitPlaceholderBne(CodeGeneratorARMVIXL* codegen, vixl32::Label* patch_label) {
127 ExactAssemblyScope eas(codegen->GetVIXLAssembler(), kMaxInstructionSizeInBytes);
128 __ bind(patch_label);
129 vixl32::Label placeholder_label;
130 __ b(ne, EncodingSize(Wide), &placeholder_label); // Placeholder, patched at link-time.
131 __ bind(&placeholder_label);
132}
133
Vladimir Marko88abba22017-05-03 17:09:25 +0100134static inline bool CanEmitNarrowLdr(vixl32::Register rt, vixl32::Register rn, uint32_t offset) {
135 return rt.IsLow() && rn.IsLow() && offset < 32u;
136}
137
Vladimir Markoeee1c0e2017-04-21 17:58:41 +0100138class EmitAdrCode {
139 public:
140 EmitAdrCode(ArmVIXLMacroAssembler* assembler, vixl32::Register rd, vixl32::Label* label)
141 : assembler_(assembler), rd_(rd), label_(label) {
142 ExactAssemblyScope aas(assembler, kMaxInstructionSizeInBytes);
143 adr_location_ = assembler->GetCursorOffset();
144 assembler->adr(EncodingSize(Wide), rd, label);
145 }
146
147 ~EmitAdrCode() {
148 DCHECK(label_->IsBound());
149 // The ADR emitted by the assembler does not set the Thumb mode bit we need.
150 // TODO: Maybe extend VIXL to allow ADR for return address?
151 uint8_t* raw_adr = assembler_->GetBuffer()->GetOffsetAddress<uint8_t*>(adr_location_);
152 // Expecting ADR encoding T3 with `(offset & 1) == 0`.
153 DCHECK_EQ(raw_adr[1] & 0xfbu, 0xf2u); // Check bits 24-31, except 26.
154 DCHECK_EQ(raw_adr[0] & 0xffu, 0x0fu); // Check bits 16-23.
155 DCHECK_EQ(raw_adr[3] & 0x8fu, rd_.GetCode()); // Check bits 8-11 and 15.
156 DCHECK_EQ(raw_adr[2] & 0x01u, 0x00u); // Check bit 0, i.e. the `offset & 1`.
157 // Add the Thumb mode bit.
158 raw_adr[2] |= 0x01u;
159 }
160
161 private:
162 ArmVIXLMacroAssembler* const assembler_;
163 vixl32::Register rd_;
164 vixl32::Label* const label_;
165 int32_t adr_location_;
166};
167
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100168// SaveLiveRegisters and RestoreLiveRegisters from SlowPathCodeARM operate on sets of S registers,
169// for each live D registers they treat two corresponding S registers as live ones.
170//
171// Two following functions (SaveContiguousSRegisterList, RestoreContiguousSRegisterList) build
172// from a list of contiguous S registers a list of contiguous D registers (processing first/last
173// S registers corner cases) and save/restore this new list treating them as D registers.
174// - decreasing code size
175// - avoiding hazards on Cortex-A57, when a pair of S registers for an actual live D register is
176// restored and then used in regular non SlowPath code as D register.
177//
178// For the following example (v means the S register is live):
179// D names: | D0 | D1 | D2 | D4 | ...
180// S names: | S0 | S1 | S2 | S3 | S4 | S5 | S6 | S7 | ...
181// Live? | | v | v | v | v | v | v | | ...
182//
183// S1 and S6 will be saved/restored independently; D registers list (D1, D2) will be processed
184// as D registers.
185//
186// TODO(VIXL): All this code should be unnecessary once the VIXL AArch32 backend provides helpers
187// for lists of floating-point registers.
188static size_t SaveContiguousSRegisterList(size_t first,
189 size_t last,
190 CodeGenerator* codegen,
191 size_t stack_offset) {
192 static_assert(kSRegSizeInBytes == kArmWordSize, "Broken assumption on reg/word sizes.");
193 static_assert(kDRegSizeInBytes == 2 * kArmWordSize, "Broken assumption on reg/word sizes.");
194 DCHECK_LE(first, last);
195 if ((first == last) && (first == 0)) {
196 __ Vstr(vixl32::SRegister(first), MemOperand(sp, stack_offset));
197 return stack_offset + kSRegSizeInBytes;
198 }
199 if (first % 2 == 1) {
200 __ Vstr(vixl32::SRegister(first++), MemOperand(sp, stack_offset));
201 stack_offset += kSRegSizeInBytes;
202 }
203
204 bool save_last = false;
205 if (last % 2 == 0) {
206 save_last = true;
207 --last;
208 }
209
210 if (first < last) {
211 vixl32::DRegister d_reg = vixl32::DRegister(first / 2);
212 DCHECK_EQ((last - first + 1) % 2, 0u);
213 size_t number_of_d_regs = (last - first + 1) / 2;
214
215 if (number_of_d_regs == 1) {
216 __ Vstr(d_reg, MemOperand(sp, stack_offset));
217 } else if (number_of_d_regs > 1) {
218 UseScratchRegisterScope temps(down_cast<CodeGeneratorARMVIXL*>(codegen)->GetVIXLAssembler());
219 vixl32::Register base = sp;
220 if (stack_offset != 0) {
221 base = temps.Acquire();
Scott Wakelingb77051e2016-11-21 19:46:00 +0000222 __ Add(base, sp, Operand::From(stack_offset));
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100223 }
224 __ Vstm(F64, base, NO_WRITE_BACK, DRegisterList(d_reg, number_of_d_regs));
225 }
226 stack_offset += number_of_d_regs * kDRegSizeInBytes;
227 }
228
229 if (save_last) {
230 __ Vstr(vixl32::SRegister(last + 1), MemOperand(sp, stack_offset));
231 stack_offset += kSRegSizeInBytes;
232 }
233
234 return stack_offset;
235}
236
237static size_t RestoreContiguousSRegisterList(size_t first,
238 size_t last,
239 CodeGenerator* codegen,
240 size_t stack_offset) {
241 static_assert(kSRegSizeInBytes == kArmWordSize, "Broken assumption on reg/word sizes.");
242 static_assert(kDRegSizeInBytes == 2 * kArmWordSize, "Broken assumption on reg/word sizes.");
243 DCHECK_LE(first, last);
244 if ((first == last) && (first == 0)) {
245 __ Vldr(vixl32::SRegister(first), MemOperand(sp, stack_offset));
246 return stack_offset + kSRegSizeInBytes;
247 }
248 if (first % 2 == 1) {
249 __ Vldr(vixl32::SRegister(first++), MemOperand(sp, stack_offset));
250 stack_offset += kSRegSizeInBytes;
251 }
252
253 bool restore_last = false;
254 if (last % 2 == 0) {
255 restore_last = true;
256 --last;
257 }
258
259 if (first < last) {
260 vixl32::DRegister d_reg = vixl32::DRegister(first / 2);
261 DCHECK_EQ((last - first + 1) % 2, 0u);
262 size_t number_of_d_regs = (last - first + 1) / 2;
263 if (number_of_d_regs == 1) {
264 __ Vldr(d_reg, MemOperand(sp, stack_offset));
265 } else if (number_of_d_regs > 1) {
266 UseScratchRegisterScope temps(down_cast<CodeGeneratorARMVIXL*>(codegen)->GetVIXLAssembler());
267 vixl32::Register base = sp;
268 if (stack_offset != 0) {
269 base = temps.Acquire();
Scott Wakelingb77051e2016-11-21 19:46:00 +0000270 __ Add(base, sp, Operand::From(stack_offset));
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100271 }
272 __ Vldm(F64, base, NO_WRITE_BACK, DRegisterList(d_reg, number_of_d_regs));
273 }
274 stack_offset += number_of_d_regs * kDRegSizeInBytes;
275 }
276
277 if (restore_last) {
278 __ Vldr(vixl32::SRegister(last + 1), MemOperand(sp, stack_offset));
279 stack_offset += kSRegSizeInBytes;
280 }
281
282 return stack_offset;
283}
284
Vladimir Markod5d2f2c2017-09-26 12:37:26 +0100285static LoadOperandType GetLoadOperandType(DataType::Type type) {
286 switch (type) {
287 case DataType::Type::kReference:
288 return kLoadWord;
289 case DataType::Type::kBool:
290 case DataType::Type::kUint8:
291 return kLoadUnsignedByte;
292 case DataType::Type::kInt8:
293 return kLoadSignedByte;
294 case DataType::Type::kUint16:
295 return kLoadUnsignedHalfword;
296 case DataType::Type::kInt16:
297 return kLoadSignedHalfword;
298 case DataType::Type::kInt32:
299 return kLoadWord;
300 case DataType::Type::kInt64:
301 return kLoadWordPair;
302 case DataType::Type::kFloat32:
303 return kLoadSWord;
304 case DataType::Type::kFloat64:
305 return kLoadDWord;
306 default:
307 LOG(FATAL) << "Unreachable type " << type;
308 UNREACHABLE();
309 }
310}
311
312static StoreOperandType GetStoreOperandType(DataType::Type type) {
313 switch (type) {
314 case DataType::Type::kReference:
315 return kStoreWord;
316 case DataType::Type::kBool:
317 case DataType::Type::kUint8:
318 case DataType::Type::kInt8:
319 return kStoreByte;
320 case DataType::Type::kUint16:
321 case DataType::Type::kInt16:
322 return kStoreHalfword;
323 case DataType::Type::kInt32:
324 return kStoreWord;
325 case DataType::Type::kInt64:
326 return kStoreWordPair;
327 case DataType::Type::kFloat32:
328 return kStoreSWord;
329 case DataType::Type::kFloat64:
330 return kStoreDWord;
331 default:
332 LOG(FATAL) << "Unreachable type " << type;
333 UNREACHABLE();
334 }
335}
336
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100337void SlowPathCodeARMVIXL::SaveLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
338 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
339 size_t orig_offset = stack_offset;
340
341 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ true);
342 for (uint32_t i : LowToHighBits(core_spills)) {
343 // If the register holds an object, update the stack mask.
344 if (locations->RegisterContainsObject(i)) {
345 locations->SetStackBit(stack_offset / kVRegSize);
346 }
347 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
348 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
349 saved_core_stack_offsets_[i] = stack_offset;
350 stack_offset += kArmWordSize;
351 }
352
353 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
354 arm_codegen->GetAssembler()->StoreRegisterList(core_spills, orig_offset);
355
356 uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ false);
357 orig_offset = stack_offset;
358 for (uint32_t i : LowToHighBits(fp_spills)) {
359 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
360 saved_fpu_stack_offsets_[i] = stack_offset;
361 stack_offset += kArmWordSize;
362 }
363
364 stack_offset = orig_offset;
365 while (fp_spills != 0u) {
366 uint32_t begin = CTZ(fp_spills);
367 uint32_t tmp = fp_spills + (1u << begin);
368 fp_spills &= tmp; // Clear the contiguous range of 1s.
369 uint32_t end = (tmp == 0u) ? 32u : CTZ(tmp); // CTZ(0) is undefined.
370 stack_offset = SaveContiguousSRegisterList(begin, end - 1, codegen, stack_offset);
371 }
372 DCHECK_LE(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
373}
374
375void SlowPathCodeARMVIXL::RestoreLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
376 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
377 size_t orig_offset = stack_offset;
378
379 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ true);
380 for (uint32_t i : LowToHighBits(core_spills)) {
381 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
382 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
383 stack_offset += kArmWordSize;
384 }
385
386 // TODO(VIXL): Check the coherency of stack_offset after this with a test.
387 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
388 arm_codegen->GetAssembler()->LoadRegisterList(core_spills, orig_offset);
389
390 uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ false);
391 while (fp_spills != 0u) {
392 uint32_t begin = CTZ(fp_spills);
393 uint32_t tmp = fp_spills + (1u << begin);
394 fp_spills &= tmp; // Clear the contiguous range of 1s.
395 uint32_t end = (tmp == 0u) ? 32u : CTZ(tmp); // CTZ(0) is undefined.
396 stack_offset = RestoreContiguousSRegisterList(begin, end - 1, codegen, stack_offset);
397 }
398 DCHECK_LE(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
399}
400
401class NullCheckSlowPathARMVIXL : public SlowPathCodeARMVIXL {
402 public:
403 explicit NullCheckSlowPathARMVIXL(HNullCheck* instruction) : SlowPathCodeARMVIXL(instruction) {}
404
405 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
406 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
407 __ Bind(GetEntryLabel());
408 if (instruction_->CanThrowIntoCatchBlock()) {
409 // Live registers will be restored in the catch block if caught.
410 SaveLiveRegisters(codegen, instruction_->GetLocations());
411 }
412 arm_codegen->InvokeRuntime(kQuickThrowNullPointer,
413 instruction_,
414 instruction_->GetDexPc(),
415 this);
416 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
417 }
418
419 bool IsFatal() const OVERRIDE { return true; }
420
421 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathARMVIXL"; }
422
423 private:
424 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARMVIXL);
425};
426
Scott Wakelingfe885462016-09-22 10:24:38 +0100427class DivZeroCheckSlowPathARMVIXL : public SlowPathCodeARMVIXL {
428 public:
429 explicit DivZeroCheckSlowPathARMVIXL(HDivZeroCheck* instruction)
430 : SlowPathCodeARMVIXL(instruction) {}
431
432 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100433 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
Scott Wakelingfe885462016-09-22 10:24:38 +0100434 __ Bind(GetEntryLabel());
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100435 arm_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Scott Wakelingfe885462016-09-22 10:24:38 +0100436 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
437 }
438
439 bool IsFatal() const OVERRIDE { return true; }
440
441 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathARMVIXL"; }
442
443 private:
444 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARMVIXL);
445};
446
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100447class SuspendCheckSlowPathARMVIXL : public SlowPathCodeARMVIXL {
448 public:
449 SuspendCheckSlowPathARMVIXL(HSuspendCheck* instruction, HBasicBlock* successor)
450 : SlowPathCodeARMVIXL(instruction), successor_(successor) {}
451
452 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
453 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
454 __ Bind(GetEntryLabel());
455 arm_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
456 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
457 if (successor_ == nullptr) {
458 __ B(GetReturnLabel());
459 } else {
460 __ B(arm_codegen->GetLabelOf(successor_));
461 }
462 }
463
464 vixl32::Label* GetReturnLabel() {
465 DCHECK(successor_ == nullptr);
466 return &return_label_;
467 }
468
469 HBasicBlock* GetSuccessor() const {
470 return successor_;
471 }
472
473 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathARMVIXL"; }
474
475 private:
476 // If not null, the block to branch to after the suspend check.
477 HBasicBlock* const successor_;
478
479 // If `successor_` is null, the label to branch to after the suspend check.
480 vixl32::Label return_label_;
481
482 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARMVIXL);
483};
484
Scott Wakelingc34dba72016-10-03 10:14:44 +0100485class BoundsCheckSlowPathARMVIXL : public SlowPathCodeARMVIXL {
486 public:
487 explicit BoundsCheckSlowPathARMVIXL(HBoundsCheck* instruction)
488 : SlowPathCodeARMVIXL(instruction) {}
489
490 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
491 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
492 LocationSummary* locations = instruction_->GetLocations();
493
494 __ Bind(GetEntryLabel());
495 if (instruction_->CanThrowIntoCatchBlock()) {
496 // Live registers will be restored in the catch block if caught.
497 SaveLiveRegisters(codegen, instruction_->GetLocations());
498 }
499 // We're moving two locations to locations that could overlap, so we need a parallel
500 // move resolver.
501 InvokeRuntimeCallingConventionARMVIXL calling_convention;
502 codegen->EmitParallelMoves(
503 locations->InAt(0),
504 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100505 DataType::Type::kInt32,
Scott Wakelingc34dba72016-10-03 10:14:44 +0100506 locations->InAt(1),
507 LocationFrom(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100508 DataType::Type::kInt32);
Scott Wakelingc34dba72016-10-03 10:14:44 +0100509 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
510 ? kQuickThrowStringBounds
511 : kQuickThrowArrayBounds;
512 arm_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
513 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
514 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
515 }
516
517 bool IsFatal() const OVERRIDE { return true; }
518
519 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathARMVIXL"; }
520
521 private:
522 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARMVIXL);
523};
524
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100525class LoadClassSlowPathARMVIXL : public SlowPathCodeARMVIXL {
526 public:
527 LoadClassSlowPathARMVIXL(HLoadClass* cls, HInstruction* at, uint32_t dex_pc, bool do_clinit)
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000528 : SlowPathCodeARMVIXL(at), cls_(cls), dex_pc_(dex_pc), do_clinit_(do_clinit) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100529 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
530 }
531
532 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000533 LocationSummary* locations = instruction_->GetLocations();
Vladimir Markoea4c1262017-02-06 19:59:33 +0000534 Location out = locations->Out();
535 constexpr bool call_saves_everything_except_r0 = (!kUseReadBarrier || kUseBakerReadBarrier);
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100536
537 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
538 __ Bind(GetEntryLabel());
539 SaveLiveRegisters(codegen, locations);
540
541 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Vladimir Markoea4c1262017-02-06 19:59:33 +0000542 // For HLoadClass/kBssEntry/kSaveEverything, make sure we preserve the address of the entry.
543 DCHECK_EQ(instruction_->IsLoadClass(), cls_ == instruction_);
544 bool is_load_class_bss_entry =
545 (cls_ == instruction_) && (cls_->GetLoadKind() == HLoadClass::LoadKind::kBssEntry);
546 vixl32::Register entry_address;
547 if (is_load_class_bss_entry && call_saves_everything_except_r0) {
548 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
549 // In the unlucky case that the `temp` is R0, we preserve the address in `out` across
550 // the kSaveEverything call.
551 bool temp_is_r0 = temp.Is(calling_convention.GetRegisterAt(0));
552 entry_address = temp_is_r0 ? RegisterFrom(out) : temp;
553 DCHECK(!entry_address.Is(calling_convention.GetRegisterAt(0)));
554 if (temp_is_r0) {
555 __ Mov(entry_address, temp);
556 }
557 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000558 dex::TypeIndex type_index = cls_->GetTypeIndex();
559 __ Mov(calling_convention.GetRegisterAt(0), type_index.index_);
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100560 QuickEntrypointEnum entrypoint = do_clinit_ ? kQuickInitializeStaticStorage
561 : kQuickInitializeType;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000562 arm_codegen->InvokeRuntime(entrypoint, instruction_, dex_pc_, this);
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100563 if (do_clinit_) {
564 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
565 } else {
566 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
567 }
568
Vladimir Markoea4c1262017-02-06 19:59:33 +0000569 // For HLoadClass/kBssEntry, store the resolved Class to the BSS entry.
570 if (is_load_class_bss_entry) {
571 if (call_saves_everything_except_r0) {
572 // The class entry address was preserved in `entry_address` thanks to kSaveEverything.
573 __ Str(r0, MemOperand(entry_address));
574 } else {
575 // For non-Baker read barrier, we need to re-calculate the address of the string entry.
576 UseScratchRegisterScope temps(
577 down_cast<CodeGeneratorARMVIXL*>(codegen)->GetVIXLAssembler());
578 vixl32::Register temp = temps.Acquire();
579 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
580 arm_codegen->NewTypeBssEntryPatch(cls_->GetDexFile(), type_index);
581 arm_codegen->EmitMovwMovtPlaceholder(labels, temp);
582 __ Str(r0, MemOperand(temp));
583 }
584 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100585 // Move the class to the desired location.
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100586 if (out.IsValid()) {
587 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
588 arm_codegen->Move32(locations->Out(), LocationFrom(r0));
589 }
590 RestoreLiveRegisters(codegen, locations);
591 __ B(GetExitLabel());
592 }
593
594 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathARMVIXL"; }
595
596 private:
597 // The class this slow path will load.
598 HLoadClass* const cls_;
599
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100600 // The dex PC of `at_`.
601 const uint32_t dex_pc_;
602
603 // Whether to initialize the class.
604 const bool do_clinit_;
605
606 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARMVIXL);
607};
608
Artem Serovd4cc5b22016-11-04 11:19:09 +0000609class LoadStringSlowPathARMVIXL : public SlowPathCodeARMVIXL {
610 public:
611 explicit LoadStringSlowPathARMVIXL(HLoadString* instruction)
612 : SlowPathCodeARMVIXL(instruction) {}
613
614 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Vladimir Markoea4c1262017-02-06 19:59:33 +0000615 DCHECK(instruction_->IsLoadString());
616 DCHECK_EQ(instruction_->AsLoadString()->GetLoadKind(), HLoadString::LoadKind::kBssEntry);
Artem Serovd4cc5b22016-11-04 11:19:09 +0000617 LocationSummary* locations = instruction_->GetLocations();
618 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
619 HLoadString* load = instruction_->AsLoadString();
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000620 const dex::StringIndex string_index = load->GetStringIndex();
Artem Serovd4cc5b22016-11-04 11:19:09 +0000621 vixl32::Register out = OutputRegister(load);
Artem Serovd4cc5b22016-11-04 11:19:09 +0000622 constexpr bool call_saves_everything_except_r0 = (!kUseReadBarrier || kUseBakerReadBarrier);
623
624 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
625 __ Bind(GetEntryLabel());
626 SaveLiveRegisters(codegen, locations);
627
628 InvokeRuntimeCallingConventionARMVIXL calling_convention;
629 // In the unlucky case that the `temp` is R0, we preserve the address in `out` across
Vladimir Markoea4c1262017-02-06 19:59:33 +0000630 // the kSaveEverything call.
631 vixl32::Register entry_address;
632 if (call_saves_everything_except_r0) {
633 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
634 bool temp_is_r0 = (temp.Is(calling_convention.GetRegisterAt(0)));
635 entry_address = temp_is_r0 ? out : temp;
636 DCHECK(!entry_address.Is(calling_convention.GetRegisterAt(0)));
637 if (temp_is_r0) {
638 __ Mov(entry_address, temp);
639 }
Artem Serovd4cc5b22016-11-04 11:19:09 +0000640 }
641
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000642 __ Mov(calling_convention.GetRegisterAt(0), string_index.index_);
Artem Serovd4cc5b22016-11-04 11:19:09 +0000643 arm_codegen->InvokeRuntime(kQuickResolveString, instruction_, instruction_->GetDexPc(), this);
644 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
645
646 // Store the resolved String to the .bss entry.
647 if (call_saves_everything_except_r0) {
648 // The string entry address was preserved in `entry_address` thanks to kSaveEverything.
649 __ Str(r0, MemOperand(entry_address));
650 } else {
651 // For non-Baker read barrier, we need to re-calculate the address of the string entry.
Vladimir Markoea4c1262017-02-06 19:59:33 +0000652 UseScratchRegisterScope temps(
653 down_cast<CodeGeneratorARMVIXL*>(codegen)->GetVIXLAssembler());
654 vixl32::Register temp = temps.Acquire();
Artem Serovd4cc5b22016-11-04 11:19:09 +0000655 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +0100656 arm_codegen->NewStringBssEntryPatch(load->GetDexFile(), string_index);
Vladimir Markoea4c1262017-02-06 19:59:33 +0000657 arm_codegen->EmitMovwMovtPlaceholder(labels, temp);
658 __ Str(r0, MemOperand(temp));
Artem Serovd4cc5b22016-11-04 11:19:09 +0000659 }
660
661 arm_codegen->Move32(locations->Out(), LocationFrom(r0));
662 RestoreLiveRegisters(codegen, locations);
663
664 __ B(GetExitLabel());
665 }
666
667 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathARMVIXL"; }
668
669 private:
670 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARMVIXL);
671};
672
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100673class TypeCheckSlowPathARMVIXL : public SlowPathCodeARMVIXL {
674 public:
675 TypeCheckSlowPathARMVIXL(HInstruction* instruction, bool is_fatal)
676 : SlowPathCodeARMVIXL(instruction), is_fatal_(is_fatal) {}
677
678 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
679 LocationSummary* locations = instruction_->GetLocations();
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100680 DCHECK(instruction_->IsCheckCast()
681 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
682
683 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
684 __ Bind(GetEntryLabel());
685
686 if (!is_fatal_) {
Artem Serovcfbe9132016-10-14 15:58:56 +0100687 SaveLiveRegisters(codegen, locations);
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100688 }
689
690 // We're moving two locations to locations that could overlap, so we need a parallel
691 // move resolver.
692 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100693
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800694 codegen->EmitParallelMoves(locations->InAt(0),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800695 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100696 DataType::Type::kReference,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800697 locations->InAt(1),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800698 LocationFrom(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100699 DataType::Type::kReference);
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100700 if (instruction_->IsInstanceOf()) {
Artem Serovcfbe9132016-10-14 15:58:56 +0100701 arm_codegen->InvokeRuntime(kQuickInstanceofNonTrivial,
702 instruction_,
703 instruction_->GetDexPc(),
704 this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800705 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Artem Serovcfbe9132016-10-14 15:58:56 +0100706 arm_codegen->Move32(locations->Out(), LocationFrom(r0));
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100707 } else {
708 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800709 arm_codegen->InvokeRuntime(kQuickCheckInstanceOf,
710 instruction_,
711 instruction_->GetDexPc(),
712 this);
713 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100714 }
715
716 if (!is_fatal_) {
Artem Serovcfbe9132016-10-14 15:58:56 +0100717 RestoreLiveRegisters(codegen, locations);
718 __ B(GetExitLabel());
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100719 }
720 }
721
722 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathARMVIXL"; }
723
724 bool IsFatal() const OVERRIDE { return is_fatal_; }
725
726 private:
727 const bool is_fatal_;
728
729 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARMVIXL);
730};
731
Scott Wakelingc34dba72016-10-03 10:14:44 +0100732class DeoptimizationSlowPathARMVIXL : public SlowPathCodeARMVIXL {
733 public:
734 explicit DeoptimizationSlowPathARMVIXL(HDeoptimize* instruction)
735 : SlowPathCodeARMVIXL(instruction) {}
736
737 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
738 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
739 __ Bind(GetEntryLabel());
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100740 LocationSummary* locations = instruction_->GetLocations();
741 SaveLiveRegisters(codegen, locations);
742 InvokeRuntimeCallingConventionARMVIXL calling_convention;
743 __ Mov(calling_convention.GetRegisterAt(0),
744 static_cast<uint32_t>(instruction_->AsDeoptimize()->GetDeoptimizationKind()));
745
Scott Wakelingc34dba72016-10-03 10:14:44 +0100746 arm_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100747 CheckEntrypointTypes<kQuickDeoptimize, void, DeoptimizationKind>();
Scott Wakelingc34dba72016-10-03 10:14:44 +0100748 }
749
750 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathARMVIXL"; }
751
752 private:
753 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARMVIXL);
754};
755
756class ArraySetSlowPathARMVIXL : public SlowPathCodeARMVIXL {
757 public:
758 explicit ArraySetSlowPathARMVIXL(HInstruction* instruction) : SlowPathCodeARMVIXL(instruction) {}
759
760 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
761 LocationSummary* locations = instruction_->GetLocations();
762 __ Bind(GetEntryLabel());
763 SaveLiveRegisters(codegen, locations);
764
765 InvokeRuntimeCallingConventionARMVIXL calling_convention;
766 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
767 parallel_move.AddMove(
768 locations->InAt(0),
769 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100770 DataType::Type::kReference,
Scott Wakelingc34dba72016-10-03 10:14:44 +0100771 nullptr);
772 parallel_move.AddMove(
773 locations->InAt(1),
774 LocationFrom(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100775 DataType::Type::kInt32,
Scott Wakelingc34dba72016-10-03 10:14:44 +0100776 nullptr);
777 parallel_move.AddMove(
778 locations->InAt(2),
779 LocationFrom(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100780 DataType::Type::kReference,
Scott Wakelingc34dba72016-10-03 10:14:44 +0100781 nullptr);
782 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
783
784 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
785 arm_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
786 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
787 RestoreLiveRegisters(codegen, locations);
788 __ B(GetExitLabel());
789 }
790
791 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathARMVIXL"; }
792
793 private:
794 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathARMVIXL);
795};
796
Roland Levillain54f869e2017-03-06 13:54:11 +0000797// Abstract base class for read barrier slow paths marking a reference
798// `ref`.
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000799//
Roland Levillain54f869e2017-03-06 13:54:11 +0000800// Argument `entrypoint` must be a register location holding the read
Roland Levillain6d729a72017-06-30 18:34:01 +0100801// barrier marking runtime entry point to be invoked or an empty
802// location; in the latter case, the read barrier marking runtime
803// entry point will be loaded by the slow path code itself.
Roland Levillain54f869e2017-03-06 13:54:11 +0000804class ReadBarrierMarkSlowPathBaseARMVIXL : public SlowPathCodeARMVIXL {
805 protected:
806 ReadBarrierMarkSlowPathBaseARMVIXL(HInstruction* instruction, Location ref, Location entrypoint)
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000807 : SlowPathCodeARMVIXL(instruction), ref_(ref), entrypoint_(entrypoint) {
808 DCHECK(kEmitCompilerReadBarrier);
809 }
810
Roland Levillain54f869e2017-03-06 13:54:11 +0000811 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathBaseARMVIXL"; }
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000812
Roland Levillain54f869e2017-03-06 13:54:11 +0000813 // Generate assembly code calling the read barrier marking runtime
814 // entry point (ReadBarrierMarkRegX).
815 void GenerateReadBarrierMarkRuntimeCall(CodeGenerator* codegen) {
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000816 vixl32::Register ref_reg = RegisterFrom(ref_);
Roland Levillain47b3ab22017-02-27 14:31:35 +0000817
Roland Levillain47b3ab22017-02-27 14:31:35 +0000818 // No need to save live registers; it's taken care of by the
819 // entrypoint. Also, there is no need to update the stack mask,
820 // as this runtime call will not trigger a garbage collection.
821 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
822 DCHECK(!ref_reg.Is(sp));
823 DCHECK(!ref_reg.Is(lr));
824 DCHECK(!ref_reg.Is(pc));
825 // IP is used internally by the ReadBarrierMarkRegX entry point
826 // as a temporary, it cannot be the entry point's input/output.
827 DCHECK(!ref_reg.Is(ip));
828 DCHECK(ref_reg.IsRegister()) << ref_reg;
829 // "Compact" slow path, saving two moves.
830 //
831 // Instead of using the standard runtime calling convention (input
832 // and output in R0):
833 //
834 // R0 <- ref
835 // R0 <- ReadBarrierMark(R0)
836 // ref <- R0
837 //
838 // we just use rX (the register containing `ref`) as input and output
839 // of a dedicated entrypoint:
840 //
841 // rX <- ReadBarrierMarkRegX(rX)
842 //
843 if (entrypoint_.IsValid()) {
844 arm_codegen->ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction_, this);
845 __ Blx(RegisterFrom(entrypoint_));
846 } else {
Roland Levillain54f869e2017-03-06 13:54:11 +0000847 // Entrypoint is not already loaded, load from the thread.
Roland Levillain47b3ab22017-02-27 14:31:35 +0000848 int32_t entry_point_offset =
Roland Levillain97c46462017-05-11 14:04:03 +0100849 Thread::ReadBarrierMarkEntryPointsOffset<kArmPointerSize>(ref_reg.GetCode());
Roland Levillain47b3ab22017-02-27 14:31:35 +0000850 // This runtime call does not require a stack map.
851 arm_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
852 }
Roland Levillain47b3ab22017-02-27 14:31:35 +0000853 }
854
Roland Levillain47b3ab22017-02-27 14:31:35 +0000855 // The location (register) of the marked object reference.
856 const Location ref_;
857
858 // The location of the entrypoint if already loaded.
859 const Location entrypoint_;
860
Roland Levillain54f869e2017-03-06 13:54:11 +0000861 private:
862 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathBaseARMVIXL);
Roland Levillain47b3ab22017-02-27 14:31:35 +0000863};
864
Scott Wakelingc34dba72016-10-03 10:14:44 +0100865// Slow path marking an object reference `ref` during a read
866// barrier. The field `obj.field` in the object `obj` holding this
Roland Levillain54f869e2017-03-06 13:54:11 +0000867// reference does not get updated by this slow path after marking.
Roland Levillain47b3ab22017-02-27 14:31:35 +0000868//
Scott Wakelingc34dba72016-10-03 10:14:44 +0100869// This means that after the execution of this slow path, `ref` will
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000870// always be up-to-date, but `obj.field` may not; i.e., after the
871// flip, `ref` will be a to-space reference, but `obj.field` will
872// probably still be a from-space reference (unless it gets updated by
873// another thread, or if another thread installed another object
874// reference (different from `ref`) in `obj.field`).
Roland Levillainba650a42017-03-06 13:52:32 +0000875//
Roland Levillain6d729a72017-06-30 18:34:01 +0100876// Argument `entrypoint` must be a register location holding the read
877// barrier marking runtime entry point to be invoked or an empty
878// location; in the latter case, the read barrier marking runtime
879// entry point will be loaded by the slow path code itself.
Roland Levillain54f869e2017-03-06 13:54:11 +0000880class ReadBarrierMarkSlowPathARMVIXL : public ReadBarrierMarkSlowPathBaseARMVIXL {
Roland Levillain47b3ab22017-02-27 14:31:35 +0000881 public:
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000882 ReadBarrierMarkSlowPathARMVIXL(HInstruction* instruction,
883 Location ref,
884 Location entrypoint = Location::NoLocation())
Roland Levillain54f869e2017-03-06 13:54:11 +0000885 : ReadBarrierMarkSlowPathBaseARMVIXL(instruction, ref, entrypoint) {
Roland Levillain47b3ab22017-02-27 14:31:35 +0000886 DCHECK(kEmitCompilerReadBarrier);
887 }
888
Roland Levillain47b3ab22017-02-27 14:31:35 +0000889 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathARMVIXL"; }
890
891 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
892 LocationSummary* locations = instruction_->GetLocations();
Roland Levillain54f869e2017-03-06 13:54:11 +0000893 DCHECK(locations->CanCall());
894 DCHECK(ref_.IsRegister()) << ref_;
895 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_.reg())) << ref_.reg();
896 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
897 << "Unexpected instruction in read barrier marking slow path: "
898 << instruction_->DebugName();
899
900 __ Bind(GetEntryLabel());
901 GenerateReadBarrierMarkRuntimeCall(codegen);
902 __ B(GetExitLabel());
903 }
904
905 private:
906 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathARMVIXL);
907};
908
909// Slow path loading `obj`'s lock word, loading a reference from
910// object `*(obj + offset + (index << scale_factor))` into `ref`, and
911// marking `ref` if `obj` is gray according to the lock word (Baker
912// read barrier). The field `obj.field` in the object `obj` holding
913// this reference does not get updated by this slow path after marking
914// (see LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARMVIXL
915// below for that).
916//
917// This means that after the execution of this slow path, `ref` will
918// always be up-to-date, but `obj.field` may not; i.e., after the
919// flip, `ref` will be a to-space reference, but `obj.field` will
920// probably still be a from-space reference (unless it gets updated by
921// another thread, or if another thread installed another object
922// reference (different from `ref`) in `obj.field`).
923//
924// Argument `entrypoint` must be a register location holding the read
Roland Levillain6d729a72017-06-30 18:34:01 +0100925// barrier marking runtime entry point to be invoked or an empty
926// location; in the latter case, the read barrier marking runtime
927// entry point will be loaded by the slow path code itself.
Roland Levillain54f869e2017-03-06 13:54:11 +0000928class LoadReferenceWithBakerReadBarrierSlowPathARMVIXL : public ReadBarrierMarkSlowPathBaseARMVIXL {
929 public:
930 LoadReferenceWithBakerReadBarrierSlowPathARMVIXL(HInstruction* instruction,
931 Location ref,
932 vixl32::Register obj,
933 uint32_t offset,
934 Location index,
935 ScaleFactor scale_factor,
936 bool needs_null_check,
937 vixl32::Register temp,
Roland Levillain6d729a72017-06-30 18:34:01 +0100938 Location entrypoint = Location::NoLocation())
Roland Levillain54f869e2017-03-06 13:54:11 +0000939 : ReadBarrierMarkSlowPathBaseARMVIXL(instruction, ref, entrypoint),
940 obj_(obj),
941 offset_(offset),
942 index_(index),
943 scale_factor_(scale_factor),
944 needs_null_check_(needs_null_check),
945 temp_(temp) {
946 DCHECK(kEmitCompilerReadBarrier);
947 DCHECK(kUseBakerReadBarrier);
948 }
949
Roland Levillain47b3ab22017-02-27 14:31:35 +0000950 const char* GetDescription() const OVERRIDE {
Roland Levillain54f869e2017-03-06 13:54:11 +0000951 return "LoadReferenceWithBakerReadBarrierSlowPathARMVIXL";
Roland Levillain47b3ab22017-02-27 14:31:35 +0000952 }
953
954 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
955 LocationSummary* locations = instruction_->GetLocations();
956 vixl32::Register ref_reg = RegisterFrom(ref_);
957 DCHECK(locations->CanCall());
958 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg.GetCode())) << ref_reg;
Roland Levillain47b3ab22017-02-27 14:31:35 +0000959 DCHECK(instruction_->IsInstanceFieldGet() ||
960 instruction_->IsStaticFieldGet() ||
961 instruction_->IsArrayGet() ||
962 instruction_->IsArraySet() ||
Roland Levillain47b3ab22017-02-27 14:31:35 +0000963 instruction_->IsInstanceOf() ||
964 instruction_->IsCheckCast() ||
965 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
966 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
967 << "Unexpected instruction in read barrier marking slow path: "
968 << instruction_->DebugName();
969 // The read barrier instrumentation of object ArrayGet
970 // instructions does not support the HIntermediateAddress
971 // instruction.
972 DCHECK(!(instruction_->IsArrayGet() &&
973 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
974
Roland Levillain54f869e2017-03-06 13:54:11 +0000975 // Temporary register `temp_`, used to store the lock word, must
976 // not be IP, as we may use it to emit the reference load (in the
977 // call to GenerateRawReferenceLoad below), and we need the lock
978 // word to still be in `temp_` after the reference load.
979 DCHECK(!temp_.Is(ip));
980
Roland Levillain47b3ab22017-02-27 14:31:35 +0000981 __ Bind(GetEntryLabel());
Roland Levillain54f869e2017-03-06 13:54:11 +0000982
983 // When using MaybeGenerateReadBarrierSlow, the read barrier call is
984 // inserted after the original load. However, in fast path based
985 // Baker's read barriers, we need to perform the load of
986 // mirror::Object::monitor_ *before* the original reference load.
987 // This load-load ordering is required by the read barrier.
Roland Levillainff487002017-03-07 16:50:01 +0000988 // The slow path (for Baker's algorithm) should look like:
Roland Levillain54f869e2017-03-06 13:54:11 +0000989 //
990 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
991 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
992 // HeapReference<mirror::Object> ref = *src; // Original reference load.
993 // bool is_gray = (rb_state == ReadBarrier::GrayState());
994 // if (is_gray) {
995 // ref = entrypoint(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
996 // }
997 //
998 // Note: the original implementation in ReadBarrier::Barrier is
999 // slightly more complex as it performs additional checks that we do
1000 // not do here for performance reasons.
1001
Roland Levillain47b3ab22017-02-27 14:31:35 +00001002 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
Roland Levillain54f869e2017-03-06 13:54:11 +00001003
1004 // /* int32_t */ monitor = obj->monitor_
1005 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
1006 arm_codegen->GetAssembler()->LoadFromOffset(kLoadWord, temp_, obj_, monitor_offset);
1007 if (needs_null_check_) {
1008 codegen->MaybeRecordImplicitNullCheck(instruction_);
Roland Levillain47b3ab22017-02-27 14:31:35 +00001009 }
Roland Levillain54f869e2017-03-06 13:54:11 +00001010 // /* LockWord */ lock_word = LockWord(monitor)
1011 static_assert(sizeof(LockWord) == sizeof(int32_t),
1012 "art::LockWord and int32_t have different sizes.");
1013
1014 // Introduce a dependency on the lock_word including the rb_state,
1015 // which shall prevent load-load reordering without using
1016 // a memory barrier (which would be more expensive).
1017 // `obj` is unchanged by this operation, but its value now depends
1018 // on `temp`.
1019 __ Add(obj_, obj_, Operand(temp_, ShiftType::LSR, 32));
1020
1021 // The actual reference load.
1022 // A possible implicit null check has already been handled above.
1023 arm_codegen->GenerateRawReferenceLoad(
1024 instruction_, ref_, obj_, offset_, index_, scale_factor_, /* needs_null_check */ false);
1025
1026 // Mark the object `ref` when `obj` is gray.
1027 //
1028 // if (rb_state == ReadBarrier::GrayState())
1029 // ref = ReadBarrier::Mark(ref);
1030 //
1031 // Given the numeric representation, it's enough to check the low bit of the
1032 // rb_state. We do that by shifting the bit out of the lock word with LSRS
1033 // which can be a 16-bit instruction unlike the TST immediate.
1034 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
1035 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
1036 __ Lsrs(temp_, temp_, LockWord::kReadBarrierStateShift + 1);
1037 __ B(cc, GetExitLabel()); // Carry flag is the last bit shifted out by LSRS.
1038 GenerateReadBarrierMarkRuntimeCall(codegen);
1039
Roland Levillain47b3ab22017-02-27 14:31:35 +00001040 __ B(GetExitLabel());
1041 }
1042
1043 private:
Roland Levillain54f869e2017-03-06 13:54:11 +00001044 // The register containing the object holding the marked object reference field.
1045 vixl32::Register obj_;
1046 // The offset, index and scale factor to access the reference in `obj_`.
1047 uint32_t offset_;
1048 Location index_;
1049 ScaleFactor scale_factor_;
1050 // Is a null check required?
1051 bool needs_null_check_;
1052 // A temporary register used to hold the lock word of `obj_`.
1053 vixl32::Register temp_;
Roland Levillain47b3ab22017-02-27 14:31:35 +00001054
Roland Levillain54f869e2017-03-06 13:54:11 +00001055 DISALLOW_COPY_AND_ASSIGN(LoadReferenceWithBakerReadBarrierSlowPathARMVIXL);
Roland Levillain47b3ab22017-02-27 14:31:35 +00001056};
1057
Roland Levillain54f869e2017-03-06 13:54:11 +00001058// Slow path loading `obj`'s lock word, loading a reference from
1059// object `*(obj + offset + (index << scale_factor))` into `ref`, and
1060// marking `ref` if `obj` is gray according to the lock word (Baker
1061// read barrier). If needed, this slow path also atomically updates
1062// the field `obj.field` in the object `obj` holding this reference
1063// after marking (contrary to
1064// LoadReferenceWithBakerReadBarrierSlowPathARMVIXL above, which never
1065// tries to update `obj.field`).
Roland Levillain47b3ab22017-02-27 14:31:35 +00001066//
1067// This means that after the execution of this slow path, both `ref`
1068// and `obj.field` will be up-to-date; i.e., after the flip, both will
1069// hold the same to-space reference (unless another thread installed
1070// another object reference (different from `ref`) in `obj.field`).
Roland Levillainba650a42017-03-06 13:52:32 +00001071//
Roland Levillain54f869e2017-03-06 13:54:11 +00001072// Argument `entrypoint` must be a register location holding the read
Roland Levillain6d729a72017-06-30 18:34:01 +01001073// barrier marking runtime entry point to be invoked or an empty
1074// location; in the latter case, the read barrier marking runtime
1075// entry point will be loaded by the slow path code itself.
Roland Levillain54f869e2017-03-06 13:54:11 +00001076class LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARMVIXL
1077 : public ReadBarrierMarkSlowPathBaseARMVIXL {
Roland Levillain47b3ab22017-02-27 14:31:35 +00001078 public:
Roland Levillain6d729a72017-06-30 18:34:01 +01001079 LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARMVIXL(
1080 HInstruction* instruction,
1081 Location ref,
1082 vixl32::Register obj,
1083 uint32_t offset,
1084 Location index,
1085 ScaleFactor scale_factor,
1086 bool needs_null_check,
1087 vixl32::Register temp1,
1088 vixl32::Register temp2,
1089 Location entrypoint = Location::NoLocation())
Roland Levillain54f869e2017-03-06 13:54:11 +00001090 : ReadBarrierMarkSlowPathBaseARMVIXL(instruction, ref, entrypoint),
Roland Levillain47b3ab22017-02-27 14:31:35 +00001091 obj_(obj),
Roland Levillain54f869e2017-03-06 13:54:11 +00001092 offset_(offset),
1093 index_(index),
1094 scale_factor_(scale_factor),
1095 needs_null_check_(needs_null_check),
Roland Levillain47b3ab22017-02-27 14:31:35 +00001096 temp1_(temp1),
Roland Levillain54f869e2017-03-06 13:54:11 +00001097 temp2_(temp2) {
Roland Levillain47b3ab22017-02-27 14:31:35 +00001098 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain54f869e2017-03-06 13:54:11 +00001099 DCHECK(kUseBakerReadBarrier);
Roland Levillain47b3ab22017-02-27 14:31:35 +00001100 }
1101
1102 const char* GetDescription() const OVERRIDE {
Roland Levillain54f869e2017-03-06 13:54:11 +00001103 return "LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARMVIXL";
Roland Levillain47b3ab22017-02-27 14:31:35 +00001104 }
1105
1106 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
1107 LocationSummary* locations = instruction_->GetLocations();
1108 vixl32::Register ref_reg = RegisterFrom(ref_);
1109 DCHECK(locations->CanCall());
1110 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg.GetCode())) << ref_reg;
Roland Levillain54f869e2017-03-06 13:54:11 +00001111 DCHECK_NE(ref_.reg(), LocationFrom(temp1_).reg());
1112
1113 // This slow path is only used by the UnsafeCASObject intrinsic at the moment.
Roland Levillain47b3ab22017-02-27 14:31:35 +00001114 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
1115 << "Unexpected instruction in read barrier marking and field updating slow path: "
1116 << instruction_->DebugName();
1117 DCHECK(instruction_->GetLocations()->Intrinsified());
1118 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
Roland Levillain54f869e2017-03-06 13:54:11 +00001119 DCHECK_EQ(offset_, 0u);
1120 DCHECK_EQ(scale_factor_, ScaleFactor::TIMES_1);
1121 Location field_offset = index_;
1122 DCHECK(field_offset.IsRegisterPair()) << field_offset;
1123
1124 // Temporary register `temp1_`, used to store the lock word, must
1125 // not be IP, as we may use it to emit the reference load (in the
1126 // call to GenerateRawReferenceLoad below), and we need the lock
1127 // word to still be in `temp1_` after the reference load.
1128 DCHECK(!temp1_.Is(ip));
Roland Levillain47b3ab22017-02-27 14:31:35 +00001129
1130 __ Bind(GetEntryLabel());
1131
Roland Levillainff487002017-03-07 16:50:01 +00001132 // The implementation is similar to LoadReferenceWithBakerReadBarrierSlowPathARMVIXL's:
1133 //
1134 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
1135 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
1136 // HeapReference<mirror::Object> ref = *src; // Original reference load.
1137 // bool is_gray = (rb_state == ReadBarrier::GrayState());
1138 // if (is_gray) {
1139 // old_ref = ref;
1140 // ref = entrypoint(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
1141 // compareAndSwapObject(obj, field_offset, old_ref, ref);
1142 // }
1143
Roland Levillain54f869e2017-03-06 13:54:11 +00001144 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
1145
1146 // /* int32_t */ monitor = obj->monitor_
1147 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
1148 arm_codegen->GetAssembler()->LoadFromOffset(kLoadWord, temp1_, obj_, monitor_offset);
1149 if (needs_null_check_) {
1150 codegen->MaybeRecordImplicitNullCheck(instruction_);
1151 }
1152 // /* LockWord */ lock_word = LockWord(monitor)
1153 static_assert(sizeof(LockWord) == sizeof(int32_t),
1154 "art::LockWord and int32_t have different sizes.");
1155
1156 // Introduce a dependency on the lock_word including the rb_state,
1157 // which shall prevent load-load reordering without using
1158 // a memory barrier (which would be more expensive).
1159 // `obj` is unchanged by this operation, but its value now depends
1160 // on `temp`.
1161 __ Add(obj_, obj_, Operand(temp1_, ShiftType::LSR, 32));
1162
1163 // The actual reference load.
1164 // A possible implicit null check has already been handled above.
1165 arm_codegen->GenerateRawReferenceLoad(
1166 instruction_, ref_, obj_, offset_, index_, scale_factor_, /* needs_null_check */ false);
1167
1168 // Mark the object `ref` when `obj` is gray.
1169 //
1170 // if (rb_state == ReadBarrier::GrayState())
1171 // ref = ReadBarrier::Mark(ref);
1172 //
1173 // Given the numeric representation, it's enough to check the low bit of the
1174 // rb_state. We do that by shifting the bit out of the lock word with LSRS
1175 // which can be a 16-bit instruction unlike the TST immediate.
1176 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
1177 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
1178 __ Lsrs(temp1_, temp1_, LockWord::kReadBarrierStateShift + 1);
1179 __ B(cc, GetExitLabel()); // Carry flag is the last bit shifted out by LSRS.
1180
1181 // Save the old value of the reference before marking it.
Roland Levillain47b3ab22017-02-27 14:31:35 +00001182 // Note that we cannot use IP to save the old reference, as IP is
1183 // used internally by the ReadBarrierMarkRegX entry point, and we
1184 // need the old reference after the call to that entry point.
1185 DCHECK(!temp1_.Is(ip));
1186 __ Mov(temp1_, ref_reg);
Roland Levillain27b1f9c2017-01-17 16:56:34 +00001187
Roland Levillain54f869e2017-03-06 13:54:11 +00001188 GenerateReadBarrierMarkRuntimeCall(codegen);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001189
1190 // If the new reference is different from the old reference,
Roland Levillain54f869e2017-03-06 13:54:11 +00001191 // update the field in the holder (`*(obj_ + field_offset)`).
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001192 //
1193 // Note that this field could also hold a different object, if
1194 // another thread had concurrently changed it. In that case, the
1195 // LDREX/SUBS/ITNE sequence of instructions in the compare-and-set
1196 // (CAS) operation below would abort the CAS, leaving the field
1197 // as-is.
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001198 __ Cmp(temp1_, ref_reg);
Roland Levillain54f869e2017-03-06 13:54:11 +00001199 __ B(eq, GetExitLabel());
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001200
1201 // Update the the holder's field atomically. This may fail if
1202 // mutator updates before us, but it's OK. This is achieved
1203 // using a strong compare-and-set (CAS) operation with relaxed
1204 // memory synchronization ordering, where the expected value is
1205 // the old reference and the desired value is the new reference.
1206
1207 UseScratchRegisterScope temps(arm_codegen->GetVIXLAssembler());
1208 // Convenience aliases.
1209 vixl32::Register base = obj_;
1210 // The UnsafeCASObject intrinsic uses a register pair as field
1211 // offset ("long offset"), of which only the low part contains
1212 // data.
Roland Levillain54f869e2017-03-06 13:54:11 +00001213 vixl32::Register offset = LowRegisterFrom(field_offset);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001214 vixl32::Register expected = temp1_;
1215 vixl32::Register value = ref_reg;
1216 vixl32::Register tmp_ptr = temps.Acquire(); // Pointer to actual memory.
1217 vixl32::Register tmp = temp2_; // Value in memory.
1218
1219 __ Add(tmp_ptr, base, offset);
1220
1221 if (kPoisonHeapReferences) {
1222 arm_codegen->GetAssembler()->PoisonHeapReference(expected);
1223 if (value.Is(expected)) {
1224 // Do not poison `value`, as it is the same register as
1225 // `expected`, which has just been poisoned.
1226 } else {
1227 arm_codegen->GetAssembler()->PoisonHeapReference(value);
1228 }
1229 }
1230
1231 // do {
1232 // tmp = [r_ptr] - expected;
1233 // } while (tmp == 0 && failure([r_ptr] <- r_new_value));
1234
1235 vixl32::Label loop_head, exit_loop;
1236 __ Bind(&loop_head);
1237
1238 __ Ldrex(tmp, MemOperand(tmp_ptr));
1239
1240 __ Subs(tmp, tmp, expected);
1241
1242 {
Artem Serov0fb37192016-12-06 18:13:40 +00001243 ExactAssemblyScope aas(arm_codegen->GetVIXLAssembler(),
1244 2 * kMaxInstructionSizeInBytes,
1245 CodeBufferCheckScope::kMaximumSize);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001246
1247 __ it(ne);
1248 __ clrex(ne);
1249 }
1250
Artem Serov517d9f62016-12-12 15:51:15 +00001251 __ B(ne, &exit_loop, /* far_target */ false);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001252
1253 __ Strex(tmp, value, MemOperand(tmp_ptr));
1254 __ Cmp(tmp, 1);
Artem Serov517d9f62016-12-12 15:51:15 +00001255 __ B(eq, &loop_head, /* far_target */ false);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001256
1257 __ Bind(&exit_loop);
1258
1259 if (kPoisonHeapReferences) {
1260 arm_codegen->GetAssembler()->UnpoisonHeapReference(expected);
1261 if (value.Is(expected)) {
1262 // Do not unpoison `value`, as it is the same register as
1263 // `expected`, which has just been unpoisoned.
1264 } else {
1265 arm_codegen->GetAssembler()->UnpoisonHeapReference(value);
1266 }
1267 }
1268
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001269 __ B(GetExitLabel());
1270 }
1271
1272 private:
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001273 // The register containing the object holding the marked object reference field.
1274 const vixl32::Register obj_;
Roland Levillain54f869e2017-03-06 13:54:11 +00001275 // The offset, index and scale factor to access the reference in `obj_`.
1276 uint32_t offset_;
1277 Location index_;
1278 ScaleFactor scale_factor_;
1279 // Is a null check required?
1280 bool needs_null_check_;
1281 // A temporary register used to hold the lock word of `obj_`; and
1282 // also to hold the original reference value, when the reference is
1283 // marked.
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001284 const vixl32::Register temp1_;
Roland Levillain54f869e2017-03-06 13:54:11 +00001285 // A temporary register used in the implementation of the CAS, to
1286 // update the object's reference field.
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001287 const vixl32::Register temp2_;
1288
Roland Levillain54f869e2017-03-06 13:54:11 +00001289 DISALLOW_COPY_AND_ASSIGN(LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARMVIXL);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001290};
1291
1292// Slow path generating a read barrier for a heap reference.
1293class ReadBarrierForHeapReferenceSlowPathARMVIXL : public SlowPathCodeARMVIXL {
1294 public:
1295 ReadBarrierForHeapReferenceSlowPathARMVIXL(HInstruction* instruction,
1296 Location out,
1297 Location ref,
1298 Location obj,
1299 uint32_t offset,
1300 Location index)
1301 : SlowPathCodeARMVIXL(instruction),
1302 out_(out),
1303 ref_(ref),
1304 obj_(obj),
1305 offset_(offset),
1306 index_(index) {
1307 DCHECK(kEmitCompilerReadBarrier);
1308 // If `obj` is equal to `out` or `ref`, it means the initial object
1309 // has been overwritten by (or after) the heap object reference load
1310 // to be instrumented, e.g.:
1311 //
1312 // __ LoadFromOffset(kLoadWord, out, out, offset);
1313 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
1314 //
1315 // In that case, we have lost the information about the original
1316 // object, and the emitted read barrier cannot work properly.
1317 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
1318 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
1319 }
1320
1321 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
1322 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
1323 LocationSummary* locations = instruction_->GetLocations();
1324 vixl32::Register reg_out = RegisterFrom(out_);
1325 DCHECK(locations->CanCall());
1326 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out.GetCode()));
1327 DCHECK(instruction_->IsInstanceFieldGet() ||
1328 instruction_->IsStaticFieldGet() ||
1329 instruction_->IsArrayGet() ||
1330 instruction_->IsInstanceOf() ||
1331 instruction_->IsCheckCast() ||
Andreas Gamped9911ee2017-03-27 13:27:24 -07001332 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001333 << "Unexpected instruction in read barrier for heap reference slow path: "
1334 << instruction_->DebugName();
1335 // The read barrier instrumentation of object ArrayGet
1336 // instructions does not support the HIntermediateAddress
1337 // instruction.
1338 DCHECK(!(instruction_->IsArrayGet() &&
1339 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
1340
1341 __ Bind(GetEntryLabel());
1342 SaveLiveRegisters(codegen, locations);
1343
1344 // We may have to change the index's value, but as `index_` is a
1345 // constant member (like other "inputs" of this slow path),
1346 // introduce a copy of it, `index`.
1347 Location index = index_;
1348 if (index_.IsValid()) {
1349 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
1350 if (instruction_->IsArrayGet()) {
1351 // Compute the actual memory offset and store it in `index`.
1352 vixl32::Register index_reg = RegisterFrom(index_);
1353 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg.GetCode()));
1354 if (codegen->IsCoreCalleeSaveRegister(index_reg.GetCode())) {
1355 // We are about to change the value of `index_reg` (see the
Roland Levillain9983e302017-07-14 14:34:22 +01001356 // calls to art::arm::ArmVIXLMacroAssembler::Lsl and
1357 // art::arm::ArmVIXLMacroAssembler::Add below), but it has
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001358 // not been saved by the previous call to
1359 // art::SlowPathCode::SaveLiveRegisters, as it is a
1360 // callee-save register --
1361 // art::SlowPathCode::SaveLiveRegisters does not consider
1362 // callee-save registers, as it has been designed with the
1363 // assumption that callee-save registers are supposed to be
1364 // handled by the called function. So, as a callee-save
1365 // register, `index_reg` _would_ eventually be saved onto
1366 // the stack, but it would be too late: we would have
1367 // changed its value earlier. Therefore, we manually save
1368 // it here into another freely available register,
1369 // `free_reg`, chosen of course among the caller-save
1370 // registers (as a callee-save `free_reg` register would
1371 // exhibit the same problem).
1372 //
1373 // Note we could have requested a temporary register from
1374 // the register allocator instead; but we prefer not to, as
1375 // this is a slow path, and we know we can find a
1376 // caller-save register that is available.
1377 vixl32::Register free_reg = FindAvailableCallerSaveRegister(codegen);
1378 __ Mov(free_reg, index_reg);
1379 index_reg = free_reg;
1380 index = LocationFrom(index_reg);
1381 } else {
1382 // The initial register stored in `index_` has already been
1383 // saved in the call to art::SlowPathCode::SaveLiveRegisters
1384 // (as it is not a callee-save register), so we can freely
1385 // use it.
1386 }
1387 // Shifting the index value contained in `index_reg` by the scale
1388 // factor (2) cannot overflow in practice, as the runtime is
1389 // unable to allocate object arrays with a size larger than
1390 // 2^26 - 1 (that is, 2^28 - 4 bytes).
1391 __ Lsl(index_reg, index_reg, TIMES_4);
1392 static_assert(
1393 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
1394 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
1395 __ Add(index_reg, index_reg, offset_);
1396 } else {
1397 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
1398 // intrinsics, `index_` is not shifted by a scale factor of 2
1399 // (as in the case of ArrayGet), as it is actually an offset
1400 // to an object field within an object.
1401 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
1402 DCHECK(instruction_->GetLocations()->Intrinsified());
1403 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
1404 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
1405 << instruction_->AsInvoke()->GetIntrinsic();
1406 DCHECK_EQ(offset_, 0U);
1407 DCHECK(index_.IsRegisterPair());
1408 // UnsafeGet's offset location is a register pair, the low
1409 // part contains the correct offset.
1410 index = index_.ToLow();
1411 }
1412 }
1413
1414 // We're moving two or three locations to locations that could
1415 // overlap, so we need a parallel move resolver.
1416 InvokeRuntimeCallingConventionARMVIXL calling_convention;
1417 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
1418 parallel_move.AddMove(ref_,
1419 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001420 DataType::Type::kReference,
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001421 nullptr);
1422 parallel_move.AddMove(obj_,
1423 LocationFrom(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001424 DataType::Type::kReference,
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001425 nullptr);
1426 if (index.IsValid()) {
1427 parallel_move.AddMove(index,
1428 LocationFrom(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001429 DataType::Type::kInt32,
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001430 nullptr);
1431 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
1432 } else {
1433 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
1434 __ Mov(calling_convention.GetRegisterAt(2), offset_);
1435 }
1436 arm_codegen->InvokeRuntime(kQuickReadBarrierSlow, instruction_, instruction_->GetDexPc(), this);
1437 CheckEntrypointTypes<
1438 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
1439 arm_codegen->Move32(out_, LocationFrom(r0));
1440
1441 RestoreLiveRegisters(codegen, locations);
1442 __ B(GetExitLabel());
1443 }
1444
1445 const char* GetDescription() const OVERRIDE {
1446 return "ReadBarrierForHeapReferenceSlowPathARMVIXL";
1447 }
1448
1449 private:
1450 vixl32::Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
1451 uint32_t ref = RegisterFrom(ref_).GetCode();
1452 uint32_t obj = RegisterFrom(obj_).GetCode();
1453 for (uint32_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
1454 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
1455 return vixl32::Register(i);
1456 }
1457 }
1458 // We shall never fail to find a free caller-save register, as
1459 // there are more than two core caller-save registers on ARM
1460 // (meaning it is possible to find one which is different from
1461 // `ref` and `obj`).
1462 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
1463 LOG(FATAL) << "Could not find a free caller-save register";
1464 UNREACHABLE();
1465 }
1466
1467 const Location out_;
1468 const Location ref_;
1469 const Location obj_;
1470 const uint32_t offset_;
1471 // An additional location containing an index to an array.
1472 // Only used for HArrayGet and the UnsafeGetObject &
1473 // UnsafeGetObjectVolatile intrinsics.
1474 const Location index_;
1475
1476 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathARMVIXL);
1477};
1478
1479// Slow path generating a read barrier for a GC root.
1480class ReadBarrierForRootSlowPathARMVIXL : public SlowPathCodeARMVIXL {
1481 public:
1482 ReadBarrierForRootSlowPathARMVIXL(HInstruction* instruction, Location out, Location root)
1483 : SlowPathCodeARMVIXL(instruction), out_(out), root_(root) {
1484 DCHECK(kEmitCompilerReadBarrier);
1485 }
1486
1487 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
1488 LocationSummary* locations = instruction_->GetLocations();
1489 vixl32::Register reg_out = RegisterFrom(out_);
1490 DCHECK(locations->CanCall());
1491 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out.GetCode()));
1492 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
1493 << "Unexpected instruction in read barrier for GC root slow path: "
1494 << instruction_->DebugName();
1495
1496 __ Bind(GetEntryLabel());
1497 SaveLiveRegisters(codegen, locations);
1498
1499 InvokeRuntimeCallingConventionARMVIXL calling_convention;
1500 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
1501 arm_codegen->Move32(LocationFrom(calling_convention.GetRegisterAt(0)), root_);
1502 arm_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
1503 instruction_,
1504 instruction_->GetDexPc(),
1505 this);
1506 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
1507 arm_codegen->Move32(out_, LocationFrom(r0));
1508
1509 RestoreLiveRegisters(codegen, locations);
1510 __ B(GetExitLabel());
1511 }
1512
1513 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathARMVIXL"; }
1514
1515 private:
1516 const Location out_;
1517 const Location root_;
1518
1519 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathARMVIXL);
1520};
Scott Wakelingc34dba72016-10-03 10:14:44 +01001521
Scott Wakelingfe885462016-09-22 10:24:38 +01001522inline vixl32::Condition ARMCondition(IfCondition cond) {
1523 switch (cond) {
1524 case kCondEQ: return eq;
1525 case kCondNE: return ne;
1526 case kCondLT: return lt;
1527 case kCondLE: return le;
1528 case kCondGT: return gt;
1529 case kCondGE: return ge;
1530 case kCondB: return lo;
1531 case kCondBE: return ls;
1532 case kCondA: return hi;
1533 case kCondAE: return hs;
1534 }
1535 LOG(FATAL) << "Unreachable";
1536 UNREACHABLE();
1537}
1538
1539// Maps signed condition to unsigned condition.
1540inline vixl32::Condition ARMUnsignedCondition(IfCondition cond) {
1541 switch (cond) {
1542 case kCondEQ: return eq;
1543 case kCondNE: return ne;
1544 // Signed to unsigned.
1545 case kCondLT: return lo;
1546 case kCondLE: return ls;
1547 case kCondGT: return hi;
1548 case kCondGE: return hs;
1549 // Unsigned remain unchanged.
1550 case kCondB: return lo;
1551 case kCondBE: return ls;
1552 case kCondA: return hi;
1553 case kCondAE: return hs;
1554 }
1555 LOG(FATAL) << "Unreachable";
1556 UNREACHABLE();
1557}
1558
1559inline vixl32::Condition ARMFPCondition(IfCondition cond, bool gt_bias) {
1560 // The ARM condition codes can express all the necessary branches, see the
1561 // "Meaning (floating-point)" column in the table A8-1 of the ARMv7 reference manual.
1562 // There is no dex instruction or HIR that would need the missing conditions
1563 // "equal or unordered" or "not equal".
1564 switch (cond) {
1565 case kCondEQ: return eq;
1566 case kCondNE: return ne /* unordered */;
1567 case kCondLT: return gt_bias ? cc : lt /* unordered */;
1568 case kCondLE: return gt_bias ? ls : le /* unordered */;
1569 case kCondGT: return gt_bias ? hi /* unordered */ : gt;
1570 case kCondGE: return gt_bias ? cs /* unordered */ : ge;
1571 default:
1572 LOG(FATAL) << "UNREACHABLE";
1573 UNREACHABLE();
1574 }
1575}
1576
Anton Kirilov74234da2017-01-13 14:42:47 +00001577inline ShiftType ShiftFromOpKind(HDataProcWithShifterOp::OpKind op_kind) {
1578 switch (op_kind) {
1579 case HDataProcWithShifterOp::kASR: return ShiftType::ASR;
1580 case HDataProcWithShifterOp::kLSL: return ShiftType::LSL;
1581 case HDataProcWithShifterOp::kLSR: return ShiftType::LSR;
1582 default:
1583 LOG(FATAL) << "Unexpected op kind " << op_kind;
1584 UNREACHABLE();
1585 }
1586}
1587
Scott Wakelingfe885462016-09-22 10:24:38 +01001588void CodeGeneratorARMVIXL::DumpCoreRegister(std::ostream& stream, int reg) const {
1589 stream << vixl32::Register(reg);
1590}
1591
1592void CodeGeneratorARMVIXL::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
1593 stream << vixl32::SRegister(reg);
1594}
1595
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001596static uint32_t ComputeSRegisterListMask(const SRegisterList& regs) {
Scott Wakelingfe885462016-09-22 10:24:38 +01001597 uint32_t mask = 0;
1598 for (uint32_t i = regs.GetFirstSRegister().GetCode();
1599 i <= regs.GetLastSRegister().GetCode();
1600 ++i) {
1601 mask |= (1 << i);
1602 }
1603 return mask;
1604}
1605
Artem Serovd4cc5b22016-11-04 11:19:09 +00001606// Saves the register in the stack. Returns the size taken on stack.
1607size_t CodeGeneratorARMVIXL::SaveCoreRegister(size_t stack_index ATTRIBUTE_UNUSED,
1608 uint32_t reg_id ATTRIBUTE_UNUSED) {
1609 TODO_VIXL32(FATAL);
1610 return 0;
1611}
1612
1613// Restores the register from the stack. Returns the size taken on stack.
1614size_t CodeGeneratorARMVIXL::RestoreCoreRegister(size_t stack_index ATTRIBUTE_UNUSED,
1615 uint32_t reg_id ATTRIBUTE_UNUSED) {
1616 TODO_VIXL32(FATAL);
1617 return 0;
1618}
1619
1620size_t CodeGeneratorARMVIXL::SaveFloatingPointRegister(size_t stack_index ATTRIBUTE_UNUSED,
1621 uint32_t reg_id ATTRIBUTE_UNUSED) {
1622 TODO_VIXL32(FATAL);
1623 return 0;
1624}
1625
1626size_t CodeGeneratorARMVIXL::RestoreFloatingPointRegister(size_t stack_index ATTRIBUTE_UNUSED,
1627 uint32_t reg_id ATTRIBUTE_UNUSED) {
1628 TODO_VIXL32(FATAL);
1629 return 0;
Anton Kirilove28d9ae2016-10-25 18:17:23 +01001630}
1631
Anton Kirilov74234da2017-01-13 14:42:47 +00001632static void GenerateDataProcInstruction(HInstruction::InstructionKind kind,
1633 vixl32::Register out,
1634 vixl32::Register first,
1635 const Operand& second,
1636 CodeGeneratorARMVIXL* codegen) {
1637 if (second.IsImmediate() && second.GetImmediate() == 0) {
1638 const Operand in = kind == HInstruction::kAnd
1639 ? Operand(0)
1640 : Operand(first);
1641
1642 __ Mov(out, in);
1643 } else {
1644 switch (kind) {
1645 case HInstruction::kAdd:
1646 __ Add(out, first, second);
1647 break;
1648 case HInstruction::kAnd:
1649 __ And(out, first, second);
1650 break;
1651 case HInstruction::kOr:
1652 __ Orr(out, first, second);
1653 break;
1654 case HInstruction::kSub:
1655 __ Sub(out, first, second);
1656 break;
1657 case HInstruction::kXor:
1658 __ Eor(out, first, second);
1659 break;
1660 default:
1661 LOG(FATAL) << "Unexpected instruction kind: " << kind;
1662 UNREACHABLE();
1663 }
1664 }
1665}
1666
1667static void GenerateDataProc(HInstruction::InstructionKind kind,
1668 const Location& out,
1669 const Location& first,
1670 const Operand& second_lo,
1671 const Operand& second_hi,
1672 CodeGeneratorARMVIXL* codegen) {
1673 const vixl32::Register first_hi = HighRegisterFrom(first);
1674 const vixl32::Register first_lo = LowRegisterFrom(first);
1675 const vixl32::Register out_hi = HighRegisterFrom(out);
1676 const vixl32::Register out_lo = LowRegisterFrom(out);
1677
1678 if (kind == HInstruction::kAdd) {
1679 __ Adds(out_lo, first_lo, second_lo);
1680 __ Adc(out_hi, first_hi, second_hi);
1681 } else if (kind == HInstruction::kSub) {
1682 __ Subs(out_lo, first_lo, second_lo);
1683 __ Sbc(out_hi, first_hi, second_hi);
1684 } else {
1685 GenerateDataProcInstruction(kind, out_lo, first_lo, second_lo, codegen);
1686 GenerateDataProcInstruction(kind, out_hi, first_hi, second_hi, codegen);
1687 }
1688}
1689
1690static Operand GetShifterOperand(vixl32::Register rm, ShiftType shift, uint32_t shift_imm) {
1691 return shift_imm == 0 ? Operand(rm) : Operand(rm, shift, shift_imm);
1692}
1693
1694static void GenerateLongDataProc(HDataProcWithShifterOp* instruction,
1695 CodeGeneratorARMVIXL* codegen) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001696 DCHECK_EQ(instruction->GetType(), DataType::Type::kInt64);
Anton Kirilov74234da2017-01-13 14:42:47 +00001697 DCHECK(HDataProcWithShifterOp::IsShiftOp(instruction->GetOpKind()));
1698
1699 const LocationSummary* const locations = instruction->GetLocations();
1700 const uint32_t shift_value = instruction->GetShiftAmount();
1701 const HInstruction::InstructionKind kind = instruction->GetInstrKind();
1702 const Location first = locations->InAt(0);
1703 const Location second = locations->InAt(1);
1704 const Location out = locations->Out();
1705 const vixl32::Register first_hi = HighRegisterFrom(first);
1706 const vixl32::Register first_lo = LowRegisterFrom(first);
1707 const vixl32::Register out_hi = HighRegisterFrom(out);
1708 const vixl32::Register out_lo = LowRegisterFrom(out);
1709 const vixl32::Register second_hi = HighRegisterFrom(second);
1710 const vixl32::Register second_lo = LowRegisterFrom(second);
1711 const ShiftType shift = ShiftFromOpKind(instruction->GetOpKind());
1712
1713 if (shift_value >= 32) {
1714 if (shift == ShiftType::LSL) {
1715 GenerateDataProcInstruction(kind,
1716 out_hi,
1717 first_hi,
1718 Operand(second_lo, ShiftType::LSL, shift_value - 32),
1719 codegen);
1720 GenerateDataProcInstruction(kind, out_lo, first_lo, 0, codegen);
1721 } else if (shift == ShiftType::ASR) {
1722 GenerateDataProc(kind,
1723 out,
1724 first,
1725 GetShifterOperand(second_hi, ShiftType::ASR, shift_value - 32),
1726 Operand(second_hi, ShiftType::ASR, 31),
1727 codegen);
1728 } else {
1729 DCHECK_EQ(shift, ShiftType::LSR);
1730 GenerateDataProc(kind,
1731 out,
1732 first,
1733 GetShifterOperand(second_hi, ShiftType::LSR, shift_value - 32),
1734 0,
1735 codegen);
1736 }
1737 } else {
1738 DCHECK_GT(shift_value, 1U);
1739 DCHECK_LT(shift_value, 32U);
1740
1741 UseScratchRegisterScope temps(codegen->GetVIXLAssembler());
1742
1743 if (shift == ShiftType::LSL) {
1744 // We are not doing this for HInstruction::kAdd because the output will require
1745 // Location::kOutputOverlap; not applicable to other cases.
1746 if (kind == HInstruction::kOr || kind == HInstruction::kXor) {
1747 GenerateDataProcInstruction(kind,
1748 out_hi,
1749 first_hi,
1750 Operand(second_hi, ShiftType::LSL, shift_value),
1751 codegen);
1752 GenerateDataProcInstruction(kind,
1753 out_hi,
1754 out_hi,
1755 Operand(second_lo, ShiftType::LSR, 32 - shift_value),
1756 codegen);
1757 GenerateDataProcInstruction(kind,
1758 out_lo,
1759 first_lo,
1760 Operand(second_lo, ShiftType::LSL, shift_value),
1761 codegen);
1762 } else {
1763 const vixl32::Register temp = temps.Acquire();
1764
1765 __ Lsl(temp, second_hi, shift_value);
1766 __ Orr(temp, temp, Operand(second_lo, ShiftType::LSR, 32 - shift_value));
1767 GenerateDataProc(kind,
1768 out,
1769 first,
1770 Operand(second_lo, ShiftType::LSL, shift_value),
1771 temp,
1772 codegen);
1773 }
1774 } else {
1775 DCHECK(shift == ShiftType::ASR || shift == ShiftType::LSR);
1776
1777 // We are not doing this for HInstruction::kAdd because the output will require
1778 // Location::kOutputOverlap; not applicable to other cases.
1779 if (kind == HInstruction::kOr || kind == HInstruction::kXor) {
1780 GenerateDataProcInstruction(kind,
1781 out_lo,
1782 first_lo,
1783 Operand(second_lo, ShiftType::LSR, shift_value),
1784 codegen);
1785 GenerateDataProcInstruction(kind,
1786 out_lo,
1787 out_lo,
1788 Operand(second_hi, ShiftType::LSL, 32 - shift_value),
1789 codegen);
1790 GenerateDataProcInstruction(kind,
1791 out_hi,
1792 first_hi,
1793 Operand(second_hi, shift, shift_value),
1794 codegen);
1795 } else {
1796 const vixl32::Register temp = temps.Acquire();
1797
1798 __ Lsr(temp, second_lo, shift_value);
1799 __ Orr(temp, temp, Operand(second_hi, ShiftType::LSL, 32 - shift_value));
1800 GenerateDataProc(kind,
1801 out,
1802 first,
1803 temp,
1804 Operand(second_hi, shift, shift_value),
1805 codegen);
1806 }
1807 }
1808 }
1809}
1810
Donghui Bai426b49c2016-11-08 14:55:38 +08001811static void GenerateVcmp(HInstruction* instruction, CodeGeneratorARMVIXL* codegen) {
1812 const Location rhs_loc = instruction->GetLocations()->InAt(1);
1813 if (rhs_loc.IsConstant()) {
1814 // 0.0 is the only immediate that can be encoded directly in
1815 // a VCMP instruction.
1816 //
1817 // Both the JLS (section 15.20.1) and the JVMS (section 6.5)
1818 // specify that in a floating-point comparison, positive zero
1819 // and negative zero are considered equal, so we can use the
1820 // literal 0.0 for both cases here.
1821 //
1822 // Note however that some methods (Float.equal, Float.compare,
1823 // Float.compareTo, Double.equal, Double.compare,
1824 // Double.compareTo, Math.max, Math.min, StrictMath.max,
1825 // StrictMath.min) consider 0.0 to be (strictly) greater than
1826 // -0.0. So if we ever translate calls to these methods into a
1827 // HCompare instruction, we must handle the -0.0 case with
1828 // care here.
1829 DCHECK(rhs_loc.GetConstant()->IsArithmeticZero());
1830
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001831 const DataType::Type type = instruction->InputAt(0)->GetType();
Donghui Bai426b49c2016-11-08 14:55:38 +08001832
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001833 if (type == DataType::Type::kFloat32) {
Donghui Bai426b49c2016-11-08 14:55:38 +08001834 __ Vcmp(F32, InputSRegisterAt(instruction, 0), 0.0);
1835 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001836 DCHECK_EQ(type, DataType::Type::kFloat64);
Donghui Bai426b49c2016-11-08 14:55:38 +08001837 __ Vcmp(F64, InputDRegisterAt(instruction, 0), 0.0);
1838 }
1839 } else {
1840 __ Vcmp(InputVRegisterAt(instruction, 0), InputVRegisterAt(instruction, 1));
1841 }
1842}
1843
Anton Kirilov5601d4e2017-05-11 19:33:50 +01001844static int64_t AdjustConstantForCondition(int64_t value,
1845 IfCondition* condition,
1846 IfCondition* opposite) {
1847 if (value == 1) {
1848 if (*condition == kCondB) {
1849 value = 0;
1850 *condition = kCondEQ;
1851 *opposite = kCondNE;
1852 } else if (*condition == kCondAE) {
1853 value = 0;
1854 *condition = kCondNE;
1855 *opposite = kCondEQ;
1856 }
1857 } else if (value == -1) {
1858 if (*condition == kCondGT) {
1859 value = 0;
1860 *condition = kCondGE;
1861 *opposite = kCondLT;
1862 } else if (*condition == kCondLE) {
1863 value = 0;
1864 *condition = kCondLT;
1865 *opposite = kCondGE;
1866 }
1867 }
1868
1869 return value;
1870}
1871
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001872static std::pair<vixl32::Condition, vixl32::Condition> GenerateLongTestConstant(
1873 HCondition* condition,
1874 bool invert,
1875 CodeGeneratorARMVIXL* codegen) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001876 DCHECK_EQ(condition->GetLeft()->GetType(), DataType::Type::kInt64);
Donghui Bai426b49c2016-11-08 14:55:38 +08001877
1878 const LocationSummary* const locations = condition->GetLocations();
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001879 IfCondition cond = condition->GetCondition();
1880 IfCondition opposite = condition->GetOppositeCondition();
1881
1882 if (invert) {
1883 std::swap(cond, opposite);
1884 }
1885
1886 std::pair<vixl32::Condition, vixl32::Condition> ret(eq, ne);
Donghui Bai426b49c2016-11-08 14:55:38 +08001887 const Location left = locations->InAt(0);
1888 const Location right = locations->InAt(1);
1889
1890 DCHECK(right.IsConstant());
1891
1892 const vixl32::Register left_high = HighRegisterFrom(left);
1893 const vixl32::Register left_low = LowRegisterFrom(left);
Anton Kirilov5601d4e2017-05-11 19:33:50 +01001894 int64_t value = AdjustConstantForCondition(Int64ConstantFrom(right), &cond, &opposite);
1895 UseScratchRegisterScope temps(codegen->GetVIXLAssembler());
1896
1897 // Comparisons against 0 are common enough to deserve special attention.
1898 if (value == 0) {
1899 switch (cond) {
1900 case kCondNE:
1901 // x > 0 iff x != 0 when the comparison is unsigned.
1902 case kCondA:
1903 ret = std::make_pair(ne, eq);
1904 FALLTHROUGH_INTENDED;
1905 case kCondEQ:
1906 // x <= 0 iff x == 0 when the comparison is unsigned.
1907 case kCondBE:
1908 __ Orrs(temps.Acquire(), left_low, left_high);
1909 return ret;
1910 case kCondLT:
1911 case kCondGE:
1912 __ Cmp(left_high, 0);
1913 return std::make_pair(ARMCondition(cond), ARMCondition(opposite));
1914 // Trivially true or false.
1915 case kCondB:
1916 ret = std::make_pair(ne, eq);
1917 FALLTHROUGH_INTENDED;
1918 case kCondAE:
1919 __ Cmp(left_low, left_low);
1920 return ret;
1921 default:
1922 break;
1923 }
1924 }
Donghui Bai426b49c2016-11-08 14:55:38 +08001925
1926 switch (cond) {
1927 case kCondEQ:
1928 case kCondNE:
1929 case kCondB:
1930 case kCondBE:
1931 case kCondA:
1932 case kCondAE: {
Anton Kirilov23b752b2017-07-20 14:40:44 +01001933 const uint32_t value_low = Low32Bits(value);
1934 Operand operand_low(value_low);
1935
Donghui Bai426b49c2016-11-08 14:55:38 +08001936 __ Cmp(left_high, High32Bits(value));
1937
Anton Kirilov23b752b2017-07-20 14:40:44 +01001938 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
1939 // we must ensure that the operands corresponding to the least significant
1940 // halves of the inputs fit into a 16-bit CMP encoding.
1941 if (!left_low.IsLow() || !IsUint<8>(value_low)) {
1942 operand_low = Operand(temps.Acquire());
1943 __ Mov(LeaveFlags, operand_low.GetBaseRegister(), value_low);
1944 }
1945
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001946 // We use the scope because of the IT block that follows.
Donghui Bai426b49c2016-11-08 14:55:38 +08001947 ExactAssemblyScope guard(codegen->GetVIXLAssembler(),
1948 2 * vixl32::k16BitT32InstructionSizeInBytes,
1949 CodeBufferCheckScope::kExactSize);
1950
1951 __ it(eq);
Anton Kirilov23b752b2017-07-20 14:40:44 +01001952 __ cmp(eq, left_low, operand_low);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001953 ret = std::make_pair(ARMUnsignedCondition(cond), ARMUnsignedCondition(opposite));
Donghui Bai426b49c2016-11-08 14:55:38 +08001954 break;
1955 }
1956 case kCondLE:
1957 case kCondGT:
1958 // Trivially true or false.
1959 if (value == std::numeric_limits<int64_t>::max()) {
1960 __ Cmp(left_low, left_low);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001961 ret = cond == kCondLE ? std::make_pair(eq, ne) : std::make_pair(ne, eq);
Donghui Bai426b49c2016-11-08 14:55:38 +08001962 break;
1963 }
1964
1965 if (cond == kCondLE) {
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001966 DCHECK_EQ(opposite, kCondGT);
Donghui Bai426b49c2016-11-08 14:55:38 +08001967 cond = kCondLT;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001968 opposite = kCondGE;
Donghui Bai426b49c2016-11-08 14:55:38 +08001969 } else {
1970 DCHECK_EQ(cond, kCondGT);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001971 DCHECK_EQ(opposite, kCondLE);
Donghui Bai426b49c2016-11-08 14:55:38 +08001972 cond = kCondGE;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001973 opposite = kCondLT;
Donghui Bai426b49c2016-11-08 14:55:38 +08001974 }
1975
1976 value++;
1977 FALLTHROUGH_INTENDED;
1978 case kCondGE:
1979 case kCondLT: {
Donghui Bai426b49c2016-11-08 14:55:38 +08001980 __ Cmp(left_low, Low32Bits(value));
1981 __ Sbcs(temps.Acquire(), left_high, High32Bits(value));
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001982 ret = std::make_pair(ARMCondition(cond), ARMCondition(opposite));
Donghui Bai426b49c2016-11-08 14:55:38 +08001983 break;
1984 }
1985 default:
1986 LOG(FATAL) << "Unreachable";
1987 UNREACHABLE();
1988 }
1989
1990 return ret;
1991}
1992
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001993static std::pair<vixl32::Condition, vixl32::Condition> GenerateLongTest(
1994 HCondition* condition,
1995 bool invert,
1996 CodeGeneratorARMVIXL* codegen) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001997 DCHECK_EQ(condition->GetLeft()->GetType(), DataType::Type::kInt64);
Donghui Bai426b49c2016-11-08 14:55:38 +08001998
1999 const LocationSummary* const locations = condition->GetLocations();
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002000 IfCondition cond = condition->GetCondition();
2001 IfCondition opposite = condition->GetOppositeCondition();
2002
2003 if (invert) {
2004 std::swap(cond, opposite);
2005 }
2006
2007 std::pair<vixl32::Condition, vixl32::Condition> ret(eq, ne);
Donghui Bai426b49c2016-11-08 14:55:38 +08002008 Location left = locations->InAt(0);
2009 Location right = locations->InAt(1);
2010
2011 DCHECK(right.IsRegisterPair());
2012
2013 switch (cond) {
2014 case kCondEQ:
2015 case kCondNE:
2016 case kCondB:
2017 case kCondBE:
2018 case kCondA:
2019 case kCondAE: {
2020 __ Cmp(HighRegisterFrom(left), HighRegisterFrom(right));
2021
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002022 // We use the scope because of the IT block that follows.
Donghui Bai426b49c2016-11-08 14:55:38 +08002023 ExactAssemblyScope guard(codegen->GetVIXLAssembler(),
2024 2 * vixl32::k16BitT32InstructionSizeInBytes,
2025 CodeBufferCheckScope::kExactSize);
2026
2027 __ it(eq);
2028 __ cmp(eq, LowRegisterFrom(left), LowRegisterFrom(right));
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002029 ret = std::make_pair(ARMUnsignedCondition(cond), ARMUnsignedCondition(opposite));
Donghui Bai426b49c2016-11-08 14:55:38 +08002030 break;
2031 }
2032 case kCondLE:
2033 case kCondGT:
2034 if (cond == kCondLE) {
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002035 DCHECK_EQ(opposite, kCondGT);
Donghui Bai426b49c2016-11-08 14:55:38 +08002036 cond = kCondGE;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002037 opposite = kCondLT;
Donghui Bai426b49c2016-11-08 14:55:38 +08002038 } else {
2039 DCHECK_EQ(cond, kCondGT);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002040 DCHECK_EQ(opposite, kCondLE);
Donghui Bai426b49c2016-11-08 14:55:38 +08002041 cond = kCondLT;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002042 opposite = kCondGE;
Donghui Bai426b49c2016-11-08 14:55:38 +08002043 }
2044
2045 std::swap(left, right);
2046 FALLTHROUGH_INTENDED;
2047 case kCondGE:
2048 case kCondLT: {
2049 UseScratchRegisterScope temps(codegen->GetVIXLAssembler());
2050
2051 __ Cmp(LowRegisterFrom(left), LowRegisterFrom(right));
2052 __ Sbcs(temps.Acquire(), HighRegisterFrom(left), HighRegisterFrom(right));
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002053 ret = std::make_pair(ARMCondition(cond), ARMCondition(opposite));
Donghui Bai426b49c2016-11-08 14:55:38 +08002054 break;
2055 }
2056 default:
2057 LOG(FATAL) << "Unreachable";
2058 UNREACHABLE();
2059 }
2060
2061 return ret;
2062}
2063
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002064static std::pair<vixl32::Condition, vixl32::Condition> GenerateTest(HCondition* condition,
2065 bool invert,
2066 CodeGeneratorARMVIXL* codegen) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002067 const DataType::Type type = condition->GetLeft()->GetType();
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002068 IfCondition cond = condition->GetCondition();
2069 IfCondition opposite = condition->GetOppositeCondition();
2070 std::pair<vixl32::Condition, vixl32::Condition> ret(eq, ne);
Donghui Bai426b49c2016-11-08 14:55:38 +08002071
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002072 if (invert) {
2073 std::swap(cond, opposite);
2074 }
Donghui Bai426b49c2016-11-08 14:55:38 +08002075
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002076 if (type == DataType::Type::kInt64) {
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002077 ret = condition->GetLocations()->InAt(1).IsConstant()
2078 ? GenerateLongTestConstant(condition, invert, codegen)
2079 : GenerateLongTest(condition, invert, codegen);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002080 } else if (DataType::IsFloatingPointType(type)) {
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002081 GenerateVcmp(condition, codegen);
2082 __ Vmrs(RegisterOrAPSR_nzcv(kPcCode), FPSCR);
2083 ret = std::make_pair(ARMFPCondition(cond, condition->IsGtBias()),
2084 ARMFPCondition(opposite, condition->IsGtBias()));
Donghui Bai426b49c2016-11-08 14:55:38 +08002085 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002086 DCHECK(DataType::IsIntegralType(type) || type == DataType::Type::kReference) << type;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002087 __ Cmp(InputRegisterAt(condition, 0), InputOperandAt(condition, 1));
2088 ret = std::make_pair(ARMCondition(cond), ARMCondition(opposite));
Donghui Bai426b49c2016-11-08 14:55:38 +08002089 }
2090
2091 return ret;
2092}
2093
Anton Kirilov5601d4e2017-05-11 19:33:50 +01002094static void GenerateConditionGeneric(HCondition* cond, CodeGeneratorARMVIXL* codegen) {
Anton Kirilov5601d4e2017-05-11 19:33:50 +01002095 const vixl32::Register out = OutputRegister(cond);
2096 const auto condition = GenerateTest(cond, false, codegen);
2097
2098 __ Mov(LeaveFlags, out, 0);
2099
2100 if (out.IsLow()) {
2101 // We use the scope because of the IT block that follows.
2102 ExactAssemblyScope guard(codegen->GetVIXLAssembler(),
2103 2 * vixl32::k16BitT32InstructionSizeInBytes,
2104 CodeBufferCheckScope::kExactSize);
2105
2106 __ it(condition.first);
2107 __ mov(condition.first, out, 1);
2108 } else {
2109 vixl32::Label done_label;
2110 vixl32::Label* const final_label = codegen->GetFinalLabel(cond, &done_label);
2111
2112 __ B(condition.second, final_label, /* far_target */ false);
2113 __ Mov(out, 1);
2114
2115 if (done_label.IsReferenced()) {
2116 __ Bind(&done_label);
2117 }
2118 }
2119}
2120
2121static void GenerateEqualLong(HCondition* cond, CodeGeneratorARMVIXL* codegen) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002122 DCHECK_EQ(cond->GetLeft()->GetType(), DataType::Type::kInt64);
Anton Kirilov5601d4e2017-05-11 19:33:50 +01002123
2124 const LocationSummary* const locations = cond->GetLocations();
2125 IfCondition condition = cond->GetCondition();
2126 const vixl32::Register out = OutputRegister(cond);
2127 const Location left = locations->InAt(0);
2128 const Location right = locations->InAt(1);
2129 vixl32::Register left_high = HighRegisterFrom(left);
2130 vixl32::Register left_low = LowRegisterFrom(left);
2131 vixl32::Register temp;
2132 UseScratchRegisterScope temps(codegen->GetVIXLAssembler());
2133
2134 if (right.IsConstant()) {
2135 IfCondition opposite = cond->GetOppositeCondition();
2136 const int64_t value = AdjustConstantForCondition(Int64ConstantFrom(right),
2137 &condition,
2138 &opposite);
2139 Operand right_high = High32Bits(value);
2140 Operand right_low = Low32Bits(value);
2141
2142 // The output uses Location::kNoOutputOverlap.
2143 if (out.Is(left_high)) {
2144 std::swap(left_low, left_high);
2145 std::swap(right_low, right_high);
2146 }
2147
2148 __ Sub(out, left_low, right_low);
2149 temp = temps.Acquire();
2150 __ Sub(temp, left_high, right_high);
2151 } else {
2152 DCHECK(right.IsRegisterPair());
2153 temp = temps.Acquire();
2154 __ Sub(temp, left_high, HighRegisterFrom(right));
2155 __ Sub(out, left_low, LowRegisterFrom(right));
2156 }
2157
2158 // Need to check after calling AdjustConstantForCondition().
2159 DCHECK(condition == kCondEQ || condition == kCondNE) << condition;
2160
2161 if (condition == kCondNE && out.IsLow()) {
2162 __ Orrs(out, out, temp);
2163
2164 // We use the scope because of the IT block that follows.
2165 ExactAssemblyScope guard(codegen->GetVIXLAssembler(),
2166 2 * vixl32::k16BitT32InstructionSizeInBytes,
2167 CodeBufferCheckScope::kExactSize);
2168
2169 __ it(ne);
2170 __ mov(ne, out, 1);
2171 } else {
2172 __ Orr(out, out, temp);
2173 codegen->GenerateConditionWithZero(condition, out, out, temp);
2174 }
2175}
2176
Anton Kirilov5601d4e2017-05-11 19:33:50 +01002177static void GenerateConditionLong(HCondition* cond, CodeGeneratorARMVIXL* codegen) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002178 DCHECK_EQ(cond->GetLeft()->GetType(), DataType::Type::kInt64);
Anton Kirilov5601d4e2017-05-11 19:33:50 +01002179
2180 const LocationSummary* const locations = cond->GetLocations();
2181 IfCondition condition = cond->GetCondition();
2182 const vixl32::Register out = OutputRegister(cond);
2183 const Location left = locations->InAt(0);
2184 const Location right = locations->InAt(1);
2185
2186 if (right.IsConstant()) {
2187 IfCondition opposite = cond->GetOppositeCondition();
2188
2189 // Comparisons against 0 are common enough to deserve special attention.
2190 if (AdjustConstantForCondition(Int64ConstantFrom(right), &condition, &opposite) == 0) {
2191 switch (condition) {
2192 case kCondNE:
2193 case kCondA:
2194 if (out.IsLow()) {
2195 // We only care if both input registers are 0 or not.
2196 __ Orrs(out, LowRegisterFrom(left), HighRegisterFrom(left));
2197
2198 // We use the scope because of the IT block that follows.
2199 ExactAssemblyScope guard(codegen->GetVIXLAssembler(),
2200 2 * vixl32::k16BitT32InstructionSizeInBytes,
2201 CodeBufferCheckScope::kExactSize);
2202
2203 __ it(ne);
2204 __ mov(ne, out, 1);
2205 return;
2206 }
2207
2208 FALLTHROUGH_INTENDED;
2209 case kCondEQ:
2210 case kCondBE:
2211 // We only care if both input registers are 0 or not.
2212 __ Orr(out, LowRegisterFrom(left), HighRegisterFrom(left));
2213 codegen->GenerateConditionWithZero(condition, out, out);
2214 return;
2215 case kCondLT:
2216 case kCondGE:
2217 // We only care about the sign bit.
2218 FALLTHROUGH_INTENDED;
2219 case kCondAE:
2220 case kCondB:
2221 codegen->GenerateConditionWithZero(condition, out, HighRegisterFrom(left));
2222 return;
2223 case kCondLE:
2224 case kCondGT:
2225 default:
2226 break;
2227 }
2228 }
2229 }
2230
Anton Kirilov23b752b2017-07-20 14:40:44 +01002231 // If `out` is a low register, then the GenerateConditionGeneric()
2232 // function generates a shorter code sequence that is still branchless.
2233 if ((condition == kCondEQ || condition == kCondNE) && !out.IsLow()) {
Anton Kirilov5601d4e2017-05-11 19:33:50 +01002234 GenerateEqualLong(cond, codegen);
2235 return;
2236 }
2237
Anton Kirilov23b752b2017-07-20 14:40:44 +01002238 GenerateConditionGeneric(cond, codegen);
Anton Kirilov5601d4e2017-05-11 19:33:50 +01002239}
2240
Roland Levillain6d729a72017-06-30 18:34:01 +01002241static void GenerateConditionIntegralOrNonPrimitive(HCondition* cond,
2242 CodeGeneratorARMVIXL* codegen) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002243 const DataType::Type type = cond->GetLeft()->GetType();
Anton Kirilov5601d4e2017-05-11 19:33:50 +01002244
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002245 DCHECK(DataType::IsIntegralType(type) || type == DataType::Type::kReference) << type;
Anton Kirilov5601d4e2017-05-11 19:33:50 +01002246
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002247 if (type == DataType::Type::kInt64) {
Anton Kirilov5601d4e2017-05-11 19:33:50 +01002248 GenerateConditionLong(cond, codegen);
2249 return;
2250 }
2251
2252 IfCondition condition = cond->GetCondition();
2253 vixl32::Register in = InputRegisterAt(cond, 0);
2254 const vixl32::Register out = OutputRegister(cond);
2255 const Location right = cond->GetLocations()->InAt(1);
2256 int64_t value;
2257
2258 if (right.IsConstant()) {
2259 IfCondition opposite = cond->GetOppositeCondition();
2260
2261 value = AdjustConstantForCondition(Int64ConstantFrom(right), &condition, &opposite);
2262
2263 // Comparisons against 0 are common enough to deserve special attention.
2264 if (value == 0) {
2265 switch (condition) {
2266 case kCondNE:
2267 case kCondA:
2268 if (out.IsLow() && out.Is(in)) {
2269 __ Cmp(out, 0);
2270
2271 // We use the scope because of the IT block that follows.
2272 ExactAssemblyScope guard(codegen->GetVIXLAssembler(),
2273 2 * vixl32::k16BitT32InstructionSizeInBytes,
2274 CodeBufferCheckScope::kExactSize);
2275
2276 __ it(ne);
2277 __ mov(ne, out, 1);
2278 return;
2279 }
2280
2281 FALLTHROUGH_INTENDED;
2282 case kCondEQ:
2283 case kCondBE:
2284 case kCondLT:
2285 case kCondGE:
2286 case kCondAE:
2287 case kCondB:
2288 codegen->GenerateConditionWithZero(condition, out, in);
2289 return;
2290 case kCondLE:
2291 case kCondGT:
2292 default:
2293 break;
2294 }
2295 }
2296 }
2297
2298 if (condition == kCondEQ || condition == kCondNE) {
2299 Operand operand(0);
2300
2301 if (right.IsConstant()) {
2302 operand = Operand::From(value);
2303 } else if (out.Is(RegisterFrom(right))) {
2304 // Avoid 32-bit instructions if possible.
2305 operand = InputOperandAt(cond, 0);
2306 in = RegisterFrom(right);
2307 } else {
2308 operand = InputOperandAt(cond, 1);
2309 }
2310
2311 if (condition == kCondNE && out.IsLow()) {
2312 __ Subs(out, in, operand);
2313
2314 // We use the scope because of the IT block that follows.
2315 ExactAssemblyScope guard(codegen->GetVIXLAssembler(),
2316 2 * vixl32::k16BitT32InstructionSizeInBytes,
2317 CodeBufferCheckScope::kExactSize);
2318
2319 __ it(ne);
2320 __ mov(ne, out, 1);
2321 } else {
2322 __ Sub(out, in, operand);
2323 codegen->GenerateConditionWithZero(condition, out, out);
2324 }
2325
2326 return;
2327 }
2328
2329 GenerateConditionGeneric(cond, codegen);
2330}
2331
Donghui Bai426b49c2016-11-08 14:55:38 +08002332static bool CanEncodeConstantAs8BitImmediate(HConstant* constant) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002333 const DataType::Type type = constant->GetType();
Donghui Bai426b49c2016-11-08 14:55:38 +08002334 bool ret = false;
2335
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002336 DCHECK(DataType::IsIntegralType(type) || type == DataType::Type::kReference) << type;
Donghui Bai426b49c2016-11-08 14:55:38 +08002337
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002338 if (type == DataType::Type::kInt64) {
Donghui Bai426b49c2016-11-08 14:55:38 +08002339 const uint64_t value = Uint64ConstantFrom(constant);
2340
2341 ret = IsUint<8>(Low32Bits(value)) && IsUint<8>(High32Bits(value));
2342 } else {
2343 ret = IsUint<8>(Int32ConstantFrom(constant));
2344 }
2345
2346 return ret;
2347}
2348
2349static Location Arm8BitEncodableConstantOrRegister(HInstruction* constant) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002350 DCHECK(!DataType::IsFloatingPointType(constant->GetType()));
Donghui Bai426b49c2016-11-08 14:55:38 +08002351
2352 if (constant->IsConstant() && CanEncodeConstantAs8BitImmediate(constant->AsConstant())) {
2353 return Location::ConstantLocation(constant->AsConstant());
2354 }
2355
2356 return Location::RequiresRegister();
2357}
2358
2359static bool CanGenerateConditionalMove(const Location& out, const Location& src) {
2360 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
2361 // we check that we are not dealing with floating-point output (there is no
2362 // 16-bit VMOV encoding).
2363 if (!out.IsRegister() && !out.IsRegisterPair()) {
2364 return false;
2365 }
2366
2367 // For constants, we also check that the output is in one or two low registers,
2368 // and that the constants fit in an 8-bit unsigned integer, so that a 16-bit
2369 // MOV encoding can be used.
2370 if (src.IsConstant()) {
2371 if (!CanEncodeConstantAs8BitImmediate(src.GetConstant())) {
2372 return false;
2373 }
2374
2375 if (out.IsRegister()) {
2376 if (!RegisterFrom(out).IsLow()) {
2377 return false;
2378 }
2379 } else {
2380 DCHECK(out.IsRegisterPair());
2381
2382 if (!HighRegisterFrom(out).IsLow()) {
2383 return false;
2384 }
2385 }
2386 }
2387
2388 return true;
2389}
2390
Scott Wakelingfe885462016-09-22 10:24:38 +01002391#undef __
2392
Donghui Bai426b49c2016-11-08 14:55:38 +08002393vixl32::Label* CodeGeneratorARMVIXL::GetFinalLabel(HInstruction* instruction,
2394 vixl32::Label* final_label) {
2395 DCHECK(!instruction->IsControlFlow() && !instruction->IsSuspendCheck());
Anton Kirilov6f644202017-02-27 18:29:45 +00002396 DCHECK(!instruction->IsInvoke() || !instruction->GetLocations()->CanCall());
Donghui Bai426b49c2016-11-08 14:55:38 +08002397
2398 const HBasicBlock* const block = instruction->GetBlock();
2399 const HLoopInformation* const info = block->GetLoopInformation();
2400 HInstruction* const next = instruction->GetNext();
2401
2402 // Avoid a branch to a branch.
2403 if (next->IsGoto() && (info == nullptr ||
2404 !info->IsBackEdge(*block) ||
2405 !info->HasSuspendCheck())) {
2406 final_label = GetLabelOf(next->AsGoto()->GetSuccessor());
2407 }
2408
2409 return final_label;
2410}
2411
Scott Wakelingfe885462016-09-22 10:24:38 +01002412CodeGeneratorARMVIXL::CodeGeneratorARMVIXL(HGraph* graph,
2413 const ArmInstructionSetFeatures& isa_features,
2414 const CompilerOptions& compiler_options,
2415 OptimizingCompilerStats* stats)
2416 : CodeGenerator(graph,
2417 kNumberOfCoreRegisters,
2418 kNumberOfSRegisters,
2419 kNumberOfRegisterPairs,
2420 kCoreCalleeSaves.GetList(),
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002421 ComputeSRegisterListMask(kFpuCalleeSaves),
Scott Wakelingfe885462016-09-22 10:24:38 +01002422 compiler_options,
2423 stats),
2424 block_labels_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Artem Serov551b28f2016-10-18 19:11:30 +01002425 jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Scott Wakelingfe885462016-09-22 10:24:38 +01002426 location_builder_(graph, this),
2427 instruction_visitor_(graph, this),
2428 move_resolver_(graph->GetArena(), this),
2429 assembler_(graph->GetArena()),
Artem Serovd4cc5b22016-11-04 11:19:09 +00002430 isa_features_(isa_features),
Artem Serovc5fcb442016-12-02 19:19:58 +00002431 uint32_literals_(std::less<uint32_t>(),
2432 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko65979462017-05-19 17:25:12 +01002433 pc_relative_method_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko0eb882b2017-05-15 13:39:18 +01002434 method_bss_entry_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Artem Serovc5fcb442016-12-02 19:19:58 +00002435 pc_relative_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko1998cd02017-01-13 13:02:58 +00002436 type_bss_entry_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko65979462017-05-19 17:25:12 +01002437 pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01002438 string_bss_entry_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01002439 baker_read_barrier_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Artem Serovc5fcb442016-12-02 19:19:58 +00002440 jit_string_patches_(StringReferenceValueComparator(),
2441 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
2442 jit_class_patches_(TypeReferenceValueComparator(),
2443 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Scott Wakelingfe885462016-09-22 10:24:38 +01002444 // Always save the LR register to mimic Quick.
2445 AddAllocatedRegister(Location::RegisterLocation(LR));
Nicolas Geoffray13a797b2017-03-15 16:41:31 +00002446 // Give D30 and D31 as scratch register to VIXL. The register allocator only works on
2447 // S0-S31, which alias to D0-D15.
2448 GetVIXLAssembler()->GetScratchVRegisterList()->Combine(d31);
2449 GetVIXLAssembler()->GetScratchVRegisterList()->Combine(d30);
Scott Wakelingfe885462016-09-22 10:24:38 +01002450}
2451
Artem Serov551b28f2016-10-18 19:11:30 +01002452void JumpTableARMVIXL::EmitTable(CodeGeneratorARMVIXL* codegen) {
2453 uint32_t num_entries = switch_instr_->GetNumEntries();
2454 DCHECK_GE(num_entries, kPackedSwitchCompareJumpThreshold);
2455
2456 // We are about to use the assembler to place literals directly. Make sure we have enough
Scott Wakelingb77051e2016-11-21 19:46:00 +00002457 // underlying code buffer and we have generated a jump table of the right size, using
2458 // codegen->GetVIXLAssembler()->GetBuffer().Align();
Artem Serov0fb37192016-12-06 18:13:40 +00002459 ExactAssemblyScope aas(codegen->GetVIXLAssembler(),
2460 num_entries * sizeof(int32_t),
2461 CodeBufferCheckScope::kMaximumSize);
Artem Serov551b28f2016-10-18 19:11:30 +01002462 // TODO(VIXL): Check that using lower case bind is fine here.
2463 codegen->GetVIXLAssembler()->bind(&table_start_);
Artem Serov09a940d2016-11-11 16:15:11 +00002464 for (uint32_t i = 0; i < num_entries; i++) {
2465 codegen->GetVIXLAssembler()->place(bb_addresses_[i].get());
2466 }
2467}
2468
2469void JumpTableARMVIXL::FixTable(CodeGeneratorARMVIXL* codegen) {
2470 uint32_t num_entries = switch_instr_->GetNumEntries();
2471 DCHECK_GE(num_entries, kPackedSwitchCompareJumpThreshold);
2472
Artem Serov551b28f2016-10-18 19:11:30 +01002473 const ArenaVector<HBasicBlock*>& successors = switch_instr_->GetBlock()->GetSuccessors();
2474 for (uint32_t i = 0; i < num_entries; i++) {
2475 vixl32::Label* target_label = codegen->GetLabelOf(successors[i]);
2476 DCHECK(target_label->IsBound());
2477 int32_t jump_offset = target_label->GetLocation() - table_start_.GetLocation();
2478 // When doing BX to address we need to have lower bit set to 1 in T32.
2479 if (codegen->GetVIXLAssembler()->IsUsingT32()) {
2480 jump_offset++;
2481 }
2482 DCHECK_GT(jump_offset, std::numeric_limits<int32_t>::min());
2483 DCHECK_LE(jump_offset, std::numeric_limits<int32_t>::max());
Artem Serov09a940d2016-11-11 16:15:11 +00002484
Scott Wakelingb77051e2016-11-21 19:46:00 +00002485 bb_addresses_[i].get()->UpdateValue(jump_offset, codegen->GetVIXLAssembler()->GetBuffer());
Artem Serov551b28f2016-10-18 19:11:30 +01002486 }
2487}
2488
Artem Serov09a940d2016-11-11 16:15:11 +00002489void CodeGeneratorARMVIXL::FixJumpTables() {
Artem Serov551b28f2016-10-18 19:11:30 +01002490 for (auto&& jump_table : jump_tables_) {
Artem Serov09a940d2016-11-11 16:15:11 +00002491 jump_table->FixTable(this);
Artem Serov551b28f2016-10-18 19:11:30 +01002492 }
2493}
2494
Andreas Gampeca620d72016-11-08 08:09:33 -08002495#define __ reinterpret_cast<ArmVIXLAssembler*>(GetAssembler())->GetVIXLAssembler()-> // NOLINT
Scott Wakelingfe885462016-09-22 10:24:38 +01002496
2497void CodeGeneratorARMVIXL::Finalize(CodeAllocator* allocator) {
Artem Serov09a940d2016-11-11 16:15:11 +00002498 FixJumpTables();
Scott Wakelingfe885462016-09-22 10:24:38 +01002499 GetAssembler()->FinalizeCode();
2500 CodeGenerator::Finalize(allocator);
2501}
2502
2503void CodeGeneratorARMVIXL::SetupBlockedRegisters() const {
Scott Wakelingfe885462016-09-22 10:24:38 +01002504 // Stack register, LR and PC are always reserved.
2505 blocked_core_registers_[SP] = true;
2506 blocked_core_registers_[LR] = true;
2507 blocked_core_registers_[PC] = true;
2508
Roland Levillain6d729a72017-06-30 18:34:01 +01002509 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2510 // Reserve marking register.
2511 blocked_core_registers_[MR] = true;
2512 }
2513
Scott Wakelingfe885462016-09-22 10:24:38 +01002514 // Reserve thread register.
2515 blocked_core_registers_[TR] = true;
2516
2517 // Reserve temp register.
2518 blocked_core_registers_[IP] = true;
2519
2520 if (GetGraph()->IsDebuggable()) {
2521 // Stubs do not save callee-save floating point registers. If the graph
2522 // is debuggable, we need to deal with these registers differently. For
2523 // now, just block them.
2524 for (uint32_t i = kFpuCalleeSaves.GetFirstSRegister().GetCode();
2525 i <= kFpuCalleeSaves.GetLastSRegister().GetCode();
2526 ++i) {
2527 blocked_fpu_registers_[i] = true;
2528 }
2529 }
Scott Wakelingfe885462016-09-22 10:24:38 +01002530}
2531
Scott Wakelingfe885462016-09-22 10:24:38 +01002532InstructionCodeGeneratorARMVIXL::InstructionCodeGeneratorARMVIXL(HGraph* graph,
2533 CodeGeneratorARMVIXL* codegen)
2534 : InstructionCodeGenerator(graph, codegen),
2535 assembler_(codegen->GetAssembler()),
2536 codegen_(codegen) {}
2537
2538void CodeGeneratorARMVIXL::ComputeSpillMask() {
2539 core_spill_mask_ = allocated_registers_.GetCoreRegisters() & core_callee_save_mask_;
2540 DCHECK_NE(core_spill_mask_, 0u) << "At least the return address register must be saved";
2541 // There is no easy instruction to restore just the PC on thumb2. We spill and
2542 // restore another arbitrary register.
2543 core_spill_mask_ |= (1 << kCoreAlwaysSpillRegister.GetCode());
2544 fpu_spill_mask_ = allocated_registers_.GetFloatingPointRegisters() & fpu_callee_save_mask_;
2545 // We use vpush and vpop for saving and restoring floating point registers, which take
2546 // a SRegister and the number of registers to save/restore after that SRegister. We
2547 // therefore update the `fpu_spill_mask_` to also contain those registers not allocated,
2548 // but in the range.
2549 if (fpu_spill_mask_ != 0) {
2550 uint32_t least_significant_bit = LeastSignificantBit(fpu_spill_mask_);
2551 uint32_t most_significant_bit = MostSignificantBit(fpu_spill_mask_);
2552 for (uint32_t i = least_significant_bit + 1 ; i < most_significant_bit; ++i) {
2553 fpu_spill_mask_ |= (1 << i);
2554 }
2555 }
2556}
2557
2558void CodeGeneratorARMVIXL::GenerateFrameEntry() {
2559 bool skip_overflow_check =
2560 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kArm);
2561 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
2562 __ Bind(&frame_entry_label_);
2563
2564 if (HasEmptyFrame()) {
2565 return;
2566 }
2567
Scott Wakelingfe885462016-09-22 10:24:38 +01002568 if (!skip_overflow_check) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002569 UseScratchRegisterScope temps(GetVIXLAssembler());
2570 vixl32::Register temp = temps.Acquire();
Anton Kirilov644032c2016-12-06 17:51:43 +00002571 __ Sub(temp, sp, Operand::From(GetStackOverflowReservedBytes(kArm)));
Scott Wakelingfe885462016-09-22 10:24:38 +01002572 // The load must immediately precede RecordPcInfo.
Artem Serov0fb37192016-12-06 18:13:40 +00002573 ExactAssemblyScope aas(GetVIXLAssembler(),
2574 vixl32::kMaxInstructionSizeInBytes,
2575 CodeBufferCheckScope::kMaximumSize);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002576 __ ldr(temp, MemOperand(temp));
2577 RecordPcInfo(nullptr, 0);
Scott Wakelingfe885462016-09-22 10:24:38 +01002578 }
2579
2580 __ Push(RegisterList(core_spill_mask_));
2581 GetAssembler()->cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(core_spill_mask_));
2582 GetAssembler()->cfi().RelOffsetForMany(DWARFReg(kMethodRegister),
2583 0,
2584 core_spill_mask_,
2585 kArmWordSize);
2586 if (fpu_spill_mask_ != 0) {
2587 uint32_t first = LeastSignificantBit(fpu_spill_mask_);
2588
2589 // Check that list is contiguous.
2590 DCHECK_EQ(fpu_spill_mask_ >> CTZ(fpu_spill_mask_), ~0u >> (32 - POPCOUNT(fpu_spill_mask_)));
2591
2592 __ Vpush(SRegisterList(vixl32::SRegister(first), POPCOUNT(fpu_spill_mask_)));
2593 GetAssembler()->cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(fpu_spill_mask_));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002594 GetAssembler()->cfi().RelOffsetForMany(DWARFReg(s0), 0, fpu_spill_mask_, kArmWordSize);
Scott Wakelingfe885462016-09-22 10:24:38 +01002595 }
Scott Wakelingbffdc702016-12-07 17:46:03 +00002596
Scott Wakelingfe885462016-09-22 10:24:38 +01002597 int adjust = GetFrameSize() - FrameEntrySpillSize();
2598 __ Sub(sp, sp, adjust);
2599 GetAssembler()->cfi().AdjustCFAOffset(adjust);
Scott Wakelingbffdc702016-12-07 17:46:03 +00002600
2601 // Save the current method if we need it. Note that we do not
2602 // do this in HCurrentMethod, as the instruction might have been removed
2603 // in the SSA graph.
2604 if (RequiresCurrentMethod()) {
2605 GetAssembler()->StoreToOffset(kStoreWord, kMethodRegister, sp, 0);
2606 }
Nicolas Geoffrayf7893532017-06-15 12:34:36 +01002607
2608 if (GetGraph()->HasShouldDeoptimizeFlag()) {
2609 UseScratchRegisterScope temps(GetVIXLAssembler());
2610 vixl32::Register temp = temps.Acquire();
2611 // Initialize should_deoptimize flag to 0.
2612 __ Mov(temp, 0);
2613 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, GetStackOffsetOfShouldDeoptimizeFlag());
2614 }
Roland Levillain5daa4952017-07-03 17:23:56 +01002615
2616 MaybeGenerateMarkingRegisterCheck(/* code */ 1);
Scott Wakelingfe885462016-09-22 10:24:38 +01002617}
2618
2619void CodeGeneratorARMVIXL::GenerateFrameExit() {
2620 if (HasEmptyFrame()) {
2621 __ Bx(lr);
2622 return;
2623 }
2624 GetAssembler()->cfi().RememberState();
2625 int adjust = GetFrameSize() - FrameEntrySpillSize();
2626 __ Add(sp, sp, adjust);
2627 GetAssembler()->cfi().AdjustCFAOffset(-adjust);
2628 if (fpu_spill_mask_ != 0) {
2629 uint32_t first = LeastSignificantBit(fpu_spill_mask_);
2630
2631 // Check that list is contiguous.
2632 DCHECK_EQ(fpu_spill_mask_ >> CTZ(fpu_spill_mask_), ~0u >> (32 - POPCOUNT(fpu_spill_mask_)));
2633
2634 __ Vpop(SRegisterList(vixl32::SRegister(first), POPCOUNT(fpu_spill_mask_)));
2635 GetAssembler()->cfi().AdjustCFAOffset(
2636 -static_cast<int>(kArmWordSize) * POPCOUNT(fpu_spill_mask_));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002637 GetAssembler()->cfi().RestoreMany(DWARFReg(vixl32::SRegister(0)), fpu_spill_mask_);
Scott Wakelingfe885462016-09-22 10:24:38 +01002638 }
2639 // Pop LR into PC to return.
2640 DCHECK_NE(core_spill_mask_ & (1 << kLrCode), 0U);
2641 uint32_t pop_mask = (core_spill_mask_ & (~(1 << kLrCode))) | 1 << kPcCode;
2642 __ Pop(RegisterList(pop_mask));
2643 GetAssembler()->cfi().RestoreState();
2644 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
2645}
2646
2647void CodeGeneratorARMVIXL::Bind(HBasicBlock* block) {
2648 __ Bind(GetLabelOf(block));
2649}
2650
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002651Location InvokeDexCallingConventionVisitorARMVIXL::GetNextLocation(DataType::Type type) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002652 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002653 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002654 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002655 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002656 case DataType::Type::kInt8:
2657 case DataType::Type::kUint16:
2658 case DataType::Type::kInt16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002659 case DataType::Type::kInt32: {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002660 uint32_t index = gp_index_++;
2661 uint32_t stack_index = stack_index_++;
2662 if (index < calling_convention.GetNumberOfRegisters()) {
2663 return LocationFrom(calling_convention.GetRegisterAt(index));
2664 } else {
2665 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
2666 }
2667 }
2668
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002669 case DataType::Type::kInt64: {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002670 uint32_t index = gp_index_;
2671 uint32_t stack_index = stack_index_;
2672 gp_index_ += 2;
2673 stack_index_ += 2;
2674 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
2675 if (calling_convention.GetRegisterAt(index).Is(r1)) {
2676 // Skip R1, and use R2_R3 instead.
2677 gp_index_++;
2678 index++;
2679 }
2680 }
2681 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
2682 DCHECK_EQ(calling_convention.GetRegisterAt(index).GetCode() + 1,
2683 calling_convention.GetRegisterAt(index + 1).GetCode());
2684
2685 return LocationFrom(calling_convention.GetRegisterAt(index),
2686 calling_convention.GetRegisterAt(index + 1));
2687 } else {
2688 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
2689 }
2690 }
2691
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002692 case DataType::Type::kFloat32: {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002693 uint32_t stack_index = stack_index_++;
2694 if (float_index_ % 2 == 0) {
2695 float_index_ = std::max(double_index_, float_index_);
2696 }
2697 if (float_index_ < calling_convention.GetNumberOfFpuRegisters()) {
2698 return LocationFrom(calling_convention.GetFpuRegisterAt(float_index_++));
2699 } else {
2700 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
2701 }
2702 }
2703
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002704 case DataType::Type::kFloat64: {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002705 double_index_ = std::max(double_index_, RoundUp(float_index_, 2));
2706 uint32_t stack_index = stack_index_;
2707 stack_index_ += 2;
2708 if (double_index_ + 1 < calling_convention.GetNumberOfFpuRegisters()) {
2709 uint32_t index = double_index_;
2710 double_index_ += 2;
2711 Location result = LocationFrom(
2712 calling_convention.GetFpuRegisterAt(index),
2713 calling_convention.GetFpuRegisterAt(index + 1));
2714 DCHECK(ExpectedPairLayout(result));
2715 return result;
2716 } else {
2717 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
2718 }
2719 }
2720
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002721 case DataType::Type::kVoid:
Artem Serovd4cc5b22016-11-04 11:19:09 +00002722 LOG(FATAL) << "Unexpected parameter type " << type;
2723 break;
2724 }
2725 return Location::NoLocation();
2726}
2727
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002728Location InvokeDexCallingConventionVisitorARMVIXL::GetReturnLocation(DataType::Type type) const {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002729 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002730 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002731 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002732 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002733 case DataType::Type::kInt8:
2734 case DataType::Type::kUint16:
2735 case DataType::Type::kInt16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002736 case DataType::Type::kInt32: {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002737 return LocationFrom(r0);
2738 }
2739
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002740 case DataType::Type::kFloat32: {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002741 return LocationFrom(s0);
2742 }
2743
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002744 case DataType::Type::kInt64: {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002745 return LocationFrom(r0, r1);
2746 }
2747
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002748 case DataType::Type::kFloat64: {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002749 return LocationFrom(s0, s1);
2750 }
2751
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002752 case DataType::Type::kVoid:
Artem Serovd4cc5b22016-11-04 11:19:09 +00002753 return Location::NoLocation();
2754 }
2755
2756 UNREACHABLE();
2757}
2758
2759Location InvokeDexCallingConventionVisitorARMVIXL::GetMethodLocation() const {
2760 return LocationFrom(kMethodRegister);
2761}
2762
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002763void CodeGeneratorARMVIXL::Move32(Location destination, Location source) {
2764 if (source.Equals(destination)) {
2765 return;
2766 }
2767 if (destination.IsRegister()) {
2768 if (source.IsRegister()) {
2769 __ Mov(RegisterFrom(destination), RegisterFrom(source));
2770 } else if (source.IsFpuRegister()) {
2771 __ Vmov(RegisterFrom(destination), SRegisterFrom(source));
2772 } else {
2773 GetAssembler()->LoadFromOffset(kLoadWord,
2774 RegisterFrom(destination),
2775 sp,
2776 source.GetStackIndex());
2777 }
2778 } else if (destination.IsFpuRegister()) {
2779 if (source.IsRegister()) {
2780 __ Vmov(SRegisterFrom(destination), RegisterFrom(source));
2781 } else if (source.IsFpuRegister()) {
2782 __ Vmov(SRegisterFrom(destination), SRegisterFrom(source));
2783 } else {
2784 GetAssembler()->LoadSFromOffset(SRegisterFrom(destination), sp, source.GetStackIndex());
2785 }
2786 } else {
2787 DCHECK(destination.IsStackSlot()) << destination;
2788 if (source.IsRegister()) {
2789 GetAssembler()->StoreToOffset(kStoreWord,
2790 RegisterFrom(source),
2791 sp,
2792 destination.GetStackIndex());
2793 } else if (source.IsFpuRegister()) {
2794 GetAssembler()->StoreSToOffset(SRegisterFrom(source), sp, destination.GetStackIndex());
2795 } else {
2796 DCHECK(source.IsStackSlot()) << source;
2797 UseScratchRegisterScope temps(GetVIXLAssembler());
2798 vixl32::Register temp = temps.Acquire();
2799 GetAssembler()->LoadFromOffset(kLoadWord, temp, sp, source.GetStackIndex());
2800 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
2801 }
2802 }
2803}
2804
Artem Serovcfbe9132016-10-14 15:58:56 +01002805void CodeGeneratorARMVIXL::MoveConstant(Location location, int32_t value) {
2806 DCHECK(location.IsRegister());
2807 __ Mov(RegisterFrom(location), value);
Scott Wakelingfe885462016-09-22 10:24:38 +01002808}
2809
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002810void CodeGeneratorARMVIXL::MoveLocation(Location dst, Location src, DataType::Type dst_type) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002811 // TODO(VIXL): Maybe refactor to have the 'move' implementation here and use it in
2812 // `ParallelMoveResolverARMVIXL::EmitMove`, as is done in the `arm64` backend.
2813 HParallelMove move(GetGraph()->GetArena());
2814 move.AddMove(src, dst, dst_type, nullptr);
2815 GetMoveResolver()->EmitNativeCode(&move);
Scott Wakelingfe885462016-09-22 10:24:38 +01002816}
2817
Artem Serovcfbe9132016-10-14 15:58:56 +01002818void CodeGeneratorARMVIXL::AddLocationAsTemp(Location location, LocationSummary* locations) {
2819 if (location.IsRegister()) {
2820 locations->AddTemp(location);
2821 } else if (location.IsRegisterPair()) {
2822 locations->AddTemp(LocationFrom(LowRegisterFrom(location)));
2823 locations->AddTemp(LocationFrom(HighRegisterFrom(location)));
2824 } else {
2825 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
2826 }
Scott Wakelingfe885462016-09-22 10:24:38 +01002827}
2828
2829void CodeGeneratorARMVIXL::InvokeRuntime(QuickEntrypointEnum entrypoint,
2830 HInstruction* instruction,
2831 uint32_t dex_pc,
2832 SlowPathCode* slow_path) {
2833 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Alexandre Rames374ddf32016-11-04 10:40:49 +00002834 __ Ldr(lr, MemOperand(tr, GetThreadOffset<kArmPointerSize>(entrypoint).Int32Value()));
2835 // Ensure the pc position is recorded immediately after the `blx` instruction.
2836 // blx in T32 has only 16bit encoding that's why a stricter check for the scope is used.
Artem Serov0fb37192016-12-06 18:13:40 +00002837 ExactAssemblyScope aas(GetVIXLAssembler(),
2838 vixl32::k16BitT32InstructionSizeInBytes,
2839 CodeBufferCheckScope::kExactSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00002840 __ blx(lr);
Scott Wakelingfe885462016-09-22 10:24:38 +01002841 if (EntrypointRequiresStackMap(entrypoint)) {
2842 RecordPcInfo(instruction, dex_pc, slow_path);
2843 }
2844}
2845
2846void CodeGeneratorARMVIXL::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
2847 HInstruction* instruction,
2848 SlowPathCode* slow_path) {
2849 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Alexandre Rames374ddf32016-11-04 10:40:49 +00002850 __ Ldr(lr, MemOperand(tr, entry_point_offset));
Scott Wakelingfe885462016-09-22 10:24:38 +01002851 __ Blx(lr);
2852}
2853
Scott Wakelingfe885462016-09-22 10:24:38 +01002854void InstructionCodeGeneratorARMVIXL::HandleGoto(HInstruction* got, HBasicBlock* successor) {
2855 DCHECK(!successor->IsExitBlock());
2856 HBasicBlock* block = got->GetBlock();
2857 HInstruction* previous = got->GetPrevious();
2858 HLoopInformation* info = block->GetLoopInformation();
2859
2860 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
2861 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
2862 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2863 return;
2864 }
2865 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2866 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
Roland Levillain5daa4952017-07-03 17:23:56 +01002867 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 2);
Scott Wakelingfe885462016-09-22 10:24:38 +01002868 }
2869 if (!codegen_->GoesToNextBlock(block, successor)) {
2870 __ B(codegen_->GetLabelOf(successor));
2871 }
2872}
2873
2874void LocationsBuilderARMVIXL::VisitGoto(HGoto* got) {
2875 got->SetLocations(nullptr);
2876}
2877
2878void InstructionCodeGeneratorARMVIXL::VisitGoto(HGoto* got) {
2879 HandleGoto(got, got->GetSuccessor());
2880}
2881
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002882void LocationsBuilderARMVIXL::VisitTryBoundary(HTryBoundary* try_boundary) {
2883 try_boundary->SetLocations(nullptr);
2884}
2885
2886void InstructionCodeGeneratorARMVIXL::VisitTryBoundary(HTryBoundary* try_boundary) {
2887 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2888 if (!successor->IsExitBlock()) {
2889 HandleGoto(try_boundary, successor);
2890 }
2891}
2892
Scott Wakelingfe885462016-09-22 10:24:38 +01002893void LocationsBuilderARMVIXL::VisitExit(HExit* exit) {
2894 exit->SetLocations(nullptr);
2895}
2896
2897void InstructionCodeGeneratorARMVIXL::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
2898}
2899
Scott Wakelingfe885462016-09-22 10:24:38 +01002900void InstructionCodeGeneratorARMVIXL::GenerateCompareTestAndBranch(HCondition* condition,
Anton Kirilov23b752b2017-07-20 14:40:44 +01002901 vixl32::Label* true_target,
2902 vixl32::Label* false_target,
Anton Kirilovfd522532017-05-10 12:46:57 +01002903 bool is_far_target) {
Anton Kirilov23b752b2017-07-20 14:40:44 +01002904 if (true_target == false_target) {
2905 DCHECK(true_target != nullptr);
2906 __ B(true_target);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002907 return;
2908 }
2909
Anton Kirilov23b752b2017-07-20 14:40:44 +01002910 vixl32::Label* non_fallthrough_target;
2911 bool invert;
2912 bool emit_both_branches;
Scott Wakelingfe885462016-09-22 10:24:38 +01002913
Anton Kirilov23b752b2017-07-20 14:40:44 +01002914 if (true_target == nullptr) {
2915 // The true target is fallthrough.
2916 DCHECK(false_target != nullptr);
2917 non_fallthrough_target = false_target;
2918 invert = true;
2919 emit_both_branches = false;
2920 } else {
2921 non_fallthrough_target = true_target;
2922 invert = false;
2923 // Either the false target is fallthrough, or there is no fallthrough
2924 // and both branches must be emitted.
2925 emit_both_branches = (false_target != nullptr);
Scott Wakelingfe885462016-09-22 10:24:38 +01002926 }
2927
Anton Kirilov23b752b2017-07-20 14:40:44 +01002928 const auto cond = GenerateTest(condition, invert, codegen_);
2929
2930 __ B(cond.first, non_fallthrough_target, is_far_target);
2931
2932 if (emit_both_branches) {
2933 // No target falls through, we need to branch.
2934 __ B(false_target);
Scott Wakelingfe885462016-09-22 10:24:38 +01002935 }
2936}
2937
2938void InstructionCodeGeneratorARMVIXL::GenerateTestAndBranch(HInstruction* instruction,
2939 size_t condition_input_index,
2940 vixl32::Label* true_target,
xueliang.zhongf51bc622016-11-04 09:23:32 +00002941 vixl32::Label* false_target,
2942 bool far_target) {
Scott Wakelingfe885462016-09-22 10:24:38 +01002943 HInstruction* cond = instruction->InputAt(condition_input_index);
2944
2945 if (true_target == nullptr && false_target == nullptr) {
2946 // Nothing to do. The code always falls through.
2947 return;
2948 } else if (cond->IsIntConstant()) {
2949 // Constant condition, statically compared against "true" (integer value 1).
2950 if (cond->AsIntConstant()->IsTrue()) {
2951 if (true_target != nullptr) {
2952 __ B(true_target);
2953 }
2954 } else {
Anton Kirilov644032c2016-12-06 17:51:43 +00002955 DCHECK(cond->AsIntConstant()->IsFalse()) << Int32ConstantFrom(cond);
Scott Wakelingfe885462016-09-22 10:24:38 +01002956 if (false_target != nullptr) {
2957 __ B(false_target);
2958 }
2959 }
2960 return;
2961 }
2962
2963 // The following code generates these patterns:
2964 // (1) true_target == nullptr && false_target != nullptr
2965 // - opposite condition true => branch to false_target
2966 // (2) true_target != nullptr && false_target == nullptr
2967 // - condition true => branch to true_target
2968 // (3) true_target != nullptr && false_target != nullptr
2969 // - condition true => branch to true_target
2970 // - branch to false_target
2971 if (IsBooleanValueOrMaterializedCondition(cond)) {
2972 // Condition has been materialized, compare the output to 0.
2973 if (kIsDebugBuild) {
2974 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
2975 DCHECK(cond_val.IsRegister());
2976 }
2977 if (true_target == nullptr) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00002978 __ CompareAndBranchIfZero(InputRegisterAt(instruction, condition_input_index),
2979 false_target,
2980 far_target);
Scott Wakelingfe885462016-09-22 10:24:38 +01002981 } else {
xueliang.zhongf51bc622016-11-04 09:23:32 +00002982 __ CompareAndBranchIfNonZero(InputRegisterAt(instruction, condition_input_index),
2983 true_target,
2984 far_target);
Scott Wakelingfe885462016-09-22 10:24:38 +01002985 }
2986 } else {
2987 // Condition has not been materialized. Use its inputs as the comparison and
2988 // its condition as the branch condition.
2989 HCondition* condition = cond->AsCondition();
2990
2991 // If this is a long or FP comparison that has been folded into
2992 // the HCondition, generate the comparison directly.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002993 DataType::Type type = condition->InputAt(0)->GetType();
2994 if (type == DataType::Type::kInt64 || DataType::IsFloatingPointType(type)) {
Anton Kirilovfd522532017-05-10 12:46:57 +01002995 GenerateCompareTestAndBranch(condition, true_target, false_target, far_target);
Scott Wakelingfe885462016-09-22 10:24:38 +01002996 return;
2997 }
2998
Donghui Bai426b49c2016-11-08 14:55:38 +08002999 vixl32::Label* non_fallthrough_target;
3000 vixl32::Condition arm_cond = vixl32::Condition::None();
3001 const vixl32::Register left = InputRegisterAt(cond, 0);
3002 const Operand right = InputOperandAt(cond, 1);
3003
Scott Wakelingfe885462016-09-22 10:24:38 +01003004 if (true_target == nullptr) {
Donghui Bai426b49c2016-11-08 14:55:38 +08003005 arm_cond = ARMCondition(condition->GetOppositeCondition());
3006 non_fallthrough_target = false_target;
Scott Wakelingfe885462016-09-22 10:24:38 +01003007 } else {
Donghui Bai426b49c2016-11-08 14:55:38 +08003008 arm_cond = ARMCondition(condition->GetCondition());
3009 non_fallthrough_target = true_target;
3010 }
3011
3012 if (right.IsImmediate() && right.GetImmediate() == 0 && (arm_cond.Is(ne) || arm_cond.Is(eq))) {
3013 if (arm_cond.Is(eq)) {
Anton Kirilovfd522532017-05-10 12:46:57 +01003014 __ CompareAndBranchIfZero(left, non_fallthrough_target, far_target);
Donghui Bai426b49c2016-11-08 14:55:38 +08003015 } else {
3016 DCHECK(arm_cond.Is(ne));
Anton Kirilovfd522532017-05-10 12:46:57 +01003017 __ CompareAndBranchIfNonZero(left, non_fallthrough_target, far_target);
Donghui Bai426b49c2016-11-08 14:55:38 +08003018 }
3019 } else {
3020 __ Cmp(left, right);
Anton Kirilovfd522532017-05-10 12:46:57 +01003021 __ B(arm_cond, non_fallthrough_target, far_target);
Scott Wakelingfe885462016-09-22 10:24:38 +01003022 }
3023 }
3024
3025 // If neither branch falls through (case 3), the conditional branch to `true_target`
3026 // was already emitted (case 2) and we need to emit a jump to `false_target`.
3027 if (true_target != nullptr && false_target != nullptr) {
3028 __ B(false_target);
3029 }
3030}
3031
3032void LocationsBuilderARMVIXL::VisitIf(HIf* if_instr) {
3033 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
3034 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
3035 locations->SetInAt(0, Location::RequiresRegister());
3036 }
3037}
3038
3039void InstructionCodeGeneratorARMVIXL::VisitIf(HIf* if_instr) {
3040 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
3041 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003042 vixl32::Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
3043 nullptr : codegen_->GetLabelOf(true_successor);
3044 vixl32::Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
3045 nullptr : codegen_->GetLabelOf(false_successor);
Scott Wakelingfe885462016-09-22 10:24:38 +01003046 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
3047}
3048
Scott Wakelingc34dba72016-10-03 10:14:44 +01003049void LocationsBuilderARMVIXL::VisitDeoptimize(HDeoptimize* deoptimize) {
3050 LocationSummary* locations = new (GetGraph()->GetArena())
3051 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003052 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3053 RegisterSet caller_saves = RegisterSet::Empty();
3054 caller_saves.Add(LocationFrom(calling_convention.GetRegisterAt(0)));
3055 locations->SetCustomSlowPathCallerSaves(caller_saves);
Scott Wakelingc34dba72016-10-03 10:14:44 +01003056 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
3057 locations->SetInAt(0, Location::RequiresRegister());
3058 }
3059}
3060
3061void InstructionCodeGeneratorARMVIXL::VisitDeoptimize(HDeoptimize* deoptimize) {
3062 SlowPathCodeARMVIXL* slow_path =
3063 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARMVIXL>(deoptimize);
3064 GenerateTestAndBranch(deoptimize,
3065 /* condition_input_index */ 0,
3066 slow_path->GetEntryLabel(),
3067 /* false_target */ nullptr);
3068}
3069
Artem Serovd4cc5b22016-11-04 11:19:09 +00003070void LocationsBuilderARMVIXL::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
3071 LocationSummary* locations = new (GetGraph()->GetArena())
3072 LocationSummary(flag, LocationSummary::kNoCall);
3073 locations->SetOut(Location::RequiresRegister());
3074}
3075
3076void InstructionCodeGeneratorARMVIXL::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
3077 GetAssembler()->LoadFromOffset(kLoadWord,
3078 OutputRegister(flag),
3079 sp,
3080 codegen_->GetStackOffsetOfShouldDeoptimizeFlag());
3081}
3082
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003083void LocationsBuilderARMVIXL::VisitSelect(HSelect* select) {
3084 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003085 const bool is_floating_point = DataType::IsFloatingPointType(select->GetType());
Donghui Bai426b49c2016-11-08 14:55:38 +08003086
3087 if (is_floating_point) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003088 locations->SetInAt(0, Location::RequiresFpuRegister());
Donghui Bai426b49c2016-11-08 14:55:38 +08003089 locations->SetInAt(1, Location::FpuRegisterOrConstant(select->GetTrueValue()));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003090 } else {
3091 locations->SetInAt(0, Location::RequiresRegister());
Donghui Bai426b49c2016-11-08 14:55:38 +08003092 locations->SetInAt(1, Arm8BitEncodableConstantOrRegister(select->GetTrueValue()));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003093 }
Donghui Bai426b49c2016-11-08 14:55:38 +08003094
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003095 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
Donghui Bai426b49c2016-11-08 14:55:38 +08003096 locations->SetInAt(2, Location::RegisterOrConstant(select->GetCondition()));
3097 // The code generator handles overlap with the values, but not with the condition.
3098 locations->SetOut(Location::SameAsFirstInput());
3099 } else if (is_floating_point) {
3100 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3101 } else {
3102 if (!locations->InAt(1).IsConstant()) {
3103 locations->SetInAt(0, Arm8BitEncodableConstantOrRegister(select->GetFalseValue()));
3104 }
3105
3106 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003107 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003108}
3109
3110void InstructionCodeGeneratorARMVIXL::VisitSelect(HSelect* select) {
Donghui Bai426b49c2016-11-08 14:55:38 +08003111 HInstruction* const condition = select->GetCondition();
3112 const LocationSummary* const locations = select->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003113 const DataType::Type type = select->GetType();
Donghui Bai426b49c2016-11-08 14:55:38 +08003114 const Location first = locations->InAt(0);
3115 const Location out = locations->Out();
3116 const Location second = locations->InAt(1);
3117 Location src;
3118
3119 if (condition->IsIntConstant()) {
3120 if (condition->AsIntConstant()->IsFalse()) {
3121 src = first;
3122 } else {
3123 src = second;
3124 }
3125
3126 codegen_->MoveLocation(out, src, type);
3127 return;
3128 }
3129
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003130 if (!DataType::IsFloatingPointType(type)) {
Donghui Bai426b49c2016-11-08 14:55:38 +08003131 bool invert = false;
3132
3133 if (out.Equals(second)) {
3134 src = first;
3135 invert = true;
3136 } else if (out.Equals(first)) {
3137 src = second;
3138 } else if (second.IsConstant()) {
3139 DCHECK(CanEncodeConstantAs8BitImmediate(second.GetConstant()));
3140 src = second;
3141 } else if (first.IsConstant()) {
3142 DCHECK(CanEncodeConstantAs8BitImmediate(first.GetConstant()));
3143 src = first;
3144 invert = true;
3145 } else {
3146 src = second;
3147 }
3148
3149 if (CanGenerateConditionalMove(out, src)) {
3150 if (!out.Equals(first) && !out.Equals(second)) {
3151 codegen_->MoveLocation(out, src.Equals(first) ? second : first, type);
3152 }
3153
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003154 std::pair<vixl32::Condition, vixl32::Condition> cond(eq, ne);
3155
3156 if (IsBooleanValueOrMaterializedCondition(condition)) {
3157 __ Cmp(InputRegisterAt(select, 2), 0);
3158 cond = invert ? std::make_pair(eq, ne) : std::make_pair(ne, eq);
3159 } else {
3160 cond = GenerateTest(condition->AsCondition(), invert, codegen_);
3161 }
3162
Donghui Bai426b49c2016-11-08 14:55:38 +08003163 const size_t instr_count = out.IsRegisterPair() ? 4 : 2;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003164 // We use the scope because of the IT block that follows.
Donghui Bai426b49c2016-11-08 14:55:38 +08003165 ExactAssemblyScope guard(GetVIXLAssembler(),
3166 instr_count * vixl32::k16BitT32InstructionSizeInBytes,
3167 CodeBufferCheckScope::kExactSize);
3168
3169 if (out.IsRegister()) {
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003170 __ it(cond.first);
3171 __ mov(cond.first, RegisterFrom(out), OperandFrom(src, type));
Donghui Bai426b49c2016-11-08 14:55:38 +08003172 } else {
3173 DCHECK(out.IsRegisterPair());
3174
3175 Operand operand_high(0);
3176 Operand operand_low(0);
3177
3178 if (src.IsConstant()) {
3179 const int64_t value = Int64ConstantFrom(src);
3180
3181 operand_high = High32Bits(value);
3182 operand_low = Low32Bits(value);
3183 } else {
3184 DCHECK(src.IsRegisterPair());
3185 operand_high = HighRegisterFrom(src);
3186 operand_low = LowRegisterFrom(src);
3187 }
3188
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003189 __ it(cond.first);
3190 __ mov(cond.first, LowRegisterFrom(out), operand_low);
3191 __ it(cond.first);
3192 __ mov(cond.first, HighRegisterFrom(out), operand_high);
Donghui Bai426b49c2016-11-08 14:55:38 +08003193 }
3194
3195 return;
3196 }
3197 }
3198
3199 vixl32::Label* false_target = nullptr;
3200 vixl32::Label* true_target = nullptr;
3201 vixl32::Label select_end;
3202 vixl32::Label* const target = codegen_->GetFinalLabel(select, &select_end);
3203
3204 if (out.Equals(second)) {
3205 true_target = target;
3206 src = first;
3207 } else {
3208 false_target = target;
3209 src = second;
3210
3211 if (!out.Equals(first)) {
3212 codegen_->MoveLocation(out, first, type);
3213 }
3214 }
3215
3216 GenerateTestAndBranch(select, 2, true_target, false_target, /* far_target */ false);
3217 codegen_->MoveLocation(out, src, type);
3218
3219 if (select_end.IsReferenced()) {
3220 __ Bind(&select_end);
3221 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003222}
3223
Artem Serov551b28f2016-10-18 19:11:30 +01003224void LocationsBuilderARMVIXL::VisitNativeDebugInfo(HNativeDebugInfo* info) {
3225 new (GetGraph()->GetArena()) LocationSummary(info);
3226}
3227
3228void InstructionCodeGeneratorARMVIXL::VisitNativeDebugInfo(HNativeDebugInfo*) {
3229 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
3230}
3231
Scott Wakelingfe885462016-09-22 10:24:38 +01003232void CodeGeneratorARMVIXL::GenerateNop() {
3233 __ Nop();
3234}
3235
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003236// `temp` is an extra temporary register that is used for some conditions;
3237// callers may not specify it, in which case the method will use a scratch
3238// register instead.
3239void CodeGeneratorARMVIXL::GenerateConditionWithZero(IfCondition condition,
3240 vixl32::Register out,
3241 vixl32::Register in,
3242 vixl32::Register temp) {
3243 switch (condition) {
3244 case kCondEQ:
3245 // x <= 0 iff x == 0 when the comparison is unsigned.
3246 case kCondBE:
3247 if (!temp.IsValid() || (out.IsLow() && !out.Is(in))) {
3248 temp = out;
3249 }
3250
3251 // Avoid 32-bit instructions if possible; note that `in` and `temp` must be
3252 // different as well.
3253 if (in.IsLow() && temp.IsLow() && !in.Is(temp)) {
3254 // temp = - in; only 0 sets the carry flag.
3255 __ Rsbs(temp, in, 0);
3256
3257 if (out.Is(in)) {
3258 std::swap(in, temp);
3259 }
3260
3261 // out = - in + in + carry = carry
3262 __ Adc(out, temp, in);
3263 } else {
3264 // If `in` is 0, then it has 32 leading zeros, and less than that otherwise.
3265 __ Clz(out, in);
3266 // Any number less than 32 logically shifted right by 5 bits results in 0;
3267 // the same operation on 32 yields 1.
3268 __ Lsr(out, out, 5);
3269 }
3270
3271 break;
3272 case kCondNE:
3273 // x > 0 iff x != 0 when the comparison is unsigned.
3274 case kCondA: {
3275 UseScratchRegisterScope temps(GetVIXLAssembler());
3276
3277 if (out.Is(in)) {
3278 if (!temp.IsValid() || in.Is(temp)) {
3279 temp = temps.Acquire();
3280 }
3281 } else if (!temp.IsValid() || !temp.IsLow()) {
3282 temp = out;
3283 }
3284
3285 // temp = in - 1; only 0 does not set the carry flag.
3286 __ Subs(temp, in, 1);
3287 // out = in + ~temp + carry = in + (-(in - 1) - 1) + carry = in - in + 1 - 1 + carry = carry
3288 __ Sbc(out, in, temp);
3289 break;
3290 }
3291 case kCondGE:
3292 __ Mvn(out, in);
3293 in = out;
3294 FALLTHROUGH_INTENDED;
3295 case kCondLT:
3296 // We only care about the sign bit.
3297 __ Lsr(out, in, 31);
3298 break;
3299 case kCondAE:
3300 // Trivially true.
3301 __ Mov(out, 1);
3302 break;
3303 case kCondB:
3304 // Trivially false.
3305 __ Mov(out, 0);
3306 break;
3307 default:
3308 LOG(FATAL) << "Unexpected condition " << condition;
3309 UNREACHABLE();
3310 }
3311}
3312
Scott Wakelingfe885462016-09-22 10:24:38 +01003313void LocationsBuilderARMVIXL::HandleCondition(HCondition* cond) {
3314 LocationSummary* locations =
3315 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
3316 // Handle the long/FP comparisons made in instruction simplification.
3317 switch (cond->InputAt(0)->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003318 case DataType::Type::kInt64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003319 locations->SetInAt(0, Location::RequiresRegister());
3320 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
3321 if (!cond->IsEmittedAtUseSite()) {
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003322 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Scott Wakelingfe885462016-09-22 10:24:38 +01003323 }
3324 break;
3325
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003326 case DataType::Type::kFloat32:
3327 case DataType::Type::kFloat64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003328 locations->SetInAt(0, Location::RequiresFpuRegister());
Artem Serov657022c2016-11-23 14:19:38 +00003329 locations->SetInAt(1, ArithmeticZeroOrFpuRegister(cond->InputAt(1)));
Scott Wakelingfe885462016-09-22 10:24:38 +01003330 if (!cond->IsEmittedAtUseSite()) {
3331 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3332 }
3333 break;
3334
3335 default:
3336 locations->SetInAt(0, Location::RequiresRegister());
3337 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
3338 if (!cond->IsEmittedAtUseSite()) {
3339 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3340 }
3341 }
3342}
3343
3344void InstructionCodeGeneratorARMVIXL::HandleCondition(HCondition* cond) {
3345 if (cond->IsEmittedAtUseSite()) {
3346 return;
3347 }
3348
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003349 const DataType::Type type = cond->GetLeft()->GetType();
Scott Wakelingfe885462016-09-22 10:24:38 +01003350
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003351 if (DataType::IsFloatingPointType(type)) {
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003352 GenerateConditionGeneric(cond, codegen_);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003353 return;
Scott Wakelingfe885462016-09-22 10:24:38 +01003354 }
3355
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003356 DCHECK(DataType::IsIntegralType(type) || type == DataType::Type::kReference) << type;
Scott Wakelingfe885462016-09-22 10:24:38 +01003357
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003358 const IfCondition condition = cond->GetCondition();
Scott Wakelingfe885462016-09-22 10:24:38 +01003359
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003360 // A condition with only one boolean input, or two boolean inputs without being equality or
3361 // inequality results from transformations done by the instruction simplifier, and is handled
3362 // as a regular condition with integral inputs.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003363 if (type == DataType::Type::kBool &&
3364 cond->GetRight()->GetType() == DataType::Type::kBool &&
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003365 (condition == kCondEQ || condition == kCondNE)) {
3366 vixl32::Register left = InputRegisterAt(cond, 0);
3367 const vixl32::Register out = OutputRegister(cond);
3368 const Location right_loc = cond->GetLocations()->InAt(1);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003369
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003370 // The constant case is handled by the instruction simplifier.
3371 DCHECK(!right_loc.IsConstant());
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003372
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003373 vixl32::Register right = RegisterFrom(right_loc);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003374
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003375 // Avoid 32-bit instructions if possible.
3376 if (out.Is(right)) {
3377 std::swap(left, right);
3378 }
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003379
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003380 __ Eor(out, left, right);
3381
3382 if (condition == kCondEQ) {
3383 __ Eor(out, out, 1);
3384 }
3385
3386 return;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003387 }
Anton Kirilov6f644202017-02-27 18:29:45 +00003388
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003389 GenerateConditionIntegralOrNonPrimitive(cond, codegen_);
Scott Wakelingfe885462016-09-22 10:24:38 +01003390}
3391
3392void LocationsBuilderARMVIXL::VisitEqual(HEqual* comp) {
3393 HandleCondition(comp);
3394}
3395
3396void InstructionCodeGeneratorARMVIXL::VisitEqual(HEqual* comp) {
3397 HandleCondition(comp);
3398}
3399
3400void LocationsBuilderARMVIXL::VisitNotEqual(HNotEqual* comp) {
3401 HandleCondition(comp);
3402}
3403
3404void InstructionCodeGeneratorARMVIXL::VisitNotEqual(HNotEqual* comp) {
3405 HandleCondition(comp);
3406}
3407
3408void LocationsBuilderARMVIXL::VisitLessThan(HLessThan* comp) {
3409 HandleCondition(comp);
3410}
3411
3412void InstructionCodeGeneratorARMVIXL::VisitLessThan(HLessThan* comp) {
3413 HandleCondition(comp);
3414}
3415
3416void LocationsBuilderARMVIXL::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
3417 HandleCondition(comp);
3418}
3419
3420void InstructionCodeGeneratorARMVIXL::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
3421 HandleCondition(comp);
3422}
3423
3424void LocationsBuilderARMVIXL::VisitGreaterThan(HGreaterThan* comp) {
3425 HandleCondition(comp);
3426}
3427
3428void InstructionCodeGeneratorARMVIXL::VisitGreaterThan(HGreaterThan* comp) {
3429 HandleCondition(comp);
3430}
3431
3432void LocationsBuilderARMVIXL::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
3433 HandleCondition(comp);
3434}
3435
3436void InstructionCodeGeneratorARMVIXL::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
3437 HandleCondition(comp);
3438}
3439
3440void LocationsBuilderARMVIXL::VisitBelow(HBelow* comp) {
3441 HandleCondition(comp);
3442}
3443
3444void InstructionCodeGeneratorARMVIXL::VisitBelow(HBelow* comp) {
3445 HandleCondition(comp);
3446}
3447
3448void LocationsBuilderARMVIXL::VisitBelowOrEqual(HBelowOrEqual* comp) {
3449 HandleCondition(comp);
3450}
3451
3452void InstructionCodeGeneratorARMVIXL::VisitBelowOrEqual(HBelowOrEqual* comp) {
3453 HandleCondition(comp);
3454}
3455
3456void LocationsBuilderARMVIXL::VisitAbove(HAbove* comp) {
3457 HandleCondition(comp);
3458}
3459
3460void InstructionCodeGeneratorARMVIXL::VisitAbove(HAbove* comp) {
3461 HandleCondition(comp);
3462}
3463
3464void LocationsBuilderARMVIXL::VisitAboveOrEqual(HAboveOrEqual* comp) {
3465 HandleCondition(comp);
3466}
3467
3468void InstructionCodeGeneratorARMVIXL::VisitAboveOrEqual(HAboveOrEqual* comp) {
3469 HandleCondition(comp);
3470}
3471
3472void LocationsBuilderARMVIXL::VisitIntConstant(HIntConstant* constant) {
3473 LocationSummary* locations =
3474 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3475 locations->SetOut(Location::ConstantLocation(constant));
3476}
3477
3478void InstructionCodeGeneratorARMVIXL::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
3479 // Will be generated at use site.
3480}
3481
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003482void LocationsBuilderARMVIXL::VisitNullConstant(HNullConstant* constant) {
3483 LocationSummary* locations =
3484 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3485 locations->SetOut(Location::ConstantLocation(constant));
3486}
3487
3488void InstructionCodeGeneratorARMVIXL::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
3489 // Will be generated at use site.
3490}
3491
Scott Wakelingfe885462016-09-22 10:24:38 +01003492void LocationsBuilderARMVIXL::VisitLongConstant(HLongConstant* constant) {
3493 LocationSummary* locations =
3494 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3495 locations->SetOut(Location::ConstantLocation(constant));
3496}
3497
3498void InstructionCodeGeneratorARMVIXL::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
3499 // Will be generated at use site.
3500}
3501
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01003502void LocationsBuilderARMVIXL::VisitFloatConstant(HFloatConstant* constant) {
3503 LocationSummary* locations =
3504 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3505 locations->SetOut(Location::ConstantLocation(constant));
3506}
3507
Scott Wakelingc34dba72016-10-03 10:14:44 +01003508void InstructionCodeGeneratorARMVIXL::VisitFloatConstant(
3509 HFloatConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01003510 // Will be generated at use site.
3511}
3512
3513void LocationsBuilderARMVIXL::VisitDoubleConstant(HDoubleConstant* constant) {
3514 LocationSummary* locations =
3515 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3516 locations->SetOut(Location::ConstantLocation(constant));
3517}
3518
Scott Wakelingc34dba72016-10-03 10:14:44 +01003519void InstructionCodeGeneratorARMVIXL::VisitDoubleConstant(
3520 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01003521 // Will be generated at use site.
3522}
3523
Igor Murashkind01745e2017-04-05 16:40:31 -07003524void LocationsBuilderARMVIXL::VisitConstructorFence(HConstructorFence* constructor_fence) {
3525 constructor_fence->SetLocations(nullptr);
3526}
3527
3528void InstructionCodeGeneratorARMVIXL::VisitConstructorFence(
3529 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
3530 codegen_->GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
3531}
3532
Scott Wakelingfe885462016-09-22 10:24:38 +01003533void LocationsBuilderARMVIXL::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3534 memory_barrier->SetLocations(nullptr);
3535}
3536
3537void InstructionCodeGeneratorARMVIXL::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3538 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
3539}
3540
3541void LocationsBuilderARMVIXL::VisitReturnVoid(HReturnVoid* ret) {
3542 ret->SetLocations(nullptr);
3543}
3544
3545void InstructionCodeGeneratorARMVIXL::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
3546 codegen_->GenerateFrameExit();
3547}
3548
3549void LocationsBuilderARMVIXL::VisitReturn(HReturn* ret) {
3550 LocationSummary* locations =
3551 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
3552 locations->SetInAt(0, parameter_visitor_.GetReturnLocation(ret->InputAt(0)->GetType()));
3553}
3554
3555void InstructionCodeGeneratorARMVIXL::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
3556 codegen_->GenerateFrameExit();
3557}
3558
Artem Serovcfbe9132016-10-14 15:58:56 +01003559void LocationsBuilderARMVIXL::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3560 // The trampoline uses the same calling convention as dex calling conventions,
3561 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
3562 // the method_idx.
3563 HandleInvoke(invoke);
3564}
3565
3566void InstructionCodeGeneratorARMVIXL::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3567 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
Roland Levillain5daa4952017-07-03 17:23:56 +01003568 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 3);
Artem Serovcfbe9132016-10-14 15:58:56 +01003569}
3570
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003571void LocationsBuilderARMVIXL::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
3572 // Explicit clinit checks triggered by static invokes must have been pruned by
3573 // art::PrepareForRegisterAllocation.
3574 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
3575
Anton Kirilov5ec62182016-10-13 20:16:02 +01003576 IntrinsicLocationsBuilderARMVIXL intrinsic(codegen_);
3577 if (intrinsic.TryDispatch(invoke)) {
Anton Kirilov5ec62182016-10-13 20:16:02 +01003578 return;
3579 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003580
3581 HandleInvoke(invoke);
3582}
3583
Anton Kirilov5ec62182016-10-13 20:16:02 +01003584static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARMVIXL* codegen) {
3585 if (invoke->GetLocations()->Intrinsified()) {
3586 IntrinsicCodeGeneratorARMVIXL intrinsic(codegen);
3587 intrinsic.Dispatch(invoke);
3588 return true;
3589 }
3590 return false;
3591}
3592
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003593void InstructionCodeGeneratorARMVIXL::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
3594 // Explicit clinit checks triggered by static invokes must have been pruned by
3595 // art::PrepareForRegisterAllocation.
3596 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
3597
Anton Kirilov5ec62182016-10-13 20:16:02 +01003598 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
Roland Levillain5daa4952017-07-03 17:23:56 +01003599 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 4);
Anton Kirilov5ec62182016-10-13 20:16:02 +01003600 return;
3601 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003602
3603 LocationSummary* locations = invoke->GetLocations();
Artem Serovd4cc5b22016-11-04 11:19:09 +00003604 codegen_->GenerateStaticOrDirectCall(
3605 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Roland Levillain5daa4952017-07-03 17:23:56 +01003606
3607 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 5);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003608}
3609
3610void LocationsBuilderARMVIXL::HandleInvoke(HInvoke* invoke) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00003611 InvokeDexCallingConventionVisitorARMVIXL calling_convention_visitor;
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003612 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
3613}
3614
3615void LocationsBuilderARMVIXL::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Anton Kirilov5ec62182016-10-13 20:16:02 +01003616 IntrinsicLocationsBuilderARMVIXL intrinsic(codegen_);
3617 if (intrinsic.TryDispatch(invoke)) {
3618 return;
3619 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003620
3621 HandleInvoke(invoke);
3622}
3623
3624void InstructionCodeGeneratorARMVIXL::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Anton Kirilov5ec62182016-10-13 20:16:02 +01003625 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
Roland Levillain5daa4952017-07-03 17:23:56 +01003626 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 6);
Anton Kirilov5ec62182016-10-13 20:16:02 +01003627 return;
3628 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003629
3630 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexandre Rames374ddf32016-11-04 10:40:49 +00003631 DCHECK(!codegen_->IsLeafMethod());
Roland Levillain5daa4952017-07-03 17:23:56 +01003632
3633 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 7);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003634}
3635
Artem Serovcfbe9132016-10-14 15:58:56 +01003636void LocationsBuilderARMVIXL::VisitInvokeInterface(HInvokeInterface* invoke) {
3637 HandleInvoke(invoke);
3638 // Add the hidden argument.
3639 invoke->GetLocations()->AddTemp(LocationFrom(r12));
3640}
3641
3642void InstructionCodeGeneratorARMVIXL::VisitInvokeInterface(HInvokeInterface* invoke) {
3643 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
3644 LocationSummary* locations = invoke->GetLocations();
3645 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
3646 vixl32::Register hidden_reg = RegisterFrom(locations->GetTemp(1));
3647 Location receiver = locations->InAt(0);
3648 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3649
3650 DCHECK(!receiver.IsStackSlot());
3651
Alexandre Rames374ddf32016-11-04 10:40:49 +00003652 // Ensure the pc position is recorded immediately after the `ldr` instruction.
3653 {
Artem Serov0fb37192016-12-06 18:13:40 +00003654 ExactAssemblyScope aas(GetVIXLAssembler(),
3655 vixl32::kMaxInstructionSizeInBytes,
3656 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00003657 // /* HeapReference<Class> */ temp = receiver->klass_
3658 __ ldr(temp, MemOperand(RegisterFrom(receiver), class_offset));
3659 codegen_->MaybeRecordImplicitNullCheck(invoke);
3660 }
Artem Serovcfbe9132016-10-14 15:58:56 +01003661 // Instead of simply (possibly) unpoisoning `temp` here, we should
3662 // emit a read barrier for the previous class reference load.
3663 // However this is not required in practice, as this is an
3664 // intermediate/temporary reference and because the current
3665 // concurrent copying collector keeps the from-space memory
3666 // intact/accessible until the end of the marking phase (the
3667 // concurrent copying collector may not in the future).
3668 GetAssembler()->MaybeUnpoisonHeapReference(temp);
3669 GetAssembler()->LoadFromOffset(kLoadWord,
3670 temp,
3671 temp,
3672 mirror::Class::ImtPtrOffset(kArmPointerSize).Uint32Value());
3673 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
3674 invoke->GetImtIndex(), kArmPointerSize));
3675 // temp = temp->GetImtEntryAt(method_offset);
3676 GetAssembler()->LoadFromOffset(kLoadWord, temp, temp, method_offset);
3677 uint32_t entry_point =
3678 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize).Int32Value();
3679 // LR = temp->GetEntryPoint();
3680 GetAssembler()->LoadFromOffset(kLoadWord, lr, temp, entry_point);
3681
3682 // Set the hidden (in r12) argument. It is done here, right before a BLX to prevent other
3683 // instruction from clobbering it as they might use r12 as a scratch register.
3684 DCHECK(hidden_reg.Is(r12));
Scott Wakelingb77051e2016-11-21 19:46:00 +00003685
3686 {
3687 // The VIXL macro assembler may clobber any of the scratch registers that are available to it,
3688 // so it checks if the application is using them (by passing them to the macro assembler
3689 // methods). The following application of UseScratchRegisterScope corrects VIXL's notion of
3690 // what is available, and is the opposite of the standard usage: Instead of requesting a
3691 // temporary location, it imposes an external constraint (i.e. a specific register is reserved
3692 // for the hidden argument). Note that this works even if VIXL needs a scratch register itself
3693 // (to materialize the constant), since the destination register becomes available for such use
3694 // internally for the duration of the macro instruction.
3695 UseScratchRegisterScope temps(GetVIXLAssembler());
3696 temps.Exclude(hidden_reg);
3697 __ Mov(hidden_reg, invoke->GetDexMethodIndex());
3698 }
Artem Serovcfbe9132016-10-14 15:58:56 +01003699 {
Alexandre Rames374ddf32016-11-04 10:40:49 +00003700 // Ensure the pc position is recorded immediately after the `blx` instruction.
3701 // 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 +00003702 ExactAssemblyScope aas(GetVIXLAssembler(),
Alexandre Rames374ddf32016-11-04 10:40:49 +00003703 vixl32::k16BitT32InstructionSizeInBytes,
3704 CodeBufferCheckScope::kExactSize);
Artem Serovcfbe9132016-10-14 15:58:56 +01003705 // LR();
3706 __ blx(lr);
Artem Serovcfbe9132016-10-14 15:58:56 +01003707 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Alexandre Rames374ddf32016-11-04 10:40:49 +00003708 DCHECK(!codegen_->IsLeafMethod());
Artem Serovcfbe9132016-10-14 15:58:56 +01003709 }
Roland Levillain5daa4952017-07-03 17:23:56 +01003710
3711 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 8);
Artem Serovcfbe9132016-10-14 15:58:56 +01003712}
3713
Orion Hodsonac141392017-01-13 11:53:47 +00003714void LocationsBuilderARMVIXL::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
3715 HandleInvoke(invoke);
3716}
3717
3718void InstructionCodeGeneratorARMVIXL::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
3719 codegen_->GenerateInvokePolymorphicCall(invoke);
Roland Levillain5daa4952017-07-03 17:23:56 +01003720 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 9);
Orion Hodsonac141392017-01-13 11:53:47 +00003721}
3722
Artem Serov02109dd2016-09-23 17:17:54 +01003723void LocationsBuilderARMVIXL::VisitNeg(HNeg* neg) {
3724 LocationSummary* locations =
3725 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
3726 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003727 case DataType::Type::kInt32: {
Artem Serov02109dd2016-09-23 17:17:54 +01003728 locations->SetInAt(0, Location::RequiresRegister());
3729 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3730 break;
3731 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003732 case DataType::Type::kInt64: {
Artem Serov02109dd2016-09-23 17:17:54 +01003733 locations->SetInAt(0, Location::RequiresRegister());
3734 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3735 break;
3736 }
3737
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003738 case DataType::Type::kFloat32:
3739 case DataType::Type::kFloat64:
Artem Serov02109dd2016-09-23 17:17:54 +01003740 locations->SetInAt(0, Location::RequiresFpuRegister());
3741 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3742 break;
3743
3744 default:
3745 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
3746 }
3747}
3748
3749void InstructionCodeGeneratorARMVIXL::VisitNeg(HNeg* neg) {
3750 LocationSummary* locations = neg->GetLocations();
3751 Location out = locations->Out();
3752 Location in = locations->InAt(0);
3753 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003754 case DataType::Type::kInt32:
Artem Serov02109dd2016-09-23 17:17:54 +01003755 __ Rsb(OutputRegister(neg), InputRegisterAt(neg, 0), 0);
3756 break;
3757
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003758 case DataType::Type::kInt64:
Artem Serov02109dd2016-09-23 17:17:54 +01003759 // out.lo = 0 - in.lo (and update the carry/borrow (C) flag)
3760 __ Rsbs(LowRegisterFrom(out), LowRegisterFrom(in), 0);
3761 // We cannot emit an RSC (Reverse Subtract with Carry)
3762 // instruction here, as it does not exist in the Thumb-2
3763 // instruction set. We use the following approach
3764 // using SBC and SUB instead.
3765 //
3766 // out.hi = -C
3767 __ Sbc(HighRegisterFrom(out), HighRegisterFrom(out), HighRegisterFrom(out));
3768 // out.hi = out.hi - in.hi
3769 __ Sub(HighRegisterFrom(out), HighRegisterFrom(out), HighRegisterFrom(in));
3770 break;
3771
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003772 case DataType::Type::kFloat32:
3773 case DataType::Type::kFloat64:
Anton Kirilov644032c2016-12-06 17:51:43 +00003774 __ Vneg(OutputVRegister(neg), InputVRegister(neg));
Artem Serov02109dd2016-09-23 17:17:54 +01003775 break;
3776
3777 default:
3778 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
3779 }
3780}
3781
Scott Wakelingfe885462016-09-22 10:24:38 +01003782void LocationsBuilderARMVIXL::VisitTypeConversion(HTypeConversion* conversion) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003783 DataType::Type result_type = conversion->GetResultType();
3784 DataType::Type input_type = conversion->GetInputType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003785 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
3786 << input_type << " -> " << result_type;
Scott Wakelingfe885462016-09-22 10:24:38 +01003787
3788 // The float-to-long, double-to-long and long-to-float type conversions
3789 // rely on a call to the runtime.
3790 LocationSummary::CallKind call_kind =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003791 (((input_type == DataType::Type::kFloat32 || input_type == DataType::Type::kFloat64)
3792 && result_type == DataType::Type::kInt64)
3793 || (input_type == DataType::Type::kInt64 && result_type == DataType::Type::kFloat32))
Scott Wakelingfe885462016-09-22 10:24:38 +01003794 ? LocationSummary::kCallOnMainOnly
3795 : LocationSummary::kNoCall;
3796 LocationSummary* locations =
3797 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
3798
Scott Wakelingfe885462016-09-22 10:24:38 +01003799 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003800 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003801 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003802 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003803 case DataType::Type::kInt16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003804 DCHECK(DataType::IsIntegralType(input_type)) << input_type;
3805 locations->SetInAt(0, Location::RequiresRegister());
3806 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Scott Wakelingfe885462016-09-22 10:24:38 +01003807 break;
3808
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003809 case DataType::Type::kInt32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003810 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003811 case DataType::Type::kInt64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003812 locations->SetInAt(0, Location::Any());
3813 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3814 break;
3815
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003816 case DataType::Type::kFloat32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003817 locations->SetInAt(0, Location::RequiresFpuRegister());
3818 locations->SetOut(Location::RequiresRegister());
3819 locations->AddTemp(Location::RequiresFpuRegister());
3820 break;
3821
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003822 case DataType::Type::kFloat64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003823 locations->SetInAt(0, Location::RequiresFpuRegister());
3824 locations->SetOut(Location::RequiresRegister());
3825 locations->AddTemp(Location::RequiresFpuRegister());
3826 break;
3827
3828 default:
3829 LOG(FATAL) << "Unexpected type conversion from " << input_type
3830 << " to " << result_type;
3831 }
3832 break;
3833
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003834 case DataType::Type::kInt64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003835 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003836 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003837 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003838 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003839 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003840 case DataType::Type::kInt16:
3841 case DataType::Type::kInt32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003842 locations->SetInAt(0, Location::RequiresRegister());
3843 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3844 break;
3845
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003846 case DataType::Type::kFloat32: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003847 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3848 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
3849 locations->SetOut(LocationFrom(r0, r1));
Scott Wakelingfe885462016-09-22 10:24:38 +01003850 break;
3851 }
3852
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003853 case DataType::Type::kFloat64: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003854 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3855 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0),
3856 calling_convention.GetFpuRegisterAt(1)));
3857 locations->SetOut(LocationFrom(r0, r1));
Scott Wakelingfe885462016-09-22 10:24:38 +01003858 break;
3859 }
3860
3861 default:
3862 LOG(FATAL) << "Unexpected type conversion from " << input_type
3863 << " to " << result_type;
3864 }
3865 break;
3866
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003867 case DataType::Type::kFloat32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003868 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003869 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003870 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003871 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003872 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003873 case DataType::Type::kInt16:
3874 case DataType::Type::kInt32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003875 locations->SetInAt(0, Location::RequiresRegister());
3876 locations->SetOut(Location::RequiresFpuRegister());
3877 break;
3878
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003879 case DataType::Type::kInt64: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003880 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3881 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0),
3882 calling_convention.GetRegisterAt(1)));
3883 locations->SetOut(LocationFrom(calling_convention.GetFpuRegisterAt(0)));
Scott Wakelingfe885462016-09-22 10:24:38 +01003884 break;
3885 }
3886
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003887 case DataType::Type::kFloat64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003888 locations->SetInAt(0, Location::RequiresFpuRegister());
3889 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3890 break;
3891
3892 default:
3893 LOG(FATAL) << "Unexpected type conversion from " << input_type
3894 << " to " << result_type;
3895 };
3896 break;
3897
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003898 case DataType::Type::kFloat64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003899 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003900 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003901 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003902 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003903 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003904 case DataType::Type::kInt16:
3905 case DataType::Type::kInt32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003906 locations->SetInAt(0, Location::RequiresRegister());
3907 locations->SetOut(Location::RequiresFpuRegister());
3908 break;
3909
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003910 case DataType::Type::kInt64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003911 locations->SetInAt(0, Location::RequiresRegister());
3912 locations->SetOut(Location::RequiresFpuRegister());
3913 locations->AddTemp(Location::RequiresFpuRegister());
3914 locations->AddTemp(Location::RequiresFpuRegister());
3915 break;
3916
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003917 case DataType::Type::kFloat32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003918 locations->SetInAt(0, Location::RequiresFpuRegister());
3919 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3920 break;
3921
3922 default:
3923 LOG(FATAL) << "Unexpected type conversion from " << input_type
3924 << " to " << result_type;
3925 };
3926 break;
3927
3928 default:
3929 LOG(FATAL) << "Unexpected type conversion from " << input_type
3930 << " to " << result_type;
3931 }
3932}
3933
3934void InstructionCodeGeneratorARMVIXL::VisitTypeConversion(HTypeConversion* conversion) {
3935 LocationSummary* locations = conversion->GetLocations();
3936 Location out = locations->Out();
3937 Location in = locations->InAt(0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003938 DataType::Type result_type = conversion->GetResultType();
3939 DataType::Type input_type = conversion->GetInputType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003940 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
3941 << input_type << " -> " << result_type;
Scott Wakelingfe885462016-09-22 10:24:38 +01003942 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003943 case DataType::Type::kUint8:
Scott Wakelingfe885462016-09-22 10:24:38 +01003944 switch (input_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003945 case DataType::Type::kInt8:
3946 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003947 case DataType::Type::kInt16:
3948 case DataType::Type::kInt32:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003949 __ Ubfx(OutputRegister(conversion), InputRegisterAt(conversion, 0), 0, 8);
3950 break;
3951 case DataType::Type::kInt64:
3952 __ Ubfx(OutputRegister(conversion), LowRegisterFrom(in), 0, 8);
3953 break;
3954
3955 default:
3956 LOG(FATAL) << "Unexpected type conversion from " << input_type
3957 << " to " << result_type;
3958 }
3959 break;
3960
3961 case DataType::Type::kInt8:
3962 switch (input_type) {
3963 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003964 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003965 case DataType::Type::kInt16:
3966 case DataType::Type::kInt32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003967 __ Sbfx(OutputRegister(conversion), InputRegisterAt(conversion, 0), 0, 8);
3968 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003969 case DataType::Type::kInt64:
3970 __ Sbfx(OutputRegister(conversion), LowRegisterFrom(in), 0, 8);
3971 break;
3972
3973 default:
3974 LOG(FATAL) << "Unexpected type conversion from " << input_type
3975 << " to " << result_type;
3976 }
3977 break;
3978
3979 case DataType::Type::kUint16:
3980 switch (input_type) {
3981 case DataType::Type::kInt8:
3982 case DataType::Type::kInt16:
3983 case DataType::Type::kInt32:
3984 __ Ubfx(OutputRegister(conversion), InputRegisterAt(conversion, 0), 0, 16);
3985 break;
3986 case DataType::Type::kInt64:
3987 __ Ubfx(OutputRegister(conversion), LowRegisterFrom(in), 0, 16);
3988 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01003989
3990 default:
3991 LOG(FATAL) << "Unexpected type conversion from " << input_type
3992 << " to " << result_type;
3993 }
3994 break;
3995
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003996 case DataType::Type::kInt16:
Scott Wakelingfe885462016-09-22 10:24:38 +01003997 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003998 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003999 case DataType::Type::kInt32:
Scott Wakelingfe885462016-09-22 10:24:38 +01004000 __ Sbfx(OutputRegister(conversion), InputRegisterAt(conversion, 0), 0, 16);
4001 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004002 case DataType::Type::kInt64:
4003 __ Sbfx(OutputRegister(conversion), LowRegisterFrom(in), 0, 16);
4004 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01004005
4006 default:
4007 LOG(FATAL) << "Unexpected type conversion from " << input_type
4008 << " to " << result_type;
4009 }
4010 break;
4011
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004012 case DataType::Type::kInt32:
Scott Wakelingfe885462016-09-22 10:24:38 +01004013 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004014 case DataType::Type::kInt64:
Scott Wakelingfe885462016-09-22 10:24:38 +01004015 DCHECK(out.IsRegister());
4016 if (in.IsRegisterPair()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004017 __ Mov(OutputRegister(conversion), LowRegisterFrom(in));
Scott Wakelingfe885462016-09-22 10:24:38 +01004018 } else if (in.IsDoubleStackSlot()) {
4019 GetAssembler()->LoadFromOffset(kLoadWord,
4020 OutputRegister(conversion),
4021 sp,
4022 in.GetStackIndex());
4023 } else {
4024 DCHECK(in.IsConstant());
4025 DCHECK(in.GetConstant()->IsLongConstant());
Vladimir Markoba1a48e2017-04-13 11:50:14 +01004026 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
4027 __ Mov(OutputRegister(conversion), static_cast<int32_t>(value));
Scott Wakelingfe885462016-09-22 10:24:38 +01004028 }
4029 break;
4030
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004031 case DataType::Type::kFloat32: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004032 vixl32::SRegister temp = LowSRegisterFrom(locations->GetTemp(0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01004033 __ Vcvt(S32, F32, temp, InputSRegisterAt(conversion, 0));
Scott Wakelingfe885462016-09-22 10:24:38 +01004034 __ Vmov(OutputRegister(conversion), temp);
4035 break;
4036 }
4037
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004038 case DataType::Type::kFloat64: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004039 vixl32::SRegister temp_s = LowSRegisterFrom(locations->GetTemp(0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01004040 __ Vcvt(S32, F64, temp_s, DRegisterFrom(in));
Scott Wakelingfe885462016-09-22 10:24:38 +01004041 __ Vmov(OutputRegister(conversion), temp_s);
4042 break;
4043 }
4044
4045 default:
4046 LOG(FATAL) << "Unexpected type conversion from " << input_type
4047 << " to " << result_type;
4048 }
4049 break;
4050
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004051 case DataType::Type::kInt64:
Scott Wakelingfe885462016-09-22 10:24:38 +01004052 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004053 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004054 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004055 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004056 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004057 case DataType::Type::kInt16:
4058 case DataType::Type::kInt32:
Scott Wakelingfe885462016-09-22 10:24:38 +01004059 DCHECK(out.IsRegisterPair());
4060 DCHECK(in.IsRegister());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004061 __ Mov(LowRegisterFrom(out), InputRegisterAt(conversion, 0));
Scott Wakelingfe885462016-09-22 10:24:38 +01004062 // Sign extension.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004063 __ Asr(HighRegisterFrom(out), LowRegisterFrom(out), 31);
Scott Wakelingfe885462016-09-22 10:24:38 +01004064 break;
4065
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004066 case DataType::Type::kFloat32:
Scott Wakelingfe885462016-09-22 10:24:38 +01004067 codegen_->InvokeRuntime(kQuickF2l, conversion, conversion->GetDexPc());
4068 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
4069 break;
4070
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004071 case DataType::Type::kFloat64:
Scott Wakelingfe885462016-09-22 10:24:38 +01004072 codegen_->InvokeRuntime(kQuickD2l, conversion, conversion->GetDexPc());
4073 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
4074 break;
4075
4076 default:
4077 LOG(FATAL) << "Unexpected type conversion from " << input_type
4078 << " to " << result_type;
4079 }
4080 break;
4081
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004082 case DataType::Type::kFloat32:
Scott Wakelingfe885462016-09-22 10:24:38 +01004083 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004084 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004085 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004086 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004087 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004088 case DataType::Type::kInt16:
4089 case DataType::Type::kInt32:
Scott Wakelingfe885462016-09-22 10:24:38 +01004090 __ Vmov(OutputSRegister(conversion), InputRegisterAt(conversion, 0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01004091 __ Vcvt(F32, S32, OutputSRegister(conversion), OutputSRegister(conversion));
Scott Wakelingfe885462016-09-22 10:24:38 +01004092 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01004093
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004094 case DataType::Type::kInt64:
Scott Wakelingfe885462016-09-22 10:24:38 +01004095 codegen_->InvokeRuntime(kQuickL2f, conversion, conversion->GetDexPc());
4096 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
4097 break;
4098
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004099 case DataType::Type::kFloat64:
Scott Wakelingc34dba72016-10-03 10:14:44 +01004100 __ Vcvt(F32, F64, OutputSRegister(conversion), DRegisterFrom(in));
Scott Wakelingfe885462016-09-22 10:24:38 +01004101 break;
4102
4103 default:
4104 LOG(FATAL) << "Unexpected type conversion from " << input_type
4105 << " to " << result_type;
4106 };
4107 break;
4108
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004109 case DataType::Type::kFloat64:
Scott Wakelingfe885462016-09-22 10:24:38 +01004110 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004111 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004112 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004113 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004114 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004115 case DataType::Type::kInt16:
4116 case DataType::Type::kInt32:
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004117 __ Vmov(LowSRegisterFrom(out), InputRegisterAt(conversion, 0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01004118 __ Vcvt(F64, S32, DRegisterFrom(out), LowSRegisterFrom(out));
Scott Wakelingfe885462016-09-22 10:24:38 +01004119 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01004120
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004121 case DataType::Type::kInt64: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004122 vixl32::Register low = LowRegisterFrom(in);
4123 vixl32::Register high = HighRegisterFrom(in);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004124 vixl32::SRegister out_s = LowSRegisterFrom(out);
Scott Wakelingc34dba72016-10-03 10:14:44 +01004125 vixl32::DRegister out_d = DRegisterFrom(out);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004126 vixl32::SRegister temp_s = LowSRegisterFrom(locations->GetTemp(0));
Scott Wakelingc34dba72016-10-03 10:14:44 +01004127 vixl32::DRegister temp_d = DRegisterFrom(locations->GetTemp(0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01004128 vixl32::DRegister constant_d = DRegisterFrom(locations->GetTemp(1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004129
4130 // temp_d = int-to-double(high)
4131 __ Vmov(temp_s, high);
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01004132 __ Vcvt(F64, S32, temp_d, temp_s);
Scott Wakelingfe885462016-09-22 10:24:38 +01004133 // constant_d = k2Pow32EncodingForDouble
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004134 __ Vmov(constant_d, bit_cast<double, int64_t>(k2Pow32EncodingForDouble));
Scott Wakelingfe885462016-09-22 10:24:38 +01004135 // out_d = unsigned-to-double(low)
4136 __ Vmov(out_s, low);
4137 __ Vcvt(F64, U32, out_d, out_s);
4138 // out_d += temp_d * constant_d
4139 __ Vmla(F64, out_d, temp_d, constant_d);
4140 break;
4141 }
4142
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004143 case DataType::Type::kFloat32:
Scott Wakelingc34dba72016-10-03 10:14:44 +01004144 __ Vcvt(F64, F32, DRegisterFrom(out), InputSRegisterAt(conversion, 0));
Scott Wakelingfe885462016-09-22 10:24:38 +01004145 break;
4146
4147 default:
4148 LOG(FATAL) << "Unexpected type conversion from " << input_type
4149 << " to " << result_type;
4150 };
4151 break;
4152
4153 default:
4154 LOG(FATAL) << "Unexpected type conversion from " << input_type
4155 << " to " << result_type;
4156 }
4157}
4158
4159void LocationsBuilderARMVIXL::VisitAdd(HAdd* add) {
4160 LocationSummary* locations =
4161 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
4162 switch (add->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004163 case DataType::Type::kInt32: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004164 locations->SetInAt(0, Location::RequiresRegister());
4165 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
4166 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4167 break;
4168 }
4169
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004170 case DataType::Type::kInt64: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004171 locations->SetInAt(0, Location::RequiresRegister());
Anton Kirilovdda43962016-11-21 19:55:20 +00004172 locations->SetInAt(1, ArmEncodableConstantOrRegister(add->InputAt(1), ADD));
Scott Wakelingfe885462016-09-22 10:24:38 +01004173 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4174 break;
4175 }
4176
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004177 case DataType::Type::kFloat32:
4178 case DataType::Type::kFloat64: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004179 locations->SetInAt(0, Location::RequiresFpuRegister());
4180 locations->SetInAt(1, Location::RequiresFpuRegister());
4181 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4182 break;
4183 }
4184
4185 default:
4186 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
4187 }
4188}
4189
4190void InstructionCodeGeneratorARMVIXL::VisitAdd(HAdd* add) {
4191 LocationSummary* locations = add->GetLocations();
4192 Location out = locations->Out();
4193 Location first = locations->InAt(0);
4194 Location second = locations->InAt(1);
4195
4196 switch (add->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004197 case DataType::Type::kInt32: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004198 __ Add(OutputRegister(add), InputRegisterAt(add, 0), InputOperandAt(add, 1));
4199 }
4200 break;
4201
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004202 case DataType::Type::kInt64: {
Anton Kirilovdda43962016-11-21 19:55:20 +00004203 if (second.IsConstant()) {
4204 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
4205 GenerateAddLongConst(out, first, value);
4206 } else {
4207 DCHECK(second.IsRegisterPair());
4208 __ Adds(LowRegisterFrom(out), LowRegisterFrom(first), LowRegisterFrom(second));
4209 __ Adc(HighRegisterFrom(out), HighRegisterFrom(first), HighRegisterFrom(second));
4210 }
Scott Wakelingfe885462016-09-22 10:24:38 +01004211 break;
4212 }
4213
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004214 case DataType::Type::kFloat32:
4215 case DataType::Type::kFloat64:
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004216 __ Vadd(OutputVRegister(add), InputVRegisterAt(add, 0), InputVRegisterAt(add, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004217 break;
4218
4219 default:
4220 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
4221 }
4222}
4223
4224void LocationsBuilderARMVIXL::VisitSub(HSub* sub) {
4225 LocationSummary* locations =
4226 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
4227 switch (sub->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004228 case DataType::Type::kInt32: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004229 locations->SetInAt(0, Location::RequiresRegister());
4230 locations->SetInAt(1, Location::RegisterOrConstant(sub->InputAt(1)));
4231 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4232 break;
4233 }
4234
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004235 case DataType::Type::kInt64: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004236 locations->SetInAt(0, Location::RequiresRegister());
Anton Kirilovdda43962016-11-21 19:55:20 +00004237 locations->SetInAt(1, ArmEncodableConstantOrRegister(sub->InputAt(1), SUB));
Scott Wakelingfe885462016-09-22 10:24:38 +01004238 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4239 break;
4240 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004241 case DataType::Type::kFloat32:
4242 case DataType::Type::kFloat64: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004243 locations->SetInAt(0, Location::RequiresFpuRegister());
4244 locations->SetInAt(1, Location::RequiresFpuRegister());
4245 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4246 break;
4247 }
4248 default:
4249 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
4250 }
4251}
4252
4253void InstructionCodeGeneratorARMVIXL::VisitSub(HSub* sub) {
4254 LocationSummary* locations = sub->GetLocations();
4255 Location out = locations->Out();
4256 Location first = locations->InAt(0);
4257 Location second = locations->InAt(1);
4258 switch (sub->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004259 case DataType::Type::kInt32: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004260 __ Sub(OutputRegister(sub), InputRegisterAt(sub, 0), InputOperandAt(sub, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004261 break;
4262 }
4263
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004264 case DataType::Type::kInt64: {
Anton Kirilovdda43962016-11-21 19:55:20 +00004265 if (second.IsConstant()) {
4266 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
4267 GenerateAddLongConst(out, first, -value);
4268 } else {
4269 DCHECK(second.IsRegisterPair());
4270 __ Subs(LowRegisterFrom(out), LowRegisterFrom(first), LowRegisterFrom(second));
4271 __ Sbc(HighRegisterFrom(out), HighRegisterFrom(first), HighRegisterFrom(second));
4272 }
Scott Wakelingfe885462016-09-22 10:24:38 +01004273 break;
4274 }
4275
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004276 case DataType::Type::kFloat32:
4277 case DataType::Type::kFloat64:
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004278 __ Vsub(OutputVRegister(sub), InputVRegisterAt(sub, 0), InputVRegisterAt(sub, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004279 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01004280
4281 default:
4282 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
4283 }
4284}
4285
4286void LocationsBuilderARMVIXL::VisitMul(HMul* mul) {
4287 LocationSummary* locations =
4288 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
4289 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004290 case DataType::Type::kInt32:
4291 case DataType::Type::kInt64: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004292 locations->SetInAt(0, Location::RequiresRegister());
4293 locations->SetInAt(1, Location::RequiresRegister());
4294 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4295 break;
4296 }
4297
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004298 case DataType::Type::kFloat32:
4299 case DataType::Type::kFloat64: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004300 locations->SetInAt(0, Location::RequiresFpuRegister());
4301 locations->SetInAt(1, Location::RequiresFpuRegister());
4302 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4303 break;
4304 }
4305
4306 default:
4307 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4308 }
4309}
4310
4311void InstructionCodeGeneratorARMVIXL::VisitMul(HMul* mul) {
4312 LocationSummary* locations = mul->GetLocations();
4313 Location out = locations->Out();
4314 Location first = locations->InAt(0);
4315 Location second = locations->InAt(1);
4316 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004317 case DataType::Type::kInt32: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004318 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
4319 break;
4320 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004321 case DataType::Type::kInt64: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004322 vixl32::Register out_hi = HighRegisterFrom(out);
4323 vixl32::Register out_lo = LowRegisterFrom(out);
4324 vixl32::Register in1_hi = HighRegisterFrom(first);
4325 vixl32::Register in1_lo = LowRegisterFrom(first);
4326 vixl32::Register in2_hi = HighRegisterFrom(second);
4327 vixl32::Register in2_lo = LowRegisterFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01004328
4329 // Extra checks to protect caused by the existence of R1_R2.
4330 // The algorithm is wrong if out.hi is either in1.lo or in2.lo:
4331 // (e.g. in1=r0_r1, in2=r2_r3 and out=r1_r2);
Anton Kirilov644032c2016-12-06 17:51:43 +00004332 DCHECK(!out_hi.Is(in1_lo));
4333 DCHECK(!out_hi.Is(in2_lo));
Scott Wakelingfe885462016-09-22 10:24:38 +01004334
4335 // input: in1 - 64 bits, in2 - 64 bits
4336 // output: out
4337 // formula: out.hi : out.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
4338 // parts: out.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
4339 // parts: out.lo = (in1.lo * in2.lo)[31:0]
4340
4341 UseScratchRegisterScope temps(GetVIXLAssembler());
4342 vixl32::Register temp = temps.Acquire();
4343 // temp <- in1.lo * in2.hi
4344 __ Mul(temp, in1_lo, in2_hi);
4345 // out.hi <- in1.lo * in2.hi + in1.hi * in2.lo
4346 __ Mla(out_hi, in1_hi, in2_lo, temp);
4347 // out.lo <- (in1.lo * in2.lo)[31:0];
4348 __ Umull(out_lo, temp, in1_lo, in2_lo);
4349 // out.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004350 __ Add(out_hi, out_hi, temp);
Scott Wakelingfe885462016-09-22 10:24:38 +01004351 break;
4352 }
4353
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004354 case DataType::Type::kFloat32:
4355 case DataType::Type::kFloat64:
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004356 __ Vmul(OutputVRegister(mul), InputVRegisterAt(mul, 0), InputVRegisterAt(mul, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004357 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01004358
4359 default:
4360 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4361 }
4362}
4363
Scott Wakelingfe885462016-09-22 10:24:38 +01004364void InstructionCodeGeneratorARMVIXL::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
4365 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004366 DCHECK(instruction->GetResultType() == DataType::Type::kInt32);
Scott Wakelingfe885462016-09-22 10:24:38 +01004367
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004368 Location second = instruction->GetLocations()->InAt(1);
Scott Wakelingfe885462016-09-22 10:24:38 +01004369 DCHECK(second.IsConstant());
4370
4371 vixl32::Register out = OutputRegister(instruction);
4372 vixl32::Register dividend = InputRegisterAt(instruction, 0);
Anton Kirilov644032c2016-12-06 17:51:43 +00004373 int32_t imm = Int32ConstantFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01004374 DCHECK(imm == 1 || imm == -1);
4375
4376 if (instruction->IsRem()) {
4377 __ Mov(out, 0);
4378 } else {
4379 if (imm == 1) {
4380 __ Mov(out, dividend);
4381 } else {
4382 __ Rsb(out, dividend, 0);
4383 }
4384 }
4385}
4386
4387void InstructionCodeGeneratorARMVIXL::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
4388 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004389 DCHECK(instruction->GetResultType() == DataType::Type::kInt32);
Scott Wakelingfe885462016-09-22 10:24:38 +01004390
4391 LocationSummary* locations = instruction->GetLocations();
4392 Location second = locations->InAt(1);
4393 DCHECK(second.IsConstant());
4394
4395 vixl32::Register out = OutputRegister(instruction);
4396 vixl32::Register dividend = InputRegisterAt(instruction, 0);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004397 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
Anton Kirilov644032c2016-12-06 17:51:43 +00004398 int32_t imm = Int32ConstantFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01004399 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
4400 int ctz_imm = CTZ(abs_imm);
4401
4402 if (ctz_imm == 1) {
4403 __ Lsr(temp, dividend, 32 - ctz_imm);
4404 } else {
4405 __ Asr(temp, dividend, 31);
4406 __ Lsr(temp, temp, 32 - ctz_imm);
4407 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004408 __ Add(out, temp, dividend);
Scott Wakelingfe885462016-09-22 10:24:38 +01004409
4410 if (instruction->IsDiv()) {
4411 __ Asr(out, out, ctz_imm);
4412 if (imm < 0) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004413 __ Rsb(out, out, 0);
Scott Wakelingfe885462016-09-22 10:24:38 +01004414 }
4415 } else {
4416 __ Ubfx(out, out, 0, ctz_imm);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004417 __ Sub(out, out, temp);
Scott Wakelingfe885462016-09-22 10:24:38 +01004418 }
4419}
4420
4421void InstructionCodeGeneratorARMVIXL::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
4422 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004423 DCHECK(instruction->GetResultType() == DataType::Type::kInt32);
Scott Wakelingfe885462016-09-22 10:24:38 +01004424
4425 LocationSummary* locations = instruction->GetLocations();
4426 Location second = locations->InAt(1);
4427 DCHECK(second.IsConstant());
4428
4429 vixl32::Register out = OutputRegister(instruction);
4430 vixl32::Register dividend = InputRegisterAt(instruction, 0);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004431 vixl32::Register temp1 = RegisterFrom(locations->GetTemp(0));
4432 vixl32::Register temp2 = RegisterFrom(locations->GetTemp(1));
Scott Wakelingb77051e2016-11-21 19:46:00 +00004433 int32_t imm = Int32ConstantFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01004434
4435 int64_t magic;
4436 int shift;
4437 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
4438
Anton Kirilovdda43962016-11-21 19:55:20 +00004439 // TODO(VIXL): Change the static cast to Operand::From() after VIXL is fixed.
4440 __ Mov(temp1, static_cast<int32_t>(magic));
Scott Wakelingfe885462016-09-22 10:24:38 +01004441 __ Smull(temp2, temp1, dividend, temp1);
4442
4443 if (imm > 0 && magic < 0) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004444 __ Add(temp1, temp1, dividend);
Scott Wakelingfe885462016-09-22 10:24:38 +01004445 } else if (imm < 0 && magic > 0) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004446 __ Sub(temp1, temp1, dividend);
Scott Wakelingfe885462016-09-22 10:24:38 +01004447 }
4448
4449 if (shift != 0) {
4450 __ Asr(temp1, temp1, shift);
4451 }
4452
4453 if (instruction->IsDiv()) {
4454 __ Sub(out, temp1, Operand(temp1, vixl32::Shift(ASR), 31));
4455 } else {
4456 __ Sub(temp1, temp1, Operand(temp1, vixl32::Shift(ASR), 31));
4457 // TODO: Strength reduction for mls.
4458 __ Mov(temp2, imm);
4459 __ Mls(out, temp1, temp2, dividend);
4460 }
4461}
4462
4463void InstructionCodeGeneratorARMVIXL::GenerateDivRemConstantIntegral(
4464 HBinaryOperation* instruction) {
4465 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004466 DCHECK(instruction->GetResultType() == DataType::Type::kInt32);
Scott Wakelingfe885462016-09-22 10:24:38 +01004467
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004468 Location second = instruction->GetLocations()->InAt(1);
Scott Wakelingfe885462016-09-22 10:24:38 +01004469 DCHECK(second.IsConstant());
4470
Anton Kirilov644032c2016-12-06 17:51:43 +00004471 int32_t imm = Int32ConstantFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01004472 if (imm == 0) {
4473 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
4474 } else if (imm == 1 || imm == -1) {
4475 DivRemOneOrMinusOne(instruction);
4476 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
4477 DivRemByPowerOfTwo(instruction);
4478 } else {
4479 DCHECK(imm <= -2 || imm >= 2);
4480 GenerateDivRemWithAnyConstant(instruction);
4481 }
4482}
4483
4484void LocationsBuilderARMVIXL::VisitDiv(HDiv* div) {
4485 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004486 if (div->GetResultType() == DataType::Type::kInt64) {
Scott Wakelingfe885462016-09-22 10:24:38 +01004487 // pLdiv runtime call.
4488 call_kind = LocationSummary::kCallOnMainOnly;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004489 } else if (div->GetResultType() == DataType::Type::kInt32 && div->InputAt(1)->IsConstant()) {
Scott Wakelingfe885462016-09-22 10:24:38 +01004490 // sdiv will be replaced by other instruction sequence.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004491 } else if (div->GetResultType() == DataType::Type::kInt32 &&
Scott Wakelingfe885462016-09-22 10:24:38 +01004492 !codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4493 // pIdivmod runtime call.
4494 call_kind = LocationSummary::kCallOnMainOnly;
4495 }
4496
4497 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
4498
4499 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004500 case DataType::Type::kInt32: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004501 if (div->InputAt(1)->IsConstant()) {
4502 locations->SetInAt(0, Location::RequiresRegister());
4503 locations->SetInAt(1, Location::ConstantLocation(div->InputAt(1)->AsConstant()));
4504 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Anton Kirilov644032c2016-12-06 17:51:43 +00004505 int32_t value = Int32ConstantFrom(div->InputAt(1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004506 if (value == 1 || value == 0 || value == -1) {
4507 // No temp register required.
4508 } else {
4509 locations->AddTemp(Location::RequiresRegister());
4510 if (!IsPowerOfTwo(AbsOrMin(value))) {
4511 locations->AddTemp(Location::RequiresRegister());
4512 }
4513 }
4514 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4515 locations->SetInAt(0, Location::RequiresRegister());
4516 locations->SetInAt(1, Location::RequiresRegister());
4517 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4518 } else {
Artem Serov551b28f2016-10-18 19:11:30 +01004519 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4520 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4521 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
Roland Levillain5e8d5f02016-10-18 18:03:43 +01004522 // Note: divmod will compute both the quotient and the remainder as the pair R0 and R1, but
Artem Serov551b28f2016-10-18 19:11:30 +01004523 // we only need the former.
4524 locations->SetOut(LocationFrom(r0));
Scott Wakelingfe885462016-09-22 10:24:38 +01004525 }
4526 break;
4527 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004528 case DataType::Type::kInt64: {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01004529 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4530 locations->SetInAt(0, LocationFrom(
4531 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
4532 locations->SetInAt(1, LocationFrom(
4533 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
4534 locations->SetOut(LocationFrom(r0, r1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004535 break;
4536 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004537 case DataType::Type::kFloat32:
4538 case DataType::Type::kFloat64: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004539 locations->SetInAt(0, Location::RequiresFpuRegister());
4540 locations->SetInAt(1, Location::RequiresFpuRegister());
4541 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4542 break;
4543 }
4544
4545 default:
4546 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
4547 }
4548}
4549
4550void InstructionCodeGeneratorARMVIXL::VisitDiv(HDiv* div) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01004551 Location lhs = div->GetLocations()->InAt(0);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004552 Location rhs = div->GetLocations()->InAt(1);
Scott Wakelingfe885462016-09-22 10:24:38 +01004553
4554 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004555 case DataType::Type::kInt32: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004556 if (rhs.IsConstant()) {
Scott Wakelingfe885462016-09-22 10:24:38 +01004557 GenerateDivRemConstantIntegral(div);
4558 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4559 __ Sdiv(OutputRegister(div), InputRegisterAt(div, 0), InputRegisterAt(div, 1));
4560 } else {
Artem Serov551b28f2016-10-18 19:11:30 +01004561 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4562 DCHECK(calling_convention.GetRegisterAt(0).Is(RegisterFrom(lhs)));
4563 DCHECK(calling_convention.GetRegisterAt(1).Is(RegisterFrom(rhs)));
4564 DCHECK(r0.Is(OutputRegister(div)));
4565
4566 codegen_->InvokeRuntime(kQuickIdivmod, div, div->GetDexPc());
4567 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
Scott Wakelingfe885462016-09-22 10:24:38 +01004568 }
4569 break;
4570 }
4571
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004572 case DataType::Type::kInt64: {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01004573 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4574 DCHECK(calling_convention.GetRegisterAt(0).Is(LowRegisterFrom(lhs)));
4575 DCHECK(calling_convention.GetRegisterAt(1).Is(HighRegisterFrom(lhs)));
4576 DCHECK(calling_convention.GetRegisterAt(2).Is(LowRegisterFrom(rhs)));
4577 DCHECK(calling_convention.GetRegisterAt(3).Is(HighRegisterFrom(rhs)));
4578 DCHECK(LowRegisterFrom(div->GetLocations()->Out()).Is(r0));
4579 DCHECK(HighRegisterFrom(div->GetLocations()->Out()).Is(r1));
4580
4581 codegen_->InvokeRuntime(kQuickLdiv, div, div->GetDexPc());
4582 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Scott Wakelingfe885462016-09-22 10:24:38 +01004583 break;
4584 }
4585
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004586 case DataType::Type::kFloat32:
4587 case DataType::Type::kFloat64:
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004588 __ Vdiv(OutputVRegister(div), InputVRegisterAt(div, 0), InputVRegisterAt(div, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004589 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01004590
4591 default:
4592 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
4593 }
4594}
4595
Artem Serov551b28f2016-10-18 19:11:30 +01004596void LocationsBuilderARMVIXL::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004597 DataType::Type type = rem->GetResultType();
Artem Serov551b28f2016-10-18 19:11:30 +01004598
4599 // Most remainders are implemented in the runtime.
4600 LocationSummary::CallKind call_kind = LocationSummary::kCallOnMainOnly;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004601 if (rem->GetResultType() == DataType::Type::kInt32 && rem->InputAt(1)->IsConstant()) {
Artem Serov551b28f2016-10-18 19:11:30 +01004602 // sdiv will be replaced by other instruction sequence.
4603 call_kind = LocationSummary::kNoCall;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004604 } else if ((rem->GetResultType() == DataType::Type::kInt32)
Artem Serov551b28f2016-10-18 19:11:30 +01004605 && codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4606 // Have hardware divide instruction for int, do it with three instructions.
4607 call_kind = LocationSummary::kNoCall;
4608 }
4609
4610 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
4611
4612 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004613 case DataType::Type::kInt32: {
Artem Serov551b28f2016-10-18 19:11:30 +01004614 if (rem->InputAt(1)->IsConstant()) {
4615 locations->SetInAt(0, Location::RequiresRegister());
4616 locations->SetInAt(1, Location::ConstantLocation(rem->InputAt(1)->AsConstant()));
4617 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Anton Kirilov644032c2016-12-06 17:51:43 +00004618 int32_t value = Int32ConstantFrom(rem->InputAt(1));
Artem Serov551b28f2016-10-18 19:11:30 +01004619 if (value == 1 || value == 0 || value == -1) {
4620 // No temp register required.
4621 } else {
4622 locations->AddTemp(Location::RequiresRegister());
4623 if (!IsPowerOfTwo(AbsOrMin(value))) {
4624 locations->AddTemp(Location::RequiresRegister());
4625 }
4626 }
4627 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4628 locations->SetInAt(0, Location::RequiresRegister());
4629 locations->SetInAt(1, Location::RequiresRegister());
4630 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4631 locations->AddTemp(Location::RequiresRegister());
4632 } else {
4633 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4634 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4635 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
Roland Levillain5e8d5f02016-10-18 18:03:43 +01004636 // Note: divmod will compute both the quotient and the remainder as the pair R0 and R1, but
Artem Serov551b28f2016-10-18 19:11:30 +01004637 // we only need the latter.
4638 locations->SetOut(LocationFrom(r1));
4639 }
4640 break;
4641 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004642 case DataType::Type::kInt64: {
Artem Serov551b28f2016-10-18 19:11:30 +01004643 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4644 locations->SetInAt(0, LocationFrom(
4645 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
4646 locations->SetInAt(1, LocationFrom(
4647 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
4648 // The runtime helper puts the output in R2,R3.
4649 locations->SetOut(LocationFrom(r2, r3));
4650 break;
4651 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004652 case DataType::Type::kFloat32: {
Artem Serov551b28f2016-10-18 19:11:30 +01004653 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4654 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
4655 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
4656 locations->SetOut(LocationFrom(s0));
4657 break;
4658 }
4659
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004660 case DataType::Type::kFloat64: {
Artem Serov551b28f2016-10-18 19:11:30 +01004661 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4662 locations->SetInAt(0, LocationFrom(
4663 calling_convention.GetFpuRegisterAt(0), calling_convention.GetFpuRegisterAt(1)));
4664 locations->SetInAt(1, LocationFrom(
4665 calling_convention.GetFpuRegisterAt(2), calling_convention.GetFpuRegisterAt(3)));
4666 locations->SetOut(LocationFrom(s0, s1));
4667 break;
4668 }
4669
4670 default:
4671 LOG(FATAL) << "Unexpected rem type " << type;
4672 }
4673}
4674
4675void InstructionCodeGeneratorARMVIXL::VisitRem(HRem* rem) {
4676 LocationSummary* locations = rem->GetLocations();
4677 Location second = locations->InAt(1);
4678
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004679 DataType::Type type = rem->GetResultType();
Artem Serov551b28f2016-10-18 19:11:30 +01004680 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004681 case DataType::Type::kInt32: {
Artem Serov551b28f2016-10-18 19:11:30 +01004682 vixl32::Register reg1 = InputRegisterAt(rem, 0);
4683 vixl32::Register out_reg = OutputRegister(rem);
4684 if (second.IsConstant()) {
4685 GenerateDivRemConstantIntegral(rem);
4686 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4687 vixl32::Register reg2 = RegisterFrom(second);
4688 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
4689
4690 // temp = reg1 / reg2 (integer division)
4691 // dest = reg1 - temp * reg2
4692 __ Sdiv(temp, reg1, reg2);
4693 __ Mls(out_reg, temp, reg2, reg1);
4694 } else {
4695 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4696 DCHECK(reg1.Is(calling_convention.GetRegisterAt(0)));
4697 DCHECK(RegisterFrom(second).Is(calling_convention.GetRegisterAt(1)));
4698 DCHECK(out_reg.Is(r1));
4699
4700 codegen_->InvokeRuntime(kQuickIdivmod, rem, rem->GetDexPc());
4701 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
4702 }
4703 break;
4704 }
4705
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004706 case DataType::Type::kInt64: {
Artem Serov551b28f2016-10-18 19:11:30 +01004707 codegen_->InvokeRuntime(kQuickLmod, rem, rem->GetDexPc());
4708 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
4709 break;
4710 }
4711
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004712 case DataType::Type::kFloat32: {
Artem Serov551b28f2016-10-18 19:11:30 +01004713 codegen_->InvokeRuntime(kQuickFmodf, rem, rem->GetDexPc());
4714 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
4715 break;
4716 }
4717
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004718 case DataType::Type::kFloat64: {
Artem Serov551b28f2016-10-18 19:11:30 +01004719 codegen_->InvokeRuntime(kQuickFmod, rem, rem->GetDexPc());
4720 CheckEntrypointTypes<kQuickFmod, double, double, double>();
4721 break;
4722 }
4723
4724 default:
4725 LOG(FATAL) << "Unexpected rem type " << type;
4726 }
4727}
4728
4729
Scott Wakelingfe885462016-09-22 10:24:38 +01004730void LocationsBuilderARMVIXL::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Artem Serov657022c2016-11-23 14:19:38 +00004731 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Scott Wakelingfe885462016-09-22 10:24:38 +01004732 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Scott Wakelingfe885462016-09-22 10:24:38 +01004733}
4734
4735void InstructionCodeGeneratorARMVIXL::VisitDivZeroCheck(HDivZeroCheck* instruction) {
4736 DivZeroCheckSlowPathARMVIXL* slow_path =
4737 new (GetGraph()->GetArena()) DivZeroCheckSlowPathARMVIXL(instruction);
4738 codegen_->AddSlowPath(slow_path);
4739
4740 LocationSummary* locations = instruction->GetLocations();
4741 Location value = locations->InAt(0);
4742
4743 switch (instruction->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004744 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004745 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004746 case DataType::Type::kInt8:
4747 case DataType::Type::kUint16:
4748 case DataType::Type::kInt16:
4749 case DataType::Type::kInt32: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004750 if (value.IsRegister()) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00004751 __ CompareAndBranchIfZero(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
Scott Wakelingfe885462016-09-22 10:24:38 +01004752 } else {
4753 DCHECK(value.IsConstant()) << value;
Anton Kirilov644032c2016-12-06 17:51:43 +00004754 if (Int32ConstantFrom(value) == 0) {
Scott Wakelingfe885462016-09-22 10:24:38 +01004755 __ B(slow_path->GetEntryLabel());
4756 }
4757 }
4758 break;
4759 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004760 case DataType::Type::kInt64: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004761 if (value.IsRegisterPair()) {
4762 UseScratchRegisterScope temps(GetVIXLAssembler());
4763 vixl32::Register temp = temps.Acquire();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004764 __ Orrs(temp, LowRegisterFrom(value), HighRegisterFrom(value));
Scott Wakelingfe885462016-09-22 10:24:38 +01004765 __ B(eq, slow_path->GetEntryLabel());
4766 } else {
4767 DCHECK(value.IsConstant()) << value;
Anton Kirilov644032c2016-12-06 17:51:43 +00004768 if (Int64ConstantFrom(value) == 0) {
Scott Wakelingfe885462016-09-22 10:24:38 +01004769 __ B(slow_path->GetEntryLabel());
4770 }
4771 }
4772 break;
4773 }
4774 default:
4775 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
4776 }
4777}
4778
Artem Serov02109dd2016-09-23 17:17:54 +01004779void InstructionCodeGeneratorARMVIXL::HandleIntegerRotate(HRor* ror) {
4780 LocationSummary* locations = ror->GetLocations();
4781 vixl32::Register in = InputRegisterAt(ror, 0);
4782 Location rhs = locations->InAt(1);
4783 vixl32::Register out = OutputRegister(ror);
4784
4785 if (rhs.IsConstant()) {
4786 // Arm32 and Thumb2 assemblers require a rotation on the interval [1,31],
4787 // so map all rotations to a +ve. equivalent in that range.
4788 // (e.g. left *or* right by -2 bits == 30 bits in the same direction.)
4789 uint32_t rot = CodeGenerator::GetInt32ValueOf(rhs.GetConstant()) & 0x1F;
4790 if (rot) {
4791 // Rotate, mapping left rotations to right equivalents if necessary.
4792 // (e.g. left by 2 bits == right by 30.)
4793 __ Ror(out, in, rot);
4794 } else if (!out.Is(in)) {
4795 __ Mov(out, in);
4796 }
4797 } else {
4798 __ Ror(out, in, RegisterFrom(rhs));
4799 }
4800}
4801
4802// Gain some speed by mapping all Long rotates onto equivalent pairs of Integer
4803// rotates by swapping input regs (effectively rotating by the first 32-bits of
4804// a larger rotation) or flipping direction (thus treating larger right/left
4805// rotations as sub-word sized rotations in the other direction) as appropriate.
4806void InstructionCodeGeneratorARMVIXL::HandleLongRotate(HRor* ror) {
4807 LocationSummary* locations = ror->GetLocations();
4808 vixl32::Register in_reg_lo = LowRegisterFrom(locations->InAt(0));
4809 vixl32::Register in_reg_hi = HighRegisterFrom(locations->InAt(0));
4810 Location rhs = locations->InAt(1);
4811 vixl32::Register out_reg_lo = LowRegisterFrom(locations->Out());
4812 vixl32::Register out_reg_hi = HighRegisterFrom(locations->Out());
4813
4814 if (rhs.IsConstant()) {
4815 uint64_t rot = CodeGenerator::GetInt64ValueOf(rhs.GetConstant());
4816 // Map all rotations to +ve. equivalents on the interval [0,63].
4817 rot &= kMaxLongShiftDistance;
4818 // For rotates over a word in size, 'pre-rotate' by 32-bits to keep rotate
4819 // logic below to a simple pair of binary orr.
4820 // (e.g. 34 bits == in_reg swap + 2 bits right.)
4821 if (rot >= kArmBitsPerWord) {
4822 rot -= kArmBitsPerWord;
4823 std::swap(in_reg_hi, in_reg_lo);
4824 }
4825 // Rotate, or mov to out for zero or word size rotations.
4826 if (rot != 0u) {
Scott Wakelingb77051e2016-11-21 19:46:00 +00004827 __ Lsr(out_reg_hi, in_reg_hi, Operand::From(rot));
Artem Serov02109dd2016-09-23 17:17:54 +01004828 __ Orr(out_reg_hi, out_reg_hi, Operand(in_reg_lo, ShiftType::LSL, kArmBitsPerWord - rot));
Scott Wakelingb77051e2016-11-21 19:46:00 +00004829 __ Lsr(out_reg_lo, in_reg_lo, Operand::From(rot));
Artem Serov02109dd2016-09-23 17:17:54 +01004830 __ Orr(out_reg_lo, out_reg_lo, Operand(in_reg_hi, ShiftType::LSL, kArmBitsPerWord - rot));
4831 } else {
4832 __ Mov(out_reg_lo, in_reg_lo);
4833 __ Mov(out_reg_hi, in_reg_hi);
4834 }
4835 } else {
4836 vixl32::Register shift_right = RegisterFrom(locations->GetTemp(0));
4837 vixl32::Register shift_left = RegisterFrom(locations->GetTemp(1));
4838 vixl32::Label end;
4839 vixl32::Label shift_by_32_plus_shift_right;
Anton Kirilov6f644202017-02-27 18:29:45 +00004840 vixl32::Label* final_label = codegen_->GetFinalLabel(ror, &end);
Artem Serov02109dd2016-09-23 17:17:54 +01004841
4842 __ And(shift_right, RegisterFrom(rhs), 0x1F);
4843 __ Lsrs(shift_left, RegisterFrom(rhs), 6);
Scott Wakelingbffdc702016-12-07 17:46:03 +00004844 __ Rsb(LeaveFlags, shift_left, shift_right, Operand::From(kArmBitsPerWord));
Artem Serov517d9f62016-12-12 15:51:15 +00004845 __ B(cc, &shift_by_32_plus_shift_right, /* far_target */ false);
Artem Serov02109dd2016-09-23 17:17:54 +01004846
4847 // out_reg_hi = (reg_hi << shift_left) | (reg_lo >> shift_right).
4848 // out_reg_lo = (reg_lo << shift_left) | (reg_hi >> shift_right).
4849 __ Lsl(out_reg_hi, in_reg_hi, shift_left);
4850 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
4851 __ Add(out_reg_hi, out_reg_hi, out_reg_lo);
4852 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
4853 __ Lsr(shift_left, in_reg_hi, shift_right);
4854 __ Add(out_reg_lo, out_reg_lo, shift_left);
Anton Kirilov6f644202017-02-27 18:29:45 +00004855 __ B(final_label);
Artem Serov02109dd2016-09-23 17:17:54 +01004856
4857 __ Bind(&shift_by_32_plus_shift_right); // Shift by 32+shift_right.
4858 // out_reg_hi = (reg_hi >> shift_right) | (reg_lo << shift_left).
4859 // out_reg_lo = (reg_lo >> shift_right) | (reg_hi << shift_left).
4860 __ Lsr(out_reg_hi, in_reg_hi, shift_right);
4861 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
4862 __ Add(out_reg_hi, out_reg_hi, out_reg_lo);
4863 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
4864 __ Lsl(shift_right, in_reg_hi, shift_left);
4865 __ Add(out_reg_lo, out_reg_lo, shift_right);
4866
Anton Kirilov6f644202017-02-27 18:29:45 +00004867 if (end.IsReferenced()) {
4868 __ Bind(&end);
4869 }
Artem Serov02109dd2016-09-23 17:17:54 +01004870 }
4871}
4872
4873void LocationsBuilderARMVIXL::VisitRor(HRor* ror) {
4874 LocationSummary* locations =
4875 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
4876 switch (ror->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004877 case DataType::Type::kInt32: {
Artem Serov02109dd2016-09-23 17:17:54 +01004878 locations->SetInAt(0, Location::RequiresRegister());
4879 locations->SetInAt(1, Location::RegisterOrConstant(ror->InputAt(1)));
4880 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4881 break;
4882 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004883 case DataType::Type::kInt64: {
Artem Serov02109dd2016-09-23 17:17:54 +01004884 locations->SetInAt(0, Location::RequiresRegister());
4885 if (ror->InputAt(1)->IsConstant()) {
4886 locations->SetInAt(1, Location::ConstantLocation(ror->InputAt(1)->AsConstant()));
4887 } else {
4888 locations->SetInAt(1, Location::RequiresRegister());
4889 locations->AddTemp(Location::RequiresRegister());
4890 locations->AddTemp(Location::RequiresRegister());
4891 }
4892 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
4893 break;
4894 }
4895 default:
4896 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
4897 }
4898}
4899
4900void InstructionCodeGeneratorARMVIXL::VisitRor(HRor* ror) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004901 DataType::Type type = ror->GetResultType();
Artem Serov02109dd2016-09-23 17:17:54 +01004902 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004903 case DataType::Type::kInt32: {
Artem Serov02109dd2016-09-23 17:17:54 +01004904 HandleIntegerRotate(ror);
4905 break;
4906 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004907 case DataType::Type::kInt64: {
Artem Serov02109dd2016-09-23 17:17:54 +01004908 HandleLongRotate(ror);
4909 break;
4910 }
4911 default:
4912 LOG(FATAL) << "Unexpected operation type " << type;
4913 UNREACHABLE();
4914 }
4915}
4916
Artem Serov02d37832016-10-25 15:25:33 +01004917void LocationsBuilderARMVIXL::HandleShift(HBinaryOperation* op) {
4918 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
4919
4920 LocationSummary* locations =
4921 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
4922
4923 switch (op->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004924 case DataType::Type::kInt32: {
Artem Serov02d37832016-10-25 15:25:33 +01004925 locations->SetInAt(0, Location::RequiresRegister());
4926 if (op->InputAt(1)->IsConstant()) {
4927 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
4928 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4929 } else {
4930 locations->SetInAt(1, Location::RequiresRegister());
4931 // Make the output overlap, as it will be used to hold the masked
4932 // second input.
4933 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
4934 }
4935 break;
4936 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004937 case DataType::Type::kInt64: {
Artem Serov02d37832016-10-25 15:25:33 +01004938 locations->SetInAt(0, Location::RequiresRegister());
4939 if (op->InputAt(1)->IsConstant()) {
4940 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
4941 // For simplicity, use kOutputOverlap even though we only require that low registers
4942 // don't clash with high registers which the register allocator currently guarantees.
4943 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
4944 } else {
4945 locations->SetInAt(1, Location::RequiresRegister());
4946 locations->AddTemp(Location::RequiresRegister());
4947 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
4948 }
4949 break;
4950 }
4951 default:
4952 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
4953 }
4954}
4955
4956void InstructionCodeGeneratorARMVIXL::HandleShift(HBinaryOperation* op) {
4957 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
4958
4959 LocationSummary* locations = op->GetLocations();
4960 Location out = locations->Out();
4961 Location first = locations->InAt(0);
4962 Location second = locations->InAt(1);
4963
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004964 DataType::Type type = op->GetResultType();
Artem Serov02d37832016-10-25 15:25:33 +01004965 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004966 case DataType::Type::kInt32: {
Artem Serov02d37832016-10-25 15:25:33 +01004967 vixl32::Register out_reg = OutputRegister(op);
4968 vixl32::Register first_reg = InputRegisterAt(op, 0);
4969 if (second.IsRegister()) {
4970 vixl32::Register second_reg = RegisterFrom(second);
4971 // ARM doesn't mask the shift count so we need to do it ourselves.
4972 __ And(out_reg, second_reg, kMaxIntShiftDistance);
4973 if (op->IsShl()) {
4974 __ Lsl(out_reg, first_reg, out_reg);
4975 } else if (op->IsShr()) {
4976 __ Asr(out_reg, first_reg, out_reg);
4977 } else {
4978 __ Lsr(out_reg, first_reg, out_reg);
4979 }
4980 } else {
Anton Kirilov644032c2016-12-06 17:51:43 +00004981 int32_t cst = Int32ConstantFrom(second);
Artem Serov02d37832016-10-25 15:25:33 +01004982 uint32_t shift_value = cst & kMaxIntShiftDistance;
4983 if (shift_value == 0) { // ARM does not support shifting with 0 immediate.
4984 __ Mov(out_reg, first_reg);
4985 } else if (op->IsShl()) {
4986 __ Lsl(out_reg, first_reg, shift_value);
4987 } else if (op->IsShr()) {
4988 __ Asr(out_reg, first_reg, shift_value);
4989 } else {
4990 __ Lsr(out_reg, first_reg, shift_value);
4991 }
4992 }
4993 break;
4994 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004995 case DataType::Type::kInt64: {
Artem Serov02d37832016-10-25 15:25:33 +01004996 vixl32::Register o_h = HighRegisterFrom(out);
4997 vixl32::Register o_l = LowRegisterFrom(out);
4998
4999 vixl32::Register high = HighRegisterFrom(first);
5000 vixl32::Register low = LowRegisterFrom(first);
5001
5002 if (second.IsRegister()) {
5003 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
5004
5005 vixl32::Register second_reg = RegisterFrom(second);
5006
5007 if (op->IsShl()) {
5008 __ And(o_l, second_reg, kMaxLongShiftDistance);
5009 // Shift the high part
5010 __ Lsl(o_h, high, o_l);
5011 // Shift the low part and `or` what overflew on the high part
Scott Wakelingb77051e2016-11-21 19:46:00 +00005012 __ Rsb(temp, o_l, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01005013 __ Lsr(temp, low, temp);
5014 __ Orr(o_h, o_h, temp);
5015 // If the shift is > 32 bits, override the high part
Scott Wakelingb77051e2016-11-21 19:46:00 +00005016 __ Subs(temp, o_l, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01005017 {
Artem Serov0fb37192016-12-06 18:13:40 +00005018 ExactAssemblyScope guard(GetVIXLAssembler(),
5019 2 * vixl32::kMaxInstructionSizeInBytes,
5020 CodeBufferCheckScope::kMaximumSize);
Artem Serov02d37832016-10-25 15:25:33 +01005021 __ it(pl);
5022 __ lsl(pl, o_h, low, temp);
5023 }
5024 // Shift the low part
5025 __ Lsl(o_l, low, o_l);
5026 } else if (op->IsShr()) {
5027 __ And(o_h, second_reg, kMaxLongShiftDistance);
5028 // Shift the low part
5029 __ Lsr(o_l, low, o_h);
5030 // Shift the high part and `or` what underflew on the low part
Scott Wakelingb77051e2016-11-21 19:46:00 +00005031 __ Rsb(temp, o_h, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01005032 __ Lsl(temp, high, temp);
5033 __ Orr(o_l, o_l, temp);
5034 // If the shift is > 32 bits, override the low part
Scott Wakelingb77051e2016-11-21 19:46:00 +00005035 __ Subs(temp, o_h, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01005036 {
Artem Serov0fb37192016-12-06 18:13:40 +00005037 ExactAssemblyScope guard(GetVIXLAssembler(),
5038 2 * vixl32::kMaxInstructionSizeInBytes,
5039 CodeBufferCheckScope::kMaximumSize);
Artem Serov02d37832016-10-25 15:25:33 +01005040 __ it(pl);
5041 __ asr(pl, o_l, high, temp);
5042 }
5043 // Shift the high part
5044 __ Asr(o_h, high, o_h);
5045 } else {
5046 __ And(o_h, second_reg, kMaxLongShiftDistance);
5047 // same as Shr except we use `Lsr`s and not `Asr`s
5048 __ Lsr(o_l, low, o_h);
Scott Wakelingb77051e2016-11-21 19:46:00 +00005049 __ Rsb(temp, o_h, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01005050 __ Lsl(temp, high, temp);
5051 __ Orr(o_l, o_l, temp);
Scott Wakelingb77051e2016-11-21 19:46:00 +00005052 __ Subs(temp, o_h, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01005053 {
Artem Serov0fb37192016-12-06 18:13:40 +00005054 ExactAssemblyScope guard(GetVIXLAssembler(),
5055 2 * vixl32::kMaxInstructionSizeInBytes,
5056 CodeBufferCheckScope::kMaximumSize);
Artem Serov02d37832016-10-25 15:25:33 +01005057 __ it(pl);
5058 __ lsr(pl, o_l, high, temp);
5059 }
5060 __ Lsr(o_h, high, o_h);
5061 }
5062 } else {
5063 // Register allocator doesn't create partial overlap.
5064 DCHECK(!o_l.Is(high));
5065 DCHECK(!o_h.Is(low));
Anton Kirilov644032c2016-12-06 17:51:43 +00005066 int32_t cst = Int32ConstantFrom(second);
Artem Serov02d37832016-10-25 15:25:33 +01005067 uint32_t shift_value = cst & kMaxLongShiftDistance;
5068 if (shift_value > 32) {
5069 if (op->IsShl()) {
5070 __ Lsl(o_h, low, shift_value - 32);
5071 __ Mov(o_l, 0);
5072 } else if (op->IsShr()) {
5073 __ Asr(o_l, high, shift_value - 32);
5074 __ Asr(o_h, high, 31);
5075 } else {
5076 __ Lsr(o_l, high, shift_value - 32);
5077 __ Mov(o_h, 0);
5078 }
5079 } else if (shift_value == 32) {
5080 if (op->IsShl()) {
5081 __ Mov(o_h, low);
5082 __ Mov(o_l, 0);
5083 } else if (op->IsShr()) {
5084 __ Mov(o_l, high);
5085 __ Asr(o_h, high, 31);
5086 } else {
5087 __ Mov(o_l, high);
5088 __ Mov(o_h, 0);
5089 }
5090 } else if (shift_value == 1) {
5091 if (op->IsShl()) {
5092 __ Lsls(o_l, low, 1);
5093 __ Adc(o_h, high, high);
5094 } else if (op->IsShr()) {
5095 __ Asrs(o_h, high, 1);
5096 __ Rrx(o_l, low);
5097 } else {
5098 __ Lsrs(o_h, high, 1);
5099 __ Rrx(o_l, low);
5100 }
5101 } else {
5102 DCHECK(2 <= shift_value && shift_value < 32) << shift_value;
5103 if (op->IsShl()) {
5104 __ Lsl(o_h, high, shift_value);
5105 __ Orr(o_h, o_h, Operand(low, ShiftType::LSR, 32 - shift_value));
5106 __ Lsl(o_l, low, shift_value);
5107 } else if (op->IsShr()) {
5108 __ Lsr(o_l, low, shift_value);
5109 __ Orr(o_l, o_l, Operand(high, ShiftType::LSL, 32 - shift_value));
5110 __ Asr(o_h, high, shift_value);
5111 } else {
5112 __ Lsr(o_l, low, shift_value);
5113 __ Orr(o_l, o_l, Operand(high, ShiftType::LSL, 32 - shift_value));
5114 __ Lsr(o_h, high, shift_value);
5115 }
5116 }
5117 }
5118 break;
5119 }
5120 default:
5121 LOG(FATAL) << "Unexpected operation type " << type;
5122 UNREACHABLE();
5123 }
5124}
5125
5126void LocationsBuilderARMVIXL::VisitShl(HShl* shl) {
5127 HandleShift(shl);
5128}
5129
5130void InstructionCodeGeneratorARMVIXL::VisitShl(HShl* shl) {
5131 HandleShift(shl);
5132}
5133
5134void LocationsBuilderARMVIXL::VisitShr(HShr* shr) {
5135 HandleShift(shr);
5136}
5137
5138void InstructionCodeGeneratorARMVIXL::VisitShr(HShr* shr) {
5139 HandleShift(shr);
5140}
5141
5142void LocationsBuilderARMVIXL::VisitUShr(HUShr* ushr) {
5143 HandleShift(ushr);
5144}
5145
5146void InstructionCodeGeneratorARMVIXL::VisitUShr(HUShr* ushr) {
5147 HandleShift(ushr);
5148}
5149
5150void LocationsBuilderARMVIXL::VisitNewInstance(HNewInstance* instruction) {
5151 LocationSummary* locations =
5152 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
5153 if (instruction->IsStringAlloc()) {
5154 locations->AddTemp(LocationFrom(kMethodRegister));
5155 } else {
5156 InvokeRuntimeCallingConventionARMVIXL calling_convention;
5157 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
Artem Serov02d37832016-10-25 15:25:33 +01005158 }
5159 locations->SetOut(LocationFrom(r0));
5160}
5161
5162void InstructionCodeGeneratorARMVIXL::VisitNewInstance(HNewInstance* instruction) {
5163 // Note: if heap poisoning is enabled, the entry point takes cares
5164 // of poisoning the reference.
5165 if (instruction->IsStringAlloc()) {
5166 // String is allocated through StringFactory. Call NewEmptyString entry point.
5167 vixl32::Register temp = RegisterFrom(instruction->GetLocations()->GetTemp(0));
5168 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize);
5169 GetAssembler()->LoadFromOffset(kLoadWord, temp, tr, QUICK_ENTRY_POINT(pNewEmptyString));
5170 GetAssembler()->LoadFromOffset(kLoadWord, lr, temp, code_offset.Int32Value());
Alexandre Rames374ddf32016-11-04 10:40:49 +00005171 // 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 +00005172 ExactAssemblyScope aas(GetVIXLAssembler(),
5173 vixl32::k16BitT32InstructionSizeInBytes,
5174 CodeBufferCheckScope::kExactSize);
Artem Serov02d37832016-10-25 15:25:33 +01005175 __ blx(lr);
5176 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
5177 } else {
5178 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00005179 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
Artem Serov02d37832016-10-25 15:25:33 +01005180 }
Roland Levillain5daa4952017-07-03 17:23:56 +01005181 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 10);
Artem Serov02d37832016-10-25 15:25:33 +01005182}
5183
5184void LocationsBuilderARMVIXL::VisitNewArray(HNewArray* instruction) {
5185 LocationSummary* locations =
5186 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
5187 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Artem Serov02d37832016-10-25 15:25:33 +01005188 locations->SetOut(LocationFrom(r0));
Nicolas Geoffray8c7c4f12017-01-26 10:13:11 +00005189 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5190 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
Artem Serov02d37832016-10-25 15:25:33 +01005191}
5192
5193void InstructionCodeGeneratorARMVIXL::VisitNewArray(HNewArray* instruction) {
Artem Serov02d37832016-10-25 15:25:33 +01005194 // Note: if heap poisoning is enabled, the entry point takes cares
5195 // of poisoning the reference.
Artem Serov7b3672e2017-02-03 17:30:34 +00005196 QuickEntrypointEnum entrypoint =
5197 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
5198 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005199 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Artem Serov7b3672e2017-02-03 17:30:34 +00005200 DCHECK(!codegen_->IsLeafMethod());
Roland Levillain5daa4952017-07-03 17:23:56 +01005201 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 11);
Artem Serov02d37832016-10-25 15:25:33 +01005202}
5203
5204void LocationsBuilderARMVIXL::VisitParameterValue(HParameterValue* instruction) {
5205 LocationSummary* locations =
5206 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5207 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
5208 if (location.IsStackSlot()) {
5209 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5210 } else if (location.IsDoubleStackSlot()) {
5211 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5212 }
5213 locations->SetOut(location);
5214}
5215
5216void InstructionCodeGeneratorARMVIXL::VisitParameterValue(
5217 HParameterValue* instruction ATTRIBUTE_UNUSED) {
5218 // Nothing to do, the parameter is already at its location.
5219}
5220
5221void LocationsBuilderARMVIXL::VisitCurrentMethod(HCurrentMethod* instruction) {
5222 LocationSummary* locations =
5223 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5224 locations->SetOut(LocationFrom(kMethodRegister));
5225}
5226
5227void InstructionCodeGeneratorARMVIXL::VisitCurrentMethod(
5228 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
5229 // Nothing to do, the method is already at its location.
5230}
5231
5232void LocationsBuilderARMVIXL::VisitNot(HNot* not_) {
5233 LocationSummary* locations =
5234 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
5235 locations->SetInAt(0, Location::RequiresRegister());
5236 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5237}
5238
5239void InstructionCodeGeneratorARMVIXL::VisitNot(HNot* not_) {
5240 LocationSummary* locations = not_->GetLocations();
5241 Location out = locations->Out();
5242 Location in = locations->InAt(0);
5243 switch (not_->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005244 case DataType::Type::kInt32:
Artem Serov02d37832016-10-25 15:25:33 +01005245 __ Mvn(OutputRegister(not_), InputRegisterAt(not_, 0));
5246 break;
5247
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005248 case DataType::Type::kInt64:
Artem Serov02d37832016-10-25 15:25:33 +01005249 __ Mvn(LowRegisterFrom(out), LowRegisterFrom(in));
5250 __ Mvn(HighRegisterFrom(out), HighRegisterFrom(in));
5251 break;
5252
5253 default:
5254 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
5255 }
5256}
5257
Scott Wakelingc34dba72016-10-03 10:14:44 +01005258void LocationsBuilderARMVIXL::VisitBooleanNot(HBooleanNot* bool_not) {
5259 LocationSummary* locations =
5260 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
5261 locations->SetInAt(0, Location::RequiresRegister());
5262 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5263}
5264
5265void InstructionCodeGeneratorARMVIXL::VisitBooleanNot(HBooleanNot* bool_not) {
5266 __ Eor(OutputRegister(bool_not), InputRegister(bool_not), 1);
5267}
5268
Artem Serov02d37832016-10-25 15:25:33 +01005269void LocationsBuilderARMVIXL::VisitCompare(HCompare* compare) {
5270 LocationSummary* locations =
5271 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
5272 switch (compare->InputAt(0)->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005273 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005274 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005275 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005276 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005277 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005278 case DataType::Type::kInt32:
5279 case DataType::Type::kInt64: {
Artem Serov02d37832016-10-25 15:25:33 +01005280 locations->SetInAt(0, Location::RequiresRegister());
5281 locations->SetInAt(1, Location::RequiresRegister());
5282 // Output overlaps because it is written before doing the low comparison.
5283 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
5284 break;
5285 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005286 case DataType::Type::kFloat32:
5287 case DataType::Type::kFloat64: {
Artem Serov02d37832016-10-25 15:25:33 +01005288 locations->SetInAt(0, Location::RequiresFpuRegister());
5289 locations->SetInAt(1, ArithmeticZeroOrFpuRegister(compare->InputAt(1)));
5290 locations->SetOut(Location::RequiresRegister());
5291 break;
5292 }
5293 default:
5294 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
5295 }
5296}
5297
5298void InstructionCodeGeneratorARMVIXL::VisitCompare(HCompare* compare) {
5299 LocationSummary* locations = compare->GetLocations();
5300 vixl32::Register out = OutputRegister(compare);
5301 Location left = locations->InAt(0);
5302 Location right = locations->InAt(1);
5303
5304 vixl32::Label less, greater, done;
Anton Kirilov6f644202017-02-27 18:29:45 +00005305 vixl32::Label* final_label = codegen_->GetFinalLabel(compare, &done);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005306 DataType::Type type = compare->InputAt(0)->GetType();
Artem Serov02d37832016-10-25 15:25:33 +01005307 vixl32::Condition less_cond = vixl32::Condition(kNone);
5308 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005309 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005310 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005311 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005312 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005313 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005314 case DataType::Type::kInt32: {
Artem Serov02d37832016-10-25 15:25:33 +01005315 // Emit move to `out` before the `Cmp`, as `Mov` might affect the status flags.
5316 __ Mov(out, 0);
5317 __ Cmp(RegisterFrom(left), RegisterFrom(right)); // Signed compare.
5318 less_cond = lt;
5319 break;
5320 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005321 case DataType::Type::kInt64: {
Artem Serov02d37832016-10-25 15:25:33 +01005322 __ Cmp(HighRegisterFrom(left), HighRegisterFrom(right)); // Signed compare.
Artem Serov517d9f62016-12-12 15:51:15 +00005323 __ B(lt, &less, /* far_target */ false);
5324 __ B(gt, &greater, /* far_target */ false);
Artem Serov02d37832016-10-25 15:25:33 +01005325 // Emit move to `out` before the last `Cmp`, as `Mov` might affect the status flags.
5326 __ Mov(out, 0);
5327 __ Cmp(LowRegisterFrom(left), LowRegisterFrom(right)); // Unsigned compare.
5328 less_cond = lo;
5329 break;
5330 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005331 case DataType::Type::kFloat32:
5332 case DataType::Type::kFloat64: {
Artem Serov02d37832016-10-25 15:25:33 +01005333 __ Mov(out, 0);
Donghui Bai426b49c2016-11-08 14:55:38 +08005334 GenerateVcmp(compare, codegen_);
Artem Serov02d37832016-10-25 15:25:33 +01005335 // To branch on the FP compare result we transfer FPSCR to APSR (encoded as PC in VMRS).
5336 __ Vmrs(RegisterOrAPSR_nzcv(kPcCode), FPSCR);
5337 less_cond = ARMFPCondition(kCondLT, compare->IsGtBias());
5338 break;
5339 }
5340 default:
5341 LOG(FATAL) << "Unexpected compare type " << type;
5342 UNREACHABLE();
5343 }
5344
Anton Kirilov6f644202017-02-27 18:29:45 +00005345 __ B(eq, final_label, /* far_target */ false);
Artem Serov517d9f62016-12-12 15:51:15 +00005346 __ B(less_cond, &less, /* far_target */ false);
Artem Serov02d37832016-10-25 15:25:33 +01005347
5348 __ Bind(&greater);
5349 __ Mov(out, 1);
Anton Kirilov6f644202017-02-27 18:29:45 +00005350 __ B(final_label);
Artem Serov02d37832016-10-25 15:25:33 +01005351
5352 __ Bind(&less);
5353 __ Mov(out, -1);
5354
Anton Kirilov6f644202017-02-27 18:29:45 +00005355 if (done.IsReferenced()) {
5356 __ Bind(&done);
5357 }
Artem Serov02d37832016-10-25 15:25:33 +01005358}
5359
5360void LocationsBuilderARMVIXL::VisitPhi(HPhi* instruction) {
5361 LocationSummary* locations =
5362 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5363 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
5364 locations->SetInAt(i, Location::Any());
5365 }
5366 locations->SetOut(Location::Any());
5367}
5368
5369void InstructionCodeGeneratorARMVIXL::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
5370 LOG(FATAL) << "Unreachable";
5371}
5372
5373void CodeGeneratorARMVIXL::GenerateMemoryBarrier(MemBarrierKind kind) {
5374 // TODO (ported from quick): revisit ARM barrier kinds.
5375 DmbOptions flavor = DmbOptions::ISH; // Quiet C++ warnings.
5376 switch (kind) {
5377 case MemBarrierKind::kAnyStore:
5378 case MemBarrierKind::kLoadAny:
5379 case MemBarrierKind::kAnyAny: {
5380 flavor = DmbOptions::ISH;
5381 break;
5382 }
5383 case MemBarrierKind::kStoreStore: {
5384 flavor = DmbOptions::ISHST;
5385 break;
5386 }
5387 default:
5388 LOG(FATAL) << "Unexpected memory barrier " << kind;
5389 }
5390 __ Dmb(flavor);
5391}
5392
5393void InstructionCodeGeneratorARMVIXL::GenerateWideAtomicLoad(vixl32::Register addr,
5394 uint32_t offset,
5395 vixl32::Register out_lo,
5396 vixl32::Register out_hi) {
5397 UseScratchRegisterScope temps(GetVIXLAssembler());
5398 if (offset != 0) {
5399 vixl32::Register temp = temps.Acquire();
5400 __ Add(temp, addr, offset);
5401 addr = temp;
5402 }
Scott Wakelingb77051e2016-11-21 19:46:00 +00005403 __ Ldrexd(out_lo, out_hi, MemOperand(addr));
Artem Serov02d37832016-10-25 15:25:33 +01005404}
5405
5406void InstructionCodeGeneratorARMVIXL::GenerateWideAtomicStore(vixl32::Register addr,
5407 uint32_t offset,
5408 vixl32::Register value_lo,
5409 vixl32::Register value_hi,
5410 vixl32::Register temp1,
5411 vixl32::Register temp2,
5412 HInstruction* instruction) {
5413 UseScratchRegisterScope temps(GetVIXLAssembler());
5414 vixl32::Label fail;
5415 if (offset != 0) {
5416 vixl32::Register temp = temps.Acquire();
5417 __ Add(temp, addr, offset);
5418 addr = temp;
5419 }
5420 __ Bind(&fail);
Alexandre Rames374ddf32016-11-04 10:40:49 +00005421 {
5422 // Ensure the pc position is recorded immediately after the `ldrexd` instruction.
Artem Serov0fb37192016-12-06 18:13:40 +00005423 ExactAssemblyScope aas(GetVIXLAssembler(),
5424 vixl32::kMaxInstructionSizeInBytes,
5425 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00005426 // We need a load followed by store. (The address used in a STREX instruction must
5427 // be the same as the address in the most recently executed LDREX instruction.)
5428 __ ldrexd(temp1, temp2, MemOperand(addr));
5429 codegen_->MaybeRecordImplicitNullCheck(instruction);
5430 }
Scott Wakelingb77051e2016-11-21 19:46:00 +00005431 __ Strexd(temp1, value_lo, value_hi, MemOperand(addr));
xueliang.zhongf51bc622016-11-04 09:23:32 +00005432 __ CompareAndBranchIfNonZero(temp1, &fail);
Artem Serov02d37832016-10-25 15:25:33 +01005433}
Artem Serov02109dd2016-09-23 17:17:54 +01005434
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005435void LocationsBuilderARMVIXL::HandleFieldSet(
5436 HInstruction* instruction, const FieldInfo& field_info) {
5437 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
5438
5439 LocationSummary* locations =
5440 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5441 locations->SetInAt(0, Location::RequiresRegister());
5442
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005443 DataType::Type field_type = field_info.GetFieldType();
5444 if (DataType::IsFloatingPointType(field_type)) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005445 locations->SetInAt(1, Location::RequiresFpuRegister());
5446 } else {
5447 locations->SetInAt(1, Location::RequiresRegister());
5448 }
5449
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005450 bool is_wide = field_type == DataType::Type::kInt64 || field_type == DataType::Type::kFloat64;
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005451 bool generate_volatile = field_info.IsVolatile()
5452 && is_wide
5453 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
5454 bool needs_write_barrier =
5455 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
5456 // Temporary registers for the write barrier.
5457 // TODO: consider renaming StoreNeedsWriteBarrier to StoreNeedsGCMark.
5458 if (needs_write_barrier) {
5459 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
5460 locations->AddTemp(Location::RequiresRegister());
5461 } else if (generate_volatile) {
5462 // ARM encoding have some additional constraints for ldrexd/strexd:
5463 // - registers need to be consecutive
5464 // - the first register should be even but not R14.
5465 // We don't test for ARM yet, and the assertion makes sure that we
5466 // revisit this if we ever enable ARM encoding.
5467 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
5468
5469 locations->AddTemp(Location::RequiresRegister());
5470 locations->AddTemp(Location::RequiresRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005471 if (field_type == DataType::Type::kFloat64) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005472 // For doubles we need two more registers to copy the value.
5473 locations->AddTemp(LocationFrom(r2));
5474 locations->AddTemp(LocationFrom(r3));
5475 }
5476 }
5477}
5478
5479void InstructionCodeGeneratorARMVIXL::HandleFieldSet(HInstruction* instruction,
5480 const FieldInfo& field_info,
5481 bool value_can_be_null) {
5482 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
5483
5484 LocationSummary* locations = instruction->GetLocations();
5485 vixl32::Register base = InputRegisterAt(instruction, 0);
5486 Location value = locations->InAt(1);
5487
5488 bool is_volatile = field_info.IsVolatile();
5489 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005490 DataType::Type field_type = field_info.GetFieldType();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005491 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
5492 bool needs_write_barrier =
5493 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
5494
5495 if (is_volatile) {
5496 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
5497 }
5498
5499 switch (field_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005500 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005501 case DataType::Type::kUint8:
5502 case DataType::Type::kInt8:
5503 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005504 case DataType::Type::kInt16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005505 case DataType::Type::kInt32: {
5506 StoreOperandType operand_type = GetStoreOperandType(field_type);
5507 GetAssembler()->StoreToOffset(operand_type, RegisterFrom(value), base, offset);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005508 break;
5509 }
5510
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005511 case DataType::Type::kReference: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005512 if (kPoisonHeapReferences && needs_write_barrier) {
5513 // Note that in the case where `value` is a null reference,
5514 // we do not enter this block, as a null reference does not
5515 // need poisoning.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005516 DCHECK_EQ(field_type, DataType::Type::kReference);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005517 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
5518 __ Mov(temp, RegisterFrom(value));
5519 GetAssembler()->PoisonHeapReference(temp);
5520 GetAssembler()->StoreToOffset(kStoreWord, temp, base, offset);
5521 } else {
5522 GetAssembler()->StoreToOffset(kStoreWord, RegisterFrom(value), base, offset);
5523 }
5524 break;
5525 }
5526
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005527 case DataType::Type::kInt64: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005528 if (is_volatile && !atomic_ldrd_strd) {
5529 GenerateWideAtomicStore(base,
5530 offset,
5531 LowRegisterFrom(value),
5532 HighRegisterFrom(value),
5533 RegisterFrom(locations->GetTemp(0)),
5534 RegisterFrom(locations->GetTemp(1)),
5535 instruction);
5536 } else {
5537 GetAssembler()->StoreToOffset(kStoreWordPair, LowRegisterFrom(value), base, offset);
5538 codegen_->MaybeRecordImplicitNullCheck(instruction);
5539 }
5540 break;
5541 }
5542
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005543 case DataType::Type::kFloat32: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005544 GetAssembler()->StoreSToOffset(SRegisterFrom(value), base, offset);
5545 break;
5546 }
5547
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005548 case DataType::Type::kFloat64: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01005549 vixl32::DRegister value_reg = DRegisterFrom(value);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005550 if (is_volatile && !atomic_ldrd_strd) {
5551 vixl32::Register value_reg_lo = RegisterFrom(locations->GetTemp(0));
5552 vixl32::Register value_reg_hi = RegisterFrom(locations->GetTemp(1));
5553
5554 __ Vmov(value_reg_lo, value_reg_hi, value_reg);
5555
5556 GenerateWideAtomicStore(base,
5557 offset,
5558 value_reg_lo,
5559 value_reg_hi,
5560 RegisterFrom(locations->GetTemp(2)),
5561 RegisterFrom(locations->GetTemp(3)),
5562 instruction);
5563 } else {
5564 GetAssembler()->StoreDToOffset(value_reg, base, offset);
5565 codegen_->MaybeRecordImplicitNullCheck(instruction);
5566 }
5567 break;
5568 }
5569
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005570 case DataType::Type::kVoid:
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005571 LOG(FATAL) << "Unreachable type " << field_type;
5572 UNREACHABLE();
5573 }
5574
5575 // Longs and doubles are handled in the switch.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005576 if (field_type != DataType::Type::kInt64 && field_type != DataType::Type::kFloat64) {
Alexandre Rames374ddf32016-11-04 10:40:49 +00005577 // TODO(VIXL): Here and for other calls to `MaybeRecordImplicitNullCheck` in this method, we
5578 // should use a scope and the assembler to emit the store instruction to guarantee that we
5579 // record the pc at the correct position. But the `Assembler` does not automatically handle
5580 // unencodable offsets. Practically, everything is fine because the helper and VIXL, at the time
5581 // of writing, do generate the store instruction last.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005582 codegen_->MaybeRecordImplicitNullCheck(instruction);
5583 }
5584
5585 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
5586 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
5587 vixl32::Register card = RegisterFrom(locations->GetTemp(1));
5588 codegen_->MarkGCCard(temp, card, base, RegisterFrom(value), value_can_be_null);
5589 }
5590
5591 if (is_volatile) {
5592 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
5593 }
5594}
5595
Artem Serov02d37832016-10-25 15:25:33 +01005596void LocationsBuilderARMVIXL::HandleFieldGet(HInstruction* instruction,
5597 const FieldInfo& field_info) {
5598 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
5599
5600 bool object_field_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005601 kEmitCompilerReadBarrier && (field_info.GetFieldType() == DataType::Type::kReference);
Artem Serov02d37832016-10-25 15:25:33 +01005602 LocationSummary* locations =
5603 new (GetGraph()->GetArena()) LocationSummary(instruction,
5604 object_field_get_with_read_barrier ?
5605 LocationSummary::kCallOnSlowPath :
5606 LocationSummary::kNoCall);
5607 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
5608 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
5609 }
5610 locations->SetInAt(0, Location::RequiresRegister());
5611
5612 bool volatile_for_double = field_info.IsVolatile()
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005613 && (field_info.GetFieldType() == DataType::Type::kFloat64)
Artem Serov02d37832016-10-25 15:25:33 +01005614 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
5615 // The output overlaps in case of volatile long: we don't want the
5616 // code generated by GenerateWideAtomicLoad to overwrite the
5617 // object's location. Likewise, in the case of an object field get
5618 // with read barriers enabled, we do not want the load to overwrite
5619 // the object's location, as we need it to emit the read barrier.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005620 bool overlap =
5621 (field_info.IsVolatile() && (field_info.GetFieldType() == DataType::Type::kInt64)) ||
Artem Serov02d37832016-10-25 15:25:33 +01005622 object_field_get_with_read_barrier;
5623
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005624 if (DataType::IsFloatingPointType(instruction->GetType())) {
Artem Serov02d37832016-10-25 15:25:33 +01005625 locations->SetOut(Location::RequiresFpuRegister());
5626 } else {
5627 locations->SetOut(Location::RequiresRegister(),
5628 (overlap ? Location::kOutputOverlap : Location::kNoOutputOverlap));
5629 }
5630 if (volatile_for_double) {
5631 // ARM encoding have some additional constraints for ldrexd/strexd:
5632 // - registers need to be consecutive
5633 // - the first register should be even but not R14.
5634 // We don't test for ARM yet, and the assertion makes sure that we
5635 // revisit this if we ever enable ARM encoding.
5636 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
5637 locations->AddTemp(Location::RequiresRegister());
5638 locations->AddTemp(Location::RequiresRegister());
5639 } else if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
5640 // We need a temporary register for the read barrier marking slow
Artem Serovc5fcb442016-12-02 19:19:58 +00005641 // path in CodeGeneratorARMVIXL::GenerateFieldLoadWithBakerReadBarrier.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01005642 if (kBakerReadBarrierLinkTimeThunksEnableForFields &&
5643 !Runtime::Current()->UseJitCompilation()) {
5644 // If link-time thunks for the Baker read barrier are enabled, for AOT
5645 // loads we need a temporary only if the offset is too big.
5646 if (field_info.GetFieldOffset().Uint32Value() >= kReferenceLoadMinFarOffset) {
5647 locations->AddTemp(Location::RequiresRegister());
5648 }
5649 // And we always need the reserved entrypoint register.
5650 locations->AddTemp(Location::RegisterLocation(kBakerCcEntrypointRegister.GetCode()));
5651 } else {
5652 locations->AddTemp(Location::RequiresRegister());
5653 }
Artem Serov02d37832016-10-25 15:25:33 +01005654 }
5655}
5656
5657Location LocationsBuilderARMVIXL::ArithmeticZeroOrFpuRegister(HInstruction* input) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005658 DCHECK(DataType::IsFloatingPointType(input->GetType())) << input->GetType();
Artem Serov02d37832016-10-25 15:25:33 +01005659 if ((input->IsFloatConstant() && (input->AsFloatConstant()->IsArithmeticZero())) ||
5660 (input->IsDoubleConstant() && (input->AsDoubleConstant()->IsArithmeticZero()))) {
5661 return Location::ConstantLocation(input->AsConstant());
5662 } else {
5663 return Location::RequiresFpuRegister();
5664 }
5665}
5666
Artem Serov02109dd2016-09-23 17:17:54 +01005667Location LocationsBuilderARMVIXL::ArmEncodableConstantOrRegister(HInstruction* constant,
5668 Opcode opcode) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005669 DCHECK(!DataType::IsFloatingPointType(constant->GetType()));
Artem Serov02109dd2016-09-23 17:17:54 +01005670 if (constant->IsConstant() &&
5671 CanEncodeConstantAsImmediate(constant->AsConstant(), opcode)) {
5672 return Location::ConstantLocation(constant->AsConstant());
5673 }
5674 return Location::RequiresRegister();
5675}
5676
5677bool LocationsBuilderARMVIXL::CanEncodeConstantAsImmediate(HConstant* input_cst,
5678 Opcode opcode) {
5679 uint64_t value = static_cast<uint64_t>(Int64FromConstant(input_cst));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005680 if (DataType::Is64BitType(input_cst->GetType())) {
Artem Serov02109dd2016-09-23 17:17:54 +01005681 Opcode high_opcode = opcode;
5682 SetCc low_set_cc = kCcDontCare;
5683 switch (opcode) {
5684 case SUB:
5685 // Flip the operation to an ADD.
5686 value = -value;
5687 opcode = ADD;
5688 FALLTHROUGH_INTENDED;
5689 case ADD:
5690 if (Low32Bits(value) == 0u) {
5691 return CanEncodeConstantAsImmediate(High32Bits(value), opcode, kCcDontCare);
5692 }
5693 high_opcode = ADC;
5694 low_set_cc = kCcSet;
5695 break;
5696 default:
5697 break;
5698 }
5699 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode, low_set_cc) &&
5700 CanEncodeConstantAsImmediate(High32Bits(value), high_opcode, kCcDontCare);
5701 } else {
5702 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode);
5703 }
5704}
5705
5706// TODO(VIXL): Replace art::arm::SetCc` with `vixl32::FlagsUpdate after flags set optimization
5707// enabled.
5708bool LocationsBuilderARMVIXL::CanEncodeConstantAsImmediate(uint32_t value,
5709 Opcode opcode,
5710 SetCc set_cc) {
5711 ArmVIXLAssembler* assembler = codegen_->GetAssembler();
5712 if (assembler->ShifterOperandCanHold(opcode, value, set_cc)) {
5713 return true;
5714 }
5715 Opcode neg_opcode = kNoOperand;
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00005716 uint32_t neg_value = 0;
Artem Serov02109dd2016-09-23 17:17:54 +01005717 switch (opcode) {
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00005718 case AND: neg_opcode = BIC; neg_value = ~value; break;
5719 case ORR: neg_opcode = ORN; neg_value = ~value; break;
5720 case ADD: neg_opcode = SUB; neg_value = -value; break;
5721 case ADC: neg_opcode = SBC; neg_value = ~value; break;
5722 case SUB: neg_opcode = ADD; neg_value = -value; break;
5723 case SBC: neg_opcode = ADC; neg_value = ~value; break;
5724 case MOV: neg_opcode = MVN; neg_value = ~value; break;
Artem Serov02109dd2016-09-23 17:17:54 +01005725 default:
5726 return false;
5727 }
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00005728
5729 if (assembler->ShifterOperandCanHold(neg_opcode, neg_value, set_cc)) {
5730 return true;
5731 }
5732
5733 return opcode == AND && IsPowerOfTwo(value + 1);
Artem Serov02109dd2016-09-23 17:17:54 +01005734}
5735
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005736void InstructionCodeGeneratorARMVIXL::HandleFieldGet(HInstruction* instruction,
5737 const FieldInfo& field_info) {
5738 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
5739
5740 LocationSummary* locations = instruction->GetLocations();
5741 vixl32::Register base = InputRegisterAt(instruction, 0);
5742 Location out = locations->Out();
5743 bool is_volatile = field_info.IsVolatile();
5744 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005745 DataType::Type field_type = field_info.GetFieldType();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005746 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
5747
5748 switch (field_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005749 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005750 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005751 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005752 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005753 case DataType::Type::kInt16:
5754 case DataType::Type::kInt32: {
5755 LoadOperandType operand_type = GetLoadOperandType(field_type);
5756 GetAssembler()->LoadFromOffset(operand_type, RegisterFrom(out), base, offset);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005757 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005758 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005759
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005760 case DataType::Type::kReference: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005761 // /* HeapReference<Object> */ out = *(base + offset)
5762 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005763 Location temp_loc = locations->GetTemp(0);
5764 // Note that a potential implicit null check is handled in this
5765 // CodeGeneratorARMVIXL::GenerateFieldLoadWithBakerReadBarrier call.
5766 codegen_->GenerateFieldLoadWithBakerReadBarrier(
5767 instruction, out, base, offset, temp_loc, /* needs_null_check */ true);
5768 if (is_volatile) {
5769 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5770 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005771 } else {
5772 GetAssembler()->LoadFromOffset(kLoadWord, RegisterFrom(out), base, offset);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005773 codegen_->MaybeRecordImplicitNullCheck(instruction);
5774 if (is_volatile) {
5775 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5776 }
5777 // If read barriers are enabled, emit read barriers other than
5778 // Baker's using a slow path (and also unpoison the loaded
5779 // reference, if heap poisoning is enabled).
5780 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, locations->InAt(0), offset);
5781 }
5782 break;
5783 }
5784
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005785 case DataType::Type::kInt64:
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005786 if (is_volatile && !atomic_ldrd_strd) {
5787 GenerateWideAtomicLoad(base, offset, LowRegisterFrom(out), HighRegisterFrom(out));
5788 } else {
5789 GetAssembler()->LoadFromOffset(kLoadWordPair, LowRegisterFrom(out), base, offset);
5790 }
5791 break;
5792
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005793 case DataType::Type::kFloat32:
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005794 GetAssembler()->LoadSFromOffset(SRegisterFrom(out), base, offset);
5795 break;
5796
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005797 case DataType::Type::kFloat64: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01005798 vixl32::DRegister out_dreg = DRegisterFrom(out);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005799 if (is_volatile && !atomic_ldrd_strd) {
5800 vixl32::Register lo = RegisterFrom(locations->GetTemp(0));
5801 vixl32::Register hi = RegisterFrom(locations->GetTemp(1));
5802 GenerateWideAtomicLoad(base, offset, lo, hi);
5803 // TODO(VIXL): Do we need to be immediately after the ldrexd instruction? If so we need a
5804 // scope.
5805 codegen_->MaybeRecordImplicitNullCheck(instruction);
5806 __ Vmov(out_dreg, lo, hi);
5807 } else {
5808 GetAssembler()->LoadDFromOffset(out_dreg, base, offset);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005809 codegen_->MaybeRecordImplicitNullCheck(instruction);
5810 }
5811 break;
5812 }
5813
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005814 case DataType::Type::kVoid:
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005815 LOG(FATAL) << "Unreachable type " << field_type;
5816 UNREACHABLE();
5817 }
5818
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005819 if (field_type == DataType::Type::kReference || field_type == DataType::Type::kFloat64) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005820 // Potential implicit null checks, in the case of reference or
5821 // double fields, are handled in the previous switch statement.
5822 } else {
5823 // Address cases other than reference and double that may require an implicit null check.
Alexandre Rames374ddf32016-11-04 10:40:49 +00005824 // TODO(VIXL): Here and for other calls to `MaybeRecordImplicitNullCheck` in this method, we
5825 // should use a scope and the assembler to emit the load instruction to guarantee that we
5826 // record the pc at the correct position. But the `Assembler` does not automatically handle
5827 // unencodable offsets. Practically, everything is fine because the helper and VIXL, at the time
5828 // of writing, do generate the store instruction last.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005829 codegen_->MaybeRecordImplicitNullCheck(instruction);
5830 }
5831
5832 if (is_volatile) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005833 if (field_type == DataType::Type::kReference) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005834 // Memory barriers, in the case of references, are also handled
5835 // in the previous switch statement.
5836 } else {
5837 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5838 }
5839 }
5840}
5841
5842void LocationsBuilderARMVIXL::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
5843 HandleFieldSet(instruction, instruction->GetFieldInfo());
5844}
5845
5846void InstructionCodeGeneratorARMVIXL::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
5847 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
5848}
5849
5850void LocationsBuilderARMVIXL::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5851 HandleFieldGet(instruction, instruction->GetFieldInfo());
5852}
5853
5854void InstructionCodeGeneratorARMVIXL::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5855 HandleFieldGet(instruction, instruction->GetFieldInfo());
5856}
5857
5858void LocationsBuilderARMVIXL::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5859 HandleFieldGet(instruction, instruction->GetFieldInfo());
5860}
5861
5862void InstructionCodeGeneratorARMVIXL::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5863 HandleFieldGet(instruction, instruction->GetFieldInfo());
5864}
5865
Scott Wakelingc34dba72016-10-03 10:14:44 +01005866void LocationsBuilderARMVIXL::VisitStaticFieldSet(HStaticFieldSet* instruction) {
5867 HandleFieldSet(instruction, instruction->GetFieldInfo());
5868}
5869
5870void InstructionCodeGeneratorARMVIXL::VisitStaticFieldSet(HStaticFieldSet* instruction) {
5871 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
5872}
5873
Artem Serovcfbe9132016-10-14 15:58:56 +01005874void LocationsBuilderARMVIXL::VisitUnresolvedInstanceFieldGet(
5875 HUnresolvedInstanceFieldGet* instruction) {
5876 FieldAccessCallingConventionARMVIXL calling_convention;
5877 codegen_->CreateUnresolvedFieldLocationSummary(
5878 instruction, instruction->GetFieldType(), calling_convention);
5879}
5880
5881void InstructionCodeGeneratorARMVIXL::VisitUnresolvedInstanceFieldGet(
5882 HUnresolvedInstanceFieldGet* instruction) {
5883 FieldAccessCallingConventionARMVIXL calling_convention;
5884 codegen_->GenerateUnresolvedFieldAccess(instruction,
5885 instruction->GetFieldType(),
5886 instruction->GetFieldIndex(),
5887 instruction->GetDexPc(),
5888 calling_convention);
5889}
5890
5891void LocationsBuilderARMVIXL::VisitUnresolvedInstanceFieldSet(
5892 HUnresolvedInstanceFieldSet* instruction) {
5893 FieldAccessCallingConventionARMVIXL calling_convention;
5894 codegen_->CreateUnresolvedFieldLocationSummary(
5895 instruction, instruction->GetFieldType(), calling_convention);
5896}
5897
5898void InstructionCodeGeneratorARMVIXL::VisitUnresolvedInstanceFieldSet(
5899 HUnresolvedInstanceFieldSet* instruction) {
5900 FieldAccessCallingConventionARMVIXL calling_convention;
5901 codegen_->GenerateUnresolvedFieldAccess(instruction,
5902 instruction->GetFieldType(),
5903 instruction->GetFieldIndex(),
5904 instruction->GetDexPc(),
5905 calling_convention);
5906}
5907
5908void LocationsBuilderARMVIXL::VisitUnresolvedStaticFieldGet(
5909 HUnresolvedStaticFieldGet* instruction) {
5910 FieldAccessCallingConventionARMVIXL calling_convention;
5911 codegen_->CreateUnresolvedFieldLocationSummary(
5912 instruction, instruction->GetFieldType(), calling_convention);
5913}
5914
5915void InstructionCodeGeneratorARMVIXL::VisitUnresolvedStaticFieldGet(
5916 HUnresolvedStaticFieldGet* instruction) {
5917 FieldAccessCallingConventionARMVIXL calling_convention;
5918 codegen_->GenerateUnresolvedFieldAccess(instruction,
5919 instruction->GetFieldType(),
5920 instruction->GetFieldIndex(),
5921 instruction->GetDexPc(),
5922 calling_convention);
5923}
5924
5925void LocationsBuilderARMVIXL::VisitUnresolvedStaticFieldSet(
5926 HUnresolvedStaticFieldSet* instruction) {
5927 FieldAccessCallingConventionARMVIXL calling_convention;
5928 codegen_->CreateUnresolvedFieldLocationSummary(
5929 instruction, instruction->GetFieldType(), calling_convention);
5930}
5931
5932void InstructionCodeGeneratorARMVIXL::VisitUnresolvedStaticFieldSet(
5933 HUnresolvedStaticFieldSet* instruction) {
5934 FieldAccessCallingConventionARMVIXL calling_convention;
5935 codegen_->GenerateUnresolvedFieldAccess(instruction,
5936 instruction->GetFieldType(),
5937 instruction->GetFieldIndex(),
5938 instruction->GetDexPc(),
5939 calling_convention);
5940}
5941
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005942void LocationsBuilderARMVIXL::VisitNullCheck(HNullCheck* instruction) {
Artem Serov657022c2016-11-23 14:19:38 +00005943 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005944 locations->SetInAt(0, Location::RequiresRegister());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005945}
5946
5947void CodeGeneratorARMVIXL::GenerateImplicitNullCheck(HNullCheck* instruction) {
5948 if (CanMoveNullCheckToUser(instruction)) {
5949 return;
5950 }
5951
5952 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames374ddf32016-11-04 10:40:49 +00005953 // Ensure the pc position is recorded immediately after the `ldr` instruction.
Artem Serov0fb37192016-12-06 18:13:40 +00005954 ExactAssemblyScope aas(GetVIXLAssembler(),
5955 vixl32::kMaxInstructionSizeInBytes,
5956 CodeBufferCheckScope::kMaximumSize);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005957 __ ldr(temps.Acquire(), MemOperand(InputRegisterAt(instruction, 0)));
5958 RecordPcInfo(instruction, instruction->GetDexPc());
5959}
5960
5961void CodeGeneratorARMVIXL::GenerateExplicitNullCheck(HNullCheck* instruction) {
5962 NullCheckSlowPathARMVIXL* slow_path =
5963 new (GetGraph()->GetArena()) NullCheckSlowPathARMVIXL(instruction);
5964 AddSlowPath(slow_path);
xueliang.zhongf51bc622016-11-04 09:23:32 +00005965 __ CompareAndBranchIfZero(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005966}
5967
5968void InstructionCodeGeneratorARMVIXL::VisitNullCheck(HNullCheck* instruction) {
5969 codegen_->GenerateNullCheck(instruction);
5970}
5971
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005972void CodeGeneratorARMVIXL::LoadFromShiftedRegOffset(DataType::Type type,
Scott Wakelingc34dba72016-10-03 10:14:44 +01005973 Location out_loc,
5974 vixl32::Register base,
5975 vixl32::Register reg_index,
5976 vixl32::Condition cond) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005977 uint32_t shift_count = DataType::SizeShift(type);
Scott Wakelingc34dba72016-10-03 10:14:44 +01005978 MemOperand mem_address(base, reg_index, vixl32::LSL, shift_count);
5979
5980 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005981 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005982 case DataType::Type::kInt8:
Scott Wakelingc34dba72016-10-03 10:14:44 +01005983 __ Ldrsb(cond, RegisterFrom(out_loc), mem_address);
5984 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005985 case DataType::Type::kBool:
Scott Wakelingc34dba72016-10-03 10:14:44 +01005986 __ Ldrb(cond, RegisterFrom(out_loc), mem_address);
5987 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005988 case DataType::Type::kUint16:
Scott Wakelingc34dba72016-10-03 10:14:44 +01005989 __ Ldrh(cond, RegisterFrom(out_loc), mem_address);
5990 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005991 case DataType::Type::kInt16:
5992 __ Ldrsh(cond, RegisterFrom(out_loc), mem_address);
5993 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005994 case DataType::Type::kReference:
5995 case DataType::Type::kInt32:
Scott Wakelingc34dba72016-10-03 10:14:44 +01005996 __ Ldr(cond, RegisterFrom(out_loc), mem_address);
5997 break;
5998 // T32 doesn't support LoadFromShiftedRegOffset mem address mode for these types.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005999 case DataType::Type::kInt64:
6000 case DataType::Type::kFloat32:
6001 case DataType::Type::kFloat64:
Scott Wakelingc34dba72016-10-03 10:14:44 +01006002 default:
6003 LOG(FATAL) << "Unreachable type " << type;
6004 UNREACHABLE();
6005 }
6006}
6007
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006008void CodeGeneratorARMVIXL::StoreToShiftedRegOffset(DataType::Type type,
Scott Wakelingc34dba72016-10-03 10:14:44 +01006009 Location loc,
6010 vixl32::Register base,
6011 vixl32::Register reg_index,
6012 vixl32::Condition cond) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006013 uint32_t shift_count = DataType::SizeShift(type);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006014 MemOperand mem_address(base, reg_index, vixl32::LSL, shift_count);
6015
6016 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006017 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006018 case DataType::Type::kUint8:
6019 case DataType::Type::kInt8:
Scott Wakelingc34dba72016-10-03 10:14:44 +01006020 __ Strb(cond, RegisterFrom(loc), mem_address);
6021 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006022 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006023 case DataType::Type::kInt16:
Scott Wakelingc34dba72016-10-03 10:14:44 +01006024 __ Strh(cond, RegisterFrom(loc), mem_address);
6025 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006026 case DataType::Type::kReference:
6027 case DataType::Type::kInt32:
Scott Wakelingc34dba72016-10-03 10:14:44 +01006028 __ Str(cond, RegisterFrom(loc), mem_address);
6029 break;
6030 // T32 doesn't support StoreToShiftedRegOffset mem address mode for these types.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006031 case DataType::Type::kInt64:
6032 case DataType::Type::kFloat32:
6033 case DataType::Type::kFloat64:
Scott Wakelingc34dba72016-10-03 10:14:44 +01006034 default:
6035 LOG(FATAL) << "Unreachable type " << type;
6036 UNREACHABLE();
6037 }
6038}
6039
6040void LocationsBuilderARMVIXL::VisitArrayGet(HArrayGet* instruction) {
6041 bool object_array_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006042 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006043 LocationSummary* locations =
6044 new (GetGraph()->GetArena()) LocationSummary(instruction,
6045 object_array_get_with_read_barrier ?
6046 LocationSummary::kCallOnSlowPath :
6047 LocationSummary::kNoCall);
6048 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006049 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Scott Wakelingc34dba72016-10-03 10:14:44 +01006050 }
6051 locations->SetInAt(0, Location::RequiresRegister());
6052 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006053 if (DataType::IsFloatingPointType(instruction->GetType())) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006054 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
6055 } else {
6056 // The output overlaps in the case of an object array get with
6057 // read barriers enabled: we do not want the move to overwrite the
6058 // array's location, as we need it to emit the read barrier.
6059 locations->SetOut(
6060 Location::RequiresRegister(),
6061 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
6062 }
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01006063 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
6064 // We need a temporary register for the read barrier marking slow
6065 // path in CodeGeneratorARMVIXL::GenerateArrayLoadWithBakerReadBarrier.
6066 if (kBakerReadBarrierLinkTimeThunksEnableForFields &&
6067 !Runtime::Current()->UseJitCompilation() &&
6068 instruction->GetIndex()->IsConstant()) {
6069 // Array loads with constant index are treated as field loads.
6070 // If link-time thunks for the Baker read barrier are enabled, for AOT
6071 // constant index loads we need a temporary only if the offset is too big.
6072 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
6073 uint32_t index = instruction->GetIndex()->AsIntConstant()->GetValue();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006074 offset += index << DataType::SizeShift(DataType::Type::kReference);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01006075 if (offset >= kReferenceLoadMinFarOffset) {
6076 locations->AddTemp(Location::RequiresRegister());
6077 }
6078 // And we always need the reserved entrypoint register.
6079 locations->AddTemp(Location::RegisterLocation(kBakerCcEntrypointRegister.GetCode()));
6080 } else if (kBakerReadBarrierLinkTimeThunksEnableForArrays &&
6081 !Runtime::Current()->UseJitCompilation() &&
6082 !instruction->GetIndex()->IsConstant()) {
6083 // We need a non-scratch temporary for the array data pointer.
6084 locations->AddTemp(Location::RequiresRegister());
6085 // And we always need the reserved entrypoint register.
6086 locations->AddTemp(Location::RegisterLocation(kBakerCcEntrypointRegister.GetCode()));
6087 } else {
6088 locations->AddTemp(Location::RequiresRegister());
6089 }
6090 } else if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
6091 // Also need a temporary for String compression feature.
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006092 locations->AddTemp(Location::RequiresRegister());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006093 }
6094}
6095
6096void InstructionCodeGeneratorARMVIXL::VisitArrayGet(HArrayGet* instruction) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006097 LocationSummary* locations = instruction->GetLocations();
6098 Location obj_loc = locations->InAt(0);
6099 vixl32::Register obj = InputRegisterAt(instruction, 0);
6100 Location index = locations->InAt(1);
6101 Location out_loc = locations->Out();
6102 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006103 DataType::Type type = instruction->GetType();
Scott Wakelingc34dba72016-10-03 10:14:44 +01006104 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
6105 instruction->IsStringCharAt();
6106 HInstruction* array_instr = instruction->GetArray();
6107 bool has_intermediate_address = array_instr->IsIntermediateAddress();
Scott Wakelingc34dba72016-10-03 10:14:44 +01006108
6109 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006110 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006111 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006112 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006113 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006114 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006115 case DataType::Type::kInt32: {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006116 vixl32::Register length;
6117 if (maybe_compressed_char_at) {
6118 length = RegisterFrom(locations->GetTemp(0));
6119 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
6120 GetAssembler()->LoadFromOffset(kLoadWord, length, obj, count_offset);
6121 codegen_->MaybeRecordImplicitNullCheck(instruction);
6122 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006123 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006124 int32_t const_index = Int32ConstantFrom(index);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006125 if (maybe_compressed_char_at) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006126 vixl32::Label uncompressed_load, done;
Anton Kirilov6f644202017-02-27 18:29:45 +00006127 vixl32::Label* final_label = codegen_->GetFinalLabel(instruction, &done);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006128 __ Lsrs(length, length, 1u); // LSRS has a 16-bit encoding, TST (immediate) does not.
6129 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
6130 "Expecting 0=compressed, 1=uncompressed");
Artem Serov517d9f62016-12-12 15:51:15 +00006131 __ B(cs, &uncompressed_load, /* far_target */ false);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006132 GetAssembler()->LoadFromOffset(kLoadUnsignedByte,
6133 RegisterFrom(out_loc),
6134 obj,
6135 data_offset + const_index);
Anton Kirilov6f644202017-02-27 18:29:45 +00006136 __ B(final_label);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006137 __ Bind(&uncompressed_load);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006138 GetAssembler()->LoadFromOffset(GetLoadOperandType(DataType::Type::kUint16),
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006139 RegisterFrom(out_loc),
6140 obj,
6141 data_offset + (const_index << 1));
Anton Kirilov6f644202017-02-27 18:29:45 +00006142 if (done.IsReferenced()) {
6143 __ Bind(&done);
6144 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006145 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006146 uint32_t full_offset = data_offset + (const_index << DataType::SizeShift(type));
Scott Wakelingc34dba72016-10-03 10:14:44 +01006147
6148 LoadOperandType load_type = GetLoadOperandType(type);
6149 GetAssembler()->LoadFromOffset(load_type, RegisterFrom(out_loc), obj, full_offset);
6150 }
6151 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006152 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006153 vixl32::Register temp = temps.Acquire();
6154
6155 if (has_intermediate_address) {
Artem Serov2bbc9532016-10-21 11:51:50 +01006156 // We do not need to compute the intermediate address from the array: the
6157 // input instruction has done it already. See the comment in
6158 // `TryExtractArrayAccessAddress()`.
6159 if (kIsDebugBuild) {
6160 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
Anton Kirilov644032c2016-12-06 17:51:43 +00006161 DCHECK_EQ(Uint64ConstantFrom(tmp->GetOffset()), data_offset);
Artem Serov2bbc9532016-10-21 11:51:50 +01006162 }
6163 temp = obj;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006164 } else {
6165 __ Add(temp, obj, data_offset);
6166 }
6167 if (maybe_compressed_char_at) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006168 vixl32::Label uncompressed_load, done;
Anton Kirilov6f644202017-02-27 18:29:45 +00006169 vixl32::Label* final_label = codegen_->GetFinalLabel(instruction, &done);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006170 __ Lsrs(length, length, 1u); // LSRS has a 16-bit encoding, TST (immediate) does not.
6171 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
6172 "Expecting 0=compressed, 1=uncompressed");
Artem Serov517d9f62016-12-12 15:51:15 +00006173 __ B(cs, &uncompressed_load, /* far_target */ false);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006174 __ Ldrb(RegisterFrom(out_loc), MemOperand(temp, RegisterFrom(index), vixl32::LSL, 0));
Anton Kirilov6f644202017-02-27 18:29:45 +00006175 __ B(final_label);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006176 __ Bind(&uncompressed_load);
6177 __ Ldrh(RegisterFrom(out_loc), MemOperand(temp, RegisterFrom(index), vixl32::LSL, 1));
Anton Kirilov6f644202017-02-27 18:29:45 +00006178 if (done.IsReferenced()) {
6179 __ Bind(&done);
6180 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006181 } else {
6182 codegen_->LoadFromShiftedRegOffset(type, out_loc, temp, RegisterFrom(index));
6183 }
6184 }
6185 break;
6186 }
6187
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006188 case DataType::Type::kReference: {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006189 // The read barrier instrumentation of object ArrayGet
6190 // instructions does not support the HIntermediateAddress
6191 // instruction.
6192 DCHECK(!(has_intermediate_address && kEmitCompilerReadBarrier));
6193
Scott Wakelingc34dba72016-10-03 10:14:44 +01006194 static_assert(
6195 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6196 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
6197 // /* HeapReference<Object> */ out =
6198 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
6199 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006200 Location temp = locations->GetTemp(0);
6201 // Note that a potential implicit null check is handled in this
6202 // CodeGeneratorARMVIXL::GenerateArrayLoadWithBakerReadBarrier call.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01006203 DCHECK(!instruction->CanDoImplicitNullCheckOn(instruction->InputAt(0)));
6204 if (index.IsConstant()) {
6205 // Array load with a constant index can be treated as a field load.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006206 data_offset += Int32ConstantFrom(index) << DataType::SizeShift(type);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01006207 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6208 out_loc,
6209 obj,
6210 data_offset,
6211 locations->GetTemp(0),
6212 /* needs_null_check */ false);
6213 } else {
6214 codegen_->GenerateArrayLoadWithBakerReadBarrier(
6215 instruction, out_loc, obj, data_offset, index, temp, /* needs_null_check */ false);
6216 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006217 } else {
6218 vixl32::Register out = OutputRegister(instruction);
6219 if (index.IsConstant()) {
6220 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00006221 (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006222 GetAssembler()->LoadFromOffset(kLoadWord, out, obj, offset);
Alexandre Rames374ddf32016-11-04 10:40:49 +00006223 // TODO(VIXL): Here and for other calls to `MaybeRecordImplicitNullCheck` in this method,
6224 // we should use a scope and the assembler to emit the load instruction to guarantee that
6225 // we record the pc at the correct position. But the `Assembler` does not automatically
6226 // handle unencodable offsets. Practically, everything is fine because the helper and
6227 // VIXL, at the time of writing, do generate the store instruction last.
Scott Wakelingc34dba72016-10-03 10:14:44 +01006228 codegen_->MaybeRecordImplicitNullCheck(instruction);
6229 // If read barriers are enabled, emit read barriers other than
6230 // Baker's using a slow path (and also unpoison the loaded
6231 // reference, if heap poisoning is enabled).
6232 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
6233 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006234 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006235 vixl32::Register temp = temps.Acquire();
6236
6237 if (has_intermediate_address) {
Artem Serov2bbc9532016-10-21 11:51:50 +01006238 // We do not need to compute the intermediate address from the array: the
6239 // input instruction has done it already. See the comment in
6240 // `TryExtractArrayAccessAddress()`.
6241 if (kIsDebugBuild) {
6242 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
Anton Kirilov644032c2016-12-06 17:51:43 +00006243 DCHECK_EQ(Uint64ConstantFrom(tmp->GetOffset()), data_offset);
Artem Serov2bbc9532016-10-21 11:51:50 +01006244 }
6245 temp = obj;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006246 } else {
6247 __ Add(temp, obj, data_offset);
6248 }
6249 codegen_->LoadFromShiftedRegOffset(type, out_loc, temp, RegisterFrom(index));
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006250 temps.Close();
Alexandre Rames374ddf32016-11-04 10:40:49 +00006251 // TODO(VIXL): Use a scope to ensure that we record the pc position immediately after the
6252 // load instruction. Practically, everything is fine because the helper and VIXL, at the
6253 // time of writing, do generate the store instruction last.
Scott Wakelingc34dba72016-10-03 10:14:44 +01006254 codegen_->MaybeRecordImplicitNullCheck(instruction);
6255 // If read barriers are enabled, emit read barriers other than
6256 // Baker's using a slow path (and also unpoison the loaded
6257 // reference, if heap poisoning is enabled).
6258 codegen_->MaybeGenerateReadBarrierSlow(
6259 instruction, out_loc, out_loc, obj_loc, data_offset, index);
6260 }
6261 }
6262 break;
6263 }
6264
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006265 case DataType::Type::kInt64: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006266 if (index.IsConstant()) {
6267 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00006268 (Int32ConstantFrom(index) << TIMES_8) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006269 GetAssembler()->LoadFromOffset(kLoadWordPair, LowRegisterFrom(out_loc), obj, offset);
6270 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006271 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006272 vixl32::Register temp = temps.Acquire();
6273 __ Add(temp, obj, Operand(RegisterFrom(index), vixl32::LSL, TIMES_8));
6274 GetAssembler()->LoadFromOffset(kLoadWordPair, LowRegisterFrom(out_loc), temp, data_offset);
6275 }
6276 break;
6277 }
6278
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006279 case DataType::Type::kFloat32: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006280 vixl32::SRegister out = SRegisterFrom(out_loc);
6281 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006282 size_t offset = (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006283 GetAssembler()->LoadSFromOffset(out, obj, offset);
6284 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006285 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006286 vixl32::Register temp = temps.Acquire();
6287 __ Add(temp, obj, Operand(RegisterFrom(index), vixl32::LSL, TIMES_4));
6288 GetAssembler()->LoadSFromOffset(out, temp, data_offset);
6289 }
6290 break;
6291 }
6292
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006293 case DataType::Type::kFloat64: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006294 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006295 size_t offset = (Int32ConstantFrom(index) << TIMES_8) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006296 GetAssembler()->LoadDFromOffset(DRegisterFrom(out_loc), obj, offset);
6297 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006298 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006299 vixl32::Register temp = temps.Acquire();
6300 __ Add(temp, obj, Operand(RegisterFrom(index), vixl32::LSL, TIMES_8));
6301 GetAssembler()->LoadDFromOffset(DRegisterFrom(out_loc), temp, data_offset);
6302 }
6303 break;
6304 }
6305
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006306 case DataType::Type::kVoid:
Scott Wakelingc34dba72016-10-03 10:14:44 +01006307 LOG(FATAL) << "Unreachable type " << type;
6308 UNREACHABLE();
6309 }
6310
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006311 if (type == DataType::Type::kReference) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006312 // Potential implicit null checks, in the case of reference
6313 // arrays, are handled in the previous switch statement.
6314 } else if (!maybe_compressed_char_at) {
Alexandre Rames374ddf32016-11-04 10:40:49 +00006315 // TODO(VIXL): Use a scope to ensure we record the pc info immediately after
6316 // the preceding load instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01006317 codegen_->MaybeRecordImplicitNullCheck(instruction);
6318 }
6319}
6320
6321void LocationsBuilderARMVIXL::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006322 DataType::Type value_type = instruction->GetComponentType();
Scott Wakelingc34dba72016-10-03 10:14:44 +01006323
6324 bool needs_write_barrier =
6325 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
6326 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
6327
6328 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
6329 instruction,
6330 may_need_runtime_call_for_type_check ?
6331 LocationSummary::kCallOnSlowPath :
6332 LocationSummary::kNoCall);
6333
6334 locations->SetInAt(0, Location::RequiresRegister());
6335 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006336 if (DataType::IsFloatingPointType(value_type)) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006337 locations->SetInAt(2, Location::RequiresFpuRegister());
6338 } else {
6339 locations->SetInAt(2, Location::RequiresRegister());
6340 }
6341 if (needs_write_barrier) {
6342 // Temporary registers for the write barrier.
6343 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
6344 locations->AddTemp(Location::RequiresRegister());
6345 }
6346}
6347
6348void InstructionCodeGeneratorARMVIXL::VisitArraySet(HArraySet* instruction) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006349 LocationSummary* locations = instruction->GetLocations();
6350 vixl32::Register array = InputRegisterAt(instruction, 0);
6351 Location index = locations->InAt(1);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006352 DataType::Type value_type = instruction->GetComponentType();
Scott Wakelingc34dba72016-10-03 10:14:44 +01006353 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
6354 bool needs_write_barrier =
6355 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
6356 uint32_t data_offset =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006357 mirror::Array::DataOffset(DataType::Size(value_type)).Uint32Value();
Scott Wakelingc34dba72016-10-03 10:14:44 +01006358 Location value_loc = locations->InAt(2);
6359 HInstruction* array_instr = instruction->GetArray();
6360 bool has_intermediate_address = array_instr->IsIntermediateAddress();
Scott Wakelingc34dba72016-10-03 10:14:44 +01006361
6362 switch (value_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006363 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006364 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006365 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006366 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006367 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006368 case DataType::Type::kInt32: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006369 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006370 int32_t const_index = Int32ConstantFrom(index);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006371 uint32_t full_offset =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006372 data_offset + (const_index << DataType::SizeShift(value_type));
Scott Wakelingc34dba72016-10-03 10:14:44 +01006373 StoreOperandType store_type = GetStoreOperandType(value_type);
6374 GetAssembler()->StoreToOffset(store_type, RegisterFrom(value_loc), array, full_offset);
6375 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006376 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006377 vixl32::Register temp = temps.Acquire();
6378
6379 if (has_intermediate_address) {
Artem Serov2bbc9532016-10-21 11:51:50 +01006380 // We do not need to compute the intermediate address from the array: the
6381 // input instruction has done it already. See the comment in
6382 // `TryExtractArrayAccessAddress()`.
6383 if (kIsDebugBuild) {
6384 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
Anton Kirilov644032c2016-12-06 17:51:43 +00006385 DCHECK_EQ(Uint64ConstantFrom(tmp->GetOffset()), data_offset);
Artem Serov2bbc9532016-10-21 11:51:50 +01006386 }
6387 temp = array;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006388 } else {
6389 __ Add(temp, array, data_offset);
6390 }
6391 codegen_->StoreToShiftedRegOffset(value_type, value_loc, temp, RegisterFrom(index));
6392 }
6393 break;
6394 }
6395
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006396 case DataType::Type::kReference: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006397 vixl32::Register value = RegisterFrom(value_loc);
6398 // TryExtractArrayAccessAddress optimization is never applied for non-primitive ArraySet.
6399 // See the comment in instruction_simplifier_shared.cc.
6400 DCHECK(!has_intermediate_address);
6401
6402 if (instruction->InputAt(2)->IsNullConstant()) {
6403 // Just setting null.
6404 if (index.IsConstant()) {
6405 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00006406 (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006407 GetAssembler()->StoreToOffset(kStoreWord, value, array, offset);
6408 } else {
6409 DCHECK(index.IsRegister()) << index;
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006410 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006411 vixl32::Register temp = temps.Acquire();
6412 __ Add(temp, array, data_offset);
6413 codegen_->StoreToShiftedRegOffset(value_type, value_loc, temp, RegisterFrom(index));
6414 }
Alexandre Rames374ddf32016-11-04 10:40:49 +00006415 // TODO(VIXL): Use a scope to ensure we record the pc info immediately after the preceding
6416 // store instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01006417 codegen_->MaybeRecordImplicitNullCheck(instruction);
6418 DCHECK(!needs_write_barrier);
6419 DCHECK(!may_need_runtime_call_for_type_check);
6420 break;
6421 }
6422
6423 DCHECK(needs_write_barrier);
6424 Location temp1_loc = locations->GetTemp(0);
6425 vixl32::Register temp1 = RegisterFrom(temp1_loc);
6426 Location temp2_loc = locations->GetTemp(1);
6427 vixl32::Register temp2 = RegisterFrom(temp2_loc);
6428 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6429 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6430 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6431 vixl32::Label done;
Anton Kirilov6f644202017-02-27 18:29:45 +00006432 vixl32::Label* final_label = codegen_->GetFinalLabel(instruction, &done);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006433 SlowPathCodeARMVIXL* slow_path = nullptr;
6434
6435 if (may_need_runtime_call_for_type_check) {
6436 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARMVIXL(instruction);
6437 codegen_->AddSlowPath(slow_path);
6438 if (instruction->GetValueCanBeNull()) {
6439 vixl32::Label non_zero;
xueliang.zhongf51bc622016-11-04 09:23:32 +00006440 __ CompareAndBranchIfNonZero(value, &non_zero);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006441 if (index.IsConstant()) {
6442 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00006443 (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006444 GetAssembler()->StoreToOffset(kStoreWord, value, array, offset);
6445 } else {
6446 DCHECK(index.IsRegister()) << index;
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006447 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006448 vixl32::Register temp = temps.Acquire();
6449 __ Add(temp, array, data_offset);
6450 codegen_->StoreToShiftedRegOffset(value_type, value_loc, temp, RegisterFrom(index));
6451 }
Alexandre Rames374ddf32016-11-04 10:40:49 +00006452 // TODO(VIXL): Use a scope to ensure we record the pc info immediately after the preceding
6453 // store instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01006454 codegen_->MaybeRecordImplicitNullCheck(instruction);
Anton Kirilov6f644202017-02-27 18:29:45 +00006455 __ B(final_label);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006456 __ Bind(&non_zero);
6457 }
6458
6459 // Note that when read barriers are enabled, the type checks
6460 // are performed without read barriers. This is fine, even in
6461 // the case where a class object is in the from-space after
6462 // the flip, as a comparison involving such a type would not
6463 // produce a false positive; it may of course produce a false
6464 // negative, in which case we would take the ArraySet slow
6465 // path.
6466
Alexandre Rames374ddf32016-11-04 10:40:49 +00006467 {
6468 // Ensure we record the pc position immediately after the `ldr` instruction.
Artem Serov0fb37192016-12-06 18:13:40 +00006469 ExactAssemblyScope aas(GetVIXLAssembler(),
6470 vixl32::kMaxInstructionSizeInBytes,
6471 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00006472 // /* HeapReference<Class> */ temp1 = array->klass_
6473 __ ldr(temp1, MemOperand(array, class_offset));
6474 codegen_->MaybeRecordImplicitNullCheck(instruction);
6475 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006476 GetAssembler()->MaybeUnpoisonHeapReference(temp1);
6477
6478 // /* HeapReference<Class> */ temp1 = temp1->component_type_
6479 GetAssembler()->LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
6480 // /* HeapReference<Class> */ temp2 = value->klass_
6481 GetAssembler()->LoadFromOffset(kLoadWord, temp2, value, class_offset);
6482 // If heap poisoning is enabled, no need to unpoison `temp1`
6483 // nor `temp2`, as we are comparing two poisoned references.
6484 __ Cmp(temp1, temp2);
6485
6486 if (instruction->StaticTypeOfArrayIsObjectArray()) {
6487 vixl32::Label do_put;
Artem Serov517d9f62016-12-12 15:51:15 +00006488 __ B(eq, &do_put, /* far_target */ false);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006489 // If heap poisoning is enabled, the `temp1` reference has
6490 // not been unpoisoned yet; unpoison it now.
6491 GetAssembler()->MaybeUnpoisonHeapReference(temp1);
6492
6493 // /* HeapReference<Class> */ temp1 = temp1->super_class_
6494 GetAssembler()->LoadFromOffset(kLoadWord, temp1, temp1, super_offset);
6495 // If heap poisoning is enabled, no need to unpoison
6496 // `temp1`, as we are comparing against null below.
xueliang.zhongf51bc622016-11-04 09:23:32 +00006497 __ CompareAndBranchIfNonZero(temp1, slow_path->GetEntryLabel());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006498 __ Bind(&do_put);
6499 } else {
6500 __ B(ne, slow_path->GetEntryLabel());
6501 }
6502 }
6503
6504 vixl32::Register source = value;
6505 if (kPoisonHeapReferences) {
6506 // Note that in the case where `value` is a null reference,
6507 // we do not enter this block, as a null reference does not
6508 // need poisoning.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006509 DCHECK_EQ(value_type, DataType::Type::kReference);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006510 __ Mov(temp1, value);
6511 GetAssembler()->PoisonHeapReference(temp1);
6512 source = temp1;
6513 }
6514
6515 if (index.IsConstant()) {
6516 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00006517 (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006518 GetAssembler()->StoreToOffset(kStoreWord, source, array, offset);
6519 } else {
6520 DCHECK(index.IsRegister()) << index;
6521
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006522 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006523 vixl32::Register temp = temps.Acquire();
6524 __ Add(temp, array, data_offset);
6525 codegen_->StoreToShiftedRegOffset(value_type,
6526 LocationFrom(source),
6527 temp,
6528 RegisterFrom(index));
6529 }
6530
6531 if (!may_need_runtime_call_for_type_check) {
Alexandre Rames374ddf32016-11-04 10:40:49 +00006532 // TODO(VIXL): Ensure we record the pc position immediately after the preceding store
6533 // instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01006534 codegen_->MaybeRecordImplicitNullCheck(instruction);
6535 }
6536
6537 codegen_->MarkGCCard(temp1, temp2, array, value, instruction->GetValueCanBeNull());
6538
6539 if (done.IsReferenced()) {
6540 __ Bind(&done);
6541 }
6542
6543 if (slow_path != nullptr) {
6544 __ Bind(slow_path->GetExitLabel());
6545 }
6546
6547 break;
6548 }
6549
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006550 case DataType::Type::kInt64: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006551 Location value = locations->InAt(2);
6552 if (index.IsConstant()) {
6553 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00006554 (Int32ConstantFrom(index) << TIMES_8) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006555 GetAssembler()->StoreToOffset(kStoreWordPair, LowRegisterFrom(value), array, offset);
6556 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006557 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006558 vixl32::Register temp = temps.Acquire();
6559 __ Add(temp, array, Operand(RegisterFrom(index), vixl32::LSL, TIMES_8));
6560 GetAssembler()->StoreToOffset(kStoreWordPair, LowRegisterFrom(value), temp, data_offset);
6561 }
6562 break;
6563 }
6564
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006565 case DataType::Type::kFloat32: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006566 Location value = locations->InAt(2);
6567 DCHECK(value.IsFpuRegister());
6568 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006569 size_t offset = (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006570 GetAssembler()->StoreSToOffset(SRegisterFrom(value), array, offset);
6571 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006572 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006573 vixl32::Register temp = temps.Acquire();
6574 __ Add(temp, array, Operand(RegisterFrom(index), vixl32::LSL, TIMES_4));
6575 GetAssembler()->StoreSToOffset(SRegisterFrom(value), temp, data_offset);
6576 }
6577 break;
6578 }
6579
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006580 case DataType::Type::kFloat64: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006581 Location value = locations->InAt(2);
6582 DCHECK(value.IsFpuRegisterPair());
6583 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006584 size_t offset = (Int32ConstantFrom(index) << TIMES_8) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006585 GetAssembler()->StoreDToOffset(DRegisterFrom(value), array, offset);
6586 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006587 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006588 vixl32::Register temp = temps.Acquire();
6589 __ Add(temp, array, Operand(RegisterFrom(index), vixl32::LSL, TIMES_8));
6590 GetAssembler()->StoreDToOffset(DRegisterFrom(value), temp, data_offset);
6591 }
6592 break;
6593 }
6594
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006595 case DataType::Type::kVoid:
Scott Wakelingc34dba72016-10-03 10:14:44 +01006596 LOG(FATAL) << "Unreachable type " << value_type;
6597 UNREACHABLE();
6598 }
6599
6600 // Objects are handled in the switch.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006601 if (value_type != DataType::Type::kReference) {
Alexandre Rames374ddf32016-11-04 10:40:49 +00006602 // TODO(VIXL): Ensure we record the pc position immediately after the preceding store
6603 // instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01006604 codegen_->MaybeRecordImplicitNullCheck(instruction);
6605 }
6606}
6607
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006608void LocationsBuilderARMVIXL::VisitArrayLength(HArrayLength* instruction) {
6609 LocationSummary* locations =
6610 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6611 locations->SetInAt(0, Location::RequiresRegister());
6612 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6613}
6614
6615void InstructionCodeGeneratorARMVIXL::VisitArrayLength(HArrayLength* instruction) {
6616 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
6617 vixl32::Register obj = InputRegisterAt(instruction, 0);
6618 vixl32::Register out = OutputRegister(instruction);
Alexandre Rames374ddf32016-11-04 10:40:49 +00006619 {
Artem Serov0fb37192016-12-06 18:13:40 +00006620 ExactAssemblyScope aas(GetVIXLAssembler(),
6621 vixl32::kMaxInstructionSizeInBytes,
6622 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00006623 __ ldr(out, MemOperand(obj, offset));
6624 codegen_->MaybeRecordImplicitNullCheck(instruction);
6625 }
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006626 // Mask out compression flag from String's array length.
6627 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006628 __ Lsr(out, out, 1u);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006629 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006630}
6631
Artem Serov2bbc9532016-10-21 11:51:50 +01006632void LocationsBuilderARMVIXL::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Artem Serov2bbc9532016-10-21 11:51:50 +01006633 LocationSummary* locations =
6634 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6635
6636 locations->SetInAt(0, Location::RequiresRegister());
6637 locations->SetInAt(1, Location::RegisterOrConstant(instruction->GetOffset()));
6638 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6639}
6640
6641void InstructionCodeGeneratorARMVIXL::VisitIntermediateAddress(HIntermediateAddress* instruction) {
6642 vixl32::Register out = OutputRegister(instruction);
6643 vixl32::Register first = InputRegisterAt(instruction, 0);
6644 Location second = instruction->GetLocations()->InAt(1);
6645
Artem Serov2bbc9532016-10-21 11:51:50 +01006646 if (second.IsRegister()) {
6647 __ Add(out, first, RegisterFrom(second));
6648 } else {
Anton Kirilov644032c2016-12-06 17:51:43 +00006649 __ Add(out, first, Int32ConstantFrom(second));
Artem Serov2bbc9532016-10-21 11:51:50 +01006650 }
6651}
6652
Artem Serove1811ed2017-04-27 16:50:47 +01006653void LocationsBuilderARMVIXL::VisitIntermediateAddressIndex(
6654 HIntermediateAddressIndex* instruction) {
6655 LOG(FATAL) << "Unreachable " << instruction->GetId();
6656}
6657
6658void InstructionCodeGeneratorARMVIXL::VisitIntermediateAddressIndex(
6659 HIntermediateAddressIndex* instruction) {
6660 LOG(FATAL) << "Unreachable " << instruction->GetId();
6661}
6662
Scott Wakelingc34dba72016-10-03 10:14:44 +01006663void LocationsBuilderARMVIXL::VisitBoundsCheck(HBoundsCheck* instruction) {
6664 RegisterSet caller_saves = RegisterSet::Empty();
6665 InvokeRuntimeCallingConventionARMVIXL calling_convention;
6666 caller_saves.Add(LocationFrom(calling_convention.GetRegisterAt(0)));
6667 caller_saves.Add(LocationFrom(calling_convention.GetRegisterAt(1)));
6668 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Artem Serov2dd053d2017-03-08 14:54:06 +00006669
6670 HInstruction* index = instruction->InputAt(0);
6671 HInstruction* length = instruction->InputAt(1);
6672 // If both index and length are constants we can statically check the bounds. But if at least one
6673 // of them is not encodable ArmEncodableConstantOrRegister will create
6674 // Location::RequiresRegister() which is not desired to happen. Instead we create constant
6675 // locations.
6676 bool both_const = index->IsConstant() && length->IsConstant();
6677 locations->SetInAt(0, both_const
6678 ? Location::ConstantLocation(index->AsConstant())
6679 : ArmEncodableConstantOrRegister(index, CMP));
6680 locations->SetInAt(1, both_const
6681 ? Location::ConstantLocation(length->AsConstant())
6682 : ArmEncodableConstantOrRegister(length, CMP));
Scott Wakelingc34dba72016-10-03 10:14:44 +01006683}
6684
6685void InstructionCodeGeneratorARMVIXL::VisitBoundsCheck(HBoundsCheck* instruction) {
Artem Serov2dd053d2017-03-08 14:54:06 +00006686 LocationSummary* locations = instruction->GetLocations();
6687 Location index_loc = locations->InAt(0);
6688 Location length_loc = locations->InAt(1);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006689
Artem Serov2dd053d2017-03-08 14:54:06 +00006690 if (length_loc.IsConstant()) {
6691 int32_t length = Int32ConstantFrom(length_loc);
6692 if (index_loc.IsConstant()) {
6693 // BCE will remove the bounds check if we are guaranteed to pass.
6694 int32_t index = Int32ConstantFrom(index_loc);
6695 if (index < 0 || index >= length) {
6696 SlowPathCodeARMVIXL* slow_path =
6697 new (GetGraph()->GetArena()) BoundsCheckSlowPathARMVIXL(instruction);
6698 codegen_->AddSlowPath(slow_path);
6699 __ B(slow_path->GetEntryLabel());
6700 } else {
6701 // Some optimization after BCE may have generated this, and we should not
6702 // generate a bounds check if it is a valid range.
6703 }
6704 return;
6705 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006706
Artem Serov2dd053d2017-03-08 14:54:06 +00006707 SlowPathCodeARMVIXL* slow_path =
6708 new (GetGraph()->GetArena()) BoundsCheckSlowPathARMVIXL(instruction);
6709 __ Cmp(RegisterFrom(index_loc), length);
6710 codegen_->AddSlowPath(slow_path);
6711 __ B(hs, slow_path->GetEntryLabel());
6712 } else {
6713 SlowPathCodeARMVIXL* slow_path =
6714 new (GetGraph()->GetArena()) BoundsCheckSlowPathARMVIXL(instruction);
6715 __ Cmp(RegisterFrom(length_loc), InputOperandAt(instruction, 0));
6716 codegen_->AddSlowPath(slow_path);
6717 __ B(ls, slow_path->GetEntryLabel());
6718 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006719}
6720
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006721void CodeGeneratorARMVIXL::MarkGCCard(vixl32::Register temp,
6722 vixl32::Register card,
6723 vixl32::Register object,
6724 vixl32::Register value,
6725 bool can_be_null) {
6726 vixl32::Label is_null;
6727 if (can_be_null) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00006728 __ CompareAndBranchIfZero(value, &is_null);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006729 }
6730 GetAssembler()->LoadFromOffset(
6731 kLoadWord, card, tr, Thread::CardTableOffset<kArmPointerSize>().Int32Value());
Scott Wakelingb77051e2016-11-21 19:46:00 +00006732 __ Lsr(temp, object, Operand::From(gc::accounting::CardTable::kCardShift));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006733 __ Strb(card, MemOperand(card, temp));
6734 if (can_be_null) {
6735 __ Bind(&is_null);
6736 }
6737}
6738
Scott Wakelingfe885462016-09-22 10:24:38 +01006739void LocationsBuilderARMVIXL::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
6740 LOG(FATAL) << "Unreachable";
6741}
6742
6743void InstructionCodeGeneratorARMVIXL::VisitParallelMove(HParallelMove* instruction) {
6744 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
6745}
6746
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006747void LocationsBuilderARMVIXL::VisitSuspendCheck(HSuspendCheck* instruction) {
Artem Serov657022c2016-11-23 14:19:38 +00006748 LocationSummary* locations =
6749 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
6750 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006751}
6752
6753void InstructionCodeGeneratorARMVIXL::VisitSuspendCheck(HSuspendCheck* instruction) {
6754 HBasicBlock* block = instruction->GetBlock();
6755 if (block->GetLoopInformation() != nullptr) {
6756 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
6757 // The back edge will generate the suspend check.
6758 return;
6759 }
6760 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
6761 // The goto will generate the suspend check.
6762 return;
6763 }
6764 GenerateSuspendCheck(instruction, nullptr);
Roland Levillain5daa4952017-07-03 17:23:56 +01006765 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 12);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006766}
6767
6768void InstructionCodeGeneratorARMVIXL::GenerateSuspendCheck(HSuspendCheck* instruction,
6769 HBasicBlock* successor) {
6770 SuspendCheckSlowPathARMVIXL* slow_path =
6771 down_cast<SuspendCheckSlowPathARMVIXL*>(instruction->GetSlowPath());
6772 if (slow_path == nullptr) {
6773 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARMVIXL(instruction, successor);
6774 instruction->SetSlowPath(slow_path);
6775 codegen_->AddSlowPath(slow_path);
6776 if (successor != nullptr) {
6777 DCHECK(successor->IsLoopHeader());
6778 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
6779 }
6780 } else {
6781 DCHECK_EQ(slow_path->GetSuccessor(), successor);
6782 }
6783
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006784 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006785 vixl32::Register temp = temps.Acquire();
6786 GetAssembler()->LoadFromOffset(
6787 kLoadUnsignedHalfword, temp, tr, Thread::ThreadFlagsOffset<kArmPointerSize>().Int32Value());
6788 if (successor == nullptr) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00006789 __ CompareAndBranchIfNonZero(temp, slow_path->GetEntryLabel());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006790 __ Bind(slow_path->GetReturnLabel());
6791 } else {
xueliang.zhongf51bc622016-11-04 09:23:32 +00006792 __ CompareAndBranchIfZero(temp, codegen_->GetLabelOf(successor));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006793 __ B(slow_path->GetEntryLabel());
6794 }
6795}
6796
Scott Wakelingfe885462016-09-22 10:24:38 +01006797ArmVIXLAssembler* ParallelMoveResolverARMVIXL::GetAssembler() const {
6798 return codegen_->GetAssembler();
6799}
6800
6801void ParallelMoveResolverARMVIXL::EmitMove(size_t index) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006802 UseScratchRegisterScope temps(GetAssembler()->GetVIXLAssembler());
Scott Wakelingfe885462016-09-22 10:24:38 +01006803 MoveOperands* move = moves_[index];
6804 Location source = move->GetSource();
6805 Location destination = move->GetDestination();
6806
6807 if (source.IsRegister()) {
6808 if (destination.IsRegister()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006809 __ Mov(RegisterFrom(destination), RegisterFrom(source));
Scott Wakelingfe885462016-09-22 10:24:38 +01006810 } else if (destination.IsFpuRegister()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006811 __ Vmov(SRegisterFrom(destination), RegisterFrom(source));
Scott Wakelingfe885462016-09-22 10:24:38 +01006812 } else {
6813 DCHECK(destination.IsStackSlot());
6814 GetAssembler()->StoreToOffset(kStoreWord,
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006815 RegisterFrom(source),
Scott Wakelingfe885462016-09-22 10:24:38 +01006816 sp,
6817 destination.GetStackIndex());
6818 }
6819 } else if (source.IsStackSlot()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006820 if (destination.IsRegister()) {
6821 GetAssembler()->LoadFromOffset(kLoadWord,
6822 RegisterFrom(destination),
6823 sp,
6824 source.GetStackIndex());
6825 } else if (destination.IsFpuRegister()) {
6826 GetAssembler()->LoadSFromOffset(SRegisterFrom(destination), sp, source.GetStackIndex());
6827 } else {
6828 DCHECK(destination.IsStackSlot());
6829 vixl32::Register temp = temps.Acquire();
6830 GetAssembler()->LoadFromOffset(kLoadWord, temp, sp, source.GetStackIndex());
6831 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
6832 }
Scott Wakelingfe885462016-09-22 10:24:38 +01006833 } else if (source.IsFpuRegister()) {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01006834 if (destination.IsRegister()) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006835 __ Vmov(RegisterFrom(destination), SRegisterFrom(source));
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01006836 } else if (destination.IsFpuRegister()) {
6837 __ Vmov(SRegisterFrom(destination), SRegisterFrom(source));
6838 } else {
6839 DCHECK(destination.IsStackSlot());
6840 GetAssembler()->StoreSToOffset(SRegisterFrom(source), sp, destination.GetStackIndex());
6841 }
Scott Wakelingfe885462016-09-22 10:24:38 +01006842 } else if (source.IsDoubleStackSlot()) {
Alexandre Rames9c19bd62016-10-24 11:50:32 +01006843 if (destination.IsDoubleStackSlot()) {
6844 vixl32::DRegister temp = temps.AcquireD();
6845 GetAssembler()->LoadDFromOffset(temp, sp, source.GetStackIndex());
6846 GetAssembler()->StoreDToOffset(temp, sp, destination.GetStackIndex());
6847 } else if (destination.IsRegisterPair()) {
6848 DCHECK(ExpectedPairLayout(destination));
6849 GetAssembler()->LoadFromOffset(
6850 kLoadWordPair, LowRegisterFrom(destination), sp, source.GetStackIndex());
6851 } else {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01006852 DCHECK(destination.IsFpuRegisterPair()) << destination;
6853 GetAssembler()->LoadDFromOffset(DRegisterFrom(destination), sp, source.GetStackIndex());
Alexandre Rames9c19bd62016-10-24 11:50:32 +01006854 }
Scott Wakelingfe885462016-09-22 10:24:38 +01006855 } else if (source.IsRegisterPair()) {
6856 if (destination.IsRegisterPair()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006857 __ Mov(LowRegisterFrom(destination), LowRegisterFrom(source));
6858 __ Mov(HighRegisterFrom(destination), HighRegisterFrom(source));
Scott Wakelingfe885462016-09-22 10:24:38 +01006859 } else if (destination.IsFpuRegisterPair()) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006860 __ Vmov(DRegisterFrom(destination), LowRegisterFrom(source), HighRegisterFrom(source));
Scott Wakelingfe885462016-09-22 10:24:38 +01006861 } else {
6862 DCHECK(destination.IsDoubleStackSlot()) << destination;
6863 DCHECK(ExpectedPairLayout(source));
6864 GetAssembler()->StoreToOffset(kStoreWordPair,
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006865 LowRegisterFrom(source),
Scott Wakelingfe885462016-09-22 10:24:38 +01006866 sp,
6867 destination.GetStackIndex());
6868 }
6869 } else if (source.IsFpuRegisterPair()) {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01006870 if (destination.IsRegisterPair()) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006871 __ Vmov(LowRegisterFrom(destination), HighRegisterFrom(destination), DRegisterFrom(source));
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01006872 } else if (destination.IsFpuRegisterPair()) {
6873 __ Vmov(DRegisterFrom(destination), DRegisterFrom(source));
6874 } else {
6875 DCHECK(destination.IsDoubleStackSlot()) << destination;
6876 GetAssembler()->StoreDToOffset(DRegisterFrom(source), sp, destination.GetStackIndex());
6877 }
Scott Wakelingfe885462016-09-22 10:24:38 +01006878 } else {
6879 DCHECK(source.IsConstant()) << source;
6880 HConstant* constant = source.GetConstant();
6881 if (constant->IsIntConstant() || constant->IsNullConstant()) {
6882 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
6883 if (destination.IsRegister()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006884 __ Mov(RegisterFrom(destination), value);
Scott Wakelingfe885462016-09-22 10:24:38 +01006885 } else {
6886 DCHECK(destination.IsStackSlot());
Scott Wakelingfe885462016-09-22 10:24:38 +01006887 vixl32::Register temp = temps.Acquire();
6888 __ Mov(temp, value);
6889 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
6890 }
6891 } else if (constant->IsLongConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006892 int64_t value = Int64ConstantFrom(source);
Scott Wakelingfe885462016-09-22 10:24:38 +01006893 if (destination.IsRegisterPair()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006894 __ Mov(LowRegisterFrom(destination), Low32Bits(value));
6895 __ Mov(HighRegisterFrom(destination), High32Bits(value));
Scott Wakelingfe885462016-09-22 10:24:38 +01006896 } else {
6897 DCHECK(destination.IsDoubleStackSlot()) << destination;
Scott Wakelingfe885462016-09-22 10:24:38 +01006898 vixl32::Register temp = temps.Acquire();
6899 __ Mov(temp, Low32Bits(value));
6900 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
6901 __ Mov(temp, High32Bits(value));
6902 GetAssembler()->StoreToOffset(kStoreWord,
6903 temp,
6904 sp,
6905 destination.GetHighStackIndex(kArmWordSize));
6906 }
6907 } else if (constant->IsDoubleConstant()) {
6908 double value = constant->AsDoubleConstant()->GetValue();
6909 if (destination.IsFpuRegisterPair()) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006910 __ Vmov(DRegisterFrom(destination), value);
Scott Wakelingfe885462016-09-22 10:24:38 +01006911 } else {
6912 DCHECK(destination.IsDoubleStackSlot()) << destination;
6913 uint64_t int_value = bit_cast<uint64_t, double>(value);
Scott Wakelingfe885462016-09-22 10:24:38 +01006914 vixl32::Register temp = temps.Acquire();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006915 __ Mov(temp, Low32Bits(int_value));
Scott Wakelingfe885462016-09-22 10:24:38 +01006916 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006917 __ Mov(temp, High32Bits(int_value));
Scott Wakelingfe885462016-09-22 10:24:38 +01006918 GetAssembler()->StoreToOffset(kStoreWord,
6919 temp,
6920 sp,
6921 destination.GetHighStackIndex(kArmWordSize));
6922 }
6923 } else {
6924 DCHECK(constant->IsFloatConstant()) << constant->DebugName();
6925 float value = constant->AsFloatConstant()->GetValue();
6926 if (destination.IsFpuRegister()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006927 __ Vmov(SRegisterFrom(destination), value);
Scott Wakelingfe885462016-09-22 10:24:38 +01006928 } else {
6929 DCHECK(destination.IsStackSlot());
Scott Wakelingfe885462016-09-22 10:24:38 +01006930 vixl32::Register temp = temps.Acquire();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006931 __ Mov(temp, bit_cast<int32_t, float>(value));
Scott Wakelingfe885462016-09-22 10:24:38 +01006932 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
6933 }
6934 }
6935 }
6936}
6937
Alexandre Rames9c19bd62016-10-24 11:50:32 +01006938void ParallelMoveResolverARMVIXL::Exchange(vixl32::Register reg, int mem) {
6939 UseScratchRegisterScope temps(GetAssembler()->GetVIXLAssembler());
6940 vixl32::Register temp = temps.Acquire();
6941 __ Mov(temp, reg);
6942 GetAssembler()->LoadFromOffset(kLoadWord, reg, sp, mem);
6943 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, mem);
Scott Wakelingfe885462016-09-22 10:24:38 +01006944}
6945
Alexandre Rames9c19bd62016-10-24 11:50:32 +01006946void ParallelMoveResolverARMVIXL::Exchange(int mem1, int mem2) {
6947 // TODO(VIXL32): Double check the performance of this implementation.
6948 UseScratchRegisterScope temps(GetAssembler()->GetVIXLAssembler());
Nicolas Geoffray13a797b2017-03-15 16:41:31 +00006949 vixl32::Register temp1 = temps.Acquire();
6950 ScratchRegisterScope ensure_scratch(
6951 this, temp1.GetCode(), r0.GetCode(), codegen_->GetNumberOfCoreRegisters());
6952 vixl32::Register temp2(ensure_scratch.GetRegister());
Alexandre Rames9c19bd62016-10-24 11:50:32 +01006953
Nicolas Geoffray13a797b2017-03-15 16:41:31 +00006954 int stack_offset = ensure_scratch.IsSpilled() ? kArmWordSize : 0;
6955 GetAssembler()->LoadFromOffset(kLoadWord, temp1, sp, mem1 + stack_offset);
6956 GetAssembler()->LoadFromOffset(kLoadWord, temp2, sp, mem2 + stack_offset);
6957 GetAssembler()->StoreToOffset(kStoreWord, temp1, sp, mem2 + stack_offset);
6958 GetAssembler()->StoreToOffset(kStoreWord, temp2, sp, mem1 + stack_offset);
Scott Wakelingfe885462016-09-22 10:24:38 +01006959}
6960
Alexandre Rames9c19bd62016-10-24 11:50:32 +01006961void ParallelMoveResolverARMVIXL::EmitSwap(size_t index) {
6962 MoveOperands* move = moves_[index];
6963 Location source = move->GetSource();
6964 Location destination = move->GetDestination();
6965 UseScratchRegisterScope temps(GetAssembler()->GetVIXLAssembler());
6966
6967 if (source.IsRegister() && destination.IsRegister()) {
6968 vixl32::Register temp = temps.Acquire();
6969 DCHECK(!RegisterFrom(source).Is(temp));
6970 DCHECK(!RegisterFrom(destination).Is(temp));
6971 __ Mov(temp, RegisterFrom(destination));
6972 __ Mov(RegisterFrom(destination), RegisterFrom(source));
6973 __ Mov(RegisterFrom(source), temp);
6974 } else if (source.IsRegister() && destination.IsStackSlot()) {
6975 Exchange(RegisterFrom(source), destination.GetStackIndex());
6976 } else if (source.IsStackSlot() && destination.IsRegister()) {
6977 Exchange(RegisterFrom(destination), source.GetStackIndex());
6978 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Anton Kirilovdda43962016-11-21 19:55:20 +00006979 Exchange(source.GetStackIndex(), destination.GetStackIndex());
Alexandre Rames9c19bd62016-10-24 11:50:32 +01006980 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Nicolas Geoffray13a797b2017-03-15 16:41:31 +00006981 vixl32::Register temp = temps.Acquire();
Anton Kirilovdda43962016-11-21 19:55:20 +00006982 __ Vmov(temp, SRegisterFrom(source));
6983 __ Vmov(SRegisterFrom(source), SRegisterFrom(destination));
6984 __ Vmov(SRegisterFrom(destination), temp);
Alexandre Rames9c19bd62016-10-24 11:50:32 +01006985 } else if (source.IsRegisterPair() && destination.IsRegisterPair()) {
6986 vixl32::DRegister temp = temps.AcquireD();
6987 __ Vmov(temp, LowRegisterFrom(source), HighRegisterFrom(source));
6988 __ Mov(LowRegisterFrom(source), LowRegisterFrom(destination));
6989 __ Mov(HighRegisterFrom(source), HighRegisterFrom(destination));
6990 __ Vmov(LowRegisterFrom(destination), HighRegisterFrom(destination), temp);
6991 } else if (source.IsRegisterPair() || destination.IsRegisterPair()) {
6992 vixl32::Register low_reg = LowRegisterFrom(source.IsRegisterPair() ? source : destination);
6993 int mem = source.IsRegisterPair() ? destination.GetStackIndex() : source.GetStackIndex();
6994 DCHECK(ExpectedPairLayout(source.IsRegisterPair() ? source : destination));
6995 vixl32::DRegister temp = temps.AcquireD();
6996 __ Vmov(temp, low_reg, vixl32::Register(low_reg.GetCode() + 1));
6997 GetAssembler()->LoadFromOffset(kLoadWordPair, low_reg, sp, mem);
6998 GetAssembler()->StoreDToOffset(temp, sp, mem);
6999 } else if (source.IsFpuRegisterPair() && destination.IsFpuRegisterPair()) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007000 vixl32::DRegister first = DRegisterFrom(source);
7001 vixl32::DRegister second = DRegisterFrom(destination);
7002 vixl32::DRegister temp = temps.AcquireD();
7003 __ Vmov(temp, first);
7004 __ Vmov(first, second);
7005 __ Vmov(second, temp);
Alexandre Rames9c19bd62016-10-24 11:50:32 +01007006 } else if (source.IsFpuRegisterPair() || destination.IsFpuRegisterPair()) {
Anton Kirilovdda43962016-11-21 19:55:20 +00007007 vixl32::DRegister reg = source.IsFpuRegisterPair()
7008 ? DRegisterFrom(source)
7009 : DRegisterFrom(destination);
7010 int mem = source.IsFpuRegisterPair()
7011 ? destination.GetStackIndex()
7012 : source.GetStackIndex();
7013 vixl32::DRegister temp = temps.AcquireD();
7014 __ Vmov(temp, reg);
7015 GetAssembler()->LoadDFromOffset(reg, sp, mem);
7016 GetAssembler()->StoreDToOffset(temp, sp, mem);
Alexandre Rames9c19bd62016-10-24 11:50:32 +01007017 } else if (source.IsFpuRegister() || destination.IsFpuRegister()) {
Anton Kirilovdda43962016-11-21 19:55:20 +00007018 vixl32::SRegister reg = source.IsFpuRegister()
7019 ? SRegisterFrom(source)
7020 : SRegisterFrom(destination);
7021 int mem = source.IsFpuRegister()
7022 ? destination.GetStackIndex()
7023 : source.GetStackIndex();
7024 vixl32::Register temp = temps.Acquire();
7025 __ Vmov(temp, reg);
7026 GetAssembler()->LoadSFromOffset(reg, sp, mem);
7027 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, mem);
Alexandre Rames9c19bd62016-10-24 11:50:32 +01007028 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
7029 vixl32::DRegister temp1 = temps.AcquireD();
7030 vixl32::DRegister temp2 = temps.AcquireD();
7031 __ Vldr(temp1, MemOperand(sp, source.GetStackIndex()));
7032 __ Vldr(temp2, MemOperand(sp, destination.GetStackIndex()));
7033 __ Vstr(temp1, MemOperand(sp, destination.GetStackIndex()));
7034 __ Vstr(temp2, MemOperand(sp, source.GetStackIndex()));
7035 } else {
7036 LOG(FATAL) << "Unimplemented" << source << " <-> " << destination;
7037 }
Scott Wakelingfe885462016-09-22 10:24:38 +01007038}
7039
Nicolas Geoffray13a797b2017-03-15 16:41:31 +00007040void ParallelMoveResolverARMVIXL::SpillScratch(int reg) {
7041 __ Push(vixl32::Register(reg));
Scott Wakelingfe885462016-09-22 10:24:38 +01007042}
7043
Nicolas Geoffray13a797b2017-03-15 16:41:31 +00007044void ParallelMoveResolverARMVIXL::RestoreScratch(int reg) {
7045 __ Pop(vixl32::Register(reg));
Scott Wakelingfe885462016-09-22 10:24:38 +01007046}
7047
Artem Serov02d37832016-10-25 15:25:33 +01007048HLoadClass::LoadKind CodeGeneratorARMVIXL::GetSupportedLoadClassKind(
Artem Serovd4cc5b22016-11-04 11:19:09 +00007049 HLoadClass::LoadKind desired_class_load_kind) {
7050 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00007051 case HLoadClass::LoadKind::kInvalid:
7052 LOG(FATAL) << "UNREACHABLE";
7053 UNREACHABLE();
Artem Serovd4cc5b22016-11-04 11:19:09 +00007054 case HLoadClass::LoadKind::kReferrersClass:
7055 break;
Artem Serovd4cc5b22016-11-04 11:19:09 +00007056 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko94ec2db2017-09-06 17:21:03 +01007057 case HLoadClass::LoadKind::kBootImageClassTable:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007058 case HLoadClass::LoadKind::kBssEntry:
7059 DCHECK(!Runtime::Current()->UseJitCompilation());
7060 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007061 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007062 DCHECK(Runtime::Current()->UseJitCompilation());
Artem Serovc5fcb442016-12-02 19:19:58 +00007063 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01007064 case HLoadClass::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007065 case HLoadClass::LoadKind::kRuntimeCall:
Artem Serovd4cc5b22016-11-04 11:19:09 +00007066 break;
7067 }
7068 return desired_class_load_kind;
Artem Serov02d37832016-10-25 15:25:33 +01007069}
7070
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007071void LocationsBuilderARMVIXL::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00007072 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007073 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007074 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00007075 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007076 cls,
7077 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00007078 LocationFrom(r0));
Vladimir Markoea4c1262017-02-06 19:59:33 +00007079 DCHECK(calling_convention.GetRegisterAt(0).Is(r0));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007080 return;
7081 }
Vladimir Marko41559982017-01-06 14:04:23 +00007082 DCHECK(!cls->NeedsAccessCheck());
Scott Wakelingfe885462016-09-22 10:24:38 +01007083
Artem Serovd4cc5b22016-11-04 11:19:09 +00007084 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
7085 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007086 ? LocationSummary::kCallOnSlowPath
7087 : LocationSummary::kNoCall;
7088 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Artem Serovd4cc5b22016-11-04 11:19:09 +00007089 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007090 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Artem Serovd4cc5b22016-11-04 11:19:09 +00007091 }
7092
Vladimir Marko41559982017-01-06 14:04:23 +00007093 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007094 locations->SetInAt(0, Location::RequiresRegister());
7095 }
7096 locations->SetOut(Location::RequiresRegister());
Vladimir Markoea4c1262017-02-06 19:59:33 +00007097 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
7098 if (!kUseReadBarrier || kUseBakerReadBarrier) {
7099 // Rely on the type resolution or initialization and marking to save everything we need.
7100 // Note that IP may be clobbered by saving/restoring the live register (only one thanks
7101 // to the custom calling convention) or by marking, so we request a different temp.
7102 locations->AddTemp(Location::RequiresRegister());
7103 RegisterSet caller_saves = RegisterSet::Empty();
7104 InvokeRuntimeCallingConventionARMVIXL calling_convention;
7105 caller_saves.Add(LocationFrom(calling_convention.GetRegisterAt(0)));
7106 // TODO: Add GetReturnLocation() to the calling convention so that we can DCHECK()
7107 // that the the kPrimNot result register is the same as the first argument register.
7108 locations->SetCustomSlowPathCallerSaves(caller_saves);
7109 } else {
7110 // For non-Baker read barrier we have a temp-clobbering call.
7111 }
7112 }
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01007113 if (kUseBakerReadBarrier && kBakerReadBarrierLinkTimeThunksEnableForGcRoots) {
7114 if (load_kind == HLoadClass::LoadKind::kBssEntry ||
7115 (load_kind == HLoadClass::LoadKind::kReferrersClass &&
7116 !Runtime::Current()->UseJitCompilation())) {
7117 locations->AddTemp(Location::RegisterLocation(kBakerCcEntrypointRegister.GetCode()));
7118 }
7119 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007120}
7121
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007122// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
7123// move.
7124void InstructionCodeGeneratorARMVIXL::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00007125 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007126 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00007127 codegen_->GenerateLoadClassRuntimeCall(cls);
Roland Levillain5daa4952017-07-03 17:23:56 +01007128 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 13);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007129 return;
7130 }
Vladimir Marko41559982017-01-06 14:04:23 +00007131 DCHECK(!cls->NeedsAccessCheck());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007132
Vladimir Marko41559982017-01-06 14:04:23 +00007133 LocationSummary* locations = cls->GetLocations();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007134 Location out_loc = locations->Out();
7135 vixl32::Register out = OutputRegister(cls);
7136
Artem Serovd4cc5b22016-11-04 11:19:09 +00007137 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
7138 ? kWithoutReadBarrier
7139 : kCompilerReadBarrierOption;
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007140 bool generate_null_check = false;
Vladimir Marko41559982017-01-06 14:04:23 +00007141 switch (load_kind) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007142 case HLoadClass::LoadKind::kReferrersClass: {
7143 DCHECK(!cls->CanCallRuntime());
7144 DCHECK(!cls->MustGenerateClinitCheck());
7145 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
7146 vixl32::Register current_method = InputRegisterAt(cls, 0);
7147 GenerateGcRootFieldLoad(cls,
7148 out_loc,
7149 current_method,
Roland Levillain00468f32016-10-27 18:02:48 +01007150 ArtMethod::DeclaringClassOffset().Int32Value(),
Artem Serovd4cc5b22016-11-04 11:19:09 +00007151 read_barrier_option);
7152 break;
7153 }
Artem Serovd4cc5b22016-11-04 11:19:09 +00007154 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007155 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Artem Serovd4cc5b22016-11-04 11:19:09 +00007156 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
7157 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
7158 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
7159 codegen_->EmitMovwMovtPlaceholder(labels, out);
7160 break;
7161 }
7162 case HLoadClass::LoadKind::kBootImageAddress: {
Artem Serovc5fcb442016-12-02 19:19:58 +00007163 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007164 uint32_t address = dchecked_integral_cast<uint32_t>(
7165 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
7166 DCHECK_NE(address, 0u);
Artem Serovc5fcb442016-12-02 19:19:58 +00007167 __ Ldr(out, codegen_->DeduplicateBootImageAddressLiteral(address));
Artem Serovd4cc5b22016-11-04 11:19:09 +00007168 break;
7169 }
Vladimir Marko94ec2db2017-09-06 17:21:03 +01007170 case HLoadClass::LoadKind::kBootImageClassTable: {
7171 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
7172 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
7173 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
7174 codegen_->EmitMovwMovtPlaceholder(labels, out);
7175 __ Ldr(out, MemOperand(out, /* offset */ 0));
7176 // Extract the reference from the slot data, i.e. clear the hash bits.
7177 int32_t masked_hash = ClassTable::TableSlot::MaskHash(
7178 ComputeModifiedUtf8Hash(cls->GetDexFile().StringByTypeIdx(cls->GetTypeIndex())));
7179 if (masked_hash != 0) {
7180 __ Sub(out, out, Operand(masked_hash));
7181 }
7182 break;
7183 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007184 case HLoadClass::LoadKind::kBssEntry: {
Vladimir Markoea4c1262017-02-06 19:59:33 +00007185 vixl32::Register temp = (!kUseReadBarrier || kUseBakerReadBarrier)
7186 ? RegisterFrom(locations->GetTemp(0))
7187 : out;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007188 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
Vladimir Marko1998cd02017-01-13 13:02:58 +00007189 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex());
Vladimir Markoea4c1262017-02-06 19:59:33 +00007190 codegen_->EmitMovwMovtPlaceholder(labels, temp);
7191 GenerateGcRootFieldLoad(cls, out_loc, temp, /* offset */ 0, read_barrier_option);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007192 generate_null_check = true;
7193 break;
7194 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007195 case HLoadClass::LoadKind::kJitTableAddress: {
Artem Serovc5fcb442016-12-02 19:19:58 +00007196 __ Ldr(out, codegen_->DeduplicateJitClassLiteral(cls->GetDexFile(),
7197 cls->GetTypeIndex(),
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007198 cls->GetClass()));
Artem Serovc5fcb442016-12-02 19:19:58 +00007199 // /* GcRoot<mirror::Class> */ out = *out
Vladimir Markoea4c1262017-02-06 19:59:33 +00007200 GenerateGcRootFieldLoad(cls, out_loc, out, /* offset */ 0, read_barrier_option);
Artem Serovd4cc5b22016-11-04 11:19:09 +00007201 break;
7202 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007203 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00007204 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00007205 LOG(FATAL) << "UNREACHABLE";
7206 UNREACHABLE();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007207 }
7208
7209 if (generate_null_check || cls->MustGenerateClinitCheck()) {
7210 DCHECK(cls->CanCallRuntime());
7211 LoadClassSlowPathARMVIXL* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARMVIXL(
7212 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
7213 codegen_->AddSlowPath(slow_path);
7214 if (generate_null_check) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00007215 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007216 }
7217 if (cls->MustGenerateClinitCheck()) {
7218 GenerateClassInitializationCheck(slow_path, out);
7219 } else {
7220 __ Bind(slow_path->GetExitLabel());
7221 }
Roland Levillain5daa4952017-07-03 17:23:56 +01007222 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 14);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007223 }
7224}
7225
Artem Serov02d37832016-10-25 15:25:33 +01007226void LocationsBuilderARMVIXL::VisitClinitCheck(HClinitCheck* check) {
7227 LocationSummary* locations =
7228 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
7229 locations->SetInAt(0, Location::RequiresRegister());
7230 if (check->HasUses()) {
7231 locations->SetOut(Location::SameAsFirstInput());
7232 }
7233}
7234
7235void InstructionCodeGeneratorARMVIXL::VisitClinitCheck(HClinitCheck* check) {
7236 // We assume the class is not null.
7237 LoadClassSlowPathARMVIXL* slow_path =
7238 new (GetGraph()->GetArena()) LoadClassSlowPathARMVIXL(check->GetLoadClass(),
7239 check,
7240 check->GetDexPc(),
7241 /* do_clinit */ true);
7242 codegen_->AddSlowPath(slow_path);
7243 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
7244}
7245
7246void InstructionCodeGeneratorARMVIXL::GenerateClassInitializationCheck(
7247 LoadClassSlowPathARMVIXL* slow_path, vixl32::Register class_reg) {
7248 UseScratchRegisterScope temps(GetVIXLAssembler());
7249 vixl32::Register temp = temps.Acquire();
7250 GetAssembler()->LoadFromOffset(kLoadWord,
7251 temp,
7252 class_reg,
7253 mirror::Class::StatusOffset().Int32Value());
7254 __ Cmp(temp, mirror::Class::kStatusInitialized);
7255 __ B(lt, slow_path->GetEntryLabel());
7256 // Even if the initialized flag is set, we may be in a situation where caches are not synced
7257 // properly. Therefore, we do a memory fence.
7258 __ Dmb(ISH);
7259 __ Bind(slow_path->GetExitLabel());
7260}
7261
Artem Serov02d37832016-10-25 15:25:33 +01007262HLoadString::LoadKind CodeGeneratorARMVIXL::GetSupportedLoadStringKind(
Artem Serovd4cc5b22016-11-04 11:19:09 +00007263 HLoadString::LoadKind desired_string_load_kind) {
7264 switch (desired_string_load_kind) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00007265 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007266 case HLoadString::LoadKind::kBootImageInternTable:
Artem Serovd4cc5b22016-11-04 11:19:09 +00007267 case HLoadString::LoadKind::kBssEntry:
7268 DCHECK(!Runtime::Current()->UseJitCompilation());
7269 break;
7270 case HLoadString::LoadKind::kJitTableAddress:
7271 DCHECK(Runtime::Current()->UseJitCompilation());
Artem Serovc5fcb442016-12-02 19:19:58 +00007272 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01007273 case HLoadString::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007274 case HLoadString::LoadKind::kRuntimeCall:
Artem Serovd4cc5b22016-11-04 11:19:09 +00007275 break;
7276 }
7277 return desired_string_load_kind;
Artem Serov02d37832016-10-25 15:25:33 +01007278}
7279
7280void LocationsBuilderARMVIXL::VisitLoadString(HLoadString* load) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00007281 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Artem Serov02d37832016-10-25 15:25:33 +01007282 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Artem Serov02d37832016-10-25 15:25:33 +01007283 HLoadString::LoadKind load_kind = load->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007284 if (load_kind == HLoadString::LoadKind::kRuntimeCall) {
Artem Serov02d37832016-10-25 15:25:33 +01007285 locations->SetOut(LocationFrom(r0));
7286 } else {
7287 locations->SetOut(Location::RequiresRegister());
Artem Serovd4cc5b22016-11-04 11:19:09 +00007288 if (load_kind == HLoadString::LoadKind::kBssEntry) {
7289 if (!kUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00007290 // Rely on the pResolveString and marking to save everything we need, including temps.
7291 // Note that IP may be clobbered by saving/restoring the live register (only one thanks
7292 // to the custom calling convention) or by marking, so we request a different temp.
Artem Serovd4cc5b22016-11-04 11:19:09 +00007293 locations->AddTemp(Location::RequiresRegister());
7294 RegisterSet caller_saves = RegisterSet::Empty();
7295 InvokeRuntimeCallingConventionARMVIXL calling_convention;
7296 caller_saves.Add(LocationFrom(calling_convention.GetRegisterAt(0)));
7297 // TODO: Add GetReturnLocation() to the calling convention so that we can DCHECK()
7298 // that the the kPrimNot result register is the same as the first argument register.
7299 locations->SetCustomSlowPathCallerSaves(caller_saves);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01007300 if (kUseBakerReadBarrier && kBakerReadBarrierLinkTimeThunksEnableForGcRoots) {
7301 locations->AddTemp(Location::RegisterLocation(kBakerCcEntrypointRegister.GetCode()));
7302 }
Artem Serovd4cc5b22016-11-04 11:19:09 +00007303 } else {
7304 // For non-Baker read barrier we have a temp-clobbering call.
7305 }
7306 }
Artem Serov02d37832016-10-25 15:25:33 +01007307 }
7308}
7309
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007310// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
7311// move.
7312void InstructionCodeGeneratorARMVIXL::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Artem Serovd4cc5b22016-11-04 11:19:09 +00007313 LocationSummary* locations = load->GetLocations();
7314 Location out_loc = locations->Out();
7315 vixl32::Register out = OutputRegister(load);
7316 HLoadString::LoadKind load_kind = load->GetLoadKind();
7317
7318 switch (load_kind) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00007319 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
7320 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
7321 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007322 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Artem Serovd4cc5b22016-11-04 11:19:09 +00007323 codegen_->EmitMovwMovtPlaceholder(labels, out);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007324 return;
Artem Serovd4cc5b22016-11-04 11:19:09 +00007325 }
7326 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007327 uint32_t address = dchecked_integral_cast<uint32_t>(
7328 reinterpret_cast<uintptr_t>(load->GetString().Get()));
7329 DCHECK_NE(address, 0u);
Artem Serovc5fcb442016-12-02 19:19:58 +00007330 __ Ldr(out, codegen_->DeduplicateBootImageAddressLiteral(address));
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007331 return;
7332 }
7333 case HLoadString::LoadKind::kBootImageInternTable: {
7334 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
7335 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
7336 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
7337 codegen_->EmitMovwMovtPlaceholder(labels, out);
7338 __ Ldr(out, MemOperand(out, /* offset */ 0));
7339 return;
Artem Serovd4cc5b22016-11-04 11:19:09 +00007340 }
7341 case HLoadString::LoadKind::kBssEntry: {
7342 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markoea4c1262017-02-06 19:59:33 +00007343 vixl32::Register temp = (!kUseReadBarrier || kUseBakerReadBarrier)
7344 ? RegisterFrom(locations->GetTemp(0))
7345 : out;
Artem Serovd4cc5b22016-11-04 11:19:09 +00007346 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007347 codegen_->NewStringBssEntryPatch(load->GetDexFile(), load->GetStringIndex());
Artem Serovd4cc5b22016-11-04 11:19:09 +00007348 codegen_->EmitMovwMovtPlaceholder(labels, temp);
7349 GenerateGcRootFieldLoad(load, out_loc, temp, /* offset */ 0, kCompilerReadBarrierOption);
7350 LoadStringSlowPathARMVIXL* slow_path =
7351 new (GetGraph()->GetArena()) LoadStringSlowPathARMVIXL(load);
7352 codegen_->AddSlowPath(slow_path);
7353 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
7354 __ Bind(slow_path->GetExitLabel());
Roland Levillain5daa4952017-07-03 17:23:56 +01007355 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 15);
Artem Serovd4cc5b22016-11-04 11:19:09 +00007356 return;
7357 }
7358 case HLoadString::LoadKind::kJitTableAddress: {
Artem Serovc5fcb442016-12-02 19:19:58 +00007359 __ Ldr(out, codegen_->DeduplicateJitStringLiteral(load->GetDexFile(),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007360 load->GetStringIndex(),
7361 load->GetString()));
Artem Serovc5fcb442016-12-02 19:19:58 +00007362 // /* GcRoot<mirror::String> */ out = *out
7363 GenerateGcRootFieldLoad(load, out_loc, out, /* offset */ 0, kCompilerReadBarrierOption);
7364 return;
Artem Serovd4cc5b22016-11-04 11:19:09 +00007365 }
7366 default:
7367 break;
7368 }
Artem Serov02d37832016-10-25 15:25:33 +01007369
7370 // TODO: Re-add the compiler code to do string dex cache lookup again.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007371 DCHECK_EQ(load->GetLoadKind(), HLoadString::LoadKind::kRuntimeCall);
Artem Serov02d37832016-10-25 15:25:33 +01007372 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Andreas Gampe8a0128a2016-11-28 07:38:35 -08007373 __ Mov(calling_convention.GetRegisterAt(0), load->GetStringIndex().index_);
Artem Serov02d37832016-10-25 15:25:33 +01007374 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
7375 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Roland Levillain5daa4952017-07-03 17:23:56 +01007376 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 16);
Artem Serov02d37832016-10-25 15:25:33 +01007377}
7378
7379static int32_t GetExceptionTlsOffset() {
7380 return Thread::ExceptionOffset<kArmPointerSize>().Int32Value();
7381}
7382
7383void LocationsBuilderARMVIXL::VisitLoadException(HLoadException* load) {
7384 LocationSummary* locations =
7385 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
7386 locations->SetOut(Location::RequiresRegister());
7387}
7388
7389void InstructionCodeGeneratorARMVIXL::VisitLoadException(HLoadException* load) {
7390 vixl32::Register out = OutputRegister(load);
7391 GetAssembler()->LoadFromOffset(kLoadWord, out, tr, GetExceptionTlsOffset());
7392}
7393
7394
7395void LocationsBuilderARMVIXL::VisitClearException(HClearException* clear) {
7396 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
7397}
7398
7399void InstructionCodeGeneratorARMVIXL::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
7400 UseScratchRegisterScope temps(GetVIXLAssembler());
7401 vixl32::Register temp = temps.Acquire();
7402 __ Mov(temp, 0);
7403 GetAssembler()->StoreToOffset(kStoreWord, temp, tr, GetExceptionTlsOffset());
7404}
7405
7406void LocationsBuilderARMVIXL::VisitThrow(HThrow* instruction) {
7407 LocationSummary* locations =
7408 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
7409 InvokeRuntimeCallingConventionARMVIXL calling_convention;
7410 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
7411}
7412
7413void InstructionCodeGeneratorARMVIXL::VisitThrow(HThrow* instruction) {
7414 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
7415 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
7416}
7417
Artem Serov657022c2016-11-23 14:19:38 +00007418// Temp is used for read barrier.
7419static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
7420 if (kEmitCompilerReadBarrier &&
7421 (kUseBakerReadBarrier ||
7422 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
7423 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
7424 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
7425 return 1;
7426 }
7427 return 0;
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007428}
7429
Artem Serov657022c2016-11-23 14:19:38 +00007430// Interface case has 3 temps, one for holding the number of interfaces, one for the current
7431// interface pointer, one for loading the current interface.
7432// The other checks have one temp for loading the object's class.
7433static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
7434 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
7435 return 3;
7436 }
7437 return 1 + NumberOfInstanceOfTemps(type_check_kind);
7438}
Artem Serovcfbe9132016-10-14 15:58:56 +01007439
7440void LocationsBuilderARMVIXL::VisitInstanceOf(HInstanceOf* instruction) {
7441 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
7442 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
7443 bool baker_read_barrier_slow_path = false;
7444 switch (type_check_kind) {
7445 case TypeCheckKind::kExactCheck:
7446 case TypeCheckKind::kAbstractClassCheck:
7447 case TypeCheckKind::kClassHierarchyCheck:
7448 case TypeCheckKind::kArrayObjectCheck:
7449 call_kind =
7450 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
7451 baker_read_barrier_slow_path = kUseBakerReadBarrier;
7452 break;
7453 case TypeCheckKind::kArrayCheck:
7454 case TypeCheckKind::kUnresolvedCheck:
7455 case TypeCheckKind::kInterfaceCheck:
7456 call_kind = LocationSummary::kCallOnSlowPath;
7457 break;
7458 }
7459
7460 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
7461 if (baker_read_barrier_slow_path) {
7462 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
7463 }
7464 locations->SetInAt(0, Location::RequiresRegister());
7465 locations->SetInAt(1, Location::RequiresRegister());
7466 // The "out" register is used as a temporary, so it overlaps with the inputs.
7467 // Note that TypeCheckSlowPathARM uses this register too.
7468 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Artem Serov657022c2016-11-23 14:19:38 +00007469 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01007470 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
7471 codegen_->MaybeAddBakerCcEntrypointTempForFields(locations);
7472 }
Artem Serovcfbe9132016-10-14 15:58:56 +01007473}
7474
7475void InstructionCodeGeneratorARMVIXL::VisitInstanceOf(HInstanceOf* instruction) {
7476 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
7477 LocationSummary* locations = instruction->GetLocations();
7478 Location obj_loc = locations->InAt(0);
7479 vixl32::Register obj = InputRegisterAt(instruction, 0);
7480 vixl32::Register cls = InputRegisterAt(instruction, 1);
7481 Location out_loc = locations->Out();
7482 vixl32::Register out = OutputRegister(instruction);
Artem Serov657022c2016-11-23 14:19:38 +00007483 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
7484 DCHECK_LE(num_temps, 1u);
7485 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Artem Serovcfbe9132016-10-14 15:58:56 +01007486 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
7487 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
7488 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
7489 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007490 vixl32::Label done;
7491 vixl32::Label* const final_label = codegen_->GetFinalLabel(instruction, &done);
Artem Serovcfbe9132016-10-14 15:58:56 +01007492 SlowPathCodeARMVIXL* slow_path = nullptr;
7493
7494 // Return 0 if `obj` is null.
7495 // avoid null check if we know obj is not null.
7496 if (instruction->MustDoNullCheck()) {
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007497 DCHECK(!out.Is(obj));
7498 __ Mov(out, 0);
7499 __ CompareAndBranchIfZero(obj, final_label, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007500 }
7501
Artem Serovcfbe9132016-10-14 15:58:56 +01007502 switch (type_check_kind) {
7503 case TypeCheckKind::kExactCheck: {
Mathieu Chartier6beced42016-11-15 15:51:31 -08007504 // /* HeapReference<Class> */ out = obj->klass_
7505 GenerateReferenceLoadTwoRegisters(instruction,
7506 out_loc,
7507 obj_loc,
7508 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00007509 maybe_temp_loc,
7510 kCompilerReadBarrierOption);
Artem Serovcfbe9132016-10-14 15:58:56 +01007511 // Classes must be equal for the instanceof to succeed.
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007512 __ Cmp(out, cls);
7513 // We speculatively set the result to false without changing the condition
7514 // flags, which allows us to avoid some branching later.
7515 __ Mov(LeaveFlags, out, 0);
7516
7517 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
7518 // we check that the output is in a low register, so that a 16-bit MOV
7519 // encoding can be used.
7520 if (out.IsLow()) {
7521 // We use the scope because of the IT block that follows.
7522 ExactAssemblyScope guard(GetVIXLAssembler(),
7523 2 * vixl32::k16BitT32InstructionSizeInBytes,
7524 CodeBufferCheckScope::kExactSize);
7525
7526 __ it(eq);
7527 __ mov(eq, out, 1);
7528 } else {
7529 __ B(ne, final_label, /* far_target */ false);
7530 __ Mov(out, 1);
7531 }
7532
Artem Serovcfbe9132016-10-14 15:58:56 +01007533 break;
7534 }
7535
7536 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier6beced42016-11-15 15:51:31 -08007537 // /* HeapReference<Class> */ out = obj->klass_
7538 GenerateReferenceLoadTwoRegisters(instruction,
7539 out_loc,
7540 obj_loc,
7541 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00007542 maybe_temp_loc,
7543 kCompilerReadBarrierOption);
Artem Serovcfbe9132016-10-14 15:58:56 +01007544 // If the class is abstract, we eagerly fetch the super class of the
7545 // object to avoid doing a comparison we know will fail.
7546 vixl32::Label loop;
7547 __ Bind(&loop);
7548 // /* HeapReference<Class> */ out = out->super_class_
Artem Serov657022c2016-11-23 14:19:38 +00007549 GenerateReferenceLoadOneRegister(instruction,
7550 out_loc,
7551 super_offset,
7552 maybe_temp_loc,
7553 kCompilerReadBarrierOption);
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007554 // If `out` is null, we use it for the result, and jump to the final label.
Anton Kirilov6f644202017-02-27 18:29:45 +00007555 __ CompareAndBranchIfZero(out, final_label, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007556 __ Cmp(out, cls);
Artem Serov517d9f62016-12-12 15:51:15 +00007557 __ B(ne, &loop, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007558 __ Mov(out, 1);
Artem Serovcfbe9132016-10-14 15:58:56 +01007559 break;
7560 }
7561
7562 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier6beced42016-11-15 15:51:31 -08007563 // /* HeapReference<Class> */ out = obj->klass_
7564 GenerateReferenceLoadTwoRegisters(instruction,
7565 out_loc,
7566 obj_loc,
7567 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00007568 maybe_temp_loc,
7569 kCompilerReadBarrierOption);
Artem Serovcfbe9132016-10-14 15:58:56 +01007570 // Walk over the class hierarchy to find a match.
7571 vixl32::Label loop, success;
7572 __ Bind(&loop);
7573 __ Cmp(out, cls);
Artem Serov517d9f62016-12-12 15:51:15 +00007574 __ B(eq, &success, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007575 // /* HeapReference<Class> */ out = out->super_class_
Artem Serov657022c2016-11-23 14:19:38 +00007576 GenerateReferenceLoadOneRegister(instruction,
7577 out_loc,
7578 super_offset,
7579 maybe_temp_loc,
7580 kCompilerReadBarrierOption);
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007581 // This is essentially a null check, but it sets the condition flags to the
7582 // proper value for the code that follows the loop, i.e. not `eq`.
7583 __ Cmp(out, 1);
7584 __ B(hs, &loop, /* far_target */ false);
7585
7586 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
7587 // we check that the output is in a low register, so that a 16-bit MOV
7588 // encoding can be used.
7589 if (out.IsLow()) {
7590 // If `out` is null, we use it for the result, and the condition flags
7591 // have already been set to `ne`, so the IT block that comes afterwards
7592 // (and which handles the successful case) turns into a NOP (instead of
7593 // overwriting `out`).
7594 __ Bind(&success);
7595
7596 // We use the scope because of the IT block that follows.
7597 ExactAssemblyScope guard(GetVIXLAssembler(),
7598 2 * vixl32::k16BitT32InstructionSizeInBytes,
7599 CodeBufferCheckScope::kExactSize);
7600
7601 // There is only one branch to the `success` label (which is bound to this
7602 // IT block), and it has the same condition, `eq`, so in that case the MOV
7603 // is executed.
7604 __ it(eq);
7605 __ mov(eq, out, 1);
7606 } else {
7607 // If `out` is null, we use it for the result, and jump to the final label.
Anton Kirilov6f644202017-02-27 18:29:45 +00007608 __ B(final_label);
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007609 __ Bind(&success);
7610 __ Mov(out, 1);
Artem Serovcfbe9132016-10-14 15:58:56 +01007611 }
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007612
Artem Serovcfbe9132016-10-14 15:58:56 +01007613 break;
7614 }
7615
7616 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier6beced42016-11-15 15:51:31 -08007617 // /* HeapReference<Class> */ out = obj->klass_
7618 GenerateReferenceLoadTwoRegisters(instruction,
7619 out_loc,
7620 obj_loc,
7621 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00007622 maybe_temp_loc,
7623 kCompilerReadBarrierOption);
Artem Serovcfbe9132016-10-14 15:58:56 +01007624 // Do an exact check.
7625 vixl32::Label exact_check;
7626 __ Cmp(out, cls);
Artem Serov517d9f62016-12-12 15:51:15 +00007627 __ B(eq, &exact_check, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007628 // Otherwise, we need to check that the object's class is a non-primitive array.
7629 // /* HeapReference<Class> */ out = out->component_type_
Artem Serov657022c2016-11-23 14:19:38 +00007630 GenerateReferenceLoadOneRegister(instruction,
7631 out_loc,
7632 component_offset,
7633 maybe_temp_loc,
7634 kCompilerReadBarrierOption);
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007635 // If `out` is null, we use it for the result, and jump to the final label.
Anton Kirilov6f644202017-02-27 18:29:45 +00007636 __ CompareAndBranchIfZero(out, final_label, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007637 GetAssembler()->LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
7638 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007639 __ Cmp(out, 0);
7640 // We speculatively set the result to false without changing the condition
7641 // flags, which allows us to avoid some branching later.
7642 __ Mov(LeaveFlags, out, 0);
7643
7644 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
7645 // we check that the output is in a low register, so that a 16-bit MOV
7646 // encoding can be used.
7647 if (out.IsLow()) {
7648 __ Bind(&exact_check);
7649
7650 // We use the scope because of the IT block that follows.
7651 ExactAssemblyScope guard(GetVIXLAssembler(),
7652 2 * vixl32::k16BitT32InstructionSizeInBytes,
7653 CodeBufferCheckScope::kExactSize);
7654
7655 __ it(eq);
7656 __ mov(eq, out, 1);
7657 } else {
7658 __ B(ne, final_label, /* far_target */ false);
7659 __ Bind(&exact_check);
7660 __ Mov(out, 1);
7661 }
7662
Artem Serovcfbe9132016-10-14 15:58:56 +01007663 break;
7664 }
7665
7666 case TypeCheckKind::kArrayCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00007667 // No read barrier since the slow path will retry upon failure.
Mathieu Chartier6beced42016-11-15 15:51:31 -08007668 // /* HeapReference<Class> */ out = obj->klass_
7669 GenerateReferenceLoadTwoRegisters(instruction,
7670 out_loc,
7671 obj_loc,
7672 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00007673 maybe_temp_loc,
7674 kWithoutReadBarrier);
Artem Serovcfbe9132016-10-14 15:58:56 +01007675 __ Cmp(out, cls);
7676 DCHECK(locations->OnlyCallsOnSlowPath());
7677 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARMVIXL(instruction,
7678 /* is_fatal */ false);
7679 codegen_->AddSlowPath(slow_path);
7680 __ B(ne, slow_path->GetEntryLabel());
7681 __ Mov(out, 1);
Artem Serovcfbe9132016-10-14 15:58:56 +01007682 break;
7683 }
7684
7685 case TypeCheckKind::kUnresolvedCheck:
7686 case TypeCheckKind::kInterfaceCheck: {
7687 // Note that we indeed only call on slow path, but we always go
7688 // into the slow path for the unresolved and interface check
7689 // cases.
7690 //
7691 // We cannot directly call the InstanceofNonTrivial runtime
7692 // entry point without resorting to a type checking slow path
7693 // here (i.e. by calling InvokeRuntime directly), as it would
7694 // require to assign fixed registers for the inputs of this
7695 // HInstanceOf instruction (following the runtime calling
7696 // convention), which might be cluttered by the potential first
7697 // read barrier emission at the beginning of this method.
7698 //
7699 // TODO: Introduce a new runtime entry point taking the object
7700 // to test (instead of its class) as argument, and let it deal
7701 // with the read barrier issues. This will let us refactor this
7702 // case of the `switch` code as it was previously (with a direct
7703 // call to the runtime not using a type checking slow path).
7704 // This should also be beneficial for the other cases above.
7705 DCHECK(locations->OnlyCallsOnSlowPath());
7706 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARMVIXL(instruction,
7707 /* is_fatal */ false);
7708 codegen_->AddSlowPath(slow_path);
7709 __ B(slow_path->GetEntryLabel());
Artem Serovcfbe9132016-10-14 15:58:56 +01007710 break;
7711 }
7712 }
7713
Artem Serovcfbe9132016-10-14 15:58:56 +01007714 if (done.IsReferenced()) {
7715 __ Bind(&done);
7716 }
7717
7718 if (slow_path != nullptr) {
7719 __ Bind(slow_path->GetExitLabel());
7720 }
7721}
7722
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007723void LocationsBuilderARMVIXL::VisitCheckCast(HCheckCast* instruction) {
7724 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
7725 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
7726
7727 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
7728 switch (type_check_kind) {
7729 case TypeCheckKind::kExactCheck:
7730 case TypeCheckKind::kAbstractClassCheck:
7731 case TypeCheckKind::kClassHierarchyCheck:
7732 case TypeCheckKind::kArrayObjectCheck:
7733 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
7734 LocationSummary::kCallOnSlowPath :
7735 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
7736 break;
7737 case TypeCheckKind::kArrayCheck:
7738 case TypeCheckKind::kUnresolvedCheck:
7739 case TypeCheckKind::kInterfaceCheck:
7740 call_kind = LocationSummary::kCallOnSlowPath;
7741 break;
7742 }
7743
7744 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
7745 locations->SetInAt(0, Location::RequiresRegister());
7746 locations->SetInAt(1, Location::RequiresRegister());
Artem Serov657022c2016-11-23 14:19:38 +00007747 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007748}
7749
7750void InstructionCodeGeneratorARMVIXL::VisitCheckCast(HCheckCast* instruction) {
7751 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
7752 LocationSummary* locations = instruction->GetLocations();
7753 Location obj_loc = locations->InAt(0);
7754 vixl32::Register obj = InputRegisterAt(instruction, 0);
7755 vixl32::Register cls = InputRegisterAt(instruction, 1);
7756 Location temp_loc = locations->GetTemp(0);
7757 vixl32::Register temp = RegisterFrom(temp_loc);
Artem Serov657022c2016-11-23 14:19:38 +00007758 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
7759 DCHECK_LE(num_temps, 3u);
7760 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
7761 Location maybe_temp3_loc = (num_temps >= 3) ? locations->GetTemp(2) : Location::NoLocation();
7762 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
7763 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
7764 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
7765 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
7766 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
7767 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
7768 const uint32_t object_array_data_offset =
7769 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007770
Artem Serov657022c2016-11-23 14:19:38 +00007771 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
7772 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
7773 // read barriers is done for performance and code size reasons.
7774 bool is_type_check_slow_path_fatal = false;
7775 if (!kEmitCompilerReadBarrier) {
7776 is_type_check_slow_path_fatal =
7777 (type_check_kind == TypeCheckKind::kExactCheck ||
7778 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
7779 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
7780 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
7781 !instruction->CanThrowIntoCatchBlock();
7782 }
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007783 SlowPathCodeARMVIXL* type_check_slow_path =
7784 new (GetGraph()->GetArena()) TypeCheckSlowPathARMVIXL(instruction,
7785 is_type_check_slow_path_fatal);
7786 codegen_->AddSlowPath(type_check_slow_path);
7787
7788 vixl32::Label done;
Anton Kirilov6f644202017-02-27 18:29:45 +00007789 vixl32::Label* final_label = codegen_->GetFinalLabel(instruction, &done);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007790 // Avoid null check if we know obj is not null.
7791 if (instruction->MustDoNullCheck()) {
Anton Kirilov6f644202017-02-27 18:29:45 +00007792 __ CompareAndBranchIfZero(obj, final_label, /* far_target */ false);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007793 }
7794
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007795 switch (type_check_kind) {
7796 case TypeCheckKind::kExactCheck:
7797 case TypeCheckKind::kArrayCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00007798 // /* HeapReference<Class> */ temp = obj->klass_
7799 GenerateReferenceLoadTwoRegisters(instruction,
7800 temp_loc,
7801 obj_loc,
7802 class_offset,
7803 maybe_temp2_loc,
7804 kWithoutReadBarrier);
7805
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007806 __ Cmp(temp, cls);
7807 // Jump to slow path for throwing the exception or doing a
7808 // more involved array check.
7809 __ B(ne, type_check_slow_path->GetEntryLabel());
7810 break;
7811 }
7812
7813 case TypeCheckKind::kAbstractClassCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00007814 // /* HeapReference<Class> */ temp = obj->klass_
7815 GenerateReferenceLoadTwoRegisters(instruction,
7816 temp_loc,
7817 obj_loc,
7818 class_offset,
7819 maybe_temp2_loc,
7820 kWithoutReadBarrier);
7821
Artem Serovcfbe9132016-10-14 15:58:56 +01007822 // If the class is abstract, we eagerly fetch the super class of the
7823 // object to avoid doing a comparison we know will fail.
7824 vixl32::Label loop;
7825 __ Bind(&loop);
7826 // /* HeapReference<Class> */ temp = temp->super_class_
Artem Serov657022c2016-11-23 14:19:38 +00007827 GenerateReferenceLoadOneRegister(instruction,
7828 temp_loc,
7829 super_offset,
7830 maybe_temp2_loc,
7831 kWithoutReadBarrier);
Artem Serovcfbe9132016-10-14 15:58:56 +01007832
7833 // If the class reference currently in `temp` is null, jump to the slow path to throw the
7834 // exception.
xueliang.zhongf51bc622016-11-04 09:23:32 +00007835 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
Artem Serovcfbe9132016-10-14 15:58:56 +01007836
7837 // Otherwise, compare the classes.
7838 __ Cmp(temp, cls);
Artem Serov517d9f62016-12-12 15:51:15 +00007839 __ B(ne, &loop, /* far_target */ false);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007840 break;
7841 }
7842
7843 case TypeCheckKind::kClassHierarchyCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00007844 // /* HeapReference<Class> */ temp = obj->klass_
7845 GenerateReferenceLoadTwoRegisters(instruction,
7846 temp_loc,
7847 obj_loc,
7848 class_offset,
7849 maybe_temp2_loc,
7850 kWithoutReadBarrier);
7851
Artem Serovcfbe9132016-10-14 15:58:56 +01007852 // Walk over the class hierarchy to find a match.
7853 vixl32::Label loop;
7854 __ Bind(&loop);
7855 __ Cmp(temp, cls);
Anton Kirilov6f644202017-02-27 18:29:45 +00007856 __ B(eq, final_label, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007857
7858 // /* HeapReference<Class> */ temp = temp->super_class_
Artem Serov657022c2016-11-23 14:19:38 +00007859 GenerateReferenceLoadOneRegister(instruction,
7860 temp_loc,
7861 super_offset,
7862 maybe_temp2_loc,
7863 kWithoutReadBarrier);
Artem Serovcfbe9132016-10-14 15:58:56 +01007864
7865 // If the class reference currently in `temp` is null, jump to the slow path to throw the
7866 // exception.
xueliang.zhongf51bc622016-11-04 09:23:32 +00007867 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
Artem Serovcfbe9132016-10-14 15:58:56 +01007868 // Otherwise, jump to the beginning of the loop.
7869 __ B(&loop);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007870 break;
7871 }
7872
Artem Serovcfbe9132016-10-14 15:58:56 +01007873 case TypeCheckKind::kArrayObjectCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00007874 // /* HeapReference<Class> */ temp = obj->klass_
7875 GenerateReferenceLoadTwoRegisters(instruction,
7876 temp_loc,
7877 obj_loc,
7878 class_offset,
7879 maybe_temp2_loc,
7880 kWithoutReadBarrier);
7881
Artem Serovcfbe9132016-10-14 15:58:56 +01007882 // Do an exact check.
7883 __ Cmp(temp, cls);
Anton Kirilov6f644202017-02-27 18:29:45 +00007884 __ B(eq, final_label, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007885
7886 // Otherwise, we need to check that the object's class is a non-primitive array.
7887 // /* HeapReference<Class> */ temp = temp->component_type_
Artem Serov657022c2016-11-23 14:19:38 +00007888 GenerateReferenceLoadOneRegister(instruction,
7889 temp_loc,
7890 component_offset,
7891 maybe_temp2_loc,
7892 kWithoutReadBarrier);
Artem Serovcfbe9132016-10-14 15:58:56 +01007893 // If the component type is null, jump to the slow path to throw the exception.
xueliang.zhongf51bc622016-11-04 09:23:32 +00007894 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
Artem Serovcfbe9132016-10-14 15:58:56 +01007895 // Otherwise,the object is indeed an array, jump to label `check_non_primitive_component_type`
7896 // to further check that this component type is not a primitive type.
7897 GetAssembler()->LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007898 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
xueliang.zhongf51bc622016-11-04 09:23:32 +00007899 __ CompareAndBranchIfNonZero(temp, type_check_slow_path->GetEntryLabel());
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007900 break;
7901 }
7902
7903 case TypeCheckKind::kUnresolvedCheck:
Artem Serov657022c2016-11-23 14:19:38 +00007904 // We always go into the type check slow path for the unresolved check case.
Artem Serovcfbe9132016-10-14 15:58:56 +01007905 // We cannot directly call the CheckCast runtime entry point
7906 // without resorting to a type checking slow path here (i.e. by
7907 // calling InvokeRuntime directly), as it would require to
7908 // assign fixed registers for the inputs of this HInstanceOf
7909 // instruction (following the runtime calling convention), which
7910 // might be cluttered by the potential first read barrier
7911 // emission at the beginning of this method.
Artem Serov657022c2016-11-23 14:19:38 +00007912
Artem Serovcfbe9132016-10-14 15:58:56 +01007913 __ B(type_check_slow_path->GetEntryLabel());
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007914 break;
Artem Serov657022c2016-11-23 14:19:38 +00007915
7916 case TypeCheckKind::kInterfaceCheck: {
7917 // Avoid read barriers to improve performance of the fast path. We can not get false
7918 // positives by doing this.
7919 // /* HeapReference<Class> */ temp = obj->klass_
7920 GenerateReferenceLoadTwoRegisters(instruction,
7921 temp_loc,
7922 obj_loc,
7923 class_offset,
7924 maybe_temp2_loc,
7925 kWithoutReadBarrier);
7926
7927 // /* HeapReference<Class> */ temp = temp->iftable_
7928 GenerateReferenceLoadTwoRegisters(instruction,
7929 temp_loc,
7930 temp_loc,
7931 iftable_offset,
7932 maybe_temp2_loc,
7933 kWithoutReadBarrier);
7934 // Iftable is never null.
7935 __ Ldr(RegisterFrom(maybe_temp2_loc), MemOperand(temp, array_length_offset));
7936 // Loop through the iftable and check if any class matches.
7937 vixl32::Label start_loop;
7938 __ Bind(&start_loop);
7939 __ CompareAndBranchIfZero(RegisterFrom(maybe_temp2_loc),
7940 type_check_slow_path->GetEntryLabel());
7941 __ Ldr(RegisterFrom(maybe_temp3_loc), MemOperand(temp, object_array_data_offset));
7942 GetAssembler()->MaybeUnpoisonHeapReference(RegisterFrom(maybe_temp3_loc));
7943 // Go to next interface.
7944 __ Add(temp, temp, Operand::From(2 * kHeapReferenceSize));
7945 __ Sub(RegisterFrom(maybe_temp2_loc), RegisterFrom(maybe_temp2_loc), 2);
7946 // Compare the classes and continue the loop if they do not match.
7947 __ Cmp(cls, RegisterFrom(maybe_temp3_loc));
Artem Serov517d9f62016-12-12 15:51:15 +00007948 __ B(ne, &start_loop, /* far_target */ false);
Artem Serov657022c2016-11-23 14:19:38 +00007949 break;
7950 }
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007951 }
Anton Kirilov6f644202017-02-27 18:29:45 +00007952 if (done.IsReferenced()) {
7953 __ Bind(&done);
7954 }
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007955
7956 __ Bind(type_check_slow_path->GetExitLabel());
7957}
7958
Artem Serov551b28f2016-10-18 19:11:30 +01007959void LocationsBuilderARMVIXL::VisitMonitorOperation(HMonitorOperation* instruction) {
7960 LocationSummary* locations =
7961 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
7962 InvokeRuntimeCallingConventionARMVIXL calling_convention;
7963 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
7964}
7965
7966void InstructionCodeGeneratorARMVIXL::VisitMonitorOperation(HMonitorOperation* instruction) {
7967 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
7968 instruction,
7969 instruction->GetDexPc());
7970 if (instruction->IsEnter()) {
7971 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
7972 } else {
7973 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
7974 }
Roland Levillain5daa4952017-07-03 17:23:56 +01007975 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 17);
Artem Serov551b28f2016-10-18 19:11:30 +01007976}
7977
Artem Serov02109dd2016-09-23 17:17:54 +01007978void LocationsBuilderARMVIXL::VisitAnd(HAnd* instruction) {
7979 HandleBitwiseOperation(instruction, AND);
7980}
7981
7982void LocationsBuilderARMVIXL::VisitOr(HOr* instruction) {
7983 HandleBitwiseOperation(instruction, ORR);
7984}
7985
7986void LocationsBuilderARMVIXL::VisitXor(HXor* instruction) {
7987 HandleBitwiseOperation(instruction, EOR);
7988}
7989
7990void LocationsBuilderARMVIXL::HandleBitwiseOperation(HBinaryOperation* instruction, Opcode opcode) {
7991 LocationSummary* locations =
7992 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007993 DCHECK(instruction->GetResultType() == DataType::Type::kInt32
7994 || instruction->GetResultType() == DataType::Type::kInt64);
Artem Serov02109dd2016-09-23 17:17:54 +01007995 // Note: GVN reorders commutative operations to have the constant on the right hand side.
7996 locations->SetInAt(0, Location::RequiresRegister());
7997 locations->SetInAt(1, ArmEncodableConstantOrRegister(instruction->InputAt(1), opcode));
7998 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
7999}
8000
8001void InstructionCodeGeneratorARMVIXL::VisitAnd(HAnd* instruction) {
8002 HandleBitwiseOperation(instruction);
8003}
8004
8005void InstructionCodeGeneratorARMVIXL::VisitOr(HOr* instruction) {
8006 HandleBitwiseOperation(instruction);
8007}
8008
8009void InstructionCodeGeneratorARMVIXL::VisitXor(HXor* instruction) {
8010 HandleBitwiseOperation(instruction);
8011}
8012
Artem Serov2bbc9532016-10-21 11:51:50 +01008013void LocationsBuilderARMVIXL::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
8014 LocationSummary* locations =
8015 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008016 DCHECK(instruction->GetResultType() == DataType::Type::kInt32
8017 || instruction->GetResultType() == DataType::Type::kInt64);
Artem Serov2bbc9532016-10-21 11:51:50 +01008018
8019 locations->SetInAt(0, Location::RequiresRegister());
8020 locations->SetInAt(1, Location::RequiresRegister());
8021 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8022}
8023
8024void InstructionCodeGeneratorARMVIXL::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
8025 LocationSummary* locations = instruction->GetLocations();
8026 Location first = locations->InAt(0);
8027 Location second = locations->InAt(1);
8028 Location out = locations->Out();
8029
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008030 if (instruction->GetResultType() == DataType::Type::kInt32) {
Artem Serov2bbc9532016-10-21 11:51:50 +01008031 vixl32::Register first_reg = RegisterFrom(first);
8032 vixl32::Register second_reg = RegisterFrom(second);
8033 vixl32::Register out_reg = RegisterFrom(out);
8034
8035 switch (instruction->GetOpKind()) {
8036 case HInstruction::kAnd:
8037 __ Bic(out_reg, first_reg, second_reg);
8038 break;
8039 case HInstruction::kOr:
8040 __ Orn(out_reg, first_reg, second_reg);
8041 break;
8042 // There is no EON on arm.
8043 case HInstruction::kXor:
8044 default:
8045 LOG(FATAL) << "Unexpected instruction " << instruction->DebugName();
8046 UNREACHABLE();
8047 }
8048 return;
8049
8050 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008051 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
Artem Serov2bbc9532016-10-21 11:51:50 +01008052 vixl32::Register first_low = LowRegisterFrom(first);
8053 vixl32::Register first_high = HighRegisterFrom(first);
8054 vixl32::Register second_low = LowRegisterFrom(second);
8055 vixl32::Register second_high = HighRegisterFrom(second);
8056 vixl32::Register out_low = LowRegisterFrom(out);
8057 vixl32::Register out_high = HighRegisterFrom(out);
8058
8059 switch (instruction->GetOpKind()) {
8060 case HInstruction::kAnd:
8061 __ Bic(out_low, first_low, second_low);
8062 __ Bic(out_high, first_high, second_high);
8063 break;
8064 case HInstruction::kOr:
8065 __ Orn(out_low, first_low, second_low);
8066 __ Orn(out_high, first_high, second_high);
8067 break;
8068 // There is no EON on arm.
8069 case HInstruction::kXor:
8070 default:
8071 LOG(FATAL) << "Unexpected instruction " << instruction->DebugName();
8072 UNREACHABLE();
8073 }
8074 }
8075}
8076
Anton Kirilov74234da2017-01-13 14:42:47 +00008077void LocationsBuilderARMVIXL::VisitDataProcWithShifterOp(
8078 HDataProcWithShifterOp* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008079 DCHECK(instruction->GetType() == DataType::Type::kInt32 ||
8080 instruction->GetType() == DataType::Type::kInt64);
Anton Kirilov74234da2017-01-13 14:42:47 +00008081 LocationSummary* locations =
8082 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008083 const bool overlap = instruction->GetType() == DataType::Type::kInt64 &&
Anton Kirilov74234da2017-01-13 14:42:47 +00008084 HDataProcWithShifterOp::IsExtensionOp(instruction->GetOpKind());
8085
8086 locations->SetInAt(0, Location::RequiresRegister());
8087 locations->SetInAt(1, Location::RequiresRegister());
8088 locations->SetOut(Location::RequiresRegister(),
8089 overlap ? Location::kOutputOverlap : Location::kNoOutputOverlap);
8090}
8091
8092void InstructionCodeGeneratorARMVIXL::VisitDataProcWithShifterOp(
8093 HDataProcWithShifterOp* instruction) {
8094 const LocationSummary* const locations = instruction->GetLocations();
8095 const HInstruction::InstructionKind kind = instruction->GetInstrKind();
8096 const HDataProcWithShifterOp::OpKind op_kind = instruction->GetOpKind();
8097
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008098 if (instruction->GetType() == DataType::Type::kInt32) {
Anton Kirilov420ee302017-02-21 18:10:26 +00008099 const vixl32::Register first = InputRegisterAt(instruction, 0);
8100 const vixl32::Register output = OutputRegister(instruction);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008101 const vixl32::Register second = instruction->InputAt(1)->GetType() == DataType::Type::kInt64
Anton Kirilov74234da2017-01-13 14:42:47 +00008102 ? LowRegisterFrom(locations->InAt(1))
8103 : InputRegisterAt(instruction, 1);
8104
Anton Kirilov420ee302017-02-21 18:10:26 +00008105 if (HDataProcWithShifterOp::IsExtensionOp(op_kind)) {
8106 DCHECK_EQ(kind, HInstruction::kAdd);
8107
8108 switch (op_kind) {
8109 case HDataProcWithShifterOp::kUXTB:
8110 __ Uxtab(output, first, second);
8111 break;
8112 case HDataProcWithShifterOp::kUXTH:
8113 __ Uxtah(output, first, second);
8114 break;
8115 case HDataProcWithShifterOp::kSXTB:
8116 __ Sxtab(output, first, second);
8117 break;
8118 case HDataProcWithShifterOp::kSXTH:
8119 __ Sxtah(output, first, second);
8120 break;
8121 default:
8122 LOG(FATAL) << "Unexpected operation kind: " << op_kind;
8123 UNREACHABLE();
8124 }
8125 } else {
8126 GenerateDataProcInstruction(kind,
8127 output,
8128 first,
8129 Operand(second,
8130 ShiftFromOpKind(op_kind),
8131 instruction->GetShiftAmount()),
8132 codegen_);
8133 }
Anton Kirilov74234da2017-01-13 14:42:47 +00008134 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008135 DCHECK_EQ(instruction->GetType(), DataType::Type::kInt64);
Anton Kirilov74234da2017-01-13 14:42:47 +00008136
8137 if (HDataProcWithShifterOp::IsExtensionOp(op_kind)) {
8138 const vixl32::Register second = InputRegisterAt(instruction, 1);
8139
8140 DCHECK(!LowRegisterFrom(locations->Out()).Is(second));
8141 GenerateDataProc(kind,
8142 locations->Out(),
8143 locations->InAt(0),
8144 second,
8145 Operand(second, ShiftType::ASR, 31),
8146 codegen_);
8147 } else {
8148 GenerateLongDataProc(instruction, codegen_);
8149 }
8150 }
8151}
8152
Artem Serov02109dd2016-09-23 17:17:54 +01008153// TODO(VIXL): Remove optimizations in the helper when they are implemented in vixl.
8154void InstructionCodeGeneratorARMVIXL::GenerateAndConst(vixl32::Register out,
8155 vixl32::Register first,
8156 uint32_t value) {
8157 // Optimize special cases for individual halfs of `and-long` (`and` is simplified earlier).
8158 if (value == 0xffffffffu) {
8159 if (!out.Is(first)) {
8160 __ Mov(out, first);
8161 }
8162 return;
8163 }
8164 if (value == 0u) {
8165 __ Mov(out, 0);
8166 return;
8167 }
8168 if (GetAssembler()->ShifterOperandCanHold(AND, value)) {
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00008169 __ And(out, first, value);
8170 } else if (GetAssembler()->ShifterOperandCanHold(BIC, ~value)) {
8171 __ Bic(out, first, ~value);
Artem Serov02109dd2016-09-23 17:17:54 +01008172 } else {
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00008173 DCHECK(IsPowerOfTwo(value + 1));
8174 __ Ubfx(out, first, 0, WhichPowerOf2(value + 1));
Artem Serov02109dd2016-09-23 17:17:54 +01008175 }
8176}
8177
8178// TODO(VIXL): Remove optimizations in the helper when they are implemented in vixl.
8179void InstructionCodeGeneratorARMVIXL::GenerateOrrConst(vixl32::Register out,
8180 vixl32::Register first,
8181 uint32_t value) {
8182 // Optimize special cases for individual halfs of `or-long` (`or` is simplified earlier).
8183 if (value == 0u) {
8184 if (!out.Is(first)) {
8185 __ Mov(out, first);
8186 }
8187 return;
8188 }
8189 if (value == 0xffffffffu) {
8190 __ Mvn(out, 0);
8191 return;
8192 }
8193 if (GetAssembler()->ShifterOperandCanHold(ORR, value)) {
8194 __ Orr(out, first, value);
8195 } else {
8196 DCHECK(GetAssembler()->ShifterOperandCanHold(ORN, ~value));
8197 __ Orn(out, first, ~value);
8198 }
8199}
8200
8201// TODO(VIXL): Remove optimizations in the helper when they are implemented in vixl.
8202void InstructionCodeGeneratorARMVIXL::GenerateEorConst(vixl32::Register out,
8203 vixl32::Register first,
8204 uint32_t value) {
8205 // Optimize special case for individual halfs of `xor-long` (`xor` is simplified earlier).
8206 if (value == 0u) {
8207 if (!out.Is(first)) {
8208 __ Mov(out, first);
8209 }
8210 return;
8211 }
8212 __ Eor(out, first, value);
8213}
8214
Anton Kirilovdda43962016-11-21 19:55:20 +00008215void InstructionCodeGeneratorARMVIXL::GenerateAddLongConst(Location out,
8216 Location first,
8217 uint64_t value) {
8218 vixl32::Register out_low = LowRegisterFrom(out);
8219 vixl32::Register out_high = HighRegisterFrom(out);
8220 vixl32::Register first_low = LowRegisterFrom(first);
8221 vixl32::Register first_high = HighRegisterFrom(first);
8222 uint32_t value_low = Low32Bits(value);
8223 uint32_t value_high = High32Bits(value);
8224 if (value_low == 0u) {
8225 if (!out_low.Is(first_low)) {
8226 __ Mov(out_low, first_low);
8227 }
8228 __ Add(out_high, first_high, value_high);
8229 return;
8230 }
8231 __ Adds(out_low, first_low, value_low);
Scott Wakelingbffdc702016-12-07 17:46:03 +00008232 if (GetAssembler()->ShifterOperandCanHold(ADC, value_high, kCcDontCare)) {
Anton Kirilovdda43962016-11-21 19:55:20 +00008233 __ Adc(out_high, first_high, value_high);
Scott Wakelingbffdc702016-12-07 17:46:03 +00008234 } else if (GetAssembler()->ShifterOperandCanHold(SBC, ~value_high, kCcDontCare)) {
Anton Kirilovdda43962016-11-21 19:55:20 +00008235 __ Sbc(out_high, first_high, ~value_high);
8236 } else {
8237 LOG(FATAL) << "Unexpected constant " << value_high;
8238 UNREACHABLE();
8239 }
8240}
8241
Artem Serov02109dd2016-09-23 17:17:54 +01008242void InstructionCodeGeneratorARMVIXL::HandleBitwiseOperation(HBinaryOperation* instruction) {
8243 LocationSummary* locations = instruction->GetLocations();
8244 Location first = locations->InAt(0);
8245 Location second = locations->InAt(1);
8246 Location out = locations->Out();
8247
8248 if (second.IsConstant()) {
8249 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
8250 uint32_t value_low = Low32Bits(value);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008251 if (instruction->GetResultType() == DataType::Type::kInt32) {
Artem Serov02109dd2016-09-23 17:17:54 +01008252 vixl32::Register first_reg = InputRegisterAt(instruction, 0);
8253 vixl32::Register out_reg = OutputRegister(instruction);
8254 if (instruction->IsAnd()) {
8255 GenerateAndConst(out_reg, first_reg, value_low);
8256 } else if (instruction->IsOr()) {
8257 GenerateOrrConst(out_reg, first_reg, value_low);
8258 } else {
8259 DCHECK(instruction->IsXor());
8260 GenerateEorConst(out_reg, first_reg, value_low);
8261 }
8262 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008263 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
Artem Serov02109dd2016-09-23 17:17:54 +01008264 uint32_t value_high = High32Bits(value);
8265 vixl32::Register first_low = LowRegisterFrom(first);
8266 vixl32::Register first_high = HighRegisterFrom(first);
8267 vixl32::Register out_low = LowRegisterFrom(out);
8268 vixl32::Register out_high = HighRegisterFrom(out);
8269 if (instruction->IsAnd()) {
8270 GenerateAndConst(out_low, first_low, value_low);
8271 GenerateAndConst(out_high, first_high, value_high);
8272 } else if (instruction->IsOr()) {
8273 GenerateOrrConst(out_low, first_low, value_low);
8274 GenerateOrrConst(out_high, first_high, value_high);
8275 } else {
8276 DCHECK(instruction->IsXor());
8277 GenerateEorConst(out_low, first_low, value_low);
8278 GenerateEorConst(out_high, first_high, value_high);
8279 }
8280 }
8281 return;
8282 }
8283
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008284 if (instruction->GetResultType() == DataType::Type::kInt32) {
Artem Serov02109dd2016-09-23 17:17:54 +01008285 vixl32::Register first_reg = InputRegisterAt(instruction, 0);
8286 vixl32::Register second_reg = InputRegisterAt(instruction, 1);
8287 vixl32::Register out_reg = OutputRegister(instruction);
8288 if (instruction->IsAnd()) {
8289 __ And(out_reg, first_reg, second_reg);
8290 } else if (instruction->IsOr()) {
8291 __ Orr(out_reg, first_reg, second_reg);
8292 } else {
8293 DCHECK(instruction->IsXor());
8294 __ Eor(out_reg, first_reg, second_reg);
8295 }
8296 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008297 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
Artem Serov02109dd2016-09-23 17:17:54 +01008298 vixl32::Register first_low = LowRegisterFrom(first);
8299 vixl32::Register first_high = HighRegisterFrom(first);
8300 vixl32::Register second_low = LowRegisterFrom(second);
8301 vixl32::Register second_high = HighRegisterFrom(second);
8302 vixl32::Register out_low = LowRegisterFrom(out);
8303 vixl32::Register out_high = HighRegisterFrom(out);
8304 if (instruction->IsAnd()) {
8305 __ And(out_low, first_low, second_low);
8306 __ And(out_high, first_high, second_high);
8307 } else if (instruction->IsOr()) {
8308 __ Orr(out_low, first_low, second_low);
8309 __ Orr(out_high, first_high, second_high);
8310 } else {
8311 DCHECK(instruction->IsXor());
8312 __ Eor(out_low, first_low, second_low);
8313 __ Eor(out_high, first_high, second_high);
8314 }
8315 }
8316}
8317
Artem Serovcfbe9132016-10-14 15:58:56 +01008318void InstructionCodeGeneratorARMVIXL::GenerateReferenceLoadOneRegister(
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008319 HInstruction* instruction,
Artem Serovcfbe9132016-10-14 15:58:56 +01008320 Location out,
8321 uint32_t offset,
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008322 Location maybe_temp,
8323 ReadBarrierOption read_barrier_option) {
Artem Serovcfbe9132016-10-14 15:58:56 +01008324 vixl32::Register out_reg = RegisterFrom(out);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008325 if (read_barrier_option == kWithReadBarrier) {
8326 CHECK(kEmitCompilerReadBarrier);
8327 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
8328 if (kUseBakerReadBarrier) {
8329 // Load with fast path based Baker's read barrier.
8330 // /* HeapReference<Object> */ out = *(out + offset)
8331 codegen_->GenerateFieldLoadWithBakerReadBarrier(
8332 instruction, out, out_reg, offset, maybe_temp, /* needs_null_check */ false);
8333 } else {
8334 // Load with slow path based read barrier.
8335 // Save the value of `out` into `maybe_temp` before overwriting it
8336 // in the following move operation, as we will need it for the
8337 // read barrier below.
8338 __ Mov(RegisterFrom(maybe_temp), out_reg);
8339 // /* HeapReference<Object> */ out = *(out + offset)
8340 GetAssembler()->LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
8341 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
8342 }
Artem Serovcfbe9132016-10-14 15:58:56 +01008343 } else {
8344 // Plain load with no read barrier.
8345 // /* HeapReference<Object> */ out = *(out + offset)
8346 GetAssembler()->LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
8347 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
8348 }
8349}
8350
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008351void InstructionCodeGeneratorARMVIXL::GenerateReferenceLoadTwoRegisters(
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008352 HInstruction* instruction,
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008353 Location out,
8354 Location obj,
8355 uint32_t offset,
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008356 Location maybe_temp,
8357 ReadBarrierOption read_barrier_option) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008358 vixl32::Register out_reg = RegisterFrom(out);
8359 vixl32::Register obj_reg = RegisterFrom(obj);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008360 if (read_barrier_option == kWithReadBarrier) {
8361 CHECK(kEmitCompilerReadBarrier);
8362 if (kUseBakerReadBarrier) {
8363 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
8364 // Load with fast path based Baker's read barrier.
8365 // /* HeapReference<Object> */ out = *(obj + offset)
8366 codegen_->GenerateFieldLoadWithBakerReadBarrier(
8367 instruction, out, obj_reg, offset, maybe_temp, /* needs_null_check */ false);
8368 } else {
8369 // Load with slow path based read barrier.
8370 // /* HeapReference<Object> */ out = *(obj + offset)
8371 GetAssembler()->LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
8372 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
8373 }
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008374 } else {
8375 // Plain load with no read barrier.
8376 // /* HeapReference<Object> */ out = *(obj + offset)
8377 GetAssembler()->LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
8378 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
8379 }
8380}
8381
Scott Wakelinga7812ae2016-10-17 10:03:36 +01008382void InstructionCodeGeneratorARMVIXL::GenerateGcRootFieldLoad(
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008383 HInstruction* instruction,
Scott Wakelinga7812ae2016-10-17 10:03:36 +01008384 Location root,
8385 vixl32::Register obj,
8386 uint32_t offset,
Artem Serovd4cc5b22016-11-04 11:19:09 +00008387 ReadBarrierOption read_barrier_option) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01008388 vixl32::Register root_reg = RegisterFrom(root);
Artem Serovd4cc5b22016-11-04 11:19:09 +00008389 if (read_barrier_option == kWithReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008390 DCHECK(kEmitCompilerReadBarrier);
8391 if (kUseBakerReadBarrier) {
8392 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
Roland Levillainba650a42017-03-06 13:52:32 +00008393 // Baker's read barrier are used.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008394 if (kBakerReadBarrierLinkTimeThunksEnableForGcRoots &&
8395 !Runtime::Current()->UseJitCompilation()) {
Roland Levillain6d729a72017-06-30 18:34:01 +01008396 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in
8397 // the Marking Register) to decide whether we need to enter
8398 // the slow path to mark the GC root.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008399 //
8400 // We use link-time generated thunks for the slow path. That thunk
8401 // checks the reference and jumps to the entrypoint if needed.
8402 //
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008403 // lr = &return_address;
8404 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
Roland Levillain6d729a72017-06-30 18:34:01 +01008405 // if (mr) { // Thread::Current()->GetIsGcMarking()
8406 // goto gc_root_thunk<root_reg>(lr)
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008407 // }
8408 // return_address:
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008409
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008410 UseScratchRegisterScope temps(GetVIXLAssembler());
8411 ExcludeIPAndBakerCcEntrypointRegister(&temps, instruction);
Vladimir Marko88abba22017-05-03 17:09:25 +01008412 bool narrow = CanEmitNarrowLdr(root_reg, obj, offset);
8413 uint32_t custom_data = linker::Thumb2RelativePatcher::EncodeBakerReadBarrierGcRootData(
8414 root_reg.GetCode(), narrow);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008415 vixl32::Label* bne_label = codegen_->NewBakerReadBarrierPatch(custom_data);
Roland Levillainba650a42017-03-06 13:52:32 +00008416
Roland Levillain6d729a72017-06-30 18:34:01 +01008417 vixl::EmissionCheckScope guard(GetVIXLAssembler(), 4 * vixl32::kMaxInstructionSizeInBytes);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008418 vixl32::Label return_address;
8419 EmitAdrCode adr(GetVIXLAssembler(), lr, &return_address);
Roland Levillain6d729a72017-06-30 18:34:01 +01008420 __ cmp(mr, Operand(0));
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008421 // Currently the offset is always within range. If that changes,
8422 // we shall have to split the load the same way as for fields.
8423 DCHECK_LT(offset, kReferenceLoadMinFarOffset);
Vladimir Marko88abba22017-05-03 17:09:25 +01008424 ptrdiff_t old_offset = GetVIXLAssembler()->GetBuffer()->GetCursorOffset();
8425 __ ldr(EncodingSize(narrow ? Narrow : Wide), root_reg, MemOperand(obj, offset));
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008426 EmitPlaceholderBne(codegen_, bne_label);
8427 __ Bind(&return_address);
Vladimir Marko88abba22017-05-03 17:09:25 +01008428 DCHECK_EQ(old_offset - GetVIXLAssembler()->GetBuffer()->GetCursorOffset(),
8429 narrow ? BAKER_MARK_INTROSPECTION_GC_ROOT_LDR_NARROW_OFFSET
8430 : BAKER_MARK_INTROSPECTION_GC_ROOT_LDR_WIDE_OFFSET);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008431 } else {
Roland Levillain6d729a72017-06-30 18:34:01 +01008432 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in
8433 // the Marking Register) to decide whether we need to enter
8434 // the slow path to mark the GC root.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008435 //
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008436 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
Roland Levillain6d729a72017-06-30 18:34:01 +01008437 // if (mr) { // Thread::Current()->GetIsGcMarking()
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008438 // // Slow path.
Roland Levillain6d729a72017-06-30 18:34:01 +01008439 // entrypoint = Thread::Current()->pReadBarrierMarkReg ## root.reg()
8440 // root = entrypoint(root); // root = ReadBarrier::Mark(root); // Entry point call.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008441 // }
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008442
Roland Levillain6d729a72017-06-30 18:34:01 +01008443 // Slow path marking the GC root `root`. The entrypoint will
8444 // be loaded by the slow path code.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008445 SlowPathCodeARMVIXL* slow_path =
Roland Levillain6d729a72017-06-30 18:34:01 +01008446 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARMVIXL(instruction, root);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008447 codegen_->AddSlowPath(slow_path);
8448
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008449 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
8450 GetAssembler()->LoadFromOffset(kLoadWord, root_reg, obj, offset);
8451 static_assert(
8452 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
8453 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
8454 "have different sizes.");
8455 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
8456 "art::mirror::CompressedReference<mirror::Object> and int32_t "
8457 "have different sizes.");
8458
Roland Levillain6d729a72017-06-30 18:34:01 +01008459 __ CompareAndBranchIfNonZero(mr, slow_path->GetEntryLabel());
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008460 __ Bind(slow_path->GetExitLabel());
8461 }
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008462 } else {
8463 // GC root loaded through a slow path for read barriers other
8464 // than Baker's.
8465 // /* GcRoot<mirror::Object>* */ root = obj + offset
8466 __ Add(root_reg, obj, offset);
8467 // /* mirror::Object* */ root = root->Read()
8468 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
8469 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01008470 } else {
8471 // Plain GC root load with no read barrier.
8472 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
8473 GetAssembler()->LoadFromOffset(kLoadWord, root_reg, obj, offset);
8474 // Note that GC roots are not affected by heap poisoning, thus we
8475 // do not have to unpoison `root_reg` here.
8476 }
Roland Levillain5daa4952017-07-03 17:23:56 +01008477 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 18);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01008478}
8479
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008480void CodeGeneratorARMVIXL::MaybeAddBakerCcEntrypointTempForFields(LocationSummary* locations) {
8481 DCHECK(kEmitCompilerReadBarrier);
8482 DCHECK(kUseBakerReadBarrier);
8483 if (kBakerReadBarrierLinkTimeThunksEnableForFields) {
8484 if (!Runtime::Current()->UseJitCompilation()) {
8485 locations->AddTemp(Location::RegisterLocation(kBakerCcEntrypointRegister.GetCode()));
8486 }
8487 }
8488}
8489
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008490void CodeGeneratorARMVIXL::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
8491 Location ref,
8492 vixl32::Register obj,
8493 uint32_t offset,
8494 Location temp,
8495 bool needs_null_check) {
8496 DCHECK(kEmitCompilerReadBarrier);
8497 DCHECK(kUseBakerReadBarrier);
8498
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008499 if (kBakerReadBarrierLinkTimeThunksEnableForFields &&
8500 !Runtime::Current()->UseJitCompilation()) {
Roland Levillain6d729a72017-06-30 18:34:01 +01008501 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in the
8502 // Marking Register) to decide whether we need to enter the slow
8503 // path to mark the reference. Then, in the slow path, check the
8504 // gray bit in the lock word of the reference's holder (`obj`) to
8505 // decide whether to mark `ref` or not.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008506 //
8507 // We use link-time generated thunks for the slow path. That thunk checks
8508 // the holder and jumps to the entrypoint if needed. If the holder is not
8509 // gray, it creates a fake dependency and returns to the LDR instruction.
8510 //
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008511 // lr = &gray_return_address;
Roland Levillain6d729a72017-06-30 18:34:01 +01008512 // if (mr) { // Thread::Current()->GetIsGcMarking()
8513 // goto field_thunk<holder_reg, base_reg>(lr)
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008514 // }
8515 // not_gray_return_address:
8516 // // Original reference load. If the offset is too large to fit
8517 // // into LDR, we use an adjusted base register here.
Vladimir Marko88abba22017-05-03 17:09:25 +01008518 // HeapReference<mirror::Object> reference = *(obj+offset);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008519 // gray_return_address:
8520
8521 DCHECK_ALIGNED(offset, sizeof(mirror::HeapReference<mirror::Object>));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008522 vixl32::Register ref_reg = RegisterFrom(ref, DataType::Type::kReference);
Vladimir Marko88abba22017-05-03 17:09:25 +01008523 bool narrow = CanEmitNarrowLdr(ref_reg, obj, offset);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008524 vixl32::Register base = obj;
8525 if (offset >= kReferenceLoadMinFarOffset) {
8526 base = RegisterFrom(temp);
8527 DCHECK(!base.Is(kBakerCcEntrypointRegister));
8528 static_assert(IsPowerOfTwo(kReferenceLoadMinFarOffset), "Expecting a power of 2.");
8529 __ Add(base, obj, Operand(offset & ~(kReferenceLoadMinFarOffset - 1u)));
8530 offset &= (kReferenceLoadMinFarOffset - 1u);
Vladimir Marko88abba22017-05-03 17:09:25 +01008531 // Use narrow LDR only for small offsets. Generating narrow encoding LDR for the large
8532 // offsets with `(offset & (kReferenceLoadMinFarOffset - 1u)) < 32u` would most likely
8533 // increase the overall code size when taking the generated thunks into account.
8534 DCHECK(!narrow);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008535 }
8536 UseScratchRegisterScope temps(GetVIXLAssembler());
8537 ExcludeIPAndBakerCcEntrypointRegister(&temps, instruction);
8538 uint32_t custom_data = linker::Thumb2RelativePatcher::EncodeBakerReadBarrierFieldData(
Vladimir Marko88abba22017-05-03 17:09:25 +01008539 base.GetCode(), obj.GetCode(), narrow);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008540 vixl32::Label* bne_label = NewBakerReadBarrierPatch(custom_data);
8541
Roland Levillain5daa4952017-07-03 17:23:56 +01008542 {
8543 vixl::EmissionCheckScope guard(
8544 GetVIXLAssembler(),
8545 (kPoisonHeapReferences ? 5u : 4u) * vixl32::kMaxInstructionSizeInBytes);
8546 vixl32::Label return_address;
8547 EmitAdrCode adr(GetVIXLAssembler(), lr, &return_address);
8548 __ cmp(mr, Operand(0));
8549 EmitPlaceholderBne(this, bne_label);
8550 ptrdiff_t old_offset = GetVIXLAssembler()->GetBuffer()->GetCursorOffset();
8551 __ ldr(EncodingSize(narrow ? Narrow : Wide), ref_reg, MemOperand(base, offset));
8552 if (needs_null_check) {
8553 MaybeRecordImplicitNullCheck(instruction);
Vladimir Marko88abba22017-05-03 17:09:25 +01008554 }
Roland Levillain5daa4952017-07-03 17:23:56 +01008555 // Note: We need a specific width for the unpoisoning NEG.
8556 if (kPoisonHeapReferences) {
8557 if (narrow) {
8558 // The only 16-bit encoding is T1 which sets flags outside IT block (i.e. RSBS, not RSB).
8559 __ rsbs(EncodingSize(Narrow), ref_reg, ref_reg, Operand(0));
8560 } else {
8561 __ rsb(EncodingSize(Wide), ref_reg, ref_reg, Operand(0));
8562 }
8563 }
8564 __ Bind(&return_address);
8565 DCHECK_EQ(old_offset - GetVIXLAssembler()->GetBuffer()->GetCursorOffset(),
8566 narrow ? BAKER_MARK_INTROSPECTION_FIELD_LDR_NARROW_OFFSET
8567 : BAKER_MARK_INTROSPECTION_FIELD_LDR_WIDE_OFFSET);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008568 }
Roland Levillain5daa4952017-07-03 17:23:56 +01008569 MaybeGenerateMarkingRegisterCheck(/* code */ 19, /* temp_loc */ LocationFrom(ip));
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008570 return;
8571 }
8572
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008573 // /* HeapReference<Object> */ ref = *(obj + offset)
8574 Location no_index = Location::NoLocation();
8575 ScaleFactor no_scale_factor = TIMES_1;
8576 GenerateReferenceLoadWithBakerReadBarrier(
8577 instruction, ref, obj, offset, no_index, no_scale_factor, temp, needs_null_check);
Roland Levillain6070e882016-11-03 17:51:58 +00008578}
8579
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008580void CodeGeneratorARMVIXL::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
8581 Location ref,
8582 vixl32::Register obj,
8583 uint32_t data_offset,
8584 Location index,
8585 Location temp,
8586 bool needs_null_check) {
8587 DCHECK(kEmitCompilerReadBarrier);
8588 DCHECK(kUseBakerReadBarrier);
8589
8590 static_assert(
8591 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
8592 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008593 ScaleFactor scale_factor = TIMES_4;
8594
8595 if (kBakerReadBarrierLinkTimeThunksEnableForArrays &&
8596 !Runtime::Current()->UseJitCompilation()) {
Roland Levillain6d729a72017-06-30 18:34:01 +01008597 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in the
8598 // Marking Register) to decide whether we need to enter the slow
8599 // path to mark the reference. Then, in the slow path, check the
8600 // gray bit in the lock word of the reference's holder (`obj`) to
8601 // decide whether to mark `ref` or not.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008602 //
8603 // We use link-time generated thunks for the slow path. That thunk checks
8604 // the holder and jumps to the entrypoint if needed. If the holder is not
8605 // gray, it creates a fake dependency and returns to the LDR instruction.
8606 //
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008607 // lr = &gray_return_address;
Roland Levillain6d729a72017-06-30 18:34:01 +01008608 // if (mr) { // Thread::Current()->GetIsGcMarking()
8609 // goto array_thunk<base_reg>(lr)
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008610 // }
8611 // not_gray_return_address:
8612 // // Original reference load. If the offset is too large to fit
8613 // // into LDR, we use an adjusted base register here.
Vladimir Marko88abba22017-05-03 17:09:25 +01008614 // HeapReference<mirror::Object> reference = data[index];
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008615 // gray_return_address:
8616
8617 DCHECK(index.IsValid());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008618 vixl32::Register index_reg = RegisterFrom(index, DataType::Type::kInt32);
8619 vixl32::Register ref_reg = RegisterFrom(ref, DataType::Type::kReference);
8620 vixl32::Register data_reg = RegisterFrom(temp, DataType::Type::kInt32); // Raw pointer.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008621 DCHECK(!data_reg.Is(kBakerCcEntrypointRegister));
8622
8623 UseScratchRegisterScope temps(GetVIXLAssembler());
8624 ExcludeIPAndBakerCcEntrypointRegister(&temps, instruction);
8625 uint32_t custom_data =
8626 linker::Thumb2RelativePatcher::EncodeBakerReadBarrierArrayData(data_reg.GetCode());
8627 vixl32::Label* bne_label = NewBakerReadBarrierPatch(custom_data);
8628
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008629 __ Add(data_reg, obj, Operand(data_offset));
Roland Levillain5daa4952017-07-03 17:23:56 +01008630 {
8631 vixl::EmissionCheckScope guard(
8632 GetVIXLAssembler(),
8633 (kPoisonHeapReferences ? 5u : 4u) * vixl32::kMaxInstructionSizeInBytes);
8634 vixl32::Label return_address;
8635 EmitAdrCode adr(GetVIXLAssembler(), lr, &return_address);
8636 __ cmp(mr, Operand(0));
8637 EmitPlaceholderBne(this, bne_label);
8638 ptrdiff_t old_offset = GetVIXLAssembler()->GetBuffer()->GetCursorOffset();
8639 __ ldr(ref_reg, MemOperand(data_reg, index_reg, vixl32::LSL, scale_factor));
8640 DCHECK(!needs_null_check); // The thunk cannot handle the null check.
8641 // Note: We need a Wide NEG for the unpoisoning.
8642 if (kPoisonHeapReferences) {
8643 __ rsb(EncodingSize(Wide), ref_reg, ref_reg, Operand(0));
8644 }
8645 __ Bind(&return_address);
8646 DCHECK_EQ(old_offset - GetVIXLAssembler()->GetBuffer()->GetCursorOffset(),
8647 BAKER_MARK_INTROSPECTION_ARRAY_LDR_OFFSET);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008648 }
Roland Levillain5daa4952017-07-03 17:23:56 +01008649 MaybeGenerateMarkingRegisterCheck(/* code */ 20, /* temp_loc */ LocationFrom(ip));
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008650 return;
8651 }
8652
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008653 // /* HeapReference<Object> */ ref =
8654 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008655 GenerateReferenceLoadWithBakerReadBarrier(
8656 instruction, ref, obj, data_offset, index, scale_factor, temp, needs_null_check);
Roland Levillain6070e882016-11-03 17:51:58 +00008657}
8658
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008659void CodeGeneratorARMVIXL::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
8660 Location ref,
8661 vixl32::Register obj,
8662 uint32_t offset,
8663 Location index,
8664 ScaleFactor scale_factor,
8665 Location temp,
Roland Levillainff487002017-03-07 16:50:01 +00008666 bool needs_null_check) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008667 DCHECK(kEmitCompilerReadBarrier);
8668 DCHECK(kUseBakerReadBarrier);
8669
Roland Levillain6d729a72017-06-30 18:34:01 +01008670 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in the
8671 // Marking Register) to decide whether we need to enter the slow
8672 // path to mark the reference. Then, in the slow path, check the
8673 // gray bit in the lock word of the reference's holder (`obj`) to
8674 // decide whether to mark `ref` or not.
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008675 //
Roland Levillain6d729a72017-06-30 18:34:01 +01008676 // if (mr) { // Thread::Current()->GetIsGcMarking()
Roland Levillainff487002017-03-07 16:50:01 +00008677 // // Slow path.
8678 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
8679 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
8680 // HeapReference<mirror::Object> ref = *src; // Original reference load.
8681 // bool is_gray = (rb_state == ReadBarrier::GrayState());
8682 // if (is_gray) {
Roland Levillain6d729a72017-06-30 18:34:01 +01008683 // entrypoint = Thread::Current()->pReadBarrierMarkReg ## root.reg()
8684 // ref = entrypoint(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
Roland Levillainff487002017-03-07 16:50:01 +00008685 // }
8686 // } else {
8687 // HeapReference<mirror::Object> ref = *src; // Original reference load.
8688 // }
8689
8690 vixl32::Register temp_reg = RegisterFrom(temp);
8691
8692 // Slow path marking the object `ref` when the GC is marking. The
Roland Levillain6d729a72017-06-30 18:34:01 +01008693 // entrypoint will be loaded by the slow path code.
Roland Levillainff487002017-03-07 16:50:01 +00008694 SlowPathCodeARMVIXL* slow_path =
8695 new (GetGraph()->GetArena()) LoadReferenceWithBakerReadBarrierSlowPathARMVIXL(
Roland Levillain6d729a72017-06-30 18:34:01 +01008696 instruction, ref, obj, offset, index, scale_factor, needs_null_check, temp_reg);
Roland Levillainff487002017-03-07 16:50:01 +00008697 AddSlowPath(slow_path);
8698
Roland Levillain6d729a72017-06-30 18:34:01 +01008699 __ CompareAndBranchIfNonZero(mr, slow_path->GetEntryLabel());
Roland Levillainff487002017-03-07 16:50:01 +00008700 // Fast path: the GC is not marking: just load the reference.
8701 GenerateRawReferenceLoad(instruction, ref, obj, offset, index, scale_factor, needs_null_check);
8702 __ Bind(slow_path->GetExitLabel());
Roland Levillain5daa4952017-07-03 17:23:56 +01008703 MaybeGenerateMarkingRegisterCheck(/* code */ 21);
Roland Levillainff487002017-03-07 16:50:01 +00008704}
8705
8706void CodeGeneratorARMVIXL::UpdateReferenceFieldWithBakerReadBarrier(HInstruction* instruction,
8707 Location ref,
8708 vixl32::Register obj,
8709 Location field_offset,
8710 Location temp,
8711 bool needs_null_check,
8712 vixl32::Register temp2) {
8713 DCHECK(kEmitCompilerReadBarrier);
8714 DCHECK(kUseBakerReadBarrier);
8715
Roland Levillain6d729a72017-06-30 18:34:01 +01008716 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in the
8717 // Marking Register) to decide whether we need to enter the slow
8718 // path to update the reference field within `obj`. Then, in the
8719 // slow path, check the gray bit in the lock word of the reference's
8720 // holder (`obj`) to decide whether to mark `ref` and update the
8721 // field or not.
Roland Levillainff487002017-03-07 16:50:01 +00008722 //
Roland Levillain6d729a72017-06-30 18:34:01 +01008723 // if (mr) { // Thread::Current()->GetIsGcMarking()
Roland Levillainba650a42017-03-06 13:52:32 +00008724 // // Slow path.
Roland Levillain54f869e2017-03-06 13:54:11 +00008725 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
8726 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
Roland Levillainff487002017-03-07 16:50:01 +00008727 // HeapReference<mirror::Object> ref = *(obj + field_offset); // Reference load.
Roland Levillain54f869e2017-03-06 13:54:11 +00008728 // bool is_gray = (rb_state == ReadBarrier::GrayState());
8729 // if (is_gray) {
Roland Levillainff487002017-03-07 16:50:01 +00008730 // old_ref = ref;
Roland Levillain6d729a72017-06-30 18:34:01 +01008731 // entrypoint = Thread::Current()->pReadBarrierMarkReg ## root.reg()
8732 // ref = entrypoint(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
Roland Levillainff487002017-03-07 16:50:01 +00008733 // compareAndSwapObject(obj, field_offset, old_ref, ref);
Roland Levillain54f869e2017-03-06 13:54:11 +00008734 // }
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008735 // }
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008736
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008737 vixl32::Register temp_reg = RegisterFrom(temp);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008738
Roland Levillainff487002017-03-07 16:50:01 +00008739 // Slow path updating the object reference at address `obj + field_offset`
Roland Levillain6d729a72017-06-30 18:34:01 +01008740 // when the GC is marking. The entrypoint will be loaded by the slow path code.
Roland Levillainff487002017-03-07 16:50:01 +00008741 SlowPathCodeARMVIXL* slow_path =
8742 new (GetGraph()->GetArena()) LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARMVIXL(
8743 instruction,
8744 ref,
8745 obj,
8746 /* offset */ 0u,
8747 /* index */ field_offset,
8748 /* scale_factor */ ScaleFactor::TIMES_1,
8749 needs_null_check,
8750 temp_reg,
Roland Levillain6d729a72017-06-30 18:34:01 +01008751 temp2);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008752 AddSlowPath(slow_path);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008753
Roland Levillain6d729a72017-06-30 18:34:01 +01008754 __ CompareAndBranchIfNonZero(mr, slow_path->GetEntryLabel());
Roland Levillainff487002017-03-07 16:50:01 +00008755 // Fast path: the GC is not marking: nothing to do (the field is
8756 // up-to-date, and we don't need to load the reference).
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008757 __ Bind(slow_path->GetExitLabel());
Roland Levillain5daa4952017-07-03 17:23:56 +01008758 MaybeGenerateMarkingRegisterCheck(/* code */ 22);
Roland Levillain844e6532016-11-03 16:09:47 +00008759}
Scott Wakelingfe885462016-09-22 10:24:38 +01008760
Roland Levillainba650a42017-03-06 13:52:32 +00008761void CodeGeneratorARMVIXL::GenerateRawReferenceLoad(HInstruction* instruction,
8762 Location ref,
8763 vixl::aarch32::Register obj,
8764 uint32_t offset,
8765 Location index,
8766 ScaleFactor scale_factor,
8767 bool needs_null_check) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008768 DataType::Type type = DataType::Type::kReference;
Roland Levillainba650a42017-03-06 13:52:32 +00008769 vixl32::Register ref_reg = RegisterFrom(ref, type);
8770
8771 // If needed, vixl::EmissionCheckScope guards are used to ensure
8772 // that no pools are emitted between the load (macro) instruction
8773 // and MaybeRecordImplicitNullCheck.
8774
Scott Wakelingfe885462016-09-22 10:24:38 +01008775 if (index.IsValid()) {
8776 // Load types involving an "index": ArrayGet,
8777 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
8778 // intrinsics.
Roland Levillainba650a42017-03-06 13:52:32 +00008779 // /* HeapReference<mirror::Object> */ ref = *(obj + offset + (index << scale_factor))
Scott Wakelingfe885462016-09-22 10:24:38 +01008780 if (index.IsConstant()) {
8781 size_t computed_offset =
8782 (Int32ConstantFrom(index) << scale_factor) + offset;
Roland Levillainba650a42017-03-06 13:52:32 +00008783 vixl::EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Scott Wakelingfe885462016-09-22 10:24:38 +01008784 GetAssembler()->LoadFromOffset(kLoadWord, ref_reg, obj, computed_offset);
Roland Levillainba650a42017-03-06 13:52:32 +00008785 if (needs_null_check) {
8786 MaybeRecordImplicitNullCheck(instruction);
8787 }
Scott Wakelingfe885462016-09-22 10:24:38 +01008788 } else {
8789 // Handle the special case of the
8790 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
8791 // intrinsics, which use a register pair as index ("long
8792 // offset"), of which only the low part contains data.
8793 vixl32::Register index_reg = index.IsRegisterPair()
8794 ? LowRegisterFrom(index)
8795 : RegisterFrom(index);
8796 UseScratchRegisterScope temps(GetVIXLAssembler());
Roland Levillainba650a42017-03-06 13:52:32 +00008797 vixl32::Register temp = temps.Acquire();
8798 __ Add(temp, obj, Operand(index_reg, ShiftType::LSL, scale_factor));
8799 {
8800 vixl::EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
8801 GetAssembler()->LoadFromOffset(kLoadWord, ref_reg, temp, offset);
8802 if (needs_null_check) {
8803 MaybeRecordImplicitNullCheck(instruction);
8804 }
8805 }
Scott Wakelingfe885462016-09-22 10:24:38 +01008806 }
8807 } else {
Roland Levillainba650a42017-03-06 13:52:32 +00008808 // /* HeapReference<mirror::Object> */ ref = *(obj + offset)
8809 vixl::EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Scott Wakelingfe885462016-09-22 10:24:38 +01008810 GetAssembler()->LoadFromOffset(kLoadWord, ref_reg, obj, offset);
Roland Levillainba650a42017-03-06 13:52:32 +00008811 if (needs_null_check) {
8812 MaybeRecordImplicitNullCheck(instruction);
8813 }
Scott Wakelingfe885462016-09-22 10:24:38 +01008814 }
8815
Roland Levillain844e6532016-11-03 16:09:47 +00008816 // Object* ref = ref_addr->AsMirrorPtr()
8817 GetAssembler()->MaybeUnpoisonHeapReference(ref_reg);
Roland Levillain844e6532016-11-03 16:09:47 +00008818}
8819
Roland Levillain5daa4952017-07-03 17:23:56 +01008820void CodeGeneratorARMVIXL::MaybeGenerateMarkingRegisterCheck(int code, Location temp_loc) {
8821 // The following condition is a compile-time one, so it does not have a run-time cost.
8822 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier && kIsDebugBuild) {
8823 // The following condition is a run-time one; it is executed after the
8824 // previous compile-time test, to avoid penalizing non-debug builds.
8825 if (GetCompilerOptions().EmitRunTimeChecksInDebugMode()) {
8826 UseScratchRegisterScope temps(GetVIXLAssembler());
8827 vixl32::Register temp = temp_loc.IsValid() ? RegisterFrom(temp_loc) : temps.Acquire();
8828 GetAssembler()->GenerateMarkingRegisterCheck(temp,
8829 kMarkingRegisterCheckBreakCodeBaseCode + code);
8830 }
8831 }
8832}
8833
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008834void CodeGeneratorARMVIXL::GenerateReadBarrierSlow(HInstruction* instruction,
8835 Location out,
8836 Location ref,
8837 Location obj,
8838 uint32_t offset,
8839 Location index) {
8840 DCHECK(kEmitCompilerReadBarrier);
8841
8842 // Insert a slow path based read barrier *after* the reference load.
8843 //
8844 // If heap poisoning is enabled, the unpoisoning of the loaded
8845 // reference will be carried out by the runtime within the slow
8846 // path.
8847 //
8848 // Note that `ref` currently does not get unpoisoned (when heap
8849 // poisoning is enabled), which is alright as the `ref` argument is
8850 // not used by the artReadBarrierSlow entry point.
8851 //
8852 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
8853 SlowPathCodeARMVIXL* slow_path = new (GetGraph()->GetArena())
8854 ReadBarrierForHeapReferenceSlowPathARMVIXL(instruction, out, ref, obj, offset, index);
8855 AddSlowPath(slow_path);
8856
8857 __ B(slow_path->GetEntryLabel());
8858 __ Bind(slow_path->GetExitLabel());
8859}
8860
8861void CodeGeneratorARMVIXL::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
Artem Serov02d37832016-10-25 15:25:33 +01008862 Location out,
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008863 Location ref,
8864 Location obj,
8865 uint32_t offset,
8866 Location index) {
Artem Serov02d37832016-10-25 15:25:33 +01008867 if (kEmitCompilerReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008868 // Baker's read barriers shall be handled by the fast path
Roland Levillain9983e302017-07-14 14:34:22 +01008869 // (CodeGeneratorARMVIXL::GenerateReferenceLoadWithBakerReadBarrier).
Artem Serov02d37832016-10-25 15:25:33 +01008870 DCHECK(!kUseBakerReadBarrier);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008871 // If heap poisoning is enabled, unpoisoning will be taken care of
8872 // by the runtime within the slow path.
8873 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Artem Serov02d37832016-10-25 15:25:33 +01008874 } else if (kPoisonHeapReferences) {
8875 GetAssembler()->UnpoisonHeapReference(RegisterFrom(out));
8876 }
8877}
8878
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008879void CodeGeneratorARMVIXL::GenerateReadBarrierForRootSlow(HInstruction* instruction,
8880 Location out,
8881 Location root) {
8882 DCHECK(kEmitCompilerReadBarrier);
8883
8884 // Insert a slow path based read barrier *after* the GC root load.
8885 //
8886 // Note that GC roots are not affected by heap poisoning, so we do
8887 // not need to do anything special for this here.
8888 SlowPathCodeARMVIXL* slow_path =
8889 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathARMVIXL(instruction, out, root);
8890 AddSlowPath(slow_path);
8891
8892 __ B(slow_path->GetEntryLabel());
8893 __ Bind(slow_path->GetExitLabel());
8894}
8895
Artem Serov02d37832016-10-25 15:25:33 +01008896// Check if the desired_dispatch_info is supported. If it is, return it,
8897// otherwise return a fall-back info that should be used instead.
8898HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARMVIXL::GetSupportedInvokeStaticOrDirectDispatch(
Artem Serovd4cc5b22016-11-04 11:19:09 +00008899 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +00008900 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Nicolas Geoffraye807ff72017-01-23 09:03:12 +00008901 return desired_dispatch_info;
Artem Serov02d37832016-10-25 15:25:33 +01008902}
8903
Scott Wakelingfe885462016-09-22 10:24:38 +01008904vixl32::Register CodeGeneratorARMVIXL::GetInvokeStaticOrDirectExtraParameter(
8905 HInvokeStaticOrDirect* invoke, vixl32::Register temp) {
8906 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
8907 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
8908 if (!invoke->GetLocations()->Intrinsified()) {
8909 return RegisterFrom(location);
8910 }
8911 // For intrinsics we allow any location, so it may be on the stack.
8912 if (!location.IsRegister()) {
8913 GetAssembler()->LoadFromOffset(kLoadWord, temp, sp, location.GetStackIndex());
8914 return temp;
8915 }
8916 // For register locations, check if the register was saved. If so, get it from the stack.
8917 // Note: There is a chance that the register was saved but not overwritten, so we could
8918 // save one load. However, since this is just an intrinsic slow path we prefer this
8919 // simple and more robust approach rather that trying to determine if that's the case.
8920 SlowPathCode* slow_path = GetCurrentSlowPath();
Scott Wakelingd5cd4972017-02-03 11:38:35 +00008921 if (slow_path != nullptr && slow_path->IsCoreRegisterSaved(RegisterFrom(location).GetCode())) {
Scott Wakelingfe885462016-09-22 10:24:38 +01008922 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(RegisterFrom(location).GetCode());
8923 GetAssembler()->LoadFromOffset(kLoadWord, temp, sp, stack_offset);
8924 return temp;
8925 }
8926 return RegisterFrom(location);
8927}
8928
Vladimir Markod254f5c2017-06-02 15:18:36 +00008929void CodeGeneratorARMVIXL::GenerateStaticOrDirectCall(
Vladimir Markoe7197bf2017-06-02 17:00:23 +01008930 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00008931 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
Scott Wakelingfe885462016-09-22 10:24:38 +01008932 switch (invoke->GetMethodLoadKind()) {
8933 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
8934 uint32_t offset =
8935 GetThreadOffset<kArmPointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
8936 // temp = thread->string_init_entrypoint
Artem Serovd4cc5b22016-11-04 11:19:09 +00008937 GetAssembler()->LoadFromOffset(kLoadWord, RegisterFrom(temp), tr, offset);
8938 break;
8939 }
8940 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
8941 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
8942 break;
Vladimir Marko65979462017-05-19 17:25:12 +01008943 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative: {
8944 DCHECK(GetCompilerOptions().IsBootImage());
8945 PcRelativePatchInfo* labels = NewPcRelativeMethodPatch(invoke->GetTargetMethod());
8946 vixl32::Register temp_reg = RegisterFrom(temp);
8947 EmitMovwMovtPlaceholder(labels, temp_reg);
8948 break;
8949 }
Artem Serovd4cc5b22016-11-04 11:19:09 +00008950 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
8951 __ Mov(RegisterFrom(temp), Operand::From(invoke->GetMethodAddress()));
8952 break;
Vladimir Marko0eb882b2017-05-15 13:39:18 +01008953 case HInvokeStaticOrDirect::MethodLoadKind::kBssEntry: {
8954 PcRelativePatchInfo* labels = NewMethodBssEntryPatch(
8955 MethodReference(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex()));
8956 vixl32::Register temp_reg = RegisterFrom(temp);
8957 EmitMovwMovtPlaceholder(labels, temp_reg);
8958 GetAssembler()->LoadFromOffset(kLoadWord, temp_reg, temp_reg, /* offset*/ 0);
Scott Wakelingfe885462016-09-22 10:24:38 +01008959 break;
8960 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01008961 case HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall: {
8962 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
8963 return; // No code pointer retrieval; the runtime performs the call directly.
Scott Wakelingfe885462016-09-22 10:24:38 +01008964 }
Scott Wakelingfe885462016-09-22 10:24:38 +01008965 }
8966
Artem Serovd4cc5b22016-11-04 11:19:09 +00008967 switch (invoke->GetCodePtrLocation()) {
8968 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Vladimir Markoe7197bf2017-06-02 17:00:23 +01008969 {
8970 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
8971 ExactAssemblyScope aas(GetVIXLAssembler(),
8972 vixl32::k32BitT32InstructionSizeInBytes,
8973 CodeBufferCheckScope::kMaximumSize);
8974 __ bl(GetFrameEntryLabel());
8975 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
8976 }
Artem Serovd4cc5b22016-11-04 11:19:09 +00008977 break;
Artem Serovd4cc5b22016-11-04 11:19:09 +00008978 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
8979 // LR = callee_method->entry_point_from_quick_compiled_code_
8980 GetAssembler()->LoadFromOffset(
8981 kLoadWord,
8982 lr,
8983 RegisterFrom(callee_method),
8984 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize).Int32Value());
Alexandre Rames374ddf32016-11-04 10:40:49 +00008985 {
Vladimir Markoe7197bf2017-06-02 17:00:23 +01008986 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
Alexandre Rames374ddf32016-11-04 10:40:49 +00008987 // 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 +00008988 ExactAssemblyScope aas(GetVIXLAssembler(),
8989 vixl32::k16BitT32InstructionSizeInBytes,
8990 CodeBufferCheckScope::kExactSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00008991 // LR()
8992 __ blx(lr);
Vladimir Markoe7197bf2017-06-02 17:00:23 +01008993 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Alexandre Rames374ddf32016-11-04 10:40:49 +00008994 }
Artem Serovd4cc5b22016-11-04 11:19:09 +00008995 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01008996 }
8997
Scott Wakelingfe885462016-09-22 10:24:38 +01008998 DCHECK(!IsLeafMethod());
8999}
9000
Vladimir Markoe7197bf2017-06-02 17:00:23 +01009001void CodeGeneratorARMVIXL::GenerateVirtualCall(
9002 HInvokeVirtual* invoke, Location temp_location, SlowPathCode* slow_path) {
Scott Wakelingfe885462016-09-22 10:24:38 +01009003 vixl32::Register temp = RegisterFrom(temp_location);
9004 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
9005 invoke->GetVTableIndex(), kArmPointerSize).Uint32Value();
9006
9007 // Use the calling convention instead of the location of the receiver, as
9008 // intrinsics may have put the receiver in a different register. In the intrinsics
9009 // slow path, the arguments have been moved to the right place, so here we are
9010 // guaranteed that the receiver is the first register of the calling convention.
9011 InvokeDexCallingConventionARMVIXL calling_convention;
9012 vixl32::Register receiver = calling_convention.GetRegisterAt(0);
9013 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Alexandre Rames374ddf32016-11-04 10:40:49 +00009014 {
9015 // Make sure the pc is recorded immediately after the `ldr` instruction.
Artem Serov0fb37192016-12-06 18:13:40 +00009016 ExactAssemblyScope aas(GetVIXLAssembler(),
9017 vixl32::kMaxInstructionSizeInBytes,
9018 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00009019 // /* HeapReference<Class> */ temp = receiver->klass_
9020 __ ldr(temp, MemOperand(receiver, class_offset));
9021 MaybeRecordImplicitNullCheck(invoke);
9022 }
Scott Wakelingfe885462016-09-22 10:24:38 +01009023 // Instead of simply (possibly) unpoisoning `temp` here, we should
9024 // emit a read barrier for the previous class reference load.
9025 // However this is not required in practice, as this is an
9026 // intermediate/temporary reference and because the current
9027 // concurrent copying collector keeps the from-space memory
9028 // intact/accessible until the end of the marking phase (the
9029 // concurrent copying collector may not in the future).
9030 GetAssembler()->MaybeUnpoisonHeapReference(temp);
9031
9032 // temp = temp->GetMethodAt(method_offset);
9033 uint32_t entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(
9034 kArmPointerSize).Int32Value();
9035 GetAssembler()->LoadFromOffset(kLoadWord, temp, temp, method_offset);
9036 // LR = temp->GetEntryPoint();
9037 GetAssembler()->LoadFromOffset(kLoadWord, lr, temp, entry_point);
Vladimir Markoe7197bf2017-06-02 17:00:23 +01009038 {
9039 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
9040 // blx in T32 has only 16bit encoding that's why a stricter check for the scope is used.
9041 ExactAssemblyScope aas(GetVIXLAssembler(),
9042 vixl32::k16BitT32InstructionSizeInBytes,
9043 CodeBufferCheckScope::kExactSize);
9044 // LR();
9045 __ blx(lr);
9046 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
9047 }
Scott Wakelingfe885462016-09-22 10:24:38 +01009048}
9049
Vladimir Marko65979462017-05-19 17:25:12 +01009050CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewPcRelativeMethodPatch(
9051 MethodReference target_method) {
9052 return NewPcRelativePatch(*target_method.dex_file,
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07009053 target_method.index,
Vladimir Marko65979462017-05-19 17:25:12 +01009054 &pc_relative_method_patches_);
Artem Serovd4cc5b22016-11-04 11:19:09 +00009055}
9056
Vladimir Marko0eb882b2017-05-15 13:39:18 +01009057CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewMethodBssEntryPatch(
9058 MethodReference target_method) {
9059 return NewPcRelativePatch(*target_method.dex_file,
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07009060 target_method.index,
Vladimir Marko0eb882b2017-05-15 13:39:18 +01009061 &method_bss_entry_patches_);
9062}
9063
Artem Serovd4cc5b22016-11-04 11:19:09 +00009064CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewPcRelativeTypePatch(
9065 const DexFile& dex_file, dex::TypeIndex type_index) {
9066 return NewPcRelativePatch(dex_file, type_index.index_, &pc_relative_type_patches_);
9067}
9068
Vladimir Marko1998cd02017-01-13 13:02:58 +00009069CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewTypeBssEntryPatch(
9070 const DexFile& dex_file, dex::TypeIndex type_index) {
9071 return NewPcRelativePatch(dex_file, type_index.index_, &type_bss_entry_patches_);
9072}
9073
Vladimir Marko65979462017-05-19 17:25:12 +01009074CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewPcRelativeStringPatch(
9075 const DexFile& dex_file, dex::StringIndex string_index) {
9076 return NewPcRelativePatch(dex_file, string_index.index_, &pc_relative_string_patches_);
9077}
9078
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01009079CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewStringBssEntryPatch(
9080 const DexFile& dex_file, dex::StringIndex string_index) {
9081 return NewPcRelativePatch(dex_file, string_index.index_, &string_bss_entry_patches_);
9082}
9083
Artem Serovd4cc5b22016-11-04 11:19:09 +00009084CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewPcRelativePatch(
9085 const DexFile& dex_file, uint32_t offset_or_index, ArenaDeque<PcRelativePatchInfo>* patches) {
9086 patches->emplace_back(dex_file, offset_or_index);
9087 return &patches->back();
9088}
9089
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01009090vixl::aarch32::Label* CodeGeneratorARMVIXL::NewBakerReadBarrierPatch(uint32_t custom_data) {
9091 baker_read_barrier_patches_.emplace_back(custom_data);
9092 return &baker_read_barrier_patches_.back().label;
9093}
9094
Artem Serovc5fcb442016-12-02 19:19:58 +00009095VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateBootImageAddressLiteral(uint32_t address) {
Richard Uhlerc52f3032017-03-02 13:45:45 +00009096 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), &uint32_literals_);
Artem Serovc5fcb442016-12-02 19:19:58 +00009097}
9098
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00009099VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateJitStringLiteral(
9100 const DexFile& dex_file,
9101 dex::StringIndex string_index,
9102 Handle<mirror::String> handle) {
9103 jit_string_roots_.Overwrite(StringReference(&dex_file, string_index),
9104 reinterpret_cast64<uint64_t>(handle.GetReference()));
Artem Serovc5fcb442016-12-02 19:19:58 +00009105 return jit_string_patches_.GetOrCreate(
9106 StringReference(&dex_file, string_index),
9107 [this]() {
9108 return GetAssembler()->CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u);
9109 });
9110}
9111
9112VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateJitClassLiteral(const DexFile& dex_file,
9113 dex::TypeIndex type_index,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00009114 Handle<mirror::Class> handle) {
9115 jit_class_roots_.Overwrite(TypeReference(&dex_file, type_index),
9116 reinterpret_cast64<uint64_t>(handle.GetReference()));
Artem Serovc5fcb442016-12-02 19:19:58 +00009117 return jit_class_patches_.GetOrCreate(
9118 TypeReference(&dex_file, type_index),
9119 [this]() {
9120 return GetAssembler()->CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u);
9121 });
9122}
9123
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01009124template <linker::LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
Artem Serovd4cc5b22016-11-04 11:19:09 +00009125inline void CodeGeneratorARMVIXL::EmitPcRelativeLinkerPatches(
9126 const ArenaDeque<PcRelativePatchInfo>& infos,
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01009127 ArenaVector<linker::LinkerPatch>* linker_patches) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00009128 for (const PcRelativePatchInfo& info : infos) {
9129 const DexFile& dex_file = info.target_dex_file;
9130 size_t offset_or_index = info.offset_or_index;
9131 DCHECK(info.add_pc_label.IsBound());
9132 uint32_t add_pc_offset = dchecked_integral_cast<uint32_t>(info.add_pc_label.GetLocation());
9133 // Add MOVW patch.
9134 DCHECK(info.movw_label.IsBound());
9135 uint32_t movw_offset = dchecked_integral_cast<uint32_t>(info.movw_label.GetLocation());
9136 linker_patches->push_back(Factory(movw_offset, &dex_file, add_pc_offset, offset_or_index));
9137 // Add MOVT patch.
9138 DCHECK(info.movt_label.IsBound());
9139 uint32_t movt_offset = dchecked_integral_cast<uint32_t>(info.movt_label.GetLocation());
9140 linker_patches->push_back(Factory(movt_offset, &dex_file, add_pc_offset, offset_or_index));
9141 }
9142}
9143
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01009144void CodeGeneratorARMVIXL::EmitLinkerPatches(ArenaVector<linker::LinkerPatch>* linker_patches) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00009145 DCHECK(linker_patches->empty());
9146 size_t size =
Vladimir Marko65979462017-05-19 17:25:12 +01009147 /* MOVW+MOVT for each entry */ 2u * pc_relative_method_patches_.size() +
Vladimir Marko0eb882b2017-05-15 13:39:18 +01009148 /* MOVW+MOVT for each entry */ 2u * method_bss_entry_patches_.size() +
Artem Serovc5fcb442016-12-02 19:19:58 +00009149 /* MOVW+MOVT for each entry */ 2u * pc_relative_type_patches_.size() +
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01009150 /* MOVW+MOVT for each entry */ 2u * type_bss_entry_patches_.size() +
Vladimir Marko65979462017-05-19 17:25:12 +01009151 /* MOVW+MOVT for each entry */ 2u * pc_relative_string_patches_.size() +
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01009152 /* MOVW+MOVT for each entry */ 2u * string_bss_entry_patches_.size() +
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01009153 baker_read_barrier_patches_.size();
Artem Serovd4cc5b22016-11-04 11:19:09 +00009154 linker_patches->reserve(size);
Vladimir Marko65979462017-05-19 17:25:12 +01009155 if (GetCompilerOptions().IsBootImage()) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01009156 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeMethodPatch>(
9157 pc_relative_method_patches_, linker_patches);
9158 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeTypePatch>(
9159 pc_relative_type_patches_, linker_patches);
9160 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeStringPatch>(
9161 pc_relative_string_patches_, linker_patches);
Vladimir Marko65979462017-05-19 17:25:12 +01009162 } else {
9163 DCHECK(pc_relative_method_patches_.empty());
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01009164 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeClassTablePatch>(
9165 pc_relative_type_patches_, linker_patches);
9166 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringInternTablePatch>(
9167 pc_relative_string_patches_, linker_patches);
Artem Serovd4cc5b22016-11-04 11:19:09 +00009168 }
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01009169 EmitPcRelativeLinkerPatches<linker::LinkerPatch::MethodBssEntryPatch>(
9170 method_bss_entry_patches_, linker_patches);
9171 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeBssEntryPatch>(
9172 type_bss_entry_patches_, linker_patches);
9173 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringBssEntryPatch>(
9174 string_bss_entry_patches_, linker_patches);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01009175 for (const BakerReadBarrierPatchInfo& info : baker_read_barrier_patches_) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01009176 linker_patches->push_back(linker::LinkerPatch::BakerReadBarrierBranchPatch(
9177 info.label.GetLocation(), info.custom_data));
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01009178 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00009179 DCHECK_EQ(size, linker_patches->size());
Artem Serovc5fcb442016-12-02 19:19:58 +00009180}
9181
9182VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateUint32Literal(
9183 uint32_t value,
9184 Uint32ToLiteralMap* map) {
9185 return map->GetOrCreate(
9186 value,
9187 [this, value]() {
9188 return GetAssembler()->CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ value);
9189 });
9190}
9191
Artem Serov2bbc9532016-10-21 11:51:50 +01009192void LocationsBuilderARMVIXL::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
9193 LocationSummary* locations =
9194 new (GetGraph()->GetArena()) LocationSummary(instr, LocationSummary::kNoCall);
9195 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
9196 Location::RequiresRegister());
9197 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
9198 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
9199 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
9200}
9201
9202void InstructionCodeGeneratorARMVIXL::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
9203 vixl32::Register res = OutputRegister(instr);
9204 vixl32::Register accumulator =
9205 InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
9206 vixl32::Register mul_left =
9207 InputRegisterAt(instr, HMultiplyAccumulate::kInputMulLeftIndex);
9208 vixl32::Register mul_right =
9209 InputRegisterAt(instr, HMultiplyAccumulate::kInputMulRightIndex);
9210
9211 if (instr->GetOpKind() == HInstruction::kAdd) {
9212 __ Mla(res, mul_left, mul_right, accumulator);
9213 } else {
9214 __ Mls(res, mul_left, mul_right, accumulator);
9215 }
9216}
9217
Artem Serov551b28f2016-10-18 19:11:30 +01009218void LocationsBuilderARMVIXL::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
9219 // Nothing to do, this should be removed during prepare for register allocator.
9220 LOG(FATAL) << "Unreachable";
9221}
9222
9223void InstructionCodeGeneratorARMVIXL::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
9224 // Nothing to do, this should be removed during prepare for register allocator.
9225 LOG(FATAL) << "Unreachable";
9226}
9227
9228// Simple implementation of packed switch - generate cascaded compare/jumps.
9229void LocationsBuilderARMVIXL::VisitPackedSwitch(HPackedSwitch* switch_instr) {
9230 LocationSummary* locations =
9231 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
9232 locations->SetInAt(0, Location::RequiresRegister());
9233 if (switch_instr->GetNumEntries() > kPackedSwitchCompareJumpThreshold &&
9234 codegen_->GetAssembler()->GetVIXLAssembler()->IsUsingT32()) {
9235 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the table base.
9236 if (switch_instr->GetStartValue() != 0) {
9237 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the bias.
9238 }
9239 }
9240}
9241
9242// TODO(VIXL): Investigate and reach the parity with old arm codegen.
9243void InstructionCodeGeneratorARMVIXL::VisitPackedSwitch(HPackedSwitch* switch_instr) {
9244 int32_t lower_bound = switch_instr->GetStartValue();
9245 uint32_t num_entries = switch_instr->GetNumEntries();
9246 LocationSummary* locations = switch_instr->GetLocations();
9247 vixl32::Register value_reg = InputRegisterAt(switch_instr, 0);
9248 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
9249
9250 if (num_entries <= kPackedSwitchCompareJumpThreshold ||
9251 !codegen_->GetAssembler()->GetVIXLAssembler()->IsUsingT32()) {
9252 // Create a series of compare/jumps.
Anton Kirilovedb2ac32016-11-30 15:14:10 +00009253 UseScratchRegisterScope temps(GetVIXLAssembler());
Artem Serov551b28f2016-10-18 19:11:30 +01009254 vixl32::Register temp_reg = temps.Acquire();
9255 // Note: It is fine for the below AddConstantSetFlags() using IP register to temporarily store
9256 // the immediate, because IP is used as the destination register. For the other
9257 // AddConstantSetFlags() and GenerateCompareWithImmediate(), the immediate values are constant,
9258 // and they can be encoded in the instruction without making use of IP register.
9259 __ Adds(temp_reg, value_reg, -lower_bound);
9260
9261 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
9262 // Jump to successors[0] if value == lower_bound.
9263 __ B(eq, codegen_->GetLabelOf(successors[0]));
9264 int32_t last_index = 0;
9265 for (; num_entries - last_index > 2; last_index += 2) {
9266 __ Adds(temp_reg, temp_reg, -2);
9267 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
9268 __ B(lo, codegen_->GetLabelOf(successors[last_index + 1]));
9269 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
9270 __ B(eq, codegen_->GetLabelOf(successors[last_index + 2]));
9271 }
9272 if (num_entries - last_index == 2) {
9273 // The last missing case_value.
9274 __ Cmp(temp_reg, 1);
9275 __ B(eq, codegen_->GetLabelOf(successors[last_index + 1]));
9276 }
9277
9278 // And the default for any other value.
9279 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
9280 __ B(codegen_->GetLabelOf(default_block));
9281 }
9282 } else {
9283 // Create a table lookup.
9284 vixl32::Register table_base = RegisterFrom(locations->GetTemp(0));
9285
9286 JumpTableARMVIXL* jump_table = codegen_->CreateJumpTable(switch_instr);
9287
9288 // Remove the bias.
9289 vixl32::Register key_reg;
9290 if (lower_bound != 0) {
9291 key_reg = RegisterFrom(locations->GetTemp(1));
9292 __ Sub(key_reg, value_reg, lower_bound);
9293 } else {
9294 key_reg = value_reg;
9295 }
9296
9297 // Check whether the value is in the table, jump to default block if not.
9298 __ Cmp(key_reg, num_entries - 1);
9299 __ B(hi, codegen_->GetLabelOf(default_block));
9300
Anton Kirilovedb2ac32016-11-30 15:14:10 +00009301 UseScratchRegisterScope temps(GetVIXLAssembler());
Artem Serov551b28f2016-10-18 19:11:30 +01009302 vixl32::Register jump_offset = temps.Acquire();
9303
9304 // Load jump offset from the table.
Scott Wakeling86e9d262017-01-18 15:59:24 +00009305 {
9306 const size_t jump_size = switch_instr->GetNumEntries() * sizeof(int32_t);
9307 ExactAssemblyScope aas(GetVIXLAssembler(),
9308 (vixl32::kMaxInstructionSizeInBytes * 4) + jump_size,
9309 CodeBufferCheckScope::kMaximumSize);
9310 __ adr(table_base, jump_table->GetTableStartLabel());
9311 __ ldr(jump_offset, MemOperand(table_base, key_reg, vixl32::LSL, 2));
Artem Serov551b28f2016-10-18 19:11:30 +01009312
Scott Wakeling86e9d262017-01-18 15:59:24 +00009313 // Jump to target block by branching to table_base(pc related) + offset.
9314 vixl32::Register target_address = table_base;
9315 __ add(target_address, table_base, jump_offset);
9316 __ bx(target_address);
Artem Serov09a940d2016-11-11 16:15:11 +00009317
Scott Wakeling86e9d262017-01-18 15:59:24 +00009318 jump_table->EmitTable(codegen_);
9319 }
Artem Serov551b28f2016-10-18 19:11:30 +01009320 }
9321}
9322
Artem Serov02d37832016-10-25 15:25:33 +01009323// Copy the result of a call into the given target.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009324void CodeGeneratorARMVIXL::MoveFromReturnRegister(Location trg, DataType::Type type) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01009325 if (!trg.IsValid()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009326 DCHECK_EQ(type, DataType::Type::kVoid);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01009327 return;
9328 }
9329
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009330 DCHECK_NE(type, DataType::Type::kVoid);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01009331
Artem Serovd4cc5b22016-11-04 11:19:09 +00009332 Location return_loc = InvokeDexCallingConventionVisitorARMVIXL().GetReturnLocation(type);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01009333 if (return_loc.Equals(trg)) {
9334 return;
9335 }
9336
9337 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
9338 // with the last branch.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009339 if (type == DataType::Type::kInt64) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01009340 TODO_VIXL32(FATAL);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009341 } else if (type == DataType::Type::kFloat64) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01009342 TODO_VIXL32(FATAL);
9343 } else {
9344 // Let the parallel move resolver take care of all of this.
9345 HParallelMove parallel_move(GetGraph()->GetArena());
9346 parallel_move.AddMove(return_loc, trg, type, nullptr);
9347 GetMoveResolver()->EmitNativeCode(&parallel_move);
9348 }
Scott Wakelingfe885462016-09-22 10:24:38 +01009349}
9350
xueliang.zhong8d2c4592016-11-23 17:05:25 +00009351void LocationsBuilderARMVIXL::VisitClassTableGet(HClassTableGet* instruction) {
9352 LocationSummary* locations =
9353 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
9354 locations->SetInAt(0, Location::RequiresRegister());
9355 locations->SetOut(Location::RequiresRegister());
Artem Serov551b28f2016-10-18 19:11:30 +01009356}
9357
xueliang.zhong8d2c4592016-11-23 17:05:25 +00009358void InstructionCodeGeneratorARMVIXL::VisitClassTableGet(HClassTableGet* instruction) {
9359 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
9360 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
9361 instruction->GetIndex(), kArmPointerSize).SizeValue();
9362 GetAssembler()->LoadFromOffset(kLoadWord,
9363 OutputRegister(instruction),
9364 InputRegisterAt(instruction, 0),
9365 method_offset);
9366 } else {
9367 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
9368 instruction->GetIndex(), kArmPointerSize));
9369 GetAssembler()->LoadFromOffset(kLoadWord,
9370 OutputRegister(instruction),
9371 InputRegisterAt(instruction, 0),
9372 mirror::Class::ImtPtrOffset(kArmPointerSize).Uint32Value());
9373 GetAssembler()->LoadFromOffset(kLoadWord,
9374 OutputRegister(instruction),
9375 OutputRegister(instruction),
9376 method_offset);
9377 }
Artem Serov551b28f2016-10-18 19:11:30 +01009378}
9379
Artem Serovc5fcb442016-12-02 19:19:58 +00009380static void PatchJitRootUse(uint8_t* code,
9381 const uint8_t* roots_data,
9382 VIXLUInt32Literal* literal,
9383 uint64_t index_in_table) {
9384 DCHECK(literal->IsBound());
9385 uint32_t literal_offset = literal->GetLocation();
9386 uintptr_t address =
9387 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
9388 uint8_t* data = code + literal_offset;
9389 reinterpret_cast<uint32_t*>(data)[0] = dchecked_integral_cast<uint32_t>(address);
9390}
9391
9392void CodeGeneratorARMVIXL::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
9393 for (const auto& entry : jit_string_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01009394 const StringReference& string_reference = entry.first;
9395 VIXLUInt32Literal* table_entry_literal = entry.second;
9396 const auto it = jit_string_roots_.find(string_reference);
Artem Serovc5fcb442016-12-02 19:19:58 +00009397 DCHECK(it != jit_string_roots_.end());
Vladimir Marko7d157fc2017-05-10 16:29:23 +01009398 uint64_t index_in_table = it->second;
9399 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Artem Serovc5fcb442016-12-02 19:19:58 +00009400 }
9401 for (const auto& entry : jit_class_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01009402 const TypeReference& type_reference = entry.first;
9403 VIXLUInt32Literal* table_entry_literal = entry.second;
9404 const auto it = jit_class_roots_.find(type_reference);
Artem Serovc5fcb442016-12-02 19:19:58 +00009405 DCHECK(it != jit_class_roots_.end());
Vladimir Marko7d157fc2017-05-10 16:29:23 +01009406 uint64_t index_in_table = it->second;
9407 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Artem Serovc5fcb442016-12-02 19:19:58 +00009408 }
9409}
9410
Artem Serovd4cc5b22016-11-04 11:19:09 +00009411void CodeGeneratorARMVIXL::EmitMovwMovtPlaceholder(
9412 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels,
9413 vixl32::Register out) {
Artem Serov0fb37192016-12-06 18:13:40 +00009414 ExactAssemblyScope aas(GetVIXLAssembler(),
9415 3 * vixl32::kMaxInstructionSizeInBytes,
9416 CodeBufferCheckScope::kMaximumSize);
Artem Serovd4cc5b22016-11-04 11:19:09 +00009417 // TODO(VIXL): Think about using mov instead of movw.
9418 __ bind(&labels->movw_label);
9419 __ movw(out, /* placeholder */ 0u);
9420 __ bind(&labels->movt_label);
9421 __ movt(out, /* placeholder */ 0u);
9422 __ bind(&labels->add_pc_label);
9423 __ add(out, out, pc);
9424}
9425
Scott Wakelingfe885462016-09-22 10:24:38 +01009426#undef __
9427#undef QUICK_ENTRY_POINT
9428#undef TODO_VIXL32
9429
9430} // namespace arm
9431} // namespace art