blob: dad18134de45b9702f62e59e6ecbb51223955296 [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"
Vladimir Markoeebb8212018-06-05 14:57:24 +010030#include "gc/space/image_space.h"
Andreas Gampe09659c22017-09-18 18:23:32 -070031#include "heap_poisoning.h"
Vladimir Marko6fd16062018-06-26 11:02:04 +010032#include "intrinsics.h"
Anton Kirilov5ec62182016-10-13 20:16:02 +010033#include "intrinsics_arm_vixl.h"
Vladimir Markod8dbc8d2017-09-20 13:37:47 +010034#include "linker/linker_patch.h"
Scott Wakelingfe885462016-09-22 10:24:38 +010035#include "mirror/array-inl.h"
36#include "mirror/class-inl.h"
37#include "thread.h"
38#include "utils/arm/assembler_arm_vixl.h"
39#include "utils/arm/managed_register_arm.h"
40#include "utils/assembler.h"
41#include "utils/stack_checks.h"
42
43namespace art {
44namespace arm {
45
46namespace vixl32 = vixl::aarch32;
47using namespace vixl32; // NOLINT(build/namespaces)
48
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +010049using helpers::DRegisterFrom;
Scott Wakelingfe885462016-09-22 10:24:38 +010050using helpers::DWARFReg;
Scott Wakelinga7812ae2016-10-17 10:03:36 +010051using 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.
Vladimir Marko008e09f32018-08-06 15:42:43 +010088// For the Baker read barrier implementation using link-time generated thunks we need to split
Vladimir Markoeee1c0e2017-04-21 17:58:41 +010089// the offset explicitly.
90constexpr uint32_t kReferenceLoadMinFarOffset = 4 * KB;
91
Roland Levillain5daa4952017-07-03 17:23:56 +010092// Using a base helps identify when we hit Marking Register check breakpoints.
93constexpr int kMarkingRegisterCheckBreakCodeBaseCode = 0x10;
94
Scott Wakelingfe885462016-09-22 10:24:38 +010095#ifdef __
96#error "ARM Codegen VIXL macro-assembler macro already defined."
97#endif
98
Scott Wakelingfe885462016-09-22 10:24:38 +010099// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
100#define __ down_cast<CodeGeneratorARMVIXL*>(codegen)->GetVIXLAssembler()-> // NOLINT
101#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kArmPointerSize, x).Int32Value()
102
103// Marker that code is yet to be, and must, be implemented.
104#define TODO_VIXL32(level) LOG(level) << __PRETTY_FUNCTION__ << " unimplemented "
105
Vladimir Marko88abba22017-05-03 17:09:25 +0100106static inline bool CanEmitNarrowLdr(vixl32::Register rt, vixl32::Register rn, uint32_t offset) {
107 return rt.IsLow() && rn.IsLow() && offset < 32u;
108}
109
Vladimir Markoeee1c0e2017-04-21 17:58:41 +0100110class EmitAdrCode {
111 public:
112 EmitAdrCode(ArmVIXLMacroAssembler* assembler, vixl32::Register rd, vixl32::Label* label)
113 : assembler_(assembler), rd_(rd), label_(label) {
Vladimir Markod887ed82018-08-14 13:52:12 +0000114 DCHECK(!assembler->AllowMacroInstructions()); // In ExactAssemblyScope.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +0100115 adr_location_ = assembler->GetCursorOffset();
116 assembler->adr(EncodingSize(Wide), rd, label);
117 }
118
119 ~EmitAdrCode() {
120 DCHECK(label_->IsBound());
121 // The ADR emitted by the assembler does not set the Thumb mode bit we need.
122 // TODO: Maybe extend VIXL to allow ADR for return address?
123 uint8_t* raw_adr = assembler_->GetBuffer()->GetOffsetAddress<uint8_t*>(adr_location_);
124 // Expecting ADR encoding T3 with `(offset & 1) == 0`.
125 DCHECK_EQ(raw_adr[1] & 0xfbu, 0xf2u); // Check bits 24-31, except 26.
126 DCHECK_EQ(raw_adr[0] & 0xffu, 0x0fu); // Check bits 16-23.
127 DCHECK_EQ(raw_adr[3] & 0x8fu, rd_.GetCode()); // Check bits 8-11 and 15.
128 DCHECK_EQ(raw_adr[2] & 0x01u, 0x00u); // Check bit 0, i.e. the `offset & 1`.
129 // Add the Thumb mode bit.
130 raw_adr[2] |= 0x01u;
131 }
132
133 private:
134 ArmVIXLMacroAssembler* const assembler_;
135 vixl32::Register rd_;
136 vixl32::Label* const label_;
137 int32_t adr_location_;
138};
139
Vladimir Marko3232dbb2018-07-25 15:42:46 +0100140static RegisterSet OneRegInReferenceOutSaveEverythingCallerSaves() {
141 InvokeRuntimeCallingConventionARMVIXL calling_convention;
142 RegisterSet caller_saves = RegisterSet::Empty();
143 caller_saves.Add(LocationFrom(calling_convention.GetRegisterAt(0)));
144 // TODO: Add GetReturnLocation() to the calling convention so that we can DCHECK()
145 // that the the kPrimNot result register is the same as the first argument register.
146 return caller_saves;
147}
148
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100149// SaveLiveRegisters and RestoreLiveRegisters from SlowPathCodeARM operate on sets of S registers,
150// for each live D registers they treat two corresponding S registers as live ones.
151//
152// Two following functions (SaveContiguousSRegisterList, RestoreContiguousSRegisterList) build
153// from a list of contiguous S registers a list of contiguous D registers (processing first/last
154// S registers corner cases) and save/restore this new list treating them as D registers.
155// - decreasing code size
156// - avoiding hazards on Cortex-A57, when a pair of S registers for an actual live D register is
157// restored and then used in regular non SlowPath code as D register.
158//
159// For the following example (v means the S register is live):
160// D names: | D0 | D1 | D2 | D4 | ...
161// S names: | S0 | S1 | S2 | S3 | S4 | S5 | S6 | S7 | ...
162// Live? | | v | v | v | v | v | v | | ...
163//
164// S1 and S6 will be saved/restored independently; D registers list (D1, D2) will be processed
165// as D registers.
166//
167// TODO(VIXL): All this code should be unnecessary once the VIXL AArch32 backend provides helpers
168// for lists of floating-point registers.
169static size_t SaveContiguousSRegisterList(size_t first,
170 size_t last,
171 CodeGenerator* codegen,
172 size_t stack_offset) {
173 static_assert(kSRegSizeInBytes == kArmWordSize, "Broken assumption on reg/word sizes.");
174 static_assert(kDRegSizeInBytes == 2 * kArmWordSize, "Broken assumption on reg/word sizes.");
175 DCHECK_LE(first, last);
176 if ((first == last) && (first == 0)) {
177 __ Vstr(vixl32::SRegister(first), MemOperand(sp, stack_offset));
178 return stack_offset + kSRegSizeInBytes;
179 }
180 if (first % 2 == 1) {
181 __ Vstr(vixl32::SRegister(first++), MemOperand(sp, stack_offset));
182 stack_offset += kSRegSizeInBytes;
183 }
184
185 bool save_last = false;
186 if (last % 2 == 0) {
187 save_last = true;
188 --last;
189 }
190
191 if (first < last) {
192 vixl32::DRegister d_reg = vixl32::DRegister(first / 2);
193 DCHECK_EQ((last - first + 1) % 2, 0u);
194 size_t number_of_d_regs = (last - first + 1) / 2;
195
196 if (number_of_d_regs == 1) {
197 __ Vstr(d_reg, MemOperand(sp, stack_offset));
198 } else if (number_of_d_regs > 1) {
199 UseScratchRegisterScope temps(down_cast<CodeGeneratorARMVIXL*>(codegen)->GetVIXLAssembler());
200 vixl32::Register base = sp;
201 if (stack_offset != 0) {
202 base = temps.Acquire();
Scott Wakelingb77051e2016-11-21 19:46:00 +0000203 __ Add(base, sp, Operand::From(stack_offset));
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100204 }
205 __ Vstm(F64, base, NO_WRITE_BACK, DRegisterList(d_reg, number_of_d_regs));
206 }
207 stack_offset += number_of_d_regs * kDRegSizeInBytes;
208 }
209
210 if (save_last) {
211 __ Vstr(vixl32::SRegister(last + 1), MemOperand(sp, stack_offset));
212 stack_offset += kSRegSizeInBytes;
213 }
214
215 return stack_offset;
216}
217
218static size_t RestoreContiguousSRegisterList(size_t first,
219 size_t last,
220 CodeGenerator* codegen,
221 size_t stack_offset) {
222 static_assert(kSRegSizeInBytes == kArmWordSize, "Broken assumption on reg/word sizes.");
223 static_assert(kDRegSizeInBytes == 2 * kArmWordSize, "Broken assumption on reg/word sizes.");
224 DCHECK_LE(first, last);
225 if ((first == last) && (first == 0)) {
226 __ Vldr(vixl32::SRegister(first), MemOperand(sp, stack_offset));
227 return stack_offset + kSRegSizeInBytes;
228 }
229 if (first % 2 == 1) {
230 __ Vldr(vixl32::SRegister(first++), MemOperand(sp, stack_offset));
231 stack_offset += kSRegSizeInBytes;
232 }
233
234 bool restore_last = false;
235 if (last % 2 == 0) {
236 restore_last = true;
237 --last;
238 }
239
240 if (first < last) {
241 vixl32::DRegister d_reg = vixl32::DRegister(first / 2);
242 DCHECK_EQ((last - first + 1) % 2, 0u);
243 size_t number_of_d_regs = (last - first + 1) / 2;
244 if (number_of_d_regs == 1) {
245 __ Vldr(d_reg, MemOperand(sp, stack_offset));
246 } else if (number_of_d_regs > 1) {
247 UseScratchRegisterScope temps(down_cast<CodeGeneratorARMVIXL*>(codegen)->GetVIXLAssembler());
248 vixl32::Register base = sp;
249 if (stack_offset != 0) {
250 base = temps.Acquire();
Scott Wakelingb77051e2016-11-21 19:46:00 +0000251 __ Add(base, sp, Operand::From(stack_offset));
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100252 }
253 __ Vldm(F64, base, NO_WRITE_BACK, DRegisterList(d_reg, number_of_d_regs));
254 }
255 stack_offset += number_of_d_regs * kDRegSizeInBytes;
256 }
257
258 if (restore_last) {
259 __ Vldr(vixl32::SRegister(last + 1), MemOperand(sp, stack_offset));
260 stack_offset += kSRegSizeInBytes;
261 }
262
263 return stack_offset;
264}
265
Vladimir Markod5d2f2c2017-09-26 12:37:26 +0100266static LoadOperandType GetLoadOperandType(DataType::Type type) {
267 switch (type) {
268 case DataType::Type::kReference:
269 return kLoadWord;
270 case DataType::Type::kBool:
271 case DataType::Type::kUint8:
272 return kLoadUnsignedByte;
273 case DataType::Type::kInt8:
274 return kLoadSignedByte;
275 case DataType::Type::kUint16:
276 return kLoadUnsignedHalfword;
277 case DataType::Type::kInt16:
278 return kLoadSignedHalfword;
279 case DataType::Type::kInt32:
280 return kLoadWord;
281 case DataType::Type::kInt64:
282 return kLoadWordPair;
283 case DataType::Type::kFloat32:
284 return kLoadSWord;
285 case DataType::Type::kFloat64:
286 return kLoadDWord;
287 default:
288 LOG(FATAL) << "Unreachable type " << type;
289 UNREACHABLE();
290 }
291}
292
293static StoreOperandType GetStoreOperandType(DataType::Type type) {
294 switch (type) {
295 case DataType::Type::kReference:
296 return kStoreWord;
297 case DataType::Type::kBool:
298 case DataType::Type::kUint8:
299 case DataType::Type::kInt8:
300 return kStoreByte;
301 case DataType::Type::kUint16:
302 case DataType::Type::kInt16:
303 return kStoreHalfword;
304 case DataType::Type::kInt32:
305 return kStoreWord;
306 case DataType::Type::kInt64:
307 return kStoreWordPair;
308 case DataType::Type::kFloat32:
309 return kStoreSWord;
310 case DataType::Type::kFloat64:
311 return kStoreDWord;
312 default:
313 LOG(FATAL) << "Unreachable type " << type;
314 UNREACHABLE();
315 }
316}
317
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100318void SlowPathCodeARMVIXL::SaveLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
319 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
320 size_t orig_offset = stack_offset;
321
322 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ true);
323 for (uint32_t i : LowToHighBits(core_spills)) {
324 // If the register holds an object, update the stack mask.
325 if (locations->RegisterContainsObject(i)) {
326 locations->SetStackBit(stack_offset / kVRegSize);
327 }
328 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
329 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
330 saved_core_stack_offsets_[i] = stack_offset;
331 stack_offset += kArmWordSize;
332 }
333
334 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
335 arm_codegen->GetAssembler()->StoreRegisterList(core_spills, orig_offset);
336
337 uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ false);
338 orig_offset = stack_offset;
339 for (uint32_t i : LowToHighBits(fp_spills)) {
340 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
341 saved_fpu_stack_offsets_[i] = stack_offset;
342 stack_offset += kArmWordSize;
343 }
344
345 stack_offset = orig_offset;
346 while (fp_spills != 0u) {
347 uint32_t begin = CTZ(fp_spills);
348 uint32_t tmp = fp_spills + (1u << begin);
349 fp_spills &= tmp; // Clear the contiguous range of 1s.
350 uint32_t end = (tmp == 0u) ? 32u : CTZ(tmp); // CTZ(0) is undefined.
351 stack_offset = SaveContiguousSRegisterList(begin, end - 1, codegen, stack_offset);
352 }
353 DCHECK_LE(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
354}
355
356void SlowPathCodeARMVIXL::RestoreLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
357 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
358 size_t orig_offset = stack_offset;
359
360 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ true);
361 for (uint32_t i : LowToHighBits(core_spills)) {
362 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
363 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
364 stack_offset += kArmWordSize;
365 }
366
367 // TODO(VIXL): Check the coherency of stack_offset after this with a test.
368 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
369 arm_codegen->GetAssembler()->LoadRegisterList(core_spills, orig_offset);
370
371 uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ false);
372 while (fp_spills != 0u) {
373 uint32_t begin = CTZ(fp_spills);
374 uint32_t tmp = fp_spills + (1u << begin);
375 fp_spills &= tmp; // Clear the contiguous range of 1s.
376 uint32_t end = (tmp == 0u) ? 32u : CTZ(tmp); // CTZ(0) is undefined.
377 stack_offset = RestoreContiguousSRegisterList(begin, end - 1, codegen, stack_offset);
378 }
379 DCHECK_LE(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
380}
381
382class NullCheckSlowPathARMVIXL : public SlowPathCodeARMVIXL {
383 public:
384 explicit NullCheckSlowPathARMVIXL(HNullCheck* instruction) : SlowPathCodeARMVIXL(instruction) {}
385
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100386 void EmitNativeCode(CodeGenerator* codegen) override {
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100387 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
388 __ Bind(GetEntryLabel());
389 if (instruction_->CanThrowIntoCatchBlock()) {
390 // Live registers will be restored in the catch block if caught.
391 SaveLiveRegisters(codegen, instruction_->GetLocations());
392 }
393 arm_codegen->InvokeRuntime(kQuickThrowNullPointer,
394 instruction_,
395 instruction_->GetDexPc(),
396 this);
397 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
398 }
399
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100400 bool IsFatal() const override { return true; }
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100401
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100402 const char* GetDescription() const override { return "NullCheckSlowPathARMVIXL"; }
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100403
404 private:
405 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARMVIXL);
406};
407
Scott Wakelingfe885462016-09-22 10:24:38 +0100408class DivZeroCheckSlowPathARMVIXL : public SlowPathCodeARMVIXL {
409 public:
410 explicit DivZeroCheckSlowPathARMVIXL(HDivZeroCheck* instruction)
411 : SlowPathCodeARMVIXL(instruction) {}
412
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100413 void EmitNativeCode(CodeGenerator* codegen) override {
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100414 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
Scott Wakelingfe885462016-09-22 10:24:38 +0100415 __ Bind(GetEntryLabel());
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100416 arm_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Scott Wakelingfe885462016-09-22 10:24:38 +0100417 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
418 }
419
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100420 bool IsFatal() const override { return true; }
Scott Wakelingfe885462016-09-22 10:24:38 +0100421
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100422 const char* GetDescription() const override { return "DivZeroCheckSlowPathARMVIXL"; }
Scott Wakelingfe885462016-09-22 10:24:38 +0100423
424 private:
425 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARMVIXL);
426};
427
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100428class SuspendCheckSlowPathARMVIXL : public SlowPathCodeARMVIXL {
429 public:
430 SuspendCheckSlowPathARMVIXL(HSuspendCheck* instruction, HBasicBlock* successor)
431 : SlowPathCodeARMVIXL(instruction), successor_(successor) {}
432
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100433 void EmitNativeCode(CodeGenerator* codegen) override {
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100434 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
435 __ Bind(GetEntryLabel());
436 arm_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
437 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
438 if (successor_ == nullptr) {
439 __ B(GetReturnLabel());
440 } else {
441 __ B(arm_codegen->GetLabelOf(successor_));
442 }
443 }
444
445 vixl32::Label* GetReturnLabel() {
446 DCHECK(successor_ == nullptr);
447 return &return_label_;
448 }
449
450 HBasicBlock* GetSuccessor() const {
451 return successor_;
452 }
453
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100454 const char* GetDescription() const override { return "SuspendCheckSlowPathARMVIXL"; }
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100455
456 private:
457 // If not null, the block to branch to after the suspend check.
458 HBasicBlock* const successor_;
459
460 // If `successor_` is null, the label to branch to after the suspend check.
461 vixl32::Label return_label_;
462
463 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARMVIXL);
464};
465
Scott Wakelingc34dba72016-10-03 10:14:44 +0100466class BoundsCheckSlowPathARMVIXL : public SlowPathCodeARMVIXL {
467 public:
468 explicit BoundsCheckSlowPathARMVIXL(HBoundsCheck* instruction)
469 : SlowPathCodeARMVIXL(instruction) {}
470
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100471 void EmitNativeCode(CodeGenerator* codegen) override {
Scott Wakelingc34dba72016-10-03 10:14:44 +0100472 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
473 LocationSummary* locations = instruction_->GetLocations();
474
475 __ Bind(GetEntryLabel());
476 if (instruction_->CanThrowIntoCatchBlock()) {
477 // Live registers will be restored in the catch block if caught.
478 SaveLiveRegisters(codegen, instruction_->GetLocations());
479 }
480 // We're moving two locations to locations that could overlap, so we need a parallel
481 // move resolver.
482 InvokeRuntimeCallingConventionARMVIXL calling_convention;
483 codegen->EmitParallelMoves(
484 locations->InAt(0),
485 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100486 DataType::Type::kInt32,
Scott Wakelingc34dba72016-10-03 10:14:44 +0100487 locations->InAt(1),
488 LocationFrom(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100489 DataType::Type::kInt32);
Scott Wakelingc34dba72016-10-03 10:14:44 +0100490 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
491 ? kQuickThrowStringBounds
492 : kQuickThrowArrayBounds;
493 arm_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
494 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
495 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
496 }
497
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100498 bool IsFatal() const override { return true; }
Scott Wakelingc34dba72016-10-03 10:14:44 +0100499
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100500 const char* GetDescription() const override { return "BoundsCheckSlowPathARMVIXL"; }
Scott Wakelingc34dba72016-10-03 10:14:44 +0100501
502 private:
503 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARMVIXL);
504};
505
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100506class LoadClassSlowPathARMVIXL : public SlowPathCodeARMVIXL {
507 public:
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100508 LoadClassSlowPathARMVIXL(HLoadClass* cls, HInstruction* at)
509 : SlowPathCodeARMVIXL(at), cls_(cls) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100510 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100511 DCHECK_EQ(instruction_->IsLoadClass(), cls_ == instruction_);
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100512 }
513
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100514 void EmitNativeCode(CodeGenerator* codegen) override {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000515 LocationSummary* locations = instruction_->GetLocations();
Vladimir Markoea4c1262017-02-06 19:59:33 +0000516 Location out = locations->Out();
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100517 const uint32_t dex_pc = instruction_->GetDexPc();
518 bool must_resolve_type = instruction_->IsLoadClass() && cls_->MustResolveTypeOnSlowPath();
519 bool must_do_clinit = instruction_->IsClinitCheck() || cls_->MustGenerateClinitCheck();
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100520
521 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
522 __ Bind(GetEntryLabel());
523 SaveLiveRegisters(codegen, locations);
524
525 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100526 if (must_resolve_type) {
527 DCHECK(IsSameDexFile(cls_->GetDexFile(), arm_codegen->GetGraph()->GetDexFile()));
528 dex::TypeIndex type_index = cls_->GetTypeIndex();
529 __ Mov(calling_convention.GetRegisterAt(0), type_index.index_);
Vladimir Marko9d479252018-07-24 11:35:20 +0100530 arm_codegen->InvokeRuntime(kQuickResolveType, instruction_, dex_pc, this);
531 CheckEntrypointTypes<kQuickResolveType, void*, uint32_t>();
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100532 // If we also must_do_clinit, the resolved type is now in the correct register.
533 } else {
534 DCHECK(must_do_clinit);
535 Location source = instruction_->IsLoadClass() ? out : locations->InAt(0);
536 arm_codegen->Move32(LocationFrom(calling_convention.GetRegisterAt(0)), source);
537 }
538 if (must_do_clinit) {
539 arm_codegen->InvokeRuntime(kQuickInitializeStaticStorage, instruction_, dex_pc, this);
540 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, mirror::Class*>();
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100541 }
542
543 // Move the class to the desired location.
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100544 if (out.IsValid()) {
545 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
546 arm_codegen->Move32(locations->Out(), LocationFrom(r0));
547 }
548 RestoreLiveRegisters(codegen, locations);
549 __ B(GetExitLabel());
550 }
551
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100552 const char* GetDescription() const override { return "LoadClassSlowPathARMVIXL"; }
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100553
554 private:
555 // The class this slow path will load.
556 HLoadClass* const cls_;
557
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100558 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARMVIXL);
559};
560
Artem Serovd4cc5b22016-11-04 11:19:09 +0000561class LoadStringSlowPathARMVIXL : public SlowPathCodeARMVIXL {
562 public:
563 explicit LoadStringSlowPathARMVIXL(HLoadString* instruction)
564 : SlowPathCodeARMVIXL(instruction) {}
565
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100566 void EmitNativeCode(CodeGenerator* codegen) override {
Vladimir Markoea4c1262017-02-06 19:59:33 +0000567 DCHECK(instruction_->IsLoadString());
568 DCHECK_EQ(instruction_->AsLoadString()->GetLoadKind(), HLoadString::LoadKind::kBssEntry);
Artem Serovd4cc5b22016-11-04 11:19:09 +0000569 LocationSummary* locations = instruction_->GetLocations();
570 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Vladimir Markof3c52b42017-11-17 17:32:12 +0000571 const dex::StringIndex string_index = instruction_->AsLoadString()->GetStringIndex();
Artem Serovd4cc5b22016-11-04 11:19:09 +0000572
573 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
574 __ Bind(GetEntryLabel());
575 SaveLiveRegisters(codegen, locations);
576
577 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000578 __ Mov(calling_convention.GetRegisterAt(0), string_index.index_);
Artem Serovd4cc5b22016-11-04 11:19:09 +0000579 arm_codegen->InvokeRuntime(kQuickResolveString, instruction_, instruction_->GetDexPc(), this);
580 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
581
Artem Serovd4cc5b22016-11-04 11:19:09 +0000582 arm_codegen->Move32(locations->Out(), LocationFrom(r0));
583 RestoreLiveRegisters(codegen, locations);
584
585 __ B(GetExitLabel());
586 }
587
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100588 const char* GetDescription() const override { return "LoadStringSlowPathARMVIXL"; }
Artem Serovd4cc5b22016-11-04 11:19:09 +0000589
590 private:
591 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARMVIXL);
592};
593
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100594class TypeCheckSlowPathARMVIXL : public SlowPathCodeARMVIXL {
595 public:
596 TypeCheckSlowPathARMVIXL(HInstruction* instruction, bool is_fatal)
597 : SlowPathCodeARMVIXL(instruction), is_fatal_(is_fatal) {}
598
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100599 void EmitNativeCode(CodeGenerator* codegen) override {
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100600 LocationSummary* locations = instruction_->GetLocations();
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100601 DCHECK(instruction_->IsCheckCast()
602 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
603
604 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
605 __ Bind(GetEntryLabel());
606
Vladimir Marko87584542017-12-12 17:47:52 +0000607 if (!is_fatal_ || instruction_->CanThrowIntoCatchBlock()) {
Artem Serovcfbe9132016-10-14 15:58:56 +0100608 SaveLiveRegisters(codegen, locations);
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100609 }
610
611 // We're moving two locations to locations that could overlap, so we need a parallel
612 // move resolver.
613 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100614
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800615 codegen->EmitParallelMoves(locations->InAt(0),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800616 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100617 DataType::Type::kReference,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800618 locations->InAt(1),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800619 LocationFrom(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100620 DataType::Type::kReference);
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100621 if (instruction_->IsInstanceOf()) {
Artem Serovcfbe9132016-10-14 15:58:56 +0100622 arm_codegen->InvokeRuntime(kQuickInstanceofNonTrivial,
623 instruction_,
624 instruction_->GetDexPc(),
625 this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800626 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Artem Serovcfbe9132016-10-14 15:58:56 +0100627 arm_codegen->Move32(locations->Out(), LocationFrom(r0));
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100628 } else {
629 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800630 arm_codegen->InvokeRuntime(kQuickCheckInstanceOf,
631 instruction_,
632 instruction_->GetDexPc(),
633 this);
634 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100635 }
636
637 if (!is_fatal_) {
Artem Serovcfbe9132016-10-14 15:58:56 +0100638 RestoreLiveRegisters(codegen, locations);
639 __ B(GetExitLabel());
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100640 }
641 }
642
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100643 const char* GetDescription() const override { return "TypeCheckSlowPathARMVIXL"; }
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100644
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100645 bool IsFatal() const override { return is_fatal_; }
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100646
647 private:
648 const bool is_fatal_;
649
650 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARMVIXL);
651};
652
Scott Wakelingc34dba72016-10-03 10:14:44 +0100653class DeoptimizationSlowPathARMVIXL : public SlowPathCodeARMVIXL {
654 public:
655 explicit DeoptimizationSlowPathARMVIXL(HDeoptimize* instruction)
656 : SlowPathCodeARMVIXL(instruction) {}
657
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100658 void EmitNativeCode(CodeGenerator* codegen) override {
Scott Wakelingc34dba72016-10-03 10:14:44 +0100659 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
660 __ Bind(GetEntryLabel());
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100661 LocationSummary* locations = instruction_->GetLocations();
662 SaveLiveRegisters(codegen, locations);
663 InvokeRuntimeCallingConventionARMVIXL calling_convention;
664 __ Mov(calling_convention.GetRegisterAt(0),
665 static_cast<uint32_t>(instruction_->AsDeoptimize()->GetDeoptimizationKind()));
666
Scott Wakelingc34dba72016-10-03 10:14:44 +0100667 arm_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100668 CheckEntrypointTypes<kQuickDeoptimize, void, DeoptimizationKind>();
Scott Wakelingc34dba72016-10-03 10:14:44 +0100669 }
670
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100671 const char* GetDescription() const override { return "DeoptimizationSlowPathARMVIXL"; }
Scott Wakelingc34dba72016-10-03 10:14:44 +0100672
673 private:
674 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARMVIXL);
675};
676
677class ArraySetSlowPathARMVIXL : public SlowPathCodeARMVIXL {
678 public:
679 explicit ArraySetSlowPathARMVIXL(HInstruction* instruction) : SlowPathCodeARMVIXL(instruction) {}
680
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100681 void EmitNativeCode(CodeGenerator* codegen) override {
Scott Wakelingc34dba72016-10-03 10:14:44 +0100682 LocationSummary* locations = instruction_->GetLocations();
683 __ Bind(GetEntryLabel());
684 SaveLiveRegisters(codegen, locations);
685
686 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100687 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Scott Wakelingc34dba72016-10-03 10:14:44 +0100688 parallel_move.AddMove(
689 locations->InAt(0),
690 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100691 DataType::Type::kReference,
Scott Wakelingc34dba72016-10-03 10:14:44 +0100692 nullptr);
693 parallel_move.AddMove(
694 locations->InAt(1),
695 LocationFrom(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100696 DataType::Type::kInt32,
Scott Wakelingc34dba72016-10-03 10:14:44 +0100697 nullptr);
698 parallel_move.AddMove(
699 locations->InAt(2),
700 LocationFrom(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100701 DataType::Type::kReference,
Scott Wakelingc34dba72016-10-03 10:14:44 +0100702 nullptr);
703 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
704
705 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
706 arm_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
707 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
708 RestoreLiveRegisters(codegen, locations);
709 __ B(GetExitLabel());
710 }
711
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100712 const char* GetDescription() const override { return "ArraySetSlowPathARMVIXL"; }
Scott Wakelingc34dba72016-10-03 10:14:44 +0100713
714 private:
715 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathARMVIXL);
716};
717
Anton Kirilovedb2ac32016-11-30 15:14:10 +0000718// Slow path generating a read barrier for a heap reference.
719class ReadBarrierForHeapReferenceSlowPathARMVIXL : public SlowPathCodeARMVIXL {
720 public:
721 ReadBarrierForHeapReferenceSlowPathARMVIXL(HInstruction* instruction,
722 Location out,
723 Location ref,
724 Location obj,
725 uint32_t offset,
726 Location index)
727 : SlowPathCodeARMVIXL(instruction),
728 out_(out),
729 ref_(ref),
730 obj_(obj),
731 offset_(offset),
732 index_(index) {
733 DCHECK(kEmitCompilerReadBarrier);
734 // If `obj` is equal to `out` or `ref`, it means the initial object
735 // has been overwritten by (or after) the heap object reference load
736 // to be instrumented, e.g.:
737 //
738 // __ LoadFromOffset(kLoadWord, out, out, offset);
739 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
740 //
741 // In that case, we have lost the information about the original
742 // object, and the emitted read barrier cannot work properly.
743 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
744 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
745 }
746
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100747 void EmitNativeCode(CodeGenerator* codegen) override {
Anton Kirilovedb2ac32016-11-30 15:14:10 +0000748 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
749 LocationSummary* locations = instruction_->GetLocations();
750 vixl32::Register reg_out = RegisterFrom(out_);
751 DCHECK(locations->CanCall());
752 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out.GetCode()));
753 DCHECK(instruction_->IsInstanceFieldGet() ||
754 instruction_->IsStaticFieldGet() ||
755 instruction_->IsArrayGet() ||
756 instruction_->IsInstanceOf() ||
757 instruction_->IsCheckCast() ||
Andreas Gamped9911ee2017-03-27 13:27:24 -0700758 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
Anton Kirilovedb2ac32016-11-30 15:14:10 +0000759 << "Unexpected instruction in read barrier for heap reference slow path: "
760 << instruction_->DebugName();
761 // The read barrier instrumentation of object ArrayGet
762 // instructions does not support the HIntermediateAddress
763 // instruction.
764 DCHECK(!(instruction_->IsArrayGet() &&
765 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
766
767 __ Bind(GetEntryLabel());
768 SaveLiveRegisters(codegen, locations);
769
770 // We may have to change the index's value, but as `index_` is a
771 // constant member (like other "inputs" of this slow path),
772 // introduce a copy of it, `index`.
773 Location index = index_;
774 if (index_.IsValid()) {
775 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
776 if (instruction_->IsArrayGet()) {
777 // Compute the actual memory offset and store it in `index`.
778 vixl32::Register index_reg = RegisterFrom(index_);
779 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg.GetCode()));
780 if (codegen->IsCoreCalleeSaveRegister(index_reg.GetCode())) {
781 // We are about to change the value of `index_reg` (see the
Roland Levillain9983e302017-07-14 14:34:22 +0100782 // calls to art::arm::ArmVIXLMacroAssembler::Lsl and
783 // art::arm::ArmVIXLMacroAssembler::Add below), but it has
Anton Kirilovedb2ac32016-11-30 15:14:10 +0000784 // not been saved by the previous call to
785 // art::SlowPathCode::SaveLiveRegisters, as it is a
786 // callee-save register --
787 // art::SlowPathCode::SaveLiveRegisters does not consider
788 // callee-save registers, as it has been designed with the
789 // assumption that callee-save registers are supposed to be
790 // handled by the called function. So, as a callee-save
791 // register, `index_reg` _would_ eventually be saved onto
792 // the stack, but it would be too late: we would have
793 // changed its value earlier. Therefore, we manually save
794 // it here into another freely available register,
795 // `free_reg`, chosen of course among the caller-save
796 // registers (as a callee-save `free_reg` register would
797 // exhibit the same problem).
798 //
799 // Note we could have requested a temporary register from
800 // the register allocator instead; but we prefer not to, as
801 // this is a slow path, and we know we can find a
802 // caller-save register that is available.
803 vixl32::Register free_reg = FindAvailableCallerSaveRegister(codegen);
804 __ Mov(free_reg, index_reg);
805 index_reg = free_reg;
806 index = LocationFrom(index_reg);
807 } else {
808 // The initial register stored in `index_` has already been
809 // saved in the call to art::SlowPathCode::SaveLiveRegisters
810 // (as it is not a callee-save register), so we can freely
811 // use it.
812 }
813 // Shifting the index value contained in `index_reg` by the scale
814 // factor (2) cannot overflow in practice, as the runtime is
815 // unable to allocate object arrays with a size larger than
816 // 2^26 - 1 (that is, 2^28 - 4 bytes).
817 __ Lsl(index_reg, index_reg, TIMES_4);
818 static_assert(
819 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
820 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
821 __ Add(index_reg, index_reg, offset_);
822 } else {
823 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
824 // intrinsics, `index_` is not shifted by a scale factor of 2
825 // (as in the case of ArrayGet), as it is actually an offset
826 // to an object field within an object.
827 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
828 DCHECK(instruction_->GetLocations()->Intrinsified());
829 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
830 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
831 << instruction_->AsInvoke()->GetIntrinsic();
832 DCHECK_EQ(offset_, 0U);
833 DCHECK(index_.IsRegisterPair());
834 // UnsafeGet's offset location is a register pair, the low
835 // part contains the correct offset.
836 index = index_.ToLow();
837 }
838 }
839
840 // We're moving two or three locations to locations that could
841 // overlap, so we need a parallel move resolver.
842 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100843 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Anton Kirilovedb2ac32016-11-30 15:14:10 +0000844 parallel_move.AddMove(ref_,
845 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100846 DataType::Type::kReference,
Anton Kirilovedb2ac32016-11-30 15:14:10 +0000847 nullptr);
848 parallel_move.AddMove(obj_,
849 LocationFrom(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100850 DataType::Type::kReference,
Anton Kirilovedb2ac32016-11-30 15:14:10 +0000851 nullptr);
852 if (index.IsValid()) {
853 parallel_move.AddMove(index,
854 LocationFrom(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100855 DataType::Type::kInt32,
Anton Kirilovedb2ac32016-11-30 15:14:10 +0000856 nullptr);
857 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
858 } else {
859 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
860 __ Mov(calling_convention.GetRegisterAt(2), offset_);
861 }
862 arm_codegen->InvokeRuntime(kQuickReadBarrierSlow, instruction_, instruction_->GetDexPc(), this);
863 CheckEntrypointTypes<
864 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
865 arm_codegen->Move32(out_, LocationFrom(r0));
866
867 RestoreLiveRegisters(codegen, locations);
868 __ B(GetExitLabel());
869 }
870
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100871 const char* GetDescription() const override {
Anton Kirilovedb2ac32016-11-30 15:14:10 +0000872 return "ReadBarrierForHeapReferenceSlowPathARMVIXL";
873 }
874
875 private:
876 vixl32::Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
877 uint32_t ref = RegisterFrom(ref_).GetCode();
878 uint32_t obj = RegisterFrom(obj_).GetCode();
879 for (uint32_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
880 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
881 return vixl32::Register(i);
882 }
883 }
884 // We shall never fail to find a free caller-save register, as
885 // there are more than two core caller-save registers on ARM
886 // (meaning it is possible to find one which is different from
887 // `ref` and `obj`).
888 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
889 LOG(FATAL) << "Could not find a free caller-save register";
890 UNREACHABLE();
891 }
892
893 const Location out_;
894 const Location ref_;
895 const Location obj_;
896 const uint32_t offset_;
897 // An additional location containing an index to an array.
898 // Only used for HArrayGet and the UnsafeGetObject &
899 // UnsafeGetObjectVolatile intrinsics.
900 const Location index_;
901
902 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathARMVIXL);
903};
904
905// Slow path generating a read barrier for a GC root.
906class ReadBarrierForRootSlowPathARMVIXL : public SlowPathCodeARMVIXL {
907 public:
908 ReadBarrierForRootSlowPathARMVIXL(HInstruction* instruction, Location out, Location root)
909 : SlowPathCodeARMVIXL(instruction), out_(out), root_(root) {
910 DCHECK(kEmitCompilerReadBarrier);
911 }
912
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100913 void EmitNativeCode(CodeGenerator* codegen) override {
Anton Kirilovedb2ac32016-11-30 15:14:10 +0000914 LocationSummary* locations = instruction_->GetLocations();
915 vixl32::Register reg_out = RegisterFrom(out_);
916 DCHECK(locations->CanCall());
917 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out.GetCode()));
918 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
919 << "Unexpected instruction in read barrier for GC root slow path: "
920 << instruction_->DebugName();
921
922 __ Bind(GetEntryLabel());
923 SaveLiveRegisters(codegen, locations);
924
925 InvokeRuntimeCallingConventionARMVIXL calling_convention;
926 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
927 arm_codegen->Move32(LocationFrom(calling_convention.GetRegisterAt(0)), root_);
928 arm_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
929 instruction_,
930 instruction_->GetDexPc(),
931 this);
932 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
933 arm_codegen->Move32(out_, LocationFrom(r0));
934
935 RestoreLiveRegisters(codegen, locations);
936 __ B(GetExitLabel());
937 }
938
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100939 const char* GetDescription() const override { return "ReadBarrierForRootSlowPathARMVIXL"; }
Anton Kirilovedb2ac32016-11-30 15:14:10 +0000940
941 private:
942 const Location out_;
943 const Location root_;
944
945 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathARMVIXL);
946};
Scott Wakelingc34dba72016-10-03 10:14:44 +0100947
Scott Wakelingfe885462016-09-22 10:24:38 +0100948inline vixl32::Condition ARMCondition(IfCondition cond) {
949 switch (cond) {
950 case kCondEQ: return eq;
951 case kCondNE: return ne;
952 case kCondLT: return lt;
953 case kCondLE: return le;
954 case kCondGT: return gt;
955 case kCondGE: return ge;
956 case kCondB: return lo;
957 case kCondBE: return ls;
958 case kCondA: return hi;
959 case kCondAE: return hs;
960 }
961 LOG(FATAL) << "Unreachable";
962 UNREACHABLE();
963}
964
965// Maps signed condition to unsigned condition.
966inline vixl32::Condition ARMUnsignedCondition(IfCondition cond) {
967 switch (cond) {
968 case kCondEQ: return eq;
969 case kCondNE: return ne;
970 // Signed to unsigned.
971 case kCondLT: return lo;
972 case kCondLE: return ls;
973 case kCondGT: return hi;
974 case kCondGE: return hs;
975 // Unsigned remain unchanged.
976 case kCondB: return lo;
977 case kCondBE: return ls;
978 case kCondA: return hi;
979 case kCondAE: return hs;
980 }
981 LOG(FATAL) << "Unreachable";
982 UNREACHABLE();
983}
984
985inline vixl32::Condition ARMFPCondition(IfCondition cond, bool gt_bias) {
986 // The ARM condition codes can express all the necessary branches, see the
987 // "Meaning (floating-point)" column in the table A8-1 of the ARMv7 reference manual.
988 // There is no dex instruction or HIR that would need the missing conditions
989 // "equal or unordered" or "not equal".
990 switch (cond) {
991 case kCondEQ: return eq;
992 case kCondNE: return ne /* unordered */;
993 case kCondLT: return gt_bias ? cc : lt /* unordered */;
994 case kCondLE: return gt_bias ? ls : le /* unordered */;
995 case kCondGT: return gt_bias ? hi /* unordered */ : gt;
996 case kCondGE: return gt_bias ? cs /* unordered */ : ge;
997 default:
998 LOG(FATAL) << "UNREACHABLE";
999 UNREACHABLE();
1000 }
1001}
1002
Anton Kirilov74234da2017-01-13 14:42:47 +00001003inline ShiftType ShiftFromOpKind(HDataProcWithShifterOp::OpKind op_kind) {
1004 switch (op_kind) {
1005 case HDataProcWithShifterOp::kASR: return ShiftType::ASR;
1006 case HDataProcWithShifterOp::kLSL: return ShiftType::LSL;
1007 case HDataProcWithShifterOp::kLSR: return ShiftType::LSR;
1008 default:
1009 LOG(FATAL) << "Unexpected op kind " << op_kind;
1010 UNREACHABLE();
1011 }
1012}
1013
Scott Wakelingfe885462016-09-22 10:24:38 +01001014void CodeGeneratorARMVIXL::DumpCoreRegister(std::ostream& stream, int reg) const {
1015 stream << vixl32::Register(reg);
1016}
1017
1018void CodeGeneratorARMVIXL::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
1019 stream << vixl32::SRegister(reg);
1020}
1021
Vladimir Markoa0431112018-06-25 09:32:54 +01001022const ArmInstructionSetFeatures& CodeGeneratorARMVIXL::GetInstructionSetFeatures() const {
1023 return *GetCompilerOptions().GetInstructionSetFeatures()->AsArmInstructionSetFeatures();
1024}
1025
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001026static uint32_t ComputeSRegisterListMask(const SRegisterList& regs) {
Scott Wakelingfe885462016-09-22 10:24:38 +01001027 uint32_t mask = 0;
1028 for (uint32_t i = regs.GetFirstSRegister().GetCode();
1029 i <= regs.GetLastSRegister().GetCode();
1030 ++i) {
1031 mask |= (1 << i);
1032 }
1033 return mask;
1034}
1035
Artem Serovd4cc5b22016-11-04 11:19:09 +00001036// Saves the register in the stack. Returns the size taken on stack.
1037size_t CodeGeneratorARMVIXL::SaveCoreRegister(size_t stack_index ATTRIBUTE_UNUSED,
1038 uint32_t reg_id ATTRIBUTE_UNUSED) {
1039 TODO_VIXL32(FATAL);
Elliott Hughesc1896c92018-11-29 11:33:18 -08001040 UNREACHABLE();
Artem Serovd4cc5b22016-11-04 11:19:09 +00001041}
1042
1043// Restores the register from the stack. Returns the size taken on stack.
1044size_t CodeGeneratorARMVIXL::RestoreCoreRegister(size_t stack_index ATTRIBUTE_UNUSED,
1045 uint32_t reg_id ATTRIBUTE_UNUSED) {
1046 TODO_VIXL32(FATAL);
Elliott Hughesc1896c92018-11-29 11:33:18 -08001047 UNREACHABLE();
Artem Serovd4cc5b22016-11-04 11:19:09 +00001048}
1049
1050size_t CodeGeneratorARMVIXL::SaveFloatingPointRegister(size_t stack_index ATTRIBUTE_UNUSED,
1051 uint32_t reg_id ATTRIBUTE_UNUSED) {
1052 TODO_VIXL32(FATAL);
Elliott Hughesc1896c92018-11-29 11:33:18 -08001053 UNREACHABLE();
Artem Serovd4cc5b22016-11-04 11:19:09 +00001054}
1055
1056size_t CodeGeneratorARMVIXL::RestoreFloatingPointRegister(size_t stack_index ATTRIBUTE_UNUSED,
1057 uint32_t reg_id ATTRIBUTE_UNUSED) {
1058 TODO_VIXL32(FATAL);
Elliott Hughesc1896c92018-11-29 11:33:18 -08001059 UNREACHABLE();
Anton Kirilove28d9ae2016-10-25 18:17:23 +01001060}
1061
Anton Kirilov74234da2017-01-13 14:42:47 +00001062static void GenerateDataProcInstruction(HInstruction::InstructionKind kind,
1063 vixl32::Register out,
1064 vixl32::Register first,
1065 const Operand& second,
1066 CodeGeneratorARMVIXL* codegen) {
1067 if (second.IsImmediate() && second.GetImmediate() == 0) {
1068 const Operand in = kind == HInstruction::kAnd
1069 ? Operand(0)
1070 : Operand(first);
1071
1072 __ Mov(out, in);
1073 } else {
1074 switch (kind) {
1075 case HInstruction::kAdd:
1076 __ Add(out, first, second);
1077 break;
1078 case HInstruction::kAnd:
1079 __ And(out, first, second);
1080 break;
1081 case HInstruction::kOr:
1082 __ Orr(out, first, second);
1083 break;
1084 case HInstruction::kSub:
1085 __ Sub(out, first, second);
1086 break;
1087 case HInstruction::kXor:
1088 __ Eor(out, first, second);
1089 break;
1090 default:
1091 LOG(FATAL) << "Unexpected instruction kind: " << kind;
1092 UNREACHABLE();
1093 }
1094 }
1095}
1096
1097static void GenerateDataProc(HInstruction::InstructionKind kind,
1098 const Location& out,
1099 const Location& first,
1100 const Operand& second_lo,
1101 const Operand& second_hi,
1102 CodeGeneratorARMVIXL* codegen) {
1103 const vixl32::Register first_hi = HighRegisterFrom(first);
1104 const vixl32::Register first_lo = LowRegisterFrom(first);
1105 const vixl32::Register out_hi = HighRegisterFrom(out);
1106 const vixl32::Register out_lo = LowRegisterFrom(out);
1107
1108 if (kind == HInstruction::kAdd) {
1109 __ Adds(out_lo, first_lo, second_lo);
1110 __ Adc(out_hi, first_hi, second_hi);
1111 } else if (kind == HInstruction::kSub) {
1112 __ Subs(out_lo, first_lo, second_lo);
1113 __ Sbc(out_hi, first_hi, second_hi);
1114 } else {
1115 GenerateDataProcInstruction(kind, out_lo, first_lo, second_lo, codegen);
1116 GenerateDataProcInstruction(kind, out_hi, first_hi, second_hi, codegen);
1117 }
1118}
1119
1120static Operand GetShifterOperand(vixl32::Register rm, ShiftType shift, uint32_t shift_imm) {
1121 return shift_imm == 0 ? Operand(rm) : Operand(rm, shift, shift_imm);
1122}
1123
1124static void GenerateLongDataProc(HDataProcWithShifterOp* instruction,
1125 CodeGeneratorARMVIXL* codegen) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001126 DCHECK_EQ(instruction->GetType(), DataType::Type::kInt64);
Anton Kirilov74234da2017-01-13 14:42:47 +00001127 DCHECK(HDataProcWithShifterOp::IsShiftOp(instruction->GetOpKind()));
1128
1129 const LocationSummary* const locations = instruction->GetLocations();
1130 const uint32_t shift_value = instruction->GetShiftAmount();
1131 const HInstruction::InstructionKind kind = instruction->GetInstrKind();
1132 const Location first = locations->InAt(0);
1133 const Location second = locations->InAt(1);
1134 const Location out = locations->Out();
1135 const vixl32::Register first_hi = HighRegisterFrom(first);
1136 const vixl32::Register first_lo = LowRegisterFrom(first);
1137 const vixl32::Register out_hi = HighRegisterFrom(out);
1138 const vixl32::Register out_lo = LowRegisterFrom(out);
1139 const vixl32::Register second_hi = HighRegisterFrom(second);
1140 const vixl32::Register second_lo = LowRegisterFrom(second);
1141 const ShiftType shift = ShiftFromOpKind(instruction->GetOpKind());
1142
1143 if (shift_value >= 32) {
1144 if (shift == ShiftType::LSL) {
1145 GenerateDataProcInstruction(kind,
1146 out_hi,
1147 first_hi,
1148 Operand(second_lo, ShiftType::LSL, shift_value - 32),
1149 codegen);
1150 GenerateDataProcInstruction(kind, out_lo, first_lo, 0, codegen);
1151 } else if (shift == ShiftType::ASR) {
1152 GenerateDataProc(kind,
1153 out,
1154 first,
1155 GetShifterOperand(second_hi, ShiftType::ASR, shift_value - 32),
1156 Operand(second_hi, ShiftType::ASR, 31),
1157 codegen);
1158 } else {
1159 DCHECK_EQ(shift, ShiftType::LSR);
1160 GenerateDataProc(kind,
1161 out,
1162 first,
1163 GetShifterOperand(second_hi, ShiftType::LSR, shift_value - 32),
1164 0,
1165 codegen);
1166 }
1167 } else {
1168 DCHECK_GT(shift_value, 1U);
1169 DCHECK_LT(shift_value, 32U);
1170
1171 UseScratchRegisterScope temps(codegen->GetVIXLAssembler());
1172
1173 if (shift == ShiftType::LSL) {
1174 // We are not doing this for HInstruction::kAdd because the output will require
1175 // Location::kOutputOverlap; not applicable to other cases.
1176 if (kind == HInstruction::kOr || kind == HInstruction::kXor) {
1177 GenerateDataProcInstruction(kind,
1178 out_hi,
1179 first_hi,
1180 Operand(second_hi, ShiftType::LSL, shift_value),
1181 codegen);
1182 GenerateDataProcInstruction(kind,
1183 out_hi,
1184 out_hi,
1185 Operand(second_lo, ShiftType::LSR, 32 - shift_value),
1186 codegen);
1187 GenerateDataProcInstruction(kind,
1188 out_lo,
1189 first_lo,
1190 Operand(second_lo, ShiftType::LSL, shift_value),
1191 codegen);
1192 } else {
1193 const vixl32::Register temp = temps.Acquire();
1194
1195 __ Lsl(temp, second_hi, shift_value);
1196 __ Orr(temp, temp, Operand(second_lo, ShiftType::LSR, 32 - shift_value));
1197 GenerateDataProc(kind,
1198 out,
1199 first,
1200 Operand(second_lo, ShiftType::LSL, shift_value),
1201 temp,
1202 codegen);
1203 }
1204 } else {
1205 DCHECK(shift == ShiftType::ASR || shift == ShiftType::LSR);
1206
1207 // We are not doing this for HInstruction::kAdd because the output will require
1208 // Location::kOutputOverlap; not applicable to other cases.
1209 if (kind == HInstruction::kOr || kind == HInstruction::kXor) {
1210 GenerateDataProcInstruction(kind,
1211 out_lo,
1212 first_lo,
1213 Operand(second_lo, ShiftType::LSR, shift_value),
1214 codegen);
1215 GenerateDataProcInstruction(kind,
1216 out_lo,
1217 out_lo,
1218 Operand(second_hi, ShiftType::LSL, 32 - shift_value),
1219 codegen);
1220 GenerateDataProcInstruction(kind,
1221 out_hi,
1222 first_hi,
1223 Operand(second_hi, shift, shift_value),
1224 codegen);
1225 } else {
1226 const vixl32::Register temp = temps.Acquire();
1227
1228 __ Lsr(temp, second_lo, shift_value);
1229 __ Orr(temp, temp, Operand(second_hi, ShiftType::LSL, 32 - shift_value));
1230 GenerateDataProc(kind,
1231 out,
1232 first,
1233 temp,
1234 Operand(second_hi, shift, shift_value),
1235 codegen);
1236 }
1237 }
1238 }
1239}
1240
Donghui Bai426b49c2016-11-08 14:55:38 +08001241static void GenerateVcmp(HInstruction* instruction, CodeGeneratorARMVIXL* codegen) {
1242 const Location rhs_loc = instruction->GetLocations()->InAt(1);
1243 if (rhs_loc.IsConstant()) {
1244 // 0.0 is the only immediate that can be encoded directly in
1245 // a VCMP instruction.
1246 //
1247 // Both the JLS (section 15.20.1) and the JVMS (section 6.5)
1248 // specify that in a floating-point comparison, positive zero
1249 // and negative zero are considered equal, so we can use the
1250 // literal 0.0 for both cases here.
1251 //
1252 // Note however that some methods (Float.equal, Float.compare,
1253 // Float.compareTo, Double.equal, Double.compare,
1254 // Double.compareTo, Math.max, Math.min, StrictMath.max,
1255 // StrictMath.min) consider 0.0 to be (strictly) greater than
1256 // -0.0. So if we ever translate calls to these methods into a
1257 // HCompare instruction, we must handle the -0.0 case with
1258 // care here.
1259 DCHECK(rhs_loc.GetConstant()->IsArithmeticZero());
1260
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001261 const DataType::Type type = instruction->InputAt(0)->GetType();
Donghui Bai426b49c2016-11-08 14:55:38 +08001262
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001263 if (type == DataType::Type::kFloat32) {
Donghui Bai426b49c2016-11-08 14:55:38 +08001264 __ Vcmp(F32, InputSRegisterAt(instruction, 0), 0.0);
1265 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001266 DCHECK_EQ(type, DataType::Type::kFloat64);
Donghui Bai426b49c2016-11-08 14:55:38 +08001267 __ Vcmp(F64, InputDRegisterAt(instruction, 0), 0.0);
1268 }
1269 } else {
1270 __ Vcmp(InputVRegisterAt(instruction, 0), InputVRegisterAt(instruction, 1));
1271 }
1272}
1273
Anton Kirilov5601d4e2017-05-11 19:33:50 +01001274static int64_t AdjustConstantForCondition(int64_t value,
1275 IfCondition* condition,
1276 IfCondition* opposite) {
1277 if (value == 1) {
1278 if (*condition == kCondB) {
1279 value = 0;
1280 *condition = kCondEQ;
1281 *opposite = kCondNE;
1282 } else if (*condition == kCondAE) {
1283 value = 0;
1284 *condition = kCondNE;
1285 *opposite = kCondEQ;
1286 }
1287 } else if (value == -1) {
1288 if (*condition == kCondGT) {
1289 value = 0;
1290 *condition = kCondGE;
1291 *opposite = kCondLT;
1292 } else if (*condition == kCondLE) {
1293 value = 0;
1294 *condition = kCondLT;
1295 *opposite = kCondGE;
1296 }
1297 }
1298
1299 return value;
1300}
1301
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001302static std::pair<vixl32::Condition, vixl32::Condition> GenerateLongTestConstant(
1303 HCondition* condition,
1304 bool invert,
1305 CodeGeneratorARMVIXL* codegen) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001306 DCHECK_EQ(condition->GetLeft()->GetType(), DataType::Type::kInt64);
Donghui Bai426b49c2016-11-08 14:55:38 +08001307
1308 const LocationSummary* const locations = condition->GetLocations();
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001309 IfCondition cond = condition->GetCondition();
1310 IfCondition opposite = condition->GetOppositeCondition();
1311
1312 if (invert) {
1313 std::swap(cond, opposite);
1314 }
1315
1316 std::pair<vixl32::Condition, vixl32::Condition> ret(eq, ne);
Donghui Bai426b49c2016-11-08 14:55:38 +08001317 const Location left = locations->InAt(0);
1318 const Location right = locations->InAt(1);
1319
1320 DCHECK(right.IsConstant());
1321
1322 const vixl32::Register left_high = HighRegisterFrom(left);
1323 const vixl32::Register left_low = LowRegisterFrom(left);
Anton Kirilov5601d4e2017-05-11 19:33:50 +01001324 int64_t value = AdjustConstantForCondition(Int64ConstantFrom(right), &cond, &opposite);
1325 UseScratchRegisterScope temps(codegen->GetVIXLAssembler());
1326
1327 // Comparisons against 0 are common enough to deserve special attention.
1328 if (value == 0) {
1329 switch (cond) {
1330 case kCondNE:
1331 // x > 0 iff x != 0 when the comparison is unsigned.
1332 case kCondA:
1333 ret = std::make_pair(ne, eq);
1334 FALLTHROUGH_INTENDED;
1335 case kCondEQ:
1336 // x <= 0 iff x == 0 when the comparison is unsigned.
1337 case kCondBE:
1338 __ Orrs(temps.Acquire(), left_low, left_high);
1339 return ret;
1340 case kCondLT:
1341 case kCondGE:
1342 __ Cmp(left_high, 0);
1343 return std::make_pair(ARMCondition(cond), ARMCondition(opposite));
1344 // Trivially true or false.
1345 case kCondB:
1346 ret = std::make_pair(ne, eq);
1347 FALLTHROUGH_INTENDED;
1348 case kCondAE:
1349 __ Cmp(left_low, left_low);
1350 return ret;
1351 default:
1352 break;
1353 }
1354 }
Donghui Bai426b49c2016-11-08 14:55:38 +08001355
1356 switch (cond) {
1357 case kCondEQ:
1358 case kCondNE:
1359 case kCondB:
1360 case kCondBE:
1361 case kCondA:
1362 case kCondAE: {
Anton Kirilov23b752b2017-07-20 14:40:44 +01001363 const uint32_t value_low = Low32Bits(value);
1364 Operand operand_low(value_low);
1365
Donghui Bai426b49c2016-11-08 14:55:38 +08001366 __ Cmp(left_high, High32Bits(value));
1367
Anton Kirilov23b752b2017-07-20 14:40:44 +01001368 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
1369 // we must ensure that the operands corresponding to the least significant
1370 // halves of the inputs fit into a 16-bit CMP encoding.
1371 if (!left_low.IsLow() || !IsUint<8>(value_low)) {
1372 operand_low = Operand(temps.Acquire());
1373 __ Mov(LeaveFlags, operand_low.GetBaseRegister(), value_low);
1374 }
1375
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001376 // We use the scope because of the IT block that follows.
Donghui Bai426b49c2016-11-08 14:55:38 +08001377 ExactAssemblyScope guard(codegen->GetVIXLAssembler(),
1378 2 * vixl32::k16BitT32InstructionSizeInBytes,
1379 CodeBufferCheckScope::kExactSize);
1380
1381 __ it(eq);
Anton Kirilov23b752b2017-07-20 14:40:44 +01001382 __ cmp(eq, left_low, operand_low);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001383 ret = std::make_pair(ARMUnsignedCondition(cond), ARMUnsignedCondition(opposite));
Donghui Bai426b49c2016-11-08 14:55:38 +08001384 break;
1385 }
1386 case kCondLE:
1387 case kCondGT:
1388 // Trivially true or false.
1389 if (value == std::numeric_limits<int64_t>::max()) {
1390 __ Cmp(left_low, left_low);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001391 ret = cond == kCondLE ? std::make_pair(eq, ne) : std::make_pair(ne, eq);
Donghui Bai426b49c2016-11-08 14:55:38 +08001392 break;
1393 }
1394
1395 if (cond == kCondLE) {
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001396 DCHECK_EQ(opposite, kCondGT);
Donghui Bai426b49c2016-11-08 14:55:38 +08001397 cond = kCondLT;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001398 opposite = kCondGE;
Donghui Bai426b49c2016-11-08 14:55:38 +08001399 } else {
1400 DCHECK_EQ(cond, kCondGT);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001401 DCHECK_EQ(opposite, kCondLE);
Donghui Bai426b49c2016-11-08 14:55:38 +08001402 cond = kCondGE;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001403 opposite = kCondLT;
Donghui Bai426b49c2016-11-08 14:55:38 +08001404 }
1405
1406 value++;
1407 FALLTHROUGH_INTENDED;
1408 case kCondGE:
1409 case kCondLT: {
Donghui Bai426b49c2016-11-08 14:55:38 +08001410 __ Cmp(left_low, Low32Bits(value));
1411 __ Sbcs(temps.Acquire(), left_high, High32Bits(value));
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001412 ret = std::make_pair(ARMCondition(cond), ARMCondition(opposite));
Donghui Bai426b49c2016-11-08 14:55:38 +08001413 break;
1414 }
1415 default:
1416 LOG(FATAL) << "Unreachable";
1417 UNREACHABLE();
1418 }
1419
1420 return ret;
1421}
1422
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001423static std::pair<vixl32::Condition, vixl32::Condition> GenerateLongTest(
1424 HCondition* condition,
1425 bool invert,
1426 CodeGeneratorARMVIXL* codegen) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001427 DCHECK_EQ(condition->GetLeft()->GetType(), DataType::Type::kInt64);
Donghui Bai426b49c2016-11-08 14:55:38 +08001428
1429 const LocationSummary* const locations = condition->GetLocations();
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001430 IfCondition cond = condition->GetCondition();
1431 IfCondition opposite = condition->GetOppositeCondition();
1432
1433 if (invert) {
1434 std::swap(cond, opposite);
1435 }
1436
1437 std::pair<vixl32::Condition, vixl32::Condition> ret(eq, ne);
Donghui Bai426b49c2016-11-08 14:55:38 +08001438 Location left = locations->InAt(0);
1439 Location right = locations->InAt(1);
1440
1441 DCHECK(right.IsRegisterPair());
1442
1443 switch (cond) {
1444 case kCondEQ:
1445 case kCondNE:
1446 case kCondB:
1447 case kCondBE:
1448 case kCondA:
1449 case kCondAE: {
1450 __ Cmp(HighRegisterFrom(left), HighRegisterFrom(right));
1451
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001452 // We use the scope because of the IT block that follows.
Donghui Bai426b49c2016-11-08 14:55:38 +08001453 ExactAssemblyScope guard(codegen->GetVIXLAssembler(),
1454 2 * vixl32::k16BitT32InstructionSizeInBytes,
1455 CodeBufferCheckScope::kExactSize);
1456
1457 __ it(eq);
1458 __ cmp(eq, LowRegisterFrom(left), LowRegisterFrom(right));
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001459 ret = std::make_pair(ARMUnsignedCondition(cond), ARMUnsignedCondition(opposite));
Donghui Bai426b49c2016-11-08 14:55:38 +08001460 break;
1461 }
1462 case kCondLE:
1463 case kCondGT:
1464 if (cond == kCondLE) {
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001465 DCHECK_EQ(opposite, kCondGT);
Donghui Bai426b49c2016-11-08 14:55:38 +08001466 cond = kCondGE;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001467 opposite = kCondLT;
Donghui Bai426b49c2016-11-08 14:55:38 +08001468 } else {
1469 DCHECK_EQ(cond, kCondGT);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001470 DCHECK_EQ(opposite, kCondLE);
Donghui Bai426b49c2016-11-08 14:55:38 +08001471 cond = kCondLT;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001472 opposite = kCondGE;
Donghui Bai426b49c2016-11-08 14:55:38 +08001473 }
1474
1475 std::swap(left, right);
1476 FALLTHROUGH_INTENDED;
1477 case kCondGE:
1478 case kCondLT: {
1479 UseScratchRegisterScope temps(codegen->GetVIXLAssembler());
1480
1481 __ Cmp(LowRegisterFrom(left), LowRegisterFrom(right));
1482 __ Sbcs(temps.Acquire(), HighRegisterFrom(left), HighRegisterFrom(right));
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001483 ret = std::make_pair(ARMCondition(cond), ARMCondition(opposite));
Donghui Bai426b49c2016-11-08 14:55:38 +08001484 break;
1485 }
1486 default:
1487 LOG(FATAL) << "Unreachable";
1488 UNREACHABLE();
1489 }
1490
1491 return ret;
1492}
1493
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001494static std::pair<vixl32::Condition, vixl32::Condition> GenerateTest(HCondition* condition,
1495 bool invert,
1496 CodeGeneratorARMVIXL* codegen) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001497 const DataType::Type type = condition->GetLeft()->GetType();
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001498 IfCondition cond = condition->GetCondition();
1499 IfCondition opposite = condition->GetOppositeCondition();
1500 std::pair<vixl32::Condition, vixl32::Condition> ret(eq, ne);
Donghui Bai426b49c2016-11-08 14:55:38 +08001501
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001502 if (invert) {
1503 std::swap(cond, opposite);
1504 }
Donghui Bai426b49c2016-11-08 14:55:38 +08001505
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001506 if (type == DataType::Type::kInt64) {
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001507 ret = condition->GetLocations()->InAt(1).IsConstant()
1508 ? GenerateLongTestConstant(condition, invert, codegen)
1509 : GenerateLongTest(condition, invert, codegen);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001510 } else if (DataType::IsFloatingPointType(type)) {
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001511 GenerateVcmp(condition, codegen);
1512 __ Vmrs(RegisterOrAPSR_nzcv(kPcCode), FPSCR);
1513 ret = std::make_pair(ARMFPCondition(cond, condition->IsGtBias()),
1514 ARMFPCondition(opposite, condition->IsGtBias()));
Donghui Bai426b49c2016-11-08 14:55:38 +08001515 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001516 DCHECK(DataType::IsIntegralType(type) || type == DataType::Type::kReference) << type;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001517 __ Cmp(InputRegisterAt(condition, 0), InputOperandAt(condition, 1));
1518 ret = std::make_pair(ARMCondition(cond), ARMCondition(opposite));
Donghui Bai426b49c2016-11-08 14:55:38 +08001519 }
1520
1521 return ret;
1522}
1523
Anton Kirilov5601d4e2017-05-11 19:33:50 +01001524static void GenerateConditionGeneric(HCondition* cond, CodeGeneratorARMVIXL* codegen) {
Anton Kirilov5601d4e2017-05-11 19:33:50 +01001525 const vixl32::Register out = OutputRegister(cond);
1526 const auto condition = GenerateTest(cond, false, codegen);
1527
1528 __ Mov(LeaveFlags, out, 0);
1529
1530 if (out.IsLow()) {
1531 // We use the scope because of the IT block that follows.
1532 ExactAssemblyScope guard(codegen->GetVIXLAssembler(),
1533 2 * vixl32::k16BitT32InstructionSizeInBytes,
1534 CodeBufferCheckScope::kExactSize);
1535
1536 __ it(condition.first);
1537 __ mov(condition.first, out, 1);
1538 } else {
1539 vixl32::Label done_label;
1540 vixl32::Label* const final_label = codegen->GetFinalLabel(cond, &done_label);
1541
1542 __ B(condition.second, final_label, /* far_target */ false);
1543 __ Mov(out, 1);
1544
1545 if (done_label.IsReferenced()) {
1546 __ Bind(&done_label);
1547 }
1548 }
1549}
1550
1551static void GenerateEqualLong(HCondition* cond, CodeGeneratorARMVIXL* codegen) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001552 DCHECK_EQ(cond->GetLeft()->GetType(), DataType::Type::kInt64);
Anton Kirilov5601d4e2017-05-11 19:33:50 +01001553
1554 const LocationSummary* const locations = cond->GetLocations();
1555 IfCondition condition = cond->GetCondition();
1556 const vixl32::Register out = OutputRegister(cond);
1557 const Location left = locations->InAt(0);
1558 const Location right = locations->InAt(1);
1559 vixl32::Register left_high = HighRegisterFrom(left);
1560 vixl32::Register left_low = LowRegisterFrom(left);
1561 vixl32::Register temp;
1562 UseScratchRegisterScope temps(codegen->GetVIXLAssembler());
1563
1564 if (right.IsConstant()) {
1565 IfCondition opposite = cond->GetOppositeCondition();
1566 const int64_t value = AdjustConstantForCondition(Int64ConstantFrom(right),
1567 &condition,
1568 &opposite);
1569 Operand right_high = High32Bits(value);
1570 Operand right_low = Low32Bits(value);
1571
1572 // The output uses Location::kNoOutputOverlap.
1573 if (out.Is(left_high)) {
1574 std::swap(left_low, left_high);
1575 std::swap(right_low, right_high);
1576 }
1577
1578 __ Sub(out, left_low, right_low);
1579 temp = temps.Acquire();
1580 __ Sub(temp, left_high, right_high);
1581 } else {
1582 DCHECK(right.IsRegisterPair());
1583 temp = temps.Acquire();
1584 __ Sub(temp, left_high, HighRegisterFrom(right));
1585 __ Sub(out, left_low, LowRegisterFrom(right));
1586 }
1587
1588 // Need to check after calling AdjustConstantForCondition().
1589 DCHECK(condition == kCondEQ || condition == kCondNE) << condition;
1590
1591 if (condition == kCondNE && out.IsLow()) {
1592 __ Orrs(out, out, temp);
1593
1594 // We use the scope because of the IT block that follows.
1595 ExactAssemblyScope guard(codegen->GetVIXLAssembler(),
1596 2 * vixl32::k16BitT32InstructionSizeInBytes,
1597 CodeBufferCheckScope::kExactSize);
1598
1599 __ it(ne);
1600 __ mov(ne, out, 1);
1601 } else {
1602 __ Orr(out, out, temp);
1603 codegen->GenerateConditionWithZero(condition, out, out, temp);
1604 }
1605}
1606
Anton Kirilov5601d4e2017-05-11 19:33:50 +01001607static void GenerateConditionLong(HCondition* cond, CodeGeneratorARMVIXL* codegen) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001608 DCHECK_EQ(cond->GetLeft()->GetType(), DataType::Type::kInt64);
Anton Kirilov5601d4e2017-05-11 19:33:50 +01001609
1610 const LocationSummary* const locations = cond->GetLocations();
1611 IfCondition condition = cond->GetCondition();
1612 const vixl32::Register out = OutputRegister(cond);
1613 const Location left = locations->InAt(0);
1614 const Location right = locations->InAt(1);
1615
1616 if (right.IsConstant()) {
1617 IfCondition opposite = cond->GetOppositeCondition();
1618
1619 // Comparisons against 0 are common enough to deserve special attention.
1620 if (AdjustConstantForCondition(Int64ConstantFrom(right), &condition, &opposite) == 0) {
1621 switch (condition) {
1622 case kCondNE:
1623 case kCondA:
1624 if (out.IsLow()) {
1625 // We only care if both input registers are 0 or not.
1626 __ Orrs(out, LowRegisterFrom(left), HighRegisterFrom(left));
1627
1628 // We use the scope because of the IT block that follows.
1629 ExactAssemblyScope guard(codegen->GetVIXLAssembler(),
1630 2 * vixl32::k16BitT32InstructionSizeInBytes,
1631 CodeBufferCheckScope::kExactSize);
1632
1633 __ it(ne);
1634 __ mov(ne, out, 1);
1635 return;
1636 }
1637
1638 FALLTHROUGH_INTENDED;
1639 case kCondEQ:
1640 case kCondBE:
1641 // We only care if both input registers are 0 or not.
1642 __ Orr(out, LowRegisterFrom(left), HighRegisterFrom(left));
1643 codegen->GenerateConditionWithZero(condition, out, out);
1644 return;
1645 case kCondLT:
1646 case kCondGE:
1647 // We only care about the sign bit.
1648 FALLTHROUGH_INTENDED;
1649 case kCondAE:
1650 case kCondB:
1651 codegen->GenerateConditionWithZero(condition, out, HighRegisterFrom(left));
1652 return;
1653 case kCondLE:
1654 case kCondGT:
1655 default:
1656 break;
1657 }
1658 }
1659 }
1660
Anton Kirilov23b752b2017-07-20 14:40:44 +01001661 // If `out` is a low register, then the GenerateConditionGeneric()
1662 // function generates a shorter code sequence that is still branchless.
1663 if ((condition == kCondEQ || condition == kCondNE) && !out.IsLow()) {
Anton Kirilov5601d4e2017-05-11 19:33:50 +01001664 GenerateEqualLong(cond, codegen);
1665 return;
1666 }
1667
Anton Kirilov23b752b2017-07-20 14:40:44 +01001668 GenerateConditionGeneric(cond, codegen);
Anton Kirilov5601d4e2017-05-11 19:33:50 +01001669}
1670
Roland Levillain6d729a72017-06-30 18:34:01 +01001671static void GenerateConditionIntegralOrNonPrimitive(HCondition* cond,
1672 CodeGeneratorARMVIXL* codegen) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001673 const DataType::Type type = cond->GetLeft()->GetType();
Anton Kirilov5601d4e2017-05-11 19:33:50 +01001674
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001675 DCHECK(DataType::IsIntegralType(type) || type == DataType::Type::kReference) << type;
Anton Kirilov5601d4e2017-05-11 19:33:50 +01001676
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001677 if (type == DataType::Type::kInt64) {
Anton Kirilov5601d4e2017-05-11 19:33:50 +01001678 GenerateConditionLong(cond, codegen);
1679 return;
1680 }
1681
1682 IfCondition condition = cond->GetCondition();
1683 vixl32::Register in = InputRegisterAt(cond, 0);
1684 const vixl32::Register out = OutputRegister(cond);
1685 const Location right = cond->GetLocations()->InAt(1);
1686 int64_t value;
1687
1688 if (right.IsConstant()) {
1689 IfCondition opposite = cond->GetOppositeCondition();
1690
1691 value = AdjustConstantForCondition(Int64ConstantFrom(right), &condition, &opposite);
1692
1693 // Comparisons against 0 are common enough to deserve special attention.
1694 if (value == 0) {
1695 switch (condition) {
1696 case kCondNE:
1697 case kCondA:
1698 if (out.IsLow() && out.Is(in)) {
1699 __ Cmp(out, 0);
1700
1701 // We use the scope because of the IT block that follows.
1702 ExactAssemblyScope guard(codegen->GetVIXLAssembler(),
1703 2 * vixl32::k16BitT32InstructionSizeInBytes,
1704 CodeBufferCheckScope::kExactSize);
1705
1706 __ it(ne);
1707 __ mov(ne, out, 1);
1708 return;
1709 }
1710
1711 FALLTHROUGH_INTENDED;
1712 case kCondEQ:
1713 case kCondBE:
1714 case kCondLT:
1715 case kCondGE:
1716 case kCondAE:
1717 case kCondB:
1718 codegen->GenerateConditionWithZero(condition, out, in);
1719 return;
1720 case kCondLE:
1721 case kCondGT:
1722 default:
1723 break;
1724 }
1725 }
1726 }
1727
1728 if (condition == kCondEQ || condition == kCondNE) {
1729 Operand operand(0);
1730
1731 if (right.IsConstant()) {
1732 operand = Operand::From(value);
1733 } else if (out.Is(RegisterFrom(right))) {
1734 // Avoid 32-bit instructions if possible.
1735 operand = InputOperandAt(cond, 0);
1736 in = RegisterFrom(right);
1737 } else {
1738 operand = InputOperandAt(cond, 1);
1739 }
1740
1741 if (condition == kCondNE && out.IsLow()) {
1742 __ Subs(out, in, operand);
1743
1744 // We use the scope because of the IT block that follows.
1745 ExactAssemblyScope guard(codegen->GetVIXLAssembler(),
1746 2 * vixl32::k16BitT32InstructionSizeInBytes,
1747 CodeBufferCheckScope::kExactSize);
1748
1749 __ it(ne);
1750 __ mov(ne, out, 1);
1751 } else {
1752 __ Sub(out, in, operand);
1753 codegen->GenerateConditionWithZero(condition, out, out);
1754 }
1755
1756 return;
1757 }
1758
1759 GenerateConditionGeneric(cond, codegen);
1760}
1761
Donghui Bai426b49c2016-11-08 14:55:38 +08001762static bool CanEncodeConstantAs8BitImmediate(HConstant* constant) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001763 const DataType::Type type = constant->GetType();
Donghui Bai426b49c2016-11-08 14:55:38 +08001764 bool ret = false;
1765
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001766 DCHECK(DataType::IsIntegralType(type) || type == DataType::Type::kReference) << type;
Donghui Bai426b49c2016-11-08 14:55:38 +08001767
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001768 if (type == DataType::Type::kInt64) {
Donghui Bai426b49c2016-11-08 14:55:38 +08001769 const uint64_t value = Uint64ConstantFrom(constant);
1770
1771 ret = IsUint<8>(Low32Bits(value)) && IsUint<8>(High32Bits(value));
1772 } else {
1773 ret = IsUint<8>(Int32ConstantFrom(constant));
1774 }
1775
1776 return ret;
1777}
1778
1779static Location Arm8BitEncodableConstantOrRegister(HInstruction* constant) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001780 DCHECK(!DataType::IsFloatingPointType(constant->GetType()));
Donghui Bai426b49c2016-11-08 14:55:38 +08001781
1782 if (constant->IsConstant() && CanEncodeConstantAs8BitImmediate(constant->AsConstant())) {
1783 return Location::ConstantLocation(constant->AsConstant());
1784 }
1785
1786 return Location::RequiresRegister();
1787}
1788
1789static bool CanGenerateConditionalMove(const Location& out, const Location& src) {
1790 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
1791 // we check that we are not dealing with floating-point output (there is no
1792 // 16-bit VMOV encoding).
1793 if (!out.IsRegister() && !out.IsRegisterPair()) {
1794 return false;
1795 }
1796
1797 // For constants, we also check that the output is in one or two low registers,
1798 // and that the constants fit in an 8-bit unsigned integer, so that a 16-bit
1799 // MOV encoding can be used.
1800 if (src.IsConstant()) {
1801 if (!CanEncodeConstantAs8BitImmediate(src.GetConstant())) {
1802 return false;
1803 }
1804
1805 if (out.IsRegister()) {
1806 if (!RegisterFrom(out).IsLow()) {
1807 return false;
1808 }
1809 } else {
1810 DCHECK(out.IsRegisterPair());
1811
1812 if (!HighRegisterFrom(out).IsLow()) {
1813 return false;
1814 }
1815 }
1816 }
1817
1818 return true;
1819}
1820
Scott Wakelingfe885462016-09-22 10:24:38 +01001821#undef __
1822
Donghui Bai426b49c2016-11-08 14:55:38 +08001823vixl32::Label* CodeGeneratorARMVIXL::GetFinalLabel(HInstruction* instruction,
1824 vixl32::Label* final_label) {
1825 DCHECK(!instruction->IsControlFlow() && !instruction->IsSuspendCheck());
Anton Kirilov6f644202017-02-27 18:29:45 +00001826 DCHECK(!instruction->IsInvoke() || !instruction->GetLocations()->CanCall());
Donghui Bai426b49c2016-11-08 14:55:38 +08001827
1828 const HBasicBlock* const block = instruction->GetBlock();
1829 const HLoopInformation* const info = block->GetLoopInformation();
1830 HInstruction* const next = instruction->GetNext();
1831
1832 // Avoid a branch to a branch.
1833 if (next->IsGoto() && (info == nullptr ||
1834 !info->IsBackEdge(*block) ||
1835 !info->HasSuspendCheck())) {
1836 final_label = GetLabelOf(next->AsGoto()->GetSuccessor());
1837 }
1838
1839 return final_label;
1840}
1841
Scott Wakelingfe885462016-09-22 10:24:38 +01001842CodeGeneratorARMVIXL::CodeGeneratorARMVIXL(HGraph* graph,
Scott Wakelingfe885462016-09-22 10:24:38 +01001843 const CompilerOptions& compiler_options,
1844 OptimizingCompilerStats* stats)
1845 : CodeGenerator(graph,
1846 kNumberOfCoreRegisters,
1847 kNumberOfSRegisters,
1848 kNumberOfRegisterPairs,
1849 kCoreCalleeSaves.GetList(),
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001850 ComputeSRegisterListMask(kFpuCalleeSaves),
Scott Wakelingfe885462016-09-22 10:24:38 +01001851 compiler_options,
1852 stats),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001853 block_labels_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1854 jump_tables_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Scott Wakelingfe885462016-09-22 10:24:38 +01001855 location_builder_(graph, this),
1856 instruction_visitor_(graph, this),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001857 move_resolver_(graph->GetAllocator(), this),
1858 assembler_(graph->GetAllocator()),
Artem Serovc5fcb442016-12-02 19:19:58 +00001859 uint32_literals_(std::less<uint32_t>(),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001860 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001861 boot_image_method_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001862 method_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001863 boot_image_type_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001864 type_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001865 boot_image_string_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001866 string_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko6fd16062018-06-26 11:02:04 +01001867 boot_image_intrinsic_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001868 baker_read_barrier_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Artem Serovc5fcb442016-12-02 19:19:58 +00001869 jit_string_patches_(StringReferenceValueComparator(),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001870 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Artem Serovc5fcb442016-12-02 19:19:58 +00001871 jit_class_patches_(TypeReferenceValueComparator(),
Vladimir Marko966b46f2018-08-03 10:20:19 +00001872 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1873 jit_baker_read_barrier_slow_paths_(std::less<uint32_t>(),
1874 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)) {
Scott Wakelingfe885462016-09-22 10:24:38 +01001875 // Always save the LR register to mimic Quick.
1876 AddAllocatedRegister(Location::RegisterLocation(LR));
Nicolas Geoffray13a797b2017-03-15 16:41:31 +00001877 // Give D30 and D31 as scratch register to VIXL. The register allocator only works on
1878 // S0-S31, which alias to D0-D15.
1879 GetVIXLAssembler()->GetScratchVRegisterList()->Combine(d31);
1880 GetVIXLAssembler()->GetScratchVRegisterList()->Combine(d30);
Scott Wakelingfe885462016-09-22 10:24:38 +01001881}
1882
Artem Serov551b28f2016-10-18 19:11:30 +01001883void JumpTableARMVIXL::EmitTable(CodeGeneratorARMVIXL* codegen) {
1884 uint32_t num_entries = switch_instr_->GetNumEntries();
1885 DCHECK_GE(num_entries, kPackedSwitchCompareJumpThreshold);
1886
1887 // We are about to use the assembler to place literals directly. Make sure we have enough
Scott Wakelingb77051e2016-11-21 19:46:00 +00001888 // underlying code buffer and we have generated a jump table of the right size, using
1889 // codegen->GetVIXLAssembler()->GetBuffer().Align();
Artem Serov0fb37192016-12-06 18:13:40 +00001890 ExactAssemblyScope aas(codegen->GetVIXLAssembler(),
1891 num_entries * sizeof(int32_t),
1892 CodeBufferCheckScope::kMaximumSize);
Artem Serov551b28f2016-10-18 19:11:30 +01001893 // TODO(VIXL): Check that using lower case bind is fine here.
1894 codegen->GetVIXLAssembler()->bind(&table_start_);
Artem Serov09a940d2016-11-11 16:15:11 +00001895 for (uint32_t i = 0; i < num_entries; i++) {
1896 codegen->GetVIXLAssembler()->place(bb_addresses_[i].get());
1897 }
1898}
1899
1900void JumpTableARMVIXL::FixTable(CodeGeneratorARMVIXL* codegen) {
1901 uint32_t num_entries = switch_instr_->GetNumEntries();
1902 DCHECK_GE(num_entries, kPackedSwitchCompareJumpThreshold);
1903
Artem Serov551b28f2016-10-18 19:11:30 +01001904 const ArenaVector<HBasicBlock*>& successors = switch_instr_->GetBlock()->GetSuccessors();
1905 for (uint32_t i = 0; i < num_entries; i++) {
1906 vixl32::Label* target_label = codegen->GetLabelOf(successors[i]);
1907 DCHECK(target_label->IsBound());
1908 int32_t jump_offset = target_label->GetLocation() - table_start_.GetLocation();
1909 // When doing BX to address we need to have lower bit set to 1 in T32.
1910 if (codegen->GetVIXLAssembler()->IsUsingT32()) {
1911 jump_offset++;
1912 }
1913 DCHECK_GT(jump_offset, std::numeric_limits<int32_t>::min());
1914 DCHECK_LE(jump_offset, std::numeric_limits<int32_t>::max());
Artem Serov09a940d2016-11-11 16:15:11 +00001915
Scott Wakelingb77051e2016-11-21 19:46:00 +00001916 bb_addresses_[i].get()->UpdateValue(jump_offset, codegen->GetVIXLAssembler()->GetBuffer());
Artem Serov551b28f2016-10-18 19:11:30 +01001917 }
1918}
1919
Artem Serov09a940d2016-11-11 16:15:11 +00001920void CodeGeneratorARMVIXL::FixJumpTables() {
Artem Serov551b28f2016-10-18 19:11:30 +01001921 for (auto&& jump_table : jump_tables_) {
Artem Serov09a940d2016-11-11 16:15:11 +00001922 jump_table->FixTable(this);
Artem Serov551b28f2016-10-18 19:11:30 +01001923 }
1924}
1925
Andreas Gampeca620d72016-11-08 08:09:33 -08001926#define __ reinterpret_cast<ArmVIXLAssembler*>(GetAssembler())->GetVIXLAssembler()-> // NOLINT
Scott Wakelingfe885462016-09-22 10:24:38 +01001927
1928void CodeGeneratorARMVIXL::Finalize(CodeAllocator* allocator) {
Artem Serov09a940d2016-11-11 16:15:11 +00001929 FixJumpTables();
Vladimir Marko966b46f2018-08-03 10:20:19 +00001930
1931 // Emit JIT baker read barrier slow paths.
1932 DCHECK(Runtime::Current()->UseJitCompilation() || jit_baker_read_barrier_slow_paths_.empty());
1933 for (auto& entry : jit_baker_read_barrier_slow_paths_) {
1934 uint32_t encoded_data = entry.first;
1935 vixl::aarch32::Label* slow_path_entry = &entry.second.label;
1936 __ Bind(slow_path_entry);
1937 CompileBakerReadBarrierThunk(*GetAssembler(), encoded_data, /* debug_name */ nullptr);
1938 }
1939
Scott Wakelingfe885462016-09-22 10:24:38 +01001940 GetAssembler()->FinalizeCode();
1941 CodeGenerator::Finalize(allocator);
Vladimir Markoca1e0382018-04-11 09:58:41 +00001942
1943 // Verify Baker read barrier linker patches.
1944 if (kIsDebugBuild) {
1945 ArrayRef<const uint8_t> code = allocator->GetMemory();
1946 for (const BakerReadBarrierPatchInfo& info : baker_read_barrier_patches_) {
1947 DCHECK(info.label.IsBound());
1948 uint32_t literal_offset = info.label.GetLocation();
1949 DCHECK_ALIGNED(literal_offset, 2u);
1950
1951 auto GetInsn16 = [&code](uint32_t offset) {
1952 DCHECK_ALIGNED(offset, 2u);
1953 return (static_cast<uint32_t>(code[offset + 0]) << 0) +
1954 (static_cast<uint32_t>(code[offset + 1]) << 8);
1955 };
1956 auto GetInsn32 = [=](uint32_t offset) {
1957 return (GetInsn16(offset) << 16) + (GetInsn16(offset + 2u) << 0);
1958 };
1959
1960 uint32_t encoded_data = info.custom_data;
1961 BakerReadBarrierKind kind = BakerReadBarrierKindField::Decode(encoded_data);
1962 // Check that the next instruction matches the expected LDR.
1963 switch (kind) {
1964 case BakerReadBarrierKind::kField: {
1965 BakerReadBarrierWidth width = BakerReadBarrierWidthField::Decode(encoded_data);
1966 if (width == BakerReadBarrierWidth::kWide) {
1967 DCHECK_GE(code.size() - literal_offset, 8u);
1968 uint32_t next_insn = GetInsn32(literal_offset + 4u);
1969 // LDR (immediate), encoding T3, with correct base_reg.
1970 CheckValidReg((next_insn >> 12) & 0xfu); // Check destination register.
1971 const uint32_t base_reg = BakerReadBarrierFirstRegField::Decode(encoded_data);
1972 CHECK_EQ(next_insn & 0xffff0000u, 0xf8d00000u | (base_reg << 16));
1973 } else {
1974 DCHECK_GE(code.size() - literal_offset, 6u);
1975 uint32_t next_insn = GetInsn16(literal_offset + 4u);
1976 // LDR (immediate), encoding T1, with correct base_reg.
1977 CheckValidReg(next_insn & 0x7u); // Check destination register.
1978 const uint32_t base_reg = BakerReadBarrierFirstRegField::Decode(encoded_data);
1979 CHECK_EQ(next_insn & 0xf838u, 0x6800u | (base_reg << 3));
1980 }
1981 break;
1982 }
1983 case BakerReadBarrierKind::kArray: {
1984 DCHECK_GE(code.size() - literal_offset, 8u);
1985 uint32_t next_insn = GetInsn32(literal_offset + 4u);
1986 // LDR (register) with correct base_reg, S=1 and option=011 (LDR Wt, [Xn, Xm, LSL #2]).
1987 CheckValidReg((next_insn >> 12) & 0xfu); // Check destination register.
1988 const uint32_t base_reg = BakerReadBarrierFirstRegField::Decode(encoded_data);
1989 CHECK_EQ(next_insn & 0xffff0ff0u, 0xf8500020u | (base_reg << 16));
1990 CheckValidReg(next_insn & 0xf); // Check index register
1991 break;
1992 }
1993 case BakerReadBarrierKind::kGcRoot: {
1994 BakerReadBarrierWidth width = BakerReadBarrierWidthField::Decode(encoded_data);
1995 if (width == BakerReadBarrierWidth::kWide) {
1996 DCHECK_GE(literal_offset, 4u);
1997 uint32_t prev_insn = GetInsn32(literal_offset - 4u);
Vladimir Markof28be432018-08-14 12:20:51 +00001998 // LDR (immediate), encoding T3, with correct root_reg.
Vladimir Markoca1e0382018-04-11 09:58:41 +00001999 const uint32_t root_reg = BakerReadBarrierFirstRegField::Decode(encoded_data);
Vladimir Markof28be432018-08-14 12:20:51 +00002000 CHECK_EQ(prev_insn & 0xfff0f000u, 0xf8d00000u | (root_reg << 12));
Vladimir Markoca1e0382018-04-11 09:58:41 +00002001 } else {
2002 DCHECK_GE(literal_offset, 2u);
2003 uint32_t prev_insn = GetInsn16(literal_offset - 2u);
2004 // LDR (immediate), encoding T1, with correct root_reg.
2005 const uint32_t root_reg = BakerReadBarrierFirstRegField::Decode(encoded_data);
2006 CHECK_EQ(prev_insn & 0xf807u, 0x6800u | root_reg);
2007 }
2008 break;
2009 }
Vladimir Markod887ed82018-08-14 13:52:12 +00002010 case BakerReadBarrierKind::kUnsafeCas: {
2011 DCHECK_GE(literal_offset, 4u);
2012 uint32_t prev_insn = GetInsn32(literal_offset - 4u);
2013 // ADD (register), encoding T3, with correct root_reg.
2014 const uint32_t root_reg = BakerReadBarrierFirstRegField::Decode(encoded_data);
2015 CHECK_EQ(prev_insn & 0xfff0fff0u, 0xeb000000u | (root_reg << 8));
2016 break;
2017 }
Vladimir Markoca1e0382018-04-11 09:58:41 +00002018 default:
2019 LOG(FATAL) << "Unexpected kind: " << static_cast<uint32_t>(kind);
2020 UNREACHABLE();
2021 }
2022 }
2023 }
Scott Wakelingfe885462016-09-22 10:24:38 +01002024}
2025
2026void CodeGeneratorARMVIXL::SetupBlockedRegisters() const {
Scott Wakelingfe885462016-09-22 10:24:38 +01002027 // Stack register, LR and PC are always reserved.
2028 blocked_core_registers_[SP] = true;
2029 blocked_core_registers_[LR] = true;
2030 blocked_core_registers_[PC] = true;
2031
Roland Levillain6d729a72017-06-30 18:34:01 +01002032 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2033 // Reserve marking register.
2034 blocked_core_registers_[MR] = true;
2035 }
2036
Scott Wakelingfe885462016-09-22 10:24:38 +01002037 // Reserve thread register.
2038 blocked_core_registers_[TR] = true;
2039
2040 // Reserve temp register.
2041 blocked_core_registers_[IP] = true;
2042
2043 if (GetGraph()->IsDebuggable()) {
2044 // Stubs do not save callee-save floating point registers. If the graph
2045 // is debuggable, we need to deal with these registers differently. For
2046 // now, just block them.
2047 for (uint32_t i = kFpuCalleeSaves.GetFirstSRegister().GetCode();
2048 i <= kFpuCalleeSaves.GetLastSRegister().GetCode();
2049 ++i) {
2050 blocked_fpu_registers_[i] = true;
2051 }
2052 }
Scott Wakelingfe885462016-09-22 10:24:38 +01002053}
2054
Scott Wakelingfe885462016-09-22 10:24:38 +01002055InstructionCodeGeneratorARMVIXL::InstructionCodeGeneratorARMVIXL(HGraph* graph,
2056 CodeGeneratorARMVIXL* codegen)
2057 : InstructionCodeGenerator(graph, codegen),
2058 assembler_(codegen->GetAssembler()),
2059 codegen_(codegen) {}
2060
2061void CodeGeneratorARMVIXL::ComputeSpillMask() {
2062 core_spill_mask_ = allocated_registers_.GetCoreRegisters() & core_callee_save_mask_;
2063 DCHECK_NE(core_spill_mask_, 0u) << "At least the return address register must be saved";
2064 // There is no easy instruction to restore just the PC on thumb2. We spill and
2065 // restore another arbitrary register.
2066 core_spill_mask_ |= (1 << kCoreAlwaysSpillRegister.GetCode());
2067 fpu_spill_mask_ = allocated_registers_.GetFloatingPointRegisters() & fpu_callee_save_mask_;
2068 // We use vpush and vpop for saving and restoring floating point registers, which take
2069 // a SRegister and the number of registers to save/restore after that SRegister. We
2070 // therefore update the `fpu_spill_mask_` to also contain those registers not allocated,
2071 // but in the range.
2072 if (fpu_spill_mask_ != 0) {
2073 uint32_t least_significant_bit = LeastSignificantBit(fpu_spill_mask_);
2074 uint32_t most_significant_bit = MostSignificantBit(fpu_spill_mask_);
2075 for (uint32_t i = least_significant_bit + 1 ; i < most_significant_bit; ++i) {
2076 fpu_spill_mask_ |= (1 << i);
2077 }
2078 }
2079}
2080
2081void CodeGeneratorARMVIXL::GenerateFrameEntry() {
2082 bool skip_overflow_check =
2083 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kArm);
2084 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
2085 __ Bind(&frame_entry_label_);
2086
Nicolas Geoffray8d728322018-01-18 22:44:32 +00002087 if (GetCompilerOptions().CountHotnessInCompiledCode()) {
2088 UseScratchRegisterScope temps(GetVIXLAssembler());
2089 vixl32::Register temp = temps.Acquire();
2090 __ Ldrh(temp, MemOperand(kMethodRegister, ArtMethod::HotnessCountOffset().Int32Value()));
2091 __ Add(temp, temp, 1);
2092 __ Strh(temp, MemOperand(kMethodRegister, ArtMethod::HotnessCountOffset().Int32Value()));
2093 }
2094
Scott Wakelingfe885462016-09-22 10:24:38 +01002095 if (HasEmptyFrame()) {
2096 return;
2097 }
2098
Scott Wakelingfe885462016-09-22 10:24:38 +01002099 if (!skip_overflow_check) {
xueliang.zhong10049552018-01-31 17:10:36 +00002100 // Using r4 instead of IP saves 2 bytes.
Nicolas Geoffray1a4f3ca2018-01-25 14:07:15 +00002101 UseScratchRegisterScope temps(GetVIXLAssembler());
xueliang.zhong10049552018-01-31 17:10:36 +00002102 vixl32::Register temp;
2103 // TODO: Remove this check when R4 is made a callee-save register
2104 // in ART compiled code (b/72801708). Currently we need to make
2105 // sure r4 is not blocked, e.g. in special purpose
2106 // TestCodeGeneratorARMVIXL; also asserting that r4 is available
2107 // here.
2108 if (!blocked_core_registers_[R4]) {
2109 for (vixl32::Register reg : kParameterCoreRegistersVIXL) {
2110 DCHECK(!reg.Is(r4));
2111 }
2112 DCHECK(!kCoreCalleeSaves.Includes(r4));
2113 temp = r4;
2114 } else {
2115 temp = temps.Acquire();
2116 }
Vladimir Marko33bff252017-11-01 14:35:42 +00002117 __ Sub(temp, sp, Operand::From(GetStackOverflowReservedBytes(InstructionSet::kArm)));
Scott Wakelingfe885462016-09-22 10:24:38 +01002118 // The load must immediately precede RecordPcInfo.
Artem Serov0fb37192016-12-06 18:13:40 +00002119 ExactAssemblyScope aas(GetVIXLAssembler(),
2120 vixl32::kMaxInstructionSizeInBytes,
2121 CodeBufferCheckScope::kMaximumSize);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002122 __ ldr(temp, MemOperand(temp));
2123 RecordPcInfo(nullptr, 0);
Scott Wakelingfe885462016-09-22 10:24:38 +01002124 }
2125
2126 __ Push(RegisterList(core_spill_mask_));
2127 GetAssembler()->cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(core_spill_mask_));
2128 GetAssembler()->cfi().RelOffsetForMany(DWARFReg(kMethodRegister),
2129 0,
2130 core_spill_mask_,
2131 kArmWordSize);
2132 if (fpu_spill_mask_ != 0) {
2133 uint32_t first = LeastSignificantBit(fpu_spill_mask_);
2134
2135 // Check that list is contiguous.
2136 DCHECK_EQ(fpu_spill_mask_ >> CTZ(fpu_spill_mask_), ~0u >> (32 - POPCOUNT(fpu_spill_mask_)));
2137
2138 __ Vpush(SRegisterList(vixl32::SRegister(first), POPCOUNT(fpu_spill_mask_)));
2139 GetAssembler()->cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(fpu_spill_mask_));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002140 GetAssembler()->cfi().RelOffsetForMany(DWARFReg(s0), 0, fpu_spill_mask_, kArmWordSize);
Scott Wakelingfe885462016-09-22 10:24:38 +01002141 }
Scott Wakelingbffdc702016-12-07 17:46:03 +00002142
Scott Wakelingfe885462016-09-22 10:24:38 +01002143 int adjust = GetFrameSize() - FrameEntrySpillSize();
2144 __ Sub(sp, sp, adjust);
2145 GetAssembler()->cfi().AdjustCFAOffset(adjust);
Scott Wakelingbffdc702016-12-07 17:46:03 +00002146
2147 // Save the current method if we need it. Note that we do not
2148 // do this in HCurrentMethod, as the instruction might have been removed
2149 // in the SSA graph.
2150 if (RequiresCurrentMethod()) {
2151 GetAssembler()->StoreToOffset(kStoreWord, kMethodRegister, sp, 0);
2152 }
Nicolas Geoffrayf7893532017-06-15 12:34:36 +01002153
2154 if (GetGraph()->HasShouldDeoptimizeFlag()) {
2155 UseScratchRegisterScope temps(GetVIXLAssembler());
2156 vixl32::Register temp = temps.Acquire();
2157 // Initialize should_deoptimize flag to 0.
2158 __ Mov(temp, 0);
2159 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, GetStackOffsetOfShouldDeoptimizeFlag());
2160 }
Roland Levillain5daa4952017-07-03 17:23:56 +01002161
2162 MaybeGenerateMarkingRegisterCheck(/* code */ 1);
Scott Wakelingfe885462016-09-22 10:24:38 +01002163}
2164
2165void CodeGeneratorARMVIXL::GenerateFrameExit() {
2166 if (HasEmptyFrame()) {
2167 __ Bx(lr);
2168 return;
2169 }
2170 GetAssembler()->cfi().RememberState();
2171 int adjust = GetFrameSize() - FrameEntrySpillSize();
2172 __ Add(sp, sp, adjust);
2173 GetAssembler()->cfi().AdjustCFAOffset(-adjust);
2174 if (fpu_spill_mask_ != 0) {
2175 uint32_t first = LeastSignificantBit(fpu_spill_mask_);
2176
2177 // Check that list is contiguous.
2178 DCHECK_EQ(fpu_spill_mask_ >> CTZ(fpu_spill_mask_), ~0u >> (32 - POPCOUNT(fpu_spill_mask_)));
2179
2180 __ Vpop(SRegisterList(vixl32::SRegister(first), POPCOUNT(fpu_spill_mask_)));
2181 GetAssembler()->cfi().AdjustCFAOffset(
2182 -static_cast<int>(kArmWordSize) * POPCOUNT(fpu_spill_mask_));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002183 GetAssembler()->cfi().RestoreMany(DWARFReg(vixl32::SRegister(0)), fpu_spill_mask_);
Scott Wakelingfe885462016-09-22 10:24:38 +01002184 }
2185 // Pop LR into PC to return.
2186 DCHECK_NE(core_spill_mask_ & (1 << kLrCode), 0U);
2187 uint32_t pop_mask = (core_spill_mask_ & (~(1 << kLrCode))) | 1 << kPcCode;
2188 __ Pop(RegisterList(pop_mask));
2189 GetAssembler()->cfi().RestoreState();
2190 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
2191}
2192
2193void CodeGeneratorARMVIXL::Bind(HBasicBlock* block) {
2194 __ Bind(GetLabelOf(block));
2195}
2196
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002197Location InvokeDexCallingConventionVisitorARMVIXL::GetNextLocation(DataType::Type type) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002198 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002199 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002200 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002201 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002202 case DataType::Type::kInt8:
2203 case DataType::Type::kUint16:
2204 case DataType::Type::kInt16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002205 case DataType::Type::kInt32: {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002206 uint32_t index = gp_index_++;
2207 uint32_t stack_index = stack_index_++;
2208 if (index < calling_convention.GetNumberOfRegisters()) {
2209 return LocationFrom(calling_convention.GetRegisterAt(index));
2210 } else {
2211 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
2212 }
2213 }
2214
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002215 case DataType::Type::kInt64: {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002216 uint32_t index = gp_index_;
2217 uint32_t stack_index = stack_index_;
2218 gp_index_ += 2;
2219 stack_index_ += 2;
2220 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
2221 if (calling_convention.GetRegisterAt(index).Is(r1)) {
2222 // Skip R1, and use R2_R3 instead.
2223 gp_index_++;
2224 index++;
2225 }
2226 }
2227 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
2228 DCHECK_EQ(calling_convention.GetRegisterAt(index).GetCode() + 1,
2229 calling_convention.GetRegisterAt(index + 1).GetCode());
2230
2231 return LocationFrom(calling_convention.GetRegisterAt(index),
2232 calling_convention.GetRegisterAt(index + 1));
2233 } else {
2234 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
2235 }
2236 }
2237
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002238 case DataType::Type::kFloat32: {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002239 uint32_t stack_index = stack_index_++;
2240 if (float_index_ % 2 == 0) {
2241 float_index_ = std::max(double_index_, float_index_);
2242 }
2243 if (float_index_ < calling_convention.GetNumberOfFpuRegisters()) {
2244 return LocationFrom(calling_convention.GetFpuRegisterAt(float_index_++));
2245 } else {
2246 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
2247 }
2248 }
2249
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002250 case DataType::Type::kFloat64: {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002251 double_index_ = std::max(double_index_, RoundUp(float_index_, 2));
2252 uint32_t stack_index = stack_index_;
2253 stack_index_ += 2;
2254 if (double_index_ + 1 < calling_convention.GetNumberOfFpuRegisters()) {
2255 uint32_t index = double_index_;
2256 double_index_ += 2;
2257 Location result = LocationFrom(
2258 calling_convention.GetFpuRegisterAt(index),
2259 calling_convention.GetFpuRegisterAt(index + 1));
2260 DCHECK(ExpectedPairLayout(result));
2261 return result;
2262 } else {
2263 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
2264 }
2265 }
2266
Aart Bik66c158e2018-01-31 12:55:04 -08002267 case DataType::Type::kUint32:
2268 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002269 case DataType::Type::kVoid:
Artem Serovd4cc5b22016-11-04 11:19:09 +00002270 LOG(FATAL) << "Unexpected parameter type " << type;
Elliott Hughesc1896c92018-11-29 11:33:18 -08002271 UNREACHABLE();
Artem Serovd4cc5b22016-11-04 11:19:09 +00002272 }
2273 return Location::NoLocation();
2274}
2275
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002276Location InvokeDexCallingConventionVisitorARMVIXL::GetReturnLocation(DataType::Type type) const {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002277 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002278 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002279 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002280 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002281 case DataType::Type::kInt8:
2282 case DataType::Type::kUint16:
2283 case DataType::Type::kInt16:
Aart Bik66c158e2018-01-31 12:55:04 -08002284 case DataType::Type::kUint32:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002285 case DataType::Type::kInt32: {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002286 return LocationFrom(r0);
2287 }
2288
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002289 case DataType::Type::kFloat32: {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002290 return LocationFrom(s0);
2291 }
2292
Aart Bik66c158e2018-01-31 12:55:04 -08002293 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002294 case DataType::Type::kInt64: {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002295 return LocationFrom(r0, r1);
2296 }
2297
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002298 case DataType::Type::kFloat64: {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002299 return LocationFrom(s0, s1);
2300 }
2301
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002302 case DataType::Type::kVoid:
Artem Serovd4cc5b22016-11-04 11:19:09 +00002303 return Location::NoLocation();
2304 }
2305
2306 UNREACHABLE();
2307}
2308
2309Location InvokeDexCallingConventionVisitorARMVIXL::GetMethodLocation() const {
2310 return LocationFrom(kMethodRegister);
2311}
2312
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002313void CodeGeneratorARMVIXL::Move32(Location destination, Location source) {
2314 if (source.Equals(destination)) {
2315 return;
2316 }
2317 if (destination.IsRegister()) {
2318 if (source.IsRegister()) {
2319 __ Mov(RegisterFrom(destination), RegisterFrom(source));
2320 } else if (source.IsFpuRegister()) {
2321 __ Vmov(RegisterFrom(destination), SRegisterFrom(source));
2322 } else {
2323 GetAssembler()->LoadFromOffset(kLoadWord,
2324 RegisterFrom(destination),
2325 sp,
2326 source.GetStackIndex());
2327 }
2328 } else if (destination.IsFpuRegister()) {
2329 if (source.IsRegister()) {
2330 __ Vmov(SRegisterFrom(destination), RegisterFrom(source));
2331 } else if (source.IsFpuRegister()) {
2332 __ Vmov(SRegisterFrom(destination), SRegisterFrom(source));
2333 } else {
2334 GetAssembler()->LoadSFromOffset(SRegisterFrom(destination), sp, source.GetStackIndex());
2335 }
2336 } else {
2337 DCHECK(destination.IsStackSlot()) << destination;
2338 if (source.IsRegister()) {
2339 GetAssembler()->StoreToOffset(kStoreWord,
2340 RegisterFrom(source),
2341 sp,
2342 destination.GetStackIndex());
2343 } else if (source.IsFpuRegister()) {
2344 GetAssembler()->StoreSToOffset(SRegisterFrom(source), sp, destination.GetStackIndex());
2345 } else {
2346 DCHECK(source.IsStackSlot()) << source;
2347 UseScratchRegisterScope temps(GetVIXLAssembler());
2348 vixl32::Register temp = temps.Acquire();
2349 GetAssembler()->LoadFromOffset(kLoadWord, temp, sp, source.GetStackIndex());
2350 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
2351 }
2352 }
2353}
2354
Artem Serovcfbe9132016-10-14 15:58:56 +01002355void CodeGeneratorARMVIXL::MoveConstant(Location location, int32_t value) {
2356 DCHECK(location.IsRegister());
2357 __ Mov(RegisterFrom(location), value);
Scott Wakelingfe885462016-09-22 10:24:38 +01002358}
2359
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002360void CodeGeneratorARMVIXL::MoveLocation(Location dst, Location src, DataType::Type dst_type) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002361 // TODO(VIXL): Maybe refactor to have the 'move' implementation here and use it in
2362 // `ParallelMoveResolverARMVIXL::EmitMove`, as is done in the `arm64` backend.
Vladimir Markoca6fff82017-10-03 14:49:14 +01002363 HParallelMove move(GetGraph()->GetAllocator());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002364 move.AddMove(src, dst, dst_type, nullptr);
2365 GetMoveResolver()->EmitNativeCode(&move);
Scott Wakelingfe885462016-09-22 10:24:38 +01002366}
2367
Artem Serovcfbe9132016-10-14 15:58:56 +01002368void CodeGeneratorARMVIXL::AddLocationAsTemp(Location location, LocationSummary* locations) {
2369 if (location.IsRegister()) {
2370 locations->AddTemp(location);
2371 } else if (location.IsRegisterPair()) {
2372 locations->AddTemp(LocationFrom(LowRegisterFrom(location)));
2373 locations->AddTemp(LocationFrom(HighRegisterFrom(location)));
2374 } else {
2375 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
2376 }
Scott Wakelingfe885462016-09-22 10:24:38 +01002377}
2378
2379void CodeGeneratorARMVIXL::InvokeRuntime(QuickEntrypointEnum entrypoint,
2380 HInstruction* instruction,
2381 uint32_t dex_pc,
2382 SlowPathCode* slow_path) {
2383 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Alexandre Rames374ddf32016-11-04 10:40:49 +00002384 __ Ldr(lr, MemOperand(tr, GetThreadOffset<kArmPointerSize>(entrypoint).Int32Value()));
2385 // Ensure the pc position is recorded immediately after the `blx` instruction.
2386 // 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 +00002387 ExactAssemblyScope aas(GetVIXLAssembler(),
2388 vixl32::k16BitT32InstructionSizeInBytes,
2389 CodeBufferCheckScope::kExactSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00002390 __ blx(lr);
Scott Wakelingfe885462016-09-22 10:24:38 +01002391 if (EntrypointRequiresStackMap(entrypoint)) {
2392 RecordPcInfo(instruction, dex_pc, slow_path);
2393 }
2394}
2395
2396void CodeGeneratorARMVIXL::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
2397 HInstruction* instruction,
2398 SlowPathCode* slow_path) {
2399 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Alexandre Rames374ddf32016-11-04 10:40:49 +00002400 __ Ldr(lr, MemOperand(tr, entry_point_offset));
Scott Wakelingfe885462016-09-22 10:24:38 +01002401 __ Blx(lr);
2402}
2403
Scott Wakelingfe885462016-09-22 10:24:38 +01002404void InstructionCodeGeneratorARMVIXL::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Aart Bika8b8e9b2018-01-09 11:01:02 -08002405 if (successor->IsExitBlock()) {
2406 DCHECK(got->GetPrevious()->AlwaysThrows());
2407 return; // no code needed
2408 }
2409
Scott Wakelingfe885462016-09-22 10:24:38 +01002410 HBasicBlock* block = got->GetBlock();
2411 HInstruction* previous = got->GetPrevious();
2412 HLoopInformation* info = block->GetLoopInformation();
2413
2414 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray8d728322018-01-18 22:44:32 +00002415 if (codegen_->GetCompilerOptions().CountHotnessInCompiledCode()) {
2416 UseScratchRegisterScope temps(GetVIXLAssembler());
2417 vixl32::Register temp = temps.Acquire();
2418 __ Push(vixl32::Register(kMethodRegister));
2419 GetAssembler()->LoadFromOffset(kLoadWord, kMethodRegister, sp, kArmWordSize);
2420 __ Ldrh(temp, MemOperand(kMethodRegister, ArtMethod::HotnessCountOffset().Int32Value()));
2421 __ Add(temp, temp, 1);
2422 __ Strh(temp, MemOperand(kMethodRegister, ArtMethod::HotnessCountOffset().Int32Value()));
2423 __ Pop(vixl32::Register(kMethodRegister));
2424 }
Scott Wakelingfe885462016-09-22 10:24:38 +01002425 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2426 return;
2427 }
2428 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2429 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
Roland Levillain5daa4952017-07-03 17:23:56 +01002430 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 2);
Scott Wakelingfe885462016-09-22 10:24:38 +01002431 }
2432 if (!codegen_->GoesToNextBlock(block, successor)) {
2433 __ B(codegen_->GetLabelOf(successor));
2434 }
2435}
2436
2437void LocationsBuilderARMVIXL::VisitGoto(HGoto* got) {
2438 got->SetLocations(nullptr);
2439}
2440
2441void InstructionCodeGeneratorARMVIXL::VisitGoto(HGoto* got) {
2442 HandleGoto(got, got->GetSuccessor());
2443}
2444
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002445void LocationsBuilderARMVIXL::VisitTryBoundary(HTryBoundary* try_boundary) {
2446 try_boundary->SetLocations(nullptr);
2447}
2448
2449void InstructionCodeGeneratorARMVIXL::VisitTryBoundary(HTryBoundary* try_boundary) {
2450 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2451 if (!successor->IsExitBlock()) {
2452 HandleGoto(try_boundary, successor);
2453 }
2454}
2455
Scott Wakelingfe885462016-09-22 10:24:38 +01002456void LocationsBuilderARMVIXL::VisitExit(HExit* exit) {
2457 exit->SetLocations(nullptr);
2458}
2459
2460void InstructionCodeGeneratorARMVIXL::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
2461}
2462
Scott Wakelingfe885462016-09-22 10:24:38 +01002463void InstructionCodeGeneratorARMVIXL::GenerateCompareTestAndBranch(HCondition* condition,
Anton Kirilov23b752b2017-07-20 14:40:44 +01002464 vixl32::Label* true_target,
2465 vixl32::Label* false_target,
Anton Kirilovfd522532017-05-10 12:46:57 +01002466 bool is_far_target) {
Anton Kirilov23b752b2017-07-20 14:40:44 +01002467 if (true_target == false_target) {
2468 DCHECK(true_target != nullptr);
2469 __ B(true_target);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002470 return;
2471 }
2472
Anton Kirilov23b752b2017-07-20 14:40:44 +01002473 vixl32::Label* non_fallthrough_target;
2474 bool invert;
2475 bool emit_both_branches;
Scott Wakelingfe885462016-09-22 10:24:38 +01002476
Anton Kirilov23b752b2017-07-20 14:40:44 +01002477 if (true_target == nullptr) {
2478 // The true target is fallthrough.
2479 DCHECK(false_target != nullptr);
2480 non_fallthrough_target = false_target;
2481 invert = true;
2482 emit_both_branches = false;
2483 } else {
2484 non_fallthrough_target = true_target;
2485 invert = false;
2486 // Either the false target is fallthrough, or there is no fallthrough
2487 // and both branches must be emitted.
2488 emit_both_branches = (false_target != nullptr);
Scott Wakelingfe885462016-09-22 10:24:38 +01002489 }
2490
Anton Kirilov23b752b2017-07-20 14:40:44 +01002491 const auto cond = GenerateTest(condition, invert, codegen_);
2492
2493 __ B(cond.first, non_fallthrough_target, is_far_target);
2494
2495 if (emit_both_branches) {
2496 // No target falls through, we need to branch.
2497 __ B(false_target);
Scott Wakelingfe885462016-09-22 10:24:38 +01002498 }
2499}
2500
2501void InstructionCodeGeneratorARMVIXL::GenerateTestAndBranch(HInstruction* instruction,
2502 size_t condition_input_index,
2503 vixl32::Label* true_target,
xueliang.zhongf51bc622016-11-04 09:23:32 +00002504 vixl32::Label* false_target,
2505 bool far_target) {
Scott Wakelingfe885462016-09-22 10:24:38 +01002506 HInstruction* cond = instruction->InputAt(condition_input_index);
2507
2508 if (true_target == nullptr && false_target == nullptr) {
2509 // Nothing to do. The code always falls through.
2510 return;
2511 } else if (cond->IsIntConstant()) {
2512 // Constant condition, statically compared against "true" (integer value 1).
2513 if (cond->AsIntConstant()->IsTrue()) {
2514 if (true_target != nullptr) {
2515 __ B(true_target);
2516 }
2517 } else {
Anton Kirilov644032c2016-12-06 17:51:43 +00002518 DCHECK(cond->AsIntConstant()->IsFalse()) << Int32ConstantFrom(cond);
Scott Wakelingfe885462016-09-22 10:24:38 +01002519 if (false_target != nullptr) {
2520 __ B(false_target);
2521 }
2522 }
2523 return;
2524 }
2525
2526 // The following code generates these patterns:
2527 // (1) true_target == nullptr && false_target != nullptr
2528 // - opposite condition true => branch to false_target
2529 // (2) true_target != nullptr && false_target == nullptr
2530 // - condition true => branch to true_target
2531 // (3) true_target != nullptr && false_target != nullptr
2532 // - condition true => branch to true_target
2533 // - branch to false_target
2534 if (IsBooleanValueOrMaterializedCondition(cond)) {
2535 // Condition has been materialized, compare the output to 0.
2536 if (kIsDebugBuild) {
2537 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
2538 DCHECK(cond_val.IsRegister());
2539 }
2540 if (true_target == nullptr) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00002541 __ CompareAndBranchIfZero(InputRegisterAt(instruction, condition_input_index),
2542 false_target,
2543 far_target);
Scott Wakelingfe885462016-09-22 10:24:38 +01002544 } else {
xueliang.zhongf51bc622016-11-04 09:23:32 +00002545 __ CompareAndBranchIfNonZero(InputRegisterAt(instruction, condition_input_index),
2546 true_target,
2547 far_target);
Scott Wakelingfe885462016-09-22 10:24:38 +01002548 }
2549 } else {
2550 // Condition has not been materialized. Use its inputs as the comparison and
2551 // its condition as the branch condition.
2552 HCondition* condition = cond->AsCondition();
2553
2554 // If this is a long or FP comparison that has been folded into
2555 // the HCondition, generate the comparison directly.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002556 DataType::Type type = condition->InputAt(0)->GetType();
2557 if (type == DataType::Type::kInt64 || DataType::IsFloatingPointType(type)) {
Anton Kirilovfd522532017-05-10 12:46:57 +01002558 GenerateCompareTestAndBranch(condition, true_target, false_target, far_target);
Scott Wakelingfe885462016-09-22 10:24:38 +01002559 return;
2560 }
2561
Donghui Bai426b49c2016-11-08 14:55:38 +08002562 vixl32::Label* non_fallthrough_target;
2563 vixl32::Condition arm_cond = vixl32::Condition::None();
2564 const vixl32::Register left = InputRegisterAt(cond, 0);
2565 const Operand right = InputOperandAt(cond, 1);
2566
Scott Wakelingfe885462016-09-22 10:24:38 +01002567 if (true_target == nullptr) {
Donghui Bai426b49c2016-11-08 14:55:38 +08002568 arm_cond = ARMCondition(condition->GetOppositeCondition());
2569 non_fallthrough_target = false_target;
Scott Wakelingfe885462016-09-22 10:24:38 +01002570 } else {
Donghui Bai426b49c2016-11-08 14:55:38 +08002571 arm_cond = ARMCondition(condition->GetCondition());
2572 non_fallthrough_target = true_target;
2573 }
2574
2575 if (right.IsImmediate() && right.GetImmediate() == 0 && (arm_cond.Is(ne) || arm_cond.Is(eq))) {
2576 if (arm_cond.Is(eq)) {
Anton Kirilovfd522532017-05-10 12:46:57 +01002577 __ CompareAndBranchIfZero(left, non_fallthrough_target, far_target);
Donghui Bai426b49c2016-11-08 14:55:38 +08002578 } else {
2579 DCHECK(arm_cond.Is(ne));
Anton Kirilovfd522532017-05-10 12:46:57 +01002580 __ CompareAndBranchIfNonZero(left, non_fallthrough_target, far_target);
Donghui Bai426b49c2016-11-08 14:55:38 +08002581 }
2582 } else {
2583 __ Cmp(left, right);
Anton Kirilovfd522532017-05-10 12:46:57 +01002584 __ B(arm_cond, non_fallthrough_target, far_target);
Scott Wakelingfe885462016-09-22 10:24:38 +01002585 }
2586 }
2587
2588 // If neither branch falls through (case 3), the conditional branch to `true_target`
2589 // was already emitted (case 2) and we need to emit a jump to `false_target`.
2590 if (true_target != nullptr && false_target != nullptr) {
2591 __ B(false_target);
2592 }
2593}
2594
2595void LocationsBuilderARMVIXL::VisitIf(HIf* if_instr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002596 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(if_instr);
Scott Wakelingfe885462016-09-22 10:24:38 +01002597 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
2598 locations->SetInAt(0, Location::RequiresRegister());
2599 }
2600}
2601
2602void InstructionCodeGeneratorARMVIXL::VisitIf(HIf* if_instr) {
2603 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
2604 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002605 vixl32::Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
2606 nullptr : codegen_->GetLabelOf(true_successor);
2607 vixl32::Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
2608 nullptr : codegen_->GetLabelOf(false_successor);
Scott Wakelingfe885462016-09-22 10:24:38 +01002609 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
2610}
2611
Scott Wakelingc34dba72016-10-03 10:14:44 +01002612void LocationsBuilderARMVIXL::VisitDeoptimize(HDeoptimize* deoptimize) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002613 LocationSummary* locations = new (GetGraph()->GetAllocator())
Scott Wakelingc34dba72016-10-03 10:14:44 +01002614 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01002615 InvokeRuntimeCallingConventionARMVIXL calling_convention;
2616 RegisterSet caller_saves = RegisterSet::Empty();
2617 caller_saves.Add(LocationFrom(calling_convention.GetRegisterAt(0)));
2618 locations->SetCustomSlowPathCallerSaves(caller_saves);
Scott Wakelingc34dba72016-10-03 10:14:44 +01002619 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
2620 locations->SetInAt(0, Location::RequiresRegister());
2621 }
2622}
2623
2624void InstructionCodeGeneratorARMVIXL::VisitDeoptimize(HDeoptimize* deoptimize) {
2625 SlowPathCodeARMVIXL* slow_path =
2626 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARMVIXL>(deoptimize);
2627 GenerateTestAndBranch(deoptimize,
2628 /* condition_input_index */ 0,
2629 slow_path->GetEntryLabel(),
2630 /* false_target */ nullptr);
2631}
2632
Artem Serovd4cc5b22016-11-04 11:19:09 +00002633void LocationsBuilderARMVIXL::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002634 LocationSummary* locations = new (GetGraph()->GetAllocator())
Artem Serovd4cc5b22016-11-04 11:19:09 +00002635 LocationSummary(flag, LocationSummary::kNoCall);
2636 locations->SetOut(Location::RequiresRegister());
2637}
2638
2639void InstructionCodeGeneratorARMVIXL::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
2640 GetAssembler()->LoadFromOffset(kLoadWord,
2641 OutputRegister(flag),
2642 sp,
2643 codegen_->GetStackOffsetOfShouldDeoptimizeFlag());
2644}
2645
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002646void LocationsBuilderARMVIXL::VisitSelect(HSelect* select) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002647 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(select);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002648 const bool is_floating_point = DataType::IsFloatingPointType(select->GetType());
Donghui Bai426b49c2016-11-08 14:55:38 +08002649
2650 if (is_floating_point) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002651 locations->SetInAt(0, Location::RequiresFpuRegister());
Donghui Bai426b49c2016-11-08 14:55:38 +08002652 locations->SetInAt(1, Location::FpuRegisterOrConstant(select->GetTrueValue()));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002653 } else {
2654 locations->SetInAt(0, Location::RequiresRegister());
Donghui Bai426b49c2016-11-08 14:55:38 +08002655 locations->SetInAt(1, Arm8BitEncodableConstantOrRegister(select->GetTrueValue()));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002656 }
Donghui Bai426b49c2016-11-08 14:55:38 +08002657
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002658 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
Donghui Bai426b49c2016-11-08 14:55:38 +08002659 locations->SetInAt(2, Location::RegisterOrConstant(select->GetCondition()));
2660 // The code generator handles overlap with the values, but not with the condition.
2661 locations->SetOut(Location::SameAsFirstInput());
2662 } else if (is_floating_point) {
2663 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2664 } else {
2665 if (!locations->InAt(1).IsConstant()) {
2666 locations->SetInAt(0, Arm8BitEncodableConstantOrRegister(select->GetFalseValue()));
2667 }
2668
2669 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002670 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002671}
2672
2673void InstructionCodeGeneratorARMVIXL::VisitSelect(HSelect* select) {
Donghui Bai426b49c2016-11-08 14:55:38 +08002674 HInstruction* const condition = select->GetCondition();
2675 const LocationSummary* const locations = select->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002676 const DataType::Type type = select->GetType();
Donghui Bai426b49c2016-11-08 14:55:38 +08002677 const Location first = locations->InAt(0);
2678 const Location out = locations->Out();
2679 const Location second = locations->InAt(1);
Nicolas Geoffray7b05c5f2018-09-21 11:31:38 +01002680
2681 // In the unlucky case the output of this instruction overlaps
2682 // with an input of an "emitted-at-use-site" condition, and
2683 // the output of this instruction is not one of its inputs, we'll
2684 // need to fallback to branches instead of conditional ARM instructions.
2685 bool output_overlaps_with_condition_inputs =
2686 !IsBooleanValueOrMaterializedCondition(condition) &&
2687 !out.Equals(first) &&
2688 !out.Equals(second) &&
2689 (condition->GetLocations()->InAt(0).Equals(out) ||
2690 condition->GetLocations()->InAt(1).Equals(out));
2691 DCHECK(!output_overlaps_with_condition_inputs || condition->IsCondition());
Donghui Bai426b49c2016-11-08 14:55:38 +08002692 Location src;
2693
2694 if (condition->IsIntConstant()) {
2695 if (condition->AsIntConstant()->IsFalse()) {
2696 src = first;
2697 } else {
2698 src = second;
2699 }
2700
2701 codegen_->MoveLocation(out, src, type);
2702 return;
2703 }
2704
Nicolas Geoffray7b05c5f2018-09-21 11:31:38 +01002705 if (!DataType::IsFloatingPointType(type) && !output_overlaps_with_condition_inputs) {
Donghui Bai426b49c2016-11-08 14:55:38 +08002706 bool invert = false;
2707
2708 if (out.Equals(second)) {
2709 src = first;
2710 invert = true;
2711 } else if (out.Equals(first)) {
2712 src = second;
2713 } else if (second.IsConstant()) {
2714 DCHECK(CanEncodeConstantAs8BitImmediate(second.GetConstant()));
2715 src = second;
2716 } else if (first.IsConstant()) {
2717 DCHECK(CanEncodeConstantAs8BitImmediate(first.GetConstant()));
2718 src = first;
2719 invert = true;
2720 } else {
2721 src = second;
2722 }
2723
2724 if (CanGenerateConditionalMove(out, src)) {
2725 if (!out.Equals(first) && !out.Equals(second)) {
2726 codegen_->MoveLocation(out, src.Equals(first) ? second : first, type);
2727 }
2728
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002729 std::pair<vixl32::Condition, vixl32::Condition> cond(eq, ne);
2730
2731 if (IsBooleanValueOrMaterializedCondition(condition)) {
2732 __ Cmp(InputRegisterAt(select, 2), 0);
2733 cond = invert ? std::make_pair(eq, ne) : std::make_pair(ne, eq);
2734 } else {
2735 cond = GenerateTest(condition->AsCondition(), invert, codegen_);
2736 }
2737
Donghui Bai426b49c2016-11-08 14:55:38 +08002738 const size_t instr_count = out.IsRegisterPair() ? 4 : 2;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002739 // We use the scope because of the IT block that follows.
Donghui Bai426b49c2016-11-08 14:55:38 +08002740 ExactAssemblyScope guard(GetVIXLAssembler(),
2741 instr_count * vixl32::k16BitT32InstructionSizeInBytes,
2742 CodeBufferCheckScope::kExactSize);
2743
2744 if (out.IsRegister()) {
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002745 __ it(cond.first);
2746 __ mov(cond.first, RegisterFrom(out), OperandFrom(src, type));
Donghui Bai426b49c2016-11-08 14:55:38 +08002747 } else {
2748 DCHECK(out.IsRegisterPair());
2749
2750 Operand operand_high(0);
2751 Operand operand_low(0);
2752
2753 if (src.IsConstant()) {
2754 const int64_t value = Int64ConstantFrom(src);
2755
2756 operand_high = High32Bits(value);
2757 operand_low = Low32Bits(value);
2758 } else {
2759 DCHECK(src.IsRegisterPair());
2760 operand_high = HighRegisterFrom(src);
2761 operand_low = LowRegisterFrom(src);
2762 }
2763
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002764 __ it(cond.first);
2765 __ mov(cond.first, LowRegisterFrom(out), operand_low);
2766 __ it(cond.first);
2767 __ mov(cond.first, HighRegisterFrom(out), operand_high);
Donghui Bai426b49c2016-11-08 14:55:38 +08002768 }
2769
2770 return;
2771 }
2772 }
2773
2774 vixl32::Label* false_target = nullptr;
2775 vixl32::Label* true_target = nullptr;
2776 vixl32::Label select_end;
Nicolas Geoffray7b05c5f2018-09-21 11:31:38 +01002777 vixl32::Label other_case;
Donghui Bai426b49c2016-11-08 14:55:38 +08002778 vixl32::Label* const target = codegen_->GetFinalLabel(select, &select_end);
2779
2780 if (out.Equals(second)) {
2781 true_target = target;
2782 src = first;
2783 } else {
2784 false_target = target;
2785 src = second;
2786
2787 if (!out.Equals(first)) {
Nicolas Geoffray7b05c5f2018-09-21 11:31:38 +01002788 if (output_overlaps_with_condition_inputs) {
2789 false_target = &other_case;
2790 } else {
2791 codegen_->MoveLocation(out, first, type);
2792 }
Donghui Bai426b49c2016-11-08 14:55:38 +08002793 }
2794 }
2795
2796 GenerateTestAndBranch(select, 2, true_target, false_target, /* far_target */ false);
2797 codegen_->MoveLocation(out, src, type);
Nicolas Geoffray7b05c5f2018-09-21 11:31:38 +01002798 if (output_overlaps_with_condition_inputs) {
2799 __ B(target);
2800 __ Bind(&other_case);
2801 codegen_->MoveLocation(out, first, type);
2802 }
Donghui Bai426b49c2016-11-08 14:55:38 +08002803
2804 if (select_end.IsReferenced()) {
2805 __ Bind(&select_end);
2806 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002807}
2808
Artem Serov551b28f2016-10-18 19:11:30 +01002809void LocationsBuilderARMVIXL::VisitNativeDebugInfo(HNativeDebugInfo* info) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002810 new (GetGraph()->GetAllocator()) LocationSummary(info);
Artem Serov551b28f2016-10-18 19:11:30 +01002811}
2812
2813void InstructionCodeGeneratorARMVIXL::VisitNativeDebugInfo(HNativeDebugInfo*) {
2814 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
2815}
2816
Scott Wakelingfe885462016-09-22 10:24:38 +01002817void CodeGeneratorARMVIXL::GenerateNop() {
2818 __ Nop();
2819}
2820
Anton Kirilov5601d4e2017-05-11 19:33:50 +01002821// `temp` is an extra temporary register that is used for some conditions;
2822// callers may not specify it, in which case the method will use a scratch
2823// register instead.
2824void CodeGeneratorARMVIXL::GenerateConditionWithZero(IfCondition condition,
2825 vixl32::Register out,
2826 vixl32::Register in,
2827 vixl32::Register temp) {
2828 switch (condition) {
2829 case kCondEQ:
2830 // x <= 0 iff x == 0 when the comparison is unsigned.
2831 case kCondBE:
2832 if (!temp.IsValid() || (out.IsLow() && !out.Is(in))) {
2833 temp = out;
2834 }
2835
2836 // Avoid 32-bit instructions if possible; note that `in` and `temp` must be
2837 // different as well.
2838 if (in.IsLow() && temp.IsLow() && !in.Is(temp)) {
2839 // temp = - in; only 0 sets the carry flag.
2840 __ Rsbs(temp, in, 0);
2841
2842 if (out.Is(in)) {
2843 std::swap(in, temp);
2844 }
2845
2846 // out = - in + in + carry = carry
2847 __ Adc(out, temp, in);
2848 } else {
2849 // If `in` is 0, then it has 32 leading zeros, and less than that otherwise.
2850 __ Clz(out, in);
2851 // Any number less than 32 logically shifted right by 5 bits results in 0;
2852 // the same operation on 32 yields 1.
2853 __ Lsr(out, out, 5);
2854 }
2855
2856 break;
2857 case kCondNE:
2858 // x > 0 iff x != 0 when the comparison is unsigned.
2859 case kCondA: {
2860 UseScratchRegisterScope temps(GetVIXLAssembler());
2861
2862 if (out.Is(in)) {
2863 if (!temp.IsValid() || in.Is(temp)) {
2864 temp = temps.Acquire();
2865 }
2866 } else if (!temp.IsValid() || !temp.IsLow()) {
2867 temp = out;
2868 }
2869
2870 // temp = in - 1; only 0 does not set the carry flag.
2871 __ Subs(temp, in, 1);
2872 // out = in + ~temp + carry = in + (-(in - 1) - 1) + carry = in - in + 1 - 1 + carry = carry
2873 __ Sbc(out, in, temp);
2874 break;
2875 }
2876 case kCondGE:
2877 __ Mvn(out, in);
2878 in = out;
2879 FALLTHROUGH_INTENDED;
2880 case kCondLT:
2881 // We only care about the sign bit.
2882 __ Lsr(out, in, 31);
2883 break;
2884 case kCondAE:
2885 // Trivially true.
2886 __ Mov(out, 1);
2887 break;
2888 case kCondB:
2889 // Trivially false.
2890 __ Mov(out, 0);
2891 break;
2892 default:
2893 LOG(FATAL) << "Unexpected condition " << condition;
2894 UNREACHABLE();
2895 }
2896}
2897
Scott Wakelingfe885462016-09-22 10:24:38 +01002898void LocationsBuilderARMVIXL::HandleCondition(HCondition* cond) {
2899 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002900 new (GetGraph()->GetAllocator()) LocationSummary(cond, LocationSummary::kNoCall);
Nicolas Geoffray7b05c5f2018-09-21 11:31:38 +01002901 const DataType::Type type = cond->InputAt(0)->GetType();
2902 if (DataType::IsFloatingPointType(type)) {
2903 locations->SetInAt(0, Location::RequiresFpuRegister());
2904 locations->SetInAt(1, ArithmeticZeroOrFpuRegister(cond->InputAt(1)));
2905 } else {
2906 locations->SetInAt(0, Location::RequiresRegister());
2907 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
2908 }
2909 if (!cond->IsEmittedAtUseSite()) {
2910 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Scott Wakelingfe885462016-09-22 10:24:38 +01002911 }
2912}
2913
2914void InstructionCodeGeneratorARMVIXL::HandleCondition(HCondition* cond) {
2915 if (cond->IsEmittedAtUseSite()) {
2916 return;
2917 }
2918
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002919 const DataType::Type type = cond->GetLeft()->GetType();
Scott Wakelingfe885462016-09-22 10:24:38 +01002920
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002921 if (DataType::IsFloatingPointType(type)) {
Anton Kirilov5601d4e2017-05-11 19:33:50 +01002922 GenerateConditionGeneric(cond, codegen_);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002923 return;
Scott Wakelingfe885462016-09-22 10:24:38 +01002924 }
2925
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002926 DCHECK(DataType::IsIntegralType(type) || type == DataType::Type::kReference) << type;
Scott Wakelingfe885462016-09-22 10:24:38 +01002927
Anton Kirilov5601d4e2017-05-11 19:33:50 +01002928 const IfCondition condition = cond->GetCondition();
Scott Wakelingfe885462016-09-22 10:24:38 +01002929
Anton Kirilov5601d4e2017-05-11 19:33:50 +01002930 // A condition with only one boolean input, or two boolean inputs without being equality or
2931 // inequality results from transformations done by the instruction simplifier, and is handled
2932 // as a regular condition with integral inputs.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002933 if (type == DataType::Type::kBool &&
2934 cond->GetRight()->GetType() == DataType::Type::kBool &&
Anton Kirilov5601d4e2017-05-11 19:33:50 +01002935 (condition == kCondEQ || condition == kCondNE)) {
2936 vixl32::Register left = InputRegisterAt(cond, 0);
2937 const vixl32::Register out = OutputRegister(cond);
2938 const Location right_loc = cond->GetLocations()->InAt(1);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002939
Anton Kirilov5601d4e2017-05-11 19:33:50 +01002940 // The constant case is handled by the instruction simplifier.
2941 DCHECK(!right_loc.IsConstant());
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002942
Anton Kirilov5601d4e2017-05-11 19:33:50 +01002943 vixl32::Register right = RegisterFrom(right_loc);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002944
Anton Kirilov5601d4e2017-05-11 19:33:50 +01002945 // Avoid 32-bit instructions if possible.
2946 if (out.Is(right)) {
2947 std::swap(left, right);
2948 }
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002949
Anton Kirilov5601d4e2017-05-11 19:33:50 +01002950 __ Eor(out, left, right);
2951
2952 if (condition == kCondEQ) {
2953 __ Eor(out, out, 1);
2954 }
2955
2956 return;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002957 }
Anton Kirilov6f644202017-02-27 18:29:45 +00002958
Anton Kirilov5601d4e2017-05-11 19:33:50 +01002959 GenerateConditionIntegralOrNonPrimitive(cond, codegen_);
Scott Wakelingfe885462016-09-22 10:24:38 +01002960}
2961
2962void LocationsBuilderARMVIXL::VisitEqual(HEqual* comp) {
2963 HandleCondition(comp);
2964}
2965
2966void InstructionCodeGeneratorARMVIXL::VisitEqual(HEqual* comp) {
2967 HandleCondition(comp);
2968}
2969
2970void LocationsBuilderARMVIXL::VisitNotEqual(HNotEqual* comp) {
2971 HandleCondition(comp);
2972}
2973
2974void InstructionCodeGeneratorARMVIXL::VisitNotEqual(HNotEqual* comp) {
2975 HandleCondition(comp);
2976}
2977
2978void LocationsBuilderARMVIXL::VisitLessThan(HLessThan* comp) {
2979 HandleCondition(comp);
2980}
2981
2982void InstructionCodeGeneratorARMVIXL::VisitLessThan(HLessThan* comp) {
2983 HandleCondition(comp);
2984}
2985
2986void LocationsBuilderARMVIXL::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
2987 HandleCondition(comp);
2988}
2989
2990void InstructionCodeGeneratorARMVIXL::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
2991 HandleCondition(comp);
2992}
2993
2994void LocationsBuilderARMVIXL::VisitGreaterThan(HGreaterThan* comp) {
2995 HandleCondition(comp);
2996}
2997
2998void InstructionCodeGeneratorARMVIXL::VisitGreaterThan(HGreaterThan* comp) {
2999 HandleCondition(comp);
3000}
3001
3002void LocationsBuilderARMVIXL::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
3003 HandleCondition(comp);
3004}
3005
3006void InstructionCodeGeneratorARMVIXL::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
3007 HandleCondition(comp);
3008}
3009
3010void LocationsBuilderARMVIXL::VisitBelow(HBelow* comp) {
3011 HandleCondition(comp);
3012}
3013
3014void InstructionCodeGeneratorARMVIXL::VisitBelow(HBelow* comp) {
3015 HandleCondition(comp);
3016}
3017
3018void LocationsBuilderARMVIXL::VisitBelowOrEqual(HBelowOrEqual* comp) {
3019 HandleCondition(comp);
3020}
3021
3022void InstructionCodeGeneratorARMVIXL::VisitBelowOrEqual(HBelowOrEqual* comp) {
3023 HandleCondition(comp);
3024}
3025
3026void LocationsBuilderARMVIXL::VisitAbove(HAbove* comp) {
3027 HandleCondition(comp);
3028}
3029
3030void InstructionCodeGeneratorARMVIXL::VisitAbove(HAbove* comp) {
3031 HandleCondition(comp);
3032}
3033
3034void LocationsBuilderARMVIXL::VisitAboveOrEqual(HAboveOrEqual* comp) {
3035 HandleCondition(comp);
3036}
3037
3038void InstructionCodeGeneratorARMVIXL::VisitAboveOrEqual(HAboveOrEqual* comp) {
3039 HandleCondition(comp);
3040}
3041
3042void LocationsBuilderARMVIXL::VisitIntConstant(HIntConstant* constant) {
3043 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003044 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Scott Wakelingfe885462016-09-22 10:24:38 +01003045 locations->SetOut(Location::ConstantLocation(constant));
3046}
3047
3048void InstructionCodeGeneratorARMVIXL::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
3049 // Will be generated at use site.
3050}
3051
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003052void LocationsBuilderARMVIXL::VisitNullConstant(HNullConstant* constant) {
3053 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003054 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003055 locations->SetOut(Location::ConstantLocation(constant));
3056}
3057
3058void InstructionCodeGeneratorARMVIXL::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
3059 // Will be generated at use site.
3060}
3061
Scott Wakelingfe885462016-09-22 10:24:38 +01003062void LocationsBuilderARMVIXL::VisitLongConstant(HLongConstant* constant) {
3063 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003064 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Scott Wakelingfe885462016-09-22 10:24:38 +01003065 locations->SetOut(Location::ConstantLocation(constant));
3066}
3067
3068void InstructionCodeGeneratorARMVIXL::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
3069 // Will be generated at use site.
3070}
3071
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01003072void LocationsBuilderARMVIXL::VisitFloatConstant(HFloatConstant* constant) {
3073 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003074 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01003075 locations->SetOut(Location::ConstantLocation(constant));
3076}
3077
Scott Wakelingc34dba72016-10-03 10:14:44 +01003078void InstructionCodeGeneratorARMVIXL::VisitFloatConstant(
3079 HFloatConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01003080 // Will be generated at use site.
3081}
3082
3083void LocationsBuilderARMVIXL::VisitDoubleConstant(HDoubleConstant* constant) {
3084 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003085 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01003086 locations->SetOut(Location::ConstantLocation(constant));
3087}
3088
Scott Wakelingc34dba72016-10-03 10:14:44 +01003089void InstructionCodeGeneratorARMVIXL::VisitDoubleConstant(
3090 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01003091 // Will be generated at use site.
3092}
3093
Igor Murashkind01745e2017-04-05 16:40:31 -07003094void LocationsBuilderARMVIXL::VisitConstructorFence(HConstructorFence* constructor_fence) {
3095 constructor_fence->SetLocations(nullptr);
3096}
3097
3098void InstructionCodeGeneratorARMVIXL::VisitConstructorFence(
3099 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
3100 codegen_->GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
3101}
3102
Scott Wakelingfe885462016-09-22 10:24:38 +01003103void LocationsBuilderARMVIXL::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3104 memory_barrier->SetLocations(nullptr);
3105}
3106
3107void InstructionCodeGeneratorARMVIXL::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3108 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
3109}
3110
3111void LocationsBuilderARMVIXL::VisitReturnVoid(HReturnVoid* ret) {
3112 ret->SetLocations(nullptr);
3113}
3114
3115void InstructionCodeGeneratorARMVIXL::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
3116 codegen_->GenerateFrameExit();
3117}
3118
3119void LocationsBuilderARMVIXL::VisitReturn(HReturn* ret) {
3120 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003121 new (GetGraph()->GetAllocator()) LocationSummary(ret, LocationSummary::kNoCall);
Scott Wakelingfe885462016-09-22 10:24:38 +01003122 locations->SetInAt(0, parameter_visitor_.GetReturnLocation(ret->InputAt(0)->GetType()));
3123}
3124
3125void InstructionCodeGeneratorARMVIXL::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
3126 codegen_->GenerateFrameExit();
3127}
3128
Artem Serovcfbe9132016-10-14 15:58:56 +01003129void LocationsBuilderARMVIXL::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3130 // The trampoline uses the same calling convention as dex calling conventions,
3131 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
3132 // the method_idx.
3133 HandleInvoke(invoke);
3134}
3135
3136void InstructionCodeGeneratorARMVIXL::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3137 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
Roland Levillain5daa4952017-07-03 17:23:56 +01003138 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 3);
Artem Serovcfbe9132016-10-14 15:58:56 +01003139}
3140
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003141void LocationsBuilderARMVIXL::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
3142 // Explicit clinit checks triggered by static invokes must have been pruned by
3143 // art::PrepareForRegisterAllocation.
3144 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
3145
Anton Kirilov5ec62182016-10-13 20:16:02 +01003146 IntrinsicLocationsBuilderARMVIXL intrinsic(codegen_);
3147 if (intrinsic.TryDispatch(invoke)) {
Anton Kirilov5ec62182016-10-13 20:16:02 +01003148 return;
3149 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003150
3151 HandleInvoke(invoke);
3152}
3153
Anton Kirilov5ec62182016-10-13 20:16:02 +01003154static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARMVIXL* codegen) {
3155 if (invoke->GetLocations()->Intrinsified()) {
3156 IntrinsicCodeGeneratorARMVIXL intrinsic(codegen);
3157 intrinsic.Dispatch(invoke);
3158 return true;
3159 }
3160 return false;
3161}
3162
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003163void InstructionCodeGeneratorARMVIXL::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
3164 // Explicit clinit checks triggered by static invokes must have been pruned by
3165 // art::PrepareForRegisterAllocation.
3166 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
3167
Anton Kirilov5ec62182016-10-13 20:16:02 +01003168 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
Roland Levillain5daa4952017-07-03 17:23:56 +01003169 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 4);
Anton Kirilov5ec62182016-10-13 20:16:02 +01003170 return;
3171 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003172
3173 LocationSummary* locations = invoke->GetLocations();
Artem Serovd4cc5b22016-11-04 11:19:09 +00003174 codegen_->GenerateStaticOrDirectCall(
3175 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Roland Levillain5daa4952017-07-03 17:23:56 +01003176
3177 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 5);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003178}
3179
3180void LocationsBuilderARMVIXL::HandleInvoke(HInvoke* invoke) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00003181 InvokeDexCallingConventionVisitorARMVIXL calling_convention_visitor;
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003182 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
3183}
3184
3185void LocationsBuilderARMVIXL::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Anton Kirilov5ec62182016-10-13 20:16:02 +01003186 IntrinsicLocationsBuilderARMVIXL intrinsic(codegen_);
3187 if (intrinsic.TryDispatch(invoke)) {
3188 return;
3189 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003190
3191 HandleInvoke(invoke);
3192}
3193
3194void InstructionCodeGeneratorARMVIXL::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Anton Kirilov5ec62182016-10-13 20:16:02 +01003195 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
Roland Levillain5daa4952017-07-03 17:23:56 +01003196 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 6);
Anton Kirilov5ec62182016-10-13 20:16:02 +01003197 return;
3198 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003199
3200 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexandre Rames374ddf32016-11-04 10:40:49 +00003201 DCHECK(!codegen_->IsLeafMethod());
Roland Levillain5daa4952017-07-03 17:23:56 +01003202
3203 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 7);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003204}
3205
Artem Serovcfbe9132016-10-14 15:58:56 +01003206void LocationsBuilderARMVIXL::VisitInvokeInterface(HInvokeInterface* invoke) {
3207 HandleInvoke(invoke);
3208 // Add the hidden argument.
3209 invoke->GetLocations()->AddTemp(LocationFrom(r12));
3210}
3211
3212void InstructionCodeGeneratorARMVIXL::VisitInvokeInterface(HInvokeInterface* invoke) {
3213 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
3214 LocationSummary* locations = invoke->GetLocations();
3215 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
3216 vixl32::Register hidden_reg = RegisterFrom(locations->GetTemp(1));
3217 Location receiver = locations->InAt(0);
3218 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3219
3220 DCHECK(!receiver.IsStackSlot());
3221
Alexandre Rames374ddf32016-11-04 10:40:49 +00003222 // Ensure the pc position is recorded immediately after the `ldr` instruction.
3223 {
Artem Serov0fb37192016-12-06 18:13:40 +00003224 ExactAssemblyScope aas(GetVIXLAssembler(),
3225 vixl32::kMaxInstructionSizeInBytes,
3226 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00003227 // /* HeapReference<Class> */ temp = receiver->klass_
3228 __ ldr(temp, MemOperand(RegisterFrom(receiver), class_offset));
3229 codegen_->MaybeRecordImplicitNullCheck(invoke);
3230 }
Artem Serovcfbe9132016-10-14 15:58:56 +01003231 // Instead of simply (possibly) unpoisoning `temp` here, we should
3232 // emit a read barrier for the previous class reference load.
3233 // However this is not required in practice, as this is an
3234 // intermediate/temporary reference and because the current
3235 // concurrent copying collector keeps the from-space memory
3236 // intact/accessible until the end of the marking phase (the
3237 // concurrent copying collector may not in the future).
3238 GetAssembler()->MaybeUnpoisonHeapReference(temp);
3239 GetAssembler()->LoadFromOffset(kLoadWord,
3240 temp,
3241 temp,
3242 mirror::Class::ImtPtrOffset(kArmPointerSize).Uint32Value());
3243 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
3244 invoke->GetImtIndex(), kArmPointerSize));
3245 // temp = temp->GetImtEntryAt(method_offset);
3246 GetAssembler()->LoadFromOffset(kLoadWord, temp, temp, method_offset);
3247 uint32_t entry_point =
3248 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize).Int32Value();
3249 // LR = temp->GetEntryPoint();
3250 GetAssembler()->LoadFromOffset(kLoadWord, lr, temp, entry_point);
3251
3252 // Set the hidden (in r12) argument. It is done here, right before a BLX to prevent other
3253 // instruction from clobbering it as they might use r12 as a scratch register.
3254 DCHECK(hidden_reg.Is(r12));
Scott Wakelingb77051e2016-11-21 19:46:00 +00003255
3256 {
3257 // The VIXL macro assembler may clobber any of the scratch registers that are available to it,
3258 // so it checks if the application is using them (by passing them to the macro assembler
3259 // methods). The following application of UseScratchRegisterScope corrects VIXL's notion of
3260 // what is available, and is the opposite of the standard usage: Instead of requesting a
3261 // temporary location, it imposes an external constraint (i.e. a specific register is reserved
3262 // for the hidden argument). Note that this works even if VIXL needs a scratch register itself
3263 // (to materialize the constant), since the destination register becomes available for such use
3264 // internally for the duration of the macro instruction.
3265 UseScratchRegisterScope temps(GetVIXLAssembler());
3266 temps.Exclude(hidden_reg);
3267 __ Mov(hidden_reg, invoke->GetDexMethodIndex());
3268 }
Artem Serovcfbe9132016-10-14 15:58:56 +01003269 {
Alexandre Rames374ddf32016-11-04 10:40:49 +00003270 // Ensure the pc position is recorded immediately after the `blx` instruction.
3271 // 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 +00003272 ExactAssemblyScope aas(GetVIXLAssembler(),
Alexandre Rames374ddf32016-11-04 10:40:49 +00003273 vixl32::k16BitT32InstructionSizeInBytes,
3274 CodeBufferCheckScope::kExactSize);
Artem Serovcfbe9132016-10-14 15:58:56 +01003275 // LR();
3276 __ blx(lr);
Artem Serovcfbe9132016-10-14 15:58:56 +01003277 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Alexandre Rames374ddf32016-11-04 10:40:49 +00003278 DCHECK(!codegen_->IsLeafMethod());
Artem Serovcfbe9132016-10-14 15:58:56 +01003279 }
Roland Levillain5daa4952017-07-03 17:23:56 +01003280
3281 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 8);
Artem Serovcfbe9132016-10-14 15:58:56 +01003282}
3283
Orion Hodsonac141392017-01-13 11:53:47 +00003284void LocationsBuilderARMVIXL::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
3285 HandleInvoke(invoke);
3286}
3287
3288void InstructionCodeGeneratorARMVIXL::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
3289 codegen_->GenerateInvokePolymorphicCall(invoke);
Roland Levillain5daa4952017-07-03 17:23:56 +01003290 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 9);
Orion Hodsonac141392017-01-13 11:53:47 +00003291}
3292
Orion Hodson4c8e12e2018-05-18 08:33:20 +01003293void LocationsBuilderARMVIXL::VisitInvokeCustom(HInvokeCustom* invoke) {
3294 HandleInvoke(invoke);
3295}
3296
3297void InstructionCodeGeneratorARMVIXL::VisitInvokeCustom(HInvokeCustom* invoke) {
3298 codegen_->GenerateInvokeCustomCall(invoke);
3299 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 10);
3300}
3301
Artem Serov02109dd2016-09-23 17:17:54 +01003302void LocationsBuilderARMVIXL::VisitNeg(HNeg* neg) {
3303 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003304 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Artem Serov02109dd2016-09-23 17:17:54 +01003305 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003306 case DataType::Type::kInt32: {
Artem Serov02109dd2016-09-23 17:17:54 +01003307 locations->SetInAt(0, Location::RequiresRegister());
3308 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3309 break;
3310 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003311 case DataType::Type::kInt64: {
Artem Serov02109dd2016-09-23 17:17:54 +01003312 locations->SetInAt(0, Location::RequiresRegister());
3313 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3314 break;
3315 }
3316
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003317 case DataType::Type::kFloat32:
3318 case DataType::Type::kFloat64:
Artem Serov02109dd2016-09-23 17:17:54 +01003319 locations->SetInAt(0, Location::RequiresFpuRegister());
3320 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3321 break;
3322
3323 default:
3324 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
3325 }
3326}
3327
3328void InstructionCodeGeneratorARMVIXL::VisitNeg(HNeg* neg) {
3329 LocationSummary* locations = neg->GetLocations();
3330 Location out = locations->Out();
3331 Location in = locations->InAt(0);
3332 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003333 case DataType::Type::kInt32:
Artem Serov02109dd2016-09-23 17:17:54 +01003334 __ Rsb(OutputRegister(neg), InputRegisterAt(neg, 0), 0);
3335 break;
3336
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003337 case DataType::Type::kInt64:
Artem Serov02109dd2016-09-23 17:17:54 +01003338 // out.lo = 0 - in.lo (and update the carry/borrow (C) flag)
3339 __ Rsbs(LowRegisterFrom(out), LowRegisterFrom(in), 0);
3340 // We cannot emit an RSC (Reverse Subtract with Carry)
3341 // instruction here, as it does not exist in the Thumb-2
3342 // instruction set. We use the following approach
3343 // using SBC and SUB instead.
3344 //
3345 // out.hi = -C
3346 __ Sbc(HighRegisterFrom(out), HighRegisterFrom(out), HighRegisterFrom(out));
3347 // out.hi = out.hi - in.hi
3348 __ Sub(HighRegisterFrom(out), HighRegisterFrom(out), HighRegisterFrom(in));
3349 break;
3350
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003351 case DataType::Type::kFloat32:
3352 case DataType::Type::kFloat64:
Anton Kirilov644032c2016-12-06 17:51:43 +00003353 __ Vneg(OutputVRegister(neg), InputVRegister(neg));
Artem Serov02109dd2016-09-23 17:17:54 +01003354 break;
3355
3356 default:
3357 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
3358 }
3359}
3360
Scott Wakelingfe885462016-09-22 10:24:38 +01003361void LocationsBuilderARMVIXL::VisitTypeConversion(HTypeConversion* conversion) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003362 DataType::Type result_type = conversion->GetResultType();
3363 DataType::Type input_type = conversion->GetInputType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003364 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
3365 << input_type << " -> " << result_type;
Scott Wakelingfe885462016-09-22 10:24:38 +01003366
3367 // The float-to-long, double-to-long and long-to-float type conversions
3368 // rely on a call to the runtime.
3369 LocationSummary::CallKind call_kind =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003370 (((input_type == DataType::Type::kFloat32 || input_type == DataType::Type::kFloat64)
3371 && result_type == DataType::Type::kInt64)
3372 || (input_type == DataType::Type::kInt64 && result_type == DataType::Type::kFloat32))
Scott Wakelingfe885462016-09-22 10:24:38 +01003373 ? LocationSummary::kCallOnMainOnly
3374 : LocationSummary::kNoCall;
3375 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003376 new (GetGraph()->GetAllocator()) LocationSummary(conversion, call_kind);
Scott Wakelingfe885462016-09-22 10:24:38 +01003377
Scott Wakelingfe885462016-09-22 10:24:38 +01003378 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003379 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003380 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003381 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003382 case DataType::Type::kInt16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003383 DCHECK(DataType::IsIntegralType(input_type)) << input_type;
3384 locations->SetInAt(0, Location::RequiresRegister());
3385 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Scott Wakelingfe885462016-09-22 10:24:38 +01003386 break;
3387
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003388 case DataType::Type::kInt32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003389 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003390 case DataType::Type::kInt64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003391 locations->SetInAt(0, Location::Any());
3392 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3393 break;
3394
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003395 case DataType::Type::kFloat32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003396 locations->SetInAt(0, Location::RequiresFpuRegister());
3397 locations->SetOut(Location::RequiresRegister());
3398 locations->AddTemp(Location::RequiresFpuRegister());
3399 break;
3400
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003401 case DataType::Type::kFloat64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003402 locations->SetInAt(0, Location::RequiresFpuRegister());
3403 locations->SetOut(Location::RequiresRegister());
3404 locations->AddTemp(Location::RequiresFpuRegister());
3405 break;
3406
3407 default:
3408 LOG(FATAL) << "Unexpected type conversion from " << input_type
3409 << " to " << result_type;
3410 }
3411 break;
3412
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003413 case DataType::Type::kInt64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003414 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003415 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003416 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003417 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003418 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003419 case DataType::Type::kInt16:
3420 case DataType::Type::kInt32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003421 locations->SetInAt(0, Location::RequiresRegister());
3422 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3423 break;
3424
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003425 case DataType::Type::kFloat32: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003426 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3427 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
3428 locations->SetOut(LocationFrom(r0, r1));
Scott Wakelingfe885462016-09-22 10:24:38 +01003429 break;
3430 }
3431
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003432 case DataType::Type::kFloat64: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003433 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3434 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0),
3435 calling_convention.GetFpuRegisterAt(1)));
3436 locations->SetOut(LocationFrom(r0, r1));
Scott Wakelingfe885462016-09-22 10:24:38 +01003437 break;
3438 }
3439
3440 default:
3441 LOG(FATAL) << "Unexpected type conversion from " << input_type
3442 << " to " << result_type;
3443 }
3444 break;
3445
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003446 case DataType::Type::kFloat32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003447 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003448 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003449 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003450 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003451 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003452 case DataType::Type::kInt16:
3453 case DataType::Type::kInt32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003454 locations->SetInAt(0, Location::RequiresRegister());
3455 locations->SetOut(Location::RequiresFpuRegister());
3456 break;
3457
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003458 case DataType::Type::kInt64: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003459 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3460 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0),
3461 calling_convention.GetRegisterAt(1)));
3462 locations->SetOut(LocationFrom(calling_convention.GetFpuRegisterAt(0)));
Scott Wakelingfe885462016-09-22 10:24:38 +01003463 break;
3464 }
3465
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003466 case DataType::Type::kFloat64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003467 locations->SetInAt(0, Location::RequiresFpuRegister());
3468 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3469 break;
3470
3471 default:
3472 LOG(FATAL) << "Unexpected type conversion from " << input_type
3473 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08003474 }
Scott Wakelingfe885462016-09-22 10:24:38 +01003475 break;
3476
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003477 case DataType::Type::kFloat64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003478 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003479 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003480 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003481 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003482 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003483 case DataType::Type::kInt16:
3484 case DataType::Type::kInt32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003485 locations->SetInAt(0, Location::RequiresRegister());
3486 locations->SetOut(Location::RequiresFpuRegister());
3487 break;
3488
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003489 case DataType::Type::kInt64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003490 locations->SetInAt(0, Location::RequiresRegister());
3491 locations->SetOut(Location::RequiresFpuRegister());
3492 locations->AddTemp(Location::RequiresFpuRegister());
3493 locations->AddTemp(Location::RequiresFpuRegister());
3494 break;
3495
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003496 case DataType::Type::kFloat32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003497 locations->SetInAt(0, Location::RequiresFpuRegister());
3498 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3499 break;
3500
3501 default:
3502 LOG(FATAL) << "Unexpected type conversion from " << input_type
3503 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08003504 }
Scott Wakelingfe885462016-09-22 10:24:38 +01003505 break;
3506
3507 default:
3508 LOG(FATAL) << "Unexpected type conversion from " << input_type
3509 << " to " << result_type;
3510 }
3511}
3512
3513void InstructionCodeGeneratorARMVIXL::VisitTypeConversion(HTypeConversion* conversion) {
3514 LocationSummary* locations = conversion->GetLocations();
3515 Location out = locations->Out();
3516 Location in = locations->InAt(0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003517 DataType::Type result_type = conversion->GetResultType();
3518 DataType::Type input_type = conversion->GetInputType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003519 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
3520 << input_type << " -> " << result_type;
Scott Wakelingfe885462016-09-22 10:24:38 +01003521 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003522 case DataType::Type::kUint8:
Scott Wakelingfe885462016-09-22 10:24:38 +01003523 switch (input_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003524 case DataType::Type::kInt8:
3525 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003526 case DataType::Type::kInt16:
3527 case DataType::Type::kInt32:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003528 __ Ubfx(OutputRegister(conversion), InputRegisterAt(conversion, 0), 0, 8);
3529 break;
3530 case DataType::Type::kInt64:
3531 __ Ubfx(OutputRegister(conversion), LowRegisterFrom(in), 0, 8);
3532 break;
3533
3534 default:
3535 LOG(FATAL) << "Unexpected type conversion from " << input_type
3536 << " to " << result_type;
3537 }
3538 break;
3539
3540 case DataType::Type::kInt8:
3541 switch (input_type) {
3542 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003543 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003544 case DataType::Type::kInt16:
3545 case DataType::Type::kInt32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003546 __ Sbfx(OutputRegister(conversion), InputRegisterAt(conversion, 0), 0, 8);
3547 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003548 case DataType::Type::kInt64:
3549 __ Sbfx(OutputRegister(conversion), LowRegisterFrom(in), 0, 8);
3550 break;
3551
3552 default:
3553 LOG(FATAL) << "Unexpected type conversion from " << input_type
3554 << " to " << result_type;
3555 }
3556 break;
3557
3558 case DataType::Type::kUint16:
3559 switch (input_type) {
3560 case DataType::Type::kInt8:
3561 case DataType::Type::kInt16:
3562 case DataType::Type::kInt32:
3563 __ Ubfx(OutputRegister(conversion), InputRegisterAt(conversion, 0), 0, 16);
3564 break;
3565 case DataType::Type::kInt64:
3566 __ Ubfx(OutputRegister(conversion), LowRegisterFrom(in), 0, 16);
3567 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01003568
3569 default:
3570 LOG(FATAL) << "Unexpected type conversion from " << input_type
3571 << " to " << result_type;
3572 }
3573 break;
3574
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003575 case DataType::Type::kInt16:
Scott Wakelingfe885462016-09-22 10:24:38 +01003576 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003577 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003578 case DataType::Type::kInt32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003579 __ Sbfx(OutputRegister(conversion), InputRegisterAt(conversion, 0), 0, 16);
3580 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003581 case DataType::Type::kInt64:
3582 __ Sbfx(OutputRegister(conversion), LowRegisterFrom(in), 0, 16);
3583 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01003584
3585 default:
3586 LOG(FATAL) << "Unexpected type conversion from " << input_type
3587 << " to " << result_type;
3588 }
3589 break;
3590
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003591 case DataType::Type::kInt32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003592 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003593 case DataType::Type::kInt64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003594 DCHECK(out.IsRegister());
3595 if (in.IsRegisterPair()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003596 __ Mov(OutputRegister(conversion), LowRegisterFrom(in));
Scott Wakelingfe885462016-09-22 10:24:38 +01003597 } else if (in.IsDoubleStackSlot()) {
3598 GetAssembler()->LoadFromOffset(kLoadWord,
3599 OutputRegister(conversion),
3600 sp,
3601 in.GetStackIndex());
3602 } else {
3603 DCHECK(in.IsConstant());
3604 DCHECK(in.GetConstant()->IsLongConstant());
Vladimir Markoba1a48e2017-04-13 11:50:14 +01003605 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
3606 __ Mov(OutputRegister(conversion), static_cast<int32_t>(value));
Scott Wakelingfe885462016-09-22 10:24:38 +01003607 }
3608 break;
3609
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003610 case DataType::Type::kFloat32: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003611 vixl32::SRegister temp = LowSRegisterFrom(locations->GetTemp(0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01003612 __ Vcvt(S32, F32, temp, InputSRegisterAt(conversion, 0));
Scott Wakelingfe885462016-09-22 10:24:38 +01003613 __ Vmov(OutputRegister(conversion), temp);
3614 break;
3615 }
3616
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003617 case DataType::Type::kFloat64: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003618 vixl32::SRegister temp_s = LowSRegisterFrom(locations->GetTemp(0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01003619 __ Vcvt(S32, F64, temp_s, DRegisterFrom(in));
Scott Wakelingfe885462016-09-22 10:24:38 +01003620 __ Vmov(OutputRegister(conversion), temp_s);
3621 break;
3622 }
3623
3624 default:
3625 LOG(FATAL) << "Unexpected type conversion from " << input_type
3626 << " to " << result_type;
3627 }
3628 break;
3629
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003630 case DataType::Type::kInt64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003631 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003632 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003633 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003634 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003635 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003636 case DataType::Type::kInt16:
3637 case DataType::Type::kInt32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003638 DCHECK(out.IsRegisterPair());
3639 DCHECK(in.IsRegister());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003640 __ Mov(LowRegisterFrom(out), InputRegisterAt(conversion, 0));
Scott Wakelingfe885462016-09-22 10:24:38 +01003641 // Sign extension.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003642 __ Asr(HighRegisterFrom(out), LowRegisterFrom(out), 31);
Scott Wakelingfe885462016-09-22 10:24:38 +01003643 break;
3644
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003645 case DataType::Type::kFloat32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003646 codegen_->InvokeRuntime(kQuickF2l, conversion, conversion->GetDexPc());
3647 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
3648 break;
3649
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003650 case DataType::Type::kFloat64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003651 codegen_->InvokeRuntime(kQuickD2l, conversion, conversion->GetDexPc());
3652 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
3653 break;
3654
3655 default:
3656 LOG(FATAL) << "Unexpected type conversion from " << input_type
3657 << " to " << result_type;
3658 }
3659 break;
3660
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003661 case DataType::Type::kFloat32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003662 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003663 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003664 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003665 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003666 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003667 case DataType::Type::kInt16:
3668 case DataType::Type::kInt32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003669 __ Vmov(OutputSRegister(conversion), InputRegisterAt(conversion, 0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01003670 __ Vcvt(F32, S32, OutputSRegister(conversion), OutputSRegister(conversion));
Scott Wakelingfe885462016-09-22 10:24:38 +01003671 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01003672
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003673 case DataType::Type::kInt64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003674 codegen_->InvokeRuntime(kQuickL2f, conversion, conversion->GetDexPc());
3675 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
3676 break;
3677
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003678 case DataType::Type::kFloat64:
Scott Wakelingc34dba72016-10-03 10:14:44 +01003679 __ Vcvt(F32, F64, OutputSRegister(conversion), DRegisterFrom(in));
Scott Wakelingfe885462016-09-22 10:24:38 +01003680 break;
3681
3682 default:
3683 LOG(FATAL) << "Unexpected type conversion from " << input_type
3684 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08003685 }
Scott Wakelingfe885462016-09-22 10:24:38 +01003686 break;
3687
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003688 case DataType::Type::kFloat64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003689 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003690 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003691 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003692 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003693 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003694 case DataType::Type::kInt16:
3695 case DataType::Type::kInt32:
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003696 __ Vmov(LowSRegisterFrom(out), InputRegisterAt(conversion, 0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01003697 __ Vcvt(F64, S32, DRegisterFrom(out), LowSRegisterFrom(out));
Scott Wakelingfe885462016-09-22 10:24:38 +01003698 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01003699
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003700 case DataType::Type::kInt64: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003701 vixl32::Register low = LowRegisterFrom(in);
3702 vixl32::Register high = HighRegisterFrom(in);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003703 vixl32::SRegister out_s = LowSRegisterFrom(out);
Scott Wakelingc34dba72016-10-03 10:14:44 +01003704 vixl32::DRegister out_d = DRegisterFrom(out);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003705 vixl32::SRegister temp_s = LowSRegisterFrom(locations->GetTemp(0));
Scott Wakelingc34dba72016-10-03 10:14:44 +01003706 vixl32::DRegister temp_d = DRegisterFrom(locations->GetTemp(0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01003707 vixl32::DRegister constant_d = DRegisterFrom(locations->GetTemp(1));
Scott Wakelingfe885462016-09-22 10:24:38 +01003708
3709 // temp_d = int-to-double(high)
3710 __ Vmov(temp_s, high);
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01003711 __ Vcvt(F64, S32, temp_d, temp_s);
Scott Wakelingfe885462016-09-22 10:24:38 +01003712 // constant_d = k2Pow32EncodingForDouble
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003713 __ Vmov(constant_d, bit_cast<double, int64_t>(k2Pow32EncodingForDouble));
Scott Wakelingfe885462016-09-22 10:24:38 +01003714 // out_d = unsigned-to-double(low)
3715 __ Vmov(out_s, low);
3716 __ Vcvt(F64, U32, out_d, out_s);
3717 // out_d += temp_d * constant_d
3718 __ Vmla(F64, out_d, temp_d, constant_d);
3719 break;
3720 }
3721
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003722 case DataType::Type::kFloat32:
Scott Wakelingc34dba72016-10-03 10:14:44 +01003723 __ Vcvt(F64, F32, DRegisterFrom(out), InputSRegisterAt(conversion, 0));
Scott Wakelingfe885462016-09-22 10:24:38 +01003724 break;
3725
3726 default:
3727 LOG(FATAL) << "Unexpected type conversion from " << input_type
3728 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08003729 }
Scott Wakelingfe885462016-09-22 10:24:38 +01003730 break;
3731
3732 default:
3733 LOG(FATAL) << "Unexpected type conversion from " << input_type
3734 << " to " << result_type;
3735 }
3736}
3737
3738void LocationsBuilderARMVIXL::VisitAdd(HAdd* add) {
3739 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003740 new (GetGraph()->GetAllocator()) LocationSummary(add, LocationSummary::kNoCall);
Scott Wakelingfe885462016-09-22 10:24:38 +01003741 switch (add->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003742 case DataType::Type::kInt32: {
Scott Wakelingfe885462016-09-22 10:24:38 +01003743 locations->SetInAt(0, Location::RequiresRegister());
3744 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
3745 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3746 break;
3747 }
3748
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003749 case DataType::Type::kInt64: {
Scott Wakelingfe885462016-09-22 10:24:38 +01003750 locations->SetInAt(0, Location::RequiresRegister());
Anton Kirilovdda43962016-11-21 19:55:20 +00003751 locations->SetInAt(1, ArmEncodableConstantOrRegister(add->InputAt(1), ADD));
Scott Wakelingfe885462016-09-22 10:24:38 +01003752 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3753 break;
3754 }
3755
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003756 case DataType::Type::kFloat32:
3757 case DataType::Type::kFloat64: {
Scott Wakelingfe885462016-09-22 10:24:38 +01003758 locations->SetInAt(0, Location::RequiresFpuRegister());
3759 locations->SetInAt(1, Location::RequiresFpuRegister());
3760 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3761 break;
3762 }
3763
3764 default:
3765 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
3766 }
3767}
3768
3769void InstructionCodeGeneratorARMVIXL::VisitAdd(HAdd* add) {
3770 LocationSummary* locations = add->GetLocations();
3771 Location out = locations->Out();
3772 Location first = locations->InAt(0);
3773 Location second = locations->InAt(1);
3774
3775 switch (add->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003776 case DataType::Type::kInt32: {
Scott Wakelingfe885462016-09-22 10:24:38 +01003777 __ Add(OutputRegister(add), InputRegisterAt(add, 0), InputOperandAt(add, 1));
3778 }
3779 break;
3780
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003781 case DataType::Type::kInt64: {
Anton Kirilovdda43962016-11-21 19:55:20 +00003782 if (second.IsConstant()) {
3783 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
3784 GenerateAddLongConst(out, first, value);
3785 } else {
3786 DCHECK(second.IsRegisterPair());
3787 __ Adds(LowRegisterFrom(out), LowRegisterFrom(first), LowRegisterFrom(second));
3788 __ Adc(HighRegisterFrom(out), HighRegisterFrom(first), HighRegisterFrom(second));
3789 }
Scott Wakelingfe885462016-09-22 10:24:38 +01003790 break;
3791 }
3792
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003793 case DataType::Type::kFloat32:
3794 case DataType::Type::kFloat64:
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003795 __ Vadd(OutputVRegister(add), InputVRegisterAt(add, 0), InputVRegisterAt(add, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01003796 break;
3797
3798 default:
3799 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
3800 }
3801}
3802
3803void LocationsBuilderARMVIXL::VisitSub(HSub* sub) {
3804 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003805 new (GetGraph()->GetAllocator()) LocationSummary(sub, LocationSummary::kNoCall);
Scott Wakelingfe885462016-09-22 10:24:38 +01003806 switch (sub->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003807 case DataType::Type::kInt32: {
Scott Wakelingfe885462016-09-22 10:24:38 +01003808 locations->SetInAt(0, Location::RequiresRegister());
3809 locations->SetInAt(1, Location::RegisterOrConstant(sub->InputAt(1)));
3810 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3811 break;
3812 }
3813
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003814 case DataType::Type::kInt64: {
Scott Wakelingfe885462016-09-22 10:24:38 +01003815 locations->SetInAt(0, Location::RequiresRegister());
Anton Kirilovdda43962016-11-21 19:55:20 +00003816 locations->SetInAt(1, ArmEncodableConstantOrRegister(sub->InputAt(1), SUB));
Scott Wakelingfe885462016-09-22 10:24:38 +01003817 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3818 break;
3819 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003820 case DataType::Type::kFloat32:
3821 case DataType::Type::kFloat64: {
Scott Wakelingfe885462016-09-22 10:24:38 +01003822 locations->SetInAt(0, Location::RequiresFpuRegister());
3823 locations->SetInAt(1, Location::RequiresFpuRegister());
3824 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3825 break;
3826 }
3827 default:
3828 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
3829 }
3830}
3831
3832void InstructionCodeGeneratorARMVIXL::VisitSub(HSub* sub) {
3833 LocationSummary* locations = sub->GetLocations();
3834 Location out = locations->Out();
3835 Location first = locations->InAt(0);
3836 Location second = locations->InAt(1);
3837 switch (sub->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003838 case DataType::Type::kInt32: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003839 __ Sub(OutputRegister(sub), InputRegisterAt(sub, 0), InputOperandAt(sub, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01003840 break;
3841 }
3842
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003843 case DataType::Type::kInt64: {
Anton Kirilovdda43962016-11-21 19:55:20 +00003844 if (second.IsConstant()) {
3845 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
3846 GenerateAddLongConst(out, first, -value);
3847 } else {
3848 DCHECK(second.IsRegisterPair());
3849 __ Subs(LowRegisterFrom(out), LowRegisterFrom(first), LowRegisterFrom(second));
3850 __ Sbc(HighRegisterFrom(out), HighRegisterFrom(first), HighRegisterFrom(second));
3851 }
Scott Wakelingfe885462016-09-22 10:24:38 +01003852 break;
3853 }
3854
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003855 case DataType::Type::kFloat32:
3856 case DataType::Type::kFloat64:
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003857 __ Vsub(OutputVRegister(sub), InputVRegisterAt(sub, 0), InputVRegisterAt(sub, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01003858 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01003859
3860 default:
3861 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
3862 }
3863}
3864
3865void LocationsBuilderARMVIXL::VisitMul(HMul* mul) {
3866 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003867 new (GetGraph()->GetAllocator()) LocationSummary(mul, LocationSummary::kNoCall);
Scott Wakelingfe885462016-09-22 10:24:38 +01003868 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003869 case DataType::Type::kInt32:
3870 case DataType::Type::kInt64: {
Scott Wakelingfe885462016-09-22 10:24:38 +01003871 locations->SetInAt(0, Location::RequiresRegister());
3872 locations->SetInAt(1, Location::RequiresRegister());
3873 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3874 break;
3875 }
3876
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003877 case DataType::Type::kFloat32:
3878 case DataType::Type::kFloat64: {
Scott Wakelingfe885462016-09-22 10:24:38 +01003879 locations->SetInAt(0, Location::RequiresFpuRegister());
3880 locations->SetInAt(1, Location::RequiresFpuRegister());
3881 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3882 break;
3883 }
3884
3885 default:
3886 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
3887 }
3888}
3889
3890void InstructionCodeGeneratorARMVIXL::VisitMul(HMul* mul) {
3891 LocationSummary* locations = mul->GetLocations();
3892 Location out = locations->Out();
3893 Location first = locations->InAt(0);
3894 Location second = locations->InAt(1);
3895 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003896 case DataType::Type::kInt32: {
Scott Wakelingfe885462016-09-22 10:24:38 +01003897 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
3898 break;
3899 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003900 case DataType::Type::kInt64: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003901 vixl32::Register out_hi = HighRegisterFrom(out);
3902 vixl32::Register out_lo = LowRegisterFrom(out);
3903 vixl32::Register in1_hi = HighRegisterFrom(first);
3904 vixl32::Register in1_lo = LowRegisterFrom(first);
3905 vixl32::Register in2_hi = HighRegisterFrom(second);
3906 vixl32::Register in2_lo = LowRegisterFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01003907
3908 // Extra checks to protect caused by the existence of R1_R2.
3909 // The algorithm is wrong if out.hi is either in1.lo or in2.lo:
3910 // (e.g. in1=r0_r1, in2=r2_r3 and out=r1_r2);
Anton Kirilov644032c2016-12-06 17:51:43 +00003911 DCHECK(!out_hi.Is(in1_lo));
3912 DCHECK(!out_hi.Is(in2_lo));
Scott Wakelingfe885462016-09-22 10:24:38 +01003913
3914 // input: in1 - 64 bits, in2 - 64 bits
3915 // output: out
3916 // formula: out.hi : out.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
3917 // parts: out.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
3918 // parts: out.lo = (in1.lo * in2.lo)[31:0]
3919
3920 UseScratchRegisterScope temps(GetVIXLAssembler());
3921 vixl32::Register temp = temps.Acquire();
3922 // temp <- in1.lo * in2.hi
3923 __ Mul(temp, in1_lo, in2_hi);
3924 // out.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3925 __ Mla(out_hi, in1_hi, in2_lo, temp);
3926 // out.lo <- (in1.lo * in2.lo)[31:0];
3927 __ Umull(out_lo, temp, in1_lo, in2_lo);
3928 // out.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003929 __ Add(out_hi, out_hi, temp);
Scott Wakelingfe885462016-09-22 10:24:38 +01003930 break;
3931 }
3932
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003933 case DataType::Type::kFloat32:
3934 case DataType::Type::kFloat64:
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003935 __ Vmul(OutputVRegister(mul), InputVRegisterAt(mul, 0), InputVRegisterAt(mul, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01003936 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01003937
3938 default:
3939 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
3940 }
3941}
3942
Scott Wakelingfe885462016-09-22 10:24:38 +01003943void InstructionCodeGeneratorARMVIXL::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3944 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003945 DCHECK(instruction->GetResultType() == DataType::Type::kInt32);
Scott Wakelingfe885462016-09-22 10:24:38 +01003946
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003947 Location second = instruction->GetLocations()->InAt(1);
Scott Wakelingfe885462016-09-22 10:24:38 +01003948 DCHECK(second.IsConstant());
3949
3950 vixl32::Register out = OutputRegister(instruction);
3951 vixl32::Register dividend = InputRegisterAt(instruction, 0);
Anton Kirilov644032c2016-12-06 17:51:43 +00003952 int32_t imm = Int32ConstantFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01003953 DCHECK(imm == 1 || imm == -1);
3954
3955 if (instruction->IsRem()) {
3956 __ Mov(out, 0);
3957 } else {
3958 if (imm == 1) {
3959 __ Mov(out, dividend);
3960 } else {
3961 __ Rsb(out, dividend, 0);
3962 }
3963 }
3964}
3965
3966void InstructionCodeGeneratorARMVIXL::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
3967 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003968 DCHECK(instruction->GetResultType() == DataType::Type::kInt32);
Scott Wakelingfe885462016-09-22 10:24:38 +01003969
3970 LocationSummary* locations = instruction->GetLocations();
3971 Location second = locations->InAt(1);
3972 DCHECK(second.IsConstant());
3973
3974 vixl32::Register out = OutputRegister(instruction);
3975 vixl32::Register dividend = InputRegisterAt(instruction, 0);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003976 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
Anton Kirilov644032c2016-12-06 17:51:43 +00003977 int32_t imm = Int32ConstantFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01003978 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
3979 int ctz_imm = CTZ(abs_imm);
3980
3981 if (ctz_imm == 1) {
3982 __ Lsr(temp, dividend, 32 - ctz_imm);
3983 } else {
3984 __ Asr(temp, dividend, 31);
3985 __ Lsr(temp, temp, 32 - ctz_imm);
3986 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003987 __ Add(out, temp, dividend);
Scott Wakelingfe885462016-09-22 10:24:38 +01003988
3989 if (instruction->IsDiv()) {
3990 __ Asr(out, out, ctz_imm);
3991 if (imm < 0) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003992 __ Rsb(out, out, 0);
Scott Wakelingfe885462016-09-22 10:24:38 +01003993 }
3994 } else {
3995 __ Ubfx(out, out, 0, ctz_imm);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003996 __ Sub(out, out, temp);
Scott Wakelingfe885462016-09-22 10:24:38 +01003997 }
3998}
3999
4000void InstructionCodeGeneratorARMVIXL::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
4001 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004002 DCHECK(instruction->GetResultType() == DataType::Type::kInt32);
Scott Wakelingfe885462016-09-22 10:24:38 +01004003
4004 LocationSummary* locations = instruction->GetLocations();
4005 Location second = locations->InAt(1);
4006 DCHECK(second.IsConstant());
4007
4008 vixl32::Register out = OutputRegister(instruction);
4009 vixl32::Register dividend = InputRegisterAt(instruction, 0);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004010 vixl32::Register temp1 = RegisterFrom(locations->GetTemp(0));
4011 vixl32::Register temp2 = RegisterFrom(locations->GetTemp(1));
Scott Wakelingb77051e2016-11-21 19:46:00 +00004012 int32_t imm = Int32ConstantFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01004013
4014 int64_t magic;
4015 int shift;
4016 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
4017
Anton Kirilovdda43962016-11-21 19:55:20 +00004018 // TODO(VIXL): Change the static cast to Operand::From() after VIXL is fixed.
4019 __ Mov(temp1, static_cast<int32_t>(magic));
Scott Wakelingfe885462016-09-22 10:24:38 +01004020 __ Smull(temp2, temp1, dividend, temp1);
4021
4022 if (imm > 0 && magic < 0) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004023 __ Add(temp1, temp1, dividend);
Scott Wakelingfe885462016-09-22 10:24:38 +01004024 } else if (imm < 0 && magic > 0) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004025 __ Sub(temp1, temp1, dividend);
Scott Wakelingfe885462016-09-22 10:24:38 +01004026 }
4027
4028 if (shift != 0) {
4029 __ Asr(temp1, temp1, shift);
4030 }
4031
4032 if (instruction->IsDiv()) {
4033 __ Sub(out, temp1, Operand(temp1, vixl32::Shift(ASR), 31));
4034 } else {
4035 __ Sub(temp1, temp1, Operand(temp1, vixl32::Shift(ASR), 31));
4036 // TODO: Strength reduction for mls.
4037 __ Mov(temp2, imm);
4038 __ Mls(out, temp1, temp2, dividend);
4039 }
4040}
4041
4042void InstructionCodeGeneratorARMVIXL::GenerateDivRemConstantIntegral(
4043 HBinaryOperation* instruction) {
4044 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004045 DCHECK(instruction->GetResultType() == DataType::Type::kInt32);
Scott Wakelingfe885462016-09-22 10:24:38 +01004046
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004047 Location second = instruction->GetLocations()->InAt(1);
Scott Wakelingfe885462016-09-22 10:24:38 +01004048 DCHECK(second.IsConstant());
4049
Anton Kirilov644032c2016-12-06 17:51:43 +00004050 int32_t imm = Int32ConstantFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01004051 if (imm == 0) {
4052 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
4053 } else if (imm == 1 || imm == -1) {
4054 DivRemOneOrMinusOne(instruction);
4055 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
4056 DivRemByPowerOfTwo(instruction);
4057 } else {
4058 DCHECK(imm <= -2 || imm >= 2);
4059 GenerateDivRemWithAnyConstant(instruction);
4060 }
4061}
4062
4063void LocationsBuilderARMVIXL::VisitDiv(HDiv* div) {
4064 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004065 if (div->GetResultType() == DataType::Type::kInt64) {
Scott Wakelingfe885462016-09-22 10:24:38 +01004066 // pLdiv runtime call.
4067 call_kind = LocationSummary::kCallOnMainOnly;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004068 } else if (div->GetResultType() == DataType::Type::kInt32 && div->InputAt(1)->IsConstant()) {
Scott Wakelingfe885462016-09-22 10:24:38 +01004069 // sdiv will be replaced by other instruction sequence.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004070 } else if (div->GetResultType() == DataType::Type::kInt32 &&
Scott Wakelingfe885462016-09-22 10:24:38 +01004071 !codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4072 // pIdivmod runtime call.
4073 call_kind = LocationSummary::kCallOnMainOnly;
4074 }
4075
Vladimir Markoca6fff82017-10-03 14:49:14 +01004076 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(div, call_kind);
Scott Wakelingfe885462016-09-22 10:24:38 +01004077
4078 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004079 case DataType::Type::kInt32: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004080 if (div->InputAt(1)->IsConstant()) {
4081 locations->SetInAt(0, Location::RequiresRegister());
4082 locations->SetInAt(1, Location::ConstantLocation(div->InputAt(1)->AsConstant()));
4083 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Anton Kirilov644032c2016-12-06 17:51:43 +00004084 int32_t value = Int32ConstantFrom(div->InputAt(1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004085 if (value == 1 || value == 0 || value == -1) {
4086 // No temp register required.
4087 } else {
4088 locations->AddTemp(Location::RequiresRegister());
4089 if (!IsPowerOfTwo(AbsOrMin(value))) {
4090 locations->AddTemp(Location::RequiresRegister());
4091 }
4092 }
4093 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4094 locations->SetInAt(0, Location::RequiresRegister());
4095 locations->SetInAt(1, Location::RequiresRegister());
4096 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4097 } else {
Artem Serov551b28f2016-10-18 19:11:30 +01004098 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4099 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4100 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
Roland Levillain5e8d5f02016-10-18 18:03:43 +01004101 // Note: divmod will compute both the quotient and the remainder as the pair R0 and R1, but
Artem Serov551b28f2016-10-18 19:11:30 +01004102 // we only need the former.
4103 locations->SetOut(LocationFrom(r0));
Scott Wakelingfe885462016-09-22 10:24:38 +01004104 }
4105 break;
4106 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004107 case DataType::Type::kInt64: {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01004108 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4109 locations->SetInAt(0, LocationFrom(
4110 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
4111 locations->SetInAt(1, LocationFrom(
4112 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
4113 locations->SetOut(LocationFrom(r0, r1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004114 break;
4115 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004116 case DataType::Type::kFloat32:
4117 case DataType::Type::kFloat64: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004118 locations->SetInAt(0, Location::RequiresFpuRegister());
4119 locations->SetInAt(1, Location::RequiresFpuRegister());
4120 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4121 break;
4122 }
4123
4124 default:
4125 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
4126 }
4127}
4128
4129void InstructionCodeGeneratorARMVIXL::VisitDiv(HDiv* div) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01004130 Location lhs = div->GetLocations()->InAt(0);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004131 Location rhs = div->GetLocations()->InAt(1);
Scott Wakelingfe885462016-09-22 10:24:38 +01004132
4133 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004134 case DataType::Type::kInt32: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004135 if (rhs.IsConstant()) {
Scott Wakelingfe885462016-09-22 10:24:38 +01004136 GenerateDivRemConstantIntegral(div);
4137 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4138 __ Sdiv(OutputRegister(div), InputRegisterAt(div, 0), InputRegisterAt(div, 1));
4139 } else {
Artem Serov551b28f2016-10-18 19:11:30 +01004140 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4141 DCHECK(calling_convention.GetRegisterAt(0).Is(RegisterFrom(lhs)));
4142 DCHECK(calling_convention.GetRegisterAt(1).Is(RegisterFrom(rhs)));
4143 DCHECK(r0.Is(OutputRegister(div)));
4144
4145 codegen_->InvokeRuntime(kQuickIdivmod, div, div->GetDexPc());
4146 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
Scott Wakelingfe885462016-09-22 10:24:38 +01004147 }
4148 break;
4149 }
4150
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004151 case DataType::Type::kInt64: {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01004152 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4153 DCHECK(calling_convention.GetRegisterAt(0).Is(LowRegisterFrom(lhs)));
4154 DCHECK(calling_convention.GetRegisterAt(1).Is(HighRegisterFrom(lhs)));
4155 DCHECK(calling_convention.GetRegisterAt(2).Is(LowRegisterFrom(rhs)));
4156 DCHECK(calling_convention.GetRegisterAt(3).Is(HighRegisterFrom(rhs)));
4157 DCHECK(LowRegisterFrom(div->GetLocations()->Out()).Is(r0));
4158 DCHECK(HighRegisterFrom(div->GetLocations()->Out()).Is(r1));
4159
4160 codegen_->InvokeRuntime(kQuickLdiv, div, div->GetDexPc());
4161 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Scott Wakelingfe885462016-09-22 10:24:38 +01004162 break;
4163 }
4164
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004165 case DataType::Type::kFloat32:
4166 case DataType::Type::kFloat64:
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004167 __ Vdiv(OutputVRegister(div), InputVRegisterAt(div, 0), InputVRegisterAt(div, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004168 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01004169
4170 default:
4171 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
4172 }
4173}
4174
Artem Serov551b28f2016-10-18 19:11:30 +01004175void LocationsBuilderARMVIXL::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004176 DataType::Type type = rem->GetResultType();
Artem Serov551b28f2016-10-18 19:11:30 +01004177
4178 // Most remainders are implemented in the runtime.
4179 LocationSummary::CallKind call_kind = LocationSummary::kCallOnMainOnly;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004180 if (rem->GetResultType() == DataType::Type::kInt32 && rem->InputAt(1)->IsConstant()) {
Artem Serov551b28f2016-10-18 19:11:30 +01004181 // sdiv will be replaced by other instruction sequence.
4182 call_kind = LocationSummary::kNoCall;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004183 } else if ((rem->GetResultType() == DataType::Type::kInt32)
Artem Serov551b28f2016-10-18 19:11:30 +01004184 && codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4185 // Have hardware divide instruction for int, do it with three instructions.
4186 call_kind = LocationSummary::kNoCall;
4187 }
4188
Vladimir Markoca6fff82017-10-03 14:49:14 +01004189 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(rem, call_kind);
Artem Serov551b28f2016-10-18 19:11:30 +01004190
4191 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004192 case DataType::Type::kInt32: {
Artem Serov551b28f2016-10-18 19:11:30 +01004193 if (rem->InputAt(1)->IsConstant()) {
4194 locations->SetInAt(0, Location::RequiresRegister());
4195 locations->SetInAt(1, Location::ConstantLocation(rem->InputAt(1)->AsConstant()));
4196 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Anton Kirilov644032c2016-12-06 17:51:43 +00004197 int32_t value = Int32ConstantFrom(rem->InputAt(1));
Artem Serov551b28f2016-10-18 19:11:30 +01004198 if (value == 1 || value == 0 || value == -1) {
4199 // No temp register required.
4200 } else {
4201 locations->AddTemp(Location::RequiresRegister());
4202 if (!IsPowerOfTwo(AbsOrMin(value))) {
4203 locations->AddTemp(Location::RequiresRegister());
4204 }
4205 }
4206 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4207 locations->SetInAt(0, Location::RequiresRegister());
4208 locations->SetInAt(1, Location::RequiresRegister());
4209 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4210 locations->AddTemp(Location::RequiresRegister());
4211 } else {
4212 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4213 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4214 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
Roland Levillain5e8d5f02016-10-18 18:03:43 +01004215 // Note: divmod will compute both the quotient and the remainder as the pair R0 and R1, but
Artem Serov551b28f2016-10-18 19:11:30 +01004216 // we only need the latter.
4217 locations->SetOut(LocationFrom(r1));
4218 }
4219 break;
4220 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004221 case DataType::Type::kInt64: {
Artem Serov551b28f2016-10-18 19:11:30 +01004222 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4223 locations->SetInAt(0, LocationFrom(
4224 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
4225 locations->SetInAt(1, LocationFrom(
4226 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
4227 // The runtime helper puts the output in R2,R3.
4228 locations->SetOut(LocationFrom(r2, r3));
4229 break;
4230 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004231 case DataType::Type::kFloat32: {
Artem Serov551b28f2016-10-18 19:11:30 +01004232 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4233 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
4234 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
4235 locations->SetOut(LocationFrom(s0));
4236 break;
4237 }
4238
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004239 case DataType::Type::kFloat64: {
Artem Serov551b28f2016-10-18 19:11:30 +01004240 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4241 locations->SetInAt(0, LocationFrom(
4242 calling_convention.GetFpuRegisterAt(0), calling_convention.GetFpuRegisterAt(1)));
4243 locations->SetInAt(1, LocationFrom(
4244 calling_convention.GetFpuRegisterAt(2), calling_convention.GetFpuRegisterAt(3)));
4245 locations->SetOut(LocationFrom(s0, s1));
4246 break;
4247 }
4248
4249 default:
4250 LOG(FATAL) << "Unexpected rem type " << type;
4251 }
4252}
4253
4254void InstructionCodeGeneratorARMVIXL::VisitRem(HRem* rem) {
4255 LocationSummary* locations = rem->GetLocations();
4256 Location second = locations->InAt(1);
4257
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004258 DataType::Type type = rem->GetResultType();
Artem Serov551b28f2016-10-18 19:11:30 +01004259 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004260 case DataType::Type::kInt32: {
Artem Serov551b28f2016-10-18 19:11:30 +01004261 vixl32::Register reg1 = InputRegisterAt(rem, 0);
4262 vixl32::Register out_reg = OutputRegister(rem);
4263 if (second.IsConstant()) {
4264 GenerateDivRemConstantIntegral(rem);
4265 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4266 vixl32::Register reg2 = RegisterFrom(second);
4267 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
4268
4269 // temp = reg1 / reg2 (integer division)
4270 // dest = reg1 - temp * reg2
4271 __ Sdiv(temp, reg1, reg2);
4272 __ Mls(out_reg, temp, reg2, reg1);
4273 } else {
4274 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4275 DCHECK(reg1.Is(calling_convention.GetRegisterAt(0)));
4276 DCHECK(RegisterFrom(second).Is(calling_convention.GetRegisterAt(1)));
4277 DCHECK(out_reg.Is(r1));
4278
4279 codegen_->InvokeRuntime(kQuickIdivmod, rem, rem->GetDexPc());
4280 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
4281 }
4282 break;
4283 }
4284
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004285 case DataType::Type::kInt64: {
Artem Serov551b28f2016-10-18 19:11:30 +01004286 codegen_->InvokeRuntime(kQuickLmod, rem, rem->GetDexPc());
4287 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
4288 break;
4289 }
4290
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004291 case DataType::Type::kFloat32: {
Artem Serov551b28f2016-10-18 19:11:30 +01004292 codegen_->InvokeRuntime(kQuickFmodf, rem, rem->GetDexPc());
4293 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
4294 break;
4295 }
4296
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004297 case DataType::Type::kFloat64: {
Artem Serov551b28f2016-10-18 19:11:30 +01004298 codegen_->InvokeRuntime(kQuickFmod, rem, rem->GetDexPc());
4299 CheckEntrypointTypes<kQuickFmod, double, double, double>();
4300 break;
4301 }
4302
4303 default:
4304 LOG(FATAL) << "Unexpected rem type " << type;
4305 }
4306}
4307
Aart Bik1f8d51b2018-02-15 10:42:37 -08004308static void CreateMinMaxLocations(ArenaAllocator* allocator, HBinaryOperation* minmax) {
4309 LocationSummary* locations = new (allocator) LocationSummary(minmax);
4310 switch (minmax->GetResultType()) {
4311 case DataType::Type::kInt32:
4312 locations->SetInAt(0, Location::RequiresRegister());
4313 locations->SetInAt(1, Location::RequiresRegister());
4314 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4315 break;
4316 case DataType::Type::kInt64:
4317 locations->SetInAt(0, Location::RequiresRegister());
4318 locations->SetInAt(1, Location::RequiresRegister());
4319 locations->SetOut(Location::SameAsFirstInput());
4320 break;
4321 case DataType::Type::kFloat32:
4322 locations->SetInAt(0, Location::RequiresFpuRegister());
4323 locations->SetInAt(1, Location::RequiresFpuRegister());
4324 locations->SetOut(Location::SameAsFirstInput());
4325 locations->AddTemp(Location::RequiresRegister());
4326 break;
4327 case DataType::Type::kFloat64:
4328 locations->SetInAt(0, Location::RequiresFpuRegister());
4329 locations->SetInAt(1, Location::RequiresFpuRegister());
4330 locations->SetOut(Location::SameAsFirstInput());
4331 break;
4332 default:
4333 LOG(FATAL) << "Unexpected type for HMinMax " << minmax->GetResultType();
4334 }
4335}
4336
Aart Bik351df3e2018-03-07 11:54:57 -08004337void InstructionCodeGeneratorARMVIXL::GenerateMinMaxInt(LocationSummary* locations, bool is_min) {
Aart Bik1f8d51b2018-02-15 10:42:37 -08004338 Location op1_loc = locations->InAt(0);
4339 Location op2_loc = locations->InAt(1);
4340 Location out_loc = locations->Out();
4341
4342 vixl32::Register op1 = RegisterFrom(op1_loc);
4343 vixl32::Register op2 = RegisterFrom(op2_loc);
4344 vixl32::Register out = RegisterFrom(out_loc);
4345
4346 __ Cmp(op1, op2);
4347
4348 {
4349 ExactAssemblyScope aas(GetVIXLAssembler(),
4350 3 * kMaxInstructionSizeInBytes,
4351 CodeBufferCheckScope::kMaximumSize);
4352
4353 __ ite(is_min ? lt : gt);
4354 __ mov(is_min ? lt : gt, out, op1);
4355 __ mov(is_min ? ge : le, out, op2);
4356 }
4357}
4358
4359void InstructionCodeGeneratorARMVIXL::GenerateMinMaxLong(LocationSummary* locations, bool is_min) {
4360 Location op1_loc = locations->InAt(0);
4361 Location op2_loc = locations->InAt(1);
4362 Location out_loc = locations->Out();
4363
4364 // Optimization: don't generate any code if inputs are the same.
4365 if (op1_loc.Equals(op2_loc)) {
4366 DCHECK(out_loc.Equals(op1_loc)); // out_loc is set as SameAsFirstInput() in location builder.
4367 return;
4368 }
4369
4370 vixl32::Register op1_lo = LowRegisterFrom(op1_loc);
4371 vixl32::Register op1_hi = HighRegisterFrom(op1_loc);
4372 vixl32::Register op2_lo = LowRegisterFrom(op2_loc);
4373 vixl32::Register op2_hi = HighRegisterFrom(op2_loc);
4374 vixl32::Register out_lo = LowRegisterFrom(out_loc);
4375 vixl32::Register out_hi = HighRegisterFrom(out_loc);
4376 UseScratchRegisterScope temps(GetVIXLAssembler());
4377 const vixl32::Register temp = temps.Acquire();
4378
4379 DCHECK(op1_lo.Is(out_lo));
4380 DCHECK(op1_hi.Is(out_hi));
4381
4382 // Compare op1 >= op2, or op1 < op2.
4383 __ Cmp(out_lo, op2_lo);
4384 __ Sbcs(temp, out_hi, op2_hi);
4385
4386 // Now GE/LT condition code is correct for the long comparison.
4387 {
4388 vixl32::ConditionType cond = is_min ? ge : lt;
4389 ExactAssemblyScope it_scope(GetVIXLAssembler(),
4390 3 * kMaxInstructionSizeInBytes,
4391 CodeBufferCheckScope::kMaximumSize);
4392 __ itt(cond);
4393 __ mov(cond, out_lo, op2_lo);
4394 __ mov(cond, out_hi, op2_hi);
4395 }
4396}
4397
Aart Bik351df3e2018-03-07 11:54:57 -08004398void InstructionCodeGeneratorARMVIXL::GenerateMinMaxFloat(HInstruction* minmax, bool is_min) {
4399 LocationSummary* locations = minmax->GetLocations();
Aart Bik1f8d51b2018-02-15 10:42:37 -08004400 Location op1_loc = locations->InAt(0);
4401 Location op2_loc = locations->InAt(1);
4402 Location out_loc = locations->Out();
4403
4404 // Optimization: don't generate any code if inputs are the same.
4405 if (op1_loc.Equals(op2_loc)) {
4406 DCHECK(out_loc.Equals(op1_loc)); // out_loc is set as SameAsFirstInput() in location builder.
4407 return;
4408 }
4409
4410 vixl32::SRegister op1 = SRegisterFrom(op1_loc);
4411 vixl32::SRegister op2 = SRegisterFrom(op2_loc);
4412 vixl32::SRegister out = SRegisterFrom(out_loc);
4413
4414 UseScratchRegisterScope temps(GetVIXLAssembler());
4415 const vixl32::Register temp1 = temps.Acquire();
4416 vixl32::Register temp2 = RegisterFrom(locations->GetTemp(0));
4417 vixl32::Label nan, done;
Aart Bik351df3e2018-03-07 11:54:57 -08004418 vixl32::Label* final_label = codegen_->GetFinalLabel(minmax, &done);
Aart Bik1f8d51b2018-02-15 10:42:37 -08004419
4420 DCHECK(op1.Is(out));
4421
4422 __ Vcmp(op1, op2);
4423 __ Vmrs(RegisterOrAPSR_nzcv(kPcCode), FPSCR);
4424 __ B(vs, &nan, /* far_target */ false); // if un-ordered, go to NaN handling.
4425
4426 // op1 <> op2
4427 vixl32::ConditionType cond = is_min ? gt : lt;
4428 {
4429 ExactAssemblyScope it_scope(GetVIXLAssembler(),
4430 2 * kMaxInstructionSizeInBytes,
4431 CodeBufferCheckScope::kMaximumSize);
4432 __ it(cond);
4433 __ vmov(cond, F32, out, op2);
4434 }
4435 // for <>(not equal), we've done min/max calculation.
4436 __ B(ne, final_label, /* far_target */ false);
4437
4438 // handle op1 == op2, max(+0.0,-0.0), min(+0.0,-0.0).
4439 __ Vmov(temp1, op1);
4440 __ Vmov(temp2, op2);
4441 if (is_min) {
4442 __ Orr(temp1, temp1, temp2);
4443 } else {
4444 __ And(temp1, temp1, temp2);
4445 }
4446 __ Vmov(out, temp1);
4447 __ B(final_label);
4448
4449 // handle NaN input.
4450 __ Bind(&nan);
4451 __ Movt(temp1, High16Bits(kNanFloat)); // 0x7FC0xxxx is a NaN.
4452 __ Vmov(out, temp1);
4453
4454 if (done.IsReferenced()) {
4455 __ Bind(&done);
4456 }
4457}
4458
Aart Bik351df3e2018-03-07 11:54:57 -08004459void InstructionCodeGeneratorARMVIXL::GenerateMinMaxDouble(HInstruction* minmax, bool is_min) {
4460 LocationSummary* locations = minmax->GetLocations();
Aart Bik1f8d51b2018-02-15 10:42:37 -08004461 Location op1_loc = locations->InAt(0);
4462 Location op2_loc = locations->InAt(1);
4463 Location out_loc = locations->Out();
4464
4465 // Optimization: don't generate any code if inputs are the same.
4466 if (op1_loc.Equals(op2_loc)) {
4467 DCHECK(out_loc.Equals(op1_loc)); // out_loc is set as SameAsFirstInput() in.
4468 return;
4469 }
4470
4471 vixl32::DRegister op1 = DRegisterFrom(op1_loc);
4472 vixl32::DRegister op2 = DRegisterFrom(op2_loc);
4473 vixl32::DRegister out = DRegisterFrom(out_loc);
4474 vixl32::Label handle_nan_eq, done;
Aart Bik351df3e2018-03-07 11:54:57 -08004475 vixl32::Label* final_label = codegen_->GetFinalLabel(minmax, &done);
Aart Bik1f8d51b2018-02-15 10:42:37 -08004476
4477 DCHECK(op1.Is(out));
4478
4479 __ Vcmp(op1, op2);
4480 __ Vmrs(RegisterOrAPSR_nzcv(kPcCode), FPSCR);
4481 __ B(vs, &handle_nan_eq, /* far_target */ false); // if un-ordered, go to NaN handling.
4482
4483 // op1 <> op2
4484 vixl32::ConditionType cond = is_min ? gt : lt;
4485 {
4486 ExactAssemblyScope it_scope(GetVIXLAssembler(),
4487 2 * kMaxInstructionSizeInBytes,
4488 CodeBufferCheckScope::kMaximumSize);
4489 __ it(cond);
4490 __ vmov(cond, F64, out, op2);
4491 }
4492 // for <>(not equal), we've done min/max calculation.
4493 __ B(ne, final_label, /* far_target */ false);
4494
4495 // handle op1 == op2, max(+0.0,-0.0).
4496 if (!is_min) {
4497 __ Vand(F64, out, op1, op2);
4498 __ B(final_label);
4499 }
4500
4501 // handle op1 == op2, min(+0.0,-0.0), NaN input.
4502 __ Bind(&handle_nan_eq);
4503 __ Vorr(F64, out, op1, op2); // assemble op1/-0.0/NaN.
4504
4505 if (done.IsReferenced()) {
4506 __ Bind(&done);
4507 }
4508}
4509
Aart Bik351df3e2018-03-07 11:54:57 -08004510void InstructionCodeGeneratorARMVIXL::GenerateMinMax(HBinaryOperation* minmax, bool is_min) {
4511 DataType::Type type = minmax->GetResultType();
4512 switch (type) {
4513 case DataType::Type::kInt32:
4514 GenerateMinMaxInt(minmax->GetLocations(), is_min);
4515 break;
4516 case DataType::Type::kInt64:
4517 GenerateMinMaxLong(minmax->GetLocations(), is_min);
4518 break;
4519 case DataType::Type::kFloat32:
4520 GenerateMinMaxFloat(minmax, is_min);
4521 break;
4522 case DataType::Type::kFloat64:
4523 GenerateMinMaxDouble(minmax, is_min);
4524 break;
4525 default:
4526 LOG(FATAL) << "Unexpected type for HMinMax " << type;
4527 }
4528}
4529
Aart Bik1f8d51b2018-02-15 10:42:37 -08004530void LocationsBuilderARMVIXL::VisitMin(HMin* min) {
4531 CreateMinMaxLocations(GetGraph()->GetAllocator(), min);
4532}
4533
4534void InstructionCodeGeneratorARMVIXL::VisitMin(HMin* min) {
Aart Bik351df3e2018-03-07 11:54:57 -08004535 GenerateMinMax(min, /*is_min*/ true);
Aart Bik1f8d51b2018-02-15 10:42:37 -08004536}
4537
4538void LocationsBuilderARMVIXL::VisitMax(HMax* max) {
4539 CreateMinMaxLocations(GetGraph()->GetAllocator(), max);
4540}
4541
4542void InstructionCodeGeneratorARMVIXL::VisitMax(HMax* max) {
Aart Bik351df3e2018-03-07 11:54:57 -08004543 GenerateMinMax(max, /*is_min*/ false);
Aart Bik1f8d51b2018-02-15 10:42:37 -08004544}
4545
Aart Bik3dad3412018-02-28 12:01:46 -08004546void LocationsBuilderARMVIXL::VisitAbs(HAbs* abs) {
4547 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(abs);
4548 switch (abs->GetResultType()) {
4549 case DataType::Type::kInt32:
4550 case DataType::Type::kInt64:
4551 locations->SetInAt(0, Location::RequiresRegister());
4552 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4553 locations->AddTemp(Location::RequiresRegister());
4554 break;
4555 case DataType::Type::kFloat32:
4556 case DataType::Type::kFloat64:
4557 locations->SetInAt(0, Location::RequiresFpuRegister());
4558 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4559 break;
4560 default:
4561 LOG(FATAL) << "Unexpected type for abs operation " << abs->GetResultType();
4562 }
4563}
4564
4565void InstructionCodeGeneratorARMVIXL::VisitAbs(HAbs* abs) {
4566 LocationSummary* locations = abs->GetLocations();
4567 switch (abs->GetResultType()) {
4568 case DataType::Type::kInt32: {
4569 vixl32::Register in_reg = RegisterFrom(locations->InAt(0));
4570 vixl32::Register out_reg = RegisterFrom(locations->Out());
4571 vixl32::Register mask = RegisterFrom(locations->GetTemp(0));
4572 __ Asr(mask, in_reg, 31);
4573 __ Add(out_reg, in_reg, mask);
4574 __ Eor(out_reg, out_reg, mask);
4575 break;
4576 }
4577 case DataType::Type::kInt64: {
4578 Location in = locations->InAt(0);
4579 vixl32::Register in_reg_lo = LowRegisterFrom(in);
4580 vixl32::Register in_reg_hi = HighRegisterFrom(in);
4581 Location output = locations->Out();
4582 vixl32::Register out_reg_lo = LowRegisterFrom(output);
4583 vixl32::Register out_reg_hi = HighRegisterFrom(output);
4584 DCHECK(!out_reg_lo.Is(in_reg_hi)) << "Diagonal overlap unexpected.";
4585 vixl32::Register mask = RegisterFrom(locations->GetTemp(0));
4586 __ Asr(mask, in_reg_hi, 31);
4587 __ Adds(out_reg_lo, in_reg_lo, mask);
4588 __ Adc(out_reg_hi, in_reg_hi, mask);
4589 __ Eor(out_reg_lo, out_reg_lo, mask);
4590 __ Eor(out_reg_hi, out_reg_hi, mask);
4591 break;
4592 }
4593 case DataType::Type::kFloat32:
4594 case DataType::Type::kFloat64:
4595 __ Vabs(OutputVRegister(abs), InputVRegisterAt(abs, 0));
4596 break;
4597 default:
4598 LOG(FATAL) << "Unexpected type for abs operation " << abs->GetResultType();
4599 }
4600}
Artem Serov551b28f2016-10-18 19:11:30 +01004601
Scott Wakelingfe885462016-09-22 10:24:38 +01004602void LocationsBuilderARMVIXL::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Artem Serov657022c2016-11-23 14:19:38 +00004603 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Scott Wakelingfe885462016-09-22 10:24:38 +01004604 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Scott Wakelingfe885462016-09-22 10:24:38 +01004605}
4606
4607void InstructionCodeGeneratorARMVIXL::VisitDivZeroCheck(HDivZeroCheck* instruction) {
4608 DivZeroCheckSlowPathARMVIXL* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01004609 new (codegen_->GetScopedAllocator()) DivZeroCheckSlowPathARMVIXL(instruction);
Scott Wakelingfe885462016-09-22 10:24:38 +01004610 codegen_->AddSlowPath(slow_path);
4611
4612 LocationSummary* locations = instruction->GetLocations();
4613 Location value = locations->InAt(0);
4614
4615 switch (instruction->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004616 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004617 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004618 case DataType::Type::kInt8:
4619 case DataType::Type::kUint16:
4620 case DataType::Type::kInt16:
4621 case DataType::Type::kInt32: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004622 if (value.IsRegister()) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00004623 __ CompareAndBranchIfZero(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
Scott Wakelingfe885462016-09-22 10:24:38 +01004624 } else {
4625 DCHECK(value.IsConstant()) << value;
Anton Kirilov644032c2016-12-06 17:51:43 +00004626 if (Int32ConstantFrom(value) == 0) {
Scott Wakelingfe885462016-09-22 10:24:38 +01004627 __ B(slow_path->GetEntryLabel());
4628 }
4629 }
4630 break;
4631 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004632 case DataType::Type::kInt64: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004633 if (value.IsRegisterPair()) {
4634 UseScratchRegisterScope temps(GetVIXLAssembler());
4635 vixl32::Register temp = temps.Acquire();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004636 __ Orrs(temp, LowRegisterFrom(value), HighRegisterFrom(value));
Scott Wakelingfe885462016-09-22 10:24:38 +01004637 __ B(eq, slow_path->GetEntryLabel());
4638 } else {
4639 DCHECK(value.IsConstant()) << value;
Anton Kirilov644032c2016-12-06 17:51:43 +00004640 if (Int64ConstantFrom(value) == 0) {
Scott Wakelingfe885462016-09-22 10:24:38 +01004641 __ B(slow_path->GetEntryLabel());
4642 }
4643 }
4644 break;
4645 }
4646 default:
4647 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
4648 }
4649}
4650
Artem Serov02109dd2016-09-23 17:17:54 +01004651void InstructionCodeGeneratorARMVIXL::HandleIntegerRotate(HRor* ror) {
4652 LocationSummary* locations = ror->GetLocations();
4653 vixl32::Register in = InputRegisterAt(ror, 0);
4654 Location rhs = locations->InAt(1);
4655 vixl32::Register out = OutputRegister(ror);
4656
4657 if (rhs.IsConstant()) {
4658 // Arm32 and Thumb2 assemblers require a rotation on the interval [1,31],
4659 // so map all rotations to a +ve. equivalent in that range.
4660 // (e.g. left *or* right by -2 bits == 30 bits in the same direction.)
4661 uint32_t rot = CodeGenerator::GetInt32ValueOf(rhs.GetConstant()) & 0x1F;
4662 if (rot) {
4663 // Rotate, mapping left rotations to right equivalents if necessary.
4664 // (e.g. left by 2 bits == right by 30.)
4665 __ Ror(out, in, rot);
4666 } else if (!out.Is(in)) {
4667 __ Mov(out, in);
4668 }
4669 } else {
4670 __ Ror(out, in, RegisterFrom(rhs));
4671 }
4672}
4673
4674// Gain some speed by mapping all Long rotates onto equivalent pairs of Integer
4675// rotates by swapping input regs (effectively rotating by the first 32-bits of
4676// a larger rotation) or flipping direction (thus treating larger right/left
4677// rotations as sub-word sized rotations in the other direction) as appropriate.
4678void InstructionCodeGeneratorARMVIXL::HandleLongRotate(HRor* ror) {
4679 LocationSummary* locations = ror->GetLocations();
4680 vixl32::Register in_reg_lo = LowRegisterFrom(locations->InAt(0));
4681 vixl32::Register in_reg_hi = HighRegisterFrom(locations->InAt(0));
4682 Location rhs = locations->InAt(1);
4683 vixl32::Register out_reg_lo = LowRegisterFrom(locations->Out());
4684 vixl32::Register out_reg_hi = HighRegisterFrom(locations->Out());
4685
4686 if (rhs.IsConstant()) {
4687 uint64_t rot = CodeGenerator::GetInt64ValueOf(rhs.GetConstant());
4688 // Map all rotations to +ve. equivalents on the interval [0,63].
4689 rot &= kMaxLongShiftDistance;
4690 // For rotates over a word in size, 'pre-rotate' by 32-bits to keep rotate
4691 // logic below to a simple pair of binary orr.
4692 // (e.g. 34 bits == in_reg swap + 2 bits right.)
4693 if (rot >= kArmBitsPerWord) {
4694 rot -= kArmBitsPerWord;
4695 std::swap(in_reg_hi, in_reg_lo);
4696 }
4697 // Rotate, or mov to out for zero or word size rotations.
4698 if (rot != 0u) {
Scott Wakelingb77051e2016-11-21 19:46:00 +00004699 __ Lsr(out_reg_hi, in_reg_hi, Operand::From(rot));
Artem Serov02109dd2016-09-23 17:17:54 +01004700 __ Orr(out_reg_hi, out_reg_hi, Operand(in_reg_lo, ShiftType::LSL, kArmBitsPerWord - rot));
Scott Wakelingb77051e2016-11-21 19:46:00 +00004701 __ Lsr(out_reg_lo, in_reg_lo, Operand::From(rot));
Artem Serov02109dd2016-09-23 17:17:54 +01004702 __ Orr(out_reg_lo, out_reg_lo, Operand(in_reg_hi, ShiftType::LSL, kArmBitsPerWord - rot));
4703 } else {
4704 __ Mov(out_reg_lo, in_reg_lo);
4705 __ Mov(out_reg_hi, in_reg_hi);
4706 }
4707 } else {
4708 vixl32::Register shift_right = RegisterFrom(locations->GetTemp(0));
4709 vixl32::Register shift_left = RegisterFrom(locations->GetTemp(1));
4710 vixl32::Label end;
4711 vixl32::Label shift_by_32_plus_shift_right;
Anton Kirilov6f644202017-02-27 18:29:45 +00004712 vixl32::Label* final_label = codegen_->GetFinalLabel(ror, &end);
Artem Serov02109dd2016-09-23 17:17:54 +01004713
4714 __ And(shift_right, RegisterFrom(rhs), 0x1F);
4715 __ Lsrs(shift_left, RegisterFrom(rhs), 6);
Scott Wakelingbffdc702016-12-07 17:46:03 +00004716 __ Rsb(LeaveFlags, shift_left, shift_right, Operand::From(kArmBitsPerWord));
Artem Serov517d9f62016-12-12 15:51:15 +00004717 __ B(cc, &shift_by_32_plus_shift_right, /* far_target */ false);
Artem Serov02109dd2016-09-23 17:17:54 +01004718
4719 // out_reg_hi = (reg_hi << shift_left) | (reg_lo >> shift_right).
4720 // out_reg_lo = (reg_lo << shift_left) | (reg_hi >> shift_right).
4721 __ Lsl(out_reg_hi, in_reg_hi, shift_left);
4722 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
4723 __ Add(out_reg_hi, out_reg_hi, out_reg_lo);
4724 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
4725 __ Lsr(shift_left, in_reg_hi, shift_right);
4726 __ Add(out_reg_lo, out_reg_lo, shift_left);
Anton Kirilov6f644202017-02-27 18:29:45 +00004727 __ B(final_label);
Artem Serov02109dd2016-09-23 17:17:54 +01004728
4729 __ Bind(&shift_by_32_plus_shift_right); // Shift by 32+shift_right.
4730 // out_reg_hi = (reg_hi >> shift_right) | (reg_lo << shift_left).
4731 // out_reg_lo = (reg_lo >> shift_right) | (reg_hi << shift_left).
4732 __ Lsr(out_reg_hi, in_reg_hi, shift_right);
4733 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
4734 __ Add(out_reg_hi, out_reg_hi, out_reg_lo);
4735 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
4736 __ Lsl(shift_right, in_reg_hi, shift_left);
4737 __ Add(out_reg_lo, out_reg_lo, shift_right);
4738
Anton Kirilov6f644202017-02-27 18:29:45 +00004739 if (end.IsReferenced()) {
4740 __ Bind(&end);
4741 }
Artem Serov02109dd2016-09-23 17:17:54 +01004742 }
4743}
4744
4745void LocationsBuilderARMVIXL::VisitRor(HRor* ror) {
4746 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004747 new (GetGraph()->GetAllocator()) LocationSummary(ror, LocationSummary::kNoCall);
Artem Serov02109dd2016-09-23 17:17:54 +01004748 switch (ror->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004749 case DataType::Type::kInt32: {
Artem Serov02109dd2016-09-23 17:17:54 +01004750 locations->SetInAt(0, Location::RequiresRegister());
4751 locations->SetInAt(1, Location::RegisterOrConstant(ror->InputAt(1)));
4752 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4753 break;
4754 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004755 case DataType::Type::kInt64: {
Artem Serov02109dd2016-09-23 17:17:54 +01004756 locations->SetInAt(0, Location::RequiresRegister());
4757 if (ror->InputAt(1)->IsConstant()) {
4758 locations->SetInAt(1, Location::ConstantLocation(ror->InputAt(1)->AsConstant()));
4759 } else {
4760 locations->SetInAt(1, Location::RequiresRegister());
4761 locations->AddTemp(Location::RequiresRegister());
4762 locations->AddTemp(Location::RequiresRegister());
4763 }
4764 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
4765 break;
4766 }
4767 default:
4768 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
4769 }
4770}
4771
4772void InstructionCodeGeneratorARMVIXL::VisitRor(HRor* ror) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004773 DataType::Type type = ror->GetResultType();
Artem Serov02109dd2016-09-23 17:17:54 +01004774 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004775 case DataType::Type::kInt32: {
Artem Serov02109dd2016-09-23 17:17:54 +01004776 HandleIntegerRotate(ror);
4777 break;
4778 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004779 case DataType::Type::kInt64: {
Artem Serov02109dd2016-09-23 17:17:54 +01004780 HandleLongRotate(ror);
4781 break;
4782 }
4783 default:
4784 LOG(FATAL) << "Unexpected operation type " << type;
4785 UNREACHABLE();
4786 }
4787}
4788
Artem Serov02d37832016-10-25 15:25:33 +01004789void LocationsBuilderARMVIXL::HandleShift(HBinaryOperation* op) {
4790 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
4791
4792 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004793 new (GetGraph()->GetAllocator()) LocationSummary(op, LocationSummary::kNoCall);
Artem Serov02d37832016-10-25 15:25:33 +01004794
4795 switch (op->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004796 case DataType::Type::kInt32: {
Artem Serov02d37832016-10-25 15:25:33 +01004797 locations->SetInAt(0, Location::RequiresRegister());
4798 if (op->InputAt(1)->IsConstant()) {
4799 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
4800 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4801 } else {
4802 locations->SetInAt(1, Location::RequiresRegister());
4803 // Make the output overlap, as it will be used to hold the masked
4804 // second input.
4805 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
4806 }
4807 break;
4808 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004809 case DataType::Type::kInt64: {
Artem Serov02d37832016-10-25 15:25:33 +01004810 locations->SetInAt(0, Location::RequiresRegister());
4811 if (op->InputAt(1)->IsConstant()) {
4812 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
4813 // For simplicity, use kOutputOverlap even though we only require that low registers
4814 // don't clash with high registers which the register allocator currently guarantees.
4815 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
4816 } else {
4817 locations->SetInAt(1, Location::RequiresRegister());
4818 locations->AddTemp(Location::RequiresRegister());
4819 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
4820 }
4821 break;
4822 }
4823 default:
4824 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
4825 }
4826}
4827
4828void InstructionCodeGeneratorARMVIXL::HandleShift(HBinaryOperation* op) {
4829 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
4830
4831 LocationSummary* locations = op->GetLocations();
4832 Location out = locations->Out();
4833 Location first = locations->InAt(0);
4834 Location second = locations->InAt(1);
4835
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004836 DataType::Type type = op->GetResultType();
Artem Serov02d37832016-10-25 15:25:33 +01004837 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004838 case DataType::Type::kInt32: {
Artem Serov02d37832016-10-25 15:25:33 +01004839 vixl32::Register out_reg = OutputRegister(op);
4840 vixl32::Register first_reg = InputRegisterAt(op, 0);
4841 if (second.IsRegister()) {
4842 vixl32::Register second_reg = RegisterFrom(second);
4843 // ARM doesn't mask the shift count so we need to do it ourselves.
4844 __ And(out_reg, second_reg, kMaxIntShiftDistance);
4845 if (op->IsShl()) {
4846 __ Lsl(out_reg, first_reg, out_reg);
4847 } else if (op->IsShr()) {
4848 __ Asr(out_reg, first_reg, out_reg);
4849 } else {
4850 __ Lsr(out_reg, first_reg, out_reg);
4851 }
4852 } else {
Anton Kirilov644032c2016-12-06 17:51:43 +00004853 int32_t cst = Int32ConstantFrom(second);
Artem Serov02d37832016-10-25 15:25:33 +01004854 uint32_t shift_value = cst & kMaxIntShiftDistance;
4855 if (shift_value == 0) { // ARM does not support shifting with 0 immediate.
4856 __ Mov(out_reg, first_reg);
4857 } else if (op->IsShl()) {
4858 __ Lsl(out_reg, first_reg, shift_value);
4859 } else if (op->IsShr()) {
4860 __ Asr(out_reg, first_reg, shift_value);
4861 } else {
4862 __ Lsr(out_reg, first_reg, shift_value);
4863 }
4864 }
4865 break;
4866 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004867 case DataType::Type::kInt64: {
Artem Serov02d37832016-10-25 15:25:33 +01004868 vixl32::Register o_h = HighRegisterFrom(out);
4869 vixl32::Register o_l = LowRegisterFrom(out);
4870
4871 vixl32::Register high = HighRegisterFrom(first);
4872 vixl32::Register low = LowRegisterFrom(first);
4873
4874 if (second.IsRegister()) {
4875 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
4876
4877 vixl32::Register second_reg = RegisterFrom(second);
4878
4879 if (op->IsShl()) {
4880 __ And(o_l, second_reg, kMaxLongShiftDistance);
4881 // Shift the high part
4882 __ Lsl(o_h, high, o_l);
4883 // Shift the low part and `or` what overflew on the high part
Scott Wakelingb77051e2016-11-21 19:46:00 +00004884 __ Rsb(temp, o_l, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01004885 __ Lsr(temp, low, temp);
4886 __ Orr(o_h, o_h, temp);
4887 // If the shift is > 32 bits, override the high part
Scott Wakelingb77051e2016-11-21 19:46:00 +00004888 __ Subs(temp, o_l, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01004889 {
Artem Serov0fb37192016-12-06 18:13:40 +00004890 ExactAssemblyScope guard(GetVIXLAssembler(),
4891 2 * vixl32::kMaxInstructionSizeInBytes,
4892 CodeBufferCheckScope::kMaximumSize);
Artem Serov02d37832016-10-25 15:25:33 +01004893 __ it(pl);
4894 __ lsl(pl, o_h, low, temp);
4895 }
4896 // Shift the low part
4897 __ Lsl(o_l, low, o_l);
4898 } else if (op->IsShr()) {
4899 __ And(o_h, second_reg, kMaxLongShiftDistance);
4900 // Shift the low part
4901 __ Lsr(o_l, low, o_h);
4902 // Shift the high part and `or` what underflew on the low part
Scott Wakelingb77051e2016-11-21 19:46:00 +00004903 __ Rsb(temp, o_h, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01004904 __ Lsl(temp, high, temp);
4905 __ Orr(o_l, o_l, temp);
4906 // If the shift is > 32 bits, override the low part
Scott Wakelingb77051e2016-11-21 19:46:00 +00004907 __ Subs(temp, o_h, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01004908 {
Artem Serov0fb37192016-12-06 18:13:40 +00004909 ExactAssemblyScope guard(GetVIXLAssembler(),
4910 2 * vixl32::kMaxInstructionSizeInBytes,
4911 CodeBufferCheckScope::kMaximumSize);
Artem Serov02d37832016-10-25 15:25:33 +01004912 __ it(pl);
4913 __ asr(pl, o_l, high, temp);
4914 }
4915 // Shift the high part
4916 __ Asr(o_h, high, o_h);
4917 } else {
4918 __ And(o_h, second_reg, kMaxLongShiftDistance);
4919 // same as Shr except we use `Lsr`s and not `Asr`s
4920 __ Lsr(o_l, low, o_h);
Scott Wakelingb77051e2016-11-21 19:46:00 +00004921 __ Rsb(temp, o_h, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01004922 __ Lsl(temp, high, temp);
4923 __ Orr(o_l, o_l, temp);
Scott Wakelingb77051e2016-11-21 19:46:00 +00004924 __ Subs(temp, o_h, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01004925 {
Artem Serov0fb37192016-12-06 18:13:40 +00004926 ExactAssemblyScope guard(GetVIXLAssembler(),
4927 2 * vixl32::kMaxInstructionSizeInBytes,
4928 CodeBufferCheckScope::kMaximumSize);
Artem Serov02d37832016-10-25 15:25:33 +01004929 __ it(pl);
4930 __ lsr(pl, o_l, high, temp);
4931 }
4932 __ Lsr(o_h, high, o_h);
4933 }
4934 } else {
4935 // Register allocator doesn't create partial overlap.
4936 DCHECK(!o_l.Is(high));
4937 DCHECK(!o_h.Is(low));
Anton Kirilov644032c2016-12-06 17:51:43 +00004938 int32_t cst = Int32ConstantFrom(second);
Artem Serov02d37832016-10-25 15:25:33 +01004939 uint32_t shift_value = cst & kMaxLongShiftDistance;
4940 if (shift_value > 32) {
4941 if (op->IsShl()) {
4942 __ Lsl(o_h, low, shift_value - 32);
4943 __ Mov(o_l, 0);
4944 } else if (op->IsShr()) {
4945 __ Asr(o_l, high, shift_value - 32);
4946 __ Asr(o_h, high, 31);
4947 } else {
4948 __ Lsr(o_l, high, shift_value - 32);
4949 __ Mov(o_h, 0);
4950 }
4951 } else if (shift_value == 32) {
4952 if (op->IsShl()) {
4953 __ Mov(o_h, low);
4954 __ Mov(o_l, 0);
4955 } else if (op->IsShr()) {
4956 __ Mov(o_l, high);
4957 __ Asr(o_h, high, 31);
4958 } else {
4959 __ Mov(o_l, high);
4960 __ Mov(o_h, 0);
4961 }
4962 } else if (shift_value == 1) {
4963 if (op->IsShl()) {
4964 __ Lsls(o_l, low, 1);
4965 __ Adc(o_h, high, high);
4966 } else if (op->IsShr()) {
4967 __ Asrs(o_h, high, 1);
4968 __ Rrx(o_l, low);
4969 } else {
4970 __ Lsrs(o_h, high, 1);
4971 __ Rrx(o_l, low);
4972 }
4973 } else {
Nicolas Geoffrayacc56ac2018-10-09 08:45:24 +01004974 DCHECK(0 <= shift_value && shift_value < 32) << shift_value;
Artem Serov02d37832016-10-25 15:25:33 +01004975 if (op->IsShl()) {
4976 __ Lsl(o_h, high, shift_value);
4977 __ Orr(o_h, o_h, Operand(low, ShiftType::LSR, 32 - shift_value));
4978 __ Lsl(o_l, low, shift_value);
4979 } else if (op->IsShr()) {
4980 __ Lsr(o_l, low, shift_value);
4981 __ Orr(o_l, o_l, Operand(high, ShiftType::LSL, 32 - shift_value));
4982 __ Asr(o_h, high, shift_value);
4983 } else {
4984 __ Lsr(o_l, low, shift_value);
4985 __ Orr(o_l, o_l, Operand(high, ShiftType::LSL, 32 - shift_value));
4986 __ Lsr(o_h, high, shift_value);
4987 }
4988 }
4989 }
4990 break;
4991 }
4992 default:
4993 LOG(FATAL) << "Unexpected operation type " << type;
4994 UNREACHABLE();
4995 }
4996}
4997
4998void LocationsBuilderARMVIXL::VisitShl(HShl* shl) {
4999 HandleShift(shl);
5000}
5001
5002void InstructionCodeGeneratorARMVIXL::VisitShl(HShl* shl) {
5003 HandleShift(shl);
5004}
5005
5006void LocationsBuilderARMVIXL::VisitShr(HShr* shr) {
5007 HandleShift(shr);
5008}
5009
5010void InstructionCodeGeneratorARMVIXL::VisitShr(HShr* shr) {
5011 HandleShift(shr);
5012}
5013
5014void LocationsBuilderARMVIXL::VisitUShr(HUShr* ushr) {
5015 HandleShift(ushr);
5016}
5017
5018void InstructionCodeGeneratorARMVIXL::VisitUShr(HUShr* ushr) {
5019 HandleShift(ushr);
5020}
5021
5022void LocationsBuilderARMVIXL::VisitNewInstance(HNewInstance* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005023 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5024 instruction, LocationSummary::kCallOnMainOnly);
Alex Lightd109e302018-06-27 10:25:41 -07005025 InvokeRuntimeCallingConventionARMVIXL calling_convention;
5026 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
Artem Serov02d37832016-10-25 15:25:33 +01005027 locations->SetOut(LocationFrom(r0));
5028}
5029
5030void InstructionCodeGeneratorARMVIXL::VisitNewInstance(HNewInstance* instruction) {
Alex Lightd109e302018-06-27 10:25:41 -07005031 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
5032 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
Orion Hodson4c8e12e2018-05-18 08:33:20 +01005033 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 11);
Artem Serov02d37832016-10-25 15:25:33 +01005034}
5035
5036void LocationsBuilderARMVIXL::VisitNewArray(HNewArray* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005037 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5038 instruction, LocationSummary::kCallOnMainOnly);
Artem Serov02d37832016-10-25 15:25:33 +01005039 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Artem Serov02d37832016-10-25 15:25:33 +01005040 locations->SetOut(LocationFrom(r0));
Nicolas Geoffray8c7c4f12017-01-26 10:13:11 +00005041 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5042 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
Artem Serov02d37832016-10-25 15:25:33 +01005043}
5044
5045void InstructionCodeGeneratorARMVIXL::VisitNewArray(HNewArray* instruction) {
Vladimir Markob5461632018-10-15 14:24:21 +01005046 // Note: if heap poisoning is enabled, the entry point takes care of poisoning the reference.
5047 QuickEntrypointEnum entrypoint = CodeGenerator::GetArrayAllocationEntrypoint(instruction);
Artem Serov7b3672e2017-02-03 17:30:34 +00005048 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005049 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Artem Serov7b3672e2017-02-03 17:30:34 +00005050 DCHECK(!codegen_->IsLeafMethod());
Orion Hodson4c8e12e2018-05-18 08:33:20 +01005051 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 12);
Artem Serov02d37832016-10-25 15:25:33 +01005052}
5053
5054void LocationsBuilderARMVIXL::VisitParameterValue(HParameterValue* instruction) {
5055 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005056 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Artem Serov02d37832016-10-25 15:25:33 +01005057 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
5058 if (location.IsStackSlot()) {
5059 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5060 } else if (location.IsDoubleStackSlot()) {
5061 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5062 }
5063 locations->SetOut(location);
5064}
5065
5066void InstructionCodeGeneratorARMVIXL::VisitParameterValue(
5067 HParameterValue* instruction ATTRIBUTE_UNUSED) {
5068 // Nothing to do, the parameter is already at its location.
5069}
5070
5071void LocationsBuilderARMVIXL::VisitCurrentMethod(HCurrentMethod* instruction) {
5072 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005073 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Artem Serov02d37832016-10-25 15:25:33 +01005074 locations->SetOut(LocationFrom(kMethodRegister));
5075}
5076
5077void InstructionCodeGeneratorARMVIXL::VisitCurrentMethod(
5078 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
5079 // Nothing to do, the method is already at its location.
5080}
5081
5082void LocationsBuilderARMVIXL::VisitNot(HNot* not_) {
5083 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005084 new (GetGraph()->GetAllocator()) LocationSummary(not_, LocationSummary::kNoCall);
Artem Serov02d37832016-10-25 15:25:33 +01005085 locations->SetInAt(0, Location::RequiresRegister());
5086 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5087}
5088
5089void InstructionCodeGeneratorARMVIXL::VisitNot(HNot* not_) {
5090 LocationSummary* locations = not_->GetLocations();
5091 Location out = locations->Out();
5092 Location in = locations->InAt(0);
5093 switch (not_->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005094 case DataType::Type::kInt32:
Artem Serov02d37832016-10-25 15:25:33 +01005095 __ Mvn(OutputRegister(not_), InputRegisterAt(not_, 0));
5096 break;
5097
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005098 case DataType::Type::kInt64:
Artem Serov02d37832016-10-25 15:25:33 +01005099 __ Mvn(LowRegisterFrom(out), LowRegisterFrom(in));
5100 __ Mvn(HighRegisterFrom(out), HighRegisterFrom(in));
5101 break;
5102
5103 default:
5104 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
5105 }
5106}
5107
Scott Wakelingc34dba72016-10-03 10:14:44 +01005108void LocationsBuilderARMVIXL::VisitBooleanNot(HBooleanNot* bool_not) {
5109 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005110 new (GetGraph()->GetAllocator()) LocationSummary(bool_not, LocationSummary::kNoCall);
Scott Wakelingc34dba72016-10-03 10:14:44 +01005111 locations->SetInAt(0, Location::RequiresRegister());
5112 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5113}
5114
5115void InstructionCodeGeneratorARMVIXL::VisitBooleanNot(HBooleanNot* bool_not) {
5116 __ Eor(OutputRegister(bool_not), InputRegister(bool_not), 1);
5117}
5118
Artem Serov02d37832016-10-25 15:25:33 +01005119void LocationsBuilderARMVIXL::VisitCompare(HCompare* compare) {
5120 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005121 new (GetGraph()->GetAllocator()) LocationSummary(compare, LocationSummary::kNoCall);
Artem Serov02d37832016-10-25 15:25:33 +01005122 switch (compare->InputAt(0)->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005123 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005124 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005125 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005126 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005127 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005128 case DataType::Type::kInt32:
5129 case DataType::Type::kInt64: {
Artem Serov02d37832016-10-25 15:25:33 +01005130 locations->SetInAt(0, Location::RequiresRegister());
5131 locations->SetInAt(1, Location::RequiresRegister());
5132 // Output overlaps because it is written before doing the low comparison.
5133 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
5134 break;
5135 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005136 case DataType::Type::kFloat32:
5137 case DataType::Type::kFloat64: {
Artem Serov02d37832016-10-25 15:25:33 +01005138 locations->SetInAt(0, Location::RequiresFpuRegister());
5139 locations->SetInAt(1, ArithmeticZeroOrFpuRegister(compare->InputAt(1)));
5140 locations->SetOut(Location::RequiresRegister());
5141 break;
5142 }
5143 default:
5144 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
5145 }
5146}
5147
5148void InstructionCodeGeneratorARMVIXL::VisitCompare(HCompare* compare) {
5149 LocationSummary* locations = compare->GetLocations();
5150 vixl32::Register out = OutputRegister(compare);
5151 Location left = locations->InAt(0);
5152 Location right = locations->InAt(1);
5153
5154 vixl32::Label less, greater, done;
Anton Kirilov6f644202017-02-27 18:29:45 +00005155 vixl32::Label* final_label = codegen_->GetFinalLabel(compare, &done);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005156 DataType::Type type = compare->InputAt(0)->GetType();
Vladimir Marko33bff252017-11-01 14:35:42 +00005157 vixl32::Condition less_cond = vixl32::Condition::None();
Artem Serov02d37832016-10-25 15:25:33 +01005158 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005159 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005160 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005161 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005162 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005163 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005164 case DataType::Type::kInt32: {
Artem Serov02d37832016-10-25 15:25:33 +01005165 // Emit move to `out` before the `Cmp`, as `Mov` might affect the status flags.
5166 __ Mov(out, 0);
5167 __ Cmp(RegisterFrom(left), RegisterFrom(right)); // Signed compare.
5168 less_cond = lt;
5169 break;
5170 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005171 case DataType::Type::kInt64: {
Artem Serov02d37832016-10-25 15:25:33 +01005172 __ Cmp(HighRegisterFrom(left), HighRegisterFrom(right)); // Signed compare.
Artem Serov517d9f62016-12-12 15:51:15 +00005173 __ B(lt, &less, /* far_target */ false);
5174 __ B(gt, &greater, /* far_target */ false);
Artem Serov02d37832016-10-25 15:25:33 +01005175 // Emit move to `out` before the last `Cmp`, as `Mov` might affect the status flags.
5176 __ Mov(out, 0);
5177 __ Cmp(LowRegisterFrom(left), LowRegisterFrom(right)); // Unsigned compare.
5178 less_cond = lo;
5179 break;
5180 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005181 case DataType::Type::kFloat32:
5182 case DataType::Type::kFloat64: {
Artem Serov02d37832016-10-25 15:25:33 +01005183 __ Mov(out, 0);
Donghui Bai426b49c2016-11-08 14:55:38 +08005184 GenerateVcmp(compare, codegen_);
Artem Serov02d37832016-10-25 15:25:33 +01005185 // To branch on the FP compare result we transfer FPSCR to APSR (encoded as PC in VMRS).
5186 __ Vmrs(RegisterOrAPSR_nzcv(kPcCode), FPSCR);
5187 less_cond = ARMFPCondition(kCondLT, compare->IsGtBias());
5188 break;
5189 }
5190 default:
5191 LOG(FATAL) << "Unexpected compare type " << type;
5192 UNREACHABLE();
5193 }
5194
Anton Kirilov6f644202017-02-27 18:29:45 +00005195 __ B(eq, final_label, /* far_target */ false);
Artem Serov517d9f62016-12-12 15:51:15 +00005196 __ B(less_cond, &less, /* far_target */ false);
Artem Serov02d37832016-10-25 15:25:33 +01005197
5198 __ Bind(&greater);
5199 __ Mov(out, 1);
Anton Kirilov6f644202017-02-27 18:29:45 +00005200 __ B(final_label);
Artem Serov02d37832016-10-25 15:25:33 +01005201
5202 __ Bind(&less);
5203 __ Mov(out, -1);
5204
Anton Kirilov6f644202017-02-27 18:29:45 +00005205 if (done.IsReferenced()) {
5206 __ Bind(&done);
5207 }
Artem Serov02d37832016-10-25 15:25:33 +01005208}
5209
5210void LocationsBuilderARMVIXL::VisitPhi(HPhi* instruction) {
5211 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005212 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Artem Serov02d37832016-10-25 15:25:33 +01005213 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
5214 locations->SetInAt(i, Location::Any());
5215 }
5216 locations->SetOut(Location::Any());
5217}
5218
5219void InstructionCodeGeneratorARMVIXL::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
5220 LOG(FATAL) << "Unreachable";
5221}
5222
5223void CodeGeneratorARMVIXL::GenerateMemoryBarrier(MemBarrierKind kind) {
5224 // TODO (ported from quick): revisit ARM barrier kinds.
5225 DmbOptions flavor = DmbOptions::ISH; // Quiet C++ warnings.
5226 switch (kind) {
5227 case MemBarrierKind::kAnyStore:
5228 case MemBarrierKind::kLoadAny:
5229 case MemBarrierKind::kAnyAny: {
5230 flavor = DmbOptions::ISH;
5231 break;
5232 }
5233 case MemBarrierKind::kStoreStore: {
5234 flavor = DmbOptions::ISHST;
5235 break;
5236 }
5237 default:
5238 LOG(FATAL) << "Unexpected memory barrier " << kind;
5239 }
5240 __ Dmb(flavor);
5241}
5242
5243void InstructionCodeGeneratorARMVIXL::GenerateWideAtomicLoad(vixl32::Register addr,
5244 uint32_t offset,
5245 vixl32::Register out_lo,
5246 vixl32::Register out_hi) {
5247 UseScratchRegisterScope temps(GetVIXLAssembler());
5248 if (offset != 0) {
5249 vixl32::Register temp = temps.Acquire();
5250 __ Add(temp, addr, offset);
5251 addr = temp;
5252 }
Scott Wakelingb77051e2016-11-21 19:46:00 +00005253 __ Ldrexd(out_lo, out_hi, MemOperand(addr));
Artem Serov02d37832016-10-25 15:25:33 +01005254}
5255
5256void InstructionCodeGeneratorARMVIXL::GenerateWideAtomicStore(vixl32::Register addr,
5257 uint32_t offset,
5258 vixl32::Register value_lo,
5259 vixl32::Register value_hi,
5260 vixl32::Register temp1,
5261 vixl32::Register temp2,
5262 HInstruction* instruction) {
5263 UseScratchRegisterScope temps(GetVIXLAssembler());
5264 vixl32::Label fail;
5265 if (offset != 0) {
5266 vixl32::Register temp = temps.Acquire();
5267 __ Add(temp, addr, offset);
5268 addr = temp;
5269 }
5270 __ Bind(&fail);
Alexandre Rames374ddf32016-11-04 10:40:49 +00005271 {
5272 // Ensure the pc position is recorded immediately after the `ldrexd` instruction.
Artem Serov0fb37192016-12-06 18:13:40 +00005273 ExactAssemblyScope aas(GetVIXLAssembler(),
5274 vixl32::kMaxInstructionSizeInBytes,
5275 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00005276 // We need a load followed by store. (The address used in a STREX instruction must
5277 // be the same as the address in the most recently executed LDREX instruction.)
5278 __ ldrexd(temp1, temp2, MemOperand(addr));
5279 codegen_->MaybeRecordImplicitNullCheck(instruction);
5280 }
Scott Wakelingb77051e2016-11-21 19:46:00 +00005281 __ Strexd(temp1, value_lo, value_hi, MemOperand(addr));
xueliang.zhongf51bc622016-11-04 09:23:32 +00005282 __ CompareAndBranchIfNonZero(temp1, &fail);
Artem Serov02d37832016-10-25 15:25:33 +01005283}
Artem Serov02109dd2016-09-23 17:17:54 +01005284
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005285void LocationsBuilderARMVIXL::HandleFieldSet(
5286 HInstruction* instruction, const FieldInfo& field_info) {
5287 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
5288
5289 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005290 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005291 locations->SetInAt(0, Location::RequiresRegister());
5292
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005293 DataType::Type field_type = field_info.GetFieldType();
5294 if (DataType::IsFloatingPointType(field_type)) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005295 locations->SetInAt(1, Location::RequiresFpuRegister());
5296 } else {
5297 locations->SetInAt(1, Location::RequiresRegister());
5298 }
5299
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005300 bool is_wide = field_type == DataType::Type::kInt64 || field_type == DataType::Type::kFloat64;
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005301 bool generate_volatile = field_info.IsVolatile()
5302 && is_wide
5303 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
5304 bool needs_write_barrier =
5305 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
5306 // Temporary registers for the write barrier.
5307 // TODO: consider renaming StoreNeedsWriteBarrier to StoreNeedsGCMark.
5308 if (needs_write_barrier) {
5309 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
5310 locations->AddTemp(Location::RequiresRegister());
5311 } else if (generate_volatile) {
5312 // ARM encoding have some additional constraints for ldrexd/strexd:
5313 // - registers need to be consecutive
5314 // - the first register should be even but not R14.
5315 // We don't test for ARM yet, and the assertion makes sure that we
5316 // revisit this if we ever enable ARM encoding.
5317 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
5318
5319 locations->AddTemp(Location::RequiresRegister());
5320 locations->AddTemp(Location::RequiresRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005321 if (field_type == DataType::Type::kFloat64) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005322 // For doubles we need two more registers to copy the value.
5323 locations->AddTemp(LocationFrom(r2));
5324 locations->AddTemp(LocationFrom(r3));
5325 }
5326 }
5327}
5328
5329void InstructionCodeGeneratorARMVIXL::HandleFieldSet(HInstruction* instruction,
5330 const FieldInfo& field_info,
5331 bool value_can_be_null) {
5332 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
5333
5334 LocationSummary* locations = instruction->GetLocations();
5335 vixl32::Register base = InputRegisterAt(instruction, 0);
5336 Location value = locations->InAt(1);
5337
5338 bool is_volatile = field_info.IsVolatile();
5339 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005340 DataType::Type field_type = field_info.GetFieldType();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005341 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
5342 bool needs_write_barrier =
5343 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
5344
5345 if (is_volatile) {
5346 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
5347 }
5348
5349 switch (field_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005350 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005351 case DataType::Type::kUint8:
5352 case DataType::Type::kInt8:
5353 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005354 case DataType::Type::kInt16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005355 case DataType::Type::kInt32: {
5356 StoreOperandType operand_type = GetStoreOperandType(field_type);
5357 GetAssembler()->StoreToOffset(operand_type, RegisterFrom(value), base, offset);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005358 break;
5359 }
5360
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005361 case DataType::Type::kReference: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005362 if (kPoisonHeapReferences && needs_write_barrier) {
5363 // Note that in the case where `value` is a null reference,
5364 // we do not enter this block, as a null reference does not
5365 // need poisoning.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005366 DCHECK_EQ(field_type, DataType::Type::kReference);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005367 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
5368 __ Mov(temp, RegisterFrom(value));
5369 GetAssembler()->PoisonHeapReference(temp);
5370 GetAssembler()->StoreToOffset(kStoreWord, temp, base, offset);
5371 } else {
5372 GetAssembler()->StoreToOffset(kStoreWord, RegisterFrom(value), base, offset);
5373 }
5374 break;
5375 }
5376
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005377 case DataType::Type::kInt64: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005378 if (is_volatile && !atomic_ldrd_strd) {
5379 GenerateWideAtomicStore(base,
5380 offset,
5381 LowRegisterFrom(value),
5382 HighRegisterFrom(value),
5383 RegisterFrom(locations->GetTemp(0)),
5384 RegisterFrom(locations->GetTemp(1)),
5385 instruction);
5386 } else {
5387 GetAssembler()->StoreToOffset(kStoreWordPair, LowRegisterFrom(value), base, offset);
5388 codegen_->MaybeRecordImplicitNullCheck(instruction);
5389 }
5390 break;
5391 }
5392
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005393 case DataType::Type::kFloat32: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005394 GetAssembler()->StoreSToOffset(SRegisterFrom(value), base, offset);
5395 break;
5396 }
5397
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005398 case DataType::Type::kFloat64: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01005399 vixl32::DRegister value_reg = DRegisterFrom(value);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005400 if (is_volatile && !atomic_ldrd_strd) {
5401 vixl32::Register value_reg_lo = RegisterFrom(locations->GetTemp(0));
5402 vixl32::Register value_reg_hi = RegisterFrom(locations->GetTemp(1));
5403
5404 __ Vmov(value_reg_lo, value_reg_hi, value_reg);
5405
5406 GenerateWideAtomicStore(base,
5407 offset,
5408 value_reg_lo,
5409 value_reg_hi,
5410 RegisterFrom(locations->GetTemp(2)),
5411 RegisterFrom(locations->GetTemp(3)),
5412 instruction);
5413 } else {
5414 GetAssembler()->StoreDToOffset(value_reg, base, offset);
5415 codegen_->MaybeRecordImplicitNullCheck(instruction);
5416 }
5417 break;
5418 }
5419
Aart Bik66c158e2018-01-31 12:55:04 -08005420 case DataType::Type::kUint32:
5421 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005422 case DataType::Type::kVoid:
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005423 LOG(FATAL) << "Unreachable type " << field_type;
5424 UNREACHABLE();
5425 }
5426
5427 // Longs and doubles are handled in the switch.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005428 if (field_type != DataType::Type::kInt64 && field_type != DataType::Type::kFloat64) {
Alexandre Rames374ddf32016-11-04 10:40:49 +00005429 // TODO(VIXL): Here and for other calls to `MaybeRecordImplicitNullCheck` in this method, we
5430 // should use a scope and the assembler to emit the store instruction to guarantee that we
5431 // record the pc at the correct position. But the `Assembler` does not automatically handle
5432 // unencodable offsets. Practically, everything is fine because the helper and VIXL, at the time
5433 // of writing, do generate the store instruction last.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005434 codegen_->MaybeRecordImplicitNullCheck(instruction);
5435 }
5436
5437 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
5438 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
5439 vixl32::Register card = RegisterFrom(locations->GetTemp(1));
5440 codegen_->MarkGCCard(temp, card, base, RegisterFrom(value), value_can_be_null);
5441 }
5442
5443 if (is_volatile) {
5444 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
5445 }
5446}
5447
Artem Serov02d37832016-10-25 15:25:33 +01005448void LocationsBuilderARMVIXL::HandleFieldGet(HInstruction* instruction,
5449 const FieldInfo& field_info) {
5450 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
5451
5452 bool object_field_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005453 kEmitCompilerReadBarrier && (field_info.GetFieldType() == DataType::Type::kReference);
Artem Serov02d37832016-10-25 15:25:33 +01005454 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005455 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
5456 object_field_get_with_read_barrier
5457 ? LocationSummary::kCallOnSlowPath
5458 : LocationSummary::kNoCall);
Artem Serov02d37832016-10-25 15:25:33 +01005459 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
5460 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
5461 }
5462 locations->SetInAt(0, Location::RequiresRegister());
5463
5464 bool volatile_for_double = field_info.IsVolatile()
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005465 && (field_info.GetFieldType() == DataType::Type::kFloat64)
Artem Serov02d37832016-10-25 15:25:33 +01005466 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
5467 // The output overlaps in case of volatile long: we don't want the
5468 // code generated by GenerateWideAtomicLoad to overwrite the
5469 // object's location. Likewise, in the case of an object field get
5470 // with read barriers enabled, we do not want the load to overwrite
5471 // the object's location, as we need it to emit the read barrier.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005472 bool overlap =
5473 (field_info.IsVolatile() && (field_info.GetFieldType() == DataType::Type::kInt64)) ||
Artem Serov02d37832016-10-25 15:25:33 +01005474 object_field_get_with_read_barrier;
5475
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005476 if (DataType::IsFloatingPointType(instruction->GetType())) {
Artem Serov02d37832016-10-25 15:25:33 +01005477 locations->SetOut(Location::RequiresFpuRegister());
5478 } else {
5479 locations->SetOut(Location::RequiresRegister(),
5480 (overlap ? Location::kOutputOverlap : Location::kNoOutputOverlap));
5481 }
5482 if (volatile_for_double) {
5483 // ARM encoding have some additional constraints for ldrexd/strexd:
5484 // - registers need to be consecutive
5485 // - the first register should be even but not R14.
5486 // We don't test for ARM yet, and the assertion makes sure that we
5487 // revisit this if we ever enable ARM encoding.
5488 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
5489 locations->AddTemp(Location::RequiresRegister());
5490 locations->AddTemp(Location::RequiresRegister());
5491 } else if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko008e09f32018-08-06 15:42:43 +01005492 // We need a temporary register for the read barrier load in
5493 // CodeGeneratorARMVIXL::GenerateFieldLoadWithBakerReadBarrier()
5494 // only if the offset is too big.
5495 if (field_info.GetFieldOffset().Uint32Value() >= kReferenceLoadMinFarOffset) {
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01005496 locations->AddTemp(Location::RequiresRegister());
5497 }
Artem Serov02d37832016-10-25 15:25:33 +01005498 }
5499}
5500
5501Location LocationsBuilderARMVIXL::ArithmeticZeroOrFpuRegister(HInstruction* input) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005502 DCHECK(DataType::IsFloatingPointType(input->GetType())) << input->GetType();
Artem Serov02d37832016-10-25 15:25:33 +01005503 if ((input->IsFloatConstant() && (input->AsFloatConstant()->IsArithmeticZero())) ||
5504 (input->IsDoubleConstant() && (input->AsDoubleConstant()->IsArithmeticZero()))) {
5505 return Location::ConstantLocation(input->AsConstant());
5506 } else {
5507 return Location::RequiresFpuRegister();
5508 }
5509}
5510
Artem Serov02109dd2016-09-23 17:17:54 +01005511Location LocationsBuilderARMVIXL::ArmEncodableConstantOrRegister(HInstruction* constant,
5512 Opcode opcode) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005513 DCHECK(!DataType::IsFloatingPointType(constant->GetType()));
Artem Serov02109dd2016-09-23 17:17:54 +01005514 if (constant->IsConstant() &&
5515 CanEncodeConstantAsImmediate(constant->AsConstant(), opcode)) {
5516 return Location::ConstantLocation(constant->AsConstant());
5517 }
5518 return Location::RequiresRegister();
5519}
5520
Vladimir Markof0a6a1d2018-01-08 14:23:56 +00005521static bool CanEncode32BitConstantAsImmediate(
5522 CodeGeneratorARMVIXL* codegen,
5523 uint32_t value,
5524 Opcode opcode,
5525 vixl32::FlagsUpdate flags_update = vixl32::FlagsUpdate::DontCare) {
5526 ArmVIXLAssembler* assembler = codegen->GetAssembler();
5527 if (assembler->ShifterOperandCanHold(opcode, value, flags_update)) {
Artem Serov02109dd2016-09-23 17:17:54 +01005528 return true;
5529 }
5530 Opcode neg_opcode = kNoOperand;
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00005531 uint32_t neg_value = 0;
Artem Serov02109dd2016-09-23 17:17:54 +01005532 switch (opcode) {
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00005533 case AND: neg_opcode = BIC; neg_value = ~value; break;
5534 case ORR: neg_opcode = ORN; neg_value = ~value; break;
5535 case ADD: neg_opcode = SUB; neg_value = -value; break;
5536 case ADC: neg_opcode = SBC; neg_value = ~value; break;
5537 case SUB: neg_opcode = ADD; neg_value = -value; break;
5538 case SBC: neg_opcode = ADC; neg_value = ~value; break;
5539 case MOV: neg_opcode = MVN; neg_value = ~value; break;
Artem Serov02109dd2016-09-23 17:17:54 +01005540 default:
5541 return false;
5542 }
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00005543
Vladimir Markof0a6a1d2018-01-08 14:23:56 +00005544 if (assembler->ShifterOperandCanHold(neg_opcode, neg_value, flags_update)) {
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00005545 return true;
5546 }
5547
5548 return opcode == AND && IsPowerOfTwo(value + 1);
Artem Serov02109dd2016-09-23 17:17:54 +01005549}
5550
Vladimir Markof0a6a1d2018-01-08 14:23:56 +00005551bool LocationsBuilderARMVIXL::CanEncodeConstantAsImmediate(HConstant* input_cst, Opcode opcode) {
5552 uint64_t value = static_cast<uint64_t>(Int64FromConstant(input_cst));
5553 if (DataType::Is64BitType(input_cst->GetType())) {
5554 Opcode high_opcode = opcode;
5555 vixl32::FlagsUpdate low_flags_update = vixl32::FlagsUpdate::DontCare;
5556 switch (opcode) {
5557 case SUB:
5558 // Flip the operation to an ADD.
5559 value = -value;
5560 opcode = ADD;
5561 FALLTHROUGH_INTENDED;
5562 case ADD:
5563 if (Low32Bits(value) == 0u) {
5564 return CanEncode32BitConstantAsImmediate(codegen_, High32Bits(value), opcode);
5565 }
5566 high_opcode = ADC;
5567 low_flags_update = vixl32::FlagsUpdate::SetFlags;
5568 break;
5569 default:
5570 break;
5571 }
5572 return CanEncode32BitConstantAsImmediate(codegen_, High32Bits(value), high_opcode) &&
5573 CanEncode32BitConstantAsImmediate(codegen_, Low32Bits(value), opcode, low_flags_update);
5574 } else {
5575 return CanEncode32BitConstantAsImmediate(codegen_, Low32Bits(value), opcode);
5576 }
5577}
5578
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005579void InstructionCodeGeneratorARMVIXL::HandleFieldGet(HInstruction* instruction,
5580 const FieldInfo& field_info) {
5581 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
5582
5583 LocationSummary* locations = instruction->GetLocations();
5584 vixl32::Register base = InputRegisterAt(instruction, 0);
5585 Location out = locations->Out();
5586 bool is_volatile = field_info.IsVolatile();
5587 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Vladimir Marko61b92282017-10-11 13:23:17 +01005588 DCHECK_EQ(DataType::Size(field_info.GetFieldType()), DataType::Size(instruction->GetType()));
5589 DataType::Type load_type = instruction->GetType();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005590 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
5591
Vladimir Marko61b92282017-10-11 13:23:17 +01005592 switch (load_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005593 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005594 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005595 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005596 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005597 case DataType::Type::kInt16:
5598 case DataType::Type::kInt32: {
Vladimir Marko61b92282017-10-11 13:23:17 +01005599 LoadOperandType operand_type = GetLoadOperandType(load_type);
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005600 GetAssembler()->LoadFromOffset(operand_type, RegisterFrom(out), base, offset);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005601 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005602 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005603
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005604 case DataType::Type::kReference: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005605 // /* HeapReference<Object> */ out = *(base + offset)
5606 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Vladimir Markodcd117e2018-04-19 11:54:00 +01005607 Location maybe_temp = (locations->GetTempCount() != 0) ? locations->GetTemp(0) : Location();
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005608 // Note that a potential implicit null check is handled in this
5609 // CodeGeneratorARMVIXL::GenerateFieldLoadWithBakerReadBarrier call.
5610 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Markodcd117e2018-04-19 11:54:00 +01005611 instruction, out, base, offset, maybe_temp, /* needs_null_check */ true);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005612 if (is_volatile) {
5613 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5614 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005615 } else {
5616 GetAssembler()->LoadFromOffset(kLoadWord, RegisterFrom(out), base, offset);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005617 codegen_->MaybeRecordImplicitNullCheck(instruction);
5618 if (is_volatile) {
5619 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5620 }
5621 // If read barriers are enabled, emit read barriers other than
5622 // Baker's using a slow path (and also unpoison the loaded
5623 // reference, if heap poisoning is enabled).
5624 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, locations->InAt(0), offset);
5625 }
5626 break;
5627 }
5628
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005629 case DataType::Type::kInt64:
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005630 if (is_volatile && !atomic_ldrd_strd) {
5631 GenerateWideAtomicLoad(base, offset, LowRegisterFrom(out), HighRegisterFrom(out));
5632 } else {
5633 GetAssembler()->LoadFromOffset(kLoadWordPair, LowRegisterFrom(out), base, offset);
5634 }
5635 break;
5636
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005637 case DataType::Type::kFloat32:
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005638 GetAssembler()->LoadSFromOffset(SRegisterFrom(out), base, offset);
5639 break;
5640
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005641 case DataType::Type::kFloat64: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01005642 vixl32::DRegister out_dreg = DRegisterFrom(out);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005643 if (is_volatile && !atomic_ldrd_strd) {
5644 vixl32::Register lo = RegisterFrom(locations->GetTemp(0));
5645 vixl32::Register hi = RegisterFrom(locations->GetTemp(1));
5646 GenerateWideAtomicLoad(base, offset, lo, hi);
5647 // TODO(VIXL): Do we need to be immediately after the ldrexd instruction? If so we need a
5648 // scope.
5649 codegen_->MaybeRecordImplicitNullCheck(instruction);
5650 __ Vmov(out_dreg, lo, hi);
5651 } else {
5652 GetAssembler()->LoadDFromOffset(out_dreg, base, offset);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005653 codegen_->MaybeRecordImplicitNullCheck(instruction);
5654 }
5655 break;
5656 }
5657
Aart Bik66c158e2018-01-31 12:55:04 -08005658 case DataType::Type::kUint32:
5659 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005660 case DataType::Type::kVoid:
Vladimir Marko61b92282017-10-11 13:23:17 +01005661 LOG(FATAL) << "Unreachable type " << load_type;
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005662 UNREACHABLE();
5663 }
5664
Vladimir Marko61b92282017-10-11 13:23:17 +01005665 if (load_type == DataType::Type::kReference || load_type == DataType::Type::kFloat64) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005666 // Potential implicit null checks, in the case of reference or
5667 // double fields, are handled in the previous switch statement.
5668 } else {
5669 // Address cases other than reference and double that may require an implicit null check.
Alexandre Rames374ddf32016-11-04 10:40:49 +00005670 // TODO(VIXL): Here and for other calls to `MaybeRecordImplicitNullCheck` in this method, we
5671 // should use a scope and the assembler to emit the load instruction to guarantee that we
5672 // record the pc at the correct position. But the `Assembler` does not automatically handle
5673 // unencodable offsets. Practically, everything is fine because the helper and VIXL, at the time
5674 // of writing, do generate the store instruction last.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005675 codegen_->MaybeRecordImplicitNullCheck(instruction);
5676 }
5677
5678 if (is_volatile) {
Vladimir Marko61b92282017-10-11 13:23:17 +01005679 if (load_type == DataType::Type::kReference) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005680 // Memory barriers, in the case of references, are also handled
5681 // in the previous switch statement.
5682 } else {
5683 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5684 }
5685 }
5686}
5687
5688void LocationsBuilderARMVIXL::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
5689 HandleFieldSet(instruction, instruction->GetFieldInfo());
5690}
5691
5692void InstructionCodeGeneratorARMVIXL::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
5693 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
5694}
5695
5696void LocationsBuilderARMVIXL::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5697 HandleFieldGet(instruction, instruction->GetFieldInfo());
5698}
5699
5700void InstructionCodeGeneratorARMVIXL::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5701 HandleFieldGet(instruction, instruction->GetFieldInfo());
5702}
5703
5704void LocationsBuilderARMVIXL::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5705 HandleFieldGet(instruction, instruction->GetFieldInfo());
5706}
5707
5708void InstructionCodeGeneratorARMVIXL::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5709 HandleFieldGet(instruction, instruction->GetFieldInfo());
5710}
5711
Scott Wakelingc34dba72016-10-03 10:14:44 +01005712void LocationsBuilderARMVIXL::VisitStaticFieldSet(HStaticFieldSet* instruction) {
5713 HandleFieldSet(instruction, instruction->GetFieldInfo());
5714}
5715
5716void InstructionCodeGeneratorARMVIXL::VisitStaticFieldSet(HStaticFieldSet* instruction) {
5717 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
5718}
5719
Artem Serovcfbe9132016-10-14 15:58:56 +01005720void LocationsBuilderARMVIXL::VisitUnresolvedInstanceFieldGet(
5721 HUnresolvedInstanceFieldGet* instruction) {
5722 FieldAccessCallingConventionARMVIXL calling_convention;
5723 codegen_->CreateUnresolvedFieldLocationSummary(
5724 instruction, instruction->GetFieldType(), calling_convention);
5725}
5726
5727void InstructionCodeGeneratorARMVIXL::VisitUnresolvedInstanceFieldGet(
5728 HUnresolvedInstanceFieldGet* instruction) {
5729 FieldAccessCallingConventionARMVIXL calling_convention;
5730 codegen_->GenerateUnresolvedFieldAccess(instruction,
5731 instruction->GetFieldType(),
5732 instruction->GetFieldIndex(),
5733 instruction->GetDexPc(),
5734 calling_convention);
5735}
5736
5737void LocationsBuilderARMVIXL::VisitUnresolvedInstanceFieldSet(
5738 HUnresolvedInstanceFieldSet* instruction) {
5739 FieldAccessCallingConventionARMVIXL calling_convention;
5740 codegen_->CreateUnresolvedFieldLocationSummary(
5741 instruction, instruction->GetFieldType(), calling_convention);
5742}
5743
5744void InstructionCodeGeneratorARMVIXL::VisitUnresolvedInstanceFieldSet(
5745 HUnresolvedInstanceFieldSet* instruction) {
5746 FieldAccessCallingConventionARMVIXL calling_convention;
5747 codegen_->GenerateUnresolvedFieldAccess(instruction,
5748 instruction->GetFieldType(),
5749 instruction->GetFieldIndex(),
5750 instruction->GetDexPc(),
5751 calling_convention);
5752}
5753
5754void LocationsBuilderARMVIXL::VisitUnresolvedStaticFieldGet(
5755 HUnresolvedStaticFieldGet* instruction) {
5756 FieldAccessCallingConventionARMVIXL calling_convention;
5757 codegen_->CreateUnresolvedFieldLocationSummary(
5758 instruction, instruction->GetFieldType(), calling_convention);
5759}
5760
5761void InstructionCodeGeneratorARMVIXL::VisitUnresolvedStaticFieldGet(
5762 HUnresolvedStaticFieldGet* instruction) {
5763 FieldAccessCallingConventionARMVIXL calling_convention;
5764 codegen_->GenerateUnresolvedFieldAccess(instruction,
5765 instruction->GetFieldType(),
5766 instruction->GetFieldIndex(),
5767 instruction->GetDexPc(),
5768 calling_convention);
5769}
5770
5771void LocationsBuilderARMVIXL::VisitUnresolvedStaticFieldSet(
5772 HUnresolvedStaticFieldSet* instruction) {
5773 FieldAccessCallingConventionARMVIXL calling_convention;
5774 codegen_->CreateUnresolvedFieldLocationSummary(
5775 instruction, instruction->GetFieldType(), calling_convention);
5776}
5777
5778void InstructionCodeGeneratorARMVIXL::VisitUnresolvedStaticFieldSet(
5779 HUnresolvedStaticFieldSet* instruction) {
5780 FieldAccessCallingConventionARMVIXL calling_convention;
5781 codegen_->GenerateUnresolvedFieldAccess(instruction,
5782 instruction->GetFieldType(),
5783 instruction->GetFieldIndex(),
5784 instruction->GetDexPc(),
5785 calling_convention);
5786}
5787
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005788void LocationsBuilderARMVIXL::VisitNullCheck(HNullCheck* instruction) {
Artem Serov657022c2016-11-23 14:19:38 +00005789 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005790 locations->SetInAt(0, Location::RequiresRegister());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005791}
5792
5793void CodeGeneratorARMVIXL::GenerateImplicitNullCheck(HNullCheck* instruction) {
5794 if (CanMoveNullCheckToUser(instruction)) {
5795 return;
5796 }
5797
5798 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames374ddf32016-11-04 10:40:49 +00005799 // Ensure the pc position is recorded immediately after the `ldr` instruction.
Artem Serov0fb37192016-12-06 18:13:40 +00005800 ExactAssemblyScope aas(GetVIXLAssembler(),
5801 vixl32::kMaxInstructionSizeInBytes,
5802 CodeBufferCheckScope::kMaximumSize);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005803 __ ldr(temps.Acquire(), MemOperand(InputRegisterAt(instruction, 0)));
5804 RecordPcInfo(instruction, instruction->GetDexPc());
5805}
5806
5807void CodeGeneratorARMVIXL::GenerateExplicitNullCheck(HNullCheck* instruction) {
5808 NullCheckSlowPathARMVIXL* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01005809 new (GetScopedAllocator()) NullCheckSlowPathARMVIXL(instruction);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005810 AddSlowPath(slow_path);
xueliang.zhongf51bc622016-11-04 09:23:32 +00005811 __ CompareAndBranchIfZero(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005812}
5813
5814void InstructionCodeGeneratorARMVIXL::VisitNullCheck(HNullCheck* instruction) {
5815 codegen_->GenerateNullCheck(instruction);
5816}
5817
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005818void CodeGeneratorARMVIXL::LoadFromShiftedRegOffset(DataType::Type type,
Scott Wakelingc34dba72016-10-03 10:14:44 +01005819 Location out_loc,
5820 vixl32::Register base,
5821 vixl32::Register reg_index,
5822 vixl32::Condition cond) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005823 uint32_t shift_count = DataType::SizeShift(type);
Scott Wakelingc34dba72016-10-03 10:14:44 +01005824 MemOperand mem_address(base, reg_index, vixl32::LSL, shift_count);
5825
5826 switch (type) {
Vladimir Marko61b92282017-10-11 13:23:17 +01005827 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005828 case DataType::Type::kUint8:
Vladimir Marko61b92282017-10-11 13:23:17 +01005829 __ Ldrb(cond, RegisterFrom(out_loc), mem_address);
5830 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005831 case DataType::Type::kInt8:
Scott Wakelingc34dba72016-10-03 10:14:44 +01005832 __ Ldrsb(cond, RegisterFrom(out_loc), mem_address);
5833 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005834 case DataType::Type::kUint16:
Scott Wakelingc34dba72016-10-03 10:14:44 +01005835 __ Ldrh(cond, RegisterFrom(out_loc), mem_address);
5836 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005837 case DataType::Type::kInt16:
5838 __ Ldrsh(cond, RegisterFrom(out_loc), mem_address);
5839 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005840 case DataType::Type::kReference:
5841 case DataType::Type::kInt32:
Scott Wakelingc34dba72016-10-03 10:14:44 +01005842 __ Ldr(cond, RegisterFrom(out_loc), mem_address);
5843 break;
5844 // T32 doesn't support LoadFromShiftedRegOffset mem address mode for these types.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005845 case DataType::Type::kInt64:
5846 case DataType::Type::kFloat32:
5847 case DataType::Type::kFloat64:
Scott Wakelingc34dba72016-10-03 10:14:44 +01005848 default:
5849 LOG(FATAL) << "Unreachable type " << type;
5850 UNREACHABLE();
5851 }
5852}
5853
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005854void CodeGeneratorARMVIXL::StoreToShiftedRegOffset(DataType::Type type,
Scott Wakelingc34dba72016-10-03 10:14:44 +01005855 Location loc,
5856 vixl32::Register base,
5857 vixl32::Register reg_index,
5858 vixl32::Condition cond) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005859 uint32_t shift_count = DataType::SizeShift(type);
Scott Wakelingc34dba72016-10-03 10:14:44 +01005860 MemOperand mem_address(base, reg_index, vixl32::LSL, shift_count);
5861
5862 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005863 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005864 case DataType::Type::kUint8:
5865 case DataType::Type::kInt8:
Scott Wakelingc34dba72016-10-03 10:14:44 +01005866 __ Strb(cond, RegisterFrom(loc), mem_address);
5867 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005868 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005869 case DataType::Type::kInt16:
Scott Wakelingc34dba72016-10-03 10:14:44 +01005870 __ Strh(cond, RegisterFrom(loc), mem_address);
5871 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005872 case DataType::Type::kReference:
5873 case DataType::Type::kInt32:
Scott Wakelingc34dba72016-10-03 10:14:44 +01005874 __ Str(cond, RegisterFrom(loc), mem_address);
5875 break;
5876 // T32 doesn't support StoreToShiftedRegOffset mem address mode for these types.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005877 case DataType::Type::kInt64:
5878 case DataType::Type::kFloat32:
5879 case DataType::Type::kFloat64:
Scott Wakelingc34dba72016-10-03 10:14:44 +01005880 default:
5881 LOG(FATAL) << "Unreachable type " << type;
5882 UNREACHABLE();
5883 }
5884}
5885
5886void LocationsBuilderARMVIXL::VisitArrayGet(HArrayGet* instruction) {
5887 bool object_array_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005888 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Scott Wakelingc34dba72016-10-03 10:14:44 +01005889 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005890 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
5891 object_array_get_with_read_barrier
5892 ? LocationSummary::kCallOnSlowPath
5893 : LocationSummary::kNoCall);
Scott Wakelingc34dba72016-10-03 10:14:44 +01005894 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005895 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Scott Wakelingc34dba72016-10-03 10:14:44 +01005896 }
5897 locations->SetInAt(0, Location::RequiresRegister());
5898 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005899 if (DataType::IsFloatingPointType(instruction->GetType())) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01005900 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5901 } else {
5902 // The output overlaps in the case of an object array get with
5903 // read barriers enabled: we do not want the move to overwrite the
5904 // array's location, as we need it to emit the read barrier.
5905 locations->SetOut(
5906 Location::RequiresRegister(),
5907 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
5908 }
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01005909 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko008e09f32018-08-06 15:42:43 +01005910 if (instruction->GetIndex()->IsConstant()) {
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01005911 // Array loads with constant index are treated as field loads.
Vladimir Marko008e09f32018-08-06 15:42:43 +01005912 // We need a temporary register for the read barrier load in
5913 // CodeGeneratorARMVIXL::GenerateFieldLoadWithBakerReadBarrier()
5914 // only if the offset is too big.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01005915 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
5916 uint32_t index = instruction->GetIndex()->AsIntConstant()->GetValue();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005917 offset += index << DataType::SizeShift(DataType::Type::kReference);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01005918 if (offset >= kReferenceLoadMinFarOffset) {
5919 locations->AddTemp(Location::RequiresRegister());
5920 }
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01005921 } else {
Vladimir Marko008e09f32018-08-06 15:42:43 +01005922 // We need a non-scratch temporary for the array data pointer in
5923 // CodeGeneratorARMVIXL::GenerateArrayLoadWithBakerReadBarrier().
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01005924 locations->AddTemp(Location::RequiresRegister());
5925 }
5926 } else if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
5927 // Also need a temporary for String compression feature.
Anton Kirilove28d9ae2016-10-25 18:17:23 +01005928 locations->AddTemp(Location::RequiresRegister());
Scott Wakelingc34dba72016-10-03 10:14:44 +01005929 }
5930}
5931
5932void InstructionCodeGeneratorARMVIXL::VisitArrayGet(HArrayGet* instruction) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01005933 LocationSummary* locations = instruction->GetLocations();
5934 Location obj_loc = locations->InAt(0);
5935 vixl32::Register obj = InputRegisterAt(instruction, 0);
5936 Location index = locations->InAt(1);
5937 Location out_loc = locations->Out();
5938 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005939 DataType::Type type = instruction->GetType();
Scott Wakelingc34dba72016-10-03 10:14:44 +01005940 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
5941 instruction->IsStringCharAt();
5942 HInstruction* array_instr = instruction->GetArray();
5943 bool has_intermediate_address = array_instr->IsIntermediateAddress();
Scott Wakelingc34dba72016-10-03 10:14:44 +01005944
5945 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005946 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005947 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005948 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005949 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005950 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005951 case DataType::Type::kInt32: {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005952 vixl32::Register length;
5953 if (maybe_compressed_char_at) {
5954 length = RegisterFrom(locations->GetTemp(0));
5955 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
5956 GetAssembler()->LoadFromOffset(kLoadWord, length, obj, count_offset);
5957 codegen_->MaybeRecordImplicitNullCheck(instruction);
5958 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01005959 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00005960 int32_t const_index = Int32ConstantFrom(index);
Scott Wakelingc34dba72016-10-03 10:14:44 +01005961 if (maybe_compressed_char_at) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01005962 vixl32::Label uncompressed_load, done;
Anton Kirilov6f644202017-02-27 18:29:45 +00005963 vixl32::Label* final_label = codegen_->GetFinalLabel(instruction, &done);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005964 __ Lsrs(length, length, 1u); // LSRS has a 16-bit encoding, TST (immediate) does not.
5965 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
5966 "Expecting 0=compressed, 1=uncompressed");
Artem Serov517d9f62016-12-12 15:51:15 +00005967 __ B(cs, &uncompressed_load, /* far_target */ false);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01005968 GetAssembler()->LoadFromOffset(kLoadUnsignedByte,
5969 RegisterFrom(out_loc),
5970 obj,
5971 data_offset + const_index);
Anton Kirilov6f644202017-02-27 18:29:45 +00005972 __ B(final_label);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01005973 __ Bind(&uncompressed_load);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005974 GetAssembler()->LoadFromOffset(GetLoadOperandType(DataType::Type::kUint16),
Anton Kirilove28d9ae2016-10-25 18:17:23 +01005975 RegisterFrom(out_loc),
5976 obj,
5977 data_offset + (const_index << 1));
Anton Kirilov6f644202017-02-27 18:29:45 +00005978 if (done.IsReferenced()) {
5979 __ Bind(&done);
5980 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01005981 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005982 uint32_t full_offset = data_offset + (const_index << DataType::SizeShift(type));
Scott Wakelingc34dba72016-10-03 10:14:44 +01005983
5984 LoadOperandType load_type = GetLoadOperandType(type);
5985 GetAssembler()->LoadFromOffset(load_type, RegisterFrom(out_loc), obj, full_offset);
5986 }
5987 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005988 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01005989 vixl32::Register temp = temps.Acquire();
5990
5991 if (has_intermediate_address) {
Artem Serov2bbc9532016-10-21 11:51:50 +01005992 // We do not need to compute the intermediate address from the array: the
5993 // input instruction has done it already. See the comment in
5994 // `TryExtractArrayAccessAddress()`.
5995 if (kIsDebugBuild) {
5996 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
Anton Kirilov644032c2016-12-06 17:51:43 +00005997 DCHECK_EQ(Uint64ConstantFrom(tmp->GetOffset()), data_offset);
Artem Serov2bbc9532016-10-21 11:51:50 +01005998 }
5999 temp = obj;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006000 } else {
6001 __ Add(temp, obj, data_offset);
6002 }
6003 if (maybe_compressed_char_at) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006004 vixl32::Label uncompressed_load, done;
Anton Kirilov6f644202017-02-27 18:29:45 +00006005 vixl32::Label* final_label = codegen_->GetFinalLabel(instruction, &done);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006006 __ Lsrs(length, length, 1u); // LSRS has a 16-bit encoding, TST (immediate) does not.
6007 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
6008 "Expecting 0=compressed, 1=uncompressed");
Artem Serov517d9f62016-12-12 15:51:15 +00006009 __ B(cs, &uncompressed_load, /* far_target */ false);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006010 __ Ldrb(RegisterFrom(out_loc), MemOperand(temp, RegisterFrom(index), vixl32::LSL, 0));
Anton Kirilov6f644202017-02-27 18:29:45 +00006011 __ B(final_label);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006012 __ Bind(&uncompressed_load);
6013 __ Ldrh(RegisterFrom(out_loc), MemOperand(temp, RegisterFrom(index), vixl32::LSL, 1));
Anton Kirilov6f644202017-02-27 18:29:45 +00006014 if (done.IsReferenced()) {
6015 __ Bind(&done);
6016 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006017 } else {
6018 codegen_->LoadFromShiftedRegOffset(type, out_loc, temp, RegisterFrom(index));
6019 }
6020 }
6021 break;
6022 }
6023
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006024 case DataType::Type::kReference: {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006025 // The read barrier instrumentation of object ArrayGet
6026 // instructions does not support the HIntermediateAddress
6027 // instruction.
6028 DCHECK(!(has_intermediate_address && kEmitCompilerReadBarrier));
6029
Scott Wakelingc34dba72016-10-03 10:14:44 +01006030 static_assert(
6031 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6032 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
6033 // /* HeapReference<Object> */ out =
6034 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
6035 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006036 // Note that a potential implicit null check is handled in this
6037 // CodeGeneratorARMVIXL::GenerateArrayLoadWithBakerReadBarrier call.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01006038 DCHECK(!instruction->CanDoImplicitNullCheckOn(instruction->InputAt(0)));
6039 if (index.IsConstant()) {
6040 // Array load with a constant index can be treated as a field load.
Vladimir Markodcd117e2018-04-19 11:54:00 +01006041 Location maybe_temp =
6042 (locations->GetTempCount() != 0) ? locations->GetTemp(0) : Location();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006043 data_offset += Int32ConstantFrom(index) << DataType::SizeShift(type);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01006044 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6045 out_loc,
6046 obj,
6047 data_offset,
Vladimir Markodcd117e2018-04-19 11:54:00 +01006048 maybe_temp,
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01006049 /* needs_null_check */ false);
6050 } else {
Vladimir Markodcd117e2018-04-19 11:54:00 +01006051 Location temp = locations->GetTemp(0);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01006052 codegen_->GenerateArrayLoadWithBakerReadBarrier(
Vladimir Marko008e09f32018-08-06 15:42:43 +01006053 out_loc, obj, data_offset, index, temp, /* needs_null_check */ false);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01006054 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006055 } else {
6056 vixl32::Register out = OutputRegister(instruction);
6057 if (index.IsConstant()) {
6058 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00006059 (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006060 GetAssembler()->LoadFromOffset(kLoadWord, out, obj, offset);
Alexandre Rames374ddf32016-11-04 10:40:49 +00006061 // TODO(VIXL): Here and for other calls to `MaybeRecordImplicitNullCheck` in this method,
6062 // we should use a scope and the assembler to emit the load instruction to guarantee that
6063 // we record the pc at the correct position. But the `Assembler` does not automatically
6064 // handle unencodable offsets. Practically, everything is fine because the helper and
6065 // VIXL, at the time of writing, do generate the store instruction last.
Scott Wakelingc34dba72016-10-03 10:14:44 +01006066 codegen_->MaybeRecordImplicitNullCheck(instruction);
6067 // If read barriers are enabled, emit read barriers other than
6068 // Baker's using a slow path (and also unpoison the loaded
6069 // reference, if heap poisoning is enabled).
6070 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
6071 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006072 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006073 vixl32::Register temp = temps.Acquire();
6074
6075 if (has_intermediate_address) {
Artem Serov2bbc9532016-10-21 11:51:50 +01006076 // We do not need to compute the intermediate address from the array: the
6077 // input instruction has done it already. See the comment in
6078 // `TryExtractArrayAccessAddress()`.
6079 if (kIsDebugBuild) {
6080 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
Anton Kirilov644032c2016-12-06 17:51:43 +00006081 DCHECK_EQ(Uint64ConstantFrom(tmp->GetOffset()), data_offset);
Artem Serov2bbc9532016-10-21 11:51:50 +01006082 }
6083 temp = obj;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006084 } else {
6085 __ Add(temp, obj, data_offset);
6086 }
6087 codegen_->LoadFromShiftedRegOffset(type, out_loc, temp, RegisterFrom(index));
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006088 temps.Close();
Alexandre Rames374ddf32016-11-04 10:40:49 +00006089 // TODO(VIXL): Use a scope to ensure that we record the pc position immediately after the
6090 // load instruction. Practically, everything is fine because the helper and VIXL, at the
6091 // time of writing, do generate the store instruction last.
Scott Wakelingc34dba72016-10-03 10:14:44 +01006092 codegen_->MaybeRecordImplicitNullCheck(instruction);
6093 // If read barriers are enabled, emit read barriers other than
6094 // Baker's using a slow path (and also unpoison the loaded
6095 // reference, if heap poisoning is enabled).
6096 codegen_->MaybeGenerateReadBarrierSlow(
6097 instruction, out_loc, out_loc, obj_loc, data_offset, index);
6098 }
6099 }
6100 break;
6101 }
6102
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006103 case DataType::Type::kInt64: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006104 if (index.IsConstant()) {
6105 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00006106 (Int32ConstantFrom(index) << TIMES_8) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006107 GetAssembler()->LoadFromOffset(kLoadWordPair, LowRegisterFrom(out_loc), obj, offset);
6108 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006109 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006110 vixl32::Register temp = temps.Acquire();
6111 __ Add(temp, obj, Operand(RegisterFrom(index), vixl32::LSL, TIMES_8));
6112 GetAssembler()->LoadFromOffset(kLoadWordPair, LowRegisterFrom(out_loc), temp, data_offset);
6113 }
6114 break;
6115 }
6116
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006117 case DataType::Type::kFloat32: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006118 vixl32::SRegister out = SRegisterFrom(out_loc);
6119 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006120 size_t offset = (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006121 GetAssembler()->LoadSFromOffset(out, obj, offset);
6122 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006123 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006124 vixl32::Register temp = temps.Acquire();
6125 __ Add(temp, obj, Operand(RegisterFrom(index), vixl32::LSL, TIMES_4));
6126 GetAssembler()->LoadSFromOffset(out, temp, data_offset);
6127 }
6128 break;
6129 }
6130
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006131 case DataType::Type::kFloat64: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006132 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006133 size_t offset = (Int32ConstantFrom(index) << TIMES_8) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006134 GetAssembler()->LoadDFromOffset(DRegisterFrom(out_loc), obj, offset);
6135 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006136 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006137 vixl32::Register temp = temps.Acquire();
6138 __ Add(temp, obj, Operand(RegisterFrom(index), vixl32::LSL, TIMES_8));
6139 GetAssembler()->LoadDFromOffset(DRegisterFrom(out_loc), temp, data_offset);
6140 }
6141 break;
6142 }
6143
Aart Bik66c158e2018-01-31 12:55:04 -08006144 case DataType::Type::kUint32:
6145 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006146 case DataType::Type::kVoid:
Scott Wakelingc34dba72016-10-03 10:14:44 +01006147 LOG(FATAL) << "Unreachable type " << type;
6148 UNREACHABLE();
6149 }
6150
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006151 if (type == DataType::Type::kReference) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006152 // Potential implicit null checks, in the case of reference
6153 // arrays, are handled in the previous switch statement.
6154 } else if (!maybe_compressed_char_at) {
Alexandre Rames374ddf32016-11-04 10:40:49 +00006155 // TODO(VIXL): Use a scope to ensure we record the pc info immediately after
6156 // the preceding load instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01006157 codegen_->MaybeRecordImplicitNullCheck(instruction);
6158 }
6159}
6160
6161void LocationsBuilderARMVIXL::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006162 DataType::Type value_type = instruction->GetComponentType();
Scott Wakelingc34dba72016-10-03 10:14:44 +01006163
6164 bool needs_write_barrier =
6165 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
6166 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
6167
Vladimir Markoca6fff82017-10-03 14:49:14 +01006168 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Scott Wakelingc34dba72016-10-03 10:14:44 +01006169 instruction,
6170 may_need_runtime_call_for_type_check ?
6171 LocationSummary::kCallOnSlowPath :
6172 LocationSummary::kNoCall);
6173
6174 locations->SetInAt(0, Location::RequiresRegister());
6175 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006176 if (DataType::IsFloatingPointType(value_type)) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006177 locations->SetInAt(2, Location::RequiresFpuRegister());
6178 } else {
6179 locations->SetInAt(2, Location::RequiresRegister());
6180 }
6181 if (needs_write_barrier) {
6182 // Temporary registers for the write barrier.
6183 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
6184 locations->AddTemp(Location::RequiresRegister());
6185 }
6186}
6187
6188void InstructionCodeGeneratorARMVIXL::VisitArraySet(HArraySet* instruction) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006189 LocationSummary* locations = instruction->GetLocations();
6190 vixl32::Register array = InputRegisterAt(instruction, 0);
6191 Location index = locations->InAt(1);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006192 DataType::Type value_type = instruction->GetComponentType();
Scott Wakelingc34dba72016-10-03 10:14:44 +01006193 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
6194 bool needs_write_barrier =
6195 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
6196 uint32_t data_offset =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006197 mirror::Array::DataOffset(DataType::Size(value_type)).Uint32Value();
Scott Wakelingc34dba72016-10-03 10:14:44 +01006198 Location value_loc = locations->InAt(2);
6199 HInstruction* array_instr = instruction->GetArray();
6200 bool has_intermediate_address = array_instr->IsIntermediateAddress();
Scott Wakelingc34dba72016-10-03 10:14:44 +01006201
6202 switch (value_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006203 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006204 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006205 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006206 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006207 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006208 case DataType::Type::kInt32: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006209 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006210 int32_t const_index = Int32ConstantFrom(index);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006211 uint32_t full_offset =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006212 data_offset + (const_index << DataType::SizeShift(value_type));
Scott Wakelingc34dba72016-10-03 10:14:44 +01006213 StoreOperandType store_type = GetStoreOperandType(value_type);
6214 GetAssembler()->StoreToOffset(store_type, RegisterFrom(value_loc), array, full_offset);
6215 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006216 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006217 vixl32::Register temp = temps.Acquire();
6218
6219 if (has_intermediate_address) {
Artem Serov2bbc9532016-10-21 11:51:50 +01006220 // We do not need to compute the intermediate address from the array: the
6221 // input instruction has done it already. See the comment in
6222 // `TryExtractArrayAccessAddress()`.
6223 if (kIsDebugBuild) {
6224 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
Anton Kirilov644032c2016-12-06 17:51:43 +00006225 DCHECK_EQ(Uint64ConstantFrom(tmp->GetOffset()), data_offset);
Artem Serov2bbc9532016-10-21 11:51:50 +01006226 }
6227 temp = array;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006228 } else {
6229 __ Add(temp, array, data_offset);
6230 }
6231 codegen_->StoreToShiftedRegOffset(value_type, value_loc, temp, RegisterFrom(index));
6232 }
6233 break;
6234 }
6235
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006236 case DataType::Type::kReference: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006237 vixl32::Register value = RegisterFrom(value_loc);
6238 // TryExtractArrayAccessAddress optimization is never applied for non-primitive ArraySet.
6239 // See the comment in instruction_simplifier_shared.cc.
6240 DCHECK(!has_intermediate_address);
6241
6242 if (instruction->InputAt(2)->IsNullConstant()) {
6243 // Just setting null.
6244 if (index.IsConstant()) {
6245 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00006246 (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006247 GetAssembler()->StoreToOffset(kStoreWord, value, array, offset);
6248 } else {
6249 DCHECK(index.IsRegister()) << index;
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006250 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006251 vixl32::Register temp = temps.Acquire();
6252 __ Add(temp, array, data_offset);
6253 codegen_->StoreToShiftedRegOffset(value_type, value_loc, temp, RegisterFrom(index));
6254 }
Alexandre Rames374ddf32016-11-04 10:40:49 +00006255 // TODO(VIXL): Use a scope to ensure we record the pc info immediately after the preceding
6256 // store instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01006257 codegen_->MaybeRecordImplicitNullCheck(instruction);
6258 DCHECK(!needs_write_barrier);
6259 DCHECK(!may_need_runtime_call_for_type_check);
6260 break;
6261 }
6262
6263 DCHECK(needs_write_barrier);
6264 Location temp1_loc = locations->GetTemp(0);
6265 vixl32::Register temp1 = RegisterFrom(temp1_loc);
6266 Location temp2_loc = locations->GetTemp(1);
6267 vixl32::Register temp2 = RegisterFrom(temp2_loc);
6268 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6269 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6270 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6271 vixl32::Label done;
Anton Kirilov6f644202017-02-27 18:29:45 +00006272 vixl32::Label* final_label = codegen_->GetFinalLabel(instruction, &done);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006273 SlowPathCodeARMVIXL* slow_path = nullptr;
6274
6275 if (may_need_runtime_call_for_type_check) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01006276 slow_path = new (codegen_->GetScopedAllocator()) ArraySetSlowPathARMVIXL(instruction);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006277 codegen_->AddSlowPath(slow_path);
6278 if (instruction->GetValueCanBeNull()) {
6279 vixl32::Label non_zero;
xueliang.zhongf51bc622016-11-04 09:23:32 +00006280 __ CompareAndBranchIfNonZero(value, &non_zero);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006281 if (index.IsConstant()) {
6282 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00006283 (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006284 GetAssembler()->StoreToOffset(kStoreWord, value, array, offset);
6285 } else {
6286 DCHECK(index.IsRegister()) << index;
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006287 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006288 vixl32::Register temp = temps.Acquire();
6289 __ Add(temp, array, data_offset);
6290 codegen_->StoreToShiftedRegOffset(value_type, value_loc, temp, RegisterFrom(index));
6291 }
Alexandre Rames374ddf32016-11-04 10:40:49 +00006292 // TODO(VIXL): Use a scope to ensure we record the pc info immediately after the preceding
6293 // store instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01006294 codegen_->MaybeRecordImplicitNullCheck(instruction);
Anton Kirilov6f644202017-02-27 18:29:45 +00006295 __ B(final_label);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006296 __ Bind(&non_zero);
6297 }
6298
6299 // Note that when read barriers are enabled, the type checks
6300 // are performed without read barriers. This is fine, even in
6301 // the case where a class object is in the from-space after
6302 // the flip, as a comparison involving such a type would not
6303 // produce a false positive; it may of course produce a false
6304 // negative, in which case we would take the ArraySet slow
6305 // path.
6306
Alexandre Rames374ddf32016-11-04 10:40:49 +00006307 {
6308 // Ensure we record the pc position immediately after the `ldr` instruction.
Artem Serov0fb37192016-12-06 18:13:40 +00006309 ExactAssemblyScope aas(GetVIXLAssembler(),
6310 vixl32::kMaxInstructionSizeInBytes,
6311 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00006312 // /* HeapReference<Class> */ temp1 = array->klass_
6313 __ ldr(temp1, MemOperand(array, class_offset));
6314 codegen_->MaybeRecordImplicitNullCheck(instruction);
6315 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006316 GetAssembler()->MaybeUnpoisonHeapReference(temp1);
6317
6318 // /* HeapReference<Class> */ temp1 = temp1->component_type_
6319 GetAssembler()->LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
6320 // /* HeapReference<Class> */ temp2 = value->klass_
6321 GetAssembler()->LoadFromOffset(kLoadWord, temp2, value, class_offset);
6322 // If heap poisoning is enabled, no need to unpoison `temp1`
6323 // nor `temp2`, as we are comparing two poisoned references.
6324 __ Cmp(temp1, temp2);
6325
6326 if (instruction->StaticTypeOfArrayIsObjectArray()) {
6327 vixl32::Label do_put;
Artem Serov517d9f62016-12-12 15:51:15 +00006328 __ B(eq, &do_put, /* far_target */ false);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006329 // If heap poisoning is enabled, the `temp1` reference has
6330 // not been unpoisoned yet; unpoison it now.
6331 GetAssembler()->MaybeUnpoisonHeapReference(temp1);
6332
6333 // /* HeapReference<Class> */ temp1 = temp1->super_class_
6334 GetAssembler()->LoadFromOffset(kLoadWord, temp1, temp1, super_offset);
6335 // If heap poisoning is enabled, no need to unpoison
6336 // `temp1`, as we are comparing against null below.
xueliang.zhongf51bc622016-11-04 09:23:32 +00006337 __ CompareAndBranchIfNonZero(temp1, slow_path->GetEntryLabel());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006338 __ Bind(&do_put);
6339 } else {
6340 __ B(ne, slow_path->GetEntryLabel());
6341 }
6342 }
6343
6344 vixl32::Register source = value;
6345 if (kPoisonHeapReferences) {
6346 // Note that in the case where `value` is a null reference,
6347 // we do not enter this block, as a null reference does not
6348 // need poisoning.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006349 DCHECK_EQ(value_type, DataType::Type::kReference);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006350 __ Mov(temp1, value);
6351 GetAssembler()->PoisonHeapReference(temp1);
6352 source = temp1;
6353 }
6354
6355 if (index.IsConstant()) {
6356 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00006357 (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006358 GetAssembler()->StoreToOffset(kStoreWord, source, array, offset);
6359 } else {
6360 DCHECK(index.IsRegister()) << index;
6361
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006362 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006363 vixl32::Register temp = temps.Acquire();
6364 __ Add(temp, array, data_offset);
6365 codegen_->StoreToShiftedRegOffset(value_type,
6366 LocationFrom(source),
6367 temp,
6368 RegisterFrom(index));
6369 }
6370
6371 if (!may_need_runtime_call_for_type_check) {
Alexandre Rames374ddf32016-11-04 10:40:49 +00006372 // TODO(VIXL): Ensure we record the pc position immediately after the preceding store
6373 // instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01006374 codegen_->MaybeRecordImplicitNullCheck(instruction);
6375 }
6376
6377 codegen_->MarkGCCard(temp1, temp2, array, value, instruction->GetValueCanBeNull());
6378
6379 if (done.IsReferenced()) {
6380 __ Bind(&done);
6381 }
6382
6383 if (slow_path != nullptr) {
6384 __ Bind(slow_path->GetExitLabel());
6385 }
6386
6387 break;
6388 }
6389
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006390 case DataType::Type::kInt64: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006391 Location value = locations->InAt(2);
6392 if (index.IsConstant()) {
6393 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00006394 (Int32ConstantFrom(index) << TIMES_8) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006395 GetAssembler()->StoreToOffset(kStoreWordPair, LowRegisterFrom(value), array, offset);
6396 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006397 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006398 vixl32::Register temp = temps.Acquire();
6399 __ Add(temp, array, Operand(RegisterFrom(index), vixl32::LSL, TIMES_8));
6400 GetAssembler()->StoreToOffset(kStoreWordPair, LowRegisterFrom(value), temp, data_offset);
6401 }
6402 break;
6403 }
6404
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006405 case DataType::Type::kFloat32: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006406 Location value = locations->InAt(2);
6407 DCHECK(value.IsFpuRegister());
6408 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006409 size_t offset = (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006410 GetAssembler()->StoreSToOffset(SRegisterFrom(value), array, offset);
6411 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006412 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006413 vixl32::Register temp = temps.Acquire();
6414 __ Add(temp, array, Operand(RegisterFrom(index), vixl32::LSL, TIMES_4));
6415 GetAssembler()->StoreSToOffset(SRegisterFrom(value), temp, data_offset);
6416 }
6417 break;
6418 }
6419
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006420 case DataType::Type::kFloat64: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006421 Location value = locations->InAt(2);
6422 DCHECK(value.IsFpuRegisterPair());
6423 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006424 size_t offset = (Int32ConstantFrom(index) << TIMES_8) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006425 GetAssembler()->StoreDToOffset(DRegisterFrom(value), array, offset);
6426 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006427 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006428 vixl32::Register temp = temps.Acquire();
6429 __ Add(temp, array, Operand(RegisterFrom(index), vixl32::LSL, TIMES_8));
6430 GetAssembler()->StoreDToOffset(DRegisterFrom(value), temp, data_offset);
6431 }
6432 break;
6433 }
6434
Aart Bik66c158e2018-01-31 12:55:04 -08006435 case DataType::Type::kUint32:
6436 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006437 case DataType::Type::kVoid:
Scott Wakelingc34dba72016-10-03 10:14:44 +01006438 LOG(FATAL) << "Unreachable type " << value_type;
6439 UNREACHABLE();
6440 }
6441
6442 // Objects are handled in the switch.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006443 if (value_type != DataType::Type::kReference) {
Alexandre Rames374ddf32016-11-04 10:40:49 +00006444 // TODO(VIXL): Ensure we record the pc position immediately after the preceding store
6445 // instruction.
Scott Wakelingc34dba72016-10-03 10:14:44 +01006446 codegen_->MaybeRecordImplicitNullCheck(instruction);
6447 }
6448}
6449
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006450void LocationsBuilderARMVIXL::VisitArrayLength(HArrayLength* instruction) {
6451 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006452 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006453 locations->SetInAt(0, Location::RequiresRegister());
6454 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6455}
6456
6457void InstructionCodeGeneratorARMVIXL::VisitArrayLength(HArrayLength* instruction) {
6458 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
6459 vixl32::Register obj = InputRegisterAt(instruction, 0);
6460 vixl32::Register out = OutputRegister(instruction);
Alexandre Rames374ddf32016-11-04 10:40:49 +00006461 {
Artem Serov0fb37192016-12-06 18:13:40 +00006462 ExactAssemblyScope aas(GetVIXLAssembler(),
6463 vixl32::kMaxInstructionSizeInBytes,
6464 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00006465 __ ldr(out, MemOperand(obj, offset));
6466 codegen_->MaybeRecordImplicitNullCheck(instruction);
6467 }
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006468 // Mask out compression flag from String's array length.
6469 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006470 __ Lsr(out, out, 1u);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006471 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006472}
6473
Artem Serov2bbc9532016-10-21 11:51:50 +01006474void LocationsBuilderARMVIXL::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Artem Serov2bbc9532016-10-21 11:51:50 +01006475 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006476 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Artem Serov2bbc9532016-10-21 11:51:50 +01006477
6478 locations->SetInAt(0, Location::RequiresRegister());
6479 locations->SetInAt(1, Location::RegisterOrConstant(instruction->GetOffset()));
6480 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6481}
6482
6483void InstructionCodeGeneratorARMVIXL::VisitIntermediateAddress(HIntermediateAddress* instruction) {
6484 vixl32::Register out = OutputRegister(instruction);
6485 vixl32::Register first = InputRegisterAt(instruction, 0);
6486 Location second = instruction->GetLocations()->InAt(1);
6487
Artem Serov2bbc9532016-10-21 11:51:50 +01006488 if (second.IsRegister()) {
6489 __ Add(out, first, RegisterFrom(second));
6490 } else {
Anton Kirilov644032c2016-12-06 17:51:43 +00006491 __ Add(out, first, Int32ConstantFrom(second));
Artem Serov2bbc9532016-10-21 11:51:50 +01006492 }
6493}
6494
Artem Serove1811ed2017-04-27 16:50:47 +01006495void LocationsBuilderARMVIXL::VisitIntermediateAddressIndex(
6496 HIntermediateAddressIndex* instruction) {
6497 LOG(FATAL) << "Unreachable " << instruction->GetId();
6498}
6499
6500void InstructionCodeGeneratorARMVIXL::VisitIntermediateAddressIndex(
6501 HIntermediateAddressIndex* instruction) {
6502 LOG(FATAL) << "Unreachable " << instruction->GetId();
6503}
6504
Scott Wakelingc34dba72016-10-03 10:14:44 +01006505void LocationsBuilderARMVIXL::VisitBoundsCheck(HBoundsCheck* instruction) {
6506 RegisterSet caller_saves = RegisterSet::Empty();
6507 InvokeRuntimeCallingConventionARMVIXL calling_convention;
6508 caller_saves.Add(LocationFrom(calling_convention.GetRegisterAt(0)));
6509 caller_saves.Add(LocationFrom(calling_convention.GetRegisterAt(1)));
6510 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Artem Serov2dd053d2017-03-08 14:54:06 +00006511
6512 HInstruction* index = instruction->InputAt(0);
6513 HInstruction* length = instruction->InputAt(1);
6514 // If both index and length are constants we can statically check the bounds. But if at least one
6515 // of them is not encodable ArmEncodableConstantOrRegister will create
6516 // Location::RequiresRegister() which is not desired to happen. Instead we create constant
6517 // locations.
6518 bool both_const = index->IsConstant() && length->IsConstant();
6519 locations->SetInAt(0, both_const
6520 ? Location::ConstantLocation(index->AsConstant())
6521 : ArmEncodableConstantOrRegister(index, CMP));
6522 locations->SetInAt(1, both_const
6523 ? Location::ConstantLocation(length->AsConstant())
6524 : ArmEncodableConstantOrRegister(length, CMP));
Scott Wakelingc34dba72016-10-03 10:14:44 +01006525}
6526
6527void InstructionCodeGeneratorARMVIXL::VisitBoundsCheck(HBoundsCheck* instruction) {
Artem Serov2dd053d2017-03-08 14:54:06 +00006528 LocationSummary* locations = instruction->GetLocations();
6529 Location index_loc = locations->InAt(0);
6530 Location length_loc = locations->InAt(1);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006531
Artem Serov2dd053d2017-03-08 14:54:06 +00006532 if (length_loc.IsConstant()) {
6533 int32_t length = Int32ConstantFrom(length_loc);
6534 if (index_loc.IsConstant()) {
6535 // BCE will remove the bounds check if we are guaranteed to pass.
6536 int32_t index = Int32ConstantFrom(index_loc);
6537 if (index < 0 || index >= length) {
6538 SlowPathCodeARMVIXL* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006539 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathARMVIXL(instruction);
Artem Serov2dd053d2017-03-08 14:54:06 +00006540 codegen_->AddSlowPath(slow_path);
6541 __ B(slow_path->GetEntryLabel());
6542 } else {
6543 // Some optimization after BCE may have generated this, and we should not
6544 // generate a bounds check if it is a valid range.
6545 }
6546 return;
6547 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006548
Artem Serov2dd053d2017-03-08 14:54:06 +00006549 SlowPathCodeARMVIXL* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006550 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathARMVIXL(instruction);
Artem Serov2dd053d2017-03-08 14:54:06 +00006551 __ Cmp(RegisterFrom(index_loc), length);
6552 codegen_->AddSlowPath(slow_path);
6553 __ B(hs, slow_path->GetEntryLabel());
6554 } else {
6555 SlowPathCodeARMVIXL* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006556 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathARMVIXL(instruction);
Artem Serov2dd053d2017-03-08 14:54:06 +00006557 __ Cmp(RegisterFrom(length_loc), InputOperandAt(instruction, 0));
6558 codegen_->AddSlowPath(slow_path);
6559 __ B(ls, slow_path->GetEntryLabel());
6560 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006561}
6562
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006563void CodeGeneratorARMVIXL::MarkGCCard(vixl32::Register temp,
6564 vixl32::Register card,
6565 vixl32::Register object,
6566 vixl32::Register value,
6567 bool can_be_null) {
6568 vixl32::Label is_null;
6569 if (can_be_null) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00006570 __ CompareAndBranchIfZero(value, &is_null);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006571 }
Roland Levillainc73f0522018-08-14 15:16:50 +01006572 // Load the address of the card table into `card`.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006573 GetAssembler()->LoadFromOffset(
6574 kLoadWord, card, tr, Thread::CardTableOffset<kArmPointerSize>().Int32Value());
Roland Levillainc73f0522018-08-14 15:16:50 +01006575 // Calculate the offset (in the card table) of the card corresponding to
6576 // `object`.
Scott Wakelingb77051e2016-11-21 19:46:00 +00006577 __ Lsr(temp, object, Operand::From(gc::accounting::CardTable::kCardShift));
Roland Levillainc73f0522018-08-14 15:16:50 +01006578 // Write the `art::gc::accounting::CardTable::kCardDirty` value into the
6579 // `object`'s card.
6580 //
6581 // Register `card` contains the address of the card table. Note that the card
6582 // table's base is biased during its creation so that it always starts at an
6583 // address whose least-significant byte is equal to `kCardDirty` (see
6584 // art::gc::accounting::CardTable::Create). Therefore the STRB instruction
6585 // below writes the `kCardDirty` (byte) value into the `object`'s card
6586 // (located at `card + object >> kCardShift`).
6587 //
6588 // This dual use of the value in register `card` (1. to calculate the location
6589 // of the card to mark; and 2. to load the `kCardDirty` value) saves a load
6590 // (no need to explicitly load `kCardDirty` as an immediate value).
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006591 __ Strb(card, MemOperand(card, temp));
6592 if (can_be_null) {
6593 __ Bind(&is_null);
6594 }
6595}
6596
Scott Wakelingfe885462016-09-22 10:24:38 +01006597void LocationsBuilderARMVIXL::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
6598 LOG(FATAL) << "Unreachable";
6599}
6600
6601void InstructionCodeGeneratorARMVIXL::VisitParallelMove(HParallelMove* instruction) {
Vladimir Markobea75ff2017-10-11 20:39:54 +01006602 if (instruction->GetNext()->IsSuspendCheck() &&
6603 instruction->GetBlock()->GetLoopInformation() != nullptr) {
6604 HSuspendCheck* suspend_check = instruction->GetNext()->AsSuspendCheck();
6605 // The back edge will generate the suspend check.
6606 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(suspend_check, instruction);
6607 }
6608
Scott Wakelingfe885462016-09-22 10:24:38 +01006609 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
6610}
6611
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006612void LocationsBuilderARMVIXL::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006613 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6614 instruction, LocationSummary::kCallOnSlowPath);
Artem Serov657022c2016-11-23 14:19:38 +00006615 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006616}
6617
6618void InstructionCodeGeneratorARMVIXL::VisitSuspendCheck(HSuspendCheck* instruction) {
6619 HBasicBlock* block = instruction->GetBlock();
6620 if (block->GetLoopInformation() != nullptr) {
6621 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
6622 // The back edge will generate the suspend check.
6623 return;
6624 }
6625 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
6626 // The goto will generate the suspend check.
6627 return;
6628 }
6629 GenerateSuspendCheck(instruction, nullptr);
Orion Hodson4c8e12e2018-05-18 08:33:20 +01006630 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 13);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006631}
6632
6633void InstructionCodeGeneratorARMVIXL::GenerateSuspendCheck(HSuspendCheck* instruction,
6634 HBasicBlock* successor) {
6635 SuspendCheckSlowPathARMVIXL* slow_path =
6636 down_cast<SuspendCheckSlowPathARMVIXL*>(instruction->GetSlowPath());
6637 if (slow_path == nullptr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006638 slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006639 new (codegen_->GetScopedAllocator()) SuspendCheckSlowPathARMVIXL(instruction, successor);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006640 instruction->SetSlowPath(slow_path);
6641 codegen_->AddSlowPath(slow_path);
6642 if (successor != nullptr) {
6643 DCHECK(successor->IsLoopHeader());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006644 }
6645 } else {
6646 DCHECK_EQ(slow_path->GetSuccessor(), successor);
6647 }
6648
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006649 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006650 vixl32::Register temp = temps.Acquire();
6651 GetAssembler()->LoadFromOffset(
6652 kLoadUnsignedHalfword, temp, tr, Thread::ThreadFlagsOffset<kArmPointerSize>().Int32Value());
6653 if (successor == nullptr) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00006654 __ CompareAndBranchIfNonZero(temp, slow_path->GetEntryLabel());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006655 __ Bind(slow_path->GetReturnLabel());
6656 } else {
xueliang.zhongf51bc622016-11-04 09:23:32 +00006657 __ CompareAndBranchIfZero(temp, codegen_->GetLabelOf(successor));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006658 __ B(slow_path->GetEntryLabel());
6659 }
6660}
6661
Scott Wakelingfe885462016-09-22 10:24:38 +01006662ArmVIXLAssembler* ParallelMoveResolverARMVIXL::GetAssembler() const {
6663 return codegen_->GetAssembler();
6664}
6665
6666void ParallelMoveResolverARMVIXL::EmitMove(size_t index) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006667 UseScratchRegisterScope temps(GetAssembler()->GetVIXLAssembler());
Scott Wakelingfe885462016-09-22 10:24:38 +01006668 MoveOperands* move = moves_[index];
6669 Location source = move->GetSource();
6670 Location destination = move->GetDestination();
6671
6672 if (source.IsRegister()) {
6673 if (destination.IsRegister()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006674 __ Mov(RegisterFrom(destination), RegisterFrom(source));
Scott Wakelingfe885462016-09-22 10:24:38 +01006675 } else if (destination.IsFpuRegister()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006676 __ Vmov(SRegisterFrom(destination), RegisterFrom(source));
Scott Wakelingfe885462016-09-22 10:24:38 +01006677 } else {
6678 DCHECK(destination.IsStackSlot());
6679 GetAssembler()->StoreToOffset(kStoreWord,
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006680 RegisterFrom(source),
Scott Wakelingfe885462016-09-22 10:24:38 +01006681 sp,
6682 destination.GetStackIndex());
6683 }
6684 } else if (source.IsStackSlot()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006685 if (destination.IsRegister()) {
6686 GetAssembler()->LoadFromOffset(kLoadWord,
6687 RegisterFrom(destination),
6688 sp,
6689 source.GetStackIndex());
6690 } else if (destination.IsFpuRegister()) {
6691 GetAssembler()->LoadSFromOffset(SRegisterFrom(destination), sp, source.GetStackIndex());
6692 } else {
6693 DCHECK(destination.IsStackSlot());
6694 vixl32::Register temp = temps.Acquire();
6695 GetAssembler()->LoadFromOffset(kLoadWord, temp, sp, source.GetStackIndex());
6696 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
6697 }
Scott Wakelingfe885462016-09-22 10:24:38 +01006698 } else if (source.IsFpuRegister()) {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01006699 if (destination.IsRegister()) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006700 __ Vmov(RegisterFrom(destination), SRegisterFrom(source));
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01006701 } else if (destination.IsFpuRegister()) {
6702 __ Vmov(SRegisterFrom(destination), SRegisterFrom(source));
6703 } else {
6704 DCHECK(destination.IsStackSlot());
6705 GetAssembler()->StoreSToOffset(SRegisterFrom(source), sp, destination.GetStackIndex());
6706 }
Scott Wakelingfe885462016-09-22 10:24:38 +01006707 } else if (source.IsDoubleStackSlot()) {
Alexandre Rames9c19bd62016-10-24 11:50:32 +01006708 if (destination.IsDoubleStackSlot()) {
6709 vixl32::DRegister temp = temps.AcquireD();
6710 GetAssembler()->LoadDFromOffset(temp, sp, source.GetStackIndex());
6711 GetAssembler()->StoreDToOffset(temp, sp, destination.GetStackIndex());
6712 } else if (destination.IsRegisterPair()) {
6713 DCHECK(ExpectedPairLayout(destination));
6714 GetAssembler()->LoadFromOffset(
6715 kLoadWordPair, LowRegisterFrom(destination), sp, source.GetStackIndex());
6716 } else {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01006717 DCHECK(destination.IsFpuRegisterPair()) << destination;
6718 GetAssembler()->LoadDFromOffset(DRegisterFrom(destination), sp, source.GetStackIndex());
Alexandre Rames9c19bd62016-10-24 11:50:32 +01006719 }
Scott Wakelingfe885462016-09-22 10:24:38 +01006720 } else if (source.IsRegisterPair()) {
6721 if (destination.IsRegisterPair()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006722 __ Mov(LowRegisterFrom(destination), LowRegisterFrom(source));
6723 __ Mov(HighRegisterFrom(destination), HighRegisterFrom(source));
Scott Wakelingfe885462016-09-22 10:24:38 +01006724 } else if (destination.IsFpuRegisterPair()) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006725 __ Vmov(DRegisterFrom(destination), LowRegisterFrom(source), HighRegisterFrom(source));
Scott Wakelingfe885462016-09-22 10:24:38 +01006726 } else {
6727 DCHECK(destination.IsDoubleStackSlot()) << destination;
6728 DCHECK(ExpectedPairLayout(source));
6729 GetAssembler()->StoreToOffset(kStoreWordPair,
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006730 LowRegisterFrom(source),
Scott Wakelingfe885462016-09-22 10:24:38 +01006731 sp,
6732 destination.GetStackIndex());
6733 }
6734 } else if (source.IsFpuRegisterPair()) {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01006735 if (destination.IsRegisterPair()) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006736 __ Vmov(LowRegisterFrom(destination), HighRegisterFrom(destination), DRegisterFrom(source));
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01006737 } else if (destination.IsFpuRegisterPair()) {
6738 __ Vmov(DRegisterFrom(destination), DRegisterFrom(source));
6739 } else {
6740 DCHECK(destination.IsDoubleStackSlot()) << destination;
6741 GetAssembler()->StoreDToOffset(DRegisterFrom(source), sp, destination.GetStackIndex());
6742 }
Scott Wakelingfe885462016-09-22 10:24:38 +01006743 } else {
6744 DCHECK(source.IsConstant()) << source;
6745 HConstant* constant = source.GetConstant();
6746 if (constant->IsIntConstant() || constant->IsNullConstant()) {
6747 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
6748 if (destination.IsRegister()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006749 __ Mov(RegisterFrom(destination), value);
Scott Wakelingfe885462016-09-22 10:24:38 +01006750 } else {
6751 DCHECK(destination.IsStackSlot());
Scott Wakelingfe885462016-09-22 10:24:38 +01006752 vixl32::Register temp = temps.Acquire();
6753 __ Mov(temp, value);
6754 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
6755 }
6756 } else if (constant->IsLongConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006757 int64_t value = Int64ConstantFrom(source);
Scott Wakelingfe885462016-09-22 10:24:38 +01006758 if (destination.IsRegisterPair()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006759 __ Mov(LowRegisterFrom(destination), Low32Bits(value));
6760 __ Mov(HighRegisterFrom(destination), High32Bits(value));
Scott Wakelingfe885462016-09-22 10:24:38 +01006761 } else {
6762 DCHECK(destination.IsDoubleStackSlot()) << destination;
Scott Wakelingfe885462016-09-22 10:24:38 +01006763 vixl32::Register temp = temps.Acquire();
6764 __ Mov(temp, Low32Bits(value));
6765 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
6766 __ Mov(temp, High32Bits(value));
6767 GetAssembler()->StoreToOffset(kStoreWord,
6768 temp,
6769 sp,
6770 destination.GetHighStackIndex(kArmWordSize));
6771 }
6772 } else if (constant->IsDoubleConstant()) {
6773 double value = constant->AsDoubleConstant()->GetValue();
6774 if (destination.IsFpuRegisterPair()) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006775 __ Vmov(DRegisterFrom(destination), value);
Scott Wakelingfe885462016-09-22 10:24:38 +01006776 } else {
6777 DCHECK(destination.IsDoubleStackSlot()) << destination;
6778 uint64_t int_value = bit_cast<uint64_t, double>(value);
Scott Wakelingfe885462016-09-22 10:24:38 +01006779 vixl32::Register temp = temps.Acquire();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006780 __ Mov(temp, Low32Bits(int_value));
Scott Wakelingfe885462016-09-22 10:24:38 +01006781 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006782 __ Mov(temp, High32Bits(int_value));
Scott Wakelingfe885462016-09-22 10:24:38 +01006783 GetAssembler()->StoreToOffset(kStoreWord,
6784 temp,
6785 sp,
6786 destination.GetHighStackIndex(kArmWordSize));
6787 }
6788 } else {
6789 DCHECK(constant->IsFloatConstant()) << constant->DebugName();
6790 float value = constant->AsFloatConstant()->GetValue();
6791 if (destination.IsFpuRegister()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006792 __ Vmov(SRegisterFrom(destination), value);
Scott Wakelingfe885462016-09-22 10:24:38 +01006793 } else {
6794 DCHECK(destination.IsStackSlot());
Scott Wakelingfe885462016-09-22 10:24:38 +01006795 vixl32::Register temp = temps.Acquire();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006796 __ Mov(temp, bit_cast<int32_t, float>(value));
Scott Wakelingfe885462016-09-22 10:24:38 +01006797 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
6798 }
6799 }
6800 }
6801}
6802
Alexandre Rames9c19bd62016-10-24 11:50:32 +01006803void ParallelMoveResolverARMVIXL::Exchange(vixl32::Register reg, int mem) {
6804 UseScratchRegisterScope temps(GetAssembler()->GetVIXLAssembler());
6805 vixl32::Register temp = temps.Acquire();
6806 __ Mov(temp, reg);
6807 GetAssembler()->LoadFromOffset(kLoadWord, reg, sp, mem);
6808 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, mem);
Scott Wakelingfe885462016-09-22 10:24:38 +01006809}
6810
Alexandre Rames9c19bd62016-10-24 11:50:32 +01006811void ParallelMoveResolverARMVIXL::Exchange(int mem1, int mem2) {
6812 // TODO(VIXL32): Double check the performance of this implementation.
6813 UseScratchRegisterScope temps(GetAssembler()->GetVIXLAssembler());
Nicolas Geoffray13a797b2017-03-15 16:41:31 +00006814 vixl32::Register temp1 = temps.Acquire();
6815 ScratchRegisterScope ensure_scratch(
6816 this, temp1.GetCode(), r0.GetCode(), codegen_->GetNumberOfCoreRegisters());
6817 vixl32::Register temp2(ensure_scratch.GetRegister());
Alexandre Rames9c19bd62016-10-24 11:50:32 +01006818
Nicolas Geoffray13a797b2017-03-15 16:41:31 +00006819 int stack_offset = ensure_scratch.IsSpilled() ? kArmWordSize : 0;
6820 GetAssembler()->LoadFromOffset(kLoadWord, temp1, sp, mem1 + stack_offset);
6821 GetAssembler()->LoadFromOffset(kLoadWord, temp2, sp, mem2 + stack_offset);
6822 GetAssembler()->StoreToOffset(kStoreWord, temp1, sp, mem2 + stack_offset);
6823 GetAssembler()->StoreToOffset(kStoreWord, temp2, sp, mem1 + stack_offset);
Scott Wakelingfe885462016-09-22 10:24:38 +01006824}
6825
Alexandre Rames9c19bd62016-10-24 11:50:32 +01006826void ParallelMoveResolverARMVIXL::EmitSwap(size_t index) {
6827 MoveOperands* move = moves_[index];
6828 Location source = move->GetSource();
6829 Location destination = move->GetDestination();
6830 UseScratchRegisterScope temps(GetAssembler()->GetVIXLAssembler());
6831
6832 if (source.IsRegister() && destination.IsRegister()) {
6833 vixl32::Register temp = temps.Acquire();
6834 DCHECK(!RegisterFrom(source).Is(temp));
6835 DCHECK(!RegisterFrom(destination).Is(temp));
6836 __ Mov(temp, RegisterFrom(destination));
6837 __ Mov(RegisterFrom(destination), RegisterFrom(source));
6838 __ Mov(RegisterFrom(source), temp);
6839 } else if (source.IsRegister() && destination.IsStackSlot()) {
6840 Exchange(RegisterFrom(source), destination.GetStackIndex());
6841 } else if (source.IsStackSlot() && destination.IsRegister()) {
6842 Exchange(RegisterFrom(destination), source.GetStackIndex());
6843 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Anton Kirilovdda43962016-11-21 19:55:20 +00006844 Exchange(source.GetStackIndex(), destination.GetStackIndex());
Alexandre Rames9c19bd62016-10-24 11:50:32 +01006845 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Nicolas Geoffray13a797b2017-03-15 16:41:31 +00006846 vixl32::Register temp = temps.Acquire();
Anton Kirilovdda43962016-11-21 19:55:20 +00006847 __ Vmov(temp, SRegisterFrom(source));
6848 __ Vmov(SRegisterFrom(source), SRegisterFrom(destination));
6849 __ Vmov(SRegisterFrom(destination), temp);
Alexandre Rames9c19bd62016-10-24 11:50:32 +01006850 } else if (source.IsRegisterPair() && destination.IsRegisterPair()) {
6851 vixl32::DRegister temp = temps.AcquireD();
6852 __ Vmov(temp, LowRegisterFrom(source), HighRegisterFrom(source));
6853 __ Mov(LowRegisterFrom(source), LowRegisterFrom(destination));
6854 __ Mov(HighRegisterFrom(source), HighRegisterFrom(destination));
6855 __ Vmov(LowRegisterFrom(destination), HighRegisterFrom(destination), temp);
6856 } else if (source.IsRegisterPair() || destination.IsRegisterPair()) {
6857 vixl32::Register low_reg = LowRegisterFrom(source.IsRegisterPair() ? source : destination);
6858 int mem = source.IsRegisterPair() ? destination.GetStackIndex() : source.GetStackIndex();
6859 DCHECK(ExpectedPairLayout(source.IsRegisterPair() ? source : destination));
6860 vixl32::DRegister temp = temps.AcquireD();
6861 __ Vmov(temp, low_reg, vixl32::Register(low_reg.GetCode() + 1));
6862 GetAssembler()->LoadFromOffset(kLoadWordPair, low_reg, sp, mem);
6863 GetAssembler()->StoreDToOffset(temp, sp, mem);
6864 } else if (source.IsFpuRegisterPair() && destination.IsFpuRegisterPair()) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006865 vixl32::DRegister first = DRegisterFrom(source);
6866 vixl32::DRegister second = DRegisterFrom(destination);
6867 vixl32::DRegister temp = temps.AcquireD();
6868 __ Vmov(temp, first);
6869 __ Vmov(first, second);
6870 __ Vmov(second, temp);
Alexandre Rames9c19bd62016-10-24 11:50:32 +01006871 } else if (source.IsFpuRegisterPair() || destination.IsFpuRegisterPair()) {
Anton Kirilovdda43962016-11-21 19:55:20 +00006872 vixl32::DRegister reg = source.IsFpuRegisterPair()
6873 ? DRegisterFrom(source)
6874 : DRegisterFrom(destination);
6875 int mem = source.IsFpuRegisterPair()
6876 ? destination.GetStackIndex()
6877 : source.GetStackIndex();
6878 vixl32::DRegister temp = temps.AcquireD();
6879 __ Vmov(temp, reg);
6880 GetAssembler()->LoadDFromOffset(reg, sp, mem);
6881 GetAssembler()->StoreDToOffset(temp, sp, mem);
Alexandre Rames9c19bd62016-10-24 11:50:32 +01006882 } else if (source.IsFpuRegister() || destination.IsFpuRegister()) {
Anton Kirilovdda43962016-11-21 19:55:20 +00006883 vixl32::SRegister reg = source.IsFpuRegister()
6884 ? SRegisterFrom(source)
6885 : SRegisterFrom(destination);
6886 int mem = source.IsFpuRegister()
6887 ? destination.GetStackIndex()
6888 : source.GetStackIndex();
6889 vixl32::Register temp = temps.Acquire();
6890 __ Vmov(temp, reg);
6891 GetAssembler()->LoadSFromOffset(reg, sp, mem);
6892 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, mem);
Alexandre Rames9c19bd62016-10-24 11:50:32 +01006893 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
6894 vixl32::DRegister temp1 = temps.AcquireD();
6895 vixl32::DRegister temp2 = temps.AcquireD();
6896 __ Vldr(temp1, MemOperand(sp, source.GetStackIndex()));
6897 __ Vldr(temp2, MemOperand(sp, destination.GetStackIndex()));
6898 __ Vstr(temp1, MemOperand(sp, destination.GetStackIndex()));
6899 __ Vstr(temp2, MemOperand(sp, source.GetStackIndex()));
6900 } else {
6901 LOG(FATAL) << "Unimplemented" << source << " <-> " << destination;
6902 }
Scott Wakelingfe885462016-09-22 10:24:38 +01006903}
6904
Nicolas Geoffray13a797b2017-03-15 16:41:31 +00006905void ParallelMoveResolverARMVIXL::SpillScratch(int reg) {
6906 __ Push(vixl32::Register(reg));
Scott Wakelingfe885462016-09-22 10:24:38 +01006907}
6908
Nicolas Geoffray13a797b2017-03-15 16:41:31 +00006909void ParallelMoveResolverARMVIXL::RestoreScratch(int reg) {
6910 __ Pop(vixl32::Register(reg));
Scott Wakelingfe885462016-09-22 10:24:38 +01006911}
6912
Artem Serov02d37832016-10-25 15:25:33 +01006913HLoadClass::LoadKind CodeGeneratorARMVIXL::GetSupportedLoadClassKind(
Artem Serovd4cc5b22016-11-04 11:19:09 +00006914 HLoadClass::LoadKind desired_class_load_kind) {
6915 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006916 case HLoadClass::LoadKind::kInvalid:
6917 LOG(FATAL) << "UNREACHABLE";
6918 UNREACHABLE();
Artem Serovd4cc5b22016-11-04 11:19:09 +00006919 case HLoadClass::LoadKind::kReferrersClass:
6920 break;
Artem Serovd4cc5b22016-11-04 11:19:09 +00006921 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006922 case HLoadClass::LoadKind::kBootImageRelRo:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006923 case HLoadClass::LoadKind::kBssEntry:
6924 DCHECK(!Runtime::Current()->UseJitCompilation());
6925 break;
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006926 case HLoadClass::LoadKind::kJitBootImageAddress:
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006927 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006928 DCHECK(Runtime::Current()->UseJitCompilation());
Artem Serovc5fcb442016-12-02 19:19:58 +00006929 break;
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006930 case HLoadClass::LoadKind::kRuntimeCall:
Artem Serovd4cc5b22016-11-04 11:19:09 +00006931 break;
6932 }
6933 return desired_class_load_kind;
Artem Serov02d37832016-10-25 15:25:33 +01006934}
6935
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006936void LocationsBuilderARMVIXL::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00006937 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006938 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006939 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00006940 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006941 cls,
6942 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00006943 LocationFrom(r0));
Vladimir Markoea4c1262017-02-06 19:59:33 +00006944 DCHECK(calling_convention.GetRegisterAt(0).Is(r0));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006945 return;
6946 }
Vladimir Marko41559982017-01-06 14:04:23 +00006947 DCHECK(!cls->NeedsAccessCheck());
Scott Wakelingfe885462016-09-22 10:24:38 +01006948
Artem Serovd4cc5b22016-11-04 11:19:09 +00006949 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
6950 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006951 ? LocationSummary::kCallOnSlowPath
6952 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01006953 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(cls, call_kind);
Artem Serovd4cc5b22016-11-04 11:19:09 +00006954 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006955 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Artem Serovd4cc5b22016-11-04 11:19:09 +00006956 }
6957
Vladimir Marko41559982017-01-06 14:04:23 +00006958 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006959 locations->SetInAt(0, Location::RequiresRegister());
6960 }
6961 locations->SetOut(Location::RequiresRegister());
Vladimir Markoea4c1262017-02-06 19:59:33 +00006962 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
6963 if (!kUseReadBarrier || kUseBakerReadBarrier) {
6964 // Rely on the type resolution or initialization and marking to save everything we need.
Vladimir Marko3232dbb2018-07-25 15:42:46 +01006965 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Vladimir Markoea4c1262017-02-06 19:59:33 +00006966 } else {
6967 // For non-Baker read barrier we have a temp-clobbering call.
6968 }
6969 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006970}
6971
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006972// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
6973// move.
6974void InstructionCodeGeneratorARMVIXL::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00006975 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006976 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00006977 codegen_->GenerateLoadClassRuntimeCall(cls);
Orion Hodson4c8e12e2018-05-18 08:33:20 +01006978 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 14);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006979 return;
6980 }
Vladimir Marko41559982017-01-06 14:04:23 +00006981 DCHECK(!cls->NeedsAccessCheck());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006982
Vladimir Marko41559982017-01-06 14:04:23 +00006983 LocationSummary* locations = cls->GetLocations();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006984 Location out_loc = locations->Out();
6985 vixl32::Register out = OutputRegister(cls);
6986
Artem Serovd4cc5b22016-11-04 11:19:09 +00006987 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
6988 ? kWithoutReadBarrier
6989 : kCompilerReadBarrierOption;
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006990 bool generate_null_check = false;
Vladimir Marko41559982017-01-06 14:04:23 +00006991 switch (load_kind) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006992 case HLoadClass::LoadKind::kReferrersClass: {
6993 DCHECK(!cls->CanCallRuntime());
6994 DCHECK(!cls->MustGenerateClinitCheck());
6995 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
6996 vixl32::Register current_method = InputRegisterAt(cls, 0);
Vladimir Markoca1e0382018-04-11 09:58:41 +00006997 codegen_->GenerateGcRootFieldLoad(cls,
6998 out_loc,
6999 current_method,
7000 ArtMethod::DeclaringClassOffset().Int32Value(),
7001 read_barrier_option);
Artem Serovd4cc5b22016-11-04 11:19:09 +00007002 break;
7003 }
Artem Serovd4cc5b22016-11-04 11:19:09 +00007004 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007005 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Artem Serovd4cc5b22016-11-04 11:19:09 +00007006 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
7007 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00007008 codegen_->NewBootImageTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
Artem Serovd4cc5b22016-11-04 11:19:09 +00007009 codegen_->EmitMovwMovtPlaceholder(labels, out);
7010 break;
7011 }
Vladimir Markoe47f60c2018-02-21 13:43:28 +00007012 case HLoadClass::LoadKind::kBootImageRelRo: {
Vladimir Marko94ec2db2017-09-06 17:21:03 +01007013 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
7014 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
Vladimir Markoe47f60c2018-02-21 13:43:28 +00007015 codegen_->NewBootImageRelRoPatch(codegen_->GetBootImageOffset(cls));
Vladimir Marko94ec2db2017-09-06 17:21:03 +01007016 codegen_->EmitMovwMovtPlaceholder(labels, out);
7017 __ Ldr(out, MemOperand(out, /* offset */ 0));
Vladimir Marko94ec2db2017-09-06 17:21:03 +01007018 break;
7019 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007020 case HLoadClass::LoadKind::kBssEntry: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007021 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
Vladimir Marko1998cd02017-01-13 13:02:58 +00007022 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex());
Vladimir Markof3c52b42017-11-17 17:32:12 +00007023 codegen_->EmitMovwMovtPlaceholder(labels, out);
Vladimir Markoca1e0382018-04-11 09:58:41 +00007024 codegen_->GenerateGcRootFieldLoad(cls, out_loc, out, /* offset */ 0, read_barrier_option);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007025 generate_null_check = true;
7026 break;
7027 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01007028 case HLoadClass::LoadKind::kJitBootImageAddress: {
7029 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
7030 uint32_t address = reinterpret_cast32<uint32_t>(cls->GetClass().Get());
7031 DCHECK_NE(address, 0u);
7032 __ Ldr(out, codegen_->DeduplicateBootImageAddressLiteral(address));
7033 break;
7034 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007035 case HLoadClass::LoadKind::kJitTableAddress: {
Artem Serovc5fcb442016-12-02 19:19:58 +00007036 __ Ldr(out, codegen_->DeduplicateJitClassLiteral(cls->GetDexFile(),
7037 cls->GetTypeIndex(),
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007038 cls->GetClass()));
Artem Serovc5fcb442016-12-02 19:19:58 +00007039 // /* GcRoot<mirror::Class> */ out = *out
Vladimir Markoca1e0382018-04-11 09:58:41 +00007040 codegen_->GenerateGcRootFieldLoad(cls, out_loc, out, /* offset */ 0, read_barrier_option);
Artem Serovd4cc5b22016-11-04 11:19:09 +00007041 break;
7042 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007043 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00007044 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00007045 LOG(FATAL) << "UNREACHABLE";
7046 UNREACHABLE();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007047 }
7048
7049 if (generate_null_check || cls->MustGenerateClinitCheck()) {
7050 DCHECK(cls->CanCallRuntime());
Vladimir Marko174b2e22017-10-12 13:34:49 +01007051 LoadClassSlowPathARMVIXL* slow_path =
Vladimir Markoa9f303c2018-07-20 16:43:56 +01007052 new (codegen_->GetScopedAllocator()) LoadClassSlowPathARMVIXL(cls, cls);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007053 codegen_->AddSlowPath(slow_path);
7054 if (generate_null_check) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00007055 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007056 }
7057 if (cls->MustGenerateClinitCheck()) {
7058 GenerateClassInitializationCheck(slow_path, out);
7059 } else {
7060 __ Bind(slow_path->GetExitLabel());
7061 }
Orion Hodson4c8e12e2018-05-18 08:33:20 +01007062 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 15);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007063 }
7064}
7065
Orion Hodsondbaa5c72018-05-10 08:22:46 +01007066void LocationsBuilderARMVIXL::VisitLoadMethodHandle(HLoadMethodHandle* load) {
7067 InvokeRuntimeCallingConventionARMVIXL calling_convention;
7068 Location location = LocationFrom(calling_convention.GetRegisterAt(0));
7069 CodeGenerator::CreateLoadMethodHandleRuntimeCallLocationSummary(load, location, location);
7070}
7071
7072void InstructionCodeGeneratorARMVIXL::VisitLoadMethodHandle(HLoadMethodHandle* load) {
7073 codegen_->GenerateLoadMethodHandleRuntimeCall(load);
7074}
7075
Orion Hodson18259d72018-04-12 11:18:23 +01007076void LocationsBuilderARMVIXL::VisitLoadMethodType(HLoadMethodType* load) {
7077 InvokeRuntimeCallingConventionARMVIXL calling_convention;
7078 Location location = LocationFrom(calling_convention.GetRegisterAt(0));
7079 CodeGenerator::CreateLoadMethodTypeRuntimeCallLocationSummary(load, location, location);
7080}
7081
7082void InstructionCodeGeneratorARMVIXL::VisitLoadMethodType(HLoadMethodType* load) {
7083 codegen_->GenerateLoadMethodTypeRuntimeCall(load);
7084}
7085
Artem Serov02d37832016-10-25 15:25:33 +01007086void LocationsBuilderARMVIXL::VisitClinitCheck(HClinitCheck* check) {
7087 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007088 new (GetGraph()->GetAllocator()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
Artem Serov02d37832016-10-25 15:25:33 +01007089 locations->SetInAt(0, Location::RequiresRegister());
7090 if (check->HasUses()) {
7091 locations->SetOut(Location::SameAsFirstInput());
7092 }
Vladimir Marko3232dbb2018-07-25 15:42:46 +01007093 // Rely on the type initialization to save everything we need.
7094 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Artem Serov02d37832016-10-25 15:25:33 +01007095}
7096
7097void InstructionCodeGeneratorARMVIXL::VisitClinitCheck(HClinitCheck* check) {
7098 // We assume the class is not null.
7099 LoadClassSlowPathARMVIXL* slow_path =
Vladimir Markoa9f303c2018-07-20 16:43:56 +01007100 new (codegen_->GetScopedAllocator()) LoadClassSlowPathARMVIXL(check->GetLoadClass(), check);
Artem Serov02d37832016-10-25 15:25:33 +01007101 codegen_->AddSlowPath(slow_path);
7102 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
7103}
7104
7105void InstructionCodeGeneratorARMVIXL::GenerateClassInitializationCheck(
7106 LoadClassSlowPathARMVIXL* slow_path, vixl32::Register class_reg) {
7107 UseScratchRegisterScope temps(GetVIXLAssembler());
7108 vixl32::Register temp = temps.Acquire();
Vladimir Markodc682aa2018-01-04 18:42:57 +00007109 constexpr size_t status_lsb_position = SubtypeCheckBits::BitStructSizeOf();
7110 const size_t status_byte_offset =
7111 mirror::Class::StatusOffset().SizeValue() + (status_lsb_position / kBitsPerByte);
7112 constexpr uint32_t shifted_initialized_value =
7113 enum_cast<uint32_t>(ClassStatus::kInitialized) << (status_lsb_position % kBitsPerByte);
7114
7115 GetAssembler()->LoadFromOffset(kLoadUnsignedByte, temp, class_reg, status_byte_offset);
7116 __ Cmp(temp, shifted_initialized_value);
Vladimir Marko2c64a832018-01-04 11:31:56 +00007117 __ B(lo, slow_path->GetEntryLabel());
Artem Serov02d37832016-10-25 15:25:33 +01007118 // Even if the initialized flag is set, we may be in a situation where caches are not synced
7119 // properly. Therefore, we do a memory fence.
7120 __ Dmb(ISH);
7121 __ Bind(slow_path->GetExitLabel());
7122}
7123
Vladimir Marko175e7862018-03-27 09:03:13 +00007124void InstructionCodeGeneratorARMVIXL::GenerateBitstringTypeCheckCompare(
7125 HTypeCheckInstruction* check,
7126 vixl32::Register temp,
7127 vixl32::FlagsUpdate flags_update) {
7128 uint32_t path_to_root = check->GetBitstringPathToRoot();
7129 uint32_t mask = check->GetBitstringMask();
7130 DCHECK(IsPowerOfTwo(mask + 1));
7131 size_t mask_bits = WhichPowerOf2(mask + 1);
7132
7133 // Note that HInstanceOf shall check for zero value in `temp` but HCheckCast needs
7134 // the Z flag for BNE. This is indicated by the `flags_update` parameter.
7135 if (mask_bits == 16u) {
7136 // Load only the bitstring part of the status word.
7137 __ Ldrh(temp, MemOperand(temp, mirror::Class::StatusOffset().Int32Value()));
7138 // Check if the bitstring bits are equal to `path_to_root`.
7139 if (flags_update == SetFlags) {
7140 __ Cmp(temp, path_to_root);
7141 } else {
7142 __ Sub(temp, temp, path_to_root);
7143 }
7144 } else {
7145 // /* uint32_t */ temp = temp->status_
7146 __ Ldr(temp, MemOperand(temp, mirror::Class::StatusOffset().Int32Value()));
7147 if (GetAssembler()->ShifterOperandCanHold(SUB, path_to_root)) {
7148 // Compare the bitstring bits using SUB.
7149 __ Sub(temp, temp, path_to_root);
7150 // Shift out bits that do not contribute to the comparison.
7151 __ Lsl(flags_update, temp, temp, dchecked_integral_cast<uint32_t>(32u - mask_bits));
7152 } else if (IsUint<16>(path_to_root)) {
7153 if (temp.IsLow()) {
7154 // Note: Optimized for size but contains one more dependent instruction than necessary.
7155 // MOVW+SUB(register) would be 8 bytes unless we find a low-reg temporary but the
7156 // macro assembler would use the high reg IP for the constant by default.
7157 // Compare the bitstring bits using SUB.
7158 __ Sub(temp, temp, path_to_root & 0x00ffu); // 16-bit SUB (immediate) T2
7159 __ Sub(temp, temp, path_to_root & 0xff00u); // 32-bit SUB (immediate) T3
7160 // Shift out bits that do not contribute to the comparison.
7161 __ Lsl(flags_update, temp, temp, dchecked_integral_cast<uint32_t>(32u - mask_bits));
7162 } else {
7163 // Extract the bitstring bits.
7164 __ Ubfx(temp, temp, 0, mask_bits);
7165 // Check if the bitstring bits are equal to `path_to_root`.
7166 if (flags_update == SetFlags) {
7167 __ Cmp(temp, path_to_root);
7168 } else {
7169 __ Sub(temp, temp, path_to_root);
7170 }
7171 }
7172 } else {
7173 // Shift out bits that do not contribute to the comparison.
7174 __ Lsl(temp, temp, dchecked_integral_cast<uint32_t>(32u - mask_bits));
7175 // Check if the shifted bitstring bits are equal to `path_to_root << (32u - mask_bits)`.
7176 if (flags_update == SetFlags) {
7177 __ Cmp(temp, path_to_root << (32u - mask_bits));
7178 } else {
7179 __ Sub(temp, temp, path_to_root << (32u - mask_bits));
7180 }
7181 }
7182 }
7183}
7184
Artem Serov02d37832016-10-25 15:25:33 +01007185HLoadString::LoadKind CodeGeneratorARMVIXL::GetSupportedLoadStringKind(
Artem Serovd4cc5b22016-11-04 11:19:09 +00007186 HLoadString::LoadKind desired_string_load_kind) {
7187 switch (desired_string_load_kind) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00007188 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00007189 case HLoadString::LoadKind::kBootImageRelRo:
Artem Serovd4cc5b22016-11-04 11:19:09 +00007190 case HLoadString::LoadKind::kBssEntry:
7191 DCHECK(!Runtime::Current()->UseJitCompilation());
7192 break;
Vladimir Marko8e524ad2018-07-13 10:27:43 +01007193 case HLoadString::LoadKind::kJitBootImageAddress:
Artem Serovd4cc5b22016-11-04 11:19:09 +00007194 case HLoadString::LoadKind::kJitTableAddress:
7195 DCHECK(Runtime::Current()->UseJitCompilation());
Artem Serovc5fcb442016-12-02 19:19:58 +00007196 break;
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007197 case HLoadString::LoadKind::kRuntimeCall:
Artem Serovd4cc5b22016-11-04 11:19:09 +00007198 break;
7199 }
7200 return desired_string_load_kind;
Artem Serov02d37832016-10-25 15:25:33 +01007201}
7202
7203void LocationsBuilderARMVIXL::VisitLoadString(HLoadString* load) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00007204 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Vladimir Markoca6fff82017-10-03 14:49:14 +01007205 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(load, call_kind);
Artem Serov02d37832016-10-25 15:25:33 +01007206 HLoadString::LoadKind load_kind = load->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007207 if (load_kind == HLoadString::LoadKind::kRuntimeCall) {
Artem Serov02d37832016-10-25 15:25:33 +01007208 locations->SetOut(LocationFrom(r0));
7209 } else {
7210 locations->SetOut(Location::RequiresRegister());
Artem Serovd4cc5b22016-11-04 11:19:09 +00007211 if (load_kind == HLoadString::LoadKind::kBssEntry) {
7212 if (!kUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00007213 // Rely on the pResolveString and marking to save everything we need, including temps.
Vladimir Marko3232dbb2018-07-25 15:42:46 +01007214 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Artem Serovd4cc5b22016-11-04 11:19:09 +00007215 } else {
7216 // For non-Baker read barrier we have a temp-clobbering call.
7217 }
7218 }
Artem Serov02d37832016-10-25 15:25:33 +01007219 }
7220}
7221
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007222// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
7223// move.
7224void InstructionCodeGeneratorARMVIXL::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Artem Serovd4cc5b22016-11-04 11:19:09 +00007225 LocationSummary* locations = load->GetLocations();
7226 Location out_loc = locations->Out();
7227 vixl32::Register out = OutputRegister(load);
7228 HLoadString::LoadKind load_kind = load->GetLoadKind();
7229
7230 switch (load_kind) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00007231 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
7232 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
7233 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00007234 codegen_->NewBootImageStringPatch(load->GetDexFile(), load->GetStringIndex());
Artem Serovd4cc5b22016-11-04 11:19:09 +00007235 codegen_->EmitMovwMovtPlaceholder(labels, out);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007236 return;
Artem Serovd4cc5b22016-11-04 11:19:09 +00007237 }
Vladimir Markoe47f60c2018-02-21 13:43:28 +00007238 case HLoadString::LoadKind::kBootImageRelRo: {
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007239 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
7240 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
Vladimir Markoe47f60c2018-02-21 13:43:28 +00007241 codegen_->NewBootImageRelRoPatch(codegen_->GetBootImageOffset(load));
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007242 codegen_->EmitMovwMovtPlaceholder(labels, out);
7243 __ Ldr(out, MemOperand(out, /* offset */ 0));
7244 return;
Artem Serovd4cc5b22016-11-04 11:19:09 +00007245 }
7246 case HLoadString::LoadKind::kBssEntry: {
7247 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Artem Serovd4cc5b22016-11-04 11:19:09 +00007248 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007249 codegen_->NewStringBssEntryPatch(load->GetDexFile(), load->GetStringIndex());
Vladimir Markof3c52b42017-11-17 17:32:12 +00007250 codegen_->EmitMovwMovtPlaceholder(labels, out);
Vladimir Markoca1e0382018-04-11 09:58:41 +00007251 codegen_->GenerateGcRootFieldLoad(
7252 load, out_loc, out, /* offset */ 0, kCompilerReadBarrierOption);
Artem Serovd4cc5b22016-11-04 11:19:09 +00007253 LoadStringSlowPathARMVIXL* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01007254 new (codegen_->GetScopedAllocator()) LoadStringSlowPathARMVIXL(load);
Artem Serovd4cc5b22016-11-04 11:19:09 +00007255 codegen_->AddSlowPath(slow_path);
7256 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
7257 __ Bind(slow_path->GetExitLabel());
Orion Hodson4c8e12e2018-05-18 08:33:20 +01007258 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 16);
Artem Serovd4cc5b22016-11-04 11:19:09 +00007259 return;
7260 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01007261 case HLoadString::LoadKind::kJitBootImageAddress: {
7262 uint32_t address = reinterpret_cast32<uint32_t>(load->GetString().Get());
7263 DCHECK_NE(address, 0u);
7264 __ Ldr(out, codegen_->DeduplicateBootImageAddressLiteral(address));
7265 return;
7266 }
Artem Serovd4cc5b22016-11-04 11:19:09 +00007267 case HLoadString::LoadKind::kJitTableAddress: {
Artem Serovc5fcb442016-12-02 19:19:58 +00007268 __ Ldr(out, codegen_->DeduplicateJitStringLiteral(load->GetDexFile(),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007269 load->GetStringIndex(),
7270 load->GetString()));
Artem Serovc5fcb442016-12-02 19:19:58 +00007271 // /* GcRoot<mirror::String> */ out = *out
Vladimir Markoca1e0382018-04-11 09:58:41 +00007272 codegen_->GenerateGcRootFieldLoad(
7273 load, out_loc, out, /* offset */ 0, kCompilerReadBarrierOption);
Artem Serovc5fcb442016-12-02 19:19:58 +00007274 return;
Artem Serovd4cc5b22016-11-04 11:19:09 +00007275 }
7276 default:
7277 break;
7278 }
Artem Serov02d37832016-10-25 15:25:33 +01007279
7280 // TODO: Re-add the compiler code to do string dex cache lookup again.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007281 DCHECK_EQ(load->GetLoadKind(), HLoadString::LoadKind::kRuntimeCall);
Artem Serov02d37832016-10-25 15:25:33 +01007282 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Andreas Gampe8a0128a2016-11-28 07:38:35 -08007283 __ Mov(calling_convention.GetRegisterAt(0), load->GetStringIndex().index_);
Artem Serov02d37832016-10-25 15:25:33 +01007284 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
7285 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Orion Hodson4c8e12e2018-05-18 08:33:20 +01007286 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 17);
Artem Serov02d37832016-10-25 15:25:33 +01007287}
7288
7289static int32_t GetExceptionTlsOffset() {
7290 return Thread::ExceptionOffset<kArmPointerSize>().Int32Value();
7291}
7292
7293void LocationsBuilderARMVIXL::VisitLoadException(HLoadException* load) {
7294 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007295 new (GetGraph()->GetAllocator()) LocationSummary(load, LocationSummary::kNoCall);
Artem Serov02d37832016-10-25 15:25:33 +01007296 locations->SetOut(Location::RequiresRegister());
7297}
7298
7299void InstructionCodeGeneratorARMVIXL::VisitLoadException(HLoadException* load) {
7300 vixl32::Register out = OutputRegister(load);
7301 GetAssembler()->LoadFromOffset(kLoadWord, out, tr, GetExceptionTlsOffset());
7302}
7303
7304
7305void LocationsBuilderARMVIXL::VisitClearException(HClearException* clear) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007306 new (GetGraph()->GetAllocator()) LocationSummary(clear, LocationSummary::kNoCall);
Artem Serov02d37832016-10-25 15:25:33 +01007307}
7308
7309void InstructionCodeGeneratorARMVIXL::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
7310 UseScratchRegisterScope temps(GetVIXLAssembler());
7311 vixl32::Register temp = temps.Acquire();
7312 __ Mov(temp, 0);
7313 GetAssembler()->StoreToOffset(kStoreWord, temp, tr, GetExceptionTlsOffset());
7314}
7315
7316void LocationsBuilderARMVIXL::VisitThrow(HThrow* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007317 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
7318 instruction, LocationSummary::kCallOnMainOnly);
Artem Serov02d37832016-10-25 15:25:33 +01007319 InvokeRuntimeCallingConventionARMVIXL calling_convention;
7320 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
7321}
7322
7323void InstructionCodeGeneratorARMVIXL::VisitThrow(HThrow* instruction) {
7324 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
7325 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
7326}
7327
Artem Serov657022c2016-11-23 14:19:38 +00007328// Temp is used for read barrier.
7329static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
7330 if (kEmitCompilerReadBarrier &&
7331 (kUseBakerReadBarrier ||
7332 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
7333 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
7334 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
7335 return 1;
7336 }
7337 return 0;
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007338}
7339
Artem Serov657022c2016-11-23 14:19:38 +00007340// Interface case has 3 temps, one for holding the number of interfaces, one for the current
7341// interface pointer, one for loading the current interface.
7342// The other checks have one temp for loading the object's class.
7343static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
7344 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
7345 return 3;
7346 }
7347 return 1 + NumberOfInstanceOfTemps(type_check_kind);
7348}
Artem Serovcfbe9132016-10-14 15:58:56 +01007349
7350void LocationsBuilderARMVIXL::VisitInstanceOf(HInstanceOf* instruction) {
7351 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
7352 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
7353 bool baker_read_barrier_slow_path = false;
7354 switch (type_check_kind) {
7355 case TypeCheckKind::kExactCheck:
7356 case TypeCheckKind::kAbstractClassCheck:
7357 case TypeCheckKind::kClassHierarchyCheck:
Vladimir Marko87584542017-12-12 17:47:52 +00007358 case TypeCheckKind::kArrayObjectCheck: {
7359 bool needs_read_barrier = CodeGenerator::InstanceOfNeedsReadBarrier(instruction);
7360 call_kind = needs_read_barrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
7361 baker_read_barrier_slow_path = kUseBakerReadBarrier && needs_read_barrier;
Artem Serovcfbe9132016-10-14 15:58:56 +01007362 break;
Vladimir Marko87584542017-12-12 17:47:52 +00007363 }
Artem Serovcfbe9132016-10-14 15:58:56 +01007364 case TypeCheckKind::kArrayCheck:
7365 case TypeCheckKind::kUnresolvedCheck:
7366 case TypeCheckKind::kInterfaceCheck:
7367 call_kind = LocationSummary::kCallOnSlowPath;
7368 break;
Vladimir Marko175e7862018-03-27 09:03:13 +00007369 case TypeCheckKind::kBitstringCheck:
7370 break;
Artem Serovcfbe9132016-10-14 15:58:56 +01007371 }
7372
Vladimir Markoca6fff82017-10-03 14:49:14 +01007373 LocationSummary* locations =
7374 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Artem Serovcfbe9132016-10-14 15:58:56 +01007375 if (baker_read_barrier_slow_path) {
7376 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
7377 }
7378 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko175e7862018-03-27 09:03:13 +00007379 if (type_check_kind == TypeCheckKind::kBitstringCheck) {
7380 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
7381 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
7382 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
7383 } else {
7384 locations->SetInAt(1, Location::RequiresRegister());
7385 }
Artem Serovcfbe9132016-10-14 15:58:56 +01007386 // The "out" register is used as a temporary, so it overlaps with the inputs.
7387 // Note that TypeCheckSlowPathARM uses this register too.
7388 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Artem Serov657022c2016-11-23 14:19:38 +00007389 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Artem Serovcfbe9132016-10-14 15:58:56 +01007390}
7391
7392void InstructionCodeGeneratorARMVIXL::VisitInstanceOf(HInstanceOf* instruction) {
7393 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
7394 LocationSummary* locations = instruction->GetLocations();
7395 Location obj_loc = locations->InAt(0);
7396 vixl32::Register obj = InputRegisterAt(instruction, 0);
Vladimir Marko175e7862018-03-27 09:03:13 +00007397 vixl32::Register cls = (type_check_kind == TypeCheckKind::kBitstringCheck)
7398 ? vixl32::Register()
7399 : InputRegisterAt(instruction, 1);
Artem Serovcfbe9132016-10-14 15:58:56 +01007400 Location out_loc = locations->Out();
7401 vixl32::Register out = OutputRegister(instruction);
Artem Serov657022c2016-11-23 14:19:38 +00007402 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
7403 DCHECK_LE(num_temps, 1u);
7404 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Artem Serovcfbe9132016-10-14 15:58:56 +01007405 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
7406 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
7407 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
7408 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007409 vixl32::Label done;
7410 vixl32::Label* const final_label = codegen_->GetFinalLabel(instruction, &done);
Artem Serovcfbe9132016-10-14 15:58:56 +01007411 SlowPathCodeARMVIXL* slow_path = nullptr;
7412
7413 // Return 0 if `obj` is null.
7414 // avoid null check if we know obj is not null.
7415 if (instruction->MustDoNullCheck()) {
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007416 DCHECK(!out.Is(obj));
7417 __ Mov(out, 0);
7418 __ CompareAndBranchIfZero(obj, final_label, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007419 }
7420
Artem Serovcfbe9132016-10-14 15:58:56 +01007421 switch (type_check_kind) {
7422 case TypeCheckKind::kExactCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00007423 ReadBarrierOption read_barrier_option =
7424 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier6beced42016-11-15 15:51:31 -08007425 // /* HeapReference<Class> */ out = obj->klass_
7426 GenerateReferenceLoadTwoRegisters(instruction,
7427 out_loc,
7428 obj_loc,
7429 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00007430 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007431 read_barrier_option);
Artem Serovcfbe9132016-10-14 15:58:56 +01007432 // Classes must be equal for the instanceof to succeed.
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007433 __ Cmp(out, cls);
7434 // We speculatively set the result to false without changing the condition
7435 // flags, which allows us to avoid some branching later.
7436 __ Mov(LeaveFlags, out, 0);
7437
7438 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
7439 // we check that the output is in a low register, so that a 16-bit MOV
7440 // encoding can be used.
7441 if (out.IsLow()) {
7442 // We use the scope because of the IT block that follows.
7443 ExactAssemblyScope guard(GetVIXLAssembler(),
7444 2 * vixl32::k16BitT32InstructionSizeInBytes,
7445 CodeBufferCheckScope::kExactSize);
7446
7447 __ it(eq);
7448 __ mov(eq, out, 1);
7449 } else {
7450 __ B(ne, final_label, /* far_target */ false);
7451 __ Mov(out, 1);
7452 }
7453
Artem Serovcfbe9132016-10-14 15:58:56 +01007454 break;
7455 }
7456
7457 case TypeCheckKind::kAbstractClassCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00007458 ReadBarrierOption read_barrier_option =
7459 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier6beced42016-11-15 15:51:31 -08007460 // /* HeapReference<Class> */ out = obj->klass_
7461 GenerateReferenceLoadTwoRegisters(instruction,
7462 out_loc,
7463 obj_loc,
7464 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00007465 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007466 read_barrier_option);
Artem Serovcfbe9132016-10-14 15:58:56 +01007467 // If the class is abstract, we eagerly fetch the super class of the
7468 // object to avoid doing a comparison we know will fail.
7469 vixl32::Label loop;
7470 __ Bind(&loop);
7471 // /* HeapReference<Class> */ out = out->super_class_
Artem Serov657022c2016-11-23 14:19:38 +00007472 GenerateReferenceLoadOneRegister(instruction,
7473 out_loc,
7474 super_offset,
7475 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007476 read_barrier_option);
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007477 // If `out` is null, we use it for the result, and jump to the final label.
Anton Kirilov6f644202017-02-27 18:29:45 +00007478 __ CompareAndBranchIfZero(out, final_label, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007479 __ Cmp(out, cls);
Artem Serov517d9f62016-12-12 15:51:15 +00007480 __ B(ne, &loop, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007481 __ Mov(out, 1);
Artem Serovcfbe9132016-10-14 15:58:56 +01007482 break;
7483 }
7484
7485 case TypeCheckKind::kClassHierarchyCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00007486 ReadBarrierOption read_barrier_option =
7487 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier6beced42016-11-15 15:51:31 -08007488 // /* HeapReference<Class> */ out = obj->klass_
7489 GenerateReferenceLoadTwoRegisters(instruction,
7490 out_loc,
7491 obj_loc,
7492 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00007493 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007494 read_barrier_option);
Artem Serovcfbe9132016-10-14 15:58:56 +01007495 // Walk over the class hierarchy to find a match.
7496 vixl32::Label loop, success;
7497 __ Bind(&loop);
7498 __ Cmp(out, cls);
Artem Serov517d9f62016-12-12 15:51:15 +00007499 __ B(eq, &success, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007500 // /* HeapReference<Class> */ out = out->super_class_
Artem Serov657022c2016-11-23 14:19:38 +00007501 GenerateReferenceLoadOneRegister(instruction,
7502 out_loc,
7503 super_offset,
7504 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007505 read_barrier_option);
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007506 // This is essentially a null check, but it sets the condition flags to the
7507 // proper value for the code that follows the loop, i.e. not `eq`.
7508 __ Cmp(out, 1);
7509 __ B(hs, &loop, /* far_target */ false);
7510
7511 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
7512 // we check that the output is in a low register, so that a 16-bit MOV
7513 // encoding can be used.
7514 if (out.IsLow()) {
7515 // If `out` is null, we use it for the result, and the condition flags
7516 // have already been set to `ne`, so the IT block that comes afterwards
7517 // (and which handles the successful case) turns into a NOP (instead of
7518 // overwriting `out`).
7519 __ Bind(&success);
7520
7521 // We use the scope because of the IT block that follows.
7522 ExactAssemblyScope guard(GetVIXLAssembler(),
7523 2 * vixl32::k16BitT32InstructionSizeInBytes,
7524 CodeBufferCheckScope::kExactSize);
7525
7526 // There is only one branch to the `success` label (which is bound to this
7527 // IT block), and it has the same condition, `eq`, so in that case the MOV
7528 // is executed.
7529 __ it(eq);
7530 __ mov(eq, out, 1);
7531 } else {
7532 // If `out` is null, we use it for the result, and jump to the final label.
Anton Kirilov6f644202017-02-27 18:29:45 +00007533 __ B(final_label);
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007534 __ Bind(&success);
7535 __ Mov(out, 1);
Artem Serovcfbe9132016-10-14 15:58:56 +01007536 }
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007537
Artem Serovcfbe9132016-10-14 15:58:56 +01007538 break;
7539 }
7540
7541 case TypeCheckKind::kArrayObjectCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00007542 ReadBarrierOption read_barrier_option =
7543 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier6beced42016-11-15 15:51:31 -08007544 // /* HeapReference<Class> */ out = obj->klass_
7545 GenerateReferenceLoadTwoRegisters(instruction,
7546 out_loc,
7547 obj_loc,
7548 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00007549 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007550 read_barrier_option);
Artem Serovcfbe9132016-10-14 15:58:56 +01007551 // Do an exact check.
7552 vixl32::Label exact_check;
7553 __ Cmp(out, cls);
Artem Serov517d9f62016-12-12 15:51:15 +00007554 __ B(eq, &exact_check, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007555 // Otherwise, we need to check that the object's class is a non-primitive array.
7556 // /* HeapReference<Class> */ out = out->component_type_
Artem Serov657022c2016-11-23 14:19:38 +00007557 GenerateReferenceLoadOneRegister(instruction,
7558 out_loc,
7559 component_offset,
7560 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007561 read_barrier_option);
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007562 // If `out` is null, we use it for the result, and jump to the final label.
Anton Kirilov6f644202017-02-27 18:29:45 +00007563 __ CompareAndBranchIfZero(out, final_label, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007564 GetAssembler()->LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
7565 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007566 __ Cmp(out, 0);
7567 // We speculatively set the result to false without changing the condition
7568 // flags, which allows us to avoid some branching later.
7569 __ Mov(LeaveFlags, out, 0);
7570
7571 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
7572 // we check that the output is in a low register, so that a 16-bit MOV
7573 // encoding can be used.
7574 if (out.IsLow()) {
7575 __ Bind(&exact_check);
7576
7577 // We use the scope because of the IT block that follows.
7578 ExactAssemblyScope guard(GetVIXLAssembler(),
7579 2 * vixl32::k16BitT32InstructionSizeInBytes,
7580 CodeBufferCheckScope::kExactSize);
7581
7582 __ it(eq);
7583 __ mov(eq, out, 1);
7584 } else {
7585 __ B(ne, final_label, /* far_target */ false);
7586 __ Bind(&exact_check);
7587 __ Mov(out, 1);
7588 }
7589
Artem Serovcfbe9132016-10-14 15:58:56 +01007590 break;
7591 }
7592
7593 case TypeCheckKind::kArrayCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00007594 // No read barrier since the slow path will retry upon failure.
Mathieu Chartier6beced42016-11-15 15:51:31 -08007595 // /* HeapReference<Class> */ out = obj->klass_
7596 GenerateReferenceLoadTwoRegisters(instruction,
7597 out_loc,
7598 obj_loc,
7599 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00007600 maybe_temp_loc,
7601 kWithoutReadBarrier);
Artem Serovcfbe9132016-10-14 15:58:56 +01007602 __ Cmp(out, cls);
7603 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01007604 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathARMVIXL(
7605 instruction, /* is_fatal */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007606 codegen_->AddSlowPath(slow_path);
7607 __ B(ne, slow_path->GetEntryLabel());
7608 __ Mov(out, 1);
Artem Serovcfbe9132016-10-14 15:58:56 +01007609 break;
7610 }
7611
7612 case TypeCheckKind::kUnresolvedCheck:
7613 case TypeCheckKind::kInterfaceCheck: {
7614 // Note that we indeed only call on slow path, but we always go
7615 // into the slow path for the unresolved and interface check
7616 // cases.
7617 //
7618 // We cannot directly call the InstanceofNonTrivial runtime
7619 // entry point without resorting to a type checking slow path
7620 // here (i.e. by calling InvokeRuntime directly), as it would
7621 // require to assign fixed registers for the inputs of this
7622 // HInstanceOf instruction (following the runtime calling
7623 // convention), which might be cluttered by the potential first
7624 // read barrier emission at the beginning of this method.
7625 //
7626 // TODO: Introduce a new runtime entry point taking the object
7627 // to test (instead of its class) as argument, and let it deal
7628 // with the read barrier issues. This will let us refactor this
7629 // case of the `switch` code as it was previously (with a direct
7630 // call to the runtime not using a type checking slow path).
7631 // This should also be beneficial for the other cases above.
7632 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01007633 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathARMVIXL(
7634 instruction, /* is_fatal */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007635 codegen_->AddSlowPath(slow_path);
7636 __ B(slow_path->GetEntryLabel());
Artem Serovcfbe9132016-10-14 15:58:56 +01007637 break;
7638 }
Vladimir Marko175e7862018-03-27 09:03:13 +00007639
7640 case TypeCheckKind::kBitstringCheck: {
7641 // /* HeapReference<Class> */ temp = obj->klass_
7642 GenerateReferenceLoadTwoRegisters(instruction,
7643 out_loc,
7644 obj_loc,
7645 class_offset,
7646 maybe_temp_loc,
7647 kWithoutReadBarrier);
7648
7649 GenerateBitstringTypeCheckCompare(instruction, out, DontCare);
7650 // If `out` is a low reg and we would have another low reg temp, we could
7651 // optimize this as RSBS+ADC, see GenerateConditionWithZero().
7652 //
7653 // Also, in some cases when `out` is a low reg and we're loading a constant to IP
7654 // it would make sense to use CMP+MOV+IT+MOV instead of SUB+CLZ+LSR as the code size
7655 // would be the same and we would have fewer direct data dependencies.
7656 codegen_->GenerateConditionWithZero(kCondEQ, out, out); // CLZ+LSR
7657 break;
7658 }
Artem Serovcfbe9132016-10-14 15:58:56 +01007659 }
7660
Artem Serovcfbe9132016-10-14 15:58:56 +01007661 if (done.IsReferenced()) {
7662 __ Bind(&done);
7663 }
7664
7665 if (slow_path != nullptr) {
7666 __ Bind(slow_path->GetExitLabel());
7667 }
7668}
7669
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007670void LocationsBuilderARMVIXL::VisitCheckCast(HCheckCast* instruction) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007671 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko87584542017-12-12 17:47:52 +00007672 LocationSummary::CallKind call_kind = CodeGenerator::GetCheckCastCallKind(instruction);
Vladimir Markoca6fff82017-10-03 14:49:14 +01007673 LocationSummary* locations =
7674 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007675 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko175e7862018-03-27 09:03:13 +00007676 if (type_check_kind == TypeCheckKind::kBitstringCheck) {
7677 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
7678 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
7679 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
7680 } else {
7681 locations->SetInAt(1, Location::RequiresRegister());
7682 }
Artem Serov657022c2016-11-23 14:19:38 +00007683 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007684}
7685
7686void InstructionCodeGeneratorARMVIXL::VisitCheckCast(HCheckCast* instruction) {
7687 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
7688 LocationSummary* locations = instruction->GetLocations();
7689 Location obj_loc = locations->InAt(0);
7690 vixl32::Register obj = InputRegisterAt(instruction, 0);
Vladimir Marko175e7862018-03-27 09:03:13 +00007691 vixl32::Register cls = (type_check_kind == TypeCheckKind::kBitstringCheck)
7692 ? vixl32::Register()
7693 : InputRegisterAt(instruction, 1);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007694 Location temp_loc = locations->GetTemp(0);
7695 vixl32::Register temp = RegisterFrom(temp_loc);
Artem Serov657022c2016-11-23 14:19:38 +00007696 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
7697 DCHECK_LE(num_temps, 3u);
7698 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
7699 Location maybe_temp3_loc = (num_temps >= 3) ? locations->GetTemp(2) : Location::NoLocation();
7700 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
7701 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
7702 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
7703 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
7704 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
7705 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
7706 const uint32_t object_array_data_offset =
7707 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007708
Vladimir Marko87584542017-12-12 17:47:52 +00007709 bool is_type_check_slow_path_fatal = CodeGenerator::IsTypeCheckSlowPathFatal(instruction);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007710 SlowPathCodeARMVIXL* type_check_slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01007711 new (codegen_->GetScopedAllocator()) TypeCheckSlowPathARMVIXL(
7712 instruction, is_type_check_slow_path_fatal);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007713 codegen_->AddSlowPath(type_check_slow_path);
7714
7715 vixl32::Label done;
Anton Kirilov6f644202017-02-27 18:29:45 +00007716 vixl32::Label* final_label = codegen_->GetFinalLabel(instruction, &done);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007717 // Avoid null check if we know obj is not null.
7718 if (instruction->MustDoNullCheck()) {
Anton Kirilov6f644202017-02-27 18:29:45 +00007719 __ CompareAndBranchIfZero(obj, final_label, /* far_target */ false);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007720 }
7721
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007722 switch (type_check_kind) {
7723 case TypeCheckKind::kExactCheck:
7724 case TypeCheckKind::kArrayCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00007725 // /* HeapReference<Class> */ temp = obj->klass_
7726 GenerateReferenceLoadTwoRegisters(instruction,
7727 temp_loc,
7728 obj_loc,
7729 class_offset,
7730 maybe_temp2_loc,
7731 kWithoutReadBarrier);
7732
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007733 __ Cmp(temp, cls);
7734 // Jump to slow path for throwing the exception or doing a
7735 // more involved array check.
7736 __ B(ne, type_check_slow_path->GetEntryLabel());
7737 break;
7738 }
7739
7740 case TypeCheckKind::kAbstractClassCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00007741 // /* HeapReference<Class> */ temp = obj->klass_
7742 GenerateReferenceLoadTwoRegisters(instruction,
7743 temp_loc,
7744 obj_loc,
7745 class_offset,
7746 maybe_temp2_loc,
7747 kWithoutReadBarrier);
7748
Artem Serovcfbe9132016-10-14 15:58:56 +01007749 // If the class is abstract, we eagerly fetch the super class of the
7750 // object to avoid doing a comparison we know will fail.
7751 vixl32::Label loop;
7752 __ Bind(&loop);
7753 // /* HeapReference<Class> */ temp = temp->super_class_
Artem Serov657022c2016-11-23 14:19:38 +00007754 GenerateReferenceLoadOneRegister(instruction,
7755 temp_loc,
7756 super_offset,
7757 maybe_temp2_loc,
7758 kWithoutReadBarrier);
Artem Serovcfbe9132016-10-14 15:58:56 +01007759
7760 // If the class reference currently in `temp` is null, jump to the slow path to throw the
7761 // exception.
xueliang.zhongf51bc622016-11-04 09:23:32 +00007762 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
Artem Serovcfbe9132016-10-14 15:58:56 +01007763
7764 // Otherwise, compare the classes.
7765 __ Cmp(temp, cls);
Artem Serov517d9f62016-12-12 15:51:15 +00007766 __ B(ne, &loop, /* far_target */ false);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007767 break;
7768 }
7769
7770 case TypeCheckKind::kClassHierarchyCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00007771 // /* HeapReference<Class> */ temp = obj->klass_
7772 GenerateReferenceLoadTwoRegisters(instruction,
7773 temp_loc,
7774 obj_loc,
7775 class_offset,
7776 maybe_temp2_loc,
7777 kWithoutReadBarrier);
7778
Artem Serovcfbe9132016-10-14 15:58:56 +01007779 // Walk over the class hierarchy to find a match.
7780 vixl32::Label loop;
7781 __ Bind(&loop);
7782 __ Cmp(temp, cls);
Anton Kirilov6f644202017-02-27 18:29:45 +00007783 __ B(eq, final_label, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007784
7785 // /* HeapReference<Class> */ temp = temp->super_class_
Artem Serov657022c2016-11-23 14:19:38 +00007786 GenerateReferenceLoadOneRegister(instruction,
7787 temp_loc,
7788 super_offset,
7789 maybe_temp2_loc,
7790 kWithoutReadBarrier);
Artem Serovcfbe9132016-10-14 15:58:56 +01007791
7792 // If the class reference currently in `temp` is null, jump to the slow path to throw the
7793 // exception.
xueliang.zhongf51bc622016-11-04 09:23:32 +00007794 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
Artem Serovcfbe9132016-10-14 15:58:56 +01007795 // Otherwise, jump to the beginning of the loop.
7796 __ B(&loop);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007797 break;
7798 }
7799
Artem Serovcfbe9132016-10-14 15:58:56 +01007800 case TypeCheckKind::kArrayObjectCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00007801 // /* HeapReference<Class> */ temp = obj->klass_
7802 GenerateReferenceLoadTwoRegisters(instruction,
7803 temp_loc,
7804 obj_loc,
7805 class_offset,
7806 maybe_temp2_loc,
7807 kWithoutReadBarrier);
7808
Artem Serovcfbe9132016-10-14 15:58:56 +01007809 // Do an exact check.
7810 __ Cmp(temp, cls);
Anton Kirilov6f644202017-02-27 18:29:45 +00007811 __ B(eq, final_label, /* far_target */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007812
7813 // Otherwise, we need to check that the object's class is a non-primitive array.
7814 // /* HeapReference<Class> */ temp = temp->component_type_
Artem Serov657022c2016-11-23 14:19:38 +00007815 GenerateReferenceLoadOneRegister(instruction,
7816 temp_loc,
7817 component_offset,
7818 maybe_temp2_loc,
7819 kWithoutReadBarrier);
Artem Serovcfbe9132016-10-14 15:58:56 +01007820 // If the component type is null, jump to the slow path to throw the exception.
xueliang.zhongf51bc622016-11-04 09:23:32 +00007821 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
Artem Serovcfbe9132016-10-14 15:58:56 +01007822 // Otherwise,the object is indeed an array, jump to label `check_non_primitive_component_type`
7823 // to further check that this component type is not a primitive type.
7824 GetAssembler()->LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007825 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
xueliang.zhongf51bc622016-11-04 09:23:32 +00007826 __ CompareAndBranchIfNonZero(temp, type_check_slow_path->GetEntryLabel());
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007827 break;
7828 }
7829
7830 case TypeCheckKind::kUnresolvedCheck:
Artem Serov657022c2016-11-23 14:19:38 +00007831 // We always go into the type check slow path for the unresolved check case.
Artem Serovcfbe9132016-10-14 15:58:56 +01007832 // We cannot directly call the CheckCast runtime entry point
7833 // without resorting to a type checking slow path here (i.e. by
7834 // calling InvokeRuntime directly), as it would require to
7835 // assign fixed registers for the inputs of this HInstanceOf
7836 // instruction (following the runtime calling convention), which
7837 // might be cluttered by the potential first read barrier
7838 // emission at the beginning of this method.
Artem Serov657022c2016-11-23 14:19:38 +00007839
Artem Serovcfbe9132016-10-14 15:58:56 +01007840 __ B(type_check_slow_path->GetEntryLabel());
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007841 break;
Artem Serov657022c2016-11-23 14:19:38 +00007842
7843 case TypeCheckKind::kInterfaceCheck: {
7844 // Avoid read barriers to improve performance of the fast path. We can not get false
7845 // positives by doing this.
7846 // /* HeapReference<Class> */ temp = obj->klass_
7847 GenerateReferenceLoadTwoRegisters(instruction,
7848 temp_loc,
7849 obj_loc,
7850 class_offset,
7851 maybe_temp2_loc,
7852 kWithoutReadBarrier);
7853
7854 // /* HeapReference<Class> */ temp = temp->iftable_
7855 GenerateReferenceLoadTwoRegisters(instruction,
7856 temp_loc,
7857 temp_loc,
7858 iftable_offset,
7859 maybe_temp2_loc,
7860 kWithoutReadBarrier);
7861 // Iftable is never null.
7862 __ Ldr(RegisterFrom(maybe_temp2_loc), MemOperand(temp, array_length_offset));
7863 // Loop through the iftable and check if any class matches.
7864 vixl32::Label start_loop;
7865 __ Bind(&start_loop);
7866 __ CompareAndBranchIfZero(RegisterFrom(maybe_temp2_loc),
7867 type_check_slow_path->GetEntryLabel());
7868 __ Ldr(RegisterFrom(maybe_temp3_loc), MemOperand(temp, object_array_data_offset));
7869 GetAssembler()->MaybeUnpoisonHeapReference(RegisterFrom(maybe_temp3_loc));
7870 // Go to next interface.
7871 __ Add(temp, temp, Operand::From(2 * kHeapReferenceSize));
7872 __ Sub(RegisterFrom(maybe_temp2_loc), RegisterFrom(maybe_temp2_loc), 2);
7873 // Compare the classes and continue the loop if they do not match.
7874 __ Cmp(cls, RegisterFrom(maybe_temp3_loc));
Artem Serov517d9f62016-12-12 15:51:15 +00007875 __ B(ne, &start_loop, /* far_target */ false);
Artem Serov657022c2016-11-23 14:19:38 +00007876 break;
7877 }
Vladimir Marko175e7862018-03-27 09:03:13 +00007878
7879 case TypeCheckKind::kBitstringCheck: {
7880 // /* HeapReference<Class> */ temp = obj->klass_
7881 GenerateReferenceLoadTwoRegisters(instruction,
7882 temp_loc,
7883 obj_loc,
7884 class_offset,
7885 maybe_temp2_loc,
7886 kWithoutReadBarrier);
7887
7888 GenerateBitstringTypeCheckCompare(instruction, temp, SetFlags);
7889 __ B(ne, type_check_slow_path->GetEntryLabel());
7890 break;
7891 }
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007892 }
Anton Kirilov6f644202017-02-27 18:29:45 +00007893 if (done.IsReferenced()) {
7894 __ Bind(&done);
7895 }
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007896
7897 __ Bind(type_check_slow_path->GetExitLabel());
7898}
7899
Artem Serov551b28f2016-10-18 19:11:30 +01007900void LocationsBuilderARMVIXL::VisitMonitorOperation(HMonitorOperation* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007901 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
7902 instruction, LocationSummary::kCallOnMainOnly);
Artem Serov551b28f2016-10-18 19:11:30 +01007903 InvokeRuntimeCallingConventionARMVIXL calling_convention;
7904 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
7905}
7906
7907void InstructionCodeGeneratorARMVIXL::VisitMonitorOperation(HMonitorOperation* instruction) {
7908 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
7909 instruction,
7910 instruction->GetDexPc());
7911 if (instruction->IsEnter()) {
7912 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
7913 } else {
7914 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
7915 }
Orion Hodson4c8e12e2018-05-18 08:33:20 +01007916 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ 18);
Artem Serov551b28f2016-10-18 19:11:30 +01007917}
7918
Artem Serov02109dd2016-09-23 17:17:54 +01007919void LocationsBuilderARMVIXL::VisitAnd(HAnd* instruction) {
7920 HandleBitwiseOperation(instruction, AND);
7921}
7922
7923void LocationsBuilderARMVIXL::VisitOr(HOr* instruction) {
7924 HandleBitwiseOperation(instruction, ORR);
7925}
7926
7927void LocationsBuilderARMVIXL::VisitXor(HXor* instruction) {
7928 HandleBitwiseOperation(instruction, EOR);
7929}
7930
7931void LocationsBuilderARMVIXL::HandleBitwiseOperation(HBinaryOperation* instruction, Opcode opcode) {
7932 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007933 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007934 DCHECK(instruction->GetResultType() == DataType::Type::kInt32
7935 || instruction->GetResultType() == DataType::Type::kInt64);
Artem Serov02109dd2016-09-23 17:17:54 +01007936 // Note: GVN reorders commutative operations to have the constant on the right hand side.
7937 locations->SetInAt(0, Location::RequiresRegister());
7938 locations->SetInAt(1, ArmEncodableConstantOrRegister(instruction->InputAt(1), opcode));
7939 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
7940}
7941
7942void InstructionCodeGeneratorARMVIXL::VisitAnd(HAnd* instruction) {
7943 HandleBitwiseOperation(instruction);
7944}
7945
7946void InstructionCodeGeneratorARMVIXL::VisitOr(HOr* instruction) {
7947 HandleBitwiseOperation(instruction);
7948}
7949
7950void InstructionCodeGeneratorARMVIXL::VisitXor(HXor* instruction) {
7951 HandleBitwiseOperation(instruction);
7952}
7953
Artem Serov2bbc9532016-10-21 11:51:50 +01007954void LocationsBuilderARMVIXL::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
7955 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007956 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007957 DCHECK(instruction->GetResultType() == DataType::Type::kInt32
7958 || instruction->GetResultType() == DataType::Type::kInt64);
Artem Serov2bbc9532016-10-21 11:51:50 +01007959
7960 locations->SetInAt(0, Location::RequiresRegister());
7961 locations->SetInAt(1, Location::RequiresRegister());
7962 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
7963}
7964
7965void InstructionCodeGeneratorARMVIXL::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
7966 LocationSummary* locations = instruction->GetLocations();
7967 Location first = locations->InAt(0);
7968 Location second = locations->InAt(1);
7969 Location out = locations->Out();
7970
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007971 if (instruction->GetResultType() == DataType::Type::kInt32) {
Artem Serov2bbc9532016-10-21 11:51:50 +01007972 vixl32::Register first_reg = RegisterFrom(first);
7973 vixl32::Register second_reg = RegisterFrom(second);
7974 vixl32::Register out_reg = RegisterFrom(out);
7975
7976 switch (instruction->GetOpKind()) {
7977 case HInstruction::kAnd:
7978 __ Bic(out_reg, first_reg, second_reg);
7979 break;
7980 case HInstruction::kOr:
7981 __ Orn(out_reg, first_reg, second_reg);
7982 break;
7983 // There is no EON on arm.
7984 case HInstruction::kXor:
7985 default:
7986 LOG(FATAL) << "Unexpected instruction " << instruction->DebugName();
7987 UNREACHABLE();
7988 }
7989 return;
7990
7991 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007992 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
Artem Serov2bbc9532016-10-21 11:51:50 +01007993 vixl32::Register first_low = LowRegisterFrom(first);
7994 vixl32::Register first_high = HighRegisterFrom(first);
7995 vixl32::Register second_low = LowRegisterFrom(second);
7996 vixl32::Register second_high = HighRegisterFrom(second);
7997 vixl32::Register out_low = LowRegisterFrom(out);
7998 vixl32::Register out_high = HighRegisterFrom(out);
7999
8000 switch (instruction->GetOpKind()) {
8001 case HInstruction::kAnd:
8002 __ Bic(out_low, first_low, second_low);
8003 __ Bic(out_high, first_high, second_high);
8004 break;
8005 case HInstruction::kOr:
8006 __ Orn(out_low, first_low, second_low);
8007 __ Orn(out_high, first_high, second_high);
8008 break;
8009 // There is no EON on arm.
8010 case HInstruction::kXor:
8011 default:
8012 LOG(FATAL) << "Unexpected instruction " << instruction->DebugName();
8013 UNREACHABLE();
8014 }
8015 }
8016}
8017
Anton Kirilov74234da2017-01-13 14:42:47 +00008018void LocationsBuilderARMVIXL::VisitDataProcWithShifterOp(
8019 HDataProcWithShifterOp* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008020 DCHECK(instruction->GetType() == DataType::Type::kInt32 ||
8021 instruction->GetType() == DataType::Type::kInt64);
Anton Kirilov74234da2017-01-13 14:42:47 +00008022 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008023 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008024 const bool overlap = instruction->GetType() == DataType::Type::kInt64 &&
Anton Kirilov74234da2017-01-13 14:42:47 +00008025 HDataProcWithShifterOp::IsExtensionOp(instruction->GetOpKind());
8026
8027 locations->SetInAt(0, Location::RequiresRegister());
8028 locations->SetInAt(1, Location::RequiresRegister());
8029 locations->SetOut(Location::RequiresRegister(),
8030 overlap ? Location::kOutputOverlap : Location::kNoOutputOverlap);
8031}
8032
8033void InstructionCodeGeneratorARMVIXL::VisitDataProcWithShifterOp(
8034 HDataProcWithShifterOp* instruction) {
8035 const LocationSummary* const locations = instruction->GetLocations();
8036 const HInstruction::InstructionKind kind = instruction->GetInstrKind();
8037 const HDataProcWithShifterOp::OpKind op_kind = instruction->GetOpKind();
8038
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008039 if (instruction->GetType() == DataType::Type::kInt32) {
Anton Kirilov420ee302017-02-21 18:10:26 +00008040 const vixl32::Register first = InputRegisterAt(instruction, 0);
8041 const vixl32::Register output = OutputRegister(instruction);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008042 const vixl32::Register second = instruction->InputAt(1)->GetType() == DataType::Type::kInt64
Anton Kirilov74234da2017-01-13 14:42:47 +00008043 ? LowRegisterFrom(locations->InAt(1))
8044 : InputRegisterAt(instruction, 1);
8045
Anton Kirilov420ee302017-02-21 18:10:26 +00008046 if (HDataProcWithShifterOp::IsExtensionOp(op_kind)) {
8047 DCHECK_EQ(kind, HInstruction::kAdd);
8048
8049 switch (op_kind) {
8050 case HDataProcWithShifterOp::kUXTB:
8051 __ Uxtab(output, first, second);
8052 break;
8053 case HDataProcWithShifterOp::kUXTH:
8054 __ Uxtah(output, first, second);
8055 break;
8056 case HDataProcWithShifterOp::kSXTB:
8057 __ Sxtab(output, first, second);
8058 break;
8059 case HDataProcWithShifterOp::kSXTH:
8060 __ Sxtah(output, first, second);
8061 break;
8062 default:
8063 LOG(FATAL) << "Unexpected operation kind: " << op_kind;
8064 UNREACHABLE();
8065 }
8066 } else {
8067 GenerateDataProcInstruction(kind,
8068 output,
8069 first,
8070 Operand(second,
8071 ShiftFromOpKind(op_kind),
8072 instruction->GetShiftAmount()),
8073 codegen_);
8074 }
Anton Kirilov74234da2017-01-13 14:42:47 +00008075 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008076 DCHECK_EQ(instruction->GetType(), DataType::Type::kInt64);
Anton Kirilov74234da2017-01-13 14:42:47 +00008077
8078 if (HDataProcWithShifterOp::IsExtensionOp(op_kind)) {
8079 const vixl32::Register second = InputRegisterAt(instruction, 1);
8080
8081 DCHECK(!LowRegisterFrom(locations->Out()).Is(second));
8082 GenerateDataProc(kind,
8083 locations->Out(),
8084 locations->InAt(0),
8085 second,
8086 Operand(second, ShiftType::ASR, 31),
8087 codegen_);
8088 } else {
8089 GenerateLongDataProc(instruction, codegen_);
8090 }
8091 }
8092}
8093
Artem Serov02109dd2016-09-23 17:17:54 +01008094// TODO(VIXL): Remove optimizations in the helper when they are implemented in vixl.
8095void InstructionCodeGeneratorARMVIXL::GenerateAndConst(vixl32::Register out,
8096 vixl32::Register first,
8097 uint32_t value) {
8098 // Optimize special cases for individual halfs of `and-long` (`and` is simplified earlier).
8099 if (value == 0xffffffffu) {
8100 if (!out.Is(first)) {
8101 __ Mov(out, first);
8102 }
8103 return;
8104 }
8105 if (value == 0u) {
8106 __ Mov(out, 0);
8107 return;
8108 }
8109 if (GetAssembler()->ShifterOperandCanHold(AND, value)) {
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00008110 __ And(out, first, value);
8111 } else if (GetAssembler()->ShifterOperandCanHold(BIC, ~value)) {
8112 __ Bic(out, first, ~value);
Artem Serov02109dd2016-09-23 17:17:54 +01008113 } else {
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00008114 DCHECK(IsPowerOfTwo(value + 1));
8115 __ Ubfx(out, first, 0, WhichPowerOf2(value + 1));
Artem Serov02109dd2016-09-23 17:17:54 +01008116 }
8117}
8118
8119// TODO(VIXL): Remove optimizations in the helper when they are implemented in vixl.
8120void InstructionCodeGeneratorARMVIXL::GenerateOrrConst(vixl32::Register out,
8121 vixl32::Register first,
8122 uint32_t value) {
8123 // Optimize special cases for individual halfs of `or-long` (`or` is simplified earlier).
8124 if (value == 0u) {
8125 if (!out.Is(first)) {
8126 __ Mov(out, first);
8127 }
8128 return;
8129 }
8130 if (value == 0xffffffffu) {
8131 __ Mvn(out, 0);
8132 return;
8133 }
8134 if (GetAssembler()->ShifterOperandCanHold(ORR, value)) {
8135 __ Orr(out, first, value);
8136 } else {
8137 DCHECK(GetAssembler()->ShifterOperandCanHold(ORN, ~value));
8138 __ Orn(out, first, ~value);
8139 }
8140}
8141
8142// TODO(VIXL): Remove optimizations in the helper when they are implemented in vixl.
8143void InstructionCodeGeneratorARMVIXL::GenerateEorConst(vixl32::Register out,
8144 vixl32::Register first,
8145 uint32_t value) {
8146 // Optimize special case for individual halfs of `xor-long` (`xor` is simplified earlier).
8147 if (value == 0u) {
8148 if (!out.Is(first)) {
8149 __ Mov(out, first);
8150 }
8151 return;
8152 }
8153 __ Eor(out, first, value);
8154}
8155
Anton Kirilovdda43962016-11-21 19:55:20 +00008156void InstructionCodeGeneratorARMVIXL::GenerateAddLongConst(Location out,
8157 Location first,
8158 uint64_t value) {
8159 vixl32::Register out_low = LowRegisterFrom(out);
8160 vixl32::Register out_high = HighRegisterFrom(out);
8161 vixl32::Register first_low = LowRegisterFrom(first);
8162 vixl32::Register first_high = HighRegisterFrom(first);
8163 uint32_t value_low = Low32Bits(value);
8164 uint32_t value_high = High32Bits(value);
8165 if (value_low == 0u) {
8166 if (!out_low.Is(first_low)) {
8167 __ Mov(out_low, first_low);
8168 }
8169 __ Add(out_high, first_high, value_high);
8170 return;
8171 }
8172 __ Adds(out_low, first_low, value_low);
Vladimir Markof0a6a1d2018-01-08 14:23:56 +00008173 if (GetAssembler()->ShifterOperandCanHold(ADC, value_high)) {
Anton Kirilovdda43962016-11-21 19:55:20 +00008174 __ Adc(out_high, first_high, value_high);
Anton Kirilovdda43962016-11-21 19:55:20 +00008175 } else {
Vladimir Markof0a6a1d2018-01-08 14:23:56 +00008176 DCHECK(GetAssembler()->ShifterOperandCanHold(SBC, ~value_high));
8177 __ Sbc(out_high, first_high, ~value_high);
Anton Kirilovdda43962016-11-21 19:55:20 +00008178 }
8179}
8180
Artem Serov02109dd2016-09-23 17:17:54 +01008181void InstructionCodeGeneratorARMVIXL::HandleBitwiseOperation(HBinaryOperation* instruction) {
8182 LocationSummary* locations = instruction->GetLocations();
8183 Location first = locations->InAt(0);
8184 Location second = locations->InAt(1);
8185 Location out = locations->Out();
8186
8187 if (second.IsConstant()) {
8188 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
8189 uint32_t value_low = Low32Bits(value);
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 out_reg = OutputRegister(instruction);
8193 if (instruction->IsAnd()) {
8194 GenerateAndConst(out_reg, first_reg, value_low);
8195 } else if (instruction->IsOr()) {
8196 GenerateOrrConst(out_reg, first_reg, value_low);
8197 } else {
8198 DCHECK(instruction->IsXor());
8199 GenerateEorConst(out_reg, first_reg, value_low);
8200 }
8201 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008202 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
Artem Serov02109dd2016-09-23 17:17:54 +01008203 uint32_t value_high = High32Bits(value);
8204 vixl32::Register first_low = LowRegisterFrom(first);
8205 vixl32::Register first_high = HighRegisterFrom(first);
8206 vixl32::Register out_low = LowRegisterFrom(out);
8207 vixl32::Register out_high = HighRegisterFrom(out);
8208 if (instruction->IsAnd()) {
8209 GenerateAndConst(out_low, first_low, value_low);
8210 GenerateAndConst(out_high, first_high, value_high);
8211 } else if (instruction->IsOr()) {
8212 GenerateOrrConst(out_low, first_low, value_low);
8213 GenerateOrrConst(out_high, first_high, value_high);
8214 } else {
8215 DCHECK(instruction->IsXor());
8216 GenerateEorConst(out_low, first_low, value_low);
8217 GenerateEorConst(out_high, first_high, value_high);
8218 }
8219 }
8220 return;
8221 }
8222
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008223 if (instruction->GetResultType() == DataType::Type::kInt32) {
Artem Serov02109dd2016-09-23 17:17:54 +01008224 vixl32::Register first_reg = InputRegisterAt(instruction, 0);
8225 vixl32::Register second_reg = InputRegisterAt(instruction, 1);
8226 vixl32::Register out_reg = OutputRegister(instruction);
8227 if (instruction->IsAnd()) {
8228 __ And(out_reg, first_reg, second_reg);
8229 } else if (instruction->IsOr()) {
8230 __ Orr(out_reg, first_reg, second_reg);
8231 } else {
8232 DCHECK(instruction->IsXor());
8233 __ Eor(out_reg, first_reg, second_reg);
8234 }
8235 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008236 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
Artem Serov02109dd2016-09-23 17:17:54 +01008237 vixl32::Register first_low = LowRegisterFrom(first);
8238 vixl32::Register first_high = HighRegisterFrom(first);
8239 vixl32::Register second_low = LowRegisterFrom(second);
8240 vixl32::Register second_high = HighRegisterFrom(second);
8241 vixl32::Register out_low = LowRegisterFrom(out);
8242 vixl32::Register out_high = HighRegisterFrom(out);
8243 if (instruction->IsAnd()) {
8244 __ And(out_low, first_low, second_low);
8245 __ And(out_high, first_high, second_high);
8246 } else if (instruction->IsOr()) {
8247 __ Orr(out_low, first_low, second_low);
8248 __ Orr(out_high, first_high, second_high);
8249 } else {
8250 DCHECK(instruction->IsXor());
8251 __ Eor(out_low, first_low, second_low);
8252 __ Eor(out_high, first_high, second_high);
8253 }
8254 }
8255}
8256
Artem Serovcfbe9132016-10-14 15:58:56 +01008257void InstructionCodeGeneratorARMVIXL::GenerateReferenceLoadOneRegister(
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008258 HInstruction* instruction,
Artem Serovcfbe9132016-10-14 15:58:56 +01008259 Location out,
8260 uint32_t offset,
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008261 Location maybe_temp,
8262 ReadBarrierOption read_barrier_option) {
Artem Serovcfbe9132016-10-14 15:58:56 +01008263 vixl32::Register out_reg = RegisterFrom(out);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008264 if (read_barrier_option == kWithReadBarrier) {
8265 CHECK(kEmitCompilerReadBarrier);
8266 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
8267 if (kUseBakerReadBarrier) {
8268 // Load with fast path based Baker's read barrier.
8269 // /* HeapReference<Object> */ out = *(out + offset)
8270 codegen_->GenerateFieldLoadWithBakerReadBarrier(
8271 instruction, out, out_reg, offset, maybe_temp, /* needs_null_check */ false);
8272 } else {
8273 // Load with slow path based read barrier.
8274 // Save the value of `out` into `maybe_temp` before overwriting it
8275 // in the following move operation, as we will need it for the
8276 // read barrier below.
8277 __ Mov(RegisterFrom(maybe_temp), out_reg);
8278 // /* HeapReference<Object> */ out = *(out + offset)
8279 GetAssembler()->LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
8280 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
8281 }
Artem Serovcfbe9132016-10-14 15:58:56 +01008282 } else {
8283 // Plain load with no read barrier.
8284 // /* HeapReference<Object> */ out = *(out + offset)
8285 GetAssembler()->LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
8286 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
8287 }
8288}
8289
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008290void InstructionCodeGeneratorARMVIXL::GenerateReferenceLoadTwoRegisters(
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008291 HInstruction* instruction,
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008292 Location out,
8293 Location obj,
8294 uint32_t offset,
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008295 Location maybe_temp,
8296 ReadBarrierOption read_barrier_option) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008297 vixl32::Register out_reg = RegisterFrom(out);
8298 vixl32::Register obj_reg = RegisterFrom(obj);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008299 if (read_barrier_option == kWithReadBarrier) {
8300 CHECK(kEmitCompilerReadBarrier);
8301 if (kUseBakerReadBarrier) {
8302 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
8303 // Load with fast path based Baker's read barrier.
8304 // /* HeapReference<Object> */ out = *(obj + offset)
8305 codegen_->GenerateFieldLoadWithBakerReadBarrier(
8306 instruction, out, obj_reg, offset, maybe_temp, /* needs_null_check */ false);
8307 } else {
8308 // Load with slow path based read barrier.
8309 // /* HeapReference<Object> */ out = *(obj + offset)
8310 GetAssembler()->LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
8311 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
8312 }
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008313 } else {
8314 // Plain load with no read barrier.
8315 // /* HeapReference<Object> */ out = *(obj + offset)
8316 GetAssembler()->LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
8317 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
8318 }
8319}
8320
Vladimir Markoca1e0382018-04-11 09:58:41 +00008321void CodeGeneratorARMVIXL::GenerateGcRootFieldLoad(
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008322 HInstruction* instruction,
Scott Wakelinga7812ae2016-10-17 10:03:36 +01008323 Location root,
8324 vixl32::Register obj,
8325 uint32_t offset,
Artem Serovd4cc5b22016-11-04 11:19:09 +00008326 ReadBarrierOption read_barrier_option) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01008327 vixl32::Register root_reg = RegisterFrom(root);
Artem Serovd4cc5b22016-11-04 11:19:09 +00008328 if (read_barrier_option == kWithReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008329 DCHECK(kEmitCompilerReadBarrier);
8330 if (kUseBakerReadBarrier) {
8331 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
Roland Levillainba650a42017-03-06 13:52:32 +00008332 // Baker's read barrier are used.
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008333
Vladimir Marko008e09f32018-08-06 15:42:43 +01008334 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in
8335 // the Marking Register) to decide whether we need to enter
8336 // the slow path to mark the GC root.
8337 //
8338 // We use shared thunks for the slow path; shared within the method
8339 // for JIT, across methods for AOT. That thunk checks the reference
8340 // and jumps to the entrypoint if needed.
8341 //
8342 // lr = &return_address;
8343 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
8344 // if (mr) { // Thread::Current()->GetIsGcMarking()
8345 // goto gc_root_thunk<root_reg>(lr)
8346 // }
8347 // return_address:
Roland Levillainba650a42017-03-06 13:52:32 +00008348
Vladimir Marko008e09f32018-08-06 15:42:43 +01008349 UseScratchRegisterScope temps(GetVIXLAssembler());
8350 temps.Exclude(ip);
8351 bool narrow = CanEmitNarrowLdr(root_reg, obj, offset);
8352 uint32_t custom_data = EncodeBakerReadBarrierGcRootData(root_reg.GetCode(), narrow);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008353
Vladimir Markod887ed82018-08-14 13:52:12 +00008354 size_t narrow_instructions = /* CMP */ (mr.IsLow() ? 1u : 0u) + /* LDR */ (narrow ? 1u : 0u);
8355 size_t wide_instructions = /* ADR+CMP+LDR+BNE */ 4u - narrow_instructions;
8356 size_t exact_size = wide_instructions * vixl32::k32BitT32InstructionSizeInBytes +
8357 narrow_instructions * vixl32::k16BitT32InstructionSizeInBytes;
8358 ExactAssemblyScope guard(GetVIXLAssembler(), exact_size);
Vladimir Marko008e09f32018-08-06 15:42:43 +01008359 vixl32::Label return_address;
8360 EmitAdrCode adr(GetVIXLAssembler(), lr, &return_address);
8361 __ cmp(mr, Operand(0));
8362 // Currently the offset is always within range. If that changes,
8363 // we shall have to split the load the same way as for fields.
8364 DCHECK_LT(offset, kReferenceLoadMinFarOffset);
8365 ptrdiff_t old_offset = GetVIXLAssembler()->GetBuffer()->GetCursorOffset();
8366 __ ldr(EncodingSize(narrow ? Narrow : Wide), root_reg, MemOperand(obj, offset));
8367 EmitBakerReadBarrierBne(custom_data);
Vladimir Markod887ed82018-08-14 13:52:12 +00008368 __ bind(&return_address);
Vladimir Marko008e09f32018-08-06 15:42:43 +01008369 DCHECK_EQ(old_offset - GetVIXLAssembler()->GetBuffer()->GetCursorOffset(),
8370 narrow ? BAKER_MARK_INTROSPECTION_GC_ROOT_LDR_NARROW_OFFSET
8371 : BAKER_MARK_INTROSPECTION_GC_ROOT_LDR_WIDE_OFFSET);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008372 } else {
8373 // GC root loaded through a slow path for read barriers other
8374 // than Baker's.
8375 // /* GcRoot<mirror::Object>* */ root = obj + offset
8376 __ Add(root_reg, obj, offset);
8377 // /* mirror::Object* */ root = root->Read()
Vladimir Markoca1e0382018-04-11 09:58:41 +00008378 GenerateReadBarrierForRootSlow(instruction, root, root);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008379 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01008380 } else {
8381 // Plain GC root load with no read barrier.
8382 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
8383 GetAssembler()->LoadFromOffset(kLoadWord, root_reg, obj, offset);
8384 // Note that GC roots are not affected by heap poisoning, thus we
8385 // do not have to unpoison `root_reg` here.
8386 }
Orion Hodson4c8e12e2018-05-18 08:33:20 +01008387 MaybeGenerateMarkingRegisterCheck(/* code */ 19);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01008388}
8389
Vladimir Markod887ed82018-08-14 13:52:12 +00008390void CodeGeneratorARMVIXL::GenerateUnsafeCasOldValueAddWithBakerReadBarrier(
8391 vixl::aarch32::Register old_value,
8392 vixl::aarch32::Register adjusted_old_value,
8393 vixl::aarch32::Register expected) {
8394 DCHECK(kEmitCompilerReadBarrier);
8395 DCHECK(kUseBakerReadBarrier);
8396
8397 // Similar to the Baker RB path in GenerateGcRootFieldLoad(), with an ADD instead of LDR.
8398 uint32_t custom_data = EncodeBakerReadBarrierUnsafeCasData(old_value.GetCode());
8399
8400 size_t narrow_instructions = /* CMP */ (mr.IsLow() ? 1u : 0u);
8401 size_t wide_instructions = /* ADR+CMP+ADD+BNE */ 4u - narrow_instructions;
8402 size_t exact_size = wide_instructions * vixl32::k32BitT32InstructionSizeInBytes +
8403 narrow_instructions * vixl32::k16BitT32InstructionSizeInBytes;
8404 ExactAssemblyScope guard(GetVIXLAssembler(), exact_size);
8405 vixl32::Label return_address;
8406 EmitAdrCode adr(GetVIXLAssembler(), lr, &return_address);
8407 __ cmp(mr, Operand(0));
8408 ptrdiff_t old_offset = GetVIXLAssembler()->GetBuffer()->GetCursorOffset();
8409 __ add(EncodingSize(Wide), old_value, adjusted_old_value, Operand(expected)); // Preserves flags.
8410 EmitBakerReadBarrierBne(custom_data);
8411 __ bind(&return_address);
8412 DCHECK_EQ(old_offset - GetVIXLAssembler()->GetBuffer()->GetCursorOffset(),
8413 BAKER_MARK_INTROSPECTION_UNSAFE_CAS_ADD_OFFSET);
8414}
8415
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008416void CodeGeneratorARMVIXL::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
8417 Location ref,
8418 vixl32::Register obj,
Vladimir Marko248141f2018-08-10 10:40:07 +01008419 const vixl32::MemOperand& src,
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008420 bool needs_null_check) {
8421 DCHECK(kEmitCompilerReadBarrier);
8422 DCHECK(kUseBakerReadBarrier);
8423
Vladimir Marko008e09f32018-08-06 15:42:43 +01008424 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in the
8425 // Marking Register) to decide whether we need to enter the slow
8426 // path to mark the reference. Then, in the slow path, check the
8427 // gray bit in the lock word of the reference's holder (`obj`) to
8428 // decide whether to mark `ref` or not.
8429 //
8430 // We use shared thunks for the slow path; shared within the method
8431 // for JIT, across methods for AOT. That thunk checks the holder
8432 // and jumps to the entrypoint if needed. If the holder is not gray,
8433 // it creates a fake dependency and returns to the LDR instruction.
8434 //
8435 // lr = &gray_return_address;
8436 // if (mr) { // Thread::Current()->GetIsGcMarking()
8437 // goto field_thunk<holder_reg, base_reg>(lr)
8438 // }
8439 // not_gray_return_address:
8440 // // Original reference load. If the offset is too large to fit
8441 // // into LDR, we use an adjusted base register here.
8442 // HeapReference<mirror::Object> reference = *(obj+offset);
8443 // gray_return_address:
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008444
Vladimir Marko248141f2018-08-10 10:40:07 +01008445 DCHECK(src.GetAddrMode() == vixl32::Offset);
8446 DCHECK_ALIGNED(src.GetOffsetImmediate(), sizeof(mirror::HeapReference<mirror::Object>));
Vladimir Marko008e09f32018-08-06 15:42:43 +01008447 vixl32::Register ref_reg = RegisterFrom(ref, DataType::Type::kReference);
Vladimir Marko248141f2018-08-10 10:40:07 +01008448 bool narrow = CanEmitNarrowLdr(ref_reg, src.GetBaseRegister(), src.GetOffsetImmediate());
8449
Vladimir Marko008e09f32018-08-06 15:42:43 +01008450 UseScratchRegisterScope temps(GetVIXLAssembler());
8451 temps.Exclude(ip);
Vladimir Marko248141f2018-08-10 10:40:07 +01008452 uint32_t custom_data =
8453 EncodeBakerReadBarrierFieldData(src.GetBaseRegister().GetCode(), obj.GetCode(), narrow);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008454
Vladimir Marko008e09f32018-08-06 15:42:43 +01008455 {
Vladimir Markod887ed82018-08-14 13:52:12 +00008456 size_t narrow_instructions =
8457 /* CMP */ (mr.IsLow() ? 1u : 0u) +
8458 /* LDR+unpoison? */ (narrow ? (kPoisonHeapReferences ? 2u : 1u) : 0u);
8459 size_t wide_instructions =
8460 /* ADR+CMP+LDR+BNE+unpoison? */ (kPoisonHeapReferences ? 5u : 4u) - narrow_instructions;
8461 size_t exact_size = wide_instructions * vixl32::k32BitT32InstructionSizeInBytes +
8462 narrow_instructions * vixl32::k16BitT32InstructionSizeInBytes;
8463 ExactAssemblyScope guard(GetVIXLAssembler(), exact_size);
Vladimir Marko008e09f32018-08-06 15:42:43 +01008464 vixl32::Label return_address;
8465 EmitAdrCode adr(GetVIXLAssembler(), lr, &return_address);
8466 __ cmp(mr, Operand(0));
8467 EmitBakerReadBarrierBne(custom_data);
8468 ptrdiff_t old_offset = GetVIXLAssembler()->GetBuffer()->GetCursorOffset();
Vladimir Marko248141f2018-08-10 10:40:07 +01008469 __ ldr(EncodingSize(narrow ? Narrow : Wide), ref_reg, src);
Vladimir Marko008e09f32018-08-06 15:42:43 +01008470 if (needs_null_check) {
8471 MaybeRecordImplicitNullCheck(instruction);
8472 }
8473 // Note: We need a specific width for the unpoisoning NEG.
8474 if (kPoisonHeapReferences) {
8475 if (narrow) {
8476 // The only 16-bit encoding is T1 which sets flags outside IT block (i.e. RSBS, not RSB).
8477 __ rsbs(EncodingSize(Narrow), ref_reg, ref_reg, Operand(0));
8478 } else {
8479 __ rsb(EncodingSize(Wide), ref_reg, ref_reg, Operand(0));
8480 }
8481 }
Vladimir Markod887ed82018-08-14 13:52:12 +00008482 __ bind(&return_address);
Vladimir Marko008e09f32018-08-06 15:42:43 +01008483 DCHECK_EQ(old_offset - GetVIXLAssembler()->GetBuffer()->GetCursorOffset(),
8484 narrow ? BAKER_MARK_INTROSPECTION_FIELD_LDR_NARROW_OFFSET
8485 : BAKER_MARK_INTROSPECTION_FIELD_LDR_WIDE_OFFSET);
8486 }
8487 MaybeGenerateMarkingRegisterCheck(/* code */ 20, /* temp_loc */ LocationFrom(ip));
Roland Levillain6070e882016-11-03 17:51:58 +00008488}
8489
Vladimir Marko248141f2018-08-10 10:40:07 +01008490void CodeGeneratorARMVIXL::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
8491 Location ref,
8492 vixl32::Register obj,
8493 uint32_t offset,
8494 Location temp,
8495 bool needs_null_check) {
8496 DCHECK_ALIGNED(offset, sizeof(mirror::HeapReference<mirror::Object>));
8497 vixl32::Register base = obj;
8498 if (offset >= kReferenceLoadMinFarOffset) {
8499 base = RegisterFrom(temp);
8500 static_assert(IsPowerOfTwo(kReferenceLoadMinFarOffset), "Expecting a power of 2.");
8501 __ Add(base, obj, Operand(offset & ~(kReferenceLoadMinFarOffset - 1u)));
8502 offset &= (kReferenceLoadMinFarOffset - 1u);
8503 }
8504 GenerateFieldLoadWithBakerReadBarrier(
8505 instruction, ref, obj, MemOperand(base, offset), needs_null_check);
8506}
8507
Vladimir Marko008e09f32018-08-06 15:42:43 +01008508void CodeGeneratorARMVIXL::GenerateArrayLoadWithBakerReadBarrier(Location ref,
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008509 vixl32::Register obj,
8510 uint32_t data_offset,
8511 Location index,
8512 Location temp,
8513 bool needs_null_check) {
8514 DCHECK(kEmitCompilerReadBarrier);
8515 DCHECK(kUseBakerReadBarrier);
8516
8517 static_assert(
8518 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
8519 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008520 ScaleFactor scale_factor = TIMES_4;
8521
Vladimir Marko008e09f32018-08-06 15:42:43 +01008522 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in the
8523 // Marking Register) to decide whether we need to enter the slow
8524 // path to mark the reference. Then, in the slow path, check the
8525 // gray bit in the lock word of the reference's holder (`obj`) to
8526 // decide whether to mark `ref` or not.
8527 //
8528 // We use shared thunks for the slow path; shared within the method
8529 // for JIT, across methods for AOT. That thunk checks the holder
8530 // and jumps to the entrypoint if needed. If the holder is not gray,
8531 // it creates a fake dependency and returns to the LDR instruction.
8532 //
8533 // lr = &gray_return_address;
8534 // if (mr) { // Thread::Current()->GetIsGcMarking()
8535 // goto array_thunk<base_reg>(lr)
8536 // }
8537 // not_gray_return_address:
8538 // // Original reference load. If the offset is too large to fit
8539 // // into LDR, we use an adjusted base register here.
8540 // HeapReference<mirror::Object> reference = data[index];
8541 // gray_return_address:
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008542
Vladimir Marko008e09f32018-08-06 15:42:43 +01008543 DCHECK(index.IsValid());
8544 vixl32::Register index_reg = RegisterFrom(index, DataType::Type::kInt32);
8545 vixl32::Register ref_reg = RegisterFrom(ref, DataType::Type::kReference);
8546 vixl32::Register data_reg = RegisterFrom(temp, DataType::Type::kInt32); // Raw pointer.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008547
Vladimir Marko008e09f32018-08-06 15:42:43 +01008548 UseScratchRegisterScope temps(GetVIXLAssembler());
8549 temps.Exclude(ip);
8550 uint32_t custom_data = EncodeBakerReadBarrierArrayData(data_reg.GetCode());
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008551
Vladimir Marko008e09f32018-08-06 15:42:43 +01008552 __ Add(data_reg, obj, Operand(data_offset));
8553 {
Vladimir Markod887ed82018-08-14 13:52:12 +00008554 size_t narrow_instructions = /* CMP */ (mr.IsLow() ? 1u : 0u);
8555 size_t wide_instructions =
8556 /* ADR+CMP+BNE+LDR+unpoison? */ (kPoisonHeapReferences ? 5u : 4u) - narrow_instructions;
8557 size_t exact_size = wide_instructions * vixl32::k32BitT32InstructionSizeInBytes +
8558 narrow_instructions * vixl32::k16BitT32InstructionSizeInBytes;
8559 ExactAssemblyScope guard(GetVIXLAssembler(), exact_size);
Vladimir Marko008e09f32018-08-06 15:42:43 +01008560 vixl32::Label return_address;
8561 EmitAdrCode adr(GetVIXLAssembler(), lr, &return_address);
8562 __ cmp(mr, Operand(0));
8563 EmitBakerReadBarrierBne(custom_data);
8564 ptrdiff_t old_offset = GetVIXLAssembler()->GetBuffer()->GetCursorOffset();
8565 __ ldr(ref_reg, MemOperand(data_reg, index_reg, vixl32::LSL, scale_factor));
8566 DCHECK(!needs_null_check); // The thunk cannot handle the null check.
8567 // Note: We need a Wide NEG for the unpoisoning.
8568 if (kPoisonHeapReferences) {
8569 __ rsb(EncodingSize(Wide), ref_reg, ref_reg, Operand(0));
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008570 }
Vladimir Markod887ed82018-08-14 13:52:12 +00008571 __ bind(&return_address);
Vladimir Marko008e09f32018-08-06 15:42:43 +01008572 DCHECK_EQ(old_offset - GetVIXLAssembler()->GetBuffer()->GetCursorOffset(),
8573 BAKER_MARK_INTROSPECTION_ARRAY_LDR_OFFSET);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008574 }
Vladimir Marko008e09f32018-08-06 15:42:43 +01008575 MaybeGenerateMarkingRegisterCheck(/* code */ 21, /* temp_loc */ LocationFrom(ip));
Roland Levillain6070e882016-11-03 17:51:58 +00008576}
8577
Roland Levillain5daa4952017-07-03 17:23:56 +01008578void CodeGeneratorARMVIXL::MaybeGenerateMarkingRegisterCheck(int code, Location temp_loc) {
8579 // The following condition is a compile-time one, so it does not have a run-time cost.
8580 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier && kIsDebugBuild) {
8581 // The following condition is a run-time one; it is executed after the
8582 // previous compile-time test, to avoid penalizing non-debug builds.
8583 if (GetCompilerOptions().EmitRunTimeChecksInDebugMode()) {
8584 UseScratchRegisterScope temps(GetVIXLAssembler());
8585 vixl32::Register temp = temp_loc.IsValid() ? RegisterFrom(temp_loc) : temps.Acquire();
8586 GetAssembler()->GenerateMarkingRegisterCheck(temp,
8587 kMarkingRegisterCheckBreakCodeBaseCode + code);
8588 }
8589 }
8590}
8591
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008592void CodeGeneratorARMVIXL::GenerateReadBarrierSlow(HInstruction* instruction,
8593 Location out,
8594 Location ref,
8595 Location obj,
8596 uint32_t offset,
8597 Location index) {
8598 DCHECK(kEmitCompilerReadBarrier);
8599
8600 // Insert a slow path based read barrier *after* the reference load.
8601 //
8602 // If heap poisoning is enabled, the unpoisoning of the loaded
8603 // reference will be carried out by the runtime within the slow
8604 // path.
8605 //
8606 // Note that `ref` currently does not get unpoisoned (when heap
8607 // poisoning is enabled), which is alright as the `ref` argument is
8608 // not used by the artReadBarrierSlow entry point.
8609 //
8610 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Vladimir Marko174b2e22017-10-12 13:34:49 +01008611 SlowPathCodeARMVIXL* slow_path = new (GetScopedAllocator())
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008612 ReadBarrierForHeapReferenceSlowPathARMVIXL(instruction, out, ref, obj, offset, index);
8613 AddSlowPath(slow_path);
8614
8615 __ B(slow_path->GetEntryLabel());
8616 __ Bind(slow_path->GetExitLabel());
8617}
8618
8619void CodeGeneratorARMVIXL::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
Artem Serov02d37832016-10-25 15:25:33 +01008620 Location out,
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008621 Location ref,
8622 Location obj,
8623 uint32_t offset,
8624 Location index) {
Artem Serov02d37832016-10-25 15:25:33 +01008625 if (kEmitCompilerReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008626 // Baker's read barriers shall be handled by the fast path
Roland Levillain9983e302017-07-14 14:34:22 +01008627 // (CodeGeneratorARMVIXL::GenerateReferenceLoadWithBakerReadBarrier).
Artem Serov02d37832016-10-25 15:25:33 +01008628 DCHECK(!kUseBakerReadBarrier);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008629 // If heap poisoning is enabled, unpoisoning will be taken care of
8630 // by the runtime within the slow path.
8631 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Artem Serov02d37832016-10-25 15:25:33 +01008632 } else if (kPoisonHeapReferences) {
8633 GetAssembler()->UnpoisonHeapReference(RegisterFrom(out));
8634 }
8635}
8636
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008637void CodeGeneratorARMVIXL::GenerateReadBarrierForRootSlow(HInstruction* instruction,
8638 Location out,
8639 Location root) {
8640 DCHECK(kEmitCompilerReadBarrier);
8641
8642 // Insert a slow path based read barrier *after* the GC root load.
8643 //
8644 // Note that GC roots are not affected by heap poisoning, so we do
8645 // not need to do anything special for this here.
8646 SlowPathCodeARMVIXL* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01008647 new (GetScopedAllocator()) ReadBarrierForRootSlowPathARMVIXL(instruction, out, root);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008648 AddSlowPath(slow_path);
8649
8650 __ B(slow_path->GetEntryLabel());
8651 __ Bind(slow_path->GetExitLabel());
8652}
8653
Artem Serov02d37832016-10-25 15:25:33 +01008654// Check if the desired_dispatch_info is supported. If it is, return it,
8655// otherwise return a fall-back info that should be used instead.
8656HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARMVIXL::GetSupportedInvokeStaticOrDirectDispatch(
Artem Serovd4cc5b22016-11-04 11:19:09 +00008657 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffraybdb2ecc2018-09-18 14:33:55 +01008658 ArtMethod* method ATTRIBUTE_UNUSED) {
Nicolas Geoffraye807ff72017-01-23 09:03:12 +00008659 return desired_dispatch_info;
Artem Serov02d37832016-10-25 15:25:33 +01008660}
8661
Scott Wakelinga7812ae2016-10-17 10:03:36 +01008662vixl32::Register CodeGeneratorARMVIXL::GetInvokeStaticOrDirectExtraParameter(
8663 HInvokeStaticOrDirect* invoke, vixl32::Register temp) {
8664 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
8665 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
8666 if (!invoke->GetLocations()->Intrinsified()) {
8667 return RegisterFrom(location);
8668 }
8669 // For intrinsics we allow any location, so it may be on the stack.
8670 if (!location.IsRegister()) {
8671 GetAssembler()->LoadFromOffset(kLoadWord, temp, sp, location.GetStackIndex());
8672 return temp;
8673 }
8674 // For register locations, check if the register was saved. If so, get it from the stack.
8675 // Note: There is a chance that the register was saved but not overwritten, so we could
8676 // save one load. However, since this is just an intrinsic slow path we prefer this
8677 // simple and more robust approach rather that trying to determine if that's the case.
8678 SlowPathCode* slow_path = GetCurrentSlowPath();
Scott Wakelingd5cd4972017-02-03 11:38:35 +00008679 if (slow_path != nullptr && slow_path->IsCoreRegisterSaved(RegisterFrom(location).GetCode())) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01008680 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(RegisterFrom(location).GetCode());
8681 GetAssembler()->LoadFromOffset(kLoadWord, temp, sp, stack_offset);
8682 return temp;
8683 }
8684 return RegisterFrom(location);
8685}
8686
Vladimir Markod254f5c2017-06-02 15:18:36 +00008687void CodeGeneratorARMVIXL::GenerateStaticOrDirectCall(
Vladimir Markoe7197bf2017-06-02 17:00:23 +01008688 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00008689 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01008690 switch (invoke->GetMethodLoadKind()) {
8691 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
8692 uint32_t offset =
8693 GetThreadOffset<kArmPointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
8694 // temp = thread->string_init_entrypoint
Artem Serovd4cc5b22016-11-04 11:19:09 +00008695 GetAssembler()->LoadFromOffset(kLoadWord, RegisterFrom(temp), tr, offset);
8696 break;
8697 }
8698 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
8699 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
8700 break;
Vladimir Marko65979462017-05-19 17:25:12 +01008701 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative: {
8702 DCHECK(GetCompilerOptions().IsBootImage());
Vladimir Marko59eb30f2018-02-20 11:52:34 +00008703 PcRelativePatchInfo* labels = NewBootImageMethodPatch(invoke->GetTargetMethod());
Vladimir Marko65979462017-05-19 17:25:12 +01008704 vixl32::Register temp_reg = RegisterFrom(temp);
8705 EmitMovwMovtPlaceholder(labels, temp_reg);
8706 break;
8707 }
Vladimir Markob066d432018-01-03 13:14:37 +00008708 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageRelRo: {
Vladimir Markoe47f60c2018-02-21 13:43:28 +00008709 uint32_t boot_image_offset = GetBootImageOffset(invoke);
Vladimir Markob066d432018-01-03 13:14:37 +00008710 PcRelativePatchInfo* labels = NewBootImageRelRoPatch(boot_image_offset);
8711 vixl32::Register temp_reg = RegisterFrom(temp);
8712 EmitMovwMovtPlaceholder(labels, temp_reg);
8713 GetAssembler()->LoadFromOffset(kLoadWord, temp_reg, temp_reg, /* offset*/ 0);
8714 break;
8715 }
Vladimir Marko0eb882b2017-05-15 13:39:18 +01008716 case HInvokeStaticOrDirect::MethodLoadKind::kBssEntry: {
8717 PcRelativePatchInfo* labels = NewMethodBssEntryPatch(
8718 MethodReference(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex()));
8719 vixl32::Register temp_reg = RegisterFrom(temp);
8720 EmitMovwMovtPlaceholder(labels, temp_reg);
8721 GetAssembler()->LoadFromOffset(kLoadWord, temp_reg, temp_reg, /* offset*/ 0);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01008722 break;
8723 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01008724 case HInvokeStaticOrDirect::MethodLoadKind::kJitDirectAddress:
8725 __ Mov(RegisterFrom(temp), Operand::From(invoke->GetMethodAddress()));
8726 break;
Vladimir Markoe7197bf2017-06-02 17:00:23 +01008727 case HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall: {
8728 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
8729 return; // No code pointer retrieval; the runtime performs the call directly.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01008730 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01008731 }
8732
Artem Serovd4cc5b22016-11-04 11:19:09 +00008733 switch (invoke->GetCodePtrLocation()) {
8734 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Vladimir Markoe7197bf2017-06-02 17:00:23 +01008735 {
8736 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
8737 ExactAssemblyScope aas(GetVIXLAssembler(),
8738 vixl32::k32BitT32InstructionSizeInBytes,
8739 CodeBufferCheckScope::kMaximumSize);
8740 __ bl(GetFrameEntryLabel());
8741 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
8742 }
Artem Serovd4cc5b22016-11-04 11:19:09 +00008743 break;
Artem Serovd4cc5b22016-11-04 11:19:09 +00008744 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
8745 // LR = callee_method->entry_point_from_quick_compiled_code_
8746 GetAssembler()->LoadFromOffset(
8747 kLoadWord,
8748 lr,
8749 RegisterFrom(callee_method),
8750 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize).Int32Value());
Alexandre Rames374ddf32016-11-04 10:40:49 +00008751 {
Vladimir Markoe7197bf2017-06-02 17:00:23 +01008752 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
Alexandre Rames374ddf32016-11-04 10:40:49 +00008753 // 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 +00008754 ExactAssemblyScope aas(GetVIXLAssembler(),
8755 vixl32::k16BitT32InstructionSizeInBytes,
8756 CodeBufferCheckScope::kExactSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00008757 // LR()
8758 __ blx(lr);
Vladimir Markoe7197bf2017-06-02 17:00:23 +01008759 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Alexandre Rames374ddf32016-11-04 10:40:49 +00008760 }
Artem Serovd4cc5b22016-11-04 11:19:09 +00008761 break;
Scott Wakelinga7812ae2016-10-17 10:03:36 +01008762 }
8763
Scott Wakelinga7812ae2016-10-17 10:03:36 +01008764 DCHECK(!IsLeafMethod());
8765}
8766
Vladimir Markoe7197bf2017-06-02 17:00:23 +01008767void CodeGeneratorARMVIXL::GenerateVirtualCall(
8768 HInvokeVirtual* invoke, Location temp_location, SlowPathCode* slow_path) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01008769 vixl32::Register temp = RegisterFrom(temp_location);
8770 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
8771 invoke->GetVTableIndex(), kArmPointerSize).Uint32Value();
8772
8773 // Use the calling convention instead of the location of the receiver, as
8774 // intrinsics may have put the receiver in a different register. In the intrinsics
8775 // slow path, the arguments have been moved to the right place, so here we are
8776 // guaranteed that the receiver is the first register of the calling convention.
8777 InvokeDexCallingConventionARMVIXL calling_convention;
8778 vixl32::Register receiver = calling_convention.GetRegisterAt(0);
8779 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Alexandre Rames374ddf32016-11-04 10:40:49 +00008780 {
8781 // Make sure the pc is recorded immediately after the `ldr` instruction.
Artem Serov0fb37192016-12-06 18:13:40 +00008782 ExactAssemblyScope aas(GetVIXLAssembler(),
8783 vixl32::kMaxInstructionSizeInBytes,
8784 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00008785 // /* HeapReference<Class> */ temp = receiver->klass_
8786 __ ldr(temp, MemOperand(receiver, class_offset));
8787 MaybeRecordImplicitNullCheck(invoke);
8788 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01008789 // Instead of simply (possibly) unpoisoning `temp` here, we should
8790 // emit a read barrier for the previous class reference load.
8791 // However this is not required in practice, as this is an
8792 // intermediate/temporary reference and because the current
8793 // concurrent copying collector keeps the from-space memory
8794 // intact/accessible until the end of the marking phase (the
8795 // concurrent copying collector may not in the future).
8796 GetAssembler()->MaybeUnpoisonHeapReference(temp);
8797
8798 // temp = temp->GetMethodAt(method_offset);
8799 uint32_t entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(
8800 kArmPointerSize).Int32Value();
8801 GetAssembler()->LoadFromOffset(kLoadWord, temp, temp, method_offset);
8802 // LR = temp->GetEntryPoint();
8803 GetAssembler()->LoadFromOffset(kLoadWord, lr, temp, entry_point);
Vladimir Markoe7197bf2017-06-02 17:00:23 +01008804 {
8805 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
8806 // blx in T32 has only 16bit encoding that's why a stricter check for the scope is used.
8807 ExactAssemblyScope aas(GetVIXLAssembler(),
8808 vixl32::k16BitT32InstructionSizeInBytes,
8809 CodeBufferCheckScope::kExactSize);
8810 // LR();
8811 __ blx(lr);
8812 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
8813 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01008814}
8815
Vladimir Marko6fd16062018-06-26 11:02:04 +01008816CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewBootImageIntrinsicPatch(
8817 uint32_t intrinsic_data) {
8818 return NewPcRelativePatch(/* dex_file */ nullptr, intrinsic_data, &boot_image_intrinsic_patches_);
8819}
8820
Vladimir Markob066d432018-01-03 13:14:37 +00008821CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewBootImageRelRoPatch(
8822 uint32_t boot_image_offset) {
8823 return NewPcRelativePatch(/* dex_file */ nullptr,
8824 boot_image_offset,
8825 &boot_image_method_patches_);
8826}
8827
Vladimir Marko59eb30f2018-02-20 11:52:34 +00008828CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewBootImageMethodPatch(
Vladimir Marko65979462017-05-19 17:25:12 +01008829 MethodReference target_method) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00008830 return NewPcRelativePatch(
8831 target_method.dex_file, target_method.index, &boot_image_method_patches_);
Artem Serovd4cc5b22016-11-04 11:19:09 +00008832}
8833
Vladimir Marko0eb882b2017-05-15 13:39:18 +01008834CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewMethodBssEntryPatch(
8835 MethodReference target_method) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00008836 return NewPcRelativePatch(
8837 target_method.dex_file, target_method.index, &method_bss_entry_patches_);
Vladimir Marko0eb882b2017-05-15 13:39:18 +01008838}
8839
Vladimir Marko59eb30f2018-02-20 11:52:34 +00008840CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewBootImageTypePatch(
Artem Serovd4cc5b22016-11-04 11:19:09 +00008841 const DexFile& dex_file, dex::TypeIndex type_index) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00008842 return NewPcRelativePatch(&dex_file, type_index.index_, &boot_image_type_patches_);
Artem Serovd4cc5b22016-11-04 11:19:09 +00008843}
8844
Vladimir Marko1998cd02017-01-13 13:02:58 +00008845CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewTypeBssEntryPatch(
8846 const DexFile& dex_file, dex::TypeIndex type_index) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00008847 return NewPcRelativePatch(&dex_file, type_index.index_, &type_bss_entry_patches_);
Vladimir Marko1998cd02017-01-13 13:02:58 +00008848}
8849
Vladimir Marko59eb30f2018-02-20 11:52:34 +00008850CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewBootImageStringPatch(
Vladimir Marko65979462017-05-19 17:25:12 +01008851 const DexFile& dex_file, dex::StringIndex string_index) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00008852 return NewPcRelativePatch(&dex_file, string_index.index_, &boot_image_string_patches_);
Vladimir Marko65979462017-05-19 17:25:12 +01008853}
8854
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01008855CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewStringBssEntryPatch(
8856 const DexFile& dex_file, dex::StringIndex string_index) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00008857 return NewPcRelativePatch(&dex_file, string_index.index_, &string_bss_entry_patches_);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01008858}
8859
Artem Serovd4cc5b22016-11-04 11:19:09 +00008860CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewPcRelativePatch(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00008861 const DexFile* dex_file, uint32_t offset_or_index, ArenaDeque<PcRelativePatchInfo>* patches) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00008862 patches->emplace_back(dex_file, offset_or_index);
8863 return &patches->back();
8864}
8865
Vladimir Marko966b46f2018-08-03 10:20:19 +00008866void CodeGeneratorARMVIXL::EmitBakerReadBarrierBne(uint32_t custom_data) {
Vladimir Markod887ed82018-08-14 13:52:12 +00008867 DCHECK(!__ AllowMacroInstructions()); // In ExactAssemblyScope.
Vladimir Marko966b46f2018-08-03 10:20:19 +00008868 if (Runtime::Current()->UseJitCompilation()) {
8869 auto it = jit_baker_read_barrier_slow_paths_.FindOrAdd(custom_data);
8870 vixl::aarch32::Label* slow_path_entry = &it->second.label;
8871 __ b(ne, EncodingSize(Wide), slow_path_entry);
8872 } else {
8873 baker_read_barrier_patches_.emplace_back(custom_data);
8874 vixl::aarch32::Label* patch_label = &baker_read_barrier_patches_.back().label;
8875 __ bind(patch_label);
8876 vixl32::Label placeholder_label;
8877 __ b(ne, EncodingSize(Wide), &placeholder_label); // Placeholder, patched at link-time.
8878 __ bind(&placeholder_label);
8879 }
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008880}
8881
Artem Serovc5fcb442016-12-02 19:19:58 +00008882VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateBootImageAddressLiteral(uint32_t address) {
Vladimir Marko8e524ad2018-07-13 10:27:43 +01008883 return DeduplicateUint32Literal(address, &uint32_literals_);
Artem Serovc5fcb442016-12-02 19:19:58 +00008884}
8885
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00008886VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateJitStringLiteral(
8887 const DexFile& dex_file,
8888 dex::StringIndex string_index,
8889 Handle<mirror::String> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01008890 ReserveJitStringRoot(StringReference(&dex_file, string_index), handle);
Artem Serovc5fcb442016-12-02 19:19:58 +00008891 return jit_string_patches_.GetOrCreate(
8892 StringReference(&dex_file, string_index),
8893 [this]() {
8894 return GetAssembler()->CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u);
8895 });
8896}
8897
8898VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateJitClassLiteral(const DexFile& dex_file,
8899 dex::TypeIndex type_index,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00008900 Handle<mirror::Class> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01008901 ReserveJitClassRoot(TypeReference(&dex_file, type_index), handle);
Artem Serovc5fcb442016-12-02 19:19:58 +00008902 return jit_class_patches_.GetOrCreate(
8903 TypeReference(&dex_file, type_index),
8904 [this]() {
8905 return GetAssembler()->CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u);
8906 });
8907}
8908
Vladimir Marko6fd16062018-06-26 11:02:04 +01008909void CodeGeneratorARMVIXL::LoadBootImageAddress(vixl32::Register reg,
8910 uint32_t boot_image_reference) {
8911 if (GetCompilerOptions().IsBootImage()) {
8912 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
8913 NewBootImageIntrinsicPatch(boot_image_reference);
8914 EmitMovwMovtPlaceholder(labels, reg);
Vladimir Markoa2da9b92018-10-10 14:21:55 +01008915 } else if (GetCompilerOptions().GetCompilePic()) {
Vladimir Marko6fd16062018-06-26 11:02:04 +01008916 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
8917 NewBootImageRelRoPatch(boot_image_reference);
Vladimir Markoeebb8212018-06-05 14:57:24 +01008918 EmitMovwMovtPlaceholder(labels, reg);
8919 __ Ldr(reg, MemOperand(reg, /* offset */ 0));
8920 } else {
Vladimir Marko8e524ad2018-07-13 10:27:43 +01008921 DCHECK(Runtime::Current()->UseJitCompilation());
Vladimir Markoeebb8212018-06-05 14:57:24 +01008922 gc::Heap* heap = Runtime::Current()->GetHeap();
8923 DCHECK(!heap->GetBootImageSpaces().empty());
8924 uintptr_t address =
Vladimir Marko6fd16062018-06-26 11:02:04 +01008925 reinterpret_cast<uintptr_t>(heap->GetBootImageSpaces()[0]->Begin() + boot_image_reference);
Vladimir Markoeebb8212018-06-05 14:57:24 +01008926 __ Ldr(reg, DeduplicateBootImageAddressLiteral(dchecked_integral_cast<uint32_t>(address)));
8927 }
8928}
8929
Vladimir Marko6fd16062018-06-26 11:02:04 +01008930void CodeGeneratorARMVIXL::AllocateInstanceForIntrinsic(HInvokeStaticOrDirect* invoke,
8931 uint32_t boot_image_offset) {
8932 DCHECK(invoke->IsStatic());
8933 InvokeRuntimeCallingConventionARMVIXL calling_convention;
8934 vixl32::Register argument = calling_convention.GetRegisterAt(0);
8935 if (GetCompilerOptions().IsBootImage()) {
8936 DCHECK_EQ(boot_image_offset, IntrinsicVisitor::IntegerValueOfInfo::kInvalidReference);
8937 // Load the class the same way as for HLoadClass::LoadKind::kBootImageLinkTimePcRelative.
8938 MethodReference target_method = invoke->GetTargetMethod();
8939 dex::TypeIndex type_idx = target_method.dex_file->GetMethodId(target_method.index).class_idx_;
8940 PcRelativePatchInfo* labels = NewBootImageTypePatch(*target_method.dex_file, type_idx);
8941 EmitMovwMovtPlaceholder(labels, argument);
8942 } else {
8943 LoadBootImageAddress(argument, boot_image_offset);
8944 }
8945 InvokeRuntime(kQuickAllocObjectInitialized, invoke, invoke->GetDexPc());
8946 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
8947}
8948
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01008949template <linker::LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
Artem Serovd4cc5b22016-11-04 11:19:09 +00008950inline void CodeGeneratorARMVIXL::EmitPcRelativeLinkerPatches(
8951 const ArenaDeque<PcRelativePatchInfo>& infos,
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01008952 ArenaVector<linker::LinkerPatch>* linker_patches) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00008953 for (const PcRelativePatchInfo& info : infos) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00008954 const DexFile* dex_file = info.target_dex_file;
Artem Serovd4cc5b22016-11-04 11:19:09 +00008955 size_t offset_or_index = info.offset_or_index;
8956 DCHECK(info.add_pc_label.IsBound());
8957 uint32_t add_pc_offset = dchecked_integral_cast<uint32_t>(info.add_pc_label.GetLocation());
8958 // Add MOVW patch.
8959 DCHECK(info.movw_label.IsBound());
8960 uint32_t movw_offset = dchecked_integral_cast<uint32_t>(info.movw_label.GetLocation());
Vladimir Marko59eb30f2018-02-20 11:52:34 +00008961 linker_patches->push_back(Factory(movw_offset, dex_file, add_pc_offset, offset_or_index));
Artem Serovd4cc5b22016-11-04 11:19:09 +00008962 // Add MOVT patch.
8963 DCHECK(info.movt_label.IsBound());
8964 uint32_t movt_offset = dchecked_integral_cast<uint32_t>(info.movt_label.GetLocation());
Vladimir Marko59eb30f2018-02-20 11:52:34 +00008965 linker_patches->push_back(Factory(movt_offset, dex_file, add_pc_offset, offset_or_index));
Artem Serovd4cc5b22016-11-04 11:19:09 +00008966 }
8967}
8968
Vladimir Marko6fd16062018-06-26 11:02:04 +01008969template <linker::LinkerPatch (*Factory)(size_t, uint32_t, uint32_t)>
8970linker::LinkerPatch NoDexFileAdapter(size_t literal_offset,
8971 const DexFile* target_dex_file,
8972 uint32_t pc_insn_offset,
8973 uint32_t boot_image_offset) {
8974 DCHECK(target_dex_file == nullptr); // Unused for these patches, should be null.
8975 return Factory(literal_offset, pc_insn_offset, boot_image_offset);
Vladimir Markob066d432018-01-03 13:14:37 +00008976}
8977
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01008978void CodeGeneratorARMVIXL::EmitLinkerPatches(ArenaVector<linker::LinkerPatch>* linker_patches) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00008979 DCHECK(linker_patches->empty());
8980 size_t size =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00008981 /* MOVW+MOVT for each entry */ 2u * boot_image_method_patches_.size() +
Vladimir Marko0eb882b2017-05-15 13:39:18 +01008982 /* MOVW+MOVT for each entry */ 2u * method_bss_entry_patches_.size() +
Vladimir Marko59eb30f2018-02-20 11:52:34 +00008983 /* MOVW+MOVT for each entry */ 2u * boot_image_type_patches_.size() +
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008984 /* MOVW+MOVT for each entry */ 2u * type_bss_entry_patches_.size() +
Vladimir Marko59eb30f2018-02-20 11:52:34 +00008985 /* MOVW+MOVT for each entry */ 2u * boot_image_string_patches_.size() +
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01008986 /* MOVW+MOVT for each entry */ 2u * string_bss_entry_patches_.size() +
Vladimir Marko6fd16062018-06-26 11:02:04 +01008987 /* MOVW+MOVT for each entry */ 2u * boot_image_intrinsic_patches_.size() +
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008988 baker_read_barrier_patches_.size();
Artem Serovd4cc5b22016-11-04 11:19:09 +00008989 linker_patches->reserve(size);
Vladimir Marko65979462017-05-19 17:25:12 +01008990 if (GetCompilerOptions().IsBootImage()) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01008991 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeMethodPatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00008992 boot_image_method_patches_, linker_patches);
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01008993 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeTypePatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00008994 boot_image_type_patches_, linker_patches);
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01008995 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeStringPatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00008996 boot_image_string_patches_, linker_patches);
Vladimir Marko6fd16062018-06-26 11:02:04 +01008997 EmitPcRelativeLinkerPatches<NoDexFileAdapter<linker::LinkerPatch::IntrinsicReferencePatch>>(
8998 boot_image_intrinsic_patches_, linker_patches);
Vladimir Marko65979462017-05-19 17:25:12 +01008999 } else {
Vladimir Marko6fd16062018-06-26 11:02:04 +01009000 EmitPcRelativeLinkerPatches<NoDexFileAdapter<linker::LinkerPatch::DataBimgRelRoPatch>>(
Vladimir Markob066d432018-01-03 13:14:37 +00009001 boot_image_method_patches_, linker_patches);
Vladimir Markoe47f60c2018-02-21 13:43:28 +00009002 DCHECK(boot_image_type_patches_.empty());
9003 DCHECK(boot_image_string_patches_.empty());
Vladimir Marko6fd16062018-06-26 11:02:04 +01009004 DCHECK(boot_image_intrinsic_patches_.empty());
Artem Serovd4cc5b22016-11-04 11:19:09 +00009005 }
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01009006 EmitPcRelativeLinkerPatches<linker::LinkerPatch::MethodBssEntryPatch>(
9007 method_bss_entry_patches_, linker_patches);
9008 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeBssEntryPatch>(
9009 type_bss_entry_patches_, linker_patches);
9010 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringBssEntryPatch>(
9011 string_bss_entry_patches_, linker_patches);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01009012 for (const BakerReadBarrierPatchInfo& info : baker_read_barrier_patches_) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01009013 linker_patches->push_back(linker::LinkerPatch::BakerReadBarrierBranchPatch(
9014 info.label.GetLocation(), info.custom_data));
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01009015 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00009016 DCHECK_EQ(size, linker_patches->size());
Artem Serovc5fcb442016-12-02 19:19:58 +00009017}
9018
Vladimir Markoca1e0382018-04-11 09:58:41 +00009019bool CodeGeneratorARMVIXL::NeedsThunkCode(const linker::LinkerPatch& patch) const {
9020 return patch.GetType() == linker::LinkerPatch::Type::kBakerReadBarrierBranch ||
9021 patch.GetType() == linker::LinkerPatch::Type::kCallRelative;
9022}
9023
9024void CodeGeneratorARMVIXL::EmitThunkCode(const linker::LinkerPatch& patch,
9025 /*out*/ ArenaVector<uint8_t>* code,
9026 /*out*/ std::string* debug_name) {
9027 arm::ArmVIXLAssembler assembler(GetGraph()->GetAllocator());
9028 switch (patch.GetType()) {
9029 case linker::LinkerPatch::Type::kCallRelative:
9030 // The thunk just uses the entry point in the ArtMethod. This works even for calls
9031 // to the generic JNI and interpreter trampolines.
9032 assembler.LoadFromOffset(
9033 arm::kLoadWord,
9034 vixl32::pc,
9035 vixl32::r0,
9036 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize).Int32Value());
9037 assembler.GetVIXLAssembler()->Bkpt(0);
9038 if (GetCompilerOptions().GenerateAnyDebugInfo()) {
9039 *debug_name = "MethodCallThunk";
9040 }
9041 break;
9042 case linker::LinkerPatch::Type::kBakerReadBarrierBranch:
9043 DCHECK_EQ(patch.GetBakerCustomValue2(), 0u);
9044 CompileBakerReadBarrierThunk(assembler, patch.GetBakerCustomValue1(), debug_name);
9045 break;
9046 default:
9047 LOG(FATAL) << "Unexpected patch type " << patch.GetType();
9048 UNREACHABLE();
9049 }
9050
9051 // Ensure we emit the literal pool if any.
9052 assembler.FinalizeCode();
9053 code->resize(assembler.CodeSize());
9054 MemoryRegion code_region(code->data(), code->size());
9055 assembler.FinalizeInstructions(code_region);
9056}
9057
Artem Serovc5fcb442016-12-02 19:19:58 +00009058VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateUint32Literal(
9059 uint32_t value,
9060 Uint32ToLiteralMap* map) {
9061 return map->GetOrCreate(
9062 value,
9063 [this, value]() {
9064 return GetAssembler()->CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ value);
9065 });
9066}
9067
Artem Serov2bbc9532016-10-21 11:51:50 +01009068void LocationsBuilderARMVIXL::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
9069 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01009070 new (GetGraph()->GetAllocator()) LocationSummary(instr, LocationSummary::kNoCall);
Artem Serov2bbc9532016-10-21 11:51:50 +01009071 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
9072 Location::RequiresRegister());
9073 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
9074 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
9075 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
9076}
9077
9078void InstructionCodeGeneratorARMVIXL::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
9079 vixl32::Register res = OutputRegister(instr);
9080 vixl32::Register accumulator =
9081 InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
9082 vixl32::Register mul_left =
9083 InputRegisterAt(instr, HMultiplyAccumulate::kInputMulLeftIndex);
9084 vixl32::Register mul_right =
9085 InputRegisterAt(instr, HMultiplyAccumulate::kInputMulRightIndex);
9086
9087 if (instr->GetOpKind() == HInstruction::kAdd) {
9088 __ Mla(res, mul_left, mul_right, accumulator);
9089 } else {
9090 __ Mls(res, mul_left, mul_right, accumulator);
9091 }
9092}
9093
Artem Serov551b28f2016-10-18 19:11:30 +01009094void LocationsBuilderARMVIXL::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
9095 // Nothing to do, this should be removed during prepare for register allocator.
9096 LOG(FATAL) << "Unreachable";
9097}
9098
9099void InstructionCodeGeneratorARMVIXL::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
9100 // Nothing to do, this should be removed during prepare for register allocator.
9101 LOG(FATAL) << "Unreachable";
9102}
9103
9104// Simple implementation of packed switch - generate cascaded compare/jumps.
9105void LocationsBuilderARMVIXL::VisitPackedSwitch(HPackedSwitch* switch_instr) {
9106 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01009107 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Artem Serov551b28f2016-10-18 19:11:30 +01009108 locations->SetInAt(0, Location::RequiresRegister());
9109 if (switch_instr->GetNumEntries() > kPackedSwitchCompareJumpThreshold &&
9110 codegen_->GetAssembler()->GetVIXLAssembler()->IsUsingT32()) {
9111 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the table base.
9112 if (switch_instr->GetStartValue() != 0) {
9113 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the bias.
9114 }
9115 }
9116}
9117
9118// TODO(VIXL): Investigate and reach the parity with old arm codegen.
9119void InstructionCodeGeneratorARMVIXL::VisitPackedSwitch(HPackedSwitch* switch_instr) {
9120 int32_t lower_bound = switch_instr->GetStartValue();
9121 uint32_t num_entries = switch_instr->GetNumEntries();
9122 LocationSummary* locations = switch_instr->GetLocations();
9123 vixl32::Register value_reg = InputRegisterAt(switch_instr, 0);
9124 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
9125
9126 if (num_entries <= kPackedSwitchCompareJumpThreshold ||
9127 !codegen_->GetAssembler()->GetVIXLAssembler()->IsUsingT32()) {
9128 // Create a series of compare/jumps.
Anton Kirilovedb2ac32016-11-30 15:14:10 +00009129 UseScratchRegisterScope temps(GetVIXLAssembler());
Artem Serov551b28f2016-10-18 19:11:30 +01009130 vixl32::Register temp_reg = temps.Acquire();
9131 // Note: It is fine for the below AddConstantSetFlags() using IP register to temporarily store
9132 // the immediate, because IP is used as the destination register. For the other
9133 // AddConstantSetFlags() and GenerateCompareWithImmediate(), the immediate values are constant,
9134 // and they can be encoded in the instruction without making use of IP register.
9135 __ Adds(temp_reg, value_reg, -lower_bound);
9136
9137 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
9138 // Jump to successors[0] if value == lower_bound.
9139 __ B(eq, codegen_->GetLabelOf(successors[0]));
9140 int32_t last_index = 0;
9141 for (; num_entries - last_index > 2; last_index += 2) {
9142 __ Adds(temp_reg, temp_reg, -2);
9143 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
9144 __ B(lo, codegen_->GetLabelOf(successors[last_index + 1]));
9145 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
9146 __ B(eq, codegen_->GetLabelOf(successors[last_index + 2]));
9147 }
9148 if (num_entries - last_index == 2) {
9149 // The last missing case_value.
9150 __ Cmp(temp_reg, 1);
9151 __ B(eq, codegen_->GetLabelOf(successors[last_index + 1]));
9152 }
9153
9154 // And the default for any other value.
9155 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
9156 __ B(codegen_->GetLabelOf(default_block));
9157 }
9158 } else {
9159 // Create a table lookup.
9160 vixl32::Register table_base = RegisterFrom(locations->GetTemp(0));
9161
9162 JumpTableARMVIXL* jump_table = codegen_->CreateJumpTable(switch_instr);
9163
9164 // Remove the bias.
9165 vixl32::Register key_reg;
9166 if (lower_bound != 0) {
9167 key_reg = RegisterFrom(locations->GetTemp(1));
9168 __ Sub(key_reg, value_reg, lower_bound);
9169 } else {
9170 key_reg = value_reg;
9171 }
9172
9173 // Check whether the value is in the table, jump to default block if not.
9174 __ Cmp(key_reg, num_entries - 1);
9175 __ B(hi, codegen_->GetLabelOf(default_block));
9176
Anton Kirilovedb2ac32016-11-30 15:14:10 +00009177 UseScratchRegisterScope temps(GetVIXLAssembler());
Artem Serov551b28f2016-10-18 19:11:30 +01009178 vixl32::Register jump_offset = temps.Acquire();
9179
9180 // Load jump offset from the table.
Scott Wakeling86e9d262017-01-18 15:59:24 +00009181 {
9182 const size_t jump_size = switch_instr->GetNumEntries() * sizeof(int32_t);
9183 ExactAssemblyScope aas(GetVIXLAssembler(),
9184 (vixl32::kMaxInstructionSizeInBytes * 4) + jump_size,
9185 CodeBufferCheckScope::kMaximumSize);
9186 __ adr(table_base, jump_table->GetTableStartLabel());
9187 __ ldr(jump_offset, MemOperand(table_base, key_reg, vixl32::LSL, 2));
Artem Serov551b28f2016-10-18 19:11:30 +01009188
Scott Wakeling86e9d262017-01-18 15:59:24 +00009189 // Jump to target block by branching to table_base(pc related) + offset.
9190 vixl32::Register target_address = table_base;
9191 __ add(target_address, table_base, jump_offset);
9192 __ bx(target_address);
Artem Serov09a940d2016-11-11 16:15:11 +00009193
Scott Wakeling86e9d262017-01-18 15:59:24 +00009194 jump_table->EmitTable(codegen_);
9195 }
Artem Serov551b28f2016-10-18 19:11:30 +01009196 }
9197}
9198
Artem Serov02d37832016-10-25 15:25:33 +01009199// Copy the result of a call into the given target.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009200void CodeGeneratorARMVIXL::MoveFromReturnRegister(Location trg, DataType::Type type) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01009201 if (!trg.IsValid()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009202 DCHECK_EQ(type, DataType::Type::kVoid);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01009203 return;
9204 }
9205
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009206 DCHECK_NE(type, DataType::Type::kVoid);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01009207
Artem Serovd4cc5b22016-11-04 11:19:09 +00009208 Location return_loc = InvokeDexCallingConventionVisitorARMVIXL().GetReturnLocation(type);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01009209 if (return_loc.Equals(trg)) {
9210 return;
9211 }
9212
9213 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
9214 // with the last branch.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009215 if (type == DataType::Type::kInt64) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01009216 TODO_VIXL32(FATAL);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009217 } else if (type == DataType::Type::kFloat64) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01009218 TODO_VIXL32(FATAL);
9219 } else {
9220 // Let the parallel move resolver take care of all of this.
Vladimir Markoca6fff82017-10-03 14:49:14 +01009221 HParallelMove parallel_move(GetGraph()->GetAllocator());
Anton Kirilove28d9ae2016-10-25 18:17:23 +01009222 parallel_move.AddMove(return_loc, trg, type, nullptr);
9223 GetMoveResolver()->EmitNativeCode(&parallel_move);
9224 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01009225}
Scott Wakelingfe885462016-09-22 10:24:38 +01009226
xueliang.zhong8d2c4592016-11-23 17:05:25 +00009227void LocationsBuilderARMVIXL::VisitClassTableGet(HClassTableGet* instruction) {
9228 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01009229 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
xueliang.zhong8d2c4592016-11-23 17:05:25 +00009230 locations->SetInAt(0, Location::RequiresRegister());
9231 locations->SetOut(Location::RequiresRegister());
Artem Serov551b28f2016-10-18 19:11:30 +01009232}
9233
xueliang.zhong8d2c4592016-11-23 17:05:25 +00009234void InstructionCodeGeneratorARMVIXL::VisitClassTableGet(HClassTableGet* instruction) {
9235 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
9236 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
9237 instruction->GetIndex(), kArmPointerSize).SizeValue();
9238 GetAssembler()->LoadFromOffset(kLoadWord,
9239 OutputRegister(instruction),
9240 InputRegisterAt(instruction, 0),
9241 method_offset);
9242 } else {
9243 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
9244 instruction->GetIndex(), kArmPointerSize));
9245 GetAssembler()->LoadFromOffset(kLoadWord,
9246 OutputRegister(instruction),
9247 InputRegisterAt(instruction, 0),
9248 mirror::Class::ImtPtrOffset(kArmPointerSize).Uint32Value());
9249 GetAssembler()->LoadFromOffset(kLoadWord,
9250 OutputRegister(instruction),
9251 OutputRegister(instruction),
9252 method_offset);
9253 }
Artem Serov551b28f2016-10-18 19:11:30 +01009254}
9255
Artem Serovc5fcb442016-12-02 19:19:58 +00009256static void PatchJitRootUse(uint8_t* code,
9257 const uint8_t* roots_data,
9258 VIXLUInt32Literal* literal,
9259 uint64_t index_in_table) {
9260 DCHECK(literal->IsBound());
9261 uint32_t literal_offset = literal->GetLocation();
9262 uintptr_t address =
9263 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
9264 uint8_t* data = code + literal_offset;
9265 reinterpret_cast<uint32_t*>(data)[0] = dchecked_integral_cast<uint32_t>(address);
9266}
9267
9268void CodeGeneratorARMVIXL::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
9269 for (const auto& entry : jit_string_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01009270 const StringReference& string_reference = entry.first;
9271 VIXLUInt32Literal* table_entry_literal = entry.second;
Vladimir Marko174b2e22017-10-12 13:34:49 +01009272 uint64_t index_in_table = GetJitStringRootIndex(string_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01009273 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Artem Serovc5fcb442016-12-02 19:19:58 +00009274 }
9275 for (const auto& entry : jit_class_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01009276 const TypeReference& type_reference = entry.first;
9277 VIXLUInt32Literal* table_entry_literal = entry.second;
Vladimir Marko174b2e22017-10-12 13:34:49 +01009278 uint64_t index_in_table = GetJitClassRootIndex(type_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01009279 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Artem Serovc5fcb442016-12-02 19:19:58 +00009280 }
9281}
9282
Artem Serovd4cc5b22016-11-04 11:19:09 +00009283void CodeGeneratorARMVIXL::EmitMovwMovtPlaceholder(
9284 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels,
9285 vixl32::Register out) {
Artem Serov0fb37192016-12-06 18:13:40 +00009286 ExactAssemblyScope aas(GetVIXLAssembler(),
9287 3 * vixl32::kMaxInstructionSizeInBytes,
9288 CodeBufferCheckScope::kMaximumSize);
Artem Serovd4cc5b22016-11-04 11:19:09 +00009289 // TODO(VIXL): Think about using mov instead of movw.
9290 __ bind(&labels->movw_label);
9291 __ movw(out, /* placeholder */ 0u);
9292 __ bind(&labels->movt_label);
9293 __ movt(out, /* placeholder */ 0u);
9294 __ bind(&labels->add_pc_label);
9295 __ add(out, out, pc);
9296}
9297
Scott Wakelingfe885462016-09-22 10:24:38 +01009298#undef __
9299#undef QUICK_ENTRY_POINT
9300#undef TODO_VIXL32
9301
Vladimir Markoca1e0382018-04-11 09:58:41 +00009302#define __ assembler.GetVIXLAssembler()->
9303
9304static void EmitGrayCheckAndFastPath(ArmVIXLAssembler& assembler,
9305 vixl32::Register base_reg,
9306 vixl32::MemOperand& lock_word,
9307 vixl32::Label* slow_path,
Vladimir Marko7a695052018-04-12 10:26:50 +01009308 int32_t raw_ldr_offset,
9309 vixl32::Label* throw_npe = nullptr) {
Vladimir Markoca1e0382018-04-11 09:58:41 +00009310 // Load the lock word containing the rb_state.
9311 __ Ldr(ip, lock_word);
9312 // Given the numeric representation, it's enough to check the low bit of the rb_state.
Roland Levillain14e5a292018-06-28 12:00:56 +01009313 static_assert(ReadBarrier::NonGrayState() == 0, "Expecting non-gray to have value 0");
Vladimir Markoca1e0382018-04-11 09:58:41 +00009314 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
9315 __ Tst(ip, Operand(LockWord::kReadBarrierStateMaskShifted));
9316 __ B(ne, slow_path, /* is_far_target */ false);
Vladimir Marko7a695052018-04-12 10:26:50 +01009317 // To throw NPE, we return to the fast path; the artificial dependence below does not matter.
9318 if (throw_npe != nullptr) {
9319 __ Bind(throw_npe);
9320 }
Vladimir Markoca1e0382018-04-11 09:58:41 +00009321 __ Add(lr, lr, raw_ldr_offset);
9322 // Introduce a dependency on the lock_word including rb_state,
9323 // to prevent load-load reordering, and without using
9324 // a memory barrier (which would be more expensive).
9325 __ Add(base_reg, base_reg, Operand(ip, LSR, 32));
9326 __ Bx(lr); // And return back to the function.
9327 // Note: The fake dependency is unnecessary for the slow path.
9328}
9329
9330// Load the read barrier introspection entrypoint in register `entrypoint`
Vladimir Markodcd117e2018-04-19 11:54:00 +01009331static vixl32::Register LoadReadBarrierMarkIntrospectionEntrypoint(ArmVIXLAssembler& assembler) {
Vladimir Markoca1e0382018-04-11 09:58:41 +00009332 // The register where the read barrier introspection entrypoint is loaded
Vladimir Markodcd117e2018-04-19 11:54:00 +01009333 // is the marking register. We clobber it here and the entrypoint restores it to 1.
9334 vixl32::Register entrypoint = mr;
Vladimir Markoca1e0382018-04-11 09:58:41 +00009335 // entrypoint = Thread::Current()->pReadBarrierMarkReg12, i.e. pReadBarrierMarkIntrospection.
9336 DCHECK_EQ(ip.GetCode(), 12u);
9337 const int32_t entry_point_offset =
9338 Thread::ReadBarrierMarkEntryPointsOffset<kArmPointerSize>(ip.GetCode());
9339 __ Ldr(entrypoint, MemOperand(tr, entry_point_offset));
Vladimir Markodcd117e2018-04-19 11:54:00 +01009340 return entrypoint;
Vladimir Markoca1e0382018-04-11 09:58:41 +00009341}
9342
9343void CodeGeneratorARMVIXL::CompileBakerReadBarrierThunk(ArmVIXLAssembler& assembler,
9344 uint32_t encoded_data,
9345 /*out*/ std::string* debug_name) {
9346 BakerReadBarrierKind kind = BakerReadBarrierKindField::Decode(encoded_data);
9347 switch (kind) {
9348 case BakerReadBarrierKind::kField: {
Vladimir Markoca1e0382018-04-11 09:58:41 +00009349 vixl32::Register base_reg(BakerReadBarrierFirstRegField::Decode(encoded_data));
9350 CheckValidReg(base_reg.GetCode());
9351 vixl32::Register holder_reg(BakerReadBarrierSecondRegField::Decode(encoded_data));
9352 CheckValidReg(holder_reg.GetCode());
9353 BakerReadBarrierWidth width = BakerReadBarrierWidthField::Decode(encoded_data);
9354 UseScratchRegisterScope temps(assembler.GetVIXLAssembler());
9355 temps.Exclude(ip);
Vladimir Marko7a695052018-04-12 10:26:50 +01009356 // If base_reg differs from holder_reg, the offset was too large and we must have emitted
9357 // an explicit null check before the load. Otherwise, for implicit null checks, we need to
9358 // null-check the holder as we do not necessarily do that check before going to the thunk.
9359 vixl32::Label throw_npe_label;
9360 vixl32::Label* throw_npe = nullptr;
9361 if (GetCompilerOptions().GetImplicitNullChecks() && holder_reg.Is(base_reg)) {
9362 throw_npe = &throw_npe_label;
9363 __ CompareAndBranchIfZero(holder_reg, throw_npe, /* is_far_target */ false);
Vladimir Markoca1e0382018-04-11 09:58:41 +00009364 }
Vladimir Marko7a695052018-04-12 10:26:50 +01009365 // Check if the holder is gray and, if not, add fake dependency to the base register
9366 // and return to the LDR instruction to load the reference. Otherwise, use introspection
9367 // to load the reference and call the entrypoint that performs further checks on the
9368 // reference and marks it if needed.
Vladimir Markoca1e0382018-04-11 09:58:41 +00009369 vixl32::Label slow_path;
9370 MemOperand lock_word(holder_reg, mirror::Object::MonitorOffset().Int32Value());
9371 const int32_t raw_ldr_offset = (width == BakerReadBarrierWidth::kWide)
9372 ? BAKER_MARK_INTROSPECTION_FIELD_LDR_WIDE_OFFSET
9373 : BAKER_MARK_INTROSPECTION_FIELD_LDR_NARROW_OFFSET;
Vladimir Marko7a695052018-04-12 10:26:50 +01009374 EmitGrayCheckAndFastPath(
9375 assembler, base_reg, lock_word, &slow_path, raw_ldr_offset, throw_npe);
Vladimir Markoca1e0382018-04-11 09:58:41 +00009376 __ Bind(&slow_path);
9377 const int32_t ldr_offset = /* Thumb state adjustment (LR contains Thumb state). */ -1 +
9378 raw_ldr_offset;
Vladimir Markodcd117e2018-04-19 11:54:00 +01009379 vixl32::Register ep_reg = LoadReadBarrierMarkIntrospectionEntrypoint(assembler);
Vladimir Markoca1e0382018-04-11 09:58:41 +00009380 if (width == BakerReadBarrierWidth::kWide) {
9381 MemOperand ldr_half_address(lr, ldr_offset + 2);
9382 __ Ldrh(ip, ldr_half_address); // Load the LDR immediate half-word with "Rt | imm12".
9383 __ Ubfx(ip, ip, 0, 12); // Extract the offset imm12.
9384 __ Ldr(ip, MemOperand(base_reg, ip)); // Load the reference.
9385 } else {
9386 MemOperand ldr_address(lr, ldr_offset);
9387 __ Ldrh(ip, ldr_address); // Load the LDR immediate, encoding T1.
9388 __ Add(ep_reg, // Adjust the entrypoint address to the entrypoint
9389 ep_reg, // for narrow LDR.
9390 Operand(BAKER_MARK_INTROSPECTION_FIELD_LDR_NARROW_ENTRYPOINT_OFFSET));
9391 __ Ubfx(ip, ip, 6, 5); // Extract the imm5, i.e. offset / 4.
9392 __ Ldr(ip, MemOperand(base_reg, ip, LSL, 2)); // Load the reference.
9393 }
9394 // Do not unpoison. With heap poisoning enabled, the entrypoint expects a poisoned reference.
9395 __ Bx(ep_reg); // Jump to the entrypoint.
Vladimir Markoca1e0382018-04-11 09:58:41 +00009396 break;
9397 }
9398 case BakerReadBarrierKind::kArray: {
9399 vixl32::Register base_reg(BakerReadBarrierFirstRegField::Decode(encoded_data));
9400 CheckValidReg(base_reg.GetCode());
9401 DCHECK_EQ(kBakerReadBarrierInvalidEncodedReg,
9402 BakerReadBarrierSecondRegField::Decode(encoded_data));
9403 DCHECK(BakerReadBarrierWidthField::Decode(encoded_data) == BakerReadBarrierWidth::kWide);
9404 UseScratchRegisterScope temps(assembler.GetVIXLAssembler());
9405 temps.Exclude(ip);
9406 vixl32::Label slow_path;
9407 int32_t data_offset =
9408 mirror::Array::DataOffset(Primitive::ComponentSize(Primitive::kPrimNot)).Int32Value();
9409 MemOperand lock_word(base_reg, mirror::Object::MonitorOffset().Int32Value() - data_offset);
9410 DCHECK_LT(lock_word.GetOffsetImmediate(), 0);
9411 const int32_t raw_ldr_offset = BAKER_MARK_INTROSPECTION_ARRAY_LDR_OFFSET;
9412 EmitGrayCheckAndFastPath(assembler, base_reg, lock_word, &slow_path, raw_ldr_offset);
9413 __ Bind(&slow_path);
9414 const int32_t ldr_offset = /* Thumb state adjustment (LR contains Thumb state). */ -1 +
9415 raw_ldr_offset;
9416 MemOperand ldr_address(lr, ldr_offset + 2);
9417 __ Ldrb(ip, ldr_address); // Load the LDR (register) byte with "00 | imm2 | Rm",
9418 // i.e. Rm+32 because the scale in imm2 is 2.
Vladimir Markodcd117e2018-04-19 11:54:00 +01009419 vixl32::Register ep_reg = LoadReadBarrierMarkIntrospectionEntrypoint(assembler);
Vladimir Markoca1e0382018-04-11 09:58:41 +00009420 __ Bfi(ep_reg, ip, 3, 6); // Insert ip to the entrypoint address to create
9421 // a switch case target based on the index register.
9422 __ Mov(ip, base_reg); // Move the base register to ip0.
9423 __ Bx(ep_reg); // Jump to the entrypoint's array switch case.
9424 break;
9425 }
Vladimir Markod887ed82018-08-14 13:52:12 +00009426 case BakerReadBarrierKind::kGcRoot:
9427 case BakerReadBarrierKind::kUnsafeCas: {
Vladimir Markoca1e0382018-04-11 09:58:41 +00009428 // Check if the reference needs to be marked and if so (i.e. not null, not marked yet
9429 // and it does not have a forwarding address), call the correct introspection entrypoint;
9430 // otherwise return the reference (or the extracted forwarding address).
9431 // There is no gray bit check for GC roots.
9432 vixl32::Register root_reg(BakerReadBarrierFirstRegField::Decode(encoded_data));
9433 CheckValidReg(root_reg.GetCode());
9434 DCHECK_EQ(kBakerReadBarrierInvalidEncodedReg,
9435 BakerReadBarrierSecondRegField::Decode(encoded_data));
9436 BakerReadBarrierWidth width = BakerReadBarrierWidthField::Decode(encoded_data);
9437 UseScratchRegisterScope temps(assembler.GetVIXLAssembler());
9438 temps.Exclude(ip);
9439 vixl32::Label return_label, not_marked, forwarding_address;
9440 __ CompareAndBranchIfZero(root_reg, &return_label, /* is_far_target */ false);
9441 MemOperand lock_word(root_reg, mirror::Object::MonitorOffset().Int32Value());
9442 __ Ldr(ip, lock_word);
9443 __ Tst(ip, LockWord::kMarkBitStateMaskShifted);
9444 __ B(eq, &not_marked);
9445 __ Bind(&return_label);
9446 __ Bx(lr);
9447 __ Bind(&not_marked);
9448 static_assert(LockWord::kStateShift == 30 && LockWord::kStateForwardingAddress == 3,
9449 "To use 'CMP ip, #modified-immediate; BHS', we need the lock word state in "
9450 " the highest bits and the 'forwarding address' state to have all bits set");
9451 __ Cmp(ip, Operand(0xc0000000));
9452 __ B(hs, &forwarding_address);
Vladimir Markodcd117e2018-04-19 11:54:00 +01009453 vixl32::Register ep_reg = LoadReadBarrierMarkIntrospectionEntrypoint(assembler);
Vladimir Markoca1e0382018-04-11 09:58:41 +00009454 // Adjust the art_quick_read_barrier_mark_introspection address in kBakerCcEntrypointRegister
Vladimir Markod887ed82018-08-14 13:52:12 +00009455 // to one of art_quick_read_barrier_mark_introspection_{gc_roots_{wide,narrow},unsafe_cas}.
9456 DCHECK(kind != BakerReadBarrierKind::kUnsafeCas || width == BakerReadBarrierWidth::kWide);
9457 int32_t entrypoint_offset =
9458 (kind == BakerReadBarrierKind::kGcRoot)
9459 ? (width == BakerReadBarrierWidth::kWide)
9460 ? BAKER_MARK_INTROSPECTION_GC_ROOT_LDR_WIDE_ENTRYPOINT_OFFSET
9461 : BAKER_MARK_INTROSPECTION_GC_ROOT_LDR_NARROW_ENTRYPOINT_OFFSET
9462 : BAKER_MARK_INTROSPECTION_UNSAFE_CAS_ENTRYPOINT_OFFSET;
Vladimir Markoca1e0382018-04-11 09:58:41 +00009463 __ Add(ep_reg, ep_reg, Operand(entrypoint_offset));
9464 __ Mov(ip, root_reg);
9465 __ Bx(ep_reg);
9466 __ Bind(&forwarding_address);
9467 __ Lsl(root_reg, ip, LockWord::kForwardingAddressShift);
9468 __ Bx(lr);
9469 break;
9470 }
9471 default:
9472 LOG(FATAL) << "Unexpected kind: " << static_cast<uint32_t>(kind);
9473 UNREACHABLE();
9474 }
9475
Vladimir Marko966b46f2018-08-03 10:20:19 +00009476 // For JIT, the slow path is considered part of the compiled method,
9477 // so JIT should pass null as `debug_name`. Tests may not have a runtime.
9478 DCHECK(Runtime::Current() == nullptr ||
9479 !Runtime::Current()->UseJitCompilation() ||
9480 debug_name == nullptr);
9481 if (debug_name != nullptr && GetCompilerOptions().GenerateAnyDebugInfo()) {
Vladimir Markoca1e0382018-04-11 09:58:41 +00009482 std::ostringstream oss;
9483 oss << "BakerReadBarrierThunk";
9484 switch (kind) {
9485 case BakerReadBarrierKind::kField:
9486 oss << "Field";
9487 if (BakerReadBarrierWidthField::Decode(encoded_data) == BakerReadBarrierWidth::kWide) {
9488 oss << "Wide";
9489 }
9490 oss << "_r" << BakerReadBarrierFirstRegField::Decode(encoded_data)
9491 << "_r" << BakerReadBarrierSecondRegField::Decode(encoded_data);
9492 break;
9493 case BakerReadBarrierKind::kArray:
9494 oss << "Array_r" << BakerReadBarrierFirstRegField::Decode(encoded_data);
9495 DCHECK_EQ(kBakerReadBarrierInvalidEncodedReg,
9496 BakerReadBarrierSecondRegField::Decode(encoded_data));
9497 DCHECK(BakerReadBarrierWidthField::Decode(encoded_data) == BakerReadBarrierWidth::kWide);
9498 break;
9499 case BakerReadBarrierKind::kGcRoot:
9500 oss << "GcRoot";
9501 if (BakerReadBarrierWidthField::Decode(encoded_data) == BakerReadBarrierWidth::kWide) {
9502 oss << "Wide";
9503 }
9504 oss << "_r" << BakerReadBarrierFirstRegField::Decode(encoded_data);
9505 DCHECK_EQ(kBakerReadBarrierInvalidEncodedReg,
9506 BakerReadBarrierSecondRegField::Decode(encoded_data));
9507 break;
Vladimir Markod887ed82018-08-14 13:52:12 +00009508 case BakerReadBarrierKind::kUnsafeCas:
9509 oss << "UnsafeCas_r" << BakerReadBarrierFirstRegField::Decode(encoded_data);
9510 DCHECK_EQ(kBakerReadBarrierInvalidEncodedReg,
9511 BakerReadBarrierSecondRegField::Decode(encoded_data));
9512 DCHECK(BakerReadBarrierWidthField::Decode(encoded_data) == BakerReadBarrierWidth::kWide);
9513 break;
Vladimir Markoca1e0382018-04-11 09:58:41 +00009514 }
9515 *debug_name = oss.str();
9516 }
9517}
9518
9519#undef __
9520
Scott Wakelingfe885462016-09-22 10:24:38 +01009521} // namespace arm
9522} // namespace art