blob: 577fe00dcd10900280077d7c435cde439e365114 [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) {
Nicolas Geoffray1a4f3ca2018-01-25 14:07:15 +00002493 UseScratchRegisterScope temps(GetVIXLAssembler());
2494 vixl32::Register temp = temps.Acquire();
Vladimir Marko33bff252017-11-01 14:35:42 +00002495 __ Sub(temp, sp, Operand::From(GetStackOverflowReservedBytes(InstructionSet::kArm)));
Scott Wakelingfe885462016-09-22 10:24:38 +01002496 // The load must immediately precede RecordPcInfo.
Artem Serov0fb37192016-12-06 18:13:40 +00002497 ExactAssemblyScope aas(GetVIXLAssembler(),
2498 vixl32::kMaxInstructionSizeInBytes,
2499 CodeBufferCheckScope::kMaximumSize);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002500 __ ldr(temp, MemOperand(temp));
2501 RecordPcInfo(nullptr, 0);
Scott Wakelingfe885462016-09-22 10:24:38 +01002502 }
2503
2504 __ Push(RegisterList(core_spill_mask_));
2505 GetAssembler()->cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(core_spill_mask_));
2506 GetAssembler()->cfi().RelOffsetForMany(DWARFReg(kMethodRegister),
2507 0,
2508 core_spill_mask_,
2509 kArmWordSize);
2510 if (fpu_spill_mask_ != 0) {
2511 uint32_t first = LeastSignificantBit(fpu_spill_mask_);
2512
2513 // Check that list is contiguous.
2514 DCHECK_EQ(fpu_spill_mask_ >> CTZ(fpu_spill_mask_), ~0u >> (32 - POPCOUNT(fpu_spill_mask_)));
2515
2516 __ Vpush(SRegisterList(vixl32::SRegister(first), POPCOUNT(fpu_spill_mask_)));
2517 GetAssembler()->cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(fpu_spill_mask_));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002518 GetAssembler()->cfi().RelOffsetForMany(DWARFReg(s0), 0, fpu_spill_mask_, kArmWordSize);
Scott Wakelingfe885462016-09-22 10:24:38 +01002519 }
Scott Wakelingbffdc702016-12-07 17:46:03 +00002520
Scott Wakelingfe885462016-09-22 10:24:38 +01002521 int adjust = GetFrameSize() - FrameEntrySpillSize();
2522 __ Sub(sp, sp, adjust);
2523 GetAssembler()->cfi().AdjustCFAOffset(adjust);
Scott Wakelingbffdc702016-12-07 17:46:03 +00002524
2525 // Save the current method if we need it. Note that we do not
2526 // do this in HCurrentMethod, as the instruction might have been removed
2527 // in the SSA graph.
2528 if (RequiresCurrentMethod()) {
2529 GetAssembler()->StoreToOffset(kStoreWord, kMethodRegister, sp, 0);
2530 }
Nicolas Geoffrayf7893532017-06-15 12:34:36 +01002531
2532 if (GetGraph()->HasShouldDeoptimizeFlag()) {
2533 UseScratchRegisterScope temps(GetVIXLAssembler());
2534 vixl32::Register temp = temps.Acquire();
2535 // Initialize should_deoptimize flag to 0.
2536 __ Mov(temp, 0);
2537 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, GetStackOffsetOfShouldDeoptimizeFlag());
2538 }
Roland Levillain5daa4952017-07-03 17:23:56 +01002539
2540 MaybeGenerateMarkingRegisterCheck(/* code */ 1);
Scott Wakelingfe885462016-09-22 10:24:38 +01002541}
2542
2543void CodeGeneratorARMVIXL::GenerateFrameExit() {
2544 if (HasEmptyFrame()) {
2545 __ Bx(lr);
2546 return;
2547 }
2548 GetAssembler()->cfi().RememberState();
2549 int adjust = GetFrameSize() - FrameEntrySpillSize();
2550 __ Add(sp, sp, adjust);
2551 GetAssembler()->cfi().AdjustCFAOffset(-adjust);
2552 if (fpu_spill_mask_ != 0) {
2553 uint32_t first = LeastSignificantBit(fpu_spill_mask_);
2554
2555 // Check that list is contiguous.
2556 DCHECK_EQ(fpu_spill_mask_ >> CTZ(fpu_spill_mask_), ~0u >> (32 - POPCOUNT(fpu_spill_mask_)));
2557
2558 __ Vpop(SRegisterList(vixl32::SRegister(first), POPCOUNT(fpu_spill_mask_)));
2559 GetAssembler()->cfi().AdjustCFAOffset(
2560 -static_cast<int>(kArmWordSize) * POPCOUNT(fpu_spill_mask_));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002561 GetAssembler()->cfi().RestoreMany(DWARFReg(vixl32::SRegister(0)), fpu_spill_mask_);
Scott Wakelingfe885462016-09-22 10:24:38 +01002562 }
2563 // Pop LR into PC to return.
2564 DCHECK_NE(core_spill_mask_ & (1 << kLrCode), 0U);
2565 uint32_t pop_mask = (core_spill_mask_ & (~(1 << kLrCode))) | 1 << kPcCode;
2566 __ Pop(RegisterList(pop_mask));
2567 GetAssembler()->cfi().RestoreState();
2568 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
2569}
2570
2571void CodeGeneratorARMVIXL::Bind(HBasicBlock* block) {
2572 __ Bind(GetLabelOf(block));
2573}
2574
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002575Location InvokeDexCallingConventionVisitorARMVIXL::GetNextLocation(DataType::Type type) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002576 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002577 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002578 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002579 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002580 case DataType::Type::kInt8:
2581 case DataType::Type::kUint16:
2582 case DataType::Type::kInt16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002583 case DataType::Type::kInt32: {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002584 uint32_t index = gp_index_++;
2585 uint32_t stack_index = stack_index_++;
2586 if (index < calling_convention.GetNumberOfRegisters()) {
2587 return LocationFrom(calling_convention.GetRegisterAt(index));
2588 } else {
2589 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
2590 }
2591 }
2592
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002593 case DataType::Type::kInt64: {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002594 uint32_t index = gp_index_;
2595 uint32_t stack_index = stack_index_;
2596 gp_index_ += 2;
2597 stack_index_ += 2;
2598 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
2599 if (calling_convention.GetRegisterAt(index).Is(r1)) {
2600 // Skip R1, and use R2_R3 instead.
2601 gp_index_++;
2602 index++;
2603 }
2604 }
2605 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
2606 DCHECK_EQ(calling_convention.GetRegisterAt(index).GetCode() + 1,
2607 calling_convention.GetRegisterAt(index + 1).GetCode());
2608
2609 return LocationFrom(calling_convention.GetRegisterAt(index),
2610 calling_convention.GetRegisterAt(index + 1));
2611 } else {
2612 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
2613 }
2614 }
2615
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002616 case DataType::Type::kFloat32: {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002617 uint32_t stack_index = stack_index_++;
2618 if (float_index_ % 2 == 0) {
2619 float_index_ = std::max(double_index_, float_index_);
2620 }
2621 if (float_index_ < calling_convention.GetNumberOfFpuRegisters()) {
2622 return LocationFrom(calling_convention.GetFpuRegisterAt(float_index_++));
2623 } else {
2624 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
2625 }
2626 }
2627
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002628 case DataType::Type::kFloat64: {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002629 double_index_ = std::max(double_index_, RoundUp(float_index_, 2));
2630 uint32_t stack_index = stack_index_;
2631 stack_index_ += 2;
2632 if (double_index_ + 1 < calling_convention.GetNumberOfFpuRegisters()) {
2633 uint32_t index = double_index_;
2634 double_index_ += 2;
2635 Location result = LocationFrom(
2636 calling_convention.GetFpuRegisterAt(index),
2637 calling_convention.GetFpuRegisterAt(index + 1));
2638 DCHECK(ExpectedPairLayout(result));
2639 return result;
2640 } else {
2641 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
2642 }
2643 }
2644
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002645 case DataType::Type::kVoid:
Artem Serovd4cc5b22016-11-04 11:19:09 +00002646 LOG(FATAL) << "Unexpected parameter type " << type;
2647 break;
2648 }
2649 return Location::NoLocation();
2650}
2651
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002652Location InvokeDexCallingConventionVisitorARMVIXL::GetReturnLocation(DataType::Type type) const {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002653 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002654 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002655 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002656 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002657 case DataType::Type::kInt8:
2658 case DataType::Type::kUint16:
2659 case DataType::Type::kInt16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002660 case DataType::Type::kInt32: {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002661 return LocationFrom(r0);
2662 }
2663
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002664 case DataType::Type::kFloat32: {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002665 return LocationFrom(s0);
2666 }
2667
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002668 case DataType::Type::kInt64: {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002669 return LocationFrom(r0, r1);
2670 }
2671
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002672 case DataType::Type::kFloat64: {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002673 return LocationFrom(s0, s1);
2674 }
2675
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002676 case DataType::Type::kVoid:
Artem Serovd4cc5b22016-11-04 11:19:09 +00002677 return Location::NoLocation();
2678 }
2679
2680 UNREACHABLE();
2681}
2682
2683Location InvokeDexCallingConventionVisitorARMVIXL::GetMethodLocation() const {
2684 return LocationFrom(kMethodRegister);
2685}
2686
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002687void CodeGeneratorARMVIXL::Move32(Location destination, Location source) {
2688 if (source.Equals(destination)) {
2689 return;
2690 }
2691 if (destination.IsRegister()) {
2692 if (source.IsRegister()) {
2693 __ Mov(RegisterFrom(destination), RegisterFrom(source));
2694 } else if (source.IsFpuRegister()) {
2695 __ Vmov(RegisterFrom(destination), SRegisterFrom(source));
2696 } else {
2697 GetAssembler()->LoadFromOffset(kLoadWord,
2698 RegisterFrom(destination),
2699 sp,
2700 source.GetStackIndex());
2701 }
2702 } else if (destination.IsFpuRegister()) {
2703 if (source.IsRegister()) {
2704 __ Vmov(SRegisterFrom(destination), RegisterFrom(source));
2705 } else if (source.IsFpuRegister()) {
2706 __ Vmov(SRegisterFrom(destination), SRegisterFrom(source));
2707 } else {
2708 GetAssembler()->LoadSFromOffset(SRegisterFrom(destination), sp, source.GetStackIndex());
2709 }
2710 } else {
2711 DCHECK(destination.IsStackSlot()) << destination;
2712 if (source.IsRegister()) {
2713 GetAssembler()->StoreToOffset(kStoreWord,
2714 RegisterFrom(source),
2715 sp,
2716 destination.GetStackIndex());
2717 } else if (source.IsFpuRegister()) {
2718 GetAssembler()->StoreSToOffset(SRegisterFrom(source), sp, destination.GetStackIndex());
2719 } else {
2720 DCHECK(source.IsStackSlot()) << source;
2721 UseScratchRegisterScope temps(GetVIXLAssembler());
2722 vixl32::Register temp = temps.Acquire();
2723 GetAssembler()->LoadFromOffset(kLoadWord, temp, sp, source.GetStackIndex());
2724 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
2725 }
2726 }
2727}
2728
Artem Serovcfbe9132016-10-14 15:58:56 +01002729void CodeGeneratorARMVIXL::MoveConstant(Location location, int32_t value) {
2730 DCHECK(location.IsRegister());
2731 __ Mov(RegisterFrom(location), value);
Scott Wakelingfe885462016-09-22 10:24:38 +01002732}
2733
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002734void CodeGeneratorARMVIXL::MoveLocation(Location dst, Location src, DataType::Type dst_type) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002735 // TODO(VIXL): Maybe refactor to have the 'move' implementation here and use it in
2736 // `ParallelMoveResolverARMVIXL::EmitMove`, as is done in the `arm64` backend.
Vladimir Markoca6fff82017-10-03 14:49:14 +01002737 HParallelMove move(GetGraph()->GetAllocator());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002738 move.AddMove(src, dst, dst_type, nullptr);
2739 GetMoveResolver()->EmitNativeCode(&move);
Scott Wakelingfe885462016-09-22 10:24:38 +01002740}
2741
Artem Serovcfbe9132016-10-14 15:58:56 +01002742void CodeGeneratorARMVIXL::AddLocationAsTemp(Location location, LocationSummary* locations) {
2743 if (location.IsRegister()) {
2744 locations->AddTemp(location);
2745 } else if (location.IsRegisterPair()) {
2746 locations->AddTemp(LocationFrom(LowRegisterFrom(location)));
2747 locations->AddTemp(LocationFrom(HighRegisterFrom(location)));
2748 } else {
2749 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
2750 }
Scott Wakelingfe885462016-09-22 10:24:38 +01002751}
2752
2753void CodeGeneratorARMVIXL::InvokeRuntime(QuickEntrypointEnum entrypoint,
2754 HInstruction* instruction,
2755 uint32_t dex_pc,
2756 SlowPathCode* slow_path) {
2757 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Alexandre Rames374ddf32016-11-04 10:40:49 +00002758 __ Ldr(lr, MemOperand(tr, GetThreadOffset<kArmPointerSize>(entrypoint).Int32Value()));
2759 // Ensure the pc position is recorded immediately after the `blx` instruction.
2760 // blx in T32 has only 16bit encoding that's why a stricter check for the scope is used.
Artem Serov0fb37192016-12-06 18:13:40 +00002761 ExactAssemblyScope aas(GetVIXLAssembler(),
2762 vixl32::k16BitT32InstructionSizeInBytes,
2763 CodeBufferCheckScope::kExactSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00002764 __ blx(lr);
Scott Wakelingfe885462016-09-22 10:24:38 +01002765 if (EntrypointRequiresStackMap(entrypoint)) {
2766 RecordPcInfo(instruction, dex_pc, slow_path);
2767 }
2768}
2769
2770void CodeGeneratorARMVIXL::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
2771 HInstruction* instruction,
2772 SlowPathCode* slow_path) {
2773 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Alexandre Rames374ddf32016-11-04 10:40:49 +00002774 __ Ldr(lr, MemOperand(tr, entry_point_offset));
Scott Wakelingfe885462016-09-22 10:24:38 +01002775 __ Blx(lr);
2776}
2777
Scott Wakelingfe885462016-09-22 10:24:38 +01002778void InstructionCodeGeneratorARMVIXL::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Aart Bika8b8e9b2018-01-09 11:01:02 -08002779 if (successor->IsExitBlock()) {
2780 DCHECK(got->GetPrevious()->AlwaysThrows());
2781 return; // no code needed
2782 }
2783
Scott Wakelingfe885462016-09-22 10:24:38 +01002784 HBasicBlock* block = got->GetBlock();
2785 HInstruction* previous = got->GetPrevious();
2786 HLoopInformation* info = block->GetLoopInformation();
2787
2788 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Scott Wakelingfe885462016-09-22 10:24:38 +01002789 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2790 return;
2791 }
2792 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2793 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
Roland Levillain5daa4952017-07-03 17:23:56 +01002794 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 2);
Scott Wakelingfe885462016-09-22 10:24:38 +01002795 }
2796 if (!codegen_->GoesToNextBlock(block, successor)) {
2797 __ B(codegen_->GetLabelOf(successor));
2798 }
2799}
2800
2801void LocationsBuilderARMVIXL::VisitGoto(HGoto* got) {
2802 got->SetLocations(nullptr);
2803}
2804
2805void InstructionCodeGeneratorARMVIXL::VisitGoto(HGoto* got) {
2806 HandleGoto(got, got->GetSuccessor());
2807}
2808
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002809void LocationsBuilderARMVIXL::VisitTryBoundary(HTryBoundary* try_boundary) {
2810 try_boundary->SetLocations(nullptr);
2811}
2812
2813void InstructionCodeGeneratorARMVIXL::VisitTryBoundary(HTryBoundary* try_boundary) {
2814 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2815 if (!successor->IsExitBlock()) {
2816 HandleGoto(try_boundary, successor);
2817 }
2818}
2819
Scott Wakelingfe885462016-09-22 10:24:38 +01002820void LocationsBuilderARMVIXL::VisitExit(HExit* exit) {
2821 exit->SetLocations(nullptr);
2822}
2823
2824void InstructionCodeGeneratorARMVIXL::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
2825}
2826
Scott Wakelingfe885462016-09-22 10:24:38 +01002827void InstructionCodeGeneratorARMVIXL::GenerateCompareTestAndBranch(HCondition* condition,
Anton Kirilov23b752b2017-07-20 14:40:44 +01002828 vixl32::Label* true_target,
2829 vixl32::Label* false_target,
Anton Kirilovfd522532017-05-10 12:46:57 +01002830 bool is_far_target) {
Anton Kirilov23b752b2017-07-20 14:40:44 +01002831 if (true_target == false_target) {
2832 DCHECK(true_target != nullptr);
2833 __ B(true_target);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002834 return;
2835 }
2836
Anton Kirilov23b752b2017-07-20 14:40:44 +01002837 vixl32::Label* non_fallthrough_target;
2838 bool invert;
2839 bool emit_both_branches;
Scott Wakelingfe885462016-09-22 10:24:38 +01002840
Anton Kirilov23b752b2017-07-20 14:40:44 +01002841 if (true_target == nullptr) {
2842 // The true target is fallthrough.
2843 DCHECK(false_target != nullptr);
2844 non_fallthrough_target = false_target;
2845 invert = true;
2846 emit_both_branches = false;
2847 } else {
2848 non_fallthrough_target = true_target;
2849 invert = false;
2850 // Either the false target is fallthrough, or there is no fallthrough
2851 // and both branches must be emitted.
2852 emit_both_branches = (false_target != nullptr);
Scott Wakelingfe885462016-09-22 10:24:38 +01002853 }
2854
Anton Kirilov23b752b2017-07-20 14:40:44 +01002855 const auto cond = GenerateTest(condition, invert, codegen_);
2856
2857 __ B(cond.first, non_fallthrough_target, is_far_target);
2858
2859 if (emit_both_branches) {
2860 // No target falls through, we need to branch.
2861 __ B(false_target);
Scott Wakelingfe885462016-09-22 10:24:38 +01002862 }
2863}
2864
2865void InstructionCodeGeneratorARMVIXL::GenerateTestAndBranch(HInstruction* instruction,
2866 size_t condition_input_index,
2867 vixl32::Label* true_target,
xueliang.zhongf51bc622016-11-04 09:23:32 +00002868 vixl32::Label* false_target,
2869 bool far_target) {
Scott Wakelingfe885462016-09-22 10:24:38 +01002870 HInstruction* cond = instruction->InputAt(condition_input_index);
2871
2872 if (true_target == nullptr && false_target == nullptr) {
2873 // Nothing to do. The code always falls through.
2874 return;
2875 } else if (cond->IsIntConstant()) {
2876 // Constant condition, statically compared against "true" (integer value 1).
2877 if (cond->AsIntConstant()->IsTrue()) {
2878 if (true_target != nullptr) {
2879 __ B(true_target);
2880 }
2881 } else {
Anton Kirilov644032c2016-12-06 17:51:43 +00002882 DCHECK(cond->AsIntConstant()->IsFalse()) << Int32ConstantFrom(cond);
Scott Wakelingfe885462016-09-22 10:24:38 +01002883 if (false_target != nullptr) {
2884 __ B(false_target);
2885 }
2886 }
2887 return;
2888 }
2889
2890 // The following code generates these patterns:
2891 // (1) true_target == nullptr && false_target != nullptr
2892 // - opposite condition true => branch to false_target
2893 // (2) true_target != nullptr && false_target == nullptr
2894 // - condition true => branch to true_target
2895 // (3) true_target != nullptr && false_target != nullptr
2896 // - condition true => branch to true_target
2897 // - branch to false_target
2898 if (IsBooleanValueOrMaterializedCondition(cond)) {
2899 // Condition has been materialized, compare the output to 0.
2900 if (kIsDebugBuild) {
2901 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
2902 DCHECK(cond_val.IsRegister());
2903 }
2904 if (true_target == nullptr) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00002905 __ CompareAndBranchIfZero(InputRegisterAt(instruction, condition_input_index),
2906 false_target,
2907 far_target);
Scott Wakelingfe885462016-09-22 10:24:38 +01002908 } else {
xueliang.zhongf51bc622016-11-04 09:23:32 +00002909 __ CompareAndBranchIfNonZero(InputRegisterAt(instruction, condition_input_index),
2910 true_target,
2911 far_target);
Scott Wakelingfe885462016-09-22 10:24:38 +01002912 }
2913 } else {
2914 // Condition has not been materialized. Use its inputs as the comparison and
2915 // its condition as the branch condition.
2916 HCondition* condition = cond->AsCondition();
2917
2918 // If this is a long or FP comparison that has been folded into
2919 // the HCondition, generate the comparison directly.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002920 DataType::Type type = condition->InputAt(0)->GetType();
2921 if (type == DataType::Type::kInt64 || DataType::IsFloatingPointType(type)) {
Anton Kirilovfd522532017-05-10 12:46:57 +01002922 GenerateCompareTestAndBranch(condition, true_target, false_target, far_target);
Scott Wakelingfe885462016-09-22 10:24:38 +01002923 return;
2924 }
2925
Donghui Bai426b49c2016-11-08 14:55:38 +08002926 vixl32::Label* non_fallthrough_target;
2927 vixl32::Condition arm_cond = vixl32::Condition::None();
2928 const vixl32::Register left = InputRegisterAt(cond, 0);
2929 const Operand right = InputOperandAt(cond, 1);
2930
Scott Wakelingfe885462016-09-22 10:24:38 +01002931 if (true_target == nullptr) {
Donghui Bai426b49c2016-11-08 14:55:38 +08002932 arm_cond = ARMCondition(condition->GetOppositeCondition());
2933 non_fallthrough_target = false_target;
Scott Wakelingfe885462016-09-22 10:24:38 +01002934 } else {
Donghui Bai426b49c2016-11-08 14:55:38 +08002935 arm_cond = ARMCondition(condition->GetCondition());
2936 non_fallthrough_target = true_target;
2937 }
2938
2939 if (right.IsImmediate() && right.GetImmediate() == 0 && (arm_cond.Is(ne) || arm_cond.Is(eq))) {
2940 if (arm_cond.Is(eq)) {
Anton Kirilovfd522532017-05-10 12:46:57 +01002941 __ CompareAndBranchIfZero(left, non_fallthrough_target, far_target);
Donghui Bai426b49c2016-11-08 14:55:38 +08002942 } else {
2943 DCHECK(arm_cond.Is(ne));
Anton Kirilovfd522532017-05-10 12:46:57 +01002944 __ CompareAndBranchIfNonZero(left, non_fallthrough_target, far_target);
Donghui Bai426b49c2016-11-08 14:55:38 +08002945 }
2946 } else {
2947 __ Cmp(left, right);
Anton Kirilovfd522532017-05-10 12:46:57 +01002948 __ B(arm_cond, non_fallthrough_target, far_target);
Scott Wakelingfe885462016-09-22 10:24:38 +01002949 }
2950 }
2951
2952 // If neither branch falls through (case 3), the conditional branch to `true_target`
2953 // was already emitted (case 2) and we need to emit a jump to `false_target`.
2954 if (true_target != nullptr && false_target != nullptr) {
2955 __ B(false_target);
2956 }
2957}
2958
2959void LocationsBuilderARMVIXL::VisitIf(HIf* if_instr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002960 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(if_instr);
Scott Wakelingfe885462016-09-22 10:24:38 +01002961 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
2962 locations->SetInAt(0, Location::RequiresRegister());
2963 }
2964}
2965
2966void InstructionCodeGeneratorARMVIXL::VisitIf(HIf* if_instr) {
2967 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
2968 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002969 vixl32::Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
2970 nullptr : codegen_->GetLabelOf(true_successor);
2971 vixl32::Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
2972 nullptr : codegen_->GetLabelOf(false_successor);
Scott Wakelingfe885462016-09-22 10:24:38 +01002973 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
2974}
2975
Scott Wakelingc34dba72016-10-03 10:14:44 +01002976void LocationsBuilderARMVIXL::VisitDeoptimize(HDeoptimize* deoptimize) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002977 LocationSummary* locations = new (GetGraph()->GetAllocator())
Scott Wakelingc34dba72016-10-03 10:14:44 +01002978 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01002979 InvokeRuntimeCallingConventionARMVIXL calling_convention;
2980 RegisterSet caller_saves = RegisterSet::Empty();
2981 caller_saves.Add(LocationFrom(calling_convention.GetRegisterAt(0)));
2982 locations->SetCustomSlowPathCallerSaves(caller_saves);
Scott Wakelingc34dba72016-10-03 10:14:44 +01002983 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
2984 locations->SetInAt(0, Location::RequiresRegister());
2985 }
2986}
2987
2988void InstructionCodeGeneratorARMVIXL::VisitDeoptimize(HDeoptimize* deoptimize) {
2989 SlowPathCodeARMVIXL* slow_path =
2990 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARMVIXL>(deoptimize);
2991 GenerateTestAndBranch(deoptimize,
2992 /* condition_input_index */ 0,
2993 slow_path->GetEntryLabel(),
2994 /* false_target */ nullptr);
2995}
2996
Artem Serovd4cc5b22016-11-04 11:19:09 +00002997void LocationsBuilderARMVIXL::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002998 LocationSummary* locations = new (GetGraph()->GetAllocator())
Artem Serovd4cc5b22016-11-04 11:19:09 +00002999 LocationSummary(flag, LocationSummary::kNoCall);
3000 locations->SetOut(Location::RequiresRegister());
3001}
3002
3003void InstructionCodeGeneratorARMVIXL::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
3004 GetAssembler()->LoadFromOffset(kLoadWord,
3005 OutputRegister(flag),
3006 sp,
3007 codegen_->GetStackOffsetOfShouldDeoptimizeFlag());
3008}
3009
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003010void LocationsBuilderARMVIXL::VisitSelect(HSelect* select) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003011 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(select);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003012 const bool is_floating_point = DataType::IsFloatingPointType(select->GetType());
Donghui Bai426b49c2016-11-08 14:55:38 +08003013
3014 if (is_floating_point) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003015 locations->SetInAt(0, Location::RequiresFpuRegister());
Donghui Bai426b49c2016-11-08 14:55:38 +08003016 locations->SetInAt(1, Location::FpuRegisterOrConstant(select->GetTrueValue()));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003017 } else {
3018 locations->SetInAt(0, Location::RequiresRegister());
Donghui Bai426b49c2016-11-08 14:55:38 +08003019 locations->SetInAt(1, Arm8BitEncodableConstantOrRegister(select->GetTrueValue()));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003020 }
Donghui Bai426b49c2016-11-08 14:55:38 +08003021
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003022 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
Donghui Bai426b49c2016-11-08 14:55:38 +08003023 locations->SetInAt(2, Location::RegisterOrConstant(select->GetCondition()));
3024 // The code generator handles overlap with the values, but not with the condition.
3025 locations->SetOut(Location::SameAsFirstInput());
3026 } else if (is_floating_point) {
3027 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3028 } else {
3029 if (!locations->InAt(1).IsConstant()) {
3030 locations->SetInAt(0, Arm8BitEncodableConstantOrRegister(select->GetFalseValue()));
3031 }
3032
3033 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003034 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003035}
3036
3037void InstructionCodeGeneratorARMVIXL::VisitSelect(HSelect* select) {
Donghui Bai426b49c2016-11-08 14:55:38 +08003038 HInstruction* const condition = select->GetCondition();
3039 const LocationSummary* const locations = select->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003040 const DataType::Type type = select->GetType();
Donghui Bai426b49c2016-11-08 14:55:38 +08003041 const Location first = locations->InAt(0);
3042 const Location out = locations->Out();
3043 const Location second = locations->InAt(1);
3044 Location src;
3045
3046 if (condition->IsIntConstant()) {
3047 if (condition->AsIntConstant()->IsFalse()) {
3048 src = first;
3049 } else {
3050 src = second;
3051 }
3052
3053 codegen_->MoveLocation(out, src, type);
3054 return;
3055 }
3056
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003057 if (!DataType::IsFloatingPointType(type)) {
Donghui Bai426b49c2016-11-08 14:55:38 +08003058 bool invert = false;
3059
3060 if (out.Equals(second)) {
3061 src = first;
3062 invert = true;
3063 } else if (out.Equals(first)) {
3064 src = second;
3065 } else if (second.IsConstant()) {
3066 DCHECK(CanEncodeConstantAs8BitImmediate(second.GetConstant()));
3067 src = second;
3068 } else if (first.IsConstant()) {
3069 DCHECK(CanEncodeConstantAs8BitImmediate(first.GetConstant()));
3070 src = first;
3071 invert = true;
3072 } else {
3073 src = second;
3074 }
3075
3076 if (CanGenerateConditionalMove(out, src)) {
3077 if (!out.Equals(first) && !out.Equals(second)) {
3078 codegen_->MoveLocation(out, src.Equals(first) ? second : first, type);
3079 }
3080
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003081 std::pair<vixl32::Condition, vixl32::Condition> cond(eq, ne);
3082
3083 if (IsBooleanValueOrMaterializedCondition(condition)) {
3084 __ Cmp(InputRegisterAt(select, 2), 0);
3085 cond = invert ? std::make_pair(eq, ne) : std::make_pair(ne, eq);
3086 } else {
3087 cond = GenerateTest(condition->AsCondition(), invert, codegen_);
3088 }
3089
Donghui Bai426b49c2016-11-08 14:55:38 +08003090 const size_t instr_count = out.IsRegisterPair() ? 4 : 2;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003091 // We use the scope because of the IT block that follows.
Donghui Bai426b49c2016-11-08 14:55:38 +08003092 ExactAssemblyScope guard(GetVIXLAssembler(),
3093 instr_count * vixl32::k16BitT32InstructionSizeInBytes,
3094 CodeBufferCheckScope::kExactSize);
3095
3096 if (out.IsRegister()) {
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003097 __ it(cond.first);
3098 __ mov(cond.first, RegisterFrom(out), OperandFrom(src, type));
Donghui Bai426b49c2016-11-08 14:55:38 +08003099 } else {
3100 DCHECK(out.IsRegisterPair());
3101
3102 Operand operand_high(0);
3103 Operand operand_low(0);
3104
3105 if (src.IsConstant()) {
3106 const int64_t value = Int64ConstantFrom(src);
3107
3108 operand_high = High32Bits(value);
3109 operand_low = Low32Bits(value);
3110 } else {
3111 DCHECK(src.IsRegisterPair());
3112 operand_high = HighRegisterFrom(src);
3113 operand_low = LowRegisterFrom(src);
3114 }
3115
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003116 __ it(cond.first);
3117 __ mov(cond.first, LowRegisterFrom(out), operand_low);
3118 __ it(cond.first);
3119 __ mov(cond.first, HighRegisterFrom(out), operand_high);
Donghui Bai426b49c2016-11-08 14:55:38 +08003120 }
3121
3122 return;
3123 }
3124 }
3125
3126 vixl32::Label* false_target = nullptr;
3127 vixl32::Label* true_target = nullptr;
3128 vixl32::Label select_end;
3129 vixl32::Label* const target = codegen_->GetFinalLabel(select, &select_end);
3130
3131 if (out.Equals(second)) {
3132 true_target = target;
3133 src = first;
3134 } else {
3135 false_target = target;
3136 src = second;
3137
3138 if (!out.Equals(first)) {
3139 codegen_->MoveLocation(out, first, type);
3140 }
3141 }
3142
3143 GenerateTestAndBranch(select, 2, true_target, false_target, /* far_target */ false);
3144 codegen_->MoveLocation(out, src, type);
3145
3146 if (select_end.IsReferenced()) {
3147 __ Bind(&select_end);
3148 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003149}
3150
Artem Serov551b28f2016-10-18 19:11:30 +01003151void LocationsBuilderARMVIXL::VisitNativeDebugInfo(HNativeDebugInfo* info) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003152 new (GetGraph()->GetAllocator()) LocationSummary(info);
Artem Serov551b28f2016-10-18 19:11:30 +01003153}
3154
3155void InstructionCodeGeneratorARMVIXL::VisitNativeDebugInfo(HNativeDebugInfo*) {
3156 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
3157}
3158
Scott Wakelingfe885462016-09-22 10:24:38 +01003159void CodeGeneratorARMVIXL::GenerateNop() {
3160 __ Nop();
3161}
3162
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003163// `temp` is an extra temporary register that is used for some conditions;
3164// callers may not specify it, in which case the method will use a scratch
3165// register instead.
3166void CodeGeneratorARMVIXL::GenerateConditionWithZero(IfCondition condition,
3167 vixl32::Register out,
3168 vixl32::Register in,
3169 vixl32::Register temp) {
3170 switch (condition) {
3171 case kCondEQ:
3172 // x <= 0 iff x == 0 when the comparison is unsigned.
3173 case kCondBE:
3174 if (!temp.IsValid() || (out.IsLow() && !out.Is(in))) {
3175 temp = out;
3176 }
3177
3178 // Avoid 32-bit instructions if possible; note that `in` and `temp` must be
3179 // different as well.
3180 if (in.IsLow() && temp.IsLow() && !in.Is(temp)) {
3181 // temp = - in; only 0 sets the carry flag.
3182 __ Rsbs(temp, in, 0);
3183
3184 if (out.Is(in)) {
3185 std::swap(in, temp);
3186 }
3187
3188 // out = - in + in + carry = carry
3189 __ Adc(out, temp, in);
3190 } else {
3191 // If `in` is 0, then it has 32 leading zeros, and less than that otherwise.
3192 __ Clz(out, in);
3193 // Any number less than 32 logically shifted right by 5 bits results in 0;
3194 // the same operation on 32 yields 1.
3195 __ Lsr(out, out, 5);
3196 }
3197
3198 break;
3199 case kCondNE:
3200 // x > 0 iff x != 0 when the comparison is unsigned.
3201 case kCondA: {
3202 UseScratchRegisterScope temps(GetVIXLAssembler());
3203
3204 if (out.Is(in)) {
3205 if (!temp.IsValid() || in.Is(temp)) {
3206 temp = temps.Acquire();
3207 }
3208 } else if (!temp.IsValid() || !temp.IsLow()) {
3209 temp = out;
3210 }
3211
3212 // temp = in - 1; only 0 does not set the carry flag.
3213 __ Subs(temp, in, 1);
3214 // out = in + ~temp + carry = in + (-(in - 1) - 1) + carry = in - in + 1 - 1 + carry = carry
3215 __ Sbc(out, in, temp);
3216 break;
3217 }
3218 case kCondGE:
3219 __ Mvn(out, in);
3220 in = out;
3221 FALLTHROUGH_INTENDED;
3222 case kCondLT:
3223 // We only care about the sign bit.
3224 __ Lsr(out, in, 31);
3225 break;
3226 case kCondAE:
3227 // Trivially true.
3228 __ Mov(out, 1);
3229 break;
3230 case kCondB:
3231 // Trivially false.
3232 __ Mov(out, 0);
3233 break;
3234 default:
3235 LOG(FATAL) << "Unexpected condition " << condition;
3236 UNREACHABLE();
3237 }
3238}
3239
Scott Wakelingfe885462016-09-22 10:24:38 +01003240void LocationsBuilderARMVIXL::HandleCondition(HCondition* cond) {
3241 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003242 new (GetGraph()->GetAllocator()) LocationSummary(cond, LocationSummary::kNoCall);
Scott Wakelingfe885462016-09-22 10:24:38 +01003243 // Handle the long/FP comparisons made in instruction simplification.
3244 switch (cond->InputAt(0)->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003245 case DataType::Type::kInt64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003246 locations->SetInAt(0, Location::RequiresRegister());
3247 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
3248 if (!cond->IsEmittedAtUseSite()) {
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003249 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Scott Wakelingfe885462016-09-22 10:24:38 +01003250 }
3251 break;
3252
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003253 case DataType::Type::kFloat32:
3254 case DataType::Type::kFloat64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003255 locations->SetInAt(0, Location::RequiresFpuRegister());
Artem Serov657022c2016-11-23 14:19:38 +00003256 locations->SetInAt(1, ArithmeticZeroOrFpuRegister(cond->InputAt(1)));
Scott Wakelingfe885462016-09-22 10:24:38 +01003257 if (!cond->IsEmittedAtUseSite()) {
3258 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3259 }
3260 break;
3261
3262 default:
3263 locations->SetInAt(0, Location::RequiresRegister());
3264 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
3265 if (!cond->IsEmittedAtUseSite()) {
3266 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3267 }
3268 }
3269}
3270
3271void InstructionCodeGeneratorARMVIXL::HandleCondition(HCondition* cond) {
3272 if (cond->IsEmittedAtUseSite()) {
3273 return;
3274 }
3275
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003276 const DataType::Type type = cond->GetLeft()->GetType();
Scott Wakelingfe885462016-09-22 10:24:38 +01003277
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003278 if (DataType::IsFloatingPointType(type)) {
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003279 GenerateConditionGeneric(cond, codegen_);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003280 return;
Scott Wakelingfe885462016-09-22 10:24:38 +01003281 }
3282
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003283 DCHECK(DataType::IsIntegralType(type) || type == DataType::Type::kReference) << type;
Scott Wakelingfe885462016-09-22 10:24:38 +01003284
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003285 const IfCondition condition = cond->GetCondition();
Scott Wakelingfe885462016-09-22 10:24:38 +01003286
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003287 // A condition with only one boolean input, or two boolean inputs without being equality or
3288 // inequality results from transformations done by the instruction simplifier, and is handled
3289 // as a regular condition with integral inputs.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003290 if (type == DataType::Type::kBool &&
3291 cond->GetRight()->GetType() == DataType::Type::kBool &&
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003292 (condition == kCondEQ || condition == kCondNE)) {
3293 vixl32::Register left = InputRegisterAt(cond, 0);
3294 const vixl32::Register out = OutputRegister(cond);
3295 const Location right_loc = cond->GetLocations()->InAt(1);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003296
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003297 // The constant case is handled by the instruction simplifier.
3298 DCHECK(!right_loc.IsConstant());
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003299
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003300 vixl32::Register right = RegisterFrom(right_loc);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003301
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003302 // Avoid 32-bit instructions if possible.
3303 if (out.Is(right)) {
3304 std::swap(left, right);
3305 }
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003306
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003307 __ Eor(out, left, right);
3308
3309 if (condition == kCondEQ) {
3310 __ Eor(out, out, 1);
3311 }
3312
3313 return;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003314 }
Anton Kirilov6f644202017-02-27 18:29:45 +00003315
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003316 GenerateConditionIntegralOrNonPrimitive(cond, codegen_);
Scott Wakelingfe885462016-09-22 10:24:38 +01003317}
3318
3319void LocationsBuilderARMVIXL::VisitEqual(HEqual* comp) {
3320 HandleCondition(comp);
3321}
3322
3323void InstructionCodeGeneratorARMVIXL::VisitEqual(HEqual* comp) {
3324 HandleCondition(comp);
3325}
3326
3327void LocationsBuilderARMVIXL::VisitNotEqual(HNotEqual* comp) {
3328 HandleCondition(comp);
3329}
3330
3331void InstructionCodeGeneratorARMVIXL::VisitNotEqual(HNotEqual* comp) {
3332 HandleCondition(comp);
3333}
3334
3335void LocationsBuilderARMVIXL::VisitLessThan(HLessThan* comp) {
3336 HandleCondition(comp);
3337}
3338
3339void InstructionCodeGeneratorARMVIXL::VisitLessThan(HLessThan* comp) {
3340 HandleCondition(comp);
3341}
3342
3343void LocationsBuilderARMVIXL::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
3344 HandleCondition(comp);
3345}
3346
3347void InstructionCodeGeneratorARMVIXL::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
3348 HandleCondition(comp);
3349}
3350
3351void LocationsBuilderARMVIXL::VisitGreaterThan(HGreaterThan* comp) {
3352 HandleCondition(comp);
3353}
3354
3355void InstructionCodeGeneratorARMVIXL::VisitGreaterThan(HGreaterThan* comp) {
3356 HandleCondition(comp);
3357}
3358
3359void LocationsBuilderARMVIXL::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
3360 HandleCondition(comp);
3361}
3362
3363void InstructionCodeGeneratorARMVIXL::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
3364 HandleCondition(comp);
3365}
3366
3367void LocationsBuilderARMVIXL::VisitBelow(HBelow* comp) {
3368 HandleCondition(comp);
3369}
3370
3371void InstructionCodeGeneratorARMVIXL::VisitBelow(HBelow* comp) {
3372 HandleCondition(comp);
3373}
3374
3375void LocationsBuilderARMVIXL::VisitBelowOrEqual(HBelowOrEqual* comp) {
3376 HandleCondition(comp);
3377}
3378
3379void InstructionCodeGeneratorARMVIXL::VisitBelowOrEqual(HBelowOrEqual* comp) {
3380 HandleCondition(comp);
3381}
3382
3383void LocationsBuilderARMVIXL::VisitAbove(HAbove* comp) {
3384 HandleCondition(comp);
3385}
3386
3387void InstructionCodeGeneratorARMVIXL::VisitAbove(HAbove* comp) {
3388 HandleCondition(comp);
3389}
3390
3391void LocationsBuilderARMVIXL::VisitAboveOrEqual(HAboveOrEqual* comp) {
3392 HandleCondition(comp);
3393}
3394
3395void InstructionCodeGeneratorARMVIXL::VisitAboveOrEqual(HAboveOrEqual* comp) {
3396 HandleCondition(comp);
3397}
3398
3399void LocationsBuilderARMVIXL::VisitIntConstant(HIntConstant* constant) {
3400 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003401 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Scott Wakelingfe885462016-09-22 10:24:38 +01003402 locations->SetOut(Location::ConstantLocation(constant));
3403}
3404
3405void InstructionCodeGeneratorARMVIXL::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
3406 // Will be generated at use site.
3407}
3408
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003409void LocationsBuilderARMVIXL::VisitNullConstant(HNullConstant* constant) {
3410 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003411 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003412 locations->SetOut(Location::ConstantLocation(constant));
3413}
3414
3415void InstructionCodeGeneratorARMVIXL::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
3416 // Will be generated at use site.
3417}
3418
Scott Wakelingfe885462016-09-22 10:24:38 +01003419void LocationsBuilderARMVIXL::VisitLongConstant(HLongConstant* constant) {
3420 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003421 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Scott Wakelingfe885462016-09-22 10:24:38 +01003422 locations->SetOut(Location::ConstantLocation(constant));
3423}
3424
3425void InstructionCodeGeneratorARMVIXL::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
3426 // Will be generated at use site.
3427}
3428
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01003429void LocationsBuilderARMVIXL::VisitFloatConstant(HFloatConstant* constant) {
3430 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003431 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01003432 locations->SetOut(Location::ConstantLocation(constant));
3433}
3434
Scott Wakelingc34dba72016-10-03 10:14:44 +01003435void InstructionCodeGeneratorARMVIXL::VisitFloatConstant(
3436 HFloatConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01003437 // Will be generated at use site.
3438}
3439
3440void LocationsBuilderARMVIXL::VisitDoubleConstant(HDoubleConstant* constant) {
3441 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003442 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01003443 locations->SetOut(Location::ConstantLocation(constant));
3444}
3445
Scott Wakelingc34dba72016-10-03 10:14:44 +01003446void InstructionCodeGeneratorARMVIXL::VisitDoubleConstant(
3447 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01003448 // Will be generated at use site.
3449}
3450
Igor Murashkind01745e2017-04-05 16:40:31 -07003451void LocationsBuilderARMVIXL::VisitConstructorFence(HConstructorFence* constructor_fence) {
3452 constructor_fence->SetLocations(nullptr);
3453}
3454
3455void InstructionCodeGeneratorARMVIXL::VisitConstructorFence(
3456 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
3457 codegen_->GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
3458}
3459
Scott Wakelingfe885462016-09-22 10:24:38 +01003460void LocationsBuilderARMVIXL::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3461 memory_barrier->SetLocations(nullptr);
3462}
3463
3464void InstructionCodeGeneratorARMVIXL::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3465 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
3466}
3467
3468void LocationsBuilderARMVIXL::VisitReturnVoid(HReturnVoid* ret) {
3469 ret->SetLocations(nullptr);
3470}
3471
3472void InstructionCodeGeneratorARMVIXL::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
3473 codegen_->GenerateFrameExit();
3474}
3475
3476void LocationsBuilderARMVIXL::VisitReturn(HReturn* ret) {
3477 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003478 new (GetGraph()->GetAllocator()) LocationSummary(ret, LocationSummary::kNoCall);
Scott Wakelingfe885462016-09-22 10:24:38 +01003479 locations->SetInAt(0, parameter_visitor_.GetReturnLocation(ret->InputAt(0)->GetType()));
3480}
3481
3482void InstructionCodeGeneratorARMVIXL::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
3483 codegen_->GenerateFrameExit();
3484}
3485
Artem Serovcfbe9132016-10-14 15:58:56 +01003486void LocationsBuilderARMVIXL::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3487 // The trampoline uses the same calling convention as dex calling conventions,
3488 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
3489 // the method_idx.
3490 HandleInvoke(invoke);
3491}
3492
3493void InstructionCodeGeneratorARMVIXL::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3494 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
Roland Levillain5daa4952017-07-03 17:23:56 +01003495 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 3);
Artem Serovcfbe9132016-10-14 15:58:56 +01003496}
3497
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003498void LocationsBuilderARMVIXL::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
3499 // Explicit clinit checks triggered by static invokes must have been pruned by
3500 // art::PrepareForRegisterAllocation.
3501 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
3502
Anton Kirilov5ec62182016-10-13 20:16:02 +01003503 IntrinsicLocationsBuilderARMVIXL intrinsic(codegen_);
3504 if (intrinsic.TryDispatch(invoke)) {
Anton Kirilov5ec62182016-10-13 20:16:02 +01003505 return;
3506 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003507
3508 HandleInvoke(invoke);
3509}
3510
Anton Kirilov5ec62182016-10-13 20:16:02 +01003511static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARMVIXL* codegen) {
3512 if (invoke->GetLocations()->Intrinsified()) {
3513 IntrinsicCodeGeneratorARMVIXL intrinsic(codegen);
3514 intrinsic.Dispatch(invoke);
3515 return true;
3516 }
3517 return false;
3518}
3519
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003520void InstructionCodeGeneratorARMVIXL::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
3521 // Explicit clinit checks triggered by static invokes must have been pruned by
3522 // art::PrepareForRegisterAllocation.
3523 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
3524
Anton Kirilov5ec62182016-10-13 20:16:02 +01003525 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
Roland Levillain5daa4952017-07-03 17:23:56 +01003526 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 4);
Anton Kirilov5ec62182016-10-13 20:16:02 +01003527 return;
3528 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003529
3530 LocationSummary* locations = invoke->GetLocations();
Artem Serovd4cc5b22016-11-04 11:19:09 +00003531 codegen_->GenerateStaticOrDirectCall(
3532 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Roland Levillain5daa4952017-07-03 17:23:56 +01003533
3534 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 5);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003535}
3536
3537void LocationsBuilderARMVIXL::HandleInvoke(HInvoke* invoke) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00003538 InvokeDexCallingConventionVisitorARMVIXL calling_convention_visitor;
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003539 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
3540}
3541
3542void LocationsBuilderARMVIXL::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Anton Kirilov5ec62182016-10-13 20:16:02 +01003543 IntrinsicLocationsBuilderARMVIXL intrinsic(codegen_);
3544 if (intrinsic.TryDispatch(invoke)) {
3545 return;
3546 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003547
3548 HandleInvoke(invoke);
3549}
3550
3551void InstructionCodeGeneratorARMVIXL::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Anton Kirilov5ec62182016-10-13 20:16:02 +01003552 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
Roland Levillain5daa4952017-07-03 17:23:56 +01003553 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 6);
Anton Kirilov5ec62182016-10-13 20:16:02 +01003554 return;
3555 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003556
3557 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexandre Rames374ddf32016-11-04 10:40:49 +00003558 DCHECK(!codegen_->IsLeafMethod());
Roland Levillain5daa4952017-07-03 17:23:56 +01003559
3560 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 7);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003561}
3562
Artem Serovcfbe9132016-10-14 15:58:56 +01003563void LocationsBuilderARMVIXL::VisitInvokeInterface(HInvokeInterface* invoke) {
3564 HandleInvoke(invoke);
3565 // Add the hidden argument.
3566 invoke->GetLocations()->AddTemp(LocationFrom(r12));
3567}
3568
3569void InstructionCodeGeneratorARMVIXL::VisitInvokeInterface(HInvokeInterface* invoke) {
3570 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
3571 LocationSummary* locations = invoke->GetLocations();
3572 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
3573 vixl32::Register hidden_reg = RegisterFrom(locations->GetTemp(1));
3574 Location receiver = locations->InAt(0);
3575 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3576
3577 DCHECK(!receiver.IsStackSlot());
3578
Alexandre Rames374ddf32016-11-04 10:40:49 +00003579 // Ensure the pc position is recorded immediately after the `ldr` instruction.
3580 {
Artem Serov0fb37192016-12-06 18:13:40 +00003581 ExactAssemblyScope aas(GetVIXLAssembler(),
3582 vixl32::kMaxInstructionSizeInBytes,
3583 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00003584 // /* HeapReference<Class> */ temp = receiver->klass_
3585 __ ldr(temp, MemOperand(RegisterFrom(receiver), class_offset));
3586 codegen_->MaybeRecordImplicitNullCheck(invoke);
3587 }
Artem Serovcfbe9132016-10-14 15:58:56 +01003588 // Instead of simply (possibly) unpoisoning `temp` here, we should
3589 // emit a read barrier for the previous class reference load.
3590 // However this is not required in practice, as this is an
3591 // intermediate/temporary reference and because the current
3592 // concurrent copying collector keeps the from-space memory
3593 // intact/accessible until the end of the marking phase (the
3594 // concurrent copying collector may not in the future).
3595 GetAssembler()->MaybeUnpoisonHeapReference(temp);
3596 GetAssembler()->LoadFromOffset(kLoadWord,
3597 temp,
3598 temp,
3599 mirror::Class::ImtPtrOffset(kArmPointerSize).Uint32Value());
3600 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
3601 invoke->GetImtIndex(), kArmPointerSize));
3602 // temp = temp->GetImtEntryAt(method_offset);
3603 GetAssembler()->LoadFromOffset(kLoadWord, temp, temp, method_offset);
3604 uint32_t entry_point =
3605 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize).Int32Value();
3606 // LR = temp->GetEntryPoint();
3607 GetAssembler()->LoadFromOffset(kLoadWord, lr, temp, entry_point);
3608
3609 // Set the hidden (in r12) argument. It is done here, right before a BLX to prevent other
3610 // instruction from clobbering it as they might use r12 as a scratch register.
3611 DCHECK(hidden_reg.Is(r12));
Scott Wakelingb77051e2016-11-21 19:46:00 +00003612
3613 {
3614 // The VIXL macro assembler may clobber any of the scratch registers that are available to it,
3615 // so it checks if the application is using them (by passing them to the macro assembler
3616 // methods). The following application of UseScratchRegisterScope corrects VIXL's notion of
3617 // what is available, and is the opposite of the standard usage: Instead of requesting a
3618 // temporary location, it imposes an external constraint (i.e. a specific register is reserved
3619 // for the hidden argument). Note that this works even if VIXL needs a scratch register itself
3620 // (to materialize the constant), since the destination register becomes available for such use
3621 // internally for the duration of the macro instruction.
3622 UseScratchRegisterScope temps(GetVIXLAssembler());
3623 temps.Exclude(hidden_reg);
3624 __ Mov(hidden_reg, invoke->GetDexMethodIndex());
3625 }
Artem Serovcfbe9132016-10-14 15:58:56 +01003626 {
Alexandre Rames374ddf32016-11-04 10:40:49 +00003627 // Ensure the pc position is recorded immediately after the `blx` instruction.
3628 // 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 +00003629 ExactAssemblyScope aas(GetVIXLAssembler(),
Alexandre Rames374ddf32016-11-04 10:40:49 +00003630 vixl32::k16BitT32InstructionSizeInBytes,
3631 CodeBufferCheckScope::kExactSize);
Artem Serovcfbe9132016-10-14 15:58:56 +01003632 // LR();
3633 __ blx(lr);
Artem Serovcfbe9132016-10-14 15:58:56 +01003634 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Alexandre Rames374ddf32016-11-04 10:40:49 +00003635 DCHECK(!codegen_->IsLeafMethod());
Artem Serovcfbe9132016-10-14 15:58:56 +01003636 }
Roland Levillain5daa4952017-07-03 17:23:56 +01003637
3638 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 8);
Artem Serovcfbe9132016-10-14 15:58:56 +01003639}
3640
Orion Hodsonac141392017-01-13 11:53:47 +00003641void LocationsBuilderARMVIXL::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
3642 HandleInvoke(invoke);
3643}
3644
3645void InstructionCodeGeneratorARMVIXL::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
3646 codegen_->GenerateInvokePolymorphicCall(invoke);
Roland Levillain5daa4952017-07-03 17:23:56 +01003647 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 9);
Orion Hodsonac141392017-01-13 11:53:47 +00003648}
3649
Artem Serov02109dd2016-09-23 17:17:54 +01003650void LocationsBuilderARMVIXL::VisitNeg(HNeg* neg) {
3651 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003652 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Artem Serov02109dd2016-09-23 17:17:54 +01003653 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003654 case DataType::Type::kInt32: {
Artem Serov02109dd2016-09-23 17:17:54 +01003655 locations->SetInAt(0, Location::RequiresRegister());
3656 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3657 break;
3658 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003659 case DataType::Type::kInt64: {
Artem Serov02109dd2016-09-23 17:17:54 +01003660 locations->SetInAt(0, Location::RequiresRegister());
3661 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3662 break;
3663 }
3664
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003665 case DataType::Type::kFloat32:
3666 case DataType::Type::kFloat64:
Artem Serov02109dd2016-09-23 17:17:54 +01003667 locations->SetInAt(0, Location::RequiresFpuRegister());
3668 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3669 break;
3670
3671 default:
3672 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
3673 }
3674}
3675
3676void InstructionCodeGeneratorARMVIXL::VisitNeg(HNeg* neg) {
3677 LocationSummary* locations = neg->GetLocations();
3678 Location out = locations->Out();
3679 Location in = locations->InAt(0);
3680 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003681 case DataType::Type::kInt32:
Artem Serov02109dd2016-09-23 17:17:54 +01003682 __ Rsb(OutputRegister(neg), InputRegisterAt(neg, 0), 0);
3683 break;
3684
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003685 case DataType::Type::kInt64:
Artem Serov02109dd2016-09-23 17:17:54 +01003686 // out.lo = 0 - in.lo (and update the carry/borrow (C) flag)
3687 __ Rsbs(LowRegisterFrom(out), LowRegisterFrom(in), 0);
3688 // We cannot emit an RSC (Reverse Subtract with Carry)
3689 // instruction here, as it does not exist in the Thumb-2
3690 // instruction set. We use the following approach
3691 // using SBC and SUB instead.
3692 //
3693 // out.hi = -C
3694 __ Sbc(HighRegisterFrom(out), HighRegisterFrom(out), HighRegisterFrom(out));
3695 // out.hi = out.hi - in.hi
3696 __ Sub(HighRegisterFrom(out), HighRegisterFrom(out), HighRegisterFrom(in));
3697 break;
3698
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003699 case DataType::Type::kFloat32:
3700 case DataType::Type::kFloat64:
Anton Kirilov644032c2016-12-06 17:51:43 +00003701 __ Vneg(OutputVRegister(neg), InputVRegister(neg));
Artem Serov02109dd2016-09-23 17:17:54 +01003702 break;
3703
3704 default:
3705 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
3706 }
3707}
3708
Scott Wakelingfe885462016-09-22 10:24:38 +01003709void LocationsBuilderARMVIXL::VisitTypeConversion(HTypeConversion* conversion) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003710 DataType::Type result_type = conversion->GetResultType();
3711 DataType::Type input_type = conversion->GetInputType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003712 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
3713 << input_type << " -> " << result_type;
Scott Wakelingfe885462016-09-22 10:24:38 +01003714
3715 // The float-to-long, double-to-long and long-to-float type conversions
3716 // rely on a call to the runtime.
3717 LocationSummary::CallKind call_kind =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003718 (((input_type == DataType::Type::kFloat32 || input_type == DataType::Type::kFloat64)
3719 && result_type == DataType::Type::kInt64)
3720 || (input_type == DataType::Type::kInt64 && result_type == DataType::Type::kFloat32))
Scott Wakelingfe885462016-09-22 10:24:38 +01003721 ? LocationSummary::kCallOnMainOnly
3722 : LocationSummary::kNoCall;
3723 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003724 new (GetGraph()->GetAllocator()) LocationSummary(conversion, call_kind);
Scott Wakelingfe885462016-09-22 10:24:38 +01003725
Scott Wakelingfe885462016-09-22 10:24:38 +01003726 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003727 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003728 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003729 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003730 case DataType::Type::kInt16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003731 DCHECK(DataType::IsIntegralType(input_type)) << input_type;
3732 locations->SetInAt(0, Location::RequiresRegister());
3733 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Scott Wakelingfe885462016-09-22 10:24:38 +01003734 break;
3735
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003736 case DataType::Type::kInt32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003737 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003738 case DataType::Type::kInt64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003739 locations->SetInAt(0, Location::Any());
3740 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3741 break;
3742
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003743 case DataType::Type::kFloat32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003744 locations->SetInAt(0, Location::RequiresFpuRegister());
3745 locations->SetOut(Location::RequiresRegister());
3746 locations->AddTemp(Location::RequiresFpuRegister());
3747 break;
3748
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003749 case DataType::Type::kFloat64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003750 locations->SetInAt(0, Location::RequiresFpuRegister());
3751 locations->SetOut(Location::RequiresRegister());
3752 locations->AddTemp(Location::RequiresFpuRegister());
3753 break;
3754
3755 default:
3756 LOG(FATAL) << "Unexpected type conversion from " << input_type
3757 << " to " << result_type;
3758 }
3759 break;
3760
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003761 case DataType::Type::kInt64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003762 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003763 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003764 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003765 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003766 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003767 case DataType::Type::kInt16:
3768 case DataType::Type::kInt32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003769 locations->SetInAt(0, Location::RequiresRegister());
3770 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3771 break;
3772
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003773 case DataType::Type::kFloat32: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003774 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3775 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
3776 locations->SetOut(LocationFrom(r0, r1));
Scott Wakelingfe885462016-09-22 10:24:38 +01003777 break;
3778 }
3779
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003780 case DataType::Type::kFloat64: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003781 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3782 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0),
3783 calling_convention.GetFpuRegisterAt(1)));
3784 locations->SetOut(LocationFrom(r0, r1));
Scott Wakelingfe885462016-09-22 10:24:38 +01003785 break;
3786 }
3787
3788 default:
3789 LOG(FATAL) << "Unexpected type conversion from " << input_type
3790 << " to " << result_type;
3791 }
3792 break;
3793
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003794 case DataType::Type::kFloat32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003795 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003796 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003797 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003798 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003799 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003800 case DataType::Type::kInt16:
3801 case DataType::Type::kInt32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003802 locations->SetInAt(0, Location::RequiresRegister());
3803 locations->SetOut(Location::RequiresFpuRegister());
3804 break;
3805
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003806 case DataType::Type::kInt64: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003807 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3808 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0),
3809 calling_convention.GetRegisterAt(1)));
3810 locations->SetOut(LocationFrom(calling_convention.GetFpuRegisterAt(0)));
Scott Wakelingfe885462016-09-22 10:24:38 +01003811 break;
3812 }
3813
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003814 case DataType::Type::kFloat64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003815 locations->SetInAt(0, Location::RequiresFpuRegister());
3816 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3817 break;
3818
3819 default:
3820 LOG(FATAL) << "Unexpected type conversion from " << input_type
3821 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08003822 }
Scott Wakelingfe885462016-09-22 10:24:38 +01003823 break;
3824
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003825 case DataType::Type::kFloat64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003826 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003827 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003828 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003829 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003830 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003831 case DataType::Type::kInt16:
3832 case DataType::Type::kInt32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003833 locations->SetInAt(0, Location::RequiresRegister());
3834 locations->SetOut(Location::RequiresFpuRegister());
3835 break;
3836
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003837 case DataType::Type::kInt64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003838 locations->SetInAt(0, Location::RequiresRegister());
3839 locations->SetOut(Location::RequiresFpuRegister());
3840 locations->AddTemp(Location::RequiresFpuRegister());
3841 locations->AddTemp(Location::RequiresFpuRegister());
3842 break;
3843
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003844 case DataType::Type::kFloat32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003845 locations->SetInAt(0, Location::RequiresFpuRegister());
3846 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3847 break;
3848
3849 default:
3850 LOG(FATAL) << "Unexpected type conversion from " << input_type
3851 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08003852 }
Scott Wakelingfe885462016-09-22 10:24:38 +01003853 break;
3854
3855 default:
3856 LOG(FATAL) << "Unexpected type conversion from " << input_type
3857 << " to " << result_type;
3858 }
3859}
3860
3861void InstructionCodeGeneratorARMVIXL::VisitTypeConversion(HTypeConversion* conversion) {
3862 LocationSummary* locations = conversion->GetLocations();
3863 Location out = locations->Out();
3864 Location in = locations->InAt(0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003865 DataType::Type result_type = conversion->GetResultType();
3866 DataType::Type input_type = conversion->GetInputType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003867 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
3868 << input_type << " -> " << result_type;
Scott Wakelingfe885462016-09-22 10:24:38 +01003869 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003870 case DataType::Type::kUint8:
Scott Wakelingfe885462016-09-22 10:24:38 +01003871 switch (input_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003872 case DataType::Type::kInt8:
3873 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003874 case DataType::Type::kInt16:
3875 case DataType::Type::kInt32:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003876 __ Ubfx(OutputRegister(conversion), InputRegisterAt(conversion, 0), 0, 8);
3877 break;
3878 case DataType::Type::kInt64:
3879 __ Ubfx(OutputRegister(conversion), LowRegisterFrom(in), 0, 8);
3880 break;
3881
3882 default:
3883 LOG(FATAL) << "Unexpected type conversion from " << input_type
3884 << " to " << result_type;
3885 }
3886 break;
3887
3888 case DataType::Type::kInt8:
3889 switch (input_type) {
3890 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003891 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003892 case DataType::Type::kInt16:
3893 case DataType::Type::kInt32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003894 __ Sbfx(OutputRegister(conversion), InputRegisterAt(conversion, 0), 0, 8);
3895 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003896 case DataType::Type::kInt64:
3897 __ Sbfx(OutputRegister(conversion), LowRegisterFrom(in), 0, 8);
3898 break;
3899
3900 default:
3901 LOG(FATAL) << "Unexpected type conversion from " << input_type
3902 << " to " << result_type;
3903 }
3904 break;
3905
3906 case DataType::Type::kUint16:
3907 switch (input_type) {
3908 case DataType::Type::kInt8:
3909 case DataType::Type::kInt16:
3910 case DataType::Type::kInt32:
3911 __ Ubfx(OutputRegister(conversion), InputRegisterAt(conversion, 0), 0, 16);
3912 break;
3913 case DataType::Type::kInt64:
3914 __ Ubfx(OutputRegister(conversion), LowRegisterFrom(in), 0, 16);
3915 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01003916
3917 default:
3918 LOG(FATAL) << "Unexpected type conversion from " << input_type
3919 << " to " << result_type;
3920 }
3921 break;
3922
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003923 case DataType::Type::kInt16:
Scott Wakelingfe885462016-09-22 10:24:38 +01003924 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003925 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003926 case DataType::Type::kInt32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003927 __ Sbfx(OutputRegister(conversion), InputRegisterAt(conversion, 0), 0, 16);
3928 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003929 case DataType::Type::kInt64:
3930 __ Sbfx(OutputRegister(conversion), LowRegisterFrom(in), 0, 16);
3931 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01003932
3933 default:
3934 LOG(FATAL) << "Unexpected type conversion from " << input_type
3935 << " to " << result_type;
3936 }
3937 break;
3938
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003939 case DataType::Type::kInt32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003940 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003941 case DataType::Type::kInt64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003942 DCHECK(out.IsRegister());
3943 if (in.IsRegisterPair()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003944 __ Mov(OutputRegister(conversion), LowRegisterFrom(in));
Scott Wakelingfe885462016-09-22 10:24:38 +01003945 } else if (in.IsDoubleStackSlot()) {
3946 GetAssembler()->LoadFromOffset(kLoadWord,
3947 OutputRegister(conversion),
3948 sp,
3949 in.GetStackIndex());
3950 } else {
3951 DCHECK(in.IsConstant());
3952 DCHECK(in.GetConstant()->IsLongConstant());
Vladimir Markoba1a48e2017-04-13 11:50:14 +01003953 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
3954 __ Mov(OutputRegister(conversion), static_cast<int32_t>(value));
Scott Wakelingfe885462016-09-22 10:24:38 +01003955 }
3956 break;
3957
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003958 case DataType::Type::kFloat32: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003959 vixl32::SRegister temp = LowSRegisterFrom(locations->GetTemp(0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01003960 __ Vcvt(S32, F32, temp, InputSRegisterAt(conversion, 0));
Scott Wakelingfe885462016-09-22 10:24:38 +01003961 __ Vmov(OutputRegister(conversion), temp);
3962 break;
3963 }
3964
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003965 case DataType::Type::kFloat64: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003966 vixl32::SRegister temp_s = LowSRegisterFrom(locations->GetTemp(0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01003967 __ Vcvt(S32, F64, temp_s, DRegisterFrom(in));
Scott Wakelingfe885462016-09-22 10:24:38 +01003968 __ Vmov(OutputRegister(conversion), temp_s);
3969 break;
3970 }
3971
3972 default:
3973 LOG(FATAL) << "Unexpected type conversion from " << input_type
3974 << " to " << result_type;
3975 }
3976 break;
3977
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003978 case DataType::Type::kInt64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003979 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003980 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003981 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003982 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003983 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003984 case DataType::Type::kInt16:
3985 case DataType::Type::kInt32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003986 DCHECK(out.IsRegisterPair());
3987 DCHECK(in.IsRegister());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003988 __ Mov(LowRegisterFrom(out), InputRegisterAt(conversion, 0));
Scott Wakelingfe885462016-09-22 10:24:38 +01003989 // Sign extension.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003990 __ Asr(HighRegisterFrom(out), LowRegisterFrom(out), 31);
Scott Wakelingfe885462016-09-22 10:24:38 +01003991 break;
3992
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003993 case DataType::Type::kFloat32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003994 codegen_->InvokeRuntime(kQuickF2l, conversion, conversion->GetDexPc());
3995 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
3996 break;
3997
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003998 case DataType::Type::kFloat64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003999 codegen_->InvokeRuntime(kQuickD2l, conversion, conversion->GetDexPc());
4000 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
4001 break;
4002
4003 default:
4004 LOG(FATAL) << "Unexpected type conversion from " << input_type
4005 << " to " << result_type;
4006 }
4007 break;
4008
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004009 case DataType::Type::kFloat32:
Scott Wakelingfe885462016-09-22 10:24:38 +01004010 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004011 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004012 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004013 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004014 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004015 case DataType::Type::kInt16:
4016 case DataType::Type::kInt32:
Scott Wakelingfe885462016-09-22 10:24:38 +01004017 __ Vmov(OutputSRegister(conversion), InputRegisterAt(conversion, 0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01004018 __ Vcvt(F32, S32, OutputSRegister(conversion), OutputSRegister(conversion));
Scott Wakelingfe885462016-09-22 10:24:38 +01004019 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01004020
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004021 case DataType::Type::kInt64:
Scott Wakelingfe885462016-09-22 10:24:38 +01004022 codegen_->InvokeRuntime(kQuickL2f, conversion, conversion->GetDexPc());
4023 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
4024 break;
4025
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004026 case DataType::Type::kFloat64:
Scott Wakelingc34dba72016-10-03 10:14:44 +01004027 __ Vcvt(F32, F64, OutputSRegister(conversion), DRegisterFrom(in));
Scott Wakelingfe885462016-09-22 10:24:38 +01004028 break;
4029
4030 default:
4031 LOG(FATAL) << "Unexpected type conversion from " << input_type
4032 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08004033 }
Scott Wakelingfe885462016-09-22 10:24:38 +01004034 break;
4035
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004036 case DataType::Type::kFloat64:
Scott Wakelingfe885462016-09-22 10:24:38 +01004037 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004038 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004039 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004040 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004041 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004042 case DataType::Type::kInt16:
4043 case DataType::Type::kInt32:
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004044 __ Vmov(LowSRegisterFrom(out), InputRegisterAt(conversion, 0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01004045 __ Vcvt(F64, S32, DRegisterFrom(out), LowSRegisterFrom(out));
Scott Wakelingfe885462016-09-22 10:24:38 +01004046 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01004047
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004048 case DataType::Type::kInt64: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004049 vixl32::Register low = LowRegisterFrom(in);
4050 vixl32::Register high = HighRegisterFrom(in);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004051 vixl32::SRegister out_s = LowSRegisterFrom(out);
Scott Wakelingc34dba72016-10-03 10:14:44 +01004052 vixl32::DRegister out_d = DRegisterFrom(out);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004053 vixl32::SRegister temp_s = LowSRegisterFrom(locations->GetTemp(0));
Scott Wakelingc34dba72016-10-03 10:14:44 +01004054 vixl32::DRegister temp_d = DRegisterFrom(locations->GetTemp(0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01004055 vixl32::DRegister constant_d = DRegisterFrom(locations->GetTemp(1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004056
4057 // temp_d = int-to-double(high)
4058 __ Vmov(temp_s, high);
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01004059 __ Vcvt(F64, S32, temp_d, temp_s);
Scott Wakelingfe885462016-09-22 10:24:38 +01004060 // constant_d = k2Pow32EncodingForDouble
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004061 __ Vmov(constant_d, bit_cast<double, int64_t>(k2Pow32EncodingForDouble));
Scott Wakelingfe885462016-09-22 10:24:38 +01004062 // out_d = unsigned-to-double(low)
4063 __ Vmov(out_s, low);
4064 __ Vcvt(F64, U32, out_d, out_s);
4065 // out_d += temp_d * constant_d
4066 __ Vmla(F64, out_d, temp_d, constant_d);
4067 break;
4068 }
4069
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004070 case DataType::Type::kFloat32:
Scott Wakelingc34dba72016-10-03 10:14:44 +01004071 __ Vcvt(F64, F32, DRegisterFrom(out), InputSRegisterAt(conversion, 0));
Scott Wakelingfe885462016-09-22 10:24:38 +01004072 break;
4073
4074 default:
4075 LOG(FATAL) << "Unexpected type conversion from " << input_type
4076 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08004077 }
Scott Wakelingfe885462016-09-22 10:24:38 +01004078 break;
4079
4080 default:
4081 LOG(FATAL) << "Unexpected type conversion from " << input_type
4082 << " to " << result_type;
4083 }
4084}
4085
4086void LocationsBuilderARMVIXL::VisitAdd(HAdd* add) {
4087 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004088 new (GetGraph()->GetAllocator()) LocationSummary(add, LocationSummary::kNoCall);
Scott Wakelingfe885462016-09-22 10:24:38 +01004089 switch (add->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004090 case DataType::Type::kInt32: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004091 locations->SetInAt(0, Location::RequiresRegister());
4092 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
4093 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4094 break;
4095 }
4096
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004097 case DataType::Type::kInt64: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004098 locations->SetInAt(0, Location::RequiresRegister());
Anton Kirilovdda43962016-11-21 19:55:20 +00004099 locations->SetInAt(1, ArmEncodableConstantOrRegister(add->InputAt(1), ADD));
Scott Wakelingfe885462016-09-22 10:24:38 +01004100 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4101 break;
4102 }
4103
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004104 case DataType::Type::kFloat32:
4105 case DataType::Type::kFloat64: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004106 locations->SetInAt(0, Location::RequiresFpuRegister());
4107 locations->SetInAt(1, Location::RequiresFpuRegister());
4108 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4109 break;
4110 }
4111
4112 default:
4113 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
4114 }
4115}
4116
4117void InstructionCodeGeneratorARMVIXL::VisitAdd(HAdd* add) {
4118 LocationSummary* locations = add->GetLocations();
4119 Location out = locations->Out();
4120 Location first = locations->InAt(0);
4121 Location second = locations->InAt(1);
4122
4123 switch (add->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004124 case DataType::Type::kInt32: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004125 __ Add(OutputRegister(add), InputRegisterAt(add, 0), InputOperandAt(add, 1));
4126 }
4127 break;
4128
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004129 case DataType::Type::kInt64: {
Anton Kirilovdda43962016-11-21 19:55:20 +00004130 if (second.IsConstant()) {
4131 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
4132 GenerateAddLongConst(out, first, value);
4133 } else {
4134 DCHECK(second.IsRegisterPair());
4135 __ Adds(LowRegisterFrom(out), LowRegisterFrom(first), LowRegisterFrom(second));
4136 __ Adc(HighRegisterFrom(out), HighRegisterFrom(first), HighRegisterFrom(second));
4137 }
Scott Wakelingfe885462016-09-22 10:24:38 +01004138 break;
4139 }
4140
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004141 case DataType::Type::kFloat32:
4142 case DataType::Type::kFloat64:
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004143 __ Vadd(OutputVRegister(add), InputVRegisterAt(add, 0), InputVRegisterAt(add, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004144 break;
4145
4146 default:
4147 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
4148 }
4149}
4150
4151void LocationsBuilderARMVIXL::VisitSub(HSub* sub) {
4152 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004153 new (GetGraph()->GetAllocator()) LocationSummary(sub, LocationSummary::kNoCall);
Scott Wakelingfe885462016-09-22 10:24:38 +01004154 switch (sub->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004155 case DataType::Type::kInt32: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004156 locations->SetInAt(0, Location::RequiresRegister());
4157 locations->SetInAt(1, Location::RegisterOrConstant(sub->InputAt(1)));
4158 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4159 break;
4160 }
4161
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004162 case DataType::Type::kInt64: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004163 locations->SetInAt(0, Location::RequiresRegister());
Anton Kirilovdda43962016-11-21 19:55:20 +00004164 locations->SetInAt(1, ArmEncodableConstantOrRegister(sub->InputAt(1), SUB));
Scott Wakelingfe885462016-09-22 10:24:38 +01004165 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4166 break;
4167 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004168 case DataType::Type::kFloat32:
4169 case DataType::Type::kFloat64: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004170 locations->SetInAt(0, Location::RequiresFpuRegister());
4171 locations->SetInAt(1, Location::RequiresFpuRegister());
4172 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4173 break;
4174 }
4175 default:
4176 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
4177 }
4178}
4179
4180void InstructionCodeGeneratorARMVIXL::VisitSub(HSub* sub) {
4181 LocationSummary* locations = sub->GetLocations();
4182 Location out = locations->Out();
4183 Location first = locations->InAt(0);
4184 Location second = locations->InAt(1);
4185 switch (sub->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004186 case DataType::Type::kInt32: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004187 __ Sub(OutputRegister(sub), InputRegisterAt(sub, 0), InputOperandAt(sub, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004188 break;
4189 }
4190
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004191 case DataType::Type::kInt64: {
Anton Kirilovdda43962016-11-21 19:55:20 +00004192 if (second.IsConstant()) {
4193 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
4194 GenerateAddLongConst(out, first, -value);
4195 } else {
4196 DCHECK(second.IsRegisterPair());
4197 __ Subs(LowRegisterFrom(out), LowRegisterFrom(first), LowRegisterFrom(second));
4198 __ Sbc(HighRegisterFrom(out), HighRegisterFrom(first), HighRegisterFrom(second));
4199 }
Scott Wakelingfe885462016-09-22 10:24:38 +01004200 break;
4201 }
4202
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004203 case DataType::Type::kFloat32:
4204 case DataType::Type::kFloat64:
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004205 __ Vsub(OutputVRegister(sub), InputVRegisterAt(sub, 0), InputVRegisterAt(sub, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004206 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01004207
4208 default:
4209 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
4210 }
4211}
4212
4213void LocationsBuilderARMVIXL::VisitMul(HMul* mul) {
4214 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004215 new (GetGraph()->GetAllocator()) LocationSummary(mul, LocationSummary::kNoCall);
Scott Wakelingfe885462016-09-22 10:24:38 +01004216 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004217 case DataType::Type::kInt32:
4218 case DataType::Type::kInt64: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004219 locations->SetInAt(0, Location::RequiresRegister());
4220 locations->SetInAt(1, Location::RequiresRegister());
4221 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4222 break;
4223 }
4224
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004225 case DataType::Type::kFloat32:
4226 case DataType::Type::kFloat64: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004227 locations->SetInAt(0, Location::RequiresFpuRegister());
4228 locations->SetInAt(1, Location::RequiresFpuRegister());
4229 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4230 break;
4231 }
4232
4233 default:
4234 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4235 }
4236}
4237
4238void InstructionCodeGeneratorARMVIXL::VisitMul(HMul* mul) {
4239 LocationSummary* locations = mul->GetLocations();
4240 Location out = locations->Out();
4241 Location first = locations->InAt(0);
4242 Location second = locations->InAt(1);
4243 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004244 case DataType::Type::kInt32: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004245 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
4246 break;
4247 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004248 case DataType::Type::kInt64: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004249 vixl32::Register out_hi = HighRegisterFrom(out);
4250 vixl32::Register out_lo = LowRegisterFrom(out);
4251 vixl32::Register in1_hi = HighRegisterFrom(first);
4252 vixl32::Register in1_lo = LowRegisterFrom(first);
4253 vixl32::Register in2_hi = HighRegisterFrom(second);
4254 vixl32::Register in2_lo = LowRegisterFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01004255
4256 // Extra checks to protect caused by the existence of R1_R2.
4257 // The algorithm is wrong if out.hi is either in1.lo or in2.lo:
4258 // (e.g. in1=r0_r1, in2=r2_r3 and out=r1_r2);
Anton Kirilov644032c2016-12-06 17:51:43 +00004259 DCHECK(!out_hi.Is(in1_lo));
4260 DCHECK(!out_hi.Is(in2_lo));
Scott Wakelingfe885462016-09-22 10:24:38 +01004261
4262 // input: in1 - 64 bits, in2 - 64 bits
4263 // output: out
4264 // formula: out.hi : out.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
4265 // parts: out.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
4266 // parts: out.lo = (in1.lo * in2.lo)[31:0]
4267
4268 UseScratchRegisterScope temps(GetVIXLAssembler());
4269 vixl32::Register temp = temps.Acquire();
4270 // temp <- in1.lo * in2.hi
4271 __ Mul(temp, in1_lo, in2_hi);
4272 // out.hi <- in1.lo * in2.hi + in1.hi * in2.lo
4273 __ Mla(out_hi, in1_hi, in2_lo, temp);
4274 // out.lo <- (in1.lo * in2.lo)[31:0];
4275 __ Umull(out_lo, temp, in1_lo, in2_lo);
4276 // out.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004277 __ Add(out_hi, out_hi, temp);
Scott Wakelingfe885462016-09-22 10:24:38 +01004278 break;
4279 }
4280
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004281 case DataType::Type::kFloat32:
4282 case DataType::Type::kFloat64:
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004283 __ Vmul(OutputVRegister(mul), InputVRegisterAt(mul, 0), InputVRegisterAt(mul, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004284 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01004285
4286 default:
4287 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4288 }
4289}
4290
Scott Wakelingfe885462016-09-22 10:24:38 +01004291void InstructionCodeGeneratorARMVIXL::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
4292 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004293 DCHECK(instruction->GetResultType() == DataType::Type::kInt32);
Scott Wakelingfe885462016-09-22 10:24:38 +01004294
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004295 Location second = instruction->GetLocations()->InAt(1);
Scott Wakelingfe885462016-09-22 10:24:38 +01004296 DCHECK(second.IsConstant());
4297
4298 vixl32::Register out = OutputRegister(instruction);
4299 vixl32::Register dividend = InputRegisterAt(instruction, 0);
Anton Kirilov644032c2016-12-06 17:51:43 +00004300 int32_t imm = Int32ConstantFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01004301 DCHECK(imm == 1 || imm == -1);
4302
4303 if (instruction->IsRem()) {
4304 __ Mov(out, 0);
4305 } else {
4306 if (imm == 1) {
4307 __ Mov(out, dividend);
4308 } else {
4309 __ Rsb(out, dividend, 0);
4310 }
4311 }
4312}
4313
4314void InstructionCodeGeneratorARMVIXL::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
4315 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004316 DCHECK(instruction->GetResultType() == DataType::Type::kInt32);
Scott Wakelingfe885462016-09-22 10:24:38 +01004317
4318 LocationSummary* locations = instruction->GetLocations();
4319 Location second = locations->InAt(1);
4320 DCHECK(second.IsConstant());
4321
4322 vixl32::Register out = OutputRegister(instruction);
4323 vixl32::Register dividend = InputRegisterAt(instruction, 0);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004324 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
Anton Kirilov644032c2016-12-06 17:51:43 +00004325 int32_t imm = Int32ConstantFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01004326 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
4327 int ctz_imm = CTZ(abs_imm);
4328
4329 if (ctz_imm == 1) {
4330 __ Lsr(temp, dividend, 32 - ctz_imm);
4331 } else {
4332 __ Asr(temp, dividend, 31);
4333 __ Lsr(temp, temp, 32 - ctz_imm);
4334 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004335 __ Add(out, temp, dividend);
Scott Wakelingfe885462016-09-22 10:24:38 +01004336
4337 if (instruction->IsDiv()) {
4338 __ Asr(out, out, ctz_imm);
4339 if (imm < 0) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004340 __ Rsb(out, out, 0);
Scott Wakelingfe885462016-09-22 10:24:38 +01004341 }
4342 } else {
4343 __ Ubfx(out, out, 0, ctz_imm);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004344 __ Sub(out, out, temp);
Scott Wakelingfe885462016-09-22 10:24:38 +01004345 }
4346}
4347
4348void InstructionCodeGeneratorARMVIXL::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
4349 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004350 DCHECK(instruction->GetResultType() == DataType::Type::kInt32);
Scott Wakelingfe885462016-09-22 10:24:38 +01004351
4352 LocationSummary* locations = instruction->GetLocations();
4353 Location second = locations->InAt(1);
4354 DCHECK(second.IsConstant());
4355
4356 vixl32::Register out = OutputRegister(instruction);
4357 vixl32::Register dividend = InputRegisterAt(instruction, 0);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004358 vixl32::Register temp1 = RegisterFrom(locations->GetTemp(0));
4359 vixl32::Register temp2 = RegisterFrom(locations->GetTemp(1));
Scott Wakelingb77051e2016-11-21 19:46:00 +00004360 int32_t imm = Int32ConstantFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01004361
4362 int64_t magic;
4363 int shift;
4364 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
4365
Anton Kirilovdda43962016-11-21 19:55:20 +00004366 // TODO(VIXL): Change the static cast to Operand::From() after VIXL is fixed.
4367 __ Mov(temp1, static_cast<int32_t>(magic));
Scott Wakelingfe885462016-09-22 10:24:38 +01004368 __ Smull(temp2, temp1, dividend, temp1);
4369
4370 if (imm > 0 && magic < 0) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004371 __ Add(temp1, temp1, dividend);
Scott Wakelingfe885462016-09-22 10:24:38 +01004372 } else if (imm < 0 && magic > 0) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004373 __ Sub(temp1, temp1, dividend);
Scott Wakelingfe885462016-09-22 10:24:38 +01004374 }
4375
4376 if (shift != 0) {
4377 __ Asr(temp1, temp1, shift);
4378 }
4379
4380 if (instruction->IsDiv()) {
4381 __ Sub(out, temp1, Operand(temp1, vixl32::Shift(ASR), 31));
4382 } else {
4383 __ Sub(temp1, temp1, Operand(temp1, vixl32::Shift(ASR), 31));
4384 // TODO: Strength reduction for mls.
4385 __ Mov(temp2, imm);
4386 __ Mls(out, temp1, temp2, dividend);
4387 }
4388}
4389
4390void InstructionCodeGeneratorARMVIXL::GenerateDivRemConstantIntegral(
4391 HBinaryOperation* instruction) {
4392 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004393 DCHECK(instruction->GetResultType() == DataType::Type::kInt32);
Scott Wakelingfe885462016-09-22 10:24:38 +01004394
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004395 Location second = instruction->GetLocations()->InAt(1);
Scott Wakelingfe885462016-09-22 10:24:38 +01004396 DCHECK(second.IsConstant());
4397
Anton Kirilov644032c2016-12-06 17:51:43 +00004398 int32_t imm = Int32ConstantFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01004399 if (imm == 0) {
4400 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
4401 } else if (imm == 1 || imm == -1) {
4402 DivRemOneOrMinusOne(instruction);
4403 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
4404 DivRemByPowerOfTwo(instruction);
4405 } else {
4406 DCHECK(imm <= -2 || imm >= 2);
4407 GenerateDivRemWithAnyConstant(instruction);
4408 }
4409}
4410
4411void LocationsBuilderARMVIXL::VisitDiv(HDiv* div) {
4412 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004413 if (div->GetResultType() == DataType::Type::kInt64) {
Scott Wakelingfe885462016-09-22 10:24:38 +01004414 // pLdiv runtime call.
4415 call_kind = LocationSummary::kCallOnMainOnly;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004416 } else if (div->GetResultType() == DataType::Type::kInt32 && div->InputAt(1)->IsConstant()) {
Scott Wakelingfe885462016-09-22 10:24:38 +01004417 // sdiv will be replaced by other instruction sequence.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004418 } else if (div->GetResultType() == DataType::Type::kInt32 &&
Scott Wakelingfe885462016-09-22 10:24:38 +01004419 !codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4420 // pIdivmod runtime call.
4421 call_kind = LocationSummary::kCallOnMainOnly;
4422 }
4423
Vladimir Markoca6fff82017-10-03 14:49:14 +01004424 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(div, call_kind);
Scott Wakelingfe885462016-09-22 10:24:38 +01004425
4426 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004427 case DataType::Type::kInt32: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004428 if (div->InputAt(1)->IsConstant()) {
4429 locations->SetInAt(0, Location::RequiresRegister());
4430 locations->SetInAt(1, Location::ConstantLocation(div->InputAt(1)->AsConstant()));
4431 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Anton Kirilov644032c2016-12-06 17:51:43 +00004432 int32_t value = Int32ConstantFrom(div->InputAt(1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004433 if (value == 1 || value == 0 || value == -1) {
4434 // No temp register required.
4435 } else {
4436 locations->AddTemp(Location::RequiresRegister());
4437 if (!IsPowerOfTwo(AbsOrMin(value))) {
4438 locations->AddTemp(Location::RequiresRegister());
4439 }
4440 }
4441 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4442 locations->SetInAt(0, Location::RequiresRegister());
4443 locations->SetInAt(1, Location::RequiresRegister());
4444 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4445 } else {
Artem Serov551b28f2016-10-18 19:11:30 +01004446 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4447 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4448 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
Roland Levillain5e8d5f02016-10-18 18:03:43 +01004449 // Note: divmod will compute both the quotient and the remainder as the pair R0 and R1, but
Artem Serov551b28f2016-10-18 19:11:30 +01004450 // we only need the former.
4451 locations->SetOut(LocationFrom(r0));
Scott Wakelingfe885462016-09-22 10:24:38 +01004452 }
4453 break;
4454 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004455 case DataType::Type::kInt64: {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01004456 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4457 locations->SetInAt(0, LocationFrom(
4458 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
4459 locations->SetInAt(1, LocationFrom(
4460 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
4461 locations->SetOut(LocationFrom(r0, r1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004462 break;
4463 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004464 case DataType::Type::kFloat32:
4465 case DataType::Type::kFloat64: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004466 locations->SetInAt(0, Location::RequiresFpuRegister());
4467 locations->SetInAt(1, Location::RequiresFpuRegister());
4468 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4469 break;
4470 }
4471
4472 default:
4473 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
4474 }
4475}
4476
4477void InstructionCodeGeneratorARMVIXL::VisitDiv(HDiv* div) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01004478 Location lhs = div->GetLocations()->InAt(0);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004479 Location rhs = div->GetLocations()->InAt(1);
Scott Wakelingfe885462016-09-22 10:24:38 +01004480
4481 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004482 case DataType::Type::kInt32: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004483 if (rhs.IsConstant()) {
Scott Wakelingfe885462016-09-22 10:24:38 +01004484 GenerateDivRemConstantIntegral(div);
4485 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4486 __ Sdiv(OutputRegister(div), InputRegisterAt(div, 0), InputRegisterAt(div, 1));
4487 } else {
Artem Serov551b28f2016-10-18 19:11:30 +01004488 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4489 DCHECK(calling_convention.GetRegisterAt(0).Is(RegisterFrom(lhs)));
4490 DCHECK(calling_convention.GetRegisterAt(1).Is(RegisterFrom(rhs)));
4491 DCHECK(r0.Is(OutputRegister(div)));
4492
4493 codegen_->InvokeRuntime(kQuickIdivmod, div, div->GetDexPc());
4494 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
Scott Wakelingfe885462016-09-22 10:24:38 +01004495 }
4496 break;
4497 }
4498
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004499 case DataType::Type::kInt64: {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01004500 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4501 DCHECK(calling_convention.GetRegisterAt(0).Is(LowRegisterFrom(lhs)));
4502 DCHECK(calling_convention.GetRegisterAt(1).Is(HighRegisterFrom(lhs)));
4503 DCHECK(calling_convention.GetRegisterAt(2).Is(LowRegisterFrom(rhs)));
4504 DCHECK(calling_convention.GetRegisterAt(3).Is(HighRegisterFrom(rhs)));
4505 DCHECK(LowRegisterFrom(div->GetLocations()->Out()).Is(r0));
4506 DCHECK(HighRegisterFrom(div->GetLocations()->Out()).Is(r1));
4507
4508 codegen_->InvokeRuntime(kQuickLdiv, div, div->GetDexPc());
4509 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Scott Wakelingfe885462016-09-22 10:24:38 +01004510 break;
4511 }
4512
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004513 case DataType::Type::kFloat32:
4514 case DataType::Type::kFloat64:
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004515 __ Vdiv(OutputVRegister(div), InputVRegisterAt(div, 0), InputVRegisterAt(div, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004516 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01004517
4518 default:
4519 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
4520 }
4521}
4522
Artem Serov551b28f2016-10-18 19:11:30 +01004523void LocationsBuilderARMVIXL::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004524 DataType::Type type = rem->GetResultType();
Artem Serov551b28f2016-10-18 19:11:30 +01004525
4526 // Most remainders are implemented in the runtime.
4527 LocationSummary::CallKind call_kind = LocationSummary::kCallOnMainOnly;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004528 if (rem->GetResultType() == DataType::Type::kInt32 && rem->InputAt(1)->IsConstant()) {
Artem Serov551b28f2016-10-18 19:11:30 +01004529 // sdiv will be replaced by other instruction sequence.
4530 call_kind = LocationSummary::kNoCall;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004531 } else if ((rem->GetResultType() == DataType::Type::kInt32)
Artem Serov551b28f2016-10-18 19:11:30 +01004532 && codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4533 // Have hardware divide instruction for int, do it with three instructions.
4534 call_kind = LocationSummary::kNoCall;
4535 }
4536
Vladimir Markoca6fff82017-10-03 14:49:14 +01004537 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(rem, call_kind);
Artem Serov551b28f2016-10-18 19:11:30 +01004538
4539 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004540 case DataType::Type::kInt32: {
Artem Serov551b28f2016-10-18 19:11:30 +01004541 if (rem->InputAt(1)->IsConstant()) {
4542 locations->SetInAt(0, Location::RequiresRegister());
4543 locations->SetInAt(1, Location::ConstantLocation(rem->InputAt(1)->AsConstant()));
4544 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Anton Kirilov644032c2016-12-06 17:51:43 +00004545 int32_t value = Int32ConstantFrom(rem->InputAt(1));
Artem Serov551b28f2016-10-18 19:11:30 +01004546 if (value == 1 || value == 0 || value == -1) {
4547 // No temp register required.
4548 } else {
4549 locations->AddTemp(Location::RequiresRegister());
4550 if (!IsPowerOfTwo(AbsOrMin(value))) {
4551 locations->AddTemp(Location::RequiresRegister());
4552 }
4553 }
4554 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4555 locations->SetInAt(0, Location::RequiresRegister());
4556 locations->SetInAt(1, Location::RequiresRegister());
4557 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4558 locations->AddTemp(Location::RequiresRegister());
4559 } else {
4560 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4561 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4562 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
Roland Levillain5e8d5f02016-10-18 18:03:43 +01004563 // Note: divmod will compute both the quotient and the remainder as the pair R0 and R1, but
Artem Serov551b28f2016-10-18 19:11:30 +01004564 // we only need the latter.
4565 locations->SetOut(LocationFrom(r1));
4566 }
4567 break;
4568 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004569 case DataType::Type::kInt64: {
Artem Serov551b28f2016-10-18 19:11:30 +01004570 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4571 locations->SetInAt(0, LocationFrom(
4572 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
4573 locations->SetInAt(1, LocationFrom(
4574 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
4575 // The runtime helper puts the output in R2,R3.
4576 locations->SetOut(LocationFrom(r2, r3));
4577 break;
4578 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004579 case DataType::Type::kFloat32: {
Artem Serov551b28f2016-10-18 19:11:30 +01004580 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4581 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
4582 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
4583 locations->SetOut(LocationFrom(s0));
4584 break;
4585 }
4586
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004587 case DataType::Type::kFloat64: {
Artem Serov551b28f2016-10-18 19:11:30 +01004588 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4589 locations->SetInAt(0, LocationFrom(
4590 calling_convention.GetFpuRegisterAt(0), calling_convention.GetFpuRegisterAt(1)));
4591 locations->SetInAt(1, LocationFrom(
4592 calling_convention.GetFpuRegisterAt(2), calling_convention.GetFpuRegisterAt(3)));
4593 locations->SetOut(LocationFrom(s0, s1));
4594 break;
4595 }
4596
4597 default:
4598 LOG(FATAL) << "Unexpected rem type " << type;
4599 }
4600}
4601
4602void InstructionCodeGeneratorARMVIXL::VisitRem(HRem* rem) {
4603 LocationSummary* locations = rem->GetLocations();
4604 Location second = locations->InAt(1);
4605
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004606 DataType::Type type = rem->GetResultType();
Artem Serov551b28f2016-10-18 19:11:30 +01004607 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004608 case DataType::Type::kInt32: {
Artem Serov551b28f2016-10-18 19:11:30 +01004609 vixl32::Register reg1 = InputRegisterAt(rem, 0);
4610 vixl32::Register out_reg = OutputRegister(rem);
4611 if (second.IsConstant()) {
4612 GenerateDivRemConstantIntegral(rem);
4613 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4614 vixl32::Register reg2 = RegisterFrom(second);
4615 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
4616
4617 // temp = reg1 / reg2 (integer division)
4618 // dest = reg1 - temp * reg2
4619 __ Sdiv(temp, reg1, reg2);
4620 __ Mls(out_reg, temp, reg2, reg1);
4621 } else {
4622 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4623 DCHECK(reg1.Is(calling_convention.GetRegisterAt(0)));
4624 DCHECK(RegisterFrom(second).Is(calling_convention.GetRegisterAt(1)));
4625 DCHECK(out_reg.Is(r1));
4626
4627 codegen_->InvokeRuntime(kQuickIdivmod, rem, rem->GetDexPc());
4628 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
4629 }
4630 break;
4631 }
4632
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004633 case DataType::Type::kInt64: {
Artem Serov551b28f2016-10-18 19:11:30 +01004634 codegen_->InvokeRuntime(kQuickLmod, rem, rem->GetDexPc());
4635 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
4636 break;
4637 }
4638
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004639 case DataType::Type::kFloat32: {
Artem Serov551b28f2016-10-18 19:11:30 +01004640 codegen_->InvokeRuntime(kQuickFmodf, rem, rem->GetDexPc());
4641 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
4642 break;
4643 }
4644
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004645 case DataType::Type::kFloat64: {
Artem Serov551b28f2016-10-18 19:11:30 +01004646 codegen_->InvokeRuntime(kQuickFmod, rem, rem->GetDexPc());
4647 CheckEntrypointTypes<kQuickFmod, double, double, double>();
4648 break;
4649 }
4650
4651 default:
4652 LOG(FATAL) << "Unexpected rem type " << type;
4653 }
4654}
4655
4656
Scott Wakelingfe885462016-09-22 10:24:38 +01004657void LocationsBuilderARMVIXL::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Artem Serov657022c2016-11-23 14:19:38 +00004658 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Scott Wakelingfe885462016-09-22 10:24:38 +01004659 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Scott Wakelingfe885462016-09-22 10:24:38 +01004660}
4661
4662void InstructionCodeGeneratorARMVIXL::VisitDivZeroCheck(HDivZeroCheck* instruction) {
4663 DivZeroCheckSlowPathARMVIXL* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01004664 new (codegen_->GetScopedAllocator()) DivZeroCheckSlowPathARMVIXL(instruction);
Scott Wakelingfe885462016-09-22 10:24:38 +01004665 codegen_->AddSlowPath(slow_path);
4666
4667 LocationSummary* locations = instruction->GetLocations();
4668 Location value = locations->InAt(0);
4669
4670 switch (instruction->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004671 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004672 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004673 case DataType::Type::kInt8:
4674 case DataType::Type::kUint16:
4675 case DataType::Type::kInt16:
4676 case DataType::Type::kInt32: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004677 if (value.IsRegister()) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00004678 __ CompareAndBranchIfZero(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
Scott Wakelingfe885462016-09-22 10:24:38 +01004679 } else {
4680 DCHECK(value.IsConstant()) << value;
Anton Kirilov644032c2016-12-06 17:51:43 +00004681 if (Int32ConstantFrom(value) == 0) {
Scott Wakelingfe885462016-09-22 10:24:38 +01004682 __ B(slow_path->GetEntryLabel());
4683 }
4684 }
4685 break;
4686 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004687 case DataType::Type::kInt64: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004688 if (value.IsRegisterPair()) {
4689 UseScratchRegisterScope temps(GetVIXLAssembler());
4690 vixl32::Register temp = temps.Acquire();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004691 __ Orrs(temp, LowRegisterFrom(value), HighRegisterFrom(value));
Scott Wakelingfe885462016-09-22 10:24:38 +01004692 __ B(eq, slow_path->GetEntryLabel());
4693 } else {
4694 DCHECK(value.IsConstant()) << value;
Anton Kirilov644032c2016-12-06 17:51:43 +00004695 if (Int64ConstantFrom(value) == 0) {
Scott Wakelingfe885462016-09-22 10:24:38 +01004696 __ B(slow_path->GetEntryLabel());
4697 }
4698 }
4699 break;
4700 }
4701 default:
4702 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
4703 }
4704}
4705
Artem Serov02109dd2016-09-23 17:17:54 +01004706void InstructionCodeGeneratorARMVIXL::HandleIntegerRotate(HRor* ror) {
4707 LocationSummary* locations = ror->GetLocations();
4708 vixl32::Register in = InputRegisterAt(ror, 0);
4709 Location rhs = locations->InAt(1);
4710 vixl32::Register out = OutputRegister(ror);
4711
4712 if (rhs.IsConstant()) {
4713 // Arm32 and Thumb2 assemblers require a rotation on the interval [1,31],
4714 // so map all rotations to a +ve. equivalent in that range.
4715 // (e.g. left *or* right by -2 bits == 30 bits in the same direction.)
4716 uint32_t rot = CodeGenerator::GetInt32ValueOf(rhs.GetConstant()) & 0x1F;
4717 if (rot) {
4718 // Rotate, mapping left rotations to right equivalents if necessary.
4719 // (e.g. left by 2 bits == right by 30.)
4720 __ Ror(out, in, rot);
4721 } else if (!out.Is(in)) {
4722 __ Mov(out, in);
4723 }
4724 } else {
4725 __ Ror(out, in, RegisterFrom(rhs));
4726 }
4727}
4728
4729// Gain some speed by mapping all Long rotates onto equivalent pairs of Integer
4730// rotates by swapping input regs (effectively rotating by the first 32-bits of
4731// a larger rotation) or flipping direction (thus treating larger right/left
4732// rotations as sub-word sized rotations in the other direction) as appropriate.
4733void InstructionCodeGeneratorARMVIXL::HandleLongRotate(HRor* ror) {
4734 LocationSummary* locations = ror->GetLocations();
4735 vixl32::Register in_reg_lo = LowRegisterFrom(locations->InAt(0));
4736 vixl32::Register in_reg_hi = HighRegisterFrom(locations->InAt(0));
4737 Location rhs = locations->InAt(1);
4738 vixl32::Register out_reg_lo = LowRegisterFrom(locations->Out());
4739 vixl32::Register out_reg_hi = HighRegisterFrom(locations->Out());
4740
4741 if (rhs.IsConstant()) {
4742 uint64_t rot = CodeGenerator::GetInt64ValueOf(rhs.GetConstant());
4743 // Map all rotations to +ve. equivalents on the interval [0,63].
4744 rot &= kMaxLongShiftDistance;
4745 // For rotates over a word in size, 'pre-rotate' by 32-bits to keep rotate
4746 // logic below to a simple pair of binary orr.
4747 // (e.g. 34 bits == in_reg swap + 2 bits right.)
4748 if (rot >= kArmBitsPerWord) {
4749 rot -= kArmBitsPerWord;
4750 std::swap(in_reg_hi, in_reg_lo);
4751 }
4752 // Rotate, or mov to out for zero or word size rotations.
4753 if (rot != 0u) {
Scott Wakelingb77051e2016-11-21 19:46:00 +00004754 __ Lsr(out_reg_hi, in_reg_hi, Operand::From(rot));
Artem Serov02109dd2016-09-23 17:17:54 +01004755 __ Orr(out_reg_hi, out_reg_hi, Operand(in_reg_lo, ShiftType::LSL, kArmBitsPerWord - rot));
Scott Wakelingb77051e2016-11-21 19:46:00 +00004756 __ Lsr(out_reg_lo, in_reg_lo, Operand::From(rot));
Artem Serov02109dd2016-09-23 17:17:54 +01004757 __ Orr(out_reg_lo, out_reg_lo, Operand(in_reg_hi, ShiftType::LSL, kArmBitsPerWord - rot));
4758 } else {
4759 __ Mov(out_reg_lo, in_reg_lo);
4760 __ Mov(out_reg_hi, in_reg_hi);
4761 }
4762 } else {
4763 vixl32::Register shift_right = RegisterFrom(locations->GetTemp(0));
4764 vixl32::Register shift_left = RegisterFrom(locations->GetTemp(1));
4765 vixl32::Label end;
4766 vixl32::Label shift_by_32_plus_shift_right;
Anton Kirilov6f644202017-02-27 18:29:45 +00004767 vixl32::Label* final_label = codegen_->GetFinalLabel(ror, &end);
Artem Serov02109dd2016-09-23 17:17:54 +01004768
4769 __ And(shift_right, RegisterFrom(rhs), 0x1F);
4770 __ Lsrs(shift_left, RegisterFrom(rhs), 6);
Scott Wakelingbffdc702016-12-07 17:46:03 +00004771 __ Rsb(LeaveFlags, shift_left, shift_right, Operand::From(kArmBitsPerWord));
Artem Serov517d9f62016-12-12 15:51:15 +00004772 __ B(cc, &shift_by_32_plus_shift_right, /* far_target */ false);
Artem Serov02109dd2016-09-23 17:17:54 +01004773
4774 // out_reg_hi = (reg_hi << shift_left) | (reg_lo >> shift_right).
4775 // out_reg_lo = (reg_lo << shift_left) | (reg_hi >> shift_right).
4776 __ Lsl(out_reg_hi, in_reg_hi, shift_left);
4777 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
4778 __ Add(out_reg_hi, out_reg_hi, out_reg_lo);
4779 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
4780 __ Lsr(shift_left, in_reg_hi, shift_right);
4781 __ Add(out_reg_lo, out_reg_lo, shift_left);
Anton Kirilov6f644202017-02-27 18:29:45 +00004782 __ B(final_label);
Artem Serov02109dd2016-09-23 17:17:54 +01004783
4784 __ Bind(&shift_by_32_plus_shift_right); // Shift by 32+shift_right.
4785 // out_reg_hi = (reg_hi >> shift_right) | (reg_lo << shift_left).
4786 // out_reg_lo = (reg_lo >> shift_right) | (reg_hi << shift_left).
4787 __ Lsr(out_reg_hi, in_reg_hi, shift_right);
4788 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
4789 __ Add(out_reg_hi, out_reg_hi, out_reg_lo);
4790 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
4791 __ Lsl(shift_right, in_reg_hi, shift_left);
4792 __ Add(out_reg_lo, out_reg_lo, shift_right);
4793
Anton Kirilov6f644202017-02-27 18:29:45 +00004794 if (end.IsReferenced()) {
4795 __ Bind(&end);
4796 }
Artem Serov02109dd2016-09-23 17:17:54 +01004797 }
4798}
4799
4800void LocationsBuilderARMVIXL::VisitRor(HRor* ror) {
4801 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004802 new (GetGraph()->GetAllocator()) LocationSummary(ror, LocationSummary::kNoCall);
Artem Serov02109dd2016-09-23 17:17:54 +01004803 switch (ror->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004804 case DataType::Type::kInt32: {
Artem Serov02109dd2016-09-23 17:17:54 +01004805 locations->SetInAt(0, Location::RequiresRegister());
4806 locations->SetInAt(1, Location::RegisterOrConstant(ror->InputAt(1)));
4807 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4808 break;
4809 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004810 case DataType::Type::kInt64: {
Artem Serov02109dd2016-09-23 17:17:54 +01004811 locations->SetInAt(0, Location::RequiresRegister());
4812 if (ror->InputAt(1)->IsConstant()) {
4813 locations->SetInAt(1, Location::ConstantLocation(ror->InputAt(1)->AsConstant()));
4814 } else {
4815 locations->SetInAt(1, Location::RequiresRegister());
4816 locations->AddTemp(Location::RequiresRegister());
4817 locations->AddTemp(Location::RequiresRegister());
4818 }
4819 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
4820 break;
4821 }
4822 default:
4823 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
4824 }
4825}
4826
4827void InstructionCodeGeneratorARMVIXL::VisitRor(HRor* ror) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004828 DataType::Type type = ror->GetResultType();
Artem Serov02109dd2016-09-23 17:17:54 +01004829 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004830 case DataType::Type::kInt32: {
Artem Serov02109dd2016-09-23 17:17:54 +01004831 HandleIntegerRotate(ror);
4832 break;
4833 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004834 case DataType::Type::kInt64: {
Artem Serov02109dd2016-09-23 17:17:54 +01004835 HandleLongRotate(ror);
4836 break;
4837 }
4838 default:
4839 LOG(FATAL) << "Unexpected operation type " << type;
4840 UNREACHABLE();
4841 }
4842}
4843
Artem Serov02d37832016-10-25 15:25:33 +01004844void LocationsBuilderARMVIXL::HandleShift(HBinaryOperation* op) {
4845 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
4846
4847 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004848 new (GetGraph()->GetAllocator()) LocationSummary(op, LocationSummary::kNoCall);
Artem Serov02d37832016-10-25 15:25:33 +01004849
4850 switch (op->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004851 case DataType::Type::kInt32: {
Artem Serov02d37832016-10-25 15:25:33 +01004852 locations->SetInAt(0, Location::RequiresRegister());
4853 if (op->InputAt(1)->IsConstant()) {
4854 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
4855 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4856 } else {
4857 locations->SetInAt(1, Location::RequiresRegister());
4858 // Make the output overlap, as it will be used to hold the masked
4859 // second input.
4860 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
4861 }
4862 break;
4863 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004864 case DataType::Type::kInt64: {
Artem Serov02d37832016-10-25 15:25:33 +01004865 locations->SetInAt(0, Location::RequiresRegister());
4866 if (op->InputAt(1)->IsConstant()) {
4867 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
4868 // For simplicity, use kOutputOverlap even though we only require that low registers
4869 // don't clash with high registers which the register allocator currently guarantees.
4870 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
4871 } else {
4872 locations->SetInAt(1, Location::RequiresRegister());
4873 locations->AddTemp(Location::RequiresRegister());
4874 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
4875 }
4876 break;
4877 }
4878 default:
4879 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
4880 }
4881}
4882
4883void InstructionCodeGeneratorARMVIXL::HandleShift(HBinaryOperation* op) {
4884 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
4885
4886 LocationSummary* locations = op->GetLocations();
4887 Location out = locations->Out();
4888 Location first = locations->InAt(0);
4889 Location second = locations->InAt(1);
4890
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004891 DataType::Type type = op->GetResultType();
Artem Serov02d37832016-10-25 15:25:33 +01004892 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004893 case DataType::Type::kInt32: {
Artem Serov02d37832016-10-25 15:25:33 +01004894 vixl32::Register out_reg = OutputRegister(op);
4895 vixl32::Register first_reg = InputRegisterAt(op, 0);
4896 if (second.IsRegister()) {
4897 vixl32::Register second_reg = RegisterFrom(second);
4898 // ARM doesn't mask the shift count so we need to do it ourselves.
4899 __ And(out_reg, second_reg, kMaxIntShiftDistance);
4900 if (op->IsShl()) {
4901 __ Lsl(out_reg, first_reg, out_reg);
4902 } else if (op->IsShr()) {
4903 __ Asr(out_reg, first_reg, out_reg);
4904 } else {
4905 __ Lsr(out_reg, first_reg, out_reg);
4906 }
4907 } else {
Anton Kirilov644032c2016-12-06 17:51:43 +00004908 int32_t cst = Int32ConstantFrom(second);
Artem Serov02d37832016-10-25 15:25:33 +01004909 uint32_t shift_value = cst & kMaxIntShiftDistance;
4910 if (shift_value == 0) { // ARM does not support shifting with 0 immediate.
4911 __ Mov(out_reg, first_reg);
4912 } else if (op->IsShl()) {
4913 __ Lsl(out_reg, first_reg, shift_value);
4914 } else if (op->IsShr()) {
4915 __ Asr(out_reg, first_reg, shift_value);
4916 } else {
4917 __ Lsr(out_reg, first_reg, shift_value);
4918 }
4919 }
4920 break;
4921 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004922 case DataType::Type::kInt64: {
Artem Serov02d37832016-10-25 15:25:33 +01004923 vixl32::Register o_h = HighRegisterFrom(out);
4924 vixl32::Register o_l = LowRegisterFrom(out);
4925
4926 vixl32::Register high = HighRegisterFrom(first);
4927 vixl32::Register low = LowRegisterFrom(first);
4928
4929 if (second.IsRegister()) {
4930 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
4931
4932 vixl32::Register second_reg = RegisterFrom(second);
4933
4934 if (op->IsShl()) {
4935 __ And(o_l, second_reg, kMaxLongShiftDistance);
4936 // Shift the high part
4937 __ Lsl(o_h, high, o_l);
4938 // Shift the low part and `or` what overflew on the high part
Scott Wakelingb77051e2016-11-21 19:46:00 +00004939 __ Rsb(temp, o_l, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01004940 __ Lsr(temp, low, temp);
4941 __ Orr(o_h, o_h, temp);
4942 // If the shift is > 32 bits, override the high part
Scott Wakelingb77051e2016-11-21 19:46:00 +00004943 __ Subs(temp, o_l, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01004944 {
Artem Serov0fb37192016-12-06 18:13:40 +00004945 ExactAssemblyScope guard(GetVIXLAssembler(),
4946 2 * vixl32::kMaxInstructionSizeInBytes,
4947 CodeBufferCheckScope::kMaximumSize);
Artem Serov02d37832016-10-25 15:25:33 +01004948 __ it(pl);
4949 __ lsl(pl, o_h, low, temp);
4950 }
4951 // Shift the low part
4952 __ Lsl(o_l, low, o_l);
4953 } else if (op->IsShr()) {
4954 __ And(o_h, second_reg, kMaxLongShiftDistance);
4955 // Shift the low part
4956 __ Lsr(o_l, low, o_h);
4957 // Shift the high part and `or` what underflew on the low part
Scott Wakelingb77051e2016-11-21 19:46:00 +00004958 __ Rsb(temp, o_h, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01004959 __ Lsl(temp, high, temp);
4960 __ Orr(o_l, o_l, temp);
4961 // If the shift is > 32 bits, override the low part
Scott Wakelingb77051e2016-11-21 19:46:00 +00004962 __ Subs(temp, o_h, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01004963 {
Artem Serov0fb37192016-12-06 18:13:40 +00004964 ExactAssemblyScope guard(GetVIXLAssembler(),
4965 2 * vixl32::kMaxInstructionSizeInBytes,
4966 CodeBufferCheckScope::kMaximumSize);
Artem Serov02d37832016-10-25 15:25:33 +01004967 __ it(pl);
4968 __ asr(pl, o_l, high, temp);
4969 }
4970 // Shift the high part
4971 __ Asr(o_h, high, o_h);
4972 } else {
4973 __ And(o_h, second_reg, kMaxLongShiftDistance);
4974 // same as Shr except we use `Lsr`s and not `Asr`s
4975 __ Lsr(o_l, low, o_h);
Scott Wakelingb77051e2016-11-21 19:46:00 +00004976 __ Rsb(temp, o_h, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01004977 __ Lsl(temp, high, temp);
4978 __ Orr(o_l, o_l, temp);
Scott Wakelingb77051e2016-11-21 19:46:00 +00004979 __ Subs(temp, o_h, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01004980 {
Artem Serov0fb37192016-12-06 18:13:40 +00004981 ExactAssemblyScope guard(GetVIXLAssembler(),
4982 2 * vixl32::kMaxInstructionSizeInBytes,
4983 CodeBufferCheckScope::kMaximumSize);
Artem Serov02d37832016-10-25 15:25:33 +01004984 __ it(pl);
4985 __ lsr(pl, o_l, high, temp);
4986 }
4987 __ Lsr(o_h, high, o_h);
4988 }
4989 } else {
4990 // Register allocator doesn't create partial overlap.
4991 DCHECK(!o_l.Is(high));
4992 DCHECK(!o_h.Is(low));
Anton Kirilov644032c2016-12-06 17:51:43 +00004993 int32_t cst = Int32ConstantFrom(second);
Artem Serov02d37832016-10-25 15:25:33 +01004994 uint32_t shift_value = cst & kMaxLongShiftDistance;
4995 if (shift_value > 32) {
4996 if (op->IsShl()) {
4997 __ Lsl(o_h, low, shift_value - 32);
4998 __ Mov(o_l, 0);
4999 } else if (op->IsShr()) {
5000 __ Asr(o_l, high, shift_value - 32);
5001 __ Asr(o_h, high, 31);
5002 } else {
5003 __ Lsr(o_l, high, shift_value - 32);
5004 __ Mov(o_h, 0);
5005 }
5006 } else if (shift_value == 32) {
5007 if (op->IsShl()) {
5008 __ Mov(o_h, low);
5009 __ Mov(o_l, 0);
5010 } else if (op->IsShr()) {
5011 __ Mov(o_l, high);
5012 __ Asr(o_h, high, 31);
5013 } else {
5014 __ Mov(o_l, high);
5015 __ Mov(o_h, 0);
5016 }
5017 } else if (shift_value == 1) {
5018 if (op->IsShl()) {
5019 __ Lsls(o_l, low, 1);
5020 __ Adc(o_h, high, high);
5021 } else if (op->IsShr()) {
5022 __ Asrs(o_h, high, 1);
5023 __ Rrx(o_l, low);
5024 } else {
5025 __ Lsrs(o_h, high, 1);
5026 __ Rrx(o_l, low);
5027 }
5028 } else {
5029 DCHECK(2 <= shift_value && shift_value < 32) << shift_value;
5030 if (op->IsShl()) {
5031 __ Lsl(o_h, high, shift_value);
5032 __ Orr(o_h, o_h, Operand(low, ShiftType::LSR, 32 - shift_value));
5033 __ Lsl(o_l, low, shift_value);
5034 } else if (op->IsShr()) {
5035 __ Lsr(o_l, low, shift_value);
5036 __ Orr(o_l, o_l, Operand(high, ShiftType::LSL, 32 - shift_value));
5037 __ Asr(o_h, high, shift_value);
5038 } else {
5039 __ Lsr(o_l, low, shift_value);
5040 __ Orr(o_l, o_l, Operand(high, ShiftType::LSL, 32 - shift_value));
5041 __ Lsr(o_h, high, shift_value);
5042 }
5043 }
5044 }
5045 break;
5046 }
5047 default:
5048 LOG(FATAL) << "Unexpected operation type " << type;
5049 UNREACHABLE();
5050 }
5051}
5052
5053void LocationsBuilderARMVIXL::VisitShl(HShl* shl) {
5054 HandleShift(shl);
5055}
5056
5057void InstructionCodeGeneratorARMVIXL::VisitShl(HShl* shl) {
5058 HandleShift(shl);
5059}
5060
5061void LocationsBuilderARMVIXL::VisitShr(HShr* shr) {
5062 HandleShift(shr);
5063}
5064
5065void InstructionCodeGeneratorARMVIXL::VisitShr(HShr* shr) {
5066 HandleShift(shr);
5067}
5068
5069void LocationsBuilderARMVIXL::VisitUShr(HUShr* ushr) {
5070 HandleShift(ushr);
5071}
5072
5073void InstructionCodeGeneratorARMVIXL::VisitUShr(HUShr* ushr) {
5074 HandleShift(ushr);
5075}
5076
5077void LocationsBuilderARMVIXL::VisitNewInstance(HNewInstance* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005078 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5079 instruction, LocationSummary::kCallOnMainOnly);
Artem Serov02d37832016-10-25 15:25:33 +01005080 if (instruction->IsStringAlloc()) {
5081 locations->AddTemp(LocationFrom(kMethodRegister));
5082 } else {
5083 InvokeRuntimeCallingConventionARMVIXL calling_convention;
5084 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
Artem Serov02d37832016-10-25 15:25:33 +01005085 }
5086 locations->SetOut(LocationFrom(r0));
5087}
5088
5089void InstructionCodeGeneratorARMVIXL::VisitNewInstance(HNewInstance* instruction) {
5090 // Note: if heap poisoning is enabled, the entry point takes cares
5091 // of poisoning the reference.
5092 if (instruction->IsStringAlloc()) {
5093 // String is allocated through StringFactory. Call NewEmptyString entry point.
5094 vixl32::Register temp = RegisterFrom(instruction->GetLocations()->GetTemp(0));
5095 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize);
5096 GetAssembler()->LoadFromOffset(kLoadWord, temp, tr, QUICK_ENTRY_POINT(pNewEmptyString));
5097 GetAssembler()->LoadFromOffset(kLoadWord, lr, temp, code_offset.Int32Value());
Alexandre Rames374ddf32016-11-04 10:40:49 +00005098 // 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 +00005099 ExactAssemblyScope aas(GetVIXLAssembler(),
5100 vixl32::k16BitT32InstructionSizeInBytes,
5101 CodeBufferCheckScope::kExactSize);
Artem Serov02d37832016-10-25 15:25:33 +01005102 __ blx(lr);
5103 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
5104 } else {
5105 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00005106 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
Artem Serov02d37832016-10-25 15:25:33 +01005107 }
Roland Levillain5daa4952017-07-03 17:23:56 +01005108 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 10);
Artem Serov02d37832016-10-25 15:25:33 +01005109}
5110
5111void LocationsBuilderARMVIXL::VisitNewArray(HNewArray* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005112 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5113 instruction, LocationSummary::kCallOnMainOnly);
Artem Serov02d37832016-10-25 15:25:33 +01005114 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Artem Serov02d37832016-10-25 15:25:33 +01005115 locations->SetOut(LocationFrom(r0));
Nicolas Geoffray8c7c4f12017-01-26 10:13:11 +00005116 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5117 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
Artem Serov02d37832016-10-25 15:25:33 +01005118}
5119
5120void InstructionCodeGeneratorARMVIXL::VisitNewArray(HNewArray* instruction) {
Artem Serov02d37832016-10-25 15:25:33 +01005121 // Note: if heap poisoning is enabled, the entry point takes cares
5122 // of poisoning the reference.
Artem Serov7b3672e2017-02-03 17:30:34 +00005123 QuickEntrypointEnum entrypoint =
5124 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
5125 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005126 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Artem Serov7b3672e2017-02-03 17:30:34 +00005127 DCHECK(!codegen_->IsLeafMethod());
Roland Levillain5daa4952017-07-03 17:23:56 +01005128 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 11);
Artem Serov02d37832016-10-25 15:25:33 +01005129}
5130
5131void LocationsBuilderARMVIXL::VisitParameterValue(HParameterValue* instruction) {
5132 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005133 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Artem Serov02d37832016-10-25 15:25:33 +01005134 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
5135 if (location.IsStackSlot()) {
5136 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5137 } else if (location.IsDoubleStackSlot()) {
5138 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5139 }
5140 locations->SetOut(location);
5141}
5142
5143void InstructionCodeGeneratorARMVIXL::VisitParameterValue(
5144 HParameterValue* instruction ATTRIBUTE_UNUSED) {
5145 // Nothing to do, the parameter is already at its location.
5146}
5147
5148void LocationsBuilderARMVIXL::VisitCurrentMethod(HCurrentMethod* instruction) {
5149 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005150 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Artem Serov02d37832016-10-25 15:25:33 +01005151 locations->SetOut(LocationFrom(kMethodRegister));
5152}
5153
5154void InstructionCodeGeneratorARMVIXL::VisitCurrentMethod(
5155 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
5156 // Nothing to do, the method is already at its location.
5157}
5158
5159void LocationsBuilderARMVIXL::VisitNot(HNot* not_) {
5160 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005161 new (GetGraph()->GetAllocator()) LocationSummary(not_, LocationSummary::kNoCall);
Artem Serov02d37832016-10-25 15:25:33 +01005162 locations->SetInAt(0, Location::RequiresRegister());
5163 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5164}
5165
5166void InstructionCodeGeneratorARMVIXL::VisitNot(HNot* not_) {
5167 LocationSummary* locations = not_->GetLocations();
5168 Location out = locations->Out();
5169 Location in = locations->InAt(0);
5170 switch (not_->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005171 case DataType::Type::kInt32:
Artem Serov02d37832016-10-25 15:25:33 +01005172 __ Mvn(OutputRegister(not_), InputRegisterAt(not_, 0));
5173 break;
5174
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005175 case DataType::Type::kInt64:
Artem Serov02d37832016-10-25 15:25:33 +01005176 __ Mvn(LowRegisterFrom(out), LowRegisterFrom(in));
5177 __ Mvn(HighRegisterFrom(out), HighRegisterFrom(in));
5178 break;
5179
5180 default:
5181 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
5182 }
5183}
5184
Scott Wakelingc34dba72016-10-03 10:14:44 +01005185void LocationsBuilderARMVIXL::VisitBooleanNot(HBooleanNot* bool_not) {
5186 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005187 new (GetGraph()->GetAllocator()) LocationSummary(bool_not, LocationSummary::kNoCall);
Scott Wakelingc34dba72016-10-03 10:14:44 +01005188 locations->SetInAt(0, Location::RequiresRegister());
5189 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5190}
5191
5192void InstructionCodeGeneratorARMVIXL::VisitBooleanNot(HBooleanNot* bool_not) {
5193 __ Eor(OutputRegister(bool_not), InputRegister(bool_not), 1);
5194}
5195
Artem Serov02d37832016-10-25 15:25:33 +01005196void LocationsBuilderARMVIXL::VisitCompare(HCompare* compare) {
5197 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005198 new (GetGraph()->GetAllocator()) LocationSummary(compare, LocationSummary::kNoCall);
Artem Serov02d37832016-10-25 15:25:33 +01005199 switch (compare->InputAt(0)->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005200 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005201 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005202 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005203 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005204 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005205 case DataType::Type::kInt32:
5206 case DataType::Type::kInt64: {
Artem Serov02d37832016-10-25 15:25:33 +01005207 locations->SetInAt(0, Location::RequiresRegister());
5208 locations->SetInAt(1, Location::RequiresRegister());
5209 // Output overlaps because it is written before doing the low comparison.
5210 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
5211 break;
5212 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005213 case DataType::Type::kFloat32:
5214 case DataType::Type::kFloat64: {
Artem Serov02d37832016-10-25 15:25:33 +01005215 locations->SetInAt(0, Location::RequiresFpuRegister());
5216 locations->SetInAt(1, ArithmeticZeroOrFpuRegister(compare->InputAt(1)));
5217 locations->SetOut(Location::RequiresRegister());
5218 break;
5219 }
5220 default:
5221 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
5222 }
5223}
5224
5225void InstructionCodeGeneratorARMVIXL::VisitCompare(HCompare* compare) {
5226 LocationSummary* locations = compare->GetLocations();
5227 vixl32::Register out = OutputRegister(compare);
5228 Location left = locations->InAt(0);
5229 Location right = locations->InAt(1);
5230
5231 vixl32::Label less, greater, done;
Anton Kirilov6f644202017-02-27 18:29:45 +00005232 vixl32::Label* final_label = codegen_->GetFinalLabel(compare, &done);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005233 DataType::Type type = compare->InputAt(0)->GetType();
Vladimir Marko33bff252017-11-01 14:35:42 +00005234 vixl32::Condition less_cond = vixl32::Condition::None();
Artem Serov02d37832016-10-25 15:25:33 +01005235 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005236 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005237 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005238 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005239 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005240 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005241 case DataType::Type::kInt32: {
Artem Serov02d37832016-10-25 15:25:33 +01005242 // Emit move to `out` before the `Cmp`, as `Mov` might affect the status flags.
5243 __ Mov(out, 0);
5244 __ Cmp(RegisterFrom(left), RegisterFrom(right)); // Signed compare.
5245 less_cond = lt;
5246 break;
5247 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005248 case DataType::Type::kInt64: {
Artem Serov02d37832016-10-25 15:25:33 +01005249 __ Cmp(HighRegisterFrom(left), HighRegisterFrom(right)); // Signed compare.
Artem Serov517d9f62016-12-12 15:51:15 +00005250 __ B(lt, &less, /* far_target */ false);
5251 __ B(gt, &greater, /* far_target */ false);
Artem Serov02d37832016-10-25 15:25:33 +01005252 // Emit move to `out` before the last `Cmp`, as `Mov` might affect the status flags.
5253 __ Mov(out, 0);
5254 __ Cmp(LowRegisterFrom(left), LowRegisterFrom(right)); // Unsigned compare.
5255 less_cond = lo;
5256 break;
5257 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005258 case DataType::Type::kFloat32:
5259 case DataType::Type::kFloat64: {
Artem Serov02d37832016-10-25 15:25:33 +01005260 __ Mov(out, 0);
Donghui Bai426b49c2016-11-08 14:55:38 +08005261 GenerateVcmp(compare, codegen_);
Artem Serov02d37832016-10-25 15:25:33 +01005262 // To branch on the FP compare result we transfer FPSCR to APSR (encoded as PC in VMRS).
5263 __ Vmrs(RegisterOrAPSR_nzcv(kPcCode), FPSCR);
5264 less_cond = ARMFPCondition(kCondLT, compare->IsGtBias());
5265 break;
5266 }
5267 default:
5268 LOG(FATAL) << "Unexpected compare type " << type;
5269 UNREACHABLE();
5270 }
5271
Anton Kirilov6f644202017-02-27 18:29:45 +00005272 __ B(eq, final_label, /* far_target */ false);
Artem Serov517d9f62016-12-12 15:51:15 +00005273 __ B(less_cond, &less, /* far_target */ false);
Artem Serov02d37832016-10-25 15:25:33 +01005274
5275 __ Bind(&greater);
5276 __ Mov(out, 1);
Anton Kirilov6f644202017-02-27 18:29:45 +00005277 __ B(final_label);
Artem Serov02d37832016-10-25 15:25:33 +01005278
5279 __ Bind(&less);
5280 __ Mov(out, -1);
5281
Anton Kirilov6f644202017-02-27 18:29:45 +00005282 if (done.IsReferenced()) {
5283 __ Bind(&done);
5284 }
Artem Serov02d37832016-10-25 15:25:33 +01005285}
5286
5287void LocationsBuilderARMVIXL::VisitPhi(HPhi* instruction) {
5288 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005289 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Artem Serov02d37832016-10-25 15:25:33 +01005290 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
5291 locations->SetInAt(i, Location::Any());
5292 }
5293 locations->SetOut(Location::Any());
5294}
5295
5296void InstructionCodeGeneratorARMVIXL::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
5297 LOG(FATAL) << "Unreachable";
5298}
5299
5300void CodeGeneratorARMVIXL::GenerateMemoryBarrier(MemBarrierKind kind) {
5301 // TODO (ported from quick): revisit ARM barrier kinds.
5302 DmbOptions flavor = DmbOptions::ISH; // Quiet C++ warnings.
5303 switch (kind) {
5304 case MemBarrierKind::kAnyStore:
5305 case MemBarrierKind::kLoadAny:
5306 case MemBarrierKind::kAnyAny: {
5307 flavor = DmbOptions::ISH;
5308 break;
5309 }
5310 case MemBarrierKind::kStoreStore: {
5311 flavor = DmbOptions::ISHST;
5312 break;
5313 }
5314 default:
5315 LOG(FATAL) << "Unexpected memory barrier " << kind;
5316 }
5317 __ Dmb(flavor);
5318}
5319
5320void InstructionCodeGeneratorARMVIXL::GenerateWideAtomicLoad(vixl32::Register addr,
5321 uint32_t offset,
5322 vixl32::Register out_lo,
5323 vixl32::Register out_hi) {
5324 UseScratchRegisterScope temps(GetVIXLAssembler());
5325 if (offset != 0) {
5326 vixl32::Register temp = temps.Acquire();
5327 __ Add(temp, addr, offset);
5328 addr = temp;
5329 }
Scott Wakelingb77051e2016-11-21 19:46:00 +00005330 __ Ldrexd(out_lo, out_hi, MemOperand(addr));
Artem Serov02d37832016-10-25 15:25:33 +01005331}
5332
5333void InstructionCodeGeneratorARMVIXL::GenerateWideAtomicStore(vixl32::Register addr,
5334 uint32_t offset,
5335 vixl32::Register value_lo,
5336 vixl32::Register value_hi,
5337 vixl32::Register temp1,
5338 vixl32::Register temp2,
5339 HInstruction* instruction) {
5340 UseScratchRegisterScope temps(GetVIXLAssembler());
5341 vixl32::Label fail;
5342 if (offset != 0) {
5343 vixl32::Register temp = temps.Acquire();
5344 __ Add(temp, addr, offset);
5345 addr = temp;
5346 }
5347 __ Bind(&fail);
Alexandre Rames374ddf32016-11-04 10:40:49 +00005348 {
5349 // Ensure the pc position is recorded immediately after the `ldrexd` instruction.
Artem Serov0fb37192016-12-06 18:13:40 +00005350 ExactAssemblyScope aas(GetVIXLAssembler(),
5351 vixl32::kMaxInstructionSizeInBytes,
5352 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00005353 // We need a load followed by store. (The address used in a STREX instruction must
5354 // be the same as the address in the most recently executed LDREX instruction.)
5355 __ ldrexd(temp1, temp2, MemOperand(addr));
5356 codegen_->MaybeRecordImplicitNullCheck(instruction);
5357 }
Scott Wakelingb77051e2016-11-21 19:46:00 +00005358 __ Strexd(temp1, value_lo, value_hi, MemOperand(addr));
xueliang.zhongf51bc622016-11-04 09:23:32 +00005359 __ CompareAndBranchIfNonZero(temp1, &fail);
Artem Serov02d37832016-10-25 15:25:33 +01005360}
Artem Serov02109dd2016-09-23 17:17:54 +01005361
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005362void LocationsBuilderARMVIXL::HandleFieldSet(
5363 HInstruction* instruction, const FieldInfo& field_info) {
5364 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
5365
5366 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005367 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005368 locations->SetInAt(0, Location::RequiresRegister());
5369
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005370 DataType::Type field_type = field_info.GetFieldType();
5371 if (DataType::IsFloatingPointType(field_type)) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005372 locations->SetInAt(1, Location::RequiresFpuRegister());
5373 } else {
5374 locations->SetInAt(1, Location::RequiresRegister());
5375 }
5376
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005377 bool is_wide = field_type == DataType::Type::kInt64 || field_type == DataType::Type::kFloat64;
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005378 bool generate_volatile = field_info.IsVolatile()
5379 && is_wide
5380 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
5381 bool needs_write_barrier =
5382 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
5383 // Temporary registers for the write barrier.
5384 // TODO: consider renaming StoreNeedsWriteBarrier to StoreNeedsGCMark.
5385 if (needs_write_barrier) {
5386 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
5387 locations->AddTemp(Location::RequiresRegister());
5388 } else if (generate_volatile) {
5389 // ARM encoding have some additional constraints for ldrexd/strexd:
5390 // - registers need to be consecutive
5391 // - the first register should be even but not R14.
5392 // We don't test for ARM yet, and the assertion makes sure that we
5393 // revisit this if we ever enable ARM encoding.
5394 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
5395
5396 locations->AddTemp(Location::RequiresRegister());
5397 locations->AddTemp(Location::RequiresRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005398 if (field_type == DataType::Type::kFloat64) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005399 // For doubles we need two more registers to copy the value.
5400 locations->AddTemp(LocationFrom(r2));
5401 locations->AddTemp(LocationFrom(r3));
5402 }
5403 }
5404}
5405
5406void InstructionCodeGeneratorARMVIXL::HandleFieldSet(HInstruction* instruction,
5407 const FieldInfo& field_info,
5408 bool value_can_be_null) {
5409 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
5410
5411 LocationSummary* locations = instruction->GetLocations();
5412 vixl32::Register base = InputRegisterAt(instruction, 0);
5413 Location value = locations->InAt(1);
5414
5415 bool is_volatile = field_info.IsVolatile();
5416 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005417 DataType::Type field_type = field_info.GetFieldType();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005418 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
5419 bool needs_write_barrier =
5420 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
5421
5422 if (is_volatile) {
5423 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
5424 }
5425
5426 switch (field_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005427 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005428 case DataType::Type::kUint8:
5429 case DataType::Type::kInt8:
5430 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005431 case DataType::Type::kInt16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005432 case DataType::Type::kInt32: {
5433 StoreOperandType operand_type = GetStoreOperandType(field_type);
5434 GetAssembler()->StoreToOffset(operand_type, RegisterFrom(value), base, offset);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005435 break;
5436 }
5437
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005438 case DataType::Type::kReference: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005439 if (kPoisonHeapReferences && needs_write_barrier) {
5440 // Note that in the case where `value` is a null reference,
5441 // we do not enter this block, as a null reference does not
5442 // need poisoning.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005443 DCHECK_EQ(field_type, DataType::Type::kReference);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005444 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
5445 __ Mov(temp, RegisterFrom(value));
5446 GetAssembler()->PoisonHeapReference(temp);
5447 GetAssembler()->StoreToOffset(kStoreWord, temp, base, offset);
5448 } else {
5449 GetAssembler()->StoreToOffset(kStoreWord, RegisterFrom(value), base, offset);
5450 }
5451 break;
5452 }
5453
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005454 case DataType::Type::kInt64: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005455 if (is_volatile && !atomic_ldrd_strd) {
5456 GenerateWideAtomicStore(base,
5457 offset,
5458 LowRegisterFrom(value),
5459 HighRegisterFrom(value),
5460 RegisterFrom(locations->GetTemp(0)),
5461 RegisterFrom(locations->GetTemp(1)),
5462 instruction);
5463 } else {
5464 GetAssembler()->StoreToOffset(kStoreWordPair, LowRegisterFrom(value), base, offset);
5465 codegen_->MaybeRecordImplicitNullCheck(instruction);
5466 }
5467 break;
5468 }
5469
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005470 case DataType::Type::kFloat32: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005471 GetAssembler()->StoreSToOffset(SRegisterFrom(value), base, offset);
5472 break;
5473 }
5474
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005475 case DataType::Type::kFloat64: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01005476 vixl32::DRegister value_reg = DRegisterFrom(value);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005477 if (is_volatile && !atomic_ldrd_strd) {
5478 vixl32::Register value_reg_lo = RegisterFrom(locations->GetTemp(0));
5479 vixl32::Register value_reg_hi = RegisterFrom(locations->GetTemp(1));
5480
5481 __ Vmov(value_reg_lo, value_reg_hi, value_reg);
5482
5483 GenerateWideAtomicStore(base,
5484 offset,
5485 value_reg_lo,
5486 value_reg_hi,
5487 RegisterFrom(locations->GetTemp(2)),
5488 RegisterFrom(locations->GetTemp(3)),
5489 instruction);
5490 } else {
5491 GetAssembler()->StoreDToOffset(value_reg, base, offset);
5492 codegen_->MaybeRecordImplicitNullCheck(instruction);
5493 }
5494 break;
5495 }
5496
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005497 case DataType::Type::kVoid:
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005498 LOG(FATAL) << "Unreachable type " << field_type;
5499 UNREACHABLE();
5500 }
5501
5502 // Longs and doubles are handled in the switch.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005503 if (field_type != DataType::Type::kInt64 && field_type != DataType::Type::kFloat64) {
Alexandre Rames374ddf32016-11-04 10:40:49 +00005504 // TODO(VIXL): Here and for other calls to `MaybeRecordImplicitNullCheck` in this method, we
5505 // should use a scope and the assembler to emit the store instruction to guarantee that we
5506 // record the pc at the correct position. But the `Assembler` does not automatically handle
5507 // unencodable offsets. Practically, everything is fine because the helper and VIXL, at the time
5508 // of writing, do generate the store instruction last.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005509 codegen_->MaybeRecordImplicitNullCheck(instruction);
5510 }
5511
5512 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
5513 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
5514 vixl32::Register card = RegisterFrom(locations->GetTemp(1));
5515 codegen_->MarkGCCard(temp, card, base, RegisterFrom(value), value_can_be_null);
5516 }
5517
5518 if (is_volatile) {
5519 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
5520 }
5521}
5522
Artem Serov02d37832016-10-25 15:25:33 +01005523void LocationsBuilderARMVIXL::HandleFieldGet(HInstruction* instruction,
5524 const FieldInfo& field_info) {
5525 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
5526
5527 bool object_field_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005528 kEmitCompilerReadBarrier && (field_info.GetFieldType() == DataType::Type::kReference);
Artem Serov02d37832016-10-25 15:25:33 +01005529 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005530 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
5531 object_field_get_with_read_barrier
5532 ? LocationSummary::kCallOnSlowPath
5533 : LocationSummary::kNoCall);
Artem Serov02d37832016-10-25 15:25:33 +01005534 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
5535 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
5536 }
5537 locations->SetInAt(0, Location::RequiresRegister());
5538
5539 bool volatile_for_double = field_info.IsVolatile()
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005540 && (field_info.GetFieldType() == DataType::Type::kFloat64)
Artem Serov02d37832016-10-25 15:25:33 +01005541 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
5542 // The output overlaps in case of volatile long: we don't want the
5543 // code generated by GenerateWideAtomicLoad to overwrite the
5544 // object's location. Likewise, in the case of an object field get
5545 // with read barriers enabled, we do not want the load to overwrite
5546 // the object's location, as we need it to emit the read barrier.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005547 bool overlap =
5548 (field_info.IsVolatile() && (field_info.GetFieldType() == DataType::Type::kInt64)) ||
Artem Serov02d37832016-10-25 15:25:33 +01005549 object_field_get_with_read_barrier;
5550
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005551 if (DataType::IsFloatingPointType(instruction->GetType())) {
Artem Serov02d37832016-10-25 15:25:33 +01005552 locations->SetOut(Location::RequiresFpuRegister());
5553 } else {
5554 locations->SetOut(Location::RequiresRegister(),
5555 (overlap ? Location::kOutputOverlap : Location::kNoOutputOverlap));
5556 }
5557 if (volatile_for_double) {
5558 // ARM encoding have some additional constraints for ldrexd/strexd:
5559 // - registers need to be consecutive
5560 // - the first register should be even but not R14.
5561 // We don't test for ARM yet, and the assertion makes sure that we
5562 // revisit this if we ever enable ARM encoding.
5563 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
5564 locations->AddTemp(Location::RequiresRegister());
5565 locations->AddTemp(Location::RequiresRegister());
5566 } else if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
5567 // We need a temporary register for the read barrier marking slow
Artem Serovc5fcb442016-12-02 19:19:58 +00005568 // path in CodeGeneratorARMVIXL::GenerateFieldLoadWithBakerReadBarrier.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01005569 if (kBakerReadBarrierLinkTimeThunksEnableForFields &&
5570 !Runtime::Current()->UseJitCompilation()) {
5571 // If link-time thunks for the Baker read barrier are enabled, for AOT
5572 // loads we need a temporary only if the offset is too big.
5573 if (field_info.GetFieldOffset().Uint32Value() >= kReferenceLoadMinFarOffset) {
5574 locations->AddTemp(Location::RequiresRegister());
5575 }
5576 // And we always need the reserved entrypoint register.
5577 locations->AddTemp(Location::RegisterLocation(kBakerCcEntrypointRegister.GetCode()));
5578 } else {
5579 locations->AddTemp(Location::RequiresRegister());
5580 }
Artem Serov02d37832016-10-25 15:25:33 +01005581 }
5582}
5583
5584Location LocationsBuilderARMVIXL::ArithmeticZeroOrFpuRegister(HInstruction* input) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005585 DCHECK(DataType::IsFloatingPointType(input->GetType())) << input->GetType();
Artem Serov02d37832016-10-25 15:25:33 +01005586 if ((input->IsFloatConstant() && (input->AsFloatConstant()->IsArithmeticZero())) ||
5587 (input->IsDoubleConstant() && (input->AsDoubleConstant()->IsArithmeticZero()))) {
5588 return Location::ConstantLocation(input->AsConstant());
5589 } else {
5590 return Location::RequiresFpuRegister();
5591 }
5592}
5593
Artem Serov02109dd2016-09-23 17:17:54 +01005594Location LocationsBuilderARMVIXL::ArmEncodableConstantOrRegister(HInstruction* constant,
5595 Opcode opcode) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005596 DCHECK(!DataType::IsFloatingPointType(constant->GetType()));
Artem Serov02109dd2016-09-23 17:17:54 +01005597 if (constant->IsConstant() &&
5598 CanEncodeConstantAsImmediate(constant->AsConstant(), opcode)) {
5599 return Location::ConstantLocation(constant->AsConstant());
5600 }
5601 return Location::RequiresRegister();
5602}
5603
Vladimir Markof0a6a1d2018-01-08 14:23:56 +00005604static bool CanEncode32BitConstantAsImmediate(
5605 CodeGeneratorARMVIXL* codegen,
5606 uint32_t value,
5607 Opcode opcode,
5608 vixl32::FlagsUpdate flags_update = vixl32::FlagsUpdate::DontCare) {
5609 ArmVIXLAssembler* assembler = codegen->GetAssembler();
5610 if (assembler->ShifterOperandCanHold(opcode, value, flags_update)) {
Artem Serov02109dd2016-09-23 17:17:54 +01005611 return true;
5612 }
5613 Opcode neg_opcode = kNoOperand;
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00005614 uint32_t neg_value = 0;
Artem Serov02109dd2016-09-23 17:17:54 +01005615 switch (opcode) {
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00005616 case AND: neg_opcode = BIC; neg_value = ~value; break;
5617 case ORR: neg_opcode = ORN; neg_value = ~value; break;
5618 case ADD: neg_opcode = SUB; neg_value = -value; break;
5619 case ADC: neg_opcode = SBC; neg_value = ~value; break;
5620 case SUB: neg_opcode = ADD; neg_value = -value; break;
5621 case SBC: neg_opcode = ADC; neg_value = ~value; break;
5622 case MOV: neg_opcode = MVN; neg_value = ~value; break;
Artem Serov02109dd2016-09-23 17:17:54 +01005623 default:
5624 return false;
5625 }
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00005626
Vladimir Markof0a6a1d2018-01-08 14:23:56 +00005627 if (assembler->ShifterOperandCanHold(neg_opcode, neg_value, flags_update)) {
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00005628 return true;
5629 }
5630
5631 return opcode == AND && IsPowerOfTwo(value + 1);
Artem Serov02109dd2016-09-23 17:17:54 +01005632}
5633
Vladimir Markof0a6a1d2018-01-08 14:23:56 +00005634bool LocationsBuilderARMVIXL::CanEncodeConstantAsImmediate(HConstant* input_cst, Opcode opcode) {
5635 uint64_t value = static_cast<uint64_t>(Int64FromConstant(input_cst));
5636 if (DataType::Is64BitType(input_cst->GetType())) {
5637 Opcode high_opcode = opcode;
5638 vixl32::FlagsUpdate low_flags_update = vixl32::FlagsUpdate::DontCare;
5639 switch (opcode) {
5640 case SUB:
5641 // Flip the operation to an ADD.
5642 value = -value;
5643 opcode = ADD;
5644 FALLTHROUGH_INTENDED;
5645 case ADD:
5646 if (Low32Bits(value) == 0u) {
5647 return CanEncode32BitConstantAsImmediate(codegen_, High32Bits(value), opcode);
5648 }
5649 high_opcode = ADC;
5650 low_flags_update = vixl32::FlagsUpdate::SetFlags;
5651 break;
5652 default:
5653 break;
5654 }
5655 return CanEncode32BitConstantAsImmediate(codegen_, High32Bits(value), high_opcode) &&
5656 CanEncode32BitConstantAsImmediate(codegen_, Low32Bits(value), opcode, low_flags_update);
5657 } else {
5658 return CanEncode32BitConstantAsImmediate(codegen_, Low32Bits(value), opcode);
5659 }
5660}
5661
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005662void InstructionCodeGeneratorARMVIXL::HandleFieldGet(HInstruction* instruction,
5663 const FieldInfo& field_info) {
5664 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
5665
5666 LocationSummary* locations = instruction->GetLocations();
5667 vixl32::Register base = InputRegisterAt(instruction, 0);
5668 Location out = locations->Out();
5669 bool is_volatile = field_info.IsVolatile();
5670 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Vladimir Marko61b92282017-10-11 13:23:17 +01005671 DCHECK_EQ(DataType::Size(field_info.GetFieldType()), DataType::Size(instruction->GetType()));
5672 DataType::Type load_type = instruction->GetType();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005673 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
5674
Vladimir Marko61b92282017-10-11 13:23:17 +01005675 switch (load_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005676 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005677 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005678 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005679 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005680 case DataType::Type::kInt16:
5681 case DataType::Type::kInt32: {
Vladimir Marko61b92282017-10-11 13:23:17 +01005682 LoadOperandType operand_type = GetLoadOperandType(load_type);
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005683 GetAssembler()->LoadFromOffset(operand_type, RegisterFrom(out), base, offset);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005684 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005685 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005686
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005687 case DataType::Type::kReference: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005688 // /* HeapReference<Object> */ out = *(base + offset)
5689 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005690 Location temp_loc = locations->GetTemp(0);
5691 // Note that a potential implicit null check is handled in this
5692 // CodeGeneratorARMVIXL::GenerateFieldLoadWithBakerReadBarrier call.
5693 codegen_->GenerateFieldLoadWithBakerReadBarrier(
5694 instruction, out, base, offset, temp_loc, /* needs_null_check */ true);
5695 if (is_volatile) {
5696 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5697 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005698 } else {
5699 GetAssembler()->LoadFromOffset(kLoadWord, RegisterFrom(out), base, offset);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005700 codegen_->MaybeRecordImplicitNullCheck(instruction);
5701 if (is_volatile) {
5702 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5703 }
5704 // If read barriers are enabled, emit read barriers other than
5705 // Baker's using a slow path (and also unpoison the loaded
5706 // reference, if heap poisoning is enabled).
5707 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, locations->InAt(0), offset);
5708 }
5709 break;
5710 }
5711
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005712 case DataType::Type::kInt64:
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005713 if (is_volatile && !atomic_ldrd_strd) {
5714 GenerateWideAtomicLoad(base, offset, LowRegisterFrom(out), HighRegisterFrom(out));
5715 } else {
5716 GetAssembler()->LoadFromOffset(kLoadWordPair, LowRegisterFrom(out), base, offset);
5717 }
5718 break;
5719
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005720 case DataType::Type::kFloat32:
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005721 GetAssembler()->LoadSFromOffset(SRegisterFrom(out), base, offset);
5722 break;
5723
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005724 case DataType::Type::kFloat64: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01005725 vixl32::DRegister out_dreg = DRegisterFrom(out);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005726 if (is_volatile && !atomic_ldrd_strd) {
5727 vixl32::Register lo = RegisterFrom(locations->GetTemp(0));
5728 vixl32::Register hi = RegisterFrom(locations->GetTemp(1));
5729 GenerateWideAtomicLoad(base, offset, lo, hi);
5730 // TODO(VIXL): Do we need to be immediately after the ldrexd instruction? If so we need a
5731 // scope.
5732 codegen_->MaybeRecordImplicitNullCheck(instruction);
5733 __ Vmov(out_dreg, lo, hi);
5734 } else {
5735 GetAssembler()->LoadDFromOffset(out_dreg, base, offset);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005736 codegen_->MaybeRecordImplicitNullCheck(instruction);
5737 }
5738 break;
5739 }
5740
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005741 case DataType::Type::kVoid:
Vladimir Marko61b92282017-10-11 13:23:17 +01005742 LOG(FATAL) << "Unreachable type " << load_type;
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005743 UNREACHABLE();
5744 }
5745
Vladimir Marko61b92282017-10-11 13:23:17 +01005746 if (load_type == DataType::Type::kReference || load_type == DataType::Type::kFloat64) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005747 // Potential implicit null checks, in the case of reference or
5748 // double fields, are handled in the previous switch statement.
5749 } else {
5750 // Address cases other than reference and double that may require an implicit null check.
Alexandre Rames374ddf32016-11-04 10:40:49 +00005751 // TODO(VIXL): Here and for other calls to `MaybeRecordImplicitNullCheck` in this method, we
5752 // should use a scope and the assembler to emit the load instruction to guarantee that we
5753 // record the pc at the correct position. But the `Assembler` does not automatically handle
5754 // unencodable offsets. Practically, everything is fine because the helper and VIXL, at the time
5755 // of writing, do generate the store instruction last.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005756 codegen_->MaybeRecordImplicitNullCheck(instruction);
5757 }
5758
5759 if (is_volatile) {
Vladimir Marko61b92282017-10-11 13:23:17 +01005760 if (load_type == DataType::Type::kReference) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005761 // Memory barriers, in the case of references, are also handled
5762 // in the previous switch statement.
5763 } else {
5764 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5765 }
5766 }
5767}
5768
5769void LocationsBuilderARMVIXL::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
5770 HandleFieldSet(instruction, instruction->GetFieldInfo());
5771}
5772
5773void InstructionCodeGeneratorARMVIXL::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
5774 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
5775}
5776
5777void LocationsBuilderARMVIXL::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5778 HandleFieldGet(instruction, instruction->GetFieldInfo());
5779}
5780
5781void InstructionCodeGeneratorARMVIXL::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5782 HandleFieldGet(instruction, instruction->GetFieldInfo());
5783}
5784
5785void LocationsBuilderARMVIXL::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5786 HandleFieldGet(instruction, instruction->GetFieldInfo());
5787}
5788
5789void InstructionCodeGeneratorARMVIXL::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5790 HandleFieldGet(instruction, instruction->GetFieldInfo());
5791}
5792
Scott Wakelingc34dba72016-10-03 10:14:44 +01005793void LocationsBuilderARMVIXL::VisitStaticFieldSet(HStaticFieldSet* instruction) {
5794 HandleFieldSet(instruction, instruction->GetFieldInfo());
5795}
5796
5797void InstructionCodeGeneratorARMVIXL::VisitStaticFieldSet(HStaticFieldSet* instruction) {
5798 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
5799}
5800
Artem Serovcfbe9132016-10-14 15:58:56 +01005801void LocationsBuilderARMVIXL::VisitUnresolvedInstanceFieldGet(
5802 HUnresolvedInstanceFieldGet* instruction) {
5803 FieldAccessCallingConventionARMVIXL calling_convention;
5804 codegen_->CreateUnresolvedFieldLocationSummary(
5805 instruction, instruction->GetFieldType(), calling_convention);
5806}
5807
5808void InstructionCodeGeneratorARMVIXL::VisitUnresolvedInstanceFieldGet(
5809 HUnresolvedInstanceFieldGet* instruction) {
5810 FieldAccessCallingConventionARMVIXL calling_convention;
5811 codegen_->GenerateUnresolvedFieldAccess(instruction,
5812 instruction->GetFieldType(),
5813 instruction->GetFieldIndex(),
5814 instruction->GetDexPc(),
5815 calling_convention);
5816}
5817
5818void LocationsBuilderARMVIXL::VisitUnresolvedInstanceFieldSet(
5819 HUnresolvedInstanceFieldSet* instruction) {
5820 FieldAccessCallingConventionARMVIXL calling_convention;
5821 codegen_->CreateUnresolvedFieldLocationSummary(
5822 instruction, instruction->GetFieldType(), calling_convention);
5823}
5824
5825void InstructionCodeGeneratorARMVIXL::VisitUnresolvedInstanceFieldSet(
5826 HUnresolvedInstanceFieldSet* instruction) {
5827 FieldAccessCallingConventionARMVIXL calling_convention;
5828 codegen_->GenerateUnresolvedFieldAccess(instruction,
5829 instruction->GetFieldType(),
5830 instruction->GetFieldIndex(),
5831 instruction->GetDexPc(),
5832 calling_convention);
5833}
5834
5835void LocationsBuilderARMVIXL::VisitUnresolvedStaticFieldGet(
5836 HUnresolvedStaticFieldGet* instruction) {
5837 FieldAccessCallingConventionARMVIXL calling_convention;
5838 codegen_->CreateUnresolvedFieldLocationSummary(
5839 instruction, instruction->GetFieldType(), calling_convention);
5840}
5841
5842void InstructionCodeGeneratorARMVIXL::VisitUnresolvedStaticFieldGet(
5843 HUnresolvedStaticFieldGet* instruction) {
5844 FieldAccessCallingConventionARMVIXL calling_convention;
5845 codegen_->GenerateUnresolvedFieldAccess(instruction,
5846 instruction->GetFieldType(),
5847 instruction->GetFieldIndex(),
5848 instruction->GetDexPc(),
5849 calling_convention);
5850}
5851
5852void LocationsBuilderARMVIXL::VisitUnresolvedStaticFieldSet(
5853 HUnresolvedStaticFieldSet* instruction) {
5854 FieldAccessCallingConventionARMVIXL calling_convention;
5855 codegen_->CreateUnresolvedFieldLocationSummary(
5856 instruction, instruction->GetFieldType(), calling_convention);
5857}
5858
5859void InstructionCodeGeneratorARMVIXL::VisitUnresolvedStaticFieldSet(
5860 HUnresolvedStaticFieldSet* instruction) {
5861 FieldAccessCallingConventionARMVIXL calling_convention;
5862 codegen_->GenerateUnresolvedFieldAccess(instruction,
5863 instruction->GetFieldType(),
5864 instruction->GetFieldIndex(),
5865 instruction->GetDexPc(),
5866 calling_convention);
5867}
5868
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005869void LocationsBuilderARMVIXL::VisitNullCheck(HNullCheck* instruction) {
Artem Serov657022c2016-11-23 14:19:38 +00005870 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005871 locations->SetInAt(0, Location::RequiresRegister());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005872}
5873
5874void CodeGeneratorARMVIXL::GenerateImplicitNullCheck(HNullCheck* instruction) {
5875 if (CanMoveNullCheckToUser(instruction)) {
5876 return;
5877 }
5878
5879 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames374ddf32016-11-04 10:40:49 +00005880 // Ensure the pc position is recorded immediately after the `ldr` instruction.
Artem Serov0fb37192016-12-06 18:13:40 +00005881 ExactAssemblyScope aas(GetVIXLAssembler(),
5882 vixl32::kMaxInstructionSizeInBytes,
5883 CodeBufferCheckScope::kMaximumSize);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005884 __ ldr(temps.Acquire(), MemOperand(InputRegisterAt(instruction, 0)));
5885 RecordPcInfo(instruction, instruction->GetDexPc());
5886}
5887
5888void CodeGeneratorARMVIXL::GenerateExplicitNullCheck(HNullCheck* instruction) {
5889 NullCheckSlowPathARMVIXL* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01005890 new (GetScopedAllocator()) NullCheckSlowPathARMVIXL(instruction);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005891 AddSlowPath(slow_path);
xueliang.zhongf51bc622016-11-04 09:23:32 +00005892 __ CompareAndBranchIfZero(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005893}
5894
5895void InstructionCodeGeneratorARMVIXL::VisitNullCheck(HNullCheck* instruction) {
5896 codegen_->GenerateNullCheck(instruction);
5897}
5898
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005899void CodeGeneratorARMVIXL::LoadFromShiftedRegOffset(DataType::Type type,
Scott Wakelingc34dba72016-10-03 10:14:44 +01005900 Location out_loc,
5901 vixl32::Register base,
5902 vixl32::Register reg_index,
5903 vixl32::Condition cond) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005904 uint32_t shift_count = DataType::SizeShift(type);
Scott Wakelingc34dba72016-10-03 10:14:44 +01005905 MemOperand mem_address(base, reg_index, vixl32::LSL, shift_count);
5906
5907 switch (type) {
Vladimir Marko61b92282017-10-11 13:23:17 +01005908 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005909 case DataType::Type::kUint8:
Vladimir Marko61b92282017-10-11 13:23:17 +01005910 __ Ldrb(cond, RegisterFrom(out_loc), mem_address);
5911 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005912 case DataType::Type::kInt8:
Scott Wakelingc34dba72016-10-03 10:14:44 +01005913 __ Ldrsb(cond, RegisterFrom(out_loc), mem_address);
5914 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005915 case DataType::Type::kUint16:
Scott Wakelingc34dba72016-10-03 10:14:44 +01005916 __ Ldrh(cond, RegisterFrom(out_loc), mem_address);
5917 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005918 case DataType::Type::kInt16:
5919 __ Ldrsh(cond, RegisterFrom(out_loc), mem_address);
5920 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005921 case DataType::Type::kReference:
5922 case DataType::Type::kInt32:
Scott Wakelingc34dba72016-10-03 10:14:44 +01005923 __ Ldr(cond, RegisterFrom(out_loc), mem_address);
5924 break;
5925 // T32 doesn't support LoadFromShiftedRegOffset mem address mode for these types.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005926 case DataType::Type::kInt64:
5927 case DataType::Type::kFloat32:
5928 case DataType::Type::kFloat64:
Scott Wakelingc34dba72016-10-03 10:14:44 +01005929 default:
5930 LOG(FATAL) << "Unreachable type " << type;
5931 UNREACHABLE();
5932 }
5933}
5934
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005935void CodeGeneratorARMVIXL::StoreToShiftedRegOffset(DataType::Type type,
Scott Wakelingc34dba72016-10-03 10:14:44 +01005936 Location loc,
5937 vixl32::Register base,
5938 vixl32::Register reg_index,
5939 vixl32::Condition cond) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005940 uint32_t shift_count = DataType::SizeShift(type);
Scott Wakelingc34dba72016-10-03 10:14:44 +01005941 MemOperand mem_address(base, reg_index, vixl32::LSL, shift_count);
5942
5943 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005944 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005945 case DataType::Type::kUint8:
5946 case DataType::Type::kInt8:
Scott Wakelingc34dba72016-10-03 10:14:44 +01005947 __ Strb(cond, RegisterFrom(loc), mem_address);
5948 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005949 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005950 case DataType::Type::kInt16:
Scott Wakelingc34dba72016-10-03 10:14:44 +01005951 __ Strh(cond, RegisterFrom(loc), mem_address);
5952 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005953 case DataType::Type::kReference:
5954 case DataType::Type::kInt32:
Scott Wakelingc34dba72016-10-03 10:14:44 +01005955 __ Str(cond, RegisterFrom(loc), mem_address);
5956 break;
5957 // T32 doesn't support StoreToShiftedRegOffset mem address mode for these types.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005958 case DataType::Type::kInt64:
5959 case DataType::Type::kFloat32:
5960 case DataType::Type::kFloat64:
Scott Wakelingc34dba72016-10-03 10:14:44 +01005961 default:
5962 LOG(FATAL) << "Unreachable type " << type;
5963 UNREACHABLE();
5964 }
5965}
5966
5967void LocationsBuilderARMVIXL::VisitArrayGet(HArrayGet* instruction) {
5968 bool object_array_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005969 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Scott Wakelingc34dba72016-10-03 10:14:44 +01005970 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005971 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
5972 object_array_get_with_read_barrier
5973 ? LocationSummary::kCallOnSlowPath
5974 : LocationSummary::kNoCall);
Scott Wakelingc34dba72016-10-03 10:14:44 +01005975 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005976 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Scott Wakelingc34dba72016-10-03 10:14:44 +01005977 }
5978 locations->SetInAt(0, Location::RequiresRegister());
5979 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005980 if (DataType::IsFloatingPointType(instruction->GetType())) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01005981 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5982 } else {
5983 // The output overlaps in the case of an object array get with
5984 // read barriers enabled: we do not want the move to overwrite the
5985 // array's location, as we need it to emit the read barrier.
5986 locations->SetOut(
5987 Location::RequiresRegister(),
5988 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
5989 }
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01005990 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
5991 // We need a temporary register for the read barrier marking slow
5992 // path in CodeGeneratorARMVIXL::GenerateArrayLoadWithBakerReadBarrier.
5993 if (kBakerReadBarrierLinkTimeThunksEnableForFields &&
5994 !Runtime::Current()->UseJitCompilation() &&
5995 instruction->GetIndex()->IsConstant()) {
5996 // Array loads with constant index are treated as field loads.
5997 // If link-time thunks for the Baker read barrier are enabled, for AOT
5998 // constant index loads we need a temporary only if the offset is too big.
5999 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
6000 uint32_t index = instruction->GetIndex()->AsIntConstant()->GetValue();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006001 offset += index << DataType::SizeShift(DataType::Type::kReference);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01006002 if (offset >= kReferenceLoadMinFarOffset) {
6003 locations->AddTemp(Location::RequiresRegister());
6004 }
6005 // And we always need the reserved entrypoint register.
6006 locations->AddTemp(Location::RegisterLocation(kBakerCcEntrypointRegister.GetCode()));
6007 } else if (kBakerReadBarrierLinkTimeThunksEnableForArrays &&
6008 !Runtime::Current()->UseJitCompilation() &&
6009 !instruction->GetIndex()->IsConstant()) {
6010 // We need a non-scratch temporary for the array data pointer.
6011 locations->AddTemp(Location::RequiresRegister());
6012 // And we always need the reserved entrypoint register.
6013 locations->AddTemp(Location::RegisterLocation(kBakerCcEntrypointRegister.GetCode()));
6014 } else {
6015 locations->AddTemp(Location::RequiresRegister());
6016 }
6017 } else if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
6018 // Also need a temporary for String compression feature.
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006019 locations->AddTemp(Location::RequiresRegister());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006020 }
6021}
6022
6023void InstructionCodeGeneratorARMVIXL::VisitArrayGet(HArrayGet* instruction) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006024 LocationSummary* locations = instruction->GetLocations();
6025 Location obj_loc = locations->InAt(0);
6026 vixl32::Register obj = InputRegisterAt(instruction, 0);
6027 Location index = locations->InAt(1);
6028 Location out_loc = locations->Out();
6029 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006030 DataType::Type type = instruction->GetType();
Scott Wakelingc34dba72016-10-03 10:14:44 +01006031 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
6032 instruction->IsStringCharAt();
6033 HInstruction* array_instr = instruction->GetArray();
6034 bool has_intermediate_address = array_instr->IsIntermediateAddress();
Scott Wakelingc34dba72016-10-03 10:14:44 +01006035
6036 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006037 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006038 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006039 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006040 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006041 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006042 case DataType::Type::kInt32: {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006043 vixl32::Register length;
6044 if (maybe_compressed_char_at) {
6045 length = RegisterFrom(locations->GetTemp(0));
6046 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
6047 GetAssembler()->LoadFromOffset(kLoadWord, length, obj, count_offset);
6048 codegen_->MaybeRecordImplicitNullCheck(instruction);
6049 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006050 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006051 int32_t const_index = Int32ConstantFrom(index);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006052 if (maybe_compressed_char_at) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006053 vixl32::Label uncompressed_load, done;
Anton Kirilov6f644202017-02-27 18:29:45 +00006054 vixl32::Label* final_label = codegen_->GetFinalLabel(instruction, &done);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006055 __ Lsrs(length, length, 1u); // LSRS has a 16-bit encoding, TST (immediate) does not.
6056 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
6057 "Expecting 0=compressed, 1=uncompressed");
Artem Serov517d9f62016-12-12 15:51:15 +00006058 __ B(cs, &uncompressed_load, /* far_target */ false);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006059 GetAssembler()->LoadFromOffset(kLoadUnsignedByte,
6060 RegisterFrom(out_loc),
6061 obj,
6062 data_offset + const_index);
Anton Kirilov6f644202017-02-27 18:29:45 +00006063 __ B(final_label);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006064 __ Bind(&uncompressed_load);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006065 GetAssembler()->LoadFromOffset(GetLoadOperandType(DataType::Type::kUint16),
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006066 RegisterFrom(out_loc),
6067 obj,
6068 data_offset + (const_index << 1));
Anton Kirilov6f644202017-02-27 18:29:45 +00006069 if (done.IsReferenced()) {
6070 __ Bind(&done);
6071 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006072 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006073 uint32_t full_offset = data_offset + (const_index << DataType::SizeShift(type));
Scott Wakelingc34dba72016-10-03 10:14:44 +01006074
6075 LoadOperandType load_type = GetLoadOperandType(type);
6076 GetAssembler()->LoadFromOffset(load_type, RegisterFrom(out_loc), obj, full_offset);
6077 }
6078 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006079 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006080 vixl32::Register temp = temps.Acquire();
6081
6082 if (has_intermediate_address) {
Artem Serov2bbc9532016-10-21 11:51:50 +01006083 // We do not need to compute the intermediate address from the array: the
6084 // input instruction has done it already. See the comment in
6085 // `TryExtractArrayAccessAddress()`.
6086 if (kIsDebugBuild) {
6087 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
Anton Kirilov644032c2016-12-06 17:51:43 +00006088 DCHECK_EQ(Uint64ConstantFrom(tmp->GetOffset()), data_offset);
Artem Serov2bbc9532016-10-21 11:51:50 +01006089 }
6090 temp = obj;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006091 } else {
6092 __ Add(temp, obj, data_offset);
6093 }
6094 if (maybe_compressed_char_at) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006095 vixl32::Label uncompressed_load, done;
Anton Kirilov6f644202017-02-27 18:29:45 +00006096 vixl32::Label* final_label = codegen_->GetFinalLabel(instruction, &done);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006097 __ Lsrs(length, length, 1u); // LSRS has a 16-bit encoding, TST (immediate) does not.
6098 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
6099 "Expecting 0=compressed, 1=uncompressed");
Artem Serov517d9f62016-12-12 15:51:15 +00006100 __ B(cs, &uncompressed_load, /* far_target */ false);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006101 __ Ldrb(RegisterFrom(out_loc), MemOperand(temp, RegisterFrom(index), vixl32::LSL, 0));
Anton Kirilov6f644202017-02-27 18:29:45 +00006102 __ B(final_label);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006103 __ Bind(&uncompressed_load);
6104 __ Ldrh(RegisterFrom(out_loc), MemOperand(temp, RegisterFrom(index), vixl32::LSL, 1));
Anton Kirilov6f644202017-02-27 18:29:45 +00006105 if (done.IsReferenced()) {
6106 __ Bind(&done);
6107 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006108 } else {
6109 codegen_->LoadFromShiftedRegOffset(type, out_loc, temp, RegisterFrom(index));
6110 }
6111 }
6112 break;
6113 }
6114
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006115 case DataType::Type::kReference: {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006116 // The read barrier instrumentation of object ArrayGet
6117 // instructions does not support the HIntermediateAddress
6118 // instruction.
6119 DCHECK(!(has_intermediate_address && kEmitCompilerReadBarrier));
6120
Scott Wakelingc34dba72016-10-03 10:14:44 +01006121 static_assert(
6122 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6123 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
6124 // /* HeapReference<Object> */ out =
6125 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
6126 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006127 Location temp = locations->GetTemp(0);
6128 // Note that a potential implicit null check is handled in this
6129 // CodeGeneratorARMVIXL::GenerateArrayLoadWithBakerReadBarrier call.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01006130 DCHECK(!instruction->CanDoImplicitNullCheckOn(instruction->InputAt(0)));
6131 if (index.IsConstant()) {
6132 // Array load with a constant index can be treated as a field load.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006133 data_offset += Int32ConstantFrom(index) << DataType::SizeShift(type);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01006134 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6135 out_loc,
6136 obj,
6137 data_offset,
6138 locations->GetTemp(0),
6139 /* needs_null_check */ false);
6140 } else {
6141 codegen_->GenerateArrayLoadWithBakerReadBarrier(
6142 instruction, out_loc, obj, data_offset, index, temp, /* needs_null_check */ false);
6143 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006144 } else {
6145 vixl32::Register out = OutputRegister(instruction);
6146 if (index.IsConstant()) {
6147 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00006148 (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006149 GetAssembler()->LoadFromOffset(kLoadWord, out, obj, offset);
Alexandre Rames374ddf32016-11-04 10:40:49 +00006150 // TODO(VIXL): Here and for other calls to `MaybeRecordImplicitNullCheck` in this method,
6151 // we should use a scope and the assembler to emit the load instruction to guarantee that
6152 // we record the pc at the correct position. But the `Assembler` does not automatically
6153 // handle unencodable offsets. Practically, everything is fine because the helper and
6154 // VIXL, at the time of writing, do generate the store instruction last.
Scott Wakelingc34dba72016-10-03 10:14:44 +01006155 codegen_->MaybeRecordImplicitNullCheck(instruction);
6156 // If read barriers are enabled, emit read barriers other than
6157 // Baker's using a slow path (and also unpoison the loaded
6158 // reference, if heap poisoning is enabled).
6159 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
6160 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006161 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006162 vixl32::Register temp = temps.Acquire();
6163
6164 if (has_intermediate_address) {
Artem Serov2bbc9532016-10-21 11:51:50 +01006165 // We do not need to compute the intermediate address from the array: the
6166 // input instruction has done it already. See the comment in
6167 // `TryExtractArrayAccessAddress()`.
6168 if (kIsDebugBuild) {
6169 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
Anton Kirilov644032c2016-12-06 17:51:43 +00006170 DCHECK_EQ(Uint64ConstantFrom(tmp->GetOffset()), data_offset);
Artem Serov2bbc9532016-10-21 11:51:50 +01006171 }
6172 temp = obj;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006173 } else {
6174 __ Add(temp, obj, data_offset);
6175 }
6176 codegen_->LoadFromShiftedRegOffset(type, out_loc, temp, RegisterFrom(index));
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006177 temps.Close();
Alexandre Rames374ddf32016-11-04 10:40:49 +00006178 // TODO(VIXL): Use a scope to ensure that we record the pc position immediately after the
6179 // load instruction. Practically, everything is fine because the helper and VIXL, at the
6180 // time of writing, do generate the store instruction last.
Scott Wakelingc34dba72016-10-03 10:14:44 +01006181 codegen_->MaybeRecordImplicitNullCheck(instruction);
6182 // If read barriers are enabled, emit read barriers other than
6183 // Baker's using a slow path (and also unpoison the loaded
6184 // reference, if heap poisoning is enabled).
6185 codegen_->MaybeGenerateReadBarrierSlow(
6186 instruction, out_loc, out_loc, obj_loc, data_offset, index);
6187 }
6188 }
6189 break;
6190 }
6191
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006192 case DataType::Type::kInt64: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006193 if (index.IsConstant()) {
6194 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00006195 (Int32ConstantFrom(index) << TIMES_8) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006196 GetAssembler()->LoadFromOffset(kLoadWordPair, LowRegisterFrom(out_loc), obj, offset);
6197 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006198 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006199 vixl32::Register temp = temps.Acquire();
6200 __ Add(temp, obj, Operand(RegisterFrom(index), vixl32::LSL, TIMES_8));
6201 GetAssembler()->LoadFromOffset(kLoadWordPair, LowRegisterFrom(out_loc), temp, data_offset);
6202 }
6203 break;
6204 }
6205
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006206 case DataType::Type::kFloat32: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006207 vixl32::SRegister out = SRegisterFrom(out_loc);
6208 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006209 size_t offset = (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006210 GetAssembler()->LoadSFromOffset(out, obj, offset);
6211 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006212 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006213 vixl32::Register temp = temps.Acquire();
6214 __ Add(temp, obj, Operand(RegisterFrom(index), vixl32::LSL, TIMES_4));
6215 GetAssembler()->LoadSFromOffset(out, temp, data_offset);
6216 }
6217 break;
6218 }
6219
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006220 case DataType::Type::kFloat64: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006221 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006222 size_t offset = (Int32ConstantFrom(index) << TIMES_8) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006223 GetAssembler()->LoadDFromOffset(DRegisterFrom(out_loc), obj, offset);
6224 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006225 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006226 vixl32::Register temp = temps.Acquire();
6227 __ Add(temp, obj, Operand(RegisterFrom(index), vixl32::LSL, TIMES_8));
6228 GetAssembler()->LoadDFromOffset(DRegisterFrom(out_loc), temp, data_offset);
6229 }
6230 break;
6231 }
6232
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006233 case DataType::Type::kVoid:
Scott Wakelingc34dba72016-10-03 10:14:44 +01006234 LOG(FATAL) << "Unreachable type " << type;
6235 UNREACHABLE();
6236 }
6237
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006238 if (type == DataType::Type::kReference) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006239 // Potential implicit null checks, in the case of reference
6240 // arrays, are handled in the previous switch statement.
6241 } else if (!maybe_compressed_char_at) {
Alexandre Rames374ddf32016-11-04 10:40:49 +00006242 // TODO(VIXL): Use a scope to ensure we record the pc info immediately after
6243 // the preceding load instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01006244 codegen_->MaybeRecordImplicitNullCheck(instruction);
6245 }
6246}
6247
6248void LocationsBuilderARMVIXL::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006249 DataType::Type value_type = instruction->GetComponentType();
Scott Wakelingc34dba72016-10-03 10:14:44 +01006250
6251 bool needs_write_barrier =
6252 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
6253 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
6254
Vladimir Markoca6fff82017-10-03 14:49:14 +01006255 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Scott Wakelingc34dba72016-10-03 10:14:44 +01006256 instruction,
6257 may_need_runtime_call_for_type_check ?
6258 LocationSummary::kCallOnSlowPath :
6259 LocationSummary::kNoCall);
6260
6261 locations->SetInAt(0, Location::RequiresRegister());
6262 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006263 if (DataType::IsFloatingPointType(value_type)) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006264 locations->SetInAt(2, Location::RequiresFpuRegister());
6265 } else {
6266 locations->SetInAt(2, Location::RequiresRegister());
6267 }
6268 if (needs_write_barrier) {
6269 // Temporary registers for the write barrier.
6270 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
6271 locations->AddTemp(Location::RequiresRegister());
6272 }
6273}
6274
6275void InstructionCodeGeneratorARMVIXL::VisitArraySet(HArraySet* instruction) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006276 LocationSummary* locations = instruction->GetLocations();
6277 vixl32::Register array = InputRegisterAt(instruction, 0);
6278 Location index = locations->InAt(1);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006279 DataType::Type value_type = instruction->GetComponentType();
Scott Wakelingc34dba72016-10-03 10:14:44 +01006280 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
6281 bool needs_write_barrier =
6282 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
6283 uint32_t data_offset =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006284 mirror::Array::DataOffset(DataType::Size(value_type)).Uint32Value();
Scott Wakelingc34dba72016-10-03 10:14:44 +01006285 Location value_loc = locations->InAt(2);
6286 HInstruction* array_instr = instruction->GetArray();
6287 bool has_intermediate_address = array_instr->IsIntermediateAddress();
Scott Wakelingc34dba72016-10-03 10:14:44 +01006288
6289 switch (value_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006290 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006291 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006292 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006293 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006294 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006295 case DataType::Type::kInt32: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006296 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006297 int32_t const_index = Int32ConstantFrom(index);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006298 uint32_t full_offset =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006299 data_offset + (const_index << DataType::SizeShift(value_type));
Scott Wakelingc34dba72016-10-03 10:14:44 +01006300 StoreOperandType store_type = GetStoreOperandType(value_type);
6301 GetAssembler()->StoreToOffset(store_type, RegisterFrom(value_loc), array, full_offset);
6302 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006303 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006304 vixl32::Register temp = temps.Acquire();
6305
6306 if (has_intermediate_address) {
Artem Serov2bbc9532016-10-21 11:51:50 +01006307 // We do not need to compute the intermediate address from the array: the
6308 // input instruction has done it already. See the comment in
6309 // `TryExtractArrayAccessAddress()`.
6310 if (kIsDebugBuild) {
6311 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
Anton Kirilov644032c2016-12-06 17:51:43 +00006312 DCHECK_EQ(Uint64ConstantFrom(tmp->GetOffset()), data_offset);
Artem Serov2bbc9532016-10-21 11:51:50 +01006313 }
6314 temp = array;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006315 } else {
6316 __ Add(temp, array, data_offset);
6317 }
6318 codegen_->StoreToShiftedRegOffset(value_type, value_loc, temp, RegisterFrom(index));
6319 }
6320 break;
6321 }
6322
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006323 case DataType::Type::kReference: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006324 vixl32::Register value = RegisterFrom(value_loc);
6325 // TryExtractArrayAccessAddress optimization is never applied for non-primitive ArraySet.
6326 // See the comment in instruction_simplifier_shared.cc.
6327 DCHECK(!has_intermediate_address);
6328
6329 if (instruction->InputAt(2)->IsNullConstant()) {
6330 // Just setting null.
6331 if (index.IsConstant()) {
6332 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00006333 (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006334 GetAssembler()->StoreToOffset(kStoreWord, value, array, offset);
6335 } else {
6336 DCHECK(index.IsRegister()) << index;
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006337 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006338 vixl32::Register temp = temps.Acquire();
6339 __ Add(temp, array, data_offset);
6340 codegen_->StoreToShiftedRegOffset(value_type, value_loc, temp, RegisterFrom(index));
6341 }
Alexandre Rames374ddf32016-11-04 10:40:49 +00006342 // TODO(VIXL): Use a scope to ensure we record the pc info immediately after the preceding
6343 // store instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01006344 codegen_->MaybeRecordImplicitNullCheck(instruction);
6345 DCHECK(!needs_write_barrier);
6346 DCHECK(!may_need_runtime_call_for_type_check);
6347 break;
6348 }
6349
6350 DCHECK(needs_write_barrier);
6351 Location temp1_loc = locations->GetTemp(0);
6352 vixl32::Register temp1 = RegisterFrom(temp1_loc);
6353 Location temp2_loc = locations->GetTemp(1);
6354 vixl32::Register temp2 = RegisterFrom(temp2_loc);
6355 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6356 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6357 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6358 vixl32::Label done;
Anton Kirilov6f644202017-02-27 18:29:45 +00006359 vixl32::Label* final_label = codegen_->GetFinalLabel(instruction, &done);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006360 SlowPathCodeARMVIXL* slow_path = nullptr;
6361
6362 if (may_need_runtime_call_for_type_check) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01006363 slow_path = new (codegen_->GetScopedAllocator()) ArraySetSlowPathARMVIXL(instruction);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006364 codegen_->AddSlowPath(slow_path);
6365 if (instruction->GetValueCanBeNull()) {
6366 vixl32::Label non_zero;
xueliang.zhongf51bc622016-11-04 09:23:32 +00006367 __ CompareAndBranchIfNonZero(value, &non_zero);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006368 if (index.IsConstant()) {
6369 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00006370 (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006371 GetAssembler()->StoreToOffset(kStoreWord, value, array, offset);
6372 } else {
6373 DCHECK(index.IsRegister()) << index;
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006374 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006375 vixl32::Register temp = temps.Acquire();
6376 __ Add(temp, array, data_offset);
6377 codegen_->StoreToShiftedRegOffset(value_type, value_loc, temp, RegisterFrom(index));
6378 }
Alexandre Rames374ddf32016-11-04 10:40:49 +00006379 // TODO(VIXL): Use a scope to ensure we record the pc info immediately after the preceding
6380 // store instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01006381 codegen_->MaybeRecordImplicitNullCheck(instruction);
Anton Kirilov6f644202017-02-27 18:29:45 +00006382 __ B(final_label);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006383 __ Bind(&non_zero);
6384 }
6385
6386 // Note that when read barriers are enabled, the type checks
6387 // are performed without read barriers. This is fine, even in
6388 // the case where a class object is in the from-space after
6389 // the flip, as a comparison involving such a type would not
6390 // produce a false positive; it may of course produce a false
6391 // negative, in which case we would take the ArraySet slow
6392 // path.
6393
Alexandre Rames374ddf32016-11-04 10:40:49 +00006394 {
6395 // Ensure we record the pc position immediately after the `ldr` instruction.
Artem Serov0fb37192016-12-06 18:13:40 +00006396 ExactAssemblyScope aas(GetVIXLAssembler(),
6397 vixl32::kMaxInstructionSizeInBytes,
6398 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00006399 // /* HeapReference<Class> */ temp1 = array->klass_
6400 __ ldr(temp1, MemOperand(array, class_offset));
6401 codegen_->MaybeRecordImplicitNullCheck(instruction);
6402 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006403 GetAssembler()->MaybeUnpoisonHeapReference(temp1);
6404
6405 // /* HeapReference<Class> */ temp1 = temp1->component_type_
6406 GetAssembler()->LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
6407 // /* HeapReference<Class> */ temp2 = value->klass_
6408 GetAssembler()->LoadFromOffset(kLoadWord, temp2, value, class_offset);
6409 // If heap poisoning is enabled, no need to unpoison `temp1`
6410 // nor `temp2`, as we are comparing two poisoned references.
6411 __ Cmp(temp1, temp2);
6412
6413 if (instruction->StaticTypeOfArrayIsObjectArray()) {
6414 vixl32::Label do_put;
Artem Serov517d9f62016-12-12 15:51:15 +00006415 __ B(eq, &do_put, /* far_target */ false);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006416 // If heap poisoning is enabled, the `temp1` reference has
6417 // not been unpoisoned yet; unpoison it now.
6418 GetAssembler()->MaybeUnpoisonHeapReference(temp1);
6419
6420 // /* HeapReference<Class> */ temp1 = temp1->super_class_
6421 GetAssembler()->LoadFromOffset(kLoadWord, temp1, temp1, super_offset);
6422 // If heap poisoning is enabled, no need to unpoison
6423 // `temp1`, as we are comparing against null below.
xueliang.zhongf51bc622016-11-04 09:23:32 +00006424 __ CompareAndBranchIfNonZero(temp1, slow_path->GetEntryLabel());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006425 __ Bind(&do_put);
6426 } else {
6427 __ B(ne, slow_path->GetEntryLabel());
6428 }
6429 }
6430
6431 vixl32::Register source = value;
6432 if (kPoisonHeapReferences) {
6433 // Note that in the case where `value` is a null reference,
6434 // we do not enter this block, as a null reference does not
6435 // need poisoning.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006436 DCHECK_EQ(value_type, DataType::Type::kReference);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006437 __ Mov(temp1, value);
6438 GetAssembler()->PoisonHeapReference(temp1);
6439 source = temp1;
6440 }
6441
6442 if (index.IsConstant()) {
6443 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00006444 (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006445 GetAssembler()->StoreToOffset(kStoreWord, source, array, offset);
6446 } else {
6447 DCHECK(index.IsRegister()) << index;
6448
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006449 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006450 vixl32::Register temp = temps.Acquire();
6451 __ Add(temp, array, data_offset);
6452 codegen_->StoreToShiftedRegOffset(value_type,
6453 LocationFrom(source),
6454 temp,
6455 RegisterFrom(index));
6456 }
6457
6458 if (!may_need_runtime_call_for_type_check) {
Alexandre Rames374ddf32016-11-04 10:40:49 +00006459 // TODO(VIXL): Ensure we record the pc position immediately after the preceding store
6460 // instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01006461 codegen_->MaybeRecordImplicitNullCheck(instruction);
6462 }
6463
6464 codegen_->MarkGCCard(temp1, temp2, array, value, instruction->GetValueCanBeNull());
6465
6466 if (done.IsReferenced()) {
6467 __ Bind(&done);
6468 }
6469
6470 if (slow_path != nullptr) {
6471 __ Bind(slow_path->GetExitLabel());
6472 }
6473
6474 break;
6475 }
6476
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006477 case DataType::Type::kInt64: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006478 Location value = locations->InAt(2);
6479 if (index.IsConstant()) {
6480 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00006481 (Int32ConstantFrom(index) << TIMES_8) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006482 GetAssembler()->StoreToOffset(kStoreWordPair, LowRegisterFrom(value), array, offset);
6483 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006484 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006485 vixl32::Register temp = temps.Acquire();
6486 __ Add(temp, array, Operand(RegisterFrom(index), vixl32::LSL, TIMES_8));
6487 GetAssembler()->StoreToOffset(kStoreWordPair, LowRegisterFrom(value), temp, data_offset);
6488 }
6489 break;
6490 }
6491
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006492 case DataType::Type::kFloat32: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006493 Location value = locations->InAt(2);
6494 DCHECK(value.IsFpuRegister());
6495 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006496 size_t offset = (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006497 GetAssembler()->StoreSToOffset(SRegisterFrom(value), array, offset);
6498 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006499 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006500 vixl32::Register temp = temps.Acquire();
6501 __ Add(temp, array, Operand(RegisterFrom(index), vixl32::LSL, TIMES_4));
6502 GetAssembler()->StoreSToOffset(SRegisterFrom(value), temp, data_offset);
6503 }
6504 break;
6505 }
6506
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006507 case DataType::Type::kFloat64: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006508 Location value = locations->InAt(2);
6509 DCHECK(value.IsFpuRegisterPair());
6510 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006511 size_t offset = (Int32ConstantFrom(index) << TIMES_8) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006512 GetAssembler()->StoreDToOffset(DRegisterFrom(value), array, offset);
6513 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006514 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006515 vixl32::Register temp = temps.Acquire();
6516 __ Add(temp, array, Operand(RegisterFrom(index), vixl32::LSL, TIMES_8));
6517 GetAssembler()->StoreDToOffset(DRegisterFrom(value), temp, data_offset);
6518 }
6519 break;
6520 }
6521
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006522 case DataType::Type::kVoid:
Scott Wakelingc34dba72016-10-03 10:14:44 +01006523 LOG(FATAL) << "Unreachable type " << value_type;
6524 UNREACHABLE();
6525 }
6526
6527 // Objects are handled in the switch.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006528 if (value_type != DataType::Type::kReference) {
Alexandre Rames374ddf32016-11-04 10:40:49 +00006529 // TODO(VIXL): Ensure we record the pc position immediately after the preceding store
6530 // instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01006531 codegen_->MaybeRecordImplicitNullCheck(instruction);
6532 }
6533}
6534
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006535void LocationsBuilderARMVIXL::VisitArrayLength(HArrayLength* instruction) {
6536 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006537 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006538 locations->SetInAt(0, Location::RequiresRegister());
6539 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6540}
6541
6542void InstructionCodeGeneratorARMVIXL::VisitArrayLength(HArrayLength* instruction) {
6543 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
6544 vixl32::Register obj = InputRegisterAt(instruction, 0);
6545 vixl32::Register out = OutputRegister(instruction);
Alexandre Rames374ddf32016-11-04 10:40:49 +00006546 {
Artem Serov0fb37192016-12-06 18:13:40 +00006547 ExactAssemblyScope aas(GetVIXLAssembler(),
6548 vixl32::kMaxInstructionSizeInBytes,
6549 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00006550 __ ldr(out, MemOperand(obj, offset));
6551 codegen_->MaybeRecordImplicitNullCheck(instruction);
6552 }
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006553 // Mask out compression flag from String's array length.
6554 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006555 __ Lsr(out, out, 1u);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006556 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006557}
6558
Artem Serov2bbc9532016-10-21 11:51:50 +01006559void LocationsBuilderARMVIXL::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Artem Serov2bbc9532016-10-21 11:51:50 +01006560 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006561 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Artem Serov2bbc9532016-10-21 11:51:50 +01006562
6563 locations->SetInAt(0, Location::RequiresRegister());
6564 locations->SetInAt(1, Location::RegisterOrConstant(instruction->GetOffset()));
6565 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6566}
6567
6568void InstructionCodeGeneratorARMVIXL::VisitIntermediateAddress(HIntermediateAddress* instruction) {
6569 vixl32::Register out = OutputRegister(instruction);
6570 vixl32::Register first = InputRegisterAt(instruction, 0);
6571 Location second = instruction->GetLocations()->InAt(1);
6572
Artem Serov2bbc9532016-10-21 11:51:50 +01006573 if (second.IsRegister()) {
6574 __ Add(out, first, RegisterFrom(second));
6575 } else {
Anton Kirilov644032c2016-12-06 17:51:43 +00006576 __ Add(out, first, Int32ConstantFrom(second));
Artem Serov2bbc9532016-10-21 11:51:50 +01006577 }
6578}
6579
Artem Serove1811ed2017-04-27 16:50:47 +01006580void LocationsBuilderARMVIXL::VisitIntermediateAddressIndex(
6581 HIntermediateAddressIndex* instruction) {
6582 LOG(FATAL) << "Unreachable " << instruction->GetId();
6583}
6584
6585void InstructionCodeGeneratorARMVIXL::VisitIntermediateAddressIndex(
6586 HIntermediateAddressIndex* instruction) {
6587 LOG(FATAL) << "Unreachable " << instruction->GetId();
6588}
6589
Scott Wakelingc34dba72016-10-03 10:14:44 +01006590void LocationsBuilderARMVIXL::VisitBoundsCheck(HBoundsCheck* instruction) {
6591 RegisterSet caller_saves = RegisterSet::Empty();
6592 InvokeRuntimeCallingConventionARMVIXL calling_convention;
6593 caller_saves.Add(LocationFrom(calling_convention.GetRegisterAt(0)));
6594 caller_saves.Add(LocationFrom(calling_convention.GetRegisterAt(1)));
6595 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Artem Serov2dd053d2017-03-08 14:54:06 +00006596
6597 HInstruction* index = instruction->InputAt(0);
6598 HInstruction* length = instruction->InputAt(1);
6599 // If both index and length are constants we can statically check the bounds. But if at least one
6600 // of them is not encodable ArmEncodableConstantOrRegister will create
6601 // Location::RequiresRegister() which is not desired to happen. Instead we create constant
6602 // locations.
6603 bool both_const = index->IsConstant() && length->IsConstant();
6604 locations->SetInAt(0, both_const
6605 ? Location::ConstantLocation(index->AsConstant())
6606 : ArmEncodableConstantOrRegister(index, CMP));
6607 locations->SetInAt(1, both_const
6608 ? Location::ConstantLocation(length->AsConstant())
6609 : ArmEncodableConstantOrRegister(length, CMP));
Scott Wakelingc34dba72016-10-03 10:14:44 +01006610}
6611
6612void InstructionCodeGeneratorARMVIXL::VisitBoundsCheck(HBoundsCheck* instruction) {
Artem Serov2dd053d2017-03-08 14:54:06 +00006613 LocationSummary* locations = instruction->GetLocations();
6614 Location index_loc = locations->InAt(0);
6615 Location length_loc = locations->InAt(1);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006616
Artem Serov2dd053d2017-03-08 14:54:06 +00006617 if (length_loc.IsConstant()) {
6618 int32_t length = Int32ConstantFrom(length_loc);
6619 if (index_loc.IsConstant()) {
6620 // BCE will remove the bounds check if we are guaranteed to pass.
6621 int32_t index = Int32ConstantFrom(index_loc);
6622 if (index < 0 || index >= length) {
6623 SlowPathCodeARMVIXL* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006624 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathARMVIXL(instruction);
Artem Serov2dd053d2017-03-08 14:54:06 +00006625 codegen_->AddSlowPath(slow_path);
6626 __ B(slow_path->GetEntryLabel());
6627 } else {
6628 // Some optimization after BCE may have generated this, and we should not
6629 // generate a bounds check if it is a valid range.
6630 }
6631 return;
6632 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006633
Artem Serov2dd053d2017-03-08 14:54:06 +00006634 SlowPathCodeARMVIXL* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006635 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathARMVIXL(instruction);
Artem Serov2dd053d2017-03-08 14:54:06 +00006636 __ Cmp(RegisterFrom(index_loc), length);
6637 codegen_->AddSlowPath(slow_path);
6638 __ B(hs, slow_path->GetEntryLabel());
6639 } else {
6640 SlowPathCodeARMVIXL* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006641 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathARMVIXL(instruction);
Artem Serov2dd053d2017-03-08 14:54:06 +00006642 __ Cmp(RegisterFrom(length_loc), InputOperandAt(instruction, 0));
6643 codegen_->AddSlowPath(slow_path);
6644 __ B(ls, slow_path->GetEntryLabel());
6645 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006646}
6647
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006648void CodeGeneratorARMVIXL::MarkGCCard(vixl32::Register temp,
6649 vixl32::Register card,
6650 vixl32::Register object,
6651 vixl32::Register value,
6652 bool can_be_null) {
6653 vixl32::Label is_null;
6654 if (can_be_null) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00006655 __ CompareAndBranchIfZero(value, &is_null);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006656 }
6657 GetAssembler()->LoadFromOffset(
6658 kLoadWord, card, tr, Thread::CardTableOffset<kArmPointerSize>().Int32Value());
Scott Wakelingb77051e2016-11-21 19:46:00 +00006659 __ Lsr(temp, object, Operand::From(gc::accounting::CardTable::kCardShift));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006660 __ Strb(card, MemOperand(card, temp));
6661 if (can_be_null) {
6662 __ Bind(&is_null);
6663 }
6664}
6665
Scott Wakelingfe885462016-09-22 10:24:38 +01006666void LocationsBuilderARMVIXL::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
6667 LOG(FATAL) << "Unreachable";
6668}
6669
6670void InstructionCodeGeneratorARMVIXL::VisitParallelMove(HParallelMove* instruction) {
Vladimir Markobea75ff2017-10-11 20:39:54 +01006671 if (instruction->GetNext()->IsSuspendCheck() &&
6672 instruction->GetBlock()->GetLoopInformation() != nullptr) {
6673 HSuspendCheck* suspend_check = instruction->GetNext()->AsSuspendCheck();
6674 // The back edge will generate the suspend check.
6675 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(suspend_check, instruction);
6676 }
6677
Scott Wakelingfe885462016-09-22 10:24:38 +01006678 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
6679}
6680
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006681void LocationsBuilderARMVIXL::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006682 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6683 instruction, LocationSummary::kCallOnSlowPath);
Artem Serov657022c2016-11-23 14:19:38 +00006684 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006685}
6686
6687void InstructionCodeGeneratorARMVIXL::VisitSuspendCheck(HSuspendCheck* instruction) {
6688 HBasicBlock* block = instruction->GetBlock();
6689 if (block->GetLoopInformation() != nullptr) {
6690 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
6691 // The back edge will generate the suspend check.
6692 return;
6693 }
6694 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
6695 // The goto will generate the suspend check.
6696 return;
6697 }
6698 GenerateSuspendCheck(instruction, nullptr);
Roland Levillain5daa4952017-07-03 17:23:56 +01006699 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 12);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006700}
6701
6702void InstructionCodeGeneratorARMVIXL::GenerateSuspendCheck(HSuspendCheck* instruction,
6703 HBasicBlock* successor) {
6704 SuspendCheckSlowPathARMVIXL* slow_path =
6705 down_cast<SuspendCheckSlowPathARMVIXL*>(instruction->GetSlowPath());
6706 if (slow_path == nullptr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006707 slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006708 new (codegen_->GetScopedAllocator()) SuspendCheckSlowPathARMVIXL(instruction, successor);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006709 instruction->SetSlowPath(slow_path);
6710 codegen_->AddSlowPath(slow_path);
6711 if (successor != nullptr) {
6712 DCHECK(successor->IsLoopHeader());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006713 }
6714 } else {
6715 DCHECK_EQ(slow_path->GetSuccessor(), successor);
6716 }
6717
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006718 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006719 vixl32::Register temp = temps.Acquire();
6720 GetAssembler()->LoadFromOffset(
6721 kLoadUnsignedHalfword, temp, tr, Thread::ThreadFlagsOffset<kArmPointerSize>().Int32Value());
6722 if (successor == nullptr) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00006723 __ CompareAndBranchIfNonZero(temp, slow_path->GetEntryLabel());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006724 __ Bind(slow_path->GetReturnLabel());
6725 } else {
xueliang.zhongf51bc622016-11-04 09:23:32 +00006726 __ CompareAndBranchIfZero(temp, codegen_->GetLabelOf(successor));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006727 __ B(slow_path->GetEntryLabel());
6728 }
6729}
6730
Scott Wakelingfe885462016-09-22 10:24:38 +01006731ArmVIXLAssembler* ParallelMoveResolverARMVIXL::GetAssembler() const {
6732 return codegen_->GetAssembler();
6733}
6734
6735void ParallelMoveResolverARMVIXL::EmitMove(size_t index) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006736 UseScratchRegisterScope temps(GetAssembler()->GetVIXLAssembler());
Scott Wakelingfe885462016-09-22 10:24:38 +01006737 MoveOperands* move = moves_[index];
6738 Location source = move->GetSource();
6739 Location destination = move->GetDestination();
6740
6741 if (source.IsRegister()) {
6742 if (destination.IsRegister()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006743 __ Mov(RegisterFrom(destination), RegisterFrom(source));
Scott Wakelingfe885462016-09-22 10:24:38 +01006744 } else if (destination.IsFpuRegister()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006745 __ Vmov(SRegisterFrom(destination), RegisterFrom(source));
Scott Wakelingfe885462016-09-22 10:24:38 +01006746 } else {
6747 DCHECK(destination.IsStackSlot());
6748 GetAssembler()->StoreToOffset(kStoreWord,
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006749 RegisterFrom(source),
Scott Wakelingfe885462016-09-22 10:24:38 +01006750 sp,
6751 destination.GetStackIndex());
6752 }
6753 } else if (source.IsStackSlot()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006754 if (destination.IsRegister()) {
6755 GetAssembler()->LoadFromOffset(kLoadWord,
6756 RegisterFrom(destination),
6757 sp,
6758 source.GetStackIndex());
6759 } else if (destination.IsFpuRegister()) {
6760 GetAssembler()->LoadSFromOffset(SRegisterFrom(destination), sp, source.GetStackIndex());
6761 } else {
6762 DCHECK(destination.IsStackSlot());
6763 vixl32::Register temp = temps.Acquire();
6764 GetAssembler()->LoadFromOffset(kLoadWord, temp, sp, source.GetStackIndex());
6765 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
6766 }
Scott Wakelingfe885462016-09-22 10:24:38 +01006767 } else if (source.IsFpuRegister()) {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01006768 if (destination.IsRegister()) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006769 __ Vmov(RegisterFrom(destination), SRegisterFrom(source));
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01006770 } else if (destination.IsFpuRegister()) {
6771 __ Vmov(SRegisterFrom(destination), SRegisterFrom(source));
6772 } else {
6773 DCHECK(destination.IsStackSlot());
6774 GetAssembler()->StoreSToOffset(SRegisterFrom(source), sp, destination.GetStackIndex());
6775 }
Scott Wakelingfe885462016-09-22 10:24:38 +01006776 } else if (source.IsDoubleStackSlot()) {
Alexandre Rames9c19bd62016-10-24 11:50:32 +01006777 if (destination.IsDoubleStackSlot()) {
6778 vixl32::DRegister temp = temps.AcquireD();
6779 GetAssembler()->LoadDFromOffset(temp, sp, source.GetStackIndex());
6780 GetAssembler()->StoreDToOffset(temp, sp, destination.GetStackIndex());
6781 } else if (destination.IsRegisterPair()) {
6782 DCHECK(ExpectedPairLayout(destination));
6783 GetAssembler()->LoadFromOffset(
6784 kLoadWordPair, LowRegisterFrom(destination), sp, source.GetStackIndex());
6785 } else {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01006786 DCHECK(destination.IsFpuRegisterPair()) << destination;
6787 GetAssembler()->LoadDFromOffset(DRegisterFrom(destination), sp, source.GetStackIndex());
Alexandre Rames9c19bd62016-10-24 11:50:32 +01006788 }
Scott Wakelingfe885462016-09-22 10:24:38 +01006789 } else if (source.IsRegisterPair()) {
6790 if (destination.IsRegisterPair()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006791 __ Mov(LowRegisterFrom(destination), LowRegisterFrom(source));
6792 __ Mov(HighRegisterFrom(destination), HighRegisterFrom(source));
Scott Wakelingfe885462016-09-22 10:24:38 +01006793 } else if (destination.IsFpuRegisterPair()) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006794 __ Vmov(DRegisterFrom(destination), LowRegisterFrom(source), HighRegisterFrom(source));
Scott Wakelingfe885462016-09-22 10:24:38 +01006795 } else {
6796 DCHECK(destination.IsDoubleStackSlot()) << destination;
6797 DCHECK(ExpectedPairLayout(source));
6798 GetAssembler()->StoreToOffset(kStoreWordPair,
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006799 LowRegisterFrom(source),
Scott Wakelingfe885462016-09-22 10:24:38 +01006800 sp,
6801 destination.GetStackIndex());
6802 }
6803 } else if (source.IsFpuRegisterPair()) {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01006804 if (destination.IsRegisterPair()) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006805 __ Vmov(LowRegisterFrom(destination), HighRegisterFrom(destination), DRegisterFrom(source));
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01006806 } else if (destination.IsFpuRegisterPair()) {
6807 __ Vmov(DRegisterFrom(destination), DRegisterFrom(source));
6808 } else {
6809 DCHECK(destination.IsDoubleStackSlot()) << destination;
6810 GetAssembler()->StoreDToOffset(DRegisterFrom(source), sp, destination.GetStackIndex());
6811 }
Scott Wakelingfe885462016-09-22 10:24:38 +01006812 } else {
6813 DCHECK(source.IsConstant()) << source;
6814 HConstant* constant = source.GetConstant();
6815 if (constant->IsIntConstant() || constant->IsNullConstant()) {
6816 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
6817 if (destination.IsRegister()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006818 __ Mov(RegisterFrom(destination), value);
Scott Wakelingfe885462016-09-22 10:24:38 +01006819 } else {
6820 DCHECK(destination.IsStackSlot());
Scott Wakelingfe885462016-09-22 10:24:38 +01006821 vixl32::Register temp = temps.Acquire();
6822 __ Mov(temp, value);
6823 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
6824 }
6825 } else if (constant->IsLongConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006826 int64_t value = Int64ConstantFrom(source);
Scott Wakelingfe885462016-09-22 10:24:38 +01006827 if (destination.IsRegisterPair()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006828 __ Mov(LowRegisterFrom(destination), Low32Bits(value));
6829 __ Mov(HighRegisterFrom(destination), High32Bits(value));
Scott Wakelingfe885462016-09-22 10:24:38 +01006830 } else {
6831 DCHECK(destination.IsDoubleStackSlot()) << destination;
Scott Wakelingfe885462016-09-22 10:24:38 +01006832 vixl32::Register temp = temps.Acquire();
6833 __ Mov(temp, Low32Bits(value));
6834 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
6835 __ Mov(temp, High32Bits(value));
6836 GetAssembler()->StoreToOffset(kStoreWord,
6837 temp,
6838 sp,
6839 destination.GetHighStackIndex(kArmWordSize));
6840 }
6841 } else if (constant->IsDoubleConstant()) {
6842 double value = constant->AsDoubleConstant()->GetValue();
6843 if (destination.IsFpuRegisterPair()) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006844 __ Vmov(DRegisterFrom(destination), value);
Scott Wakelingfe885462016-09-22 10:24:38 +01006845 } else {
6846 DCHECK(destination.IsDoubleStackSlot()) << destination;
6847 uint64_t int_value = bit_cast<uint64_t, double>(value);
Scott Wakelingfe885462016-09-22 10:24:38 +01006848 vixl32::Register temp = temps.Acquire();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006849 __ Mov(temp, Low32Bits(int_value));
Scott Wakelingfe885462016-09-22 10:24:38 +01006850 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006851 __ Mov(temp, High32Bits(int_value));
Scott Wakelingfe885462016-09-22 10:24:38 +01006852 GetAssembler()->StoreToOffset(kStoreWord,
6853 temp,
6854 sp,
6855 destination.GetHighStackIndex(kArmWordSize));
6856 }
6857 } else {
6858 DCHECK(constant->IsFloatConstant()) << constant->DebugName();
6859 float value = constant->AsFloatConstant()->GetValue();
6860 if (destination.IsFpuRegister()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006861 __ Vmov(SRegisterFrom(destination), value);
Scott Wakelingfe885462016-09-22 10:24:38 +01006862 } else {
6863 DCHECK(destination.IsStackSlot());
Scott Wakelingfe885462016-09-22 10:24:38 +01006864 vixl32::Register temp = temps.Acquire();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006865 __ Mov(temp, bit_cast<int32_t, float>(value));
Scott Wakelingfe885462016-09-22 10:24:38 +01006866 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
6867 }
6868 }
6869 }
6870}
6871
Alexandre Rames9c19bd62016-10-24 11:50:32 +01006872void ParallelMoveResolverARMVIXL::Exchange(vixl32::Register reg, int mem) {
6873 UseScratchRegisterScope temps(GetAssembler()->GetVIXLAssembler());
6874 vixl32::Register temp = temps.Acquire();
6875 __ Mov(temp, reg);
6876 GetAssembler()->LoadFromOffset(kLoadWord, reg, sp, mem);
6877 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, mem);
Scott Wakelingfe885462016-09-22 10:24:38 +01006878}
6879
Alexandre Rames9c19bd62016-10-24 11:50:32 +01006880void ParallelMoveResolverARMVIXL::Exchange(int mem1, int mem2) {
6881 // TODO(VIXL32): Double check the performance of this implementation.
6882 UseScratchRegisterScope temps(GetAssembler()->GetVIXLAssembler());
Nicolas Geoffray13a797b2017-03-15 16:41:31 +00006883 vixl32::Register temp1 = temps.Acquire();
6884 ScratchRegisterScope ensure_scratch(
6885 this, temp1.GetCode(), r0.GetCode(), codegen_->GetNumberOfCoreRegisters());
6886 vixl32::Register temp2(ensure_scratch.GetRegister());
Alexandre Rames9c19bd62016-10-24 11:50:32 +01006887
Nicolas Geoffray13a797b2017-03-15 16:41:31 +00006888 int stack_offset = ensure_scratch.IsSpilled() ? kArmWordSize : 0;
6889 GetAssembler()->LoadFromOffset(kLoadWord, temp1, sp, mem1 + stack_offset);
6890 GetAssembler()->LoadFromOffset(kLoadWord, temp2, sp, mem2 + stack_offset);
6891 GetAssembler()->StoreToOffset(kStoreWord, temp1, sp, mem2 + stack_offset);
6892 GetAssembler()->StoreToOffset(kStoreWord, temp2, sp, mem1 + stack_offset);
Scott Wakelingfe885462016-09-22 10:24:38 +01006893}
6894
Alexandre Rames9c19bd62016-10-24 11:50:32 +01006895void ParallelMoveResolverARMVIXL::EmitSwap(size_t index) {
6896 MoveOperands* move = moves_[index];
6897 Location source = move->GetSource();
6898 Location destination = move->GetDestination();
6899 UseScratchRegisterScope temps(GetAssembler()->GetVIXLAssembler());
6900
6901 if (source.IsRegister() && destination.IsRegister()) {
6902 vixl32::Register temp = temps.Acquire();
6903 DCHECK(!RegisterFrom(source).Is(temp));
6904 DCHECK(!RegisterFrom(destination).Is(temp));
6905 __ Mov(temp, RegisterFrom(destination));
6906 __ Mov(RegisterFrom(destination), RegisterFrom(source));
6907 __ Mov(RegisterFrom(source), temp);
6908 } else if (source.IsRegister() && destination.IsStackSlot()) {
6909 Exchange(RegisterFrom(source), destination.GetStackIndex());
6910 } else if (source.IsStackSlot() && destination.IsRegister()) {
6911 Exchange(RegisterFrom(destination), source.GetStackIndex());
6912 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Anton Kirilovdda43962016-11-21 19:55:20 +00006913 Exchange(source.GetStackIndex(), destination.GetStackIndex());
Alexandre Rames9c19bd62016-10-24 11:50:32 +01006914 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Nicolas Geoffray13a797b2017-03-15 16:41:31 +00006915 vixl32::Register temp = temps.Acquire();
Anton Kirilovdda43962016-11-21 19:55:20 +00006916 __ Vmov(temp, SRegisterFrom(source));
6917 __ Vmov(SRegisterFrom(source), SRegisterFrom(destination));
6918 __ Vmov(SRegisterFrom(destination), temp);
Alexandre Rames9c19bd62016-10-24 11:50:32 +01006919 } else if (source.IsRegisterPair() && destination.IsRegisterPair()) {
6920 vixl32::DRegister temp = temps.AcquireD();
6921 __ Vmov(temp, LowRegisterFrom(source), HighRegisterFrom(source));
6922 __ Mov(LowRegisterFrom(source), LowRegisterFrom(destination));
6923 __ Mov(HighRegisterFrom(source), HighRegisterFrom(destination));
6924 __ Vmov(LowRegisterFrom(destination), HighRegisterFrom(destination), temp);
6925 } else if (source.IsRegisterPair() || destination.IsRegisterPair()) {
6926 vixl32::Register low_reg = LowRegisterFrom(source.IsRegisterPair() ? source : destination);
6927 int mem = source.IsRegisterPair() ? destination.GetStackIndex() : source.GetStackIndex();
6928 DCHECK(ExpectedPairLayout(source.IsRegisterPair() ? source : destination));
6929 vixl32::DRegister temp = temps.AcquireD();
6930 __ Vmov(temp, low_reg, vixl32::Register(low_reg.GetCode() + 1));
6931 GetAssembler()->LoadFromOffset(kLoadWordPair, low_reg, sp, mem);
6932 GetAssembler()->StoreDToOffset(temp, sp, mem);
6933 } else if (source.IsFpuRegisterPair() && destination.IsFpuRegisterPair()) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006934 vixl32::DRegister first = DRegisterFrom(source);
6935 vixl32::DRegister second = DRegisterFrom(destination);
6936 vixl32::DRegister temp = temps.AcquireD();
6937 __ Vmov(temp, first);
6938 __ Vmov(first, second);
6939 __ Vmov(second, temp);
Alexandre Rames9c19bd62016-10-24 11:50:32 +01006940 } else if (source.IsFpuRegisterPair() || destination.IsFpuRegisterPair()) {
Anton Kirilovdda43962016-11-21 19:55:20 +00006941 vixl32::DRegister reg = source.IsFpuRegisterPair()
6942 ? DRegisterFrom(source)
6943 : DRegisterFrom(destination);
6944 int mem = source.IsFpuRegisterPair()
6945 ? destination.GetStackIndex()
6946 : source.GetStackIndex();
6947 vixl32::DRegister temp = temps.AcquireD();
6948 __ Vmov(temp, reg);
6949 GetAssembler()->LoadDFromOffset(reg, sp, mem);
6950 GetAssembler()->StoreDToOffset(temp, sp, mem);
Alexandre Rames9c19bd62016-10-24 11:50:32 +01006951 } else if (source.IsFpuRegister() || destination.IsFpuRegister()) {
Anton Kirilovdda43962016-11-21 19:55:20 +00006952 vixl32::SRegister reg = source.IsFpuRegister()
6953 ? SRegisterFrom(source)
6954 : SRegisterFrom(destination);
6955 int mem = source.IsFpuRegister()
6956 ? destination.GetStackIndex()
6957 : source.GetStackIndex();
6958 vixl32::Register temp = temps.Acquire();
6959 __ Vmov(temp, reg);
6960 GetAssembler()->LoadSFromOffset(reg, sp, mem);
6961 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, mem);
Alexandre Rames9c19bd62016-10-24 11:50:32 +01006962 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
6963 vixl32::DRegister temp1 = temps.AcquireD();
6964 vixl32::DRegister temp2 = temps.AcquireD();
6965 __ Vldr(temp1, MemOperand(sp, source.GetStackIndex()));
6966 __ Vldr(temp2, MemOperand(sp, destination.GetStackIndex()));
6967 __ Vstr(temp1, MemOperand(sp, destination.GetStackIndex()));
6968 __ Vstr(temp2, MemOperand(sp, source.GetStackIndex()));
6969 } else {
6970 LOG(FATAL) << "Unimplemented" << source << " <-> " << destination;
6971 }
Scott Wakelingfe885462016-09-22 10:24:38 +01006972}
6973
Nicolas Geoffray13a797b2017-03-15 16:41:31 +00006974void ParallelMoveResolverARMVIXL::SpillScratch(int reg) {
6975 __ Push(vixl32::Register(reg));
Scott Wakelingfe885462016-09-22 10:24:38 +01006976}
6977
Nicolas Geoffray13a797b2017-03-15 16:41:31 +00006978void ParallelMoveResolverARMVIXL::RestoreScratch(int reg) {
6979 __ Pop(vixl32::Register(reg));
Scott Wakelingfe885462016-09-22 10:24:38 +01006980}
6981
Artem Serov02d37832016-10-25 15:25:33 +01006982HLoadClass::LoadKind CodeGeneratorARMVIXL::GetSupportedLoadClassKind(
Artem Serovd4cc5b22016-11-04 11:19:09 +00006983 HLoadClass::LoadKind desired_class_load_kind) {
6984 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006985 case HLoadClass::LoadKind::kInvalid:
6986 LOG(FATAL) << "UNREACHABLE";
6987 UNREACHABLE();
Artem Serovd4cc5b22016-11-04 11:19:09 +00006988 case HLoadClass::LoadKind::kReferrersClass:
6989 break;
Artem Serovd4cc5b22016-11-04 11:19:09 +00006990 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006991 case HLoadClass::LoadKind::kBootImageClassTable:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006992 case HLoadClass::LoadKind::kBssEntry:
6993 DCHECK(!Runtime::Current()->UseJitCompilation());
6994 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006995 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006996 DCHECK(Runtime::Current()->UseJitCompilation());
Artem Serovc5fcb442016-12-02 19:19:58 +00006997 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01006998 case HLoadClass::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006999 case HLoadClass::LoadKind::kRuntimeCall:
Artem Serovd4cc5b22016-11-04 11:19:09 +00007000 break;
7001 }
7002 return desired_class_load_kind;
Artem Serov02d37832016-10-25 15:25:33 +01007003}
7004
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007005void LocationsBuilderARMVIXL::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00007006 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007007 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007008 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00007009 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007010 cls,
7011 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00007012 LocationFrom(r0));
Vladimir Markoea4c1262017-02-06 19:59:33 +00007013 DCHECK(calling_convention.GetRegisterAt(0).Is(r0));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007014 return;
7015 }
Vladimir Marko41559982017-01-06 14:04:23 +00007016 DCHECK(!cls->NeedsAccessCheck());
Scott Wakelingfe885462016-09-22 10:24:38 +01007017
Artem Serovd4cc5b22016-11-04 11:19:09 +00007018 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
7019 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007020 ? LocationSummary::kCallOnSlowPath
7021 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01007022 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(cls, call_kind);
Artem Serovd4cc5b22016-11-04 11:19:09 +00007023 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007024 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Artem Serovd4cc5b22016-11-04 11:19:09 +00007025 }
7026
Vladimir Marko41559982017-01-06 14:04:23 +00007027 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007028 locations->SetInAt(0, Location::RequiresRegister());
7029 }
7030 locations->SetOut(Location::RequiresRegister());
Vladimir Markoea4c1262017-02-06 19:59:33 +00007031 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
7032 if (!kUseReadBarrier || kUseBakerReadBarrier) {
7033 // Rely on the type resolution or initialization and marking to save everything we need.
Vladimir Markoea4c1262017-02-06 19:59:33 +00007034 RegisterSet caller_saves = RegisterSet::Empty();
7035 InvokeRuntimeCallingConventionARMVIXL calling_convention;
7036 caller_saves.Add(LocationFrom(calling_convention.GetRegisterAt(0)));
7037 // TODO: Add GetReturnLocation() to the calling convention so that we can DCHECK()
7038 // that the the kPrimNot result register is the same as the first argument register.
7039 locations->SetCustomSlowPathCallerSaves(caller_saves);
7040 } else {
7041 // For non-Baker read barrier we have a temp-clobbering call.
7042 }
7043 }
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01007044 if (kUseBakerReadBarrier && kBakerReadBarrierLinkTimeThunksEnableForGcRoots) {
7045 if (load_kind == HLoadClass::LoadKind::kBssEntry ||
7046 (load_kind == HLoadClass::LoadKind::kReferrersClass &&
7047 !Runtime::Current()->UseJitCompilation())) {
7048 locations->AddTemp(Location::RegisterLocation(kBakerCcEntrypointRegister.GetCode()));
7049 }
7050 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007051}
7052
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007053// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
7054// move.
7055void InstructionCodeGeneratorARMVIXL::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00007056 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007057 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00007058 codegen_->GenerateLoadClassRuntimeCall(cls);
Roland Levillain5daa4952017-07-03 17:23:56 +01007059 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 13);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007060 return;
7061 }
Vladimir Marko41559982017-01-06 14:04:23 +00007062 DCHECK(!cls->NeedsAccessCheck());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007063
Vladimir Marko41559982017-01-06 14:04:23 +00007064 LocationSummary* locations = cls->GetLocations();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007065 Location out_loc = locations->Out();
7066 vixl32::Register out = OutputRegister(cls);
7067
Artem Serovd4cc5b22016-11-04 11:19:09 +00007068 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
7069 ? kWithoutReadBarrier
7070 : kCompilerReadBarrierOption;
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007071 bool generate_null_check = false;
Vladimir Marko41559982017-01-06 14:04:23 +00007072 switch (load_kind) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007073 case HLoadClass::LoadKind::kReferrersClass: {
7074 DCHECK(!cls->CanCallRuntime());
7075 DCHECK(!cls->MustGenerateClinitCheck());
7076 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
7077 vixl32::Register current_method = InputRegisterAt(cls, 0);
7078 GenerateGcRootFieldLoad(cls,
7079 out_loc,
7080 current_method,
Roland Levillain00468f32016-10-27 18:02:48 +01007081 ArtMethod::DeclaringClassOffset().Int32Value(),
Artem Serovd4cc5b22016-11-04 11:19:09 +00007082 read_barrier_option);
7083 break;
7084 }
Artem Serovd4cc5b22016-11-04 11:19:09 +00007085 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007086 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Artem Serovd4cc5b22016-11-04 11:19:09 +00007087 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
7088 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
7089 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
7090 codegen_->EmitMovwMovtPlaceholder(labels, out);
7091 break;
7092 }
7093 case HLoadClass::LoadKind::kBootImageAddress: {
Artem Serovc5fcb442016-12-02 19:19:58 +00007094 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007095 uint32_t address = dchecked_integral_cast<uint32_t>(
7096 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
7097 DCHECK_NE(address, 0u);
Artem Serovc5fcb442016-12-02 19:19:58 +00007098 __ Ldr(out, codegen_->DeduplicateBootImageAddressLiteral(address));
Artem Serovd4cc5b22016-11-04 11:19:09 +00007099 break;
7100 }
Vladimir Marko94ec2db2017-09-06 17:21:03 +01007101 case HLoadClass::LoadKind::kBootImageClassTable: {
7102 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
7103 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
7104 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
7105 codegen_->EmitMovwMovtPlaceholder(labels, out);
7106 __ Ldr(out, MemOperand(out, /* offset */ 0));
7107 // Extract the reference from the slot data, i.e. clear the hash bits.
7108 int32_t masked_hash = ClassTable::TableSlot::MaskHash(
7109 ComputeModifiedUtf8Hash(cls->GetDexFile().StringByTypeIdx(cls->GetTypeIndex())));
7110 if (masked_hash != 0) {
7111 __ Sub(out, out, Operand(masked_hash));
7112 }
7113 break;
7114 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007115 case HLoadClass::LoadKind::kBssEntry: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007116 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
Vladimir Marko1998cd02017-01-13 13:02:58 +00007117 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex());
Vladimir Markof3c52b42017-11-17 17:32:12 +00007118 codegen_->EmitMovwMovtPlaceholder(labels, out);
7119 GenerateGcRootFieldLoad(cls, out_loc, out, /* offset */ 0, read_barrier_option);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007120 generate_null_check = true;
7121 break;
7122 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007123 case HLoadClass::LoadKind::kJitTableAddress: {
Artem Serovc5fcb442016-12-02 19:19:58 +00007124 __ Ldr(out, codegen_->DeduplicateJitClassLiteral(cls->GetDexFile(),
7125 cls->GetTypeIndex(),
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007126 cls->GetClass()));
Artem Serovc5fcb442016-12-02 19:19:58 +00007127 // /* GcRoot<mirror::Class> */ out = *out
Vladimir Markoea4c1262017-02-06 19:59:33 +00007128 GenerateGcRootFieldLoad(cls, out_loc, out, /* offset */ 0, read_barrier_option);
Artem Serovd4cc5b22016-11-04 11:19:09 +00007129 break;
7130 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007131 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00007132 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00007133 LOG(FATAL) << "UNREACHABLE";
7134 UNREACHABLE();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007135 }
7136
7137 if (generate_null_check || cls->MustGenerateClinitCheck()) {
7138 DCHECK(cls->CanCallRuntime());
Vladimir Marko174b2e22017-10-12 13:34:49 +01007139 LoadClassSlowPathARMVIXL* slow_path =
7140 new (codegen_->GetScopedAllocator()) LoadClassSlowPathARMVIXL(
7141 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007142 codegen_->AddSlowPath(slow_path);
7143 if (generate_null_check) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00007144 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007145 }
7146 if (cls->MustGenerateClinitCheck()) {
7147 GenerateClassInitializationCheck(slow_path, out);
7148 } else {
7149 __ Bind(slow_path->GetExitLabel());
7150 }
Roland Levillain5daa4952017-07-03 17:23:56 +01007151 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 14);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007152 }
7153}
7154
Artem Serov02d37832016-10-25 15:25:33 +01007155void LocationsBuilderARMVIXL::VisitClinitCheck(HClinitCheck* check) {
7156 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007157 new (GetGraph()->GetAllocator()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
Artem Serov02d37832016-10-25 15:25:33 +01007158 locations->SetInAt(0, Location::RequiresRegister());
7159 if (check->HasUses()) {
7160 locations->SetOut(Location::SameAsFirstInput());
7161 }
7162}
7163
7164void InstructionCodeGeneratorARMVIXL::VisitClinitCheck(HClinitCheck* check) {
7165 // We assume the class is not null.
7166 LoadClassSlowPathARMVIXL* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01007167 new (codegen_->GetScopedAllocator()) LoadClassSlowPathARMVIXL(check->GetLoadClass(),
7168 check,
7169 check->GetDexPc(),
7170 /* do_clinit */ true);
Artem Serov02d37832016-10-25 15:25:33 +01007171 codegen_->AddSlowPath(slow_path);
7172 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
7173}
7174
7175void InstructionCodeGeneratorARMVIXL::GenerateClassInitializationCheck(
7176 LoadClassSlowPathARMVIXL* slow_path, vixl32::Register class_reg) {
7177 UseScratchRegisterScope temps(GetVIXLAssembler());
7178 vixl32::Register temp = temps.Acquire();
Vladimir Markodc682aa2018-01-04 18:42:57 +00007179 constexpr size_t status_lsb_position = SubtypeCheckBits::BitStructSizeOf();
7180 const size_t status_byte_offset =
7181 mirror::Class::StatusOffset().SizeValue() + (status_lsb_position / kBitsPerByte);
7182 constexpr uint32_t shifted_initialized_value =
7183 enum_cast<uint32_t>(ClassStatus::kInitialized) << (status_lsb_position % kBitsPerByte);
7184
7185 GetAssembler()->LoadFromOffset(kLoadUnsignedByte, temp, class_reg, status_byte_offset);
7186 __ Cmp(temp, shifted_initialized_value);
Vladimir Marko2c64a832018-01-04 11:31:56 +00007187 __ B(lo, slow_path->GetEntryLabel());
Artem Serov02d37832016-10-25 15:25:33 +01007188 // Even if the initialized flag is set, we may be in a situation where caches are not synced
7189 // properly. Therefore, we do a memory fence.
7190 __ Dmb(ISH);
7191 __ Bind(slow_path->GetExitLabel());
7192}
7193
Artem Serov02d37832016-10-25 15:25:33 +01007194HLoadString::LoadKind CodeGeneratorARMVIXL::GetSupportedLoadStringKind(
Artem Serovd4cc5b22016-11-04 11:19:09 +00007195 HLoadString::LoadKind desired_string_load_kind) {
7196 switch (desired_string_load_kind) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00007197 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007198 case HLoadString::LoadKind::kBootImageInternTable:
Artem Serovd4cc5b22016-11-04 11:19:09 +00007199 case HLoadString::LoadKind::kBssEntry:
7200 DCHECK(!Runtime::Current()->UseJitCompilation());
7201 break;
7202 case HLoadString::LoadKind::kJitTableAddress:
7203 DCHECK(Runtime::Current()->UseJitCompilation());
Artem Serovc5fcb442016-12-02 19:19:58 +00007204 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01007205 case HLoadString::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007206 case HLoadString::LoadKind::kRuntimeCall:
Artem Serovd4cc5b22016-11-04 11:19:09 +00007207 break;
7208 }
7209 return desired_string_load_kind;
Artem Serov02d37832016-10-25 15:25:33 +01007210}
7211
7212void LocationsBuilderARMVIXL::VisitLoadString(HLoadString* load) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00007213 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Vladimir Markoca6fff82017-10-03 14:49:14 +01007214 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(load, call_kind);
Artem Serov02d37832016-10-25 15:25:33 +01007215 HLoadString::LoadKind load_kind = load->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007216 if (load_kind == HLoadString::LoadKind::kRuntimeCall) {
Artem Serov02d37832016-10-25 15:25:33 +01007217 locations->SetOut(LocationFrom(r0));
7218 } else {
7219 locations->SetOut(Location::RequiresRegister());
Artem Serovd4cc5b22016-11-04 11:19:09 +00007220 if (load_kind == HLoadString::LoadKind::kBssEntry) {
7221 if (!kUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00007222 // Rely on the pResolveString and marking to save everything we need, including temps.
Artem Serovd4cc5b22016-11-04 11:19:09 +00007223 RegisterSet caller_saves = RegisterSet::Empty();
7224 InvokeRuntimeCallingConventionARMVIXL calling_convention;
7225 caller_saves.Add(LocationFrom(calling_convention.GetRegisterAt(0)));
7226 // TODO: Add GetReturnLocation() to the calling convention so that we can DCHECK()
7227 // that the the kPrimNot result register is the same as the first argument register.
7228 locations->SetCustomSlowPathCallerSaves(caller_saves);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01007229 if (kUseBakerReadBarrier && kBakerReadBarrierLinkTimeThunksEnableForGcRoots) {
7230 locations->AddTemp(Location::RegisterLocation(kBakerCcEntrypointRegister.GetCode()));
7231 }
Artem Serovd4cc5b22016-11-04 11:19:09 +00007232 } else {
7233 // For non-Baker read barrier we have a temp-clobbering call.
7234 }
7235 }
Artem Serov02d37832016-10-25 15:25:33 +01007236 }
7237}
7238
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007239// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
7240// move.
7241void InstructionCodeGeneratorARMVIXL::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Artem Serovd4cc5b22016-11-04 11:19:09 +00007242 LocationSummary* locations = load->GetLocations();
7243 Location out_loc = locations->Out();
7244 vixl32::Register out = OutputRegister(load);
7245 HLoadString::LoadKind load_kind = load->GetLoadKind();
7246
7247 switch (load_kind) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00007248 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
7249 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
7250 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007251 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Artem Serovd4cc5b22016-11-04 11:19:09 +00007252 codegen_->EmitMovwMovtPlaceholder(labels, out);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007253 return;
Artem Serovd4cc5b22016-11-04 11:19:09 +00007254 }
7255 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007256 uint32_t address = dchecked_integral_cast<uint32_t>(
7257 reinterpret_cast<uintptr_t>(load->GetString().Get()));
7258 DCHECK_NE(address, 0u);
Artem Serovc5fcb442016-12-02 19:19:58 +00007259 __ Ldr(out, codegen_->DeduplicateBootImageAddressLiteral(address));
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007260 return;
7261 }
7262 case HLoadString::LoadKind::kBootImageInternTable: {
7263 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
7264 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
7265 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
7266 codegen_->EmitMovwMovtPlaceholder(labels, out);
7267 __ Ldr(out, MemOperand(out, /* offset */ 0));
7268 return;
Artem Serovd4cc5b22016-11-04 11:19:09 +00007269 }
7270 case HLoadString::LoadKind::kBssEntry: {
7271 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Artem Serovd4cc5b22016-11-04 11:19:09 +00007272 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007273 codegen_->NewStringBssEntryPatch(load->GetDexFile(), load->GetStringIndex());
Vladimir Markof3c52b42017-11-17 17:32:12 +00007274 codegen_->EmitMovwMovtPlaceholder(labels, out);
7275 GenerateGcRootFieldLoad(load, out_loc, out, /* offset */ 0, kCompilerReadBarrierOption);
Artem Serovd4cc5b22016-11-04 11:19:09 +00007276 LoadStringSlowPathARMVIXL* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01007277 new (codegen_->GetScopedAllocator()) LoadStringSlowPathARMVIXL(load);
Artem Serovd4cc5b22016-11-04 11:19:09 +00007278 codegen_->AddSlowPath(slow_path);
7279 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
7280 __ Bind(slow_path->GetExitLabel());
Roland Levillain5daa4952017-07-03 17:23:56 +01007281 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 15);
Artem Serovd4cc5b22016-11-04 11:19:09 +00007282 return;
7283 }
7284 case HLoadString::LoadKind::kJitTableAddress: {
Artem Serovc5fcb442016-12-02 19:19:58 +00007285 __ Ldr(out, codegen_->DeduplicateJitStringLiteral(load->GetDexFile(),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007286 load->GetStringIndex(),
7287 load->GetString()));
Artem Serovc5fcb442016-12-02 19:19:58 +00007288 // /* GcRoot<mirror::String> */ out = *out
7289 GenerateGcRootFieldLoad(load, out_loc, out, /* offset */ 0, kCompilerReadBarrierOption);
7290 return;
Artem Serovd4cc5b22016-11-04 11:19:09 +00007291 }
7292 default:
7293 break;
7294 }
Artem Serov02d37832016-10-25 15:25:33 +01007295
7296 // TODO: Re-add the compiler code to do string dex cache lookup again.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007297 DCHECK_EQ(load->GetLoadKind(), HLoadString::LoadKind::kRuntimeCall);
Artem Serov02d37832016-10-25 15:25:33 +01007298 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Andreas Gampe8a0128a2016-11-28 07:38:35 -08007299 __ Mov(calling_convention.GetRegisterAt(0), load->GetStringIndex().index_);
Artem Serov02d37832016-10-25 15:25:33 +01007300 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
7301 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Roland Levillain5daa4952017-07-03 17:23:56 +01007302 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 16);
Artem Serov02d37832016-10-25 15:25:33 +01007303}
7304
7305static int32_t GetExceptionTlsOffset() {
7306 return Thread::ExceptionOffset<kArmPointerSize>().Int32Value();
7307}
7308
7309void LocationsBuilderARMVIXL::VisitLoadException(HLoadException* load) {
7310 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007311 new (GetGraph()->GetAllocator()) LocationSummary(load, LocationSummary::kNoCall);
Artem Serov02d37832016-10-25 15:25:33 +01007312 locations->SetOut(Location::RequiresRegister());
7313}
7314
7315void InstructionCodeGeneratorARMVIXL::VisitLoadException(HLoadException* load) {
7316 vixl32::Register out = OutputRegister(load);
7317 GetAssembler()->LoadFromOffset(kLoadWord, out, tr, GetExceptionTlsOffset());
7318}
7319
7320
7321void LocationsBuilderARMVIXL::VisitClearException(HClearException* clear) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007322 new (GetGraph()->GetAllocator()) LocationSummary(clear, LocationSummary::kNoCall);
Artem Serov02d37832016-10-25 15:25:33 +01007323}
7324
7325void InstructionCodeGeneratorARMVIXL::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
7326 UseScratchRegisterScope temps(GetVIXLAssembler());
7327 vixl32::Register temp = temps.Acquire();
7328 __ Mov(temp, 0);
7329 GetAssembler()->StoreToOffset(kStoreWord, temp, tr, GetExceptionTlsOffset());
7330}
7331
7332void LocationsBuilderARMVIXL::VisitThrow(HThrow* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007333 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
7334 instruction, LocationSummary::kCallOnMainOnly);
Artem Serov02d37832016-10-25 15:25:33 +01007335 InvokeRuntimeCallingConventionARMVIXL calling_convention;
7336 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
7337}
7338
7339void InstructionCodeGeneratorARMVIXL::VisitThrow(HThrow* instruction) {
7340 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
7341 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
7342}
7343
Artem Serov657022c2016-11-23 14:19:38 +00007344// Temp is used for read barrier.
7345static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
7346 if (kEmitCompilerReadBarrier &&
7347 (kUseBakerReadBarrier ||
7348 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
7349 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
7350 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
7351 return 1;
7352 }
7353 return 0;
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007354}
7355
Artem Serov657022c2016-11-23 14:19:38 +00007356// Interface case has 3 temps, one for holding the number of interfaces, one for the current
7357// interface pointer, one for loading the current interface.
7358// The other checks have one temp for loading the object's class.
7359static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
7360 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
7361 return 3;
7362 }
7363 return 1 + NumberOfInstanceOfTemps(type_check_kind);
7364}
Artem Serovcfbe9132016-10-14 15:58:56 +01007365
7366void LocationsBuilderARMVIXL::VisitInstanceOf(HInstanceOf* instruction) {
7367 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
7368 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
7369 bool baker_read_barrier_slow_path = false;
7370 switch (type_check_kind) {
7371 case TypeCheckKind::kExactCheck:
7372 case TypeCheckKind::kAbstractClassCheck:
7373 case TypeCheckKind::kClassHierarchyCheck:
Vladimir Marko87584542017-12-12 17:47:52 +00007374 case TypeCheckKind::kArrayObjectCheck: {
7375 bool needs_read_barrier = CodeGenerator::InstanceOfNeedsReadBarrier(instruction);
7376 call_kind = needs_read_barrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
7377 baker_read_barrier_slow_path = kUseBakerReadBarrier && needs_read_barrier;
Artem Serovcfbe9132016-10-14 15:58:56 +01007378 break;
Vladimir Marko87584542017-12-12 17:47:52 +00007379 }
Artem Serovcfbe9132016-10-14 15:58:56 +01007380 case TypeCheckKind::kArrayCheck:
7381 case TypeCheckKind::kUnresolvedCheck:
7382 case TypeCheckKind::kInterfaceCheck:
7383 call_kind = LocationSummary::kCallOnSlowPath;
7384 break;
7385 }
7386
Vladimir Markoca6fff82017-10-03 14:49:14 +01007387 LocationSummary* locations =
7388 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Artem Serovcfbe9132016-10-14 15:58:56 +01007389 if (baker_read_barrier_slow_path) {
7390 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
7391 }
7392 locations->SetInAt(0, Location::RequiresRegister());
7393 locations->SetInAt(1, Location::RequiresRegister());
7394 // The "out" register is used as a temporary, so it overlaps with the inputs.
7395 // Note that TypeCheckSlowPathARM uses this register too.
7396 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Artem Serov657022c2016-11-23 14:19:38 +00007397 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01007398 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
7399 codegen_->MaybeAddBakerCcEntrypointTempForFields(locations);
7400 }
Artem Serovcfbe9132016-10-14 15:58:56 +01007401}
7402
7403void InstructionCodeGeneratorARMVIXL::VisitInstanceOf(HInstanceOf* instruction) {
7404 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
7405 LocationSummary* locations = instruction->GetLocations();
7406 Location obj_loc = locations->InAt(0);
7407 vixl32::Register obj = InputRegisterAt(instruction, 0);
7408 vixl32::Register cls = InputRegisterAt(instruction, 1);
7409 Location out_loc = locations->Out();
7410 vixl32::Register out = OutputRegister(instruction);
Artem Serov657022c2016-11-23 14:19:38 +00007411 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
7412 DCHECK_LE(num_temps, 1u);
7413 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Artem Serovcfbe9132016-10-14 15:58:56 +01007414 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
7415 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
7416 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
7417 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007418 vixl32::Label done;
7419 vixl32::Label* const final_label = codegen_->GetFinalLabel(instruction, &done);
Artem Serovcfbe9132016-10-14 15:58:56 +01007420 SlowPathCodeARMVIXL* slow_path = nullptr;
7421
7422 // Return 0 if `obj` is null.
7423 // avoid null check if we know obj is not null.
7424 if (instruction->MustDoNullCheck()) {
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007425 DCHECK(!out.Is(obj));
7426 __ Mov(out, 0);
7427 __ CompareAndBranchIfZero(obj, final_label, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007428 }
7429
Artem Serovcfbe9132016-10-14 15:58:56 +01007430 switch (type_check_kind) {
7431 case TypeCheckKind::kExactCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00007432 ReadBarrierOption read_barrier_option =
7433 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier6beced42016-11-15 15:51:31 -08007434 // /* HeapReference<Class> */ out = obj->klass_
7435 GenerateReferenceLoadTwoRegisters(instruction,
7436 out_loc,
7437 obj_loc,
7438 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00007439 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007440 read_barrier_option);
Artem Serovcfbe9132016-10-14 15:58:56 +01007441 // Classes must be equal for the instanceof to succeed.
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007442 __ Cmp(out, cls);
7443 // We speculatively set the result to false without changing the condition
7444 // flags, which allows us to avoid some branching later.
7445 __ Mov(LeaveFlags, out, 0);
7446
7447 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
7448 // we check that the output is in a low register, so that a 16-bit MOV
7449 // encoding can be used.
7450 if (out.IsLow()) {
7451 // We use the scope because of the IT block that follows.
7452 ExactAssemblyScope guard(GetVIXLAssembler(),
7453 2 * vixl32::k16BitT32InstructionSizeInBytes,
7454 CodeBufferCheckScope::kExactSize);
7455
7456 __ it(eq);
7457 __ mov(eq, out, 1);
7458 } else {
7459 __ B(ne, final_label, /* far_target */ false);
7460 __ Mov(out, 1);
7461 }
7462
Artem Serovcfbe9132016-10-14 15:58:56 +01007463 break;
7464 }
7465
7466 case TypeCheckKind::kAbstractClassCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00007467 ReadBarrierOption read_barrier_option =
7468 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier6beced42016-11-15 15:51:31 -08007469 // /* HeapReference<Class> */ out = obj->klass_
7470 GenerateReferenceLoadTwoRegisters(instruction,
7471 out_loc,
7472 obj_loc,
7473 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00007474 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007475 read_barrier_option);
Artem Serovcfbe9132016-10-14 15:58:56 +01007476 // If the class is abstract, we eagerly fetch the super class of the
7477 // object to avoid doing a comparison we know will fail.
7478 vixl32::Label loop;
7479 __ Bind(&loop);
7480 // /* HeapReference<Class> */ out = out->super_class_
Artem Serov657022c2016-11-23 14:19:38 +00007481 GenerateReferenceLoadOneRegister(instruction,
7482 out_loc,
7483 super_offset,
7484 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007485 read_barrier_option);
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007486 // If `out` is null, we use it for the result, and jump to the final label.
Anton Kirilov6f644202017-02-27 18:29:45 +00007487 __ CompareAndBranchIfZero(out, final_label, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007488 __ Cmp(out, cls);
Artem Serov517d9f62016-12-12 15:51:15 +00007489 __ B(ne, &loop, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007490 __ Mov(out, 1);
Artem Serovcfbe9132016-10-14 15:58:56 +01007491 break;
7492 }
7493
7494 case TypeCheckKind::kClassHierarchyCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00007495 ReadBarrierOption read_barrier_option =
7496 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier6beced42016-11-15 15:51:31 -08007497 // /* HeapReference<Class> */ out = obj->klass_
7498 GenerateReferenceLoadTwoRegisters(instruction,
7499 out_loc,
7500 obj_loc,
7501 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00007502 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007503 read_barrier_option);
Artem Serovcfbe9132016-10-14 15:58:56 +01007504 // Walk over the class hierarchy to find a match.
7505 vixl32::Label loop, success;
7506 __ Bind(&loop);
7507 __ Cmp(out, cls);
Artem Serov517d9f62016-12-12 15:51:15 +00007508 __ B(eq, &success, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007509 // /* HeapReference<Class> */ out = out->super_class_
Artem Serov657022c2016-11-23 14:19:38 +00007510 GenerateReferenceLoadOneRegister(instruction,
7511 out_loc,
7512 super_offset,
7513 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007514 read_barrier_option);
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007515 // This is essentially a null check, but it sets the condition flags to the
7516 // proper value for the code that follows the loop, i.e. not `eq`.
7517 __ Cmp(out, 1);
7518 __ B(hs, &loop, /* far_target */ false);
7519
7520 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
7521 // we check that the output is in a low register, so that a 16-bit MOV
7522 // encoding can be used.
7523 if (out.IsLow()) {
7524 // If `out` is null, we use it for the result, and the condition flags
7525 // have already been set to `ne`, so the IT block that comes afterwards
7526 // (and which handles the successful case) turns into a NOP (instead of
7527 // overwriting `out`).
7528 __ Bind(&success);
7529
7530 // We use the scope because of the IT block that follows.
7531 ExactAssemblyScope guard(GetVIXLAssembler(),
7532 2 * vixl32::k16BitT32InstructionSizeInBytes,
7533 CodeBufferCheckScope::kExactSize);
7534
7535 // There is only one branch to the `success` label (which is bound to this
7536 // IT block), and it has the same condition, `eq`, so in that case the MOV
7537 // is executed.
7538 __ it(eq);
7539 __ mov(eq, out, 1);
7540 } else {
7541 // If `out` is null, we use it for the result, and jump to the final label.
Anton Kirilov6f644202017-02-27 18:29:45 +00007542 __ B(final_label);
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007543 __ Bind(&success);
7544 __ Mov(out, 1);
Artem Serovcfbe9132016-10-14 15:58:56 +01007545 }
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007546
Artem Serovcfbe9132016-10-14 15:58:56 +01007547 break;
7548 }
7549
7550 case TypeCheckKind::kArrayObjectCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00007551 ReadBarrierOption read_barrier_option =
7552 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier6beced42016-11-15 15:51:31 -08007553 // /* HeapReference<Class> */ out = obj->klass_
7554 GenerateReferenceLoadTwoRegisters(instruction,
7555 out_loc,
7556 obj_loc,
7557 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00007558 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007559 read_barrier_option);
Artem Serovcfbe9132016-10-14 15:58:56 +01007560 // Do an exact check.
7561 vixl32::Label exact_check;
7562 __ Cmp(out, cls);
Artem Serov517d9f62016-12-12 15:51:15 +00007563 __ B(eq, &exact_check, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007564 // Otherwise, we need to check that the object's class is a non-primitive array.
7565 // /* HeapReference<Class> */ out = out->component_type_
Artem Serov657022c2016-11-23 14:19:38 +00007566 GenerateReferenceLoadOneRegister(instruction,
7567 out_loc,
7568 component_offset,
7569 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007570 read_barrier_option);
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007571 // If `out` is null, we use it for the result, and jump to the final label.
Anton Kirilov6f644202017-02-27 18:29:45 +00007572 __ CompareAndBranchIfZero(out, final_label, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007573 GetAssembler()->LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
7574 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007575 __ Cmp(out, 0);
7576 // We speculatively set the result to false without changing the condition
7577 // flags, which allows us to avoid some branching later.
7578 __ Mov(LeaveFlags, out, 0);
7579
7580 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
7581 // we check that the output is in a low register, so that a 16-bit MOV
7582 // encoding can be used.
7583 if (out.IsLow()) {
7584 __ Bind(&exact_check);
7585
7586 // We use the scope because of the IT block that follows.
7587 ExactAssemblyScope guard(GetVIXLAssembler(),
7588 2 * vixl32::k16BitT32InstructionSizeInBytes,
7589 CodeBufferCheckScope::kExactSize);
7590
7591 __ it(eq);
7592 __ mov(eq, out, 1);
7593 } else {
7594 __ B(ne, final_label, /* far_target */ false);
7595 __ Bind(&exact_check);
7596 __ Mov(out, 1);
7597 }
7598
Artem Serovcfbe9132016-10-14 15:58:56 +01007599 break;
7600 }
7601
7602 case TypeCheckKind::kArrayCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00007603 // No read barrier since the slow path will retry upon failure.
Mathieu Chartier6beced42016-11-15 15:51:31 -08007604 // /* HeapReference<Class> */ out = obj->klass_
7605 GenerateReferenceLoadTwoRegisters(instruction,
7606 out_loc,
7607 obj_loc,
7608 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00007609 maybe_temp_loc,
7610 kWithoutReadBarrier);
Artem Serovcfbe9132016-10-14 15:58:56 +01007611 __ Cmp(out, cls);
7612 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01007613 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathARMVIXL(
7614 instruction, /* is_fatal */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007615 codegen_->AddSlowPath(slow_path);
7616 __ B(ne, slow_path->GetEntryLabel());
7617 __ Mov(out, 1);
Artem Serovcfbe9132016-10-14 15:58:56 +01007618 break;
7619 }
7620
7621 case TypeCheckKind::kUnresolvedCheck:
7622 case TypeCheckKind::kInterfaceCheck: {
7623 // Note that we indeed only call on slow path, but we always go
7624 // into the slow path for the unresolved and interface check
7625 // cases.
7626 //
7627 // We cannot directly call the InstanceofNonTrivial runtime
7628 // entry point without resorting to a type checking slow path
7629 // here (i.e. by calling InvokeRuntime directly), as it would
7630 // require to assign fixed registers for the inputs of this
7631 // HInstanceOf instruction (following the runtime calling
7632 // convention), which might be cluttered by the potential first
7633 // read barrier emission at the beginning of this method.
7634 //
7635 // TODO: Introduce a new runtime entry point taking the object
7636 // to test (instead of its class) as argument, and let it deal
7637 // with the read barrier issues. This will let us refactor this
7638 // case of the `switch` code as it was previously (with a direct
7639 // call to the runtime not using a type checking slow path).
7640 // This should also be beneficial for the other cases above.
7641 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01007642 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathARMVIXL(
7643 instruction, /* is_fatal */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007644 codegen_->AddSlowPath(slow_path);
7645 __ B(slow_path->GetEntryLabel());
Artem Serovcfbe9132016-10-14 15:58:56 +01007646 break;
7647 }
7648 }
7649
Artem Serovcfbe9132016-10-14 15:58:56 +01007650 if (done.IsReferenced()) {
7651 __ Bind(&done);
7652 }
7653
7654 if (slow_path != nullptr) {
7655 __ Bind(slow_path->GetExitLabel());
7656 }
7657}
7658
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007659void LocationsBuilderARMVIXL::VisitCheckCast(HCheckCast* instruction) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007660 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko87584542017-12-12 17:47:52 +00007661 LocationSummary::CallKind call_kind = CodeGenerator::GetCheckCastCallKind(instruction);
Vladimir Markoca6fff82017-10-03 14:49:14 +01007662 LocationSummary* locations =
7663 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007664 locations->SetInAt(0, Location::RequiresRegister());
7665 locations->SetInAt(1, Location::RequiresRegister());
Artem Serov657022c2016-11-23 14:19:38 +00007666 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007667}
7668
7669void InstructionCodeGeneratorARMVIXL::VisitCheckCast(HCheckCast* instruction) {
7670 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
7671 LocationSummary* locations = instruction->GetLocations();
7672 Location obj_loc = locations->InAt(0);
7673 vixl32::Register obj = InputRegisterAt(instruction, 0);
7674 vixl32::Register cls = InputRegisterAt(instruction, 1);
7675 Location temp_loc = locations->GetTemp(0);
7676 vixl32::Register temp = RegisterFrom(temp_loc);
Artem Serov657022c2016-11-23 14:19:38 +00007677 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
7678 DCHECK_LE(num_temps, 3u);
7679 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
7680 Location maybe_temp3_loc = (num_temps >= 3) ? locations->GetTemp(2) : Location::NoLocation();
7681 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
7682 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
7683 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
7684 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
7685 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
7686 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
7687 const uint32_t object_array_data_offset =
7688 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007689
Vladimir Marko87584542017-12-12 17:47:52 +00007690 bool is_type_check_slow_path_fatal = CodeGenerator::IsTypeCheckSlowPathFatal(instruction);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007691 SlowPathCodeARMVIXL* type_check_slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01007692 new (codegen_->GetScopedAllocator()) TypeCheckSlowPathARMVIXL(
7693 instruction, is_type_check_slow_path_fatal);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007694 codegen_->AddSlowPath(type_check_slow_path);
7695
7696 vixl32::Label done;
Anton Kirilov6f644202017-02-27 18:29:45 +00007697 vixl32::Label* final_label = codegen_->GetFinalLabel(instruction, &done);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007698 // Avoid null check if we know obj is not null.
7699 if (instruction->MustDoNullCheck()) {
Anton Kirilov6f644202017-02-27 18:29:45 +00007700 __ CompareAndBranchIfZero(obj, final_label, /* far_target */ false);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007701 }
7702
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007703 switch (type_check_kind) {
7704 case TypeCheckKind::kExactCheck:
7705 case TypeCheckKind::kArrayCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00007706 // /* HeapReference<Class> */ temp = obj->klass_
7707 GenerateReferenceLoadTwoRegisters(instruction,
7708 temp_loc,
7709 obj_loc,
7710 class_offset,
7711 maybe_temp2_loc,
7712 kWithoutReadBarrier);
7713
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007714 __ Cmp(temp, cls);
7715 // Jump to slow path for throwing the exception or doing a
7716 // more involved array check.
7717 __ B(ne, type_check_slow_path->GetEntryLabel());
7718 break;
7719 }
7720
7721 case TypeCheckKind::kAbstractClassCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00007722 // /* HeapReference<Class> */ temp = obj->klass_
7723 GenerateReferenceLoadTwoRegisters(instruction,
7724 temp_loc,
7725 obj_loc,
7726 class_offset,
7727 maybe_temp2_loc,
7728 kWithoutReadBarrier);
7729
Artem Serovcfbe9132016-10-14 15:58:56 +01007730 // If the class is abstract, we eagerly fetch the super class of the
7731 // object to avoid doing a comparison we know will fail.
7732 vixl32::Label loop;
7733 __ Bind(&loop);
7734 // /* HeapReference<Class> */ temp = temp->super_class_
Artem Serov657022c2016-11-23 14:19:38 +00007735 GenerateReferenceLoadOneRegister(instruction,
7736 temp_loc,
7737 super_offset,
7738 maybe_temp2_loc,
7739 kWithoutReadBarrier);
Artem Serovcfbe9132016-10-14 15:58:56 +01007740
7741 // If the class reference currently in `temp` is null, jump to the slow path to throw the
7742 // exception.
xueliang.zhongf51bc622016-11-04 09:23:32 +00007743 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
Artem Serovcfbe9132016-10-14 15:58:56 +01007744
7745 // Otherwise, compare the classes.
7746 __ Cmp(temp, cls);
Artem Serov517d9f62016-12-12 15:51:15 +00007747 __ B(ne, &loop, /* far_target */ false);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007748 break;
7749 }
7750
7751 case TypeCheckKind::kClassHierarchyCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00007752 // /* HeapReference<Class> */ temp = obj->klass_
7753 GenerateReferenceLoadTwoRegisters(instruction,
7754 temp_loc,
7755 obj_loc,
7756 class_offset,
7757 maybe_temp2_loc,
7758 kWithoutReadBarrier);
7759
Artem Serovcfbe9132016-10-14 15:58:56 +01007760 // Walk over the class hierarchy to find a match.
7761 vixl32::Label loop;
7762 __ Bind(&loop);
7763 __ Cmp(temp, cls);
Anton Kirilov6f644202017-02-27 18:29:45 +00007764 __ B(eq, final_label, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007765
7766 // /* HeapReference<Class> */ temp = temp->super_class_
Artem Serov657022c2016-11-23 14:19:38 +00007767 GenerateReferenceLoadOneRegister(instruction,
7768 temp_loc,
7769 super_offset,
7770 maybe_temp2_loc,
7771 kWithoutReadBarrier);
Artem Serovcfbe9132016-10-14 15:58:56 +01007772
7773 // If the class reference currently in `temp` is null, jump to the slow path to throw the
7774 // exception.
xueliang.zhongf51bc622016-11-04 09:23:32 +00007775 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
Artem Serovcfbe9132016-10-14 15:58:56 +01007776 // Otherwise, jump to the beginning of the loop.
7777 __ B(&loop);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007778 break;
7779 }
7780
Artem Serovcfbe9132016-10-14 15:58:56 +01007781 case TypeCheckKind::kArrayObjectCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00007782 // /* HeapReference<Class> */ temp = obj->klass_
7783 GenerateReferenceLoadTwoRegisters(instruction,
7784 temp_loc,
7785 obj_loc,
7786 class_offset,
7787 maybe_temp2_loc,
7788 kWithoutReadBarrier);
7789
Artem Serovcfbe9132016-10-14 15:58:56 +01007790 // Do an exact check.
7791 __ Cmp(temp, cls);
Anton Kirilov6f644202017-02-27 18:29:45 +00007792 __ B(eq, final_label, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007793
7794 // Otherwise, we need to check that the object's class is a non-primitive array.
7795 // /* HeapReference<Class> */ temp = temp->component_type_
Artem Serov657022c2016-11-23 14:19:38 +00007796 GenerateReferenceLoadOneRegister(instruction,
7797 temp_loc,
7798 component_offset,
7799 maybe_temp2_loc,
7800 kWithoutReadBarrier);
Artem Serovcfbe9132016-10-14 15:58:56 +01007801 // If the component type is null, jump to the slow path to throw the exception.
xueliang.zhongf51bc622016-11-04 09:23:32 +00007802 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
Artem Serovcfbe9132016-10-14 15:58:56 +01007803 // Otherwise,the object is indeed an array, jump to label `check_non_primitive_component_type`
7804 // to further check that this component type is not a primitive type.
7805 GetAssembler()->LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007806 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
xueliang.zhongf51bc622016-11-04 09:23:32 +00007807 __ CompareAndBranchIfNonZero(temp, type_check_slow_path->GetEntryLabel());
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007808 break;
7809 }
7810
7811 case TypeCheckKind::kUnresolvedCheck:
Artem Serov657022c2016-11-23 14:19:38 +00007812 // We always go into the type check slow path for the unresolved check case.
Artem Serovcfbe9132016-10-14 15:58:56 +01007813 // We cannot directly call the CheckCast runtime entry point
7814 // without resorting to a type checking slow path here (i.e. by
7815 // calling InvokeRuntime directly), as it would require to
7816 // assign fixed registers for the inputs of this HInstanceOf
7817 // instruction (following the runtime calling convention), which
7818 // might be cluttered by the potential first read barrier
7819 // emission at the beginning of this method.
Artem Serov657022c2016-11-23 14:19:38 +00007820
Artem Serovcfbe9132016-10-14 15:58:56 +01007821 __ B(type_check_slow_path->GetEntryLabel());
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007822 break;
Artem Serov657022c2016-11-23 14:19:38 +00007823
7824 case TypeCheckKind::kInterfaceCheck: {
7825 // Avoid read barriers to improve performance of the fast path. We can not get false
7826 // positives by doing this.
7827 // /* HeapReference<Class> */ temp = obj->klass_
7828 GenerateReferenceLoadTwoRegisters(instruction,
7829 temp_loc,
7830 obj_loc,
7831 class_offset,
7832 maybe_temp2_loc,
7833 kWithoutReadBarrier);
7834
7835 // /* HeapReference<Class> */ temp = temp->iftable_
7836 GenerateReferenceLoadTwoRegisters(instruction,
7837 temp_loc,
7838 temp_loc,
7839 iftable_offset,
7840 maybe_temp2_loc,
7841 kWithoutReadBarrier);
7842 // Iftable is never null.
7843 __ Ldr(RegisterFrom(maybe_temp2_loc), MemOperand(temp, array_length_offset));
7844 // Loop through the iftable and check if any class matches.
7845 vixl32::Label start_loop;
7846 __ Bind(&start_loop);
7847 __ CompareAndBranchIfZero(RegisterFrom(maybe_temp2_loc),
7848 type_check_slow_path->GetEntryLabel());
7849 __ Ldr(RegisterFrom(maybe_temp3_loc), MemOperand(temp, object_array_data_offset));
7850 GetAssembler()->MaybeUnpoisonHeapReference(RegisterFrom(maybe_temp3_loc));
7851 // Go to next interface.
7852 __ Add(temp, temp, Operand::From(2 * kHeapReferenceSize));
7853 __ Sub(RegisterFrom(maybe_temp2_loc), RegisterFrom(maybe_temp2_loc), 2);
7854 // Compare the classes and continue the loop if they do not match.
7855 __ Cmp(cls, RegisterFrom(maybe_temp3_loc));
Artem Serov517d9f62016-12-12 15:51:15 +00007856 __ B(ne, &start_loop, /* far_target */ false);
Artem Serov657022c2016-11-23 14:19:38 +00007857 break;
7858 }
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007859 }
Anton Kirilov6f644202017-02-27 18:29:45 +00007860 if (done.IsReferenced()) {
7861 __ Bind(&done);
7862 }
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007863
7864 __ Bind(type_check_slow_path->GetExitLabel());
7865}
7866
Artem Serov551b28f2016-10-18 19:11:30 +01007867void LocationsBuilderARMVIXL::VisitMonitorOperation(HMonitorOperation* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007868 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
7869 instruction, LocationSummary::kCallOnMainOnly);
Artem Serov551b28f2016-10-18 19:11:30 +01007870 InvokeRuntimeCallingConventionARMVIXL calling_convention;
7871 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
7872}
7873
7874void InstructionCodeGeneratorARMVIXL::VisitMonitorOperation(HMonitorOperation* instruction) {
7875 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
7876 instruction,
7877 instruction->GetDexPc());
7878 if (instruction->IsEnter()) {
7879 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
7880 } else {
7881 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
7882 }
Roland Levillain5daa4952017-07-03 17:23:56 +01007883 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 17);
Artem Serov551b28f2016-10-18 19:11:30 +01007884}
7885
Artem Serov02109dd2016-09-23 17:17:54 +01007886void LocationsBuilderARMVIXL::VisitAnd(HAnd* instruction) {
7887 HandleBitwiseOperation(instruction, AND);
7888}
7889
7890void LocationsBuilderARMVIXL::VisitOr(HOr* instruction) {
7891 HandleBitwiseOperation(instruction, ORR);
7892}
7893
7894void LocationsBuilderARMVIXL::VisitXor(HXor* instruction) {
7895 HandleBitwiseOperation(instruction, EOR);
7896}
7897
7898void LocationsBuilderARMVIXL::HandleBitwiseOperation(HBinaryOperation* instruction, Opcode opcode) {
7899 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007900 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007901 DCHECK(instruction->GetResultType() == DataType::Type::kInt32
7902 || instruction->GetResultType() == DataType::Type::kInt64);
Artem Serov02109dd2016-09-23 17:17:54 +01007903 // Note: GVN reorders commutative operations to have the constant on the right hand side.
7904 locations->SetInAt(0, Location::RequiresRegister());
7905 locations->SetInAt(1, ArmEncodableConstantOrRegister(instruction->InputAt(1), opcode));
7906 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
7907}
7908
7909void InstructionCodeGeneratorARMVIXL::VisitAnd(HAnd* instruction) {
7910 HandleBitwiseOperation(instruction);
7911}
7912
7913void InstructionCodeGeneratorARMVIXL::VisitOr(HOr* instruction) {
7914 HandleBitwiseOperation(instruction);
7915}
7916
7917void InstructionCodeGeneratorARMVIXL::VisitXor(HXor* instruction) {
7918 HandleBitwiseOperation(instruction);
7919}
7920
Artem Serov2bbc9532016-10-21 11:51:50 +01007921void LocationsBuilderARMVIXL::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
7922 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007923 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007924 DCHECK(instruction->GetResultType() == DataType::Type::kInt32
7925 || instruction->GetResultType() == DataType::Type::kInt64);
Artem Serov2bbc9532016-10-21 11:51:50 +01007926
7927 locations->SetInAt(0, Location::RequiresRegister());
7928 locations->SetInAt(1, Location::RequiresRegister());
7929 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
7930}
7931
7932void InstructionCodeGeneratorARMVIXL::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
7933 LocationSummary* locations = instruction->GetLocations();
7934 Location first = locations->InAt(0);
7935 Location second = locations->InAt(1);
7936 Location out = locations->Out();
7937
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007938 if (instruction->GetResultType() == DataType::Type::kInt32) {
Artem Serov2bbc9532016-10-21 11:51:50 +01007939 vixl32::Register first_reg = RegisterFrom(first);
7940 vixl32::Register second_reg = RegisterFrom(second);
7941 vixl32::Register out_reg = RegisterFrom(out);
7942
7943 switch (instruction->GetOpKind()) {
7944 case HInstruction::kAnd:
7945 __ Bic(out_reg, first_reg, second_reg);
7946 break;
7947 case HInstruction::kOr:
7948 __ Orn(out_reg, first_reg, second_reg);
7949 break;
7950 // There is no EON on arm.
7951 case HInstruction::kXor:
7952 default:
7953 LOG(FATAL) << "Unexpected instruction " << instruction->DebugName();
7954 UNREACHABLE();
7955 }
7956 return;
7957
7958 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007959 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
Artem Serov2bbc9532016-10-21 11:51:50 +01007960 vixl32::Register first_low = LowRegisterFrom(first);
7961 vixl32::Register first_high = HighRegisterFrom(first);
7962 vixl32::Register second_low = LowRegisterFrom(second);
7963 vixl32::Register second_high = HighRegisterFrom(second);
7964 vixl32::Register out_low = LowRegisterFrom(out);
7965 vixl32::Register out_high = HighRegisterFrom(out);
7966
7967 switch (instruction->GetOpKind()) {
7968 case HInstruction::kAnd:
7969 __ Bic(out_low, first_low, second_low);
7970 __ Bic(out_high, first_high, second_high);
7971 break;
7972 case HInstruction::kOr:
7973 __ Orn(out_low, first_low, second_low);
7974 __ Orn(out_high, first_high, second_high);
7975 break;
7976 // There is no EON on arm.
7977 case HInstruction::kXor:
7978 default:
7979 LOG(FATAL) << "Unexpected instruction " << instruction->DebugName();
7980 UNREACHABLE();
7981 }
7982 }
7983}
7984
Anton Kirilov74234da2017-01-13 14:42:47 +00007985void LocationsBuilderARMVIXL::VisitDataProcWithShifterOp(
7986 HDataProcWithShifterOp* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007987 DCHECK(instruction->GetType() == DataType::Type::kInt32 ||
7988 instruction->GetType() == DataType::Type::kInt64);
Anton Kirilov74234da2017-01-13 14:42:47 +00007989 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007990 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007991 const bool overlap = instruction->GetType() == DataType::Type::kInt64 &&
Anton Kirilov74234da2017-01-13 14:42:47 +00007992 HDataProcWithShifterOp::IsExtensionOp(instruction->GetOpKind());
7993
7994 locations->SetInAt(0, Location::RequiresRegister());
7995 locations->SetInAt(1, Location::RequiresRegister());
7996 locations->SetOut(Location::RequiresRegister(),
7997 overlap ? Location::kOutputOverlap : Location::kNoOutputOverlap);
7998}
7999
8000void InstructionCodeGeneratorARMVIXL::VisitDataProcWithShifterOp(
8001 HDataProcWithShifterOp* instruction) {
8002 const LocationSummary* const locations = instruction->GetLocations();
8003 const HInstruction::InstructionKind kind = instruction->GetInstrKind();
8004 const HDataProcWithShifterOp::OpKind op_kind = instruction->GetOpKind();
8005
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008006 if (instruction->GetType() == DataType::Type::kInt32) {
Anton Kirilov420ee302017-02-21 18:10:26 +00008007 const vixl32::Register first = InputRegisterAt(instruction, 0);
8008 const vixl32::Register output = OutputRegister(instruction);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008009 const vixl32::Register second = instruction->InputAt(1)->GetType() == DataType::Type::kInt64
Anton Kirilov74234da2017-01-13 14:42:47 +00008010 ? LowRegisterFrom(locations->InAt(1))
8011 : InputRegisterAt(instruction, 1);
8012
Anton Kirilov420ee302017-02-21 18:10:26 +00008013 if (HDataProcWithShifterOp::IsExtensionOp(op_kind)) {
8014 DCHECK_EQ(kind, HInstruction::kAdd);
8015
8016 switch (op_kind) {
8017 case HDataProcWithShifterOp::kUXTB:
8018 __ Uxtab(output, first, second);
8019 break;
8020 case HDataProcWithShifterOp::kUXTH:
8021 __ Uxtah(output, first, second);
8022 break;
8023 case HDataProcWithShifterOp::kSXTB:
8024 __ Sxtab(output, first, second);
8025 break;
8026 case HDataProcWithShifterOp::kSXTH:
8027 __ Sxtah(output, first, second);
8028 break;
8029 default:
8030 LOG(FATAL) << "Unexpected operation kind: " << op_kind;
8031 UNREACHABLE();
8032 }
8033 } else {
8034 GenerateDataProcInstruction(kind,
8035 output,
8036 first,
8037 Operand(second,
8038 ShiftFromOpKind(op_kind),
8039 instruction->GetShiftAmount()),
8040 codegen_);
8041 }
Anton Kirilov74234da2017-01-13 14:42:47 +00008042 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008043 DCHECK_EQ(instruction->GetType(), DataType::Type::kInt64);
Anton Kirilov74234da2017-01-13 14:42:47 +00008044
8045 if (HDataProcWithShifterOp::IsExtensionOp(op_kind)) {
8046 const vixl32::Register second = InputRegisterAt(instruction, 1);
8047
8048 DCHECK(!LowRegisterFrom(locations->Out()).Is(second));
8049 GenerateDataProc(kind,
8050 locations->Out(),
8051 locations->InAt(0),
8052 second,
8053 Operand(second, ShiftType::ASR, 31),
8054 codegen_);
8055 } else {
8056 GenerateLongDataProc(instruction, codegen_);
8057 }
8058 }
8059}
8060
Artem Serov02109dd2016-09-23 17:17:54 +01008061// TODO(VIXL): Remove optimizations in the helper when they are implemented in vixl.
8062void InstructionCodeGeneratorARMVIXL::GenerateAndConst(vixl32::Register out,
8063 vixl32::Register first,
8064 uint32_t value) {
8065 // Optimize special cases for individual halfs of `and-long` (`and` is simplified earlier).
8066 if (value == 0xffffffffu) {
8067 if (!out.Is(first)) {
8068 __ Mov(out, first);
8069 }
8070 return;
8071 }
8072 if (value == 0u) {
8073 __ Mov(out, 0);
8074 return;
8075 }
8076 if (GetAssembler()->ShifterOperandCanHold(AND, value)) {
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00008077 __ And(out, first, value);
8078 } else if (GetAssembler()->ShifterOperandCanHold(BIC, ~value)) {
8079 __ Bic(out, first, ~value);
Artem Serov02109dd2016-09-23 17:17:54 +01008080 } else {
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00008081 DCHECK(IsPowerOfTwo(value + 1));
8082 __ Ubfx(out, first, 0, WhichPowerOf2(value + 1));
Artem Serov02109dd2016-09-23 17:17:54 +01008083 }
8084}
8085
8086// TODO(VIXL): Remove optimizations in the helper when they are implemented in vixl.
8087void InstructionCodeGeneratorARMVIXL::GenerateOrrConst(vixl32::Register out,
8088 vixl32::Register first,
8089 uint32_t value) {
8090 // Optimize special cases for individual halfs of `or-long` (`or` is simplified earlier).
8091 if (value == 0u) {
8092 if (!out.Is(first)) {
8093 __ Mov(out, first);
8094 }
8095 return;
8096 }
8097 if (value == 0xffffffffu) {
8098 __ Mvn(out, 0);
8099 return;
8100 }
8101 if (GetAssembler()->ShifterOperandCanHold(ORR, value)) {
8102 __ Orr(out, first, value);
8103 } else {
8104 DCHECK(GetAssembler()->ShifterOperandCanHold(ORN, ~value));
8105 __ Orn(out, first, ~value);
8106 }
8107}
8108
8109// TODO(VIXL): Remove optimizations in the helper when they are implemented in vixl.
8110void InstructionCodeGeneratorARMVIXL::GenerateEorConst(vixl32::Register out,
8111 vixl32::Register first,
8112 uint32_t value) {
8113 // Optimize special case for individual halfs of `xor-long` (`xor` is simplified earlier).
8114 if (value == 0u) {
8115 if (!out.Is(first)) {
8116 __ Mov(out, first);
8117 }
8118 return;
8119 }
8120 __ Eor(out, first, value);
8121}
8122
Anton Kirilovdda43962016-11-21 19:55:20 +00008123void InstructionCodeGeneratorARMVIXL::GenerateAddLongConst(Location out,
8124 Location first,
8125 uint64_t value) {
8126 vixl32::Register out_low = LowRegisterFrom(out);
8127 vixl32::Register out_high = HighRegisterFrom(out);
8128 vixl32::Register first_low = LowRegisterFrom(first);
8129 vixl32::Register first_high = HighRegisterFrom(first);
8130 uint32_t value_low = Low32Bits(value);
8131 uint32_t value_high = High32Bits(value);
8132 if (value_low == 0u) {
8133 if (!out_low.Is(first_low)) {
8134 __ Mov(out_low, first_low);
8135 }
8136 __ Add(out_high, first_high, value_high);
8137 return;
8138 }
8139 __ Adds(out_low, first_low, value_low);
Vladimir Markof0a6a1d2018-01-08 14:23:56 +00008140 if (GetAssembler()->ShifterOperandCanHold(ADC, value_high)) {
Anton Kirilovdda43962016-11-21 19:55:20 +00008141 __ Adc(out_high, first_high, value_high);
Anton Kirilovdda43962016-11-21 19:55:20 +00008142 } else {
Vladimir Markof0a6a1d2018-01-08 14:23:56 +00008143 DCHECK(GetAssembler()->ShifterOperandCanHold(SBC, ~value_high));
8144 __ Sbc(out_high, first_high, ~value_high);
Anton Kirilovdda43962016-11-21 19:55:20 +00008145 }
8146}
8147
Artem Serov02109dd2016-09-23 17:17:54 +01008148void InstructionCodeGeneratorARMVIXL::HandleBitwiseOperation(HBinaryOperation* instruction) {
8149 LocationSummary* locations = instruction->GetLocations();
8150 Location first = locations->InAt(0);
8151 Location second = locations->InAt(1);
8152 Location out = locations->Out();
8153
8154 if (second.IsConstant()) {
8155 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
8156 uint32_t value_low = Low32Bits(value);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008157 if (instruction->GetResultType() == DataType::Type::kInt32) {
Artem Serov02109dd2016-09-23 17:17:54 +01008158 vixl32::Register first_reg = InputRegisterAt(instruction, 0);
8159 vixl32::Register out_reg = OutputRegister(instruction);
8160 if (instruction->IsAnd()) {
8161 GenerateAndConst(out_reg, first_reg, value_low);
8162 } else if (instruction->IsOr()) {
8163 GenerateOrrConst(out_reg, first_reg, value_low);
8164 } else {
8165 DCHECK(instruction->IsXor());
8166 GenerateEorConst(out_reg, first_reg, value_low);
8167 }
8168 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008169 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
Artem Serov02109dd2016-09-23 17:17:54 +01008170 uint32_t value_high = High32Bits(value);
8171 vixl32::Register first_low = LowRegisterFrom(first);
8172 vixl32::Register first_high = HighRegisterFrom(first);
8173 vixl32::Register out_low = LowRegisterFrom(out);
8174 vixl32::Register out_high = HighRegisterFrom(out);
8175 if (instruction->IsAnd()) {
8176 GenerateAndConst(out_low, first_low, value_low);
8177 GenerateAndConst(out_high, first_high, value_high);
8178 } else if (instruction->IsOr()) {
8179 GenerateOrrConst(out_low, first_low, value_low);
8180 GenerateOrrConst(out_high, first_high, value_high);
8181 } else {
8182 DCHECK(instruction->IsXor());
8183 GenerateEorConst(out_low, first_low, value_low);
8184 GenerateEorConst(out_high, first_high, value_high);
8185 }
8186 }
8187 return;
8188 }
8189
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008190 if (instruction->GetResultType() == DataType::Type::kInt32) {
Artem Serov02109dd2016-09-23 17:17:54 +01008191 vixl32::Register first_reg = InputRegisterAt(instruction, 0);
8192 vixl32::Register second_reg = InputRegisterAt(instruction, 1);
8193 vixl32::Register out_reg = OutputRegister(instruction);
8194 if (instruction->IsAnd()) {
8195 __ And(out_reg, first_reg, second_reg);
8196 } else if (instruction->IsOr()) {
8197 __ Orr(out_reg, first_reg, second_reg);
8198 } else {
8199 DCHECK(instruction->IsXor());
8200 __ Eor(out_reg, first_reg, second_reg);
8201 }
8202 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008203 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
Artem Serov02109dd2016-09-23 17:17:54 +01008204 vixl32::Register first_low = LowRegisterFrom(first);
8205 vixl32::Register first_high = HighRegisterFrom(first);
8206 vixl32::Register second_low = LowRegisterFrom(second);
8207 vixl32::Register second_high = HighRegisterFrom(second);
8208 vixl32::Register out_low = LowRegisterFrom(out);
8209 vixl32::Register out_high = HighRegisterFrom(out);
8210 if (instruction->IsAnd()) {
8211 __ And(out_low, first_low, second_low);
8212 __ And(out_high, first_high, second_high);
8213 } else if (instruction->IsOr()) {
8214 __ Orr(out_low, first_low, second_low);
8215 __ Orr(out_high, first_high, second_high);
8216 } else {
8217 DCHECK(instruction->IsXor());
8218 __ Eor(out_low, first_low, second_low);
8219 __ Eor(out_high, first_high, second_high);
8220 }
8221 }
8222}
8223
Artem Serovcfbe9132016-10-14 15:58:56 +01008224void InstructionCodeGeneratorARMVIXL::GenerateReferenceLoadOneRegister(
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008225 HInstruction* instruction,
Artem Serovcfbe9132016-10-14 15:58:56 +01008226 Location out,
8227 uint32_t offset,
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008228 Location maybe_temp,
8229 ReadBarrierOption read_barrier_option) {
Artem Serovcfbe9132016-10-14 15:58:56 +01008230 vixl32::Register out_reg = RegisterFrom(out);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008231 if (read_barrier_option == kWithReadBarrier) {
8232 CHECK(kEmitCompilerReadBarrier);
8233 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
8234 if (kUseBakerReadBarrier) {
8235 // Load with fast path based Baker's read barrier.
8236 // /* HeapReference<Object> */ out = *(out + offset)
8237 codegen_->GenerateFieldLoadWithBakerReadBarrier(
8238 instruction, out, out_reg, offset, maybe_temp, /* needs_null_check */ false);
8239 } else {
8240 // Load with slow path based read barrier.
8241 // Save the value of `out` into `maybe_temp` before overwriting it
8242 // in the following move operation, as we will need it for the
8243 // read barrier below.
8244 __ Mov(RegisterFrom(maybe_temp), out_reg);
8245 // /* HeapReference<Object> */ out = *(out + offset)
8246 GetAssembler()->LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
8247 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
8248 }
Artem Serovcfbe9132016-10-14 15:58:56 +01008249 } else {
8250 // Plain load with no read barrier.
8251 // /* HeapReference<Object> */ out = *(out + offset)
8252 GetAssembler()->LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
8253 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
8254 }
8255}
8256
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008257void InstructionCodeGeneratorARMVIXL::GenerateReferenceLoadTwoRegisters(
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008258 HInstruction* instruction,
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008259 Location out,
8260 Location obj,
8261 uint32_t offset,
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008262 Location maybe_temp,
8263 ReadBarrierOption read_barrier_option) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008264 vixl32::Register out_reg = RegisterFrom(out);
8265 vixl32::Register obj_reg = RegisterFrom(obj);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008266 if (read_barrier_option == kWithReadBarrier) {
8267 CHECK(kEmitCompilerReadBarrier);
8268 if (kUseBakerReadBarrier) {
8269 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
8270 // Load with fast path based Baker's read barrier.
8271 // /* HeapReference<Object> */ out = *(obj + offset)
8272 codegen_->GenerateFieldLoadWithBakerReadBarrier(
8273 instruction, out, obj_reg, offset, maybe_temp, /* needs_null_check */ false);
8274 } else {
8275 // Load with slow path based read barrier.
8276 // /* HeapReference<Object> */ out = *(obj + offset)
8277 GetAssembler()->LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
8278 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
8279 }
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008280 } else {
8281 // Plain load with no read barrier.
8282 // /* HeapReference<Object> */ out = *(obj + offset)
8283 GetAssembler()->LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
8284 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
8285 }
8286}
8287
Scott Wakelinga7812ae2016-10-17 10:03:36 +01008288void InstructionCodeGeneratorARMVIXL::GenerateGcRootFieldLoad(
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008289 HInstruction* instruction,
Scott Wakelinga7812ae2016-10-17 10:03:36 +01008290 Location root,
8291 vixl32::Register obj,
8292 uint32_t offset,
Artem Serovd4cc5b22016-11-04 11:19:09 +00008293 ReadBarrierOption read_barrier_option) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01008294 vixl32::Register root_reg = RegisterFrom(root);
Artem Serovd4cc5b22016-11-04 11:19:09 +00008295 if (read_barrier_option == kWithReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008296 DCHECK(kEmitCompilerReadBarrier);
8297 if (kUseBakerReadBarrier) {
8298 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
Roland Levillainba650a42017-03-06 13:52:32 +00008299 // Baker's read barrier are used.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008300 if (kBakerReadBarrierLinkTimeThunksEnableForGcRoots &&
8301 !Runtime::Current()->UseJitCompilation()) {
Roland Levillain6d729a72017-06-30 18:34:01 +01008302 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in
8303 // the Marking Register) to decide whether we need to enter
8304 // the slow path to mark the GC root.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008305 //
8306 // We use link-time generated thunks for the slow path. That thunk
8307 // checks the reference and jumps to the entrypoint if needed.
8308 //
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008309 // lr = &return_address;
8310 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
Roland Levillain6d729a72017-06-30 18:34:01 +01008311 // if (mr) { // Thread::Current()->GetIsGcMarking()
8312 // goto gc_root_thunk<root_reg>(lr)
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008313 // }
8314 // return_address:
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008315
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008316 UseScratchRegisterScope temps(GetVIXLAssembler());
8317 ExcludeIPAndBakerCcEntrypointRegister(&temps, instruction);
Vladimir Marko88abba22017-05-03 17:09:25 +01008318 bool narrow = CanEmitNarrowLdr(root_reg, obj, offset);
8319 uint32_t custom_data = linker::Thumb2RelativePatcher::EncodeBakerReadBarrierGcRootData(
8320 root_reg.GetCode(), narrow);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008321 vixl32::Label* bne_label = codegen_->NewBakerReadBarrierPatch(custom_data);
Roland Levillainba650a42017-03-06 13:52:32 +00008322
Roland Levillain6d729a72017-06-30 18:34:01 +01008323 vixl::EmissionCheckScope guard(GetVIXLAssembler(), 4 * vixl32::kMaxInstructionSizeInBytes);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008324 vixl32::Label return_address;
8325 EmitAdrCode adr(GetVIXLAssembler(), lr, &return_address);
Roland Levillain6d729a72017-06-30 18:34:01 +01008326 __ cmp(mr, Operand(0));
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008327 // Currently the offset is always within range. If that changes,
8328 // we shall have to split the load the same way as for fields.
8329 DCHECK_LT(offset, kReferenceLoadMinFarOffset);
Vladimir Marko88abba22017-05-03 17:09:25 +01008330 ptrdiff_t old_offset = GetVIXLAssembler()->GetBuffer()->GetCursorOffset();
8331 __ ldr(EncodingSize(narrow ? Narrow : Wide), root_reg, MemOperand(obj, offset));
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008332 EmitPlaceholderBne(codegen_, bne_label);
8333 __ Bind(&return_address);
Vladimir Marko88abba22017-05-03 17:09:25 +01008334 DCHECK_EQ(old_offset - GetVIXLAssembler()->GetBuffer()->GetCursorOffset(),
8335 narrow ? BAKER_MARK_INTROSPECTION_GC_ROOT_LDR_NARROW_OFFSET
8336 : BAKER_MARK_INTROSPECTION_GC_ROOT_LDR_WIDE_OFFSET);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008337 } else {
Roland Levillain6d729a72017-06-30 18:34:01 +01008338 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in
8339 // the Marking Register) to decide whether we need to enter
8340 // the slow path to mark the GC root.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008341 //
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008342 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
Roland Levillain6d729a72017-06-30 18:34:01 +01008343 // if (mr) { // Thread::Current()->GetIsGcMarking()
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008344 // // Slow path.
Roland Levillain6d729a72017-06-30 18:34:01 +01008345 // entrypoint = Thread::Current()->pReadBarrierMarkReg ## root.reg()
8346 // root = entrypoint(root); // root = ReadBarrier::Mark(root); // Entry point call.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008347 // }
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008348
Roland Levillain6d729a72017-06-30 18:34:01 +01008349 // Slow path marking the GC root `root`. The entrypoint will
8350 // be loaded by the slow path code.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008351 SlowPathCodeARMVIXL* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01008352 new (codegen_->GetScopedAllocator()) ReadBarrierMarkSlowPathARMVIXL(instruction, root);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008353 codegen_->AddSlowPath(slow_path);
8354
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008355 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
8356 GetAssembler()->LoadFromOffset(kLoadWord, root_reg, obj, offset);
8357 static_assert(
8358 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
8359 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
8360 "have different sizes.");
8361 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
8362 "art::mirror::CompressedReference<mirror::Object> and int32_t "
8363 "have different sizes.");
8364
Roland Levillain6d729a72017-06-30 18:34:01 +01008365 __ CompareAndBranchIfNonZero(mr, slow_path->GetEntryLabel());
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008366 __ Bind(slow_path->GetExitLabel());
8367 }
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008368 } else {
8369 // GC root loaded through a slow path for read barriers other
8370 // than Baker's.
8371 // /* GcRoot<mirror::Object>* */ root = obj + offset
8372 __ Add(root_reg, obj, offset);
8373 // /* mirror::Object* */ root = root->Read()
8374 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
8375 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01008376 } else {
8377 // Plain GC root load with no read barrier.
8378 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
8379 GetAssembler()->LoadFromOffset(kLoadWord, root_reg, obj, offset);
8380 // Note that GC roots are not affected by heap poisoning, thus we
8381 // do not have to unpoison `root_reg` here.
8382 }
Roland Levillain5daa4952017-07-03 17:23:56 +01008383 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 18);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01008384}
8385
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008386void CodeGeneratorARMVIXL::MaybeAddBakerCcEntrypointTempForFields(LocationSummary* locations) {
8387 DCHECK(kEmitCompilerReadBarrier);
8388 DCHECK(kUseBakerReadBarrier);
8389 if (kBakerReadBarrierLinkTimeThunksEnableForFields) {
8390 if (!Runtime::Current()->UseJitCompilation()) {
8391 locations->AddTemp(Location::RegisterLocation(kBakerCcEntrypointRegister.GetCode()));
8392 }
8393 }
8394}
8395
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008396void CodeGeneratorARMVIXL::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
8397 Location ref,
8398 vixl32::Register obj,
8399 uint32_t offset,
8400 Location temp,
8401 bool needs_null_check) {
8402 DCHECK(kEmitCompilerReadBarrier);
8403 DCHECK(kUseBakerReadBarrier);
8404
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008405 if (kBakerReadBarrierLinkTimeThunksEnableForFields &&
8406 !Runtime::Current()->UseJitCompilation()) {
Roland Levillain6d729a72017-06-30 18:34:01 +01008407 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in the
8408 // Marking Register) to decide whether we need to enter the slow
8409 // path to mark the reference. Then, in the slow path, check the
8410 // gray bit in the lock word of the reference's holder (`obj`) to
8411 // decide whether to mark `ref` or not.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008412 //
8413 // We use link-time generated thunks for the slow path. That thunk checks
8414 // the holder and jumps to the entrypoint if needed. If the holder is not
8415 // gray, it creates a fake dependency and returns to the LDR instruction.
8416 //
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008417 // lr = &gray_return_address;
Roland Levillain6d729a72017-06-30 18:34:01 +01008418 // if (mr) { // Thread::Current()->GetIsGcMarking()
8419 // goto field_thunk<holder_reg, base_reg>(lr)
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008420 // }
8421 // not_gray_return_address:
8422 // // Original reference load. If the offset is too large to fit
8423 // // into LDR, we use an adjusted base register here.
Vladimir Marko88abba22017-05-03 17:09:25 +01008424 // HeapReference<mirror::Object> reference = *(obj+offset);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008425 // gray_return_address:
8426
8427 DCHECK_ALIGNED(offset, sizeof(mirror::HeapReference<mirror::Object>));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008428 vixl32::Register ref_reg = RegisterFrom(ref, DataType::Type::kReference);
Vladimir Marko88abba22017-05-03 17:09:25 +01008429 bool narrow = CanEmitNarrowLdr(ref_reg, obj, offset);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008430 vixl32::Register base = obj;
8431 if (offset >= kReferenceLoadMinFarOffset) {
8432 base = RegisterFrom(temp);
8433 DCHECK(!base.Is(kBakerCcEntrypointRegister));
8434 static_assert(IsPowerOfTwo(kReferenceLoadMinFarOffset), "Expecting a power of 2.");
8435 __ Add(base, obj, Operand(offset & ~(kReferenceLoadMinFarOffset - 1u)));
8436 offset &= (kReferenceLoadMinFarOffset - 1u);
Vladimir Marko88abba22017-05-03 17:09:25 +01008437 // Use narrow LDR only for small offsets. Generating narrow encoding LDR for the large
8438 // offsets with `(offset & (kReferenceLoadMinFarOffset - 1u)) < 32u` would most likely
8439 // increase the overall code size when taking the generated thunks into account.
8440 DCHECK(!narrow);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008441 }
8442 UseScratchRegisterScope temps(GetVIXLAssembler());
8443 ExcludeIPAndBakerCcEntrypointRegister(&temps, instruction);
8444 uint32_t custom_data = linker::Thumb2RelativePatcher::EncodeBakerReadBarrierFieldData(
Vladimir Marko88abba22017-05-03 17:09:25 +01008445 base.GetCode(), obj.GetCode(), narrow);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008446 vixl32::Label* bne_label = NewBakerReadBarrierPatch(custom_data);
8447
Roland Levillain5daa4952017-07-03 17:23:56 +01008448 {
8449 vixl::EmissionCheckScope guard(
8450 GetVIXLAssembler(),
8451 (kPoisonHeapReferences ? 5u : 4u) * vixl32::kMaxInstructionSizeInBytes);
8452 vixl32::Label return_address;
8453 EmitAdrCode adr(GetVIXLAssembler(), lr, &return_address);
8454 __ cmp(mr, Operand(0));
8455 EmitPlaceholderBne(this, bne_label);
8456 ptrdiff_t old_offset = GetVIXLAssembler()->GetBuffer()->GetCursorOffset();
8457 __ ldr(EncodingSize(narrow ? Narrow : Wide), ref_reg, MemOperand(base, offset));
8458 if (needs_null_check) {
8459 MaybeRecordImplicitNullCheck(instruction);
Vladimir Marko88abba22017-05-03 17:09:25 +01008460 }
Roland Levillain5daa4952017-07-03 17:23:56 +01008461 // Note: We need a specific width for the unpoisoning NEG.
8462 if (kPoisonHeapReferences) {
8463 if (narrow) {
8464 // The only 16-bit encoding is T1 which sets flags outside IT block (i.e. RSBS, not RSB).
8465 __ rsbs(EncodingSize(Narrow), ref_reg, ref_reg, Operand(0));
8466 } else {
8467 __ rsb(EncodingSize(Wide), ref_reg, ref_reg, Operand(0));
8468 }
8469 }
8470 __ Bind(&return_address);
8471 DCHECK_EQ(old_offset - GetVIXLAssembler()->GetBuffer()->GetCursorOffset(),
8472 narrow ? BAKER_MARK_INTROSPECTION_FIELD_LDR_NARROW_OFFSET
8473 : BAKER_MARK_INTROSPECTION_FIELD_LDR_WIDE_OFFSET);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008474 }
Roland Levillain5daa4952017-07-03 17:23:56 +01008475 MaybeGenerateMarkingRegisterCheck(/* code */ 19, /* temp_loc */ LocationFrom(ip));
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008476 return;
8477 }
8478
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008479 // /* HeapReference<Object> */ ref = *(obj + offset)
8480 Location no_index = Location::NoLocation();
8481 ScaleFactor no_scale_factor = TIMES_1;
8482 GenerateReferenceLoadWithBakerReadBarrier(
8483 instruction, ref, obj, offset, no_index, no_scale_factor, temp, needs_null_check);
Roland Levillain6070e882016-11-03 17:51:58 +00008484}
8485
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008486void CodeGeneratorARMVIXL::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
8487 Location ref,
8488 vixl32::Register obj,
8489 uint32_t data_offset,
8490 Location index,
8491 Location temp,
8492 bool needs_null_check) {
8493 DCHECK(kEmitCompilerReadBarrier);
8494 DCHECK(kUseBakerReadBarrier);
8495
8496 static_assert(
8497 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
8498 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008499 ScaleFactor scale_factor = TIMES_4;
8500
8501 if (kBakerReadBarrierLinkTimeThunksEnableForArrays &&
8502 !Runtime::Current()->UseJitCompilation()) {
Roland Levillain6d729a72017-06-30 18:34:01 +01008503 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in the
8504 // Marking Register) to decide whether we need to enter the slow
8505 // path to mark the reference. Then, in the slow path, check the
8506 // gray bit in the lock word of the reference's holder (`obj`) to
8507 // decide whether to mark `ref` or not.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008508 //
8509 // We use link-time generated thunks for the slow path. That thunk checks
8510 // the holder and jumps to the entrypoint if needed. If the holder is not
8511 // gray, it creates a fake dependency and returns to the LDR instruction.
8512 //
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008513 // lr = &gray_return_address;
Roland Levillain6d729a72017-06-30 18:34:01 +01008514 // if (mr) { // Thread::Current()->GetIsGcMarking()
8515 // goto array_thunk<base_reg>(lr)
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008516 // }
8517 // not_gray_return_address:
8518 // // Original reference load. If the offset is too large to fit
8519 // // into LDR, we use an adjusted base register here.
Vladimir Marko88abba22017-05-03 17:09:25 +01008520 // HeapReference<mirror::Object> reference = data[index];
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008521 // gray_return_address:
8522
8523 DCHECK(index.IsValid());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008524 vixl32::Register index_reg = RegisterFrom(index, DataType::Type::kInt32);
8525 vixl32::Register ref_reg = RegisterFrom(ref, DataType::Type::kReference);
8526 vixl32::Register data_reg = RegisterFrom(temp, DataType::Type::kInt32); // Raw pointer.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008527 DCHECK(!data_reg.Is(kBakerCcEntrypointRegister));
8528
8529 UseScratchRegisterScope temps(GetVIXLAssembler());
8530 ExcludeIPAndBakerCcEntrypointRegister(&temps, instruction);
8531 uint32_t custom_data =
8532 linker::Thumb2RelativePatcher::EncodeBakerReadBarrierArrayData(data_reg.GetCode());
8533 vixl32::Label* bne_label = NewBakerReadBarrierPatch(custom_data);
8534
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008535 __ Add(data_reg, obj, Operand(data_offset));
Roland Levillain5daa4952017-07-03 17:23:56 +01008536 {
8537 vixl::EmissionCheckScope guard(
8538 GetVIXLAssembler(),
8539 (kPoisonHeapReferences ? 5u : 4u) * vixl32::kMaxInstructionSizeInBytes);
8540 vixl32::Label return_address;
8541 EmitAdrCode adr(GetVIXLAssembler(), lr, &return_address);
8542 __ cmp(mr, Operand(0));
8543 EmitPlaceholderBne(this, bne_label);
8544 ptrdiff_t old_offset = GetVIXLAssembler()->GetBuffer()->GetCursorOffset();
8545 __ ldr(ref_reg, MemOperand(data_reg, index_reg, vixl32::LSL, scale_factor));
8546 DCHECK(!needs_null_check); // The thunk cannot handle the null check.
8547 // Note: We need a Wide NEG for the unpoisoning.
8548 if (kPoisonHeapReferences) {
8549 __ rsb(EncodingSize(Wide), ref_reg, ref_reg, Operand(0));
8550 }
8551 __ Bind(&return_address);
8552 DCHECK_EQ(old_offset - GetVIXLAssembler()->GetBuffer()->GetCursorOffset(),
8553 BAKER_MARK_INTROSPECTION_ARRAY_LDR_OFFSET);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008554 }
Roland Levillain5daa4952017-07-03 17:23:56 +01008555 MaybeGenerateMarkingRegisterCheck(/* code */ 20, /* temp_loc */ LocationFrom(ip));
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008556 return;
8557 }
8558
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008559 // /* HeapReference<Object> */ ref =
8560 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008561 GenerateReferenceLoadWithBakerReadBarrier(
8562 instruction, ref, obj, data_offset, index, scale_factor, temp, needs_null_check);
Roland Levillain6070e882016-11-03 17:51:58 +00008563}
8564
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008565void CodeGeneratorARMVIXL::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
8566 Location ref,
8567 vixl32::Register obj,
8568 uint32_t offset,
8569 Location index,
8570 ScaleFactor scale_factor,
8571 Location temp,
Roland Levillainff487002017-03-07 16:50:01 +00008572 bool needs_null_check) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008573 DCHECK(kEmitCompilerReadBarrier);
8574 DCHECK(kUseBakerReadBarrier);
8575
Roland Levillain6d729a72017-06-30 18:34:01 +01008576 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in the
8577 // Marking Register) to decide whether we need to enter the slow
8578 // path to mark the reference. Then, in the slow path, check the
8579 // gray bit in the lock word of the reference's holder (`obj`) to
8580 // decide whether to mark `ref` or not.
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008581 //
Roland Levillain6d729a72017-06-30 18:34:01 +01008582 // if (mr) { // Thread::Current()->GetIsGcMarking()
Roland Levillainff487002017-03-07 16:50:01 +00008583 // // Slow path.
8584 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
8585 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
8586 // HeapReference<mirror::Object> ref = *src; // Original reference load.
8587 // bool is_gray = (rb_state == ReadBarrier::GrayState());
8588 // if (is_gray) {
Roland Levillain6d729a72017-06-30 18:34:01 +01008589 // entrypoint = Thread::Current()->pReadBarrierMarkReg ## root.reg()
8590 // ref = entrypoint(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
Roland Levillainff487002017-03-07 16:50:01 +00008591 // }
8592 // } else {
8593 // HeapReference<mirror::Object> ref = *src; // Original reference load.
8594 // }
8595
8596 vixl32::Register temp_reg = RegisterFrom(temp);
8597
8598 // Slow path marking the object `ref` when the GC is marking. The
Roland Levillain6d729a72017-06-30 18:34:01 +01008599 // entrypoint will be loaded by the slow path code.
Roland Levillainff487002017-03-07 16:50:01 +00008600 SlowPathCodeARMVIXL* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01008601 new (GetScopedAllocator()) LoadReferenceWithBakerReadBarrierSlowPathARMVIXL(
Roland Levillain6d729a72017-06-30 18:34:01 +01008602 instruction, ref, obj, offset, index, scale_factor, needs_null_check, temp_reg);
Roland Levillainff487002017-03-07 16:50:01 +00008603 AddSlowPath(slow_path);
8604
Roland Levillain6d729a72017-06-30 18:34:01 +01008605 __ CompareAndBranchIfNonZero(mr, slow_path->GetEntryLabel());
Roland Levillainff487002017-03-07 16:50:01 +00008606 // Fast path: the GC is not marking: just load the reference.
8607 GenerateRawReferenceLoad(instruction, ref, obj, offset, index, scale_factor, needs_null_check);
8608 __ Bind(slow_path->GetExitLabel());
Roland Levillain5daa4952017-07-03 17:23:56 +01008609 MaybeGenerateMarkingRegisterCheck(/* code */ 21);
Roland Levillainff487002017-03-07 16:50:01 +00008610}
8611
8612void CodeGeneratorARMVIXL::UpdateReferenceFieldWithBakerReadBarrier(HInstruction* instruction,
8613 Location ref,
8614 vixl32::Register obj,
8615 Location field_offset,
8616 Location temp,
8617 bool needs_null_check,
8618 vixl32::Register temp2) {
8619 DCHECK(kEmitCompilerReadBarrier);
8620 DCHECK(kUseBakerReadBarrier);
8621
Roland Levillain6d729a72017-06-30 18:34:01 +01008622 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in the
8623 // Marking Register) to decide whether we need to enter the slow
8624 // path to update the reference field within `obj`. Then, in the
8625 // slow path, check the gray bit in the lock word of the reference's
8626 // holder (`obj`) to decide whether to mark `ref` and update the
8627 // field or not.
Roland Levillainff487002017-03-07 16:50:01 +00008628 //
Roland Levillain6d729a72017-06-30 18:34:01 +01008629 // if (mr) { // Thread::Current()->GetIsGcMarking()
Roland Levillainba650a42017-03-06 13:52:32 +00008630 // // Slow path.
Roland Levillain54f869e2017-03-06 13:54:11 +00008631 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
8632 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
Roland Levillainff487002017-03-07 16:50:01 +00008633 // HeapReference<mirror::Object> ref = *(obj + field_offset); // Reference load.
Roland Levillain54f869e2017-03-06 13:54:11 +00008634 // bool is_gray = (rb_state == ReadBarrier::GrayState());
8635 // if (is_gray) {
Roland Levillainff487002017-03-07 16:50:01 +00008636 // old_ref = ref;
Roland Levillain6d729a72017-06-30 18:34:01 +01008637 // entrypoint = Thread::Current()->pReadBarrierMarkReg ## root.reg()
8638 // ref = entrypoint(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
Roland Levillainff487002017-03-07 16:50:01 +00008639 // compareAndSwapObject(obj, field_offset, old_ref, ref);
Roland Levillain54f869e2017-03-06 13:54:11 +00008640 // }
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008641 // }
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008642
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008643 vixl32::Register temp_reg = RegisterFrom(temp);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008644
Roland Levillainff487002017-03-07 16:50:01 +00008645 // Slow path updating the object reference at address `obj + field_offset`
Roland Levillain6d729a72017-06-30 18:34:01 +01008646 // when the GC is marking. The entrypoint will be loaded by the slow path code.
Vladimir Marko174b2e22017-10-12 13:34:49 +01008647 SlowPathCodeARMVIXL* slow_path =
8648 new (GetScopedAllocator()) LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARMVIXL(
Roland Levillainff487002017-03-07 16:50:01 +00008649 instruction,
8650 ref,
8651 obj,
8652 /* offset */ 0u,
8653 /* index */ field_offset,
8654 /* scale_factor */ ScaleFactor::TIMES_1,
8655 needs_null_check,
8656 temp_reg,
Roland Levillain6d729a72017-06-30 18:34:01 +01008657 temp2);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008658 AddSlowPath(slow_path);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008659
Roland Levillain6d729a72017-06-30 18:34:01 +01008660 __ CompareAndBranchIfNonZero(mr, slow_path->GetEntryLabel());
Roland Levillainff487002017-03-07 16:50:01 +00008661 // Fast path: the GC is not marking: nothing to do (the field is
8662 // up-to-date, and we don't need to load the reference).
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008663 __ Bind(slow_path->GetExitLabel());
Roland Levillain5daa4952017-07-03 17:23:56 +01008664 MaybeGenerateMarkingRegisterCheck(/* code */ 22);
Roland Levillain844e6532016-11-03 16:09:47 +00008665}
Scott Wakelingfe885462016-09-22 10:24:38 +01008666
Roland Levillainba650a42017-03-06 13:52:32 +00008667void CodeGeneratorARMVIXL::GenerateRawReferenceLoad(HInstruction* instruction,
8668 Location ref,
8669 vixl::aarch32::Register obj,
8670 uint32_t offset,
8671 Location index,
8672 ScaleFactor scale_factor,
8673 bool needs_null_check) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008674 DataType::Type type = DataType::Type::kReference;
Roland Levillainba650a42017-03-06 13:52:32 +00008675 vixl32::Register ref_reg = RegisterFrom(ref, type);
8676
8677 // If needed, vixl::EmissionCheckScope guards are used to ensure
8678 // that no pools are emitted between the load (macro) instruction
8679 // and MaybeRecordImplicitNullCheck.
8680
Scott Wakelingfe885462016-09-22 10:24:38 +01008681 if (index.IsValid()) {
8682 // Load types involving an "index": ArrayGet,
8683 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
8684 // intrinsics.
Roland Levillainba650a42017-03-06 13:52:32 +00008685 // /* HeapReference<mirror::Object> */ ref = *(obj + offset + (index << scale_factor))
Scott Wakelingfe885462016-09-22 10:24:38 +01008686 if (index.IsConstant()) {
8687 size_t computed_offset =
8688 (Int32ConstantFrom(index) << scale_factor) + offset;
Roland Levillainba650a42017-03-06 13:52:32 +00008689 vixl::EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Scott Wakelingfe885462016-09-22 10:24:38 +01008690 GetAssembler()->LoadFromOffset(kLoadWord, ref_reg, obj, computed_offset);
Roland Levillainba650a42017-03-06 13:52:32 +00008691 if (needs_null_check) {
8692 MaybeRecordImplicitNullCheck(instruction);
8693 }
Scott Wakelingfe885462016-09-22 10:24:38 +01008694 } else {
8695 // Handle the special case of the
8696 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
8697 // intrinsics, which use a register pair as index ("long
8698 // offset"), of which only the low part contains data.
8699 vixl32::Register index_reg = index.IsRegisterPair()
8700 ? LowRegisterFrom(index)
8701 : RegisterFrom(index);
8702 UseScratchRegisterScope temps(GetVIXLAssembler());
Roland Levillainba650a42017-03-06 13:52:32 +00008703 vixl32::Register temp = temps.Acquire();
8704 __ Add(temp, obj, Operand(index_reg, ShiftType::LSL, scale_factor));
8705 {
8706 vixl::EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
8707 GetAssembler()->LoadFromOffset(kLoadWord, ref_reg, temp, offset);
8708 if (needs_null_check) {
8709 MaybeRecordImplicitNullCheck(instruction);
8710 }
8711 }
Scott Wakelingfe885462016-09-22 10:24:38 +01008712 }
8713 } else {
Roland Levillainba650a42017-03-06 13:52:32 +00008714 // /* HeapReference<mirror::Object> */ ref = *(obj + offset)
8715 vixl::EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Scott Wakelingfe885462016-09-22 10:24:38 +01008716 GetAssembler()->LoadFromOffset(kLoadWord, ref_reg, obj, offset);
Roland Levillainba650a42017-03-06 13:52:32 +00008717 if (needs_null_check) {
8718 MaybeRecordImplicitNullCheck(instruction);
8719 }
Scott Wakelingfe885462016-09-22 10:24:38 +01008720 }
8721
Roland Levillain844e6532016-11-03 16:09:47 +00008722 // Object* ref = ref_addr->AsMirrorPtr()
8723 GetAssembler()->MaybeUnpoisonHeapReference(ref_reg);
Roland Levillain844e6532016-11-03 16:09:47 +00008724}
8725
Roland Levillain5daa4952017-07-03 17:23:56 +01008726void CodeGeneratorARMVIXL::MaybeGenerateMarkingRegisterCheck(int code, Location temp_loc) {
8727 // The following condition is a compile-time one, so it does not have a run-time cost.
8728 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier && kIsDebugBuild) {
8729 // The following condition is a run-time one; it is executed after the
8730 // previous compile-time test, to avoid penalizing non-debug builds.
8731 if (GetCompilerOptions().EmitRunTimeChecksInDebugMode()) {
8732 UseScratchRegisterScope temps(GetVIXLAssembler());
8733 vixl32::Register temp = temp_loc.IsValid() ? RegisterFrom(temp_loc) : temps.Acquire();
8734 GetAssembler()->GenerateMarkingRegisterCheck(temp,
8735 kMarkingRegisterCheckBreakCodeBaseCode + code);
8736 }
8737 }
8738}
8739
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008740void CodeGeneratorARMVIXL::GenerateReadBarrierSlow(HInstruction* instruction,
8741 Location out,
8742 Location ref,
8743 Location obj,
8744 uint32_t offset,
8745 Location index) {
8746 DCHECK(kEmitCompilerReadBarrier);
8747
8748 // Insert a slow path based read barrier *after* the reference load.
8749 //
8750 // If heap poisoning is enabled, the unpoisoning of the loaded
8751 // reference will be carried out by the runtime within the slow
8752 // path.
8753 //
8754 // Note that `ref` currently does not get unpoisoned (when heap
8755 // poisoning is enabled), which is alright as the `ref` argument is
8756 // not used by the artReadBarrierSlow entry point.
8757 //
8758 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Vladimir Marko174b2e22017-10-12 13:34:49 +01008759 SlowPathCodeARMVIXL* slow_path = new (GetScopedAllocator())
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008760 ReadBarrierForHeapReferenceSlowPathARMVIXL(instruction, out, ref, obj, offset, index);
8761 AddSlowPath(slow_path);
8762
8763 __ B(slow_path->GetEntryLabel());
8764 __ Bind(slow_path->GetExitLabel());
8765}
8766
8767void CodeGeneratorARMVIXL::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
Artem Serov02d37832016-10-25 15:25:33 +01008768 Location out,
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008769 Location ref,
8770 Location obj,
8771 uint32_t offset,
8772 Location index) {
Artem Serov02d37832016-10-25 15:25:33 +01008773 if (kEmitCompilerReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008774 // Baker's read barriers shall be handled by the fast path
Roland Levillain9983e302017-07-14 14:34:22 +01008775 // (CodeGeneratorARMVIXL::GenerateReferenceLoadWithBakerReadBarrier).
Artem Serov02d37832016-10-25 15:25:33 +01008776 DCHECK(!kUseBakerReadBarrier);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008777 // If heap poisoning is enabled, unpoisoning will be taken care of
8778 // by the runtime within the slow path.
8779 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Artem Serov02d37832016-10-25 15:25:33 +01008780 } else if (kPoisonHeapReferences) {
8781 GetAssembler()->UnpoisonHeapReference(RegisterFrom(out));
8782 }
8783}
8784
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008785void CodeGeneratorARMVIXL::GenerateReadBarrierForRootSlow(HInstruction* instruction,
8786 Location out,
8787 Location root) {
8788 DCHECK(kEmitCompilerReadBarrier);
8789
8790 // Insert a slow path based read barrier *after* the GC root load.
8791 //
8792 // Note that GC roots are not affected by heap poisoning, so we do
8793 // not need to do anything special for this here.
8794 SlowPathCodeARMVIXL* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01008795 new (GetScopedAllocator()) ReadBarrierForRootSlowPathARMVIXL(instruction, out, root);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008796 AddSlowPath(slow_path);
8797
8798 __ B(slow_path->GetEntryLabel());
8799 __ Bind(slow_path->GetExitLabel());
8800}
8801
Artem Serov02d37832016-10-25 15:25:33 +01008802// Check if the desired_dispatch_info is supported. If it is, return it,
8803// otherwise return a fall-back info that should be used instead.
8804HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARMVIXL::GetSupportedInvokeStaticOrDirectDispatch(
Artem Serovd4cc5b22016-11-04 11:19:09 +00008805 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +00008806 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Nicolas Geoffraye807ff72017-01-23 09:03:12 +00008807 return desired_dispatch_info;
Artem Serov02d37832016-10-25 15:25:33 +01008808}
8809
Scott Wakelingfe885462016-09-22 10:24:38 +01008810vixl32::Register CodeGeneratorARMVIXL::GetInvokeStaticOrDirectExtraParameter(
8811 HInvokeStaticOrDirect* invoke, vixl32::Register temp) {
8812 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
8813 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
8814 if (!invoke->GetLocations()->Intrinsified()) {
8815 return RegisterFrom(location);
8816 }
8817 // For intrinsics we allow any location, so it may be on the stack.
8818 if (!location.IsRegister()) {
8819 GetAssembler()->LoadFromOffset(kLoadWord, temp, sp, location.GetStackIndex());
8820 return temp;
8821 }
8822 // For register locations, check if the register was saved. If so, get it from the stack.
8823 // Note: There is a chance that the register was saved but not overwritten, so we could
8824 // save one load. However, since this is just an intrinsic slow path we prefer this
8825 // simple and more robust approach rather that trying to determine if that's the case.
8826 SlowPathCode* slow_path = GetCurrentSlowPath();
Scott Wakelingd5cd4972017-02-03 11:38:35 +00008827 if (slow_path != nullptr && slow_path->IsCoreRegisterSaved(RegisterFrom(location).GetCode())) {
Scott Wakelingfe885462016-09-22 10:24:38 +01008828 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(RegisterFrom(location).GetCode());
8829 GetAssembler()->LoadFromOffset(kLoadWord, temp, sp, stack_offset);
8830 return temp;
8831 }
8832 return RegisterFrom(location);
8833}
8834
Vladimir Markod254f5c2017-06-02 15:18:36 +00008835void CodeGeneratorARMVIXL::GenerateStaticOrDirectCall(
Vladimir Markoe7197bf2017-06-02 17:00:23 +01008836 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00008837 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
Scott Wakelingfe885462016-09-22 10:24:38 +01008838 switch (invoke->GetMethodLoadKind()) {
8839 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
8840 uint32_t offset =
8841 GetThreadOffset<kArmPointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
8842 // temp = thread->string_init_entrypoint
Artem Serovd4cc5b22016-11-04 11:19:09 +00008843 GetAssembler()->LoadFromOffset(kLoadWord, RegisterFrom(temp), tr, offset);
8844 break;
8845 }
8846 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
8847 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
8848 break;
Vladimir Marko65979462017-05-19 17:25:12 +01008849 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative: {
8850 DCHECK(GetCompilerOptions().IsBootImage());
8851 PcRelativePatchInfo* labels = NewPcRelativeMethodPatch(invoke->GetTargetMethod());
8852 vixl32::Register temp_reg = RegisterFrom(temp);
8853 EmitMovwMovtPlaceholder(labels, temp_reg);
8854 break;
8855 }
Artem Serovd4cc5b22016-11-04 11:19:09 +00008856 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
8857 __ Mov(RegisterFrom(temp), Operand::From(invoke->GetMethodAddress()));
8858 break;
Vladimir Marko0eb882b2017-05-15 13:39:18 +01008859 case HInvokeStaticOrDirect::MethodLoadKind::kBssEntry: {
8860 PcRelativePatchInfo* labels = NewMethodBssEntryPatch(
8861 MethodReference(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex()));
8862 vixl32::Register temp_reg = RegisterFrom(temp);
8863 EmitMovwMovtPlaceholder(labels, temp_reg);
8864 GetAssembler()->LoadFromOffset(kLoadWord, temp_reg, temp_reg, /* offset*/ 0);
Scott Wakelingfe885462016-09-22 10:24:38 +01008865 break;
8866 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01008867 case HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall: {
8868 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
8869 return; // No code pointer retrieval; the runtime performs the call directly.
Scott Wakelingfe885462016-09-22 10:24:38 +01008870 }
Scott Wakelingfe885462016-09-22 10:24:38 +01008871 }
8872
Artem Serovd4cc5b22016-11-04 11:19:09 +00008873 switch (invoke->GetCodePtrLocation()) {
8874 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Vladimir Markoe7197bf2017-06-02 17:00:23 +01008875 {
8876 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
8877 ExactAssemblyScope aas(GetVIXLAssembler(),
8878 vixl32::k32BitT32InstructionSizeInBytes,
8879 CodeBufferCheckScope::kMaximumSize);
8880 __ bl(GetFrameEntryLabel());
8881 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
8882 }
Artem Serovd4cc5b22016-11-04 11:19:09 +00008883 break;
Artem Serovd4cc5b22016-11-04 11:19:09 +00008884 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
8885 // LR = callee_method->entry_point_from_quick_compiled_code_
8886 GetAssembler()->LoadFromOffset(
8887 kLoadWord,
8888 lr,
8889 RegisterFrom(callee_method),
8890 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize).Int32Value());
Alexandre Rames374ddf32016-11-04 10:40:49 +00008891 {
Vladimir Markoe7197bf2017-06-02 17:00:23 +01008892 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
Alexandre Rames374ddf32016-11-04 10:40:49 +00008893 // 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 +00008894 ExactAssemblyScope aas(GetVIXLAssembler(),
8895 vixl32::k16BitT32InstructionSizeInBytes,
8896 CodeBufferCheckScope::kExactSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00008897 // LR()
8898 __ blx(lr);
Vladimir Markoe7197bf2017-06-02 17:00:23 +01008899 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Alexandre Rames374ddf32016-11-04 10:40:49 +00008900 }
Artem Serovd4cc5b22016-11-04 11:19:09 +00008901 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01008902 }
8903
Scott Wakelingfe885462016-09-22 10:24:38 +01008904 DCHECK(!IsLeafMethod());
8905}
8906
Vladimir Markoe7197bf2017-06-02 17:00:23 +01008907void CodeGeneratorARMVIXL::GenerateVirtualCall(
8908 HInvokeVirtual* invoke, Location temp_location, SlowPathCode* slow_path) {
Scott Wakelingfe885462016-09-22 10:24:38 +01008909 vixl32::Register temp = RegisterFrom(temp_location);
8910 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
8911 invoke->GetVTableIndex(), kArmPointerSize).Uint32Value();
8912
8913 // Use the calling convention instead of the location of the receiver, as
8914 // intrinsics may have put the receiver in a different register. In the intrinsics
8915 // slow path, the arguments have been moved to the right place, so here we are
8916 // guaranteed that the receiver is the first register of the calling convention.
8917 InvokeDexCallingConventionARMVIXL calling_convention;
8918 vixl32::Register receiver = calling_convention.GetRegisterAt(0);
8919 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Alexandre Rames374ddf32016-11-04 10:40:49 +00008920 {
8921 // Make sure the pc is recorded immediately after the `ldr` instruction.
Artem Serov0fb37192016-12-06 18:13:40 +00008922 ExactAssemblyScope aas(GetVIXLAssembler(),
8923 vixl32::kMaxInstructionSizeInBytes,
8924 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00008925 // /* HeapReference<Class> */ temp = receiver->klass_
8926 __ ldr(temp, MemOperand(receiver, class_offset));
8927 MaybeRecordImplicitNullCheck(invoke);
8928 }
Scott Wakelingfe885462016-09-22 10:24:38 +01008929 // Instead of simply (possibly) unpoisoning `temp` here, we should
8930 // emit a read barrier for the previous class reference load.
8931 // However this is not required in practice, as this is an
8932 // intermediate/temporary reference and because the current
8933 // concurrent copying collector keeps the from-space memory
8934 // intact/accessible until the end of the marking phase (the
8935 // concurrent copying collector may not in the future).
8936 GetAssembler()->MaybeUnpoisonHeapReference(temp);
8937
8938 // temp = temp->GetMethodAt(method_offset);
8939 uint32_t entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(
8940 kArmPointerSize).Int32Value();
8941 GetAssembler()->LoadFromOffset(kLoadWord, temp, temp, method_offset);
8942 // LR = temp->GetEntryPoint();
8943 GetAssembler()->LoadFromOffset(kLoadWord, lr, temp, entry_point);
Vladimir Markoe7197bf2017-06-02 17:00:23 +01008944 {
8945 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
8946 // blx in T32 has only 16bit encoding that's why a stricter check for the scope is used.
8947 ExactAssemblyScope aas(GetVIXLAssembler(),
8948 vixl32::k16BitT32InstructionSizeInBytes,
8949 CodeBufferCheckScope::kExactSize);
8950 // LR();
8951 __ blx(lr);
8952 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
8953 }
Scott Wakelingfe885462016-09-22 10:24:38 +01008954}
8955
Vladimir Marko65979462017-05-19 17:25:12 +01008956CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewPcRelativeMethodPatch(
8957 MethodReference target_method) {
8958 return NewPcRelativePatch(*target_method.dex_file,
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07008959 target_method.index,
Vladimir Marko65979462017-05-19 17:25:12 +01008960 &pc_relative_method_patches_);
Artem Serovd4cc5b22016-11-04 11:19:09 +00008961}
8962
Vladimir Marko0eb882b2017-05-15 13:39:18 +01008963CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewMethodBssEntryPatch(
8964 MethodReference target_method) {
8965 return NewPcRelativePatch(*target_method.dex_file,
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07008966 target_method.index,
Vladimir Marko0eb882b2017-05-15 13:39:18 +01008967 &method_bss_entry_patches_);
8968}
8969
Artem Serovd4cc5b22016-11-04 11:19:09 +00008970CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewPcRelativeTypePatch(
8971 const DexFile& dex_file, dex::TypeIndex type_index) {
8972 return NewPcRelativePatch(dex_file, type_index.index_, &pc_relative_type_patches_);
8973}
8974
Vladimir Marko1998cd02017-01-13 13:02:58 +00008975CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewTypeBssEntryPatch(
8976 const DexFile& dex_file, dex::TypeIndex type_index) {
8977 return NewPcRelativePatch(dex_file, type_index.index_, &type_bss_entry_patches_);
8978}
8979
Vladimir Marko65979462017-05-19 17:25:12 +01008980CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewPcRelativeStringPatch(
8981 const DexFile& dex_file, dex::StringIndex string_index) {
8982 return NewPcRelativePatch(dex_file, string_index.index_, &pc_relative_string_patches_);
8983}
8984
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01008985CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewStringBssEntryPatch(
8986 const DexFile& dex_file, dex::StringIndex string_index) {
8987 return NewPcRelativePatch(dex_file, string_index.index_, &string_bss_entry_patches_);
8988}
8989
Artem Serovd4cc5b22016-11-04 11:19:09 +00008990CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewPcRelativePatch(
8991 const DexFile& dex_file, uint32_t offset_or_index, ArenaDeque<PcRelativePatchInfo>* patches) {
8992 patches->emplace_back(dex_file, offset_or_index);
8993 return &patches->back();
8994}
8995
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008996vixl::aarch32::Label* CodeGeneratorARMVIXL::NewBakerReadBarrierPatch(uint32_t custom_data) {
8997 baker_read_barrier_patches_.emplace_back(custom_data);
8998 return &baker_read_barrier_patches_.back().label;
8999}
9000
Artem Serovc5fcb442016-12-02 19:19:58 +00009001VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateBootImageAddressLiteral(uint32_t address) {
Richard Uhlerc52f3032017-03-02 13:45:45 +00009002 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), &uint32_literals_);
Artem Serovc5fcb442016-12-02 19:19:58 +00009003}
9004
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00009005VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateJitStringLiteral(
9006 const DexFile& dex_file,
9007 dex::StringIndex string_index,
9008 Handle<mirror::String> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01009009 ReserveJitStringRoot(StringReference(&dex_file, string_index), handle);
Artem Serovc5fcb442016-12-02 19:19:58 +00009010 return jit_string_patches_.GetOrCreate(
9011 StringReference(&dex_file, string_index),
9012 [this]() {
9013 return GetAssembler()->CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u);
9014 });
9015}
9016
9017VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateJitClassLiteral(const DexFile& dex_file,
9018 dex::TypeIndex type_index,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00009019 Handle<mirror::Class> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01009020 ReserveJitClassRoot(TypeReference(&dex_file, type_index), handle);
Artem Serovc5fcb442016-12-02 19:19:58 +00009021 return jit_class_patches_.GetOrCreate(
9022 TypeReference(&dex_file, type_index),
9023 [this]() {
9024 return GetAssembler()->CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u);
9025 });
9026}
9027
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01009028template <linker::LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
Artem Serovd4cc5b22016-11-04 11:19:09 +00009029inline void CodeGeneratorARMVIXL::EmitPcRelativeLinkerPatches(
9030 const ArenaDeque<PcRelativePatchInfo>& infos,
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01009031 ArenaVector<linker::LinkerPatch>* linker_patches) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00009032 for (const PcRelativePatchInfo& info : infos) {
9033 const DexFile& dex_file = info.target_dex_file;
9034 size_t offset_or_index = info.offset_or_index;
9035 DCHECK(info.add_pc_label.IsBound());
9036 uint32_t add_pc_offset = dchecked_integral_cast<uint32_t>(info.add_pc_label.GetLocation());
9037 // Add MOVW patch.
9038 DCHECK(info.movw_label.IsBound());
9039 uint32_t movw_offset = dchecked_integral_cast<uint32_t>(info.movw_label.GetLocation());
9040 linker_patches->push_back(Factory(movw_offset, &dex_file, add_pc_offset, offset_or_index));
9041 // Add MOVT patch.
9042 DCHECK(info.movt_label.IsBound());
9043 uint32_t movt_offset = dchecked_integral_cast<uint32_t>(info.movt_label.GetLocation());
9044 linker_patches->push_back(Factory(movt_offset, &dex_file, add_pc_offset, offset_or_index));
9045 }
9046}
9047
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01009048void CodeGeneratorARMVIXL::EmitLinkerPatches(ArenaVector<linker::LinkerPatch>* linker_patches) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00009049 DCHECK(linker_patches->empty());
9050 size_t size =
Vladimir Marko65979462017-05-19 17:25:12 +01009051 /* MOVW+MOVT for each entry */ 2u * pc_relative_method_patches_.size() +
Vladimir Marko0eb882b2017-05-15 13:39:18 +01009052 /* MOVW+MOVT for each entry */ 2u * method_bss_entry_patches_.size() +
Artem Serovc5fcb442016-12-02 19:19:58 +00009053 /* MOVW+MOVT for each entry */ 2u * pc_relative_type_patches_.size() +
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01009054 /* MOVW+MOVT for each entry */ 2u * type_bss_entry_patches_.size() +
Vladimir Marko65979462017-05-19 17:25:12 +01009055 /* MOVW+MOVT for each entry */ 2u * pc_relative_string_patches_.size() +
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01009056 /* MOVW+MOVT for each entry */ 2u * string_bss_entry_patches_.size() +
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01009057 baker_read_barrier_patches_.size();
Artem Serovd4cc5b22016-11-04 11:19:09 +00009058 linker_patches->reserve(size);
Vladimir Marko65979462017-05-19 17:25:12 +01009059 if (GetCompilerOptions().IsBootImage()) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01009060 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeMethodPatch>(
9061 pc_relative_method_patches_, linker_patches);
9062 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeTypePatch>(
9063 pc_relative_type_patches_, linker_patches);
9064 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeStringPatch>(
9065 pc_relative_string_patches_, linker_patches);
Vladimir Marko65979462017-05-19 17:25:12 +01009066 } else {
9067 DCHECK(pc_relative_method_patches_.empty());
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01009068 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeClassTablePatch>(
9069 pc_relative_type_patches_, linker_patches);
9070 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringInternTablePatch>(
9071 pc_relative_string_patches_, linker_patches);
Artem Serovd4cc5b22016-11-04 11:19:09 +00009072 }
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01009073 EmitPcRelativeLinkerPatches<linker::LinkerPatch::MethodBssEntryPatch>(
9074 method_bss_entry_patches_, linker_patches);
9075 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeBssEntryPatch>(
9076 type_bss_entry_patches_, linker_patches);
9077 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringBssEntryPatch>(
9078 string_bss_entry_patches_, linker_patches);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01009079 for (const BakerReadBarrierPatchInfo& info : baker_read_barrier_patches_) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01009080 linker_patches->push_back(linker::LinkerPatch::BakerReadBarrierBranchPatch(
9081 info.label.GetLocation(), info.custom_data));
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01009082 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00009083 DCHECK_EQ(size, linker_patches->size());
Artem Serovc5fcb442016-12-02 19:19:58 +00009084}
9085
9086VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateUint32Literal(
9087 uint32_t value,
9088 Uint32ToLiteralMap* map) {
9089 return map->GetOrCreate(
9090 value,
9091 [this, value]() {
9092 return GetAssembler()->CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ value);
9093 });
9094}
9095
Artem Serov2bbc9532016-10-21 11:51:50 +01009096void LocationsBuilderARMVIXL::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
9097 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01009098 new (GetGraph()->GetAllocator()) LocationSummary(instr, LocationSummary::kNoCall);
Artem Serov2bbc9532016-10-21 11:51:50 +01009099 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
9100 Location::RequiresRegister());
9101 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
9102 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
9103 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
9104}
9105
9106void InstructionCodeGeneratorARMVIXL::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
9107 vixl32::Register res = OutputRegister(instr);
9108 vixl32::Register accumulator =
9109 InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
9110 vixl32::Register mul_left =
9111 InputRegisterAt(instr, HMultiplyAccumulate::kInputMulLeftIndex);
9112 vixl32::Register mul_right =
9113 InputRegisterAt(instr, HMultiplyAccumulate::kInputMulRightIndex);
9114
9115 if (instr->GetOpKind() == HInstruction::kAdd) {
9116 __ Mla(res, mul_left, mul_right, accumulator);
9117 } else {
9118 __ Mls(res, mul_left, mul_right, accumulator);
9119 }
9120}
9121
Artem Serov551b28f2016-10-18 19:11:30 +01009122void LocationsBuilderARMVIXL::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
9123 // Nothing to do, this should be removed during prepare for register allocator.
9124 LOG(FATAL) << "Unreachable";
9125}
9126
9127void InstructionCodeGeneratorARMVIXL::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
9128 // Nothing to do, this should be removed during prepare for register allocator.
9129 LOG(FATAL) << "Unreachable";
9130}
9131
9132// Simple implementation of packed switch - generate cascaded compare/jumps.
9133void LocationsBuilderARMVIXL::VisitPackedSwitch(HPackedSwitch* switch_instr) {
9134 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01009135 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Artem Serov551b28f2016-10-18 19:11:30 +01009136 locations->SetInAt(0, Location::RequiresRegister());
9137 if (switch_instr->GetNumEntries() > kPackedSwitchCompareJumpThreshold &&
9138 codegen_->GetAssembler()->GetVIXLAssembler()->IsUsingT32()) {
9139 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the table base.
9140 if (switch_instr->GetStartValue() != 0) {
9141 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the bias.
9142 }
9143 }
9144}
9145
9146// TODO(VIXL): Investigate and reach the parity with old arm codegen.
9147void InstructionCodeGeneratorARMVIXL::VisitPackedSwitch(HPackedSwitch* switch_instr) {
9148 int32_t lower_bound = switch_instr->GetStartValue();
9149 uint32_t num_entries = switch_instr->GetNumEntries();
9150 LocationSummary* locations = switch_instr->GetLocations();
9151 vixl32::Register value_reg = InputRegisterAt(switch_instr, 0);
9152 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
9153
9154 if (num_entries <= kPackedSwitchCompareJumpThreshold ||
9155 !codegen_->GetAssembler()->GetVIXLAssembler()->IsUsingT32()) {
9156 // Create a series of compare/jumps.
Anton Kirilovedb2ac32016-11-30 15:14:10 +00009157 UseScratchRegisterScope temps(GetVIXLAssembler());
Artem Serov551b28f2016-10-18 19:11:30 +01009158 vixl32::Register temp_reg = temps.Acquire();
9159 // Note: It is fine for the below AddConstantSetFlags() using IP register to temporarily store
9160 // the immediate, because IP is used as the destination register. For the other
9161 // AddConstantSetFlags() and GenerateCompareWithImmediate(), the immediate values are constant,
9162 // and they can be encoded in the instruction without making use of IP register.
9163 __ Adds(temp_reg, value_reg, -lower_bound);
9164
9165 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
9166 // Jump to successors[0] if value == lower_bound.
9167 __ B(eq, codegen_->GetLabelOf(successors[0]));
9168 int32_t last_index = 0;
9169 for (; num_entries - last_index > 2; last_index += 2) {
9170 __ Adds(temp_reg, temp_reg, -2);
9171 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
9172 __ B(lo, codegen_->GetLabelOf(successors[last_index + 1]));
9173 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
9174 __ B(eq, codegen_->GetLabelOf(successors[last_index + 2]));
9175 }
9176 if (num_entries - last_index == 2) {
9177 // The last missing case_value.
9178 __ Cmp(temp_reg, 1);
9179 __ B(eq, codegen_->GetLabelOf(successors[last_index + 1]));
9180 }
9181
9182 // And the default for any other value.
9183 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
9184 __ B(codegen_->GetLabelOf(default_block));
9185 }
9186 } else {
9187 // Create a table lookup.
9188 vixl32::Register table_base = RegisterFrom(locations->GetTemp(0));
9189
9190 JumpTableARMVIXL* jump_table = codegen_->CreateJumpTable(switch_instr);
9191
9192 // Remove the bias.
9193 vixl32::Register key_reg;
9194 if (lower_bound != 0) {
9195 key_reg = RegisterFrom(locations->GetTemp(1));
9196 __ Sub(key_reg, value_reg, lower_bound);
9197 } else {
9198 key_reg = value_reg;
9199 }
9200
9201 // Check whether the value is in the table, jump to default block if not.
9202 __ Cmp(key_reg, num_entries - 1);
9203 __ B(hi, codegen_->GetLabelOf(default_block));
9204
Anton Kirilovedb2ac32016-11-30 15:14:10 +00009205 UseScratchRegisterScope temps(GetVIXLAssembler());
Artem Serov551b28f2016-10-18 19:11:30 +01009206 vixl32::Register jump_offset = temps.Acquire();
9207
9208 // Load jump offset from the table.
Scott Wakeling86e9d262017-01-18 15:59:24 +00009209 {
9210 const size_t jump_size = switch_instr->GetNumEntries() * sizeof(int32_t);
9211 ExactAssemblyScope aas(GetVIXLAssembler(),
9212 (vixl32::kMaxInstructionSizeInBytes * 4) + jump_size,
9213 CodeBufferCheckScope::kMaximumSize);
9214 __ adr(table_base, jump_table->GetTableStartLabel());
9215 __ ldr(jump_offset, MemOperand(table_base, key_reg, vixl32::LSL, 2));
Artem Serov551b28f2016-10-18 19:11:30 +01009216
Scott Wakeling86e9d262017-01-18 15:59:24 +00009217 // Jump to target block by branching to table_base(pc related) + offset.
9218 vixl32::Register target_address = table_base;
9219 __ add(target_address, table_base, jump_offset);
9220 __ bx(target_address);
Artem Serov09a940d2016-11-11 16:15:11 +00009221
Scott Wakeling86e9d262017-01-18 15:59:24 +00009222 jump_table->EmitTable(codegen_);
9223 }
Artem Serov551b28f2016-10-18 19:11:30 +01009224 }
9225}
9226
Artem Serov02d37832016-10-25 15:25:33 +01009227// Copy the result of a call into the given target.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009228void CodeGeneratorARMVIXL::MoveFromReturnRegister(Location trg, DataType::Type type) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01009229 if (!trg.IsValid()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009230 DCHECK_EQ(type, DataType::Type::kVoid);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01009231 return;
9232 }
9233
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009234 DCHECK_NE(type, DataType::Type::kVoid);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01009235
Artem Serovd4cc5b22016-11-04 11:19:09 +00009236 Location return_loc = InvokeDexCallingConventionVisitorARMVIXL().GetReturnLocation(type);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01009237 if (return_loc.Equals(trg)) {
9238 return;
9239 }
9240
9241 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
9242 // with the last branch.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009243 if (type == DataType::Type::kInt64) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01009244 TODO_VIXL32(FATAL);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009245 } else if (type == DataType::Type::kFloat64) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01009246 TODO_VIXL32(FATAL);
9247 } else {
9248 // Let the parallel move resolver take care of all of this.
Vladimir Markoca6fff82017-10-03 14:49:14 +01009249 HParallelMove parallel_move(GetGraph()->GetAllocator());
Anton Kirilove28d9ae2016-10-25 18:17:23 +01009250 parallel_move.AddMove(return_loc, trg, type, nullptr);
9251 GetMoveResolver()->EmitNativeCode(&parallel_move);
9252 }
Scott Wakelingfe885462016-09-22 10:24:38 +01009253}
9254
xueliang.zhong8d2c4592016-11-23 17:05:25 +00009255void LocationsBuilderARMVIXL::VisitClassTableGet(HClassTableGet* instruction) {
9256 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01009257 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
xueliang.zhong8d2c4592016-11-23 17:05:25 +00009258 locations->SetInAt(0, Location::RequiresRegister());
9259 locations->SetOut(Location::RequiresRegister());
Artem Serov551b28f2016-10-18 19:11:30 +01009260}
9261
xueliang.zhong8d2c4592016-11-23 17:05:25 +00009262void InstructionCodeGeneratorARMVIXL::VisitClassTableGet(HClassTableGet* instruction) {
9263 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
9264 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
9265 instruction->GetIndex(), kArmPointerSize).SizeValue();
9266 GetAssembler()->LoadFromOffset(kLoadWord,
9267 OutputRegister(instruction),
9268 InputRegisterAt(instruction, 0),
9269 method_offset);
9270 } else {
9271 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
9272 instruction->GetIndex(), kArmPointerSize));
9273 GetAssembler()->LoadFromOffset(kLoadWord,
9274 OutputRegister(instruction),
9275 InputRegisterAt(instruction, 0),
9276 mirror::Class::ImtPtrOffset(kArmPointerSize).Uint32Value());
9277 GetAssembler()->LoadFromOffset(kLoadWord,
9278 OutputRegister(instruction),
9279 OutputRegister(instruction),
9280 method_offset);
9281 }
Artem Serov551b28f2016-10-18 19:11:30 +01009282}
9283
Artem Serovc5fcb442016-12-02 19:19:58 +00009284static void PatchJitRootUse(uint8_t* code,
9285 const uint8_t* roots_data,
9286 VIXLUInt32Literal* literal,
9287 uint64_t index_in_table) {
9288 DCHECK(literal->IsBound());
9289 uint32_t literal_offset = literal->GetLocation();
9290 uintptr_t address =
9291 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
9292 uint8_t* data = code + literal_offset;
9293 reinterpret_cast<uint32_t*>(data)[0] = dchecked_integral_cast<uint32_t>(address);
9294}
9295
9296void CodeGeneratorARMVIXL::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
9297 for (const auto& entry : jit_string_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01009298 const StringReference& string_reference = entry.first;
9299 VIXLUInt32Literal* table_entry_literal = entry.second;
Vladimir Marko174b2e22017-10-12 13:34:49 +01009300 uint64_t index_in_table = GetJitStringRootIndex(string_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01009301 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Artem Serovc5fcb442016-12-02 19:19:58 +00009302 }
9303 for (const auto& entry : jit_class_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01009304 const TypeReference& type_reference = entry.first;
9305 VIXLUInt32Literal* table_entry_literal = entry.second;
Vladimir Marko174b2e22017-10-12 13:34:49 +01009306 uint64_t index_in_table = GetJitClassRootIndex(type_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01009307 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Artem Serovc5fcb442016-12-02 19:19:58 +00009308 }
9309}
9310
Artem Serovd4cc5b22016-11-04 11:19:09 +00009311void CodeGeneratorARMVIXL::EmitMovwMovtPlaceholder(
9312 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels,
9313 vixl32::Register out) {
Artem Serov0fb37192016-12-06 18:13:40 +00009314 ExactAssemblyScope aas(GetVIXLAssembler(),
9315 3 * vixl32::kMaxInstructionSizeInBytes,
9316 CodeBufferCheckScope::kMaximumSize);
Artem Serovd4cc5b22016-11-04 11:19:09 +00009317 // TODO(VIXL): Think about using mov instead of movw.
9318 __ bind(&labels->movw_label);
9319 __ movw(out, /* placeholder */ 0u);
9320 __ bind(&labels->movt_label);
9321 __ movt(out, /* placeholder */ 0u);
9322 __ bind(&labels->add_pc_label);
9323 __ add(out, out, pc);
9324}
9325
Scott Wakelingfe885462016-09-22 10:24:38 +01009326#undef __
9327#undef QUICK_ENTRY_POINT
9328#undef TODO_VIXL32
9329
9330} // namespace arm
9331} // namespace art