blob: 504c6479cc1b9a2e760b364c6caeb582708ceb0d [file] [log] [blame]
Scott Wakelingfe885462016-09-22 10:24:38 +01001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_arm_vixl.h"
18
Vladimir Markoeee1c0e2017-04-21 17:58:41 +010019#include "arch/arm/asm_support_arm.h"
Scott Wakelingfe885462016-09-22 10:24:38 +010020#include "arch/arm/instruction_set_features_arm.h"
21#include "art_method.h"
Andreas Gampe5678db52017-06-08 14:11:18 -070022#include "base/bit_utils.h"
23#include "base/bit_utils_iterator.h"
Vladimir Marko94ec2db2017-09-06 17:21:03 +010024#include "class_table.h"
Scott Wakelingfe885462016-09-22 10:24:38 +010025#include "code_generator_utils.h"
26#include "common_arm.h"
27#include "compiled_method.h"
28#include "entrypoints/quick/quick_entrypoints.h"
29#include "gc/accounting/card_table.h"
Andreas Gampe09659c22017-09-18 18:23:32 -070030#include "heap_poisoning.h"
Anton Kirilov5ec62182016-10-13 20:16:02 +010031#include "intrinsics_arm_vixl.h"
Vladimir Markoeee1c0e2017-04-21 17:58:41 +010032#include "linker/arm/relative_patcher_thumb2.h"
Vladimir Markod8dbc8d2017-09-20 13:37:47 +010033#include "linker/linker_patch.h"
Scott Wakelingfe885462016-09-22 10:24:38 +010034#include "mirror/array-inl.h"
35#include "mirror/class-inl.h"
36#include "thread.h"
37#include "utils/arm/assembler_arm_vixl.h"
38#include "utils/arm/managed_register_arm.h"
39#include "utils/assembler.h"
40#include "utils/stack_checks.h"
41
42namespace art {
43namespace arm {
44
45namespace vixl32 = vixl::aarch32;
46using namespace vixl32; // NOLINT(build/namespaces)
47
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +010048using helpers::DRegisterFrom;
Scott Wakelingfe885462016-09-22 10:24:38 +010049using helpers::DWARFReg;
Scott Wakelinga7812ae2016-10-17 10:03:36 +010050using helpers::HighDRegisterFrom;
51using helpers::HighRegisterFrom;
Donghui Bai426b49c2016-11-08 14:55:38 +080052using helpers::InputDRegisterAt;
Scott Wakelingfe885462016-09-22 10:24:38 +010053using helpers::InputOperandAt;
Scott Wakelingc34dba72016-10-03 10:14:44 +010054using helpers::InputRegister;
Scott Wakelinga7812ae2016-10-17 10:03:36 +010055using helpers::InputRegisterAt;
Scott Wakelingfe885462016-09-22 10:24:38 +010056using helpers::InputSRegisterAt;
Anton Kirilov644032c2016-12-06 17:51:43 +000057using helpers::InputVRegister;
Scott Wakelinga7812ae2016-10-17 10:03:36 +010058using helpers::InputVRegisterAt;
Scott Wakelingb77051e2016-11-21 19:46:00 +000059using helpers::Int32ConstantFrom;
Anton Kirilov644032c2016-12-06 17:51:43 +000060using helpers::Int64ConstantFrom;
Scott Wakelinga7812ae2016-10-17 10:03:36 +010061using helpers::LocationFrom;
62using helpers::LowRegisterFrom;
63using helpers::LowSRegisterFrom;
Donghui Bai426b49c2016-11-08 14:55:38 +080064using helpers::OperandFrom;
Scott Wakelinga7812ae2016-10-17 10:03:36 +010065using helpers::OutputRegister;
66using helpers::OutputSRegister;
67using helpers::OutputVRegister;
68using helpers::RegisterFrom;
69using helpers::SRegisterFrom;
Anton Kirilov644032c2016-12-06 17:51:43 +000070using helpers::Uint64ConstantFrom;
Scott Wakelingfe885462016-09-22 10:24:38 +010071
Artem Serov0fb37192016-12-06 18:13:40 +000072using vixl::ExactAssemblyScope;
73using vixl::CodeBufferCheckScope;
74
Scott Wakelingfe885462016-09-22 10:24:38 +010075using RegisterList = vixl32::RegisterList;
76
77static bool ExpectedPairLayout(Location location) {
78 // We expected this for both core and fpu register pairs.
79 return ((location.low() & 1) == 0) && (location.low() + 1 == location.high());
80}
Artem Serovd4cc5b22016-11-04 11:19:09 +000081// Use a local definition to prevent copying mistakes.
82static constexpr size_t kArmWordSize = static_cast<size_t>(kArmPointerSize);
83static constexpr size_t kArmBitsPerWord = kArmWordSize * kBitsPerByte;
Artem Serov551b28f2016-10-18 19:11:30 +010084static constexpr uint32_t kPackedSwitchCompareJumpThreshold = 7;
Scott Wakelingfe885462016-09-22 10:24:38 +010085
Vladimir Markoeee1c0e2017-04-21 17:58:41 +010086// Reference load (except object array loads) is using LDR Rt, [Rn, #offset] which can handle
87// offset < 4KiB. For offsets >= 4KiB, the load shall be emitted as two or more instructions.
88// For the Baker read barrier implementation using link-generated thunks we need to split
89// the offset explicitly.
90constexpr uint32_t kReferenceLoadMinFarOffset = 4 * KB;
91
92// Flags controlling the use of link-time generated thunks for Baker read barriers.
93constexpr bool kBakerReadBarrierLinkTimeThunksEnableForFields = true;
94constexpr bool kBakerReadBarrierLinkTimeThunksEnableForArrays = true;
95constexpr bool kBakerReadBarrierLinkTimeThunksEnableForGcRoots = true;
96
97// The reserved entrypoint register for link-time generated thunks.
98const vixl32::Register kBakerCcEntrypointRegister = r4;
99
Roland Levillain5daa4952017-07-03 17:23:56 +0100100// Using a base helps identify when we hit Marking Register check breakpoints.
101constexpr int kMarkingRegisterCheckBreakCodeBaseCode = 0x10;
102
Scott Wakelingfe885462016-09-22 10:24:38 +0100103#ifdef __
104#error "ARM Codegen VIXL macro-assembler macro already defined."
105#endif
106
Scott Wakelingfe885462016-09-22 10:24:38 +0100107// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
108#define __ down_cast<CodeGeneratorARMVIXL*>(codegen)->GetVIXLAssembler()-> // NOLINT
109#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kArmPointerSize, x).Int32Value()
110
111// Marker that code is yet to be, and must, be implemented.
112#define TODO_VIXL32(level) LOG(level) << __PRETTY_FUNCTION__ << " unimplemented "
113
Vladimir Markoeee1c0e2017-04-21 17:58:41 +0100114static inline void ExcludeIPAndBakerCcEntrypointRegister(UseScratchRegisterScope* temps,
115 HInstruction* instruction) {
116 DCHECK(temps->IsAvailable(ip));
117 temps->Exclude(ip);
118 DCHECK(!temps->IsAvailable(kBakerCcEntrypointRegister));
119 DCHECK_EQ(kBakerCcEntrypointRegister.GetCode(),
120 linker::Thumb2RelativePatcher::kBakerCcEntrypointRegister);
121 DCHECK_NE(instruction->GetLocations()->GetTempCount(), 0u);
122 DCHECK(RegisterFrom(instruction->GetLocations()->GetTemp(
123 instruction->GetLocations()->GetTempCount() - 1u)).Is(kBakerCcEntrypointRegister));
124}
125
126static inline void EmitPlaceholderBne(CodeGeneratorARMVIXL* codegen, vixl32::Label* patch_label) {
127 ExactAssemblyScope eas(codegen->GetVIXLAssembler(), kMaxInstructionSizeInBytes);
128 __ bind(patch_label);
129 vixl32::Label placeholder_label;
130 __ b(ne, EncodingSize(Wide), &placeholder_label); // Placeholder, patched at link-time.
131 __ bind(&placeholder_label);
132}
133
Vladimir Marko88abba22017-05-03 17:09:25 +0100134static inline bool CanEmitNarrowLdr(vixl32::Register rt, vixl32::Register rn, uint32_t offset) {
135 return rt.IsLow() && rn.IsLow() && offset < 32u;
136}
137
Vladimir Markoeee1c0e2017-04-21 17:58:41 +0100138class EmitAdrCode {
139 public:
140 EmitAdrCode(ArmVIXLMacroAssembler* assembler, vixl32::Register rd, vixl32::Label* label)
141 : assembler_(assembler), rd_(rd), label_(label) {
142 ExactAssemblyScope aas(assembler, kMaxInstructionSizeInBytes);
143 adr_location_ = assembler->GetCursorOffset();
144 assembler->adr(EncodingSize(Wide), rd, label);
145 }
146
147 ~EmitAdrCode() {
148 DCHECK(label_->IsBound());
149 // The ADR emitted by the assembler does not set the Thumb mode bit we need.
150 // TODO: Maybe extend VIXL to allow ADR for return address?
151 uint8_t* raw_adr = assembler_->GetBuffer()->GetOffsetAddress<uint8_t*>(adr_location_);
152 // Expecting ADR encoding T3 with `(offset & 1) == 0`.
153 DCHECK_EQ(raw_adr[1] & 0xfbu, 0xf2u); // Check bits 24-31, except 26.
154 DCHECK_EQ(raw_adr[0] & 0xffu, 0x0fu); // Check bits 16-23.
155 DCHECK_EQ(raw_adr[3] & 0x8fu, rd_.GetCode()); // Check bits 8-11 and 15.
156 DCHECK_EQ(raw_adr[2] & 0x01u, 0x00u); // Check bit 0, i.e. the `offset & 1`.
157 // Add the Thumb mode bit.
158 raw_adr[2] |= 0x01u;
159 }
160
161 private:
162 ArmVIXLMacroAssembler* const assembler_;
163 vixl32::Register rd_;
164 vixl32::Label* const label_;
165 int32_t adr_location_;
166};
167
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100168// SaveLiveRegisters and RestoreLiveRegisters from SlowPathCodeARM operate on sets of S registers,
169// for each live D registers they treat two corresponding S registers as live ones.
170//
171// Two following functions (SaveContiguousSRegisterList, RestoreContiguousSRegisterList) build
172// from a list of contiguous S registers a list of contiguous D registers (processing first/last
173// S registers corner cases) and save/restore this new list treating them as D registers.
174// - decreasing code size
175// - avoiding hazards on Cortex-A57, when a pair of S registers for an actual live D register is
176// restored and then used in regular non SlowPath code as D register.
177//
178// For the following example (v means the S register is live):
179// D names: | D0 | D1 | D2 | D4 | ...
180// S names: | S0 | S1 | S2 | S3 | S4 | S5 | S6 | S7 | ...
181// Live? | | v | v | v | v | v | v | | ...
182//
183// S1 and S6 will be saved/restored independently; D registers list (D1, D2) will be processed
184// as D registers.
185//
186// TODO(VIXL): All this code should be unnecessary once the VIXL AArch32 backend provides helpers
187// for lists of floating-point registers.
188static size_t SaveContiguousSRegisterList(size_t first,
189 size_t last,
190 CodeGenerator* codegen,
191 size_t stack_offset) {
192 static_assert(kSRegSizeInBytes == kArmWordSize, "Broken assumption on reg/word sizes.");
193 static_assert(kDRegSizeInBytes == 2 * kArmWordSize, "Broken assumption on reg/word sizes.");
194 DCHECK_LE(first, last);
195 if ((first == last) && (first == 0)) {
196 __ Vstr(vixl32::SRegister(first), MemOperand(sp, stack_offset));
197 return stack_offset + kSRegSizeInBytes;
198 }
199 if (first % 2 == 1) {
200 __ Vstr(vixl32::SRegister(first++), MemOperand(sp, stack_offset));
201 stack_offset += kSRegSizeInBytes;
202 }
203
204 bool save_last = false;
205 if (last % 2 == 0) {
206 save_last = true;
207 --last;
208 }
209
210 if (first < last) {
211 vixl32::DRegister d_reg = vixl32::DRegister(first / 2);
212 DCHECK_EQ((last - first + 1) % 2, 0u);
213 size_t number_of_d_regs = (last - first + 1) / 2;
214
215 if (number_of_d_regs == 1) {
216 __ Vstr(d_reg, MemOperand(sp, stack_offset));
217 } else if (number_of_d_regs > 1) {
218 UseScratchRegisterScope temps(down_cast<CodeGeneratorARMVIXL*>(codegen)->GetVIXLAssembler());
219 vixl32::Register base = sp;
220 if (stack_offset != 0) {
221 base = temps.Acquire();
Scott Wakelingb77051e2016-11-21 19:46:00 +0000222 __ Add(base, sp, Operand::From(stack_offset));
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100223 }
224 __ Vstm(F64, base, NO_WRITE_BACK, DRegisterList(d_reg, number_of_d_regs));
225 }
226 stack_offset += number_of_d_regs * kDRegSizeInBytes;
227 }
228
229 if (save_last) {
230 __ Vstr(vixl32::SRegister(last + 1), MemOperand(sp, stack_offset));
231 stack_offset += kSRegSizeInBytes;
232 }
233
234 return stack_offset;
235}
236
237static size_t RestoreContiguousSRegisterList(size_t first,
238 size_t last,
239 CodeGenerator* codegen,
240 size_t stack_offset) {
241 static_assert(kSRegSizeInBytes == kArmWordSize, "Broken assumption on reg/word sizes.");
242 static_assert(kDRegSizeInBytes == 2 * kArmWordSize, "Broken assumption on reg/word sizes.");
243 DCHECK_LE(first, last);
244 if ((first == last) && (first == 0)) {
245 __ Vldr(vixl32::SRegister(first), MemOperand(sp, stack_offset));
246 return stack_offset + kSRegSizeInBytes;
247 }
248 if (first % 2 == 1) {
249 __ Vldr(vixl32::SRegister(first++), MemOperand(sp, stack_offset));
250 stack_offset += kSRegSizeInBytes;
251 }
252
253 bool restore_last = false;
254 if (last % 2 == 0) {
255 restore_last = true;
256 --last;
257 }
258
259 if (first < last) {
260 vixl32::DRegister d_reg = vixl32::DRegister(first / 2);
261 DCHECK_EQ((last - first + 1) % 2, 0u);
262 size_t number_of_d_regs = (last - first + 1) / 2;
263 if (number_of_d_regs == 1) {
264 __ Vldr(d_reg, MemOperand(sp, stack_offset));
265 } else if (number_of_d_regs > 1) {
266 UseScratchRegisterScope temps(down_cast<CodeGeneratorARMVIXL*>(codegen)->GetVIXLAssembler());
267 vixl32::Register base = sp;
268 if (stack_offset != 0) {
269 base = temps.Acquire();
Scott Wakelingb77051e2016-11-21 19:46:00 +0000270 __ Add(base, sp, Operand::From(stack_offset));
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100271 }
272 __ Vldm(F64, base, NO_WRITE_BACK, DRegisterList(d_reg, number_of_d_regs));
273 }
274 stack_offset += number_of_d_regs * kDRegSizeInBytes;
275 }
276
277 if (restore_last) {
278 __ Vldr(vixl32::SRegister(last + 1), MemOperand(sp, stack_offset));
279 stack_offset += kSRegSizeInBytes;
280 }
281
282 return stack_offset;
283}
284
Vladimir Markod5d2f2c2017-09-26 12:37:26 +0100285static LoadOperandType GetLoadOperandType(DataType::Type type) {
286 switch (type) {
287 case DataType::Type::kReference:
288 return kLoadWord;
289 case DataType::Type::kBool:
290 case DataType::Type::kUint8:
291 return kLoadUnsignedByte;
292 case DataType::Type::kInt8:
293 return kLoadSignedByte;
294 case DataType::Type::kUint16:
295 return kLoadUnsignedHalfword;
296 case DataType::Type::kInt16:
297 return kLoadSignedHalfword;
298 case DataType::Type::kInt32:
299 return kLoadWord;
300 case DataType::Type::kInt64:
301 return kLoadWordPair;
302 case DataType::Type::kFloat32:
303 return kLoadSWord;
304 case DataType::Type::kFloat64:
305 return kLoadDWord;
306 default:
307 LOG(FATAL) << "Unreachable type " << type;
308 UNREACHABLE();
309 }
310}
311
312static StoreOperandType GetStoreOperandType(DataType::Type type) {
313 switch (type) {
314 case DataType::Type::kReference:
315 return kStoreWord;
316 case DataType::Type::kBool:
317 case DataType::Type::kUint8:
318 case DataType::Type::kInt8:
319 return kStoreByte;
320 case DataType::Type::kUint16:
321 case DataType::Type::kInt16:
322 return kStoreHalfword;
323 case DataType::Type::kInt32:
324 return kStoreWord;
325 case DataType::Type::kInt64:
326 return kStoreWordPair;
327 case DataType::Type::kFloat32:
328 return kStoreSWord;
329 case DataType::Type::kFloat64:
330 return kStoreDWord;
331 default:
332 LOG(FATAL) << "Unreachable type " << type;
333 UNREACHABLE();
334 }
335}
336
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100337void SlowPathCodeARMVIXL::SaveLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
338 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
339 size_t orig_offset = stack_offset;
340
341 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ true);
342 for (uint32_t i : LowToHighBits(core_spills)) {
343 // If the register holds an object, update the stack mask.
344 if (locations->RegisterContainsObject(i)) {
345 locations->SetStackBit(stack_offset / kVRegSize);
346 }
347 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
348 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
349 saved_core_stack_offsets_[i] = stack_offset;
350 stack_offset += kArmWordSize;
351 }
352
353 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
354 arm_codegen->GetAssembler()->StoreRegisterList(core_spills, orig_offset);
355
356 uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ false);
357 orig_offset = stack_offset;
358 for (uint32_t i : LowToHighBits(fp_spills)) {
359 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
360 saved_fpu_stack_offsets_[i] = stack_offset;
361 stack_offset += kArmWordSize;
362 }
363
364 stack_offset = orig_offset;
365 while (fp_spills != 0u) {
366 uint32_t begin = CTZ(fp_spills);
367 uint32_t tmp = fp_spills + (1u << begin);
368 fp_spills &= tmp; // Clear the contiguous range of 1s.
369 uint32_t end = (tmp == 0u) ? 32u : CTZ(tmp); // CTZ(0) is undefined.
370 stack_offset = SaveContiguousSRegisterList(begin, end - 1, codegen, stack_offset);
371 }
372 DCHECK_LE(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
373}
374
375void SlowPathCodeARMVIXL::RestoreLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
376 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
377 size_t orig_offset = stack_offset;
378
379 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ true);
380 for (uint32_t i : LowToHighBits(core_spills)) {
381 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
382 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
383 stack_offset += kArmWordSize;
384 }
385
386 // TODO(VIXL): Check the coherency of stack_offset after this with a test.
387 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
388 arm_codegen->GetAssembler()->LoadRegisterList(core_spills, orig_offset);
389
390 uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ false);
391 while (fp_spills != 0u) {
392 uint32_t begin = CTZ(fp_spills);
393 uint32_t tmp = fp_spills + (1u << begin);
394 fp_spills &= tmp; // Clear the contiguous range of 1s.
395 uint32_t end = (tmp == 0u) ? 32u : CTZ(tmp); // CTZ(0) is undefined.
396 stack_offset = RestoreContiguousSRegisterList(begin, end - 1, codegen, stack_offset);
397 }
398 DCHECK_LE(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
399}
400
401class NullCheckSlowPathARMVIXL : public SlowPathCodeARMVIXL {
402 public:
403 explicit NullCheckSlowPathARMVIXL(HNullCheck* instruction) : SlowPathCodeARMVIXL(instruction) {}
404
405 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
406 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
407 __ Bind(GetEntryLabel());
408 if (instruction_->CanThrowIntoCatchBlock()) {
409 // Live registers will be restored in the catch block if caught.
410 SaveLiveRegisters(codegen, instruction_->GetLocations());
411 }
412 arm_codegen->InvokeRuntime(kQuickThrowNullPointer,
413 instruction_,
414 instruction_->GetDexPc(),
415 this);
416 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
417 }
418
419 bool IsFatal() const OVERRIDE { return true; }
420
421 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathARMVIXL"; }
422
423 private:
424 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARMVIXL);
425};
426
Scott Wakelingfe885462016-09-22 10:24:38 +0100427class DivZeroCheckSlowPathARMVIXL : public SlowPathCodeARMVIXL {
428 public:
429 explicit DivZeroCheckSlowPathARMVIXL(HDivZeroCheck* instruction)
430 : SlowPathCodeARMVIXL(instruction) {}
431
432 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100433 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
Scott Wakelingfe885462016-09-22 10:24:38 +0100434 __ Bind(GetEntryLabel());
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100435 arm_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Scott Wakelingfe885462016-09-22 10:24:38 +0100436 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
437 }
438
439 bool IsFatal() const OVERRIDE { return true; }
440
441 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathARMVIXL"; }
442
443 private:
444 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARMVIXL);
445};
446
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100447class SuspendCheckSlowPathARMVIXL : public SlowPathCodeARMVIXL {
448 public:
449 SuspendCheckSlowPathARMVIXL(HSuspendCheck* instruction, HBasicBlock* successor)
450 : SlowPathCodeARMVIXL(instruction), successor_(successor) {}
451
452 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
453 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
454 __ Bind(GetEntryLabel());
455 arm_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
456 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
457 if (successor_ == nullptr) {
458 __ B(GetReturnLabel());
459 } else {
460 __ B(arm_codegen->GetLabelOf(successor_));
461 }
462 }
463
464 vixl32::Label* GetReturnLabel() {
465 DCHECK(successor_ == nullptr);
466 return &return_label_;
467 }
468
469 HBasicBlock* GetSuccessor() const {
470 return successor_;
471 }
472
473 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathARMVIXL"; }
474
475 private:
476 // If not null, the block to branch to after the suspend check.
477 HBasicBlock* const successor_;
478
479 // If `successor_` is null, the label to branch to after the suspend check.
480 vixl32::Label return_label_;
481
482 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARMVIXL);
483};
484
Scott Wakelingc34dba72016-10-03 10:14:44 +0100485class BoundsCheckSlowPathARMVIXL : public SlowPathCodeARMVIXL {
486 public:
487 explicit BoundsCheckSlowPathARMVIXL(HBoundsCheck* instruction)
488 : SlowPathCodeARMVIXL(instruction) {}
489
490 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
491 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
492 LocationSummary* locations = instruction_->GetLocations();
493
494 __ Bind(GetEntryLabel());
495 if (instruction_->CanThrowIntoCatchBlock()) {
496 // Live registers will be restored in the catch block if caught.
497 SaveLiveRegisters(codegen, instruction_->GetLocations());
498 }
499 // We're moving two locations to locations that could overlap, so we need a parallel
500 // move resolver.
501 InvokeRuntimeCallingConventionARMVIXL calling_convention;
502 codegen->EmitParallelMoves(
503 locations->InAt(0),
504 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100505 DataType::Type::kInt32,
Scott Wakelingc34dba72016-10-03 10:14:44 +0100506 locations->InAt(1),
507 LocationFrom(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100508 DataType::Type::kInt32);
Scott Wakelingc34dba72016-10-03 10:14:44 +0100509 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
510 ? kQuickThrowStringBounds
511 : kQuickThrowArrayBounds;
512 arm_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
513 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
514 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
515 }
516
517 bool IsFatal() const OVERRIDE { return true; }
518
519 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathARMVIXL"; }
520
521 private:
522 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARMVIXL);
523};
524
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100525class LoadClassSlowPathARMVIXL : public SlowPathCodeARMVIXL {
526 public:
527 LoadClassSlowPathARMVIXL(HLoadClass* cls, HInstruction* at, uint32_t dex_pc, bool do_clinit)
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000528 : SlowPathCodeARMVIXL(at), cls_(cls), dex_pc_(dex_pc), do_clinit_(do_clinit) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100529 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
530 }
531
532 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000533 LocationSummary* locations = instruction_->GetLocations();
Vladimir Markoea4c1262017-02-06 19:59:33 +0000534 Location out = locations->Out();
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100535
536 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
537 __ Bind(GetEntryLabel());
538 SaveLiveRegisters(codegen, locations);
539
540 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000541 dex::TypeIndex type_index = cls_->GetTypeIndex();
542 __ Mov(calling_convention.GetRegisterAt(0), type_index.index_);
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100543 QuickEntrypointEnum entrypoint = do_clinit_ ? kQuickInitializeStaticStorage
544 : kQuickInitializeType;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000545 arm_codegen->InvokeRuntime(entrypoint, instruction_, dex_pc_, this);
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100546 if (do_clinit_) {
547 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
548 } else {
549 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
550 }
551
552 // Move the class to the desired location.
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100553 if (out.IsValid()) {
554 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
555 arm_codegen->Move32(locations->Out(), LocationFrom(r0));
556 }
557 RestoreLiveRegisters(codegen, locations);
558 __ B(GetExitLabel());
559 }
560
561 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathARMVIXL"; }
562
563 private:
564 // The class this slow path will load.
565 HLoadClass* const cls_;
566
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100567 // The dex PC of `at_`.
568 const uint32_t dex_pc_;
569
570 // Whether to initialize the class.
571 const bool do_clinit_;
572
573 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARMVIXL);
574};
575
Artem Serovd4cc5b22016-11-04 11:19:09 +0000576class LoadStringSlowPathARMVIXL : public SlowPathCodeARMVIXL {
577 public:
578 explicit LoadStringSlowPathARMVIXL(HLoadString* instruction)
579 : SlowPathCodeARMVIXL(instruction) {}
580
581 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Vladimir Markoea4c1262017-02-06 19:59:33 +0000582 DCHECK(instruction_->IsLoadString());
583 DCHECK_EQ(instruction_->AsLoadString()->GetLoadKind(), HLoadString::LoadKind::kBssEntry);
Artem Serovd4cc5b22016-11-04 11:19:09 +0000584 LocationSummary* locations = instruction_->GetLocations();
585 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Vladimir Markof3c52b42017-11-17 17:32:12 +0000586 const dex::StringIndex string_index = instruction_->AsLoadString()->GetStringIndex();
Artem Serovd4cc5b22016-11-04 11:19:09 +0000587
588 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
589 __ Bind(GetEntryLabel());
590 SaveLiveRegisters(codegen, locations);
591
592 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000593 __ Mov(calling_convention.GetRegisterAt(0), string_index.index_);
Artem Serovd4cc5b22016-11-04 11:19:09 +0000594 arm_codegen->InvokeRuntime(kQuickResolveString, instruction_, instruction_->GetDexPc(), this);
595 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
596
Artem Serovd4cc5b22016-11-04 11:19:09 +0000597 arm_codegen->Move32(locations->Out(), LocationFrom(r0));
598 RestoreLiveRegisters(codegen, locations);
599
600 __ B(GetExitLabel());
601 }
602
603 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathARMVIXL"; }
604
605 private:
606 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARMVIXL);
607};
608
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100609class TypeCheckSlowPathARMVIXL : public SlowPathCodeARMVIXL {
610 public:
611 TypeCheckSlowPathARMVIXL(HInstruction* instruction, bool is_fatal)
612 : SlowPathCodeARMVIXL(instruction), is_fatal_(is_fatal) {}
613
614 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
615 LocationSummary* locations = instruction_->GetLocations();
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100616 DCHECK(instruction_->IsCheckCast()
617 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
618
619 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
620 __ Bind(GetEntryLabel());
621
Vladimir Marko87584542017-12-12 17:47:52 +0000622 if (!is_fatal_ || instruction_->CanThrowIntoCatchBlock()) {
Artem Serovcfbe9132016-10-14 15:58:56 +0100623 SaveLiveRegisters(codegen, locations);
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100624 }
625
626 // We're moving two locations to locations that could overlap, so we need a parallel
627 // move resolver.
628 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100629
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800630 codegen->EmitParallelMoves(locations->InAt(0),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800631 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100632 DataType::Type::kReference,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800633 locations->InAt(1),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800634 LocationFrom(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100635 DataType::Type::kReference);
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100636 if (instruction_->IsInstanceOf()) {
Artem Serovcfbe9132016-10-14 15:58:56 +0100637 arm_codegen->InvokeRuntime(kQuickInstanceofNonTrivial,
638 instruction_,
639 instruction_->GetDexPc(),
640 this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800641 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Artem Serovcfbe9132016-10-14 15:58:56 +0100642 arm_codegen->Move32(locations->Out(), LocationFrom(r0));
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100643 } else {
644 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800645 arm_codegen->InvokeRuntime(kQuickCheckInstanceOf,
646 instruction_,
647 instruction_->GetDexPc(),
648 this);
649 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100650 }
651
652 if (!is_fatal_) {
Artem Serovcfbe9132016-10-14 15:58:56 +0100653 RestoreLiveRegisters(codegen, locations);
654 __ B(GetExitLabel());
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100655 }
656 }
657
658 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathARMVIXL"; }
659
660 bool IsFatal() const OVERRIDE { return is_fatal_; }
661
662 private:
663 const bool is_fatal_;
664
665 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARMVIXL);
666};
667
Scott Wakelingc34dba72016-10-03 10:14:44 +0100668class DeoptimizationSlowPathARMVIXL : public SlowPathCodeARMVIXL {
669 public:
670 explicit DeoptimizationSlowPathARMVIXL(HDeoptimize* instruction)
671 : SlowPathCodeARMVIXL(instruction) {}
672
673 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
674 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
675 __ Bind(GetEntryLabel());
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100676 LocationSummary* locations = instruction_->GetLocations();
677 SaveLiveRegisters(codegen, locations);
678 InvokeRuntimeCallingConventionARMVIXL calling_convention;
679 __ Mov(calling_convention.GetRegisterAt(0),
680 static_cast<uint32_t>(instruction_->AsDeoptimize()->GetDeoptimizationKind()));
681
Scott Wakelingc34dba72016-10-03 10:14:44 +0100682 arm_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100683 CheckEntrypointTypes<kQuickDeoptimize, void, DeoptimizationKind>();
Scott Wakelingc34dba72016-10-03 10:14:44 +0100684 }
685
686 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathARMVIXL"; }
687
688 private:
689 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARMVIXL);
690};
691
692class ArraySetSlowPathARMVIXL : public SlowPathCodeARMVIXL {
693 public:
694 explicit ArraySetSlowPathARMVIXL(HInstruction* instruction) : SlowPathCodeARMVIXL(instruction) {}
695
696 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
697 LocationSummary* locations = instruction_->GetLocations();
698 __ Bind(GetEntryLabel());
699 SaveLiveRegisters(codegen, locations);
700
701 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100702 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Scott Wakelingc34dba72016-10-03 10:14:44 +0100703 parallel_move.AddMove(
704 locations->InAt(0),
705 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100706 DataType::Type::kReference,
Scott Wakelingc34dba72016-10-03 10:14:44 +0100707 nullptr);
708 parallel_move.AddMove(
709 locations->InAt(1),
710 LocationFrom(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100711 DataType::Type::kInt32,
Scott Wakelingc34dba72016-10-03 10:14:44 +0100712 nullptr);
713 parallel_move.AddMove(
714 locations->InAt(2),
715 LocationFrom(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100716 DataType::Type::kReference,
Scott Wakelingc34dba72016-10-03 10:14:44 +0100717 nullptr);
718 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
719
720 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
721 arm_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
722 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
723 RestoreLiveRegisters(codegen, locations);
724 __ B(GetExitLabel());
725 }
726
727 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathARMVIXL"; }
728
729 private:
730 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathARMVIXL);
731};
732
Roland Levillain54f869e2017-03-06 13:54:11 +0000733// Abstract base class for read barrier slow paths marking a reference
734// `ref`.
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000735//
Roland Levillain54f869e2017-03-06 13:54:11 +0000736// Argument `entrypoint` must be a register location holding the read
Roland Levillain6d729a72017-06-30 18:34:01 +0100737// barrier marking runtime entry point to be invoked or an empty
738// location; in the latter case, the read barrier marking runtime
739// entry point will be loaded by the slow path code itself.
Roland Levillain54f869e2017-03-06 13:54:11 +0000740class ReadBarrierMarkSlowPathBaseARMVIXL : public SlowPathCodeARMVIXL {
741 protected:
742 ReadBarrierMarkSlowPathBaseARMVIXL(HInstruction* instruction, Location ref, Location entrypoint)
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000743 : SlowPathCodeARMVIXL(instruction), ref_(ref), entrypoint_(entrypoint) {
744 DCHECK(kEmitCompilerReadBarrier);
745 }
746
Roland Levillain54f869e2017-03-06 13:54:11 +0000747 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathBaseARMVIXL"; }
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000748
Roland Levillain54f869e2017-03-06 13:54:11 +0000749 // Generate assembly code calling the read barrier marking runtime
750 // entry point (ReadBarrierMarkRegX).
751 void GenerateReadBarrierMarkRuntimeCall(CodeGenerator* codegen) {
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000752 vixl32::Register ref_reg = RegisterFrom(ref_);
Roland Levillain47b3ab22017-02-27 14:31:35 +0000753
Roland Levillain47b3ab22017-02-27 14:31:35 +0000754 // No need to save live registers; it's taken care of by the
755 // entrypoint. Also, there is no need to update the stack mask,
756 // as this runtime call will not trigger a garbage collection.
757 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
758 DCHECK(!ref_reg.Is(sp));
759 DCHECK(!ref_reg.Is(lr));
760 DCHECK(!ref_reg.Is(pc));
761 // IP is used internally by the ReadBarrierMarkRegX entry point
762 // as a temporary, it cannot be the entry point's input/output.
763 DCHECK(!ref_reg.Is(ip));
764 DCHECK(ref_reg.IsRegister()) << ref_reg;
765 // "Compact" slow path, saving two moves.
766 //
767 // Instead of using the standard runtime calling convention (input
768 // and output in R0):
769 //
770 // R0 <- ref
771 // R0 <- ReadBarrierMark(R0)
772 // ref <- R0
773 //
774 // we just use rX (the register containing `ref`) as input and output
775 // of a dedicated entrypoint:
776 //
777 // rX <- ReadBarrierMarkRegX(rX)
778 //
779 if (entrypoint_.IsValid()) {
780 arm_codegen->ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction_, this);
781 __ Blx(RegisterFrom(entrypoint_));
782 } else {
Roland Levillain54f869e2017-03-06 13:54:11 +0000783 // Entrypoint is not already loaded, load from the thread.
Roland Levillain47b3ab22017-02-27 14:31:35 +0000784 int32_t entry_point_offset =
Roland Levillain97c46462017-05-11 14:04:03 +0100785 Thread::ReadBarrierMarkEntryPointsOffset<kArmPointerSize>(ref_reg.GetCode());
Roland Levillain47b3ab22017-02-27 14:31:35 +0000786 // This runtime call does not require a stack map.
787 arm_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
788 }
Roland Levillain47b3ab22017-02-27 14:31:35 +0000789 }
790
Roland Levillain47b3ab22017-02-27 14:31:35 +0000791 // The location (register) of the marked object reference.
792 const Location ref_;
793
794 // The location of the entrypoint if already loaded.
795 const Location entrypoint_;
796
Roland Levillain54f869e2017-03-06 13:54:11 +0000797 private:
798 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathBaseARMVIXL);
Roland Levillain47b3ab22017-02-27 14:31:35 +0000799};
800
Scott Wakelingc34dba72016-10-03 10:14:44 +0100801// Slow path marking an object reference `ref` during a read
802// barrier. The field `obj.field` in the object `obj` holding this
Roland Levillain54f869e2017-03-06 13:54:11 +0000803// reference does not get updated by this slow path after marking.
Roland Levillain47b3ab22017-02-27 14:31:35 +0000804//
Scott Wakelingc34dba72016-10-03 10:14:44 +0100805// This means that after the execution of this slow path, `ref` will
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000806// always be up-to-date, but `obj.field` may not; i.e., after the
807// flip, `ref` will be a to-space reference, but `obj.field` will
808// probably still be a from-space reference (unless it gets updated by
809// another thread, or if another thread installed another object
810// reference (different from `ref`) in `obj.field`).
Roland Levillainba650a42017-03-06 13:52:32 +0000811//
Roland Levillain6d729a72017-06-30 18:34:01 +0100812// Argument `entrypoint` must be a register location holding the read
813// barrier marking runtime entry point to be invoked or an empty
814// location; in the latter case, the read barrier marking runtime
815// entry point will be loaded by the slow path code itself.
Roland Levillain54f869e2017-03-06 13:54:11 +0000816class ReadBarrierMarkSlowPathARMVIXL : public ReadBarrierMarkSlowPathBaseARMVIXL {
Roland Levillain47b3ab22017-02-27 14:31:35 +0000817 public:
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000818 ReadBarrierMarkSlowPathARMVIXL(HInstruction* instruction,
819 Location ref,
820 Location entrypoint = Location::NoLocation())
Roland Levillain54f869e2017-03-06 13:54:11 +0000821 : ReadBarrierMarkSlowPathBaseARMVIXL(instruction, ref, entrypoint) {
Roland Levillain47b3ab22017-02-27 14:31:35 +0000822 DCHECK(kEmitCompilerReadBarrier);
823 }
824
Roland Levillain47b3ab22017-02-27 14:31:35 +0000825 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathARMVIXL"; }
826
827 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
828 LocationSummary* locations = instruction_->GetLocations();
Roland Levillain54f869e2017-03-06 13:54:11 +0000829 DCHECK(locations->CanCall());
830 DCHECK(ref_.IsRegister()) << ref_;
831 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_.reg())) << ref_.reg();
832 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
833 << "Unexpected instruction in read barrier marking slow path: "
834 << instruction_->DebugName();
835
836 __ Bind(GetEntryLabel());
837 GenerateReadBarrierMarkRuntimeCall(codegen);
838 __ B(GetExitLabel());
839 }
840
841 private:
842 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathARMVIXL);
843};
844
845// Slow path loading `obj`'s lock word, loading a reference from
846// object `*(obj + offset + (index << scale_factor))` into `ref`, and
847// marking `ref` if `obj` is gray according to the lock word (Baker
848// read barrier). The field `obj.field` in the object `obj` holding
849// this reference does not get updated by this slow path after marking
850// (see LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARMVIXL
851// below for that).
852//
853// This means that after the execution of this slow path, `ref` will
854// always be up-to-date, but `obj.field` may not; i.e., after the
855// flip, `ref` will be a to-space reference, but `obj.field` will
856// probably still be a from-space reference (unless it gets updated by
857// another thread, or if another thread installed another object
858// reference (different from `ref`) in `obj.field`).
859//
860// Argument `entrypoint` must be a register location holding the read
Roland Levillain6d729a72017-06-30 18:34:01 +0100861// barrier marking runtime entry point to be invoked or an empty
862// location; in the latter case, the read barrier marking runtime
863// entry point will be loaded by the slow path code itself.
Roland Levillain54f869e2017-03-06 13:54:11 +0000864class LoadReferenceWithBakerReadBarrierSlowPathARMVIXL : public ReadBarrierMarkSlowPathBaseARMVIXL {
865 public:
866 LoadReferenceWithBakerReadBarrierSlowPathARMVIXL(HInstruction* instruction,
867 Location ref,
868 vixl32::Register obj,
869 uint32_t offset,
870 Location index,
871 ScaleFactor scale_factor,
872 bool needs_null_check,
873 vixl32::Register temp,
Roland Levillain6d729a72017-06-30 18:34:01 +0100874 Location entrypoint = Location::NoLocation())
Roland Levillain54f869e2017-03-06 13:54:11 +0000875 : ReadBarrierMarkSlowPathBaseARMVIXL(instruction, ref, entrypoint),
876 obj_(obj),
877 offset_(offset),
878 index_(index),
879 scale_factor_(scale_factor),
880 needs_null_check_(needs_null_check),
881 temp_(temp) {
882 DCHECK(kEmitCompilerReadBarrier);
883 DCHECK(kUseBakerReadBarrier);
884 }
885
Roland Levillain47b3ab22017-02-27 14:31:35 +0000886 const char* GetDescription() const OVERRIDE {
Roland Levillain54f869e2017-03-06 13:54:11 +0000887 return "LoadReferenceWithBakerReadBarrierSlowPathARMVIXL";
Roland Levillain47b3ab22017-02-27 14:31:35 +0000888 }
889
890 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
891 LocationSummary* locations = instruction_->GetLocations();
892 vixl32::Register ref_reg = RegisterFrom(ref_);
893 DCHECK(locations->CanCall());
894 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg.GetCode())) << ref_reg;
Roland Levillain47b3ab22017-02-27 14:31:35 +0000895 DCHECK(instruction_->IsInstanceFieldGet() ||
896 instruction_->IsStaticFieldGet() ||
897 instruction_->IsArrayGet() ||
898 instruction_->IsArraySet() ||
Roland Levillain47b3ab22017-02-27 14:31:35 +0000899 instruction_->IsInstanceOf() ||
900 instruction_->IsCheckCast() ||
901 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
902 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
903 << "Unexpected instruction in read barrier marking slow path: "
904 << instruction_->DebugName();
905 // The read barrier instrumentation of object ArrayGet
906 // instructions does not support the HIntermediateAddress
907 // instruction.
908 DCHECK(!(instruction_->IsArrayGet() &&
909 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
910
Roland Levillain54f869e2017-03-06 13:54:11 +0000911 // Temporary register `temp_`, used to store the lock word, must
912 // not be IP, as we may use it to emit the reference load (in the
913 // call to GenerateRawReferenceLoad below), and we need the lock
914 // word to still be in `temp_` after the reference load.
915 DCHECK(!temp_.Is(ip));
916
Roland Levillain47b3ab22017-02-27 14:31:35 +0000917 __ Bind(GetEntryLabel());
Roland Levillain54f869e2017-03-06 13:54:11 +0000918
919 // When using MaybeGenerateReadBarrierSlow, the read barrier call is
920 // inserted after the original load. However, in fast path based
921 // Baker's read barriers, we need to perform the load of
922 // mirror::Object::monitor_ *before* the original reference load.
923 // This load-load ordering is required by the read barrier.
Roland Levillainff487002017-03-07 16:50:01 +0000924 // The slow path (for Baker's algorithm) should look like:
Roland Levillain54f869e2017-03-06 13:54:11 +0000925 //
926 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
927 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
928 // HeapReference<mirror::Object> ref = *src; // Original reference load.
929 // bool is_gray = (rb_state == ReadBarrier::GrayState());
930 // if (is_gray) {
931 // ref = entrypoint(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
932 // }
933 //
934 // Note: the original implementation in ReadBarrier::Barrier is
935 // slightly more complex as it performs additional checks that we do
936 // not do here for performance reasons.
937
Roland Levillain47b3ab22017-02-27 14:31:35 +0000938 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
Roland Levillain54f869e2017-03-06 13:54:11 +0000939
940 // /* int32_t */ monitor = obj->monitor_
941 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
942 arm_codegen->GetAssembler()->LoadFromOffset(kLoadWord, temp_, obj_, monitor_offset);
943 if (needs_null_check_) {
944 codegen->MaybeRecordImplicitNullCheck(instruction_);
Roland Levillain47b3ab22017-02-27 14:31:35 +0000945 }
Roland Levillain54f869e2017-03-06 13:54:11 +0000946 // /* LockWord */ lock_word = LockWord(monitor)
947 static_assert(sizeof(LockWord) == sizeof(int32_t),
948 "art::LockWord and int32_t have different sizes.");
949
950 // Introduce a dependency on the lock_word including the rb_state,
951 // which shall prevent load-load reordering without using
952 // a memory barrier (which would be more expensive).
953 // `obj` is unchanged by this operation, but its value now depends
954 // on `temp`.
955 __ Add(obj_, obj_, Operand(temp_, ShiftType::LSR, 32));
956
957 // The actual reference load.
958 // A possible implicit null check has already been handled above.
959 arm_codegen->GenerateRawReferenceLoad(
960 instruction_, ref_, obj_, offset_, index_, scale_factor_, /* needs_null_check */ false);
961
962 // Mark the object `ref` when `obj` is gray.
963 //
964 // if (rb_state == ReadBarrier::GrayState())
965 // ref = ReadBarrier::Mark(ref);
966 //
967 // Given the numeric representation, it's enough to check the low bit of the
968 // rb_state. We do that by shifting the bit out of the lock word with LSRS
969 // which can be a 16-bit instruction unlike the TST immediate.
970 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
971 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
972 __ Lsrs(temp_, temp_, LockWord::kReadBarrierStateShift + 1);
973 __ B(cc, GetExitLabel()); // Carry flag is the last bit shifted out by LSRS.
974 GenerateReadBarrierMarkRuntimeCall(codegen);
975
Roland Levillain47b3ab22017-02-27 14:31:35 +0000976 __ B(GetExitLabel());
977 }
978
979 private:
Roland Levillain54f869e2017-03-06 13:54:11 +0000980 // The register containing the object holding the marked object reference field.
981 vixl32::Register obj_;
982 // The offset, index and scale factor to access the reference in `obj_`.
983 uint32_t offset_;
984 Location index_;
985 ScaleFactor scale_factor_;
986 // Is a null check required?
987 bool needs_null_check_;
988 // A temporary register used to hold the lock word of `obj_`.
989 vixl32::Register temp_;
Roland Levillain47b3ab22017-02-27 14:31:35 +0000990
Roland Levillain54f869e2017-03-06 13:54:11 +0000991 DISALLOW_COPY_AND_ASSIGN(LoadReferenceWithBakerReadBarrierSlowPathARMVIXL);
Roland Levillain47b3ab22017-02-27 14:31:35 +0000992};
993
Roland Levillain54f869e2017-03-06 13:54:11 +0000994// Slow path loading `obj`'s lock word, loading a reference from
995// object `*(obj + offset + (index << scale_factor))` into `ref`, and
996// marking `ref` if `obj` is gray according to the lock word (Baker
997// read barrier). If needed, this slow path also atomically updates
998// the field `obj.field` in the object `obj` holding this reference
999// after marking (contrary to
1000// LoadReferenceWithBakerReadBarrierSlowPathARMVIXL above, which never
1001// tries to update `obj.field`).
Roland Levillain47b3ab22017-02-27 14:31:35 +00001002//
1003// This means that after the execution of this slow path, both `ref`
1004// and `obj.field` will be up-to-date; i.e., after the flip, both will
1005// hold the same to-space reference (unless another thread installed
1006// another object reference (different from `ref`) in `obj.field`).
Roland Levillainba650a42017-03-06 13:52:32 +00001007//
Roland Levillain54f869e2017-03-06 13:54:11 +00001008// Argument `entrypoint` must be a register location holding the read
Roland Levillain6d729a72017-06-30 18:34:01 +01001009// barrier marking runtime entry point to be invoked or an empty
1010// location; in the latter case, the read barrier marking runtime
1011// entry point will be loaded by the slow path code itself.
Roland Levillain54f869e2017-03-06 13:54:11 +00001012class LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARMVIXL
1013 : public ReadBarrierMarkSlowPathBaseARMVIXL {
Roland Levillain47b3ab22017-02-27 14:31:35 +00001014 public:
Roland Levillain6d729a72017-06-30 18:34:01 +01001015 LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARMVIXL(
1016 HInstruction* instruction,
1017 Location ref,
1018 vixl32::Register obj,
1019 uint32_t offset,
1020 Location index,
1021 ScaleFactor scale_factor,
1022 bool needs_null_check,
1023 vixl32::Register temp1,
1024 vixl32::Register temp2,
1025 Location entrypoint = Location::NoLocation())
Roland Levillain54f869e2017-03-06 13:54:11 +00001026 : ReadBarrierMarkSlowPathBaseARMVIXL(instruction, ref, entrypoint),
Roland Levillain47b3ab22017-02-27 14:31:35 +00001027 obj_(obj),
Roland Levillain54f869e2017-03-06 13:54:11 +00001028 offset_(offset),
1029 index_(index),
1030 scale_factor_(scale_factor),
1031 needs_null_check_(needs_null_check),
Roland Levillain47b3ab22017-02-27 14:31:35 +00001032 temp1_(temp1),
Roland Levillain54f869e2017-03-06 13:54:11 +00001033 temp2_(temp2) {
Roland Levillain47b3ab22017-02-27 14:31:35 +00001034 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain54f869e2017-03-06 13:54:11 +00001035 DCHECK(kUseBakerReadBarrier);
Roland Levillain47b3ab22017-02-27 14:31:35 +00001036 }
1037
1038 const char* GetDescription() const OVERRIDE {
Roland Levillain54f869e2017-03-06 13:54:11 +00001039 return "LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARMVIXL";
Roland Levillain47b3ab22017-02-27 14:31:35 +00001040 }
1041
1042 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
1043 LocationSummary* locations = instruction_->GetLocations();
1044 vixl32::Register ref_reg = RegisterFrom(ref_);
1045 DCHECK(locations->CanCall());
1046 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg.GetCode())) << ref_reg;
Roland Levillain54f869e2017-03-06 13:54:11 +00001047 DCHECK_NE(ref_.reg(), LocationFrom(temp1_).reg());
1048
1049 // This slow path is only used by the UnsafeCASObject intrinsic at the moment.
Roland Levillain47b3ab22017-02-27 14:31:35 +00001050 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
1051 << "Unexpected instruction in read barrier marking and field updating slow path: "
1052 << instruction_->DebugName();
1053 DCHECK(instruction_->GetLocations()->Intrinsified());
1054 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
Roland Levillain54f869e2017-03-06 13:54:11 +00001055 DCHECK_EQ(offset_, 0u);
1056 DCHECK_EQ(scale_factor_, ScaleFactor::TIMES_1);
1057 Location field_offset = index_;
1058 DCHECK(field_offset.IsRegisterPair()) << field_offset;
1059
1060 // Temporary register `temp1_`, used to store the lock word, must
1061 // not be IP, as we may use it to emit the reference load (in the
1062 // call to GenerateRawReferenceLoad below), and we need the lock
1063 // word to still be in `temp1_` after the reference load.
1064 DCHECK(!temp1_.Is(ip));
Roland Levillain47b3ab22017-02-27 14:31:35 +00001065
1066 __ Bind(GetEntryLabel());
1067
Roland Levillainff487002017-03-07 16:50:01 +00001068 // The implementation is similar to LoadReferenceWithBakerReadBarrierSlowPathARMVIXL's:
1069 //
1070 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
1071 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
1072 // HeapReference<mirror::Object> ref = *src; // Original reference load.
1073 // bool is_gray = (rb_state == ReadBarrier::GrayState());
1074 // if (is_gray) {
1075 // old_ref = ref;
1076 // ref = entrypoint(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
1077 // compareAndSwapObject(obj, field_offset, old_ref, ref);
1078 // }
1079
Roland Levillain54f869e2017-03-06 13:54:11 +00001080 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
1081
1082 // /* int32_t */ monitor = obj->monitor_
1083 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
1084 arm_codegen->GetAssembler()->LoadFromOffset(kLoadWord, temp1_, obj_, monitor_offset);
1085 if (needs_null_check_) {
1086 codegen->MaybeRecordImplicitNullCheck(instruction_);
1087 }
1088 // /* LockWord */ lock_word = LockWord(monitor)
1089 static_assert(sizeof(LockWord) == sizeof(int32_t),
1090 "art::LockWord and int32_t have different sizes.");
1091
1092 // Introduce a dependency on the lock_word including the rb_state,
1093 // which shall prevent load-load reordering without using
1094 // a memory barrier (which would be more expensive).
1095 // `obj` is unchanged by this operation, but its value now depends
1096 // on `temp`.
1097 __ Add(obj_, obj_, Operand(temp1_, ShiftType::LSR, 32));
1098
1099 // The actual reference load.
1100 // A possible implicit null check has already been handled above.
1101 arm_codegen->GenerateRawReferenceLoad(
1102 instruction_, ref_, obj_, offset_, index_, scale_factor_, /* needs_null_check */ false);
1103
1104 // Mark the object `ref` when `obj` is gray.
1105 //
1106 // if (rb_state == ReadBarrier::GrayState())
1107 // ref = ReadBarrier::Mark(ref);
1108 //
1109 // Given the numeric representation, it's enough to check the low bit of the
1110 // rb_state. We do that by shifting the bit out of the lock word with LSRS
1111 // which can be a 16-bit instruction unlike the TST immediate.
1112 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
1113 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
1114 __ Lsrs(temp1_, temp1_, LockWord::kReadBarrierStateShift + 1);
1115 __ B(cc, GetExitLabel()); // Carry flag is the last bit shifted out by LSRS.
1116
1117 // Save the old value of the reference before marking it.
Roland Levillain47b3ab22017-02-27 14:31:35 +00001118 // Note that we cannot use IP to save the old reference, as IP is
1119 // used internally by the ReadBarrierMarkRegX entry point, and we
1120 // need the old reference after the call to that entry point.
1121 DCHECK(!temp1_.Is(ip));
1122 __ Mov(temp1_, ref_reg);
Roland Levillain27b1f9c2017-01-17 16:56:34 +00001123
Roland Levillain54f869e2017-03-06 13:54:11 +00001124 GenerateReadBarrierMarkRuntimeCall(codegen);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001125
1126 // If the new reference is different from the old reference,
Roland Levillain54f869e2017-03-06 13:54:11 +00001127 // update the field in the holder (`*(obj_ + field_offset)`).
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001128 //
1129 // Note that this field could also hold a different object, if
1130 // another thread had concurrently changed it. In that case, the
Anton Kirilov349e61f2017-12-15 17:11:33 +00001131 // LDREX/CMP/BNE sequence of instructions in the compare-and-set
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001132 // (CAS) operation below would abort the CAS, leaving the field
1133 // as-is.
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001134 __ Cmp(temp1_, ref_reg);
Roland Levillain54f869e2017-03-06 13:54:11 +00001135 __ B(eq, GetExitLabel());
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001136
1137 // Update the the holder's field atomically. This may fail if
1138 // mutator updates before us, but it's OK. This is achieved
1139 // using a strong compare-and-set (CAS) operation with relaxed
1140 // memory synchronization ordering, where the expected value is
1141 // the old reference and the desired value is the new reference.
1142
1143 UseScratchRegisterScope temps(arm_codegen->GetVIXLAssembler());
1144 // Convenience aliases.
1145 vixl32::Register base = obj_;
1146 // The UnsafeCASObject intrinsic uses a register pair as field
1147 // offset ("long offset"), of which only the low part contains
1148 // data.
Roland Levillain54f869e2017-03-06 13:54:11 +00001149 vixl32::Register offset = LowRegisterFrom(field_offset);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001150 vixl32::Register expected = temp1_;
1151 vixl32::Register value = ref_reg;
1152 vixl32::Register tmp_ptr = temps.Acquire(); // Pointer to actual memory.
1153 vixl32::Register tmp = temp2_; // Value in memory.
1154
1155 __ Add(tmp_ptr, base, offset);
1156
1157 if (kPoisonHeapReferences) {
1158 arm_codegen->GetAssembler()->PoisonHeapReference(expected);
1159 if (value.Is(expected)) {
1160 // Do not poison `value`, as it is the same register as
1161 // `expected`, which has just been poisoned.
1162 } else {
1163 arm_codegen->GetAssembler()->PoisonHeapReference(value);
1164 }
1165 }
1166
1167 // do {
1168 // tmp = [r_ptr] - expected;
1169 // } while (tmp == 0 && failure([r_ptr] <- r_new_value));
1170
Anton Kirilov349e61f2017-12-15 17:11:33 +00001171 vixl32::Label loop_head, comparison_failed, exit_loop;
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001172 __ Bind(&loop_head);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001173 __ Ldrex(tmp, MemOperand(tmp_ptr));
Anton Kirilov349e61f2017-12-15 17:11:33 +00001174 __ Cmp(tmp, expected);
1175 __ B(ne, &comparison_failed, /* far_target */ false);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001176 __ Strex(tmp, value, MemOperand(tmp_ptr));
Anton Kirilov349e61f2017-12-15 17:11:33 +00001177 __ CompareAndBranchIfZero(tmp, &exit_loop, /* far_target */ false);
1178 __ B(&loop_head);
1179 __ Bind(&comparison_failed);
1180 __ Clrex();
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001181 __ Bind(&exit_loop);
1182
1183 if (kPoisonHeapReferences) {
1184 arm_codegen->GetAssembler()->UnpoisonHeapReference(expected);
1185 if (value.Is(expected)) {
1186 // Do not unpoison `value`, as it is the same register as
1187 // `expected`, which has just been unpoisoned.
1188 } else {
1189 arm_codegen->GetAssembler()->UnpoisonHeapReference(value);
1190 }
1191 }
1192
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001193 __ B(GetExitLabel());
1194 }
1195
1196 private:
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001197 // The register containing the object holding the marked object reference field.
1198 const vixl32::Register obj_;
Roland Levillain54f869e2017-03-06 13:54:11 +00001199 // The offset, index and scale factor to access the reference in `obj_`.
1200 uint32_t offset_;
1201 Location index_;
1202 ScaleFactor scale_factor_;
1203 // Is a null check required?
1204 bool needs_null_check_;
1205 // A temporary register used to hold the lock word of `obj_`; and
1206 // also to hold the original reference value, when the reference is
1207 // marked.
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001208 const vixl32::Register temp1_;
Roland Levillain54f869e2017-03-06 13:54:11 +00001209 // A temporary register used in the implementation of the CAS, to
1210 // update the object's reference field.
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001211 const vixl32::Register temp2_;
1212
Roland Levillain54f869e2017-03-06 13:54:11 +00001213 DISALLOW_COPY_AND_ASSIGN(LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARMVIXL);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001214};
1215
1216// Slow path generating a read barrier for a heap reference.
1217class ReadBarrierForHeapReferenceSlowPathARMVIXL : public SlowPathCodeARMVIXL {
1218 public:
1219 ReadBarrierForHeapReferenceSlowPathARMVIXL(HInstruction* instruction,
1220 Location out,
1221 Location ref,
1222 Location obj,
1223 uint32_t offset,
1224 Location index)
1225 : SlowPathCodeARMVIXL(instruction),
1226 out_(out),
1227 ref_(ref),
1228 obj_(obj),
1229 offset_(offset),
1230 index_(index) {
1231 DCHECK(kEmitCompilerReadBarrier);
1232 // If `obj` is equal to `out` or `ref`, it means the initial object
1233 // has been overwritten by (or after) the heap object reference load
1234 // to be instrumented, e.g.:
1235 //
1236 // __ LoadFromOffset(kLoadWord, out, out, offset);
1237 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
1238 //
1239 // In that case, we have lost the information about the original
1240 // object, and the emitted read barrier cannot work properly.
1241 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
1242 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
1243 }
1244
1245 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
1246 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
1247 LocationSummary* locations = instruction_->GetLocations();
1248 vixl32::Register reg_out = RegisterFrom(out_);
1249 DCHECK(locations->CanCall());
1250 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out.GetCode()));
1251 DCHECK(instruction_->IsInstanceFieldGet() ||
1252 instruction_->IsStaticFieldGet() ||
1253 instruction_->IsArrayGet() ||
1254 instruction_->IsInstanceOf() ||
1255 instruction_->IsCheckCast() ||
Andreas Gamped9911ee2017-03-27 13:27:24 -07001256 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001257 << "Unexpected instruction in read barrier for heap reference slow path: "
1258 << instruction_->DebugName();
1259 // The read barrier instrumentation of object ArrayGet
1260 // instructions does not support the HIntermediateAddress
1261 // instruction.
1262 DCHECK(!(instruction_->IsArrayGet() &&
1263 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
1264
1265 __ Bind(GetEntryLabel());
1266 SaveLiveRegisters(codegen, locations);
1267
1268 // We may have to change the index's value, but as `index_` is a
1269 // constant member (like other "inputs" of this slow path),
1270 // introduce a copy of it, `index`.
1271 Location index = index_;
1272 if (index_.IsValid()) {
1273 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
1274 if (instruction_->IsArrayGet()) {
1275 // Compute the actual memory offset and store it in `index`.
1276 vixl32::Register index_reg = RegisterFrom(index_);
1277 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg.GetCode()));
1278 if (codegen->IsCoreCalleeSaveRegister(index_reg.GetCode())) {
1279 // We are about to change the value of `index_reg` (see the
Roland Levillain9983e302017-07-14 14:34:22 +01001280 // calls to art::arm::ArmVIXLMacroAssembler::Lsl and
1281 // art::arm::ArmVIXLMacroAssembler::Add below), but it has
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001282 // not been saved by the previous call to
1283 // art::SlowPathCode::SaveLiveRegisters, as it is a
1284 // callee-save register --
1285 // art::SlowPathCode::SaveLiveRegisters does not consider
1286 // callee-save registers, as it has been designed with the
1287 // assumption that callee-save registers are supposed to be
1288 // handled by the called function. So, as a callee-save
1289 // register, `index_reg` _would_ eventually be saved onto
1290 // the stack, but it would be too late: we would have
1291 // changed its value earlier. Therefore, we manually save
1292 // it here into another freely available register,
1293 // `free_reg`, chosen of course among the caller-save
1294 // registers (as a callee-save `free_reg` register would
1295 // exhibit the same problem).
1296 //
1297 // Note we could have requested a temporary register from
1298 // the register allocator instead; but we prefer not to, as
1299 // this is a slow path, and we know we can find a
1300 // caller-save register that is available.
1301 vixl32::Register free_reg = FindAvailableCallerSaveRegister(codegen);
1302 __ Mov(free_reg, index_reg);
1303 index_reg = free_reg;
1304 index = LocationFrom(index_reg);
1305 } else {
1306 // The initial register stored in `index_` has already been
1307 // saved in the call to art::SlowPathCode::SaveLiveRegisters
1308 // (as it is not a callee-save register), so we can freely
1309 // use it.
1310 }
1311 // Shifting the index value contained in `index_reg` by the scale
1312 // factor (2) cannot overflow in practice, as the runtime is
1313 // unable to allocate object arrays with a size larger than
1314 // 2^26 - 1 (that is, 2^28 - 4 bytes).
1315 __ Lsl(index_reg, index_reg, TIMES_4);
1316 static_assert(
1317 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
1318 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
1319 __ Add(index_reg, index_reg, offset_);
1320 } else {
1321 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
1322 // intrinsics, `index_` is not shifted by a scale factor of 2
1323 // (as in the case of ArrayGet), as it is actually an offset
1324 // to an object field within an object.
1325 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
1326 DCHECK(instruction_->GetLocations()->Intrinsified());
1327 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
1328 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
1329 << instruction_->AsInvoke()->GetIntrinsic();
1330 DCHECK_EQ(offset_, 0U);
1331 DCHECK(index_.IsRegisterPair());
1332 // UnsafeGet's offset location is a register pair, the low
1333 // part contains the correct offset.
1334 index = index_.ToLow();
1335 }
1336 }
1337
1338 // We're moving two or three locations to locations that could
1339 // overlap, so we need a parallel move resolver.
1340 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +01001341 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001342 parallel_move.AddMove(ref_,
1343 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001344 DataType::Type::kReference,
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001345 nullptr);
1346 parallel_move.AddMove(obj_,
1347 LocationFrom(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001348 DataType::Type::kReference,
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001349 nullptr);
1350 if (index.IsValid()) {
1351 parallel_move.AddMove(index,
1352 LocationFrom(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001353 DataType::Type::kInt32,
Anton Kirilovedb2ac32016-11-30 15:14:10 +00001354 nullptr);
1355 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
1356 } else {
1357 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
1358 __ Mov(calling_convention.GetRegisterAt(2), offset_);
1359 }
1360 arm_codegen->InvokeRuntime(kQuickReadBarrierSlow, instruction_, instruction_->GetDexPc(), this);
1361 CheckEntrypointTypes<
1362 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
1363 arm_codegen->Move32(out_, LocationFrom(r0));
1364
1365 RestoreLiveRegisters(codegen, locations);
1366 __ B(GetExitLabel());
1367 }
1368
1369 const char* GetDescription() const OVERRIDE {
1370 return "ReadBarrierForHeapReferenceSlowPathARMVIXL";
1371 }
1372
1373 private:
1374 vixl32::Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
1375 uint32_t ref = RegisterFrom(ref_).GetCode();
1376 uint32_t obj = RegisterFrom(obj_).GetCode();
1377 for (uint32_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
1378 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
1379 return vixl32::Register(i);
1380 }
1381 }
1382 // We shall never fail to find a free caller-save register, as
1383 // there are more than two core caller-save registers on ARM
1384 // (meaning it is possible to find one which is different from
1385 // `ref` and `obj`).
1386 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
1387 LOG(FATAL) << "Could not find a free caller-save register";
1388 UNREACHABLE();
1389 }
1390
1391 const Location out_;
1392 const Location ref_;
1393 const Location obj_;
1394 const uint32_t offset_;
1395 // An additional location containing an index to an array.
1396 // Only used for HArrayGet and the UnsafeGetObject &
1397 // UnsafeGetObjectVolatile intrinsics.
1398 const Location index_;
1399
1400 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathARMVIXL);
1401};
1402
1403// Slow path generating a read barrier for a GC root.
1404class ReadBarrierForRootSlowPathARMVIXL : public SlowPathCodeARMVIXL {
1405 public:
1406 ReadBarrierForRootSlowPathARMVIXL(HInstruction* instruction, Location out, Location root)
1407 : SlowPathCodeARMVIXL(instruction), out_(out), root_(root) {
1408 DCHECK(kEmitCompilerReadBarrier);
1409 }
1410
1411 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
1412 LocationSummary* locations = instruction_->GetLocations();
1413 vixl32::Register reg_out = RegisterFrom(out_);
1414 DCHECK(locations->CanCall());
1415 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out.GetCode()));
1416 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
1417 << "Unexpected instruction in read barrier for GC root slow path: "
1418 << instruction_->DebugName();
1419
1420 __ Bind(GetEntryLabel());
1421 SaveLiveRegisters(codegen, locations);
1422
1423 InvokeRuntimeCallingConventionARMVIXL calling_convention;
1424 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
1425 arm_codegen->Move32(LocationFrom(calling_convention.GetRegisterAt(0)), root_);
1426 arm_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
1427 instruction_,
1428 instruction_->GetDexPc(),
1429 this);
1430 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
1431 arm_codegen->Move32(out_, LocationFrom(r0));
1432
1433 RestoreLiveRegisters(codegen, locations);
1434 __ B(GetExitLabel());
1435 }
1436
1437 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathARMVIXL"; }
1438
1439 private:
1440 const Location out_;
1441 const Location root_;
1442
1443 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathARMVIXL);
1444};
Scott Wakelingc34dba72016-10-03 10:14:44 +01001445
Scott Wakelingfe885462016-09-22 10:24:38 +01001446inline vixl32::Condition ARMCondition(IfCondition cond) {
1447 switch (cond) {
1448 case kCondEQ: return eq;
1449 case kCondNE: return ne;
1450 case kCondLT: return lt;
1451 case kCondLE: return le;
1452 case kCondGT: return gt;
1453 case kCondGE: return ge;
1454 case kCondB: return lo;
1455 case kCondBE: return ls;
1456 case kCondA: return hi;
1457 case kCondAE: return hs;
1458 }
1459 LOG(FATAL) << "Unreachable";
1460 UNREACHABLE();
1461}
1462
1463// Maps signed condition to unsigned condition.
1464inline vixl32::Condition ARMUnsignedCondition(IfCondition cond) {
1465 switch (cond) {
1466 case kCondEQ: return eq;
1467 case kCondNE: return ne;
1468 // Signed to unsigned.
1469 case kCondLT: return lo;
1470 case kCondLE: return ls;
1471 case kCondGT: return hi;
1472 case kCondGE: return hs;
1473 // Unsigned remain unchanged.
1474 case kCondB: return lo;
1475 case kCondBE: return ls;
1476 case kCondA: return hi;
1477 case kCondAE: return hs;
1478 }
1479 LOG(FATAL) << "Unreachable";
1480 UNREACHABLE();
1481}
1482
1483inline vixl32::Condition ARMFPCondition(IfCondition cond, bool gt_bias) {
1484 // The ARM condition codes can express all the necessary branches, see the
1485 // "Meaning (floating-point)" column in the table A8-1 of the ARMv7 reference manual.
1486 // There is no dex instruction or HIR that would need the missing conditions
1487 // "equal or unordered" or "not equal".
1488 switch (cond) {
1489 case kCondEQ: return eq;
1490 case kCondNE: return ne /* unordered */;
1491 case kCondLT: return gt_bias ? cc : lt /* unordered */;
1492 case kCondLE: return gt_bias ? ls : le /* unordered */;
1493 case kCondGT: return gt_bias ? hi /* unordered */ : gt;
1494 case kCondGE: return gt_bias ? cs /* unordered */ : ge;
1495 default:
1496 LOG(FATAL) << "UNREACHABLE";
1497 UNREACHABLE();
1498 }
1499}
1500
Anton Kirilov74234da2017-01-13 14:42:47 +00001501inline ShiftType ShiftFromOpKind(HDataProcWithShifterOp::OpKind op_kind) {
1502 switch (op_kind) {
1503 case HDataProcWithShifterOp::kASR: return ShiftType::ASR;
1504 case HDataProcWithShifterOp::kLSL: return ShiftType::LSL;
1505 case HDataProcWithShifterOp::kLSR: return ShiftType::LSR;
1506 default:
1507 LOG(FATAL) << "Unexpected op kind " << op_kind;
1508 UNREACHABLE();
1509 }
1510}
1511
Scott Wakelingfe885462016-09-22 10:24:38 +01001512void CodeGeneratorARMVIXL::DumpCoreRegister(std::ostream& stream, int reg) const {
1513 stream << vixl32::Register(reg);
1514}
1515
1516void CodeGeneratorARMVIXL::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
1517 stream << vixl32::SRegister(reg);
1518}
1519
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001520static uint32_t ComputeSRegisterListMask(const SRegisterList& regs) {
Scott Wakelingfe885462016-09-22 10:24:38 +01001521 uint32_t mask = 0;
1522 for (uint32_t i = regs.GetFirstSRegister().GetCode();
1523 i <= regs.GetLastSRegister().GetCode();
1524 ++i) {
1525 mask |= (1 << i);
1526 }
1527 return mask;
1528}
1529
Artem Serovd4cc5b22016-11-04 11:19:09 +00001530// Saves the register in the stack. Returns the size taken on stack.
1531size_t CodeGeneratorARMVIXL::SaveCoreRegister(size_t stack_index ATTRIBUTE_UNUSED,
1532 uint32_t reg_id ATTRIBUTE_UNUSED) {
1533 TODO_VIXL32(FATAL);
1534 return 0;
1535}
1536
1537// Restores the register from the stack. Returns the size taken on stack.
1538size_t CodeGeneratorARMVIXL::RestoreCoreRegister(size_t stack_index ATTRIBUTE_UNUSED,
1539 uint32_t reg_id ATTRIBUTE_UNUSED) {
1540 TODO_VIXL32(FATAL);
1541 return 0;
1542}
1543
1544size_t CodeGeneratorARMVIXL::SaveFloatingPointRegister(size_t stack_index ATTRIBUTE_UNUSED,
1545 uint32_t reg_id ATTRIBUTE_UNUSED) {
1546 TODO_VIXL32(FATAL);
1547 return 0;
1548}
1549
1550size_t CodeGeneratorARMVIXL::RestoreFloatingPointRegister(size_t stack_index ATTRIBUTE_UNUSED,
1551 uint32_t reg_id ATTRIBUTE_UNUSED) {
1552 TODO_VIXL32(FATAL);
1553 return 0;
Anton Kirilove28d9ae2016-10-25 18:17:23 +01001554}
1555
Anton Kirilov74234da2017-01-13 14:42:47 +00001556static void GenerateDataProcInstruction(HInstruction::InstructionKind kind,
1557 vixl32::Register out,
1558 vixl32::Register first,
1559 const Operand& second,
1560 CodeGeneratorARMVIXL* codegen) {
1561 if (second.IsImmediate() && second.GetImmediate() == 0) {
1562 const Operand in = kind == HInstruction::kAnd
1563 ? Operand(0)
1564 : Operand(first);
1565
1566 __ Mov(out, in);
1567 } else {
1568 switch (kind) {
1569 case HInstruction::kAdd:
1570 __ Add(out, first, second);
1571 break;
1572 case HInstruction::kAnd:
1573 __ And(out, first, second);
1574 break;
1575 case HInstruction::kOr:
1576 __ Orr(out, first, second);
1577 break;
1578 case HInstruction::kSub:
1579 __ Sub(out, first, second);
1580 break;
1581 case HInstruction::kXor:
1582 __ Eor(out, first, second);
1583 break;
1584 default:
1585 LOG(FATAL) << "Unexpected instruction kind: " << kind;
1586 UNREACHABLE();
1587 }
1588 }
1589}
1590
1591static void GenerateDataProc(HInstruction::InstructionKind kind,
1592 const Location& out,
1593 const Location& first,
1594 const Operand& second_lo,
1595 const Operand& second_hi,
1596 CodeGeneratorARMVIXL* codegen) {
1597 const vixl32::Register first_hi = HighRegisterFrom(first);
1598 const vixl32::Register first_lo = LowRegisterFrom(first);
1599 const vixl32::Register out_hi = HighRegisterFrom(out);
1600 const vixl32::Register out_lo = LowRegisterFrom(out);
1601
1602 if (kind == HInstruction::kAdd) {
1603 __ Adds(out_lo, first_lo, second_lo);
1604 __ Adc(out_hi, first_hi, second_hi);
1605 } else if (kind == HInstruction::kSub) {
1606 __ Subs(out_lo, first_lo, second_lo);
1607 __ Sbc(out_hi, first_hi, second_hi);
1608 } else {
1609 GenerateDataProcInstruction(kind, out_lo, first_lo, second_lo, codegen);
1610 GenerateDataProcInstruction(kind, out_hi, first_hi, second_hi, codegen);
1611 }
1612}
1613
1614static Operand GetShifterOperand(vixl32::Register rm, ShiftType shift, uint32_t shift_imm) {
1615 return shift_imm == 0 ? Operand(rm) : Operand(rm, shift, shift_imm);
1616}
1617
1618static void GenerateLongDataProc(HDataProcWithShifterOp* instruction,
1619 CodeGeneratorARMVIXL* codegen) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001620 DCHECK_EQ(instruction->GetType(), DataType::Type::kInt64);
Anton Kirilov74234da2017-01-13 14:42:47 +00001621 DCHECK(HDataProcWithShifterOp::IsShiftOp(instruction->GetOpKind()));
1622
1623 const LocationSummary* const locations = instruction->GetLocations();
1624 const uint32_t shift_value = instruction->GetShiftAmount();
1625 const HInstruction::InstructionKind kind = instruction->GetInstrKind();
1626 const Location first = locations->InAt(0);
1627 const Location second = locations->InAt(1);
1628 const Location out = locations->Out();
1629 const vixl32::Register first_hi = HighRegisterFrom(first);
1630 const vixl32::Register first_lo = LowRegisterFrom(first);
1631 const vixl32::Register out_hi = HighRegisterFrom(out);
1632 const vixl32::Register out_lo = LowRegisterFrom(out);
1633 const vixl32::Register second_hi = HighRegisterFrom(second);
1634 const vixl32::Register second_lo = LowRegisterFrom(second);
1635 const ShiftType shift = ShiftFromOpKind(instruction->GetOpKind());
1636
1637 if (shift_value >= 32) {
1638 if (shift == ShiftType::LSL) {
1639 GenerateDataProcInstruction(kind,
1640 out_hi,
1641 first_hi,
1642 Operand(second_lo, ShiftType::LSL, shift_value - 32),
1643 codegen);
1644 GenerateDataProcInstruction(kind, out_lo, first_lo, 0, codegen);
1645 } else if (shift == ShiftType::ASR) {
1646 GenerateDataProc(kind,
1647 out,
1648 first,
1649 GetShifterOperand(second_hi, ShiftType::ASR, shift_value - 32),
1650 Operand(second_hi, ShiftType::ASR, 31),
1651 codegen);
1652 } else {
1653 DCHECK_EQ(shift, ShiftType::LSR);
1654 GenerateDataProc(kind,
1655 out,
1656 first,
1657 GetShifterOperand(second_hi, ShiftType::LSR, shift_value - 32),
1658 0,
1659 codegen);
1660 }
1661 } else {
1662 DCHECK_GT(shift_value, 1U);
1663 DCHECK_LT(shift_value, 32U);
1664
1665 UseScratchRegisterScope temps(codegen->GetVIXLAssembler());
1666
1667 if (shift == ShiftType::LSL) {
1668 // We are not doing this for HInstruction::kAdd because the output will require
1669 // Location::kOutputOverlap; not applicable to other cases.
1670 if (kind == HInstruction::kOr || kind == HInstruction::kXor) {
1671 GenerateDataProcInstruction(kind,
1672 out_hi,
1673 first_hi,
1674 Operand(second_hi, ShiftType::LSL, shift_value),
1675 codegen);
1676 GenerateDataProcInstruction(kind,
1677 out_hi,
1678 out_hi,
1679 Operand(second_lo, ShiftType::LSR, 32 - shift_value),
1680 codegen);
1681 GenerateDataProcInstruction(kind,
1682 out_lo,
1683 first_lo,
1684 Operand(second_lo, ShiftType::LSL, shift_value),
1685 codegen);
1686 } else {
1687 const vixl32::Register temp = temps.Acquire();
1688
1689 __ Lsl(temp, second_hi, shift_value);
1690 __ Orr(temp, temp, Operand(second_lo, ShiftType::LSR, 32 - shift_value));
1691 GenerateDataProc(kind,
1692 out,
1693 first,
1694 Operand(second_lo, ShiftType::LSL, shift_value),
1695 temp,
1696 codegen);
1697 }
1698 } else {
1699 DCHECK(shift == ShiftType::ASR || shift == ShiftType::LSR);
1700
1701 // We are not doing this for HInstruction::kAdd because the output will require
1702 // Location::kOutputOverlap; not applicable to other cases.
1703 if (kind == HInstruction::kOr || kind == HInstruction::kXor) {
1704 GenerateDataProcInstruction(kind,
1705 out_lo,
1706 first_lo,
1707 Operand(second_lo, ShiftType::LSR, shift_value),
1708 codegen);
1709 GenerateDataProcInstruction(kind,
1710 out_lo,
1711 out_lo,
1712 Operand(second_hi, ShiftType::LSL, 32 - shift_value),
1713 codegen);
1714 GenerateDataProcInstruction(kind,
1715 out_hi,
1716 first_hi,
1717 Operand(second_hi, shift, shift_value),
1718 codegen);
1719 } else {
1720 const vixl32::Register temp = temps.Acquire();
1721
1722 __ Lsr(temp, second_lo, shift_value);
1723 __ Orr(temp, temp, Operand(second_hi, ShiftType::LSL, 32 - shift_value));
1724 GenerateDataProc(kind,
1725 out,
1726 first,
1727 temp,
1728 Operand(second_hi, shift, shift_value),
1729 codegen);
1730 }
1731 }
1732 }
1733}
1734
Donghui Bai426b49c2016-11-08 14:55:38 +08001735static void GenerateVcmp(HInstruction* instruction, CodeGeneratorARMVIXL* codegen) {
1736 const Location rhs_loc = instruction->GetLocations()->InAt(1);
1737 if (rhs_loc.IsConstant()) {
1738 // 0.0 is the only immediate that can be encoded directly in
1739 // a VCMP instruction.
1740 //
1741 // Both the JLS (section 15.20.1) and the JVMS (section 6.5)
1742 // specify that in a floating-point comparison, positive zero
1743 // and negative zero are considered equal, so we can use the
1744 // literal 0.0 for both cases here.
1745 //
1746 // Note however that some methods (Float.equal, Float.compare,
1747 // Float.compareTo, Double.equal, Double.compare,
1748 // Double.compareTo, Math.max, Math.min, StrictMath.max,
1749 // StrictMath.min) consider 0.0 to be (strictly) greater than
1750 // -0.0. So if we ever translate calls to these methods into a
1751 // HCompare instruction, we must handle the -0.0 case with
1752 // care here.
1753 DCHECK(rhs_loc.GetConstant()->IsArithmeticZero());
1754
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001755 const DataType::Type type = instruction->InputAt(0)->GetType();
Donghui Bai426b49c2016-11-08 14:55:38 +08001756
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001757 if (type == DataType::Type::kFloat32) {
Donghui Bai426b49c2016-11-08 14:55:38 +08001758 __ Vcmp(F32, InputSRegisterAt(instruction, 0), 0.0);
1759 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001760 DCHECK_EQ(type, DataType::Type::kFloat64);
Donghui Bai426b49c2016-11-08 14:55:38 +08001761 __ Vcmp(F64, InputDRegisterAt(instruction, 0), 0.0);
1762 }
1763 } else {
1764 __ Vcmp(InputVRegisterAt(instruction, 0), InputVRegisterAt(instruction, 1));
1765 }
1766}
1767
Anton Kirilov5601d4e2017-05-11 19:33:50 +01001768static int64_t AdjustConstantForCondition(int64_t value,
1769 IfCondition* condition,
1770 IfCondition* opposite) {
1771 if (value == 1) {
1772 if (*condition == kCondB) {
1773 value = 0;
1774 *condition = kCondEQ;
1775 *opposite = kCondNE;
1776 } else if (*condition == kCondAE) {
1777 value = 0;
1778 *condition = kCondNE;
1779 *opposite = kCondEQ;
1780 }
1781 } else if (value == -1) {
1782 if (*condition == kCondGT) {
1783 value = 0;
1784 *condition = kCondGE;
1785 *opposite = kCondLT;
1786 } else if (*condition == kCondLE) {
1787 value = 0;
1788 *condition = kCondLT;
1789 *opposite = kCondGE;
1790 }
1791 }
1792
1793 return value;
1794}
1795
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001796static std::pair<vixl32::Condition, vixl32::Condition> GenerateLongTestConstant(
1797 HCondition* condition,
1798 bool invert,
1799 CodeGeneratorARMVIXL* codegen) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001800 DCHECK_EQ(condition->GetLeft()->GetType(), DataType::Type::kInt64);
Donghui Bai426b49c2016-11-08 14:55:38 +08001801
1802 const LocationSummary* const locations = condition->GetLocations();
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001803 IfCondition cond = condition->GetCondition();
1804 IfCondition opposite = condition->GetOppositeCondition();
1805
1806 if (invert) {
1807 std::swap(cond, opposite);
1808 }
1809
1810 std::pair<vixl32::Condition, vixl32::Condition> ret(eq, ne);
Donghui Bai426b49c2016-11-08 14:55:38 +08001811 const Location left = locations->InAt(0);
1812 const Location right = locations->InAt(1);
1813
1814 DCHECK(right.IsConstant());
1815
1816 const vixl32::Register left_high = HighRegisterFrom(left);
1817 const vixl32::Register left_low = LowRegisterFrom(left);
Anton Kirilov5601d4e2017-05-11 19:33:50 +01001818 int64_t value = AdjustConstantForCondition(Int64ConstantFrom(right), &cond, &opposite);
1819 UseScratchRegisterScope temps(codegen->GetVIXLAssembler());
1820
1821 // Comparisons against 0 are common enough to deserve special attention.
1822 if (value == 0) {
1823 switch (cond) {
1824 case kCondNE:
1825 // x > 0 iff x != 0 when the comparison is unsigned.
1826 case kCondA:
1827 ret = std::make_pair(ne, eq);
1828 FALLTHROUGH_INTENDED;
1829 case kCondEQ:
1830 // x <= 0 iff x == 0 when the comparison is unsigned.
1831 case kCondBE:
1832 __ Orrs(temps.Acquire(), left_low, left_high);
1833 return ret;
1834 case kCondLT:
1835 case kCondGE:
1836 __ Cmp(left_high, 0);
1837 return std::make_pair(ARMCondition(cond), ARMCondition(opposite));
1838 // Trivially true or false.
1839 case kCondB:
1840 ret = std::make_pair(ne, eq);
1841 FALLTHROUGH_INTENDED;
1842 case kCondAE:
1843 __ Cmp(left_low, left_low);
1844 return ret;
1845 default:
1846 break;
1847 }
1848 }
Donghui Bai426b49c2016-11-08 14:55:38 +08001849
1850 switch (cond) {
1851 case kCondEQ:
1852 case kCondNE:
1853 case kCondB:
1854 case kCondBE:
1855 case kCondA:
1856 case kCondAE: {
Anton Kirilov23b752b2017-07-20 14:40:44 +01001857 const uint32_t value_low = Low32Bits(value);
1858 Operand operand_low(value_low);
1859
Donghui Bai426b49c2016-11-08 14:55:38 +08001860 __ Cmp(left_high, High32Bits(value));
1861
Anton Kirilov23b752b2017-07-20 14:40:44 +01001862 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
1863 // we must ensure that the operands corresponding to the least significant
1864 // halves of the inputs fit into a 16-bit CMP encoding.
1865 if (!left_low.IsLow() || !IsUint<8>(value_low)) {
1866 operand_low = Operand(temps.Acquire());
1867 __ Mov(LeaveFlags, operand_low.GetBaseRegister(), value_low);
1868 }
1869
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001870 // We use the scope because of the IT block that follows.
Donghui Bai426b49c2016-11-08 14:55:38 +08001871 ExactAssemblyScope guard(codegen->GetVIXLAssembler(),
1872 2 * vixl32::k16BitT32InstructionSizeInBytes,
1873 CodeBufferCheckScope::kExactSize);
1874
1875 __ it(eq);
Anton Kirilov23b752b2017-07-20 14:40:44 +01001876 __ cmp(eq, left_low, operand_low);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001877 ret = std::make_pair(ARMUnsignedCondition(cond), ARMUnsignedCondition(opposite));
Donghui Bai426b49c2016-11-08 14:55:38 +08001878 break;
1879 }
1880 case kCondLE:
1881 case kCondGT:
1882 // Trivially true or false.
1883 if (value == std::numeric_limits<int64_t>::max()) {
1884 __ Cmp(left_low, left_low);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001885 ret = cond == kCondLE ? std::make_pair(eq, ne) : std::make_pair(ne, eq);
Donghui Bai426b49c2016-11-08 14:55:38 +08001886 break;
1887 }
1888
1889 if (cond == kCondLE) {
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001890 DCHECK_EQ(opposite, kCondGT);
Donghui Bai426b49c2016-11-08 14:55:38 +08001891 cond = kCondLT;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001892 opposite = kCondGE;
Donghui Bai426b49c2016-11-08 14:55:38 +08001893 } else {
1894 DCHECK_EQ(cond, kCondGT);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001895 DCHECK_EQ(opposite, kCondLE);
Donghui Bai426b49c2016-11-08 14:55:38 +08001896 cond = kCondGE;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001897 opposite = kCondLT;
Donghui Bai426b49c2016-11-08 14:55:38 +08001898 }
1899
1900 value++;
1901 FALLTHROUGH_INTENDED;
1902 case kCondGE:
1903 case kCondLT: {
Donghui Bai426b49c2016-11-08 14:55:38 +08001904 __ Cmp(left_low, Low32Bits(value));
1905 __ Sbcs(temps.Acquire(), left_high, High32Bits(value));
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001906 ret = std::make_pair(ARMCondition(cond), ARMCondition(opposite));
Donghui Bai426b49c2016-11-08 14:55:38 +08001907 break;
1908 }
1909 default:
1910 LOG(FATAL) << "Unreachable";
1911 UNREACHABLE();
1912 }
1913
1914 return ret;
1915}
1916
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001917static std::pair<vixl32::Condition, vixl32::Condition> GenerateLongTest(
1918 HCondition* condition,
1919 bool invert,
1920 CodeGeneratorARMVIXL* codegen) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001921 DCHECK_EQ(condition->GetLeft()->GetType(), DataType::Type::kInt64);
Donghui Bai426b49c2016-11-08 14:55:38 +08001922
1923 const LocationSummary* const locations = condition->GetLocations();
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001924 IfCondition cond = condition->GetCondition();
1925 IfCondition opposite = condition->GetOppositeCondition();
1926
1927 if (invert) {
1928 std::swap(cond, opposite);
1929 }
1930
1931 std::pair<vixl32::Condition, vixl32::Condition> ret(eq, ne);
Donghui Bai426b49c2016-11-08 14:55:38 +08001932 Location left = locations->InAt(0);
1933 Location right = locations->InAt(1);
1934
1935 DCHECK(right.IsRegisterPair());
1936
1937 switch (cond) {
1938 case kCondEQ:
1939 case kCondNE:
1940 case kCondB:
1941 case kCondBE:
1942 case kCondA:
1943 case kCondAE: {
1944 __ Cmp(HighRegisterFrom(left), HighRegisterFrom(right));
1945
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001946 // We use the scope because of the IT block that follows.
Donghui Bai426b49c2016-11-08 14:55:38 +08001947 ExactAssemblyScope guard(codegen->GetVIXLAssembler(),
1948 2 * vixl32::k16BitT32InstructionSizeInBytes,
1949 CodeBufferCheckScope::kExactSize);
1950
1951 __ it(eq);
1952 __ cmp(eq, LowRegisterFrom(left), LowRegisterFrom(right));
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001953 ret = std::make_pair(ARMUnsignedCondition(cond), ARMUnsignedCondition(opposite));
Donghui Bai426b49c2016-11-08 14:55:38 +08001954 break;
1955 }
1956 case kCondLE:
1957 case kCondGT:
1958 if (cond == kCondLE) {
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001959 DCHECK_EQ(opposite, kCondGT);
Donghui Bai426b49c2016-11-08 14:55:38 +08001960 cond = kCondGE;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001961 opposite = kCondLT;
Donghui Bai426b49c2016-11-08 14:55:38 +08001962 } else {
1963 DCHECK_EQ(cond, kCondGT);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001964 DCHECK_EQ(opposite, kCondLE);
Donghui Bai426b49c2016-11-08 14:55:38 +08001965 cond = kCondLT;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001966 opposite = kCondGE;
Donghui Bai426b49c2016-11-08 14:55:38 +08001967 }
1968
1969 std::swap(left, right);
1970 FALLTHROUGH_INTENDED;
1971 case kCondGE:
1972 case kCondLT: {
1973 UseScratchRegisterScope temps(codegen->GetVIXLAssembler());
1974
1975 __ Cmp(LowRegisterFrom(left), LowRegisterFrom(right));
1976 __ Sbcs(temps.Acquire(), HighRegisterFrom(left), HighRegisterFrom(right));
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001977 ret = std::make_pair(ARMCondition(cond), ARMCondition(opposite));
Donghui Bai426b49c2016-11-08 14:55:38 +08001978 break;
1979 }
1980 default:
1981 LOG(FATAL) << "Unreachable";
1982 UNREACHABLE();
1983 }
1984
1985 return ret;
1986}
1987
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001988static std::pair<vixl32::Condition, vixl32::Condition> GenerateTest(HCondition* condition,
1989 bool invert,
1990 CodeGeneratorARMVIXL* codegen) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001991 const DataType::Type type = condition->GetLeft()->GetType();
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001992 IfCondition cond = condition->GetCondition();
1993 IfCondition opposite = condition->GetOppositeCondition();
1994 std::pair<vixl32::Condition, vixl32::Condition> ret(eq, ne);
Donghui Bai426b49c2016-11-08 14:55:38 +08001995
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001996 if (invert) {
1997 std::swap(cond, opposite);
1998 }
Donghui Bai426b49c2016-11-08 14:55:38 +08001999
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002000 if (type == DataType::Type::kInt64) {
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002001 ret = condition->GetLocations()->InAt(1).IsConstant()
2002 ? GenerateLongTestConstant(condition, invert, codegen)
2003 : GenerateLongTest(condition, invert, codegen);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002004 } else if (DataType::IsFloatingPointType(type)) {
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002005 GenerateVcmp(condition, codegen);
2006 __ Vmrs(RegisterOrAPSR_nzcv(kPcCode), FPSCR);
2007 ret = std::make_pair(ARMFPCondition(cond, condition->IsGtBias()),
2008 ARMFPCondition(opposite, condition->IsGtBias()));
Donghui Bai426b49c2016-11-08 14:55:38 +08002009 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002010 DCHECK(DataType::IsIntegralType(type) || type == DataType::Type::kReference) << type;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002011 __ Cmp(InputRegisterAt(condition, 0), InputOperandAt(condition, 1));
2012 ret = std::make_pair(ARMCondition(cond), ARMCondition(opposite));
Donghui Bai426b49c2016-11-08 14:55:38 +08002013 }
2014
2015 return ret;
2016}
2017
Anton Kirilov5601d4e2017-05-11 19:33:50 +01002018static void GenerateConditionGeneric(HCondition* cond, CodeGeneratorARMVIXL* codegen) {
Anton Kirilov5601d4e2017-05-11 19:33:50 +01002019 const vixl32::Register out = OutputRegister(cond);
2020 const auto condition = GenerateTest(cond, false, codegen);
2021
2022 __ Mov(LeaveFlags, out, 0);
2023
2024 if (out.IsLow()) {
2025 // We use the scope because of the IT block that follows.
2026 ExactAssemblyScope guard(codegen->GetVIXLAssembler(),
2027 2 * vixl32::k16BitT32InstructionSizeInBytes,
2028 CodeBufferCheckScope::kExactSize);
2029
2030 __ it(condition.first);
2031 __ mov(condition.first, out, 1);
2032 } else {
2033 vixl32::Label done_label;
2034 vixl32::Label* const final_label = codegen->GetFinalLabel(cond, &done_label);
2035
2036 __ B(condition.second, final_label, /* far_target */ false);
2037 __ Mov(out, 1);
2038
2039 if (done_label.IsReferenced()) {
2040 __ Bind(&done_label);
2041 }
2042 }
2043}
2044
2045static void GenerateEqualLong(HCondition* cond, CodeGeneratorARMVIXL* codegen) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002046 DCHECK_EQ(cond->GetLeft()->GetType(), DataType::Type::kInt64);
Anton Kirilov5601d4e2017-05-11 19:33:50 +01002047
2048 const LocationSummary* const locations = cond->GetLocations();
2049 IfCondition condition = cond->GetCondition();
2050 const vixl32::Register out = OutputRegister(cond);
2051 const Location left = locations->InAt(0);
2052 const Location right = locations->InAt(1);
2053 vixl32::Register left_high = HighRegisterFrom(left);
2054 vixl32::Register left_low = LowRegisterFrom(left);
2055 vixl32::Register temp;
2056 UseScratchRegisterScope temps(codegen->GetVIXLAssembler());
2057
2058 if (right.IsConstant()) {
2059 IfCondition opposite = cond->GetOppositeCondition();
2060 const int64_t value = AdjustConstantForCondition(Int64ConstantFrom(right),
2061 &condition,
2062 &opposite);
2063 Operand right_high = High32Bits(value);
2064 Operand right_low = Low32Bits(value);
2065
2066 // The output uses Location::kNoOutputOverlap.
2067 if (out.Is(left_high)) {
2068 std::swap(left_low, left_high);
2069 std::swap(right_low, right_high);
2070 }
2071
2072 __ Sub(out, left_low, right_low);
2073 temp = temps.Acquire();
2074 __ Sub(temp, left_high, right_high);
2075 } else {
2076 DCHECK(right.IsRegisterPair());
2077 temp = temps.Acquire();
2078 __ Sub(temp, left_high, HighRegisterFrom(right));
2079 __ Sub(out, left_low, LowRegisterFrom(right));
2080 }
2081
2082 // Need to check after calling AdjustConstantForCondition().
2083 DCHECK(condition == kCondEQ || condition == kCondNE) << condition;
2084
2085 if (condition == kCondNE && out.IsLow()) {
2086 __ Orrs(out, out, temp);
2087
2088 // We use the scope because of the IT block that follows.
2089 ExactAssemblyScope guard(codegen->GetVIXLAssembler(),
2090 2 * vixl32::k16BitT32InstructionSizeInBytes,
2091 CodeBufferCheckScope::kExactSize);
2092
2093 __ it(ne);
2094 __ mov(ne, out, 1);
2095 } else {
2096 __ Orr(out, out, temp);
2097 codegen->GenerateConditionWithZero(condition, out, out, temp);
2098 }
2099}
2100
Anton Kirilov5601d4e2017-05-11 19:33:50 +01002101static void GenerateConditionLong(HCondition* cond, CodeGeneratorARMVIXL* codegen) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002102 DCHECK_EQ(cond->GetLeft()->GetType(), DataType::Type::kInt64);
Anton Kirilov5601d4e2017-05-11 19:33:50 +01002103
2104 const LocationSummary* const locations = cond->GetLocations();
2105 IfCondition condition = cond->GetCondition();
2106 const vixl32::Register out = OutputRegister(cond);
2107 const Location left = locations->InAt(0);
2108 const Location right = locations->InAt(1);
2109
2110 if (right.IsConstant()) {
2111 IfCondition opposite = cond->GetOppositeCondition();
2112
2113 // Comparisons against 0 are common enough to deserve special attention.
2114 if (AdjustConstantForCondition(Int64ConstantFrom(right), &condition, &opposite) == 0) {
2115 switch (condition) {
2116 case kCondNE:
2117 case kCondA:
2118 if (out.IsLow()) {
2119 // We only care if both input registers are 0 or not.
2120 __ Orrs(out, LowRegisterFrom(left), HighRegisterFrom(left));
2121
2122 // We use the scope because of the IT block that follows.
2123 ExactAssemblyScope guard(codegen->GetVIXLAssembler(),
2124 2 * vixl32::k16BitT32InstructionSizeInBytes,
2125 CodeBufferCheckScope::kExactSize);
2126
2127 __ it(ne);
2128 __ mov(ne, out, 1);
2129 return;
2130 }
2131
2132 FALLTHROUGH_INTENDED;
2133 case kCondEQ:
2134 case kCondBE:
2135 // We only care if both input registers are 0 or not.
2136 __ Orr(out, LowRegisterFrom(left), HighRegisterFrom(left));
2137 codegen->GenerateConditionWithZero(condition, out, out);
2138 return;
2139 case kCondLT:
2140 case kCondGE:
2141 // We only care about the sign bit.
2142 FALLTHROUGH_INTENDED;
2143 case kCondAE:
2144 case kCondB:
2145 codegen->GenerateConditionWithZero(condition, out, HighRegisterFrom(left));
2146 return;
2147 case kCondLE:
2148 case kCondGT:
2149 default:
2150 break;
2151 }
2152 }
2153 }
2154
Anton Kirilov23b752b2017-07-20 14:40:44 +01002155 // If `out` is a low register, then the GenerateConditionGeneric()
2156 // function generates a shorter code sequence that is still branchless.
2157 if ((condition == kCondEQ || condition == kCondNE) && !out.IsLow()) {
Anton Kirilov5601d4e2017-05-11 19:33:50 +01002158 GenerateEqualLong(cond, codegen);
2159 return;
2160 }
2161
Anton Kirilov23b752b2017-07-20 14:40:44 +01002162 GenerateConditionGeneric(cond, codegen);
Anton Kirilov5601d4e2017-05-11 19:33:50 +01002163}
2164
Roland Levillain6d729a72017-06-30 18:34:01 +01002165static void GenerateConditionIntegralOrNonPrimitive(HCondition* cond,
2166 CodeGeneratorARMVIXL* codegen) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002167 const DataType::Type type = cond->GetLeft()->GetType();
Anton Kirilov5601d4e2017-05-11 19:33:50 +01002168
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002169 DCHECK(DataType::IsIntegralType(type) || type == DataType::Type::kReference) << type;
Anton Kirilov5601d4e2017-05-11 19:33:50 +01002170
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002171 if (type == DataType::Type::kInt64) {
Anton Kirilov5601d4e2017-05-11 19:33:50 +01002172 GenerateConditionLong(cond, codegen);
2173 return;
2174 }
2175
2176 IfCondition condition = cond->GetCondition();
2177 vixl32::Register in = InputRegisterAt(cond, 0);
2178 const vixl32::Register out = OutputRegister(cond);
2179 const Location right = cond->GetLocations()->InAt(1);
2180 int64_t value;
2181
2182 if (right.IsConstant()) {
2183 IfCondition opposite = cond->GetOppositeCondition();
2184
2185 value = AdjustConstantForCondition(Int64ConstantFrom(right), &condition, &opposite);
2186
2187 // Comparisons against 0 are common enough to deserve special attention.
2188 if (value == 0) {
2189 switch (condition) {
2190 case kCondNE:
2191 case kCondA:
2192 if (out.IsLow() && out.Is(in)) {
2193 __ Cmp(out, 0);
2194
2195 // We use the scope because of the IT block that follows.
2196 ExactAssemblyScope guard(codegen->GetVIXLAssembler(),
2197 2 * vixl32::k16BitT32InstructionSizeInBytes,
2198 CodeBufferCheckScope::kExactSize);
2199
2200 __ it(ne);
2201 __ mov(ne, out, 1);
2202 return;
2203 }
2204
2205 FALLTHROUGH_INTENDED;
2206 case kCondEQ:
2207 case kCondBE:
2208 case kCondLT:
2209 case kCondGE:
2210 case kCondAE:
2211 case kCondB:
2212 codegen->GenerateConditionWithZero(condition, out, in);
2213 return;
2214 case kCondLE:
2215 case kCondGT:
2216 default:
2217 break;
2218 }
2219 }
2220 }
2221
2222 if (condition == kCondEQ || condition == kCondNE) {
2223 Operand operand(0);
2224
2225 if (right.IsConstant()) {
2226 operand = Operand::From(value);
2227 } else if (out.Is(RegisterFrom(right))) {
2228 // Avoid 32-bit instructions if possible.
2229 operand = InputOperandAt(cond, 0);
2230 in = RegisterFrom(right);
2231 } else {
2232 operand = InputOperandAt(cond, 1);
2233 }
2234
2235 if (condition == kCondNE && out.IsLow()) {
2236 __ Subs(out, in, operand);
2237
2238 // We use the scope because of the IT block that follows.
2239 ExactAssemblyScope guard(codegen->GetVIXLAssembler(),
2240 2 * vixl32::k16BitT32InstructionSizeInBytes,
2241 CodeBufferCheckScope::kExactSize);
2242
2243 __ it(ne);
2244 __ mov(ne, out, 1);
2245 } else {
2246 __ Sub(out, in, operand);
2247 codegen->GenerateConditionWithZero(condition, out, out);
2248 }
2249
2250 return;
2251 }
2252
2253 GenerateConditionGeneric(cond, codegen);
2254}
2255
Donghui Bai426b49c2016-11-08 14:55:38 +08002256static bool CanEncodeConstantAs8BitImmediate(HConstant* constant) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002257 const DataType::Type type = constant->GetType();
Donghui Bai426b49c2016-11-08 14:55:38 +08002258 bool ret = false;
2259
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002260 DCHECK(DataType::IsIntegralType(type) || type == DataType::Type::kReference) << type;
Donghui Bai426b49c2016-11-08 14:55:38 +08002261
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002262 if (type == DataType::Type::kInt64) {
Donghui Bai426b49c2016-11-08 14:55:38 +08002263 const uint64_t value = Uint64ConstantFrom(constant);
2264
2265 ret = IsUint<8>(Low32Bits(value)) && IsUint<8>(High32Bits(value));
2266 } else {
2267 ret = IsUint<8>(Int32ConstantFrom(constant));
2268 }
2269
2270 return ret;
2271}
2272
2273static Location Arm8BitEncodableConstantOrRegister(HInstruction* constant) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002274 DCHECK(!DataType::IsFloatingPointType(constant->GetType()));
Donghui Bai426b49c2016-11-08 14:55:38 +08002275
2276 if (constant->IsConstant() && CanEncodeConstantAs8BitImmediate(constant->AsConstant())) {
2277 return Location::ConstantLocation(constant->AsConstant());
2278 }
2279
2280 return Location::RequiresRegister();
2281}
2282
2283static bool CanGenerateConditionalMove(const Location& out, const Location& src) {
2284 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
2285 // we check that we are not dealing with floating-point output (there is no
2286 // 16-bit VMOV encoding).
2287 if (!out.IsRegister() && !out.IsRegisterPair()) {
2288 return false;
2289 }
2290
2291 // For constants, we also check that the output is in one or two low registers,
2292 // and that the constants fit in an 8-bit unsigned integer, so that a 16-bit
2293 // MOV encoding can be used.
2294 if (src.IsConstant()) {
2295 if (!CanEncodeConstantAs8BitImmediate(src.GetConstant())) {
2296 return false;
2297 }
2298
2299 if (out.IsRegister()) {
2300 if (!RegisterFrom(out).IsLow()) {
2301 return false;
2302 }
2303 } else {
2304 DCHECK(out.IsRegisterPair());
2305
2306 if (!HighRegisterFrom(out).IsLow()) {
2307 return false;
2308 }
2309 }
2310 }
2311
2312 return true;
2313}
2314
Scott Wakelingfe885462016-09-22 10:24:38 +01002315#undef __
2316
Donghui Bai426b49c2016-11-08 14:55:38 +08002317vixl32::Label* CodeGeneratorARMVIXL::GetFinalLabel(HInstruction* instruction,
2318 vixl32::Label* final_label) {
2319 DCHECK(!instruction->IsControlFlow() && !instruction->IsSuspendCheck());
Anton Kirilov6f644202017-02-27 18:29:45 +00002320 DCHECK(!instruction->IsInvoke() || !instruction->GetLocations()->CanCall());
Donghui Bai426b49c2016-11-08 14:55:38 +08002321
2322 const HBasicBlock* const block = instruction->GetBlock();
2323 const HLoopInformation* const info = block->GetLoopInformation();
2324 HInstruction* const next = instruction->GetNext();
2325
2326 // Avoid a branch to a branch.
2327 if (next->IsGoto() && (info == nullptr ||
2328 !info->IsBackEdge(*block) ||
2329 !info->HasSuspendCheck())) {
2330 final_label = GetLabelOf(next->AsGoto()->GetSuccessor());
2331 }
2332
2333 return final_label;
2334}
2335
Scott Wakelingfe885462016-09-22 10:24:38 +01002336CodeGeneratorARMVIXL::CodeGeneratorARMVIXL(HGraph* graph,
2337 const ArmInstructionSetFeatures& isa_features,
2338 const CompilerOptions& compiler_options,
2339 OptimizingCompilerStats* stats)
2340 : CodeGenerator(graph,
2341 kNumberOfCoreRegisters,
2342 kNumberOfSRegisters,
2343 kNumberOfRegisterPairs,
2344 kCoreCalleeSaves.GetList(),
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002345 ComputeSRegisterListMask(kFpuCalleeSaves),
Scott Wakelingfe885462016-09-22 10:24:38 +01002346 compiler_options,
2347 stats),
Vladimir Markoca6fff82017-10-03 14:49:14 +01002348 block_labels_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
2349 jump_tables_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Scott Wakelingfe885462016-09-22 10:24:38 +01002350 location_builder_(graph, this),
2351 instruction_visitor_(graph, this),
Vladimir Markoca6fff82017-10-03 14:49:14 +01002352 move_resolver_(graph->GetAllocator(), this),
2353 assembler_(graph->GetAllocator()),
Artem Serovd4cc5b22016-11-04 11:19:09 +00002354 isa_features_(isa_features),
Artem Serovc5fcb442016-12-02 19:19:58 +00002355 uint32_literals_(std::less<uint32_t>(),
Vladimir Markoca6fff82017-10-03 14:49:14 +01002356 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
2357 pc_relative_method_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
2358 method_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
2359 pc_relative_type_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
2360 type_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
2361 pc_relative_string_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
2362 string_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
2363 baker_read_barrier_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Artem Serovc5fcb442016-12-02 19:19:58 +00002364 jit_string_patches_(StringReferenceValueComparator(),
Vladimir Markoca6fff82017-10-03 14:49:14 +01002365 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Artem Serovc5fcb442016-12-02 19:19:58 +00002366 jit_class_patches_(TypeReferenceValueComparator(),
Vladimir Markoca6fff82017-10-03 14:49:14 +01002367 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)) {
Scott Wakelingfe885462016-09-22 10:24:38 +01002368 // Always save the LR register to mimic Quick.
2369 AddAllocatedRegister(Location::RegisterLocation(LR));
Nicolas Geoffray13a797b2017-03-15 16:41:31 +00002370 // Give D30 and D31 as scratch register to VIXL. The register allocator only works on
2371 // S0-S31, which alias to D0-D15.
2372 GetVIXLAssembler()->GetScratchVRegisterList()->Combine(d31);
2373 GetVIXLAssembler()->GetScratchVRegisterList()->Combine(d30);
Scott Wakelingfe885462016-09-22 10:24:38 +01002374}
2375
Artem Serov551b28f2016-10-18 19:11:30 +01002376void JumpTableARMVIXL::EmitTable(CodeGeneratorARMVIXL* codegen) {
2377 uint32_t num_entries = switch_instr_->GetNumEntries();
2378 DCHECK_GE(num_entries, kPackedSwitchCompareJumpThreshold);
2379
2380 // We are about to use the assembler to place literals directly. Make sure we have enough
Scott Wakelingb77051e2016-11-21 19:46:00 +00002381 // underlying code buffer and we have generated a jump table of the right size, using
2382 // codegen->GetVIXLAssembler()->GetBuffer().Align();
Artem Serov0fb37192016-12-06 18:13:40 +00002383 ExactAssemblyScope aas(codegen->GetVIXLAssembler(),
2384 num_entries * sizeof(int32_t),
2385 CodeBufferCheckScope::kMaximumSize);
Artem Serov551b28f2016-10-18 19:11:30 +01002386 // TODO(VIXL): Check that using lower case bind is fine here.
2387 codegen->GetVIXLAssembler()->bind(&table_start_);
Artem Serov09a940d2016-11-11 16:15:11 +00002388 for (uint32_t i = 0; i < num_entries; i++) {
2389 codegen->GetVIXLAssembler()->place(bb_addresses_[i].get());
2390 }
2391}
2392
2393void JumpTableARMVIXL::FixTable(CodeGeneratorARMVIXL* codegen) {
2394 uint32_t num_entries = switch_instr_->GetNumEntries();
2395 DCHECK_GE(num_entries, kPackedSwitchCompareJumpThreshold);
2396
Artem Serov551b28f2016-10-18 19:11:30 +01002397 const ArenaVector<HBasicBlock*>& successors = switch_instr_->GetBlock()->GetSuccessors();
2398 for (uint32_t i = 0; i < num_entries; i++) {
2399 vixl32::Label* target_label = codegen->GetLabelOf(successors[i]);
2400 DCHECK(target_label->IsBound());
2401 int32_t jump_offset = target_label->GetLocation() - table_start_.GetLocation();
2402 // When doing BX to address we need to have lower bit set to 1 in T32.
2403 if (codegen->GetVIXLAssembler()->IsUsingT32()) {
2404 jump_offset++;
2405 }
2406 DCHECK_GT(jump_offset, std::numeric_limits<int32_t>::min());
2407 DCHECK_LE(jump_offset, std::numeric_limits<int32_t>::max());
Artem Serov09a940d2016-11-11 16:15:11 +00002408
Scott Wakelingb77051e2016-11-21 19:46:00 +00002409 bb_addresses_[i].get()->UpdateValue(jump_offset, codegen->GetVIXLAssembler()->GetBuffer());
Artem Serov551b28f2016-10-18 19:11:30 +01002410 }
2411}
2412
Artem Serov09a940d2016-11-11 16:15:11 +00002413void CodeGeneratorARMVIXL::FixJumpTables() {
Artem Serov551b28f2016-10-18 19:11:30 +01002414 for (auto&& jump_table : jump_tables_) {
Artem Serov09a940d2016-11-11 16:15:11 +00002415 jump_table->FixTable(this);
Artem Serov551b28f2016-10-18 19:11:30 +01002416 }
2417}
2418
Andreas Gampeca620d72016-11-08 08:09:33 -08002419#define __ reinterpret_cast<ArmVIXLAssembler*>(GetAssembler())->GetVIXLAssembler()-> // NOLINT
Scott Wakelingfe885462016-09-22 10:24:38 +01002420
2421void CodeGeneratorARMVIXL::Finalize(CodeAllocator* allocator) {
Artem Serov09a940d2016-11-11 16:15:11 +00002422 FixJumpTables();
Scott Wakelingfe885462016-09-22 10:24:38 +01002423 GetAssembler()->FinalizeCode();
2424 CodeGenerator::Finalize(allocator);
2425}
2426
2427void CodeGeneratorARMVIXL::SetupBlockedRegisters() const {
Scott Wakelingfe885462016-09-22 10:24:38 +01002428 // Stack register, LR and PC are always reserved.
2429 blocked_core_registers_[SP] = true;
2430 blocked_core_registers_[LR] = true;
2431 blocked_core_registers_[PC] = true;
2432
Roland Levillain6d729a72017-06-30 18:34:01 +01002433 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2434 // Reserve marking register.
2435 blocked_core_registers_[MR] = true;
2436 }
2437
Scott Wakelingfe885462016-09-22 10:24:38 +01002438 // Reserve thread register.
2439 blocked_core_registers_[TR] = true;
2440
2441 // Reserve temp register.
2442 blocked_core_registers_[IP] = true;
2443
2444 if (GetGraph()->IsDebuggable()) {
2445 // Stubs do not save callee-save floating point registers. If the graph
2446 // is debuggable, we need to deal with these registers differently. For
2447 // now, just block them.
2448 for (uint32_t i = kFpuCalleeSaves.GetFirstSRegister().GetCode();
2449 i <= kFpuCalleeSaves.GetLastSRegister().GetCode();
2450 ++i) {
2451 blocked_fpu_registers_[i] = true;
2452 }
2453 }
Scott Wakelingfe885462016-09-22 10:24:38 +01002454}
2455
Scott Wakelingfe885462016-09-22 10:24:38 +01002456InstructionCodeGeneratorARMVIXL::InstructionCodeGeneratorARMVIXL(HGraph* graph,
2457 CodeGeneratorARMVIXL* codegen)
2458 : InstructionCodeGenerator(graph, codegen),
2459 assembler_(codegen->GetAssembler()),
2460 codegen_(codegen) {}
2461
2462void CodeGeneratorARMVIXL::ComputeSpillMask() {
2463 core_spill_mask_ = allocated_registers_.GetCoreRegisters() & core_callee_save_mask_;
2464 DCHECK_NE(core_spill_mask_, 0u) << "At least the return address register must be saved";
2465 // There is no easy instruction to restore just the PC on thumb2. We spill and
2466 // restore another arbitrary register.
2467 core_spill_mask_ |= (1 << kCoreAlwaysSpillRegister.GetCode());
2468 fpu_spill_mask_ = allocated_registers_.GetFloatingPointRegisters() & fpu_callee_save_mask_;
2469 // We use vpush and vpop for saving and restoring floating point registers, which take
2470 // a SRegister and the number of registers to save/restore after that SRegister. We
2471 // therefore update the `fpu_spill_mask_` to also contain those registers not allocated,
2472 // but in the range.
2473 if (fpu_spill_mask_ != 0) {
2474 uint32_t least_significant_bit = LeastSignificantBit(fpu_spill_mask_);
2475 uint32_t most_significant_bit = MostSignificantBit(fpu_spill_mask_);
2476 for (uint32_t i = least_significant_bit + 1 ; i < most_significant_bit; ++i) {
2477 fpu_spill_mask_ |= (1 << i);
2478 }
2479 }
2480}
2481
2482void CodeGeneratorARMVIXL::GenerateFrameEntry() {
2483 bool skip_overflow_check =
2484 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kArm);
2485 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
2486 __ Bind(&frame_entry_label_);
2487
2488 if (HasEmptyFrame()) {
2489 return;
2490 }
2491
Scott Wakelingfe885462016-09-22 10:24:38 +01002492 if (!skip_overflow_check) {
Vladimir Marko4eb45632018-01-23 14:26:01 +00002493 // Using r4 instead of IP saves 2 bytes. Start by asserting that r4 is available here.
2494 for (vixl32::Register reg : kParameterCoreRegistersVIXL) {
2495 DCHECK(!reg.Is(r4));
2496 }
2497 DCHECK(!kCoreCalleeSaves.Includes(r4));
2498 vixl32::Register temp = r4;
Vladimir Marko33bff252017-11-01 14:35:42 +00002499 __ Sub(temp, sp, Operand::From(GetStackOverflowReservedBytes(InstructionSet::kArm)));
Scott Wakelingfe885462016-09-22 10:24:38 +01002500 // The load must immediately precede RecordPcInfo.
Artem Serov0fb37192016-12-06 18:13:40 +00002501 ExactAssemblyScope aas(GetVIXLAssembler(),
2502 vixl32::kMaxInstructionSizeInBytes,
2503 CodeBufferCheckScope::kMaximumSize);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002504 __ ldr(temp, MemOperand(temp));
2505 RecordPcInfo(nullptr, 0);
Scott Wakelingfe885462016-09-22 10:24:38 +01002506 }
2507
2508 __ Push(RegisterList(core_spill_mask_));
2509 GetAssembler()->cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(core_spill_mask_));
2510 GetAssembler()->cfi().RelOffsetForMany(DWARFReg(kMethodRegister),
2511 0,
2512 core_spill_mask_,
2513 kArmWordSize);
2514 if (fpu_spill_mask_ != 0) {
2515 uint32_t first = LeastSignificantBit(fpu_spill_mask_);
2516
2517 // Check that list is contiguous.
2518 DCHECK_EQ(fpu_spill_mask_ >> CTZ(fpu_spill_mask_), ~0u >> (32 - POPCOUNT(fpu_spill_mask_)));
2519
2520 __ Vpush(SRegisterList(vixl32::SRegister(first), POPCOUNT(fpu_spill_mask_)));
2521 GetAssembler()->cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(fpu_spill_mask_));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002522 GetAssembler()->cfi().RelOffsetForMany(DWARFReg(s0), 0, fpu_spill_mask_, kArmWordSize);
Scott Wakelingfe885462016-09-22 10:24:38 +01002523 }
Scott Wakelingbffdc702016-12-07 17:46:03 +00002524
Scott Wakelingfe885462016-09-22 10:24:38 +01002525 int adjust = GetFrameSize() - FrameEntrySpillSize();
2526 __ Sub(sp, sp, adjust);
2527 GetAssembler()->cfi().AdjustCFAOffset(adjust);
Scott Wakelingbffdc702016-12-07 17:46:03 +00002528
2529 // Save the current method if we need it. Note that we do not
2530 // do this in HCurrentMethod, as the instruction might have been removed
2531 // in the SSA graph.
2532 if (RequiresCurrentMethod()) {
2533 GetAssembler()->StoreToOffset(kStoreWord, kMethodRegister, sp, 0);
2534 }
Nicolas Geoffrayf7893532017-06-15 12:34:36 +01002535
2536 if (GetGraph()->HasShouldDeoptimizeFlag()) {
2537 UseScratchRegisterScope temps(GetVIXLAssembler());
2538 vixl32::Register temp = temps.Acquire();
2539 // Initialize should_deoptimize flag to 0.
2540 __ Mov(temp, 0);
2541 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, GetStackOffsetOfShouldDeoptimizeFlag());
2542 }
Roland Levillain5daa4952017-07-03 17:23:56 +01002543
2544 MaybeGenerateMarkingRegisterCheck(/* code */ 1);
Scott Wakelingfe885462016-09-22 10:24:38 +01002545}
2546
2547void CodeGeneratorARMVIXL::GenerateFrameExit() {
2548 if (HasEmptyFrame()) {
2549 __ Bx(lr);
2550 return;
2551 }
2552 GetAssembler()->cfi().RememberState();
2553 int adjust = GetFrameSize() - FrameEntrySpillSize();
2554 __ Add(sp, sp, adjust);
2555 GetAssembler()->cfi().AdjustCFAOffset(-adjust);
2556 if (fpu_spill_mask_ != 0) {
2557 uint32_t first = LeastSignificantBit(fpu_spill_mask_);
2558
2559 // Check that list is contiguous.
2560 DCHECK_EQ(fpu_spill_mask_ >> CTZ(fpu_spill_mask_), ~0u >> (32 - POPCOUNT(fpu_spill_mask_)));
2561
2562 __ Vpop(SRegisterList(vixl32::SRegister(first), POPCOUNT(fpu_spill_mask_)));
2563 GetAssembler()->cfi().AdjustCFAOffset(
2564 -static_cast<int>(kArmWordSize) * POPCOUNT(fpu_spill_mask_));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002565 GetAssembler()->cfi().RestoreMany(DWARFReg(vixl32::SRegister(0)), fpu_spill_mask_);
Scott Wakelingfe885462016-09-22 10:24:38 +01002566 }
2567 // Pop LR into PC to return.
2568 DCHECK_NE(core_spill_mask_ & (1 << kLrCode), 0U);
2569 uint32_t pop_mask = (core_spill_mask_ & (~(1 << kLrCode))) | 1 << kPcCode;
2570 __ Pop(RegisterList(pop_mask));
2571 GetAssembler()->cfi().RestoreState();
2572 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
2573}
2574
2575void CodeGeneratorARMVIXL::Bind(HBasicBlock* block) {
2576 __ Bind(GetLabelOf(block));
2577}
2578
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002579Location InvokeDexCallingConventionVisitorARMVIXL::GetNextLocation(DataType::Type type) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002580 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002581 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002582 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002583 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002584 case DataType::Type::kInt8:
2585 case DataType::Type::kUint16:
2586 case DataType::Type::kInt16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002587 case DataType::Type::kInt32: {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002588 uint32_t index = gp_index_++;
2589 uint32_t stack_index = stack_index_++;
2590 if (index < calling_convention.GetNumberOfRegisters()) {
2591 return LocationFrom(calling_convention.GetRegisterAt(index));
2592 } else {
2593 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
2594 }
2595 }
2596
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002597 case DataType::Type::kInt64: {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002598 uint32_t index = gp_index_;
2599 uint32_t stack_index = stack_index_;
2600 gp_index_ += 2;
2601 stack_index_ += 2;
2602 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
2603 if (calling_convention.GetRegisterAt(index).Is(r1)) {
2604 // Skip R1, and use R2_R3 instead.
2605 gp_index_++;
2606 index++;
2607 }
2608 }
2609 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
2610 DCHECK_EQ(calling_convention.GetRegisterAt(index).GetCode() + 1,
2611 calling_convention.GetRegisterAt(index + 1).GetCode());
2612
2613 return LocationFrom(calling_convention.GetRegisterAt(index),
2614 calling_convention.GetRegisterAt(index + 1));
2615 } else {
2616 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
2617 }
2618 }
2619
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002620 case DataType::Type::kFloat32: {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002621 uint32_t stack_index = stack_index_++;
2622 if (float_index_ % 2 == 0) {
2623 float_index_ = std::max(double_index_, float_index_);
2624 }
2625 if (float_index_ < calling_convention.GetNumberOfFpuRegisters()) {
2626 return LocationFrom(calling_convention.GetFpuRegisterAt(float_index_++));
2627 } else {
2628 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
2629 }
2630 }
2631
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002632 case DataType::Type::kFloat64: {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002633 double_index_ = std::max(double_index_, RoundUp(float_index_, 2));
2634 uint32_t stack_index = stack_index_;
2635 stack_index_ += 2;
2636 if (double_index_ + 1 < calling_convention.GetNumberOfFpuRegisters()) {
2637 uint32_t index = double_index_;
2638 double_index_ += 2;
2639 Location result = LocationFrom(
2640 calling_convention.GetFpuRegisterAt(index),
2641 calling_convention.GetFpuRegisterAt(index + 1));
2642 DCHECK(ExpectedPairLayout(result));
2643 return result;
2644 } else {
2645 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
2646 }
2647 }
2648
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002649 case DataType::Type::kVoid:
Artem Serovd4cc5b22016-11-04 11:19:09 +00002650 LOG(FATAL) << "Unexpected parameter type " << type;
2651 break;
2652 }
2653 return Location::NoLocation();
2654}
2655
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002656Location InvokeDexCallingConventionVisitorARMVIXL::GetReturnLocation(DataType::Type type) const {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002657 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002658 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002659 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002660 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002661 case DataType::Type::kInt8:
2662 case DataType::Type::kUint16:
2663 case DataType::Type::kInt16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002664 case DataType::Type::kInt32: {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002665 return LocationFrom(r0);
2666 }
2667
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002668 case DataType::Type::kFloat32: {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002669 return LocationFrom(s0);
2670 }
2671
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002672 case DataType::Type::kInt64: {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002673 return LocationFrom(r0, r1);
2674 }
2675
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002676 case DataType::Type::kFloat64: {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002677 return LocationFrom(s0, s1);
2678 }
2679
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002680 case DataType::Type::kVoid:
Artem Serovd4cc5b22016-11-04 11:19:09 +00002681 return Location::NoLocation();
2682 }
2683
2684 UNREACHABLE();
2685}
2686
2687Location InvokeDexCallingConventionVisitorARMVIXL::GetMethodLocation() const {
2688 return LocationFrom(kMethodRegister);
2689}
2690
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002691void CodeGeneratorARMVIXL::Move32(Location destination, Location source) {
2692 if (source.Equals(destination)) {
2693 return;
2694 }
2695 if (destination.IsRegister()) {
2696 if (source.IsRegister()) {
2697 __ Mov(RegisterFrom(destination), RegisterFrom(source));
2698 } else if (source.IsFpuRegister()) {
2699 __ Vmov(RegisterFrom(destination), SRegisterFrom(source));
2700 } else {
2701 GetAssembler()->LoadFromOffset(kLoadWord,
2702 RegisterFrom(destination),
2703 sp,
2704 source.GetStackIndex());
2705 }
2706 } else if (destination.IsFpuRegister()) {
2707 if (source.IsRegister()) {
2708 __ Vmov(SRegisterFrom(destination), RegisterFrom(source));
2709 } else if (source.IsFpuRegister()) {
2710 __ Vmov(SRegisterFrom(destination), SRegisterFrom(source));
2711 } else {
2712 GetAssembler()->LoadSFromOffset(SRegisterFrom(destination), sp, source.GetStackIndex());
2713 }
2714 } else {
2715 DCHECK(destination.IsStackSlot()) << destination;
2716 if (source.IsRegister()) {
2717 GetAssembler()->StoreToOffset(kStoreWord,
2718 RegisterFrom(source),
2719 sp,
2720 destination.GetStackIndex());
2721 } else if (source.IsFpuRegister()) {
2722 GetAssembler()->StoreSToOffset(SRegisterFrom(source), sp, destination.GetStackIndex());
2723 } else {
2724 DCHECK(source.IsStackSlot()) << source;
2725 UseScratchRegisterScope temps(GetVIXLAssembler());
2726 vixl32::Register temp = temps.Acquire();
2727 GetAssembler()->LoadFromOffset(kLoadWord, temp, sp, source.GetStackIndex());
2728 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
2729 }
2730 }
2731}
2732
Artem Serovcfbe9132016-10-14 15:58:56 +01002733void CodeGeneratorARMVIXL::MoveConstant(Location location, int32_t value) {
2734 DCHECK(location.IsRegister());
2735 __ Mov(RegisterFrom(location), value);
Scott Wakelingfe885462016-09-22 10:24:38 +01002736}
2737
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002738void CodeGeneratorARMVIXL::MoveLocation(Location dst, Location src, DataType::Type dst_type) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002739 // TODO(VIXL): Maybe refactor to have the 'move' implementation here and use it in
2740 // `ParallelMoveResolverARMVIXL::EmitMove`, as is done in the `arm64` backend.
Vladimir Markoca6fff82017-10-03 14:49:14 +01002741 HParallelMove move(GetGraph()->GetAllocator());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002742 move.AddMove(src, dst, dst_type, nullptr);
2743 GetMoveResolver()->EmitNativeCode(&move);
Scott Wakelingfe885462016-09-22 10:24:38 +01002744}
2745
Artem Serovcfbe9132016-10-14 15:58:56 +01002746void CodeGeneratorARMVIXL::AddLocationAsTemp(Location location, LocationSummary* locations) {
2747 if (location.IsRegister()) {
2748 locations->AddTemp(location);
2749 } else if (location.IsRegisterPair()) {
2750 locations->AddTemp(LocationFrom(LowRegisterFrom(location)));
2751 locations->AddTemp(LocationFrom(HighRegisterFrom(location)));
2752 } else {
2753 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
2754 }
Scott Wakelingfe885462016-09-22 10:24:38 +01002755}
2756
2757void CodeGeneratorARMVIXL::InvokeRuntime(QuickEntrypointEnum entrypoint,
2758 HInstruction* instruction,
2759 uint32_t dex_pc,
2760 SlowPathCode* slow_path) {
2761 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Alexandre Rames374ddf32016-11-04 10:40:49 +00002762 __ Ldr(lr, MemOperand(tr, GetThreadOffset<kArmPointerSize>(entrypoint).Int32Value()));
2763 // Ensure the pc position is recorded immediately after the `blx` instruction.
2764 // 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 +00002765 ExactAssemblyScope aas(GetVIXLAssembler(),
2766 vixl32::k16BitT32InstructionSizeInBytes,
2767 CodeBufferCheckScope::kExactSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00002768 __ blx(lr);
Scott Wakelingfe885462016-09-22 10:24:38 +01002769 if (EntrypointRequiresStackMap(entrypoint)) {
2770 RecordPcInfo(instruction, dex_pc, slow_path);
2771 }
2772}
2773
2774void CodeGeneratorARMVIXL::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
2775 HInstruction* instruction,
2776 SlowPathCode* slow_path) {
2777 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Alexandre Rames374ddf32016-11-04 10:40:49 +00002778 __ Ldr(lr, MemOperand(tr, entry_point_offset));
Scott Wakelingfe885462016-09-22 10:24:38 +01002779 __ Blx(lr);
2780}
2781
Scott Wakelingfe885462016-09-22 10:24:38 +01002782void InstructionCodeGeneratorARMVIXL::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Aart Bika8b8e9b2018-01-09 11:01:02 -08002783 if (successor->IsExitBlock()) {
2784 DCHECK(got->GetPrevious()->AlwaysThrows());
2785 return; // no code needed
2786 }
2787
Scott Wakelingfe885462016-09-22 10:24:38 +01002788 HBasicBlock* block = got->GetBlock();
2789 HInstruction* previous = got->GetPrevious();
2790 HLoopInformation* info = block->GetLoopInformation();
2791
2792 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Scott Wakelingfe885462016-09-22 10:24:38 +01002793 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2794 return;
2795 }
2796 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2797 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
Roland Levillain5daa4952017-07-03 17:23:56 +01002798 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 2);
Scott Wakelingfe885462016-09-22 10:24:38 +01002799 }
2800 if (!codegen_->GoesToNextBlock(block, successor)) {
2801 __ B(codegen_->GetLabelOf(successor));
2802 }
2803}
2804
2805void LocationsBuilderARMVIXL::VisitGoto(HGoto* got) {
2806 got->SetLocations(nullptr);
2807}
2808
2809void InstructionCodeGeneratorARMVIXL::VisitGoto(HGoto* got) {
2810 HandleGoto(got, got->GetSuccessor());
2811}
2812
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002813void LocationsBuilderARMVIXL::VisitTryBoundary(HTryBoundary* try_boundary) {
2814 try_boundary->SetLocations(nullptr);
2815}
2816
2817void InstructionCodeGeneratorARMVIXL::VisitTryBoundary(HTryBoundary* try_boundary) {
2818 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2819 if (!successor->IsExitBlock()) {
2820 HandleGoto(try_boundary, successor);
2821 }
2822}
2823
Scott Wakelingfe885462016-09-22 10:24:38 +01002824void LocationsBuilderARMVIXL::VisitExit(HExit* exit) {
2825 exit->SetLocations(nullptr);
2826}
2827
2828void InstructionCodeGeneratorARMVIXL::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
2829}
2830
Scott Wakelingfe885462016-09-22 10:24:38 +01002831void InstructionCodeGeneratorARMVIXL::GenerateCompareTestAndBranch(HCondition* condition,
Anton Kirilov23b752b2017-07-20 14:40:44 +01002832 vixl32::Label* true_target,
2833 vixl32::Label* false_target,
Anton Kirilovfd522532017-05-10 12:46:57 +01002834 bool is_far_target) {
Anton Kirilov23b752b2017-07-20 14:40:44 +01002835 if (true_target == false_target) {
2836 DCHECK(true_target != nullptr);
2837 __ B(true_target);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002838 return;
2839 }
2840
Anton Kirilov23b752b2017-07-20 14:40:44 +01002841 vixl32::Label* non_fallthrough_target;
2842 bool invert;
2843 bool emit_both_branches;
Scott Wakelingfe885462016-09-22 10:24:38 +01002844
Anton Kirilov23b752b2017-07-20 14:40:44 +01002845 if (true_target == nullptr) {
2846 // The true target is fallthrough.
2847 DCHECK(false_target != nullptr);
2848 non_fallthrough_target = false_target;
2849 invert = true;
2850 emit_both_branches = false;
2851 } else {
2852 non_fallthrough_target = true_target;
2853 invert = false;
2854 // Either the false target is fallthrough, or there is no fallthrough
2855 // and both branches must be emitted.
2856 emit_both_branches = (false_target != nullptr);
Scott Wakelingfe885462016-09-22 10:24:38 +01002857 }
2858
Anton Kirilov23b752b2017-07-20 14:40:44 +01002859 const auto cond = GenerateTest(condition, invert, codegen_);
2860
2861 __ B(cond.first, non_fallthrough_target, is_far_target);
2862
2863 if (emit_both_branches) {
2864 // No target falls through, we need to branch.
2865 __ B(false_target);
Scott Wakelingfe885462016-09-22 10:24:38 +01002866 }
2867}
2868
2869void InstructionCodeGeneratorARMVIXL::GenerateTestAndBranch(HInstruction* instruction,
2870 size_t condition_input_index,
2871 vixl32::Label* true_target,
xueliang.zhongf51bc622016-11-04 09:23:32 +00002872 vixl32::Label* false_target,
2873 bool far_target) {
Scott Wakelingfe885462016-09-22 10:24:38 +01002874 HInstruction* cond = instruction->InputAt(condition_input_index);
2875
2876 if (true_target == nullptr && false_target == nullptr) {
2877 // Nothing to do. The code always falls through.
2878 return;
2879 } else if (cond->IsIntConstant()) {
2880 // Constant condition, statically compared against "true" (integer value 1).
2881 if (cond->AsIntConstant()->IsTrue()) {
2882 if (true_target != nullptr) {
2883 __ B(true_target);
2884 }
2885 } else {
Anton Kirilov644032c2016-12-06 17:51:43 +00002886 DCHECK(cond->AsIntConstant()->IsFalse()) << Int32ConstantFrom(cond);
Scott Wakelingfe885462016-09-22 10:24:38 +01002887 if (false_target != nullptr) {
2888 __ B(false_target);
2889 }
2890 }
2891 return;
2892 }
2893
2894 // The following code generates these patterns:
2895 // (1) true_target == nullptr && false_target != nullptr
2896 // - opposite condition true => branch to false_target
2897 // (2) true_target != nullptr && false_target == nullptr
2898 // - condition true => branch to true_target
2899 // (3) true_target != nullptr && false_target != nullptr
2900 // - condition true => branch to true_target
2901 // - branch to false_target
2902 if (IsBooleanValueOrMaterializedCondition(cond)) {
2903 // Condition has been materialized, compare the output to 0.
2904 if (kIsDebugBuild) {
2905 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
2906 DCHECK(cond_val.IsRegister());
2907 }
2908 if (true_target == nullptr) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00002909 __ CompareAndBranchIfZero(InputRegisterAt(instruction, condition_input_index),
2910 false_target,
2911 far_target);
Scott Wakelingfe885462016-09-22 10:24:38 +01002912 } else {
xueliang.zhongf51bc622016-11-04 09:23:32 +00002913 __ CompareAndBranchIfNonZero(InputRegisterAt(instruction, condition_input_index),
2914 true_target,
2915 far_target);
Scott Wakelingfe885462016-09-22 10:24:38 +01002916 }
2917 } else {
2918 // Condition has not been materialized. Use its inputs as the comparison and
2919 // its condition as the branch condition.
2920 HCondition* condition = cond->AsCondition();
2921
2922 // If this is a long or FP comparison that has been folded into
2923 // the HCondition, generate the comparison directly.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002924 DataType::Type type = condition->InputAt(0)->GetType();
2925 if (type == DataType::Type::kInt64 || DataType::IsFloatingPointType(type)) {
Anton Kirilovfd522532017-05-10 12:46:57 +01002926 GenerateCompareTestAndBranch(condition, true_target, false_target, far_target);
Scott Wakelingfe885462016-09-22 10:24:38 +01002927 return;
2928 }
2929
Donghui Bai426b49c2016-11-08 14:55:38 +08002930 vixl32::Label* non_fallthrough_target;
2931 vixl32::Condition arm_cond = vixl32::Condition::None();
2932 const vixl32::Register left = InputRegisterAt(cond, 0);
2933 const Operand right = InputOperandAt(cond, 1);
2934
Scott Wakelingfe885462016-09-22 10:24:38 +01002935 if (true_target == nullptr) {
Donghui Bai426b49c2016-11-08 14:55:38 +08002936 arm_cond = ARMCondition(condition->GetOppositeCondition());
2937 non_fallthrough_target = false_target;
Scott Wakelingfe885462016-09-22 10:24:38 +01002938 } else {
Donghui Bai426b49c2016-11-08 14:55:38 +08002939 arm_cond = ARMCondition(condition->GetCondition());
2940 non_fallthrough_target = true_target;
2941 }
2942
2943 if (right.IsImmediate() && right.GetImmediate() == 0 && (arm_cond.Is(ne) || arm_cond.Is(eq))) {
2944 if (arm_cond.Is(eq)) {
Anton Kirilovfd522532017-05-10 12:46:57 +01002945 __ CompareAndBranchIfZero(left, non_fallthrough_target, far_target);
Donghui Bai426b49c2016-11-08 14:55:38 +08002946 } else {
2947 DCHECK(arm_cond.Is(ne));
Anton Kirilovfd522532017-05-10 12:46:57 +01002948 __ CompareAndBranchIfNonZero(left, non_fallthrough_target, far_target);
Donghui Bai426b49c2016-11-08 14:55:38 +08002949 }
2950 } else {
2951 __ Cmp(left, right);
Anton Kirilovfd522532017-05-10 12:46:57 +01002952 __ B(arm_cond, non_fallthrough_target, far_target);
Scott Wakelingfe885462016-09-22 10:24:38 +01002953 }
2954 }
2955
2956 // If neither branch falls through (case 3), the conditional branch to `true_target`
2957 // was already emitted (case 2) and we need to emit a jump to `false_target`.
2958 if (true_target != nullptr && false_target != nullptr) {
2959 __ B(false_target);
2960 }
2961}
2962
2963void LocationsBuilderARMVIXL::VisitIf(HIf* if_instr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002964 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(if_instr);
Scott Wakelingfe885462016-09-22 10:24:38 +01002965 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
2966 locations->SetInAt(0, Location::RequiresRegister());
2967 }
2968}
2969
2970void InstructionCodeGeneratorARMVIXL::VisitIf(HIf* if_instr) {
2971 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
2972 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002973 vixl32::Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
2974 nullptr : codegen_->GetLabelOf(true_successor);
2975 vixl32::Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
2976 nullptr : codegen_->GetLabelOf(false_successor);
Scott Wakelingfe885462016-09-22 10:24:38 +01002977 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
2978}
2979
Scott Wakelingc34dba72016-10-03 10:14:44 +01002980void LocationsBuilderARMVIXL::VisitDeoptimize(HDeoptimize* deoptimize) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002981 LocationSummary* locations = new (GetGraph()->GetAllocator())
Scott Wakelingc34dba72016-10-03 10:14:44 +01002982 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01002983 InvokeRuntimeCallingConventionARMVIXL calling_convention;
2984 RegisterSet caller_saves = RegisterSet::Empty();
2985 caller_saves.Add(LocationFrom(calling_convention.GetRegisterAt(0)));
2986 locations->SetCustomSlowPathCallerSaves(caller_saves);
Scott Wakelingc34dba72016-10-03 10:14:44 +01002987 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
2988 locations->SetInAt(0, Location::RequiresRegister());
2989 }
2990}
2991
2992void InstructionCodeGeneratorARMVIXL::VisitDeoptimize(HDeoptimize* deoptimize) {
2993 SlowPathCodeARMVIXL* slow_path =
2994 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARMVIXL>(deoptimize);
2995 GenerateTestAndBranch(deoptimize,
2996 /* condition_input_index */ 0,
2997 slow_path->GetEntryLabel(),
2998 /* false_target */ nullptr);
2999}
3000
Artem Serovd4cc5b22016-11-04 11:19:09 +00003001void LocationsBuilderARMVIXL::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003002 LocationSummary* locations = new (GetGraph()->GetAllocator())
Artem Serovd4cc5b22016-11-04 11:19:09 +00003003 LocationSummary(flag, LocationSummary::kNoCall);
3004 locations->SetOut(Location::RequiresRegister());
3005}
3006
3007void InstructionCodeGeneratorARMVIXL::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
3008 GetAssembler()->LoadFromOffset(kLoadWord,
3009 OutputRegister(flag),
3010 sp,
3011 codegen_->GetStackOffsetOfShouldDeoptimizeFlag());
3012}
3013
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003014void LocationsBuilderARMVIXL::VisitSelect(HSelect* select) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003015 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(select);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003016 const bool is_floating_point = DataType::IsFloatingPointType(select->GetType());
Donghui Bai426b49c2016-11-08 14:55:38 +08003017
3018 if (is_floating_point) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003019 locations->SetInAt(0, Location::RequiresFpuRegister());
Donghui Bai426b49c2016-11-08 14:55:38 +08003020 locations->SetInAt(1, Location::FpuRegisterOrConstant(select->GetTrueValue()));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003021 } else {
3022 locations->SetInAt(0, Location::RequiresRegister());
Donghui Bai426b49c2016-11-08 14:55:38 +08003023 locations->SetInAt(1, Arm8BitEncodableConstantOrRegister(select->GetTrueValue()));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003024 }
Donghui Bai426b49c2016-11-08 14:55:38 +08003025
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003026 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
Donghui Bai426b49c2016-11-08 14:55:38 +08003027 locations->SetInAt(2, Location::RegisterOrConstant(select->GetCondition()));
3028 // The code generator handles overlap with the values, but not with the condition.
3029 locations->SetOut(Location::SameAsFirstInput());
3030 } else if (is_floating_point) {
3031 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3032 } else {
3033 if (!locations->InAt(1).IsConstant()) {
3034 locations->SetInAt(0, Arm8BitEncodableConstantOrRegister(select->GetFalseValue()));
3035 }
3036
3037 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003038 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003039}
3040
3041void InstructionCodeGeneratorARMVIXL::VisitSelect(HSelect* select) {
Donghui Bai426b49c2016-11-08 14:55:38 +08003042 HInstruction* const condition = select->GetCondition();
3043 const LocationSummary* const locations = select->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003044 const DataType::Type type = select->GetType();
Donghui Bai426b49c2016-11-08 14:55:38 +08003045 const Location first = locations->InAt(0);
3046 const Location out = locations->Out();
3047 const Location second = locations->InAt(1);
3048 Location src;
3049
3050 if (condition->IsIntConstant()) {
3051 if (condition->AsIntConstant()->IsFalse()) {
3052 src = first;
3053 } else {
3054 src = second;
3055 }
3056
3057 codegen_->MoveLocation(out, src, type);
3058 return;
3059 }
3060
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003061 if (!DataType::IsFloatingPointType(type)) {
Donghui Bai426b49c2016-11-08 14:55:38 +08003062 bool invert = false;
3063
3064 if (out.Equals(second)) {
3065 src = first;
3066 invert = true;
3067 } else if (out.Equals(first)) {
3068 src = second;
3069 } else if (second.IsConstant()) {
3070 DCHECK(CanEncodeConstantAs8BitImmediate(second.GetConstant()));
3071 src = second;
3072 } else if (first.IsConstant()) {
3073 DCHECK(CanEncodeConstantAs8BitImmediate(first.GetConstant()));
3074 src = first;
3075 invert = true;
3076 } else {
3077 src = second;
3078 }
3079
3080 if (CanGenerateConditionalMove(out, src)) {
3081 if (!out.Equals(first) && !out.Equals(second)) {
3082 codegen_->MoveLocation(out, src.Equals(first) ? second : first, type);
3083 }
3084
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003085 std::pair<vixl32::Condition, vixl32::Condition> cond(eq, ne);
3086
3087 if (IsBooleanValueOrMaterializedCondition(condition)) {
3088 __ Cmp(InputRegisterAt(select, 2), 0);
3089 cond = invert ? std::make_pair(eq, ne) : std::make_pair(ne, eq);
3090 } else {
3091 cond = GenerateTest(condition->AsCondition(), invert, codegen_);
3092 }
3093
Donghui Bai426b49c2016-11-08 14:55:38 +08003094 const size_t instr_count = out.IsRegisterPair() ? 4 : 2;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003095 // We use the scope because of the IT block that follows.
Donghui Bai426b49c2016-11-08 14:55:38 +08003096 ExactAssemblyScope guard(GetVIXLAssembler(),
3097 instr_count * vixl32::k16BitT32InstructionSizeInBytes,
3098 CodeBufferCheckScope::kExactSize);
3099
3100 if (out.IsRegister()) {
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003101 __ it(cond.first);
3102 __ mov(cond.first, RegisterFrom(out), OperandFrom(src, type));
Donghui Bai426b49c2016-11-08 14:55:38 +08003103 } else {
3104 DCHECK(out.IsRegisterPair());
3105
3106 Operand operand_high(0);
3107 Operand operand_low(0);
3108
3109 if (src.IsConstant()) {
3110 const int64_t value = Int64ConstantFrom(src);
3111
3112 operand_high = High32Bits(value);
3113 operand_low = Low32Bits(value);
3114 } else {
3115 DCHECK(src.IsRegisterPair());
3116 operand_high = HighRegisterFrom(src);
3117 operand_low = LowRegisterFrom(src);
3118 }
3119
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003120 __ it(cond.first);
3121 __ mov(cond.first, LowRegisterFrom(out), operand_low);
3122 __ it(cond.first);
3123 __ mov(cond.first, HighRegisterFrom(out), operand_high);
Donghui Bai426b49c2016-11-08 14:55:38 +08003124 }
3125
3126 return;
3127 }
3128 }
3129
3130 vixl32::Label* false_target = nullptr;
3131 vixl32::Label* true_target = nullptr;
3132 vixl32::Label select_end;
3133 vixl32::Label* const target = codegen_->GetFinalLabel(select, &select_end);
3134
3135 if (out.Equals(second)) {
3136 true_target = target;
3137 src = first;
3138 } else {
3139 false_target = target;
3140 src = second;
3141
3142 if (!out.Equals(first)) {
3143 codegen_->MoveLocation(out, first, type);
3144 }
3145 }
3146
3147 GenerateTestAndBranch(select, 2, true_target, false_target, /* far_target */ false);
3148 codegen_->MoveLocation(out, src, type);
3149
3150 if (select_end.IsReferenced()) {
3151 __ Bind(&select_end);
3152 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003153}
3154
Artem Serov551b28f2016-10-18 19:11:30 +01003155void LocationsBuilderARMVIXL::VisitNativeDebugInfo(HNativeDebugInfo* info) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003156 new (GetGraph()->GetAllocator()) LocationSummary(info);
Artem Serov551b28f2016-10-18 19:11:30 +01003157}
3158
3159void InstructionCodeGeneratorARMVIXL::VisitNativeDebugInfo(HNativeDebugInfo*) {
3160 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
3161}
3162
Scott Wakelingfe885462016-09-22 10:24:38 +01003163void CodeGeneratorARMVIXL::GenerateNop() {
3164 __ Nop();
3165}
3166
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003167// `temp` is an extra temporary register that is used for some conditions;
3168// callers may not specify it, in which case the method will use a scratch
3169// register instead.
3170void CodeGeneratorARMVIXL::GenerateConditionWithZero(IfCondition condition,
3171 vixl32::Register out,
3172 vixl32::Register in,
3173 vixl32::Register temp) {
3174 switch (condition) {
3175 case kCondEQ:
3176 // x <= 0 iff x == 0 when the comparison is unsigned.
3177 case kCondBE:
3178 if (!temp.IsValid() || (out.IsLow() && !out.Is(in))) {
3179 temp = out;
3180 }
3181
3182 // Avoid 32-bit instructions if possible; note that `in` and `temp` must be
3183 // different as well.
3184 if (in.IsLow() && temp.IsLow() && !in.Is(temp)) {
3185 // temp = - in; only 0 sets the carry flag.
3186 __ Rsbs(temp, in, 0);
3187
3188 if (out.Is(in)) {
3189 std::swap(in, temp);
3190 }
3191
3192 // out = - in + in + carry = carry
3193 __ Adc(out, temp, in);
3194 } else {
3195 // If `in` is 0, then it has 32 leading zeros, and less than that otherwise.
3196 __ Clz(out, in);
3197 // Any number less than 32 logically shifted right by 5 bits results in 0;
3198 // the same operation on 32 yields 1.
3199 __ Lsr(out, out, 5);
3200 }
3201
3202 break;
3203 case kCondNE:
3204 // x > 0 iff x != 0 when the comparison is unsigned.
3205 case kCondA: {
3206 UseScratchRegisterScope temps(GetVIXLAssembler());
3207
3208 if (out.Is(in)) {
3209 if (!temp.IsValid() || in.Is(temp)) {
3210 temp = temps.Acquire();
3211 }
3212 } else if (!temp.IsValid() || !temp.IsLow()) {
3213 temp = out;
3214 }
3215
3216 // temp = in - 1; only 0 does not set the carry flag.
3217 __ Subs(temp, in, 1);
3218 // out = in + ~temp + carry = in + (-(in - 1) - 1) + carry = in - in + 1 - 1 + carry = carry
3219 __ Sbc(out, in, temp);
3220 break;
3221 }
3222 case kCondGE:
3223 __ Mvn(out, in);
3224 in = out;
3225 FALLTHROUGH_INTENDED;
3226 case kCondLT:
3227 // We only care about the sign bit.
3228 __ Lsr(out, in, 31);
3229 break;
3230 case kCondAE:
3231 // Trivially true.
3232 __ Mov(out, 1);
3233 break;
3234 case kCondB:
3235 // Trivially false.
3236 __ Mov(out, 0);
3237 break;
3238 default:
3239 LOG(FATAL) << "Unexpected condition " << condition;
3240 UNREACHABLE();
3241 }
3242}
3243
Scott Wakelingfe885462016-09-22 10:24:38 +01003244void LocationsBuilderARMVIXL::HandleCondition(HCondition* cond) {
3245 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003246 new (GetGraph()->GetAllocator()) LocationSummary(cond, LocationSummary::kNoCall);
Scott Wakelingfe885462016-09-22 10:24:38 +01003247 // Handle the long/FP comparisons made in instruction simplification.
3248 switch (cond->InputAt(0)->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003249 case DataType::Type::kInt64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003250 locations->SetInAt(0, Location::RequiresRegister());
3251 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
3252 if (!cond->IsEmittedAtUseSite()) {
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003253 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Scott Wakelingfe885462016-09-22 10:24:38 +01003254 }
3255 break;
3256
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003257 case DataType::Type::kFloat32:
3258 case DataType::Type::kFloat64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003259 locations->SetInAt(0, Location::RequiresFpuRegister());
Artem Serov657022c2016-11-23 14:19:38 +00003260 locations->SetInAt(1, ArithmeticZeroOrFpuRegister(cond->InputAt(1)));
Scott Wakelingfe885462016-09-22 10:24:38 +01003261 if (!cond->IsEmittedAtUseSite()) {
3262 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3263 }
3264 break;
3265
3266 default:
3267 locations->SetInAt(0, Location::RequiresRegister());
3268 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
3269 if (!cond->IsEmittedAtUseSite()) {
3270 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3271 }
3272 }
3273}
3274
3275void InstructionCodeGeneratorARMVIXL::HandleCondition(HCondition* cond) {
3276 if (cond->IsEmittedAtUseSite()) {
3277 return;
3278 }
3279
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003280 const DataType::Type type = cond->GetLeft()->GetType();
Scott Wakelingfe885462016-09-22 10:24:38 +01003281
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003282 if (DataType::IsFloatingPointType(type)) {
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003283 GenerateConditionGeneric(cond, codegen_);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003284 return;
Scott Wakelingfe885462016-09-22 10:24:38 +01003285 }
3286
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003287 DCHECK(DataType::IsIntegralType(type) || type == DataType::Type::kReference) << type;
Scott Wakelingfe885462016-09-22 10:24:38 +01003288
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003289 const IfCondition condition = cond->GetCondition();
Scott Wakelingfe885462016-09-22 10:24:38 +01003290
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003291 // A condition with only one boolean input, or two boolean inputs without being equality or
3292 // inequality results from transformations done by the instruction simplifier, and is handled
3293 // as a regular condition with integral inputs.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003294 if (type == DataType::Type::kBool &&
3295 cond->GetRight()->GetType() == DataType::Type::kBool &&
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003296 (condition == kCondEQ || condition == kCondNE)) {
3297 vixl32::Register left = InputRegisterAt(cond, 0);
3298 const vixl32::Register out = OutputRegister(cond);
3299 const Location right_loc = cond->GetLocations()->InAt(1);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003300
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003301 // The constant case is handled by the instruction simplifier.
3302 DCHECK(!right_loc.IsConstant());
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003303
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003304 vixl32::Register right = RegisterFrom(right_loc);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003305
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003306 // Avoid 32-bit instructions if possible.
3307 if (out.Is(right)) {
3308 std::swap(left, right);
3309 }
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003310
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003311 __ Eor(out, left, right);
3312
3313 if (condition == kCondEQ) {
3314 __ Eor(out, out, 1);
3315 }
3316
3317 return;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003318 }
Anton Kirilov6f644202017-02-27 18:29:45 +00003319
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003320 GenerateConditionIntegralOrNonPrimitive(cond, codegen_);
Scott Wakelingfe885462016-09-22 10:24:38 +01003321}
3322
3323void LocationsBuilderARMVIXL::VisitEqual(HEqual* comp) {
3324 HandleCondition(comp);
3325}
3326
3327void InstructionCodeGeneratorARMVIXL::VisitEqual(HEqual* comp) {
3328 HandleCondition(comp);
3329}
3330
3331void LocationsBuilderARMVIXL::VisitNotEqual(HNotEqual* comp) {
3332 HandleCondition(comp);
3333}
3334
3335void InstructionCodeGeneratorARMVIXL::VisitNotEqual(HNotEqual* comp) {
3336 HandleCondition(comp);
3337}
3338
3339void LocationsBuilderARMVIXL::VisitLessThan(HLessThan* comp) {
3340 HandleCondition(comp);
3341}
3342
3343void InstructionCodeGeneratorARMVIXL::VisitLessThan(HLessThan* comp) {
3344 HandleCondition(comp);
3345}
3346
3347void LocationsBuilderARMVIXL::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
3348 HandleCondition(comp);
3349}
3350
3351void InstructionCodeGeneratorARMVIXL::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
3352 HandleCondition(comp);
3353}
3354
3355void LocationsBuilderARMVIXL::VisitGreaterThan(HGreaterThan* comp) {
3356 HandleCondition(comp);
3357}
3358
3359void InstructionCodeGeneratorARMVIXL::VisitGreaterThan(HGreaterThan* comp) {
3360 HandleCondition(comp);
3361}
3362
3363void LocationsBuilderARMVIXL::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
3364 HandleCondition(comp);
3365}
3366
3367void InstructionCodeGeneratorARMVIXL::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
3368 HandleCondition(comp);
3369}
3370
3371void LocationsBuilderARMVIXL::VisitBelow(HBelow* comp) {
3372 HandleCondition(comp);
3373}
3374
3375void InstructionCodeGeneratorARMVIXL::VisitBelow(HBelow* comp) {
3376 HandleCondition(comp);
3377}
3378
3379void LocationsBuilderARMVIXL::VisitBelowOrEqual(HBelowOrEqual* comp) {
3380 HandleCondition(comp);
3381}
3382
3383void InstructionCodeGeneratorARMVIXL::VisitBelowOrEqual(HBelowOrEqual* comp) {
3384 HandleCondition(comp);
3385}
3386
3387void LocationsBuilderARMVIXL::VisitAbove(HAbove* comp) {
3388 HandleCondition(comp);
3389}
3390
3391void InstructionCodeGeneratorARMVIXL::VisitAbove(HAbove* comp) {
3392 HandleCondition(comp);
3393}
3394
3395void LocationsBuilderARMVIXL::VisitAboveOrEqual(HAboveOrEqual* comp) {
3396 HandleCondition(comp);
3397}
3398
3399void InstructionCodeGeneratorARMVIXL::VisitAboveOrEqual(HAboveOrEqual* comp) {
3400 HandleCondition(comp);
3401}
3402
3403void LocationsBuilderARMVIXL::VisitIntConstant(HIntConstant* constant) {
3404 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003405 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Scott Wakelingfe885462016-09-22 10:24:38 +01003406 locations->SetOut(Location::ConstantLocation(constant));
3407}
3408
3409void InstructionCodeGeneratorARMVIXL::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
3410 // Will be generated at use site.
3411}
3412
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003413void LocationsBuilderARMVIXL::VisitNullConstant(HNullConstant* constant) {
3414 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003415 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003416 locations->SetOut(Location::ConstantLocation(constant));
3417}
3418
3419void InstructionCodeGeneratorARMVIXL::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
3420 // Will be generated at use site.
3421}
3422
Scott Wakelingfe885462016-09-22 10:24:38 +01003423void LocationsBuilderARMVIXL::VisitLongConstant(HLongConstant* constant) {
3424 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003425 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Scott Wakelingfe885462016-09-22 10:24:38 +01003426 locations->SetOut(Location::ConstantLocation(constant));
3427}
3428
3429void InstructionCodeGeneratorARMVIXL::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
3430 // Will be generated at use site.
3431}
3432
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01003433void LocationsBuilderARMVIXL::VisitFloatConstant(HFloatConstant* constant) {
3434 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003435 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01003436 locations->SetOut(Location::ConstantLocation(constant));
3437}
3438
Scott Wakelingc34dba72016-10-03 10:14:44 +01003439void InstructionCodeGeneratorARMVIXL::VisitFloatConstant(
3440 HFloatConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01003441 // Will be generated at use site.
3442}
3443
3444void LocationsBuilderARMVIXL::VisitDoubleConstant(HDoubleConstant* constant) {
3445 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003446 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01003447 locations->SetOut(Location::ConstantLocation(constant));
3448}
3449
Scott Wakelingc34dba72016-10-03 10:14:44 +01003450void InstructionCodeGeneratorARMVIXL::VisitDoubleConstant(
3451 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01003452 // Will be generated at use site.
3453}
3454
Igor Murashkind01745e2017-04-05 16:40:31 -07003455void LocationsBuilderARMVIXL::VisitConstructorFence(HConstructorFence* constructor_fence) {
3456 constructor_fence->SetLocations(nullptr);
3457}
3458
3459void InstructionCodeGeneratorARMVIXL::VisitConstructorFence(
3460 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
3461 codegen_->GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
3462}
3463
Scott Wakelingfe885462016-09-22 10:24:38 +01003464void LocationsBuilderARMVIXL::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3465 memory_barrier->SetLocations(nullptr);
3466}
3467
3468void InstructionCodeGeneratorARMVIXL::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3469 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
3470}
3471
3472void LocationsBuilderARMVIXL::VisitReturnVoid(HReturnVoid* ret) {
3473 ret->SetLocations(nullptr);
3474}
3475
3476void InstructionCodeGeneratorARMVIXL::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
3477 codegen_->GenerateFrameExit();
3478}
3479
3480void LocationsBuilderARMVIXL::VisitReturn(HReturn* ret) {
3481 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003482 new (GetGraph()->GetAllocator()) LocationSummary(ret, LocationSummary::kNoCall);
Scott Wakelingfe885462016-09-22 10:24:38 +01003483 locations->SetInAt(0, parameter_visitor_.GetReturnLocation(ret->InputAt(0)->GetType()));
3484}
3485
3486void InstructionCodeGeneratorARMVIXL::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
3487 codegen_->GenerateFrameExit();
3488}
3489
Artem Serovcfbe9132016-10-14 15:58:56 +01003490void LocationsBuilderARMVIXL::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3491 // The trampoline uses the same calling convention as dex calling conventions,
3492 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
3493 // the method_idx.
3494 HandleInvoke(invoke);
3495}
3496
3497void InstructionCodeGeneratorARMVIXL::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3498 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
Roland Levillain5daa4952017-07-03 17:23:56 +01003499 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 3);
Artem Serovcfbe9132016-10-14 15:58:56 +01003500}
3501
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003502void LocationsBuilderARMVIXL::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
3503 // Explicit clinit checks triggered by static invokes must have been pruned by
3504 // art::PrepareForRegisterAllocation.
3505 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
3506
Anton Kirilov5ec62182016-10-13 20:16:02 +01003507 IntrinsicLocationsBuilderARMVIXL intrinsic(codegen_);
3508 if (intrinsic.TryDispatch(invoke)) {
Anton Kirilov5ec62182016-10-13 20:16:02 +01003509 return;
3510 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003511
3512 HandleInvoke(invoke);
3513}
3514
Anton Kirilov5ec62182016-10-13 20:16:02 +01003515static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARMVIXL* codegen) {
3516 if (invoke->GetLocations()->Intrinsified()) {
3517 IntrinsicCodeGeneratorARMVIXL intrinsic(codegen);
3518 intrinsic.Dispatch(invoke);
3519 return true;
3520 }
3521 return false;
3522}
3523
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003524void InstructionCodeGeneratorARMVIXL::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
3525 // Explicit clinit checks triggered by static invokes must have been pruned by
3526 // art::PrepareForRegisterAllocation.
3527 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
3528
Anton Kirilov5ec62182016-10-13 20:16:02 +01003529 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
Roland Levillain5daa4952017-07-03 17:23:56 +01003530 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 4);
Anton Kirilov5ec62182016-10-13 20:16:02 +01003531 return;
3532 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003533
3534 LocationSummary* locations = invoke->GetLocations();
Artem Serovd4cc5b22016-11-04 11:19:09 +00003535 codegen_->GenerateStaticOrDirectCall(
3536 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Roland Levillain5daa4952017-07-03 17:23:56 +01003537
3538 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 5);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003539}
3540
3541void LocationsBuilderARMVIXL::HandleInvoke(HInvoke* invoke) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00003542 InvokeDexCallingConventionVisitorARMVIXL calling_convention_visitor;
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003543 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
3544}
3545
3546void LocationsBuilderARMVIXL::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Anton Kirilov5ec62182016-10-13 20:16:02 +01003547 IntrinsicLocationsBuilderARMVIXL intrinsic(codegen_);
3548 if (intrinsic.TryDispatch(invoke)) {
3549 return;
3550 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003551
3552 HandleInvoke(invoke);
3553}
3554
3555void InstructionCodeGeneratorARMVIXL::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Anton Kirilov5ec62182016-10-13 20:16:02 +01003556 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
Roland Levillain5daa4952017-07-03 17:23:56 +01003557 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 6);
Anton Kirilov5ec62182016-10-13 20:16:02 +01003558 return;
3559 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003560
3561 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexandre Rames374ddf32016-11-04 10:40:49 +00003562 DCHECK(!codegen_->IsLeafMethod());
Roland Levillain5daa4952017-07-03 17:23:56 +01003563
3564 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 7);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003565}
3566
Artem Serovcfbe9132016-10-14 15:58:56 +01003567void LocationsBuilderARMVIXL::VisitInvokeInterface(HInvokeInterface* invoke) {
3568 HandleInvoke(invoke);
3569 // Add the hidden argument.
3570 invoke->GetLocations()->AddTemp(LocationFrom(r12));
3571}
3572
3573void InstructionCodeGeneratorARMVIXL::VisitInvokeInterface(HInvokeInterface* invoke) {
3574 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
3575 LocationSummary* locations = invoke->GetLocations();
3576 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
3577 vixl32::Register hidden_reg = RegisterFrom(locations->GetTemp(1));
3578 Location receiver = locations->InAt(0);
3579 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3580
3581 DCHECK(!receiver.IsStackSlot());
3582
Alexandre Rames374ddf32016-11-04 10:40:49 +00003583 // Ensure the pc position is recorded immediately after the `ldr` instruction.
3584 {
Artem Serov0fb37192016-12-06 18:13:40 +00003585 ExactAssemblyScope aas(GetVIXLAssembler(),
3586 vixl32::kMaxInstructionSizeInBytes,
3587 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00003588 // /* HeapReference<Class> */ temp = receiver->klass_
3589 __ ldr(temp, MemOperand(RegisterFrom(receiver), class_offset));
3590 codegen_->MaybeRecordImplicitNullCheck(invoke);
3591 }
Artem Serovcfbe9132016-10-14 15:58:56 +01003592 // Instead of simply (possibly) unpoisoning `temp` here, we should
3593 // emit a read barrier for the previous class reference load.
3594 // However this is not required in practice, as this is an
3595 // intermediate/temporary reference and because the current
3596 // concurrent copying collector keeps the from-space memory
3597 // intact/accessible until the end of the marking phase (the
3598 // concurrent copying collector may not in the future).
3599 GetAssembler()->MaybeUnpoisonHeapReference(temp);
3600 GetAssembler()->LoadFromOffset(kLoadWord,
3601 temp,
3602 temp,
3603 mirror::Class::ImtPtrOffset(kArmPointerSize).Uint32Value());
3604 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
3605 invoke->GetImtIndex(), kArmPointerSize));
3606 // temp = temp->GetImtEntryAt(method_offset);
3607 GetAssembler()->LoadFromOffset(kLoadWord, temp, temp, method_offset);
3608 uint32_t entry_point =
3609 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize).Int32Value();
3610 // LR = temp->GetEntryPoint();
3611 GetAssembler()->LoadFromOffset(kLoadWord, lr, temp, entry_point);
3612
3613 // Set the hidden (in r12) argument. It is done here, right before a BLX to prevent other
3614 // instruction from clobbering it as they might use r12 as a scratch register.
3615 DCHECK(hidden_reg.Is(r12));
Scott Wakelingb77051e2016-11-21 19:46:00 +00003616
3617 {
3618 // The VIXL macro assembler may clobber any of the scratch registers that are available to it,
3619 // so it checks if the application is using them (by passing them to the macro assembler
3620 // methods). The following application of UseScratchRegisterScope corrects VIXL's notion of
3621 // what is available, and is the opposite of the standard usage: Instead of requesting a
3622 // temporary location, it imposes an external constraint (i.e. a specific register is reserved
3623 // for the hidden argument). Note that this works even if VIXL needs a scratch register itself
3624 // (to materialize the constant), since the destination register becomes available for such use
3625 // internally for the duration of the macro instruction.
3626 UseScratchRegisterScope temps(GetVIXLAssembler());
3627 temps.Exclude(hidden_reg);
3628 __ Mov(hidden_reg, invoke->GetDexMethodIndex());
3629 }
Artem Serovcfbe9132016-10-14 15:58:56 +01003630 {
Alexandre Rames374ddf32016-11-04 10:40:49 +00003631 // Ensure the pc position is recorded immediately after the `blx` instruction.
3632 // 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 +00003633 ExactAssemblyScope aas(GetVIXLAssembler(),
Alexandre Rames374ddf32016-11-04 10:40:49 +00003634 vixl32::k16BitT32InstructionSizeInBytes,
3635 CodeBufferCheckScope::kExactSize);
Artem Serovcfbe9132016-10-14 15:58:56 +01003636 // LR();
3637 __ blx(lr);
Artem Serovcfbe9132016-10-14 15:58:56 +01003638 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Alexandre Rames374ddf32016-11-04 10:40:49 +00003639 DCHECK(!codegen_->IsLeafMethod());
Artem Serovcfbe9132016-10-14 15:58:56 +01003640 }
Roland Levillain5daa4952017-07-03 17:23:56 +01003641
3642 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 8);
Artem Serovcfbe9132016-10-14 15:58:56 +01003643}
3644
Orion Hodsonac141392017-01-13 11:53:47 +00003645void LocationsBuilderARMVIXL::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
3646 HandleInvoke(invoke);
3647}
3648
3649void InstructionCodeGeneratorARMVIXL::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
3650 codegen_->GenerateInvokePolymorphicCall(invoke);
Roland Levillain5daa4952017-07-03 17:23:56 +01003651 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 9);
Orion Hodsonac141392017-01-13 11:53:47 +00003652}
3653
Artem Serov02109dd2016-09-23 17:17:54 +01003654void LocationsBuilderARMVIXL::VisitNeg(HNeg* neg) {
3655 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003656 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Artem Serov02109dd2016-09-23 17:17:54 +01003657 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003658 case DataType::Type::kInt32: {
Artem Serov02109dd2016-09-23 17:17:54 +01003659 locations->SetInAt(0, Location::RequiresRegister());
3660 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3661 break;
3662 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003663 case DataType::Type::kInt64: {
Artem Serov02109dd2016-09-23 17:17:54 +01003664 locations->SetInAt(0, Location::RequiresRegister());
3665 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3666 break;
3667 }
3668
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003669 case DataType::Type::kFloat32:
3670 case DataType::Type::kFloat64:
Artem Serov02109dd2016-09-23 17:17:54 +01003671 locations->SetInAt(0, Location::RequiresFpuRegister());
3672 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3673 break;
3674
3675 default:
3676 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
3677 }
3678}
3679
3680void InstructionCodeGeneratorARMVIXL::VisitNeg(HNeg* neg) {
3681 LocationSummary* locations = neg->GetLocations();
3682 Location out = locations->Out();
3683 Location in = locations->InAt(0);
3684 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003685 case DataType::Type::kInt32:
Artem Serov02109dd2016-09-23 17:17:54 +01003686 __ Rsb(OutputRegister(neg), InputRegisterAt(neg, 0), 0);
3687 break;
3688
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003689 case DataType::Type::kInt64:
Artem Serov02109dd2016-09-23 17:17:54 +01003690 // out.lo = 0 - in.lo (and update the carry/borrow (C) flag)
3691 __ Rsbs(LowRegisterFrom(out), LowRegisterFrom(in), 0);
3692 // We cannot emit an RSC (Reverse Subtract with Carry)
3693 // instruction here, as it does not exist in the Thumb-2
3694 // instruction set. We use the following approach
3695 // using SBC and SUB instead.
3696 //
3697 // out.hi = -C
3698 __ Sbc(HighRegisterFrom(out), HighRegisterFrom(out), HighRegisterFrom(out));
3699 // out.hi = out.hi - in.hi
3700 __ Sub(HighRegisterFrom(out), HighRegisterFrom(out), HighRegisterFrom(in));
3701 break;
3702
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003703 case DataType::Type::kFloat32:
3704 case DataType::Type::kFloat64:
Anton Kirilov644032c2016-12-06 17:51:43 +00003705 __ Vneg(OutputVRegister(neg), InputVRegister(neg));
Artem Serov02109dd2016-09-23 17:17:54 +01003706 break;
3707
3708 default:
3709 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
3710 }
3711}
3712
Scott Wakelingfe885462016-09-22 10:24:38 +01003713void LocationsBuilderARMVIXL::VisitTypeConversion(HTypeConversion* conversion) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003714 DataType::Type result_type = conversion->GetResultType();
3715 DataType::Type input_type = conversion->GetInputType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003716 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
3717 << input_type << " -> " << result_type;
Scott Wakelingfe885462016-09-22 10:24:38 +01003718
3719 // The float-to-long, double-to-long and long-to-float type conversions
3720 // rely on a call to the runtime.
3721 LocationSummary::CallKind call_kind =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003722 (((input_type == DataType::Type::kFloat32 || input_type == DataType::Type::kFloat64)
3723 && result_type == DataType::Type::kInt64)
3724 || (input_type == DataType::Type::kInt64 && result_type == DataType::Type::kFloat32))
Scott Wakelingfe885462016-09-22 10:24:38 +01003725 ? LocationSummary::kCallOnMainOnly
3726 : LocationSummary::kNoCall;
3727 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003728 new (GetGraph()->GetAllocator()) LocationSummary(conversion, call_kind);
Scott Wakelingfe885462016-09-22 10:24:38 +01003729
Scott Wakelingfe885462016-09-22 10:24:38 +01003730 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003731 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003732 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003733 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003734 case DataType::Type::kInt16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003735 DCHECK(DataType::IsIntegralType(input_type)) << input_type;
3736 locations->SetInAt(0, Location::RequiresRegister());
3737 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Scott Wakelingfe885462016-09-22 10:24:38 +01003738 break;
3739
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003740 case DataType::Type::kInt32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003741 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003742 case DataType::Type::kInt64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003743 locations->SetInAt(0, Location::Any());
3744 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3745 break;
3746
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003747 case DataType::Type::kFloat32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003748 locations->SetInAt(0, Location::RequiresFpuRegister());
3749 locations->SetOut(Location::RequiresRegister());
3750 locations->AddTemp(Location::RequiresFpuRegister());
3751 break;
3752
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003753 case DataType::Type::kFloat64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003754 locations->SetInAt(0, Location::RequiresFpuRegister());
3755 locations->SetOut(Location::RequiresRegister());
3756 locations->AddTemp(Location::RequiresFpuRegister());
3757 break;
3758
3759 default:
3760 LOG(FATAL) << "Unexpected type conversion from " << input_type
3761 << " to " << result_type;
3762 }
3763 break;
3764
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003765 case DataType::Type::kInt64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003766 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003767 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003768 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003769 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003770 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003771 case DataType::Type::kInt16:
3772 case DataType::Type::kInt32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003773 locations->SetInAt(0, Location::RequiresRegister());
3774 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3775 break;
3776
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003777 case DataType::Type::kFloat32: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003778 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3779 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
3780 locations->SetOut(LocationFrom(r0, r1));
Scott Wakelingfe885462016-09-22 10:24:38 +01003781 break;
3782 }
3783
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003784 case DataType::Type::kFloat64: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003785 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3786 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0),
3787 calling_convention.GetFpuRegisterAt(1)));
3788 locations->SetOut(LocationFrom(r0, r1));
Scott Wakelingfe885462016-09-22 10:24:38 +01003789 break;
3790 }
3791
3792 default:
3793 LOG(FATAL) << "Unexpected type conversion from " << input_type
3794 << " to " << result_type;
3795 }
3796 break;
3797
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003798 case DataType::Type::kFloat32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003799 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003800 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003801 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003802 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003803 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003804 case DataType::Type::kInt16:
3805 case DataType::Type::kInt32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003806 locations->SetInAt(0, Location::RequiresRegister());
3807 locations->SetOut(Location::RequiresFpuRegister());
3808 break;
3809
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003810 case DataType::Type::kInt64: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003811 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3812 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0),
3813 calling_convention.GetRegisterAt(1)));
3814 locations->SetOut(LocationFrom(calling_convention.GetFpuRegisterAt(0)));
Scott Wakelingfe885462016-09-22 10:24:38 +01003815 break;
3816 }
3817
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003818 case DataType::Type::kFloat64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003819 locations->SetInAt(0, Location::RequiresFpuRegister());
3820 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3821 break;
3822
3823 default:
3824 LOG(FATAL) << "Unexpected type conversion from " << input_type
3825 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08003826 }
Scott Wakelingfe885462016-09-22 10:24:38 +01003827 break;
3828
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003829 case DataType::Type::kFloat64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003830 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003831 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003832 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003833 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003834 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003835 case DataType::Type::kInt16:
3836 case DataType::Type::kInt32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003837 locations->SetInAt(0, Location::RequiresRegister());
3838 locations->SetOut(Location::RequiresFpuRegister());
3839 break;
3840
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003841 case DataType::Type::kInt64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003842 locations->SetInAt(0, Location::RequiresRegister());
3843 locations->SetOut(Location::RequiresFpuRegister());
3844 locations->AddTemp(Location::RequiresFpuRegister());
3845 locations->AddTemp(Location::RequiresFpuRegister());
3846 break;
3847
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003848 case DataType::Type::kFloat32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003849 locations->SetInAt(0, Location::RequiresFpuRegister());
3850 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3851 break;
3852
3853 default:
3854 LOG(FATAL) << "Unexpected type conversion from " << input_type
3855 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08003856 }
Scott Wakelingfe885462016-09-22 10:24:38 +01003857 break;
3858
3859 default:
3860 LOG(FATAL) << "Unexpected type conversion from " << input_type
3861 << " to " << result_type;
3862 }
3863}
3864
3865void InstructionCodeGeneratorARMVIXL::VisitTypeConversion(HTypeConversion* conversion) {
3866 LocationSummary* locations = conversion->GetLocations();
3867 Location out = locations->Out();
3868 Location in = locations->InAt(0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003869 DataType::Type result_type = conversion->GetResultType();
3870 DataType::Type input_type = conversion->GetInputType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003871 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
3872 << input_type << " -> " << result_type;
Scott Wakelingfe885462016-09-22 10:24:38 +01003873 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003874 case DataType::Type::kUint8:
Scott Wakelingfe885462016-09-22 10:24:38 +01003875 switch (input_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003876 case DataType::Type::kInt8:
3877 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003878 case DataType::Type::kInt16:
3879 case DataType::Type::kInt32:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003880 __ Ubfx(OutputRegister(conversion), InputRegisterAt(conversion, 0), 0, 8);
3881 break;
3882 case DataType::Type::kInt64:
3883 __ Ubfx(OutputRegister(conversion), LowRegisterFrom(in), 0, 8);
3884 break;
3885
3886 default:
3887 LOG(FATAL) << "Unexpected type conversion from " << input_type
3888 << " to " << result_type;
3889 }
3890 break;
3891
3892 case DataType::Type::kInt8:
3893 switch (input_type) {
3894 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003895 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003896 case DataType::Type::kInt16:
3897 case DataType::Type::kInt32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003898 __ Sbfx(OutputRegister(conversion), InputRegisterAt(conversion, 0), 0, 8);
3899 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003900 case DataType::Type::kInt64:
3901 __ Sbfx(OutputRegister(conversion), LowRegisterFrom(in), 0, 8);
3902 break;
3903
3904 default:
3905 LOG(FATAL) << "Unexpected type conversion from " << input_type
3906 << " to " << result_type;
3907 }
3908 break;
3909
3910 case DataType::Type::kUint16:
3911 switch (input_type) {
3912 case DataType::Type::kInt8:
3913 case DataType::Type::kInt16:
3914 case DataType::Type::kInt32:
3915 __ Ubfx(OutputRegister(conversion), InputRegisterAt(conversion, 0), 0, 16);
3916 break;
3917 case DataType::Type::kInt64:
3918 __ Ubfx(OutputRegister(conversion), LowRegisterFrom(in), 0, 16);
3919 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01003920
3921 default:
3922 LOG(FATAL) << "Unexpected type conversion from " << input_type
3923 << " to " << result_type;
3924 }
3925 break;
3926
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003927 case DataType::Type::kInt16:
Scott Wakelingfe885462016-09-22 10:24:38 +01003928 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003929 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003930 case DataType::Type::kInt32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003931 __ Sbfx(OutputRegister(conversion), InputRegisterAt(conversion, 0), 0, 16);
3932 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003933 case DataType::Type::kInt64:
3934 __ Sbfx(OutputRegister(conversion), LowRegisterFrom(in), 0, 16);
3935 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01003936
3937 default:
3938 LOG(FATAL) << "Unexpected type conversion from " << input_type
3939 << " to " << result_type;
3940 }
3941 break;
3942
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003943 case DataType::Type::kInt32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003944 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003945 case DataType::Type::kInt64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003946 DCHECK(out.IsRegister());
3947 if (in.IsRegisterPair()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003948 __ Mov(OutputRegister(conversion), LowRegisterFrom(in));
Scott Wakelingfe885462016-09-22 10:24:38 +01003949 } else if (in.IsDoubleStackSlot()) {
3950 GetAssembler()->LoadFromOffset(kLoadWord,
3951 OutputRegister(conversion),
3952 sp,
3953 in.GetStackIndex());
3954 } else {
3955 DCHECK(in.IsConstant());
3956 DCHECK(in.GetConstant()->IsLongConstant());
Vladimir Markoba1a48e2017-04-13 11:50:14 +01003957 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
3958 __ Mov(OutputRegister(conversion), static_cast<int32_t>(value));
Scott Wakelingfe885462016-09-22 10:24:38 +01003959 }
3960 break;
3961
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003962 case DataType::Type::kFloat32: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003963 vixl32::SRegister temp = LowSRegisterFrom(locations->GetTemp(0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01003964 __ Vcvt(S32, F32, temp, InputSRegisterAt(conversion, 0));
Scott Wakelingfe885462016-09-22 10:24:38 +01003965 __ Vmov(OutputRegister(conversion), temp);
3966 break;
3967 }
3968
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003969 case DataType::Type::kFloat64: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003970 vixl32::SRegister temp_s = LowSRegisterFrom(locations->GetTemp(0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01003971 __ Vcvt(S32, F64, temp_s, DRegisterFrom(in));
Scott Wakelingfe885462016-09-22 10:24:38 +01003972 __ Vmov(OutputRegister(conversion), temp_s);
3973 break;
3974 }
3975
3976 default:
3977 LOG(FATAL) << "Unexpected type conversion from " << input_type
3978 << " to " << result_type;
3979 }
3980 break;
3981
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003982 case DataType::Type::kInt64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003983 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003984 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003985 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003986 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003987 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003988 case DataType::Type::kInt16:
3989 case DataType::Type::kInt32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003990 DCHECK(out.IsRegisterPair());
3991 DCHECK(in.IsRegister());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003992 __ Mov(LowRegisterFrom(out), InputRegisterAt(conversion, 0));
Scott Wakelingfe885462016-09-22 10:24:38 +01003993 // Sign extension.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003994 __ Asr(HighRegisterFrom(out), LowRegisterFrom(out), 31);
Scott Wakelingfe885462016-09-22 10:24:38 +01003995 break;
3996
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003997 case DataType::Type::kFloat32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003998 codegen_->InvokeRuntime(kQuickF2l, conversion, conversion->GetDexPc());
3999 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
4000 break;
4001
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004002 case DataType::Type::kFloat64:
Scott Wakelingfe885462016-09-22 10:24:38 +01004003 codegen_->InvokeRuntime(kQuickD2l, conversion, conversion->GetDexPc());
4004 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
4005 break;
4006
4007 default:
4008 LOG(FATAL) << "Unexpected type conversion from " << input_type
4009 << " to " << result_type;
4010 }
4011 break;
4012
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004013 case DataType::Type::kFloat32:
Scott Wakelingfe885462016-09-22 10:24:38 +01004014 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004015 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004016 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004017 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004018 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004019 case DataType::Type::kInt16:
4020 case DataType::Type::kInt32:
Scott Wakelingfe885462016-09-22 10:24:38 +01004021 __ Vmov(OutputSRegister(conversion), InputRegisterAt(conversion, 0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01004022 __ Vcvt(F32, S32, OutputSRegister(conversion), OutputSRegister(conversion));
Scott Wakelingfe885462016-09-22 10:24:38 +01004023 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01004024
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004025 case DataType::Type::kInt64:
Scott Wakelingfe885462016-09-22 10:24:38 +01004026 codegen_->InvokeRuntime(kQuickL2f, conversion, conversion->GetDexPc());
4027 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
4028 break;
4029
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004030 case DataType::Type::kFloat64:
Scott Wakelingc34dba72016-10-03 10:14:44 +01004031 __ Vcvt(F32, F64, OutputSRegister(conversion), DRegisterFrom(in));
Scott Wakelingfe885462016-09-22 10:24:38 +01004032 break;
4033
4034 default:
4035 LOG(FATAL) << "Unexpected type conversion from " << input_type
4036 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08004037 }
Scott Wakelingfe885462016-09-22 10:24:38 +01004038 break;
4039
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004040 case DataType::Type::kFloat64:
Scott Wakelingfe885462016-09-22 10:24:38 +01004041 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004042 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004043 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004044 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004045 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004046 case DataType::Type::kInt16:
4047 case DataType::Type::kInt32:
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004048 __ Vmov(LowSRegisterFrom(out), InputRegisterAt(conversion, 0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01004049 __ Vcvt(F64, S32, DRegisterFrom(out), LowSRegisterFrom(out));
Scott Wakelingfe885462016-09-22 10:24:38 +01004050 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01004051
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004052 case DataType::Type::kInt64: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004053 vixl32::Register low = LowRegisterFrom(in);
4054 vixl32::Register high = HighRegisterFrom(in);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004055 vixl32::SRegister out_s = LowSRegisterFrom(out);
Scott Wakelingc34dba72016-10-03 10:14:44 +01004056 vixl32::DRegister out_d = DRegisterFrom(out);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004057 vixl32::SRegister temp_s = LowSRegisterFrom(locations->GetTemp(0));
Scott Wakelingc34dba72016-10-03 10:14:44 +01004058 vixl32::DRegister temp_d = DRegisterFrom(locations->GetTemp(0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01004059 vixl32::DRegister constant_d = DRegisterFrom(locations->GetTemp(1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004060
4061 // temp_d = int-to-double(high)
4062 __ Vmov(temp_s, high);
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01004063 __ Vcvt(F64, S32, temp_d, temp_s);
Scott Wakelingfe885462016-09-22 10:24:38 +01004064 // constant_d = k2Pow32EncodingForDouble
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004065 __ Vmov(constant_d, bit_cast<double, int64_t>(k2Pow32EncodingForDouble));
Scott Wakelingfe885462016-09-22 10:24:38 +01004066 // out_d = unsigned-to-double(low)
4067 __ Vmov(out_s, low);
4068 __ Vcvt(F64, U32, out_d, out_s);
4069 // out_d += temp_d * constant_d
4070 __ Vmla(F64, out_d, temp_d, constant_d);
4071 break;
4072 }
4073
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004074 case DataType::Type::kFloat32:
Scott Wakelingc34dba72016-10-03 10:14:44 +01004075 __ Vcvt(F64, F32, DRegisterFrom(out), InputSRegisterAt(conversion, 0));
Scott Wakelingfe885462016-09-22 10:24:38 +01004076 break;
4077
4078 default:
4079 LOG(FATAL) << "Unexpected type conversion from " << input_type
4080 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08004081 }
Scott Wakelingfe885462016-09-22 10:24:38 +01004082 break;
4083
4084 default:
4085 LOG(FATAL) << "Unexpected type conversion from " << input_type
4086 << " to " << result_type;
4087 }
4088}
4089
4090void LocationsBuilderARMVIXL::VisitAdd(HAdd* add) {
4091 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004092 new (GetGraph()->GetAllocator()) LocationSummary(add, LocationSummary::kNoCall);
Scott Wakelingfe885462016-09-22 10:24:38 +01004093 switch (add->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004094 case DataType::Type::kInt32: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004095 locations->SetInAt(0, Location::RequiresRegister());
4096 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
4097 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4098 break;
4099 }
4100
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004101 case DataType::Type::kInt64: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004102 locations->SetInAt(0, Location::RequiresRegister());
Anton Kirilovdda43962016-11-21 19:55:20 +00004103 locations->SetInAt(1, ArmEncodableConstantOrRegister(add->InputAt(1), ADD));
Scott Wakelingfe885462016-09-22 10:24:38 +01004104 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4105 break;
4106 }
4107
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004108 case DataType::Type::kFloat32:
4109 case DataType::Type::kFloat64: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004110 locations->SetInAt(0, Location::RequiresFpuRegister());
4111 locations->SetInAt(1, Location::RequiresFpuRegister());
4112 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4113 break;
4114 }
4115
4116 default:
4117 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
4118 }
4119}
4120
4121void InstructionCodeGeneratorARMVIXL::VisitAdd(HAdd* add) {
4122 LocationSummary* locations = add->GetLocations();
4123 Location out = locations->Out();
4124 Location first = locations->InAt(0);
4125 Location second = locations->InAt(1);
4126
4127 switch (add->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004128 case DataType::Type::kInt32: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004129 __ Add(OutputRegister(add), InputRegisterAt(add, 0), InputOperandAt(add, 1));
4130 }
4131 break;
4132
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004133 case DataType::Type::kInt64: {
Anton Kirilovdda43962016-11-21 19:55:20 +00004134 if (second.IsConstant()) {
4135 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
4136 GenerateAddLongConst(out, first, value);
4137 } else {
4138 DCHECK(second.IsRegisterPair());
4139 __ Adds(LowRegisterFrom(out), LowRegisterFrom(first), LowRegisterFrom(second));
4140 __ Adc(HighRegisterFrom(out), HighRegisterFrom(first), HighRegisterFrom(second));
4141 }
Scott Wakelingfe885462016-09-22 10:24:38 +01004142 break;
4143 }
4144
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004145 case DataType::Type::kFloat32:
4146 case DataType::Type::kFloat64:
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004147 __ Vadd(OutputVRegister(add), InputVRegisterAt(add, 0), InputVRegisterAt(add, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004148 break;
4149
4150 default:
4151 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
4152 }
4153}
4154
4155void LocationsBuilderARMVIXL::VisitSub(HSub* sub) {
4156 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004157 new (GetGraph()->GetAllocator()) LocationSummary(sub, LocationSummary::kNoCall);
Scott Wakelingfe885462016-09-22 10:24:38 +01004158 switch (sub->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004159 case DataType::Type::kInt32: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004160 locations->SetInAt(0, Location::RequiresRegister());
4161 locations->SetInAt(1, Location::RegisterOrConstant(sub->InputAt(1)));
4162 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4163 break;
4164 }
4165
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004166 case DataType::Type::kInt64: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004167 locations->SetInAt(0, Location::RequiresRegister());
Anton Kirilovdda43962016-11-21 19:55:20 +00004168 locations->SetInAt(1, ArmEncodableConstantOrRegister(sub->InputAt(1), SUB));
Scott Wakelingfe885462016-09-22 10:24:38 +01004169 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4170 break;
4171 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004172 case DataType::Type::kFloat32:
4173 case DataType::Type::kFloat64: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004174 locations->SetInAt(0, Location::RequiresFpuRegister());
4175 locations->SetInAt(1, Location::RequiresFpuRegister());
4176 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4177 break;
4178 }
4179 default:
4180 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
4181 }
4182}
4183
4184void InstructionCodeGeneratorARMVIXL::VisitSub(HSub* sub) {
4185 LocationSummary* locations = sub->GetLocations();
4186 Location out = locations->Out();
4187 Location first = locations->InAt(0);
4188 Location second = locations->InAt(1);
4189 switch (sub->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004190 case DataType::Type::kInt32: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004191 __ Sub(OutputRegister(sub), InputRegisterAt(sub, 0), InputOperandAt(sub, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004192 break;
4193 }
4194
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004195 case DataType::Type::kInt64: {
Anton Kirilovdda43962016-11-21 19:55:20 +00004196 if (second.IsConstant()) {
4197 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
4198 GenerateAddLongConst(out, first, -value);
4199 } else {
4200 DCHECK(second.IsRegisterPair());
4201 __ Subs(LowRegisterFrom(out), LowRegisterFrom(first), LowRegisterFrom(second));
4202 __ Sbc(HighRegisterFrom(out), HighRegisterFrom(first), HighRegisterFrom(second));
4203 }
Scott Wakelingfe885462016-09-22 10:24:38 +01004204 break;
4205 }
4206
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004207 case DataType::Type::kFloat32:
4208 case DataType::Type::kFloat64:
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004209 __ Vsub(OutputVRegister(sub), InputVRegisterAt(sub, 0), InputVRegisterAt(sub, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004210 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01004211
4212 default:
4213 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
4214 }
4215}
4216
4217void LocationsBuilderARMVIXL::VisitMul(HMul* mul) {
4218 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004219 new (GetGraph()->GetAllocator()) LocationSummary(mul, LocationSummary::kNoCall);
Scott Wakelingfe885462016-09-22 10:24:38 +01004220 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004221 case DataType::Type::kInt32:
4222 case DataType::Type::kInt64: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004223 locations->SetInAt(0, Location::RequiresRegister());
4224 locations->SetInAt(1, Location::RequiresRegister());
4225 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4226 break;
4227 }
4228
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004229 case DataType::Type::kFloat32:
4230 case DataType::Type::kFloat64: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004231 locations->SetInAt(0, Location::RequiresFpuRegister());
4232 locations->SetInAt(1, Location::RequiresFpuRegister());
4233 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4234 break;
4235 }
4236
4237 default:
4238 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4239 }
4240}
4241
4242void InstructionCodeGeneratorARMVIXL::VisitMul(HMul* mul) {
4243 LocationSummary* locations = mul->GetLocations();
4244 Location out = locations->Out();
4245 Location first = locations->InAt(0);
4246 Location second = locations->InAt(1);
4247 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004248 case DataType::Type::kInt32: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004249 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
4250 break;
4251 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004252 case DataType::Type::kInt64: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004253 vixl32::Register out_hi = HighRegisterFrom(out);
4254 vixl32::Register out_lo = LowRegisterFrom(out);
4255 vixl32::Register in1_hi = HighRegisterFrom(first);
4256 vixl32::Register in1_lo = LowRegisterFrom(first);
4257 vixl32::Register in2_hi = HighRegisterFrom(second);
4258 vixl32::Register in2_lo = LowRegisterFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01004259
4260 // Extra checks to protect caused by the existence of R1_R2.
4261 // The algorithm is wrong if out.hi is either in1.lo or in2.lo:
4262 // (e.g. in1=r0_r1, in2=r2_r3 and out=r1_r2);
Anton Kirilov644032c2016-12-06 17:51:43 +00004263 DCHECK(!out_hi.Is(in1_lo));
4264 DCHECK(!out_hi.Is(in2_lo));
Scott Wakelingfe885462016-09-22 10:24:38 +01004265
4266 // input: in1 - 64 bits, in2 - 64 bits
4267 // output: out
4268 // formula: out.hi : out.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
4269 // parts: out.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
4270 // parts: out.lo = (in1.lo * in2.lo)[31:0]
4271
4272 UseScratchRegisterScope temps(GetVIXLAssembler());
4273 vixl32::Register temp = temps.Acquire();
4274 // temp <- in1.lo * in2.hi
4275 __ Mul(temp, in1_lo, in2_hi);
4276 // out.hi <- in1.lo * in2.hi + in1.hi * in2.lo
4277 __ Mla(out_hi, in1_hi, in2_lo, temp);
4278 // out.lo <- (in1.lo * in2.lo)[31:0];
4279 __ Umull(out_lo, temp, in1_lo, in2_lo);
4280 // out.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004281 __ Add(out_hi, out_hi, temp);
Scott Wakelingfe885462016-09-22 10:24:38 +01004282 break;
4283 }
4284
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004285 case DataType::Type::kFloat32:
4286 case DataType::Type::kFloat64:
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004287 __ Vmul(OutputVRegister(mul), InputVRegisterAt(mul, 0), InputVRegisterAt(mul, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004288 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01004289
4290 default:
4291 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4292 }
4293}
4294
Scott Wakelingfe885462016-09-22 10:24:38 +01004295void InstructionCodeGeneratorARMVIXL::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
4296 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004297 DCHECK(instruction->GetResultType() == DataType::Type::kInt32);
Scott Wakelingfe885462016-09-22 10:24:38 +01004298
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004299 Location second = instruction->GetLocations()->InAt(1);
Scott Wakelingfe885462016-09-22 10:24:38 +01004300 DCHECK(second.IsConstant());
4301
4302 vixl32::Register out = OutputRegister(instruction);
4303 vixl32::Register dividend = InputRegisterAt(instruction, 0);
Anton Kirilov644032c2016-12-06 17:51:43 +00004304 int32_t imm = Int32ConstantFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01004305 DCHECK(imm == 1 || imm == -1);
4306
4307 if (instruction->IsRem()) {
4308 __ Mov(out, 0);
4309 } else {
4310 if (imm == 1) {
4311 __ Mov(out, dividend);
4312 } else {
4313 __ Rsb(out, dividend, 0);
4314 }
4315 }
4316}
4317
4318void InstructionCodeGeneratorARMVIXL::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
4319 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004320 DCHECK(instruction->GetResultType() == DataType::Type::kInt32);
Scott Wakelingfe885462016-09-22 10:24:38 +01004321
4322 LocationSummary* locations = instruction->GetLocations();
4323 Location second = locations->InAt(1);
4324 DCHECK(second.IsConstant());
4325
4326 vixl32::Register out = OutputRegister(instruction);
4327 vixl32::Register dividend = InputRegisterAt(instruction, 0);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004328 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
Anton Kirilov644032c2016-12-06 17:51:43 +00004329 int32_t imm = Int32ConstantFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01004330 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
4331 int ctz_imm = CTZ(abs_imm);
4332
4333 if (ctz_imm == 1) {
4334 __ Lsr(temp, dividend, 32 - ctz_imm);
4335 } else {
4336 __ Asr(temp, dividend, 31);
4337 __ Lsr(temp, temp, 32 - ctz_imm);
4338 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004339 __ Add(out, temp, dividend);
Scott Wakelingfe885462016-09-22 10:24:38 +01004340
4341 if (instruction->IsDiv()) {
4342 __ Asr(out, out, ctz_imm);
4343 if (imm < 0) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004344 __ Rsb(out, out, 0);
Scott Wakelingfe885462016-09-22 10:24:38 +01004345 }
4346 } else {
4347 __ Ubfx(out, out, 0, ctz_imm);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004348 __ Sub(out, out, temp);
Scott Wakelingfe885462016-09-22 10:24:38 +01004349 }
4350}
4351
4352void InstructionCodeGeneratorARMVIXL::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
4353 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004354 DCHECK(instruction->GetResultType() == DataType::Type::kInt32);
Scott Wakelingfe885462016-09-22 10:24:38 +01004355
4356 LocationSummary* locations = instruction->GetLocations();
4357 Location second = locations->InAt(1);
4358 DCHECK(second.IsConstant());
4359
4360 vixl32::Register out = OutputRegister(instruction);
4361 vixl32::Register dividend = InputRegisterAt(instruction, 0);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004362 vixl32::Register temp1 = RegisterFrom(locations->GetTemp(0));
4363 vixl32::Register temp2 = RegisterFrom(locations->GetTemp(1));
Scott Wakelingb77051e2016-11-21 19:46:00 +00004364 int32_t imm = Int32ConstantFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01004365
4366 int64_t magic;
4367 int shift;
4368 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
4369
Anton Kirilovdda43962016-11-21 19:55:20 +00004370 // TODO(VIXL): Change the static cast to Operand::From() after VIXL is fixed.
4371 __ Mov(temp1, static_cast<int32_t>(magic));
Scott Wakelingfe885462016-09-22 10:24:38 +01004372 __ Smull(temp2, temp1, dividend, temp1);
4373
4374 if (imm > 0 && magic < 0) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004375 __ Add(temp1, temp1, dividend);
Scott Wakelingfe885462016-09-22 10:24:38 +01004376 } else if (imm < 0 && magic > 0) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004377 __ Sub(temp1, temp1, dividend);
Scott Wakelingfe885462016-09-22 10:24:38 +01004378 }
4379
4380 if (shift != 0) {
4381 __ Asr(temp1, temp1, shift);
4382 }
4383
4384 if (instruction->IsDiv()) {
4385 __ Sub(out, temp1, Operand(temp1, vixl32::Shift(ASR), 31));
4386 } else {
4387 __ Sub(temp1, temp1, Operand(temp1, vixl32::Shift(ASR), 31));
4388 // TODO: Strength reduction for mls.
4389 __ Mov(temp2, imm);
4390 __ Mls(out, temp1, temp2, dividend);
4391 }
4392}
4393
4394void InstructionCodeGeneratorARMVIXL::GenerateDivRemConstantIntegral(
4395 HBinaryOperation* instruction) {
4396 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004397 DCHECK(instruction->GetResultType() == DataType::Type::kInt32);
Scott Wakelingfe885462016-09-22 10:24:38 +01004398
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004399 Location second = instruction->GetLocations()->InAt(1);
Scott Wakelingfe885462016-09-22 10:24:38 +01004400 DCHECK(second.IsConstant());
4401
Anton Kirilov644032c2016-12-06 17:51:43 +00004402 int32_t imm = Int32ConstantFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01004403 if (imm == 0) {
4404 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
4405 } else if (imm == 1 || imm == -1) {
4406 DivRemOneOrMinusOne(instruction);
4407 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
4408 DivRemByPowerOfTwo(instruction);
4409 } else {
4410 DCHECK(imm <= -2 || imm >= 2);
4411 GenerateDivRemWithAnyConstant(instruction);
4412 }
4413}
4414
4415void LocationsBuilderARMVIXL::VisitDiv(HDiv* div) {
4416 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004417 if (div->GetResultType() == DataType::Type::kInt64) {
Scott Wakelingfe885462016-09-22 10:24:38 +01004418 // pLdiv runtime call.
4419 call_kind = LocationSummary::kCallOnMainOnly;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004420 } else if (div->GetResultType() == DataType::Type::kInt32 && div->InputAt(1)->IsConstant()) {
Scott Wakelingfe885462016-09-22 10:24:38 +01004421 // sdiv will be replaced by other instruction sequence.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004422 } else if (div->GetResultType() == DataType::Type::kInt32 &&
Scott Wakelingfe885462016-09-22 10:24:38 +01004423 !codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4424 // pIdivmod runtime call.
4425 call_kind = LocationSummary::kCallOnMainOnly;
4426 }
4427
Vladimir Markoca6fff82017-10-03 14:49:14 +01004428 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(div, call_kind);
Scott Wakelingfe885462016-09-22 10:24:38 +01004429
4430 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004431 case DataType::Type::kInt32: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004432 if (div->InputAt(1)->IsConstant()) {
4433 locations->SetInAt(0, Location::RequiresRegister());
4434 locations->SetInAt(1, Location::ConstantLocation(div->InputAt(1)->AsConstant()));
4435 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Anton Kirilov644032c2016-12-06 17:51:43 +00004436 int32_t value = Int32ConstantFrom(div->InputAt(1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004437 if (value == 1 || value == 0 || value == -1) {
4438 // No temp register required.
4439 } else {
4440 locations->AddTemp(Location::RequiresRegister());
4441 if (!IsPowerOfTwo(AbsOrMin(value))) {
4442 locations->AddTemp(Location::RequiresRegister());
4443 }
4444 }
4445 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4446 locations->SetInAt(0, Location::RequiresRegister());
4447 locations->SetInAt(1, Location::RequiresRegister());
4448 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4449 } else {
Artem Serov551b28f2016-10-18 19:11:30 +01004450 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4451 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4452 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
Roland Levillain5e8d5f02016-10-18 18:03:43 +01004453 // Note: divmod will compute both the quotient and the remainder as the pair R0 and R1, but
Artem Serov551b28f2016-10-18 19:11:30 +01004454 // we only need the former.
4455 locations->SetOut(LocationFrom(r0));
Scott Wakelingfe885462016-09-22 10:24:38 +01004456 }
4457 break;
4458 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004459 case DataType::Type::kInt64: {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01004460 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4461 locations->SetInAt(0, LocationFrom(
4462 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
4463 locations->SetInAt(1, LocationFrom(
4464 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
4465 locations->SetOut(LocationFrom(r0, r1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004466 break;
4467 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004468 case DataType::Type::kFloat32:
4469 case DataType::Type::kFloat64: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004470 locations->SetInAt(0, Location::RequiresFpuRegister());
4471 locations->SetInAt(1, Location::RequiresFpuRegister());
4472 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4473 break;
4474 }
4475
4476 default:
4477 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
4478 }
4479}
4480
4481void InstructionCodeGeneratorARMVIXL::VisitDiv(HDiv* div) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01004482 Location lhs = div->GetLocations()->InAt(0);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004483 Location rhs = div->GetLocations()->InAt(1);
Scott Wakelingfe885462016-09-22 10:24:38 +01004484
4485 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004486 case DataType::Type::kInt32: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004487 if (rhs.IsConstant()) {
Scott Wakelingfe885462016-09-22 10:24:38 +01004488 GenerateDivRemConstantIntegral(div);
4489 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4490 __ Sdiv(OutputRegister(div), InputRegisterAt(div, 0), InputRegisterAt(div, 1));
4491 } else {
Artem Serov551b28f2016-10-18 19:11:30 +01004492 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4493 DCHECK(calling_convention.GetRegisterAt(0).Is(RegisterFrom(lhs)));
4494 DCHECK(calling_convention.GetRegisterAt(1).Is(RegisterFrom(rhs)));
4495 DCHECK(r0.Is(OutputRegister(div)));
4496
4497 codegen_->InvokeRuntime(kQuickIdivmod, div, div->GetDexPc());
4498 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
Scott Wakelingfe885462016-09-22 10:24:38 +01004499 }
4500 break;
4501 }
4502
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004503 case DataType::Type::kInt64: {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01004504 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4505 DCHECK(calling_convention.GetRegisterAt(0).Is(LowRegisterFrom(lhs)));
4506 DCHECK(calling_convention.GetRegisterAt(1).Is(HighRegisterFrom(lhs)));
4507 DCHECK(calling_convention.GetRegisterAt(2).Is(LowRegisterFrom(rhs)));
4508 DCHECK(calling_convention.GetRegisterAt(3).Is(HighRegisterFrom(rhs)));
4509 DCHECK(LowRegisterFrom(div->GetLocations()->Out()).Is(r0));
4510 DCHECK(HighRegisterFrom(div->GetLocations()->Out()).Is(r1));
4511
4512 codegen_->InvokeRuntime(kQuickLdiv, div, div->GetDexPc());
4513 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Scott Wakelingfe885462016-09-22 10:24:38 +01004514 break;
4515 }
4516
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004517 case DataType::Type::kFloat32:
4518 case DataType::Type::kFloat64:
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004519 __ Vdiv(OutputVRegister(div), InputVRegisterAt(div, 0), InputVRegisterAt(div, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004520 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01004521
4522 default:
4523 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
4524 }
4525}
4526
Artem Serov551b28f2016-10-18 19:11:30 +01004527void LocationsBuilderARMVIXL::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004528 DataType::Type type = rem->GetResultType();
Artem Serov551b28f2016-10-18 19:11:30 +01004529
4530 // Most remainders are implemented in the runtime.
4531 LocationSummary::CallKind call_kind = LocationSummary::kCallOnMainOnly;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004532 if (rem->GetResultType() == DataType::Type::kInt32 && rem->InputAt(1)->IsConstant()) {
Artem Serov551b28f2016-10-18 19:11:30 +01004533 // sdiv will be replaced by other instruction sequence.
4534 call_kind = LocationSummary::kNoCall;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004535 } else if ((rem->GetResultType() == DataType::Type::kInt32)
Artem Serov551b28f2016-10-18 19:11:30 +01004536 && codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4537 // Have hardware divide instruction for int, do it with three instructions.
4538 call_kind = LocationSummary::kNoCall;
4539 }
4540
Vladimir Markoca6fff82017-10-03 14:49:14 +01004541 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(rem, call_kind);
Artem Serov551b28f2016-10-18 19:11:30 +01004542
4543 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004544 case DataType::Type::kInt32: {
Artem Serov551b28f2016-10-18 19:11:30 +01004545 if (rem->InputAt(1)->IsConstant()) {
4546 locations->SetInAt(0, Location::RequiresRegister());
4547 locations->SetInAt(1, Location::ConstantLocation(rem->InputAt(1)->AsConstant()));
4548 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Anton Kirilov644032c2016-12-06 17:51:43 +00004549 int32_t value = Int32ConstantFrom(rem->InputAt(1));
Artem Serov551b28f2016-10-18 19:11:30 +01004550 if (value == 1 || value == 0 || value == -1) {
4551 // No temp register required.
4552 } else {
4553 locations->AddTemp(Location::RequiresRegister());
4554 if (!IsPowerOfTwo(AbsOrMin(value))) {
4555 locations->AddTemp(Location::RequiresRegister());
4556 }
4557 }
4558 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4559 locations->SetInAt(0, Location::RequiresRegister());
4560 locations->SetInAt(1, Location::RequiresRegister());
4561 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4562 locations->AddTemp(Location::RequiresRegister());
4563 } else {
4564 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4565 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4566 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
Roland Levillain5e8d5f02016-10-18 18:03:43 +01004567 // Note: divmod will compute both the quotient and the remainder as the pair R0 and R1, but
Artem Serov551b28f2016-10-18 19:11:30 +01004568 // we only need the latter.
4569 locations->SetOut(LocationFrom(r1));
4570 }
4571 break;
4572 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004573 case DataType::Type::kInt64: {
Artem Serov551b28f2016-10-18 19:11:30 +01004574 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4575 locations->SetInAt(0, LocationFrom(
4576 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
4577 locations->SetInAt(1, LocationFrom(
4578 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
4579 // The runtime helper puts the output in R2,R3.
4580 locations->SetOut(LocationFrom(r2, r3));
4581 break;
4582 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004583 case DataType::Type::kFloat32: {
Artem Serov551b28f2016-10-18 19:11:30 +01004584 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4585 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
4586 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
4587 locations->SetOut(LocationFrom(s0));
4588 break;
4589 }
4590
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004591 case DataType::Type::kFloat64: {
Artem Serov551b28f2016-10-18 19:11:30 +01004592 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4593 locations->SetInAt(0, LocationFrom(
4594 calling_convention.GetFpuRegisterAt(0), calling_convention.GetFpuRegisterAt(1)));
4595 locations->SetInAt(1, LocationFrom(
4596 calling_convention.GetFpuRegisterAt(2), calling_convention.GetFpuRegisterAt(3)));
4597 locations->SetOut(LocationFrom(s0, s1));
4598 break;
4599 }
4600
4601 default:
4602 LOG(FATAL) << "Unexpected rem type " << type;
4603 }
4604}
4605
4606void InstructionCodeGeneratorARMVIXL::VisitRem(HRem* rem) {
4607 LocationSummary* locations = rem->GetLocations();
4608 Location second = locations->InAt(1);
4609
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004610 DataType::Type type = rem->GetResultType();
Artem Serov551b28f2016-10-18 19:11:30 +01004611 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004612 case DataType::Type::kInt32: {
Artem Serov551b28f2016-10-18 19:11:30 +01004613 vixl32::Register reg1 = InputRegisterAt(rem, 0);
4614 vixl32::Register out_reg = OutputRegister(rem);
4615 if (second.IsConstant()) {
4616 GenerateDivRemConstantIntegral(rem);
4617 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4618 vixl32::Register reg2 = RegisterFrom(second);
4619 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
4620
4621 // temp = reg1 / reg2 (integer division)
4622 // dest = reg1 - temp * reg2
4623 __ Sdiv(temp, reg1, reg2);
4624 __ Mls(out_reg, temp, reg2, reg1);
4625 } else {
4626 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4627 DCHECK(reg1.Is(calling_convention.GetRegisterAt(0)));
4628 DCHECK(RegisterFrom(second).Is(calling_convention.GetRegisterAt(1)));
4629 DCHECK(out_reg.Is(r1));
4630
4631 codegen_->InvokeRuntime(kQuickIdivmod, rem, rem->GetDexPc());
4632 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
4633 }
4634 break;
4635 }
4636
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004637 case DataType::Type::kInt64: {
Artem Serov551b28f2016-10-18 19:11:30 +01004638 codegen_->InvokeRuntime(kQuickLmod, rem, rem->GetDexPc());
4639 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
4640 break;
4641 }
4642
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004643 case DataType::Type::kFloat32: {
Artem Serov551b28f2016-10-18 19:11:30 +01004644 codegen_->InvokeRuntime(kQuickFmodf, rem, rem->GetDexPc());
4645 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
4646 break;
4647 }
4648
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004649 case DataType::Type::kFloat64: {
Artem Serov551b28f2016-10-18 19:11:30 +01004650 codegen_->InvokeRuntime(kQuickFmod, rem, rem->GetDexPc());
4651 CheckEntrypointTypes<kQuickFmod, double, double, double>();
4652 break;
4653 }
4654
4655 default:
4656 LOG(FATAL) << "Unexpected rem type " << type;
4657 }
4658}
4659
4660
Scott Wakelingfe885462016-09-22 10:24:38 +01004661void LocationsBuilderARMVIXL::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Artem Serov657022c2016-11-23 14:19:38 +00004662 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Scott Wakelingfe885462016-09-22 10:24:38 +01004663 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Scott Wakelingfe885462016-09-22 10:24:38 +01004664}
4665
4666void InstructionCodeGeneratorARMVIXL::VisitDivZeroCheck(HDivZeroCheck* instruction) {
4667 DivZeroCheckSlowPathARMVIXL* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01004668 new (codegen_->GetScopedAllocator()) DivZeroCheckSlowPathARMVIXL(instruction);
Scott Wakelingfe885462016-09-22 10:24:38 +01004669 codegen_->AddSlowPath(slow_path);
4670
4671 LocationSummary* locations = instruction->GetLocations();
4672 Location value = locations->InAt(0);
4673
4674 switch (instruction->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004675 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004676 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004677 case DataType::Type::kInt8:
4678 case DataType::Type::kUint16:
4679 case DataType::Type::kInt16:
4680 case DataType::Type::kInt32: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004681 if (value.IsRegister()) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00004682 __ CompareAndBranchIfZero(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
Scott Wakelingfe885462016-09-22 10:24:38 +01004683 } else {
4684 DCHECK(value.IsConstant()) << value;
Anton Kirilov644032c2016-12-06 17:51:43 +00004685 if (Int32ConstantFrom(value) == 0) {
Scott Wakelingfe885462016-09-22 10:24:38 +01004686 __ B(slow_path->GetEntryLabel());
4687 }
4688 }
4689 break;
4690 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004691 case DataType::Type::kInt64: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004692 if (value.IsRegisterPair()) {
4693 UseScratchRegisterScope temps(GetVIXLAssembler());
4694 vixl32::Register temp = temps.Acquire();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004695 __ Orrs(temp, LowRegisterFrom(value), HighRegisterFrom(value));
Scott Wakelingfe885462016-09-22 10:24:38 +01004696 __ B(eq, slow_path->GetEntryLabel());
4697 } else {
4698 DCHECK(value.IsConstant()) << value;
Anton Kirilov644032c2016-12-06 17:51:43 +00004699 if (Int64ConstantFrom(value) == 0) {
Scott Wakelingfe885462016-09-22 10:24:38 +01004700 __ B(slow_path->GetEntryLabel());
4701 }
4702 }
4703 break;
4704 }
4705 default:
4706 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
4707 }
4708}
4709
Artem Serov02109dd2016-09-23 17:17:54 +01004710void InstructionCodeGeneratorARMVIXL::HandleIntegerRotate(HRor* ror) {
4711 LocationSummary* locations = ror->GetLocations();
4712 vixl32::Register in = InputRegisterAt(ror, 0);
4713 Location rhs = locations->InAt(1);
4714 vixl32::Register out = OutputRegister(ror);
4715
4716 if (rhs.IsConstant()) {
4717 // Arm32 and Thumb2 assemblers require a rotation on the interval [1,31],
4718 // so map all rotations to a +ve. equivalent in that range.
4719 // (e.g. left *or* right by -2 bits == 30 bits in the same direction.)
4720 uint32_t rot = CodeGenerator::GetInt32ValueOf(rhs.GetConstant()) & 0x1F;
4721 if (rot) {
4722 // Rotate, mapping left rotations to right equivalents if necessary.
4723 // (e.g. left by 2 bits == right by 30.)
4724 __ Ror(out, in, rot);
4725 } else if (!out.Is(in)) {
4726 __ Mov(out, in);
4727 }
4728 } else {
4729 __ Ror(out, in, RegisterFrom(rhs));
4730 }
4731}
4732
4733// Gain some speed by mapping all Long rotates onto equivalent pairs of Integer
4734// rotates by swapping input regs (effectively rotating by the first 32-bits of
4735// a larger rotation) or flipping direction (thus treating larger right/left
4736// rotations as sub-word sized rotations in the other direction) as appropriate.
4737void InstructionCodeGeneratorARMVIXL::HandleLongRotate(HRor* ror) {
4738 LocationSummary* locations = ror->GetLocations();
4739 vixl32::Register in_reg_lo = LowRegisterFrom(locations->InAt(0));
4740 vixl32::Register in_reg_hi = HighRegisterFrom(locations->InAt(0));
4741 Location rhs = locations->InAt(1);
4742 vixl32::Register out_reg_lo = LowRegisterFrom(locations->Out());
4743 vixl32::Register out_reg_hi = HighRegisterFrom(locations->Out());
4744
4745 if (rhs.IsConstant()) {
4746 uint64_t rot = CodeGenerator::GetInt64ValueOf(rhs.GetConstant());
4747 // Map all rotations to +ve. equivalents on the interval [0,63].
4748 rot &= kMaxLongShiftDistance;
4749 // For rotates over a word in size, 'pre-rotate' by 32-bits to keep rotate
4750 // logic below to a simple pair of binary orr.
4751 // (e.g. 34 bits == in_reg swap + 2 bits right.)
4752 if (rot >= kArmBitsPerWord) {
4753 rot -= kArmBitsPerWord;
4754 std::swap(in_reg_hi, in_reg_lo);
4755 }
4756 // Rotate, or mov to out for zero or word size rotations.
4757 if (rot != 0u) {
Scott Wakelingb77051e2016-11-21 19:46:00 +00004758 __ Lsr(out_reg_hi, in_reg_hi, Operand::From(rot));
Artem Serov02109dd2016-09-23 17:17:54 +01004759 __ Orr(out_reg_hi, out_reg_hi, Operand(in_reg_lo, ShiftType::LSL, kArmBitsPerWord - rot));
Scott Wakelingb77051e2016-11-21 19:46:00 +00004760 __ Lsr(out_reg_lo, in_reg_lo, Operand::From(rot));
Artem Serov02109dd2016-09-23 17:17:54 +01004761 __ Orr(out_reg_lo, out_reg_lo, Operand(in_reg_hi, ShiftType::LSL, kArmBitsPerWord - rot));
4762 } else {
4763 __ Mov(out_reg_lo, in_reg_lo);
4764 __ Mov(out_reg_hi, in_reg_hi);
4765 }
4766 } else {
4767 vixl32::Register shift_right = RegisterFrom(locations->GetTemp(0));
4768 vixl32::Register shift_left = RegisterFrom(locations->GetTemp(1));
4769 vixl32::Label end;
4770 vixl32::Label shift_by_32_plus_shift_right;
Anton Kirilov6f644202017-02-27 18:29:45 +00004771 vixl32::Label* final_label = codegen_->GetFinalLabel(ror, &end);
Artem Serov02109dd2016-09-23 17:17:54 +01004772
4773 __ And(shift_right, RegisterFrom(rhs), 0x1F);
4774 __ Lsrs(shift_left, RegisterFrom(rhs), 6);
Scott Wakelingbffdc702016-12-07 17:46:03 +00004775 __ Rsb(LeaveFlags, shift_left, shift_right, Operand::From(kArmBitsPerWord));
Artem Serov517d9f62016-12-12 15:51:15 +00004776 __ B(cc, &shift_by_32_plus_shift_right, /* far_target */ false);
Artem Serov02109dd2016-09-23 17:17:54 +01004777
4778 // out_reg_hi = (reg_hi << shift_left) | (reg_lo >> shift_right).
4779 // out_reg_lo = (reg_lo << shift_left) | (reg_hi >> shift_right).
4780 __ Lsl(out_reg_hi, in_reg_hi, shift_left);
4781 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
4782 __ Add(out_reg_hi, out_reg_hi, out_reg_lo);
4783 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
4784 __ Lsr(shift_left, in_reg_hi, shift_right);
4785 __ Add(out_reg_lo, out_reg_lo, shift_left);
Anton Kirilov6f644202017-02-27 18:29:45 +00004786 __ B(final_label);
Artem Serov02109dd2016-09-23 17:17:54 +01004787
4788 __ Bind(&shift_by_32_plus_shift_right); // Shift by 32+shift_right.
4789 // out_reg_hi = (reg_hi >> shift_right) | (reg_lo << shift_left).
4790 // out_reg_lo = (reg_lo >> shift_right) | (reg_hi << shift_left).
4791 __ Lsr(out_reg_hi, in_reg_hi, shift_right);
4792 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
4793 __ Add(out_reg_hi, out_reg_hi, out_reg_lo);
4794 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
4795 __ Lsl(shift_right, in_reg_hi, shift_left);
4796 __ Add(out_reg_lo, out_reg_lo, shift_right);
4797
Anton Kirilov6f644202017-02-27 18:29:45 +00004798 if (end.IsReferenced()) {
4799 __ Bind(&end);
4800 }
Artem Serov02109dd2016-09-23 17:17:54 +01004801 }
4802}
4803
4804void LocationsBuilderARMVIXL::VisitRor(HRor* ror) {
4805 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004806 new (GetGraph()->GetAllocator()) LocationSummary(ror, LocationSummary::kNoCall);
Artem Serov02109dd2016-09-23 17:17:54 +01004807 switch (ror->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004808 case DataType::Type::kInt32: {
Artem Serov02109dd2016-09-23 17:17:54 +01004809 locations->SetInAt(0, Location::RequiresRegister());
4810 locations->SetInAt(1, Location::RegisterOrConstant(ror->InputAt(1)));
4811 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4812 break;
4813 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004814 case DataType::Type::kInt64: {
Artem Serov02109dd2016-09-23 17:17:54 +01004815 locations->SetInAt(0, Location::RequiresRegister());
4816 if (ror->InputAt(1)->IsConstant()) {
4817 locations->SetInAt(1, Location::ConstantLocation(ror->InputAt(1)->AsConstant()));
4818 } else {
4819 locations->SetInAt(1, Location::RequiresRegister());
4820 locations->AddTemp(Location::RequiresRegister());
4821 locations->AddTemp(Location::RequiresRegister());
4822 }
4823 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
4824 break;
4825 }
4826 default:
4827 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
4828 }
4829}
4830
4831void InstructionCodeGeneratorARMVIXL::VisitRor(HRor* ror) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004832 DataType::Type type = ror->GetResultType();
Artem Serov02109dd2016-09-23 17:17:54 +01004833 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004834 case DataType::Type::kInt32: {
Artem Serov02109dd2016-09-23 17:17:54 +01004835 HandleIntegerRotate(ror);
4836 break;
4837 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004838 case DataType::Type::kInt64: {
Artem Serov02109dd2016-09-23 17:17:54 +01004839 HandleLongRotate(ror);
4840 break;
4841 }
4842 default:
4843 LOG(FATAL) << "Unexpected operation type " << type;
4844 UNREACHABLE();
4845 }
4846}
4847
Artem Serov02d37832016-10-25 15:25:33 +01004848void LocationsBuilderARMVIXL::HandleShift(HBinaryOperation* op) {
4849 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
4850
4851 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004852 new (GetGraph()->GetAllocator()) LocationSummary(op, LocationSummary::kNoCall);
Artem Serov02d37832016-10-25 15:25:33 +01004853
4854 switch (op->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004855 case DataType::Type::kInt32: {
Artem Serov02d37832016-10-25 15:25:33 +01004856 locations->SetInAt(0, Location::RequiresRegister());
4857 if (op->InputAt(1)->IsConstant()) {
4858 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
4859 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4860 } else {
4861 locations->SetInAt(1, Location::RequiresRegister());
4862 // Make the output overlap, as it will be used to hold the masked
4863 // second input.
4864 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
4865 }
4866 break;
4867 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004868 case DataType::Type::kInt64: {
Artem Serov02d37832016-10-25 15:25:33 +01004869 locations->SetInAt(0, Location::RequiresRegister());
4870 if (op->InputAt(1)->IsConstant()) {
4871 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
4872 // For simplicity, use kOutputOverlap even though we only require that low registers
4873 // don't clash with high registers which the register allocator currently guarantees.
4874 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
4875 } else {
4876 locations->SetInAt(1, Location::RequiresRegister());
4877 locations->AddTemp(Location::RequiresRegister());
4878 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
4879 }
4880 break;
4881 }
4882 default:
4883 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
4884 }
4885}
4886
4887void InstructionCodeGeneratorARMVIXL::HandleShift(HBinaryOperation* op) {
4888 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
4889
4890 LocationSummary* locations = op->GetLocations();
4891 Location out = locations->Out();
4892 Location first = locations->InAt(0);
4893 Location second = locations->InAt(1);
4894
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004895 DataType::Type type = op->GetResultType();
Artem Serov02d37832016-10-25 15:25:33 +01004896 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004897 case DataType::Type::kInt32: {
Artem Serov02d37832016-10-25 15:25:33 +01004898 vixl32::Register out_reg = OutputRegister(op);
4899 vixl32::Register first_reg = InputRegisterAt(op, 0);
4900 if (second.IsRegister()) {
4901 vixl32::Register second_reg = RegisterFrom(second);
4902 // ARM doesn't mask the shift count so we need to do it ourselves.
4903 __ And(out_reg, second_reg, kMaxIntShiftDistance);
4904 if (op->IsShl()) {
4905 __ Lsl(out_reg, first_reg, out_reg);
4906 } else if (op->IsShr()) {
4907 __ Asr(out_reg, first_reg, out_reg);
4908 } else {
4909 __ Lsr(out_reg, first_reg, out_reg);
4910 }
4911 } else {
Anton Kirilov644032c2016-12-06 17:51:43 +00004912 int32_t cst = Int32ConstantFrom(second);
Artem Serov02d37832016-10-25 15:25:33 +01004913 uint32_t shift_value = cst & kMaxIntShiftDistance;
4914 if (shift_value == 0) { // ARM does not support shifting with 0 immediate.
4915 __ Mov(out_reg, first_reg);
4916 } else if (op->IsShl()) {
4917 __ Lsl(out_reg, first_reg, shift_value);
4918 } else if (op->IsShr()) {
4919 __ Asr(out_reg, first_reg, shift_value);
4920 } else {
4921 __ Lsr(out_reg, first_reg, shift_value);
4922 }
4923 }
4924 break;
4925 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004926 case DataType::Type::kInt64: {
Artem Serov02d37832016-10-25 15:25:33 +01004927 vixl32::Register o_h = HighRegisterFrom(out);
4928 vixl32::Register o_l = LowRegisterFrom(out);
4929
4930 vixl32::Register high = HighRegisterFrom(first);
4931 vixl32::Register low = LowRegisterFrom(first);
4932
4933 if (second.IsRegister()) {
4934 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
4935
4936 vixl32::Register second_reg = RegisterFrom(second);
4937
4938 if (op->IsShl()) {
4939 __ And(o_l, second_reg, kMaxLongShiftDistance);
4940 // Shift the high part
4941 __ Lsl(o_h, high, o_l);
4942 // Shift the low part and `or` what overflew on the high part
Scott Wakelingb77051e2016-11-21 19:46:00 +00004943 __ Rsb(temp, o_l, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01004944 __ Lsr(temp, low, temp);
4945 __ Orr(o_h, o_h, temp);
4946 // If the shift is > 32 bits, override the high part
Scott Wakelingb77051e2016-11-21 19:46:00 +00004947 __ Subs(temp, o_l, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01004948 {
Artem Serov0fb37192016-12-06 18:13:40 +00004949 ExactAssemblyScope guard(GetVIXLAssembler(),
4950 2 * vixl32::kMaxInstructionSizeInBytes,
4951 CodeBufferCheckScope::kMaximumSize);
Artem Serov02d37832016-10-25 15:25:33 +01004952 __ it(pl);
4953 __ lsl(pl, o_h, low, temp);
4954 }
4955 // Shift the low part
4956 __ Lsl(o_l, low, o_l);
4957 } else if (op->IsShr()) {
4958 __ And(o_h, second_reg, kMaxLongShiftDistance);
4959 // Shift the low part
4960 __ Lsr(o_l, low, o_h);
4961 // Shift the high part and `or` what underflew on the low part
Scott Wakelingb77051e2016-11-21 19:46:00 +00004962 __ Rsb(temp, o_h, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01004963 __ Lsl(temp, high, temp);
4964 __ Orr(o_l, o_l, temp);
4965 // If the shift is > 32 bits, override the low part
Scott Wakelingb77051e2016-11-21 19:46:00 +00004966 __ Subs(temp, o_h, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01004967 {
Artem Serov0fb37192016-12-06 18:13:40 +00004968 ExactAssemblyScope guard(GetVIXLAssembler(),
4969 2 * vixl32::kMaxInstructionSizeInBytes,
4970 CodeBufferCheckScope::kMaximumSize);
Artem Serov02d37832016-10-25 15:25:33 +01004971 __ it(pl);
4972 __ asr(pl, o_l, high, temp);
4973 }
4974 // Shift the high part
4975 __ Asr(o_h, high, o_h);
4976 } else {
4977 __ And(o_h, second_reg, kMaxLongShiftDistance);
4978 // same as Shr except we use `Lsr`s and not `Asr`s
4979 __ Lsr(o_l, low, o_h);
Scott Wakelingb77051e2016-11-21 19:46:00 +00004980 __ Rsb(temp, o_h, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01004981 __ Lsl(temp, high, temp);
4982 __ Orr(o_l, o_l, temp);
Scott Wakelingb77051e2016-11-21 19:46:00 +00004983 __ Subs(temp, o_h, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01004984 {
Artem Serov0fb37192016-12-06 18:13:40 +00004985 ExactAssemblyScope guard(GetVIXLAssembler(),
4986 2 * vixl32::kMaxInstructionSizeInBytes,
4987 CodeBufferCheckScope::kMaximumSize);
Artem Serov02d37832016-10-25 15:25:33 +01004988 __ it(pl);
4989 __ lsr(pl, o_l, high, temp);
4990 }
4991 __ Lsr(o_h, high, o_h);
4992 }
4993 } else {
4994 // Register allocator doesn't create partial overlap.
4995 DCHECK(!o_l.Is(high));
4996 DCHECK(!o_h.Is(low));
Anton Kirilov644032c2016-12-06 17:51:43 +00004997 int32_t cst = Int32ConstantFrom(second);
Artem Serov02d37832016-10-25 15:25:33 +01004998 uint32_t shift_value = cst & kMaxLongShiftDistance;
4999 if (shift_value > 32) {
5000 if (op->IsShl()) {
5001 __ Lsl(o_h, low, shift_value - 32);
5002 __ Mov(o_l, 0);
5003 } else if (op->IsShr()) {
5004 __ Asr(o_l, high, shift_value - 32);
5005 __ Asr(o_h, high, 31);
5006 } else {
5007 __ Lsr(o_l, high, shift_value - 32);
5008 __ Mov(o_h, 0);
5009 }
5010 } else if (shift_value == 32) {
5011 if (op->IsShl()) {
5012 __ Mov(o_h, low);
5013 __ Mov(o_l, 0);
5014 } else if (op->IsShr()) {
5015 __ Mov(o_l, high);
5016 __ Asr(o_h, high, 31);
5017 } else {
5018 __ Mov(o_l, high);
5019 __ Mov(o_h, 0);
5020 }
5021 } else if (shift_value == 1) {
5022 if (op->IsShl()) {
5023 __ Lsls(o_l, low, 1);
5024 __ Adc(o_h, high, high);
5025 } else if (op->IsShr()) {
5026 __ Asrs(o_h, high, 1);
5027 __ Rrx(o_l, low);
5028 } else {
5029 __ Lsrs(o_h, high, 1);
5030 __ Rrx(o_l, low);
5031 }
5032 } else {
5033 DCHECK(2 <= shift_value && shift_value < 32) << shift_value;
5034 if (op->IsShl()) {
5035 __ Lsl(o_h, high, shift_value);
5036 __ Orr(o_h, o_h, Operand(low, ShiftType::LSR, 32 - shift_value));
5037 __ Lsl(o_l, low, shift_value);
5038 } else if (op->IsShr()) {
5039 __ Lsr(o_l, low, shift_value);
5040 __ Orr(o_l, o_l, Operand(high, ShiftType::LSL, 32 - shift_value));
5041 __ Asr(o_h, high, shift_value);
5042 } else {
5043 __ Lsr(o_l, low, shift_value);
5044 __ Orr(o_l, o_l, Operand(high, ShiftType::LSL, 32 - shift_value));
5045 __ Lsr(o_h, high, shift_value);
5046 }
5047 }
5048 }
5049 break;
5050 }
5051 default:
5052 LOG(FATAL) << "Unexpected operation type " << type;
5053 UNREACHABLE();
5054 }
5055}
5056
5057void LocationsBuilderARMVIXL::VisitShl(HShl* shl) {
5058 HandleShift(shl);
5059}
5060
5061void InstructionCodeGeneratorARMVIXL::VisitShl(HShl* shl) {
5062 HandleShift(shl);
5063}
5064
5065void LocationsBuilderARMVIXL::VisitShr(HShr* shr) {
5066 HandleShift(shr);
5067}
5068
5069void InstructionCodeGeneratorARMVIXL::VisitShr(HShr* shr) {
5070 HandleShift(shr);
5071}
5072
5073void LocationsBuilderARMVIXL::VisitUShr(HUShr* ushr) {
5074 HandleShift(ushr);
5075}
5076
5077void InstructionCodeGeneratorARMVIXL::VisitUShr(HUShr* ushr) {
5078 HandleShift(ushr);
5079}
5080
5081void LocationsBuilderARMVIXL::VisitNewInstance(HNewInstance* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005082 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5083 instruction, LocationSummary::kCallOnMainOnly);
Artem Serov02d37832016-10-25 15:25:33 +01005084 if (instruction->IsStringAlloc()) {
5085 locations->AddTemp(LocationFrom(kMethodRegister));
5086 } else {
5087 InvokeRuntimeCallingConventionARMVIXL calling_convention;
5088 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
Artem Serov02d37832016-10-25 15:25:33 +01005089 }
5090 locations->SetOut(LocationFrom(r0));
5091}
5092
5093void InstructionCodeGeneratorARMVIXL::VisitNewInstance(HNewInstance* instruction) {
5094 // Note: if heap poisoning is enabled, the entry point takes cares
5095 // of poisoning the reference.
5096 if (instruction->IsStringAlloc()) {
5097 // String is allocated through StringFactory. Call NewEmptyString entry point.
5098 vixl32::Register temp = RegisterFrom(instruction->GetLocations()->GetTemp(0));
5099 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize);
5100 GetAssembler()->LoadFromOffset(kLoadWord, temp, tr, QUICK_ENTRY_POINT(pNewEmptyString));
5101 GetAssembler()->LoadFromOffset(kLoadWord, lr, temp, code_offset.Int32Value());
Alexandre Rames374ddf32016-11-04 10:40:49 +00005102 // 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 +00005103 ExactAssemblyScope aas(GetVIXLAssembler(),
5104 vixl32::k16BitT32InstructionSizeInBytes,
5105 CodeBufferCheckScope::kExactSize);
Artem Serov02d37832016-10-25 15:25:33 +01005106 __ blx(lr);
5107 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
5108 } else {
5109 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00005110 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
Artem Serov02d37832016-10-25 15:25:33 +01005111 }
Roland Levillain5daa4952017-07-03 17:23:56 +01005112 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 10);
Artem Serov02d37832016-10-25 15:25:33 +01005113}
5114
5115void LocationsBuilderARMVIXL::VisitNewArray(HNewArray* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005116 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5117 instruction, LocationSummary::kCallOnMainOnly);
Artem Serov02d37832016-10-25 15:25:33 +01005118 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Artem Serov02d37832016-10-25 15:25:33 +01005119 locations->SetOut(LocationFrom(r0));
Nicolas Geoffray8c7c4f12017-01-26 10:13:11 +00005120 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5121 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
Artem Serov02d37832016-10-25 15:25:33 +01005122}
5123
5124void InstructionCodeGeneratorARMVIXL::VisitNewArray(HNewArray* instruction) {
Artem Serov02d37832016-10-25 15:25:33 +01005125 // Note: if heap poisoning is enabled, the entry point takes cares
5126 // of poisoning the reference.
Artem Serov7b3672e2017-02-03 17:30:34 +00005127 QuickEntrypointEnum entrypoint =
5128 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
5129 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005130 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Artem Serov7b3672e2017-02-03 17:30:34 +00005131 DCHECK(!codegen_->IsLeafMethod());
Roland Levillain5daa4952017-07-03 17:23:56 +01005132 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 11);
Artem Serov02d37832016-10-25 15:25:33 +01005133}
5134
5135void LocationsBuilderARMVIXL::VisitParameterValue(HParameterValue* instruction) {
5136 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005137 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Artem Serov02d37832016-10-25 15:25:33 +01005138 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
5139 if (location.IsStackSlot()) {
5140 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5141 } else if (location.IsDoubleStackSlot()) {
5142 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5143 }
5144 locations->SetOut(location);
5145}
5146
5147void InstructionCodeGeneratorARMVIXL::VisitParameterValue(
5148 HParameterValue* instruction ATTRIBUTE_UNUSED) {
5149 // Nothing to do, the parameter is already at its location.
5150}
5151
5152void LocationsBuilderARMVIXL::VisitCurrentMethod(HCurrentMethod* instruction) {
5153 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005154 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Artem Serov02d37832016-10-25 15:25:33 +01005155 locations->SetOut(LocationFrom(kMethodRegister));
5156}
5157
5158void InstructionCodeGeneratorARMVIXL::VisitCurrentMethod(
5159 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
5160 // Nothing to do, the method is already at its location.
5161}
5162
5163void LocationsBuilderARMVIXL::VisitNot(HNot* not_) {
5164 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005165 new (GetGraph()->GetAllocator()) LocationSummary(not_, LocationSummary::kNoCall);
Artem Serov02d37832016-10-25 15:25:33 +01005166 locations->SetInAt(0, Location::RequiresRegister());
5167 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5168}
5169
5170void InstructionCodeGeneratorARMVIXL::VisitNot(HNot* not_) {
5171 LocationSummary* locations = not_->GetLocations();
5172 Location out = locations->Out();
5173 Location in = locations->InAt(0);
5174 switch (not_->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005175 case DataType::Type::kInt32:
Artem Serov02d37832016-10-25 15:25:33 +01005176 __ Mvn(OutputRegister(not_), InputRegisterAt(not_, 0));
5177 break;
5178
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005179 case DataType::Type::kInt64:
Artem Serov02d37832016-10-25 15:25:33 +01005180 __ Mvn(LowRegisterFrom(out), LowRegisterFrom(in));
5181 __ Mvn(HighRegisterFrom(out), HighRegisterFrom(in));
5182 break;
5183
5184 default:
5185 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
5186 }
5187}
5188
Scott Wakelingc34dba72016-10-03 10:14:44 +01005189void LocationsBuilderARMVIXL::VisitBooleanNot(HBooleanNot* bool_not) {
5190 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005191 new (GetGraph()->GetAllocator()) LocationSummary(bool_not, LocationSummary::kNoCall);
Scott Wakelingc34dba72016-10-03 10:14:44 +01005192 locations->SetInAt(0, Location::RequiresRegister());
5193 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5194}
5195
5196void InstructionCodeGeneratorARMVIXL::VisitBooleanNot(HBooleanNot* bool_not) {
5197 __ Eor(OutputRegister(bool_not), InputRegister(bool_not), 1);
5198}
5199
Artem Serov02d37832016-10-25 15:25:33 +01005200void LocationsBuilderARMVIXL::VisitCompare(HCompare* compare) {
5201 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005202 new (GetGraph()->GetAllocator()) LocationSummary(compare, LocationSummary::kNoCall);
Artem Serov02d37832016-10-25 15:25:33 +01005203 switch (compare->InputAt(0)->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005204 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005205 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005206 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005207 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005208 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005209 case DataType::Type::kInt32:
5210 case DataType::Type::kInt64: {
Artem Serov02d37832016-10-25 15:25:33 +01005211 locations->SetInAt(0, Location::RequiresRegister());
5212 locations->SetInAt(1, Location::RequiresRegister());
5213 // Output overlaps because it is written before doing the low comparison.
5214 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
5215 break;
5216 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005217 case DataType::Type::kFloat32:
5218 case DataType::Type::kFloat64: {
Artem Serov02d37832016-10-25 15:25:33 +01005219 locations->SetInAt(0, Location::RequiresFpuRegister());
5220 locations->SetInAt(1, ArithmeticZeroOrFpuRegister(compare->InputAt(1)));
5221 locations->SetOut(Location::RequiresRegister());
5222 break;
5223 }
5224 default:
5225 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
5226 }
5227}
5228
5229void InstructionCodeGeneratorARMVIXL::VisitCompare(HCompare* compare) {
5230 LocationSummary* locations = compare->GetLocations();
5231 vixl32::Register out = OutputRegister(compare);
5232 Location left = locations->InAt(0);
5233 Location right = locations->InAt(1);
5234
5235 vixl32::Label less, greater, done;
Anton Kirilov6f644202017-02-27 18:29:45 +00005236 vixl32::Label* final_label = codegen_->GetFinalLabel(compare, &done);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005237 DataType::Type type = compare->InputAt(0)->GetType();
Vladimir Marko33bff252017-11-01 14:35:42 +00005238 vixl32::Condition less_cond = vixl32::Condition::None();
Artem Serov02d37832016-10-25 15:25:33 +01005239 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005240 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005241 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005242 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005243 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005244 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005245 case DataType::Type::kInt32: {
Artem Serov02d37832016-10-25 15:25:33 +01005246 // Emit move to `out` before the `Cmp`, as `Mov` might affect the status flags.
5247 __ Mov(out, 0);
5248 __ Cmp(RegisterFrom(left), RegisterFrom(right)); // Signed compare.
5249 less_cond = lt;
5250 break;
5251 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005252 case DataType::Type::kInt64: {
Artem Serov02d37832016-10-25 15:25:33 +01005253 __ Cmp(HighRegisterFrom(left), HighRegisterFrom(right)); // Signed compare.
Artem Serov517d9f62016-12-12 15:51:15 +00005254 __ B(lt, &less, /* far_target */ false);
5255 __ B(gt, &greater, /* far_target */ false);
Artem Serov02d37832016-10-25 15:25:33 +01005256 // Emit move to `out` before the last `Cmp`, as `Mov` might affect the status flags.
5257 __ Mov(out, 0);
5258 __ Cmp(LowRegisterFrom(left), LowRegisterFrom(right)); // Unsigned compare.
5259 less_cond = lo;
5260 break;
5261 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005262 case DataType::Type::kFloat32:
5263 case DataType::Type::kFloat64: {
Artem Serov02d37832016-10-25 15:25:33 +01005264 __ Mov(out, 0);
Donghui Bai426b49c2016-11-08 14:55:38 +08005265 GenerateVcmp(compare, codegen_);
Artem Serov02d37832016-10-25 15:25:33 +01005266 // To branch on the FP compare result we transfer FPSCR to APSR (encoded as PC in VMRS).
5267 __ Vmrs(RegisterOrAPSR_nzcv(kPcCode), FPSCR);
5268 less_cond = ARMFPCondition(kCondLT, compare->IsGtBias());
5269 break;
5270 }
5271 default:
5272 LOG(FATAL) << "Unexpected compare type " << type;
5273 UNREACHABLE();
5274 }
5275
Anton Kirilov6f644202017-02-27 18:29:45 +00005276 __ B(eq, final_label, /* far_target */ false);
Artem Serov517d9f62016-12-12 15:51:15 +00005277 __ B(less_cond, &less, /* far_target */ false);
Artem Serov02d37832016-10-25 15:25:33 +01005278
5279 __ Bind(&greater);
5280 __ Mov(out, 1);
Anton Kirilov6f644202017-02-27 18:29:45 +00005281 __ B(final_label);
Artem Serov02d37832016-10-25 15:25:33 +01005282
5283 __ Bind(&less);
5284 __ Mov(out, -1);
5285
Anton Kirilov6f644202017-02-27 18:29:45 +00005286 if (done.IsReferenced()) {
5287 __ Bind(&done);
5288 }
Artem Serov02d37832016-10-25 15:25:33 +01005289}
5290
5291void LocationsBuilderARMVIXL::VisitPhi(HPhi* instruction) {
5292 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005293 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Artem Serov02d37832016-10-25 15:25:33 +01005294 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
5295 locations->SetInAt(i, Location::Any());
5296 }
5297 locations->SetOut(Location::Any());
5298}
5299
5300void InstructionCodeGeneratorARMVIXL::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
5301 LOG(FATAL) << "Unreachable";
5302}
5303
5304void CodeGeneratorARMVIXL::GenerateMemoryBarrier(MemBarrierKind kind) {
5305 // TODO (ported from quick): revisit ARM barrier kinds.
5306 DmbOptions flavor = DmbOptions::ISH; // Quiet C++ warnings.
5307 switch (kind) {
5308 case MemBarrierKind::kAnyStore:
5309 case MemBarrierKind::kLoadAny:
5310 case MemBarrierKind::kAnyAny: {
5311 flavor = DmbOptions::ISH;
5312 break;
5313 }
5314 case MemBarrierKind::kStoreStore: {
5315 flavor = DmbOptions::ISHST;
5316 break;
5317 }
5318 default:
5319 LOG(FATAL) << "Unexpected memory barrier " << kind;
5320 }
5321 __ Dmb(flavor);
5322}
5323
5324void InstructionCodeGeneratorARMVIXL::GenerateWideAtomicLoad(vixl32::Register addr,
5325 uint32_t offset,
5326 vixl32::Register out_lo,
5327 vixl32::Register out_hi) {
5328 UseScratchRegisterScope temps(GetVIXLAssembler());
5329 if (offset != 0) {
5330 vixl32::Register temp = temps.Acquire();
5331 __ Add(temp, addr, offset);
5332 addr = temp;
5333 }
Scott Wakelingb77051e2016-11-21 19:46:00 +00005334 __ Ldrexd(out_lo, out_hi, MemOperand(addr));
Artem Serov02d37832016-10-25 15:25:33 +01005335}
5336
5337void InstructionCodeGeneratorARMVIXL::GenerateWideAtomicStore(vixl32::Register addr,
5338 uint32_t offset,
5339 vixl32::Register value_lo,
5340 vixl32::Register value_hi,
5341 vixl32::Register temp1,
5342 vixl32::Register temp2,
5343 HInstruction* instruction) {
5344 UseScratchRegisterScope temps(GetVIXLAssembler());
5345 vixl32::Label fail;
5346 if (offset != 0) {
5347 vixl32::Register temp = temps.Acquire();
5348 __ Add(temp, addr, offset);
5349 addr = temp;
5350 }
5351 __ Bind(&fail);
Alexandre Rames374ddf32016-11-04 10:40:49 +00005352 {
5353 // Ensure the pc position is recorded immediately after the `ldrexd` instruction.
Artem Serov0fb37192016-12-06 18:13:40 +00005354 ExactAssemblyScope aas(GetVIXLAssembler(),
5355 vixl32::kMaxInstructionSizeInBytes,
5356 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00005357 // We need a load followed by store. (The address used in a STREX instruction must
5358 // be the same as the address in the most recently executed LDREX instruction.)
5359 __ ldrexd(temp1, temp2, MemOperand(addr));
5360 codegen_->MaybeRecordImplicitNullCheck(instruction);
5361 }
Scott Wakelingb77051e2016-11-21 19:46:00 +00005362 __ Strexd(temp1, value_lo, value_hi, MemOperand(addr));
xueliang.zhongf51bc622016-11-04 09:23:32 +00005363 __ CompareAndBranchIfNonZero(temp1, &fail);
Artem Serov02d37832016-10-25 15:25:33 +01005364}
Artem Serov02109dd2016-09-23 17:17:54 +01005365
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005366void LocationsBuilderARMVIXL::HandleFieldSet(
5367 HInstruction* instruction, const FieldInfo& field_info) {
5368 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
5369
5370 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005371 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005372 locations->SetInAt(0, Location::RequiresRegister());
5373
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005374 DataType::Type field_type = field_info.GetFieldType();
5375 if (DataType::IsFloatingPointType(field_type)) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005376 locations->SetInAt(1, Location::RequiresFpuRegister());
5377 } else {
5378 locations->SetInAt(1, Location::RequiresRegister());
5379 }
5380
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005381 bool is_wide = field_type == DataType::Type::kInt64 || field_type == DataType::Type::kFloat64;
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005382 bool generate_volatile = field_info.IsVolatile()
5383 && is_wide
5384 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
5385 bool needs_write_barrier =
5386 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
5387 // Temporary registers for the write barrier.
5388 // TODO: consider renaming StoreNeedsWriteBarrier to StoreNeedsGCMark.
5389 if (needs_write_barrier) {
5390 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
5391 locations->AddTemp(Location::RequiresRegister());
5392 } else if (generate_volatile) {
5393 // ARM encoding have some additional constraints for ldrexd/strexd:
5394 // - registers need to be consecutive
5395 // - the first register should be even but not R14.
5396 // We don't test for ARM yet, and the assertion makes sure that we
5397 // revisit this if we ever enable ARM encoding.
5398 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
5399
5400 locations->AddTemp(Location::RequiresRegister());
5401 locations->AddTemp(Location::RequiresRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005402 if (field_type == DataType::Type::kFloat64) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005403 // For doubles we need two more registers to copy the value.
5404 locations->AddTemp(LocationFrom(r2));
5405 locations->AddTemp(LocationFrom(r3));
5406 }
5407 }
5408}
5409
5410void InstructionCodeGeneratorARMVIXL::HandleFieldSet(HInstruction* instruction,
5411 const FieldInfo& field_info,
5412 bool value_can_be_null) {
5413 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
5414
5415 LocationSummary* locations = instruction->GetLocations();
5416 vixl32::Register base = InputRegisterAt(instruction, 0);
5417 Location value = locations->InAt(1);
5418
5419 bool is_volatile = field_info.IsVolatile();
5420 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005421 DataType::Type field_type = field_info.GetFieldType();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005422 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
5423 bool needs_write_barrier =
5424 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
5425
5426 if (is_volatile) {
5427 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
5428 }
5429
5430 switch (field_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005431 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005432 case DataType::Type::kUint8:
5433 case DataType::Type::kInt8:
5434 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005435 case DataType::Type::kInt16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005436 case DataType::Type::kInt32: {
5437 StoreOperandType operand_type = GetStoreOperandType(field_type);
5438 GetAssembler()->StoreToOffset(operand_type, RegisterFrom(value), base, offset);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005439 break;
5440 }
5441
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005442 case DataType::Type::kReference: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005443 if (kPoisonHeapReferences && needs_write_barrier) {
5444 // Note that in the case where `value` is a null reference,
5445 // we do not enter this block, as a null reference does not
5446 // need poisoning.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005447 DCHECK_EQ(field_type, DataType::Type::kReference);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005448 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
5449 __ Mov(temp, RegisterFrom(value));
5450 GetAssembler()->PoisonHeapReference(temp);
5451 GetAssembler()->StoreToOffset(kStoreWord, temp, base, offset);
5452 } else {
5453 GetAssembler()->StoreToOffset(kStoreWord, RegisterFrom(value), base, offset);
5454 }
5455 break;
5456 }
5457
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005458 case DataType::Type::kInt64: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005459 if (is_volatile && !atomic_ldrd_strd) {
5460 GenerateWideAtomicStore(base,
5461 offset,
5462 LowRegisterFrom(value),
5463 HighRegisterFrom(value),
5464 RegisterFrom(locations->GetTemp(0)),
5465 RegisterFrom(locations->GetTemp(1)),
5466 instruction);
5467 } else {
5468 GetAssembler()->StoreToOffset(kStoreWordPair, LowRegisterFrom(value), base, offset);
5469 codegen_->MaybeRecordImplicitNullCheck(instruction);
5470 }
5471 break;
5472 }
5473
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005474 case DataType::Type::kFloat32: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005475 GetAssembler()->StoreSToOffset(SRegisterFrom(value), base, offset);
5476 break;
5477 }
5478
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005479 case DataType::Type::kFloat64: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01005480 vixl32::DRegister value_reg = DRegisterFrom(value);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005481 if (is_volatile && !atomic_ldrd_strd) {
5482 vixl32::Register value_reg_lo = RegisterFrom(locations->GetTemp(0));
5483 vixl32::Register value_reg_hi = RegisterFrom(locations->GetTemp(1));
5484
5485 __ Vmov(value_reg_lo, value_reg_hi, value_reg);
5486
5487 GenerateWideAtomicStore(base,
5488 offset,
5489 value_reg_lo,
5490 value_reg_hi,
5491 RegisterFrom(locations->GetTemp(2)),
5492 RegisterFrom(locations->GetTemp(3)),
5493 instruction);
5494 } else {
5495 GetAssembler()->StoreDToOffset(value_reg, base, offset);
5496 codegen_->MaybeRecordImplicitNullCheck(instruction);
5497 }
5498 break;
5499 }
5500
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005501 case DataType::Type::kVoid:
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005502 LOG(FATAL) << "Unreachable type " << field_type;
5503 UNREACHABLE();
5504 }
5505
5506 // Longs and doubles are handled in the switch.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005507 if (field_type != DataType::Type::kInt64 && field_type != DataType::Type::kFloat64) {
Alexandre Rames374ddf32016-11-04 10:40:49 +00005508 // TODO(VIXL): Here and for other calls to `MaybeRecordImplicitNullCheck` in this method, we
5509 // should use a scope and the assembler to emit the store instruction to guarantee that we
5510 // record the pc at the correct position. But the `Assembler` does not automatically handle
5511 // unencodable offsets. Practically, everything is fine because the helper and VIXL, at the time
5512 // of writing, do generate the store instruction last.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005513 codegen_->MaybeRecordImplicitNullCheck(instruction);
5514 }
5515
5516 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
5517 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
5518 vixl32::Register card = RegisterFrom(locations->GetTemp(1));
5519 codegen_->MarkGCCard(temp, card, base, RegisterFrom(value), value_can_be_null);
5520 }
5521
5522 if (is_volatile) {
5523 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
5524 }
5525}
5526
Artem Serov02d37832016-10-25 15:25:33 +01005527void LocationsBuilderARMVIXL::HandleFieldGet(HInstruction* instruction,
5528 const FieldInfo& field_info) {
5529 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
5530
5531 bool object_field_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005532 kEmitCompilerReadBarrier && (field_info.GetFieldType() == DataType::Type::kReference);
Artem Serov02d37832016-10-25 15:25:33 +01005533 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005534 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
5535 object_field_get_with_read_barrier
5536 ? LocationSummary::kCallOnSlowPath
5537 : LocationSummary::kNoCall);
Artem Serov02d37832016-10-25 15:25:33 +01005538 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
5539 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
5540 }
5541 locations->SetInAt(0, Location::RequiresRegister());
5542
5543 bool volatile_for_double = field_info.IsVolatile()
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005544 && (field_info.GetFieldType() == DataType::Type::kFloat64)
Artem Serov02d37832016-10-25 15:25:33 +01005545 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
5546 // The output overlaps in case of volatile long: we don't want the
5547 // code generated by GenerateWideAtomicLoad to overwrite the
5548 // object's location. Likewise, in the case of an object field get
5549 // with read barriers enabled, we do not want the load to overwrite
5550 // the object's location, as we need it to emit the read barrier.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005551 bool overlap =
5552 (field_info.IsVolatile() && (field_info.GetFieldType() == DataType::Type::kInt64)) ||
Artem Serov02d37832016-10-25 15:25:33 +01005553 object_field_get_with_read_barrier;
5554
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005555 if (DataType::IsFloatingPointType(instruction->GetType())) {
Artem Serov02d37832016-10-25 15:25:33 +01005556 locations->SetOut(Location::RequiresFpuRegister());
5557 } else {
5558 locations->SetOut(Location::RequiresRegister(),
5559 (overlap ? Location::kOutputOverlap : Location::kNoOutputOverlap));
5560 }
5561 if (volatile_for_double) {
5562 // ARM encoding have some additional constraints for ldrexd/strexd:
5563 // - registers need to be consecutive
5564 // - the first register should be even but not R14.
5565 // We don't test for ARM yet, and the assertion makes sure that we
5566 // revisit this if we ever enable ARM encoding.
5567 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
5568 locations->AddTemp(Location::RequiresRegister());
5569 locations->AddTemp(Location::RequiresRegister());
5570 } else if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
5571 // We need a temporary register for the read barrier marking slow
Artem Serovc5fcb442016-12-02 19:19:58 +00005572 // path in CodeGeneratorARMVIXL::GenerateFieldLoadWithBakerReadBarrier.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01005573 if (kBakerReadBarrierLinkTimeThunksEnableForFields &&
5574 !Runtime::Current()->UseJitCompilation()) {
5575 // If link-time thunks for the Baker read barrier are enabled, for AOT
5576 // loads we need a temporary only if the offset is too big.
5577 if (field_info.GetFieldOffset().Uint32Value() >= kReferenceLoadMinFarOffset) {
5578 locations->AddTemp(Location::RequiresRegister());
5579 }
5580 // And we always need the reserved entrypoint register.
5581 locations->AddTemp(Location::RegisterLocation(kBakerCcEntrypointRegister.GetCode()));
5582 } else {
5583 locations->AddTemp(Location::RequiresRegister());
5584 }
Artem Serov02d37832016-10-25 15:25:33 +01005585 }
5586}
5587
5588Location LocationsBuilderARMVIXL::ArithmeticZeroOrFpuRegister(HInstruction* input) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005589 DCHECK(DataType::IsFloatingPointType(input->GetType())) << input->GetType();
Artem Serov02d37832016-10-25 15:25:33 +01005590 if ((input->IsFloatConstant() && (input->AsFloatConstant()->IsArithmeticZero())) ||
5591 (input->IsDoubleConstant() && (input->AsDoubleConstant()->IsArithmeticZero()))) {
5592 return Location::ConstantLocation(input->AsConstant());
5593 } else {
5594 return Location::RequiresFpuRegister();
5595 }
5596}
5597
Artem Serov02109dd2016-09-23 17:17:54 +01005598Location LocationsBuilderARMVIXL::ArmEncodableConstantOrRegister(HInstruction* constant,
5599 Opcode opcode) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005600 DCHECK(!DataType::IsFloatingPointType(constant->GetType()));
Artem Serov02109dd2016-09-23 17:17:54 +01005601 if (constant->IsConstant() &&
5602 CanEncodeConstantAsImmediate(constant->AsConstant(), opcode)) {
5603 return Location::ConstantLocation(constant->AsConstant());
5604 }
5605 return Location::RequiresRegister();
5606}
5607
Vladimir Markof0a6a1d2018-01-08 14:23:56 +00005608static bool CanEncode32BitConstantAsImmediate(
5609 CodeGeneratorARMVIXL* codegen,
5610 uint32_t value,
5611 Opcode opcode,
5612 vixl32::FlagsUpdate flags_update = vixl32::FlagsUpdate::DontCare) {
5613 ArmVIXLAssembler* assembler = codegen->GetAssembler();
5614 if (assembler->ShifterOperandCanHold(opcode, value, flags_update)) {
Artem Serov02109dd2016-09-23 17:17:54 +01005615 return true;
5616 }
5617 Opcode neg_opcode = kNoOperand;
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00005618 uint32_t neg_value = 0;
Artem Serov02109dd2016-09-23 17:17:54 +01005619 switch (opcode) {
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00005620 case AND: neg_opcode = BIC; neg_value = ~value; break;
5621 case ORR: neg_opcode = ORN; neg_value = ~value; break;
5622 case ADD: neg_opcode = SUB; neg_value = -value; break;
5623 case ADC: neg_opcode = SBC; neg_value = ~value; break;
5624 case SUB: neg_opcode = ADD; neg_value = -value; break;
5625 case SBC: neg_opcode = ADC; neg_value = ~value; break;
5626 case MOV: neg_opcode = MVN; neg_value = ~value; break;
Artem Serov02109dd2016-09-23 17:17:54 +01005627 default:
5628 return false;
5629 }
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00005630
Vladimir Markof0a6a1d2018-01-08 14:23:56 +00005631 if (assembler->ShifterOperandCanHold(neg_opcode, neg_value, flags_update)) {
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00005632 return true;
5633 }
5634
5635 return opcode == AND && IsPowerOfTwo(value + 1);
Artem Serov02109dd2016-09-23 17:17:54 +01005636}
5637
Vladimir Markof0a6a1d2018-01-08 14:23:56 +00005638bool LocationsBuilderARMVIXL::CanEncodeConstantAsImmediate(HConstant* input_cst, Opcode opcode) {
5639 uint64_t value = static_cast<uint64_t>(Int64FromConstant(input_cst));
5640 if (DataType::Is64BitType(input_cst->GetType())) {
5641 Opcode high_opcode = opcode;
5642 vixl32::FlagsUpdate low_flags_update = vixl32::FlagsUpdate::DontCare;
5643 switch (opcode) {
5644 case SUB:
5645 // Flip the operation to an ADD.
5646 value = -value;
5647 opcode = ADD;
5648 FALLTHROUGH_INTENDED;
5649 case ADD:
5650 if (Low32Bits(value) == 0u) {
5651 return CanEncode32BitConstantAsImmediate(codegen_, High32Bits(value), opcode);
5652 }
5653 high_opcode = ADC;
5654 low_flags_update = vixl32::FlagsUpdate::SetFlags;
5655 break;
5656 default:
5657 break;
5658 }
5659 return CanEncode32BitConstantAsImmediate(codegen_, High32Bits(value), high_opcode) &&
5660 CanEncode32BitConstantAsImmediate(codegen_, Low32Bits(value), opcode, low_flags_update);
5661 } else {
5662 return CanEncode32BitConstantAsImmediate(codegen_, Low32Bits(value), opcode);
5663 }
5664}
5665
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005666void InstructionCodeGeneratorARMVIXL::HandleFieldGet(HInstruction* instruction,
5667 const FieldInfo& field_info) {
5668 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
5669
5670 LocationSummary* locations = instruction->GetLocations();
5671 vixl32::Register base = InputRegisterAt(instruction, 0);
5672 Location out = locations->Out();
5673 bool is_volatile = field_info.IsVolatile();
5674 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Vladimir Marko61b92282017-10-11 13:23:17 +01005675 DCHECK_EQ(DataType::Size(field_info.GetFieldType()), DataType::Size(instruction->GetType()));
5676 DataType::Type load_type = instruction->GetType();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005677 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
5678
Vladimir Marko61b92282017-10-11 13:23:17 +01005679 switch (load_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005680 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005681 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005682 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005683 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005684 case DataType::Type::kInt16:
5685 case DataType::Type::kInt32: {
Vladimir Marko61b92282017-10-11 13:23:17 +01005686 LoadOperandType operand_type = GetLoadOperandType(load_type);
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005687 GetAssembler()->LoadFromOffset(operand_type, RegisterFrom(out), base, offset);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005688 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005689 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005690
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005691 case DataType::Type::kReference: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005692 // /* HeapReference<Object> */ out = *(base + offset)
5693 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005694 Location temp_loc = locations->GetTemp(0);
5695 // Note that a potential implicit null check is handled in this
5696 // CodeGeneratorARMVIXL::GenerateFieldLoadWithBakerReadBarrier call.
5697 codegen_->GenerateFieldLoadWithBakerReadBarrier(
5698 instruction, out, base, offset, temp_loc, /* needs_null_check */ true);
5699 if (is_volatile) {
5700 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5701 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005702 } else {
5703 GetAssembler()->LoadFromOffset(kLoadWord, RegisterFrom(out), base, offset);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005704 codegen_->MaybeRecordImplicitNullCheck(instruction);
5705 if (is_volatile) {
5706 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5707 }
5708 // If read barriers are enabled, emit read barriers other than
5709 // Baker's using a slow path (and also unpoison the loaded
5710 // reference, if heap poisoning is enabled).
5711 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, locations->InAt(0), offset);
5712 }
5713 break;
5714 }
5715
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005716 case DataType::Type::kInt64:
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005717 if (is_volatile && !atomic_ldrd_strd) {
5718 GenerateWideAtomicLoad(base, offset, LowRegisterFrom(out), HighRegisterFrom(out));
5719 } else {
5720 GetAssembler()->LoadFromOffset(kLoadWordPair, LowRegisterFrom(out), base, offset);
5721 }
5722 break;
5723
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005724 case DataType::Type::kFloat32:
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005725 GetAssembler()->LoadSFromOffset(SRegisterFrom(out), base, offset);
5726 break;
5727
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005728 case DataType::Type::kFloat64: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01005729 vixl32::DRegister out_dreg = DRegisterFrom(out);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005730 if (is_volatile && !atomic_ldrd_strd) {
5731 vixl32::Register lo = RegisterFrom(locations->GetTemp(0));
5732 vixl32::Register hi = RegisterFrom(locations->GetTemp(1));
5733 GenerateWideAtomicLoad(base, offset, lo, hi);
5734 // TODO(VIXL): Do we need to be immediately after the ldrexd instruction? If so we need a
5735 // scope.
5736 codegen_->MaybeRecordImplicitNullCheck(instruction);
5737 __ Vmov(out_dreg, lo, hi);
5738 } else {
5739 GetAssembler()->LoadDFromOffset(out_dreg, base, offset);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005740 codegen_->MaybeRecordImplicitNullCheck(instruction);
5741 }
5742 break;
5743 }
5744
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005745 case DataType::Type::kVoid:
Vladimir Marko61b92282017-10-11 13:23:17 +01005746 LOG(FATAL) << "Unreachable type " << load_type;
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005747 UNREACHABLE();
5748 }
5749
Vladimir Marko61b92282017-10-11 13:23:17 +01005750 if (load_type == DataType::Type::kReference || load_type == DataType::Type::kFloat64) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005751 // Potential implicit null checks, in the case of reference or
5752 // double fields, are handled in the previous switch statement.
5753 } else {
5754 // Address cases other than reference and double that may require an implicit null check.
Alexandre Rames374ddf32016-11-04 10:40:49 +00005755 // TODO(VIXL): Here and for other calls to `MaybeRecordImplicitNullCheck` in this method, we
5756 // should use a scope and the assembler to emit the load instruction to guarantee that we
5757 // record the pc at the correct position. But the `Assembler` does not automatically handle
5758 // unencodable offsets. Practically, everything is fine because the helper and VIXL, at the time
5759 // of writing, do generate the store instruction last.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005760 codegen_->MaybeRecordImplicitNullCheck(instruction);
5761 }
5762
5763 if (is_volatile) {
Vladimir Marko61b92282017-10-11 13:23:17 +01005764 if (load_type == DataType::Type::kReference) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005765 // Memory barriers, in the case of references, are also handled
5766 // in the previous switch statement.
5767 } else {
5768 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5769 }
5770 }
5771}
5772
5773void LocationsBuilderARMVIXL::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
5774 HandleFieldSet(instruction, instruction->GetFieldInfo());
5775}
5776
5777void InstructionCodeGeneratorARMVIXL::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
5778 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
5779}
5780
5781void LocationsBuilderARMVIXL::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5782 HandleFieldGet(instruction, instruction->GetFieldInfo());
5783}
5784
5785void InstructionCodeGeneratorARMVIXL::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5786 HandleFieldGet(instruction, instruction->GetFieldInfo());
5787}
5788
5789void LocationsBuilderARMVIXL::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5790 HandleFieldGet(instruction, instruction->GetFieldInfo());
5791}
5792
5793void InstructionCodeGeneratorARMVIXL::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5794 HandleFieldGet(instruction, instruction->GetFieldInfo());
5795}
5796
Scott Wakelingc34dba72016-10-03 10:14:44 +01005797void LocationsBuilderARMVIXL::VisitStaticFieldSet(HStaticFieldSet* instruction) {
5798 HandleFieldSet(instruction, instruction->GetFieldInfo());
5799}
5800
5801void InstructionCodeGeneratorARMVIXL::VisitStaticFieldSet(HStaticFieldSet* instruction) {
5802 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
5803}
5804
Artem Serovcfbe9132016-10-14 15:58:56 +01005805void LocationsBuilderARMVIXL::VisitUnresolvedInstanceFieldGet(
5806 HUnresolvedInstanceFieldGet* instruction) {
5807 FieldAccessCallingConventionARMVIXL calling_convention;
5808 codegen_->CreateUnresolvedFieldLocationSummary(
5809 instruction, instruction->GetFieldType(), calling_convention);
5810}
5811
5812void InstructionCodeGeneratorARMVIXL::VisitUnresolvedInstanceFieldGet(
5813 HUnresolvedInstanceFieldGet* instruction) {
5814 FieldAccessCallingConventionARMVIXL calling_convention;
5815 codegen_->GenerateUnresolvedFieldAccess(instruction,
5816 instruction->GetFieldType(),
5817 instruction->GetFieldIndex(),
5818 instruction->GetDexPc(),
5819 calling_convention);
5820}
5821
5822void LocationsBuilderARMVIXL::VisitUnresolvedInstanceFieldSet(
5823 HUnresolvedInstanceFieldSet* instruction) {
5824 FieldAccessCallingConventionARMVIXL calling_convention;
5825 codegen_->CreateUnresolvedFieldLocationSummary(
5826 instruction, instruction->GetFieldType(), calling_convention);
5827}
5828
5829void InstructionCodeGeneratorARMVIXL::VisitUnresolvedInstanceFieldSet(
5830 HUnresolvedInstanceFieldSet* instruction) {
5831 FieldAccessCallingConventionARMVIXL calling_convention;
5832 codegen_->GenerateUnresolvedFieldAccess(instruction,
5833 instruction->GetFieldType(),
5834 instruction->GetFieldIndex(),
5835 instruction->GetDexPc(),
5836 calling_convention);
5837}
5838
5839void LocationsBuilderARMVIXL::VisitUnresolvedStaticFieldGet(
5840 HUnresolvedStaticFieldGet* instruction) {
5841 FieldAccessCallingConventionARMVIXL calling_convention;
5842 codegen_->CreateUnresolvedFieldLocationSummary(
5843 instruction, instruction->GetFieldType(), calling_convention);
5844}
5845
5846void InstructionCodeGeneratorARMVIXL::VisitUnresolvedStaticFieldGet(
5847 HUnresolvedStaticFieldGet* instruction) {
5848 FieldAccessCallingConventionARMVIXL calling_convention;
5849 codegen_->GenerateUnresolvedFieldAccess(instruction,
5850 instruction->GetFieldType(),
5851 instruction->GetFieldIndex(),
5852 instruction->GetDexPc(),
5853 calling_convention);
5854}
5855
5856void LocationsBuilderARMVIXL::VisitUnresolvedStaticFieldSet(
5857 HUnresolvedStaticFieldSet* instruction) {
5858 FieldAccessCallingConventionARMVIXL calling_convention;
5859 codegen_->CreateUnresolvedFieldLocationSummary(
5860 instruction, instruction->GetFieldType(), calling_convention);
5861}
5862
5863void InstructionCodeGeneratorARMVIXL::VisitUnresolvedStaticFieldSet(
5864 HUnresolvedStaticFieldSet* instruction) {
5865 FieldAccessCallingConventionARMVIXL calling_convention;
5866 codegen_->GenerateUnresolvedFieldAccess(instruction,
5867 instruction->GetFieldType(),
5868 instruction->GetFieldIndex(),
5869 instruction->GetDexPc(),
5870 calling_convention);
5871}
5872
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005873void LocationsBuilderARMVIXL::VisitNullCheck(HNullCheck* instruction) {
Artem Serov657022c2016-11-23 14:19:38 +00005874 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005875 locations->SetInAt(0, Location::RequiresRegister());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005876}
5877
5878void CodeGeneratorARMVIXL::GenerateImplicitNullCheck(HNullCheck* instruction) {
5879 if (CanMoveNullCheckToUser(instruction)) {
5880 return;
5881 }
5882
5883 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames374ddf32016-11-04 10:40:49 +00005884 // Ensure the pc position is recorded immediately after the `ldr` instruction.
Artem Serov0fb37192016-12-06 18:13:40 +00005885 ExactAssemblyScope aas(GetVIXLAssembler(),
5886 vixl32::kMaxInstructionSizeInBytes,
5887 CodeBufferCheckScope::kMaximumSize);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005888 __ ldr(temps.Acquire(), MemOperand(InputRegisterAt(instruction, 0)));
5889 RecordPcInfo(instruction, instruction->GetDexPc());
5890}
5891
5892void CodeGeneratorARMVIXL::GenerateExplicitNullCheck(HNullCheck* instruction) {
5893 NullCheckSlowPathARMVIXL* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01005894 new (GetScopedAllocator()) NullCheckSlowPathARMVIXL(instruction);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005895 AddSlowPath(slow_path);
xueliang.zhongf51bc622016-11-04 09:23:32 +00005896 __ CompareAndBranchIfZero(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005897}
5898
5899void InstructionCodeGeneratorARMVIXL::VisitNullCheck(HNullCheck* instruction) {
5900 codegen_->GenerateNullCheck(instruction);
5901}
5902
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005903void CodeGeneratorARMVIXL::LoadFromShiftedRegOffset(DataType::Type type,
Scott Wakelingc34dba72016-10-03 10:14:44 +01005904 Location out_loc,
5905 vixl32::Register base,
5906 vixl32::Register reg_index,
5907 vixl32::Condition cond) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005908 uint32_t shift_count = DataType::SizeShift(type);
Scott Wakelingc34dba72016-10-03 10:14:44 +01005909 MemOperand mem_address(base, reg_index, vixl32::LSL, shift_count);
5910
5911 switch (type) {
Vladimir Marko61b92282017-10-11 13:23:17 +01005912 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005913 case DataType::Type::kUint8:
Vladimir Marko61b92282017-10-11 13:23:17 +01005914 __ Ldrb(cond, RegisterFrom(out_loc), mem_address);
5915 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005916 case DataType::Type::kInt8:
Scott Wakelingc34dba72016-10-03 10:14:44 +01005917 __ Ldrsb(cond, RegisterFrom(out_loc), mem_address);
5918 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005919 case DataType::Type::kUint16:
Scott Wakelingc34dba72016-10-03 10:14:44 +01005920 __ Ldrh(cond, RegisterFrom(out_loc), mem_address);
5921 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005922 case DataType::Type::kInt16:
5923 __ Ldrsh(cond, RegisterFrom(out_loc), mem_address);
5924 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005925 case DataType::Type::kReference:
5926 case DataType::Type::kInt32:
Scott Wakelingc34dba72016-10-03 10:14:44 +01005927 __ Ldr(cond, RegisterFrom(out_loc), mem_address);
5928 break;
5929 // T32 doesn't support LoadFromShiftedRegOffset mem address mode for these types.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005930 case DataType::Type::kInt64:
5931 case DataType::Type::kFloat32:
5932 case DataType::Type::kFloat64:
Scott Wakelingc34dba72016-10-03 10:14:44 +01005933 default:
5934 LOG(FATAL) << "Unreachable type " << type;
5935 UNREACHABLE();
5936 }
5937}
5938
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005939void CodeGeneratorARMVIXL::StoreToShiftedRegOffset(DataType::Type type,
Scott Wakelingc34dba72016-10-03 10:14:44 +01005940 Location loc,
5941 vixl32::Register base,
5942 vixl32::Register reg_index,
5943 vixl32::Condition cond) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005944 uint32_t shift_count = DataType::SizeShift(type);
Scott Wakelingc34dba72016-10-03 10:14:44 +01005945 MemOperand mem_address(base, reg_index, vixl32::LSL, shift_count);
5946
5947 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005948 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005949 case DataType::Type::kUint8:
5950 case DataType::Type::kInt8:
Scott Wakelingc34dba72016-10-03 10:14:44 +01005951 __ Strb(cond, RegisterFrom(loc), mem_address);
5952 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005953 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005954 case DataType::Type::kInt16:
Scott Wakelingc34dba72016-10-03 10:14:44 +01005955 __ Strh(cond, RegisterFrom(loc), mem_address);
5956 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005957 case DataType::Type::kReference:
5958 case DataType::Type::kInt32:
Scott Wakelingc34dba72016-10-03 10:14:44 +01005959 __ Str(cond, RegisterFrom(loc), mem_address);
5960 break;
5961 // T32 doesn't support StoreToShiftedRegOffset mem address mode for these types.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005962 case DataType::Type::kInt64:
5963 case DataType::Type::kFloat32:
5964 case DataType::Type::kFloat64:
Scott Wakelingc34dba72016-10-03 10:14:44 +01005965 default:
5966 LOG(FATAL) << "Unreachable type " << type;
5967 UNREACHABLE();
5968 }
5969}
5970
5971void LocationsBuilderARMVIXL::VisitArrayGet(HArrayGet* instruction) {
5972 bool object_array_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005973 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Scott Wakelingc34dba72016-10-03 10:14:44 +01005974 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005975 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
5976 object_array_get_with_read_barrier
5977 ? LocationSummary::kCallOnSlowPath
5978 : LocationSummary::kNoCall);
Scott Wakelingc34dba72016-10-03 10:14:44 +01005979 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005980 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Scott Wakelingc34dba72016-10-03 10:14:44 +01005981 }
5982 locations->SetInAt(0, Location::RequiresRegister());
5983 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005984 if (DataType::IsFloatingPointType(instruction->GetType())) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01005985 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5986 } else {
5987 // The output overlaps in the case of an object array get with
5988 // read barriers enabled: we do not want the move to overwrite the
5989 // array's location, as we need it to emit the read barrier.
5990 locations->SetOut(
5991 Location::RequiresRegister(),
5992 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
5993 }
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01005994 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
5995 // We need a temporary register for the read barrier marking slow
5996 // path in CodeGeneratorARMVIXL::GenerateArrayLoadWithBakerReadBarrier.
5997 if (kBakerReadBarrierLinkTimeThunksEnableForFields &&
5998 !Runtime::Current()->UseJitCompilation() &&
5999 instruction->GetIndex()->IsConstant()) {
6000 // Array loads with constant index are treated as field loads.
6001 // If link-time thunks for the Baker read barrier are enabled, for AOT
6002 // constant index loads we need a temporary only if the offset is too big.
6003 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
6004 uint32_t index = instruction->GetIndex()->AsIntConstant()->GetValue();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006005 offset += index << DataType::SizeShift(DataType::Type::kReference);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01006006 if (offset >= kReferenceLoadMinFarOffset) {
6007 locations->AddTemp(Location::RequiresRegister());
6008 }
6009 // And we always need the reserved entrypoint register.
6010 locations->AddTemp(Location::RegisterLocation(kBakerCcEntrypointRegister.GetCode()));
6011 } else if (kBakerReadBarrierLinkTimeThunksEnableForArrays &&
6012 !Runtime::Current()->UseJitCompilation() &&
6013 !instruction->GetIndex()->IsConstant()) {
6014 // We need a non-scratch temporary for the array data pointer.
6015 locations->AddTemp(Location::RequiresRegister());
6016 // And we always need the reserved entrypoint register.
6017 locations->AddTemp(Location::RegisterLocation(kBakerCcEntrypointRegister.GetCode()));
6018 } else {
6019 locations->AddTemp(Location::RequiresRegister());
6020 }
6021 } else if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
6022 // Also need a temporary for String compression feature.
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006023 locations->AddTemp(Location::RequiresRegister());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006024 }
6025}
6026
6027void InstructionCodeGeneratorARMVIXL::VisitArrayGet(HArrayGet* instruction) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006028 LocationSummary* locations = instruction->GetLocations();
6029 Location obj_loc = locations->InAt(0);
6030 vixl32::Register obj = InputRegisterAt(instruction, 0);
6031 Location index = locations->InAt(1);
6032 Location out_loc = locations->Out();
6033 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006034 DataType::Type type = instruction->GetType();
Scott Wakelingc34dba72016-10-03 10:14:44 +01006035 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
6036 instruction->IsStringCharAt();
6037 HInstruction* array_instr = instruction->GetArray();
6038 bool has_intermediate_address = array_instr->IsIntermediateAddress();
Scott Wakelingc34dba72016-10-03 10:14:44 +01006039
6040 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006041 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006042 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006043 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006044 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006045 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006046 case DataType::Type::kInt32: {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006047 vixl32::Register length;
6048 if (maybe_compressed_char_at) {
6049 length = RegisterFrom(locations->GetTemp(0));
6050 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
6051 GetAssembler()->LoadFromOffset(kLoadWord, length, obj, count_offset);
6052 codegen_->MaybeRecordImplicitNullCheck(instruction);
6053 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006054 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006055 int32_t const_index = Int32ConstantFrom(index);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006056 if (maybe_compressed_char_at) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006057 vixl32::Label uncompressed_load, done;
Anton Kirilov6f644202017-02-27 18:29:45 +00006058 vixl32::Label* final_label = codegen_->GetFinalLabel(instruction, &done);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006059 __ Lsrs(length, length, 1u); // LSRS has a 16-bit encoding, TST (immediate) does not.
6060 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
6061 "Expecting 0=compressed, 1=uncompressed");
Artem Serov517d9f62016-12-12 15:51:15 +00006062 __ B(cs, &uncompressed_load, /* far_target */ false);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006063 GetAssembler()->LoadFromOffset(kLoadUnsignedByte,
6064 RegisterFrom(out_loc),
6065 obj,
6066 data_offset + const_index);
Anton Kirilov6f644202017-02-27 18:29:45 +00006067 __ B(final_label);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006068 __ Bind(&uncompressed_load);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006069 GetAssembler()->LoadFromOffset(GetLoadOperandType(DataType::Type::kUint16),
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006070 RegisterFrom(out_loc),
6071 obj,
6072 data_offset + (const_index << 1));
Anton Kirilov6f644202017-02-27 18:29:45 +00006073 if (done.IsReferenced()) {
6074 __ Bind(&done);
6075 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006076 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006077 uint32_t full_offset = data_offset + (const_index << DataType::SizeShift(type));
Scott Wakelingc34dba72016-10-03 10:14:44 +01006078
6079 LoadOperandType load_type = GetLoadOperandType(type);
6080 GetAssembler()->LoadFromOffset(load_type, RegisterFrom(out_loc), obj, full_offset);
6081 }
6082 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006083 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006084 vixl32::Register temp = temps.Acquire();
6085
6086 if (has_intermediate_address) {
Artem Serov2bbc9532016-10-21 11:51:50 +01006087 // We do not need to compute the intermediate address from the array: the
6088 // input instruction has done it already. See the comment in
6089 // `TryExtractArrayAccessAddress()`.
6090 if (kIsDebugBuild) {
6091 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
Anton Kirilov644032c2016-12-06 17:51:43 +00006092 DCHECK_EQ(Uint64ConstantFrom(tmp->GetOffset()), data_offset);
Artem Serov2bbc9532016-10-21 11:51:50 +01006093 }
6094 temp = obj;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006095 } else {
6096 __ Add(temp, obj, data_offset);
6097 }
6098 if (maybe_compressed_char_at) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006099 vixl32::Label uncompressed_load, done;
Anton Kirilov6f644202017-02-27 18:29:45 +00006100 vixl32::Label* final_label = codegen_->GetFinalLabel(instruction, &done);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006101 __ Lsrs(length, length, 1u); // LSRS has a 16-bit encoding, TST (immediate) does not.
6102 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
6103 "Expecting 0=compressed, 1=uncompressed");
Artem Serov517d9f62016-12-12 15:51:15 +00006104 __ B(cs, &uncompressed_load, /* far_target */ false);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006105 __ Ldrb(RegisterFrom(out_loc), MemOperand(temp, RegisterFrom(index), vixl32::LSL, 0));
Anton Kirilov6f644202017-02-27 18:29:45 +00006106 __ B(final_label);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006107 __ Bind(&uncompressed_load);
6108 __ Ldrh(RegisterFrom(out_loc), MemOperand(temp, RegisterFrom(index), vixl32::LSL, 1));
Anton Kirilov6f644202017-02-27 18:29:45 +00006109 if (done.IsReferenced()) {
6110 __ Bind(&done);
6111 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006112 } else {
6113 codegen_->LoadFromShiftedRegOffset(type, out_loc, temp, RegisterFrom(index));
6114 }
6115 }
6116 break;
6117 }
6118
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006119 case DataType::Type::kReference: {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006120 // The read barrier instrumentation of object ArrayGet
6121 // instructions does not support the HIntermediateAddress
6122 // instruction.
6123 DCHECK(!(has_intermediate_address && kEmitCompilerReadBarrier));
6124
Scott Wakelingc34dba72016-10-03 10:14:44 +01006125 static_assert(
6126 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6127 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
6128 // /* HeapReference<Object> */ out =
6129 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
6130 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006131 Location temp = locations->GetTemp(0);
6132 // Note that a potential implicit null check is handled in this
6133 // CodeGeneratorARMVIXL::GenerateArrayLoadWithBakerReadBarrier call.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01006134 DCHECK(!instruction->CanDoImplicitNullCheckOn(instruction->InputAt(0)));
6135 if (index.IsConstant()) {
6136 // Array load with a constant index can be treated as a field load.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006137 data_offset += Int32ConstantFrom(index) << DataType::SizeShift(type);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01006138 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6139 out_loc,
6140 obj,
6141 data_offset,
6142 locations->GetTemp(0),
6143 /* needs_null_check */ false);
6144 } else {
6145 codegen_->GenerateArrayLoadWithBakerReadBarrier(
6146 instruction, out_loc, obj, data_offset, index, temp, /* needs_null_check */ false);
6147 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006148 } else {
6149 vixl32::Register out = OutputRegister(instruction);
6150 if (index.IsConstant()) {
6151 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00006152 (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006153 GetAssembler()->LoadFromOffset(kLoadWord, out, obj, offset);
Alexandre Rames374ddf32016-11-04 10:40:49 +00006154 // TODO(VIXL): Here and for other calls to `MaybeRecordImplicitNullCheck` in this method,
6155 // we should use a scope and the assembler to emit the load instruction to guarantee that
6156 // we record the pc at the correct position. But the `Assembler` does not automatically
6157 // handle unencodable offsets. Practically, everything is fine because the helper and
6158 // VIXL, at the time of writing, do generate the store instruction last.
Scott Wakelingc34dba72016-10-03 10:14:44 +01006159 codegen_->MaybeRecordImplicitNullCheck(instruction);
6160 // If read barriers are enabled, emit read barriers other than
6161 // Baker's using a slow path (and also unpoison the loaded
6162 // reference, if heap poisoning is enabled).
6163 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
6164 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006165 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006166 vixl32::Register temp = temps.Acquire();
6167
6168 if (has_intermediate_address) {
Artem Serov2bbc9532016-10-21 11:51:50 +01006169 // We do not need to compute the intermediate address from the array: the
6170 // input instruction has done it already. See the comment in
6171 // `TryExtractArrayAccessAddress()`.
6172 if (kIsDebugBuild) {
6173 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
Anton Kirilov644032c2016-12-06 17:51:43 +00006174 DCHECK_EQ(Uint64ConstantFrom(tmp->GetOffset()), data_offset);
Artem Serov2bbc9532016-10-21 11:51:50 +01006175 }
6176 temp = obj;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006177 } else {
6178 __ Add(temp, obj, data_offset);
6179 }
6180 codegen_->LoadFromShiftedRegOffset(type, out_loc, temp, RegisterFrom(index));
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006181 temps.Close();
Alexandre Rames374ddf32016-11-04 10:40:49 +00006182 // TODO(VIXL): Use a scope to ensure that we record the pc position immediately after the
6183 // load instruction. Practically, everything is fine because the helper and VIXL, at the
6184 // time of writing, do generate the store instruction last.
Scott Wakelingc34dba72016-10-03 10:14:44 +01006185 codegen_->MaybeRecordImplicitNullCheck(instruction);
6186 // If read barriers are enabled, emit read barriers other than
6187 // Baker's using a slow path (and also unpoison the loaded
6188 // reference, if heap poisoning is enabled).
6189 codegen_->MaybeGenerateReadBarrierSlow(
6190 instruction, out_loc, out_loc, obj_loc, data_offset, index);
6191 }
6192 }
6193 break;
6194 }
6195
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006196 case DataType::Type::kInt64: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006197 if (index.IsConstant()) {
6198 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00006199 (Int32ConstantFrom(index) << TIMES_8) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006200 GetAssembler()->LoadFromOffset(kLoadWordPair, LowRegisterFrom(out_loc), obj, offset);
6201 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006202 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006203 vixl32::Register temp = temps.Acquire();
6204 __ Add(temp, obj, Operand(RegisterFrom(index), vixl32::LSL, TIMES_8));
6205 GetAssembler()->LoadFromOffset(kLoadWordPair, LowRegisterFrom(out_loc), temp, data_offset);
6206 }
6207 break;
6208 }
6209
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006210 case DataType::Type::kFloat32: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006211 vixl32::SRegister out = SRegisterFrom(out_loc);
6212 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006213 size_t offset = (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006214 GetAssembler()->LoadSFromOffset(out, obj, offset);
6215 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006216 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006217 vixl32::Register temp = temps.Acquire();
6218 __ Add(temp, obj, Operand(RegisterFrom(index), vixl32::LSL, TIMES_4));
6219 GetAssembler()->LoadSFromOffset(out, temp, data_offset);
6220 }
6221 break;
6222 }
6223
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006224 case DataType::Type::kFloat64: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006225 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006226 size_t offset = (Int32ConstantFrom(index) << TIMES_8) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006227 GetAssembler()->LoadDFromOffset(DRegisterFrom(out_loc), obj, offset);
6228 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006229 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006230 vixl32::Register temp = temps.Acquire();
6231 __ Add(temp, obj, Operand(RegisterFrom(index), vixl32::LSL, TIMES_8));
6232 GetAssembler()->LoadDFromOffset(DRegisterFrom(out_loc), temp, data_offset);
6233 }
6234 break;
6235 }
6236
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006237 case DataType::Type::kVoid:
Scott Wakelingc34dba72016-10-03 10:14:44 +01006238 LOG(FATAL) << "Unreachable type " << type;
6239 UNREACHABLE();
6240 }
6241
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006242 if (type == DataType::Type::kReference) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006243 // Potential implicit null checks, in the case of reference
6244 // arrays, are handled in the previous switch statement.
6245 } else if (!maybe_compressed_char_at) {
Alexandre Rames374ddf32016-11-04 10:40:49 +00006246 // TODO(VIXL): Use a scope to ensure we record the pc info immediately after
6247 // the preceding load instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01006248 codegen_->MaybeRecordImplicitNullCheck(instruction);
6249 }
6250}
6251
6252void LocationsBuilderARMVIXL::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006253 DataType::Type value_type = instruction->GetComponentType();
Scott Wakelingc34dba72016-10-03 10:14:44 +01006254
6255 bool needs_write_barrier =
6256 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
6257 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
6258
Vladimir Markoca6fff82017-10-03 14:49:14 +01006259 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Scott Wakelingc34dba72016-10-03 10:14:44 +01006260 instruction,
6261 may_need_runtime_call_for_type_check ?
6262 LocationSummary::kCallOnSlowPath :
6263 LocationSummary::kNoCall);
6264
6265 locations->SetInAt(0, Location::RequiresRegister());
6266 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006267 if (DataType::IsFloatingPointType(value_type)) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006268 locations->SetInAt(2, Location::RequiresFpuRegister());
6269 } else {
6270 locations->SetInAt(2, Location::RequiresRegister());
6271 }
6272 if (needs_write_barrier) {
6273 // Temporary registers for the write barrier.
6274 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
6275 locations->AddTemp(Location::RequiresRegister());
6276 }
6277}
6278
6279void InstructionCodeGeneratorARMVIXL::VisitArraySet(HArraySet* instruction) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006280 LocationSummary* locations = instruction->GetLocations();
6281 vixl32::Register array = InputRegisterAt(instruction, 0);
6282 Location index = locations->InAt(1);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006283 DataType::Type value_type = instruction->GetComponentType();
Scott Wakelingc34dba72016-10-03 10:14:44 +01006284 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
6285 bool needs_write_barrier =
6286 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
6287 uint32_t data_offset =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006288 mirror::Array::DataOffset(DataType::Size(value_type)).Uint32Value();
Scott Wakelingc34dba72016-10-03 10:14:44 +01006289 Location value_loc = locations->InAt(2);
6290 HInstruction* array_instr = instruction->GetArray();
6291 bool has_intermediate_address = array_instr->IsIntermediateAddress();
Scott Wakelingc34dba72016-10-03 10:14:44 +01006292
6293 switch (value_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006294 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006295 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006296 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006297 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006298 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006299 case DataType::Type::kInt32: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006300 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006301 int32_t const_index = Int32ConstantFrom(index);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006302 uint32_t full_offset =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006303 data_offset + (const_index << DataType::SizeShift(value_type));
Scott Wakelingc34dba72016-10-03 10:14:44 +01006304 StoreOperandType store_type = GetStoreOperandType(value_type);
6305 GetAssembler()->StoreToOffset(store_type, RegisterFrom(value_loc), array, full_offset);
6306 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006307 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006308 vixl32::Register temp = temps.Acquire();
6309
6310 if (has_intermediate_address) {
Artem Serov2bbc9532016-10-21 11:51:50 +01006311 // We do not need to compute the intermediate address from the array: the
6312 // input instruction has done it already. See the comment in
6313 // `TryExtractArrayAccessAddress()`.
6314 if (kIsDebugBuild) {
6315 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
Anton Kirilov644032c2016-12-06 17:51:43 +00006316 DCHECK_EQ(Uint64ConstantFrom(tmp->GetOffset()), data_offset);
Artem Serov2bbc9532016-10-21 11:51:50 +01006317 }
6318 temp = array;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006319 } else {
6320 __ Add(temp, array, data_offset);
6321 }
6322 codegen_->StoreToShiftedRegOffset(value_type, value_loc, temp, RegisterFrom(index));
6323 }
6324 break;
6325 }
6326
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006327 case DataType::Type::kReference: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006328 vixl32::Register value = RegisterFrom(value_loc);
6329 // TryExtractArrayAccessAddress optimization is never applied for non-primitive ArraySet.
6330 // See the comment in instruction_simplifier_shared.cc.
6331 DCHECK(!has_intermediate_address);
6332
6333 if (instruction->InputAt(2)->IsNullConstant()) {
6334 // Just setting null.
6335 if (index.IsConstant()) {
6336 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00006337 (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006338 GetAssembler()->StoreToOffset(kStoreWord, value, array, offset);
6339 } else {
6340 DCHECK(index.IsRegister()) << index;
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006341 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006342 vixl32::Register temp = temps.Acquire();
6343 __ Add(temp, array, data_offset);
6344 codegen_->StoreToShiftedRegOffset(value_type, value_loc, temp, RegisterFrom(index));
6345 }
Alexandre Rames374ddf32016-11-04 10:40:49 +00006346 // TODO(VIXL): Use a scope to ensure we record the pc info immediately after the preceding
6347 // store instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01006348 codegen_->MaybeRecordImplicitNullCheck(instruction);
6349 DCHECK(!needs_write_barrier);
6350 DCHECK(!may_need_runtime_call_for_type_check);
6351 break;
6352 }
6353
6354 DCHECK(needs_write_barrier);
6355 Location temp1_loc = locations->GetTemp(0);
6356 vixl32::Register temp1 = RegisterFrom(temp1_loc);
6357 Location temp2_loc = locations->GetTemp(1);
6358 vixl32::Register temp2 = RegisterFrom(temp2_loc);
6359 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6360 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6361 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6362 vixl32::Label done;
Anton Kirilov6f644202017-02-27 18:29:45 +00006363 vixl32::Label* final_label = codegen_->GetFinalLabel(instruction, &done);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006364 SlowPathCodeARMVIXL* slow_path = nullptr;
6365
6366 if (may_need_runtime_call_for_type_check) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01006367 slow_path = new (codegen_->GetScopedAllocator()) ArraySetSlowPathARMVIXL(instruction);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006368 codegen_->AddSlowPath(slow_path);
6369 if (instruction->GetValueCanBeNull()) {
6370 vixl32::Label non_zero;
xueliang.zhongf51bc622016-11-04 09:23:32 +00006371 __ CompareAndBranchIfNonZero(value, &non_zero);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006372 if (index.IsConstant()) {
6373 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00006374 (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006375 GetAssembler()->StoreToOffset(kStoreWord, value, array, offset);
6376 } else {
6377 DCHECK(index.IsRegister()) << index;
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006378 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006379 vixl32::Register temp = temps.Acquire();
6380 __ Add(temp, array, data_offset);
6381 codegen_->StoreToShiftedRegOffset(value_type, value_loc, temp, RegisterFrom(index));
6382 }
Alexandre Rames374ddf32016-11-04 10:40:49 +00006383 // TODO(VIXL): Use a scope to ensure we record the pc info immediately after the preceding
6384 // store instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01006385 codegen_->MaybeRecordImplicitNullCheck(instruction);
Anton Kirilov6f644202017-02-27 18:29:45 +00006386 __ B(final_label);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006387 __ Bind(&non_zero);
6388 }
6389
6390 // Note that when read barriers are enabled, the type checks
6391 // are performed without read barriers. This is fine, even in
6392 // the case where a class object is in the from-space after
6393 // the flip, as a comparison involving such a type would not
6394 // produce a false positive; it may of course produce a false
6395 // negative, in which case we would take the ArraySet slow
6396 // path.
6397
Alexandre Rames374ddf32016-11-04 10:40:49 +00006398 {
6399 // Ensure we record the pc position immediately after the `ldr` instruction.
Artem Serov0fb37192016-12-06 18:13:40 +00006400 ExactAssemblyScope aas(GetVIXLAssembler(),
6401 vixl32::kMaxInstructionSizeInBytes,
6402 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00006403 // /* HeapReference<Class> */ temp1 = array->klass_
6404 __ ldr(temp1, MemOperand(array, class_offset));
6405 codegen_->MaybeRecordImplicitNullCheck(instruction);
6406 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006407 GetAssembler()->MaybeUnpoisonHeapReference(temp1);
6408
6409 // /* HeapReference<Class> */ temp1 = temp1->component_type_
6410 GetAssembler()->LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
6411 // /* HeapReference<Class> */ temp2 = value->klass_
6412 GetAssembler()->LoadFromOffset(kLoadWord, temp2, value, class_offset);
6413 // If heap poisoning is enabled, no need to unpoison `temp1`
6414 // nor `temp2`, as we are comparing two poisoned references.
6415 __ Cmp(temp1, temp2);
6416
6417 if (instruction->StaticTypeOfArrayIsObjectArray()) {
6418 vixl32::Label do_put;
Artem Serov517d9f62016-12-12 15:51:15 +00006419 __ B(eq, &do_put, /* far_target */ false);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006420 // If heap poisoning is enabled, the `temp1` reference has
6421 // not been unpoisoned yet; unpoison it now.
6422 GetAssembler()->MaybeUnpoisonHeapReference(temp1);
6423
6424 // /* HeapReference<Class> */ temp1 = temp1->super_class_
6425 GetAssembler()->LoadFromOffset(kLoadWord, temp1, temp1, super_offset);
6426 // If heap poisoning is enabled, no need to unpoison
6427 // `temp1`, as we are comparing against null below.
xueliang.zhongf51bc622016-11-04 09:23:32 +00006428 __ CompareAndBranchIfNonZero(temp1, slow_path->GetEntryLabel());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006429 __ Bind(&do_put);
6430 } else {
6431 __ B(ne, slow_path->GetEntryLabel());
6432 }
6433 }
6434
6435 vixl32::Register source = value;
6436 if (kPoisonHeapReferences) {
6437 // Note that in the case where `value` is a null reference,
6438 // we do not enter this block, as a null reference does not
6439 // need poisoning.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006440 DCHECK_EQ(value_type, DataType::Type::kReference);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006441 __ Mov(temp1, value);
6442 GetAssembler()->PoisonHeapReference(temp1);
6443 source = temp1;
6444 }
6445
6446 if (index.IsConstant()) {
6447 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00006448 (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006449 GetAssembler()->StoreToOffset(kStoreWord, source, array, offset);
6450 } else {
6451 DCHECK(index.IsRegister()) << index;
6452
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006453 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006454 vixl32::Register temp = temps.Acquire();
6455 __ Add(temp, array, data_offset);
6456 codegen_->StoreToShiftedRegOffset(value_type,
6457 LocationFrom(source),
6458 temp,
6459 RegisterFrom(index));
6460 }
6461
6462 if (!may_need_runtime_call_for_type_check) {
Alexandre Rames374ddf32016-11-04 10:40:49 +00006463 // TODO(VIXL): Ensure we record the pc position immediately after the preceding store
6464 // instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01006465 codegen_->MaybeRecordImplicitNullCheck(instruction);
6466 }
6467
6468 codegen_->MarkGCCard(temp1, temp2, array, value, instruction->GetValueCanBeNull());
6469
6470 if (done.IsReferenced()) {
6471 __ Bind(&done);
6472 }
6473
6474 if (slow_path != nullptr) {
6475 __ Bind(slow_path->GetExitLabel());
6476 }
6477
6478 break;
6479 }
6480
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006481 case DataType::Type::kInt64: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006482 Location value = locations->InAt(2);
6483 if (index.IsConstant()) {
6484 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00006485 (Int32ConstantFrom(index) << TIMES_8) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006486 GetAssembler()->StoreToOffset(kStoreWordPair, LowRegisterFrom(value), array, offset);
6487 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006488 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006489 vixl32::Register temp = temps.Acquire();
6490 __ Add(temp, array, Operand(RegisterFrom(index), vixl32::LSL, TIMES_8));
6491 GetAssembler()->StoreToOffset(kStoreWordPair, LowRegisterFrom(value), temp, data_offset);
6492 }
6493 break;
6494 }
6495
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006496 case DataType::Type::kFloat32: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006497 Location value = locations->InAt(2);
6498 DCHECK(value.IsFpuRegister());
6499 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006500 size_t offset = (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006501 GetAssembler()->StoreSToOffset(SRegisterFrom(value), array, offset);
6502 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006503 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006504 vixl32::Register temp = temps.Acquire();
6505 __ Add(temp, array, Operand(RegisterFrom(index), vixl32::LSL, TIMES_4));
6506 GetAssembler()->StoreSToOffset(SRegisterFrom(value), temp, data_offset);
6507 }
6508 break;
6509 }
6510
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006511 case DataType::Type::kFloat64: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006512 Location value = locations->InAt(2);
6513 DCHECK(value.IsFpuRegisterPair());
6514 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006515 size_t offset = (Int32ConstantFrom(index) << TIMES_8) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006516 GetAssembler()->StoreDToOffset(DRegisterFrom(value), array, offset);
6517 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006518 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006519 vixl32::Register temp = temps.Acquire();
6520 __ Add(temp, array, Operand(RegisterFrom(index), vixl32::LSL, TIMES_8));
6521 GetAssembler()->StoreDToOffset(DRegisterFrom(value), temp, data_offset);
6522 }
6523 break;
6524 }
6525
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006526 case DataType::Type::kVoid:
Scott Wakelingc34dba72016-10-03 10:14:44 +01006527 LOG(FATAL) << "Unreachable type " << value_type;
6528 UNREACHABLE();
6529 }
6530
6531 // Objects are handled in the switch.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006532 if (value_type != DataType::Type::kReference) {
Alexandre Rames374ddf32016-11-04 10:40:49 +00006533 // TODO(VIXL): Ensure we record the pc position immediately after the preceding store
6534 // instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01006535 codegen_->MaybeRecordImplicitNullCheck(instruction);
6536 }
6537}
6538
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006539void LocationsBuilderARMVIXL::VisitArrayLength(HArrayLength* instruction) {
6540 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006541 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006542 locations->SetInAt(0, Location::RequiresRegister());
6543 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6544}
6545
6546void InstructionCodeGeneratorARMVIXL::VisitArrayLength(HArrayLength* instruction) {
6547 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
6548 vixl32::Register obj = InputRegisterAt(instruction, 0);
6549 vixl32::Register out = OutputRegister(instruction);
Alexandre Rames374ddf32016-11-04 10:40:49 +00006550 {
Artem Serov0fb37192016-12-06 18:13:40 +00006551 ExactAssemblyScope aas(GetVIXLAssembler(),
6552 vixl32::kMaxInstructionSizeInBytes,
6553 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00006554 __ ldr(out, MemOperand(obj, offset));
6555 codegen_->MaybeRecordImplicitNullCheck(instruction);
6556 }
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006557 // Mask out compression flag from String's array length.
6558 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006559 __ Lsr(out, out, 1u);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006560 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006561}
6562
Artem Serov2bbc9532016-10-21 11:51:50 +01006563void LocationsBuilderARMVIXL::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Artem Serov2bbc9532016-10-21 11:51:50 +01006564 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006565 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Artem Serov2bbc9532016-10-21 11:51:50 +01006566
6567 locations->SetInAt(0, Location::RequiresRegister());
6568 locations->SetInAt(1, Location::RegisterOrConstant(instruction->GetOffset()));
6569 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6570}
6571
6572void InstructionCodeGeneratorARMVIXL::VisitIntermediateAddress(HIntermediateAddress* instruction) {
6573 vixl32::Register out = OutputRegister(instruction);
6574 vixl32::Register first = InputRegisterAt(instruction, 0);
6575 Location second = instruction->GetLocations()->InAt(1);
6576
Artem Serov2bbc9532016-10-21 11:51:50 +01006577 if (second.IsRegister()) {
6578 __ Add(out, first, RegisterFrom(second));
6579 } else {
Anton Kirilov644032c2016-12-06 17:51:43 +00006580 __ Add(out, first, Int32ConstantFrom(second));
Artem Serov2bbc9532016-10-21 11:51:50 +01006581 }
6582}
6583
Artem Serove1811ed2017-04-27 16:50:47 +01006584void LocationsBuilderARMVIXL::VisitIntermediateAddressIndex(
6585 HIntermediateAddressIndex* instruction) {
6586 LOG(FATAL) << "Unreachable " << instruction->GetId();
6587}
6588
6589void InstructionCodeGeneratorARMVIXL::VisitIntermediateAddressIndex(
6590 HIntermediateAddressIndex* instruction) {
6591 LOG(FATAL) << "Unreachable " << instruction->GetId();
6592}
6593
Scott Wakelingc34dba72016-10-03 10:14:44 +01006594void LocationsBuilderARMVIXL::VisitBoundsCheck(HBoundsCheck* instruction) {
6595 RegisterSet caller_saves = RegisterSet::Empty();
6596 InvokeRuntimeCallingConventionARMVIXL calling_convention;
6597 caller_saves.Add(LocationFrom(calling_convention.GetRegisterAt(0)));
6598 caller_saves.Add(LocationFrom(calling_convention.GetRegisterAt(1)));
6599 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Artem Serov2dd053d2017-03-08 14:54:06 +00006600
6601 HInstruction* index = instruction->InputAt(0);
6602 HInstruction* length = instruction->InputAt(1);
6603 // If both index and length are constants we can statically check the bounds. But if at least one
6604 // of them is not encodable ArmEncodableConstantOrRegister will create
6605 // Location::RequiresRegister() which is not desired to happen. Instead we create constant
6606 // locations.
6607 bool both_const = index->IsConstant() && length->IsConstant();
6608 locations->SetInAt(0, both_const
6609 ? Location::ConstantLocation(index->AsConstant())
6610 : ArmEncodableConstantOrRegister(index, CMP));
6611 locations->SetInAt(1, both_const
6612 ? Location::ConstantLocation(length->AsConstant())
6613 : ArmEncodableConstantOrRegister(length, CMP));
Scott Wakelingc34dba72016-10-03 10:14:44 +01006614}
6615
6616void InstructionCodeGeneratorARMVIXL::VisitBoundsCheck(HBoundsCheck* instruction) {
Artem Serov2dd053d2017-03-08 14:54:06 +00006617 LocationSummary* locations = instruction->GetLocations();
6618 Location index_loc = locations->InAt(0);
6619 Location length_loc = locations->InAt(1);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006620
Artem Serov2dd053d2017-03-08 14:54:06 +00006621 if (length_loc.IsConstant()) {
6622 int32_t length = Int32ConstantFrom(length_loc);
6623 if (index_loc.IsConstant()) {
6624 // BCE will remove the bounds check if we are guaranteed to pass.
6625 int32_t index = Int32ConstantFrom(index_loc);
6626 if (index < 0 || index >= length) {
6627 SlowPathCodeARMVIXL* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006628 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathARMVIXL(instruction);
Artem Serov2dd053d2017-03-08 14:54:06 +00006629 codegen_->AddSlowPath(slow_path);
6630 __ B(slow_path->GetEntryLabel());
6631 } else {
6632 // Some optimization after BCE may have generated this, and we should not
6633 // generate a bounds check if it is a valid range.
6634 }
6635 return;
6636 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006637
Artem Serov2dd053d2017-03-08 14:54:06 +00006638 SlowPathCodeARMVIXL* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006639 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathARMVIXL(instruction);
Artem Serov2dd053d2017-03-08 14:54:06 +00006640 __ Cmp(RegisterFrom(index_loc), length);
6641 codegen_->AddSlowPath(slow_path);
6642 __ B(hs, slow_path->GetEntryLabel());
6643 } else {
6644 SlowPathCodeARMVIXL* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006645 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathARMVIXL(instruction);
Artem Serov2dd053d2017-03-08 14:54:06 +00006646 __ Cmp(RegisterFrom(length_loc), InputOperandAt(instruction, 0));
6647 codegen_->AddSlowPath(slow_path);
6648 __ B(ls, slow_path->GetEntryLabel());
6649 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006650}
6651
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006652void CodeGeneratorARMVIXL::MarkGCCard(vixl32::Register temp,
6653 vixl32::Register card,
6654 vixl32::Register object,
6655 vixl32::Register value,
6656 bool can_be_null) {
6657 vixl32::Label is_null;
6658 if (can_be_null) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00006659 __ CompareAndBranchIfZero(value, &is_null);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006660 }
6661 GetAssembler()->LoadFromOffset(
6662 kLoadWord, card, tr, Thread::CardTableOffset<kArmPointerSize>().Int32Value());
Scott Wakelingb77051e2016-11-21 19:46:00 +00006663 __ Lsr(temp, object, Operand::From(gc::accounting::CardTable::kCardShift));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006664 __ Strb(card, MemOperand(card, temp));
6665 if (can_be_null) {
6666 __ Bind(&is_null);
6667 }
6668}
6669
Scott Wakelingfe885462016-09-22 10:24:38 +01006670void LocationsBuilderARMVIXL::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
6671 LOG(FATAL) << "Unreachable";
6672}
6673
6674void InstructionCodeGeneratorARMVIXL::VisitParallelMove(HParallelMove* instruction) {
Vladimir Markobea75ff2017-10-11 20:39:54 +01006675 if (instruction->GetNext()->IsSuspendCheck() &&
6676 instruction->GetBlock()->GetLoopInformation() != nullptr) {
6677 HSuspendCheck* suspend_check = instruction->GetNext()->AsSuspendCheck();
6678 // The back edge will generate the suspend check.
6679 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(suspend_check, instruction);
6680 }
6681
Scott Wakelingfe885462016-09-22 10:24:38 +01006682 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
6683}
6684
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006685void LocationsBuilderARMVIXL::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006686 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6687 instruction, LocationSummary::kCallOnSlowPath);
Artem Serov657022c2016-11-23 14:19:38 +00006688 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006689}
6690
6691void InstructionCodeGeneratorARMVIXL::VisitSuspendCheck(HSuspendCheck* instruction) {
6692 HBasicBlock* block = instruction->GetBlock();
6693 if (block->GetLoopInformation() != nullptr) {
6694 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
6695 // The back edge will generate the suspend check.
6696 return;
6697 }
6698 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
6699 // The goto will generate the suspend check.
6700 return;
6701 }
6702 GenerateSuspendCheck(instruction, nullptr);
Roland Levillain5daa4952017-07-03 17:23:56 +01006703 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 12);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006704}
6705
6706void InstructionCodeGeneratorARMVIXL::GenerateSuspendCheck(HSuspendCheck* instruction,
6707 HBasicBlock* successor) {
6708 SuspendCheckSlowPathARMVIXL* slow_path =
6709 down_cast<SuspendCheckSlowPathARMVIXL*>(instruction->GetSlowPath());
6710 if (slow_path == nullptr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006711 slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006712 new (codegen_->GetScopedAllocator()) SuspendCheckSlowPathARMVIXL(instruction, successor);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006713 instruction->SetSlowPath(slow_path);
6714 codegen_->AddSlowPath(slow_path);
6715 if (successor != nullptr) {
6716 DCHECK(successor->IsLoopHeader());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006717 }
6718 } else {
6719 DCHECK_EQ(slow_path->GetSuccessor(), successor);
6720 }
6721
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006722 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006723 vixl32::Register temp = temps.Acquire();
6724 GetAssembler()->LoadFromOffset(
6725 kLoadUnsignedHalfword, temp, tr, Thread::ThreadFlagsOffset<kArmPointerSize>().Int32Value());
6726 if (successor == nullptr) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00006727 __ CompareAndBranchIfNonZero(temp, slow_path->GetEntryLabel());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006728 __ Bind(slow_path->GetReturnLabel());
6729 } else {
xueliang.zhongf51bc622016-11-04 09:23:32 +00006730 __ CompareAndBranchIfZero(temp, codegen_->GetLabelOf(successor));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006731 __ B(slow_path->GetEntryLabel());
6732 }
6733}
6734
Scott Wakelingfe885462016-09-22 10:24:38 +01006735ArmVIXLAssembler* ParallelMoveResolverARMVIXL::GetAssembler() const {
6736 return codegen_->GetAssembler();
6737}
6738
6739void ParallelMoveResolverARMVIXL::EmitMove(size_t index) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006740 UseScratchRegisterScope temps(GetAssembler()->GetVIXLAssembler());
Scott Wakelingfe885462016-09-22 10:24:38 +01006741 MoveOperands* move = moves_[index];
6742 Location source = move->GetSource();
6743 Location destination = move->GetDestination();
6744
6745 if (source.IsRegister()) {
6746 if (destination.IsRegister()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006747 __ Mov(RegisterFrom(destination), RegisterFrom(source));
Scott Wakelingfe885462016-09-22 10:24:38 +01006748 } else if (destination.IsFpuRegister()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006749 __ Vmov(SRegisterFrom(destination), RegisterFrom(source));
Scott Wakelingfe885462016-09-22 10:24:38 +01006750 } else {
6751 DCHECK(destination.IsStackSlot());
6752 GetAssembler()->StoreToOffset(kStoreWord,
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006753 RegisterFrom(source),
Scott Wakelingfe885462016-09-22 10:24:38 +01006754 sp,
6755 destination.GetStackIndex());
6756 }
6757 } else if (source.IsStackSlot()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006758 if (destination.IsRegister()) {
6759 GetAssembler()->LoadFromOffset(kLoadWord,
6760 RegisterFrom(destination),
6761 sp,
6762 source.GetStackIndex());
6763 } else if (destination.IsFpuRegister()) {
6764 GetAssembler()->LoadSFromOffset(SRegisterFrom(destination), sp, source.GetStackIndex());
6765 } else {
6766 DCHECK(destination.IsStackSlot());
6767 vixl32::Register temp = temps.Acquire();
6768 GetAssembler()->LoadFromOffset(kLoadWord, temp, sp, source.GetStackIndex());
6769 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
6770 }
Scott Wakelingfe885462016-09-22 10:24:38 +01006771 } else if (source.IsFpuRegister()) {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01006772 if (destination.IsRegister()) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006773 __ Vmov(RegisterFrom(destination), SRegisterFrom(source));
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01006774 } else if (destination.IsFpuRegister()) {
6775 __ Vmov(SRegisterFrom(destination), SRegisterFrom(source));
6776 } else {
6777 DCHECK(destination.IsStackSlot());
6778 GetAssembler()->StoreSToOffset(SRegisterFrom(source), sp, destination.GetStackIndex());
6779 }
Scott Wakelingfe885462016-09-22 10:24:38 +01006780 } else if (source.IsDoubleStackSlot()) {
Alexandre Rames9c19bd62016-10-24 11:50:32 +01006781 if (destination.IsDoubleStackSlot()) {
6782 vixl32::DRegister temp = temps.AcquireD();
6783 GetAssembler()->LoadDFromOffset(temp, sp, source.GetStackIndex());
6784 GetAssembler()->StoreDToOffset(temp, sp, destination.GetStackIndex());
6785 } else if (destination.IsRegisterPair()) {
6786 DCHECK(ExpectedPairLayout(destination));
6787 GetAssembler()->LoadFromOffset(
6788 kLoadWordPair, LowRegisterFrom(destination), sp, source.GetStackIndex());
6789 } else {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01006790 DCHECK(destination.IsFpuRegisterPair()) << destination;
6791 GetAssembler()->LoadDFromOffset(DRegisterFrom(destination), sp, source.GetStackIndex());
Alexandre Rames9c19bd62016-10-24 11:50:32 +01006792 }
Scott Wakelingfe885462016-09-22 10:24:38 +01006793 } else if (source.IsRegisterPair()) {
6794 if (destination.IsRegisterPair()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006795 __ Mov(LowRegisterFrom(destination), LowRegisterFrom(source));
6796 __ Mov(HighRegisterFrom(destination), HighRegisterFrom(source));
Scott Wakelingfe885462016-09-22 10:24:38 +01006797 } else if (destination.IsFpuRegisterPair()) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006798 __ Vmov(DRegisterFrom(destination), LowRegisterFrom(source), HighRegisterFrom(source));
Scott Wakelingfe885462016-09-22 10:24:38 +01006799 } else {
6800 DCHECK(destination.IsDoubleStackSlot()) << destination;
6801 DCHECK(ExpectedPairLayout(source));
6802 GetAssembler()->StoreToOffset(kStoreWordPair,
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006803 LowRegisterFrom(source),
Scott Wakelingfe885462016-09-22 10:24:38 +01006804 sp,
6805 destination.GetStackIndex());
6806 }
6807 } else if (source.IsFpuRegisterPair()) {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01006808 if (destination.IsRegisterPair()) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006809 __ Vmov(LowRegisterFrom(destination), HighRegisterFrom(destination), DRegisterFrom(source));
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01006810 } else if (destination.IsFpuRegisterPair()) {
6811 __ Vmov(DRegisterFrom(destination), DRegisterFrom(source));
6812 } else {
6813 DCHECK(destination.IsDoubleStackSlot()) << destination;
6814 GetAssembler()->StoreDToOffset(DRegisterFrom(source), sp, destination.GetStackIndex());
6815 }
Scott Wakelingfe885462016-09-22 10:24:38 +01006816 } else {
6817 DCHECK(source.IsConstant()) << source;
6818 HConstant* constant = source.GetConstant();
6819 if (constant->IsIntConstant() || constant->IsNullConstant()) {
6820 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
6821 if (destination.IsRegister()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006822 __ Mov(RegisterFrom(destination), value);
Scott Wakelingfe885462016-09-22 10:24:38 +01006823 } else {
6824 DCHECK(destination.IsStackSlot());
Scott Wakelingfe885462016-09-22 10:24:38 +01006825 vixl32::Register temp = temps.Acquire();
6826 __ Mov(temp, value);
6827 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
6828 }
6829 } else if (constant->IsLongConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006830 int64_t value = Int64ConstantFrom(source);
Scott Wakelingfe885462016-09-22 10:24:38 +01006831 if (destination.IsRegisterPair()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006832 __ Mov(LowRegisterFrom(destination), Low32Bits(value));
6833 __ Mov(HighRegisterFrom(destination), High32Bits(value));
Scott Wakelingfe885462016-09-22 10:24:38 +01006834 } else {
6835 DCHECK(destination.IsDoubleStackSlot()) << destination;
Scott Wakelingfe885462016-09-22 10:24:38 +01006836 vixl32::Register temp = temps.Acquire();
6837 __ Mov(temp, Low32Bits(value));
6838 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
6839 __ Mov(temp, High32Bits(value));
6840 GetAssembler()->StoreToOffset(kStoreWord,
6841 temp,
6842 sp,
6843 destination.GetHighStackIndex(kArmWordSize));
6844 }
6845 } else if (constant->IsDoubleConstant()) {
6846 double value = constant->AsDoubleConstant()->GetValue();
6847 if (destination.IsFpuRegisterPair()) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006848 __ Vmov(DRegisterFrom(destination), value);
Scott Wakelingfe885462016-09-22 10:24:38 +01006849 } else {
6850 DCHECK(destination.IsDoubleStackSlot()) << destination;
6851 uint64_t int_value = bit_cast<uint64_t, double>(value);
Scott Wakelingfe885462016-09-22 10:24:38 +01006852 vixl32::Register temp = temps.Acquire();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006853 __ Mov(temp, Low32Bits(int_value));
Scott Wakelingfe885462016-09-22 10:24:38 +01006854 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006855 __ Mov(temp, High32Bits(int_value));
Scott Wakelingfe885462016-09-22 10:24:38 +01006856 GetAssembler()->StoreToOffset(kStoreWord,
6857 temp,
6858 sp,
6859 destination.GetHighStackIndex(kArmWordSize));
6860 }
6861 } else {
6862 DCHECK(constant->IsFloatConstant()) << constant->DebugName();
6863 float value = constant->AsFloatConstant()->GetValue();
6864 if (destination.IsFpuRegister()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006865 __ Vmov(SRegisterFrom(destination), value);
Scott Wakelingfe885462016-09-22 10:24:38 +01006866 } else {
6867 DCHECK(destination.IsStackSlot());
Scott Wakelingfe885462016-09-22 10:24:38 +01006868 vixl32::Register temp = temps.Acquire();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006869 __ Mov(temp, bit_cast<int32_t, float>(value));
Scott Wakelingfe885462016-09-22 10:24:38 +01006870 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
6871 }
6872 }
6873 }
6874}
6875
Alexandre Rames9c19bd62016-10-24 11:50:32 +01006876void ParallelMoveResolverARMVIXL::Exchange(vixl32::Register reg, int mem) {
6877 UseScratchRegisterScope temps(GetAssembler()->GetVIXLAssembler());
6878 vixl32::Register temp = temps.Acquire();
6879 __ Mov(temp, reg);
6880 GetAssembler()->LoadFromOffset(kLoadWord, reg, sp, mem);
6881 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, mem);
Scott Wakelingfe885462016-09-22 10:24:38 +01006882}
6883
Alexandre Rames9c19bd62016-10-24 11:50:32 +01006884void ParallelMoveResolverARMVIXL::Exchange(int mem1, int mem2) {
6885 // TODO(VIXL32): Double check the performance of this implementation.
6886 UseScratchRegisterScope temps(GetAssembler()->GetVIXLAssembler());
Nicolas Geoffray13a797b2017-03-15 16:41:31 +00006887 vixl32::Register temp1 = temps.Acquire();
6888 ScratchRegisterScope ensure_scratch(
6889 this, temp1.GetCode(), r0.GetCode(), codegen_->GetNumberOfCoreRegisters());
6890 vixl32::Register temp2(ensure_scratch.GetRegister());
Alexandre Rames9c19bd62016-10-24 11:50:32 +01006891
Nicolas Geoffray13a797b2017-03-15 16:41:31 +00006892 int stack_offset = ensure_scratch.IsSpilled() ? kArmWordSize : 0;
6893 GetAssembler()->LoadFromOffset(kLoadWord, temp1, sp, mem1 + stack_offset);
6894 GetAssembler()->LoadFromOffset(kLoadWord, temp2, sp, mem2 + stack_offset);
6895 GetAssembler()->StoreToOffset(kStoreWord, temp1, sp, mem2 + stack_offset);
6896 GetAssembler()->StoreToOffset(kStoreWord, temp2, sp, mem1 + stack_offset);
Scott Wakelingfe885462016-09-22 10:24:38 +01006897}
6898
Alexandre Rames9c19bd62016-10-24 11:50:32 +01006899void ParallelMoveResolverARMVIXL::EmitSwap(size_t index) {
6900 MoveOperands* move = moves_[index];
6901 Location source = move->GetSource();
6902 Location destination = move->GetDestination();
6903 UseScratchRegisterScope temps(GetAssembler()->GetVIXLAssembler());
6904
6905 if (source.IsRegister() && destination.IsRegister()) {
6906 vixl32::Register temp = temps.Acquire();
6907 DCHECK(!RegisterFrom(source).Is(temp));
6908 DCHECK(!RegisterFrom(destination).Is(temp));
6909 __ Mov(temp, RegisterFrom(destination));
6910 __ Mov(RegisterFrom(destination), RegisterFrom(source));
6911 __ Mov(RegisterFrom(source), temp);
6912 } else if (source.IsRegister() && destination.IsStackSlot()) {
6913 Exchange(RegisterFrom(source), destination.GetStackIndex());
6914 } else if (source.IsStackSlot() && destination.IsRegister()) {
6915 Exchange(RegisterFrom(destination), source.GetStackIndex());
6916 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Anton Kirilovdda43962016-11-21 19:55:20 +00006917 Exchange(source.GetStackIndex(), destination.GetStackIndex());
Alexandre Rames9c19bd62016-10-24 11:50:32 +01006918 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Nicolas Geoffray13a797b2017-03-15 16:41:31 +00006919 vixl32::Register temp = temps.Acquire();
Anton Kirilovdda43962016-11-21 19:55:20 +00006920 __ Vmov(temp, SRegisterFrom(source));
6921 __ Vmov(SRegisterFrom(source), SRegisterFrom(destination));
6922 __ Vmov(SRegisterFrom(destination), temp);
Alexandre Rames9c19bd62016-10-24 11:50:32 +01006923 } else if (source.IsRegisterPair() && destination.IsRegisterPair()) {
6924 vixl32::DRegister temp = temps.AcquireD();
6925 __ Vmov(temp, LowRegisterFrom(source), HighRegisterFrom(source));
6926 __ Mov(LowRegisterFrom(source), LowRegisterFrom(destination));
6927 __ Mov(HighRegisterFrom(source), HighRegisterFrom(destination));
6928 __ Vmov(LowRegisterFrom(destination), HighRegisterFrom(destination), temp);
6929 } else if (source.IsRegisterPair() || destination.IsRegisterPair()) {
6930 vixl32::Register low_reg = LowRegisterFrom(source.IsRegisterPair() ? source : destination);
6931 int mem = source.IsRegisterPair() ? destination.GetStackIndex() : source.GetStackIndex();
6932 DCHECK(ExpectedPairLayout(source.IsRegisterPair() ? source : destination));
6933 vixl32::DRegister temp = temps.AcquireD();
6934 __ Vmov(temp, low_reg, vixl32::Register(low_reg.GetCode() + 1));
6935 GetAssembler()->LoadFromOffset(kLoadWordPair, low_reg, sp, mem);
6936 GetAssembler()->StoreDToOffset(temp, sp, mem);
6937 } else if (source.IsFpuRegisterPair() && destination.IsFpuRegisterPair()) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006938 vixl32::DRegister first = DRegisterFrom(source);
6939 vixl32::DRegister second = DRegisterFrom(destination);
6940 vixl32::DRegister temp = temps.AcquireD();
6941 __ Vmov(temp, first);
6942 __ Vmov(first, second);
6943 __ Vmov(second, temp);
Alexandre Rames9c19bd62016-10-24 11:50:32 +01006944 } else if (source.IsFpuRegisterPair() || destination.IsFpuRegisterPair()) {
Anton Kirilovdda43962016-11-21 19:55:20 +00006945 vixl32::DRegister reg = source.IsFpuRegisterPair()
6946 ? DRegisterFrom(source)
6947 : DRegisterFrom(destination);
6948 int mem = source.IsFpuRegisterPair()
6949 ? destination.GetStackIndex()
6950 : source.GetStackIndex();
6951 vixl32::DRegister temp = temps.AcquireD();
6952 __ Vmov(temp, reg);
6953 GetAssembler()->LoadDFromOffset(reg, sp, mem);
6954 GetAssembler()->StoreDToOffset(temp, sp, mem);
Alexandre Rames9c19bd62016-10-24 11:50:32 +01006955 } else if (source.IsFpuRegister() || destination.IsFpuRegister()) {
Anton Kirilovdda43962016-11-21 19:55:20 +00006956 vixl32::SRegister reg = source.IsFpuRegister()
6957 ? SRegisterFrom(source)
6958 : SRegisterFrom(destination);
6959 int mem = source.IsFpuRegister()
6960 ? destination.GetStackIndex()
6961 : source.GetStackIndex();
6962 vixl32::Register temp = temps.Acquire();
6963 __ Vmov(temp, reg);
6964 GetAssembler()->LoadSFromOffset(reg, sp, mem);
6965 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, mem);
Alexandre Rames9c19bd62016-10-24 11:50:32 +01006966 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
6967 vixl32::DRegister temp1 = temps.AcquireD();
6968 vixl32::DRegister temp2 = temps.AcquireD();
6969 __ Vldr(temp1, MemOperand(sp, source.GetStackIndex()));
6970 __ Vldr(temp2, MemOperand(sp, destination.GetStackIndex()));
6971 __ Vstr(temp1, MemOperand(sp, destination.GetStackIndex()));
6972 __ Vstr(temp2, MemOperand(sp, source.GetStackIndex()));
6973 } else {
6974 LOG(FATAL) << "Unimplemented" << source << " <-> " << destination;
6975 }
Scott Wakelingfe885462016-09-22 10:24:38 +01006976}
6977
Nicolas Geoffray13a797b2017-03-15 16:41:31 +00006978void ParallelMoveResolverARMVIXL::SpillScratch(int reg) {
6979 __ Push(vixl32::Register(reg));
Scott Wakelingfe885462016-09-22 10:24:38 +01006980}
6981
Nicolas Geoffray13a797b2017-03-15 16:41:31 +00006982void ParallelMoveResolverARMVIXL::RestoreScratch(int reg) {
6983 __ Pop(vixl32::Register(reg));
Scott Wakelingfe885462016-09-22 10:24:38 +01006984}
6985
Artem Serov02d37832016-10-25 15:25:33 +01006986HLoadClass::LoadKind CodeGeneratorARMVIXL::GetSupportedLoadClassKind(
Artem Serovd4cc5b22016-11-04 11:19:09 +00006987 HLoadClass::LoadKind desired_class_load_kind) {
6988 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006989 case HLoadClass::LoadKind::kInvalid:
6990 LOG(FATAL) << "UNREACHABLE";
6991 UNREACHABLE();
Artem Serovd4cc5b22016-11-04 11:19:09 +00006992 case HLoadClass::LoadKind::kReferrersClass:
6993 break;
Artem Serovd4cc5b22016-11-04 11:19:09 +00006994 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006995 case HLoadClass::LoadKind::kBootImageClassTable:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006996 case HLoadClass::LoadKind::kBssEntry:
6997 DCHECK(!Runtime::Current()->UseJitCompilation());
6998 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006999 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007000 DCHECK(Runtime::Current()->UseJitCompilation());
Artem Serovc5fcb442016-12-02 19:19:58 +00007001 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01007002 case HLoadClass::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007003 case HLoadClass::LoadKind::kRuntimeCall:
Artem Serovd4cc5b22016-11-04 11:19:09 +00007004 break;
7005 }
7006 return desired_class_load_kind;
Artem Serov02d37832016-10-25 15:25:33 +01007007}
7008
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007009void LocationsBuilderARMVIXL::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00007010 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007011 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007012 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00007013 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007014 cls,
7015 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00007016 LocationFrom(r0));
Vladimir Markoea4c1262017-02-06 19:59:33 +00007017 DCHECK(calling_convention.GetRegisterAt(0).Is(r0));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007018 return;
7019 }
Vladimir Marko41559982017-01-06 14:04:23 +00007020 DCHECK(!cls->NeedsAccessCheck());
Scott Wakelingfe885462016-09-22 10:24:38 +01007021
Artem Serovd4cc5b22016-11-04 11:19:09 +00007022 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
7023 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007024 ? LocationSummary::kCallOnSlowPath
7025 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01007026 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(cls, call_kind);
Artem Serovd4cc5b22016-11-04 11:19:09 +00007027 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007028 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Artem Serovd4cc5b22016-11-04 11:19:09 +00007029 }
7030
Vladimir Marko41559982017-01-06 14:04:23 +00007031 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007032 locations->SetInAt(0, Location::RequiresRegister());
7033 }
7034 locations->SetOut(Location::RequiresRegister());
Vladimir Markoea4c1262017-02-06 19:59:33 +00007035 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
7036 if (!kUseReadBarrier || kUseBakerReadBarrier) {
7037 // Rely on the type resolution or initialization and marking to save everything we need.
Vladimir Markoea4c1262017-02-06 19:59:33 +00007038 RegisterSet caller_saves = RegisterSet::Empty();
7039 InvokeRuntimeCallingConventionARMVIXL calling_convention;
7040 caller_saves.Add(LocationFrom(calling_convention.GetRegisterAt(0)));
7041 // TODO: Add GetReturnLocation() to the calling convention so that we can DCHECK()
7042 // that the the kPrimNot result register is the same as the first argument register.
7043 locations->SetCustomSlowPathCallerSaves(caller_saves);
7044 } else {
7045 // For non-Baker read barrier we have a temp-clobbering call.
7046 }
7047 }
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01007048 if (kUseBakerReadBarrier && kBakerReadBarrierLinkTimeThunksEnableForGcRoots) {
7049 if (load_kind == HLoadClass::LoadKind::kBssEntry ||
7050 (load_kind == HLoadClass::LoadKind::kReferrersClass &&
7051 !Runtime::Current()->UseJitCompilation())) {
7052 locations->AddTemp(Location::RegisterLocation(kBakerCcEntrypointRegister.GetCode()));
7053 }
7054 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007055}
7056
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007057// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
7058// move.
7059void InstructionCodeGeneratorARMVIXL::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00007060 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007061 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00007062 codegen_->GenerateLoadClassRuntimeCall(cls);
Roland Levillain5daa4952017-07-03 17:23:56 +01007063 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 13);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007064 return;
7065 }
Vladimir Marko41559982017-01-06 14:04:23 +00007066 DCHECK(!cls->NeedsAccessCheck());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007067
Vladimir Marko41559982017-01-06 14:04:23 +00007068 LocationSummary* locations = cls->GetLocations();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007069 Location out_loc = locations->Out();
7070 vixl32::Register out = OutputRegister(cls);
7071
Artem Serovd4cc5b22016-11-04 11:19:09 +00007072 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
7073 ? kWithoutReadBarrier
7074 : kCompilerReadBarrierOption;
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007075 bool generate_null_check = false;
Vladimir Marko41559982017-01-06 14:04:23 +00007076 switch (load_kind) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007077 case HLoadClass::LoadKind::kReferrersClass: {
7078 DCHECK(!cls->CanCallRuntime());
7079 DCHECK(!cls->MustGenerateClinitCheck());
7080 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
7081 vixl32::Register current_method = InputRegisterAt(cls, 0);
7082 GenerateGcRootFieldLoad(cls,
7083 out_loc,
7084 current_method,
Roland Levillain00468f32016-10-27 18:02:48 +01007085 ArtMethod::DeclaringClassOffset().Int32Value(),
Artem Serovd4cc5b22016-11-04 11:19:09 +00007086 read_barrier_option);
7087 break;
7088 }
Artem Serovd4cc5b22016-11-04 11:19:09 +00007089 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007090 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Artem Serovd4cc5b22016-11-04 11:19:09 +00007091 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
7092 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
7093 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
7094 codegen_->EmitMovwMovtPlaceholder(labels, out);
7095 break;
7096 }
7097 case HLoadClass::LoadKind::kBootImageAddress: {
Artem Serovc5fcb442016-12-02 19:19:58 +00007098 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007099 uint32_t address = dchecked_integral_cast<uint32_t>(
7100 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
7101 DCHECK_NE(address, 0u);
Artem Serovc5fcb442016-12-02 19:19:58 +00007102 __ Ldr(out, codegen_->DeduplicateBootImageAddressLiteral(address));
Artem Serovd4cc5b22016-11-04 11:19:09 +00007103 break;
7104 }
Vladimir Marko94ec2db2017-09-06 17:21:03 +01007105 case HLoadClass::LoadKind::kBootImageClassTable: {
7106 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
7107 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
7108 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
7109 codegen_->EmitMovwMovtPlaceholder(labels, out);
7110 __ Ldr(out, MemOperand(out, /* offset */ 0));
7111 // Extract the reference from the slot data, i.e. clear the hash bits.
7112 int32_t masked_hash = ClassTable::TableSlot::MaskHash(
7113 ComputeModifiedUtf8Hash(cls->GetDexFile().StringByTypeIdx(cls->GetTypeIndex())));
7114 if (masked_hash != 0) {
7115 __ Sub(out, out, Operand(masked_hash));
7116 }
7117 break;
7118 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007119 case HLoadClass::LoadKind::kBssEntry: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007120 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
Vladimir Marko1998cd02017-01-13 13:02:58 +00007121 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex());
Vladimir Markof3c52b42017-11-17 17:32:12 +00007122 codegen_->EmitMovwMovtPlaceholder(labels, out);
7123 GenerateGcRootFieldLoad(cls, out_loc, out, /* offset */ 0, read_barrier_option);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007124 generate_null_check = true;
7125 break;
7126 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007127 case HLoadClass::LoadKind::kJitTableAddress: {
Artem Serovc5fcb442016-12-02 19:19:58 +00007128 __ Ldr(out, codegen_->DeduplicateJitClassLiteral(cls->GetDexFile(),
7129 cls->GetTypeIndex(),
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007130 cls->GetClass()));
Artem Serovc5fcb442016-12-02 19:19:58 +00007131 // /* GcRoot<mirror::Class> */ out = *out
Vladimir Markoea4c1262017-02-06 19:59:33 +00007132 GenerateGcRootFieldLoad(cls, out_loc, out, /* offset */ 0, read_barrier_option);
Artem Serovd4cc5b22016-11-04 11:19:09 +00007133 break;
7134 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007135 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00007136 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00007137 LOG(FATAL) << "UNREACHABLE";
7138 UNREACHABLE();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007139 }
7140
7141 if (generate_null_check || cls->MustGenerateClinitCheck()) {
7142 DCHECK(cls->CanCallRuntime());
Vladimir Marko174b2e22017-10-12 13:34:49 +01007143 LoadClassSlowPathARMVIXL* slow_path =
7144 new (codegen_->GetScopedAllocator()) LoadClassSlowPathARMVIXL(
7145 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007146 codegen_->AddSlowPath(slow_path);
7147 if (generate_null_check) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00007148 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007149 }
7150 if (cls->MustGenerateClinitCheck()) {
7151 GenerateClassInitializationCheck(slow_path, out);
7152 } else {
7153 __ Bind(slow_path->GetExitLabel());
7154 }
Roland Levillain5daa4952017-07-03 17:23:56 +01007155 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 14);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007156 }
7157}
7158
Artem Serov02d37832016-10-25 15:25:33 +01007159void LocationsBuilderARMVIXL::VisitClinitCheck(HClinitCheck* check) {
7160 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007161 new (GetGraph()->GetAllocator()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
Artem Serov02d37832016-10-25 15:25:33 +01007162 locations->SetInAt(0, Location::RequiresRegister());
7163 if (check->HasUses()) {
7164 locations->SetOut(Location::SameAsFirstInput());
7165 }
7166}
7167
7168void InstructionCodeGeneratorARMVIXL::VisitClinitCheck(HClinitCheck* check) {
7169 // We assume the class is not null.
7170 LoadClassSlowPathARMVIXL* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01007171 new (codegen_->GetScopedAllocator()) LoadClassSlowPathARMVIXL(check->GetLoadClass(),
7172 check,
7173 check->GetDexPc(),
7174 /* do_clinit */ true);
Artem Serov02d37832016-10-25 15:25:33 +01007175 codegen_->AddSlowPath(slow_path);
7176 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
7177}
7178
7179void InstructionCodeGeneratorARMVIXL::GenerateClassInitializationCheck(
7180 LoadClassSlowPathARMVIXL* slow_path, vixl32::Register class_reg) {
7181 UseScratchRegisterScope temps(GetVIXLAssembler());
7182 vixl32::Register temp = temps.Acquire();
Vladimir Markodc682aa2018-01-04 18:42:57 +00007183 constexpr size_t status_lsb_position = SubtypeCheckBits::BitStructSizeOf();
7184 const size_t status_byte_offset =
7185 mirror::Class::StatusOffset().SizeValue() + (status_lsb_position / kBitsPerByte);
7186 constexpr uint32_t shifted_initialized_value =
7187 enum_cast<uint32_t>(ClassStatus::kInitialized) << (status_lsb_position % kBitsPerByte);
7188
7189 GetAssembler()->LoadFromOffset(kLoadUnsignedByte, temp, class_reg, status_byte_offset);
7190 __ Cmp(temp, shifted_initialized_value);
Vladimir Marko2c64a832018-01-04 11:31:56 +00007191 __ B(lo, slow_path->GetEntryLabel());
Artem Serov02d37832016-10-25 15:25:33 +01007192 // Even if the initialized flag is set, we may be in a situation where caches are not synced
7193 // properly. Therefore, we do a memory fence.
7194 __ Dmb(ISH);
7195 __ Bind(slow_path->GetExitLabel());
7196}
7197
Vladimir Markoeb0ebed2018-01-10 18:26:38 +00007198void InstructionCodeGeneratorARMVIXL::GenerateBitstringTypeCheckCompare(
7199 HTypeCheckInstruction* check,
7200 vixl32::Register temp,
7201 vixl32::FlagsUpdate flags_update) {
7202 uint32_t path_to_root = check->GetBitstringPathToRoot();
7203 uint32_t mask = check->GetBitstringMask();
7204 DCHECK(IsPowerOfTwo(mask + 1));
7205 size_t mask_bits = WhichPowerOf2(mask + 1);
7206
7207 // Note that HInstanceOf shall check for zero value in `temp` but HCheckCast needs
7208 // the Z flag for BNE. This is indicated by the `flags_update` parameter.
7209 if (mask_bits == 16u) {
7210 // Load only the bitstring part of the status word.
7211 __ Ldrh(temp, MemOperand(temp, mirror::Class::StatusOffset().Int32Value()));
7212 // Check if the bitstring bits are equal to `path_to_root`.
7213 if (flags_update == SetFlags) {
7214 __ Cmp(temp, path_to_root);
7215 } else {
7216 __ Sub(temp, temp, path_to_root);
7217 }
7218 } else {
7219 // /* uint32_t */ temp = temp->status_
7220 __ Ldr(temp, MemOperand(temp, mirror::Class::StatusOffset().Int32Value()));
7221 if (GetAssembler()->ShifterOperandCanHold(SUB, path_to_root)) {
7222 // Compare the bitstring bits using SUB.
7223 __ Sub(temp, temp, path_to_root);
7224 // Shift out bits that do not contribute to the comparison.
7225 __ Lsl(flags_update, temp, temp, dchecked_integral_cast<uint32_t>(32u - mask_bits));
7226 } else if (IsUint<16>(path_to_root)) {
7227 if (temp.IsLow()) {
7228 // Note: Optimized for size but contains one more dependent instruction than necessary.
7229 // MOVW+SUB(register) would be 8 bytes unless we find a low-reg temporary but the
7230 // macro assembler would use the high reg IP for the constant by default.
7231 // Compare the bitstring bits using SUB.
7232 __ Sub(temp, temp, path_to_root & 0x00ffu); // 16-bit SUB (immediate) T2
7233 __ Sub(temp, temp, path_to_root & 0xff00u); // 32-bit SUB (immediate) T3
7234 // Shift out bits that do not contribute to the comparison.
7235 __ Lsl(flags_update, temp, temp, dchecked_integral_cast<uint32_t>(32u - mask_bits));
7236 } else {
7237 // Extract the bitstring bits.
7238 __ Ubfx(temp, temp, 0, mask_bits);
7239 // Check if the bitstring bits are equal to `path_to_root`.
7240 if (flags_update == SetFlags) {
7241 __ Cmp(temp, path_to_root);
7242 } else {
7243 __ Sub(temp, temp, path_to_root);
7244 }
7245 }
7246 } else {
7247 // Shift out bits that do not contribute to the comparison.
7248 __ Lsl(temp, temp, dchecked_integral_cast<uint32_t>(32u - mask_bits));
7249 // Check if the shifted bitstring bits are equal to `path_to_root << (32u - mask_bits)`.
7250 if (flags_update == SetFlags) {
7251 __ Cmp(temp, path_to_root << (32u - mask_bits));
7252 } else {
7253 __ Sub(temp, temp, path_to_root << (32u - mask_bits));
7254 }
7255 }
7256 }
7257}
7258
Artem Serov02d37832016-10-25 15:25:33 +01007259HLoadString::LoadKind CodeGeneratorARMVIXL::GetSupportedLoadStringKind(
Artem Serovd4cc5b22016-11-04 11:19:09 +00007260 HLoadString::LoadKind desired_string_load_kind) {
7261 switch (desired_string_load_kind) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00007262 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007263 case HLoadString::LoadKind::kBootImageInternTable:
Artem Serovd4cc5b22016-11-04 11:19:09 +00007264 case HLoadString::LoadKind::kBssEntry:
7265 DCHECK(!Runtime::Current()->UseJitCompilation());
7266 break;
7267 case HLoadString::LoadKind::kJitTableAddress:
7268 DCHECK(Runtime::Current()->UseJitCompilation());
Artem Serovc5fcb442016-12-02 19:19:58 +00007269 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01007270 case HLoadString::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007271 case HLoadString::LoadKind::kRuntimeCall:
Artem Serovd4cc5b22016-11-04 11:19:09 +00007272 break;
7273 }
7274 return desired_string_load_kind;
Artem Serov02d37832016-10-25 15:25:33 +01007275}
7276
7277void LocationsBuilderARMVIXL::VisitLoadString(HLoadString* load) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00007278 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Vladimir Markoca6fff82017-10-03 14:49:14 +01007279 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(load, call_kind);
Artem Serov02d37832016-10-25 15:25:33 +01007280 HLoadString::LoadKind load_kind = load->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007281 if (load_kind == HLoadString::LoadKind::kRuntimeCall) {
Artem Serov02d37832016-10-25 15:25:33 +01007282 locations->SetOut(LocationFrom(r0));
7283 } else {
7284 locations->SetOut(Location::RequiresRegister());
Artem Serovd4cc5b22016-11-04 11:19:09 +00007285 if (load_kind == HLoadString::LoadKind::kBssEntry) {
7286 if (!kUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00007287 // Rely on the pResolveString and marking to save everything we need, including temps.
Artem Serovd4cc5b22016-11-04 11:19:09 +00007288 RegisterSet caller_saves = RegisterSet::Empty();
7289 InvokeRuntimeCallingConventionARMVIXL calling_convention;
7290 caller_saves.Add(LocationFrom(calling_convention.GetRegisterAt(0)));
7291 // TODO: Add GetReturnLocation() to the calling convention so that we can DCHECK()
7292 // that the the kPrimNot result register is the same as the first argument register.
7293 locations->SetCustomSlowPathCallerSaves(caller_saves);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01007294 if (kUseBakerReadBarrier && kBakerReadBarrierLinkTimeThunksEnableForGcRoots) {
7295 locations->AddTemp(Location::RegisterLocation(kBakerCcEntrypointRegister.GetCode()));
7296 }
Artem Serovd4cc5b22016-11-04 11:19:09 +00007297 } else {
7298 // For non-Baker read barrier we have a temp-clobbering call.
7299 }
7300 }
Artem Serov02d37832016-10-25 15:25:33 +01007301 }
7302}
7303
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007304// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
7305// move.
7306void InstructionCodeGeneratorARMVIXL::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Artem Serovd4cc5b22016-11-04 11:19:09 +00007307 LocationSummary* locations = load->GetLocations();
7308 Location out_loc = locations->Out();
7309 vixl32::Register out = OutputRegister(load);
7310 HLoadString::LoadKind load_kind = load->GetLoadKind();
7311
7312 switch (load_kind) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00007313 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
7314 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
7315 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007316 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Artem Serovd4cc5b22016-11-04 11:19:09 +00007317 codegen_->EmitMovwMovtPlaceholder(labels, out);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007318 return;
Artem Serovd4cc5b22016-11-04 11:19:09 +00007319 }
7320 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007321 uint32_t address = dchecked_integral_cast<uint32_t>(
7322 reinterpret_cast<uintptr_t>(load->GetString().Get()));
7323 DCHECK_NE(address, 0u);
Artem Serovc5fcb442016-12-02 19:19:58 +00007324 __ Ldr(out, codegen_->DeduplicateBootImageAddressLiteral(address));
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007325 return;
7326 }
7327 case HLoadString::LoadKind::kBootImageInternTable: {
7328 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
7329 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
7330 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
7331 codegen_->EmitMovwMovtPlaceholder(labels, out);
7332 __ Ldr(out, MemOperand(out, /* offset */ 0));
7333 return;
Artem Serovd4cc5b22016-11-04 11:19:09 +00007334 }
7335 case HLoadString::LoadKind::kBssEntry: {
7336 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Artem Serovd4cc5b22016-11-04 11:19:09 +00007337 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007338 codegen_->NewStringBssEntryPatch(load->GetDexFile(), load->GetStringIndex());
Vladimir Markof3c52b42017-11-17 17:32:12 +00007339 codegen_->EmitMovwMovtPlaceholder(labels, out);
7340 GenerateGcRootFieldLoad(load, out_loc, out, /* offset */ 0, kCompilerReadBarrierOption);
Artem Serovd4cc5b22016-11-04 11:19:09 +00007341 LoadStringSlowPathARMVIXL* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01007342 new (codegen_->GetScopedAllocator()) LoadStringSlowPathARMVIXL(load);
Artem Serovd4cc5b22016-11-04 11:19:09 +00007343 codegen_->AddSlowPath(slow_path);
7344 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
7345 __ Bind(slow_path->GetExitLabel());
Roland Levillain5daa4952017-07-03 17:23:56 +01007346 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 15);
Artem Serovd4cc5b22016-11-04 11:19:09 +00007347 return;
7348 }
7349 case HLoadString::LoadKind::kJitTableAddress: {
Artem Serovc5fcb442016-12-02 19:19:58 +00007350 __ Ldr(out, codegen_->DeduplicateJitStringLiteral(load->GetDexFile(),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007351 load->GetStringIndex(),
7352 load->GetString()));
Artem Serovc5fcb442016-12-02 19:19:58 +00007353 // /* GcRoot<mirror::String> */ out = *out
7354 GenerateGcRootFieldLoad(load, out_loc, out, /* offset */ 0, kCompilerReadBarrierOption);
7355 return;
Artem Serovd4cc5b22016-11-04 11:19:09 +00007356 }
7357 default:
7358 break;
7359 }
Artem Serov02d37832016-10-25 15:25:33 +01007360
7361 // TODO: Re-add the compiler code to do string dex cache lookup again.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007362 DCHECK_EQ(load->GetLoadKind(), HLoadString::LoadKind::kRuntimeCall);
Artem Serov02d37832016-10-25 15:25:33 +01007363 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Andreas Gampe8a0128a2016-11-28 07:38:35 -08007364 __ Mov(calling_convention.GetRegisterAt(0), load->GetStringIndex().index_);
Artem Serov02d37832016-10-25 15:25:33 +01007365 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
7366 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Roland Levillain5daa4952017-07-03 17:23:56 +01007367 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 16);
Artem Serov02d37832016-10-25 15:25:33 +01007368}
7369
7370static int32_t GetExceptionTlsOffset() {
7371 return Thread::ExceptionOffset<kArmPointerSize>().Int32Value();
7372}
7373
7374void LocationsBuilderARMVIXL::VisitLoadException(HLoadException* load) {
7375 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007376 new (GetGraph()->GetAllocator()) LocationSummary(load, LocationSummary::kNoCall);
Artem Serov02d37832016-10-25 15:25:33 +01007377 locations->SetOut(Location::RequiresRegister());
7378}
7379
7380void InstructionCodeGeneratorARMVIXL::VisitLoadException(HLoadException* load) {
7381 vixl32::Register out = OutputRegister(load);
7382 GetAssembler()->LoadFromOffset(kLoadWord, out, tr, GetExceptionTlsOffset());
7383}
7384
7385
7386void LocationsBuilderARMVIXL::VisitClearException(HClearException* clear) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007387 new (GetGraph()->GetAllocator()) LocationSummary(clear, LocationSummary::kNoCall);
Artem Serov02d37832016-10-25 15:25:33 +01007388}
7389
7390void InstructionCodeGeneratorARMVIXL::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
7391 UseScratchRegisterScope temps(GetVIXLAssembler());
7392 vixl32::Register temp = temps.Acquire();
7393 __ Mov(temp, 0);
7394 GetAssembler()->StoreToOffset(kStoreWord, temp, tr, GetExceptionTlsOffset());
7395}
7396
7397void LocationsBuilderARMVIXL::VisitThrow(HThrow* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007398 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
7399 instruction, LocationSummary::kCallOnMainOnly);
Artem Serov02d37832016-10-25 15:25:33 +01007400 InvokeRuntimeCallingConventionARMVIXL calling_convention;
7401 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
7402}
7403
7404void InstructionCodeGeneratorARMVIXL::VisitThrow(HThrow* instruction) {
7405 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
7406 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
7407}
7408
Artem Serov657022c2016-11-23 14:19:38 +00007409// Temp is used for read barrier.
7410static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
7411 if (kEmitCompilerReadBarrier &&
7412 (kUseBakerReadBarrier ||
7413 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
7414 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
7415 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
7416 return 1;
7417 }
7418 return 0;
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007419}
7420
Artem Serov657022c2016-11-23 14:19:38 +00007421// Interface case has 3 temps, one for holding the number of interfaces, one for the current
7422// interface pointer, one for loading the current interface.
7423// The other checks have one temp for loading the object's class.
7424static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
7425 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
7426 return 3;
7427 }
7428 return 1 + NumberOfInstanceOfTemps(type_check_kind);
7429}
Artem Serovcfbe9132016-10-14 15:58:56 +01007430
7431void LocationsBuilderARMVIXL::VisitInstanceOf(HInstanceOf* instruction) {
7432 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
7433 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
7434 bool baker_read_barrier_slow_path = false;
7435 switch (type_check_kind) {
7436 case TypeCheckKind::kExactCheck:
7437 case TypeCheckKind::kAbstractClassCheck:
7438 case TypeCheckKind::kClassHierarchyCheck:
Vladimir Marko87584542017-12-12 17:47:52 +00007439 case TypeCheckKind::kArrayObjectCheck: {
7440 bool needs_read_barrier = CodeGenerator::InstanceOfNeedsReadBarrier(instruction);
7441 call_kind = needs_read_barrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
7442 baker_read_barrier_slow_path = kUseBakerReadBarrier && needs_read_barrier;
Artem Serovcfbe9132016-10-14 15:58:56 +01007443 break;
Vladimir Marko87584542017-12-12 17:47:52 +00007444 }
Artem Serovcfbe9132016-10-14 15:58:56 +01007445 case TypeCheckKind::kArrayCheck:
7446 case TypeCheckKind::kUnresolvedCheck:
7447 case TypeCheckKind::kInterfaceCheck:
7448 call_kind = LocationSummary::kCallOnSlowPath;
7449 break;
Vladimir Markoeb0ebed2018-01-10 18:26:38 +00007450 case TypeCheckKind::kBitstringCheck:
7451 break;
Artem Serovcfbe9132016-10-14 15:58:56 +01007452 }
7453
Vladimir Markoca6fff82017-10-03 14:49:14 +01007454 LocationSummary* locations =
7455 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Artem Serovcfbe9132016-10-14 15:58:56 +01007456 if (baker_read_barrier_slow_path) {
7457 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
7458 }
7459 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Markoeb0ebed2018-01-10 18:26:38 +00007460 if (type_check_kind == TypeCheckKind::kBitstringCheck) {
7461 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
7462 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
7463 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
7464 } else {
7465 locations->SetInAt(1, Location::RequiresRegister());
7466 }
Artem Serovcfbe9132016-10-14 15:58:56 +01007467 // The "out" register is used as a temporary, so it overlaps with the inputs.
7468 // Note that TypeCheckSlowPathARM uses this register too.
7469 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Artem Serov657022c2016-11-23 14:19:38 +00007470 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01007471 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
7472 codegen_->MaybeAddBakerCcEntrypointTempForFields(locations);
7473 }
Artem Serovcfbe9132016-10-14 15:58:56 +01007474}
7475
7476void InstructionCodeGeneratorARMVIXL::VisitInstanceOf(HInstanceOf* instruction) {
7477 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
7478 LocationSummary* locations = instruction->GetLocations();
7479 Location obj_loc = locations->InAt(0);
7480 vixl32::Register obj = InputRegisterAt(instruction, 0);
Vladimir Markoeb0ebed2018-01-10 18:26:38 +00007481 vixl32::Register cls = (type_check_kind == TypeCheckKind::kBitstringCheck)
7482 ? vixl32::Register()
7483 : InputRegisterAt(instruction, 1);
Artem Serovcfbe9132016-10-14 15:58:56 +01007484 Location out_loc = locations->Out();
7485 vixl32::Register out = OutputRegister(instruction);
Artem Serov657022c2016-11-23 14:19:38 +00007486 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
7487 DCHECK_LE(num_temps, 1u);
7488 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Artem Serovcfbe9132016-10-14 15:58:56 +01007489 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
7490 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
7491 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
7492 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007493 vixl32::Label done;
7494 vixl32::Label* const final_label = codegen_->GetFinalLabel(instruction, &done);
Artem Serovcfbe9132016-10-14 15:58:56 +01007495 SlowPathCodeARMVIXL* slow_path = nullptr;
7496
7497 // Return 0 if `obj` is null.
7498 // avoid null check if we know obj is not null.
7499 if (instruction->MustDoNullCheck()) {
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007500 DCHECK(!out.Is(obj));
7501 __ Mov(out, 0);
7502 __ CompareAndBranchIfZero(obj, final_label, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007503 }
7504
Artem Serovcfbe9132016-10-14 15:58:56 +01007505 switch (type_check_kind) {
7506 case TypeCheckKind::kExactCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00007507 ReadBarrierOption read_barrier_option =
7508 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier6beced42016-11-15 15:51:31 -08007509 // /* HeapReference<Class> */ out = obj->klass_
7510 GenerateReferenceLoadTwoRegisters(instruction,
7511 out_loc,
7512 obj_loc,
7513 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00007514 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007515 read_barrier_option);
Artem Serovcfbe9132016-10-14 15:58:56 +01007516 // Classes must be equal for the instanceof to succeed.
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007517 __ Cmp(out, cls);
7518 // We speculatively set the result to false without changing the condition
7519 // flags, which allows us to avoid some branching later.
7520 __ Mov(LeaveFlags, out, 0);
7521
7522 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
7523 // we check that the output is in a low register, so that a 16-bit MOV
7524 // encoding can be used.
7525 if (out.IsLow()) {
7526 // We use the scope because of the IT block that follows.
7527 ExactAssemblyScope guard(GetVIXLAssembler(),
7528 2 * vixl32::k16BitT32InstructionSizeInBytes,
7529 CodeBufferCheckScope::kExactSize);
7530
7531 __ it(eq);
7532 __ mov(eq, out, 1);
7533 } else {
7534 __ B(ne, final_label, /* far_target */ false);
7535 __ Mov(out, 1);
7536 }
7537
Artem Serovcfbe9132016-10-14 15:58:56 +01007538 break;
7539 }
7540
7541 case TypeCheckKind::kAbstractClassCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00007542 ReadBarrierOption read_barrier_option =
7543 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier6beced42016-11-15 15:51:31 -08007544 // /* HeapReference<Class> */ out = obj->klass_
7545 GenerateReferenceLoadTwoRegisters(instruction,
7546 out_loc,
7547 obj_loc,
7548 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00007549 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007550 read_barrier_option);
Artem Serovcfbe9132016-10-14 15:58:56 +01007551 // If the class is abstract, we eagerly fetch the super class of the
7552 // object to avoid doing a comparison we know will fail.
7553 vixl32::Label loop;
7554 __ Bind(&loop);
7555 // /* HeapReference<Class> */ out = out->super_class_
Artem Serov657022c2016-11-23 14:19:38 +00007556 GenerateReferenceLoadOneRegister(instruction,
7557 out_loc,
7558 super_offset,
7559 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007560 read_barrier_option);
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007561 // If `out` is null, we use it for the result, and jump to the final label.
Anton Kirilov6f644202017-02-27 18:29:45 +00007562 __ CompareAndBranchIfZero(out, final_label, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007563 __ Cmp(out, cls);
Artem Serov517d9f62016-12-12 15:51:15 +00007564 __ B(ne, &loop, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007565 __ Mov(out, 1);
Artem Serovcfbe9132016-10-14 15:58:56 +01007566 break;
7567 }
7568
7569 case TypeCheckKind::kClassHierarchyCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00007570 ReadBarrierOption read_barrier_option =
7571 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier6beced42016-11-15 15:51:31 -08007572 // /* HeapReference<Class> */ out = obj->klass_
7573 GenerateReferenceLoadTwoRegisters(instruction,
7574 out_loc,
7575 obj_loc,
7576 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00007577 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007578 read_barrier_option);
Artem Serovcfbe9132016-10-14 15:58:56 +01007579 // Walk over the class hierarchy to find a match.
7580 vixl32::Label loop, success;
7581 __ Bind(&loop);
7582 __ Cmp(out, cls);
Artem Serov517d9f62016-12-12 15:51:15 +00007583 __ B(eq, &success, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007584 // /* HeapReference<Class> */ out = out->super_class_
Artem Serov657022c2016-11-23 14:19:38 +00007585 GenerateReferenceLoadOneRegister(instruction,
7586 out_loc,
7587 super_offset,
7588 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007589 read_barrier_option);
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007590 // This is essentially a null check, but it sets the condition flags to the
7591 // proper value for the code that follows the loop, i.e. not `eq`.
7592 __ Cmp(out, 1);
7593 __ B(hs, &loop, /* far_target */ false);
7594
7595 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
7596 // we check that the output is in a low register, so that a 16-bit MOV
7597 // encoding can be used.
7598 if (out.IsLow()) {
7599 // If `out` is null, we use it for the result, and the condition flags
7600 // have already been set to `ne`, so the IT block that comes afterwards
7601 // (and which handles the successful case) turns into a NOP (instead of
7602 // overwriting `out`).
7603 __ Bind(&success);
7604
7605 // We use the scope because of the IT block that follows.
7606 ExactAssemblyScope guard(GetVIXLAssembler(),
7607 2 * vixl32::k16BitT32InstructionSizeInBytes,
7608 CodeBufferCheckScope::kExactSize);
7609
7610 // There is only one branch to the `success` label (which is bound to this
7611 // IT block), and it has the same condition, `eq`, so in that case the MOV
7612 // is executed.
7613 __ it(eq);
7614 __ mov(eq, out, 1);
7615 } else {
7616 // If `out` is null, we use it for the result, and jump to the final label.
Anton Kirilov6f644202017-02-27 18:29:45 +00007617 __ B(final_label);
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007618 __ Bind(&success);
7619 __ Mov(out, 1);
Artem Serovcfbe9132016-10-14 15:58:56 +01007620 }
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007621
Artem Serovcfbe9132016-10-14 15:58:56 +01007622 break;
7623 }
7624
7625 case TypeCheckKind::kArrayObjectCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00007626 ReadBarrierOption read_barrier_option =
7627 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier6beced42016-11-15 15:51:31 -08007628 // /* HeapReference<Class> */ out = obj->klass_
7629 GenerateReferenceLoadTwoRegisters(instruction,
7630 out_loc,
7631 obj_loc,
7632 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00007633 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007634 read_barrier_option);
Artem Serovcfbe9132016-10-14 15:58:56 +01007635 // Do an exact check.
7636 vixl32::Label exact_check;
7637 __ Cmp(out, cls);
Artem Serov517d9f62016-12-12 15:51:15 +00007638 __ B(eq, &exact_check, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007639 // Otherwise, we need to check that the object's class is a non-primitive array.
7640 // /* HeapReference<Class> */ out = out->component_type_
Artem Serov657022c2016-11-23 14:19:38 +00007641 GenerateReferenceLoadOneRegister(instruction,
7642 out_loc,
7643 component_offset,
7644 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007645 read_barrier_option);
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007646 // If `out` is null, we use it for the result, and jump to the final label.
Anton Kirilov6f644202017-02-27 18:29:45 +00007647 __ CompareAndBranchIfZero(out, final_label, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007648 GetAssembler()->LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
7649 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007650 __ Cmp(out, 0);
7651 // We speculatively set the result to false without changing the condition
7652 // flags, which allows us to avoid some branching later.
7653 __ Mov(LeaveFlags, out, 0);
7654
7655 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
7656 // we check that the output is in a low register, so that a 16-bit MOV
7657 // encoding can be used.
7658 if (out.IsLow()) {
7659 __ Bind(&exact_check);
7660
7661 // We use the scope because of the IT block that follows.
7662 ExactAssemblyScope guard(GetVIXLAssembler(),
7663 2 * vixl32::k16BitT32InstructionSizeInBytes,
7664 CodeBufferCheckScope::kExactSize);
7665
7666 __ it(eq);
7667 __ mov(eq, out, 1);
7668 } else {
7669 __ B(ne, final_label, /* far_target */ false);
7670 __ Bind(&exact_check);
7671 __ Mov(out, 1);
7672 }
7673
Artem Serovcfbe9132016-10-14 15:58:56 +01007674 break;
7675 }
7676
7677 case TypeCheckKind::kArrayCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00007678 // No read barrier since the slow path will retry upon failure.
Mathieu Chartier6beced42016-11-15 15:51:31 -08007679 // /* HeapReference<Class> */ out = obj->klass_
7680 GenerateReferenceLoadTwoRegisters(instruction,
7681 out_loc,
7682 obj_loc,
7683 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00007684 maybe_temp_loc,
7685 kWithoutReadBarrier);
Artem Serovcfbe9132016-10-14 15:58:56 +01007686 __ Cmp(out, cls);
7687 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01007688 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathARMVIXL(
7689 instruction, /* is_fatal */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007690 codegen_->AddSlowPath(slow_path);
7691 __ B(ne, slow_path->GetEntryLabel());
7692 __ Mov(out, 1);
Artem Serovcfbe9132016-10-14 15:58:56 +01007693 break;
7694 }
7695
7696 case TypeCheckKind::kUnresolvedCheck:
7697 case TypeCheckKind::kInterfaceCheck: {
7698 // Note that we indeed only call on slow path, but we always go
7699 // into the slow path for the unresolved and interface check
7700 // cases.
7701 //
7702 // We cannot directly call the InstanceofNonTrivial runtime
7703 // entry point without resorting to a type checking slow path
7704 // here (i.e. by calling InvokeRuntime directly), as it would
7705 // require to assign fixed registers for the inputs of this
7706 // HInstanceOf instruction (following the runtime calling
7707 // convention), which might be cluttered by the potential first
7708 // read barrier emission at the beginning of this method.
7709 //
7710 // TODO: Introduce a new runtime entry point taking the object
7711 // to test (instead of its class) as argument, and let it deal
7712 // with the read barrier issues. This will let us refactor this
7713 // case of the `switch` code as it was previously (with a direct
7714 // call to the runtime not using a type checking slow path).
7715 // This should also be beneficial for the other cases above.
7716 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01007717 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathARMVIXL(
7718 instruction, /* is_fatal */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007719 codegen_->AddSlowPath(slow_path);
7720 __ B(slow_path->GetEntryLabel());
Artem Serovcfbe9132016-10-14 15:58:56 +01007721 break;
7722 }
Vladimir Markoeb0ebed2018-01-10 18:26:38 +00007723
7724 case TypeCheckKind::kBitstringCheck: {
7725 // /* HeapReference<Class> */ temp = obj->klass_
7726 GenerateReferenceLoadTwoRegisters(instruction,
7727 out_loc,
7728 obj_loc,
7729 class_offset,
7730 maybe_temp_loc,
7731 kWithoutReadBarrier);
7732
7733 GenerateBitstringTypeCheckCompare(instruction, out, DontCare);
7734 // If `out` is a low reg and we would have another low reg temp, we could
7735 // optimize this as RSBS+ADC, see GenerateConditionWithZero().
7736 //
7737 // Also, in some cases when `out` is a low reg and we're loading a constant to IP
7738 // it would make sense to use CMP+MOV+IT+MOV instead of SUB+CLZ+LSR as the code size
7739 // would be the same and we would have fewer direct data dependencies.
7740 codegen_->GenerateConditionWithZero(kCondEQ, out, out); // CLZ+LSR
7741 break;
7742 }
Artem Serovcfbe9132016-10-14 15:58:56 +01007743 }
7744
Artem Serovcfbe9132016-10-14 15:58:56 +01007745 if (done.IsReferenced()) {
7746 __ Bind(&done);
7747 }
7748
7749 if (slow_path != nullptr) {
7750 __ Bind(slow_path->GetExitLabel());
7751 }
7752}
7753
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007754void LocationsBuilderARMVIXL::VisitCheckCast(HCheckCast* instruction) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007755 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko87584542017-12-12 17:47:52 +00007756 LocationSummary::CallKind call_kind = CodeGenerator::GetCheckCastCallKind(instruction);
Vladimir Markoca6fff82017-10-03 14:49:14 +01007757 LocationSummary* locations =
7758 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007759 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Markoeb0ebed2018-01-10 18:26:38 +00007760 if (type_check_kind == TypeCheckKind::kBitstringCheck) {
7761 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
7762 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
7763 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
7764 } else {
7765 locations->SetInAt(1, Location::RequiresRegister());
7766 }
Artem Serov657022c2016-11-23 14:19:38 +00007767 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007768}
7769
7770void InstructionCodeGeneratorARMVIXL::VisitCheckCast(HCheckCast* instruction) {
7771 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
7772 LocationSummary* locations = instruction->GetLocations();
7773 Location obj_loc = locations->InAt(0);
7774 vixl32::Register obj = InputRegisterAt(instruction, 0);
Vladimir Markoeb0ebed2018-01-10 18:26:38 +00007775 vixl32::Register cls = (type_check_kind == TypeCheckKind::kBitstringCheck)
7776 ? vixl32::Register()
7777 : InputRegisterAt(instruction, 1);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007778 Location temp_loc = locations->GetTemp(0);
7779 vixl32::Register temp = RegisterFrom(temp_loc);
Artem Serov657022c2016-11-23 14:19:38 +00007780 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
7781 DCHECK_LE(num_temps, 3u);
7782 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
7783 Location maybe_temp3_loc = (num_temps >= 3) ? locations->GetTemp(2) : Location::NoLocation();
7784 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
7785 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
7786 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
7787 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
7788 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
7789 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
7790 const uint32_t object_array_data_offset =
7791 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007792
Vladimir Marko87584542017-12-12 17:47:52 +00007793 bool is_type_check_slow_path_fatal = CodeGenerator::IsTypeCheckSlowPathFatal(instruction);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007794 SlowPathCodeARMVIXL* type_check_slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01007795 new (codegen_->GetScopedAllocator()) TypeCheckSlowPathARMVIXL(
7796 instruction, is_type_check_slow_path_fatal);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007797 codegen_->AddSlowPath(type_check_slow_path);
7798
7799 vixl32::Label done;
Anton Kirilov6f644202017-02-27 18:29:45 +00007800 vixl32::Label* final_label = codegen_->GetFinalLabel(instruction, &done);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007801 // Avoid null check if we know obj is not null.
7802 if (instruction->MustDoNullCheck()) {
Anton Kirilov6f644202017-02-27 18:29:45 +00007803 __ CompareAndBranchIfZero(obj, final_label, /* far_target */ false);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007804 }
7805
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007806 switch (type_check_kind) {
7807 case TypeCheckKind::kExactCheck:
7808 case TypeCheckKind::kArrayCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00007809 // /* HeapReference<Class> */ temp = obj->klass_
7810 GenerateReferenceLoadTwoRegisters(instruction,
7811 temp_loc,
7812 obj_loc,
7813 class_offset,
7814 maybe_temp2_loc,
7815 kWithoutReadBarrier);
7816
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007817 __ Cmp(temp, cls);
7818 // Jump to slow path for throwing the exception or doing a
7819 // more involved array check.
7820 __ B(ne, type_check_slow_path->GetEntryLabel());
7821 break;
7822 }
7823
7824 case TypeCheckKind::kAbstractClassCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00007825 // /* HeapReference<Class> */ temp = obj->klass_
7826 GenerateReferenceLoadTwoRegisters(instruction,
7827 temp_loc,
7828 obj_loc,
7829 class_offset,
7830 maybe_temp2_loc,
7831 kWithoutReadBarrier);
7832
Artem Serovcfbe9132016-10-14 15:58:56 +01007833 // If the class is abstract, we eagerly fetch the super class of the
7834 // object to avoid doing a comparison we know will fail.
7835 vixl32::Label loop;
7836 __ Bind(&loop);
7837 // /* HeapReference<Class> */ temp = temp->super_class_
Artem Serov657022c2016-11-23 14:19:38 +00007838 GenerateReferenceLoadOneRegister(instruction,
7839 temp_loc,
7840 super_offset,
7841 maybe_temp2_loc,
7842 kWithoutReadBarrier);
Artem Serovcfbe9132016-10-14 15:58:56 +01007843
7844 // If the class reference currently in `temp` is null, jump to the slow path to throw the
7845 // exception.
xueliang.zhongf51bc622016-11-04 09:23:32 +00007846 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
Artem Serovcfbe9132016-10-14 15:58:56 +01007847
7848 // Otherwise, compare the classes.
7849 __ Cmp(temp, cls);
Artem Serov517d9f62016-12-12 15:51:15 +00007850 __ B(ne, &loop, /* far_target */ false);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007851 break;
7852 }
7853
7854 case TypeCheckKind::kClassHierarchyCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00007855 // /* HeapReference<Class> */ temp = obj->klass_
7856 GenerateReferenceLoadTwoRegisters(instruction,
7857 temp_loc,
7858 obj_loc,
7859 class_offset,
7860 maybe_temp2_loc,
7861 kWithoutReadBarrier);
7862
Artem Serovcfbe9132016-10-14 15:58:56 +01007863 // Walk over the class hierarchy to find a match.
7864 vixl32::Label loop;
7865 __ Bind(&loop);
7866 __ Cmp(temp, cls);
Anton Kirilov6f644202017-02-27 18:29:45 +00007867 __ B(eq, final_label, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007868
7869 // /* HeapReference<Class> */ temp = temp->super_class_
Artem Serov657022c2016-11-23 14:19:38 +00007870 GenerateReferenceLoadOneRegister(instruction,
7871 temp_loc,
7872 super_offset,
7873 maybe_temp2_loc,
7874 kWithoutReadBarrier);
Artem Serovcfbe9132016-10-14 15:58:56 +01007875
7876 // If the class reference currently in `temp` is null, jump to the slow path to throw the
7877 // exception.
xueliang.zhongf51bc622016-11-04 09:23:32 +00007878 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
Artem Serovcfbe9132016-10-14 15:58:56 +01007879 // Otherwise, jump to the beginning of the loop.
7880 __ B(&loop);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007881 break;
7882 }
7883
Artem Serovcfbe9132016-10-14 15:58:56 +01007884 case TypeCheckKind::kArrayObjectCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00007885 // /* HeapReference<Class> */ temp = obj->klass_
7886 GenerateReferenceLoadTwoRegisters(instruction,
7887 temp_loc,
7888 obj_loc,
7889 class_offset,
7890 maybe_temp2_loc,
7891 kWithoutReadBarrier);
7892
Artem Serovcfbe9132016-10-14 15:58:56 +01007893 // Do an exact check.
7894 __ Cmp(temp, cls);
Anton Kirilov6f644202017-02-27 18:29:45 +00007895 __ B(eq, final_label, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007896
7897 // Otherwise, we need to check that the object's class is a non-primitive array.
7898 // /* HeapReference<Class> */ temp = temp->component_type_
Artem Serov657022c2016-11-23 14:19:38 +00007899 GenerateReferenceLoadOneRegister(instruction,
7900 temp_loc,
7901 component_offset,
7902 maybe_temp2_loc,
7903 kWithoutReadBarrier);
Artem Serovcfbe9132016-10-14 15:58:56 +01007904 // If the component type is null, jump to the slow path to throw the exception.
xueliang.zhongf51bc622016-11-04 09:23:32 +00007905 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
Artem Serovcfbe9132016-10-14 15:58:56 +01007906 // Otherwise,the object is indeed an array, jump to label `check_non_primitive_component_type`
7907 // to further check that this component type is not a primitive type.
7908 GetAssembler()->LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007909 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
xueliang.zhongf51bc622016-11-04 09:23:32 +00007910 __ CompareAndBranchIfNonZero(temp, type_check_slow_path->GetEntryLabel());
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007911 break;
7912 }
7913
7914 case TypeCheckKind::kUnresolvedCheck:
Artem Serov657022c2016-11-23 14:19:38 +00007915 // We always go into the type check slow path for the unresolved check case.
Artem Serovcfbe9132016-10-14 15:58:56 +01007916 // We cannot directly call the CheckCast runtime entry point
7917 // without resorting to a type checking slow path here (i.e. by
7918 // calling InvokeRuntime directly), as it would require to
7919 // assign fixed registers for the inputs of this HInstanceOf
7920 // instruction (following the runtime calling convention), which
7921 // might be cluttered by the potential first read barrier
7922 // emission at the beginning of this method.
Artem Serov657022c2016-11-23 14:19:38 +00007923
Artem Serovcfbe9132016-10-14 15:58:56 +01007924 __ B(type_check_slow_path->GetEntryLabel());
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007925 break;
Artem Serov657022c2016-11-23 14:19:38 +00007926
7927 case TypeCheckKind::kInterfaceCheck: {
7928 // Avoid read barriers to improve performance of the fast path. We can not get false
7929 // positives by doing this.
7930 // /* HeapReference<Class> */ temp = obj->klass_
7931 GenerateReferenceLoadTwoRegisters(instruction,
7932 temp_loc,
7933 obj_loc,
7934 class_offset,
7935 maybe_temp2_loc,
7936 kWithoutReadBarrier);
7937
7938 // /* HeapReference<Class> */ temp = temp->iftable_
7939 GenerateReferenceLoadTwoRegisters(instruction,
7940 temp_loc,
7941 temp_loc,
7942 iftable_offset,
7943 maybe_temp2_loc,
7944 kWithoutReadBarrier);
7945 // Iftable is never null.
7946 __ Ldr(RegisterFrom(maybe_temp2_loc), MemOperand(temp, array_length_offset));
7947 // Loop through the iftable and check if any class matches.
7948 vixl32::Label start_loop;
7949 __ Bind(&start_loop);
7950 __ CompareAndBranchIfZero(RegisterFrom(maybe_temp2_loc),
7951 type_check_slow_path->GetEntryLabel());
7952 __ Ldr(RegisterFrom(maybe_temp3_loc), MemOperand(temp, object_array_data_offset));
7953 GetAssembler()->MaybeUnpoisonHeapReference(RegisterFrom(maybe_temp3_loc));
7954 // Go to next interface.
7955 __ Add(temp, temp, Operand::From(2 * kHeapReferenceSize));
7956 __ Sub(RegisterFrom(maybe_temp2_loc), RegisterFrom(maybe_temp2_loc), 2);
7957 // Compare the classes and continue the loop if they do not match.
7958 __ Cmp(cls, RegisterFrom(maybe_temp3_loc));
Artem Serov517d9f62016-12-12 15:51:15 +00007959 __ B(ne, &start_loop, /* far_target */ false);
Artem Serov657022c2016-11-23 14:19:38 +00007960 break;
7961 }
Vladimir Markoeb0ebed2018-01-10 18:26:38 +00007962
7963 case TypeCheckKind::kBitstringCheck: {
7964 // /* HeapReference<Class> */ temp = obj->klass_
7965 GenerateReferenceLoadTwoRegisters(instruction,
7966 temp_loc,
7967 obj_loc,
7968 class_offset,
7969 maybe_temp2_loc,
7970 kWithoutReadBarrier);
7971
7972 GenerateBitstringTypeCheckCompare(instruction, temp, SetFlags);
7973 __ B(ne, type_check_slow_path->GetEntryLabel());
7974 break;
7975 }
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007976 }
Anton Kirilov6f644202017-02-27 18:29:45 +00007977 if (done.IsReferenced()) {
7978 __ Bind(&done);
7979 }
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007980
7981 __ Bind(type_check_slow_path->GetExitLabel());
7982}
7983
Artem Serov551b28f2016-10-18 19:11:30 +01007984void LocationsBuilderARMVIXL::VisitMonitorOperation(HMonitorOperation* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007985 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
7986 instruction, LocationSummary::kCallOnMainOnly);
Artem Serov551b28f2016-10-18 19:11:30 +01007987 InvokeRuntimeCallingConventionARMVIXL calling_convention;
7988 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
7989}
7990
7991void InstructionCodeGeneratorARMVIXL::VisitMonitorOperation(HMonitorOperation* instruction) {
7992 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
7993 instruction,
7994 instruction->GetDexPc());
7995 if (instruction->IsEnter()) {
7996 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
7997 } else {
7998 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
7999 }
Roland Levillain5daa4952017-07-03 17:23:56 +01008000 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 17);
Artem Serov551b28f2016-10-18 19:11:30 +01008001}
8002
Artem Serov02109dd2016-09-23 17:17:54 +01008003void LocationsBuilderARMVIXL::VisitAnd(HAnd* instruction) {
8004 HandleBitwiseOperation(instruction, AND);
8005}
8006
8007void LocationsBuilderARMVIXL::VisitOr(HOr* instruction) {
8008 HandleBitwiseOperation(instruction, ORR);
8009}
8010
8011void LocationsBuilderARMVIXL::VisitXor(HXor* instruction) {
8012 HandleBitwiseOperation(instruction, EOR);
8013}
8014
8015void LocationsBuilderARMVIXL::HandleBitwiseOperation(HBinaryOperation* instruction, Opcode opcode) {
8016 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008017 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008018 DCHECK(instruction->GetResultType() == DataType::Type::kInt32
8019 || instruction->GetResultType() == DataType::Type::kInt64);
Artem Serov02109dd2016-09-23 17:17:54 +01008020 // Note: GVN reorders commutative operations to have the constant on the right hand side.
8021 locations->SetInAt(0, Location::RequiresRegister());
8022 locations->SetInAt(1, ArmEncodableConstantOrRegister(instruction->InputAt(1), opcode));
8023 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8024}
8025
8026void InstructionCodeGeneratorARMVIXL::VisitAnd(HAnd* instruction) {
8027 HandleBitwiseOperation(instruction);
8028}
8029
8030void InstructionCodeGeneratorARMVIXL::VisitOr(HOr* instruction) {
8031 HandleBitwiseOperation(instruction);
8032}
8033
8034void InstructionCodeGeneratorARMVIXL::VisitXor(HXor* instruction) {
8035 HandleBitwiseOperation(instruction);
8036}
8037
Artem Serov2bbc9532016-10-21 11:51:50 +01008038void LocationsBuilderARMVIXL::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
8039 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008040 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008041 DCHECK(instruction->GetResultType() == DataType::Type::kInt32
8042 || instruction->GetResultType() == DataType::Type::kInt64);
Artem Serov2bbc9532016-10-21 11:51:50 +01008043
8044 locations->SetInAt(0, Location::RequiresRegister());
8045 locations->SetInAt(1, Location::RequiresRegister());
8046 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8047}
8048
8049void InstructionCodeGeneratorARMVIXL::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
8050 LocationSummary* locations = instruction->GetLocations();
8051 Location first = locations->InAt(0);
8052 Location second = locations->InAt(1);
8053 Location out = locations->Out();
8054
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008055 if (instruction->GetResultType() == DataType::Type::kInt32) {
Artem Serov2bbc9532016-10-21 11:51:50 +01008056 vixl32::Register first_reg = RegisterFrom(first);
8057 vixl32::Register second_reg = RegisterFrom(second);
8058 vixl32::Register out_reg = RegisterFrom(out);
8059
8060 switch (instruction->GetOpKind()) {
8061 case HInstruction::kAnd:
8062 __ Bic(out_reg, first_reg, second_reg);
8063 break;
8064 case HInstruction::kOr:
8065 __ Orn(out_reg, first_reg, second_reg);
8066 break;
8067 // There is no EON on arm.
8068 case HInstruction::kXor:
8069 default:
8070 LOG(FATAL) << "Unexpected instruction " << instruction->DebugName();
8071 UNREACHABLE();
8072 }
8073 return;
8074
8075 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008076 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
Artem Serov2bbc9532016-10-21 11:51:50 +01008077 vixl32::Register first_low = LowRegisterFrom(first);
8078 vixl32::Register first_high = HighRegisterFrom(first);
8079 vixl32::Register second_low = LowRegisterFrom(second);
8080 vixl32::Register second_high = HighRegisterFrom(second);
8081 vixl32::Register out_low = LowRegisterFrom(out);
8082 vixl32::Register out_high = HighRegisterFrom(out);
8083
8084 switch (instruction->GetOpKind()) {
8085 case HInstruction::kAnd:
8086 __ Bic(out_low, first_low, second_low);
8087 __ Bic(out_high, first_high, second_high);
8088 break;
8089 case HInstruction::kOr:
8090 __ Orn(out_low, first_low, second_low);
8091 __ Orn(out_high, first_high, second_high);
8092 break;
8093 // There is no EON on arm.
8094 case HInstruction::kXor:
8095 default:
8096 LOG(FATAL) << "Unexpected instruction " << instruction->DebugName();
8097 UNREACHABLE();
8098 }
8099 }
8100}
8101
Anton Kirilov74234da2017-01-13 14:42:47 +00008102void LocationsBuilderARMVIXL::VisitDataProcWithShifterOp(
8103 HDataProcWithShifterOp* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008104 DCHECK(instruction->GetType() == DataType::Type::kInt32 ||
8105 instruction->GetType() == DataType::Type::kInt64);
Anton Kirilov74234da2017-01-13 14:42:47 +00008106 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008107 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008108 const bool overlap = instruction->GetType() == DataType::Type::kInt64 &&
Anton Kirilov74234da2017-01-13 14:42:47 +00008109 HDataProcWithShifterOp::IsExtensionOp(instruction->GetOpKind());
8110
8111 locations->SetInAt(0, Location::RequiresRegister());
8112 locations->SetInAt(1, Location::RequiresRegister());
8113 locations->SetOut(Location::RequiresRegister(),
8114 overlap ? Location::kOutputOverlap : Location::kNoOutputOverlap);
8115}
8116
8117void InstructionCodeGeneratorARMVIXL::VisitDataProcWithShifterOp(
8118 HDataProcWithShifterOp* instruction) {
8119 const LocationSummary* const locations = instruction->GetLocations();
8120 const HInstruction::InstructionKind kind = instruction->GetInstrKind();
8121 const HDataProcWithShifterOp::OpKind op_kind = instruction->GetOpKind();
8122
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008123 if (instruction->GetType() == DataType::Type::kInt32) {
Anton Kirilov420ee302017-02-21 18:10:26 +00008124 const vixl32::Register first = InputRegisterAt(instruction, 0);
8125 const vixl32::Register output = OutputRegister(instruction);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008126 const vixl32::Register second = instruction->InputAt(1)->GetType() == DataType::Type::kInt64
Anton Kirilov74234da2017-01-13 14:42:47 +00008127 ? LowRegisterFrom(locations->InAt(1))
8128 : InputRegisterAt(instruction, 1);
8129
Anton Kirilov420ee302017-02-21 18:10:26 +00008130 if (HDataProcWithShifterOp::IsExtensionOp(op_kind)) {
8131 DCHECK_EQ(kind, HInstruction::kAdd);
8132
8133 switch (op_kind) {
8134 case HDataProcWithShifterOp::kUXTB:
8135 __ Uxtab(output, first, second);
8136 break;
8137 case HDataProcWithShifterOp::kUXTH:
8138 __ Uxtah(output, first, second);
8139 break;
8140 case HDataProcWithShifterOp::kSXTB:
8141 __ Sxtab(output, first, second);
8142 break;
8143 case HDataProcWithShifterOp::kSXTH:
8144 __ Sxtah(output, first, second);
8145 break;
8146 default:
8147 LOG(FATAL) << "Unexpected operation kind: " << op_kind;
8148 UNREACHABLE();
8149 }
8150 } else {
8151 GenerateDataProcInstruction(kind,
8152 output,
8153 first,
8154 Operand(second,
8155 ShiftFromOpKind(op_kind),
8156 instruction->GetShiftAmount()),
8157 codegen_);
8158 }
Anton Kirilov74234da2017-01-13 14:42:47 +00008159 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008160 DCHECK_EQ(instruction->GetType(), DataType::Type::kInt64);
Anton Kirilov74234da2017-01-13 14:42:47 +00008161
8162 if (HDataProcWithShifterOp::IsExtensionOp(op_kind)) {
8163 const vixl32::Register second = InputRegisterAt(instruction, 1);
8164
8165 DCHECK(!LowRegisterFrom(locations->Out()).Is(second));
8166 GenerateDataProc(kind,
8167 locations->Out(),
8168 locations->InAt(0),
8169 second,
8170 Operand(second, ShiftType::ASR, 31),
8171 codegen_);
8172 } else {
8173 GenerateLongDataProc(instruction, codegen_);
8174 }
8175 }
8176}
8177
Artem Serov02109dd2016-09-23 17:17:54 +01008178// TODO(VIXL): Remove optimizations in the helper when they are implemented in vixl.
8179void InstructionCodeGeneratorARMVIXL::GenerateAndConst(vixl32::Register out,
8180 vixl32::Register first,
8181 uint32_t value) {
8182 // Optimize special cases for individual halfs of `and-long` (`and` is simplified earlier).
8183 if (value == 0xffffffffu) {
8184 if (!out.Is(first)) {
8185 __ Mov(out, first);
8186 }
8187 return;
8188 }
8189 if (value == 0u) {
8190 __ Mov(out, 0);
8191 return;
8192 }
8193 if (GetAssembler()->ShifterOperandCanHold(AND, value)) {
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00008194 __ And(out, first, value);
8195 } else if (GetAssembler()->ShifterOperandCanHold(BIC, ~value)) {
8196 __ Bic(out, first, ~value);
Artem Serov02109dd2016-09-23 17:17:54 +01008197 } else {
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00008198 DCHECK(IsPowerOfTwo(value + 1));
8199 __ Ubfx(out, first, 0, WhichPowerOf2(value + 1));
Artem Serov02109dd2016-09-23 17:17:54 +01008200 }
8201}
8202
8203// TODO(VIXL): Remove optimizations in the helper when they are implemented in vixl.
8204void InstructionCodeGeneratorARMVIXL::GenerateOrrConst(vixl32::Register out,
8205 vixl32::Register first,
8206 uint32_t value) {
8207 // Optimize special cases for individual halfs of `or-long` (`or` is simplified earlier).
8208 if (value == 0u) {
8209 if (!out.Is(first)) {
8210 __ Mov(out, first);
8211 }
8212 return;
8213 }
8214 if (value == 0xffffffffu) {
8215 __ Mvn(out, 0);
8216 return;
8217 }
8218 if (GetAssembler()->ShifterOperandCanHold(ORR, value)) {
8219 __ Orr(out, first, value);
8220 } else {
8221 DCHECK(GetAssembler()->ShifterOperandCanHold(ORN, ~value));
8222 __ Orn(out, first, ~value);
8223 }
8224}
8225
8226// TODO(VIXL): Remove optimizations in the helper when they are implemented in vixl.
8227void InstructionCodeGeneratorARMVIXL::GenerateEorConst(vixl32::Register out,
8228 vixl32::Register first,
8229 uint32_t value) {
8230 // Optimize special case for individual halfs of `xor-long` (`xor` is simplified earlier).
8231 if (value == 0u) {
8232 if (!out.Is(first)) {
8233 __ Mov(out, first);
8234 }
8235 return;
8236 }
8237 __ Eor(out, first, value);
8238}
8239
Anton Kirilovdda43962016-11-21 19:55:20 +00008240void InstructionCodeGeneratorARMVIXL::GenerateAddLongConst(Location out,
8241 Location first,
8242 uint64_t value) {
8243 vixl32::Register out_low = LowRegisterFrom(out);
8244 vixl32::Register out_high = HighRegisterFrom(out);
8245 vixl32::Register first_low = LowRegisterFrom(first);
8246 vixl32::Register first_high = HighRegisterFrom(first);
8247 uint32_t value_low = Low32Bits(value);
8248 uint32_t value_high = High32Bits(value);
8249 if (value_low == 0u) {
8250 if (!out_low.Is(first_low)) {
8251 __ Mov(out_low, first_low);
8252 }
8253 __ Add(out_high, first_high, value_high);
8254 return;
8255 }
8256 __ Adds(out_low, first_low, value_low);
Vladimir Markof0a6a1d2018-01-08 14:23:56 +00008257 if (GetAssembler()->ShifterOperandCanHold(ADC, value_high)) {
Anton Kirilovdda43962016-11-21 19:55:20 +00008258 __ Adc(out_high, first_high, value_high);
Anton Kirilovdda43962016-11-21 19:55:20 +00008259 } else {
Vladimir Markof0a6a1d2018-01-08 14:23:56 +00008260 DCHECK(GetAssembler()->ShifterOperandCanHold(SBC, ~value_high));
8261 __ Sbc(out_high, first_high, ~value_high);
Anton Kirilovdda43962016-11-21 19:55:20 +00008262 }
8263}
8264
Artem Serov02109dd2016-09-23 17:17:54 +01008265void InstructionCodeGeneratorARMVIXL::HandleBitwiseOperation(HBinaryOperation* instruction) {
8266 LocationSummary* locations = instruction->GetLocations();
8267 Location first = locations->InAt(0);
8268 Location second = locations->InAt(1);
8269 Location out = locations->Out();
8270
8271 if (second.IsConstant()) {
8272 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
8273 uint32_t value_low = Low32Bits(value);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008274 if (instruction->GetResultType() == DataType::Type::kInt32) {
Artem Serov02109dd2016-09-23 17:17:54 +01008275 vixl32::Register first_reg = InputRegisterAt(instruction, 0);
8276 vixl32::Register out_reg = OutputRegister(instruction);
8277 if (instruction->IsAnd()) {
8278 GenerateAndConst(out_reg, first_reg, value_low);
8279 } else if (instruction->IsOr()) {
8280 GenerateOrrConst(out_reg, first_reg, value_low);
8281 } else {
8282 DCHECK(instruction->IsXor());
8283 GenerateEorConst(out_reg, first_reg, value_low);
8284 }
8285 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008286 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
Artem Serov02109dd2016-09-23 17:17:54 +01008287 uint32_t value_high = High32Bits(value);
8288 vixl32::Register first_low = LowRegisterFrom(first);
8289 vixl32::Register first_high = HighRegisterFrom(first);
8290 vixl32::Register out_low = LowRegisterFrom(out);
8291 vixl32::Register out_high = HighRegisterFrom(out);
8292 if (instruction->IsAnd()) {
8293 GenerateAndConst(out_low, first_low, value_low);
8294 GenerateAndConst(out_high, first_high, value_high);
8295 } else if (instruction->IsOr()) {
8296 GenerateOrrConst(out_low, first_low, value_low);
8297 GenerateOrrConst(out_high, first_high, value_high);
8298 } else {
8299 DCHECK(instruction->IsXor());
8300 GenerateEorConst(out_low, first_low, value_low);
8301 GenerateEorConst(out_high, first_high, value_high);
8302 }
8303 }
8304 return;
8305 }
8306
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008307 if (instruction->GetResultType() == DataType::Type::kInt32) {
Artem Serov02109dd2016-09-23 17:17:54 +01008308 vixl32::Register first_reg = InputRegisterAt(instruction, 0);
8309 vixl32::Register second_reg = InputRegisterAt(instruction, 1);
8310 vixl32::Register out_reg = OutputRegister(instruction);
8311 if (instruction->IsAnd()) {
8312 __ And(out_reg, first_reg, second_reg);
8313 } else if (instruction->IsOr()) {
8314 __ Orr(out_reg, first_reg, second_reg);
8315 } else {
8316 DCHECK(instruction->IsXor());
8317 __ Eor(out_reg, first_reg, second_reg);
8318 }
8319 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008320 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
Artem Serov02109dd2016-09-23 17:17:54 +01008321 vixl32::Register first_low = LowRegisterFrom(first);
8322 vixl32::Register first_high = HighRegisterFrom(first);
8323 vixl32::Register second_low = LowRegisterFrom(second);
8324 vixl32::Register second_high = HighRegisterFrom(second);
8325 vixl32::Register out_low = LowRegisterFrom(out);
8326 vixl32::Register out_high = HighRegisterFrom(out);
8327 if (instruction->IsAnd()) {
8328 __ And(out_low, first_low, second_low);
8329 __ And(out_high, first_high, second_high);
8330 } else if (instruction->IsOr()) {
8331 __ Orr(out_low, first_low, second_low);
8332 __ Orr(out_high, first_high, second_high);
8333 } else {
8334 DCHECK(instruction->IsXor());
8335 __ Eor(out_low, first_low, second_low);
8336 __ Eor(out_high, first_high, second_high);
8337 }
8338 }
8339}
8340
Artem Serovcfbe9132016-10-14 15:58:56 +01008341void InstructionCodeGeneratorARMVIXL::GenerateReferenceLoadOneRegister(
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008342 HInstruction* instruction,
Artem Serovcfbe9132016-10-14 15:58:56 +01008343 Location out,
8344 uint32_t offset,
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008345 Location maybe_temp,
8346 ReadBarrierOption read_barrier_option) {
Artem Serovcfbe9132016-10-14 15:58:56 +01008347 vixl32::Register out_reg = RegisterFrom(out);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008348 if (read_barrier_option == kWithReadBarrier) {
8349 CHECK(kEmitCompilerReadBarrier);
8350 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
8351 if (kUseBakerReadBarrier) {
8352 // Load with fast path based Baker's read barrier.
8353 // /* HeapReference<Object> */ out = *(out + offset)
8354 codegen_->GenerateFieldLoadWithBakerReadBarrier(
8355 instruction, out, out_reg, offset, maybe_temp, /* needs_null_check */ false);
8356 } else {
8357 // Load with slow path based read barrier.
8358 // Save the value of `out` into `maybe_temp` before overwriting it
8359 // in the following move operation, as we will need it for the
8360 // read barrier below.
8361 __ Mov(RegisterFrom(maybe_temp), out_reg);
8362 // /* HeapReference<Object> */ out = *(out + offset)
8363 GetAssembler()->LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
8364 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
8365 }
Artem Serovcfbe9132016-10-14 15:58:56 +01008366 } else {
8367 // Plain load with no read barrier.
8368 // /* HeapReference<Object> */ out = *(out + offset)
8369 GetAssembler()->LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
8370 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
8371 }
8372}
8373
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008374void InstructionCodeGeneratorARMVIXL::GenerateReferenceLoadTwoRegisters(
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008375 HInstruction* instruction,
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008376 Location out,
8377 Location obj,
8378 uint32_t offset,
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008379 Location maybe_temp,
8380 ReadBarrierOption read_barrier_option) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008381 vixl32::Register out_reg = RegisterFrom(out);
8382 vixl32::Register obj_reg = RegisterFrom(obj);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008383 if (read_barrier_option == kWithReadBarrier) {
8384 CHECK(kEmitCompilerReadBarrier);
8385 if (kUseBakerReadBarrier) {
8386 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
8387 // Load with fast path based Baker's read barrier.
8388 // /* HeapReference<Object> */ out = *(obj + offset)
8389 codegen_->GenerateFieldLoadWithBakerReadBarrier(
8390 instruction, out, obj_reg, offset, maybe_temp, /* needs_null_check */ false);
8391 } else {
8392 // Load with slow path based read barrier.
8393 // /* HeapReference<Object> */ out = *(obj + offset)
8394 GetAssembler()->LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
8395 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
8396 }
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008397 } else {
8398 // Plain load with no read barrier.
8399 // /* HeapReference<Object> */ out = *(obj + offset)
8400 GetAssembler()->LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
8401 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
8402 }
8403}
8404
Scott Wakelinga7812ae2016-10-17 10:03:36 +01008405void InstructionCodeGeneratorARMVIXL::GenerateGcRootFieldLoad(
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008406 HInstruction* instruction,
Scott Wakelinga7812ae2016-10-17 10:03:36 +01008407 Location root,
8408 vixl32::Register obj,
8409 uint32_t offset,
Artem Serovd4cc5b22016-11-04 11:19:09 +00008410 ReadBarrierOption read_barrier_option) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01008411 vixl32::Register root_reg = RegisterFrom(root);
Artem Serovd4cc5b22016-11-04 11:19:09 +00008412 if (read_barrier_option == kWithReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008413 DCHECK(kEmitCompilerReadBarrier);
8414 if (kUseBakerReadBarrier) {
8415 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
Roland Levillainba650a42017-03-06 13:52:32 +00008416 // Baker's read barrier are used.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008417 if (kBakerReadBarrierLinkTimeThunksEnableForGcRoots &&
8418 !Runtime::Current()->UseJitCompilation()) {
Roland Levillain6d729a72017-06-30 18:34:01 +01008419 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in
8420 // the Marking Register) to decide whether we need to enter
8421 // the slow path to mark the GC root.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008422 //
8423 // We use link-time generated thunks for the slow path. That thunk
8424 // checks the reference and jumps to the entrypoint if needed.
8425 //
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008426 // lr = &return_address;
8427 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
Roland Levillain6d729a72017-06-30 18:34:01 +01008428 // if (mr) { // Thread::Current()->GetIsGcMarking()
8429 // goto gc_root_thunk<root_reg>(lr)
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008430 // }
8431 // return_address:
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008432
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008433 UseScratchRegisterScope temps(GetVIXLAssembler());
8434 ExcludeIPAndBakerCcEntrypointRegister(&temps, instruction);
Vladimir Marko88abba22017-05-03 17:09:25 +01008435 bool narrow = CanEmitNarrowLdr(root_reg, obj, offset);
8436 uint32_t custom_data = linker::Thumb2RelativePatcher::EncodeBakerReadBarrierGcRootData(
8437 root_reg.GetCode(), narrow);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008438 vixl32::Label* bne_label = codegen_->NewBakerReadBarrierPatch(custom_data);
Roland Levillainba650a42017-03-06 13:52:32 +00008439
Roland Levillain6d729a72017-06-30 18:34:01 +01008440 vixl::EmissionCheckScope guard(GetVIXLAssembler(), 4 * vixl32::kMaxInstructionSizeInBytes);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008441 vixl32::Label return_address;
8442 EmitAdrCode adr(GetVIXLAssembler(), lr, &return_address);
Roland Levillain6d729a72017-06-30 18:34:01 +01008443 __ cmp(mr, Operand(0));
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008444 // Currently the offset is always within range. If that changes,
8445 // we shall have to split the load the same way as for fields.
8446 DCHECK_LT(offset, kReferenceLoadMinFarOffset);
Vladimir Marko88abba22017-05-03 17:09:25 +01008447 ptrdiff_t old_offset = GetVIXLAssembler()->GetBuffer()->GetCursorOffset();
8448 __ ldr(EncodingSize(narrow ? Narrow : Wide), root_reg, MemOperand(obj, offset));
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008449 EmitPlaceholderBne(codegen_, bne_label);
8450 __ Bind(&return_address);
Vladimir Marko88abba22017-05-03 17:09:25 +01008451 DCHECK_EQ(old_offset - GetVIXLAssembler()->GetBuffer()->GetCursorOffset(),
8452 narrow ? BAKER_MARK_INTROSPECTION_GC_ROOT_LDR_NARROW_OFFSET
8453 : BAKER_MARK_INTROSPECTION_GC_ROOT_LDR_WIDE_OFFSET);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008454 } else {
Roland Levillain6d729a72017-06-30 18:34:01 +01008455 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in
8456 // the Marking Register) to decide whether we need to enter
8457 // the slow path to mark the GC root.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008458 //
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008459 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
Roland Levillain6d729a72017-06-30 18:34:01 +01008460 // if (mr) { // Thread::Current()->GetIsGcMarking()
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008461 // // Slow path.
Roland Levillain6d729a72017-06-30 18:34:01 +01008462 // entrypoint = Thread::Current()->pReadBarrierMarkReg ## root.reg()
8463 // root = entrypoint(root); // root = ReadBarrier::Mark(root); // Entry point call.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008464 // }
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008465
Roland Levillain6d729a72017-06-30 18:34:01 +01008466 // Slow path marking the GC root `root`. The entrypoint will
8467 // be loaded by the slow path code.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008468 SlowPathCodeARMVIXL* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01008469 new (codegen_->GetScopedAllocator()) ReadBarrierMarkSlowPathARMVIXL(instruction, root);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008470 codegen_->AddSlowPath(slow_path);
8471
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008472 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
8473 GetAssembler()->LoadFromOffset(kLoadWord, root_reg, obj, offset);
8474 static_assert(
8475 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
8476 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
8477 "have different sizes.");
8478 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
8479 "art::mirror::CompressedReference<mirror::Object> and int32_t "
8480 "have different sizes.");
8481
Roland Levillain6d729a72017-06-30 18:34:01 +01008482 __ CompareAndBranchIfNonZero(mr, slow_path->GetEntryLabel());
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008483 __ Bind(slow_path->GetExitLabel());
8484 }
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008485 } else {
8486 // GC root loaded through a slow path for read barriers other
8487 // than Baker's.
8488 // /* GcRoot<mirror::Object>* */ root = obj + offset
8489 __ Add(root_reg, obj, offset);
8490 // /* mirror::Object* */ root = root->Read()
8491 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
8492 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01008493 } else {
8494 // Plain GC root load with no read barrier.
8495 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
8496 GetAssembler()->LoadFromOffset(kLoadWord, root_reg, obj, offset);
8497 // Note that GC roots are not affected by heap poisoning, thus we
8498 // do not have to unpoison `root_reg` here.
8499 }
Roland Levillain5daa4952017-07-03 17:23:56 +01008500 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 18);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01008501}
8502
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008503void CodeGeneratorARMVIXL::MaybeAddBakerCcEntrypointTempForFields(LocationSummary* locations) {
8504 DCHECK(kEmitCompilerReadBarrier);
8505 DCHECK(kUseBakerReadBarrier);
8506 if (kBakerReadBarrierLinkTimeThunksEnableForFields) {
8507 if (!Runtime::Current()->UseJitCompilation()) {
8508 locations->AddTemp(Location::RegisterLocation(kBakerCcEntrypointRegister.GetCode()));
8509 }
8510 }
8511}
8512
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008513void CodeGeneratorARMVIXL::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
8514 Location ref,
8515 vixl32::Register obj,
8516 uint32_t offset,
8517 Location temp,
8518 bool needs_null_check) {
8519 DCHECK(kEmitCompilerReadBarrier);
8520 DCHECK(kUseBakerReadBarrier);
8521
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008522 if (kBakerReadBarrierLinkTimeThunksEnableForFields &&
8523 !Runtime::Current()->UseJitCompilation()) {
Roland Levillain6d729a72017-06-30 18:34:01 +01008524 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in the
8525 // Marking Register) to decide whether we need to enter the slow
8526 // path to mark the reference. Then, in the slow path, check the
8527 // gray bit in the lock word of the reference's holder (`obj`) to
8528 // decide whether to mark `ref` or not.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008529 //
8530 // We use link-time generated thunks for the slow path. That thunk checks
8531 // the holder and jumps to the entrypoint if needed. If the holder is not
8532 // gray, it creates a fake dependency and returns to the LDR instruction.
8533 //
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008534 // lr = &gray_return_address;
Roland Levillain6d729a72017-06-30 18:34:01 +01008535 // if (mr) { // Thread::Current()->GetIsGcMarking()
8536 // goto field_thunk<holder_reg, base_reg>(lr)
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008537 // }
8538 // not_gray_return_address:
8539 // // Original reference load. If the offset is too large to fit
8540 // // into LDR, we use an adjusted base register here.
Vladimir Marko88abba22017-05-03 17:09:25 +01008541 // HeapReference<mirror::Object> reference = *(obj+offset);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008542 // gray_return_address:
8543
8544 DCHECK_ALIGNED(offset, sizeof(mirror::HeapReference<mirror::Object>));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008545 vixl32::Register ref_reg = RegisterFrom(ref, DataType::Type::kReference);
Vladimir Marko88abba22017-05-03 17:09:25 +01008546 bool narrow = CanEmitNarrowLdr(ref_reg, obj, offset);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008547 vixl32::Register base = obj;
8548 if (offset >= kReferenceLoadMinFarOffset) {
8549 base = RegisterFrom(temp);
8550 DCHECK(!base.Is(kBakerCcEntrypointRegister));
8551 static_assert(IsPowerOfTwo(kReferenceLoadMinFarOffset), "Expecting a power of 2.");
8552 __ Add(base, obj, Operand(offset & ~(kReferenceLoadMinFarOffset - 1u)));
8553 offset &= (kReferenceLoadMinFarOffset - 1u);
Vladimir Marko88abba22017-05-03 17:09:25 +01008554 // Use narrow LDR only for small offsets. Generating narrow encoding LDR for the large
8555 // offsets with `(offset & (kReferenceLoadMinFarOffset - 1u)) < 32u` would most likely
8556 // increase the overall code size when taking the generated thunks into account.
8557 DCHECK(!narrow);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008558 }
8559 UseScratchRegisterScope temps(GetVIXLAssembler());
8560 ExcludeIPAndBakerCcEntrypointRegister(&temps, instruction);
8561 uint32_t custom_data = linker::Thumb2RelativePatcher::EncodeBakerReadBarrierFieldData(
Vladimir Marko88abba22017-05-03 17:09:25 +01008562 base.GetCode(), obj.GetCode(), narrow);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008563 vixl32::Label* bne_label = NewBakerReadBarrierPatch(custom_data);
8564
Roland Levillain5daa4952017-07-03 17:23:56 +01008565 {
8566 vixl::EmissionCheckScope guard(
8567 GetVIXLAssembler(),
8568 (kPoisonHeapReferences ? 5u : 4u) * vixl32::kMaxInstructionSizeInBytes);
8569 vixl32::Label return_address;
8570 EmitAdrCode adr(GetVIXLAssembler(), lr, &return_address);
8571 __ cmp(mr, Operand(0));
8572 EmitPlaceholderBne(this, bne_label);
8573 ptrdiff_t old_offset = GetVIXLAssembler()->GetBuffer()->GetCursorOffset();
8574 __ ldr(EncodingSize(narrow ? Narrow : Wide), ref_reg, MemOperand(base, offset));
8575 if (needs_null_check) {
8576 MaybeRecordImplicitNullCheck(instruction);
Vladimir Marko88abba22017-05-03 17:09:25 +01008577 }
Roland Levillain5daa4952017-07-03 17:23:56 +01008578 // Note: We need a specific width for the unpoisoning NEG.
8579 if (kPoisonHeapReferences) {
8580 if (narrow) {
8581 // The only 16-bit encoding is T1 which sets flags outside IT block (i.e. RSBS, not RSB).
8582 __ rsbs(EncodingSize(Narrow), ref_reg, ref_reg, Operand(0));
8583 } else {
8584 __ rsb(EncodingSize(Wide), ref_reg, ref_reg, Operand(0));
8585 }
8586 }
8587 __ Bind(&return_address);
8588 DCHECK_EQ(old_offset - GetVIXLAssembler()->GetBuffer()->GetCursorOffset(),
8589 narrow ? BAKER_MARK_INTROSPECTION_FIELD_LDR_NARROW_OFFSET
8590 : BAKER_MARK_INTROSPECTION_FIELD_LDR_WIDE_OFFSET);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008591 }
Roland Levillain5daa4952017-07-03 17:23:56 +01008592 MaybeGenerateMarkingRegisterCheck(/* code */ 19, /* temp_loc */ LocationFrom(ip));
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008593 return;
8594 }
8595
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008596 // /* HeapReference<Object> */ ref = *(obj + offset)
8597 Location no_index = Location::NoLocation();
8598 ScaleFactor no_scale_factor = TIMES_1;
8599 GenerateReferenceLoadWithBakerReadBarrier(
8600 instruction, ref, obj, offset, no_index, no_scale_factor, temp, needs_null_check);
Roland Levillain6070e882016-11-03 17:51:58 +00008601}
8602
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008603void CodeGeneratorARMVIXL::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
8604 Location ref,
8605 vixl32::Register obj,
8606 uint32_t data_offset,
8607 Location index,
8608 Location temp,
8609 bool needs_null_check) {
8610 DCHECK(kEmitCompilerReadBarrier);
8611 DCHECK(kUseBakerReadBarrier);
8612
8613 static_assert(
8614 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
8615 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008616 ScaleFactor scale_factor = TIMES_4;
8617
8618 if (kBakerReadBarrierLinkTimeThunksEnableForArrays &&
8619 !Runtime::Current()->UseJitCompilation()) {
Roland Levillain6d729a72017-06-30 18:34:01 +01008620 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in the
8621 // Marking Register) to decide whether we need to enter the slow
8622 // path to mark the reference. Then, in the slow path, check the
8623 // gray bit in the lock word of the reference's holder (`obj`) to
8624 // decide whether to mark `ref` or not.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008625 //
8626 // We use link-time generated thunks for the slow path. That thunk checks
8627 // the holder and jumps to the entrypoint if needed. If the holder is not
8628 // gray, it creates a fake dependency and returns to the LDR instruction.
8629 //
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008630 // lr = &gray_return_address;
Roland Levillain6d729a72017-06-30 18:34:01 +01008631 // if (mr) { // Thread::Current()->GetIsGcMarking()
8632 // goto array_thunk<base_reg>(lr)
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008633 // }
8634 // not_gray_return_address:
8635 // // Original reference load. If the offset is too large to fit
8636 // // into LDR, we use an adjusted base register here.
Vladimir Marko88abba22017-05-03 17:09:25 +01008637 // HeapReference<mirror::Object> reference = data[index];
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008638 // gray_return_address:
8639
8640 DCHECK(index.IsValid());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008641 vixl32::Register index_reg = RegisterFrom(index, DataType::Type::kInt32);
8642 vixl32::Register ref_reg = RegisterFrom(ref, DataType::Type::kReference);
8643 vixl32::Register data_reg = RegisterFrom(temp, DataType::Type::kInt32); // Raw pointer.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008644 DCHECK(!data_reg.Is(kBakerCcEntrypointRegister));
8645
8646 UseScratchRegisterScope temps(GetVIXLAssembler());
8647 ExcludeIPAndBakerCcEntrypointRegister(&temps, instruction);
8648 uint32_t custom_data =
8649 linker::Thumb2RelativePatcher::EncodeBakerReadBarrierArrayData(data_reg.GetCode());
8650 vixl32::Label* bne_label = NewBakerReadBarrierPatch(custom_data);
8651
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008652 __ Add(data_reg, obj, Operand(data_offset));
Roland Levillain5daa4952017-07-03 17:23:56 +01008653 {
8654 vixl::EmissionCheckScope guard(
8655 GetVIXLAssembler(),
8656 (kPoisonHeapReferences ? 5u : 4u) * vixl32::kMaxInstructionSizeInBytes);
8657 vixl32::Label return_address;
8658 EmitAdrCode adr(GetVIXLAssembler(), lr, &return_address);
8659 __ cmp(mr, Operand(0));
8660 EmitPlaceholderBne(this, bne_label);
8661 ptrdiff_t old_offset = GetVIXLAssembler()->GetBuffer()->GetCursorOffset();
8662 __ ldr(ref_reg, MemOperand(data_reg, index_reg, vixl32::LSL, scale_factor));
8663 DCHECK(!needs_null_check); // The thunk cannot handle the null check.
8664 // Note: We need a Wide NEG for the unpoisoning.
8665 if (kPoisonHeapReferences) {
8666 __ rsb(EncodingSize(Wide), ref_reg, ref_reg, Operand(0));
8667 }
8668 __ Bind(&return_address);
8669 DCHECK_EQ(old_offset - GetVIXLAssembler()->GetBuffer()->GetCursorOffset(),
8670 BAKER_MARK_INTROSPECTION_ARRAY_LDR_OFFSET);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008671 }
Roland Levillain5daa4952017-07-03 17:23:56 +01008672 MaybeGenerateMarkingRegisterCheck(/* code */ 20, /* temp_loc */ LocationFrom(ip));
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008673 return;
8674 }
8675
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008676 // /* HeapReference<Object> */ ref =
8677 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008678 GenerateReferenceLoadWithBakerReadBarrier(
8679 instruction, ref, obj, data_offset, index, scale_factor, temp, needs_null_check);
Roland Levillain6070e882016-11-03 17:51:58 +00008680}
8681
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008682void CodeGeneratorARMVIXL::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
8683 Location ref,
8684 vixl32::Register obj,
8685 uint32_t offset,
8686 Location index,
8687 ScaleFactor scale_factor,
8688 Location temp,
Roland Levillainff487002017-03-07 16:50:01 +00008689 bool needs_null_check) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008690 DCHECK(kEmitCompilerReadBarrier);
8691 DCHECK(kUseBakerReadBarrier);
8692
Roland Levillain6d729a72017-06-30 18:34:01 +01008693 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in the
8694 // Marking Register) to decide whether we need to enter the slow
8695 // path to mark the reference. Then, in the slow path, check the
8696 // gray bit in the lock word of the reference's holder (`obj`) to
8697 // decide whether to mark `ref` or not.
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008698 //
Roland Levillain6d729a72017-06-30 18:34:01 +01008699 // if (mr) { // Thread::Current()->GetIsGcMarking()
Roland Levillainff487002017-03-07 16:50:01 +00008700 // // Slow path.
8701 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
8702 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
8703 // HeapReference<mirror::Object> ref = *src; // Original reference load.
8704 // bool is_gray = (rb_state == ReadBarrier::GrayState());
8705 // if (is_gray) {
Roland Levillain6d729a72017-06-30 18:34:01 +01008706 // entrypoint = Thread::Current()->pReadBarrierMarkReg ## root.reg()
8707 // ref = entrypoint(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
Roland Levillainff487002017-03-07 16:50:01 +00008708 // }
8709 // } else {
8710 // HeapReference<mirror::Object> ref = *src; // Original reference load.
8711 // }
8712
8713 vixl32::Register temp_reg = RegisterFrom(temp);
8714
8715 // Slow path marking the object `ref` when the GC is marking. The
Roland Levillain6d729a72017-06-30 18:34:01 +01008716 // entrypoint will be loaded by the slow path code.
Roland Levillainff487002017-03-07 16:50:01 +00008717 SlowPathCodeARMVIXL* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01008718 new (GetScopedAllocator()) LoadReferenceWithBakerReadBarrierSlowPathARMVIXL(
Roland Levillain6d729a72017-06-30 18:34:01 +01008719 instruction, ref, obj, offset, index, scale_factor, needs_null_check, temp_reg);
Roland Levillainff487002017-03-07 16:50:01 +00008720 AddSlowPath(slow_path);
8721
Roland Levillain6d729a72017-06-30 18:34:01 +01008722 __ CompareAndBranchIfNonZero(mr, slow_path->GetEntryLabel());
Roland Levillainff487002017-03-07 16:50:01 +00008723 // Fast path: the GC is not marking: just load the reference.
8724 GenerateRawReferenceLoad(instruction, ref, obj, offset, index, scale_factor, needs_null_check);
8725 __ Bind(slow_path->GetExitLabel());
Roland Levillain5daa4952017-07-03 17:23:56 +01008726 MaybeGenerateMarkingRegisterCheck(/* code */ 21);
Roland Levillainff487002017-03-07 16:50:01 +00008727}
8728
8729void CodeGeneratorARMVIXL::UpdateReferenceFieldWithBakerReadBarrier(HInstruction* instruction,
8730 Location ref,
8731 vixl32::Register obj,
8732 Location field_offset,
8733 Location temp,
8734 bool needs_null_check,
8735 vixl32::Register temp2) {
8736 DCHECK(kEmitCompilerReadBarrier);
8737 DCHECK(kUseBakerReadBarrier);
8738
Roland Levillain6d729a72017-06-30 18:34:01 +01008739 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in the
8740 // Marking Register) to decide whether we need to enter the slow
8741 // path to update the reference field within `obj`. Then, in the
8742 // slow path, check the gray bit in the lock word of the reference's
8743 // holder (`obj`) to decide whether to mark `ref` and update the
8744 // field or not.
Roland Levillainff487002017-03-07 16:50:01 +00008745 //
Roland Levillain6d729a72017-06-30 18:34:01 +01008746 // if (mr) { // Thread::Current()->GetIsGcMarking()
Roland Levillainba650a42017-03-06 13:52:32 +00008747 // // Slow path.
Roland Levillain54f869e2017-03-06 13:54:11 +00008748 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
8749 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
Roland Levillainff487002017-03-07 16:50:01 +00008750 // HeapReference<mirror::Object> ref = *(obj + field_offset); // Reference load.
Roland Levillain54f869e2017-03-06 13:54:11 +00008751 // bool is_gray = (rb_state == ReadBarrier::GrayState());
8752 // if (is_gray) {
Roland Levillainff487002017-03-07 16:50:01 +00008753 // old_ref = ref;
Roland Levillain6d729a72017-06-30 18:34:01 +01008754 // entrypoint = Thread::Current()->pReadBarrierMarkReg ## root.reg()
8755 // ref = entrypoint(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
Roland Levillainff487002017-03-07 16:50:01 +00008756 // compareAndSwapObject(obj, field_offset, old_ref, ref);
Roland Levillain54f869e2017-03-06 13:54:11 +00008757 // }
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008758 // }
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008759
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008760 vixl32::Register temp_reg = RegisterFrom(temp);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008761
Roland Levillainff487002017-03-07 16:50:01 +00008762 // Slow path updating the object reference at address `obj + field_offset`
Roland Levillain6d729a72017-06-30 18:34:01 +01008763 // when the GC is marking. The entrypoint will be loaded by the slow path code.
Vladimir Marko174b2e22017-10-12 13:34:49 +01008764 SlowPathCodeARMVIXL* slow_path =
8765 new (GetScopedAllocator()) LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARMVIXL(
Roland Levillainff487002017-03-07 16:50:01 +00008766 instruction,
8767 ref,
8768 obj,
8769 /* offset */ 0u,
8770 /* index */ field_offset,
8771 /* scale_factor */ ScaleFactor::TIMES_1,
8772 needs_null_check,
8773 temp_reg,
Roland Levillain6d729a72017-06-30 18:34:01 +01008774 temp2);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008775 AddSlowPath(slow_path);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008776
Roland Levillain6d729a72017-06-30 18:34:01 +01008777 __ CompareAndBranchIfNonZero(mr, slow_path->GetEntryLabel());
Roland Levillainff487002017-03-07 16:50:01 +00008778 // Fast path: the GC is not marking: nothing to do (the field is
8779 // up-to-date, and we don't need to load the reference).
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008780 __ Bind(slow_path->GetExitLabel());
Roland Levillain5daa4952017-07-03 17:23:56 +01008781 MaybeGenerateMarkingRegisterCheck(/* code */ 22);
Roland Levillain844e6532016-11-03 16:09:47 +00008782}
Scott Wakelingfe885462016-09-22 10:24:38 +01008783
Roland Levillainba650a42017-03-06 13:52:32 +00008784void CodeGeneratorARMVIXL::GenerateRawReferenceLoad(HInstruction* instruction,
8785 Location ref,
8786 vixl::aarch32::Register obj,
8787 uint32_t offset,
8788 Location index,
8789 ScaleFactor scale_factor,
8790 bool needs_null_check) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008791 DataType::Type type = DataType::Type::kReference;
Roland Levillainba650a42017-03-06 13:52:32 +00008792 vixl32::Register ref_reg = RegisterFrom(ref, type);
8793
8794 // If needed, vixl::EmissionCheckScope guards are used to ensure
8795 // that no pools are emitted between the load (macro) instruction
8796 // and MaybeRecordImplicitNullCheck.
8797
Scott Wakelingfe885462016-09-22 10:24:38 +01008798 if (index.IsValid()) {
8799 // Load types involving an "index": ArrayGet,
8800 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
8801 // intrinsics.
Roland Levillainba650a42017-03-06 13:52:32 +00008802 // /* HeapReference<mirror::Object> */ ref = *(obj + offset + (index << scale_factor))
Scott Wakelingfe885462016-09-22 10:24:38 +01008803 if (index.IsConstant()) {
8804 size_t computed_offset =
8805 (Int32ConstantFrom(index) << scale_factor) + offset;
Roland Levillainba650a42017-03-06 13:52:32 +00008806 vixl::EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Scott Wakelingfe885462016-09-22 10:24:38 +01008807 GetAssembler()->LoadFromOffset(kLoadWord, ref_reg, obj, computed_offset);
Roland Levillainba650a42017-03-06 13:52:32 +00008808 if (needs_null_check) {
8809 MaybeRecordImplicitNullCheck(instruction);
8810 }
Scott Wakelingfe885462016-09-22 10:24:38 +01008811 } else {
8812 // Handle the special case of the
8813 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
8814 // intrinsics, which use a register pair as index ("long
8815 // offset"), of which only the low part contains data.
8816 vixl32::Register index_reg = index.IsRegisterPair()
8817 ? LowRegisterFrom(index)
8818 : RegisterFrom(index);
8819 UseScratchRegisterScope temps(GetVIXLAssembler());
Roland Levillainba650a42017-03-06 13:52:32 +00008820 vixl32::Register temp = temps.Acquire();
8821 __ Add(temp, obj, Operand(index_reg, ShiftType::LSL, scale_factor));
8822 {
8823 vixl::EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
8824 GetAssembler()->LoadFromOffset(kLoadWord, ref_reg, temp, offset);
8825 if (needs_null_check) {
8826 MaybeRecordImplicitNullCheck(instruction);
8827 }
8828 }
Scott Wakelingfe885462016-09-22 10:24:38 +01008829 }
8830 } else {
Roland Levillainba650a42017-03-06 13:52:32 +00008831 // /* HeapReference<mirror::Object> */ ref = *(obj + offset)
8832 vixl::EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Scott Wakelingfe885462016-09-22 10:24:38 +01008833 GetAssembler()->LoadFromOffset(kLoadWord, ref_reg, obj, offset);
Roland Levillainba650a42017-03-06 13:52:32 +00008834 if (needs_null_check) {
8835 MaybeRecordImplicitNullCheck(instruction);
8836 }
Scott Wakelingfe885462016-09-22 10:24:38 +01008837 }
8838
Roland Levillain844e6532016-11-03 16:09:47 +00008839 // Object* ref = ref_addr->AsMirrorPtr()
8840 GetAssembler()->MaybeUnpoisonHeapReference(ref_reg);
Roland Levillain844e6532016-11-03 16:09:47 +00008841}
8842
Roland Levillain5daa4952017-07-03 17:23:56 +01008843void CodeGeneratorARMVIXL::MaybeGenerateMarkingRegisterCheck(int code, Location temp_loc) {
8844 // The following condition is a compile-time one, so it does not have a run-time cost.
8845 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier && kIsDebugBuild) {
8846 // The following condition is a run-time one; it is executed after the
8847 // previous compile-time test, to avoid penalizing non-debug builds.
8848 if (GetCompilerOptions().EmitRunTimeChecksInDebugMode()) {
8849 UseScratchRegisterScope temps(GetVIXLAssembler());
8850 vixl32::Register temp = temp_loc.IsValid() ? RegisterFrom(temp_loc) : temps.Acquire();
8851 GetAssembler()->GenerateMarkingRegisterCheck(temp,
8852 kMarkingRegisterCheckBreakCodeBaseCode + code);
8853 }
8854 }
8855}
8856
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008857void CodeGeneratorARMVIXL::GenerateReadBarrierSlow(HInstruction* instruction,
8858 Location out,
8859 Location ref,
8860 Location obj,
8861 uint32_t offset,
8862 Location index) {
8863 DCHECK(kEmitCompilerReadBarrier);
8864
8865 // Insert a slow path based read barrier *after* the reference load.
8866 //
8867 // If heap poisoning is enabled, the unpoisoning of the loaded
8868 // reference will be carried out by the runtime within the slow
8869 // path.
8870 //
8871 // Note that `ref` currently does not get unpoisoned (when heap
8872 // poisoning is enabled), which is alright as the `ref` argument is
8873 // not used by the artReadBarrierSlow entry point.
8874 //
8875 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Vladimir Marko174b2e22017-10-12 13:34:49 +01008876 SlowPathCodeARMVIXL* slow_path = new (GetScopedAllocator())
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008877 ReadBarrierForHeapReferenceSlowPathARMVIXL(instruction, out, ref, obj, offset, index);
8878 AddSlowPath(slow_path);
8879
8880 __ B(slow_path->GetEntryLabel());
8881 __ Bind(slow_path->GetExitLabel());
8882}
8883
8884void CodeGeneratorARMVIXL::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
Artem Serov02d37832016-10-25 15:25:33 +01008885 Location out,
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008886 Location ref,
8887 Location obj,
8888 uint32_t offset,
8889 Location index) {
Artem Serov02d37832016-10-25 15:25:33 +01008890 if (kEmitCompilerReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008891 // Baker's read barriers shall be handled by the fast path
Roland Levillain9983e302017-07-14 14:34:22 +01008892 // (CodeGeneratorARMVIXL::GenerateReferenceLoadWithBakerReadBarrier).
Artem Serov02d37832016-10-25 15:25:33 +01008893 DCHECK(!kUseBakerReadBarrier);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008894 // If heap poisoning is enabled, unpoisoning will be taken care of
8895 // by the runtime within the slow path.
8896 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Artem Serov02d37832016-10-25 15:25:33 +01008897 } else if (kPoisonHeapReferences) {
8898 GetAssembler()->UnpoisonHeapReference(RegisterFrom(out));
8899 }
8900}
8901
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008902void CodeGeneratorARMVIXL::GenerateReadBarrierForRootSlow(HInstruction* instruction,
8903 Location out,
8904 Location root) {
8905 DCHECK(kEmitCompilerReadBarrier);
8906
8907 // Insert a slow path based read barrier *after* the GC root load.
8908 //
8909 // Note that GC roots are not affected by heap poisoning, so we do
8910 // not need to do anything special for this here.
8911 SlowPathCodeARMVIXL* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01008912 new (GetScopedAllocator()) ReadBarrierForRootSlowPathARMVIXL(instruction, out, root);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008913 AddSlowPath(slow_path);
8914
8915 __ B(slow_path->GetEntryLabel());
8916 __ Bind(slow_path->GetExitLabel());
8917}
8918
Artem Serov02d37832016-10-25 15:25:33 +01008919// Check if the desired_dispatch_info is supported. If it is, return it,
8920// otherwise return a fall-back info that should be used instead.
8921HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARMVIXL::GetSupportedInvokeStaticOrDirectDispatch(
Artem Serovd4cc5b22016-11-04 11:19:09 +00008922 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +00008923 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Nicolas Geoffraye807ff72017-01-23 09:03:12 +00008924 return desired_dispatch_info;
Artem Serov02d37832016-10-25 15:25:33 +01008925}
8926
Scott Wakelingfe885462016-09-22 10:24:38 +01008927vixl32::Register CodeGeneratorARMVIXL::GetInvokeStaticOrDirectExtraParameter(
8928 HInvokeStaticOrDirect* invoke, vixl32::Register temp) {
8929 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
8930 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
8931 if (!invoke->GetLocations()->Intrinsified()) {
8932 return RegisterFrom(location);
8933 }
8934 // For intrinsics we allow any location, so it may be on the stack.
8935 if (!location.IsRegister()) {
8936 GetAssembler()->LoadFromOffset(kLoadWord, temp, sp, location.GetStackIndex());
8937 return temp;
8938 }
8939 // For register locations, check if the register was saved. If so, get it from the stack.
8940 // Note: There is a chance that the register was saved but not overwritten, so we could
8941 // save one load. However, since this is just an intrinsic slow path we prefer this
8942 // simple and more robust approach rather that trying to determine if that's the case.
8943 SlowPathCode* slow_path = GetCurrentSlowPath();
Scott Wakelingd5cd4972017-02-03 11:38:35 +00008944 if (slow_path != nullptr && slow_path->IsCoreRegisterSaved(RegisterFrom(location).GetCode())) {
Scott Wakelingfe885462016-09-22 10:24:38 +01008945 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(RegisterFrom(location).GetCode());
8946 GetAssembler()->LoadFromOffset(kLoadWord, temp, sp, stack_offset);
8947 return temp;
8948 }
8949 return RegisterFrom(location);
8950}
8951
Vladimir Markod254f5c2017-06-02 15:18:36 +00008952void CodeGeneratorARMVIXL::GenerateStaticOrDirectCall(
Vladimir Markoe7197bf2017-06-02 17:00:23 +01008953 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00008954 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
Scott Wakelingfe885462016-09-22 10:24:38 +01008955 switch (invoke->GetMethodLoadKind()) {
8956 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
8957 uint32_t offset =
8958 GetThreadOffset<kArmPointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
8959 // temp = thread->string_init_entrypoint
Artem Serovd4cc5b22016-11-04 11:19:09 +00008960 GetAssembler()->LoadFromOffset(kLoadWord, RegisterFrom(temp), tr, offset);
8961 break;
8962 }
8963 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
8964 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
8965 break;
Vladimir Marko65979462017-05-19 17:25:12 +01008966 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative: {
8967 DCHECK(GetCompilerOptions().IsBootImage());
8968 PcRelativePatchInfo* labels = NewPcRelativeMethodPatch(invoke->GetTargetMethod());
8969 vixl32::Register temp_reg = RegisterFrom(temp);
8970 EmitMovwMovtPlaceholder(labels, temp_reg);
8971 break;
8972 }
Artem Serovd4cc5b22016-11-04 11:19:09 +00008973 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
8974 __ Mov(RegisterFrom(temp), Operand::From(invoke->GetMethodAddress()));
8975 break;
Vladimir Marko0eb882b2017-05-15 13:39:18 +01008976 case HInvokeStaticOrDirect::MethodLoadKind::kBssEntry: {
8977 PcRelativePatchInfo* labels = NewMethodBssEntryPatch(
8978 MethodReference(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex()));
8979 vixl32::Register temp_reg = RegisterFrom(temp);
8980 EmitMovwMovtPlaceholder(labels, temp_reg);
8981 GetAssembler()->LoadFromOffset(kLoadWord, temp_reg, temp_reg, /* offset*/ 0);
Scott Wakelingfe885462016-09-22 10:24:38 +01008982 break;
8983 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01008984 case HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall: {
8985 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
8986 return; // No code pointer retrieval; the runtime performs the call directly.
Scott Wakelingfe885462016-09-22 10:24:38 +01008987 }
Scott Wakelingfe885462016-09-22 10:24:38 +01008988 }
8989
Artem Serovd4cc5b22016-11-04 11:19:09 +00008990 switch (invoke->GetCodePtrLocation()) {
8991 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Vladimir Markoe7197bf2017-06-02 17:00:23 +01008992 {
8993 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
8994 ExactAssemblyScope aas(GetVIXLAssembler(),
8995 vixl32::k32BitT32InstructionSizeInBytes,
8996 CodeBufferCheckScope::kMaximumSize);
8997 __ bl(GetFrameEntryLabel());
8998 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
8999 }
Artem Serovd4cc5b22016-11-04 11:19:09 +00009000 break;
Artem Serovd4cc5b22016-11-04 11:19:09 +00009001 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
9002 // LR = callee_method->entry_point_from_quick_compiled_code_
9003 GetAssembler()->LoadFromOffset(
9004 kLoadWord,
9005 lr,
9006 RegisterFrom(callee_method),
9007 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize).Int32Value());
Alexandre Rames374ddf32016-11-04 10:40:49 +00009008 {
Vladimir Markoe7197bf2017-06-02 17:00:23 +01009009 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
Alexandre Rames374ddf32016-11-04 10:40:49 +00009010 // 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 +00009011 ExactAssemblyScope aas(GetVIXLAssembler(),
9012 vixl32::k16BitT32InstructionSizeInBytes,
9013 CodeBufferCheckScope::kExactSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00009014 // LR()
9015 __ blx(lr);
Vladimir Markoe7197bf2017-06-02 17:00:23 +01009016 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Alexandre Rames374ddf32016-11-04 10:40:49 +00009017 }
Artem Serovd4cc5b22016-11-04 11:19:09 +00009018 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01009019 }
9020
Scott Wakelingfe885462016-09-22 10:24:38 +01009021 DCHECK(!IsLeafMethod());
9022}
9023
Vladimir Markoe7197bf2017-06-02 17:00:23 +01009024void CodeGeneratorARMVIXL::GenerateVirtualCall(
9025 HInvokeVirtual* invoke, Location temp_location, SlowPathCode* slow_path) {
Scott Wakelingfe885462016-09-22 10:24:38 +01009026 vixl32::Register temp = RegisterFrom(temp_location);
9027 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
9028 invoke->GetVTableIndex(), kArmPointerSize).Uint32Value();
9029
9030 // Use the calling convention instead of the location of the receiver, as
9031 // intrinsics may have put the receiver in a different register. In the intrinsics
9032 // slow path, the arguments have been moved to the right place, so here we are
9033 // guaranteed that the receiver is the first register of the calling convention.
9034 InvokeDexCallingConventionARMVIXL calling_convention;
9035 vixl32::Register receiver = calling_convention.GetRegisterAt(0);
9036 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Alexandre Rames374ddf32016-11-04 10:40:49 +00009037 {
9038 // Make sure the pc is recorded immediately after the `ldr` instruction.
Artem Serov0fb37192016-12-06 18:13:40 +00009039 ExactAssemblyScope aas(GetVIXLAssembler(),
9040 vixl32::kMaxInstructionSizeInBytes,
9041 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00009042 // /* HeapReference<Class> */ temp = receiver->klass_
9043 __ ldr(temp, MemOperand(receiver, class_offset));
9044 MaybeRecordImplicitNullCheck(invoke);
9045 }
Scott Wakelingfe885462016-09-22 10:24:38 +01009046 // Instead of simply (possibly) unpoisoning `temp` here, we should
9047 // emit a read barrier for the previous class reference load.
9048 // However this is not required in practice, as this is an
9049 // intermediate/temporary reference and because the current
9050 // concurrent copying collector keeps the from-space memory
9051 // intact/accessible until the end of the marking phase (the
9052 // concurrent copying collector may not in the future).
9053 GetAssembler()->MaybeUnpoisonHeapReference(temp);
9054
9055 // temp = temp->GetMethodAt(method_offset);
9056 uint32_t entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(
9057 kArmPointerSize).Int32Value();
9058 GetAssembler()->LoadFromOffset(kLoadWord, temp, temp, method_offset);
9059 // LR = temp->GetEntryPoint();
9060 GetAssembler()->LoadFromOffset(kLoadWord, lr, temp, entry_point);
Vladimir Markoe7197bf2017-06-02 17:00:23 +01009061 {
9062 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
9063 // blx in T32 has only 16bit encoding that's why a stricter check for the scope is used.
9064 ExactAssemblyScope aas(GetVIXLAssembler(),
9065 vixl32::k16BitT32InstructionSizeInBytes,
9066 CodeBufferCheckScope::kExactSize);
9067 // LR();
9068 __ blx(lr);
9069 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
9070 }
Scott Wakelingfe885462016-09-22 10:24:38 +01009071}
9072
Vladimir Marko65979462017-05-19 17:25:12 +01009073CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewPcRelativeMethodPatch(
9074 MethodReference target_method) {
9075 return NewPcRelativePatch(*target_method.dex_file,
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07009076 target_method.index,
Vladimir Marko65979462017-05-19 17:25:12 +01009077 &pc_relative_method_patches_);
Artem Serovd4cc5b22016-11-04 11:19:09 +00009078}
9079
Vladimir Marko0eb882b2017-05-15 13:39:18 +01009080CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewMethodBssEntryPatch(
9081 MethodReference target_method) {
9082 return NewPcRelativePatch(*target_method.dex_file,
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07009083 target_method.index,
Vladimir Marko0eb882b2017-05-15 13:39:18 +01009084 &method_bss_entry_patches_);
9085}
9086
Artem Serovd4cc5b22016-11-04 11:19:09 +00009087CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewPcRelativeTypePatch(
9088 const DexFile& dex_file, dex::TypeIndex type_index) {
9089 return NewPcRelativePatch(dex_file, type_index.index_, &pc_relative_type_patches_);
9090}
9091
Vladimir Marko1998cd02017-01-13 13:02:58 +00009092CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewTypeBssEntryPatch(
9093 const DexFile& dex_file, dex::TypeIndex type_index) {
9094 return NewPcRelativePatch(dex_file, type_index.index_, &type_bss_entry_patches_);
9095}
9096
Vladimir Marko65979462017-05-19 17:25:12 +01009097CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewPcRelativeStringPatch(
9098 const DexFile& dex_file, dex::StringIndex string_index) {
9099 return NewPcRelativePatch(dex_file, string_index.index_, &pc_relative_string_patches_);
9100}
9101
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01009102CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewStringBssEntryPatch(
9103 const DexFile& dex_file, dex::StringIndex string_index) {
9104 return NewPcRelativePatch(dex_file, string_index.index_, &string_bss_entry_patches_);
9105}
9106
Artem Serovd4cc5b22016-11-04 11:19:09 +00009107CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewPcRelativePatch(
9108 const DexFile& dex_file, uint32_t offset_or_index, ArenaDeque<PcRelativePatchInfo>* patches) {
9109 patches->emplace_back(dex_file, offset_or_index);
9110 return &patches->back();
9111}
9112
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01009113vixl::aarch32::Label* CodeGeneratorARMVIXL::NewBakerReadBarrierPatch(uint32_t custom_data) {
9114 baker_read_barrier_patches_.emplace_back(custom_data);
9115 return &baker_read_barrier_patches_.back().label;
9116}
9117
Artem Serovc5fcb442016-12-02 19:19:58 +00009118VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateBootImageAddressLiteral(uint32_t address) {
Richard Uhlerc52f3032017-03-02 13:45:45 +00009119 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), &uint32_literals_);
Artem Serovc5fcb442016-12-02 19:19:58 +00009120}
9121
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00009122VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateJitStringLiteral(
9123 const DexFile& dex_file,
9124 dex::StringIndex string_index,
9125 Handle<mirror::String> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01009126 ReserveJitStringRoot(StringReference(&dex_file, string_index), handle);
Artem Serovc5fcb442016-12-02 19:19:58 +00009127 return jit_string_patches_.GetOrCreate(
9128 StringReference(&dex_file, string_index),
9129 [this]() {
9130 return GetAssembler()->CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u);
9131 });
9132}
9133
9134VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateJitClassLiteral(const DexFile& dex_file,
9135 dex::TypeIndex type_index,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00009136 Handle<mirror::Class> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01009137 ReserveJitClassRoot(TypeReference(&dex_file, type_index), handle);
Artem Serovc5fcb442016-12-02 19:19:58 +00009138 return jit_class_patches_.GetOrCreate(
9139 TypeReference(&dex_file, type_index),
9140 [this]() {
9141 return GetAssembler()->CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u);
9142 });
9143}
9144
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01009145template <linker::LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
Artem Serovd4cc5b22016-11-04 11:19:09 +00009146inline void CodeGeneratorARMVIXL::EmitPcRelativeLinkerPatches(
9147 const ArenaDeque<PcRelativePatchInfo>& infos,
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01009148 ArenaVector<linker::LinkerPatch>* linker_patches) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00009149 for (const PcRelativePatchInfo& info : infos) {
9150 const DexFile& dex_file = info.target_dex_file;
9151 size_t offset_or_index = info.offset_or_index;
9152 DCHECK(info.add_pc_label.IsBound());
9153 uint32_t add_pc_offset = dchecked_integral_cast<uint32_t>(info.add_pc_label.GetLocation());
9154 // Add MOVW patch.
9155 DCHECK(info.movw_label.IsBound());
9156 uint32_t movw_offset = dchecked_integral_cast<uint32_t>(info.movw_label.GetLocation());
9157 linker_patches->push_back(Factory(movw_offset, &dex_file, add_pc_offset, offset_or_index));
9158 // Add MOVT patch.
9159 DCHECK(info.movt_label.IsBound());
9160 uint32_t movt_offset = dchecked_integral_cast<uint32_t>(info.movt_label.GetLocation());
9161 linker_patches->push_back(Factory(movt_offset, &dex_file, add_pc_offset, offset_or_index));
9162 }
9163}
9164
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01009165void CodeGeneratorARMVIXL::EmitLinkerPatches(ArenaVector<linker::LinkerPatch>* linker_patches) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00009166 DCHECK(linker_patches->empty());
9167 size_t size =
Vladimir Marko65979462017-05-19 17:25:12 +01009168 /* MOVW+MOVT for each entry */ 2u * pc_relative_method_patches_.size() +
Vladimir Marko0eb882b2017-05-15 13:39:18 +01009169 /* MOVW+MOVT for each entry */ 2u * method_bss_entry_patches_.size() +
Artem Serovc5fcb442016-12-02 19:19:58 +00009170 /* MOVW+MOVT for each entry */ 2u * pc_relative_type_patches_.size() +
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01009171 /* MOVW+MOVT for each entry */ 2u * type_bss_entry_patches_.size() +
Vladimir Marko65979462017-05-19 17:25:12 +01009172 /* MOVW+MOVT for each entry */ 2u * pc_relative_string_patches_.size() +
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01009173 /* MOVW+MOVT for each entry */ 2u * string_bss_entry_patches_.size() +
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01009174 baker_read_barrier_patches_.size();
Artem Serovd4cc5b22016-11-04 11:19:09 +00009175 linker_patches->reserve(size);
Vladimir Marko65979462017-05-19 17:25:12 +01009176 if (GetCompilerOptions().IsBootImage()) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01009177 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeMethodPatch>(
9178 pc_relative_method_patches_, linker_patches);
9179 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeTypePatch>(
9180 pc_relative_type_patches_, linker_patches);
9181 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeStringPatch>(
9182 pc_relative_string_patches_, linker_patches);
Vladimir Marko65979462017-05-19 17:25:12 +01009183 } else {
9184 DCHECK(pc_relative_method_patches_.empty());
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01009185 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeClassTablePatch>(
9186 pc_relative_type_patches_, linker_patches);
9187 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringInternTablePatch>(
9188 pc_relative_string_patches_, linker_patches);
Artem Serovd4cc5b22016-11-04 11:19:09 +00009189 }
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01009190 EmitPcRelativeLinkerPatches<linker::LinkerPatch::MethodBssEntryPatch>(
9191 method_bss_entry_patches_, linker_patches);
9192 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeBssEntryPatch>(
9193 type_bss_entry_patches_, linker_patches);
9194 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringBssEntryPatch>(
9195 string_bss_entry_patches_, linker_patches);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01009196 for (const BakerReadBarrierPatchInfo& info : baker_read_barrier_patches_) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01009197 linker_patches->push_back(linker::LinkerPatch::BakerReadBarrierBranchPatch(
9198 info.label.GetLocation(), info.custom_data));
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01009199 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00009200 DCHECK_EQ(size, linker_patches->size());
Artem Serovc5fcb442016-12-02 19:19:58 +00009201}
9202
9203VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateUint32Literal(
9204 uint32_t value,
9205 Uint32ToLiteralMap* map) {
9206 return map->GetOrCreate(
9207 value,
9208 [this, value]() {
9209 return GetAssembler()->CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ value);
9210 });
9211}
9212
Artem Serov2bbc9532016-10-21 11:51:50 +01009213void LocationsBuilderARMVIXL::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
9214 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01009215 new (GetGraph()->GetAllocator()) LocationSummary(instr, LocationSummary::kNoCall);
Artem Serov2bbc9532016-10-21 11:51:50 +01009216 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
9217 Location::RequiresRegister());
9218 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
9219 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
9220 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
9221}
9222
9223void InstructionCodeGeneratorARMVIXL::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
9224 vixl32::Register res = OutputRegister(instr);
9225 vixl32::Register accumulator =
9226 InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
9227 vixl32::Register mul_left =
9228 InputRegisterAt(instr, HMultiplyAccumulate::kInputMulLeftIndex);
9229 vixl32::Register mul_right =
9230 InputRegisterAt(instr, HMultiplyAccumulate::kInputMulRightIndex);
9231
9232 if (instr->GetOpKind() == HInstruction::kAdd) {
9233 __ Mla(res, mul_left, mul_right, accumulator);
9234 } else {
9235 __ Mls(res, mul_left, mul_right, accumulator);
9236 }
9237}
9238
Artem Serov551b28f2016-10-18 19:11:30 +01009239void LocationsBuilderARMVIXL::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
9240 // Nothing to do, this should be removed during prepare for register allocator.
9241 LOG(FATAL) << "Unreachable";
9242}
9243
9244void InstructionCodeGeneratorARMVIXL::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
9245 // Nothing to do, this should be removed during prepare for register allocator.
9246 LOG(FATAL) << "Unreachable";
9247}
9248
9249// Simple implementation of packed switch - generate cascaded compare/jumps.
9250void LocationsBuilderARMVIXL::VisitPackedSwitch(HPackedSwitch* switch_instr) {
9251 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01009252 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Artem Serov551b28f2016-10-18 19:11:30 +01009253 locations->SetInAt(0, Location::RequiresRegister());
9254 if (switch_instr->GetNumEntries() > kPackedSwitchCompareJumpThreshold &&
9255 codegen_->GetAssembler()->GetVIXLAssembler()->IsUsingT32()) {
9256 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the table base.
9257 if (switch_instr->GetStartValue() != 0) {
9258 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the bias.
9259 }
9260 }
9261}
9262
9263// TODO(VIXL): Investigate and reach the parity with old arm codegen.
9264void InstructionCodeGeneratorARMVIXL::VisitPackedSwitch(HPackedSwitch* switch_instr) {
9265 int32_t lower_bound = switch_instr->GetStartValue();
9266 uint32_t num_entries = switch_instr->GetNumEntries();
9267 LocationSummary* locations = switch_instr->GetLocations();
9268 vixl32::Register value_reg = InputRegisterAt(switch_instr, 0);
9269 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
9270
9271 if (num_entries <= kPackedSwitchCompareJumpThreshold ||
9272 !codegen_->GetAssembler()->GetVIXLAssembler()->IsUsingT32()) {
9273 // Create a series of compare/jumps.
Anton Kirilovedb2ac32016-11-30 15:14:10 +00009274 UseScratchRegisterScope temps(GetVIXLAssembler());
Artem Serov551b28f2016-10-18 19:11:30 +01009275 vixl32::Register temp_reg = temps.Acquire();
9276 // Note: It is fine for the below AddConstantSetFlags() using IP register to temporarily store
9277 // the immediate, because IP is used as the destination register. For the other
9278 // AddConstantSetFlags() and GenerateCompareWithImmediate(), the immediate values are constant,
9279 // and they can be encoded in the instruction without making use of IP register.
9280 __ Adds(temp_reg, value_reg, -lower_bound);
9281
9282 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
9283 // Jump to successors[0] if value == lower_bound.
9284 __ B(eq, codegen_->GetLabelOf(successors[0]));
9285 int32_t last_index = 0;
9286 for (; num_entries - last_index > 2; last_index += 2) {
9287 __ Adds(temp_reg, temp_reg, -2);
9288 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
9289 __ B(lo, codegen_->GetLabelOf(successors[last_index + 1]));
9290 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
9291 __ B(eq, codegen_->GetLabelOf(successors[last_index + 2]));
9292 }
9293 if (num_entries - last_index == 2) {
9294 // The last missing case_value.
9295 __ Cmp(temp_reg, 1);
9296 __ B(eq, codegen_->GetLabelOf(successors[last_index + 1]));
9297 }
9298
9299 // And the default for any other value.
9300 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
9301 __ B(codegen_->GetLabelOf(default_block));
9302 }
9303 } else {
9304 // Create a table lookup.
9305 vixl32::Register table_base = RegisterFrom(locations->GetTemp(0));
9306
9307 JumpTableARMVIXL* jump_table = codegen_->CreateJumpTable(switch_instr);
9308
9309 // Remove the bias.
9310 vixl32::Register key_reg;
9311 if (lower_bound != 0) {
9312 key_reg = RegisterFrom(locations->GetTemp(1));
9313 __ Sub(key_reg, value_reg, lower_bound);
9314 } else {
9315 key_reg = value_reg;
9316 }
9317
9318 // Check whether the value is in the table, jump to default block if not.
9319 __ Cmp(key_reg, num_entries - 1);
9320 __ B(hi, codegen_->GetLabelOf(default_block));
9321
Anton Kirilovedb2ac32016-11-30 15:14:10 +00009322 UseScratchRegisterScope temps(GetVIXLAssembler());
Artem Serov551b28f2016-10-18 19:11:30 +01009323 vixl32::Register jump_offset = temps.Acquire();
9324
9325 // Load jump offset from the table.
Scott Wakeling86e9d262017-01-18 15:59:24 +00009326 {
9327 const size_t jump_size = switch_instr->GetNumEntries() * sizeof(int32_t);
9328 ExactAssemblyScope aas(GetVIXLAssembler(),
9329 (vixl32::kMaxInstructionSizeInBytes * 4) + jump_size,
9330 CodeBufferCheckScope::kMaximumSize);
9331 __ adr(table_base, jump_table->GetTableStartLabel());
9332 __ ldr(jump_offset, MemOperand(table_base, key_reg, vixl32::LSL, 2));
Artem Serov551b28f2016-10-18 19:11:30 +01009333
Scott Wakeling86e9d262017-01-18 15:59:24 +00009334 // Jump to target block by branching to table_base(pc related) + offset.
9335 vixl32::Register target_address = table_base;
9336 __ add(target_address, table_base, jump_offset);
9337 __ bx(target_address);
Artem Serov09a940d2016-11-11 16:15:11 +00009338
Scott Wakeling86e9d262017-01-18 15:59:24 +00009339 jump_table->EmitTable(codegen_);
9340 }
Artem Serov551b28f2016-10-18 19:11:30 +01009341 }
9342}
9343
Artem Serov02d37832016-10-25 15:25:33 +01009344// Copy the result of a call into the given target.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009345void CodeGeneratorARMVIXL::MoveFromReturnRegister(Location trg, DataType::Type type) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01009346 if (!trg.IsValid()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009347 DCHECK_EQ(type, DataType::Type::kVoid);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01009348 return;
9349 }
9350
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009351 DCHECK_NE(type, DataType::Type::kVoid);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01009352
Artem Serovd4cc5b22016-11-04 11:19:09 +00009353 Location return_loc = InvokeDexCallingConventionVisitorARMVIXL().GetReturnLocation(type);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01009354 if (return_loc.Equals(trg)) {
9355 return;
9356 }
9357
9358 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
9359 // with the last branch.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009360 if (type == DataType::Type::kInt64) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01009361 TODO_VIXL32(FATAL);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009362 } else if (type == DataType::Type::kFloat64) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01009363 TODO_VIXL32(FATAL);
9364 } else {
9365 // Let the parallel move resolver take care of all of this.
Vladimir Markoca6fff82017-10-03 14:49:14 +01009366 HParallelMove parallel_move(GetGraph()->GetAllocator());
Anton Kirilove28d9ae2016-10-25 18:17:23 +01009367 parallel_move.AddMove(return_loc, trg, type, nullptr);
9368 GetMoveResolver()->EmitNativeCode(&parallel_move);
9369 }
Scott Wakelingfe885462016-09-22 10:24:38 +01009370}
9371
xueliang.zhong8d2c4592016-11-23 17:05:25 +00009372void LocationsBuilderARMVIXL::VisitClassTableGet(HClassTableGet* instruction) {
9373 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01009374 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
xueliang.zhong8d2c4592016-11-23 17:05:25 +00009375 locations->SetInAt(0, Location::RequiresRegister());
9376 locations->SetOut(Location::RequiresRegister());
Artem Serov551b28f2016-10-18 19:11:30 +01009377}
9378
xueliang.zhong8d2c4592016-11-23 17:05:25 +00009379void InstructionCodeGeneratorARMVIXL::VisitClassTableGet(HClassTableGet* instruction) {
9380 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
9381 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
9382 instruction->GetIndex(), kArmPointerSize).SizeValue();
9383 GetAssembler()->LoadFromOffset(kLoadWord,
9384 OutputRegister(instruction),
9385 InputRegisterAt(instruction, 0),
9386 method_offset);
9387 } else {
9388 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
9389 instruction->GetIndex(), kArmPointerSize));
9390 GetAssembler()->LoadFromOffset(kLoadWord,
9391 OutputRegister(instruction),
9392 InputRegisterAt(instruction, 0),
9393 mirror::Class::ImtPtrOffset(kArmPointerSize).Uint32Value());
9394 GetAssembler()->LoadFromOffset(kLoadWord,
9395 OutputRegister(instruction),
9396 OutputRegister(instruction),
9397 method_offset);
9398 }
Artem Serov551b28f2016-10-18 19:11:30 +01009399}
9400
Artem Serovc5fcb442016-12-02 19:19:58 +00009401static void PatchJitRootUse(uint8_t* code,
9402 const uint8_t* roots_data,
9403 VIXLUInt32Literal* literal,
9404 uint64_t index_in_table) {
9405 DCHECK(literal->IsBound());
9406 uint32_t literal_offset = literal->GetLocation();
9407 uintptr_t address =
9408 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
9409 uint8_t* data = code + literal_offset;
9410 reinterpret_cast<uint32_t*>(data)[0] = dchecked_integral_cast<uint32_t>(address);
9411}
9412
9413void CodeGeneratorARMVIXL::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
9414 for (const auto& entry : jit_string_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01009415 const StringReference& string_reference = entry.first;
9416 VIXLUInt32Literal* table_entry_literal = entry.second;
Vladimir Marko174b2e22017-10-12 13:34:49 +01009417 uint64_t index_in_table = GetJitStringRootIndex(string_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01009418 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Artem Serovc5fcb442016-12-02 19:19:58 +00009419 }
9420 for (const auto& entry : jit_class_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01009421 const TypeReference& type_reference = entry.first;
9422 VIXLUInt32Literal* table_entry_literal = entry.second;
Vladimir Marko174b2e22017-10-12 13:34:49 +01009423 uint64_t index_in_table = GetJitClassRootIndex(type_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01009424 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Artem Serovc5fcb442016-12-02 19:19:58 +00009425 }
9426}
9427
Artem Serovd4cc5b22016-11-04 11:19:09 +00009428void CodeGeneratorARMVIXL::EmitMovwMovtPlaceholder(
9429 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels,
9430 vixl32::Register out) {
Artem Serov0fb37192016-12-06 18:13:40 +00009431 ExactAssemblyScope aas(GetVIXLAssembler(),
9432 3 * vixl32::kMaxInstructionSizeInBytes,
9433 CodeBufferCheckScope::kMaximumSize);
Artem Serovd4cc5b22016-11-04 11:19:09 +00009434 // TODO(VIXL): Think about using mov instead of movw.
9435 __ bind(&labels->movw_label);
9436 __ movw(out, /* placeholder */ 0u);
9437 __ bind(&labels->movt_label);
9438 __ movt(out, /* placeholder */ 0u);
9439 __ bind(&labels->add_pc_label);
9440 __ add(out, out, pc);
9441}
9442
Scott Wakelingfe885462016-09-22 10:24:38 +01009443#undef __
9444#undef QUICK_ENTRY_POINT
9445#undef TODO_VIXL32
9446
9447} // namespace arm
9448} // namespace art